* gdb.threads/linux-dp.exp: Unset 'seen' before 'array set'.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5
PA
61#include "c-lang.h"
62#include "valprint.h"
60d5a603 63#include <ctype.h>
4c2df51b 64
c906108c
SS
65#include <fcntl.h>
66#include "gdb_string.h"
4bdf3d34 67#include "gdb_assert.h"
c906108c 68#include <sys/types.h>
233a11ab
CS
69#ifdef HAVE_ZLIB_H
70#include <zlib.h>
71#endif
dce234bc
PP
72#ifdef HAVE_MMAP
73#include <sys/mman.h>
85d9bd0e
TT
74#ifndef MAP_FAILED
75#define MAP_FAILED ((void *) -1)
76#endif
dce234bc 77#endif
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
d97bc12b
DE
82/* When non-zero, dump DIEs after they are read in. */
83static int dwarf2_die_debug = 0;
84
900e11f9
JK
85/* When non-zero, cross-check physname against demangler. */
86static int check_physname = 0;
87
481860b3
GB
88/* When non-zero, do not reject deprecated .gdb_index sections. */
89int use_deprecated_index_sections = 0;
90
dce234bc
PP
91static int pagesize;
92
df8a16a1
DJ
93/* When set, the file that we're processing is known to have debugging
94 info for C++ namespaces. GCC 3.3.x did not produce this information,
95 but later versions do. */
96
97static int processing_has_namespace_info;
98
6502dd73
DJ
99static const struct objfile_data *dwarf2_objfile_data_key;
100
dce234bc
PP
101struct dwarf2_section_info
102{
103 asection *asection;
104 gdb_byte *buffer;
105 bfd_size_type size;
b315ab21
TG
106 /* Not NULL if the section was actually mmapped. */
107 void *map_addr;
108 /* Page aligned size of mmapped area. */
109 bfd_size_type map_len;
be391dca
TT
110 /* True if we have tried to read this section. */
111 int readin;
dce234bc
PP
112};
113
8b70b953
TT
114typedef struct dwarf2_section_info dwarf2_section_info_def;
115DEF_VEC_O (dwarf2_section_info_def);
116
9291a0cd
TT
117/* All offsets in the index are of this type. It must be
118 architecture-independent. */
119typedef uint32_t offset_type;
120
121DEF_VEC_I (offset_type);
122
123/* A description of the mapped index. The file format is described in
124 a comment by the code that writes the index. */
125struct mapped_index
126{
559a7a62
JK
127 /* Index data format version. */
128 int version;
129
9291a0cd
TT
130 /* The total length of the buffer. */
131 off_t total_size;
b11b1f88 132
9291a0cd
TT
133 /* A pointer to the address table data. */
134 const gdb_byte *address_table;
b11b1f88 135
9291a0cd
TT
136 /* Size of the address table data in bytes. */
137 offset_type address_table_size;
b11b1f88 138
3876f04e
DE
139 /* The symbol table, implemented as a hash table. */
140 const offset_type *symbol_table;
b11b1f88 141
9291a0cd 142 /* Size in slots, each slot is 2 offset_types. */
3876f04e 143 offset_type symbol_table_slots;
b11b1f88 144
9291a0cd
TT
145 /* A pointer to the constant pool. */
146 const char *constant_pool;
147};
148
9cdd5dbd
DE
149/* Collection of data recorded per objfile.
150 This hangs off of dwarf2_objfile_data_key. */
151
6502dd73
DJ
152struct dwarf2_per_objfile
153{
dce234bc
PP
154 struct dwarf2_section_info info;
155 struct dwarf2_section_info abbrev;
156 struct dwarf2_section_info line;
dce234bc
PP
157 struct dwarf2_section_info loc;
158 struct dwarf2_section_info macinfo;
cf2c3c16 159 struct dwarf2_section_info macro;
dce234bc
PP
160 struct dwarf2_section_info str;
161 struct dwarf2_section_info ranges;
162 struct dwarf2_section_info frame;
163 struct dwarf2_section_info eh_frame;
9291a0cd 164 struct dwarf2_section_info gdb_index;
ae038cb0 165
8b70b953
TT
166 VEC (dwarf2_section_info_def) *types;
167
be391dca
TT
168 /* Back link. */
169 struct objfile *objfile;
170
d467dd73 171 /* Table of all the compilation units. This is used to locate
10b3939b 172 the target compilation unit of a particular reference. */
ae038cb0
DJ
173 struct dwarf2_per_cu_data **all_comp_units;
174
175 /* The number of compilation units in ALL_COMP_UNITS. */
176 int n_comp_units;
177
1fd400ff 178 /* The number of .debug_types-related CUs. */
d467dd73 179 int n_type_units;
1fd400ff 180
d467dd73
DE
181 /* The .debug_types-related CUs (TUs). */
182 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 183
ae038cb0
DJ
184 /* A chain of compilation units that are currently read in, so that
185 they can be freed later. */
186 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 187
348e048f
DE
188 /* A table mapping .debug_types signatures to its signatured_type entry.
189 This is NULL if the .debug_types section hasn't been read in yet. */
190 htab_t signatured_types;
191
72dca2f5
FR
192 /* A flag indicating wether this objfile has a section loaded at a
193 VMA of 0. */
194 int has_section_at_zero;
9291a0cd 195
ae2de4f8
DE
196 /* True if we are using the mapped index,
197 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
198 unsigned char using_index;
199
ae2de4f8 200 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 201 struct mapped_index *index_table;
98bfdba5 202
7b9f3c50
DE
203 /* When using index_table, this keeps track of all quick_file_names entries.
204 TUs can share line table entries with CUs or other TUs, and there can be
205 a lot more TUs than unique line tables, so we maintain a separate table
206 of all line table entries to support the sharing. */
207 htab_t quick_file_names_table;
208
98bfdba5
PA
209 /* Set during partial symbol reading, to prevent queueing of full
210 symbols. */
211 int reading_partial_symbols;
673bfd45
DE
212
213 /* Table mapping type .debug_info DIE offsets to types.
214 This is NULL if not allocated yet.
215 It (currently) makes sense to allocate debug_types_type_hash lazily.
216 To keep things simple we allocate both lazily. */
217 htab_t debug_info_type_hash;
218
b64f50a1 219 /* Table mapping type .debug_types DIE sect_offset to types.
673bfd45
DE
220 This is NULL if not allocated yet. */
221 htab_t debug_types_type_hash;
6502dd73
DJ
222};
223
224static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 225
251d32d9 226/* Default names of the debugging sections. */
c906108c 227
233a11ab
CS
228/* Note that if the debugging section has been compressed, it might
229 have a name like .zdebug_info. */
230
9cdd5dbd
DE
231static const struct dwarf2_debug_sections dwarf2_elf_names =
232{
251d32d9
TG
233 { ".debug_info", ".zdebug_info" },
234 { ".debug_abbrev", ".zdebug_abbrev" },
235 { ".debug_line", ".zdebug_line" },
236 { ".debug_loc", ".zdebug_loc" },
237 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 238 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
239 { ".debug_str", ".zdebug_str" },
240 { ".debug_ranges", ".zdebug_ranges" },
241 { ".debug_types", ".zdebug_types" },
242 { ".debug_frame", ".zdebug_frame" },
243 { ".eh_frame", NULL },
24d3216f
TT
244 { ".gdb_index", ".zgdb_index" },
245 23
251d32d9 246};
c906108c
SS
247
248/* local data types */
249
0963b4bd 250/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
251#ifndef ABBREV_HASH_SIZE
252#define ABBREV_HASH_SIZE 121
253#endif
254
107d2387
AC
255/* The data in a compilation unit header, after target2host
256 translation, looks like this. */
c906108c 257struct comp_unit_head
a738430d 258{
c764a876 259 unsigned int length;
a738430d 260 short version;
a738430d
MK
261 unsigned char addr_size;
262 unsigned char signed_addr_p;
b64f50a1 263 sect_offset abbrev_offset;
57349743 264
a738430d
MK
265 /* Size of file offsets; either 4 or 8. */
266 unsigned int offset_size;
57349743 267
a738430d
MK
268 /* Size of the length field; either 4 or 12. */
269 unsigned int initial_length_size;
57349743 270
a738430d
MK
271 /* Offset to the first byte of this compilation unit header in the
272 .debug_info section, for resolving relative reference dies. */
b64f50a1 273 sect_offset offset;
57349743 274
d00adf39
DE
275 /* Offset to first die in this cu from the start of the cu.
276 This will be the first byte following the compilation unit header. */
b64f50a1 277 cu_offset first_die_offset;
a738430d 278};
c906108c 279
3da10d80
KS
280/* Type used for delaying computation of method physnames.
281 See comments for compute_delayed_physnames. */
282struct delayed_method_info
283{
284 /* The type to which the method is attached, i.e., its parent class. */
285 struct type *type;
286
287 /* The index of the method in the type's function fieldlists. */
288 int fnfield_index;
289
290 /* The index of the method in the fieldlist. */
291 int index;
292
293 /* The name of the DIE. */
294 const char *name;
295
296 /* The DIE associated with this method. */
297 struct die_info *die;
298};
299
300typedef struct delayed_method_info delayed_method_info;
301DEF_VEC_O (delayed_method_info);
302
e7c27a73
DJ
303/* Internal state when decoding a particular compilation unit. */
304struct dwarf2_cu
305{
306 /* The objfile containing this compilation unit. */
307 struct objfile *objfile;
308
d00adf39 309 /* The header of the compilation unit. */
e7c27a73 310 struct comp_unit_head header;
e142c38c 311
d00adf39
DE
312 /* Base address of this compilation unit. */
313 CORE_ADDR base_address;
314
315 /* Non-zero if base_address has been set. */
316 int base_known;
317
e142c38c
DJ
318 /* The language we are debugging. */
319 enum language language;
320 const struct language_defn *language_defn;
321
b0f35d58
DL
322 const char *producer;
323
e142c38c
DJ
324 /* The generic symbol table building routines have separate lists for
325 file scope symbols and all all other scopes (local scopes). So
326 we need to select the right one to pass to add_symbol_to_list().
327 We do it by keeping a pointer to the correct list in list_in_scope.
328
329 FIXME: The original dwarf code just treated the file scope as the
330 first local scope, and all other local scopes as nested local
331 scopes, and worked fine. Check to see if we really need to
332 distinguish these in buildsym.c. */
333 struct pending **list_in_scope;
334
f3dd6933
DJ
335 /* DWARF abbreviation table associated with this compilation unit. */
336 struct abbrev_info **dwarf2_abbrevs;
337
338 /* Storage for the abbrev table. */
339 struct obstack abbrev_obstack;
72bf9492 340
b64f50a1
JK
341 /* Hash table holding all the loaded partial DIEs
342 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
b64f50a1
JK
361 /* A hash table of DIE cu_offset for following references with
362 die_info->offset.sect_off as hash. */
51545339 363 htab_t die_hash;
10b3939b
DJ
364
365 /* Full DIEs if read in. */
366 struct die_info *dies;
367
368 /* A set of pointers to dwarf2_per_cu_data objects for compilation
369 units referenced by this one. Only set during full symbol processing;
370 partial symbol tables do not have dependencies. */
371 htab_t dependencies;
372
cb1df416
DJ
373 /* Header data from the line table, during full symbol processing. */
374 struct line_header *line_header;
375
3da10d80
KS
376 /* A list of methods which need to have physnames computed
377 after all type information has been read. */
378 VEC (delayed_method_info) *method_list;
379
96408a79
SA
380 /* To be copied to symtab->call_site_htab. */
381 htab_t call_site_htab;
382
ae038cb0
DJ
383 /* Mark used when releasing cached dies. */
384 unsigned int mark : 1;
385
8be455d7
JK
386 /* This CU references .debug_loc. See the symtab->locations_valid field.
387 This test is imperfect as there may exist optimized debug code not using
388 any location list and still facing inlining issues if handled as
389 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 390 unsigned int has_loclist : 1;
ba919b58
TT
391
392 /* These cache the results of producer_is_gxx_lt_4_6.
393 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
394 information is cached because profiling CU expansion showed
395 excessive time spent in producer_is_gxx_lt_4_6. */
396 unsigned int checked_producer : 1;
397 unsigned int producer_is_gxx_lt_4_6 : 1;
e7c27a73
DJ
398};
399
10b3939b
DJ
400/* Persistent data held for a compilation unit, even when not
401 processing it. We put a pointer to this structure in the
28dee7f5 402 read_symtab_private field of the psymtab. */
10b3939b 403
ae038cb0
DJ
404struct dwarf2_per_cu_data
405{
348e048f 406 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 407 bytes should suffice to store the length of any compilation unit
45452591
DE
408 - if it doesn't, GDB will fall over anyway.
409 NOTE: Unlike comp_unit_head.length, this length includes
410 initial_length_size. */
b64f50a1 411 sect_offset offset;
348e048f 412 unsigned int length : 29;
ae038cb0
DJ
413
414 /* Flag indicating this compilation unit will be read in before
415 any of the current compilation units are processed. */
c764a876 416 unsigned int queued : 1;
ae038cb0 417
0d99eb77
DE
418 /* This flag will be set when reading partial DIEs if we need to load
419 absolutely all DIEs for this compilation unit, instead of just the ones
420 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
421 hash table and don't find it. */
422 unsigned int load_all_dies : 1;
423
8b70b953
TT
424 /* Non-null if this CU is from .debug_types; in which case it points
425 to the section. Otherwise it's from .debug_info. */
b0df02fd 426 struct dwarf2_section_info *debug_types_section;
348e048f 427
17ea53c3
JK
428 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
429 of the CU cache it gets reset to NULL again. */
ae038cb0 430 struct dwarf2_cu *cu;
1c379e20 431
9cdd5dbd
DE
432 /* The corresponding objfile.
433 Normally we can get the objfile from dwarf2_per_objfile.
434 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
435 struct objfile *objfile;
436
437 /* When using partial symbol tables, the 'psymtab' field is active.
438 Otherwise the 'quick' field is active. */
439 union
440 {
441 /* The partial symbol table associated with this compilation unit,
442 or NULL for partial units (which do not have an associated
443 symtab). */
444 struct partial_symtab *psymtab;
445
446 /* Data needed by the "quick" functions. */
447 struct dwarf2_per_cu_quick_data *quick;
448 } v;
ae038cb0
DJ
449};
450
348e048f
DE
451/* Entry in the signatured_types hash table. */
452
453struct signatured_type
454{
455 ULONGEST signature;
456
b64f50a1
JK
457 /* Offset in this TU of the type defined by this TU. */
458 cu_offset type_offset;
348e048f
DE
459
460 /* The CU(/TU) of this type. */
461 struct dwarf2_per_cu_data per_cu;
462};
463
0963b4bd
MS
464/* Struct used to pass misc. parameters to read_die_and_children, et
465 al. which are used for both .debug_info and .debug_types dies.
466 All parameters here are unchanging for the life of the call. This
467 struct exists to abstract away the constant parameters of die
468 reading. */
93311388
DE
469
470struct die_reader_specs
471{
472 /* The bfd of this objfile. */
473 bfd* abfd;
474
475 /* The CU of the DIE we are parsing. */
476 struct dwarf2_cu *cu;
477
478 /* Pointer to start of section buffer.
479 This is either the start of .debug_info or .debug_types. */
480 const gdb_byte *buffer;
481};
482
debd256d
JB
483/* The line number information for a compilation unit (found in the
484 .debug_line section) begins with a "statement program header",
485 which contains the following information. */
486struct line_header
487{
488 unsigned int total_length;
489 unsigned short version;
490 unsigned int header_length;
491 unsigned char minimum_instruction_length;
2dc7f7b3 492 unsigned char maximum_ops_per_instruction;
debd256d
JB
493 unsigned char default_is_stmt;
494 int line_base;
495 unsigned char line_range;
496 unsigned char opcode_base;
497
498 /* standard_opcode_lengths[i] is the number of operands for the
499 standard opcode whose value is i. This means that
500 standard_opcode_lengths[0] is unused, and the last meaningful
501 element is standard_opcode_lengths[opcode_base - 1]. */
502 unsigned char *standard_opcode_lengths;
503
504 /* The include_directories table. NOTE! These strings are not
505 allocated with xmalloc; instead, they are pointers into
506 debug_line_buffer. If you try to free them, `free' will get
507 indigestion. */
508 unsigned int num_include_dirs, include_dirs_size;
509 char **include_dirs;
510
511 /* The file_names table. NOTE! These strings are not allocated
512 with xmalloc; instead, they are pointers into debug_line_buffer.
513 Don't try to free them directly. */
514 unsigned int num_file_names, file_names_size;
515 struct file_entry
c906108c 516 {
debd256d
JB
517 char *name;
518 unsigned int dir_index;
519 unsigned int mod_time;
520 unsigned int length;
aaa75496 521 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 522 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
523 } *file_names;
524
525 /* The start and end of the statement program following this
6502dd73 526 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 527 gdb_byte *statement_program_start, *statement_program_end;
debd256d 528};
c906108c
SS
529
530/* When we construct a partial symbol table entry we only
0963b4bd 531 need this much information. */
c906108c
SS
532struct partial_die_info
533 {
72bf9492 534 /* Offset of this DIE. */
b64f50a1 535 sect_offset offset;
72bf9492
DJ
536
537 /* DWARF-2 tag for this DIE. */
538 ENUM_BITFIELD(dwarf_tag) tag : 16;
539
72bf9492
DJ
540 /* Assorted flags describing the data found in this DIE. */
541 unsigned int has_children : 1;
542 unsigned int is_external : 1;
543 unsigned int is_declaration : 1;
544 unsigned int has_type : 1;
545 unsigned int has_specification : 1;
546 unsigned int has_pc_info : 1;
481860b3 547 unsigned int may_be_inlined : 1;
72bf9492
DJ
548
549 /* Flag set if the SCOPE field of this structure has been
550 computed. */
551 unsigned int scope_set : 1;
552
fa4028e9
JB
553 /* Flag set if the DIE has a byte_size attribute. */
554 unsigned int has_byte_size : 1;
555
98bfdba5
PA
556 /* Flag set if any of the DIE's children are template arguments. */
557 unsigned int has_template_arguments : 1;
558
abc72ce4
DE
559 /* Flag set if fixup_partial_die has been called on this die. */
560 unsigned int fixup_called : 1;
561
72bf9492 562 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 563 sometimes a default name for unnamed DIEs. */
c906108c 564 char *name;
72bf9492 565
abc72ce4
DE
566 /* The linkage name, if present. */
567 const char *linkage_name;
568
72bf9492
DJ
569 /* The scope to prepend to our children. This is generally
570 allocated on the comp_unit_obstack, so will disappear
571 when this compilation unit leaves the cache. */
572 char *scope;
573
574 /* The location description associated with this DIE, if any. */
575 struct dwarf_block *locdesc;
576
577 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
578 CORE_ADDR lowpc;
579 CORE_ADDR highpc;
72bf9492 580
93311388 581 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 582 DW_AT_sibling, if any. */
abc72ce4
DE
583 /* NOTE: This member isn't strictly necessary, read_partial_die could
584 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 585 gdb_byte *sibling;
72bf9492
DJ
586
587 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
588 DW_AT_specification (or DW_AT_abstract_origin or
589 DW_AT_extension). */
b64f50a1 590 sect_offset spec_offset;
72bf9492
DJ
591
592 /* Pointers to this DIE's parent, first child, and next sibling,
593 if any. */
594 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
595 };
596
0963b4bd 597/* This data structure holds the information of an abbrev. */
c906108c
SS
598struct abbrev_info
599 {
600 unsigned int number; /* number identifying abbrev */
601 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
602 unsigned short has_children; /* boolean */
603 unsigned short num_attrs; /* number of attributes */
c906108c
SS
604 struct attr_abbrev *attrs; /* an array of attribute descriptions */
605 struct abbrev_info *next; /* next in chain */
606 };
607
608struct attr_abbrev
609 {
9d25dd43
DE
610 ENUM_BITFIELD(dwarf_attribute) name : 16;
611 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
612 };
613
0963b4bd 614/* Attributes have a name and a value. */
b60c80d6
DJ
615struct attribute
616 {
9d25dd43 617 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
618 ENUM_BITFIELD(dwarf_form) form : 15;
619
620 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
621 field should be in u.str (existing only for DW_STRING) but it is kept
622 here for better struct attribute alignment. */
623 unsigned int string_is_canonical : 1;
624
b60c80d6
DJ
625 union
626 {
627 char *str;
628 struct dwarf_block *blk;
43bbcdc2
PH
629 ULONGEST unsnd;
630 LONGEST snd;
b60c80d6 631 CORE_ADDR addr;
348e048f 632 struct signatured_type *signatured_type;
b60c80d6
DJ
633 }
634 u;
635 };
636
0963b4bd 637/* This data structure holds a complete die structure. */
c906108c
SS
638struct die_info
639 {
76815b17
DE
640 /* DWARF-2 tag for this DIE. */
641 ENUM_BITFIELD(dwarf_tag) tag : 16;
642
643 /* Number of attributes */
98bfdba5
PA
644 unsigned char num_attrs;
645
646 /* True if we're presently building the full type name for the
647 type derived from this DIE. */
648 unsigned char building_fullname : 1;
76815b17
DE
649
650 /* Abbrev number */
651 unsigned int abbrev;
652
93311388 653 /* Offset in .debug_info or .debug_types section. */
b64f50a1 654 sect_offset offset;
78ba4af6
JB
655
656 /* The dies in a compilation unit form an n-ary tree. PARENT
657 points to this die's parent; CHILD points to the first child of
658 this node; and all the children of a given node are chained
4950bc1c 659 together via their SIBLING fields. */
639d11d3
DC
660 struct die_info *child; /* Its first child, if any. */
661 struct die_info *sibling; /* Its next sibling, if any. */
662 struct die_info *parent; /* Its parent, if any. */
c906108c 663
b60c80d6
DJ
664 /* An array of attributes, with NUM_ATTRS elements. There may be
665 zero, but it's not common and zero-sized arrays are not
666 sufficiently portable C. */
667 struct attribute attrs[1];
c906108c
SS
668 };
669
0963b4bd 670/* Get at parts of an attribute structure. */
c906108c
SS
671
672#define DW_STRING(attr) ((attr)->u.str)
8285870a 673#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
674#define DW_UNSND(attr) ((attr)->u.unsnd)
675#define DW_BLOCK(attr) ((attr)->u.blk)
676#define DW_SND(attr) ((attr)->u.snd)
677#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 678#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 679
0963b4bd 680/* Blocks are a bunch of untyped bytes. */
c906108c
SS
681struct dwarf_block
682 {
683 unsigned int size;
1d6edc3c
JK
684
685 /* Valid only if SIZE is not zero. */
fe1b8b76 686 gdb_byte *data;
c906108c
SS
687 };
688
c906108c
SS
689#ifndef ATTR_ALLOC_CHUNK
690#define ATTR_ALLOC_CHUNK 4
691#endif
692
c906108c
SS
693/* Allocate fields for structs, unions and enums in this size. */
694#ifndef DW_FIELD_ALLOC_CHUNK
695#define DW_FIELD_ALLOC_CHUNK 4
696#endif
697
c906108c
SS
698/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
699 but this would require a corresponding change in unpack_field_as_long
700 and friends. */
701static int bits_per_byte = 8;
702
703/* The routines that read and process dies for a C struct or C++ class
704 pass lists of data member fields and lists of member function fields
705 in an instance of a field_info structure, as defined below. */
706struct field_info
c5aa993b 707 {
0963b4bd 708 /* List of data member and baseclasses fields. */
c5aa993b
JM
709 struct nextfield
710 {
711 struct nextfield *next;
712 int accessibility;
713 int virtuality;
714 struct field field;
715 }
7d0ccb61 716 *fields, *baseclasses;
c906108c 717
7d0ccb61 718 /* Number of fields (including baseclasses). */
c5aa993b 719 int nfields;
c906108c 720
c5aa993b
JM
721 /* Number of baseclasses. */
722 int nbaseclasses;
c906108c 723
c5aa993b
JM
724 /* Set if the accesibility of one of the fields is not public. */
725 int non_public_fields;
c906108c 726
c5aa993b
JM
727 /* Member function fields array, entries are allocated in the order they
728 are encountered in the object file. */
729 struct nextfnfield
730 {
731 struct nextfnfield *next;
732 struct fn_field fnfield;
733 }
734 *fnfields;
c906108c 735
c5aa993b
JM
736 /* Member function fieldlist array, contains name of possibly overloaded
737 member function, number of overloaded member functions and a pointer
738 to the head of the member function field chain. */
739 struct fnfieldlist
740 {
741 char *name;
742 int length;
743 struct nextfnfield *head;
744 }
745 *fnfieldlists;
c906108c 746
c5aa993b
JM
747 /* Number of entries in the fnfieldlists array. */
748 int nfnfields;
98751a41
JK
749
750 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
751 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
752 struct typedef_field_list
753 {
754 struct typedef_field field;
755 struct typedef_field_list *next;
756 }
757 *typedef_field_list;
758 unsigned typedef_field_list_count;
c5aa993b 759 };
c906108c 760
10b3939b
DJ
761/* One item on the queue of compilation units to read in full symbols
762 for. */
763struct dwarf2_queue_item
764{
765 struct dwarf2_per_cu_data *per_cu;
766 struct dwarf2_queue_item *next;
767};
768
769/* The current queue. */
770static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
771
ae038cb0
DJ
772/* Loaded secondary compilation units are kept in memory until they
773 have not been referenced for the processing of this many
774 compilation units. Set this to zero to disable caching. Cache
775 sizes of up to at least twenty will improve startup time for
776 typical inter-CU-reference binaries, at an obvious memory cost. */
777static int dwarf2_max_cache_age = 5;
920d2a44
AC
778static void
779show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
780 struct cmd_list_element *c, const char *value)
781{
3e43a32a
MS
782 fprintf_filtered (file, _("The upper bound on the age of cached "
783 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
784 value);
785}
786
ae038cb0 787
0963b4bd 788/* Various complaints about symbol reading that don't abort the process. */
c906108c 789
4d3c2250
KB
790static void
791dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 792{
4d3c2250 793 complaint (&symfile_complaints,
e2e0b3e5 794 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
795}
796
25e43795
DJ
797static void
798dwarf2_debug_line_missing_file_complaint (void)
799{
800 complaint (&symfile_complaints,
801 _(".debug_line section has line data without a file"));
802}
803
59205f5a
JB
804static void
805dwarf2_debug_line_missing_end_sequence_complaint (void)
806{
807 complaint (&symfile_complaints,
3e43a32a
MS
808 _(".debug_line section has line "
809 "program sequence without an end"));
59205f5a
JB
810}
811
4d3c2250
KB
812static void
813dwarf2_complex_location_expr_complaint (void)
2e276125 814{
e2e0b3e5 815 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
816}
817
4d3c2250
KB
818static void
819dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
820 int arg3)
2e276125 821{
4d3c2250 822 complaint (&symfile_complaints,
3e43a32a
MS
823 _("const value length mismatch for '%s', got %d, expected %d"),
824 arg1, arg2, arg3);
4d3c2250
KB
825}
826
827static void
cf2c3c16 828dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
cf2c3c16
TT
831 _("macro info runs off end of `%s' section"),
832 section->asection->name);
4d3c2250
KB
833}
834
835static void
836dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 837{
4d3c2250 838 complaint (&symfile_complaints,
3e43a32a
MS
839 _("macro debug info contains a "
840 "malformed macro definition:\n`%s'"),
4d3c2250
KB
841 arg1);
842}
843
844static void
845dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 846{
4d3c2250 847 complaint (&symfile_complaints,
3e43a32a
MS
848 _("invalid attribute class or form for '%s' in '%s'"),
849 arg1, arg2);
4d3c2250 850}
c906108c 851
c906108c
SS
852/* local function prototypes */
853
4efb68b1 854static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 855
aaa75496
JB
856static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
857 struct objfile *);
858
918dd910
JK
859static void dwarf2_find_base_address (struct die_info *die,
860 struct dwarf2_cu *cu);
861
c67a9c90 862static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 863
72bf9492
DJ
864static void scan_partial_symbols (struct partial_die_info *,
865 CORE_ADDR *, CORE_ADDR *,
5734ee8b 866 int, struct dwarf2_cu *);
c906108c 867
72bf9492
DJ
868static void add_partial_symbol (struct partial_die_info *,
869 struct dwarf2_cu *);
63d06c5c 870
72bf9492
DJ
871static void add_partial_namespace (struct partial_die_info *pdi,
872 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 873 int need_pc, struct dwarf2_cu *cu);
63d06c5c 874
5d7cb8df
JK
875static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
876 CORE_ADDR *highpc, int need_pc,
877 struct dwarf2_cu *cu);
878
72bf9492
DJ
879static void add_partial_enumeration (struct partial_die_info *enum_pdi,
880 struct dwarf2_cu *cu);
91c24f0a 881
bc30ff58
JB
882static void add_partial_subprogram (struct partial_die_info *pdi,
883 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 884 int need_pc, struct dwarf2_cu *cu);
bc30ff58 885
fe1b8b76 886static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
887 gdb_byte *buffer, gdb_byte *info_ptr,
888 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 889
a14ed312 890static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 891
a14ed312 892static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 893
e5fe5e75 894static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 895
f3dd6933 896static void dwarf2_free_abbrev_table (void *);
c906108c 897
6caca83c
CC
898static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
899
fe1b8b76 900static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 901 struct dwarf2_cu *);
72bf9492 902
57349743 903static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 904 struct dwarf2_cu *);
c906108c 905
93311388
DE
906static struct partial_die_info *load_partial_dies (bfd *,
907 gdb_byte *, gdb_byte *,
908 int, struct dwarf2_cu *);
72bf9492 909
fe1b8b76 910static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
911 struct abbrev_info *abbrev,
912 unsigned int, bfd *,
913 gdb_byte *, gdb_byte *,
914 struct dwarf2_cu *);
c906108c 915
b64f50a1 916static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 917 struct dwarf2_cu *);
72bf9492
DJ
918
919static void fixup_partial_die (struct partial_die_info *,
920 struct dwarf2_cu *);
921
fe1b8b76
JB
922static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 924
fe1b8b76
JB
925static gdb_byte *read_attribute_value (struct attribute *, unsigned,
926 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 927
fe1b8b76 928static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 929
fe1b8b76 930static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 933
fe1b8b76 934static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 935
93311388 936static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 937
fe1b8b76 938static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 939 unsigned int *);
c906108c 940
c764a876
DE
941static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
942
943static LONGEST read_checked_initial_length_and_offset
944 (bfd *, gdb_byte *, const struct comp_unit_head *,
945 unsigned int *, unsigned int *);
613e1657 946
fe1b8b76 947static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
948 unsigned int *);
949
950static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 951
fe1b8b76 952static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 953
9b1c24c8 954static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 955
fe1b8b76
JB
956static char *read_indirect_string (bfd *, gdb_byte *,
957 const struct comp_unit_head *,
958 unsigned int *);
4bdf3d34 959
12df843f 960static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 961
12df843f 962static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 963
fe1b8b76 964static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 965
e142c38c 966static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 967
e142c38c
DJ
968static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
969 struct dwarf2_cu *);
c906108c 970
348e048f
DE
971static struct attribute *dwarf2_attr_no_follow (struct die_info *,
972 unsigned int,
973 struct dwarf2_cu *);
974
05cf31d1
JB
975static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
976 struct dwarf2_cu *cu);
977
e142c38c 978static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 979
e142c38c 980static struct die_info *die_specification (struct die_info *die,
f2f0e013 981 struct dwarf2_cu **);
63d06c5c 982
debd256d
JB
983static void free_line_header (struct line_header *lh);
984
aaa75496
JB
985static void add_file_name (struct line_header *, char *, unsigned int,
986 unsigned int, unsigned int);
987
debd256d
JB
988static struct line_header *(dwarf_decode_line_header
989 (unsigned int offset,
e7c27a73 990 bfd *abfd, struct dwarf2_cu *cu));
debd256d 991
f3f5162e
DE
992static void dwarf_decode_lines (struct line_header *, const char *,
993 struct dwarf2_cu *, struct partial_symtab *,
994 int);
c906108c 995
72b9f47f 996static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 997
a14ed312 998static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 999 struct dwarf2_cu *);
c906108c 1000
34eaf542
TT
1001static struct symbol *new_symbol_full (struct die_info *, struct type *,
1002 struct dwarf2_cu *, struct symbol *);
1003
a14ed312 1004static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1005 struct dwarf2_cu *);
c906108c 1006
98bfdba5
PA
1007static void dwarf2_const_value_attr (struct attribute *attr,
1008 struct type *type,
1009 const char *name,
1010 struct obstack *obstack,
12df843f 1011 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1012 gdb_byte **bytes,
1013 struct dwarf2_locexpr_baton **baton);
2df3850c 1014
e7c27a73 1015static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1016
b4ba55a1
JB
1017static int need_gnat_info (struct dwarf2_cu *);
1018
3e43a32a
MS
1019static struct type *die_descriptive_type (struct die_info *,
1020 struct dwarf2_cu *);
b4ba55a1
JB
1021
1022static void set_descriptive_type (struct type *, struct die_info *,
1023 struct dwarf2_cu *);
1024
e7c27a73
DJ
1025static struct type *die_containing_type (struct die_info *,
1026 struct dwarf2_cu *);
c906108c 1027
673bfd45
DE
1028static struct type *lookup_die_type (struct die_info *, struct attribute *,
1029 struct dwarf2_cu *);
c906108c 1030
f792889a 1031static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1032
673bfd45
DE
1033static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1034
0d5cff50 1035static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1036
6e70227d 1037static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1038 const char *suffix, int physname,
1039 struct dwarf2_cu *cu);
63d06c5c 1040
e7c27a73 1041static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1042
348e048f
DE
1043static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1044
e7c27a73 1045static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1046
e7c27a73 1047static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1048
96408a79
SA
1049static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1050
ff013f42
JK
1051static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1052 struct dwarf2_cu *, struct partial_symtab *);
1053
a14ed312 1054static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1055 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1056 struct partial_symtab *);
c906108c 1057
fae299cd
DC
1058static void get_scope_pc_bounds (struct die_info *,
1059 CORE_ADDR *, CORE_ADDR *,
1060 struct dwarf2_cu *);
1061
801e3a5b
JB
1062static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1063 CORE_ADDR, struct dwarf2_cu *);
1064
a14ed312 1065static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1066 struct dwarf2_cu *);
c906108c 1067
a14ed312 1068static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1069 struct type *, struct dwarf2_cu *);
c906108c 1070
a14ed312 1071static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1072 struct die_info *, struct type *,
e7c27a73 1073 struct dwarf2_cu *);
c906108c 1074
a14ed312 1075static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1076 struct type *,
1077 struct dwarf2_cu *);
c906108c 1078
134d01f1 1079static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1080
e7c27a73 1081static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1082
e7c27a73 1083static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1084
5d7cb8df
JK
1085static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1086
27aa8d6a
SW
1087static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1088
f55ee35c
JK
1089static struct type *read_module_type (struct die_info *die,
1090 struct dwarf2_cu *cu);
1091
38d518c9 1092static const char *namespace_name (struct die_info *die,
e142c38c 1093 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1094
134d01f1 1095static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1096
e7c27a73 1097static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1098
6e70227d 1099static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1100 struct dwarf2_cu *);
1101
93311388 1102static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1103
93311388
DE
1104static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1105 gdb_byte *info_ptr,
d97bc12b
DE
1106 gdb_byte **new_info_ptr,
1107 struct die_info *parent);
1108
93311388
DE
1109static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
fe1b8b76 1111 gdb_byte **new_info_ptr,
639d11d3
DC
1112 struct die_info *parent);
1113
93311388
DE
1114static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
fe1b8b76 1116 gdb_byte **new_info_ptr,
639d11d3
DC
1117 struct die_info *parent);
1118
93311388
DE
1119static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1120 struct die_info **, gdb_byte *,
1121 int *);
1122
e7c27a73 1123static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1124
71c25dea
TT
1125static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1126 struct obstack *);
1127
e142c38c 1128static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1129
98bfdba5
PA
1130static const char *dwarf2_full_name (char *name,
1131 struct die_info *die,
1132 struct dwarf2_cu *cu);
1133
e142c38c 1134static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1135 struct dwarf2_cu **);
9219021c 1136
a14ed312 1137static char *dwarf_tag_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_attr_name (unsigned int);
c906108c 1140
a14ed312 1141static char *dwarf_form_name (unsigned int);
c906108c 1142
a14ed312 1143static char *dwarf_bool_name (unsigned int);
c906108c 1144
a14ed312 1145static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1146
1147#if 0
a14ed312 1148static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1149#endif
1150
f9aca02d 1151static struct die_info *sibling_die (struct die_info *);
c906108c 1152
d97bc12b
DE
1153static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1154
1155static void dump_die_for_error (struct die_info *);
1156
1157static void dump_die_1 (struct ui_file *, int level, int max_level,
1158 struct die_info *);
c906108c 1159
d97bc12b 1160/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1161
51545339 1162static void store_in_ref_table (struct die_info *,
10b3939b 1163 struct dwarf2_cu *);
c906108c 1164
93311388
DE
1165static int is_ref_attr (struct attribute *);
1166
b64f50a1 1167static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1168
43bbcdc2 1169static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1170
348e048f
DE
1171static struct die_info *follow_die_ref_or_sig (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
10b3939b
DJ
1175static struct die_info *follow_die_ref (struct die_info *,
1176 struct attribute *,
f2f0e013 1177 struct dwarf2_cu **);
c906108c 1178
348e048f
DE
1179static struct die_info *follow_die_sig (struct die_info *,
1180 struct attribute *,
1181 struct dwarf2_cu **);
1182
6c83ed52
TT
1183static struct signatured_type *lookup_signatured_type_at_offset
1184 (struct objfile *objfile,
b64f50a1 1185 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1186
e5fe5e75 1187static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1188
52dc124a 1189static void read_signatured_type (struct signatured_type *);
348e048f 1190
c906108c
SS
1191/* memory allocation interface */
1192
7b5a2f43 1193static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1194
f3dd6933 1195static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1196
b60c80d6 1197static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1198
2e276125 1199static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1200 char *, bfd *, struct dwarf2_cu *,
1201 struct dwarf2_section_info *,
1202 int);
2e276125 1203
8e19ed76
PS
1204static int attr_form_is_block (struct attribute *);
1205
3690dd37
JB
1206static int attr_form_is_section_offset (struct attribute *);
1207
1208static int attr_form_is_constant (struct attribute *);
1209
8cf6f0b1
TT
1210static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211 struct dwarf2_loclist_baton *baton,
1212 struct attribute *attr);
1213
93e7bd98
DJ
1214static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215 struct symbol *sym,
1216 struct dwarf2_cu *cu);
4c2df51b 1217
93311388
DE
1218static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219 struct abbrev_info *abbrev,
1220 struct dwarf2_cu *cu);
4bb7a0a7 1221
72bf9492
DJ
1222static void free_stack_comp_unit (void *);
1223
72bf9492
DJ
1224static hashval_t partial_die_hash (const void *item);
1225
1226static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
ae038cb0 1228static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1229 (sect_offset offset, struct objfile *objfile);
ae038cb0 1230
9816fde3 1231static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1232 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1233
1234static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1235 struct die_info *comp_unit_die);
93311388 1236
68dc6402 1237static void free_heap_comp_unit (void *);
ae038cb0
DJ
1238
1239static void free_cached_comp_units (void *);
1240
1241static void age_cached_comp_units (void);
1242
1243static void free_one_cached_comp_unit (void *);
1244
f792889a
DJ
1245static struct type *set_die_type (struct die_info *, struct type *,
1246 struct dwarf2_cu *);
1c379e20 1247
ae038cb0
DJ
1248static void create_all_comp_units (struct objfile *);
1249
0e50663e 1250static int create_all_type_units (struct objfile *);
1fd400ff 1251
a0f42c21 1252static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1253
1254static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1255
1256static void dwarf2_add_dependence (struct dwarf2_cu *,
1257 struct dwarf2_per_cu_data *);
1258
ae038cb0
DJ
1259static void dwarf2_mark (struct dwarf2_cu *);
1260
1261static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1262
b64f50a1 1263static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1264 struct dwarf2_per_cu_data *per_cu);
1265
f792889a 1266static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1267
9291a0cd
TT
1268static void dwarf2_release_queue (void *dummy);
1269
a0f42c21 1270static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1271
a0f42c21 1272static void process_queue (void);
9291a0cd
TT
1273
1274static void find_file_and_directory (struct die_info *die,
1275 struct dwarf2_cu *cu,
1276 char **name, char **comp_dir);
1277
1278static char *file_full_name (int file, struct line_header *lh,
1279 const char *comp_dir);
1280
9ff913ba
DE
1281static gdb_byte *read_and_check_comp_unit_head
1282 (struct comp_unit_head *header,
1283 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1284 int is_debug_types_section);
9291a0cd
TT
1285
1286static void init_cu_die_reader (struct die_reader_specs *reader,
1287 struct dwarf2_cu *cu);
1288
673bfd45 1289static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1290
9291a0cd
TT
1291#if WORDS_BIGENDIAN
1292
1293/* Convert VALUE between big- and little-endian. */
1294static offset_type
1295byte_swap (offset_type value)
1296{
1297 offset_type result;
1298
1299 result = (value & 0xff) << 24;
1300 result |= (value & 0xff00) << 8;
1301 result |= (value & 0xff0000) >> 8;
1302 result |= (value & 0xff000000) >> 24;
1303 return result;
1304}
1305
1306#define MAYBE_SWAP(V) byte_swap (V)
1307
1308#else
1309#define MAYBE_SWAP(V) (V)
1310#endif /* WORDS_BIGENDIAN */
1311
1312/* The suffix for an index file. */
1313#define INDEX_SUFFIX ".gdb-index"
1314
3da10d80
KS
1315static const char *dwarf2_physname (char *name, struct die_info *die,
1316 struct dwarf2_cu *cu);
1317
c906108c 1318/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1319 information and return true if we have enough to do something.
1320 NAMES points to the dwarf2 section names, or is NULL if the standard
1321 ELF names are used. */
c906108c
SS
1322
1323int
251d32d9
TG
1324dwarf2_has_info (struct objfile *objfile,
1325 const struct dwarf2_debug_sections *names)
c906108c 1326{
be391dca
TT
1327 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1328 if (!dwarf2_per_objfile)
1329 {
1330 /* Initialize per-objfile state. */
1331 struct dwarf2_per_objfile *data
1332 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1333
be391dca
TT
1334 memset (data, 0, sizeof (*data));
1335 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1336 dwarf2_per_objfile = data;
6502dd73 1337
251d32d9
TG
1338 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1339 (void *) names);
be391dca
TT
1340 dwarf2_per_objfile->objfile = objfile;
1341 }
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1344}
1345
251d32d9
TG
1346/* When loading sections, we look either for uncompressed section or for
1347 compressed section names. */
233a11ab
CS
1348
1349static int
251d32d9
TG
1350section_is_p (const char *section_name,
1351 const struct dwarf2_section_names *names)
233a11ab 1352{
251d32d9
TG
1353 if (names->normal != NULL
1354 && strcmp (section_name, names->normal) == 0)
1355 return 1;
1356 if (names->compressed != NULL
1357 && strcmp (section_name, names->compressed) == 0)
1358 return 1;
1359 return 0;
233a11ab
CS
1360}
1361
c906108c
SS
1362/* This function is mapped across the sections and remembers the
1363 offset and size of each of the debugging sections we are interested
1364 in. */
1365
1366static void
251d32d9 1367dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1368{
251d32d9
TG
1369 const struct dwarf2_debug_sections *names;
1370
1371 if (vnames == NULL)
1372 names = &dwarf2_elf_names;
1373 else
1374 names = (const struct dwarf2_debug_sections *) vnames;
1375
1376 if (section_is_p (sectp->name, &names->info))
c906108c 1377 {
dce234bc
PP
1378 dwarf2_per_objfile->info.asection = sectp;
1379 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1380 }
251d32d9 1381 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->abbrev.asection = sectp;
1384 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1385 }
251d32d9 1386 else if (section_is_p (sectp->name, &names->line))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->line.asection = sectp;
1389 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1390 }
251d32d9 1391 else if (section_is_p (sectp->name, &names->loc))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->loc.asection = sectp;
1394 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1395 }
251d32d9 1396 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->macinfo.asection = sectp;
1399 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1400 }
cf2c3c16
TT
1401 else if (section_is_p (sectp->name, &names->macro))
1402 {
1403 dwarf2_per_objfile->macro.asection = sectp;
1404 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1405 }
251d32d9 1406 else if (section_is_p (sectp->name, &names->str))
c906108c 1407 {
dce234bc
PP
1408 dwarf2_per_objfile->str.asection = sectp;
1409 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1410 }
251d32d9 1411 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->frame.asection = sectp;
1414 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1415 }
251d32d9 1416 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1417 {
3799ccc6 1418 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1419
3799ccc6
EZ
1420 if (aflag & SEC_HAS_CONTENTS)
1421 {
dce234bc
PP
1422 dwarf2_per_objfile->eh_frame.asection = sectp;
1423 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1424 }
b6af0555 1425 }
251d32d9 1426 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1427 {
dce234bc
PP
1428 dwarf2_per_objfile->ranges.asection = sectp;
1429 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1430 }
251d32d9 1431 else if (section_is_p (sectp->name, &names->types))
348e048f 1432 {
8b70b953
TT
1433 struct dwarf2_section_info type_section;
1434
1435 memset (&type_section, 0, sizeof (type_section));
1436 type_section.asection = sectp;
1437 type_section.size = bfd_get_section_size (sectp);
1438
1439 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1440 &type_section);
348e048f 1441 }
251d32d9 1442 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1443 {
1444 dwarf2_per_objfile->gdb_index.asection = sectp;
1445 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1446 }
dce234bc 1447
72dca2f5
FR
1448 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1449 && bfd_section_vma (abfd, sectp) == 0)
1450 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1451}
1452
dce234bc
PP
1453/* Decompress a section that was compressed using zlib. Store the
1454 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1455
1456static void
dce234bc
PP
1457zlib_decompress_section (struct objfile *objfile, asection *sectp,
1458 gdb_byte **outbuf, bfd_size_type *outsize)
1459{
1460 bfd *abfd = objfile->obfd;
1461#ifndef HAVE_ZLIB_H
1462 error (_("Support for zlib-compressed DWARF data (from '%s') "
1463 "is disabled in this copy of GDB"),
1464 bfd_get_filename (abfd));
1465#else
1466 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1467 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1468 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1469 bfd_size_type uncompressed_size;
1470 gdb_byte *uncompressed_buffer;
1471 z_stream strm;
1472 int rc;
1473 int header_size = 12;
1474
1475 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1476 || bfd_bread (compressed_buffer,
1477 compressed_size, abfd) != compressed_size)
dce234bc
PP
1478 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1479 bfd_get_filename (abfd));
1480
1481 /* Read the zlib header. In this case, it should be "ZLIB" followed
1482 by the uncompressed section size, 8 bytes in big-endian order. */
1483 if (compressed_size < header_size
1484 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1485 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1486 bfd_get_filename (abfd));
1487 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1494 uncompressed_size += compressed_buffer[11];
1495
1496 /* It is possible the section consists of several compressed
1497 buffers concatenated together, so we uncompress in a loop. */
1498 strm.zalloc = NULL;
1499 strm.zfree = NULL;
1500 strm.opaque = NULL;
1501 strm.avail_in = compressed_size - header_size;
1502 strm.next_in = (Bytef*) compressed_buffer + header_size;
1503 strm.avail_out = uncompressed_size;
1504 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1505 uncompressed_size);
1506 rc = inflateInit (&strm);
1507 while (strm.avail_in > 0)
1508 {
1509 if (rc != Z_OK)
1510 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1511 bfd_get_filename (abfd), rc);
1512 strm.next_out = ((Bytef*) uncompressed_buffer
1513 + (uncompressed_size - strm.avail_out));
1514 rc = inflate (&strm, Z_FINISH);
1515 if (rc != Z_STREAM_END)
1516 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518 rc = inflateReset (&strm);
1519 }
1520 rc = inflateEnd (&strm);
1521 if (rc != Z_OK
1522 || strm.avail_out != 0)
1523 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1524 bfd_get_filename (abfd), rc);
1525
affddf13 1526 do_cleanups (cleanup);
dce234bc
PP
1527 *outbuf = uncompressed_buffer;
1528 *outsize = uncompressed_size;
1529#endif
233a11ab
CS
1530}
1531
9e0ac564
TT
1532/* A helper function that decides whether a section is empty. */
1533
1534static int
1535dwarf2_section_empty_p (struct dwarf2_section_info *info)
1536{
1537 return info->asection == NULL || info->size == 0;
1538}
1539
9cdd5dbd 1540/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1541 OBJFILE, store info about the section into INFO.
1542 If the section is compressed, uncompress it before returning. */
c906108c 1543
dce234bc
PP
1544static void
1545dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1546{
dce234bc
PP
1547 bfd *abfd = objfile->obfd;
1548 asection *sectp = info->asection;
1549 gdb_byte *buf, *retbuf;
1550 unsigned char header[4];
c906108c 1551
be391dca
TT
1552 if (info->readin)
1553 return;
dce234bc 1554 info->buffer = NULL;
b315ab21 1555 info->map_addr = NULL;
be391dca 1556 info->readin = 1;
188dd5d6 1557
9e0ac564 1558 if (dwarf2_section_empty_p (info))
dce234bc 1559 return;
c906108c 1560
dce234bc
PP
1561 /* Check if the file has a 4-byte header indicating compression. */
1562 if (info->size > sizeof (header)
1563 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1564 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1565 {
1566 /* Upon decompression, update the buffer and its size. */
1567 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1568 {
1569 zlib_decompress_section (objfile, sectp, &info->buffer,
1570 &info->size);
1571 return;
1572 }
1573 }
4bdf3d34 1574
dce234bc
PP
1575#ifdef HAVE_MMAP
1576 if (pagesize == 0)
1577 pagesize = getpagesize ();
2e276125 1578
dce234bc
PP
1579 /* Only try to mmap sections which are large enough: we don't want to
1580 waste space due to fragmentation. Also, only try mmap for sections
1581 without relocations. */
1582
1583 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1584 {
b315ab21
TG
1585 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1586 MAP_PRIVATE, sectp->filepos,
1587 &info->map_addr, &info->map_len);
dce234bc 1588
b315ab21 1589 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1590 {
be391dca 1591#if HAVE_POSIX_MADVISE
b315ab21 1592 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1593#endif
dce234bc
PP
1594 return;
1595 }
1596 }
1597#endif
1598
1599 /* If we get here, we are a normal, not-compressed section. */
1600 info->buffer = buf
1601 = obstack_alloc (&objfile->objfile_obstack, info->size);
1602
1603 /* When debugging .o files, we may need to apply relocations; see
1604 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1605 We never compress sections in .o files, so we only need to
1606 try this when the section is not compressed. */
ac8035ab 1607 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1608 if (retbuf != NULL)
1609 {
1610 info->buffer = retbuf;
1611 return;
1612 }
1613
1614 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1615 || bfd_bread (buf, info->size, abfd) != info->size)
1616 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1617 bfd_get_filename (abfd));
1618}
1619
9e0ac564
TT
1620/* A helper function that returns the size of a section in a safe way.
1621 If you are positive that the section has been read before using the
1622 size, then it is safe to refer to the dwarf2_section_info object's
1623 "size" field directly. In other cases, you must call this
1624 function, because for compressed sections the size field is not set
1625 correctly until the section has been read. */
1626
1627static bfd_size_type
1628dwarf2_section_size (struct objfile *objfile,
1629 struct dwarf2_section_info *info)
1630{
1631 if (!info->readin)
1632 dwarf2_read_section (objfile, info);
1633 return info->size;
1634}
1635
dce234bc 1636/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1637 SECTION_NAME. */
af34e669 1638
dce234bc 1639void
3017a003
TG
1640dwarf2_get_section_info (struct objfile *objfile,
1641 enum dwarf2_section_enum sect,
dce234bc
PP
1642 asection **sectp, gdb_byte **bufp,
1643 bfd_size_type *sizep)
1644{
1645 struct dwarf2_per_objfile *data
1646 = objfile_data (objfile, dwarf2_objfile_data_key);
1647 struct dwarf2_section_info *info;
a3b2a86b
TT
1648
1649 /* We may see an objfile without any DWARF, in which case we just
1650 return nothing. */
1651 if (data == NULL)
1652 {
1653 *sectp = NULL;
1654 *bufp = NULL;
1655 *sizep = 0;
1656 return;
1657 }
3017a003
TG
1658 switch (sect)
1659 {
1660 case DWARF2_DEBUG_FRAME:
1661 info = &data->frame;
1662 break;
1663 case DWARF2_EH_FRAME:
1664 info = &data->eh_frame;
1665 break;
1666 default:
1667 gdb_assert_not_reached ("unexpected section");
1668 }
dce234bc 1669
9e0ac564 1670 dwarf2_read_section (objfile, info);
dce234bc
PP
1671
1672 *sectp = info->asection;
1673 *bufp = info->buffer;
1674 *sizep = info->size;
1675}
1676
9291a0cd 1677\f
7b9f3c50
DE
1678/* DWARF quick_symbols_functions support. */
1679
1680/* TUs can share .debug_line entries, and there can be a lot more TUs than
1681 unique line tables, so we maintain a separate table of all .debug_line
1682 derived entries to support the sharing.
1683 All the quick functions need is the list of file names. We discard the
1684 line_header when we're done and don't need to record it here. */
1685struct quick_file_names
1686{
1687 /* The offset in .debug_line of the line table. We hash on this. */
1688 unsigned int offset;
1689
1690 /* The number of entries in file_names, real_names. */
1691 unsigned int num_file_names;
1692
1693 /* The file names from the line table, after being run through
1694 file_full_name. */
1695 const char **file_names;
1696
1697 /* The file names from the line table after being run through
1698 gdb_realpath. These are computed lazily. */
1699 const char **real_names;
1700};
1701
1702/* When using the index (and thus not using psymtabs), each CU has an
1703 object of this type. This is used to hold information needed by
1704 the various "quick" methods. */
1705struct dwarf2_per_cu_quick_data
1706{
1707 /* The file table. This can be NULL if there was no file table
1708 or it's currently not read in.
1709 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1710 struct quick_file_names *file_names;
1711
1712 /* The corresponding symbol table. This is NULL if symbols for this
1713 CU have not yet been read. */
1714 struct symtab *symtab;
1715
1716 /* A temporary mark bit used when iterating over all CUs in
1717 expand_symtabs_matching. */
1718 unsigned int mark : 1;
1719
1720 /* True if we've tried to read the file table and found there isn't one.
1721 There will be no point in trying to read it again next time. */
1722 unsigned int no_file_data : 1;
1723};
1724
1725/* Hash function for a quick_file_names. */
1726
1727static hashval_t
1728hash_file_name_entry (const void *e)
1729{
1730 const struct quick_file_names *file_data = e;
1731
1732 return file_data->offset;
1733}
1734
1735/* Equality function for a quick_file_names. */
1736
1737static int
1738eq_file_name_entry (const void *a, const void *b)
1739{
1740 const struct quick_file_names *ea = a;
1741 const struct quick_file_names *eb = b;
1742
1743 return ea->offset == eb->offset;
1744}
1745
1746/* Delete function for a quick_file_names. */
1747
1748static void
1749delete_file_name_entry (void *e)
1750{
1751 struct quick_file_names *file_data = e;
1752 int i;
1753
1754 for (i = 0; i < file_data->num_file_names; ++i)
1755 {
1756 xfree ((void*) file_data->file_names[i]);
1757 if (file_data->real_names)
1758 xfree ((void*) file_data->real_names[i]);
1759 }
1760
1761 /* The space for the struct itself lives on objfile_obstack,
1762 so we don't free it here. */
1763}
1764
1765/* Create a quick_file_names hash table. */
1766
1767static htab_t
1768create_quick_file_names_table (unsigned int nr_initial_entries)
1769{
1770 return htab_create_alloc (nr_initial_entries,
1771 hash_file_name_entry, eq_file_name_entry,
1772 delete_file_name_entry, xcalloc, xfree);
1773}
9291a0cd 1774
918dd910
JK
1775/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1776 have to be created afterwards. You should call age_cached_comp_units after
1777 processing PER_CU->CU. dw2_setup must have been already called. */
1778
1779static void
1780load_cu (struct dwarf2_per_cu_data *per_cu)
1781{
b0df02fd 1782 if (per_cu->debug_types_section)
e5fe5e75 1783 load_full_type_unit (per_cu);
918dd910 1784 else
a0f42c21 1785 load_full_comp_unit (per_cu);
918dd910 1786
918dd910 1787 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1788
1789 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1790}
1791
a0f42c21 1792/* Read in the symbols for PER_CU. */
2fdf6df6 1793
9291a0cd 1794static void
a0f42c21 1795dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1796{
1797 struct cleanup *back_to;
1798
1799 back_to = make_cleanup (dwarf2_release_queue, NULL);
1800
a0f42c21 1801 queue_comp_unit (per_cu);
9291a0cd 1802
918dd910 1803 load_cu (per_cu);
9291a0cd 1804
a0f42c21 1805 process_queue ();
9291a0cd
TT
1806
1807 /* Age the cache, releasing compilation units that have not
1808 been used recently. */
1809 age_cached_comp_units ();
1810
1811 do_cleanups (back_to);
1812}
1813
1814/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1815 the objfile from which this CU came. Returns the resulting symbol
1816 table. */
2fdf6df6 1817
9291a0cd 1818static struct symtab *
a0f42c21 1819dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1820{
1821 if (!per_cu->v.quick->symtab)
1822 {
1823 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1824 increment_reading_symtab ();
a0f42c21 1825 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1826 do_cleanups (back_to);
1827 }
1828 return per_cu->v.quick->symtab;
1829}
1830
1fd400ff 1831/* Return the CU given its index. */
2fdf6df6 1832
1fd400ff
TT
1833static struct dwarf2_per_cu_data *
1834dw2_get_cu (int index)
1835{
1836 if (index >= dwarf2_per_objfile->n_comp_units)
1837 {
1838 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1839 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1840 }
1841 return dwarf2_per_objfile->all_comp_units[index];
1842}
1843
9291a0cd
TT
1844/* A helper function that knows how to read a 64-bit value in a way
1845 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1846 otherwise. */
2fdf6df6 1847
9291a0cd
TT
1848static int
1849extract_cu_value (const char *bytes, ULONGEST *result)
1850{
1851 if (sizeof (ULONGEST) < 8)
1852 {
1853 int i;
1854
1855 /* Ignore the upper 4 bytes if they are all zero. */
1856 for (i = 0; i < 4; ++i)
1857 if (bytes[i + 4] != 0)
1858 return 0;
1859
1860 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1861 }
1862 else
1863 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1864 return 1;
1865}
1866
1867/* Read the CU list from the mapped index, and use it to create all
1868 the CU objects for this objfile. Return 0 if something went wrong,
1869 1 if everything went ok. */
2fdf6df6 1870
9291a0cd 1871static int
1fd400ff
TT
1872create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1873 offset_type cu_list_elements)
9291a0cd
TT
1874{
1875 offset_type i;
9291a0cd
TT
1876
1877 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1878 dwarf2_per_objfile->all_comp_units
1879 = obstack_alloc (&objfile->objfile_obstack,
1880 dwarf2_per_objfile->n_comp_units
1881 * sizeof (struct dwarf2_per_cu_data *));
1882
1883 for (i = 0; i < cu_list_elements; i += 2)
1884 {
1885 struct dwarf2_per_cu_data *the_cu;
1886 ULONGEST offset, length;
1887
1888 if (!extract_cu_value (cu_list, &offset)
1889 || !extract_cu_value (cu_list + 8, &length))
1890 return 0;
1891 cu_list += 2 * 8;
1892
1893 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1894 struct dwarf2_per_cu_data);
b64f50a1 1895 the_cu->offset.sect_off = offset;
9291a0cd
TT
1896 the_cu->length = length;
1897 the_cu->objfile = objfile;
1898 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1899 struct dwarf2_per_cu_quick_data);
1900 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1901 }
1902
1903 return 1;
1904}
1905
1fd400ff 1906/* Create the signatured type hash table from the index. */
673bfd45 1907
1fd400ff 1908static int
673bfd45 1909create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1910 struct dwarf2_section_info *section,
673bfd45
DE
1911 const gdb_byte *bytes,
1912 offset_type elements)
1fd400ff
TT
1913{
1914 offset_type i;
673bfd45 1915 htab_t sig_types_hash;
1fd400ff 1916
d467dd73
DE
1917 dwarf2_per_objfile->n_type_units = elements / 3;
1918 dwarf2_per_objfile->all_type_units
1fd400ff 1919 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1920 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1921 * sizeof (struct dwarf2_per_cu_data *));
1922
673bfd45 1923 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1924
1925 for (i = 0; i < elements; i += 3)
1926 {
52dc124a
DE
1927 struct signatured_type *sig_type;
1928 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
1929 void **slot;
1930
1931 if (!extract_cu_value (bytes, &offset)
52dc124a 1932 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
1933 return 0;
1934 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1935 bytes += 3 * 8;
1936
52dc124a 1937 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 1938 struct signatured_type);
52dc124a
DE
1939 sig_type->signature = signature;
1940 sig_type->type_offset.cu_off = type_offset_in_tu;
1941 sig_type->per_cu.debug_types_section = section;
1942 sig_type->per_cu.offset.sect_off = offset;
1943 sig_type->per_cu.objfile = objfile;
1944 sig_type->per_cu.v.quick
1fd400ff
TT
1945 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1946 struct dwarf2_per_cu_quick_data);
1947
52dc124a
DE
1948 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
1949 *slot = sig_type;
1fd400ff 1950
52dc124a 1951 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
1952 }
1953
673bfd45 1954 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1955
1956 return 1;
1957}
1958
9291a0cd
TT
1959/* Read the address map data from the mapped index, and use it to
1960 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1961
9291a0cd
TT
1962static void
1963create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1964{
1965 const gdb_byte *iter, *end;
1966 struct obstack temp_obstack;
1967 struct addrmap *mutable_map;
1968 struct cleanup *cleanup;
1969 CORE_ADDR baseaddr;
1970
1971 obstack_init (&temp_obstack);
1972 cleanup = make_cleanup_obstack_free (&temp_obstack);
1973 mutable_map = addrmap_create_mutable (&temp_obstack);
1974
1975 iter = index->address_table;
1976 end = iter + index->address_table_size;
1977
1978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1979
1980 while (iter < end)
1981 {
1982 ULONGEST hi, lo, cu_index;
1983 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984 iter += 8;
1985 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1986 iter += 8;
1987 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1988 iter += 4;
1989
1990 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1991 dw2_get_cu (cu_index));
9291a0cd
TT
1992 }
1993
1994 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1995 &objfile->objfile_obstack);
1996 do_cleanups (cleanup);
1997}
1998
59d7bcaf
JK
1999/* The hash function for strings in the mapped index. This is the same as
2000 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2001 implementation. This is necessary because the hash function is tied to the
2002 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2003 SYMBOL_HASH_NEXT.
2004
2005 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2006
9291a0cd 2007static hashval_t
559a7a62 2008mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2009{
2010 const unsigned char *str = (const unsigned char *) p;
2011 hashval_t r = 0;
2012 unsigned char c;
2013
2014 while ((c = *str++) != 0)
559a7a62
JK
2015 {
2016 if (index_version >= 5)
2017 c = tolower (c);
2018 r = r * 67 + c - 113;
2019 }
9291a0cd
TT
2020
2021 return r;
2022}
2023
2024/* Find a slot in the mapped index INDEX for the object named NAME.
2025 If NAME is found, set *VEC_OUT to point to the CU vector in the
2026 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2027
9291a0cd
TT
2028static int
2029find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2030 offset_type **vec_out)
2031{
0cf03b49
JK
2032 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2033 offset_type hash;
9291a0cd 2034 offset_type slot, step;
559a7a62 2035 int (*cmp) (const char *, const char *);
9291a0cd 2036
0cf03b49
JK
2037 if (current_language->la_language == language_cplus
2038 || current_language->la_language == language_java
2039 || current_language->la_language == language_fortran)
2040 {
2041 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2042 not contain any. */
2043 const char *paren = strchr (name, '(');
2044
2045 if (paren)
2046 {
2047 char *dup;
2048
2049 dup = xmalloc (paren - name + 1);
2050 memcpy (dup, name, paren - name);
2051 dup[paren - name] = 0;
2052
2053 make_cleanup (xfree, dup);
2054 name = dup;
2055 }
2056 }
2057
559a7a62 2058 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2059 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2060 simulate our NAME being searched is also lowercased. */
2061 hash = mapped_index_string_hash ((index->version == 4
2062 && case_sensitivity == case_sensitive_off
2063 ? 5 : index->version),
2064 name);
2065
3876f04e
DE
2066 slot = hash & (index->symbol_table_slots - 1);
2067 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2068 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2069
2070 for (;;)
2071 {
2072 /* Convert a slot number to an offset into the table. */
2073 offset_type i = 2 * slot;
2074 const char *str;
3876f04e 2075 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2076 {
2077 do_cleanups (back_to);
2078 return 0;
2079 }
9291a0cd 2080
3876f04e 2081 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2082 if (!cmp (name, str))
9291a0cd
TT
2083 {
2084 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2085 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2086 do_cleanups (back_to);
9291a0cd
TT
2087 return 1;
2088 }
2089
3876f04e 2090 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2091 }
2092}
2093
2094/* Read the index file. If everything went ok, initialize the "quick"
2095 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2096
9291a0cd
TT
2097static int
2098dwarf2_read_index (struct objfile *objfile)
2099{
9291a0cd
TT
2100 char *addr;
2101 struct mapped_index *map;
b3b272e1 2102 offset_type *metadata;
ac0b195c
KW
2103 const gdb_byte *cu_list;
2104 const gdb_byte *types_list = NULL;
2105 offset_type version, cu_list_elements;
2106 offset_type types_list_elements = 0;
1fd400ff 2107 int i;
9291a0cd 2108
9e0ac564 2109 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2110 return 0;
82430852
JK
2111
2112 /* Older elfutils strip versions could keep the section in the main
2113 executable while splitting it for the separate debug info file. */
2114 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2115 & SEC_HAS_CONTENTS) == 0)
2116 return 0;
2117
9291a0cd
TT
2118 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2119
2120 addr = dwarf2_per_objfile->gdb_index.buffer;
2121 /* Version check. */
1fd400ff 2122 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2123 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2124 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2125 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2126 indices. */
831adc1f 2127 if (version < 4)
481860b3
GB
2128 {
2129 static int warning_printed = 0;
2130 if (!warning_printed)
2131 {
2132 warning (_("Skipping obsolete .gdb_index section in %s."),
2133 objfile->name);
2134 warning_printed = 1;
2135 }
2136 return 0;
2137 }
2138 /* Index version 4 uses a different hash function than index version
2139 5 and later.
2140
2141 Versions earlier than 6 did not emit psymbols for inlined
2142 functions. Using these files will cause GDB not to be able to
2143 set breakpoints on inlined functions by name, so we ignore these
2144 indices unless the --use-deprecated-index-sections command line
2145 option was supplied. */
2146 if (version < 6 && !use_deprecated_index_sections)
2147 {
2148 static int warning_printed = 0;
2149 if (!warning_printed)
2150 {
2151 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2152 "--use-deprecated-index-sections to use them anyway"),
2153 objfile->name);
2154 warning_printed = 1;
2155 }
2156 return 0;
2157 }
2158 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2159 longer backward compatible. */
481860b3 2160 if (version > 6)
594e8718 2161 return 0;
9291a0cd
TT
2162
2163 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2164 map->version = version;
b3b272e1 2165 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2166
2167 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2168
2169 i = 0;
2170 cu_list = addr + MAYBE_SWAP (metadata[i]);
2171 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2172 / 8);
1fd400ff
TT
2173 ++i;
2174
987d643c
TT
2175 types_list = addr + MAYBE_SWAP (metadata[i]);
2176 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2177 - MAYBE_SWAP (metadata[i]))
2178 / 8);
2179 ++i;
1fd400ff
TT
2180
2181 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2182 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2183 - MAYBE_SWAP (metadata[i]));
2184 ++i;
2185
3876f04e
DE
2186 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2187 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2188 - MAYBE_SWAP (metadata[i]))
2189 / (2 * sizeof (offset_type)));
1fd400ff 2190 ++i;
9291a0cd 2191
1fd400ff
TT
2192 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2193
2194 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2195 return 0;
2196
8b70b953
TT
2197 if (types_list_elements)
2198 {
2199 struct dwarf2_section_info *section;
2200
2201 /* We can only handle a single .debug_types when we have an
2202 index. */
2203 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2204 return 0;
2205
2206 section = VEC_index (dwarf2_section_info_def,
2207 dwarf2_per_objfile->types, 0);
2208
2209 if (!create_signatured_type_table_from_index (objfile, section,
2210 types_list,
2211 types_list_elements))
2212 return 0;
2213 }
9291a0cd
TT
2214
2215 create_addrmap_from_index (objfile, map);
2216
2217 dwarf2_per_objfile->index_table = map;
2218 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2219 dwarf2_per_objfile->quick_file_names_table =
2220 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2221
2222 return 1;
2223}
2224
2225/* A helper for the "quick" functions which sets the global
2226 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2227
9291a0cd
TT
2228static void
2229dw2_setup (struct objfile *objfile)
2230{
2231 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2232 gdb_assert (dwarf2_per_objfile);
2233}
2234
2235/* A helper for the "quick" functions which attempts to read the line
2236 table for THIS_CU. */
2fdf6df6 2237
7b9f3c50
DE
2238static struct quick_file_names *
2239dw2_get_file_names (struct objfile *objfile,
2240 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2241{
2242 bfd *abfd = objfile->obfd;
7b9f3c50 2243 struct line_header *lh;
9291a0cd
TT
2244 struct attribute *attr;
2245 struct cleanup *cleanups;
2246 struct die_info *comp_unit_die;
36374493 2247 struct dwarf2_section_info* sec;
9ff913ba 2248 gdb_byte *info_ptr;
9291a0cd
TT
2249 int has_children, i;
2250 struct dwarf2_cu cu;
9ff913ba 2251 unsigned int bytes_read;
9291a0cd
TT
2252 struct die_reader_specs reader_specs;
2253 char *name, *comp_dir;
7b9f3c50
DE
2254 void **slot;
2255 struct quick_file_names *qfn;
2256 unsigned int line_offset;
9291a0cd 2257
7b9f3c50
DE
2258 if (this_cu->v.quick->file_names != NULL)
2259 return this_cu->v.quick->file_names;
2260 /* If we know there is no line data, no point in looking again. */
2261 if (this_cu->v.quick->no_file_data)
2262 return NULL;
9291a0cd 2263
23745b47 2264 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2265 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2266
b0df02fd
DE
2267 if (this_cu->debug_types_section)
2268 sec = this_cu->debug_types_section;
36374493
DE
2269 else
2270 sec = &dwarf2_per_objfile->info;
2271 dwarf2_read_section (objfile, sec);
b64f50a1 2272 info_ptr = sec->buffer + this_cu->offset.sect_off;
9291a0cd 2273
9ff913ba
DE
2274 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2275 this_cu->debug_types_section != NULL);
9291a0cd 2276
6caca83c 2277 /* Skip dummy compilation units. */
9ff913ba 2278 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2279 || peek_abbrev_code (abfd, info_ptr) == 0)
2280 {
2281 do_cleanups (cleanups);
2282 return NULL;
2283 }
2284
e5fe5e75 2285 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2286 make_cleanup (dwarf2_free_abbrev_table, &cu);
2287
9291a0cd 2288 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2289 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2290 &has_children);
9291a0cd 2291
7b9f3c50
DE
2292 lh = NULL;
2293 slot = NULL;
2294 line_offset = 0;
9291a0cd
TT
2295 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2296 if (attr)
2297 {
7b9f3c50
DE
2298 struct quick_file_names find_entry;
2299
2300 line_offset = DW_UNSND (attr);
2301
2302 /* We may have already read in this line header (TU line header sharing).
2303 If we have we're done. */
2304 find_entry.offset = line_offset;
2305 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2306 &find_entry, INSERT);
2307 if (*slot != NULL)
2308 {
2309 do_cleanups (cleanups);
2310 this_cu->v.quick->file_names = *slot;
2311 return *slot;
2312 }
2313
9291a0cd
TT
2314 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2315 }
2316 if (lh == NULL)
2317 {
2318 do_cleanups (cleanups);
7b9f3c50
DE
2319 this_cu->v.quick->no_file_data = 1;
2320 return NULL;
9291a0cd
TT
2321 }
2322
7b9f3c50
DE
2323 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2324 qfn->offset = line_offset;
2325 gdb_assert (slot != NULL);
2326 *slot = qfn;
9291a0cd 2327
7b9f3c50 2328 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2329
7b9f3c50
DE
2330 qfn->num_file_names = lh->num_file_names;
2331 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2332 lh->num_file_names * sizeof (char *));
9291a0cd 2333 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2334 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2335 qfn->real_names = NULL;
9291a0cd 2336
7b9f3c50 2337 free_line_header (lh);
9291a0cd 2338 do_cleanups (cleanups);
7b9f3c50
DE
2339
2340 this_cu->v.quick->file_names = qfn;
2341 return qfn;
9291a0cd
TT
2342}
2343
2344/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2345 real path for a given file name from the line table. */
2fdf6df6 2346
9291a0cd 2347static const char *
7b9f3c50
DE
2348dw2_get_real_path (struct objfile *objfile,
2349 struct quick_file_names *qfn, int index)
9291a0cd 2350{
7b9f3c50
DE
2351 if (qfn->real_names == NULL)
2352 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2353 qfn->num_file_names, sizeof (char *));
9291a0cd 2354
7b9f3c50
DE
2355 if (qfn->real_names[index] == NULL)
2356 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2357
7b9f3c50 2358 return qfn->real_names[index];
9291a0cd
TT
2359}
2360
2361static struct symtab *
2362dw2_find_last_source_symtab (struct objfile *objfile)
2363{
2364 int index;
ae2de4f8 2365
9291a0cd
TT
2366 dw2_setup (objfile);
2367 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2368 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2369}
2370
7b9f3c50
DE
2371/* Traversal function for dw2_forget_cached_source_info. */
2372
2373static int
2374dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2375{
7b9f3c50 2376 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2377
7b9f3c50 2378 if (file_data->real_names)
9291a0cd 2379 {
7b9f3c50 2380 int i;
9291a0cd 2381
7b9f3c50 2382 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2383 {
7b9f3c50
DE
2384 xfree ((void*) file_data->real_names[i]);
2385 file_data->real_names[i] = NULL;
9291a0cd
TT
2386 }
2387 }
7b9f3c50
DE
2388
2389 return 1;
2390}
2391
2392static void
2393dw2_forget_cached_source_info (struct objfile *objfile)
2394{
2395 dw2_setup (objfile);
2396
2397 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2398 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2399}
2400
f8eba3c6
TT
2401/* Helper function for dw2_map_symtabs_matching_filename that expands
2402 the symtabs and calls the iterator. */
2403
2404static int
2405dw2_map_expand_apply (struct objfile *objfile,
2406 struct dwarf2_per_cu_data *per_cu,
2407 const char *name,
2408 const char *full_path, const char *real_path,
2409 int (*callback) (struct symtab *, void *),
2410 void *data)
2411{
2412 struct symtab *last_made = objfile->symtabs;
2413
2414 /* Don't visit already-expanded CUs. */
2415 if (per_cu->v.quick->symtab)
2416 return 0;
2417
2418 /* This may expand more than one symtab, and we want to iterate over
2419 all of them. */
a0f42c21 2420 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2421
2422 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2423 objfile->symtabs, last_made);
2424}
2425
2426/* Implementation of the map_symtabs_matching_filename method. */
2427
9291a0cd 2428static int
f8eba3c6
TT
2429dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2430 const char *full_path, const char *real_path,
2431 int (*callback) (struct symtab *, void *),
2432 void *data)
9291a0cd
TT
2433{
2434 int i;
c011a4f4 2435 const char *name_basename = lbasename (name);
4aac40c8
TT
2436 int name_len = strlen (name);
2437 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2438
2439 dw2_setup (objfile);
ae2de4f8 2440
1fd400ff 2441 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2442 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2443 {
2444 int j;
e254ef6a 2445 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2446 struct quick_file_names *file_data;
9291a0cd 2447
3d7bb9d9 2448 /* We only need to look at symtabs not already expanded. */
e254ef6a 2449 if (per_cu->v.quick->symtab)
9291a0cd
TT
2450 continue;
2451
7b9f3c50
DE
2452 file_data = dw2_get_file_names (objfile, per_cu);
2453 if (file_data == NULL)
9291a0cd
TT
2454 continue;
2455
7b9f3c50 2456 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2457 {
7b9f3c50 2458 const char *this_name = file_data->file_names[j];
9291a0cd 2459
4aac40c8
TT
2460 if (FILENAME_CMP (name, this_name) == 0
2461 || (!is_abs && compare_filenames_for_search (this_name,
2462 name, name_len)))
9291a0cd 2463 {
f8eba3c6
TT
2464 if (dw2_map_expand_apply (objfile, per_cu,
2465 name, full_path, real_path,
2466 callback, data))
2467 return 1;
4aac40c8 2468 }
9291a0cd 2469
c011a4f4
DE
2470 /* Before we invoke realpath, which can get expensive when many
2471 files are involved, do a quick comparison of the basenames. */
2472 if (! basenames_may_differ
2473 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2474 continue;
2475
9291a0cd
TT
2476 if (full_path != NULL)
2477 {
7b9f3c50
DE
2478 const char *this_real_name = dw2_get_real_path (objfile,
2479 file_data, j);
9291a0cd 2480
7b9f3c50 2481 if (this_real_name != NULL
4aac40c8
TT
2482 && (FILENAME_CMP (full_path, this_real_name) == 0
2483 || (!is_abs
2484 && compare_filenames_for_search (this_real_name,
2485 name, name_len))))
9291a0cd 2486 {
f8eba3c6
TT
2487 if (dw2_map_expand_apply (objfile, per_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
9291a0cd
TT
2491 }
2492 }
2493
2494 if (real_path != NULL)
2495 {
7b9f3c50
DE
2496 const char *this_real_name = dw2_get_real_path (objfile,
2497 file_data, j);
9291a0cd 2498
7b9f3c50 2499 if (this_real_name != NULL
4aac40c8
TT
2500 && (FILENAME_CMP (real_path, this_real_name) == 0
2501 || (!is_abs
2502 && compare_filenames_for_search (this_real_name,
2503 name, name_len))))
9291a0cd 2504 {
f8eba3c6
TT
2505 if (dw2_map_expand_apply (objfile, per_cu,
2506 name, full_path, real_path,
2507 callback, data))
2508 return 1;
9291a0cd
TT
2509 }
2510 }
2511 }
2512 }
2513
9291a0cd
TT
2514 return 0;
2515}
2516
2517static struct symtab *
2518dw2_lookup_symbol (struct objfile *objfile, int block_index,
2519 const char *name, domain_enum domain)
2520{
774b6a14 2521 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2522 instead. */
2523 return NULL;
2524}
2525
2526/* A helper function that expands all symtabs that hold an object
2527 named NAME. */
2fdf6df6 2528
9291a0cd
TT
2529static void
2530dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2531{
2532 dw2_setup (objfile);
2533
ae2de4f8 2534 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2535 if (dwarf2_per_objfile->index_table)
2536 {
2537 offset_type *vec;
2538
2539 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2540 name, &vec))
2541 {
2542 offset_type i, len = MAYBE_SWAP (*vec);
2543 for (i = 0; i < len; ++i)
2544 {
2545 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2546 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2547
a0f42c21 2548 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2549 }
2550 }
2551 }
2552}
2553
774b6a14
TT
2554static void
2555dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2556 enum block_enum block_kind, const char *name,
774b6a14 2557 domain_enum domain)
9291a0cd 2558{
774b6a14 2559 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2560}
2561
2562static void
2563dw2_print_stats (struct objfile *objfile)
2564{
2565 int i, count;
2566
2567 dw2_setup (objfile);
2568 count = 0;
1fd400ff 2569 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2570 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2571 {
e254ef6a 2572 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2573
e254ef6a 2574 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2575 ++count;
2576 }
2577 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2578}
2579
2580static void
2581dw2_dump (struct objfile *objfile)
2582{
2583 /* Nothing worth printing. */
2584}
2585
2586static void
2587dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2588 struct section_offsets *delta)
2589{
2590 /* There's nothing to relocate here. */
2591}
2592
2593static void
2594dw2_expand_symtabs_for_function (struct objfile *objfile,
2595 const char *func_name)
2596{
2597 dw2_do_expand_symtabs_matching (objfile, func_name);
2598}
2599
2600static void
2601dw2_expand_all_symtabs (struct objfile *objfile)
2602{
2603 int i;
2604
2605 dw2_setup (objfile);
1fd400ff
TT
2606
2607 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2608 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2609 {
e254ef6a 2610 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2611
a0f42c21 2612 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2613 }
2614}
2615
2616static void
2617dw2_expand_symtabs_with_filename (struct objfile *objfile,
2618 const char *filename)
2619{
2620 int i;
2621
2622 dw2_setup (objfile);
d4637a04
DE
2623
2624 /* We don't need to consider type units here.
2625 This is only called for examining code, e.g. expand_line_sal.
2626 There can be an order of magnitude (or more) more type units
2627 than comp units, and we avoid them if we can. */
2628
2629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2630 {
2631 int j;
e254ef6a 2632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2633 struct quick_file_names *file_data;
9291a0cd 2634
3d7bb9d9 2635 /* We only need to look at symtabs not already expanded. */
e254ef6a 2636 if (per_cu->v.quick->symtab)
9291a0cd
TT
2637 continue;
2638
7b9f3c50
DE
2639 file_data = dw2_get_file_names (objfile, per_cu);
2640 if (file_data == NULL)
9291a0cd
TT
2641 continue;
2642
7b9f3c50 2643 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2644 {
7b9f3c50 2645 const char *this_name = file_data->file_names[j];
1ef75ecc 2646 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2647 {
a0f42c21 2648 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2649 break;
2650 }
2651 }
2652 }
2653}
2654
dd786858 2655static const char *
9291a0cd
TT
2656dw2_find_symbol_file (struct objfile *objfile, const char *name)
2657{
e254ef6a 2658 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2659 offset_type *vec;
7b9f3c50 2660 struct quick_file_names *file_data;
9291a0cd
TT
2661
2662 dw2_setup (objfile);
2663
ae2de4f8 2664 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2665 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2666 {
2667 struct symtab *s;
2668
2669 ALL_OBJFILE_SYMTABS (objfile, s)
2670 if (s->primary)
2671 {
2672 struct blockvector *bv = BLOCKVECTOR (s);
2673 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2674 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2675
2676 if (sym)
2677 return sym->symtab->filename;
2678 }
2679 return NULL;
2680 }
9291a0cd
TT
2681
2682 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2683 name, &vec))
2684 return NULL;
2685
2686 /* Note that this just looks at the very first one named NAME -- but
2687 actually we are looking for a function. find_main_filename
2688 should be rewritten so that it doesn't require a custom hook. It
2689 could just use the ordinary symbol tables. */
2690 /* vec[0] is the length, which must always be >0. */
e254ef6a 2691 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2692
7b9f3c50
DE
2693 file_data = dw2_get_file_names (objfile, per_cu);
2694 if (file_data == NULL)
9291a0cd
TT
2695 return NULL;
2696
7b9f3c50 2697 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2698}
2699
2700static void
40658b94
PH
2701dw2_map_matching_symbols (const char * name, domain_enum namespace,
2702 struct objfile *objfile, int global,
2703 int (*callback) (struct block *,
2704 struct symbol *, void *),
2edb89d3
JK
2705 void *data, symbol_compare_ftype *match,
2706 symbol_compare_ftype *ordered_compare)
9291a0cd 2707{
40658b94 2708 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2709 current language is Ada for a non-Ada objfile using GNU index. As Ada
2710 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2711}
2712
2713static void
f8eba3c6
TT
2714dw2_expand_symtabs_matching
2715 (struct objfile *objfile,
2716 int (*file_matcher) (const char *, void *),
e078317b 2717 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2718 enum search_domain kind,
2719 void *data)
9291a0cd
TT
2720{
2721 int i;
2722 offset_type iter;
4b5246aa 2723 struct mapped_index *index;
9291a0cd
TT
2724
2725 dw2_setup (objfile);
ae2de4f8
DE
2726
2727 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2728 if (!dwarf2_per_objfile->index_table)
2729 return;
4b5246aa 2730 index = dwarf2_per_objfile->index_table;
9291a0cd 2731
7b08b9eb 2732 if (file_matcher != NULL)
24c79950
TT
2733 {
2734 struct cleanup *cleanup;
2735 htab_t visited_found, visited_not_found;
2736
2737 visited_found = htab_create_alloc (10,
2738 htab_hash_pointer, htab_eq_pointer,
2739 NULL, xcalloc, xfree);
2740 cleanup = make_cleanup_htab_delete (visited_found);
2741 visited_not_found = htab_create_alloc (10,
2742 htab_hash_pointer, htab_eq_pointer,
2743 NULL, xcalloc, xfree);
2744 make_cleanup_htab_delete (visited_not_found);
2745
2746 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2747 + dwarf2_per_objfile->n_type_units); ++i)
2748 {
2749 int j;
2750 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2751 struct quick_file_names *file_data;
2752 void **slot;
7b08b9eb 2753
24c79950 2754 per_cu->v.quick->mark = 0;
3d7bb9d9 2755
24c79950
TT
2756 /* We only need to look at symtabs not already expanded. */
2757 if (per_cu->v.quick->symtab)
2758 continue;
7b08b9eb 2759
24c79950
TT
2760 file_data = dw2_get_file_names (objfile, per_cu);
2761 if (file_data == NULL)
2762 continue;
7b08b9eb 2763
24c79950
TT
2764 if (htab_find (visited_not_found, file_data) != NULL)
2765 continue;
2766 else if (htab_find (visited_found, file_data) != NULL)
2767 {
2768 per_cu->v.quick->mark = 1;
2769 continue;
2770 }
2771
2772 for (j = 0; j < file_data->num_file_names; ++j)
2773 {
2774 if (file_matcher (file_data->file_names[j], data))
2775 {
2776 per_cu->v.quick->mark = 1;
2777 break;
2778 }
2779 }
2780
2781 slot = htab_find_slot (per_cu->v.quick->mark
2782 ? visited_found
2783 : visited_not_found,
2784 file_data, INSERT);
2785 *slot = file_data;
2786 }
2787
2788 do_cleanups (cleanup);
2789 }
9291a0cd 2790
3876f04e 2791 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2792 {
2793 offset_type idx = 2 * iter;
2794 const char *name;
2795 offset_type *vec, vec_len, vec_idx;
2796
3876f04e 2797 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2798 continue;
2799
3876f04e 2800 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2801
e078317b 2802 if (! (*name_matcher) (name, data))
9291a0cd
TT
2803 continue;
2804
2805 /* The name was matched, now expand corresponding CUs that were
2806 marked. */
4b5246aa 2807 vec = (offset_type *) (index->constant_pool
3876f04e 2808 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2809 vec_len = MAYBE_SWAP (vec[0]);
2810 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2811 {
e254ef6a 2812 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2813
e254ef6a 2814 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2815 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2816 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2817 }
2818 }
2819}
2820
2821static struct symtab *
2822dw2_find_pc_sect_symtab (struct objfile *objfile,
2823 struct minimal_symbol *msymbol,
2824 CORE_ADDR pc,
2825 struct obj_section *section,
2826 int warn_if_readin)
2827{
2828 struct dwarf2_per_cu_data *data;
2829
2830 dw2_setup (objfile);
2831
2832 if (!objfile->psymtabs_addrmap)
2833 return NULL;
2834
2835 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2836 if (!data)
2837 return NULL;
2838
2839 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2840 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2841 paddress (get_objfile_arch (objfile), pc));
2842
a0f42c21 2843 return dw2_instantiate_symtab (data);
9291a0cd
TT
2844}
2845
9291a0cd 2846static void
44b13c5a 2847dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2848 void *data, int need_fullname)
9291a0cd
TT
2849{
2850 int i;
24c79950
TT
2851 struct cleanup *cleanup;
2852 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2853 NULL, xcalloc, xfree);
9291a0cd 2854
24c79950 2855 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2856 dw2_setup (objfile);
ae2de4f8 2857
24c79950
TT
2858 /* We can ignore file names coming from already-expanded CUs. */
2859 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2860 + dwarf2_per_objfile->n_type_units); ++i)
2861 {
2862 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2863
2864 if (per_cu->v.quick->symtab)
2865 {
2866 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2867 INSERT);
2868
2869 *slot = per_cu->v.quick->file_names;
2870 }
2871 }
2872
1fd400ff 2873 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2874 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2875 {
2876 int j;
e254ef6a 2877 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2878 struct quick_file_names *file_data;
24c79950 2879 void **slot;
9291a0cd 2880
3d7bb9d9 2881 /* We only need to look at symtabs not already expanded. */
e254ef6a 2882 if (per_cu->v.quick->symtab)
9291a0cd
TT
2883 continue;
2884
7b9f3c50
DE
2885 file_data = dw2_get_file_names (objfile, per_cu);
2886 if (file_data == NULL)
9291a0cd
TT
2887 continue;
2888
24c79950
TT
2889 slot = htab_find_slot (visited, file_data, INSERT);
2890 if (*slot)
2891 {
2892 /* Already visited. */
2893 continue;
2894 }
2895 *slot = file_data;
2896
7b9f3c50 2897 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2898 {
74e2f255
DE
2899 const char *this_real_name;
2900
2901 if (need_fullname)
2902 this_real_name = dw2_get_real_path (objfile, file_data, j);
2903 else
2904 this_real_name = NULL;
7b9f3c50 2905 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2906 }
2907 }
24c79950
TT
2908
2909 do_cleanups (cleanup);
9291a0cd
TT
2910}
2911
2912static int
2913dw2_has_symbols (struct objfile *objfile)
2914{
2915 return 1;
2916}
2917
2918const struct quick_symbol_functions dwarf2_gdb_index_functions =
2919{
2920 dw2_has_symbols,
2921 dw2_find_last_source_symtab,
2922 dw2_forget_cached_source_info,
f8eba3c6 2923 dw2_map_symtabs_matching_filename,
9291a0cd 2924 dw2_lookup_symbol,
774b6a14 2925 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2926 dw2_print_stats,
2927 dw2_dump,
2928 dw2_relocate,
2929 dw2_expand_symtabs_for_function,
2930 dw2_expand_all_symtabs,
2931 dw2_expand_symtabs_with_filename,
2932 dw2_find_symbol_file,
40658b94 2933 dw2_map_matching_symbols,
9291a0cd
TT
2934 dw2_expand_symtabs_matching,
2935 dw2_find_pc_sect_symtab,
9291a0cd
TT
2936 dw2_map_symbol_filenames
2937};
2938
2939/* Initialize for reading DWARF for this objfile. Return 0 if this
2940 file will use psymtabs, or 1 if using the GNU index. */
2941
2942int
2943dwarf2_initialize_objfile (struct objfile *objfile)
2944{
2945 /* If we're about to read full symbols, don't bother with the
2946 indices. In this case we also don't care if some other debug
2947 format is making psymtabs, because they are all about to be
2948 expanded anyway. */
2949 if ((objfile->flags & OBJF_READNOW))
2950 {
2951 int i;
2952
2953 dwarf2_per_objfile->using_index = 1;
2954 create_all_comp_units (objfile);
0e50663e 2955 create_all_type_units (objfile);
7b9f3c50
DE
2956 dwarf2_per_objfile->quick_file_names_table =
2957 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2958
1fd400ff 2959 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2960 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2961 {
e254ef6a 2962 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2963
e254ef6a
DE
2964 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2966 }
2967
2968 /* Return 1 so that gdb sees the "quick" functions. However,
2969 these functions will be no-ops because we will have expanded
2970 all symtabs. */
2971 return 1;
2972 }
2973
2974 if (dwarf2_read_index (objfile))
2975 return 1;
2976
9291a0cd
TT
2977 return 0;
2978}
2979
2980\f
2981
dce234bc
PP
2982/* Build a partial symbol table. */
2983
2984void
f29dff0a 2985dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2986{
f29dff0a 2987 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2988 {
2989 init_psymbol_list (objfile, 1024);
2990 }
2991
d146bf1e 2992 dwarf2_build_psymtabs_hard (objfile);
c906108c 2993}
c906108c 2994
45452591
DE
2995/* Return TRUE if OFFSET is within CU_HEADER. */
2996
2997static inline int
b64f50a1 2998offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 2999{
b64f50a1
JK
3000 sect_offset bottom = { cu_header->offset.sect_off };
3001 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3002 + cu_header->initial_length_size) };
9a619af0 3003
b64f50a1 3004 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3005}
3006
93311388
DE
3007/* Read in the comp unit header information from the debug_info at info_ptr.
3008 NOTE: This leaves members offset, first_die_offset to be filled in
3009 by the caller. */
107d2387 3010
fe1b8b76 3011static gdb_byte *
107d2387 3012read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3013 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3014{
3015 int signed_addr;
891d2f0b 3016 unsigned int bytes_read;
c764a876
DE
3017
3018 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3019 cu_header->initial_length_size = bytes_read;
3020 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3021 info_ptr += bytes_read;
107d2387
AC
3022 cu_header->version = read_2_bytes (abfd, info_ptr);
3023 info_ptr += 2;
b64f50a1
JK
3024 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3025 &bytes_read);
613e1657 3026 info_ptr += bytes_read;
107d2387
AC
3027 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3028 info_ptr += 1;
3029 signed_addr = bfd_get_sign_extend_vma (abfd);
3030 if (signed_addr < 0)
8e65ff28 3031 internal_error (__FILE__, __LINE__,
e2e0b3e5 3032 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3033 cu_header->signed_addr_p = signed_addr;
c764a876 3034
107d2387
AC
3035 return info_ptr;
3036}
3037
9ff913ba
DE
3038/* Subroutine of read_and_check_comp_unit_head and
3039 read_and_check_type_unit_head to simplify them.
3040 Perform various error checking on the header. */
3041
3042static void
3043error_check_comp_unit_head (struct comp_unit_head *header,
3044 struct dwarf2_section_info *section)
3045{
3046 bfd *abfd = section->asection->owner;
3047 const char *filename = bfd_get_filename (abfd);
3048
3049 if (header->version != 2 && header->version != 3 && header->version != 4)
3050 error (_("Dwarf Error: wrong version in compilation unit header "
3051 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3052 filename);
3053
b64f50a1 3054 if (header->abbrev_offset.sect_off
9ff913ba
DE
3055 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3056 &dwarf2_per_objfile->abbrev))
3057 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3058 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3059 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3060 filename);
3061
3062 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3063 avoid potential 32-bit overflow. */
b64f50a1 3064 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3065 + header->length + header->initial_length_size)
3066 > section->size)
3067 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3068 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3069 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3070 filename);
3071}
3072
3073/* Read in a CU/TU header and perform some basic error checking.
3074 The contents of the header are stored in HEADER.
3075 The result is a pointer to the start of the first DIE. */
adabb602 3076
fe1b8b76 3077static gdb_byte *
9ff913ba
DE
3078read_and_check_comp_unit_head (struct comp_unit_head *header,
3079 struct dwarf2_section_info *section,
3080 gdb_byte *info_ptr,
3081 int is_debug_types_section)
72bf9492 3082{
fe1b8b76 3083 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3084 bfd *abfd = section->asection->owner;
72bf9492 3085
b64f50a1 3086 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3087
72bf9492
DJ
3088 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3089
460c1c54
CC
3090 /* If we're reading a type unit, skip over the signature and
3091 type_offset fields. */
b0df02fd 3092 if (is_debug_types_section)
460c1c54
CC
3093 info_ptr += 8 /*signature*/ + header->offset_size;
3094
b64f50a1 3095 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3096
9ff913ba 3097 error_check_comp_unit_head (header, section);
72bf9492
DJ
3098
3099 return info_ptr;
3100}
3101
348e048f
DE
3102/* Read in the types comp unit header information from .debug_types entry at
3103 types_ptr. The result is a pointer to one past the end of the header. */
3104
3105static gdb_byte *
9ff913ba
DE
3106read_and_check_type_unit_head (struct comp_unit_head *header,
3107 struct dwarf2_section_info *section,
3108 gdb_byte *info_ptr,
b64f50a1 3109 ULONGEST *signature, cu_offset *type_offset)
348e048f 3110{
9ff913ba
DE
3111 gdb_byte *beg_of_comp_unit = info_ptr;
3112 bfd *abfd = section->asection->owner;
348e048f 3113
b64f50a1 3114 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3115
9ff913ba 3116 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3117
9ff913ba
DE
3118 /* If we're reading a type unit, skip over the signature and
3119 type_offset fields. */
3120 if (signature != NULL)
3121 *signature = read_8_bytes (abfd, info_ptr);
3122 info_ptr += 8;
3123 if (type_offset != NULL)
b64f50a1 3124 type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
9ff913ba
DE
3125 info_ptr += header->offset_size;
3126
b64f50a1 3127 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3128
9ff913ba
DE
3129 error_check_comp_unit_head (header, section);
3130
3131 return info_ptr;
348e048f
DE
3132}
3133
aaa75496
JB
3134/* Allocate a new partial symtab for file named NAME and mark this new
3135 partial symtab as being an include of PST. */
3136
3137static void
3138dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3139 struct objfile *objfile)
3140{
3141 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3142
3143 subpst->section_offsets = pst->section_offsets;
3144 subpst->textlow = 0;
3145 subpst->texthigh = 0;
3146
3147 subpst->dependencies = (struct partial_symtab **)
3148 obstack_alloc (&objfile->objfile_obstack,
3149 sizeof (struct partial_symtab *));
3150 subpst->dependencies[0] = pst;
3151 subpst->number_of_dependencies = 1;
3152
3153 subpst->globals_offset = 0;
3154 subpst->n_global_syms = 0;
3155 subpst->statics_offset = 0;
3156 subpst->n_static_syms = 0;
3157 subpst->symtab = NULL;
3158 subpst->read_symtab = pst->read_symtab;
3159 subpst->readin = 0;
3160
3161 /* No private part is necessary for include psymtabs. This property
3162 can be used to differentiate between such include psymtabs and
10b3939b 3163 the regular ones. */
58a9656e 3164 subpst->read_symtab_private = NULL;
aaa75496
JB
3165}
3166
3167/* Read the Line Number Program data and extract the list of files
3168 included by the source file represented by PST. Build an include
d85a05f0 3169 partial symtab for each of these included files. */
aaa75496
JB
3170
3171static void
3172dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3173 struct die_info *die,
aaa75496
JB
3174 struct partial_symtab *pst)
3175{
3176 struct objfile *objfile = cu->objfile;
3177 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3178 struct line_header *lh = NULL;
3179 struct attribute *attr;
aaa75496 3180
d85a05f0
DJ
3181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3182 if (attr)
3183 {
3184 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3185
d85a05f0
DJ
3186 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3187 }
aaa75496
JB
3188 if (lh == NULL)
3189 return; /* No linetable, so no includes. */
3190
c6da4cef 3191 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3192 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3193
3194 free_line_header (lh);
3195}
3196
348e048f 3197static hashval_t
52dc124a 3198hash_signatured_type (const void *item)
348e048f 3199{
52dc124a 3200 const struct signatured_type *sig_type = item;
9a619af0 3201
348e048f 3202 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3203 return sig_type->signature;
348e048f
DE
3204}
3205
3206static int
52dc124a 3207eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3208{
3209 const struct signatured_type *lhs = item_lhs;
3210 const struct signatured_type *rhs = item_rhs;
9a619af0 3211
348e048f
DE
3212 return lhs->signature == rhs->signature;
3213}
3214
1fd400ff
TT
3215/* Allocate a hash table for signatured types. */
3216
3217static htab_t
673bfd45 3218allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3219{
3220 return htab_create_alloc_ex (41,
52dc124a
DE
3221 hash_signatured_type,
3222 eq_signatured_type,
1fd400ff
TT
3223 NULL,
3224 &objfile->objfile_obstack,
3225 hashtab_obstack_allocate,
3226 dummy_obstack_deallocate);
3227}
3228
d467dd73 3229/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3230
3231static int
d467dd73 3232add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3233{
3234 struct signatured_type *sigt = *slot;
3235 struct dwarf2_per_cu_data ***datap = datum;
3236
3237 **datap = &sigt->per_cu;
3238 ++*datap;
3239
3240 return 1;
3241}
3242
d467dd73 3243/* Create the hash table of all entries in the .debug_types section(s).
0e50663e
DE
3244 The result is zero if there are no .debug_types sections,
3245 otherwise non-zero. */
348e048f
DE
3246
3247static int
0e50663e 3248create_all_type_units (struct objfile *objfile)
348e048f 3249{
8b70b953 3250 htab_t types_htab = NULL;
1fd400ff 3251 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3252 int ix;
3253 struct dwarf2_section_info *section;
348e048f 3254
8b70b953 3255 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3256 {
3257 dwarf2_per_objfile->signatured_types = NULL;
3258 return 0;
3259 }
3260
8b70b953
TT
3261 for (ix = 0;
3262 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3263 ix, section);
3264 ++ix)
3265 {
3266 gdb_byte *info_ptr, *end_ptr;
348e048f 3267
8b70b953
TT
3268 dwarf2_read_section (objfile, section);
3269 info_ptr = section->buffer;
348e048f 3270
8b70b953
TT
3271 if (info_ptr == NULL)
3272 continue;
348e048f 3273
8b70b953
TT
3274 if (types_htab == NULL)
3275 types_htab = allocate_signatured_type_table (objfile);
348e048f 3276
8b70b953
TT
3277 if (dwarf2_die_debug)
3278 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3279
3280 end_ptr = info_ptr + section->size;
3281 while (info_ptr < end_ptr)
3282 {
b64f50a1
JK
3283 sect_offset offset;
3284 cu_offset type_offset;
8b70b953 3285 ULONGEST signature;
52dc124a 3286 struct signatured_type *sig_type;
8b70b953
TT
3287 void **slot;
3288 gdb_byte *ptr = info_ptr;
9ff913ba 3289 struct comp_unit_head header;
348e048f 3290
b64f50a1 3291 offset.sect_off = ptr - section->buffer;
348e048f 3292
8b70b953 3293 /* We need to read the type's signature in order to build the hash
9ff913ba 3294 table, but we don't need anything else just yet. */
348e048f 3295
9ff913ba
DE
3296 ptr = read_and_check_type_unit_head (&header, section, ptr,
3297 &signature, &type_offset);
6caca83c
CC
3298
3299 /* Skip dummy type units. */
3300 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3301 {
9ff913ba 3302 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3303 continue;
3304 }
8b70b953 3305
52dc124a
DE
3306 sig_type = obstack_alloc (&objfile->objfile_obstack, sizeof (*sig_type));
3307 memset (sig_type, 0, sizeof (*sig_type));
3308 sig_type->signature = signature;
3309 sig_type->type_offset = type_offset;
3310 sig_type->per_cu.objfile = objfile;
3311 sig_type->per_cu.debug_types_section = section;
3312 sig_type->per_cu.offset = offset;
8b70b953 3313
52dc124a 3314 slot = htab_find_slot (types_htab, sig_type, INSERT);
8b70b953
TT
3315 gdb_assert (slot != NULL);
3316 if (*slot != NULL)
3317 {
3318 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3319
8b70b953
TT
3320 complaint (&symfile_complaints,
3321 _("debug type entry at offset 0x%x is duplicate to the "
3322 "entry at offset 0x%x, signature 0x%s"),
b64f50a1 3323 offset.sect_off, dup_sig->per_cu.offset.sect_off,
8b70b953
TT
3324 phex (signature, sizeof (signature)));
3325 gdb_assert (signature == dup_sig->signature);
3326 }
52dc124a 3327 *slot = sig_type;
348e048f 3328
8b70b953
TT
3329 if (dwarf2_die_debug)
3330 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3331 offset.sect_off,
3332 phex (signature, sizeof (signature)));
348e048f 3333
9ff913ba 3334 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3335 }
348e048f
DE
3336 }
3337
3338 dwarf2_per_objfile->signatured_types = types_htab;
3339
d467dd73
DE
3340 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3341 dwarf2_per_objfile->all_type_units
1fd400ff 3342 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3343 dwarf2_per_objfile->n_type_units
1fd400ff 3344 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3345 iter = &dwarf2_per_objfile->all_type_units[0];
3346 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3347 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3348 == dwarf2_per_objfile->n_type_units);
1fd400ff 3349
348e048f
DE
3350 return 1;
3351}
3352
380bca97 3353/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3354 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3355
3356static struct signatured_type *
e319fa28 3357lookup_signatured_type (ULONGEST sig)
348e048f
DE
3358{
3359 struct signatured_type find_entry, *entry;
3360
3361 if (dwarf2_per_objfile->signatured_types == NULL)
3362 {
3363 complaint (&symfile_complaints,
55f1336d 3364 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3365 return NULL;
348e048f
DE
3366 }
3367
3368 find_entry.signature = sig;
3369 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3370 return entry;
3371}
3372
d85a05f0
DJ
3373/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3374
3375static void
3376init_cu_die_reader (struct die_reader_specs *reader,
3377 struct dwarf2_cu *cu)
3378{
3379 reader->abfd = cu->objfile->obfd;
3380 reader->cu = cu;
b0df02fd 3381 if (cu->per_cu->debug_types_section)
be391dca 3382 {
b0df02fd
DE
3383 gdb_assert (cu->per_cu->debug_types_section->readin);
3384 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3385 }
d85a05f0 3386 else
be391dca
TT
3387 {
3388 gdb_assert (dwarf2_per_objfile->info.readin);
3389 reader->buffer = dwarf2_per_objfile->info.buffer;
3390 }
d85a05f0
DJ
3391}
3392
3393/* Find the base address of the compilation unit for range lists and
3394 location lists. It will normally be specified by DW_AT_low_pc.
3395 In DWARF-3 draft 4, the base address could be overridden by
3396 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3397 compilation units with discontinuous ranges. */
3398
3399static void
3400dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3401{
3402 struct attribute *attr;
3403
3404 cu->base_known = 0;
3405 cu->base_address = 0;
3406
3407 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3408 if (attr)
3409 {
3410 cu->base_address = DW_ADDR (attr);
3411 cu->base_known = 1;
3412 }
3413 else
3414 {
3415 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3416 if (attr)
3417 {
3418 cu->base_address = DW_ADDR (attr);
3419 cu->base_known = 1;
3420 }
3421 }
3422}
3423
348e048f
DE
3424/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3425 to combine the common parts.
21b2bd31
DE
3426 Process compilation unit THIS_CU for a psymtab.
3427 SECTION is the section the CU/TU comes from,
3428 either .debug_info or .debug_types. */
aaa75496 3429
70221824 3430static void
a0f42c21 3431process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3432 struct dwarf2_section_info *section,
3433 int is_debug_types_section)
c906108c 3434{
a0f42c21 3435 struct objfile *objfile = this_cu->objfile;
c906108c 3436 bfd *abfd = objfile->obfd;
21b2bd31 3437 gdb_byte *buffer = section->buffer;
b64f50a1 3438 gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
21b2bd31 3439 unsigned int buffer_size = section->size;
93311388 3440 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3441 struct die_info *comp_unit_die;
c906108c 3442 struct partial_symtab *pst;
5734ee8b 3443 CORE_ADDR baseaddr;
93311388
DE
3444 struct cleanup *back_to_inner;
3445 struct dwarf2_cu cu;
d85a05f0
DJ
3446 int has_children, has_pc_info;
3447 struct attribute *attr;
d85a05f0
DJ
3448 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3449 struct die_reader_specs reader_specs;
3e2a0cee 3450 const char *filename;
c906108c 3451
23745b47
DE
3452 /* If this compilation unit was already read in, free the
3453 cached copy in order to read it in again. This is
3454 necessary because we skipped some symbols when we first
3455 read in the compilation unit (see load_partial_dies).
3456 This problem could be avoided, but the benefit is
3457 unclear. */
3458 if (this_cu->cu != NULL)
3459 free_one_cached_comp_unit (this_cu->cu);
3460
3461 /* Note that this is a pointer to our stack frame, being
3462 added to a global data structure. It will be cleaned up
3463 in free_stack_comp_unit when we finish with this
3464 compilation unit. */
3465 init_one_comp_unit (&cu, this_cu);
93311388 3466 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3467
9ff913ba
DE
3468 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3469 is_debug_types_section);
10b3939b 3470
6caca83c
CC
3471 /* Skip dummy compilation units. */
3472 if (info_ptr >= buffer + buffer_size
3473 || peek_abbrev_code (abfd, info_ptr) == 0)
3474 {
6caca83c 3475 do_cleanups (back_to_inner);
21b2bd31 3476 return;
6caca83c
CC
3477 }
3478
93311388 3479 cu.list_in_scope = &file_symbols;
af703f96 3480
93311388 3481 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3482 dwarf2_read_abbrevs (&cu);
93311388 3483 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3484
93311388 3485 /* Read the compilation unit die. */
d85a05f0
DJ
3486 init_cu_die_reader (&reader_specs, &cu);
3487 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3488 &has_children);
93311388 3489
21b2bd31 3490 if (is_debug_types_section)
348e048f 3491 {
b3c8eb43 3492 /* LENGTH has not been set yet for type units. */
b64f50a1 3493 gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
348e048f
DE
3494 this_cu->length = cu.header.length + cu.header.initial_length_size;
3495 }
d85a05f0 3496 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3497 {
93311388 3498 do_cleanups (back_to_inner);
21b2bd31 3499 return;
93311388 3500 }
72bf9492 3501
9816fde3 3502 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3503
93311388 3504 /* Allocate a new partial symbol table structure. */
d85a05f0 3505 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3506 if (attr == NULL || !DW_STRING (attr))
3507 filename = "";
3508 else
3509 filename = DW_STRING (attr);
93311388 3510 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3511 filename,
93311388
DE
3512 /* TEXTLOW and TEXTHIGH are set below. */
3513 0,
3514 objfile->global_psymbols.next,
3515 objfile->static_psymbols.next);
9750bca9 3516 pst->psymtabs_addrmap_supported = 1;
72bf9492 3517
d85a05f0
DJ
3518 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3519 if (attr != NULL)
3520 pst->dirname = DW_STRING (attr);
72bf9492 3521
e38df1d0 3522 pst->read_symtab_private = this_cu;
72bf9492 3523
93311388 3524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3525
0963b4bd 3526 /* Store the function that reads in the rest of the symbol table. */
93311388 3527 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3528
9291a0cd 3529 this_cu->v.psymtab = pst;
c906108c 3530
d85a05f0
DJ
3531 dwarf2_find_base_address (comp_unit_die, &cu);
3532
93311388
DE
3533 /* Possibly set the default values of LOWPC and HIGHPC from
3534 `DW_AT_ranges'. */
d85a05f0
DJ
3535 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3536 &best_highpc, &cu, pst);
3537 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3538 /* Store the contiguous range if it is not empty; it can be empty for
3539 CUs with no code. */
3540 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3541 best_lowpc + baseaddr,
3542 best_highpc + baseaddr - 1, pst);
93311388
DE
3543
3544 /* Check if comp unit has_children.
3545 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3546 If not, there's no more debug_info for this comp unit. */
d85a05f0 3547 if (has_children)
93311388
DE
3548 {
3549 struct partial_die_info *first_die;
3550 CORE_ADDR lowpc, highpc;
31ffec48 3551
93311388
DE
3552 lowpc = ((CORE_ADDR) -1);
3553 highpc = ((CORE_ADDR) 0);
c906108c 3554
93311388 3555 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3556
93311388 3557 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3558 ! has_pc_info, &cu);
57c22c6c 3559
93311388
DE
3560 /* If we didn't find a lowpc, set it to highpc to avoid
3561 complaints from `maint check'. */
3562 if (lowpc == ((CORE_ADDR) -1))
3563 lowpc = highpc;
10b3939b 3564
93311388
DE
3565 /* If the compilation unit didn't have an explicit address range,
3566 then use the information extracted from its child dies. */
d85a05f0 3567 if (! has_pc_info)
93311388 3568 {
d85a05f0
DJ
3569 best_lowpc = lowpc;
3570 best_highpc = highpc;
93311388
DE
3571 }
3572 }
d85a05f0
DJ
3573 pst->textlow = best_lowpc + baseaddr;
3574 pst->texthigh = best_highpc + baseaddr;
c906108c 3575
93311388
DE
3576 pst->n_global_syms = objfile->global_psymbols.next -
3577 (objfile->global_psymbols.list + pst->globals_offset);
3578 pst->n_static_syms = objfile->static_psymbols.next -
3579 (objfile->static_psymbols.list + pst->statics_offset);
3580 sort_pst_symbols (pst);
c906108c 3581
21b2bd31 3582 if (is_debug_types_section)
348e048f
DE
3583 {
3584 /* It's not clear we want to do anything with stmt lists here.
3585 Waiting to see what gcc ultimately does. */
3586 }
d85a05f0 3587 else
93311388
DE
3588 {
3589 /* Get the list of files included in the current compilation unit,
3590 and build a psymtab for each of them. */
d85a05f0 3591 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3592 }
ae038cb0 3593
93311388 3594 do_cleanups (back_to_inner);
93311388 3595}
ff013f42 3596
348e048f
DE
3597/* Traversal function for htab_traverse_noresize.
3598 Process one .debug_types comp-unit. */
3599
3600static int
3601process_type_comp_unit (void **slot, void *info)
3602{
3603 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3604 struct dwarf2_per_cu_data *this_cu;
3605
a0f42c21 3606 gdb_assert (info == NULL);
348e048f 3607 this_cu = &entry->per_cu;
348e048f 3608
b0df02fd 3609 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3610 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3611
3612 return 1;
3613}
3614
3615/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3616 Build partial symbol tables for the .debug_types comp-units. */
3617
3618static void
3619build_type_psymtabs (struct objfile *objfile)
3620{
0e50663e 3621 if (! create_all_type_units (objfile))
348e048f
DE
3622 return;
3623
3624 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3625 process_type_comp_unit, NULL);
348e048f
DE
3626}
3627
60606b2c
TT
3628/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3629
3630static void
3631psymtabs_addrmap_cleanup (void *o)
3632{
3633 struct objfile *objfile = o;
ec61707d 3634
60606b2c
TT
3635 objfile->psymtabs_addrmap = NULL;
3636}
3637
93311388
DE
3638/* Build the partial symbol table by doing a quick pass through the
3639 .debug_info and .debug_abbrev sections. */
72bf9492 3640
93311388 3641static void
c67a9c90 3642dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3643{
60606b2c
TT
3644 struct cleanup *back_to, *addrmap_cleanup;
3645 struct obstack temp_obstack;
21b2bd31 3646 int i;
93311388 3647
98bfdba5
PA
3648 dwarf2_per_objfile->reading_partial_symbols = 1;
3649
be391dca 3650 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3651
93311388
DE
3652 /* Any cached compilation units will be linked by the per-objfile
3653 read_in_chain. Make sure to free them when we're done. */
3654 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3655
348e048f
DE
3656 build_type_psymtabs (objfile);
3657
93311388 3658 create_all_comp_units (objfile);
c906108c 3659
60606b2c
TT
3660 /* Create a temporary address map on a temporary obstack. We later
3661 copy this to the final obstack. */
3662 obstack_init (&temp_obstack);
3663 make_cleanup_obstack_free (&temp_obstack);
3664 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3665 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3666
21b2bd31 3667 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3668 {
21b2bd31 3669 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3670
21b2bd31 3671 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3672 }
ff013f42
JK
3673
3674 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3675 &objfile->objfile_obstack);
60606b2c 3676 discard_cleanups (addrmap_cleanup);
ff013f42 3677
ae038cb0
DJ
3678 do_cleanups (back_to);
3679}
3680
93311388 3681/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3682
3683static void
a0f42c21 3684load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3685{
a0f42c21 3686 struct objfile *objfile = this_cu->objfile;
ae038cb0 3687 bfd *abfd = objfile->obfd;
adabb602 3688 gdb_byte *info_ptr;
d85a05f0 3689 struct die_info *comp_unit_die;
ae038cb0 3690 struct dwarf2_cu *cu;
1d9ec526 3691 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3692 int has_children;
3693 struct die_reader_specs reader_specs;
98bfdba5 3694 int read_cu = 0;
9ff913ba 3695 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3696
b0df02fd 3697 gdb_assert (! this_cu->debug_types_section);
348e048f 3698
9ff913ba 3699 gdb_assert (section->readin);
b64f50a1 3700 info_ptr = section->buffer + this_cu->offset.sect_off;
ae038cb0 3701
98bfdba5
PA
3702 if (this_cu->cu == NULL)
3703 {
9816fde3 3704 cu = xmalloc (sizeof (*cu));
23745b47 3705 init_one_comp_unit (cu, this_cu);
ae038cb0 3706
98bfdba5 3707 read_cu = 1;
ae038cb0 3708
98bfdba5 3709 /* If an error occurs while loading, release our storage. */
68dc6402 3710 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3711
9ff913ba
DE
3712 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3713 0);
ae038cb0 3714
6caca83c 3715 /* Skip dummy compilation units. */
9ff913ba 3716 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3717 || peek_abbrev_code (abfd, info_ptr) == 0)
3718 {
3719 do_cleanups (free_cu_cleanup);
3720 return;
3721 }
98bfdba5
PA
3722 }
3723 else
3724 {
3725 cu = this_cu->cu;
b64f50a1 3726 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 3727 }
ae038cb0
DJ
3728
3729 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3730 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3731 dwarf2_read_abbrevs (cu);
98bfdba5 3732 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3733
3734 /* Read the compilation unit die. */
d85a05f0
DJ
3735 init_cu_die_reader (&reader_specs, cu);
3736 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3737 &has_children);
ae038cb0 3738
9816fde3 3739 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3740
ae038cb0
DJ
3741 /* Check if comp unit has_children.
3742 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3743 If not, there's no more debug_info for this comp unit. */
d85a05f0 3744 if (has_children)
9ff913ba 3745 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3746
98bfdba5
PA
3747 do_cleanups (free_abbrevs_cleanup);
3748
3749 if (read_cu)
3750 {
3751 /* We've successfully allocated this compilation unit. Let our
3752 caller clean it up when finished with it. */
3753 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
3754
3755 /* Link this CU into read_in_chain. */
3756 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3757 dwarf2_per_objfile->read_in_chain = this_cu;
98bfdba5 3758 }
ae038cb0
DJ
3759}
3760
9cdd5dbd
DE
3761/* Create a list of all compilation units in OBJFILE.
3762 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3763
3764static void
3765create_all_comp_units (struct objfile *objfile)
3766{
3767 int n_allocated;
3768 int n_comp_units;
3769 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3770 gdb_byte *info_ptr;
3771
3772 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3773 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3774
3775 n_comp_units = 0;
3776 n_allocated = 10;
3777 all_comp_units = xmalloc (n_allocated
3778 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3779
3e43a32a
MS
3780 while (info_ptr < dwarf2_per_objfile->info.buffer
3781 + dwarf2_per_objfile->info.size)
ae038cb0 3782 {
c764a876 3783 unsigned int length, initial_length_size;
ae038cb0 3784 struct dwarf2_per_cu_data *this_cu;
b64f50a1 3785 sect_offset offset;
ae038cb0 3786
b64f50a1 3787 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3788
3789 /* Read just enough information to find out where the next
3790 compilation unit is. */
c764a876
DE
3791 length = read_initial_length (objfile->obfd, info_ptr,
3792 &initial_length_size);
ae038cb0
DJ
3793
3794 /* Save the compilation unit for later lookup. */
3795 this_cu = obstack_alloc (&objfile->objfile_obstack,
3796 sizeof (struct dwarf2_per_cu_data));
3797 memset (this_cu, 0, sizeof (*this_cu));
3798 this_cu->offset = offset;
c764a876 3799 this_cu->length = length + initial_length_size;
9291a0cd 3800 this_cu->objfile = objfile;
ae038cb0
DJ
3801
3802 if (n_comp_units == n_allocated)
3803 {
3804 n_allocated *= 2;
3805 all_comp_units = xrealloc (all_comp_units,
3806 n_allocated
3807 * sizeof (struct dwarf2_per_cu_data *));
3808 }
3809 all_comp_units[n_comp_units++] = this_cu;
3810
3811 info_ptr = info_ptr + this_cu->length;
3812 }
3813
3814 dwarf2_per_objfile->all_comp_units
3815 = obstack_alloc (&objfile->objfile_obstack,
3816 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3817 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3818 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3819 xfree (all_comp_units);
3820 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3821}
3822
5734ee8b
DJ
3823/* Process all loaded DIEs for compilation unit CU, starting at
3824 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3825 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3826 DW_AT_ranges). If NEED_PC is set, then this function will set
3827 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3828 and record the covered ranges in the addrmap. */
c906108c 3829
72bf9492
DJ
3830static void
3831scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3832 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3833{
72bf9492 3834 struct partial_die_info *pdi;
c906108c 3835
91c24f0a
DC
3836 /* Now, march along the PDI's, descending into ones which have
3837 interesting children but skipping the children of the other ones,
3838 until we reach the end of the compilation unit. */
c906108c 3839
72bf9492 3840 pdi = first_die;
91c24f0a 3841
72bf9492
DJ
3842 while (pdi != NULL)
3843 {
3844 fixup_partial_die (pdi, cu);
c906108c 3845
f55ee35c 3846 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3847 children, so we need to look at them. Ditto for anonymous
3848 enums. */
933c6fe4 3849
72bf9492 3850 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3851 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3852 {
72bf9492 3853 switch (pdi->tag)
c906108c
SS
3854 {
3855 case DW_TAG_subprogram:
5734ee8b 3856 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3857 break;
72929c62 3858 case DW_TAG_constant:
c906108c
SS
3859 case DW_TAG_variable:
3860 case DW_TAG_typedef:
91c24f0a 3861 case DW_TAG_union_type:
72bf9492 3862 if (!pdi->is_declaration)
63d06c5c 3863 {
72bf9492 3864 add_partial_symbol (pdi, cu);
63d06c5c
DC
3865 }
3866 break;
c906108c 3867 case DW_TAG_class_type:
680b30c7 3868 case DW_TAG_interface_type:
c906108c 3869 case DW_TAG_structure_type:
72bf9492 3870 if (!pdi->is_declaration)
c906108c 3871 {
72bf9492 3872 add_partial_symbol (pdi, cu);
c906108c
SS
3873 }
3874 break;
91c24f0a 3875 case DW_TAG_enumeration_type:
72bf9492
DJ
3876 if (!pdi->is_declaration)
3877 add_partial_enumeration (pdi, cu);
c906108c
SS
3878 break;
3879 case DW_TAG_base_type:
a02abb62 3880 case DW_TAG_subrange_type:
c906108c 3881 /* File scope base type definitions are added to the partial
c5aa993b 3882 symbol table. */
72bf9492 3883 add_partial_symbol (pdi, cu);
c906108c 3884 break;
d9fa45fe 3885 case DW_TAG_namespace:
5734ee8b 3886 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3887 break;
5d7cb8df
JK
3888 case DW_TAG_module:
3889 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3890 break;
c906108c
SS
3891 default:
3892 break;
3893 }
3894 }
3895
72bf9492
DJ
3896 /* If the die has a sibling, skip to the sibling. */
3897
3898 pdi = pdi->die_sibling;
3899 }
3900}
3901
3902/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3903
72bf9492 3904 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3905 name is concatenated with "::" and the partial DIE's name. For
3906 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3907 Enumerators are an exception; they use the scope of their parent
3908 enumeration type, i.e. the name of the enumeration type is not
3909 prepended to the enumerator.
91c24f0a 3910
72bf9492
DJ
3911 There are two complexities. One is DW_AT_specification; in this
3912 case "parent" means the parent of the target of the specification,
3913 instead of the direct parent of the DIE. The other is compilers
3914 which do not emit DW_TAG_namespace; in this case we try to guess
3915 the fully qualified name of structure types from their members'
3916 linkage names. This must be done using the DIE's children rather
3917 than the children of any DW_AT_specification target. We only need
3918 to do this for structures at the top level, i.e. if the target of
3919 any DW_AT_specification (if any; otherwise the DIE itself) does not
3920 have a parent. */
3921
3922/* Compute the scope prefix associated with PDI's parent, in
3923 compilation unit CU. The result will be allocated on CU's
3924 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3925 field. NULL is returned if no prefix is necessary. */
3926static char *
3927partial_die_parent_scope (struct partial_die_info *pdi,
3928 struct dwarf2_cu *cu)
3929{
3930 char *grandparent_scope;
3931 struct partial_die_info *parent, *real_pdi;
91c24f0a 3932
72bf9492
DJ
3933 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3934 then this means the parent of the specification DIE. */
3935
3936 real_pdi = pdi;
72bf9492 3937 while (real_pdi->has_specification)
10b3939b 3938 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3939
3940 parent = real_pdi->die_parent;
3941 if (parent == NULL)
3942 return NULL;
3943
3944 if (parent->scope_set)
3945 return parent->scope;
3946
3947 fixup_partial_die (parent, cu);
3948
10b3939b 3949 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3950
acebe513
UW
3951 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3952 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3953 Work around this problem here. */
3954 if (cu->language == language_cplus
6e70227d 3955 && parent->tag == DW_TAG_namespace
acebe513
UW
3956 && strcmp (parent->name, "::") == 0
3957 && grandparent_scope == NULL)
3958 {
3959 parent->scope = NULL;
3960 parent->scope_set = 1;
3961 return NULL;
3962 }
3963
9c6c53f7
SA
3964 if (pdi->tag == DW_TAG_enumerator)
3965 /* Enumerators should not get the name of the enumeration as a prefix. */
3966 parent->scope = grandparent_scope;
3967 else if (parent->tag == DW_TAG_namespace
f55ee35c 3968 || parent->tag == DW_TAG_module
72bf9492
DJ
3969 || parent->tag == DW_TAG_structure_type
3970 || parent->tag == DW_TAG_class_type
680b30c7 3971 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3972 || parent->tag == DW_TAG_union_type
3973 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3974 {
3975 if (grandparent_scope == NULL)
3976 parent->scope = parent->name;
3977 else
3e43a32a
MS
3978 parent->scope = typename_concat (&cu->comp_unit_obstack,
3979 grandparent_scope,
f55ee35c 3980 parent->name, 0, cu);
72bf9492 3981 }
72bf9492
DJ
3982 else
3983 {
3984 /* FIXME drow/2004-04-01: What should we be doing with
3985 function-local names? For partial symbols, we should probably be
3986 ignoring them. */
3987 complaint (&symfile_complaints,
e2e0b3e5 3988 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 3989 parent->tag, pdi->offset.sect_off);
72bf9492 3990 parent->scope = grandparent_scope;
c906108c
SS
3991 }
3992
72bf9492
DJ
3993 parent->scope_set = 1;
3994 return parent->scope;
3995}
3996
3997/* Return the fully scoped name associated with PDI, from compilation unit
3998 CU. The result will be allocated with malloc. */
4568ecf9 3999
72bf9492
DJ
4000static char *
4001partial_die_full_name (struct partial_die_info *pdi,
4002 struct dwarf2_cu *cu)
4003{
4004 char *parent_scope;
4005
98bfdba5
PA
4006 /* If this is a template instantiation, we can not work out the
4007 template arguments from partial DIEs. So, unfortunately, we have
4008 to go through the full DIEs. At least any work we do building
4009 types here will be reused if full symbols are loaded later. */
4010 if (pdi->has_template_arguments)
4011 {
4012 fixup_partial_die (pdi, cu);
4013
4014 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4015 {
4016 struct die_info *die;
4017 struct attribute attr;
4018 struct dwarf2_cu *ref_cu = cu;
4019
b64f50a1 4020 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4021 attr.name = 0;
4022 attr.form = DW_FORM_ref_addr;
4568ecf9 4023 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4024 die = follow_die_ref (NULL, &attr, &ref_cu);
4025
4026 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4027 }
4028 }
4029
72bf9492
DJ
4030 parent_scope = partial_die_parent_scope (pdi, cu);
4031 if (parent_scope == NULL)
4032 return NULL;
4033 else
f55ee35c 4034 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4035}
4036
4037static void
72bf9492 4038add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4039{
e7c27a73 4040 struct objfile *objfile = cu->objfile;
c906108c 4041 CORE_ADDR addr = 0;
decbce07 4042 char *actual_name = NULL;
e142c38c 4043 CORE_ADDR baseaddr;
72bf9492 4044 int built_actual_name = 0;
e142c38c
DJ
4045
4046 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4047
94af9270
KS
4048 actual_name = partial_die_full_name (pdi, cu);
4049 if (actual_name)
4050 built_actual_name = 1;
63d06c5c 4051
72bf9492
DJ
4052 if (actual_name == NULL)
4053 actual_name = pdi->name;
4054
c906108c
SS
4055 switch (pdi->tag)
4056 {
4057 case DW_TAG_subprogram:
2cfa0c8d 4058 if (pdi->is_external || cu->language == language_ada)
c906108c 4059 {
2cfa0c8d
JB
4060 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4061 of the global scope. But in Ada, we want to be able to access
4062 nested procedures globally. So all Ada subprograms are stored
4063 in the global scope. */
f47fb265 4064 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4065 mst_text, objfile); */
f47fb265
MS
4066 add_psymbol_to_list (actual_name, strlen (actual_name),
4067 built_actual_name,
4068 VAR_DOMAIN, LOC_BLOCK,
4069 &objfile->global_psymbols,
4070 0, pdi->lowpc + baseaddr,
4071 cu->language, objfile);
c906108c
SS
4072 }
4073 else
4074 {
f47fb265 4075 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4076 mst_file_text, objfile); */
f47fb265
MS
4077 add_psymbol_to_list (actual_name, strlen (actual_name),
4078 built_actual_name,
4079 VAR_DOMAIN, LOC_BLOCK,
4080 &objfile->static_psymbols,
4081 0, pdi->lowpc + baseaddr,
4082 cu->language, objfile);
c906108c
SS
4083 }
4084 break;
72929c62
JB
4085 case DW_TAG_constant:
4086 {
4087 struct psymbol_allocation_list *list;
4088
4089 if (pdi->is_external)
4090 list = &objfile->global_psymbols;
4091 else
4092 list = &objfile->static_psymbols;
f47fb265
MS
4093 add_psymbol_to_list (actual_name, strlen (actual_name),
4094 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4095 list, 0, 0, cu->language, objfile);
72929c62
JB
4096 }
4097 break;
c906108c 4098 case DW_TAG_variable:
caac4577
JG
4099 if (pdi->locdesc)
4100 addr = decode_locdesc (pdi->locdesc, cu);
4101
4102 if (pdi->locdesc
4103 && addr == 0
4104 && !dwarf2_per_objfile->has_section_at_zero)
4105 {
4106 /* A global or static variable may also have been stripped
4107 out by the linker if unused, in which case its address
4108 will be nullified; do not add such variables into partial
4109 symbol table then. */
4110 }
4111 else if (pdi->is_external)
c906108c
SS
4112 {
4113 /* Global Variable.
4114 Don't enter into the minimal symbol tables as there is
4115 a minimal symbol table entry from the ELF symbols already.
4116 Enter into partial symbol table if it has a location
4117 descriptor or a type.
4118 If the location descriptor is missing, new_symbol will create
4119 a LOC_UNRESOLVED symbol, the address of the variable will then
4120 be determined from the minimal symbol table whenever the variable
4121 is referenced.
4122 The address for the partial symbol table entry is not
4123 used by GDB, but it comes in handy for debugging partial symbol
4124 table building. */
4125
c906108c 4126 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4127 add_psymbol_to_list (actual_name, strlen (actual_name),
4128 built_actual_name,
4129 VAR_DOMAIN, LOC_STATIC,
4130 &objfile->global_psymbols,
4131 0, addr + baseaddr,
4132 cu->language, objfile);
c906108c
SS
4133 }
4134 else
4135 {
0963b4bd 4136 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4137 if (pdi->locdesc == NULL)
decbce07
MS
4138 {
4139 if (built_actual_name)
4140 xfree (actual_name);
4141 return;
4142 }
f47fb265 4143 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4144 mst_file_data, objfile); */
f47fb265
MS
4145 add_psymbol_to_list (actual_name, strlen (actual_name),
4146 built_actual_name,
4147 VAR_DOMAIN, LOC_STATIC,
4148 &objfile->static_psymbols,
4149 0, addr + baseaddr,
4150 cu->language, objfile);
c906108c
SS
4151 }
4152 break;
4153 case DW_TAG_typedef:
4154 case DW_TAG_base_type:
a02abb62 4155 case DW_TAG_subrange_type:
38d518c9 4156 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4157 built_actual_name,
176620f1 4158 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4159 &objfile->static_psymbols,
e142c38c 4160 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4161 break;
72bf9492
DJ
4162 case DW_TAG_namespace:
4163 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4164 built_actual_name,
72bf9492
DJ
4165 VAR_DOMAIN, LOC_TYPEDEF,
4166 &objfile->global_psymbols,
4167 0, (CORE_ADDR) 0, cu->language, objfile);
4168 break;
c906108c 4169 case DW_TAG_class_type:
680b30c7 4170 case DW_TAG_interface_type:
c906108c
SS
4171 case DW_TAG_structure_type:
4172 case DW_TAG_union_type:
4173 case DW_TAG_enumeration_type:
fa4028e9
JB
4174 /* Skip external references. The DWARF standard says in the section
4175 about "Structure, Union, and Class Type Entries": "An incomplete
4176 structure, union or class type is represented by a structure,
4177 union or class entry that does not have a byte size attribute
4178 and that has a DW_AT_declaration attribute." */
4179 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4180 {
4181 if (built_actual_name)
4182 xfree (actual_name);
4183 return;
4184 }
fa4028e9 4185
63d06c5c
DC
4186 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4187 static vs. global. */
38d518c9 4188 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4189 built_actual_name,
176620f1 4190 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4191 (cu->language == language_cplus
4192 || cu->language == language_java)
63d06c5c
DC
4193 ? &objfile->global_psymbols
4194 : &objfile->static_psymbols,
e142c38c 4195 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4196
c906108c
SS
4197 break;
4198 case DW_TAG_enumerator:
38d518c9 4199 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4200 built_actual_name,
176620f1 4201 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4202 (cu->language == language_cplus
4203 || cu->language == language_java)
f6fe98ef
DJ
4204 ? &objfile->global_psymbols
4205 : &objfile->static_psymbols,
e142c38c 4206 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4207 break;
4208 default:
4209 break;
4210 }
5c4e30ca 4211
72bf9492
DJ
4212 if (built_actual_name)
4213 xfree (actual_name);
c906108c
SS
4214}
4215
5c4e30ca
DC
4216/* Read a partial die corresponding to a namespace; also, add a symbol
4217 corresponding to that namespace to the symbol table. NAMESPACE is
4218 the name of the enclosing namespace. */
91c24f0a 4219
72bf9492
DJ
4220static void
4221add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4223 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4224{
72bf9492 4225 /* Add a symbol for the namespace. */
e7c27a73 4226
72bf9492 4227 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4228
4229 /* Now scan partial symbols in that namespace. */
4230
91c24f0a 4231 if (pdi->has_children)
5734ee8b 4232 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4233}
4234
5d7cb8df
JK
4235/* Read a partial die corresponding to a Fortran module. */
4236
4237static void
4238add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4239 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4240{
f55ee35c 4241 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4242
4243 if (pdi->has_children)
4244 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4245}
4246
bc30ff58
JB
4247/* Read a partial die corresponding to a subprogram and create a partial
4248 symbol for that subprogram. When the CU language allows it, this
4249 routine also defines a partial symbol for each nested subprogram
4250 that this subprogram contains.
6e70227d 4251
bc30ff58
JB
4252 DIE my also be a lexical block, in which case we simply search
4253 recursively for suprograms defined inside that lexical block.
4254 Again, this is only performed when the CU language allows this
4255 type of definitions. */
4256
4257static void
4258add_partial_subprogram (struct partial_die_info *pdi,
4259 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4260 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4261{
4262 if (pdi->tag == DW_TAG_subprogram)
4263 {
4264 if (pdi->has_pc_info)
4265 {
4266 if (pdi->lowpc < *lowpc)
4267 *lowpc = pdi->lowpc;
4268 if (pdi->highpc > *highpc)
4269 *highpc = pdi->highpc;
5734ee8b
DJ
4270 if (need_pc)
4271 {
4272 CORE_ADDR baseaddr;
4273 struct objfile *objfile = cu->objfile;
4274
4275 baseaddr = ANOFFSET (objfile->section_offsets,
4276 SECT_OFF_TEXT (objfile));
4277 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4278 pdi->lowpc + baseaddr,
4279 pdi->highpc - 1 + baseaddr,
9291a0cd 4280 cu->per_cu->v.psymtab);
5734ee8b 4281 }
481860b3
GB
4282 }
4283
4284 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4285 {
bc30ff58 4286 if (!pdi->is_declaration)
e8d05480
JB
4287 /* Ignore subprogram DIEs that do not have a name, they are
4288 illegal. Do not emit a complaint at this point, we will
4289 do so when we convert this psymtab into a symtab. */
4290 if (pdi->name)
4291 add_partial_symbol (pdi, cu);
bc30ff58
JB
4292 }
4293 }
6e70227d 4294
bc30ff58
JB
4295 if (! pdi->has_children)
4296 return;
4297
4298 if (cu->language == language_ada)
4299 {
4300 pdi = pdi->die_child;
4301 while (pdi != NULL)
4302 {
4303 fixup_partial_die (pdi, cu);
4304 if (pdi->tag == DW_TAG_subprogram
4305 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4306 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4307 pdi = pdi->die_sibling;
4308 }
4309 }
4310}
4311
91c24f0a
DC
4312/* Read a partial die corresponding to an enumeration type. */
4313
72bf9492
DJ
4314static void
4315add_partial_enumeration (struct partial_die_info *enum_pdi,
4316 struct dwarf2_cu *cu)
91c24f0a 4317{
72bf9492 4318 struct partial_die_info *pdi;
91c24f0a
DC
4319
4320 if (enum_pdi->name != NULL)
72bf9492
DJ
4321 add_partial_symbol (enum_pdi, cu);
4322
4323 pdi = enum_pdi->die_child;
4324 while (pdi)
91c24f0a 4325 {
72bf9492 4326 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4327 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4328 else
72bf9492
DJ
4329 add_partial_symbol (pdi, cu);
4330 pdi = pdi->die_sibling;
91c24f0a 4331 }
91c24f0a
DC
4332}
4333
6caca83c
CC
4334/* Return the initial uleb128 in the die at INFO_PTR. */
4335
4336static unsigned int
4337peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4338{
4339 unsigned int bytes_read;
4340
4341 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4342}
4343
4bb7a0a7
DJ
4344/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4345 Return the corresponding abbrev, or NULL if the number is zero (indicating
4346 an empty DIE). In either case *BYTES_READ will be set to the length of
4347 the initial number. */
4348
4349static struct abbrev_info *
fe1b8b76 4350peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4351 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4352{
4353 bfd *abfd = cu->objfile->obfd;
4354 unsigned int abbrev_number;
4355 struct abbrev_info *abbrev;
4356
4357 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4358
4359 if (abbrev_number == 0)
4360 return NULL;
4361
4362 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4363 if (!abbrev)
4364 {
3e43a32a
MS
4365 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4366 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4367 }
4368
4369 return abbrev;
4370}
4371
93311388
DE
4372/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4373 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4374 DIE. Any children of the skipped DIEs will also be skipped. */
4375
fe1b8b76 4376static gdb_byte *
93311388 4377skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4378{
4379 struct abbrev_info *abbrev;
4380 unsigned int bytes_read;
4381
4382 while (1)
4383 {
4384 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4385 if (abbrev == NULL)
4386 return info_ptr + bytes_read;
4387 else
93311388 4388 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4389 }
4390}
4391
93311388
DE
4392/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4393 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4394 abbrev corresponding to that skipped uleb128 should be passed in
4395 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4396 children. */
4397
fe1b8b76 4398static gdb_byte *
93311388
DE
4399skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4400 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4401{
4402 unsigned int bytes_read;
4403 struct attribute attr;
4404 bfd *abfd = cu->objfile->obfd;
4405 unsigned int form, i;
4406
4407 for (i = 0; i < abbrev->num_attrs; i++)
4408 {
4409 /* The only abbrev we care about is DW_AT_sibling. */
4410 if (abbrev->attrs[i].name == DW_AT_sibling)
4411 {
4412 read_attribute (&attr, &abbrev->attrs[i],
4413 abfd, info_ptr, cu);
4414 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4415 complaint (&symfile_complaints,
4416 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4417 else
b64f50a1 4418 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
4419 }
4420
4421 /* If it isn't DW_AT_sibling, skip this attribute. */
4422 form = abbrev->attrs[i].form;
4423 skip_attribute:
4424 switch (form)
4425 {
4bb7a0a7 4426 case DW_FORM_ref_addr:
ae411497
TT
4427 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4428 and later it is offset sized. */
4429 if (cu->header.version == 2)
4430 info_ptr += cu->header.addr_size;
4431 else
4432 info_ptr += cu->header.offset_size;
4433 break;
4434 case DW_FORM_addr:
4bb7a0a7
DJ
4435 info_ptr += cu->header.addr_size;
4436 break;
4437 case DW_FORM_data1:
4438 case DW_FORM_ref1:
4439 case DW_FORM_flag:
4440 info_ptr += 1;
4441 break;
2dc7f7b3
TT
4442 case DW_FORM_flag_present:
4443 break;
4bb7a0a7
DJ
4444 case DW_FORM_data2:
4445 case DW_FORM_ref2:
4446 info_ptr += 2;
4447 break;
4448 case DW_FORM_data4:
4449 case DW_FORM_ref4:
4450 info_ptr += 4;
4451 break;
4452 case DW_FORM_data8:
4453 case DW_FORM_ref8:
55f1336d 4454 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4455 info_ptr += 8;
4456 break;
4457 case DW_FORM_string:
9b1c24c8 4458 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4459 info_ptr += bytes_read;
4460 break;
2dc7f7b3 4461 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4462 case DW_FORM_strp:
4463 info_ptr += cu->header.offset_size;
4464 break;
2dc7f7b3 4465 case DW_FORM_exprloc:
4bb7a0a7
DJ
4466 case DW_FORM_block:
4467 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4468 info_ptr += bytes_read;
4469 break;
4470 case DW_FORM_block1:
4471 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4472 break;
4473 case DW_FORM_block2:
4474 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4475 break;
4476 case DW_FORM_block4:
4477 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4478 break;
4479 case DW_FORM_sdata:
4480 case DW_FORM_udata:
4481 case DW_FORM_ref_udata:
4482 info_ptr = skip_leb128 (abfd, info_ptr);
4483 break;
4484 case DW_FORM_indirect:
4485 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4486 info_ptr += bytes_read;
4487 /* We need to continue parsing from here, so just go back to
4488 the top. */
4489 goto skip_attribute;
4490
4491 default:
3e43a32a
MS
4492 error (_("Dwarf Error: Cannot handle %s "
4493 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4494 dwarf_form_name (form),
4495 bfd_get_filename (abfd));
4496 }
4497 }
4498
4499 if (abbrev->has_children)
93311388 4500 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4501 else
4502 return info_ptr;
4503}
4504
93311388
DE
4505/* Locate ORIG_PDI's sibling.
4506 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4507 in BUFFER. */
91c24f0a 4508
fe1b8b76 4509static gdb_byte *
93311388
DE
4510locate_pdi_sibling (struct partial_die_info *orig_pdi,
4511 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4512 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4513{
4514 /* Do we know the sibling already? */
72bf9492 4515
91c24f0a
DC
4516 if (orig_pdi->sibling)
4517 return orig_pdi->sibling;
4518
4519 /* Are there any children to deal with? */
4520
4521 if (!orig_pdi->has_children)
4522 return info_ptr;
4523
4bb7a0a7 4524 /* Skip the children the long way. */
91c24f0a 4525
93311388 4526 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4527}
4528
c906108c
SS
4529/* Expand this partial symbol table into a full symbol table. */
4530
4531static void
fba45db2 4532dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4533{
c906108c
SS
4534 if (pst != NULL)
4535 {
4536 if (pst->readin)
4537 {
3e43a32a
MS
4538 warning (_("bug: psymtab for %s is already read in."),
4539 pst->filename);
c906108c
SS
4540 }
4541 else
4542 {
4543 if (info_verbose)
4544 {
3e43a32a
MS
4545 printf_filtered (_("Reading in symbols for %s..."),
4546 pst->filename);
c906108c
SS
4547 gdb_flush (gdb_stdout);
4548 }
4549
10b3939b
DJ
4550 /* Restore our global data. */
4551 dwarf2_per_objfile = objfile_data (pst->objfile,
4552 dwarf2_objfile_data_key);
4553
b2ab525c
KB
4554 /* If this psymtab is constructed from a debug-only objfile, the
4555 has_section_at_zero flag will not necessarily be correct. We
4556 can get the correct value for this flag by looking at the data
4557 associated with the (presumably stripped) associated objfile. */
4558 if (pst->objfile->separate_debug_objfile_backlink)
4559 {
4560 struct dwarf2_per_objfile *dpo_backlink
4561 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4562 dwarf2_objfile_data_key);
9a619af0 4563
b2ab525c
KB
4564 dwarf2_per_objfile->has_section_at_zero
4565 = dpo_backlink->has_section_at_zero;
4566 }
4567
98bfdba5
PA
4568 dwarf2_per_objfile->reading_partial_symbols = 0;
4569
c906108c
SS
4570 psymtab_to_symtab_1 (pst);
4571
4572 /* Finish up the debug error message. */
4573 if (info_verbose)
a3f17187 4574 printf_filtered (_("done.\n"));
c906108c
SS
4575 }
4576 }
4577}
9cdd5dbd
DE
4578\f
4579/* Reading in full CUs. */
c906108c 4580
10b3939b
DJ
4581/* Add PER_CU to the queue. */
4582
4583static void
a0f42c21 4584queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4585{
4586 struct dwarf2_queue_item *item;
4587
4588 per_cu->queued = 1;
4589 item = xmalloc (sizeof (*item));
4590 item->per_cu = per_cu;
4591 item->next = NULL;
4592
4593 if (dwarf2_queue == NULL)
4594 dwarf2_queue = item;
4595 else
4596 dwarf2_queue_tail->next = item;
4597
4598 dwarf2_queue_tail = item;
4599}
4600
4601/* Process the queue. */
4602
4603static void
a0f42c21 4604process_queue (void)
10b3939b
DJ
4605{
4606 struct dwarf2_queue_item *item, *next_item;
4607
03dd20cc
DJ
4608 /* The queue starts out with one item, but following a DIE reference
4609 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4610 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4611 {
9291a0cd
TT
4612 if (dwarf2_per_objfile->using_index
4613 ? !item->per_cu->v.quick->symtab
4614 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4615 process_full_comp_unit (item->per_cu);
4616
4617 item->per_cu->queued = 0;
4618 next_item = item->next;
4619 xfree (item);
4620 }
4621
4622 dwarf2_queue_tail = NULL;
4623}
4624
4625/* Free all allocated queue entries. This function only releases anything if
4626 an error was thrown; if the queue was processed then it would have been
4627 freed as we went along. */
4628
4629static void
4630dwarf2_release_queue (void *dummy)
4631{
4632 struct dwarf2_queue_item *item, *last;
4633
4634 item = dwarf2_queue;
4635 while (item)
4636 {
4637 /* Anything still marked queued is likely to be in an
4638 inconsistent state, so discard it. */
4639 if (item->per_cu->queued)
4640 {
4641 if (item->per_cu->cu != NULL)
4642 free_one_cached_comp_unit (item->per_cu->cu);
4643 item->per_cu->queued = 0;
4644 }
4645
4646 last = item;
4647 item = item->next;
4648 xfree (last);
4649 }
4650
4651 dwarf2_queue = dwarf2_queue_tail = NULL;
4652}
4653
4654/* Read in full symbols for PST, and anything it depends on. */
4655
c906108c 4656static void
fba45db2 4657psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4658{
10b3939b 4659 struct dwarf2_per_cu_data *per_cu;
c906108c 4660 struct cleanup *back_to;
aaa75496
JB
4661 int i;
4662
4663 for (i = 0; i < pst->number_of_dependencies; i++)
4664 if (!pst->dependencies[i]->readin)
4665 {
4666 /* Inform about additional files that need to be read in. */
4667 if (info_verbose)
4668 {
a3f17187 4669 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4670 fputs_filtered (" ", gdb_stdout);
4671 wrap_here ("");
4672 fputs_filtered ("and ", gdb_stdout);
4673 wrap_here ("");
4674 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4675 wrap_here (""); /* Flush output. */
aaa75496
JB
4676 gdb_flush (gdb_stdout);
4677 }
4678 psymtab_to_symtab_1 (pst->dependencies[i]);
4679 }
4680
e38df1d0 4681 per_cu = pst->read_symtab_private;
10b3939b
DJ
4682
4683 if (per_cu == NULL)
aaa75496
JB
4684 {
4685 /* It's an include file, no symbols to read for it.
4686 Everything is in the parent symtab. */
4687 pst->readin = 1;
4688 return;
4689 }
c906108c 4690
a0f42c21 4691 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4692}
4693
93311388 4694/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4695
93311388 4696static void
a0f42c21 4697load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4698{
a0f42c21 4699 struct objfile *objfile = per_cu->objfile;
31ffec48 4700 bfd *abfd = objfile->obfd;
10b3939b 4701 struct dwarf2_cu *cu;
b64f50a1 4702 sect_offset offset;
93311388 4703 gdb_byte *info_ptr, *beg_of_comp_unit;
7fb3ad1f 4704 struct cleanup *free_cu_cleanup = NULL;
10b3939b 4705 struct attribute *attr;
98bfdba5 4706 int read_cu = 0;
6502dd73 4707
b0df02fd 4708 gdb_assert (! per_cu->debug_types_section);
348e048f 4709
c906108c 4710 /* Set local variables from the partial symbol table info. */
10b3939b 4711 offset = per_cu->offset;
6502dd73 4712
be391dca 4713 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
b64f50a1 4714 info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
93311388 4715 beg_of_comp_unit = info_ptr;
63d06c5c 4716
98bfdba5
PA
4717 if (per_cu->cu == NULL)
4718 {
9816fde3 4719 cu = xmalloc (sizeof (*cu));
23745b47 4720 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4721
4722 read_cu = 1;
c906108c 4723
98bfdba5 4724 /* If an error occurs while loading, release our storage. */
68dc6402 4725 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4726
98bfdba5
PA
4727 /* Read in the comp_unit header. */
4728 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4729
6caca83c
CC
4730 /* Skip dummy compilation units. */
4731 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4732 + dwarf2_per_objfile->info.size)
4733 || peek_abbrev_code (abfd, info_ptr) == 0)
4734 {
4735 do_cleanups (free_cu_cleanup);
4736 return;
4737 }
4738
98bfdba5
PA
4739 /* Complete the cu_header. */
4740 cu->header.offset = offset;
b64f50a1 4741 cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
98bfdba5
PA
4742 }
4743 else
4744 {
4745 cu = per_cu->cu;
b64f50a1 4746 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 4747 }
e142c38c 4748
93311388 4749 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4750
4751 /* We try not to read any attributes in this function, because not
9cdd5dbd 4752 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4753 table processing isn't initialized. But we have to set the CU language,
4754 or we won't be able to build types correctly. */
9816fde3 4755 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4756
a6c727b2
DJ
4757 /* Similarly, if we do not read the producer, we can not apply
4758 producer-specific interpretation. */
4759 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4760 if (attr)
4761 cu->producer = DW_STRING (attr);
4762
98bfdba5
PA
4763 if (read_cu)
4764 {
98bfdba5
PA
4765 /* We've successfully allocated this compilation unit. Let our
4766 caller clean it up when finished with it. */
4767 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
4768
4769 /* Link this CU into read_in_chain. */
4770 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4771 dwarf2_per_objfile->read_in_chain = per_cu;
98bfdba5 4772 }
10b3939b
DJ
4773}
4774
3da10d80
KS
4775/* Add a DIE to the delayed physname list. */
4776
4777static void
4778add_to_method_list (struct type *type, int fnfield_index, int index,
4779 const char *name, struct die_info *die,
4780 struct dwarf2_cu *cu)
4781{
4782 struct delayed_method_info mi;
4783 mi.type = type;
4784 mi.fnfield_index = fnfield_index;
4785 mi.index = index;
4786 mi.name = name;
4787 mi.die = die;
4788 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4789}
4790
4791/* A cleanup for freeing the delayed method list. */
4792
4793static void
4794free_delayed_list (void *ptr)
4795{
4796 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4797 if (cu->method_list != NULL)
4798 {
4799 VEC_free (delayed_method_info, cu->method_list);
4800 cu->method_list = NULL;
4801 }
4802}
4803
4804/* Compute the physnames of any methods on the CU's method list.
4805
4806 The computation of method physnames is delayed in order to avoid the
4807 (bad) condition that one of the method's formal parameters is of an as yet
4808 incomplete type. */
4809
4810static void
4811compute_delayed_physnames (struct dwarf2_cu *cu)
4812{
4813 int i;
4814 struct delayed_method_info *mi;
4815 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4816 {
1d06ead6 4817 const char *physname;
3da10d80
KS
4818 struct fn_fieldlist *fn_flp
4819 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4820 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4821 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4822 }
4823}
4824
9cdd5dbd 4825/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4826 already been loaded into memory. */
4827
4828static void
4829process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4830{
10b3939b 4831 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4832 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4833 CORE_ADDR lowpc, highpc;
4834 struct symtab *symtab;
3da10d80 4835 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4836 CORE_ADDR baseaddr;
4837
4838 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4839
10b3939b
DJ
4840 buildsym_init ();
4841 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4842 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4843
4844 cu->list_in_scope = &file_symbols;
c906108c
SS
4845
4846 /* Do line number decoding in read_file_scope () */
10b3939b 4847 process_die (cu->dies, cu);
c906108c 4848
3da10d80
KS
4849 /* Now that we have processed all the DIEs in the CU, all the types
4850 should be complete, and it should now be safe to compute all of the
4851 physnames. */
4852 compute_delayed_physnames (cu);
4853 do_cleanups (delayed_list_cleanup);
4854
fae299cd
DC
4855 /* Some compilers don't define a DW_AT_high_pc attribute for the
4856 compilation unit. If the DW_AT_high_pc is missing, synthesize
4857 it, by scanning the DIE's below the compilation unit. */
10b3939b 4858 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4859
613e1657 4860 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4861
8be455d7 4862 if (symtab != NULL)
c906108c 4863 {
df15bd07 4864 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4865
8be455d7
JK
4866 /* Set symtab language to language from DW_AT_language. If the
4867 compilation is from a C file generated by language preprocessors, do
4868 not set the language if it was already deduced by start_subfile. */
4869 if (!(cu->language == language_c && symtab->language != language_c))
4870 symtab->language = cu->language;
4871
4872 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4873 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
4874 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
4875 there were bugs in prologue debug info, fixed later in GCC-4.5
4876 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
4877
4878 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4879 needed, it would be wrong due to missing DW_AT_producer there.
4880
4881 Still one can confuse GDB by using non-standard GCC compilation
4882 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4883 */
ab260dad 4884 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 4885 symtab->locations_valid = 1;
e0d00bc7
JK
4886
4887 if (gcc_4_minor >= 5)
4888 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4889
4890 symtab->call_site_htab = cu->call_site_htab;
c906108c 4891 }
9291a0cd
TT
4892
4893 if (dwarf2_per_objfile->using_index)
4894 per_cu->v.quick->symtab = symtab;
4895 else
4896 {
4897 struct partial_symtab *pst = per_cu->v.psymtab;
4898 pst->symtab = symtab;
4899 pst->readin = 1;
4900 }
c906108c
SS
4901
4902 do_cleanups (back_to);
4903}
4904
4905/* Process a die and its children. */
4906
4907static void
e7c27a73 4908process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4909{
4910 switch (die->tag)
4911 {
4912 case DW_TAG_padding:
4913 break;
4914 case DW_TAG_compile_unit:
e7c27a73 4915 read_file_scope (die, cu);
c906108c 4916 break;
348e048f
DE
4917 case DW_TAG_type_unit:
4918 read_type_unit_scope (die, cu);
4919 break;
c906108c 4920 case DW_TAG_subprogram:
c906108c 4921 case DW_TAG_inlined_subroutine:
edb3359d 4922 read_func_scope (die, cu);
c906108c
SS
4923 break;
4924 case DW_TAG_lexical_block:
14898363
L
4925 case DW_TAG_try_block:
4926 case DW_TAG_catch_block:
e7c27a73 4927 read_lexical_block_scope (die, cu);
c906108c 4928 break;
96408a79
SA
4929 case DW_TAG_GNU_call_site:
4930 read_call_site_scope (die, cu);
4931 break;
c906108c 4932 case DW_TAG_class_type:
680b30c7 4933 case DW_TAG_interface_type:
c906108c
SS
4934 case DW_TAG_structure_type:
4935 case DW_TAG_union_type:
134d01f1 4936 process_structure_scope (die, cu);
c906108c
SS
4937 break;
4938 case DW_TAG_enumeration_type:
134d01f1 4939 process_enumeration_scope (die, cu);
c906108c 4940 break;
134d01f1 4941
f792889a
DJ
4942 /* These dies have a type, but processing them does not create
4943 a symbol or recurse to process the children. Therefore we can
4944 read them on-demand through read_type_die. */
c906108c 4945 case DW_TAG_subroutine_type:
72019c9c 4946 case DW_TAG_set_type:
c906108c 4947 case DW_TAG_array_type:
c906108c 4948 case DW_TAG_pointer_type:
c906108c 4949 case DW_TAG_ptr_to_member_type:
c906108c 4950 case DW_TAG_reference_type:
c906108c 4951 case DW_TAG_string_type:
c906108c 4952 break;
134d01f1 4953
c906108c 4954 case DW_TAG_base_type:
a02abb62 4955 case DW_TAG_subrange_type:
cb249c71 4956 case DW_TAG_typedef:
134d01f1
DJ
4957 /* Add a typedef symbol for the type definition, if it has a
4958 DW_AT_name. */
f792889a 4959 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4960 break;
c906108c 4961 case DW_TAG_common_block:
e7c27a73 4962 read_common_block (die, cu);
c906108c
SS
4963 break;
4964 case DW_TAG_common_inclusion:
4965 break;
d9fa45fe 4966 case DW_TAG_namespace:
63d06c5c 4967 processing_has_namespace_info = 1;
e7c27a73 4968 read_namespace (die, cu);
d9fa45fe 4969 break;
5d7cb8df 4970 case DW_TAG_module:
f55ee35c 4971 processing_has_namespace_info = 1;
5d7cb8df
JK
4972 read_module (die, cu);
4973 break;
d9fa45fe
DC
4974 case DW_TAG_imported_declaration:
4975 case DW_TAG_imported_module:
63d06c5c 4976 processing_has_namespace_info = 1;
27aa8d6a
SW
4977 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4978 || cu->language != language_fortran))
4979 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4980 dwarf_tag_name (die->tag));
4981 read_import_statement (die, cu);
d9fa45fe 4982 break;
c906108c 4983 default:
e7c27a73 4984 new_symbol (die, NULL, cu);
c906108c
SS
4985 break;
4986 }
4987}
4988
94af9270
KS
4989/* A helper function for dwarf2_compute_name which determines whether DIE
4990 needs to have the name of the scope prepended to the name listed in the
4991 die. */
4992
4993static int
4994die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4995{
1c809c68
TT
4996 struct attribute *attr;
4997
94af9270
KS
4998 switch (die->tag)
4999 {
5000 case DW_TAG_namespace:
5001 case DW_TAG_typedef:
5002 case DW_TAG_class_type:
5003 case DW_TAG_interface_type:
5004 case DW_TAG_structure_type:
5005 case DW_TAG_union_type:
5006 case DW_TAG_enumeration_type:
5007 case DW_TAG_enumerator:
5008 case DW_TAG_subprogram:
5009 case DW_TAG_member:
5010 return 1;
5011
5012 case DW_TAG_variable:
c2b0a229 5013 case DW_TAG_constant:
94af9270
KS
5014 /* We only need to prefix "globally" visible variables. These include
5015 any variable marked with DW_AT_external or any variable that
5016 lives in a namespace. [Variables in anonymous namespaces
5017 require prefixing, but they are not DW_AT_external.] */
5018
5019 if (dwarf2_attr (die, DW_AT_specification, cu))
5020 {
5021 struct dwarf2_cu *spec_cu = cu;
9a619af0 5022
94af9270
KS
5023 return die_needs_namespace (die_specification (die, &spec_cu),
5024 spec_cu);
5025 }
5026
1c809c68 5027 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5028 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5029 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5030 return 0;
5031 /* A variable in a lexical block of some kind does not need a
5032 namespace, even though in C++ such variables may be external
5033 and have a mangled name. */
5034 if (die->parent->tag == DW_TAG_lexical_block
5035 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5036 || die->parent->tag == DW_TAG_catch_block
5037 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5038 return 0;
5039 return 1;
94af9270
KS
5040
5041 default:
5042 return 0;
5043 }
5044}
5045
98bfdba5
PA
5046/* Retrieve the last character from a mem_file. */
5047
5048static void
5049do_ui_file_peek_last (void *object, const char *buffer, long length)
5050{
5051 char *last_char_p = (char *) object;
5052
5053 if (length > 0)
5054 *last_char_p = buffer[length - 1];
5055}
5056
94af9270
KS
5057/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5058 compute the physname for the object, which include a method's
5059 formal parameters (C++/Java) and return type (Java).
5060
af6b7be1
JB
5061 For Ada, return the DIE's linkage name rather than the fully qualified
5062 name. PHYSNAME is ignored..
5063
94af9270
KS
5064 The result is allocated on the objfile_obstack and canonicalized. */
5065
5066static const char *
5067dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5068 int physname)
5069{
bb5ed363
DE
5070 struct objfile *objfile = cu->objfile;
5071
94af9270
KS
5072 if (name == NULL)
5073 name = dwarf2_name (die, cu);
5074
f55ee35c
JK
5075 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5076 compute it by typename_concat inside GDB. */
5077 if (cu->language == language_ada
5078 || (cu->language == language_fortran && physname))
5079 {
5080 /* For Ada unit, we prefer the linkage name over the name, as
5081 the former contains the exported name, which the user expects
5082 to be able to reference. Ideally, we want the user to be able
5083 to reference this entity using either natural or linkage name,
5084 but we haven't started looking at this enhancement yet. */
5085 struct attribute *attr;
5086
5087 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5088 if (attr == NULL)
5089 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5090 if (attr && DW_STRING (attr))
5091 return DW_STRING (attr);
5092 }
5093
94af9270
KS
5094 /* These are the only languages we know how to qualify names in. */
5095 if (name != NULL
f55ee35c
JK
5096 && (cu->language == language_cplus || cu->language == language_java
5097 || cu->language == language_fortran))
94af9270
KS
5098 {
5099 if (die_needs_namespace (die, cu))
5100 {
5101 long length;
0d5cff50 5102 const char *prefix;
94af9270
KS
5103 struct ui_file *buf;
5104
5105 prefix = determine_prefix (die, cu);
5106 buf = mem_fileopen ();
5107 if (*prefix != '\0')
5108 {
f55ee35c
JK
5109 char *prefixed_name = typename_concat (NULL, prefix, name,
5110 physname, cu);
9a619af0 5111
94af9270
KS
5112 fputs_unfiltered (prefixed_name, buf);
5113 xfree (prefixed_name);
5114 }
5115 else
62d5b8da 5116 fputs_unfiltered (name, buf);
94af9270 5117
98bfdba5
PA
5118 /* Template parameters may be specified in the DIE's DW_AT_name, or
5119 as children with DW_TAG_template_type_param or
5120 DW_TAG_value_type_param. If the latter, add them to the name
5121 here. If the name already has template parameters, then
5122 skip this step; some versions of GCC emit both, and
5123 it is more efficient to use the pre-computed name.
5124
5125 Something to keep in mind about this process: it is very
5126 unlikely, or in some cases downright impossible, to produce
5127 something that will match the mangled name of a function.
5128 If the definition of the function has the same debug info,
5129 we should be able to match up with it anyway. But fallbacks
5130 using the minimal symbol, for instance to find a method
5131 implemented in a stripped copy of libstdc++, will not work.
5132 If we do not have debug info for the definition, we will have to
5133 match them up some other way.
5134
5135 When we do name matching there is a related problem with function
5136 templates; two instantiated function templates are allowed to
5137 differ only by their return types, which we do not add here. */
5138
5139 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5140 {
5141 struct attribute *attr;
5142 struct die_info *child;
5143 int first = 1;
5144
5145 die->building_fullname = 1;
5146
5147 for (child = die->child; child != NULL; child = child->sibling)
5148 {
5149 struct type *type;
12df843f 5150 LONGEST value;
98bfdba5
PA
5151 gdb_byte *bytes;
5152 struct dwarf2_locexpr_baton *baton;
5153 struct value *v;
5154
5155 if (child->tag != DW_TAG_template_type_param
5156 && child->tag != DW_TAG_template_value_param)
5157 continue;
5158
5159 if (first)
5160 {
5161 fputs_unfiltered ("<", buf);
5162 first = 0;
5163 }
5164 else
5165 fputs_unfiltered (", ", buf);
5166
5167 attr = dwarf2_attr (child, DW_AT_type, cu);
5168 if (attr == NULL)
5169 {
5170 complaint (&symfile_complaints,
5171 _("template parameter missing DW_AT_type"));
5172 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5173 continue;
5174 }
5175 type = die_type (child, cu);
5176
5177 if (child->tag == DW_TAG_template_type_param)
5178 {
5179 c_print_type (type, "", buf, -1, 0);
5180 continue;
5181 }
5182
5183 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5184 if (attr == NULL)
5185 {
5186 complaint (&symfile_complaints,
3e43a32a
MS
5187 _("template parameter missing "
5188 "DW_AT_const_value"));
98bfdba5
PA
5189 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5190 continue;
5191 }
5192
5193 dwarf2_const_value_attr (attr, type, name,
5194 &cu->comp_unit_obstack, cu,
5195 &value, &bytes, &baton);
5196
5197 if (TYPE_NOSIGN (type))
5198 /* GDB prints characters as NUMBER 'CHAR'. If that's
5199 changed, this can use value_print instead. */
5200 c_printchar (value, type, buf);
5201 else
5202 {
5203 struct value_print_options opts;
5204
5205 if (baton != NULL)
5206 v = dwarf2_evaluate_loc_desc (type, NULL,
5207 baton->data,
5208 baton->size,
5209 baton->per_cu);
5210 else if (bytes != NULL)
5211 {
5212 v = allocate_value (type);
5213 memcpy (value_contents_writeable (v), bytes,
5214 TYPE_LENGTH (type));
5215 }
5216 else
5217 v = value_from_longest (type, value);
5218
3e43a32a
MS
5219 /* Specify decimal so that we do not depend on
5220 the radix. */
98bfdba5
PA
5221 get_formatted_print_options (&opts, 'd');
5222 opts.raw = 1;
5223 value_print (v, buf, &opts);
5224 release_value (v);
5225 value_free (v);
5226 }
5227 }
5228
5229 die->building_fullname = 0;
5230
5231 if (!first)
5232 {
5233 /* Close the argument list, with a space if necessary
5234 (nested templates). */
5235 char last_char = '\0';
5236 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5237 if (last_char == '>')
5238 fputs_unfiltered (" >", buf);
5239 else
5240 fputs_unfiltered (">", buf);
5241 }
5242 }
5243
94af9270
KS
5244 /* For Java and C++ methods, append formal parameter type
5245 information, if PHYSNAME. */
6e70227d 5246
94af9270
KS
5247 if (physname && die->tag == DW_TAG_subprogram
5248 && (cu->language == language_cplus
5249 || cu->language == language_java))
5250 {
5251 struct type *type = read_type_die (die, cu);
5252
3167638f 5253 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5254
5255 if (cu->language == language_java)
5256 {
5257 /* For java, we must append the return type to method
0963b4bd 5258 names. */
94af9270
KS
5259 if (die->tag == DW_TAG_subprogram)
5260 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5261 0, 0);
5262 }
5263 else if (cu->language == language_cplus)
5264 {
60430eff
DJ
5265 /* Assume that an artificial first parameter is
5266 "this", but do not crash if it is not. RealView
5267 marks unnamed (and thus unused) parameters as
5268 artificial; there is no way to differentiate
5269 the two cases. */
94af9270
KS
5270 if (TYPE_NFIELDS (type) > 0
5271 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5272 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5273 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5274 0))))
94af9270
KS
5275 fputs_unfiltered (" const", buf);
5276 }
5277 }
5278
bb5ed363 5279 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5280 &length);
5281 ui_file_delete (buf);
5282
5283 if (cu->language == language_cplus)
5284 {
5285 char *cname
5286 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5287 &objfile->objfile_obstack);
9a619af0 5288
94af9270
KS
5289 if (cname != NULL)
5290 name = cname;
5291 }
5292 }
5293 }
5294
5295 return name;
5296}
5297
0114d602
DJ
5298/* Return the fully qualified name of DIE, based on its DW_AT_name.
5299 If scope qualifiers are appropriate they will be added. The result
5300 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5301 not have a name. NAME may either be from a previous call to
5302 dwarf2_name or NULL.
5303
0963b4bd 5304 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5305
5306static const char *
94af9270 5307dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5308{
94af9270
KS
5309 return dwarf2_compute_name (name, die, cu, 0);
5310}
0114d602 5311
94af9270
KS
5312/* Construct a physname for the given DIE in CU. NAME may either be
5313 from a previous call to dwarf2_name or NULL. The result will be
5314 allocated on the objfile_objstack or NULL if the DIE does not have a
5315 name.
0114d602 5316
94af9270 5317 The output string will be canonicalized (if C++/Java). */
0114d602 5318
94af9270
KS
5319static const char *
5320dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5321{
bb5ed363 5322 struct objfile *objfile = cu->objfile;
900e11f9
JK
5323 struct attribute *attr;
5324 const char *retval, *mangled = NULL, *canon = NULL;
5325 struct cleanup *back_to;
5326 int need_copy = 1;
5327
5328 /* In this case dwarf2_compute_name is just a shortcut not building anything
5329 on its own. */
5330 if (!die_needs_namespace (die, cu))
5331 return dwarf2_compute_name (name, die, cu, 1);
5332
5333 back_to = make_cleanup (null_cleanup, NULL);
5334
5335 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5336 if (!attr)
5337 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5338
5339 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5340 has computed. */
5341 if (attr && DW_STRING (attr))
5342 {
5343 char *demangled;
5344
5345 mangled = DW_STRING (attr);
5346
5347 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5348 type. It is easier for GDB users to search for such functions as
5349 `name(params)' than `long name(params)'. In such case the minimal
5350 symbol names do not match the full symbol names but for template
5351 functions there is never a need to look up their definition from their
5352 declaration so the only disadvantage remains the minimal symbol
5353 variant `long name(params)' does not have the proper inferior type.
5354 */
5355
5356 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5357 | (cu->language == language_java
5358 ? DMGL_JAVA | DMGL_RET_POSTFIX
5359 : DMGL_RET_DROP)));
5360 if (demangled)
5361 {
5362 make_cleanup (xfree, demangled);
5363 canon = demangled;
5364 }
5365 else
5366 {
5367 canon = mangled;
5368 need_copy = 0;
5369 }
5370 }
5371
5372 if (canon == NULL || check_physname)
5373 {
5374 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5375
5376 if (canon != NULL && strcmp (physname, canon) != 0)
5377 {
5378 /* It may not mean a bug in GDB. The compiler could also
5379 compute DW_AT_linkage_name incorrectly. But in such case
5380 GDB would need to be bug-to-bug compatible. */
5381
5382 complaint (&symfile_complaints,
5383 _("Computed physname <%s> does not match demangled <%s> "
5384 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 5385 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
5386
5387 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5388 is available here - over computed PHYSNAME. It is safer
5389 against both buggy GDB and buggy compilers. */
5390
5391 retval = canon;
5392 }
5393 else
5394 {
5395 retval = physname;
5396 need_copy = 0;
5397 }
5398 }
5399 else
5400 retval = canon;
5401
5402 if (need_copy)
5403 retval = obsavestring (retval, strlen (retval),
bb5ed363 5404 &objfile->objfile_obstack);
900e11f9
JK
5405
5406 do_cleanups (back_to);
5407 return retval;
0114d602
DJ
5408}
5409
27aa8d6a
SW
5410/* Read the import statement specified by the given die and record it. */
5411
5412static void
5413read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5414{
bb5ed363 5415 struct objfile *objfile = cu->objfile;
27aa8d6a 5416 struct attribute *import_attr;
32019081 5417 struct die_info *imported_die, *child_die;
de4affc9 5418 struct dwarf2_cu *imported_cu;
27aa8d6a 5419 const char *imported_name;
794684b6 5420 const char *imported_name_prefix;
13387711
SW
5421 const char *canonical_name;
5422 const char *import_alias;
5423 const char *imported_declaration = NULL;
794684b6 5424 const char *import_prefix;
32019081
JK
5425 VEC (const_char_ptr) *excludes = NULL;
5426 struct cleanup *cleanups;
13387711
SW
5427
5428 char *temp;
27aa8d6a
SW
5429
5430 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5431 if (import_attr == NULL)
5432 {
5433 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5434 dwarf_tag_name (die->tag));
5435 return;
5436 }
5437
de4affc9
CC
5438 imported_cu = cu;
5439 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5440 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5441 if (imported_name == NULL)
5442 {
5443 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5444
5445 The import in the following code:
5446 namespace A
5447 {
5448 typedef int B;
5449 }
5450
5451 int main ()
5452 {
5453 using A::B;
5454 B b;
5455 return b;
5456 }
5457
5458 ...
5459 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5460 <52> DW_AT_decl_file : 1
5461 <53> DW_AT_decl_line : 6
5462 <54> DW_AT_import : <0x75>
5463 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5464 <59> DW_AT_name : B
5465 <5b> DW_AT_decl_file : 1
5466 <5c> DW_AT_decl_line : 2
5467 <5d> DW_AT_type : <0x6e>
5468 ...
5469 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5470 <76> DW_AT_byte_size : 4
5471 <77> DW_AT_encoding : 5 (signed)
5472
5473 imports the wrong die ( 0x75 instead of 0x58 ).
5474 This case will be ignored until the gcc bug is fixed. */
5475 return;
5476 }
5477
82856980
SW
5478 /* Figure out the local name after import. */
5479 import_alias = dwarf2_name (die, cu);
27aa8d6a 5480
794684b6
SW
5481 /* Figure out where the statement is being imported to. */
5482 import_prefix = determine_prefix (die, cu);
5483
5484 /* Figure out what the scope of the imported die is and prepend it
5485 to the name of the imported die. */
de4affc9 5486 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5487
f55ee35c
JK
5488 if (imported_die->tag != DW_TAG_namespace
5489 && imported_die->tag != DW_TAG_module)
794684b6 5490 {
13387711
SW
5491 imported_declaration = imported_name;
5492 canonical_name = imported_name_prefix;
794684b6 5493 }
13387711 5494 else if (strlen (imported_name_prefix) > 0)
794684b6 5495 {
13387711
SW
5496 temp = alloca (strlen (imported_name_prefix)
5497 + 2 + strlen (imported_name) + 1);
5498 strcpy (temp, imported_name_prefix);
5499 strcat (temp, "::");
5500 strcat (temp, imported_name);
5501 canonical_name = temp;
794684b6 5502 }
13387711
SW
5503 else
5504 canonical_name = imported_name;
794684b6 5505
32019081
JK
5506 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5507
5508 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5509 for (child_die = die->child; child_die && child_die->tag;
5510 child_die = sibling_die (child_die))
5511 {
5512 /* DWARF-4: A Fortran use statement with a “rename list” may be
5513 represented by an imported module entry with an import attribute
5514 referring to the module and owned entries corresponding to those
5515 entities that are renamed as part of being imported. */
5516
5517 if (child_die->tag != DW_TAG_imported_declaration)
5518 {
5519 complaint (&symfile_complaints,
5520 _("child DW_TAG_imported_declaration expected "
5521 "- DIE at 0x%x [in module %s]"),
b64f50a1 5522 child_die->offset.sect_off, objfile->name);
32019081
JK
5523 continue;
5524 }
5525
5526 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5527 if (import_attr == NULL)
5528 {
5529 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5530 dwarf_tag_name (child_die->tag));
5531 continue;
5532 }
5533
5534 imported_cu = cu;
5535 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5536 &imported_cu);
5537 imported_name = dwarf2_name (imported_die, imported_cu);
5538 if (imported_name == NULL)
5539 {
5540 complaint (&symfile_complaints,
5541 _("child DW_TAG_imported_declaration has unknown "
5542 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 5543 child_die->offset.sect_off, objfile->name);
32019081
JK
5544 continue;
5545 }
5546
5547 VEC_safe_push (const_char_ptr, excludes, imported_name);
5548
5549 process_die (child_die, cu);
5550 }
5551
c0cc3a76
SW
5552 cp_add_using_directive (import_prefix,
5553 canonical_name,
5554 import_alias,
13387711 5555 imported_declaration,
32019081 5556 excludes,
bb5ed363 5557 &objfile->objfile_obstack);
32019081
JK
5558
5559 do_cleanups (cleanups);
27aa8d6a
SW
5560}
5561
ae2de4f8
DE
5562/* Cleanup function for read_file_scope. */
5563
cb1df416
DJ
5564static void
5565free_cu_line_header (void *arg)
5566{
5567 struct dwarf2_cu *cu = arg;
5568
5569 free_line_header (cu->line_header);
5570 cu->line_header = NULL;
5571}
5572
9291a0cd
TT
5573static void
5574find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5575 char **name, char **comp_dir)
5576{
5577 struct attribute *attr;
5578
5579 *name = NULL;
5580 *comp_dir = NULL;
5581
5582 /* Find the filename. Do not use dwarf2_name here, since the filename
5583 is not a source language identifier. */
5584 attr = dwarf2_attr (die, DW_AT_name, cu);
5585 if (attr)
5586 {
5587 *name = DW_STRING (attr);
5588 }
5589
5590 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5591 if (attr)
5592 *comp_dir = DW_STRING (attr);
5593 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5594 {
5595 *comp_dir = ldirname (*name);
5596 if (*comp_dir != NULL)
5597 make_cleanup (xfree, *comp_dir);
5598 }
5599 if (*comp_dir != NULL)
5600 {
5601 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5602 directory, get rid of it. */
5603 char *cp = strchr (*comp_dir, ':');
5604
5605 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5606 *comp_dir = cp + 1;
5607 }
5608
5609 if (*name == NULL)
5610 *name = "<unknown>";
5611}
5612
f3f5162e
DE
5613/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5614 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5615 COMP_DIR is the compilation directory.
5616 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5617
5618static void
5619handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5620 const char *comp_dir, int want_line_info)
2ab95328
TT
5621{
5622 struct attribute *attr;
5623 struct objfile *objfile = cu->objfile;
5624 bfd *abfd = objfile->obfd;
5625
2ab95328
TT
5626 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5627 if (attr)
5628 {
5629 unsigned int line_offset = DW_UNSND (attr);
5630 struct line_header *line_header
5631 = dwarf_decode_line_header (line_offset, abfd, cu);
5632
5633 if (line_header)
5634 {
5635 cu->line_header = line_header;
5636 make_cleanup (free_cu_line_header, cu);
f3f5162e 5637 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5638 }
5639 }
5640}
5641
ae2de4f8
DE
5642/* Process DW_TAG_compile_unit. */
5643
c906108c 5644static void
e7c27a73 5645read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5646{
e7c27a73 5647 struct objfile *objfile = cu->objfile;
debd256d 5648 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5649 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5650 CORE_ADDR highpc = ((CORE_ADDR) 0);
5651 struct attribute *attr;
e1024ff1 5652 char *name = NULL;
c906108c
SS
5653 char *comp_dir = NULL;
5654 struct die_info *child_die;
5655 bfd *abfd = objfile->obfd;
e142c38c 5656 CORE_ADDR baseaddr;
6e70227d 5657
e142c38c 5658 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5659
fae299cd 5660 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5661
5662 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5663 from finish_block. */
2acceee2 5664 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5665 lowpc = highpc;
5666 lowpc += baseaddr;
5667 highpc += baseaddr;
5668
9291a0cd 5669 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5670
e142c38c 5671 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5672 if (attr)
5673 {
e142c38c 5674 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5675 }
5676
b0f35d58 5677 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5678 if (attr)
b0f35d58 5679 cu->producer = DW_STRING (attr);
303b6f5d 5680
f4b8a18d
KW
5681 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5682 standardised yet. As a workaround for the language detection we fall
5683 back to the DW_AT_producer string. */
5684 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5685 cu->language = language_opencl;
5686
0963b4bd 5687 /* We assume that we're processing GCC output. */
c906108c 5688 processing_gcc_compilation = 2;
c906108c 5689
df8a16a1
DJ
5690 processing_has_namespace_info = 0;
5691
c906108c
SS
5692 start_symtab (name, comp_dir, lowpc);
5693 record_debugformat ("DWARF 2");
303b6f5d 5694 record_producer (cu->producer);
c906108c 5695
f3f5162e
DE
5696 /* Decode line number information if present. We do this before
5697 processing child DIEs, so that the line header table is available
5698 for DW_AT_decl_file. */
5699 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5700
cb1df416
DJ
5701 /* Process all dies in compilation unit. */
5702 if (die->child != NULL)
5703 {
5704 child_die = die->child;
5705 while (child_die && child_die->tag)
5706 {
5707 process_die (child_die, cu);
5708 child_die = sibling_die (child_die);
5709 }
5710 }
5711
2e276125
JB
5712 /* Decode macro information, if present. Dwarf 2 macro information
5713 refers to information in the line number info statement program
5714 header, so we can only read it if we've read the header
5715 successfully. */
cf2c3c16 5716 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5717 if (attr && cu->line_header)
2e276125 5718 {
cf2c3c16
TT
5719 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5720 complaint (&symfile_complaints,
5721 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5722
5723 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5724 comp_dir, abfd, cu,
5725 &dwarf2_per_objfile->macro, 1);
5726 }
5727 else
5728 {
5729 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5730 if (attr && cu->line_header)
5731 {
5732 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5733
cf2c3c16
TT
5734 dwarf_decode_macros (cu->line_header, macro_offset,
5735 comp_dir, abfd, cu,
5736 &dwarf2_per_objfile->macinfo, 0);
5737 }
2e276125 5738 }
9cdd5dbd 5739
debd256d 5740 do_cleanups (back_to);
5fb290d7
DJ
5741}
5742
ae2de4f8
DE
5743/* Process DW_TAG_type_unit.
5744 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5745 actual type being defined by this TU. In this case the first top
5746 level sibling is there to provide context only. */
5747
5748static void
5749read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5750{
5751 struct objfile *objfile = cu->objfile;
5752 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5753 CORE_ADDR lowpc;
5754 struct attribute *attr;
5755 char *name = NULL;
5756 char *comp_dir = NULL;
5757 struct die_info *child_die;
5758 bfd *abfd = objfile->obfd;
348e048f
DE
5759
5760 /* start_symtab needs a low pc, but we don't really have one.
5761 Do what read_file_scope would do in the absence of such info. */
5762 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5763
5764 /* Find the filename. Do not use dwarf2_name here, since the filename
5765 is not a source language identifier. */
5766 attr = dwarf2_attr (die, DW_AT_name, cu);
5767 if (attr)
5768 name = DW_STRING (attr);
5769
5770 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5771 if (attr)
5772 comp_dir = DW_STRING (attr);
5773 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5774 {
5775 comp_dir = ldirname (name);
5776 if (comp_dir != NULL)
5777 make_cleanup (xfree, comp_dir);
5778 }
5779
5780 if (name == NULL)
5781 name = "<unknown>";
5782
5783 attr = dwarf2_attr (die, DW_AT_language, cu);
5784 if (attr)
5785 set_cu_language (DW_UNSND (attr), cu);
5786
5787 /* This isn't technically needed today. It is done for symmetry
5788 with read_file_scope. */
5789 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5790 if (attr)
348e048f
DE
5791 cu->producer = DW_STRING (attr);
5792
0963b4bd 5793 /* We assume that we're processing GCC output. */
348e048f
DE
5794 processing_gcc_compilation = 2;
5795
5796 processing_has_namespace_info = 0;
5797
5798 start_symtab (name, comp_dir, lowpc);
5799 record_debugformat ("DWARF 2");
5800 record_producer (cu->producer);
5801
f3f5162e
DE
5802 /* Decode line number information if present. We do this before
5803 processing child DIEs, so that the line header table is available
5804 for DW_AT_decl_file.
5805 We don't need the pc/line-number mapping for type units. */
5806 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5807
348e048f
DE
5808 /* Process the dies in the type unit. */
5809 if (die->child == NULL)
5810 {
5811 dump_die_for_error (die);
5812 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5813 bfd_get_filename (abfd));
5814 }
5815
5816 child_die = die->child;
5817
5818 while (child_die && child_die->tag)
5819 {
5820 process_die (child_die, cu);
5821
5822 child_die = sibling_die (child_die);
5823 }
5824
5825 do_cleanups (back_to);
5826}
5827
d389af10
JK
5828/* qsort helper for inherit_abstract_dies. */
5829
5830static int
5831unsigned_int_compar (const void *ap, const void *bp)
5832{
5833 unsigned int a = *(unsigned int *) ap;
5834 unsigned int b = *(unsigned int *) bp;
5835
5836 return (a > b) - (b > a);
5837}
5838
5839/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5840 Inherit only the children of the DW_AT_abstract_origin DIE not being
5841 already referenced by DW_AT_abstract_origin from the children of the
5842 current DIE. */
d389af10
JK
5843
5844static void
5845inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5846{
5847 struct die_info *child_die;
5848 unsigned die_children_count;
5849 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
5850 sect_offset *offsets;
5851 sect_offset *offsets_end, *offsetp;
d389af10
JK
5852 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5853 struct die_info *origin_die;
5854 /* Iterator of the ORIGIN_DIE children. */
5855 struct die_info *origin_child_die;
5856 struct cleanup *cleanups;
5857 struct attribute *attr;
cd02d79d
PA
5858 struct dwarf2_cu *origin_cu;
5859 struct pending **origin_previous_list_in_scope;
d389af10
JK
5860
5861 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5862 if (!attr)
5863 return;
5864
cd02d79d
PA
5865 /* Note that following die references may follow to a die in a
5866 different cu. */
5867
5868 origin_cu = cu;
5869 origin_die = follow_die_ref (die, attr, &origin_cu);
5870
5871 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5872 symbols in. */
5873 origin_previous_list_in_scope = origin_cu->list_in_scope;
5874 origin_cu->list_in_scope = cu->list_in_scope;
5875
edb3359d
DJ
5876 if (die->tag != origin_die->tag
5877 && !(die->tag == DW_TAG_inlined_subroutine
5878 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5879 complaint (&symfile_complaints,
5880 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 5881 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
5882
5883 child_die = die->child;
5884 die_children_count = 0;
5885 while (child_die && child_die->tag)
5886 {
5887 child_die = sibling_die (child_die);
5888 die_children_count++;
5889 }
5890 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5891 cleanups = make_cleanup (xfree, offsets);
5892
5893 offsets_end = offsets;
5894 child_die = die->child;
5895 while (child_die && child_die->tag)
5896 {
c38f313d
DJ
5897 /* For each CHILD_DIE, find the corresponding child of
5898 ORIGIN_DIE. If there is more than one layer of
5899 DW_AT_abstract_origin, follow them all; there shouldn't be,
5900 but GCC versions at least through 4.4 generate this (GCC PR
5901 40573). */
5902 struct die_info *child_origin_die = child_die;
cd02d79d 5903 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5904
c38f313d
DJ
5905 while (1)
5906 {
cd02d79d
PA
5907 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5908 child_origin_cu);
c38f313d
DJ
5909 if (attr == NULL)
5910 break;
cd02d79d
PA
5911 child_origin_die = follow_die_ref (child_origin_die, attr,
5912 &child_origin_cu);
c38f313d
DJ
5913 }
5914
d389af10
JK
5915 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5916 counterpart may exist. */
c38f313d 5917 if (child_origin_die != child_die)
d389af10 5918 {
edb3359d
DJ
5919 if (child_die->tag != child_origin_die->tag
5920 && !(child_die->tag == DW_TAG_inlined_subroutine
5921 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5922 complaint (&symfile_complaints,
5923 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
5924 "different tags"), child_die->offset.sect_off,
5925 child_origin_die->offset.sect_off);
c38f313d
DJ
5926 if (child_origin_die->parent != origin_die)
5927 complaint (&symfile_complaints,
5928 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
5929 "different parents"), child_die->offset.sect_off,
5930 child_origin_die->offset.sect_off);
c38f313d
DJ
5931 else
5932 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5933 }
5934 child_die = sibling_die (child_die);
5935 }
5936 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5937 unsigned_int_compar);
5938 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 5939 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
5940 complaint (&symfile_complaints,
5941 _("Multiple children of DIE 0x%x refer "
5942 "to DIE 0x%x as their abstract origin"),
b64f50a1 5943 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
5944
5945 offsetp = offsets;
5946 origin_child_die = origin_die->child;
5947 while (origin_child_die && origin_child_die->tag)
5948 {
5949 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
5950 while (offsetp < offsets_end
5951 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 5952 offsetp++;
b64f50a1
JK
5953 if (offsetp >= offsets_end
5954 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
5955 {
5956 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5957 process_die (origin_child_die, origin_cu);
d389af10
JK
5958 }
5959 origin_child_die = sibling_die (origin_child_die);
5960 }
cd02d79d 5961 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5962
5963 do_cleanups (cleanups);
5964}
5965
c906108c 5966static void
e7c27a73 5967read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5968{
e7c27a73 5969 struct objfile *objfile = cu->objfile;
52f0bd74 5970 struct context_stack *new;
c906108c
SS
5971 CORE_ADDR lowpc;
5972 CORE_ADDR highpc;
5973 struct die_info *child_die;
edb3359d 5974 struct attribute *attr, *call_line, *call_file;
c906108c 5975 char *name;
e142c38c 5976 CORE_ADDR baseaddr;
801e3a5b 5977 struct block *block;
edb3359d 5978 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5979 VEC (symbolp) *template_args = NULL;
5980 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5981
5982 if (inlined_func)
5983 {
5984 /* If we do not have call site information, we can't show the
5985 caller of this inlined function. That's too confusing, so
5986 only use the scope for local variables. */
5987 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5988 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5989 if (call_line == NULL || call_file == NULL)
5990 {
5991 read_lexical_block_scope (die, cu);
5992 return;
5993 }
5994 }
c906108c 5995
e142c38c
DJ
5996 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5997
94af9270 5998 name = dwarf2_name (die, cu);
c906108c 5999
e8d05480
JB
6000 /* Ignore functions with missing or empty names. These are actually
6001 illegal according to the DWARF standard. */
6002 if (name == NULL)
6003 {
6004 complaint (&symfile_complaints,
b64f50a1
JK
6005 _("missing name for subprogram DIE at %d"),
6006 die->offset.sect_off);
e8d05480
JB
6007 return;
6008 }
6009
6010 /* Ignore functions with missing or invalid low and high pc attributes. */
6011 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6012 {
ae4d0c03
PM
6013 attr = dwarf2_attr (die, DW_AT_external, cu);
6014 if (!attr || !DW_UNSND (attr))
6015 complaint (&symfile_complaints,
3e43a32a
MS
6016 _("cannot get low and high bounds "
6017 "for subprogram DIE at %d"),
b64f50a1 6018 die->offset.sect_off);
e8d05480
JB
6019 return;
6020 }
c906108c
SS
6021
6022 lowpc += baseaddr;
6023 highpc += baseaddr;
6024
34eaf542
TT
6025 /* If we have any template arguments, then we must allocate a
6026 different sort of symbol. */
6027 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6028 {
6029 if (child_die->tag == DW_TAG_template_type_param
6030 || child_die->tag == DW_TAG_template_value_param)
6031 {
6032 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6033 struct template_symbol);
6034 templ_func->base.is_cplus_template_function = 1;
6035 break;
6036 }
6037 }
6038
c906108c 6039 new = push_context (0, lowpc);
34eaf542
TT
6040 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6041 (struct symbol *) templ_func);
4c2df51b 6042
4cecd739
DJ
6043 /* If there is a location expression for DW_AT_frame_base, record
6044 it. */
e142c38c 6045 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6046 if (attr)
c034e007
AC
6047 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6048 expression is being recorded directly in the function's symbol
6049 and not in a separate frame-base object. I guess this hack is
6050 to avoid adding some sort of frame-base adjunct/annex to the
6051 function's symbol :-(. The problem with doing this is that it
6052 results in a function symbol with a location expression that
6053 has nothing to do with the location of the function, ouch! The
6054 relationship should be: a function's symbol has-a frame base; a
6055 frame-base has-a location expression. */
e7c27a73 6056 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6057
e142c38c 6058 cu->list_in_scope = &local_symbols;
c906108c 6059
639d11d3 6060 if (die->child != NULL)
c906108c 6061 {
639d11d3 6062 child_die = die->child;
c906108c
SS
6063 while (child_die && child_die->tag)
6064 {
34eaf542
TT
6065 if (child_die->tag == DW_TAG_template_type_param
6066 || child_die->tag == DW_TAG_template_value_param)
6067 {
6068 struct symbol *arg = new_symbol (child_die, NULL, cu);
6069
f1078f66
DJ
6070 if (arg != NULL)
6071 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6072 }
6073 else
6074 process_die (child_die, cu);
c906108c
SS
6075 child_die = sibling_die (child_die);
6076 }
6077 }
6078
d389af10
JK
6079 inherit_abstract_dies (die, cu);
6080
4a811a97
UW
6081 /* If we have a DW_AT_specification, we might need to import using
6082 directives from the context of the specification DIE. See the
6083 comment in determine_prefix. */
6084 if (cu->language == language_cplus
6085 && dwarf2_attr (die, DW_AT_specification, cu))
6086 {
6087 struct dwarf2_cu *spec_cu = cu;
6088 struct die_info *spec_die = die_specification (die, &spec_cu);
6089
6090 while (spec_die)
6091 {
6092 child_die = spec_die->child;
6093 while (child_die && child_die->tag)
6094 {
6095 if (child_die->tag == DW_TAG_imported_module)
6096 process_die (child_die, spec_cu);
6097 child_die = sibling_die (child_die);
6098 }
6099
6100 /* In some cases, GCC generates specification DIEs that
6101 themselves contain DW_AT_specification attributes. */
6102 spec_die = die_specification (spec_die, &spec_cu);
6103 }
6104 }
6105
c906108c
SS
6106 new = pop_context ();
6107 /* Make a block for the local symbols within. */
801e3a5b
JB
6108 block = finish_block (new->name, &local_symbols, new->old_blocks,
6109 lowpc, highpc, objfile);
6110
df8a16a1 6111 /* For C++, set the block's scope. */
f55ee35c 6112 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6113 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6114 determine_prefix (die, cu),
df8a16a1
DJ
6115 processing_has_namespace_info);
6116
801e3a5b
JB
6117 /* If we have address ranges, record them. */
6118 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6119
34eaf542
TT
6120 /* Attach template arguments to function. */
6121 if (! VEC_empty (symbolp, template_args))
6122 {
6123 gdb_assert (templ_func != NULL);
6124
6125 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6126 templ_func->template_arguments
6127 = obstack_alloc (&objfile->objfile_obstack,
6128 (templ_func->n_template_arguments
6129 * sizeof (struct symbol *)));
6130 memcpy (templ_func->template_arguments,
6131 VEC_address (symbolp, template_args),
6132 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6133 VEC_free (symbolp, template_args);
6134 }
6135
208d8187
JB
6136 /* In C++, we can have functions nested inside functions (e.g., when
6137 a function declares a class that has methods). This means that
6138 when we finish processing a function scope, we may need to go
6139 back to building a containing block's symbol lists. */
6140 local_symbols = new->locals;
6141 param_symbols = new->params;
27aa8d6a 6142 using_directives = new->using_directives;
208d8187 6143
921e78cf
JB
6144 /* If we've finished processing a top-level function, subsequent
6145 symbols go in the file symbol list. */
6146 if (outermost_context_p ())
e142c38c 6147 cu->list_in_scope = &file_symbols;
c906108c
SS
6148}
6149
6150/* Process all the DIES contained within a lexical block scope. Start
6151 a new scope, process the dies, and then close the scope. */
6152
6153static void
e7c27a73 6154read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6155{
e7c27a73 6156 struct objfile *objfile = cu->objfile;
52f0bd74 6157 struct context_stack *new;
c906108c
SS
6158 CORE_ADDR lowpc, highpc;
6159 struct die_info *child_die;
e142c38c
DJ
6160 CORE_ADDR baseaddr;
6161
6162 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6163
6164 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6165 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6166 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6167 be nasty. Might be easier to properly extend generic blocks to
af34e669 6168 describe ranges. */
d85a05f0 6169 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6170 return;
6171 lowpc += baseaddr;
6172 highpc += baseaddr;
6173
6174 push_context (0, lowpc);
639d11d3 6175 if (die->child != NULL)
c906108c 6176 {
639d11d3 6177 child_die = die->child;
c906108c
SS
6178 while (child_die && child_die->tag)
6179 {
e7c27a73 6180 process_die (child_die, cu);
c906108c
SS
6181 child_die = sibling_die (child_die);
6182 }
6183 }
6184 new = pop_context ();
6185
8540c487 6186 if (local_symbols != NULL || using_directives != NULL)
c906108c 6187 {
801e3a5b
JB
6188 struct block *block
6189 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6190 highpc, objfile);
6191
6192 /* Note that recording ranges after traversing children, as we
6193 do here, means that recording a parent's ranges entails
6194 walking across all its children's ranges as they appear in
6195 the address map, which is quadratic behavior.
6196
6197 It would be nicer to record the parent's ranges before
6198 traversing its children, simply overriding whatever you find
6199 there. But since we don't even decide whether to create a
6200 block until after we've traversed its children, that's hard
6201 to do. */
6202 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6203 }
6204 local_symbols = new->locals;
27aa8d6a 6205 using_directives = new->using_directives;
c906108c
SS
6206}
6207
96408a79
SA
6208/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6209
6210static void
6211read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6212{
6213 struct objfile *objfile = cu->objfile;
6214 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6215 CORE_ADDR pc, baseaddr;
6216 struct attribute *attr;
6217 struct call_site *call_site, call_site_local;
6218 void **slot;
6219 int nparams;
6220 struct die_info *child_die;
6221
6222 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6223
6224 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6225 if (!attr)
6226 {
6227 complaint (&symfile_complaints,
6228 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6229 "DIE 0x%x [in module %s]"),
b64f50a1 6230 die->offset.sect_off, objfile->name);
96408a79
SA
6231 return;
6232 }
6233 pc = DW_ADDR (attr) + baseaddr;
6234
6235 if (cu->call_site_htab == NULL)
6236 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6237 NULL, &objfile->objfile_obstack,
6238 hashtab_obstack_allocate, NULL);
6239 call_site_local.pc = pc;
6240 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6241 if (*slot != NULL)
6242 {
6243 complaint (&symfile_complaints,
6244 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6245 "DIE 0x%x [in module %s]"),
b64f50a1 6246 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
6247 return;
6248 }
6249
6250 /* Count parameters at the caller. */
6251
6252 nparams = 0;
6253 for (child_die = die->child; child_die && child_die->tag;
6254 child_die = sibling_die (child_die))
6255 {
6256 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6257 {
6258 complaint (&symfile_complaints,
6259 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6260 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6261 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
6262 continue;
6263 }
6264
6265 nparams++;
6266 }
6267
6268 call_site = obstack_alloc (&objfile->objfile_obstack,
6269 (sizeof (*call_site)
6270 + (sizeof (*call_site->parameter)
6271 * (nparams - 1))));
6272 *slot = call_site;
6273 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6274 call_site->pc = pc;
6275
6276 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6277 {
6278 struct die_info *func_die;
6279
6280 /* Skip also over DW_TAG_inlined_subroutine. */
6281 for (func_die = die->parent;
6282 func_die && func_die->tag != DW_TAG_subprogram
6283 && func_die->tag != DW_TAG_subroutine_type;
6284 func_die = func_die->parent);
6285
6286 /* DW_AT_GNU_all_call_sites is a superset
6287 of DW_AT_GNU_all_tail_call_sites. */
6288 if (func_die
6289 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6290 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6291 {
6292 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6293 not complete. But keep CALL_SITE for look ups via call_site_htab,
6294 both the initial caller containing the real return address PC and
6295 the final callee containing the current PC of a chain of tail
6296 calls do not need to have the tail call list complete. But any
6297 function candidate for a virtual tail call frame searched via
6298 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6299 determined unambiguously. */
6300 }
6301 else
6302 {
6303 struct type *func_type = NULL;
6304
6305 if (func_die)
6306 func_type = get_die_type (func_die, cu);
6307 if (func_type != NULL)
6308 {
6309 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6310
6311 /* Enlist this call site to the function. */
6312 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6313 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6314 }
6315 else
6316 complaint (&symfile_complaints,
6317 _("Cannot find function owning DW_TAG_GNU_call_site "
6318 "DIE 0x%x [in module %s]"),
b64f50a1 6319 die->offset.sect_off, objfile->name);
96408a79
SA
6320 }
6321 }
6322
6323 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6324 if (attr == NULL)
6325 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6326 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6327 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6328 /* Keep NULL DWARF_BLOCK. */;
6329 else if (attr_form_is_block (attr))
6330 {
6331 struct dwarf2_locexpr_baton *dlbaton;
6332
6333 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6334 dlbaton->data = DW_BLOCK (attr)->data;
6335 dlbaton->size = DW_BLOCK (attr)->size;
6336 dlbaton->per_cu = cu->per_cu;
6337
6338 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6339 }
6340 else if (is_ref_attr (attr))
6341 {
96408a79
SA
6342 struct dwarf2_cu *target_cu = cu;
6343 struct die_info *target_die;
6344
6345 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6346 gdb_assert (target_cu->objfile == objfile);
6347 if (die_is_declaration (target_die, target_cu))
6348 {
6349 const char *target_physname;
6350
6351 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6352 if (target_physname == NULL)
6353 complaint (&symfile_complaints,
6354 _("DW_AT_GNU_call_site_target target DIE has invalid "
6355 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6356 die->offset.sect_off, objfile->name);
96408a79
SA
6357 else
6358 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6359 }
6360 else
6361 {
6362 CORE_ADDR lowpc;
6363
6364 /* DW_AT_entry_pc should be preferred. */
6365 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6366 complaint (&symfile_complaints,
6367 _("DW_AT_GNU_call_site_target target DIE has invalid "
6368 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6369 die->offset.sect_off, objfile->name);
96408a79
SA
6370 else
6371 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6372 }
6373 }
6374 else
6375 complaint (&symfile_complaints,
6376 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6377 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 6378 die->offset.sect_off, objfile->name);
96408a79
SA
6379
6380 call_site->per_cu = cu->per_cu;
6381
6382 for (child_die = die->child;
6383 child_die && child_die->tag;
6384 child_die = sibling_die (child_die))
6385 {
6386 struct dwarf2_locexpr_baton *dlbaton;
6387 struct call_site_parameter *parameter;
6388
6389 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6390 {
6391 /* Already printed the complaint above. */
6392 continue;
6393 }
6394
6395 gdb_assert (call_site->parameter_count < nparams);
6396 parameter = &call_site->parameter[call_site->parameter_count];
6397
6398 /* DW_AT_location specifies the register number. Value of the data
6399 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6400
6401 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6402 if (!attr || !attr_form_is_block (attr))
6403 {
6404 complaint (&symfile_complaints,
6405 _("No DW_FORM_block* DW_AT_location for "
6406 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6407 child_die->offset.sect_off, objfile->name);
96408a79
SA
6408 continue;
6409 }
6410 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6411 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6412 if (parameter->dwarf_reg == -1
6413 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6414 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6415 &parameter->fb_offset))
6416 {
6417 complaint (&symfile_complaints,
6418 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6419 "for DW_FORM_block* DW_AT_location for "
6420 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6421 child_die->offset.sect_off, objfile->name);
96408a79
SA
6422 continue;
6423 }
6424
6425 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6426 if (!attr_form_is_block (attr))
6427 {
6428 complaint (&symfile_complaints,
6429 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6430 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6431 child_die->offset.sect_off, objfile->name);
96408a79
SA
6432 continue;
6433 }
6434 parameter->value = DW_BLOCK (attr)->data;
6435 parameter->value_size = DW_BLOCK (attr)->size;
6436
6437 /* Parameters are not pre-cleared by memset above. */
6438 parameter->data_value = NULL;
6439 parameter->data_value_size = 0;
6440 call_site->parameter_count++;
6441
6442 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6443 if (attr)
6444 {
6445 if (!attr_form_is_block (attr))
6446 complaint (&symfile_complaints,
6447 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6448 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6449 child_die->offset.sect_off, objfile->name);
96408a79
SA
6450 else
6451 {
6452 parameter->data_value = DW_BLOCK (attr)->data;
6453 parameter->data_value_size = DW_BLOCK (attr)->size;
6454 }
6455 }
6456 }
6457}
6458
43039443 6459/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6460 Return 1 if the attributes are present and valid, otherwise, return 0.
6461 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6462
6463static int
6464dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6465 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6466 struct partial_symtab *ranges_pst)
43039443
JK
6467{
6468 struct objfile *objfile = cu->objfile;
6469 struct comp_unit_head *cu_header = &cu->header;
6470 bfd *obfd = objfile->obfd;
6471 unsigned int addr_size = cu_header->addr_size;
6472 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6473 /* Base address selection entry. */
6474 CORE_ADDR base;
6475 int found_base;
6476 unsigned int dummy;
6477 gdb_byte *buffer;
6478 CORE_ADDR marker;
6479 int low_set;
6480 CORE_ADDR low = 0;
6481 CORE_ADDR high = 0;
ff013f42 6482 CORE_ADDR baseaddr;
43039443 6483
d00adf39
DE
6484 found_base = cu->base_known;
6485 base = cu->base_address;
43039443 6486
be391dca 6487 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6488 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6489 {
6490 complaint (&symfile_complaints,
6491 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6492 offset);
6493 return 0;
6494 }
dce234bc 6495 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6496
6497 /* Read in the largest possible address. */
6498 marker = read_address (obfd, buffer, cu, &dummy);
6499 if ((marker & mask) == mask)
6500 {
6501 /* If we found the largest possible address, then
6502 read the base address. */
6503 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6504 buffer += 2 * addr_size;
6505 offset += 2 * addr_size;
6506 found_base = 1;
6507 }
6508
6509 low_set = 0;
6510
e7030f15 6511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6512
43039443
JK
6513 while (1)
6514 {
6515 CORE_ADDR range_beginning, range_end;
6516
6517 range_beginning = read_address (obfd, buffer, cu, &dummy);
6518 buffer += addr_size;
6519 range_end = read_address (obfd, buffer, cu, &dummy);
6520 buffer += addr_size;
6521 offset += 2 * addr_size;
6522
6523 /* An end of list marker is a pair of zero addresses. */
6524 if (range_beginning == 0 && range_end == 0)
6525 /* Found the end of list entry. */
6526 break;
6527
6528 /* Each base address selection entry is a pair of 2 values.
6529 The first is the largest possible address, the second is
6530 the base address. Check for a base address here. */
6531 if ((range_beginning & mask) == mask)
6532 {
6533 /* If we found the largest possible address, then
6534 read the base address. */
6535 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6536 found_base = 1;
6537 continue;
6538 }
6539
6540 if (!found_base)
6541 {
6542 /* We have no valid base address for the ranges
6543 data. */
6544 complaint (&symfile_complaints,
6545 _("Invalid .debug_ranges data (no base address)"));
6546 return 0;
6547 }
6548
9277c30c
UW
6549 if (range_beginning > range_end)
6550 {
6551 /* Inverted range entries are invalid. */
6552 complaint (&symfile_complaints,
6553 _("Invalid .debug_ranges data (inverted range)"));
6554 return 0;
6555 }
6556
6557 /* Empty range entries have no effect. */
6558 if (range_beginning == range_end)
6559 continue;
6560
43039443
JK
6561 range_beginning += base;
6562 range_end += base;
6563
9277c30c 6564 if (ranges_pst != NULL)
ff013f42 6565 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6566 range_beginning + baseaddr,
6567 range_end - 1 + baseaddr,
ff013f42
JK
6568 ranges_pst);
6569
43039443
JK
6570 /* FIXME: This is recording everything as a low-high
6571 segment of consecutive addresses. We should have a
6572 data structure for discontiguous block ranges
6573 instead. */
6574 if (! low_set)
6575 {
6576 low = range_beginning;
6577 high = range_end;
6578 low_set = 1;
6579 }
6580 else
6581 {
6582 if (range_beginning < low)
6583 low = range_beginning;
6584 if (range_end > high)
6585 high = range_end;
6586 }
6587 }
6588
6589 if (! low_set)
6590 /* If the first entry is an end-of-list marker, the range
6591 describes an empty scope, i.e. no instructions. */
6592 return 0;
6593
6594 if (low_return)
6595 *low_return = low;
6596 if (high_return)
6597 *high_return = high;
6598 return 1;
6599}
6600
af34e669
DJ
6601/* Get low and high pc attributes from a die. Return 1 if the attributes
6602 are present and valid, otherwise, return 0. Return -1 if the range is
6603 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 6604
c906108c 6605static int
af34e669 6606dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6607 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6608 struct partial_symtab *pst)
c906108c
SS
6609{
6610 struct attribute *attr;
af34e669
DJ
6611 CORE_ADDR low = 0;
6612 CORE_ADDR high = 0;
6613 int ret = 0;
c906108c 6614
e142c38c 6615 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6616 if (attr)
af34e669
DJ
6617 {
6618 high = DW_ADDR (attr);
e142c38c 6619 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6620 if (attr)
6621 low = DW_ADDR (attr);
6622 else
6623 /* Found high w/o low attribute. */
6624 return 0;
6625
6626 /* Found consecutive range of addresses. */
6627 ret = 1;
6628 }
c906108c 6629 else
af34e669 6630 {
e142c38c 6631 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6632 if (attr != NULL)
6633 {
af34e669 6634 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6635 .debug_ranges section. */
d85a05f0 6636 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6637 return 0;
43039443 6638 /* Found discontinuous range of addresses. */
af34e669
DJ
6639 ret = -1;
6640 }
6641 }
c906108c 6642
9373cf26
JK
6643 /* read_partial_die has also the strict LOW < HIGH requirement. */
6644 if (high <= low)
c906108c
SS
6645 return 0;
6646
6647 /* When using the GNU linker, .gnu.linkonce. sections are used to
6648 eliminate duplicate copies of functions and vtables and such.
6649 The linker will arbitrarily choose one and discard the others.
6650 The AT_*_pc values for such functions refer to local labels in
6651 these sections. If the section from that file was discarded, the
6652 labels are not in the output, so the relocs get a value of 0.
6653 If this is a discarded function, mark the pc bounds as invalid,
6654 so that GDB will ignore it. */
72dca2f5 6655 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6656 return 0;
6657
6658 *lowpc = low;
96408a79
SA
6659 if (highpc)
6660 *highpc = high;
af34e669 6661 return ret;
c906108c
SS
6662}
6663
b084d499
JB
6664/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6665 its low and high PC addresses. Do nothing if these addresses could not
6666 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6667 and HIGHPC to the high address if greater than HIGHPC. */
6668
6669static void
6670dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6671 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6672 struct dwarf2_cu *cu)
6673{
6674 CORE_ADDR low, high;
6675 struct die_info *child = die->child;
6676
d85a05f0 6677 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6678 {
6679 *lowpc = min (*lowpc, low);
6680 *highpc = max (*highpc, high);
6681 }
6682
6683 /* If the language does not allow nested subprograms (either inside
6684 subprograms or lexical blocks), we're done. */
6685 if (cu->language != language_ada)
6686 return;
6e70227d 6687
b084d499
JB
6688 /* Check all the children of the given DIE. If it contains nested
6689 subprograms, then check their pc bounds. Likewise, we need to
6690 check lexical blocks as well, as they may also contain subprogram
6691 definitions. */
6692 while (child && child->tag)
6693 {
6694 if (child->tag == DW_TAG_subprogram
6695 || child->tag == DW_TAG_lexical_block)
6696 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6697 child = sibling_die (child);
6698 }
6699}
6700
fae299cd
DC
6701/* Get the low and high pc's represented by the scope DIE, and store
6702 them in *LOWPC and *HIGHPC. If the correct values can't be
6703 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6704
6705static void
6706get_scope_pc_bounds (struct die_info *die,
6707 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6708 struct dwarf2_cu *cu)
6709{
6710 CORE_ADDR best_low = (CORE_ADDR) -1;
6711 CORE_ADDR best_high = (CORE_ADDR) 0;
6712 CORE_ADDR current_low, current_high;
6713
d85a05f0 6714 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6715 {
6716 best_low = current_low;
6717 best_high = current_high;
6718 }
6719 else
6720 {
6721 struct die_info *child = die->child;
6722
6723 while (child && child->tag)
6724 {
6725 switch (child->tag) {
6726 case DW_TAG_subprogram:
b084d499 6727 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6728 break;
6729 case DW_TAG_namespace:
f55ee35c 6730 case DW_TAG_module:
fae299cd
DC
6731 /* FIXME: carlton/2004-01-16: Should we do this for
6732 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6733 that current GCC's always emit the DIEs corresponding
6734 to definitions of methods of classes as children of a
6735 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6736 the DIEs giving the declarations, which could be
6737 anywhere). But I don't see any reason why the
6738 standards says that they have to be there. */
6739 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6740
6741 if (current_low != ((CORE_ADDR) -1))
6742 {
6743 best_low = min (best_low, current_low);
6744 best_high = max (best_high, current_high);
6745 }
6746 break;
6747 default:
0963b4bd 6748 /* Ignore. */
fae299cd
DC
6749 break;
6750 }
6751
6752 child = sibling_die (child);
6753 }
6754 }
6755
6756 *lowpc = best_low;
6757 *highpc = best_high;
6758}
6759
801e3a5b
JB
6760/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6761 in DIE. */
380bca97 6762
801e3a5b
JB
6763static void
6764dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6765 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6766{
bb5ed363 6767 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6768 struct attribute *attr;
6769
6770 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6771 if (attr)
6772 {
6773 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6774
801e3a5b
JB
6775 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6776 if (attr)
6777 {
6778 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6779
801e3a5b
JB
6780 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6781 }
6782 }
6783
6784 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6785 if (attr)
6786 {
bb5ed363 6787 bfd *obfd = objfile->obfd;
801e3a5b
JB
6788
6789 /* The value of the DW_AT_ranges attribute is the offset of the
6790 address range list in the .debug_ranges section. */
6791 unsigned long offset = DW_UNSND (attr);
dce234bc 6792 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6793
6794 /* For some target architectures, but not others, the
6795 read_address function sign-extends the addresses it returns.
6796 To recognize base address selection entries, we need a
6797 mask. */
6798 unsigned int addr_size = cu->header.addr_size;
6799 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6800
6801 /* The base address, to which the next pair is relative. Note
6802 that this 'base' is a DWARF concept: most entries in a range
6803 list are relative, to reduce the number of relocs against the
6804 debugging information. This is separate from this function's
6805 'baseaddr' argument, which GDB uses to relocate debugging
6806 information from a shared library based on the address at
6807 which the library was loaded. */
d00adf39
DE
6808 CORE_ADDR base = cu->base_address;
6809 int base_known = cu->base_known;
801e3a5b 6810
be391dca 6811 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6812 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6813 {
6814 complaint (&symfile_complaints,
6815 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6816 offset);
6817 return;
6818 }
6819
6820 for (;;)
6821 {
6822 unsigned int bytes_read;
6823 CORE_ADDR start, end;
6824
6825 start = read_address (obfd, buffer, cu, &bytes_read);
6826 buffer += bytes_read;
6827 end = read_address (obfd, buffer, cu, &bytes_read);
6828 buffer += bytes_read;
6829
6830 /* Did we find the end of the range list? */
6831 if (start == 0 && end == 0)
6832 break;
6833
6834 /* Did we find a base address selection entry? */
6835 else if ((start & base_select_mask) == base_select_mask)
6836 {
6837 base = end;
6838 base_known = 1;
6839 }
6840
6841 /* We found an ordinary address range. */
6842 else
6843 {
6844 if (!base_known)
6845 {
6846 complaint (&symfile_complaints,
3e43a32a
MS
6847 _("Invalid .debug_ranges data "
6848 "(no base address)"));
801e3a5b
JB
6849 return;
6850 }
6851
9277c30c
UW
6852 if (start > end)
6853 {
6854 /* Inverted range entries are invalid. */
6855 complaint (&symfile_complaints,
6856 _("Invalid .debug_ranges data "
6857 "(inverted range)"));
6858 return;
6859 }
6860
6861 /* Empty range entries have no effect. */
6862 if (start == end)
6863 continue;
6864
6e70227d
DE
6865 record_block_range (block,
6866 baseaddr + base + start,
801e3a5b
JB
6867 baseaddr + base + end - 1);
6868 }
6869 }
6870 }
6871}
6872
60d5a603
JK
6873/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6874 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6875 during 4.6.0 experimental. */
6876
6877static int
6878producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6879{
6880 const char *cs;
6881 int major, minor, release;
ba919b58 6882 int result = 0;
60d5a603
JK
6883
6884 if (cu->producer == NULL)
6885 {
6886 /* For unknown compilers expect their behavior is DWARF version
6887 compliant.
6888
6889 GCC started to support .debug_types sections by -gdwarf-4 since
6890 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6891 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6892 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6893 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6894
6895 return 0;
6896 }
6897
ba919b58
TT
6898 if (cu->checked_producer)
6899 return cu->producer_is_gxx_lt_4_6;
6900
60d5a603
JK
6901 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6902
6903 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6904 {
6905 /* For non-GCC compilers expect their behavior is DWARF version
6906 compliant. */
60d5a603 6907 }
ba919b58 6908 else
60d5a603 6909 {
ba919b58
TT
6910 cs = &cu->producer[strlen ("GNU ")];
6911 while (*cs && !isdigit (*cs))
6912 cs++;
6913 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6914 {
6915 /* Not recognized as GCC. */
6916 }
6917 else
6918 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
6919 }
6920
ba919b58
TT
6921 cu->checked_producer = 1;
6922 cu->producer_is_gxx_lt_4_6 = result;
6923
6924 return result;
60d5a603
JK
6925}
6926
6927/* Return the default accessibility type if it is not overriden by
6928 DW_AT_accessibility. */
6929
6930static enum dwarf_access_attribute
6931dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6932{
6933 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6934 {
6935 /* The default DWARF 2 accessibility for members is public, the default
6936 accessibility for inheritance is private. */
6937
6938 if (die->tag != DW_TAG_inheritance)
6939 return DW_ACCESS_public;
6940 else
6941 return DW_ACCESS_private;
6942 }
6943 else
6944 {
6945 /* DWARF 3+ defines the default accessibility a different way. The same
6946 rules apply now for DW_TAG_inheritance as for the members and it only
6947 depends on the container kind. */
6948
6949 if (die->parent->tag == DW_TAG_class_type)
6950 return DW_ACCESS_private;
6951 else
6952 return DW_ACCESS_public;
6953 }
6954}
6955
74ac6d43
TT
6956/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6957 offset. If the attribute was not found return 0, otherwise return
6958 1. If it was found but could not properly be handled, set *OFFSET
6959 to 0. */
6960
6961static int
6962handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6963 LONGEST *offset)
6964{
6965 struct attribute *attr;
6966
6967 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6968 if (attr != NULL)
6969 {
6970 *offset = 0;
6971
6972 /* Note that we do not check for a section offset first here.
6973 This is because DW_AT_data_member_location is new in DWARF 4,
6974 so if we see it, we can assume that a constant form is really
6975 a constant and not a section offset. */
6976 if (attr_form_is_constant (attr))
6977 *offset = dwarf2_get_attr_constant_value (attr, 0);
6978 else if (attr_form_is_section_offset (attr))
6979 dwarf2_complex_location_expr_complaint ();
6980 else if (attr_form_is_block (attr))
6981 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6982 else
6983 dwarf2_complex_location_expr_complaint ();
6984
6985 return 1;
6986 }
6987
6988 return 0;
6989}
6990
c906108c
SS
6991/* Add an aggregate field to the field list. */
6992
6993static void
107d2387 6994dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6995 struct dwarf2_cu *cu)
6e70227d 6996{
e7c27a73 6997 struct objfile *objfile = cu->objfile;
5e2b427d 6998 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6999 struct nextfield *new_field;
7000 struct attribute *attr;
7001 struct field *fp;
7002 char *fieldname = "";
7003
7004 /* Allocate a new field list entry and link it in. */
7005 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 7006 make_cleanup (xfree, new_field);
c906108c 7007 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7008
7009 if (die->tag == DW_TAG_inheritance)
7010 {
7011 new_field->next = fip->baseclasses;
7012 fip->baseclasses = new_field;
7013 }
7014 else
7015 {
7016 new_field->next = fip->fields;
7017 fip->fields = new_field;
7018 }
c906108c
SS
7019 fip->nfields++;
7020
e142c38c 7021 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7022 if (attr)
7023 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7024 else
7025 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7026 if (new_field->accessibility != DW_ACCESS_public)
7027 fip->non_public_fields = 1;
60d5a603 7028
e142c38c 7029 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7030 if (attr)
7031 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7032 else
7033 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7034
7035 fp = &new_field->field;
a9a9bd0f 7036
e142c38c 7037 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7038 {
74ac6d43
TT
7039 LONGEST offset;
7040
a9a9bd0f 7041 /* Data member other than a C++ static data member. */
6e70227d 7042
c906108c 7043 /* Get type of field. */
e7c27a73 7044 fp->type = die_type (die, cu);
c906108c 7045
d6a843b5 7046 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7047
c906108c 7048 /* Get bit size of field (zero if none). */
e142c38c 7049 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7050 if (attr)
7051 {
7052 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7053 }
7054 else
7055 {
7056 FIELD_BITSIZE (*fp) = 0;
7057 }
7058
7059 /* Get bit offset of field. */
74ac6d43
TT
7060 if (handle_data_member_location (die, cu, &offset))
7061 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7062 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7063 if (attr)
7064 {
5e2b427d 7065 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7066 {
7067 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7068 additional bit offset from the MSB of the containing
7069 anonymous object to the MSB of the field. We don't
7070 have to do anything special since we don't need to
7071 know the size of the anonymous object. */
f41f5e61 7072 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
7073 }
7074 else
7075 {
7076 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7077 MSB of the anonymous object, subtract off the number of
7078 bits from the MSB of the field to the MSB of the
7079 object, and then subtract off the number of bits of
7080 the field itself. The result is the bit offset of
7081 the LSB of the field. */
c906108c
SS
7082 int anonymous_size;
7083 int bit_offset = DW_UNSND (attr);
7084
e142c38c 7085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7086 if (attr)
7087 {
7088 /* The size of the anonymous object containing
7089 the bit field is explicit, so use the
7090 indicated size (in bytes). */
7091 anonymous_size = DW_UNSND (attr);
7092 }
7093 else
7094 {
7095 /* The size of the anonymous object containing
7096 the bit field must be inferred from the type
7097 attribute of the data member containing the
7098 bit field. */
7099 anonymous_size = TYPE_LENGTH (fp->type);
7100 }
f41f5e61
PA
7101 SET_FIELD_BITPOS (*fp,
7102 (FIELD_BITPOS (*fp)
7103 + anonymous_size * bits_per_byte
7104 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
7105 }
7106 }
7107
7108 /* Get name of field. */
39cbfefa
DJ
7109 fieldname = dwarf2_name (die, cu);
7110 if (fieldname == NULL)
7111 fieldname = "";
d8151005
DJ
7112
7113 /* The name is already allocated along with this objfile, so we don't
7114 need to duplicate it for the type. */
7115 fp->name = fieldname;
c906108c
SS
7116
7117 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7118 pointer or virtual base class pointer) to private. */
e142c38c 7119 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7120 {
d48cc9dd 7121 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7122 new_field->accessibility = DW_ACCESS_private;
7123 fip->non_public_fields = 1;
7124 }
7125 }
a9a9bd0f 7126 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7127 {
a9a9bd0f
DC
7128 /* C++ static member. */
7129
7130 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7131 is a declaration, but all versions of G++ as of this writing
7132 (so through at least 3.2.1) incorrectly generate
7133 DW_TAG_variable tags. */
6e70227d 7134
ff355380 7135 const char *physname;
c906108c 7136
a9a9bd0f 7137 /* Get name of field. */
39cbfefa
DJ
7138 fieldname = dwarf2_name (die, cu);
7139 if (fieldname == NULL)
c906108c
SS
7140 return;
7141
254e6b9e 7142 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7143 if (attr
7144 /* Only create a symbol if this is an external value.
7145 new_symbol checks this and puts the value in the global symbol
7146 table, which we want. If it is not external, new_symbol
7147 will try to put the value in cu->list_in_scope which is wrong. */
7148 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7149 {
7150 /* A static const member, not much different than an enum as far as
7151 we're concerned, except that we can support more types. */
7152 new_symbol (die, NULL, cu);
7153 }
7154
2df3850c 7155 /* Get physical name. */
ff355380 7156 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7157
d8151005
DJ
7158 /* The name is already allocated along with this objfile, so we don't
7159 need to duplicate it for the type. */
7160 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7161 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7162 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7163 }
7164 else if (die->tag == DW_TAG_inheritance)
7165 {
74ac6d43 7166 LONGEST offset;
d4b96c9a 7167
74ac6d43
TT
7168 /* C++ base class field. */
7169 if (handle_data_member_location (die, cu, &offset))
7170 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7171 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7172 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7173 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7174 fip->nbaseclasses++;
7175 }
7176}
7177
98751a41
JK
7178/* Add a typedef defined in the scope of the FIP's class. */
7179
7180static void
7181dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7182 struct dwarf2_cu *cu)
6e70227d 7183{
98751a41 7184 struct objfile *objfile = cu->objfile;
98751a41
JK
7185 struct typedef_field_list *new_field;
7186 struct attribute *attr;
7187 struct typedef_field *fp;
7188 char *fieldname = "";
7189
7190 /* Allocate a new field list entry and link it in. */
7191 new_field = xzalloc (sizeof (*new_field));
7192 make_cleanup (xfree, new_field);
7193
7194 gdb_assert (die->tag == DW_TAG_typedef);
7195
7196 fp = &new_field->field;
7197
7198 /* Get name of field. */
7199 fp->name = dwarf2_name (die, cu);
7200 if (fp->name == NULL)
7201 return;
7202
7203 fp->type = read_type_die (die, cu);
7204
7205 new_field->next = fip->typedef_field_list;
7206 fip->typedef_field_list = new_field;
7207 fip->typedef_field_list_count++;
7208}
7209
c906108c
SS
7210/* Create the vector of fields, and attach it to the type. */
7211
7212static void
fba45db2 7213dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7214 struct dwarf2_cu *cu)
c906108c
SS
7215{
7216 int nfields = fip->nfields;
7217
7218 /* Record the field count, allocate space for the array of fields,
7219 and create blank accessibility bitfields if necessary. */
7220 TYPE_NFIELDS (type) = nfields;
7221 TYPE_FIELDS (type) = (struct field *)
7222 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7223 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7224
b4ba55a1 7225 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7226 {
7227 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7228
7229 TYPE_FIELD_PRIVATE_BITS (type) =
7230 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7231 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7232
7233 TYPE_FIELD_PROTECTED_BITS (type) =
7234 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7235 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7236
774b6a14
TT
7237 TYPE_FIELD_IGNORE_BITS (type) =
7238 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7239 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7240 }
7241
7242 /* If the type has baseclasses, allocate and clear a bit vector for
7243 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7244 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7245 {
7246 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7247 unsigned char *pointer;
c906108c
SS
7248
7249 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7250 pointer = TYPE_ALLOC (type, num_bytes);
7251 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7252 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7253 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7254 }
7255
3e43a32a
MS
7256 /* Copy the saved-up fields into the field vector. Start from the head of
7257 the list, adding to the tail of the field array, so that they end up in
7258 the same order in the array in which they were added to the list. */
c906108c
SS
7259 while (nfields-- > 0)
7260 {
7d0ccb61
DJ
7261 struct nextfield *fieldp;
7262
7263 if (fip->fields)
7264 {
7265 fieldp = fip->fields;
7266 fip->fields = fieldp->next;
7267 }
7268 else
7269 {
7270 fieldp = fip->baseclasses;
7271 fip->baseclasses = fieldp->next;
7272 }
7273
7274 TYPE_FIELD (type, nfields) = fieldp->field;
7275 switch (fieldp->accessibility)
c906108c 7276 {
c5aa993b 7277 case DW_ACCESS_private:
b4ba55a1
JB
7278 if (cu->language != language_ada)
7279 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7280 break;
c906108c 7281
c5aa993b 7282 case DW_ACCESS_protected:
b4ba55a1
JB
7283 if (cu->language != language_ada)
7284 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7285 break;
c906108c 7286
c5aa993b
JM
7287 case DW_ACCESS_public:
7288 break;
c906108c 7289
c5aa993b
JM
7290 default:
7291 /* Unknown accessibility. Complain and treat it as public. */
7292 {
e2e0b3e5 7293 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7294 fieldp->accessibility);
c5aa993b
JM
7295 }
7296 break;
c906108c
SS
7297 }
7298 if (nfields < fip->nbaseclasses)
7299 {
7d0ccb61 7300 switch (fieldp->virtuality)
c906108c 7301 {
c5aa993b
JM
7302 case DW_VIRTUALITY_virtual:
7303 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7304 if (cu->language == language_ada)
a73c6dcd 7305 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7306 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7307 break;
c906108c
SS
7308 }
7309 }
c906108c
SS
7310 }
7311}
7312
c906108c
SS
7313/* Add a member function to the proper fieldlist. */
7314
7315static void
107d2387 7316dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7317 struct type *type, struct dwarf2_cu *cu)
c906108c 7318{
e7c27a73 7319 struct objfile *objfile = cu->objfile;
c906108c
SS
7320 struct attribute *attr;
7321 struct fnfieldlist *flp;
7322 int i;
7323 struct fn_field *fnp;
7324 char *fieldname;
c906108c 7325 struct nextfnfield *new_fnfield;
f792889a 7326 struct type *this_type;
60d5a603 7327 enum dwarf_access_attribute accessibility;
c906108c 7328
b4ba55a1 7329 if (cu->language == language_ada)
a73c6dcd 7330 error (_("unexpected member function in Ada type"));
b4ba55a1 7331
2df3850c 7332 /* Get name of member function. */
39cbfefa
DJ
7333 fieldname = dwarf2_name (die, cu);
7334 if (fieldname == NULL)
2df3850c 7335 return;
c906108c 7336
c906108c
SS
7337 /* Look up member function name in fieldlist. */
7338 for (i = 0; i < fip->nfnfields; i++)
7339 {
27bfe10e 7340 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7341 break;
7342 }
7343
7344 /* Create new list element if necessary. */
7345 if (i < fip->nfnfields)
7346 flp = &fip->fnfieldlists[i];
7347 else
7348 {
7349 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7350 {
7351 fip->fnfieldlists = (struct fnfieldlist *)
7352 xrealloc (fip->fnfieldlists,
7353 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7354 * sizeof (struct fnfieldlist));
c906108c 7355 if (fip->nfnfields == 0)
c13c43fd 7356 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7357 }
7358 flp = &fip->fnfieldlists[fip->nfnfields];
7359 flp->name = fieldname;
7360 flp->length = 0;
7361 flp->head = NULL;
3da10d80 7362 i = fip->nfnfields++;
c906108c
SS
7363 }
7364
7365 /* Create a new member function field and chain it to the field list
0963b4bd 7366 entry. */
c906108c 7367 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7368 make_cleanup (xfree, new_fnfield);
c906108c
SS
7369 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7370 new_fnfield->next = flp->head;
7371 flp->head = new_fnfield;
7372 flp->length++;
7373
7374 /* Fill in the member function field info. */
7375 fnp = &new_fnfield->fnfield;
3da10d80
KS
7376
7377 /* Delay processing of the physname until later. */
7378 if (cu->language == language_cplus || cu->language == language_java)
7379 {
7380 add_to_method_list (type, i, flp->length - 1, fieldname,
7381 die, cu);
7382 }
7383 else
7384 {
1d06ead6 7385 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7386 fnp->physname = physname ? physname : "";
7387 }
7388
c906108c 7389 fnp->type = alloc_type (objfile);
f792889a
DJ
7390 this_type = read_type_die (die, cu);
7391 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7392 {
f792889a 7393 int nparams = TYPE_NFIELDS (this_type);
c906108c 7394
f792889a 7395 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7396 of the method itself (TYPE_CODE_METHOD). */
7397 smash_to_method_type (fnp->type, type,
f792889a
DJ
7398 TYPE_TARGET_TYPE (this_type),
7399 TYPE_FIELDS (this_type),
7400 TYPE_NFIELDS (this_type),
7401 TYPE_VARARGS (this_type));
c906108c
SS
7402
7403 /* Handle static member functions.
c5aa993b 7404 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7405 member functions. G++ helps GDB by marking the first
7406 parameter for non-static member functions (which is the this
7407 pointer) as artificial. We obtain this information from
7408 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7409 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7410 fnp->voffset = VOFFSET_STATIC;
7411 }
7412 else
e2e0b3e5 7413 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7414 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7415
7416 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7417 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7418 fnp->fcontext = die_containing_type (die, cu);
c906108c 7419
3e43a32a
MS
7420 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7421 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7422
7423 /* Get accessibility. */
e142c38c 7424 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7425 if (attr)
60d5a603
JK
7426 accessibility = DW_UNSND (attr);
7427 else
7428 accessibility = dwarf2_default_access_attribute (die, cu);
7429 switch (accessibility)
c906108c 7430 {
60d5a603
JK
7431 case DW_ACCESS_private:
7432 fnp->is_private = 1;
7433 break;
7434 case DW_ACCESS_protected:
7435 fnp->is_protected = 1;
7436 break;
c906108c
SS
7437 }
7438
b02dede2 7439 /* Check for artificial methods. */
e142c38c 7440 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7441 if (attr && DW_UNSND (attr) != 0)
7442 fnp->is_artificial = 1;
7443
0d564a31 7444 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7445 function. For older versions of GCC, this is an offset in the
7446 appropriate virtual table, as specified by DW_AT_containing_type.
7447 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7448 to the object address. */
7449
e142c38c 7450 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7451 if (attr)
8e19ed76 7452 {
aec5aa8b 7453 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7454 {
aec5aa8b
TT
7455 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7456 {
7457 /* Old-style GCC. */
7458 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7459 }
7460 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7461 || (DW_BLOCK (attr)->size > 1
7462 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7463 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7464 {
7465 struct dwarf_block blk;
7466 int offset;
7467
7468 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7469 ? 1 : 2);
7470 blk.size = DW_BLOCK (attr)->size - offset;
7471 blk.data = DW_BLOCK (attr)->data + offset;
7472 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7473 if ((fnp->voffset % cu->header.addr_size) != 0)
7474 dwarf2_complex_location_expr_complaint ();
7475 else
7476 fnp->voffset /= cu->header.addr_size;
7477 fnp->voffset += 2;
7478 }
7479 else
7480 dwarf2_complex_location_expr_complaint ();
7481
7482 if (!fnp->fcontext)
7483 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7484 }
3690dd37 7485 else if (attr_form_is_section_offset (attr))
8e19ed76 7486 {
4d3c2250 7487 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7488 }
7489 else
7490 {
4d3c2250
KB
7491 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7492 fieldname);
8e19ed76 7493 }
0d564a31 7494 }
d48cc9dd
DJ
7495 else
7496 {
7497 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7498 if (attr && DW_UNSND (attr))
7499 {
7500 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7501 complaint (&symfile_complaints,
3e43a32a
MS
7502 _("Member function \"%s\" (offset %d) is virtual "
7503 "but the vtable offset is not specified"),
b64f50a1 7504 fieldname, die->offset.sect_off);
9655fd1a 7505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7506 TYPE_CPLUS_DYNAMIC (type) = 1;
7507 }
7508 }
c906108c
SS
7509}
7510
7511/* Create the vector of member function fields, and attach it to the type. */
7512
7513static void
fba45db2 7514dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7515 struct dwarf2_cu *cu)
c906108c
SS
7516{
7517 struct fnfieldlist *flp;
c906108c
SS
7518 int i;
7519
b4ba55a1 7520 if (cu->language == language_ada)
a73c6dcd 7521 error (_("unexpected member functions in Ada type"));
b4ba55a1 7522
c906108c
SS
7523 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7524 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7525 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7526
7527 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7528 {
7529 struct nextfnfield *nfp = flp->head;
7530 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7531 int k;
7532
7533 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7534 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7535 fn_flp->fn_fields = (struct fn_field *)
7536 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7537 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7538 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7539 }
7540
7541 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7542}
7543
1168df01
JB
7544/* Returns non-zero if NAME is the name of a vtable member in CU's
7545 language, zero otherwise. */
7546static int
7547is_vtable_name (const char *name, struct dwarf2_cu *cu)
7548{
7549 static const char vptr[] = "_vptr";
987504bb 7550 static const char vtable[] = "vtable";
1168df01 7551
987504bb
JJ
7552 /* Look for the C++ and Java forms of the vtable. */
7553 if ((cu->language == language_java
7554 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7555 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7556 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7557 return 1;
7558
7559 return 0;
7560}
7561
c0dd20ea 7562/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7563 functions, with the ABI-specified layout. If TYPE describes
7564 such a structure, smash it into a member function type.
61049d3b
DJ
7565
7566 GCC shouldn't do this; it should just output pointer to member DIEs.
7567 This is GCC PR debug/28767. */
c0dd20ea 7568
0b92b5bb
TT
7569static void
7570quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7571{
0b92b5bb 7572 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7573
7574 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7575 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7576 return;
c0dd20ea
DJ
7577
7578 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7579 if (TYPE_FIELD_NAME (type, 0) == NULL
7580 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7581 || TYPE_FIELD_NAME (type, 1) == NULL
7582 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7583 return;
c0dd20ea
DJ
7584
7585 /* Find the type of the method. */
0b92b5bb 7586 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7587 if (pfn_type == NULL
7588 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7589 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7590 return;
c0dd20ea
DJ
7591
7592 /* Look for the "this" argument. */
7593 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7594 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7595 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7596 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7597 return;
c0dd20ea
DJ
7598
7599 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7600 new_type = alloc_type (objfile);
7601 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7602 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7603 TYPE_VARARGS (pfn_type));
0b92b5bb 7604 smash_to_methodptr_type (type, new_type);
c0dd20ea 7605}
1168df01 7606
c906108c 7607/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7608 (definition) to create a type for the structure or union. Fill in
7609 the type's name and general properties; the members will not be
7610 processed until process_structure_type.
c906108c 7611
c767944b
DJ
7612 NOTE: we need to call these functions regardless of whether or not the
7613 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7614 structure or union. This gets the type entered into our set of
7615 user defined types.
7616
7617 However, if the structure is incomplete (an opaque struct/union)
7618 then suppress creating a symbol table entry for it since gdb only
7619 wants to find the one with the complete definition. Note that if
7620 it is complete, we just call new_symbol, which does it's own
7621 checking about whether the struct/union is anonymous or not (and
7622 suppresses creating a symbol table entry itself). */
7623
f792889a 7624static struct type *
134d01f1 7625read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7626{
e7c27a73 7627 struct objfile *objfile = cu->objfile;
c906108c
SS
7628 struct type *type;
7629 struct attribute *attr;
39cbfefa 7630 char *name;
c906108c 7631
348e048f
DE
7632 /* If the definition of this type lives in .debug_types, read that type.
7633 Don't follow DW_AT_specification though, that will take us back up
7634 the chain and we want to go down. */
7635 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7636 if (attr)
7637 {
7638 struct dwarf2_cu *type_cu = cu;
7639 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7640
348e048f
DE
7641 /* We could just recurse on read_structure_type, but we need to call
7642 get_die_type to ensure only one type for this DIE is created.
7643 This is important, for example, because for c++ classes we need
7644 TYPE_NAME set which is only done by new_symbol. Blech. */
7645 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7646
7647 /* TYPE_CU may not be the same as CU.
7648 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7649 return set_die_type (die, type, cu);
7650 }
7651
c0dd20ea 7652 type = alloc_type (objfile);
c906108c 7653 INIT_CPLUS_SPECIFIC (type);
93311388 7654
39cbfefa
DJ
7655 name = dwarf2_name (die, cu);
7656 if (name != NULL)
c906108c 7657 {
987504bb
JJ
7658 if (cu->language == language_cplus
7659 || cu->language == language_java)
63d06c5c 7660 {
3da10d80
KS
7661 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7662
7663 /* dwarf2_full_name might have already finished building the DIE's
7664 type. If so, there is no need to continue. */
7665 if (get_die_type (die, cu) != NULL)
7666 return get_die_type (die, cu);
7667
7668 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7669 if (die->tag == DW_TAG_structure_type
7670 || die->tag == DW_TAG_class_type)
7671 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7672 }
7673 else
7674 {
d8151005
DJ
7675 /* The name is already allocated along with this objfile, so
7676 we don't need to duplicate it for the type. */
94af9270
KS
7677 TYPE_TAG_NAME (type) = (char *) name;
7678 if (die->tag == DW_TAG_class_type)
7679 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7680 }
c906108c
SS
7681 }
7682
7683 if (die->tag == DW_TAG_structure_type)
7684 {
7685 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7686 }
7687 else if (die->tag == DW_TAG_union_type)
7688 {
7689 TYPE_CODE (type) = TYPE_CODE_UNION;
7690 }
7691 else
7692 {
c906108c
SS
7693 TYPE_CODE (type) = TYPE_CODE_CLASS;
7694 }
7695
0cc2414c
TT
7696 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7697 TYPE_DECLARED_CLASS (type) = 1;
7698
e142c38c 7699 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7700 if (attr)
7701 {
7702 TYPE_LENGTH (type) = DW_UNSND (attr);
7703 }
7704 else
7705 {
7706 TYPE_LENGTH (type) = 0;
7707 }
7708
876cecd0 7709 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7710 if (die_is_declaration (die, cu))
876cecd0 7711 TYPE_STUB (type) = 1;
a6c727b2
DJ
7712 else if (attr == NULL && die->child == NULL
7713 && producer_is_realview (cu->producer))
7714 /* RealView does not output the required DW_AT_declaration
7715 on incomplete types. */
7716 TYPE_STUB (type) = 1;
dc718098 7717
c906108c
SS
7718 /* We need to add the type field to the die immediately so we don't
7719 infinitely recurse when dealing with pointers to the structure
0963b4bd 7720 type within the structure itself. */
1c379e20 7721 set_die_type (die, type, cu);
c906108c 7722
7e314c57
JK
7723 /* set_die_type should be already done. */
7724 set_descriptive_type (type, die, cu);
7725
c767944b
DJ
7726 return type;
7727}
7728
7729/* Finish creating a structure or union type, including filling in
7730 its members and creating a symbol for it. */
7731
7732static void
7733process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7734{
7735 struct objfile *objfile = cu->objfile;
7736 struct die_info *child_die = die->child;
7737 struct type *type;
7738
7739 type = get_die_type (die, cu);
7740 if (type == NULL)
7741 type = read_structure_type (die, cu);
7742
e142c38c 7743 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7744 {
7745 struct field_info fi;
7746 struct die_info *child_die;
34eaf542 7747 VEC (symbolp) *template_args = NULL;
c767944b 7748 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7749
7750 memset (&fi, 0, sizeof (struct field_info));
7751
639d11d3 7752 child_die = die->child;
c906108c
SS
7753
7754 while (child_die && child_die->tag)
7755 {
a9a9bd0f
DC
7756 if (child_die->tag == DW_TAG_member
7757 || child_die->tag == DW_TAG_variable)
c906108c 7758 {
a9a9bd0f
DC
7759 /* NOTE: carlton/2002-11-05: A C++ static data member
7760 should be a DW_TAG_member that is a declaration, but
7761 all versions of G++ as of this writing (so through at
7762 least 3.2.1) incorrectly generate DW_TAG_variable
7763 tags for them instead. */
e7c27a73 7764 dwarf2_add_field (&fi, child_die, cu);
c906108c 7765 }
8713b1b1 7766 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7767 {
0963b4bd 7768 /* C++ member function. */
e7c27a73 7769 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7770 }
7771 else if (child_die->tag == DW_TAG_inheritance)
7772 {
7773 /* C++ base class field. */
e7c27a73 7774 dwarf2_add_field (&fi, child_die, cu);
c906108c 7775 }
98751a41
JK
7776 else if (child_die->tag == DW_TAG_typedef)
7777 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7778 else if (child_die->tag == DW_TAG_template_type_param
7779 || child_die->tag == DW_TAG_template_value_param)
7780 {
7781 struct symbol *arg = new_symbol (child_die, NULL, cu);
7782
f1078f66
DJ
7783 if (arg != NULL)
7784 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7785 }
7786
c906108c
SS
7787 child_die = sibling_die (child_die);
7788 }
7789
34eaf542
TT
7790 /* Attach template arguments to type. */
7791 if (! VEC_empty (symbolp, template_args))
7792 {
7793 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7794 TYPE_N_TEMPLATE_ARGUMENTS (type)
7795 = VEC_length (symbolp, template_args);
7796 TYPE_TEMPLATE_ARGUMENTS (type)
7797 = obstack_alloc (&objfile->objfile_obstack,
7798 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7799 * sizeof (struct symbol *)));
7800 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7801 VEC_address (symbolp, template_args),
7802 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7803 * sizeof (struct symbol *)));
7804 VEC_free (symbolp, template_args);
7805 }
7806
c906108c
SS
7807 /* Attach fields and member functions to the type. */
7808 if (fi.nfields)
e7c27a73 7809 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7810 if (fi.nfnfields)
7811 {
e7c27a73 7812 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7813
c5aa993b 7814 /* Get the type which refers to the base class (possibly this
c906108c 7815 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7816 class from the DW_AT_containing_type attribute. This use of
7817 DW_AT_containing_type is a GNU extension. */
c906108c 7818
e142c38c 7819 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7820 {
e7c27a73 7821 struct type *t = die_containing_type (die, cu);
c906108c
SS
7822
7823 TYPE_VPTR_BASETYPE (type) = t;
7824 if (type == t)
7825 {
c906108c
SS
7826 int i;
7827
7828 /* Our own class provides vtbl ptr. */
7829 for (i = TYPE_NFIELDS (t) - 1;
7830 i >= TYPE_N_BASECLASSES (t);
7831 --i)
7832 {
0d5cff50 7833 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 7834
1168df01 7835 if (is_vtable_name (fieldname, cu))
c906108c
SS
7836 {
7837 TYPE_VPTR_FIELDNO (type) = i;
7838 break;
7839 }
7840 }
7841
7842 /* Complain if virtual function table field not found. */
7843 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7844 complaint (&symfile_complaints,
3e43a32a
MS
7845 _("virtual function table pointer "
7846 "not found when defining class '%s'"),
4d3c2250
KB
7847 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7848 "");
c906108c
SS
7849 }
7850 else
7851 {
7852 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7853 }
7854 }
f6235d4c
EZ
7855 else if (cu->producer
7856 && strncmp (cu->producer,
7857 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7858 {
7859 /* The IBM XLC compiler does not provide direct indication
7860 of the containing type, but the vtable pointer is
7861 always named __vfp. */
7862
7863 int i;
7864
7865 for (i = TYPE_NFIELDS (type) - 1;
7866 i >= TYPE_N_BASECLASSES (type);
7867 --i)
7868 {
7869 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7870 {
7871 TYPE_VPTR_FIELDNO (type) = i;
7872 TYPE_VPTR_BASETYPE (type) = type;
7873 break;
7874 }
7875 }
7876 }
c906108c 7877 }
98751a41
JK
7878
7879 /* Copy fi.typedef_field_list linked list elements content into the
7880 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7881 if (fi.typedef_field_list)
7882 {
7883 int i = fi.typedef_field_list_count;
7884
a0d7a4ff 7885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7886 TYPE_TYPEDEF_FIELD_ARRAY (type)
7887 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7888 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7889
7890 /* Reverse the list order to keep the debug info elements order. */
7891 while (--i >= 0)
7892 {
7893 struct typedef_field *dest, *src;
6e70227d 7894
98751a41
JK
7895 dest = &TYPE_TYPEDEF_FIELD (type, i);
7896 src = &fi.typedef_field_list->field;
7897 fi.typedef_field_list = fi.typedef_field_list->next;
7898 *dest = *src;
7899 }
7900 }
c767944b
DJ
7901
7902 do_cleanups (back_to);
eb2a6f42
TT
7903
7904 if (HAVE_CPLUS_STRUCT (type))
7905 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7906 }
63d06c5c 7907
bb5ed363 7908 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7909
90aeadfc
DC
7910 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7911 snapshots) has been known to create a die giving a declaration
7912 for a class that has, as a child, a die giving a definition for a
7913 nested class. So we have to process our children even if the
7914 current die is a declaration. Normally, of course, a declaration
7915 won't have any children at all. */
134d01f1 7916
90aeadfc
DC
7917 while (child_die != NULL && child_die->tag)
7918 {
7919 if (child_die->tag == DW_TAG_member
7920 || child_die->tag == DW_TAG_variable
34eaf542
TT
7921 || child_die->tag == DW_TAG_inheritance
7922 || child_die->tag == DW_TAG_template_value_param
7923 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7924 {
90aeadfc 7925 /* Do nothing. */
134d01f1 7926 }
90aeadfc
DC
7927 else
7928 process_die (child_die, cu);
134d01f1 7929
90aeadfc 7930 child_die = sibling_die (child_die);
134d01f1
DJ
7931 }
7932
fa4028e9
JB
7933 /* Do not consider external references. According to the DWARF standard,
7934 these DIEs are identified by the fact that they have no byte_size
7935 attribute, and a declaration attribute. */
7936 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7937 || !die_is_declaration (die, cu))
c767944b 7938 new_symbol (die, type, cu);
134d01f1
DJ
7939}
7940
7941/* Given a DW_AT_enumeration_type die, set its type. We do not
7942 complete the type's fields yet, or create any symbols. */
c906108c 7943
f792889a 7944static struct type *
134d01f1 7945read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7946{
e7c27a73 7947 struct objfile *objfile = cu->objfile;
c906108c 7948 struct type *type;
c906108c 7949 struct attribute *attr;
0114d602 7950 const char *name;
134d01f1 7951
348e048f
DE
7952 /* If the definition of this type lives in .debug_types, read that type.
7953 Don't follow DW_AT_specification though, that will take us back up
7954 the chain and we want to go down. */
7955 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7956 if (attr)
7957 {
7958 struct dwarf2_cu *type_cu = cu;
7959 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7960
348e048f 7961 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7962
7963 /* TYPE_CU may not be the same as CU.
7964 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7965 return set_die_type (die, type, cu);
7966 }
7967
c906108c
SS
7968 type = alloc_type (objfile);
7969
7970 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7971 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7972 if (name != NULL)
0114d602 7973 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7974
e142c38c 7975 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7976 if (attr)
7977 {
7978 TYPE_LENGTH (type) = DW_UNSND (attr);
7979 }
7980 else
7981 {
7982 TYPE_LENGTH (type) = 0;
7983 }
7984
137033e9
JB
7985 /* The enumeration DIE can be incomplete. In Ada, any type can be
7986 declared as private in the package spec, and then defined only
7987 inside the package body. Such types are known as Taft Amendment
7988 Types. When another package uses such a type, an incomplete DIE
7989 may be generated by the compiler. */
02eb380e 7990 if (die_is_declaration (die, cu))
876cecd0 7991 TYPE_STUB (type) = 1;
02eb380e 7992
f792889a 7993 return set_die_type (die, type, cu);
134d01f1
DJ
7994}
7995
7996/* Given a pointer to a die which begins an enumeration, process all
7997 the dies that define the members of the enumeration, and create the
7998 symbol for the enumeration type.
7999
8000 NOTE: We reverse the order of the element list. */
8001
8002static void
8003process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8004{
f792889a 8005 struct type *this_type;
134d01f1 8006
f792889a
DJ
8007 this_type = get_die_type (die, cu);
8008 if (this_type == NULL)
8009 this_type = read_enumeration_type (die, cu);
9dc481d3 8010
639d11d3 8011 if (die->child != NULL)
c906108c 8012 {
9dc481d3
DE
8013 struct die_info *child_die;
8014 struct symbol *sym;
8015 struct field *fields = NULL;
8016 int num_fields = 0;
8017 int unsigned_enum = 1;
8018 char *name;
cafec441
TT
8019 int flag_enum = 1;
8020 ULONGEST mask = 0;
9dc481d3 8021
639d11d3 8022 child_die = die->child;
c906108c
SS
8023 while (child_die && child_die->tag)
8024 {
8025 if (child_die->tag != DW_TAG_enumerator)
8026 {
e7c27a73 8027 process_die (child_die, cu);
c906108c
SS
8028 }
8029 else
8030 {
39cbfefa
DJ
8031 name = dwarf2_name (child_die, cu);
8032 if (name)
c906108c 8033 {
f792889a 8034 sym = new_symbol (child_die, this_type, cu);
c906108c 8035 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
8036 {
8037 unsigned_enum = 0;
8038 flag_enum = 0;
8039 }
8040 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8041 flag_enum = 0;
8042 else
8043 mask |= SYMBOL_VALUE (sym);
c906108c
SS
8044
8045 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8046 {
8047 fields = (struct field *)
8048 xrealloc (fields,
8049 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8050 * sizeof (struct field));
c906108c
SS
8051 }
8052
3567439c 8053 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8054 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 8055 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8056 FIELD_BITSIZE (fields[num_fields]) = 0;
8057
8058 num_fields++;
8059 }
8060 }
8061
8062 child_die = sibling_die (child_die);
8063 }
8064
8065 if (num_fields)
8066 {
f792889a
DJ
8067 TYPE_NFIELDS (this_type) = num_fields;
8068 TYPE_FIELDS (this_type) = (struct field *)
8069 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8070 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8071 sizeof (struct field) * num_fields);
b8c9b27d 8072 xfree (fields);
c906108c
SS
8073 }
8074 if (unsigned_enum)
876cecd0 8075 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
8076 if (flag_enum)
8077 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 8078 }
134d01f1 8079
6c83ed52
TT
8080 /* If we are reading an enum from a .debug_types unit, and the enum
8081 is a declaration, and the enum is not the signatured type in the
8082 unit, then we do not want to add a symbol for it. Adding a
8083 symbol would in some cases obscure the true definition of the
8084 enum, giving users an incomplete type when the definition is
8085 actually available. Note that we do not want to do this for all
8086 enums which are just declarations, because C++0x allows forward
8087 enum declarations. */
b0df02fd 8088 if (cu->per_cu->debug_types_section
6c83ed52
TT
8089 && die_is_declaration (die, cu))
8090 {
52dc124a 8091 struct signatured_type *sig_type;
6c83ed52 8092
52dc124a 8093 sig_type
6c83ed52 8094 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8095 cu->per_cu->debug_types_section,
6c83ed52 8096 cu->per_cu->offset);
52dc124a 8097 if (sig_type->per_cu.offset.sect_off + sig_type->type_offset.cu_off
b64f50a1 8098 != die->offset.sect_off)
6c83ed52
TT
8099 return;
8100 }
8101
f792889a 8102 new_symbol (die, this_type, cu);
c906108c
SS
8103}
8104
8105/* Extract all information from a DW_TAG_array_type DIE and put it in
8106 the DIE's type field. For now, this only handles one dimensional
8107 arrays. */
8108
f792889a 8109static struct type *
e7c27a73 8110read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8111{
e7c27a73 8112 struct objfile *objfile = cu->objfile;
c906108c 8113 struct die_info *child_die;
7e314c57 8114 struct type *type;
c906108c
SS
8115 struct type *element_type, *range_type, *index_type;
8116 struct type **range_types = NULL;
8117 struct attribute *attr;
8118 int ndim = 0;
8119 struct cleanup *back_to;
39cbfefa 8120 char *name;
c906108c 8121
e7c27a73 8122 element_type = die_type (die, cu);
c906108c 8123
7e314c57
JK
8124 /* The die_type call above may have already set the type for this DIE. */
8125 type = get_die_type (die, cu);
8126 if (type)
8127 return type;
8128
c906108c
SS
8129 /* Irix 6.2 native cc creates array types without children for
8130 arrays with unspecified length. */
639d11d3 8131 if (die->child == NULL)
c906108c 8132 {
46bf5051 8133 index_type = objfile_type (objfile)->builtin_int;
c906108c 8134 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8135 type = create_array_type (NULL, element_type, range_type);
8136 return set_die_type (die, type, cu);
c906108c
SS
8137 }
8138
8139 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8140 child_die = die->child;
c906108c
SS
8141 while (child_die && child_die->tag)
8142 {
8143 if (child_die->tag == DW_TAG_subrange_type)
8144 {
f792889a 8145 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8146
f792889a 8147 if (child_type != NULL)
a02abb62 8148 {
0963b4bd
MS
8149 /* The range type was succesfully read. Save it for the
8150 array type creation. */
a02abb62
JB
8151 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8152 {
8153 range_types = (struct type **)
8154 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8155 * sizeof (struct type *));
8156 if (ndim == 0)
8157 make_cleanup (free_current_contents, &range_types);
8158 }
f792889a 8159 range_types[ndim++] = child_type;
a02abb62 8160 }
c906108c
SS
8161 }
8162 child_die = sibling_die (child_die);
8163 }
8164
8165 /* Dwarf2 dimensions are output from left to right, create the
8166 necessary array types in backwards order. */
7ca2d3a3 8167
c906108c 8168 type = element_type;
7ca2d3a3
DL
8169
8170 if (read_array_order (die, cu) == DW_ORD_col_major)
8171 {
8172 int i = 0;
9a619af0 8173
7ca2d3a3
DL
8174 while (i < ndim)
8175 type = create_array_type (NULL, type, range_types[i++]);
8176 }
8177 else
8178 {
8179 while (ndim-- > 0)
8180 type = create_array_type (NULL, type, range_types[ndim]);
8181 }
c906108c 8182
f5f8a009
EZ
8183 /* Understand Dwarf2 support for vector types (like they occur on
8184 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8185 array type. This is not part of the Dwarf2/3 standard yet, but a
8186 custom vendor extension. The main difference between a regular
8187 array and the vector variant is that vectors are passed by value
8188 to functions. */
e142c38c 8189 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8190 if (attr)
ea37ba09 8191 make_vector_type (type);
f5f8a009 8192
dbc98a8b
KW
8193 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8194 implementation may choose to implement triple vectors using this
8195 attribute. */
8196 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8197 if (attr)
8198 {
8199 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8200 TYPE_LENGTH (type) = DW_UNSND (attr);
8201 else
3e43a32a
MS
8202 complaint (&symfile_complaints,
8203 _("DW_AT_byte_size for array type smaller "
8204 "than the total size of elements"));
dbc98a8b
KW
8205 }
8206
39cbfefa
DJ
8207 name = dwarf2_name (die, cu);
8208 if (name)
8209 TYPE_NAME (type) = name;
6e70227d 8210
0963b4bd 8211 /* Install the type in the die. */
7e314c57
JK
8212 set_die_type (die, type, cu);
8213
8214 /* set_die_type should be already done. */
b4ba55a1
JB
8215 set_descriptive_type (type, die, cu);
8216
c906108c
SS
8217 do_cleanups (back_to);
8218
7e314c57 8219 return type;
c906108c
SS
8220}
8221
7ca2d3a3 8222static enum dwarf_array_dim_ordering
6e70227d 8223read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8224{
8225 struct attribute *attr;
8226
8227 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8228
8229 if (attr) return DW_SND (attr);
8230
0963b4bd
MS
8231 /* GNU F77 is a special case, as at 08/2004 array type info is the
8232 opposite order to the dwarf2 specification, but data is still
8233 laid out as per normal fortran.
7ca2d3a3 8234
0963b4bd
MS
8235 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8236 version checking. */
7ca2d3a3 8237
905e0470
PM
8238 if (cu->language == language_fortran
8239 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8240 {
8241 return DW_ORD_row_major;
8242 }
8243
6e70227d 8244 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8245 {
8246 case array_column_major:
8247 return DW_ORD_col_major;
8248 case array_row_major:
8249 default:
8250 return DW_ORD_row_major;
8251 };
8252}
8253
72019c9c 8254/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8255 the DIE's type field. */
72019c9c 8256
f792889a 8257static struct type *
72019c9c
GM
8258read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8259{
7e314c57
JK
8260 struct type *domain_type, *set_type;
8261 struct attribute *attr;
f792889a 8262
7e314c57
JK
8263 domain_type = die_type (die, cu);
8264
8265 /* The die_type call above may have already set the type for this DIE. */
8266 set_type = get_die_type (die, cu);
8267 if (set_type)
8268 return set_type;
8269
8270 set_type = create_set_type (NULL, domain_type);
8271
8272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8273 if (attr)
8274 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8275
f792889a 8276 return set_die_type (die, set_type, cu);
72019c9c 8277}
7ca2d3a3 8278
c906108c
SS
8279/* First cut: install each common block member as a global variable. */
8280
8281static void
e7c27a73 8282read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8283{
8284 struct die_info *child_die;
8285 struct attribute *attr;
8286 struct symbol *sym;
8287 CORE_ADDR base = (CORE_ADDR) 0;
8288
e142c38c 8289 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8290 if (attr)
8291 {
0963b4bd 8292 /* Support the .debug_loc offsets. */
8e19ed76
PS
8293 if (attr_form_is_block (attr))
8294 {
e7c27a73 8295 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8296 }
3690dd37 8297 else if (attr_form_is_section_offset (attr))
8e19ed76 8298 {
4d3c2250 8299 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8300 }
8301 else
8302 {
4d3c2250
KB
8303 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8304 "common block member");
8e19ed76 8305 }
c906108c 8306 }
639d11d3 8307 if (die->child != NULL)
c906108c 8308 {
639d11d3 8309 child_die = die->child;
c906108c
SS
8310 while (child_die && child_die->tag)
8311 {
74ac6d43
TT
8312 LONGEST offset;
8313
e7c27a73 8314 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8315 if (sym != NULL
8316 && handle_data_member_location (child_die, cu, &offset))
c906108c 8317 {
74ac6d43 8318 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8319 add_symbol_to_list (sym, &global_symbols);
8320 }
8321 child_die = sibling_die (child_die);
8322 }
8323 }
8324}
8325
0114d602 8326/* Create a type for a C++ namespace. */
d9fa45fe 8327
0114d602
DJ
8328static struct type *
8329read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8330{
e7c27a73 8331 struct objfile *objfile = cu->objfile;
0114d602 8332 const char *previous_prefix, *name;
9219021c 8333 int is_anonymous;
0114d602
DJ
8334 struct type *type;
8335
8336 /* For extensions, reuse the type of the original namespace. */
8337 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8338 {
8339 struct die_info *ext_die;
8340 struct dwarf2_cu *ext_cu = cu;
9a619af0 8341
0114d602
DJ
8342 ext_die = dwarf2_extension (die, &ext_cu);
8343 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8344
8345 /* EXT_CU may not be the same as CU.
8346 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8347 return set_die_type (die, type, cu);
8348 }
9219021c 8349
e142c38c 8350 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8351
8352 /* Now build the name of the current namespace. */
8353
0114d602
DJ
8354 previous_prefix = determine_prefix (die, cu);
8355 if (previous_prefix[0] != '\0')
8356 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8357 previous_prefix, name, 0, cu);
0114d602
DJ
8358
8359 /* Create the type. */
8360 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8361 objfile);
8362 TYPE_NAME (type) = (char *) name;
8363 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8364
60531b24 8365 return set_die_type (die, type, cu);
0114d602
DJ
8366}
8367
8368/* Read a C++ namespace. */
8369
8370static void
8371read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8372{
8373 struct objfile *objfile = cu->objfile;
0114d602 8374 int is_anonymous;
9219021c 8375
5c4e30ca
DC
8376 /* Add a symbol associated to this if we haven't seen the namespace
8377 before. Also, add a using directive if it's an anonymous
8378 namespace. */
9219021c 8379
f2f0e013 8380 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8381 {
8382 struct type *type;
8383
0114d602 8384 type = read_type_die (die, cu);
e7c27a73 8385 new_symbol (die, type, cu);
5c4e30ca 8386
e8e80198 8387 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8388 if (is_anonymous)
0114d602
DJ
8389 {
8390 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8391
c0cc3a76 8392 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8393 NULL, NULL, &objfile->objfile_obstack);
0114d602 8394 }
5c4e30ca 8395 }
9219021c 8396
639d11d3 8397 if (die->child != NULL)
d9fa45fe 8398 {
639d11d3 8399 struct die_info *child_die = die->child;
6e70227d 8400
d9fa45fe
DC
8401 while (child_die && child_die->tag)
8402 {
e7c27a73 8403 process_die (child_die, cu);
d9fa45fe
DC
8404 child_die = sibling_die (child_die);
8405 }
8406 }
38d518c9
EZ
8407}
8408
f55ee35c
JK
8409/* Read a Fortran module as type. This DIE can be only a declaration used for
8410 imported module. Still we need that type as local Fortran "use ... only"
8411 declaration imports depend on the created type in determine_prefix. */
8412
8413static struct type *
8414read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8415{
8416 struct objfile *objfile = cu->objfile;
8417 char *module_name;
8418 struct type *type;
8419
8420 module_name = dwarf2_name (die, cu);
8421 if (!module_name)
3e43a32a
MS
8422 complaint (&symfile_complaints,
8423 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 8424 die->offset.sect_off);
f55ee35c
JK
8425 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8426
8427 /* determine_prefix uses TYPE_TAG_NAME. */
8428 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8429
8430 return set_die_type (die, type, cu);
8431}
8432
5d7cb8df
JK
8433/* Read a Fortran module. */
8434
8435static void
8436read_module (struct die_info *die, struct dwarf2_cu *cu)
8437{
8438 struct die_info *child_die = die->child;
8439
5d7cb8df
JK
8440 while (child_die && child_die->tag)
8441 {
8442 process_die (child_die, cu);
8443 child_die = sibling_die (child_die);
8444 }
8445}
8446
38d518c9
EZ
8447/* Return the name of the namespace represented by DIE. Set
8448 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8449 namespace. */
8450
8451static const char *
e142c38c 8452namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8453{
8454 struct die_info *current_die;
8455 const char *name = NULL;
8456
8457 /* Loop through the extensions until we find a name. */
8458
8459 for (current_die = die;
8460 current_die != NULL;
f2f0e013 8461 current_die = dwarf2_extension (die, &cu))
38d518c9 8462 {
e142c38c 8463 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8464 if (name != NULL)
8465 break;
8466 }
8467
8468 /* Is it an anonymous namespace? */
8469
8470 *is_anonymous = (name == NULL);
8471 if (*is_anonymous)
2b1dbab0 8472 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8473
8474 return name;
d9fa45fe
DC
8475}
8476
c906108c
SS
8477/* Extract all information from a DW_TAG_pointer_type DIE and add to
8478 the user defined type vector. */
8479
f792889a 8480static struct type *
e7c27a73 8481read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8482{
5e2b427d 8483 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8484 struct comp_unit_head *cu_header = &cu->header;
c906108c 8485 struct type *type;
8b2dbe47
KB
8486 struct attribute *attr_byte_size;
8487 struct attribute *attr_address_class;
8488 int byte_size, addr_class;
7e314c57
JK
8489 struct type *target_type;
8490
8491 target_type = die_type (die, cu);
c906108c 8492
7e314c57
JK
8493 /* The die_type call above may have already set the type for this DIE. */
8494 type = get_die_type (die, cu);
8495 if (type)
8496 return type;
8497
8498 type = lookup_pointer_type (target_type);
8b2dbe47 8499
e142c38c 8500 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8501 if (attr_byte_size)
8502 byte_size = DW_UNSND (attr_byte_size);
c906108c 8503 else
8b2dbe47
KB
8504 byte_size = cu_header->addr_size;
8505
e142c38c 8506 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8507 if (attr_address_class)
8508 addr_class = DW_UNSND (attr_address_class);
8509 else
8510 addr_class = DW_ADDR_none;
8511
8512 /* If the pointer size or address class is different than the
8513 default, create a type variant marked as such and set the
8514 length accordingly. */
8515 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8516 {
5e2b427d 8517 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8518 {
8519 int type_flags;
8520
849957d9 8521 type_flags = gdbarch_address_class_type_flags
5e2b427d 8522 (gdbarch, byte_size, addr_class);
876cecd0
TT
8523 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8524 == 0);
8b2dbe47
KB
8525 type = make_type_with_address_space (type, type_flags);
8526 }
8527 else if (TYPE_LENGTH (type) != byte_size)
8528 {
3e43a32a
MS
8529 complaint (&symfile_complaints,
8530 _("invalid pointer size %d"), byte_size);
8b2dbe47 8531 }
6e70227d 8532 else
9a619af0
MS
8533 {
8534 /* Should we also complain about unhandled address classes? */
8535 }
c906108c 8536 }
8b2dbe47
KB
8537
8538 TYPE_LENGTH (type) = byte_size;
f792889a 8539 return set_die_type (die, type, cu);
c906108c
SS
8540}
8541
8542/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8543 the user defined type vector. */
8544
f792889a 8545static struct type *
e7c27a73 8546read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8547{
8548 struct type *type;
8549 struct type *to_type;
8550 struct type *domain;
8551
e7c27a73
DJ
8552 to_type = die_type (die, cu);
8553 domain = die_containing_type (die, cu);
0d5de010 8554
7e314c57
JK
8555 /* The calls above may have already set the type for this DIE. */
8556 type = get_die_type (die, cu);
8557 if (type)
8558 return type;
8559
0d5de010
DJ
8560 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8561 type = lookup_methodptr_type (to_type);
8562 else
8563 type = lookup_memberptr_type (to_type, domain);
c906108c 8564
f792889a 8565 return set_die_type (die, type, cu);
c906108c
SS
8566}
8567
8568/* Extract all information from a DW_TAG_reference_type DIE and add to
8569 the user defined type vector. */
8570
f792889a 8571static struct type *
e7c27a73 8572read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8573{
e7c27a73 8574 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8575 struct type *type, *target_type;
c906108c
SS
8576 struct attribute *attr;
8577
7e314c57
JK
8578 target_type = die_type (die, cu);
8579
8580 /* The die_type call above may have already set the type for this DIE. */
8581 type = get_die_type (die, cu);
8582 if (type)
8583 return type;
8584
8585 type = lookup_reference_type (target_type);
e142c38c 8586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8587 if (attr)
8588 {
8589 TYPE_LENGTH (type) = DW_UNSND (attr);
8590 }
8591 else
8592 {
107d2387 8593 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8594 }
f792889a 8595 return set_die_type (die, type, cu);
c906108c
SS
8596}
8597
f792889a 8598static struct type *
e7c27a73 8599read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8600{
f792889a 8601 struct type *base_type, *cv_type;
c906108c 8602
e7c27a73 8603 base_type = die_type (die, cu);
7e314c57
JK
8604
8605 /* The die_type call above may have already set the type for this DIE. */
8606 cv_type = get_die_type (die, cu);
8607 if (cv_type)
8608 return cv_type;
8609
2f608a3a
KW
8610 /* In case the const qualifier is applied to an array type, the element type
8611 is so qualified, not the array type (section 6.7.3 of C99). */
8612 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8613 {
8614 struct type *el_type, *inner_array;
8615
8616 base_type = copy_type (base_type);
8617 inner_array = base_type;
8618
8619 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8620 {
8621 TYPE_TARGET_TYPE (inner_array) =
8622 copy_type (TYPE_TARGET_TYPE (inner_array));
8623 inner_array = TYPE_TARGET_TYPE (inner_array);
8624 }
8625
8626 el_type = TYPE_TARGET_TYPE (inner_array);
8627 TYPE_TARGET_TYPE (inner_array) =
8628 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8629
8630 return set_die_type (die, base_type, cu);
8631 }
8632
f792889a
DJ
8633 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8634 return set_die_type (die, cv_type, cu);
c906108c
SS
8635}
8636
f792889a 8637static struct type *
e7c27a73 8638read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8639{
f792889a 8640 struct type *base_type, *cv_type;
c906108c 8641
e7c27a73 8642 base_type = die_type (die, cu);
7e314c57
JK
8643
8644 /* The die_type call above may have already set the type for this DIE. */
8645 cv_type = get_die_type (die, cu);
8646 if (cv_type)
8647 return cv_type;
8648
f792889a
DJ
8649 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8650 return set_die_type (die, cv_type, cu);
c906108c
SS
8651}
8652
8653/* Extract all information from a DW_TAG_string_type DIE and add to
8654 the user defined type vector. It isn't really a user defined type,
8655 but it behaves like one, with other DIE's using an AT_user_def_type
8656 attribute to reference it. */
8657
f792889a 8658static struct type *
e7c27a73 8659read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8660{
e7c27a73 8661 struct objfile *objfile = cu->objfile;
3b7538c0 8662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8663 struct type *type, *range_type, *index_type, *char_type;
8664 struct attribute *attr;
8665 unsigned int length;
8666
e142c38c 8667 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8668 if (attr)
8669 {
8670 length = DW_UNSND (attr);
8671 }
8672 else
8673 {
0963b4bd 8674 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8675 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8676 if (attr)
8677 {
8678 length = DW_UNSND (attr);
8679 }
8680 else
8681 {
8682 length = 1;
8683 }
c906108c 8684 }
6ccb9162 8685
46bf5051 8686 index_type = objfile_type (objfile)->builtin_int;
c906108c 8687 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8688 char_type = language_string_char_type (cu->language_defn, gdbarch);
8689 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8690
f792889a 8691 return set_die_type (die, type, cu);
c906108c
SS
8692}
8693
8694/* Handle DIES due to C code like:
8695
8696 struct foo
c5aa993b
JM
8697 {
8698 int (*funcp)(int a, long l);
8699 int b;
8700 };
c906108c 8701
0963b4bd 8702 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8703
f792889a 8704static struct type *
e7c27a73 8705read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8706{
bb5ed363 8707 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8708 struct type *type; /* Type that this function returns. */
8709 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8710 struct attribute *attr;
8711
e7c27a73 8712 type = die_type (die, cu);
7e314c57
JK
8713
8714 /* The die_type call above may have already set the type for this DIE. */
8715 ftype = get_die_type (die, cu);
8716 if (ftype)
8717 return ftype;
8718
0c8b41f1 8719 ftype = lookup_function_type (type);
c906108c 8720
5b8101ae 8721 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8722 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8723 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8724 || cu->language == language_cplus
5b8101ae
PM
8725 || cu->language == language_java
8726 || cu->language == language_pascal)
876cecd0 8727 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8728 else if (producer_is_realview (cu->producer))
8729 /* RealView does not emit DW_AT_prototyped. We can not
8730 distinguish prototyped and unprototyped functions; default to
8731 prototyped, since that is more common in modern code (and
8732 RealView warns about unprototyped functions). */
8733 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8734
c055b101
CV
8735 /* Store the calling convention in the type if it's available in
8736 the subroutine die. Otherwise set the calling convention to
8737 the default value DW_CC_normal. */
8738 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8739 if (attr)
8740 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8741 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8742 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8743 else
8744 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8745
8746 /* We need to add the subroutine type to the die immediately so
8747 we don't infinitely recurse when dealing with parameters
0963b4bd 8748 declared as the same subroutine type. */
76c10ea2 8749 set_die_type (die, ftype, cu);
6e70227d 8750
639d11d3 8751 if (die->child != NULL)
c906108c 8752 {
bb5ed363 8753 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8754 struct die_info *child_die;
8072405b 8755 int nparams, iparams;
c906108c
SS
8756
8757 /* Count the number of parameters.
8758 FIXME: GDB currently ignores vararg functions, but knows about
8759 vararg member functions. */
8072405b 8760 nparams = 0;
639d11d3 8761 child_die = die->child;
c906108c
SS
8762 while (child_die && child_die->tag)
8763 {
8764 if (child_die->tag == DW_TAG_formal_parameter)
8765 nparams++;
8766 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8767 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8768 child_die = sibling_die (child_die);
8769 }
8770
8771 /* Allocate storage for parameters and fill them in. */
8772 TYPE_NFIELDS (ftype) = nparams;
8773 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8774 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8775
8072405b
JK
8776 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8777 even if we error out during the parameters reading below. */
8778 for (iparams = 0; iparams < nparams; iparams++)
8779 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8780
8781 iparams = 0;
639d11d3 8782 child_die = die->child;
c906108c
SS
8783 while (child_die && child_die->tag)
8784 {
8785 if (child_die->tag == DW_TAG_formal_parameter)
8786 {
3ce3b1ba
PA
8787 struct type *arg_type;
8788
8789 /* DWARF version 2 has no clean way to discern C++
8790 static and non-static member functions. G++ helps
8791 GDB by marking the first parameter for non-static
8792 member functions (which is the this pointer) as
8793 artificial. We pass this information to
8794 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8795
8796 DWARF version 3 added DW_AT_object_pointer, which GCC
8797 4.5 does not yet generate. */
e142c38c 8798 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8799 if (attr)
8800 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8801 else
418835cc
KS
8802 {
8803 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8804
8805 /* GCC/43521: In java, the formal parameter
8806 "this" is sometimes not marked with DW_AT_artificial. */
8807 if (cu->language == language_java)
8808 {
8809 const char *name = dwarf2_name (child_die, cu);
9a619af0 8810
418835cc
KS
8811 if (name && !strcmp (name, "this"))
8812 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8813 }
8814 }
3ce3b1ba
PA
8815 arg_type = die_type (child_die, cu);
8816
8817 /* RealView does not mark THIS as const, which the testsuite
8818 expects. GCC marks THIS as const in method definitions,
8819 but not in the class specifications (GCC PR 43053). */
8820 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8821 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8822 {
8823 int is_this = 0;
8824 struct dwarf2_cu *arg_cu = cu;
8825 const char *name = dwarf2_name (child_die, cu);
8826
8827 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8828 if (attr)
8829 {
8830 /* If the compiler emits this, use it. */
8831 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8832 is_this = 1;
8833 }
8834 else if (name && strcmp (name, "this") == 0)
8835 /* Function definitions will have the argument names. */
8836 is_this = 1;
8837 else if (name == NULL && iparams == 0)
8838 /* Declarations may not have the names, so like
8839 elsewhere in GDB, assume an artificial first
8840 argument is "this". */
8841 is_this = 1;
8842
8843 if (is_this)
8844 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8845 arg_type, 0);
8846 }
8847
8848 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8849 iparams++;
8850 }
8851 child_die = sibling_die (child_die);
8852 }
8853 }
8854
76c10ea2 8855 return ftype;
c906108c
SS
8856}
8857
f792889a 8858static struct type *
e7c27a73 8859read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8860{
e7c27a73 8861 struct objfile *objfile = cu->objfile;
0114d602 8862 const char *name = NULL;
3c8e0968 8863 struct type *this_type, *target_type;
c906108c 8864
94af9270 8865 name = dwarf2_full_name (NULL, die, cu);
f792889a 8866 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8867 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8868 TYPE_NAME (this_type) = (char *) name;
f792889a 8869 set_die_type (die, this_type, cu);
3c8e0968
DE
8870 target_type = die_type (die, cu);
8871 if (target_type != this_type)
8872 TYPE_TARGET_TYPE (this_type) = target_type;
8873 else
8874 {
8875 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8876 spec and cause infinite loops in GDB. */
8877 complaint (&symfile_complaints,
8878 _("Self-referential DW_TAG_typedef "
8879 "- DIE at 0x%x [in module %s]"),
b64f50a1 8880 die->offset.sect_off, objfile->name);
3c8e0968
DE
8881 TYPE_TARGET_TYPE (this_type) = NULL;
8882 }
f792889a 8883 return this_type;
c906108c
SS
8884}
8885
8886/* Find a representation of a given base type and install
8887 it in the TYPE field of the die. */
8888
f792889a 8889static struct type *
e7c27a73 8890read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8891{
e7c27a73 8892 struct objfile *objfile = cu->objfile;
c906108c
SS
8893 struct type *type;
8894 struct attribute *attr;
8895 int encoding = 0, size = 0;
39cbfefa 8896 char *name;
6ccb9162
UW
8897 enum type_code code = TYPE_CODE_INT;
8898 int type_flags = 0;
8899 struct type *target_type = NULL;
c906108c 8900
e142c38c 8901 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8902 if (attr)
8903 {
8904 encoding = DW_UNSND (attr);
8905 }
e142c38c 8906 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8907 if (attr)
8908 {
8909 size = DW_UNSND (attr);
8910 }
39cbfefa 8911 name = dwarf2_name (die, cu);
6ccb9162 8912 if (!name)
c906108c 8913 {
6ccb9162
UW
8914 complaint (&symfile_complaints,
8915 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8916 }
6ccb9162
UW
8917
8918 switch (encoding)
c906108c 8919 {
6ccb9162
UW
8920 case DW_ATE_address:
8921 /* Turn DW_ATE_address into a void * pointer. */
8922 code = TYPE_CODE_PTR;
8923 type_flags |= TYPE_FLAG_UNSIGNED;
8924 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8925 break;
8926 case DW_ATE_boolean:
8927 code = TYPE_CODE_BOOL;
8928 type_flags |= TYPE_FLAG_UNSIGNED;
8929 break;
8930 case DW_ATE_complex_float:
8931 code = TYPE_CODE_COMPLEX;
8932 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8933 break;
8934 case DW_ATE_decimal_float:
8935 code = TYPE_CODE_DECFLOAT;
8936 break;
8937 case DW_ATE_float:
8938 code = TYPE_CODE_FLT;
8939 break;
8940 case DW_ATE_signed:
8941 break;
8942 case DW_ATE_unsigned:
8943 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8944 if (cu->language == language_fortran
8945 && name
8946 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8947 code = TYPE_CODE_CHAR;
6ccb9162
UW
8948 break;
8949 case DW_ATE_signed_char:
6e70227d 8950 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8951 || cu->language == language_pascal
8952 || cu->language == language_fortran)
6ccb9162
UW
8953 code = TYPE_CODE_CHAR;
8954 break;
8955 case DW_ATE_unsigned_char:
868a0084 8956 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8957 || cu->language == language_pascal
8958 || cu->language == language_fortran)
6ccb9162
UW
8959 code = TYPE_CODE_CHAR;
8960 type_flags |= TYPE_FLAG_UNSIGNED;
8961 break;
75079b2b
TT
8962 case DW_ATE_UTF:
8963 /* We just treat this as an integer and then recognize the
8964 type by name elsewhere. */
8965 break;
8966
6ccb9162
UW
8967 default:
8968 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8969 dwarf_type_encoding_name (encoding));
8970 break;
c906108c 8971 }
6ccb9162 8972
0114d602
DJ
8973 type = init_type (code, size, type_flags, NULL, objfile);
8974 TYPE_NAME (type) = name;
6ccb9162
UW
8975 TYPE_TARGET_TYPE (type) = target_type;
8976
0114d602 8977 if (name && strcmp (name, "char") == 0)
876cecd0 8978 TYPE_NOSIGN (type) = 1;
0114d602 8979
f792889a 8980 return set_die_type (die, type, cu);
c906108c
SS
8981}
8982
a02abb62
JB
8983/* Read the given DW_AT_subrange DIE. */
8984
f792889a 8985static struct type *
a02abb62
JB
8986read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8987{
8988 struct type *base_type;
8989 struct type *range_type;
8990 struct attribute *attr;
43bbcdc2
PH
8991 LONGEST low = 0;
8992 LONGEST high = -1;
39cbfefa 8993 char *name;
43bbcdc2 8994 LONGEST negative_mask;
e77813c8 8995
a02abb62 8996 base_type = die_type (die, cu);
953ac07e
JK
8997 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8998 check_typedef (base_type);
a02abb62 8999
7e314c57
JK
9000 /* The die_type call above may have already set the type for this DIE. */
9001 range_type = get_die_type (die, cu);
9002 if (range_type)
9003 return range_type;
9004
e142c38c 9005 if (cu->language == language_fortran)
6e70227d 9006 {
a02abb62
JB
9007 /* FORTRAN implies a lower bound of 1, if not given. */
9008 low = 1;
9009 }
9010
dd5e6932
DJ
9011 /* FIXME: For variable sized arrays either of these could be
9012 a variable rather than a constant value. We'll allow it,
9013 but we don't know how to handle it. */
e142c38c 9014 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
9015 if (attr)
9016 low = dwarf2_get_attr_constant_value (attr, 0);
9017
e142c38c 9018 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9019 if (attr)
6e70227d 9020 {
d48323d8 9021 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9022 {
9023 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9024 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9025 FIXME: GDB does not yet know how to handle dynamic
9026 arrays properly, treat them as arrays with unspecified
9027 length for now.
9028
9029 FIXME: jimb/2003-09-22: GDB does not really know
9030 how to handle arrays of unspecified length
9031 either; we just represent them as zero-length
9032 arrays. Choose an appropriate upper bound given
9033 the lower bound we've computed above. */
9034 high = low - 1;
9035 }
9036 else
9037 high = dwarf2_get_attr_constant_value (attr, 1);
9038 }
e77813c8
PM
9039 else
9040 {
9041 attr = dwarf2_attr (die, DW_AT_count, cu);
9042 if (attr)
9043 {
9044 int count = dwarf2_get_attr_constant_value (attr, 1);
9045 high = low + count - 1;
9046 }
c2ff108b
JK
9047 else
9048 {
9049 /* Unspecified array length. */
9050 high = low - 1;
9051 }
e77813c8
PM
9052 }
9053
9054 /* Dwarf-2 specifications explicitly allows to create subrange types
9055 without specifying a base type.
9056 In that case, the base type must be set to the type of
9057 the lower bound, upper bound or count, in that order, if any of these
9058 three attributes references an object that has a type.
9059 If no base type is found, the Dwarf-2 specifications say that
9060 a signed integer type of size equal to the size of an address should
9061 be used.
9062 For the following C code: `extern char gdb_int [];'
9063 GCC produces an empty range DIE.
9064 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9065 high bound or count are not yet handled by this code. */
e77813c8
PM
9066 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9067 {
9068 struct objfile *objfile = cu->objfile;
9069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9070 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9071 struct type *int_type = objfile_type (objfile)->builtin_int;
9072
9073 /* Test "int", "long int", and "long long int" objfile types,
9074 and select the first one having a size above or equal to the
9075 architecture address size. */
9076 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9077 base_type = int_type;
9078 else
9079 {
9080 int_type = objfile_type (objfile)->builtin_long;
9081 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9082 base_type = int_type;
9083 else
9084 {
9085 int_type = objfile_type (objfile)->builtin_long_long;
9086 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9087 base_type = int_type;
9088 }
9089 }
9090 }
a02abb62 9091
6e70227d 9092 negative_mask =
43bbcdc2
PH
9093 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9094 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9095 low |= negative_mask;
9096 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9097 high |= negative_mask;
9098
a02abb62
JB
9099 range_type = create_range_type (NULL, base_type, low, high);
9100
bbb0eef6
JK
9101 /* Mark arrays with dynamic length at least as an array of unspecified
9102 length. GDB could check the boundary but before it gets implemented at
9103 least allow accessing the array elements. */
d48323d8 9104 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9105 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9106
c2ff108b
JK
9107 /* Ada expects an empty array on no boundary attributes. */
9108 if (attr == NULL && cu->language != language_ada)
9109 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9110
39cbfefa
DJ
9111 name = dwarf2_name (die, cu);
9112 if (name)
9113 TYPE_NAME (range_type) = name;
6e70227d 9114
e142c38c 9115 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9116 if (attr)
9117 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9118
7e314c57
JK
9119 set_die_type (die, range_type, cu);
9120
9121 /* set_die_type should be already done. */
b4ba55a1
JB
9122 set_descriptive_type (range_type, die, cu);
9123
7e314c57 9124 return range_type;
a02abb62 9125}
6e70227d 9126
f792889a 9127static struct type *
81a17f79
JB
9128read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9129{
9130 struct type *type;
81a17f79 9131
81a17f79
JB
9132 /* For now, we only support the C meaning of an unspecified type: void. */
9133
0114d602
DJ
9134 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9135 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9136
f792889a 9137 return set_die_type (die, type, cu);
81a17f79 9138}
a02abb62 9139
51545339
DJ
9140/* Trivial hash function for die_info: the hash value of a DIE
9141 is its offset in .debug_info for this objfile. */
9142
9143static hashval_t
9144die_hash (const void *item)
9145{
9146 const struct die_info *die = item;
9a619af0 9147
b64f50a1 9148 return die->offset.sect_off;
51545339
DJ
9149}
9150
9151/* Trivial comparison function for die_info structures: two DIEs
9152 are equal if they have the same offset. */
9153
9154static int
9155die_eq (const void *item_lhs, const void *item_rhs)
9156{
9157 const struct die_info *die_lhs = item_lhs;
9158 const struct die_info *die_rhs = item_rhs;
9a619af0 9159
b64f50a1 9160 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
51545339
DJ
9161}
9162
c906108c
SS
9163/* Read a whole compilation unit into a linked list of dies. */
9164
f9aca02d 9165static struct die_info *
93311388 9166read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9167{
93311388 9168 struct die_reader_specs reader_specs;
98bfdba5 9169 int read_abbrevs = 0;
1d9ec526 9170 struct cleanup *back_to = NULL;
98bfdba5
PA
9171 struct die_info *die;
9172
9173 if (cu->dwarf2_abbrevs == NULL)
9174 {
e5fe5e75 9175 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9176 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9177 read_abbrevs = 1;
9178 }
93311388 9179
348e048f 9180 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9181 cu->die_hash
9182 = htab_create_alloc_ex (cu->header.length / 12,
9183 die_hash,
9184 die_eq,
9185 NULL,
9186 &cu->comp_unit_obstack,
9187 hashtab_obstack_allocate,
9188 dummy_obstack_deallocate);
9189
93311388
DE
9190 init_cu_die_reader (&reader_specs, cu);
9191
98bfdba5
PA
9192 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9193
9194 if (read_abbrevs)
9195 do_cleanups (back_to);
9196
9197 return die;
639d11d3
DC
9198}
9199
d97bc12b
DE
9200/* Main entry point for reading a DIE and all children.
9201 Read the DIE and dump it if requested. */
9202
9203static struct die_info *
93311388
DE
9204read_die_and_children (const struct die_reader_specs *reader,
9205 gdb_byte *info_ptr,
d97bc12b
DE
9206 gdb_byte **new_info_ptr,
9207 struct die_info *parent)
9208{
93311388 9209 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9210 new_info_ptr, parent);
9211
9212 if (dwarf2_die_debug)
9213 {
348e048f
DE
9214 fprintf_unfiltered (gdb_stdlog,
9215 "\nRead die from %s of %s:\n",
b0df02fd 9216 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9217 ? ".debug_types"
9218 : ".debug_info"),
348e048f 9219 reader->abfd->filename);
d97bc12b
DE
9220 dump_die (result, dwarf2_die_debug);
9221 }
9222
9223 return result;
9224}
9225
639d11d3
DC
9226/* Read a single die and all its descendents. Set the die's sibling
9227 field to NULL; set other fields in the die correctly, and set all
9228 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9229 location of the info_ptr after reading all of those dies. PARENT
9230 is the parent of the die in question. */
9231
9232static struct die_info *
93311388
DE
9233read_die_and_children_1 (const struct die_reader_specs *reader,
9234 gdb_byte *info_ptr,
d97bc12b
DE
9235 gdb_byte **new_info_ptr,
9236 struct die_info *parent)
639d11d3
DC
9237{
9238 struct die_info *die;
fe1b8b76 9239 gdb_byte *cur_ptr;
639d11d3
DC
9240 int has_children;
9241
93311388 9242 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9243 if (die == NULL)
9244 {
9245 *new_info_ptr = cur_ptr;
9246 return NULL;
9247 }
93311388 9248 store_in_ref_table (die, reader->cu);
639d11d3
DC
9249
9250 if (has_children)
348e048f 9251 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9252 else
9253 {
9254 die->child = NULL;
9255 *new_info_ptr = cur_ptr;
9256 }
9257
9258 die->sibling = NULL;
9259 die->parent = parent;
9260 return die;
9261}
9262
9263/* Read a die, all of its descendents, and all of its siblings; set
9264 all of the fields of all of the dies correctly. Arguments are as
9265 in read_die_and_children. */
9266
9267static struct die_info *
93311388
DE
9268read_die_and_siblings (const struct die_reader_specs *reader,
9269 gdb_byte *info_ptr,
fe1b8b76 9270 gdb_byte **new_info_ptr,
639d11d3
DC
9271 struct die_info *parent)
9272{
9273 struct die_info *first_die, *last_sibling;
fe1b8b76 9274 gdb_byte *cur_ptr;
639d11d3 9275
c906108c 9276 cur_ptr = info_ptr;
639d11d3
DC
9277 first_die = last_sibling = NULL;
9278
9279 while (1)
c906108c 9280 {
639d11d3 9281 struct die_info *die
93311388 9282 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9283
1d325ec1 9284 if (die == NULL)
c906108c 9285 {
639d11d3
DC
9286 *new_info_ptr = cur_ptr;
9287 return first_die;
c906108c 9288 }
1d325ec1
DJ
9289
9290 if (!first_die)
9291 first_die = die;
c906108c 9292 else
1d325ec1
DJ
9293 last_sibling->sibling = die;
9294
9295 last_sibling = die;
c906108c 9296 }
c906108c
SS
9297}
9298
93311388
DE
9299/* Read the die from the .debug_info section buffer. Set DIEP to
9300 point to a newly allocated die with its information, except for its
9301 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9302 whether the die has children or not. */
9303
9304static gdb_byte *
9305read_full_die (const struct die_reader_specs *reader,
9306 struct die_info **diep, gdb_byte *info_ptr,
9307 int *has_children)
9308{
b64f50a1
JK
9309 unsigned int abbrev_number, bytes_read, i;
9310 sect_offset offset;
93311388
DE
9311 struct abbrev_info *abbrev;
9312 struct die_info *die;
9313 struct dwarf2_cu *cu = reader->cu;
9314 bfd *abfd = reader->abfd;
9315
b64f50a1 9316 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
9317 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9318 info_ptr += bytes_read;
9319 if (!abbrev_number)
9320 {
9321 *diep = NULL;
9322 *has_children = 0;
9323 return info_ptr;
9324 }
9325
9326 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9327 if (!abbrev)
348e048f
DE
9328 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9329 abbrev_number,
9330 bfd_get_filename (abfd));
9331
93311388
DE
9332 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9333 die->offset = offset;
9334 die->tag = abbrev->tag;
9335 die->abbrev = abbrev_number;
9336
9337 die->num_attrs = abbrev->num_attrs;
9338
9339 for (i = 0; i < abbrev->num_attrs; ++i)
9340 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9341 abfd, info_ptr, cu);
9342
9343 *diep = die;
9344 *has_children = abbrev->has_children;
9345 return info_ptr;
9346}
9347
c906108c
SS
9348/* In DWARF version 2, the description of the debugging information is
9349 stored in a separate .debug_abbrev section. Before we read any
9350 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9351 in a hash table. This function also sets flags in CU describing
9352 the data found in the abbrev table. */
c906108c
SS
9353
9354static void
e5fe5e75 9355dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9356{
e5fe5e75 9357 bfd *abfd = cu->objfile->obfd;
e7c27a73 9358 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9359 gdb_byte *abbrev_ptr;
c906108c
SS
9360 struct abbrev_info *cur_abbrev;
9361 unsigned int abbrev_number, bytes_read, abbrev_name;
9362 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9363 struct attr_abbrev *cur_attrs;
9364 unsigned int allocated_attrs;
c906108c 9365
0963b4bd 9366 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9367 obstack_init (&cu->abbrev_obstack);
9368 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9369 (ABBREV_HASH_SIZE
9370 * sizeof (struct abbrev_info *)));
9371 memset (cu->dwarf2_abbrevs, 0,
9372 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9373
be391dca
TT
9374 dwarf2_read_section (dwarf2_per_objfile->objfile,
9375 &dwarf2_per_objfile->abbrev);
b64f50a1
JK
9376 abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9377 + cu_header->abbrev_offset.sect_off);
c906108c
SS
9378 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9379 abbrev_ptr += bytes_read;
9380
f3dd6933
DJ
9381 allocated_attrs = ATTR_ALLOC_CHUNK;
9382 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9383
0963b4bd 9384 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9385 while (abbrev_number)
9386 {
f3dd6933 9387 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9388
9389 /* read in abbrev header */
9390 cur_abbrev->number = abbrev_number;
9391 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9392 abbrev_ptr += bytes_read;
9393 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9394 abbrev_ptr += 1;
9395
9396 /* now read in declarations */
9397 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9398 abbrev_ptr += bytes_read;
9399 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9400 abbrev_ptr += bytes_read;
9401 while (abbrev_name)
9402 {
f3dd6933 9403 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9404 {
f3dd6933
DJ
9405 allocated_attrs += ATTR_ALLOC_CHUNK;
9406 cur_attrs
9407 = xrealloc (cur_attrs, (allocated_attrs
9408 * sizeof (struct attr_abbrev)));
c906108c 9409 }
ae038cb0 9410
f3dd6933
DJ
9411 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9412 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9413 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9414 abbrev_ptr += bytes_read;
9415 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9416 abbrev_ptr += bytes_read;
9417 }
9418
f3dd6933
DJ
9419 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9420 (cur_abbrev->num_attrs
9421 * sizeof (struct attr_abbrev)));
9422 memcpy (cur_abbrev->attrs, cur_attrs,
9423 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9424
c906108c 9425 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9426 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9427 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9428
9429 /* Get next abbreviation.
9430 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9431 always properly terminated with an abbrev number of 0.
9432 Exit loop if we encounter an abbreviation which we have
9433 already read (which means we are about to read the abbreviations
9434 for the next compile unit) or if the end of the abbreviation
9435 table is reached. */
dce234bc
PP
9436 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9437 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9438 break;
9439 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9440 abbrev_ptr += bytes_read;
e7c27a73 9441 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9442 break;
9443 }
f3dd6933
DJ
9444
9445 xfree (cur_attrs);
c906108c
SS
9446}
9447
f3dd6933 9448/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9449
c906108c 9450static void
f3dd6933 9451dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9452{
f3dd6933 9453 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9454
f3dd6933
DJ
9455 obstack_free (&cu->abbrev_obstack, NULL);
9456 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9457}
9458
9459/* Lookup an abbrev_info structure in the abbrev hash table. */
9460
9461static struct abbrev_info *
e7c27a73 9462dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9463{
9464 unsigned int hash_number;
9465 struct abbrev_info *abbrev;
9466
9467 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9468 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9469
9470 while (abbrev)
9471 {
9472 if (abbrev->number == number)
9473 return abbrev;
9474 else
9475 abbrev = abbrev->next;
9476 }
9477 return NULL;
9478}
9479
72bf9492
DJ
9480/* Returns nonzero if TAG represents a type that we might generate a partial
9481 symbol for. */
9482
9483static int
9484is_type_tag_for_partial (int tag)
9485{
9486 switch (tag)
9487 {
9488#if 0
9489 /* Some types that would be reasonable to generate partial symbols for,
9490 that we don't at present. */
9491 case DW_TAG_array_type:
9492 case DW_TAG_file_type:
9493 case DW_TAG_ptr_to_member_type:
9494 case DW_TAG_set_type:
9495 case DW_TAG_string_type:
9496 case DW_TAG_subroutine_type:
9497#endif
9498 case DW_TAG_base_type:
9499 case DW_TAG_class_type:
680b30c7 9500 case DW_TAG_interface_type:
72bf9492
DJ
9501 case DW_TAG_enumeration_type:
9502 case DW_TAG_structure_type:
9503 case DW_TAG_subrange_type:
9504 case DW_TAG_typedef:
9505 case DW_TAG_union_type:
9506 return 1;
9507 default:
9508 return 0;
9509 }
9510}
9511
9512/* Load all DIEs that are interesting for partial symbols into memory. */
9513
9514static struct partial_die_info *
93311388
DE
9515load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9516 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9517{
bb5ed363 9518 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9519 struct partial_die_info *part_die;
9520 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9521 struct abbrev_info *abbrev;
9522 unsigned int bytes_read;
5afb4e99 9523 unsigned int load_all = 0;
72bf9492
DJ
9524 int nesting_level = 1;
9525
9526 parent_die = NULL;
9527 last_die = NULL;
9528
7adf1e79
DE
9529 gdb_assert (cu->per_cu != NULL);
9530 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
9531 load_all = 1;
9532
72bf9492
DJ
9533 cu->partial_dies
9534 = htab_create_alloc_ex (cu->header.length / 12,
9535 partial_die_hash,
9536 partial_die_eq,
9537 NULL,
9538 &cu->comp_unit_obstack,
9539 hashtab_obstack_allocate,
9540 dummy_obstack_deallocate);
9541
9542 part_die = obstack_alloc (&cu->comp_unit_obstack,
9543 sizeof (struct partial_die_info));
9544
9545 while (1)
9546 {
9547 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9548
9549 /* A NULL abbrev means the end of a series of children. */
9550 if (abbrev == NULL)
9551 {
9552 if (--nesting_level == 0)
9553 {
9554 /* PART_DIE was probably the last thing allocated on the
9555 comp_unit_obstack, so we could call obstack_free
9556 here. We don't do that because the waste is small,
9557 and will be cleaned up when we're done with this
9558 compilation unit. This way, we're also more robust
9559 against other users of the comp_unit_obstack. */
9560 return first_die;
9561 }
9562 info_ptr += bytes_read;
9563 last_die = parent_die;
9564 parent_die = parent_die->die_parent;
9565 continue;
9566 }
9567
98bfdba5
PA
9568 /* Check for template arguments. We never save these; if
9569 they're seen, we just mark the parent, and go on our way. */
9570 if (parent_die != NULL
9571 && cu->language == language_cplus
9572 && (abbrev->tag == DW_TAG_template_type_param
9573 || abbrev->tag == DW_TAG_template_value_param))
9574 {
9575 parent_die->has_template_arguments = 1;
9576
9577 if (!load_all)
9578 {
9579 /* We don't need a partial DIE for the template argument. */
9580 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9581 cu);
9582 continue;
9583 }
9584 }
9585
0d99eb77 9586 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
9587 Skip their other children. */
9588 if (!load_all
9589 && cu->language == language_cplus
9590 && parent_die != NULL
9591 && parent_die->tag == DW_TAG_subprogram)
9592 {
9593 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9594 continue;
9595 }
9596
5afb4e99
DJ
9597 /* Check whether this DIE is interesting enough to save. Normally
9598 we would not be interested in members here, but there may be
9599 later variables referencing them via DW_AT_specification (for
9600 static members). */
9601 if (!load_all
9602 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9603 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9604 && abbrev->tag != DW_TAG_enumerator
9605 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9606 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9607 && abbrev->tag != DW_TAG_variable
5afb4e99 9608 && abbrev->tag != DW_TAG_namespace
f55ee35c 9609 && abbrev->tag != DW_TAG_module
5afb4e99 9610 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9611 {
9612 /* Otherwise we skip to the next sibling, if any. */
93311388 9613 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9614 continue;
9615 }
9616
93311388
DE
9617 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9618 buffer, info_ptr, cu);
72bf9492
DJ
9619
9620 /* This two-pass algorithm for processing partial symbols has a
9621 high cost in cache pressure. Thus, handle some simple cases
9622 here which cover the majority of C partial symbols. DIEs
9623 which neither have specification tags in them, nor could have
9624 specification tags elsewhere pointing at them, can simply be
9625 processed and discarded.
9626
9627 This segment is also optional; scan_partial_symbols and
9628 add_partial_symbol will handle these DIEs if we chain
9629 them in normally. When compilers which do not emit large
9630 quantities of duplicate debug information are more common,
9631 this code can probably be removed. */
9632
9633 /* Any complete simple types at the top level (pretty much all
9634 of them, for a language without namespaces), can be processed
9635 directly. */
9636 if (parent_die == NULL
9637 && part_die->has_specification == 0
9638 && part_die->is_declaration == 0
d8228535 9639 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9640 || part_die->tag == DW_TAG_base_type
9641 || part_die->tag == DW_TAG_subrange_type))
9642 {
9643 if (building_psymtab && part_die->name != NULL)
04a679b8 9644 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9645 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9646 &objfile->static_psymbols,
9647 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9648 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9649 continue;
9650 }
9651
d8228535
JK
9652 /* The exception for DW_TAG_typedef with has_children above is
9653 a workaround of GCC PR debug/47510. In the case of this complaint
9654 type_name_no_tag_or_error will error on such types later.
9655
9656 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9657 it could not find the child DIEs referenced later, this is checked
9658 above. In correct DWARF DW_TAG_typedef should have no children. */
9659
9660 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9661 complaint (&symfile_complaints,
9662 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9663 "- DIE at 0x%x [in module %s]"),
b64f50a1 9664 part_die->offset.sect_off, objfile->name);
d8228535 9665
72bf9492
DJ
9666 /* If we're at the second level, and we're an enumerator, and
9667 our parent has no specification (meaning possibly lives in a
9668 namespace elsewhere), then we can add the partial symbol now
9669 instead of queueing it. */
9670 if (part_die->tag == DW_TAG_enumerator
9671 && parent_die != NULL
9672 && parent_die->die_parent == NULL
9673 && parent_die->tag == DW_TAG_enumeration_type
9674 && parent_die->has_specification == 0)
9675 {
9676 if (part_die->name == NULL)
3e43a32a
MS
9677 complaint (&symfile_complaints,
9678 _("malformed enumerator DIE ignored"));
72bf9492 9679 else if (building_psymtab)
04a679b8 9680 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9681 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9682 (cu->language == language_cplus
9683 || cu->language == language_java)
bb5ed363
DE
9684 ? &objfile->global_psymbols
9685 : &objfile->static_psymbols,
9686 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9687
93311388 9688 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9689 continue;
9690 }
9691
9692 /* We'll save this DIE so link it in. */
9693 part_die->die_parent = parent_die;
9694 part_die->die_sibling = NULL;
9695 part_die->die_child = NULL;
9696
9697 if (last_die && last_die == parent_die)
9698 last_die->die_child = part_die;
9699 else if (last_die)
9700 last_die->die_sibling = part_die;
9701
9702 last_die = part_die;
9703
9704 if (first_die == NULL)
9705 first_die = part_die;
9706
9707 /* Maybe add the DIE to the hash table. Not all DIEs that we
9708 find interesting need to be in the hash table, because we
9709 also have the parent/sibling/child chains; only those that we
9710 might refer to by offset later during partial symbol reading.
9711
9712 For now this means things that might have be the target of a
9713 DW_AT_specification, DW_AT_abstract_origin, or
9714 DW_AT_extension. DW_AT_extension will refer only to
9715 namespaces; DW_AT_abstract_origin refers to functions (and
9716 many things under the function DIE, but we do not recurse
9717 into function DIEs during partial symbol reading) and
9718 possibly variables as well; DW_AT_specification refers to
9719 declarations. Declarations ought to have the DW_AT_declaration
9720 flag. It happens that GCC forgets to put it in sometimes, but
9721 only for functions, not for types.
9722
9723 Adding more things than necessary to the hash table is harmless
9724 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9725 wasted time in find_partial_die, when we reread the compilation
9726 unit with load_all_dies set. */
72bf9492 9727
5afb4e99 9728 if (load_all
72929c62 9729 || abbrev->tag == DW_TAG_constant
5afb4e99 9730 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9731 || abbrev->tag == DW_TAG_variable
9732 || abbrev->tag == DW_TAG_namespace
9733 || part_die->is_declaration)
9734 {
9735 void **slot;
9736
9737 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 9738 part_die->offset.sect_off, INSERT);
72bf9492
DJ
9739 *slot = part_die;
9740 }
9741
9742 part_die = obstack_alloc (&cu->comp_unit_obstack,
9743 sizeof (struct partial_die_info));
9744
9745 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9746 we have no reason to follow the children of structures; for other
98bfdba5
PA
9747 languages we have to, so that we can get at method physnames
9748 to infer fully qualified class names, for DW_AT_specification,
9749 and for C++ template arguments. For C++, we also look one level
9750 inside functions to find template arguments (if the name of the
9751 function does not already contain the template arguments).
bc30ff58
JB
9752
9753 For Ada, we need to scan the children of subprograms and lexical
9754 blocks as well because Ada allows the definition of nested
9755 entities that could be interesting for the debugger, such as
9756 nested subprograms for instance. */
72bf9492 9757 if (last_die->has_children
5afb4e99
DJ
9758 && (load_all
9759 || last_die->tag == DW_TAG_namespace
f55ee35c 9760 || last_die->tag == DW_TAG_module
72bf9492 9761 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9762 || (cu->language == language_cplus
9763 && last_die->tag == DW_TAG_subprogram
9764 && (last_die->name == NULL
9765 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9766 || (cu->language != language_c
9767 && (last_die->tag == DW_TAG_class_type
680b30c7 9768 || last_die->tag == DW_TAG_interface_type
72bf9492 9769 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9770 || last_die->tag == DW_TAG_union_type))
9771 || (cu->language == language_ada
9772 && (last_die->tag == DW_TAG_subprogram
9773 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9774 {
9775 nesting_level++;
9776 parent_die = last_die;
9777 continue;
9778 }
9779
9780 /* Otherwise we skip to the next sibling, if any. */
93311388 9781 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9782
9783 /* Back to the top, do it again. */
9784 }
9785}
9786
c906108c
SS
9787/* Read a minimal amount of information into the minimal die structure. */
9788
fe1b8b76 9789static gdb_byte *
72bf9492
DJ
9790read_partial_die (struct partial_die_info *part_die,
9791 struct abbrev_info *abbrev,
9792 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9793 gdb_byte *buffer, gdb_byte *info_ptr,
9794 struct dwarf2_cu *cu)
c906108c 9795{
bb5ed363 9796 struct objfile *objfile = cu->objfile;
fa238c03 9797 unsigned int i;
c906108c 9798 struct attribute attr;
c5aa993b 9799 int has_low_pc_attr = 0;
c906108c
SS
9800 int has_high_pc_attr = 0;
9801
72bf9492 9802 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9803
b64f50a1 9804 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
9805
9806 info_ptr += abbrev_len;
9807
9808 if (abbrev == NULL)
9809 return info_ptr;
9810
c906108c
SS
9811 part_die->tag = abbrev->tag;
9812 part_die->has_children = abbrev->has_children;
c906108c
SS
9813
9814 for (i = 0; i < abbrev->num_attrs; ++i)
9815 {
e7c27a73 9816 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9817
9818 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9819 partial symbol table. */
c906108c
SS
9820 switch (attr.name)
9821 {
9822 case DW_AT_name:
71c25dea
TT
9823 switch (part_die->tag)
9824 {
9825 case DW_TAG_compile_unit:
348e048f 9826 case DW_TAG_type_unit:
71c25dea
TT
9827 /* Compilation units have a DW_AT_name that is a filename, not
9828 a source language identifier. */
9829 case DW_TAG_enumeration_type:
9830 case DW_TAG_enumerator:
9831 /* These tags always have simple identifiers already; no need
9832 to canonicalize them. */
9833 part_die->name = DW_STRING (&attr);
9834 break;
9835 default:
9836 part_die->name
9837 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9838 &objfile->objfile_obstack);
71c25dea
TT
9839 break;
9840 }
c906108c 9841 break;
31ef98ae 9842 case DW_AT_linkage_name:
c906108c 9843 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9844 /* Note that both forms of linkage name might appear. We
9845 assume they will be the same, and we only store the last
9846 one we see. */
94af9270
KS
9847 if (cu->language == language_ada)
9848 part_die->name = DW_STRING (&attr);
abc72ce4 9849 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9850 break;
9851 case DW_AT_low_pc:
9852 has_low_pc_attr = 1;
9853 part_die->lowpc = DW_ADDR (&attr);
9854 break;
9855 case DW_AT_high_pc:
9856 has_high_pc_attr = 1;
9857 part_die->highpc = DW_ADDR (&attr);
9858 break;
9859 case DW_AT_location:
0963b4bd 9860 /* Support the .debug_loc offsets. */
8e19ed76
PS
9861 if (attr_form_is_block (&attr))
9862 {
9863 part_die->locdesc = DW_BLOCK (&attr);
9864 }
3690dd37 9865 else if (attr_form_is_section_offset (&attr))
8e19ed76 9866 {
4d3c2250 9867 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9868 }
9869 else
9870 {
4d3c2250
KB
9871 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9872 "partial symbol information");
8e19ed76 9873 }
c906108c 9874 break;
c906108c
SS
9875 case DW_AT_external:
9876 part_die->is_external = DW_UNSND (&attr);
9877 break;
9878 case DW_AT_declaration:
9879 part_die->is_declaration = DW_UNSND (&attr);
9880 break;
9881 case DW_AT_type:
9882 part_die->has_type = 1;
9883 break;
9884 case DW_AT_abstract_origin:
9885 case DW_AT_specification:
72bf9492
DJ
9886 case DW_AT_extension:
9887 part_die->has_specification = 1;
c764a876 9888 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9889 break;
9890 case DW_AT_sibling:
9891 /* Ignore absolute siblings, they might point outside of
9892 the current compile unit. */
9893 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9894 complaint (&symfile_complaints,
9895 _("ignoring absolute DW_AT_sibling"));
c906108c 9896 else
b64f50a1 9897 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 9898 break;
fa4028e9
JB
9899 case DW_AT_byte_size:
9900 part_die->has_byte_size = 1;
9901 break;
68511cec
CES
9902 case DW_AT_calling_convention:
9903 /* DWARF doesn't provide a way to identify a program's source-level
9904 entry point. DW_AT_calling_convention attributes are only meant
9905 to describe functions' calling conventions.
9906
9907 However, because it's a necessary piece of information in
9908 Fortran, and because DW_CC_program is the only piece of debugging
9909 information whose definition refers to a 'main program' at all,
9910 several compilers have begun marking Fortran main programs with
9911 DW_CC_program --- even when those functions use the standard
9912 calling conventions.
9913
9914 So until DWARF specifies a way to provide this information and
9915 compilers pick up the new representation, we'll support this
9916 practice. */
9917 if (DW_UNSND (&attr) == DW_CC_program
9918 && cu->language == language_fortran)
01f8c46d
JK
9919 {
9920 set_main_name (part_die->name);
9921
9922 /* As this DIE has a static linkage the name would be difficult
9923 to look up later. */
9924 language_of_main = language_fortran;
9925 }
68511cec 9926 break;
481860b3
GB
9927 case DW_AT_inline:
9928 if (DW_UNSND (&attr) == DW_INL_inlined
9929 || DW_UNSND (&attr) == DW_INL_declared_inlined)
9930 part_die->may_be_inlined = 1;
9931 break;
c906108c
SS
9932 default:
9933 break;
9934 }
9935 }
9936
9373cf26
JK
9937 if (has_low_pc_attr && has_high_pc_attr)
9938 {
9939 /* When using the GNU linker, .gnu.linkonce. sections are used to
9940 eliminate duplicate copies of functions and vtables and such.
9941 The linker will arbitrarily choose one and discard the others.
9942 The AT_*_pc values for such functions refer to local labels in
9943 these sections. If the section from that file was discarded, the
9944 labels are not in the output, so the relocs get a value of 0.
9945 If this is a discarded function, mark the pc bounds as invalid,
9946 so that GDB will ignore it. */
9947 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9948 {
bb5ed363 9949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9950
9951 complaint (&symfile_complaints,
9952 _("DW_AT_low_pc %s is zero "
9953 "for DIE at 0x%x [in module %s]"),
9954 paddress (gdbarch, part_die->lowpc),
b64f50a1 9955 part_die->offset.sect_off, objfile->name);
9373cf26
JK
9956 }
9957 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9958 else if (part_die->lowpc >= part_die->highpc)
9959 {
bb5ed363 9960 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9961
9962 complaint (&symfile_complaints,
9963 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9964 "for DIE at 0x%x [in module %s]"),
9965 paddress (gdbarch, part_die->lowpc),
9966 paddress (gdbarch, part_die->highpc),
b64f50a1 9967 part_die->offset.sect_off, objfile->name);
9373cf26
JK
9968 }
9969 else
9970 part_die->has_pc_info = 1;
9971 }
85cbf3d3 9972
c906108c
SS
9973 return info_ptr;
9974}
9975
72bf9492
DJ
9976/* Find a cached partial DIE at OFFSET in CU. */
9977
9978static struct partial_die_info *
b64f50a1 9979find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
9980{
9981 struct partial_die_info *lookup_die = NULL;
9982 struct partial_die_info part_die;
9983
9984 part_die.offset = offset;
b64f50a1
JK
9985 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
9986 offset.sect_off);
72bf9492 9987
72bf9492
DJ
9988 return lookup_die;
9989}
9990
348e048f
DE
9991/* Find a partial DIE at OFFSET, which may or may not be in CU,
9992 except in the case of .debug_types DIEs which do not reference
9993 outside their CU (they do however referencing other types via
55f1336d 9994 DW_FORM_ref_sig8). */
72bf9492
DJ
9995
9996static struct partial_die_info *
b64f50a1 9997find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 9998{
bb5ed363 9999 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
10000 struct dwarf2_per_cu_data *per_cu = NULL;
10001 struct partial_die_info *pd = NULL;
72bf9492 10002
45452591 10003 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
10004 {
10005 pd = find_partial_die_in_comp_unit (offset, cu);
10006 if (pd != NULL)
10007 return pd;
0d99eb77
DE
10008 /* We missed recording what we needed.
10009 Load all dies and try again. */
10010 per_cu = cu->per_cu;
5afb4e99 10011 }
0d99eb77
DE
10012 else
10013 {
10014 /* TUs don't reference other CUs/TUs (except via type signatures). */
10015 if (cu->per_cu->debug_types_section)
10016 {
10017 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
10018 " external reference to offset 0x%lx [in module %s].\n"),
10019 (long) cu->header.offset.sect_off, (long) offset.sect_off,
10020 bfd_get_filename (objfile->obfd));
10021 }
10022 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 10023
0d99eb77
DE
10024 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10025 load_partial_comp_unit (per_cu);
ae038cb0 10026
0d99eb77
DE
10027 per_cu->cu->last_used = 0;
10028 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10029 }
5afb4e99
DJ
10030
10031 if (pd == NULL && per_cu->load_all_dies == 0)
10032 {
10033 struct cleanup *back_to;
10034 struct partial_die_info comp_unit_die;
10035 struct abbrev_info *abbrev;
10036 unsigned int bytes_read;
10037 char *info_ptr;
0d99eb77 10038 struct dwarf2_section_info *sec;
5afb4e99
DJ
10039
10040 per_cu->load_all_dies = 1;
10041
0d99eb77
DE
10042 if (per_cu->debug_types_section)
10043 sec = per_cu->debug_types_section;
10044 else
10045 sec = &dwarf2_per_objfile->info;
10046
10047 /* Re-read the DIEs, this time reading all of them.
10048 NOTE: We don't discard the previous set of DIEs.
10049 This doesn't happen very often so it's (hopefully) not a problem. */
5afb4e99
DJ
10050 back_to = make_cleanup (null_cleanup, 0);
10051 if (per_cu->cu->dwarf2_abbrevs == NULL)
10052 {
e5fe5e75 10053 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 10054 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10055 }
0d99eb77 10056 info_ptr = (sec->buffer
b64f50a1
JK
10057 + per_cu->cu->header.offset.sect_off
10058 + per_cu->cu->header.first_die_offset.cu_off);
5afb4e99
DJ
10059 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10060 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
0d99eb77 10061 objfile->obfd, sec->buffer, info_ptr,
5afb4e99
DJ
10062 per_cu->cu);
10063 if (comp_unit_die.has_children)
0d99eb77
DE
10064 load_partial_dies (objfile->obfd, sec->buffer, info_ptr, 0,
10065 per_cu->cu);
5afb4e99
DJ
10066 do_cleanups (back_to);
10067
10068 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10069 }
10070
10071 if (pd == NULL)
10072 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10073 _("could not find partial DIE 0x%x "
10074 "in cache [from module %s]\n"),
b64f50a1 10075 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 10076 return pd;
72bf9492
DJ
10077}
10078
abc72ce4
DE
10079/* See if we can figure out if the class lives in a namespace. We do
10080 this by looking for a member function; its demangled name will
10081 contain namespace info, if there is any. */
10082
10083static void
10084guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10085 struct dwarf2_cu *cu)
10086{
10087 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10088 what template types look like, because the demangler
10089 frequently doesn't give the same name as the debug info. We
10090 could fix this by only using the demangled name to get the
10091 prefix (but see comment in read_structure_type). */
10092
10093 struct partial_die_info *real_pdi;
10094 struct partial_die_info *child_pdi;
10095
10096 /* If this DIE (this DIE's specification, if any) has a parent, then
10097 we should not do this. We'll prepend the parent's fully qualified
10098 name when we create the partial symbol. */
10099
10100 real_pdi = struct_pdi;
10101 while (real_pdi->has_specification)
10102 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10103
10104 if (real_pdi->die_parent != NULL)
10105 return;
10106
10107 for (child_pdi = struct_pdi->die_child;
10108 child_pdi != NULL;
10109 child_pdi = child_pdi->die_sibling)
10110 {
10111 if (child_pdi->tag == DW_TAG_subprogram
10112 && child_pdi->linkage_name != NULL)
10113 {
10114 char *actual_class_name
10115 = language_class_name_from_physname (cu->language_defn,
10116 child_pdi->linkage_name);
10117 if (actual_class_name != NULL)
10118 {
10119 struct_pdi->name
10120 = obsavestring (actual_class_name,
10121 strlen (actual_class_name),
10122 &cu->objfile->objfile_obstack);
10123 xfree (actual_class_name);
10124 }
10125 break;
10126 }
10127 }
10128}
10129
72bf9492
DJ
10130/* Adjust PART_DIE before generating a symbol for it. This function
10131 may set the is_external flag or change the DIE's name. */
10132
10133static void
10134fixup_partial_die (struct partial_die_info *part_die,
10135 struct dwarf2_cu *cu)
10136{
abc72ce4
DE
10137 /* Once we've fixed up a die, there's no point in doing so again.
10138 This also avoids a memory leak if we were to call
10139 guess_partial_die_structure_name multiple times. */
10140 if (part_die->fixup_called)
10141 return;
10142
72bf9492
DJ
10143 /* If we found a reference attribute and the DIE has no name, try
10144 to find a name in the referred to DIE. */
10145
10146 if (part_die->name == NULL && part_die->has_specification)
10147 {
10148 struct partial_die_info *spec_die;
72bf9492 10149
10b3939b 10150 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10151
10b3939b 10152 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10153
10154 if (spec_die->name)
10155 {
10156 part_die->name = spec_die->name;
10157
10158 /* Copy DW_AT_external attribute if it is set. */
10159 if (spec_die->is_external)
10160 part_die->is_external = spec_die->is_external;
10161 }
10162 }
10163
10164 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10165
10166 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10167 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10168
abc72ce4
DE
10169 /* If there is no parent die to provide a namespace, and there are
10170 children, see if we can determine the namespace from their linkage
122d1940 10171 name. */
abc72ce4 10172 if (cu->language == language_cplus
8b70b953 10173 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10174 && part_die->die_parent == NULL
10175 && part_die->has_children
10176 && (part_die->tag == DW_TAG_class_type
10177 || part_die->tag == DW_TAG_structure_type
10178 || part_die->tag == DW_TAG_union_type))
10179 guess_partial_die_structure_name (part_die, cu);
10180
53832f31
TT
10181 /* GCC might emit a nameless struct or union that has a linkage
10182 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10183 if (part_die->name == NULL
96408a79
SA
10184 && (part_die->tag == DW_TAG_class_type
10185 || part_die->tag == DW_TAG_interface_type
10186 || part_die->tag == DW_TAG_structure_type
10187 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10188 && part_die->linkage_name != NULL)
10189 {
10190 char *demangled;
10191
10192 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10193 if (demangled)
10194 {
96408a79
SA
10195 const char *base;
10196
10197 /* Strip any leading namespaces/classes, keep only the base name.
10198 DW_AT_name for named DIEs does not contain the prefixes. */
10199 base = strrchr (demangled, ':');
10200 if (base && base > demangled && base[-1] == ':')
10201 base++;
10202 else
10203 base = demangled;
10204
10205 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10206 &cu->objfile->objfile_obstack);
10207 xfree (demangled);
10208 }
10209 }
10210
abc72ce4 10211 part_die->fixup_called = 1;
72bf9492
DJ
10212}
10213
a8329558 10214/* Read an attribute value described by an attribute form. */
c906108c 10215
fe1b8b76 10216static gdb_byte *
a8329558 10217read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10218 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10219 struct dwarf2_cu *cu)
c906108c 10220{
e7c27a73 10221 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10222 unsigned int bytes_read;
10223 struct dwarf_block *blk;
10224
a8329558
KW
10225 attr->form = form;
10226 switch (form)
c906108c 10227 {
c906108c 10228 case DW_FORM_ref_addr:
ae411497 10229 if (cu->header.version == 2)
4568ecf9 10230 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 10231 else
4568ecf9
DE
10232 DW_UNSND (attr) = read_offset (abfd, info_ptr,
10233 &cu->header, &bytes_read);
ae411497
TT
10234 info_ptr += bytes_read;
10235 break;
10236 case DW_FORM_addr:
e7c27a73 10237 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10238 info_ptr += bytes_read;
c906108c
SS
10239 break;
10240 case DW_FORM_block2:
7b5a2f43 10241 blk = dwarf_alloc_block (cu);
c906108c
SS
10242 blk->size = read_2_bytes (abfd, info_ptr);
10243 info_ptr += 2;
10244 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10245 info_ptr += blk->size;
10246 DW_BLOCK (attr) = blk;
10247 break;
10248 case DW_FORM_block4:
7b5a2f43 10249 blk = dwarf_alloc_block (cu);
c906108c
SS
10250 blk->size = read_4_bytes (abfd, info_ptr);
10251 info_ptr += 4;
10252 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10253 info_ptr += blk->size;
10254 DW_BLOCK (attr) = blk;
10255 break;
10256 case DW_FORM_data2:
10257 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10258 info_ptr += 2;
10259 break;
10260 case DW_FORM_data4:
10261 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10262 info_ptr += 4;
10263 break;
10264 case DW_FORM_data8:
10265 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10266 info_ptr += 8;
10267 break;
2dc7f7b3
TT
10268 case DW_FORM_sec_offset:
10269 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10270 info_ptr += bytes_read;
10271 break;
c906108c 10272 case DW_FORM_string:
9b1c24c8 10273 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10274 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10275 info_ptr += bytes_read;
10276 break;
4bdf3d34
JJ
10277 case DW_FORM_strp:
10278 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10279 &bytes_read);
8285870a 10280 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10281 info_ptr += bytes_read;
10282 break;
2dc7f7b3 10283 case DW_FORM_exprloc:
c906108c 10284 case DW_FORM_block:
7b5a2f43 10285 blk = dwarf_alloc_block (cu);
c906108c
SS
10286 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10287 info_ptr += bytes_read;
10288 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10289 info_ptr += blk->size;
10290 DW_BLOCK (attr) = blk;
10291 break;
10292 case DW_FORM_block1:
7b5a2f43 10293 blk = dwarf_alloc_block (cu);
c906108c
SS
10294 blk->size = read_1_byte (abfd, info_ptr);
10295 info_ptr += 1;
10296 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10297 info_ptr += blk->size;
10298 DW_BLOCK (attr) = blk;
10299 break;
10300 case DW_FORM_data1:
10301 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10302 info_ptr += 1;
10303 break;
10304 case DW_FORM_flag:
10305 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10306 info_ptr += 1;
10307 break;
2dc7f7b3
TT
10308 case DW_FORM_flag_present:
10309 DW_UNSND (attr) = 1;
10310 break;
c906108c
SS
10311 case DW_FORM_sdata:
10312 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10313 info_ptr += bytes_read;
10314 break;
10315 case DW_FORM_udata:
10316 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10317 info_ptr += bytes_read;
10318 break;
10319 case DW_FORM_ref1:
4568ecf9
DE
10320 DW_UNSND (attr) = (cu->header.offset.sect_off
10321 + read_1_byte (abfd, info_ptr));
c906108c
SS
10322 info_ptr += 1;
10323 break;
10324 case DW_FORM_ref2:
4568ecf9
DE
10325 DW_UNSND (attr) = (cu->header.offset.sect_off
10326 + read_2_bytes (abfd, info_ptr));
c906108c
SS
10327 info_ptr += 2;
10328 break;
10329 case DW_FORM_ref4:
4568ecf9
DE
10330 DW_UNSND (attr) = (cu->header.offset.sect_off
10331 + read_4_bytes (abfd, info_ptr));
c906108c
SS
10332 info_ptr += 4;
10333 break;
613e1657 10334 case DW_FORM_ref8:
4568ecf9
DE
10335 DW_UNSND (attr) = (cu->header.offset.sect_off
10336 + read_8_bytes (abfd, info_ptr));
613e1657
KB
10337 info_ptr += 8;
10338 break;
55f1336d 10339 case DW_FORM_ref_sig8:
348e048f
DE
10340 /* Convert the signature to something we can record in DW_UNSND
10341 for later lookup.
10342 NOTE: This is NULL if the type wasn't found. */
10343 DW_SIGNATURED_TYPE (attr) =
e319fa28 10344 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
10345 info_ptr += 8;
10346 break;
c906108c 10347 case DW_FORM_ref_udata:
4568ecf9
DE
10348 DW_UNSND (attr) = (cu->header.offset.sect_off
10349 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10350 info_ptr += bytes_read;
10351 break;
c906108c 10352 case DW_FORM_indirect:
a8329558
KW
10353 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10354 info_ptr += bytes_read;
e7c27a73 10355 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10356 break;
c906108c 10357 default:
8a3fe4f8 10358 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10359 dwarf_form_name (form),
10360 bfd_get_filename (abfd));
c906108c 10361 }
28e94949
JB
10362
10363 /* We have seen instances where the compiler tried to emit a byte
10364 size attribute of -1 which ended up being encoded as an unsigned
10365 0xffffffff. Although 0xffffffff is technically a valid size value,
10366 an object of this size seems pretty unlikely so we can relatively
10367 safely treat these cases as if the size attribute was invalid and
10368 treat them as zero by default. */
10369 if (attr->name == DW_AT_byte_size
10370 && form == DW_FORM_data4
10371 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10372 {
10373 complaint
10374 (&symfile_complaints,
43bbcdc2
PH
10375 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10376 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10377 DW_UNSND (attr) = 0;
10378 }
28e94949 10379
c906108c
SS
10380 return info_ptr;
10381}
10382
a8329558
KW
10383/* Read an attribute described by an abbreviated attribute. */
10384
fe1b8b76 10385static gdb_byte *
a8329558 10386read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10387 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10388{
10389 attr->name = abbrev->name;
e7c27a73 10390 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10391}
10392
0963b4bd 10393/* Read dwarf information from a buffer. */
c906108c
SS
10394
10395static unsigned int
fe1b8b76 10396read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10397{
fe1b8b76 10398 return bfd_get_8 (abfd, buf);
c906108c
SS
10399}
10400
10401static int
fe1b8b76 10402read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10403{
fe1b8b76 10404 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10405}
10406
10407static unsigned int
fe1b8b76 10408read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10409{
fe1b8b76 10410 return bfd_get_16 (abfd, buf);
c906108c
SS
10411}
10412
21ae7a4d
JK
10413static int
10414read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10415{
10416 return bfd_get_signed_16 (abfd, buf);
10417}
10418
c906108c 10419static unsigned int
fe1b8b76 10420read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10421{
fe1b8b76 10422 return bfd_get_32 (abfd, buf);
c906108c
SS
10423}
10424
21ae7a4d
JK
10425static int
10426read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10427{
10428 return bfd_get_signed_32 (abfd, buf);
10429}
10430
93311388 10431static ULONGEST
fe1b8b76 10432read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10433{
fe1b8b76 10434 return bfd_get_64 (abfd, buf);
c906108c
SS
10435}
10436
10437static CORE_ADDR
fe1b8b76 10438read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10439 unsigned int *bytes_read)
c906108c 10440{
e7c27a73 10441 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10442 CORE_ADDR retval = 0;
10443
107d2387 10444 if (cu_header->signed_addr_p)
c906108c 10445 {
107d2387
AC
10446 switch (cu_header->addr_size)
10447 {
10448 case 2:
fe1b8b76 10449 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10450 break;
10451 case 4:
fe1b8b76 10452 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10453 break;
10454 case 8:
fe1b8b76 10455 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10456 break;
10457 default:
8e65ff28 10458 internal_error (__FILE__, __LINE__,
e2e0b3e5 10459 _("read_address: bad switch, signed [in module %s]"),
659b0389 10460 bfd_get_filename (abfd));
107d2387
AC
10461 }
10462 }
10463 else
10464 {
10465 switch (cu_header->addr_size)
10466 {
10467 case 2:
fe1b8b76 10468 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10469 break;
10470 case 4:
fe1b8b76 10471 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10472 break;
10473 case 8:
fe1b8b76 10474 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10475 break;
10476 default:
8e65ff28 10477 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10478 _("read_address: bad switch, "
10479 "unsigned [in module %s]"),
659b0389 10480 bfd_get_filename (abfd));
107d2387 10481 }
c906108c 10482 }
64367e0a 10483
107d2387
AC
10484 *bytes_read = cu_header->addr_size;
10485 return retval;
c906108c
SS
10486}
10487
f7ef9339 10488/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10489 specification allows the initial length to take up either 4 bytes
10490 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10491 bytes describe the length and all offsets will be 8 bytes in length
10492 instead of 4.
10493
f7ef9339
KB
10494 An older, non-standard 64-bit format is also handled by this
10495 function. The older format in question stores the initial length
10496 as an 8-byte quantity without an escape value. Lengths greater
10497 than 2^32 aren't very common which means that the initial 4 bytes
10498 is almost always zero. Since a length value of zero doesn't make
10499 sense for the 32-bit format, this initial zero can be considered to
10500 be an escape value which indicates the presence of the older 64-bit
10501 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10502 greater than 4GB. If it becomes necessary to handle lengths
10503 somewhat larger than 4GB, we could allow other small values (such
10504 as the non-sensical values of 1, 2, and 3) to also be used as
10505 escape values indicating the presence of the old format.
f7ef9339 10506
917c78fc
MK
10507 The value returned via bytes_read should be used to increment the
10508 relevant pointer after calling read_initial_length().
c764a876 10509
613e1657
KB
10510 [ Note: read_initial_length() and read_offset() are based on the
10511 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10512 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10513 from:
10514
f7ef9339 10515 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10516
613e1657
KB
10517 This document is only a draft and is subject to change. (So beware.)
10518
f7ef9339 10519 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10520 determined empirically by examining 64-bit ELF files produced by
10521 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10522
10523 - Kevin, July 16, 2002
613e1657
KB
10524 ] */
10525
10526static LONGEST
c764a876 10527read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10528{
fe1b8b76 10529 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10530
dd373385 10531 if (length == 0xffffffff)
613e1657 10532 {
fe1b8b76 10533 length = bfd_get_64 (abfd, buf + 4);
613e1657 10534 *bytes_read = 12;
613e1657 10535 }
dd373385 10536 else if (length == 0)
f7ef9339 10537 {
dd373385 10538 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10539 length = bfd_get_64 (abfd, buf);
f7ef9339 10540 *bytes_read = 8;
f7ef9339 10541 }
613e1657
KB
10542 else
10543 {
10544 *bytes_read = 4;
613e1657
KB
10545 }
10546
c764a876
DE
10547 return length;
10548}
dd373385 10549
c764a876
DE
10550/* Cover function for read_initial_length.
10551 Returns the length of the object at BUF, and stores the size of the
10552 initial length in *BYTES_READ and stores the size that offsets will be in
10553 *OFFSET_SIZE.
10554 If the initial length size is not equivalent to that specified in
10555 CU_HEADER then issue a complaint.
10556 This is useful when reading non-comp-unit headers. */
dd373385 10557
c764a876
DE
10558static LONGEST
10559read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10560 const struct comp_unit_head *cu_header,
10561 unsigned int *bytes_read,
10562 unsigned int *offset_size)
10563{
10564 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10565
10566 gdb_assert (cu_header->initial_length_size == 4
10567 || cu_header->initial_length_size == 8
10568 || cu_header->initial_length_size == 12);
10569
10570 if (cu_header->initial_length_size != *bytes_read)
10571 complaint (&symfile_complaints,
10572 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10573
c764a876 10574 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10575 return length;
613e1657
KB
10576}
10577
10578/* Read an offset from the data stream. The size of the offset is
917c78fc 10579 given by cu_header->offset_size. */
613e1657
KB
10580
10581static LONGEST
fe1b8b76 10582read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10583 unsigned int *bytes_read)
c764a876
DE
10584{
10585 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10586
c764a876
DE
10587 *bytes_read = cu_header->offset_size;
10588 return offset;
10589}
10590
10591/* Read an offset from the data stream. */
10592
10593static LONGEST
10594read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10595{
10596 LONGEST retval = 0;
10597
c764a876 10598 switch (offset_size)
613e1657
KB
10599 {
10600 case 4:
fe1b8b76 10601 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10602 break;
10603 case 8:
fe1b8b76 10604 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10605 break;
10606 default:
8e65ff28 10607 internal_error (__FILE__, __LINE__,
c764a876 10608 _("read_offset_1: bad switch [in module %s]"),
659b0389 10609 bfd_get_filename (abfd));
613e1657
KB
10610 }
10611
917c78fc 10612 return retval;
613e1657
KB
10613}
10614
fe1b8b76
JB
10615static gdb_byte *
10616read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10617{
10618 /* If the size of a host char is 8 bits, we can return a pointer
10619 to the buffer, otherwise we have to copy the data to a buffer
10620 allocated on the temporary obstack. */
4bdf3d34 10621 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10622 return buf;
c906108c
SS
10623}
10624
10625static char *
9b1c24c8 10626read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10627{
10628 /* If the size of a host char is 8 bits, we can return a pointer
10629 to the string, otherwise we have to copy the string to a buffer
10630 allocated on the temporary obstack. */
4bdf3d34 10631 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10632 if (*buf == '\0')
10633 {
10634 *bytes_read_ptr = 1;
10635 return NULL;
10636 }
fe1b8b76
JB
10637 *bytes_read_ptr = strlen ((char *) buf) + 1;
10638 return (char *) buf;
4bdf3d34
JJ
10639}
10640
10641static char *
cf2c3c16 10642read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10643{
be391dca 10644 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10645 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10646 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10647 bfd_get_filename (abfd));
dce234bc 10648 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10649 error (_("DW_FORM_strp pointing outside of "
10650 ".debug_str section [in module %s]"),
10651 bfd_get_filename (abfd));
4bdf3d34 10652 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10653 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10654 return NULL;
dce234bc 10655 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10656}
10657
cf2c3c16
TT
10658static char *
10659read_indirect_string (bfd *abfd, gdb_byte *buf,
10660 const struct comp_unit_head *cu_header,
10661 unsigned int *bytes_read_ptr)
10662{
10663 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10664
10665 return read_indirect_string_at_offset (abfd, str_offset);
10666}
10667
12df843f 10668static ULONGEST
fe1b8b76 10669read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10670{
12df843f 10671 ULONGEST result;
ce5d95e1 10672 unsigned int num_read;
c906108c
SS
10673 int i, shift;
10674 unsigned char byte;
10675
10676 result = 0;
10677 shift = 0;
10678 num_read = 0;
10679 i = 0;
10680 while (1)
10681 {
fe1b8b76 10682 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10683 buf++;
10684 num_read++;
12df843f 10685 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
10686 if ((byte & 128) == 0)
10687 {
10688 break;
10689 }
10690 shift += 7;
10691 }
10692 *bytes_read_ptr = num_read;
10693 return result;
10694}
10695
12df843f 10696static LONGEST
fe1b8b76 10697read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10698{
12df843f 10699 LONGEST result;
77e0b926 10700 int i, shift, num_read;
c906108c
SS
10701 unsigned char byte;
10702
10703 result = 0;
10704 shift = 0;
c906108c
SS
10705 num_read = 0;
10706 i = 0;
10707 while (1)
10708 {
fe1b8b76 10709 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10710 buf++;
10711 num_read++;
12df843f 10712 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
10713 shift += 7;
10714 if ((byte & 128) == 0)
10715 {
10716 break;
10717 }
10718 }
77e0b926 10719 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 10720 result |= -(((LONGEST) 1) << shift);
c906108c
SS
10721 *bytes_read_ptr = num_read;
10722 return result;
10723}
10724
4bb7a0a7
DJ
10725/* Return a pointer to just past the end of an LEB128 number in BUF. */
10726
fe1b8b76
JB
10727static gdb_byte *
10728skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10729{
10730 int byte;
10731
10732 while (1)
10733 {
fe1b8b76 10734 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10735 buf++;
10736 if ((byte & 128) == 0)
10737 return buf;
10738 }
10739}
10740
c906108c 10741static void
e142c38c 10742set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10743{
10744 switch (lang)
10745 {
10746 case DW_LANG_C89:
76bee0cc 10747 case DW_LANG_C99:
c906108c 10748 case DW_LANG_C:
e142c38c 10749 cu->language = language_c;
c906108c
SS
10750 break;
10751 case DW_LANG_C_plus_plus:
e142c38c 10752 cu->language = language_cplus;
c906108c 10753 break;
6aecb9c2
JB
10754 case DW_LANG_D:
10755 cu->language = language_d;
10756 break;
c906108c
SS
10757 case DW_LANG_Fortran77:
10758 case DW_LANG_Fortran90:
b21b22e0 10759 case DW_LANG_Fortran95:
e142c38c 10760 cu->language = language_fortran;
c906108c
SS
10761 break;
10762 case DW_LANG_Mips_Assembler:
e142c38c 10763 cu->language = language_asm;
c906108c 10764 break;
bebd888e 10765 case DW_LANG_Java:
e142c38c 10766 cu->language = language_java;
bebd888e 10767 break;
c906108c 10768 case DW_LANG_Ada83:
8aaf0b47 10769 case DW_LANG_Ada95:
bc5f45f8
JB
10770 cu->language = language_ada;
10771 break;
72019c9c
GM
10772 case DW_LANG_Modula2:
10773 cu->language = language_m2;
10774 break;
fe8e67fd
PM
10775 case DW_LANG_Pascal83:
10776 cu->language = language_pascal;
10777 break;
22566fbd
DJ
10778 case DW_LANG_ObjC:
10779 cu->language = language_objc;
10780 break;
c906108c
SS
10781 case DW_LANG_Cobol74:
10782 case DW_LANG_Cobol85:
c906108c 10783 default:
e142c38c 10784 cu->language = language_minimal;
c906108c
SS
10785 break;
10786 }
e142c38c 10787 cu->language_defn = language_def (cu->language);
c906108c
SS
10788}
10789
10790/* Return the named attribute or NULL if not there. */
10791
10792static struct attribute *
e142c38c 10793dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 10794{
a48e046c 10795 for (;;)
c906108c 10796 {
a48e046c
TT
10797 unsigned int i;
10798 struct attribute *spec = NULL;
10799
10800 for (i = 0; i < die->num_attrs; ++i)
10801 {
10802 if (die->attrs[i].name == name)
10803 return &die->attrs[i];
10804 if (die->attrs[i].name == DW_AT_specification
10805 || die->attrs[i].name == DW_AT_abstract_origin)
10806 spec = &die->attrs[i];
10807 }
10808
10809 if (!spec)
10810 break;
c906108c 10811
f2f0e013 10812 die = follow_die_ref (die, spec, &cu);
f2f0e013 10813 }
c5aa993b 10814
c906108c
SS
10815 return NULL;
10816}
10817
348e048f
DE
10818/* Return the named attribute or NULL if not there,
10819 but do not follow DW_AT_specification, etc.
10820 This is for use in contexts where we're reading .debug_types dies.
10821 Following DW_AT_specification, DW_AT_abstract_origin will take us
10822 back up the chain, and we want to go down. */
10823
10824static struct attribute *
10825dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10826 struct dwarf2_cu *cu)
10827{
10828 unsigned int i;
10829
10830 for (i = 0; i < die->num_attrs; ++i)
10831 if (die->attrs[i].name == name)
10832 return &die->attrs[i];
10833
10834 return NULL;
10835}
10836
05cf31d1
JB
10837/* Return non-zero iff the attribute NAME is defined for the given DIE,
10838 and holds a non-zero value. This function should only be used for
2dc7f7b3 10839 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10840
10841static int
10842dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10843{
10844 struct attribute *attr = dwarf2_attr (die, name, cu);
10845
10846 return (attr && DW_UNSND (attr));
10847}
10848
3ca72b44 10849static int
e142c38c 10850die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10851{
05cf31d1
JB
10852 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10853 which value is non-zero. However, we have to be careful with
10854 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10855 (via dwarf2_flag_true_p) follows this attribute. So we may
10856 end up accidently finding a declaration attribute that belongs
10857 to a different DIE referenced by the specification attribute,
10858 even though the given DIE does not have a declaration attribute. */
10859 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10860 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10861}
10862
63d06c5c 10863/* Return the die giving the specification for DIE, if there is
f2f0e013 10864 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10865 containing the return value on output. If there is no
10866 specification, but there is an abstract origin, that is
10867 returned. */
63d06c5c
DC
10868
10869static struct die_info *
f2f0e013 10870die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10871{
f2f0e013
DJ
10872 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10873 *spec_cu);
63d06c5c 10874
edb3359d
DJ
10875 if (spec_attr == NULL)
10876 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10877
63d06c5c
DC
10878 if (spec_attr == NULL)
10879 return NULL;
10880 else
f2f0e013 10881 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10882}
c906108c 10883
debd256d 10884/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10885 refers to.
10886 NOTE: This is also used as a "cleanup" function. */
10887
debd256d
JB
10888static void
10889free_line_header (struct line_header *lh)
10890{
10891 if (lh->standard_opcode_lengths)
a8bc7b56 10892 xfree (lh->standard_opcode_lengths);
debd256d
JB
10893
10894 /* Remember that all the lh->file_names[i].name pointers are
10895 pointers into debug_line_buffer, and don't need to be freed. */
10896 if (lh->file_names)
a8bc7b56 10897 xfree (lh->file_names);
debd256d
JB
10898
10899 /* Similarly for the include directory names. */
10900 if (lh->include_dirs)
a8bc7b56 10901 xfree (lh->include_dirs);
debd256d 10902
a8bc7b56 10903 xfree (lh);
debd256d
JB
10904}
10905
debd256d 10906/* Add an entry to LH's include directory table. */
ae2de4f8 10907
debd256d
JB
10908static void
10909add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10910{
debd256d
JB
10911 /* Grow the array if necessary. */
10912 if (lh->include_dirs_size == 0)
c5aa993b 10913 {
debd256d
JB
10914 lh->include_dirs_size = 1; /* for testing */
10915 lh->include_dirs = xmalloc (lh->include_dirs_size
10916 * sizeof (*lh->include_dirs));
10917 }
10918 else if (lh->num_include_dirs >= lh->include_dirs_size)
10919 {
10920 lh->include_dirs_size *= 2;
10921 lh->include_dirs = xrealloc (lh->include_dirs,
10922 (lh->include_dirs_size
10923 * sizeof (*lh->include_dirs)));
c5aa993b 10924 }
c906108c 10925
debd256d
JB
10926 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10927}
6e70227d 10928
debd256d 10929/* Add an entry to LH's file name table. */
ae2de4f8 10930
debd256d
JB
10931static void
10932add_file_name (struct line_header *lh,
10933 char *name,
10934 unsigned int dir_index,
10935 unsigned int mod_time,
10936 unsigned int length)
10937{
10938 struct file_entry *fe;
10939
10940 /* Grow the array if necessary. */
10941 if (lh->file_names_size == 0)
10942 {
10943 lh->file_names_size = 1; /* for testing */
10944 lh->file_names = xmalloc (lh->file_names_size
10945 * sizeof (*lh->file_names));
10946 }
10947 else if (lh->num_file_names >= lh->file_names_size)
10948 {
10949 lh->file_names_size *= 2;
10950 lh->file_names = xrealloc (lh->file_names,
10951 (lh->file_names_size
10952 * sizeof (*lh->file_names)));
10953 }
10954
10955 fe = &lh->file_names[lh->num_file_names++];
10956 fe->name = name;
10957 fe->dir_index = dir_index;
10958 fe->mod_time = mod_time;
10959 fe->length = length;
aaa75496 10960 fe->included_p = 0;
cb1df416 10961 fe->symtab = NULL;
debd256d 10962}
6e70227d 10963
debd256d 10964/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10965 .debug_line, according to the endianness of ABFD. Return a pointer
10966 to a struct line_header, allocated using xmalloc.
debd256d
JB
10967
10968 NOTE: the strings in the include directory and file name tables of
10969 the returned object point into debug_line_buffer, and must not be
10970 freed. */
ae2de4f8 10971
debd256d
JB
10972static struct line_header *
10973dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10974 struct dwarf2_cu *cu)
debd256d
JB
10975{
10976 struct cleanup *back_to;
10977 struct line_header *lh;
fe1b8b76 10978 gdb_byte *line_ptr;
c764a876 10979 unsigned int bytes_read, offset_size;
debd256d
JB
10980 int i;
10981 char *cur_dir, *cur_file;
10982
be391dca 10983 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10984 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10985 {
e2e0b3e5 10986 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10987 return 0;
10988 }
10989
a738430d
MK
10990 /* Make sure that at least there's room for the total_length field.
10991 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10992 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10993 {
4d3c2250 10994 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10995 return 0;
10996 }
10997
10998 lh = xmalloc (sizeof (*lh));
10999 memset (lh, 0, sizeof (*lh));
11000 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11001 (void *) lh);
11002
dce234bc 11003 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 11004
a738430d 11005 /* Read in the header. */
6e70227d 11006 lh->total_length =
c764a876
DE
11007 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11008 &bytes_read, &offset_size);
debd256d 11009 line_ptr += bytes_read;
dce234bc
PP
11010 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11011 + dwarf2_per_objfile->line.size))
debd256d 11012 {
4d3c2250 11013 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
11014 return 0;
11015 }
11016 lh->statement_program_end = line_ptr + lh->total_length;
11017 lh->version = read_2_bytes (abfd, line_ptr);
11018 line_ptr += 2;
c764a876
DE
11019 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11020 line_ptr += offset_size;
debd256d
JB
11021 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11022 line_ptr += 1;
2dc7f7b3
TT
11023 if (lh->version >= 4)
11024 {
11025 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11026 line_ptr += 1;
11027 }
11028 else
11029 lh->maximum_ops_per_instruction = 1;
11030
11031 if (lh->maximum_ops_per_instruction == 0)
11032 {
11033 lh->maximum_ops_per_instruction = 1;
11034 complaint (&symfile_complaints,
3e43a32a
MS
11035 _("invalid maximum_ops_per_instruction "
11036 "in `.debug_line' section"));
2dc7f7b3
TT
11037 }
11038
debd256d
JB
11039 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11040 line_ptr += 1;
11041 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11042 line_ptr += 1;
11043 lh->line_range = read_1_byte (abfd, line_ptr);
11044 line_ptr += 1;
11045 lh->opcode_base = read_1_byte (abfd, line_ptr);
11046 line_ptr += 1;
11047 lh->standard_opcode_lengths
fe1b8b76 11048 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11049
11050 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11051 for (i = 1; i < lh->opcode_base; ++i)
11052 {
11053 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11054 line_ptr += 1;
11055 }
11056
a738430d 11057 /* Read directory table. */
9b1c24c8 11058 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11059 {
11060 line_ptr += bytes_read;
11061 add_include_dir (lh, cur_dir);
11062 }
11063 line_ptr += bytes_read;
11064
a738430d 11065 /* Read file name table. */
9b1c24c8 11066 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11067 {
11068 unsigned int dir_index, mod_time, length;
11069
11070 line_ptr += bytes_read;
11071 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11072 line_ptr += bytes_read;
11073 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11074 line_ptr += bytes_read;
11075 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11076 line_ptr += bytes_read;
11077
11078 add_file_name (lh, cur_file, dir_index, mod_time, length);
11079 }
11080 line_ptr += bytes_read;
6e70227d 11081 lh->statement_program_start = line_ptr;
debd256d 11082
dce234bc
PP
11083 if (line_ptr > (dwarf2_per_objfile->line.buffer
11084 + dwarf2_per_objfile->line.size))
4d3c2250 11085 complaint (&symfile_complaints,
3e43a32a
MS
11086 _("line number info header doesn't "
11087 "fit in `.debug_line' section"));
debd256d
JB
11088
11089 discard_cleanups (back_to);
11090 return lh;
11091}
c906108c 11092
c6da4cef
DE
11093/* Subroutine of dwarf_decode_lines to simplify it.
11094 Return the file name of the psymtab for included file FILE_INDEX
11095 in line header LH of PST.
11096 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11097 If space for the result is malloc'd, it will be freed by a cleanup.
11098 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11099
11100static char *
11101psymtab_include_file_name (const struct line_header *lh, int file_index,
11102 const struct partial_symtab *pst,
11103 const char *comp_dir)
11104{
11105 const struct file_entry fe = lh->file_names [file_index];
11106 char *include_name = fe.name;
11107 char *include_name_to_compare = include_name;
11108 char *dir_name = NULL;
72b9f47f
TT
11109 const char *pst_filename;
11110 char *copied_name = NULL;
c6da4cef
DE
11111 int file_is_pst;
11112
11113 if (fe.dir_index)
11114 dir_name = lh->include_dirs[fe.dir_index - 1];
11115
11116 if (!IS_ABSOLUTE_PATH (include_name)
11117 && (dir_name != NULL || comp_dir != NULL))
11118 {
11119 /* Avoid creating a duplicate psymtab for PST.
11120 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11121 Before we do the comparison, however, we need to account
11122 for DIR_NAME and COMP_DIR.
11123 First prepend dir_name (if non-NULL). If we still don't
11124 have an absolute path prepend comp_dir (if non-NULL).
11125 However, the directory we record in the include-file's
11126 psymtab does not contain COMP_DIR (to match the
11127 corresponding symtab(s)).
11128
11129 Example:
11130
11131 bash$ cd /tmp
11132 bash$ gcc -g ./hello.c
11133 include_name = "hello.c"
11134 dir_name = "."
11135 DW_AT_comp_dir = comp_dir = "/tmp"
11136 DW_AT_name = "./hello.c" */
11137
11138 if (dir_name != NULL)
11139 {
11140 include_name = concat (dir_name, SLASH_STRING,
11141 include_name, (char *)NULL);
11142 include_name_to_compare = include_name;
11143 make_cleanup (xfree, include_name);
11144 }
11145 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11146 {
11147 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11148 include_name, (char *)NULL);
11149 }
11150 }
11151
11152 pst_filename = pst->filename;
11153 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11154 {
72b9f47f
TT
11155 copied_name = concat (pst->dirname, SLASH_STRING,
11156 pst_filename, (char *)NULL);
11157 pst_filename = copied_name;
c6da4cef
DE
11158 }
11159
1e3fad37 11160 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11161
11162 if (include_name_to_compare != include_name)
11163 xfree (include_name_to_compare);
72b9f47f
TT
11164 if (copied_name != NULL)
11165 xfree (copied_name);
c6da4cef
DE
11166
11167 if (file_is_pst)
11168 return NULL;
11169 return include_name;
11170}
11171
c91513d8
PP
11172/* Ignore this record_line request. */
11173
11174static void
11175noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11176{
11177 return;
11178}
11179
f3f5162e
DE
11180/* Subroutine of dwarf_decode_lines to simplify it.
11181 Process the line number information in LH. */
debd256d 11182
c906108c 11183static void
f3f5162e
DE
11184dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11185 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11186{
a8c50c1f 11187 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11188 gdb_byte *line_end;
a8c50c1f 11189 unsigned int bytes_read, extended_len;
c906108c 11190 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11191 CORE_ADDR baseaddr;
11192 struct objfile *objfile = cu->objfile;
f3f5162e 11193 bfd *abfd = objfile->obfd;
fbf65064 11194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11195 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11196 struct subfile *last_subfile = NULL;
c91513d8
PP
11197 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11198 = record_line;
e142c38c
DJ
11199
11200 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11201
debd256d
JB
11202 line_ptr = lh->statement_program_start;
11203 line_end = lh->statement_program_end;
c906108c
SS
11204
11205 /* Read the statement sequences until there's nothing left. */
11206 while (line_ptr < line_end)
11207 {
11208 /* state machine registers */
11209 CORE_ADDR address = 0;
11210 unsigned int file = 1;
11211 unsigned int line = 1;
11212 unsigned int column = 0;
debd256d 11213 int is_stmt = lh->default_is_stmt;
c906108c
SS
11214 int basic_block = 0;
11215 int end_sequence = 0;
fbf65064 11216 CORE_ADDR addr;
2dc7f7b3 11217 unsigned char op_index = 0;
c906108c 11218
aaa75496 11219 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11220 {
aaa75496 11221 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11222 /* lh->include_dirs and lh->file_names are 0-based, but the
11223 directory and file name numbers in the statement program
11224 are 1-based. */
11225 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11226 char *dir = NULL;
a738430d 11227
debd256d
JB
11228 if (fe->dir_index)
11229 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11230
11231 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11232 }
11233
a738430d 11234 /* Decode the table. */
c5aa993b 11235 while (!end_sequence)
c906108c
SS
11236 {
11237 op_code = read_1_byte (abfd, line_ptr);
11238 line_ptr += 1;
59205f5a
JB
11239 if (line_ptr > line_end)
11240 {
11241 dwarf2_debug_line_missing_end_sequence_complaint ();
11242 break;
11243 }
9aa1fe7e 11244
debd256d 11245 if (op_code >= lh->opcode_base)
6e70227d 11246 {
a738430d 11247 /* Special operand. */
debd256d 11248 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11249 address += (((op_index + (adj_opcode / lh->line_range))
11250 / lh->maximum_ops_per_instruction)
11251 * lh->minimum_instruction_length);
11252 op_index = ((op_index + (adj_opcode / lh->line_range))
11253 % lh->maximum_ops_per_instruction);
debd256d 11254 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11255 if (lh->num_file_names < file || file == 0)
25e43795 11256 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11257 /* For now we ignore lines not starting on an
11258 instruction boundary. */
11259 else if (op_index == 0)
25e43795
DJ
11260 {
11261 lh->file_names[file - 1].included_p = 1;
ca5f395d 11262 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11263 {
11264 if (last_subfile != current_subfile)
11265 {
11266 addr = gdbarch_addr_bits_remove (gdbarch, address);
11267 if (last_subfile)
c91513d8 11268 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11269 last_subfile = current_subfile;
11270 }
25e43795 11271 /* Append row to matrix using current values. */
7019d805 11272 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11273 (*p_record_line) (current_subfile, line, addr);
366da635 11274 }
25e43795 11275 }
ca5f395d 11276 basic_block = 0;
9aa1fe7e
GK
11277 }
11278 else switch (op_code)
c906108c
SS
11279 {
11280 case DW_LNS_extended_op:
3e43a32a
MS
11281 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11282 &bytes_read);
473b7be6 11283 line_ptr += bytes_read;
a8c50c1f 11284 extended_end = line_ptr + extended_len;
c906108c
SS
11285 extended_op = read_1_byte (abfd, line_ptr);
11286 line_ptr += 1;
11287 switch (extended_op)
11288 {
11289 case DW_LNE_end_sequence:
c91513d8 11290 p_record_line = record_line;
c906108c 11291 end_sequence = 1;
c906108c
SS
11292 break;
11293 case DW_LNE_set_address:
e7c27a73 11294 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11295
11296 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11297 {
11298 /* This line table is for a function which has been
11299 GCd by the linker. Ignore it. PR gdb/12528 */
11300
11301 long line_offset
11302 = line_ptr - dwarf2_per_objfile->line.buffer;
11303
11304 complaint (&symfile_complaints,
11305 _(".debug_line address at offset 0x%lx is 0 "
11306 "[in module %s]"),
bb5ed363 11307 line_offset, objfile->name);
c91513d8
PP
11308 p_record_line = noop_record_line;
11309 }
11310
2dc7f7b3 11311 op_index = 0;
107d2387
AC
11312 line_ptr += bytes_read;
11313 address += baseaddr;
c906108c
SS
11314 break;
11315 case DW_LNE_define_file:
debd256d
JB
11316 {
11317 char *cur_file;
11318 unsigned int dir_index, mod_time, length;
6e70227d 11319
3e43a32a
MS
11320 cur_file = read_direct_string (abfd, line_ptr,
11321 &bytes_read);
debd256d
JB
11322 line_ptr += bytes_read;
11323 dir_index =
11324 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11325 line_ptr += bytes_read;
11326 mod_time =
11327 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11328 line_ptr += bytes_read;
11329 length =
11330 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11331 line_ptr += bytes_read;
11332 add_file_name (lh, cur_file, dir_index, mod_time, length);
11333 }
c906108c 11334 break;
d0c6ba3d
CC
11335 case DW_LNE_set_discriminator:
11336 /* The discriminator is not interesting to the debugger;
11337 just ignore it. */
11338 line_ptr = extended_end;
11339 break;
c906108c 11340 default:
4d3c2250 11341 complaint (&symfile_complaints,
e2e0b3e5 11342 _("mangled .debug_line section"));
debd256d 11343 return;
c906108c 11344 }
a8c50c1f
DJ
11345 /* Make sure that we parsed the extended op correctly. If e.g.
11346 we expected a different address size than the producer used,
11347 we may have read the wrong number of bytes. */
11348 if (line_ptr != extended_end)
11349 {
11350 complaint (&symfile_complaints,
11351 _("mangled .debug_line section"));
11352 return;
11353 }
c906108c
SS
11354 break;
11355 case DW_LNS_copy:
59205f5a 11356 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11357 dwarf2_debug_line_missing_file_complaint ();
11358 else
366da635 11359 {
25e43795 11360 lh->file_names[file - 1].included_p = 1;
ca5f395d 11361 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11362 {
11363 if (last_subfile != current_subfile)
11364 {
11365 addr = gdbarch_addr_bits_remove (gdbarch, address);
11366 if (last_subfile)
c91513d8 11367 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11368 last_subfile = current_subfile;
11369 }
7019d805 11370 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11371 (*p_record_line) (current_subfile, line, addr);
fbf65064 11372 }
366da635 11373 }
c906108c
SS
11374 basic_block = 0;
11375 break;
11376 case DW_LNS_advance_pc:
2dc7f7b3
TT
11377 {
11378 CORE_ADDR adjust
11379 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11380
11381 address += (((op_index + adjust)
11382 / lh->maximum_ops_per_instruction)
11383 * lh->minimum_instruction_length);
11384 op_index = ((op_index + adjust)
11385 % lh->maximum_ops_per_instruction);
11386 line_ptr += bytes_read;
11387 }
c906108c
SS
11388 break;
11389 case DW_LNS_advance_line:
11390 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11391 line_ptr += bytes_read;
11392 break;
11393 case DW_LNS_set_file:
debd256d 11394 {
a738430d
MK
11395 /* The arrays lh->include_dirs and lh->file_names are
11396 0-based, but the directory and file name numbers in
11397 the statement program are 1-based. */
debd256d 11398 struct file_entry *fe;
4f1520fb 11399 char *dir = NULL;
a738430d 11400
debd256d
JB
11401 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11402 line_ptr += bytes_read;
59205f5a 11403 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11404 dwarf2_debug_line_missing_file_complaint ();
11405 else
11406 {
11407 fe = &lh->file_names[file - 1];
11408 if (fe->dir_index)
11409 dir = lh->include_dirs[fe->dir_index - 1];
11410 if (!decode_for_pst_p)
11411 {
11412 last_subfile = current_subfile;
11413 dwarf2_start_subfile (fe->name, dir, comp_dir);
11414 }
11415 }
debd256d 11416 }
c906108c
SS
11417 break;
11418 case DW_LNS_set_column:
11419 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11420 line_ptr += bytes_read;
11421 break;
11422 case DW_LNS_negate_stmt:
11423 is_stmt = (!is_stmt);
11424 break;
11425 case DW_LNS_set_basic_block:
11426 basic_block = 1;
11427 break;
c2c6d25f
JM
11428 /* Add to the address register of the state machine the
11429 address increment value corresponding to special opcode
a738430d
MK
11430 255. I.e., this value is scaled by the minimum
11431 instruction length since special opcode 255 would have
b021a221 11432 scaled the increment. */
c906108c 11433 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11434 {
11435 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11436
11437 address += (((op_index + adjust)
11438 / lh->maximum_ops_per_instruction)
11439 * lh->minimum_instruction_length);
11440 op_index = ((op_index + adjust)
11441 % lh->maximum_ops_per_instruction);
11442 }
c906108c
SS
11443 break;
11444 case DW_LNS_fixed_advance_pc:
11445 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11446 op_index = 0;
c906108c
SS
11447 line_ptr += 2;
11448 break;
9aa1fe7e 11449 default:
a738430d
MK
11450 {
11451 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11452 int i;
a738430d 11453
debd256d 11454 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11455 {
11456 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11457 line_ptr += bytes_read;
11458 }
11459 }
c906108c
SS
11460 }
11461 }
59205f5a
JB
11462 if (lh->num_file_names < file || file == 0)
11463 dwarf2_debug_line_missing_file_complaint ();
11464 else
11465 {
11466 lh->file_names[file - 1].included_p = 1;
11467 if (!decode_for_pst_p)
fbf65064
UW
11468 {
11469 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11470 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11471 }
59205f5a 11472 }
c906108c 11473 }
f3f5162e
DE
11474}
11475
11476/* Decode the Line Number Program (LNP) for the given line_header
11477 structure and CU. The actual information extracted and the type
11478 of structures created from the LNP depends on the value of PST.
11479
11480 1. If PST is NULL, then this procedure uses the data from the program
11481 to create all necessary symbol tables, and their linetables.
11482
11483 2. If PST is not NULL, this procedure reads the program to determine
11484 the list of files included by the unit represented by PST, and
11485 builds all the associated partial symbol tables.
11486
11487 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11488 It is used for relative paths in the line table.
11489 NOTE: When processing partial symtabs (pst != NULL),
11490 comp_dir == pst->dirname.
11491
11492 NOTE: It is important that psymtabs have the same file name (via strcmp)
11493 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11494 symtab we don't use it in the name of the psymtabs we create.
11495 E.g. expand_line_sal requires this when finding psymtabs to expand.
11496 A good testcase for this is mb-inline.exp. */
11497
11498static void
11499dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11500 struct dwarf2_cu *cu, struct partial_symtab *pst,
11501 int want_line_info)
11502{
11503 struct objfile *objfile = cu->objfile;
11504 const int decode_for_pst_p = (pst != NULL);
11505 struct subfile *first_subfile = current_subfile;
11506
11507 if (want_line_info)
11508 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11509
11510 if (decode_for_pst_p)
11511 {
11512 int file_index;
11513
11514 /* Now that we're done scanning the Line Header Program, we can
11515 create the psymtab of each included file. */
11516 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11517 if (lh->file_names[file_index].included_p == 1)
11518 {
c6da4cef
DE
11519 char *include_name =
11520 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11521 if (include_name != NULL)
aaa75496
JB
11522 dwarf2_create_include_psymtab (include_name, pst, objfile);
11523 }
11524 }
cb1df416
DJ
11525 else
11526 {
11527 /* Make sure a symtab is created for every file, even files
11528 which contain only variables (i.e. no code with associated
11529 line numbers). */
cb1df416 11530 int i;
cb1df416
DJ
11531
11532 for (i = 0; i < lh->num_file_names; i++)
11533 {
11534 char *dir = NULL;
f3f5162e 11535 struct file_entry *fe;
9a619af0 11536
cb1df416
DJ
11537 fe = &lh->file_names[i];
11538 if (fe->dir_index)
11539 dir = lh->include_dirs[fe->dir_index - 1];
11540 dwarf2_start_subfile (fe->name, dir, comp_dir);
11541
11542 /* Skip the main file; we don't need it, and it must be
11543 allocated last, so that it will show up before the
11544 non-primary symtabs in the objfile's symtab list. */
11545 if (current_subfile == first_subfile)
11546 continue;
11547
11548 if (current_subfile->symtab == NULL)
11549 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11550 objfile);
cb1df416
DJ
11551 fe->symtab = current_subfile->symtab;
11552 }
11553 }
c906108c
SS
11554}
11555
11556/* Start a subfile for DWARF. FILENAME is the name of the file and
11557 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11558 or NULL if not known. COMP_DIR is the compilation directory for the
11559 linetable's compilation unit or NULL if not known.
c906108c
SS
11560 This routine tries to keep line numbers from identical absolute and
11561 relative file names in a common subfile.
11562
11563 Using the `list' example from the GDB testsuite, which resides in
11564 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11565 of /srcdir/list0.c yields the following debugging information for list0.c:
11566
c5aa993b
JM
11567 DW_AT_name: /srcdir/list0.c
11568 DW_AT_comp_dir: /compdir
357e46e7 11569 files.files[0].name: list0.h
c5aa993b 11570 files.files[0].dir: /srcdir
357e46e7 11571 files.files[1].name: list0.c
c5aa993b 11572 files.files[1].dir: /srcdir
c906108c
SS
11573
11574 The line number information for list0.c has to end up in a single
4f1520fb
FR
11575 subfile, so that `break /srcdir/list0.c:1' works as expected.
11576 start_subfile will ensure that this happens provided that we pass the
11577 concatenation of files.files[1].dir and files.files[1].name as the
11578 subfile's name. */
c906108c
SS
11579
11580static void
3e43a32a
MS
11581dwarf2_start_subfile (char *filename, const char *dirname,
11582 const char *comp_dir)
c906108c 11583{
4f1520fb
FR
11584 char *fullname;
11585
11586 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11587 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11588 second argument to start_subfile. To be consistent, we do the
11589 same here. In order not to lose the line information directory,
11590 we concatenate it to the filename when it makes sense.
11591 Note that the Dwarf3 standard says (speaking of filenames in line
11592 information): ``The directory index is ignored for file names
11593 that represent full path names''. Thus ignoring dirname in the
11594 `else' branch below isn't an issue. */
c906108c 11595
d5166ae1 11596 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11597 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11598 else
11599 fullname = filename;
c906108c 11600
4f1520fb
FR
11601 start_subfile (fullname, comp_dir);
11602
11603 if (fullname != filename)
11604 xfree (fullname);
c906108c
SS
11605}
11606
4c2df51b
DJ
11607static void
11608var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11609 struct dwarf2_cu *cu)
4c2df51b 11610{
e7c27a73
DJ
11611 struct objfile *objfile = cu->objfile;
11612 struct comp_unit_head *cu_header = &cu->header;
11613
4c2df51b
DJ
11614 /* NOTE drow/2003-01-30: There used to be a comment and some special
11615 code here to turn a symbol with DW_AT_external and a
11616 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11617 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11618 with some versions of binutils) where shared libraries could have
11619 relocations against symbols in their debug information - the
11620 minimal symbol would have the right address, but the debug info
11621 would not. It's no longer necessary, because we will explicitly
11622 apply relocations when we read in the debug information now. */
11623
11624 /* A DW_AT_location attribute with no contents indicates that a
11625 variable has been optimized away. */
11626 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11627 {
11628 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11629 return;
11630 }
11631
11632 /* Handle one degenerate form of location expression specially, to
11633 preserve GDB's previous behavior when section offsets are
11634 specified. If this is just a DW_OP_addr then mark this symbol
11635 as LOC_STATIC. */
11636
11637 if (attr_form_is_block (attr)
11638 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11639 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11640 {
891d2f0b 11641 unsigned int dummy;
4c2df51b
DJ
11642
11643 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11644 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11645 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11646 fixup_symbol_section (sym, objfile);
11647 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11648 SYMBOL_SECTION (sym));
4c2df51b
DJ
11649 return;
11650 }
11651
11652 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11653 expression evaluator, and use LOC_COMPUTED only when necessary
11654 (i.e. when the value of a register or memory location is
11655 referenced, or a thread-local block, etc.). Then again, it might
11656 not be worthwhile. I'm assuming that it isn't unless performance
11657 or memory numbers show me otherwise. */
11658
e7c27a73 11659 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11660 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11661
11662 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11663 cu->has_loclist = 1;
4c2df51b
DJ
11664}
11665
c906108c
SS
11666/* Given a pointer to a DWARF information entry, figure out if we need
11667 to make a symbol table entry for it, and if so, create a new entry
11668 and return a pointer to it.
11669 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11670 used the passed type.
11671 If SPACE is not NULL, use it to hold the new symbol. If it is
11672 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11673
11674static struct symbol *
34eaf542
TT
11675new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11676 struct symbol *space)
c906108c 11677{
e7c27a73 11678 struct objfile *objfile = cu->objfile;
c906108c
SS
11679 struct symbol *sym = NULL;
11680 char *name;
11681 struct attribute *attr = NULL;
11682 struct attribute *attr2 = NULL;
e142c38c 11683 CORE_ADDR baseaddr;
e37fd15a
SW
11684 struct pending **list_to_add = NULL;
11685
edb3359d 11686 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11687
11688 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11689
94af9270 11690 name = dwarf2_name (die, cu);
c906108c
SS
11691 if (name)
11692 {
94af9270 11693 const char *linkagename;
34eaf542 11694 int suppress_add = 0;
94af9270 11695
34eaf542
TT
11696 if (space)
11697 sym = space;
11698 else
11699 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11700 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11701
11702 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11703 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11704 linkagename = dwarf2_physname (name, die, cu);
11705 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11706
f55ee35c
JK
11707 /* Fortran does not have mangling standard and the mangling does differ
11708 between gfortran, iFort etc. */
11709 if (cu->language == language_fortran
b250c185 11710 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11711 symbol_set_demangled_name (&(sym->ginfo),
11712 (char *) dwarf2_full_name (name, die, cu),
11713 NULL);
f55ee35c 11714
c906108c 11715 /* Default assumptions.
c5aa993b 11716 Use the passed type or decode it from the die. */
176620f1 11717 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11718 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11719 if (type != NULL)
11720 SYMBOL_TYPE (sym) = type;
11721 else
e7c27a73 11722 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11723 attr = dwarf2_attr (die,
11724 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11725 cu);
c906108c
SS
11726 if (attr)
11727 {
11728 SYMBOL_LINE (sym) = DW_UNSND (attr);
11729 }
cb1df416 11730
edb3359d
DJ
11731 attr = dwarf2_attr (die,
11732 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11733 cu);
cb1df416
DJ
11734 if (attr)
11735 {
11736 int file_index = DW_UNSND (attr);
9a619af0 11737
cb1df416
DJ
11738 if (cu->line_header == NULL
11739 || file_index > cu->line_header->num_file_names)
11740 complaint (&symfile_complaints,
11741 _("file index out of range"));
1c3d648d 11742 else if (file_index > 0)
cb1df416
DJ
11743 {
11744 struct file_entry *fe;
9a619af0 11745
cb1df416
DJ
11746 fe = &cu->line_header->file_names[file_index - 1];
11747 SYMBOL_SYMTAB (sym) = fe->symtab;
11748 }
11749 }
11750
c906108c
SS
11751 switch (die->tag)
11752 {
11753 case DW_TAG_label:
e142c38c 11754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11755 if (attr)
11756 {
11757 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11758 }
0f5238ed
TT
11759 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11760 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11761 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11762 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11763 break;
11764 case DW_TAG_subprogram:
11765 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11766 finish_block. */
11767 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11768 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11769 if ((attr2 && (DW_UNSND (attr2) != 0))
11770 || cu->language == language_ada)
c906108c 11771 {
2cfa0c8d
JB
11772 /* Subprograms marked external are stored as a global symbol.
11773 Ada subprograms, whether marked external or not, are always
11774 stored as a global symbol, because we want to be able to
11775 access them globally. For instance, we want to be able
11776 to break on a nested subprogram without having to
11777 specify the context. */
e37fd15a 11778 list_to_add = &global_symbols;
c906108c
SS
11779 }
11780 else
11781 {
e37fd15a 11782 list_to_add = cu->list_in_scope;
c906108c
SS
11783 }
11784 break;
edb3359d
DJ
11785 case DW_TAG_inlined_subroutine:
11786 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11787 finish_block. */
11788 SYMBOL_CLASS (sym) = LOC_BLOCK;
11789 SYMBOL_INLINED (sym) = 1;
481860b3 11790 list_to_add = cu->list_in_scope;
edb3359d 11791 break;
34eaf542
TT
11792 case DW_TAG_template_value_param:
11793 suppress_add = 1;
11794 /* Fall through. */
72929c62 11795 case DW_TAG_constant:
c906108c 11796 case DW_TAG_variable:
254e6b9e 11797 case DW_TAG_member:
0963b4bd
MS
11798 /* Compilation with minimal debug info may result in
11799 variables with missing type entries. Change the
11800 misleading `void' type to something sensible. */
c906108c 11801 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11802 SYMBOL_TYPE (sym)
46bf5051 11803 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11804
e142c38c 11805 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11806 /* In the case of DW_TAG_member, we should only be called for
11807 static const members. */
11808 if (die->tag == DW_TAG_member)
11809 {
3863f96c
DE
11810 /* dwarf2_add_field uses die_is_declaration,
11811 so we do the same. */
254e6b9e
DE
11812 gdb_assert (die_is_declaration (die, cu));
11813 gdb_assert (attr);
11814 }
c906108c
SS
11815 if (attr)
11816 {
e7c27a73 11817 dwarf2_const_value (attr, sym, cu);
e142c38c 11818 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11819 if (!suppress_add)
34eaf542
TT
11820 {
11821 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11822 list_to_add = &global_symbols;
34eaf542 11823 else
e37fd15a 11824 list_to_add = cu->list_in_scope;
34eaf542 11825 }
c906108c
SS
11826 break;
11827 }
e142c38c 11828 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11829 if (attr)
11830 {
e7c27a73 11831 var_decode_location (attr, sym, cu);
e142c38c 11832 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11833 if (SYMBOL_CLASS (sym) == LOC_STATIC
11834 && SYMBOL_VALUE_ADDRESS (sym) == 0
11835 && !dwarf2_per_objfile->has_section_at_zero)
11836 {
11837 /* When a static variable is eliminated by the linker,
11838 the corresponding debug information is not stripped
11839 out, but the variable address is set to null;
11840 do not add such variables into symbol table. */
11841 }
11842 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11843 {
f55ee35c
JK
11844 /* Workaround gfortran PR debug/40040 - it uses
11845 DW_AT_location for variables in -fPIC libraries which may
11846 get overriden by other libraries/executable and get
11847 a different address. Resolve it by the minimal symbol
11848 which may come from inferior's executable using copy
11849 relocation. Make this workaround only for gfortran as for
11850 other compilers GDB cannot guess the minimal symbol
11851 Fortran mangling kind. */
11852 if (cu->language == language_fortran && die->parent
11853 && die->parent->tag == DW_TAG_module
11854 && cu->producer
11855 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11856 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11857
1c809c68
TT
11858 /* A variable with DW_AT_external is never static,
11859 but it may be block-scoped. */
11860 list_to_add = (cu->list_in_scope == &file_symbols
11861 ? &global_symbols : cu->list_in_scope);
1c809c68 11862 }
c906108c 11863 else
e37fd15a 11864 list_to_add = cu->list_in_scope;
c906108c
SS
11865 }
11866 else
11867 {
11868 /* We do not know the address of this symbol.
c5aa993b
JM
11869 If it is an external symbol and we have type information
11870 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11871 The address of the variable will then be determined from
11872 the minimal symbol table whenever the variable is
11873 referenced. */
e142c38c 11874 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11875 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11876 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11877 {
0fe7935b
DJ
11878 /* A variable with DW_AT_external is never static, but it
11879 may be block-scoped. */
11880 list_to_add = (cu->list_in_scope == &file_symbols
11881 ? &global_symbols : cu->list_in_scope);
11882
c906108c 11883 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11884 }
442ddf59
JK
11885 else if (!die_is_declaration (die, cu))
11886 {
11887 /* Use the default LOC_OPTIMIZED_OUT class. */
11888 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11889 if (!suppress_add)
11890 list_to_add = cu->list_in_scope;
442ddf59 11891 }
c906108c
SS
11892 }
11893 break;
11894 case DW_TAG_formal_parameter:
edb3359d
DJ
11895 /* If we are inside a function, mark this as an argument. If
11896 not, we might be looking at an argument to an inlined function
11897 when we do not have enough information to show inlined frames;
11898 pretend it's a local variable in that case so that the user can
11899 still see it. */
11900 if (context_stack_depth > 0
11901 && context_stack[context_stack_depth - 1].name != NULL)
11902 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11903 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11904 if (attr)
11905 {
e7c27a73 11906 var_decode_location (attr, sym, cu);
c906108c 11907 }
e142c38c 11908 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11909 if (attr)
11910 {
e7c27a73 11911 dwarf2_const_value (attr, sym, cu);
c906108c 11912 }
f346a30d 11913
e37fd15a 11914 list_to_add = cu->list_in_scope;
c906108c
SS
11915 break;
11916 case DW_TAG_unspecified_parameters:
11917 /* From varargs functions; gdb doesn't seem to have any
11918 interest in this information, so just ignore it for now.
11919 (FIXME?) */
11920 break;
34eaf542
TT
11921 case DW_TAG_template_type_param:
11922 suppress_add = 1;
11923 /* Fall through. */
c906108c 11924 case DW_TAG_class_type:
680b30c7 11925 case DW_TAG_interface_type:
c906108c
SS
11926 case DW_TAG_structure_type:
11927 case DW_TAG_union_type:
72019c9c 11928 case DW_TAG_set_type:
c906108c
SS
11929 case DW_TAG_enumeration_type:
11930 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11931 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11932
63d06c5c 11933 {
987504bb 11934 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11935 really ever be static objects: otherwise, if you try
11936 to, say, break of a class's method and you're in a file
11937 which doesn't mention that class, it won't work unless
11938 the check for all static symbols in lookup_symbol_aux
11939 saves you. See the OtherFileClass tests in
11940 gdb.c++/namespace.exp. */
11941
e37fd15a 11942 if (!suppress_add)
34eaf542 11943 {
34eaf542
TT
11944 list_to_add = (cu->list_in_scope == &file_symbols
11945 && (cu->language == language_cplus
11946 || cu->language == language_java)
11947 ? &global_symbols : cu->list_in_scope);
63d06c5c 11948
64382290
TT
11949 /* The semantics of C++ state that "struct foo {
11950 ... }" also defines a typedef for "foo". A Java
11951 class declaration also defines a typedef for the
11952 class. */
11953 if (cu->language == language_cplus
11954 || cu->language == language_java
11955 || cu->language == language_ada)
11956 {
11957 /* The symbol's name is already allocated along
11958 with this objfile, so we don't need to
11959 duplicate it for the type. */
11960 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11961 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11962 }
63d06c5c
DC
11963 }
11964 }
c906108c
SS
11965 break;
11966 case DW_TAG_typedef:
63d06c5c
DC
11967 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11968 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11969 list_to_add = cu->list_in_scope;
63d06c5c 11970 break;
c906108c 11971 case DW_TAG_base_type:
a02abb62 11972 case DW_TAG_subrange_type:
c906108c 11973 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11974 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11975 list_to_add = cu->list_in_scope;
c906108c
SS
11976 break;
11977 case DW_TAG_enumerator:
e142c38c 11978 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11979 if (attr)
11980 {
e7c27a73 11981 dwarf2_const_value (attr, sym, cu);
c906108c 11982 }
63d06c5c
DC
11983 {
11984 /* NOTE: carlton/2003-11-10: See comment above in the
11985 DW_TAG_class_type, etc. block. */
11986
e142c38c 11987 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11988 && (cu->language == language_cplus
11989 || cu->language == language_java)
e142c38c 11990 ? &global_symbols : cu->list_in_scope);
63d06c5c 11991 }
c906108c 11992 break;
5c4e30ca
DC
11993 case DW_TAG_namespace:
11994 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11995 list_to_add = &global_symbols;
5c4e30ca 11996 break;
c906108c
SS
11997 default:
11998 /* Not a tag we recognize. Hopefully we aren't processing
11999 trash data, but since we must specifically ignore things
12000 we don't recognize, there is nothing else we should do at
0963b4bd 12001 this point. */
e2e0b3e5 12002 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 12003 dwarf_tag_name (die->tag));
c906108c
SS
12004 break;
12005 }
df8a16a1 12006
e37fd15a
SW
12007 if (suppress_add)
12008 {
12009 sym->hash_next = objfile->template_symbols;
12010 objfile->template_symbols = sym;
12011 list_to_add = NULL;
12012 }
12013
12014 if (list_to_add != NULL)
12015 add_symbol_to_list (sym, list_to_add);
12016
df8a16a1
DJ
12017 /* For the benefit of old versions of GCC, check for anonymous
12018 namespaces based on the demangled name. */
12019 if (!processing_has_namespace_info
94af9270 12020 && cu->language == language_cplus)
a10964d1 12021 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12022 }
12023 return (sym);
12024}
12025
34eaf542
TT
12026/* A wrapper for new_symbol_full that always allocates a new symbol. */
12027
12028static struct symbol *
12029new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12030{
12031 return new_symbol_full (die, type, cu, NULL);
12032}
12033
98bfdba5
PA
12034/* Given an attr with a DW_FORM_dataN value in host byte order,
12035 zero-extend it as appropriate for the symbol's type. The DWARF
12036 standard (v4) is not entirely clear about the meaning of using
12037 DW_FORM_dataN for a constant with a signed type, where the type is
12038 wider than the data. The conclusion of a discussion on the DWARF
12039 list was that this is unspecified. We choose to always zero-extend
12040 because that is the interpretation long in use by GCC. */
c906108c 12041
98bfdba5
PA
12042static gdb_byte *
12043dwarf2_const_value_data (struct attribute *attr, struct type *type,
12044 const char *name, struct obstack *obstack,
12df843f 12045 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 12046{
e7c27a73 12047 struct objfile *objfile = cu->objfile;
e17a4113
UW
12048 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12049 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12050 LONGEST l = DW_UNSND (attr);
12051
12052 if (bits < sizeof (*value) * 8)
12053 {
12054 l &= ((LONGEST) 1 << bits) - 1;
12055 *value = l;
12056 }
12057 else if (bits == sizeof (*value) * 8)
12058 *value = l;
12059 else
12060 {
12061 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12062 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12063 return bytes;
12064 }
12065
12066 return NULL;
12067}
12068
12069/* Read a constant value from an attribute. Either set *VALUE, or if
12070 the value does not fit in *VALUE, set *BYTES - either already
12071 allocated on the objfile obstack, or newly allocated on OBSTACK,
12072 or, set *BATON, if we translated the constant to a location
12073 expression. */
12074
12075static void
12076dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12077 const char *name, struct obstack *obstack,
12078 struct dwarf2_cu *cu,
12df843f 12079 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
12080 struct dwarf2_locexpr_baton **baton)
12081{
12082 struct objfile *objfile = cu->objfile;
12083 struct comp_unit_head *cu_header = &cu->header;
c906108c 12084 struct dwarf_block *blk;
98bfdba5
PA
12085 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12086 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12087
12088 *value = 0;
12089 *bytes = NULL;
12090 *baton = NULL;
c906108c
SS
12091
12092 switch (attr->form)
12093 {
12094 case DW_FORM_addr:
ac56253d 12095 {
ac56253d
TT
12096 gdb_byte *data;
12097
98bfdba5
PA
12098 if (TYPE_LENGTH (type) != cu_header->addr_size)
12099 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12100 cu_header->addr_size,
98bfdba5 12101 TYPE_LENGTH (type));
ac56253d
TT
12102 /* Symbols of this form are reasonably rare, so we just
12103 piggyback on the existing location code rather than writing
12104 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12105 *baton = obstack_alloc (&objfile->objfile_obstack,
12106 sizeof (struct dwarf2_locexpr_baton));
12107 (*baton)->per_cu = cu->per_cu;
12108 gdb_assert ((*baton)->per_cu);
ac56253d 12109
98bfdba5
PA
12110 (*baton)->size = 2 + cu_header->addr_size;
12111 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12112 (*baton)->data = data;
ac56253d
TT
12113
12114 data[0] = DW_OP_addr;
12115 store_unsigned_integer (&data[1], cu_header->addr_size,
12116 byte_order, DW_ADDR (attr));
12117 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12118 }
c906108c 12119 break;
4ac36638 12120 case DW_FORM_string:
93b5768b 12121 case DW_FORM_strp:
98bfdba5
PA
12122 /* DW_STRING is already allocated on the objfile obstack, point
12123 directly to it. */
12124 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12125 break;
c906108c
SS
12126 case DW_FORM_block1:
12127 case DW_FORM_block2:
12128 case DW_FORM_block4:
12129 case DW_FORM_block:
2dc7f7b3 12130 case DW_FORM_exprloc:
c906108c 12131 blk = DW_BLOCK (attr);
98bfdba5
PA
12132 if (TYPE_LENGTH (type) != blk->size)
12133 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12134 TYPE_LENGTH (type));
12135 *bytes = blk->data;
c906108c 12136 break;
2df3850c
JM
12137
12138 /* The DW_AT_const_value attributes are supposed to carry the
12139 symbol's value "represented as it would be on the target
12140 architecture." By the time we get here, it's already been
12141 converted to host endianness, so we just need to sign- or
12142 zero-extend it as appropriate. */
12143 case DW_FORM_data1:
3e43a32a
MS
12144 *bytes = dwarf2_const_value_data (attr, type, name,
12145 obstack, cu, value, 8);
2df3850c 12146 break;
c906108c 12147 case DW_FORM_data2:
3e43a32a
MS
12148 *bytes = dwarf2_const_value_data (attr, type, name,
12149 obstack, cu, value, 16);
2df3850c 12150 break;
c906108c 12151 case DW_FORM_data4:
3e43a32a
MS
12152 *bytes = dwarf2_const_value_data (attr, type, name,
12153 obstack, cu, value, 32);
2df3850c 12154 break;
c906108c 12155 case DW_FORM_data8:
3e43a32a
MS
12156 *bytes = dwarf2_const_value_data (attr, type, name,
12157 obstack, cu, value, 64);
2df3850c
JM
12158 break;
12159
c906108c 12160 case DW_FORM_sdata:
98bfdba5 12161 *value = DW_SND (attr);
2df3850c
JM
12162 break;
12163
c906108c 12164 case DW_FORM_udata:
98bfdba5 12165 *value = DW_UNSND (attr);
c906108c 12166 break;
2df3850c 12167
c906108c 12168 default:
4d3c2250 12169 complaint (&symfile_complaints,
e2e0b3e5 12170 _("unsupported const value attribute form: '%s'"),
4d3c2250 12171 dwarf_form_name (attr->form));
98bfdba5 12172 *value = 0;
c906108c
SS
12173 break;
12174 }
12175}
12176
2df3850c 12177
98bfdba5
PA
12178/* Copy constant value from an attribute to a symbol. */
12179
2df3850c 12180static void
98bfdba5
PA
12181dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12182 struct dwarf2_cu *cu)
2df3850c 12183{
98bfdba5
PA
12184 struct objfile *objfile = cu->objfile;
12185 struct comp_unit_head *cu_header = &cu->header;
12df843f 12186 LONGEST value;
98bfdba5
PA
12187 gdb_byte *bytes;
12188 struct dwarf2_locexpr_baton *baton;
2df3850c 12189
98bfdba5
PA
12190 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12191 SYMBOL_PRINT_NAME (sym),
12192 &objfile->objfile_obstack, cu,
12193 &value, &bytes, &baton);
2df3850c 12194
98bfdba5
PA
12195 if (baton != NULL)
12196 {
12197 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12198 SYMBOL_LOCATION_BATON (sym) = baton;
12199 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12200 }
12201 else if (bytes != NULL)
12202 {
12203 SYMBOL_VALUE_BYTES (sym) = bytes;
12204 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12205 }
12206 else
12207 {
12208 SYMBOL_VALUE (sym) = value;
12209 SYMBOL_CLASS (sym) = LOC_CONST;
12210 }
2df3850c
JM
12211}
12212
c906108c
SS
12213/* Return the type of the die in question using its DW_AT_type attribute. */
12214
12215static struct type *
e7c27a73 12216die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12217{
c906108c 12218 struct attribute *type_attr;
c906108c 12219
e142c38c 12220 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12221 if (!type_attr)
12222 {
12223 /* A missing DW_AT_type represents a void type. */
46bf5051 12224 return objfile_type (cu->objfile)->builtin_void;
c906108c 12225 }
348e048f 12226
673bfd45 12227 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12228}
12229
b4ba55a1
JB
12230/* True iff CU's producer generates GNAT Ada auxiliary information
12231 that allows to find parallel types through that information instead
12232 of having to do expensive parallel lookups by type name. */
12233
12234static int
12235need_gnat_info (struct dwarf2_cu *cu)
12236{
12237 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12238 of GNAT produces this auxiliary information, without any indication
12239 that it is produced. Part of enhancing the FSF version of GNAT
12240 to produce that information will be to put in place an indicator
12241 that we can use in order to determine whether the descriptive type
12242 info is available or not. One suggestion that has been made is
12243 to use a new attribute, attached to the CU die. For now, assume
12244 that the descriptive type info is not available. */
12245 return 0;
12246}
12247
b4ba55a1
JB
12248/* Return the auxiliary type of the die in question using its
12249 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12250 attribute is not present. */
12251
12252static struct type *
12253die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12254{
b4ba55a1 12255 struct attribute *type_attr;
b4ba55a1
JB
12256
12257 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12258 if (!type_attr)
12259 return NULL;
12260
673bfd45 12261 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12262}
12263
12264/* If DIE has a descriptive_type attribute, then set the TYPE's
12265 descriptive type accordingly. */
12266
12267static void
12268set_descriptive_type (struct type *type, struct die_info *die,
12269 struct dwarf2_cu *cu)
12270{
12271 struct type *descriptive_type = die_descriptive_type (die, cu);
12272
12273 if (descriptive_type)
12274 {
12275 ALLOCATE_GNAT_AUX_TYPE (type);
12276 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12277 }
12278}
12279
c906108c
SS
12280/* Return the containing type of the die in question using its
12281 DW_AT_containing_type attribute. */
12282
12283static struct type *
e7c27a73 12284die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12285{
c906108c 12286 struct attribute *type_attr;
c906108c 12287
e142c38c 12288 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12289 if (!type_attr)
12290 error (_("Dwarf Error: Problem turning containing type into gdb type "
12291 "[in module %s]"), cu->objfile->name);
12292
673bfd45 12293 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12294}
12295
673bfd45
DE
12296/* Look up the type of DIE in CU using its type attribute ATTR.
12297 If there is no type substitute an error marker. */
12298
c906108c 12299static struct type *
673bfd45
DE
12300lookup_die_type (struct die_info *die, struct attribute *attr,
12301 struct dwarf2_cu *cu)
c906108c 12302{
bb5ed363 12303 struct objfile *objfile = cu->objfile;
f792889a
DJ
12304 struct type *this_type;
12305
673bfd45
DE
12306 /* First see if we have it cached. */
12307
12308 if (is_ref_attr (attr))
12309 {
b64f50a1 12310 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
12311
12312 this_type = get_die_type_at_offset (offset, cu->per_cu);
12313 }
55f1336d 12314 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12315 {
12316 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12317 struct dwarf2_cu *sig_cu;
b64f50a1 12318 sect_offset offset;
673bfd45
DE
12319
12320 /* sig_type will be NULL if the signatured type is missing from
12321 the debug info. */
12322 if (sig_type == NULL)
12323 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12324 "at 0x%x [in module %s]"),
b64f50a1 12325 die->offset.sect_off, objfile->name);
673bfd45 12326
b0df02fd 12327 gdb_assert (sig_type->per_cu.debug_types_section);
b64f50a1
JK
12328 offset.sect_off = (sig_type->per_cu.offset.sect_off
12329 + sig_type->type_offset.cu_off);
673bfd45
DE
12330 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12331 }
12332 else
12333 {
12334 dump_die_for_error (die);
12335 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12336 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12337 }
12338
12339 /* If not cached we need to read it in. */
12340
12341 if (this_type == NULL)
12342 {
12343 struct die_info *type_die;
12344 struct dwarf2_cu *type_cu = cu;
12345
12346 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12347 /* If the type is cached, we should have found it above. */
12348 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12349 this_type = read_type_die_1 (type_die, type_cu);
12350 }
12351
12352 /* If we still don't have a type use an error marker. */
12353
12354 if (this_type == NULL)
c906108c 12355 {
b00fdb78
TT
12356 char *message, *saved;
12357
12358 /* read_type_die already issued a complaint. */
12359 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12360 objfile->name,
b64f50a1
JK
12361 cu->header.offset.sect_off,
12362 die->offset.sect_off);
bb5ed363 12363 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12364 message, strlen (message));
12365 xfree (message);
12366
bb5ed363 12367 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12368 }
673bfd45 12369
f792889a 12370 return this_type;
c906108c
SS
12371}
12372
673bfd45
DE
12373/* Return the type in DIE, CU.
12374 Returns NULL for invalid types.
12375
12376 This first does a lookup in the appropriate type_hash table,
12377 and only reads the die in if necessary.
12378
12379 NOTE: This can be called when reading in partial or full symbols. */
12380
f792889a 12381static struct type *
e7c27a73 12382read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12383{
f792889a
DJ
12384 struct type *this_type;
12385
12386 this_type = get_die_type (die, cu);
12387 if (this_type)
12388 return this_type;
12389
673bfd45
DE
12390 return read_type_die_1 (die, cu);
12391}
12392
12393/* Read the type in DIE, CU.
12394 Returns NULL for invalid types. */
12395
12396static struct type *
12397read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12398{
12399 struct type *this_type = NULL;
12400
c906108c
SS
12401 switch (die->tag)
12402 {
12403 case DW_TAG_class_type:
680b30c7 12404 case DW_TAG_interface_type:
c906108c
SS
12405 case DW_TAG_structure_type:
12406 case DW_TAG_union_type:
f792889a 12407 this_type = read_structure_type (die, cu);
c906108c
SS
12408 break;
12409 case DW_TAG_enumeration_type:
f792889a 12410 this_type = read_enumeration_type (die, cu);
c906108c
SS
12411 break;
12412 case DW_TAG_subprogram:
12413 case DW_TAG_subroutine_type:
edb3359d 12414 case DW_TAG_inlined_subroutine:
f792889a 12415 this_type = read_subroutine_type (die, cu);
c906108c
SS
12416 break;
12417 case DW_TAG_array_type:
f792889a 12418 this_type = read_array_type (die, cu);
c906108c 12419 break;
72019c9c 12420 case DW_TAG_set_type:
f792889a 12421 this_type = read_set_type (die, cu);
72019c9c 12422 break;
c906108c 12423 case DW_TAG_pointer_type:
f792889a 12424 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12425 break;
12426 case DW_TAG_ptr_to_member_type:
f792889a 12427 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12428 break;
12429 case DW_TAG_reference_type:
f792889a 12430 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12431 break;
12432 case DW_TAG_const_type:
f792889a 12433 this_type = read_tag_const_type (die, cu);
c906108c
SS
12434 break;
12435 case DW_TAG_volatile_type:
f792889a 12436 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12437 break;
12438 case DW_TAG_string_type:
f792889a 12439 this_type = read_tag_string_type (die, cu);
c906108c
SS
12440 break;
12441 case DW_TAG_typedef:
f792889a 12442 this_type = read_typedef (die, cu);
c906108c 12443 break;
a02abb62 12444 case DW_TAG_subrange_type:
f792889a 12445 this_type = read_subrange_type (die, cu);
a02abb62 12446 break;
c906108c 12447 case DW_TAG_base_type:
f792889a 12448 this_type = read_base_type (die, cu);
c906108c 12449 break;
81a17f79 12450 case DW_TAG_unspecified_type:
f792889a 12451 this_type = read_unspecified_type (die, cu);
81a17f79 12452 break;
0114d602
DJ
12453 case DW_TAG_namespace:
12454 this_type = read_namespace_type (die, cu);
12455 break;
f55ee35c
JK
12456 case DW_TAG_module:
12457 this_type = read_module_type (die, cu);
12458 break;
c906108c 12459 default:
3e43a32a
MS
12460 complaint (&symfile_complaints,
12461 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12462 dwarf_tag_name (die->tag));
c906108c
SS
12463 break;
12464 }
63d06c5c 12465
f792889a 12466 return this_type;
63d06c5c
DC
12467}
12468
abc72ce4
DE
12469/* See if we can figure out if the class lives in a namespace. We do
12470 this by looking for a member function; its demangled name will
12471 contain namespace info, if there is any.
12472 Return the computed name or NULL.
12473 Space for the result is allocated on the objfile's obstack.
12474 This is the full-die version of guess_partial_die_structure_name.
12475 In this case we know DIE has no useful parent. */
12476
12477static char *
12478guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12479{
12480 struct die_info *spec_die;
12481 struct dwarf2_cu *spec_cu;
12482 struct die_info *child;
12483
12484 spec_cu = cu;
12485 spec_die = die_specification (die, &spec_cu);
12486 if (spec_die != NULL)
12487 {
12488 die = spec_die;
12489 cu = spec_cu;
12490 }
12491
12492 for (child = die->child;
12493 child != NULL;
12494 child = child->sibling)
12495 {
12496 if (child->tag == DW_TAG_subprogram)
12497 {
12498 struct attribute *attr;
12499
12500 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12501 if (attr == NULL)
12502 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12503 if (attr != NULL)
12504 {
12505 char *actual_name
12506 = language_class_name_from_physname (cu->language_defn,
12507 DW_STRING (attr));
12508 char *name = NULL;
12509
12510 if (actual_name != NULL)
12511 {
12512 char *die_name = dwarf2_name (die, cu);
12513
12514 if (die_name != NULL
12515 && strcmp (die_name, actual_name) != 0)
12516 {
12517 /* Strip off the class name from the full name.
12518 We want the prefix. */
12519 int die_name_len = strlen (die_name);
12520 int actual_name_len = strlen (actual_name);
12521
12522 /* Test for '::' as a sanity check. */
12523 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12524 && actual_name[actual_name_len
12525 - die_name_len - 1] == ':')
abc72ce4
DE
12526 name =
12527 obsavestring (actual_name,
12528 actual_name_len - die_name_len - 2,
12529 &cu->objfile->objfile_obstack);
12530 }
12531 }
12532 xfree (actual_name);
12533 return name;
12534 }
12535 }
12536 }
12537
12538 return NULL;
12539}
12540
96408a79
SA
12541/* GCC might emit a nameless typedef that has a linkage name. Determine the
12542 prefix part in such case. See
12543 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12544
12545static char *
12546anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12547{
12548 struct attribute *attr;
12549 char *base;
12550
12551 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12552 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12553 return NULL;
12554
12555 attr = dwarf2_attr (die, DW_AT_name, cu);
12556 if (attr != NULL && DW_STRING (attr) != NULL)
12557 return NULL;
12558
12559 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12560 if (attr == NULL)
12561 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12562 if (attr == NULL || DW_STRING (attr) == NULL)
12563 return NULL;
12564
12565 /* dwarf2_name had to be already called. */
12566 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12567
12568 /* Strip the base name, keep any leading namespaces/classes. */
12569 base = strrchr (DW_STRING (attr), ':');
12570 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12571 return "";
12572
12573 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12574 &cu->objfile->objfile_obstack);
12575}
12576
fdde2d81 12577/* Return the name of the namespace/class that DIE is defined within,
0114d602 12578 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12579
0114d602
DJ
12580 For example, if we're within the method foo() in the following
12581 code:
12582
12583 namespace N {
12584 class C {
12585 void foo () {
12586 }
12587 };
12588 }
12589
12590 then determine_prefix on foo's die will return "N::C". */
fdde2d81 12591
0d5cff50 12592static const char *
e142c38c 12593determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12594{
0114d602
DJ
12595 struct die_info *parent, *spec_die;
12596 struct dwarf2_cu *spec_cu;
12597 struct type *parent_type;
96408a79 12598 char *retval;
63d06c5c 12599
f55ee35c
JK
12600 if (cu->language != language_cplus && cu->language != language_java
12601 && cu->language != language_fortran)
0114d602
DJ
12602 return "";
12603
96408a79
SA
12604 retval = anonymous_struct_prefix (die, cu);
12605 if (retval)
12606 return retval;
12607
0114d602
DJ
12608 /* We have to be careful in the presence of DW_AT_specification.
12609 For example, with GCC 3.4, given the code
12610
12611 namespace N {
12612 void foo() {
12613 // Definition of N::foo.
12614 }
12615 }
12616
12617 then we'll have a tree of DIEs like this:
12618
12619 1: DW_TAG_compile_unit
12620 2: DW_TAG_namespace // N
12621 3: DW_TAG_subprogram // declaration of N::foo
12622 4: DW_TAG_subprogram // definition of N::foo
12623 DW_AT_specification // refers to die #3
12624
12625 Thus, when processing die #4, we have to pretend that we're in
12626 the context of its DW_AT_specification, namely the contex of die
12627 #3. */
12628 spec_cu = cu;
12629 spec_die = die_specification (die, &spec_cu);
12630 if (spec_die == NULL)
12631 parent = die->parent;
12632 else
63d06c5c 12633 {
0114d602
DJ
12634 parent = spec_die->parent;
12635 cu = spec_cu;
63d06c5c 12636 }
0114d602
DJ
12637
12638 if (parent == NULL)
12639 return "";
98bfdba5
PA
12640 else if (parent->building_fullname)
12641 {
12642 const char *name;
12643 const char *parent_name;
12644
12645 /* It has been seen on RealView 2.2 built binaries,
12646 DW_TAG_template_type_param types actually _defined_ as
12647 children of the parent class:
12648
12649 enum E {};
12650 template class <class Enum> Class{};
12651 Class<enum E> class_e;
12652
12653 1: DW_TAG_class_type (Class)
12654 2: DW_TAG_enumeration_type (E)
12655 3: DW_TAG_enumerator (enum1:0)
12656 3: DW_TAG_enumerator (enum2:1)
12657 ...
12658 2: DW_TAG_template_type_param
12659 DW_AT_type DW_FORM_ref_udata (E)
12660
12661 Besides being broken debug info, it can put GDB into an
12662 infinite loop. Consider:
12663
12664 When we're building the full name for Class<E>, we'll start
12665 at Class, and go look over its template type parameters,
12666 finding E. We'll then try to build the full name of E, and
12667 reach here. We're now trying to build the full name of E,
12668 and look over the parent DIE for containing scope. In the
12669 broken case, if we followed the parent DIE of E, we'd again
12670 find Class, and once again go look at its template type
12671 arguments, etc., etc. Simply don't consider such parent die
12672 as source-level parent of this die (it can't be, the language
12673 doesn't allow it), and break the loop here. */
12674 name = dwarf2_name (die, cu);
12675 parent_name = dwarf2_name (parent, cu);
12676 complaint (&symfile_complaints,
12677 _("template param type '%s' defined within parent '%s'"),
12678 name ? name : "<unknown>",
12679 parent_name ? parent_name : "<unknown>");
12680 return "";
12681 }
63d06c5c 12682 else
0114d602
DJ
12683 switch (parent->tag)
12684 {
63d06c5c 12685 case DW_TAG_namespace:
0114d602 12686 parent_type = read_type_die (parent, cu);
acebe513
UW
12687 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12688 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12689 Work around this problem here. */
12690 if (cu->language == language_cplus
12691 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12692 return "";
0114d602
DJ
12693 /* We give a name to even anonymous namespaces. */
12694 return TYPE_TAG_NAME (parent_type);
63d06c5c 12695 case DW_TAG_class_type:
680b30c7 12696 case DW_TAG_interface_type:
63d06c5c 12697 case DW_TAG_structure_type:
0114d602 12698 case DW_TAG_union_type:
f55ee35c 12699 case DW_TAG_module:
0114d602
DJ
12700 parent_type = read_type_die (parent, cu);
12701 if (TYPE_TAG_NAME (parent_type) != NULL)
12702 return TYPE_TAG_NAME (parent_type);
12703 else
12704 /* An anonymous structure is only allowed non-static data
12705 members; no typedefs, no member functions, et cetera.
12706 So it does not need a prefix. */
12707 return "";
abc72ce4
DE
12708 case DW_TAG_compile_unit:
12709 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12710 if (cu->language == language_cplus
8b70b953 12711 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12712 && die->child != NULL
12713 && (die->tag == DW_TAG_class_type
12714 || die->tag == DW_TAG_structure_type
12715 || die->tag == DW_TAG_union_type))
12716 {
12717 char *name = guess_full_die_structure_name (die, cu);
12718 if (name != NULL)
12719 return name;
12720 }
12721 return "";
63d06c5c 12722 default:
8176b9b8 12723 return determine_prefix (parent, cu);
63d06c5c 12724 }
63d06c5c
DC
12725}
12726
3e43a32a
MS
12727/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12728 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12729 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12730 an obconcat, otherwise allocate storage for the result. The CU argument is
12731 used to determine the language and hence, the appropriate separator. */
987504bb 12732
f55ee35c 12733#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12734
12735static char *
f55ee35c
JK
12736typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12737 int physname, struct dwarf2_cu *cu)
63d06c5c 12738{
f55ee35c 12739 const char *lead = "";
5c315b68 12740 const char *sep;
63d06c5c 12741
3e43a32a
MS
12742 if (suffix == NULL || suffix[0] == '\0'
12743 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12744 sep = "";
12745 else if (cu->language == language_java)
12746 sep = ".";
f55ee35c
JK
12747 else if (cu->language == language_fortran && physname)
12748 {
12749 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12750 DW_AT_MIPS_linkage_name is preferred and used instead. */
12751
12752 lead = "__";
12753 sep = "_MOD_";
12754 }
987504bb
JJ
12755 else
12756 sep = "::";
63d06c5c 12757
6dd47d34
DE
12758 if (prefix == NULL)
12759 prefix = "";
12760 if (suffix == NULL)
12761 suffix = "";
12762
987504bb
JJ
12763 if (obs == NULL)
12764 {
3e43a32a
MS
12765 char *retval
12766 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12767
f55ee35c
JK
12768 strcpy (retval, lead);
12769 strcat (retval, prefix);
6dd47d34
DE
12770 strcat (retval, sep);
12771 strcat (retval, suffix);
63d06c5c
DC
12772 return retval;
12773 }
987504bb
JJ
12774 else
12775 {
12776 /* We have an obstack. */
f55ee35c 12777 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12778 }
63d06c5c
DC
12779}
12780
c906108c
SS
12781/* Return sibling of die, NULL if no sibling. */
12782
f9aca02d 12783static struct die_info *
fba45db2 12784sibling_die (struct die_info *die)
c906108c 12785{
639d11d3 12786 return die->sibling;
c906108c
SS
12787}
12788
71c25dea
TT
12789/* Get name of a die, return NULL if not found. */
12790
12791static char *
12792dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12793 struct obstack *obstack)
12794{
12795 if (name && cu->language == language_cplus)
12796 {
12797 char *canon_name = cp_canonicalize_string (name);
12798
12799 if (canon_name != NULL)
12800 {
12801 if (strcmp (canon_name, name) != 0)
12802 name = obsavestring (canon_name, strlen (canon_name),
12803 obstack);
12804 xfree (canon_name);
12805 }
12806 }
12807
12808 return name;
c906108c
SS
12809}
12810
9219021c
DC
12811/* Get name of a die, return NULL if not found. */
12812
12813static char *
e142c38c 12814dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12815{
12816 struct attribute *attr;
12817
e142c38c 12818 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12819 if ((!attr || !DW_STRING (attr))
12820 && die->tag != DW_TAG_class_type
12821 && die->tag != DW_TAG_interface_type
12822 && die->tag != DW_TAG_structure_type
12823 && die->tag != DW_TAG_union_type)
71c25dea
TT
12824 return NULL;
12825
12826 switch (die->tag)
12827 {
12828 case DW_TAG_compile_unit:
12829 /* Compilation units have a DW_AT_name that is a filename, not
12830 a source language identifier. */
12831 case DW_TAG_enumeration_type:
12832 case DW_TAG_enumerator:
12833 /* These tags always have simple identifiers already; no need
12834 to canonicalize them. */
12835 return DW_STRING (attr);
907af001 12836
418835cc
KS
12837 case DW_TAG_subprogram:
12838 /* Java constructors will all be named "<init>", so return
12839 the class name when we see this special case. */
12840 if (cu->language == language_java
12841 && DW_STRING (attr) != NULL
12842 && strcmp (DW_STRING (attr), "<init>") == 0)
12843 {
12844 struct dwarf2_cu *spec_cu = cu;
12845 struct die_info *spec_die;
12846
12847 /* GCJ will output '<init>' for Java constructor names.
12848 For this special case, return the name of the parent class. */
12849
12850 /* GCJ may output suprogram DIEs with AT_specification set.
12851 If so, use the name of the specified DIE. */
12852 spec_die = die_specification (die, &spec_cu);
12853 if (spec_die != NULL)
12854 return dwarf2_name (spec_die, spec_cu);
12855
12856 do
12857 {
12858 die = die->parent;
12859 if (die->tag == DW_TAG_class_type)
12860 return dwarf2_name (die, cu);
12861 }
12862 while (die->tag != DW_TAG_compile_unit);
12863 }
907af001
UW
12864 break;
12865
12866 case DW_TAG_class_type:
12867 case DW_TAG_interface_type:
12868 case DW_TAG_structure_type:
12869 case DW_TAG_union_type:
12870 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12871 structures or unions. These were of the form "._%d" in GCC 4.1,
12872 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12873 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12874 if (attr && DW_STRING (attr)
12875 && (strncmp (DW_STRING (attr), "._", 2) == 0
12876 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12877 return NULL;
53832f31
TT
12878
12879 /* GCC might emit a nameless typedef that has a linkage name. See
12880 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12881 if (!attr || DW_STRING (attr) == NULL)
12882 {
df5c6c50 12883 char *demangled = NULL;
53832f31
TT
12884
12885 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12886 if (attr == NULL)
12887 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12888
12889 if (attr == NULL || DW_STRING (attr) == NULL)
12890 return NULL;
12891
df5c6c50
JK
12892 /* Avoid demangling DW_STRING (attr) the second time on a second
12893 call for the same DIE. */
12894 if (!DW_STRING_IS_CANONICAL (attr))
12895 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12896
12897 if (demangled)
12898 {
96408a79
SA
12899 char *base;
12900
53832f31 12901 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12902 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12903 &cu->objfile->objfile_obstack);
53832f31
TT
12904 DW_STRING_IS_CANONICAL (attr) = 1;
12905 xfree (demangled);
96408a79
SA
12906
12907 /* Strip any leading namespaces/classes, keep only the base name.
12908 DW_AT_name for named DIEs does not contain the prefixes. */
12909 base = strrchr (DW_STRING (attr), ':');
12910 if (base && base > DW_STRING (attr) && base[-1] == ':')
12911 return &base[1];
12912 else
12913 return DW_STRING (attr);
53832f31
TT
12914 }
12915 }
907af001
UW
12916 break;
12917
71c25dea 12918 default:
907af001
UW
12919 break;
12920 }
12921
12922 if (!DW_STRING_IS_CANONICAL (attr))
12923 {
12924 DW_STRING (attr)
12925 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12926 &cu->objfile->objfile_obstack);
12927 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12928 }
907af001 12929 return DW_STRING (attr);
9219021c
DC
12930}
12931
12932/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12933 is none. *EXT_CU is the CU containing DIE on input, and the CU
12934 containing the return value on output. */
9219021c
DC
12935
12936static struct die_info *
f2f0e013 12937dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12938{
12939 struct attribute *attr;
9219021c 12940
f2f0e013 12941 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12942 if (attr == NULL)
12943 return NULL;
12944
f2f0e013 12945 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12946}
12947
c906108c
SS
12948/* Convert a DIE tag into its string name. */
12949
12950static char *
aa1ee363 12951dwarf_tag_name (unsigned tag)
c906108c
SS
12952{
12953 switch (tag)
12954 {
12955 case DW_TAG_padding:
12956 return "DW_TAG_padding";
12957 case DW_TAG_array_type:
12958 return "DW_TAG_array_type";
12959 case DW_TAG_class_type:
12960 return "DW_TAG_class_type";
12961 case DW_TAG_entry_point:
12962 return "DW_TAG_entry_point";
12963 case DW_TAG_enumeration_type:
12964 return "DW_TAG_enumeration_type";
12965 case DW_TAG_formal_parameter:
12966 return "DW_TAG_formal_parameter";
12967 case DW_TAG_imported_declaration:
12968 return "DW_TAG_imported_declaration";
12969 case DW_TAG_label:
12970 return "DW_TAG_label";
12971 case DW_TAG_lexical_block:
12972 return "DW_TAG_lexical_block";
12973 case DW_TAG_member:
12974 return "DW_TAG_member";
12975 case DW_TAG_pointer_type:
12976 return "DW_TAG_pointer_type";
12977 case DW_TAG_reference_type:
12978 return "DW_TAG_reference_type";
12979 case DW_TAG_compile_unit:
12980 return "DW_TAG_compile_unit";
12981 case DW_TAG_string_type:
12982 return "DW_TAG_string_type";
12983 case DW_TAG_structure_type:
12984 return "DW_TAG_structure_type";
12985 case DW_TAG_subroutine_type:
12986 return "DW_TAG_subroutine_type";
12987 case DW_TAG_typedef:
12988 return "DW_TAG_typedef";
12989 case DW_TAG_union_type:
12990 return "DW_TAG_union_type";
12991 case DW_TAG_unspecified_parameters:
12992 return "DW_TAG_unspecified_parameters";
12993 case DW_TAG_variant:
12994 return "DW_TAG_variant";
12995 case DW_TAG_common_block:
12996 return "DW_TAG_common_block";
12997 case DW_TAG_common_inclusion:
12998 return "DW_TAG_common_inclusion";
12999 case DW_TAG_inheritance:
13000 return "DW_TAG_inheritance";
13001 case DW_TAG_inlined_subroutine:
13002 return "DW_TAG_inlined_subroutine";
13003 case DW_TAG_module:
13004 return "DW_TAG_module";
13005 case DW_TAG_ptr_to_member_type:
13006 return "DW_TAG_ptr_to_member_type";
13007 case DW_TAG_set_type:
13008 return "DW_TAG_set_type";
13009 case DW_TAG_subrange_type:
13010 return "DW_TAG_subrange_type";
13011 case DW_TAG_with_stmt:
13012 return "DW_TAG_with_stmt";
13013 case DW_TAG_access_declaration:
13014 return "DW_TAG_access_declaration";
13015 case DW_TAG_base_type:
13016 return "DW_TAG_base_type";
13017 case DW_TAG_catch_block:
13018 return "DW_TAG_catch_block";
13019 case DW_TAG_const_type:
13020 return "DW_TAG_const_type";
13021 case DW_TAG_constant:
13022 return "DW_TAG_constant";
13023 case DW_TAG_enumerator:
13024 return "DW_TAG_enumerator";
13025 case DW_TAG_file_type:
13026 return "DW_TAG_file_type";
13027 case DW_TAG_friend:
13028 return "DW_TAG_friend";
13029 case DW_TAG_namelist:
13030 return "DW_TAG_namelist";
13031 case DW_TAG_namelist_item:
13032 return "DW_TAG_namelist_item";
13033 case DW_TAG_packed_type:
13034 return "DW_TAG_packed_type";
13035 case DW_TAG_subprogram:
13036 return "DW_TAG_subprogram";
13037 case DW_TAG_template_type_param:
13038 return "DW_TAG_template_type_param";
13039 case DW_TAG_template_value_param:
13040 return "DW_TAG_template_value_param";
13041 case DW_TAG_thrown_type:
13042 return "DW_TAG_thrown_type";
13043 case DW_TAG_try_block:
13044 return "DW_TAG_try_block";
13045 case DW_TAG_variant_part:
13046 return "DW_TAG_variant_part";
13047 case DW_TAG_variable:
13048 return "DW_TAG_variable";
13049 case DW_TAG_volatile_type:
13050 return "DW_TAG_volatile_type";
d9fa45fe
DC
13051 case DW_TAG_dwarf_procedure:
13052 return "DW_TAG_dwarf_procedure";
13053 case DW_TAG_restrict_type:
13054 return "DW_TAG_restrict_type";
13055 case DW_TAG_interface_type:
13056 return "DW_TAG_interface_type";
13057 case DW_TAG_namespace:
13058 return "DW_TAG_namespace";
13059 case DW_TAG_imported_module:
13060 return "DW_TAG_imported_module";
13061 case DW_TAG_unspecified_type:
13062 return "DW_TAG_unspecified_type";
13063 case DW_TAG_partial_unit:
13064 return "DW_TAG_partial_unit";
13065 case DW_TAG_imported_unit:
13066 return "DW_TAG_imported_unit";
b7619582
GF
13067 case DW_TAG_condition:
13068 return "DW_TAG_condition";
13069 case DW_TAG_shared_type:
13070 return "DW_TAG_shared_type";
348e048f
DE
13071 case DW_TAG_type_unit:
13072 return "DW_TAG_type_unit";
c906108c
SS
13073 case DW_TAG_MIPS_loop:
13074 return "DW_TAG_MIPS_loop";
b7619582
GF
13075 case DW_TAG_HP_array_descriptor:
13076 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13077 case DW_TAG_format_label:
13078 return "DW_TAG_format_label";
13079 case DW_TAG_function_template:
13080 return "DW_TAG_function_template";
13081 case DW_TAG_class_template:
13082 return "DW_TAG_class_template";
b7619582
GF
13083 case DW_TAG_GNU_BINCL:
13084 return "DW_TAG_GNU_BINCL";
13085 case DW_TAG_GNU_EINCL:
13086 return "DW_TAG_GNU_EINCL";
13087 case DW_TAG_upc_shared_type:
13088 return "DW_TAG_upc_shared_type";
13089 case DW_TAG_upc_strict_type:
13090 return "DW_TAG_upc_strict_type";
13091 case DW_TAG_upc_relaxed_type:
13092 return "DW_TAG_upc_relaxed_type";
13093 case DW_TAG_PGI_kanji_type:
13094 return "DW_TAG_PGI_kanji_type";
13095 case DW_TAG_PGI_interface_block:
13096 return "DW_TAG_PGI_interface_block";
96408a79
SA
13097 case DW_TAG_GNU_call_site:
13098 return "DW_TAG_GNU_call_site";
c906108c
SS
13099 default:
13100 return "DW_TAG_<unknown>";
13101 }
13102}
13103
13104/* Convert a DWARF attribute code into its string name. */
13105
13106static char *
aa1ee363 13107dwarf_attr_name (unsigned attr)
c906108c
SS
13108{
13109 switch (attr)
13110 {
13111 case DW_AT_sibling:
13112 return "DW_AT_sibling";
13113 case DW_AT_location:
13114 return "DW_AT_location";
13115 case DW_AT_name:
13116 return "DW_AT_name";
13117 case DW_AT_ordering:
13118 return "DW_AT_ordering";
13119 case DW_AT_subscr_data:
13120 return "DW_AT_subscr_data";
13121 case DW_AT_byte_size:
13122 return "DW_AT_byte_size";
13123 case DW_AT_bit_offset:
13124 return "DW_AT_bit_offset";
13125 case DW_AT_bit_size:
13126 return "DW_AT_bit_size";
13127 case DW_AT_element_list:
13128 return "DW_AT_element_list";
13129 case DW_AT_stmt_list:
13130 return "DW_AT_stmt_list";
13131 case DW_AT_low_pc:
13132 return "DW_AT_low_pc";
13133 case DW_AT_high_pc:
13134 return "DW_AT_high_pc";
13135 case DW_AT_language:
13136 return "DW_AT_language";
13137 case DW_AT_member:
13138 return "DW_AT_member";
13139 case DW_AT_discr:
13140 return "DW_AT_discr";
13141 case DW_AT_discr_value:
13142 return "DW_AT_discr_value";
13143 case DW_AT_visibility:
13144 return "DW_AT_visibility";
13145 case DW_AT_import:
13146 return "DW_AT_import";
13147 case DW_AT_string_length:
13148 return "DW_AT_string_length";
13149 case DW_AT_common_reference:
13150 return "DW_AT_common_reference";
13151 case DW_AT_comp_dir:
13152 return "DW_AT_comp_dir";
13153 case DW_AT_const_value:
13154 return "DW_AT_const_value";
13155 case DW_AT_containing_type:
13156 return "DW_AT_containing_type";
13157 case DW_AT_default_value:
13158 return "DW_AT_default_value";
13159 case DW_AT_inline:
13160 return "DW_AT_inline";
13161 case DW_AT_is_optional:
13162 return "DW_AT_is_optional";
13163 case DW_AT_lower_bound:
13164 return "DW_AT_lower_bound";
13165 case DW_AT_producer:
13166 return "DW_AT_producer";
13167 case DW_AT_prototyped:
13168 return "DW_AT_prototyped";
13169 case DW_AT_return_addr:
13170 return "DW_AT_return_addr";
13171 case DW_AT_start_scope:
13172 return "DW_AT_start_scope";
09fa0d7c
JK
13173 case DW_AT_bit_stride:
13174 return "DW_AT_bit_stride";
c906108c
SS
13175 case DW_AT_upper_bound:
13176 return "DW_AT_upper_bound";
13177 case DW_AT_abstract_origin:
13178 return "DW_AT_abstract_origin";
13179 case DW_AT_accessibility:
13180 return "DW_AT_accessibility";
13181 case DW_AT_address_class:
13182 return "DW_AT_address_class";
13183 case DW_AT_artificial:
13184 return "DW_AT_artificial";
13185 case DW_AT_base_types:
13186 return "DW_AT_base_types";
13187 case DW_AT_calling_convention:
13188 return "DW_AT_calling_convention";
13189 case DW_AT_count:
13190 return "DW_AT_count";
13191 case DW_AT_data_member_location:
13192 return "DW_AT_data_member_location";
13193 case DW_AT_decl_column:
13194 return "DW_AT_decl_column";
13195 case DW_AT_decl_file:
13196 return "DW_AT_decl_file";
13197 case DW_AT_decl_line:
13198 return "DW_AT_decl_line";
13199 case DW_AT_declaration:
13200 return "DW_AT_declaration";
13201 case DW_AT_discr_list:
13202 return "DW_AT_discr_list";
13203 case DW_AT_encoding:
13204 return "DW_AT_encoding";
13205 case DW_AT_external:
13206 return "DW_AT_external";
13207 case DW_AT_frame_base:
13208 return "DW_AT_frame_base";
13209 case DW_AT_friend:
13210 return "DW_AT_friend";
13211 case DW_AT_identifier_case:
13212 return "DW_AT_identifier_case";
13213 case DW_AT_macro_info:
13214 return "DW_AT_macro_info";
13215 case DW_AT_namelist_items:
13216 return "DW_AT_namelist_items";
13217 case DW_AT_priority:
13218 return "DW_AT_priority";
13219 case DW_AT_segment:
13220 return "DW_AT_segment";
13221 case DW_AT_specification:
13222 return "DW_AT_specification";
13223 case DW_AT_static_link:
13224 return "DW_AT_static_link";
13225 case DW_AT_type:
13226 return "DW_AT_type";
13227 case DW_AT_use_location:
13228 return "DW_AT_use_location";
13229 case DW_AT_variable_parameter:
13230 return "DW_AT_variable_parameter";
13231 case DW_AT_virtuality:
13232 return "DW_AT_virtuality";
13233 case DW_AT_vtable_elem_location:
13234 return "DW_AT_vtable_elem_location";
b7619582 13235 /* DWARF 3 values. */
d9fa45fe
DC
13236 case DW_AT_allocated:
13237 return "DW_AT_allocated";
13238 case DW_AT_associated:
13239 return "DW_AT_associated";
13240 case DW_AT_data_location:
13241 return "DW_AT_data_location";
09fa0d7c
JK
13242 case DW_AT_byte_stride:
13243 return "DW_AT_byte_stride";
d9fa45fe
DC
13244 case DW_AT_entry_pc:
13245 return "DW_AT_entry_pc";
13246 case DW_AT_use_UTF8:
13247 return "DW_AT_use_UTF8";
13248 case DW_AT_extension:
13249 return "DW_AT_extension";
13250 case DW_AT_ranges:
13251 return "DW_AT_ranges";
13252 case DW_AT_trampoline:
13253 return "DW_AT_trampoline";
13254 case DW_AT_call_column:
13255 return "DW_AT_call_column";
13256 case DW_AT_call_file:
13257 return "DW_AT_call_file";
13258 case DW_AT_call_line:
13259 return "DW_AT_call_line";
b7619582
GF
13260 case DW_AT_description:
13261 return "DW_AT_description";
13262 case DW_AT_binary_scale:
13263 return "DW_AT_binary_scale";
13264 case DW_AT_decimal_scale:
13265 return "DW_AT_decimal_scale";
13266 case DW_AT_small:
13267 return "DW_AT_small";
13268 case DW_AT_decimal_sign:
13269 return "DW_AT_decimal_sign";
13270 case DW_AT_digit_count:
13271 return "DW_AT_digit_count";
13272 case DW_AT_picture_string:
13273 return "DW_AT_picture_string";
13274 case DW_AT_mutable:
13275 return "DW_AT_mutable";
13276 case DW_AT_threads_scaled:
13277 return "DW_AT_threads_scaled";
13278 case DW_AT_explicit:
13279 return "DW_AT_explicit";
13280 case DW_AT_object_pointer:
13281 return "DW_AT_object_pointer";
13282 case DW_AT_endianity:
13283 return "DW_AT_endianity";
13284 case DW_AT_elemental:
13285 return "DW_AT_elemental";
13286 case DW_AT_pure:
13287 return "DW_AT_pure";
13288 case DW_AT_recursive:
13289 return "DW_AT_recursive";
348e048f
DE
13290 /* DWARF 4 values. */
13291 case DW_AT_signature:
13292 return "DW_AT_signature";
31ef98ae
TT
13293 case DW_AT_linkage_name:
13294 return "DW_AT_linkage_name";
b7619582 13295 /* SGI/MIPS extensions. */
c764a876 13296#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13297 case DW_AT_MIPS_fde:
13298 return "DW_AT_MIPS_fde";
c764a876 13299#endif
c906108c
SS
13300 case DW_AT_MIPS_loop_begin:
13301 return "DW_AT_MIPS_loop_begin";
13302 case DW_AT_MIPS_tail_loop_begin:
13303 return "DW_AT_MIPS_tail_loop_begin";
13304 case DW_AT_MIPS_epilog_begin:
13305 return "DW_AT_MIPS_epilog_begin";
13306 case DW_AT_MIPS_loop_unroll_factor:
13307 return "DW_AT_MIPS_loop_unroll_factor";
13308 case DW_AT_MIPS_software_pipeline_depth:
13309 return "DW_AT_MIPS_software_pipeline_depth";
13310 case DW_AT_MIPS_linkage_name:
13311 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13312 case DW_AT_MIPS_stride:
13313 return "DW_AT_MIPS_stride";
13314 case DW_AT_MIPS_abstract_name:
13315 return "DW_AT_MIPS_abstract_name";
13316 case DW_AT_MIPS_clone_origin:
13317 return "DW_AT_MIPS_clone_origin";
13318 case DW_AT_MIPS_has_inlines:
13319 return "DW_AT_MIPS_has_inlines";
b7619582 13320 /* HP extensions. */
c764a876 13321#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13322 case DW_AT_HP_block_index:
13323 return "DW_AT_HP_block_index";
c764a876 13324#endif
b7619582
GF
13325 case DW_AT_HP_unmodifiable:
13326 return "DW_AT_HP_unmodifiable";
13327 case DW_AT_HP_actuals_stmt_list:
13328 return "DW_AT_HP_actuals_stmt_list";
13329 case DW_AT_HP_proc_per_section:
13330 return "DW_AT_HP_proc_per_section";
13331 case DW_AT_HP_raw_data_ptr:
13332 return "DW_AT_HP_raw_data_ptr";
13333 case DW_AT_HP_pass_by_reference:
13334 return "DW_AT_HP_pass_by_reference";
13335 case DW_AT_HP_opt_level:
13336 return "DW_AT_HP_opt_level";
13337 case DW_AT_HP_prof_version_id:
13338 return "DW_AT_HP_prof_version_id";
13339 case DW_AT_HP_opt_flags:
13340 return "DW_AT_HP_opt_flags";
13341 case DW_AT_HP_cold_region_low_pc:
13342 return "DW_AT_HP_cold_region_low_pc";
13343 case DW_AT_HP_cold_region_high_pc:
13344 return "DW_AT_HP_cold_region_high_pc";
13345 case DW_AT_HP_all_variables_modifiable:
13346 return "DW_AT_HP_all_variables_modifiable";
13347 case DW_AT_HP_linkage_name:
13348 return "DW_AT_HP_linkage_name";
13349 case DW_AT_HP_prof_flags:
13350 return "DW_AT_HP_prof_flags";
13351 /* GNU extensions. */
c906108c
SS
13352 case DW_AT_sf_names:
13353 return "DW_AT_sf_names";
13354 case DW_AT_src_info:
13355 return "DW_AT_src_info";
13356 case DW_AT_mac_info:
13357 return "DW_AT_mac_info";
13358 case DW_AT_src_coords:
13359 return "DW_AT_src_coords";
13360 case DW_AT_body_begin:
13361 return "DW_AT_body_begin";
13362 case DW_AT_body_end:
13363 return "DW_AT_body_end";
f5f8a009
EZ
13364 case DW_AT_GNU_vector:
13365 return "DW_AT_GNU_vector";
2de00c64
DE
13366 case DW_AT_GNU_odr_signature:
13367 return "DW_AT_GNU_odr_signature";
b7619582
GF
13368 /* VMS extensions. */
13369 case DW_AT_VMS_rtnbeg_pd_address:
13370 return "DW_AT_VMS_rtnbeg_pd_address";
13371 /* UPC extension. */
13372 case DW_AT_upc_threads_scaled:
13373 return "DW_AT_upc_threads_scaled";
13374 /* PGI (STMicroelectronics) extensions. */
13375 case DW_AT_PGI_lbase:
13376 return "DW_AT_PGI_lbase";
13377 case DW_AT_PGI_soffset:
13378 return "DW_AT_PGI_soffset";
13379 case DW_AT_PGI_lstride:
13380 return "DW_AT_PGI_lstride";
c906108c
SS
13381 default:
13382 return "DW_AT_<unknown>";
13383 }
13384}
13385
13386/* Convert a DWARF value form code into its string name. */
13387
13388static char *
aa1ee363 13389dwarf_form_name (unsigned form)
c906108c
SS
13390{
13391 switch (form)
13392 {
13393 case DW_FORM_addr:
13394 return "DW_FORM_addr";
13395 case DW_FORM_block2:
13396 return "DW_FORM_block2";
13397 case DW_FORM_block4:
13398 return "DW_FORM_block4";
13399 case DW_FORM_data2:
13400 return "DW_FORM_data2";
13401 case DW_FORM_data4:
13402 return "DW_FORM_data4";
13403 case DW_FORM_data8:
13404 return "DW_FORM_data8";
13405 case DW_FORM_string:
13406 return "DW_FORM_string";
13407 case DW_FORM_block:
13408 return "DW_FORM_block";
13409 case DW_FORM_block1:
13410 return "DW_FORM_block1";
13411 case DW_FORM_data1:
13412 return "DW_FORM_data1";
13413 case DW_FORM_flag:
13414 return "DW_FORM_flag";
13415 case DW_FORM_sdata:
13416 return "DW_FORM_sdata";
13417 case DW_FORM_strp:
13418 return "DW_FORM_strp";
13419 case DW_FORM_udata:
13420 return "DW_FORM_udata";
13421 case DW_FORM_ref_addr:
13422 return "DW_FORM_ref_addr";
13423 case DW_FORM_ref1:
13424 return "DW_FORM_ref1";
13425 case DW_FORM_ref2:
13426 return "DW_FORM_ref2";
13427 case DW_FORM_ref4:
13428 return "DW_FORM_ref4";
13429 case DW_FORM_ref8:
13430 return "DW_FORM_ref8";
13431 case DW_FORM_ref_udata:
13432 return "DW_FORM_ref_udata";
13433 case DW_FORM_indirect:
13434 return "DW_FORM_indirect";
348e048f
DE
13435 case DW_FORM_sec_offset:
13436 return "DW_FORM_sec_offset";
13437 case DW_FORM_exprloc:
13438 return "DW_FORM_exprloc";
13439 case DW_FORM_flag_present:
13440 return "DW_FORM_flag_present";
55f1336d
TT
13441 case DW_FORM_ref_sig8:
13442 return "DW_FORM_ref_sig8";
c906108c
SS
13443 default:
13444 return "DW_FORM_<unknown>";
13445 }
13446}
13447
13448/* Convert a DWARF stack opcode into its string name. */
13449
9eae7c52 13450const char *
b1bfef65 13451dwarf_stack_op_name (unsigned op)
c906108c
SS
13452{
13453 switch (op)
13454 {
13455 case DW_OP_addr:
13456 return "DW_OP_addr";
13457 case DW_OP_deref:
13458 return "DW_OP_deref";
13459 case DW_OP_const1u:
13460 return "DW_OP_const1u";
13461 case DW_OP_const1s:
13462 return "DW_OP_const1s";
13463 case DW_OP_const2u:
13464 return "DW_OP_const2u";
13465 case DW_OP_const2s:
13466 return "DW_OP_const2s";
13467 case DW_OP_const4u:
13468 return "DW_OP_const4u";
13469 case DW_OP_const4s:
13470 return "DW_OP_const4s";
13471 case DW_OP_const8u:
13472 return "DW_OP_const8u";
13473 case DW_OP_const8s:
13474 return "DW_OP_const8s";
13475 case DW_OP_constu:
13476 return "DW_OP_constu";
13477 case DW_OP_consts:
13478 return "DW_OP_consts";
13479 case DW_OP_dup:
13480 return "DW_OP_dup";
13481 case DW_OP_drop:
13482 return "DW_OP_drop";
13483 case DW_OP_over:
13484 return "DW_OP_over";
13485 case DW_OP_pick:
13486 return "DW_OP_pick";
13487 case DW_OP_swap:
13488 return "DW_OP_swap";
13489 case DW_OP_rot:
13490 return "DW_OP_rot";
13491 case DW_OP_xderef:
13492 return "DW_OP_xderef";
13493 case DW_OP_abs:
13494 return "DW_OP_abs";
13495 case DW_OP_and:
13496 return "DW_OP_and";
13497 case DW_OP_div:
13498 return "DW_OP_div";
13499 case DW_OP_minus:
13500 return "DW_OP_minus";
13501 case DW_OP_mod:
13502 return "DW_OP_mod";
13503 case DW_OP_mul:
13504 return "DW_OP_mul";
13505 case DW_OP_neg:
13506 return "DW_OP_neg";
13507 case DW_OP_not:
13508 return "DW_OP_not";
13509 case DW_OP_or:
13510 return "DW_OP_or";
13511 case DW_OP_plus:
13512 return "DW_OP_plus";
13513 case DW_OP_plus_uconst:
13514 return "DW_OP_plus_uconst";
13515 case DW_OP_shl:
13516 return "DW_OP_shl";
13517 case DW_OP_shr:
13518 return "DW_OP_shr";
13519 case DW_OP_shra:
13520 return "DW_OP_shra";
13521 case DW_OP_xor:
13522 return "DW_OP_xor";
13523 case DW_OP_bra:
13524 return "DW_OP_bra";
13525 case DW_OP_eq:
13526 return "DW_OP_eq";
13527 case DW_OP_ge:
13528 return "DW_OP_ge";
13529 case DW_OP_gt:
13530 return "DW_OP_gt";
13531 case DW_OP_le:
13532 return "DW_OP_le";
13533 case DW_OP_lt:
13534 return "DW_OP_lt";
13535 case DW_OP_ne:
13536 return "DW_OP_ne";
13537 case DW_OP_skip:
13538 return "DW_OP_skip";
13539 case DW_OP_lit0:
13540 return "DW_OP_lit0";
13541 case DW_OP_lit1:
13542 return "DW_OP_lit1";
13543 case DW_OP_lit2:
13544 return "DW_OP_lit2";
13545 case DW_OP_lit3:
13546 return "DW_OP_lit3";
13547 case DW_OP_lit4:
13548 return "DW_OP_lit4";
13549 case DW_OP_lit5:
13550 return "DW_OP_lit5";
13551 case DW_OP_lit6:
13552 return "DW_OP_lit6";
13553 case DW_OP_lit7:
13554 return "DW_OP_lit7";
13555 case DW_OP_lit8:
13556 return "DW_OP_lit8";
13557 case DW_OP_lit9:
13558 return "DW_OP_lit9";
13559 case DW_OP_lit10:
13560 return "DW_OP_lit10";
13561 case DW_OP_lit11:
13562 return "DW_OP_lit11";
13563 case DW_OP_lit12:
13564 return "DW_OP_lit12";
13565 case DW_OP_lit13:
13566 return "DW_OP_lit13";
13567 case DW_OP_lit14:
13568 return "DW_OP_lit14";
13569 case DW_OP_lit15:
13570 return "DW_OP_lit15";
13571 case DW_OP_lit16:
13572 return "DW_OP_lit16";
13573 case DW_OP_lit17:
13574 return "DW_OP_lit17";
13575 case DW_OP_lit18:
13576 return "DW_OP_lit18";
13577 case DW_OP_lit19:
13578 return "DW_OP_lit19";
13579 case DW_OP_lit20:
13580 return "DW_OP_lit20";
13581 case DW_OP_lit21:
13582 return "DW_OP_lit21";
13583 case DW_OP_lit22:
13584 return "DW_OP_lit22";
13585 case DW_OP_lit23:
13586 return "DW_OP_lit23";
13587 case DW_OP_lit24:
13588 return "DW_OP_lit24";
13589 case DW_OP_lit25:
13590 return "DW_OP_lit25";
13591 case DW_OP_lit26:
13592 return "DW_OP_lit26";
13593 case DW_OP_lit27:
13594 return "DW_OP_lit27";
13595 case DW_OP_lit28:
13596 return "DW_OP_lit28";
13597 case DW_OP_lit29:
13598 return "DW_OP_lit29";
13599 case DW_OP_lit30:
13600 return "DW_OP_lit30";
13601 case DW_OP_lit31:
13602 return "DW_OP_lit31";
13603 case DW_OP_reg0:
13604 return "DW_OP_reg0";
13605 case DW_OP_reg1:
13606 return "DW_OP_reg1";
13607 case DW_OP_reg2:
13608 return "DW_OP_reg2";
13609 case DW_OP_reg3:
13610 return "DW_OP_reg3";
13611 case DW_OP_reg4:
13612 return "DW_OP_reg4";
13613 case DW_OP_reg5:
13614 return "DW_OP_reg5";
13615 case DW_OP_reg6:
13616 return "DW_OP_reg6";
13617 case DW_OP_reg7:
13618 return "DW_OP_reg7";
13619 case DW_OP_reg8:
13620 return "DW_OP_reg8";
13621 case DW_OP_reg9:
13622 return "DW_OP_reg9";
13623 case DW_OP_reg10:
13624 return "DW_OP_reg10";
13625 case DW_OP_reg11:
13626 return "DW_OP_reg11";
13627 case DW_OP_reg12:
13628 return "DW_OP_reg12";
13629 case DW_OP_reg13:
13630 return "DW_OP_reg13";
13631 case DW_OP_reg14:
13632 return "DW_OP_reg14";
13633 case DW_OP_reg15:
13634 return "DW_OP_reg15";
13635 case DW_OP_reg16:
13636 return "DW_OP_reg16";
13637 case DW_OP_reg17:
13638 return "DW_OP_reg17";
13639 case DW_OP_reg18:
13640 return "DW_OP_reg18";
13641 case DW_OP_reg19:
13642 return "DW_OP_reg19";
13643 case DW_OP_reg20:
13644 return "DW_OP_reg20";
13645 case DW_OP_reg21:
13646 return "DW_OP_reg21";
13647 case DW_OP_reg22:
13648 return "DW_OP_reg22";
13649 case DW_OP_reg23:
13650 return "DW_OP_reg23";
13651 case DW_OP_reg24:
13652 return "DW_OP_reg24";
13653 case DW_OP_reg25:
13654 return "DW_OP_reg25";
13655 case DW_OP_reg26:
13656 return "DW_OP_reg26";
13657 case DW_OP_reg27:
13658 return "DW_OP_reg27";
13659 case DW_OP_reg28:
13660 return "DW_OP_reg28";
13661 case DW_OP_reg29:
13662 return "DW_OP_reg29";
13663 case DW_OP_reg30:
13664 return "DW_OP_reg30";
13665 case DW_OP_reg31:
13666 return "DW_OP_reg31";
13667 case DW_OP_breg0:
13668 return "DW_OP_breg0";
13669 case DW_OP_breg1:
13670 return "DW_OP_breg1";
13671 case DW_OP_breg2:
13672 return "DW_OP_breg2";
13673 case DW_OP_breg3:
13674 return "DW_OP_breg3";
13675 case DW_OP_breg4:
13676 return "DW_OP_breg4";
13677 case DW_OP_breg5:
13678 return "DW_OP_breg5";
13679 case DW_OP_breg6:
13680 return "DW_OP_breg6";
13681 case DW_OP_breg7:
13682 return "DW_OP_breg7";
13683 case DW_OP_breg8:
13684 return "DW_OP_breg8";
13685 case DW_OP_breg9:
13686 return "DW_OP_breg9";
13687 case DW_OP_breg10:
13688 return "DW_OP_breg10";
13689 case DW_OP_breg11:
13690 return "DW_OP_breg11";
13691 case DW_OP_breg12:
13692 return "DW_OP_breg12";
13693 case DW_OP_breg13:
13694 return "DW_OP_breg13";
13695 case DW_OP_breg14:
13696 return "DW_OP_breg14";
13697 case DW_OP_breg15:
13698 return "DW_OP_breg15";
13699 case DW_OP_breg16:
13700 return "DW_OP_breg16";
13701 case DW_OP_breg17:
13702 return "DW_OP_breg17";
13703 case DW_OP_breg18:
13704 return "DW_OP_breg18";
13705 case DW_OP_breg19:
13706 return "DW_OP_breg19";
13707 case DW_OP_breg20:
13708 return "DW_OP_breg20";
13709 case DW_OP_breg21:
13710 return "DW_OP_breg21";
13711 case DW_OP_breg22:
13712 return "DW_OP_breg22";
13713 case DW_OP_breg23:
13714 return "DW_OP_breg23";
13715 case DW_OP_breg24:
13716 return "DW_OP_breg24";
13717 case DW_OP_breg25:
13718 return "DW_OP_breg25";
13719 case DW_OP_breg26:
13720 return "DW_OP_breg26";
13721 case DW_OP_breg27:
13722 return "DW_OP_breg27";
13723 case DW_OP_breg28:
13724 return "DW_OP_breg28";
13725 case DW_OP_breg29:
13726 return "DW_OP_breg29";
13727 case DW_OP_breg30:
13728 return "DW_OP_breg30";
13729 case DW_OP_breg31:
13730 return "DW_OP_breg31";
13731 case DW_OP_regx:
13732 return "DW_OP_regx";
13733 case DW_OP_fbreg:
13734 return "DW_OP_fbreg";
13735 case DW_OP_bregx:
13736 return "DW_OP_bregx";
13737 case DW_OP_piece:
13738 return "DW_OP_piece";
13739 case DW_OP_deref_size:
13740 return "DW_OP_deref_size";
13741 case DW_OP_xderef_size:
13742 return "DW_OP_xderef_size";
13743 case DW_OP_nop:
13744 return "DW_OP_nop";
b7619582 13745 /* DWARF 3 extensions. */
ed348acc
EZ
13746 case DW_OP_push_object_address:
13747 return "DW_OP_push_object_address";
13748 case DW_OP_call2:
13749 return "DW_OP_call2";
13750 case DW_OP_call4:
13751 return "DW_OP_call4";
13752 case DW_OP_call_ref:
13753 return "DW_OP_call_ref";
b7619582
GF
13754 case DW_OP_form_tls_address:
13755 return "DW_OP_form_tls_address";
13756 case DW_OP_call_frame_cfa:
13757 return "DW_OP_call_frame_cfa";
13758 case DW_OP_bit_piece:
13759 return "DW_OP_bit_piece";
9eae7c52
TT
13760 /* DWARF 4 extensions. */
13761 case DW_OP_implicit_value:
13762 return "DW_OP_implicit_value";
13763 case DW_OP_stack_value:
13764 return "DW_OP_stack_value";
13765 /* GNU extensions. */
ed348acc
EZ
13766 case DW_OP_GNU_push_tls_address:
13767 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13768 case DW_OP_GNU_uninit:
13769 return "DW_OP_GNU_uninit";
589b4a32
DE
13770 case DW_OP_GNU_encoded_addr:
13771 return "DW_OP_GNU_encoded_addr";
8cf6f0b1
TT
13772 case DW_OP_GNU_implicit_pointer:
13773 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13774 case DW_OP_GNU_entry_value:
13775 return "DW_OP_GNU_entry_value";
13776 case DW_OP_GNU_const_type:
13777 return "DW_OP_GNU_const_type";
13778 case DW_OP_GNU_regval_type:
13779 return "DW_OP_GNU_regval_type";
13780 case DW_OP_GNU_deref_type:
13781 return "DW_OP_GNU_deref_type";
13782 case DW_OP_GNU_convert:
13783 return "DW_OP_GNU_convert";
13784 case DW_OP_GNU_reinterpret:
13785 return "DW_OP_GNU_reinterpret";
589b4a32
DE
13786 case DW_OP_GNU_parameter_ref:
13787 return "DW_OP_GNU_parameter_ref";
c906108c 13788 default:
b1bfef65 13789 return NULL;
c906108c
SS
13790 }
13791}
13792
13793static char *
fba45db2 13794dwarf_bool_name (unsigned mybool)
c906108c
SS
13795{
13796 if (mybool)
13797 return "TRUE";
13798 else
13799 return "FALSE";
13800}
13801
13802/* Convert a DWARF type code into its string name. */
13803
13804static char *
aa1ee363 13805dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13806{
13807 switch (enc)
13808 {
b7619582
GF
13809 case DW_ATE_void:
13810 return "DW_ATE_void";
c906108c
SS
13811 case DW_ATE_address:
13812 return "DW_ATE_address";
13813 case DW_ATE_boolean:
13814 return "DW_ATE_boolean";
13815 case DW_ATE_complex_float:
13816 return "DW_ATE_complex_float";
13817 case DW_ATE_float:
13818 return "DW_ATE_float";
13819 case DW_ATE_signed:
13820 return "DW_ATE_signed";
13821 case DW_ATE_signed_char:
13822 return "DW_ATE_signed_char";
13823 case DW_ATE_unsigned:
13824 return "DW_ATE_unsigned";
13825 case DW_ATE_unsigned_char:
13826 return "DW_ATE_unsigned_char";
b7619582 13827 /* DWARF 3. */
d9fa45fe
DC
13828 case DW_ATE_imaginary_float:
13829 return "DW_ATE_imaginary_float";
b7619582
GF
13830 case DW_ATE_packed_decimal:
13831 return "DW_ATE_packed_decimal";
13832 case DW_ATE_numeric_string:
13833 return "DW_ATE_numeric_string";
13834 case DW_ATE_edited:
13835 return "DW_ATE_edited";
13836 case DW_ATE_signed_fixed:
13837 return "DW_ATE_signed_fixed";
13838 case DW_ATE_unsigned_fixed:
13839 return "DW_ATE_unsigned_fixed";
13840 case DW_ATE_decimal_float:
13841 return "DW_ATE_decimal_float";
75079b2b
TT
13842 /* DWARF 4. */
13843 case DW_ATE_UTF:
13844 return "DW_ATE_UTF";
b7619582
GF
13845 /* HP extensions. */
13846 case DW_ATE_HP_float80:
13847 return "DW_ATE_HP_float80";
13848 case DW_ATE_HP_complex_float80:
13849 return "DW_ATE_HP_complex_float80";
13850 case DW_ATE_HP_float128:
13851 return "DW_ATE_HP_float128";
13852 case DW_ATE_HP_complex_float128:
13853 return "DW_ATE_HP_complex_float128";
13854 case DW_ATE_HP_floathpintel:
13855 return "DW_ATE_HP_floathpintel";
13856 case DW_ATE_HP_imaginary_float80:
13857 return "DW_ATE_HP_imaginary_float80";
13858 case DW_ATE_HP_imaginary_float128:
13859 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13860 default:
13861 return "DW_ATE_<unknown>";
13862 }
13863}
13864
0963b4bd 13865/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13866
13867#if 0
13868static char *
aa1ee363 13869dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13870{
13871 switch (cfi_opc)
13872 {
13873 case DW_CFA_advance_loc:
13874 return "DW_CFA_advance_loc";
13875 case DW_CFA_offset:
13876 return "DW_CFA_offset";
13877 case DW_CFA_restore:
13878 return "DW_CFA_restore";
13879 case DW_CFA_nop:
13880 return "DW_CFA_nop";
13881 case DW_CFA_set_loc:
13882 return "DW_CFA_set_loc";
13883 case DW_CFA_advance_loc1:
13884 return "DW_CFA_advance_loc1";
13885 case DW_CFA_advance_loc2:
13886 return "DW_CFA_advance_loc2";
13887 case DW_CFA_advance_loc4:
13888 return "DW_CFA_advance_loc4";
13889 case DW_CFA_offset_extended:
13890 return "DW_CFA_offset_extended";
13891 case DW_CFA_restore_extended:
13892 return "DW_CFA_restore_extended";
13893 case DW_CFA_undefined:
13894 return "DW_CFA_undefined";
13895 case DW_CFA_same_value:
13896 return "DW_CFA_same_value";
13897 case DW_CFA_register:
13898 return "DW_CFA_register";
13899 case DW_CFA_remember_state:
13900 return "DW_CFA_remember_state";
13901 case DW_CFA_restore_state:
13902 return "DW_CFA_restore_state";
13903 case DW_CFA_def_cfa:
13904 return "DW_CFA_def_cfa";
13905 case DW_CFA_def_cfa_register:
13906 return "DW_CFA_def_cfa_register";
13907 case DW_CFA_def_cfa_offset:
13908 return "DW_CFA_def_cfa_offset";
b7619582 13909 /* DWARF 3. */
985cb1a3
JM
13910 case DW_CFA_def_cfa_expression:
13911 return "DW_CFA_def_cfa_expression";
13912 case DW_CFA_expression:
13913 return "DW_CFA_expression";
13914 case DW_CFA_offset_extended_sf:
13915 return "DW_CFA_offset_extended_sf";
13916 case DW_CFA_def_cfa_sf:
13917 return "DW_CFA_def_cfa_sf";
13918 case DW_CFA_def_cfa_offset_sf:
13919 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13920 case DW_CFA_val_offset:
13921 return "DW_CFA_val_offset";
13922 case DW_CFA_val_offset_sf:
13923 return "DW_CFA_val_offset_sf";
13924 case DW_CFA_val_expression:
13925 return "DW_CFA_val_expression";
13926 /* SGI/MIPS specific. */
c906108c
SS
13927 case DW_CFA_MIPS_advance_loc8:
13928 return "DW_CFA_MIPS_advance_loc8";
b7619582 13929 /* GNU extensions. */
985cb1a3
JM
13930 case DW_CFA_GNU_window_save:
13931 return "DW_CFA_GNU_window_save";
13932 case DW_CFA_GNU_args_size:
13933 return "DW_CFA_GNU_args_size";
13934 case DW_CFA_GNU_negative_offset_extended:
13935 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13936 default:
13937 return "DW_CFA_<unknown>";
13938 }
13939}
13940#endif
13941
f9aca02d 13942static void
d97bc12b 13943dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13944{
13945 unsigned int i;
13946
d97bc12b
DE
13947 print_spaces (indent, f);
13948 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 13949 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
13950
13951 if (die->parent != NULL)
13952 {
13953 print_spaces (indent, f);
13954 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 13955 die->parent->offset.sect_off);
d97bc12b
DE
13956 }
13957
13958 print_spaces (indent, f);
13959 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13960 dwarf_bool_name (die->child != NULL));
c906108c 13961
d97bc12b
DE
13962 print_spaces (indent, f);
13963 fprintf_unfiltered (f, " attributes:\n");
13964
c906108c
SS
13965 for (i = 0; i < die->num_attrs; ++i)
13966 {
d97bc12b
DE
13967 print_spaces (indent, f);
13968 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13969 dwarf_attr_name (die->attrs[i].name),
13970 dwarf_form_name (die->attrs[i].form));
d97bc12b 13971
c906108c
SS
13972 switch (die->attrs[i].form)
13973 {
c906108c 13974 case DW_FORM_addr:
d97bc12b 13975 fprintf_unfiltered (f, "address: ");
5af949e3 13976 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13977 break;
13978 case DW_FORM_block2:
13979 case DW_FORM_block4:
13980 case DW_FORM_block:
13981 case DW_FORM_block1:
3e43a32a
MS
13982 fprintf_unfiltered (f, "block: size %d",
13983 DW_BLOCK (&die->attrs[i])->size);
c906108c 13984 break;
2dc7f7b3
TT
13985 case DW_FORM_exprloc:
13986 fprintf_unfiltered (f, "expression: size %u",
13987 DW_BLOCK (&die->attrs[i])->size);
13988 break;
4568ecf9
DE
13989 case DW_FORM_ref_addr:
13990 fprintf_unfiltered (f, "ref address: ");
13991 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
13992 break;
10b3939b
DJ
13993 case DW_FORM_ref1:
13994 case DW_FORM_ref2:
13995 case DW_FORM_ref4:
4568ecf9
DE
13996 case DW_FORM_ref8:
13997 case DW_FORM_ref_udata:
d97bc12b 13998 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 13999 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14000 break;
c906108c
SS
14001 case DW_FORM_data1:
14002 case DW_FORM_data2:
14003 case DW_FORM_data4:
ce5d95e1 14004 case DW_FORM_data8:
c906108c
SS
14005 case DW_FORM_udata:
14006 case DW_FORM_sdata:
43bbcdc2
PH
14007 fprintf_unfiltered (f, "constant: %s",
14008 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14009 break;
2dc7f7b3
TT
14010 case DW_FORM_sec_offset:
14011 fprintf_unfiltered (f, "section offset: %s",
14012 pulongest (DW_UNSND (&die->attrs[i])));
14013 break;
55f1336d 14014 case DW_FORM_ref_sig8:
348e048f
DE
14015 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14016 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14017 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14018 else
14019 fprintf_unfiltered (f, "signatured type, offset: unknown");
14020 break;
c906108c 14021 case DW_FORM_string:
4bdf3d34 14022 case DW_FORM_strp:
8285870a 14023 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14024 DW_STRING (&die->attrs[i])
8285870a
JK
14025 ? DW_STRING (&die->attrs[i]) : "",
14026 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14027 break;
14028 case DW_FORM_flag:
14029 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14030 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14031 else
d97bc12b 14032 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14033 break;
2dc7f7b3
TT
14034 case DW_FORM_flag_present:
14035 fprintf_unfiltered (f, "flag: TRUE");
14036 break;
a8329558 14037 case DW_FORM_indirect:
0963b4bd
MS
14038 /* The reader will have reduced the indirect form to
14039 the "base form" so this form should not occur. */
3e43a32a
MS
14040 fprintf_unfiltered (f,
14041 "unexpected attribute form: DW_FORM_indirect");
a8329558 14042 break;
c906108c 14043 default:
d97bc12b 14044 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14045 die->attrs[i].form);
d97bc12b 14046 break;
c906108c 14047 }
d97bc12b 14048 fprintf_unfiltered (f, "\n");
c906108c
SS
14049 }
14050}
14051
f9aca02d 14052static void
d97bc12b 14053dump_die_for_error (struct die_info *die)
c906108c 14054{
d97bc12b
DE
14055 dump_die_shallow (gdb_stderr, 0, die);
14056}
14057
14058static void
14059dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14060{
14061 int indent = level * 4;
14062
14063 gdb_assert (die != NULL);
14064
14065 if (level >= max_level)
14066 return;
14067
14068 dump_die_shallow (f, indent, die);
14069
14070 if (die->child != NULL)
c906108c 14071 {
d97bc12b
DE
14072 print_spaces (indent, f);
14073 fprintf_unfiltered (f, " Children:");
14074 if (level + 1 < max_level)
14075 {
14076 fprintf_unfiltered (f, "\n");
14077 dump_die_1 (f, level + 1, max_level, die->child);
14078 }
14079 else
14080 {
3e43a32a
MS
14081 fprintf_unfiltered (f,
14082 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14083 }
14084 }
14085
14086 if (die->sibling != NULL && level > 0)
14087 {
14088 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14089 }
14090}
14091
d97bc12b
DE
14092/* This is called from the pdie macro in gdbinit.in.
14093 It's not static so gcc will keep a copy callable from gdb. */
14094
14095void
14096dump_die (struct die_info *die, int max_level)
14097{
14098 dump_die_1 (gdb_stdlog, 0, max_level, die);
14099}
14100
f9aca02d 14101static void
51545339 14102store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14103{
51545339 14104 void **slot;
c906108c 14105
b64f50a1
JK
14106 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14107 INSERT);
51545339
DJ
14108
14109 *slot = die;
c906108c
SS
14110}
14111
b64f50a1
JK
14112/* DW_ADDR is always stored already as sect_offset; despite for the forms
14113 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14114
93311388
DE
14115static int
14116is_ref_attr (struct attribute *attr)
c906108c 14117{
c906108c
SS
14118 switch (attr->form)
14119 {
14120 case DW_FORM_ref_addr:
c906108c
SS
14121 case DW_FORM_ref1:
14122 case DW_FORM_ref2:
14123 case DW_FORM_ref4:
613e1657 14124 case DW_FORM_ref8:
c906108c 14125 case DW_FORM_ref_udata:
93311388 14126 return 1;
c906108c 14127 default:
93311388 14128 return 0;
c906108c 14129 }
93311388
DE
14130}
14131
b64f50a1
JK
14132/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14133 required kind. */
14134
14135static sect_offset
93311388
DE
14136dwarf2_get_ref_die_offset (struct attribute *attr)
14137{
4568ecf9 14138 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14139
93311388 14140 if (is_ref_attr (attr))
b64f50a1 14141 return retval;
93311388 14142
b64f50a1 14143 retval.sect_off = 0;
93311388
DE
14144 complaint (&symfile_complaints,
14145 _("unsupported die ref attribute form: '%s'"),
14146 dwarf_form_name (attr->form));
b64f50a1 14147 return retval;
c906108c
SS
14148}
14149
43bbcdc2
PH
14150/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14151 * the value held by the attribute is not constant. */
a02abb62 14152
43bbcdc2 14153static LONGEST
a02abb62
JB
14154dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14155{
14156 if (attr->form == DW_FORM_sdata)
14157 return DW_SND (attr);
14158 else if (attr->form == DW_FORM_udata
14159 || attr->form == DW_FORM_data1
14160 || attr->form == DW_FORM_data2
14161 || attr->form == DW_FORM_data4
14162 || attr->form == DW_FORM_data8)
14163 return DW_UNSND (attr);
14164 else
14165 {
3e43a32a
MS
14166 complaint (&symfile_complaints,
14167 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14168 dwarf_form_name (attr->form));
14169 return default_value;
14170 }
14171}
14172
03dd20cc 14173/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14174 unit and add it to our queue.
14175 The result is non-zero if PER_CU was queued, otherwise the result is zero
14176 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14177
348e048f 14178static int
03dd20cc
DJ
14179maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14180 struct dwarf2_per_cu_data *per_cu)
14181{
98bfdba5
PA
14182 /* We may arrive here during partial symbol reading, if we need full
14183 DIEs to process an unusual case (e.g. template arguments). Do
14184 not queue PER_CU, just tell our caller to load its DIEs. */
14185 if (dwarf2_per_objfile->reading_partial_symbols)
14186 {
14187 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14188 return 1;
14189 return 0;
14190 }
14191
03dd20cc
DJ
14192 /* Mark the dependence relation so that we don't flush PER_CU
14193 too early. */
14194 dwarf2_add_dependence (this_cu, per_cu);
14195
14196 /* If it's already on the queue, we have nothing to do. */
14197 if (per_cu->queued)
348e048f 14198 return 0;
03dd20cc
DJ
14199
14200 /* If the compilation unit is already loaded, just mark it as
14201 used. */
14202 if (per_cu->cu != NULL)
14203 {
14204 per_cu->cu->last_used = 0;
348e048f 14205 return 0;
03dd20cc
DJ
14206 }
14207
14208 /* Add it to the queue. */
a0f42c21 14209 queue_comp_unit (per_cu);
348e048f
DE
14210
14211 return 1;
14212}
14213
14214/* Follow reference or signature attribute ATTR of SRC_DIE.
14215 On entry *REF_CU is the CU of SRC_DIE.
14216 On exit *REF_CU is the CU of the result. */
14217
14218static struct die_info *
14219follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14220 struct dwarf2_cu **ref_cu)
14221{
14222 struct die_info *die;
14223
14224 if (is_ref_attr (attr))
14225 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14226 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14227 die = follow_die_sig (src_die, attr, ref_cu);
14228 else
14229 {
14230 dump_die_for_error (src_die);
14231 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14232 (*ref_cu)->objfile->name);
14233 }
14234
14235 return die;
03dd20cc
DJ
14236}
14237
5c631832 14238/* Follow reference OFFSET.
673bfd45
DE
14239 On entry *REF_CU is the CU of the source die referencing OFFSET.
14240 On exit *REF_CU is the CU of the result.
14241 Returns NULL if OFFSET is invalid. */
f504f079 14242
f9aca02d 14243static struct die_info *
b64f50a1 14244follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14245{
10b3939b 14246 struct die_info temp_die;
f2f0e013 14247 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14248
348e048f
DE
14249 gdb_assert (cu->per_cu != NULL);
14250
98bfdba5
PA
14251 target_cu = cu;
14252
b0df02fd 14253 if (cu->per_cu->debug_types_section)
348e048f
DE
14254 {
14255 /* .debug_types CUs cannot reference anything outside their CU.
14256 If they need to, they have to reference a signatured type via
55f1336d 14257 DW_FORM_ref_sig8. */
348e048f 14258 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14259 return NULL;
348e048f
DE
14260 }
14261 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14262 {
14263 struct dwarf2_per_cu_data *per_cu;
9a619af0 14264
45452591 14265 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14266
14267 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14268 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14269 load_full_comp_unit (per_cu);
03dd20cc 14270
10b3939b
DJ
14271 target_cu = per_cu->cu;
14272 }
98bfdba5
PA
14273 else if (cu->dies == NULL)
14274 {
14275 /* We're loading full DIEs during partial symbol reading. */
14276 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14277 load_full_comp_unit (cu->per_cu);
98bfdba5 14278 }
c906108c 14279
f2f0e013 14280 *ref_cu = target_cu;
51545339 14281 temp_die.offset = offset;
b64f50a1 14282 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14283}
10b3939b 14284
5c631832
JK
14285/* Follow reference attribute ATTR of SRC_DIE.
14286 On entry *REF_CU is the CU of SRC_DIE.
14287 On exit *REF_CU is the CU of the result. */
14288
14289static struct die_info *
14290follow_die_ref (struct die_info *src_die, struct attribute *attr,
14291 struct dwarf2_cu **ref_cu)
14292{
b64f50a1 14293 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14294 struct dwarf2_cu *cu = *ref_cu;
14295 struct die_info *die;
14296
14297 die = follow_die_offset (offset, ref_cu);
14298 if (!die)
14299 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14300 "at 0x%x [in module %s]"),
b64f50a1 14301 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14302
5c631832
JK
14303 return die;
14304}
14305
d83e736b
JK
14306/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14307 Returned value is intended for DW_OP_call*. Returned
14308 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14309
14310struct dwarf2_locexpr_baton
b64f50a1 14311dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14312 struct dwarf2_per_cu_data *per_cu,
14313 CORE_ADDR (*get_frame_pc) (void *baton),
14314 void *baton)
5c631832 14315{
b64f50a1 14316 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14317 struct dwarf2_cu *cu;
5c631832
JK
14318 struct die_info *die;
14319 struct attribute *attr;
14320 struct dwarf2_locexpr_baton retval;
14321
8cf6f0b1
TT
14322 dw2_setup (per_cu->objfile);
14323
918dd910
JK
14324 if (per_cu->cu == NULL)
14325 load_cu (per_cu);
14326 cu = per_cu->cu;
14327
5c631832
JK
14328 die = follow_die_offset (offset, &cu);
14329 if (!die)
14330 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 14331 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14332
14333 attr = dwarf2_attr (die, DW_AT_location, cu);
14334 if (!attr)
14335 {
e103e986
JK
14336 /* DWARF: "If there is no such attribute, then there is no effect.".
14337 DATA is ignored if SIZE is 0. */
5c631832 14338
e103e986 14339 retval.data = NULL;
5c631832
JK
14340 retval.size = 0;
14341 }
8cf6f0b1
TT
14342 else if (attr_form_is_section_offset (attr))
14343 {
14344 struct dwarf2_loclist_baton loclist_baton;
14345 CORE_ADDR pc = (*get_frame_pc) (baton);
14346 size_t size;
14347
14348 fill_in_loclist_baton (cu, &loclist_baton, attr);
14349
14350 retval.data = dwarf2_find_location_expression (&loclist_baton,
14351 &size, pc);
14352 retval.size = size;
14353 }
5c631832
JK
14354 else
14355 {
14356 if (!attr_form_is_block (attr))
14357 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14358 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 14359 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14360
14361 retval.data = DW_BLOCK (attr)->data;
14362 retval.size = DW_BLOCK (attr)->size;
14363 }
14364 retval.per_cu = cu->per_cu;
918dd910 14365
918dd910
JK
14366 age_cached_comp_units ();
14367
5c631832 14368 return retval;
348e048f
DE
14369}
14370
8a9b8146
TT
14371/* Return the type of the DIE at DIE_OFFSET in the CU named by
14372 PER_CU. */
14373
14374struct type *
b64f50a1 14375dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
14376 struct dwarf2_per_cu_data *per_cu)
14377{
b64f50a1
JK
14378 sect_offset die_offset_sect;
14379
8a9b8146 14380 dw2_setup (per_cu->objfile);
b64f50a1
JK
14381
14382 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14383 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
14384}
14385
348e048f
DE
14386/* Follow the signature attribute ATTR in 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_sig (struct die_info *src_die, struct attribute *attr,
14392 struct dwarf2_cu **ref_cu)
14393{
14394 struct objfile *objfile = (*ref_cu)->objfile;
14395 struct die_info temp_die;
14396 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14397 struct dwarf2_cu *sig_cu;
14398 struct die_info *die;
14399
14400 /* sig_type will be NULL if the signatured type is missing from
14401 the debug info. */
14402 if (sig_type == NULL)
14403 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14404 "at 0x%x [in module %s]"),
b64f50a1 14405 src_die->offset.sect_off, objfile->name);
348e048f
DE
14406
14407 /* If necessary, add it to the queue and load its DIEs. */
14408
14409 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14410 read_signatured_type (sig_type);
348e048f
DE
14411
14412 gdb_assert (sig_type->per_cu.cu != NULL);
14413
14414 sig_cu = sig_type->per_cu.cu;
b64f50a1
JK
14415 temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14416 + sig_type->type_offset.cu_off);
14417 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14418 temp_die.offset.sect_off);
348e048f
DE
14419 if (die)
14420 {
14421 *ref_cu = sig_cu;
14422 return die;
14423 }
14424
3e43a32a
MS
14425 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14426 "from DIE at 0x%x [in module %s]"),
b64f50a1 14427 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
14428}
14429
14430/* Given an offset of a signatured type, return its signatured_type. */
14431
14432static struct signatured_type *
8b70b953
TT
14433lookup_signatured_type_at_offset (struct objfile *objfile,
14434 struct dwarf2_section_info *section,
b64f50a1 14435 sect_offset offset)
348e048f 14436{
b64f50a1 14437 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
14438 unsigned int length, initial_length_size;
14439 unsigned int sig_offset;
52dc124a 14440 struct signatured_type find_entry, *sig_type;
348e048f
DE
14441
14442 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14443 sig_offset = (initial_length_size
14444 + 2 /*version*/
14445 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14446 + 1 /*address_size*/);
14447 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 14448 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
14449
14450 /* This is only used to lookup previously recorded types.
14451 If we didn't find it, it's our bug. */
52dc124a
DE
14452 gdb_assert (sig_type != NULL);
14453 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 14454
52dc124a 14455 return sig_type;
348e048f
DE
14456}
14457
e5fe5e75 14458/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14459
14460static void
e5fe5e75 14461load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14462{
e5fe5e75
DE
14463 struct objfile *objfile = per_cu->objfile;
14464 struct dwarf2_section_info *sect = per_cu->debug_types_section;
b64f50a1 14465 sect_offset offset = per_cu->offset;
52dc124a 14466 struct signatured_type *sig_type;
348e048f 14467
8b70b953 14468 dwarf2_read_section (objfile, sect);
be391dca 14469
348e048f 14470 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14471 hash table lookup. */
14472 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14473 the signature to assert we found the right one.
14474 Ok, but it's a lot of work. We should simplify things so any needed
14475 assert doesn't require all this clumsiness. */
52dc124a 14476 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 14477
52dc124a 14478 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14479
52dc124a 14480 read_signatured_type (sig_type);
348e048f 14481
52dc124a 14482 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
14483}
14484
14485/* Read in a signatured type and build its CU and DIEs. */
14486
14487static void
52dc124a 14488read_signatured_type (struct signatured_type *sig_type)
348e048f 14489{
52dc124a 14490 struct objfile *objfile = sig_type->per_cu.objfile;
1fd400ff 14491 gdb_byte *types_ptr;
348e048f
DE
14492 struct die_reader_specs reader_specs;
14493 struct dwarf2_cu *cu;
14494 ULONGEST signature;
14495 struct cleanup *back_to, *free_cu_cleanup;
52dc124a 14496 struct dwarf2_section_info *section = sig_type->per_cu.debug_types_section;
348e048f 14497
8b70b953 14498 dwarf2_read_section (objfile, section);
52dc124a 14499 types_ptr = section->buffer + sig_type->per_cu.offset.sect_off;
1fd400ff 14500
52dc124a 14501 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14502
9816fde3 14503 cu = xmalloc (sizeof (*cu));
52dc124a 14504 init_one_comp_unit (cu, &sig_type->per_cu);
348e048f
DE
14505
14506 /* If an error occurs while loading, release our storage. */
68dc6402 14507 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14508
9ff913ba
DE
14509 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14510 &signature, NULL);
52dc124a 14511 gdb_assert (signature == sig_type->signature);
348e048f
DE
14512
14513 cu->die_hash
14514 = htab_create_alloc_ex (cu->header.length / 12,
14515 die_hash,
14516 die_eq,
14517 NULL,
14518 &cu->comp_unit_obstack,
14519 hashtab_obstack_allocate,
14520 dummy_obstack_deallocate);
14521
e5fe5e75 14522 dwarf2_read_abbrevs (cu);
348e048f
DE
14523 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14524
14525 init_cu_die_reader (&reader_specs, cu);
14526
14527 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14528 NULL /*parent*/);
14529
14530 /* We try not to read any attributes in this function, because not
9cdd5dbd 14531 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14532 table processing isn't initialized. But we have to set the CU language,
14533 or we won't be able to build types correctly. */
9816fde3 14534 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14535
14536 do_cleanups (back_to);
14537
14538 /* We've successfully allocated this compilation unit. Let our caller
14539 clean it up when finished with it. */
14540 discard_cleanups (free_cu_cleanup);
14541
c5b7e1cb 14542 /* Link this TU into read_in_chain. */
52dc124a
DE
14543 sig_type->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14544 dwarf2_per_objfile->read_in_chain = &sig_type->per_cu;
c906108c
SS
14545}
14546
c906108c
SS
14547/* Decode simple location descriptions.
14548 Given a pointer to a dwarf block that defines a location, compute
14549 the location and return the value.
14550
4cecd739
DJ
14551 NOTE drow/2003-11-18: This function is called in two situations
14552 now: for the address of static or global variables (partial symbols
14553 only) and for offsets into structures which are expected to be
14554 (more or less) constant. The partial symbol case should go away,
14555 and only the constant case should remain. That will let this
14556 function complain more accurately. A few special modes are allowed
14557 without complaint for global variables (for instance, global
14558 register values and thread-local values).
c906108c
SS
14559
14560 A location description containing no operations indicates that the
4cecd739 14561 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14562 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14563 callers will only want a very basic result and this can become a
21ae7a4d
JK
14564 complaint.
14565
14566 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14567
14568static CORE_ADDR
e7c27a73 14569decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14570{
e7c27a73 14571 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14572 int i;
14573 int size = blk->size;
14574 gdb_byte *data = blk->data;
14575 CORE_ADDR stack[64];
14576 int stacki;
14577 unsigned int bytes_read, unsnd;
14578 gdb_byte op;
c906108c 14579
21ae7a4d
JK
14580 i = 0;
14581 stacki = 0;
14582 stack[stacki] = 0;
14583 stack[++stacki] = 0;
14584
14585 while (i < size)
14586 {
14587 op = data[i++];
14588 switch (op)
14589 {
14590 case DW_OP_lit0:
14591 case DW_OP_lit1:
14592 case DW_OP_lit2:
14593 case DW_OP_lit3:
14594 case DW_OP_lit4:
14595 case DW_OP_lit5:
14596 case DW_OP_lit6:
14597 case DW_OP_lit7:
14598 case DW_OP_lit8:
14599 case DW_OP_lit9:
14600 case DW_OP_lit10:
14601 case DW_OP_lit11:
14602 case DW_OP_lit12:
14603 case DW_OP_lit13:
14604 case DW_OP_lit14:
14605 case DW_OP_lit15:
14606 case DW_OP_lit16:
14607 case DW_OP_lit17:
14608 case DW_OP_lit18:
14609 case DW_OP_lit19:
14610 case DW_OP_lit20:
14611 case DW_OP_lit21:
14612 case DW_OP_lit22:
14613 case DW_OP_lit23:
14614 case DW_OP_lit24:
14615 case DW_OP_lit25:
14616 case DW_OP_lit26:
14617 case DW_OP_lit27:
14618 case DW_OP_lit28:
14619 case DW_OP_lit29:
14620 case DW_OP_lit30:
14621 case DW_OP_lit31:
14622 stack[++stacki] = op - DW_OP_lit0;
14623 break;
f1bea926 14624
21ae7a4d
JK
14625 case DW_OP_reg0:
14626 case DW_OP_reg1:
14627 case DW_OP_reg2:
14628 case DW_OP_reg3:
14629 case DW_OP_reg4:
14630 case DW_OP_reg5:
14631 case DW_OP_reg6:
14632 case DW_OP_reg7:
14633 case DW_OP_reg8:
14634 case DW_OP_reg9:
14635 case DW_OP_reg10:
14636 case DW_OP_reg11:
14637 case DW_OP_reg12:
14638 case DW_OP_reg13:
14639 case DW_OP_reg14:
14640 case DW_OP_reg15:
14641 case DW_OP_reg16:
14642 case DW_OP_reg17:
14643 case DW_OP_reg18:
14644 case DW_OP_reg19:
14645 case DW_OP_reg20:
14646 case DW_OP_reg21:
14647 case DW_OP_reg22:
14648 case DW_OP_reg23:
14649 case DW_OP_reg24:
14650 case DW_OP_reg25:
14651 case DW_OP_reg26:
14652 case DW_OP_reg27:
14653 case DW_OP_reg28:
14654 case DW_OP_reg29:
14655 case DW_OP_reg30:
14656 case DW_OP_reg31:
14657 stack[++stacki] = op - DW_OP_reg0;
14658 if (i < size)
14659 dwarf2_complex_location_expr_complaint ();
14660 break;
c906108c 14661
21ae7a4d
JK
14662 case DW_OP_regx:
14663 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14664 i += bytes_read;
14665 stack[++stacki] = unsnd;
14666 if (i < size)
14667 dwarf2_complex_location_expr_complaint ();
14668 break;
c906108c 14669
21ae7a4d
JK
14670 case DW_OP_addr:
14671 stack[++stacki] = read_address (objfile->obfd, &data[i],
14672 cu, &bytes_read);
14673 i += bytes_read;
14674 break;
d53d4ac5 14675
21ae7a4d
JK
14676 case DW_OP_const1u:
14677 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14678 i += 1;
14679 break;
14680
14681 case DW_OP_const1s:
14682 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14683 i += 1;
14684 break;
14685
14686 case DW_OP_const2u:
14687 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14688 i += 2;
14689 break;
14690
14691 case DW_OP_const2s:
14692 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14693 i += 2;
14694 break;
d53d4ac5 14695
21ae7a4d
JK
14696 case DW_OP_const4u:
14697 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14698 i += 4;
14699 break;
14700
14701 case DW_OP_const4s:
14702 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14703 i += 4;
14704 break;
14705
585861ea
JK
14706 case DW_OP_const8u:
14707 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14708 i += 8;
14709 break;
14710
21ae7a4d
JK
14711 case DW_OP_constu:
14712 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14713 &bytes_read);
14714 i += bytes_read;
14715 break;
14716
14717 case DW_OP_consts:
14718 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14719 i += bytes_read;
14720 break;
14721
14722 case DW_OP_dup:
14723 stack[stacki + 1] = stack[stacki];
14724 stacki++;
14725 break;
14726
14727 case DW_OP_plus:
14728 stack[stacki - 1] += stack[stacki];
14729 stacki--;
14730 break;
14731
14732 case DW_OP_plus_uconst:
14733 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14734 &bytes_read);
14735 i += bytes_read;
14736 break;
14737
14738 case DW_OP_minus:
14739 stack[stacki - 1] -= stack[stacki];
14740 stacki--;
14741 break;
14742
14743 case DW_OP_deref:
14744 /* If we're not the last op, then we definitely can't encode
14745 this using GDB's address_class enum. This is valid for partial
14746 global symbols, although the variable's address will be bogus
14747 in the psymtab. */
14748 if (i < size)
14749 dwarf2_complex_location_expr_complaint ();
14750 break;
14751
14752 case DW_OP_GNU_push_tls_address:
14753 /* The top of the stack has the offset from the beginning
14754 of the thread control block at which the variable is located. */
14755 /* Nothing should follow this operator, so the top of stack would
14756 be returned. */
14757 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14758 address will be bogus in the psymtab. Make it always at least
14759 non-zero to not look as a variable garbage collected by linker
14760 which have DW_OP_addr 0. */
21ae7a4d
JK
14761 if (i < size)
14762 dwarf2_complex_location_expr_complaint ();
585861ea 14763 stack[stacki]++;
21ae7a4d
JK
14764 break;
14765
14766 case DW_OP_GNU_uninit:
14767 break;
14768
14769 default:
14770 {
14771 const char *name = dwarf_stack_op_name (op);
14772
14773 if (name)
14774 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14775 name);
14776 else
14777 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14778 op);
14779 }
14780
14781 return (stack[stacki]);
d53d4ac5 14782 }
3c6e0cb3 14783
21ae7a4d
JK
14784 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14785 outside of the allocated space. Also enforce minimum>0. */
14786 if (stacki >= ARRAY_SIZE (stack) - 1)
14787 {
14788 complaint (&symfile_complaints,
14789 _("location description stack overflow"));
14790 return 0;
14791 }
14792
14793 if (stacki <= 0)
14794 {
14795 complaint (&symfile_complaints,
14796 _("location description stack underflow"));
14797 return 0;
14798 }
14799 }
14800 return (stack[stacki]);
c906108c
SS
14801}
14802
14803/* memory allocation interface */
14804
c906108c 14805static struct dwarf_block *
7b5a2f43 14806dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14807{
14808 struct dwarf_block *blk;
14809
14810 blk = (struct dwarf_block *)
7b5a2f43 14811 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14812 return (blk);
14813}
14814
14815static struct abbrev_info *
f3dd6933 14816dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14817{
14818 struct abbrev_info *abbrev;
14819
f3dd6933
DJ
14820 abbrev = (struct abbrev_info *)
14821 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14822 memset (abbrev, 0, sizeof (struct abbrev_info));
14823 return (abbrev);
14824}
14825
14826static struct die_info *
b60c80d6 14827dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14828{
14829 struct die_info *die;
b60c80d6
DJ
14830 size_t size = sizeof (struct die_info);
14831
14832 if (num_attrs > 1)
14833 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14834
b60c80d6 14835 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14836 memset (die, 0, sizeof (struct die_info));
14837 return (die);
14838}
2e276125
JB
14839
14840\f
14841/* Macro support. */
14842
2e276125
JB
14843/* Return the full name of file number I in *LH's file name table.
14844 Use COMP_DIR as the name of the current directory of the
14845 compilation. The result is allocated using xmalloc; the caller is
14846 responsible for freeing it. */
14847static char *
14848file_full_name (int file, struct line_header *lh, const char *comp_dir)
14849{
6a83a1e6
EZ
14850 /* Is the file number a valid index into the line header's file name
14851 table? Remember that file numbers start with one, not zero. */
14852 if (1 <= file && file <= lh->num_file_names)
14853 {
14854 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14855
6a83a1e6
EZ
14856 if (IS_ABSOLUTE_PATH (fe->name))
14857 return xstrdup (fe->name);
14858 else
14859 {
14860 const char *dir;
14861 int dir_len;
14862 char *full_name;
14863
14864 if (fe->dir_index)
14865 dir = lh->include_dirs[fe->dir_index - 1];
14866 else
14867 dir = comp_dir;
14868
14869 if (dir)
14870 {
14871 dir_len = strlen (dir);
14872 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14873 strcpy (full_name, dir);
14874 full_name[dir_len] = '/';
14875 strcpy (full_name + dir_len + 1, fe->name);
14876 return full_name;
14877 }
14878 else
14879 return xstrdup (fe->name);
14880 }
14881 }
2e276125
JB
14882 else
14883 {
6a83a1e6
EZ
14884 /* The compiler produced a bogus file number. We can at least
14885 record the macro definitions made in the file, even if we
14886 won't be able to find the file by name. */
14887 char fake_name[80];
9a619af0 14888
6a83a1e6 14889 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14890
6e70227d 14891 complaint (&symfile_complaints,
6a83a1e6
EZ
14892 _("bad file number in macro information (%d)"),
14893 file);
2e276125 14894
6a83a1e6 14895 return xstrdup (fake_name);
2e276125
JB
14896 }
14897}
14898
14899
14900static struct macro_source_file *
14901macro_start_file (int file, int line,
14902 struct macro_source_file *current_file,
14903 const char *comp_dir,
14904 struct line_header *lh, struct objfile *objfile)
14905{
14906 /* The full name of this source file. */
14907 char *full_name = file_full_name (file, lh, comp_dir);
14908
14909 /* We don't create a macro table for this compilation unit
14910 at all until we actually get a filename. */
14911 if (! pending_macros)
4a146b47 14912 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14913 objfile->macro_cache);
2e276125
JB
14914
14915 if (! current_file)
14916 /* If we have no current file, then this must be the start_file
14917 directive for the compilation unit's main source file. */
14918 current_file = macro_set_main (pending_macros, full_name);
14919 else
14920 current_file = macro_include (current_file, line, full_name);
14921
14922 xfree (full_name);
6e70227d 14923
2e276125
JB
14924 return current_file;
14925}
14926
14927
14928/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14929 followed by a null byte. */
14930static char *
14931copy_string (const char *buf, int len)
14932{
14933 char *s = xmalloc (len + 1);
9a619af0 14934
2e276125
JB
14935 memcpy (s, buf, len);
14936 s[len] = '\0';
2e276125
JB
14937 return s;
14938}
14939
14940
14941static const char *
14942consume_improper_spaces (const char *p, const char *body)
14943{
14944 if (*p == ' ')
14945 {
4d3c2250 14946 complaint (&symfile_complaints,
3e43a32a
MS
14947 _("macro definition contains spaces "
14948 "in formal argument list:\n`%s'"),
4d3c2250 14949 body);
2e276125
JB
14950
14951 while (*p == ' ')
14952 p++;
14953 }
14954
14955 return p;
14956}
14957
14958
14959static void
14960parse_macro_definition (struct macro_source_file *file, int line,
14961 const char *body)
14962{
14963 const char *p;
14964
14965 /* The body string takes one of two forms. For object-like macro
14966 definitions, it should be:
14967
14968 <macro name> " " <definition>
14969
14970 For function-like macro definitions, it should be:
14971
14972 <macro name> "() " <definition>
14973 or
14974 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14975
14976 Spaces may appear only where explicitly indicated, and in the
14977 <definition>.
14978
14979 The Dwarf 2 spec says that an object-like macro's name is always
14980 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14981 the space when the macro's definition is the empty string.
2e276125
JB
14982
14983 The Dwarf 2 spec says that there should be no spaces between the
14984 formal arguments in a function-like macro's formal argument list,
14985 but versions of GCC around March 2002 include spaces after the
14986 commas. */
14987
14988
14989 /* Find the extent of the macro name. The macro name is terminated
14990 by either a space or null character (for an object-like macro) or
14991 an opening paren (for a function-like macro). */
14992 for (p = body; *p; p++)
14993 if (*p == ' ' || *p == '(')
14994 break;
14995
14996 if (*p == ' ' || *p == '\0')
14997 {
14998 /* It's an object-like macro. */
14999 int name_len = p - body;
15000 char *name = copy_string (body, name_len);
15001 const char *replacement;
15002
15003 if (*p == ' ')
15004 replacement = body + name_len + 1;
15005 else
15006 {
4d3c2250 15007 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15008 replacement = body + name_len;
15009 }
6e70227d 15010
2e276125
JB
15011 macro_define_object (file, line, name, replacement);
15012
15013 xfree (name);
15014 }
15015 else if (*p == '(')
15016 {
15017 /* It's a function-like macro. */
15018 char *name = copy_string (body, p - body);
15019 int argc = 0;
15020 int argv_size = 1;
15021 char **argv = xmalloc (argv_size * sizeof (*argv));
15022
15023 p++;
15024
15025 p = consume_improper_spaces (p, body);
15026
15027 /* Parse the formal argument list. */
15028 while (*p && *p != ')')
15029 {
15030 /* Find the extent of the current argument name. */
15031 const char *arg_start = p;
15032
15033 while (*p && *p != ',' && *p != ')' && *p != ' ')
15034 p++;
15035
15036 if (! *p || p == arg_start)
4d3c2250 15037 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15038 else
15039 {
15040 /* Make sure argv has room for the new argument. */
15041 if (argc >= argv_size)
15042 {
15043 argv_size *= 2;
15044 argv = xrealloc (argv, argv_size * sizeof (*argv));
15045 }
15046
15047 argv[argc++] = copy_string (arg_start, p - arg_start);
15048 }
15049
15050 p = consume_improper_spaces (p, body);
15051
15052 /* Consume the comma, if present. */
15053 if (*p == ',')
15054 {
15055 p++;
15056
15057 p = consume_improper_spaces (p, body);
15058 }
15059 }
15060
15061 if (*p == ')')
15062 {
15063 p++;
15064
15065 if (*p == ' ')
15066 /* Perfectly formed definition, no complaints. */
15067 macro_define_function (file, line, name,
6e70227d 15068 argc, (const char **) argv,
2e276125
JB
15069 p + 1);
15070 else if (*p == '\0')
15071 {
15072 /* Complain, but do define it. */
4d3c2250 15073 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15074 macro_define_function (file, line, name,
6e70227d 15075 argc, (const char **) argv,
2e276125
JB
15076 p);
15077 }
15078 else
15079 /* Just complain. */
4d3c2250 15080 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15081 }
15082 else
15083 /* Just complain. */
4d3c2250 15084 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15085
15086 xfree (name);
15087 {
15088 int i;
15089
15090 for (i = 0; i < argc; i++)
15091 xfree (argv[i]);
15092 }
15093 xfree (argv);
15094 }
15095 else
4d3c2250 15096 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15097}
15098
cf2c3c16
TT
15099/* Skip some bytes from BYTES according to the form given in FORM.
15100 Returns the new pointer. */
2e276125 15101
cf2c3c16
TT
15102static gdb_byte *
15103skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15104 enum dwarf_form form,
15105 unsigned int offset_size,
15106 struct dwarf2_section_info *section)
2e276125 15107{
cf2c3c16 15108 unsigned int bytes_read;
2e276125 15109
cf2c3c16 15110 switch (form)
2e276125 15111 {
cf2c3c16
TT
15112 case DW_FORM_data1:
15113 case DW_FORM_flag:
15114 ++bytes;
15115 break;
15116
15117 case DW_FORM_data2:
15118 bytes += 2;
15119 break;
15120
15121 case DW_FORM_data4:
15122 bytes += 4;
15123 break;
15124
15125 case DW_FORM_data8:
15126 bytes += 8;
15127 break;
15128
15129 case DW_FORM_string:
15130 read_direct_string (abfd, bytes, &bytes_read);
15131 bytes += bytes_read;
15132 break;
15133
15134 case DW_FORM_sec_offset:
15135 case DW_FORM_strp:
15136 bytes += offset_size;
15137 break;
15138
15139 case DW_FORM_block:
15140 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15141 bytes += bytes_read;
15142 break;
15143
15144 case DW_FORM_block1:
15145 bytes += 1 + read_1_byte (abfd, bytes);
15146 break;
15147 case DW_FORM_block2:
15148 bytes += 2 + read_2_bytes (abfd, bytes);
15149 break;
15150 case DW_FORM_block4:
15151 bytes += 4 + read_4_bytes (abfd, bytes);
15152 break;
15153
15154 case DW_FORM_sdata:
15155 case DW_FORM_udata:
15156 bytes = skip_leb128 (abfd, bytes);
15157 break;
15158
15159 default:
15160 {
15161 complain:
15162 complaint (&symfile_complaints,
15163 _("invalid form 0x%x in `%s'"),
15164 form,
15165 section->asection->name);
15166 return NULL;
15167 }
2e276125
JB
15168 }
15169
cf2c3c16
TT
15170 return bytes;
15171}
757a13d0 15172
cf2c3c16
TT
15173/* A helper for dwarf_decode_macros that handles skipping an unknown
15174 opcode. Returns an updated pointer to the macro data buffer; or,
15175 on error, issues a complaint and returns NULL. */
757a13d0 15176
cf2c3c16
TT
15177static gdb_byte *
15178skip_unknown_opcode (unsigned int opcode,
15179 gdb_byte **opcode_definitions,
15180 gdb_byte *mac_ptr,
15181 bfd *abfd,
15182 unsigned int offset_size,
15183 struct dwarf2_section_info *section)
15184{
15185 unsigned int bytes_read, i;
15186 unsigned long arg;
15187 gdb_byte *defn;
2e276125 15188
cf2c3c16 15189 if (opcode_definitions[opcode] == NULL)
2e276125 15190 {
cf2c3c16
TT
15191 complaint (&symfile_complaints,
15192 _("unrecognized DW_MACFINO opcode 0x%x"),
15193 opcode);
15194 return NULL;
15195 }
2e276125 15196
cf2c3c16
TT
15197 defn = opcode_definitions[opcode];
15198 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15199 defn += bytes_read;
2e276125 15200
cf2c3c16
TT
15201 for (i = 0; i < arg; ++i)
15202 {
15203 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15204 if (mac_ptr == NULL)
15205 {
15206 /* skip_form_bytes already issued the complaint. */
15207 return NULL;
15208 }
15209 }
757a13d0 15210
cf2c3c16
TT
15211 return mac_ptr;
15212}
757a13d0 15213
cf2c3c16
TT
15214/* A helper function which parses the header of a macro section.
15215 If the macro section is the extended (for now called "GNU") type,
15216 then this updates *OFFSET_SIZE. Returns a pointer to just after
15217 the header, or issues a complaint and returns NULL on error. */
757a13d0 15218
cf2c3c16
TT
15219static gdb_byte *
15220dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15221 bfd *abfd,
15222 gdb_byte *mac_ptr,
15223 unsigned int *offset_size,
15224 int section_is_gnu)
15225{
15226 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15227
cf2c3c16
TT
15228 if (section_is_gnu)
15229 {
15230 unsigned int version, flags;
757a13d0 15231
cf2c3c16
TT
15232 version = read_2_bytes (abfd, mac_ptr);
15233 if (version != 4)
15234 {
15235 complaint (&symfile_complaints,
15236 _("unrecognized version `%d' in .debug_macro section"),
15237 version);
15238 return NULL;
15239 }
15240 mac_ptr += 2;
757a13d0 15241
cf2c3c16
TT
15242 flags = read_1_byte (abfd, mac_ptr);
15243 ++mac_ptr;
15244 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15245
cf2c3c16
TT
15246 if ((flags & 2) != 0)
15247 /* We don't need the line table offset. */
15248 mac_ptr += *offset_size;
757a13d0 15249
cf2c3c16
TT
15250 /* Vendor opcode descriptions. */
15251 if ((flags & 4) != 0)
15252 {
15253 unsigned int i, count;
757a13d0 15254
cf2c3c16
TT
15255 count = read_1_byte (abfd, mac_ptr);
15256 ++mac_ptr;
15257 for (i = 0; i < count; ++i)
15258 {
15259 unsigned int opcode, bytes_read;
15260 unsigned long arg;
15261
15262 opcode = read_1_byte (abfd, mac_ptr);
15263 ++mac_ptr;
15264 opcode_definitions[opcode] = mac_ptr;
15265 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15266 mac_ptr += bytes_read;
15267 mac_ptr += arg;
15268 }
757a13d0 15269 }
cf2c3c16 15270 }
757a13d0 15271
cf2c3c16
TT
15272 return mac_ptr;
15273}
757a13d0 15274
cf2c3c16 15275/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15276 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15277
15278static void
15279dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15280 struct macro_source_file *current_file,
15281 struct line_header *lh, char *comp_dir,
15282 struct dwarf2_section_info *section,
15283 int section_is_gnu,
15284 unsigned int offset_size,
8fc3fc34
TT
15285 struct objfile *objfile,
15286 htab_t include_hash)
cf2c3c16
TT
15287{
15288 enum dwarf_macro_record_type macinfo_type;
15289 int at_commandline;
15290 gdb_byte *opcode_definitions[256];
757a13d0 15291
cf2c3c16
TT
15292 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15293 &offset_size, section_is_gnu);
15294 if (mac_ptr == NULL)
15295 {
15296 /* We already issued a complaint. */
15297 return;
15298 }
757a13d0
JK
15299
15300 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15301 GDB is still reading the definitions from command line. First
15302 DW_MACINFO_start_file will need to be ignored as it was already executed
15303 to create CURRENT_FILE for the main source holding also the command line
15304 definitions. On first met DW_MACINFO_start_file this flag is reset to
15305 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15306
15307 at_commandline = 1;
15308
15309 do
15310 {
15311 /* Do we at least have room for a macinfo type byte? */
15312 if (mac_ptr >= mac_end)
15313 {
cf2c3c16 15314 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15315 break;
15316 }
15317
15318 macinfo_type = read_1_byte (abfd, mac_ptr);
15319 mac_ptr++;
15320
cf2c3c16
TT
15321 /* Note that we rely on the fact that the corresponding GNU and
15322 DWARF constants are the same. */
757a13d0
JK
15323 switch (macinfo_type)
15324 {
15325 /* A zero macinfo type indicates the end of the macro
15326 information. */
15327 case 0:
15328 break;
2e276125 15329
cf2c3c16
TT
15330 case DW_MACRO_GNU_define:
15331 case DW_MACRO_GNU_undef:
15332 case DW_MACRO_GNU_define_indirect:
15333 case DW_MACRO_GNU_undef_indirect:
2e276125 15334 {
891d2f0b 15335 unsigned int bytes_read;
2e276125
JB
15336 int line;
15337 char *body;
cf2c3c16 15338 int is_define;
2e276125 15339
cf2c3c16
TT
15340 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15341 mac_ptr += bytes_read;
15342
15343 if (macinfo_type == DW_MACRO_GNU_define
15344 || macinfo_type == DW_MACRO_GNU_undef)
15345 {
15346 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15347 mac_ptr += bytes_read;
15348 }
15349 else
15350 {
15351 LONGEST str_offset;
15352
15353 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15354 mac_ptr += offset_size;
2e276125 15355
cf2c3c16
TT
15356 body = read_indirect_string_at_offset (abfd, str_offset);
15357 }
15358
15359 is_define = (macinfo_type == DW_MACRO_GNU_define
15360 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15361 if (! current_file)
757a13d0
JK
15362 {
15363 /* DWARF violation as no main source is present. */
15364 complaint (&symfile_complaints,
15365 _("debug info with no main source gives macro %s "
15366 "on line %d: %s"),
cf2c3c16
TT
15367 is_define ? _("definition") : _("undefinition"),
15368 line, body);
757a13d0
JK
15369 break;
15370 }
3e43a32a
MS
15371 if ((line == 0 && !at_commandline)
15372 || (line != 0 && at_commandline))
4d3c2250 15373 complaint (&symfile_complaints,
757a13d0
JK
15374 _("debug info gives %s macro %s with %s line %d: %s"),
15375 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15376 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15377 line == 0 ? _("zero") : _("non-zero"), line, body);
15378
cf2c3c16 15379 if (is_define)
757a13d0 15380 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15381 else
15382 {
15383 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15384 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15385 macro_undef (current_file, line, body);
15386 }
2e276125
JB
15387 }
15388 break;
15389
cf2c3c16 15390 case DW_MACRO_GNU_start_file:
2e276125 15391 {
891d2f0b 15392 unsigned int bytes_read;
2e276125
JB
15393 int line, file;
15394
15395 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15396 mac_ptr += bytes_read;
15397 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15398 mac_ptr += bytes_read;
15399
3e43a32a
MS
15400 if ((line == 0 && !at_commandline)
15401 || (line != 0 && at_commandline))
757a13d0
JK
15402 complaint (&symfile_complaints,
15403 _("debug info gives source %d included "
15404 "from %s at %s line %d"),
15405 file, at_commandline ? _("command-line") : _("file"),
15406 line == 0 ? _("zero") : _("non-zero"), line);
15407
15408 if (at_commandline)
15409 {
cf2c3c16
TT
15410 /* This DW_MACRO_GNU_start_file was executed in the
15411 pass one. */
757a13d0
JK
15412 at_commandline = 0;
15413 }
15414 else
15415 current_file = macro_start_file (file, line,
15416 current_file, comp_dir,
cf2c3c16 15417 lh, objfile);
2e276125
JB
15418 }
15419 break;
15420
cf2c3c16 15421 case DW_MACRO_GNU_end_file:
2e276125 15422 if (! current_file)
4d3c2250 15423 complaint (&symfile_complaints,
3e43a32a
MS
15424 _("macro debug info has an unmatched "
15425 "`close_file' directive"));
2e276125
JB
15426 else
15427 {
15428 current_file = current_file->included_by;
15429 if (! current_file)
15430 {
cf2c3c16 15431 enum dwarf_macro_record_type next_type;
2e276125
JB
15432
15433 /* GCC circa March 2002 doesn't produce the zero
15434 type byte marking the end of the compilation
15435 unit. Complain if it's not there, but exit no
15436 matter what. */
15437
15438 /* Do we at least have room for a macinfo type byte? */
15439 if (mac_ptr >= mac_end)
15440 {
cf2c3c16 15441 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15442 return;
15443 }
15444
15445 /* We don't increment mac_ptr here, so this is just
15446 a look-ahead. */
15447 next_type = read_1_byte (abfd, mac_ptr);
15448 if (next_type != 0)
4d3c2250 15449 complaint (&symfile_complaints,
3e43a32a
MS
15450 _("no terminating 0-type entry for "
15451 "macros in `.debug_macinfo' section"));
2e276125
JB
15452
15453 return;
15454 }
15455 }
15456 break;
15457
cf2c3c16
TT
15458 case DW_MACRO_GNU_transparent_include:
15459 {
15460 LONGEST offset;
8fc3fc34 15461 void **slot;
cf2c3c16
TT
15462
15463 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15464 mac_ptr += offset_size;
15465
8fc3fc34
TT
15466 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15467 if (*slot != NULL)
15468 {
15469 /* This has actually happened; see
15470 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15471 complaint (&symfile_complaints,
15472 _("recursive DW_MACRO_GNU_transparent_include in "
15473 ".debug_macro section"));
15474 }
15475 else
15476 {
15477 *slot = mac_ptr;
15478
15479 dwarf_decode_macro_bytes (abfd,
15480 section->buffer + offset,
15481 mac_end, current_file,
15482 lh, comp_dir,
15483 section, section_is_gnu,
15484 offset_size, objfile, include_hash);
15485
15486 htab_remove_elt (include_hash, mac_ptr);
15487 }
cf2c3c16
TT
15488 }
15489 break;
15490
2e276125 15491 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15492 if (!section_is_gnu)
15493 {
15494 unsigned int bytes_read;
15495 int constant;
2e276125 15496
cf2c3c16
TT
15497 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15498 mac_ptr += bytes_read;
15499 read_direct_string (abfd, mac_ptr, &bytes_read);
15500 mac_ptr += bytes_read;
2e276125 15501
cf2c3c16
TT
15502 /* We don't recognize any vendor extensions. */
15503 break;
15504 }
15505 /* FALLTHROUGH */
15506
15507 default:
15508 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15509 mac_ptr, abfd, offset_size,
15510 section);
15511 if (mac_ptr == NULL)
15512 return;
15513 break;
2e276125 15514 }
757a13d0 15515 } while (macinfo_type != 0);
2e276125 15516}
8e19ed76 15517
cf2c3c16
TT
15518static void
15519dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15520 char *comp_dir, bfd *abfd,
15521 struct dwarf2_cu *cu,
15522 struct dwarf2_section_info *section,
15523 int section_is_gnu)
15524{
bb5ed363 15525 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15526 gdb_byte *mac_ptr, *mac_end;
15527 struct macro_source_file *current_file = 0;
15528 enum dwarf_macro_record_type macinfo_type;
15529 unsigned int offset_size = cu->header.offset_size;
15530 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15531 struct cleanup *cleanup;
15532 htab_t include_hash;
15533 void **slot;
cf2c3c16 15534
bb5ed363 15535 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15536 if (section->buffer == NULL)
15537 {
15538 complaint (&symfile_complaints, _("missing %s section"),
15539 section->asection->name);
15540 return;
15541 }
15542
15543 /* First pass: Find the name of the base filename.
15544 This filename is needed in order to process all macros whose definition
15545 (or undefinition) comes from the command line. These macros are defined
15546 before the first DW_MACINFO_start_file entry, and yet still need to be
15547 associated to the base file.
15548
15549 To determine the base file name, we scan the macro definitions until we
15550 reach the first DW_MACINFO_start_file entry. We then initialize
15551 CURRENT_FILE accordingly so that any macro definition found before the
15552 first DW_MACINFO_start_file can still be associated to the base file. */
15553
15554 mac_ptr = section->buffer + offset;
15555 mac_end = section->buffer + section->size;
15556
15557 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15558 &offset_size, section_is_gnu);
15559 if (mac_ptr == NULL)
15560 {
15561 /* We already issued a complaint. */
15562 return;
15563 }
15564
15565 do
15566 {
15567 /* Do we at least have room for a macinfo type byte? */
15568 if (mac_ptr >= mac_end)
15569 {
15570 /* Complaint is printed during the second pass as GDB will probably
15571 stop the first pass earlier upon finding
15572 DW_MACINFO_start_file. */
15573 break;
15574 }
15575
15576 macinfo_type = read_1_byte (abfd, mac_ptr);
15577 mac_ptr++;
15578
15579 /* Note that we rely on the fact that the corresponding GNU and
15580 DWARF constants are the same. */
15581 switch (macinfo_type)
15582 {
15583 /* A zero macinfo type indicates the end of the macro
15584 information. */
15585 case 0:
15586 break;
15587
15588 case DW_MACRO_GNU_define:
15589 case DW_MACRO_GNU_undef:
15590 /* Only skip the data by MAC_PTR. */
15591 {
15592 unsigned int bytes_read;
15593
15594 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15595 mac_ptr += bytes_read;
15596 read_direct_string (abfd, mac_ptr, &bytes_read);
15597 mac_ptr += bytes_read;
15598 }
15599 break;
15600
15601 case DW_MACRO_GNU_start_file:
15602 {
15603 unsigned int bytes_read;
15604 int line, file;
15605
15606 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15607 mac_ptr += bytes_read;
15608 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15609 mac_ptr += bytes_read;
15610
15611 current_file = macro_start_file (file, line, current_file,
bb5ed363 15612 comp_dir, lh, objfile);
cf2c3c16
TT
15613 }
15614 break;
15615
15616 case DW_MACRO_GNU_end_file:
15617 /* No data to skip by MAC_PTR. */
15618 break;
15619
15620 case DW_MACRO_GNU_define_indirect:
15621 case DW_MACRO_GNU_undef_indirect:
15622 {
15623 unsigned int bytes_read;
15624
15625 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15626 mac_ptr += bytes_read;
15627 mac_ptr += offset_size;
15628 }
15629 break;
15630
15631 case DW_MACRO_GNU_transparent_include:
15632 /* Note that, according to the spec, a transparent include
15633 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15634 skip this opcode. */
15635 mac_ptr += offset_size;
15636 break;
15637
15638 case DW_MACINFO_vendor_ext:
15639 /* Only skip the data by MAC_PTR. */
15640 if (!section_is_gnu)
15641 {
15642 unsigned int bytes_read;
15643
15644 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15645 mac_ptr += bytes_read;
15646 read_direct_string (abfd, mac_ptr, &bytes_read);
15647 mac_ptr += bytes_read;
15648 }
15649 /* FALLTHROUGH */
15650
15651 default:
15652 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15653 mac_ptr, abfd, offset_size,
15654 section);
15655 if (mac_ptr == NULL)
15656 return;
15657 break;
15658 }
15659 } while (macinfo_type != 0 && current_file == NULL);
15660
15661 /* Second pass: Process all entries.
15662
15663 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15664 command-line macro definitions/undefinitions. This flag is unset when we
15665 reach the first DW_MACINFO_start_file entry. */
15666
8fc3fc34
TT
15667 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15668 NULL, xcalloc, xfree);
15669 cleanup = make_cleanup_htab_delete (include_hash);
15670 mac_ptr = section->buffer + offset;
15671 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15672 *slot = mac_ptr;
15673 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 15674 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
15675 offset_size, objfile, include_hash);
15676 do_cleanups (cleanup);
cf2c3c16
TT
15677}
15678
8e19ed76 15679/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15680 if so return true else false. */
380bca97 15681
8e19ed76
PS
15682static int
15683attr_form_is_block (struct attribute *attr)
15684{
15685 return (attr == NULL ? 0 :
15686 attr->form == DW_FORM_block1
15687 || attr->form == DW_FORM_block2
15688 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15689 || attr->form == DW_FORM_block
15690 || attr->form == DW_FORM_exprloc);
8e19ed76 15691}
4c2df51b 15692
c6a0999f
JB
15693/* Return non-zero if ATTR's value is a section offset --- classes
15694 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15695 You may use DW_UNSND (attr) to retrieve such offsets.
15696
15697 Section 7.5.4, "Attribute Encodings", explains that no attribute
15698 may have a value that belongs to more than one of these classes; it
15699 would be ambiguous if we did, because we use the same forms for all
15700 of them. */
380bca97 15701
3690dd37
JB
15702static int
15703attr_form_is_section_offset (struct attribute *attr)
15704{
15705 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15706 || attr->form == DW_FORM_data8
15707 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15708}
15709
15710
15711/* Return non-zero if ATTR's value falls in the 'constant' class, or
15712 zero otherwise. When this function returns true, you can apply
15713 dwarf2_get_attr_constant_value to it.
15714
15715 However, note that for some attributes you must check
15716 attr_form_is_section_offset before using this test. DW_FORM_data4
15717 and DW_FORM_data8 are members of both the constant class, and of
15718 the classes that contain offsets into other debug sections
15719 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15720 that, if an attribute's can be either a constant or one of the
15721 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15722 taken as section offsets, not constants. */
380bca97 15723
3690dd37
JB
15724static int
15725attr_form_is_constant (struct attribute *attr)
15726{
15727 switch (attr->form)
15728 {
15729 case DW_FORM_sdata:
15730 case DW_FORM_udata:
15731 case DW_FORM_data1:
15732 case DW_FORM_data2:
15733 case DW_FORM_data4:
15734 case DW_FORM_data8:
15735 return 1;
15736 default:
15737 return 0;
15738 }
15739}
15740
8cf6f0b1
TT
15741/* A helper function that fills in a dwarf2_loclist_baton. */
15742
15743static void
15744fill_in_loclist_baton (struct dwarf2_cu *cu,
15745 struct dwarf2_loclist_baton *baton,
15746 struct attribute *attr)
15747{
15748 dwarf2_read_section (dwarf2_per_objfile->objfile,
15749 &dwarf2_per_objfile->loc);
15750
15751 baton->per_cu = cu->per_cu;
15752 gdb_assert (baton->per_cu);
15753 /* We don't know how long the location list is, but make sure we
15754 don't run off the edge of the section. */
15755 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15756 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15757 baton->base_address = cu->base_address;
15758}
15759
4c2df51b
DJ
15760static void
15761dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15762 struct dwarf2_cu *cu)
4c2df51b 15763{
bb5ed363
DE
15764 struct objfile *objfile = dwarf2_per_objfile->objfile;
15765
3690dd37 15766 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15767 /* ".debug_loc" may not exist at all, or the offset may be outside
15768 the section. If so, fall through to the complaint in the
15769 other branch. */
bb5ed363 15770 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15771 &dwarf2_per_objfile->loc))
4c2df51b 15772 {
0d53c4c4 15773 struct dwarf2_loclist_baton *baton;
4c2df51b 15774
bb5ed363 15775 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15776 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15777
8cf6f0b1 15778 fill_in_loclist_baton (cu, baton, attr);
be391dca 15779
d00adf39 15780 if (cu->base_known == 0)
0d53c4c4 15781 complaint (&symfile_complaints,
3e43a32a
MS
15782 _("Location list used without "
15783 "specifying the CU base address."));
4c2df51b 15784
768a979c 15785 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15786 SYMBOL_LOCATION_BATON (sym) = baton;
15787 }
15788 else
15789 {
15790 struct dwarf2_locexpr_baton *baton;
15791
bb5ed363 15792 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15793 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15794 baton->per_cu = cu->per_cu;
15795 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15796
15797 if (attr_form_is_block (attr))
15798 {
15799 /* Note that we're just copying the block's data pointer
15800 here, not the actual data. We're still pointing into the
6502dd73
DJ
15801 info_buffer for SYM's objfile; right now we never release
15802 that buffer, but when we do clean up properly this may
15803 need to change. */
0d53c4c4
DJ
15804 baton->size = DW_BLOCK (attr)->size;
15805 baton->data = DW_BLOCK (attr)->data;
15806 }
15807 else
15808 {
15809 dwarf2_invalid_attrib_class_complaint ("location description",
15810 SYMBOL_NATURAL_NAME (sym));
15811 baton->size = 0;
0d53c4c4 15812 }
6e70227d 15813
768a979c 15814 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15815 SYMBOL_LOCATION_BATON (sym) = baton;
15816 }
4c2df51b 15817}
6502dd73 15818
9aa1f1e3
TT
15819/* Return the OBJFILE associated with the compilation unit CU. If CU
15820 came from a separate debuginfo file, then the master objfile is
15821 returned. */
ae0d2f24
UW
15822
15823struct objfile *
15824dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15825{
9291a0cd 15826 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15827
15828 /* Return the master objfile, so that we can report and look up the
15829 correct file containing this variable. */
15830 if (objfile->separate_debug_objfile_backlink)
15831 objfile = objfile->separate_debug_objfile_backlink;
15832
15833 return objfile;
15834}
15835
96408a79
SA
15836/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15837 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15838 CU_HEADERP first. */
15839
15840static const struct comp_unit_head *
15841per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15842 struct dwarf2_per_cu_data *per_cu)
15843{
15844 struct objfile *objfile;
15845 struct dwarf2_per_objfile *per_objfile;
15846 gdb_byte *info_ptr;
15847
15848 if (per_cu->cu)
15849 return &per_cu->cu->header;
15850
15851 objfile = per_cu->objfile;
15852 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 15853 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
15854
15855 memset (cu_headerp, 0, sizeof (*cu_headerp));
15856 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15857
15858 return cu_headerp;
15859}
15860
ae0d2f24
UW
15861/* Return the address size given in the compilation unit header for CU. */
15862
98714339 15863int
ae0d2f24
UW
15864dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15865{
96408a79
SA
15866 struct comp_unit_head cu_header_local;
15867 const struct comp_unit_head *cu_headerp;
c471e790 15868
96408a79
SA
15869 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15870
15871 return cu_headerp->addr_size;
ae0d2f24
UW
15872}
15873
9eae7c52
TT
15874/* Return the offset size given in the compilation unit header for CU. */
15875
15876int
15877dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15878{
96408a79
SA
15879 struct comp_unit_head cu_header_local;
15880 const struct comp_unit_head *cu_headerp;
9c6c53f7 15881
96408a79
SA
15882 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15883
15884 return cu_headerp->offset_size;
15885}
15886
15887/* See its dwarf2loc.h declaration. */
15888
15889int
15890dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15891{
15892 struct comp_unit_head cu_header_local;
15893 const struct comp_unit_head *cu_headerp;
15894
15895 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15896
15897 if (cu_headerp->version == 2)
15898 return cu_headerp->addr_size;
15899 else
15900 return cu_headerp->offset_size;
181cebd4
JK
15901}
15902
9aa1f1e3
TT
15903/* Return the text offset of the CU. The returned offset comes from
15904 this CU's objfile. If this objfile came from a separate debuginfo
15905 file, then the offset may be different from the corresponding
15906 offset in the parent objfile. */
15907
15908CORE_ADDR
15909dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15910{
bb3fa9d0 15911 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15912
15913 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15914}
15915
348e048f
DE
15916/* Locate the .debug_info compilation unit from CU's objfile which contains
15917 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15918
15919static struct dwarf2_per_cu_data *
b64f50a1 15920dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
15921 struct objfile *objfile)
15922{
15923 struct dwarf2_per_cu_data *this_cu;
15924 int low, high;
15925
ae038cb0
DJ
15926 low = 0;
15927 high = dwarf2_per_objfile->n_comp_units - 1;
15928 while (high > low)
15929 {
15930 int mid = low + (high - low) / 2;
9a619af0 15931
b64f50a1
JK
15932 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
15933 >= offset.sect_off)
ae038cb0
DJ
15934 high = mid;
15935 else
15936 low = mid + 1;
15937 }
15938 gdb_assert (low == high);
b64f50a1
JK
15939 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
15940 > offset.sect_off)
ae038cb0 15941 {
10b3939b 15942 if (low == 0)
8a3fe4f8
AC
15943 error (_("Dwarf Error: could not find partial DIE containing "
15944 "offset 0x%lx [in module %s]"),
b64f50a1 15945 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 15946
b64f50a1
JK
15947 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
15948 <= offset.sect_off);
ae038cb0
DJ
15949 return dwarf2_per_objfile->all_comp_units[low-1];
15950 }
15951 else
15952 {
15953 this_cu = dwarf2_per_objfile->all_comp_units[low];
15954 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
15955 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
15956 error (_("invalid dwarf2 offset %u"), offset.sect_off);
15957 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
15958 return this_cu;
15959 }
15960}
15961
23745b47 15962/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15963
9816fde3 15964static void
23745b47 15965init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15966{
9816fde3 15967 memset (cu, 0, sizeof (*cu));
23745b47
DE
15968 per_cu->cu = cu;
15969 cu->per_cu = per_cu;
15970 cu->objfile = per_cu->objfile;
93311388 15971 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15972}
15973
15974/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15975
15976static void
15977prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15978{
15979 struct attribute *attr;
15980
15981 /* Set the language we're debugging. */
15982 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15983 if (attr)
15984 set_cu_language (DW_UNSND (attr), cu);
15985 else
9cded63f
TT
15986 {
15987 cu->language = language_minimal;
15988 cu->language_defn = language_def (cu->language);
15989 }
93311388
DE
15990}
15991
ae038cb0
DJ
15992/* Release one cached compilation unit, CU. We unlink it from the tree
15993 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15994 the caller is responsible for that.
15995 NOTE: DATA is a void * because this function is also used as a
15996 cleanup routine. */
ae038cb0
DJ
15997
15998static void
68dc6402 15999free_heap_comp_unit (void *data)
ae038cb0
DJ
16000{
16001 struct dwarf2_cu *cu = data;
16002
23745b47
DE
16003 gdb_assert (cu->per_cu != NULL);
16004 cu->per_cu->cu = NULL;
ae038cb0
DJ
16005 cu->per_cu = NULL;
16006
16007 obstack_free (&cu->comp_unit_obstack, NULL);
16008
16009 xfree (cu);
16010}
16011
72bf9492 16012/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
16013 when we're finished with it. We can't free the pointer itself, but be
16014 sure to unlink it from the cache. Also release any associated storage
16015 and perform cache maintenance.
72bf9492
DJ
16016
16017 Only used during partial symbol parsing. */
16018
16019static void
16020free_stack_comp_unit (void *data)
16021{
16022 struct dwarf2_cu *cu = data;
16023
23745b47
DE
16024 gdb_assert (cu->per_cu != NULL);
16025 cu->per_cu->cu = NULL;
16026 cu->per_cu = NULL;
16027
72bf9492
DJ
16028 obstack_free (&cu->comp_unit_obstack, NULL);
16029 cu->partial_dies = NULL;
ae038cb0 16030
23745b47
DE
16031 /* The previous code only did this if per_cu != NULL.
16032 But that would always succeed, so now we just unconditionally do
16033 the aging. This seems like the wrong place to do such aging,
16034 but cleaning that up is left for later. */
16035 age_cached_comp_units ();
ae038cb0
DJ
16036}
16037
16038/* Free all cached compilation units. */
16039
16040static void
16041free_cached_comp_units (void *data)
16042{
16043 struct dwarf2_per_cu_data *per_cu, **last_chain;
16044
16045 per_cu = dwarf2_per_objfile->read_in_chain;
16046 last_chain = &dwarf2_per_objfile->read_in_chain;
16047 while (per_cu != NULL)
16048 {
16049 struct dwarf2_per_cu_data *next_cu;
16050
16051 next_cu = per_cu->cu->read_in_chain;
16052
68dc6402 16053 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16054 *last_chain = next_cu;
16055
16056 per_cu = next_cu;
16057 }
16058}
16059
16060/* Increase the age counter on each cached compilation unit, and free
16061 any that are too old. */
16062
16063static void
16064age_cached_comp_units (void)
16065{
16066 struct dwarf2_per_cu_data *per_cu, **last_chain;
16067
16068 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16069 per_cu = dwarf2_per_objfile->read_in_chain;
16070 while (per_cu != NULL)
16071 {
16072 per_cu->cu->last_used ++;
16073 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16074 dwarf2_mark (per_cu->cu);
16075 per_cu = per_cu->cu->read_in_chain;
16076 }
16077
16078 per_cu = dwarf2_per_objfile->read_in_chain;
16079 last_chain = &dwarf2_per_objfile->read_in_chain;
16080 while (per_cu != NULL)
16081 {
16082 struct dwarf2_per_cu_data *next_cu;
16083
16084 next_cu = per_cu->cu->read_in_chain;
16085
16086 if (!per_cu->cu->mark)
16087 {
68dc6402 16088 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16089 *last_chain = next_cu;
16090 }
16091 else
16092 last_chain = &per_cu->cu->read_in_chain;
16093
16094 per_cu = next_cu;
16095 }
16096}
16097
16098/* Remove a single compilation unit from the cache. */
16099
16100static void
16101free_one_cached_comp_unit (void *target_cu)
16102{
16103 struct dwarf2_per_cu_data *per_cu, **last_chain;
16104
16105 per_cu = dwarf2_per_objfile->read_in_chain;
16106 last_chain = &dwarf2_per_objfile->read_in_chain;
16107 while (per_cu != NULL)
16108 {
16109 struct dwarf2_per_cu_data *next_cu;
16110
16111 next_cu = per_cu->cu->read_in_chain;
16112
16113 if (per_cu->cu == target_cu)
16114 {
68dc6402 16115 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16116 *last_chain = next_cu;
16117 break;
16118 }
16119 else
16120 last_chain = &per_cu->cu->read_in_chain;
16121
16122 per_cu = next_cu;
16123 }
16124}
16125
fe3e1990
DJ
16126/* Release all extra memory associated with OBJFILE. */
16127
16128void
16129dwarf2_free_objfile (struct objfile *objfile)
16130{
16131 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16132
16133 if (dwarf2_per_objfile == NULL)
16134 return;
16135
16136 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16137 free_cached_comp_units (NULL);
16138
7b9f3c50
DE
16139 if (dwarf2_per_objfile->quick_file_names_table)
16140 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16141
fe3e1990
DJ
16142 /* Everything else should be on the objfile obstack. */
16143}
16144
1c379e20
DJ
16145/* A pair of DIE offset and GDB type pointer. We store these
16146 in a hash table separate from the DIEs, and preserve them
16147 when the DIEs are flushed out of cache. */
16148
16149struct dwarf2_offset_and_type
16150{
b64f50a1 16151 sect_offset offset;
1c379e20
DJ
16152 struct type *type;
16153};
16154
16155/* Hash function for a dwarf2_offset_and_type. */
16156
16157static hashval_t
16158offset_and_type_hash (const void *item)
16159{
16160 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16161
b64f50a1 16162 return ofs->offset.sect_off;
1c379e20
DJ
16163}
16164
16165/* Equality function for a dwarf2_offset_and_type. */
16166
16167static int
16168offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16169{
16170 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16171 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16172
b64f50a1 16173 return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
1c379e20
DJ
16174}
16175
16176/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16177 table if necessary. For convenience, return TYPE.
16178
16179 The DIEs reading must have careful ordering to:
16180 * Not cause infite loops trying to read in DIEs as a prerequisite for
16181 reading current DIE.
16182 * Not trying to dereference contents of still incompletely read in types
16183 while reading in other DIEs.
16184 * Enable referencing still incompletely read in types just by a pointer to
16185 the type without accessing its fields.
16186
16187 Therefore caller should follow these rules:
16188 * Try to fetch any prerequisite types we may need to build this DIE type
16189 before building the type and calling set_die_type.
e71ec853 16190 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16191 possible before fetching more types to complete the current type.
16192 * Make the type as complete as possible before fetching more types. */
1c379e20 16193
f792889a 16194static struct type *
1c379e20
DJ
16195set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16196{
16197 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16198 struct objfile *objfile = cu->objfile;
16199 htab_t *type_hash_ptr;
1c379e20 16200
b4ba55a1
JB
16201 /* For Ada types, make sure that the gnat-specific data is always
16202 initialized (if not already set). There are a few types where
16203 we should not be doing so, because the type-specific area is
16204 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16205 where the type-specific area is used to store the floatformat).
16206 But this is not a problem, because the gnat-specific information
16207 is actually not needed for these types. */
16208 if (need_gnat_info (cu)
16209 && TYPE_CODE (type) != TYPE_CODE_FUNC
16210 && TYPE_CODE (type) != TYPE_CODE_FLT
16211 && !HAVE_GNAT_AUX_INFO (type))
16212 INIT_GNAT_SPECIFIC (type);
16213
b0df02fd 16214 if (cu->per_cu->debug_types_section)
673bfd45
DE
16215 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16216 else
16217 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16218
16219 if (*type_hash_ptr == NULL)
f792889a 16220 {
673bfd45
DE
16221 *type_hash_ptr
16222 = htab_create_alloc_ex (127,
f792889a
DJ
16223 offset_and_type_hash,
16224 offset_and_type_eq,
16225 NULL,
673bfd45 16226 &objfile->objfile_obstack,
f792889a
DJ
16227 hashtab_obstack_allocate,
16228 dummy_obstack_deallocate);
f792889a 16229 }
1c379e20
DJ
16230
16231 ofs.offset = die->offset;
16232 ofs.type = type;
16233 slot = (struct dwarf2_offset_and_type **)
b64f50a1
JK
16234 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16235 INSERT);
7e314c57
JK
16236 if (*slot)
16237 complaint (&symfile_complaints,
16238 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16239 die->offset.sect_off);
673bfd45 16240 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16241 **slot = ofs;
f792889a 16242 return type;
1c379e20
DJ
16243}
16244
380bca97 16245/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16246 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16247
16248static struct type *
b64f50a1 16249get_die_type_at_offset (sect_offset offset,
673bfd45 16250 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16251{
16252 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16253 htab_t type_hash;
f792889a 16254
b0df02fd 16255 if (per_cu->debug_types_section)
673bfd45
DE
16256 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16257 else
16258 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16259 if (type_hash == NULL)
16260 return NULL;
1c379e20 16261
673bfd45 16262 ofs.offset = offset;
b64f50a1 16263 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
1c379e20
DJ
16264 if (slot)
16265 return slot->type;
16266 else
16267 return NULL;
16268}
16269
673bfd45
DE
16270/* Look up the type for DIE in the appropriate type_hash table,
16271 or return NULL if DIE does not have a saved type. */
16272
16273static struct type *
16274get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16275{
16276 return get_die_type_at_offset (die->offset, cu->per_cu);
16277}
16278
10b3939b
DJ
16279/* Add a dependence relationship from CU to REF_PER_CU. */
16280
16281static void
16282dwarf2_add_dependence (struct dwarf2_cu *cu,
16283 struct dwarf2_per_cu_data *ref_per_cu)
16284{
16285 void **slot;
16286
16287 if (cu->dependencies == NULL)
16288 cu->dependencies
16289 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16290 NULL, &cu->comp_unit_obstack,
16291 hashtab_obstack_allocate,
16292 dummy_obstack_deallocate);
16293
16294 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16295 if (*slot == NULL)
16296 *slot = ref_per_cu;
16297}
1c379e20 16298
f504f079
DE
16299/* Subroutine of dwarf2_mark to pass to htab_traverse.
16300 Set the mark field in every compilation unit in the
ae038cb0
DJ
16301 cache that we must keep because we are keeping CU. */
16302
10b3939b
DJ
16303static int
16304dwarf2_mark_helper (void **slot, void *data)
16305{
16306 struct dwarf2_per_cu_data *per_cu;
16307
16308 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16309
16310 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16311 reading of the chain. As such dependencies remain valid it is not much
16312 useful to track and undo them during QUIT cleanups. */
16313 if (per_cu->cu == NULL)
16314 return 1;
16315
10b3939b
DJ
16316 if (per_cu->cu->mark)
16317 return 1;
16318 per_cu->cu->mark = 1;
16319
16320 if (per_cu->cu->dependencies != NULL)
16321 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16322
16323 return 1;
16324}
16325
f504f079
DE
16326/* Set the mark field in CU and in every other compilation unit in the
16327 cache that we must keep because we are keeping CU. */
16328
ae038cb0
DJ
16329static void
16330dwarf2_mark (struct dwarf2_cu *cu)
16331{
16332 if (cu->mark)
16333 return;
16334 cu->mark = 1;
10b3939b
DJ
16335 if (cu->dependencies != NULL)
16336 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16337}
16338
16339static void
16340dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16341{
16342 while (per_cu)
16343 {
16344 per_cu->cu->mark = 0;
16345 per_cu = per_cu->cu->read_in_chain;
16346 }
72bf9492
DJ
16347}
16348
72bf9492
DJ
16349/* Trivial hash function for partial_die_info: the hash value of a DIE
16350 is its offset in .debug_info for this objfile. */
16351
16352static hashval_t
16353partial_die_hash (const void *item)
16354{
16355 const struct partial_die_info *part_die = item;
9a619af0 16356
b64f50a1 16357 return part_die->offset.sect_off;
72bf9492
DJ
16358}
16359
16360/* Trivial comparison function for partial_die_info structures: two DIEs
16361 are equal if they have the same offset. */
16362
16363static int
16364partial_die_eq (const void *item_lhs, const void *item_rhs)
16365{
16366 const struct partial_die_info *part_die_lhs = item_lhs;
16367 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16368
b64f50a1 16369 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
16370}
16371
ae038cb0
DJ
16372static struct cmd_list_element *set_dwarf2_cmdlist;
16373static struct cmd_list_element *show_dwarf2_cmdlist;
16374
16375static void
16376set_dwarf2_cmd (char *args, int from_tty)
16377{
16378 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16379}
16380
16381static void
16382show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16383{
ae038cb0
DJ
16384 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16385}
16386
dce234bc
PP
16387/* If section described by INFO was mmapped, munmap it now. */
16388
16389static void
16390munmap_section_buffer (struct dwarf2_section_info *info)
16391{
b315ab21 16392 if (info->map_addr != NULL)
dce234bc
PP
16393 {
16394#ifdef HAVE_MMAP
b315ab21 16395 int res;
9a619af0 16396
b315ab21
TG
16397 res = munmap (info->map_addr, info->map_len);
16398 gdb_assert (res == 0);
dce234bc
PP
16399#else
16400 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16401 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16402#endif
16403 }
16404}
16405
16406/* munmap debug sections for OBJFILE, if necessary. */
16407
16408static void
c1bd65d0 16409dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16410{
16411 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16412 int ix;
16413 struct dwarf2_section_info *section;
9a619af0 16414
16be1145
DE
16415 /* This is sorted according to the order they're defined in to make it easier
16416 to keep in sync. */
dce234bc
PP
16417 munmap_section_buffer (&data->info);
16418 munmap_section_buffer (&data->abbrev);
16419 munmap_section_buffer (&data->line);
16be1145 16420 munmap_section_buffer (&data->loc);
dce234bc 16421 munmap_section_buffer (&data->macinfo);
cf2c3c16 16422 munmap_section_buffer (&data->macro);
16be1145 16423 munmap_section_buffer (&data->str);
dce234bc 16424 munmap_section_buffer (&data->ranges);
dce234bc
PP
16425 munmap_section_buffer (&data->frame);
16426 munmap_section_buffer (&data->eh_frame);
9291a0cd 16427 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16428
16429 for (ix = 0;
16430 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16431 ++ix)
16432 munmap_section_buffer (section);
16433
16434 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16435}
16436
16437\f
ae2de4f8 16438/* The "save gdb-index" command. */
9291a0cd
TT
16439
16440/* The contents of the hash table we create when building the string
16441 table. */
16442struct strtab_entry
16443{
16444 offset_type offset;
16445 const char *str;
16446};
16447
559a7a62
JK
16448/* Hash function for a strtab_entry.
16449
16450 Function is used only during write_hash_table so no index format backward
16451 compatibility is needed. */
b89be57b 16452
9291a0cd
TT
16453static hashval_t
16454hash_strtab_entry (const void *e)
16455{
16456 const struct strtab_entry *entry = e;
559a7a62 16457 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16458}
16459
16460/* Equality function for a strtab_entry. */
b89be57b 16461
9291a0cd
TT
16462static int
16463eq_strtab_entry (const void *a, const void *b)
16464{
16465 const struct strtab_entry *ea = a;
16466 const struct strtab_entry *eb = b;
16467 return !strcmp (ea->str, eb->str);
16468}
16469
16470/* Create a strtab_entry hash table. */
b89be57b 16471
9291a0cd
TT
16472static htab_t
16473create_strtab (void)
16474{
16475 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16476 xfree, xcalloc, xfree);
16477}
16478
16479/* Add a string to the constant pool. Return the string's offset in
16480 host order. */
b89be57b 16481
9291a0cd
TT
16482static offset_type
16483add_string (htab_t table, struct obstack *cpool, const char *str)
16484{
16485 void **slot;
16486 struct strtab_entry entry;
16487 struct strtab_entry *result;
16488
16489 entry.str = str;
16490 slot = htab_find_slot (table, &entry, INSERT);
16491 if (*slot)
16492 result = *slot;
16493 else
16494 {
16495 result = XNEW (struct strtab_entry);
16496 result->offset = obstack_object_size (cpool);
16497 result->str = str;
16498 obstack_grow_str0 (cpool, str);
16499 *slot = result;
16500 }
16501 return result->offset;
16502}
16503
16504/* An entry in the symbol table. */
16505struct symtab_index_entry
16506{
16507 /* The name of the symbol. */
16508 const char *name;
16509 /* The offset of the name in the constant pool. */
16510 offset_type index_offset;
16511 /* A sorted vector of the indices of all the CUs that hold an object
16512 of this name. */
16513 VEC (offset_type) *cu_indices;
16514};
16515
16516/* The symbol table. This is a power-of-2-sized hash table. */
16517struct mapped_symtab
16518{
16519 offset_type n_elements;
16520 offset_type size;
16521 struct symtab_index_entry **data;
16522};
16523
16524/* Hash function for a symtab_index_entry. */
b89be57b 16525
9291a0cd
TT
16526static hashval_t
16527hash_symtab_entry (const void *e)
16528{
16529 const struct symtab_index_entry *entry = e;
16530 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16531 sizeof (offset_type) * VEC_length (offset_type,
16532 entry->cu_indices),
16533 0);
16534}
16535
16536/* Equality function for a symtab_index_entry. */
b89be57b 16537
9291a0cd
TT
16538static int
16539eq_symtab_entry (const void *a, const void *b)
16540{
16541 const struct symtab_index_entry *ea = a;
16542 const struct symtab_index_entry *eb = b;
16543 int len = VEC_length (offset_type, ea->cu_indices);
16544 if (len != VEC_length (offset_type, eb->cu_indices))
16545 return 0;
16546 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16547 VEC_address (offset_type, eb->cu_indices),
16548 sizeof (offset_type) * len);
16549}
16550
16551/* Destroy a symtab_index_entry. */
b89be57b 16552
9291a0cd
TT
16553static void
16554delete_symtab_entry (void *p)
16555{
16556 struct symtab_index_entry *entry = p;
16557 VEC_free (offset_type, entry->cu_indices);
16558 xfree (entry);
16559}
16560
16561/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16562
9291a0cd 16563static htab_t
3876f04e 16564create_symbol_hash_table (void)
9291a0cd
TT
16565{
16566 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16567 delete_symtab_entry, xcalloc, xfree);
16568}
16569
16570/* Create a new mapped symtab object. */
b89be57b 16571
9291a0cd
TT
16572static struct mapped_symtab *
16573create_mapped_symtab (void)
16574{
16575 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16576 symtab->n_elements = 0;
16577 symtab->size = 1024;
16578 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16579 return symtab;
16580}
16581
16582/* Destroy a mapped_symtab. */
b89be57b 16583
9291a0cd
TT
16584static void
16585cleanup_mapped_symtab (void *p)
16586{
16587 struct mapped_symtab *symtab = p;
16588 /* The contents of the array are freed when the other hash table is
16589 destroyed. */
16590 xfree (symtab->data);
16591 xfree (symtab);
16592}
16593
16594/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16595 the slot.
16596
16597 Function is used only during write_hash_table so no index format backward
16598 compatibility is needed. */
b89be57b 16599
9291a0cd
TT
16600static struct symtab_index_entry **
16601find_slot (struct mapped_symtab *symtab, const char *name)
16602{
559a7a62 16603 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16604
16605 index = hash & (symtab->size - 1);
16606 step = ((hash * 17) & (symtab->size - 1)) | 1;
16607
16608 for (;;)
16609 {
16610 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16611 return &symtab->data[index];
16612 index = (index + step) & (symtab->size - 1);
16613 }
16614}
16615
16616/* Expand SYMTAB's hash table. */
b89be57b 16617
9291a0cd
TT
16618static void
16619hash_expand (struct mapped_symtab *symtab)
16620{
16621 offset_type old_size = symtab->size;
16622 offset_type i;
16623 struct symtab_index_entry **old_entries = symtab->data;
16624
16625 symtab->size *= 2;
16626 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16627
16628 for (i = 0; i < old_size; ++i)
16629 {
16630 if (old_entries[i])
16631 {
16632 struct symtab_index_entry **slot = find_slot (symtab,
16633 old_entries[i]->name);
16634 *slot = old_entries[i];
16635 }
16636 }
16637
16638 xfree (old_entries);
16639}
16640
16641/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16642 is the index of the CU in which the symbol appears. */
b89be57b 16643
9291a0cd
TT
16644static void
16645add_index_entry (struct mapped_symtab *symtab, const char *name,
16646 offset_type cu_index)
16647{
16648 struct symtab_index_entry **slot;
16649
16650 ++symtab->n_elements;
16651 if (4 * symtab->n_elements / 3 >= symtab->size)
16652 hash_expand (symtab);
16653
16654 slot = find_slot (symtab, name);
16655 if (!*slot)
16656 {
16657 *slot = XNEW (struct symtab_index_entry);
16658 (*slot)->name = name;
16659 (*slot)->cu_indices = NULL;
16660 }
16661 /* Don't push an index twice. Due to how we add entries we only
16662 have to check the last one. */
16663 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16664 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16665 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16666}
16667
16668/* Add a vector of indices to the constant pool. */
b89be57b 16669
9291a0cd 16670static offset_type
3876f04e 16671add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16672 struct symtab_index_entry *entry)
16673{
16674 void **slot;
16675
3876f04e 16676 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16677 if (!*slot)
16678 {
16679 offset_type len = VEC_length (offset_type, entry->cu_indices);
16680 offset_type val = MAYBE_SWAP (len);
16681 offset_type iter;
16682 int i;
16683
16684 *slot = entry;
16685 entry->index_offset = obstack_object_size (cpool);
16686
16687 obstack_grow (cpool, &val, sizeof (val));
16688 for (i = 0;
16689 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16690 ++i)
16691 {
16692 val = MAYBE_SWAP (iter);
16693 obstack_grow (cpool, &val, sizeof (val));
16694 }
16695 }
16696 else
16697 {
16698 struct symtab_index_entry *old_entry = *slot;
16699 entry->index_offset = old_entry->index_offset;
16700 entry = old_entry;
16701 }
16702 return entry->index_offset;
16703}
16704
16705/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16706 constant pool entries going into the obstack CPOOL. */
b89be57b 16707
9291a0cd
TT
16708static void
16709write_hash_table (struct mapped_symtab *symtab,
16710 struct obstack *output, struct obstack *cpool)
16711{
16712 offset_type i;
3876f04e 16713 htab_t symbol_hash_table;
9291a0cd
TT
16714 htab_t str_table;
16715
3876f04e 16716 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16717 str_table = create_strtab ();
3876f04e 16718
9291a0cd
TT
16719 /* We add all the index vectors to the constant pool first, to
16720 ensure alignment is ok. */
16721 for (i = 0; i < symtab->size; ++i)
16722 {
16723 if (symtab->data[i])
3876f04e 16724 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16725 }
16726
16727 /* Now write out the hash table. */
16728 for (i = 0; i < symtab->size; ++i)
16729 {
16730 offset_type str_off, vec_off;
16731
16732 if (symtab->data[i])
16733 {
16734 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16735 vec_off = symtab->data[i]->index_offset;
16736 }
16737 else
16738 {
16739 /* While 0 is a valid constant pool index, it is not valid
16740 to have 0 for both offsets. */
16741 str_off = 0;
16742 vec_off = 0;
16743 }
16744
16745 str_off = MAYBE_SWAP (str_off);
16746 vec_off = MAYBE_SWAP (vec_off);
16747
16748 obstack_grow (output, &str_off, sizeof (str_off));
16749 obstack_grow (output, &vec_off, sizeof (vec_off));
16750 }
16751
16752 htab_delete (str_table);
3876f04e 16753 htab_delete (symbol_hash_table);
9291a0cd
TT
16754}
16755
0a5429f6
DE
16756/* Struct to map psymtab to CU index in the index file. */
16757struct psymtab_cu_index_map
16758{
16759 struct partial_symtab *psymtab;
16760 unsigned int cu_index;
16761};
16762
16763static hashval_t
16764hash_psymtab_cu_index (const void *item)
16765{
16766 const struct psymtab_cu_index_map *map = item;
16767
16768 return htab_hash_pointer (map->psymtab);
16769}
16770
16771static int
16772eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16773{
16774 const struct psymtab_cu_index_map *lhs = item_lhs;
16775 const struct psymtab_cu_index_map *rhs = item_rhs;
16776
16777 return lhs->psymtab == rhs->psymtab;
16778}
16779
16780/* Helper struct for building the address table. */
16781struct addrmap_index_data
16782{
16783 struct objfile *objfile;
16784 struct obstack *addr_obstack;
16785 htab_t cu_index_htab;
16786
16787 /* Non-zero if the previous_* fields are valid.
16788 We can't write an entry until we see the next entry (since it is only then
16789 that we know the end of the entry). */
16790 int previous_valid;
16791 /* Index of the CU in the table of all CUs in the index file. */
16792 unsigned int previous_cu_index;
0963b4bd 16793 /* Start address of the CU. */
0a5429f6
DE
16794 CORE_ADDR previous_cu_start;
16795};
16796
16797/* Write an address entry to OBSTACK. */
b89be57b 16798
9291a0cd 16799static void
0a5429f6
DE
16800add_address_entry (struct objfile *objfile, struct obstack *obstack,
16801 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16802{
0a5429f6 16803 offset_type cu_index_to_write;
9291a0cd
TT
16804 char addr[8];
16805 CORE_ADDR baseaddr;
16806
16807 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16808
0a5429f6
DE
16809 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16810 obstack_grow (obstack, addr, 8);
16811 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16812 obstack_grow (obstack, addr, 8);
16813 cu_index_to_write = MAYBE_SWAP (cu_index);
16814 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16815}
16816
16817/* Worker function for traversing an addrmap to build the address table. */
16818
16819static int
16820add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16821{
16822 struct addrmap_index_data *data = datap;
16823 struct partial_symtab *pst = obj;
16824 offset_type cu_index;
16825 void **slot;
16826
16827 if (data->previous_valid)
16828 add_address_entry (data->objfile, data->addr_obstack,
16829 data->previous_cu_start, start_addr,
16830 data->previous_cu_index);
16831
16832 data->previous_cu_start = start_addr;
16833 if (pst != NULL)
16834 {
16835 struct psymtab_cu_index_map find_map, *map;
16836 find_map.psymtab = pst;
16837 map = htab_find (data->cu_index_htab, &find_map);
16838 gdb_assert (map != NULL);
16839 data->previous_cu_index = map->cu_index;
16840 data->previous_valid = 1;
16841 }
16842 else
16843 data->previous_valid = 0;
16844
16845 return 0;
16846}
16847
16848/* Write OBJFILE's address map to OBSTACK.
16849 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16850 in the index file. */
16851
16852static void
16853write_address_map (struct objfile *objfile, struct obstack *obstack,
16854 htab_t cu_index_htab)
16855{
16856 struct addrmap_index_data addrmap_index_data;
16857
16858 /* When writing the address table, we have to cope with the fact that
16859 the addrmap iterator only provides the start of a region; we have to
16860 wait until the next invocation to get the start of the next region. */
16861
16862 addrmap_index_data.objfile = objfile;
16863 addrmap_index_data.addr_obstack = obstack;
16864 addrmap_index_data.cu_index_htab = cu_index_htab;
16865 addrmap_index_data.previous_valid = 0;
16866
16867 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16868 &addrmap_index_data);
16869
16870 /* It's highly unlikely the last entry (end address = 0xff...ff)
16871 is valid, but we should still handle it.
16872 The end address is recorded as the start of the next region, but that
16873 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16874 anyway. */
16875 if (addrmap_index_data.previous_valid)
16876 add_address_entry (objfile, obstack,
16877 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16878 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16879}
16880
16881/* Add a list of partial symbols to SYMTAB. */
b89be57b 16882
9291a0cd
TT
16883static void
16884write_psymbols (struct mapped_symtab *symtab,
987d643c 16885 htab_t psyms_seen,
9291a0cd
TT
16886 struct partial_symbol **psymp,
16887 int count,
987d643c
TT
16888 offset_type cu_index,
16889 int is_static)
9291a0cd
TT
16890{
16891 for (; count-- > 0; ++psymp)
16892 {
987d643c
TT
16893 void **slot, *lookup;
16894
9291a0cd
TT
16895 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16896 error (_("Ada is not currently supported by the index"));
987d643c
TT
16897
16898 /* We only want to add a given psymbol once. However, we also
16899 want to account for whether it is global or static. So, we
16900 may add it twice, using slightly different values. */
16901 if (is_static)
16902 {
16903 uintptr_t val = 1 | (uintptr_t) *psymp;
16904
16905 lookup = (void *) val;
16906 }
16907 else
16908 lookup = *psymp;
16909
16910 /* Only add a given psymbol once. */
16911 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16912 if (!*slot)
16913 {
16914 *slot = lookup;
bb2f58dc 16915 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16916 }
9291a0cd
TT
16917 }
16918}
16919
16920/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16921 exception if there is an error. */
b89be57b 16922
9291a0cd
TT
16923static void
16924write_obstack (FILE *file, struct obstack *obstack)
16925{
16926 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16927 file)
16928 != obstack_object_size (obstack))
16929 error (_("couldn't data write to file"));
16930}
16931
16932/* Unlink a file if the argument is not NULL. */
b89be57b 16933
9291a0cd
TT
16934static void
16935unlink_if_set (void *p)
16936{
16937 char **filename = p;
16938 if (*filename)
16939 unlink (*filename);
16940}
16941
1fd400ff
TT
16942/* A helper struct used when iterating over debug_types. */
16943struct signatured_type_index_data
16944{
16945 struct objfile *objfile;
16946 struct mapped_symtab *symtab;
16947 struct obstack *types_list;
987d643c 16948 htab_t psyms_seen;
1fd400ff
TT
16949 int cu_index;
16950};
16951
16952/* A helper function that writes a single signatured_type to an
16953 obstack. */
b89be57b 16954
1fd400ff
TT
16955static int
16956write_one_signatured_type (void **slot, void *d)
16957{
16958 struct signatured_type_index_data *info = d;
16959 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16960 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16961 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16962 gdb_byte val[8];
16963
16964 write_psymbols (info->symtab,
987d643c 16965 info->psyms_seen,
3e43a32a
MS
16966 info->objfile->global_psymbols.list
16967 + psymtab->globals_offset,
987d643c
TT
16968 psymtab->n_global_syms, info->cu_index,
16969 0);
1fd400ff 16970 write_psymbols (info->symtab,
987d643c 16971 info->psyms_seen,
3e43a32a
MS
16972 info->objfile->static_psymbols.list
16973 + psymtab->statics_offset,
987d643c
TT
16974 psymtab->n_static_syms, info->cu_index,
16975 1);
1fd400ff 16976
b64f50a1
JK
16977 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
16978 entry->per_cu.offset.sect_off);
1fd400ff 16979 obstack_grow (info->types_list, val, 8);
b64f50a1 16980 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
1fd400ff
TT
16981 obstack_grow (info->types_list, val, 8);
16982 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16983 obstack_grow (info->types_list, val, 8);
16984
16985 ++info->cu_index;
16986
16987 return 1;
16988}
16989
9291a0cd 16990/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16991
9291a0cd
TT
16992static void
16993write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16994{
16995 struct cleanup *cleanup;
16996 char *filename, *cleanup_filename;
1fd400ff
TT
16997 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16998 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16999 int i;
17000 FILE *out_file;
17001 struct mapped_symtab *symtab;
17002 offset_type val, size_of_contents, total_len;
17003 struct stat st;
17004 char buf[8];
987d643c 17005 htab_t psyms_seen;
0a5429f6
DE
17006 htab_t cu_index_htab;
17007 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17008
b4f2f049 17009 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17010 return;
b4f2f049 17011
9291a0cd
TT
17012 if (dwarf2_per_objfile->using_index)
17013 error (_("Cannot use an index to create the index"));
17014
8b70b953
TT
17015 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17016 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17017
9291a0cd 17018 if (stat (objfile->name, &st) < 0)
7e17e088 17019 perror_with_name (objfile->name);
9291a0cd
TT
17020
17021 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17022 INDEX_SUFFIX, (char *) NULL);
17023 cleanup = make_cleanup (xfree, filename);
17024
17025 out_file = fopen (filename, "wb");
17026 if (!out_file)
17027 error (_("Can't open `%s' for writing"), filename);
17028
17029 cleanup_filename = filename;
17030 make_cleanup (unlink_if_set, &cleanup_filename);
17031
17032 symtab = create_mapped_symtab ();
17033 make_cleanup (cleanup_mapped_symtab, symtab);
17034
17035 obstack_init (&addr_obstack);
17036 make_cleanup_obstack_free (&addr_obstack);
17037
17038 obstack_init (&cu_list);
17039 make_cleanup_obstack_free (&cu_list);
17040
1fd400ff
TT
17041 obstack_init (&types_cu_list);
17042 make_cleanup_obstack_free (&types_cu_list);
17043
987d643c
TT
17044 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17045 NULL, xcalloc, xfree);
96408a79 17046 make_cleanup_htab_delete (psyms_seen);
987d643c 17047
0a5429f6
DE
17048 /* While we're scanning CU's create a table that maps a psymtab pointer
17049 (which is what addrmap records) to its index (which is what is recorded
17050 in the index file). This will later be needed to write the address
17051 table. */
17052 cu_index_htab = htab_create_alloc (100,
17053 hash_psymtab_cu_index,
17054 eq_psymtab_cu_index,
17055 NULL, xcalloc, xfree);
96408a79 17056 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17057 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17058 xmalloc (sizeof (struct psymtab_cu_index_map)
17059 * dwarf2_per_objfile->n_comp_units);
17060 make_cleanup (xfree, psymtab_cu_index_map);
17061
17062 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17063 work here. Also, the debug_types entries do not appear in
17064 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17065 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17066 {
3e43a32a
MS
17067 struct dwarf2_per_cu_data *per_cu
17068 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17069 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17070 gdb_byte val[8];
0a5429f6
DE
17071 struct psymtab_cu_index_map *map;
17072 void **slot;
9291a0cd
TT
17073
17074 write_psymbols (symtab,
987d643c 17075 psyms_seen,
9291a0cd 17076 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17077 psymtab->n_global_syms, i,
17078 0);
9291a0cd 17079 write_psymbols (symtab,
987d643c 17080 psyms_seen,
9291a0cd 17081 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17082 psymtab->n_static_syms, i,
17083 1);
9291a0cd 17084
0a5429f6
DE
17085 map = &psymtab_cu_index_map[i];
17086 map->psymtab = psymtab;
17087 map->cu_index = i;
17088 slot = htab_find_slot (cu_index_htab, map, INSERT);
17089 gdb_assert (slot != NULL);
17090 gdb_assert (*slot == NULL);
17091 *slot = map;
9291a0cd 17092
b64f50a1
JK
17093 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17094 per_cu->offset.sect_off);
9291a0cd 17095 obstack_grow (&cu_list, val, 8);
e254ef6a 17096 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17097 obstack_grow (&cu_list, val, 8);
17098 }
17099
0a5429f6
DE
17100 /* Dump the address map. */
17101 write_address_map (objfile, &addr_obstack, cu_index_htab);
17102
1fd400ff
TT
17103 /* Write out the .debug_type entries, if any. */
17104 if (dwarf2_per_objfile->signatured_types)
17105 {
17106 struct signatured_type_index_data sig_data;
17107
17108 sig_data.objfile = objfile;
17109 sig_data.symtab = symtab;
17110 sig_data.types_list = &types_cu_list;
987d643c 17111 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17112 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17113 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17114 write_one_signatured_type, &sig_data);
17115 }
17116
9291a0cd
TT
17117 obstack_init (&constant_pool);
17118 make_cleanup_obstack_free (&constant_pool);
17119 obstack_init (&symtab_obstack);
17120 make_cleanup_obstack_free (&symtab_obstack);
17121 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17122
17123 obstack_init (&contents);
17124 make_cleanup_obstack_free (&contents);
1fd400ff 17125 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17126 total_len = size_of_contents;
17127
17128 /* The version number. */
481860b3 17129 val = MAYBE_SWAP (6);
9291a0cd
TT
17130 obstack_grow (&contents, &val, sizeof (val));
17131
17132 /* The offset of the CU list from the start of the file. */
17133 val = MAYBE_SWAP (total_len);
17134 obstack_grow (&contents, &val, sizeof (val));
17135 total_len += obstack_object_size (&cu_list);
17136
1fd400ff
TT
17137 /* The offset of the types CU list from the start of the file. */
17138 val = MAYBE_SWAP (total_len);
17139 obstack_grow (&contents, &val, sizeof (val));
17140 total_len += obstack_object_size (&types_cu_list);
17141
9291a0cd
TT
17142 /* The offset of the address table from the start of the file. */
17143 val = MAYBE_SWAP (total_len);
17144 obstack_grow (&contents, &val, sizeof (val));
17145 total_len += obstack_object_size (&addr_obstack);
17146
17147 /* The offset of the symbol table from the start of the file. */
17148 val = MAYBE_SWAP (total_len);
17149 obstack_grow (&contents, &val, sizeof (val));
17150 total_len += obstack_object_size (&symtab_obstack);
17151
17152 /* The offset of the constant pool from the start of the file. */
17153 val = MAYBE_SWAP (total_len);
17154 obstack_grow (&contents, &val, sizeof (val));
17155 total_len += obstack_object_size (&constant_pool);
17156
17157 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17158
17159 write_obstack (out_file, &contents);
17160 write_obstack (out_file, &cu_list);
1fd400ff 17161 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17162 write_obstack (out_file, &addr_obstack);
17163 write_obstack (out_file, &symtab_obstack);
17164 write_obstack (out_file, &constant_pool);
17165
17166 fclose (out_file);
17167
17168 /* We want to keep the file, so we set cleanup_filename to NULL
17169 here. See unlink_if_set. */
17170 cleanup_filename = NULL;
17171
17172 do_cleanups (cleanup);
17173}
17174
90476074
TT
17175/* Implementation of the `save gdb-index' command.
17176
17177 Note that the file format used by this command is documented in the
17178 GDB manual. Any changes here must be documented there. */
11570e71 17179
9291a0cd
TT
17180static void
17181save_gdb_index_command (char *arg, int from_tty)
17182{
17183 struct objfile *objfile;
17184
17185 if (!arg || !*arg)
96d19272 17186 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17187
17188 ALL_OBJFILES (objfile)
17189 {
17190 struct stat st;
17191
17192 /* If the objfile does not correspond to an actual file, skip it. */
17193 if (stat (objfile->name, &st) < 0)
17194 continue;
17195
17196 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17197 if (dwarf2_per_objfile)
17198 {
17199 volatile struct gdb_exception except;
17200
17201 TRY_CATCH (except, RETURN_MASK_ERROR)
17202 {
17203 write_psymtabs_to_index (objfile, arg);
17204 }
17205 if (except.reason < 0)
17206 exception_fprintf (gdb_stderr, except,
17207 _("Error while writing index for `%s': "),
17208 objfile->name);
17209 }
17210 }
dce234bc
PP
17211}
17212
9291a0cd
TT
17213\f
17214
9eae7c52
TT
17215int dwarf2_always_disassemble;
17216
17217static void
17218show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17219 struct cmd_list_element *c, const char *value)
17220{
3e43a32a
MS
17221 fprintf_filtered (file,
17222 _("Whether to always disassemble "
17223 "DWARF expressions is %s.\n"),
9eae7c52
TT
17224 value);
17225}
17226
900e11f9
JK
17227static void
17228show_check_physname (struct ui_file *file, int from_tty,
17229 struct cmd_list_element *c, const char *value)
17230{
17231 fprintf_filtered (file,
17232 _("Whether to check \"physname\" is %s.\n"),
17233 value);
17234}
17235
6502dd73
DJ
17236void _initialize_dwarf2_read (void);
17237
17238void
17239_initialize_dwarf2_read (void)
17240{
96d19272
JK
17241 struct cmd_list_element *c;
17242
dce234bc 17243 dwarf2_objfile_data_key
c1bd65d0 17244 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17245
1bedd215
AC
17246 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17247Set DWARF 2 specific variables.\n\
17248Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17249 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17250 0/*allow-unknown*/, &maintenance_set_cmdlist);
17251
1bedd215
AC
17252 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17253Show DWARF 2 specific variables\n\
17254Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17255 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17256 0/*allow-unknown*/, &maintenance_show_cmdlist);
17257
17258 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17259 &dwarf2_max_cache_age, _("\
17260Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17261Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17262A higher limit means that cached compilation units will be stored\n\
17263in memory longer, and more total memory will be used. Zero disables\n\
17264caching, which can slow down startup."),
2c5b56ce 17265 NULL,
920d2a44 17266 show_dwarf2_max_cache_age,
2c5b56ce 17267 &set_dwarf2_cmdlist,
ae038cb0 17268 &show_dwarf2_cmdlist);
d97bc12b 17269
9eae7c52
TT
17270 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17271 &dwarf2_always_disassemble, _("\
17272Set whether `info address' always disassembles DWARF expressions."), _("\
17273Show whether `info address' always disassembles DWARF expressions."), _("\
17274When enabled, DWARF expressions are always printed in an assembly-like\n\
17275syntax. When disabled, expressions will be printed in a more\n\
17276conversational style, when possible."),
17277 NULL,
17278 show_dwarf2_always_disassemble,
17279 &set_dwarf2_cmdlist,
17280 &show_dwarf2_cmdlist);
17281
d97bc12b
DE
17282 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17283Set debugging of the dwarf2 DIE reader."), _("\
17284Show debugging of the dwarf2 DIE reader."), _("\
17285When enabled (non-zero), DIEs are dumped after they are read in.\n\
17286The value is the maximum depth to print."),
17287 NULL,
17288 NULL,
17289 &setdebuglist, &showdebuglist);
9291a0cd 17290
900e11f9
JK
17291 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17292Set cross-checking of \"physname\" code against demangler."), _("\
17293Show cross-checking of \"physname\" code against demangler."), _("\
17294When enabled, GDB's internal \"physname\" code is checked against\n\
17295the demangler."),
17296 NULL, show_check_physname,
17297 &setdebuglist, &showdebuglist);
17298
96d19272 17299 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17300 _("\
fc1a9d6e 17301Save a gdb-index file.\n\
11570e71 17302Usage: save gdb-index DIRECTORY"),
96d19272
JK
17303 &save_cmdlist);
17304 set_cmd_completer (c, filename_completer);
6502dd73 17305}
This page took 2.688385 seconds and 4 git commands to generate.