Initial pass at Go language support.
[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 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
60d5a603 64#include <ctype.h>
4c2df51b 65
c906108c
SS
66#include <fcntl.h>
67#include "gdb_string.h"
4bdf3d34 68#include "gdb_assert.h"
c906108c 69#include <sys/types.h>
233a11ab
CS
70#ifdef HAVE_ZLIB_H
71#include <zlib.h>
72#endif
dce234bc
PP
73#ifdef HAVE_MMAP
74#include <sys/mman.h>
85d9bd0e
TT
75#ifndef MAP_FAILED
76#define MAP_FAILED ((void *) -1)
77#endif
dce234bc 78#endif
d8151005 79
34eaf542
TT
80typedef struct symbol *symbolp;
81DEF_VEC_P (symbolp);
82
d97bc12b
DE
83/* When non-zero, dump DIEs after they are read in. */
84static int dwarf2_die_debug = 0;
85
900e11f9
JK
86/* When non-zero, cross-check physname against demangler. */
87static int check_physname = 0;
88
481860b3
GB
89/* When non-zero, do not reject deprecated .gdb_index sections. */
90int use_deprecated_index_sections = 0;
91
dce234bc
PP
92static int pagesize;
93
df8a16a1
DJ
94/* When set, the file that we're processing is known to have debugging
95 info for C++ namespaces. GCC 3.3.x did not produce this information,
96 but later versions do. */
97
98static int processing_has_namespace_info;
99
6502dd73
DJ
100static const struct objfile_data *dwarf2_objfile_data_key;
101
dce234bc
PP
102struct dwarf2_section_info
103{
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
b315ab21
TG
107 /* Not NULL if the section was actually mmapped. */
108 void *map_addr;
109 /* Page aligned size of mmapped area. */
110 bfd_size_type map_len;
be391dca
TT
111 /* True if we have tried to read this section. */
112 int readin;
dce234bc
PP
113};
114
8b70b953
TT
115typedef struct dwarf2_section_info dwarf2_section_info_def;
116DEF_VEC_O (dwarf2_section_info_def);
117
9291a0cd
TT
118/* All offsets in the index are of this type. It must be
119 architecture-independent. */
120typedef uint32_t offset_type;
121
122DEF_VEC_I (offset_type);
123
124/* A description of the mapped index. The file format is described in
125 a comment by the code that writes the index. */
126struct mapped_index
127{
559a7a62
JK
128 /* Index data format version. */
129 int version;
130
9291a0cd
TT
131 /* The total length of the buffer. */
132 off_t total_size;
b11b1f88 133
9291a0cd
TT
134 /* A pointer to the address table data. */
135 const gdb_byte *address_table;
b11b1f88 136
9291a0cd
TT
137 /* Size of the address table data in bytes. */
138 offset_type address_table_size;
b11b1f88 139
3876f04e
DE
140 /* The symbol table, implemented as a hash table. */
141 const offset_type *symbol_table;
b11b1f88 142
9291a0cd 143 /* Size in slots, each slot is 2 offset_types. */
3876f04e 144 offset_type symbol_table_slots;
b11b1f88 145
9291a0cd
TT
146 /* A pointer to the constant pool. */
147 const char *constant_pool;
148};
149
9cdd5dbd
DE
150/* Collection of data recorded per objfile.
151 This hangs off of dwarf2_objfile_data_key. */
152
6502dd73
DJ
153struct dwarf2_per_objfile
154{
dce234bc
PP
155 struct dwarf2_section_info info;
156 struct dwarf2_section_info abbrev;
157 struct dwarf2_section_info line;
dce234bc
PP
158 struct dwarf2_section_info loc;
159 struct dwarf2_section_info macinfo;
cf2c3c16 160 struct dwarf2_section_info macro;
dce234bc
PP
161 struct dwarf2_section_info str;
162 struct dwarf2_section_info ranges;
163 struct dwarf2_section_info frame;
164 struct dwarf2_section_info eh_frame;
9291a0cd 165 struct dwarf2_section_info gdb_index;
ae038cb0 166
8b70b953
TT
167 VEC (dwarf2_section_info_def) *types;
168
be391dca
TT
169 /* Back link. */
170 struct objfile *objfile;
171
d467dd73 172 /* Table of all the compilation units. This is used to locate
10b3939b 173 the target compilation unit of a particular reference. */
ae038cb0
DJ
174 struct dwarf2_per_cu_data **all_comp_units;
175
176 /* The number of compilation units in ALL_COMP_UNITS. */
177 int n_comp_units;
178
1fd400ff 179 /* The number of .debug_types-related CUs. */
d467dd73 180 int n_type_units;
1fd400ff 181
d467dd73
DE
182 /* The .debug_types-related CUs (TUs). */
183 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 184
ae038cb0
DJ
185 /* A chain of compilation units that are currently read in, so that
186 they can be freed later. */
187 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 188
348e048f
DE
189 /* A table mapping .debug_types signatures to its signatured_type entry.
190 This is NULL if the .debug_types section hasn't been read in yet. */
191 htab_t signatured_types;
192
72dca2f5
FR
193 /* A flag indicating wether this objfile has a section loaded at a
194 VMA of 0. */
195 int has_section_at_zero;
9291a0cd 196
ae2de4f8
DE
197 /* True if we are using the mapped index,
198 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
199 unsigned char using_index;
200
ae2de4f8 201 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 202 struct mapped_index *index_table;
98bfdba5 203
7b9f3c50
DE
204 /* When using index_table, this keeps track of all quick_file_names entries.
205 TUs can share line table entries with CUs or other TUs, and there can be
206 a lot more TUs than unique line tables, so we maintain a separate table
207 of all line table entries to support the sharing. */
208 htab_t quick_file_names_table;
209
98bfdba5
PA
210 /* Set during partial symbol reading, to prevent queueing of full
211 symbols. */
212 int reading_partial_symbols;
673bfd45
DE
213
214 /* Table mapping type .debug_info DIE offsets to types.
215 This is NULL if not allocated yet.
216 It (currently) makes sense to allocate debug_types_type_hash lazily.
217 To keep things simple we allocate both lazily. */
218 htab_t debug_info_type_hash;
219
b64f50a1 220 /* Table mapping type .debug_types DIE sect_offset to types.
673bfd45
DE
221 This is NULL if not allocated yet. */
222 htab_t debug_types_type_hash;
6502dd73
DJ
223};
224
225static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 226
251d32d9 227/* Default names of the debugging sections. */
c906108c 228
233a11ab
CS
229/* Note that if the debugging section has been compressed, it might
230 have a name like .zdebug_info. */
231
9cdd5dbd
DE
232static const struct dwarf2_debug_sections dwarf2_elf_names =
233{
251d32d9
TG
234 { ".debug_info", ".zdebug_info" },
235 { ".debug_abbrev", ".zdebug_abbrev" },
236 { ".debug_line", ".zdebug_line" },
237 { ".debug_loc", ".zdebug_loc" },
238 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 239 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
240 { ".debug_str", ".zdebug_str" },
241 { ".debug_ranges", ".zdebug_ranges" },
242 { ".debug_types", ".zdebug_types" },
243 { ".debug_frame", ".zdebug_frame" },
244 { ".eh_frame", NULL },
24d3216f
TT
245 { ".gdb_index", ".zgdb_index" },
246 23
251d32d9 247};
c906108c
SS
248
249/* local data types */
250
0963b4bd 251/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
252#ifndef ABBREV_HASH_SIZE
253#define ABBREV_HASH_SIZE 121
254#endif
255
107d2387
AC
256/* The data in a compilation unit header, after target2host
257 translation, looks like this. */
c906108c 258struct comp_unit_head
a738430d 259{
c764a876 260 unsigned int length;
a738430d 261 short version;
a738430d
MK
262 unsigned char addr_size;
263 unsigned char signed_addr_p;
b64f50a1 264 sect_offset abbrev_offset;
57349743 265
a738430d
MK
266 /* Size of file offsets; either 4 or 8. */
267 unsigned int offset_size;
57349743 268
a738430d
MK
269 /* Size of the length field; either 4 or 12. */
270 unsigned int initial_length_size;
57349743 271
a738430d
MK
272 /* Offset to the first byte of this compilation unit header in the
273 .debug_info section, for resolving relative reference dies. */
b64f50a1 274 sect_offset offset;
57349743 275
d00adf39
DE
276 /* Offset to first die in this cu from the start of the cu.
277 This will be the first byte following the compilation unit header. */
b64f50a1 278 cu_offset first_die_offset;
a738430d 279};
c906108c 280
3da10d80
KS
281/* Type used for delaying computation of method physnames.
282 See comments for compute_delayed_physnames. */
283struct delayed_method_info
284{
285 /* The type to which the method is attached, i.e., its parent class. */
286 struct type *type;
287
288 /* The index of the method in the type's function fieldlists. */
289 int fnfield_index;
290
291 /* The index of the method in the fieldlist. */
292 int index;
293
294 /* The name of the DIE. */
295 const char *name;
296
297 /* The DIE associated with this method. */
298 struct die_info *die;
299};
300
301typedef struct delayed_method_info delayed_method_info;
302DEF_VEC_O (delayed_method_info);
303
e7c27a73
DJ
304/* Internal state when decoding a particular compilation unit. */
305struct dwarf2_cu
306{
307 /* The objfile containing this compilation unit. */
308 struct objfile *objfile;
309
d00adf39 310 /* The header of the compilation unit. */
e7c27a73 311 struct comp_unit_head header;
e142c38c 312
d00adf39
DE
313 /* Base address of this compilation unit. */
314 CORE_ADDR base_address;
315
316 /* Non-zero if base_address has been set. */
317 int base_known;
318
e142c38c
DJ
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492 341
b64f50a1
JK
342 /* Hash table holding all the loaded partial DIEs
343 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
344 htab_t partial_dies;
345
346 /* Storage for things with the same lifetime as this read-in compilation
347 unit, including partial DIEs. */
348 struct obstack comp_unit_obstack;
349
ae038cb0
DJ
350 /* When multiple dwarf2_cu structures are living in memory, this field
351 chains them all together, so that they can be released efficiently.
352 We will probably also want a generation counter so that most-recently-used
353 compilation units are cached... */
354 struct dwarf2_per_cu_data *read_in_chain;
355
356 /* Backchain to our per_cu entry if the tree has been built. */
357 struct dwarf2_per_cu_data *per_cu;
358
359 /* How many compilation units ago was this CU last referenced? */
360 int last_used;
361
b64f50a1
JK
362 /* A hash table of DIE cu_offset for following references with
363 die_info->offset.sect_off as hash. */
51545339 364 htab_t die_hash;
10b3939b
DJ
365
366 /* Full DIEs if read in. */
367 struct die_info *dies;
368
369 /* A set of pointers to dwarf2_per_cu_data objects for compilation
370 units referenced by this one. Only set during full symbol processing;
371 partial symbol tables do not have dependencies. */
372 htab_t dependencies;
373
cb1df416
DJ
374 /* Header data from the line table, during full symbol processing. */
375 struct line_header *line_header;
376
3da10d80
KS
377 /* A list of methods which need to have physnames computed
378 after all type information has been read. */
379 VEC (delayed_method_info) *method_list;
380
96408a79
SA
381 /* To be copied to symtab->call_site_htab. */
382 htab_t call_site_htab;
383
ae038cb0
DJ
384 /* Mark used when releasing cached dies. */
385 unsigned int mark : 1;
386
8be455d7
JK
387 /* This CU references .debug_loc. See the symtab->locations_valid field.
388 This test is imperfect as there may exist optimized debug code not using
389 any location list and still facing inlining issues if handled as
390 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 391 unsigned int has_loclist : 1;
ba919b58
TT
392
393 /* These cache the results of producer_is_gxx_lt_4_6.
394 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
395 information is cached because profiling CU expansion showed
396 excessive time spent in producer_is_gxx_lt_4_6. */
397 unsigned int checked_producer : 1;
398 unsigned int producer_is_gxx_lt_4_6 : 1;
e7c27a73
DJ
399};
400
10b3939b
DJ
401/* Persistent data held for a compilation unit, even when not
402 processing it. We put a pointer to this structure in the
28dee7f5 403 read_symtab_private field of the psymtab. */
10b3939b 404
ae038cb0
DJ
405struct dwarf2_per_cu_data
406{
348e048f 407 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 408 bytes should suffice to store the length of any compilation unit
45452591
DE
409 - if it doesn't, GDB will fall over anyway.
410 NOTE: Unlike comp_unit_head.length, this length includes
411 initial_length_size. */
b64f50a1 412 sect_offset offset;
348e048f 413 unsigned int length : 29;
ae038cb0
DJ
414
415 /* Flag indicating this compilation unit will be read in before
416 any of the current compilation units are processed. */
c764a876 417 unsigned int queued : 1;
ae038cb0 418
0d99eb77
DE
419 /* This flag will be set when reading partial DIEs if we need to load
420 absolutely all DIEs for this compilation unit, instead of just the ones
421 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
422 hash table and don't find it. */
423 unsigned int load_all_dies : 1;
424
8b70b953
TT
425 /* Non-null if this CU is from .debug_types; in which case it points
426 to the section. Otherwise it's from .debug_info. */
b0df02fd 427 struct dwarf2_section_info *debug_types_section;
348e048f 428
17ea53c3
JK
429 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
430 of the CU cache it gets reset to NULL again. */
ae038cb0 431 struct dwarf2_cu *cu;
1c379e20 432
9cdd5dbd
DE
433 /* The corresponding objfile.
434 Normally we can get the objfile from dwarf2_per_objfile.
435 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
436 struct objfile *objfile;
437
438 /* When using partial symbol tables, the 'psymtab' field is active.
439 Otherwise the 'quick' field is active. */
440 union
441 {
442 /* The partial symbol table associated with this compilation unit,
443 or NULL for partial units (which do not have an associated
444 symtab). */
445 struct partial_symtab *psymtab;
446
447 /* Data needed by the "quick" functions. */
448 struct dwarf2_per_cu_quick_data *quick;
449 } v;
ae038cb0
DJ
450};
451
348e048f
DE
452/* Entry in the signatured_types hash table. */
453
454struct signatured_type
455{
456 ULONGEST signature;
457
b64f50a1
JK
458 /* Offset in this TU of the type defined by this TU. */
459 cu_offset type_offset;
348e048f
DE
460
461 /* The CU(/TU) of this type. */
462 struct dwarf2_per_cu_data per_cu;
463};
464
0963b4bd
MS
465/* Struct used to pass misc. parameters to read_die_and_children, et
466 al. which are used for both .debug_info and .debug_types dies.
467 All parameters here are unchanging for the life of the call. This
468 struct exists to abstract away the constant parameters of die
469 reading. */
93311388
DE
470
471struct die_reader_specs
472{
473 /* The bfd of this objfile. */
474 bfd* abfd;
475
476 /* The CU of the DIE we are parsing. */
477 struct dwarf2_cu *cu;
478
479 /* Pointer to start of section buffer.
480 This is either the start of .debug_info or .debug_types. */
481 const gdb_byte *buffer;
482};
483
debd256d
JB
484/* The line number information for a compilation unit (found in the
485 .debug_line section) begins with a "statement program header",
486 which contains the following information. */
487struct line_header
488{
489 unsigned int total_length;
490 unsigned short version;
491 unsigned int header_length;
492 unsigned char minimum_instruction_length;
2dc7f7b3 493 unsigned char maximum_ops_per_instruction;
debd256d
JB
494 unsigned char default_is_stmt;
495 int line_base;
496 unsigned char line_range;
497 unsigned char opcode_base;
498
499 /* standard_opcode_lengths[i] is the number of operands for the
500 standard opcode whose value is i. This means that
501 standard_opcode_lengths[0] is unused, and the last meaningful
502 element is standard_opcode_lengths[opcode_base - 1]. */
503 unsigned char *standard_opcode_lengths;
504
505 /* The include_directories table. NOTE! These strings are not
506 allocated with xmalloc; instead, they are pointers into
507 debug_line_buffer. If you try to free them, `free' will get
508 indigestion. */
509 unsigned int num_include_dirs, include_dirs_size;
510 char **include_dirs;
511
512 /* The file_names table. NOTE! These strings are not allocated
513 with xmalloc; instead, they are pointers into debug_line_buffer.
514 Don't try to free them directly. */
515 unsigned int num_file_names, file_names_size;
516 struct file_entry
c906108c 517 {
debd256d
JB
518 char *name;
519 unsigned int dir_index;
520 unsigned int mod_time;
521 unsigned int length;
aaa75496 522 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 523 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
524 } *file_names;
525
526 /* The start and end of the statement program following this
6502dd73 527 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 528 gdb_byte *statement_program_start, *statement_program_end;
debd256d 529};
c906108c
SS
530
531/* When we construct a partial symbol table entry we only
0963b4bd 532 need this much information. */
c906108c
SS
533struct partial_die_info
534 {
72bf9492 535 /* Offset of this DIE. */
b64f50a1 536 sect_offset offset;
72bf9492
DJ
537
538 /* DWARF-2 tag for this DIE. */
539 ENUM_BITFIELD(dwarf_tag) tag : 16;
540
72bf9492
DJ
541 /* Assorted flags describing the data found in this DIE. */
542 unsigned int has_children : 1;
543 unsigned int is_external : 1;
544 unsigned int is_declaration : 1;
545 unsigned int has_type : 1;
546 unsigned int has_specification : 1;
547 unsigned int has_pc_info : 1;
481860b3 548 unsigned int may_be_inlined : 1;
72bf9492
DJ
549
550 /* Flag set if the SCOPE field of this structure has been
551 computed. */
552 unsigned int scope_set : 1;
553
fa4028e9
JB
554 /* Flag set if the DIE has a byte_size attribute. */
555 unsigned int has_byte_size : 1;
556
98bfdba5
PA
557 /* Flag set if any of the DIE's children are template arguments. */
558 unsigned int has_template_arguments : 1;
559
abc72ce4
DE
560 /* Flag set if fixup_partial_die has been called on this die. */
561 unsigned int fixup_called : 1;
562
72bf9492 563 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 564 sometimes a default name for unnamed DIEs. */
c906108c 565 char *name;
72bf9492 566
abc72ce4
DE
567 /* The linkage name, if present. */
568 const char *linkage_name;
569
72bf9492
DJ
570 /* The scope to prepend to our children. This is generally
571 allocated on the comp_unit_obstack, so will disappear
572 when this compilation unit leaves the cache. */
573 char *scope;
574
575 /* The location description associated with this DIE, if any. */
576 struct dwarf_block *locdesc;
577
578 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
579 CORE_ADDR lowpc;
580 CORE_ADDR highpc;
72bf9492 581
93311388 582 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 583 DW_AT_sibling, if any. */
abc72ce4
DE
584 /* NOTE: This member isn't strictly necessary, read_partial_die could
585 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 586 gdb_byte *sibling;
72bf9492
DJ
587
588 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
589 DW_AT_specification (or DW_AT_abstract_origin or
590 DW_AT_extension). */
b64f50a1 591 sect_offset spec_offset;
72bf9492
DJ
592
593 /* Pointers to this DIE's parent, first child, and next sibling,
594 if any. */
595 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
596 };
597
0963b4bd 598/* This data structure holds the information of an abbrev. */
c906108c
SS
599struct abbrev_info
600 {
601 unsigned int number; /* number identifying abbrev */
602 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
603 unsigned short has_children; /* boolean */
604 unsigned short num_attrs; /* number of attributes */
c906108c
SS
605 struct attr_abbrev *attrs; /* an array of attribute descriptions */
606 struct abbrev_info *next; /* next in chain */
607 };
608
609struct attr_abbrev
610 {
9d25dd43
DE
611 ENUM_BITFIELD(dwarf_attribute) name : 16;
612 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
613 };
614
0963b4bd 615/* Attributes have a name and a value. */
b60c80d6
DJ
616struct attribute
617 {
9d25dd43 618 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
619 ENUM_BITFIELD(dwarf_form) form : 15;
620
621 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
622 field should be in u.str (existing only for DW_STRING) but it is kept
623 here for better struct attribute alignment. */
624 unsigned int string_is_canonical : 1;
625
b60c80d6
DJ
626 union
627 {
628 char *str;
629 struct dwarf_block *blk;
43bbcdc2
PH
630 ULONGEST unsnd;
631 LONGEST snd;
b60c80d6 632 CORE_ADDR addr;
348e048f 633 struct signatured_type *signatured_type;
b60c80d6
DJ
634 }
635 u;
636 };
637
0963b4bd 638/* This data structure holds a complete die structure. */
c906108c
SS
639struct die_info
640 {
76815b17
DE
641 /* DWARF-2 tag for this DIE. */
642 ENUM_BITFIELD(dwarf_tag) tag : 16;
643
644 /* Number of attributes */
98bfdba5
PA
645 unsigned char num_attrs;
646
647 /* True if we're presently building the full type name for the
648 type derived from this DIE. */
649 unsigned char building_fullname : 1;
76815b17
DE
650
651 /* Abbrev number */
652 unsigned int abbrev;
653
93311388 654 /* Offset in .debug_info or .debug_types section. */
b64f50a1 655 sect_offset offset;
78ba4af6
JB
656
657 /* The dies in a compilation unit form an n-ary tree. PARENT
658 points to this die's parent; CHILD points to the first child of
659 this node; and all the children of a given node are chained
4950bc1c 660 together via their SIBLING fields. */
639d11d3
DC
661 struct die_info *child; /* Its first child, if any. */
662 struct die_info *sibling; /* Its next sibling, if any. */
663 struct die_info *parent; /* Its parent, if any. */
c906108c 664
b60c80d6
DJ
665 /* An array of attributes, with NUM_ATTRS elements. There may be
666 zero, but it's not common and zero-sized arrays are not
667 sufficiently portable C. */
668 struct attribute attrs[1];
c906108c
SS
669 };
670
0963b4bd 671/* Get at parts of an attribute structure. */
c906108c
SS
672
673#define DW_STRING(attr) ((attr)->u.str)
8285870a 674#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
675#define DW_UNSND(attr) ((attr)->u.unsnd)
676#define DW_BLOCK(attr) ((attr)->u.blk)
677#define DW_SND(attr) ((attr)->u.snd)
678#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 679#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 680
0963b4bd 681/* Blocks are a bunch of untyped bytes. */
c906108c
SS
682struct dwarf_block
683 {
684 unsigned int size;
1d6edc3c
JK
685
686 /* Valid only if SIZE is not zero. */
fe1b8b76 687 gdb_byte *data;
c906108c
SS
688 };
689
c906108c
SS
690#ifndef ATTR_ALLOC_CHUNK
691#define ATTR_ALLOC_CHUNK 4
692#endif
693
c906108c
SS
694/* Allocate fields for structs, unions and enums in this size. */
695#ifndef DW_FIELD_ALLOC_CHUNK
696#define DW_FIELD_ALLOC_CHUNK 4
697#endif
698
c906108c
SS
699/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
700 but this would require a corresponding change in unpack_field_as_long
701 and friends. */
702static int bits_per_byte = 8;
703
704/* The routines that read and process dies for a C struct or C++ class
705 pass lists of data member fields and lists of member function fields
706 in an instance of a field_info structure, as defined below. */
707struct field_info
c5aa993b 708 {
0963b4bd 709 /* List of data member and baseclasses fields. */
c5aa993b
JM
710 struct nextfield
711 {
712 struct nextfield *next;
713 int accessibility;
714 int virtuality;
715 struct field field;
716 }
7d0ccb61 717 *fields, *baseclasses;
c906108c 718
7d0ccb61 719 /* Number of fields (including baseclasses). */
c5aa993b 720 int nfields;
c906108c 721
c5aa993b
JM
722 /* Number of baseclasses. */
723 int nbaseclasses;
c906108c 724
c5aa993b
JM
725 /* Set if the accesibility of one of the fields is not public. */
726 int non_public_fields;
c906108c 727
c5aa993b
JM
728 /* Member function fields array, entries are allocated in the order they
729 are encountered in the object file. */
730 struct nextfnfield
731 {
732 struct nextfnfield *next;
733 struct fn_field fnfield;
734 }
735 *fnfields;
c906108c 736
c5aa993b
JM
737 /* Member function fieldlist array, contains name of possibly overloaded
738 member function, number of overloaded member functions and a pointer
739 to the head of the member function field chain. */
740 struct fnfieldlist
741 {
742 char *name;
743 int length;
744 struct nextfnfield *head;
745 }
746 *fnfieldlists;
c906108c 747
c5aa993b
JM
748 /* Number of entries in the fnfieldlists array. */
749 int nfnfields;
98751a41
JK
750
751 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
752 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
753 struct typedef_field_list
754 {
755 struct typedef_field field;
756 struct typedef_field_list *next;
757 }
758 *typedef_field_list;
759 unsigned typedef_field_list_count;
c5aa993b 760 };
c906108c 761
10b3939b
DJ
762/* One item on the queue of compilation units to read in full symbols
763 for. */
764struct dwarf2_queue_item
765{
766 struct dwarf2_per_cu_data *per_cu;
767 struct dwarf2_queue_item *next;
768};
769
770/* The current queue. */
771static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
772
ae038cb0
DJ
773/* Loaded secondary compilation units are kept in memory until they
774 have not been referenced for the processing of this many
775 compilation units. Set this to zero to disable caching. Cache
776 sizes of up to at least twenty will improve startup time for
777 typical inter-CU-reference binaries, at an obvious memory cost. */
778static int dwarf2_max_cache_age = 5;
920d2a44
AC
779static void
780show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
781 struct cmd_list_element *c, const char *value)
782{
3e43a32a
MS
783 fprintf_filtered (file, _("The upper bound on the age of cached "
784 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
785 value);
786}
787
ae038cb0 788
0963b4bd 789/* Various complaints about symbol reading that don't abort the process. */
c906108c 790
4d3c2250
KB
791static void
792dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 793{
4d3c2250 794 complaint (&symfile_complaints,
e2e0b3e5 795 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
796}
797
25e43795
DJ
798static void
799dwarf2_debug_line_missing_file_complaint (void)
800{
801 complaint (&symfile_complaints,
802 _(".debug_line section has line data without a file"));
803}
804
59205f5a
JB
805static void
806dwarf2_debug_line_missing_end_sequence_complaint (void)
807{
808 complaint (&symfile_complaints,
3e43a32a
MS
809 _(".debug_line section has line "
810 "program sequence without an end"));
59205f5a
JB
811}
812
4d3c2250
KB
813static void
814dwarf2_complex_location_expr_complaint (void)
2e276125 815{
e2e0b3e5 816 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
817}
818
4d3c2250
KB
819static void
820dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
821 int arg3)
2e276125 822{
4d3c2250 823 complaint (&symfile_complaints,
3e43a32a
MS
824 _("const value length mismatch for '%s', got %d, expected %d"),
825 arg1, arg2, arg3);
4d3c2250
KB
826}
827
828static void
cf2c3c16 829dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 830{
4d3c2250 831 complaint (&symfile_complaints,
cf2c3c16
TT
832 _("macro info runs off end of `%s' section"),
833 section->asection->name);
4d3c2250
KB
834}
835
836static void
837dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 838{
4d3c2250 839 complaint (&symfile_complaints,
3e43a32a
MS
840 _("macro debug info contains a "
841 "malformed macro definition:\n`%s'"),
4d3c2250
KB
842 arg1);
843}
844
845static void
846dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 847{
4d3c2250 848 complaint (&symfile_complaints,
3e43a32a
MS
849 _("invalid attribute class or form for '%s' in '%s'"),
850 arg1, arg2);
4d3c2250 851}
c906108c 852
c906108c
SS
853/* local function prototypes */
854
4efb68b1 855static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 856
aaa75496
JB
857static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
858 struct objfile *);
859
918dd910
JK
860static void dwarf2_find_base_address (struct die_info *die,
861 struct dwarf2_cu *cu);
862
c67a9c90 863static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 864
72bf9492
DJ
865static void scan_partial_symbols (struct partial_die_info *,
866 CORE_ADDR *, CORE_ADDR *,
5734ee8b 867 int, struct dwarf2_cu *);
c906108c 868
72bf9492
DJ
869static void add_partial_symbol (struct partial_die_info *,
870 struct dwarf2_cu *);
63d06c5c 871
72bf9492
DJ
872static void add_partial_namespace (struct partial_die_info *pdi,
873 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 874 int need_pc, struct dwarf2_cu *cu);
63d06c5c 875
5d7cb8df
JK
876static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
877 CORE_ADDR *highpc, int need_pc,
878 struct dwarf2_cu *cu);
879
72bf9492
DJ
880static void add_partial_enumeration (struct partial_die_info *enum_pdi,
881 struct dwarf2_cu *cu);
91c24f0a 882
bc30ff58
JB
883static void add_partial_subprogram (struct partial_die_info *pdi,
884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 885 int need_pc, struct dwarf2_cu *cu);
bc30ff58 886
fe1b8b76 887static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
888 gdb_byte *buffer, gdb_byte *info_ptr,
889 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 890
a14ed312 891static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 892
a14ed312 893static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 894
e5fe5e75 895static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 896
f3dd6933 897static void dwarf2_free_abbrev_table (void *);
c906108c 898
6caca83c
CC
899static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
900
fe1b8b76 901static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 902 struct dwarf2_cu *);
72bf9492 903
57349743 904static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 905 struct dwarf2_cu *);
c906108c 906
93311388
DE
907static struct partial_die_info *load_partial_dies (bfd *,
908 gdb_byte *, gdb_byte *,
909 int, struct dwarf2_cu *);
72bf9492 910
fe1b8b76 911static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
912 struct abbrev_info *abbrev,
913 unsigned int, bfd *,
914 gdb_byte *, gdb_byte *,
915 struct dwarf2_cu *);
c906108c 916
b64f50a1 917static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 918 struct dwarf2_cu *);
72bf9492
DJ
919
920static void fixup_partial_die (struct partial_die_info *,
921 struct dwarf2_cu *);
922
fe1b8b76
JB
923static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
924 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 925
fe1b8b76
JB
926static gdb_byte *read_attribute_value (struct attribute *, unsigned,
927 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 928
fe1b8b76 929static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 930
fe1b8b76 931static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 932
fe1b8b76 933static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 934
fe1b8b76 935static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 936
93311388 937static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 938
fe1b8b76 939static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 940 unsigned int *);
c906108c 941
c764a876
DE
942static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
943
944static LONGEST read_checked_initial_length_and_offset
945 (bfd *, gdb_byte *, const struct comp_unit_head *,
946 unsigned int *, unsigned int *);
613e1657 947
fe1b8b76 948static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
949 unsigned int *);
950
951static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 952
fe1b8b76 953static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 954
9b1c24c8 955static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 956
fe1b8b76
JB
957static char *read_indirect_string (bfd *, gdb_byte *,
958 const struct comp_unit_head *,
959 unsigned int *);
4bdf3d34 960
12df843f 961static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 962
12df843f 963static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 964
fe1b8b76 965static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 966
e142c38c 967static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 968
e142c38c
DJ
969static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
970 struct dwarf2_cu *);
c906108c 971
348e048f
DE
972static struct attribute *dwarf2_attr_no_follow (struct die_info *,
973 unsigned int,
974 struct dwarf2_cu *);
975
05cf31d1
JB
976static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
977 struct dwarf2_cu *cu);
978
e142c38c 979static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 980
e142c38c 981static struct die_info *die_specification (struct die_info *die,
f2f0e013 982 struct dwarf2_cu **);
63d06c5c 983
debd256d
JB
984static void free_line_header (struct line_header *lh);
985
aaa75496
JB
986static void add_file_name (struct line_header *, char *, unsigned int,
987 unsigned int, unsigned int);
988
debd256d
JB
989static struct line_header *(dwarf_decode_line_header
990 (unsigned int offset,
e7c27a73 991 bfd *abfd, struct dwarf2_cu *cu));
debd256d 992
f3f5162e
DE
993static void dwarf_decode_lines (struct line_header *, const char *,
994 struct dwarf2_cu *, struct partial_symtab *,
995 int);
c906108c 996
72b9f47f 997static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 998
a14ed312 999static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1000 struct dwarf2_cu *);
c906108c 1001
34eaf542
TT
1002static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003 struct dwarf2_cu *, struct symbol *);
1004
a14ed312 1005static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1006 struct dwarf2_cu *);
c906108c 1007
98bfdba5
PA
1008static void dwarf2_const_value_attr (struct attribute *attr,
1009 struct type *type,
1010 const char *name,
1011 struct obstack *obstack,
12df843f 1012 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1013 gdb_byte **bytes,
1014 struct dwarf2_locexpr_baton **baton);
2df3850c 1015
e7c27a73 1016static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1017
b4ba55a1
JB
1018static int need_gnat_info (struct dwarf2_cu *);
1019
3e43a32a
MS
1020static struct type *die_descriptive_type (struct die_info *,
1021 struct dwarf2_cu *);
b4ba55a1
JB
1022
1023static void set_descriptive_type (struct type *, struct die_info *,
1024 struct dwarf2_cu *);
1025
e7c27a73
DJ
1026static struct type *die_containing_type (struct die_info *,
1027 struct dwarf2_cu *);
c906108c 1028
673bfd45
DE
1029static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030 struct dwarf2_cu *);
c906108c 1031
f792889a 1032static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1033
673bfd45
DE
1034static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
0d5cff50 1036static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1037
6e70227d 1038static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1039 const char *suffix, int physname,
1040 struct dwarf2_cu *cu);
63d06c5c 1041
e7c27a73 1042static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
348e048f
DE
1044static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
e7c27a73 1046static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1047
e7c27a73 1048static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1049
96408a79
SA
1050static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1051
ff013f42
JK
1052static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1053 struct dwarf2_cu *, struct partial_symtab *);
1054
a14ed312 1055static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1056 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1057 struct partial_symtab *);
c906108c 1058
fae299cd
DC
1059static void get_scope_pc_bounds (struct die_info *,
1060 CORE_ADDR *, CORE_ADDR *,
1061 struct dwarf2_cu *);
1062
801e3a5b
JB
1063static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1064 CORE_ADDR, struct dwarf2_cu *);
1065
a14ed312 1066static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1067 struct dwarf2_cu *);
c906108c 1068
a14ed312 1069static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1070 struct type *, struct dwarf2_cu *);
c906108c 1071
a14ed312 1072static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1073 struct die_info *, struct type *,
e7c27a73 1074 struct dwarf2_cu *);
c906108c 1075
a14ed312 1076static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1077 struct type *,
1078 struct dwarf2_cu *);
c906108c 1079
134d01f1 1080static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1081
e7c27a73 1082static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1083
e7c27a73 1084static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1085
5d7cb8df
JK
1086static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1087
27aa8d6a
SW
1088static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1089
f55ee35c
JK
1090static struct type *read_module_type (struct die_info *die,
1091 struct dwarf2_cu *cu);
1092
38d518c9 1093static const char *namespace_name (struct die_info *die,
e142c38c 1094 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1095
134d01f1 1096static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1097
e7c27a73 1098static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1099
6e70227d 1100static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1101 struct dwarf2_cu *);
1102
93311388 1103static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1104
93311388
DE
1105static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1106 gdb_byte *info_ptr,
d97bc12b
DE
1107 gdb_byte **new_info_ptr,
1108 struct die_info *parent);
1109
93311388
DE
1110static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
fe1b8b76 1112 gdb_byte **new_info_ptr,
639d11d3
DC
1113 struct die_info *parent);
1114
93311388
DE
1115static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1116 gdb_byte *info_ptr,
fe1b8b76 1117 gdb_byte **new_info_ptr,
639d11d3
DC
1118 struct die_info *parent);
1119
93311388
DE
1120static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1121 struct die_info **, gdb_byte *,
1122 int *);
1123
e7c27a73 1124static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1125
71c25dea
TT
1126static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1127 struct obstack *);
1128
e142c38c 1129static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1130
98bfdba5
PA
1131static const char *dwarf2_full_name (char *name,
1132 struct die_info *die,
1133 struct dwarf2_cu *cu);
1134
e142c38c 1135static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1136 struct dwarf2_cu **);
9219021c 1137
a14ed312 1138static char *dwarf_tag_name (unsigned int);
c906108c 1139
a14ed312 1140static char *dwarf_attr_name (unsigned int);
c906108c 1141
a14ed312 1142static char *dwarf_form_name (unsigned int);
c906108c 1143
a14ed312 1144static char *dwarf_bool_name (unsigned int);
c906108c 1145
a14ed312 1146static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1147
1148#if 0
a14ed312 1149static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1150#endif
1151
f9aca02d 1152static struct die_info *sibling_die (struct die_info *);
c906108c 1153
d97bc12b
DE
1154static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1155
1156static void dump_die_for_error (struct die_info *);
1157
1158static void dump_die_1 (struct ui_file *, int level, int max_level,
1159 struct die_info *);
c906108c 1160
d97bc12b 1161/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1162
51545339 1163static void store_in_ref_table (struct die_info *,
10b3939b 1164 struct dwarf2_cu *);
c906108c 1165
93311388
DE
1166static int is_ref_attr (struct attribute *);
1167
b64f50a1 1168static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1169
43bbcdc2 1170static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1171
348e048f
DE
1172static struct die_info *follow_die_ref_or_sig (struct die_info *,
1173 struct attribute *,
1174 struct dwarf2_cu **);
1175
10b3939b
DJ
1176static struct die_info *follow_die_ref (struct die_info *,
1177 struct attribute *,
f2f0e013 1178 struct dwarf2_cu **);
c906108c 1179
348e048f
DE
1180static struct die_info *follow_die_sig (struct die_info *,
1181 struct attribute *,
1182 struct dwarf2_cu **);
1183
6c83ed52
TT
1184static struct signatured_type *lookup_signatured_type_at_offset
1185 (struct objfile *objfile,
b64f50a1 1186 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1187
e5fe5e75 1188static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1189
52dc124a 1190static void read_signatured_type (struct signatured_type *);
348e048f 1191
c906108c
SS
1192/* memory allocation interface */
1193
7b5a2f43 1194static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1195
f3dd6933 1196static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1197
b60c80d6 1198static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1199
2e276125 1200static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1201 char *, bfd *, struct dwarf2_cu *,
1202 struct dwarf2_section_info *,
1203 int);
2e276125 1204
8e19ed76
PS
1205static int attr_form_is_block (struct attribute *);
1206
3690dd37
JB
1207static int attr_form_is_section_offset (struct attribute *);
1208
1209static int attr_form_is_constant (struct attribute *);
1210
8cf6f0b1
TT
1211static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1212 struct dwarf2_loclist_baton *baton,
1213 struct attribute *attr);
1214
93e7bd98
DJ
1215static void dwarf2_symbol_mark_computed (struct attribute *attr,
1216 struct symbol *sym,
1217 struct dwarf2_cu *cu);
4c2df51b 1218
93311388
DE
1219static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1220 struct abbrev_info *abbrev,
1221 struct dwarf2_cu *cu);
4bb7a0a7 1222
72bf9492
DJ
1223static void free_stack_comp_unit (void *);
1224
72bf9492
DJ
1225static hashval_t partial_die_hash (const void *item);
1226
1227static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1228
ae038cb0 1229static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1230 (sect_offset offset, struct objfile *objfile);
ae038cb0 1231
9816fde3 1232static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1233 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1234
1235static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1236 struct die_info *comp_unit_die);
93311388 1237
68dc6402 1238static void free_heap_comp_unit (void *);
ae038cb0
DJ
1239
1240static void free_cached_comp_units (void *);
1241
1242static void age_cached_comp_units (void);
1243
1244static void free_one_cached_comp_unit (void *);
1245
f792889a
DJ
1246static struct type *set_die_type (struct die_info *, struct type *,
1247 struct dwarf2_cu *);
1c379e20 1248
ae038cb0
DJ
1249static void create_all_comp_units (struct objfile *);
1250
0e50663e 1251static int create_all_type_units (struct objfile *);
1fd400ff 1252
a0f42c21 1253static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1254
1255static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1256
1257static void dwarf2_add_dependence (struct dwarf2_cu *,
1258 struct dwarf2_per_cu_data *);
1259
ae038cb0
DJ
1260static void dwarf2_mark (struct dwarf2_cu *);
1261
1262static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1263
b64f50a1 1264static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1265 struct dwarf2_per_cu_data *per_cu);
1266
f792889a 1267static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1268
9291a0cd
TT
1269static void dwarf2_release_queue (void *dummy);
1270
a0f42c21 1271static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1272
a0f42c21 1273static void process_queue (void);
9291a0cd
TT
1274
1275static void find_file_and_directory (struct die_info *die,
1276 struct dwarf2_cu *cu,
1277 char **name, char **comp_dir);
1278
1279static char *file_full_name (int file, struct line_header *lh,
1280 const char *comp_dir);
1281
9ff913ba
DE
1282static gdb_byte *read_and_check_comp_unit_head
1283 (struct comp_unit_head *header,
1284 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1285 int is_debug_types_section);
9291a0cd
TT
1286
1287static void init_cu_die_reader (struct die_reader_specs *reader,
1288 struct dwarf2_cu *cu);
1289
673bfd45 1290static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1291
9291a0cd
TT
1292#if WORDS_BIGENDIAN
1293
1294/* Convert VALUE between big- and little-endian. */
1295static offset_type
1296byte_swap (offset_type value)
1297{
1298 offset_type result;
1299
1300 result = (value & 0xff) << 24;
1301 result |= (value & 0xff00) << 8;
1302 result |= (value & 0xff0000) >> 8;
1303 result |= (value & 0xff000000) >> 24;
1304 return result;
1305}
1306
1307#define MAYBE_SWAP(V) byte_swap (V)
1308
1309#else
1310#define MAYBE_SWAP(V) (V)
1311#endif /* WORDS_BIGENDIAN */
1312
1313/* The suffix for an index file. */
1314#define INDEX_SUFFIX ".gdb-index"
1315
3da10d80
KS
1316static const char *dwarf2_physname (char *name, struct die_info *die,
1317 struct dwarf2_cu *cu);
1318
c906108c 1319/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1320 information and return true if we have enough to do something.
1321 NAMES points to the dwarf2 section names, or is NULL if the standard
1322 ELF names are used. */
c906108c
SS
1323
1324int
251d32d9
TG
1325dwarf2_has_info (struct objfile *objfile,
1326 const struct dwarf2_debug_sections *names)
c906108c 1327{
be391dca
TT
1328 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329 if (!dwarf2_per_objfile)
1330 {
1331 /* Initialize per-objfile state. */
1332 struct dwarf2_per_objfile *data
1333 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1334
be391dca
TT
1335 memset (data, 0, sizeof (*data));
1336 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337 dwarf2_per_objfile = data;
6502dd73 1338
251d32d9
TG
1339 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1340 (void *) names);
be391dca
TT
1341 dwarf2_per_objfile->objfile = objfile;
1342 }
1343 return (dwarf2_per_objfile->info.asection != NULL
1344 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1345}
1346
251d32d9
TG
1347/* When loading sections, we look either for uncompressed section or for
1348 compressed section names. */
233a11ab
CS
1349
1350static int
251d32d9
TG
1351section_is_p (const char *section_name,
1352 const struct dwarf2_section_names *names)
233a11ab 1353{
251d32d9
TG
1354 if (names->normal != NULL
1355 && strcmp (section_name, names->normal) == 0)
1356 return 1;
1357 if (names->compressed != NULL
1358 && strcmp (section_name, names->compressed) == 0)
1359 return 1;
1360 return 0;
233a11ab
CS
1361}
1362
c906108c
SS
1363/* This function is mapped across the sections and remembers the
1364 offset and size of each of the debugging sections we are interested
1365 in. */
1366
1367static void
251d32d9 1368dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1369{
251d32d9
TG
1370 const struct dwarf2_debug_sections *names;
1371
1372 if (vnames == NULL)
1373 names = &dwarf2_elf_names;
1374 else
1375 names = (const struct dwarf2_debug_sections *) vnames;
1376
1377 if (section_is_p (sectp->name, &names->info))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->info.asection = sectp;
1380 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1381 }
251d32d9 1382 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->abbrev.asection = sectp;
1385 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1386 }
251d32d9 1387 else if (section_is_p (sectp->name, &names->line))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->line.asection = sectp;
1390 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1391 }
251d32d9 1392 else if (section_is_p (sectp->name, &names->loc))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->loc.asection = sectp;
1395 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1396 }
251d32d9 1397 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->macinfo.asection = sectp;
1400 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1401 }
cf2c3c16
TT
1402 else if (section_is_p (sectp->name, &names->macro))
1403 {
1404 dwarf2_per_objfile->macro.asection = sectp;
1405 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1406 }
251d32d9 1407 else if (section_is_p (sectp->name, &names->str))
c906108c 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->str.asection = sectp;
1410 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1411 }
251d32d9 1412 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->frame.asection = sectp;
1415 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1416 }
251d32d9 1417 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1418 {
3799ccc6 1419 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1420
3799ccc6
EZ
1421 if (aflag & SEC_HAS_CONTENTS)
1422 {
dce234bc
PP
1423 dwarf2_per_objfile->eh_frame.asection = sectp;
1424 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1425 }
b6af0555 1426 }
251d32d9 1427 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1428 {
dce234bc
PP
1429 dwarf2_per_objfile->ranges.asection = sectp;
1430 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1431 }
251d32d9 1432 else if (section_is_p (sectp->name, &names->types))
348e048f 1433 {
8b70b953
TT
1434 struct dwarf2_section_info type_section;
1435
1436 memset (&type_section, 0, sizeof (type_section));
1437 type_section.asection = sectp;
1438 type_section.size = bfd_get_section_size (sectp);
1439
1440 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1441 &type_section);
348e048f 1442 }
251d32d9 1443 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1444 {
1445 dwarf2_per_objfile->gdb_index.asection = sectp;
1446 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1447 }
dce234bc 1448
72dca2f5
FR
1449 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1450 && bfd_section_vma (abfd, sectp) == 0)
1451 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1452}
1453
dce234bc
PP
1454/* Decompress a section that was compressed using zlib. Store the
1455 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1456
1457static void
dce234bc
PP
1458zlib_decompress_section (struct objfile *objfile, asection *sectp,
1459 gdb_byte **outbuf, bfd_size_type *outsize)
1460{
1461 bfd *abfd = objfile->obfd;
1462#ifndef HAVE_ZLIB_H
1463 error (_("Support for zlib-compressed DWARF data (from '%s') "
1464 "is disabled in this copy of GDB"),
1465 bfd_get_filename (abfd));
1466#else
1467 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1468 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1469 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1470 bfd_size_type uncompressed_size;
1471 gdb_byte *uncompressed_buffer;
1472 z_stream strm;
1473 int rc;
1474 int header_size = 12;
1475
1476 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1477 || bfd_bread (compressed_buffer,
1478 compressed_size, abfd) != compressed_size)
dce234bc
PP
1479 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1480 bfd_get_filename (abfd));
1481
1482 /* Read the zlib header. In this case, it should be "ZLIB" followed
1483 by the uncompressed section size, 8 bytes in big-endian order. */
1484 if (compressed_size < header_size
1485 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1486 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1487 bfd_get_filename (abfd));
1488 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1494 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1495 uncompressed_size += compressed_buffer[11];
1496
1497 /* It is possible the section consists of several compressed
1498 buffers concatenated together, so we uncompress in a loop. */
1499 strm.zalloc = NULL;
1500 strm.zfree = NULL;
1501 strm.opaque = NULL;
1502 strm.avail_in = compressed_size - header_size;
1503 strm.next_in = (Bytef*) compressed_buffer + header_size;
1504 strm.avail_out = uncompressed_size;
1505 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1506 uncompressed_size);
1507 rc = inflateInit (&strm);
1508 while (strm.avail_in > 0)
1509 {
1510 if (rc != Z_OK)
1511 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1512 bfd_get_filename (abfd), rc);
1513 strm.next_out = ((Bytef*) uncompressed_buffer
1514 + (uncompressed_size - strm.avail_out));
1515 rc = inflate (&strm, Z_FINISH);
1516 if (rc != Z_STREAM_END)
1517 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1518 bfd_get_filename (abfd), rc);
1519 rc = inflateReset (&strm);
1520 }
1521 rc = inflateEnd (&strm);
1522 if (rc != Z_OK
1523 || strm.avail_out != 0)
1524 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1525 bfd_get_filename (abfd), rc);
1526
affddf13 1527 do_cleanups (cleanup);
dce234bc
PP
1528 *outbuf = uncompressed_buffer;
1529 *outsize = uncompressed_size;
1530#endif
233a11ab
CS
1531}
1532
9e0ac564
TT
1533/* A helper function that decides whether a section is empty. */
1534
1535static int
1536dwarf2_section_empty_p (struct dwarf2_section_info *info)
1537{
1538 return info->asection == NULL || info->size == 0;
1539}
1540
9cdd5dbd 1541/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1542 OBJFILE, store info about the section into INFO.
1543 If the section is compressed, uncompress it before returning. */
c906108c 1544
dce234bc
PP
1545static void
1546dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1547{
dce234bc
PP
1548 bfd *abfd = objfile->obfd;
1549 asection *sectp = info->asection;
1550 gdb_byte *buf, *retbuf;
1551 unsigned char header[4];
c906108c 1552
be391dca
TT
1553 if (info->readin)
1554 return;
dce234bc 1555 info->buffer = NULL;
b315ab21 1556 info->map_addr = NULL;
be391dca 1557 info->readin = 1;
188dd5d6 1558
9e0ac564 1559 if (dwarf2_section_empty_p (info))
dce234bc 1560 return;
c906108c 1561
dce234bc
PP
1562 /* Check if the file has a 4-byte header indicating compression. */
1563 if (info->size > sizeof (header)
1564 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1565 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1566 {
1567 /* Upon decompression, update the buffer and its size. */
1568 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1569 {
1570 zlib_decompress_section (objfile, sectp, &info->buffer,
1571 &info->size);
1572 return;
1573 }
1574 }
4bdf3d34 1575
dce234bc
PP
1576#ifdef HAVE_MMAP
1577 if (pagesize == 0)
1578 pagesize = getpagesize ();
2e276125 1579
dce234bc
PP
1580 /* Only try to mmap sections which are large enough: we don't want to
1581 waste space due to fragmentation. Also, only try mmap for sections
1582 without relocations. */
1583
1584 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1585 {
b315ab21
TG
1586 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1587 MAP_PRIVATE, sectp->filepos,
1588 &info->map_addr, &info->map_len);
dce234bc 1589
b315ab21 1590 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1591 {
be391dca 1592#if HAVE_POSIX_MADVISE
b315ab21 1593 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1594#endif
dce234bc
PP
1595 return;
1596 }
1597 }
1598#endif
1599
1600 /* If we get here, we are a normal, not-compressed section. */
1601 info->buffer = buf
1602 = obstack_alloc (&objfile->objfile_obstack, info->size);
1603
1604 /* When debugging .o files, we may need to apply relocations; see
1605 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1606 We never compress sections in .o files, so we only need to
1607 try this when the section is not compressed. */
ac8035ab 1608 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1609 if (retbuf != NULL)
1610 {
1611 info->buffer = retbuf;
1612 return;
1613 }
1614
1615 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1616 || bfd_bread (buf, info->size, abfd) != info->size)
1617 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1618 bfd_get_filename (abfd));
1619}
1620
9e0ac564
TT
1621/* A helper function that returns the size of a section in a safe way.
1622 If you are positive that the section has been read before using the
1623 size, then it is safe to refer to the dwarf2_section_info object's
1624 "size" field directly. In other cases, you must call this
1625 function, because for compressed sections the size field is not set
1626 correctly until the section has been read. */
1627
1628static bfd_size_type
1629dwarf2_section_size (struct objfile *objfile,
1630 struct dwarf2_section_info *info)
1631{
1632 if (!info->readin)
1633 dwarf2_read_section (objfile, info);
1634 return info->size;
1635}
1636
dce234bc 1637/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1638 SECTION_NAME. */
af34e669 1639
dce234bc 1640void
3017a003
TG
1641dwarf2_get_section_info (struct objfile *objfile,
1642 enum dwarf2_section_enum sect,
dce234bc
PP
1643 asection **sectp, gdb_byte **bufp,
1644 bfd_size_type *sizep)
1645{
1646 struct dwarf2_per_objfile *data
1647 = objfile_data (objfile, dwarf2_objfile_data_key);
1648 struct dwarf2_section_info *info;
a3b2a86b
TT
1649
1650 /* We may see an objfile without any DWARF, in which case we just
1651 return nothing. */
1652 if (data == NULL)
1653 {
1654 *sectp = NULL;
1655 *bufp = NULL;
1656 *sizep = 0;
1657 return;
1658 }
3017a003
TG
1659 switch (sect)
1660 {
1661 case DWARF2_DEBUG_FRAME:
1662 info = &data->frame;
1663 break;
1664 case DWARF2_EH_FRAME:
1665 info = &data->eh_frame;
1666 break;
1667 default:
1668 gdb_assert_not_reached ("unexpected section");
1669 }
dce234bc 1670
9e0ac564 1671 dwarf2_read_section (objfile, info);
dce234bc
PP
1672
1673 *sectp = info->asection;
1674 *bufp = info->buffer;
1675 *sizep = info->size;
1676}
1677
9291a0cd 1678\f
7b9f3c50
DE
1679/* DWARF quick_symbols_functions support. */
1680
1681/* TUs can share .debug_line entries, and there can be a lot more TUs than
1682 unique line tables, so we maintain a separate table of all .debug_line
1683 derived entries to support the sharing.
1684 All the quick functions need is the list of file names. We discard the
1685 line_header when we're done and don't need to record it here. */
1686struct quick_file_names
1687{
1688 /* The offset in .debug_line of the line table. We hash on this. */
1689 unsigned int offset;
1690
1691 /* The number of entries in file_names, real_names. */
1692 unsigned int num_file_names;
1693
1694 /* The file names from the line table, after being run through
1695 file_full_name. */
1696 const char **file_names;
1697
1698 /* The file names from the line table after being run through
1699 gdb_realpath. These are computed lazily. */
1700 const char **real_names;
1701};
1702
1703/* When using the index (and thus not using psymtabs), each CU has an
1704 object of this type. This is used to hold information needed by
1705 the various "quick" methods. */
1706struct dwarf2_per_cu_quick_data
1707{
1708 /* The file table. This can be NULL if there was no file table
1709 or it's currently not read in.
1710 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1711 struct quick_file_names *file_names;
1712
1713 /* The corresponding symbol table. This is NULL if symbols for this
1714 CU have not yet been read. */
1715 struct symtab *symtab;
1716
1717 /* A temporary mark bit used when iterating over all CUs in
1718 expand_symtabs_matching. */
1719 unsigned int mark : 1;
1720
1721 /* True if we've tried to read the file table and found there isn't one.
1722 There will be no point in trying to read it again next time. */
1723 unsigned int no_file_data : 1;
1724};
1725
1726/* Hash function for a quick_file_names. */
1727
1728static hashval_t
1729hash_file_name_entry (const void *e)
1730{
1731 const struct quick_file_names *file_data = e;
1732
1733 return file_data->offset;
1734}
1735
1736/* Equality function for a quick_file_names. */
1737
1738static int
1739eq_file_name_entry (const void *a, const void *b)
1740{
1741 const struct quick_file_names *ea = a;
1742 const struct quick_file_names *eb = b;
1743
1744 return ea->offset == eb->offset;
1745}
1746
1747/* Delete function for a quick_file_names. */
1748
1749static void
1750delete_file_name_entry (void *e)
1751{
1752 struct quick_file_names *file_data = e;
1753 int i;
1754
1755 for (i = 0; i < file_data->num_file_names; ++i)
1756 {
1757 xfree ((void*) file_data->file_names[i]);
1758 if (file_data->real_names)
1759 xfree ((void*) file_data->real_names[i]);
1760 }
1761
1762 /* The space for the struct itself lives on objfile_obstack,
1763 so we don't free it here. */
1764}
1765
1766/* Create a quick_file_names hash table. */
1767
1768static htab_t
1769create_quick_file_names_table (unsigned int nr_initial_entries)
1770{
1771 return htab_create_alloc (nr_initial_entries,
1772 hash_file_name_entry, eq_file_name_entry,
1773 delete_file_name_entry, xcalloc, xfree);
1774}
9291a0cd 1775
918dd910
JK
1776/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1777 have to be created afterwards. You should call age_cached_comp_units after
1778 processing PER_CU->CU. dw2_setup must have been already called. */
1779
1780static void
1781load_cu (struct dwarf2_per_cu_data *per_cu)
1782{
b0df02fd 1783 if (per_cu->debug_types_section)
e5fe5e75 1784 load_full_type_unit (per_cu);
918dd910 1785 else
a0f42c21 1786 load_full_comp_unit (per_cu);
918dd910 1787
918dd910 1788 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1789
1790 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1791}
1792
a0f42c21 1793/* Read in the symbols for PER_CU. */
2fdf6df6 1794
9291a0cd 1795static void
a0f42c21 1796dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1797{
1798 struct cleanup *back_to;
1799
1800 back_to = make_cleanup (dwarf2_release_queue, NULL);
1801
a0f42c21 1802 queue_comp_unit (per_cu);
9291a0cd 1803
918dd910 1804 load_cu (per_cu);
9291a0cd 1805
a0f42c21 1806 process_queue ();
9291a0cd
TT
1807
1808 /* Age the cache, releasing compilation units that have not
1809 been used recently. */
1810 age_cached_comp_units ();
1811
1812 do_cleanups (back_to);
1813}
1814
1815/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1816 the objfile from which this CU came. Returns the resulting symbol
1817 table. */
2fdf6df6 1818
9291a0cd 1819static struct symtab *
a0f42c21 1820dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1821{
1822 if (!per_cu->v.quick->symtab)
1823 {
1824 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1825 increment_reading_symtab ();
a0f42c21 1826 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1827 do_cleanups (back_to);
1828 }
1829 return per_cu->v.quick->symtab;
1830}
1831
1fd400ff 1832/* Return the CU given its index. */
2fdf6df6 1833
1fd400ff
TT
1834static struct dwarf2_per_cu_data *
1835dw2_get_cu (int index)
1836{
1837 if (index >= dwarf2_per_objfile->n_comp_units)
1838 {
1839 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1840 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1841 }
1842 return dwarf2_per_objfile->all_comp_units[index];
1843}
1844
9291a0cd
TT
1845/* A helper function that knows how to read a 64-bit value in a way
1846 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1847 otherwise. */
2fdf6df6 1848
9291a0cd
TT
1849static int
1850extract_cu_value (const char *bytes, ULONGEST *result)
1851{
1852 if (sizeof (ULONGEST) < 8)
1853 {
1854 int i;
1855
1856 /* Ignore the upper 4 bytes if they are all zero. */
1857 for (i = 0; i < 4; ++i)
1858 if (bytes[i + 4] != 0)
1859 return 0;
1860
1861 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1862 }
1863 else
1864 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1865 return 1;
1866}
1867
1868/* Read the CU list from the mapped index, and use it to create all
1869 the CU objects for this objfile. Return 0 if something went wrong,
1870 1 if everything went ok. */
2fdf6df6 1871
9291a0cd 1872static int
1fd400ff
TT
1873create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1874 offset_type cu_list_elements)
9291a0cd
TT
1875{
1876 offset_type i;
9291a0cd
TT
1877
1878 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1879 dwarf2_per_objfile->all_comp_units
1880 = obstack_alloc (&objfile->objfile_obstack,
1881 dwarf2_per_objfile->n_comp_units
1882 * sizeof (struct dwarf2_per_cu_data *));
1883
1884 for (i = 0; i < cu_list_elements; i += 2)
1885 {
1886 struct dwarf2_per_cu_data *the_cu;
1887 ULONGEST offset, length;
1888
1889 if (!extract_cu_value (cu_list, &offset)
1890 || !extract_cu_value (cu_list + 8, &length))
1891 return 0;
1892 cu_list += 2 * 8;
1893
1894 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1895 struct dwarf2_per_cu_data);
b64f50a1 1896 the_cu->offset.sect_off = offset;
9291a0cd
TT
1897 the_cu->length = length;
1898 the_cu->objfile = objfile;
1899 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1900 struct dwarf2_per_cu_quick_data);
1901 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1902 }
1903
1904 return 1;
1905}
1906
1fd400ff 1907/* Create the signatured type hash table from the index. */
673bfd45 1908
1fd400ff 1909static int
673bfd45 1910create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1911 struct dwarf2_section_info *section,
673bfd45
DE
1912 const gdb_byte *bytes,
1913 offset_type elements)
1fd400ff
TT
1914{
1915 offset_type i;
673bfd45 1916 htab_t sig_types_hash;
1fd400ff 1917
d467dd73
DE
1918 dwarf2_per_objfile->n_type_units = elements / 3;
1919 dwarf2_per_objfile->all_type_units
1fd400ff 1920 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1921 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1922 * sizeof (struct dwarf2_per_cu_data *));
1923
673bfd45 1924 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1925
1926 for (i = 0; i < elements; i += 3)
1927 {
52dc124a
DE
1928 struct signatured_type *sig_type;
1929 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
1930 void **slot;
1931
1932 if (!extract_cu_value (bytes, &offset)
52dc124a 1933 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
1934 return 0;
1935 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936 bytes += 3 * 8;
1937
52dc124a 1938 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 1939 struct signatured_type);
52dc124a
DE
1940 sig_type->signature = signature;
1941 sig_type->type_offset.cu_off = type_offset_in_tu;
1942 sig_type->per_cu.debug_types_section = section;
1943 sig_type->per_cu.offset.sect_off = offset;
1944 sig_type->per_cu.objfile = objfile;
1945 sig_type->per_cu.v.quick
1fd400ff
TT
1946 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947 struct dwarf2_per_cu_quick_data);
1948
52dc124a
DE
1949 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
1950 *slot = sig_type;
1fd400ff 1951
52dc124a 1952 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
1953 }
1954
673bfd45 1955 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1956
1957 return 1;
1958}
1959
9291a0cd
TT
1960/* Read the address map data from the mapped index, and use it to
1961 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1962
9291a0cd
TT
1963static void
1964create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965{
1966 const gdb_byte *iter, *end;
1967 struct obstack temp_obstack;
1968 struct addrmap *mutable_map;
1969 struct cleanup *cleanup;
1970 CORE_ADDR baseaddr;
1971
1972 obstack_init (&temp_obstack);
1973 cleanup = make_cleanup_obstack_free (&temp_obstack);
1974 mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976 iter = index->address_table;
1977 end = iter + index->address_table_size;
1978
1979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981 while (iter < end)
1982 {
1983 ULONGEST hi, lo, cu_index;
1984 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987 iter += 8;
1988 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989 iter += 4;
1990
1991 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1992 dw2_get_cu (cu_index));
9291a0cd
TT
1993 }
1994
1995 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996 &objfile->objfile_obstack);
1997 do_cleanups (cleanup);
1998}
1999
59d7bcaf
JK
2000/* The hash function for strings in the mapped index. This is the same as
2001 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002 implementation. This is necessary because the hash function is tied to the
2003 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2004 SYMBOL_HASH_NEXT.
2005
2006 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2007
9291a0cd 2008static hashval_t
559a7a62 2009mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2010{
2011 const unsigned char *str = (const unsigned char *) p;
2012 hashval_t r = 0;
2013 unsigned char c;
2014
2015 while ((c = *str++) != 0)
559a7a62
JK
2016 {
2017 if (index_version >= 5)
2018 c = tolower (c);
2019 r = r * 67 + c - 113;
2020 }
9291a0cd
TT
2021
2022 return r;
2023}
2024
2025/* Find a slot in the mapped index INDEX for the object named NAME.
2026 If NAME is found, set *VEC_OUT to point to the CU vector in the
2027 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2028
9291a0cd
TT
2029static int
2030find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031 offset_type **vec_out)
2032{
0cf03b49
JK
2033 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034 offset_type hash;
9291a0cd 2035 offset_type slot, step;
559a7a62 2036 int (*cmp) (const char *, const char *);
9291a0cd 2037
0cf03b49
JK
2038 if (current_language->la_language == language_cplus
2039 || current_language->la_language == language_java
2040 || current_language->la_language == language_fortran)
2041 {
2042 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2043 not contain any. */
2044 const char *paren = strchr (name, '(');
2045
2046 if (paren)
2047 {
2048 char *dup;
2049
2050 dup = xmalloc (paren - name + 1);
2051 memcpy (dup, name, paren - name);
2052 dup[paren - name] = 0;
2053
2054 make_cleanup (xfree, dup);
2055 name = dup;
2056 }
2057 }
2058
559a7a62 2059 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2060 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2061 simulate our NAME being searched is also lowercased. */
2062 hash = mapped_index_string_hash ((index->version == 4
2063 && case_sensitivity == case_sensitive_off
2064 ? 5 : index->version),
2065 name);
2066
3876f04e
DE
2067 slot = hash & (index->symbol_table_slots - 1);
2068 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2069 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2070
2071 for (;;)
2072 {
2073 /* Convert a slot number to an offset into the table. */
2074 offset_type i = 2 * slot;
2075 const char *str;
3876f04e 2076 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2077 {
2078 do_cleanups (back_to);
2079 return 0;
2080 }
9291a0cd 2081
3876f04e 2082 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2083 if (!cmp (name, str))
9291a0cd
TT
2084 {
2085 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2086 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2087 do_cleanups (back_to);
9291a0cd
TT
2088 return 1;
2089 }
2090
3876f04e 2091 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2092 }
2093}
2094
2095/* Read the index file. If everything went ok, initialize the "quick"
2096 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2097
9291a0cd
TT
2098static int
2099dwarf2_read_index (struct objfile *objfile)
2100{
9291a0cd
TT
2101 char *addr;
2102 struct mapped_index *map;
b3b272e1 2103 offset_type *metadata;
ac0b195c
KW
2104 const gdb_byte *cu_list;
2105 const gdb_byte *types_list = NULL;
2106 offset_type version, cu_list_elements;
2107 offset_type types_list_elements = 0;
1fd400ff 2108 int i;
9291a0cd 2109
9e0ac564 2110 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2111 return 0;
82430852
JK
2112
2113 /* Older elfutils strip versions could keep the section in the main
2114 executable while splitting it for the separate debug info file. */
2115 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116 & SEC_HAS_CONTENTS) == 0)
2117 return 0;
2118
9291a0cd
TT
2119 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121 addr = dwarf2_per_objfile->gdb_index.buffer;
2122 /* Version check. */
1fd400ff 2123 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2124 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2125 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2126 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2127 indices. */
831adc1f 2128 if (version < 4)
481860b3
GB
2129 {
2130 static int warning_printed = 0;
2131 if (!warning_printed)
2132 {
2133 warning (_("Skipping obsolete .gdb_index section in %s."),
2134 objfile->name);
2135 warning_printed = 1;
2136 }
2137 return 0;
2138 }
2139 /* Index version 4 uses a different hash function than index version
2140 5 and later.
2141
2142 Versions earlier than 6 did not emit psymbols for inlined
2143 functions. Using these files will cause GDB not to be able to
2144 set breakpoints on inlined functions by name, so we ignore these
2145 indices unless the --use-deprecated-index-sections command line
2146 option was supplied. */
2147 if (version < 6 && !use_deprecated_index_sections)
2148 {
2149 static int warning_printed = 0;
2150 if (!warning_printed)
2151 {
2152 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2153 "--use-deprecated-index-sections to use them anyway"),
2154 objfile->name);
2155 warning_printed = 1;
2156 }
2157 return 0;
2158 }
2159 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2160 longer backward compatible. */
481860b3 2161 if (version > 6)
594e8718 2162 return 0;
9291a0cd
TT
2163
2164 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2165 map->version = version;
b3b272e1 2166 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2167
2168 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2169
2170 i = 0;
2171 cu_list = addr + MAYBE_SWAP (metadata[i]);
2172 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2173 / 8);
1fd400ff
TT
2174 ++i;
2175
987d643c
TT
2176 types_list = addr + MAYBE_SWAP (metadata[i]);
2177 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2178 - MAYBE_SWAP (metadata[i]))
2179 / 8);
2180 ++i;
1fd400ff
TT
2181
2182 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2183 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2184 - MAYBE_SWAP (metadata[i]));
2185 ++i;
2186
3876f04e
DE
2187 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2188 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2189 - MAYBE_SWAP (metadata[i]))
2190 / (2 * sizeof (offset_type)));
1fd400ff 2191 ++i;
9291a0cd 2192
1fd400ff
TT
2193 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2194
2195 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2196 return 0;
2197
8b70b953
TT
2198 if (types_list_elements)
2199 {
2200 struct dwarf2_section_info *section;
2201
2202 /* We can only handle a single .debug_types when we have an
2203 index. */
2204 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2205 return 0;
2206
2207 section = VEC_index (dwarf2_section_info_def,
2208 dwarf2_per_objfile->types, 0);
2209
2210 if (!create_signatured_type_table_from_index (objfile, section,
2211 types_list,
2212 types_list_elements))
2213 return 0;
2214 }
9291a0cd
TT
2215
2216 create_addrmap_from_index (objfile, map);
2217
2218 dwarf2_per_objfile->index_table = map;
2219 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2220 dwarf2_per_objfile->quick_file_names_table =
2221 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2222
2223 return 1;
2224}
2225
2226/* A helper for the "quick" functions which sets the global
2227 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2228
9291a0cd
TT
2229static void
2230dw2_setup (struct objfile *objfile)
2231{
2232 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2233 gdb_assert (dwarf2_per_objfile);
2234}
2235
2236/* A helper for the "quick" functions which attempts to read the line
2237 table for THIS_CU. */
2fdf6df6 2238
7b9f3c50
DE
2239static struct quick_file_names *
2240dw2_get_file_names (struct objfile *objfile,
2241 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2242{
2243 bfd *abfd = objfile->obfd;
7b9f3c50 2244 struct line_header *lh;
9291a0cd
TT
2245 struct attribute *attr;
2246 struct cleanup *cleanups;
2247 struct die_info *comp_unit_die;
36374493 2248 struct dwarf2_section_info* sec;
9ff913ba 2249 gdb_byte *info_ptr;
9291a0cd
TT
2250 int has_children, i;
2251 struct dwarf2_cu cu;
9ff913ba 2252 unsigned int bytes_read;
9291a0cd
TT
2253 struct die_reader_specs reader_specs;
2254 char *name, *comp_dir;
7b9f3c50
DE
2255 void **slot;
2256 struct quick_file_names *qfn;
2257 unsigned int line_offset;
9291a0cd 2258
7b9f3c50
DE
2259 if (this_cu->v.quick->file_names != NULL)
2260 return this_cu->v.quick->file_names;
2261 /* If we know there is no line data, no point in looking again. */
2262 if (this_cu->v.quick->no_file_data)
2263 return NULL;
9291a0cd 2264
23745b47 2265 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2266 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2267
b0df02fd
DE
2268 if (this_cu->debug_types_section)
2269 sec = this_cu->debug_types_section;
36374493
DE
2270 else
2271 sec = &dwarf2_per_objfile->info;
2272 dwarf2_read_section (objfile, sec);
b64f50a1 2273 info_ptr = sec->buffer + this_cu->offset.sect_off;
9291a0cd 2274
9ff913ba
DE
2275 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2276 this_cu->debug_types_section != NULL);
9291a0cd 2277
6caca83c 2278 /* Skip dummy compilation units. */
9ff913ba 2279 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2280 || peek_abbrev_code (abfd, info_ptr) == 0)
2281 {
2282 do_cleanups (cleanups);
2283 return NULL;
2284 }
2285
e5fe5e75 2286 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2287 make_cleanup (dwarf2_free_abbrev_table, &cu);
2288
9291a0cd 2289 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2290 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2291 &has_children);
9291a0cd 2292
7b9f3c50
DE
2293 lh = NULL;
2294 slot = NULL;
2295 line_offset = 0;
9291a0cd
TT
2296 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2297 if (attr)
2298 {
7b9f3c50
DE
2299 struct quick_file_names find_entry;
2300
2301 line_offset = DW_UNSND (attr);
2302
2303 /* We may have already read in this line header (TU line header sharing).
2304 If we have we're done. */
2305 find_entry.offset = line_offset;
2306 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2307 &find_entry, INSERT);
2308 if (*slot != NULL)
2309 {
2310 do_cleanups (cleanups);
2311 this_cu->v.quick->file_names = *slot;
2312 return *slot;
2313 }
2314
9291a0cd
TT
2315 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2316 }
2317 if (lh == NULL)
2318 {
2319 do_cleanups (cleanups);
7b9f3c50
DE
2320 this_cu->v.quick->no_file_data = 1;
2321 return NULL;
9291a0cd
TT
2322 }
2323
7b9f3c50
DE
2324 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2325 qfn->offset = line_offset;
2326 gdb_assert (slot != NULL);
2327 *slot = qfn;
9291a0cd 2328
7b9f3c50 2329 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2330
7b9f3c50
DE
2331 qfn->num_file_names = lh->num_file_names;
2332 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2333 lh->num_file_names * sizeof (char *));
9291a0cd 2334 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2335 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2336 qfn->real_names = NULL;
9291a0cd 2337
7b9f3c50 2338 free_line_header (lh);
9291a0cd 2339 do_cleanups (cleanups);
7b9f3c50
DE
2340
2341 this_cu->v.quick->file_names = qfn;
2342 return qfn;
9291a0cd
TT
2343}
2344
2345/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2346 real path for a given file name from the line table. */
2fdf6df6 2347
9291a0cd 2348static const char *
7b9f3c50
DE
2349dw2_get_real_path (struct objfile *objfile,
2350 struct quick_file_names *qfn, int index)
9291a0cd 2351{
7b9f3c50
DE
2352 if (qfn->real_names == NULL)
2353 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2354 qfn->num_file_names, sizeof (char *));
9291a0cd 2355
7b9f3c50
DE
2356 if (qfn->real_names[index] == NULL)
2357 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2358
7b9f3c50 2359 return qfn->real_names[index];
9291a0cd
TT
2360}
2361
2362static struct symtab *
2363dw2_find_last_source_symtab (struct objfile *objfile)
2364{
2365 int index;
ae2de4f8 2366
9291a0cd
TT
2367 dw2_setup (objfile);
2368 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2369 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2370}
2371
7b9f3c50
DE
2372/* Traversal function for dw2_forget_cached_source_info. */
2373
2374static int
2375dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2376{
7b9f3c50 2377 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2378
7b9f3c50 2379 if (file_data->real_names)
9291a0cd 2380 {
7b9f3c50 2381 int i;
9291a0cd 2382
7b9f3c50 2383 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2384 {
7b9f3c50
DE
2385 xfree ((void*) file_data->real_names[i]);
2386 file_data->real_names[i] = NULL;
9291a0cd
TT
2387 }
2388 }
7b9f3c50
DE
2389
2390 return 1;
2391}
2392
2393static void
2394dw2_forget_cached_source_info (struct objfile *objfile)
2395{
2396 dw2_setup (objfile);
2397
2398 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2399 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2400}
2401
f8eba3c6
TT
2402/* Helper function for dw2_map_symtabs_matching_filename that expands
2403 the symtabs and calls the iterator. */
2404
2405static int
2406dw2_map_expand_apply (struct objfile *objfile,
2407 struct dwarf2_per_cu_data *per_cu,
2408 const char *name,
2409 const char *full_path, const char *real_path,
2410 int (*callback) (struct symtab *, void *),
2411 void *data)
2412{
2413 struct symtab *last_made = objfile->symtabs;
2414
2415 /* Don't visit already-expanded CUs. */
2416 if (per_cu->v.quick->symtab)
2417 return 0;
2418
2419 /* This may expand more than one symtab, and we want to iterate over
2420 all of them. */
a0f42c21 2421 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2422
2423 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2424 objfile->symtabs, last_made);
2425}
2426
2427/* Implementation of the map_symtabs_matching_filename method. */
2428
9291a0cd 2429static int
f8eba3c6
TT
2430dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2431 const char *full_path, const char *real_path,
2432 int (*callback) (struct symtab *, void *),
2433 void *data)
9291a0cd
TT
2434{
2435 int i;
c011a4f4 2436 const char *name_basename = lbasename (name);
4aac40c8
TT
2437 int name_len = strlen (name);
2438 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2439
2440 dw2_setup (objfile);
ae2de4f8 2441
1fd400ff 2442 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2443 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2444 {
2445 int j;
e254ef6a 2446 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2447 struct quick_file_names *file_data;
9291a0cd 2448
3d7bb9d9 2449 /* We only need to look at symtabs not already expanded. */
e254ef6a 2450 if (per_cu->v.quick->symtab)
9291a0cd
TT
2451 continue;
2452
7b9f3c50
DE
2453 file_data = dw2_get_file_names (objfile, per_cu);
2454 if (file_data == NULL)
9291a0cd
TT
2455 continue;
2456
7b9f3c50 2457 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2458 {
7b9f3c50 2459 const char *this_name = file_data->file_names[j];
9291a0cd 2460
4aac40c8
TT
2461 if (FILENAME_CMP (name, this_name) == 0
2462 || (!is_abs && compare_filenames_for_search (this_name,
2463 name, name_len)))
9291a0cd 2464 {
f8eba3c6
TT
2465 if (dw2_map_expand_apply (objfile, per_cu,
2466 name, full_path, real_path,
2467 callback, data))
2468 return 1;
4aac40c8 2469 }
9291a0cd 2470
c011a4f4
DE
2471 /* Before we invoke realpath, which can get expensive when many
2472 files are involved, do a quick comparison of the basenames. */
2473 if (! basenames_may_differ
2474 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2475 continue;
2476
9291a0cd
TT
2477 if (full_path != NULL)
2478 {
7b9f3c50
DE
2479 const char *this_real_name = dw2_get_real_path (objfile,
2480 file_data, j);
9291a0cd 2481
7b9f3c50 2482 if (this_real_name != NULL
4aac40c8
TT
2483 && (FILENAME_CMP (full_path, this_real_name) == 0
2484 || (!is_abs
2485 && compare_filenames_for_search (this_real_name,
2486 name, name_len))))
9291a0cd 2487 {
f8eba3c6
TT
2488 if (dw2_map_expand_apply (objfile, per_cu,
2489 name, full_path, real_path,
2490 callback, data))
2491 return 1;
9291a0cd
TT
2492 }
2493 }
2494
2495 if (real_path != NULL)
2496 {
7b9f3c50
DE
2497 const char *this_real_name = dw2_get_real_path (objfile,
2498 file_data, j);
9291a0cd 2499
7b9f3c50 2500 if (this_real_name != NULL
4aac40c8
TT
2501 && (FILENAME_CMP (real_path, this_real_name) == 0
2502 || (!is_abs
2503 && compare_filenames_for_search (this_real_name,
2504 name, name_len))))
9291a0cd 2505 {
f8eba3c6
TT
2506 if (dw2_map_expand_apply (objfile, per_cu,
2507 name, full_path, real_path,
2508 callback, data))
2509 return 1;
9291a0cd
TT
2510 }
2511 }
2512 }
2513 }
2514
9291a0cd
TT
2515 return 0;
2516}
2517
2518static struct symtab *
2519dw2_lookup_symbol (struct objfile *objfile, int block_index,
2520 const char *name, domain_enum domain)
2521{
774b6a14 2522 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2523 instead. */
2524 return NULL;
2525}
2526
2527/* A helper function that expands all symtabs that hold an object
2528 named NAME. */
2fdf6df6 2529
9291a0cd
TT
2530static void
2531dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2532{
2533 dw2_setup (objfile);
2534
ae2de4f8 2535 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2536 if (dwarf2_per_objfile->index_table)
2537 {
2538 offset_type *vec;
2539
2540 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2541 name, &vec))
2542 {
2543 offset_type i, len = MAYBE_SWAP (*vec);
2544 for (i = 0; i < len; ++i)
2545 {
2546 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2547 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2548
a0f42c21 2549 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2550 }
2551 }
2552 }
2553}
2554
774b6a14
TT
2555static void
2556dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2557 enum block_enum block_kind, const char *name,
774b6a14 2558 domain_enum domain)
9291a0cd 2559{
774b6a14 2560 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2561}
2562
2563static void
2564dw2_print_stats (struct objfile *objfile)
2565{
2566 int i, count;
2567
2568 dw2_setup (objfile);
2569 count = 0;
1fd400ff 2570 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2571 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2572 {
e254ef6a 2573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2574
e254ef6a 2575 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2576 ++count;
2577 }
2578 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2579}
2580
2581static void
2582dw2_dump (struct objfile *objfile)
2583{
2584 /* Nothing worth printing. */
2585}
2586
2587static void
2588dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2589 struct section_offsets *delta)
2590{
2591 /* There's nothing to relocate here. */
2592}
2593
2594static void
2595dw2_expand_symtabs_for_function (struct objfile *objfile,
2596 const char *func_name)
2597{
2598 dw2_do_expand_symtabs_matching (objfile, func_name);
2599}
2600
2601static void
2602dw2_expand_all_symtabs (struct objfile *objfile)
2603{
2604 int i;
2605
2606 dw2_setup (objfile);
1fd400ff
TT
2607
2608 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2609 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2610 {
e254ef6a 2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2612
a0f42c21 2613 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2614 }
2615}
2616
2617static void
2618dw2_expand_symtabs_with_filename (struct objfile *objfile,
2619 const char *filename)
2620{
2621 int i;
2622
2623 dw2_setup (objfile);
d4637a04
DE
2624
2625 /* We don't need to consider type units here.
2626 This is only called for examining code, e.g. expand_line_sal.
2627 There can be an order of magnitude (or more) more type units
2628 than comp units, and we avoid them if we can. */
2629
2630 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2631 {
2632 int j;
e254ef6a 2633 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2634 struct quick_file_names *file_data;
9291a0cd 2635
3d7bb9d9 2636 /* We only need to look at symtabs not already expanded. */
e254ef6a 2637 if (per_cu->v.quick->symtab)
9291a0cd
TT
2638 continue;
2639
7b9f3c50
DE
2640 file_data = dw2_get_file_names (objfile, per_cu);
2641 if (file_data == NULL)
9291a0cd
TT
2642 continue;
2643
7b9f3c50 2644 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2645 {
7b9f3c50 2646 const char *this_name = file_data->file_names[j];
1ef75ecc 2647 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2648 {
a0f42c21 2649 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2650 break;
2651 }
2652 }
2653 }
2654}
2655
dd786858 2656static const char *
9291a0cd
TT
2657dw2_find_symbol_file (struct objfile *objfile, const char *name)
2658{
e254ef6a 2659 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2660 offset_type *vec;
7b9f3c50 2661 struct quick_file_names *file_data;
9291a0cd
TT
2662
2663 dw2_setup (objfile);
2664
ae2de4f8 2665 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2666 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2667 {
2668 struct symtab *s;
2669
2670 ALL_OBJFILE_SYMTABS (objfile, s)
2671 if (s->primary)
2672 {
2673 struct blockvector *bv = BLOCKVECTOR (s);
2674 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2675 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2676
2677 if (sym)
2678 return sym->symtab->filename;
2679 }
2680 return NULL;
2681 }
9291a0cd
TT
2682
2683 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2684 name, &vec))
2685 return NULL;
2686
2687 /* Note that this just looks at the very first one named NAME -- but
2688 actually we are looking for a function. find_main_filename
2689 should be rewritten so that it doesn't require a custom hook. It
2690 could just use the ordinary symbol tables. */
2691 /* vec[0] is the length, which must always be >0. */
e254ef6a 2692 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2693
7b9f3c50
DE
2694 file_data = dw2_get_file_names (objfile, per_cu);
2695 if (file_data == NULL)
9291a0cd
TT
2696 return NULL;
2697
7b9f3c50 2698 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2699}
2700
2701static void
40658b94
PH
2702dw2_map_matching_symbols (const char * name, domain_enum namespace,
2703 struct objfile *objfile, int global,
2704 int (*callback) (struct block *,
2705 struct symbol *, void *),
2edb89d3
JK
2706 void *data, symbol_compare_ftype *match,
2707 symbol_compare_ftype *ordered_compare)
9291a0cd 2708{
40658b94 2709 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2710 current language is Ada for a non-Ada objfile using GNU index. As Ada
2711 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2712}
2713
2714static void
f8eba3c6
TT
2715dw2_expand_symtabs_matching
2716 (struct objfile *objfile,
2717 int (*file_matcher) (const char *, void *),
e078317b 2718 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2719 enum search_domain kind,
2720 void *data)
9291a0cd
TT
2721{
2722 int i;
2723 offset_type iter;
4b5246aa 2724 struct mapped_index *index;
9291a0cd
TT
2725
2726 dw2_setup (objfile);
ae2de4f8
DE
2727
2728 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2729 if (!dwarf2_per_objfile->index_table)
2730 return;
4b5246aa 2731 index = dwarf2_per_objfile->index_table;
9291a0cd 2732
7b08b9eb 2733 if (file_matcher != NULL)
24c79950
TT
2734 {
2735 struct cleanup *cleanup;
2736 htab_t visited_found, visited_not_found;
2737
2738 visited_found = htab_create_alloc (10,
2739 htab_hash_pointer, htab_eq_pointer,
2740 NULL, xcalloc, xfree);
2741 cleanup = make_cleanup_htab_delete (visited_found);
2742 visited_not_found = htab_create_alloc (10,
2743 htab_hash_pointer, htab_eq_pointer,
2744 NULL, xcalloc, xfree);
2745 make_cleanup_htab_delete (visited_not_found);
2746
2747 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2748 + dwarf2_per_objfile->n_type_units); ++i)
2749 {
2750 int j;
2751 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2752 struct quick_file_names *file_data;
2753 void **slot;
7b08b9eb 2754
24c79950 2755 per_cu->v.quick->mark = 0;
3d7bb9d9 2756
24c79950
TT
2757 /* We only need to look at symtabs not already expanded. */
2758 if (per_cu->v.quick->symtab)
2759 continue;
7b08b9eb 2760
24c79950
TT
2761 file_data = dw2_get_file_names (objfile, per_cu);
2762 if (file_data == NULL)
2763 continue;
7b08b9eb 2764
24c79950
TT
2765 if (htab_find (visited_not_found, file_data) != NULL)
2766 continue;
2767 else if (htab_find (visited_found, file_data) != NULL)
2768 {
2769 per_cu->v.quick->mark = 1;
2770 continue;
2771 }
2772
2773 for (j = 0; j < file_data->num_file_names; ++j)
2774 {
2775 if (file_matcher (file_data->file_names[j], data))
2776 {
2777 per_cu->v.quick->mark = 1;
2778 break;
2779 }
2780 }
2781
2782 slot = htab_find_slot (per_cu->v.quick->mark
2783 ? visited_found
2784 : visited_not_found,
2785 file_data, INSERT);
2786 *slot = file_data;
2787 }
2788
2789 do_cleanups (cleanup);
2790 }
9291a0cd 2791
3876f04e 2792 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2793 {
2794 offset_type idx = 2 * iter;
2795 const char *name;
2796 offset_type *vec, vec_len, vec_idx;
2797
3876f04e 2798 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2799 continue;
2800
3876f04e 2801 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2802
e078317b 2803 if (! (*name_matcher) (name, data))
9291a0cd
TT
2804 continue;
2805
2806 /* The name was matched, now expand corresponding CUs that were
2807 marked. */
4b5246aa 2808 vec = (offset_type *) (index->constant_pool
3876f04e 2809 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2810 vec_len = MAYBE_SWAP (vec[0]);
2811 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2812 {
e254ef6a 2813 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2814
e254ef6a 2815 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2816 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2817 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2818 }
2819 }
2820}
2821
2822static struct symtab *
2823dw2_find_pc_sect_symtab (struct objfile *objfile,
2824 struct minimal_symbol *msymbol,
2825 CORE_ADDR pc,
2826 struct obj_section *section,
2827 int warn_if_readin)
2828{
2829 struct dwarf2_per_cu_data *data;
2830
2831 dw2_setup (objfile);
2832
2833 if (!objfile->psymtabs_addrmap)
2834 return NULL;
2835
2836 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2837 if (!data)
2838 return NULL;
2839
2840 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2841 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2842 paddress (get_objfile_arch (objfile), pc));
2843
a0f42c21 2844 return dw2_instantiate_symtab (data);
9291a0cd
TT
2845}
2846
9291a0cd 2847static void
44b13c5a 2848dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2849 void *data, int need_fullname)
9291a0cd
TT
2850{
2851 int i;
24c79950
TT
2852 struct cleanup *cleanup;
2853 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2854 NULL, xcalloc, xfree);
9291a0cd 2855
24c79950 2856 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2857 dw2_setup (objfile);
ae2de4f8 2858
24c79950
TT
2859 /* We can ignore file names coming from already-expanded CUs. */
2860 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2861 + dwarf2_per_objfile->n_type_units); ++i)
2862 {
2863 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2864
2865 if (per_cu->v.quick->symtab)
2866 {
2867 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2868 INSERT);
2869
2870 *slot = per_cu->v.quick->file_names;
2871 }
2872 }
2873
1fd400ff 2874 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2875 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2876 {
2877 int j;
e254ef6a 2878 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2879 struct quick_file_names *file_data;
24c79950 2880 void **slot;
9291a0cd 2881
3d7bb9d9 2882 /* We only need to look at symtabs not already expanded. */
e254ef6a 2883 if (per_cu->v.quick->symtab)
9291a0cd
TT
2884 continue;
2885
7b9f3c50
DE
2886 file_data = dw2_get_file_names (objfile, per_cu);
2887 if (file_data == NULL)
9291a0cd
TT
2888 continue;
2889
24c79950
TT
2890 slot = htab_find_slot (visited, file_data, INSERT);
2891 if (*slot)
2892 {
2893 /* Already visited. */
2894 continue;
2895 }
2896 *slot = file_data;
2897
7b9f3c50 2898 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2899 {
74e2f255
DE
2900 const char *this_real_name;
2901
2902 if (need_fullname)
2903 this_real_name = dw2_get_real_path (objfile, file_data, j);
2904 else
2905 this_real_name = NULL;
7b9f3c50 2906 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2907 }
2908 }
24c79950
TT
2909
2910 do_cleanups (cleanup);
9291a0cd
TT
2911}
2912
2913static int
2914dw2_has_symbols (struct objfile *objfile)
2915{
2916 return 1;
2917}
2918
2919const struct quick_symbol_functions dwarf2_gdb_index_functions =
2920{
2921 dw2_has_symbols,
2922 dw2_find_last_source_symtab,
2923 dw2_forget_cached_source_info,
f8eba3c6 2924 dw2_map_symtabs_matching_filename,
9291a0cd 2925 dw2_lookup_symbol,
774b6a14 2926 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2927 dw2_print_stats,
2928 dw2_dump,
2929 dw2_relocate,
2930 dw2_expand_symtabs_for_function,
2931 dw2_expand_all_symtabs,
2932 dw2_expand_symtabs_with_filename,
2933 dw2_find_symbol_file,
40658b94 2934 dw2_map_matching_symbols,
9291a0cd
TT
2935 dw2_expand_symtabs_matching,
2936 dw2_find_pc_sect_symtab,
9291a0cd
TT
2937 dw2_map_symbol_filenames
2938};
2939
2940/* Initialize for reading DWARF for this objfile. Return 0 if this
2941 file will use psymtabs, or 1 if using the GNU index. */
2942
2943int
2944dwarf2_initialize_objfile (struct objfile *objfile)
2945{
2946 /* If we're about to read full symbols, don't bother with the
2947 indices. In this case we also don't care if some other debug
2948 format is making psymtabs, because they are all about to be
2949 expanded anyway. */
2950 if ((objfile->flags & OBJF_READNOW))
2951 {
2952 int i;
2953
2954 dwarf2_per_objfile->using_index = 1;
2955 create_all_comp_units (objfile);
0e50663e 2956 create_all_type_units (objfile);
7b9f3c50
DE
2957 dwarf2_per_objfile->quick_file_names_table =
2958 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2959
1fd400ff 2960 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2961 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2962 {
e254ef6a 2963 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2964
e254ef6a
DE
2965 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2967 }
2968
2969 /* Return 1 so that gdb sees the "quick" functions. However,
2970 these functions will be no-ops because we will have expanded
2971 all symtabs. */
2972 return 1;
2973 }
2974
2975 if (dwarf2_read_index (objfile))
2976 return 1;
2977
9291a0cd
TT
2978 return 0;
2979}
2980
2981\f
2982
dce234bc
PP
2983/* Build a partial symbol table. */
2984
2985void
f29dff0a 2986dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2987{
f29dff0a 2988 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2989 {
2990 init_psymbol_list (objfile, 1024);
2991 }
2992
d146bf1e 2993 dwarf2_build_psymtabs_hard (objfile);
c906108c 2994}
c906108c 2995
45452591
DE
2996/* Return TRUE if OFFSET is within CU_HEADER. */
2997
2998static inline int
b64f50a1 2999offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3000{
b64f50a1
JK
3001 sect_offset bottom = { cu_header->offset.sect_off };
3002 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3003 + cu_header->initial_length_size) };
9a619af0 3004
b64f50a1 3005 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3006}
3007
93311388
DE
3008/* Read in the comp unit header information from the debug_info at info_ptr.
3009 NOTE: This leaves members offset, first_die_offset to be filled in
3010 by the caller. */
107d2387 3011
fe1b8b76 3012static gdb_byte *
107d2387 3013read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3014 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3015{
3016 int signed_addr;
891d2f0b 3017 unsigned int bytes_read;
c764a876
DE
3018
3019 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3020 cu_header->initial_length_size = bytes_read;
3021 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3022 info_ptr += bytes_read;
107d2387
AC
3023 cu_header->version = read_2_bytes (abfd, info_ptr);
3024 info_ptr += 2;
b64f50a1
JK
3025 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3026 &bytes_read);
613e1657 3027 info_ptr += bytes_read;
107d2387
AC
3028 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3029 info_ptr += 1;
3030 signed_addr = bfd_get_sign_extend_vma (abfd);
3031 if (signed_addr < 0)
8e65ff28 3032 internal_error (__FILE__, __LINE__,
e2e0b3e5 3033 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3034 cu_header->signed_addr_p = signed_addr;
c764a876 3035
107d2387
AC
3036 return info_ptr;
3037}
3038
9ff913ba
DE
3039/* Subroutine of read_and_check_comp_unit_head and
3040 read_and_check_type_unit_head to simplify them.
3041 Perform various error checking on the header. */
3042
3043static void
3044error_check_comp_unit_head (struct comp_unit_head *header,
3045 struct dwarf2_section_info *section)
3046{
3047 bfd *abfd = section->asection->owner;
3048 const char *filename = bfd_get_filename (abfd);
3049
3050 if (header->version != 2 && header->version != 3 && header->version != 4)
3051 error (_("Dwarf Error: wrong version in compilation unit header "
3052 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3053 filename);
3054
b64f50a1 3055 if (header->abbrev_offset.sect_off
9ff913ba
DE
3056 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3057 &dwarf2_per_objfile->abbrev))
3058 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3059 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3060 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3061 filename);
3062
3063 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3064 avoid potential 32-bit overflow. */
b64f50a1 3065 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3066 + header->length + header->initial_length_size)
3067 > section->size)
3068 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3069 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3070 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3071 filename);
3072}
3073
3074/* Read in a CU/TU header and perform some basic error checking.
3075 The contents of the header are stored in HEADER.
3076 The result is a pointer to the start of the first DIE. */
adabb602 3077
fe1b8b76 3078static gdb_byte *
9ff913ba
DE
3079read_and_check_comp_unit_head (struct comp_unit_head *header,
3080 struct dwarf2_section_info *section,
3081 gdb_byte *info_ptr,
3082 int is_debug_types_section)
72bf9492 3083{
fe1b8b76 3084 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3085 bfd *abfd = section->asection->owner;
72bf9492 3086
b64f50a1 3087 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3088
72bf9492
DJ
3089 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3090
460c1c54
CC
3091 /* If we're reading a type unit, skip over the signature and
3092 type_offset fields. */
b0df02fd 3093 if (is_debug_types_section)
460c1c54
CC
3094 info_ptr += 8 /*signature*/ + header->offset_size;
3095
b64f50a1 3096 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3097
9ff913ba 3098 error_check_comp_unit_head (header, section);
72bf9492
DJ
3099
3100 return info_ptr;
3101}
3102
348e048f
DE
3103/* Read in the types comp unit header information from .debug_types entry at
3104 types_ptr. The result is a pointer to one past the end of the header. */
3105
3106static gdb_byte *
9ff913ba
DE
3107read_and_check_type_unit_head (struct comp_unit_head *header,
3108 struct dwarf2_section_info *section,
3109 gdb_byte *info_ptr,
b64f50a1 3110 ULONGEST *signature, cu_offset *type_offset)
348e048f 3111{
9ff913ba
DE
3112 gdb_byte *beg_of_comp_unit = info_ptr;
3113 bfd *abfd = section->asection->owner;
348e048f 3114
b64f50a1 3115 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3116
9ff913ba 3117 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3118
9ff913ba
DE
3119 /* If we're reading a type unit, skip over the signature and
3120 type_offset fields. */
3121 if (signature != NULL)
3122 *signature = read_8_bytes (abfd, info_ptr);
3123 info_ptr += 8;
3124 if (type_offset != NULL)
b64f50a1 3125 type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
9ff913ba
DE
3126 info_ptr += header->offset_size;
3127
b64f50a1 3128 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3129
9ff913ba
DE
3130 error_check_comp_unit_head (header, section);
3131
3132 return info_ptr;
348e048f
DE
3133}
3134
aaa75496
JB
3135/* Allocate a new partial symtab for file named NAME and mark this new
3136 partial symtab as being an include of PST. */
3137
3138static void
3139dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3140 struct objfile *objfile)
3141{
3142 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3143
3144 subpst->section_offsets = pst->section_offsets;
3145 subpst->textlow = 0;
3146 subpst->texthigh = 0;
3147
3148 subpst->dependencies = (struct partial_symtab **)
3149 obstack_alloc (&objfile->objfile_obstack,
3150 sizeof (struct partial_symtab *));
3151 subpst->dependencies[0] = pst;
3152 subpst->number_of_dependencies = 1;
3153
3154 subpst->globals_offset = 0;
3155 subpst->n_global_syms = 0;
3156 subpst->statics_offset = 0;
3157 subpst->n_static_syms = 0;
3158 subpst->symtab = NULL;
3159 subpst->read_symtab = pst->read_symtab;
3160 subpst->readin = 0;
3161
3162 /* No private part is necessary for include psymtabs. This property
3163 can be used to differentiate between such include psymtabs and
10b3939b 3164 the regular ones. */
58a9656e 3165 subpst->read_symtab_private = NULL;
aaa75496
JB
3166}
3167
3168/* Read the Line Number Program data and extract the list of files
3169 included by the source file represented by PST. Build an include
d85a05f0 3170 partial symtab for each of these included files. */
aaa75496
JB
3171
3172static void
3173dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3174 struct die_info *die,
aaa75496
JB
3175 struct partial_symtab *pst)
3176{
3177 struct objfile *objfile = cu->objfile;
3178 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3179 struct line_header *lh = NULL;
3180 struct attribute *attr;
aaa75496 3181
d85a05f0
DJ
3182 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3183 if (attr)
3184 {
3185 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3186
d85a05f0
DJ
3187 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3188 }
aaa75496
JB
3189 if (lh == NULL)
3190 return; /* No linetable, so no includes. */
3191
c6da4cef 3192 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3193 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3194
3195 free_line_header (lh);
3196}
3197
348e048f 3198static hashval_t
52dc124a 3199hash_signatured_type (const void *item)
348e048f 3200{
52dc124a 3201 const struct signatured_type *sig_type = item;
9a619af0 3202
348e048f 3203 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3204 return sig_type->signature;
348e048f
DE
3205}
3206
3207static int
52dc124a 3208eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3209{
3210 const struct signatured_type *lhs = item_lhs;
3211 const struct signatured_type *rhs = item_rhs;
9a619af0 3212
348e048f
DE
3213 return lhs->signature == rhs->signature;
3214}
3215
1fd400ff
TT
3216/* Allocate a hash table for signatured types. */
3217
3218static htab_t
673bfd45 3219allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3220{
3221 return htab_create_alloc_ex (41,
52dc124a
DE
3222 hash_signatured_type,
3223 eq_signatured_type,
1fd400ff
TT
3224 NULL,
3225 &objfile->objfile_obstack,
3226 hashtab_obstack_allocate,
3227 dummy_obstack_deallocate);
3228}
3229
d467dd73 3230/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3231
3232static int
d467dd73 3233add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3234{
3235 struct signatured_type *sigt = *slot;
3236 struct dwarf2_per_cu_data ***datap = datum;
3237
3238 **datap = &sigt->per_cu;
3239 ++*datap;
3240
3241 return 1;
3242}
3243
d467dd73 3244/* Create the hash table of all entries in the .debug_types section(s).
0e50663e
DE
3245 The result is zero if there are no .debug_types sections,
3246 otherwise non-zero. */
348e048f
DE
3247
3248static int
0e50663e 3249create_all_type_units (struct objfile *objfile)
348e048f 3250{
8b70b953 3251 htab_t types_htab = NULL;
1fd400ff 3252 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3253 int ix;
3254 struct dwarf2_section_info *section;
348e048f 3255
8b70b953 3256 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3257 {
3258 dwarf2_per_objfile->signatured_types = NULL;
3259 return 0;
3260 }
3261
8b70b953
TT
3262 for (ix = 0;
3263 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3264 ix, section);
3265 ++ix)
3266 {
3267 gdb_byte *info_ptr, *end_ptr;
348e048f 3268
8b70b953
TT
3269 dwarf2_read_section (objfile, section);
3270 info_ptr = section->buffer;
348e048f 3271
8b70b953
TT
3272 if (info_ptr == NULL)
3273 continue;
348e048f 3274
8b70b953
TT
3275 if (types_htab == NULL)
3276 types_htab = allocate_signatured_type_table (objfile);
348e048f 3277
8b70b953
TT
3278 if (dwarf2_die_debug)
3279 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3280
3281 end_ptr = info_ptr + section->size;
3282 while (info_ptr < end_ptr)
3283 {
b64f50a1
JK
3284 sect_offset offset;
3285 cu_offset type_offset;
8b70b953 3286 ULONGEST signature;
52dc124a 3287 struct signatured_type *sig_type;
8b70b953
TT
3288 void **slot;
3289 gdb_byte *ptr = info_ptr;
9ff913ba 3290 struct comp_unit_head header;
348e048f 3291
b64f50a1 3292 offset.sect_off = ptr - section->buffer;
348e048f 3293
8b70b953 3294 /* We need to read the type's signature in order to build the hash
9ff913ba 3295 table, but we don't need anything else just yet. */
348e048f 3296
9ff913ba
DE
3297 ptr = read_and_check_type_unit_head (&header, section, ptr,
3298 &signature, &type_offset);
6caca83c
CC
3299
3300 /* Skip dummy type units. */
3301 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3302 {
9ff913ba 3303 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3304 continue;
3305 }
8b70b953 3306
52dc124a
DE
3307 sig_type = obstack_alloc (&objfile->objfile_obstack, sizeof (*sig_type));
3308 memset (sig_type, 0, sizeof (*sig_type));
3309 sig_type->signature = signature;
3310 sig_type->type_offset = type_offset;
3311 sig_type->per_cu.objfile = objfile;
3312 sig_type->per_cu.debug_types_section = section;
3313 sig_type->per_cu.offset = offset;
8b70b953 3314
52dc124a 3315 slot = htab_find_slot (types_htab, sig_type, INSERT);
8b70b953
TT
3316 gdb_assert (slot != NULL);
3317 if (*slot != NULL)
3318 {
3319 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3320
8b70b953
TT
3321 complaint (&symfile_complaints,
3322 _("debug type entry at offset 0x%x is duplicate to the "
3323 "entry at offset 0x%x, signature 0x%s"),
b64f50a1 3324 offset.sect_off, dup_sig->per_cu.offset.sect_off,
8b70b953
TT
3325 phex (signature, sizeof (signature)));
3326 gdb_assert (signature == dup_sig->signature);
3327 }
52dc124a 3328 *slot = sig_type;
348e048f 3329
8b70b953
TT
3330 if (dwarf2_die_debug)
3331 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3332 offset.sect_off,
3333 phex (signature, sizeof (signature)));
348e048f 3334
9ff913ba 3335 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3336 }
348e048f
DE
3337 }
3338
3339 dwarf2_per_objfile->signatured_types = types_htab;
3340
d467dd73
DE
3341 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3342 dwarf2_per_objfile->all_type_units
1fd400ff 3343 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3344 dwarf2_per_objfile->n_type_units
1fd400ff 3345 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3346 iter = &dwarf2_per_objfile->all_type_units[0];
3347 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3348 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3349 == dwarf2_per_objfile->n_type_units);
1fd400ff 3350
348e048f
DE
3351 return 1;
3352}
3353
380bca97 3354/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3355 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3356
3357static struct signatured_type *
e319fa28 3358lookup_signatured_type (ULONGEST sig)
348e048f
DE
3359{
3360 struct signatured_type find_entry, *entry;
3361
3362 if (dwarf2_per_objfile->signatured_types == NULL)
3363 {
3364 complaint (&symfile_complaints,
55f1336d 3365 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3366 return NULL;
348e048f
DE
3367 }
3368
3369 find_entry.signature = sig;
3370 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3371 return entry;
3372}
3373
d85a05f0
DJ
3374/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3375
3376static void
3377init_cu_die_reader (struct die_reader_specs *reader,
3378 struct dwarf2_cu *cu)
3379{
3380 reader->abfd = cu->objfile->obfd;
3381 reader->cu = cu;
b0df02fd 3382 if (cu->per_cu->debug_types_section)
be391dca 3383 {
b0df02fd
DE
3384 gdb_assert (cu->per_cu->debug_types_section->readin);
3385 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3386 }
d85a05f0 3387 else
be391dca
TT
3388 {
3389 gdb_assert (dwarf2_per_objfile->info.readin);
3390 reader->buffer = dwarf2_per_objfile->info.buffer;
3391 }
d85a05f0
DJ
3392}
3393
3394/* Find the base address of the compilation unit for range lists and
3395 location lists. It will normally be specified by DW_AT_low_pc.
3396 In DWARF-3 draft 4, the base address could be overridden by
3397 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3398 compilation units with discontinuous ranges. */
3399
3400static void
3401dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3402{
3403 struct attribute *attr;
3404
3405 cu->base_known = 0;
3406 cu->base_address = 0;
3407
3408 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3409 if (attr)
3410 {
3411 cu->base_address = DW_ADDR (attr);
3412 cu->base_known = 1;
3413 }
3414 else
3415 {
3416 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3417 if (attr)
3418 {
3419 cu->base_address = DW_ADDR (attr);
3420 cu->base_known = 1;
3421 }
3422 }
3423}
3424
348e048f
DE
3425/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3426 to combine the common parts.
21b2bd31
DE
3427 Process compilation unit THIS_CU for a psymtab.
3428 SECTION is the section the CU/TU comes from,
3429 either .debug_info or .debug_types. */
aaa75496 3430
70221824 3431static void
a0f42c21 3432process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3433 struct dwarf2_section_info *section,
3434 int is_debug_types_section)
c906108c 3435{
a0f42c21 3436 struct objfile *objfile = this_cu->objfile;
c906108c 3437 bfd *abfd = objfile->obfd;
21b2bd31 3438 gdb_byte *buffer = section->buffer;
b64f50a1 3439 gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
21b2bd31 3440 unsigned int buffer_size = section->size;
93311388 3441 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3442 struct die_info *comp_unit_die;
c906108c 3443 struct partial_symtab *pst;
5734ee8b 3444 CORE_ADDR baseaddr;
93311388
DE
3445 struct cleanup *back_to_inner;
3446 struct dwarf2_cu cu;
d85a05f0
DJ
3447 int has_children, has_pc_info;
3448 struct attribute *attr;
d85a05f0
DJ
3449 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3450 struct die_reader_specs reader_specs;
3e2a0cee 3451 const char *filename;
c906108c 3452
23745b47
DE
3453 /* If this compilation unit was already read in, free the
3454 cached copy in order to read it in again. This is
3455 necessary because we skipped some symbols when we first
3456 read in the compilation unit (see load_partial_dies).
3457 This problem could be avoided, but the benefit is
3458 unclear. */
3459 if (this_cu->cu != NULL)
3460 free_one_cached_comp_unit (this_cu->cu);
3461
3462 /* Note that this is a pointer to our stack frame, being
3463 added to a global data structure. It will be cleaned up
3464 in free_stack_comp_unit when we finish with this
3465 compilation unit. */
3466 init_one_comp_unit (&cu, this_cu);
93311388 3467 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3468
9ff913ba
DE
3469 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3470 is_debug_types_section);
10b3939b 3471
6caca83c
CC
3472 /* Skip dummy compilation units. */
3473 if (info_ptr >= buffer + buffer_size
3474 || peek_abbrev_code (abfd, info_ptr) == 0)
3475 {
6caca83c 3476 do_cleanups (back_to_inner);
21b2bd31 3477 return;
6caca83c
CC
3478 }
3479
93311388 3480 cu.list_in_scope = &file_symbols;
af703f96 3481
93311388 3482 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3483 dwarf2_read_abbrevs (&cu);
93311388 3484 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3485
93311388 3486 /* Read the compilation unit die. */
d85a05f0
DJ
3487 init_cu_die_reader (&reader_specs, &cu);
3488 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3489 &has_children);
93311388 3490
21b2bd31 3491 if (is_debug_types_section)
348e048f 3492 {
b3c8eb43 3493 /* LENGTH has not been set yet for type units. */
b64f50a1 3494 gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
348e048f
DE
3495 this_cu->length = cu.header.length + cu.header.initial_length_size;
3496 }
d85a05f0 3497 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3498 {
93311388 3499 do_cleanups (back_to_inner);
21b2bd31 3500 return;
93311388 3501 }
72bf9492 3502
9816fde3 3503 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3504
93311388 3505 /* Allocate a new partial symbol table structure. */
d85a05f0 3506 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3507 if (attr == NULL || !DW_STRING (attr))
3508 filename = "";
3509 else
3510 filename = DW_STRING (attr);
93311388 3511 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3512 filename,
93311388
DE
3513 /* TEXTLOW and TEXTHIGH are set below. */
3514 0,
3515 objfile->global_psymbols.next,
3516 objfile->static_psymbols.next);
9750bca9 3517 pst->psymtabs_addrmap_supported = 1;
72bf9492 3518
d85a05f0
DJ
3519 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3520 if (attr != NULL)
3521 pst->dirname = DW_STRING (attr);
72bf9492 3522
e38df1d0 3523 pst->read_symtab_private = this_cu;
72bf9492 3524
93311388 3525 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3526
0963b4bd 3527 /* Store the function that reads in the rest of the symbol table. */
93311388 3528 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3529
9291a0cd 3530 this_cu->v.psymtab = pst;
c906108c 3531
d85a05f0
DJ
3532 dwarf2_find_base_address (comp_unit_die, &cu);
3533
93311388
DE
3534 /* Possibly set the default values of LOWPC and HIGHPC from
3535 `DW_AT_ranges'. */
d85a05f0
DJ
3536 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3537 &best_highpc, &cu, pst);
3538 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3539 /* Store the contiguous range if it is not empty; it can be empty for
3540 CUs with no code. */
3541 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3542 best_lowpc + baseaddr,
3543 best_highpc + baseaddr - 1, pst);
93311388
DE
3544
3545 /* Check if comp unit has_children.
3546 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3547 If not, there's no more debug_info for this comp unit. */
d85a05f0 3548 if (has_children)
93311388
DE
3549 {
3550 struct partial_die_info *first_die;
3551 CORE_ADDR lowpc, highpc;
31ffec48 3552
93311388
DE
3553 lowpc = ((CORE_ADDR) -1);
3554 highpc = ((CORE_ADDR) 0);
c906108c 3555
93311388 3556 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3557
93311388 3558 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3559 ! has_pc_info, &cu);
57c22c6c 3560
93311388
DE
3561 /* If we didn't find a lowpc, set it to highpc to avoid
3562 complaints from `maint check'. */
3563 if (lowpc == ((CORE_ADDR) -1))
3564 lowpc = highpc;
10b3939b 3565
93311388
DE
3566 /* If the compilation unit didn't have an explicit address range,
3567 then use the information extracted from its child dies. */
d85a05f0 3568 if (! has_pc_info)
93311388 3569 {
d85a05f0
DJ
3570 best_lowpc = lowpc;
3571 best_highpc = highpc;
93311388
DE
3572 }
3573 }
d85a05f0
DJ
3574 pst->textlow = best_lowpc + baseaddr;
3575 pst->texthigh = best_highpc + baseaddr;
c906108c 3576
93311388
DE
3577 pst->n_global_syms = objfile->global_psymbols.next -
3578 (objfile->global_psymbols.list + pst->globals_offset);
3579 pst->n_static_syms = objfile->static_psymbols.next -
3580 (objfile->static_psymbols.list + pst->statics_offset);
3581 sort_pst_symbols (pst);
c906108c 3582
21b2bd31 3583 if (is_debug_types_section)
348e048f
DE
3584 {
3585 /* It's not clear we want to do anything with stmt lists here.
3586 Waiting to see what gcc ultimately does. */
3587 }
d85a05f0 3588 else
93311388
DE
3589 {
3590 /* Get the list of files included in the current compilation unit,
3591 and build a psymtab for each of them. */
d85a05f0 3592 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3593 }
ae038cb0 3594
93311388 3595 do_cleanups (back_to_inner);
93311388 3596}
ff013f42 3597
348e048f
DE
3598/* Traversal function for htab_traverse_noresize.
3599 Process one .debug_types comp-unit. */
3600
3601static int
3602process_type_comp_unit (void **slot, void *info)
3603{
3604 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3605 struct dwarf2_per_cu_data *this_cu;
3606
a0f42c21 3607 gdb_assert (info == NULL);
348e048f 3608 this_cu = &entry->per_cu;
348e048f 3609
b0df02fd 3610 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3611 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3612
3613 return 1;
3614}
3615
3616/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3617 Build partial symbol tables for the .debug_types comp-units. */
3618
3619static void
3620build_type_psymtabs (struct objfile *objfile)
3621{
0e50663e 3622 if (! create_all_type_units (objfile))
348e048f
DE
3623 return;
3624
3625 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3626 process_type_comp_unit, NULL);
348e048f
DE
3627}
3628
60606b2c
TT
3629/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3630
3631static void
3632psymtabs_addrmap_cleanup (void *o)
3633{
3634 struct objfile *objfile = o;
ec61707d 3635
60606b2c
TT
3636 objfile->psymtabs_addrmap = NULL;
3637}
3638
93311388
DE
3639/* Build the partial symbol table by doing a quick pass through the
3640 .debug_info and .debug_abbrev sections. */
72bf9492 3641
93311388 3642static void
c67a9c90 3643dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3644{
60606b2c
TT
3645 struct cleanup *back_to, *addrmap_cleanup;
3646 struct obstack temp_obstack;
21b2bd31 3647 int i;
93311388 3648
98bfdba5
PA
3649 dwarf2_per_objfile->reading_partial_symbols = 1;
3650
be391dca 3651 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3652
93311388
DE
3653 /* Any cached compilation units will be linked by the per-objfile
3654 read_in_chain. Make sure to free them when we're done. */
3655 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3656
348e048f
DE
3657 build_type_psymtabs (objfile);
3658
93311388 3659 create_all_comp_units (objfile);
c906108c 3660
60606b2c
TT
3661 /* Create a temporary address map on a temporary obstack. We later
3662 copy this to the final obstack. */
3663 obstack_init (&temp_obstack);
3664 make_cleanup_obstack_free (&temp_obstack);
3665 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3666 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3667
21b2bd31 3668 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3669 {
21b2bd31 3670 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3671
21b2bd31 3672 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3673 }
ff013f42
JK
3674
3675 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3676 &objfile->objfile_obstack);
60606b2c 3677 discard_cleanups (addrmap_cleanup);
ff013f42 3678
ae038cb0
DJ
3679 do_cleanups (back_to);
3680}
3681
93311388 3682/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3683
3684static void
a0f42c21 3685load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3686{
a0f42c21 3687 struct objfile *objfile = this_cu->objfile;
ae038cb0 3688 bfd *abfd = objfile->obfd;
adabb602 3689 gdb_byte *info_ptr;
d85a05f0 3690 struct die_info *comp_unit_die;
ae038cb0 3691 struct dwarf2_cu *cu;
1d9ec526 3692 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3693 int has_children;
3694 struct die_reader_specs reader_specs;
98bfdba5 3695 int read_cu = 0;
9ff913ba 3696 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3697
b0df02fd 3698 gdb_assert (! this_cu->debug_types_section);
348e048f 3699
9ff913ba 3700 gdb_assert (section->readin);
b64f50a1 3701 info_ptr = section->buffer + this_cu->offset.sect_off;
ae038cb0 3702
98bfdba5
PA
3703 if (this_cu->cu == NULL)
3704 {
9816fde3 3705 cu = xmalloc (sizeof (*cu));
23745b47 3706 init_one_comp_unit (cu, this_cu);
ae038cb0 3707
98bfdba5 3708 read_cu = 1;
ae038cb0 3709
98bfdba5 3710 /* If an error occurs while loading, release our storage. */
68dc6402 3711 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3712
9ff913ba
DE
3713 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3714 0);
ae038cb0 3715
6caca83c 3716 /* Skip dummy compilation units. */
9ff913ba 3717 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3718 || peek_abbrev_code (abfd, info_ptr) == 0)
3719 {
3720 do_cleanups (free_cu_cleanup);
3721 return;
3722 }
98bfdba5
PA
3723 }
3724 else
3725 {
3726 cu = this_cu->cu;
b64f50a1 3727 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 3728 }
ae038cb0
DJ
3729
3730 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3731 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3732 dwarf2_read_abbrevs (cu);
98bfdba5 3733 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3734
3735 /* Read the compilation unit die. */
d85a05f0
DJ
3736 init_cu_die_reader (&reader_specs, cu);
3737 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3738 &has_children);
ae038cb0 3739
9816fde3 3740 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3741
ae038cb0
DJ
3742 /* Check if comp unit has_children.
3743 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3744 If not, there's no more debug_info for this comp unit. */
d85a05f0 3745 if (has_children)
9ff913ba 3746 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3747
98bfdba5
PA
3748 do_cleanups (free_abbrevs_cleanup);
3749
3750 if (read_cu)
3751 {
3752 /* We've successfully allocated this compilation unit. Let our
3753 caller clean it up when finished with it. */
3754 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
3755
3756 /* Link this CU into read_in_chain. */
3757 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3758 dwarf2_per_objfile->read_in_chain = this_cu;
98bfdba5 3759 }
ae038cb0
DJ
3760}
3761
9cdd5dbd
DE
3762/* Create a list of all compilation units in OBJFILE.
3763 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3764
3765static void
3766create_all_comp_units (struct objfile *objfile)
3767{
3768 int n_allocated;
3769 int n_comp_units;
3770 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3771 gdb_byte *info_ptr;
3772
3773 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3774 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3775
3776 n_comp_units = 0;
3777 n_allocated = 10;
3778 all_comp_units = xmalloc (n_allocated
3779 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3780
3e43a32a
MS
3781 while (info_ptr < dwarf2_per_objfile->info.buffer
3782 + dwarf2_per_objfile->info.size)
ae038cb0 3783 {
c764a876 3784 unsigned int length, initial_length_size;
ae038cb0 3785 struct dwarf2_per_cu_data *this_cu;
b64f50a1 3786 sect_offset offset;
ae038cb0 3787
b64f50a1 3788 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3789
3790 /* Read just enough information to find out where the next
3791 compilation unit is. */
c764a876
DE
3792 length = read_initial_length (objfile->obfd, info_ptr,
3793 &initial_length_size);
ae038cb0
DJ
3794
3795 /* Save the compilation unit for later lookup. */
3796 this_cu = obstack_alloc (&objfile->objfile_obstack,
3797 sizeof (struct dwarf2_per_cu_data));
3798 memset (this_cu, 0, sizeof (*this_cu));
3799 this_cu->offset = offset;
c764a876 3800 this_cu->length = length + initial_length_size;
9291a0cd 3801 this_cu->objfile = objfile;
ae038cb0
DJ
3802
3803 if (n_comp_units == n_allocated)
3804 {
3805 n_allocated *= 2;
3806 all_comp_units = xrealloc (all_comp_units,
3807 n_allocated
3808 * sizeof (struct dwarf2_per_cu_data *));
3809 }
3810 all_comp_units[n_comp_units++] = this_cu;
3811
3812 info_ptr = info_ptr + this_cu->length;
3813 }
3814
3815 dwarf2_per_objfile->all_comp_units
3816 = obstack_alloc (&objfile->objfile_obstack,
3817 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3818 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3819 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3820 xfree (all_comp_units);
3821 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3822}
3823
5734ee8b
DJ
3824/* Process all loaded DIEs for compilation unit CU, starting at
3825 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3826 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3827 DW_AT_ranges). If NEED_PC is set, then this function will set
3828 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3829 and record the covered ranges in the addrmap. */
c906108c 3830
72bf9492
DJ
3831static void
3832scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3833 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3834{
72bf9492 3835 struct partial_die_info *pdi;
c906108c 3836
91c24f0a
DC
3837 /* Now, march along the PDI's, descending into ones which have
3838 interesting children but skipping the children of the other ones,
3839 until we reach the end of the compilation unit. */
c906108c 3840
72bf9492 3841 pdi = first_die;
91c24f0a 3842
72bf9492
DJ
3843 while (pdi != NULL)
3844 {
3845 fixup_partial_die (pdi, cu);
c906108c 3846
f55ee35c 3847 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3848 children, so we need to look at them. Ditto for anonymous
3849 enums. */
933c6fe4 3850
72bf9492 3851 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3852 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3853 {
72bf9492 3854 switch (pdi->tag)
c906108c
SS
3855 {
3856 case DW_TAG_subprogram:
5734ee8b 3857 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3858 break;
72929c62 3859 case DW_TAG_constant:
c906108c
SS
3860 case DW_TAG_variable:
3861 case DW_TAG_typedef:
91c24f0a 3862 case DW_TAG_union_type:
72bf9492 3863 if (!pdi->is_declaration)
63d06c5c 3864 {
72bf9492 3865 add_partial_symbol (pdi, cu);
63d06c5c
DC
3866 }
3867 break;
c906108c 3868 case DW_TAG_class_type:
680b30c7 3869 case DW_TAG_interface_type:
c906108c 3870 case DW_TAG_structure_type:
72bf9492 3871 if (!pdi->is_declaration)
c906108c 3872 {
72bf9492 3873 add_partial_symbol (pdi, cu);
c906108c
SS
3874 }
3875 break;
91c24f0a 3876 case DW_TAG_enumeration_type:
72bf9492
DJ
3877 if (!pdi->is_declaration)
3878 add_partial_enumeration (pdi, cu);
c906108c
SS
3879 break;
3880 case DW_TAG_base_type:
a02abb62 3881 case DW_TAG_subrange_type:
c906108c 3882 /* File scope base type definitions are added to the partial
c5aa993b 3883 symbol table. */
72bf9492 3884 add_partial_symbol (pdi, cu);
c906108c 3885 break;
d9fa45fe 3886 case DW_TAG_namespace:
5734ee8b 3887 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3888 break;
5d7cb8df
JK
3889 case DW_TAG_module:
3890 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3891 break;
c906108c
SS
3892 default:
3893 break;
3894 }
3895 }
3896
72bf9492
DJ
3897 /* If the die has a sibling, skip to the sibling. */
3898
3899 pdi = pdi->die_sibling;
3900 }
3901}
3902
3903/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3904
72bf9492 3905 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3906 name is concatenated with "::" and the partial DIE's name. For
3907 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3908 Enumerators are an exception; they use the scope of their parent
3909 enumeration type, i.e. the name of the enumeration type is not
3910 prepended to the enumerator.
91c24f0a 3911
72bf9492
DJ
3912 There are two complexities. One is DW_AT_specification; in this
3913 case "parent" means the parent of the target of the specification,
3914 instead of the direct parent of the DIE. The other is compilers
3915 which do not emit DW_TAG_namespace; in this case we try to guess
3916 the fully qualified name of structure types from their members'
3917 linkage names. This must be done using the DIE's children rather
3918 than the children of any DW_AT_specification target. We only need
3919 to do this for structures at the top level, i.e. if the target of
3920 any DW_AT_specification (if any; otherwise the DIE itself) does not
3921 have a parent. */
3922
3923/* Compute the scope prefix associated with PDI's parent, in
3924 compilation unit CU. The result will be allocated on CU's
3925 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3926 field. NULL is returned if no prefix is necessary. */
3927static char *
3928partial_die_parent_scope (struct partial_die_info *pdi,
3929 struct dwarf2_cu *cu)
3930{
3931 char *grandparent_scope;
3932 struct partial_die_info *parent, *real_pdi;
91c24f0a 3933
72bf9492
DJ
3934 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3935 then this means the parent of the specification DIE. */
3936
3937 real_pdi = pdi;
72bf9492 3938 while (real_pdi->has_specification)
10b3939b 3939 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3940
3941 parent = real_pdi->die_parent;
3942 if (parent == NULL)
3943 return NULL;
3944
3945 if (parent->scope_set)
3946 return parent->scope;
3947
3948 fixup_partial_die (parent, cu);
3949
10b3939b 3950 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3951
acebe513
UW
3952 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3953 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3954 Work around this problem here. */
3955 if (cu->language == language_cplus
6e70227d 3956 && parent->tag == DW_TAG_namespace
acebe513
UW
3957 && strcmp (parent->name, "::") == 0
3958 && grandparent_scope == NULL)
3959 {
3960 parent->scope = NULL;
3961 parent->scope_set = 1;
3962 return NULL;
3963 }
3964
9c6c53f7
SA
3965 if (pdi->tag == DW_TAG_enumerator)
3966 /* Enumerators should not get the name of the enumeration as a prefix. */
3967 parent->scope = grandparent_scope;
3968 else if (parent->tag == DW_TAG_namespace
f55ee35c 3969 || parent->tag == DW_TAG_module
72bf9492
DJ
3970 || parent->tag == DW_TAG_structure_type
3971 || parent->tag == DW_TAG_class_type
680b30c7 3972 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3973 || parent->tag == DW_TAG_union_type
3974 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3975 {
3976 if (grandparent_scope == NULL)
3977 parent->scope = parent->name;
3978 else
3e43a32a
MS
3979 parent->scope = typename_concat (&cu->comp_unit_obstack,
3980 grandparent_scope,
f55ee35c 3981 parent->name, 0, cu);
72bf9492 3982 }
72bf9492
DJ
3983 else
3984 {
3985 /* FIXME drow/2004-04-01: What should we be doing with
3986 function-local names? For partial symbols, we should probably be
3987 ignoring them. */
3988 complaint (&symfile_complaints,
e2e0b3e5 3989 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 3990 parent->tag, pdi->offset.sect_off);
72bf9492 3991 parent->scope = grandparent_scope;
c906108c
SS
3992 }
3993
72bf9492
DJ
3994 parent->scope_set = 1;
3995 return parent->scope;
3996}
3997
3998/* Return the fully scoped name associated with PDI, from compilation unit
3999 CU. The result will be allocated with malloc. */
4568ecf9 4000
72bf9492
DJ
4001static char *
4002partial_die_full_name (struct partial_die_info *pdi,
4003 struct dwarf2_cu *cu)
4004{
4005 char *parent_scope;
4006
98bfdba5
PA
4007 /* If this is a template instantiation, we can not work out the
4008 template arguments from partial DIEs. So, unfortunately, we have
4009 to go through the full DIEs. At least any work we do building
4010 types here will be reused if full symbols are loaded later. */
4011 if (pdi->has_template_arguments)
4012 {
4013 fixup_partial_die (pdi, cu);
4014
4015 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4016 {
4017 struct die_info *die;
4018 struct attribute attr;
4019 struct dwarf2_cu *ref_cu = cu;
4020
b64f50a1 4021 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4022 attr.name = 0;
4023 attr.form = DW_FORM_ref_addr;
4568ecf9 4024 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4025 die = follow_die_ref (NULL, &attr, &ref_cu);
4026
4027 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4028 }
4029 }
4030
72bf9492
DJ
4031 parent_scope = partial_die_parent_scope (pdi, cu);
4032 if (parent_scope == NULL)
4033 return NULL;
4034 else
f55ee35c 4035 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4036}
4037
4038static void
72bf9492 4039add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4040{
e7c27a73 4041 struct objfile *objfile = cu->objfile;
c906108c 4042 CORE_ADDR addr = 0;
decbce07 4043 char *actual_name = NULL;
e142c38c 4044 CORE_ADDR baseaddr;
72bf9492 4045 int built_actual_name = 0;
e142c38c
DJ
4046
4047 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4048
94af9270
KS
4049 actual_name = partial_die_full_name (pdi, cu);
4050 if (actual_name)
4051 built_actual_name = 1;
63d06c5c 4052
72bf9492
DJ
4053 if (actual_name == NULL)
4054 actual_name = pdi->name;
4055
c906108c
SS
4056 switch (pdi->tag)
4057 {
4058 case DW_TAG_subprogram:
2cfa0c8d 4059 if (pdi->is_external || cu->language == language_ada)
c906108c 4060 {
2cfa0c8d
JB
4061 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4062 of the global scope. But in Ada, we want to be able to access
4063 nested procedures globally. So all Ada subprograms are stored
4064 in the global scope. */
f47fb265 4065 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4066 mst_text, objfile); */
f47fb265
MS
4067 add_psymbol_to_list (actual_name, strlen (actual_name),
4068 built_actual_name,
4069 VAR_DOMAIN, LOC_BLOCK,
4070 &objfile->global_psymbols,
4071 0, pdi->lowpc + baseaddr,
4072 cu->language, objfile);
c906108c
SS
4073 }
4074 else
4075 {
f47fb265 4076 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4077 mst_file_text, objfile); */
f47fb265
MS
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
4079 built_actual_name,
4080 VAR_DOMAIN, LOC_BLOCK,
4081 &objfile->static_psymbols,
4082 0, pdi->lowpc + baseaddr,
4083 cu->language, objfile);
c906108c
SS
4084 }
4085 break;
72929c62
JB
4086 case DW_TAG_constant:
4087 {
4088 struct psymbol_allocation_list *list;
4089
4090 if (pdi->is_external)
4091 list = &objfile->global_psymbols;
4092 else
4093 list = &objfile->static_psymbols;
f47fb265
MS
4094 add_psymbol_to_list (actual_name, strlen (actual_name),
4095 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4096 list, 0, 0, cu->language, objfile);
72929c62
JB
4097 }
4098 break;
c906108c 4099 case DW_TAG_variable:
caac4577
JG
4100 if (pdi->locdesc)
4101 addr = decode_locdesc (pdi->locdesc, cu);
4102
4103 if (pdi->locdesc
4104 && addr == 0
4105 && !dwarf2_per_objfile->has_section_at_zero)
4106 {
4107 /* A global or static variable may also have been stripped
4108 out by the linker if unused, in which case its address
4109 will be nullified; do not add such variables into partial
4110 symbol table then. */
4111 }
4112 else if (pdi->is_external)
c906108c
SS
4113 {
4114 /* Global Variable.
4115 Don't enter into the minimal symbol tables as there is
4116 a minimal symbol table entry from the ELF symbols already.
4117 Enter into partial symbol table if it has a location
4118 descriptor or a type.
4119 If the location descriptor is missing, new_symbol will create
4120 a LOC_UNRESOLVED symbol, the address of the variable will then
4121 be determined from the minimal symbol table whenever the variable
4122 is referenced.
4123 The address for the partial symbol table entry is not
4124 used by GDB, but it comes in handy for debugging partial symbol
4125 table building. */
4126
c906108c 4127 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4128 add_psymbol_to_list (actual_name, strlen (actual_name),
4129 built_actual_name,
4130 VAR_DOMAIN, LOC_STATIC,
4131 &objfile->global_psymbols,
4132 0, addr + baseaddr,
4133 cu->language, objfile);
c906108c
SS
4134 }
4135 else
4136 {
0963b4bd 4137 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4138 if (pdi->locdesc == NULL)
decbce07
MS
4139 {
4140 if (built_actual_name)
4141 xfree (actual_name);
4142 return;
4143 }
f47fb265 4144 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4145 mst_file_data, objfile); */
f47fb265
MS
4146 add_psymbol_to_list (actual_name, strlen (actual_name),
4147 built_actual_name,
4148 VAR_DOMAIN, LOC_STATIC,
4149 &objfile->static_psymbols,
4150 0, addr + baseaddr,
4151 cu->language, objfile);
c906108c
SS
4152 }
4153 break;
4154 case DW_TAG_typedef:
4155 case DW_TAG_base_type:
a02abb62 4156 case DW_TAG_subrange_type:
38d518c9 4157 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4158 built_actual_name,
176620f1 4159 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4160 &objfile->static_psymbols,
e142c38c 4161 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4162 break;
72bf9492
DJ
4163 case DW_TAG_namespace:
4164 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4165 built_actual_name,
72bf9492
DJ
4166 VAR_DOMAIN, LOC_TYPEDEF,
4167 &objfile->global_psymbols,
4168 0, (CORE_ADDR) 0, cu->language, objfile);
4169 break;
c906108c 4170 case DW_TAG_class_type:
680b30c7 4171 case DW_TAG_interface_type:
c906108c
SS
4172 case DW_TAG_structure_type:
4173 case DW_TAG_union_type:
4174 case DW_TAG_enumeration_type:
fa4028e9
JB
4175 /* Skip external references. The DWARF standard says in the section
4176 about "Structure, Union, and Class Type Entries": "An incomplete
4177 structure, union or class type is represented by a structure,
4178 union or class entry that does not have a byte size attribute
4179 and that has a DW_AT_declaration attribute." */
4180 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4181 {
4182 if (built_actual_name)
4183 xfree (actual_name);
4184 return;
4185 }
fa4028e9 4186
63d06c5c
DC
4187 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4188 static vs. global. */
38d518c9 4189 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4190 built_actual_name,
176620f1 4191 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4192 (cu->language == language_cplus
4193 || cu->language == language_java)
63d06c5c
DC
4194 ? &objfile->global_psymbols
4195 : &objfile->static_psymbols,
e142c38c 4196 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4197
c906108c
SS
4198 break;
4199 case DW_TAG_enumerator:
38d518c9 4200 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4201 built_actual_name,
176620f1 4202 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4203 (cu->language == language_cplus
4204 || cu->language == language_java)
f6fe98ef
DJ
4205 ? &objfile->global_psymbols
4206 : &objfile->static_psymbols,
e142c38c 4207 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4208 break;
4209 default:
4210 break;
4211 }
5c4e30ca 4212
72bf9492
DJ
4213 if (built_actual_name)
4214 xfree (actual_name);
c906108c
SS
4215}
4216
5c4e30ca
DC
4217/* Read a partial die corresponding to a namespace; also, add a symbol
4218 corresponding to that namespace to the symbol table. NAMESPACE is
4219 the name of the enclosing namespace. */
91c24f0a 4220
72bf9492
DJ
4221static void
4222add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4223 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4224 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4225{
72bf9492 4226 /* Add a symbol for the namespace. */
e7c27a73 4227
72bf9492 4228 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4229
4230 /* Now scan partial symbols in that namespace. */
4231
91c24f0a 4232 if (pdi->has_children)
5734ee8b 4233 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4234}
4235
5d7cb8df
JK
4236/* Read a partial die corresponding to a Fortran module. */
4237
4238static void
4239add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4240 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4241{
f55ee35c 4242 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4243
4244 if (pdi->has_children)
4245 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4246}
4247
bc30ff58
JB
4248/* Read a partial die corresponding to a subprogram and create a partial
4249 symbol for that subprogram. When the CU language allows it, this
4250 routine also defines a partial symbol for each nested subprogram
4251 that this subprogram contains.
6e70227d 4252
bc30ff58
JB
4253 DIE my also be a lexical block, in which case we simply search
4254 recursively for suprograms defined inside that lexical block.
4255 Again, this is only performed when the CU language allows this
4256 type of definitions. */
4257
4258static void
4259add_partial_subprogram (struct partial_die_info *pdi,
4260 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4261 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4262{
4263 if (pdi->tag == DW_TAG_subprogram)
4264 {
4265 if (pdi->has_pc_info)
4266 {
4267 if (pdi->lowpc < *lowpc)
4268 *lowpc = pdi->lowpc;
4269 if (pdi->highpc > *highpc)
4270 *highpc = pdi->highpc;
5734ee8b
DJ
4271 if (need_pc)
4272 {
4273 CORE_ADDR baseaddr;
4274 struct objfile *objfile = cu->objfile;
4275
4276 baseaddr = ANOFFSET (objfile->section_offsets,
4277 SECT_OFF_TEXT (objfile));
4278 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4279 pdi->lowpc + baseaddr,
4280 pdi->highpc - 1 + baseaddr,
9291a0cd 4281 cu->per_cu->v.psymtab);
5734ee8b 4282 }
481860b3
GB
4283 }
4284
4285 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4286 {
bc30ff58 4287 if (!pdi->is_declaration)
e8d05480
JB
4288 /* Ignore subprogram DIEs that do not have a name, they are
4289 illegal. Do not emit a complaint at this point, we will
4290 do so when we convert this psymtab into a symtab. */
4291 if (pdi->name)
4292 add_partial_symbol (pdi, cu);
bc30ff58
JB
4293 }
4294 }
6e70227d 4295
bc30ff58
JB
4296 if (! pdi->has_children)
4297 return;
4298
4299 if (cu->language == language_ada)
4300 {
4301 pdi = pdi->die_child;
4302 while (pdi != NULL)
4303 {
4304 fixup_partial_die (pdi, cu);
4305 if (pdi->tag == DW_TAG_subprogram
4306 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4307 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4308 pdi = pdi->die_sibling;
4309 }
4310 }
4311}
4312
91c24f0a
DC
4313/* Read a partial die corresponding to an enumeration type. */
4314
72bf9492
DJ
4315static void
4316add_partial_enumeration (struct partial_die_info *enum_pdi,
4317 struct dwarf2_cu *cu)
91c24f0a 4318{
72bf9492 4319 struct partial_die_info *pdi;
91c24f0a
DC
4320
4321 if (enum_pdi->name != NULL)
72bf9492
DJ
4322 add_partial_symbol (enum_pdi, cu);
4323
4324 pdi = enum_pdi->die_child;
4325 while (pdi)
91c24f0a 4326 {
72bf9492 4327 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4328 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4329 else
72bf9492
DJ
4330 add_partial_symbol (pdi, cu);
4331 pdi = pdi->die_sibling;
91c24f0a 4332 }
91c24f0a
DC
4333}
4334
6caca83c
CC
4335/* Return the initial uleb128 in the die at INFO_PTR. */
4336
4337static unsigned int
4338peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4339{
4340 unsigned int bytes_read;
4341
4342 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4343}
4344
4bb7a0a7
DJ
4345/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4346 Return the corresponding abbrev, or NULL if the number is zero (indicating
4347 an empty DIE). In either case *BYTES_READ will be set to the length of
4348 the initial number. */
4349
4350static struct abbrev_info *
fe1b8b76 4351peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4352 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4353{
4354 bfd *abfd = cu->objfile->obfd;
4355 unsigned int abbrev_number;
4356 struct abbrev_info *abbrev;
4357
4358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4359
4360 if (abbrev_number == 0)
4361 return NULL;
4362
4363 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4364 if (!abbrev)
4365 {
3e43a32a
MS
4366 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4367 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4368 }
4369
4370 return abbrev;
4371}
4372
93311388
DE
4373/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4374 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4375 DIE. Any children of the skipped DIEs will also be skipped. */
4376
fe1b8b76 4377static gdb_byte *
93311388 4378skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4379{
4380 struct abbrev_info *abbrev;
4381 unsigned int bytes_read;
4382
4383 while (1)
4384 {
4385 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4386 if (abbrev == NULL)
4387 return info_ptr + bytes_read;
4388 else
93311388 4389 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4390 }
4391}
4392
93311388
DE
4393/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4394 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4395 abbrev corresponding to that skipped uleb128 should be passed in
4396 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4397 children. */
4398
fe1b8b76 4399static gdb_byte *
93311388
DE
4400skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4401 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4402{
4403 unsigned int bytes_read;
4404 struct attribute attr;
4405 bfd *abfd = cu->objfile->obfd;
4406 unsigned int form, i;
4407
4408 for (i = 0; i < abbrev->num_attrs; i++)
4409 {
4410 /* The only abbrev we care about is DW_AT_sibling. */
4411 if (abbrev->attrs[i].name == DW_AT_sibling)
4412 {
4413 read_attribute (&attr, &abbrev->attrs[i],
4414 abfd, info_ptr, cu);
4415 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4416 complaint (&symfile_complaints,
4417 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4418 else
b64f50a1 4419 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
4420 }
4421
4422 /* If it isn't DW_AT_sibling, skip this attribute. */
4423 form = abbrev->attrs[i].form;
4424 skip_attribute:
4425 switch (form)
4426 {
4bb7a0a7 4427 case DW_FORM_ref_addr:
ae411497
TT
4428 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4429 and later it is offset sized. */
4430 if (cu->header.version == 2)
4431 info_ptr += cu->header.addr_size;
4432 else
4433 info_ptr += cu->header.offset_size;
4434 break;
4435 case DW_FORM_addr:
4bb7a0a7
DJ
4436 info_ptr += cu->header.addr_size;
4437 break;
4438 case DW_FORM_data1:
4439 case DW_FORM_ref1:
4440 case DW_FORM_flag:
4441 info_ptr += 1;
4442 break;
2dc7f7b3
TT
4443 case DW_FORM_flag_present:
4444 break;
4bb7a0a7
DJ
4445 case DW_FORM_data2:
4446 case DW_FORM_ref2:
4447 info_ptr += 2;
4448 break;
4449 case DW_FORM_data4:
4450 case DW_FORM_ref4:
4451 info_ptr += 4;
4452 break;
4453 case DW_FORM_data8:
4454 case DW_FORM_ref8:
55f1336d 4455 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4456 info_ptr += 8;
4457 break;
4458 case DW_FORM_string:
9b1c24c8 4459 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4460 info_ptr += bytes_read;
4461 break;
2dc7f7b3 4462 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4463 case DW_FORM_strp:
4464 info_ptr += cu->header.offset_size;
4465 break;
2dc7f7b3 4466 case DW_FORM_exprloc:
4bb7a0a7
DJ
4467 case DW_FORM_block:
4468 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4469 info_ptr += bytes_read;
4470 break;
4471 case DW_FORM_block1:
4472 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4473 break;
4474 case DW_FORM_block2:
4475 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4476 break;
4477 case DW_FORM_block4:
4478 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4479 break;
4480 case DW_FORM_sdata:
4481 case DW_FORM_udata:
4482 case DW_FORM_ref_udata:
4483 info_ptr = skip_leb128 (abfd, info_ptr);
4484 break;
4485 case DW_FORM_indirect:
4486 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4487 info_ptr += bytes_read;
4488 /* We need to continue parsing from here, so just go back to
4489 the top. */
4490 goto skip_attribute;
4491
4492 default:
3e43a32a
MS
4493 error (_("Dwarf Error: Cannot handle %s "
4494 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4495 dwarf_form_name (form),
4496 bfd_get_filename (abfd));
4497 }
4498 }
4499
4500 if (abbrev->has_children)
93311388 4501 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4502 else
4503 return info_ptr;
4504}
4505
93311388
DE
4506/* Locate ORIG_PDI's sibling.
4507 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4508 in BUFFER. */
91c24f0a 4509
fe1b8b76 4510static gdb_byte *
93311388
DE
4511locate_pdi_sibling (struct partial_die_info *orig_pdi,
4512 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4513 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4514{
4515 /* Do we know the sibling already? */
72bf9492 4516
91c24f0a
DC
4517 if (orig_pdi->sibling)
4518 return orig_pdi->sibling;
4519
4520 /* Are there any children to deal with? */
4521
4522 if (!orig_pdi->has_children)
4523 return info_ptr;
4524
4bb7a0a7 4525 /* Skip the children the long way. */
91c24f0a 4526
93311388 4527 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4528}
4529
c906108c
SS
4530/* Expand this partial symbol table into a full symbol table. */
4531
4532static void
fba45db2 4533dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4534{
c906108c
SS
4535 if (pst != NULL)
4536 {
4537 if (pst->readin)
4538 {
3e43a32a
MS
4539 warning (_("bug: psymtab for %s is already read in."),
4540 pst->filename);
c906108c
SS
4541 }
4542 else
4543 {
4544 if (info_verbose)
4545 {
3e43a32a
MS
4546 printf_filtered (_("Reading in symbols for %s..."),
4547 pst->filename);
c906108c
SS
4548 gdb_flush (gdb_stdout);
4549 }
4550
10b3939b
DJ
4551 /* Restore our global data. */
4552 dwarf2_per_objfile = objfile_data (pst->objfile,
4553 dwarf2_objfile_data_key);
4554
b2ab525c
KB
4555 /* If this psymtab is constructed from a debug-only objfile, the
4556 has_section_at_zero flag will not necessarily be correct. We
4557 can get the correct value for this flag by looking at the data
4558 associated with the (presumably stripped) associated objfile. */
4559 if (pst->objfile->separate_debug_objfile_backlink)
4560 {
4561 struct dwarf2_per_objfile *dpo_backlink
4562 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4563 dwarf2_objfile_data_key);
9a619af0 4564
b2ab525c
KB
4565 dwarf2_per_objfile->has_section_at_zero
4566 = dpo_backlink->has_section_at_zero;
4567 }
4568
98bfdba5
PA
4569 dwarf2_per_objfile->reading_partial_symbols = 0;
4570
c906108c
SS
4571 psymtab_to_symtab_1 (pst);
4572
4573 /* Finish up the debug error message. */
4574 if (info_verbose)
a3f17187 4575 printf_filtered (_("done.\n"));
c906108c
SS
4576 }
4577 }
4578}
9cdd5dbd
DE
4579\f
4580/* Reading in full CUs. */
c906108c 4581
10b3939b
DJ
4582/* Add PER_CU to the queue. */
4583
4584static void
a0f42c21 4585queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4586{
4587 struct dwarf2_queue_item *item;
4588
4589 per_cu->queued = 1;
4590 item = xmalloc (sizeof (*item));
4591 item->per_cu = per_cu;
4592 item->next = NULL;
4593
4594 if (dwarf2_queue == NULL)
4595 dwarf2_queue = item;
4596 else
4597 dwarf2_queue_tail->next = item;
4598
4599 dwarf2_queue_tail = item;
4600}
4601
4602/* Process the queue. */
4603
4604static void
a0f42c21 4605process_queue (void)
10b3939b
DJ
4606{
4607 struct dwarf2_queue_item *item, *next_item;
4608
03dd20cc
DJ
4609 /* The queue starts out with one item, but following a DIE reference
4610 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4611 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4612 {
9291a0cd
TT
4613 if (dwarf2_per_objfile->using_index
4614 ? !item->per_cu->v.quick->symtab
4615 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4616 process_full_comp_unit (item->per_cu);
4617
4618 item->per_cu->queued = 0;
4619 next_item = item->next;
4620 xfree (item);
4621 }
4622
4623 dwarf2_queue_tail = NULL;
4624}
4625
4626/* Free all allocated queue entries. This function only releases anything if
4627 an error was thrown; if the queue was processed then it would have been
4628 freed as we went along. */
4629
4630static void
4631dwarf2_release_queue (void *dummy)
4632{
4633 struct dwarf2_queue_item *item, *last;
4634
4635 item = dwarf2_queue;
4636 while (item)
4637 {
4638 /* Anything still marked queued is likely to be in an
4639 inconsistent state, so discard it. */
4640 if (item->per_cu->queued)
4641 {
4642 if (item->per_cu->cu != NULL)
4643 free_one_cached_comp_unit (item->per_cu->cu);
4644 item->per_cu->queued = 0;
4645 }
4646
4647 last = item;
4648 item = item->next;
4649 xfree (last);
4650 }
4651
4652 dwarf2_queue = dwarf2_queue_tail = NULL;
4653}
4654
4655/* Read in full symbols for PST, and anything it depends on. */
4656
c906108c 4657static void
fba45db2 4658psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4659{
10b3939b 4660 struct dwarf2_per_cu_data *per_cu;
c906108c 4661 struct cleanup *back_to;
aaa75496
JB
4662 int i;
4663
4664 for (i = 0; i < pst->number_of_dependencies; i++)
4665 if (!pst->dependencies[i]->readin)
4666 {
4667 /* Inform about additional files that need to be read in. */
4668 if (info_verbose)
4669 {
a3f17187 4670 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4671 fputs_filtered (" ", gdb_stdout);
4672 wrap_here ("");
4673 fputs_filtered ("and ", gdb_stdout);
4674 wrap_here ("");
4675 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4676 wrap_here (""); /* Flush output. */
aaa75496
JB
4677 gdb_flush (gdb_stdout);
4678 }
4679 psymtab_to_symtab_1 (pst->dependencies[i]);
4680 }
4681
e38df1d0 4682 per_cu = pst->read_symtab_private;
10b3939b
DJ
4683
4684 if (per_cu == NULL)
aaa75496
JB
4685 {
4686 /* It's an include file, no symbols to read for it.
4687 Everything is in the parent symtab. */
4688 pst->readin = 1;
4689 return;
4690 }
c906108c 4691
a0f42c21 4692 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4693}
4694
93311388 4695/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4696
93311388 4697static void
a0f42c21 4698load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4699{
a0f42c21 4700 struct objfile *objfile = per_cu->objfile;
31ffec48 4701 bfd *abfd = objfile->obfd;
10b3939b 4702 struct dwarf2_cu *cu;
b64f50a1 4703 sect_offset offset;
93311388 4704 gdb_byte *info_ptr, *beg_of_comp_unit;
7fb3ad1f 4705 struct cleanup *free_cu_cleanup = NULL;
10b3939b 4706 struct attribute *attr;
98bfdba5 4707 int read_cu = 0;
6502dd73 4708
b0df02fd 4709 gdb_assert (! per_cu->debug_types_section);
348e048f 4710
c906108c 4711 /* Set local variables from the partial symbol table info. */
10b3939b 4712 offset = per_cu->offset;
6502dd73 4713
be391dca 4714 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
b64f50a1 4715 info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
93311388 4716 beg_of_comp_unit = info_ptr;
63d06c5c 4717
98bfdba5
PA
4718 if (per_cu->cu == NULL)
4719 {
9816fde3 4720 cu = xmalloc (sizeof (*cu));
23745b47 4721 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4722
4723 read_cu = 1;
c906108c 4724
98bfdba5 4725 /* If an error occurs while loading, release our storage. */
68dc6402 4726 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4727
98bfdba5
PA
4728 /* Read in the comp_unit header. */
4729 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4730
6caca83c
CC
4731 /* Skip dummy compilation units. */
4732 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4733 + dwarf2_per_objfile->info.size)
4734 || peek_abbrev_code (abfd, info_ptr) == 0)
4735 {
4736 do_cleanups (free_cu_cleanup);
4737 return;
4738 }
4739
98bfdba5
PA
4740 /* Complete the cu_header. */
4741 cu->header.offset = offset;
b64f50a1 4742 cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
98bfdba5
PA
4743 }
4744 else
4745 {
4746 cu = per_cu->cu;
b64f50a1 4747 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 4748 }
e142c38c 4749
93311388 4750 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4751
4752 /* We try not to read any attributes in this function, because not
9cdd5dbd 4753 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4754 table processing isn't initialized. But we have to set the CU language,
4755 or we won't be able to build types correctly. */
9816fde3 4756 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4757
a6c727b2
DJ
4758 /* Similarly, if we do not read the producer, we can not apply
4759 producer-specific interpretation. */
4760 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4761 if (attr)
4762 cu->producer = DW_STRING (attr);
4763
98bfdba5
PA
4764 if (read_cu)
4765 {
98bfdba5
PA
4766 /* We've successfully allocated this compilation unit. Let our
4767 caller clean it up when finished with it. */
4768 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
4769
4770 /* Link this CU into read_in_chain. */
4771 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4772 dwarf2_per_objfile->read_in_chain = per_cu;
98bfdba5 4773 }
10b3939b
DJ
4774}
4775
3da10d80
KS
4776/* Add a DIE to the delayed physname list. */
4777
4778static void
4779add_to_method_list (struct type *type, int fnfield_index, int index,
4780 const char *name, struct die_info *die,
4781 struct dwarf2_cu *cu)
4782{
4783 struct delayed_method_info mi;
4784 mi.type = type;
4785 mi.fnfield_index = fnfield_index;
4786 mi.index = index;
4787 mi.name = name;
4788 mi.die = die;
4789 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4790}
4791
4792/* A cleanup for freeing the delayed method list. */
4793
4794static void
4795free_delayed_list (void *ptr)
4796{
4797 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4798 if (cu->method_list != NULL)
4799 {
4800 VEC_free (delayed_method_info, cu->method_list);
4801 cu->method_list = NULL;
4802 }
4803}
4804
4805/* Compute the physnames of any methods on the CU's method list.
4806
4807 The computation of method physnames is delayed in order to avoid the
4808 (bad) condition that one of the method's formal parameters is of an as yet
4809 incomplete type. */
4810
4811static void
4812compute_delayed_physnames (struct dwarf2_cu *cu)
4813{
4814 int i;
4815 struct delayed_method_info *mi;
4816 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4817 {
1d06ead6 4818 const char *physname;
3da10d80
KS
4819 struct fn_fieldlist *fn_flp
4820 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4821 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4822 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4823 }
4824}
4825
a766d390
DE
4826/* Go objects should be embedded in a DW_TAG_module DIE,
4827 and it's not clear if/how imported objects will appear.
4828 To keep Go support simple until that's worked out,
4829 go back through what we've read and create something usable.
4830 We could do this while processing each DIE, and feels kinda cleaner,
4831 but that way is more invasive.
4832 This is to, for example, allow the user to type "p var" or "b main"
4833 without having to specify the package name, and allow lookups
4834 of module.object to work in contexts that use the expression
4835 parser. */
4836
4837static void
4838fixup_go_packaging (struct dwarf2_cu *cu)
4839{
4840 char *package_name = NULL;
4841 struct pending *list;
4842 int i;
4843
4844 for (list = global_symbols; list != NULL; list = list->next)
4845 {
4846 for (i = 0; i < list->nsyms; ++i)
4847 {
4848 struct symbol *sym = list->symbol[i];
4849
4850 if (SYMBOL_LANGUAGE (sym) == language_go
4851 && SYMBOL_CLASS (sym) == LOC_BLOCK)
4852 {
4853 char *this_package_name = go_symbol_package_name (sym);
4854
4855 if (this_package_name == NULL)
4856 continue;
4857 if (package_name == NULL)
4858 package_name = this_package_name;
4859 else
4860 {
4861 if (strcmp (package_name, this_package_name) != 0)
4862 complaint (&symfile_complaints,
4863 _("Symtab %s has objects from two different Go packages: %s and %s"),
4864 (sym->symtab && sym->symtab->filename
4865 ? sym->symtab->filename
4866 : cu->objfile->name),
4867 this_package_name, package_name);
4868 xfree (this_package_name);
4869 }
4870 }
4871 }
4872 }
4873
4874 if (package_name != NULL)
4875 {
4876 struct objfile *objfile = cu->objfile;
4877 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
4878 package_name, objfile);
4879 struct symbol *sym;
4880
4881 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4882
4883 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
4884 SYMBOL_SET_LANGUAGE (sym, language_go);
4885 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
4886 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
4887 e.g., "main" finds the "main" module and not C's main(). */
4888 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
4889 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4890 SYMBOL_TYPE (sym) = type;
4891
4892 add_symbol_to_list (sym, &global_symbols);
4893
4894 xfree (package_name);
4895 }
4896}
4897
9cdd5dbd 4898/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4899 already been loaded into memory. */
4900
4901static void
4902process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4903{
10b3939b 4904 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4905 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4906 CORE_ADDR lowpc, highpc;
4907 struct symtab *symtab;
3da10d80 4908 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4909 CORE_ADDR baseaddr;
4910
4911 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4912
10b3939b
DJ
4913 buildsym_init ();
4914 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4915 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4916
4917 cu->list_in_scope = &file_symbols;
c906108c
SS
4918
4919 /* Do line number decoding in read_file_scope () */
10b3939b 4920 process_die (cu->dies, cu);
c906108c 4921
a766d390
DE
4922 /* For now fudge the Go package. */
4923 if (cu->language == language_go)
4924 fixup_go_packaging (cu);
4925
3da10d80
KS
4926 /* Now that we have processed all the DIEs in the CU, all the types
4927 should be complete, and it should now be safe to compute all of the
4928 physnames. */
4929 compute_delayed_physnames (cu);
4930 do_cleanups (delayed_list_cleanup);
4931
fae299cd
DC
4932 /* Some compilers don't define a DW_AT_high_pc attribute for the
4933 compilation unit. If the DW_AT_high_pc is missing, synthesize
4934 it, by scanning the DIE's below the compilation unit. */
10b3939b 4935 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4936
613e1657 4937 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4938
8be455d7 4939 if (symtab != NULL)
c906108c 4940 {
df15bd07 4941 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4942
8be455d7
JK
4943 /* Set symtab language to language from DW_AT_language. If the
4944 compilation is from a C file generated by language preprocessors, do
4945 not set the language if it was already deduced by start_subfile. */
4946 if (!(cu->language == language_c && symtab->language != language_c))
4947 symtab->language = cu->language;
4948
4949 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4950 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
4951 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
4952 there were bugs in prologue debug info, fixed later in GCC-4.5
4953 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
4954
4955 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4956 needed, it would be wrong due to missing DW_AT_producer there.
4957
4958 Still one can confuse GDB by using non-standard GCC compilation
4959 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4960 */
ab260dad 4961 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 4962 symtab->locations_valid = 1;
e0d00bc7
JK
4963
4964 if (gcc_4_minor >= 5)
4965 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4966
4967 symtab->call_site_htab = cu->call_site_htab;
c906108c 4968 }
9291a0cd
TT
4969
4970 if (dwarf2_per_objfile->using_index)
4971 per_cu->v.quick->symtab = symtab;
4972 else
4973 {
4974 struct partial_symtab *pst = per_cu->v.psymtab;
4975 pst->symtab = symtab;
4976 pst->readin = 1;
4977 }
c906108c
SS
4978
4979 do_cleanups (back_to);
4980}
4981
4982/* Process a die and its children. */
4983
4984static void
e7c27a73 4985process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4986{
4987 switch (die->tag)
4988 {
4989 case DW_TAG_padding:
4990 break;
4991 case DW_TAG_compile_unit:
e7c27a73 4992 read_file_scope (die, cu);
c906108c 4993 break;
348e048f
DE
4994 case DW_TAG_type_unit:
4995 read_type_unit_scope (die, cu);
4996 break;
c906108c 4997 case DW_TAG_subprogram:
c906108c 4998 case DW_TAG_inlined_subroutine:
edb3359d 4999 read_func_scope (die, cu);
c906108c
SS
5000 break;
5001 case DW_TAG_lexical_block:
14898363
L
5002 case DW_TAG_try_block:
5003 case DW_TAG_catch_block:
e7c27a73 5004 read_lexical_block_scope (die, cu);
c906108c 5005 break;
96408a79
SA
5006 case DW_TAG_GNU_call_site:
5007 read_call_site_scope (die, cu);
5008 break;
c906108c 5009 case DW_TAG_class_type:
680b30c7 5010 case DW_TAG_interface_type:
c906108c
SS
5011 case DW_TAG_structure_type:
5012 case DW_TAG_union_type:
134d01f1 5013 process_structure_scope (die, cu);
c906108c
SS
5014 break;
5015 case DW_TAG_enumeration_type:
134d01f1 5016 process_enumeration_scope (die, cu);
c906108c 5017 break;
134d01f1 5018
f792889a
DJ
5019 /* These dies have a type, but processing them does not create
5020 a symbol or recurse to process the children. Therefore we can
5021 read them on-demand through read_type_die. */
c906108c 5022 case DW_TAG_subroutine_type:
72019c9c 5023 case DW_TAG_set_type:
c906108c 5024 case DW_TAG_array_type:
c906108c 5025 case DW_TAG_pointer_type:
c906108c 5026 case DW_TAG_ptr_to_member_type:
c906108c 5027 case DW_TAG_reference_type:
c906108c 5028 case DW_TAG_string_type:
c906108c 5029 break;
134d01f1 5030
c906108c 5031 case DW_TAG_base_type:
a02abb62 5032 case DW_TAG_subrange_type:
cb249c71 5033 case DW_TAG_typedef:
134d01f1
DJ
5034 /* Add a typedef symbol for the type definition, if it has a
5035 DW_AT_name. */
f792889a 5036 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5037 break;
c906108c 5038 case DW_TAG_common_block:
e7c27a73 5039 read_common_block (die, cu);
c906108c
SS
5040 break;
5041 case DW_TAG_common_inclusion:
5042 break;
d9fa45fe 5043 case DW_TAG_namespace:
63d06c5c 5044 processing_has_namespace_info = 1;
e7c27a73 5045 read_namespace (die, cu);
d9fa45fe 5046 break;
5d7cb8df 5047 case DW_TAG_module:
f55ee35c 5048 processing_has_namespace_info = 1;
5d7cb8df
JK
5049 read_module (die, cu);
5050 break;
d9fa45fe
DC
5051 case DW_TAG_imported_declaration:
5052 case DW_TAG_imported_module:
63d06c5c 5053 processing_has_namespace_info = 1;
27aa8d6a
SW
5054 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5055 || cu->language != language_fortran))
5056 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5057 dwarf_tag_name (die->tag));
5058 read_import_statement (die, cu);
d9fa45fe 5059 break;
c906108c 5060 default:
e7c27a73 5061 new_symbol (die, NULL, cu);
c906108c
SS
5062 break;
5063 }
5064}
5065
94af9270
KS
5066/* A helper function for dwarf2_compute_name which determines whether DIE
5067 needs to have the name of the scope prepended to the name listed in the
5068 die. */
5069
5070static int
5071die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5072{
1c809c68
TT
5073 struct attribute *attr;
5074
94af9270
KS
5075 switch (die->tag)
5076 {
5077 case DW_TAG_namespace:
5078 case DW_TAG_typedef:
5079 case DW_TAG_class_type:
5080 case DW_TAG_interface_type:
5081 case DW_TAG_structure_type:
5082 case DW_TAG_union_type:
5083 case DW_TAG_enumeration_type:
5084 case DW_TAG_enumerator:
5085 case DW_TAG_subprogram:
5086 case DW_TAG_member:
5087 return 1;
5088
5089 case DW_TAG_variable:
c2b0a229 5090 case DW_TAG_constant:
94af9270
KS
5091 /* We only need to prefix "globally" visible variables. These include
5092 any variable marked with DW_AT_external or any variable that
5093 lives in a namespace. [Variables in anonymous namespaces
5094 require prefixing, but they are not DW_AT_external.] */
5095
5096 if (dwarf2_attr (die, DW_AT_specification, cu))
5097 {
5098 struct dwarf2_cu *spec_cu = cu;
9a619af0 5099
94af9270
KS
5100 return die_needs_namespace (die_specification (die, &spec_cu),
5101 spec_cu);
5102 }
5103
1c809c68 5104 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5105 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5106 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5107 return 0;
5108 /* A variable in a lexical block of some kind does not need a
5109 namespace, even though in C++ such variables may be external
5110 and have a mangled name. */
5111 if (die->parent->tag == DW_TAG_lexical_block
5112 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5113 || die->parent->tag == DW_TAG_catch_block
5114 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5115 return 0;
5116 return 1;
94af9270
KS
5117
5118 default:
5119 return 0;
5120 }
5121}
5122
98bfdba5
PA
5123/* Retrieve the last character from a mem_file. */
5124
5125static void
5126do_ui_file_peek_last (void *object, const char *buffer, long length)
5127{
5128 char *last_char_p = (char *) object;
5129
5130 if (length > 0)
5131 *last_char_p = buffer[length - 1];
5132}
5133
94af9270 5134/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5135 compute the physname for the object, which include a method's:
5136 - formal parameters (C++/Java),
5137 - receiver type (Go),
5138 - return type (Java).
5139
5140 The term "physname" is a bit confusing.
5141 For C++, for example, it is the demangled name.
5142 For Go, for example, it's the mangled name.
94af9270 5143
af6b7be1
JB
5144 For Ada, return the DIE's linkage name rather than the fully qualified
5145 name. PHYSNAME is ignored..
5146
94af9270
KS
5147 The result is allocated on the objfile_obstack and canonicalized. */
5148
5149static const char *
5150dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5151 int physname)
5152{
bb5ed363
DE
5153 struct objfile *objfile = cu->objfile;
5154
94af9270
KS
5155 if (name == NULL)
5156 name = dwarf2_name (die, cu);
5157
f55ee35c
JK
5158 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5159 compute it by typename_concat inside GDB. */
5160 if (cu->language == language_ada
5161 || (cu->language == language_fortran && physname))
5162 {
5163 /* For Ada unit, we prefer the linkage name over the name, as
5164 the former contains the exported name, which the user expects
5165 to be able to reference. Ideally, we want the user to be able
5166 to reference this entity using either natural or linkage name,
5167 but we haven't started looking at this enhancement yet. */
5168 struct attribute *attr;
5169
5170 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5171 if (attr == NULL)
5172 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5173 if (attr && DW_STRING (attr))
5174 return DW_STRING (attr);
5175 }
5176
94af9270
KS
5177 /* These are the only languages we know how to qualify names in. */
5178 if (name != NULL
f55ee35c
JK
5179 && (cu->language == language_cplus || cu->language == language_java
5180 || cu->language == language_fortran))
94af9270
KS
5181 {
5182 if (die_needs_namespace (die, cu))
5183 {
5184 long length;
0d5cff50 5185 const char *prefix;
94af9270
KS
5186 struct ui_file *buf;
5187
5188 prefix = determine_prefix (die, cu);
5189 buf = mem_fileopen ();
5190 if (*prefix != '\0')
5191 {
f55ee35c
JK
5192 char *prefixed_name = typename_concat (NULL, prefix, name,
5193 physname, cu);
9a619af0 5194
94af9270
KS
5195 fputs_unfiltered (prefixed_name, buf);
5196 xfree (prefixed_name);
5197 }
5198 else
62d5b8da 5199 fputs_unfiltered (name, buf);
94af9270 5200
98bfdba5
PA
5201 /* Template parameters may be specified in the DIE's DW_AT_name, or
5202 as children with DW_TAG_template_type_param or
5203 DW_TAG_value_type_param. If the latter, add them to the name
5204 here. If the name already has template parameters, then
5205 skip this step; some versions of GCC emit both, and
5206 it is more efficient to use the pre-computed name.
5207
5208 Something to keep in mind about this process: it is very
5209 unlikely, or in some cases downright impossible, to produce
5210 something that will match the mangled name of a function.
5211 If the definition of the function has the same debug info,
5212 we should be able to match up with it anyway. But fallbacks
5213 using the minimal symbol, for instance to find a method
5214 implemented in a stripped copy of libstdc++, will not work.
5215 If we do not have debug info for the definition, we will have to
5216 match them up some other way.
5217
5218 When we do name matching there is a related problem with function
5219 templates; two instantiated function templates are allowed to
5220 differ only by their return types, which we do not add here. */
5221
5222 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5223 {
5224 struct attribute *attr;
5225 struct die_info *child;
5226 int first = 1;
5227
5228 die->building_fullname = 1;
5229
5230 for (child = die->child; child != NULL; child = child->sibling)
5231 {
5232 struct type *type;
12df843f 5233 LONGEST value;
98bfdba5
PA
5234 gdb_byte *bytes;
5235 struct dwarf2_locexpr_baton *baton;
5236 struct value *v;
5237
5238 if (child->tag != DW_TAG_template_type_param
5239 && child->tag != DW_TAG_template_value_param)
5240 continue;
5241
5242 if (first)
5243 {
5244 fputs_unfiltered ("<", buf);
5245 first = 0;
5246 }
5247 else
5248 fputs_unfiltered (", ", buf);
5249
5250 attr = dwarf2_attr (child, DW_AT_type, cu);
5251 if (attr == NULL)
5252 {
5253 complaint (&symfile_complaints,
5254 _("template parameter missing DW_AT_type"));
5255 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5256 continue;
5257 }
5258 type = die_type (child, cu);
5259
5260 if (child->tag == DW_TAG_template_type_param)
5261 {
5262 c_print_type (type, "", buf, -1, 0);
5263 continue;
5264 }
5265
5266 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5267 if (attr == NULL)
5268 {
5269 complaint (&symfile_complaints,
3e43a32a
MS
5270 _("template parameter missing "
5271 "DW_AT_const_value"));
98bfdba5
PA
5272 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5273 continue;
5274 }
5275
5276 dwarf2_const_value_attr (attr, type, name,
5277 &cu->comp_unit_obstack, cu,
5278 &value, &bytes, &baton);
5279
5280 if (TYPE_NOSIGN (type))
5281 /* GDB prints characters as NUMBER 'CHAR'. If that's
5282 changed, this can use value_print instead. */
5283 c_printchar (value, type, buf);
5284 else
5285 {
5286 struct value_print_options opts;
5287
5288 if (baton != NULL)
5289 v = dwarf2_evaluate_loc_desc (type, NULL,
5290 baton->data,
5291 baton->size,
5292 baton->per_cu);
5293 else if (bytes != NULL)
5294 {
5295 v = allocate_value (type);
5296 memcpy (value_contents_writeable (v), bytes,
5297 TYPE_LENGTH (type));
5298 }
5299 else
5300 v = value_from_longest (type, value);
5301
3e43a32a
MS
5302 /* Specify decimal so that we do not depend on
5303 the radix. */
98bfdba5
PA
5304 get_formatted_print_options (&opts, 'd');
5305 opts.raw = 1;
5306 value_print (v, buf, &opts);
5307 release_value (v);
5308 value_free (v);
5309 }
5310 }
5311
5312 die->building_fullname = 0;
5313
5314 if (!first)
5315 {
5316 /* Close the argument list, with a space if necessary
5317 (nested templates). */
5318 char last_char = '\0';
5319 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5320 if (last_char == '>')
5321 fputs_unfiltered (" >", buf);
5322 else
5323 fputs_unfiltered (">", buf);
5324 }
5325 }
5326
94af9270
KS
5327 /* For Java and C++ methods, append formal parameter type
5328 information, if PHYSNAME. */
6e70227d 5329
94af9270
KS
5330 if (physname && die->tag == DW_TAG_subprogram
5331 && (cu->language == language_cplus
5332 || cu->language == language_java))
5333 {
5334 struct type *type = read_type_die (die, cu);
5335
3167638f 5336 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5337
5338 if (cu->language == language_java)
5339 {
5340 /* For java, we must append the return type to method
0963b4bd 5341 names. */
94af9270
KS
5342 if (die->tag == DW_TAG_subprogram)
5343 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5344 0, 0);
5345 }
5346 else if (cu->language == language_cplus)
5347 {
60430eff
DJ
5348 /* Assume that an artificial first parameter is
5349 "this", but do not crash if it is not. RealView
5350 marks unnamed (and thus unused) parameters as
5351 artificial; there is no way to differentiate
5352 the two cases. */
94af9270
KS
5353 if (TYPE_NFIELDS (type) > 0
5354 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5355 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5356 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5357 0))))
94af9270
KS
5358 fputs_unfiltered (" const", buf);
5359 }
5360 }
5361
bb5ed363 5362 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5363 &length);
5364 ui_file_delete (buf);
5365
5366 if (cu->language == language_cplus)
5367 {
5368 char *cname
5369 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5370 &objfile->objfile_obstack);
9a619af0 5371
94af9270
KS
5372 if (cname != NULL)
5373 name = cname;
5374 }
5375 }
5376 }
5377
5378 return name;
5379}
5380
0114d602
DJ
5381/* Return the fully qualified name of DIE, based on its DW_AT_name.
5382 If scope qualifiers are appropriate they will be added. The result
5383 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5384 not have a name. NAME may either be from a previous call to
5385 dwarf2_name or NULL.
5386
0963b4bd 5387 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5388
5389static const char *
94af9270 5390dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5391{
94af9270
KS
5392 return dwarf2_compute_name (name, die, cu, 0);
5393}
0114d602 5394
94af9270
KS
5395/* Construct a physname for the given DIE in CU. NAME may either be
5396 from a previous call to dwarf2_name or NULL. The result will be
5397 allocated on the objfile_objstack or NULL if the DIE does not have a
5398 name.
0114d602 5399
94af9270 5400 The output string will be canonicalized (if C++/Java). */
0114d602 5401
94af9270
KS
5402static const char *
5403dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5404{
bb5ed363 5405 struct objfile *objfile = cu->objfile;
900e11f9
JK
5406 struct attribute *attr;
5407 const char *retval, *mangled = NULL, *canon = NULL;
5408 struct cleanup *back_to;
5409 int need_copy = 1;
5410
5411 /* In this case dwarf2_compute_name is just a shortcut not building anything
5412 on its own. */
5413 if (!die_needs_namespace (die, cu))
5414 return dwarf2_compute_name (name, die, cu, 1);
5415
5416 back_to = make_cleanup (null_cleanup, NULL);
5417
5418 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5419 if (!attr)
5420 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5421
5422 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5423 has computed. */
5424 if (attr && DW_STRING (attr))
5425 {
5426 char *demangled;
5427
5428 mangled = DW_STRING (attr);
5429
5430 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5431 type. It is easier for GDB users to search for such functions as
5432 `name(params)' than `long name(params)'. In such case the minimal
5433 symbol names do not match the full symbol names but for template
5434 functions there is never a need to look up their definition from their
5435 declaration so the only disadvantage remains the minimal symbol
5436 variant `long name(params)' does not have the proper inferior type.
5437 */
5438
a766d390
DE
5439 if (cu->language == language_go)
5440 {
5441 /* This is a lie, but we already lie to the caller new_symbol_full.
5442 new_symbol_full assumes we return the mangled name.
5443 This just undoes that lie until things are cleaned up. */
5444 demangled = NULL;
5445 }
5446 else
5447 {
5448 demangled = cplus_demangle (mangled,
5449 (DMGL_PARAMS | DMGL_ANSI
5450 | (cu->language == language_java
5451 ? DMGL_JAVA | DMGL_RET_POSTFIX
5452 : DMGL_RET_DROP)));
5453 }
900e11f9
JK
5454 if (demangled)
5455 {
5456 make_cleanup (xfree, demangled);
5457 canon = demangled;
5458 }
5459 else
5460 {
5461 canon = mangled;
5462 need_copy = 0;
5463 }
5464 }
5465
5466 if (canon == NULL || check_physname)
5467 {
5468 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5469
5470 if (canon != NULL && strcmp (physname, canon) != 0)
5471 {
5472 /* It may not mean a bug in GDB. The compiler could also
5473 compute DW_AT_linkage_name incorrectly. But in such case
5474 GDB would need to be bug-to-bug compatible. */
5475
5476 complaint (&symfile_complaints,
5477 _("Computed physname <%s> does not match demangled <%s> "
5478 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 5479 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
5480
5481 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5482 is available here - over computed PHYSNAME. It is safer
5483 against both buggy GDB and buggy compilers. */
5484
5485 retval = canon;
5486 }
5487 else
5488 {
5489 retval = physname;
5490 need_copy = 0;
5491 }
5492 }
5493 else
5494 retval = canon;
5495
5496 if (need_copy)
5497 retval = obsavestring (retval, strlen (retval),
bb5ed363 5498 &objfile->objfile_obstack);
900e11f9
JK
5499
5500 do_cleanups (back_to);
5501 return retval;
0114d602
DJ
5502}
5503
27aa8d6a
SW
5504/* Read the import statement specified by the given die and record it. */
5505
5506static void
5507read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5508{
bb5ed363 5509 struct objfile *objfile = cu->objfile;
27aa8d6a 5510 struct attribute *import_attr;
32019081 5511 struct die_info *imported_die, *child_die;
de4affc9 5512 struct dwarf2_cu *imported_cu;
27aa8d6a 5513 const char *imported_name;
794684b6 5514 const char *imported_name_prefix;
13387711
SW
5515 const char *canonical_name;
5516 const char *import_alias;
5517 const char *imported_declaration = NULL;
794684b6 5518 const char *import_prefix;
32019081
JK
5519 VEC (const_char_ptr) *excludes = NULL;
5520 struct cleanup *cleanups;
13387711
SW
5521
5522 char *temp;
27aa8d6a
SW
5523
5524 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5525 if (import_attr == NULL)
5526 {
5527 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5528 dwarf_tag_name (die->tag));
5529 return;
5530 }
5531
de4affc9
CC
5532 imported_cu = cu;
5533 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5534 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5535 if (imported_name == NULL)
5536 {
5537 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5538
5539 The import in the following code:
5540 namespace A
5541 {
5542 typedef int B;
5543 }
5544
5545 int main ()
5546 {
5547 using A::B;
5548 B b;
5549 return b;
5550 }
5551
5552 ...
5553 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5554 <52> DW_AT_decl_file : 1
5555 <53> DW_AT_decl_line : 6
5556 <54> DW_AT_import : <0x75>
5557 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5558 <59> DW_AT_name : B
5559 <5b> DW_AT_decl_file : 1
5560 <5c> DW_AT_decl_line : 2
5561 <5d> DW_AT_type : <0x6e>
5562 ...
5563 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5564 <76> DW_AT_byte_size : 4
5565 <77> DW_AT_encoding : 5 (signed)
5566
5567 imports the wrong die ( 0x75 instead of 0x58 ).
5568 This case will be ignored until the gcc bug is fixed. */
5569 return;
5570 }
5571
82856980
SW
5572 /* Figure out the local name after import. */
5573 import_alias = dwarf2_name (die, cu);
27aa8d6a 5574
794684b6
SW
5575 /* Figure out where the statement is being imported to. */
5576 import_prefix = determine_prefix (die, cu);
5577
5578 /* Figure out what the scope of the imported die is and prepend it
5579 to the name of the imported die. */
de4affc9 5580 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5581
f55ee35c
JK
5582 if (imported_die->tag != DW_TAG_namespace
5583 && imported_die->tag != DW_TAG_module)
794684b6 5584 {
13387711
SW
5585 imported_declaration = imported_name;
5586 canonical_name = imported_name_prefix;
794684b6 5587 }
13387711 5588 else if (strlen (imported_name_prefix) > 0)
794684b6 5589 {
13387711
SW
5590 temp = alloca (strlen (imported_name_prefix)
5591 + 2 + strlen (imported_name) + 1);
5592 strcpy (temp, imported_name_prefix);
5593 strcat (temp, "::");
5594 strcat (temp, imported_name);
5595 canonical_name = temp;
794684b6 5596 }
13387711
SW
5597 else
5598 canonical_name = imported_name;
794684b6 5599
32019081
JK
5600 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5601
5602 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5603 for (child_die = die->child; child_die && child_die->tag;
5604 child_die = sibling_die (child_die))
5605 {
5606 /* DWARF-4: A Fortran use statement with a “rename list” may be
5607 represented by an imported module entry with an import attribute
5608 referring to the module and owned entries corresponding to those
5609 entities that are renamed as part of being imported. */
5610
5611 if (child_die->tag != DW_TAG_imported_declaration)
5612 {
5613 complaint (&symfile_complaints,
5614 _("child DW_TAG_imported_declaration expected "
5615 "- DIE at 0x%x [in module %s]"),
b64f50a1 5616 child_die->offset.sect_off, objfile->name);
32019081
JK
5617 continue;
5618 }
5619
5620 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5621 if (import_attr == NULL)
5622 {
5623 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5624 dwarf_tag_name (child_die->tag));
5625 continue;
5626 }
5627
5628 imported_cu = cu;
5629 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5630 &imported_cu);
5631 imported_name = dwarf2_name (imported_die, imported_cu);
5632 if (imported_name == NULL)
5633 {
5634 complaint (&symfile_complaints,
5635 _("child DW_TAG_imported_declaration has unknown "
5636 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 5637 child_die->offset.sect_off, objfile->name);
32019081
JK
5638 continue;
5639 }
5640
5641 VEC_safe_push (const_char_ptr, excludes, imported_name);
5642
5643 process_die (child_die, cu);
5644 }
5645
c0cc3a76
SW
5646 cp_add_using_directive (import_prefix,
5647 canonical_name,
5648 import_alias,
13387711 5649 imported_declaration,
32019081 5650 excludes,
bb5ed363 5651 &objfile->objfile_obstack);
32019081
JK
5652
5653 do_cleanups (cleanups);
27aa8d6a
SW
5654}
5655
ae2de4f8
DE
5656/* Cleanup function for read_file_scope. */
5657
cb1df416
DJ
5658static void
5659free_cu_line_header (void *arg)
5660{
5661 struct dwarf2_cu *cu = arg;
5662
5663 free_line_header (cu->line_header);
5664 cu->line_header = NULL;
5665}
5666
9291a0cd
TT
5667static void
5668find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5669 char **name, char **comp_dir)
5670{
5671 struct attribute *attr;
5672
5673 *name = NULL;
5674 *comp_dir = NULL;
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 {
5681 *name = DW_STRING (attr);
5682 }
5683
5684 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5685 if (attr)
5686 *comp_dir = DW_STRING (attr);
5687 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5688 {
5689 *comp_dir = ldirname (*name);
5690 if (*comp_dir != NULL)
5691 make_cleanup (xfree, *comp_dir);
5692 }
5693 if (*comp_dir != NULL)
5694 {
5695 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5696 directory, get rid of it. */
5697 char *cp = strchr (*comp_dir, ':');
5698
5699 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5700 *comp_dir = cp + 1;
5701 }
5702
5703 if (*name == NULL)
5704 *name = "<unknown>";
5705}
5706
f3f5162e
DE
5707/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5708 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5709 COMP_DIR is the compilation directory.
5710 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5711
5712static void
5713handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5714 const char *comp_dir, int want_line_info)
2ab95328
TT
5715{
5716 struct attribute *attr;
5717 struct objfile *objfile = cu->objfile;
5718 bfd *abfd = objfile->obfd;
5719
2ab95328
TT
5720 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5721 if (attr)
5722 {
5723 unsigned int line_offset = DW_UNSND (attr);
5724 struct line_header *line_header
5725 = dwarf_decode_line_header (line_offset, abfd, cu);
5726
5727 if (line_header)
5728 {
5729 cu->line_header = line_header;
5730 make_cleanup (free_cu_line_header, cu);
f3f5162e 5731 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5732 }
5733 }
5734}
5735
ae2de4f8
DE
5736/* Process DW_TAG_compile_unit. */
5737
c906108c 5738static void
e7c27a73 5739read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5740{
e7c27a73 5741 struct objfile *objfile = cu->objfile;
debd256d 5742 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5743 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5744 CORE_ADDR highpc = ((CORE_ADDR) 0);
5745 struct attribute *attr;
e1024ff1 5746 char *name = NULL;
c906108c
SS
5747 char *comp_dir = NULL;
5748 struct die_info *child_die;
5749 bfd *abfd = objfile->obfd;
e142c38c 5750 CORE_ADDR baseaddr;
6e70227d 5751
e142c38c 5752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5753
fae299cd 5754 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5755
5756 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5757 from finish_block. */
2acceee2 5758 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5759 lowpc = highpc;
5760 lowpc += baseaddr;
5761 highpc += baseaddr;
5762
9291a0cd 5763 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5764
e142c38c 5765 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5766 if (attr)
5767 {
e142c38c 5768 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5769 }
5770
b0f35d58 5771 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5772 if (attr)
b0f35d58 5773 cu->producer = DW_STRING (attr);
303b6f5d 5774
f4b8a18d
KW
5775 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5776 standardised yet. As a workaround for the language detection we fall
5777 back to the DW_AT_producer string. */
5778 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5779 cu->language = language_opencl;
5780
a766d390
DE
5781 /* Similar hack for Go. */
5782 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
5783 set_cu_language (DW_LANG_Go, cu);
5784
0963b4bd 5785 /* We assume that we're processing GCC output. */
c906108c 5786 processing_gcc_compilation = 2;
c906108c 5787
df8a16a1
DJ
5788 processing_has_namespace_info = 0;
5789
c906108c
SS
5790 start_symtab (name, comp_dir, lowpc);
5791 record_debugformat ("DWARF 2");
303b6f5d 5792 record_producer (cu->producer);
c906108c 5793
f3f5162e
DE
5794 /* Decode line number information if present. We do this before
5795 processing child DIEs, so that the line header table is available
5796 for DW_AT_decl_file. */
5797 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5798
cb1df416
DJ
5799 /* Process all dies in compilation unit. */
5800 if (die->child != NULL)
5801 {
5802 child_die = die->child;
5803 while (child_die && child_die->tag)
5804 {
5805 process_die (child_die, cu);
5806 child_die = sibling_die (child_die);
5807 }
5808 }
5809
2e276125
JB
5810 /* Decode macro information, if present. Dwarf 2 macro information
5811 refers to information in the line number info statement program
5812 header, so we can only read it if we've read the header
5813 successfully. */
cf2c3c16 5814 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5815 if (attr && cu->line_header)
2e276125 5816 {
cf2c3c16
TT
5817 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5818 complaint (&symfile_complaints,
5819 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5820
5821 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5822 comp_dir, abfd, cu,
5823 &dwarf2_per_objfile->macro, 1);
5824 }
5825 else
5826 {
5827 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5828 if (attr && cu->line_header)
5829 {
5830 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5831
cf2c3c16
TT
5832 dwarf_decode_macros (cu->line_header, macro_offset,
5833 comp_dir, abfd, cu,
5834 &dwarf2_per_objfile->macinfo, 0);
5835 }
2e276125 5836 }
9cdd5dbd 5837
debd256d 5838 do_cleanups (back_to);
5fb290d7
DJ
5839}
5840
ae2de4f8
DE
5841/* Process DW_TAG_type_unit.
5842 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5843 actual type being defined by this TU. In this case the first top
5844 level sibling is there to provide context only. */
5845
5846static void
5847read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5848{
5849 struct objfile *objfile = cu->objfile;
5850 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5851 CORE_ADDR lowpc;
5852 struct attribute *attr;
5853 char *name = NULL;
5854 char *comp_dir = NULL;
5855 struct die_info *child_die;
5856 bfd *abfd = objfile->obfd;
348e048f
DE
5857
5858 /* start_symtab needs a low pc, but we don't really have one.
5859 Do what read_file_scope would do in the absence of such info. */
5860 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5861
5862 /* Find the filename. Do not use dwarf2_name here, since the filename
5863 is not a source language identifier. */
5864 attr = dwarf2_attr (die, DW_AT_name, cu);
5865 if (attr)
5866 name = DW_STRING (attr);
5867
5868 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5869 if (attr)
5870 comp_dir = DW_STRING (attr);
5871 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5872 {
5873 comp_dir = ldirname (name);
5874 if (comp_dir != NULL)
5875 make_cleanup (xfree, comp_dir);
5876 }
5877
5878 if (name == NULL)
5879 name = "<unknown>";
5880
5881 attr = dwarf2_attr (die, DW_AT_language, cu);
5882 if (attr)
5883 set_cu_language (DW_UNSND (attr), cu);
5884
5885 /* This isn't technically needed today. It is done for symmetry
5886 with read_file_scope. */
5887 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5888 if (attr)
348e048f
DE
5889 cu->producer = DW_STRING (attr);
5890
0963b4bd 5891 /* We assume that we're processing GCC output. */
348e048f
DE
5892 processing_gcc_compilation = 2;
5893
5894 processing_has_namespace_info = 0;
5895
5896 start_symtab (name, comp_dir, lowpc);
5897 record_debugformat ("DWARF 2");
5898 record_producer (cu->producer);
5899
f3f5162e
DE
5900 /* Decode line number information if present. We do this before
5901 processing child DIEs, so that the line header table is available
5902 for DW_AT_decl_file.
5903 We don't need the pc/line-number mapping for type units. */
5904 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5905
348e048f
DE
5906 /* Process the dies in the type unit. */
5907 if (die->child == NULL)
5908 {
5909 dump_die_for_error (die);
5910 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5911 bfd_get_filename (abfd));
5912 }
5913
5914 child_die = die->child;
5915
5916 while (child_die && child_die->tag)
5917 {
5918 process_die (child_die, cu);
5919
5920 child_die = sibling_die (child_die);
5921 }
5922
5923 do_cleanups (back_to);
5924}
5925
d389af10
JK
5926/* qsort helper for inherit_abstract_dies. */
5927
5928static int
5929unsigned_int_compar (const void *ap, const void *bp)
5930{
5931 unsigned int a = *(unsigned int *) ap;
5932 unsigned int b = *(unsigned int *) bp;
5933
5934 return (a > b) - (b > a);
5935}
5936
5937/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5938 Inherit only the children of the DW_AT_abstract_origin DIE not being
5939 already referenced by DW_AT_abstract_origin from the children of the
5940 current DIE. */
d389af10
JK
5941
5942static void
5943inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5944{
5945 struct die_info *child_die;
5946 unsigned die_children_count;
5947 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
5948 sect_offset *offsets;
5949 sect_offset *offsets_end, *offsetp;
d389af10
JK
5950 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5951 struct die_info *origin_die;
5952 /* Iterator of the ORIGIN_DIE children. */
5953 struct die_info *origin_child_die;
5954 struct cleanup *cleanups;
5955 struct attribute *attr;
cd02d79d
PA
5956 struct dwarf2_cu *origin_cu;
5957 struct pending **origin_previous_list_in_scope;
d389af10
JK
5958
5959 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5960 if (!attr)
5961 return;
5962
cd02d79d
PA
5963 /* Note that following die references may follow to a die in a
5964 different cu. */
5965
5966 origin_cu = cu;
5967 origin_die = follow_die_ref (die, attr, &origin_cu);
5968
5969 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5970 symbols in. */
5971 origin_previous_list_in_scope = origin_cu->list_in_scope;
5972 origin_cu->list_in_scope = cu->list_in_scope;
5973
edb3359d
DJ
5974 if (die->tag != origin_die->tag
5975 && !(die->tag == DW_TAG_inlined_subroutine
5976 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5977 complaint (&symfile_complaints,
5978 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 5979 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
5980
5981 child_die = die->child;
5982 die_children_count = 0;
5983 while (child_die && child_die->tag)
5984 {
5985 child_die = sibling_die (child_die);
5986 die_children_count++;
5987 }
5988 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5989 cleanups = make_cleanup (xfree, offsets);
5990
5991 offsets_end = offsets;
5992 child_die = die->child;
5993 while (child_die && child_die->tag)
5994 {
c38f313d
DJ
5995 /* For each CHILD_DIE, find the corresponding child of
5996 ORIGIN_DIE. If there is more than one layer of
5997 DW_AT_abstract_origin, follow them all; there shouldn't be,
5998 but GCC versions at least through 4.4 generate this (GCC PR
5999 40573). */
6000 struct die_info *child_origin_die = child_die;
cd02d79d 6001 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 6002
c38f313d
DJ
6003 while (1)
6004 {
cd02d79d
PA
6005 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
6006 child_origin_cu);
c38f313d
DJ
6007 if (attr == NULL)
6008 break;
cd02d79d
PA
6009 child_origin_die = follow_die_ref (child_origin_die, attr,
6010 &child_origin_cu);
c38f313d
DJ
6011 }
6012
d389af10
JK
6013 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
6014 counterpart may exist. */
c38f313d 6015 if (child_origin_die != child_die)
d389af10 6016 {
edb3359d
DJ
6017 if (child_die->tag != child_origin_die->tag
6018 && !(child_die->tag == DW_TAG_inlined_subroutine
6019 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
6020 complaint (&symfile_complaints,
6021 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
6022 "different tags"), child_die->offset.sect_off,
6023 child_origin_die->offset.sect_off);
c38f313d
DJ
6024 if (child_origin_die->parent != origin_die)
6025 complaint (&symfile_complaints,
6026 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
6027 "different parents"), child_die->offset.sect_off,
6028 child_origin_die->offset.sect_off);
c38f313d
DJ
6029 else
6030 *offsets_end++ = child_origin_die->offset;
d389af10
JK
6031 }
6032 child_die = sibling_die (child_die);
6033 }
6034 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
6035 unsigned_int_compar);
6036 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 6037 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
6038 complaint (&symfile_complaints,
6039 _("Multiple children of DIE 0x%x refer "
6040 "to DIE 0x%x as their abstract origin"),
b64f50a1 6041 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
6042
6043 offsetp = offsets;
6044 origin_child_die = origin_die->child;
6045 while (origin_child_die && origin_child_die->tag)
6046 {
6047 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
6048 while (offsetp < offsets_end
6049 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 6050 offsetp++;
b64f50a1
JK
6051 if (offsetp >= offsets_end
6052 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
6053 {
6054 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 6055 process_die (origin_child_die, origin_cu);
d389af10
JK
6056 }
6057 origin_child_die = sibling_die (origin_child_die);
6058 }
cd02d79d 6059 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
6060
6061 do_cleanups (cleanups);
6062}
6063
c906108c 6064static void
e7c27a73 6065read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6066{
e7c27a73 6067 struct objfile *objfile = cu->objfile;
52f0bd74 6068 struct context_stack *new;
c906108c
SS
6069 CORE_ADDR lowpc;
6070 CORE_ADDR highpc;
6071 struct die_info *child_die;
edb3359d 6072 struct attribute *attr, *call_line, *call_file;
c906108c 6073 char *name;
e142c38c 6074 CORE_ADDR baseaddr;
801e3a5b 6075 struct block *block;
edb3359d 6076 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
6077 VEC (symbolp) *template_args = NULL;
6078 struct template_symbol *templ_func = NULL;
edb3359d
DJ
6079
6080 if (inlined_func)
6081 {
6082 /* If we do not have call site information, we can't show the
6083 caller of this inlined function. That's too confusing, so
6084 only use the scope for local variables. */
6085 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
6086 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
6087 if (call_line == NULL || call_file == NULL)
6088 {
6089 read_lexical_block_scope (die, cu);
6090 return;
6091 }
6092 }
c906108c 6093
e142c38c
DJ
6094 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6095
94af9270 6096 name = dwarf2_name (die, cu);
c906108c 6097
e8d05480
JB
6098 /* Ignore functions with missing or empty names. These are actually
6099 illegal according to the DWARF standard. */
6100 if (name == NULL)
6101 {
6102 complaint (&symfile_complaints,
b64f50a1
JK
6103 _("missing name for subprogram DIE at %d"),
6104 die->offset.sect_off);
e8d05480
JB
6105 return;
6106 }
6107
6108 /* Ignore functions with missing or invalid low and high pc attributes. */
6109 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6110 {
ae4d0c03
PM
6111 attr = dwarf2_attr (die, DW_AT_external, cu);
6112 if (!attr || !DW_UNSND (attr))
6113 complaint (&symfile_complaints,
3e43a32a
MS
6114 _("cannot get low and high bounds "
6115 "for subprogram DIE at %d"),
b64f50a1 6116 die->offset.sect_off);
e8d05480
JB
6117 return;
6118 }
c906108c
SS
6119
6120 lowpc += baseaddr;
6121 highpc += baseaddr;
6122
34eaf542
TT
6123 /* If we have any template arguments, then we must allocate a
6124 different sort of symbol. */
6125 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6126 {
6127 if (child_die->tag == DW_TAG_template_type_param
6128 || child_die->tag == DW_TAG_template_value_param)
6129 {
6130 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6131 struct template_symbol);
6132 templ_func->base.is_cplus_template_function = 1;
6133 break;
6134 }
6135 }
6136
c906108c 6137 new = push_context (0, lowpc);
34eaf542
TT
6138 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6139 (struct symbol *) templ_func);
4c2df51b 6140
4cecd739
DJ
6141 /* If there is a location expression for DW_AT_frame_base, record
6142 it. */
e142c38c 6143 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6144 if (attr)
c034e007
AC
6145 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6146 expression is being recorded directly in the function's symbol
6147 and not in a separate frame-base object. I guess this hack is
6148 to avoid adding some sort of frame-base adjunct/annex to the
6149 function's symbol :-(. The problem with doing this is that it
6150 results in a function symbol with a location expression that
6151 has nothing to do with the location of the function, ouch! The
6152 relationship should be: a function's symbol has-a frame base; a
6153 frame-base has-a location expression. */
e7c27a73 6154 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6155
e142c38c 6156 cu->list_in_scope = &local_symbols;
c906108c 6157
639d11d3 6158 if (die->child != NULL)
c906108c 6159 {
639d11d3 6160 child_die = die->child;
c906108c
SS
6161 while (child_die && child_die->tag)
6162 {
34eaf542
TT
6163 if (child_die->tag == DW_TAG_template_type_param
6164 || child_die->tag == DW_TAG_template_value_param)
6165 {
6166 struct symbol *arg = new_symbol (child_die, NULL, cu);
6167
f1078f66
DJ
6168 if (arg != NULL)
6169 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6170 }
6171 else
6172 process_die (child_die, cu);
c906108c
SS
6173 child_die = sibling_die (child_die);
6174 }
6175 }
6176
d389af10
JK
6177 inherit_abstract_dies (die, cu);
6178
4a811a97
UW
6179 /* If we have a DW_AT_specification, we might need to import using
6180 directives from the context of the specification DIE. See the
6181 comment in determine_prefix. */
6182 if (cu->language == language_cplus
6183 && dwarf2_attr (die, DW_AT_specification, cu))
6184 {
6185 struct dwarf2_cu *spec_cu = cu;
6186 struct die_info *spec_die = die_specification (die, &spec_cu);
6187
6188 while (spec_die)
6189 {
6190 child_die = spec_die->child;
6191 while (child_die && child_die->tag)
6192 {
6193 if (child_die->tag == DW_TAG_imported_module)
6194 process_die (child_die, spec_cu);
6195 child_die = sibling_die (child_die);
6196 }
6197
6198 /* In some cases, GCC generates specification DIEs that
6199 themselves contain DW_AT_specification attributes. */
6200 spec_die = die_specification (spec_die, &spec_cu);
6201 }
6202 }
6203
c906108c
SS
6204 new = pop_context ();
6205 /* Make a block for the local symbols within. */
801e3a5b
JB
6206 block = finish_block (new->name, &local_symbols, new->old_blocks,
6207 lowpc, highpc, objfile);
6208
df8a16a1 6209 /* For C++, set the block's scope. */
f55ee35c 6210 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6211 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6212 determine_prefix (die, cu),
df8a16a1
DJ
6213 processing_has_namespace_info);
6214
801e3a5b
JB
6215 /* If we have address ranges, record them. */
6216 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6217
34eaf542
TT
6218 /* Attach template arguments to function. */
6219 if (! VEC_empty (symbolp, template_args))
6220 {
6221 gdb_assert (templ_func != NULL);
6222
6223 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6224 templ_func->template_arguments
6225 = obstack_alloc (&objfile->objfile_obstack,
6226 (templ_func->n_template_arguments
6227 * sizeof (struct symbol *)));
6228 memcpy (templ_func->template_arguments,
6229 VEC_address (symbolp, template_args),
6230 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6231 VEC_free (symbolp, template_args);
6232 }
6233
208d8187
JB
6234 /* In C++, we can have functions nested inside functions (e.g., when
6235 a function declares a class that has methods). This means that
6236 when we finish processing a function scope, we may need to go
6237 back to building a containing block's symbol lists. */
6238 local_symbols = new->locals;
6239 param_symbols = new->params;
27aa8d6a 6240 using_directives = new->using_directives;
208d8187 6241
921e78cf
JB
6242 /* If we've finished processing a top-level function, subsequent
6243 symbols go in the file symbol list. */
6244 if (outermost_context_p ())
e142c38c 6245 cu->list_in_scope = &file_symbols;
c906108c
SS
6246}
6247
6248/* Process all the DIES contained within a lexical block scope. Start
6249 a new scope, process the dies, and then close the scope. */
6250
6251static void
e7c27a73 6252read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6253{
e7c27a73 6254 struct objfile *objfile = cu->objfile;
52f0bd74 6255 struct context_stack *new;
c906108c
SS
6256 CORE_ADDR lowpc, highpc;
6257 struct die_info *child_die;
e142c38c
DJ
6258 CORE_ADDR baseaddr;
6259
6260 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6261
6262 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6263 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6264 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6265 be nasty. Might be easier to properly extend generic blocks to
af34e669 6266 describe ranges. */
d85a05f0 6267 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6268 return;
6269 lowpc += baseaddr;
6270 highpc += baseaddr;
6271
6272 push_context (0, lowpc);
639d11d3 6273 if (die->child != NULL)
c906108c 6274 {
639d11d3 6275 child_die = die->child;
c906108c
SS
6276 while (child_die && child_die->tag)
6277 {
e7c27a73 6278 process_die (child_die, cu);
c906108c
SS
6279 child_die = sibling_die (child_die);
6280 }
6281 }
6282 new = pop_context ();
6283
8540c487 6284 if (local_symbols != NULL || using_directives != NULL)
c906108c 6285 {
801e3a5b
JB
6286 struct block *block
6287 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6288 highpc, objfile);
6289
6290 /* Note that recording ranges after traversing children, as we
6291 do here, means that recording a parent's ranges entails
6292 walking across all its children's ranges as they appear in
6293 the address map, which is quadratic behavior.
6294
6295 It would be nicer to record the parent's ranges before
6296 traversing its children, simply overriding whatever you find
6297 there. But since we don't even decide whether to create a
6298 block until after we've traversed its children, that's hard
6299 to do. */
6300 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6301 }
6302 local_symbols = new->locals;
27aa8d6a 6303 using_directives = new->using_directives;
c906108c
SS
6304}
6305
96408a79
SA
6306/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6307
6308static void
6309read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6310{
6311 struct objfile *objfile = cu->objfile;
6312 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6313 CORE_ADDR pc, baseaddr;
6314 struct attribute *attr;
6315 struct call_site *call_site, call_site_local;
6316 void **slot;
6317 int nparams;
6318 struct die_info *child_die;
6319
6320 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6321
6322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6323 if (!attr)
6324 {
6325 complaint (&symfile_complaints,
6326 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6327 "DIE 0x%x [in module %s]"),
b64f50a1 6328 die->offset.sect_off, objfile->name);
96408a79
SA
6329 return;
6330 }
6331 pc = DW_ADDR (attr) + baseaddr;
6332
6333 if (cu->call_site_htab == NULL)
6334 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6335 NULL, &objfile->objfile_obstack,
6336 hashtab_obstack_allocate, NULL);
6337 call_site_local.pc = pc;
6338 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6339 if (*slot != NULL)
6340 {
6341 complaint (&symfile_complaints,
6342 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6343 "DIE 0x%x [in module %s]"),
b64f50a1 6344 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
6345 return;
6346 }
6347
6348 /* Count parameters at the caller. */
6349
6350 nparams = 0;
6351 for (child_die = die->child; child_die && child_die->tag;
6352 child_die = sibling_die (child_die))
6353 {
6354 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6355 {
6356 complaint (&symfile_complaints,
6357 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6358 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6359 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
6360 continue;
6361 }
6362
6363 nparams++;
6364 }
6365
6366 call_site = obstack_alloc (&objfile->objfile_obstack,
6367 (sizeof (*call_site)
6368 + (sizeof (*call_site->parameter)
6369 * (nparams - 1))));
6370 *slot = call_site;
6371 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6372 call_site->pc = pc;
6373
6374 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6375 {
6376 struct die_info *func_die;
6377
6378 /* Skip also over DW_TAG_inlined_subroutine. */
6379 for (func_die = die->parent;
6380 func_die && func_die->tag != DW_TAG_subprogram
6381 && func_die->tag != DW_TAG_subroutine_type;
6382 func_die = func_die->parent);
6383
6384 /* DW_AT_GNU_all_call_sites is a superset
6385 of DW_AT_GNU_all_tail_call_sites. */
6386 if (func_die
6387 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6388 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6389 {
6390 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6391 not complete. But keep CALL_SITE for look ups via call_site_htab,
6392 both the initial caller containing the real return address PC and
6393 the final callee containing the current PC of a chain of tail
6394 calls do not need to have the tail call list complete. But any
6395 function candidate for a virtual tail call frame searched via
6396 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6397 determined unambiguously. */
6398 }
6399 else
6400 {
6401 struct type *func_type = NULL;
6402
6403 if (func_die)
6404 func_type = get_die_type (func_die, cu);
6405 if (func_type != NULL)
6406 {
6407 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6408
6409 /* Enlist this call site to the function. */
6410 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6411 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6412 }
6413 else
6414 complaint (&symfile_complaints,
6415 _("Cannot find function owning DW_TAG_GNU_call_site "
6416 "DIE 0x%x [in module %s]"),
b64f50a1 6417 die->offset.sect_off, objfile->name);
96408a79
SA
6418 }
6419 }
6420
6421 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6422 if (attr == NULL)
6423 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6424 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6425 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6426 /* Keep NULL DWARF_BLOCK. */;
6427 else if (attr_form_is_block (attr))
6428 {
6429 struct dwarf2_locexpr_baton *dlbaton;
6430
6431 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6432 dlbaton->data = DW_BLOCK (attr)->data;
6433 dlbaton->size = DW_BLOCK (attr)->size;
6434 dlbaton->per_cu = cu->per_cu;
6435
6436 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6437 }
6438 else if (is_ref_attr (attr))
6439 {
96408a79
SA
6440 struct dwarf2_cu *target_cu = cu;
6441 struct die_info *target_die;
6442
6443 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6444 gdb_assert (target_cu->objfile == objfile);
6445 if (die_is_declaration (target_die, target_cu))
6446 {
6447 const char *target_physname;
6448
6449 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6450 if (target_physname == NULL)
6451 complaint (&symfile_complaints,
6452 _("DW_AT_GNU_call_site_target target DIE has invalid "
6453 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6454 die->offset.sect_off, objfile->name);
96408a79
SA
6455 else
6456 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6457 }
6458 else
6459 {
6460 CORE_ADDR lowpc;
6461
6462 /* DW_AT_entry_pc should be preferred. */
6463 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6464 complaint (&symfile_complaints,
6465 _("DW_AT_GNU_call_site_target target DIE has invalid "
6466 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6467 die->offset.sect_off, objfile->name);
96408a79
SA
6468 else
6469 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6470 }
6471 }
6472 else
6473 complaint (&symfile_complaints,
6474 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6475 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 6476 die->offset.sect_off, objfile->name);
96408a79
SA
6477
6478 call_site->per_cu = cu->per_cu;
6479
6480 for (child_die = die->child;
6481 child_die && child_die->tag;
6482 child_die = sibling_die (child_die))
6483 {
6484 struct dwarf2_locexpr_baton *dlbaton;
6485 struct call_site_parameter *parameter;
6486
6487 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6488 {
6489 /* Already printed the complaint above. */
6490 continue;
6491 }
6492
6493 gdb_assert (call_site->parameter_count < nparams);
6494 parameter = &call_site->parameter[call_site->parameter_count];
6495
6496 /* DW_AT_location specifies the register number. Value of the data
6497 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6498
6499 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6500 if (!attr || !attr_form_is_block (attr))
6501 {
6502 complaint (&symfile_complaints,
6503 _("No DW_FORM_block* DW_AT_location for "
6504 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6505 child_die->offset.sect_off, objfile->name);
96408a79
SA
6506 continue;
6507 }
6508 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6509 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6510 if (parameter->dwarf_reg == -1
6511 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6512 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6513 &parameter->fb_offset))
6514 {
6515 complaint (&symfile_complaints,
6516 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6517 "for DW_FORM_block* DW_AT_location for "
6518 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6519 child_die->offset.sect_off, objfile->name);
96408a79
SA
6520 continue;
6521 }
6522
6523 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6524 if (!attr_form_is_block (attr))
6525 {
6526 complaint (&symfile_complaints,
6527 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6528 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6529 child_die->offset.sect_off, objfile->name);
96408a79
SA
6530 continue;
6531 }
6532 parameter->value = DW_BLOCK (attr)->data;
6533 parameter->value_size = DW_BLOCK (attr)->size;
6534
6535 /* Parameters are not pre-cleared by memset above. */
6536 parameter->data_value = NULL;
6537 parameter->data_value_size = 0;
6538 call_site->parameter_count++;
6539
6540 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6541 if (attr)
6542 {
6543 if (!attr_form_is_block (attr))
6544 complaint (&symfile_complaints,
6545 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6546 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6547 child_die->offset.sect_off, objfile->name);
96408a79
SA
6548 else
6549 {
6550 parameter->data_value = DW_BLOCK (attr)->data;
6551 parameter->data_value_size = DW_BLOCK (attr)->size;
6552 }
6553 }
6554 }
6555}
6556
43039443 6557/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6558 Return 1 if the attributes are present and valid, otherwise, return 0.
6559 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6560
6561static int
6562dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6563 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6564 struct partial_symtab *ranges_pst)
43039443
JK
6565{
6566 struct objfile *objfile = cu->objfile;
6567 struct comp_unit_head *cu_header = &cu->header;
6568 bfd *obfd = objfile->obfd;
6569 unsigned int addr_size = cu_header->addr_size;
6570 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6571 /* Base address selection entry. */
6572 CORE_ADDR base;
6573 int found_base;
6574 unsigned int dummy;
6575 gdb_byte *buffer;
6576 CORE_ADDR marker;
6577 int low_set;
6578 CORE_ADDR low = 0;
6579 CORE_ADDR high = 0;
ff013f42 6580 CORE_ADDR baseaddr;
43039443 6581
d00adf39
DE
6582 found_base = cu->base_known;
6583 base = cu->base_address;
43039443 6584
be391dca 6585 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6586 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6587 {
6588 complaint (&symfile_complaints,
6589 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6590 offset);
6591 return 0;
6592 }
dce234bc 6593 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6594
6595 /* Read in the largest possible address. */
6596 marker = read_address (obfd, buffer, cu, &dummy);
6597 if ((marker & mask) == mask)
6598 {
6599 /* If we found the largest possible address, then
6600 read the base address. */
6601 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6602 buffer += 2 * addr_size;
6603 offset += 2 * addr_size;
6604 found_base = 1;
6605 }
6606
6607 low_set = 0;
6608
e7030f15 6609 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6610
43039443
JK
6611 while (1)
6612 {
6613 CORE_ADDR range_beginning, range_end;
6614
6615 range_beginning = read_address (obfd, buffer, cu, &dummy);
6616 buffer += addr_size;
6617 range_end = read_address (obfd, buffer, cu, &dummy);
6618 buffer += addr_size;
6619 offset += 2 * addr_size;
6620
6621 /* An end of list marker is a pair of zero addresses. */
6622 if (range_beginning == 0 && range_end == 0)
6623 /* Found the end of list entry. */
6624 break;
6625
6626 /* Each base address selection entry is a pair of 2 values.
6627 The first is the largest possible address, the second is
6628 the base address. Check for a base address here. */
6629 if ((range_beginning & mask) == mask)
6630 {
6631 /* If we found the largest possible address, then
6632 read the base address. */
6633 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6634 found_base = 1;
6635 continue;
6636 }
6637
6638 if (!found_base)
6639 {
6640 /* We have no valid base address for the ranges
6641 data. */
6642 complaint (&symfile_complaints,
6643 _("Invalid .debug_ranges data (no base address)"));
6644 return 0;
6645 }
6646
9277c30c
UW
6647 if (range_beginning > range_end)
6648 {
6649 /* Inverted range entries are invalid. */
6650 complaint (&symfile_complaints,
6651 _("Invalid .debug_ranges data (inverted range)"));
6652 return 0;
6653 }
6654
6655 /* Empty range entries have no effect. */
6656 if (range_beginning == range_end)
6657 continue;
6658
43039443
JK
6659 range_beginning += base;
6660 range_end += base;
6661
9277c30c 6662 if (ranges_pst != NULL)
ff013f42 6663 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6664 range_beginning + baseaddr,
6665 range_end - 1 + baseaddr,
ff013f42
JK
6666 ranges_pst);
6667
43039443
JK
6668 /* FIXME: This is recording everything as a low-high
6669 segment of consecutive addresses. We should have a
6670 data structure for discontiguous block ranges
6671 instead. */
6672 if (! low_set)
6673 {
6674 low = range_beginning;
6675 high = range_end;
6676 low_set = 1;
6677 }
6678 else
6679 {
6680 if (range_beginning < low)
6681 low = range_beginning;
6682 if (range_end > high)
6683 high = range_end;
6684 }
6685 }
6686
6687 if (! low_set)
6688 /* If the first entry is an end-of-list marker, the range
6689 describes an empty scope, i.e. no instructions. */
6690 return 0;
6691
6692 if (low_return)
6693 *low_return = low;
6694 if (high_return)
6695 *high_return = high;
6696 return 1;
6697}
6698
af34e669
DJ
6699/* Get low and high pc attributes from a die. Return 1 if the attributes
6700 are present and valid, otherwise, return 0. Return -1 if the range is
6701 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 6702
c906108c 6703static int
af34e669 6704dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6705 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6706 struct partial_symtab *pst)
c906108c
SS
6707{
6708 struct attribute *attr;
af34e669
DJ
6709 CORE_ADDR low = 0;
6710 CORE_ADDR high = 0;
6711 int ret = 0;
c906108c 6712
e142c38c 6713 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6714 if (attr)
af34e669
DJ
6715 {
6716 high = DW_ADDR (attr);
e142c38c 6717 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6718 if (attr)
6719 low = DW_ADDR (attr);
6720 else
6721 /* Found high w/o low attribute. */
6722 return 0;
6723
6724 /* Found consecutive range of addresses. */
6725 ret = 1;
6726 }
c906108c 6727 else
af34e669 6728 {
e142c38c 6729 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6730 if (attr != NULL)
6731 {
af34e669 6732 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6733 .debug_ranges section. */
d85a05f0 6734 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6735 return 0;
43039443 6736 /* Found discontinuous range of addresses. */
af34e669
DJ
6737 ret = -1;
6738 }
6739 }
c906108c 6740
9373cf26
JK
6741 /* read_partial_die has also the strict LOW < HIGH requirement. */
6742 if (high <= low)
c906108c
SS
6743 return 0;
6744
6745 /* When using the GNU linker, .gnu.linkonce. sections are used to
6746 eliminate duplicate copies of functions and vtables and such.
6747 The linker will arbitrarily choose one and discard the others.
6748 The AT_*_pc values for such functions refer to local labels in
6749 these sections. If the section from that file was discarded, the
6750 labels are not in the output, so the relocs get a value of 0.
6751 If this is a discarded function, mark the pc bounds as invalid,
6752 so that GDB will ignore it. */
72dca2f5 6753 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6754 return 0;
6755
6756 *lowpc = low;
96408a79
SA
6757 if (highpc)
6758 *highpc = high;
af34e669 6759 return ret;
c906108c
SS
6760}
6761
b084d499
JB
6762/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6763 its low and high PC addresses. Do nothing if these addresses could not
6764 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6765 and HIGHPC to the high address if greater than HIGHPC. */
6766
6767static void
6768dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6769 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6770 struct dwarf2_cu *cu)
6771{
6772 CORE_ADDR low, high;
6773 struct die_info *child = die->child;
6774
d85a05f0 6775 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6776 {
6777 *lowpc = min (*lowpc, low);
6778 *highpc = max (*highpc, high);
6779 }
6780
6781 /* If the language does not allow nested subprograms (either inside
6782 subprograms or lexical blocks), we're done. */
6783 if (cu->language != language_ada)
6784 return;
6e70227d 6785
b084d499
JB
6786 /* Check all the children of the given DIE. If it contains nested
6787 subprograms, then check their pc bounds. Likewise, we need to
6788 check lexical blocks as well, as they may also contain subprogram
6789 definitions. */
6790 while (child && child->tag)
6791 {
6792 if (child->tag == DW_TAG_subprogram
6793 || child->tag == DW_TAG_lexical_block)
6794 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6795 child = sibling_die (child);
6796 }
6797}
6798
fae299cd
DC
6799/* Get the low and high pc's represented by the scope DIE, and store
6800 them in *LOWPC and *HIGHPC. If the correct values can't be
6801 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6802
6803static void
6804get_scope_pc_bounds (struct die_info *die,
6805 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6806 struct dwarf2_cu *cu)
6807{
6808 CORE_ADDR best_low = (CORE_ADDR) -1;
6809 CORE_ADDR best_high = (CORE_ADDR) 0;
6810 CORE_ADDR current_low, current_high;
6811
d85a05f0 6812 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6813 {
6814 best_low = current_low;
6815 best_high = current_high;
6816 }
6817 else
6818 {
6819 struct die_info *child = die->child;
6820
6821 while (child && child->tag)
6822 {
6823 switch (child->tag) {
6824 case DW_TAG_subprogram:
b084d499 6825 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6826 break;
6827 case DW_TAG_namespace:
f55ee35c 6828 case DW_TAG_module:
fae299cd
DC
6829 /* FIXME: carlton/2004-01-16: Should we do this for
6830 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6831 that current GCC's always emit the DIEs corresponding
6832 to definitions of methods of classes as children of a
6833 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6834 the DIEs giving the declarations, which could be
6835 anywhere). But I don't see any reason why the
6836 standards says that they have to be there. */
6837 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6838
6839 if (current_low != ((CORE_ADDR) -1))
6840 {
6841 best_low = min (best_low, current_low);
6842 best_high = max (best_high, current_high);
6843 }
6844 break;
6845 default:
0963b4bd 6846 /* Ignore. */
fae299cd
DC
6847 break;
6848 }
6849
6850 child = sibling_die (child);
6851 }
6852 }
6853
6854 *lowpc = best_low;
6855 *highpc = best_high;
6856}
6857
801e3a5b
JB
6858/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6859 in DIE. */
380bca97 6860
801e3a5b
JB
6861static void
6862dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6863 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6864{
bb5ed363 6865 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6866 struct attribute *attr;
6867
6868 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6869 if (attr)
6870 {
6871 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6872
801e3a5b
JB
6873 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6874 if (attr)
6875 {
6876 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6877
801e3a5b
JB
6878 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6879 }
6880 }
6881
6882 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6883 if (attr)
6884 {
bb5ed363 6885 bfd *obfd = objfile->obfd;
801e3a5b
JB
6886
6887 /* The value of the DW_AT_ranges attribute is the offset of the
6888 address range list in the .debug_ranges section. */
6889 unsigned long offset = DW_UNSND (attr);
dce234bc 6890 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6891
6892 /* For some target architectures, but not others, the
6893 read_address function sign-extends the addresses it returns.
6894 To recognize base address selection entries, we need a
6895 mask. */
6896 unsigned int addr_size = cu->header.addr_size;
6897 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6898
6899 /* The base address, to which the next pair is relative. Note
6900 that this 'base' is a DWARF concept: most entries in a range
6901 list are relative, to reduce the number of relocs against the
6902 debugging information. This is separate from this function's
6903 'baseaddr' argument, which GDB uses to relocate debugging
6904 information from a shared library based on the address at
6905 which the library was loaded. */
d00adf39
DE
6906 CORE_ADDR base = cu->base_address;
6907 int base_known = cu->base_known;
801e3a5b 6908
be391dca 6909 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6910 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6911 {
6912 complaint (&symfile_complaints,
6913 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6914 offset);
6915 return;
6916 }
6917
6918 for (;;)
6919 {
6920 unsigned int bytes_read;
6921 CORE_ADDR start, end;
6922
6923 start = read_address (obfd, buffer, cu, &bytes_read);
6924 buffer += bytes_read;
6925 end = read_address (obfd, buffer, cu, &bytes_read);
6926 buffer += bytes_read;
6927
6928 /* Did we find the end of the range list? */
6929 if (start == 0 && end == 0)
6930 break;
6931
6932 /* Did we find a base address selection entry? */
6933 else if ((start & base_select_mask) == base_select_mask)
6934 {
6935 base = end;
6936 base_known = 1;
6937 }
6938
6939 /* We found an ordinary address range. */
6940 else
6941 {
6942 if (!base_known)
6943 {
6944 complaint (&symfile_complaints,
3e43a32a
MS
6945 _("Invalid .debug_ranges data "
6946 "(no base address)"));
801e3a5b
JB
6947 return;
6948 }
6949
9277c30c
UW
6950 if (start > end)
6951 {
6952 /* Inverted range entries are invalid. */
6953 complaint (&symfile_complaints,
6954 _("Invalid .debug_ranges data "
6955 "(inverted range)"));
6956 return;
6957 }
6958
6959 /* Empty range entries have no effect. */
6960 if (start == end)
6961 continue;
6962
6e70227d
DE
6963 record_block_range (block,
6964 baseaddr + base + start,
801e3a5b
JB
6965 baseaddr + base + end - 1);
6966 }
6967 }
6968 }
6969}
6970
60d5a603
JK
6971/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6972 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6973 during 4.6.0 experimental. */
6974
6975static int
6976producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6977{
6978 const char *cs;
6979 int major, minor, release;
ba919b58 6980 int result = 0;
60d5a603
JK
6981
6982 if (cu->producer == NULL)
6983 {
6984 /* For unknown compilers expect their behavior is DWARF version
6985 compliant.
6986
6987 GCC started to support .debug_types sections by -gdwarf-4 since
6988 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6989 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6990 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6991 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6992
6993 return 0;
6994 }
6995
ba919b58
TT
6996 if (cu->checked_producer)
6997 return cu->producer_is_gxx_lt_4_6;
6998
60d5a603
JK
6999 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
7000
7001 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
7002 {
7003 /* For non-GCC compilers expect their behavior is DWARF version
7004 compliant. */
60d5a603 7005 }
ba919b58 7006 else
60d5a603 7007 {
ba919b58
TT
7008 cs = &cu->producer[strlen ("GNU ")];
7009 while (*cs && !isdigit (*cs))
7010 cs++;
7011 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
7012 {
7013 /* Not recognized as GCC. */
7014 }
7015 else
7016 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
7017 }
7018
ba919b58
TT
7019 cu->checked_producer = 1;
7020 cu->producer_is_gxx_lt_4_6 = result;
7021
7022 return result;
60d5a603
JK
7023}
7024
7025/* Return the default accessibility type if it is not overriden by
7026 DW_AT_accessibility. */
7027
7028static enum dwarf_access_attribute
7029dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
7030{
7031 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
7032 {
7033 /* The default DWARF 2 accessibility for members is public, the default
7034 accessibility for inheritance is private. */
7035
7036 if (die->tag != DW_TAG_inheritance)
7037 return DW_ACCESS_public;
7038 else
7039 return DW_ACCESS_private;
7040 }
7041 else
7042 {
7043 /* DWARF 3+ defines the default accessibility a different way. The same
7044 rules apply now for DW_TAG_inheritance as for the members and it only
7045 depends on the container kind. */
7046
7047 if (die->parent->tag == DW_TAG_class_type)
7048 return DW_ACCESS_private;
7049 else
7050 return DW_ACCESS_public;
7051 }
7052}
7053
74ac6d43
TT
7054/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
7055 offset. If the attribute was not found return 0, otherwise return
7056 1. If it was found but could not properly be handled, set *OFFSET
7057 to 0. */
7058
7059static int
7060handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
7061 LONGEST *offset)
7062{
7063 struct attribute *attr;
7064
7065 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
7066 if (attr != NULL)
7067 {
7068 *offset = 0;
7069
7070 /* Note that we do not check for a section offset first here.
7071 This is because DW_AT_data_member_location is new in DWARF 4,
7072 so if we see it, we can assume that a constant form is really
7073 a constant and not a section offset. */
7074 if (attr_form_is_constant (attr))
7075 *offset = dwarf2_get_attr_constant_value (attr, 0);
7076 else if (attr_form_is_section_offset (attr))
7077 dwarf2_complex_location_expr_complaint ();
7078 else if (attr_form_is_block (attr))
7079 *offset = decode_locdesc (DW_BLOCK (attr), cu);
7080 else
7081 dwarf2_complex_location_expr_complaint ();
7082
7083 return 1;
7084 }
7085
7086 return 0;
7087}
7088
c906108c
SS
7089/* Add an aggregate field to the field list. */
7090
7091static void
107d2387 7092dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 7093 struct dwarf2_cu *cu)
6e70227d 7094{
e7c27a73 7095 struct objfile *objfile = cu->objfile;
5e2b427d 7096 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7097 struct nextfield *new_field;
7098 struct attribute *attr;
7099 struct field *fp;
7100 char *fieldname = "";
7101
7102 /* Allocate a new field list entry and link it in. */
7103 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 7104 make_cleanup (xfree, new_field);
c906108c 7105 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7106
7107 if (die->tag == DW_TAG_inheritance)
7108 {
7109 new_field->next = fip->baseclasses;
7110 fip->baseclasses = new_field;
7111 }
7112 else
7113 {
7114 new_field->next = fip->fields;
7115 fip->fields = new_field;
7116 }
c906108c
SS
7117 fip->nfields++;
7118
e142c38c 7119 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7120 if (attr)
7121 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7122 else
7123 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7124 if (new_field->accessibility != DW_ACCESS_public)
7125 fip->non_public_fields = 1;
60d5a603 7126
e142c38c 7127 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7128 if (attr)
7129 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7130 else
7131 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7132
7133 fp = &new_field->field;
a9a9bd0f 7134
e142c38c 7135 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7136 {
74ac6d43
TT
7137 LONGEST offset;
7138
a9a9bd0f 7139 /* Data member other than a C++ static data member. */
6e70227d 7140
c906108c 7141 /* Get type of field. */
e7c27a73 7142 fp->type = die_type (die, cu);
c906108c 7143
d6a843b5 7144 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7145
c906108c 7146 /* Get bit size of field (zero if none). */
e142c38c 7147 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7148 if (attr)
7149 {
7150 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7151 }
7152 else
7153 {
7154 FIELD_BITSIZE (*fp) = 0;
7155 }
7156
7157 /* Get bit offset of field. */
74ac6d43
TT
7158 if (handle_data_member_location (die, cu, &offset))
7159 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7160 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7161 if (attr)
7162 {
5e2b427d 7163 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7164 {
7165 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7166 additional bit offset from the MSB of the containing
7167 anonymous object to the MSB of the field. We don't
7168 have to do anything special since we don't need to
7169 know the size of the anonymous object. */
f41f5e61 7170 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
7171 }
7172 else
7173 {
7174 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7175 MSB of the anonymous object, subtract off the number of
7176 bits from the MSB of the field to the MSB of the
7177 object, and then subtract off the number of bits of
7178 the field itself. The result is the bit offset of
7179 the LSB of the field. */
c906108c
SS
7180 int anonymous_size;
7181 int bit_offset = DW_UNSND (attr);
7182
e142c38c 7183 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7184 if (attr)
7185 {
7186 /* The size of the anonymous object containing
7187 the bit field is explicit, so use the
7188 indicated size (in bytes). */
7189 anonymous_size = DW_UNSND (attr);
7190 }
7191 else
7192 {
7193 /* The size of the anonymous object containing
7194 the bit field must be inferred from the type
7195 attribute of the data member containing the
7196 bit field. */
7197 anonymous_size = TYPE_LENGTH (fp->type);
7198 }
f41f5e61
PA
7199 SET_FIELD_BITPOS (*fp,
7200 (FIELD_BITPOS (*fp)
7201 + anonymous_size * bits_per_byte
7202 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
7203 }
7204 }
7205
7206 /* Get name of field. */
39cbfefa
DJ
7207 fieldname = dwarf2_name (die, cu);
7208 if (fieldname == NULL)
7209 fieldname = "";
d8151005
DJ
7210
7211 /* The name is already allocated along with this objfile, so we don't
7212 need to duplicate it for the type. */
7213 fp->name = fieldname;
c906108c
SS
7214
7215 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7216 pointer or virtual base class pointer) to private. */
e142c38c 7217 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7218 {
d48cc9dd 7219 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7220 new_field->accessibility = DW_ACCESS_private;
7221 fip->non_public_fields = 1;
7222 }
7223 }
a9a9bd0f 7224 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7225 {
a9a9bd0f
DC
7226 /* C++ static member. */
7227
7228 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7229 is a declaration, but all versions of G++ as of this writing
7230 (so through at least 3.2.1) incorrectly generate
7231 DW_TAG_variable tags. */
6e70227d 7232
ff355380 7233 const char *physname;
c906108c 7234
a9a9bd0f 7235 /* Get name of field. */
39cbfefa
DJ
7236 fieldname = dwarf2_name (die, cu);
7237 if (fieldname == NULL)
c906108c
SS
7238 return;
7239
254e6b9e 7240 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7241 if (attr
7242 /* Only create a symbol if this is an external value.
7243 new_symbol checks this and puts the value in the global symbol
7244 table, which we want. If it is not external, new_symbol
7245 will try to put the value in cu->list_in_scope which is wrong. */
7246 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7247 {
7248 /* A static const member, not much different than an enum as far as
7249 we're concerned, except that we can support more types. */
7250 new_symbol (die, NULL, cu);
7251 }
7252
2df3850c 7253 /* Get physical name. */
ff355380 7254 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7255
d8151005
DJ
7256 /* The name is already allocated along with this objfile, so we don't
7257 need to duplicate it for the type. */
7258 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7259 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7260 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7261 }
7262 else if (die->tag == DW_TAG_inheritance)
7263 {
74ac6d43 7264 LONGEST offset;
d4b96c9a 7265
74ac6d43
TT
7266 /* C++ base class field. */
7267 if (handle_data_member_location (die, cu, &offset))
7268 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7269 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7270 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7271 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7272 fip->nbaseclasses++;
7273 }
7274}
7275
98751a41
JK
7276/* Add a typedef defined in the scope of the FIP's class. */
7277
7278static void
7279dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7280 struct dwarf2_cu *cu)
6e70227d 7281{
98751a41 7282 struct objfile *objfile = cu->objfile;
98751a41
JK
7283 struct typedef_field_list *new_field;
7284 struct attribute *attr;
7285 struct typedef_field *fp;
7286 char *fieldname = "";
7287
7288 /* Allocate a new field list entry and link it in. */
7289 new_field = xzalloc (sizeof (*new_field));
7290 make_cleanup (xfree, new_field);
7291
7292 gdb_assert (die->tag == DW_TAG_typedef);
7293
7294 fp = &new_field->field;
7295
7296 /* Get name of field. */
7297 fp->name = dwarf2_name (die, cu);
7298 if (fp->name == NULL)
7299 return;
7300
7301 fp->type = read_type_die (die, cu);
7302
7303 new_field->next = fip->typedef_field_list;
7304 fip->typedef_field_list = new_field;
7305 fip->typedef_field_list_count++;
7306}
7307
c906108c
SS
7308/* Create the vector of fields, and attach it to the type. */
7309
7310static void
fba45db2 7311dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7312 struct dwarf2_cu *cu)
c906108c
SS
7313{
7314 int nfields = fip->nfields;
7315
7316 /* Record the field count, allocate space for the array of fields,
7317 and create blank accessibility bitfields if necessary. */
7318 TYPE_NFIELDS (type) = nfields;
7319 TYPE_FIELDS (type) = (struct field *)
7320 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7321 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7322
b4ba55a1 7323 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7324 {
7325 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7326
7327 TYPE_FIELD_PRIVATE_BITS (type) =
7328 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7329 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7330
7331 TYPE_FIELD_PROTECTED_BITS (type) =
7332 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7333 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7334
774b6a14
TT
7335 TYPE_FIELD_IGNORE_BITS (type) =
7336 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7337 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7338 }
7339
7340 /* If the type has baseclasses, allocate and clear a bit vector for
7341 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7342 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7343 {
7344 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7345 unsigned char *pointer;
c906108c
SS
7346
7347 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7348 pointer = TYPE_ALLOC (type, num_bytes);
7349 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7350 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7351 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7352 }
7353
3e43a32a
MS
7354 /* Copy the saved-up fields into the field vector. Start from the head of
7355 the list, adding to the tail of the field array, so that they end up in
7356 the same order in the array in which they were added to the list. */
c906108c
SS
7357 while (nfields-- > 0)
7358 {
7d0ccb61
DJ
7359 struct nextfield *fieldp;
7360
7361 if (fip->fields)
7362 {
7363 fieldp = fip->fields;
7364 fip->fields = fieldp->next;
7365 }
7366 else
7367 {
7368 fieldp = fip->baseclasses;
7369 fip->baseclasses = fieldp->next;
7370 }
7371
7372 TYPE_FIELD (type, nfields) = fieldp->field;
7373 switch (fieldp->accessibility)
c906108c 7374 {
c5aa993b 7375 case DW_ACCESS_private:
b4ba55a1
JB
7376 if (cu->language != language_ada)
7377 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7378 break;
c906108c 7379
c5aa993b 7380 case DW_ACCESS_protected:
b4ba55a1
JB
7381 if (cu->language != language_ada)
7382 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7383 break;
c906108c 7384
c5aa993b
JM
7385 case DW_ACCESS_public:
7386 break;
c906108c 7387
c5aa993b
JM
7388 default:
7389 /* Unknown accessibility. Complain and treat it as public. */
7390 {
e2e0b3e5 7391 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7392 fieldp->accessibility);
c5aa993b
JM
7393 }
7394 break;
c906108c
SS
7395 }
7396 if (nfields < fip->nbaseclasses)
7397 {
7d0ccb61 7398 switch (fieldp->virtuality)
c906108c 7399 {
c5aa993b
JM
7400 case DW_VIRTUALITY_virtual:
7401 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7402 if (cu->language == language_ada)
a73c6dcd 7403 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7404 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7405 break;
c906108c
SS
7406 }
7407 }
c906108c
SS
7408 }
7409}
7410
c906108c
SS
7411/* Add a member function to the proper fieldlist. */
7412
7413static void
107d2387 7414dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7415 struct type *type, struct dwarf2_cu *cu)
c906108c 7416{
e7c27a73 7417 struct objfile *objfile = cu->objfile;
c906108c
SS
7418 struct attribute *attr;
7419 struct fnfieldlist *flp;
7420 int i;
7421 struct fn_field *fnp;
7422 char *fieldname;
c906108c 7423 struct nextfnfield *new_fnfield;
f792889a 7424 struct type *this_type;
60d5a603 7425 enum dwarf_access_attribute accessibility;
c906108c 7426
b4ba55a1 7427 if (cu->language == language_ada)
a73c6dcd 7428 error (_("unexpected member function in Ada type"));
b4ba55a1 7429
2df3850c 7430 /* Get name of member function. */
39cbfefa
DJ
7431 fieldname = dwarf2_name (die, cu);
7432 if (fieldname == NULL)
2df3850c 7433 return;
c906108c 7434
c906108c
SS
7435 /* Look up member function name in fieldlist. */
7436 for (i = 0; i < fip->nfnfields; i++)
7437 {
27bfe10e 7438 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7439 break;
7440 }
7441
7442 /* Create new list element if necessary. */
7443 if (i < fip->nfnfields)
7444 flp = &fip->fnfieldlists[i];
7445 else
7446 {
7447 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7448 {
7449 fip->fnfieldlists = (struct fnfieldlist *)
7450 xrealloc (fip->fnfieldlists,
7451 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7452 * sizeof (struct fnfieldlist));
c906108c 7453 if (fip->nfnfields == 0)
c13c43fd 7454 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7455 }
7456 flp = &fip->fnfieldlists[fip->nfnfields];
7457 flp->name = fieldname;
7458 flp->length = 0;
7459 flp->head = NULL;
3da10d80 7460 i = fip->nfnfields++;
c906108c
SS
7461 }
7462
7463 /* Create a new member function field and chain it to the field list
0963b4bd 7464 entry. */
c906108c 7465 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7466 make_cleanup (xfree, new_fnfield);
c906108c
SS
7467 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7468 new_fnfield->next = flp->head;
7469 flp->head = new_fnfield;
7470 flp->length++;
7471
7472 /* Fill in the member function field info. */
7473 fnp = &new_fnfield->fnfield;
3da10d80
KS
7474
7475 /* Delay processing of the physname until later. */
7476 if (cu->language == language_cplus || cu->language == language_java)
7477 {
7478 add_to_method_list (type, i, flp->length - 1, fieldname,
7479 die, cu);
7480 }
7481 else
7482 {
1d06ead6 7483 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7484 fnp->physname = physname ? physname : "";
7485 }
7486
c906108c 7487 fnp->type = alloc_type (objfile);
f792889a
DJ
7488 this_type = read_type_die (die, cu);
7489 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7490 {
f792889a 7491 int nparams = TYPE_NFIELDS (this_type);
c906108c 7492
f792889a 7493 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7494 of the method itself (TYPE_CODE_METHOD). */
7495 smash_to_method_type (fnp->type, type,
f792889a
DJ
7496 TYPE_TARGET_TYPE (this_type),
7497 TYPE_FIELDS (this_type),
7498 TYPE_NFIELDS (this_type),
7499 TYPE_VARARGS (this_type));
c906108c
SS
7500
7501 /* Handle static member functions.
c5aa993b 7502 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7503 member functions. G++ helps GDB by marking the first
7504 parameter for non-static member functions (which is the this
7505 pointer) as artificial. We obtain this information from
7506 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7507 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7508 fnp->voffset = VOFFSET_STATIC;
7509 }
7510 else
e2e0b3e5 7511 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7512 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7513
7514 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7515 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7516 fnp->fcontext = die_containing_type (die, cu);
c906108c 7517
3e43a32a
MS
7518 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7519 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7520
7521 /* Get accessibility. */
e142c38c 7522 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7523 if (attr)
60d5a603
JK
7524 accessibility = DW_UNSND (attr);
7525 else
7526 accessibility = dwarf2_default_access_attribute (die, cu);
7527 switch (accessibility)
c906108c 7528 {
60d5a603
JK
7529 case DW_ACCESS_private:
7530 fnp->is_private = 1;
7531 break;
7532 case DW_ACCESS_protected:
7533 fnp->is_protected = 1;
7534 break;
c906108c
SS
7535 }
7536
b02dede2 7537 /* Check for artificial methods. */
e142c38c 7538 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7539 if (attr && DW_UNSND (attr) != 0)
7540 fnp->is_artificial = 1;
7541
0d564a31 7542 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7543 function. For older versions of GCC, this is an offset in the
7544 appropriate virtual table, as specified by DW_AT_containing_type.
7545 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7546 to the object address. */
7547
e142c38c 7548 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7549 if (attr)
8e19ed76 7550 {
aec5aa8b 7551 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7552 {
aec5aa8b
TT
7553 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7554 {
7555 /* Old-style GCC. */
7556 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7557 }
7558 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7559 || (DW_BLOCK (attr)->size > 1
7560 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7561 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7562 {
7563 struct dwarf_block blk;
7564 int offset;
7565
7566 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7567 ? 1 : 2);
7568 blk.size = DW_BLOCK (attr)->size - offset;
7569 blk.data = DW_BLOCK (attr)->data + offset;
7570 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7571 if ((fnp->voffset % cu->header.addr_size) != 0)
7572 dwarf2_complex_location_expr_complaint ();
7573 else
7574 fnp->voffset /= cu->header.addr_size;
7575 fnp->voffset += 2;
7576 }
7577 else
7578 dwarf2_complex_location_expr_complaint ();
7579
7580 if (!fnp->fcontext)
7581 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7582 }
3690dd37 7583 else if (attr_form_is_section_offset (attr))
8e19ed76 7584 {
4d3c2250 7585 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7586 }
7587 else
7588 {
4d3c2250
KB
7589 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7590 fieldname);
8e19ed76 7591 }
0d564a31 7592 }
d48cc9dd
DJ
7593 else
7594 {
7595 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7596 if (attr && DW_UNSND (attr))
7597 {
7598 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7599 complaint (&symfile_complaints,
3e43a32a
MS
7600 _("Member function \"%s\" (offset %d) is virtual "
7601 "but the vtable offset is not specified"),
b64f50a1 7602 fieldname, die->offset.sect_off);
9655fd1a 7603 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7604 TYPE_CPLUS_DYNAMIC (type) = 1;
7605 }
7606 }
c906108c
SS
7607}
7608
7609/* Create the vector of member function fields, and attach it to the type. */
7610
7611static void
fba45db2 7612dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7613 struct dwarf2_cu *cu)
c906108c
SS
7614{
7615 struct fnfieldlist *flp;
c906108c
SS
7616 int i;
7617
b4ba55a1 7618 if (cu->language == language_ada)
a73c6dcd 7619 error (_("unexpected member functions in Ada type"));
b4ba55a1 7620
c906108c
SS
7621 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7622 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7623 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7624
7625 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7626 {
7627 struct nextfnfield *nfp = flp->head;
7628 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7629 int k;
7630
7631 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7632 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7633 fn_flp->fn_fields = (struct fn_field *)
7634 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7635 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7636 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7637 }
7638
7639 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7640}
7641
1168df01
JB
7642/* Returns non-zero if NAME is the name of a vtable member in CU's
7643 language, zero otherwise. */
7644static int
7645is_vtable_name (const char *name, struct dwarf2_cu *cu)
7646{
7647 static const char vptr[] = "_vptr";
987504bb 7648 static const char vtable[] = "vtable";
1168df01 7649
987504bb
JJ
7650 /* Look for the C++ and Java forms of the vtable. */
7651 if ((cu->language == language_java
7652 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7653 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7654 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7655 return 1;
7656
7657 return 0;
7658}
7659
c0dd20ea 7660/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7661 functions, with the ABI-specified layout. If TYPE describes
7662 such a structure, smash it into a member function type.
61049d3b
DJ
7663
7664 GCC shouldn't do this; it should just output pointer to member DIEs.
7665 This is GCC PR debug/28767. */
c0dd20ea 7666
0b92b5bb
TT
7667static void
7668quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7669{
0b92b5bb 7670 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7671
7672 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7673 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7674 return;
c0dd20ea
DJ
7675
7676 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7677 if (TYPE_FIELD_NAME (type, 0) == NULL
7678 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7679 || TYPE_FIELD_NAME (type, 1) == NULL
7680 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7681 return;
c0dd20ea
DJ
7682
7683 /* Find the type of the method. */
0b92b5bb 7684 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7685 if (pfn_type == NULL
7686 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7687 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7688 return;
c0dd20ea
DJ
7689
7690 /* Look for the "this" argument. */
7691 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7692 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7693 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7694 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7695 return;
c0dd20ea
DJ
7696
7697 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7698 new_type = alloc_type (objfile);
7699 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7700 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7701 TYPE_VARARGS (pfn_type));
0b92b5bb 7702 smash_to_methodptr_type (type, new_type);
c0dd20ea 7703}
1168df01 7704
c906108c 7705/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7706 (definition) to create a type for the structure or union. Fill in
7707 the type's name and general properties; the members will not be
7708 processed until process_structure_type.
c906108c 7709
c767944b
DJ
7710 NOTE: we need to call these functions regardless of whether or not the
7711 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7712 structure or union. This gets the type entered into our set of
7713 user defined types.
7714
7715 However, if the structure is incomplete (an opaque struct/union)
7716 then suppress creating a symbol table entry for it since gdb only
7717 wants to find the one with the complete definition. Note that if
7718 it is complete, we just call new_symbol, which does it's own
7719 checking about whether the struct/union is anonymous or not (and
7720 suppresses creating a symbol table entry itself). */
7721
f792889a 7722static struct type *
134d01f1 7723read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7724{
e7c27a73 7725 struct objfile *objfile = cu->objfile;
c906108c
SS
7726 struct type *type;
7727 struct attribute *attr;
39cbfefa 7728 char *name;
c906108c 7729
348e048f
DE
7730 /* If the definition of this type lives in .debug_types, read that type.
7731 Don't follow DW_AT_specification though, that will take us back up
7732 the chain and we want to go down. */
7733 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7734 if (attr)
7735 {
7736 struct dwarf2_cu *type_cu = cu;
7737 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7738
348e048f
DE
7739 /* We could just recurse on read_structure_type, but we need to call
7740 get_die_type to ensure only one type for this DIE is created.
7741 This is important, for example, because for c++ classes we need
7742 TYPE_NAME set which is only done by new_symbol. Blech. */
7743 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7744
7745 /* TYPE_CU may not be the same as CU.
7746 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7747 return set_die_type (die, type, cu);
7748 }
7749
c0dd20ea 7750 type = alloc_type (objfile);
c906108c 7751 INIT_CPLUS_SPECIFIC (type);
93311388 7752
39cbfefa
DJ
7753 name = dwarf2_name (die, cu);
7754 if (name != NULL)
c906108c 7755 {
987504bb
JJ
7756 if (cu->language == language_cplus
7757 || cu->language == language_java)
63d06c5c 7758 {
3da10d80
KS
7759 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7760
7761 /* dwarf2_full_name might have already finished building the DIE's
7762 type. If so, there is no need to continue. */
7763 if (get_die_type (die, cu) != NULL)
7764 return get_die_type (die, cu);
7765
7766 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7767 if (die->tag == DW_TAG_structure_type
7768 || die->tag == DW_TAG_class_type)
7769 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7770 }
7771 else
7772 {
d8151005
DJ
7773 /* The name is already allocated along with this objfile, so
7774 we don't need to duplicate it for the type. */
94af9270
KS
7775 TYPE_TAG_NAME (type) = (char *) name;
7776 if (die->tag == DW_TAG_class_type)
7777 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7778 }
c906108c
SS
7779 }
7780
7781 if (die->tag == DW_TAG_structure_type)
7782 {
7783 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7784 }
7785 else if (die->tag == DW_TAG_union_type)
7786 {
7787 TYPE_CODE (type) = TYPE_CODE_UNION;
7788 }
7789 else
7790 {
c906108c
SS
7791 TYPE_CODE (type) = TYPE_CODE_CLASS;
7792 }
7793
0cc2414c
TT
7794 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7795 TYPE_DECLARED_CLASS (type) = 1;
7796
e142c38c 7797 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7798 if (attr)
7799 {
7800 TYPE_LENGTH (type) = DW_UNSND (attr);
7801 }
7802 else
7803 {
7804 TYPE_LENGTH (type) = 0;
7805 }
7806
876cecd0 7807 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7808 if (die_is_declaration (die, cu))
876cecd0 7809 TYPE_STUB (type) = 1;
a6c727b2
DJ
7810 else if (attr == NULL && die->child == NULL
7811 && producer_is_realview (cu->producer))
7812 /* RealView does not output the required DW_AT_declaration
7813 on incomplete types. */
7814 TYPE_STUB (type) = 1;
dc718098 7815
c906108c
SS
7816 /* We need to add the type field to the die immediately so we don't
7817 infinitely recurse when dealing with pointers to the structure
0963b4bd 7818 type within the structure itself. */
1c379e20 7819 set_die_type (die, type, cu);
c906108c 7820
7e314c57
JK
7821 /* set_die_type should be already done. */
7822 set_descriptive_type (type, die, cu);
7823
c767944b
DJ
7824 return type;
7825}
7826
7827/* Finish creating a structure or union type, including filling in
7828 its members and creating a symbol for it. */
7829
7830static void
7831process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7832{
7833 struct objfile *objfile = cu->objfile;
7834 struct die_info *child_die = die->child;
7835 struct type *type;
7836
7837 type = get_die_type (die, cu);
7838 if (type == NULL)
7839 type = read_structure_type (die, cu);
7840
e142c38c 7841 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7842 {
7843 struct field_info fi;
7844 struct die_info *child_die;
34eaf542 7845 VEC (symbolp) *template_args = NULL;
c767944b 7846 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7847
7848 memset (&fi, 0, sizeof (struct field_info));
7849
639d11d3 7850 child_die = die->child;
c906108c
SS
7851
7852 while (child_die && child_die->tag)
7853 {
a9a9bd0f
DC
7854 if (child_die->tag == DW_TAG_member
7855 || child_die->tag == DW_TAG_variable)
c906108c 7856 {
a9a9bd0f
DC
7857 /* NOTE: carlton/2002-11-05: A C++ static data member
7858 should be a DW_TAG_member that is a declaration, but
7859 all versions of G++ as of this writing (so through at
7860 least 3.2.1) incorrectly generate DW_TAG_variable
7861 tags for them instead. */
e7c27a73 7862 dwarf2_add_field (&fi, child_die, cu);
c906108c 7863 }
8713b1b1 7864 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7865 {
0963b4bd 7866 /* C++ member function. */
e7c27a73 7867 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7868 }
7869 else if (child_die->tag == DW_TAG_inheritance)
7870 {
7871 /* C++ base class field. */
e7c27a73 7872 dwarf2_add_field (&fi, child_die, cu);
c906108c 7873 }
98751a41
JK
7874 else if (child_die->tag == DW_TAG_typedef)
7875 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7876 else if (child_die->tag == DW_TAG_template_type_param
7877 || child_die->tag == DW_TAG_template_value_param)
7878 {
7879 struct symbol *arg = new_symbol (child_die, NULL, cu);
7880
f1078f66
DJ
7881 if (arg != NULL)
7882 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7883 }
7884
c906108c
SS
7885 child_die = sibling_die (child_die);
7886 }
7887
34eaf542
TT
7888 /* Attach template arguments to type. */
7889 if (! VEC_empty (symbolp, template_args))
7890 {
7891 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7892 TYPE_N_TEMPLATE_ARGUMENTS (type)
7893 = VEC_length (symbolp, template_args);
7894 TYPE_TEMPLATE_ARGUMENTS (type)
7895 = obstack_alloc (&objfile->objfile_obstack,
7896 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7897 * sizeof (struct symbol *)));
7898 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7899 VEC_address (symbolp, template_args),
7900 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7901 * sizeof (struct symbol *)));
7902 VEC_free (symbolp, template_args);
7903 }
7904
c906108c
SS
7905 /* Attach fields and member functions to the type. */
7906 if (fi.nfields)
e7c27a73 7907 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7908 if (fi.nfnfields)
7909 {
e7c27a73 7910 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7911
c5aa993b 7912 /* Get the type which refers to the base class (possibly this
c906108c 7913 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7914 class from the DW_AT_containing_type attribute. This use of
7915 DW_AT_containing_type is a GNU extension. */
c906108c 7916
e142c38c 7917 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7918 {
e7c27a73 7919 struct type *t = die_containing_type (die, cu);
c906108c
SS
7920
7921 TYPE_VPTR_BASETYPE (type) = t;
7922 if (type == t)
7923 {
c906108c
SS
7924 int i;
7925
7926 /* Our own class provides vtbl ptr. */
7927 for (i = TYPE_NFIELDS (t) - 1;
7928 i >= TYPE_N_BASECLASSES (t);
7929 --i)
7930 {
0d5cff50 7931 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 7932
1168df01 7933 if (is_vtable_name (fieldname, cu))
c906108c
SS
7934 {
7935 TYPE_VPTR_FIELDNO (type) = i;
7936 break;
7937 }
7938 }
7939
7940 /* Complain if virtual function table field not found. */
7941 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7942 complaint (&symfile_complaints,
3e43a32a
MS
7943 _("virtual function table pointer "
7944 "not found when defining class '%s'"),
4d3c2250
KB
7945 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7946 "");
c906108c
SS
7947 }
7948 else
7949 {
7950 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7951 }
7952 }
f6235d4c
EZ
7953 else if (cu->producer
7954 && strncmp (cu->producer,
7955 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7956 {
7957 /* The IBM XLC compiler does not provide direct indication
7958 of the containing type, but the vtable pointer is
7959 always named __vfp. */
7960
7961 int i;
7962
7963 for (i = TYPE_NFIELDS (type) - 1;
7964 i >= TYPE_N_BASECLASSES (type);
7965 --i)
7966 {
7967 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7968 {
7969 TYPE_VPTR_FIELDNO (type) = i;
7970 TYPE_VPTR_BASETYPE (type) = type;
7971 break;
7972 }
7973 }
7974 }
c906108c 7975 }
98751a41
JK
7976
7977 /* Copy fi.typedef_field_list linked list elements content into the
7978 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7979 if (fi.typedef_field_list)
7980 {
7981 int i = fi.typedef_field_list_count;
7982
a0d7a4ff 7983 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7984 TYPE_TYPEDEF_FIELD_ARRAY (type)
7985 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7986 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7987
7988 /* Reverse the list order to keep the debug info elements order. */
7989 while (--i >= 0)
7990 {
7991 struct typedef_field *dest, *src;
6e70227d 7992
98751a41
JK
7993 dest = &TYPE_TYPEDEF_FIELD (type, i);
7994 src = &fi.typedef_field_list->field;
7995 fi.typedef_field_list = fi.typedef_field_list->next;
7996 *dest = *src;
7997 }
7998 }
c767944b
DJ
7999
8000 do_cleanups (back_to);
eb2a6f42
TT
8001
8002 if (HAVE_CPLUS_STRUCT (type))
8003 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 8004 }
63d06c5c 8005
bb5ed363 8006 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 8007
90aeadfc
DC
8008 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
8009 snapshots) has been known to create a die giving a declaration
8010 for a class that has, as a child, a die giving a definition for a
8011 nested class. So we have to process our children even if the
8012 current die is a declaration. Normally, of course, a declaration
8013 won't have any children at all. */
134d01f1 8014
90aeadfc
DC
8015 while (child_die != NULL && child_die->tag)
8016 {
8017 if (child_die->tag == DW_TAG_member
8018 || child_die->tag == DW_TAG_variable
34eaf542
TT
8019 || child_die->tag == DW_TAG_inheritance
8020 || child_die->tag == DW_TAG_template_value_param
8021 || child_die->tag == DW_TAG_template_type_param)
134d01f1 8022 {
90aeadfc 8023 /* Do nothing. */
134d01f1 8024 }
90aeadfc
DC
8025 else
8026 process_die (child_die, cu);
134d01f1 8027
90aeadfc 8028 child_die = sibling_die (child_die);
134d01f1
DJ
8029 }
8030
fa4028e9
JB
8031 /* Do not consider external references. According to the DWARF standard,
8032 these DIEs are identified by the fact that they have no byte_size
8033 attribute, and a declaration attribute. */
8034 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
8035 || !die_is_declaration (die, cu))
c767944b 8036 new_symbol (die, type, cu);
134d01f1
DJ
8037}
8038
8039/* Given a DW_AT_enumeration_type die, set its type. We do not
8040 complete the type's fields yet, or create any symbols. */
c906108c 8041
f792889a 8042static struct type *
134d01f1 8043read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8044{
e7c27a73 8045 struct objfile *objfile = cu->objfile;
c906108c 8046 struct type *type;
c906108c 8047 struct attribute *attr;
0114d602 8048 const char *name;
134d01f1 8049
348e048f
DE
8050 /* If the definition of this type lives in .debug_types, read that type.
8051 Don't follow DW_AT_specification though, that will take us back up
8052 the chain and we want to go down. */
8053 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8054 if (attr)
8055 {
8056 struct dwarf2_cu *type_cu = cu;
8057 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 8058
348e048f 8059 type = read_type_die (type_die, type_cu);
9dc481d3
DE
8060
8061 /* TYPE_CU may not be the same as CU.
8062 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
8063 return set_die_type (die, type, cu);
8064 }
8065
c906108c
SS
8066 type = alloc_type (objfile);
8067
8068 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 8069 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 8070 if (name != NULL)
0114d602 8071 TYPE_TAG_NAME (type) = (char *) name;
c906108c 8072
e142c38c 8073 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8074 if (attr)
8075 {
8076 TYPE_LENGTH (type) = DW_UNSND (attr);
8077 }
8078 else
8079 {
8080 TYPE_LENGTH (type) = 0;
8081 }
8082
137033e9
JB
8083 /* The enumeration DIE can be incomplete. In Ada, any type can be
8084 declared as private in the package spec, and then defined only
8085 inside the package body. Such types are known as Taft Amendment
8086 Types. When another package uses such a type, an incomplete DIE
8087 may be generated by the compiler. */
02eb380e 8088 if (die_is_declaration (die, cu))
876cecd0 8089 TYPE_STUB (type) = 1;
02eb380e 8090
f792889a 8091 return set_die_type (die, type, cu);
134d01f1
DJ
8092}
8093
8094/* Given a pointer to a die which begins an enumeration, process all
8095 the dies that define the members of the enumeration, and create the
8096 symbol for the enumeration type.
8097
8098 NOTE: We reverse the order of the element list. */
8099
8100static void
8101process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8102{
f792889a 8103 struct type *this_type;
134d01f1 8104
f792889a
DJ
8105 this_type = get_die_type (die, cu);
8106 if (this_type == NULL)
8107 this_type = read_enumeration_type (die, cu);
9dc481d3 8108
639d11d3 8109 if (die->child != NULL)
c906108c 8110 {
9dc481d3
DE
8111 struct die_info *child_die;
8112 struct symbol *sym;
8113 struct field *fields = NULL;
8114 int num_fields = 0;
8115 int unsigned_enum = 1;
8116 char *name;
cafec441
TT
8117 int flag_enum = 1;
8118 ULONGEST mask = 0;
9dc481d3 8119
639d11d3 8120 child_die = die->child;
c906108c
SS
8121 while (child_die && child_die->tag)
8122 {
8123 if (child_die->tag != DW_TAG_enumerator)
8124 {
e7c27a73 8125 process_die (child_die, cu);
c906108c
SS
8126 }
8127 else
8128 {
39cbfefa
DJ
8129 name = dwarf2_name (child_die, cu);
8130 if (name)
c906108c 8131 {
f792889a 8132 sym = new_symbol (child_die, this_type, cu);
c906108c 8133 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
8134 {
8135 unsigned_enum = 0;
8136 flag_enum = 0;
8137 }
8138 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8139 flag_enum = 0;
8140 else
8141 mask |= SYMBOL_VALUE (sym);
c906108c
SS
8142
8143 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8144 {
8145 fields = (struct field *)
8146 xrealloc (fields,
8147 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8148 * sizeof (struct field));
c906108c
SS
8149 }
8150
3567439c 8151 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8152 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 8153 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8154 FIELD_BITSIZE (fields[num_fields]) = 0;
8155
8156 num_fields++;
8157 }
8158 }
8159
8160 child_die = sibling_die (child_die);
8161 }
8162
8163 if (num_fields)
8164 {
f792889a
DJ
8165 TYPE_NFIELDS (this_type) = num_fields;
8166 TYPE_FIELDS (this_type) = (struct field *)
8167 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8168 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8169 sizeof (struct field) * num_fields);
b8c9b27d 8170 xfree (fields);
c906108c
SS
8171 }
8172 if (unsigned_enum)
876cecd0 8173 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
8174 if (flag_enum)
8175 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 8176 }
134d01f1 8177
6c83ed52
TT
8178 /* If we are reading an enum from a .debug_types unit, and the enum
8179 is a declaration, and the enum is not the signatured type in the
8180 unit, then we do not want to add a symbol for it. Adding a
8181 symbol would in some cases obscure the true definition of the
8182 enum, giving users an incomplete type when the definition is
8183 actually available. Note that we do not want to do this for all
8184 enums which are just declarations, because C++0x allows forward
8185 enum declarations. */
b0df02fd 8186 if (cu->per_cu->debug_types_section
6c83ed52
TT
8187 && die_is_declaration (die, cu))
8188 {
52dc124a 8189 struct signatured_type *sig_type;
6c83ed52 8190
52dc124a 8191 sig_type
6c83ed52 8192 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8193 cu->per_cu->debug_types_section,
6c83ed52 8194 cu->per_cu->offset);
52dc124a 8195 if (sig_type->per_cu.offset.sect_off + sig_type->type_offset.cu_off
b64f50a1 8196 != die->offset.sect_off)
6c83ed52
TT
8197 return;
8198 }
8199
f792889a 8200 new_symbol (die, this_type, cu);
c906108c
SS
8201}
8202
8203/* Extract all information from a DW_TAG_array_type DIE and put it in
8204 the DIE's type field. For now, this only handles one dimensional
8205 arrays. */
8206
f792889a 8207static struct type *
e7c27a73 8208read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8209{
e7c27a73 8210 struct objfile *objfile = cu->objfile;
c906108c 8211 struct die_info *child_die;
7e314c57 8212 struct type *type;
c906108c
SS
8213 struct type *element_type, *range_type, *index_type;
8214 struct type **range_types = NULL;
8215 struct attribute *attr;
8216 int ndim = 0;
8217 struct cleanup *back_to;
39cbfefa 8218 char *name;
c906108c 8219
e7c27a73 8220 element_type = die_type (die, cu);
c906108c 8221
7e314c57
JK
8222 /* The die_type call above may have already set the type for this DIE. */
8223 type = get_die_type (die, cu);
8224 if (type)
8225 return type;
8226
c906108c
SS
8227 /* Irix 6.2 native cc creates array types without children for
8228 arrays with unspecified length. */
639d11d3 8229 if (die->child == NULL)
c906108c 8230 {
46bf5051 8231 index_type = objfile_type (objfile)->builtin_int;
c906108c 8232 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8233 type = create_array_type (NULL, element_type, range_type);
8234 return set_die_type (die, type, cu);
c906108c
SS
8235 }
8236
8237 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8238 child_die = die->child;
c906108c
SS
8239 while (child_die && child_die->tag)
8240 {
8241 if (child_die->tag == DW_TAG_subrange_type)
8242 {
f792889a 8243 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8244
f792889a 8245 if (child_type != NULL)
a02abb62 8246 {
0963b4bd
MS
8247 /* The range type was succesfully read. Save it for the
8248 array type creation. */
a02abb62
JB
8249 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8250 {
8251 range_types = (struct type **)
8252 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8253 * sizeof (struct type *));
8254 if (ndim == 0)
8255 make_cleanup (free_current_contents, &range_types);
8256 }
f792889a 8257 range_types[ndim++] = child_type;
a02abb62 8258 }
c906108c
SS
8259 }
8260 child_die = sibling_die (child_die);
8261 }
8262
8263 /* Dwarf2 dimensions are output from left to right, create the
8264 necessary array types in backwards order. */
7ca2d3a3 8265
c906108c 8266 type = element_type;
7ca2d3a3
DL
8267
8268 if (read_array_order (die, cu) == DW_ORD_col_major)
8269 {
8270 int i = 0;
9a619af0 8271
7ca2d3a3
DL
8272 while (i < ndim)
8273 type = create_array_type (NULL, type, range_types[i++]);
8274 }
8275 else
8276 {
8277 while (ndim-- > 0)
8278 type = create_array_type (NULL, type, range_types[ndim]);
8279 }
c906108c 8280
f5f8a009
EZ
8281 /* Understand Dwarf2 support for vector types (like they occur on
8282 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8283 array type. This is not part of the Dwarf2/3 standard yet, but a
8284 custom vendor extension. The main difference between a regular
8285 array and the vector variant is that vectors are passed by value
8286 to functions. */
e142c38c 8287 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8288 if (attr)
ea37ba09 8289 make_vector_type (type);
f5f8a009 8290
dbc98a8b
KW
8291 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8292 implementation may choose to implement triple vectors using this
8293 attribute. */
8294 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8295 if (attr)
8296 {
8297 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8298 TYPE_LENGTH (type) = DW_UNSND (attr);
8299 else
3e43a32a
MS
8300 complaint (&symfile_complaints,
8301 _("DW_AT_byte_size for array type smaller "
8302 "than the total size of elements"));
dbc98a8b
KW
8303 }
8304
39cbfefa
DJ
8305 name = dwarf2_name (die, cu);
8306 if (name)
8307 TYPE_NAME (type) = name;
6e70227d 8308
0963b4bd 8309 /* Install the type in the die. */
7e314c57
JK
8310 set_die_type (die, type, cu);
8311
8312 /* set_die_type should be already done. */
b4ba55a1
JB
8313 set_descriptive_type (type, die, cu);
8314
c906108c
SS
8315 do_cleanups (back_to);
8316
7e314c57 8317 return type;
c906108c
SS
8318}
8319
7ca2d3a3 8320static enum dwarf_array_dim_ordering
6e70227d 8321read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8322{
8323 struct attribute *attr;
8324
8325 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8326
8327 if (attr) return DW_SND (attr);
8328
0963b4bd
MS
8329 /* GNU F77 is a special case, as at 08/2004 array type info is the
8330 opposite order to the dwarf2 specification, but data is still
8331 laid out as per normal fortran.
7ca2d3a3 8332
0963b4bd
MS
8333 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8334 version checking. */
7ca2d3a3 8335
905e0470
PM
8336 if (cu->language == language_fortran
8337 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8338 {
8339 return DW_ORD_row_major;
8340 }
8341
6e70227d 8342 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8343 {
8344 case array_column_major:
8345 return DW_ORD_col_major;
8346 case array_row_major:
8347 default:
8348 return DW_ORD_row_major;
8349 };
8350}
8351
72019c9c 8352/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8353 the DIE's type field. */
72019c9c 8354
f792889a 8355static struct type *
72019c9c
GM
8356read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8357{
7e314c57
JK
8358 struct type *domain_type, *set_type;
8359 struct attribute *attr;
f792889a 8360
7e314c57
JK
8361 domain_type = die_type (die, cu);
8362
8363 /* The die_type call above may have already set the type for this DIE. */
8364 set_type = get_die_type (die, cu);
8365 if (set_type)
8366 return set_type;
8367
8368 set_type = create_set_type (NULL, domain_type);
8369
8370 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8371 if (attr)
8372 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8373
f792889a 8374 return set_die_type (die, set_type, cu);
72019c9c 8375}
7ca2d3a3 8376
c906108c
SS
8377/* First cut: install each common block member as a global variable. */
8378
8379static void
e7c27a73 8380read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8381{
8382 struct die_info *child_die;
8383 struct attribute *attr;
8384 struct symbol *sym;
8385 CORE_ADDR base = (CORE_ADDR) 0;
8386
e142c38c 8387 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8388 if (attr)
8389 {
0963b4bd 8390 /* Support the .debug_loc offsets. */
8e19ed76
PS
8391 if (attr_form_is_block (attr))
8392 {
e7c27a73 8393 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8394 }
3690dd37 8395 else if (attr_form_is_section_offset (attr))
8e19ed76 8396 {
4d3c2250 8397 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8398 }
8399 else
8400 {
4d3c2250
KB
8401 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8402 "common block member");
8e19ed76 8403 }
c906108c 8404 }
639d11d3 8405 if (die->child != NULL)
c906108c 8406 {
639d11d3 8407 child_die = die->child;
c906108c
SS
8408 while (child_die && child_die->tag)
8409 {
74ac6d43
TT
8410 LONGEST offset;
8411
e7c27a73 8412 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8413 if (sym != NULL
8414 && handle_data_member_location (child_die, cu, &offset))
c906108c 8415 {
74ac6d43 8416 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8417 add_symbol_to_list (sym, &global_symbols);
8418 }
8419 child_die = sibling_die (child_die);
8420 }
8421 }
8422}
8423
0114d602 8424/* Create a type for a C++ namespace. */
d9fa45fe 8425
0114d602
DJ
8426static struct type *
8427read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8428{
e7c27a73 8429 struct objfile *objfile = cu->objfile;
0114d602 8430 const char *previous_prefix, *name;
9219021c 8431 int is_anonymous;
0114d602
DJ
8432 struct type *type;
8433
8434 /* For extensions, reuse the type of the original namespace. */
8435 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8436 {
8437 struct die_info *ext_die;
8438 struct dwarf2_cu *ext_cu = cu;
9a619af0 8439
0114d602
DJ
8440 ext_die = dwarf2_extension (die, &ext_cu);
8441 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8442
8443 /* EXT_CU may not be the same as CU.
8444 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8445 return set_die_type (die, type, cu);
8446 }
9219021c 8447
e142c38c 8448 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8449
8450 /* Now build the name of the current namespace. */
8451
0114d602
DJ
8452 previous_prefix = determine_prefix (die, cu);
8453 if (previous_prefix[0] != '\0')
8454 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8455 previous_prefix, name, 0, cu);
0114d602
DJ
8456
8457 /* Create the type. */
8458 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8459 objfile);
8460 TYPE_NAME (type) = (char *) name;
8461 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8462
60531b24 8463 return set_die_type (die, type, cu);
0114d602
DJ
8464}
8465
8466/* Read a C++ namespace. */
8467
8468static void
8469read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8470{
8471 struct objfile *objfile = cu->objfile;
0114d602 8472 int is_anonymous;
9219021c 8473
5c4e30ca
DC
8474 /* Add a symbol associated to this if we haven't seen the namespace
8475 before. Also, add a using directive if it's an anonymous
8476 namespace. */
9219021c 8477
f2f0e013 8478 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8479 {
8480 struct type *type;
8481
0114d602 8482 type = read_type_die (die, cu);
e7c27a73 8483 new_symbol (die, type, cu);
5c4e30ca 8484
e8e80198 8485 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8486 if (is_anonymous)
0114d602
DJ
8487 {
8488 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8489
c0cc3a76 8490 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8491 NULL, NULL, &objfile->objfile_obstack);
0114d602 8492 }
5c4e30ca 8493 }
9219021c 8494
639d11d3 8495 if (die->child != NULL)
d9fa45fe 8496 {
639d11d3 8497 struct die_info *child_die = die->child;
6e70227d 8498
d9fa45fe
DC
8499 while (child_die && child_die->tag)
8500 {
e7c27a73 8501 process_die (child_die, cu);
d9fa45fe
DC
8502 child_die = sibling_die (child_die);
8503 }
8504 }
38d518c9
EZ
8505}
8506
f55ee35c
JK
8507/* Read a Fortran module as type. This DIE can be only a declaration used for
8508 imported module. Still we need that type as local Fortran "use ... only"
8509 declaration imports depend on the created type in determine_prefix. */
8510
8511static struct type *
8512read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8513{
8514 struct objfile *objfile = cu->objfile;
8515 char *module_name;
8516 struct type *type;
8517
8518 module_name = dwarf2_name (die, cu);
8519 if (!module_name)
3e43a32a
MS
8520 complaint (&symfile_complaints,
8521 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 8522 die->offset.sect_off);
f55ee35c
JK
8523 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8524
8525 /* determine_prefix uses TYPE_TAG_NAME. */
8526 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8527
8528 return set_die_type (die, type, cu);
8529}
8530
5d7cb8df
JK
8531/* Read a Fortran module. */
8532
8533static void
8534read_module (struct die_info *die, struct dwarf2_cu *cu)
8535{
8536 struct die_info *child_die = die->child;
8537
5d7cb8df
JK
8538 while (child_die && child_die->tag)
8539 {
8540 process_die (child_die, cu);
8541 child_die = sibling_die (child_die);
8542 }
8543}
8544
38d518c9
EZ
8545/* Return the name of the namespace represented by DIE. Set
8546 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8547 namespace. */
8548
8549static const char *
e142c38c 8550namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8551{
8552 struct die_info *current_die;
8553 const char *name = NULL;
8554
8555 /* Loop through the extensions until we find a name. */
8556
8557 for (current_die = die;
8558 current_die != NULL;
f2f0e013 8559 current_die = dwarf2_extension (die, &cu))
38d518c9 8560 {
e142c38c 8561 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8562 if (name != NULL)
8563 break;
8564 }
8565
8566 /* Is it an anonymous namespace? */
8567
8568 *is_anonymous = (name == NULL);
8569 if (*is_anonymous)
2b1dbab0 8570 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8571
8572 return name;
d9fa45fe
DC
8573}
8574
c906108c
SS
8575/* Extract all information from a DW_TAG_pointer_type DIE and add to
8576 the user defined type vector. */
8577
f792889a 8578static struct type *
e7c27a73 8579read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8580{
5e2b427d 8581 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8582 struct comp_unit_head *cu_header = &cu->header;
c906108c 8583 struct type *type;
8b2dbe47
KB
8584 struct attribute *attr_byte_size;
8585 struct attribute *attr_address_class;
8586 int byte_size, addr_class;
7e314c57
JK
8587 struct type *target_type;
8588
8589 target_type = die_type (die, cu);
c906108c 8590
7e314c57
JK
8591 /* The die_type call above may have already set the type for this DIE. */
8592 type = get_die_type (die, cu);
8593 if (type)
8594 return type;
8595
8596 type = lookup_pointer_type (target_type);
8b2dbe47 8597
e142c38c 8598 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8599 if (attr_byte_size)
8600 byte_size = DW_UNSND (attr_byte_size);
c906108c 8601 else
8b2dbe47
KB
8602 byte_size = cu_header->addr_size;
8603
e142c38c 8604 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8605 if (attr_address_class)
8606 addr_class = DW_UNSND (attr_address_class);
8607 else
8608 addr_class = DW_ADDR_none;
8609
8610 /* If the pointer size or address class is different than the
8611 default, create a type variant marked as such and set the
8612 length accordingly. */
8613 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8614 {
5e2b427d 8615 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8616 {
8617 int type_flags;
8618
849957d9 8619 type_flags = gdbarch_address_class_type_flags
5e2b427d 8620 (gdbarch, byte_size, addr_class);
876cecd0
TT
8621 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8622 == 0);
8b2dbe47
KB
8623 type = make_type_with_address_space (type, type_flags);
8624 }
8625 else if (TYPE_LENGTH (type) != byte_size)
8626 {
3e43a32a
MS
8627 complaint (&symfile_complaints,
8628 _("invalid pointer size %d"), byte_size);
8b2dbe47 8629 }
6e70227d 8630 else
9a619af0
MS
8631 {
8632 /* Should we also complain about unhandled address classes? */
8633 }
c906108c 8634 }
8b2dbe47
KB
8635
8636 TYPE_LENGTH (type) = byte_size;
f792889a 8637 return set_die_type (die, type, cu);
c906108c
SS
8638}
8639
8640/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8641 the user defined type vector. */
8642
f792889a 8643static struct type *
e7c27a73 8644read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8645{
8646 struct type *type;
8647 struct type *to_type;
8648 struct type *domain;
8649
e7c27a73
DJ
8650 to_type = die_type (die, cu);
8651 domain = die_containing_type (die, cu);
0d5de010 8652
7e314c57
JK
8653 /* The calls above may have already set the type for this DIE. */
8654 type = get_die_type (die, cu);
8655 if (type)
8656 return type;
8657
0d5de010
DJ
8658 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8659 type = lookup_methodptr_type (to_type);
8660 else
8661 type = lookup_memberptr_type (to_type, domain);
c906108c 8662
f792889a 8663 return set_die_type (die, type, cu);
c906108c
SS
8664}
8665
8666/* Extract all information from a DW_TAG_reference_type DIE and add to
8667 the user defined type vector. */
8668
f792889a 8669static struct type *
e7c27a73 8670read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8671{
e7c27a73 8672 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8673 struct type *type, *target_type;
c906108c
SS
8674 struct attribute *attr;
8675
7e314c57
JK
8676 target_type = die_type (die, cu);
8677
8678 /* The die_type call above may have already set the type for this DIE. */
8679 type = get_die_type (die, cu);
8680 if (type)
8681 return type;
8682
8683 type = lookup_reference_type (target_type);
e142c38c 8684 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8685 if (attr)
8686 {
8687 TYPE_LENGTH (type) = DW_UNSND (attr);
8688 }
8689 else
8690 {
107d2387 8691 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8692 }
f792889a 8693 return set_die_type (die, type, cu);
c906108c
SS
8694}
8695
f792889a 8696static struct type *
e7c27a73 8697read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8698{
f792889a 8699 struct type *base_type, *cv_type;
c906108c 8700
e7c27a73 8701 base_type = die_type (die, cu);
7e314c57
JK
8702
8703 /* The die_type call above may have already set the type for this DIE. */
8704 cv_type = get_die_type (die, cu);
8705 if (cv_type)
8706 return cv_type;
8707
2f608a3a
KW
8708 /* In case the const qualifier is applied to an array type, the element type
8709 is so qualified, not the array type (section 6.7.3 of C99). */
8710 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8711 {
8712 struct type *el_type, *inner_array;
8713
8714 base_type = copy_type (base_type);
8715 inner_array = base_type;
8716
8717 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8718 {
8719 TYPE_TARGET_TYPE (inner_array) =
8720 copy_type (TYPE_TARGET_TYPE (inner_array));
8721 inner_array = TYPE_TARGET_TYPE (inner_array);
8722 }
8723
8724 el_type = TYPE_TARGET_TYPE (inner_array);
8725 TYPE_TARGET_TYPE (inner_array) =
8726 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8727
8728 return set_die_type (die, base_type, cu);
8729 }
8730
f792889a
DJ
8731 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8732 return set_die_type (die, cv_type, cu);
c906108c
SS
8733}
8734
f792889a 8735static struct type *
e7c27a73 8736read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8737{
f792889a 8738 struct type *base_type, *cv_type;
c906108c 8739
e7c27a73 8740 base_type = die_type (die, cu);
7e314c57
JK
8741
8742 /* The die_type call above may have already set the type for this DIE. */
8743 cv_type = get_die_type (die, cu);
8744 if (cv_type)
8745 return cv_type;
8746
f792889a
DJ
8747 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8748 return set_die_type (die, cv_type, cu);
c906108c
SS
8749}
8750
8751/* Extract all information from a DW_TAG_string_type DIE and add to
8752 the user defined type vector. It isn't really a user defined type,
8753 but it behaves like one, with other DIE's using an AT_user_def_type
8754 attribute to reference it. */
8755
f792889a 8756static struct type *
e7c27a73 8757read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8758{
e7c27a73 8759 struct objfile *objfile = cu->objfile;
3b7538c0 8760 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8761 struct type *type, *range_type, *index_type, *char_type;
8762 struct attribute *attr;
8763 unsigned int length;
8764
e142c38c 8765 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8766 if (attr)
8767 {
8768 length = DW_UNSND (attr);
8769 }
8770 else
8771 {
0963b4bd 8772 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8773 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8774 if (attr)
8775 {
8776 length = DW_UNSND (attr);
8777 }
8778 else
8779 {
8780 length = 1;
8781 }
c906108c 8782 }
6ccb9162 8783
46bf5051 8784 index_type = objfile_type (objfile)->builtin_int;
c906108c 8785 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8786 char_type = language_string_char_type (cu->language_defn, gdbarch);
8787 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8788
f792889a 8789 return set_die_type (die, type, cu);
c906108c
SS
8790}
8791
8792/* Handle DIES due to C code like:
8793
8794 struct foo
c5aa993b
JM
8795 {
8796 int (*funcp)(int a, long l);
8797 int b;
8798 };
c906108c 8799
0963b4bd 8800 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8801
f792889a 8802static struct type *
e7c27a73 8803read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8804{
bb5ed363 8805 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8806 struct type *type; /* Type that this function returns. */
8807 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8808 struct attribute *attr;
8809
e7c27a73 8810 type = die_type (die, cu);
7e314c57
JK
8811
8812 /* The die_type call above may have already set the type for this DIE. */
8813 ftype = get_die_type (die, cu);
8814 if (ftype)
8815 return ftype;
8816
0c8b41f1 8817 ftype = lookup_function_type (type);
c906108c 8818
5b8101ae 8819 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8820 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8821 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8822 || cu->language == language_cplus
5b8101ae
PM
8823 || cu->language == language_java
8824 || cu->language == language_pascal)
876cecd0 8825 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8826 else if (producer_is_realview (cu->producer))
8827 /* RealView does not emit DW_AT_prototyped. We can not
8828 distinguish prototyped and unprototyped functions; default to
8829 prototyped, since that is more common in modern code (and
8830 RealView warns about unprototyped functions). */
8831 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8832
c055b101
CV
8833 /* Store the calling convention in the type if it's available in
8834 the subroutine die. Otherwise set the calling convention to
8835 the default value DW_CC_normal. */
8836 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8837 if (attr)
8838 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8839 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8840 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8841 else
8842 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8843
8844 /* We need to add the subroutine type to the die immediately so
8845 we don't infinitely recurse when dealing with parameters
0963b4bd 8846 declared as the same subroutine type. */
76c10ea2 8847 set_die_type (die, ftype, cu);
6e70227d 8848
639d11d3 8849 if (die->child != NULL)
c906108c 8850 {
bb5ed363 8851 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8852 struct die_info *child_die;
8072405b 8853 int nparams, iparams;
c906108c
SS
8854
8855 /* Count the number of parameters.
8856 FIXME: GDB currently ignores vararg functions, but knows about
8857 vararg member functions. */
8072405b 8858 nparams = 0;
639d11d3 8859 child_die = die->child;
c906108c
SS
8860 while (child_die && child_die->tag)
8861 {
8862 if (child_die->tag == DW_TAG_formal_parameter)
8863 nparams++;
8864 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8865 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8866 child_die = sibling_die (child_die);
8867 }
8868
8869 /* Allocate storage for parameters and fill them in. */
8870 TYPE_NFIELDS (ftype) = nparams;
8871 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8872 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8873
8072405b
JK
8874 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8875 even if we error out during the parameters reading below. */
8876 for (iparams = 0; iparams < nparams; iparams++)
8877 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8878
8879 iparams = 0;
639d11d3 8880 child_die = die->child;
c906108c
SS
8881 while (child_die && child_die->tag)
8882 {
8883 if (child_die->tag == DW_TAG_formal_parameter)
8884 {
3ce3b1ba
PA
8885 struct type *arg_type;
8886
8887 /* DWARF version 2 has no clean way to discern C++
8888 static and non-static member functions. G++ helps
8889 GDB by marking the first parameter for non-static
8890 member functions (which is the this pointer) as
8891 artificial. We pass this information to
8892 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8893
8894 DWARF version 3 added DW_AT_object_pointer, which GCC
8895 4.5 does not yet generate. */
e142c38c 8896 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8897 if (attr)
8898 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8899 else
418835cc
KS
8900 {
8901 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8902
8903 /* GCC/43521: In java, the formal parameter
8904 "this" is sometimes not marked with DW_AT_artificial. */
8905 if (cu->language == language_java)
8906 {
8907 const char *name = dwarf2_name (child_die, cu);
9a619af0 8908
418835cc
KS
8909 if (name && !strcmp (name, "this"))
8910 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8911 }
8912 }
3ce3b1ba
PA
8913 arg_type = die_type (child_die, cu);
8914
8915 /* RealView does not mark THIS as const, which the testsuite
8916 expects. GCC marks THIS as const in method definitions,
8917 but not in the class specifications (GCC PR 43053). */
8918 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8919 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8920 {
8921 int is_this = 0;
8922 struct dwarf2_cu *arg_cu = cu;
8923 const char *name = dwarf2_name (child_die, cu);
8924
8925 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8926 if (attr)
8927 {
8928 /* If the compiler emits this, use it. */
8929 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8930 is_this = 1;
8931 }
8932 else if (name && strcmp (name, "this") == 0)
8933 /* Function definitions will have the argument names. */
8934 is_this = 1;
8935 else if (name == NULL && iparams == 0)
8936 /* Declarations may not have the names, so like
8937 elsewhere in GDB, assume an artificial first
8938 argument is "this". */
8939 is_this = 1;
8940
8941 if (is_this)
8942 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8943 arg_type, 0);
8944 }
8945
8946 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8947 iparams++;
8948 }
8949 child_die = sibling_die (child_die);
8950 }
8951 }
8952
76c10ea2 8953 return ftype;
c906108c
SS
8954}
8955
f792889a 8956static struct type *
e7c27a73 8957read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8958{
e7c27a73 8959 struct objfile *objfile = cu->objfile;
0114d602 8960 const char *name = NULL;
3c8e0968 8961 struct type *this_type, *target_type;
c906108c 8962
94af9270 8963 name = dwarf2_full_name (NULL, die, cu);
f792889a 8964 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8965 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8966 TYPE_NAME (this_type) = (char *) name;
f792889a 8967 set_die_type (die, this_type, cu);
3c8e0968
DE
8968 target_type = die_type (die, cu);
8969 if (target_type != this_type)
8970 TYPE_TARGET_TYPE (this_type) = target_type;
8971 else
8972 {
8973 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8974 spec and cause infinite loops in GDB. */
8975 complaint (&symfile_complaints,
8976 _("Self-referential DW_TAG_typedef "
8977 "- DIE at 0x%x [in module %s]"),
b64f50a1 8978 die->offset.sect_off, objfile->name);
3c8e0968
DE
8979 TYPE_TARGET_TYPE (this_type) = NULL;
8980 }
f792889a 8981 return this_type;
c906108c
SS
8982}
8983
8984/* Find a representation of a given base type and install
8985 it in the TYPE field of the die. */
8986
f792889a 8987static struct type *
e7c27a73 8988read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8989{
e7c27a73 8990 struct objfile *objfile = cu->objfile;
c906108c
SS
8991 struct type *type;
8992 struct attribute *attr;
8993 int encoding = 0, size = 0;
39cbfefa 8994 char *name;
6ccb9162
UW
8995 enum type_code code = TYPE_CODE_INT;
8996 int type_flags = 0;
8997 struct type *target_type = NULL;
c906108c 8998
e142c38c 8999 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
9000 if (attr)
9001 {
9002 encoding = DW_UNSND (attr);
9003 }
e142c38c 9004 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9005 if (attr)
9006 {
9007 size = DW_UNSND (attr);
9008 }
39cbfefa 9009 name = dwarf2_name (die, cu);
6ccb9162 9010 if (!name)
c906108c 9011 {
6ccb9162
UW
9012 complaint (&symfile_complaints,
9013 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 9014 }
6ccb9162
UW
9015
9016 switch (encoding)
c906108c 9017 {
6ccb9162
UW
9018 case DW_ATE_address:
9019 /* Turn DW_ATE_address into a void * pointer. */
9020 code = TYPE_CODE_PTR;
9021 type_flags |= TYPE_FLAG_UNSIGNED;
9022 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
9023 break;
9024 case DW_ATE_boolean:
9025 code = TYPE_CODE_BOOL;
9026 type_flags |= TYPE_FLAG_UNSIGNED;
9027 break;
9028 case DW_ATE_complex_float:
9029 code = TYPE_CODE_COMPLEX;
9030 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
9031 break;
9032 case DW_ATE_decimal_float:
9033 code = TYPE_CODE_DECFLOAT;
9034 break;
9035 case DW_ATE_float:
9036 code = TYPE_CODE_FLT;
9037 break;
9038 case DW_ATE_signed:
9039 break;
9040 case DW_ATE_unsigned:
9041 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
9042 if (cu->language == language_fortran
9043 && name
9044 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
9045 code = TYPE_CODE_CHAR;
6ccb9162
UW
9046 break;
9047 case DW_ATE_signed_char:
6e70227d 9048 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
9049 || cu->language == language_pascal
9050 || cu->language == language_fortran)
6ccb9162
UW
9051 code = TYPE_CODE_CHAR;
9052 break;
9053 case DW_ATE_unsigned_char:
868a0084 9054 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
9055 || cu->language == language_pascal
9056 || cu->language == language_fortran)
6ccb9162
UW
9057 code = TYPE_CODE_CHAR;
9058 type_flags |= TYPE_FLAG_UNSIGNED;
9059 break;
75079b2b
TT
9060 case DW_ATE_UTF:
9061 /* We just treat this as an integer and then recognize the
9062 type by name elsewhere. */
9063 break;
9064
6ccb9162
UW
9065 default:
9066 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
9067 dwarf_type_encoding_name (encoding));
9068 break;
c906108c 9069 }
6ccb9162 9070
0114d602
DJ
9071 type = init_type (code, size, type_flags, NULL, objfile);
9072 TYPE_NAME (type) = name;
6ccb9162
UW
9073 TYPE_TARGET_TYPE (type) = target_type;
9074
0114d602 9075 if (name && strcmp (name, "char") == 0)
876cecd0 9076 TYPE_NOSIGN (type) = 1;
0114d602 9077
f792889a 9078 return set_die_type (die, type, cu);
c906108c
SS
9079}
9080
a02abb62
JB
9081/* Read the given DW_AT_subrange DIE. */
9082
f792889a 9083static struct type *
a02abb62
JB
9084read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
9085{
9086 struct type *base_type;
9087 struct type *range_type;
9088 struct attribute *attr;
43bbcdc2
PH
9089 LONGEST low = 0;
9090 LONGEST high = -1;
39cbfefa 9091 char *name;
43bbcdc2 9092 LONGEST negative_mask;
e77813c8 9093
a02abb62 9094 base_type = die_type (die, cu);
953ac07e
JK
9095 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
9096 check_typedef (base_type);
a02abb62 9097
7e314c57
JK
9098 /* The die_type call above may have already set the type for this DIE. */
9099 range_type = get_die_type (die, cu);
9100 if (range_type)
9101 return range_type;
9102
e142c38c 9103 if (cu->language == language_fortran)
6e70227d 9104 {
a02abb62
JB
9105 /* FORTRAN implies a lower bound of 1, if not given. */
9106 low = 1;
9107 }
9108
dd5e6932
DJ
9109 /* FIXME: For variable sized arrays either of these could be
9110 a variable rather than a constant value. We'll allow it,
9111 but we don't know how to handle it. */
e142c38c 9112 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
9113 if (attr)
9114 low = dwarf2_get_attr_constant_value (attr, 0);
9115
e142c38c 9116 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9117 if (attr)
6e70227d 9118 {
d48323d8 9119 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9120 {
9121 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9122 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9123 FIXME: GDB does not yet know how to handle dynamic
9124 arrays properly, treat them as arrays with unspecified
9125 length for now.
9126
9127 FIXME: jimb/2003-09-22: GDB does not really know
9128 how to handle arrays of unspecified length
9129 either; we just represent them as zero-length
9130 arrays. Choose an appropriate upper bound given
9131 the lower bound we've computed above. */
9132 high = low - 1;
9133 }
9134 else
9135 high = dwarf2_get_attr_constant_value (attr, 1);
9136 }
e77813c8
PM
9137 else
9138 {
9139 attr = dwarf2_attr (die, DW_AT_count, cu);
9140 if (attr)
9141 {
9142 int count = dwarf2_get_attr_constant_value (attr, 1);
9143 high = low + count - 1;
9144 }
c2ff108b
JK
9145 else
9146 {
9147 /* Unspecified array length. */
9148 high = low - 1;
9149 }
e77813c8
PM
9150 }
9151
9152 /* Dwarf-2 specifications explicitly allows to create subrange types
9153 without specifying a base type.
9154 In that case, the base type must be set to the type of
9155 the lower bound, upper bound or count, in that order, if any of these
9156 three attributes references an object that has a type.
9157 If no base type is found, the Dwarf-2 specifications say that
9158 a signed integer type of size equal to the size of an address should
9159 be used.
9160 For the following C code: `extern char gdb_int [];'
9161 GCC produces an empty range DIE.
9162 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9163 high bound or count are not yet handled by this code. */
e77813c8
PM
9164 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9165 {
9166 struct objfile *objfile = cu->objfile;
9167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9168 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9169 struct type *int_type = objfile_type (objfile)->builtin_int;
9170
9171 /* Test "int", "long int", and "long long int" objfile types,
9172 and select the first one having a size above or equal to the
9173 architecture address size. */
9174 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9175 base_type = int_type;
9176 else
9177 {
9178 int_type = objfile_type (objfile)->builtin_long;
9179 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9180 base_type = int_type;
9181 else
9182 {
9183 int_type = objfile_type (objfile)->builtin_long_long;
9184 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9185 base_type = int_type;
9186 }
9187 }
9188 }
a02abb62 9189
6e70227d 9190 negative_mask =
43bbcdc2
PH
9191 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9192 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9193 low |= negative_mask;
9194 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9195 high |= negative_mask;
9196
a02abb62
JB
9197 range_type = create_range_type (NULL, base_type, low, high);
9198
bbb0eef6
JK
9199 /* Mark arrays with dynamic length at least as an array of unspecified
9200 length. GDB could check the boundary but before it gets implemented at
9201 least allow accessing the array elements. */
d48323d8 9202 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9203 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9204
c2ff108b
JK
9205 /* Ada expects an empty array on no boundary attributes. */
9206 if (attr == NULL && cu->language != language_ada)
9207 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9208
39cbfefa
DJ
9209 name = dwarf2_name (die, cu);
9210 if (name)
9211 TYPE_NAME (range_type) = name;
6e70227d 9212
e142c38c 9213 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9214 if (attr)
9215 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9216
7e314c57
JK
9217 set_die_type (die, range_type, cu);
9218
9219 /* set_die_type should be already done. */
b4ba55a1
JB
9220 set_descriptive_type (range_type, die, cu);
9221
7e314c57 9222 return range_type;
a02abb62 9223}
6e70227d 9224
f792889a 9225static struct type *
81a17f79
JB
9226read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9227{
9228 struct type *type;
81a17f79 9229
81a17f79
JB
9230 /* For now, we only support the C meaning of an unspecified type: void. */
9231
0114d602
DJ
9232 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9233 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9234
f792889a 9235 return set_die_type (die, type, cu);
81a17f79 9236}
a02abb62 9237
51545339
DJ
9238/* Trivial hash function for die_info: the hash value of a DIE
9239 is its offset in .debug_info for this objfile. */
9240
9241static hashval_t
9242die_hash (const void *item)
9243{
9244 const struct die_info *die = item;
9a619af0 9245
b64f50a1 9246 return die->offset.sect_off;
51545339
DJ
9247}
9248
9249/* Trivial comparison function for die_info structures: two DIEs
9250 are equal if they have the same offset. */
9251
9252static int
9253die_eq (const void *item_lhs, const void *item_rhs)
9254{
9255 const struct die_info *die_lhs = item_lhs;
9256 const struct die_info *die_rhs = item_rhs;
9a619af0 9257
b64f50a1 9258 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
51545339
DJ
9259}
9260
c906108c
SS
9261/* Read a whole compilation unit into a linked list of dies. */
9262
f9aca02d 9263static struct die_info *
93311388 9264read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9265{
93311388 9266 struct die_reader_specs reader_specs;
98bfdba5 9267 int read_abbrevs = 0;
1d9ec526 9268 struct cleanup *back_to = NULL;
98bfdba5
PA
9269 struct die_info *die;
9270
9271 if (cu->dwarf2_abbrevs == NULL)
9272 {
e5fe5e75 9273 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9274 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9275 read_abbrevs = 1;
9276 }
93311388 9277
348e048f 9278 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9279 cu->die_hash
9280 = htab_create_alloc_ex (cu->header.length / 12,
9281 die_hash,
9282 die_eq,
9283 NULL,
9284 &cu->comp_unit_obstack,
9285 hashtab_obstack_allocate,
9286 dummy_obstack_deallocate);
9287
93311388
DE
9288 init_cu_die_reader (&reader_specs, cu);
9289
98bfdba5
PA
9290 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9291
9292 if (read_abbrevs)
9293 do_cleanups (back_to);
9294
9295 return die;
639d11d3
DC
9296}
9297
d97bc12b
DE
9298/* Main entry point for reading a DIE and all children.
9299 Read the DIE and dump it if requested. */
9300
9301static struct die_info *
93311388
DE
9302read_die_and_children (const struct die_reader_specs *reader,
9303 gdb_byte *info_ptr,
d97bc12b
DE
9304 gdb_byte **new_info_ptr,
9305 struct die_info *parent)
9306{
93311388 9307 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9308 new_info_ptr, parent);
9309
9310 if (dwarf2_die_debug)
9311 {
348e048f
DE
9312 fprintf_unfiltered (gdb_stdlog,
9313 "\nRead die from %s of %s:\n",
b0df02fd 9314 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9315 ? ".debug_types"
9316 : ".debug_info"),
348e048f 9317 reader->abfd->filename);
d97bc12b
DE
9318 dump_die (result, dwarf2_die_debug);
9319 }
9320
9321 return result;
9322}
9323
639d11d3
DC
9324/* Read a single die and all its descendents. Set the die's sibling
9325 field to NULL; set other fields in the die correctly, and set all
9326 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9327 location of the info_ptr after reading all of those dies. PARENT
9328 is the parent of the die in question. */
9329
9330static struct die_info *
93311388
DE
9331read_die_and_children_1 (const struct die_reader_specs *reader,
9332 gdb_byte *info_ptr,
d97bc12b
DE
9333 gdb_byte **new_info_ptr,
9334 struct die_info *parent)
639d11d3
DC
9335{
9336 struct die_info *die;
fe1b8b76 9337 gdb_byte *cur_ptr;
639d11d3
DC
9338 int has_children;
9339
93311388 9340 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9341 if (die == NULL)
9342 {
9343 *new_info_ptr = cur_ptr;
9344 return NULL;
9345 }
93311388 9346 store_in_ref_table (die, reader->cu);
639d11d3
DC
9347
9348 if (has_children)
348e048f 9349 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9350 else
9351 {
9352 die->child = NULL;
9353 *new_info_ptr = cur_ptr;
9354 }
9355
9356 die->sibling = NULL;
9357 die->parent = parent;
9358 return die;
9359}
9360
9361/* Read a die, all of its descendents, and all of its siblings; set
9362 all of the fields of all of the dies correctly. Arguments are as
9363 in read_die_and_children. */
9364
9365static struct die_info *
93311388
DE
9366read_die_and_siblings (const struct die_reader_specs *reader,
9367 gdb_byte *info_ptr,
fe1b8b76 9368 gdb_byte **new_info_ptr,
639d11d3
DC
9369 struct die_info *parent)
9370{
9371 struct die_info *first_die, *last_sibling;
fe1b8b76 9372 gdb_byte *cur_ptr;
639d11d3 9373
c906108c 9374 cur_ptr = info_ptr;
639d11d3
DC
9375 first_die = last_sibling = NULL;
9376
9377 while (1)
c906108c 9378 {
639d11d3 9379 struct die_info *die
93311388 9380 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9381
1d325ec1 9382 if (die == NULL)
c906108c 9383 {
639d11d3
DC
9384 *new_info_ptr = cur_ptr;
9385 return first_die;
c906108c 9386 }
1d325ec1
DJ
9387
9388 if (!first_die)
9389 first_die = die;
c906108c 9390 else
1d325ec1
DJ
9391 last_sibling->sibling = die;
9392
9393 last_sibling = die;
c906108c 9394 }
c906108c
SS
9395}
9396
93311388
DE
9397/* Read the die from the .debug_info section buffer. Set DIEP to
9398 point to a newly allocated die with its information, except for its
9399 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9400 whether the die has children or not. */
9401
9402static gdb_byte *
9403read_full_die (const struct die_reader_specs *reader,
9404 struct die_info **diep, gdb_byte *info_ptr,
9405 int *has_children)
9406{
b64f50a1
JK
9407 unsigned int abbrev_number, bytes_read, i;
9408 sect_offset offset;
93311388
DE
9409 struct abbrev_info *abbrev;
9410 struct die_info *die;
9411 struct dwarf2_cu *cu = reader->cu;
9412 bfd *abfd = reader->abfd;
9413
b64f50a1 9414 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
9415 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9416 info_ptr += bytes_read;
9417 if (!abbrev_number)
9418 {
9419 *diep = NULL;
9420 *has_children = 0;
9421 return info_ptr;
9422 }
9423
9424 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9425 if (!abbrev)
348e048f
DE
9426 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9427 abbrev_number,
9428 bfd_get_filename (abfd));
9429
93311388
DE
9430 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9431 die->offset = offset;
9432 die->tag = abbrev->tag;
9433 die->abbrev = abbrev_number;
9434
9435 die->num_attrs = abbrev->num_attrs;
9436
9437 for (i = 0; i < abbrev->num_attrs; ++i)
9438 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9439 abfd, info_ptr, cu);
9440
9441 *diep = die;
9442 *has_children = abbrev->has_children;
9443 return info_ptr;
9444}
9445
c906108c
SS
9446/* In DWARF version 2, the description of the debugging information is
9447 stored in a separate .debug_abbrev section. Before we read any
9448 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9449 in a hash table. This function also sets flags in CU describing
9450 the data found in the abbrev table. */
c906108c
SS
9451
9452static void
e5fe5e75 9453dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9454{
e5fe5e75 9455 bfd *abfd = cu->objfile->obfd;
e7c27a73 9456 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9457 gdb_byte *abbrev_ptr;
c906108c
SS
9458 struct abbrev_info *cur_abbrev;
9459 unsigned int abbrev_number, bytes_read, abbrev_name;
9460 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9461 struct attr_abbrev *cur_attrs;
9462 unsigned int allocated_attrs;
c906108c 9463
0963b4bd 9464 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9465 obstack_init (&cu->abbrev_obstack);
9466 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9467 (ABBREV_HASH_SIZE
9468 * sizeof (struct abbrev_info *)));
9469 memset (cu->dwarf2_abbrevs, 0,
9470 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9471
be391dca
TT
9472 dwarf2_read_section (dwarf2_per_objfile->objfile,
9473 &dwarf2_per_objfile->abbrev);
b64f50a1
JK
9474 abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9475 + cu_header->abbrev_offset.sect_off);
c906108c
SS
9476 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9477 abbrev_ptr += bytes_read;
9478
f3dd6933
DJ
9479 allocated_attrs = ATTR_ALLOC_CHUNK;
9480 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9481
0963b4bd 9482 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9483 while (abbrev_number)
9484 {
f3dd6933 9485 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9486
9487 /* read in abbrev header */
9488 cur_abbrev->number = abbrev_number;
9489 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9490 abbrev_ptr += bytes_read;
9491 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9492 abbrev_ptr += 1;
9493
9494 /* now read in declarations */
9495 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9496 abbrev_ptr += bytes_read;
9497 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9498 abbrev_ptr += bytes_read;
9499 while (abbrev_name)
9500 {
f3dd6933 9501 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9502 {
f3dd6933
DJ
9503 allocated_attrs += ATTR_ALLOC_CHUNK;
9504 cur_attrs
9505 = xrealloc (cur_attrs, (allocated_attrs
9506 * sizeof (struct attr_abbrev)));
c906108c 9507 }
ae038cb0 9508
f3dd6933
DJ
9509 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9510 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9511 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9512 abbrev_ptr += bytes_read;
9513 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9514 abbrev_ptr += bytes_read;
9515 }
9516
f3dd6933
DJ
9517 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9518 (cur_abbrev->num_attrs
9519 * sizeof (struct attr_abbrev)));
9520 memcpy (cur_abbrev->attrs, cur_attrs,
9521 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9522
c906108c 9523 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9524 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9525 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9526
9527 /* Get next abbreviation.
9528 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9529 always properly terminated with an abbrev number of 0.
9530 Exit loop if we encounter an abbreviation which we have
9531 already read (which means we are about to read the abbreviations
9532 for the next compile unit) or if the end of the abbreviation
9533 table is reached. */
dce234bc
PP
9534 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9535 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9536 break;
9537 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9538 abbrev_ptr += bytes_read;
e7c27a73 9539 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9540 break;
9541 }
f3dd6933
DJ
9542
9543 xfree (cur_attrs);
c906108c
SS
9544}
9545
f3dd6933 9546/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9547
c906108c 9548static void
f3dd6933 9549dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9550{
f3dd6933 9551 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9552
f3dd6933
DJ
9553 obstack_free (&cu->abbrev_obstack, NULL);
9554 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9555}
9556
9557/* Lookup an abbrev_info structure in the abbrev hash table. */
9558
9559static struct abbrev_info *
e7c27a73 9560dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9561{
9562 unsigned int hash_number;
9563 struct abbrev_info *abbrev;
9564
9565 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9566 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9567
9568 while (abbrev)
9569 {
9570 if (abbrev->number == number)
9571 return abbrev;
9572 else
9573 abbrev = abbrev->next;
9574 }
9575 return NULL;
9576}
9577
72bf9492
DJ
9578/* Returns nonzero if TAG represents a type that we might generate a partial
9579 symbol for. */
9580
9581static int
9582is_type_tag_for_partial (int tag)
9583{
9584 switch (tag)
9585 {
9586#if 0
9587 /* Some types that would be reasonable to generate partial symbols for,
9588 that we don't at present. */
9589 case DW_TAG_array_type:
9590 case DW_TAG_file_type:
9591 case DW_TAG_ptr_to_member_type:
9592 case DW_TAG_set_type:
9593 case DW_TAG_string_type:
9594 case DW_TAG_subroutine_type:
9595#endif
9596 case DW_TAG_base_type:
9597 case DW_TAG_class_type:
680b30c7 9598 case DW_TAG_interface_type:
72bf9492
DJ
9599 case DW_TAG_enumeration_type:
9600 case DW_TAG_structure_type:
9601 case DW_TAG_subrange_type:
9602 case DW_TAG_typedef:
9603 case DW_TAG_union_type:
9604 return 1;
9605 default:
9606 return 0;
9607 }
9608}
9609
9610/* Load all DIEs that are interesting for partial symbols into memory. */
9611
9612static struct partial_die_info *
93311388
DE
9613load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9614 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9615{
bb5ed363 9616 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9617 struct partial_die_info *part_die;
9618 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9619 struct abbrev_info *abbrev;
9620 unsigned int bytes_read;
5afb4e99 9621 unsigned int load_all = 0;
72bf9492
DJ
9622 int nesting_level = 1;
9623
9624 parent_die = NULL;
9625 last_die = NULL;
9626
7adf1e79
DE
9627 gdb_assert (cu->per_cu != NULL);
9628 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
9629 load_all = 1;
9630
72bf9492
DJ
9631 cu->partial_dies
9632 = htab_create_alloc_ex (cu->header.length / 12,
9633 partial_die_hash,
9634 partial_die_eq,
9635 NULL,
9636 &cu->comp_unit_obstack,
9637 hashtab_obstack_allocate,
9638 dummy_obstack_deallocate);
9639
9640 part_die = obstack_alloc (&cu->comp_unit_obstack,
9641 sizeof (struct partial_die_info));
9642
9643 while (1)
9644 {
9645 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9646
9647 /* A NULL abbrev means the end of a series of children. */
9648 if (abbrev == NULL)
9649 {
9650 if (--nesting_level == 0)
9651 {
9652 /* PART_DIE was probably the last thing allocated on the
9653 comp_unit_obstack, so we could call obstack_free
9654 here. We don't do that because the waste is small,
9655 and will be cleaned up when we're done with this
9656 compilation unit. This way, we're also more robust
9657 against other users of the comp_unit_obstack. */
9658 return first_die;
9659 }
9660 info_ptr += bytes_read;
9661 last_die = parent_die;
9662 parent_die = parent_die->die_parent;
9663 continue;
9664 }
9665
98bfdba5
PA
9666 /* Check for template arguments. We never save these; if
9667 they're seen, we just mark the parent, and go on our way. */
9668 if (parent_die != NULL
9669 && cu->language == language_cplus
9670 && (abbrev->tag == DW_TAG_template_type_param
9671 || abbrev->tag == DW_TAG_template_value_param))
9672 {
9673 parent_die->has_template_arguments = 1;
9674
9675 if (!load_all)
9676 {
9677 /* We don't need a partial DIE for the template argument. */
9678 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9679 cu);
9680 continue;
9681 }
9682 }
9683
0d99eb77 9684 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
9685 Skip their other children. */
9686 if (!load_all
9687 && cu->language == language_cplus
9688 && parent_die != NULL
9689 && parent_die->tag == DW_TAG_subprogram)
9690 {
9691 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9692 continue;
9693 }
9694
5afb4e99
DJ
9695 /* Check whether this DIE is interesting enough to save. Normally
9696 we would not be interested in members here, but there may be
9697 later variables referencing them via DW_AT_specification (for
9698 static members). */
9699 if (!load_all
9700 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9701 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9702 && abbrev->tag != DW_TAG_enumerator
9703 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9704 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9705 && abbrev->tag != DW_TAG_variable
5afb4e99 9706 && abbrev->tag != DW_TAG_namespace
f55ee35c 9707 && abbrev->tag != DW_TAG_module
5afb4e99 9708 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9709 {
9710 /* Otherwise we skip to the next sibling, if any. */
93311388 9711 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9712 continue;
9713 }
9714
93311388
DE
9715 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9716 buffer, info_ptr, cu);
72bf9492
DJ
9717
9718 /* This two-pass algorithm for processing partial symbols has a
9719 high cost in cache pressure. Thus, handle some simple cases
9720 here which cover the majority of C partial symbols. DIEs
9721 which neither have specification tags in them, nor could have
9722 specification tags elsewhere pointing at them, can simply be
9723 processed and discarded.
9724
9725 This segment is also optional; scan_partial_symbols and
9726 add_partial_symbol will handle these DIEs if we chain
9727 them in normally. When compilers which do not emit large
9728 quantities of duplicate debug information are more common,
9729 this code can probably be removed. */
9730
9731 /* Any complete simple types at the top level (pretty much all
9732 of them, for a language without namespaces), can be processed
9733 directly. */
9734 if (parent_die == NULL
9735 && part_die->has_specification == 0
9736 && part_die->is_declaration == 0
d8228535 9737 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9738 || part_die->tag == DW_TAG_base_type
9739 || part_die->tag == DW_TAG_subrange_type))
9740 {
9741 if (building_psymtab && part_die->name != NULL)
04a679b8 9742 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9743 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9744 &objfile->static_psymbols,
9745 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9746 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9747 continue;
9748 }
9749
d8228535
JK
9750 /* The exception for DW_TAG_typedef with has_children above is
9751 a workaround of GCC PR debug/47510. In the case of this complaint
9752 type_name_no_tag_or_error will error on such types later.
9753
9754 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9755 it could not find the child DIEs referenced later, this is checked
9756 above. In correct DWARF DW_TAG_typedef should have no children. */
9757
9758 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9759 complaint (&symfile_complaints,
9760 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9761 "- DIE at 0x%x [in module %s]"),
b64f50a1 9762 part_die->offset.sect_off, objfile->name);
d8228535 9763
72bf9492
DJ
9764 /* If we're at the second level, and we're an enumerator, and
9765 our parent has no specification (meaning possibly lives in a
9766 namespace elsewhere), then we can add the partial symbol now
9767 instead of queueing it. */
9768 if (part_die->tag == DW_TAG_enumerator
9769 && parent_die != NULL
9770 && parent_die->die_parent == NULL
9771 && parent_die->tag == DW_TAG_enumeration_type
9772 && parent_die->has_specification == 0)
9773 {
9774 if (part_die->name == NULL)
3e43a32a
MS
9775 complaint (&symfile_complaints,
9776 _("malformed enumerator DIE ignored"));
72bf9492 9777 else if (building_psymtab)
04a679b8 9778 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9779 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9780 (cu->language == language_cplus
9781 || cu->language == language_java)
bb5ed363
DE
9782 ? &objfile->global_psymbols
9783 : &objfile->static_psymbols,
9784 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9785
93311388 9786 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9787 continue;
9788 }
9789
9790 /* We'll save this DIE so link it in. */
9791 part_die->die_parent = parent_die;
9792 part_die->die_sibling = NULL;
9793 part_die->die_child = NULL;
9794
9795 if (last_die && last_die == parent_die)
9796 last_die->die_child = part_die;
9797 else if (last_die)
9798 last_die->die_sibling = part_die;
9799
9800 last_die = part_die;
9801
9802 if (first_die == NULL)
9803 first_die = part_die;
9804
9805 /* Maybe add the DIE to the hash table. Not all DIEs that we
9806 find interesting need to be in the hash table, because we
9807 also have the parent/sibling/child chains; only those that we
9808 might refer to by offset later during partial symbol reading.
9809
9810 For now this means things that might have be the target of a
9811 DW_AT_specification, DW_AT_abstract_origin, or
9812 DW_AT_extension. DW_AT_extension will refer only to
9813 namespaces; DW_AT_abstract_origin refers to functions (and
9814 many things under the function DIE, but we do not recurse
9815 into function DIEs during partial symbol reading) and
9816 possibly variables as well; DW_AT_specification refers to
9817 declarations. Declarations ought to have the DW_AT_declaration
9818 flag. It happens that GCC forgets to put it in sometimes, but
9819 only for functions, not for types.
9820
9821 Adding more things than necessary to the hash table is harmless
9822 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9823 wasted time in find_partial_die, when we reread the compilation
9824 unit with load_all_dies set. */
72bf9492 9825
5afb4e99 9826 if (load_all
72929c62 9827 || abbrev->tag == DW_TAG_constant
5afb4e99 9828 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9829 || abbrev->tag == DW_TAG_variable
9830 || abbrev->tag == DW_TAG_namespace
9831 || part_die->is_declaration)
9832 {
9833 void **slot;
9834
9835 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 9836 part_die->offset.sect_off, INSERT);
72bf9492
DJ
9837 *slot = part_die;
9838 }
9839
9840 part_die = obstack_alloc (&cu->comp_unit_obstack,
9841 sizeof (struct partial_die_info));
9842
9843 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9844 we have no reason to follow the children of structures; for other
98bfdba5
PA
9845 languages we have to, so that we can get at method physnames
9846 to infer fully qualified class names, for DW_AT_specification,
9847 and for C++ template arguments. For C++, we also look one level
9848 inside functions to find template arguments (if the name of the
9849 function does not already contain the template arguments).
bc30ff58
JB
9850
9851 For Ada, we need to scan the children of subprograms and lexical
9852 blocks as well because Ada allows the definition of nested
9853 entities that could be interesting for the debugger, such as
9854 nested subprograms for instance. */
72bf9492 9855 if (last_die->has_children
5afb4e99
DJ
9856 && (load_all
9857 || last_die->tag == DW_TAG_namespace
f55ee35c 9858 || last_die->tag == DW_TAG_module
72bf9492 9859 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9860 || (cu->language == language_cplus
9861 && last_die->tag == DW_TAG_subprogram
9862 && (last_die->name == NULL
9863 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9864 || (cu->language != language_c
9865 && (last_die->tag == DW_TAG_class_type
680b30c7 9866 || last_die->tag == DW_TAG_interface_type
72bf9492 9867 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9868 || last_die->tag == DW_TAG_union_type))
9869 || (cu->language == language_ada
9870 && (last_die->tag == DW_TAG_subprogram
9871 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9872 {
9873 nesting_level++;
9874 parent_die = last_die;
9875 continue;
9876 }
9877
9878 /* Otherwise we skip to the next sibling, if any. */
93311388 9879 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9880
9881 /* Back to the top, do it again. */
9882 }
9883}
9884
c906108c
SS
9885/* Read a minimal amount of information into the minimal die structure. */
9886
fe1b8b76 9887static gdb_byte *
72bf9492
DJ
9888read_partial_die (struct partial_die_info *part_die,
9889 struct abbrev_info *abbrev,
9890 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9891 gdb_byte *buffer, gdb_byte *info_ptr,
9892 struct dwarf2_cu *cu)
c906108c 9893{
bb5ed363 9894 struct objfile *objfile = cu->objfile;
fa238c03 9895 unsigned int i;
c906108c 9896 struct attribute attr;
c5aa993b 9897 int has_low_pc_attr = 0;
c906108c
SS
9898 int has_high_pc_attr = 0;
9899
72bf9492 9900 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9901
b64f50a1 9902 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
9903
9904 info_ptr += abbrev_len;
9905
9906 if (abbrev == NULL)
9907 return info_ptr;
9908
c906108c
SS
9909 part_die->tag = abbrev->tag;
9910 part_die->has_children = abbrev->has_children;
c906108c
SS
9911
9912 for (i = 0; i < abbrev->num_attrs; ++i)
9913 {
e7c27a73 9914 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9915
9916 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9917 partial symbol table. */
c906108c
SS
9918 switch (attr.name)
9919 {
9920 case DW_AT_name:
71c25dea
TT
9921 switch (part_die->tag)
9922 {
9923 case DW_TAG_compile_unit:
348e048f 9924 case DW_TAG_type_unit:
71c25dea
TT
9925 /* Compilation units have a DW_AT_name that is a filename, not
9926 a source language identifier. */
9927 case DW_TAG_enumeration_type:
9928 case DW_TAG_enumerator:
9929 /* These tags always have simple identifiers already; no need
9930 to canonicalize them. */
9931 part_die->name = DW_STRING (&attr);
9932 break;
9933 default:
9934 part_die->name
9935 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9936 &objfile->objfile_obstack);
71c25dea
TT
9937 break;
9938 }
c906108c 9939 break;
31ef98ae 9940 case DW_AT_linkage_name:
c906108c 9941 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9942 /* Note that both forms of linkage name might appear. We
9943 assume they will be the same, and we only store the last
9944 one we see. */
94af9270
KS
9945 if (cu->language == language_ada)
9946 part_die->name = DW_STRING (&attr);
abc72ce4 9947 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9948 break;
9949 case DW_AT_low_pc:
9950 has_low_pc_attr = 1;
9951 part_die->lowpc = DW_ADDR (&attr);
9952 break;
9953 case DW_AT_high_pc:
9954 has_high_pc_attr = 1;
9955 part_die->highpc = DW_ADDR (&attr);
9956 break;
9957 case DW_AT_location:
0963b4bd 9958 /* Support the .debug_loc offsets. */
8e19ed76
PS
9959 if (attr_form_is_block (&attr))
9960 {
9961 part_die->locdesc = DW_BLOCK (&attr);
9962 }
3690dd37 9963 else if (attr_form_is_section_offset (&attr))
8e19ed76 9964 {
4d3c2250 9965 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9966 }
9967 else
9968 {
4d3c2250
KB
9969 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9970 "partial symbol information");
8e19ed76 9971 }
c906108c 9972 break;
c906108c
SS
9973 case DW_AT_external:
9974 part_die->is_external = DW_UNSND (&attr);
9975 break;
9976 case DW_AT_declaration:
9977 part_die->is_declaration = DW_UNSND (&attr);
9978 break;
9979 case DW_AT_type:
9980 part_die->has_type = 1;
9981 break;
9982 case DW_AT_abstract_origin:
9983 case DW_AT_specification:
72bf9492
DJ
9984 case DW_AT_extension:
9985 part_die->has_specification = 1;
c764a876 9986 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9987 break;
9988 case DW_AT_sibling:
9989 /* Ignore absolute siblings, they might point outside of
9990 the current compile unit. */
9991 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9992 complaint (&symfile_complaints,
9993 _("ignoring absolute DW_AT_sibling"));
c906108c 9994 else
b64f50a1 9995 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 9996 break;
fa4028e9
JB
9997 case DW_AT_byte_size:
9998 part_die->has_byte_size = 1;
9999 break;
68511cec
CES
10000 case DW_AT_calling_convention:
10001 /* DWARF doesn't provide a way to identify a program's source-level
10002 entry point. DW_AT_calling_convention attributes are only meant
10003 to describe functions' calling conventions.
10004
10005 However, because it's a necessary piece of information in
10006 Fortran, and because DW_CC_program is the only piece of debugging
10007 information whose definition refers to a 'main program' at all,
10008 several compilers have begun marking Fortran main programs with
10009 DW_CC_program --- even when those functions use the standard
10010 calling conventions.
10011
10012 So until DWARF specifies a way to provide this information and
10013 compilers pick up the new representation, we'll support this
10014 practice. */
10015 if (DW_UNSND (&attr) == DW_CC_program
10016 && cu->language == language_fortran)
01f8c46d
JK
10017 {
10018 set_main_name (part_die->name);
10019
10020 /* As this DIE has a static linkage the name would be difficult
10021 to look up later. */
10022 language_of_main = language_fortran;
10023 }
68511cec 10024 break;
481860b3
GB
10025 case DW_AT_inline:
10026 if (DW_UNSND (&attr) == DW_INL_inlined
10027 || DW_UNSND (&attr) == DW_INL_declared_inlined)
10028 part_die->may_be_inlined = 1;
10029 break;
c906108c
SS
10030 default:
10031 break;
10032 }
10033 }
10034
9373cf26
JK
10035 if (has_low_pc_attr && has_high_pc_attr)
10036 {
10037 /* When using the GNU linker, .gnu.linkonce. sections are used to
10038 eliminate duplicate copies of functions and vtables and such.
10039 The linker will arbitrarily choose one and discard the others.
10040 The AT_*_pc values for such functions refer to local labels in
10041 these sections. If the section from that file was discarded, the
10042 labels are not in the output, so the relocs get a value of 0.
10043 If this is a discarded function, mark the pc bounds as invalid,
10044 so that GDB will ignore it. */
10045 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
10046 {
bb5ed363 10047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
10048
10049 complaint (&symfile_complaints,
10050 _("DW_AT_low_pc %s is zero "
10051 "for DIE at 0x%x [in module %s]"),
10052 paddress (gdbarch, part_die->lowpc),
b64f50a1 10053 part_die->offset.sect_off, objfile->name);
9373cf26
JK
10054 }
10055 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
10056 else if (part_die->lowpc >= part_die->highpc)
10057 {
bb5ed363 10058 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
10059
10060 complaint (&symfile_complaints,
10061 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
10062 "for DIE at 0x%x [in module %s]"),
10063 paddress (gdbarch, part_die->lowpc),
10064 paddress (gdbarch, part_die->highpc),
b64f50a1 10065 part_die->offset.sect_off, objfile->name);
9373cf26
JK
10066 }
10067 else
10068 part_die->has_pc_info = 1;
10069 }
85cbf3d3 10070
c906108c
SS
10071 return info_ptr;
10072}
10073
72bf9492
DJ
10074/* Find a cached partial DIE at OFFSET in CU. */
10075
10076static struct partial_die_info *
b64f50a1 10077find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
10078{
10079 struct partial_die_info *lookup_die = NULL;
10080 struct partial_die_info part_die;
10081
10082 part_die.offset = offset;
b64f50a1
JK
10083 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
10084 offset.sect_off);
72bf9492 10085
72bf9492
DJ
10086 return lookup_die;
10087}
10088
348e048f
DE
10089/* Find a partial DIE at OFFSET, which may or may not be in CU,
10090 except in the case of .debug_types DIEs which do not reference
10091 outside their CU (they do however referencing other types via
55f1336d 10092 DW_FORM_ref_sig8). */
72bf9492
DJ
10093
10094static struct partial_die_info *
b64f50a1 10095find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 10096{
bb5ed363 10097 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
10098 struct dwarf2_per_cu_data *per_cu = NULL;
10099 struct partial_die_info *pd = NULL;
72bf9492 10100
45452591 10101 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
10102 {
10103 pd = find_partial_die_in_comp_unit (offset, cu);
10104 if (pd != NULL)
10105 return pd;
0d99eb77
DE
10106 /* We missed recording what we needed.
10107 Load all dies and try again. */
10108 per_cu = cu->per_cu;
5afb4e99 10109 }
0d99eb77
DE
10110 else
10111 {
10112 /* TUs don't reference other CUs/TUs (except via type signatures). */
10113 if (cu->per_cu->debug_types_section)
10114 {
10115 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
10116 " external reference to offset 0x%lx [in module %s].\n"),
10117 (long) cu->header.offset.sect_off, (long) offset.sect_off,
10118 bfd_get_filename (objfile->obfd));
10119 }
10120 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 10121
0d99eb77
DE
10122 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10123 load_partial_comp_unit (per_cu);
ae038cb0 10124
0d99eb77
DE
10125 per_cu->cu->last_used = 0;
10126 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10127 }
5afb4e99
DJ
10128
10129 if (pd == NULL && per_cu->load_all_dies == 0)
10130 {
10131 struct cleanup *back_to;
10132 struct partial_die_info comp_unit_die;
10133 struct abbrev_info *abbrev;
10134 unsigned int bytes_read;
10135 char *info_ptr;
0d99eb77 10136 struct dwarf2_section_info *sec;
5afb4e99
DJ
10137
10138 per_cu->load_all_dies = 1;
10139
0d99eb77
DE
10140 if (per_cu->debug_types_section)
10141 sec = per_cu->debug_types_section;
10142 else
10143 sec = &dwarf2_per_objfile->info;
10144
10145 /* Re-read the DIEs, this time reading all of them.
10146 NOTE: We don't discard the previous set of DIEs.
10147 This doesn't happen very often so it's (hopefully) not a problem. */
5afb4e99
DJ
10148 back_to = make_cleanup (null_cleanup, 0);
10149 if (per_cu->cu->dwarf2_abbrevs == NULL)
10150 {
e5fe5e75 10151 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 10152 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10153 }
0d99eb77 10154 info_ptr = (sec->buffer
b64f50a1
JK
10155 + per_cu->cu->header.offset.sect_off
10156 + per_cu->cu->header.first_die_offset.cu_off);
5afb4e99
DJ
10157 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10158 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
0d99eb77 10159 objfile->obfd, sec->buffer, info_ptr,
5afb4e99
DJ
10160 per_cu->cu);
10161 if (comp_unit_die.has_children)
0d99eb77
DE
10162 load_partial_dies (objfile->obfd, sec->buffer, info_ptr, 0,
10163 per_cu->cu);
5afb4e99
DJ
10164 do_cleanups (back_to);
10165
10166 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10167 }
10168
10169 if (pd == NULL)
10170 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10171 _("could not find partial DIE 0x%x "
10172 "in cache [from module %s]\n"),
b64f50a1 10173 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 10174 return pd;
72bf9492
DJ
10175}
10176
abc72ce4
DE
10177/* See if we can figure out if the class lives in a namespace. We do
10178 this by looking for a member function; its demangled name will
10179 contain namespace info, if there is any. */
10180
10181static void
10182guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10183 struct dwarf2_cu *cu)
10184{
10185 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10186 what template types look like, because the demangler
10187 frequently doesn't give the same name as the debug info. We
10188 could fix this by only using the demangled name to get the
10189 prefix (but see comment in read_structure_type). */
10190
10191 struct partial_die_info *real_pdi;
10192 struct partial_die_info *child_pdi;
10193
10194 /* If this DIE (this DIE's specification, if any) has a parent, then
10195 we should not do this. We'll prepend the parent's fully qualified
10196 name when we create the partial symbol. */
10197
10198 real_pdi = struct_pdi;
10199 while (real_pdi->has_specification)
10200 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10201
10202 if (real_pdi->die_parent != NULL)
10203 return;
10204
10205 for (child_pdi = struct_pdi->die_child;
10206 child_pdi != NULL;
10207 child_pdi = child_pdi->die_sibling)
10208 {
10209 if (child_pdi->tag == DW_TAG_subprogram
10210 && child_pdi->linkage_name != NULL)
10211 {
10212 char *actual_class_name
10213 = language_class_name_from_physname (cu->language_defn,
10214 child_pdi->linkage_name);
10215 if (actual_class_name != NULL)
10216 {
10217 struct_pdi->name
10218 = obsavestring (actual_class_name,
10219 strlen (actual_class_name),
10220 &cu->objfile->objfile_obstack);
10221 xfree (actual_class_name);
10222 }
10223 break;
10224 }
10225 }
10226}
10227
72bf9492
DJ
10228/* Adjust PART_DIE before generating a symbol for it. This function
10229 may set the is_external flag or change the DIE's name. */
10230
10231static void
10232fixup_partial_die (struct partial_die_info *part_die,
10233 struct dwarf2_cu *cu)
10234{
abc72ce4
DE
10235 /* Once we've fixed up a die, there's no point in doing so again.
10236 This also avoids a memory leak if we were to call
10237 guess_partial_die_structure_name multiple times. */
10238 if (part_die->fixup_called)
10239 return;
10240
72bf9492
DJ
10241 /* If we found a reference attribute and the DIE has no name, try
10242 to find a name in the referred to DIE. */
10243
10244 if (part_die->name == NULL && part_die->has_specification)
10245 {
10246 struct partial_die_info *spec_die;
72bf9492 10247
10b3939b 10248 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10249
10b3939b 10250 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10251
10252 if (spec_die->name)
10253 {
10254 part_die->name = spec_die->name;
10255
10256 /* Copy DW_AT_external attribute if it is set. */
10257 if (spec_die->is_external)
10258 part_die->is_external = spec_die->is_external;
10259 }
10260 }
10261
10262 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10263
10264 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10265 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10266
abc72ce4
DE
10267 /* If there is no parent die to provide a namespace, and there are
10268 children, see if we can determine the namespace from their linkage
122d1940 10269 name. */
abc72ce4 10270 if (cu->language == language_cplus
8b70b953 10271 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10272 && part_die->die_parent == NULL
10273 && part_die->has_children
10274 && (part_die->tag == DW_TAG_class_type
10275 || part_die->tag == DW_TAG_structure_type
10276 || part_die->tag == DW_TAG_union_type))
10277 guess_partial_die_structure_name (part_die, cu);
10278
53832f31
TT
10279 /* GCC might emit a nameless struct or union that has a linkage
10280 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10281 if (part_die->name == NULL
96408a79
SA
10282 && (part_die->tag == DW_TAG_class_type
10283 || part_die->tag == DW_TAG_interface_type
10284 || part_die->tag == DW_TAG_structure_type
10285 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10286 && part_die->linkage_name != NULL)
10287 {
10288 char *demangled;
10289
10290 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10291 if (demangled)
10292 {
96408a79
SA
10293 const char *base;
10294
10295 /* Strip any leading namespaces/classes, keep only the base name.
10296 DW_AT_name for named DIEs does not contain the prefixes. */
10297 base = strrchr (demangled, ':');
10298 if (base && base > demangled && base[-1] == ':')
10299 base++;
10300 else
10301 base = demangled;
10302
10303 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10304 &cu->objfile->objfile_obstack);
10305 xfree (demangled);
10306 }
10307 }
10308
abc72ce4 10309 part_die->fixup_called = 1;
72bf9492
DJ
10310}
10311
a8329558 10312/* Read an attribute value described by an attribute form. */
c906108c 10313
fe1b8b76 10314static gdb_byte *
a8329558 10315read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10316 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10317 struct dwarf2_cu *cu)
c906108c 10318{
e7c27a73 10319 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10320 unsigned int bytes_read;
10321 struct dwarf_block *blk;
10322
a8329558
KW
10323 attr->form = form;
10324 switch (form)
c906108c 10325 {
c906108c 10326 case DW_FORM_ref_addr:
ae411497 10327 if (cu->header.version == 2)
4568ecf9 10328 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 10329 else
4568ecf9
DE
10330 DW_UNSND (attr) = read_offset (abfd, info_ptr,
10331 &cu->header, &bytes_read);
ae411497
TT
10332 info_ptr += bytes_read;
10333 break;
10334 case DW_FORM_addr:
e7c27a73 10335 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10336 info_ptr += bytes_read;
c906108c
SS
10337 break;
10338 case DW_FORM_block2:
7b5a2f43 10339 blk = dwarf_alloc_block (cu);
c906108c
SS
10340 blk->size = read_2_bytes (abfd, info_ptr);
10341 info_ptr += 2;
10342 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10343 info_ptr += blk->size;
10344 DW_BLOCK (attr) = blk;
10345 break;
10346 case DW_FORM_block4:
7b5a2f43 10347 blk = dwarf_alloc_block (cu);
c906108c
SS
10348 blk->size = read_4_bytes (abfd, info_ptr);
10349 info_ptr += 4;
10350 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10351 info_ptr += blk->size;
10352 DW_BLOCK (attr) = blk;
10353 break;
10354 case DW_FORM_data2:
10355 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10356 info_ptr += 2;
10357 break;
10358 case DW_FORM_data4:
10359 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10360 info_ptr += 4;
10361 break;
10362 case DW_FORM_data8:
10363 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10364 info_ptr += 8;
10365 break;
2dc7f7b3
TT
10366 case DW_FORM_sec_offset:
10367 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10368 info_ptr += bytes_read;
10369 break;
c906108c 10370 case DW_FORM_string:
9b1c24c8 10371 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10372 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10373 info_ptr += bytes_read;
10374 break;
4bdf3d34
JJ
10375 case DW_FORM_strp:
10376 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10377 &bytes_read);
8285870a 10378 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10379 info_ptr += bytes_read;
10380 break;
2dc7f7b3 10381 case DW_FORM_exprloc:
c906108c 10382 case DW_FORM_block:
7b5a2f43 10383 blk = dwarf_alloc_block (cu);
c906108c
SS
10384 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10385 info_ptr += bytes_read;
10386 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10387 info_ptr += blk->size;
10388 DW_BLOCK (attr) = blk;
10389 break;
10390 case DW_FORM_block1:
7b5a2f43 10391 blk = dwarf_alloc_block (cu);
c906108c
SS
10392 blk->size = read_1_byte (abfd, info_ptr);
10393 info_ptr += 1;
10394 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10395 info_ptr += blk->size;
10396 DW_BLOCK (attr) = blk;
10397 break;
10398 case DW_FORM_data1:
10399 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10400 info_ptr += 1;
10401 break;
10402 case DW_FORM_flag:
10403 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10404 info_ptr += 1;
10405 break;
2dc7f7b3
TT
10406 case DW_FORM_flag_present:
10407 DW_UNSND (attr) = 1;
10408 break;
c906108c
SS
10409 case DW_FORM_sdata:
10410 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10411 info_ptr += bytes_read;
10412 break;
10413 case DW_FORM_udata:
10414 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10415 info_ptr += bytes_read;
10416 break;
10417 case DW_FORM_ref1:
4568ecf9
DE
10418 DW_UNSND (attr) = (cu->header.offset.sect_off
10419 + read_1_byte (abfd, info_ptr));
c906108c
SS
10420 info_ptr += 1;
10421 break;
10422 case DW_FORM_ref2:
4568ecf9
DE
10423 DW_UNSND (attr) = (cu->header.offset.sect_off
10424 + read_2_bytes (abfd, info_ptr));
c906108c
SS
10425 info_ptr += 2;
10426 break;
10427 case DW_FORM_ref4:
4568ecf9
DE
10428 DW_UNSND (attr) = (cu->header.offset.sect_off
10429 + read_4_bytes (abfd, info_ptr));
c906108c
SS
10430 info_ptr += 4;
10431 break;
613e1657 10432 case DW_FORM_ref8:
4568ecf9
DE
10433 DW_UNSND (attr) = (cu->header.offset.sect_off
10434 + read_8_bytes (abfd, info_ptr));
613e1657
KB
10435 info_ptr += 8;
10436 break;
55f1336d 10437 case DW_FORM_ref_sig8:
348e048f
DE
10438 /* Convert the signature to something we can record in DW_UNSND
10439 for later lookup.
10440 NOTE: This is NULL if the type wasn't found. */
10441 DW_SIGNATURED_TYPE (attr) =
e319fa28 10442 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
10443 info_ptr += 8;
10444 break;
c906108c 10445 case DW_FORM_ref_udata:
4568ecf9
DE
10446 DW_UNSND (attr) = (cu->header.offset.sect_off
10447 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10448 info_ptr += bytes_read;
10449 break;
c906108c 10450 case DW_FORM_indirect:
a8329558
KW
10451 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10452 info_ptr += bytes_read;
e7c27a73 10453 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10454 break;
c906108c 10455 default:
8a3fe4f8 10456 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10457 dwarf_form_name (form),
10458 bfd_get_filename (abfd));
c906108c 10459 }
28e94949
JB
10460
10461 /* We have seen instances where the compiler tried to emit a byte
10462 size attribute of -1 which ended up being encoded as an unsigned
10463 0xffffffff. Although 0xffffffff is technically a valid size value,
10464 an object of this size seems pretty unlikely so we can relatively
10465 safely treat these cases as if the size attribute was invalid and
10466 treat them as zero by default. */
10467 if (attr->name == DW_AT_byte_size
10468 && form == DW_FORM_data4
10469 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10470 {
10471 complaint
10472 (&symfile_complaints,
43bbcdc2
PH
10473 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10474 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10475 DW_UNSND (attr) = 0;
10476 }
28e94949 10477
c906108c
SS
10478 return info_ptr;
10479}
10480
a8329558
KW
10481/* Read an attribute described by an abbreviated attribute. */
10482
fe1b8b76 10483static gdb_byte *
a8329558 10484read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10485 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10486{
10487 attr->name = abbrev->name;
e7c27a73 10488 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10489}
10490
0963b4bd 10491/* Read dwarf information from a buffer. */
c906108c
SS
10492
10493static unsigned int
fe1b8b76 10494read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10495{
fe1b8b76 10496 return bfd_get_8 (abfd, buf);
c906108c
SS
10497}
10498
10499static int
fe1b8b76 10500read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10501{
fe1b8b76 10502 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10503}
10504
10505static unsigned int
fe1b8b76 10506read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10507{
fe1b8b76 10508 return bfd_get_16 (abfd, buf);
c906108c
SS
10509}
10510
21ae7a4d
JK
10511static int
10512read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10513{
10514 return bfd_get_signed_16 (abfd, buf);
10515}
10516
c906108c 10517static unsigned int
fe1b8b76 10518read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10519{
fe1b8b76 10520 return bfd_get_32 (abfd, buf);
c906108c
SS
10521}
10522
21ae7a4d
JK
10523static int
10524read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10525{
10526 return bfd_get_signed_32 (abfd, buf);
10527}
10528
93311388 10529static ULONGEST
fe1b8b76 10530read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10531{
fe1b8b76 10532 return bfd_get_64 (abfd, buf);
c906108c
SS
10533}
10534
10535static CORE_ADDR
fe1b8b76 10536read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10537 unsigned int *bytes_read)
c906108c 10538{
e7c27a73 10539 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10540 CORE_ADDR retval = 0;
10541
107d2387 10542 if (cu_header->signed_addr_p)
c906108c 10543 {
107d2387
AC
10544 switch (cu_header->addr_size)
10545 {
10546 case 2:
fe1b8b76 10547 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10548 break;
10549 case 4:
fe1b8b76 10550 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10551 break;
10552 case 8:
fe1b8b76 10553 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10554 break;
10555 default:
8e65ff28 10556 internal_error (__FILE__, __LINE__,
e2e0b3e5 10557 _("read_address: bad switch, signed [in module %s]"),
659b0389 10558 bfd_get_filename (abfd));
107d2387
AC
10559 }
10560 }
10561 else
10562 {
10563 switch (cu_header->addr_size)
10564 {
10565 case 2:
fe1b8b76 10566 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10567 break;
10568 case 4:
fe1b8b76 10569 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10570 break;
10571 case 8:
fe1b8b76 10572 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10573 break;
10574 default:
8e65ff28 10575 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10576 _("read_address: bad switch, "
10577 "unsigned [in module %s]"),
659b0389 10578 bfd_get_filename (abfd));
107d2387 10579 }
c906108c 10580 }
64367e0a 10581
107d2387
AC
10582 *bytes_read = cu_header->addr_size;
10583 return retval;
c906108c
SS
10584}
10585
f7ef9339 10586/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10587 specification allows the initial length to take up either 4 bytes
10588 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10589 bytes describe the length and all offsets will be 8 bytes in length
10590 instead of 4.
10591
f7ef9339
KB
10592 An older, non-standard 64-bit format is also handled by this
10593 function. The older format in question stores the initial length
10594 as an 8-byte quantity without an escape value. Lengths greater
10595 than 2^32 aren't very common which means that the initial 4 bytes
10596 is almost always zero. Since a length value of zero doesn't make
10597 sense for the 32-bit format, this initial zero can be considered to
10598 be an escape value which indicates the presence of the older 64-bit
10599 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10600 greater than 4GB. If it becomes necessary to handle lengths
10601 somewhat larger than 4GB, we could allow other small values (such
10602 as the non-sensical values of 1, 2, and 3) to also be used as
10603 escape values indicating the presence of the old format.
f7ef9339 10604
917c78fc
MK
10605 The value returned via bytes_read should be used to increment the
10606 relevant pointer after calling read_initial_length().
c764a876 10607
613e1657
KB
10608 [ Note: read_initial_length() and read_offset() are based on the
10609 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10610 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10611 from:
10612
f7ef9339 10613 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10614
613e1657
KB
10615 This document is only a draft and is subject to change. (So beware.)
10616
f7ef9339 10617 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10618 determined empirically by examining 64-bit ELF files produced by
10619 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10620
10621 - Kevin, July 16, 2002
613e1657
KB
10622 ] */
10623
10624static LONGEST
c764a876 10625read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10626{
fe1b8b76 10627 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10628
dd373385 10629 if (length == 0xffffffff)
613e1657 10630 {
fe1b8b76 10631 length = bfd_get_64 (abfd, buf + 4);
613e1657 10632 *bytes_read = 12;
613e1657 10633 }
dd373385 10634 else if (length == 0)
f7ef9339 10635 {
dd373385 10636 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10637 length = bfd_get_64 (abfd, buf);
f7ef9339 10638 *bytes_read = 8;
f7ef9339 10639 }
613e1657
KB
10640 else
10641 {
10642 *bytes_read = 4;
613e1657
KB
10643 }
10644
c764a876
DE
10645 return length;
10646}
dd373385 10647
c764a876
DE
10648/* Cover function for read_initial_length.
10649 Returns the length of the object at BUF, and stores the size of the
10650 initial length in *BYTES_READ and stores the size that offsets will be in
10651 *OFFSET_SIZE.
10652 If the initial length size is not equivalent to that specified in
10653 CU_HEADER then issue a complaint.
10654 This is useful when reading non-comp-unit headers. */
dd373385 10655
c764a876
DE
10656static LONGEST
10657read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10658 const struct comp_unit_head *cu_header,
10659 unsigned int *bytes_read,
10660 unsigned int *offset_size)
10661{
10662 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10663
10664 gdb_assert (cu_header->initial_length_size == 4
10665 || cu_header->initial_length_size == 8
10666 || cu_header->initial_length_size == 12);
10667
10668 if (cu_header->initial_length_size != *bytes_read)
10669 complaint (&symfile_complaints,
10670 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10671
c764a876 10672 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10673 return length;
613e1657
KB
10674}
10675
10676/* Read an offset from the data stream. The size of the offset is
917c78fc 10677 given by cu_header->offset_size. */
613e1657
KB
10678
10679static LONGEST
fe1b8b76 10680read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10681 unsigned int *bytes_read)
c764a876
DE
10682{
10683 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10684
c764a876
DE
10685 *bytes_read = cu_header->offset_size;
10686 return offset;
10687}
10688
10689/* Read an offset from the data stream. */
10690
10691static LONGEST
10692read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10693{
10694 LONGEST retval = 0;
10695
c764a876 10696 switch (offset_size)
613e1657
KB
10697 {
10698 case 4:
fe1b8b76 10699 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10700 break;
10701 case 8:
fe1b8b76 10702 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10703 break;
10704 default:
8e65ff28 10705 internal_error (__FILE__, __LINE__,
c764a876 10706 _("read_offset_1: bad switch [in module %s]"),
659b0389 10707 bfd_get_filename (abfd));
613e1657
KB
10708 }
10709
917c78fc 10710 return retval;
613e1657
KB
10711}
10712
fe1b8b76
JB
10713static gdb_byte *
10714read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10715{
10716 /* If the size of a host char is 8 bits, we can return a pointer
10717 to the buffer, otherwise we have to copy the data to a buffer
10718 allocated on the temporary obstack. */
4bdf3d34 10719 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10720 return buf;
c906108c
SS
10721}
10722
10723static char *
9b1c24c8 10724read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10725{
10726 /* If the size of a host char is 8 bits, we can return a pointer
10727 to the string, otherwise we have to copy the string to a buffer
10728 allocated on the temporary obstack. */
4bdf3d34 10729 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10730 if (*buf == '\0')
10731 {
10732 *bytes_read_ptr = 1;
10733 return NULL;
10734 }
fe1b8b76
JB
10735 *bytes_read_ptr = strlen ((char *) buf) + 1;
10736 return (char *) buf;
4bdf3d34
JJ
10737}
10738
10739static char *
cf2c3c16 10740read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10741{
be391dca 10742 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10743 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10744 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10745 bfd_get_filename (abfd));
dce234bc 10746 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10747 error (_("DW_FORM_strp pointing outside of "
10748 ".debug_str section [in module %s]"),
10749 bfd_get_filename (abfd));
4bdf3d34 10750 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10751 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10752 return NULL;
dce234bc 10753 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10754}
10755
cf2c3c16
TT
10756static char *
10757read_indirect_string (bfd *abfd, gdb_byte *buf,
10758 const struct comp_unit_head *cu_header,
10759 unsigned int *bytes_read_ptr)
10760{
10761 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10762
10763 return read_indirect_string_at_offset (abfd, str_offset);
10764}
10765
12df843f 10766static ULONGEST
fe1b8b76 10767read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10768{
12df843f 10769 ULONGEST result;
ce5d95e1 10770 unsigned int num_read;
c906108c
SS
10771 int i, shift;
10772 unsigned char byte;
10773
10774 result = 0;
10775 shift = 0;
10776 num_read = 0;
10777 i = 0;
10778 while (1)
10779 {
fe1b8b76 10780 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10781 buf++;
10782 num_read++;
12df843f 10783 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
10784 if ((byte & 128) == 0)
10785 {
10786 break;
10787 }
10788 shift += 7;
10789 }
10790 *bytes_read_ptr = num_read;
10791 return result;
10792}
10793
12df843f 10794static LONGEST
fe1b8b76 10795read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10796{
12df843f 10797 LONGEST result;
77e0b926 10798 int i, shift, num_read;
c906108c
SS
10799 unsigned char byte;
10800
10801 result = 0;
10802 shift = 0;
c906108c
SS
10803 num_read = 0;
10804 i = 0;
10805 while (1)
10806 {
fe1b8b76 10807 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10808 buf++;
10809 num_read++;
12df843f 10810 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
10811 shift += 7;
10812 if ((byte & 128) == 0)
10813 {
10814 break;
10815 }
10816 }
77e0b926 10817 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 10818 result |= -(((LONGEST) 1) << shift);
c906108c
SS
10819 *bytes_read_ptr = num_read;
10820 return result;
10821}
10822
4bb7a0a7
DJ
10823/* Return a pointer to just past the end of an LEB128 number in BUF. */
10824
fe1b8b76
JB
10825static gdb_byte *
10826skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10827{
10828 int byte;
10829
10830 while (1)
10831 {
fe1b8b76 10832 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10833 buf++;
10834 if ((byte & 128) == 0)
10835 return buf;
10836 }
10837}
10838
c906108c 10839static void
e142c38c 10840set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10841{
10842 switch (lang)
10843 {
10844 case DW_LANG_C89:
76bee0cc 10845 case DW_LANG_C99:
c906108c 10846 case DW_LANG_C:
e142c38c 10847 cu->language = language_c;
c906108c
SS
10848 break;
10849 case DW_LANG_C_plus_plus:
e142c38c 10850 cu->language = language_cplus;
c906108c 10851 break;
6aecb9c2
JB
10852 case DW_LANG_D:
10853 cu->language = language_d;
10854 break;
c906108c
SS
10855 case DW_LANG_Fortran77:
10856 case DW_LANG_Fortran90:
b21b22e0 10857 case DW_LANG_Fortran95:
e142c38c 10858 cu->language = language_fortran;
c906108c 10859 break;
a766d390
DE
10860 case DW_LANG_Go:
10861 cu->language = language_go;
10862 break;
c906108c 10863 case DW_LANG_Mips_Assembler:
e142c38c 10864 cu->language = language_asm;
c906108c 10865 break;
bebd888e 10866 case DW_LANG_Java:
e142c38c 10867 cu->language = language_java;
bebd888e 10868 break;
c906108c 10869 case DW_LANG_Ada83:
8aaf0b47 10870 case DW_LANG_Ada95:
bc5f45f8
JB
10871 cu->language = language_ada;
10872 break;
72019c9c
GM
10873 case DW_LANG_Modula2:
10874 cu->language = language_m2;
10875 break;
fe8e67fd
PM
10876 case DW_LANG_Pascal83:
10877 cu->language = language_pascal;
10878 break;
22566fbd
DJ
10879 case DW_LANG_ObjC:
10880 cu->language = language_objc;
10881 break;
c906108c
SS
10882 case DW_LANG_Cobol74:
10883 case DW_LANG_Cobol85:
c906108c 10884 default:
e142c38c 10885 cu->language = language_minimal;
c906108c
SS
10886 break;
10887 }
e142c38c 10888 cu->language_defn = language_def (cu->language);
c906108c
SS
10889}
10890
10891/* Return the named attribute or NULL if not there. */
10892
10893static struct attribute *
e142c38c 10894dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 10895{
a48e046c 10896 for (;;)
c906108c 10897 {
a48e046c
TT
10898 unsigned int i;
10899 struct attribute *spec = NULL;
10900
10901 for (i = 0; i < die->num_attrs; ++i)
10902 {
10903 if (die->attrs[i].name == name)
10904 return &die->attrs[i];
10905 if (die->attrs[i].name == DW_AT_specification
10906 || die->attrs[i].name == DW_AT_abstract_origin)
10907 spec = &die->attrs[i];
10908 }
10909
10910 if (!spec)
10911 break;
c906108c 10912
f2f0e013 10913 die = follow_die_ref (die, spec, &cu);
f2f0e013 10914 }
c5aa993b 10915
c906108c
SS
10916 return NULL;
10917}
10918
348e048f
DE
10919/* Return the named attribute or NULL if not there,
10920 but do not follow DW_AT_specification, etc.
10921 This is for use in contexts where we're reading .debug_types dies.
10922 Following DW_AT_specification, DW_AT_abstract_origin will take us
10923 back up the chain, and we want to go down. */
10924
10925static struct attribute *
10926dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10927 struct dwarf2_cu *cu)
10928{
10929 unsigned int i;
10930
10931 for (i = 0; i < die->num_attrs; ++i)
10932 if (die->attrs[i].name == name)
10933 return &die->attrs[i];
10934
10935 return NULL;
10936}
10937
05cf31d1
JB
10938/* Return non-zero iff the attribute NAME is defined for the given DIE,
10939 and holds a non-zero value. This function should only be used for
2dc7f7b3 10940 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10941
10942static int
10943dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10944{
10945 struct attribute *attr = dwarf2_attr (die, name, cu);
10946
10947 return (attr && DW_UNSND (attr));
10948}
10949
3ca72b44 10950static int
e142c38c 10951die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10952{
05cf31d1
JB
10953 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10954 which value is non-zero. However, we have to be careful with
10955 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10956 (via dwarf2_flag_true_p) follows this attribute. So we may
10957 end up accidently finding a declaration attribute that belongs
10958 to a different DIE referenced by the specification attribute,
10959 even though the given DIE does not have a declaration attribute. */
10960 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10961 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10962}
10963
63d06c5c 10964/* Return the die giving the specification for DIE, if there is
f2f0e013 10965 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10966 containing the return value on output. If there is no
10967 specification, but there is an abstract origin, that is
10968 returned. */
63d06c5c
DC
10969
10970static struct die_info *
f2f0e013 10971die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10972{
f2f0e013
DJ
10973 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10974 *spec_cu);
63d06c5c 10975
edb3359d
DJ
10976 if (spec_attr == NULL)
10977 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10978
63d06c5c
DC
10979 if (spec_attr == NULL)
10980 return NULL;
10981 else
f2f0e013 10982 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10983}
c906108c 10984
debd256d 10985/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10986 refers to.
10987 NOTE: This is also used as a "cleanup" function. */
10988
debd256d
JB
10989static void
10990free_line_header (struct line_header *lh)
10991{
10992 if (lh->standard_opcode_lengths)
a8bc7b56 10993 xfree (lh->standard_opcode_lengths);
debd256d
JB
10994
10995 /* Remember that all the lh->file_names[i].name pointers are
10996 pointers into debug_line_buffer, and don't need to be freed. */
10997 if (lh->file_names)
a8bc7b56 10998 xfree (lh->file_names);
debd256d
JB
10999
11000 /* Similarly for the include directory names. */
11001 if (lh->include_dirs)
a8bc7b56 11002 xfree (lh->include_dirs);
debd256d 11003
a8bc7b56 11004 xfree (lh);
debd256d
JB
11005}
11006
debd256d 11007/* Add an entry to LH's include directory table. */
ae2de4f8 11008
debd256d
JB
11009static void
11010add_include_dir (struct line_header *lh, char *include_dir)
c906108c 11011{
debd256d
JB
11012 /* Grow the array if necessary. */
11013 if (lh->include_dirs_size == 0)
c5aa993b 11014 {
debd256d
JB
11015 lh->include_dirs_size = 1; /* for testing */
11016 lh->include_dirs = xmalloc (lh->include_dirs_size
11017 * sizeof (*lh->include_dirs));
11018 }
11019 else if (lh->num_include_dirs >= lh->include_dirs_size)
11020 {
11021 lh->include_dirs_size *= 2;
11022 lh->include_dirs = xrealloc (lh->include_dirs,
11023 (lh->include_dirs_size
11024 * sizeof (*lh->include_dirs)));
c5aa993b 11025 }
c906108c 11026
debd256d
JB
11027 lh->include_dirs[lh->num_include_dirs++] = include_dir;
11028}
6e70227d 11029
debd256d 11030/* Add an entry to LH's file name table. */
ae2de4f8 11031
debd256d
JB
11032static void
11033add_file_name (struct line_header *lh,
11034 char *name,
11035 unsigned int dir_index,
11036 unsigned int mod_time,
11037 unsigned int length)
11038{
11039 struct file_entry *fe;
11040
11041 /* Grow the array if necessary. */
11042 if (lh->file_names_size == 0)
11043 {
11044 lh->file_names_size = 1; /* for testing */
11045 lh->file_names = xmalloc (lh->file_names_size
11046 * sizeof (*lh->file_names));
11047 }
11048 else if (lh->num_file_names >= lh->file_names_size)
11049 {
11050 lh->file_names_size *= 2;
11051 lh->file_names = xrealloc (lh->file_names,
11052 (lh->file_names_size
11053 * sizeof (*lh->file_names)));
11054 }
11055
11056 fe = &lh->file_names[lh->num_file_names++];
11057 fe->name = name;
11058 fe->dir_index = dir_index;
11059 fe->mod_time = mod_time;
11060 fe->length = length;
aaa75496 11061 fe->included_p = 0;
cb1df416 11062 fe->symtab = NULL;
debd256d 11063}
6e70227d 11064
debd256d 11065/* Read the statement program header starting at OFFSET in
6502dd73
DJ
11066 .debug_line, according to the endianness of ABFD. Return a pointer
11067 to a struct line_header, allocated using xmalloc.
debd256d
JB
11068
11069 NOTE: the strings in the include directory and file name tables of
11070 the returned object point into debug_line_buffer, and must not be
11071 freed. */
ae2de4f8 11072
debd256d
JB
11073static struct line_header *
11074dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 11075 struct dwarf2_cu *cu)
debd256d
JB
11076{
11077 struct cleanup *back_to;
11078 struct line_header *lh;
fe1b8b76 11079 gdb_byte *line_ptr;
c764a876 11080 unsigned int bytes_read, offset_size;
debd256d
JB
11081 int i;
11082 char *cur_dir, *cur_file;
11083
be391dca 11084 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 11085 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 11086 {
e2e0b3e5 11087 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
11088 return 0;
11089 }
11090
a738430d
MK
11091 /* Make sure that at least there's room for the total_length field.
11092 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 11093 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 11094 {
4d3c2250 11095 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
11096 return 0;
11097 }
11098
11099 lh = xmalloc (sizeof (*lh));
11100 memset (lh, 0, sizeof (*lh));
11101 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11102 (void *) lh);
11103
dce234bc 11104 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 11105
a738430d 11106 /* Read in the header. */
6e70227d 11107 lh->total_length =
c764a876
DE
11108 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11109 &bytes_read, &offset_size);
debd256d 11110 line_ptr += bytes_read;
dce234bc
PP
11111 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11112 + dwarf2_per_objfile->line.size))
debd256d 11113 {
4d3c2250 11114 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
11115 return 0;
11116 }
11117 lh->statement_program_end = line_ptr + lh->total_length;
11118 lh->version = read_2_bytes (abfd, line_ptr);
11119 line_ptr += 2;
c764a876
DE
11120 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11121 line_ptr += offset_size;
debd256d
JB
11122 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11123 line_ptr += 1;
2dc7f7b3
TT
11124 if (lh->version >= 4)
11125 {
11126 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11127 line_ptr += 1;
11128 }
11129 else
11130 lh->maximum_ops_per_instruction = 1;
11131
11132 if (lh->maximum_ops_per_instruction == 0)
11133 {
11134 lh->maximum_ops_per_instruction = 1;
11135 complaint (&symfile_complaints,
3e43a32a
MS
11136 _("invalid maximum_ops_per_instruction "
11137 "in `.debug_line' section"));
2dc7f7b3
TT
11138 }
11139
debd256d
JB
11140 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11141 line_ptr += 1;
11142 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11143 line_ptr += 1;
11144 lh->line_range = read_1_byte (abfd, line_ptr);
11145 line_ptr += 1;
11146 lh->opcode_base = read_1_byte (abfd, line_ptr);
11147 line_ptr += 1;
11148 lh->standard_opcode_lengths
fe1b8b76 11149 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11150
11151 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11152 for (i = 1; i < lh->opcode_base; ++i)
11153 {
11154 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11155 line_ptr += 1;
11156 }
11157
a738430d 11158 /* Read directory table. */
9b1c24c8 11159 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11160 {
11161 line_ptr += bytes_read;
11162 add_include_dir (lh, cur_dir);
11163 }
11164 line_ptr += bytes_read;
11165
a738430d 11166 /* Read file name table. */
9b1c24c8 11167 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11168 {
11169 unsigned int dir_index, mod_time, length;
11170
11171 line_ptr += bytes_read;
11172 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11173 line_ptr += bytes_read;
11174 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11175 line_ptr += bytes_read;
11176 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11177 line_ptr += bytes_read;
11178
11179 add_file_name (lh, cur_file, dir_index, mod_time, length);
11180 }
11181 line_ptr += bytes_read;
6e70227d 11182 lh->statement_program_start = line_ptr;
debd256d 11183
dce234bc
PP
11184 if (line_ptr > (dwarf2_per_objfile->line.buffer
11185 + dwarf2_per_objfile->line.size))
4d3c2250 11186 complaint (&symfile_complaints,
3e43a32a
MS
11187 _("line number info header doesn't "
11188 "fit in `.debug_line' section"));
debd256d
JB
11189
11190 discard_cleanups (back_to);
11191 return lh;
11192}
c906108c 11193
c6da4cef
DE
11194/* Subroutine of dwarf_decode_lines to simplify it.
11195 Return the file name of the psymtab for included file FILE_INDEX
11196 in line header LH of PST.
11197 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11198 If space for the result is malloc'd, it will be freed by a cleanup.
11199 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11200
11201static char *
11202psymtab_include_file_name (const struct line_header *lh, int file_index,
11203 const struct partial_symtab *pst,
11204 const char *comp_dir)
11205{
11206 const struct file_entry fe = lh->file_names [file_index];
11207 char *include_name = fe.name;
11208 char *include_name_to_compare = include_name;
11209 char *dir_name = NULL;
72b9f47f
TT
11210 const char *pst_filename;
11211 char *copied_name = NULL;
c6da4cef
DE
11212 int file_is_pst;
11213
11214 if (fe.dir_index)
11215 dir_name = lh->include_dirs[fe.dir_index - 1];
11216
11217 if (!IS_ABSOLUTE_PATH (include_name)
11218 && (dir_name != NULL || comp_dir != NULL))
11219 {
11220 /* Avoid creating a duplicate psymtab for PST.
11221 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11222 Before we do the comparison, however, we need to account
11223 for DIR_NAME and COMP_DIR.
11224 First prepend dir_name (if non-NULL). If we still don't
11225 have an absolute path prepend comp_dir (if non-NULL).
11226 However, the directory we record in the include-file's
11227 psymtab does not contain COMP_DIR (to match the
11228 corresponding symtab(s)).
11229
11230 Example:
11231
11232 bash$ cd /tmp
11233 bash$ gcc -g ./hello.c
11234 include_name = "hello.c"
11235 dir_name = "."
11236 DW_AT_comp_dir = comp_dir = "/tmp"
11237 DW_AT_name = "./hello.c" */
11238
11239 if (dir_name != NULL)
11240 {
11241 include_name = concat (dir_name, SLASH_STRING,
11242 include_name, (char *)NULL);
11243 include_name_to_compare = include_name;
11244 make_cleanup (xfree, include_name);
11245 }
11246 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11247 {
11248 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11249 include_name, (char *)NULL);
11250 }
11251 }
11252
11253 pst_filename = pst->filename;
11254 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11255 {
72b9f47f
TT
11256 copied_name = concat (pst->dirname, SLASH_STRING,
11257 pst_filename, (char *)NULL);
11258 pst_filename = copied_name;
c6da4cef
DE
11259 }
11260
1e3fad37 11261 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11262
11263 if (include_name_to_compare != include_name)
11264 xfree (include_name_to_compare);
72b9f47f
TT
11265 if (copied_name != NULL)
11266 xfree (copied_name);
c6da4cef
DE
11267
11268 if (file_is_pst)
11269 return NULL;
11270 return include_name;
11271}
11272
c91513d8
PP
11273/* Ignore this record_line request. */
11274
11275static void
11276noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11277{
11278 return;
11279}
11280
f3f5162e
DE
11281/* Subroutine of dwarf_decode_lines to simplify it.
11282 Process the line number information in LH. */
debd256d 11283
c906108c 11284static void
f3f5162e
DE
11285dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11286 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11287{
a8c50c1f 11288 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11289 gdb_byte *line_end;
a8c50c1f 11290 unsigned int bytes_read, extended_len;
c906108c 11291 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11292 CORE_ADDR baseaddr;
11293 struct objfile *objfile = cu->objfile;
f3f5162e 11294 bfd *abfd = objfile->obfd;
fbf65064 11295 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11296 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11297 struct subfile *last_subfile = NULL;
c91513d8
PP
11298 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11299 = record_line;
e142c38c
DJ
11300
11301 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11302
debd256d
JB
11303 line_ptr = lh->statement_program_start;
11304 line_end = lh->statement_program_end;
c906108c
SS
11305
11306 /* Read the statement sequences until there's nothing left. */
11307 while (line_ptr < line_end)
11308 {
11309 /* state machine registers */
11310 CORE_ADDR address = 0;
11311 unsigned int file = 1;
11312 unsigned int line = 1;
11313 unsigned int column = 0;
debd256d 11314 int is_stmt = lh->default_is_stmt;
c906108c
SS
11315 int basic_block = 0;
11316 int end_sequence = 0;
fbf65064 11317 CORE_ADDR addr;
2dc7f7b3 11318 unsigned char op_index = 0;
c906108c 11319
aaa75496 11320 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11321 {
aaa75496 11322 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11323 /* lh->include_dirs and lh->file_names are 0-based, but the
11324 directory and file name numbers in the statement program
11325 are 1-based. */
11326 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11327 char *dir = NULL;
a738430d 11328
debd256d
JB
11329 if (fe->dir_index)
11330 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11331
11332 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11333 }
11334
a738430d 11335 /* Decode the table. */
c5aa993b 11336 while (!end_sequence)
c906108c
SS
11337 {
11338 op_code = read_1_byte (abfd, line_ptr);
11339 line_ptr += 1;
59205f5a
JB
11340 if (line_ptr > line_end)
11341 {
11342 dwarf2_debug_line_missing_end_sequence_complaint ();
11343 break;
11344 }
9aa1fe7e 11345
debd256d 11346 if (op_code >= lh->opcode_base)
6e70227d 11347 {
a738430d 11348 /* Special operand. */
debd256d 11349 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11350 address += (((op_index + (adj_opcode / lh->line_range))
11351 / lh->maximum_ops_per_instruction)
11352 * lh->minimum_instruction_length);
11353 op_index = ((op_index + (adj_opcode / lh->line_range))
11354 % lh->maximum_ops_per_instruction);
debd256d 11355 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11356 if (lh->num_file_names < file || file == 0)
25e43795 11357 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11358 /* For now we ignore lines not starting on an
11359 instruction boundary. */
11360 else if (op_index == 0)
25e43795
DJ
11361 {
11362 lh->file_names[file - 1].included_p = 1;
ca5f395d 11363 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11364 {
11365 if (last_subfile != current_subfile)
11366 {
11367 addr = gdbarch_addr_bits_remove (gdbarch, address);
11368 if (last_subfile)
c91513d8 11369 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11370 last_subfile = current_subfile;
11371 }
25e43795 11372 /* Append row to matrix using current values. */
7019d805 11373 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11374 (*p_record_line) (current_subfile, line, addr);
366da635 11375 }
25e43795 11376 }
ca5f395d 11377 basic_block = 0;
9aa1fe7e
GK
11378 }
11379 else switch (op_code)
c906108c
SS
11380 {
11381 case DW_LNS_extended_op:
3e43a32a
MS
11382 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11383 &bytes_read);
473b7be6 11384 line_ptr += bytes_read;
a8c50c1f 11385 extended_end = line_ptr + extended_len;
c906108c
SS
11386 extended_op = read_1_byte (abfd, line_ptr);
11387 line_ptr += 1;
11388 switch (extended_op)
11389 {
11390 case DW_LNE_end_sequence:
c91513d8 11391 p_record_line = record_line;
c906108c 11392 end_sequence = 1;
c906108c
SS
11393 break;
11394 case DW_LNE_set_address:
e7c27a73 11395 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11396
11397 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11398 {
11399 /* This line table is for a function which has been
11400 GCd by the linker. Ignore it. PR gdb/12528 */
11401
11402 long line_offset
11403 = line_ptr - dwarf2_per_objfile->line.buffer;
11404
11405 complaint (&symfile_complaints,
11406 _(".debug_line address at offset 0x%lx is 0 "
11407 "[in module %s]"),
bb5ed363 11408 line_offset, objfile->name);
c91513d8
PP
11409 p_record_line = noop_record_line;
11410 }
11411
2dc7f7b3 11412 op_index = 0;
107d2387
AC
11413 line_ptr += bytes_read;
11414 address += baseaddr;
c906108c
SS
11415 break;
11416 case DW_LNE_define_file:
debd256d
JB
11417 {
11418 char *cur_file;
11419 unsigned int dir_index, mod_time, length;
6e70227d 11420
3e43a32a
MS
11421 cur_file = read_direct_string (abfd, line_ptr,
11422 &bytes_read);
debd256d
JB
11423 line_ptr += bytes_read;
11424 dir_index =
11425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11426 line_ptr += bytes_read;
11427 mod_time =
11428 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11429 line_ptr += bytes_read;
11430 length =
11431 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11432 line_ptr += bytes_read;
11433 add_file_name (lh, cur_file, dir_index, mod_time, length);
11434 }
c906108c 11435 break;
d0c6ba3d
CC
11436 case DW_LNE_set_discriminator:
11437 /* The discriminator is not interesting to the debugger;
11438 just ignore it. */
11439 line_ptr = extended_end;
11440 break;
c906108c 11441 default:
4d3c2250 11442 complaint (&symfile_complaints,
e2e0b3e5 11443 _("mangled .debug_line section"));
debd256d 11444 return;
c906108c 11445 }
a8c50c1f
DJ
11446 /* Make sure that we parsed the extended op correctly. If e.g.
11447 we expected a different address size than the producer used,
11448 we may have read the wrong number of bytes. */
11449 if (line_ptr != extended_end)
11450 {
11451 complaint (&symfile_complaints,
11452 _("mangled .debug_line section"));
11453 return;
11454 }
c906108c
SS
11455 break;
11456 case DW_LNS_copy:
59205f5a 11457 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11458 dwarf2_debug_line_missing_file_complaint ();
11459 else
366da635 11460 {
25e43795 11461 lh->file_names[file - 1].included_p = 1;
ca5f395d 11462 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11463 {
11464 if (last_subfile != current_subfile)
11465 {
11466 addr = gdbarch_addr_bits_remove (gdbarch, address);
11467 if (last_subfile)
c91513d8 11468 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11469 last_subfile = current_subfile;
11470 }
7019d805 11471 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11472 (*p_record_line) (current_subfile, line, addr);
fbf65064 11473 }
366da635 11474 }
c906108c
SS
11475 basic_block = 0;
11476 break;
11477 case DW_LNS_advance_pc:
2dc7f7b3
TT
11478 {
11479 CORE_ADDR adjust
11480 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11481
11482 address += (((op_index + adjust)
11483 / lh->maximum_ops_per_instruction)
11484 * lh->minimum_instruction_length);
11485 op_index = ((op_index + adjust)
11486 % lh->maximum_ops_per_instruction);
11487 line_ptr += bytes_read;
11488 }
c906108c
SS
11489 break;
11490 case DW_LNS_advance_line:
11491 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11492 line_ptr += bytes_read;
11493 break;
11494 case DW_LNS_set_file:
debd256d 11495 {
a738430d
MK
11496 /* The arrays lh->include_dirs and lh->file_names are
11497 0-based, but the directory and file name numbers in
11498 the statement program are 1-based. */
debd256d 11499 struct file_entry *fe;
4f1520fb 11500 char *dir = NULL;
a738430d 11501
debd256d
JB
11502 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11503 line_ptr += bytes_read;
59205f5a 11504 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11505 dwarf2_debug_line_missing_file_complaint ();
11506 else
11507 {
11508 fe = &lh->file_names[file - 1];
11509 if (fe->dir_index)
11510 dir = lh->include_dirs[fe->dir_index - 1];
11511 if (!decode_for_pst_p)
11512 {
11513 last_subfile = current_subfile;
11514 dwarf2_start_subfile (fe->name, dir, comp_dir);
11515 }
11516 }
debd256d 11517 }
c906108c
SS
11518 break;
11519 case DW_LNS_set_column:
11520 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11521 line_ptr += bytes_read;
11522 break;
11523 case DW_LNS_negate_stmt:
11524 is_stmt = (!is_stmt);
11525 break;
11526 case DW_LNS_set_basic_block:
11527 basic_block = 1;
11528 break;
c2c6d25f
JM
11529 /* Add to the address register of the state machine the
11530 address increment value corresponding to special opcode
a738430d
MK
11531 255. I.e., this value is scaled by the minimum
11532 instruction length since special opcode 255 would have
b021a221 11533 scaled the increment. */
c906108c 11534 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11535 {
11536 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11537
11538 address += (((op_index + adjust)
11539 / lh->maximum_ops_per_instruction)
11540 * lh->minimum_instruction_length);
11541 op_index = ((op_index + adjust)
11542 % lh->maximum_ops_per_instruction);
11543 }
c906108c
SS
11544 break;
11545 case DW_LNS_fixed_advance_pc:
11546 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11547 op_index = 0;
c906108c
SS
11548 line_ptr += 2;
11549 break;
9aa1fe7e 11550 default:
a738430d
MK
11551 {
11552 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11553 int i;
a738430d 11554
debd256d 11555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11556 {
11557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11558 line_ptr += bytes_read;
11559 }
11560 }
c906108c
SS
11561 }
11562 }
59205f5a
JB
11563 if (lh->num_file_names < file || file == 0)
11564 dwarf2_debug_line_missing_file_complaint ();
11565 else
11566 {
11567 lh->file_names[file - 1].included_p = 1;
11568 if (!decode_for_pst_p)
fbf65064
UW
11569 {
11570 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11571 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11572 }
59205f5a 11573 }
c906108c 11574 }
f3f5162e
DE
11575}
11576
11577/* Decode the Line Number Program (LNP) for the given line_header
11578 structure and CU. The actual information extracted and the type
11579 of structures created from the LNP depends on the value of PST.
11580
11581 1. If PST is NULL, then this procedure uses the data from the program
11582 to create all necessary symbol tables, and their linetables.
11583
11584 2. If PST is not NULL, this procedure reads the program to determine
11585 the list of files included by the unit represented by PST, and
11586 builds all the associated partial symbol tables.
11587
11588 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11589 It is used for relative paths in the line table.
11590 NOTE: When processing partial symtabs (pst != NULL),
11591 comp_dir == pst->dirname.
11592
11593 NOTE: It is important that psymtabs have the same file name (via strcmp)
11594 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11595 symtab we don't use it in the name of the psymtabs we create.
11596 E.g. expand_line_sal requires this when finding psymtabs to expand.
11597 A good testcase for this is mb-inline.exp. */
11598
11599static void
11600dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11601 struct dwarf2_cu *cu, struct partial_symtab *pst,
11602 int want_line_info)
11603{
11604 struct objfile *objfile = cu->objfile;
11605 const int decode_for_pst_p = (pst != NULL);
11606 struct subfile *first_subfile = current_subfile;
11607
11608 if (want_line_info)
11609 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11610
11611 if (decode_for_pst_p)
11612 {
11613 int file_index;
11614
11615 /* Now that we're done scanning the Line Header Program, we can
11616 create the psymtab of each included file. */
11617 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11618 if (lh->file_names[file_index].included_p == 1)
11619 {
c6da4cef
DE
11620 char *include_name =
11621 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11622 if (include_name != NULL)
aaa75496
JB
11623 dwarf2_create_include_psymtab (include_name, pst, objfile);
11624 }
11625 }
cb1df416
DJ
11626 else
11627 {
11628 /* Make sure a symtab is created for every file, even files
11629 which contain only variables (i.e. no code with associated
11630 line numbers). */
cb1df416 11631 int i;
cb1df416
DJ
11632
11633 for (i = 0; i < lh->num_file_names; i++)
11634 {
11635 char *dir = NULL;
f3f5162e 11636 struct file_entry *fe;
9a619af0 11637
cb1df416
DJ
11638 fe = &lh->file_names[i];
11639 if (fe->dir_index)
11640 dir = lh->include_dirs[fe->dir_index - 1];
11641 dwarf2_start_subfile (fe->name, dir, comp_dir);
11642
11643 /* Skip the main file; we don't need it, and it must be
11644 allocated last, so that it will show up before the
11645 non-primary symtabs in the objfile's symtab list. */
11646 if (current_subfile == first_subfile)
11647 continue;
11648
11649 if (current_subfile->symtab == NULL)
11650 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11651 objfile);
cb1df416
DJ
11652 fe->symtab = current_subfile->symtab;
11653 }
11654 }
c906108c
SS
11655}
11656
11657/* Start a subfile for DWARF. FILENAME is the name of the file and
11658 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11659 or NULL if not known. COMP_DIR is the compilation directory for the
11660 linetable's compilation unit or NULL if not known.
c906108c
SS
11661 This routine tries to keep line numbers from identical absolute and
11662 relative file names in a common subfile.
11663
11664 Using the `list' example from the GDB testsuite, which resides in
11665 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11666 of /srcdir/list0.c yields the following debugging information for list0.c:
11667
c5aa993b
JM
11668 DW_AT_name: /srcdir/list0.c
11669 DW_AT_comp_dir: /compdir
357e46e7 11670 files.files[0].name: list0.h
c5aa993b 11671 files.files[0].dir: /srcdir
357e46e7 11672 files.files[1].name: list0.c
c5aa993b 11673 files.files[1].dir: /srcdir
c906108c
SS
11674
11675 The line number information for list0.c has to end up in a single
4f1520fb
FR
11676 subfile, so that `break /srcdir/list0.c:1' works as expected.
11677 start_subfile will ensure that this happens provided that we pass the
11678 concatenation of files.files[1].dir and files.files[1].name as the
11679 subfile's name. */
c906108c
SS
11680
11681static void
3e43a32a
MS
11682dwarf2_start_subfile (char *filename, const char *dirname,
11683 const char *comp_dir)
c906108c 11684{
4f1520fb
FR
11685 char *fullname;
11686
11687 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11688 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11689 second argument to start_subfile. To be consistent, we do the
11690 same here. In order not to lose the line information directory,
11691 we concatenate it to the filename when it makes sense.
11692 Note that the Dwarf3 standard says (speaking of filenames in line
11693 information): ``The directory index is ignored for file names
11694 that represent full path names''. Thus ignoring dirname in the
11695 `else' branch below isn't an issue. */
c906108c 11696
d5166ae1 11697 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11698 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11699 else
11700 fullname = filename;
c906108c 11701
4f1520fb
FR
11702 start_subfile (fullname, comp_dir);
11703
11704 if (fullname != filename)
11705 xfree (fullname);
c906108c
SS
11706}
11707
4c2df51b
DJ
11708static void
11709var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11710 struct dwarf2_cu *cu)
4c2df51b 11711{
e7c27a73
DJ
11712 struct objfile *objfile = cu->objfile;
11713 struct comp_unit_head *cu_header = &cu->header;
11714
4c2df51b
DJ
11715 /* NOTE drow/2003-01-30: There used to be a comment and some special
11716 code here to turn a symbol with DW_AT_external and a
11717 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11718 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11719 with some versions of binutils) where shared libraries could have
11720 relocations against symbols in their debug information - the
11721 minimal symbol would have the right address, but the debug info
11722 would not. It's no longer necessary, because we will explicitly
11723 apply relocations when we read in the debug information now. */
11724
11725 /* A DW_AT_location attribute with no contents indicates that a
11726 variable has been optimized away. */
11727 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11728 {
11729 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11730 return;
11731 }
11732
11733 /* Handle one degenerate form of location expression specially, to
11734 preserve GDB's previous behavior when section offsets are
11735 specified. If this is just a DW_OP_addr then mark this symbol
11736 as LOC_STATIC. */
11737
11738 if (attr_form_is_block (attr)
11739 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11740 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11741 {
891d2f0b 11742 unsigned int dummy;
4c2df51b
DJ
11743
11744 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11745 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11746 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11747 fixup_symbol_section (sym, objfile);
11748 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11749 SYMBOL_SECTION (sym));
4c2df51b
DJ
11750 return;
11751 }
11752
11753 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11754 expression evaluator, and use LOC_COMPUTED only when necessary
11755 (i.e. when the value of a register or memory location is
11756 referenced, or a thread-local block, etc.). Then again, it might
11757 not be worthwhile. I'm assuming that it isn't unless performance
11758 or memory numbers show me otherwise. */
11759
e7c27a73 11760 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11761 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11762
11763 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11764 cu->has_loclist = 1;
4c2df51b
DJ
11765}
11766
c906108c
SS
11767/* Given a pointer to a DWARF information entry, figure out if we need
11768 to make a symbol table entry for it, and if so, create a new entry
11769 and return a pointer to it.
11770 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11771 used the passed type.
11772 If SPACE is not NULL, use it to hold the new symbol. If it is
11773 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11774
11775static struct symbol *
34eaf542
TT
11776new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11777 struct symbol *space)
c906108c 11778{
e7c27a73 11779 struct objfile *objfile = cu->objfile;
c906108c
SS
11780 struct symbol *sym = NULL;
11781 char *name;
11782 struct attribute *attr = NULL;
11783 struct attribute *attr2 = NULL;
e142c38c 11784 CORE_ADDR baseaddr;
e37fd15a
SW
11785 struct pending **list_to_add = NULL;
11786
edb3359d 11787 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11788
11789 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11790
94af9270 11791 name = dwarf2_name (die, cu);
c906108c
SS
11792 if (name)
11793 {
94af9270 11794 const char *linkagename;
34eaf542 11795 int suppress_add = 0;
94af9270 11796
34eaf542
TT
11797 if (space)
11798 sym = space;
11799 else
11800 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11801 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11802
11803 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11804 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11805 linkagename = dwarf2_physname (name, die, cu);
11806 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11807
f55ee35c
JK
11808 /* Fortran does not have mangling standard and the mangling does differ
11809 between gfortran, iFort etc. */
11810 if (cu->language == language_fortran
b250c185 11811 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11812 symbol_set_demangled_name (&(sym->ginfo),
11813 (char *) dwarf2_full_name (name, die, cu),
11814 NULL);
f55ee35c 11815
c906108c 11816 /* Default assumptions.
c5aa993b 11817 Use the passed type or decode it from the die. */
176620f1 11818 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11819 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11820 if (type != NULL)
11821 SYMBOL_TYPE (sym) = type;
11822 else
e7c27a73 11823 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11824 attr = dwarf2_attr (die,
11825 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11826 cu);
c906108c
SS
11827 if (attr)
11828 {
11829 SYMBOL_LINE (sym) = DW_UNSND (attr);
11830 }
cb1df416 11831
edb3359d
DJ
11832 attr = dwarf2_attr (die,
11833 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11834 cu);
cb1df416
DJ
11835 if (attr)
11836 {
11837 int file_index = DW_UNSND (attr);
9a619af0 11838
cb1df416
DJ
11839 if (cu->line_header == NULL
11840 || file_index > cu->line_header->num_file_names)
11841 complaint (&symfile_complaints,
11842 _("file index out of range"));
1c3d648d 11843 else if (file_index > 0)
cb1df416
DJ
11844 {
11845 struct file_entry *fe;
9a619af0 11846
cb1df416
DJ
11847 fe = &cu->line_header->file_names[file_index - 1];
11848 SYMBOL_SYMTAB (sym) = fe->symtab;
11849 }
11850 }
11851
c906108c
SS
11852 switch (die->tag)
11853 {
11854 case DW_TAG_label:
e142c38c 11855 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11856 if (attr)
11857 {
11858 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11859 }
0f5238ed
TT
11860 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11861 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11862 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11863 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11864 break;
11865 case DW_TAG_subprogram:
11866 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11867 finish_block. */
11868 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11869 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11870 if ((attr2 && (DW_UNSND (attr2) != 0))
11871 || cu->language == language_ada)
c906108c 11872 {
2cfa0c8d
JB
11873 /* Subprograms marked external are stored as a global symbol.
11874 Ada subprograms, whether marked external or not, are always
11875 stored as a global symbol, because we want to be able to
11876 access them globally. For instance, we want to be able
11877 to break on a nested subprogram without having to
11878 specify the context. */
e37fd15a 11879 list_to_add = &global_symbols;
c906108c
SS
11880 }
11881 else
11882 {
e37fd15a 11883 list_to_add = cu->list_in_scope;
c906108c
SS
11884 }
11885 break;
edb3359d
DJ
11886 case DW_TAG_inlined_subroutine:
11887 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11888 finish_block. */
11889 SYMBOL_CLASS (sym) = LOC_BLOCK;
11890 SYMBOL_INLINED (sym) = 1;
481860b3 11891 list_to_add = cu->list_in_scope;
edb3359d 11892 break;
34eaf542
TT
11893 case DW_TAG_template_value_param:
11894 suppress_add = 1;
11895 /* Fall through. */
72929c62 11896 case DW_TAG_constant:
c906108c 11897 case DW_TAG_variable:
254e6b9e 11898 case DW_TAG_member:
0963b4bd
MS
11899 /* Compilation with minimal debug info may result in
11900 variables with missing type entries. Change the
11901 misleading `void' type to something sensible. */
c906108c 11902 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11903 SYMBOL_TYPE (sym)
46bf5051 11904 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11905
e142c38c 11906 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11907 /* In the case of DW_TAG_member, we should only be called for
11908 static const members. */
11909 if (die->tag == DW_TAG_member)
11910 {
3863f96c
DE
11911 /* dwarf2_add_field uses die_is_declaration,
11912 so we do the same. */
254e6b9e
DE
11913 gdb_assert (die_is_declaration (die, cu));
11914 gdb_assert (attr);
11915 }
c906108c
SS
11916 if (attr)
11917 {
e7c27a73 11918 dwarf2_const_value (attr, sym, cu);
e142c38c 11919 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11920 if (!suppress_add)
34eaf542
TT
11921 {
11922 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11923 list_to_add = &global_symbols;
34eaf542 11924 else
e37fd15a 11925 list_to_add = cu->list_in_scope;
34eaf542 11926 }
c906108c
SS
11927 break;
11928 }
e142c38c 11929 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11930 if (attr)
11931 {
e7c27a73 11932 var_decode_location (attr, sym, cu);
e142c38c 11933 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11934 if (SYMBOL_CLASS (sym) == LOC_STATIC
11935 && SYMBOL_VALUE_ADDRESS (sym) == 0
11936 && !dwarf2_per_objfile->has_section_at_zero)
11937 {
11938 /* When a static variable is eliminated by the linker,
11939 the corresponding debug information is not stripped
11940 out, but the variable address is set to null;
11941 do not add such variables into symbol table. */
11942 }
11943 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11944 {
f55ee35c
JK
11945 /* Workaround gfortran PR debug/40040 - it uses
11946 DW_AT_location for variables in -fPIC libraries which may
11947 get overriden by other libraries/executable and get
11948 a different address. Resolve it by the minimal symbol
11949 which may come from inferior's executable using copy
11950 relocation. Make this workaround only for gfortran as for
11951 other compilers GDB cannot guess the minimal symbol
11952 Fortran mangling kind. */
11953 if (cu->language == language_fortran && die->parent
11954 && die->parent->tag == DW_TAG_module
11955 && cu->producer
11956 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11957 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11958
1c809c68
TT
11959 /* A variable with DW_AT_external is never static,
11960 but it may be block-scoped. */
11961 list_to_add = (cu->list_in_scope == &file_symbols
11962 ? &global_symbols : cu->list_in_scope);
1c809c68 11963 }
c906108c 11964 else
e37fd15a 11965 list_to_add = cu->list_in_scope;
c906108c
SS
11966 }
11967 else
11968 {
11969 /* We do not know the address of this symbol.
c5aa993b
JM
11970 If it is an external symbol and we have type information
11971 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11972 The address of the variable will then be determined from
11973 the minimal symbol table whenever the variable is
11974 referenced. */
e142c38c 11975 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11976 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11977 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11978 {
0fe7935b
DJ
11979 /* A variable with DW_AT_external is never static, but it
11980 may be block-scoped. */
11981 list_to_add = (cu->list_in_scope == &file_symbols
11982 ? &global_symbols : cu->list_in_scope);
11983
c906108c 11984 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11985 }
442ddf59
JK
11986 else if (!die_is_declaration (die, cu))
11987 {
11988 /* Use the default LOC_OPTIMIZED_OUT class. */
11989 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11990 if (!suppress_add)
11991 list_to_add = cu->list_in_scope;
442ddf59 11992 }
c906108c
SS
11993 }
11994 break;
11995 case DW_TAG_formal_parameter:
edb3359d
DJ
11996 /* If we are inside a function, mark this as an argument. If
11997 not, we might be looking at an argument to an inlined function
11998 when we do not have enough information to show inlined frames;
11999 pretend it's a local variable in that case so that the user can
12000 still see it. */
12001 if (context_stack_depth > 0
12002 && context_stack[context_stack_depth - 1].name != NULL)
12003 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 12004 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
12005 if (attr)
12006 {
e7c27a73 12007 var_decode_location (attr, sym, cu);
c906108c 12008 }
e142c38c 12009 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
12010 if (attr)
12011 {
e7c27a73 12012 dwarf2_const_value (attr, sym, cu);
c906108c 12013 }
f346a30d 12014
e37fd15a 12015 list_to_add = cu->list_in_scope;
c906108c
SS
12016 break;
12017 case DW_TAG_unspecified_parameters:
12018 /* From varargs functions; gdb doesn't seem to have any
12019 interest in this information, so just ignore it for now.
12020 (FIXME?) */
12021 break;
34eaf542
TT
12022 case DW_TAG_template_type_param:
12023 suppress_add = 1;
12024 /* Fall through. */
c906108c 12025 case DW_TAG_class_type:
680b30c7 12026 case DW_TAG_interface_type:
c906108c
SS
12027 case DW_TAG_structure_type:
12028 case DW_TAG_union_type:
72019c9c 12029 case DW_TAG_set_type:
c906108c
SS
12030 case DW_TAG_enumeration_type:
12031 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 12032 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 12033
63d06c5c 12034 {
987504bb 12035 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
12036 really ever be static objects: otherwise, if you try
12037 to, say, break of a class's method and you're in a file
12038 which doesn't mention that class, it won't work unless
12039 the check for all static symbols in lookup_symbol_aux
12040 saves you. See the OtherFileClass tests in
12041 gdb.c++/namespace.exp. */
12042
e37fd15a 12043 if (!suppress_add)
34eaf542 12044 {
34eaf542
TT
12045 list_to_add = (cu->list_in_scope == &file_symbols
12046 && (cu->language == language_cplus
12047 || cu->language == language_java)
12048 ? &global_symbols : cu->list_in_scope);
63d06c5c 12049
64382290
TT
12050 /* The semantics of C++ state that "struct foo {
12051 ... }" also defines a typedef for "foo". A Java
12052 class declaration also defines a typedef for the
12053 class. */
12054 if (cu->language == language_cplus
12055 || cu->language == language_java
12056 || cu->language == language_ada)
12057 {
12058 /* The symbol's name is already allocated along
12059 with this objfile, so we don't need to
12060 duplicate it for the type. */
12061 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
12062 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
12063 }
63d06c5c
DC
12064 }
12065 }
c906108c
SS
12066 break;
12067 case DW_TAG_typedef:
63d06c5c
DC
12068 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12069 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 12070 list_to_add = cu->list_in_scope;
63d06c5c 12071 break;
c906108c 12072 case DW_TAG_base_type:
a02abb62 12073 case DW_TAG_subrange_type:
c906108c 12074 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 12075 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 12076 list_to_add = cu->list_in_scope;
c906108c
SS
12077 break;
12078 case DW_TAG_enumerator:
e142c38c 12079 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
12080 if (attr)
12081 {
e7c27a73 12082 dwarf2_const_value (attr, sym, cu);
c906108c 12083 }
63d06c5c
DC
12084 {
12085 /* NOTE: carlton/2003-11-10: See comment above in the
12086 DW_TAG_class_type, etc. block. */
12087
e142c38c 12088 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
12089 && (cu->language == language_cplus
12090 || cu->language == language_java)
e142c38c 12091 ? &global_symbols : cu->list_in_scope);
63d06c5c 12092 }
c906108c 12093 break;
5c4e30ca
DC
12094 case DW_TAG_namespace:
12095 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 12096 list_to_add = &global_symbols;
5c4e30ca 12097 break;
c906108c
SS
12098 default:
12099 /* Not a tag we recognize. Hopefully we aren't processing
12100 trash data, but since we must specifically ignore things
12101 we don't recognize, there is nothing else we should do at
0963b4bd 12102 this point. */
e2e0b3e5 12103 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 12104 dwarf_tag_name (die->tag));
c906108c
SS
12105 break;
12106 }
df8a16a1 12107
e37fd15a
SW
12108 if (suppress_add)
12109 {
12110 sym->hash_next = objfile->template_symbols;
12111 objfile->template_symbols = sym;
12112 list_to_add = NULL;
12113 }
12114
12115 if (list_to_add != NULL)
12116 add_symbol_to_list (sym, list_to_add);
12117
df8a16a1
DJ
12118 /* For the benefit of old versions of GCC, check for anonymous
12119 namespaces based on the demangled name. */
12120 if (!processing_has_namespace_info
94af9270 12121 && cu->language == language_cplus)
a10964d1 12122 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12123 }
12124 return (sym);
12125}
12126
34eaf542
TT
12127/* A wrapper for new_symbol_full that always allocates a new symbol. */
12128
12129static struct symbol *
12130new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12131{
12132 return new_symbol_full (die, type, cu, NULL);
12133}
12134
98bfdba5
PA
12135/* Given an attr with a DW_FORM_dataN value in host byte order,
12136 zero-extend it as appropriate for the symbol's type. The DWARF
12137 standard (v4) is not entirely clear about the meaning of using
12138 DW_FORM_dataN for a constant with a signed type, where the type is
12139 wider than the data. The conclusion of a discussion on the DWARF
12140 list was that this is unspecified. We choose to always zero-extend
12141 because that is the interpretation long in use by GCC. */
c906108c 12142
98bfdba5
PA
12143static gdb_byte *
12144dwarf2_const_value_data (struct attribute *attr, struct type *type,
12145 const char *name, struct obstack *obstack,
12df843f 12146 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 12147{
e7c27a73 12148 struct objfile *objfile = cu->objfile;
e17a4113
UW
12149 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12150 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12151 LONGEST l = DW_UNSND (attr);
12152
12153 if (bits < sizeof (*value) * 8)
12154 {
12155 l &= ((LONGEST) 1 << bits) - 1;
12156 *value = l;
12157 }
12158 else if (bits == sizeof (*value) * 8)
12159 *value = l;
12160 else
12161 {
12162 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12163 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12164 return bytes;
12165 }
12166
12167 return NULL;
12168}
12169
12170/* Read a constant value from an attribute. Either set *VALUE, or if
12171 the value does not fit in *VALUE, set *BYTES - either already
12172 allocated on the objfile obstack, or newly allocated on OBSTACK,
12173 or, set *BATON, if we translated the constant to a location
12174 expression. */
12175
12176static void
12177dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12178 const char *name, struct obstack *obstack,
12179 struct dwarf2_cu *cu,
12df843f 12180 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
12181 struct dwarf2_locexpr_baton **baton)
12182{
12183 struct objfile *objfile = cu->objfile;
12184 struct comp_unit_head *cu_header = &cu->header;
c906108c 12185 struct dwarf_block *blk;
98bfdba5
PA
12186 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12187 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12188
12189 *value = 0;
12190 *bytes = NULL;
12191 *baton = NULL;
c906108c
SS
12192
12193 switch (attr->form)
12194 {
12195 case DW_FORM_addr:
ac56253d 12196 {
ac56253d
TT
12197 gdb_byte *data;
12198
98bfdba5
PA
12199 if (TYPE_LENGTH (type) != cu_header->addr_size)
12200 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12201 cu_header->addr_size,
98bfdba5 12202 TYPE_LENGTH (type));
ac56253d
TT
12203 /* Symbols of this form are reasonably rare, so we just
12204 piggyback on the existing location code rather than writing
12205 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12206 *baton = obstack_alloc (&objfile->objfile_obstack,
12207 sizeof (struct dwarf2_locexpr_baton));
12208 (*baton)->per_cu = cu->per_cu;
12209 gdb_assert ((*baton)->per_cu);
ac56253d 12210
98bfdba5
PA
12211 (*baton)->size = 2 + cu_header->addr_size;
12212 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12213 (*baton)->data = data;
ac56253d
TT
12214
12215 data[0] = DW_OP_addr;
12216 store_unsigned_integer (&data[1], cu_header->addr_size,
12217 byte_order, DW_ADDR (attr));
12218 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12219 }
c906108c 12220 break;
4ac36638 12221 case DW_FORM_string:
93b5768b 12222 case DW_FORM_strp:
98bfdba5
PA
12223 /* DW_STRING is already allocated on the objfile obstack, point
12224 directly to it. */
12225 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12226 break;
c906108c
SS
12227 case DW_FORM_block1:
12228 case DW_FORM_block2:
12229 case DW_FORM_block4:
12230 case DW_FORM_block:
2dc7f7b3 12231 case DW_FORM_exprloc:
c906108c 12232 blk = DW_BLOCK (attr);
98bfdba5
PA
12233 if (TYPE_LENGTH (type) != blk->size)
12234 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12235 TYPE_LENGTH (type));
12236 *bytes = blk->data;
c906108c 12237 break;
2df3850c
JM
12238
12239 /* The DW_AT_const_value attributes are supposed to carry the
12240 symbol's value "represented as it would be on the target
12241 architecture." By the time we get here, it's already been
12242 converted to host endianness, so we just need to sign- or
12243 zero-extend it as appropriate. */
12244 case DW_FORM_data1:
3e43a32a
MS
12245 *bytes = dwarf2_const_value_data (attr, type, name,
12246 obstack, cu, value, 8);
2df3850c 12247 break;
c906108c 12248 case DW_FORM_data2:
3e43a32a
MS
12249 *bytes = dwarf2_const_value_data (attr, type, name,
12250 obstack, cu, value, 16);
2df3850c 12251 break;
c906108c 12252 case DW_FORM_data4:
3e43a32a
MS
12253 *bytes = dwarf2_const_value_data (attr, type, name,
12254 obstack, cu, value, 32);
2df3850c 12255 break;
c906108c 12256 case DW_FORM_data8:
3e43a32a
MS
12257 *bytes = dwarf2_const_value_data (attr, type, name,
12258 obstack, cu, value, 64);
2df3850c
JM
12259 break;
12260
c906108c 12261 case DW_FORM_sdata:
98bfdba5 12262 *value = DW_SND (attr);
2df3850c
JM
12263 break;
12264
c906108c 12265 case DW_FORM_udata:
98bfdba5 12266 *value = DW_UNSND (attr);
c906108c 12267 break;
2df3850c 12268
c906108c 12269 default:
4d3c2250 12270 complaint (&symfile_complaints,
e2e0b3e5 12271 _("unsupported const value attribute form: '%s'"),
4d3c2250 12272 dwarf_form_name (attr->form));
98bfdba5 12273 *value = 0;
c906108c
SS
12274 break;
12275 }
12276}
12277
2df3850c 12278
98bfdba5
PA
12279/* Copy constant value from an attribute to a symbol. */
12280
2df3850c 12281static void
98bfdba5
PA
12282dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12283 struct dwarf2_cu *cu)
2df3850c 12284{
98bfdba5
PA
12285 struct objfile *objfile = cu->objfile;
12286 struct comp_unit_head *cu_header = &cu->header;
12df843f 12287 LONGEST value;
98bfdba5
PA
12288 gdb_byte *bytes;
12289 struct dwarf2_locexpr_baton *baton;
2df3850c 12290
98bfdba5
PA
12291 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12292 SYMBOL_PRINT_NAME (sym),
12293 &objfile->objfile_obstack, cu,
12294 &value, &bytes, &baton);
2df3850c 12295
98bfdba5
PA
12296 if (baton != NULL)
12297 {
12298 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12299 SYMBOL_LOCATION_BATON (sym) = baton;
12300 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12301 }
12302 else if (bytes != NULL)
12303 {
12304 SYMBOL_VALUE_BYTES (sym) = bytes;
12305 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12306 }
12307 else
12308 {
12309 SYMBOL_VALUE (sym) = value;
12310 SYMBOL_CLASS (sym) = LOC_CONST;
12311 }
2df3850c
JM
12312}
12313
c906108c
SS
12314/* Return the type of the die in question using its DW_AT_type attribute. */
12315
12316static struct type *
e7c27a73 12317die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12318{
c906108c 12319 struct attribute *type_attr;
c906108c 12320
e142c38c 12321 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12322 if (!type_attr)
12323 {
12324 /* A missing DW_AT_type represents a void type. */
46bf5051 12325 return objfile_type (cu->objfile)->builtin_void;
c906108c 12326 }
348e048f 12327
673bfd45 12328 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12329}
12330
b4ba55a1
JB
12331/* True iff CU's producer generates GNAT Ada auxiliary information
12332 that allows to find parallel types through that information instead
12333 of having to do expensive parallel lookups by type name. */
12334
12335static int
12336need_gnat_info (struct dwarf2_cu *cu)
12337{
12338 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12339 of GNAT produces this auxiliary information, without any indication
12340 that it is produced. Part of enhancing the FSF version of GNAT
12341 to produce that information will be to put in place an indicator
12342 that we can use in order to determine whether the descriptive type
12343 info is available or not. One suggestion that has been made is
12344 to use a new attribute, attached to the CU die. For now, assume
12345 that the descriptive type info is not available. */
12346 return 0;
12347}
12348
b4ba55a1
JB
12349/* Return the auxiliary type of the die in question using its
12350 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12351 attribute is not present. */
12352
12353static struct type *
12354die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12355{
b4ba55a1 12356 struct attribute *type_attr;
b4ba55a1
JB
12357
12358 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12359 if (!type_attr)
12360 return NULL;
12361
673bfd45 12362 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12363}
12364
12365/* If DIE has a descriptive_type attribute, then set the TYPE's
12366 descriptive type accordingly. */
12367
12368static void
12369set_descriptive_type (struct type *type, struct die_info *die,
12370 struct dwarf2_cu *cu)
12371{
12372 struct type *descriptive_type = die_descriptive_type (die, cu);
12373
12374 if (descriptive_type)
12375 {
12376 ALLOCATE_GNAT_AUX_TYPE (type);
12377 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12378 }
12379}
12380
c906108c
SS
12381/* Return the containing type of the die in question using its
12382 DW_AT_containing_type attribute. */
12383
12384static struct type *
e7c27a73 12385die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12386{
c906108c 12387 struct attribute *type_attr;
c906108c 12388
e142c38c 12389 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12390 if (!type_attr)
12391 error (_("Dwarf Error: Problem turning containing type into gdb type "
12392 "[in module %s]"), cu->objfile->name);
12393
673bfd45 12394 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12395}
12396
673bfd45
DE
12397/* Look up the type of DIE in CU using its type attribute ATTR.
12398 If there is no type substitute an error marker. */
12399
c906108c 12400static struct type *
673bfd45
DE
12401lookup_die_type (struct die_info *die, struct attribute *attr,
12402 struct dwarf2_cu *cu)
c906108c 12403{
bb5ed363 12404 struct objfile *objfile = cu->objfile;
f792889a
DJ
12405 struct type *this_type;
12406
673bfd45
DE
12407 /* First see if we have it cached. */
12408
12409 if (is_ref_attr (attr))
12410 {
b64f50a1 12411 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
12412
12413 this_type = get_die_type_at_offset (offset, cu->per_cu);
12414 }
55f1336d 12415 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12416 {
12417 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12418 struct dwarf2_cu *sig_cu;
b64f50a1 12419 sect_offset offset;
673bfd45
DE
12420
12421 /* sig_type will be NULL if the signatured type is missing from
12422 the debug info. */
12423 if (sig_type == NULL)
12424 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12425 "at 0x%x [in module %s]"),
b64f50a1 12426 die->offset.sect_off, objfile->name);
673bfd45 12427
b0df02fd 12428 gdb_assert (sig_type->per_cu.debug_types_section);
b64f50a1
JK
12429 offset.sect_off = (sig_type->per_cu.offset.sect_off
12430 + sig_type->type_offset.cu_off);
673bfd45
DE
12431 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12432 }
12433 else
12434 {
12435 dump_die_for_error (die);
12436 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12437 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12438 }
12439
12440 /* If not cached we need to read it in. */
12441
12442 if (this_type == NULL)
12443 {
12444 struct die_info *type_die;
12445 struct dwarf2_cu *type_cu = cu;
12446
12447 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12448 /* If the type is cached, we should have found it above. */
12449 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12450 this_type = read_type_die_1 (type_die, type_cu);
12451 }
12452
12453 /* If we still don't have a type use an error marker. */
12454
12455 if (this_type == NULL)
c906108c 12456 {
b00fdb78
TT
12457 char *message, *saved;
12458
12459 /* read_type_die already issued a complaint. */
12460 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12461 objfile->name,
b64f50a1
JK
12462 cu->header.offset.sect_off,
12463 die->offset.sect_off);
bb5ed363 12464 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12465 message, strlen (message));
12466 xfree (message);
12467
bb5ed363 12468 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12469 }
673bfd45 12470
f792889a 12471 return this_type;
c906108c
SS
12472}
12473
673bfd45
DE
12474/* Return the type in DIE, CU.
12475 Returns NULL for invalid types.
12476
12477 This first does a lookup in the appropriate type_hash table,
12478 and only reads the die in if necessary.
12479
12480 NOTE: This can be called when reading in partial or full symbols. */
12481
f792889a 12482static struct type *
e7c27a73 12483read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12484{
f792889a
DJ
12485 struct type *this_type;
12486
12487 this_type = get_die_type (die, cu);
12488 if (this_type)
12489 return this_type;
12490
673bfd45
DE
12491 return read_type_die_1 (die, cu);
12492}
12493
12494/* Read the type in DIE, CU.
12495 Returns NULL for invalid types. */
12496
12497static struct type *
12498read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12499{
12500 struct type *this_type = NULL;
12501
c906108c
SS
12502 switch (die->tag)
12503 {
12504 case DW_TAG_class_type:
680b30c7 12505 case DW_TAG_interface_type:
c906108c
SS
12506 case DW_TAG_structure_type:
12507 case DW_TAG_union_type:
f792889a 12508 this_type = read_structure_type (die, cu);
c906108c
SS
12509 break;
12510 case DW_TAG_enumeration_type:
f792889a 12511 this_type = read_enumeration_type (die, cu);
c906108c
SS
12512 break;
12513 case DW_TAG_subprogram:
12514 case DW_TAG_subroutine_type:
edb3359d 12515 case DW_TAG_inlined_subroutine:
f792889a 12516 this_type = read_subroutine_type (die, cu);
c906108c
SS
12517 break;
12518 case DW_TAG_array_type:
f792889a 12519 this_type = read_array_type (die, cu);
c906108c 12520 break;
72019c9c 12521 case DW_TAG_set_type:
f792889a 12522 this_type = read_set_type (die, cu);
72019c9c 12523 break;
c906108c 12524 case DW_TAG_pointer_type:
f792889a 12525 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12526 break;
12527 case DW_TAG_ptr_to_member_type:
f792889a 12528 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12529 break;
12530 case DW_TAG_reference_type:
f792889a 12531 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12532 break;
12533 case DW_TAG_const_type:
f792889a 12534 this_type = read_tag_const_type (die, cu);
c906108c
SS
12535 break;
12536 case DW_TAG_volatile_type:
f792889a 12537 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12538 break;
12539 case DW_TAG_string_type:
f792889a 12540 this_type = read_tag_string_type (die, cu);
c906108c
SS
12541 break;
12542 case DW_TAG_typedef:
f792889a 12543 this_type = read_typedef (die, cu);
c906108c 12544 break;
a02abb62 12545 case DW_TAG_subrange_type:
f792889a 12546 this_type = read_subrange_type (die, cu);
a02abb62 12547 break;
c906108c 12548 case DW_TAG_base_type:
f792889a 12549 this_type = read_base_type (die, cu);
c906108c 12550 break;
81a17f79 12551 case DW_TAG_unspecified_type:
f792889a 12552 this_type = read_unspecified_type (die, cu);
81a17f79 12553 break;
0114d602
DJ
12554 case DW_TAG_namespace:
12555 this_type = read_namespace_type (die, cu);
12556 break;
f55ee35c
JK
12557 case DW_TAG_module:
12558 this_type = read_module_type (die, cu);
12559 break;
c906108c 12560 default:
3e43a32a
MS
12561 complaint (&symfile_complaints,
12562 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12563 dwarf_tag_name (die->tag));
c906108c
SS
12564 break;
12565 }
63d06c5c 12566
f792889a 12567 return this_type;
63d06c5c
DC
12568}
12569
abc72ce4
DE
12570/* See if we can figure out if the class lives in a namespace. We do
12571 this by looking for a member function; its demangled name will
12572 contain namespace info, if there is any.
12573 Return the computed name or NULL.
12574 Space for the result is allocated on the objfile's obstack.
12575 This is the full-die version of guess_partial_die_structure_name.
12576 In this case we know DIE has no useful parent. */
12577
12578static char *
12579guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12580{
12581 struct die_info *spec_die;
12582 struct dwarf2_cu *spec_cu;
12583 struct die_info *child;
12584
12585 spec_cu = cu;
12586 spec_die = die_specification (die, &spec_cu);
12587 if (spec_die != NULL)
12588 {
12589 die = spec_die;
12590 cu = spec_cu;
12591 }
12592
12593 for (child = die->child;
12594 child != NULL;
12595 child = child->sibling)
12596 {
12597 if (child->tag == DW_TAG_subprogram)
12598 {
12599 struct attribute *attr;
12600
12601 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12602 if (attr == NULL)
12603 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12604 if (attr != NULL)
12605 {
12606 char *actual_name
12607 = language_class_name_from_physname (cu->language_defn,
12608 DW_STRING (attr));
12609 char *name = NULL;
12610
12611 if (actual_name != NULL)
12612 {
12613 char *die_name = dwarf2_name (die, cu);
12614
12615 if (die_name != NULL
12616 && strcmp (die_name, actual_name) != 0)
12617 {
12618 /* Strip off the class name from the full name.
12619 We want the prefix. */
12620 int die_name_len = strlen (die_name);
12621 int actual_name_len = strlen (actual_name);
12622
12623 /* Test for '::' as a sanity check. */
12624 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12625 && actual_name[actual_name_len
12626 - die_name_len - 1] == ':')
abc72ce4
DE
12627 name =
12628 obsavestring (actual_name,
12629 actual_name_len - die_name_len - 2,
12630 &cu->objfile->objfile_obstack);
12631 }
12632 }
12633 xfree (actual_name);
12634 return name;
12635 }
12636 }
12637 }
12638
12639 return NULL;
12640}
12641
96408a79
SA
12642/* GCC might emit a nameless typedef that has a linkage name. Determine the
12643 prefix part in such case. See
12644 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12645
12646static char *
12647anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12648{
12649 struct attribute *attr;
12650 char *base;
12651
12652 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12653 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12654 return NULL;
12655
12656 attr = dwarf2_attr (die, DW_AT_name, cu);
12657 if (attr != NULL && DW_STRING (attr) != NULL)
12658 return NULL;
12659
12660 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12661 if (attr == NULL)
12662 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12663 if (attr == NULL || DW_STRING (attr) == NULL)
12664 return NULL;
12665
12666 /* dwarf2_name had to be already called. */
12667 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12668
12669 /* Strip the base name, keep any leading namespaces/classes. */
12670 base = strrchr (DW_STRING (attr), ':');
12671 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12672 return "";
12673
12674 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12675 &cu->objfile->objfile_obstack);
12676}
12677
fdde2d81 12678/* Return the name of the namespace/class that DIE is defined within,
0114d602 12679 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12680
0114d602
DJ
12681 For example, if we're within the method foo() in the following
12682 code:
12683
12684 namespace N {
12685 class C {
12686 void foo () {
12687 }
12688 };
12689 }
12690
12691 then determine_prefix on foo's die will return "N::C". */
fdde2d81 12692
0d5cff50 12693static const char *
e142c38c 12694determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12695{
0114d602
DJ
12696 struct die_info *parent, *spec_die;
12697 struct dwarf2_cu *spec_cu;
12698 struct type *parent_type;
96408a79 12699 char *retval;
63d06c5c 12700
f55ee35c
JK
12701 if (cu->language != language_cplus && cu->language != language_java
12702 && cu->language != language_fortran)
0114d602
DJ
12703 return "";
12704
96408a79
SA
12705 retval = anonymous_struct_prefix (die, cu);
12706 if (retval)
12707 return retval;
12708
0114d602
DJ
12709 /* We have to be careful in the presence of DW_AT_specification.
12710 For example, with GCC 3.4, given the code
12711
12712 namespace N {
12713 void foo() {
12714 // Definition of N::foo.
12715 }
12716 }
12717
12718 then we'll have a tree of DIEs like this:
12719
12720 1: DW_TAG_compile_unit
12721 2: DW_TAG_namespace // N
12722 3: DW_TAG_subprogram // declaration of N::foo
12723 4: DW_TAG_subprogram // definition of N::foo
12724 DW_AT_specification // refers to die #3
12725
12726 Thus, when processing die #4, we have to pretend that we're in
12727 the context of its DW_AT_specification, namely the contex of die
12728 #3. */
12729 spec_cu = cu;
12730 spec_die = die_specification (die, &spec_cu);
12731 if (spec_die == NULL)
12732 parent = die->parent;
12733 else
63d06c5c 12734 {
0114d602
DJ
12735 parent = spec_die->parent;
12736 cu = spec_cu;
63d06c5c 12737 }
0114d602
DJ
12738
12739 if (parent == NULL)
12740 return "";
98bfdba5
PA
12741 else if (parent->building_fullname)
12742 {
12743 const char *name;
12744 const char *parent_name;
12745
12746 /* It has been seen on RealView 2.2 built binaries,
12747 DW_TAG_template_type_param types actually _defined_ as
12748 children of the parent class:
12749
12750 enum E {};
12751 template class <class Enum> Class{};
12752 Class<enum E> class_e;
12753
12754 1: DW_TAG_class_type (Class)
12755 2: DW_TAG_enumeration_type (E)
12756 3: DW_TAG_enumerator (enum1:0)
12757 3: DW_TAG_enumerator (enum2:1)
12758 ...
12759 2: DW_TAG_template_type_param
12760 DW_AT_type DW_FORM_ref_udata (E)
12761
12762 Besides being broken debug info, it can put GDB into an
12763 infinite loop. Consider:
12764
12765 When we're building the full name for Class<E>, we'll start
12766 at Class, and go look over its template type parameters,
12767 finding E. We'll then try to build the full name of E, and
12768 reach here. We're now trying to build the full name of E,
12769 and look over the parent DIE for containing scope. In the
12770 broken case, if we followed the parent DIE of E, we'd again
12771 find Class, and once again go look at its template type
12772 arguments, etc., etc. Simply don't consider such parent die
12773 as source-level parent of this die (it can't be, the language
12774 doesn't allow it), and break the loop here. */
12775 name = dwarf2_name (die, cu);
12776 parent_name = dwarf2_name (parent, cu);
12777 complaint (&symfile_complaints,
12778 _("template param type '%s' defined within parent '%s'"),
12779 name ? name : "<unknown>",
12780 parent_name ? parent_name : "<unknown>");
12781 return "";
12782 }
63d06c5c 12783 else
0114d602
DJ
12784 switch (parent->tag)
12785 {
63d06c5c 12786 case DW_TAG_namespace:
0114d602 12787 parent_type = read_type_die (parent, cu);
acebe513
UW
12788 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12789 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12790 Work around this problem here. */
12791 if (cu->language == language_cplus
12792 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12793 return "";
0114d602
DJ
12794 /* We give a name to even anonymous namespaces. */
12795 return TYPE_TAG_NAME (parent_type);
63d06c5c 12796 case DW_TAG_class_type:
680b30c7 12797 case DW_TAG_interface_type:
63d06c5c 12798 case DW_TAG_structure_type:
0114d602 12799 case DW_TAG_union_type:
f55ee35c 12800 case DW_TAG_module:
0114d602
DJ
12801 parent_type = read_type_die (parent, cu);
12802 if (TYPE_TAG_NAME (parent_type) != NULL)
12803 return TYPE_TAG_NAME (parent_type);
12804 else
12805 /* An anonymous structure is only allowed non-static data
12806 members; no typedefs, no member functions, et cetera.
12807 So it does not need a prefix. */
12808 return "";
abc72ce4
DE
12809 case DW_TAG_compile_unit:
12810 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12811 if (cu->language == language_cplus
8b70b953 12812 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12813 && die->child != NULL
12814 && (die->tag == DW_TAG_class_type
12815 || die->tag == DW_TAG_structure_type
12816 || die->tag == DW_TAG_union_type))
12817 {
12818 char *name = guess_full_die_structure_name (die, cu);
12819 if (name != NULL)
12820 return name;
12821 }
12822 return "";
63d06c5c 12823 default:
8176b9b8 12824 return determine_prefix (parent, cu);
63d06c5c 12825 }
63d06c5c
DC
12826}
12827
3e43a32a
MS
12828/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12829 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12830 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12831 an obconcat, otherwise allocate storage for the result. The CU argument is
12832 used to determine the language and hence, the appropriate separator. */
987504bb 12833
f55ee35c 12834#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12835
12836static char *
f55ee35c
JK
12837typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12838 int physname, struct dwarf2_cu *cu)
63d06c5c 12839{
f55ee35c 12840 const char *lead = "";
5c315b68 12841 const char *sep;
63d06c5c 12842
3e43a32a
MS
12843 if (suffix == NULL || suffix[0] == '\0'
12844 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12845 sep = "";
12846 else if (cu->language == language_java)
12847 sep = ".";
f55ee35c
JK
12848 else if (cu->language == language_fortran && physname)
12849 {
12850 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12851 DW_AT_MIPS_linkage_name is preferred and used instead. */
12852
12853 lead = "__";
12854 sep = "_MOD_";
12855 }
987504bb
JJ
12856 else
12857 sep = "::";
63d06c5c 12858
6dd47d34
DE
12859 if (prefix == NULL)
12860 prefix = "";
12861 if (suffix == NULL)
12862 suffix = "";
12863
987504bb
JJ
12864 if (obs == NULL)
12865 {
3e43a32a
MS
12866 char *retval
12867 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12868
f55ee35c
JK
12869 strcpy (retval, lead);
12870 strcat (retval, prefix);
6dd47d34
DE
12871 strcat (retval, sep);
12872 strcat (retval, suffix);
63d06c5c
DC
12873 return retval;
12874 }
987504bb
JJ
12875 else
12876 {
12877 /* We have an obstack. */
f55ee35c 12878 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12879 }
63d06c5c
DC
12880}
12881
c906108c
SS
12882/* Return sibling of die, NULL if no sibling. */
12883
f9aca02d 12884static struct die_info *
fba45db2 12885sibling_die (struct die_info *die)
c906108c 12886{
639d11d3 12887 return die->sibling;
c906108c
SS
12888}
12889
71c25dea
TT
12890/* Get name of a die, return NULL if not found. */
12891
12892static char *
12893dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12894 struct obstack *obstack)
12895{
12896 if (name && cu->language == language_cplus)
12897 {
12898 char *canon_name = cp_canonicalize_string (name);
12899
12900 if (canon_name != NULL)
12901 {
12902 if (strcmp (canon_name, name) != 0)
12903 name = obsavestring (canon_name, strlen (canon_name),
12904 obstack);
12905 xfree (canon_name);
12906 }
12907 }
12908
12909 return name;
c906108c
SS
12910}
12911
9219021c
DC
12912/* Get name of a die, return NULL if not found. */
12913
12914static char *
e142c38c 12915dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12916{
12917 struct attribute *attr;
12918
e142c38c 12919 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12920 if ((!attr || !DW_STRING (attr))
12921 && die->tag != DW_TAG_class_type
12922 && die->tag != DW_TAG_interface_type
12923 && die->tag != DW_TAG_structure_type
12924 && die->tag != DW_TAG_union_type)
71c25dea
TT
12925 return NULL;
12926
12927 switch (die->tag)
12928 {
12929 case DW_TAG_compile_unit:
12930 /* Compilation units have a DW_AT_name that is a filename, not
12931 a source language identifier. */
12932 case DW_TAG_enumeration_type:
12933 case DW_TAG_enumerator:
12934 /* These tags always have simple identifiers already; no need
12935 to canonicalize them. */
12936 return DW_STRING (attr);
907af001 12937
418835cc
KS
12938 case DW_TAG_subprogram:
12939 /* Java constructors will all be named "<init>", so return
12940 the class name when we see this special case. */
12941 if (cu->language == language_java
12942 && DW_STRING (attr) != NULL
12943 && strcmp (DW_STRING (attr), "<init>") == 0)
12944 {
12945 struct dwarf2_cu *spec_cu = cu;
12946 struct die_info *spec_die;
12947
12948 /* GCJ will output '<init>' for Java constructor names.
12949 For this special case, return the name of the parent class. */
12950
12951 /* GCJ may output suprogram DIEs with AT_specification set.
12952 If so, use the name of the specified DIE. */
12953 spec_die = die_specification (die, &spec_cu);
12954 if (spec_die != NULL)
12955 return dwarf2_name (spec_die, spec_cu);
12956
12957 do
12958 {
12959 die = die->parent;
12960 if (die->tag == DW_TAG_class_type)
12961 return dwarf2_name (die, cu);
12962 }
12963 while (die->tag != DW_TAG_compile_unit);
12964 }
907af001
UW
12965 break;
12966
12967 case DW_TAG_class_type:
12968 case DW_TAG_interface_type:
12969 case DW_TAG_structure_type:
12970 case DW_TAG_union_type:
12971 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12972 structures or unions. These were of the form "._%d" in GCC 4.1,
12973 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12974 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12975 if (attr && DW_STRING (attr)
12976 && (strncmp (DW_STRING (attr), "._", 2) == 0
12977 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12978 return NULL;
53832f31
TT
12979
12980 /* GCC might emit a nameless typedef that has a linkage name. See
12981 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12982 if (!attr || DW_STRING (attr) == NULL)
12983 {
df5c6c50 12984 char *demangled = NULL;
53832f31
TT
12985
12986 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12987 if (attr == NULL)
12988 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12989
12990 if (attr == NULL || DW_STRING (attr) == NULL)
12991 return NULL;
12992
df5c6c50
JK
12993 /* Avoid demangling DW_STRING (attr) the second time on a second
12994 call for the same DIE. */
12995 if (!DW_STRING_IS_CANONICAL (attr))
12996 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12997
12998 if (demangled)
12999 {
96408a79
SA
13000 char *base;
13001
53832f31 13002 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
13003 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
13004 &cu->objfile->objfile_obstack);
53832f31
TT
13005 DW_STRING_IS_CANONICAL (attr) = 1;
13006 xfree (demangled);
96408a79
SA
13007
13008 /* Strip any leading namespaces/classes, keep only the base name.
13009 DW_AT_name for named DIEs does not contain the prefixes. */
13010 base = strrchr (DW_STRING (attr), ':');
13011 if (base && base > DW_STRING (attr) && base[-1] == ':')
13012 return &base[1];
13013 else
13014 return DW_STRING (attr);
53832f31
TT
13015 }
13016 }
907af001
UW
13017 break;
13018
71c25dea 13019 default:
907af001
UW
13020 break;
13021 }
13022
13023 if (!DW_STRING_IS_CANONICAL (attr))
13024 {
13025 DW_STRING (attr)
13026 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
13027 &cu->objfile->objfile_obstack);
13028 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 13029 }
907af001 13030 return DW_STRING (attr);
9219021c
DC
13031}
13032
13033/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
13034 is none. *EXT_CU is the CU containing DIE on input, and the CU
13035 containing the return value on output. */
9219021c
DC
13036
13037static struct die_info *
f2f0e013 13038dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
13039{
13040 struct attribute *attr;
9219021c 13041
f2f0e013 13042 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
13043 if (attr == NULL)
13044 return NULL;
13045
f2f0e013 13046 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
13047}
13048
c906108c
SS
13049/* Convert a DIE tag into its string name. */
13050
13051static char *
aa1ee363 13052dwarf_tag_name (unsigned tag)
c906108c
SS
13053{
13054 switch (tag)
13055 {
13056 case DW_TAG_padding:
13057 return "DW_TAG_padding";
13058 case DW_TAG_array_type:
13059 return "DW_TAG_array_type";
13060 case DW_TAG_class_type:
13061 return "DW_TAG_class_type";
13062 case DW_TAG_entry_point:
13063 return "DW_TAG_entry_point";
13064 case DW_TAG_enumeration_type:
13065 return "DW_TAG_enumeration_type";
13066 case DW_TAG_formal_parameter:
13067 return "DW_TAG_formal_parameter";
13068 case DW_TAG_imported_declaration:
13069 return "DW_TAG_imported_declaration";
13070 case DW_TAG_label:
13071 return "DW_TAG_label";
13072 case DW_TAG_lexical_block:
13073 return "DW_TAG_lexical_block";
13074 case DW_TAG_member:
13075 return "DW_TAG_member";
13076 case DW_TAG_pointer_type:
13077 return "DW_TAG_pointer_type";
13078 case DW_TAG_reference_type:
13079 return "DW_TAG_reference_type";
13080 case DW_TAG_compile_unit:
13081 return "DW_TAG_compile_unit";
13082 case DW_TAG_string_type:
13083 return "DW_TAG_string_type";
13084 case DW_TAG_structure_type:
13085 return "DW_TAG_structure_type";
13086 case DW_TAG_subroutine_type:
13087 return "DW_TAG_subroutine_type";
13088 case DW_TAG_typedef:
13089 return "DW_TAG_typedef";
13090 case DW_TAG_union_type:
13091 return "DW_TAG_union_type";
13092 case DW_TAG_unspecified_parameters:
13093 return "DW_TAG_unspecified_parameters";
13094 case DW_TAG_variant:
13095 return "DW_TAG_variant";
13096 case DW_TAG_common_block:
13097 return "DW_TAG_common_block";
13098 case DW_TAG_common_inclusion:
13099 return "DW_TAG_common_inclusion";
13100 case DW_TAG_inheritance:
13101 return "DW_TAG_inheritance";
13102 case DW_TAG_inlined_subroutine:
13103 return "DW_TAG_inlined_subroutine";
13104 case DW_TAG_module:
13105 return "DW_TAG_module";
13106 case DW_TAG_ptr_to_member_type:
13107 return "DW_TAG_ptr_to_member_type";
13108 case DW_TAG_set_type:
13109 return "DW_TAG_set_type";
13110 case DW_TAG_subrange_type:
13111 return "DW_TAG_subrange_type";
13112 case DW_TAG_with_stmt:
13113 return "DW_TAG_with_stmt";
13114 case DW_TAG_access_declaration:
13115 return "DW_TAG_access_declaration";
13116 case DW_TAG_base_type:
13117 return "DW_TAG_base_type";
13118 case DW_TAG_catch_block:
13119 return "DW_TAG_catch_block";
13120 case DW_TAG_const_type:
13121 return "DW_TAG_const_type";
13122 case DW_TAG_constant:
13123 return "DW_TAG_constant";
13124 case DW_TAG_enumerator:
13125 return "DW_TAG_enumerator";
13126 case DW_TAG_file_type:
13127 return "DW_TAG_file_type";
13128 case DW_TAG_friend:
13129 return "DW_TAG_friend";
13130 case DW_TAG_namelist:
13131 return "DW_TAG_namelist";
13132 case DW_TAG_namelist_item:
13133 return "DW_TAG_namelist_item";
13134 case DW_TAG_packed_type:
13135 return "DW_TAG_packed_type";
13136 case DW_TAG_subprogram:
13137 return "DW_TAG_subprogram";
13138 case DW_TAG_template_type_param:
13139 return "DW_TAG_template_type_param";
13140 case DW_TAG_template_value_param:
13141 return "DW_TAG_template_value_param";
13142 case DW_TAG_thrown_type:
13143 return "DW_TAG_thrown_type";
13144 case DW_TAG_try_block:
13145 return "DW_TAG_try_block";
13146 case DW_TAG_variant_part:
13147 return "DW_TAG_variant_part";
13148 case DW_TAG_variable:
13149 return "DW_TAG_variable";
13150 case DW_TAG_volatile_type:
13151 return "DW_TAG_volatile_type";
d9fa45fe
DC
13152 case DW_TAG_dwarf_procedure:
13153 return "DW_TAG_dwarf_procedure";
13154 case DW_TAG_restrict_type:
13155 return "DW_TAG_restrict_type";
13156 case DW_TAG_interface_type:
13157 return "DW_TAG_interface_type";
13158 case DW_TAG_namespace:
13159 return "DW_TAG_namespace";
13160 case DW_TAG_imported_module:
13161 return "DW_TAG_imported_module";
13162 case DW_TAG_unspecified_type:
13163 return "DW_TAG_unspecified_type";
13164 case DW_TAG_partial_unit:
13165 return "DW_TAG_partial_unit";
13166 case DW_TAG_imported_unit:
13167 return "DW_TAG_imported_unit";
b7619582
GF
13168 case DW_TAG_condition:
13169 return "DW_TAG_condition";
13170 case DW_TAG_shared_type:
13171 return "DW_TAG_shared_type";
348e048f
DE
13172 case DW_TAG_type_unit:
13173 return "DW_TAG_type_unit";
c906108c
SS
13174 case DW_TAG_MIPS_loop:
13175 return "DW_TAG_MIPS_loop";
b7619582
GF
13176 case DW_TAG_HP_array_descriptor:
13177 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13178 case DW_TAG_format_label:
13179 return "DW_TAG_format_label";
13180 case DW_TAG_function_template:
13181 return "DW_TAG_function_template";
13182 case DW_TAG_class_template:
13183 return "DW_TAG_class_template";
b7619582
GF
13184 case DW_TAG_GNU_BINCL:
13185 return "DW_TAG_GNU_BINCL";
13186 case DW_TAG_GNU_EINCL:
13187 return "DW_TAG_GNU_EINCL";
13188 case DW_TAG_upc_shared_type:
13189 return "DW_TAG_upc_shared_type";
13190 case DW_TAG_upc_strict_type:
13191 return "DW_TAG_upc_strict_type";
13192 case DW_TAG_upc_relaxed_type:
13193 return "DW_TAG_upc_relaxed_type";
13194 case DW_TAG_PGI_kanji_type:
13195 return "DW_TAG_PGI_kanji_type";
13196 case DW_TAG_PGI_interface_block:
13197 return "DW_TAG_PGI_interface_block";
96408a79
SA
13198 case DW_TAG_GNU_call_site:
13199 return "DW_TAG_GNU_call_site";
c906108c
SS
13200 default:
13201 return "DW_TAG_<unknown>";
13202 }
13203}
13204
13205/* Convert a DWARF attribute code into its string name. */
13206
13207static char *
aa1ee363 13208dwarf_attr_name (unsigned attr)
c906108c
SS
13209{
13210 switch (attr)
13211 {
13212 case DW_AT_sibling:
13213 return "DW_AT_sibling";
13214 case DW_AT_location:
13215 return "DW_AT_location";
13216 case DW_AT_name:
13217 return "DW_AT_name";
13218 case DW_AT_ordering:
13219 return "DW_AT_ordering";
13220 case DW_AT_subscr_data:
13221 return "DW_AT_subscr_data";
13222 case DW_AT_byte_size:
13223 return "DW_AT_byte_size";
13224 case DW_AT_bit_offset:
13225 return "DW_AT_bit_offset";
13226 case DW_AT_bit_size:
13227 return "DW_AT_bit_size";
13228 case DW_AT_element_list:
13229 return "DW_AT_element_list";
13230 case DW_AT_stmt_list:
13231 return "DW_AT_stmt_list";
13232 case DW_AT_low_pc:
13233 return "DW_AT_low_pc";
13234 case DW_AT_high_pc:
13235 return "DW_AT_high_pc";
13236 case DW_AT_language:
13237 return "DW_AT_language";
13238 case DW_AT_member:
13239 return "DW_AT_member";
13240 case DW_AT_discr:
13241 return "DW_AT_discr";
13242 case DW_AT_discr_value:
13243 return "DW_AT_discr_value";
13244 case DW_AT_visibility:
13245 return "DW_AT_visibility";
13246 case DW_AT_import:
13247 return "DW_AT_import";
13248 case DW_AT_string_length:
13249 return "DW_AT_string_length";
13250 case DW_AT_common_reference:
13251 return "DW_AT_common_reference";
13252 case DW_AT_comp_dir:
13253 return "DW_AT_comp_dir";
13254 case DW_AT_const_value:
13255 return "DW_AT_const_value";
13256 case DW_AT_containing_type:
13257 return "DW_AT_containing_type";
13258 case DW_AT_default_value:
13259 return "DW_AT_default_value";
13260 case DW_AT_inline:
13261 return "DW_AT_inline";
13262 case DW_AT_is_optional:
13263 return "DW_AT_is_optional";
13264 case DW_AT_lower_bound:
13265 return "DW_AT_lower_bound";
13266 case DW_AT_producer:
13267 return "DW_AT_producer";
13268 case DW_AT_prototyped:
13269 return "DW_AT_prototyped";
13270 case DW_AT_return_addr:
13271 return "DW_AT_return_addr";
13272 case DW_AT_start_scope:
13273 return "DW_AT_start_scope";
09fa0d7c
JK
13274 case DW_AT_bit_stride:
13275 return "DW_AT_bit_stride";
c906108c
SS
13276 case DW_AT_upper_bound:
13277 return "DW_AT_upper_bound";
13278 case DW_AT_abstract_origin:
13279 return "DW_AT_abstract_origin";
13280 case DW_AT_accessibility:
13281 return "DW_AT_accessibility";
13282 case DW_AT_address_class:
13283 return "DW_AT_address_class";
13284 case DW_AT_artificial:
13285 return "DW_AT_artificial";
13286 case DW_AT_base_types:
13287 return "DW_AT_base_types";
13288 case DW_AT_calling_convention:
13289 return "DW_AT_calling_convention";
13290 case DW_AT_count:
13291 return "DW_AT_count";
13292 case DW_AT_data_member_location:
13293 return "DW_AT_data_member_location";
13294 case DW_AT_decl_column:
13295 return "DW_AT_decl_column";
13296 case DW_AT_decl_file:
13297 return "DW_AT_decl_file";
13298 case DW_AT_decl_line:
13299 return "DW_AT_decl_line";
13300 case DW_AT_declaration:
13301 return "DW_AT_declaration";
13302 case DW_AT_discr_list:
13303 return "DW_AT_discr_list";
13304 case DW_AT_encoding:
13305 return "DW_AT_encoding";
13306 case DW_AT_external:
13307 return "DW_AT_external";
13308 case DW_AT_frame_base:
13309 return "DW_AT_frame_base";
13310 case DW_AT_friend:
13311 return "DW_AT_friend";
13312 case DW_AT_identifier_case:
13313 return "DW_AT_identifier_case";
13314 case DW_AT_macro_info:
13315 return "DW_AT_macro_info";
13316 case DW_AT_namelist_items:
13317 return "DW_AT_namelist_items";
13318 case DW_AT_priority:
13319 return "DW_AT_priority";
13320 case DW_AT_segment:
13321 return "DW_AT_segment";
13322 case DW_AT_specification:
13323 return "DW_AT_specification";
13324 case DW_AT_static_link:
13325 return "DW_AT_static_link";
13326 case DW_AT_type:
13327 return "DW_AT_type";
13328 case DW_AT_use_location:
13329 return "DW_AT_use_location";
13330 case DW_AT_variable_parameter:
13331 return "DW_AT_variable_parameter";
13332 case DW_AT_virtuality:
13333 return "DW_AT_virtuality";
13334 case DW_AT_vtable_elem_location:
13335 return "DW_AT_vtable_elem_location";
b7619582 13336 /* DWARF 3 values. */
d9fa45fe
DC
13337 case DW_AT_allocated:
13338 return "DW_AT_allocated";
13339 case DW_AT_associated:
13340 return "DW_AT_associated";
13341 case DW_AT_data_location:
13342 return "DW_AT_data_location";
09fa0d7c
JK
13343 case DW_AT_byte_stride:
13344 return "DW_AT_byte_stride";
d9fa45fe
DC
13345 case DW_AT_entry_pc:
13346 return "DW_AT_entry_pc";
13347 case DW_AT_use_UTF8:
13348 return "DW_AT_use_UTF8";
13349 case DW_AT_extension:
13350 return "DW_AT_extension";
13351 case DW_AT_ranges:
13352 return "DW_AT_ranges";
13353 case DW_AT_trampoline:
13354 return "DW_AT_trampoline";
13355 case DW_AT_call_column:
13356 return "DW_AT_call_column";
13357 case DW_AT_call_file:
13358 return "DW_AT_call_file";
13359 case DW_AT_call_line:
13360 return "DW_AT_call_line";
b7619582
GF
13361 case DW_AT_description:
13362 return "DW_AT_description";
13363 case DW_AT_binary_scale:
13364 return "DW_AT_binary_scale";
13365 case DW_AT_decimal_scale:
13366 return "DW_AT_decimal_scale";
13367 case DW_AT_small:
13368 return "DW_AT_small";
13369 case DW_AT_decimal_sign:
13370 return "DW_AT_decimal_sign";
13371 case DW_AT_digit_count:
13372 return "DW_AT_digit_count";
13373 case DW_AT_picture_string:
13374 return "DW_AT_picture_string";
13375 case DW_AT_mutable:
13376 return "DW_AT_mutable";
13377 case DW_AT_threads_scaled:
13378 return "DW_AT_threads_scaled";
13379 case DW_AT_explicit:
13380 return "DW_AT_explicit";
13381 case DW_AT_object_pointer:
13382 return "DW_AT_object_pointer";
13383 case DW_AT_endianity:
13384 return "DW_AT_endianity";
13385 case DW_AT_elemental:
13386 return "DW_AT_elemental";
13387 case DW_AT_pure:
13388 return "DW_AT_pure";
13389 case DW_AT_recursive:
13390 return "DW_AT_recursive";
348e048f
DE
13391 /* DWARF 4 values. */
13392 case DW_AT_signature:
13393 return "DW_AT_signature";
31ef98ae
TT
13394 case DW_AT_linkage_name:
13395 return "DW_AT_linkage_name";
b7619582 13396 /* SGI/MIPS extensions. */
c764a876 13397#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13398 case DW_AT_MIPS_fde:
13399 return "DW_AT_MIPS_fde";
c764a876 13400#endif
c906108c
SS
13401 case DW_AT_MIPS_loop_begin:
13402 return "DW_AT_MIPS_loop_begin";
13403 case DW_AT_MIPS_tail_loop_begin:
13404 return "DW_AT_MIPS_tail_loop_begin";
13405 case DW_AT_MIPS_epilog_begin:
13406 return "DW_AT_MIPS_epilog_begin";
13407 case DW_AT_MIPS_loop_unroll_factor:
13408 return "DW_AT_MIPS_loop_unroll_factor";
13409 case DW_AT_MIPS_software_pipeline_depth:
13410 return "DW_AT_MIPS_software_pipeline_depth";
13411 case DW_AT_MIPS_linkage_name:
13412 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13413 case DW_AT_MIPS_stride:
13414 return "DW_AT_MIPS_stride";
13415 case DW_AT_MIPS_abstract_name:
13416 return "DW_AT_MIPS_abstract_name";
13417 case DW_AT_MIPS_clone_origin:
13418 return "DW_AT_MIPS_clone_origin";
13419 case DW_AT_MIPS_has_inlines:
13420 return "DW_AT_MIPS_has_inlines";
b7619582 13421 /* HP extensions. */
c764a876 13422#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13423 case DW_AT_HP_block_index:
13424 return "DW_AT_HP_block_index";
c764a876 13425#endif
b7619582
GF
13426 case DW_AT_HP_unmodifiable:
13427 return "DW_AT_HP_unmodifiable";
13428 case DW_AT_HP_actuals_stmt_list:
13429 return "DW_AT_HP_actuals_stmt_list";
13430 case DW_AT_HP_proc_per_section:
13431 return "DW_AT_HP_proc_per_section";
13432 case DW_AT_HP_raw_data_ptr:
13433 return "DW_AT_HP_raw_data_ptr";
13434 case DW_AT_HP_pass_by_reference:
13435 return "DW_AT_HP_pass_by_reference";
13436 case DW_AT_HP_opt_level:
13437 return "DW_AT_HP_opt_level";
13438 case DW_AT_HP_prof_version_id:
13439 return "DW_AT_HP_prof_version_id";
13440 case DW_AT_HP_opt_flags:
13441 return "DW_AT_HP_opt_flags";
13442 case DW_AT_HP_cold_region_low_pc:
13443 return "DW_AT_HP_cold_region_low_pc";
13444 case DW_AT_HP_cold_region_high_pc:
13445 return "DW_AT_HP_cold_region_high_pc";
13446 case DW_AT_HP_all_variables_modifiable:
13447 return "DW_AT_HP_all_variables_modifiable";
13448 case DW_AT_HP_linkage_name:
13449 return "DW_AT_HP_linkage_name";
13450 case DW_AT_HP_prof_flags:
13451 return "DW_AT_HP_prof_flags";
13452 /* GNU extensions. */
c906108c
SS
13453 case DW_AT_sf_names:
13454 return "DW_AT_sf_names";
13455 case DW_AT_src_info:
13456 return "DW_AT_src_info";
13457 case DW_AT_mac_info:
13458 return "DW_AT_mac_info";
13459 case DW_AT_src_coords:
13460 return "DW_AT_src_coords";
13461 case DW_AT_body_begin:
13462 return "DW_AT_body_begin";
13463 case DW_AT_body_end:
13464 return "DW_AT_body_end";
f5f8a009
EZ
13465 case DW_AT_GNU_vector:
13466 return "DW_AT_GNU_vector";
2de00c64
DE
13467 case DW_AT_GNU_odr_signature:
13468 return "DW_AT_GNU_odr_signature";
b7619582
GF
13469 /* VMS extensions. */
13470 case DW_AT_VMS_rtnbeg_pd_address:
13471 return "DW_AT_VMS_rtnbeg_pd_address";
13472 /* UPC extension. */
13473 case DW_AT_upc_threads_scaled:
13474 return "DW_AT_upc_threads_scaled";
13475 /* PGI (STMicroelectronics) extensions. */
13476 case DW_AT_PGI_lbase:
13477 return "DW_AT_PGI_lbase";
13478 case DW_AT_PGI_soffset:
13479 return "DW_AT_PGI_soffset";
13480 case DW_AT_PGI_lstride:
13481 return "DW_AT_PGI_lstride";
c906108c
SS
13482 default:
13483 return "DW_AT_<unknown>";
13484 }
13485}
13486
13487/* Convert a DWARF value form code into its string name. */
13488
13489static char *
aa1ee363 13490dwarf_form_name (unsigned form)
c906108c
SS
13491{
13492 switch (form)
13493 {
13494 case DW_FORM_addr:
13495 return "DW_FORM_addr";
13496 case DW_FORM_block2:
13497 return "DW_FORM_block2";
13498 case DW_FORM_block4:
13499 return "DW_FORM_block4";
13500 case DW_FORM_data2:
13501 return "DW_FORM_data2";
13502 case DW_FORM_data4:
13503 return "DW_FORM_data4";
13504 case DW_FORM_data8:
13505 return "DW_FORM_data8";
13506 case DW_FORM_string:
13507 return "DW_FORM_string";
13508 case DW_FORM_block:
13509 return "DW_FORM_block";
13510 case DW_FORM_block1:
13511 return "DW_FORM_block1";
13512 case DW_FORM_data1:
13513 return "DW_FORM_data1";
13514 case DW_FORM_flag:
13515 return "DW_FORM_flag";
13516 case DW_FORM_sdata:
13517 return "DW_FORM_sdata";
13518 case DW_FORM_strp:
13519 return "DW_FORM_strp";
13520 case DW_FORM_udata:
13521 return "DW_FORM_udata";
13522 case DW_FORM_ref_addr:
13523 return "DW_FORM_ref_addr";
13524 case DW_FORM_ref1:
13525 return "DW_FORM_ref1";
13526 case DW_FORM_ref2:
13527 return "DW_FORM_ref2";
13528 case DW_FORM_ref4:
13529 return "DW_FORM_ref4";
13530 case DW_FORM_ref8:
13531 return "DW_FORM_ref8";
13532 case DW_FORM_ref_udata:
13533 return "DW_FORM_ref_udata";
13534 case DW_FORM_indirect:
13535 return "DW_FORM_indirect";
348e048f
DE
13536 case DW_FORM_sec_offset:
13537 return "DW_FORM_sec_offset";
13538 case DW_FORM_exprloc:
13539 return "DW_FORM_exprloc";
13540 case DW_FORM_flag_present:
13541 return "DW_FORM_flag_present";
55f1336d
TT
13542 case DW_FORM_ref_sig8:
13543 return "DW_FORM_ref_sig8";
c906108c
SS
13544 default:
13545 return "DW_FORM_<unknown>";
13546 }
13547}
13548
13549/* Convert a DWARF stack opcode into its string name. */
13550
9eae7c52 13551const char *
b1bfef65 13552dwarf_stack_op_name (unsigned op)
c906108c
SS
13553{
13554 switch (op)
13555 {
13556 case DW_OP_addr:
13557 return "DW_OP_addr";
13558 case DW_OP_deref:
13559 return "DW_OP_deref";
13560 case DW_OP_const1u:
13561 return "DW_OP_const1u";
13562 case DW_OP_const1s:
13563 return "DW_OP_const1s";
13564 case DW_OP_const2u:
13565 return "DW_OP_const2u";
13566 case DW_OP_const2s:
13567 return "DW_OP_const2s";
13568 case DW_OP_const4u:
13569 return "DW_OP_const4u";
13570 case DW_OP_const4s:
13571 return "DW_OP_const4s";
13572 case DW_OP_const8u:
13573 return "DW_OP_const8u";
13574 case DW_OP_const8s:
13575 return "DW_OP_const8s";
13576 case DW_OP_constu:
13577 return "DW_OP_constu";
13578 case DW_OP_consts:
13579 return "DW_OP_consts";
13580 case DW_OP_dup:
13581 return "DW_OP_dup";
13582 case DW_OP_drop:
13583 return "DW_OP_drop";
13584 case DW_OP_over:
13585 return "DW_OP_over";
13586 case DW_OP_pick:
13587 return "DW_OP_pick";
13588 case DW_OP_swap:
13589 return "DW_OP_swap";
13590 case DW_OP_rot:
13591 return "DW_OP_rot";
13592 case DW_OP_xderef:
13593 return "DW_OP_xderef";
13594 case DW_OP_abs:
13595 return "DW_OP_abs";
13596 case DW_OP_and:
13597 return "DW_OP_and";
13598 case DW_OP_div:
13599 return "DW_OP_div";
13600 case DW_OP_minus:
13601 return "DW_OP_minus";
13602 case DW_OP_mod:
13603 return "DW_OP_mod";
13604 case DW_OP_mul:
13605 return "DW_OP_mul";
13606 case DW_OP_neg:
13607 return "DW_OP_neg";
13608 case DW_OP_not:
13609 return "DW_OP_not";
13610 case DW_OP_or:
13611 return "DW_OP_or";
13612 case DW_OP_plus:
13613 return "DW_OP_plus";
13614 case DW_OP_plus_uconst:
13615 return "DW_OP_plus_uconst";
13616 case DW_OP_shl:
13617 return "DW_OP_shl";
13618 case DW_OP_shr:
13619 return "DW_OP_shr";
13620 case DW_OP_shra:
13621 return "DW_OP_shra";
13622 case DW_OP_xor:
13623 return "DW_OP_xor";
13624 case DW_OP_bra:
13625 return "DW_OP_bra";
13626 case DW_OP_eq:
13627 return "DW_OP_eq";
13628 case DW_OP_ge:
13629 return "DW_OP_ge";
13630 case DW_OP_gt:
13631 return "DW_OP_gt";
13632 case DW_OP_le:
13633 return "DW_OP_le";
13634 case DW_OP_lt:
13635 return "DW_OP_lt";
13636 case DW_OP_ne:
13637 return "DW_OP_ne";
13638 case DW_OP_skip:
13639 return "DW_OP_skip";
13640 case DW_OP_lit0:
13641 return "DW_OP_lit0";
13642 case DW_OP_lit1:
13643 return "DW_OP_lit1";
13644 case DW_OP_lit2:
13645 return "DW_OP_lit2";
13646 case DW_OP_lit3:
13647 return "DW_OP_lit3";
13648 case DW_OP_lit4:
13649 return "DW_OP_lit4";
13650 case DW_OP_lit5:
13651 return "DW_OP_lit5";
13652 case DW_OP_lit6:
13653 return "DW_OP_lit6";
13654 case DW_OP_lit7:
13655 return "DW_OP_lit7";
13656 case DW_OP_lit8:
13657 return "DW_OP_lit8";
13658 case DW_OP_lit9:
13659 return "DW_OP_lit9";
13660 case DW_OP_lit10:
13661 return "DW_OP_lit10";
13662 case DW_OP_lit11:
13663 return "DW_OP_lit11";
13664 case DW_OP_lit12:
13665 return "DW_OP_lit12";
13666 case DW_OP_lit13:
13667 return "DW_OP_lit13";
13668 case DW_OP_lit14:
13669 return "DW_OP_lit14";
13670 case DW_OP_lit15:
13671 return "DW_OP_lit15";
13672 case DW_OP_lit16:
13673 return "DW_OP_lit16";
13674 case DW_OP_lit17:
13675 return "DW_OP_lit17";
13676 case DW_OP_lit18:
13677 return "DW_OP_lit18";
13678 case DW_OP_lit19:
13679 return "DW_OP_lit19";
13680 case DW_OP_lit20:
13681 return "DW_OP_lit20";
13682 case DW_OP_lit21:
13683 return "DW_OP_lit21";
13684 case DW_OP_lit22:
13685 return "DW_OP_lit22";
13686 case DW_OP_lit23:
13687 return "DW_OP_lit23";
13688 case DW_OP_lit24:
13689 return "DW_OP_lit24";
13690 case DW_OP_lit25:
13691 return "DW_OP_lit25";
13692 case DW_OP_lit26:
13693 return "DW_OP_lit26";
13694 case DW_OP_lit27:
13695 return "DW_OP_lit27";
13696 case DW_OP_lit28:
13697 return "DW_OP_lit28";
13698 case DW_OP_lit29:
13699 return "DW_OP_lit29";
13700 case DW_OP_lit30:
13701 return "DW_OP_lit30";
13702 case DW_OP_lit31:
13703 return "DW_OP_lit31";
13704 case DW_OP_reg0:
13705 return "DW_OP_reg0";
13706 case DW_OP_reg1:
13707 return "DW_OP_reg1";
13708 case DW_OP_reg2:
13709 return "DW_OP_reg2";
13710 case DW_OP_reg3:
13711 return "DW_OP_reg3";
13712 case DW_OP_reg4:
13713 return "DW_OP_reg4";
13714 case DW_OP_reg5:
13715 return "DW_OP_reg5";
13716 case DW_OP_reg6:
13717 return "DW_OP_reg6";
13718 case DW_OP_reg7:
13719 return "DW_OP_reg7";
13720 case DW_OP_reg8:
13721 return "DW_OP_reg8";
13722 case DW_OP_reg9:
13723 return "DW_OP_reg9";
13724 case DW_OP_reg10:
13725 return "DW_OP_reg10";
13726 case DW_OP_reg11:
13727 return "DW_OP_reg11";
13728 case DW_OP_reg12:
13729 return "DW_OP_reg12";
13730 case DW_OP_reg13:
13731 return "DW_OP_reg13";
13732 case DW_OP_reg14:
13733 return "DW_OP_reg14";
13734 case DW_OP_reg15:
13735 return "DW_OP_reg15";
13736 case DW_OP_reg16:
13737 return "DW_OP_reg16";
13738 case DW_OP_reg17:
13739 return "DW_OP_reg17";
13740 case DW_OP_reg18:
13741 return "DW_OP_reg18";
13742 case DW_OP_reg19:
13743 return "DW_OP_reg19";
13744 case DW_OP_reg20:
13745 return "DW_OP_reg20";
13746 case DW_OP_reg21:
13747 return "DW_OP_reg21";
13748 case DW_OP_reg22:
13749 return "DW_OP_reg22";
13750 case DW_OP_reg23:
13751 return "DW_OP_reg23";
13752 case DW_OP_reg24:
13753 return "DW_OP_reg24";
13754 case DW_OP_reg25:
13755 return "DW_OP_reg25";
13756 case DW_OP_reg26:
13757 return "DW_OP_reg26";
13758 case DW_OP_reg27:
13759 return "DW_OP_reg27";
13760 case DW_OP_reg28:
13761 return "DW_OP_reg28";
13762 case DW_OP_reg29:
13763 return "DW_OP_reg29";
13764 case DW_OP_reg30:
13765 return "DW_OP_reg30";
13766 case DW_OP_reg31:
13767 return "DW_OP_reg31";
13768 case DW_OP_breg0:
13769 return "DW_OP_breg0";
13770 case DW_OP_breg1:
13771 return "DW_OP_breg1";
13772 case DW_OP_breg2:
13773 return "DW_OP_breg2";
13774 case DW_OP_breg3:
13775 return "DW_OP_breg3";
13776 case DW_OP_breg4:
13777 return "DW_OP_breg4";
13778 case DW_OP_breg5:
13779 return "DW_OP_breg5";
13780 case DW_OP_breg6:
13781 return "DW_OP_breg6";
13782 case DW_OP_breg7:
13783 return "DW_OP_breg7";
13784 case DW_OP_breg8:
13785 return "DW_OP_breg8";
13786 case DW_OP_breg9:
13787 return "DW_OP_breg9";
13788 case DW_OP_breg10:
13789 return "DW_OP_breg10";
13790 case DW_OP_breg11:
13791 return "DW_OP_breg11";
13792 case DW_OP_breg12:
13793 return "DW_OP_breg12";
13794 case DW_OP_breg13:
13795 return "DW_OP_breg13";
13796 case DW_OP_breg14:
13797 return "DW_OP_breg14";
13798 case DW_OP_breg15:
13799 return "DW_OP_breg15";
13800 case DW_OP_breg16:
13801 return "DW_OP_breg16";
13802 case DW_OP_breg17:
13803 return "DW_OP_breg17";
13804 case DW_OP_breg18:
13805 return "DW_OP_breg18";
13806 case DW_OP_breg19:
13807 return "DW_OP_breg19";
13808 case DW_OP_breg20:
13809 return "DW_OP_breg20";
13810 case DW_OP_breg21:
13811 return "DW_OP_breg21";
13812 case DW_OP_breg22:
13813 return "DW_OP_breg22";
13814 case DW_OP_breg23:
13815 return "DW_OP_breg23";
13816 case DW_OP_breg24:
13817 return "DW_OP_breg24";
13818 case DW_OP_breg25:
13819 return "DW_OP_breg25";
13820 case DW_OP_breg26:
13821 return "DW_OP_breg26";
13822 case DW_OP_breg27:
13823 return "DW_OP_breg27";
13824 case DW_OP_breg28:
13825 return "DW_OP_breg28";
13826 case DW_OP_breg29:
13827 return "DW_OP_breg29";
13828 case DW_OP_breg30:
13829 return "DW_OP_breg30";
13830 case DW_OP_breg31:
13831 return "DW_OP_breg31";
13832 case DW_OP_regx:
13833 return "DW_OP_regx";
13834 case DW_OP_fbreg:
13835 return "DW_OP_fbreg";
13836 case DW_OP_bregx:
13837 return "DW_OP_bregx";
13838 case DW_OP_piece:
13839 return "DW_OP_piece";
13840 case DW_OP_deref_size:
13841 return "DW_OP_deref_size";
13842 case DW_OP_xderef_size:
13843 return "DW_OP_xderef_size";
13844 case DW_OP_nop:
13845 return "DW_OP_nop";
b7619582 13846 /* DWARF 3 extensions. */
ed348acc
EZ
13847 case DW_OP_push_object_address:
13848 return "DW_OP_push_object_address";
13849 case DW_OP_call2:
13850 return "DW_OP_call2";
13851 case DW_OP_call4:
13852 return "DW_OP_call4";
13853 case DW_OP_call_ref:
13854 return "DW_OP_call_ref";
b7619582
GF
13855 case DW_OP_form_tls_address:
13856 return "DW_OP_form_tls_address";
13857 case DW_OP_call_frame_cfa:
13858 return "DW_OP_call_frame_cfa";
13859 case DW_OP_bit_piece:
13860 return "DW_OP_bit_piece";
9eae7c52
TT
13861 /* DWARF 4 extensions. */
13862 case DW_OP_implicit_value:
13863 return "DW_OP_implicit_value";
13864 case DW_OP_stack_value:
13865 return "DW_OP_stack_value";
13866 /* GNU extensions. */
ed348acc
EZ
13867 case DW_OP_GNU_push_tls_address:
13868 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13869 case DW_OP_GNU_uninit:
13870 return "DW_OP_GNU_uninit";
589b4a32
DE
13871 case DW_OP_GNU_encoded_addr:
13872 return "DW_OP_GNU_encoded_addr";
8cf6f0b1
TT
13873 case DW_OP_GNU_implicit_pointer:
13874 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13875 case DW_OP_GNU_entry_value:
13876 return "DW_OP_GNU_entry_value";
13877 case DW_OP_GNU_const_type:
13878 return "DW_OP_GNU_const_type";
13879 case DW_OP_GNU_regval_type:
13880 return "DW_OP_GNU_regval_type";
13881 case DW_OP_GNU_deref_type:
13882 return "DW_OP_GNU_deref_type";
13883 case DW_OP_GNU_convert:
13884 return "DW_OP_GNU_convert";
13885 case DW_OP_GNU_reinterpret:
13886 return "DW_OP_GNU_reinterpret";
589b4a32
DE
13887 case DW_OP_GNU_parameter_ref:
13888 return "DW_OP_GNU_parameter_ref";
c906108c 13889 default:
b1bfef65 13890 return NULL;
c906108c
SS
13891 }
13892}
13893
13894static char *
fba45db2 13895dwarf_bool_name (unsigned mybool)
c906108c
SS
13896{
13897 if (mybool)
13898 return "TRUE";
13899 else
13900 return "FALSE";
13901}
13902
13903/* Convert a DWARF type code into its string name. */
13904
13905static char *
aa1ee363 13906dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13907{
13908 switch (enc)
13909 {
b7619582
GF
13910 case DW_ATE_void:
13911 return "DW_ATE_void";
c906108c
SS
13912 case DW_ATE_address:
13913 return "DW_ATE_address";
13914 case DW_ATE_boolean:
13915 return "DW_ATE_boolean";
13916 case DW_ATE_complex_float:
13917 return "DW_ATE_complex_float";
13918 case DW_ATE_float:
13919 return "DW_ATE_float";
13920 case DW_ATE_signed:
13921 return "DW_ATE_signed";
13922 case DW_ATE_signed_char:
13923 return "DW_ATE_signed_char";
13924 case DW_ATE_unsigned:
13925 return "DW_ATE_unsigned";
13926 case DW_ATE_unsigned_char:
13927 return "DW_ATE_unsigned_char";
b7619582 13928 /* DWARF 3. */
d9fa45fe
DC
13929 case DW_ATE_imaginary_float:
13930 return "DW_ATE_imaginary_float";
b7619582
GF
13931 case DW_ATE_packed_decimal:
13932 return "DW_ATE_packed_decimal";
13933 case DW_ATE_numeric_string:
13934 return "DW_ATE_numeric_string";
13935 case DW_ATE_edited:
13936 return "DW_ATE_edited";
13937 case DW_ATE_signed_fixed:
13938 return "DW_ATE_signed_fixed";
13939 case DW_ATE_unsigned_fixed:
13940 return "DW_ATE_unsigned_fixed";
13941 case DW_ATE_decimal_float:
13942 return "DW_ATE_decimal_float";
75079b2b
TT
13943 /* DWARF 4. */
13944 case DW_ATE_UTF:
13945 return "DW_ATE_UTF";
b7619582
GF
13946 /* HP extensions. */
13947 case DW_ATE_HP_float80:
13948 return "DW_ATE_HP_float80";
13949 case DW_ATE_HP_complex_float80:
13950 return "DW_ATE_HP_complex_float80";
13951 case DW_ATE_HP_float128:
13952 return "DW_ATE_HP_float128";
13953 case DW_ATE_HP_complex_float128:
13954 return "DW_ATE_HP_complex_float128";
13955 case DW_ATE_HP_floathpintel:
13956 return "DW_ATE_HP_floathpintel";
13957 case DW_ATE_HP_imaginary_float80:
13958 return "DW_ATE_HP_imaginary_float80";
13959 case DW_ATE_HP_imaginary_float128:
13960 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13961 default:
13962 return "DW_ATE_<unknown>";
13963 }
13964}
13965
0963b4bd 13966/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13967
13968#if 0
13969static char *
aa1ee363 13970dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13971{
13972 switch (cfi_opc)
13973 {
13974 case DW_CFA_advance_loc:
13975 return "DW_CFA_advance_loc";
13976 case DW_CFA_offset:
13977 return "DW_CFA_offset";
13978 case DW_CFA_restore:
13979 return "DW_CFA_restore";
13980 case DW_CFA_nop:
13981 return "DW_CFA_nop";
13982 case DW_CFA_set_loc:
13983 return "DW_CFA_set_loc";
13984 case DW_CFA_advance_loc1:
13985 return "DW_CFA_advance_loc1";
13986 case DW_CFA_advance_loc2:
13987 return "DW_CFA_advance_loc2";
13988 case DW_CFA_advance_loc4:
13989 return "DW_CFA_advance_loc4";
13990 case DW_CFA_offset_extended:
13991 return "DW_CFA_offset_extended";
13992 case DW_CFA_restore_extended:
13993 return "DW_CFA_restore_extended";
13994 case DW_CFA_undefined:
13995 return "DW_CFA_undefined";
13996 case DW_CFA_same_value:
13997 return "DW_CFA_same_value";
13998 case DW_CFA_register:
13999 return "DW_CFA_register";
14000 case DW_CFA_remember_state:
14001 return "DW_CFA_remember_state";
14002 case DW_CFA_restore_state:
14003 return "DW_CFA_restore_state";
14004 case DW_CFA_def_cfa:
14005 return "DW_CFA_def_cfa";
14006 case DW_CFA_def_cfa_register:
14007 return "DW_CFA_def_cfa_register";
14008 case DW_CFA_def_cfa_offset:
14009 return "DW_CFA_def_cfa_offset";
b7619582 14010 /* DWARF 3. */
985cb1a3
JM
14011 case DW_CFA_def_cfa_expression:
14012 return "DW_CFA_def_cfa_expression";
14013 case DW_CFA_expression:
14014 return "DW_CFA_expression";
14015 case DW_CFA_offset_extended_sf:
14016 return "DW_CFA_offset_extended_sf";
14017 case DW_CFA_def_cfa_sf:
14018 return "DW_CFA_def_cfa_sf";
14019 case DW_CFA_def_cfa_offset_sf:
14020 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
14021 case DW_CFA_val_offset:
14022 return "DW_CFA_val_offset";
14023 case DW_CFA_val_offset_sf:
14024 return "DW_CFA_val_offset_sf";
14025 case DW_CFA_val_expression:
14026 return "DW_CFA_val_expression";
14027 /* SGI/MIPS specific. */
c906108c
SS
14028 case DW_CFA_MIPS_advance_loc8:
14029 return "DW_CFA_MIPS_advance_loc8";
b7619582 14030 /* GNU extensions. */
985cb1a3
JM
14031 case DW_CFA_GNU_window_save:
14032 return "DW_CFA_GNU_window_save";
14033 case DW_CFA_GNU_args_size:
14034 return "DW_CFA_GNU_args_size";
14035 case DW_CFA_GNU_negative_offset_extended:
14036 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
14037 default:
14038 return "DW_CFA_<unknown>";
14039 }
14040}
14041#endif
14042
f9aca02d 14043static void
d97bc12b 14044dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14045{
14046 unsigned int i;
14047
d97bc12b
DE
14048 print_spaces (indent, f);
14049 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14050 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14051
14052 if (die->parent != NULL)
14053 {
14054 print_spaces (indent, f);
14055 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14056 die->parent->offset.sect_off);
d97bc12b
DE
14057 }
14058
14059 print_spaces (indent, f);
14060 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14061 dwarf_bool_name (die->child != NULL));
c906108c 14062
d97bc12b
DE
14063 print_spaces (indent, f);
14064 fprintf_unfiltered (f, " attributes:\n");
14065
c906108c
SS
14066 for (i = 0; i < die->num_attrs; ++i)
14067 {
d97bc12b
DE
14068 print_spaces (indent, f);
14069 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14070 dwarf_attr_name (die->attrs[i].name),
14071 dwarf_form_name (die->attrs[i].form));
d97bc12b 14072
c906108c
SS
14073 switch (die->attrs[i].form)
14074 {
c906108c 14075 case DW_FORM_addr:
d97bc12b 14076 fprintf_unfiltered (f, "address: ");
5af949e3 14077 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14078 break;
14079 case DW_FORM_block2:
14080 case DW_FORM_block4:
14081 case DW_FORM_block:
14082 case DW_FORM_block1:
3e43a32a
MS
14083 fprintf_unfiltered (f, "block: size %d",
14084 DW_BLOCK (&die->attrs[i])->size);
c906108c 14085 break;
2dc7f7b3
TT
14086 case DW_FORM_exprloc:
14087 fprintf_unfiltered (f, "expression: size %u",
14088 DW_BLOCK (&die->attrs[i])->size);
14089 break;
4568ecf9
DE
14090 case DW_FORM_ref_addr:
14091 fprintf_unfiltered (f, "ref address: ");
14092 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14093 break;
10b3939b
DJ
14094 case DW_FORM_ref1:
14095 case DW_FORM_ref2:
14096 case DW_FORM_ref4:
4568ecf9
DE
14097 case DW_FORM_ref8:
14098 case DW_FORM_ref_udata:
d97bc12b 14099 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14100 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14101 break;
c906108c
SS
14102 case DW_FORM_data1:
14103 case DW_FORM_data2:
14104 case DW_FORM_data4:
ce5d95e1 14105 case DW_FORM_data8:
c906108c
SS
14106 case DW_FORM_udata:
14107 case DW_FORM_sdata:
43bbcdc2
PH
14108 fprintf_unfiltered (f, "constant: %s",
14109 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14110 break;
2dc7f7b3
TT
14111 case DW_FORM_sec_offset:
14112 fprintf_unfiltered (f, "section offset: %s",
14113 pulongest (DW_UNSND (&die->attrs[i])));
14114 break;
55f1336d 14115 case DW_FORM_ref_sig8:
348e048f
DE
14116 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14117 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14118 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14119 else
14120 fprintf_unfiltered (f, "signatured type, offset: unknown");
14121 break;
c906108c 14122 case DW_FORM_string:
4bdf3d34 14123 case DW_FORM_strp:
8285870a 14124 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14125 DW_STRING (&die->attrs[i])
8285870a
JK
14126 ? DW_STRING (&die->attrs[i]) : "",
14127 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14128 break;
14129 case DW_FORM_flag:
14130 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14131 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14132 else
d97bc12b 14133 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14134 break;
2dc7f7b3
TT
14135 case DW_FORM_flag_present:
14136 fprintf_unfiltered (f, "flag: TRUE");
14137 break;
a8329558 14138 case DW_FORM_indirect:
0963b4bd
MS
14139 /* The reader will have reduced the indirect form to
14140 the "base form" so this form should not occur. */
3e43a32a
MS
14141 fprintf_unfiltered (f,
14142 "unexpected attribute form: DW_FORM_indirect");
a8329558 14143 break;
c906108c 14144 default:
d97bc12b 14145 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14146 die->attrs[i].form);
d97bc12b 14147 break;
c906108c 14148 }
d97bc12b 14149 fprintf_unfiltered (f, "\n");
c906108c
SS
14150 }
14151}
14152
f9aca02d 14153static void
d97bc12b 14154dump_die_for_error (struct die_info *die)
c906108c 14155{
d97bc12b
DE
14156 dump_die_shallow (gdb_stderr, 0, die);
14157}
14158
14159static void
14160dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14161{
14162 int indent = level * 4;
14163
14164 gdb_assert (die != NULL);
14165
14166 if (level >= max_level)
14167 return;
14168
14169 dump_die_shallow (f, indent, die);
14170
14171 if (die->child != NULL)
c906108c 14172 {
d97bc12b
DE
14173 print_spaces (indent, f);
14174 fprintf_unfiltered (f, " Children:");
14175 if (level + 1 < max_level)
14176 {
14177 fprintf_unfiltered (f, "\n");
14178 dump_die_1 (f, level + 1, max_level, die->child);
14179 }
14180 else
14181 {
3e43a32a
MS
14182 fprintf_unfiltered (f,
14183 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14184 }
14185 }
14186
14187 if (die->sibling != NULL && level > 0)
14188 {
14189 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14190 }
14191}
14192
d97bc12b
DE
14193/* This is called from the pdie macro in gdbinit.in.
14194 It's not static so gcc will keep a copy callable from gdb. */
14195
14196void
14197dump_die (struct die_info *die, int max_level)
14198{
14199 dump_die_1 (gdb_stdlog, 0, max_level, die);
14200}
14201
f9aca02d 14202static void
51545339 14203store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14204{
51545339 14205 void **slot;
c906108c 14206
b64f50a1
JK
14207 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14208 INSERT);
51545339
DJ
14209
14210 *slot = die;
c906108c
SS
14211}
14212
b64f50a1
JK
14213/* DW_ADDR is always stored already as sect_offset; despite for the forms
14214 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14215
93311388
DE
14216static int
14217is_ref_attr (struct attribute *attr)
c906108c 14218{
c906108c
SS
14219 switch (attr->form)
14220 {
14221 case DW_FORM_ref_addr:
c906108c
SS
14222 case DW_FORM_ref1:
14223 case DW_FORM_ref2:
14224 case DW_FORM_ref4:
613e1657 14225 case DW_FORM_ref8:
c906108c 14226 case DW_FORM_ref_udata:
93311388 14227 return 1;
c906108c 14228 default:
93311388 14229 return 0;
c906108c 14230 }
93311388
DE
14231}
14232
b64f50a1
JK
14233/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14234 required kind. */
14235
14236static sect_offset
93311388
DE
14237dwarf2_get_ref_die_offset (struct attribute *attr)
14238{
4568ecf9 14239 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14240
93311388 14241 if (is_ref_attr (attr))
b64f50a1 14242 return retval;
93311388 14243
b64f50a1 14244 retval.sect_off = 0;
93311388
DE
14245 complaint (&symfile_complaints,
14246 _("unsupported die ref attribute form: '%s'"),
14247 dwarf_form_name (attr->form));
b64f50a1 14248 return retval;
c906108c
SS
14249}
14250
43bbcdc2
PH
14251/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14252 * the value held by the attribute is not constant. */
a02abb62 14253
43bbcdc2 14254static LONGEST
a02abb62
JB
14255dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14256{
14257 if (attr->form == DW_FORM_sdata)
14258 return DW_SND (attr);
14259 else if (attr->form == DW_FORM_udata
14260 || attr->form == DW_FORM_data1
14261 || attr->form == DW_FORM_data2
14262 || attr->form == DW_FORM_data4
14263 || attr->form == DW_FORM_data8)
14264 return DW_UNSND (attr);
14265 else
14266 {
3e43a32a
MS
14267 complaint (&symfile_complaints,
14268 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14269 dwarf_form_name (attr->form));
14270 return default_value;
14271 }
14272}
14273
03dd20cc 14274/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14275 unit and add it to our queue.
14276 The result is non-zero if PER_CU was queued, otherwise the result is zero
14277 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14278
348e048f 14279static int
03dd20cc
DJ
14280maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14281 struct dwarf2_per_cu_data *per_cu)
14282{
98bfdba5
PA
14283 /* We may arrive here during partial symbol reading, if we need full
14284 DIEs to process an unusual case (e.g. template arguments). Do
14285 not queue PER_CU, just tell our caller to load its DIEs. */
14286 if (dwarf2_per_objfile->reading_partial_symbols)
14287 {
14288 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14289 return 1;
14290 return 0;
14291 }
14292
03dd20cc
DJ
14293 /* Mark the dependence relation so that we don't flush PER_CU
14294 too early. */
14295 dwarf2_add_dependence (this_cu, per_cu);
14296
14297 /* If it's already on the queue, we have nothing to do. */
14298 if (per_cu->queued)
348e048f 14299 return 0;
03dd20cc
DJ
14300
14301 /* If the compilation unit is already loaded, just mark it as
14302 used. */
14303 if (per_cu->cu != NULL)
14304 {
14305 per_cu->cu->last_used = 0;
348e048f 14306 return 0;
03dd20cc
DJ
14307 }
14308
14309 /* Add it to the queue. */
a0f42c21 14310 queue_comp_unit (per_cu);
348e048f
DE
14311
14312 return 1;
14313}
14314
14315/* Follow reference or signature attribute ATTR of SRC_DIE.
14316 On entry *REF_CU is the CU of SRC_DIE.
14317 On exit *REF_CU is the CU of the result. */
14318
14319static struct die_info *
14320follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14321 struct dwarf2_cu **ref_cu)
14322{
14323 struct die_info *die;
14324
14325 if (is_ref_attr (attr))
14326 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14327 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14328 die = follow_die_sig (src_die, attr, ref_cu);
14329 else
14330 {
14331 dump_die_for_error (src_die);
14332 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14333 (*ref_cu)->objfile->name);
14334 }
14335
14336 return die;
03dd20cc
DJ
14337}
14338
5c631832 14339/* Follow reference OFFSET.
673bfd45
DE
14340 On entry *REF_CU is the CU of the source die referencing OFFSET.
14341 On exit *REF_CU is the CU of the result.
14342 Returns NULL if OFFSET is invalid. */
f504f079 14343
f9aca02d 14344static struct die_info *
b64f50a1 14345follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14346{
10b3939b 14347 struct die_info temp_die;
f2f0e013 14348 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14349
348e048f
DE
14350 gdb_assert (cu->per_cu != NULL);
14351
98bfdba5
PA
14352 target_cu = cu;
14353
b0df02fd 14354 if (cu->per_cu->debug_types_section)
348e048f
DE
14355 {
14356 /* .debug_types CUs cannot reference anything outside their CU.
14357 If they need to, they have to reference a signatured type via
55f1336d 14358 DW_FORM_ref_sig8. */
348e048f 14359 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14360 return NULL;
348e048f
DE
14361 }
14362 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14363 {
14364 struct dwarf2_per_cu_data *per_cu;
9a619af0 14365
45452591 14366 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14367
14368 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14369 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14370 load_full_comp_unit (per_cu);
03dd20cc 14371
10b3939b
DJ
14372 target_cu = per_cu->cu;
14373 }
98bfdba5
PA
14374 else if (cu->dies == NULL)
14375 {
14376 /* We're loading full DIEs during partial symbol reading. */
14377 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14378 load_full_comp_unit (cu->per_cu);
98bfdba5 14379 }
c906108c 14380
f2f0e013 14381 *ref_cu = target_cu;
51545339 14382 temp_die.offset = offset;
b64f50a1 14383 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14384}
10b3939b 14385
5c631832
JK
14386/* Follow reference attribute ATTR of SRC_DIE.
14387 On entry *REF_CU is the CU of SRC_DIE.
14388 On exit *REF_CU is the CU of the result. */
14389
14390static struct die_info *
14391follow_die_ref (struct die_info *src_die, struct attribute *attr,
14392 struct dwarf2_cu **ref_cu)
14393{
b64f50a1 14394 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14395 struct dwarf2_cu *cu = *ref_cu;
14396 struct die_info *die;
14397
14398 die = follow_die_offset (offset, ref_cu);
14399 if (!die)
14400 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14401 "at 0x%x [in module %s]"),
b64f50a1 14402 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14403
5c631832
JK
14404 return die;
14405}
14406
d83e736b
JK
14407/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14408 Returned value is intended for DW_OP_call*. Returned
14409 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14410
14411struct dwarf2_locexpr_baton
b64f50a1 14412dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14413 struct dwarf2_per_cu_data *per_cu,
14414 CORE_ADDR (*get_frame_pc) (void *baton),
14415 void *baton)
5c631832 14416{
b64f50a1 14417 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14418 struct dwarf2_cu *cu;
5c631832
JK
14419 struct die_info *die;
14420 struct attribute *attr;
14421 struct dwarf2_locexpr_baton retval;
14422
8cf6f0b1
TT
14423 dw2_setup (per_cu->objfile);
14424
918dd910
JK
14425 if (per_cu->cu == NULL)
14426 load_cu (per_cu);
14427 cu = per_cu->cu;
14428
5c631832
JK
14429 die = follow_die_offset (offset, &cu);
14430 if (!die)
14431 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 14432 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14433
14434 attr = dwarf2_attr (die, DW_AT_location, cu);
14435 if (!attr)
14436 {
e103e986
JK
14437 /* DWARF: "If there is no such attribute, then there is no effect.".
14438 DATA is ignored if SIZE is 0. */
5c631832 14439
e103e986 14440 retval.data = NULL;
5c631832
JK
14441 retval.size = 0;
14442 }
8cf6f0b1
TT
14443 else if (attr_form_is_section_offset (attr))
14444 {
14445 struct dwarf2_loclist_baton loclist_baton;
14446 CORE_ADDR pc = (*get_frame_pc) (baton);
14447 size_t size;
14448
14449 fill_in_loclist_baton (cu, &loclist_baton, attr);
14450
14451 retval.data = dwarf2_find_location_expression (&loclist_baton,
14452 &size, pc);
14453 retval.size = size;
14454 }
5c631832
JK
14455 else
14456 {
14457 if (!attr_form_is_block (attr))
14458 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14459 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 14460 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14461
14462 retval.data = DW_BLOCK (attr)->data;
14463 retval.size = DW_BLOCK (attr)->size;
14464 }
14465 retval.per_cu = cu->per_cu;
918dd910 14466
918dd910
JK
14467 age_cached_comp_units ();
14468
5c631832 14469 return retval;
348e048f
DE
14470}
14471
8a9b8146
TT
14472/* Return the type of the DIE at DIE_OFFSET in the CU named by
14473 PER_CU. */
14474
14475struct type *
b64f50a1 14476dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
14477 struct dwarf2_per_cu_data *per_cu)
14478{
b64f50a1
JK
14479 sect_offset die_offset_sect;
14480
8a9b8146 14481 dw2_setup (per_cu->objfile);
b64f50a1
JK
14482
14483 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14484 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
14485}
14486
348e048f
DE
14487/* Follow the signature attribute ATTR in SRC_DIE.
14488 On entry *REF_CU is the CU of SRC_DIE.
14489 On exit *REF_CU is the CU of the result. */
14490
14491static struct die_info *
14492follow_die_sig (struct die_info *src_die, struct attribute *attr,
14493 struct dwarf2_cu **ref_cu)
14494{
14495 struct objfile *objfile = (*ref_cu)->objfile;
14496 struct die_info temp_die;
14497 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14498 struct dwarf2_cu *sig_cu;
14499 struct die_info *die;
14500
14501 /* sig_type will be NULL if the signatured type is missing from
14502 the debug info. */
14503 if (sig_type == NULL)
14504 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14505 "at 0x%x [in module %s]"),
b64f50a1 14506 src_die->offset.sect_off, objfile->name);
348e048f
DE
14507
14508 /* If necessary, add it to the queue and load its DIEs. */
14509
14510 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14511 read_signatured_type (sig_type);
348e048f
DE
14512
14513 gdb_assert (sig_type->per_cu.cu != NULL);
14514
14515 sig_cu = sig_type->per_cu.cu;
b64f50a1
JK
14516 temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14517 + sig_type->type_offset.cu_off);
14518 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14519 temp_die.offset.sect_off);
348e048f
DE
14520 if (die)
14521 {
14522 *ref_cu = sig_cu;
14523 return die;
14524 }
14525
3e43a32a
MS
14526 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14527 "from DIE at 0x%x [in module %s]"),
b64f50a1 14528 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
14529}
14530
14531/* Given an offset of a signatured type, return its signatured_type. */
14532
14533static struct signatured_type *
8b70b953
TT
14534lookup_signatured_type_at_offset (struct objfile *objfile,
14535 struct dwarf2_section_info *section,
b64f50a1 14536 sect_offset offset)
348e048f 14537{
b64f50a1 14538 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
14539 unsigned int length, initial_length_size;
14540 unsigned int sig_offset;
52dc124a 14541 struct signatured_type find_entry, *sig_type;
348e048f
DE
14542
14543 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14544 sig_offset = (initial_length_size
14545 + 2 /*version*/
14546 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14547 + 1 /*address_size*/);
14548 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 14549 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
14550
14551 /* This is only used to lookup previously recorded types.
14552 If we didn't find it, it's our bug. */
52dc124a
DE
14553 gdb_assert (sig_type != NULL);
14554 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 14555
52dc124a 14556 return sig_type;
348e048f
DE
14557}
14558
e5fe5e75 14559/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14560
14561static void
e5fe5e75 14562load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14563{
e5fe5e75
DE
14564 struct objfile *objfile = per_cu->objfile;
14565 struct dwarf2_section_info *sect = per_cu->debug_types_section;
b64f50a1 14566 sect_offset offset = per_cu->offset;
52dc124a 14567 struct signatured_type *sig_type;
348e048f 14568
8b70b953 14569 dwarf2_read_section (objfile, sect);
be391dca 14570
348e048f 14571 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14572 hash table lookup. */
14573 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14574 the signature to assert we found the right one.
14575 Ok, but it's a lot of work. We should simplify things so any needed
14576 assert doesn't require all this clumsiness. */
52dc124a 14577 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 14578
52dc124a 14579 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14580
52dc124a 14581 read_signatured_type (sig_type);
348e048f 14582
52dc124a 14583 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
14584}
14585
14586/* Read in a signatured type and build its CU and DIEs. */
14587
14588static void
52dc124a 14589read_signatured_type (struct signatured_type *sig_type)
348e048f 14590{
52dc124a 14591 struct objfile *objfile = sig_type->per_cu.objfile;
1fd400ff 14592 gdb_byte *types_ptr;
348e048f
DE
14593 struct die_reader_specs reader_specs;
14594 struct dwarf2_cu *cu;
14595 ULONGEST signature;
14596 struct cleanup *back_to, *free_cu_cleanup;
52dc124a 14597 struct dwarf2_section_info *section = sig_type->per_cu.debug_types_section;
348e048f 14598
8b70b953 14599 dwarf2_read_section (objfile, section);
52dc124a 14600 types_ptr = section->buffer + sig_type->per_cu.offset.sect_off;
1fd400ff 14601
52dc124a 14602 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14603
9816fde3 14604 cu = xmalloc (sizeof (*cu));
52dc124a 14605 init_one_comp_unit (cu, &sig_type->per_cu);
348e048f
DE
14606
14607 /* If an error occurs while loading, release our storage. */
68dc6402 14608 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14609
9ff913ba
DE
14610 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14611 &signature, NULL);
52dc124a 14612 gdb_assert (signature == sig_type->signature);
348e048f
DE
14613
14614 cu->die_hash
14615 = htab_create_alloc_ex (cu->header.length / 12,
14616 die_hash,
14617 die_eq,
14618 NULL,
14619 &cu->comp_unit_obstack,
14620 hashtab_obstack_allocate,
14621 dummy_obstack_deallocate);
14622
e5fe5e75 14623 dwarf2_read_abbrevs (cu);
348e048f
DE
14624 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14625
14626 init_cu_die_reader (&reader_specs, cu);
14627
14628 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14629 NULL /*parent*/);
14630
14631 /* We try not to read any attributes in this function, because not
9cdd5dbd 14632 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14633 table processing isn't initialized. But we have to set the CU language,
14634 or we won't be able to build types correctly. */
9816fde3 14635 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14636
14637 do_cleanups (back_to);
14638
14639 /* We've successfully allocated this compilation unit. Let our caller
14640 clean it up when finished with it. */
14641 discard_cleanups (free_cu_cleanup);
14642
c5b7e1cb 14643 /* Link this TU into read_in_chain. */
52dc124a
DE
14644 sig_type->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14645 dwarf2_per_objfile->read_in_chain = &sig_type->per_cu;
c906108c
SS
14646}
14647
c906108c
SS
14648/* Decode simple location descriptions.
14649 Given a pointer to a dwarf block that defines a location, compute
14650 the location and return the value.
14651
4cecd739
DJ
14652 NOTE drow/2003-11-18: This function is called in two situations
14653 now: for the address of static or global variables (partial symbols
14654 only) and for offsets into structures which are expected to be
14655 (more or less) constant. The partial symbol case should go away,
14656 and only the constant case should remain. That will let this
14657 function complain more accurately. A few special modes are allowed
14658 without complaint for global variables (for instance, global
14659 register values and thread-local values).
c906108c
SS
14660
14661 A location description containing no operations indicates that the
4cecd739 14662 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14663 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14664 callers will only want a very basic result and this can become a
21ae7a4d
JK
14665 complaint.
14666
14667 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14668
14669static CORE_ADDR
e7c27a73 14670decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14671{
e7c27a73 14672 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14673 int i;
14674 int size = blk->size;
14675 gdb_byte *data = blk->data;
14676 CORE_ADDR stack[64];
14677 int stacki;
14678 unsigned int bytes_read, unsnd;
14679 gdb_byte op;
c906108c 14680
21ae7a4d
JK
14681 i = 0;
14682 stacki = 0;
14683 stack[stacki] = 0;
14684 stack[++stacki] = 0;
14685
14686 while (i < size)
14687 {
14688 op = data[i++];
14689 switch (op)
14690 {
14691 case DW_OP_lit0:
14692 case DW_OP_lit1:
14693 case DW_OP_lit2:
14694 case DW_OP_lit3:
14695 case DW_OP_lit4:
14696 case DW_OP_lit5:
14697 case DW_OP_lit6:
14698 case DW_OP_lit7:
14699 case DW_OP_lit8:
14700 case DW_OP_lit9:
14701 case DW_OP_lit10:
14702 case DW_OP_lit11:
14703 case DW_OP_lit12:
14704 case DW_OP_lit13:
14705 case DW_OP_lit14:
14706 case DW_OP_lit15:
14707 case DW_OP_lit16:
14708 case DW_OP_lit17:
14709 case DW_OP_lit18:
14710 case DW_OP_lit19:
14711 case DW_OP_lit20:
14712 case DW_OP_lit21:
14713 case DW_OP_lit22:
14714 case DW_OP_lit23:
14715 case DW_OP_lit24:
14716 case DW_OP_lit25:
14717 case DW_OP_lit26:
14718 case DW_OP_lit27:
14719 case DW_OP_lit28:
14720 case DW_OP_lit29:
14721 case DW_OP_lit30:
14722 case DW_OP_lit31:
14723 stack[++stacki] = op - DW_OP_lit0;
14724 break;
f1bea926 14725
21ae7a4d
JK
14726 case DW_OP_reg0:
14727 case DW_OP_reg1:
14728 case DW_OP_reg2:
14729 case DW_OP_reg3:
14730 case DW_OP_reg4:
14731 case DW_OP_reg5:
14732 case DW_OP_reg6:
14733 case DW_OP_reg7:
14734 case DW_OP_reg8:
14735 case DW_OP_reg9:
14736 case DW_OP_reg10:
14737 case DW_OP_reg11:
14738 case DW_OP_reg12:
14739 case DW_OP_reg13:
14740 case DW_OP_reg14:
14741 case DW_OP_reg15:
14742 case DW_OP_reg16:
14743 case DW_OP_reg17:
14744 case DW_OP_reg18:
14745 case DW_OP_reg19:
14746 case DW_OP_reg20:
14747 case DW_OP_reg21:
14748 case DW_OP_reg22:
14749 case DW_OP_reg23:
14750 case DW_OP_reg24:
14751 case DW_OP_reg25:
14752 case DW_OP_reg26:
14753 case DW_OP_reg27:
14754 case DW_OP_reg28:
14755 case DW_OP_reg29:
14756 case DW_OP_reg30:
14757 case DW_OP_reg31:
14758 stack[++stacki] = op - DW_OP_reg0;
14759 if (i < size)
14760 dwarf2_complex_location_expr_complaint ();
14761 break;
c906108c 14762
21ae7a4d
JK
14763 case DW_OP_regx:
14764 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14765 i += bytes_read;
14766 stack[++stacki] = unsnd;
14767 if (i < size)
14768 dwarf2_complex_location_expr_complaint ();
14769 break;
c906108c 14770
21ae7a4d
JK
14771 case DW_OP_addr:
14772 stack[++stacki] = read_address (objfile->obfd, &data[i],
14773 cu, &bytes_read);
14774 i += bytes_read;
14775 break;
d53d4ac5 14776
21ae7a4d
JK
14777 case DW_OP_const1u:
14778 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14779 i += 1;
14780 break;
14781
14782 case DW_OP_const1s:
14783 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14784 i += 1;
14785 break;
14786
14787 case DW_OP_const2u:
14788 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14789 i += 2;
14790 break;
14791
14792 case DW_OP_const2s:
14793 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14794 i += 2;
14795 break;
d53d4ac5 14796
21ae7a4d
JK
14797 case DW_OP_const4u:
14798 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14799 i += 4;
14800 break;
14801
14802 case DW_OP_const4s:
14803 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14804 i += 4;
14805 break;
14806
585861ea
JK
14807 case DW_OP_const8u:
14808 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14809 i += 8;
14810 break;
14811
21ae7a4d
JK
14812 case DW_OP_constu:
14813 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14814 &bytes_read);
14815 i += bytes_read;
14816 break;
14817
14818 case DW_OP_consts:
14819 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14820 i += bytes_read;
14821 break;
14822
14823 case DW_OP_dup:
14824 stack[stacki + 1] = stack[stacki];
14825 stacki++;
14826 break;
14827
14828 case DW_OP_plus:
14829 stack[stacki - 1] += stack[stacki];
14830 stacki--;
14831 break;
14832
14833 case DW_OP_plus_uconst:
14834 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14835 &bytes_read);
14836 i += bytes_read;
14837 break;
14838
14839 case DW_OP_minus:
14840 stack[stacki - 1] -= stack[stacki];
14841 stacki--;
14842 break;
14843
14844 case DW_OP_deref:
14845 /* If we're not the last op, then we definitely can't encode
14846 this using GDB's address_class enum. This is valid for partial
14847 global symbols, although the variable's address will be bogus
14848 in the psymtab. */
14849 if (i < size)
14850 dwarf2_complex_location_expr_complaint ();
14851 break;
14852
14853 case DW_OP_GNU_push_tls_address:
14854 /* The top of the stack has the offset from the beginning
14855 of the thread control block at which the variable is located. */
14856 /* Nothing should follow this operator, so the top of stack would
14857 be returned. */
14858 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14859 address will be bogus in the psymtab. Make it always at least
14860 non-zero to not look as a variable garbage collected by linker
14861 which have DW_OP_addr 0. */
21ae7a4d
JK
14862 if (i < size)
14863 dwarf2_complex_location_expr_complaint ();
585861ea 14864 stack[stacki]++;
21ae7a4d
JK
14865 break;
14866
14867 case DW_OP_GNU_uninit:
14868 break;
14869
14870 default:
14871 {
14872 const char *name = dwarf_stack_op_name (op);
14873
14874 if (name)
14875 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14876 name);
14877 else
14878 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14879 op);
14880 }
14881
14882 return (stack[stacki]);
d53d4ac5 14883 }
3c6e0cb3 14884
21ae7a4d
JK
14885 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14886 outside of the allocated space. Also enforce minimum>0. */
14887 if (stacki >= ARRAY_SIZE (stack) - 1)
14888 {
14889 complaint (&symfile_complaints,
14890 _("location description stack overflow"));
14891 return 0;
14892 }
14893
14894 if (stacki <= 0)
14895 {
14896 complaint (&symfile_complaints,
14897 _("location description stack underflow"));
14898 return 0;
14899 }
14900 }
14901 return (stack[stacki]);
c906108c
SS
14902}
14903
14904/* memory allocation interface */
14905
c906108c 14906static struct dwarf_block *
7b5a2f43 14907dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14908{
14909 struct dwarf_block *blk;
14910
14911 blk = (struct dwarf_block *)
7b5a2f43 14912 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14913 return (blk);
14914}
14915
14916static struct abbrev_info *
f3dd6933 14917dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14918{
14919 struct abbrev_info *abbrev;
14920
f3dd6933
DJ
14921 abbrev = (struct abbrev_info *)
14922 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14923 memset (abbrev, 0, sizeof (struct abbrev_info));
14924 return (abbrev);
14925}
14926
14927static struct die_info *
b60c80d6 14928dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14929{
14930 struct die_info *die;
b60c80d6
DJ
14931 size_t size = sizeof (struct die_info);
14932
14933 if (num_attrs > 1)
14934 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14935
b60c80d6 14936 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14937 memset (die, 0, sizeof (struct die_info));
14938 return (die);
14939}
2e276125
JB
14940
14941\f
14942/* Macro support. */
14943
2e276125
JB
14944/* Return the full name of file number I in *LH's file name table.
14945 Use COMP_DIR as the name of the current directory of the
14946 compilation. The result is allocated using xmalloc; the caller is
14947 responsible for freeing it. */
14948static char *
14949file_full_name (int file, struct line_header *lh, const char *comp_dir)
14950{
6a83a1e6
EZ
14951 /* Is the file number a valid index into the line header's file name
14952 table? Remember that file numbers start with one, not zero. */
14953 if (1 <= file && file <= lh->num_file_names)
14954 {
14955 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14956
6a83a1e6
EZ
14957 if (IS_ABSOLUTE_PATH (fe->name))
14958 return xstrdup (fe->name);
14959 else
14960 {
14961 const char *dir;
14962 int dir_len;
14963 char *full_name;
14964
14965 if (fe->dir_index)
14966 dir = lh->include_dirs[fe->dir_index - 1];
14967 else
14968 dir = comp_dir;
14969
14970 if (dir)
14971 {
14972 dir_len = strlen (dir);
14973 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14974 strcpy (full_name, dir);
14975 full_name[dir_len] = '/';
14976 strcpy (full_name + dir_len + 1, fe->name);
14977 return full_name;
14978 }
14979 else
14980 return xstrdup (fe->name);
14981 }
14982 }
2e276125
JB
14983 else
14984 {
6a83a1e6
EZ
14985 /* The compiler produced a bogus file number. We can at least
14986 record the macro definitions made in the file, even if we
14987 won't be able to find the file by name. */
14988 char fake_name[80];
9a619af0 14989
6a83a1e6 14990 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14991
6e70227d 14992 complaint (&symfile_complaints,
6a83a1e6
EZ
14993 _("bad file number in macro information (%d)"),
14994 file);
2e276125 14995
6a83a1e6 14996 return xstrdup (fake_name);
2e276125
JB
14997 }
14998}
14999
15000
15001static struct macro_source_file *
15002macro_start_file (int file, int line,
15003 struct macro_source_file *current_file,
15004 const char *comp_dir,
15005 struct line_header *lh, struct objfile *objfile)
15006{
15007 /* The full name of this source file. */
15008 char *full_name = file_full_name (file, lh, comp_dir);
15009
15010 /* We don't create a macro table for this compilation unit
15011 at all until we actually get a filename. */
15012 if (! pending_macros)
4a146b47 15013 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15014 objfile->macro_cache);
2e276125
JB
15015
15016 if (! current_file)
15017 /* If we have no current file, then this must be the start_file
15018 directive for the compilation unit's main source file. */
15019 current_file = macro_set_main (pending_macros, full_name);
15020 else
15021 current_file = macro_include (current_file, line, full_name);
15022
15023 xfree (full_name);
6e70227d 15024
2e276125
JB
15025 return current_file;
15026}
15027
15028
15029/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15030 followed by a null byte. */
15031static char *
15032copy_string (const char *buf, int len)
15033{
15034 char *s = xmalloc (len + 1);
9a619af0 15035
2e276125
JB
15036 memcpy (s, buf, len);
15037 s[len] = '\0';
2e276125
JB
15038 return s;
15039}
15040
15041
15042static const char *
15043consume_improper_spaces (const char *p, const char *body)
15044{
15045 if (*p == ' ')
15046 {
4d3c2250 15047 complaint (&symfile_complaints,
3e43a32a
MS
15048 _("macro definition contains spaces "
15049 "in formal argument list:\n`%s'"),
4d3c2250 15050 body);
2e276125
JB
15051
15052 while (*p == ' ')
15053 p++;
15054 }
15055
15056 return p;
15057}
15058
15059
15060static void
15061parse_macro_definition (struct macro_source_file *file, int line,
15062 const char *body)
15063{
15064 const char *p;
15065
15066 /* The body string takes one of two forms. For object-like macro
15067 definitions, it should be:
15068
15069 <macro name> " " <definition>
15070
15071 For function-like macro definitions, it should be:
15072
15073 <macro name> "() " <definition>
15074 or
15075 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15076
15077 Spaces may appear only where explicitly indicated, and in the
15078 <definition>.
15079
15080 The Dwarf 2 spec says that an object-like macro's name is always
15081 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15082 the space when the macro's definition is the empty string.
2e276125
JB
15083
15084 The Dwarf 2 spec says that there should be no spaces between the
15085 formal arguments in a function-like macro's formal argument list,
15086 but versions of GCC around March 2002 include spaces after the
15087 commas. */
15088
15089
15090 /* Find the extent of the macro name. The macro name is terminated
15091 by either a space or null character (for an object-like macro) or
15092 an opening paren (for a function-like macro). */
15093 for (p = body; *p; p++)
15094 if (*p == ' ' || *p == '(')
15095 break;
15096
15097 if (*p == ' ' || *p == '\0')
15098 {
15099 /* It's an object-like macro. */
15100 int name_len = p - body;
15101 char *name = copy_string (body, name_len);
15102 const char *replacement;
15103
15104 if (*p == ' ')
15105 replacement = body + name_len + 1;
15106 else
15107 {
4d3c2250 15108 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15109 replacement = body + name_len;
15110 }
6e70227d 15111
2e276125
JB
15112 macro_define_object (file, line, name, replacement);
15113
15114 xfree (name);
15115 }
15116 else if (*p == '(')
15117 {
15118 /* It's a function-like macro. */
15119 char *name = copy_string (body, p - body);
15120 int argc = 0;
15121 int argv_size = 1;
15122 char **argv = xmalloc (argv_size * sizeof (*argv));
15123
15124 p++;
15125
15126 p = consume_improper_spaces (p, body);
15127
15128 /* Parse the formal argument list. */
15129 while (*p && *p != ')')
15130 {
15131 /* Find the extent of the current argument name. */
15132 const char *arg_start = p;
15133
15134 while (*p && *p != ',' && *p != ')' && *p != ' ')
15135 p++;
15136
15137 if (! *p || p == arg_start)
4d3c2250 15138 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15139 else
15140 {
15141 /* Make sure argv has room for the new argument. */
15142 if (argc >= argv_size)
15143 {
15144 argv_size *= 2;
15145 argv = xrealloc (argv, argv_size * sizeof (*argv));
15146 }
15147
15148 argv[argc++] = copy_string (arg_start, p - arg_start);
15149 }
15150
15151 p = consume_improper_spaces (p, body);
15152
15153 /* Consume the comma, if present. */
15154 if (*p == ',')
15155 {
15156 p++;
15157
15158 p = consume_improper_spaces (p, body);
15159 }
15160 }
15161
15162 if (*p == ')')
15163 {
15164 p++;
15165
15166 if (*p == ' ')
15167 /* Perfectly formed definition, no complaints. */
15168 macro_define_function (file, line, name,
6e70227d 15169 argc, (const char **) argv,
2e276125
JB
15170 p + 1);
15171 else if (*p == '\0')
15172 {
15173 /* Complain, but do define it. */
4d3c2250 15174 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15175 macro_define_function (file, line, name,
6e70227d 15176 argc, (const char **) argv,
2e276125
JB
15177 p);
15178 }
15179 else
15180 /* Just complain. */
4d3c2250 15181 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15182 }
15183 else
15184 /* Just complain. */
4d3c2250 15185 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15186
15187 xfree (name);
15188 {
15189 int i;
15190
15191 for (i = 0; i < argc; i++)
15192 xfree (argv[i]);
15193 }
15194 xfree (argv);
15195 }
15196 else
4d3c2250 15197 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15198}
15199
cf2c3c16
TT
15200/* Skip some bytes from BYTES according to the form given in FORM.
15201 Returns the new pointer. */
2e276125 15202
cf2c3c16
TT
15203static gdb_byte *
15204skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15205 enum dwarf_form form,
15206 unsigned int offset_size,
15207 struct dwarf2_section_info *section)
2e276125 15208{
cf2c3c16 15209 unsigned int bytes_read;
2e276125 15210
cf2c3c16 15211 switch (form)
2e276125 15212 {
cf2c3c16
TT
15213 case DW_FORM_data1:
15214 case DW_FORM_flag:
15215 ++bytes;
15216 break;
15217
15218 case DW_FORM_data2:
15219 bytes += 2;
15220 break;
15221
15222 case DW_FORM_data4:
15223 bytes += 4;
15224 break;
15225
15226 case DW_FORM_data8:
15227 bytes += 8;
15228 break;
15229
15230 case DW_FORM_string:
15231 read_direct_string (abfd, bytes, &bytes_read);
15232 bytes += bytes_read;
15233 break;
15234
15235 case DW_FORM_sec_offset:
15236 case DW_FORM_strp:
15237 bytes += offset_size;
15238 break;
15239
15240 case DW_FORM_block:
15241 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15242 bytes += bytes_read;
15243 break;
15244
15245 case DW_FORM_block1:
15246 bytes += 1 + read_1_byte (abfd, bytes);
15247 break;
15248 case DW_FORM_block2:
15249 bytes += 2 + read_2_bytes (abfd, bytes);
15250 break;
15251 case DW_FORM_block4:
15252 bytes += 4 + read_4_bytes (abfd, bytes);
15253 break;
15254
15255 case DW_FORM_sdata:
15256 case DW_FORM_udata:
15257 bytes = skip_leb128 (abfd, bytes);
15258 break;
15259
15260 default:
15261 {
15262 complain:
15263 complaint (&symfile_complaints,
15264 _("invalid form 0x%x in `%s'"),
15265 form,
15266 section->asection->name);
15267 return NULL;
15268 }
2e276125
JB
15269 }
15270
cf2c3c16
TT
15271 return bytes;
15272}
757a13d0 15273
cf2c3c16
TT
15274/* A helper for dwarf_decode_macros that handles skipping an unknown
15275 opcode. Returns an updated pointer to the macro data buffer; or,
15276 on error, issues a complaint and returns NULL. */
757a13d0 15277
cf2c3c16
TT
15278static gdb_byte *
15279skip_unknown_opcode (unsigned int opcode,
15280 gdb_byte **opcode_definitions,
15281 gdb_byte *mac_ptr,
15282 bfd *abfd,
15283 unsigned int offset_size,
15284 struct dwarf2_section_info *section)
15285{
15286 unsigned int bytes_read, i;
15287 unsigned long arg;
15288 gdb_byte *defn;
2e276125 15289
cf2c3c16 15290 if (opcode_definitions[opcode] == NULL)
2e276125 15291 {
cf2c3c16
TT
15292 complaint (&symfile_complaints,
15293 _("unrecognized DW_MACFINO opcode 0x%x"),
15294 opcode);
15295 return NULL;
15296 }
2e276125 15297
cf2c3c16
TT
15298 defn = opcode_definitions[opcode];
15299 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15300 defn += bytes_read;
2e276125 15301
cf2c3c16
TT
15302 for (i = 0; i < arg; ++i)
15303 {
15304 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15305 if (mac_ptr == NULL)
15306 {
15307 /* skip_form_bytes already issued the complaint. */
15308 return NULL;
15309 }
15310 }
757a13d0 15311
cf2c3c16
TT
15312 return mac_ptr;
15313}
757a13d0 15314
cf2c3c16
TT
15315/* A helper function which parses the header of a macro section.
15316 If the macro section is the extended (for now called "GNU") type,
15317 then this updates *OFFSET_SIZE. Returns a pointer to just after
15318 the header, or issues a complaint and returns NULL on error. */
757a13d0 15319
cf2c3c16
TT
15320static gdb_byte *
15321dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15322 bfd *abfd,
15323 gdb_byte *mac_ptr,
15324 unsigned int *offset_size,
15325 int section_is_gnu)
15326{
15327 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15328
cf2c3c16
TT
15329 if (section_is_gnu)
15330 {
15331 unsigned int version, flags;
757a13d0 15332
cf2c3c16
TT
15333 version = read_2_bytes (abfd, mac_ptr);
15334 if (version != 4)
15335 {
15336 complaint (&symfile_complaints,
15337 _("unrecognized version `%d' in .debug_macro section"),
15338 version);
15339 return NULL;
15340 }
15341 mac_ptr += 2;
757a13d0 15342
cf2c3c16
TT
15343 flags = read_1_byte (abfd, mac_ptr);
15344 ++mac_ptr;
15345 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15346
cf2c3c16
TT
15347 if ((flags & 2) != 0)
15348 /* We don't need the line table offset. */
15349 mac_ptr += *offset_size;
757a13d0 15350
cf2c3c16
TT
15351 /* Vendor opcode descriptions. */
15352 if ((flags & 4) != 0)
15353 {
15354 unsigned int i, count;
757a13d0 15355
cf2c3c16
TT
15356 count = read_1_byte (abfd, mac_ptr);
15357 ++mac_ptr;
15358 for (i = 0; i < count; ++i)
15359 {
15360 unsigned int opcode, bytes_read;
15361 unsigned long arg;
15362
15363 opcode = read_1_byte (abfd, mac_ptr);
15364 ++mac_ptr;
15365 opcode_definitions[opcode] = mac_ptr;
15366 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15367 mac_ptr += bytes_read;
15368 mac_ptr += arg;
15369 }
757a13d0 15370 }
cf2c3c16 15371 }
757a13d0 15372
cf2c3c16
TT
15373 return mac_ptr;
15374}
757a13d0 15375
cf2c3c16 15376/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15377 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15378
15379static void
15380dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15381 struct macro_source_file *current_file,
15382 struct line_header *lh, char *comp_dir,
15383 struct dwarf2_section_info *section,
15384 int section_is_gnu,
15385 unsigned int offset_size,
8fc3fc34
TT
15386 struct objfile *objfile,
15387 htab_t include_hash)
cf2c3c16
TT
15388{
15389 enum dwarf_macro_record_type macinfo_type;
15390 int at_commandline;
15391 gdb_byte *opcode_definitions[256];
757a13d0 15392
cf2c3c16
TT
15393 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15394 &offset_size, section_is_gnu);
15395 if (mac_ptr == NULL)
15396 {
15397 /* We already issued a complaint. */
15398 return;
15399 }
757a13d0
JK
15400
15401 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15402 GDB is still reading the definitions from command line. First
15403 DW_MACINFO_start_file will need to be ignored as it was already executed
15404 to create CURRENT_FILE for the main source holding also the command line
15405 definitions. On first met DW_MACINFO_start_file this flag is reset to
15406 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15407
15408 at_commandline = 1;
15409
15410 do
15411 {
15412 /* Do we at least have room for a macinfo type byte? */
15413 if (mac_ptr >= mac_end)
15414 {
cf2c3c16 15415 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15416 break;
15417 }
15418
15419 macinfo_type = read_1_byte (abfd, mac_ptr);
15420 mac_ptr++;
15421
cf2c3c16
TT
15422 /* Note that we rely on the fact that the corresponding GNU and
15423 DWARF constants are the same. */
757a13d0
JK
15424 switch (macinfo_type)
15425 {
15426 /* A zero macinfo type indicates the end of the macro
15427 information. */
15428 case 0:
15429 break;
2e276125 15430
cf2c3c16
TT
15431 case DW_MACRO_GNU_define:
15432 case DW_MACRO_GNU_undef:
15433 case DW_MACRO_GNU_define_indirect:
15434 case DW_MACRO_GNU_undef_indirect:
2e276125 15435 {
891d2f0b 15436 unsigned int bytes_read;
2e276125
JB
15437 int line;
15438 char *body;
cf2c3c16 15439 int is_define;
2e276125 15440
cf2c3c16
TT
15441 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15442 mac_ptr += bytes_read;
15443
15444 if (macinfo_type == DW_MACRO_GNU_define
15445 || macinfo_type == DW_MACRO_GNU_undef)
15446 {
15447 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15448 mac_ptr += bytes_read;
15449 }
15450 else
15451 {
15452 LONGEST str_offset;
15453
15454 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15455 mac_ptr += offset_size;
2e276125 15456
cf2c3c16
TT
15457 body = read_indirect_string_at_offset (abfd, str_offset);
15458 }
15459
15460 is_define = (macinfo_type == DW_MACRO_GNU_define
15461 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15462 if (! current_file)
757a13d0
JK
15463 {
15464 /* DWARF violation as no main source is present. */
15465 complaint (&symfile_complaints,
15466 _("debug info with no main source gives macro %s "
15467 "on line %d: %s"),
cf2c3c16
TT
15468 is_define ? _("definition") : _("undefinition"),
15469 line, body);
757a13d0
JK
15470 break;
15471 }
3e43a32a
MS
15472 if ((line == 0 && !at_commandline)
15473 || (line != 0 && at_commandline))
4d3c2250 15474 complaint (&symfile_complaints,
757a13d0
JK
15475 _("debug info gives %s macro %s with %s line %d: %s"),
15476 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15477 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15478 line == 0 ? _("zero") : _("non-zero"), line, body);
15479
cf2c3c16 15480 if (is_define)
757a13d0 15481 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15482 else
15483 {
15484 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15485 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15486 macro_undef (current_file, line, body);
15487 }
2e276125
JB
15488 }
15489 break;
15490
cf2c3c16 15491 case DW_MACRO_GNU_start_file:
2e276125 15492 {
891d2f0b 15493 unsigned int bytes_read;
2e276125
JB
15494 int line, file;
15495
15496 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15497 mac_ptr += bytes_read;
15498 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15499 mac_ptr += bytes_read;
15500
3e43a32a
MS
15501 if ((line == 0 && !at_commandline)
15502 || (line != 0 && at_commandline))
757a13d0
JK
15503 complaint (&symfile_complaints,
15504 _("debug info gives source %d included "
15505 "from %s at %s line %d"),
15506 file, at_commandline ? _("command-line") : _("file"),
15507 line == 0 ? _("zero") : _("non-zero"), line);
15508
15509 if (at_commandline)
15510 {
cf2c3c16
TT
15511 /* This DW_MACRO_GNU_start_file was executed in the
15512 pass one. */
757a13d0
JK
15513 at_commandline = 0;
15514 }
15515 else
15516 current_file = macro_start_file (file, line,
15517 current_file, comp_dir,
cf2c3c16 15518 lh, objfile);
2e276125
JB
15519 }
15520 break;
15521
cf2c3c16 15522 case DW_MACRO_GNU_end_file:
2e276125 15523 if (! current_file)
4d3c2250 15524 complaint (&symfile_complaints,
3e43a32a
MS
15525 _("macro debug info has an unmatched "
15526 "`close_file' directive"));
2e276125
JB
15527 else
15528 {
15529 current_file = current_file->included_by;
15530 if (! current_file)
15531 {
cf2c3c16 15532 enum dwarf_macro_record_type next_type;
2e276125
JB
15533
15534 /* GCC circa March 2002 doesn't produce the zero
15535 type byte marking the end of the compilation
15536 unit. Complain if it's not there, but exit no
15537 matter what. */
15538
15539 /* Do we at least have room for a macinfo type byte? */
15540 if (mac_ptr >= mac_end)
15541 {
cf2c3c16 15542 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15543 return;
15544 }
15545
15546 /* We don't increment mac_ptr here, so this is just
15547 a look-ahead. */
15548 next_type = read_1_byte (abfd, mac_ptr);
15549 if (next_type != 0)
4d3c2250 15550 complaint (&symfile_complaints,
3e43a32a
MS
15551 _("no terminating 0-type entry for "
15552 "macros in `.debug_macinfo' section"));
2e276125
JB
15553
15554 return;
15555 }
15556 }
15557 break;
15558
cf2c3c16
TT
15559 case DW_MACRO_GNU_transparent_include:
15560 {
15561 LONGEST offset;
8fc3fc34 15562 void **slot;
cf2c3c16
TT
15563
15564 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15565 mac_ptr += offset_size;
15566
8fc3fc34
TT
15567 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15568 if (*slot != NULL)
15569 {
15570 /* This has actually happened; see
15571 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15572 complaint (&symfile_complaints,
15573 _("recursive DW_MACRO_GNU_transparent_include in "
15574 ".debug_macro section"));
15575 }
15576 else
15577 {
15578 *slot = mac_ptr;
15579
15580 dwarf_decode_macro_bytes (abfd,
15581 section->buffer + offset,
15582 mac_end, current_file,
15583 lh, comp_dir,
15584 section, section_is_gnu,
15585 offset_size, objfile, include_hash);
15586
15587 htab_remove_elt (include_hash, mac_ptr);
15588 }
cf2c3c16
TT
15589 }
15590 break;
15591
2e276125 15592 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15593 if (!section_is_gnu)
15594 {
15595 unsigned int bytes_read;
15596 int constant;
2e276125 15597
cf2c3c16
TT
15598 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15599 mac_ptr += bytes_read;
15600 read_direct_string (abfd, mac_ptr, &bytes_read);
15601 mac_ptr += bytes_read;
2e276125 15602
cf2c3c16
TT
15603 /* We don't recognize any vendor extensions. */
15604 break;
15605 }
15606 /* FALLTHROUGH */
15607
15608 default:
15609 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15610 mac_ptr, abfd, offset_size,
15611 section);
15612 if (mac_ptr == NULL)
15613 return;
15614 break;
2e276125 15615 }
757a13d0 15616 } while (macinfo_type != 0);
2e276125 15617}
8e19ed76 15618
cf2c3c16
TT
15619static void
15620dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15621 char *comp_dir, bfd *abfd,
15622 struct dwarf2_cu *cu,
15623 struct dwarf2_section_info *section,
15624 int section_is_gnu)
15625{
bb5ed363 15626 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15627 gdb_byte *mac_ptr, *mac_end;
15628 struct macro_source_file *current_file = 0;
15629 enum dwarf_macro_record_type macinfo_type;
15630 unsigned int offset_size = cu->header.offset_size;
15631 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15632 struct cleanup *cleanup;
15633 htab_t include_hash;
15634 void **slot;
cf2c3c16 15635
bb5ed363 15636 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15637 if (section->buffer == NULL)
15638 {
15639 complaint (&symfile_complaints, _("missing %s section"),
15640 section->asection->name);
15641 return;
15642 }
15643
15644 /* First pass: Find the name of the base filename.
15645 This filename is needed in order to process all macros whose definition
15646 (or undefinition) comes from the command line. These macros are defined
15647 before the first DW_MACINFO_start_file entry, and yet still need to be
15648 associated to the base file.
15649
15650 To determine the base file name, we scan the macro definitions until we
15651 reach the first DW_MACINFO_start_file entry. We then initialize
15652 CURRENT_FILE accordingly so that any macro definition found before the
15653 first DW_MACINFO_start_file can still be associated to the base file. */
15654
15655 mac_ptr = section->buffer + offset;
15656 mac_end = section->buffer + section->size;
15657
15658 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15659 &offset_size, section_is_gnu);
15660 if (mac_ptr == NULL)
15661 {
15662 /* We already issued a complaint. */
15663 return;
15664 }
15665
15666 do
15667 {
15668 /* Do we at least have room for a macinfo type byte? */
15669 if (mac_ptr >= mac_end)
15670 {
15671 /* Complaint is printed during the second pass as GDB will probably
15672 stop the first pass earlier upon finding
15673 DW_MACINFO_start_file. */
15674 break;
15675 }
15676
15677 macinfo_type = read_1_byte (abfd, mac_ptr);
15678 mac_ptr++;
15679
15680 /* Note that we rely on the fact that the corresponding GNU and
15681 DWARF constants are the same. */
15682 switch (macinfo_type)
15683 {
15684 /* A zero macinfo type indicates the end of the macro
15685 information. */
15686 case 0:
15687 break;
15688
15689 case DW_MACRO_GNU_define:
15690 case DW_MACRO_GNU_undef:
15691 /* Only skip the data by MAC_PTR. */
15692 {
15693 unsigned int bytes_read;
15694
15695 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15696 mac_ptr += bytes_read;
15697 read_direct_string (abfd, mac_ptr, &bytes_read);
15698 mac_ptr += bytes_read;
15699 }
15700 break;
15701
15702 case DW_MACRO_GNU_start_file:
15703 {
15704 unsigned int bytes_read;
15705 int line, file;
15706
15707 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15708 mac_ptr += bytes_read;
15709 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15710 mac_ptr += bytes_read;
15711
15712 current_file = macro_start_file (file, line, current_file,
bb5ed363 15713 comp_dir, lh, objfile);
cf2c3c16
TT
15714 }
15715 break;
15716
15717 case DW_MACRO_GNU_end_file:
15718 /* No data to skip by MAC_PTR. */
15719 break;
15720
15721 case DW_MACRO_GNU_define_indirect:
15722 case DW_MACRO_GNU_undef_indirect:
15723 {
15724 unsigned int bytes_read;
15725
15726 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15727 mac_ptr += bytes_read;
15728 mac_ptr += offset_size;
15729 }
15730 break;
15731
15732 case DW_MACRO_GNU_transparent_include:
15733 /* Note that, according to the spec, a transparent include
15734 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15735 skip this opcode. */
15736 mac_ptr += offset_size;
15737 break;
15738
15739 case DW_MACINFO_vendor_ext:
15740 /* Only skip the data by MAC_PTR. */
15741 if (!section_is_gnu)
15742 {
15743 unsigned int bytes_read;
15744
15745 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15746 mac_ptr += bytes_read;
15747 read_direct_string (abfd, mac_ptr, &bytes_read);
15748 mac_ptr += bytes_read;
15749 }
15750 /* FALLTHROUGH */
15751
15752 default:
15753 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15754 mac_ptr, abfd, offset_size,
15755 section);
15756 if (mac_ptr == NULL)
15757 return;
15758 break;
15759 }
15760 } while (macinfo_type != 0 && current_file == NULL);
15761
15762 /* Second pass: Process all entries.
15763
15764 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15765 command-line macro definitions/undefinitions. This flag is unset when we
15766 reach the first DW_MACINFO_start_file entry. */
15767
8fc3fc34
TT
15768 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15769 NULL, xcalloc, xfree);
15770 cleanup = make_cleanup_htab_delete (include_hash);
15771 mac_ptr = section->buffer + offset;
15772 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15773 *slot = mac_ptr;
15774 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 15775 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
15776 offset_size, objfile, include_hash);
15777 do_cleanups (cleanup);
cf2c3c16
TT
15778}
15779
8e19ed76 15780/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15781 if so return true else false. */
380bca97 15782
8e19ed76
PS
15783static int
15784attr_form_is_block (struct attribute *attr)
15785{
15786 return (attr == NULL ? 0 :
15787 attr->form == DW_FORM_block1
15788 || attr->form == DW_FORM_block2
15789 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15790 || attr->form == DW_FORM_block
15791 || attr->form == DW_FORM_exprloc);
8e19ed76 15792}
4c2df51b 15793
c6a0999f
JB
15794/* Return non-zero if ATTR's value is a section offset --- classes
15795 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15796 You may use DW_UNSND (attr) to retrieve such offsets.
15797
15798 Section 7.5.4, "Attribute Encodings", explains that no attribute
15799 may have a value that belongs to more than one of these classes; it
15800 would be ambiguous if we did, because we use the same forms for all
15801 of them. */
380bca97 15802
3690dd37
JB
15803static int
15804attr_form_is_section_offset (struct attribute *attr)
15805{
15806 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15807 || attr->form == DW_FORM_data8
15808 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15809}
15810
15811
15812/* Return non-zero if ATTR's value falls in the 'constant' class, or
15813 zero otherwise. When this function returns true, you can apply
15814 dwarf2_get_attr_constant_value to it.
15815
15816 However, note that for some attributes you must check
15817 attr_form_is_section_offset before using this test. DW_FORM_data4
15818 and DW_FORM_data8 are members of both the constant class, and of
15819 the classes that contain offsets into other debug sections
15820 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15821 that, if an attribute's can be either a constant or one of the
15822 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15823 taken as section offsets, not constants. */
380bca97 15824
3690dd37
JB
15825static int
15826attr_form_is_constant (struct attribute *attr)
15827{
15828 switch (attr->form)
15829 {
15830 case DW_FORM_sdata:
15831 case DW_FORM_udata:
15832 case DW_FORM_data1:
15833 case DW_FORM_data2:
15834 case DW_FORM_data4:
15835 case DW_FORM_data8:
15836 return 1;
15837 default:
15838 return 0;
15839 }
15840}
15841
8cf6f0b1
TT
15842/* A helper function that fills in a dwarf2_loclist_baton. */
15843
15844static void
15845fill_in_loclist_baton (struct dwarf2_cu *cu,
15846 struct dwarf2_loclist_baton *baton,
15847 struct attribute *attr)
15848{
15849 dwarf2_read_section (dwarf2_per_objfile->objfile,
15850 &dwarf2_per_objfile->loc);
15851
15852 baton->per_cu = cu->per_cu;
15853 gdb_assert (baton->per_cu);
15854 /* We don't know how long the location list is, but make sure we
15855 don't run off the edge of the section. */
15856 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15857 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15858 baton->base_address = cu->base_address;
15859}
15860
4c2df51b
DJ
15861static void
15862dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15863 struct dwarf2_cu *cu)
4c2df51b 15864{
bb5ed363
DE
15865 struct objfile *objfile = dwarf2_per_objfile->objfile;
15866
3690dd37 15867 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15868 /* ".debug_loc" may not exist at all, or the offset may be outside
15869 the section. If so, fall through to the complaint in the
15870 other branch. */
bb5ed363 15871 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15872 &dwarf2_per_objfile->loc))
4c2df51b 15873 {
0d53c4c4 15874 struct dwarf2_loclist_baton *baton;
4c2df51b 15875
bb5ed363 15876 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15877 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15878
8cf6f0b1 15879 fill_in_loclist_baton (cu, baton, attr);
be391dca 15880
d00adf39 15881 if (cu->base_known == 0)
0d53c4c4 15882 complaint (&symfile_complaints,
3e43a32a
MS
15883 _("Location list used without "
15884 "specifying the CU base address."));
4c2df51b 15885
768a979c 15886 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15887 SYMBOL_LOCATION_BATON (sym) = baton;
15888 }
15889 else
15890 {
15891 struct dwarf2_locexpr_baton *baton;
15892
bb5ed363 15893 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15894 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15895 baton->per_cu = cu->per_cu;
15896 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15897
15898 if (attr_form_is_block (attr))
15899 {
15900 /* Note that we're just copying the block's data pointer
15901 here, not the actual data. We're still pointing into the
6502dd73
DJ
15902 info_buffer for SYM's objfile; right now we never release
15903 that buffer, but when we do clean up properly this may
15904 need to change. */
0d53c4c4
DJ
15905 baton->size = DW_BLOCK (attr)->size;
15906 baton->data = DW_BLOCK (attr)->data;
15907 }
15908 else
15909 {
15910 dwarf2_invalid_attrib_class_complaint ("location description",
15911 SYMBOL_NATURAL_NAME (sym));
15912 baton->size = 0;
0d53c4c4 15913 }
6e70227d 15914
768a979c 15915 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15916 SYMBOL_LOCATION_BATON (sym) = baton;
15917 }
4c2df51b 15918}
6502dd73 15919
9aa1f1e3
TT
15920/* Return the OBJFILE associated with the compilation unit CU. If CU
15921 came from a separate debuginfo file, then the master objfile is
15922 returned. */
ae0d2f24
UW
15923
15924struct objfile *
15925dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15926{
9291a0cd 15927 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15928
15929 /* Return the master objfile, so that we can report and look up the
15930 correct file containing this variable. */
15931 if (objfile->separate_debug_objfile_backlink)
15932 objfile = objfile->separate_debug_objfile_backlink;
15933
15934 return objfile;
15935}
15936
96408a79
SA
15937/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15938 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15939 CU_HEADERP first. */
15940
15941static const struct comp_unit_head *
15942per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15943 struct dwarf2_per_cu_data *per_cu)
15944{
15945 struct objfile *objfile;
15946 struct dwarf2_per_objfile *per_objfile;
15947 gdb_byte *info_ptr;
15948
15949 if (per_cu->cu)
15950 return &per_cu->cu->header;
15951
15952 objfile = per_cu->objfile;
15953 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 15954 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
15955
15956 memset (cu_headerp, 0, sizeof (*cu_headerp));
15957 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15958
15959 return cu_headerp;
15960}
15961
ae0d2f24
UW
15962/* Return the address size given in the compilation unit header for CU. */
15963
98714339 15964int
ae0d2f24
UW
15965dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15966{
96408a79
SA
15967 struct comp_unit_head cu_header_local;
15968 const struct comp_unit_head *cu_headerp;
c471e790 15969
96408a79
SA
15970 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15971
15972 return cu_headerp->addr_size;
ae0d2f24
UW
15973}
15974
9eae7c52
TT
15975/* Return the offset size given in the compilation unit header for CU. */
15976
15977int
15978dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15979{
96408a79
SA
15980 struct comp_unit_head cu_header_local;
15981 const struct comp_unit_head *cu_headerp;
9c6c53f7 15982
96408a79
SA
15983 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15984
15985 return cu_headerp->offset_size;
15986}
15987
15988/* See its dwarf2loc.h declaration. */
15989
15990int
15991dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15992{
15993 struct comp_unit_head cu_header_local;
15994 const struct comp_unit_head *cu_headerp;
15995
15996 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15997
15998 if (cu_headerp->version == 2)
15999 return cu_headerp->addr_size;
16000 else
16001 return cu_headerp->offset_size;
181cebd4
JK
16002}
16003
9aa1f1e3
TT
16004/* Return the text offset of the CU. The returned offset comes from
16005 this CU's objfile. If this objfile came from a separate debuginfo
16006 file, then the offset may be different from the corresponding
16007 offset in the parent objfile. */
16008
16009CORE_ADDR
16010dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16011{
bb3fa9d0 16012 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16013
16014 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16015}
16016
348e048f
DE
16017/* Locate the .debug_info compilation unit from CU's objfile which contains
16018 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16019
16020static struct dwarf2_per_cu_data *
b64f50a1 16021dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16022 struct objfile *objfile)
16023{
16024 struct dwarf2_per_cu_data *this_cu;
16025 int low, high;
16026
ae038cb0
DJ
16027 low = 0;
16028 high = dwarf2_per_objfile->n_comp_units - 1;
16029 while (high > low)
16030 {
16031 int mid = low + (high - low) / 2;
9a619af0 16032
b64f50a1
JK
16033 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16034 >= offset.sect_off)
ae038cb0
DJ
16035 high = mid;
16036 else
16037 low = mid + 1;
16038 }
16039 gdb_assert (low == high);
b64f50a1
JK
16040 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16041 > offset.sect_off)
ae038cb0 16042 {
10b3939b 16043 if (low == 0)
8a3fe4f8
AC
16044 error (_("Dwarf Error: could not find partial DIE containing "
16045 "offset 0x%lx [in module %s]"),
b64f50a1 16046 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16047
b64f50a1
JK
16048 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16049 <= offset.sect_off);
ae038cb0
DJ
16050 return dwarf2_per_objfile->all_comp_units[low-1];
16051 }
16052 else
16053 {
16054 this_cu = dwarf2_per_objfile->all_comp_units[low];
16055 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16056 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16057 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16058 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16059 return this_cu;
16060 }
16061}
16062
23745b47 16063/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16064
9816fde3 16065static void
23745b47 16066init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16067{
9816fde3 16068 memset (cu, 0, sizeof (*cu));
23745b47
DE
16069 per_cu->cu = cu;
16070 cu->per_cu = per_cu;
16071 cu->objfile = per_cu->objfile;
93311388 16072 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16073}
16074
16075/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16076
16077static void
16078prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
16079{
16080 struct attribute *attr;
16081
16082 /* Set the language we're debugging. */
16083 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16084 if (attr)
16085 set_cu_language (DW_UNSND (attr), cu);
16086 else
9cded63f
TT
16087 {
16088 cu->language = language_minimal;
16089 cu->language_defn = language_def (cu->language);
16090 }
93311388
DE
16091}
16092
ae038cb0
DJ
16093/* Release one cached compilation unit, CU. We unlink it from the tree
16094 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16095 the caller is responsible for that.
16096 NOTE: DATA is a void * because this function is also used as a
16097 cleanup routine. */
ae038cb0
DJ
16098
16099static void
68dc6402 16100free_heap_comp_unit (void *data)
ae038cb0
DJ
16101{
16102 struct dwarf2_cu *cu = data;
16103
23745b47
DE
16104 gdb_assert (cu->per_cu != NULL);
16105 cu->per_cu->cu = NULL;
ae038cb0
DJ
16106 cu->per_cu = NULL;
16107
16108 obstack_free (&cu->comp_unit_obstack, NULL);
16109
16110 xfree (cu);
16111}
16112
72bf9492 16113/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
16114 when we're finished with it. We can't free the pointer itself, but be
16115 sure to unlink it from the cache. Also release any associated storage
16116 and perform cache maintenance.
72bf9492
DJ
16117
16118 Only used during partial symbol parsing. */
16119
16120static void
16121free_stack_comp_unit (void *data)
16122{
16123 struct dwarf2_cu *cu = data;
16124
23745b47
DE
16125 gdb_assert (cu->per_cu != NULL);
16126 cu->per_cu->cu = NULL;
16127 cu->per_cu = NULL;
16128
72bf9492
DJ
16129 obstack_free (&cu->comp_unit_obstack, NULL);
16130 cu->partial_dies = NULL;
ae038cb0 16131
23745b47
DE
16132 /* The previous code only did this if per_cu != NULL.
16133 But that would always succeed, so now we just unconditionally do
16134 the aging. This seems like the wrong place to do such aging,
16135 but cleaning that up is left for later. */
16136 age_cached_comp_units ();
ae038cb0
DJ
16137}
16138
16139/* Free all cached compilation units. */
16140
16141static void
16142free_cached_comp_units (void *data)
16143{
16144 struct dwarf2_per_cu_data *per_cu, **last_chain;
16145
16146 per_cu = dwarf2_per_objfile->read_in_chain;
16147 last_chain = &dwarf2_per_objfile->read_in_chain;
16148 while (per_cu != NULL)
16149 {
16150 struct dwarf2_per_cu_data *next_cu;
16151
16152 next_cu = per_cu->cu->read_in_chain;
16153
68dc6402 16154 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16155 *last_chain = next_cu;
16156
16157 per_cu = next_cu;
16158 }
16159}
16160
16161/* Increase the age counter on each cached compilation unit, and free
16162 any that are too old. */
16163
16164static void
16165age_cached_comp_units (void)
16166{
16167 struct dwarf2_per_cu_data *per_cu, **last_chain;
16168
16169 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16170 per_cu = dwarf2_per_objfile->read_in_chain;
16171 while (per_cu != NULL)
16172 {
16173 per_cu->cu->last_used ++;
16174 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16175 dwarf2_mark (per_cu->cu);
16176 per_cu = per_cu->cu->read_in_chain;
16177 }
16178
16179 per_cu = dwarf2_per_objfile->read_in_chain;
16180 last_chain = &dwarf2_per_objfile->read_in_chain;
16181 while (per_cu != NULL)
16182 {
16183 struct dwarf2_per_cu_data *next_cu;
16184
16185 next_cu = per_cu->cu->read_in_chain;
16186
16187 if (!per_cu->cu->mark)
16188 {
68dc6402 16189 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16190 *last_chain = next_cu;
16191 }
16192 else
16193 last_chain = &per_cu->cu->read_in_chain;
16194
16195 per_cu = next_cu;
16196 }
16197}
16198
16199/* Remove a single compilation unit from the cache. */
16200
16201static void
16202free_one_cached_comp_unit (void *target_cu)
16203{
16204 struct dwarf2_per_cu_data *per_cu, **last_chain;
16205
16206 per_cu = dwarf2_per_objfile->read_in_chain;
16207 last_chain = &dwarf2_per_objfile->read_in_chain;
16208 while (per_cu != NULL)
16209 {
16210 struct dwarf2_per_cu_data *next_cu;
16211
16212 next_cu = per_cu->cu->read_in_chain;
16213
16214 if (per_cu->cu == target_cu)
16215 {
68dc6402 16216 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16217 *last_chain = next_cu;
16218 break;
16219 }
16220 else
16221 last_chain = &per_cu->cu->read_in_chain;
16222
16223 per_cu = next_cu;
16224 }
16225}
16226
fe3e1990
DJ
16227/* Release all extra memory associated with OBJFILE. */
16228
16229void
16230dwarf2_free_objfile (struct objfile *objfile)
16231{
16232 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16233
16234 if (dwarf2_per_objfile == NULL)
16235 return;
16236
16237 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16238 free_cached_comp_units (NULL);
16239
7b9f3c50
DE
16240 if (dwarf2_per_objfile->quick_file_names_table)
16241 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16242
fe3e1990
DJ
16243 /* Everything else should be on the objfile obstack. */
16244}
16245
1c379e20
DJ
16246/* A pair of DIE offset and GDB type pointer. We store these
16247 in a hash table separate from the DIEs, and preserve them
16248 when the DIEs are flushed out of cache. */
16249
16250struct dwarf2_offset_and_type
16251{
b64f50a1 16252 sect_offset offset;
1c379e20
DJ
16253 struct type *type;
16254};
16255
16256/* Hash function for a dwarf2_offset_and_type. */
16257
16258static hashval_t
16259offset_and_type_hash (const void *item)
16260{
16261 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16262
b64f50a1 16263 return ofs->offset.sect_off;
1c379e20
DJ
16264}
16265
16266/* Equality function for a dwarf2_offset_and_type. */
16267
16268static int
16269offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16270{
16271 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16272 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16273
b64f50a1 16274 return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
1c379e20
DJ
16275}
16276
16277/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16278 table if necessary. For convenience, return TYPE.
16279
16280 The DIEs reading must have careful ordering to:
16281 * Not cause infite loops trying to read in DIEs as a prerequisite for
16282 reading current DIE.
16283 * Not trying to dereference contents of still incompletely read in types
16284 while reading in other DIEs.
16285 * Enable referencing still incompletely read in types just by a pointer to
16286 the type without accessing its fields.
16287
16288 Therefore caller should follow these rules:
16289 * Try to fetch any prerequisite types we may need to build this DIE type
16290 before building the type and calling set_die_type.
e71ec853 16291 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16292 possible before fetching more types to complete the current type.
16293 * Make the type as complete as possible before fetching more types. */
1c379e20 16294
f792889a 16295static struct type *
1c379e20
DJ
16296set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16297{
16298 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16299 struct objfile *objfile = cu->objfile;
16300 htab_t *type_hash_ptr;
1c379e20 16301
b4ba55a1
JB
16302 /* For Ada types, make sure that the gnat-specific data is always
16303 initialized (if not already set). There are a few types where
16304 we should not be doing so, because the type-specific area is
16305 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16306 where the type-specific area is used to store the floatformat).
16307 But this is not a problem, because the gnat-specific information
16308 is actually not needed for these types. */
16309 if (need_gnat_info (cu)
16310 && TYPE_CODE (type) != TYPE_CODE_FUNC
16311 && TYPE_CODE (type) != TYPE_CODE_FLT
16312 && !HAVE_GNAT_AUX_INFO (type))
16313 INIT_GNAT_SPECIFIC (type);
16314
b0df02fd 16315 if (cu->per_cu->debug_types_section)
673bfd45
DE
16316 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16317 else
16318 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16319
16320 if (*type_hash_ptr == NULL)
f792889a 16321 {
673bfd45
DE
16322 *type_hash_ptr
16323 = htab_create_alloc_ex (127,
f792889a
DJ
16324 offset_and_type_hash,
16325 offset_and_type_eq,
16326 NULL,
673bfd45 16327 &objfile->objfile_obstack,
f792889a
DJ
16328 hashtab_obstack_allocate,
16329 dummy_obstack_deallocate);
f792889a 16330 }
1c379e20
DJ
16331
16332 ofs.offset = die->offset;
16333 ofs.type = type;
16334 slot = (struct dwarf2_offset_and_type **)
b64f50a1
JK
16335 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16336 INSERT);
7e314c57
JK
16337 if (*slot)
16338 complaint (&symfile_complaints,
16339 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16340 die->offset.sect_off);
673bfd45 16341 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16342 **slot = ofs;
f792889a 16343 return type;
1c379e20
DJ
16344}
16345
380bca97 16346/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16347 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16348
16349static struct type *
b64f50a1 16350get_die_type_at_offset (sect_offset offset,
673bfd45 16351 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16352{
16353 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16354 htab_t type_hash;
f792889a 16355
b0df02fd 16356 if (per_cu->debug_types_section)
673bfd45
DE
16357 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16358 else
16359 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16360 if (type_hash == NULL)
16361 return NULL;
1c379e20 16362
673bfd45 16363 ofs.offset = offset;
b64f50a1 16364 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
1c379e20
DJ
16365 if (slot)
16366 return slot->type;
16367 else
16368 return NULL;
16369}
16370
673bfd45
DE
16371/* Look up the type for DIE in the appropriate type_hash table,
16372 or return NULL if DIE does not have a saved type. */
16373
16374static struct type *
16375get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16376{
16377 return get_die_type_at_offset (die->offset, cu->per_cu);
16378}
16379
10b3939b
DJ
16380/* Add a dependence relationship from CU to REF_PER_CU. */
16381
16382static void
16383dwarf2_add_dependence (struct dwarf2_cu *cu,
16384 struct dwarf2_per_cu_data *ref_per_cu)
16385{
16386 void **slot;
16387
16388 if (cu->dependencies == NULL)
16389 cu->dependencies
16390 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16391 NULL, &cu->comp_unit_obstack,
16392 hashtab_obstack_allocate,
16393 dummy_obstack_deallocate);
16394
16395 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16396 if (*slot == NULL)
16397 *slot = ref_per_cu;
16398}
1c379e20 16399
f504f079
DE
16400/* Subroutine of dwarf2_mark to pass to htab_traverse.
16401 Set the mark field in every compilation unit in the
ae038cb0
DJ
16402 cache that we must keep because we are keeping CU. */
16403
10b3939b
DJ
16404static int
16405dwarf2_mark_helper (void **slot, void *data)
16406{
16407 struct dwarf2_per_cu_data *per_cu;
16408
16409 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16410
16411 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16412 reading of the chain. As such dependencies remain valid it is not much
16413 useful to track and undo them during QUIT cleanups. */
16414 if (per_cu->cu == NULL)
16415 return 1;
16416
10b3939b
DJ
16417 if (per_cu->cu->mark)
16418 return 1;
16419 per_cu->cu->mark = 1;
16420
16421 if (per_cu->cu->dependencies != NULL)
16422 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16423
16424 return 1;
16425}
16426
f504f079
DE
16427/* Set the mark field in CU and in every other compilation unit in the
16428 cache that we must keep because we are keeping CU. */
16429
ae038cb0
DJ
16430static void
16431dwarf2_mark (struct dwarf2_cu *cu)
16432{
16433 if (cu->mark)
16434 return;
16435 cu->mark = 1;
10b3939b
DJ
16436 if (cu->dependencies != NULL)
16437 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16438}
16439
16440static void
16441dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16442{
16443 while (per_cu)
16444 {
16445 per_cu->cu->mark = 0;
16446 per_cu = per_cu->cu->read_in_chain;
16447 }
72bf9492
DJ
16448}
16449
72bf9492
DJ
16450/* Trivial hash function for partial_die_info: the hash value of a DIE
16451 is its offset in .debug_info for this objfile. */
16452
16453static hashval_t
16454partial_die_hash (const void *item)
16455{
16456 const struct partial_die_info *part_die = item;
9a619af0 16457
b64f50a1 16458 return part_die->offset.sect_off;
72bf9492
DJ
16459}
16460
16461/* Trivial comparison function for partial_die_info structures: two DIEs
16462 are equal if they have the same offset. */
16463
16464static int
16465partial_die_eq (const void *item_lhs, const void *item_rhs)
16466{
16467 const struct partial_die_info *part_die_lhs = item_lhs;
16468 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16469
b64f50a1 16470 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
16471}
16472
ae038cb0
DJ
16473static struct cmd_list_element *set_dwarf2_cmdlist;
16474static struct cmd_list_element *show_dwarf2_cmdlist;
16475
16476static void
16477set_dwarf2_cmd (char *args, int from_tty)
16478{
16479 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16480}
16481
16482static void
16483show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16484{
ae038cb0
DJ
16485 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16486}
16487
dce234bc
PP
16488/* If section described by INFO was mmapped, munmap it now. */
16489
16490static void
16491munmap_section_buffer (struct dwarf2_section_info *info)
16492{
b315ab21 16493 if (info->map_addr != NULL)
dce234bc
PP
16494 {
16495#ifdef HAVE_MMAP
b315ab21 16496 int res;
9a619af0 16497
b315ab21
TG
16498 res = munmap (info->map_addr, info->map_len);
16499 gdb_assert (res == 0);
dce234bc
PP
16500#else
16501 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16502 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16503#endif
16504 }
16505}
16506
16507/* munmap debug sections for OBJFILE, if necessary. */
16508
16509static void
c1bd65d0 16510dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16511{
16512 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16513 int ix;
16514 struct dwarf2_section_info *section;
9a619af0 16515
16be1145
DE
16516 /* This is sorted according to the order they're defined in to make it easier
16517 to keep in sync. */
dce234bc
PP
16518 munmap_section_buffer (&data->info);
16519 munmap_section_buffer (&data->abbrev);
16520 munmap_section_buffer (&data->line);
16be1145 16521 munmap_section_buffer (&data->loc);
dce234bc 16522 munmap_section_buffer (&data->macinfo);
cf2c3c16 16523 munmap_section_buffer (&data->macro);
16be1145 16524 munmap_section_buffer (&data->str);
dce234bc 16525 munmap_section_buffer (&data->ranges);
dce234bc
PP
16526 munmap_section_buffer (&data->frame);
16527 munmap_section_buffer (&data->eh_frame);
9291a0cd 16528 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16529
16530 for (ix = 0;
16531 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16532 ++ix)
16533 munmap_section_buffer (section);
16534
16535 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16536}
16537
16538\f
ae2de4f8 16539/* The "save gdb-index" command. */
9291a0cd
TT
16540
16541/* The contents of the hash table we create when building the string
16542 table. */
16543struct strtab_entry
16544{
16545 offset_type offset;
16546 const char *str;
16547};
16548
559a7a62
JK
16549/* Hash function for a strtab_entry.
16550
16551 Function is used only during write_hash_table so no index format backward
16552 compatibility is needed. */
b89be57b 16553
9291a0cd
TT
16554static hashval_t
16555hash_strtab_entry (const void *e)
16556{
16557 const struct strtab_entry *entry = e;
559a7a62 16558 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16559}
16560
16561/* Equality function for a strtab_entry. */
b89be57b 16562
9291a0cd
TT
16563static int
16564eq_strtab_entry (const void *a, const void *b)
16565{
16566 const struct strtab_entry *ea = a;
16567 const struct strtab_entry *eb = b;
16568 return !strcmp (ea->str, eb->str);
16569}
16570
16571/* Create a strtab_entry hash table. */
b89be57b 16572
9291a0cd
TT
16573static htab_t
16574create_strtab (void)
16575{
16576 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16577 xfree, xcalloc, xfree);
16578}
16579
16580/* Add a string to the constant pool. Return the string's offset in
16581 host order. */
b89be57b 16582
9291a0cd
TT
16583static offset_type
16584add_string (htab_t table, struct obstack *cpool, const char *str)
16585{
16586 void **slot;
16587 struct strtab_entry entry;
16588 struct strtab_entry *result;
16589
16590 entry.str = str;
16591 slot = htab_find_slot (table, &entry, INSERT);
16592 if (*slot)
16593 result = *slot;
16594 else
16595 {
16596 result = XNEW (struct strtab_entry);
16597 result->offset = obstack_object_size (cpool);
16598 result->str = str;
16599 obstack_grow_str0 (cpool, str);
16600 *slot = result;
16601 }
16602 return result->offset;
16603}
16604
16605/* An entry in the symbol table. */
16606struct symtab_index_entry
16607{
16608 /* The name of the symbol. */
16609 const char *name;
16610 /* The offset of the name in the constant pool. */
16611 offset_type index_offset;
16612 /* A sorted vector of the indices of all the CUs that hold an object
16613 of this name. */
16614 VEC (offset_type) *cu_indices;
16615};
16616
16617/* The symbol table. This is a power-of-2-sized hash table. */
16618struct mapped_symtab
16619{
16620 offset_type n_elements;
16621 offset_type size;
16622 struct symtab_index_entry **data;
16623};
16624
16625/* Hash function for a symtab_index_entry. */
b89be57b 16626
9291a0cd
TT
16627static hashval_t
16628hash_symtab_entry (const void *e)
16629{
16630 const struct symtab_index_entry *entry = e;
16631 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16632 sizeof (offset_type) * VEC_length (offset_type,
16633 entry->cu_indices),
16634 0);
16635}
16636
16637/* Equality function for a symtab_index_entry. */
b89be57b 16638
9291a0cd
TT
16639static int
16640eq_symtab_entry (const void *a, const void *b)
16641{
16642 const struct symtab_index_entry *ea = a;
16643 const struct symtab_index_entry *eb = b;
16644 int len = VEC_length (offset_type, ea->cu_indices);
16645 if (len != VEC_length (offset_type, eb->cu_indices))
16646 return 0;
16647 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16648 VEC_address (offset_type, eb->cu_indices),
16649 sizeof (offset_type) * len);
16650}
16651
16652/* Destroy a symtab_index_entry. */
b89be57b 16653
9291a0cd
TT
16654static void
16655delete_symtab_entry (void *p)
16656{
16657 struct symtab_index_entry *entry = p;
16658 VEC_free (offset_type, entry->cu_indices);
16659 xfree (entry);
16660}
16661
16662/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16663
9291a0cd 16664static htab_t
3876f04e 16665create_symbol_hash_table (void)
9291a0cd
TT
16666{
16667 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16668 delete_symtab_entry, xcalloc, xfree);
16669}
16670
16671/* Create a new mapped symtab object. */
b89be57b 16672
9291a0cd
TT
16673static struct mapped_symtab *
16674create_mapped_symtab (void)
16675{
16676 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16677 symtab->n_elements = 0;
16678 symtab->size = 1024;
16679 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16680 return symtab;
16681}
16682
16683/* Destroy a mapped_symtab. */
b89be57b 16684
9291a0cd
TT
16685static void
16686cleanup_mapped_symtab (void *p)
16687{
16688 struct mapped_symtab *symtab = p;
16689 /* The contents of the array are freed when the other hash table is
16690 destroyed. */
16691 xfree (symtab->data);
16692 xfree (symtab);
16693}
16694
16695/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16696 the slot.
16697
16698 Function is used only during write_hash_table so no index format backward
16699 compatibility is needed. */
b89be57b 16700
9291a0cd
TT
16701static struct symtab_index_entry **
16702find_slot (struct mapped_symtab *symtab, const char *name)
16703{
559a7a62 16704 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16705
16706 index = hash & (symtab->size - 1);
16707 step = ((hash * 17) & (symtab->size - 1)) | 1;
16708
16709 for (;;)
16710 {
16711 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16712 return &symtab->data[index];
16713 index = (index + step) & (symtab->size - 1);
16714 }
16715}
16716
16717/* Expand SYMTAB's hash table. */
b89be57b 16718
9291a0cd
TT
16719static void
16720hash_expand (struct mapped_symtab *symtab)
16721{
16722 offset_type old_size = symtab->size;
16723 offset_type i;
16724 struct symtab_index_entry **old_entries = symtab->data;
16725
16726 symtab->size *= 2;
16727 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16728
16729 for (i = 0; i < old_size; ++i)
16730 {
16731 if (old_entries[i])
16732 {
16733 struct symtab_index_entry **slot = find_slot (symtab,
16734 old_entries[i]->name);
16735 *slot = old_entries[i];
16736 }
16737 }
16738
16739 xfree (old_entries);
16740}
16741
16742/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16743 is the index of the CU in which the symbol appears. */
b89be57b 16744
9291a0cd
TT
16745static void
16746add_index_entry (struct mapped_symtab *symtab, const char *name,
16747 offset_type cu_index)
16748{
16749 struct symtab_index_entry **slot;
16750
16751 ++symtab->n_elements;
16752 if (4 * symtab->n_elements / 3 >= symtab->size)
16753 hash_expand (symtab);
16754
16755 slot = find_slot (symtab, name);
16756 if (!*slot)
16757 {
16758 *slot = XNEW (struct symtab_index_entry);
16759 (*slot)->name = name;
16760 (*slot)->cu_indices = NULL;
16761 }
16762 /* Don't push an index twice. Due to how we add entries we only
16763 have to check the last one. */
16764 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16765 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16766 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16767}
16768
16769/* Add a vector of indices to the constant pool. */
b89be57b 16770
9291a0cd 16771static offset_type
3876f04e 16772add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16773 struct symtab_index_entry *entry)
16774{
16775 void **slot;
16776
3876f04e 16777 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16778 if (!*slot)
16779 {
16780 offset_type len = VEC_length (offset_type, entry->cu_indices);
16781 offset_type val = MAYBE_SWAP (len);
16782 offset_type iter;
16783 int i;
16784
16785 *slot = entry;
16786 entry->index_offset = obstack_object_size (cpool);
16787
16788 obstack_grow (cpool, &val, sizeof (val));
16789 for (i = 0;
16790 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16791 ++i)
16792 {
16793 val = MAYBE_SWAP (iter);
16794 obstack_grow (cpool, &val, sizeof (val));
16795 }
16796 }
16797 else
16798 {
16799 struct symtab_index_entry *old_entry = *slot;
16800 entry->index_offset = old_entry->index_offset;
16801 entry = old_entry;
16802 }
16803 return entry->index_offset;
16804}
16805
16806/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16807 constant pool entries going into the obstack CPOOL. */
b89be57b 16808
9291a0cd
TT
16809static void
16810write_hash_table (struct mapped_symtab *symtab,
16811 struct obstack *output, struct obstack *cpool)
16812{
16813 offset_type i;
3876f04e 16814 htab_t symbol_hash_table;
9291a0cd
TT
16815 htab_t str_table;
16816
3876f04e 16817 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16818 str_table = create_strtab ();
3876f04e 16819
9291a0cd
TT
16820 /* We add all the index vectors to the constant pool first, to
16821 ensure alignment is ok. */
16822 for (i = 0; i < symtab->size; ++i)
16823 {
16824 if (symtab->data[i])
3876f04e 16825 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16826 }
16827
16828 /* Now write out the hash table. */
16829 for (i = 0; i < symtab->size; ++i)
16830 {
16831 offset_type str_off, vec_off;
16832
16833 if (symtab->data[i])
16834 {
16835 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16836 vec_off = symtab->data[i]->index_offset;
16837 }
16838 else
16839 {
16840 /* While 0 is a valid constant pool index, it is not valid
16841 to have 0 for both offsets. */
16842 str_off = 0;
16843 vec_off = 0;
16844 }
16845
16846 str_off = MAYBE_SWAP (str_off);
16847 vec_off = MAYBE_SWAP (vec_off);
16848
16849 obstack_grow (output, &str_off, sizeof (str_off));
16850 obstack_grow (output, &vec_off, sizeof (vec_off));
16851 }
16852
16853 htab_delete (str_table);
3876f04e 16854 htab_delete (symbol_hash_table);
9291a0cd
TT
16855}
16856
0a5429f6
DE
16857/* Struct to map psymtab to CU index in the index file. */
16858struct psymtab_cu_index_map
16859{
16860 struct partial_symtab *psymtab;
16861 unsigned int cu_index;
16862};
16863
16864static hashval_t
16865hash_psymtab_cu_index (const void *item)
16866{
16867 const struct psymtab_cu_index_map *map = item;
16868
16869 return htab_hash_pointer (map->psymtab);
16870}
16871
16872static int
16873eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16874{
16875 const struct psymtab_cu_index_map *lhs = item_lhs;
16876 const struct psymtab_cu_index_map *rhs = item_rhs;
16877
16878 return lhs->psymtab == rhs->psymtab;
16879}
16880
16881/* Helper struct for building the address table. */
16882struct addrmap_index_data
16883{
16884 struct objfile *objfile;
16885 struct obstack *addr_obstack;
16886 htab_t cu_index_htab;
16887
16888 /* Non-zero if the previous_* fields are valid.
16889 We can't write an entry until we see the next entry (since it is only then
16890 that we know the end of the entry). */
16891 int previous_valid;
16892 /* Index of the CU in the table of all CUs in the index file. */
16893 unsigned int previous_cu_index;
0963b4bd 16894 /* Start address of the CU. */
0a5429f6
DE
16895 CORE_ADDR previous_cu_start;
16896};
16897
16898/* Write an address entry to OBSTACK. */
b89be57b 16899
9291a0cd 16900static void
0a5429f6
DE
16901add_address_entry (struct objfile *objfile, struct obstack *obstack,
16902 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16903{
0a5429f6 16904 offset_type cu_index_to_write;
9291a0cd
TT
16905 char addr[8];
16906 CORE_ADDR baseaddr;
16907
16908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16909
0a5429f6
DE
16910 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16911 obstack_grow (obstack, addr, 8);
16912 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16913 obstack_grow (obstack, addr, 8);
16914 cu_index_to_write = MAYBE_SWAP (cu_index);
16915 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16916}
16917
16918/* Worker function for traversing an addrmap to build the address table. */
16919
16920static int
16921add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16922{
16923 struct addrmap_index_data *data = datap;
16924 struct partial_symtab *pst = obj;
16925 offset_type cu_index;
16926 void **slot;
16927
16928 if (data->previous_valid)
16929 add_address_entry (data->objfile, data->addr_obstack,
16930 data->previous_cu_start, start_addr,
16931 data->previous_cu_index);
16932
16933 data->previous_cu_start = start_addr;
16934 if (pst != NULL)
16935 {
16936 struct psymtab_cu_index_map find_map, *map;
16937 find_map.psymtab = pst;
16938 map = htab_find (data->cu_index_htab, &find_map);
16939 gdb_assert (map != NULL);
16940 data->previous_cu_index = map->cu_index;
16941 data->previous_valid = 1;
16942 }
16943 else
16944 data->previous_valid = 0;
16945
16946 return 0;
16947}
16948
16949/* Write OBJFILE's address map to OBSTACK.
16950 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16951 in the index file. */
16952
16953static void
16954write_address_map (struct objfile *objfile, struct obstack *obstack,
16955 htab_t cu_index_htab)
16956{
16957 struct addrmap_index_data addrmap_index_data;
16958
16959 /* When writing the address table, we have to cope with the fact that
16960 the addrmap iterator only provides the start of a region; we have to
16961 wait until the next invocation to get the start of the next region. */
16962
16963 addrmap_index_data.objfile = objfile;
16964 addrmap_index_data.addr_obstack = obstack;
16965 addrmap_index_data.cu_index_htab = cu_index_htab;
16966 addrmap_index_data.previous_valid = 0;
16967
16968 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16969 &addrmap_index_data);
16970
16971 /* It's highly unlikely the last entry (end address = 0xff...ff)
16972 is valid, but we should still handle it.
16973 The end address is recorded as the start of the next region, but that
16974 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16975 anyway. */
16976 if (addrmap_index_data.previous_valid)
16977 add_address_entry (objfile, obstack,
16978 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16979 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16980}
16981
16982/* Add a list of partial symbols to SYMTAB. */
b89be57b 16983
9291a0cd
TT
16984static void
16985write_psymbols (struct mapped_symtab *symtab,
987d643c 16986 htab_t psyms_seen,
9291a0cd
TT
16987 struct partial_symbol **psymp,
16988 int count,
987d643c
TT
16989 offset_type cu_index,
16990 int is_static)
9291a0cd
TT
16991{
16992 for (; count-- > 0; ++psymp)
16993 {
987d643c
TT
16994 void **slot, *lookup;
16995
9291a0cd
TT
16996 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16997 error (_("Ada is not currently supported by the index"));
987d643c
TT
16998
16999 /* We only want to add a given psymbol once. However, we also
17000 want to account for whether it is global or static. So, we
17001 may add it twice, using slightly different values. */
17002 if (is_static)
17003 {
17004 uintptr_t val = 1 | (uintptr_t) *psymp;
17005
17006 lookup = (void *) val;
17007 }
17008 else
17009 lookup = *psymp;
17010
17011 /* Only add a given psymbol once. */
17012 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17013 if (!*slot)
17014 {
17015 *slot = lookup;
bb2f58dc 17016 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17017 }
9291a0cd
TT
17018 }
17019}
17020
17021/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17022 exception if there is an error. */
b89be57b 17023
9291a0cd
TT
17024static void
17025write_obstack (FILE *file, struct obstack *obstack)
17026{
17027 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17028 file)
17029 != obstack_object_size (obstack))
17030 error (_("couldn't data write to file"));
17031}
17032
17033/* Unlink a file if the argument is not NULL. */
b89be57b 17034
9291a0cd
TT
17035static void
17036unlink_if_set (void *p)
17037{
17038 char **filename = p;
17039 if (*filename)
17040 unlink (*filename);
17041}
17042
1fd400ff
TT
17043/* A helper struct used when iterating over debug_types. */
17044struct signatured_type_index_data
17045{
17046 struct objfile *objfile;
17047 struct mapped_symtab *symtab;
17048 struct obstack *types_list;
987d643c 17049 htab_t psyms_seen;
1fd400ff
TT
17050 int cu_index;
17051};
17052
17053/* A helper function that writes a single signatured_type to an
17054 obstack. */
b89be57b 17055
1fd400ff
TT
17056static int
17057write_one_signatured_type (void **slot, void *d)
17058{
17059 struct signatured_type_index_data *info = d;
17060 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17061 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17062 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17063 gdb_byte val[8];
17064
17065 write_psymbols (info->symtab,
987d643c 17066 info->psyms_seen,
3e43a32a
MS
17067 info->objfile->global_psymbols.list
17068 + psymtab->globals_offset,
987d643c
TT
17069 psymtab->n_global_syms, info->cu_index,
17070 0);
1fd400ff 17071 write_psymbols (info->symtab,
987d643c 17072 info->psyms_seen,
3e43a32a
MS
17073 info->objfile->static_psymbols.list
17074 + psymtab->statics_offset,
987d643c
TT
17075 psymtab->n_static_syms, info->cu_index,
17076 1);
1fd400ff 17077
b64f50a1
JK
17078 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17079 entry->per_cu.offset.sect_off);
1fd400ff 17080 obstack_grow (info->types_list, val, 8);
b64f50a1 17081 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
1fd400ff
TT
17082 obstack_grow (info->types_list, val, 8);
17083 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17084 obstack_grow (info->types_list, val, 8);
17085
17086 ++info->cu_index;
17087
17088 return 1;
17089}
17090
9291a0cd 17091/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17092
9291a0cd
TT
17093static void
17094write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17095{
17096 struct cleanup *cleanup;
17097 char *filename, *cleanup_filename;
1fd400ff
TT
17098 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17099 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17100 int i;
17101 FILE *out_file;
17102 struct mapped_symtab *symtab;
17103 offset_type val, size_of_contents, total_len;
17104 struct stat st;
17105 char buf[8];
987d643c 17106 htab_t psyms_seen;
0a5429f6
DE
17107 htab_t cu_index_htab;
17108 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17109
b4f2f049 17110 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17111 return;
b4f2f049 17112
9291a0cd
TT
17113 if (dwarf2_per_objfile->using_index)
17114 error (_("Cannot use an index to create the index"));
17115
8b70b953
TT
17116 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17117 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17118
9291a0cd 17119 if (stat (objfile->name, &st) < 0)
7e17e088 17120 perror_with_name (objfile->name);
9291a0cd
TT
17121
17122 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17123 INDEX_SUFFIX, (char *) NULL);
17124 cleanup = make_cleanup (xfree, filename);
17125
17126 out_file = fopen (filename, "wb");
17127 if (!out_file)
17128 error (_("Can't open `%s' for writing"), filename);
17129
17130 cleanup_filename = filename;
17131 make_cleanup (unlink_if_set, &cleanup_filename);
17132
17133 symtab = create_mapped_symtab ();
17134 make_cleanup (cleanup_mapped_symtab, symtab);
17135
17136 obstack_init (&addr_obstack);
17137 make_cleanup_obstack_free (&addr_obstack);
17138
17139 obstack_init (&cu_list);
17140 make_cleanup_obstack_free (&cu_list);
17141
1fd400ff
TT
17142 obstack_init (&types_cu_list);
17143 make_cleanup_obstack_free (&types_cu_list);
17144
987d643c
TT
17145 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17146 NULL, xcalloc, xfree);
96408a79 17147 make_cleanup_htab_delete (psyms_seen);
987d643c 17148
0a5429f6
DE
17149 /* While we're scanning CU's create a table that maps a psymtab pointer
17150 (which is what addrmap records) to its index (which is what is recorded
17151 in the index file). This will later be needed to write the address
17152 table. */
17153 cu_index_htab = htab_create_alloc (100,
17154 hash_psymtab_cu_index,
17155 eq_psymtab_cu_index,
17156 NULL, xcalloc, xfree);
96408a79 17157 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17158 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17159 xmalloc (sizeof (struct psymtab_cu_index_map)
17160 * dwarf2_per_objfile->n_comp_units);
17161 make_cleanup (xfree, psymtab_cu_index_map);
17162
17163 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17164 work here. Also, the debug_types entries do not appear in
17165 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17166 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17167 {
3e43a32a
MS
17168 struct dwarf2_per_cu_data *per_cu
17169 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17170 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17171 gdb_byte val[8];
0a5429f6
DE
17172 struct psymtab_cu_index_map *map;
17173 void **slot;
9291a0cd
TT
17174
17175 write_psymbols (symtab,
987d643c 17176 psyms_seen,
9291a0cd 17177 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17178 psymtab->n_global_syms, i,
17179 0);
9291a0cd 17180 write_psymbols (symtab,
987d643c 17181 psyms_seen,
9291a0cd 17182 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17183 psymtab->n_static_syms, i,
17184 1);
9291a0cd 17185
0a5429f6
DE
17186 map = &psymtab_cu_index_map[i];
17187 map->psymtab = psymtab;
17188 map->cu_index = i;
17189 slot = htab_find_slot (cu_index_htab, map, INSERT);
17190 gdb_assert (slot != NULL);
17191 gdb_assert (*slot == NULL);
17192 *slot = map;
9291a0cd 17193
b64f50a1
JK
17194 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17195 per_cu->offset.sect_off);
9291a0cd 17196 obstack_grow (&cu_list, val, 8);
e254ef6a 17197 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17198 obstack_grow (&cu_list, val, 8);
17199 }
17200
0a5429f6
DE
17201 /* Dump the address map. */
17202 write_address_map (objfile, &addr_obstack, cu_index_htab);
17203
1fd400ff
TT
17204 /* Write out the .debug_type entries, if any. */
17205 if (dwarf2_per_objfile->signatured_types)
17206 {
17207 struct signatured_type_index_data sig_data;
17208
17209 sig_data.objfile = objfile;
17210 sig_data.symtab = symtab;
17211 sig_data.types_list = &types_cu_list;
987d643c 17212 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17213 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17214 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17215 write_one_signatured_type, &sig_data);
17216 }
17217
9291a0cd
TT
17218 obstack_init (&constant_pool);
17219 make_cleanup_obstack_free (&constant_pool);
17220 obstack_init (&symtab_obstack);
17221 make_cleanup_obstack_free (&symtab_obstack);
17222 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17223
17224 obstack_init (&contents);
17225 make_cleanup_obstack_free (&contents);
1fd400ff 17226 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17227 total_len = size_of_contents;
17228
17229 /* The version number. */
481860b3 17230 val = MAYBE_SWAP (6);
9291a0cd
TT
17231 obstack_grow (&contents, &val, sizeof (val));
17232
17233 /* The offset of the CU list from the start of the file. */
17234 val = MAYBE_SWAP (total_len);
17235 obstack_grow (&contents, &val, sizeof (val));
17236 total_len += obstack_object_size (&cu_list);
17237
1fd400ff
TT
17238 /* The offset of the types CU list from the start of the file. */
17239 val = MAYBE_SWAP (total_len);
17240 obstack_grow (&contents, &val, sizeof (val));
17241 total_len += obstack_object_size (&types_cu_list);
17242
9291a0cd
TT
17243 /* The offset of the address table from the start of the file. */
17244 val = MAYBE_SWAP (total_len);
17245 obstack_grow (&contents, &val, sizeof (val));
17246 total_len += obstack_object_size (&addr_obstack);
17247
17248 /* The offset of the symbol table from the start of the file. */
17249 val = MAYBE_SWAP (total_len);
17250 obstack_grow (&contents, &val, sizeof (val));
17251 total_len += obstack_object_size (&symtab_obstack);
17252
17253 /* The offset of the constant pool from the start of the file. */
17254 val = MAYBE_SWAP (total_len);
17255 obstack_grow (&contents, &val, sizeof (val));
17256 total_len += obstack_object_size (&constant_pool);
17257
17258 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17259
17260 write_obstack (out_file, &contents);
17261 write_obstack (out_file, &cu_list);
1fd400ff 17262 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17263 write_obstack (out_file, &addr_obstack);
17264 write_obstack (out_file, &symtab_obstack);
17265 write_obstack (out_file, &constant_pool);
17266
17267 fclose (out_file);
17268
17269 /* We want to keep the file, so we set cleanup_filename to NULL
17270 here. See unlink_if_set. */
17271 cleanup_filename = NULL;
17272
17273 do_cleanups (cleanup);
17274}
17275
90476074
TT
17276/* Implementation of the `save gdb-index' command.
17277
17278 Note that the file format used by this command is documented in the
17279 GDB manual. Any changes here must be documented there. */
11570e71 17280
9291a0cd
TT
17281static void
17282save_gdb_index_command (char *arg, int from_tty)
17283{
17284 struct objfile *objfile;
17285
17286 if (!arg || !*arg)
96d19272 17287 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17288
17289 ALL_OBJFILES (objfile)
17290 {
17291 struct stat st;
17292
17293 /* If the objfile does not correspond to an actual file, skip it. */
17294 if (stat (objfile->name, &st) < 0)
17295 continue;
17296
17297 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17298 if (dwarf2_per_objfile)
17299 {
17300 volatile struct gdb_exception except;
17301
17302 TRY_CATCH (except, RETURN_MASK_ERROR)
17303 {
17304 write_psymtabs_to_index (objfile, arg);
17305 }
17306 if (except.reason < 0)
17307 exception_fprintf (gdb_stderr, except,
17308 _("Error while writing index for `%s': "),
17309 objfile->name);
17310 }
17311 }
dce234bc
PP
17312}
17313
9291a0cd
TT
17314\f
17315
9eae7c52
TT
17316int dwarf2_always_disassemble;
17317
17318static void
17319show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17320 struct cmd_list_element *c, const char *value)
17321{
3e43a32a
MS
17322 fprintf_filtered (file,
17323 _("Whether to always disassemble "
17324 "DWARF expressions is %s.\n"),
9eae7c52
TT
17325 value);
17326}
17327
900e11f9
JK
17328static void
17329show_check_physname (struct ui_file *file, int from_tty,
17330 struct cmd_list_element *c, const char *value)
17331{
17332 fprintf_filtered (file,
17333 _("Whether to check \"physname\" is %s.\n"),
17334 value);
17335}
17336
6502dd73
DJ
17337void _initialize_dwarf2_read (void);
17338
17339void
17340_initialize_dwarf2_read (void)
17341{
96d19272
JK
17342 struct cmd_list_element *c;
17343
dce234bc 17344 dwarf2_objfile_data_key
c1bd65d0 17345 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17346
1bedd215
AC
17347 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17348Set DWARF 2 specific variables.\n\
17349Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17350 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17351 0/*allow-unknown*/, &maintenance_set_cmdlist);
17352
1bedd215
AC
17353 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17354Show DWARF 2 specific variables\n\
17355Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17356 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17357 0/*allow-unknown*/, &maintenance_show_cmdlist);
17358
17359 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17360 &dwarf2_max_cache_age, _("\
17361Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17362Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17363A higher limit means that cached compilation units will be stored\n\
17364in memory longer, and more total memory will be used. Zero disables\n\
17365caching, which can slow down startup."),
2c5b56ce 17366 NULL,
920d2a44 17367 show_dwarf2_max_cache_age,
2c5b56ce 17368 &set_dwarf2_cmdlist,
ae038cb0 17369 &show_dwarf2_cmdlist);
d97bc12b 17370
9eae7c52
TT
17371 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17372 &dwarf2_always_disassemble, _("\
17373Set whether `info address' always disassembles DWARF expressions."), _("\
17374Show whether `info address' always disassembles DWARF expressions."), _("\
17375When enabled, DWARF expressions are always printed in an assembly-like\n\
17376syntax. When disabled, expressions will be printed in a more\n\
17377conversational style, when possible."),
17378 NULL,
17379 show_dwarf2_always_disassemble,
17380 &set_dwarf2_cmdlist,
17381 &show_dwarf2_cmdlist);
17382
d97bc12b
DE
17383 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17384Set debugging of the dwarf2 DIE reader."), _("\
17385Show debugging of the dwarf2 DIE reader."), _("\
17386When enabled (non-zero), DIEs are dumped after they are read in.\n\
17387The value is the maximum depth to print."),
17388 NULL,
17389 NULL,
17390 &setdebuglist, &showdebuglist);
9291a0cd 17391
900e11f9
JK
17392 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17393Set cross-checking of \"physname\" code against demangler."), _("\
17394Show cross-checking of \"physname\" code against demangler."), _("\
17395When enabled, GDB's internal \"physname\" code is checked against\n\
17396the demangler."),
17397 NULL, show_check_physname,
17398 &setdebuglist, &showdebuglist);
17399
96d19272 17400 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17401 _("\
fc1a9d6e 17402Save a gdb-index file.\n\
11570e71 17403Usage: save gdb-index DIRECTORY"),
96d19272
JK
17404 &save_cmdlist);
17405 set_cmd_completer (c, filename_completer);
6502dd73 17406}
This page took 2.497988 seconds and 4 git commands to generate.