Problem after hitting breakpoint on Windows (with GDBserver)
[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
dce234bc
PP
88static int pagesize;
89
df8a16a1
DJ
90/* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94static int processing_has_namespace_info;
95
6502dd73
DJ
96static const struct objfile_data *dwarf2_objfile_data_key;
97
dce234bc
PP
98struct dwarf2_section_info
99{
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
b315ab21
TG
103 /* Not NULL if the section was actually mmapped. */
104 void *map_addr;
105 /* Page aligned size of mmapped area. */
106 bfd_size_type map_len;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
120/* A description of the mapped index. The file format is described in
121 a comment by the code that writes the index. */
122struct mapped_index
123{
559a7a62
JK
124 /* Index data format version. */
125 int version;
126
9291a0cd
TT
127 /* The total length of the buffer. */
128 off_t total_size;
b11b1f88 129
9291a0cd
TT
130 /* A pointer to the address table data. */
131 const gdb_byte *address_table;
b11b1f88 132
9291a0cd
TT
133 /* Size of the address table data in bytes. */
134 offset_type address_table_size;
b11b1f88 135
3876f04e
DE
136 /* The symbol table, implemented as a hash table. */
137 const offset_type *symbol_table;
b11b1f88 138
9291a0cd 139 /* Size in slots, each slot is 2 offset_types. */
3876f04e 140 offset_type symbol_table_slots;
b11b1f88 141
9291a0cd
TT
142 /* A pointer to the constant pool. */
143 const char *constant_pool;
144};
145
9cdd5dbd
DE
146/* Collection of data recorded per objfile.
147 This hangs off of dwarf2_objfile_data_key. */
148
6502dd73
DJ
149struct dwarf2_per_objfile
150{
dce234bc
PP
151 struct dwarf2_section_info info;
152 struct dwarf2_section_info abbrev;
153 struct dwarf2_section_info line;
dce234bc
PP
154 struct dwarf2_section_info loc;
155 struct dwarf2_section_info macinfo;
cf2c3c16 156 struct dwarf2_section_info macro;
dce234bc
PP
157 struct dwarf2_section_info str;
158 struct dwarf2_section_info ranges;
159 struct dwarf2_section_info frame;
160 struct dwarf2_section_info eh_frame;
9291a0cd 161 struct dwarf2_section_info gdb_index;
ae038cb0 162
8b70b953
TT
163 VEC (dwarf2_section_info_def) *types;
164
be391dca
TT
165 /* Back link. */
166 struct objfile *objfile;
167
d467dd73 168 /* Table of all the compilation units. This is used to locate
10b3939b 169 the target compilation unit of a particular reference. */
ae038cb0
DJ
170 struct dwarf2_per_cu_data **all_comp_units;
171
172 /* The number of compilation units in ALL_COMP_UNITS. */
173 int n_comp_units;
174
1fd400ff 175 /* The number of .debug_types-related CUs. */
d467dd73 176 int n_type_units;
1fd400ff 177
d467dd73
DE
178 /* The .debug_types-related CUs (TUs). */
179 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 180
ae038cb0
DJ
181 /* A chain of compilation units that are currently read in, so that
182 they can be freed later. */
183 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 184
348e048f
DE
185 /* A table mapping .debug_types signatures to its signatured_type entry.
186 This is NULL if the .debug_types section hasn't been read in yet. */
187 htab_t signatured_types;
188
72dca2f5
FR
189 /* A flag indicating wether this objfile has a section loaded at a
190 VMA of 0. */
191 int has_section_at_zero;
9291a0cd 192
ae2de4f8
DE
193 /* True if we are using the mapped index,
194 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
195 unsigned char using_index;
196
ae2de4f8 197 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 198 struct mapped_index *index_table;
98bfdba5 199
7b9f3c50
DE
200 /* When using index_table, this keeps track of all quick_file_names entries.
201 TUs can share line table entries with CUs or other TUs, and there can be
202 a lot more TUs than unique line tables, so we maintain a separate table
203 of all line table entries to support the sharing. */
204 htab_t quick_file_names_table;
205
98bfdba5
PA
206 /* Set during partial symbol reading, to prevent queueing of full
207 symbols. */
208 int reading_partial_symbols;
673bfd45
DE
209
210 /* Table mapping type .debug_info DIE offsets to types.
211 This is NULL if not allocated yet.
212 It (currently) makes sense to allocate debug_types_type_hash lazily.
213 To keep things simple we allocate both lazily. */
214 htab_t debug_info_type_hash;
215
216 /* Table mapping type .debug_types DIE offsets to types.
217 This is NULL if not allocated yet. */
218 htab_t debug_types_type_hash;
6502dd73
DJ
219};
220
221static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 222
251d32d9 223/* Default names of the debugging sections. */
c906108c 224
233a11ab
CS
225/* Note that if the debugging section has been compressed, it might
226 have a name like .zdebug_info. */
227
9cdd5dbd
DE
228static const struct dwarf2_debug_sections dwarf2_elf_names =
229{
251d32d9
TG
230 { ".debug_info", ".zdebug_info" },
231 { ".debug_abbrev", ".zdebug_abbrev" },
232 { ".debug_line", ".zdebug_line" },
233 { ".debug_loc", ".zdebug_loc" },
234 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 235 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
236 { ".debug_str", ".zdebug_str" },
237 { ".debug_ranges", ".zdebug_ranges" },
238 { ".debug_types", ".zdebug_types" },
239 { ".debug_frame", ".zdebug_frame" },
240 { ".eh_frame", NULL },
24d3216f
TT
241 { ".gdb_index", ".zgdb_index" },
242 23
251d32d9 243};
c906108c
SS
244
245/* local data types */
246
0963b4bd 247/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
248#ifndef ABBREV_HASH_SIZE
249#define ABBREV_HASH_SIZE 121
250#endif
251
107d2387
AC
252/* The data in a compilation unit header, after target2host
253 translation, looks like this. */
c906108c 254struct comp_unit_head
a738430d 255{
c764a876 256 unsigned int length;
a738430d 257 short version;
a738430d
MK
258 unsigned char addr_size;
259 unsigned char signed_addr_p;
9cbfa09e 260 unsigned int abbrev_offset;
57349743 261
a738430d
MK
262 /* Size of file offsets; either 4 or 8. */
263 unsigned int offset_size;
57349743 264
a738430d
MK
265 /* Size of the length field; either 4 or 12. */
266 unsigned int initial_length_size;
57349743 267
a738430d
MK
268 /* Offset to the first byte of this compilation unit header in the
269 .debug_info section, for resolving relative reference dies. */
270 unsigned int offset;
57349743 271
d00adf39
DE
272 /* Offset to first die in this cu from the start of the cu.
273 This will be the first byte following the compilation unit header. */
274 unsigned int first_die_offset;
a738430d 275};
c906108c 276
3da10d80
KS
277/* Type used for delaying computation of method physnames.
278 See comments for compute_delayed_physnames. */
279struct delayed_method_info
280{
281 /* The type to which the method is attached, i.e., its parent class. */
282 struct type *type;
283
284 /* The index of the method in the type's function fieldlists. */
285 int fnfield_index;
286
287 /* The index of the method in the fieldlist. */
288 int index;
289
290 /* The name of the DIE. */
291 const char *name;
292
293 /* The DIE associated with this method. */
294 struct die_info *die;
295};
296
297typedef struct delayed_method_info delayed_method_info;
298DEF_VEC_O (delayed_method_info);
299
e7c27a73
DJ
300/* Internal state when decoding a particular compilation unit. */
301struct dwarf2_cu
302{
303 /* The objfile containing this compilation unit. */
304 struct objfile *objfile;
305
d00adf39 306 /* The header of the compilation unit. */
e7c27a73 307 struct comp_unit_head header;
e142c38c 308
d00adf39
DE
309 /* Base address of this compilation unit. */
310 CORE_ADDR base_address;
311
312 /* Non-zero if base_address has been set. */
313 int base_known;
314
e142c38c
DJ
315 /* The language we are debugging. */
316 enum language language;
317 const struct language_defn *language_defn;
318
b0f35d58
DL
319 const char *producer;
320
e142c38c
DJ
321 /* The generic symbol table building routines have separate lists for
322 file scope symbols and all all other scopes (local scopes). So
323 we need to select the right one to pass to add_symbol_to_list().
324 We do it by keeping a pointer to the correct list in list_in_scope.
325
326 FIXME: The original dwarf code just treated the file scope as the
327 first local scope, and all other local scopes as nested local
328 scopes, and worked fine. Check to see if we really need to
329 distinguish these in buildsym.c. */
330 struct pending **list_in_scope;
331
f3dd6933
DJ
332 /* DWARF abbreviation table associated with this compilation unit. */
333 struct abbrev_info **dwarf2_abbrevs;
334
335 /* Storage for the abbrev table. */
336 struct obstack abbrev_obstack;
72bf9492
DJ
337
338 /* Hash table holding all the loaded partial DIEs. */
339 htab_t partial_dies;
340
341 /* Storage for things with the same lifetime as this read-in compilation
342 unit, including partial DIEs. */
343 struct obstack comp_unit_obstack;
344
ae038cb0
DJ
345 /* When multiple dwarf2_cu structures are living in memory, this field
346 chains them all together, so that they can be released efficiently.
347 We will probably also want a generation counter so that most-recently-used
348 compilation units are cached... */
349 struct dwarf2_per_cu_data *read_in_chain;
350
351 /* Backchain to our per_cu entry if the tree has been built. */
352 struct dwarf2_per_cu_data *per_cu;
353
354 /* How many compilation units ago was this CU last referenced? */
355 int last_used;
356
10b3939b 357 /* A hash table of die offsets for following references. */
51545339 358 htab_t die_hash;
10b3939b
DJ
359
360 /* Full DIEs if read in. */
361 struct die_info *dies;
362
363 /* A set of pointers to dwarf2_per_cu_data objects for compilation
364 units referenced by this one. Only set during full symbol processing;
365 partial symbol tables do not have dependencies. */
366 htab_t dependencies;
367
cb1df416
DJ
368 /* Header data from the line table, during full symbol processing. */
369 struct line_header *line_header;
370
3da10d80
KS
371 /* A list of methods which need to have physnames computed
372 after all type information has been read. */
373 VEC (delayed_method_info) *method_list;
374
96408a79
SA
375 /* To be copied to symtab->call_site_htab. */
376 htab_t call_site_htab;
377
ae038cb0
DJ
378 /* Mark used when releasing cached dies. */
379 unsigned int mark : 1;
380
8be455d7
JK
381 /* This CU references .debug_loc. See the symtab->locations_valid field.
382 This test is imperfect as there may exist optimized debug code not using
383 any location list and still facing inlining issues if handled as
384 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 385 unsigned int has_loclist : 1;
ba919b58
TT
386
387 /* These cache the results of producer_is_gxx_lt_4_6.
388 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
389 information is cached because profiling CU expansion showed
390 excessive time spent in producer_is_gxx_lt_4_6. */
391 unsigned int checked_producer : 1;
392 unsigned int producer_is_gxx_lt_4_6 : 1;
e7c27a73
DJ
393};
394
10b3939b
DJ
395/* Persistent data held for a compilation unit, even when not
396 processing it. We put a pointer to this structure in the
28dee7f5 397 read_symtab_private field of the psymtab. */
10b3939b 398
ae038cb0
DJ
399struct dwarf2_per_cu_data
400{
348e048f 401 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 402 bytes should suffice to store the length of any compilation unit
45452591
DE
403 - if it doesn't, GDB will fall over anyway.
404 NOTE: Unlike comp_unit_head.length, this length includes
405 initial_length_size. */
c764a876 406 unsigned int offset;
348e048f 407 unsigned int length : 29;
ae038cb0
DJ
408
409 /* Flag indicating this compilation unit will be read in before
410 any of the current compilation units are processed. */
c764a876 411 unsigned int queued : 1;
ae038cb0 412
5afb4e99
DJ
413 /* This flag will be set if we need to load absolutely all DIEs
414 for this compilation unit, instead of just the ones we think
415 are interesting. It gets set if we look for a DIE in the
416 hash table and don't find it. */
417 unsigned int load_all_dies : 1;
418
8b70b953
TT
419 /* Non-null if this CU is from .debug_types; in which case it points
420 to the section. Otherwise it's from .debug_info. */
b0df02fd 421 struct dwarf2_section_info *debug_types_section;
348e048f 422
17ea53c3
JK
423 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
424 of the CU cache it gets reset to NULL again. */
ae038cb0 425 struct dwarf2_cu *cu;
1c379e20 426
9cdd5dbd
DE
427 /* The corresponding objfile.
428 Normally we can get the objfile from dwarf2_per_objfile.
429 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
430 struct objfile *objfile;
431
432 /* When using partial symbol tables, the 'psymtab' field is active.
433 Otherwise the 'quick' field is active. */
434 union
435 {
436 /* The partial symbol table associated with this compilation unit,
437 or NULL for partial units (which do not have an associated
438 symtab). */
439 struct partial_symtab *psymtab;
440
441 /* Data needed by the "quick" functions. */
442 struct dwarf2_per_cu_quick_data *quick;
443 } v;
ae038cb0
DJ
444};
445
348e048f
DE
446/* Entry in the signatured_types hash table. */
447
448struct signatured_type
449{
450 ULONGEST signature;
451
348e048f
DE
452 /* Offset in .debug_types of the type defined by this TU. */
453 unsigned int type_offset;
454
455 /* The CU(/TU) of this type. */
456 struct dwarf2_per_cu_data per_cu;
457};
458
0963b4bd
MS
459/* Struct used to pass misc. parameters to read_die_and_children, et
460 al. which are used for both .debug_info and .debug_types dies.
461 All parameters here are unchanging for the life of the call. This
462 struct exists to abstract away the constant parameters of die
463 reading. */
93311388
DE
464
465struct die_reader_specs
466{
467 /* The bfd of this objfile. */
468 bfd* abfd;
469
470 /* The CU of the DIE we are parsing. */
471 struct dwarf2_cu *cu;
472
473 /* Pointer to start of section buffer.
474 This is either the start of .debug_info or .debug_types. */
475 const gdb_byte *buffer;
476};
477
debd256d
JB
478/* The line number information for a compilation unit (found in the
479 .debug_line section) begins with a "statement program header",
480 which contains the following information. */
481struct line_header
482{
483 unsigned int total_length;
484 unsigned short version;
485 unsigned int header_length;
486 unsigned char minimum_instruction_length;
2dc7f7b3 487 unsigned char maximum_ops_per_instruction;
debd256d
JB
488 unsigned char default_is_stmt;
489 int line_base;
490 unsigned char line_range;
491 unsigned char opcode_base;
492
493 /* standard_opcode_lengths[i] is the number of operands for the
494 standard opcode whose value is i. This means that
495 standard_opcode_lengths[0] is unused, and the last meaningful
496 element is standard_opcode_lengths[opcode_base - 1]. */
497 unsigned char *standard_opcode_lengths;
498
499 /* The include_directories table. NOTE! These strings are not
500 allocated with xmalloc; instead, they are pointers into
501 debug_line_buffer. If you try to free them, `free' will get
502 indigestion. */
503 unsigned int num_include_dirs, include_dirs_size;
504 char **include_dirs;
505
506 /* The file_names table. NOTE! These strings are not allocated
507 with xmalloc; instead, they are pointers into debug_line_buffer.
508 Don't try to free them directly. */
509 unsigned int num_file_names, file_names_size;
510 struct file_entry
c906108c 511 {
debd256d
JB
512 char *name;
513 unsigned int dir_index;
514 unsigned int mod_time;
515 unsigned int length;
aaa75496 516 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 517 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
518 } *file_names;
519
520 /* The start and end of the statement program following this
6502dd73 521 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 522 gdb_byte *statement_program_start, *statement_program_end;
debd256d 523};
c906108c
SS
524
525/* When we construct a partial symbol table entry we only
0963b4bd 526 need this much information. */
c906108c
SS
527struct partial_die_info
528 {
72bf9492 529 /* Offset of this DIE. */
c906108c 530 unsigned int offset;
72bf9492
DJ
531
532 /* DWARF-2 tag for this DIE. */
533 ENUM_BITFIELD(dwarf_tag) tag : 16;
534
72bf9492
DJ
535 /* Assorted flags describing the data found in this DIE. */
536 unsigned int has_children : 1;
537 unsigned int is_external : 1;
538 unsigned int is_declaration : 1;
539 unsigned int has_type : 1;
540 unsigned int has_specification : 1;
541 unsigned int has_pc_info : 1;
542
543 /* Flag set if the SCOPE field of this structure has been
544 computed. */
545 unsigned int scope_set : 1;
546
fa4028e9
JB
547 /* Flag set if the DIE has a byte_size attribute. */
548 unsigned int has_byte_size : 1;
549
98bfdba5
PA
550 /* Flag set if any of the DIE's children are template arguments. */
551 unsigned int has_template_arguments : 1;
552
abc72ce4
DE
553 /* Flag set if fixup_partial_die has been called on this die. */
554 unsigned int fixup_called : 1;
555
72bf9492 556 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 557 sometimes a default name for unnamed DIEs. */
c906108c 558 char *name;
72bf9492 559
abc72ce4
DE
560 /* The linkage name, if present. */
561 const char *linkage_name;
562
72bf9492
DJ
563 /* The scope to prepend to our children. This is generally
564 allocated on the comp_unit_obstack, so will disappear
565 when this compilation unit leaves the cache. */
566 char *scope;
567
568 /* The location description associated with this DIE, if any. */
569 struct dwarf_block *locdesc;
570
571 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
572 CORE_ADDR lowpc;
573 CORE_ADDR highpc;
72bf9492 574
93311388 575 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 576 DW_AT_sibling, if any. */
abc72ce4
DE
577 /* NOTE: This member isn't strictly necessary, read_partial_die could
578 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 579 gdb_byte *sibling;
72bf9492
DJ
580
581 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
582 DW_AT_specification (or DW_AT_abstract_origin or
583 DW_AT_extension). */
584 unsigned int spec_offset;
585
586 /* Pointers to this DIE's parent, first child, and next sibling,
587 if any. */
588 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
589 };
590
0963b4bd 591/* This data structure holds the information of an abbrev. */
c906108c
SS
592struct abbrev_info
593 {
594 unsigned int number; /* number identifying abbrev */
595 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
596 unsigned short has_children; /* boolean */
597 unsigned short num_attrs; /* number of attributes */
c906108c
SS
598 struct attr_abbrev *attrs; /* an array of attribute descriptions */
599 struct abbrev_info *next; /* next in chain */
600 };
601
602struct attr_abbrev
603 {
9d25dd43
DE
604 ENUM_BITFIELD(dwarf_attribute) name : 16;
605 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
606 };
607
0963b4bd 608/* Attributes have a name and a value. */
b60c80d6
DJ
609struct attribute
610 {
9d25dd43 611 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
612 ENUM_BITFIELD(dwarf_form) form : 15;
613
614 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
615 field should be in u.str (existing only for DW_STRING) but it is kept
616 here for better struct attribute alignment. */
617 unsigned int string_is_canonical : 1;
618
b60c80d6
DJ
619 union
620 {
621 char *str;
622 struct dwarf_block *blk;
43bbcdc2
PH
623 ULONGEST unsnd;
624 LONGEST snd;
b60c80d6 625 CORE_ADDR addr;
348e048f 626 struct signatured_type *signatured_type;
b60c80d6
DJ
627 }
628 u;
629 };
630
0963b4bd 631/* This data structure holds a complete die structure. */
c906108c
SS
632struct die_info
633 {
76815b17
DE
634 /* DWARF-2 tag for this DIE. */
635 ENUM_BITFIELD(dwarf_tag) tag : 16;
636
637 /* Number of attributes */
98bfdba5
PA
638 unsigned char num_attrs;
639
640 /* True if we're presently building the full type name for the
641 type derived from this DIE. */
642 unsigned char building_fullname : 1;
76815b17
DE
643
644 /* Abbrev number */
645 unsigned int abbrev;
646
93311388 647 /* Offset in .debug_info or .debug_types section. */
76815b17 648 unsigned int offset;
78ba4af6
JB
649
650 /* The dies in a compilation unit form an n-ary tree. PARENT
651 points to this die's parent; CHILD points to the first child of
652 this node; and all the children of a given node are chained
4950bc1c 653 together via their SIBLING fields. */
639d11d3
DC
654 struct die_info *child; /* Its first child, if any. */
655 struct die_info *sibling; /* Its next sibling, if any. */
656 struct die_info *parent; /* Its parent, if any. */
c906108c 657
b60c80d6
DJ
658 /* An array of attributes, with NUM_ATTRS elements. There may be
659 zero, but it's not common and zero-sized arrays are not
660 sufficiently portable C. */
661 struct attribute attrs[1];
c906108c
SS
662 };
663
0963b4bd 664/* Get at parts of an attribute structure. */
c906108c
SS
665
666#define DW_STRING(attr) ((attr)->u.str)
8285870a 667#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
668#define DW_UNSND(attr) ((attr)->u.unsnd)
669#define DW_BLOCK(attr) ((attr)->u.blk)
670#define DW_SND(attr) ((attr)->u.snd)
671#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 672#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 673
0963b4bd 674/* Blocks are a bunch of untyped bytes. */
c906108c
SS
675struct dwarf_block
676 {
677 unsigned int size;
1d6edc3c
JK
678
679 /* Valid only if SIZE is not zero. */
fe1b8b76 680 gdb_byte *data;
c906108c
SS
681 };
682
c906108c
SS
683#ifndef ATTR_ALLOC_CHUNK
684#define ATTR_ALLOC_CHUNK 4
685#endif
686
c906108c
SS
687/* Allocate fields for structs, unions and enums in this size. */
688#ifndef DW_FIELD_ALLOC_CHUNK
689#define DW_FIELD_ALLOC_CHUNK 4
690#endif
691
c906108c
SS
692/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
693 but this would require a corresponding change in unpack_field_as_long
694 and friends. */
695static int bits_per_byte = 8;
696
697/* The routines that read and process dies for a C struct or C++ class
698 pass lists of data member fields and lists of member function fields
699 in an instance of a field_info structure, as defined below. */
700struct field_info
c5aa993b 701 {
0963b4bd 702 /* List of data member and baseclasses fields. */
c5aa993b
JM
703 struct nextfield
704 {
705 struct nextfield *next;
706 int accessibility;
707 int virtuality;
708 struct field field;
709 }
7d0ccb61 710 *fields, *baseclasses;
c906108c 711
7d0ccb61 712 /* Number of fields (including baseclasses). */
c5aa993b 713 int nfields;
c906108c 714
c5aa993b
JM
715 /* Number of baseclasses. */
716 int nbaseclasses;
c906108c 717
c5aa993b
JM
718 /* Set if the accesibility of one of the fields is not public. */
719 int non_public_fields;
c906108c 720
c5aa993b
JM
721 /* Member function fields array, entries are allocated in the order they
722 are encountered in the object file. */
723 struct nextfnfield
724 {
725 struct nextfnfield *next;
726 struct fn_field fnfield;
727 }
728 *fnfields;
c906108c 729
c5aa993b
JM
730 /* Member function fieldlist array, contains name of possibly overloaded
731 member function, number of overloaded member functions and a pointer
732 to the head of the member function field chain. */
733 struct fnfieldlist
734 {
735 char *name;
736 int length;
737 struct nextfnfield *head;
738 }
739 *fnfieldlists;
c906108c 740
c5aa993b
JM
741 /* Number of entries in the fnfieldlists array. */
742 int nfnfields;
98751a41
JK
743
744 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
745 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
746 struct typedef_field_list
747 {
748 struct typedef_field field;
749 struct typedef_field_list *next;
750 }
751 *typedef_field_list;
752 unsigned typedef_field_list_count;
c5aa993b 753 };
c906108c 754
10b3939b
DJ
755/* One item on the queue of compilation units to read in full symbols
756 for. */
757struct dwarf2_queue_item
758{
759 struct dwarf2_per_cu_data *per_cu;
760 struct dwarf2_queue_item *next;
761};
762
763/* The current queue. */
764static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
765
ae038cb0
DJ
766/* Loaded secondary compilation units are kept in memory until they
767 have not been referenced for the processing of this many
768 compilation units. Set this to zero to disable caching. Cache
769 sizes of up to at least twenty will improve startup time for
770 typical inter-CU-reference binaries, at an obvious memory cost. */
771static int dwarf2_max_cache_age = 5;
920d2a44
AC
772static void
773show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
774 struct cmd_list_element *c, const char *value)
775{
3e43a32a
MS
776 fprintf_filtered (file, _("The upper bound on the age of cached "
777 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
778 value);
779}
780
ae038cb0 781
0963b4bd 782/* Various complaints about symbol reading that don't abort the process. */
c906108c 783
4d3c2250
KB
784static void
785dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 786{
4d3c2250 787 complaint (&symfile_complaints,
e2e0b3e5 788 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
789}
790
25e43795
DJ
791static void
792dwarf2_debug_line_missing_file_complaint (void)
793{
794 complaint (&symfile_complaints,
795 _(".debug_line section has line data without a file"));
796}
797
59205f5a
JB
798static void
799dwarf2_debug_line_missing_end_sequence_complaint (void)
800{
801 complaint (&symfile_complaints,
3e43a32a
MS
802 _(".debug_line section has line "
803 "program sequence without an end"));
59205f5a
JB
804}
805
4d3c2250
KB
806static void
807dwarf2_complex_location_expr_complaint (void)
2e276125 808{
e2e0b3e5 809 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
810}
811
4d3c2250
KB
812static void
813dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
814 int arg3)
2e276125 815{
4d3c2250 816 complaint (&symfile_complaints,
3e43a32a
MS
817 _("const value length mismatch for '%s', got %d, expected %d"),
818 arg1, arg2, arg3);
4d3c2250
KB
819}
820
821static void
cf2c3c16 822dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 823{
4d3c2250 824 complaint (&symfile_complaints,
cf2c3c16
TT
825 _("macro info runs off end of `%s' section"),
826 section->asection->name);
4d3c2250
KB
827}
828
829static void
830dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 831{
4d3c2250 832 complaint (&symfile_complaints,
3e43a32a
MS
833 _("macro debug info contains a "
834 "malformed macro definition:\n`%s'"),
4d3c2250
KB
835 arg1);
836}
837
838static void
839dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 840{
4d3c2250 841 complaint (&symfile_complaints,
3e43a32a
MS
842 _("invalid attribute class or form for '%s' in '%s'"),
843 arg1, arg2);
4d3c2250 844}
c906108c 845
c906108c
SS
846/* local function prototypes */
847
4efb68b1 848static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 849
aaa75496
JB
850static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
851 struct objfile *);
852
918dd910
JK
853static void dwarf2_find_base_address (struct die_info *die,
854 struct dwarf2_cu *cu);
855
c67a9c90 856static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 857
72bf9492
DJ
858static void scan_partial_symbols (struct partial_die_info *,
859 CORE_ADDR *, CORE_ADDR *,
5734ee8b 860 int, struct dwarf2_cu *);
c906108c 861
72bf9492
DJ
862static void add_partial_symbol (struct partial_die_info *,
863 struct dwarf2_cu *);
63d06c5c 864
72bf9492
DJ
865static void add_partial_namespace (struct partial_die_info *pdi,
866 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 867 int need_pc, struct dwarf2_cu *cu);
63d06c5c 868
5d7cb8df
JK
869static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
870 CORE_ADDR *highpc, int need_pc,
871 struct dwarf2_cu *cu);
872
72bf9492
DJ
873static void add_partial_enumeration (struct partial_die_info *enum_pdi,
874 struct dwarf2_cu *cu);
91c24f0a 875
bc30ff58
JB
876static void add_partial_subprogram (struct partial_die_info *pdi,
877 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 878 int need_pc, struct dwarf2_cu *cu);
bc30ff58 879
fe1b8b76 880static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
881 gdb_byte *buffer, gdb_byte *info_ptr,
882 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 883
a14ed312 884static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 885
a14ed312 886static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 887
e5fe5e75 888static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 889
f3dd6933 890static void dwarf2_free_abbrev_table (void *);
c906108c 891
6caca83c
CC
892static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
893
fe1b8b76 894static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 895 struct dwarf2_cu *);
72bf9492 896
57349743 897static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 898 struct dwarf2_cu *);
c906108c 899
93311388
DE
900static struct partial_die_info *load_partial_dies (bfd *,
901 gdb_byte *, gdb_byte *,
902 int, struct dwarf2_cu *);
72bf9492 903
fe1b8b76 904static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
905 struct abbrev_info *abbrev,
906 unsigned int, bfd *,
907 gdb_byte *, gdb_byte *,
908 struct dwarf2_cu *);
c906108c 909
c764a876 910static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 911 struct dwarf2_cu *);
72bf9492
DJ
912
913static void fixup_partial_die (struct partial_die_info *,
914 struct dwarf2_cu *);
915
fe1b8b76
JB
916static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
917 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 918
fe1b8b76
JB
919static gdb_byte *read_attribute_value (struct attribute *, unsigned,
920 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 921
fe1b8b76 922static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 923
fe1b8b76 924static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 925
fe1b8b76 926static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 927
fe1b8b76 928static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 929
93311388 930static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 933 unsigned int *);
c906108c 934
c764a876
DE
935static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
936
937static LONGEST read_checked_initial_length_and_offset
938 (bfd *, gdb_byte *, const struct comp_unit_head *,
939 unsigned int *, unsigned int *);
613e1657 940
fe1b8b76 941static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
942 unsigned int *);
943
944static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 945
fe1b8b76 946static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 947
9b1c24c8 948static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 949
fe1b8b76
JB
950static char *read_indirect_string (bfd *, gdb_byte *,
951 const struct comp_unit_head *,
952 unsigned int *);
4bdf3d34 953
fe1b8b76 954static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 955
fe1b8b76 956static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 957
fe1b8b76 958static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 959
e142c38c 960static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 961
e142c38c
DJ
962static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
963 struct dwarf2_cu *);
c906108c 964
348e048f
DE
965static struct attribute *dwarf2_attr_no_follow (struct die_info *,
966 unsigned int,
967 struct dwarf2_cu *);
968
05cf31d1
JB
969static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
970 struct dwarf2_cu *cu);
971
e142c38c 972static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 973
e142c38c 974static struct die_info *die_specification (struct die_info *die,
f2f0e013 975 struct dwarf2_cu **);
63d06c5c 976
debd256d
JB
977static void free_line_header (struct line_header *lh);
978
aaa75496
JB
979static void add_file_name (struct line_header *, char *, unsigned int,
980 unsigned int, unsigned int);
981
debd256d
JB
982static struct line_header *(dwarf_decode_line_header
983 (unsigned int offset,
e7c27a73 984 bfd *abfd, struct dwarf2_cu *cu));
debd256d 985
f3f5162e
DE
986static void dwarf_decode_lines (struct line_header *, const char *,
987 struct dwarf2_cu *, struct partial_symtab *,
988 int);
c906108c 989
72b9f47f 990static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 991
a14ed312 992static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 993 struct dwarf2_cu *);
c906108c 994
34eaf542
TT
995static struct symbol *new_symbol_full (struct die_info *, struct type *,
996 struct dwarf2_cu *, struct symbol *);
997
a14ed312 998static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 999 struct dwarf2_cu *);
c906108c 1000
98bfdba5
PA
1001static void dwarf2_const_value_attr (struct attribute *attr,
1002 struct type *type,
1003 const char *name,
1004 struct obstack *obstack,
1005 struct dwarf2_cu *cu, long *value,
1006 gdb_byte **bytes,
1007 struct dwarf2_locexpr_baton **baton);
2df3850c 1008
e7c27a73 1009static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1010
b4ba55a1
JB
1011static int need_gnat_info (struct dwarf2_cu *);
1012
3e43a32a
MS
1013static struct type *die_descriptive_type (struct die_info *,
1014 struct dwarf2_cu *);
b4ba55a1
JB
1015
1016static void set_descriptive_type (struct type *, struct die_info *,
1017 struct dwarf2_cu *);
1018
e7c27a73
DJ
1019static struct type *die_containing_type (struct die_info *,
1020 struct dwarf2_cu *);
c906108c 1021
673bfd45
DE
1022static struct type *lookup_die_type (struct die_info *, struct attribute *,
1023 struct dwarf2_cu *);
c906108c 1024
f792889a 1025static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1026
673bfd45
DE
1027static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1028
0d5cff50 1029static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1030
6e70227d 1031static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1032 const char *suffix, int physname,
1033 struct dwarf2_cu *cu);
63d06c5c 1034
e7c27a73 1035static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1036
348e048f
DE
1037static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1038
e7c27a73 1039static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1040
e7c27a73 1041static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1042
96408a79
SA
1043static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1044
ff013f42
JK
1045static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1046 struct dwarf2_cu *, struct partial_symtab *);
1047
a14ed312 1048static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1049 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1050 struct partial_symtab *);
c906108c 1051
fae299cd
DC
1052static void get_scope_pc_bounds (struct die_info *,
1053 CORE_ADDR *, CORE_ADDR *,
1054 struct dwarf2_cu *);
1055
801e3a5b
JB
1056static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1057 CORE_ADDR, struct dwarf2_cu *);
1058
a14ed312 1059static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1060 struct dwarf2_cu *);
c906108c 1061
a14ed312 1062static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1063 struct type *, struct dwarf2_cu *);
c906108c 1064
a14ed312 1065static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1066 struct die_info *, struct type *,
e7c27a73 1067 struct dwarf2_cu *);
c906108c 1068
a14ed312 1069static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1070 struct type *,
1071 struct dwarf2_cu *);
c906108c 1072
134d01f1 1073static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1074
e7c27a73 1075static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1076
e7c27a73 1077static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1078
5d7cb8df
JK
1079static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1080
27aa8d6a
SW
1081static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1082
f55ee35c
JK
1083static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1085
38d518c9 1086static const char *namespace_name (struct die_info *die,
e142c38c 1087 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1088
134d01f1 1089static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1090
e7c27a73 1091static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1092
6e70227d 1093static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1094 struct dwarf2_cu *);
1095
93311388 1096static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1097
93311388
DE
1098static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1099 gdb_byte *info_ptr,
d97bc12b
DE
1100 gdb_byte **new_info_ptr,
1101 struct die_info *parent);
1102
93311388
DE
1103static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
fe1b8b76 1105 gdb_byte **new_info_ptr,
639d11d3
DC
1106 struct die_info *parent);
1107
93311388
DE
1108static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1109 gdb_byte *info_ptr,
fe1b8b76 1110 gdb_byte **new_info_ptr,
639d11d3
DC
1111 struct die_info *parent);
1112
93311388
DE
1113static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1114 struct die_info **, gdb_byte *,
1115 int *);
1116
e7c27a73 1117static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1118
71c25dea
TT
1119static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1120 struct obstack *);
1121
e142c38c 1122static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1123
98bfdba5
PA
1124static const char *dwarf2_full_name (char *name,
1125 struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
e142c38c 1128static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1129 struct dwarf2_cu **);
9219021c 1130
a14ed312 1131static char *dwarf_tag_name (unsigned int);
c906108c 1132
a14ed312 1133static char *dwarf_attr_name (unsigned int);
c906108c 1134
a14ed312 1135static char *dwarf_form_name (unsigned int);
c906108c 1136
a14ed312 1137static char *dwarf_bool_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1140
1141#if 0
a14ed312 1142static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1143#endif
1144
f9aca02d 1145static struct die_info *sibling_die (struct die_info *);
c906108c 1146
d97bc12b
DE
1147static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1148
1149static void dump_die_for_error (struct die_info *);
1150
1151static void dump_die_1 (struct ui_file *, int level, int max_level,
1152 struct die_info *);
c906108c 1153
d97bc12b 1154/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1155
51545339 1156static void store_in_ref_table (struct die_info *,
10b3939b 1157 struct dwarf2_cu *);
c906108c 1158
93311388
DE
1159static int is_ref_attr (struct attribute *);
1160
c764a876 1161static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1162
43bbcdc2 1163static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1164
348e048f
DE
1165static struct die_info *follow_die_ref_or_sig (struct die_info *,
1166 struct attribute *,
1167 struct dwarf2_cu **);
1168
10b3939b
DJ
1169static struct die_info *follow_die_ref (struct die_info *,
1170 struct attribute *,
f2f0e013 1171 struct dwarf2_cu **);
c906108c 1172
348e048f
DE
1173static struct die_info *follow_die_sig (struct die_info *,
1174 struct attribute *,
1175 struct dwarf2_cu **);
1176
6c83ed52
TT
1177static struct signatured_type *lookup_signatured_type_at_offset
1178 (struct objfile *objfile,
1179 struct dwarf2_section_info *section,
1180 unsigned int offset);
1181
e5fe5e75 1182static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1183
a0f42c21 1184static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1185
c906108c
SS
1186/* memory allocation interface */
1187
7b5a2f43 1188static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1189
f3dd6933 1190static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1191
b60c80d6 1192static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1193
2e276125 1194static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1195 char *, bfd *, struct dwarf2_cu *,
1196 struct dwarf2_section_info *,
1197 int);
2e276125 1198
8e19ed76
PS
1199static int attr_form_is_block (struct attribute *);
1200
3690dd37
JB
1201static int attr_form_is_section_offset (struct attribute *);
1202
1203static int attr_form_is_constant (struct attribute *);
1204
8cf6f0b1
TT
1205static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1206 struct dwarf2_loclist_baton *baton,
1207 struct attribute *attr);
1208
93e7bd98
DJ
1209static void dwarf2_symbol_mark_computed (struct attribute *attr,
1210 struct symbol *sym,
1211 struct dwarf2_cu *cu);
4c2df51b 1212
93311388
DE
1213static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1214 struct abbrev_info *abbrev,
1215 struct dwarf2_cu *cu);
4bb7a0a7 1216
72bf9492
DJ
1217static void free_stack_comp_unit (void *);
1218
72bf9492
DJ
1219static hashval_t partial_die_hash (const void *item);
1220
1221static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1222
ae038cb0 1223static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1224 (unsigned int offset, struct objfile *objfile);
ae038cb0 1225
9816fde3 1226static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1227 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1228
1229static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1230 struct die_info *comp_unit_die);
93311388 1231
68dc6402 1232static void free_heap_comp_unit (void *);
ae038cb0
DJ
1233
1234static void free_cached_comp_units (void *);
1235
1236static void age_cached_comp_units (void);
1237
1238static void free_one_cached_comp_unit (void *);
1239
f792889a
DJ
1240static struct type *set_die_type (struct die_info *, struct type *,
1241 struct dwarf2_cu *);
1c379e20 1242
ae038cb0
DJ
1243static void create_all_comp_units (struct objfile *);
1244
1fd400ff
TT
1245static int create_debug_types_hash_table (struct objfile *objfile);
1246
a0f42c21 1247static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1248
1249static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1250
1251static void dwarf2_add_dependence (struct dwarf2_cu *,
1252 struct dwarf2_per_cu_data *);
1253
ae038cb0
DJ
1254static void dwarf2_mark (struct dwarf2_cu *);
1255
1256static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1257
673bfd45
DE
1258static struct type *get_die_type_at_offset (unsigned int,
1259 struct dwarf2_per_cu_data *per_cu);
1260
f792889a 1261static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1262
9291a0cd
TT
1263static void dwarf2_release_queue (void *dummy);
1264
a0f42c21 1265static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1266
a0f42c21 1267static void process_queue (void);
9291a0cd
TT
1268
1269static void find_file_and_directory (struct die_info *die,
1270 struct dwarf2_cu *cu,
1271 char **name, char **comp_dir);
1272
1273static char *file_full_name (int file, struct line_header *lh,
1274 const char *comp_dir);
1275
9ff913ba
DE
1276static gdb_byte *read_and_check_comp_unit_head
1277 (struct comp_unit_head *header,
1278 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1279 int is_debug_types_section);
9291a0cd
TT
1280
1281static void init_cu_die_reader (struct die_reader_specs *reader,
1282 struct dwarf2_cu *cu);
1283
673bfd45 1284static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1285
9291a0cd
TT
1286#if WORDS_BIGENDIAN
1287
1288/* Convert VALUE between big- and little-endian. */
1289static offset_type
1290byte_swap (offset_type value)
1291{
1292 offset_type result;
1293
1294 result = (value & 0xff) << 24;
1295 result |= (value & 0xff00) << 8;
1296 result |= (value & 0xff0000) >> 8;
1297 result |= (value & 0xff000000) >> 24;
1298 return result;
1299}
1300
1301#define MAYBE_SWAP(V) byte_swap (V)
1302
1303#else
1304#define MAYBE_SWAP(V) (V)
1305#endif /* WORDS_BIGENDIAN */
1306
1307/* The suffix for an index file. */
1308#define INDEX_SUFFIX ".gdb-index"
1309
3da10d80
KS
1310static const char *dwarf2_physname (char *name, struct die_info *die,
1311 struct dwarf2_cu *cu);
1312
c906108c 1313/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1314 information and return true if we have enough to do something.
1315 NAMES points to the dwarf2 section names, or is NULL if the standard
1316 ELF names are used. */
c906108c
SS
1317
1318int
251d32d9
TG
1319dwarf2_has_info (struct objfile *objfile,
1320 const struct dwarf2_debug_sections *names)
c906108c 1321{
be391dca
TT
1322 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1323 if (!dwarf2_per_objfile)
1324 {
1325 /* Initialize per-objfile state. */
1326 struct dwarf2_per_objfile *data
1327 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1328
be391dca
TT
1329 memset (data, 0, sizeof (*data));
1330 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1331 dwarf2_per_objfile = data;
6502dd73 1332
251d32d9
TG
1333 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1334 (void *) names);
be391dca
TT
1335 dwarf2_per_objfile->objfile = objfile;
1336 }
1337 return (dwarf2_per_objfile->info.asection != NULL
1338 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1339}
1340
251d32d9
TG
1341/* When loading sections, we look either for uncompressed section or for
1342 compressed section names. */
233a11ab
CS
1343
1344static int
251d32d9
TG
1345section_is_p (const char *section_name,
1346 const struct dwarf2_section_names *names)
233a11ab 1347{
251d32d9
TG
1348 if (names->normal != NULL
1349 && strcmp (section_name, names->normal) == 0)
1350 return 1;
1351 if (names->compressed != NULL
1352 && strcmp (section_name, names->compressed) == 0)
1353 return 1;
1354 return 0;
233a11ab
CS
1355}
1356
c906108c
SS
1357/* This function is mapped across the sections and remembers the
1358 offset and size of each of the debugging sections we are interested
1359 in. */
1360
1361static void
251d32d9 1362dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1363{
251d32d9
TG
1364 const struct dwarf2_debug_sections *names;
1365
1366 if (vnames == NULL)
1367 names = &dwarf2_elf_names;
1368 else
1369 names = (const struct dwarf2_debug_sections *) vnames;
1370
1371 if (section_is_p (sectp->name, &names->info))
c906108c 1372 {
dce234bc
PP
1373 dwarf2_per_objfile->info.asection = sectp;
1374 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1375 }
251d32d9 1376 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1377 {
dce234bc
PP
1378 dwarf2_per_objfile->abbrev.asection = sectp;
1379 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1380 }
251d32d9 1381 else if (section_is_p (sectp->name, &names->line))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->line.asection = sectp;
1384 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1385 }
251d32d9 1386 else if (section_is_p (sectp->name, &names->loc))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->loc.asection = sectp;
1389 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1390 }
251d32d9 1391 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->macinfo.asection = sectp;
1394 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1395 }
cf2c3c16
TT
1396 else if (section_is_p (sectp->name, &names->macro))
1397 {
1398 dwarf2_per_objfile->macro.asection = sectp;
1399 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1400 }
251d32d9 1401 else if (section_is_p (sectp->name, &names->str))
c906108c 1402 {
dce234bc
PP
1403 dwarf2_per_objfile->str.asection = sectp;
1404 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1405 }
251d32d9 1406 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1407 {
dce234bc
PP
1408 dwarf2_per_objfile->frame.asection = sectp;
1409 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1410 }
251d32d9 1411 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1412 {
3799ccc6 1413 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1414
3799ccc6
EZ
1415 if (aflag & SEC_HAS_CONTENTS)
1416 {
dce234bc
PP
1417 dwarf2_per_objfile->eh_frame.asection = sectp;
1418 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1419 }
b6af0555 1420 }
251d32d9 1421 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1422 {
dce234bc
PP
1423 dwarf2_per_objfile->ranges.asection = sectp;
1424 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1425 }
251d32d9 1426 else if (section_is_p (sectp->name, &names->types))
348e048f 1427 {
8b70b953
TT
1428 struct dwarf2_section_info type_section;
1429
1430 memset (&type_section, 0, sizeof (type_section));
1431 type_section.asection = sectp;
1432 type_section.size = bfd_get_section_size (sectp);
1433
1434 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1435 &type_section);
348e048f 1436 }
251d32d9 1437 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1438 {
1439 dwarf2_per_objfile->gdb_index.asection = sectp;
1440 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1441 }
dce234bc 1442
72dca2f5
FR
1443 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1444 && bfd_section_vma (abfd, sectp) == 0)
1445 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1446}
1447
dce234bc
PP
1448/* Decompress a section that was compressed using zlib. Store the
1449 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1450
1451static void
dce234bc
PP
1452zlib_decompress_section (struct objfile *objfile, asection *sectp,
1453 gdb_byte **outbuf, bfd_size_type *outsize)
1454{
1455 bfd *abfd = objfile->obfd;
1456#ifndef HAVE_ZLIB_H
1457 error (_("Support for zlib-compressed DWARF data (from '%s') "
1458 "is disabled in this copy of GDB"),
1459 bfd_get_filename (abfd));
1460#else
1461 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1462 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1463 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1464 bfd_size_type uncompressed_size;
1465 gdb_byte *uncompressed_buffer;
1466 z_stream strm;
1467 int rc;
1468 int header_size = 12;
1469
1470 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1471 || bfd_bread (compressed_buffer,
1472 compressed_size, abfd) != compressed_size)
dce234bc
PP
1473 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1474 bfd_get_filename (abfd));
1475
1476 /* Read the zlib header. In this case, it should be "ZLIB" followed
1477 by the uncompressed section size, 8 bytes in big-endian order. */
1478 if (compressed_size < header_size
1479 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1480 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1481 bfd_get_filename (abfd));
1482 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[11];
1490
1491 /* It is possible the section consists of several compressed
1492 buffers concatenated together, so we uncompress in a loop. */
1493 strm.zalloc = NULL;
1494 strm.zfree = NULL;
1495 strm.opaque = NULL;
1496 strm.avail_in = compressed_size - header_size;
1497 strm.next_in = (Bytef*) compressed_buffer + header_size;
1498 strm.avail_out = uncompressed_size;
1499 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1500 uncompressed_size);
1501 rc = inflateInit (&strm);
1502 while (strm.avail_in > 0)
1503 {
1504 if (rc != Z_OK)
1505 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1506 bfd_get_filename (abfd), rc);
1507 strm.next_out = ((Bytef*) uncompressed_buffer
1508 + (uncompressed_size - strm.avail_out));
1509 rc = inflate (&strm, Z_FINISH);
1510 if (rc != Z_STREAM_END)
1511 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1512 bfd_get_filename (abfd), rc);
1513 rc = inflateReset (&strm);
1514 }
1515 rc = inflateEnd (&strm);
1516 if (rc != Z_OK
1517 || strm.avail_out != 0)
1518 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1519 bfd_get_filename (abfd), rc);
1520
affddf13 1521 do_cleanups (cleanup);
dce234bc
PP
1522 *outbuf = uncompressed_buffer;
1523 *outsize = uncompressed_size;
1524#endif
233a11ab
CS
1525}
1526
9e0ac564
TT
1527/* A helper function that decides whether a section is empty. */
1528
1529static int
1530dwarf2_section_empty_p (struct dwarf2_section_info *info)
1531{
1532 return info->asection == NULL || info->size == 0;
1533}
1534
9cdd5dbd 1535/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1536 OBJFILE, store info about the section into INFO.
1537 If the section is compressed, uncompress it before returning. */
c906108c 1538
dce234bc
PP
1539static void
1540dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1541{
dce234bc
PP
1542 bfd *abfd = objfile->obfd;
1543 asection *sectp = info->asection;
1544 gdb_byte *buf, *retbuf;
1545 unsigned char header[4];
c906108c 1546
be391dca
TT
1547 if (info->readin)
1548 return;
dce234bc 1549 info->buffer = NULL;
b315ab21 1550 info->map_addr = NULL;
be391dca 1551 info->readin = 1;
188dd5d6 1552
9e0ac564 1553 if (dwarf2_section_empty_p (info))
dce234bc 1554 return;
c906108c 1555
dce234bc
PP
1556 /* Check if the file has a 4-byte header indicating compression. */
1557 if (info->size > sizeof (header)
1558 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1559 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1560 {
1561 /* Upon decompression, update the buffer and its size. */
1562 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1563 {
1564 zlib_decompress_section (objfile, sectp, &info->buffer,
1565 &info->size);
1566 return;
1567 }
1568 }
4bdf3d34 1569
dce234bc
PP
1570#ifdef HAVE_MMAP
1571 if (pagesize == 0)
1572 pagesize = getpagesize ();
2e276125 1573
dce234bc
PP
1574 /* Only try to mmap sections which are large enough: we don't want to
1575 waste space due to fragmentation. Also, only try mmap for sections
1576 without relocations. */
1577
1578 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1579 {
b315ab21
TG
1580 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1581 MAP_PRIVATE, sectp->filepos,
1582 &info->map_addr, &info->map_len);
dce234bc 1583
b315ab21 1584 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1585 {
be391dca 1586#if HAVE_POSIX_MADVISE
b315ab21 1587 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1588#endif
dce234bc
PP
1589 return;
1590 }
1591 }
1592#endif
1593
1594 /* If we get here, we are a normal, not-compressed section. */
1595 info->buffer = buf
1596 = obstack_alloc (&objfile->objfile_obstack, info->size);
1597
1598 /* When debugging .o files, we may need to apply relocations; see
1599 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1600 We never compress sections in .o files, so we only need to
1601 try this when the section is not compressed. */
ac8035ab 1602 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1603 if (retbuf != NULL)
1604 {
1605 info->buffer = retbuf;
1606 return;
1607 }
1608
1609 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1610 || bfd_bread (buf, info->size, abfd) != info->size)
1611 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1612 bfd_get_filename (abfd));
1613}
1614
9e0ac564
TT
1615/* A helper function that returns the size of a section in a safe way.
1616 If you are positive that the section has been read before using the
1617 size, then it is safe to refer to the dwarf2_section_info object's
1618 "size" field directly. In other cases, you must call this
1619 function, because for compressed sections the size field is not set
1620 correctly until the section has been read. */
1621
1622static bfd_size_type
1623dwarf2_section_size (struct objfile *objfile,
1624 struct dwarf2_section_info *info)
1625{
1626 if (!info->readin)
1627 dwarf2_read_section (objfile, info);
1628 return info->size;
1629}
1630
dce234bc 1631/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1632 SECTION_NAME. */
af34e669 1633
dce234bc 1634void
3017a003
TG
1635dwarf2_get_section_info (struct objfile *objfile,
1636 enum dwarf2_section_enum sect,
dce234bc
PP
1637 asection **sectp, gdb_byte **bufp,
1638 bfd_size_type *sizep)
1639{
1640 struct dwarf2_per_objfile *data
1641 = objfile_data (objfile, dwarf2_objfile_data_key);
1642 struct dwarf2_section_info *info;
a3b2a86b
TT
1643
1644 /* We may see an objfile without any DWARF, in which case we just
1645 return nothing. */
1646 if (data == NULL)
1647 {
1648 *sectp = NULL;
1649 *bufp = NULL;
1650 *sizep = 0;
1651 return;
1652 }
3017a003
TG
1653 switch (sect)
1654 {
1655 case DWARF2_DEBUG_FRAME:
1656 info = &data->frame;
1657 break;
1658 case DWARF2_EH_FRAME:
1659 info = &data->eh_frame;
1660 break;
1661 default:
1662 gdb_assert_not_reached ("unexpected section");
1663 }
dce234bc 1664
9e0ac564 1665 dwarf2_read_section (objfile, info);
dce234bc
PP
1666
1667 *sectp = info->asection;
1668 *bufp = info->buffer;
1669 *sizep = info->size;
1670}
1671
9291a0cd 1672\f
7b9f3c50
DE
1673/* DWARF quick_symbols_functions support. */
1674
1675/* TUs can share .debug_line entries, and there can be a lot more TUs than
1676 unique line tables, so we maintain a separate table of all .debug_line
1677 derived entries to support the sharing.
1678 All the quick functions need is the list of file names. We discard the
1679 line_header when we're done and don't need to record it here. */
1680struct quick_file_names
1681{
1682 /* The offset in .debug_line of the line table. We hash on this. */
1683 unsigned int offset;
1684
1685 /* The number of entries in file_names, real_names. */
1686 unsigned int num_file_names;
1687
1688 /* The file names from the line table, after being run through
1689 file_full_name. */
1690 const char **file_names;
1691
1692 /* The file names from the line table after being run through
1693 gdb_realpath. These are computed lazily. */
1694 const char **real_names;
1695};
1696
1697/* When using the index (and thus not using psymtabs), each CU has an
1698 object of this type. This is used to hold information needed by
1699 the various "quick" methods. */
1700struct dwarf2_per_cu_quick_data
1701{
1702 /* The file table. This can be NULL if there was no file table
1703 or it's currently not read in.
1704 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1705 struct quick_file_names *file_names;
1706
1707 /* The corresponding symbol table. This is NULL if symbols for this
1708 CU have not yet been read. */
1709 struct symtab *symtab;
1710
1711 /* A temporary mark bit used when iterating over all CUs in
1712 expand_symtabs_matching. */
1713 unsigned int mark : 1;
1714
1715 /* True if we've tried to read the file table and found there isn't one.
1716 There will be no point in trying to read it again next time. */
1717 unsigned int no_file_data : 1;
1718};
1719
1720/* Hash function for a quick_file_names. */
1721
1722static hashval_t
1723hash_file_name_entry (const void *e)
1724{
1725 const struct quick_file_names *file_data = e;
1726
1727 return file_data->offset;
1728}
1729
1730/* Equality function for a quick_file_names. */
1731
1732static int
1733eq_file_name_entry (const void *a, const void *b)
1734{
1735 const struct quick_file_names *ea = a;
1736 const struct quick_file_names *eb = b;
1737
1738 return ea->offset == eb->offset;
1739}
1740
1741/* Delete function for a quick_file_names. */
1742
1743static void
1744delete_file_name_entry (void *e)
1745{
1746 struct quick_file_names *file_data = e;
1747 int i;
1748
1749 for (i = 0; i < file_data->num_file_names; ++i)
1750 {
1751 xfree ((void*) file_data->file_names[i]);
1752 if (file_data->real_names)
1753 xfree ((void*) file_data->real_names[i]);
1754 }
1755
1756 /* The space for the struct itself lives on objfile_obstack,
1757 so we don't free it here. */
1758}
1759
1760/* Create a quick_file_names hash table. */
1761
1762static htab_t
1763create_quick_file_names_table (unsigned int nr_initial_entries)
1764{
1765 return htab_create_alloc (nr_initial_entries,
1766 hash_file_name_entry, eq_file_name_entry,
1767 delete_file_name_entry, xcalloc, xfree);
1768}
9291a0cd 1769
918dd910
JK
1770/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1771 have to be created afterwards. You should call age_cached_comp_units after
1772 processing PER_CU->CU. dw2_setup must have been already called. */
1773
1774static void
1775load_cu (struct dwarf2_per_cu_data *per_cu)
1776{
b0df02fd 1777 if (per_cu->debug_types_section)
e5fe5e75 1778 load_full_type_unit (per_cu);
918dd910 1779 else
a0f42c21 1780 load_full_comp_unit (per_cu);
918dd910 1781
918dd910 1782 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1783
1784 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1785}
1786
a0f42c21 1787/* Read in the symbols for PER_CU. */
2fdf6df6 1788
9291a0cd 1789static void
a0f42c21 1790dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1791{
1792 struct cleanup *back_to;
1793
1794 back_to = make_cleanup (dwarf2_release_queue, NULL);
1795
a0f42c21 1796 queue_comp_unit (per_cu);
9291a0cd 1797
918dd910 1798 load_cu (per_cu);
9291a0cd 1799
a0f42c21 1800 process_queue ();
9291a0cd
TT
1801
1802 /* Age the cache, releasing compilation units that have not
1803 been used recently. */
1804 age_cached_comp_units ();
1805
1806 do_cleanups (back_to);
1807}
1808
1809/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1810 the objfile from which this CU came. Returns the resulting symbol
1811 table. */
2fdf6df6 1812
9291a0cd 1813static struct symtab *
a0f42c21 1814dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1815{
1816 if (!per_cu->v.quick->symtab)
1817 {
1818 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1819 increment_reading_symtab ();
a0f42c21 1820 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1821 do_cleanups (back_to);
1822 }
1823 return per_cu->v.quick->symtab;
1824}
1825
1fd400ff 1826/* Return the CU given its index. */
2fdf6df6 1827
1fd400ff
TT
1828static struct dwarf2_per_cu_data *
1829dw2_get_cu (int index)
1830{
1831 if (index >= dwarf2_per_objfile->n_comp_units)
1832 {
1833 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1834 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1835 }
1836 return dwarf2_per_objfile->all_comp_units[index];
1837}
1838
9291a0cd
TT
1839/* A helper function that knows how to read a 64-bit value in a way
1840 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1841 otherwise. */
2fdf6df6 1842
9291a0cd
TT
1843static int
1844extract_cu_value (const char *bytes, ULONGEST *result)
1845{
1846 if (sizeof (ULONGEST) < 8)
1847 {
1848 int i;
1849
1850 /* Ignore the upper 4 bytes if they are all zero. */
1851 for (i = 0; i < 4; ++i)
1852 if (bytes[i + 4] != 0)
1853 return 0;
1854
1855 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1856 }
1857 else
1858 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1859 return 1;
1860}
1861
1862/* Read the CU list from the mapped index, and use it to create all
1863 the CU objects for this objfile. Return 0 if something went wrong,
1864 1 if everything went ok. */
2fdf6df6 1865
9291a0cd 1866static int
1fd400ff
TT
1867create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1868 offset_type cu_list_elements)
9291a0cd
TT
1869{
1870 offset_type i;
9291a0cd
TT
1871
1872 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1873 dwarf2_per_objfile->all_comp_units
1874 = obstack_alloc (&objfile->objfile_obstack,
1875 dwarf2_per_objfile->n_comp_units
1876 * sizeof (struct dwarf2_per_cu_data *));
1877
1878 for (i = 0; i < cu_list_elements; i += 2)
1879 {
1880 struct dwarf2_per_cu_data *the_cu;
1881 ULONGEST offset, length;
1882
1883 if (!extract_cu_value (cu_list, &offset)
1884 || !extract_cu_value (cu_list + 8, &length))
1885 return 0;
1886 cu_list += 2 * 8;
1887
1888 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1889 struct dwarf2_per_cu_data);
1890 the_cu->offset = offset;
1891 the_cu->length = length;
1892 the_cu->objfile = objfile;
1893 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1894 struct dwarf2_per_cu_quick_data);
1895 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1896 }
1897
1898 return 1;
1899}
1900
1fd400ff 1901/* Create the signatured type hash table from the index. */
673bfd45 1902
1fd400ff 1903static int
673bfd45 1904create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1905 struct dwarf2_section_info *section,
673bfd45
DE
1906 const gdb_byte *bytes,
1907 offset_type elements)
1fd400ff
TT
1908{
1909 offset_type i;
673bfd45 1910 htab_t sig_types_hash;
1fd400ff 1911
d467dd73
DE
1912 dwarf2_per_objfile->n_type_units = elements / 3;
1913 dwarf2_per_objfile->all_type_units
1fd400ff 1914 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1915 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1916 * sizeof (struct dwarf2_per_cu_data *));
1917
673bfd45 1918 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1919
1920 for (i = 0; i < elements; i += 3)
1921 {
1922 struct signatured_type *type_sig;
1923 ULONGEST offset, type_offset, signature;
1924 void **slot;
1925
1926 if (!extract_cu_value (bytes, &offset)
1927 || !extract_cu_value (bytes + 8, &type_offset))
1928 return 0;
1929 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1930 bytes += 3 * 8;
1931
1932 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1933 struct signatured_type);
1934 type_sig->signature = signature;
1fd400ff 1935 type_sig->type_offset = type_offset;
b0df02fd 1936 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1937 type_sig->per_cu.offset = offset;
1938 type_sig->per_cu.objfile = objfile;
1939 type_sig->per_cu.v.quick
1940 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1941 struct dwarf2_per_cu_quick_data);
1942
673bfd45 1943 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1944 *slot = type_sig;
1945
d467dd73 1946 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1fd400ff
TT
1947 }
1948
673bfd45 1949 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1950
1951 return 1;
1952}
1953
9291a0cd
TT
1954/* Read the address map data from the mapped index, and use it to
1955 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1956
9291a0cd
TT
1957static void
1958create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1959{
1960 const gdb_byte *iter, *end;
1961 struct obstack temp_obstack;
1962 struct addrmap *mutable_map;
1963 struct cleanup *cleanup;
1964 CORE_ADDR baseaddr;
1965
1966 obstack_init (&temp_obstack);
1967 cleanup = make_cleanup_obstack_free (&temp_obstack);
1968 mutable_map = addrmap_create_mutable (&temp_obstack);
1969
1970 iter = index->address_table;
1971 end = iter + index->address_table_size;
1972
1973 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1974
1975 while (iter < end)
1976 {
1977 ULONGEST hi, lo, cu_index;
1978 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1979 iter += 8;
1980 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1981 iter += 8;
1982 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1983 iter += 4;
1984
1985 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1986 dw2_get_cu (cu_index));
9291a0cd
TT
1987 }
1988
1989 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1990 &objfile->objfile_obstack);
1991 do_cleanups (cleanup);
1992}
1993
59d7bcaf
JK
1994/* The hash function for strings in the mapped index. This is the same as
1995 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1996 implementation. This is necessary because the hash function is tied to the
1997 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
1998 SYMBOL_HASH_NEXT.
1999
2000 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2001
9291a0cd 2002static hashval_t
559a7a62 2003mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2004{
2005 const unsigned char *str = (const unsigned char *) p;
2006 hashval_t r = 0;
2007 unsigned char c;
2008
2009 while ((c = *str++) != 0)
559a7a62
JK
2010 {
2011 if (index_version >= 5)
2012 c = tolower (c);
2013 r = r * 67 + c - 113;
2014 }
9291a0cd
TT
2015
2016 return r;
2017}
2018
2019/* Find a slot in the mapped index INDEX for the object named NAME.
2020 If NAME is found, set *VEC_OUT to point to the CU vector in the
2021 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2022
9291a0cd
TT
2023static int
2024find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2025 offset_type **vec_out)
2026{
0cf03b49
JK
2027 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2028 offset_type hash;
9291a0cd 2029 offset_type slot, step;
559a7a62 2030 int (*cmp) (const char *, const char *);
9291a0cd 2031
0cf03b49
JK
2032 if (current_language->la_language == language_cplus
2033 || current_language->la_language == language_java
2034 || current_language->la_language == language_fortran)
2035 {
2036 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2037 not contain any. */
2038 const char *paren = strchr (name, '(');
2039
2040 if (paren)
2041 {
2042 char *dup;
2043
2044 dup = xmalloc (paren - name + 1);
2045 memcpy (dup, name, paren - name);
2046 dup[paren - name] = 0;
2047
2048 make_cleanup (xfree, dup);
2049 name = dup;
2050 }
2051 }
2052
559a7a62 2053 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2054 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2055 simulate our NAME being searched is also lowercased. */
2056 hash = mapped_index_string_hash ((index->version == 4
2057 && case_sensitivity == case_sensitive_off
2058 ? 5 : index->version),
2059 name);
2060
3876f04e
DE
2061 slot = hash & (index->symbol_table_slots - 1);
2062 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2063 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2064
2065 for (;;)
2066 {
2067 /* Convert a slot number to an offset into the table. */
2068 offset_type i = 2 * slot;
2069 const char *str;
3876f04e 2070 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2071 {
2072 do_cleanups (back_to);
2073 return 0;
2074 }
9291a0cd 2075
3876f04e 2076 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2077 if (!cmp (name, str))
9291a0cd
TT
2078 {
2079 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2080 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2081 do_cleanups (back_to);
9291a0cd
TT
2082 return 1;
2083 }
2084
3876f04e 2085 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2086 }
2087}
2088
2089/* Read the index file. If everything went ok, initialize the "quick"
2090 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2091
9291a0cd
TT
2092static int
2093dwarf2_read_index (struct objfile *objfile)
2094{
9291a0cd
TT
2095 char *addr;
2096 struct mapped_index *map;
b3b272e1 2097 offset_type *metadata;
ac0b195c
KW
2098 const gdb_byte *cu_list;
2099 const gdb_byte *types_list = NULL;
2100 offset_type version, cu_list_elements;
2101 offset_type types_list_elements = 0;
1fd400ff 2102 int i;
9291a0cd 2103
9e0ac564 2104 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2105 return 0;
82430852
JK
2106
2107 /* Older elfutils strip versions could keep the section in the main
2108 executable while splitting it for the separate debug info file. */
2109 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2110 & SEC_HAS_CONTENTS) == 0)
2111 return 0;
2112
9291a0cd
TT
2113 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2114
2115 addr = dwarf2_per_objfile->gdb_index.buffer;
2116 /* Version check. */
1fd400ff 2117 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2118 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2119 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2120 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2121 indices. Index version 4 uses a different hash function than index
2122 version 5 and later. */
831adc1f 2123 if (version < 4)
9291a0cd 2124 return 0;
feea76c2 2125 /* Indices with higher version than the one supported by GDB may be no
594e8718 2126 longer backward compatible. */
559a7a62 2127 if (version > 5)
594e8718 2128 return 0;
9291a0cd
TT
2129
2130 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2131 map->version = version;
b3b272e1 2132 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2133
2134 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2135
2136 i = 0;
2137 cu_list = addr + MAYBE_SWAP (metadata[i]);
2138 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2139 / 8);
1fd400ff
TT
2140 ++i;
2141
987d643c
TT
2142 types_list = addr + MAYBE_SWAP (metadata[i]);
2143 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2144 - MAYBE_SWAP (metadata[i]))
2145 / 8);
2146 ++i;
1fd400ff
TT
2147
2148 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2149 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2150 - MAYBE_SWAP (metadata[i]));
2151 ++i;
2152
3876f04e
DE
2153 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2154 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2155 - MAYBE_SWAP (metadata[i]))
2156 / (2 * sizeof (offset_type)));
1fd400ff 2157 ++i;
9291a0cd 2158
1fd400ff
TT
2159 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2160
2161 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2162 return 0;
2163
8b70b953
TT
2164 if (types_list_elements)
2165 {
2166 struct dwarf2_section_info *section;
2167
2168 /* We can only handle a single .debug_types when we have an
2169 index. */
2170 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2171 return 0;
2172
2173 section = VEC_index (dwarf2_section_info_def,
2174 dwarf2_per_objfile->types, 0);
2175
2176 if (!create_signatured_type_table_from_index (objfile, section,
2177 types_list,
2178 types_list_elements))
2179 return 0;
2180 }
9291a0cd
TT
2181
2182 create_addrmap_from_index (objfile, map);
2183
2184 dwarf2_per_objfile->index_table = map;
2185 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2186 dwarf2_per_objfile->quick_file_names_table =
2187 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2188
2189 return 1;
2190}
2191
2192/* A helper for the "quick" functions which sets the global
2193 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2194
9291a0cd
TT
2195static void
2196dw2_setup (struct objfile *objfile)
2197{
2198 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2199 gdb_assert (dwarf2_per_objfile);
2200}
2201
2202/* A helper for the "quick" functions which attempts to read the line
2203 table for THIS_CU. */
2fdf6df6 2204
7b9f3c50
DE
2205static struct quick_file_names *
2206dw2_get_file_names (struct objfile *objfile,
2207 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2208{
2209 bfd *abfd = objfile->obfd;
7b9f3c50 2210 struct line_header *lh;
9291a0cd
TT
2211 struct attribute *attr;
2212 struct cleanup *cleanups;
2213 struct die_info *comp_unit_die;
36374493 2214 struct dwarf2_section_info* sec;
9ff913ba 2215 gdb_byte *info_ptr;
9291a0cd
TT
2216 int has_children, i;
2217 struct dwarf2_cu cu;
9ff913ba 2218 unsigned int bytes_read;
9291a0cd
TT
2219 struct die_reader_specs reader_specs;
2220 char *name, *comp_dir;
7b9f3c50
DE
2221 void **slot;
2222 struct quick_file_names *qfn;
2223 unsigned int line_offset;
9291a0cd 2224
7b9f3c50
DE
2225 if (this_cu->v.quick->file_names != NULL)
2226 return this_cu->v.quick->file_names;
2227 /* If we know there is no line data, no point in looking again. */
2228 if (this_cu->v.quick->no_file_data)
2229 return NULL;
9291a0cd 2230
23745b47 2231 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2232 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2233
b0df02fd
DE
2234 if (this_cu->debug_types_section)
2235 sec = this_cu->debug_types_section;
36374493
DE
2236 else
2237 sec = &dwarf2_per_objfile->info;
2238 dwarf2_read_section (objfile, sec);
9ff913ba 2239 info_ptr = sec->buffer + this_cu->offset;
9291a0cd 2240
9ff913ba
DE
2241 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2242 this_cu->debug_types_section != NULL);
9291a0cd 2243
6caca83c 2244 /* Skip dummy compilation units. */
9ff913ba 2245 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2246 || peek_abbrev_code (abfd, info_ptr) == 0)
2247 {
2248 do_cleanups (cleanups);
2249 return NULL;
2250 }
2251
e5fe5e75 2252 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2253 make_cleanup (dwarf2_free_abbrev_table, &cu);
2254
9291a0cd 2255 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2256 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2257 &has_children);
9291a0cd 2258
7b9f3c50
DE
2259 lh = NULL;
2260 slot = NULL;
2261 line_offset = 0;
9291a0cd
TT
2262 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2263 if (attr)
2264 {
7b9f3c50
DE
2265 struct quick_file_names find_entry;
2266
2267 line_offset = DW_UNSND (attr);
2268
2269 /* We may have already read in this line header (TU line header sharing).
2270 If we have we're done. */
2271 find_entry.offset = line_offset;
2272 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2273 &find_entry, INSERT);
2274 if (*slot != NULL)
2275 {
2276 do_cleanups (cleanups);
2277 this_cu->v.quick->file_names = *slot;
2278 return *slot;
2279 }
2280
9291a0cd
TT
2281 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2282 }
2283 if (lh == NULL)
2284 {
2285 do_cleanups (cleanups);
7b9f3c50
DE
2286 this_cu->v.quick->no_file_data = 1;
2287 return NULL;
9291a0cd
TT
2288 }
2289
7b9f3c50
DE
2290 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2291 qfn->offset = line_offset;
2292 gdb_assert (slot != NULL);
2293 *slot = qfn;
9291a0cd 2294
7b9f3c50 2295 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2296
7b9f3c50
DE
2297 qfn->num_file_names = lh->num_file_names;
2298 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2299 lh->num_file_names * sizeof (char *));
9291a0cd 2300 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2301 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2302 qfn->real_names = NULL;
9291a0cd 2303
7b9f3c50 2304 free_line_header (lh);
9291a0cd 2305 do_cleanups (cleanups);
7b9f3c50
DE
2306
2307 this_cu->v.quick->file_names = qfn;
2308 return qfn;
9291a0cd
TT
2309}
2310
2311/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2312 real path for a given file name from the line table. */
2fdf6df6 2313
9291a0cd 2314static const char *
7b9f3c50
DE
2315dw2_get_real_path (struct objfile *objfile,
2316 struct quick_file_names *qfn, int index)
9291a0cd 2317{
7b9f3c50
DE
2318 if (qfn->real_names == NULL)
2319 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2320 qfn->num_file_names, sizeof (char *));
9291a0cd 2321
7b9f3c50
DE
2322 if (qfn->real_names[index] == NULL)
2323 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2324
7b9f3c50 2325 return qfn->real_names[index];
9291a0cd
TT
2326}
2327
2328static struct symtab *
2329dw2_find_last_source_symtab (struct objfile *objfile)
2330{
2331 int index;
ae2de4f8 2332
9291a0cd
TT
2333 dw2_setup (objfile);
2334 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2335 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2336}
2337
7b9f3c50
DE
2338/* Traversal function for dw2_forget_cached_source_info. */
2339
2340static int
2341dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2342{
7b9f3c50 2343 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2344
7b9f3c50 2345 if (file_data->real_names)
9291a0cd 2346 {
7b9f3c50 2347 int i;
9291a0cd 2348
7b9f3c50 2349 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2350 {
7b9f3c50
DE
2351 xfree ((void*) file_data->real_names[i]);
2352 file_data->real_names[i] = NULL;
9291a0cd
TT
2353 }
2354 }
7b9f3c50
DE
2355
2356 return 1;
2357}
2358
2359static void
2360dw2_forget_cached_source_info (struct objfile *objfile)
2361{
2362 dw2_setup (objfile);
2363
2364 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2365 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2366}
2367
f8eba3c6
TT
2368/* Helper function for dw2_map_symtabs_matching_filename that expands
2369 the symtabs and calls the iterator. */
2370
2371static int
2372dw2_map_expand_apply (struct objfile *objfile,
2373 struct dwarf2_per_cu_data *per_cu,
2374 const char *name,
2375 const char *full_path, const char *real_path,
2376 int (*callback) (struct symtab *, void *),
2377 void *data)
2378{
2379 struct symtab *last_made = objfile->symtabs;
2380
2381 /* Don't visit already-expanded CUs. */
2382 if (per_cu->v.quick->symtab)
2383 return 0;
2384
2385 /* This may expand more than one symtab, and we want to iterate over
2386 all of them. */
a0f42c21 2387 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2388
2389 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2390 objfile->symtabs, last_made);
2391}
2392
2393/* Implementation of the map_symtabs_matching_filename method. */
2394
9291a0cd 2395static int
f8eba3c6
TT
2396dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2397 const char *full_path, const char *real_path,
2398 int (*callback) (struct symtab *, void *),
2399 void *data)
9291a0cd
TT
2400{
2401 int i;
c011a4f4 2402 const char *name_basename = lbasename (name);
4aac40c8
TT
2403 int name_len = strlen (name);
2404 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2405
2406 dw2_setup (objfile);
ae2de4f8 2407
1fd400ff 2408 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2409 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2410 {
2411 int j;
e254ef6a 2412 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2413 struct quick_file_names *file_data;
9291a0cd 2414
3d7bb9d9 2415 /* We only need to look at symtabs not already expanded. */
e254ef6a 2416 if (per_cu->v.quick->symtab)
9291a0cd
TT
2417 continue;
2418
7b9f3c50
DE
2419 file_data = dw2_get_file_names (objfile, per_cu);
2420 if (file_data == NULL)
9291a0cd
TT
2421 continue;
2422
7b9f3c50 2423 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2424 {
7b9f3c50 2425 const char *this_name = file_data->file_names[j];
9291a0cd 2426
4aac40c8
TT
2427 if (FILENAME_CMP (name, this_name) == 0
2428 || (!is_abs && compare_filenames_for_search (this_name,
2429 name, name_len)))
9291a0cd 2430 {
f8eba3c6
TT
2431 if (dw2_map_expand_apply (objfile, per_cu,
2432 name, full_path, real_path,
2433 callback, data))
2434 return 1;
4aac40c8 2435 }
9291a0cd 2436
c011a4f4
DE
2437 /* Before we invoke realpath, which can get expensive when many
2438 files are involved, do a quick comparison of the basenames. */
2439 if (! basenames_may_differ
2440 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2441 continue;
2442
9291a0cd
TT
2443 if (full_path != NULL)
2444 {
7b9f3c50
DE
2445 const char *this_real_name = dw2_get_real_path (objfile,
2446 file_data, j);
9291a0cd 2447
7b9f3c50 2448 if (this_real_name != NULL
4aac40c8
TT
2449 && (FILENAME_CMP (full_path, this_real_name) == 0
2450 || (!is_abs
2451 && compare_filenames_for_search (this_real_name,
2452 name, name_len))))
9291a0cd 2453 {
f8eba3c6
TT
2454 if (dw2_map_expand_apply (objfile, per_cu,
2455 name, full_path, real_path,
2456 callback, data))
2457 return 1;
9291a0cd
TT
2458 }
2459 }
2460
2461 if (real_path != NULL)
2462 {
7b9f3c50
DE
2463 const char *this_real_name = dw2_get_real_path (objfile,
2464 file_data, j);
9291a0cd 2465
7b9f3c50 2466 if (this_real_name != NULL
4aac40c8
TT
2467 && (FILENAME_CMP (real_path, this_real_name) == 0
2468 || (!is_abs
2469 && compare_filenames_for_search (this_real_name,
2470 name, name_len))))
9291a0cd 2471 {
f8eba3c6
TT
2472 if (dw2_map_expand_apply (objfile, per_cu,
2473 name, full_path, real_path,
2474 callback, data))
2475 return 1;
9291a0cd
TT
2476 }
2477 }
2478 }
2479 }
2480
9291a0cd
TT
2481 return 0;
2482}
2483
2484static struct symtab *
2485dw2_lookup_symbol (struct objfile *objfile, int block_index,
2486 const char *name, domain_enum domain)
2487{
774b6a14 2488 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2489 instead. */
2490 return NULL;
2491}
2492
2493/* A helper function that expands all symtabs that hold an object
2494 named NAME. */
2fdf6df6 2495
9291a0cd
TT
2496static void
2497dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2498{
2499 dw2_setup (objfile);
2500
ae2de4f8 2501 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2502 if (dwarf2_per_objfile->index_table)
2503 {
2504 offset_type *vec;
2505
2506 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2507 name, &vec))
2508 {
2509 offset_type i, len = MAYBE_SWAP (*vec);
2510 for (i = 0; i < len; ++i)
2511 {
2512 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2513 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2514
a0f42c21 2515 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2516 }
2517 }
2518 }
2519}
2520
774b6a14
TT
2521static void
2522dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2523 enum block_enum block_kind, const char *name,
774b6a14 2524 domain_enum domain)
9291a0cd 2525{
774b6a14 2526 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2527}
2528
2529static void
2530dw2_print_stats (struct objfile *objfile)
2531{
2532 int i, count;
2533
2534 dw2_setup (objfile);
2535 count = 0;
1fd400ff 2536 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2537 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2538 {
e254ef6a 2539 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2540
e254ef6a 2541 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2542 ++count;
2543 }
2544 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2545}
2546
2547static void
2548dw2_dump (struct objfile *objfile)
2549{
2550 /* Nothing worth printing. */
2551}
2552
2553static void
2554dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2555 struct section_offsets *delta)
2556{
2557 /* There's nothing to relocate here. */
2558}
2559
2560static void
2561dw2_expand_symtabs_for_function (struct objfile *objfile,
2562 const char *func_name)
2563{
2564 dw2_do_expand_symtabs_matching (objfile, func_name);
2565}
2566
2567static void
2568dw2_expand_all_symtabs (struct objfile *objfile)
2569{
2570 int i;
2571
2572 dw2_setup (objfile);
1fd400ff
TT
2573
2574 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2575 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2576 {
e254ef6a 2577 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2578
a0f42c21 2579 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2580 }
2581}
2582
2583static void
2584dw2_expand_symtabs_with_filename (struct objfile *objfile,
2585 const char *filename)
2586{
2587 int i;
2588
2589 dw2_setup (objfile);
d4637a04
DE
2590
2591 /* We don't need to consider type units here.
2592 This is only called for examining code, e.g. expand_line_sal.
2593 There can be an order of magnitude (or more) more type units
2594 than comp units, and we avoid them if we can. */
2595
2596 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2597 {
2598 int j;
e254ef6a 2599 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2600 struct quick_file_names *file_data;
9291a0cd 2601
3d7bb9d9 2602 /* We only need to look at symtabs not already expanded. */
e254ef6a 2603 if (per_cu->v.quick->symtab)
9291a0cd
TT
2604 continue;
2605
7b9f3c50
DE
2606 file_data = dw2_get_file_names (objfile, per_cu);
2607 if (file_data == NULL)
9291a0cd
TT
2608 continue;
2609
7b9f3c50 2610 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2611 {
7b9f3c50 2612 const char *this_name = file_data->file_names[j];
1ef75ecc 2613 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2614 {
a0f42c21 2615 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2616 break;
2617 }
2618 }
2619 }
2620}
2621
dd786858 2622static const char *
9291a0cd
TT
2623dw2_find_symbol_file (struct objfile *objfile, const char *name)
2624{
e254ef6a 2625 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2626 offset_type *vec;
7b9f3c50 2627 struct quick_file_names *file_data;
9291a0cd
TT
2628
2629 dw2_setup (objfile);
2630
ae2de4f8 2631 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2632 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2633 {
2634 struct symtab *s;
2635
2636 ALL_OBJFILE_SYMTABS (objfile, s)
2637 if (s->primary)
2638 {
2639 struct blockvector *bv = BLOCKVECTOR (s);
2640 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2641 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2642
2643 if (sym)
2644 return sym->symtab->filename;
2645 }
2646 return NULL;
2647 }
9291a0cd
TT
2648
2649 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2650 name, &vec))
2651 return NULL;
2652
2653 /* Note that this just looks at the very first one named NAME -- but
2654 actually we are looking for a function. find_main_filename
2655 should be rewritten so that it doesn't require a custom hook. It
2656 could just use the ordinary symbol tables. */
2657 /* vec[0] is the length, which must always be >0. */
e254ef6a 2658 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2659
7b9f3c50
DE
2660 file_data = dw2_get_file_names (objfile, per_cu);
2661 if (file_data == NULL)
9291a0cd
TT
2662 return NULL;
2663
7b9f3c50 2664 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2665}
2666
2667static void
40658b94
PH
2668dw2_map_matching_symbols (const char * name, domain_enum namespace,
2669 struct objfile *objfile, int global,
2670 int (*callback) (struct block *,
2671 struct symbol *, void *),
2edb89d3
JK
2672 void *data, symbol_compare_ftype *match,
2673 symbol_compare_ftype *ordered_compare)
9291a0cd 2674{
40658b94 2675 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2676 current language is Ada for a non-Ada objfile using GNU index. As Ada
2677 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2678}
2679
2680static void
f8eba3c6
TT
2681dw2_expand_symtabs_matching
2682 (struct objfile *objfile,
2683 int (*file_matcher) (const char *, void *),
e078317b 2684 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2685 enum search_domain kind,
2686 void *data)
9291a0cd
TT
2687{
2688 int i;
2689 offset_type iter;
4b5246aa 2690 struct mapped_index *index;
9291a0cd
TT
2691
2692 dw2_setup (objfile);
ae2de4f8
DE
2693
2694 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2695 if (!dwarf2_per_objfile->index_table)
2696 return;
4b5246aa 2697 index = dwarf2_per_objfile->index_table;
9291a0cd 2698
7b08b9eb 2699 if (file_matcher != NULL)
24c79950
TT
2700 {
2701 struct cleanup *cleanup;
2702 htab_t visited_found, visited_not_found;
2703
2704 visited_found = htab_create_alloc (10,
2705 htab_hash_pointer, htab_eq_pointer,
2706 NULL, xcalloc, xfree);
2707 cleanup = make_cleanup_htab_delete (visited_found);
2708 visited_not_found = htab_create_alloc (10,
2709 htab_hash_pointer, htab_eq_pointer,
2710 NULL, xcalloc, xfree);
2711 make_cleanup_htab_delete (visited_not_found);
2712
2713 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2714 + dwarf2_per_objfile->n_type_units); ++i)
2715 {
2716 int j;
2717 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2718 struct quick_file_names *file_data;
2719 void **slot;
7b08b9eb 2720
24c79950 2721 per_cu->v.quick->mark = 0;
3d7bb9d9 2722
24c79950
TT
2723 /* We only need to look at symtabs not already expanded. */
2724 if (per_cu->v.quick->symtab)
2725 continue;
7b08b9eb 2726
24c79950
TT
2727 file_data = dw2_get_file_names (objfile, per_cu);
2728 if (file_data == NULL)
2729 continue;
7b08b9eb 2730
24c79950
TT
2731 if (htab_find (visited_not_found, file_data) != NULL)
2732 continue;
2733 else if (htab_find (visited_found, file_data) != NULL)
2734 {
2735 per_cu->v.quick->mark = 1;
2736 continue;
2737 }
2738
2739 for (j = 0; j < file_data->num_file_names; ++j)
2740 {
2741 if (file_matcher (file_data->file_names[j], data))
2742 {
2743 per_cu->v.quick->mark = 1;
2744 break;
2745 }
2746 }
2747
2748 slot = htab_find_slot (per_cu->v.quick->mark
2749 ? visited_found
2750 : visited_not_found,
2751 file_data, INSERT);
2752 *slot = file_data;
2753 }
2754
2755 do_cleanups (cleanup);
2756 }
9291a0cd 2757
3876f04e 2758 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2759 {
2760 offset_type idx = 2 * iter;
2761 const char *name;
2762 offset_type *vec, vec_len, vec_idx;
2763
3876f04e 2764 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2765 continue;
2766
3876f04e 2767 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2768
e078317b 2769 if (! (*name_matcher) (name, data))
9291a0cd
TT
2770 continue;
2771
2772 /* The name was matched, now expand corresponding CUs that were
2773 marked. */
4b5246aa 2774 vec = (offset_type *) (index->constant_pool
3876f04e 2775 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2776 vec_len = MAYBE_SWAP (vec[0]);
2777 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2778 {
e254ef6a 2779 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2780
e254ef6a 2781 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2782 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2783 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2784 }
2785 }
2786}
2787
2788static struct symtab *
2789dw2_find_pc_sect_symtab (struct objfile *objfile,
2790 struct minimal_symbol *msymbol,
2791 CORE_ADDR pc,
2792 struct obj_section *section,
2793 int warn_if_readin)
2794{
2795 struct dwarf2_per_cu_data *data;
2796
2797 dw2_setup (objfile);
2798
2799 if (!objfile->psymtabs_addrmap)
2800 return NULL;
2801
2802 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2803 if (!data)
2804 return NULL;
2805
2806 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2807 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2808 paddress (get_objfile_arch (objfile), pc));
2809
a0f42c21 2810 return dw2_instantiate_symtab (data);
9291a0cd
TT
2811}
2812
9291a0cd 2813static void
44b13c5a 2814dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2815 void *data, int need_fullname)
9291a0cd
TT
2816{
2817 int i;
24c79950
TT
2818 struct cleanup *cleanup;
2819 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2820 NULL, xcalloc, xfree);
9291a0cd 2821
24c79950 2822 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2823 dw2_setup (objfile);
ae2de4f8 2824
24c79950
TT
2825 /* We can ignore file names coming from already-expanded CUs. */
2826 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2827 + dwarf2_per_objfile->n_type_units); ++i)
2828 {
2829 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2830
2831 if (per_cu->v.quick->symtab)
2832 {
2833 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2834 INSERT);
2835
2836 *slot = per_cu->v.quick->file_names;
2837 }
2838 }
2839
1fd400ff 2840 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2841 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2842 {
2843 int j;
e254ef6a 2844 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2845 struct quick_file_names *file_data;
24c79950 2846 void **slot;
9291a0cd 2847
3d7bb9d9 2848 /* We only need to look at symtabs not already expanded. */
e254ef6a 2849 if (per_cu->v.quick->symtab)
9291a0cd
TT
2850 continue;
2851
7b9f3c50
DE
2852 file_data = dw2_get_file_names (objfile, per_cu);
2853 if (file_data == NULL)
9291a0cd
TT
2854 continue;
2855
24c79950
TT
2856 slot = htab_find_slot (visited, file_data, INSERT);
2857 if (*slot)
2858 {
2859 /* Already visited. */
2860 continue;
2861 }
2862 *slot = file_data;
2863
7b9f3c50 2864 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2865 {
74e2f255
DE
2866 const char *this_real_name;
2867
2868 if (need_fullname)
2869 this_real_name = dw2_get_real_path (objfile, file_data, j);
2870 else
2871 this_real_name = NULL;
7b9f3c50 2872 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2873 }
2874 }
24c79950
TT
2875
2876 do_cleanups (cleanup);
9291a0cd
TT
2877}
2878
2879static int
2880dw2_has_symbols (struct objfile *objfile)
2881{
2882 return 1;
2883}
2884
2885const struct quick_symbol_functions dwarf2_gdb_index_functions =
2886{
2887 dw2_has_symbols,
2888 dw2_find_last_source_symtab,
2889 dw2_forget_cached_source_info,
f8eba3c6 2890 dw2_map_symtabs_matching_filename,
9291a0cd 2891 dw2_lookup_symbol,
774b6a14 2892 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2893 dw2_print_stats,
2894 dw2_dump,
2895 dw2_relocate,
2896 dw2_expand_symtabs_for_function,
2897 dw2_expand_all_symtabs,
2898 dw2_expand_symtabs_with_filename,
2899 dw2_find_symbol_file,
40658b94 2900 dw2_map_matching_symbols,
9291a0cd
TT
2901 dw2_expand_symtabs_matching,
2902 dw2_find_pc_sect_symtab,
9291a0cd
TT
2903 dw2_map_symbol_filenames
2904};
2905
2906/* Initialize for reading DWARF for this objfile. Return 0 if this
2907 file will use psymtabs, or 1 if using the GNU index. */
2908
2909int
2910dwarf2_initialize_objfile (struct objfile *objfile)
2911{
2912 /* If we're about to read full symbols, don't bother with the
2913 indices. In this case we also don't care if some other debug
2914 format is making psymtabs, because they are all about to be
2915 expanded anyway. */
2916 if ((objfile->flags & OBJF_READNOW))
2917 {
2918 int i;
2919
2920 dwarf2_per_objfile->using_index = 1;
2921 create_all_comp_units (objfile);
1fd400ff 2922 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2923 dwarf2_per_objfile->quick_file_names_table =
2924 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2925
1fd400ff 2926 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2927 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2928 {
e254ef6a 2929 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2930
e254ef6a
DE
2931 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2932 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2933 }
2934
2935 /* Return 1 so that gdb sees the "quick" functions. However,
2936 these functions will be no-ops because we will have expanded
2937 all symtabs. */
2938 return 1;
2939 }
2940
2941 if (dwarf2_read_index (objfile))
2942 return 1;
2943
9291a0cd
TT
2944 return 0;
2945}
2946
2947\f
2948
dce234bc
PP
2949/* Build a partial symbol table. */
2950
2951void
f29dff0a 2952dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2953{
f29dff0a 2954 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2955 {
2956 init_psymbol_list (objfile, 1024);
2957 }
2958
d146bf1e 2959 dwarf2_build_psymtabs_hard (objfile);
c906108c 2960}
c906108c 2961
45452591
DE
2962/* Return TRUE if OFFSET is within CU_HEADER. */
2963
2964static inline int
2965offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2966{
2967 unsigned int bottom = cu_header->offset;
2968 unsigned int top = (cu_header->offset
2969 + cu_header->length
2970 + cu_header->initial_length_size);
9a619af0 2971
45452591
DE
2972 return (offset >= bottom && offset < top);
2973}
2974
93311388
DE
2975/* Read in the comp unit header information from the debug_info at info_ptr.
2976 NOTE: This leaves members offset, first_die_offset to be filled in
2977 by the caller. */
107d2387 2978
fe1b8b76 2979static gdb_byte *
107d2387 2980read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2981 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2982{
2983 int signed_addr;
891d2f0b 2984 unsigned int bytes_read;
c764a876
DE
2985
2986 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2987 cu_header->initial_length_size = bytes_read;
2988 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2989 info_ptr += bytes_read;
107d2387
AC
2990 cu_header->version = read_2_bytes (abfd, info_ptr);
2991 info_ptr += 2;
613e1657 2992 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2993 &bytes_read);
613e1657 2994 info_ptr += bytes_read;
107d2387
AC
2995 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2996 info_ptr += 1;
2997 signed_addr = bfd_get_sign_extend_vma (abfd);
2998 if (signed_addr < 0)
8e65ff28 2999 internal_error (__FILE__, __LINE__,
e2e0b3e5 3000 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3001 cu_header->signed_addr_p = signed_addr;
c764a876 3002
107d2387
AC
3003 return info_ptr;
3004}
3005
9ff913ba
DE
3006/* Subroutine of read_and_check_comp_unit_head and
3007 read_and_check_type_unit_head to simplify them.
3008 Perform various error checking on the header. */
3009
3010static void
3011error_check_comp_unit_head (struct comp_unit_head *header,
3012 struct dwarf2_section_info *section)
3013{
3014 bfd *abfd = section->asection->owner;
3015 const char *filename = bfd_get_filename (abfd);
3016
3017 if (header->version != 2 && header->version != 3 && header->version != 4)
3018 error (_("Dwarf Error: wrong version in compilation unit header "
3019 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3020 filename);
3021
3022 if (header->abbrev_offset
3023 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3024 &dwarf2_per_objfile->abbrev))
3025 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3026 "(offset 0x%lx + 6) [in module %s]"),
3027 (long) header->abbrev_offset, (long) header->offset,
3028 filename);
3029
3030 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3031 avoid potential 32-bit overflow. */
3032 if (((unsigned long) header->offset
3033 + header->length + header->initial_length_size)
3034 > section->size)
3035 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3036 "(offset 0x%lx + 0) [in module %s]"),
3037 (long) header->length, (long) header->offset,
3038 filename);
3039}
3040
3041/* Read in a CU/TU header and perform some basic error checking.
3042 The contents of the header are stored in HEADER.
3043 The result is a pointer to the start of the first DIE. */
adabb602 3044
fe1b8b76 3045static gdb_byte *
9ff913ba
DE
3046read_and_check_comp_unit_head (struct comp_unit_head *header,
3047 struct dwarf2_section_info *section,
3048 gdb_byte *info_ptr,
3049 int is_debug_types_section)
72bf9492 3050{
fe1b8b76 3051 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3052 bfd *abfd = section->asection->owner;
72bf9492 3053
9ff913ba 3054 header->offset = beg_of_comp_unit - section->buffer;
adabb602 3055
72bf9492
DJ
3056 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3057
460c1c54
CC
3058 /* If we're reading a type unit, skip over the signature and
3059 type_offset fields. */
b0df02fd 3060 if (is_debug_types_section)
460c1c54
CC
3061 info_ptr += 8 /*signature*/ + header->offset_size;
3062
adabb602
DE
3063 header->first_die_offset = info_ptr - beg_of_comp_unit;
3064
9ff913ba 3065 error_check_comp_unit_head (header, section);
72bf9492
DJ
3066
3067 return info_ptr;
3068}
3069
348e048f
DE
3070/* Read in the types comp unit header information from .debug_types entry at
3071 types_ptr. The result is a pointer to one past the end of the header. */
3072
3073static gdb_byte *
9ff913ba
DE
3074read_and_check_type_unit_head (struct comp_unit_head *header,
3075 struct dwarf2_section_info *section,
3076 gdb_byte *info_ptr,
3077 ULONGEST *signature, unsigned int *type_offset)
348e048f 3078{
9ff913ba
DE
3079 gdb_byte *beg_of_comp_unit = info_ptr;
3080 bfd *abfd = section->asection->owner;
348e048f 3081
9ff913ba 3082 header->offset = beg_of_comp_unit - section->buffer;
348e048f 3083
9ff913ba 3084 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3085
9ff913ba
DE
3086 /* If we're reading a type unit, skip over the signature and
3087 type_offset fields. */
3088 if (signature != NULL)
3089 *signature = read_8_bytes (abfd, info_ptr);
3090 info_ptr += 8;
3091 if (type_offset != NULL)
3092 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3093 info_ptr += header->offset_size;
3094
3095 header->first_die_offset = info_ptr - beg_of_comp_unit;
348e048f 3096
9ff913ba
DE
3097 error_check_comp_unit_head (header, section);
3098
3099 return info_ptr;
348e048f
DE
3100}
3101
aaa75496
JB
3102/* Allocate a new partial symtab for file named NAME and mark this new
3103 partial symtab as being an include of PST. */
3104
3105static void
3106dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3107 struct objfile *objfile)
3108{
3109 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3110
3111 subpst->section_offsets = pst->section_offsets;
3112 subpst->textlow = 0;
3113 subpst->texthigh = 0;
3114
3115 subpst->dependencies = (struct partial_symtab **)
3116 obstack_alloc (&objfile->objfile_obstack,
3117 sizeof (struct partial_symtab *));
3118 subpst->dependencies[0] = pst;
3119 subpst->number_of_dependencies = 1;
3120
3121 subpst->globals_offset = 0;
3122 subpst->n_global_syms = 0;
3123 subpst->statics_offset = 0;
3124 subpst->n_static_syms = 0;
3125 subpst->symtab = NULL;
3126 subpst->read_symtab = pst->read_symtab;
3127 subpst->readin = 0;
3128
3129 /* No private part is necessary for include psymtabs. This property
3130 can be used to differentiate between such include psymtabs and
10b3939b 3131 the regular ones. */
58a9656e 3132 subpst->read_symtab_private = NULL;
aaa75496
JB
3133}
3134
3135/* Read the Line Number Program data and extract the list of files
3136 included by the source file represented by PST. Build an include
d85a05f0 3137 partial symtab for each of these included files. */
aaa75496
JB
3138
3139static void
3140dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3141 struct die_info *die,
aaa75496
JB
3142 struct partial_symtab *pst)
3143{
3144 struct objfile *objfile = cu->objfile;
3145 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3146 struct line_header *lh = NULL;
3147 struct attribute *attr;
aaa75496 3148
d85a05f0
DJ
3149 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3150 if (attr)
3151 {
3152 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3153
d85a05f0
DJ
3154 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3155 }
aaa75496
JB
3156 if (lh == NULL)
3157 return; /* No linetable, so no includes. */
3158
c6da4cef 3159 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3160 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3161
3162 free_line_header (lh);
3163}
3164
348e048f
DE
3165static hashval_t
3166hash_type_signature (const void *item)
3167{
3168 const struct signatured_type *type_sig = item;
9a619af0 3169
348e048f
DE
3170 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3171 return type_sig->signature;
3172}
3173
3174static int
3175eq_type_signature (const void *item_lhs, const void *item_rhs)
3176{
3177 const struct signatured_type *lhs = item_lhs;
3178 const struct signatured_type *rhs = item_rhs;
9a619af0 3179
348e048f
DE
3180 return lhs->signature == rhs->signature;
3181}
3182
1fd400ff
TT
3183/* Allocate a hash table for signatured types. */
3184
3185static htab_t
673bfd45 3186allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3187{
3188 return htab_create_alloc_ex (41,
3189 hash_type_signature,
3190 eq_type_signature,
3191 NULL,
3192 &objfile->objfile_obstack,
3193 hashtab_obstack_allocate,
3194 dummy_obstack_deallocate);
3195}
3196
d467dd73 3197/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3198
3199static int
d467dd73 3200add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3201{
3202 struct signatured_type *sigt = *slot;
3203 struct dwarf2_per_cu_data ***datap = datum;
3204
3205 **datap = &sigt->per_cu;
3206 ++*datap;
3207
3208 return 1;
3209}
3210
d467dd73 3211/* Create the hash table of all entries in the .debug_types section(s).
348e048f
DE
3212 The result is zero if there is an error (e.g. missing .debug_types section),
3213 otherwise non-zero. */
3214
3215static int
3216create_debug_types_hash_table (struct objfile *objfile)
3217{
8b70b953 3218 htab_t types_htab = NULL;
1fd400ff 3219 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3220 int ix;
3221 struct dwarf2_section_info *section;
348e048f 3222
8b70b953 3223 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3224 {
3225 dwarf2_per_objfile->signatured_types = NULL;
3226 return 0;
3227 }
3228
8b70b953
TT
3229 for (ix = 0;
3230 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3231 ix, section);
3232 ++ix)
3233 {
3234 gdb_byte *info_ptr, *end_ptr;
348e048f 3235
8b70b953
TT
3236 dwarf2_read_section (objfile, section);
3237 info_ptr = section->buffer;
348e048f 3238
8b70b953
TT
3239 if (info_ptr == NULL)
3240 continue;
348e048f 3241
8b70b953
TT
3242 if (types_htab == NULL)
3243 types_htab = allocate_signatured_type_table (objfile);
348e048f 3244
8b70b953
TT
3245 if (dwarf2_die_debug)
3246 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3247
3248 end_ptr = info_ptr + section->size;
3249 while (info_ptr < end_ptr)
3250 {
3251 unsigned int offset;
8b70b953 3252 unsigned int type_offset;
8b70b953
TT
3253 ULONGEST signature;
3254 struct signatured_type *type_sig;
3255 void **slot;
3256 gdb_byte *ptr = info_ptr;
9ff913ba 3257 struct comp_unit_head header;
348e048f 3258
8b70b953 3259 offset = ptr - section->buffer;
348e048f 3260
8b70b953 3261 /* We need to read the type's signature in order to build the hash
9ff913ba 3262 table, but we don't need anything else just yet. */
348e048f 3263
9ff913ba
DE
3264 ptr = read_and_check_type_unit_head (&header, section, ptr,
3265 &signature, &type_offset);
6caca83c
CC
3266
3267 /* Skip dummy type units. */
3268 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3269 {
9ff913ba 3270 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3271 continue;
3272 }
8b70b953
TT
3273
3274 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3275 memset (type_sig, 0, sizeof (*type_sig));
3276 type_sig->signature = signature;
3277 type_sig->type_offset = type_offset;
3278 type_sig->per_cu.objfile = objfile;
b0df02fd 3279 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3280 type_sig->per_cu.offset = offset;
3281
3282 slot = htab_find_slot (types_htab, type_sig, INSERT);
3283 gdb_assert (slot != NULL);
3284 if (*slot != NULL)
3285 {
3286 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3287
8b70b953
TT
3288 complaint (&symfile_complaints,
3289 _("debug type entry at offset 0x%x is duplicate to the "
3290 "entry at offset 0x%x, signature 0x%s"),
3291 offset, dup_sig->per_cu.offset,
3292 phex (signature, sizeof (signature)));
3293 gdb_assert (signature == dup_sig->signature);
3294 }
3295 *slot = type_sig;
348e048f 3296
8b70b953
TT
3297 if (dwarf2_die_debug)
3298 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3299 offset, phex (signature, sizeof (signature)));
348e048f 3300
9ff913ba 3301 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3302 }
348e048f
DE
3303 }
3304
3305 dwarf2_per_objfile->signatured_types = types_htab;
3306
d467dd73
DE
3307 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3308 dwarf2_per_objfile->all_type_units
1fd400ff 3309 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3310 dwarf2_per_objfile->n_type_units
1fd400ff 3311 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3312 iter = &dwarf2_per_objfile->all_type_units[0];
3313 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3314 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3315 == dwarf2_per_objfile->n_type_units);
1fd400ff 3316
348e048f
DE
3317 return 1;
3318}
3319
3320/* Lookup a signature based type.
3321 Returns NULL if SIG is not present in the table. */
3322
3323static struct signatured_type *
3324lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3325{
3326 struct signatured_type find_entry, *entry;
3327
3328 if (dwarf2_per_objfile->signatured_types == NULL)
3329 {
3330 complaint (&symfile_complaints,
55f1336d 3331 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3332 return 0;
3333 }
3334
3335 find_entry.signature = sig;
3336 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3337 return entry;
3338}
3339
d85a05f0
DJ
3340/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3341
3342static void
3343init_cu_die_reader (struct die_reader_specs *reader,
3344 struct dwarf2_cu *cu)
3345{
3346 reader->abfd = cu->objfile->obfd;
3347 reader->cu = cu;
b0df02fd 3348 if (cu->per_cu->debug_types_section)
be391dca 3349 {
b0df02fd
DE
3350 gdb_assert (cu->per_cu->debug_types_section->readin);
3351 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3352 }
d85a05f0 3353 else
be391dca
TT
3354 {
3355 gdb_assert (dwarf2_per_objfile->info.readin);
3356 reader->buffer = dwarf2_per_objfile->info.buffer;
3357 }
d85a05f0
DJ
3358}
3359
3360/* Find the base address of the compilation unit for range lists and
3361 location lists. It will normally be specified by DW_AT_low_pc.
3362 In DWARF-3 draft 4, the base address could be overridden by
3363 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3364 compilation units with discontinuous ranges. */
3365
3366static void
3367dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3368{
3369 struct attribute *attr;
3370
3371 cu->base_known = 0;
3372 cu->base_address = 0;
3373
3374 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3375 if (attr)
3376 {
3377 cu->base_address = DW_ADDR (attr);
3378 cu->base_known = 1;
3379 }
3380 else
3381 {
3382 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3383 if (attr)
3384 {
3385 cu->base_address = DW_ADDR (attr);
3386 cu->base_known = 1;
3387 }
3388 }
3389}
3390
348e048f
DE
3391/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3392 to combine the common parts.
21b2bd31
DE
3393 Process compilation unit THIS_CU for a psymtab.
3394 SECTION is the section the CU/TU comes from,
3395 either .debug_info or .debug_types. */
aaa75496 3396
70221824 3397static void
a0f42c21 3398process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3399 struct dwarf2_section_info *section,
3400 int is_debug_types_section)
c906108c 3401{
a0f42c21 3402 struct objfile *objfile = this_cu->objfile;
c906108c 3403 bfd *abfd = objfile->obfd;
21b2bd31
DE
3404 gdb_byte *buffer = section->buffer;
3405 gdb_byte *info_ptr = buffer + this_cu->offset;
3406 unsigned int buffer_size = section->size;
93311388 3407 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3408 struct die_info *comp_unit_die;
c906108c 3409 struct partial_symtab *pst;
5734ee8b 3410 CORE_ADDR baseaddr;
93311388
DE
3411 struct cleanup *back_to_inner;
3412 struct dwarf2_cu cu;
d85a05f0
DJ
3413 int has_children, has_pc_info;
3414 struct attribute *attr;
d85a05f0
DJ
3415 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3416 struct die_reader_specs reader_specs;
3e2a0cee 3417 const char *filename;
c906108c 3418
23745b47
DE
3419 /* If this compilation unit was already read in, free the
3420 cached copy in order to read it in again. This is
3421 necessary because we skipped some symbols when we first
3422 read in the compilation unit (see load_partial_dies).
3423 This problem could be avoided, but the benefit is
3424 unclear. */
3425 if (this_cu->cu != NULL)
3426 free_one_cached_comp_unit (this_cu->cu);
3427
3428 /* Note that this is a pointer to our stack frame, being
3429 added to a global data structure. It will be cleaned up
3430 in free_stack_comp_unit when we finish with this
3431 compilation unit. */
3432 init_one_comp_unit (&cu, this_cu);
93311388 3433 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3434
9ff913ba
DE
3435 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3436 is_debug_types_section);
10b3939b 3437
6caca83c
CC
3438 /* Skip dummy compilation units. */
3439 if (info_ptr >= buffer + buffer_size
3440 || peek_abbrev_code (abfd, info_ptr) == 0)
3441 {
6caca83c 3442 do_cleanups (back_to_inner);
21b2bd31 3443 return;
6caca83c
CC
3444 }
3445
93311388 3446 cu.list_in_scope = &file_symbols;
af703f96 3447
93311388 3448 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3449 dwarf2_read_abbrevs (&cu);
93311388 3450 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3451
93311388 3452 /* Read the compilation unit die. */
d85a05f0
DJ
3453 init_cu_die_reader (&reader_specs, &cu);
3454 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3455 &has_children);
93311388 3456
21b2bd31 3457 if (is_debug_types_section)
348e048f 3458 {
b3c8eb43
JK
3459 /* LENGTH has not been set yet for type units. */
3460 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3461 this_cu->length = cu.header.length + cu.header.initial_length_size;
3462 }
d85a05f0 3463 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3464 {
93311388 3465 do_cleanups (back_to_inner);
21b2bd31 3466 return;
93311388 3467 }
72bf9492 3468
9816fde3 3469 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3470
93311388 3471 /* Allocate a new partial symbol table structure. */
d85a05f0 3472 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3473 if (attr == NULL || !DW_STRING (attr))
3474 filename = "";
3475 else
3476 filename = DW_STRING (attr);
93311388 3477 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3478 filename,
93311388
DE
3479 /* TEXTLOW and TEXTHIGH are set below. */
3480 0,
3481 objfile->global_psymbols.next,
3482 objfile->static_psymbols.next);
9750bca9 3483 pst->psymtabs_addrmap_supported = 1;
72bf9492 3484
d85a05f0
DJ
3485 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3486 if (attr != NULL)
3487 pst->dirname = DW_STRING (attr);
72bf9492 3488
e38df1d0 3489 pst->read_symtab_private = this_cu;
72bf9492 3490
93311388 3491 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3492
0963b4bd 3493 /* Store the function that reads in the rest of the symbol table. */
93311388 3494 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3495
9291a0cd 3496 this_cu->v.psymtab = pst;
c906108c 3497
d85a05f0
DJ
3498 dwarf2_find_base_address (comp_unit_die, &cu);
3499
93311388
DE
3500 /* Possibly set the default values of LOWPC and HIGHPC from
3501 `DW_AT_ranges'. */
d85a05f0
DJ
3502 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3503 &best_highpc, &cu, pst);
3504 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3505 /* Store the contiguous range if it is not empty; it can be empty for
3506 CUs with no code. */
3507 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3508 best_lowpc + baseaddr,
3509 best_highpc + baseaddr - 1, pst);
93311388
DE
3510
3511 /* Check if comp unit has_children.
3512 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3513 If not, there's no more debug_info for this comp unit. */
d85a05f0 3514 if (has_children)
93311388
DE
3515 {
3516 struct partial_die_info *first_die;
3517 CORE_ADDR lowpc, highpc;
31ffec48 3518
93311388
DE
3519 lowpc = ((CORE_ADDR) -1);
3520 highpc = ((CORE_ADDR) 0);
c906108c 3521
93311388 3522 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3523
93311388 3524 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3525 ! has_pc_info, &cu);
57c22c6c 3526
93311388
DE
3527 /* If we didn't find a lowpc, set it to highpc to avoid
3528 complaints from `maint check'. */
3529 if (lowpc == ((CORE_ADDR) -1))
3530 lowpc = highpc;
10b3939b 3531
93311388
DE
3532 /* If the compilation unit didn't have an explicit address range,
3533 then use the information extracted from its child dies. */
d85a05f0 3534 if (! has_pc_info)
93311388 3535 {
d85a05f0
DJ
3536 best_lowpc = lowpc;
3537 best_highpc = highpc;
93311388
DE
3538 }
3539 }
d85a05f0
DJ
3540 pst->textlow = best_lowpc + baseaddr;
3541 pst->texthigh = best_highpc + baseaddr;
c906108c 3542
93311388
DE
3543 pst->n_global_syms = objfile->global_psymbols.next -
3544 (objfile->global_psymbols.list + pst->globals_offset);
3545 pst->n_static_syms = objfile->static_psymbols.next -
3546 (objfile->static_psymbols.list + pst->statics_offset);
3547 sort_pst_symbols (pst);
c906108c 3548
21b2bd31 3549 if (is_debug_types_section)
348e048f
DE
3550 {
3551 /* It's not clear we want to do anything with stmt lists here.
3552 Waiting to see what gcc ultimately does. */
3553 }
d85a05f0 3554 else
93311388
DE
3555 {
3556 /* Get the list of files included in the current compilation unit,
3557 and build a psymtab for each of them. */
d85a05f0 3558 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3559 }
ae038cb0 3560
93311388 3561 do_cleanups (back_to_inner);
93311388 3562}
ff013f42 3563
348e048f
DE
3564/* Traversal function for htab_traverse_noresize.
3565 Process one .debug_types comp-unit. */
3566
3567static int
3568process_type_comp_unit (void **slot, void *info)
3569{
3570 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3571 struct dwarf2_per_cu_data *this_cu;
3572
a0f42c21 3573 gdb_assert (info == NULL);
348e048f 3574 this_cu = &entry->per_cu;
348e048f 3575
b0df02fd 3576 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3577 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3578
3579 return 1;
3580}
3581
3582/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3583 Build partial symbol tables for the .debug_types comp-units. */
3584
3585static void
3586build_type_psymtabs (struct objfile *objfile)
3587{
3588 if (! create_debug_types_hash_table (objfile))
3589 return;
3590
3591 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3592 process_type_comp_unit, NULL);
348e048f
DE
3593}
3594
60606b2c
TT
3595/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3596
3597static void
3598psymtabs_addrmap_cleanup (void *o)
3599{
3600 struct objfile *objfile = o;
ec61707d 3601
60606b2c
TT
3602 objfile->psymtabs_addrmap = NULL;
3603}
3604
93311388
DE
3605/* Build the partial symbol table by doing a quick pass through the
3606 .debug_info and .debug_abbrev sections. */
72bf9492 3607
93311388 3608static void
c67a9c90 3609dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3610{
60606b2c
TT
3611 struct cleanup *back_to, *addrmap_cleanup;
3612 struct obstack temp_obstack;
21b2bd31 3613 int i;
93311388 3614
98bfdba5
PA
3615 dwarf2_per_objfile->reading_partial_symbols = 1;
3616
be391dca 3617 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3618
93311388
DE
3619 /* Any cached compilation units will be linked by the per-objfile
3620 read_in_chain. Make sure to free them when we're done. */
3621 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3622
348e048f
DE
3623 build_type_psymtabs (objfile);
3624
93311388 3625 create_all_comp_units (objfile);
c906108c 3626
60606b2c
TT
3627 /* Create a temporary address map on a temporary obstack. We later
3628 copy this to the final obstack. */
3629 obstack_init (&temp_obstack);
3630 make_cleanup_obstack_free (&temp_obstack);
3631 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3632 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3633
21b2bd31 3634 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3635 {
21b2bd31 3636 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3637
21b2bd31 3638 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3639 }
ff013f42
JK
3640
3641 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3642 &objfile->objfile_obstack);
60606b2c 3643 discard_cleanups (addrmap_cleanup);
ff013f42 3644
ae038cb0
DJ
3645 do_cleanups (back_to);
3646}
3647
93311388 3648/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3649
3650static void
a0f42c21 3651load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3652{
a0f42c21 3653 struct objfile *objfile = this_cu->objfile;
ae038cb0 3654 bfd *abfd = objfile->obfd;
adabb602 3655 gdb_byte *info_ptr;
d85a05f0 3656 struct die_info *comp_unit_die;
ae038cb0 3657 struct dwarf2_cu *cu;
1d9ec526 3658 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3659 int has_children;
3660 struct die_reader_specs reader_specs;
98bfdba5 3661 int read_cu = 0;
9ff913ba 3662 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3663
b0df02fd 3664 gdb_assert (! this_cu->debug_types_section);
348e048f 3665
9ff913ba
DE
3666 gdb_assert (section->readin);
3667 info_ptr = section->buffer + this_cu->offset;
ae038cb0 3668
98bfdba5
PA
3669 if (this_cu->cu == NULL)
3670 {
9816fde3 3671 cu = xmalloc (sizeof (*cu));
23745b47 3672 init_one_comp_unit (cu, this_cu);
ae038cb0 3673
98bfdba5 3674 read_cu = 1;
ae038cb0 3675
98bfdba5 3676 /* If an error occurs while loading, release our storage. */
68dc6402 3677 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3678
9ff913ba
DE
3679 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3680 0);
ae038cb0 3681
6caca83c 3682 /* Skip dummy compilation units. */
9ff913ba 3683 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3684 || peek_abbrev_code (abfd, info_ptr) == 0)
3685 {
3686 do_cleanups (free_cu_cleanup);
3687 return;
3688 }
98bfdba5
PA
3689 }
3690 else
3691 {
3692 cu = this_cu->cu;
3693 info_ptr += cu->header.first_die_offset;
3694 }
ae038cb0
DJ
3695
3696 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3697 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3698 dwarf2_read_abbrevs (cu);
98bfdba5 3699 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3700
3701 /* Read the compilation unit die. */
d85a05f0
DJ
3702 init_cu_die_reader (&reader_specs, cu);
3703 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3704 &has_children);
ae038cb0 3705
9816fde3 3706 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3707
ae038cb0
DJ
3708 /* Check if comp unit has_children.
3709 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3710 If not, there's no more debug_info for this comp unit. */
d85a05f0 3711 if (has_children)
9ff913ba 3712 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3713
98bfdba5
PA
3714 do_cleanups (free_abbrevs_cleanup);
3715
3716 if (read_cu)
3717 {
3718 /* We've successfully allocated this compilation unit. Let our
3719 caller clean it up when finished with it. */
3720 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
3721
3722 /* Link this CU into read_in_chain. */
3723 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3724 dwarf2_per_objfile->read_in_chain = this_cu;
98bfdba5 3725 }
ae038cb0
DJ
3726}
3727
9cdd5dbd
DE
3728/* Create a list of all compilation units in OBJFILE.
3729 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3730
3731static void
3732create_all_comp_units (struct objfile *objfile)
3733{
3734 int n_allocated;
3735 int n_comp_units;
3736 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3737 gdb_byte *info_ptr;
3738
3739 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3740 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3741
3742 n_comp_units = 0;
3743 n_allocated = 10;
3744 all_comp_units = xmalloc (n_allocated
3745 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3746
3e43a32a
MS
3747 while (info_ptr < dwarf2_per_objfile->info.buffer
3748 + dwarf2_per_objfile->info.size)
ae038cb0 3749 {
c764a876 3750 unsigned int length, initial_length_size;
ae038cb0 3751 struct dwarf2_per_cu_data *this_cu;
c764a876 3752 unsigned int offset;
ae038cb0 3753
dce234bc 3754 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3755
3756 /* Read just enough information to find out where the next
3757 compilation unit is. */
c764a876
DE
3758 length = read_initial_length (objfile->obfd, info_ptr,
3759 &initial_length_size);
ae038cb0
DJ
3760
3761 /* Save the compilation unit for later lookup. */
3762 this_cu = obstack_alloc (&objfile->objfile_obstack,
3763 sizeof (struct dwarf2_per_cu_data));
3764 memset (this_cu, 0, sizeof (*this_cu));
3765 this_cu->offset = offset;
c764a876 3766 this_cu->length = length + initial_length_size;
9291a0cd 3767 this_cu->objfile = objfile;
ae038cb0
DJ
3768
3769 if (n_comp_units == n_allocated)
3770 {
3771 n_allocated *= 2;
3772 all_comp_units = xrealloc (all_comp_units,
3773 n_allocated
3774 * sizeof (struct dwarf2_per_cu_data *));
3775 }
3776 all_comp_units[n_comp_units++] = this_cu;
3777
3778 info_ptr = info_ptr + this_cu->length;
3779 }
3780
3781 dwarf2_per_objfile->all_comp_units
3782 = obstack_alloc (&objfile->objfile_obstack,
3783 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3784 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3785 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3786 xfree (all_comp_units);
3787 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3788}
3789
5734ee8b
DJ
3790/* Process all loaded DIEs for compilation unit CU, starting at
3791 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3792 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3793 DW_AT_ranges). If NEED_PC is set, then this function will set
3794 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3795 and record the covered ranges in the addrmap. */
c906108c 3796
72bf9492
DJ
3797static void
3798scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3799 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3800{
72bf9492 3801 struct partial_die_info *pdi;
c906108c 3802
91c24f0a
DC
3803 /* Now, march along the PDI's, descending into ones which have
3804 interesting children but skipping the children of the other ones,
3805 until we reach the end of the compilation unit. */
c906108c 3806
72bf9492 3807 pdi = first_die;
91c24f0a 3808
72bf9492
DJ
3809 while (pdi != NULL)
3810 {
3811 fixup_partial_die (pdi, cu);
c906108c 3812
f55ee35c 3813 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3814 children, so we need to look at them. Ditto for anonymous
3815 enums. */
933c6fe4 3816
72bf9492 3817 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3818 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3819 {
72bf9492 3820 switch (pdi->tag)
c906108c
SS
3821 {
3822 case DW_TAG_subprogram:
5734ee8b 3823 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3824 break;
72929c62 3825 case DW_TAG_constant:
c906108c
SS
3826 case DW_TAG_variable:
3827 case DW_TAG_typedef:
91c24f0a 3828 case DW_TAG_union_type:
72bf9492 3829 if (!pdi->is_declaration)
63d06c5c 3830 {
72bf9492 3831 add_partial_symbol (pdi, cu);
63d06c5c
DC
3832 }
3833 break;
c906108c 3834 case DW_TAG_class_type:
680b30c7 3835 case DW_TAG_interface_type:
c906108c 3836 case DW_TAG_structure_type:
72bf9492 3837 if (!pdi->is_declaration)
c906108c 3838 {
72bf9492 3839 add_partial_symbol (pdi, cu);
c906108c
SS
3840 }
3841 break;
91c24f0a 3842 case DW_TAG_enumeration_type:
72bf9492
DJ
3843 if (!pdi->is_declaration)
3844 add_partial_enumeration (pdi, cu);
c906108c
SS
3845 break;
3846 case DW_TAG_base_type:
a02abb62 3847 case DW_TAG_subrange_type:
c906108c 3848 /* File scope base type definitions are added to the partial
c5aa993b 3849 symbol table. */
72bf9492 3850 add_partial_symbol (pdi, cu);
c906108c 3851 break;
d9fa45fe 3852 case DW_TAG_namespace:
5734ee8b 3853 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3854 break;
5d7cb8df
JK
3855 case DW_TAG_module:
3856 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3857 break;
c906108c
SS
3858 default:
3859 break;
3860 }
3861 }
3862
72bf9492
DJ
3863 /* If the die has a sibling, skip to the sibling. */
3864
3865 pdi = pdi->die_sibling;
3866 }
3867}
3868
3869/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3870
72bf9492 3871 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3872 name is concatenated with "::" and the partial DIE's name. For
3873 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3874 Enumerators are an exception; they use the scope of their parent
3875 enumeration type, i.e. the name of the enumeration type is not
3876 prepended to the enumerator.
91c24f0a 3877
72bf9492
DJ
3878 There are two complexities. One is DW_AT_specification; in this
3879 case "parent" means the parent of the target of the specification,
3880 instead of the direct parent of the DIE. The other is compilers
3881 which do not emit DW_TAG_namespace; in this case we try to guess
3882 the fully qualified name of structure types from their members'
3883 linkage names. This must be done using the DIE's children rather
3884 than the children of any DW_AT_specification target. We only need
3885 to do this for structures at the top level, i.e. if the target of
3886 any DW_AT_specification (if any; otherwise the DIE itself) does not
3887 have a parent. */
3888
3889/* Compute the scope prefix associated with PDI's parent, in
3890 compilation unit CU. The result will be allocated on CU's
3891 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3892 field. NULL is returned if no prefix is necessary. */
3893static char *
3894partial_die_parent_scope (struct partial_die_info *pdi,
3895 struct dwarf2_cu *cu)
3896{
3897 char *grandparent_scope;
3898 struct partial_die_info *parent, *real_pdi;
91c24f0a 3899
72bf9492
DJ
3900 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3901 then this means the parent of the specification DIE. */
3902
3903 real_pdi = pdi;
72bf9492 3904 while (real_pdi->has_specification)
10b3939b 3905 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3906
3907 parent = real_pdi->die_parent;
3908 if (parent == NULL)
3909 return NULL;
3910
3911 if (parent->scope_set)
3912 return parent->scope;
3913
3914 fixup_partial_die (parent, cu);
3915
10b3939b 3916 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3917
acebe513
UW
3918 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3919 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3920 Work around this problem here. */
3921 if (cu->language == language_cplus
6e70227d 3922 && parent->tag == DW_TAG_namespace
acebe513
UW
3923 && strcmp (parent->name, "::") == 0
3924 && grandparent_scope == NULL)
3925 {
3926 parent->scope = NULL;
3927 parent->scope_set = 1;
3928 return NULL;
3929 }
3930
9c6c53f7
SA
3931 if (pdi->tag == DW_TAG_enumerator)
3932 /* Enumerators should not get the name of the enumeration as a prefix. */
3933 parent->scope = grandparent_scope;
3934 else if (parent->tag == DW_TAG_namespace
f55ee35c 3935 || parent->tag == DW_TAG_module
72bf9492
DJ
3936 || parent->tag == DW_TAG_structure_type
3937 || parent->tag == DW_TAG_class_type
680b30c7 3938 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3939 || parent->tag == DW_TAG_union_type
3940 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3941 {
3942 if (grandparent_scope == NULL)
3943 parent->scope = parent->name;
3944 else
3e43a32a
MS
3945 parent->scope = typename_concat (&cu->comp_unit_obstack,
3946 grandparent_scope,
f55ee35c 3947 parent->name, 0, cu);
72bf9492 3948 }
72bf9492
DJ
3949 else
3950 {
3951 /* FIXME drow/2004-04-01: What should we be doing with
3952 function-local names? For partial symbols, we should probably be
3953 ignoring them. */
3954 complaint (&symfile_complaints,
e2e0b3e5 3955 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3956 parent->tag, pdi->offset);
3957 parent->scope = grandparent_scope;
c906108c
SS
3958 }
3959
72bf9492
DJ
3960 parent->scope_set = 1;
3961 return parent->scope;
3962}
3963
3964/* Return the fully scoped name associated with PDI, from compilation unit
3965 CU. The result will be allocated with malloc. */
3966static char *
3967partial_die_full_name (struct partial_die_info *pdi,
3968 struct dwarf2_cu *cu)
3969{
3970 char *parent_scope;
3971
98bfdba5
PA
3972 /* If this is a template instantiation, we can not work out the
3973 template arguments from partial DIEs. So, unfortunately, we have
3974 to go through the full DIEs. At least any work we do building
3975 types here will be reused if full symbols are loaded later. */
3976 if (pdi->has_template_arguments)
3977 {
3978 fixup_partial_die (pdi, cu);
3979
3980 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3981 {
3982 struct die_info *die;
3983 struct attribute attr;
3984 struct dwarf2_cu *ref_cu = cu;
3985
3986 attr.name = 0;
3987 attr.form = DW_FORM_ref_addr;
3988 attr.u.addr = pdi->offset;
3989 die = follow_die_ref (NULL, &attr, &ref_cu);
3990
3991 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3992 }
3993 }
3994
72bf9492
DJ
3995 parent_scope = partial_die_parent_scope (pdi, cu);
3996 if (parent_scope == NULL)
3997 return NULL;
3998 else
f55ee35c 3999 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4000}
4001
4002static void
72bf9492 4003add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4004{
e7c27a73 4005 struct objfile *objfile = cu->objfile;
c906108c 4006 CORE_ADDR addr = 0;
decbce07 4007 char *actual_name = NULL;
e142c38c 4008 CORE_ADDR baseaddr;
72bf9492 4009 int built_actual_name = 0;
e142c38c
DJ
4010
4011 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4012
94af9270
KS
4013 actual_name = partial_die_full_name (pdi, cu);
4014 if (actual_name)
4015 built_actual_name = 1;
63d06c5c 4016
72bf9492
DJ
4017 if (actual_name == NULL)
4018 actual_name = pdi->name;
4019
c906108c
SS
4020 switch (pdi->tag)
4021 {
4022 case DW_TAG_subprogram:
2cfa0c8d 4023 if (pdi->is_external || cu->language == language_ada)
c906108c 4024 {
2cfa0c8d
JB
4025 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4026 of the global scope. But in Ada, we want to be able to access
4027 nested procedures globally. So all Ada subprograms are stored
4028 in the global scope. */
f47fb265 4029 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4030 mst_text, objfile); */
f47fb265
MS
4031 add_psymbol_to_list (actual_name, strlen (actual_name),
4032 built_actual_name,
4033 VAR_DOMAIN, LOC_BLOCK,
4034 &objfile->global_psymbols,
4035 0, pdi->lowpc + baseaddr,
4036 cu->language, objfile);
c906108c
SS
4037 }
4038 else
4039 {
f47fb265 4040 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4041 mst_file_text, objfile); */
f47fb265
MS
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_BLOCK,
4045 &objfile->static_psymbols,
4046 0, pdi->lowpc + baseaddr,
4047 cu->language, objfile);
c906108c
SS
4048 }
4049 break;
72929c62
JB
4050 case DW_TAG_constant:
4051 {
4052 struct psymbol_allocation_list *list;
4053
4054 if (pdi->is_external)
4055 list = &objfile->global_psymbols;
4056 else
4057 list = &objfile->static_psymbols;
f47fb265
MS
4058 add_psymbol_to_list (actual_name, strlen (actual_name),
4059 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4060 list, 0, 0, cu->language, objfile);
72929c62
JB
4061 }
4062 break;
c906108c 4063 case DW_TAG_variable:
caac4577
JG
4064 if (pdi->locdesc)
4065 addr = decode_locdesc (pdi->locdesc, cu);
4066
4067 if (pdi->locdesc
4068 && addr == 0
4069 && !dwarf2_per_objfile->has_section_at_zero)
4070 {
4071 /* A global or static variable may also have been stripped
4072 out by the linker if unused, in which case its address
4073 will be nullified; do not add such variables into partial
4074 symbol table then. */
4075 }
4076 else if (pdi->is_external)
c906108c
SS
4077 {
4078 /* Global Variable.
4079 Don't enter into the minimal symbol tables as there is
4080 a minimal symbol table entry from the ELF symbols already.
4081 Enter into partial symbol table if it has a location
4082 descriptor or a type.
4083 If the location descriptor is missing, new_symbol will create
4084 a LOC_UNRESOLVED symbol, the address of the variable will then
4085 be determined from the minimal symbol table whenever the variable
4086 is referenced.
4087 The address for the partial symbol table entry is not
4088 used by GDB, but it comes in handy for debugging partial symbol
4089 table building. */
4090
c906108c 4091 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4092 add_psymbol_to_list (actual_name, strlen (actual_name),
4093 built_actual_name,
4094 VAR_DOMAIN, LOC_STATIC,
4095 &objfile->global_psymbols,
4096 0, addr + baseaddr,
4097 cu->language, objfile);
c906108c
SS
4098 }
4099 else
4100 {
0963b4bd 4101 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4102 if (pdi->locdesc == NULL)
decbce07
MS
4103 {
4104 if (built_actual_name)
4105 xfree (actual_name);
4106 return;
4107 }
f47fb265 4108 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4109 mst_file_data, objfile); */
f47fb265
MS
4110 add_psymbol_to_list (actual_name, strlen (actual_name),
4111 built_actual_name,
4112 VAR_DOMAIN, LOC_STATIC,
4113 &objfile->static_psymbols,
4114 0, addr + baseaddr,
4115 cu->language, objfile);
c906108c
SS
4116 }
4117 break;
4118 case DW_TAG_typedef:
4119 case DW_TAG_base_type:
a02abb62 4120 case DW_TAG_subrange_type:
38d518c9 4121 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4122 built_actual_name,
176620f1 4123 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4124 &objfile->static_psymbols,
e142c38c 4125 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4126 break;
72bf9492
DJ
4127 case DW_TAG_namespace:
4128 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4129 built_actual_name,
72bf9492
DJ
4130 VAR_DOMAIN, LOC_TYPEDEF,
4131 &objfile->global_psymbols,
4132 0, (CORE_ADDR) 0, cu->language, objfile);
4133 break;
c906108c 4134 case DW_TAG_class_type:
680b30c7 4135 case DW_TAG_interface_type:
c906108c
SS
4136 case DW_TAG_structure_type:
4137 case DW_TAG_union_type:
4138 case DW_TAG_enumeration_type:
fa4028e9
JB
4139 /* Skip external references. The DWARF standard says in the section
4140 about "Structure, Union, and Class Type Entries": "An incomplete
4141 structure, union or class type is represented by a structure,
4142 union or class entry that does not have a byte size attribute
4143 and that has a DW_AT_declaration attribute." */
4144 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4145 {
4146 if (built_actual_name)
4147 xfree (actual_name);
4148 return;
4149 }
fa4028e9 4150
63d06c5c
DC
4151 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4152 static vs. global. */
38d518c9 4153 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4154 built_actual_name,
176620f1 4155 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4156 (cu->language == language_cplus
4157 || cu->language == language_java)
63d06c5c
DC
4158 ? &objfile->global_psymbols
4159 : &objfile->static_psymbols,
e142c38c 4160 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4161
c906108c
SS
4162 break;
4163 case DW_TAG_enumerator:
38d518c9 4164 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4165 built_actual_name,
176620f1 4166 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4167 (cu->language == language_cplus
4168 || cu->language == language_java)
f6fe98ef
DJ
4169 ? &objfile->global_psymbols
4170 : &objfile->static_psymbols,
e142c38c 4171 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4172 break;
4173 default:
4174 break;
4175 }
5c4e30ca 4176
72bf9492
DJ
4177 if (built_actual_name)
4178 xfree (actual_name);
c906108c
SS
4179}
4180
5c4e30ca
DC
4181/* Read a partial die corresponding to a namespace; also, add a symbol
4182 corresponding to that namespace to the symbol table. NAMESPACE is
4183 the name of the enclosing namespace. */
91c24f0a 4184
72bf9492
DJ
4185static void
4186add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4188 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4189{
72bf9492 4190 /* Add a symbol for the namespace. */
e7c27a73 4191
72bf9492 4192 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4193
4194 /* Now scan partial symbols in that namespace. */
4195
91c24f0a 4196 if (pdi->has_children)
5734ee8b 4197 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4198}
4199
5d7cb8df
JK
4200/* Read a partial die corresponding to a Fortran module. */
4201
4202static void
4203add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4204 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4205{
f55ee35c 4206 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4207
4208 if (pdi->has_children)
4209 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4210}
4211
bc30ff58
JB
4212/* Read a partial die corresponding to a subprogram and create a partial
4213 symbol for that subprogram. When the CU language allows it, this
4214 routine also defines a partial symbol for each nested subprogram
4215 that this subprogram contains.
6e70227d 4216
bc30ff58
JB
4217 DIE my also be a lexical block, in which case we simply search
4218 recursively for suprograms defined inside that lexical block.
4219 Again, this is only performed when the CU language allows this
4220 type of definitions. */
4221
4222static void
4223add_partial_subprogram (struct partial_die_info *pdi,
4224 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4225 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4226{
4227 if (pdi->tag == DW_TAG_subprogram)
4228 {
4229 if (pdi->has_pc_info)
4230 {
4231 if (pdi->lowpc < *lowpc)
4232 *lowpc = pdi->lowpc;
4233 if (pdi->highpc > *highpc)
4234 *highpc = pdi->highpc;
5734ee8b
DJ
4235 if (need_pc)
4236 {
4237 CORE_ADDR baseaddr;
4238 struct objfile *objfile = cu->objfile;
4239
4240 baseaddr = ANOFFSET (objfile->section_offsets,
4241 SECT_OFF_TEXT (objfile));
4242 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4243 pdi->lowpc + baseaddr,
4244 pdi->highpc - 1 + baseaddr,
9291a0cd 4245 cu->per_cu->v.psymtab);
5734ee8b 4246 }
bc30ff58 4247 if (!pdi->is_declaration)
e8d05480
JB
4248 /* Ignore subprogram DIEs that do not have a name, they are
4249 illegal. Do not emit a complaint at this point, we will
4250 do so when we convert this psymtab into a symtab. */
4251 if (pdi->name)
4252 add_partial_symbol (pdi, cu);
bc30ff58
JB
4253 }
4254 }
6e70227d 4255
bc30ff58
JB
4256 if (! pdi->has_children)
4257 return;
4258
4259 if (cu->language == language_ada)
4260 {
4261 pdi = pdi->die_child;
4262 while (pdi != NULL)
4263 {
4264 fixup_partial_die (pdi, cu);
4265 if (pdi->tag == DW_TAG_subprogram
4266 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4267 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4268 pdi = pdi->die_sibling;
4269 }
4270 }
4271}
4272
91c24f0a
DC
4273/* Read a partial die corresponding to an enumeration type. */
4274
72bf9492
DJ
4275static void
4276add_partial_enumeration (struct partial_die_info *enum_pdi,
4277 struct dwarf2_cu *cu)
91c24f0a 4278{
72bf9492 4279 struct partial_die_info *pdi;
91c24f0a
DC
4280
4281 if (enum_pdi->name != NULL)
72bf9492
DJ
4282 add_partial_symbol (enum_pdi, cu);
4283
4284 pdi = enum_pdi->die_child;
4285 while (pdi)
91c24f0a 4286 {
72bf9492 4287 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4288 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4289 else
72bf9492
DJ
4290 add_partial_symbol (pdi, cu);
4291 pdi = pdi->die_sibling;
91c24f0a 4292 }
91c24f0a
DC
4293}
4294
6caca83c
CC
4295/* Return the initial uleb128 in the die at INFO_PTR. */
4296
4297static unsigned int
4298peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4299{
4300 unsigned int bytes_read;
4301
4302 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4303}
4304
4bb7a0a7
DJ
4305/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4306 Return the corresponding abbrev, or NULL if the number is zero (indicating
4307 an empty DIE). In either case *BYTES_READ will be set to the length of
4308 the initial number. */
4309
4310static struct abbrev_info *
fe1b8b76 4311peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4312 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4313{
4314 bfd *abfd = cu->objfile->obfd;
4315 unsigned int abbrev_number;
4316 struct abbrev_info *abbrev;
4317
4318 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4319
4320 if (abbrev_number == 0)
4321 return NULL;
4322
4323 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4324 if (!abbrev)
4325 {
3e43a32a
MS
4326 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4327 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4328 }
4329
4330 return abbrev;
4331}
4332
93311388
DE
4333/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4334 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4335 DIE. Any children of the skipped DIEs will also be skipped. */
4336
fe1b8b76 4337static gdb_byte *
93311388 4338skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4339{
4340 struct abbrev_info *abbrev;
4341 unsigned int bytes_read;
4342
4343 while (1)
4344 {
4345 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4346 if (abbrev == NULL)
4347 return info_ptr + bytes_read;
4348 else
93311388 4349 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4350 }
4351}
4352
93311388
DE
4353/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4354 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4355 abbrev corresponding to that skipped uleb128 should be passed in
4356 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4357 children. */
4358
fe1b8b76 4359static gdb_byte *
93311388
DE
4360skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4361 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4362{
4363 unsigned int bytes_read;
4364 struct attribute attr;
4365 bfd *abfd = cu->objfile->obfd;
4366 unsigned int form, i;
4367
4368 for (i = 0; i < abbrev->num_attrs; i++)
4369 {
4370 /* The only abbrev we care about is DW_AT_sibling. */
4371 if (abbrev->attrs[i].name == DW_AT_sibling)
4372 {
4373 read_attribute (&attr, &abbrev->attrs[i],
4374 abfd, info_ptr, cu);
4375 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4376 complaint (&symfile_complaints,
4377 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4378 else
93311388 4379 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4380 }
4381
4382 /* If it isn't DW_AT_sibling, skip this attribute. */
4383 form = abbrev->attrs[i].form;
4384 skip_attribute:
4385 switch (form)
4386 {
4bb7a0a7 4387 case DW_FORM_ref_addr:
ae411497
TT
4388 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4389 and later it is offset sized. */
4390 if (cu->header.version == 2)
4391 info_ptr += cu->header.addr_size;
4392 else
4393 info_ptr += cu->header.offset_size;
4394 break;
4395 case DW_FORM_addr:
4bb7a0a7
DJ
4396 info_ptr += cu->header.addr_size;
4397 break;
4398 case DW_FORM_data1:
4399 case DW_FORM_ref1:
4400 case DW_FORM_flag:
4401 info_ptr += 1;
4402 break;
2dc7f7b3
TT
4403 case DW_FORM_flag_present:
4404 break;
4bb7a0a7
DJ
4405 case DW_FORM_data2:
4406 case DW_FORM_ref2:
4407 info_ptr += 2;
4408 break;
4409 case DW_FORM_data4:
4410 case DW_FORM_ref4:
4411 info_ptr += 4;
4412 break;
4413 case DW_FORM_data8:
4414 case DW_FORM_ref8:
55f1336d 4415 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4416 info_ptr += 8;
4417 break;
4418 case DW_FORM_string:
9b1c24c8 4419 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4420 info_ptr += bytes_read;
4421 break;
2dc7f7b3 4422 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4423 case DW_FORM_strp:
4424 info_ptr += cu->header.offset_size;
4425 break;
2dc7f7b3 4426 case DW_FORM_exprloc:
4bb7a0a7
DJ
4427 case DW_FORM_block:
4428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4429 info_ptr += bytes_read;
4430 break;
4431 case DW_FORM_block1:
4432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4433 break;
4434 case DW_FORM_block2:
4435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4436 break;
4437 case DW_FORM_block4:
4438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4439 break;
4440 case DW_FORM_sdata:
4441 case DW_FORM_udata:
4442 case DW_FORM_ref_udata:
4443 info_ptr = skip_leb128 (abfd, info_ptr);
4444 break;
4445 case DW_FORM_indirect:
4446 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4447 info_ptr += bytes_read;
4448 /* We need to continue parsing from here, so just go back to
4449 the top. */
4450 goto skip_attribute;
4451
4452 default:
3e43a32a
MS
4453 error (_("Dwarf Error: Cannot handle %s "
4454 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4455 dwarf_form_name (form),
4456 bfd_get_filename (abfd));
4457 }
4458 }
4459
4460 if (abbrev->has_children)
93311388 4461 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4462 else
4463 return info_ptr;
4464}
4465
93311388
DE
4466/* Locate ORIG_PDI's sibling.
4467 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4468 in BUFFER. */
91c24f0a 4469
fe1b8b76 4470static gdb_byte *
93311388
DE
4471locate_pdi_sibling (struct partial_die_info *orig_pdi,
4472 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4473 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4474{
4475 /* Do we know the sibling already? */
72bf9492 4476
91c24f0a
DC
4477 if (orig_pdi->sibling)
4478 return orig_pdi->sibling;
4479
4480 /* Are there any children to deal with? */
4481
4482 if (!orig_pdi->has_children)
4483 return info_ptr;
4484
4bb7a0a7 4485 /* Skip the children the long way. */
91c24f0a 4486
93311388 4487 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4488}
4489
c906108c
SS
4490/* Expand this partial symbol table into a full symbol table. */
4491
4492static void
fba45db2 4493dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4494{
c906108c
SS
4495 if (pst != NULL)
4496 {
4497 if (pst->readin)
4498 {
3e43a32a
MS
4499 warning (_("bug: psymtab for %s is already read in."),
4500 pst->filename);
c906108c
SS
4501 }
4502 else
4503 {
4504 if (info_verbose)
4505 {
3e43a32a
MS
4506 printf_filtered (_("Reading in symbols for %s..."),
4507 pst->filename);
c906108c
SS
4508 gdb_flush (gdb_stdout);
4509 }
4510
10b3939b
DJ
4511 /* Restore our global data. */
4512 dwarf2_per_objfile = objfile_data (pst->objfile,
4513 dwarf2_objfile_data_key);
4514
b2ab525c
KB
4515 /* If this psymtab is constructed from a debug-only objfile, the
4516 has_section_at_zero flag will not necessarily be correct. We
4517 can get the correct value for this flag by looking at the data
4518 associated with the (presumably stripped) associated objfile. */
4519 if (pst->objfile->separate_debug_objfile_backlink)
4520 {
4521 struct dwarf2_per_objfile *dpo_backlink
4522 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4523 dwarf2_objfile_data_key);
9a619af0 4524
b2ab525c
KB
4525 dwarf2_per_objfile->has_section_at_zero
4526 = dpo_backlink->has_section_at_zero;
4527 }
4528
98bfdba5
PA
4529 dwarf2_per_objfile->reading_partial_symbols = 0;
4530
c906108c
SS
4531 psymtab_to_symtab_1 (pst);
4532
4533 /* Finish up the debug error message. */
4534 if (info_verbose)
a3f17187 4535 printf_filtered (_("done.\n"));
c906108c
SS
4536 }
4537 }
4538}
9cdd5dbd
DE
4539\f
4540/* Reading in full CUs. */
c906108c 4541
10b3939b
DJ
4542/* Add PER_CU to the queue. */
4543
4544static void
a0f42c21 4545queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4546{
4547 struct dwarf2_queue_item *item;
4548
4549 per_cu->queued = 1;
4550 item = xmalloc (sizeof (*item));
4551 item->per_cu = per_cu;
4552 item->next = NULL;
4553
4554 if (dwarf2_queue == NULL)
4555 dwarf2_queue = item;
4556 else
4557 dwarf2_queue_tail->next = item;
4558
4559 dwarf2_queue_tail = item;
4560}
4561
4562/* Process the queue. */
4563
4564static void
a0f42c21 4565process_queue (void)
10b3939b
DJ
4566{
4567 struct dwarf2_queue_item *item, *next_item;
4568
03dd20cc
DJ
4569 /* The queue starts out with one item, but following a DIE reference
4570 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4571 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4572 {
9291a0cd
TT
4573 if (dwarf2_per_objfile->using_index
4574 ? !item->per_cu->v.quick->symtab
4575 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4576 process_full_comp_unit (item->per_cu);
4577
4578 item->per_cu->queued = 0;
4579 next_item = item->next;
4580 xfree (item);
4581 }
4582
4583 dwarf2_queue_tail = NULL;
4584}
4585
4586/* Free all allocated queue entries. This function only releases anything if
4587 an error was thrown; if the queue was processed then it would have been
4588 freed as we went along. */
4589
4590static void
4591dwarf2_release_queue (void *dummy)
4592{
4593 struct dwarf2_queue_item *item, *last;
4594
4595 item = dwarf2_queue;
4596 while (item)
4597 {
4598 /* Anything still marked queued is likely to be in an
4599 inconsistent state, so discard it. */
4600 if (item->per_cu->queued)
4601 {
4602 if (item->per_cu->cu != NULL)
4603 free_one_cached_comp_unit (item->per_cu->cu);
4604 item->per_cu->queued = 0;
4605 }
4606
4607 last = item;
4608 item = item->next;
4609 xfree (last);
4610 }
4611
4612 dwarf2_queue = dwarf2_queue_tail = NULL;
4613}
4614
4615/* Read in full symbols for PST, and anything it depends on. */
4616
c906108c 4617static void
fba45db2 4618psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4619{
10b3939b 4620 struct dwarf2_per_cu_data *per_cu;
c906108c 4621 struct cleanup *back_to;
aaa75496
JB
4622 int i;
4623
4624 for (i = 0; i < pst->number_of_dependencies; i++)
4625 if (!pst->dependencies[i]->readin)
4626 {
4627 /* Inform about additional files that need to be read in. */
4628 if (info_verbose)
4629 {
a3f17187 4630 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4631 fputs_filtered (" ", gdb_stdout);
4632 wrap_here ("");
4633 fputs_filtered ("and ", gdb_stdout);
4634 wrap_here ("");
4635 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4636 wrap_here (""); /* Flush output. */
aaa75496
JB
4637 gdb_flush (gdb_stdout);
4638 }
4639 psymtab_to_symtab_1 (pst->dependencies[i]);
4640 }
4641
e38df1d0 4642 per_cu = pst->read_symtab_private;
10b3939b
DJ
4643
4644 if (per_cu == NULL)
aaa75496
JB
4645 {
4646 /* It's an include file, no symbols to read for it.
4647 Everything is in the parent symtab. */
4648 pst->readin = 1;
4649 return;
4650 }
c906108c 4651
a0f42c21 4652 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4653}
4654
93311388 4655/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4656
93311388 4657static void
a0f42c21 4658load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4659{
a0f42c21 4660 struct objfile *objfile = per_cu->objfile;
31ffec48 4661 bfd *abfd = objfile->obfd;
10b3939b 4662 struct dwarf2_cu *cu;
c764a876 4663 unsigned int offset;
93311388 4664 gdb_byte *info_ptr, *beg_of_comp_unit;
7fb3ad1f 4665 struct cleanup *free_cu_cleanup = NULL;
10b3939b 4666 struct attribute *attr;
98bfdba5 4667 int read_cu = 0;
6502dd73 4668
b0df02fd 4669 gdb_assert (! per_cu->debug_types_section);
348e048f 4670
c906108c 4671 /* Set local variables from the partial symbol table info. */
10b3939b 4672 offset = per_cu->offset;
6502dd73 4673
be391dca 4674 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4675 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4676 beg_of_comp_unit = info_ptr;
63d06c5c 4677
98bfdba5
PA
4678 if (per_cu->cu == NULL)
4679 {
9816fde3 4680 cu = xmalloc (sizeof (*cu));
23745b47 4681 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4682
4683 read_cu = 1;
c906108c 4684
98bfdba5 4685 /* If an error occurs while loading, release our storage. */
68dc6402 4686 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4687
98bfdba5
PA
4688 /* Read in the comp_unit header. */
4689 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4690
6caca83c
CC
4691 /* Skip dummy compilation units. */
4692 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4693 + dwarf2_per_objfile->info.size)
4694 || peek_abbrev_code (abfd, info_ptr) == 0)
4695 {
4696 do_cleanups (free_cu_cleanup);
4697 return;
4698 }
4699
98bfdba5
PA
4700 /* Complete the cu_header. */
4701 cu->header.offset = offset;
4702 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
98bfdba5
PA
4703 }
4704 else
4705 {
4706 cu = per_cu->cu;
4707 info_ptr += cu->header.first_die_offset;
4708 }
e142c38c 4709
93311388 4710 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4711
4712 /* We try not to read any attributes in this function, because not
9cdd5dbd 4713 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4714 table processing isn't initialized. But we have to set the CU language,
4715 or we won't be able to build types correctly. */
9816fde3 4716 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4717
a6c727b2
DJ
4718 /* Similarly, if we do not read the producer, we can not apply
4719 producer-specific interpretation. */
4720 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4721 if (attr)
4722 cu->producer = DW_STRING (attr);
4723
98bfdba5
PA
4724 if (read_cu)
4725 {
98bfdba5
PA
4726 /* We've successfully allocated this compilation unit. Let our
4727 caller clean it up when finished with it. */
4728 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
4729
4730 /* Link this CU into read_in_chain. */
4731 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4732 dwarf2_per_objfile->read_in_chain = per_cu;
98bfdba5 4733 }
10b3939b
DJ
4734}
4735
3da10d80
KS
4736/* Add a DIE to the delayed physname list. */
4737
4738static void
4739add_to_method_list (struct type *type, int fnfield_index, int index,
4740 const char *name, struct die_info *die,
4741 struct dwarf2_cu *cu)
4742{
4743 struct delayed_method_info mi;
4744 mi.type = type;
4745 mi.fnfield_index = fnfield_index;
4746 mi.index = index;
4747 mi.name = name;
4748 mi.die = die;
4749 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4750}
4751
4752/* A cleanup for freeing the delayed method list. */
4753
4754static void
4755free_delayed_list (void *ptr)
4756{
4757 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4758 if (cu->method_list != NULL)
4759 {
4760 VEC_free (delayed_method_info, cu->method_list);
4761 cu->method_list = NULL;
4762 }
4763}
4764
4765/* Compute the physnames of any methods on the CU's method list.
4766
4767 The computation of method physnames is delayed in order to avoid the
4768 (bad) condition that one of the method's formal parameters is of an as yet
4769 incomplete type. */
4770
4771static void
4772compute_delayed_physnames (struct dwarf2_cu *cu)
4773{
4774 int i;
4775 struct delayed_method_info *mi;
4776 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4777 {
1d06ead6 4778 const char *physname;
3da10d80
KS
4779 struct fn_fieldlist *fn_flp
4780 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4781 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4782 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4783 }
4784}
4785
9cdd5dbd 4786/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4787 already been loaded into memory. */
4788
4789static void
4790process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4791{
10b3939b 4792 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4793 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4794 CORE_ADDR lowpc, highpc;
4795 struct symtab *symtab;
3da10d80 4796 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4797 CORE_ADDR baseaddr;
4798
4799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4800
10b3939b
DJ
4801 buildsym_init ();
4802 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4803 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4804
4805 cu->list_in_scope = &file_symbols;
c906108c
SS
4806
4807 /* Do line number decoding in read_file_scope () */
10b3939b 4808 process_die (cu->dies, cu);
c906108c 4809
3da10d80
KS
4810 /* Now that we have processed all the DIEs in the CU, all the types
4811 should be complete, and it should now be safe to compute all of the
4812 physnames. */
4813 compute_delayed_physnames (cu);
4814 do_cleanups (delayed_list_cleanup);
4815
fae299cd
DC
4816 /* Some compilers don't define a DW_AT_high_pc attribute for the
4817 compilation unit. If the DW_AT_high_pc is missing, synthesize
4818 it, by scanning the DIE's below the compilation unit. */
10b3939b 4819 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4820
613e1657 4821 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4822
8be455d7 4823 if (symtab != NULL)
c906108c 4824 {
df15bd07 4825 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4826
8be455d7
JK
4827 /* Set symtab language to language from DW_AT_language. If the
4828 compilation is from a C file generated by language preprocessors, do
4829 not set the language if it was already deduced by start_subfile. */
4830 if (!(cu->language == language_c && symtab->language != language_c))
4831 symtab->language = cu->language;
4832
4833 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4834 produce DW_AT_location with location lists but it can be possibly
4835 invalid without -fvar-tracking.
4836
4837 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4838 needed, it would be wrong due to missing DW_AT_producer there.
4839
4840 Still one can confuse GDB by using non-standard GCC compilation
4841 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4842 */
4632c0d0 4843 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4844 symtab->locations_valid = 1;
e0d00bc7
JK
4845
4846 if (gcc_4_minor >= 5)
4847 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4848
4849 symtab->call_site_htab = cu->call_site_htab;
c906108c 4850 }
9291a0cd
TT
4851
4852 if (dwarf2_per_objfile->using_index)
4853 per_cu->v.quick->symtab = symtab;
4854 else
4855 {
4856 struct partial_symtab *pst = per_cu->v.psymtab;
4857 pst->symtab = symtab;
4858 pst->readin = 1;
4859 }
c906108c
SS
4860
4861 do_cleanups (back_to);
4862}
4863
4864/* Process a die and its children. */
4865
4866static void
e7c27a73 4867process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4868{
4869 switch (die->tag)
4870 {
4871 case DW_TAG_padding:
4872 break;
4873 case DW_TAG_compile_unit:
e7c27a73 4874 read_file_scope (die, cu);
c906108c 4875 break;
348e048f
DE
4876 case DW_TAG_type_unit:
4877 read_type_unit_scope (die, cu);
4878 break;
c906108c 4879 case DW_TAG_subprogram:
c906108c 4880 case DW_TAG_inlined_subroutine:
edb3359d 4881 read_func_scope (die, cu);
c906108c
SS
4882 break;
4883 case DW_TAG_lexical_block:
14898363
L
4884 case DW_TAG_try_block:
4885 case DW_TAG_catch_block:
e7c27a73 4886 read_lexical_block_scope (die, cu);
c906108c 4887 break;
96408a79
SA
4888 case DW_TAG_GNU_call_site:
4889 read_call_site_scope (die, cu);
4890 break;
c906108c 4891 case DW_TAG_class_type:
680b30c7 4892 case DW_TAG_interface_type:
c906108c
SS
4893 case DW_TAG_structure_type:
4894 case DW_TAG_union_type:
134d01f1 4895 process_structure_scope (die, cu);
c906108c
SS
4896 break;
4897 case DW_TAG_enumeration_type:
134d01f1 4898 process_enumeration_scope (die, cu);
c906108c 4899 break;
134d01f1 4900
f792889a
DJ
4901 /* These dies have a type, but processing them does not create
4902 a symbol or recurse to process the children. Therefore we can
4903 read them on-demand through read_type_die. */
c906108c 4904 case DW_TAG_subroutine_type:
72019c9c 4905 case DW_TAG_set_type:
c906108c 4906 case DW_TAG_array_type:
c906108c 4907 case DW_TAG_pointer_type:
c906108c 4908 case DW_TAG_ptr_to_member_type:
c906108c 4909 case DW_TAG_reference_type:
c906108c 4910 case DW_TAG_string_type:
c906108c 4911 break;
134d01f1 4912
c906108c 4913 case DW_TAG_base_type:
a02abb62 4914 case DW_TAG_subrange_type:
cb249c71 4915 case DW_TAG_typedef:
134d01f1
DJ
4916 /* Add a typedef symbol for the type definition, if it has a
4917 DW_AT_name. */
f792889a 4918 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4919 break;
c906108c 4920 case DW_TAG_common_block:
e7c27a73 4921 read_common_block (die, cu);
c906108c
SS
4922 break;
4923 case DW_TAG_common_inclusion:
4924 break;
d9fa45fe 4925 case DW_TAG_namespace:
63d06c5c 4926 processing_has_namespace_info = 1;
e7c27a73 4927 read_namespace (die, cu);
d9fa45fe 4928 break;
5d7cb8df 4929 case DW_TAG_module:
f55ee35c 4930 processing_has_namespace_info = 1;
5d7cb8df
JK
4931 read_module (die, cu);
4932 break;
d9fa45fe
DC
4933 case DW_TAG_imported_declaration:
4934 case DW_TAG_imported_module:
63d06c5c 4935 processing_has_namespace_info = 1;
27aa8d6a
SW
4936 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4937 || cu->language != language_fortran))
4938 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4939 dwarf_tag_name (die->tag));
4940 read_import_statement (die, cu);
d9fa45fe 4941 break;
c906108c 4942 default:
e7c27a73 4943 new_symbol (die, NULL, cu);
c906108c
SS
4944 break;
4945 }
4946}
4947
94af9270
KS
4948/* A helper function for dwarf2_compute_name which determines whether DIE
4949 needs to have the name of the scope prepended to the name listed in the
4950 die. */
4951
4952static int
4953die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4954{
1c809c68
TT
4955 struct attribute *attr;
4956
94af9270
KS
4957 switch (die->tag)
4958 {
4959 case DW_TAG_namespace:
4960 case DW_TAG_typedef:
4961 case DW_TAG_class_type:
4962 case DW_TAG_interface_type:
4963 case DW_TAG_structure_type:
4964 case DW_TAG_union_type:
4965 case DW_TAG_enumeration_type:
4966 case DW_TAG_enumerator:
4967 case DW_TAG_subprogram:
4968 case DW_TAG_member:
4969 return 1;
4970
4971 case DW_TAG_variable:
c2b0a229 4972 case DW_TAG_constant:
94af9270
KS
4973 /* We only need to prefix "globally" visible variables. These include
4974 any variable marked with DW_AT_external or any variable that
4975 lives in a namespace. [Variables in anonymous namespaces
4976 require prefixing, but they are not DW_AT_external.] */
4977
4978 if (dwarf2_attr (die, DW_AT_specification, cu))
4979 {
4980 struct dwarf2_cu *spec_cu = cu;
9a619af0 4981
94af9270
KS
4982 return die_needs_namespace (die_specification (die, &spec_cu),
4983 spec_cu);
4984 }
4985
1c809c68 4986 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4987 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4988 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4989 return 0;
4990 /* A variable in a lexical block of some kind does not need a
4991 namespace, even though in C++ such variables may be external
4992 and have a mangled name. */
4993 if (die->parent->tag == DW_TAG_lexical_block
4994 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4995 || die->parent->tag == DW_TAG_catch_block
4996 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4997 return 0;
4998 return 1;
94af9270
KS
4999
5000 default:
5001 return 0;
5002 }
5003}
5004
98bfdba5
PA
5005/* Retrieve the last character from a mem_file. */
5006
5007static void
5008do_ui_file_peek_last (void *object, const char *buffer, long length)
5009{
5010 char *last_char_p = (char *) object;
5011
5012 if (length > 0)
5013 *last_char_p = buffer[length - 1];
5014}
5015
94af9270
KS
5016/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5017 compute the physname for the object, which include a method's
5018 formal parameters (C++/Java) and return type (Java).
5019
af6b7be1
JB
5020 For Ada, return the DIE's linkage name rather than the fully qualified
5021 name. PHYSNAME is ignored..
5022
94af9270
KS
5023 The result is allocated on the objfile_obstack and canonicalized. */
5024
5025static const char *
5026dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5027 int physname)
5028{
bb5ed363
DE
5029 struct objfile *objfile = cu->objfile;
5030
94af9270
KS
5031 if (name == NULL)
5032 name = dwarf2_name (die, cu);
5033
f55ee35c
JK
5034 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5035 compute it by typename_concat inside GDB. */
5036 if (cu->language == language_ada
5037 || (cu->language == language_fortran && physname))
5038 {
5039 /* For Ada unit, we prefer the linkage name over the name, as
5040 the former contains the exported name, which the user expects
5041 to be able to reference. Ideally, we want the user to be able
5042 to reference this entity using either natural or linkage name,
5043 but we haven't started looking at this enhancement yet. */
5044 struct attribute *attr;
5045
5046 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5047 if (attr == NULL)
5048 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5049 if (attr && DW_STRING (attr))
5050 return DW_STRING (attr);
5051 }
5052
94af9270
KS
5053 /* These are the only languages we know how to qualify names in. */
5054 if (name != NULL
f55ee35c
JK
5055 && (cu->language == language_cplus || cu->language == language_java
5056 || cu->language == language_fortran))
94af9270
KS
5057 {
5058 if (die_needs_namespace (die, cu))
5059 {
5060 long length;
0d5cff50 5061 const char *prefix;
94af9270
KS
5062 struct ui_file *buf;
5063
5064 prefix = determine_prefix (die, cu);
5065 buf = mem_fileopen ();
5066 if (*prefix != '\0')
5067 {
f55ee35c
JK
5068 char *prefixed_name = typename_concat (NULL, prefix, name,
5069 physname, cu);
9a619af0 5070
94af9270
KS
5071 fputs_unfiltered (prefixed_name, buf);
5072 xfree (prefixed_name);
5073 }
5074 else
62d5b8da 5075 fputs_unfiltered (name, buf);
94af9270 5076
98bfdba5
PA
5077 /* Template parameters may be specified in the DIE's DW_AT_name, or
5078 as children with DW_TAG_template_type_param or
5079 DW_TAG_value_type_param. If the latter, add them to the name
5080 here. If the name already has template parameters, then
5081 skip this step; some versions of GCC emit both, and
5082 it is more efficient to use the pre-computed name.
5083
5084 Something to keep in mind about this process: it is very
5085 unlikely, or in some cases downright impossible, to produce
5086 something that will match the mangled name of a function.
5087 If the definition of the function has the same debug info,
5088 we should be able to match up with it anyway. But fallbacks
5089 using the minimal symbol, for instance to find a method
5090 implemented in a stripped copy of libstdc++, will not work.
5091 If we do not have debug info for the definition, we will have to
5092 match them up some other way.
5093
5094 When we do name matching there is a related problem with function
5095 templates; two instantiated function templates are allowed to
5096 differ only by their return types, which we do not add here. */
5097
5098 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5099 {
5100 struct attribute *attr;
5101 struct die_info *child;
5102 int first = 1;
5103
5104 die->building_fullname = 1;
5105
5106 for (child = die->child; child != NULL; child = child->sibling)
5107 {
5108 struct type *type;
5109 long value;
5110 gdb_byte *bytes;
5111 struct dwarf2_locexpr_baton *baton;
5112 struct value *v;
5113
5114 if (child->tag != DW_TAG_template_type_param
5115 && child->tag != DW_TAG_template_value_param)
5116 continue;
5117
5118 if (first)
5119 {
5120 fputs_unfiltered ("<", buf);
5121 first = 0;
5122 }
5123 else
5124 fputs_unfiltered (", ", buf);
5125
5126 attr = dwarf2_attr (child, DW_AT_type, cu);
5127 if (attr == NULL)
5128 {
5129 complaint (&symfile_complaints,
5130 _("template parameter missing DW_AT_type"));
5131 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5132 continue;
5133 }
5134 type = die_type (child, cu);
5135
5136 if (child->tag == DW_TAG_template_type_param)
5137 {
5138 c_print_type (type, "", buf, -1, 0);
5139 continue;
5140 }
5141
5142 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5143 if (attr == NULL)
5144 {
5145 complaint (&symfile_complaints,
3e43a32a
MS
5146 _("template parameter missing "
5147 "DW_AT_const_value"));
98bfdba5
PA
5148 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5149 continue;
5150 }
5151
5152 dwarf2_const_value_attr (attr, type, name,
5153 &cu->comp_unit_obstack, cu,
5154 &value, &bytes, &baton);
5155
5156 if (TYPE_NOSIGN (type))
5157 /* GDB prints characters as NUMBER 'CHAR'. If that's
5158 changed, this can use value_print instead. */
5159 c_printchar (value, type, buf);
5160 else
5161 {
5162 struct value_print_options opts;
5163
5164 if (baton != NULL)
5165 v = dwarf2_evaluate_loc_desc (type, NULL,
5166 baton->data,
5167 baton->size,
5168 baton->per_cu);
5169 else if (bytes != NULL)
5170 {
5171 v = allocate_value (type);
5172 memcpy (value_contents_writeable (v), bytes,
5173 TYPE_LENGTH (type));
5174 }
5175 else
5176 v = value_from_longest (type, value);
5177
3e43a32a
MS
5178 /* Specify decimal so that we do not depend on
5179 the radix. */
98bfdba5
PA
5180 get_formatted_print_options (&opts, 'd');
5181 opts.raw = 1;
5182 value_print (v, buf, &opts);
5183 release_value (v);
5184 value_free (v);
5185 }
5186 }
5187
5188 die->building_fullname = 0;
5189
5190 if (!first)
5191 {
5192 /* Close the argument list, with a space if necessary
5193 (nested templates). */
5194 char last_char = '\0';
5195 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5196 if (last_char == '>')
5197 fputs_unfiltered (" >", buf);
5198 else
5199 fputs_unfiltered (">", buf);
5200 }
5201 }
5202
94af9270
KS
5203 /* For Java and C++ methods, append formal parameter type
5204 information, if PHYSNAME. */
6e70227d 5205
94af9270
KS
5206 if (physname && die->tag == DW_TAG_subprogram
5207 && (cu->language == language_cplus
5208 || cu->language == language_java))
5209 {
5210 struct type *type = read_type_die (die, cu);
5211
3167638f 5212 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5213
5214 if (cu->language == language_java)
5215 {
5216 /* For java, we must append the return type to method
0963b4bd 5217 names. */
94af9270
KS
5218 if (die->tag == DW_TAG_subprogram)
5219 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5220 0, 0);
5221 }
5222 else if (cu->language == language_cplus)
5223 {
60430eff
DJ
5224 /* Assume that an artificial first parameter is
5225 "this", but do not crash if it is not. RealView
5226 marks unnamed (and thus unused) parameters as
5227 artificial; there is no way to differentiate
5228 the two cases. */
94af9270
KS
5229 if (TYPE_NFIELDS (type) > 0
5230 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5231 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5232 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5233 0))))
94af9270
KS
5234 fputs_unfiltered (" const", buf);
5235 }
5236 }
5237
bb5ed363 5238 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5239 &length);
5240 ui_file_delete (buf);
5241
5242 if (cu->language == language_cplus)
5243 {
5244 char *cname
5245 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5246 &objfile->objfile_obstack);
9a619af0 5247
94af9270
KS
5248 if (cname != NULL)
5249 name = cname;
5250 }
5251 }
5252 }
5253
5254 return name;
5255}
5256
0114d602
DJ
5257/* Return the fully qualified name of DIE, based on its DW_AT_name.
5258 If scope qualifiers are appropriate they will be added. The result
5259 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5260 not have a name. NAME may either be from a previous call to
5261 dwarf2_name or NULL.
5262
0963b4bd 5263 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5264
5265static const char *
94af9270 5266dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5267{
94af9270
KS
5268 return dwarf2_compute_name (name, die, cu, 0);
5269}
0114d602 5270
94af9270
KS
5271/* Construct a physname for the given DIE in CU. NAME may either be
5272 from a previous call to dwarf2_name or NULL. The result will be
5273 allocated on the objfile_objstack or NULL if the DIE does not have a
5274 name.
0114d602 5275
94af9270 5276 The output string will be canonicalized (if C++/Java). */
0114d602 5277
94af9270
KS
5278static const char *
5279dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5280{
bb5ed363 5281 struct objfile *objfile = cu->objfile;
900e11f9
JK
5282 struct attribute *attr;
5283 const char *retval, *mangled = NULL, *canon = NULL;
5284 struct cleanup *back_to;
5285 int need_copy = 1;
5286
5287 /* In this case dwarf2_compute_name is just a shortcut not building anything
5288 on its own. */
5289 if (!die_needs_namespace (die, cu))
5290 return dwarf2_compute_name (name, die, cu, 1);
5291
5292 back_to = make_cleanup (null_cleanup, NULL);
5293
5294 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5295 if (!attr)
5296 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5297
5298 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5299 has computed. */
5300 if (attr && DW_STRING (attr))
5301 {
5302 char *demangled;
5303
5304 mangled = DW_STRING (attr);
5305
5306 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5307 type. It is easier for GDB users to search for such functions as
5308 `name(params)' than `long name(params)'. In such case the minimal
5309 symbol names do not match the full symbol names but for template
5310 functions there is never a need to look up their definition from their
5311 declaration so the only disadvantage remains the minimal symbol
5312 variant `long name(params)' does not have the proper inferior type.
5313 */
5314
5315 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5316 | (cu->language == language_java
5317 ? DMGL_JAVA | DMGL_RET_POSTFIX
5318 : DMGL_RET_DROP)));
5319 if (demangled)
5320 {
5321 make_cleanup (xfree, demangled);
5322 canon = demangled;
5323 }
5324 else
5325 {
5326 canon = mangled;
5327 need_copy = 0;
5328 }
5329 }
5330
5331 if (canon == NULL || check_physname)
5332 {
5333 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5334
5335 if (canon != NULL && strcmp (physname, canon) != 0)
5336 {
5337 /* It may not mean a bug in GDB. The compiler could also
5338 compute DW_AT_linkage_name incorrectly. But in such case
5339 GDB would need to be bug-to-bug compatible. */
5340
5341 complaint (&symfile_complaints,
5342 _("Computed physname <%s> does not match demangled <%s> "
5343 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
bb5ed363 5344 physname, canon, mangled, die->offset, objfile->name);
900e11f9
JK
5345
5346 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5347 is available here - over computed PHYSNAME. It is safer
5348 against both buggy GDB and buggy compilers. */
5349
5350 retval = canon;
5351 }
5352 else
5353 {
5354 retval = physname;
5355 need_copy = 0;
5356 }
5357 }
5358 else
5359 retval = canon;
5360
5361 if (need_copy)
5362 retval = obsavestring (retval, strlen (retval),
bb5ed363 5363 &objfile->objfile_obstack);
900e11f9
JK
5364
5365 do_cleanups (back_to);
5366 return retval;
0114d602
DJ
5367}
5368
27aa8d6a
SW
5369/* Read the import statement specified by the given die and record it. */
5370
5371static void
5372read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5373{
bb5ed363 5374 struct objfile *objfile = cu->objfile;
27aa8d6a 5375 struct attribute *import_attr;
32019081 5376 struct die_info *imported_die, *child_die;
de4affc9 5377 struct dwarf2_cu *imported_cu;
27aa8d6a 5378 const char *imported_name;
794684b6 5379 const char *imported_name_prefix;
13387711
SW
5380 const char *canonical_name;
5381 const char *import_alias;
5382 const char *imported_declaration = NULL;
794684b6 5383 const char *import_prefix;
32019081
JK
5384 VEC (const_char_ptr) *excludes = NULL;
5385 struct cleanup *cleanups;
13387711
SW
5386
5387 char *temp;
27aa8d6a
SW
5388
5389 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5390 if (import_attr == NULL)
5391 {
5392 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5393 dwarf_tag_name (die->tag));
5394 return;
5395 }
5396
de4affc9
CC
5397 imported_cu = cu;
5398 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5399 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5400 if (imported_name == NULL)
5401 {
5402 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5403
5404 The import in the following code:
5405 namespace A
5406 {
5407 typedef int B;
5408 }
5409
5410 int main ()
5411 {
5412 using A::B;
5413 B b;
5414 return b;
5415 }
5416
5417 ...
5418 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5419 <52> DW_AT_decl_file : 1
5420 <53> DW_AT_decl_line : 6
5421 <54> DW_AT_import : <0x75>
5422 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5423 <59> DW_AT_name : B
5424 <5b> DW_AT_decl_file : 1
5425 <5c> DW_AT_decl_line : 2
5426 <5d> DW_AT_type : <0x6e>
5427 ...
5428 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5429 <76> DW_AT_byte_size : 4
5430 <77> DW_AT_encoding : 5 (signed)
5431
5432 imports the wrong die ( 0x75 instead of 0x58 ).
5433 This case will be ignored until the gcc bug is fixed. */
5434 return;
5435 }
5436
82856980
SW
5437 /* Figure out the local name after import. */
5438 import_alias = dwarf2_name (die, cu);
27aa8d6a 5439
794684b6
SW
5440 /* Figure out where the statement is being imported to. */
5441 import_prefix = determine_prefix (die, cu);
5442
5443 /* Figure out what the scope of the imported die is and prepend it
5444 to the name of the imported die. */
de4affc9 5445 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5446
f55ee35c
JK
5447 if (imported_die->tag != DW_TAG_namespace
5448 && imported_die->tag != DW_TAG_module)
794684b6 5449 {
13387711
SW
5450 imported_declaration = imported_name;
5451 canonical_name = imported_name_prefix;
794684b6 5452 }
13387711 5453 else if (strlen (imported_name_prefix) > 0)
794684b6 5454 {
13387711
SW
5455 temp = alloca (strlen (imported_name_prefix)
5456 + 2 + strlen (imported_name) + 1);
5457 strcpy (temp, imported_name_prefix);
5458 strcat (temp, "::");
5459 strcat (temp, imported_name);
5460 canonical_name = temp;
794684b6 5461 }
13387711
SW
5462 else
5463 canonical_name = imported_name;
794684b6 5464
32019081
JK
5465 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5466
5467 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5468 for (child_die = die->child; child_die && child_die->tag;
5469 child_die = sibling_die (child_die))
5470 {
5471 /* DWARF-4: A Fortran use statement with a “rename list” may be
5472 represented by an imported module entry with an import attribute
5473 referring to the module and owned entries corresponding to those
5474 entities that are renamed as part of being imported. */
5475
5476 if (child_die->tag != DW_TAG_imported_declaration)
5477 {
5478 complaint (&symfile_complaints,
5479 _("child DW_TAG_imported_declaration expected "
5480 "- DIE at 0x%x [in module %s]"),
bb5ed363 5481 child_die->offset, objfile->name);
32019081
JK
5482 continue;
5483 }
5484
5485 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5486 if (import_attr == NULL)
5487 {
5488 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5489 dwarf_tag_name (child_die->tag));
5490 continue;
5491 }
5492
5493 imported_cu = cu;
5494 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5495 &imported_cu);
5496 imported_name = dwarf2_name (imported_die, imported_cu);
5497 if (imported_name == NULL)
5498 {
5499 complaint (&symfile_complaints,
5500 _("child DW_TAG_imported_declaration has unknown "
5501 "imported name - DIE at 0x%x [in module %s]"),
bb5ed363 5502 child_die->offset, objfile->name);
32019081
JK
5503 continue;
5504 }
5505
5506 VEC_safe_push (const_char_ptr, excludes, imported_name);
5507
5508 process_die (child_die, cu);
5509 }
5510
c0cc3a76
SW
5511 cp_add_using_directive (import_prefix,
5512 canonical_name,
5513 import_alias,
13387711 5514 imported_declaration,
32019081 5515 excludes,
bb5ed363 5516 &objfile->objfile_obstack);
32019081
JK
5517
5518 do_cleanups (cleanups);
27aa8d6a
SW
5519}
5520
ae2de4f8
DE
5521/* Cleanup function for read_file_scope. */
5522
cb1df416
DJ
5523static void
5524free_cu_line_header (void *arg)
5525{
5526 struct dwarf2_cu *cu = arg;
5527
5528 free_line_header (cu->line_header);
5529 cu->line_header = NULL;
5530}
5531
9291a0cd
TT
5532static void
5533find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5534 char **name, char **comp_dir)
5535{
5536 struct attribute *attr;
5537
5538 *name = NULL;
5539 *comp_dir = NULL;
5540
5541 /* Find the filename. Do not use dwarf2_name here, since the filename
5542 is not a source language identifier. */
5543 attr = dwarf2_attr (die, DW_AT_name, cu);
5544 if (attr)
5545 {
5546 *name = DW_STRING (attr);
5547 }
5548
5549 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5550 if (attr)
5551 *comp_dir = DW_STRING (attr);
5552 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5553 {
5554 *comp_dir = ldirname (*name);
5555 if (*comp_dir != NULL)
5556 make_cleanup (xfree, *comp_dir);
5557 }
5558 if (*comp_dir != NULL)
5559 {
5560 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5561 directory, get rid of it. */
5562 char *cp = strchr (*comp_dir, ':');
5563
5564 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5565 *comp_dir = cp + 1;
5566 }
5567
5568 if (*name == NULL)
5569 *name = "<unknown>";
5570}
5571
f3f5162e
DE
5572/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5573 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5574 COMP_DIR is the compilation directory.
5575 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5576
5577static void
5578handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5579 const char *comp_dir, int want_line_info)
2ab95328
TT
5580{
5581 struct attribute *attr;
5582 struct objfile *objfile = cu->objfile;
5583 bfd *abfd = objfile->obfd;
5584
2ab95328
TT
5585 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5586 if (attr)
5587 {
5588 unsigned int line_offset = DW_UNSND (attr);
5589 struct line_header *line_header
5590 = dwarf_decode_line_header (line_offset, abfd, cu);
5591
5592 if (line_header)
5593 {
5594 cu->line_header = line_header;
5595 make_cleanup (free_cu_line_header, cu);
f3f5162e 5596 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5597 }
5598 }
5599}
5600
ae2de4f8
DE
5601/* Process DW_TAG_compile_unit. */
5602
c906108c 5603static void
e7c27a73 5604read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5605{
e7c27a73 5606 struct objfile *objfile = cu->objfile;
debd256d 5607 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5608 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5609 CORE_ADDR highpc = ((CORE_ADDR) 0);
5610 struct attribute *attr;
e1024ff1 5611 char *name = NULL;
c906108c
SS
5612 char *comp_dir = NULL;
5613 struct die_info *child_die;
5614 bfd *abfd = objfile->obfd;
e142c38c 5615 CORE_ADDR baseaddr;
6e70227d 5616
e142c38c 5617 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5618
fae299cd 5619 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5620
5621 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5622 from finish_block. */
2acceee2 5623 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5624 lowpc = highpc;
5625 lowpc += baseaddr;
5626 highpc += baseaddr;
5627
9291a0cd 5628 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5629
e142c38c 5630 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5631 if (attr)
5632 {
e142c38c 5633 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5634 }
5635
b0f35d58 5636 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5637 if (attr)
b0f35d58 5638 cu->producer = DW_STRING (attr);
303b6f5d 5639
f4b8a18d
KW
5640 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5641 standardised yet. As a workaround for the language detection we fall
5642 back to the DW_AT_producer string. */
5643 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5644 cu->language = language_opencl;
5645
0963b4bd 5646 /* We assume that we're processing GCC output. */
c906108c 5647 processing_gcc_compilation = 2;
c906108c 5648
df8a16a1
DJ
5649 processing_has_namespace_info = 0;
5650
c906108c
SS
5651 start_symtab (name, comp_dir, lowpc);
5652 record_debugformat ("DWARF 2");
303b6f5d 5653 record_producer (cu->producer);
c906108c 5654
f3f5162e
DE
5655 /* Decode line number information if present. We do this before
5656 processing child DIEs, so that the line header table is available
5657 for DW_AT_decl_file. */
5658 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5659
cb1df416
DJ
5660 /* Process all dies in compilation unit. */
5661 if (die->child != NULL)
5662 {
5663 child_die = die->child;
5664 while (child_die && child_die->tag)
5665 {
5666 process_die (child_die, cu);
5667 child_die = sibling_die (child_die);
5668 }
5669 }
5670
2e276125
JB
5671 /* Decode macro information, if present. Dwarf 2 macro information
5672 refers to information in the line number info statement program
5673 header, so we can only read it if we've read the header
5674 successfully. */
cf2c3c16 5675 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5676 if (attr && cu->line_header)
2e276125 5677 {
cf2c3c16
TT
5678 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5679 complaint (&symfile_complaints,
5680 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5681
5682 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5683 comp_dir, abfd, cu,
5684 &dwarf2_per_objfile->macro, 1);
5685 }
5686 else
5687 {
5688 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5689 if (attr && cu->line_header)
5690 {
5691 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5692
cf2c3c16
TT
5693 dwarf_decode_macros (cu->line_header, macro_offset,
5694 comp_dir, abfd, cu,
5695 &dwarf2_per_objfile->macinfo, 0);
5696 }
2e276125 5697 }
9cdd5dbd 5698
debd256d 5699 do_cleanups (back_to);
5fb290d7
DJ
5700}
5701
ae2de4f8
DE
5702/* Process DW_TAG_type_unit.
5703 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5704 actual type being defined by this TU. In this case the first top
5705 level sibling is there to provide context only. */
5706
5707static void
5708read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5709{
5710 struct objfile *objfile = cu->objfile;
5711 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5712 CORE_ADDR lowpc;
5713 struct attribute *attr;
5714 char *name = NULL;
5715 char *comp_dir = NULL;
5716 struct die_info *child_die;
5717 bfd *abfd = objfile->obfd;
348e048f
DE
5718
5719 /* start_symtab needs a low pc, but we don't really have one.
5720 Do what read_file_scope would do in the absence of such info. */
5721 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5722
5723 /* Find the filename. Do not use dwarf2_name here, since the filename
5724 is not a source language identifier. */
5725 attr = dwarf2_attr (die, DW_AT_name, cu);
5726 if (attr)
5727 name = DW_STRING (attr);
5728
5729 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5730 if (attr)
5731 comp_dir = DW_STRING (attr);
5732 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5733 {
5734 comp_dir = ldirname (name);
5735 if (comp_dir != NULL)
5736 make_cleanup (xfree, comp_dir);
5737 }
5738
5739 if (name == NULL)
5740 name = "<unknown>";
5741
5742 attr = dwarf2_attr (die, DW_AT_language, cu);
5743 if (attr)
5744 set_cu_language (DW_UNSND (attr), cu);
5745
5746 /* This isn't technically needed today. It is done for symmetry
5747 with read_file_scope. */
5748 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5749 if (attr)
348e048f
DE
5750 cu->producer = DW_STRING (attr);
5751
0963b4bd 5752 /* We assume that we're processing GCC output. */
348e048f
DE
5753 processing_gcc_compilation = 2;
5754
5755 processing_has_namespace_info = 0;
5756
5757 start_symtab (name, comp_dir, lowpc);
5758 record_debugformat ("DWARF 2");
5759 record_producer (cu->producer);
5760
f3f5162e
DE
5761 /* Decode line number information if present. We do this before
5762 processing child DIEs, so that the line header table is available
5763 for DW_AT_decl_file.
5764 We don't need the pc/line-number mapping for type units. */
5765 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5766
348e048f
DE
5767 /* Process the dies in the type unit. */
5768 if (die->child == NULL)
5769 {
5770 dump_die_for_error (die);
5771 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5772 bfd_get_filename (abfd));
5773 }
5774
5775 child_die = die->child;
5776
5777 while (child_die && child_die->tag)
5778 {
5779 process_die (child_die, cu);
5780
5781 child_die = sibling_die (child_die);
5782 }
5783
5784 do_cleanups (back_to);
5785}
5786
d389af10
JK
5787/* qsort helper for inherit_abstract_dies. */
5788
5789static int
5790unsigned_int_compar (const void *ap, const void *bp)
5791{
5792 unsigned int a = *(unsigned int *) ap;
5793 unsigned int b = *(unsigned int *) bp;
5794
5795 return (a > b) - (b > a);
5796}
5797
5798/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5799 Inherit only the children of the DW_AT_abstract_origin DIE not being
5800 already referenced by DW_AT_abstract_origin from the children of the
5801 current DIE. */
d389af10
JK
5802
5803static void
5804inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5805{
5806 struct die_info *child_die;
5807 unsigned die_children_count;
5808 /* CU offsets which were referenced by children of the current DIE. */
5809 unsigned *offsets;
5810 unsigned *offsets_end, *offsetp;
5811 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5812 struct die_info *origin_die;
5813 /* Iterator of the ORIGIN_DIE children. */
5814 struct die_info *origin_child_die;
5815 struct cleanup *cleanups;
5816 struct attribute *attr;
cd02d79d
PA
5817 struct dwarf2_cu *origin_cu;
5818 struct pending **origin_previous_list_in_scope;
d389af10
JK
5819
5820 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5821 if (!attr)
5822 return;
5823
cd02d79d
PA
5824 /* Note that following die references may follow to a die in a
5825 different cu. */
5826
5827 origin_cu = cu;
5828 origin_die = follow_die_ref (die, attr, &origin_cu);
5829
5830 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5831 symbols in. */
5832 origin_previous_list_in_scope = origin_cu->list_in_scope;
5833 origin_cu->list_in_scope = cu->list_in_scope;
5834
edb3359d
DJ
5835 if (die->tag != origin_die->tag
5836 && !(die->tag == DW_TAG_inlined_subroutine
5837 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5838 complaint (&symfile_complaints,
5839 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5840 die->offset, origin_die->offset);
5841
5842 child_die = die->child;
5843 die_children_count = 0;
5844 while (child_die && child_die->tag)
5845 {
5846 child_die = sibling_die (child_die);
5847 die_children_count++;
5848 }
5849 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5850 cleanups = make_cleanup (xfree, offsets);
5851
5852 offsets_end = offsets;
5853 child_die = die->child;
5854 while (child_die && child_die->tag)
5855 {
c38f313d
DJ
5856 /* For each CHILD_DIE, find the corresponding child of
5857 ORIGIN_DIE. If there is more than one layer of
5858 DW_AT_abstract_origin, follow them all; there shouldn't be,
5859 but GCC versions at least through 4.4 generate this (GCC PR
5860 40573). */
5861 struct die_info *child_origin_die = child_die;
cd02d79d 5862 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5863
c38f313d
DJ
5864 while (1)
5865 {
cd02d79d
PA
5866 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5867 child_origin_cu);
c38f313d
DJ
5868 if (attr == NULL)
5869 break;
cd02d79d
PA
5870 child_origin_die = follow_die_ref (child_origin_die, attr,
5871 &child_origin_cu);
c38f313d
DJ
5872 }
5873
d389af10
JK
5874 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5875 counterpart may exist. */
c38f313d 5876 if (child_origin_die != child_die)
d389af10 5877 {
edb3359d
DJ
5878 if (child_die->tag != child_origin_die->tag
5879 && !(child_die->tag == DW_TAG_inlined_subroutine
5880 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5881 complaint (&symfile_complaints,
5882 _("Child DIE 0x%x and its abstract origin 0x%x have "
5883 "different tags"), child_die->offset,
5884 child_origin_die->offset);
c38f313d
DJ
5885 if (child_origin_die->parent != origin_die)
5886 complaint (&symfile_complaints,
5887 _("Child DIE 0x%x and its abstract origin 0x%x have "
5888 "different parents"), child_die->offset,
5889 child_origin_die->offset);
5890 else
5891 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5892 }
5893 child_die = sibling_die (child_die);
5894 }
5895 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5896 unsigned_int_compar);
5897 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5898 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5899 complaint (&symfile_complaints,
5900 _("Multiple children of DIE 0x%x refer "
5901 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5902 die->offset, *offsetp);
5903
5904 offsetp = offsets;
5905 origin_child_die = origin_die->child;
5906 while (origin_child_die && origin_child_die->tag)
5907 {
5908 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5909 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5910 offsetp++;
5911 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5912 {
5913 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5914 process_die (origin_child_die, origin_cu);
d389af10
JK
5915 }
5916 origin_child_die = sibling_die (origin_child_die);
5917 }
cd02d79d 5918 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5919
5920 do_cleanups (cleanups);
5921}
5922
c906108c 5923static void
e7c27a73 5924read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5925{
e7c27a73 5926 struct objfile *objfile = cu->objfile;
52f0bd74 5927 struct context_stack *new;
c906108c
SS
5928 CORE_ADDR lowpc;
5929 CORE_ADDR highpc;
5930 struct die_info *child_die;
edb3359d 5931 struct attribute *attr, *call_line, *call_file;
c906108c 5932 char *name;
e142c38c 5933 CORE_ADDR baseaddr;
801e3a5b 5934 struct block *block;
edb3359d 5935 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5936 VEC (symbolp) *template_args = NULL;
5937 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5938
5939 if (inlined_func)
5940 {
5941 /* If we do not have call site information, we can't show the
5942 caller of this inlined function. That's too confusing, so
5943 only use the scope for local variables. */
5944 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5945 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5946 if (call_line == NULL || call_file == NULL)
5947 {
5948 read_lexical_block_scope (die, cu);
5949 return;
5950 }
5951 }
c906108c 5952
e142c38c
DJ
5953 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5954
94af9270 5955 name = dwarf2_name (die, cu);
c906108c 5956
e8d05480
JB
5957 /* Ignore functions with missing or empty names. These are actually
5958 illegal according to the DWARF standard. */
5959 if (name == NULL)
5960 {
5961 complaint (&symfile_complaints,
5962 _("missing name for subprogram DIE at %d"), die->offset);
5963 return;
5964 }
5965
5966 /* Ignore functions with missing or invalid low and high pc attributes. */
5967 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5968 {
ae4d0c03
PM
5969 attr = dwarf2_attr (die, DW_AT_external, cu);
5970 if (!attr || !DW_UNSND (attr))
5971 complaint (&symfile_complaints,
3e43a32a
MS
5972 _("cannot get low and high bounds "
5973 "for subprogram DIE at %d"),
ae4d0c03 5974 die->offset);
e8d05480
JB
5975 return;
5976 }
c906108c
SS
5977
5978 lowpc += baseaddr;
5979 highpc += baseaddr;
5980
34eaf542
TT
5981 /* If we have any template arguments, then we must allocate a
5982 different sort of symbol. */
5983 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5984 {
5985 if (child_die->tag == DW_TAG_template_type_param
5986 || child_die->tag == DW_TAG_template_value_param)
5987 {
5988 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5989 struct template_symbol);
5990 templ_func->base.is_cplus_template_function = 1;
5991 break;
5992 }
5993 }
5994
c906108c 5995 new = push_context (0, lowpc);
34eaf542
TT
5996 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5997 (struct symbol *) templ_func);
4c2df51b 5998
4cecd739
DJ
5999 /* If there is a location expression for DW_AT_frame_base, record
6000 it. */
e142c38c 6001 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6002 if (attr)
c034e007
AC
6003 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6004 expression is being recorded directly in the function's symbol
6005 and not in a separate frame-base object. I guess this hack is
6006 to avoid adding some sort of frame-base adjunct/annex to the
6007 function's symbol :-(. The problem with doing this is that it
6008 results in a function symbol with a location expression that
6009 has nothing to do with the location of the function, ouch! The
6010 relationship should be: a function's symbol has-a frame base; a
6011 frame-base has-a location expression. */
e7c27a73 6012 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6013
e142c38c 6014 cu->list_in_scope = &local_symbols;
c906108c 6015
639d11d3 6016 if (die->child != NULL)
c906108c 6017 {
639d11d3 6018 child_die = die->child;
c906108c
SS
6019 while (child_die && child_die->tag)
6020 {
34eaf542
TT
6021 if (child_die->tag == DW_TAG_template_type_param
6022 || child_die->tag == DW_TAG_template_value_param)
6023 {
6024 struct symbol *arg = new_symbol (child_die, NULL, cu);
6025
f1078f66
DJ
6026 if (arg != NULL)
6027 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6028 }
6029 else
6030 process_die (child_die, cu);
c906108c
SS
6031 child_die = sibling_die (child_die);
6032 }
6033 }
6034
d389af10
JK
6035 inherit_abstract_dies (die, cu);
6036
4a811a97
UW
6037 /* If we have a DW_AT_specification, we might need to import using
6038 directives from the context of the specification DIE. See the
6039 comment in determine_prefix. */
6040 if (cu->language == language_cplus
6041 && dwarf2_attr (die, DW_AT_specification, cu))
6042 {
6043 struct dwarf2_cu *spec_cu = cu;
6044 struct die_info *spec_die = die_specification (die, &spec_cu);
6045
6046 while (spec_die)
6047 {
6048 child_die = spec_die->child;
6049 while (child_die && child_die->tag)
6050 {
6051 if (child_die->tag == DW_TAG_imported_module)
6052 process_die (child_die, spec_cu);
6053 child_die = sibling_die (child_die);
6054 }
6055
6056 /* In some cases, GCC generates specification DIEs that
6057 themselves contain DW_AT_specification attributes. */
6058 spec_die = die_specification (spec_die, &spec_cu);
6059 }
6060 }
6061
c906108c
SS
6062 new = pop_context ();
6063 /* Make a block for the local symbols within. */
801e3a5b
JB
6064 block = finish_block (new->name, &local_symbols, new->old_blocks,
6065 lowpc, highpc, objfile);
6066
df8a16a1 6067 /* For C++, set the block's scope. */
f55ee35c 6068 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6069 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6070 determine_prefix (die, cu),
df8a16a1
DJ
6071 processing_has_namespace_info);
6072
801e3a5b
JB
6073 /* If we have address ranges, record them. */
6074 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6075
34eaf542
TT
6076 /* Attach template arguments to function. */
6077 if (! VEC_empty (symbolp, template_args))
6078 {
6079 gdb_assert (templ_func != NULL);
6080
6081 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6082 templ_func->template_arguments
6083 = obstack_alloc (&objfile->objfile_obstack,
6084 (templ_func->n_template_arguments
6085 * sizeof (struct symbol *)));
6086 memcpy (templ_func->template_arguments,
6087 VEC_address (symbolp, template_args),
6088 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6089 VEC_free (symbolp, template_args);
6090 }
6091
208d8187
JB
6092 /* In C++, we can have functions nested inside functions (e.g., when
6093 a function declares a class that has methods). This means that
6094 when we finish processing a function scope, we may need to go
6095 back to building a containing block's symbol lists. */
6096 local_symbols = new->locals;
6097 param_symbols = new->params;
27aa8d6a 6098 using_directives = new->using_directives;
208d8187 6099
921e78cf
JB
6100 /* If we've finished processing a top-level function, subsequent
6101 symbols go in the file symbol list. */
6102 if (outermost_context_p ())
e142c38c 6103 cu->list_in_scope = &file_symbols;
c906108c
SS
6104}
6105
6106/* Process all the DIES contained within a lexical block scope. Start
6107 a new scope, process the dies, and then close the scope. */
6108
6109static void
e7c27a73 6110read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6111{
e7c27a73 6112 struct objfile *objfile = cu->objfile;
52f0bd74 6113 struct context_stack *new;
c906108c
SS
6114 CORE_ADDR lowpc, highpc;
6115 struct die_info *child_die;
e142c38c
DJ
6116 CORE_ADDR baseaddr;
6117
6118 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6119
6120 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6121 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6122 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6123 be nasty. Might be easier to properly extend generic blocks to
af34e669 6124 describe ranges. */
d85a05f0 6125 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6126 return;
6127 lowpc += baseaddr;
6128 highpc += baseaddr;
6129
6130 push_context (0, lowpc);
639d11d3 6131 if (die->child != NULL)
c906108c 6132 {
639d11d3 6133 child_die = die->child;
c906108c
SS
6134 while (child_die && child_die->tag)
6135 {
e7c27a73 6136 process_die (child_die, cu);
c906108c
SS
6137 child_die = sibling_die (child_die);
6138 }
6139 }
6140 new = pop_context ();
6141
8540c487 6142 if (local_symbols != NULL || using_directives != NULL)
c906108c 6143 {
801e3a5b
JB
6144 struct block *block
6145 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6146 highpc, objfile);
6147
6148 /* Note that recording ranges after traversing children, as we
6149 do here, means that recording a parent's ranges entails
6150 walking across all its children's ranges as they appear in
6151 the address map, which is quadratic behavior.
6152
6153 It would be nicer to record the parent's ranges before
6154 traversing its children, simply overriding whatever you find
6155 there. But since we don't even decide whether to create a
6156 block until after we've traversed its children, that's hard
6157 to do. */
6158 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6159 }
6160 local_symbols = new->locals;
27aa8d6a 6161 using_directives = new->using_directives;
c906108c
SS
6162}
6163
96408a79
SA
6164/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6165
6166static void
6167read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6168{
6169 struct objfile *objfile = cu->objfile;
6170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6171 CORE_ADDR pc, baseaddr;
6172 struct attribute *attr;
6173 struct call_site *call_site, call_site_local;
6174 void **slot;
6175 int nparams;
6176 struct die_info *child_die;
6177
6178 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6179
6180 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6181 if (!attr)
6182 {
6183 complaint (&symfile_complaints,
6184 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6185 "DIE 0x%x [in module %s]"),
bb5ed363 6186 die->offset, objfile->name);
96408a79
SA
6187 return;
6188 }
6189 pc = DW_ADDR (attr) + baseaddr;
6190
6191 if (cu->call_site_htab == NULL)
6192 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6193 NULL, &objfile->objfile_obstack,
6194 hashtab_obstack_allocate, NULL);
6195 call_site_local.pc = pc;
6196 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6197 if (*slot != NULL)
6198 {
6199 complaint (&symfile_complaints,
6200 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6201 "DIE 0x%x [in module %s]"),
bb5ed363 6202 paddress (gdbarch, pc), die->offset, objfile->name);
96408a79
SA
6203 return;
6204 }
6205
6206 /* Count parameters at the caller. */
6207
6208 nparams = 0;
6209 for (child_die = die->child; child_die && child_die->tag;
6210 child_die = sibling_die (child_die))
6211 {
6212 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6213 {
6214 complaint (&symfile_complaints,
6215 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6216 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6217 child_die->tag, child_die->offset, objfile->name);
96408a79
SA
6218 continue;
6219 }
6220
6221 nparams++;
6222 }
6223
6224 call_site = obstack_alloc (&objfile->objfile_obstack,
6225 (sizeof (*call_site)
6226 + (sizeof (*call_site->parameter)
6227 * (nparams - 1))));
6228 *slot = call_site;
6229 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6230 call_site->pc = pc;
6231
6232 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6233 {
6234 struct die_info *func_die;
6235
6236 /* Skip also over DW_TAG_inlined_subroutine. */
6237 for (func_die = die->parent;
6238 func_die && func_die->tag != DW_TAG_subprogram
6239 && func_die->tag != DW_TAG_subroutine_type;
6240 func_die = func_die->parent);
6241
6242 /* DW_AT_GNU_all_call_sites is a superset
6243 of DW_AT_GNU_all_tail_call_sites. */
6244 if (func_die
6245 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6246 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6247 {
6248 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6249 not complete. But keep CALL_SITE for look ups via call_site_htab,
6250 both the initial caller containing the real return address PC and
6251 the final callee containing the current PC of a chain of tail
6252 calls do not need to have the tail call list complete. But any
6253 function candidate for a virtual tail call frame searched via
6254 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6255 determined unambiguously. */
6256 }
6257 else
6258 {
6259 struct type *func_type = NULL;
6260
6261 if (func_die)
6262 func_type = get_die_type (func_die, cu);
6263 if (func_type != NULL)
6264 {
6265 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6266
6267 /* Enlist this call site to the function. */
6268 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6269 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6270 }
6271 else
6272 complaint (&symfile_complaints,
6273 _("Cannot find function owning DW_TAG_GNU_call_site "
6274 "DIE 0x%x [in module %s]"),
bb5ed363 6275 die->offset, objfile->name);
96408a79
SA
6276 }
6277 }
6278
6279 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6280 if (attr == NULL)
6281 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6282 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6283 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6284 /* Keep NULL DWARF_BLOCK. */;
6285 else if (attr_form_is_block (attr))
6286 {
6287 struct dwarf2_locexpr_baton *dlbaton;
6288
6289 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6290 dlbaton->data = DW_BLOCK (attr)->data;
6291 dlbaton->size = DW_BLOCK (attr)->size;
6292 dlbaton->per_cu = cu->per_cu;
6293
6294 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6295 }
6296 else if (is_ref_attr (attr))
6297 {
96408a79
SA
6298 struct dwarf2_cu *target_cu = cu;
6299 struct die_info *target_die;
6300
6301 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6302 gdb_assert (target_cu->objfile == objfile);
6303 if (die_is_declaration (target_die, target_cu))
6304 {
6305 const char *target_physname;
6306
6307 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6308 if (target_physname == NULL)
6309 complaint (&symfile_complaints,
6310 _("DW_AT_GNU_call_site_target target DIE has invalid "
6311 "physname, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6312 die->offset, objfile->name);
96408a79
SA
6313 else
6314 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6315 }
6316 else
6317 {
6318 CORE_ADDR lowpc;
6319
6320 /* DW_AT_entry_pc should be preferred. */
6321 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6322 complaint (&symfile_complaints,
6323 _("DW_AT_GNU_call_site_target target DIE has invalid "
6324 "low pc, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6325 die->offset, objfile->name);
96408a79
SA
6326 else
6327 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6328 }
6329 }
6330 else
6331 complaint (&symfile_complaints,
6332 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6333 "block nor reference, for DIE 0x%x [in module %s]"),
bb5ed363 6334 die->offset, objfile->name);
96408a79
SA
6335
6336 call_site->per_cu = cu->per_cu;
6337
6338 for (child_die = die->child;
6339 child_die && child_die->tag;
6340 child_die = sibling_die (child_die))
6341 {
6342 struct dwarf2_locexpr_baton *dlbaton;
6343 struct call_site_parameter *parameter;
6344
6345 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6346 {
6347 /* Already printed the complaint above. */
6348 continue;
6349 }
6350
6351 gdb_assert (call_site->parameter_count < nparams);
6352 parameter = &call_site->parameter[call_site->parameter_count];
6353
6354 /* DW_AT_location specifies the register number. Value of the data
6355 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6356
6357 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6358 if (!attr || !attr_form_is_block (attr))
6359 {
6360 complaint (&symfile_complaints,
6361 _("No DW_FORM_block* DW_AT_location for "
6362 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6363 child_die->offset, objfile->name);
96408a79
SA
6364 continue;
6365 }
6366 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6367 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6368 if (parameter->dwarf_reg == -1
6369 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6370 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6371 &parameter->fb_offset))
6372 {
6373 complaint (&symfile_complaints,
6374 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6375 "for DW_FORM_block* DW_AT_location for "
6376 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6377 child_die->offset, objfile->name);
96408a79
SA
6378 continue;
6379 }
6380
6381 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6382 if (!attr_form_is_block (attr))
6383 {
6384 complaint (&symfile_complaints,
6385 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6386 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6387 child_die->offset, objfile->name);
96408a79
SA
6388 continue;
6389 }
6390 parameter->value = DW_BLOCK (attr)->data;
6391 parameter->value_size = DW_BLOCK (attr)->size;
6392
6393 /* Parameters are not pre-cleared by memset above. */
6394 parameter->data_value = NULL;
6395 parameter->data_value_size = 0;
6396 call_site->parameter_count++;
6397
6398 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6399 if (attr)
6400 {
6401 if (!attr_form_is_block (attr))
6402 complaint (&symfile_complaints,
6403 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6404 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6405 child_die->offset, objfile->name);
96408a79
SA
6406 else
6407 {
6408 parameter->data_value = DW_BLOCK (attr)->data;
6409 parameter->data_value_size = DW_BLOCK (attr)->size;
6410 }
6411 }
6412 }
6413}
6414
43039443 6415/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6416 Return 1 if the attributes are present and valid, otherwise, return 0.
6417 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6418
6419static int
6420dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6421 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6422 struct partial_symtab *ranges_pst)
43039443
JK
6423{
6424 struct objfile *objfile = cu->objfile;
6425 struct comp_unit_head *cu_header = &cu->header;
6426 bfd *obfd = objfile->obfd;
6427 unsigned int addr_size = cu_header->addr_size;
6428 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6429 /* Base address selection entry. */
6430 CORE_ADDR base;
6431 int found_base;
6432 unsigned int dummy;
6433 gdb_byte *buffer;
6434 CORE_ADDR marker;
6435 int low_set;
6436 CORE_ADDR low = 0;
6437 CORE_ADDR high = 0;
ff013f42 6438 CORE_ADDR baseaddr;
43039443 6439
d00adf39
DE
6440 found_base = cu->base_known;
6441 base = cu->base_address;
43039443 6442
be391dca 6443 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6444 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6445 {
6446 complaint (&symfile_complaints,
6447 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6448 offset);
6449 return 0;
6450 }
dce234bc 6451 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6452
6453 /* Read in the largest possible address. */
6454 marker = read_address (obfd, buffer, cu, &dummy);
6455 if ((marker & mask) == mask)
6456 {
6457 /* If we found the largest possible address, then
6458 read the base address. */
6459 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6460 buffer += 2 * addr_size;
6461 offset += 2 * addr_size;
6462 found_base = 1;
6463 }
6464
6465 low_set = 0;
6466
e7030f15 6467 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6468
43039443
JK
6469 while (1)
6470 {
6471 CORE_ADDR range_beginning, range_end;
6472
6473 range_beginning = read_address (obfd, buffer, cu, &dummy);
6474 buffer += addr_size;
6475 range_end = read_address (obfd, buffer, cu, &dummy);
6476 buffer += addr_size;
6477 offset += 2 * addr_size;
6478
6479 /* An end of list marker is a pair of zero addresses. */
6480 if (range_beginning == 0 && range_end == 0)
6481 /* Found the end of list entry. */
6482 break;
6483
6484 /* Each base address selection entry is a pair of 2 values.
6485 The first is the largest possible address, the second is
6486 the base address. Check for a base address here. */
6487 if ((range_beginning & mask) == mask)
6488 {
6489 /* If we found the largest possible address, then
6490 read the base address. */
6491 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6492 found_base = 1;
6493 continue;
6494 }
6495
6496 if (!found_base)
6497 {
6498 /* We have no valid base address for the ranges
6499 data. */
6500 complaint (&symfile_complaints,
6501 _("Invalid .debug_ranges data (no base address)"));
6502 return 0;
6503 }
6504
9277c30c
UW
6505 if (range_beginning > range_end)
6506 {
6507 /* Inverted range entries are invalid. */
6508 complaint (&symfile_complaints,
6509 _("Invalid .debug_ranges data (inverted range)"));
6510 return 0;
6511 }
6512
6513 /* Empty range entries have no effect. */
6514 if (range_beginning == range_end)
6515 continue;
6516
43039443
JK
6517 range_beginning += base;
6518 range_end += base;
6519
9277c30c 6520 if (ranges_pst != NULL)
ff013f42 6521 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6522 range_beginning + baseaddr,
6523 range_end - 1 + baseaddr,
ff013f42
JK
6524 ranges_pst);
6525
43039443
JK
6526 /* FIXME: This is recording everything as a low-high
6527 segment of consecutive addresses. We should have a
6528 data structure for discontiguous block ranges
6529 instead. */
6530 if (! low_set)
6531 {
6532 low = range_beginning;
6533 high = range_end;
6534 low_set = 1;
6535 }
6536 else
6537 {
6538 if (range_beginning < low)
6539 low = range_beginning;
6540 if (range_end > high)
6541 high = range_end;
6542 }
6543 }
6544
6545 if (! low_set)
6546 /* If the first entry is an end-of-list marker, the range
6547 describes an empty scope, i.e. no instructions. */
6548 return 0;
6549
6550 if (low_return)
6551 *low_return = low;
6552 if (high_return)
6553 *high_return = high;
6554 return 1;
6555}
6556
af34e669
DJ
6557/* Get low and high pc attributes from a die. Return 1 if the attributes
6558 are present and valid, otherwise, return 0. Return -1 if the range is
6559 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6560static int
af34e669 6561dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6562 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6563 struct partial_symtab *pst)
c906108c
SS
6564{
6565 struct attribute *attr;
af34e669
DJ
6566 CORE_ADDR low = 0;
6567 CORE_ADDR high = 0;
6568 int ret = 0;
c906108c 6569
e142c38c 6570 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6571 if (attr)
af34e669
DJ
6572 {
6573 high = DW_ADDR (attr);
e142c38c 6574 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6575 if (attr)
6576 low = DW_ADDR (attr);
6577 else
6578 /* Found high w/o low attribute. */
6579 return 0;
6580
6581 /* Found consecutive range of addresses. */
6582 ret = 1;
6583 }
c906108c 6584 else
af34e669 6585 {
e142c38c 6586 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6587 if (attr != NULL)
6588 {
af34e669 6589 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6590 .debug_ranges section. */
d85a05f0 6591 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6592 return 0;
43039443 6593 /* Found discontinuous range of addresses. */
af34e669
DJ
6594 ret = -1;
6595 }
6596 }
c906108c 6597
9373cf26
JK
6598 /* read_partial_die has also the strict LOW < HIGH requirement. */
6599 if (high <= low)
c906108c
SS
6600 return 0;
6601
6602 /* When using the GNU linker, .gnu.linkonce. sections are used to
6603 eliminate duplicate copies of functions and vtables and such.
6604 The linker will arbitrarily choose one and discard the others.
6605 The AT_*_pc values for such functions refer to local labels in
6606 these sections. If the section from that file was discarded, the
6607 labels are not in the output, so the relocs get a value of 0.
6608 If this is a discarded function, mark the pc bounds as invalid,
6609 so that GDB will ignore it. */
72dca2f5 6610 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6611 return 0;
6612
6613 *lowpc = low;
96408a79
SA
6614 if (highpc)
6615 *highpc = high;
af34e669 6616 return ret;
c906108c
SS
6617}
6618
b084d499
JB
6619/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6620 its low and high PC addresses. Do nothing if these addresses could not
6621 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6622 and HIGHPC to the high address if greater than HIGHPC. */
6623
6624static void
6625dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6626 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6627 struct dwarf2_cu *cu)
6628{
6629 CORE_ADDR low, high;
6630 struct die_info *child = die->child;
6631
d85a05f0 6632 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6633 {
6634 *lowpc = min (*lowpc, low);
6635 *highpc = max (*highpc, high);
6636 }
6637
6638 /* If the language does not allow nested subprograms (either inside
6639 subprograms or lexical blocks), we're done. */
6640 if (cu->language != language_ada)
6641 return;
6e70227d 6642
b084d499
JB
6643 /* Check all the children of the given DIE. If it contains nested
6644 subprograms, then check their pc bounds. Likewise, we need to
6645 check lexical blocks as well, as they may also contain subprogram
6646 definitions. */
6647 while (child && child->tag)
6648 {
6649 if (child->tag == DW_TAG_subprogram
6650 || child->tag == DW_TAG_lexical_block)
6651 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6652 child = sibling_die (child);
6653 }
6654}
6655
fae299cd
DC
6656/* Get the low and high pc's represented by the scope DIE, and store
6657 them in *LOWPC and *HIGHPC. If the correct values can't be
6658 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6659
6660static void
6661get_scope_pc_bounds (struct die_info *die,
6662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6663 struct dwarf2_cu *cu)
6664{
6665 CORE_ADDR best_low = (CORE_ADDR) -1;
6666 CORE_ADDR best_high = (CORE_ADDR) 0;
6667 CORE_ADDR current_low, current_high;
6668
d85a05f0 6669 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6670 {
6671 best_low = current_low;
6672 best_high = current_high;
6673 }
6674 else
6675 {
6676 struct die_info *child = die->child;
6677
6678 while (child && child->tag)
6679 {
6680 switch (child->tag) {
6681 case DW_TAG_subprogram:
b084d499 6682 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6683 break;
6684 case DW_TAG_namespace:
f55ee35c 6685 case DW_TAG_module:
fae299cd
DC
6686 /* FIXME: carlton/2004-01-16: Should we do this for
6687 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6688 that current GCC's always emit the DIEs corresponding
6689 to definitions of methods of classes as children of a
6690 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6691 the DIEs giving the declarations, which could be
6692 anywhere). But I don't see any reason why the
6693 standards says that they have to be there. */
6694 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6695
6696 if (current_low != ((CORE_ADDR) -1))
6697 {
6698 best_low = min (best_low, current_low);
6699 best_high = max (best_high, current_high);
6700 }
6701 break;
6702 default:
0963b4bd 6703 /* Ignore. */
fae299cd
DC
6704 break;
6705 }
6706
6707 child = sibling_die (child);
6708 }
6709 }
6710
6711 *lowpc = best_low;
6712 *highpc = best_high;
6713}
6714
801e3a5b
JB
6715/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6716 in DIE. */
6717static void
6718dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6719 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6720{
bb5ed363 6721 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6722 struct attribute *attr;
6723
6724 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6725 if (attr)
6726 {
6727 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6728
801e3a5b
JB
6729 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6730 if (attr)
6731 {
6732 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6733
801e3a5b
JB
6734 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6735 }
6736 }
6737
6738 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6739 if (attr)
6740 {
bb5ed363 6741 bfd *obfd = objfile->obfd;
801e3a5b
JB
6742
6743 /* The value of the DW_AT_ranges attribute is the offset of the
6744 address range list in the .debug_ranges section. */
6745 unsigned long offset = DW_UNSND (attr);
dce234bc 6746 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6747
6748 /* For some target architectures, but not others, the
6749 read_address function sign-extends the addresses it returns.
6750 To recognize base address selection entries, we need a
6751 mask. */
6752 unsigned int addr_size = cu->header.addr_size;
6753 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6754
6755 /* The base address, to which the next pair is relative. Note
6756 that this 'base' is a DWARF concept: most entries in a range
6757 list are relative, to reduce the number of relocs against the
6758 debugging information. This is separate from this function's
6759 'baseaddr' argument, which GDB uses to relocate debugging
6760 information from a shared library based on the address at
6761 which the library was loaded. */
d00adf39
DE
6762 CORE_ADDR base = cu->base_address;
6763 int base_known = cu->base_known;
801e3a5b 6764
be391dca 6765 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6766 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6767 {
6768 complaint (&symfile_complaints,
6769 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6770 offset);
6771 return;
6772 }
6773
6774 for (;;)
6775 {
6776 unsigned int bytes_read;
6777 CORE_ADDR start, end;
6778
6779 start = read_address (obfd, buffer, cu, &bytes_read);
6780 buffer += bytes_read;
6781 end = read_address (obfd, buffer, cu, &bytes_read);
6782 buffer += bytes_read;
6783
6784 /* Did we find the end of the range list? */
6785 if (start == 0 && end == 0)
6786 break;
6787
6788 /* Did we find a base address selection entry? */
6789 else if ((start & base_select_mask) == base_select_mask)
6790 {
6791 base = end;
6792 base_known = 1;
6793 }
6794
6795 /* We found an ordinary address range. */
6796 else
6797 {
6798 if (!base_known)
6799 {
6800 complaint (&symfile_complaints,
3e43a32a
MS
6801 _("Invalid .debug_ranges data "
6802 "(no base address)"));
801e3a5b
JB
6803 return;
6804 }
6805
9277c30c
UW
6806 if (start > end)
6807 {
6808 /* Inverted range entries are invalid. */
6809 complaint (&symfile_complaints,
6810 _("Invalid .debug_ranges data "
6811 "(inverted range)"));
6812 return;
6813 }
6814
6815 /* Empty range entries have no effect. */
6816 if (start == end)
6817 continue;
6818
6e70227d
DE
6819 record_block_range (block,
6820 baseaddr + base + start,
801e3a5b
JB
6821 baseaddr + base + end - 1);
6822 }
6823 }
6824 }
6825}
6826
60d5a603
JK
6827/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6828 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6829 during 4.6.0 experimental. */
6830
6831static int
6832producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6833{
6834 const char *cs;
6835 int major, minor, release;
ba919b58 6836 int result = 0;
60d5a603
JK
6837
6838 if (cu->producer == NULL)
6839 {
6840 /* For unknown compilers expect their behavior is DWARF version
6841 compliant.
6842
6843 GCC started to support .debug_types sections by -gdwarf-4 since
6844 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6845 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6846 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6847 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6848
6849 return 0;
6850 }
6851
ba919b58
TT
6852 if (cu->checked_producer)
6853 return cu->producer_is_gxx_lt_4_6;
6854
60d5a603
JK
6855 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6856
6857 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6858 {
6859 /* For non-GCC compilers expect their behavior is DWARF version
6860 compliant. */
60d5a603 6861 }
ba919b58 6862 else
60d5a603 6863 {
ba919b58
TT
6864 cs = &cu->producer[strlen ("GNU ")];
6865 while (*cs && !isdigit (*cs))
6866 cs++;
6867 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6868 {
6869 /* Not recognized as GCC. */
6870 }
6871 else
6872 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
6873 }
6874
ba919b58
TT
6875 cu->checked_producer = 1;
6876 cu->producer_is_gxx_lt_4_6 = result;
6877
6878 return result;
60d5a603
JK
6879}
6880
6881/* Return the default accessibility type if it is not overriden by
6882 DW_AT_accessibility. */
6883
6884static enum dwarf_access_attribute
6885dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6886{
6887 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6888 {
6889 /* The default DWARF 2 accessibility for members is public, the default
6890 accessibility for inheritance is private. */
6891
6892 if (die->tag != DW_TAG_inheritance)
6893 return DW_ACCESS_public;
6894 else
6895 return DW_ACCESS_private;
6896 }
6897 else
6898 {
6899 /* DWARF 3+ defines the default accessibility a different way. The same
6900 rules apply now for DW_TAG_inheritance as for the members and it only
6901 depends on the container kind. */
6902
6903 if (die->parent->tag == DW_TAG_class_type)
6904 return DW_ACCESS_private;
6905 else
6906 return DW_ACCESS_public;
6907 }
6908}
6909
74ac6d43
TT
6910/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6911 offset. If the attribute was not found return 0, otherwise return
6912 1. If it was found but could not properly be handled, set *OFFSET
6913 to 0. */
6914
6915static int
6916handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6917 LONGEST *offset)
6918{
6919 struct attribute *attr;
6920
6921 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6922 if (attr != NULL)
6923 {
6924 *offset = 0;
6925
6926 /* Note that we do not check for a section offset first here.
6927 This is because DW_AT_data_member_location is new in DWARF 4,
6928 so if we see it, we can assume that a constant form is really
6929 a constant and not a section offset. */
6930 if (attr_form_is_constant (attr))
6931 *offset = dwarf2_get_attr_constant_value (attr, 0);
6932 else if (attr_form_is_section_offset (attr))
6933 dwarf2_complex_location_expr_complaint ();
6934 else if (attr_form_is_block (attr))
6935 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6936 else
6937 dwarf2_complex_location_expr_complaint ();
6938
6939 return 1;
6940 }
6941
6942 return 0;
6943}
6944
c906108c
SS
6945/* Add an aggregate field to the field list. */
6946
6947static void
107d2387 6948dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6949 struct dwarf2_cu *cu)
6e70227d 6950{
e7c27a73 6951 struct objfile *objfile = cu->objfile;
5e2b427d 6952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6953 struct nextfield *new_field;
6954 struct attribute *attr;
6955 struct field *fp;
6956 char *fieldname = "";
6957
6958 /* Allocate a new field list entry and link it in. */
6959 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6960 make_cleanup (xfree, new_field);
c906108c 6961 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6962
6963 if (die->tag == DW_TAG_inheritance)
6964 {
6965 new_field->next = fip->baseclasses;
6966 fip->baseclasses = new_field;
6967 }
6968 else
6969 {
6970 new_field->next = fip->fields;
6971 fip->fields = new_field;
6972 }
c906108c
SS
6973 fip->nfields++;
6974
e142c38c 6975 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6976 if (attr)
6977 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6978 else
6979 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6980 if (new_field->accessibility != DW_ACCESS_public)
6981 fip->non_public_fields = 1;
60d5a603 6982
e142c38c 6983 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6984 if (attr)
6985 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6986 else
6987 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6988
6989 fp = &new_field->field;
a9a9bd0f 6990
e142c38c 6991 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6992 {
74ac6d43
TT
6993 LONGEST offset;
6994
a9a9bd0f 6995 /* Data member other than a C++ static data member. */
6e70227d 6996
c906108c 6997 /* Get type of field. */
e7c27a73 6998 fp->type = die_type (die, cu);
c906108c 6999
d6a843b5 7000 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7001
c906108c 7002 /* Get bit size of field (zero if none). */
e142c38c 7003 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7004 if (attr)
7005 {
7006 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7007 }
7008 else
7009 {
7010 FIELD_BITSIZE (*fp) = 0;
7011 }
7012
7013 /* Get bit offset of field. */
74ac6d43
TT
7014 if (handle_data_member_location (die, cu, &offset))
7015 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7016 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7017 if (attr)
7018 {
5e2b427d 7019 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7020 {
7021 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7022 additional bit offset from the MSB of the containing
7023 anonymous object to the MSB of the field. We don't
7024 have to do anything special since we don't need to
7025 know the size of the anonymous object. */
c906108c
SS
7026 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7027 }
7028 else
7029 {
7030 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7031 MSB of the anonymous object, subtract off the number of
7032 bits from the MSB of the field to the MSB of the
7033 object, and then subtract off the number of bits of
7034 the field itself. The result is the bit offset of
7035 the LSB of the field. */
c906108c
SS
7036 int anonymous_size;
7037 int bit_offset = DW_UNSND (attr);
7038
e142c38c 7039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7040 if (attr)
7041 {
7042 /* The size of the anonymous object containing
7043 the bit field is explicit, so use the
7044 indicated size (in bytes). */
7045 anonymous_size = DW_UNSND (attr);
7046 }
7047 else
7048 {
7049 /* The size of the anonymous object containing
7050 the bit field must be inferred from the type
7051 attribute of the data member containing the
7052 bit field. */
7053 anonymous_size = TYPE_LENGTH (fp->type);
7054 }
7055 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7056 - bit_offset - FIELD_BITSIZE (*fp);
7057 }
7058 }
7059
7060 /* Get name of field. */
39cbfefa
DJ
7061 fieldname = dwarf2_name (die, cu);
7062 if (fieldname == NULL)
7063 fieldname = "";
d8151005
DJ
7064
7065 /* The name is already allocated along with this objfile, so we don't
7066 need to duplicate it for the type. */
7067 fp->name = fieldname;
c906108c
SS
7068
7069 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7070 pointer or virtual base class pointer) to private. */
e142c38c 7071 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7072 {
d48cc9dd 7073 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7074 new_field->accessibility = DW_ACCESS_private;
7075 fip->non_public_fields = 1;
7076 }
7077 }
a9a9bd0f 7078 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7079 {
a9a9bd0f
DC
7080 /* C++ static member. */
7081
7082 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7083 is a declaration, but all versions of G++ as of this writing
7084 (so through at least 3.2.1) incorrectly generate
7085 DW_TAG_variable tags. */
6e70227d 7086
ff355380 7087 const char *physname;
c906108c 7088
a9a9bd0f 7089 /* Get name of field. */
39cbfefa
DJ
7090 fieldname = dwarf2_name (die, cu);
7091 if (fieldname == NULL)
c906108c
SS
7092 return;
7093
254e6b9e 7094 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7095 if (attr
7096 /* Only create a symbol if this is an external value.
7097 new_symbol checks this and puts the value in the global symbol
7098 table, which we want. If it is not external, new_symbol
7099 will try to put the value in cu->list_in_scope which is wrong. */
7100 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7101 {
7102 /* A static const member, not much different than an enum as far as
7103 we're concerned, except that we can support more types. */
7104 new_symbol (die, NULL, cu);
7105 }
7106
2df3850c 7107 /* Get physical name. */
ff355380 7108 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7109
d8151005
DJ
7110 /* The name is already allocated along with this objfile, so we don't
7111 need to duplicate it for the type. */
7112 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7113 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7114 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7115 }
7116 else if (die->tag == DW_TAG_inheritance)
7117 {
74ac6d43 7118 LONGEST offset;
d4b96c9a 7119
74ac6d43
TT
7120 /* C++ base class field. */
7121 if (handle_data_member_location (die, cu, &offset))
7122 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7123 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7124 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7125 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7126 fip->nbaseclasses++;
7127 }
7128}
7129
98751a41
JK
7130/* Add a typedef defined in the scope of the FIP's class. */
7131
7132static void
7133dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7134 struct dwarf2_cu *cu)
6e70227d 7135{
98751a41 7136 struct objfile *objfile = cu->objfile;
98751a41
JK
7137 struct typedef_field_list *new_field;
7138 struct attribute *attr;
7139 struct typedef_field *fp;
7140 char *fieldname = "";
7141
7142 /* Allocate a new field list entry and link it in. */
7143 new_field = xzalloc (sizeof (*new_field));
7144 make_cleanup (xfree, new_field);
7145
7146 gdb_assert (die->tag == DW_TAG_typedef);
7147
7148 fp = &new_field->field;
7149
7150 /* Get name of field. */
7151 fp->name = dwarf2_name (die, cu);
7152 if (fp->name == NULL)
7153 return;
7154
7155 fp->type = read_type_die (die, cu);
7156
7157 new_field->next = fip->typedef_field_list;
7158 fip->typedef_field_list = new_field;
7159 fip->typedef_field_list_count++;
7160}
7161
c906108c
SS
7162/* Create the vector of fields, and attach it to the type. */
7163
7164static void
fba45db2 7165dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7166 struct dwarf2_cu *cu)
c906108c
SS
7167{
7168 int nfields = fip->nfields;
7169
7170 /* Record the field count, allocate space for the array of fields,
7171 and create blank accessibility bitfields if necessary. */
7172 TYPE_NFIELDS (type) = nfields;
7173 TYPE_FIELDS (type) = (struct field *)
7174 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7175 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7176
b4ba55a1 7177 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7178 {
7179 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7180
7181 TYPE_FIELD_PRIVATE_BITS (type) =
7182 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7183 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7184
7185 TYPE_FIELD_PROTECTED_BITS (type) =
7186 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7187 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7188
774b6a14
TT
7189 TYPE_FIELD_IGNORE_BITS (type) =
7190 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7191 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7192 }
7193
7194 /* If the type has baseclasses, allocate and clear a bit vector for
7195 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7196 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7197 {
7198 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7199 unsigned char *pointer;
c906108c
SS
7200
7201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7202 pointer = TYPE_ALLOC (type, num_bytes);
7203 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7204 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7205 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7206 }
7207
3e43a32a
MS
7208 /* Copy the saved-up fields into the field vector. Start from the head of
7209 the list, adding to the tail of the field array, so that they end up in
7210 the same order in the array in which they were added to the list. */
c906108c
SS
7211 while (nfields-- > 0)
7212 {
7d0ccb61
DJ
7213 struct nextfield *fieldp;
7214
7215 if (fip->fields)
7216 {
7217 fieldp = fip->fields;
7218 fip->fields = fieldp->next;
7219 }
7220 else
7221 {
7222 fieldp = fip->baseclasses;
7223 fip->baseclasses = fieldp->next;
7224 }
7225
7226 TYPE_FIELD (type, nfields) = fieldp->field;
7227 switch (fieldp->accessibility)
c906108c 7228 {
c5aa993b 7229 case DW_ACCESS_private:
b4ba55a1
JB
7230 if (cu->language != language_ada)
7231 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7232 break;
c906108c 7233
c5aa993b 7234 case DW_ACCESS_protected:
b4ba55a1
JB
7235 if (cu->language != language_ada)
7236 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7237 break;
c906108c 7238
c5aa993b
JM
7239 case DW_ACCESS_public:
7240 break;
c906108c 7241
c5aa993b
JM
7242 default:
7243 /* Unknown accessibility. Complain and treat it as public. */
7244 {
e2e0b3e5 7245 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7246 fieldp->accessibility);
c5aa993b
JM
7247 }
7248 break;
c906108c
SS
7249 }
7250 if (nfields < fip->nbaseclasses)
7251 {
7d0ccb61 7252 switch (fieldp->virtuality)
c906108c 7253 {
c5aa993b
JM
7254 case DW_VIRTUALITY_virtual:
7255 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7256 if (cu->language == language_ada)
a73c6dcd 7257 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7258 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7259 break;
c906108c
SS
7260 }
7261 }
c906108c
SS
7262 }
7263}
7264
c906108c
SS
7265/* Add a member function to the proper fieldlist. */
7266
7267static void
107d2387 7268dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7269 struct type *type, struct dwarf2_cu *cu)
c906108c 7270{
e7c27a73 7271 struct objfile *objfile = cu->objfile;
c906108c
SS
7272 struct attribute *attr;
7273 struct fnfieldlist *flp;
7274 int i;
7275 struct fn_field *fnp;
7276 char *fieldname;
c906108c 7277 struct nextfnfield *new_fnfield;
f792889a 7278 struct type *this_type;
60d5a603 7279 enum dwarf_access_attribute accessibility;
c906108c 7280
b4ba55a1 7281 if (cu->language == language_ada)
a73c6dcd 7282 error (_("unexpected member function in Ada type"));
b4ba55a1 7283
2df3850c 7284 /* Get name of member function. */
39cbfefa
DJ
7285 fieldname = dwarf2_name (die, cu);
7286 if (fieldname == NULL)
2df3850c 7287 return;
c906108c 7288
c906108c
SS
7289 /* Look up member function name in fieldlist. */
7290 for (i = 0; i < fip->nfnfields; i++)
7291 {
27bfe10e 7292 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7293 break;
7294 }
7295
7296 /* Create new list element if necessary. */
7297 if (i < fip->nfnfields)
7298 flp = &fip->fnfieldlists[i];
7299 else
7300 {
7301 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7302 {
7303 fip->fnfieldlists = (struct fnfieldlist *)
7304 xrealloc (fip->fnfieldlists,
7305 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7306 * sizeof (struct fnfieldlist));
c906108c 7307 if (fip->nfnfields == 0)
c13c43fd 7308 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7309 }
7310 flp = &fip->fnfieldlists[fip->nfnfields];
7311 flp->name = fieldname;
7312 flp->length = 0;
7313 flp->head = NULL;
3da10d80 7314 i = fip->nfnfields++;
c906108c
SS
7315 }
7316
7317 /* Create a new member function field and chain it to the field list
0963b4bd 7318 entry. */
c906108c 7319 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7320 make_cleanup (xfree, new_fnfield);
c906108c
SS
7321 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7322 new_fnfield->next = flp->head;
7323 flp->head = new_fnfield;
7324 flp->length++;
7325
7326 /* Fill in the member function field info. */
7327 fnp = &new_fnfield->fnfield;
3da10d80
KS
7328
7329 /* Delay processing of the physname until later. */
7330 if (cu->language == language_cplus || cu->language == language_java)
7331 {
7332 add_to_method_list (type, i, flp->length - 1, fieldname,
7333 die, cu);
7334 }
7335 else
7336 {
1d06ead6 7337 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7338 fnp->physname = physname ? physname : "";
7339 }
7340
c906108c 7341 fnp->type = alloc_type (objfile);
f792889a
DJ
7342 this_type = read_type_die (die, cu);
7343 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7344 {
f792889a 7345 int nparams = TYPE_NFIELDS (this_type);
c906108c 7346
f792889a 7347 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7348 of the method itself (TYPE_CODE_METHOD). */
7349 smash_to_method_type (fnp->type, type,
f792889a
DJ
7350 TYPE_TARGET_TYPE (this_type),
7351 TYPE_FIELDS (this_type),
7352 TYPE_NFIELDS (this_type),
7353 TYPE_VARARGS (this_type));
c906108c
SS
7354
7355 /* Handle static member functions.
c5aa993b 7356 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7357 member functions. G++ helps GDB by marking the first
7358 parameter for non-static member functions (which is the this
7359 pointer) as artificial. We obtain this information from
7360 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7361 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7362 fnp->voffset = VOFFSET_STATIC;
7363 }
7364 else
e2e0b3e5 7365 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7366 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7367
7368 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7369 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7370 fnp->fcontext = die_containing_type (die, cu);
c906108c 7371
3e43a32a
MS
7372 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7373 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7374
7375 /* Get accessibility. */
e142c38c 7376 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7377 if (attr)
60d5a603
JK
7378 accessibility = DW_UNSND (attr);
7379 else
7380 accessibility = dwarf2_default_access_attribute (die, cu);
7381 switch (accessibility)
c906108c 7382 {
60d5a603
JK
7383 case DW_ACCESS_private:
7384 fnp->is_private = 1;
7385 break;
7386 case DW_ACCESS_protected:
7387 fnp->is_protected = 1;
7388 break;
c906108c
SS
7389 }
7390
b02dede2 7391 /* Check for artificial methods. */
e142c38c 7392 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7393 if (attr && DW_UNSND (attr) != 0)
7394 fnp->is_artificial = 1;
7395
0d564a31 7396 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7397 function. For older versions of GCC, this is an offset in the
7398 appropriate virtual table, as specified by DW_AT_containing_type.
7399 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7400 to the object address. */
7401
e142c38c 7402 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7403 if (attr)
8e19ed76 7404 {
aec5aa8b 7405 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7406 {
aec5aa8b
TT
7407 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7408 {
7409 /* Old-style GCC. */
7410 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7411 }
7412 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7413 || (DW_BLOCK (attr)->size > 1
7414 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7415 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7416 {
7417 struct dwarf_block blk;
7418 int offset;
7419
7420 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7421 ? 1 : 2);
7422 blk.size = DW_BLOCK (attr)->size - offset;
7423 blk.data = DW_BLOCK (attr)->data + offset;
7424 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7425 if ((fnp->voffset % cu->header.addr_size) != 0)
7426 dwarf2_complex_location_expr_complaint ();
7427 else
7428 fnp->voffset /= cu->header.addr_size;
7429 fnp->voffset += 2;
7430 }
7431 else
7432 dwarf2_complex_location_expr_complaint ();
7433
7434 if (!fnp->fcontext)
7435 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7436 }
3690dd37 7437 else if (attr_form_is_section_offset (attr))
8e19ed76 7438 {
4d3c2250 7439 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7440 }
7441 else
7442 {
4d3c2250
KB
7443 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7444 fieldname);
8e19ed76 7445 }
0d564a31 7446 }
d48cc9dd
DJ
7447 else
7448 {
7449 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7450 if (attr && DW_UNSND (attr))
7451 {
7452 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7453 complaint (&symfile_complaints,
3e43a32a
MS
7454 _("Member function \"%s\" (offset %d) is virtual "
7455 "but the vtable offset is not specified"),
d48cc9dd 7456 fieldname, die->offset);
9655fd1a 7457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7458 TYPE_CPLUS_DYNAMIC (type) = 1;
7459 }
7460 }
c906108c
SS
7461}
7462
7463/* Create the vector of member function fields, and attach it to the type. */
7464
7465static void
fba45db2 7466dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7467 struct dwarf2_cu *cu)
c906108c
SS
7468{
7469 struct fnfieldlist *flp;
c906108c
SS
7470 int i;
7471
b4ba55a1 7472 if (cu->language == language_ada)
a73c6dcd 7473 error (_("unexpected member functions in Ada type"));
b4ba55a1 7474
c906108c
SS
7475 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7476 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7477 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7478
7479 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7480 {
7481 struct nextfnfield *nfp = flp->head;
7482 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7483 int k;
7484
7485 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7486 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7487 fn_flp->fn_fields = (struct fn_field *)
7488 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7489 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7490 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7491 }
7492
7493 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7494}
7495
1168df01
JB
7496/* Returns non-zero if NAME is the name of a vtable member in CU's
7497 language, zero otherwise. */
7498static int
7499is_vtable_name (const char *name, struct dwarf2_cu *cu)
7500{
7501 static const char vptr[] = "_vptr";
987504bb 7502 static const char vtable[] = "vtable";
1168df01 7503
987504bb
JJ
7504 /* Look for the C++ and Java forms of the vtable. */
7505 if ((cu->language == language_java
7506 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7507 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7508 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7509 return 1;
7510
7511 return 0;
7512}
7513
c0dd20ea 7514/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7515 functions, with the ABI-specified layout. If TYPE describes
7516 such a structure, smash it into a member function type.
61049d3b
DJ
7517
7518 GCC shouldn't do this; it should just output pointer to member DIEs.
7519 This is GCC PR debug/28767. */
c0dd20ea 7520
0b92b5bb
TT
7521static void
7522quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7523{
0b92b5bb 7524 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7525
7526 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7527 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7528 return;
c0dd20ea
DJ
7529
7530 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7531 if (TYPE_FIELD_NAME (type, 0) == NULL
7532 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7533 || TYPE_FIELD_NAME (type, 1) == NULL
7534 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7535 return;
c0dd20ea
DJ
7536
7537 /* Find the type of the method. */
0b92b5bb 7538 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7539 if (pfn_type == NULL
7540 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7541 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7542 return;
c0dd20ea
DJ
7543
7544 /* Look for the "this" argument. */
7545 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7546 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7547 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7548 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7549 return;
c0dd20ea
DJ
7550
7551 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7552 new_type = alloc_type (objfile);
7553 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7554 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7555 TYPE_VARARGS (pfn_type));
0b92b5bb 7556 smash_to_methodptr_type (type, new_type);
c0dd20ea 7557}
1168df01 7558
c906108c 7559/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7560 (definition) to create a type for the structure or union. Fill in
7561 the type's name and general properties; the members will not be
7562 processed until process_structure_type.
c906108c 7563
c767944b
DJ
7564 NOTE: we need to call these functions regardless of whether or not the
7565 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7566 structure or union. This gets the type entered into our set of
7567 user defined types.
7568
7569 However, if the structure is incomplete (an opaque struct/union)
7570 then suppress creating a symbol table entry for it since gdb only
7571 wants to find the one with the complete definition. Note that if
7572 it is complete, we just call new_symbol, which does it's own
7573 checking about whether the struct/union is anonymous or not (and
7574 suppresses creating a symbol table entry itself). */
7575
f792889a 7576static struct type *
134d01f1 7577read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7578{
e7c27a73 7579 struct objfile *objfile = cu->objfile;
c906108c
SS
7580 struct type *type;
7581 struct attribute *attr;
39cbfefa 7582 char *name;
c906108c 7583
348e048f
DE
7584 /* If the definition of this type lives in .debug_types, read that type.
7585 Don't follow DW_AT_specification though, that will take us back up
7586 the chain and we want to go down. */
7587 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7588 if (attr)
7589 {
7590 struct dwarf2_cu *type_cu = cu;
7591 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7592
348e048f
DE
7593 /* We could just recurse on read_structure_type, but we need to call
7594 get_die_type to ensure only one type for this DIE is created.
7595 This is important, for example, because for c++ classes we need
7596 TYPE_NAME set which is only done by new_symbol. Blech. */
7597 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7598
7599 /* TYPE_CU may not be the same as CU.
7600 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7601 return set_die_type (die, type, cu);
7602 }
7603
c0dd20ea 7604 type = alloc_type (objfile);
c906108c 7605 INIT_CPLUS_SPECIFIC (type);
93311388 7606
39cbfefa
DJ
7607 name = dwarf2_name (die, cu);
7608 if (name != NULL)
c906108c 7609 {
987504bb
JJ
7610 if (cu->language == language_cplus
7611 || cu->language == language_java)
63d06c5c 7612 {
3da10d80
KS
7613 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7614
7615 /* dwarf2_full_name might have already finished building the DIE's
7616 type. If so, there is no need to continue. */
7617 if (get_die_type (die, cu) != NULL)
7618 return get_die_type (die, cu);
7619
7620 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7621 if (die->tag == DW_TAG_structure_type
7622 || die->tag == DW_TAG_class_type)
7623 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7624 }
7625 else
7626 {
d8151005
DJ
7627 /* The name is already allocated along with this objfile, so
7628 we don't need to duplicate it for the type. */
94af9270
KS
7629 TYPE_TAG_NAME (type) = (char *) name;
7630 if (die->tag == DW_TAG_class_type)
7631 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7632 }
c906108c
SS
7633 }
7634
7635 if (die->tag == DW_TAG_structure_type)
7636 {
7637 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7638 }
7639 else if (die->tag == DW_TAG_union_type)
7640 {
7641 TYPE_CODE (type) = TYPE_CODE_UNION;
7642 }
7643 else
7644 {
c906108c
SS
7645 TYPE_CODE (type) = TYPE_CODE_CLASS;
7646 }
7647
0cc2414c
TT
7648 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7649 TYPE_DECLARED_CLASS (type) = 1;
7650
e142c38c 7651 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7652 if (attr)
7653 {
7654 TYPE_LENGTH (type) = DW_UNSND (attr);
7655 }
7656 else
7657 {
7658 TYPE_LENGTH (type) = 0;
7659 }
7660
876cecd0 7661 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7662 if (die_is_declaration (die, cu))
876cecd0 7663 TYPE_STUB (type) = 1;
a6c727b2
DJ
7664 else if (attr == NULL && die->child == NULL
7665 && producer_is_realview (cu->producer))
7666 /* RealView does not output the required DW_AT_declaration
7667 on incomplete types. */
7668 TYPE_STUB (type) = 1;
dc718098 7669
c906108c
SS
7670 /* We need to add the type field to the die immediately so we don't
7671 infinitely recurse when dealing with pointers to the structure
0963b4bd 7672 type within the structure itself. */
1c379e20 7673 set_die_type (die, type, cu);
c906108c 7674
7e314c57
JK
7675 /* set_die_type should be already done. */
7676 set_descriptive_type (type, die, cu);
7677
c767944b
DJ
7678 return type;
7679}
7680
7681/* Finish creating a structure or union type, including filling in
7682 its members and creating a symbol for it. */
7683
7684static void
7685process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7686{
7687 struct objfile *objfile = cu->objfile;
7688 struct die_info *child_die = die->child;
7689 struct type *type;
7690
7691 type = get_die_type (die, cu);
7692 if (type == NULL)
7693 type = read_structure_type (die, cu);
7694
e142c38c 7695 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7696 {
7697 struct field_info fi;
7698 struct die_info *child_die;
34eaf542 7699 VEC (symbolp) *template_args = NULL;
c767944b 7700 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7701
7702 memset (&fi, 0, sizeof (struct field_info));
7703
639d11d3 7704 child_die = die->child;
c906108c
SS
7705
7706 while (child_die && child_die->tag)
7707 {
a9a9bd0f
DC
7708 if (child_die->tag == DW_TAG_member
7709 || child_die->tag == DW_TAG_variable)
c906108c 7710 {
a9a9bd0f
DC
7711 /* NOTE: carlton/2002-11-05: A C++ static data member
7712 should be a DW_TAG_member that is a declaration, but
7713 all versions of G++ as of this writing (so through at
7714 least 3.2.1) incorrectly generate DW_TAG_variable
7715 tags for them instead. */
e7c27a73 7716 dwarf2_add_field (&fi, child_die, cu);
c906108c 7717 }
8713b1b1 7718 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7719 {
0963b4bd 7720 /* C++ member function. */
e7c27a73 7721 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7722 }
7723 else if (child_die->tag == DW_TAG_inheritance)
7724 {
7725 /* C++ base class field. */
e7c27a73 7726 dwarf2_add_field (&fi, child_die, cu);
c906108c 7727 }
98751a41
JK
7728 else if (child_die->tag == DW_TAG_typedef)
7729 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7730 else if (child_die->tag == DW_TAG_template_type_param
7731 || child_die->tag == DW_TAG_template_value_param)
7732 {
7733 struct symbol *arg = new_symbol (child_die, NULL, cu);
7734
f1078f66
DJ
7735 if (arg != NULL)
7736 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7737 }
7738
c906108c
SS
7739 child_die = sibling_die (child_die);
7740 }
7741
34eaf542
TT
7742 /* Attach template arguments to type. */
7743 if (! VEC_empty (symbolp, template_args))
7744 {
7745 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7746 TYPE_N_TEMPLATE_ARGUMENTS (type)
7747 = VEC_length (symbolp, template_args);
7748 TYPE_TEMPLATE_ARGUMENTS (type)
7749 = obstack_alloc (&objfile->objfile_obstack,
7750 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7751 * sizeof (struct symbol *)));
7752 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7753 VEC_address (symbolp, template_args),
7754 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7755 * sizeof (struct symbol *)));
7756 VEC_free (symbolp, template_args);
7757 }
7758
c906108c
SS
7759 /* Attach fields and member functions to the type. */
7760 if (fi.nfields)
e7c27a73 7761 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7762 if (fi.nfnfields)
7763 {
e7c27a73 7764 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7765
c5aa993b 7766 /* Get the type which refers to the base class (possibly this
c906108c 7767 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7768 class from the DW_AT_containing_type attribute. This use of
7769 DW_AT_containing_type is a GNU extension. */
c906108c 7770
e142c38c 7771 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7772 {
e7c27a73 7773 struct type *t = die_containing_type (die, cu);
c906108c
SS
7774
7775 TYPE_VPTR_BASETYPE (type) = t;
7776 if (type == t)
7777 {
c906108c
SS
7778 int i;
7779
7780 /* Our own class provides vtbl ptr. */
7781 for (i = TYPE_NFIELDS (t) - 1;
7782 i >= TYPE_N_BASECLASSES (t);
7783 --i)
7784 {
0d5cff50 7785 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 7786
1168df01 7787 if (is_vtable_name (fieldname, cu))
c906108c
SS
7788 {
7789 TYPE_VPTR_FIELDNO (type) = i;
7790 break;
7791 }
7792 }
7793
7794 /* Complain if virtual function table field not found. */
7795 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7796 complaint (&symfile_complaints,
3e43a32a
MS
7797 _("virtual function table pointer "
7798 "not found when defining class '%s'"),
4d3c2250
KB
7799 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7800 "");
c906108c
SS
7801 }
7802 else
7803 {
7804 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7805 }
7806 }
f6235d4c
EZ
7807 else if (cu->producer
7808 && strncmp (cu->producer,
7809 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7810 {
7811 /* The IBM XLC compiler does not provide direct indication
7812 of the containing type, but the vtable pointer is
7813 always named __vfp. */
7814
7815 int i;
7816
7817 for (i = TYPE_NFIELDS (type) - 1;
7818 i >= TYPE_N_BASECLASSES (type);
7819 --i)
7820 {
7821 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7822 {
7823 TYPE_VPTR_FIELDNO (type) = i;
7824 TYPE_VPTR_BASETYPE (type) = type;
7825 break;
7826 }
7827 }
7828 }
c906108c 7829 }
98751a41
JK
7830
7831 /* Copy fi.typedef_field_list linked list elements content into the
7832 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7833 if (fi.typedef_field_list)
7834 {
7835 int i = fi.typedef_field_list_count;
7836
a0d7a4ff 7837 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7838 TYPE_TYPEDEF_FIELD_ARRAY (type)
7839 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7840 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7841
7842 /* Reverse the list order to keep the debug info elements order. */
7843 while (--i >= 0)
7844 {
7845 struct typedef_field *dest, *src;
6e70227d 7846
98751a41
JK
7847 dest = &TYPE_TYPEDEF_FIELD (type, i);
7848 src = &fi.typedef_field_list->field;
7849 fi.typedef_field_list = fi.typedef_field_list->next;
7850 *dest = *src;
7851 }
7852 }
c767944b
DJ
7853
7854 do_cleanups (back_to);
eb2a6f42
TT
7855
7856 if (HAVE_CPLUS_STRUCT (type))
7857 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7858 }
63d06c5c 7859
bb5ed363 7860 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7861
90aeadfc
DC
7862 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7863 snapshots) has been known to create a die giving a declaration
7864 for a class that has, as a child, a die giving a definition for a
7865 nested class. So we have to process our children even if the
7866 current die is a declaration. Normally, of course, a declaration
7867 won't have any children at all. */
134d01f1 7868
90aeadfc
DC
7869 while (child_die != NULL && child_die->tag)
7870 {
7871 if (child_die->tag == DW_TAG_member
7872 || child_die->tag == DW_TAG_variable
34eaf542
TT
7873 || child_die->tag == DW_TAG_inheritance
7874 || child_die->tag == DW_TAG_template_value_param
7875 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7876 {
90aeadfc 7877 /* Do nothing. */
134d01f1 7878 }
90aeadfc
DC
7879 else
7880 process_die (child_die, cu);
134d01f1 7881
90aeadfc 7882 child_die = sibling_die (child_die);
134d01f1
DJ
7883 }
7884
fa4028e9
JB
7885 /* Do not consider external references. According to the DWARF standard,
7886 these DIEs are identified by the fact that they have no byte_size
7887 attribute, and a declaration attribute. */
7888 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7889 || !die_is_declaration (die, cu))
c767944b 7890 new_symbol (die, type, cu);
134d01f1
DJ
7891}
7892
7893/* Given a DW_AT_enumeration_type die, set its type. We do not
7894 complete the type's fields yet, or create any symbols. */
c906108c 7895
f792889a 7896static struct type *
134d01f1 7897read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7898{
e7c27a73 7899 struct objfile *objfile = cu->objfile;
c906108c 7900 struct type *type;
c906108c 7901 struct attribute *attr;
0114d602 7902 const char *name;
134d01f1 7903
348e048f
DE
7904 /* If the definition of this type lives in .debug_types, read that type.
7905 Don't follow DW_AT_specification though, that will take us back up
7906 the chain and we want to go down. */
7907 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7908 if (attr)
7909 {
7910 struct dwarf2_cu *type_cu = cu;
7911 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7912
348e048f 7913 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7914
7915 /* TYPE_CU may not be the same as CU.
7916 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7917 return set_die_type (die, type, cu);
7918 }
7919
c906108c
SS
7920 type = alloc_type (objfile);
7921
7922 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7923 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7924 if (name != NULL)
0114d602 7925 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7926
e142c38c 7927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7928 if (attr)
7929 {
7930 TYPE_LENGTH (type) = DW_UNSND (attr);
7931 }
7932 else
7933 {
7934 TYPE_LENGTH (type) = 0;
7935 }
7936
137033e9
JB
7937 /* The enumeration DIE can be incomplete. In Ada, any type can be
7938 declared as private in the package spec, and then defined only
7939 inside the package body. Such types are known as Taft Amendment
7940 Types. When another package uses such a type, an incomplete DIE
7941 may be generated by the compiler. */
02eb380e 7942 if (die_is_declaration (die, cu))
876cecd0 7943 TYPE_STUB (type) = 1;
02eb380e 7944
f792889a 7945 return set_die_type (die, type, cu);
134d01f1
DJ
7946}
7947
7948/* Given a pointer to a die which begins an enumeration, process all
7949 the dies that define the members of the enumeration, and create the
7950 symbol for the enumeration type.
7951
7952 NOTE: We reverse the order of the element list. */
7953
7954static void
7955process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7956{
f792889a 7957 struct type *this_type;
134d01f1 7958
f792889a
DJ
7959 this_type = get_die_type (die, cu);
7960 if (this_type == NULL)
7961 this_type = read_enumeration_type (die, cu);
9dc481d3 7962
639d11d3 7963 if (die->child != NULL)
c906108c 7964 {
9dc481d3
DE
7965 struct die_info *child_die;
7966 struct symbol *sym;
7967 struct field *fields = NULL;
7968 int num_fields = 0;
7969 int unsigned_enum = 1;
7970 char *name;
cafec441
TT
7971 int flag_enum = 1;
7972 ULONGEST mask = 0;
9dc481d3 7973
639d11d3 7974 child_die = die->child;
c906108c
SS
7975 while (child_die && child_die->tag)
7976 {
7977 if (child_die->tag != DW_TAG_enumerator)
7978 {
e7c27a73 7979 process_die (child_die, cu);
c906108c
SS
7980 }
7981 else
7982 {
39cbfefa
DJ
7983 name = dwarf2_name (child_die, cu);
7984 if (name)
c906108c 7985 {
f792889a 7986 sym = new_symbol (child_die, this_type, cu);
c906108c 7987 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
7988 {
7989 unsigned_enum = 0;
7990 flag_enum = 0;
7991 }
7992 else if ((mask & SYMBOL_VALUE (sym)) != 0)
7993 flag_enum = 0;
7994 else
7995 mask |= SYMBOL_VALUE (sym);
c906108c
SS
7996
7997 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7998 {
7999 fields = (struct field *)
8000 xrealloc (fields,
8001 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8002 * sizeof (struct field));
c906108c
SS
8003 }
8004
3567439c 8005 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8006 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8007 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8008 FIELD_BITSIZE (fields[num_fields]) = 0;
8009
8010 num_fields++;
8011 }
8012 }
8013
8014 child_die = sibling_die (child_die);
8015 }
8016
8017 if (num_fields)
8018 {
f792889a
DJ
8019 TYPE_NFIELDS (this_type) = num_fields;
8020 TYPE_FIELDS (this_type) = (struct field *)
8021 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8022 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8023 sizeof (struct field) * num_fields);
b8c9b27d 8024 xfree (fields);
c906108c
SS
8025 }
8026 if (unsigned_enum)
876cecd0 8027 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
8028 if (flag_enum)
8029 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 8030 }
134d01f1 8031
6c83ed52
TT
8032 /* If we are reading an enum from a .debug_types unit, and the enum
8033 is a declaration, and the enum is not the signatured type in the
8034 unit, then we do not want to add a symbol for it. Adding a
8035 symbol would in some cases obscure the true definition of the
8036 enum, giving users an incomplete type when the definition is
8037 actually available. Note that we do not want to do this for all
8038 enums which are just declarations, because C++0x allows forward
8039 enum declarations. */
b0df02fd 8040 if (cu->per_cu->debug_types_section
6c83ed52
TT
8041 && die_is_declaration (die, cu))
8042 {
8043 struct signatured_type *type_sig;
8044
8045 type_sig
8046 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8047 cu->per_cu->debug_types_section,
6c83ed52 8048 cu->per_cu->offset);
7fe25d9b
JK
8049 if (type_sig->per_cu.offset + type_sig->type_offset
8050 != die->offset)
6c83ed52
TT
8051 return;
8052 }
8053
f792889a 8054 new_symbol (die, this_type, cu);
c906108c
SS
8055}
8056
8057/* Extract all information from a DW_TAG_array_type DIE and put it in
8058 the DIE's type field. For now, this only handles one dimensional
8059 arrays. */
8060
f792889a 8061static struct type *
e7c27a73 8062read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8063{
e7c27a73 8064 struct objfile *objfile = cu->objfile;
c906108c 8065 struct die_info *child_die;
7e314c57 8066 struct type *type;
c906108c
SS
8067 struct type *element_type, *range_type, *index_type;
8068 struct type **range_types = NULL;
8069 struct attribute *attr;
8070 int ndim = 0;
8071 struct cleanup *back_to;
39cbfefa 8072 char *name;
c906108c 8073
e7c27a73 8074 element_type = die_type (die, cu);
c906108c 8075
7e314c57
JK
8076 /* The die_type call above may have already set the type for this DIE. */
8077 type = get_die_type (die, cu);
8078 if (type)
8079 return type;
8080
c906108c
SS
8081 /* Irix 6.2 native cc creates array types without children for
8082 arrays with unspecified length. */
639d11d3 8083 if (die->child == NULL)
c906108c 8084 {
46bf5051 8085 index_type = objfile_type (objfile)->builtin_int;
c906108c 8086 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8087 type = create_array_type (NULL, element_type, range_type);
8088 return set_die_type (die, type, cu);
c906108c
SS
8089 }
8090
8091 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8092 child_die = die->child;
c906108c
SS
8093 while (child_die && child_die->tag)
8094 {
8095 if (child_die->tag == DW_TAG_subrange_type)
8096 {
f792889a 8097 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8098
f792889a 8099 if (child_type != NULL)
a02abb62 8100 {
0963b4bd
MS
8101 /* The range type was succesfully read. Save it for the
8102 array type creation. */
a02abb62
JB
8103 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8104 {
8105 range_types = (struct type **)
8106 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8107 * sizeof (struct type *));
8108 if (ndim == 0)
8109 make_cleanup (free_current_contents, &range_types);
8110 }
f792889a 8111 range_types[ndim++] = child_type;
a02abb62 8112 }
c906108c
SS
8113 }
8114 child_die = sibling_die (child_die);
8115 }
8116
8117 /* Dwarf2 dimensions are output from left to right, create the
8118 necessary array types in backwards order. */
7ca2d3a3 8119
c906108c 8120 type = element_type;
7ca2d3a3
DL
8121
8122 if (read_array_order (die, cu) == DW_ORD_col_major)
8123 {
8124 int i = 0;
9a619af0 8125
7ca2d3a3
DL
8126 while (i < ndim)
8127 type = create_array_type (NULL, type, range_types[i++]);
8128 }
8129 else
8130 {
8131 while (ndim-- > 0)
8132 type = create_array_type (NULL, type, range_types[ndim]);
8133 }
c906108c 8134
f5f8a009
EZ
8135 /* Understand Dwarf2 support for vector types (like they occur on
8136 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8137 array type. This is not part of the Dwarf2/3 standard yet, but a
8138 custom vendor extension. The main difference between a regular
8139 array and the vector variant is that vectors are passed by value
8140 to functions. */
e142c38c 8141 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8142 if (attr)
ea37ba09 8143 make_vector_type (type);
f5f8a009 8144
dbc98a8b
KW
8145 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8146 implementation may choose to implement triple vectors using this
8147 attribute. */
8148 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8149 if (attr)
8150 {
8151 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8152 TYPE_LENGTH (type) = DW_UNSND (attr);
8153 else
3e43a32a
MS
8154 complaint (&symfile_complaints,
8155 _("DW_AT_byte_size for array type smaller "
8156 "than the total size of elements"));
dbc98a8b
KW
8157 }
8158
39cbfefa
DJ
8159 name = dwarf2_name (die, cu);
8160 if (name)
8161 TYPE_NAME (type) = name;
6e70227d 8162
0963b4bd 8163 /* Install the type in the die. */
7e314c57
JK
8164 set_die_type (die, type, cu);
8165
8166 /* set_die_type should be already done. */
b4ba55a1
JB
8167 set_descriptive_type (type, die, cu);
8168
c906108c
SS
8169 do_cleanups (back_to);
8170
7e314c57 8171 return type;
c906108c
SS
8172}
8173
7ca2d3a3 8174static enum dwarf_array_dim_ordering
6e70227d 8175read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8176{
8177 struct attribute *attr;
8178
8179 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8180
8181 if (attr) return DW_SND (attr);
8182
0963b4bd
MS
8183 /* GNU F77 is a special case, as at 08/2004 array type info is the
8184 opposite order to the dwarf2 specification, but data is still
8185 laid out as per normal fortran.
7ca2d3a3 8186
0963b4bd
MS
8187 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8188 version checking. */
7ca2d3a3 8189
905e0470
PM
8190 if (cu->language == language_fortran
8191 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8192 {
8193 return DW_ORD_row_major;
8194 }
8195
6e70227d 8196 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8197 {
8198 case array_column_major:
8199 return DW_ORD_col_major;
8200 case array_row_major:
8201 default:
8202 return DW_ORD_row_major;
8203 };
8204}
8205
72019c9c 8206/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8207 the DIE's type field. */
72019c9c 8208
f792889a 8209static struct type *
72019c9c
GM
8210read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8211{
7e314c57
JK
8212 struct type *domain_type, *set_type;
8213 struct attribute *attr;
f792889a 8214
7e314c57
JK
8215 domain_type = die_type (die, cu);
8216
8217 /* The die_type call above may have already set the type for this DIE. */
8218 set_type = get_die_type (die, cu);
8219 if (set_type)
8220 return set_type;
8221
8222 set_type = create_set_type (NULL, domain_type);
8223
8224 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8225 if (attr)
8226 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8227
f792889a 8228 return set_die_type (die, set_type, cu);
72019c9c 8229}
7ca2d3a3 8230
c906108c
SS
8231/* First cut: install each common block member as a global variable. */
8232
8233static void
e7c27a73 8234read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8235{
8236 struct die_info *child_die;
8237 struct attribute *attr;
8238 struct symbol *sym;
8239 CORE_ADDR base = (CORE_ADDR) 0;
8240
e142c38c 8241 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8242 if (attr)
8243 {
0963b4bd 8244 /* Support the .debug_loc offsets. */
8e19ed76
PS
8245 if (attr_form_is_block (attr))
8246 {
e7c27a73 8247 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8248 }
3690dd37 8249 else if (attr_form_is_section_offset (attr))
8e19ed76 8250 {
4d3c2250 8251 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8252 }
8253 else
8254 {
4d3c2250
KB
8255 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8256 "common block member");
8e19ed76 8257 }
c906108c 8258 }
639d11d3 8259 if (die->child != NULL)
c906108c 8260 {
639d11d3 8261 child_die = die->child;
c906108c
SS
8262 while (child_die && child_die->tag)
8263 {
74ac6d43
TT
8264 LONGEST offset;
8265
e7c27a73 8266 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8267 if (sym != NULL
8268 && handle_data_member_location (child_die, cu, &offset))
c906108c 8269 {
74ac6d43 8270 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8271 add_symbol_to_list (sym, &global_symbols);
8272 }
8273 child_die = sibling_die (child_die);
8274 }
8275 }
8276}
8277
0114d602 8278/* Create a type for a C++ namespace. */
d9fa45fe 8279
0114d602
DJ
8280static struct type *
8281read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8282{
e7c27a73 8283 struct objfile *objfile = cu->objfile;
0114d602 8284 const char *previous_prefix, *name;
9219021c 8285 int is_anonymous;
0114d602
DJ
8286 struct type *type;
8287
8288 /* For extensions, reuse the type of the original namespace. */
8289 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8290 {
8291 struct die_info *ext_die;
8292 struct dwarf2_cu *ext_cu = cu;
9a619af0 8293
0114d602
DJ
8294 ext_die = dwarf2_extension (die, &ext_cu);
8295 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8296
8297 /* EXT_CU may not be the same as CU.
8298 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8299 return set_die_type (die, type, cu);
8300 }
9219021c 8301
e142c38c 8302 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8303
8304 /* Now build the name of the current namespace. */
8305
0114d602
DJ
8306 previous_prefix = determine_prefix (die, cu);
8307 if (previous_prefix[0] != '\0')
8308 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8309 previous_prefix, name, 0, cu);
0114d602
DJ
8310
8311 /* Create the type. */
8312 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8313 objfile);
8314 TYPE_NAME (type) = (char *) name;
8315 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8316
60531b24 8317 return set_die_type (die, type, cu);
0114d602
DJ
8318}
8319
8320/* Read a C++ namespace. */
8321
8322static void
8323read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8324{
8325 struct objfile *objfile = cu->objfile;
0114d602 8326 int is_anonymous;
9219021c 8327
5c4e30ca
DC
8328 /* Add a symbol associated to this if we haven't seen the namespace
8329 before. Also, add a using directive if it's an anonymous
8330 namespace. */
9219021c 8331
f2f0e013 8332 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8333 {
8334 struct type *type;
8335
0114d602 8336 type = read_type_die (die, cu);
e7c27a73 8337 new_symbol (die, type, cu);
5c4e30ca 8338
e8e80198 8339 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8340 if (is_anonymous)
0114d602
DJ
8341 {
8342 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8343
c0cc3a76 8344 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8345 NULL, NULL, &objfile->objfile_obstack);
0114d602 8346 }
5c4e30ca 8347 }
9219021c 8348
639d11d3 8349 if (die->child != NULL)
d9fa45fe 8350 {
639d11d3 8351 struct die_info *child_die = die->child;
6e70227d 8352
d9fa45fe
DC
8353 while (child_die && child_die->tag)
8354 {
e7c27a73 8355 process_die (child_die, cu);
d9fa45fe
DC
8356 child_die = sibling_die (child_die);
8357 }
8358 }
38d518c9
EZ
8359}
8360
f55ee35c
JK
8361/* Read a Fortran module as type. This DIE can be only a declaration used for
8362 imported module. Still we need that type as local Fortran "use ... only"
8363 declaration imports depend on the created type in determine_prefix. */
8364
8365static struct type *
8366read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8367{
8368 struct objfile *objfile = cu->objfile;
8369 char *module_name;
8370 struct type *type;
8371
8372 module_name = dwarf2_name (die, cu);
8373 if (!module_name)
3e43a32a
MS
8374 complaint (&symfile_complaints,
8375 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8376 die->offset);
8377 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8378
8379 /* determine_prefix uses TYPE_TAG_NAME. */
8380 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8381
8382 return set_die_type (die, type, cu);
8383}
8384
5d7cb8df
JK
8385/* Read a Fortran module. */
8386
8387static void
8388read_module (struct die_info *die, struct dwarf2_cu *cu)
8389{
8390 struct die_info *child_die = die->child;
8391
5d7cb8df
JK
8392 while (child_die && child_die->tag)
8393 {
8394 process_die (child_die, cu);
8395 child_die = sibling_die (child_die);
8396 }
8397}
8398
38d518c9
EZ
8399/* Return the name of the namespace represented by DIE. Set
8400 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8401 namespace. */
8402
8403static const char *
e142c38c 8404namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8405{
8406 struct die_info *current_die;
8407 const char *name = NULL;
8408
8409 /* Loop through the extensions until we find a name. */
8410
8411 for (current_die = die;
8412 current_die != NULL;
f2f0e013 8413 current_die = dwarf2_extension (die, &cu))
38d518c9 8414 {
e142c38c 8415 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8416 if (name != NULL)
8417 break;
8418 }
8419
8420 /* Is it an anonymous namespace? */
8421
8422 *is_anonymous = (name == NULL);
8423 if (*is_anonymous)
2b1dbab0 8424 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8425
8426 return name;
d9fa45fe
DC
8427}
8428
c906108c
SS
8429/* Extract all information from a DW_TAG_pointer_type DIE and add to
8430 the user defined type vector. */
8431
f792889a 8432static struct type *
e7c27a73 8433read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8434{
5e2b427d 8435 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8436 struct comp_unit_head *cu_header = &cu->header;
c906108c 8437 struct type *type;
8b2dbe47
KB
8438 struct attribute *attr_byte_size;
8439 struct attribute *attr_address_class;
8440 int byte_size, addr_class;
7e314c57
JK
8441 struct type *target_type;
8442
8443 target_type = die_type (die, cu);
c906108c 8444
7e314c57
JK
8445 /* The die_type call above may have already set the type for this DIE. */
8446 type = get_die_type (die, cu);
8447 if (type)
8448 return type;
8449
8450 type = lookup_pointer_type (target_type);
8b2dbe47 8451
e142c38c 8452 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8453 if (attr_byte_size)
8454 byte_size = DW_UNSND (attr_byte_size);
c906108c 8455 else
8b2dbe47
KB
8456 byte_size = cu_header->addr_size;
8457
e142c38c 8458 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8459 if (attr_address_class)
8460 addr_class = DW_UNSND (attr_address_class);
8461 else
8462 addr_class = DW_ADDR_none;
8463
8464 /* If the pointer size or address class is different than the
8465 default, create a type variant marked as such and set the
8466 length accordingly. */
8467 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8468 {
5e2b427d 8469 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8470 {
8471 int type_flags;
8472
849957d9 8473 type_flags = gdbarch_address_class_type_flags
5e2b427d 8474 (gdbarch, byte_size, addr_class);
876cecd0
TT
8475 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8476 == 0);
8b2dbe47
KB
8477 type = make_type_with_address_space (type, type_flags);
8478 }
8479 else if (TYPE_LENGTH (type) != byte_size)
8480 {
3e43a32a
MS
8481 complaint (&symfile_complaints,
8482 _("invalid pointer size %d"), byte_size);
8b2dbe47 8483 }
6e70227d 8484 else
9a619af0
MS
8485 {
8486 /* Should we also complain about unhandled address classes? */
8487 }
c906108c 8488 }
8b2dbe47
KB
8489
8490 TYPE_LENGTH (type) = byte_size;
f792889a 8491 return set_die_type (die, type, cu);
c906108c
SS
8492}
8493
8494/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8495 the user defined type vector. */
8496
f792889a 8497static struct type *
e7c27a73 8498read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8499{
8500 struct type *type;
8501 struct type *to_type;
8502 struct type *domain;
8503
e7c27a73
DJ
8504 to_type = die_type (die, cu);
8505 domain = die_containing_type (die, cu);
0d5de010 8506
7e314c57
JK
8507 /* The calls above may have already set the type for this DIE. */
8508 type = get_die_type (die, cu);
8509 if (type)
8510 return type;
8511
0d5de010
DJ
8512 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8513 type = lookup_methodptr_type (to_type);
8514 else
8515 type = lookup_memberptr_type (to_type, domain);
c906108c 8516
f792889a 8517 return set_die_type (die, type, cu);
c906108c
SS
8518}
8519
8520/* Extract all information from a DW_TAG_reference_type DIE and add to
8521 the user defined type vector. */
8522
f792889a 8523static struct type *
e7c27a73 8524read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8525{
e7c27a73 8526 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8527 struct type *type, *target_type;
c906108c
SS
8528 struct attribute *attr;
8529
7e314c57
JK
8530 target_type = die_type (die, cu);
8531
8532 /* The die_type call above may have already set the type for this DIE. */
8533 type = get_die_type (die, cu);
8534 if (type)
8535 return type;
8536
8537 type = lookup_reference_type (target_type);
e142c38c 8538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8539 if (attr)
8540 {
8541 TYPE_LENGTH (type) = DW_UNSND (attr);
8542 }
8543 else
8544 {
107d2387 8545 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8546 }
f792889a 8547 return set_die_type (die, type, cu);
c906108c
SS
8548}
8549
f792889a 8550static struct type *
e7c27a73 8551read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8552{
f792889a 8553 struct type *base_type, *cv_type;
c906108c 8554
e7c27a73 8555 base_type = die_type (die, cu);
7e314c57
JK
8556
8557 /* The die_type call above may have already set the type for this DIE. */
8558 cv_type = get_die_type (die, cu);
8559 if (cv_type)
8560 return cv_type;
8561
2f608a3a
KW
8562 /* In case the const qualifier is applied to an array type, the element type
8563 is so qualified, not the array type (section 6.7.3 of C99). */
8564 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8565 {
8566 struct type *el_type, *inner_array;
8567
8568 base_type = copy_type (base_type);
8569 inner_array = base_type;
8570
8571 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8572 {
8573 TYPE_TARGET_TYPE (inner_array) =
8574 copy_type (TYPE_TARGET_TYPE (inner_array));
8575 inner_array = TYPE_TARGET_TYPE (inner_array);
8576 }
8577
8578 el_type = TYPE_TARGET_TYPE (inner_array);
8579 TYPE_TARGET_TYPE (inner_array) =
8580 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8581
8582 return set_die_type (die, base_type, cu);
8583 }
8584
f792889a
DJ
8585 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8586 return set_die_type (die, cv_type, cu);
c906108c
SS
8587}
8588
f792889a 8589static struct type *
e7c27a73 8590read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8591{
f792889a 8592 struct type *base_type, *cv_type;
c906108c 8593
e7c27a73 8594 base_type = die_type (die, cu);
7e314c57
JK
8595
8596 /* The die_type call above may have already set the type for this DIE. */
8597 cv_type = get_die_type (die, cu);
8598 if (cv_type)
8599 return cv_type;
8600
f792889a
DJ
8601 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8602 return set_die_type (die, cv_type, cu);
c906108c
SS
8603}
8604
8605/* Extract all information from a DW_TAG_string_type DIE and add to
8606 the user defined type vector. It isn't really a user defined type,
8607 but it behaves like one, with other DIE's using an AT_user_def_type
8608 attribute to reference it. */
8609
f792889a 8610static struct type *
e7c27a73 8611read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8612{
e7c27a73 8613 struct objfile *objfile = cu->objfile;
3b7538c0 8614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8615 struct type *type, *range_type, *index_type, *char_type;
8616 struct attribute *attr;
8617 unsigned int length;
8618
e142c38c 8619 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8620 if (attr)
8621 {
8622 length = DW_UNSND (attr);
8623 }
8624 else
8625 {
0963b4bd 8626 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8628 if (attr)
8629 {
8630 length = DW_UNSND (attr);
8631 }
8632 else
8633 {
8634 length = 1;
8635 }
c906108c 8636 }
6ccb9162 8637
46bf5051 8638 index_type = objfile_type (objfile)->builtin_int;
c906108c 8639 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8640 char_type = language_string_char_type (cu->language_defn, gdbarch);
8641 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8642
f792889a 8643 return set_die_type (die, type, cu);
c906108c
SS
8644}
8645
8646/* Handle DIES due to C code like:
8647
8648 struct foo
c5aa993b
JM
8649 {
8650 int (*funcp)(int a, long l);
8651 int b;
8652 };
c906108c 8653
0963b4bd 8654 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8655
f792889a 8656static struct type *
e7c27a73 8657read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8658{
bb5ed363 8659 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8660 struct type *type; /* Type that this function returns. */
8661 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8662 struct attribute *attr;
8663
e7c27a73 8664 type = die_type (die, cu);
7e314c57
JK
8665
8666 /* The die_type call above may have already set the type for this DIE. */
8667 ftype = get_die_type (die, cu);
8668 if (ftype)
8669 return ftype;
8670
0c8b41f1 8671 ftype = lookup_function_type (type);
c906108c 8672
5b8101ae 8673 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8674 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8675 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8676 || cu->language == language_cplus
5b8101ae
PM
8677 || cu->language == language_java
8678 || cu->language == language_pascal)
876cecd0 8679 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8680 else if (producer_is_realview (cu->producer))
8681 /* RealView does not emit DW_AT_prototyped. We can not
8682 distinguish prototyped and unprototyped functions; default to
8683 prototyped, since that is more common in modern code (and
8684 RealView warns about unprototyped functions). */
8685 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8686
c055b101
CV
8687 /* Store the calling convention in the type if it's available in
8688 the subroutine die. Otherwise set the calling convention to
8689 the default value DW_CC_normal. */
8690 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8691 if (attr)
8692 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8693 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8694 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8695 else
8696 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8697
8698 /* We need to add the subroutine type to the die immediately so
8699 we don't infinitely recurse when dealing with parameters
0963b4bd 8700 declared as the same subroutine type. */
76c10ea2 8701 set_die_type (die, ftype, cu);
6e70227d 8702
639d11d3 8703 if (die->child != NULL)
c906108c 8704 {
bb5ed363 8705 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8706 struct die_info *child_die;
8072405b 8707 int nparams, iparams;
c906108c
SS
8708
8709 /* Count the number of parameters.
8710 FIXME: GDB currently ignores vararg functions, but knows about
8711 vararg member functions. */
8072405b 8712 nparams = 0;
639d11d3 8713 child_die = die->child;
c906108c
SS
8714 while (child_die && child_die->tag)
8715 {
8716 if (child_die->tag == DW_TAG_formal_parameter)
8717 nparams++;
8718 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8719 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8720 child_die = sibling_die (child_die);
8721 }
8722
8723 /* Allocate storage for parameters and fill them in. */
8724 TYPE_NFIELDS (ftype) = nparams;
8725 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8726 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8727
8072405b
JK
8728 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8729 even if we error out during the parameters reading below. */
8730 for (iparams = 0; iparams < nparams; iparams++)
8731 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8732
8733 iparams = 0;
639d11d3 8734 child_die = die->child;
c906108c
SS
8735 while (child_die && child_die->tag)
8736 {
8737 if (child_die->tag == DW_TAG_formal_parameter)
8738 {
3ce3b1ba
PA
8739 struct type *arg_type;
8740
8741 /* DWARF version 2 has no clean way to discern C++
8742 static and non-static member functions. G++ helps
8743 GDB by marking the first parameter for non-static
8744 member functions (which is the this pointer) as
8745 artificial. We pass this information to
8746 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8747
8748 DWARF version 3 added DW_AT_object_pointer, which GCC
8749 4.5 does not yet generate. */
e142c38c 8750 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8751 if (attr)
8752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8753 else
418835cc
KS
8754 {
8755 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8756
8757 /* GCC/43521: In java, the formal parameter
8758 "this" is sometimes not marked with DW_AT_artificial. */
8759 if (cu->language == language_java)
8760 {
8761 const char *name = dwarf2_name (child_die, cu);
9a619af0 8762
418835cc
KS
8763 if (name && !strcmp (name, "this"))
8764 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8765 }
8766 }
3ce3b1ba
PA
8767 arg_type = die_type (child_die, cu);
8768
8769 /* RealView does not mark THIS as const, which the testsuite
8770 expects. GCC marks THIS as const in method definitions,
8771 but not in the class specifications (GCC PR 43053). */
8772 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8773 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8774 {
8775 int is_this = 0;
8776 struct dwarf2_cu *arg_cu = cu;
8777 const char *name = dwarf2_name (child_die, cu);
8778
8779 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8780 if (attr)
8781 {
8782 /* If the compiler emits this, use it. */
8783 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8784 is_this = 1;
8785 }
8786 else if (name && strcmp (name, "this") == 0)
8787 /* Function definitions will have the argument names. */
8788 is_this = 1;
8789 else if (name == NULL && iparams == 0)
8790 /* Declarations may not have the names, so like
8791 elsewhere in GDB, assume an artificial first
8792 argument is "this". */
8793 is_this = 1;
8794
8795 if (is_this)
8796 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8797 arg_type, 0);
8798 }
8799
8800 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8801 iparams++;
8802 }
8803 child_die = sibling_die (child_die);
8804 }
8805 }
8806
76c10ea2 8807 return ftype;
c906108c
SS
8808}
8809
f792889a 8810static struct type *
e7c27a73 8811read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8812{
e7c27a73 8813 struct objfile *objfile = cu->objfile;
0114d602 8814 const char *name = NULL;
3c8e0968 8815 struct type *this_type, *target_type;
c906108c 8816
94af9270 8817 name = dwarf2_full_name (NULL, die, cu);
f792889a 8818 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8819 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8820 TYPE_NAME (this_type) = (char *) name;
f792889a 8821 set_die_type (die, this_type, cu);
3c8e0968
DE
8822 target_type = die_type (die, cu);
8823 if (target_type != this_type)
8824 TYPE_TARGET_TYPE (this_type) = target_type;
8825 else
8826 {
8827 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8828 spec and cause infinite loops in GDB. */
8829 complaint (&symfile_complaints,
8830 _("Self-referential DW_TAG_typedef "
8831 "- DIE at 0x%x [in module %s]"),
bb5ed363 8832 die->offset, objfile->name);
3c8e0968
DE
8833 TYPE_TARGET_TYPE (this_type) = NULL;
8834 }
f792889a 8835 return this_type;
c906108c
SS
8836}
8837
8838/* Find a representation of a given base type and install
8839 it in the TYPE field of the die. */
8840
f792889a 8841static struct type *
e7c27a73 8842read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8843{
e7c27a73 8844 struct objfile *objfile = cu->objfile;
c906108c
SS
8845 struct type *type;
8846 struct attribute *attr;
8847 int encoding = 0, size = 0;
39cbfefa 8848 char *name;
6ccb9162
UW
8849 enum type_code code = TYPE_CODE_INT;
8850 int type_flags = 0;
8851 struct type *target_type = NULL;
c906108c 8852
e142c38c 8853 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8854 if (attr)
8855 {
8856 encoding = DW_UNSND (attr);
8857 }
e142c38c 8858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8859 if (attr)
8860 {
8861 size = DW_UNSND (attr);
8862 }
39cbfefa 8863 name = dwarf2_name (die, cu);
6ccb9162 8864 if (!name)
c906108c 8865 {
6ccb9162
UW
8866 complaint (&symfile_complaints,
8867 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8868 }
6ccb9162
UW
8869
8870 switch (encoding)
c906108c 8871 {
6ccb9162
UW
8872 case DW_ATE_address:
8873 /* Turn DW_ATE_address into a void * pointer. */
8874 code = TYPE_CODE_PTR;
8875 type_flags |= TYPE_FLAG_UNSIGNED;
8876 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8877 break;
8878 case DW_ATE_boolean:
8879 code = TYPE_CODE_BOOL;
8880 type_flags |= TYPE_FLAG_UNSIGNED;
8881 break;
8882 case DW_ATE_complex_float:
8883 code = TYPE_CODE_COMPLEX;
8884 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8885 break;
8886 case DW_ATE_decimal_float:
8887 code = TYPE_CODE_DECFLOAT;
8888 break;
8889 case DW_ATE_float:
8890 code = TYPE_CODE_FLT;
8891 break;
8892 case DW_ATE_signed:
8893 break;
8894 case DW_ATE_unsigned:
8895 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8896 if (cu->language == language_fortran
8897 && name
8898 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8899 code = TYPE_CODE_CHAR;
6ccb9162
UW
8900 break;
8901 case DW_ATE_signed_char:
6e70227d 8902 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8903 || cu->language == language_pascal
8904 || cu->language == language_fortran)
6ccb9162
UW
8905 code = TYPE_CODE_CHAR;
8906 break;
8907 case DW_ATE_unsigned_char:
868a0084 8908 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8909 || cu->language == language_pascal
8910 || cu->language == language_fortran)
6ccb9162
UW
8911 code = TYPE_CODE_CHAR;
8912 type_flags |= TYPE_FLAG_UNSIGNED;
8913 break;
75079b2b
TT
8914 case DW_ATE_UTF:
8915 /* We just treat this as an integer and then recognize the
8916 type by name elsewhere. */
8917 break;
8918
6ccb9162
UW
8919 default:
8920 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8921 dwarf_type_encoding_name (encoding));
8922 break;
c906108c 8923 }
6ccb9162 8924
0114d602
DJ
8925 type = init_type (code, size, type_flags, NULL, objfile);
8926 TYPE_NAME (type) = name;
6ccb9162
UW
8927 TYPE_TARGET_TYPE (type) = target_type;
8928
0114d602 8929 if (name && strcmp (name, "char") == 0)
876cecd0 8930 TYPE_NOSIGN (type) = 1;
0114d602 8931
f792889a 8932 return set_die_type (die, type, cu);
c906108c
SS
8933}
8934
a02abb62
JB
8935/* Read the given DW_AT_subrange DIE. */
8936
f792889a 8937static struct type *
a02abb62
JB
8938read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8939{
8940 struct type *base_type;
8941 struct type *range_type;
8942 struct attribute *attr;
43bbcdc2
PH
8943 LONGEST low = 0;
8944 LONGEST high = -1;
39cbfefa 8945 char *name;
43bbcdc2 8946 LONGEST negative_mask;
e77813c8 8947
a02abb62 8948 base_type = die_type (die, cu);
953ac07e
JK
8949 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8950 check_typedef (base_type);
a02abb62 8951
7e314c57
JK
8952 /* The die_type call above may have already set the type for this DIE. */
8953 range_type = get_die_type (die, cu);
8954 if (range_type)
8955 return range_type;
8956
e142c38c 8957 if (cu->language == language_fortran)
6e70227d 8958 {
a02abb62
JB
8959 /* FORTRAN implies a lower bound of 1, if not given. */
8960 low = 1;
8961 }
8962
dd5e6932
DJ
8963 /* FIXME: For variable sized arrays either of these could be
8964 a variable rather than a constant value. We'll allow it,
8965 but we don't know how to handle it. */
e142c38c 8966 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8967 if (attr)
8968 low = dwarf2_get_attr_constant_value (attr, 0);
8969
e142c38c 8970 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8971 if (attr)
6e70227d 8972 {
d48323d8 8973 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8974 {
8975 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8976 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8977 FIXME: GDB does not yet know how to handle dynamic
8978 arrays properly, treat them as arrays with unspecified
8979 length for now.
8980
8981 FIXME: jimb/2003-09-22: GDB does not really know
8982 how to handle arrays of unspecified length
8983 either; we just represent them as zero-length
8984 arrays. Choose an appropriate upper bound given
8985 the lower bound we've computed above. */
8986 high = low - 1;
8987 }
8988 else
8989 high = dwarf2_get_attr_constant_value (attr, 1);
8990 }
e77813c8
PM
8991 else
8992 {
8993 attr = dwarf2_attr (die, DW_AT_count, cu);
8994 if (attr)
8995 {
8996 int count = dwarf2_get_attr_constant_value (attr, 1);
8997 high = low + count - 1;
8998 }
c2ff108b
JK
8999 else
9000 {
9001 /* Unspecified array length. */
9002 high = low - 1;
9003 }
e77813c8
PM
9004 }
9005
9006 /* Dwarf-2 specifications explicitly allows to create subrange types
9007 without specifying a base type.
9008 In that case, the base type must be set to the type of
9009 the lower bound, upper bound or count, in that order, if any of these
9010 three attributes references an object that has a type.
9011 If no base type is found, the Dwarf-2 specifications say that
9012 a signed integer type of size equal to the size of an address should
9013 be used.
9014 For the following C code: `extern char gdb_int [];'
9015 GCC produces an empty range DIE.
9016 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9017 high bound or count are not yet handled by this code. */
e77813c8
PM
9018 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9019 {
9020 struct objfile *objfile = cu->objfile;
9021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9022 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9023 struct type *int_type = objfile_type (objfile)->builtin_int;
9024
9025 /* Test "int", "long int", and "long long int" objfile types,
9026 and select the first one having a size above or equal to the
9027 architecture address size. */
9028 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9029 base_type = int_type;
9030 else
9031 {
9032 int_type = objfile_type (objfile)->builtin_long;
9033 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9034 base_type = int_type;
9035 else
9036 {
9037 int_type = objfile_type (objfile)->builtin_long_long;
9038 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9039 base_type = int_type;
9040 }
9041 }
9042 }
a02abb62 9043
6e70227d 9044 negative_mask =
43bbcdc2
PH
9045 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9046 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9047 low |= negative_mask;
9048 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9049 high |= negative_mask;
9050
a02abb62
JB
9051 range_type = create_range_type (NULL, base_type, low, high);
9052
bbb0eef6
JK
9053 /* Mark arrays with dynamic length at least as an array of unspecified
9054 length. GDB could check the boundary but before it gets implemented at
9055 least allow accessing the array elements. */
d48323d8 9056 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9057 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9058
c2ff108b
JK
9059 /* Ada expects an empty array on no boundary attributes. */
9060 if (attr == NULL && cu->language != language_ada)
9061 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9062
39cbfefa
DJ
9063 name = dwarf2_name (die, cu);
9064 if (name)
9065 TYPE_NAME (range_type) = name;
6e70227d 9066
e142c38c 9067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9068 if (attr)
9069 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9070
7e314c57
JK
9071 set_die_type (die, range_type, cu);
9072
9073 /* set_die_type should be already done. */
b4ba55a1
JB
9074 set_descriptive_type (range_type, die, cu);
9075
7e314c57 9076 return range_type;
a02abb62 9077}
6e70227d 9078
f792889a 9079static struct type *
81a17f79
JB
9080read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9081{
9082 struct type *type;
81a17f79 9083
81a17f79
JB
9084 /* For now, we only support the C meaning of an unspecified type: void. */
9085
0114d602
DJ
9086 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9087 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9088
f792889a 9089 return set_die_type (die, type, cu);
81a17f79 9090}
a02abb62 9091
51545339
DJ
9092/* Trivial hash function for die_info: the hash value of a DIE
9093 is its offset in .debug_info for this objfile. */
9094
9095static hashval_t
9096die_hash (const void *item)
9097{
9098 const struct die_info *die = item;
9a619af0 9099
51545339
DJ
9100 return die->offset;
9101}
9102
9103/* Trivial comparison function for die_info structures: two DIEs
9104 are equal if they have the same offset. */
9105
9106static int
9107die_eq (const void *item_lhs, const void *item_rhs)
9108{
9109 const struct die_info *die_lhs = item_lhs;
9110 const struct die_info *die_rhs = item_rhs;
9a619af0 9111
51545339
DJ
9112 return die_lhs->offset == die_rhs->offset;
9113}
9114
c906108c
SS
9115/* Read a whole compilation unit into a linked list of dies. */
9116
f9aca02d 9117static struct die_info *
93311388 9118read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9119{
93311388 9120 struct die_reader_specs reader_specs;
98bfdba5 9121 int read_abbrevs = 0;
1d9ec526 9122 struct cleanup *back_to = NULL;
98bfdba5
PA
9123 struct die_info *die;
9124
9125 if (cu->dwarf2_abbrevs == NULL)
9126 {
e5fe5e75 9127 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9128 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9129 read_abbrevs = 1;
9130 }
93311388 9131
348e048f 9132 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9133 cu->die_hash
9134 = htab_create_alloc_ex (cu->header.length / 12,
9135 die_hash,
9136 die_eq,
9137 NULL,
9138 &cu->comp_unit_obstack,
9139 hashtab_obstack_allocate,
9140 dummy_obstack_deallocate);
9141
93311388
DE
9142 init_cu_die_reader (&reader_specs, cu);
9143
98bfdba5
PA
9144 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9145
9146 if (read_abbrevs)
9147 do_cleanups (back_to);
9148
9149 return die;
639d11d3
DC
9150}
9151
d97bc12b
DE
9152/* Main entry point for reading a DIE and all children.
9153 Read the DIE and dump it if requested. */
9154
9155static struct die_info *
93311388
DE
9156read_die_and_children (const struct die_reader_specs *reader,
9157 gdb_byte *info_ptr,
d97bc12b
DE
9158 gdb_byte **new_info_ptr,
9159 struct die_info *parent)
9160{
93311388 9161 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9162 new_info_ptr, parent);
9163
9164 if (dwarf2_die_debug)
9165 {
348e048f
DE
9166 fprintf_unfiltered (gdb_stdlog,
9167 "\nRead die from %s of %s:\n",
b0df02fd 9168 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9169 ? ".debug_types"
9170 : ".debug_info"),
348e048f 9171 reader->abfd->filename);
d97bc12b
DE
9172 dump_die (result, dwarf2_die_debug);
9173 }
9174
9175 return result;
9176}
9177
639d11d3
DC
9178/* Read a single die and all its descendents. Set the die's sibling
9179 field to NULL; set other fields in the die correctly, and set all
9180 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9181 location of the info_ptr after reading all of those dies. PARENT
9182 is the parent of the die in question. */
9183
9184static struct die_info *
93311388
DE
9185read_die_and_children_1 (const struct die_reader_specs *reader,
9186 gdb_byte *info_ptr,
d97bc12b
DE
9187 gdb_byte **new_info_ptr,
9188 struct die_info *parent)
639d11d3
DC
9189{
9190 struct die_info *die;
fe1b8b76 9191 gdb_byte *cur_ptr;
639d11d3
DC
9192 int has_children;
9193
93311388 9194 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9195 if (die == NULL)
9196 {
9197 *new_info_ptr = cur_ptr;
9198 return NULL;
9199 }
93311388 9200 store_in_ref_table (die, reader->cu);
639d11d3
DC
9201
9202 if (has_children)
348e048f 9203 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9204 else
9205 {
9206 die->child = NULL;
9207 *new_info_ptr = cur_ptr;
9208 }
9209
9210 die->sibling = NULL;
9211 die->parent = parent;
9212 return die;
9213}
9214
9215/* Read a die, all of its descendents, and all of its siblings; set
9216 all of the fields of all of the dies correctly. Arguments are as
9217 in read_die_and_children. */
9218
9219static struct die_info *
93311388
DE
9220read_die_and_siblings (const struct die_reader_specs *reader,
9221 gdb_byte *info_ptr,
fe1b8b76 9222 gdb_byte **new_info_ptr,
639d11d3
DC
9223 struct die_info *parent)
9224{
9225 struct die_info *first_die, *last_sibling;
fe1b8b76 9226 gdb_byte *cur_ptr;
639d11d3 9227
c906108c 9228 cur_ptr = info_ptr;
639d11d3
DC
9229 first_die = last_sibling = NULL;
9230
9231 while (1)
c906108c 9232 {
639d11d3 9233 struct die_info *die
93311388 9234 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9235
1d325ec1 9236 if (die == NULL)
c906108c 9237 {
639d11d3
DC
9238 *new_info_ptr = cur_ptr;
9239 return first_die;
c906108c 9240 }
1d325ec1
DJ
9241
9242 if (!first_die)
9243 first_die = die;
c906108c 9244 else
1d325ec1
DJ
9245 last_sibling->sibling = die;
9246
9247 last_sibling = die;
c906108c 9248 }
c906108c
SS
9249}
9250
93311388
DE
9251/* Read the die from the .debug_info section buffer. Set DIEP to
9252 point to a newly allocated die with its information, except for its
9253 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9254 whether the die has children or not. */
9255
9256static gdb_byte *
9257read_full_die (const struct die_reader_specs *reader,
9258 struct die_info **diep, gdb_byte *info_ptr,
9259 int *has_children)
9260{
9261 unsigned int abbrev_number, bytes_read, i, offset;
9262 struct abbrev_info *abbrev;
9263 struct die_info *die;
9264 struct dwarf2_cu *cu = reader->cu;
9265 bfd *abfd = reader->abfd;
9266
9267 offset = info_ptr - reader->buffer;
9268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9269 info_ptr += bytes_read;
9270 if (!abbrev_number)
9271 {
9272 *diep = NULL;
9273 *has_children = 0;
9274 return info_ptr;
9275 }
9276
9277 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9278 if (!abbrev)
348e048f
DE
9279 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9280 abbrev_number,
9281 bfd_get_filename (abfd));
9282
93311388
DE
9283 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9284 die->offset = offset;
9285 die->tag = abbrev->tag;
9286 die->abbrev = abbrev_number;
9287
9288 die->num_attrs = abbrev->num_attrs;
9289
9290 for (i = 0; i < abbrev->num_attrs; ++i)
9291 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9292 abfd, info_ptr, cu);
9293
9294 *diep = die;
9295 *has_children = abbrev->has_children;
9296 return info_ptr;
9297}
9298
c906108c
SS
9299/* In DWARF version 2, the description of the debugging information is
9300 stored in a separate .debug_abbrev section. Before we read any
9301 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9302 in a hash table. This function also sets flags in CU describing
9303 the data found in the abbrev table. */
c906108c
SS
9304
9305static void
e5fe5e75 9306dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9307{
e5fe5e75 9308 bfd *abfd = cu->objfile->obfd;
e7c27a73 9309 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9310 gdb_byte *abbrev_ptr;
c906108c
SS
9311 struct abbrev_info *cur_abbrev;
9312 unsigned int abbrev_number, bytes_read, abbrev_name;
9313 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9314 struct attr_abbrev *cur_attrs;
9315 unsigned int allocated_attrs;
c906108c 9316
0963b4bd 9317 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9318 obstack_init (&cu->abbrev_obstack);
9319 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9320 (ABBREV_HASH_SIZE
9321 * sizeof (struct abbrev_info *)));
9322 memset (cu->dwarf2_abbrevs, 0,
9323 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9324
be391dca
TT
9325 dwarf2_read_section (dwarf2_per_objfile->objfile,
9326 &dwarf2_per_objfile->abbrev);
dce234bc 9327 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9328 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9329 abbrev_ptr += bytes_read;
9330
f3dd6933
DJ
9331 allocated_attrs = ATTR_ALLOC_CHUNK;
9332 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9333
0963b4bd 9334 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9335 while (abbrev_number)
9336 {
f3dd6933 9337 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9338
9339 /* read in abbrev header */
9340 cur_abbrev->number = abbrev_number;
9341 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9342 abbrev_ptr += bytes_read;
9343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9344 abbrev_ptr += 1;
9345
9346 /* now read in declarations */
9347 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9348 abbrev_ptr += bytes_read;
9349 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9350 abbrev_ptr += bytes_read;
9351 while (abbrev_name)
9352 {
f3dd6933 9353 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9354 {
f3dd6933
DJ
9355 allocated_attrs += ATTR_ALLOC_CHUNK;
9356 cur_attrs
9357 = xrealloc (cur_attrs, (allocated_attrs
9358 * sizeof (struct attr_abbrev)));
c906108c 9359 }
ae038cb0 9360
f3dd6933
DJ
9361 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9362 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9363 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9364 abbrev_ptr += bytes_read;
9365 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9366 abbrev_ptr += bytes_read;
9367 }
9368
f3dd6933
DJ
9369 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9370 (cur_abbrev->num_attrs
9371 * sizeof (struct attr_abbrev)));
9372 memcpy (cur_abbrev->attrs, cur_attrs,
9373 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9374
c906108c 9375 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9376 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9377 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9378
9379 /* Get next abbreviation.
9380 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9381 always properly terminated with an abbrev number of 0.
9382 Exit loop if we encounter an abbreviation which we have
9383 already read (which means we are about to read the abbreviations
9384 for the next compile unit) or if the end of the abbreviation
9385 table is reached. */
dce234bc
PP
9386 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9387 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9388 break;
9389 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9390 abbrev_ptr += bytes_read;
e7c27a73 9391 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9392 break;
9393 }
f3dd6933
DJ
9394
9395 xfree (cur_attrs);
c906108c
SS
9396}
9397
f3dd6933 9398/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9399
c906108c 9400static void
f3dd6933 9401dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9402{
f3dd6933 9403 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9404
f3dd6933
DJ
9405 obstack_free (&cu->abbrev_obstack, NULL);
9406 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9407}
9408
9409/* Lookup an abbrev_info structure in the abbrev hash table. */
9410
9411static struct abbrev_info *
e7c27a73 9412dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9413{
9414 unsigned int hash_number;
9415 struct abbrev_info *abbrev;
9416
9417 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9418 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9419
9420 while (abbrev)
9421 {
9422 if (abbrev->number == number)
9423 return abbrev;
9424 else
9425 abbrev = abbrev->next;
9426 }
9427 return NULL;
9428}
9429
72bf9492
DJ
9430/* Returns nonzero if TAG represents a type that we might generate a partial
9431 symbol for. */
9432
9433static int
9434is_type_tag_for_partial (int tag)
9435{
9436 switch (tag)
9437 {
9438#if 0
9439 /* Some types that would be reasonable to generate partial symbols for,
9440 that we don't at present. */
9441 case DW_TAG_array_type:
9442 case DW_TAG_file_type:
9443 case DW_TAG_ptr_to_member_type:
9444 case DW_TAG_set_type:
9445 case DW_TAG_string_type:
9446 case DW_TAG_subroutine_type:
9447#endif
9448 case DW_TAG_base_type:
9449 case DW_TAG_class_type:
680b30c7 9450 case DW_TAG_interface_type:
72bf9492
DJ
9451 case DW_TAG_enumeration_type:
9452 case DW_TAG_structure_type:
9453 case DW_TAG_subrange_type:
9454 case DW_TAG_typedef:
9455 case DW_TAG_union_type:
9456 return 1;
9457 default:
9458 return 0;
9459 }
9460}
9461
9462/* Load all DIEs that are interesting for partial symbols into memory. */
9463
9464static struct partial_die_info *
93311388
DE
9465load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9466 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9467{
bb5ed363 9468 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9469 struct partial_die_info *part_die;
9470 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9471 struct abbrev_info *abbrev;
9472 unsigned int bytes_read;
5afb4e99 9473 unsigned int load_all = 0;
72bf9492
DJ
9474
9475 int nesting_level = 1;
9476
9477 parent_die = NULL;
9478 last_die = NULL;
9479
5afb4e99
DJ
9480 if (cu->per_cu && cu->per_cu->load_all_dies)
9481 load_all = 1;
9482
72bf9492
DJ
9483 cu->partial_dies
9484 = htab_create_alloc_ex (cu->header.length / 12,
9485 partial_die_hash,
9486 partial_die_eq,
9487 NULL,
9488 &cu->comp_unit_obstack,
9489 hashtab_obstack_allocate,
9490 dummy_obstack_deallocate);
9491
9492 part_die = obstack_alloc (&cu->comp_unit_obstack,
9493 sizeof (struct partial_die_info));
9494
9495 while (1)
9496 {
9497 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9498
9499 /* A NULL abbrev means the end of a series of children. */
9500 if (abbrev == NULL)
9501 {
9502 if (--nesting_level == 0)
9503 {
9504 /* PART_DIE was probably the last thing allocated on the
9505 comp_unit_obstack, so we could call obstack_free
9506 here. We don't do that because the waste is small,
9507 and will be cleaned up when we're done with this
9508 compilation unit. This way, we're also more robust
9509 against other users of the comp_unit_obstack. */
9510 return first_die;
9511 }
9512 info_ptr += bytes_read;
9513 last_die = parent_die;
9514 parent_die = parent_die->die_parent;
9515 continue;
9516 }
9517
98bfdba5
PA
9518 /* Check for template arguments. We never save these; if
9519 they're seen, we just mark the parent, and go on our way. */
9520 if (parent_die != NULL
9521 && cu->language == language_cplus
9522 && (abbrev->tag == DW_TAG_template_type_param
9523 || abbrev->tag == DW_TAG_template_value_param))
9524 {
9525 parent_die->has_template_arguments = 1;
9526
9527 if (!load_all)
9528 {
9529 /* We don't need a partial DIE for the template argument. */
9530 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9531 cu);
9532 continue;
9533 }
9534 }
9535
9536 /* We only recurse into subprograms looking for template arguments.
9537 Skip their other children. */
9538 if (!load_all
9539 && cu->language == language_cplus
9540 && parent_die != NULL
9541 && parent_die->tag == DW_TAG_subprogram)
9542 {
9543 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9544 continue;
9545 }
9546
5afb4e99
DJ
9547 /* Check whether this DIE is interesting enough to save. Normally
9548 we would not be interested in members here, but there may be
9549 later variables referencing them via DW_AT_specification (for
9550 static members). */
9551 if (!load_all
9552 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9553 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9554 && abbrev->tag != DW_TAG_enumerator
9555 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9556 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9557 && abbrev->tag != DW_TAG_variable
5afb4e99 9558 && abbrev->tag != DW_TAG_namespace
f55ee35c 9559 && abbrev->tag != DW_TAG_module
5afb4e99 9560 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9561 {
9562 /* Otherwise we skip to the next sibling, if any. */
93311388 9563 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9564 continue;
9565 }
9566
93311388
DE
9567 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9568 buffer, info_ptr, cu);
72bf9492
DJ
9569
9570 /* This two-pass algorithm for processing partial symbols has a
9571 high cost in cache pressure. Thus, handle some simple cases
9572 here which cover the majority of C partial symbols. DIEs
9573 which neither have specification tags in them, nor could have
9574 specification tags elsewhere pointing at them, can simply be
9575 processed and discarded.
9576
9577 This segment is also optional; scan_partial_symbols and
9578 add_partial_symbol will handle these DIEs if we chain
9579 them in normally. When compilers which do not emit large
9580 quantities of duplicate debug information are more common,
9581 this code can probably be removed. */
9582
9583 /* Any complete simple types at the top level (pretty much all
9584 of them, for a language without namespaces), can be processed
9585 directly. */
9586 if (parent_die == NULL
9587 && part_die->has_specification == 0
9588 && part_die->is_declaration == 0
d8228535 9589 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9590 || part_die->tag == DW_TAG_base_type
9591 || part_die->tag == DW_TAG_subrange_type))
9592 {
9593 if (building_psymtab && part_die->name != NULL)
04a679b8 9594 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9595 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9596 &objfile->static_psymbols,
9597 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9598 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9599 continue;
9600 }
9601
d8228535
JK
9602 /* The exception for DW_TAG_typedef with has_children above is
9603 a workaround of GCC PR debug/47510. In the case of this complaint
9604 type_name_no_tag_or_error will error on such types later.
9605
9606 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9607 it could not find the child DIEs referenced later, this is checked
9608 above. In correct DWARF DW_TAG_typedef should have no children. */
9609
9610 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9611 complaint (&symfile_complaints,
9612 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9613 "- DIE at 0x%x [in module %s]"),
bb5ed363 9614 part_die->offset, objfile->name);
d8228535 9615
72bf9492
DJ
9616 /* If we're at the second level, and we're an enumerator, and
9617 our parent has no specification (meaning possibly lives in a
9618 namespace elsewhere), then we can add the partial symbol now
9619 instead of queueing it. */
9620 if (part_die->tag == DW_TAG_enumerator
9621 && parent_die != NULL
9622 && parent_die->die_parent == NULL
9623 && parent_die->tag == DW_TAG_enumeration_type
9624 && parent_die->has_specification == 0)
9625 {
9626 if (part_die->name == NULL)
3e43a32a
MS
9627 complaint (&symfile_complaints,
9628 _("malformed enumerator DIE ignored"));
72bf9492 9629 else if (building_psymtab)
04a679b8 9630 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9631 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9632 (cu->language == language_cplus
9633 || cu->language == language_java)
bb5ed363
DE
9634 ? &objfile->global_psymbols
9635 : &objfile->static_psymbols,
9636 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9637
93311388 9638 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9639 continue;
9640 }
9641
9642 /* We'll save this DIE so link it in. */
9643 part_die->die_parent = parent_die;
9644 part_die->die_sibling = NULL;
9645 part_die->die_child = NULL;
9646
9647 if (last_die && last_die == parent_die)
9648 last_die->die_child = part_die;
9649 else if (last_die)
9650 last_die->die_sibling = part_die;
9651
9652 last_die = part_die;
9653
9654 if (first_die == NULL)
9655 first_die = part_die;
9656
9657 /* Maybe add the DIE to the hash table. Not all DIEs that we
9658 find interesting need to be in the hash table, because we
9659 also have the parent/sibling/child chains; only those that we
9660 might refer to by offset later during partial symbol reading.
9661
9662 For now this means things that might have be the target of a
9663 DW_AT_specification, DW_AT_abstract_origin, or
9664 DW_AT_extension. DW_AT_extension will refer only to
9665 namespaces; DW_AT_abstract_origin refers to functions (and
9666 many things under the function DIE, but we do not recurse
9667 into function DIEs during partial symbol reading) and
9668 possibly variables as well; DW_AT_specification refers to
9669 declarations. Declarations ought to have the DW_AT_declaration
9670 flag. It happens that GCC forgets to put it in sometimes, but
9671 only for functions, not for types.
9672
9673 Adding more things than necessary to the hash table is harmless
9674 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9675 wasted time in find_partial_die, when we reread the compilation
9676 unit with load_all_dies set. */
72bf9492 9677
5afb4e99 9678 if (load_all
72929c62 9679 || abbrev->tag == DW_TAG_constant
5afb4e99 9680 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9681 || abbrev->tag == DW_TAG_variable
9682 || abbrev->tag == DW_TAG_namespace
9683 || part_die->is_declaration)
9684 {
9685 void **slot;
9686
9687 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9688 part_die->offset, INSERT);
9689 *slot = part_die;
9690 }
9691
9692 part_die = obstack_alloc (&cu->comp_unit_obstack,
9693 sizeof (struct partial_die_info));
9694
9695 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9696 we have no reason to follow the children of structures; for other
98bfdba5
PA
9697 languages we have to, so that we can get at method physnames
9698 to infer fully qualified class names, for DW_AT_specification,
9699 and for C++ template arguments. For C++, we also look one level
9700 inside functions to find template arguments (if the name of the
9701 function does not already contain the template arguments).
bc30ff58
JB
9702
9703 For Ada, we need to scan the children of subprograms and lexical
9704 blocks as well because Ada allows the definition of nested
9705 entities that could be interesting for the debugger, such as
9706 nested subprograms for instance. */
72bf9492 9707 if (last_die->has_children
5afb4e99
DJ
9708 && (load_all
9709 || last_die->tag == DW_TAG_namespace
f55ee35c 9710 || last_die->tag == DW_TAG_module
72bf9492 9711 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9712 || (cu->language == language_cplus
9713 && last_die->tag == DW_TAG_subprogram
9714 && (last_die->name == NULL
9715 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9716 || (cu->language != language_c
9717 && (last_die->tag == DW_TAG_class_type
680b30c7 9718 || last_die->tag == DW_TAG_interface_type
72bf9492 9719 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9720 || last_die->tag == DW_TAG_union_type))
9721 || (cu->language == language_ada
9722 && (last_die->tag == DW_TAG_subprogram
9723 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9724 {
9725 nesting_level++;
9726 parent_die = last_die;
9727 continue;
9728 }
9729
9730 /* Otherwise we skip to the next sibling, if any. */
93311388 9731 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9732
9733 /* Back to the top, do it again. */
9734 }
9735}
9736
c906108c
SS
9737/* Read a minimal amount of information into the minimal die structure. */
9738
fe1b8b76 9739static gdb_byte *
72bf9492
DJ
9740read_partial_die (struct partial_die_info *part_die,
9741 struct abbrev_info *abbrev,
9742 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9743 gdb_byte *buffer, gdb_byte *info_ptr,
9744 struct dwarf2_cu *cu)
c906108c 9745{
bb5ed363 9746 struct objfile *objfile = cu->objfile;
fa238c03 9747 unsigned int i;
c906108c 9748 struct attribute attr;
c5aa993b 9749 int has_low_pc_attr = 0;
c906108c
SS
9750 int has_high_pc_attr = 0;
9751
72bf9492 9752 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9753
93311388 9754 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9755
9756 info_ptr += abbrev_len;
9757
9758 if (abbrev == NULL)
9759 return info_ptr;
9760
c906108c
SS
9761 part_die->tag = abbrev->tag;
9762 part_die->has_children = abbrev->has_children;
c906108c
SS
9763
9764 for (i = 0; i < abbrev->num_attrs; ++i)
9765 {
e7c27a73 9766 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9767
9768 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9769 partial symbol table. */
c906108c
SS
9770 switch (attr.name)
9771 {
9772 case DW_AT_name:
71c25dea
TT
9773 switch (part_die->tag)
9774 {
9775 case DW_TAG_compile_unit:
348e048f 9776 case DW_TAG_type_unit:
71c25dea
TT
9777 /* Compilation units have a DW_AT_name that is a filename, not
9778 a source language identifier. */
9779 case DW_TAG_enumeration_type:
9780 case DW_TAG_enumerator:
9781 /* These tags always have simple identifiers already; no need
9782 to canonicalize them. */
9783 part_die->name = DW_STRING (&attr);
9784 break;
9785 default:
9786 part_die->name
9787 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9788 &objfile->objfile_obstack);
71c25dea
TT
9789 break;
9790 }
c906108c 9791 break;
31ef98ae 9792 case DW_AT_linkage_name:
c906108c 9793 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9794 /* Note that both forms of linkage name might appear. We
9795 assume they will be the same, and we only store the last
9796 one we see. */
94af9270
KS
9797 if (cu->language == language_ada)
9798 part_die->name = DW_STRING (&attr);
abc72ce4 9799 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9800 break;
9801 case DW_AT_low_pc:
9802 has_low_pc_attr = 1;
9803 part_die->lowpc = DW_ADDR (&attr);
9804 break;
9805 case DW_AT_high_pc:
9806 has_high_pc_attr = 1;
9807 part_die->highpc = DW_ADDR (&attr);
9808 break;
9809 case DW_AT_location:
0963b4bd 9810 /* Support the .debug_loc offsets. */
8e19ed76
PS
9811 if (attr_form_is_block (&attr))
9812 {
9813 part_die->locdesc = DW_BLOCK (&attr);
9814 }
3690dd37 9815 else if (attr_form_is_section_offset (&attr))
8e19ed76 9816 {
4d3c2250 9817 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9818 }
9819 else
9820 {
4d3c2250
KB
9821 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9822 "partial symbol information");
8e19ed76 9823 }
c906108c 9824 break;
c906108c
SS
9825 case DW_AT_external:
9826 part_die->is_external = DW_UNSND (&attr);
9827 break;
9828 case DW_AT_declaration:
9829 part_die->is_declaration = DW_UNSND (&attr);
9830 break;
9831 case DW_AT_type:
9832 part_die->has_type = 1;
9833 break;
9834 case DW_AT_abstract_origin:
9835 case DW_AT_specification:
72bf9492
DJ
9836 case DW_AT_extension:
9837 part_die->has_specification = 1;
c764a876 9838 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9839 break;
9840 case DW_AT_sibling:
9841 /* Ignore absolute siblings, they might point outside of
9842 the current compile unit. */
9843 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9844 complaint (&symfile_complaints,
9845 _("ignoring absolute DW_AT_sibling"));
c906108c 9846 else
93311388 9847 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9848 break;
fa4028e9
JB
9849 case DW_AT_byte_size:
9850 part_die->has_byte_size = 1;
9851 break;
68511cec
CES
9852 case DW_AT_calling_convention:
9853 /* DWARF doesn't provide a way to identify a program's source-level
9854 entry point. DW_AT_calling_convention attributes are only meant
9855 to describe functions' calling conventions.
9856
9857 However, because it's a necessary piece of information in
9858 Fortran, and because DW_CC_program is the only piece of debugging
9859 information whose definition refers to a 'main program' at all,
9860 several compilers have begun marking Fortran main programs with
9861 DW_CC_program --- even when those functions use the standard
9862 calling conventions.
9863
9864 So until DWARF specifies a way to provide this information and
9865 compilers pick up the new representation, we'll support this
9866 practice. */
9867 if (DW_UNSND (&attr) == DW_CC_program
9868 && cu->language == language_fortran)
01f8c46d
JK
9869 {
9870 set_main_name (part_die->name);
9871
9872 /* As this DIE has a static linkage the name would be difficult
9873 to look up later. */
9874 language_of_main = language_fortran;
9875 }
68511cec 9876 break;
c906108c
SS
9877 default:
9878 break;
9879 }
9880 }
9881
9373cf26
JK
9882 if (has_low_pc_attr && has_high_pc_attr)
9883 {
9884 /* When using the GNU linker, .gnu.linkonce. sections are used to
9885 eliminate duplicate copies of functions and vtables and such.
9886 The linker will arbitrarily choose one and discard the others.
9887 The AT_*_pc values for such functions refer to local labels in
9888 these sections. If the section from that file was discarded, the
9889 labels are not in the output, so the relocs get a value of 0.
9890 If this is a discarded function, mark the pc bounds as invalid,
9891 so that GDB will ignore it. */
9892 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9893 {
bb5ed363 9894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9895
9896 complaint (&symfile_complaints,
9897 _("DW_AT_low_pc %s is zero "
9898 "for DIE at 0x%x [in module %s]"),
9899 paddress (gdbarch, part_die->lowpc),
bb5ed363 9900 part_die->offset, objfile->name);
9373cf26
JK
9901 }
9902 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9903 else if (part_die->lowpc >= part_die->highpc)
9904 {
bb5ed363 9905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9906
9907 complaint (&symfile_complaints,
9908 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9909 "for DIE at 0x%x [in module %s]"),
9910 paddress (gdbarch, part_die->lowpc),
9911 paddress (gdbarch, part_die->highpc),
bb5ed363 9912 part_die->offset, objfile->name);
9373cf26
JK
9913 }
9914 else
9915 part_die->has_pc_info = 1;
9916 }
85cbf3d3 9917
c906108c
SS
9918 return info_ptr;
9919}
9920
72bf9492
DJ
9921/* Find a cached partial DIE at OFFSET in CU. */
9922
9923static struct partial_die_info *
c764a876 9924find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9925{
9926 struct partial_die_info *lookup_die = NULL;
9927 struct partial_die_info part_die;
9928
9929 part_die.offset = offset;
9930 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9931
72bf9492
DJ
9932 return lookup_die;
9933}
9934
348e048f
DE
9935/* Find a partial DIE at OFFSET, which may or may not be in CU,
9936 except in the case of .debug_types DIEs which do not reference
9937 outside their CU (they do however referencing other types via
55f1336d 9938 DW_FORM_ref_sig8). */
72bf9492
DJ
9939
9940static struct partial_die_info *
c764a876 9941find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9942{
bb5ed363 9943 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9944 struct dwarf2_per_cu_data *per_cu = NULL;
9945 struct partial_die_info *pd = NULL;
72bf9492 9946
b0df02fd 9947 if (cu->per_cu->debug_types_section)
348e048f
DE
9948 {
9949 pd = find_partial_die_in_comp_unit (offset, cu);
9950 if (pd != NULL)
9951 return pd;
9952 goto not_found;
9953 }
9954
45452591 9955 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9956 {
9957 pd = find_partial_die_in_comp_unit (offset, cu);
9958 if (pd != NULL)
9959 return pd;
9960 }
72bf9492 9961
bb5ed363 9962 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 9963
98bfdba5 9964 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 9965 load_partial_comp_unit (per_cu);
ae038cb0
DJ
9966
9967 per_cu->cu->last_used = 0;
5afb4e99
DJ
9968 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9969
9970 if (pd == NULL && per_cu->load_all_dies == 0)
9971 {
9972 struct cleanup *back_to;
9973 struct partial_die_info comp_unit_die;
9974 struct abbrev_info *abbrev;
9975 unsigned int bytes_read;
9976 char *info_ptr;
9977
9978 per_cu->load_all_dies = 1;
9979
9980 /* Re-read the DIEs. */
9981 back_to = make_cleanup (null_cleanup, 0);
9982 if (per_cu->cu->dwarf2_abbrevs == NULL)
9983 {
e5fe5e75 9984 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 9985 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9986 }
dce234bc 9987 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9988 + per_cu->cu->header.offset
9989 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9990 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9991 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 9992 objfile->obfd,
93311388 9993 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9994 per_cu->cu);
9995 if (comp_unit_die.has_children)
bb5ed363 9996 load_partial_dies (objfile->obfd,
93311388
DE
9997 dwarf2_per_objfile->info.buffer, info_ptr,
9998 0, per_cu->cu);
5afb4e99
DJ
9999 do_cleanups (back_to);
10000
10001 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10002 }
10003
348e048f
DE
10004 not_found:
10005
5afb4e99
DJ
10006 if (pd == NULL)
10007 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10008 _("could not find partial DIE 0x%x "
10009 "in cache [from module %s]\n"),
bb5ed363 10010 offset, bfd_get_filename (objfile->obfd));
5afb4e99 10011 return pd;
72bf9492
DJ
10012}
10013
abc72ce4
DE
10014/* See if we can figure out if the class lives in a namespace. We do
10015 this by looking for a member function; its demangled name will
10016 contain namespace info, if there is any. */
10017
10018static void
10019guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10020 struct dwarf2_cu *cu)
10021{
10022 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10023 what template types look like, because the demangler
10024 frequently doesn't give the same name as the debug info. We
10025 could fix this by only using the demangled name to get the
10026 prefix (but see comment in read_structure_type). */
10027
10028 struct partial_die_info *real_pdi;
10029 struct partial_die_info *child_pdi;
10030
10031 /* If this DIE (this DIE's specification, if any) has a parent, then
10032 we should not do this. We'll prepend the parent's fully qualified
10033 name when we create the partial symbol. */
10034
10035 real_pdi = struct_pdi;
10036 while (real_pdi->has_specification)
10037 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10038
10039 if (real_pdi->die_parent != NULL)
10040 return;
10041
10042 for (child_pdi = struct_pdi->die_child;
10043 child_pdi != NULL;
10044 child_pdi = child_pdi->die_sibling)
10045 {
10046 if (child_pdi->tag == DW_TAG_subprogram
10047 && child_pdi->linkage_name != NULL)
10048 {
10049 char *actual_class_name
10050 = language_class_name_from_physname (cu->language_defn,
10051 child_pdi->linkage_name);
10052 if (actual_class_name != NULL)
10053 {
10054 struct_pdi->name
10055 = obsavestring (actual_class_name,
10056 strlen (actual_class_name),
10057 &cu->objfile->objfile_obstack);
10058 xfree (actual_class_name);
10059 }
10060 break;
10061 }
10062 }
10063}
10064
72bf9492
DJ
10065/* Adjust PART_DIE before generating a symbol for it. This function
10066 may set the is_external flag or change the DIE's name. */
10067
10068static void
10069fixup_partial_die (struct partial_die_info *part_die,
10070 struct dwarf2_cu *cu)
10071{
abc72ce4
DE
10072 /* Once we've fixed up a die, there's no point in doing so again.
10073 This also avoids a memory leak if we were to call
10074 guess_partial_die_structure_name multiple times. */
10075 if (part_die->fixup_called)
10076 return;
10077
72bf9492
DJ
10078 /* If we found a reference attribute and the DIE has no name, try
10079 to find a name in the referred to DIE. */
10080
10081 if (part_die->name == NULL && part_die->has_specification)
10082 {
10083 struct partial_die_info *spec_die;
72bf9492 10084
10b3939b 10085 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10086
10b3939b 10087 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10088
10089 if (spec_die->name)
10090 {
10091 part_die->name = spec_die->name;
10092
10093 /* Copy DW_AT_external attribute if it is set. */
10094 if (spec_die->is_external)
10095 part_die->is_external = spec_die->is_external;
10096 }
10097 }
10098
10099 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10100
10101 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10102 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10103
abc72ce4
DE
10104 /* If there is no parent die to provide a namespace, and there are
10105 children, see if we can determine the namespace from their linkage
122d1940 10106 name. */
abc72ce4 10107 if (cu->language == language_cplus
8b70b953 10108 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10109 && part_die->die_parent == NULL
10110 && part_die->has_children
10111 && (part_die->tag == DW_TAG_class_type
10112 || part_die->tag == DW_TAG_structure_type
10113 || part_die->tag == DW_TAG_union_type))
10114 guess_partial_die_structure_name (part_die, cu);
10115
53832f31
TT
10116 /* GCC might emit a nameless struct or union that has a linkage
10117 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10118 if (part_die->name == NULL
96408a79
SA
10119 && (part_die->tag == DW_TAG_class_type
10120 || part_die->tag == DW_TAG_interface_type
10121 || part_die->tag == DW_TAG_structure_type
10122 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10123 && part_die->linkage_name != NULL)
10124 {
10125 char *demangled;
10126
10127 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10128 if (demangled)
10129 {
96408a79
SA
10130 const char *base;
10131
10132 /* Strip any leading namespaces/classes, keep only the base name.
10133 DW_AT_name for named DIEs does not contain the prefixes. */
10134 base = strrchr (demangled, ':');
10135 if (base && base > demangled && base[-1] == ':')
10136 base++;
10137 else
10138 base = demangled;
10139
10140 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10141 &cu->objfile->objfile_obstack);
10142 xfree (demangled);
10143 }
10144 }
10145
abc72ce4 10146 part_die->fixup_called = 1;
72bf9492
DJ
10147}
10148
a8329558 10149/* Read an attribute value described by an attribute form. */
c906108c 10150
fe1b8b76 10151static gdb_byte *
a8329558 10152read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10153 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10154 struct dwarf2_cu *cu)
c906108c 10155{
e7c27a73 10156 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10157 unsigned int bytes_read;
10158 struct dwarf_block *blk;
10159
a8329558
KW
10160 attr->form = form;
10161 switch (form)
c906108c 10162 {
c906108c 10163 case DW_FORM_ref_addr:
ae411497
TT
10164 if (cu->header.version == 2)
10165 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10166 else
3e43a32a
MS
10167 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10168 &cu->header, &bytes_read);
ae411497
TT
10169 info_ptr += bytes_read;
10170 break;
10171 case DW_FORM_addr:
e7c27a73 10172 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10173 info_ptr += bytes_read;
c906108c
SS
10174 break;
10175 case DW_FORM_block2:
7b5a2f43 10176 blk = dwarf_alloc_block (cu);
c906108c
SS
10177 blk->size = read_2_bytes (abfd, info_ptr);
10178 info_ptr += 2;
10179 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10180 info_ptr += blk->size;
10181 DW_BLOCK (attr) = blk;
10182 break;
10183 case DW_FORM_block4:
7b5a2f43 10184 blk = dwarf_alloc_block (cu);
c906108c
SS
10185 blk->size = read_4_bytes (abfd, info_ptr);
10186 info_ptr += 4;
10187 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10188 info_ptr += blk->size;
10189 DW_BLOCK (attr) = blk;
10190 break;
10191 case DW_FORM_data2:
10192 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10193 info_ptr += 2;
10194 break;
10195 case DW_FORM_data4:
10196 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10197 info_ptr += 4;
10198 break;
10199 case DW_FORM_data8:
10200 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10201 info_ptr += 8;
10202 break;
2dc7f7b3
TT
10203 case DW_FORM_sec_offset:
10204 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10205 info_ptr += bytes_read;
10206 break;
c906108c 10207 case DW_FORM_string:
9b1c24c8 10208 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10209 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10210 info_ptr += bytes_read;
10211 break;
4bdf3d34
JJ
10212 case DW_FORM_strp:
10213 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10214 &bytes_read);
8285870a 10215 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10216 info_ptr += bytes_read;
10217 break;
2dc7f7b3 10218 case DW_FORM_exprloc:
c906108c 10219 case DW_FORM_block:
7b5a2f43 10220 blk = dwarf_alloc_block (cu);
c906108c
SS
10221 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10222 info_ptr += bytes_read;
10223 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10224 info_ptr += blk->size;
10225 DW_BLOCK (attr) = blk;
10226 break;
10227 case DW_FORM_block1:
7b5a2f43 10228 blk = dwarf_alloc_block (cu);
c906108c
SS
10229 blk->size = read_1_byte (abfd, info_ptr);
10230 info_ptr += 1;
10231 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10232 info_ptr += blk->size;
10233 DW_BLOCK (attr) = blk;
10234 break;
10235 case DW_FORM_data1:
10236 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10237 info_ptr += 1;
10238 break;
10239 case DW_FORM_flag:
10240 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10241 info_ptr += 1;
10242 break;
2dc7f7b3
TT
10243 case DW_FORM_flag_present:
10244 DW_UNSND (attr) = 1;
10245 break;
c906108c
SS
10246 case DW_FORM_sdata:
10247 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10248 info_ptr += bytes_read;
10249 break;
10250 case DW_FORM_udata:
10251 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10252 info_ptr += bytes_read;
10253 break;
10254 case DW_FORM_ref1:
10b3939b 10255 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10256 info_ptr += 1;
10257 break;
10258 case DW_FORM_ref2:
10b3939b 10259 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10260 info_ptr += 2;
10261 break;
10262 case DW_FORM_ref4:
10b3939b 10263 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10264 info_ptr += 4;
10265 break;
613e1657 10266 case DW_FORM_ref8:
10b3939b 10267 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10268 info_ptr += 8;
10269 break;
55f1336d 10270 case DW_FORM_ref_sig8:
348e048f
DE
10271 /* Convert the signature to something we can record in DW_UNSND
10272 for later lookup.
10273 NOTE: This is NULL if the type wasn't found. */
10274 DW_SIGNATURED_TYPE (attr) =
10275 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10276 info_ptr += 8;
10277 break;
c906108c 10278 case DW_FORM_ref_udata:
10b3939b
DJ
10279 DW_ADDR (attr) = (cu->header.offset
10280 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10281 info_ptr += bytes_read;
10282 break;
c906108c 10283 case DW_FORM_indirect:
a8329558
KW
10284 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10285 info_ptr += bytes_read;
e7c27a73 10286 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10287 break;
c906108c 10288 default:
8a3fe4f8 10289 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10290 dwarf_form_name (form),
10291 bfd_get_filename (abfd));
c906108c 10292 }
28e94949
JB
10293
10294 /* We have seen instances where the compiler tried to emit a byte
10295 size attribute of -1 which ended up being encoded as an unsigned
10296 0xffffffff. Although 0xffffffff is technically a valid size value,
10297 an object of this size seems pretty unlikely so we can relatively
10298 safely treat these cases as if the size attribute was invalid and
10299 treat them as zero by default. */
10300 if (attr->name == DW_AT_byte_size
10301 && form == DW_FORM_data4
10302 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10303 {
10304 complaint
10305 (&symfile_complaints,
43bbcdc2
PH
10306 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10307 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10308 DW_UNSND (attr) = 0;
10309 }
28e94949 10310
c906108c
SS
10311 return info_ptr;
10312}
10313
a8329558
KW
10314/* Read an attribute described by an abbreviated attribute. */
10315
fe1b8b76 10316static gdb_byte *
a8329558 10317read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10318 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10319{
10320 attr->name = abbrev->name;
e7c27a73 10321 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10322}
10323
0963b4bd 10324/* Read dwarf information from a buffer. */
c906108c
SS
10325
10326static unsigned int
fe1b8b76 10327read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10328{
fe1b8b76 10329 return bfd_get_8 (abfd, buf);
c906108c
SS
10330}
10331
10332static int
fe1b8b76 10333read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10334{
fe1b8b76 10335 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10336}
10337
10338static unsigned int
fe1b8b76 10339read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10340{
fe1b8b76 10341 return bfd_get_16 (abfd, buf);
c906108c
SS
10342}
10343
21ae7a4d
JK
10344static int
10345read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10346{
10347 return bfd_get_signed_16 (abfd, buf);
10348}
10349
c906108c 10350static unsigned int
fe1b8b76 10351read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10352{
fe1b8b76 10353 return bfd_get_32 (abfd, buf);
c906108c
SS
10354}
10355
21ae7a4d
JK
10356static int
10357read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10358{
10359 return bfd_get_signed_32 (abfd, buf);
10360}
10361
93311388 10362static ULONGEST
fe1b8b76 10363read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10364{
fe1b8b76 10365 return bfd_get_64 (abfd, buf);
c906108c
SS
10366}
10367
10368static CORE_ADDR
fe1b8b76 10369read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10370 unsigned int *bytes_read)
c906108c 10371{
e7c27a73 10372 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10373 CORE_ADDR retval = 0;
10374
107d2387 10375 if (cu_header->signed_addr_p)
c906108c 10376 {
107d2387
AC
10377 switch (cu_header->addr_size)
10378 {
10379 case 2:
fe1b8b76 10380 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10381 break;
10382 case 4:
fe1b8b76 10383 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10384 break;
10385 case 8:
fe1b8b76 10386 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10387 break;
10388 default:
8e65ff28 10389 internal_error (__FILE__, __LINE__,
e2e0b3e5 10390 _("read_address: bad switch, signed [in module %s]"),
659b0389 10391 bfd_get_filename (abfd));
107d2387
AC
10392 }
10393 }
10394 else
10395 {
10396 switch (cu_header->addr_size)
10397 {
10398 case 2:
fe1b8b76 10399 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10400 break;
10401 case 4:
fe1b8b76 10402 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10403 break;
10404 case 8:
fe1b8b76 10405 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10406 break;
10407 default:
8e65ff28 10408 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10409 _("read_address: bad switch, "
10410 "unsigned [in module %s]"),
659b0389 10411 bfd_get_filename (abfd));
107d2387 10412 }
c906108c 10413 }
64367e0a 10414
107d2387
AC
10415 *bytes_read = cu_header->addr_size;
10416 return retval;
c906108c
SS
10417}
10418
f7ef9339 10419/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10420 specification allows the initial length to take up either 4 bytes
10421 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10422 bytes describe the length and all offsets will be 8 bytes in length
10423 instead of 4.
10424
f7ef9339
KB
10425 An older, non-standard 64-bit format is also handled by this
10426 function. The older format in question stores the initial length
10427 as an 8-byte quantity without an escape value. Lengths greater
10428 than 2^32 aren't very common which means that the initial 4 bytes
10429 is almost always zero. Since a length value of zero doesn't make
10430 sense for the 32-bit format, this initial zero can be considered to
10431 be an escape value which indicates the presence of the older 64-bit
10432 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10433 greater than 4GB. If it becomes necessary to handle lengths
10434 somewhat larger than 4GB, we could allow other small values (such
10435 as the non-sensical values of 1, 2, and 3) to also be used as
10436 escape values indicating the presence of the old format.
f7ef9339 10437
917c78fc
MK
10438 The value returned via bytes_read should be used to increment the
10439 relevant pointer after calling read_initial_length().
c764a876 10440
613e1657
KB
10441 [ Note: read_initial_length() and read_offset() are based on the
10442 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10443 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10444 from:
10445
f7ef9339 10446 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10447
613e1657
KB
10448 This document is only a draft and is subject to change. (So beware.)
10449
f7ef9339 10450 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10451 determined empirically by examining 64-bit ELF files produced by
10452 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10453
10454 - Kevin, July 16, 2002
613e1657
KB
10455 ] */
10456
10457static LONGEST
c764a876 10458read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10459{
fe1b8b76 10460 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10461
dd373385 10462 if (length == 0xffffffff)
613e1657 10463 {
fe1b8b76 10464 length = bfd_get_64 (abfd, buf + 4);
613e1657 10465 *bytes_read = 12;
613e1657 10466 }
dd373385 10467 else if (length == 0)
f7ef9339 10468 {
dd373385 10469 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10470 length = bfd_get_64 (abfd, buf);
f7ef9339 10471 *bytes_read = 8;
f7ef9339 10472 }
613e1657
KB
10473 else
10474 {
10475 *bytes_read = 4;
613e1657
KB
10476 }
10477
c764a876
DE
10478 return length;
10479}
dd373385 10480
c764a876
DE
10481/* Cover function for read_initial_length.
10482 Returns the length of the object at BUF, and stores the size of the
10483 initial length in *BYTES_READ and stores the size that offsets will be in
10484 *OFFSET_SIZE.
10485 If the initial length size is not equivalent to that specified in
10486 CU_HEADER then issue a complaint.
10487 This is useful when reading non-comp-unit headers. */
dd373385 10488
c764a876
DE
10489static LONGEST
10490read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10491 const struct comp_unit_head *cu_header,
10492 unsigned int *bytes_read,
10493 unsigned int *offset_size)
10494{
10495 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10496
10497 gdb_assert (cu_header->initial_length_size == 4
10498 || cu_header->initial_length_size == 8
10499 || cu_header->initial_length_size == 12);
10500
10501 if (cu_header->initial_length_size != *bytes_read)
10502 complaint (&symfile_complaints,
10503 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10504
c764a876 10505 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10506 return length;
613e1657
KB
10507}
10508
10509/* Read an offset from the data stream. The size of the offset is
917c78fc 10510 given by cu_header->offset_size. */
613e1657
KB
10511
10512static LONGEST
fe1b8b76 10513read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10514 unsigned int *bytes_read)
c764a876
DE
10515{
10516 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10517
c764a876
DE
10518 *bytes_read = cu_header->offset_size;
10519 return offset;
10520}
10521
10522/* Read an offset from the data stream. */
10523
10524static LONGEST
10525read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10526{
10527 LONGEST retval = 0;
10528
c764a876 10529 switch (offset_size)
613e1657
KB
10530 {
10531 case 4:
fe1b8b76 10532 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10533 break;
10534 case 8:
fe1b8b76 10535 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10536 break;
10537 default:
8e65ff28 10538 internal_error (__FILE__, __LINE__,
c764a876 10539 _("read_offset_1: bad switch [in module %s]"),
659b0389 10540 bfd_get_filename (abfd));
613e1657
KB
10541 }
10542
917c78fc 10543 return retval;
613e1657
KB
10544}
10545
fe1b8b76
JB
10546static gdb_byte *
10547read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10548{
10549 /* If the size of a host char is 8 bits, we can return a pointer
10550 to the buffer, otherwise we have to copy the data to a buffer
10551 allocated on the temporary obstack. */
4bdf3d34 10552 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10553 return buf;
c906108c
SS
10554}
10555
10556static char *
9b1c24c8 10557read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10558{
10559 /* If the size of a host char is 8 bits, we can return a pointer
10560 to the string, otherwise we have to copy the string to a buffer
10561 allocated on the temporary obstack. */
4bdf3d34 10562 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10563 if (*buf == '\0')
10564 {
10565 *bytes_read_ptr = 1;
10566 return NULL;
10567 }
fe1b8b76
JB
10568 *bytes_read_ptr = strlen ((char *) buf) + 1;
10569 return (char *) buf;
4bdf3d34
JJ
10570}
10571
10572static char *
cf2c3c16 10573read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10574{
be391dca 10575 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10576 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10577 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10578 bfd_get_filename (abfd));
dce234bc 10579 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10580 error (_("DW_FORM_strp pointing outside of "
10581 ".debug_str section [in module %s]"),
10582 bfd_get_filename (abfd));
4bdf3d34 10583 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10584 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10585 return NULL;
dce234bc 10586 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10587}
10588
cf2c3c16
TT
10589static char *
10590read_indirect_string (bfd *abfd, gdb_byte *buf,
10591 const struct comp_unit_head *cu_header,
10592 unsigned int *bytes_read_ptr)
10593{
10594 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10595
10596 return read_indirect_string_at_offset (abfd, str_offset);
10597}
10598
ce5d95e1 10599static unsigned long
fe1b8b76 10600read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10601{
ce5d95e1
JB
10602 unsigned long result;
10603 unsigned int num_read;
c906108c
SS
10604 int i, shift;
10605 unsigned char byte;
10606
10607 result = 0;
10608 shift = 0;
10609 num_read = 0;
10610 i = 0;
10611 while (1)
10612 {
fe1b8b76 10613 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10614 buf++;
10615 num_read++;
ce5d95e1 10616 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10617 if ((byte & 128) == 0)
10618 {
10619 break;
10620 }
10621 shift += 7;
10622 }
10623 *bytes_read_ptr = num_read;
10624 return result;
10625}
10626
ce5d95e1 10627static long
fe1b8b76 10628read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10629{
ce5d95e1 10630 long result;
77e0b926 10631 int i, shift, num_read;
c906108c
SS
10632 unsigned char byte;
10633
10634 result = 0;
10635 shift = 0;
c906108c
SS
10636 num_read = 0;
10637 i = 0;
10638 while (1)
10639 {
fe1b8b76 10640 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10641 buf++;
10642 num_read++;
ce5d95e1 10643 result |= ((long)(byte & 127) << shift);
c906108c
SS
10644 shift += 7;
10645 if ((byte & 128) == 0)
10646 {
10647 break;
10648 }
10649 }
77e0b926
DJ
10650 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10651 result |= -(((long)1) << shift);
c906108c
SS
10652 *bytes_read_ptr = num_read;
10653 return result;
10654}
10655
4bb7a0a7
DJ
10656/* Return a pointer to just past the end of an LEB128 number in BUF. */
10657
fe1b8b76
JB
10658static gdb_byte *
10659skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10660{
10661 int byte;
10662
10663 while (1)
10664 {
fe1b8b76 10665 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10666 buf++;
10667 if ((byte & 128) == 0)
10668 return buf;
10669 }
10670}
10671
c906108c 10672static void
e142c38c 10673set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10674{
10675 switch (lang)
10676 {
10677 case DW_LANG_C89:
76bee0cc 10678 case DW_LANG_C99:
c906108c 10679 case DW_LANG_C:
e142c38c 10680 cu->language = language_c;
c906108c
SS
10681 break;
10682 case DW_LANG_C_plus_plus:
e142c38c 10683 cu->language = language_cplus;
c906108c 10684 break;
6aecb9c2
JB
10685 case DW_LANG_D:
10686 cu->language = language_d;
10687 break;
c906108c
SS
10688 case DW_LANG_Fortran77:
10689 case DW_LANG_Fortran90:
b21b22e0 10690 case DW_LANG_Fortran95:
e142c38c 10691 cu->language = language_fortran;
c906108c
SS
10692 break;
10693 case DW_LANG_Mips_Assembler:
e142c38c 10694 cu->language = language_asm;
c906108c 10695 break;
bebd888e 10696 case DW_LANG_Java:
e142c38c 10697 cu->language = language_java;
bebd888e 10698 break;
c906108c 10699 case DW_LANG_Ada83:
8aaf0b47 10700 case DW_LANG_Ada95:
bc5f45f8
JB
10701 cu->language = language_ada;
10702 break;
72019c9c
GM
10703 case DW_LANG_Modula2:
10704 cu->language = language_m2;
10705 break;
fe8e67fd
PM
10706 case DW_LANG_Pascal83:
10707 cu->language = language_pascal;
10708 break;
22566fbd
DJ
10709 case DW_LANG_ObjC:
10710 cu->language = language_objc;
10711 break;
c906108c
SS
10712 case DW_LANG_Cobol74:
10713 case DW_LANG_Cobol85:
c906108c 10714 default:
e142c38c 10715 cu->language = language_minimal;
c906108c
SS
10716 break;
10717 }
e142c38c 10718 cu->language_defn = language_def (cu->language);
c906108c
SS
10719}
10720
10721/* Return the named attribute or NULL if not there. */
10722
10723static struct attribute *
e142c38c 10724dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 10725{
a48e046c 10726 for (;;)
c906108c 10727 {
a48e046c
TT
10728 unsigned int i;
10729 struct attribute *spec = NULL;
10730
10731 for (i = 0; i < die->num_attrs; ++i)
10732 {
10733 if (die->attrs[i].name == name)
10734 return &die->attrs[i];
10735 if (die->attrs[i].name == DW_AT_specification
10736 || die->attrs[i].name == DW_AT_abstract_origin)
10737 spec = &die->attrs[i];
10738 }
10739
10740 if (!spec)
10741 break;
c906108c 10742
f2f0e013 10743 die = follow_die_ref (die, spec, &cu);
f2f0e013 10744 }
c5aa993b 10745
c906108c
SS
10746 return NULL;
10747}
10748
348e048f
DE
10749/* Return the named attribute or NULL if not there,
10750 but do not follow DW_AT_specification, etc.
10751 This is for use in contexts where we're reading .debug_types dies.
10752 Following DW_AT_specification, DW_AT_abstract_origin will take us
10753 back up the chain, and we want to go down. */
10754
10755static struct attribute *
10756dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10757 struct dwarf2_cu *cu)
10758{
10759 unsigned int i;
10760
10761 for (i = 0; i < die->num_attrs; ++i)
10762 if (die->attrs[i].name == name)
10763 return &die->attrs[i];
10764
10765 return NULL;
10766}
10767
05cf31d1
JB
10768/* Return non-zero iff the attribute NAME is defined for the given DIE,
10769 and holds a non-zero value. This function should only be used for
2dc7f7b3 10770 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10771
10772static int
10773dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10774{
10775 struct attribute *attr = dwarf2_attr (die, name, cu);
10776
10777 return (attr && DW_UNSND (attr));
10778}
10779
3ca72b44 10780static int
e142c38c 10781die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10782{
05cf31d1
JB
10783 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10784 which value is non-zero. However, we have to be careful with
10785 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10786 (via dwarf2_flag_true_p) follows this attribute. So we may
10787 end up accidently finding a declaration attribute that belongs
10788 to a different DIE referenced by the specification attribute,
10789 even though the given DIE does not have a declaration attribute. */
10790 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10791 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10792}
10793
63d06c5c 10794/* Return the die giving the specification for DIE, if there is
f2f0e013 10795 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10796 containing the return value on output. If there is no
10797 specification, but there is an abstract origin, that is
10798 returned. */
63d06c5c
DC
10799
10800static struct die_info *
f2f0e013 10801die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10802{
f2f0e013
DJ
10803 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10804 *spec_cu);
63d06c5c 10805
edb3359d
DJ
10806 if (spec_attr == NULL)
10807 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10808
63d06c5c
DC
10809 if (spec_attr == NULL)
10810 return NULL;
10811 else
f2f0e013 10812 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10813}
c906108c 10814
debd256d 10815/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10816 refers to.
10817 NOTE: This is also used as a "cleanup" function. */
10818
debd256d
JB
10819static void
10820free_line_header (struct line_header *lh)
10821{
10822 if (lh->standard_opcode_lengths)
a8bc7b56 10823 xfree (lh->standard_opcode_lengths);
debd256d
JB
10824
10825 /* Remember that all the lh->file_names[i].name pointers are
10826 pointers into debug_line_buffer, and don't need to be freed. */
10827 if (lh->file_names)
a8bc7b56 10828 xfree (lh->file_names);
debd256d
JB
10829
10830 /* Similarly for the include directory names. */
10831 if (lh->include_dirs)
a8bc7b56 10832 xfree (lh->include_dirs);
debd256d 10833
a8bc7b56 10834 xfree (lh);
debd256d
JB
10835}
10836
debd256d 10837/* Add an entry to LH's include directory table. */
ae2de4f8 10838
debd256d
JB
10839static void
10840add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10841{
debd256d
JB
10842 /* Grow the array if necessary. */
10843 if (lh->include_dirs_size == 0)
c5aa993b 10844 {
debd256d
JB
10845 lh->include_dirs_size = 1; /* for testing */
10846 lh->include_dirs = xmalloc (lh->include_dirs_size
10847 * sizeof (*lh->include_dirs));
10848 }
10849 else if (lh->num_include_dirs >= lh->include_dirs_size)
10850 {
10851 lh->include_dirs_size *= 2;
10852 lh->include_dirs = xrealloc (lh->include_dirs,
10853 (lh->include_dirs_size
10854 * sizeof (*lh->include_dirs)));
c5aa993b 10855 }
c906108c 10856
debd256d
JB
10857 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10858}
6e70227d 10859
debd256d 10860/* Add an entry to LH's file name table. */
ae2de4f8 10861
debd256d
JB
10862static void
10863add_file_name (struct line_header *lh,
10864 char *name,
10865 unsigned int dir_index,
10866 unsigned int mod_time,
10867 unsigned int length)
10868{
10869 struct file_entry *fe;
10870
10871 /* Grow the array if necessary. */
10872 if (lh->file_names_size == 0)
10873 {
10874 lh->file_names_size = 1; /* for testing */
10875 lh->file_names = xmalloc (lh->file_names_size
10876 * sizeof (*lh->file_names));
10877 }
10878 else if (lh->num_file_names >= lh->file_names_size)
10879 {
10880 lh->file_names_size *= 2;
10881 lh->file_names = xrealloc (lh->file_names,
10882 (lh->file_names_size
10883 * sizeof (*lh->file_names)));
10884 }
10885
10886 fe = &lh->file_names[lh->num_file_names++];
10887 fe->name = name;
10888 fe->dir_index = dir_index;
10889 fe->mod_time = mod_time;
10890 fe->length = length;
aaa75496 10891 fe->included_p = 0;
cb1df416 10892 fe->symtab = NULL;
debd256d 10893}
6e70227d 10894
debd256d 10895/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10896 .debug_line, according to the endianness of ABFD. Return a pointer
10897 to a struct line_header, allocated using xmalloc.
debd256d
JB
10898
10899 NOTE: the strings in the include directory and file name tables of
10900 the returned object point into debug_line_buffer, and must not be
10901 freed. */
ae2de4f8 10902
debd256d
JB
10903static struct line_header *
10904dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10905 struct dwarf2_cu *cu)
debd256d
JB
10906{
10907 struct cleanup *back_to;
10908 struct line_header *lh;
fe1b8b76 10909 gdb_byte *line_ptr;
c764a876 10910 unsigned int bytes_read, offset_size;
debd256d
JB
10911 int i;
10912 char *cur_dir, *cur_file;
10913
be391dca 10914 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10915 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10916 {
e2e0b3e5 10917 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10918 return 0;
10919 }
10920
a738430d
MK
10921 /* Make sure that at least there's room for the total_length field.
10922 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10923 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10924 {
4d3c2250 10925 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10926 return 0;
10927 }
10928
10929 lh = xmalloc (sizeof (*lh));
10930 memset (lh, 0, sizeof (*lh));
10931 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10932 (void *) lh);
10933
dce234bc 10934 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10935
a738430d 10936 /* Read in the header. */
6e70227d 10937 lh->total_length =
c764a876
DE
10938 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10939 &bytes_read, &offset_size);
debd256d 10940 line_ptr += bytes_read;
dce234bc
PP
10941 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10942 + dwarf2_per_objfile->line.size))
debd256d 10943 {
4d3c2250 10944 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10945 return 0;
10946 }
10947 lh->statement_program_end = line_ptr + lh->total_length;
10948 lh->version = read_2_bytes (abfd, line_ptr);
10949 line_ptr += 2;
c764a876
DE
10950 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10951 line_ptr += offset_size;
debd256d
JB
10952 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10953 line_ptr += 1;
2dc7f7b3
TT
10954 if (lh->version >= 4)
10955 {
10956 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10957 line_ptr += 1;
10958 }
10959 else
10960 lh->maximum_ops_per_instruction = 1;
10961
10962 if (lh->maximum_ops_per_instruction == 0)
10963 {
10964 lh->maximum_ops_per_instruction = 1;
10965 complaint (&symfile_complaints,
3e43a32a
MS
10966 _("invalid maximum_ops_per_instruction "
10967 "in `.debug_line' section"));
2dc7f7b3
TT
10968 }
10969
debd256d
JB
10970 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10971 line_ptr += 1;
10972 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10973 line_ptr += 1;
10974 lh->line_range = read_1_byte (abfd, line_ptr);
10975 line_ptr += 1;
10976 lh->opcode_base = read_1_byte (abfd, line_ptr);
10977 line_ptr += 1;
10978 lh->standard_opcode_lengths
fe1b8b76 10979 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10980
10981 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10982 for (i = 1; i < lh->opcode_base; ++i)
10983 {
10984 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10985 line_ptr += 1;
10986 }
10987
a738430d 10988 /* Read directory table. */
9b1c24c8 10989 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10990 {
10991 line_ptr += bytes_read;
10992 add_include_dir (lh, cur_dir);
10993 }
10994 line_ptr += bytes_read;
10995
a738430d 10996 /* Read file name table. */
9b1c24c8 10997 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10998 {
10999 unsigned int dir_index, mod_time, length;
11000
11001 line_ptr += bytes_read;
11002 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11003 line_ptr += bytes_read;
11004 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11005 line_ptr += bytes_read;
11006 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11007 line_ptr += bytes_read;
11008
11009 add_file_name (lh, cur_file, dir_index, mod_time, length);
11010 }
11011 line_ptr += bytes_read;
6e70227d 11012 lh->statement_program_start = line_ptr;
debd256d 11013
dce234bc
PP
11014 if (line_ptr > (dwarf2_per_objfile->line.buffer
11015 + dwarf2_per_objfile->line.size))
4d3c2250 11016 complaint (&symfile_complaints,
3e43a32a
MS
11017 _("line number info header doesn't "
11018 "fit in `.debug_line' section"));
debd256d
JB
11019
11020 discard_cleanups (back_to);
11021 return lh;
11022}
c906108c 11023
c6da4cef
DE
11024/* Subroutine of dwarf_decode_lines to simplify it.
11025 Return the file name of the psymtab for included file FILE_INDEX
11026 in line header LH of PST.
11027 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11028 If space for the result is malloc'd, it will be freed by a cleanup.
11029 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11030
11031static char *
11032psymtab_include_file_name (const struct line_header *lh, int file_index,
11033 const struct partial_symtab *pst,
11034 const char *comp_dir)
11035{
11036 const struct file_entry fe = lh->file_names [file_index];
11037 char *include_name = fe.name;
11038 char *include_name_to_compare = include_name;
11039 char *dir_name = NULL;
72b9f47f
TT
11040 const char *pst_filename;
11041 char *copied_name = NULL;
c6da4cef
DE
11042 int file_is_pst;
11043
11044 if (fe.dir_index)
11045 dir_name = lh->include_dirs[fe.dir_index - 1];
11046
11047 if (!IS_ABSOLUTE_PATH (include_name)
11048 && (dir_name != NULL || comp_dir != NULL))
11049 {
11050 /* Avoid creating a duplicate psymtab for PST.
11051 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11052 Before we do the comparison, however, we need to account
11053 for DIR_NAME and COMP_DIR.
11054 First prepend dir_name (if non-NULL). If we still don't
11055 have an absolute path prepend comp_dir (if non-NULL).
11056 However, the directory we record in the include-file's
11057 psymtab does not contain COMP_DIR (to match the
11058 corresponding symtab(s)).
11059
11060 Example:
11061
11062 bash$ cd /tmp
11063 bash$ gcc -g ./hello.c
11064 include_name = "hello.c"
11065 dir_name = "."
11066 DW_AT_comp_dir = comp_dir = "/tmp"
11067 DW_AT_name = "./hello.c" */
11068
11069 if (dir_name != NULL)
11070 {
11071 include_name = concat (dir_name, SLASH_STRING,
11072 include_name, (char *)NULL);
11073 include_name_to_compare = include_name;
11074 make_cleanup (xfree, include_name);
11075 }
11076 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11077 {
11078 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11079 include_name, (char *)NULL);
11080 }
11081 }
11082
11083 pst_filename = pst->filename;
11084 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11085 {
72b9f47f
TT
11086 copied_name = concat (pst->dirname, SLASH_STRING,
11087 pst_filename, (char *)NULL);
11088 pst_filename = copied_name;
c6da4cef
DE
11089 }
11090
1e3fad37 11091 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11092
11093 if (include_name_to_compare != include_name)
11094 xfree (include_name_to_compare);
72b9f47f
TT
11095 if (copied_name != NULL)
11096 xfree (copied_name);
c6da4cef
DE
11097
11098 if (file_is_pst)
11099 return NULL;
11100 return include_name;
11101}
11102
c91513d8
PP
11103/* Ignore this record_line request. */
11104
11105static void
11106noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11107{
11108 return;
11109}
11110
f3f5162e
DE
11111/* Subroutine of dwarf_decode_lines to simplify it.
11112 Process the line number information in LH. */
debd256d 11113
c906108c 11114static void
f3f5162e
DE
11115dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11116 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11117{
a8c50c1f 11118 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11119 gdb_byte *line_end;
a8c50c1f 11120 unsigned int bytes_read, extended_len;
c906108c 11121 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11122 CORE_ADDR baseaddr;
11123 struct objfile *objfile = cu->objfile;
f3f5162e 11124 bfd *abfd = objfile->obfd;
fbf65064 11125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11126 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11127 struct subfile *last_subfile = NULL;
c91513d8
PP
11128 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11129 = record_line;
e142c38c
DJ
11130
11131 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11132
debd256d
JB
11133 line_ptr = lh->statement_program_start;
11134 line_end = lh->statement_program_end;
c906108c
SS
11135
11136 /* Read the statement sequences until there's nothing left. */
11137 while (line_ptr < line_end)
11138 {
11139 /* state machine registers */
11140 CORE_ADDR address = 0;
11141 unsigned int file = 1;
11142 unsigned int line = 1;
11143 unsigned int column = 0;
debd256d 11144 int is_stmt = lh->default_is_stmt;
c906108c
SS
11145 int basic_block = 0;
11146 int end_sequence = 0;
fbf65064 11147 CORE_ADDR addr;
2dc7f7b3 11148 unsigned char op_index = 0;
c906108c 11149
aaa75496 11150 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11151 {
aaa75496 11152 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11153 /* lh->include_dirs and lh->file_names are 0-based, but the
11154 directory and file name numbers in the statement program
11155 are 1-based. */
11156 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11157 char *dir = NULL;
a738430d 11158
debd256d
JB
11159 if (fe->dir_index)
11160 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11161
11162 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11163 }
11164
a738430d 11165 /* Decode the table. */
c5aa993b 11166 while (!end_sequence)
c906108c
SS
11167 {
11168 op_code = read_1_byte (abfd, line_ptr);
11169 line_ptr += 1;
59205f5a
JB
11170 if (line_ptr > line_end)
11171 {
11172 dwarf2_debug_line_missing_end_sequence_complaint ();
11173 break;
11174 }
9aa1fe7e 11175
debd256d 11176 if (op_code >= lh->opcode_base)
6e70227d 11177 {
a738430d 11178 /* Special operand. */
debd256d 11179 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11180 address += (((op_index + (adj_opcode / lh->line_range))
11181 / lh->maximum_ops_per_instruction)
11182 * lh->minimum_instruction_length);
11183 op_index = ((op_index + (adj_opcode / lh->line_range))
11184 % lh->maximum_ops_per_instruction);
debd256d 11185 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11186 if (lh->num_file_names < file || file == 0)
25e43795 11187 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11188 /* For now we ignore lines not starting on an
11189 instruction boundary. */
11190 else if (op_index == 0)
25e43795
DJ
11191 {
11192 lh->file_names[file - 1].included_p = 1;
ca5f395d 11193 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11194 {
11195 if (last_subfile != current_subfile)
11196 {
11197 addr = gdbarch_addr_bits_remove (gdbarch, address);
11198 if (last_subfile)
c91513d8 11199 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11200 last_subfile = current_subfile;
11201 }
25e43795 11202 /* Append row to matrix using current values. */
7019d805 11203 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11204 (*p_record_line) (current_subfile, line, addr);
366da635 11205 }
25e43795 11206 }
ca5f395d 11207 basic_block = 0;
9aa1fe7e
GK
11208 }
11209 else switch (op_code)
c906108c
SS
11210 {
11211 case DW_LNS_extended_op:
3e43a32a
MS
11212 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11213 &bytes_read);
473b7be6 11214 line_ptr += bytes_read;
a8c50c1f 11215 extended_end = line_ptr + extended_len;
c906108c
SS
11216 extended_op = read_1_byte (abfd, line_ptr);
11217 line_ptr += 1;
11218 switch (extended_op)
11219 {
11220 case DW_LNE_end_sequence:
c91513d8 11221 p_record_line = record_line;
c906108c 11222 end_sequence = 1;
c906108c
SS
11223 break;
11224 case DW_LNE_set_address:
e7c27a73 11225 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11226
11227 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11228 {
11229 /* This line table is for a function which has been
11230 GCd by the linker. Ignore it. PR gdb/12528 */
11231
11232 long line_offset
11233 = line_ptr - dwarf2_per_objfile->line.buffer;
11234
11235 complaint (&symfile_complaints,
11236 _(".debug_line address at offset 0x%lx is 0 "
11237 "[in module %s]"),
bb5ed363 11238 line_offset, objfile->name);
c91513d8
PP
11239 p_record_line = noop_record_line;
11240 }
11241
2dc7f7b3 11242 op_index = 0;
107d2387
AC
11243 line_ptr += bytes_read;
11244 address += baseaddr;
c906108c
SS
11245 break;
11246 case DW_LNE_define_file:
debd256d
JB
11247 {
11248 char *cur_file;
11249 unsigned int dir_index, mod_time, length;
6e70227d 11250
3e43a32a
MS
11251 cur_file = read_direct_string (abfd, line_ptr,
11252 &bytes_read);
debd256d
JB
11253 line_ptr += bytes_read;
11254 dir_index =
11255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11256 line_ptr += bytes_read;
11257 mod_time =
11258 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11259 line_ptr += bytes_read;
11260 length =
11261 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11262 line_ptr += bytes_read;
11263 add_file_name (lh, cur_file, dir_index, mod_time, length);
11264 }
c906108c 11265 break;
d0c6ba3d
CC
11266 case DW_LNE_set_discriminator:
11267 /* The discriminator is not interesting to the debugger;
11268 just ignore it. */
11269 line_ptr = extended_end;
11270 break;
c906108c 11271 default:
4d3c2250 11272 complaint (&symfile_complaints,
e2e0b3e5 11273 _("mangled .debug_line section"));
debd256d 11274 return;
c906108c 11275 }
a8c50c1f
DJ
11276 /* Make sure that we parsed the extended op correctly. If e.g.
11277 we expected a different address size than the producer used,
11278 we may have read the wrong number of bytes. */
11279 if (line_ptr != extended_end)
11280 {
11281 complaint (&symfile_complaints,
11282 _("mangled .debug_line section"));
11283 return;
11284 }
c906108c
SS
11285 break;
11286 case DW_LNS_copy:
59205f5a 11287 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11288 dwarf2_debug_line_missing_file_complaint ();
11289 else
366da635 11290 {
25e43795 11291 lh->file_names[file - 1].included_p = 1;
ca5f395d 11292 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11293 {
11294 if (last_subfile != current_subfile)
11295 {
11296 addr = gdbarch_addr_bits_remove (gdbarch, address);
11297 if (last_subfile)
c91513d8 11298 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11299 last_subfile = current_subfile;
11300 }
7019d805 11301 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11302 (*p_record_line) (current_subfile, line, addr);
fbf65064 11303 }
366da635 11304 }
c906108c
SS
11305 basic_block = 0;
11306 break;
11307 case DW_LNS_advance_pc:
2dc7f7b3
TT
11308 {
11309 CORE_ADDR adjust
11310 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11311
11312 address += (((op_index + adjust)
11313 / lh->maximum_ops_per_instruction)
11314 * lh->minimum_instruction_length);
11315 op_index = ((op_index + adjust)
11316 % lh->maximum_ops_per_instruction);
11317 line_ptr += bytes_read;
11318 }
c906108c
SS
11319 break;
11320 case DW_LNS_advance_line:
11321 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11322 line_ptr += bytes_read;
11323 break;
11324 case DW_LNS_set_file:
debd256d 11325 {
a738430d
MK
11326 /* The arrays lh->include_dirs and lh->file_names are
11327 0-based, but the directory and file name numbers in
11328 the statement program are 1-based. */
debd256d 11329 struct file_entry *fe;
4f1520fb 11330 char *dir = NULL;
a738430d 11331
debd256d
JB
11332 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11333 line_ptr += bytes_read;
59205f5a 11334 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11335 dwarf2_debug_line_missing_file_complaint ();
11336 else
11337 {
11338 fe = &lh->file_names[file - 1];
11339 if (fe->dir_index)
11340 dir = lh->include_dirs[fe->dir_index - 1];
11341 if (!decode_for_pst_p)
11342 {
11343 last_subfile = current_subfile;
11344 dwarf2_start_subfile (fe->name, dir, comp_dir);
11345 }
11346 }
debd256d 11347 }
c906108c
SS
11348 break;
11349 case DW_LNS_set_column:
11350 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11351 line_ptr += bytes_read;
11352 break;
11353 case DW_LNS_negate_stmt:
11354 is_stmt = (!is_stmt);
11355 break;
11356 case DW_LNS_set_basic_block:
11357 basic_block = 1;
11358 break;
c2c6d25f
JM
11359 /* Add to the address register of the state machine the
11360 address increment value corresponding to special opcode
a738430d
MK
11361 255. I.e., this value is scaled by the minimum
11362 instruction length since special opcode 255 would have
b021a221 11363 scaled the increment. */
c906108c 11364 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11365 {
11366 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11367
11368 address += (((op_index + adjust)
11369 / lh->maximum_ops_per_instruction)
11370 * lh->minimum_instruction_length);
11371 op_index = ((op_index + adjust)
11372 % lh->maximum_ops_per_instruction);
11373 }
c906108c
SS
11374 break;
11375 case DW_LNS_fixed_advance_pc:
11376 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11377 op_index = 0;
c906108c
SS
11378 line_ptr += 2;
11379 break;
9aa1fe7e 11380 default:
a738430d
MK
11381 {
11382 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11383 int i;
a738430d 11384
debd256d 11385 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11386 {
11387 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11388 line_ptr += bytes_read;
11389 }
11390 }
c906108c
SS
11391 }
11392 }
59205f5a
JB
11393 if (lh->num_file_names < file || file == 0)
11394 dwarf2_debug_line_missing_file_complaint ();
11395 else
11396 {
11397 lh->file_names[file - 1].included_p = 1;
11398 if (!decode_for_pst_p)
fbf65064
UW
11399 {
11400 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11401 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11402 }
59205f5a 11403 }
c906108c 11404 }
f3f5162e
DE
11405}
11406
11407/* Decode the Line Number Program (LNP) for the given line_header
11408 structure and CU. The actual information extracted and the type
11409 of structures created from the LNP depends on the value of PST.
11410
11411 1. If PST is NULL, then this procedure uses the data from the program
11412 to create all necessary symbol tables, and their linetables.
11413
11414 2. If PST is not NULL, this procedure reads the program to determine
11415 the list of files included by the unit represented by PST, and
11416 builds all the associated partial symbol tables.
11417
11418 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11419 It is used for relative paths in the line table.
11420 NOTE: When processing partial symtabs (pst != NULL),
11421 comp_dir == pst->dirname.
11422
11423 NOTE: It is important that psymtabs have the same file name (via strcmp)
11424 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11425 symtab we don't use it in the name of the psymtabs we create.
11426 E.g. expand_line_sal requires this when finding psymtabs to expand.
11427 A good testcase for this is mb-inline.exp. */
11428
11429static void
11430dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11431 struct dwarf2_cu *cu, struct partial_symtab *pst,
11432 int want_line_info)
11433{
11434 struct objfile *objfile = cu->objfile;
11435 const int decode_for_pst_p = (pst != NULL);
11436 struct subfile *first_subfile = current_subfile;
11437
11438 if (want_line_info)
11439 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11440
11441 if (decode_for_pst_p)
11442 {
11443 int file_index;
11444
11445 /* Now that we're done scanning the Line Header Program, we can
11446 create the psymtab of each included file. */
11447 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11448 if (lh->file_names[file_index].included_p == 1)
11449 {
c6da4cef
DE
11450 char *include_name =
11451 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11452 if (include_name != NULL)
aaa75496
JB
11453 dwarf2_create_include_psymtab (include_name, pst, objfile);
11454 }
11455 }
cb1df416
DJ
11456 else
11457 {
11458 /* Make sure a symtab is created for every file, even files
11459 which contain only variables (i.e. no code with associated
11460 line numbers). */
cb1df416 11461 int i;
cb1df416
DJ
11462
11463 for (i = 0; i < lh->num_file_names; i++)
11464 {
11465 char *dir = NULL;
f3f5162e 11466 struct file_entry *fe;
9a619af0 11467
cb1df416
DJ
11468 fe = &lh->file_names[i];
11469 if (fe->dir_index)
11470 dir = lh->include_dirs[fe->dir_index - 1];
11471 dwarf2_start_subfile (fe->name, dir, comp_dir);
11472
11473 /* Skip the main file; we don't need it, and it must be
11474 allocated last, so that it will show up before the
11475 non-primary symtabs in the objfile's symtab list. */
11476 if (current_subfile == first_subfile)
11477 continue;
11478
11479 if (current_subfile->symtab == NULL)
11480 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11481 objfile);
cb1df416
DJ
11482 fe->symtab = current_subfile->symtab;
11483 }
11484 }
c906108c
SS
11485}
11486
11487/* Start a subfile for DWARF. FILENAME is the name of the file and
11488 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11489 or NULL if not known. COMP_DIR is the compilation directory for the
11490 linetable's compilation unit or NULL if not known.
c906108c
SS
11491 This routine tries to keep line numbers from identical absolute and
11492 relative file names in a common subfile.
11493
11494 Using the `list' example from the GDB testsuite, which resides in
11495 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11496 of /srcdir/list0.c yields the following debugging information for list0.c:
11497
c5aa993b
JM
11498 DW_AT_name: /srcdir/list0.c
11499 DW_AT_comp_dir: /compdir
357e46e7 11500 files.files[0].name: list0.h
c5aa993b 11501 files.files[0].dir: /srcdir
357e46e7 11502 files.files[1].name: list0.c
c5aa993b 11503 files.files[1].dir: /srcdir
c906108c
SS
11504
11505 The line number information for list0.c has to end up in a single
4f1520fb
FR
11506 subfile, so that `break /srcdir/list0.c:1' works as expected.
11507 start_subfile will ensure that this happens provided that we pass the
11508 concatenation of files.files[1].dir and files.files[1].name as the
11509 subfile's name. */
c906108c
SS
11510
11511static void
3e43a32a
MS
11512dwarf2_start_subfile (char *filename, const char *dirname,
11513 const char *comp_dir)
c906108c 11514{
4f1520fb
FR
11515 char *fullname;
11516
11517 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11518 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11519 second argument to start_subfile. To be consistent, we do the
11520 same here. In order not to lose the line information directory,
11521 we concatenate it to the filename when it makes sense.
11522 Note that the Dwarf3 standard says (speaking of filenames in line
11523 information): ``The directory index is ignored for file names
11524 that represent full path names''. Thus ignoring dirname in the
11525 `else' branch below isn't an issue. */
c906108c 11526
d5166ae1 11527 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11528 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11529 else
11530 fullname = filename;
c906108c 11531
4f1520fb
FR
11532 start_subfile (fullname, comp_dir);
11533
11534 if (fullname != filename)
11535 xfree (fullname);
c906108c
SS
11536}
11537
4c2df51b
DJ
11538static void
11539var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11540 struct dwarf2_cu *cu)
4c2df51b 11541{
e7c27a73
DJ
11542 struct objfile *objfile = cu->objfile;
11543 struct comp_unit_head *cu_header = &cu->header;
11544
4c2df51b
DJ
11545 /* NOTE drow/2003-01-30: There used to be a comment and some special
11546 code here to turn a symbol with DW_AT_external and a
11547 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11548 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11549 with some versions of binutils) where shared libraries could have
11550 relocations against symbols in their debug information - the
11551 minimal symbol would have the right address, but the debug info
11552 would not. It's no longer necessary, because we will explicitly
11553 apply relocations when we read in the debug information now. */
11554
11555 /* A DW_AT_location attribute with no contents indicates that a
11556 variable has been optimized away. */
11557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11558 {
11559 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11560 return;
11561 }
11562
11563 /* Handle one degenerate form of location expression specially, to
11564 preserve GDB's previous behavior when section offsets are
11565 specified. If this is just a DW_OP_addr then mark this symbol
11566 as LOC_STATIC. */
11567
11568 if (attr_form_is_block (attr)
11569 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11570 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11571 {
891d2f0b 11572 unsigned int dummy;
4c2df51b
DJ
11573
11574 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11575 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11576 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11577 fixup_symbol_section (sym, objfile);
11578 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11579 SYMBOL_SECTION (sym));
4c2df51b
DJ
11580 return;
11581 }
11582
11583 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11584 expression evaluator, and use LOC_COMPUTED only when necessary
11585 (i.e. when the value of a register or memory location is
11586 referenced, or a thread-local block, etc.). Then again, it might
11587 not be worthwhile. I'm assuming that it isn't unless performance
11588 or memory numbers show me otherwise. */
11589
e7c27a73 11590 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11591 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11592
11593 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11594 cu->has_loclist = 1;
4c2df51b
DJ
11595}
11596
c906108c
SS
11597/* Given a pointer to a DWARF information entry, figure out if we need
11598 to make a symbol table entry for it, and if so, create a new entry
11599 and return a pointer to it.
11600 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11601 used the passed type.
11602 If SPACE is not NULL, use it to hold the new symbol. If it is
11603 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11604
11605static struct symbol *
34eaf542
TT
11606new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11607 struct symbol *space)
c906108c 11608{
e7c27a73 11609 struct objfile *objfile = cu->objfile;
c906108c
SS
11610 struct symbol *sym = NULL;
11611 char *name;
11612 struct attribute *attr = NULL;
11613 struct attribute *attr2 = NULL;
e142c38c 11614 CORE_ADDR baseaddr;
e37fd15a
SW
11615 struct pending **list_to_add = NULL;
11616
edb3359d 11617 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11618
11619 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11620
94af9270 11621 name = dwarf2_name (die, cu);
c906108c
SS
11622 if (name)
11623 {
94af9270 11624 const char *linkagename;
34eaf542 11625 int suppress_add = 0;
94af9270 11626
34eaf542
TT
11627 if (space)
11628 sym = space;
11629 else
11630 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11631 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11632
11633 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11634 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11635 linkagename = dwarf2_physname (name, die, cu);
11636 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11637
f55ee35c
JK
11638 /* Fortran does not have mangling standard and the mangling does differ
11639 between gfortran, iFort etc. */
11640 if (cu->language == language_fortran
b250c185 11641 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11642 symbol_set_demangled_name (&(sym->ginfo),
11643 (char *) dwarf2_full_name (name, die, cu),
11644 NULL);
f55ee35c 11645
c906108c 11646 /* Default assumptions.
c5aa993b 11647 Use the passed type or decode it from the die. */
176620f1 11648 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11649 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11650 if (type != NULL)
11651 SYMBOL_TYPE (sym) = type;
11652 else
e7c27a73 11653 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11654 attr = dwarf2_attr (die,
11655 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11656 cu);
c906108c
SS
11657 if (attr)
11658 {
11659 SYMBOL_LINE (sym) = DW_UNSND (attr);
11660 }
cb1df416 11661
edb3359d
DJ
11662 attr = dwarf2_attr (die,
11663 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11664 cu);
cb1df416
DJ
11665 if (attr)
11666 {
11667 int file_index = DW_UNSND (attr);
9a619af0 11668
cb1df416
DJ
11669 if (cu->line_header == NULL
11670 || file_index > cu->line_header->num_file_names)
11671 complaint (&symfile_complaints,
11672 _("file index out of range"));
1c3d648d 11673 else if (file_index > 0)
cb1df416
DJ
11674 {
11675 struct file_entry *fe;
9a619af0 11676
cb1df416
DJ
11677 fe = &cu->line_header->file_names[file_index - 1];
11678 SYMBOL_SYMTAB (sym) = fe->symtab;
11679 }
11680 }
11681
c906108c
SS
11682 switch (die->tag)
11683 {
11684 case DW_TAG_label:
e142c38c 11685 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11686 if (attr)
11687 {
11688 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11689 }
0f5238ed
TT
11690 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11691 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11692 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11693 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11694 break;
11695 case DW_TAG_subprogram:
11696 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11697 finish_block. */
11698 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11699 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11700 if ((attr2 && (DW_UNSND (attr2) != 0))
11701 || cu->language == language_ada)
c906108c 11702 {
2cfa0c8d
JB
11703 /* Subprograms marked external are stored as a global symbol.
11704 Ada subprograms, whether marked external or not, are always
11705 stored as a global symbol, because we want to be able to
11706 access them globally. For instance, we want to be able
11707 to break on a nested subprogram without having to
11708 specify the context. */
e37fd15a 11709 list_to_add = &global_symbols;
c906108c
SS
11710 }
11711 else
11712 {
e37fd15a 11713 list_to_add = cu->list_in_scope;
c906108c
SS
11714 }
11715 break;
edb3359d
DJ
11716 case DW_TAG_inlined_subroutine:
11717 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11718 finish_block. */
11719 SYMBOL_CLASS (sym) = LOC_BLOCK;
11720 SYMBOL_INLINED (sym) = 1;
11721 /* Do not add the symbol to any lists. It will be found via
11722 BLOCK_FUNCTION from the blockvector. */
11723 break;
34eaf542
TT
11724 case DW_TAG_template_value_param:
11725 suppress_add = 1;
11726 /* Fall through. */
72929c62 11727 case DW_TAG_constant:
c906108c 11728 case DW_TAG_variable:
254e6b9e 11729 case DW_TAG_member:
0963b4bd
MS
11730 /* Compilation with minimal debug info may result in
11731 variables with missing type entries. Change the
11732 misleading `void' type to something sensible. */
c906108c 11733 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11734 SYMBOL_TYPE (sym)
46bf5051 11735 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11736
e142c38c 11737 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11738 /* In the case of DW_TAG_member, we should only be called for
11739 static const members. */
11740 if (die->tag == DW_TAG_member)
11741 {
3863f96c
DE
11742 /* dwarf2_add_field uses die_is_declaration,
11743 so we do the same. */
254e6b9e
DE
11744 gdb_assert (die_is_declaration (die, cu));
11745 gdb_assert (attr);
11746 }
c906108c
SS
11747 if (attr)
11748 {
e7c27a73 11749 dwarf2_const_value (attr, sym, cu);
e142c38c 11750 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11751 if (!suppress_add)
34eaf542
TT
11752 {
11753 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11754 list_to_add = &global_symbols;
34eaf542 11755 else
e37fd15a 11756 list_to_add = cu->list_in_scope;
34eaf542 11757 }
c906108c
SS
11758 break;
11759 }
e142c38c 11760 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11761 if (attr)
11762 {
e7c27a73 11763 var_decode_location (attr, sym, cu);
e142c38c 11764 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11765 if (SYMBOL_CLASS (sym) == LOC_STATIC
11766 && SYMBOL_VALUE_ADDRESS (sym) == 0
11767 && !dwarf2_per_objfile->has_section_at_zero)
11768 {
11769 /* When a static variable is eliminated by the linker,
11770 the corresponding debug information is not stripped
11771 out, but the variable address is set to null;
11772 do not add such variables into symbol table. */
11773 }
11774 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11775 {
f55ee35c
JK
11776 /* Workaround gfortran PR debug/40040 - it uses
11777 DW_AT_location for variables in -fPIC libraries which may
11778 get overriden by other libraries/executable and get
11779 a different address. Resolve it by the minimal symbol
11780 which may come from inferior's executable using copy
11781 relocation. Make this workaround only for gfortran as for
11782 other compilers GDB cannot guess the minimal symbol
11783 Fortran mangling kind. */
11784 if (cu->language == language_fortran && die->parent
11785 && die->parent->tag == DW_TAG_module
11786 && cu->producer
11787 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11788 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11789
1c809c68
TT
11790 /* A variable with DW_AT_external is never static,
11791 but it may be block-scoped. */
11792 list_to_add = (cu->list_in_scope == &file_symbols
11793 ? &global_symbols : cu->list_in_scope);
1c809c68 11794 }
c906108c 11795 else
e37fd15a 11796 list_to_add = cu->list_in_scope;
c906108c
SS
11797 }
11798 else
11799 {
11800 /* We do not know the address of this symbol.
c5aa993b
JM
11801 If it is an external symbol and we have type information
11802 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11803 The address of the variable will then be determined from
11804 the minimal symbol table whenever the variable is
11805 referenced. */
e142c38c 11806 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11807 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11808 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11809 {
0fe7935b
DJ
11810 /* A variable with DW_AT_external is never static, but it
11811 may be block-scoped. */
11812 list_to_add = (cu->list_in_scope == &file_symbols
11813 ? &global_symbols : cu->list_in_scope);
11814
c906108c 11815 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11816 }
442ddf59
JK
11817 else if (!die_is_declaration (die, cu))
11818 {
11819 /* Use the default LOC_OPTIMIZED_OUT class. */
11820 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11821 if (!suppress_add)
11822 list_to_add = cu->list_in_scope;
442ddf59 11823 }
c906108c
SS
11824 }
11825 break;
11826 case DW_TAG_formal_parameter:
edb3359d
DJ
11827 /* If we are inside a function, mark this as an argument. If
11828 not, we might be looking at an argument to an inlined function
11829 when we do not have enough information to show inlined frames;
11830 pretend it's a local variable in that case so that the user can
11831 still see it. */
11832 if (context_stack_depth > 0
11833 && context_stack[context_stack_depth - 1].name != NULL)
11834 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11835 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11836 if (attr)
11837 {
e7c27a73 11838 var_decode_location (attr, sym, cu);
c906108c 11839 }
e142c38c 11840 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11841 if (attr)
11842 {
e7c27a73 11843 dwarf2_const_value (attr, sym, cu);
c906108c 11844 }
f346a30d 11845
e37fd15a 11846 list_to_add = cu->list_in_scope;
c906108c
SS
11847 break;
11848 case DW_TAG_unspecified_parameters:
11849 /* From varargs functions; gdb doesn't seem to have any
11850 interest in this information, so just ignore it for now.
11851 (FIXME?) */
11852 break;
34eaf542
TT
11853 case DW_TAG_template_type_param:
11854 suppress_add = 1;
11855 /* Fall through. */
c906108c 11856 case DW_TAG_class_type:
680b30c7 11857 case DW_TAG_interface_type:
c906108c
SS
11858 case DW_TAG_structure_type:
11859 case DW_TAG_union_type:
72019c9c 11860 case DW_TAG_set_type:
c906108c
SS
11861 case DW_TAG_enumeration_type:
11862 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11863 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11864
63d06c5c 11865 {
987504bb 11866 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11867 really ever be static objects: otherwise, if you try
11868 to, say, break of a class's method and you're in a file
11869 which doesn't mention that class, it won't work unless
11870 the check for all static symbols in lookup_symbol_aux
11871 saves you. See the OtherFileClass tests in
11872 gdb.c++/namespace.exp. */
11873
e37fd15a 11874 if (!suppress_add)
34eaf542 11875 {
34eaf542
TT
11876 list_to_add = (cu->list_in_scope == &file_symbols
11877 && (cu->language == language_cplus
11878 || cu->language == language_java)
11879 ? &global_symbols : cu->list_in_scope);
63d06c5c 11880
64382290
TT
11881 /* The semantics of C++ state that "struct foo {
11882 ... }" also defines a typedef for "foo". A Java
11883 class declaration also defines a typedef for the
11884 class. */
11885 if (cu->language == language_cplus
11886 || cu->language == language_java
11887 || cu->language == language_ada)
11888 {
11889 /* The symbol's name is already allocated along
11890 with this objfile, so we don't need to
11891 duplicate it for the type. */
11892 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11893 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11894 }
63d06c5c
DC
11895 }
11896 }
c906108c
SS
11897 break;
11898 case DW_TAG_typedef:
63d06c5c
DC
11899 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11900 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11901 list_to_add = cu->list_in_scope;
63d06c5c 11902 break;
c906108c 11903 case DW_TAG_base_type:
a02abb62 11904 case DW_TAG_subrange_type:
c906108c 11905 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11906 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11907 list_to_add = cu->list_in_scope;
c906108c
SS
11908 break;
11909 case DW_TAG_enumerator:
e142c38c 11910 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11911 if (attr)
11912 {
e7c27a73 11913 dwarf2_const_value (attr, sym, cu);
c906108c 11914 }
63d06c5c
DC
11915 {
11916 /* NOTE: carlton/2003-11-10: See comment above in the
11917 DW_TAG_class_type, etc. block. */
11918
e142c38c 11919 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11920 && (cu->language == language_cplus
11921 || cu->language == language_java)
e142c38c 11922 ? &global_symbols : cu->list_in_scope);
63d06c5c 11923 }
c906108c 11924 break;
5c4e30ca
DC
11925 case DW_TAG_namespace:
11926 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11927 list_to_add = &global_symbols;
5c4e30ca 11928 break;
c906108c
SS
11929 default:
11930 /* Not a tag we recognize. Hopefully we aren't processing
11931 trash data, but since we must specifically ignore things
11932 we don't recognize, there is nothing else we should do at
0963b4bd 11933 this point. */
e2e0b3e5 11934 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11935 dwarf_tag_name (die->tag));
c906108c
SS
11936 break;
11937 }
df8a16a1 11938
e37fd15a
SW
11939 if (suppress_add)
11940 {
11941 sym->hash_next = objfile->template_symbols;
11942 objfile->template_symbols = sym;
11943 list_to_add = NULL;
11944 }
11945
11946 if (list_to_add != NULL)
11947 add_symbol_to_list (sym, list_to_add);
11948
df8a16a1
DJ
11949 /* For the benefit of old versions of GCC, check for anonymous
11950 namespaces based on the demangled name. */
11951 if (!processing_has_namespace_info
94af9270 11952 && cu->language == language_cplus)
a10964d1 11953 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11954 }
11955 return (sym);
11956}
11957
34eaf542
TT
11958/* A wrapper for new_symbol_full that always allocates a new symbol. */
11959
11960static struct symbol *
11961new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11962{
11963 return new_symbol_full (die, type, cu, NULL);
11964}
11965
98bfdba5
PA
11966/* Given an attr with a DW_FORM_dataN value in host byte order,
11967 zero-extend it as appropriate for the symbol's type. The DWARF
11968 standard (v4) is not entirely clear about the meaning of using
11969 DW_FORM_dataN for a constant with a signed type, where the type is
11970 wider than the data. The conclusion of a discussion on the DWARF
11971 list was that this is unspecified. We choose to always zero-extend
11972 because that is the interpretation long in use by GCC. */
c906108c 11973
98bfdba5
PA
11974static gdb_byte *
11975dwarf2_const_value_data (struct attribute *attr, struct type *type,
11976 const char *name, struct obstack *obstack,
11977 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11978{
e7c27a73 11979 struct objfile *objfile = cu->objfile;
e17a4113
UW
11980 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11981 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11982 LONGEST l = DW_UNSND (attr);
11983
11984 if (bits < sizeof (*value) * 8)
11985 {
11986 l &= ((LONGEST) 1 << bits) - 1;
11987 *value = l;
11988 }
11989 else if (bits == sizeof (*value) * 8)
11990 *value = l;
11991 else
11992 {
11993 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11994 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11995 return bytes;
11996 }
11997
11998 return NULL;
11999}
12000
12001/* Read a constant value from an attribute. Either set *VALUE, or if
12002 the value does not fit in *VALUE, set *BYTES - either already
12003 allocated on the objfile obstack, or newly allocated on OBSTACK,
12004 or, set *BATON, if we translated the constant to a location
12005 expression. */
12006
12007static void
12008dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12009 const char *name, struct obstack *obstack,
12010 struct dwarf2_cu *cu,
12011 long *value, gdb_byte **bytes,
12012 struct dwarf2_locexpr_baton **baton)
12013{
12014 struct objfile *objfile = cu->objfile;
12015 struct comp_unit_head *cu_header = &cu->header;
c906108c 12016 struct dwarf_block *blk;
98bfdba5
PA
12017 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12018 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12019
12020 *value = 0;
12021 *bytes = NULL;
12022 *baton = NULL;
c906108c
SS
12023
12024 switch (attr->form)
12025 {
12026 case DW_FORM_addr:
ac56253d 12027 {
ac56253d
TT
12028 gdb_byte *data;
12029
98bfdba5
PA
12030 if (TYPE_LENGTH (type) != cu_header->addr_size)
12031 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12032 cu_header->addr_size,
98bfdba5 12033 TYPE_LENGTH (type));
ac56253d
TT
12034 /* Symbols of this form are reasonably rare, so we just
12035 piggyback on the existing location code rather than writing
12036 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12037 *baton = obstack_alloc (&objfile->objfile_obstack,
12038 sizeof (struct dwarf2_locexpr_baton));
12039 (*baton)->per_cu = cu->per_cu;
12040 gdb_assert ((*baton)->per_cu);
ac56253d 12041
98bfdba5
PA
12042 (*baton)->size = 2 + cu_header->addr_size;
12043 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12044 (*baton)->data = data;
ac56253d
TT
12045
12046 data[0] = DW_OP_addr;
12047 store_unsigned_integer (&data[1], cu_header->addr_size,
12048 byte_order, DW_ADDR (attr));
12049 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12050 }
c906108c 12051 break;
4ac36638 12052 case DW_FORM_string:
93b5768b 12053 case DW_FORM_strp:
98bfdba5
PA
12054 /* DW_STRING is already allocated on the objfile obstack, point
12055 directly to it. */
12056 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12057 break;
c906108c
SS
12058 case DW_FORM_block1:
12059 case DW_FORM_block2:
12060 case DW_FORM_block4:
12061 case DW_FORM_block:
2dc7f7b3 12062 case DW_FORM_exprloc:
c906108c 12063 blk = DW_BLOCK (attr);
98bfdba5
PA
12064 if (TYPE_LENGTH (type) != blk->size)
12065 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12066 TYPE_LENGTH (type));
12067 *bytes = blk->data;
c906108c 12068 break;
2df3850c
JM
12069
12070 /* The DW_AT_const_value attributes are supposed to carry the
12071 symbol's value "represented as it would be on the target
12072 architecture." By the time we get here, it's already been
12073 converted to host endianness, so we just need to sign- or
12074 zero-extend it as appropriate. */
12075 case DW_FORM_data1:
3e43a32a
MS
12076 *bytes = dwarf2_const_value_data (attr, type, name,
12077 obstack, cu, value, 8);
2df3850c 12078 break;
c906108c 12079 case DW_FORM_data2:
3e43a32a
MS
12080 *bytes = dwarf2_const_value_data (attr, type, name,
12081 obstack, cu, value, 16);
2df3850c 12082 break;
c906108c 12083 case DW_FORM_data4:
3e43a32a
MS
12084 *bytes = dwarf2_const_value_data (attr, type, name,
12085 obstack, cu, value, 32);
2df3850c 12086 break;
c906108c 12087 case DW_FORM_data8:
3e43a32a
MS
12088 *bytes = dwarf2_const_value_data (attr, type, name,
12089 obstack, cu, value, 64);
2df3850c
JM
12090 break;
12091
c906108c 12092 case DW_FORM_sdata:
98bfdba5 12093 *value = DW_SND (attr);
2df3850c
JM
12094 break;
12095
c906108c 12096 case DW_FORM_udata:
98bfdba5 12097 *value = DW_UNSND (attr);
c906108c 12098 break;
2df3850c 12099
c906108c 12100 default:
4d3c2250 12101 complaint (&symfile_complaints,
e2e0b3e5 12102 _("unsupported const value attribute form: '%s'"),
4d3c2250 12103 dwarf_form_name (attr->form));
98bfdba5 12104 *value = 0;
c906108c
SS
12105 break;
12106 }
12107}
12108
2df3850c 12109
98bfdba5
PA
12110/* Copy constant value from an attribute to a symbol. */
12111
2df3850c 12112static void
98bfdba5
PA
12113dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12114 struct dwarf2_cu *cu)
2df3850c 12115{
98bfdba5
PA
12116 struct objfile *objfile = cu->objfile;
12117 struct comp_unit_head *cu_header = &cu->header;
12118 long value;
12119 gdb_byte *bytes;
12120 struct dwarf2_locexpr_baton *baton;
2df3850c 12121
98bfdba5
PA
12122 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12123 SYMBOL_PRINT_NAME (sym),
12124 &objfile->objfile_obstack, cu,
12125 &value, &bytes, &baton);
2df3850c 12126
98bfdba5
PA
12127 if (baton != NULL)
12128 {
12129 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12130 SYMBOL_LOCATION_BATON (sym) = baton;
12131 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12132 }
12133 else if (bytes != NULL)
12134 {
12135 SYMBOL_VALUE_BYTES (sym) = bytes;
12136 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12137 }
12138 else
12139 {
12140 SYMBOL_VALUE (sym) = value;
12141 SYMBOL_CLASS (sym) = LOC_CONST;
12142 }
2df3850c
JM
12143}
12144
c906108c
SS
12145/* Return the type of the die in question using its DW_AT_type attribute. */
12146
12147static struct type *
e7c27a73 12148die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12149{
c906108c 12150 struct attribute *type_attr;
c906108c 12151
e142c38c 12152 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12153 if (!type_attr)
12154 {
12155 /* A missing DW_AT_type represents a void type. */
46bf5051 12156 return objfile_type (cu->objfile)->builtin_void;
c906108c 12157 }
348e048f 12158
673bfd45 12159 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12160}
12161
b4ba55a1
JB
12162/* True iff CU's producer generates GNAT Ada auxiliary information
12163 that allows to find parallel types through that information instead
12164 of having to do expensive parallel lookups by type name. */
12165
12166static int
12167need_gnat_info (struct dwarf2_cu *cu)
12168{
12169 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12170 of GNAT produces this auxiliary information, without any indication
12171 that it is produced. Part of enhancing the FSF version of GNAT
12172 to produce that information will be to put in place an indicator
12173 that we can use in order to determine whether the descriptive type
12174 info is available or not. One suggestion that has been made is
12175 to use a new attribute, attached to the CU die. For now, assume
12176 that the descriptive type info is not available. */
12177 return 0;
12178}
12179
b4ba55a1
JB
12180/* Return the auxiliary type of the die in question using its
12181 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12182 attribute is not present. */
12183
12184static struct type *
12185die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12186{
b4ba55a1 12187 struct attribute *type_attr;
b4ba55a1
JB
12188
12189 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12190 if (!type_attr)
12191 return NULL;
12192
673bfd45 12193 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12194}
12195
12196/* If DIE has a descriptive_type attribute, then set the TYPE's
12197 descriptive type accordingly. */
12198
12199static void
12200set_descriptive_type (struct type *type, struct die_info *die,
12201 struct dwarf2_cu *cu)
12202{
12203 struct type *descriptive_type = die_descriptive_type (die, cu);
12204
12205 if (descriptive_type)
12206 {
12207 ALLOCATE_GNAT_AUX_TYPE (type);
12208 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12209 }
12210}
12211
c906108c
SS
12212/* Return the containing type of the die in question using its
12213 DW_AT_containing_type attribute. */
12214
12215static struct type *
e7c27a73 12216die_containing_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_containing_type, cu);
33ac96f0
JK
12221 if (!type_attr)
12222 error (_("Dwarf Error: Problem turning containing type into gdb type "
12223 "[in module %s]"), cu->objfile->name);
12224
673bfd45 12225 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12226}
12227
673bfd45
DE
12228/* Look up the type of DIE in CU using its type attribute ATTR.
12229 If there is no type substitute an error marker. */
12230
c906108c 12231static struct type *
673bfd45
DE
12232lookup_die_type (struct die_info *die, struct attribute *attr,
12233 struct dwarf2_cu *cu)
c906108c 12234{
bb5ed363 12235 struct objfile *objfile = cu->objfile;
f792889a
DJ
12236 struct type *this_type;
12237
673bfd45
DE
12238 /* First see if we have it cached. */
12239
12240 if (is_ref_attr (attr))
12241 {
12242 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12243
12244 this_type = get_die_type_at_offset (offset, cu->per_cu);
12245 }
55f1336d 12246 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12247 {
12248 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12249 struct dwarf2_cu *sig_cu;
12250 unsigned int offset;
12251
12252 /* sig_type will be NULL if the signatured type is missing from
12253 the debug info. */
12254 if (sig_type == NULL)
12255 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12256 "at 0x%x [in module %s]"),
bb5ed363 12257 die->offset, objfile->name);
673bfd45 12258
b0df02fd 12259 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12260 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12261 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12262 }
12263 else
12264 {
12265 dump_die_for_error (die);
12266 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12267 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12268 }
12269
12270 /* If not cached we need to read it in. */
12271
12272 if (this_type == NULL)
12273 {
12274 struct die_info *type_die;
12275 struct dwarf2_cu *type_cu = cu;
12276
12277 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12278 /* If the type is cached, we should have found it above. */
12279 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12280 this_type = read_type_die_1 (type_die, type_cu);
12281 }
12282
12283 /* If we still don't have a type use an error marker. */
12284
12285 if (this_type == NULL)
c906108c 12286 {
b00fdb78
TT
12287 char *message, *saved;
12288
12289 /* read_type_die already issued a complaint. */
12290 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12291 objfile->name,
b00fdb78
TT
12292 cu->header.offset,
12293 die->offset);
bb5ed363 12294 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12295 message, strlen (message));
12296 xfree (message);
12297
bb5ed363 12298 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12299 }
673bfd45 12300
f792889a 12301 return this_type;
c906108c
SS
12302}
12303
673bfd45
DE
12304/* Return the type in DIE, CU.
12305 Returns NULL for invalid types.
12306
12307 This first does a lookup in the appropriate type_hash table,
12308 and only reads the die in if necessary.
12309
12310 NOTE: This can be called when reading in partial or full symbols. */
12311
f792889a 12312static struct type *
e7c27a73 12313read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12314{
f792889a
DJ
12315 struct type *this_type;
12316
12317 this_type = get_die_type (die, cu);
12318 if (this_type)
12319 return this_type;
12320
673bfd45
DE
12321 return read_type_die_1 (die, cu);
12322}
12323
12324/* Read the type in DIE, CU.
12325 Returns NULL for invalid types. */
12326
12327static struct type *
12328read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12329{
12330 struct type *this_type = NULL;
12331
c906108c
SS
12332 switch (die->tag)
12333 {
12334 case DW_TAG_class_type:
680b30c7 12335 case DW_TAG_interface_type:
c906108c
SS
12336 case DW_TAG_structure_type:
12337 case DW_TAG_union_type:
f792889a 12338 this_type = read_structure_type (die, cu);
c906108c
SS
12339 break;
12340 case DW_TAG_enumeration_type:
f792889a 12341 this_type = read_enumeration_type (die, cu);
c906108c
SS
12342 break;
12343 case DW_TAG_subprogram:
12344 case DW_TAG_subroutine_type:
edb3359d 12345 case DW_TAG_inlined_subroutine:
f792889a 12346 this_type = read_subroutine_type (die, cu);
c906108c
SS
12347 break;
12348 case DW_TAG_array_type:
f792889a 12349 this_type = read_array_type (die, cu);
c906108c 12350 break;
72019c9c 12351 case DW_TAG_set_type:
f792889a 12352 this_type = read_set_type (die, cu);
72019c9c 12353 break;
c906108c 12354 case DW_TAG_pointer_type:
f792889a 12355 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12356 break;
12357 case DW_TAG_ptr_to_member_type:
f792889a 12358 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12359 break;
12360 case DW_TAG_reference_type:
f792889a 12361 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12362 break;
12363 case DW_TAG_const_type:
f792889a 12364 this_type = read_tag_const_type (die, cu);
c906108c
SS
12365 break;
12366 case DW_TAG_volatile_type:
f792889a 12367 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12368 break;
12369 case DW_TAG_string_type:
f792889a 12370 this_type = read_tag_string_type (die, cu);
c906108c
SS
12371 break;
12372 case DW_TAG_typedef:
f792889a 12373 this_type = read_typedef (die, cu);
c906108c 12374 break;
a02abb62 12375 case DW_TAG_subrange_type:
f792889a 12376 this_type = read_subrange_type (die, cu);
a02abb62 12377 break;
c906108c 12378 case DW_TAG_base_type:
f792889a 12379 this_type = read_base_type (die, cu);
c906108c 12380 break;
81a17f79 12381 case DW_TAG_unspecified_type:
f792889a 12382 this_type = read_unspecified_type (die, cu);
81a17f79 12383 break;
0114d602
DJ
12384 case DW_TAG_namespace:
12385 this_type = read_namespace_type (die, cu);
12386 break;
f55ee35c
JK
12387 case DW_TAG_module:
12388 this_type = read_module_type (die, cu);
12389 break;
c906108c 12390 default:
3e43a32a
MS
12391 complaint (&symfile_complaints,
12392 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12393 dwarf_tag_name (die->tag));
c906108c
SS
12394 break;
12395 }
63d06c5c 12396
f792889a 12397 return this_type;
63d06c5c
DC
12398}
12399
abc72ce4
DE
12400/* See if we can figure out if the class lives in a namespace. We do
12401 this by looking for a member function; its demangled name will
12402 contain namespace info, if there is any.
12403 Return the computed name or NULL.
12404 Space for the result is allocated on the objfile's obstack.
12405 This is the full-die version of guess_partial_die_structure_name.
12406 In this case we know DIE has no useful parent. */
12407
12408static char *
12409guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12410{
12411 struct die_info *spec_die;
12412 struct dwarf2_cu *spec_cu;
12413 struct die_info *child;
12414
12415 spec_cu = cu;
12416 spec_die = die_specification (die, &spec_cu);
12417 if (spec_die != NULL)
12418 {
12419 die = spec_die;
12420 cu = spec_cu;
12421 }
12422
12423 for (child = die->child;
12424 child != NULL;
12425 child = child->sibling)
12426 {
12427 if (child->tag == DW_TAG_subprogram)
12428 {
12429 struct attribute *attr;
12430
12431 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12432 if (attr == NULL)
12433 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12434 if (attr != NULL)
12435 {
12436 char *actual_name
12437 = language_class_name_from_physname (cu->language_defn,
12438 DW_STRING (attr));
12439 char *name = NULL;
12440
12441 if (actual_name != NULL)
12442 {
12443 char *die_name = dwarf2_name (die, cu);
12444
12445 if (die_name != NULL
12446 && strcmp (die_name, actual_name) != 0)
12447 {
12448 /* Strip off the class name from the full name.
12449 We want the prefix. */
12450 int die_name_len = strlen (die_name);
12451 int actual_name_len = strlen (actual_name);
12452
12453 /* Test for '::' as a sanity check. */
12454 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12455 && actual_name[actual_name_len
12456 - die_name_len - 1] == ':')
abc72ce4
DE
12457 name =
12458 obsavestring (actual_name,
12459 actual_name_len - die_name_len - 2,
12460 &cu->objfile->objfile_obstack);
12461 }
12462 }
12463 xfree (actual_name);
12464 return name;
12465 }
12466 }
12467 }
12468
12469 return NULL;
12470}
12471
96408a79
SA
12472/* GCC might emit a nameless typedef that has a linkage name. Determine the
12473 prefix part in such case. See
12474 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12475
12476static char *
12477anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12478{
12479 struct attribute *attr;
12480 char *base;
12481
12482 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12483 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12484 return NULL;
12485
12486 attr = dwarf2_attr (die, DW_AT_name, cu);
12487 if (attr != NULL && DW_STRING (attr) != NULL)
12488 return NULL;
12489
12490 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12491 if (attr == NULL)
12492 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12493 if (attr == NULL || DW_STRING (attr) == NULL)
12494 return NULL;
12495
12496 /* dwarf2_name had to be already called. */
12497 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12498
12499 /* Strip the base name, keep any leading namespaces/classes. */
12500 base = strrchr (DW_STRING (attr), ':');
12501 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12502 return "";
12503
12504 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12505 &cu->objfile->objfile_obstack);
12506}
12507
fdde2d81 12508/* Return the name of the namespace/class that DIE is defined within,
0114d602 12509 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12510
0114d602
DJ
12511 For example, if we're within the method foo() in the following
12512 code:
12513
12514 namespace N {
12515 class C {
12516 void foo () {
12517 }
12518 };
12519 }
12520
12521 then determine_prefix on foo's die will return "N::C". */
fdde2d81 12522
0d5cff50 12523static const char *
e142c38c 12524determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12525{
0114d602
DJ
12526 struct die_info *parent, *spec_die;
12527 struct dwarf2_cu *spec_cu;
12528 struct type *parent_type;
96408a79 12529 char *retval;
63d06c5c 12530
f55ee35c
JK
12531 if (cu->language != language_cplus && cu->language != language_java
12532 && cu->language != language_fortran)
0114d602
DJ
12533 return "";
12534
96408a79
SA
12535 retval = anonymous_struct_prefix (die, cu);
12536 if (retval)
12537 return retval;
12538
0114d602
DJ
12539 /* We have to be careful in the presence of DW_AT_specification.
12540 For example, with GCC 3.4, given the code
12541
12542 namespace N {
12543 void foo() {
12544 // Definition of N::foo.
12545 }
12546 }
12547
12548 then we'll have a tree of DIEs like this:
12549
12550 1: DW_TAG_compile_unit
12551 2: DW_TAG_namespace // N
12552 3: DW_TAG_subprogram // declaration of N::foo
12553 4: DW_TAG_subprogram // definition of N::foo
12554 DW_AT_specification // refers to die #3
12555
12556 Thus, when processing die #4, we have to pretend that we're in
12557 the context of its DW_AT_specification, namely the contex of die
12558 #3. */
12559 spec_cu = cu;
12560 spec_die = die_specification (die, &spec_cu);
12561 if (spec_die == NULL)
12562 parent = die->parent;
12563 else
63d06c5c 12564 {
0114d602
DJ
12565 parent = spec_die->parent;
12566 cu = spec_cu;
63d06c5c 12567 }
0114d602
DJ
12568
12569 if (parent == NULL)
12570 return "";
98bfdba5
PA
12571 else if (parent->building_fullname)
12572 {
12573 const char *name;
12574 const char *parent_name;
12575
12576 /* It has been seen on RealView 2.2 built binaries,
12577 DW_TAG_template_type_param types actually _defined_ as
12578 children of the parent class:
12579
12580 enum E {};
12581 template class <class Enum> Class{};
12582 Class<enum E> class_e;
12583
12584 1: DW_TAG_class_type (Class)
12585 2: DW_TAG_enumeration_type (E)
12586 3: DW_TAG_enumerator (enum1:0)
12587 3: DW_TAG_enumerator (enum2:1)
12588 ...
12589 2: DW_TAG_template_type_param
12590 DW_AT_type DW_FORM_ref_udata (E)
12591
12592 Besides being broken debug info, it can put GDB into an
12593 infinite loop. Consider:
12594
12595 When we're building the full name for Class<E>, we'll start
12596 at Class, and go look over its template type parameters,
12597 finding E. We'll then try to build the full name of E, and
12598 reach here. We're now trying to build the full name of E,
12599 and look over the parent DIE for containing scope. In the
12600 broken case, if we followed the parent DIE of E, we'd again
12601 find Class, and once again go look at its template type
12602 arguments, etc., etc. Simply don't consider such parent die
12603 as source-level parent of this die (it can't be, the language
12604 doesn't allow it), and break the loop here. */
12605 name = dwarf2_name (die, cu);
12606 parent_name = dwarf2_name (parent, cu);
12607 complaint (&symfile_complaints,
12608 _("template param type '%s' defined within parent '%s'"),
12609 name ? name : "<unknown>",
12610 parent_name ? parent_name : "<unknown>");
12611 return "";
12612 }
63d06c5c 12613 else
0114d602
DJ
12614 switch (parent->tag)
12615 {
63d06c5c 12616 case DW_TAG_namespace:
0114d602 12617 parent_type = read_type_die (parent, cu);
acebe513
UW
12618 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12619 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12620 Work around this problem here. */
12621 if (cu->language == language_cplus
12622 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12623 return "";
0114d602
DJ
12624 /* We give a name to even anonymous namespaces. */
12625 return TYPE_TAG_NAME (parent_type);
63d06c5c 12626 case DW_TAG_class_type:
680b30c7 12627 case DW_TAG_interface_type:
63d06c5c 12628 case DW_TAG_structure_type:
0114d602 12629 case DW_TAG_union_type:
f55ee35c 12630 case DW_TAG_module:
0114d602
DJ
12631 parent_type = read_type_die (parent, cu);
12632 if (TYPE_TAG_NAME (parent_type) != NULL)
12633 return TYPE_TAG_NAME (parent_type);
12634 else
12635 /* An anonymous structure is only allowed non-static data
12636 members; no typedefs, no member functions, et cetera.
12637 So it does not need a prefix. */
12638 return "";
abc72ce4
DE
12639 case DW_TAG_compile_unit:
12640 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12641 if (cu->language == language_cplus
8b70b953 12642 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12643 && die->child != NULL
12644 && (die->tag == DW_TAG_class_type
12645 || die->tag == DW_TAG_structure_type
12646 || die->tag == DW_TAG_union_type))
12647 {
12648 char *name = guess_full_die_structure_name (die, cu);
12649 if (name != NULL)
12650 return name;
12651 }
12652 return "";
63d06c5c 12653 default:
8176b9b8 12654 return determine_prefix (parent, cu);
63d06c5c 12655 }
63d06c5c
DC
12656}
12657
3e43a32a
MS
12658/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12659 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12660 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12661 an obconcat, otherwise allocate storage for the result. The CU argument is
12662 used to determine the language and hence, the appropriate separator. */
987504bb 12663
f55ee35c 12664#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12665
12666static char *
f55ee35c
JK
12667typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12668 int physname, struct dwarf2_cu *cu)
63d06c5c 12669{
f55ee35c 12670 const char *lead = "";
5c315b68 12671 const char *sep;
63d06c5c 12672
3e43a32a
MS
12673 if (suffix == NULL || suffix[0] == '\0'
12674 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12675 sep = "";
12676 else if (cu->language == language_java)
12677 sep = ".";
f55ee35c
JK
12678 else if (cu->language == language_fortran && physname)
12679 {
12680 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12681 DW_AT_MIPS_linkage_name is preferred and used instead. */
12682
12683 lead = "__";
12684 sep = "_MOD_";
12685 }
987504bb
JJ
12686 else
12687 sep = "::";
63d06c5c 12688
6dd47d34
DE
12689 if (prefix == NULL)
12690 prefix = "";
12691 if (suffix == NULL)
12692 suffix = "";
12693
987504bb
JJ
12694 if (obs == NULL)
12695 {
3e43a32a
MS
12696 char *retval
12697 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12698
f55ee35c
JK
12699 strcpy (retval, lead);
12700 strcat (retval, prefix);
6dd47d34
DE
12701 strcat (retval, sep);
12702 strcat (retval, suffix);
63d06c5c
DC
12703 return retval;
12704 }
987504bb
JJ
12705 else
12706 {
12707 /* We have an obstack. */
f55ee35c 12708 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12709 }
63d06c5c
DC
12710}
12711
c906108c
SS
12712/* Return sibling of die, NULL if no sibling. */
12713
f9aca02d 12714static struct die_info *
fba45db2 12715sibling_die (struct die_info *die)
c906108c 12716{
639d11d3 12717 return die->sibling;
c906108c
SS
12718}
12719
71c25dea
TT
12720/* Get name of a die, return NULL if not found. */
12721
12722static char *
12723dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12724 struct obstack *obstack)
12725{
12726 if (name && cu->language == language_cplus)
12727 {
12728 char *canon_name = cp_canonicalize_string (name);
12729
12730 if (canon_name != NULL)
12731 {
12732 if (strcmp (canon_name, name) != 0)
12733 name = obsavestring (canon_name, strlen (canon_name),
12734 obstack);
12735 xfree (canon_name);
12736 }
12737 }
12738
12739 return name;
c906108c
SS
12740}
12741
9219021c
DC
12742/* Get name of a die, return NULL if not found. */
12743
12744static char *
e142c38c 12745dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12746{
12747 struct attribute *attr;
12748
e142c38c 12749 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12750 if ((!attr || !DW_STRING (attr))
12751 && die->tag != DW_TAG_class_type
12752 && die->tag != DW_TAG_interface_type
12753 && die->tag != DW_TAG_structure_type
12754 && die->tag != DW_TAG_union_type)
71c25dea
TT
12755 return NULL;
12756
12757 switch (die->tag)
12758 {
12759 case DW_TAG_compile_unit:
12760 /* Compilation units have a DW_AT_name that is a filename, not
12761 a source language identifier. */
12762 case DW_TAG_enumeration_type:
12763 case DW_TAG_enumerator:
12764 /* These tags always have simple identifiers already; no need
12765 to canonicalize them. */
12766 return DW_STRING (attr);
907af001 12767
418835cc
KS
12768 case DW_TAG_subprogram:
12769 /* Java constructors will all be named "<init>", so return
12770 the class name when we see this special case. */
12771 if (cu->language == language_java
12772 && DW_STRING (attr) != NULL
12773 && strcmp (DW_STRING (attr), "<init>") == 0)
12774 {
12775 struct dwarf2_cu *spec_cu = cu;
12776 struct die_info *spec_die;
12777
12778 /* GCJ will output '<init>' for Java constructor names.
12779 For this special case, return the name of the parent class. */
12780
12781 /* GCJ may output suprogram DIEs with AT_specification set.
12782 If so, use the name of the specified DIE. */
12783 spec_die = die_specification (die, &spec_cu);
12784 if (spec_die != NULL)
12785 return dwarf2_name (spec_die, spec_cu);
12786
12787 do
12788 {
12789 die = die->parent;
12790 if (die->tag == DW_TAG_class_type)
12791 return dwarf2_name (die, cu);
12792 }
12793 while (die->tag != DW_TAG_compile_unit);
12794 }
907af001
UW
12795 break;
12796
12797 case DW_TAG_class_type:
12798 case DW_TAG_interface_type:
12799 case DW_TAG_structure_type:
12800 case DW_TAG_union_type:
12801 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12802 structures or unions. These were of the form "._%d" in GCC 4.1,
12803 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12804 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12805 if (attr && DW_STRING (attr)
12806 && (strncmp (DW_STRING (attr), "._", 2) == 0
12807 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12808 return NULL;
53832f31
TT
12809
12810 /* GCC might emit a nameless typedef that has a linkage name. See
12811 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12812 if (!attr || DW_STRING (attr) == NULL)
12813 {
df5c6c50 12814 char *demangled = NULL;
53832f31
TT
12815
12816 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12817 if (attr == NULL)
12818 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12819
12820 if (attr == NULL || DW_STRING (attr) == NULL)
12821 return NULL;
12822
df5c6c50
JK
12823 /* Avoid demangling DW_STRING (attr) the second time on a second
12824 call for the same DIE. */
12825 if (!DW_STRING_IS_CANONICAL (attr))
12826 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12827
12828 if (demangled)
12829 {
96408a79
SA
12830 char *base;
12831
53832f31 12832 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12833 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12834 &cu->objfile->objfile_obstack);
53832f31
TT
12835 DW_STRING_IS_CANONICAL (attr) = 1;
12836 xfree (demangled);
96408a79
SA
12837
12838 /* Strip any leading namespaces/classes, keep only the base name.
12839 DW_AT_name for named DIEs does not contain the prefixes. */
12840 base = strrchr (DW_STRING (attr), ':');
12841 if (base && base > DW_STRING (attr) && base[-1] == ':')
12842 return &base[1];
12843 else
12844 return DW_STRING (attr);
53832f31
TT
12845 }
12846 }
907af001
UW
12847 break;
12848
71c25dea 12849 default:
907af001
UW
12850 break;
12851 }
12852
12853 if (!DW_STRING_IS_CANONICAL (attr))
12854 {
12855 DW_STRING (attr)
12856 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12857 &cu->objfile->objfile_obstack);
12858 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12859 }
907af001 12860 return DW_STRING (attr);
9219021c
DC
12861}
12862
12863/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12864 is none. *EXT_CU is the CU containing DIE on input, and the CU
12865 containing the return value on output. */
9219021c
DC
12866
12867static struct die_info *
f2f0e013 12868dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12869{
12870 struct attribute *attr;
9219021c 12871
f2f0e013 12872 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12873 if (attr == NULL)
12874 return NULL;
12875
f2f0e013 12876 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12877}
12878
c906108c
SS
12879/* Convert a DIE tag into its string name. */
12880
12881static char *
aa1ee363 12882dwarf_tag_name (unsigned tag)
c906108c
SS
12883{
12884 switch (tag)
12885 {
12886 case DW_TAG_padding:
12887 return "DW_TAG_padding";
12888 case DW_TAG_array_type:
12889 return "DW_TAG_array_type";
12890 case DW_TAG_class_type:
12891 return "DW_TAG_class_type";
12892 case DW_TAG_entry_point:
12893 return "DW_TAG_entry_point";
12894 case DW_TAG_enumeration_type:
12895 return "DW_TAG_enumeration_type";
12896 case DW_TAG_formal_parameter:
12897 return "DW_TAG_formal_parameter";
12898 case DW_TAG_imported_declaration:
12899 return "DW_TAG_imported_declaration";
12900 case DW_TAG_label:
12901 return "DW_TAG_label";
12902 case DW_TAG_lexical_block:
12903 return "DW_TAG_lexical_block";
12904 case DW_TAG_member:
12905 return "DW_TAG_member";
12906 case DW_TAG_pointer_type:
12907 return "DW_TAG_pointer_type";
12908 case DW_TAG_reference_type:
12909 return "DW_TAG_reference_type";
12910 case DW_TAG_compile_unit:
12911 return "DW_TAG_compile_unit";
12912 case DW_TAG_string_type:
12913 return "DW_TAG_string_type";
12914 case DW_TAG_structure_type:
12915 return "DW_TAG_structure_type";
12916 case DW_TAG_subroutine_type:
12917 return "DW_TAG_subroutine_type";
12918 case DW_TAG_typedef:
12919 return "DW_TAG_typedef";
12920 case DW_TAG_union_type:
12921 return "DW_TAG_union_type";
12922 case DW_TAG_unspecified_parameters:
12923 return "DW_TAG_unspecified_parameters";
12924 case DW_TAG_variant:
12925 return "DW_TAG_variant";
12926 case DW_TAG_common_block:
12927 return "DW_TAG_common_block";
12928 case DW_TAG_common_inclusion:
12929 return "DW_TAG_common_inclusion";
12930 case DW_TAG_inheritance:
12931 return "DW_TAG_inheritance";
12932 case DW_TAG_inlined_subroutine:
12933 return "DW_TAG_inlined_subroutine";
12934 case DW_TAG_module:
12935 return "DW_TAG_module";
12936 case DW_TAG_ptr_to_member_type:
12937 return "DW_TAG_ptr_to_member_type";
12938 case DW_TAG_set_type:
12939 return "DW_TAG_set_type";
12940 case DW_TAG_subrange_type:
12941 return "DW_TAG_subrange_type";
12942 case DW_TAG_with_stmt:
12943 return "DW_TAG_with_stmt";
12944 case DW_TAG_access_declaration:
12945 return "DW_TAG_access_declaration";
12946 case DW_TAG_base_type:
12947 return "DW_TAG_base_type";
12948 case DW_TAG_catch_block:
12949 return "DW_TAG_catch_block";
12950 case DW_TAG_const_type:
12951 return "DW_TAG_const_type";
12952 case DW_TAG_constant:
12953 return "DW_TAG_constant";
12954 case DW_TAG_enumerator:
12955 return "DW_TAG_enumerator";
12956 case DW_TAG_file_type:
12957 return "DW_TAG_file_type";
12958 case DW_TAG_friend:
12959 return "DW_TAG_friend";
12960 case DW_TAG_namelist:
12961 return "DW_TAG_namelist";
12962 case DW_TAG_namelist_item:
12963 return "DW_TAG_namelist_item";
12964 case DW_TAG_packed_type:
12965 return "DW_TAG_packed_type";
12966 case DW_TAG_subprogram:
12967 return "DW_TAG_subprogram";
12968 case DW_TAG_template_type_param:
12969 return "DW_TAG_template_type_param";
12970 case DW_TAG_template_value_param:
12971 return "DW_TAG_template_value_param";
12972 case DW_TAG_thrown_type:
12973 return "DW_TAG_thrown_type";
12974 case DW_TAG_try_block:
12975 return "DW_TAG_try_block";
12976 case DW_TAG_variant_part:
12977 return "DW_TAG_variant_part";
12978 case DW_TAG_variable:
12979 return "DW_TAG_variable";
12980 case DW_TAG_volatile_type:
12981 return "DW_TAG_volatile_type";
d9fa45fe
DC
12982 case DW_TAG_dwarf_procedure:
12983 return "DW_TAG_dwarf_procedure";
12984 case DW_TAG_restrict_type:
12985 return "DW_TAG_restrict_type";
12986 case DW_TAG_interface_type:
12987 return "DW_TAG_interface_type";
12988 case DW_TAG_namespace:
12989 return "DW_TAG_namespace";
12990 case DW_TAG_imported_module:
12991 return "DW_TAG_imported_module";
12992 case DW_TAG_unspecified_type:
12993 return "DW_TAG_unspecified_type";
12994 case DW_TAG_partial_unit:
12995 return "DW_TAG_partial_unit";
12996 case DW_TAG_imported_unit:
12997 return "DW_TAG_imported_unit";
b7619582
GF
12998 case DW_TAG_condition:
12999 return "DW_TAG_condition";
13000 case DW_TAG_shared_type:
13001 return "DW_TAG_shared_type";
348e048f
DE
13002 case DW_TAG_type_unit:
13003 return "DW_TAG_type_unit";
c906108c
SS
13004 case DW_TAG_MIPS_loop:
13005 return "DW_TAG_MIPS_loop";
b7619582
GF
13006 case DW_TAG_HP_array_descriptor:
13007 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13008 case DW_TAG_format_label:
13009 return "DW_TAG_format_label";
13010 case DW_TAG_function_template:
13011 return "DW_TAG_function_template";
13012 case DW_TAG_class_template:
13013 return "DW_TAG_class_template";
b7619582
GF
13014 case DW_TAG_GNU_BINCL:
13015 return "DW_TAG_GNU_BINCL";
13016 case DW_TAG_GNU_EINCL:
13017 return "DW_TAG_GNU_EINCL";
13018 case DW_TAG_upc_shared_type:
13019 return "DW_TAG_upc_shared_type";
13020 case DW_TAG_upc_strict_type:
13021 return "DW_TAG_upc_strict_type";
13022 case DW_TAG_upc_relaxed_type:
13023 return "DW_TAG_upc_relaxed_type";
13024 case DW_TAG_PGI_kanji_type:
13025 return "DW_TAG_PGI_kanji_type";
13026 case DW_TAG_PGI_interface_block:
13027 return "DW_TAG_PGI_interface_block";
96408a79
SA
13028 case DW_TAG_GNU_call_site:
13029 return "DW_TAG_GNU_call_site";
c906108c
SS
13030 default:
13031 return "DW_TAG_<unknown>";
13032 }
13033}
13034
13035/* Convert a DWARF attribute code into its string name. */
13036
13037static char *
aa1ee363 13038dwarf_attr_name (unsigned attr)
c906108c
SS
13039{
13040 switch (attr)
13041 {
13042 case DW_AT_sibling:
13043 return "DW_AT_sibling";
13044 case DW_AT_location:
13045 return "DW_AT_location";
13046 case DW_AT_name:
13047 return "DW_AT_name";
13048 case DW_AT_ordering:
13049 return "DW_AT_ordering";
13050 case DW_AT_subscr_data:
13051 return "DW_AT_subscr_data";
13052 case DW_AT_byte_size:
13053 return "DW_AT_byte_size";
13054 case DW_AT_bit_offset:
13055 return "DW_AT_bit_offset";
13056 case DW_AT_bit_size:
13057 return "DW_AT_bit_size";
13058 case DW_AT_element_list:
13059 return "DW_AT_element_list";
13060 case DW_AT_stmt_list:
13061 return "DW_AT_stmt_list";
13062 case DW_AT_low_pc:
13063 return "DW_AT_low_pc";
13064 case DW_AT_high_pc:
13065 return "DW_AT_high_pc";
13066 case DW_AT_language:
13067 return "DW_AT_language";
13068 case DW_AT_member:
13069 return "DW_AT_member";
13070 case DW_AT_discr:
13071 return "DW_AT_discr";
13072 case DW_AT_discr_value:
13073 return "DW_AT_discr_value";
13074 case DW_AT_visibility:
13075 return "DW_AT_visibility";
13076 case DW_AT_import:
13077 return "DW_AT_import";
13078 case DW_AT_string_length:
13079 return "DW_AT_string_length";
13080 case DW_AT_common_reference:
13081 return "DW_AT_common_reference";
13082 case DW_AT_comp_dir:
13083 return "DW_AT_comp_dir";
13084 case DW_AT_const_value:
13085 return "DW_AT_const_value";
13086 case DW_AT_containing_type:
13087 return "DW_AT_containing_type";
13088 case DW_AT_default_value:
13089 return "DW_AT_default_value";
13090 case DW_AT_inline:
13091 return "DW_AT_inline";
13092 case DW_AT_is_optional:
13093 return "DW_AT_is_optional";
13094 case DW_AT_lower_bound:
13095 return "DW_AT_lower_bound";
13096 case DW_AT_producer:
13097 return "DW_AT_producer";
13098 case DW_AT_prototyped:
13099 return "DW_AT_prototyped";
13100 case DW_AT_return_addr:
13101 return "DW_AT_return_addr";
13102 case DW_AT_start_scope:
13103 return "DW_AT_start_scope";
09fa0d7c
JK
13104 case DW_AT_bit_stride:
13105 return "DW_AT_bit_stride";
c906108c
SS
13106 case DW_AT_upper_bound:
13107 return "DW_AT_upper_bound";
13108 case DW_AT_abstract_origin:
13109 return "DW_AT_abstract_origin";
13110 case DW_AT_accessibility:
13111 return "DW_AT_accessibility";
13112 case DW_AT_address_class:
13113 return "DW_AT_address_class";
13114 case DW_AT_artificial:
13115 return "DW_AT_artificial";
13116 case DW_AT_base_types:
13117 return "DW_AT_base_types";
13118 case DW_AT_calling_convention:
13119 return "DW_AT_calling_convention";
13120 case DW_AT_count:
13121 return "DW_AT_count";
13122 case DW_AT_data_member_location:
13123 return "DW_AT_data_member_location";
13124 case DW_AT_decl_column:
13125 return "DW_AT_decl_column";
13126 case DW_AT_decl_file:
13127 return "DW_AT_decl_file";
13128 case DW_AT_decl_line:
13129 return "DW_AT_decl_line";
13130 case DW_AT_declaration:
13131 return "DW_AT_declaration";
13132 case DW_AT_discr_list:
13133 return "DW_AT_discr_list";
13134 case DW_AT_encoding:
13135 return "DW_AT_encoding";
13136 case DW_AT_external:
13137 return "DW_AT_external";
13138 case DW_AT_frame_base:
13139 return "DW_AT_frame_base";
13140 case DW_AT_friend:
13141 return "DW_AT_friend";
13142 case DW_AT_identifier_case:
13143 return "DW_AT_identifier_case";
13144 case DW_AT_macro_info:
13145 return "DW_AT_macro_info";
13146 case DW_AT_namelist_items:
13147 return "DW_AT_namelist_items";
13148 case DW_AT_priority:
13149 return "DW_AT_priority";
13150 case DW_AT_segment:
13151 return "DW_AT_segment";
13152 case DW_AT_specification:
13153 return "DW_AT_specification";
13154 case DW_AT_static_link:
13155 return "DW_AT_static_link";
13156 case DW_AT_type:
13157 return "DW_AT_type";
13158 case DW_AT_use_location:
13159 return "DW_AT_use_location";
13160 case DW_AT_variable_parameter:
13161 return "DW_AT_variable_parameter";
13162 case DW_AT_virtuality:
13163 return "DW_AT_virtuality";
13164 case DW_AT_vtable_elem_location:
13165 return "DW_AT_vtable_elem_location";
b7619582 13166 /* DWARF 3 values. */
d9fa45fe
DC
13167 case DW_AT_allocated:
13168 return "DW_AT_allocated";
13169 case DW_AT_associated:
13170 return "DW_AT_associated";
13171 case DW_AT_data_location:
13172 return "DW_AT_data_location";
09fa0d7c
JK
13173 case DW_AT_byte_stride:
13174 return "DW_AT_byte_stride";
d9fa45fe
DC
13175 case DW_AT_entry_pc:
13176 return "DW_AT_entry_pc";
13177 case DW_AT_use_UTF8:
13178 return "DW_AT_use_UTF8";
13179 case DW_AT_extension:
13180 return "DW_AT_extension";
13181 case DW_AT_ranges:
13182 return "DW_AT_ranges";
13183 case DW_AT_trampoline:
13184 return "DW_AT_trampoline";
13185 case DW_AT_call_column:
13186 return "DW_AT_call_column";
13187 case DW_AT_call_file:
13188 return "DW_AT_call_file";
13189 case DW_AT_call_line:
13190 return "DW_AT_call_line";
b7619582
GF
13191 case DW_AT_description:
13192 return "DW_AT_description";
13193 case DW_AT_binary_scale:
13194 return "DW_AT_binary_scale";
13195 case DW_AT_decimal_scale:
13196 return "DW_AT_decimal_scale";
13197 case DW_AT_small:
13198 return "DW_AT_small";
13199 case DW_AT_decimal_sign:
13200 return "DW_AT_decimal_sign";
13201 case DW_AT_digit_count:
13202 return "DW_AT_digit_count";
13203 case DW_AT_picture_string:
13204 return "DW_AT_picture_string";
13205 case DW_AT_mutable:
13206 return "DW_AT_mutable";
13207 case DW_AT_threads_scaled:
13208 return "DW_AT_threads_scaled";
13209 case DW_AT_explicit:
13210 return "DW_AT_explicit";
13211 case DW_AT_object_pointer:
13212 return "DW_AT_object_pointer";
13213 case DW_AT_endianity:
13214 return "DW_AT_endianity";
13215 case DW_AT_elemental:
13216 return "DW_AT_elemental";
13217 case DW_AT_pure:
13218 return "DW_AT_pure";
13219 case DW_AT_recursive:
13220 return "DW_AT_recursive";
348e048f
DE
13221 /* DWARF 4 values. */
13222 case DW_AT_signature:
13223 return "DW_AT_signature";
31ef98ae
TT
13224 case DW_AT_linkage_name:
13225 return "DW_AT_linkage_name";
b7619582 13226 /* SGI/MIPS extensions. */
c764a876 13227#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13228 case DW_AT_MIPS_fde:
13229 return "DW_AT_MIPS_fde";
c764a876 13230#endif
c906108c
SS
13231 case DW_AT_MIPS_loop_begin:
13232 return "DW_AT_MIPS_loop_begin";
13233 case DW_AT_MIPS_tail_loop_begin:
13234 return "DW_AT_MIPS_tail_loop_begin";
13235 case DW_AT_MIPS_epilog_begin:
13236 return "DW_AT_MIPS_epilog_begin";
13237 case DW_AT_MIPS_loop_unroll_factor:
13238 return "DW_AT_MIPS_loop_unroll_factor";
13239 case DW_AT_MIPS_software_pipeline_depth:
13240 return "DW_AT_MIPS_software_pipeline_depth";
13241 case DW_AT_MIPS_linkage_name:
13242 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13243 case DW_AT_MIPS_stride:
13244 return "DW_AT_MIPS_stride";
13245 case DW_AT_MIPS_abstract_name:
13246 return "DW_AT_MIPS_abstract_name";
13247 case DW_AT_MIPS_clone_origin:
13248 return "DW_AT_MIPS_clone_origin";
13249 case DW_AT_MIPS_has_inlines:
13250 return "DW_AT_MIPS_has_inlines";
b7619582 13251 /* HP extensions. */
c764a876 13252#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13253 case DW_AT_HP_block_index:
13254 return "DW_AT_HP_block_index";
c764a876 13255#endif
b7619582
GF
13256 case DW_AT_HP_unmodifiable:
13257 return "DW_AT_HP_unmodifiable";
13258 case DW_AT_HP_actuals_stmt_list:
13259 return "DW_AT_HP_actuals_stmt_list";
13260 case DW_AT_HP_proc_per_section:
13261 return "DW_AT_HP_proc_per_section";
13262 case DW_AT_HP_raw_data_ptr:
13263 return "DW_AT_HP_raw_data_ptr";
13264 case DW_AT_HP_pass_by_reference:
13265 return "DW_AT_HP_pass_by_reference";
13266 case DW_AT_HP_opt_level:
13267 return "DW_AT_HP_opt_level";
13268 case DW_AT_HP_prof_version_id:
13269 return "DW_AT_HP_prof_version_id";
13270 case DW_AT_HP_opt_flags:
13271 return "DW_AT_HP_opt_flags";
13272 case DW_AT_HP_cold_region_low_pc:
13273 return "DW_AT_HP_cold_region_low_pc";
13274 case DW_AT_HP_cold_region_high_pc:
13275 return "DW_AT_HP_cold_region_high_pc";
13276 case DW_AT_HP_all_variables_modifiable:
13277 return "DW_AT_HP_all_variables_modifiable";
13278 case DW_AT_HP_linkage_name:
13279 return "DW_AT_HP_linkage_name";
13280 case DW_AT_HP_prof_flags:
13281 return "DW_AT_HP_prof_flags";
13282 /* GNU extensions. */
c906108c
SS
13283 case DW_AT_sf_names:
13284 return "DW_AT_sf_names";
13285 case DW_AT_src_info:
13286 return "DW_AT_src_info";
13287 case DW_AT_mac_info:
13288 return "DW_AT_mac_info";
13289 case DW_AT_src_coords:
13290 return "DW_AT_src_coords";
13291 case DW_AT_body_begin:
13292 return "DW_AT_body_begin";
13293 case DW_AT_body_end:
13294 return "DW_AT_body_end";
f5f8a009
EZ
13295 case DW_AT_GNU_vector:
13296 return "DW_AT_GNU_vector";
2de00c64
DE
13297 case DW_AT_GNU_odr_signature:
13298 return "DW_AT_GNU_odr_signature";
b7619582
GF
13299 /* VMS extensions. */
13300 case DW_AT_VMS_rtnbeg_pd_address:
13301 return "DW_AT_VMS_rtnbeg_pd_address";
13302 /* UPC extension. */
13303 case DW_AT_upc_threads_scaled:
13304 return "DW_AT_upc_threads_scaled";
13305 /* PGI (STMicroelectronics) extensions. */
13306 case DW_AT_PGI_lbase:
13307 return "DW_AT_PGI_lbase";
13308 case DW_AT_PGI_soffset:
13309 return "DW_AT_PGI_soffset";
13310 case DW_AT_PGI_lstride:
13311 return "DW_AT_PGI_lstride";
c906108c
SS
13312 default:
13313 return "DW_AT_<unknown>";
13314 }
13315}
13316
13317/* Convert a DWARF value form code into its string name. */
13318
13319static char *
aa1ee363 13320dwarf_form_name (unsigned form)
c906108c
SS
13321{
13322 switch (form)
13323 {
13324 case DW_FORM_addr:
13325 return "DW_FORM_addr";
13326 case DW_FORM_block2:
13327 return "DW_FORM_block2";
13328 case DW_FORM_block4:
13329 return "DW_FORM_block4";
13330 case DW_FORM_data2:
13331 return "DW_FORM_data2";
13332 case DW_FORM_data4:
13333 return "DW_FORM_data4";
13334 case DW_FORM_data8:
13335 return "DW_FORM_data8";
13336 case DW_FORM_string:
13337 return "DW_FORM_string";
13338 case DW_FORM_block:
13339 return "DW_FORM_block";
13340 case DW_FORM_block1:
13341 return "DW_FORM_block1";
13342 case DW_FORM_data1:
13343 return "DW_FORM_data1";
13344 case DW_FORM_flag:
13345 return "DW_FORM_flag";
13346 case DW_FORM_sdata:
13347 return "DW_FORM_sdata";
13348 case DW_FORM_strp:
13349 return "DW_FORM_strp";
13350 case DW_FORM_udata:
13351 return "DW_FORM_udata";
13352 case DW_FORM_ref_addr:
13353 return "DW_FORM_ref_addr";
13354 case DW_FORM_ref1:
13355 return "DW_FORM_ref1";
13356 case DW_FORM_ref2:
13357 return "DW_FORM_ref2";
13358 case DW_FORM_ref4:
13359 return "DW_FORM_ref4";
13360 case DW_FORM_ref8:
13361 return "DW_FORM_ref8";
13362 case DW_FORM_ref_udata:
13363 return "DW_FORM_ref_udata";
13364 case DW_FORM_indirect:
13365 return "DW_FORM_indirect";
348e048f
DE
13366 case DW_FORM_sec_offset:
13367 return "DW_FORM_sec_offset";
13368 case DW_FORM_exprloc:
13369 return "DW_FORM_exprloc";
13370 case DW_FORM_flag_present:
13371 return "DW_FORM_flag_present";
55f1336d
TT
13372 case DW_FORM_ref_sig8:
13373 return "DW_FORM_ref_sig8";
c906108c
SS
13374 default:
13375 return "DW_FORM_<unknown>";
13376 }
13377}
13378
13379/* Convert a DWARF stack opcode into its string name. */
13380
9eae7c52 13381const char *
b1bfef65 13382dwarf_stack_op_name (unsigned op)
c906108c
SS
13383{
13384 switch (op)
13385 {
13386 case DW_OP_addr:
13387 return "DW_OP_addr";
13388 case DW_OP_deref:
13389 return "DW_OP_deref";
13390 case DW_OP_const1u:
13391 return "DW_OP_const1u";
13392 case DW_OP_const1s:
13393 return "DW_OP_const1s";
13394 case DW_OP_const2u:
13395 return "DW_OP_const2u";
13396 case DW_OP_const2s:
13397 return "DW_OP_const2s";
13398 case DW_OP_const4u:
13399 return "DW_OP_const4u";
13400 case DW_OP_const4s:
13401 return "DW_OP_const4s";
13402 case DW_OP_const8u:
13403 return "DW_OP_const8u";
13404 case DW_OP_const8s:
13405 return "DW_OP_const8s";
13406 case DW_OP_constu:
13407 return "DW_OP_constu";
13408 case DW_OP_consts:
13409 return "DW_OP_consts";
13410 case DW_OP_dup:
13411 return "DW_OP_dup";
13412 case DW_OP_drop:
13413 return "DW_OP_drop";
13414 case DW_OP_over:
13415 return "DW_OP_over";
13416 case DW_OP_pick:
13417 return "DW_OP_pick";
13418 case DW_OP_swap:
13419 return "DW_OP_swap";
13420 case DW_OP_rot:
13421 return "DW_OP_rot";
13422 case DW_OP_xderef:
13423 return "DW_OP_xderef";
13424 case DW_OP_abs:
13425 return "DW_OP_abs";
13426 case DW_OP_and:
13427 return "DW_OP_and";
13428 case DW_OP_div:
13429 return "DW_OP_div";
13430 case DW_OP_minus:
13431 return "DW_OP_minus";
13432 case DW_OP_mod:
13433 return "DW_OP_mod";
13434 case DW_OP_mul:
13435 return "DW_OP_mul";
13436 case DW_OP_neg:
13437 return "DW_OP_neg";
13438 case DW_OP_not:
13439 return "DW_OP_not";
13440 case DW_OP_or:
13441 return "DW_OP_or";
13442 case DW_OP_plus:
13443 return "DW_OP_plus";
13444 case DW_OP_plus_uconst:
13445 return "DW_OP_plus_uconst";
13446 case DW_OP_shl:
13447 return "DW_OP_shl";
13448 case DW_OP_shr:
13449 return "DW_OP_shr";
13450 case DW_OP_shra:
13451 return "DW_OP_shra";
13452 case DW_OP_xor:
13453 return "DW_OP_xor";
13454 case DW_OP_bra:
13455 return "DW_OP_bra";
13456 case DW_OP_eq:
13457 return "DW_OP_eq";
13458 case DW_OP_ge:
13459 return "DW_OP_ge";
13460 case DW_OP_gt:
13461 return "DW_OP_gt";
13462 case DW_OP_le:
13463 return "DW_OP_le";
13464 case DW_OP_lt:
13465 return "DW_OP_lt";
13466 case DW_OP_ne:
13467 return "DW_OP_ne";
13468 case DW_OP_skip:
13469 return "DW_OP_skip";
13470 case DW_OP_lit0:
13471 return "DW_OP_lit0";
13472 case DW_OP_lit1:
13473 return "DW_OP_lit1";
13474 case DW_OP_lit2:
13475 return "DW_OP_lit2";
13476 case DW_OP_lit3:
13477 return "DW_OP_lit3";
13478 case DW_OP_lit4:
13479 return "DW_OP_lit4";
13480 case DW_OP_lit5:
13481 return "DW_OP_lit5";
13482 case DW_OP_lit6:
13483 return "DW_OP_lit6";
13484 case DW_OP_lit7:
13485 return "DW_OP_lit7";
13486 case DW_OP_lit8:
13487 return "DW_OP_lit8";
13488 case DW_OP_lit9:
13489 return "DW_OP_lit9";
13490 case DW_OP_lit10:
13491 return "DW_OP_lit10";
13492 case DW_OP_lit11:
13493 return "DW_OP_lit11";
13494 case DW_OP_lit12:
13495 return "DW_OP_lit12";
13496 case DW_OP_lit13:
13497 return "DW_OP_lit13";
13498 case DW_OP_lit14:
13499 return "DW_OP_lit14";
13500 case DW_OP_lit15:
13501 return "DW_OP_lit15";
13502 case DW_OP_lit16:
13503 return "DW_OP_lit16";
13504 case DW_OP_lit17:
13505 return "DW_OP_lit17";
13506 case DW_OP_lit18:
13507 return "DW_OP_lit18";
13508 case DW_OP_lit19:
13509 return "DW_OP_lit19";
13510 case DW_OP_lit20:
13511 return "DW_OP_lit20";
13512 case DW_OP_lit21:
13513 return "DW_OP_lit21";
13514 case DW_OP_lit22:
13515 return "DW_OP_lit22";
13516 case DW_OP_lit23:
13517 return "DW_OP_lit23";
13518 case DW_OP_lit24:
13519 return "DW_OP_lit24";
13520 case DW_OP_lit25:
13521 return "DW_OP_lit25";
13522 case DW_OP_lit26:
13523 return "DW_OP_lit26";
13524 case DW_OP_lit27:
13525 return "DW_OP_lit27";
13526 case DW_OP_lit28:
13527 return "DW_OP_lit28";
13528 case DW_OP_lit29:
13529 return "DW_OP_lit29";
13530 case DW_OP_lit30:
13531 return "DW_OP_lit30";
13532 case DW_OP_lit31:
13533 return "DW_OP_lit31";
13534 case DW_OP_reg0:
13535 return "DW_OP_reg0";
13536 case DW_OP_reg1:
13537 return "DW_OP_reg1";
13538 case DW_OP_reg2:
13539 return "DW_OP_reg2";
13540 case DW_OP_reg3:
13541 return "DW_OP_reg3";
13542 case DW_OP_reg4:
13543 return "DW_OP_reg4";
13544 case DW_OP_reg5:
13545 return "DW_OP_reg5";
13546 case DW_OP_reg6:
13547 return "DW_OP_reg6";
13548 case DW_OP_reg7:
13549 return "DW_OP_reg7";
13550 case DW_OP_reg8:
13551 return "DW_OP_reg8";
13552 case DW_OP_reg9:
13553 return "DW_OP_reg9";
13554 case DW_OP_reg10:
13555 return "DW_OP_reg10";
13556 case DW_OP_reg11:
13557 return "DW_OP_reg11";
13558 case DW_OP_reg12:
13559 return "DW_OP_reg12";
13560 case DW_OP_reg13:
13561 return "DW_OP_reg13";
13562 case DW_OP_reg14:
13563 return "DW_OP_reg14";
13564 case DW_OP_reg15:
13565 return "DW_OP_reg15";
13566 case DW_OP_reg16:
13567 return "DW_OP_reg16";
13568 case DW_OP_reg17:
13569 return "DW_OP_reg17";
13570 case DW_OP_reg18:
13571 return "DW_OP_reg18";
13572 case DW_OP_reg19:
13573 return "DW_OP_reg19";
13574 case DW_OP_reg20:
13575 return "DW_OP_reg20";
13576 case DW_OP_reg21:
13577 return "DW_OP_reg21";
13578 case DW_OP_reg22:
13579 return "DW_OP_reg22";
13580 case DW_OP_reg23:
13581 return "DW_OP_reg23";
13582 case DW_OP_reg24:
13583 return "DW_OP_reg24";
13584 case DW_OP_reg25:
13585 return "DW_OP_reg25";
13586 case DW_OP_reg26:
13587 return "DW_OP_reg26";
13588 case DW_OP_reg27:
13589 return "DW_OP_reg27";
13590 case DW_OP_reg28:
13591 return "DW_OP_reg28";
13592 case DW_OP_reg29:
13593 return "DW_OP_reg29";
13594 case DW_OP_reg30:
13595 return "DW_OP_reg30";
13596 case DW_OP_reg31:
13597 return "DW_OP_reg31";
13598 case DW_OP_breg0:
13599 return "DW_OP_breg0";
13600 case DW_OP_breg1:
13601 return "DW_OP_breg1";
13602 case DW_OP_breg2:
13603 return "DW_OP_breg2";
13604 case DW_OP_breg3:
13605 return "DW_OP_breg3";
13606 case DW_OP_breg4:
13607 return "DW_OP_breg4";
13608 case DW_OP_breg5:
13609 return "DW_OP_breg5";
13610 case DW_OP_breg6:
13611 return "DW_OP_breg6";
13612 case DW_OP_breg7:
13613 return "DW_OP_breg7";
13614 case DW_OP_breg8:
13615 return "DW_OP_breg8";
13616 case DW_OP_breg9:
13617 return "DW_OP_breg9";
13618 case DW_OP_breg10:
13619 return "DW_OP_breg10";
13620 case DW_OP_breg11:
13621 return "DW_OP_breg11";
13622 case DW_OP_breg12:
13623 return "DW_OP_breg12";
13624 case DW_OP_breg13:
13625 return "DW_OP_breg13";
13626 case DW_OP_breg14:
13627 return "DW_OP_breg14";
13628 case DW_OP_breg15:
13629 return "DW_OP_breg15";
13630 case DW_OP_breg16:
13631 return "DW_OP_breg16";
13632 case DW_OP_breg17:
13633 return "DW_OP_breg17";
13634 case DW_OP_breg18:
13635 return "DW_OP_breg18";
13636 case DW_OP_breg19:
13637 return "DW_OP_breg19";
13638 case DW_OP_breg20:
13639 return "DW_OP_breg20";
13640 case DW_OP_breg21:
13641 return "DW_OP_breg21";
13642 case DW_OP_breg22:
13643 return "DW_OP_breg22";
13644 case DW_OP_breg23:
13645 return "DW_OP_breg23";
13646 case DW_OP_breg24:
13647 return "DW_OP_breg24";
13648 case DW_OP_breg25:
13649 return "DW_OP_breg25";
13650 case DW_OP_breg26:
13651 return "DW_OP_breg26";
13652 case DW_OP_breg27:
13653 return "DW_OP_breg27";
13654 case DW_OP_breg28:
13655 return "DW_OP_breg28";
13656 case DW_OP_breg29:
13657 return "DW_OP_breg29";
13658 case DW_OP_breg30:
13659 return "DW_OP_breg30";
13660 case DW_OP_breg31:
13661 return "DW_OP_breg31";
13662 case DW_OP_regx:
13663 return "DW_OP_regx";
13664 case DW_OP_fbreg:
13665 return "DW_OP_fbreg";
13666 case DW_OP_bregx:
13667 return "DW_OP_bregx";
13668 case DW_OP_piece:
13669 return "DW_OP_piece";
13670 case DW_OP_deref_size:
13671 return "DW_OP_deref_size";
13672 case DW_OP_xderef_size:
13673 return "DW_OP_xderef_size";
13674 case DW_OP_nop:
13675 return "DW_OP_nop";
b7619582 13676 /* DWARF 3 extensions. */
ed348acc
EZ
13677 case DW_OP_push_object_address:
13678 return "DW_OP_push_object_address";
13679 case DW_OP_call2:
13680 return "DW_OP_call2";
13681 case DW_OP_call4:
13682 return "DW_OP_call4";
13683 case DW_OP_call_ref:
13684 return "DW_OP_call_ref";
b7619582
GF
13685 case DW_OP_form_tls_address:
13686 return "DW_OP_form_tls_address";
13687 case DW_OP_call_frame_cfa:
13688 return "DW_OP_call_frame_cfa";
13689 case DW_OP_bit_piece:
13690 return "DW_OP_bit_piece";
9eae7c52
TT
13691 /* DWARF 4 extensions. */
13692 case DW_OP_implicit_value:
13693 return "DW_OP_implicit_value";
13694 case DW_OP_stack_value:
13695 return "DW_OP_stack_value";
13696 /* GNU extensions. */
ed348acc
EZ
13697 case DW_OP_GNU_push_tls_address:
13698 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13699 case DW_OP_GNU_uninit:
13700 return "DW_OP_GNU_uninit";
589b4a32
DE
13701 case DW_OP_GNU_encoded_addr:
13702 return "DW_OP_GNU_encoded_addr";
8cf6f0b1
TT
13703 case DW_OP_GNU_implicit_pointer:
13704 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13705 case DW_OP_GNU_entry_value:
13706 return "DW_OP_GNU_entry_value";
13707 case DW_OP_GNU_const_type:
13708 return "DW_OP_GNU_const_type";
13709 case DW_OP_GNU_regval_type:
13710 return "DW_OP_GNU_regval_type";
13711 case DW_OP_GNU_deref_type:
13712 return "DW_OP_GNU_deref_type";
13713 case DW_OP_GNU_convert:
13714 return "DW_OP_GNU_convert";
13715 case DW_OP_GNU_reinterpret:
13716 return "DW_OP_GNU_reinterpret";
589b4a32
DE
13717 case DW_OP_GNU_parameter_ref:
13718 return "DW_OP_GNU_parameter_ref";
c906108c 13719 default:
b1bfef65 13720 return NULL;
c906108c
SS
13721 }
13722}
13723
13724static char *
fba45db2 13725dwarf_bool_name (unsigned mybool)
c906108c
SS
13726{
13727 if (mybool)
13728 return "TRUE";
13729 else
13730 return "FALSE";
13731}
13732
13733/* Convert a DWARF type code into its string name. */
13734
13735static char *
aa1ee363 13736dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13737{
13738 switch (enc)
13739 {
b7619582
GF
13740 case DW_ATE_void:
13741 return "DW_ATE_void";
c906108c
SS
13742 case DW_ATE_address:
13743 return "DW_ATE_address";
13744 case DW_ATE_boolean:
13745 return "DW_ATE_boolean";
13746 case DW_ATE_complex_float:
13747 return "DW_ATE_complex_float";
13748 case DW_ATE_float:
13749 return "DW_ATE_float";
13750 case DW_ATE_signed:
13751 return "DW_ATE_signed";
13752 case DW_ATE_signed_char:
13753 return "DW_ATE_signed_char";
13754 case DW_ATE_unsigned:
13755 return "DW_ATE_unsigned";
13756 case DW_ATE_unsigned_char:
13757 return "DW_ATE_unsigned_char";
b7619582 13758 /* DWARF 3. */
d9fa45fe
DC
13759 case DW_ATE_imaginary_float:
13760 return "DW_ATE_imaginary_float";
b7619582
GF
13761 case DW_ATE_packed_decimal:
13762 return "DW_ATE_packed_decimal";
13763 case DW_ATE_numeric_string:
13764 return "DW_ATE_numeric_string";
13765 case DW_ATE_edited:
13766 return "DW_ATE_edited";
13767 case DW_ATE_signed_fixed:
13768 return "DW_ATE_signed_fixed";
13769 case DW_ATE_unsigned_fixed:
13770 return "DW_ATE_unsigned_fixed";
13771 case DW_ATE_decimal_float:
13772 return "DW_ATE_decimal_float";
75079b2b
TT
13773 /* DWARF 4. */
13774 case DW_ATE_UTF:
13775 return "DW_ATE_UTF";
b7619582
GF
13776 /* HP extensions. */
13777 case DW_ATE_HP_float80:
13778 return "DW_ATE_HP_float80";
13779 case DW_ATE_HP_complex_float80:
13780 return "DW_ATE_HP_complex_float80";
13781 case DW_ATE_HP_float128:
13782 return "DW_ATE_HP_float128";
13783 case DW_ATE_HP_complex_float128:
13784 return "DW_ATE_HP_complex_float128";
13785 case DW_ATE_HP_floathpintel:
13786 return "DW_ATE_HP_floathpintel";
13787 case DW_ATE_HP_imaginary_float80:
13788 return "DW_ATE_HP_imaginary_float80";
13789 case DW_ATE_HP_imaginary_float128:
13790 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13791 default:
13792 return "DW_ATE_<unknown>";
13793 }
13794}
13795
0963b4bd 13796/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13797
13798#if 0
13799static char *
aa1ee363 13800dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13801{
13802 switch (cfi_opc)
13803 {
13804 case DW_CFA_advance_loc:
13805 return "DW_CFA_advance_loc";
13806 case DW_CFA_offset:
13807 return "DW_CFA_offset";
13808 case DW_CFA_restore:
13809 return "DW_CFA_restore";
13810 case DW_CFA_nop:
13811 return "DW_CFA_nop";
13812 case DW_CFA_set_loc:
13813 return "DW_CFA_set_loc";
13814 case DW_CFA_advance_loc1:
13815 return "DW_CFA_advance_loc1";
13816 case DW_CFA_advance_loc2:
13817 return "DW_CFA_advance_loc2";
13818 case DW_CFA_advance_loc4:
13819 return "DW_CFA_advance_loc4";
13820 case DW_CFA_offset_extended:
13821 return "DW_CFA_offset_extended";
13822 case DW_CFA_restore_extended:
13823 return "DW_CFA_restore_extended";
13824 case DW_CFA_undefined:
13825 return "DW_CFA_undefined";
13826 case DW_CFA_same_value:
13827 return "DW_CFA_same_value";
13828 case DW_CFA_register:
13829 return "DW_CFA_register";
13830 case DW_CFA_remember_state:
13831 return "DW_CFA_remember_state";
13832 case DW_CFA_restore_state:
13833 return "DW_CFA_restore_state";
13834 case DW_CFA_def_cfa:
13835 return "DW_CFA_def_cfa";
13836 case DW_CFA_def_cfa_register:
13837 return "DW_CFA_def_cfa_register";
13838 case DW_CFA_def_cfa_offset:
13839 return "DW_CFA_def_cfa_offset";
b7619582 13840 /* DWARF 3. */
985cb1a3
JM
13841 case DW_CFA_def_cfa_expression:
13842 return "DW_CFA_def_cfa_expression";
13843 case DW_CFA_expression:
13844 return "DW_CFA_expression";
13845 case DW_CFA_offset_extended_sf:
13846 return "DW_CFA_offset_extended_sf";
13847 case DW_CFA_def_cfa_sf:
13848 return "DW_CFA_def_cfa_sf";
13849 case DW_CFA_def_cfa_offset_sf:
13850 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13851 case DW_CFA_val_offset:
13852 return "DW_CFA_val_offset";
13853 case DW_CFA_val_offset_sf:
13854 return "DW_CFA_val_offset_sf";
13855 case DW_CFA_val_expression:
13856 return "DW_CFA_val_expression";
13857 /* SGI/MIPS specific. */
c906108c
SS
13858 case DW_CFA_MIPS_advance_loc8:
13859 return "DW_CFA_MIPS_advance_loc8";
b7619582 13860 /* GNU extensions. */
985cb1a3
JM
13861 case DW_CFA_GNU_window_save:
13862 return "DW_CFA_GNU_window_save";
13863 case DW_CFA_GNU_args_size:
13864 return "DW_CFA_GNU_args_size";
13865 case DW_CFA_GNU_negative_offset_extended:
13866 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13867 default:
13868 return "DW_CFA_<unknown>";
13869 }
13870}
13871#endif
13872
f9aca02d 13873static void
d97bc12b 13874dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13875{
13876 unsigned int i;
13877
d97bc12b
DE
13878 print_spaces (indent, f);
13879 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13880 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13881
13882 if (die->parent != NULL)
13883 {
13884 print_spaces (indent, f);
13885 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13886 die->parent->offset);
13887 }
13888
13889 print_spaces (indent, f);
13890 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13891 dwarf_bool_name (die->child != NULL));
c906108c 13892
d97bc12b
DE
13893 print_spaces (indent, f);
13894 fprintf_unfiltered (f, " attributes:\n");
13895
c906108c
SS
13896 for (i = 0; i < die->num_attrs; ++i)
13897 {
d97bc12b
DE
13898 print_spaces (indent, f);
13899 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13900 dwarf_attr_name (die->attrs[i].name),
13901 dwarf_form_name (die->attrs[i].form));
d97bc12b 13902
c906108c
SS
13903 switch (die->attrs[i].form)
13904 {
13905 case DW_FORM_ref_addr:
13906 case DW_FORM_addr:
d97bc12b 13907 fprintf_unfiltered (f, "address: ");
5af949e3 13908 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13909 break;
13910 case DW_FORM_block2:
13911 case DW_FORM_block4:
13912 case DW_FORM_block:
13913 case DW_FORM_block1:
3e43a32a
MS
13914 fprintf_unfiltered (f, "block: size %d",
13915 DW_BLOCK (&die->attrs[i])->size);
c906108c 13916 break;
2dc7f7b3
TT
13917 case DW_FORM_exprloc:
13918 fprintf_unfiltered (f, "expression: size %u",
13919 DW_BLOCK (&die->attrs[i])->size);
13920 break;
10b3939b
DJ
13921 case DW_FORM_ref1:
13922 case DW_FORM_ref2:
13923 case DW_FORM_ref4:
d97bc12b 13924 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13925 (long) (DW_ADDR (&die->attrs[i])));
13926 break;
c906108c
SS
13927 case DW_FORM_data1:
13928 case DW_FORM_data2:
13929 case DW_FORM_data4:
ce5d95e1 13930 case DW_FORM_data8:
c906108c
SS
13931 case DW_FORM_udata:
13932 case DW_FORM_sdata:
43bbcdc2
PH
13933 fprintf_unfiltered (f, "constant: %s",
13934 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13935 break;
2dc7f7b3
TT
13936 case DW_FORM_sec_offset:
13937 fprintf_unfiltered (f, "section offset: %s",
13938 pulongest (DW_UNSND (&die->attrs[i])));
13939 break;
55f1336d 13940 case DW_FORM_ref_sig8:
348e048f
DE
13941 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13942 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13943 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13944 else
13945 fprintf_unfiltered (f, "signatured type, offset: unknown");
13946 break;
c906108c 13947 case DW_FORM_string:
4bdf3d34 13948 case DW_FORM_strp:
8285870a 13949 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13950 DW_STRING (&die->attrs[i])
8285870a
JK
13951 ? DW_STRING (&die->attrs[i]) : "",
13952 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13953 break;
13954 case DW_FORM_flag:
13955 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13956 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13957 else
d97bc12b 13958 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13959 break;
2dc7f7b3
TT
13960 case DW_FORM_flag_present:
13961 fprintf_unfiltered (f, "flag: TRUE");
13962 break;
a8329558 13963 case DW_FORM_indirect:
0963b4bd
MS
13964 /* The reader will have reduced the indirect form to
13965 the "base form" so this form should not occur. */
3e43a32a
MS
13966 fprintf_unfiltered (f,
13967 "unexpected attribute form: DW_FORM_indirect");
a8329558 13968 break;
c906108c 13969 default:
d97bc12b 13970 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13971 die->attrs[i].form);
d97bc12b 13972 break;
c906108c 13973 }
d97bc12b 13974 fprintf_unfiltered (f, "\n");
c906108c
SS
13975 }
13976}
13977
f9aca02d 13978static void
d97bc12b 13979dump_die_for_error (struct die_info *die)
c906108c 13980{
d97bc12b
DE
13981 dump_die_shallow (gdb_stderr, 0, die);
13982}
13983
13984static void
13985dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13986{
13987 int indent = level * 4;
13988
13989 gdb_assert (die != NULL);
13990
13991 if (level >= max_level)
13992 return;
13993
13994 dump_die_shallow (f, indent, die);
13995
13996 if (die->child != NULL)
c906108c 13997 {
d97bc12b
DE
13998 print_spaces (indent, f);
13999 fprintf_unfiltered (f, " Children:");
14000 if (level + 1 < max_level)
14001 {
14002 fprintf_unfiltered (f, "\n");
14003 dump_die_1 (f, level + 1, max_level, die->child);
14004 }
14005 else
14006 {
3e43a32a
MS
14007 fprintf_unfiltered (f,
14008 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14009 }
14010 }
14011
14012 if (die->sibling != NULL && level > 0)
14013 {
14014 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14015 }
14016}
14017
d97bc12b
DE
14018/* This is called from the pdie macro in gdbinit.in.
14019 It's not static so gcc will keep a copy callable from gdb. */
14020
14021void
14022dump_die (struct die_info *die, int max_level)
14023{
14024 dump_die_1 (gdb_stdlog, 0, max_level, die);
14025}
14026
f9aca02d 14027static void
51545339 14028store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14029{
51545339 14030 void **slot;
c906108c 14031
51545339
DJ
14032 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14033
14034 *slot = die;
c906108c
SS
14035}
14036
93311388
DE
14037static int
14038is_ref_attr (struct attribute *attr)
c906108c 14039{
c906108c
SS
14040 switch (attr->form)
14041 {
14042 case DW_FORM_ref_addr:
c906108c
SS
14043 case DW_FORM_ref1:
14044 case DW_FORM_ref2:
14045 case DW_FORM_ref4:
613e1657 14046 case DW_FORM_ref8:
c906108c 14047 case DW_FORM_ref_udata:
93311388 14048 return 1;
c906108c 14049 default:
93311388 14050 return 0;
c906108c 14051 }
93311388
DE
14052}
14053
14054static unsigned int
14055dwarf2_get_ref_die_offset (struct attribute *attr)
14056{
14057 if (is_ref_attr (attr))
14058 return DW_ADDR (attr);
14059
14060 complaint (&symfile_complaints,
14061 _("unsupported die ref attribute form: '%s'"),
14062 dwarf_form_name (attr->form));
14063 return 0;
c906108c
SS
14064}
14065
43bbcdc2
PH
14066/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14067 * the value held by the attribute is not constant. */
a02abb62 14068
43bbcdc2 14069static LONGEST
a02abb62
JB
14070dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14071{
14072 if (attr->form == DW_FORM_sdata)
14073 return DW_SND (attr);
14074 else if (attr->form == DW_FORM_udata
14075 || attr->form == DW_FORM_data1
14076 || attr->form == DW_FORM_data2
14077 || attr->form == DW_FORM_data4
14078 || attr->form == DW_FORM_data8)
14079 return DW_UNSND (attr);
14080 else
14081 {
3e43a32a
MS
14082 complaint (&symfile_complaints,
14083 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14084 dwarf_form_name (attr->form));
14085 return default_value;
14086 }
14087}
14088
03dd20cc 14089/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14090 unit and add it to our queue.
14091 The result is non-zero if PER_CU was queued, otherwise the result is zero
14092 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14093
348e048f 14094static int
03dd20cc
DJ
14095maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14096 struct dwarf2_per_cu_data *per_cu)
14097{
98bfdba5
PA
14098 /* We may arrive here during partial symbol reading, if we need full
14099 DIEs to process an unusual case (e.g. template arguments). Do
14100 not queue PER_CU, just tell our caller to load its DIEs. */
14101 if (dwarf2_per_objfile->reading_partial_symbols)
14102 {
14103 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14104 return 1;
14105 return 0;
14106 }
14107
03dd20cc
DJ
14108 /* Mark the dependence relation so that we don't flush PER_CU
14109 too early. */
14110 dwarf2_add_dependence (this_cu, per_cu);
14111
14112 /* If it's already on the queue, we have nothing to do. */
14113 if (per_cu->queued)
348e048f 14114 return 0;
03dd20cc
DJ
14115
14116 /* If the compilation unit is already loaded, just mark it as
14117 used. */
14118 if (per_cu->cu != NULL)
14119 {
14120 per_cu->cu->last_used = 0;
348e048f 14121 return 0;
03dd20cc
DJ
14122 }
14123
14124 /* Add it to the queue. */
a0f42c21 14125 queue_comp_unit (per_cu);
348e048f
DE
14126
14127 return 1;
14128}
14129
14130/* Follow reference or signature attribute ATTR of SRC_DIE.
14131 On entry *REF_CU is the CU of SRC_DIE.
14132 On exit *REF_CU is the CU of the result. */
14133
14134static struct die_info *
14135follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14136 struct dwarf2_cu **ref_cu)
14137{
14138 struct die_info *die;
14139
14140 if (is_ref_attr (attr))
14141 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14142 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14143 die = follow_die_sig (src_die, attr, ref_cu);
14144 else
14145 {
14146 dump_die_for_error (src_die);
14147 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14148 (*ref_cu)->objfile->name);
14149 }
14150
14151 return die;
03dd20cc
DJ
14152}
14153
5c631832 14154/* Follow reference OFFSET.
673bfd45
DE
14155 On entry *REF_CU is the CU of the source die referencing OFFSET.
14156 On exit *REF_CU is the CU of the result.
14157 Returns NULL if OFFSET is invalid. */
f504f079 14158
f9aca02d 14159static struct die_info *
5c631832 14160follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14161{
10b3939b 14162 struct die_info temp_die;
f2f0e013 14163 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14164
348e048f
DE
14165 gdb_assert (cu->per_cu != NULL);
14166
98bfdba5
PA
14167 target_cu = cu;
14168
b0df02fd 14169 if (cu->per_cu->debug_types_section)
348e048f
DE
14170 {
14171 /* .debug_types CUs cannot reference anything outside their CU.
14172 If they need to, they have to reference a signatured type via
55f1336d 14173 DW_FORM_ref_sig8. */
348e048f 14174 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14175 return NULL;
348e048f
DE
14176 }
14177 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14178 {
14179 struct dwarf2_per_cu_data *per_cu;
9a619af0 14180
45452591 14181 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14182
14183 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14184 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14185 load_full_comp_unit (per_cu);
03dd20cc 14186
10b3939b
DJ
14187 target_cu = per_cu->cu;
14188 }
98bfdba5
PA
14189 else if (cu->dies == NULL)
14190 {
14191 /* We're loading full DIEs during partial symbol reading. */
14192 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14193 load_full_comp_unit (cu->per_cu);
98bfdba5 14194 }
c906108c 14195
f2f0e013 14196 *ref_cu = target_cu;
51545339 14197 temp_die.offset = offset;
5c631832
JK
14198 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14199}
10b3939b 14200
5c631832
JK
14201/* Follow reference attribute ATTR of SRC_DIE.
14202 On entry *REF_CU is the CU of SRC_DIE.
14203 On exit *REF_CU is the CU of the result. */
14204
14205static struct die_info *
14206follow_die_ref (struct die_info *src_die, struct attribute *attr,
14207 struct dwarf2_cu **ref_cu)
14208{
14209 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14210 struct dwarf2_cu *cu = *ref_cu;
14211 struct die_info *die;
14212
14213 die = follow_die_offset (offset, ref_cu);
14214 if (!die)
14215 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14216 "at 0x%x [in module %s]"),
14217 offset, src_die->offset, cu->objfile->name);
348e048f 14218
5c631832
JK
14219 return die;
14220}
14221
d83e736b
JK
14222/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14223 Returned value is intended for DW_OP_call*. Returned
14224 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14225
14226struct dwarf2_locexpr_baton
7fe25d9b 14227dwarf2_fetch_die_location_block (unsigned int offset_in_cu,
8cf6f0b1
TT
14228 struct dwarf2_per_cu_data *per_cu,
14229 CORE_ADDR (*get_frame_pc) (void *baton),
14230 void *baton)
5c631832 14231{
7fe25d9b 14232 unsigned int offset = per_cu->offset + offset_in_cu;
918dd910 14233 struct dwarf2_cu *cu;
5c631832
JK
14234 struct die_info *die;
14235 struct attribute *attr;
14236 struct dwarf2_locexpr_baton retval;
14237
8cf6f0b1
TT
14238 dw2_setup (per_cu->objfile);
14239
918dd910
JK
14240 if (per_cu->cu == NULL)
14241 load_cu (per_cu);
14242 cu = per_cu->cu;
14243
5c631832
JK
14244 die = follow_die_offset (offset, &cu);
14245 if (!die)
14246 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
bb5ed363 14247 offset, per_cu->objfile->name);
5c631832
JK
14248
14249 attr = dwarf2_attr (die, DW_AT_location, cu);
14250 if (!attr)
14251 {
e103e986
JK
14252 /* DWARF: "If there is no such attribute, then there is no effect.".
14253 DATA is ignored if SIZE is 0. */
5c631832 14254
e103e986 14255 retval.data = NULL;
5c631832
JK
14256 retval.size = 0;
14257 }
8cf6f0b1
TT
14258 else if (attr_form_is_section_offset (attr))
14259 {
14260 struct dwarf2_loclist_baton loclist_baton;
14261 CORE_ADDR pc = (*get_frame_pc) (baton);
14262 size_t size;
14263
14264 fill_in_loclist_baton (cu, &loclist_baton, attr);
14265
14266 retval.data = dwarf2_find_location_expression (&loclist_baton,
14267 &size, pc);
14268 retval.size = size;
14269 }
5c631832
JK
14270 else
14271 {
14272 if (!attr_form_is_block (attr))
14273 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14274 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
bb5ed363 14275 offset, per_cu->objfile->name);
5c631832
JK
14276
14277 retval.data = DW_BLOCK (attr)->data;
14278 retval.size = DW_BLOCK (attr)->size;
14279 }
14280 retval.per_cu = cu->per_cu;
918dd910 14281
918dd910
JK
14282 age_cached_comp_units ();
14283
5c631832 14284 return retval;
348e048f
DE
14285}
14286
8a9b8146
TT
14287/* Return the type of the DIE at DIE_OFFSET in the CU named by
14288 PER_CU. */
14289
14290struct type *
14291dwarf2_get_die_type (unsigned int die_offset,
14292 struct dwarf2_per_cu_data *per_cu)
14293{
8a9b8146 14294 dw2_setup (per_cu->objfile);
1281d2a3 14295 return get_die_type_at_offset (per_cu->offset + die_offset, per_cu);
8a9b8146
TT
14296}
14297
348e048f
DE
14298/* Follow the signature attribute ATTR in SRC_DIE.
14299 On entry *REF_CU is the CU of SRC_DIE.
14300 On exit *REF_CU is the CU of the result. */
14301
14302static struct die_info *
14303follow_die_sig (struct die_info *src_die, struct attribute *attr,
14304 struct dwarf2_cu **ref_cu)
14305{
14306 struct objfile *objfile = (*ref_cu)->objfile;
14307 struct die_info temp_die;
14308 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14309 struct dwarf2_cu *sig_cu;
14310 struct die_info *die;
14311
14312 /* sig_type will be NULL if the signatured type is missing from
14313 the debug info. */
14314 if (sig_type == NULL)
14315 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14316 "at 0x%x [in module %s]"),
14317 src_die->offset, objfile->name);
14318
14319 /* If necessary, add it to the queue and load its DIEs. */
14320
14321 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14322 read_signatured_type (sig_type);
348e048f
DE
14323
14324 gdb_assert (sig_type->per_cu.cu != NULL);
14325
14326 sig_cu = sig_type->per_cu.cu;
14327 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14328 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14329 if (die)
14330 {
14331 *ref_cu = sig_cu;
14332 return die;
14333 }
14334
3e43a32a
MS
14335 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14336 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14337 sig_type->type_offset, src_die->offset, objfile->name);
14338}
14339
14340/* Given an offset of a signatured type, return its signatured_type. */
14341
14342static struct signatured_type *
8b70b953
TT
14343lookup_signatured_type_at_offset (struct objfile *objfile,
14344 struct dwarf2_section_info *section,
14345 unsigned int offset)
348e048f 14346{
8b70b953 14347 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14348 unsigned int length, initial_length_size;
14349 unsigned int sig_offset;
14350 struct signatured_type find_entry, *type_sig;
14351
14352 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14353 sig_offset = (initial_length_size
14354 + 2 /*version*/
14355 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14356 + 1 /*address_size*/);
14357 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14358 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14359
14360 /* This is only used to lookup previously recorded types.
14361 If we didn't find it, it's our bug. */
14362 gdb_assert (type_sig != NULL);
b3c8eb43 14363 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14364
14365 return type_sig;
14366}
14367
e5fe5e75 14368/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14369
14370static void
e5fe5e75 14371load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14372{
e5fe5e75
DE
14373 struct objfile *objfile = per_cu->objfile;
14374 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14375 unsigned int offset = per_cu->offset;
348e048f
DE
14376 struct signatured_type *type_sig;
14377
8b70b953 14378 dwarf2_read_section (objfile, sect);
be391dca 14379
348e048f 14380 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14381 hash table lookup. */
14382 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14383 the signature to assert we found the right one.
14384 Ok, but it's a lot of work. We should simplify things so any needed
14385 assert doesn't require all this clumsiness. */
8b70b953 14386 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14387
14388 gdb_assert (type_sig->per_cu.cu == NULL);
14389
a0f42c21 14390 read_signatured_type (type_sig);
348e048f
DE
14391
14392 gdb_assert (type_sig->per_cu.cu != NULL);
14393}
14394
14395/* Read in a signatured type and build its CU and DIEs. */
14396
14397static void
a0f42c21 14398read_signatured_type (struct signatured_type *type_sig)
348e048f 14399{
a0f42c21 14400 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14401 gdb_byte *types_ptr;
348e048f
DE
14402 struct die_reader_specs reader_specs;
14403 struct dwarf2_cu *cu;
14404 ULONGEST signature;
14405 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14406 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14407
8b70b953
TT
14408 dwarf2_read_section (objfile, section);
14409 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14410
348e048f
DE
14411 gdb_assert (type_sig->per_cu.cu == NULL);
14412
9816fde3 14413 cu = xmalloc (sizeof (*cu));
23745b47 14414 init_one_comp_unit (cu, &type_sig->per_cu);
348e048f
DE
14415
14416 /* If an error occurs while loading, release our storage. */
68dc6402 14417 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14418
9ff913ba
DE
14419 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14420 &signature, NULL);
348e048f
DE
14421 gdb_assert (signature == type_sig->signature);
14422
14423 cu->die_hash
14424 = htab_create_alloc_ex (cu->header.length / 12,
14425 die_hash,
14426 die_eq,
14427 NULL,
14428 &cu->comp_unit_obstack,
14429 hashtab_obstack_allocate,
14430 dummy_obstack_deallocate);
14431
e5fe5e75 14432 dwarf2_read_abbrevs (cu);
348e048f
DE
14433 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14434
14435 init_cu_die_reader (&reader_specs, cu);
14436
14437 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14438 NULL /*parent*/);
14439
14440 /* We try not to read any attributes in this function, because not
9cdd5dbd 14441 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14442 table processing isn't initialized. But we have to set the CU language,
14443 or we won't be able to build types correctly. */
9816fde3 14444 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14445
14446 do_cleanups (back_to);
14447
14448 /* We've successfully allocated this compilation unit. Let our caller
14449 clean it up when finished with it. */
14450 discard_cleanups (free_cu_cleanup);
14451
c5b7e1cb 14452 /* Link this TU into read_in_chain. */
348e048f
DE
14453 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14454 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14455}
14456
c906108c
SS
14457/* Decode simple location descriptions.
14458 Given a pointer to a dwarf block that defines a location, compute
14459 the location and return the value.
14460
4cecd739
DJ
14461 NOTE drow/2003-11-18: This function is called in two situations
14462 now: for the address of static or global variables (partial symbols
14463 only) and for offsets into structures which are expected to be
14464 (more or less) constant. The partial symbol case should go away,
14465 and only the constant case should remain. That will let this
14466 function complain more accurately. A few special modes are allowed
14467 without complaint for global variables (for instance, global
14468 register values and thread-local values).
c906108c
SS
14469
14470 A location description containing no operations indicates that the
4cecd739 14471 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14472 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14473 callers will only want a very basic result and this can become a
21ae7a4d
JK
14474 complaint.
14475
14476 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14477
14478static CORE_ADDR
e7c27a73 14479decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14480{
e7c27a73 14481 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14482 int i;
14483 int size = blk->size;
14484 gdb_byte *data = blk->data;
14485 CORE_ADDR stack[64];
14486 int stacki;
14487 unsigned int bytes_read, unsnd;
14488 gdb_byte op;
c906108c 14489
21ae7a4d
JK
14490 i = 0;
14491 stacki = 0;
14492 stack[stacki] = 0;
14493 stack[++stacki] = 0;
14494
14495 while (i < size)
14496 {
14497 op = data[i++];
14498 switch (op)
14499 {
14500 case DW_OP_lit0:
14501 case DW_OP_lit1:
14502 case DW_OP_lit2:
14503 case DW_OP_lit3:
14504 case DW_OP_lit4:
14505 case DW_OP_lit5:
14506 case DW_OP_lit6:
14507 case DW_OP_lit7:
14508 case DW_OP_lit8:
14509 case DW_OP_lit9:
14510 case DW_OP_lit10:
14511 case DW_OP_lit11:
14512 case DW_OP_lit12:
14513 case DW_OP_lit13:
14514 case DW_OP_lit14:
14515 case DW_OP_lit15:
14516 case DW_OP_lit16:
14517 case DW_OP_lit17:
14518 case DW_OP_lit18:
14519 case DW_OP_lit19:
14520 case DW_OP_lit20:
14521 case DW_OP_lit21:
14522 case DW_OP_lit22:
14523 case DW_OP_lit23:
14524 case DW_OP_lit24:
14525 case DW_OP_lit25:
14526 case DW_OP_lit26:
14527 case DW_OP_lit27:
14528 case DW_OP_lit28:
14529 case DW_OP_lit29:
14530 case DW_OP_lit30:
14531 case DW_OP_lit31:
14532 stack[++stacki] = op - DW_OP_lit0;
14533 break;
f1bea926 14534
21ae7a4d
JK
14535 case DW_OP_reg0:
14536 case DW_OP_reg1:
14537 case DW_OP_reg2:
14538 case DW_OP_reg3:
14539 case DW_OP_reg4:
14540 case DW_OP_reg5:
14541 case DW_OP_reg6:
14542 case DW_OP_reg7:
14543 case DW_OP_reg8:
14544 case DW_OP_reg9:
14545 case DW_OP_reg10:
14546 case DW_OP_reg11:
14547 case DW_OP_reg12:
14548 case DW_OP_reg13:
14549 case DW_OP_reg14:
14550 case DW_OP_reg15:
14551 case DW_OP_reg16:
14552 case DW_OP_reg17:
14553 case DW_OP_reg18:
14554 case DW_OP_reg19:
14555 case DW_OP_reg20:
14556 case DW_OP_reg21:
14557 case DW_OP_reg22:
14558 case DW_OP_reg23:
14559 case DW_OP_reg24:
14560 case DW_OP_reg25:
14561 case DW_OP_reg26:
14562 case DW_OP_reg27:
14563 case DW_OP_reg28:
14564 case DW_OP_reg29:
14565 case DW_OP_reg30:
14566 case DW_OP_reg31:
14567 stack[++stacki] = op - DW_OP_reg0;
14568 if (i < size)
14569 dwarf2_complex_location_expr_complaint ();
14570 break;
c906108c 14571
21ae7a4d
JK
14572 case DW_OP_regx:
14573 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14574 i += bytes_read;
14575 stack[++stacki] = unsnd;
14576 if (i < size)
14577 dwarf2_complex_location_expr_complaint ();
14578 break;
c906108c 14579
21ae7a4d
JK
14580 case DW_OP_addr:
14581 stack[++stacki] = read_address (objfile->obfd, &data[i],
14582 cu, &bytes_read);
14583 i += bytes_read;
14584 break;
d53d4ac5 14585
21ae7a4d
JK
14586 case DW_OP_const1u:
14587 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14588 i += 1;
14589 break;
14590
14591 case DW_OP_const1s:
14592 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14593 i += 1;
14594 break;
14595
14596 case DW_OP_const2u:
14597 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14598 i += 2;
14599 break;
14600
14601 case DW_OP_const2s:
14602 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14603 i += 2;
14604 break;
d53d4ac5 14605
21ae7a4d
JK
14606 case DW_OP_const4u:
14607 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14608 i += 4;
14609 break;
14610
14611 case DW_OP_const4s:
14612 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14613 i += 4;
14614 break;
14615
585861ea
JK
14616 case DW_OP_const8u:
14617 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14618 i += 8;
14619 break;
14620
21ae7a4d
JK
14621 case DW_OP_constu:
14622 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14623 &bytes_read);
14624 i += bytes_read;
14625 break;
14626
14627 case DW_OP_consts:
14628 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14629 i += bytes_read;
14630 break;
14631
14632 case DW_OP_dup:
14633 stack[stacki + 1] = stack[stacki];
14634 stacki++;
14635 break;
14636
14637 case DW_OP_plus:
14638 stack[stacki - 1] += stack[stacki];
14639 stacki--;
14640 break;
14641
14642 case DW_OP_plus_uconst:
14643 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14644 &bytes_read);
14645 i += bytes_read;
14646 break;
14647
14648 case DW_OP_minus:
14649 stack[stacki - 1] -= stack[stacki];
14650 stacki--;
14651 break;
14652
14653 case DW_OP_deref:
14654 /* If we're not the last op, then we definitely can't encode
14655 this using GDB's address_class enum. This is valid for partial
14656 global symbols, although the variable's address will be bogus
14657 in the psymtab. */
14658 if (i < size)
14659 dwarf2_complex_location_expr_complaint ();
14660 break;
14661
14662 case DW_OP_GNU_push_tls_address:
14663 /* The top of the stack has the offset from the beginning
14664 of the thread control block at which the variable is located. */
14665 /* Nothing should follow this operator, so the top of stack would
14666 be returned. */
14667 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14668 address will be bogus in the psymtab. Make it always at least
14669 non-zero to not look as a variable garbage collected by linker
14670 which have DW_OP_addr 0. */
21ae7a4d
JK
14671 if (i < size)
14672 dwarf2_complex_location_expr_complaint ();
585861ea 14673 stack[stacki]++;
21ae7a4d
JK
14674 break;
14675
14676 case DW_OP_GNU_uninit:
14677 break;
14678
14679 default:
14680 {
14681 const char *name = dwarf_stack_op_name (op);
14682
14683 if (name)
14684 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14685 name);
14686 else
14687 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14688 op);
14689 }
14690
14691 return (stack[stacki]);
d53d4ac5 14692 }
3c6e0cb3 14693
21ae7a4d
JK
14694 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14695 outside of the allocated space. Also enforce minimum>0. */
14696 if (stacki >= ARRAY_SIZE (stack) - 1)
14697 {
14698 complaint (&symfile_complaints,
14699 _("location description stack overflow"));
14700 return 0;
14701 }
14702
14703 if (stacki <= 0)
14704 {
14705 complaint (&symfile_complaints,
14706 _("location description stack underflow"));
14707 return 0;
14708 }
14709 }
14710 return (stack[stacki]);
c906108c
SS
14711}
14712
14713/* memory allocation interface */
14714
c906108c 14715static struct dwarf_block *
7b5a2f43 14716dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14717{
14718 struct dwarf_block *blk;
14719
14720 blk = (struct dwarf_block *)
7b5a2f43 14721 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14722 return (blk);
14723}
14724
14725static struct abbrev_info *
f3dd6933 14726dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14727{
14728 struct abbrev_info *abbrev;
14729
f3dd6933
DJ
14730 abbrev = (struct abbrev_info *)
14731 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14732 memset (abbrev, 0, sizeof (struct abbrev_info));
14733 return (abbrev);
14734}
14735
14736static struct die_info *
b60c80d6 14737dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14738{
14739 struct die_info *die;
b60c80d6
DJ
14740 size_t size = sizeof (struct die_info);
14741
14742 if (num_attrs > 1)
14743 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14744
b60c80d6 14745 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14746 memset (die, 0, sizeof (struct die_info));
14747 return (die);
14748}
2e276125
JB
14749
14750\f
14751/* Macro support. */
14752
2e276125
JB
14753/* Return the full name of file number I in *LH's file name table.
14754 Use COMP_DIR as the name of the current directory of the
14755 compilation. The result is allocated using xmalloc; the caller is
14756 responsible for freeing it. */
14757static char *
14758file_full_name (int file, struct line_header *lh, const char *comp_dir)
14759{
6a83a1e6
EZ
14760 /* Is the file number a valid index into the line header's file name
14761 table? Remember that file numbers start with one, not zero. */
14762 if (1 <= file && file <= lh->num_file_names)
14763 {
14764 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14765
6a83a1e6
EZ
14766 if (IS_ABSOLUTE_PATH (fe->name))
14767 return xstrdup (fe->name);
14768 else
14769 {
14770 const char *dir;
14771 int dir_len;
14772 char *full_name;
14773
14774 if (fe->dir_index)
14775 dir = lh->include_dirs[fe->dir_index - 1];
14776 else
14777 dir = comp_dir;
14778
14779 if (dir)
14780 {
14781 dir_len = strlen (dir);
14782 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14783 strcpy (full_name, dir);
14784 full_name[dir_len] = '/';
14785 strcpy (full_name + dir_len + 1, fe->name);
14786 return full_name;
14787 }
14788 else
14789 return xstrdup (fe->name);
14790 }
14791 }
2e276125
JB
14792 else
14793 {
6a83a1e6
EZ
14794 /* The compiler produced a bogus file number. We can at least
14795 record the macro definitions made in the file, even if we
14796 won't be able to find the file by name. */
14797 char fake_name[80];
9a619af0 14798
6a83a1e6 14799 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14800
6e70227d 14801 complaint (&symfile_complaints,
6a83a1e6
EZ
14802 _("bad file number in macro information (%d)"),
14803 file);
2e276125 14804
6a83a1e6 14805 return xstrdup (fake_name);
2e276125
JB
14806 }
14807}
14808
14809
14810static struct macro_source_file *
14811macro_start_file (int file, int line,
14812 struct macro_source_file *current_file,
14813 const char *comp_dir,
14814 struct line_header *lh, struct objfile *objfile)
14815{
14816 /* The full name of this source file. */
14817 char *full_name = file_full_name (file, lh, comp_dir);
14818
14819 /* We don't create a macro table for this compilation unit
14820 at all until we actually get a filename. */
14821 if (! pending_macros)
4a146b47 14822 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14823 objfile->macro_cache);
2e276125
JB
14824
14825 if (! current_file)
14826 /* If we have no current file, then this must be the start_file
14827 directive for the compilation unit's main source file. */
14828 current_file = macro_set_main (pending_macros, full_name);
14829 else
14830 current_file = macro_include (current_file, line, full_name);
14831
14832 xfree (full_name);
6e70227d 14833
2e276125
JB
14834 return current_file;
14835}
14836
14837
14838/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14839 followed by a null byte. */
14840static char *
14841copy_string (const char *buf, int len)
14842{
14843 char *s = xmalloc (len + 1);
9a619af0 14844
2e276125
JB
14845 memcpy (s, buf, len);
14846 s[len] = '\0';
2e276125
JB
14847 return s;
14848}
14849
14850
14851static const char *
14852consume_improper_spaces (const char *p, const char *body)
14853{
14854 if (*p == ' ')
14855 {
4d3c2250 14856 complaint (&symfile_complaints,
3e43a32a
MS
14857 _("macro definition contains spaces "
14858 "in formal argument list:\n`%s'"),
4d3c2250 14859 body);
2e276125
JB
14860
14861 while (*p == ' ')
14862 p++;
14863 }
14864
14865 return p;
14866}
14867
14868
14869static void
14870parse_macro_definition (struct macro_source_file *file, int line,
14871 const char *body)
14872{
14873 const char *p;
14874
14875 /* The body string takes one of two forms. For object-like macro
14876 definitions, it should be:
14877
14878 <macro name> " " <definition>
14879
14880 For function-like macro definitions, it should be:
14881
14882 <macro name> "() " <definition>
14883 or
14884 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14885
14886 Spaces may appear only where explicitly indicated, and in the
14887 <definition>.
14888
14889 The Dwarf 2 spec says that an object-like macro's name is always
14890 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14891 the space when the macro's definition is the empty string.
2e276125
JB
14892
14893 The Dwarf 2 spec says that there should be no spaces between the
14894 formal arguments in a function-like macro's formal argument list,
14895 but versions of GCC around March 2002 include spaces after the
14896 commas. */
14897
14898
14899 /* Find the extent of the macro name. The macro name is terminated
14900 by either a space or null character (for an object-like macro) or
14901 an opening paren (for a function-like macro). */
14902 for (p = body; *p; p++)
14903 if (*p == ' ' || *p == '(')
14904 break;
14905
14906 if (*p == ' ' || *p == '\0')
14907 {
14908 /* It's an object-like macro. */
14909 int name_len = p - body;
14910 char *name = copy_string (body, name_len);
14911 const char *replacement;
14912
14913 if (*p == ' ')
14914 replacement = body + name_len + 1;
14915 else
14916 {
4d3c2250 14917 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14918 replacement = body + name_len;
14919 }
6e70227d 14920
2e276125
JB
14921 macro_define_object (file, line, name, replacement);
14922
14923 xfree (name);
14924 }
14925 else if (*p == '(')
14926 {
14927 /* It's a function-like macro. */
14928 char *name = copy_string (body, p - body);
14929 int argc = 0;
14930 int argv_size = 1;
14931 char **argv = xmalloc (argv_size * sizeof (*argv));
14932
14933 p++;
14934
14935 p = consume_improper_spaces (p, body);
14936
14937 /* Parse the formal argument list. */
14938 while (*p && *p != ')')
14939 {
14940 /* Find the extent of the current argument name. */
14941 const char *arg_start = p;
14942
14943 while (*p && *p != ',' && *p != ')' && *p != ' ')
14944 p++;
14945
14946 if (! *p || p == arg_start)
4d3c2250 14947 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14948 else
14949 {
14950 /* Make sure argv has room for the new argument. */
14951 if (argc >= argv_size)
14952 {
14953 argv_size *= 2;
14954 argv = xrealloc (argv, argv_size * sizeof (*argv));
14955 }
14956
14957 argv[argc++] = copy_string (arg_start, p - arg_start);
14958 }
14959
14960 p = consume_improper_spaces (p, body);
14961
14962 /* Consume the comma, if present. */
14963 if (*p == ',')
14964 {
14965 p++;
14966
14967 p = consume_improper_spaces (p, body);
14968 }
14969 }
14970
14971 if (*p == ')')
14972 {
14973 p++;
14974
14975 if (*p == ' ')
14976 /* Perfectly formed definition, no complaints. */
14977 macro_define_function (file, line, name,
6e70227d 14978 argc, (const char **) argv,
2e276125
JB
14979 p + 1);
14980 else if (*p == '\0')
14981 {
14982 /* Complain, but do define it. */
4d3c2250 14983 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14984 macro_define_function (file, line, name,
6e70227d 14985 argc, (const char **) argv,
2e276125
JB
14986 p);
14987 }
14988 else
14989 /* Just complain. */
4d3c2250 14990 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14991 }
14992 else
14993 /* Just complain. */
4d3c2250 14994 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14995
14996 xfree (name);
14997 {
14998 int i;
14999
15000 for (i = 0; i < argc; i++)
15001 xfree (argv[i]);
15002 }
15003 xfree (argv);
15004 }
15005 else
4d3c2250 15006 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15007}
15008
cf2c3c16
TT
15009/* Skip some bytes from BYTES according to the form given in FORM.
15010 Returns the new pointer. */
2e276125 15011
cf2c3c16
TT
15012static gdb_byte *
15013skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15014 enum dwarf_form form,
15015 unsigned int offset_size,
15016 struct dwarf2_section_info *section)
2e276125 15017{
cf2c3c16 15018 unsigned int bytes_read;
2e276125 15019
cf2c3c16 15020 switch (form)
2e276125 15021 {
cf2c3c16
TT
15022 case DW_FORM_data1:
15023 case DW_FORM_flag:
15024 ++bytes;
15025 break;
15026
15027 case DW_FORM_data2:
15028 bytes += 2;
15029 break;
15030
15031 case DW_FORM_data4:
15032 bytes += 4;
15033 break;
15034
15035 case DW_FORM_data8:
15036 bytes += 8;
15037 break;
15038
15039 case DW_FORM_string:
15040 read_direct_string (abfd, bytes, &bytes_read);
15041 bytes += bytes_read;
15042 break;
15043
15044 case DW_FORM_sec_offset:
15045 case DW_FORM_strp:
15046 bytes += offset_size;
15047 break;
15048
15049 case DW_FORM_block:
15050 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15051 bytes += bytes_read;
15052 break;
15053
15054 case DW_FORM_block1:
15055 bytes += 1 + read_1_byte (abfd, bytes);
15056 break;
15057 case DW_FORM_block2:
15058 bytes += 2 + read_2_bytes (abfd, bytes);
15059 break;
15060 case DW_FORM_block4:
15061 bytes += 4 + read_4_bytes (abfd, bytes);
15062 break;
15063
15064 case DW_FORM_sdata:
15065 case DW_FORM_udata:
15066 bytes = skip_leb128 (abfd, bytes);
15067 break;
15068
15069 default:
15070 {
15071 complain:
15072 complaint (&symfile_complaints,
15073 _("invalid form 0x%x in `%s'"),
15074 form,
15075 section->asection->name);
15076 return NULL;
15077 }
2e276125
JB
15078 }
15079
cf2c3c16
TT
15080 return bytes;
15081}
757a13d0 15082
cf2c3c16
TT
15083/* A helper for dwarf_decode_macros that handles skipping an unknown
15084 opcode. Returns an updated pointer to the macro data buffer; or,
15085 on error, issues a complaint and returns NULL. */
757a13d0 15086
cf2c3c16
TT
15087static gdb_byte *
15088skip_unknown_opcode (unsigned int opcode,
15089 gdb_byte **opcode_definitions,
15090 gdb_byte *mac_ptr,
15091 bfd *abfd,
15092 unsigned int offset_size,
15093 struct dwarf2_section_info *section)
15094{
15095 unsigned int bytes_read, i;
15096 unsigned long arg;
15097 gdb_byte *defn;
2e276125 15098
cf2c3c16 15099 if (opcode_definitions[opcode] == NULL)
2e276125 15100 {
cf2c3c16
TT
15101 complaint (&symfile_complaints,
15102 _("unrecognized DW_MACFINO opcode 0x%x"),
15103 opcode);
15104 return NULL;
15105 }
2e276125 15106
cf2c3c16
TT
15107 defn = opcode_definitions[opcode];
15108 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15109 defn += bytes_read;
2e276125 15110
cf2c3c16
TT
15111 for (i = 0; i < arg; ++i)
15112 {
15113 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15114 if (mac_ptr == NULL)
15115 {
15116 /* skip_form_bytes already issued the complaint. */
15117 return NULL;
15118 }
15119 }
757a13d0 15120
cf2c3c16
TT
15121 return mac_ptr;
15122}
757a13d0 15123
cf2c3c16
TT
15124/* A helper function which parses the header of a macro section.
15125 If the macro section is the extended (for now called "GNU") type,
15126 then this updates *OFFSET_SIZE. Returns a pointer to just after
15127 the header, or issues a complaint and returns NULL on error. */
757a13d0 15128
cf2c3c16
TT
15129static gdb_byte *
15130dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15131 bfd *abfd,
15132 gdb_byte *mac_ptr,
15133 unsigned int *offset_size,
15134 int section_is_gnu)
15135{
15136 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15137
cf2c3c16
TT
15138 if (section_is_gnu)
15139 {
15140 unsigned int version, flags;
757a13d0 15141
cf2c3c16
TT
15142 version = read_2_bytes (abfd, mac_ptr);
15143 if (version != 4)
15144 {
15145 complaint (&symfile_complaints,
15146 _("unrecognized version `%d' in .debug_macro section"),
15147 version);
15148 return NULL;
15149 }
15150 mac_ptr += 2;
757a13d0 15151
cf2c3c16
TT
15152 flags = read_1_byte (abfd, mac_ptr);
15153 ++mac_ptr;
15154 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15155
cf2c3c16
TT
15156 if ((flags & 2) != 0)
15157 /* We don't need the line table offset. */
15158 mac_ptr += *offset_size;
757a13d0 15159
cf2c3c16
TT
15160 /* Vendor opcode descriptions. */
15161 if ((flags & 4) != 0)
15162 {
15163 unsigned int i, count;
757a13d0 15164
cf2c3c16
TT
15165 count = read_1_byte (abfd, mac_ptr);
15166 ++mac_ptr;
15167 for (i = 0; i < count; ++i)
15168 {
15169 unsigned int opcode, bytes_read;
15170 unsigned long arg;
15171
15172 opcode = read_1_byte (abfd, mac_ptr);
15173 ++mac_ptr;
15174 opcode_definitions[opcode] = mac_ptr;
15175 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15176 mac_ptr += bytes_read;
15177 mac_ptr += arg;
15178 }
757a13d0 15179 }
cf2c3c16 15180 }
757a13d0 15181
cf2c3c16
TT
15182 return mac_ptr;
15183}
757a13d0 15184
cf2c3c16 15185/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15186 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15187
15188static void
15189dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15190 struct macro_source_file *current_file,
15191 struct line_header *lh, char *comp_dir,
15192 struct dwarf2_section_info *section,
15193 int section_is_gnu,
15194 unsigned int offset_size,
8fc3fc34
TT
15195 struct objfile *objfile,
15196 htab_t include_hash)
cf2c3c16
TT
15197{
15198 enum dwarf_macro_record_type macinfo_type;
15199 int at_commandline;
15200 gdb_byte *opcode_definitions[256];
757a13d0 15201
cf2c3c16
TT
15202 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15203 &offset_size, section_is_gnu);
15204 if (mac_ptr == NULL)
15205 {
15206 /* We already issued a complaint. */
15207 return;
15208 }
757a13d0
JK
15209
15210 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15211 GDB is still reading the definitions from command line. First
15212 DW_MACINFO_start_file will need to be ignored as it was already executed
15213 to create CURRENT_FILE for the main source holding also the command line
15214 definitions. On first met DW_MACINFO_start_file this flag is reset to
15215 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15216
15217 at_commandline = 1;
15218
15219 do
15220 {
15221 /* Do we at least have room for a macinfo type byte? */
15222 if (mac_ptr >= mac_end)
15223 {
cf2c3c16 15224 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15225 break;
15226 }
15227
15228 macinfo_type = read_1_byte (abfd, mac_ptr);
15229 mac_ptr++;
15230
cf2c3c16
TT
15231 /* Note that we rely on the fact that the corresponding GNU and
15232 DWARF constants are the same. */
757a13d0
JK
15233 switch (macinfo_type)
15234 {
15235 /* A zero macinfo type indicates the end of the macro
15236 information. */
15237 case 0:
15238 break;
2e276125 15239
cf2c3c16
TT
15240 case DW_MACRO_GNU_define:
15241 case DW_MACRO_GNU_undef:
15242 case DW_MACRO_GNU_define_indirect:
15243 case DW_MACRO_GNU_undef_indirect:
2e276125 15244 {
891d2f0b 15245 unsigned int bytes_read;
2e276125
JB
15246 int line;
15247 char *body;
cf2c3c16 15248 int is_define;
2e276125 15249
cf2c3c16
TT
15250 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15251 mac_ptr += bytes_read;
15252
15253 if (macinfo_type == DW_MACRO_GNU_define
15254 || macinfo_type == DW_MACRO_GNU_undef)
15255 {
15256 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15257 mac_ptr += bytes_read;
15258 }
15259 else
15260 {
15261 LONGEST str_offset;
15262
15263 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15264 mac_ptr += offset_size;
2e276125 15265
cf2c3c16
TT
15266 body = read_indirect_string_at_offset (abfd, str_offset);
15267 }
15268
15269 is_define = (macinfo_type == DW_MACRO_GNU_define
15270 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15271 if (! current_file)
757a13d0
JK
15272 {
15273 /* DWARF violation as no main source is present. */
15274 complaint (&symfile_complaints,
15275 _("debug info with no main source gives macro %s "
15276 "on line %d: %s"),
cf2c3c16
TT
15277 is_define ? _("definition") : _("undefinition"),
15278 line, body);
757a13d0
JK
15279 break;
15280 }
3e43a32a
MS
15281 if ((line == 0 && !at_commandline)
15282 || (line != 0 && at_commandline))
4d3c2250 15283 complaint (&symfile_complaints,
757a13d0
JK
15284 _("debug info gives %s macro %s with %s line %d: %s"),
15285 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15286 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15287 line == 0 ? _("zero") : _("non-zero"), line, body);
15288
cf2c3c16 15289 if (is_define)
757a13d0 15290 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15291 else
15292 {
15293 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15294 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15295 macro_undef (current_file, line, body);
15296 }
2e276125
JB
15297 }
15298 break;
15299
cf2c3c16 15300 case DW_MACRO_GNU_start_file:
2e276125 15301 {
891d2f0b 15302 unsigned int bytes_read;
2e276125
JB
15303 int line, file;
15304
15305 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15306 mac_ptr += bytes_read;
15307 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15308 mac_ptr += bytes_read;
15309
3e43a32a
MS
15310 if ((line == 0 && !at_commandline)
15311 || (line != 0 && at_commandline))
757a13d0
JK
15312 complaint (&symfile_complaints,
15313 _("debug info gives source %d included "
15314 "from %s at %s line %d"),
15315 file, at_commandline ? _("command-line") : _("file"),
15316 line == 0 ? _("zero") : _("non-zero"), line);
15317
15318 if (at_commandline)
15319 {
cf2c3c16
TT
15320 /* This DW_MACRO_GNU_start_file was executed in the
15321 pass one. */
757a13d0
JK
15322 at_commandline = 0;
15323 }
15324 else
15325 current_file = macro_start_file (file, line,
15326 current_file, comp_dir,
cf2c3c16 15327 lh, objfile);
2e276125
JB
15328 }
15329 break;
15330
cf2c3c16 15331 case DW_MACRO_GNU_end_file:
2e276125 15332 if (! current_file)
4d3c2250 15333 complaint (&symfile_complaints,
3e43a32a
MS
15334 _("macro debug info has an unmatched "
15335 "`close_file' directive"));
2e276125
JB
15336 else
15337 {
15338 current_file = current_file->included_by;
15339 if (! current_file)
15340 {
cf2c3c16 15341 enum dwarf_macro_record_type next_type;
2e276125
JB
15342
15343 /* GCC circa March 2002 doesn't produce the zero
15344 type byte marking the end of the compilation
15345 unit. Complain if it's not there, but exit no
15346 matter what. */
15347
15348 /* Do we at least have room for a macinfo type byte? */
15349 if (mac_ptr >= mac_end)
15350 {
cf2c3c16 15351 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15352 return;
15353 }
15354
15355 /* We don't increment mac_ptr here, so this is just
15356 a look-ahead. */
15357 next_type = read_1_byte (abfd, mac_ptr);
15358 if (next_type != 0)
4d3c2250 15359 complaint (&symfile_complaints,
3e43a32a
MS
15360 _("no terminating 0-type entry for "
15361 "macros in `.debug_macinfo' section"));
2e276125
JB
15362
15363 return;
15364 }
15365 }
15366 break;
15367
cf2c3c16
TT
15368 case DW_MACRO_GNU_transparent_include:
15369 {
15370 LONGEST offset;
8fc3fc34 15371 void **slot;
cf2c3c16
TT
15372
15373 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15374 mac_ptr += offset_size;
15375
8fc3fc34
TT
15376 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15377 if (*slot != NULL)
15378 {
15379 /* This has actually happened; see
15380 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15381 complaint (&symfile_complaints,
15382 _("recursive DW_MACRO_GNU_transparent_include in "
15383 ".debug_macro section"));
15384 }
15385 else
15386 {
15387 *slot = mac_ptr;
15388
15389 dwarf_decode_macro_bytes (abfd,
15390 section->buffer + offset,
15391 mac_end, current_file,
15392 lh, comp_dir,
15393 section, section_is_gnu,
15394 offset_size, objfile, include_hash);
15395
15396 htab_remove_elt (include_hash, mac_ptr);
15397 }
cf2c3c16
TT
15398 }
15399 break;
15400
2e276125 15401 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15402 if (!section_is_gnu)
15403 {
15404 unsigned int bytes_read;
15405 int constant;
2e276125 15406
cf2c3c16
TT
15407 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15408 mac_ptr += bytes_read;
15409 read_direct_string (abfd, mac_ptr, &bytes_read);
15410 mac_ptr += bytes_read;
2e276125 15411
cf2c3c16
TT
15412 /* We don't recognize any vendor extensions. */
15413 break;
15414 }
15415 /* FALLTHROUGH */
15416
15417 default:
15418 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15419 mac_ptr, abfd, offset_size,
15420 section);
15421 if (mac_ptr == NULL)
15422 return;
15423 break;
2e276125 15424 }
757a13d0 15425 } while (macinfo_type != 0);
2e276125 15426}
8e19ed76 15427
cf2c3c16
TT
15428static void
15429dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15430 char *comp_dir, bfd *abfd,
15431 struct dwarf2_cu *cu,
15432 struct dwarf2_section_info *section,
15433 int section_is_gnu)
15434{
bb5ed363 15435 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15436 gdb_byte *mac_ptr, *mac_end;
15437 struct macro_source_file *current_file = 0;
15438 enum dwarf_macro_record_type macinfo_type;
15439 unsigned int offset_size = cu->header.offset_size;
15440 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15441 struct cleanup *cleanup;
15442 htab_t include_hash;
15443 void **slot;
cf2c3c16 15444
bb5ed363 15445 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15446 if (section->buffer == NULL)
15447 {
15448 complaint (&symfile_complaints, _("missing %s section"),
15449 section->asection->name);
15450 return;
15451 }
15452
15453 /* First pass: Find the name of the base filename.
15454 This filename is needed in order to process all macros whose definition
15455 (or undefinition) comes from the command line. These macros are defined
15456 before the first DW_MACINFO_start_file entry, and yet still need to be
15457 associated to the base file.
15458
15459 To determine the base file name, we scan the macro definitions until we
15460 reach the first DW_MACINFO_start_file entry. We then initialize
15461 CURRENT_FILE accordingly so that any macro definition found before the
15462 first DW_MACINFO_start_file can still be associated to the base file. */
15463
15464 mac_ptr = section->buffer + offset;
15465 mac_end = section->buffer + section->size;
15466
15467 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15468 &offset_size, section_is_gnu);
15469 if (mac_ptr == NULL)
15470 {
15471 /* We already issued a complaint. */
15472 return;
15473 }
15474
15475 do
15476 {
15477 /* Do we at least have room for a macinfo type byte? */
15478 if (mac_ptr >= mac_end)
15479 {
15480 /* Complaint is printed during the second pass as GDB will probably
15481 stop the first pass earlier upon finding
15482 DW_MACINFO_start_file. */
15483 break;
15484 }
15485
15486 macinfo_type = read_1_byte (abfd, mac_ptr);
15487 mac_ptr++;
15488
15489 /* Note that we rely on the fact that the corresponding GNU and
15490 DWARF constants are the same. */
15491 switch (macinfo_type)
15492 {
15493 /* A zero macinfo type indicates the end of the macro
15494 information. */
15495 case 0:
15496 break;
15497
15498 case DW_MACRO_GNU_define:
15499 case DW_MACRO_GNU_undef:
15500 /* Only skip the data by MAC_PTR. */
15501 {
15502 unsigned int bytes_read;
15503
15504 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15505 mac_ptr += bytes_read;
15506 read_direct_string (abfd, mac_ptr, &bytes_read);
15507 mac_ptr += bytes_read;
15508 }
15509 break;
15510
15511 case DW_MACRO_GNU_start_file:
15512 {
15513 unsigned int bytes_read;
15514 int line, file;
15515
15516 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15517 mac_ptr += bytes_read;
15518 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15519 mac_ptr += bytes_read;
15520
15521 current_file = macro_start_file (file, line, current_file,
bb5ed363 15522 comp_dir, lh, objfile);
cf2c3c16
TT
15523 }
15524 break;
15525
15526 case DW_MACRO_GNU_end_file:
15527 /* No data to skip by MAC_PTR. */
15528 break;
15529
15530 case DW_MACRO_GNU_define_indirect:
15531 case DW_MACRO_GNU_undef_indirect:
15532 {
15533 unsigned int bytes_read;
15534
15535 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15536 mac_ptr += bytes_read;
15537 mac_ptr += offset_size;
15538 }
15539 break;
15540
15541 case DW_MACRO_GNU_transparent_include:
15542 /* Note that, according to the spec, a transparent include
15543 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15544 skip this opcode. */
15545 mac_ptr += offset_size;
15546 break;
15547
15548 case DW_MACINFO_vendor_ext:
15549 /* Only skip the data by MAC_PTR. */
15550 if (!section_is_gnu)
15551 {
15552 unsigned int bytes_read;
15553
15554 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15555 mac_ptr += bytes_read;
15556 read_direct_string (abfd, mac_ptr, &bytes_read);
15557 mac_ptr += bytes_read;
15558 }
15559 /* FALLTHROUGH */
15560
15561 default:
15562 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15563 mac_ptr, abfd, offset_size,
15564 section);
15565 if (mac_ptr == NULL)
15566 return;
15567 break;
15568 }
15569 } while (macinfo_type != 0 && current_file == NULL);
15570
15571 /* Second pass: Process all entries.
15572
15573 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15574 command-line macro definitions/undefinitions. This flag is unset when we
15575 reach the first DW_MACINFO_start_file entry. */
15576
8fc3fc34
TT
15577 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15578 NULL, xcalloc, xfree);
15579 cleanup = make_cleanup_htab_delete (include_hash);
15580 mac_ptr = section->buffer + offset;
15581 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15582 *slot = mac_ptr;
15583 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 15584 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
15585 offset_size, objfile, include_hash);
15586 do_cleanups (cleanup);
cf2c3c16
TT
15587}
15588
8e19ed76 15589/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15590 if so return true else false. */
8e19ed76
PS
15591static int
15592attr_form_is_block (struct attribute *attr)
15593{
15594 return (attr == NULL ? 0 :
15595 attr->form == DW_FORM_block1
15596 || attr->form == DW_FORM_block2
15597 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15598 || attr->form == DW_FORM_block
15599 || attr->form == DW_FORM_exprloc);
8e19ed76 15600}
4c2df51b 15601
c6a0999f
JB
15602/* Return non-zero if ATTR's value is a section offset --- classes
15603 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15604 You may use DW_UNSND (attr) to retrieve such offsets.
15605
15606 Section 7.5.4, "Attribute Encodings", explains that no attribute
15607 may have a value that belongs to more than one of these classes; it
15608 would be ambiguous if we did, because we use the same forms for all
15609 of them. */
3690dd37
JB
15610static int
15611attr_form_is_section_offset (struct attribute *attr)
15612{
15613 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15614 || attr->form == DW_FORM_data8
15615 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15616}
15617
15618
15619/* Return non-zero if ATTR's value falls in the 'constant' class, or
15620 zero otherwise. When this function returns true, you can apply
15621 dwarf2_get_attr_constant_value to it.
15622
15623 However, note that for some attributes you must check
15624 attr_form_is_section_offset before using this test. DW_FORM_data4
15625 and DW_FORM_data8 are members of both the constant class, and of
15626 the classes that contain offsets into other debug sections
15627 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15628 that, if an attribute's can be either a constant or one of the
15629 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15630 taken as section offsets, not constants. */
15631static int
15632attr_form_is_constant (struct attribute *attr)
15633{
15634 switch (attr->form)
15635 {
15636 case DW_FORM_sdata:
15637 case DW_FORM_udata:
15638 case DW_FORM_data1:
15639 case DW_FORM_data2:
15640 case DW_FORM_data4:
15641 case DW_FORM_data8:
15642 return 1;
15643 default:
15644 return 0;
15645 }
15646}
15647
8cf6f0b1
TT
15648/* A helper function that fills in a dwarf2_loclist_baton. */
15649
15650static void
15651fill_in_loclist_baton (struct dwarf2_cu *cu,
15652 struct dwarf2_loclist_baton *baton,
15653 struct attribute *attr)
15654{
15655 dwarf2_read_section (dwarf2_per_objfile->objfile,
15656 &dwarf2_per_objfile->loc);
15657
15658 baton->per_cu = cu->per_cu;
15659 gdb_assert (baton->per_cu);
15660 /* We don't know how long the location list is, but make sure we
15661 don't run off the edge of the section. */
15662 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15663 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15664 baton->base_address = cu->base_address;
15665}
15666
4c2df51b
DJ
15667static void
15668dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15669 struct dwarf2_cu *cu)
4c2df51b 15670{
bb5ed363
DE
15671 struct objfile *objfile = dwarf2_per_objfile->objfile;
15672
3690dd37 15673 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15674 /* ".debug_loc" may not exist at all, or the offset may be outside
15675 the section. If so, fall through to the complaint in the
15676 other branch. */
bb5ed363 15677 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15678 &dwarf2_per_objfile->loc))
4c2df51b 15679 {
0d53c4c4 15680 struct dwarf2_loclist_baton *baton;
4c2df51b 15681
bb5ed363 15682 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15683 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15684
8cf6f0b1 15685 fill_in_loclist_baton (cu, baton, attr);
be391dca 15686
d00adf39 15687 if (cu->base_known == 0)
0d53c4c4 15688 complaint (&symfile_complaints,
3e43a32a
MS
15689 _("Location list used without "
15690 "specifying the CU base address."));
4c2df51b 15691
768a979c 15692 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15693 SYMBOL_LOCATION_BATON (sym) = baton;
15694 }
15695 else
15696 {
15697 struct dwarf2_locexpr_baton *baton;
15698
bb5ed363 15699 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15700 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15701 baton->per_cu = cu->per_cu;
15702 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15703
15704 if (attr_form_is_block (attr))
15705 {
15706 /* Note that we're just copying the block's data pointer
15707 here, not the actual data. We're still pointing into the
6502dd73
DJ
15708 info_buffer for SYM's objfile; right now we never release
15709 that buffer, but when we do clean up properly this may
15710 need to change. */
0d53c4c4
DJ
15711 baton->size = DW_BLOCK (attr)->size;
15712 baton->data = DW_BLOCK (attr)->data;
15713 }
15714 else
15715 {
15716 dwarf2_invalid_attrib_class_complaint ("location description",
15717 SYMBOL_NATURAL_NAME (sym));
15718 baton->size = 0;
0d53c4c4 15719 }
6e70227d 15720
768a979c 15721 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15722 SYMBOL_LOCATION_BATON (sym) = baton;
15723 }
4c2df51b 15724}
6502dd73 15725
9aa1f1e3
TT
15726/* Return the OBJFILE associated with the compilation unit CU. If CU
15727 came from a separate debuginfo file, then the master objfile is
15728 returned. */
ae0d2f24
UW
15729
15730struct objfile *
15731dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15732{
9291a0cd 15733 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15734
15735 /* Return the master objfile, so that we can report and look up the
15736 correct file containing this variable. */
15737 if (objfile->separate_debug_objfile_backlink)
15738 objfile = objfile->separate_debug_objfile_backlink;
15739
15740 return objfile;
15741}
15742
96408a79
SA
15743/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15744 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15745 CU_HEADERP first. */
15746
15747static const struct comp_unit_head *
15748per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15749 struct dwarf2_per_cu_data *per_cu)
15750{
15751 struct objfile *objfile;
15752 struct dwarf2_per_objfile *per_objfile;
15753 gdb_byte *info_ptr;
15754
15755 if (per_cu->cu)
15756 return &per_cu->cu->header;
15757
15758 objfile = per_cu->objfile;
15759 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15760 info_ptr = per_objfile->info.buffer + per_cu->offset;
15761
15762 memset (cu_headerp, 0, sizeof (*cu_headerp));
15763 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15764
15765 return cu_headerp;
15766}
15767
ae0d2f24
UW
15768/* Return the address size given in the compilation unit header for CU. */
15769
98714339 15770int
ae0d2f24
UW
15771dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15772{
96408a79
SA
15773 struct comp_unit_head cu_header_local;
15774 const struct comp_unit_head *cu_headerp;
c471e790 15775
96408a79
SA
15776 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15777
15778 return cu_headerp->addr_size;
ae0d2f24
UW
15779}
15780
9eae7c52
TT
15781/* Return the offset size given in the compilation unit header for CU. */
15782
15783int
15784dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15785{
96408a79
SA
15786 struct comp_unit_head cu_header_local;
15787 const struct comp_unit_head *cu_headerp;
9c6c53f7 15788
96408a79
SA
15789 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15790
15791 return cu_headerp->offset_size;
15792}
15793
15794/* See its dwarf2loc.h declaration. */
15795
15796int
15797dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15798{
15799 struct comp_unit_head cu_header_local;
15800 const struct comp_unit_head *cu_headerp;
15801
15802 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15803
15804 if (cu_headerp->version == 2)
15805 return cu_headerp->addr_size;
15806 else
15807 return cu_headerp->offset_size;
181cebd4
JK
15808}
15809
9aa1f1e3
TT
15810/* Return the text offset of the CU. The returned offset comes from
15811 this CU's objfile. If this objfile came from a separate debuginfo
15812 file, then the offset may be different from the corresponding
15813 offset in the parent objfile. */
15814
15815CORE_ADDR
15816dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15817{
bb3fa9d0 15818 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15819
15820 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15821}
15822
348e048f
DE
15823/* Locate the .debug_info compilation unit from CU's objfile which contains
15824 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15825
15826static struct dwarf2_per_cu_data *
c764a876 15827dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15828 struct objfile *objfile)
15829{
15830 struct dwarf2_per_cu_data *this_cu;
15831 int low, high;
15832
ae038cb0
DJ
15833 low = 0;
15834 high = dwarf2_per_objfile->n_comp_units - 1;
15835 while (high > low)
15836 {
15837 int mid = low + (high - low) / 2;
9a619af0 15838
ae038cb0
DJ
15839 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15840 high = mid;
15841 else
15842 low = mid + 1;
15843 }
15844 gdb_assert (low == high);
15845 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15846 {
10b3939b 15847 if (low == 0)
8a3fe4f8
AC
15848 error (_("Dwarf Error: could not find partial DIE containing "
15849 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15850 (long) offset, bfd_get_filename (objfile->obfd));
15851
ae038cb0
DJ
15852 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15853 return dwarf2_per_objfile->all_comp_units[low-1];
15854 }
15855 else
15856 {
15857 this_cu = dwarf2_per_objfile->all_comp_units[low];
15858 if (low == dwarf2_per_objfile->n_comp_units - 1
15859 && offset >= this_cu->offset + this_cu->length)
c764a876 15860 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15861 gdb_assert (offset < this_cu->offset + this_cu->length);
15862 return this_cu;
15863 }
15864}
15865
23745b47 15866/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15867
9816fde3 15868static void
23745b47 15869init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15870{
9816fde3 15871 memset (cu, 0, sizeof (*cu));
23745b47
DE
15872 per_cu->cu = cu;
15873 cu->per_cu = per_cu;
15874 cu->objfile = per_cu->objfile;
93311388 15875 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15876}
15877
15878/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15879
15880static void
15881prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15882{
15883 struct attribute *attr;
15884
15885 /* Set the language we're debugging. */
15886 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15887 if (attr)
15888 set_cu_language (DW_UNSND (attr), cu);
15889 else
9cded63f
TT
15890 {
15891 cu->language = language_minimal;
15892 cu->language_defn = language_def (cu->language);
15893 }
93311388
DE
15894}
15895
ae038cb0
DJ
15896/* Release one cached compilation unit, CU. We unlink it from the tree
15897 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15898 the caller is responsible for that.
15899 NOTE: DATA is a void * because this function is also used as a
15900 cleanup routine. */
ae038cb0
DJ
15901
15902static void
68dc6402 15903free_heap_comp_unit (void *data)
ae038cb0
DJ
15904{
15905 struct dwarf2_cu *cu = data;
15906
23745b47
DE
15907 gdb_assert (cu->per_cu != NULL);
15908 cu->per_cu->cu = NULL;
ae038cb0
DJ
15909 cu->per_cu = NULL;
15910
15911 obstack_free (&cu->comp_unit_obstack, NULL);
15912
15913 xfree (cu);
15914}
15915
72bf9492 15916/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15917 when we're finished with it. We can't free the pointer itself, but be
15918 sure to unlink it from the cache. Also release any associated storage
15919 and perform cache maintenance.
72bf9492
DJ
15920
15921 Only used during partial symbol parsing. */
15922
15923static void
15924free_stack_comp_unit (void *data)
15925{
15926 struct dwarf2_cu *cu = data;
15927
23745b47
DE
15928 gdb_assert (cu->per_cu != NULL);
15929 cu->per_cu->cu = NULL;
15930 cu->per_cu = NULL;
15931
72bf9492
DJ
15932 obstack_free (&cu->comp_unit_obstack, NULL);
15933 cu->partial_dies = NULL;
ae038cb0 15934
23745b47
DE
15935 /* The previous code only did this if per_cu != NULL.
15936 But that would always succeed, so now we just unconditionally do
15937 the aging. This seems like the wrong place to do such aging,
15938 but cleaning that up is left for later. */
15939 age_cached_comp_units ();
ae038cb0
DJ
15940}
15941
15942/* Free all cached compilation units. */
15943
15944static void
15945free_cached_comp_units (void *data)
15946{
15947 struct dwarf2_per_cu_data *per_cu, **last_chain;
15948
15949 per_cu = dwarf2_per_objfile->read_in_chain;
15950 last_chain = &dwarf2_per_objfile->read_in_chain;
15951 while (per_cu != NULL)
15952 {
15953 struct dwarf2_per_cu_data *next_cu;
15954
15955 next_cu = per_cu->cu->read_in_chain;
15956
68dc6402 15957 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15958 *last_chain = next_cu;
15959
15960 per_cu = next_cu;
15961 }
15962}
15963
15964/* Increase the age counter on each cached compilation unit, and free
15965 any that are too old. */
15966
15967static void
15968age_cached_comp_units (void)
15969{
15970 struct dwarf2_per_cu_data *per_cu, **last_chain;
15971
15972 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15973 per_cu = dwarf2_per_objfile->read_in_chain;
15974 while (per_cu != NULL)
15975 {
15976 per_cu->cu->last_used ++;
15977 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15978 dwarf2_mark (per_cu->cu);
15979 per_cu = per_cu->cu->read_in_chain;
15980 }
15981
15982 per_cu = dwarf2_per_objfile->read_in_chain;
15983 last_chain = &dwarf2_per_objfile->read_in_chain;
15984 while (per_cu != NULL)
15985 {
15986 struct dwarf2_per_cu_data *next_cu;
15987
15988 next_cu = per_cu->cu->read_in_chain;
15989
15990 if (!per_cu->cu->mark)
15991 {
68dc6402 15992 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15993 *last_chain = next_cu;
15994 }
15995 else
15996 last_chain = &per_cu->cu->read_in_chain;
15997
15998 per_cu = next_cu;
15999 }
16000}
16001
16002/* Remove a single compilation unit from the cache. */
16003
16004static void
16005free_one_cached_comp_unit (void *target_cu)
16006{
16007 struct dwarf2_per_cu_data *per_cu, **last_chain;
16008
16009 per_cu = dwarf2_per_objfile->read_in_chain;
16010 last_chain = &dwarf2_per_objfile->read_in_chain;
16011 while (per_cu != NULL)
16012 {
16013 struct dwarf2_per_cu_data *next_cu;
16014
16015 next_cu = per_cu->cu->read_in_chain;
16016
16017 if (per_cu->cu == target_cu)
16018 {
68dc6402 16019 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16020 *last_chain = next_cu;
16021 break;
16022 }
16023 else
16024 last_chain = &per_cu->cu->read_in_chain;
16025
16026 per_cu = next_cu;
16027 }
16028}
16029
fe3e1990
DJ
16030/* Release all extra memory associated with OBJFILE. */
16031
16032void
16033dwarf2_free_objfile (struct objfile *objfile)
16034{
16035 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16036
16037 if (dwarf2_per_objfile == NULL)
16038 return;
16039
16040 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16041 free_cached_comp_units (NULL);
16042
7b9f3c50
DE
16043 if (dwarf2_per_objfile->quick_file_names_table)
16044 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16045
fe3e1990
DJ
16046 /* Everything else should be on the objfile obstack. */
16047}
16048
1c379e20
DJ
16049/* A pair of DIE offset and GDB type pointer. We store these
16050 in a hash table separate from the DIEs, and preserve them
16051 when the DIEs are flushed out of cache. */
16052
16053struct dwarf2_offset_and_type
16054{
16055 unsigned int offset;
16056 struct type *type;
16057};
16058
16059/* Hash function for a dwarf2_offset_and_type. */
16060
16061static hashval_t
16062offset_and_type_hash (const void *item)
16063{
16064 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16065
1c379e20
DJ
16066 return ofs->offset;
16067}
16068
16069/* Equality function for a dwarf2_offset_and_type. */
16070
16071static int
16072offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16073{
16074 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16075 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16076
1c379e20
DJ
16077 return ofs_lhs->offset == ofs_rhs->offset;
16078}
16079
16080/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16081 table if necessary. For convenience, return TYPE.
16082
16083 The DIEs reading must have careful ordering to:
16084 * Not cause infite loops trying to read in DIEs as a prerequisite for
16085 reading current DIE.
16086 * Not trying to dereference contents of still incompletely read in types
16087 while reading in other DIEs.
16088 * Enable referencing still incompletely read in types just by a pointer to
16089 the type without accessing its fields.
16090
16091 Therefore caller should follow these rules:
16092 * Try to fetch any prerequisite types we may need to build this DIE type
16093 before building the type and calling set_die_type.
e71ec853 16094 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16095 possible before fetching more types to complete the current type.
16096 * Make the type as complete as possible before fetching more types. */
1c379e20 16097
f792889a 16098static struct type *
1c379e20
DJ
16099set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16100{
16101 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16102 struct objfile *objfile = cu->objfile;
16103 htab_t *type_hash_ptr;
1c379e20 16104
b4ba55a1
JB
16105 /* For Ada types, make sure that the gnat-specific data is always
16106 initialized (if not already set). There are a few types where
16107 we should not be doing so, because the type-specific area is
16108 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16109 where the type-specific area is used to store the floatformat).
16110 But this is not a problem, because the gnat-specific information
16111 is actually not needed for these types. */
16112 if (need_gnat_info (cu)
16113 && TYPE_CODE (type) != TYPE_CODE_FUNC
16114 && TYPE_CODE (type) != TYPE_CODE_FLT
16115 && !HAVE_GNAT_AUX_INFO (type))
16116 INIT_GNAT_SPECIFIC (type);
16117
b0df02fd 16118 if (cu->per_cu->debug_types_section)
673bfd45
DE
16119 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16120 else
16121 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16122
16123 if (*type_hash_ptr == NULL)
f792889a 16124 {
673bfd45
DE
16125 *type_hash_ptr
16126 = htab_create_alloc_ex (127,
f792889a
DJ
16127 offset_and_type_hash,
16128 offset_and_type_eq,
16129 NULL,
673bfd45 16130 &objfile->objfile_obstack,
f792889a
DJ
16131 hashtab_obstack_allocate,
16132 dummy_obstack_deallocate);
f792889a 16133 }
1c379e20
DJ
16134
16135 ofs.offset = die->offset;
16136 ofs.type = type;
16137 slot = (struct dwarf2_offset_and_type **)
673bfd45 16138 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16139 if (*slot)
16140 complaint (&symfile_complaints,
16141 _("A problem internal to GDB: DIE 0x%x has type already set"),
16142 die->offset);
673bfd45 16143 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16144 **slot = ofs;
f792889a 16145 return type;
1c379e20
DJ
16146}
16147
673bfd45
DE
16148/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16149 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16150
16151static struct type *
673bfd45
DE
16152get_die_type_at_offset (unsigned int offset,
16153 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16154{
16155 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16156 htab_t type_hash;
f792889a 16157
b0df02fd 16158 if (per_cu->debug_types_section)
673bfd45
DE
16159 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16160 else
16161 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16162 if (type_hash == NULL)
16163 return NULL;
1c379e20 16164
673bfd45 16165 ofs.offset = offset;
1c379e20
DJ
16166 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16167 if (slot)
16168 return slot->type;
16169 else
16170 return NULL;
16171}
16172
673bfd45
DE
16173/* Look up the type for DIE in the appropriate type_hash table,
16174 or return NULL if DIE does not have a saved type. */
16175
16176static struct type *
16177get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16178{
16179 return get_die_type_at_offset (die->offset, cu->per_cu);
16180}
16181
10b3939b
DJ
16182/* Add a dependence relationship from CU to REF_PER_CU. */
16183
16184static void
16185dwarf2_add_dependence (struct dwarf2_cu *cu,
16186 struct dwarf2_per_cu_data *ref_per_cu)
16187{
16188 void **slot;
16189
16190 if (cu->dependencies == NULL)
16191 cu->dependencies
16192 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16193 NULL, &cu->comp_unit_obstack,
16194 hashtab_obstack_allocate,
16195 dummy_obstack_deallocate);
16196
16197 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16198 if (*slot == NULL)
16199 *slot = ref_per_cu;
16200}
1c379e20 16201
f504f079
DE
16202/* Subroutine of dwarf2_mark to pass to htab_traverse.
16203 Set the mark field in every compilation unit in the
ae038cb0
DJ
16204 cache that we must keep because we are keeping CU. */
16205
10b3939b
DJ
16206static int
16207dwarf2_mark_helper (void **slot, void *data)
16208{
16209 struct dwarf2_per_cu_data *per_cu;
16210
16211 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16212
16213 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16214 reading of the chain. As such dependencies remain valid it is not much
16215 useful to track and undo them during QUIT cleanups. */
16216 if (per_cu->cu == NULL)
16217 return 1;
16218
10b3939b
DJ
16219 if (per_cu->cu->mark)
16220 return 1;
16221 per_cu->cu->mark = 1;
16222
16223 if (per_cu->cu->dependencies != NULL)
16224 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16225
16226 return 1;
16227}
16228
f504f079
DE
16229/* Set the mark field in CU and in every other compilation unit in the
16230 cache that we must keep because we are keeping CU. */
16231
ae038cb0
DJ
16232static void
16233dwarf2_mark (struct dwarf2_cu *cu)
16234{
16235 if (cu->mark)
16236 return;
16237 cu->mark = 1;
10b3939b
DJ
16238 if (cu->dependencies != NULL)
16239 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16240}
16241
16242static void
16243dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16244{
16245 while (per_cu)
16246 {
16247 per_cu->cu->mark = 0;
16248 per_cu = per_cu->cu->read_in_chain;
16249 }
72bf9492
DJ
16250}
16251
72bf9492
DJ
16252/* Trivial hash function for partial_die_info: the hash value of a DIE
16253 is its offset in .debug_info for this objfile. */
16254
16255static hashval_t
16256partial_die_hash (const void *item)
16257{
16258 const struct partial_die_info *part_die = item;
9a619af0 16259
72bf9492
DJ
16260 return part_die->offset;
16261}
16262
16263/* Trivial comparison function for partial_die_info structures: two DIEs
16264 are equal if they have the same offset. */
16265
16266static int
16267partial_die_eq (const void *item_lhs, const void *item_rhs)
16268{
16269 const struct partial_die_info *part_die_lhs = item_lhs;
16270 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16271
72bf9492
DJ
16272 return part_die_lhs->offset == part_die_rhs->offset;
16273}
16274
ae038cb0
DJ
16275static struct cmd_list_element *set_dwarf2_cmdlist;
16276static struct cmd_list_element *show_dwarf2_cmdlist;
16277
16278static void
16279set_dwarf2_cmd (char *args, int from_tty)
16280{
16281 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16282}
16283
16284static void
16285show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16286{
ae038cb0
DJ
16287 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16288}
16289
dce234bc
PP
16290/* If section described by INFO was mmapped, munmap it now. */
16291
16292static void
16293munmap_section_buffer (struct dwarf2_section_info *info)
16294{
b315ab21 16295 if (info->map_addr != NULL)
dce234bc
PP
16296 {
16297#ifdef HAVE_MMAP
b315ab21 16298 int res;
9a619af0 16299
b315ab21
TG
16300 res = munmap (info->map_addr, info->map_len);
16301 gdb_assert (res == 0);
dce234bc
PP
16302#else
16303 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16304 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16305#endif
16306 }
16307}
16308
16309/* munmap debug sections for OBJFILE, if necessary. */
16310
16311static void
c1bd65d0 16312dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16313{
16314 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16315 int ix;
16316 struct dwarf2_section_info *section;
9a619af0 16317
16be1145
DE
16318 /* This is sorted according to the order they're defined in to make it easier
16319 to keep in sync. */
dce234bc
PP
16320 munmap_section_buffer (&data->info);
16321 munmap_section_buffer (&data->abbrev);
16322 munmap_section_buffer (&data->line);
16be1145 16323 munmap_section_buffer (&data->loc);
dce234bc 16324 munmap_section_buffer (&data->macinfo);
cf2c3c16 16325 munmap_section_buffer (&data->macro);
16be1145 16326 munmap_section_buffer (&data->str);
dce234bc 16327 munmap_section_buffer (&data->ranges);
dce234bc
PP
16328 munmap_section_buffer (&data->frame);
16329 munmap_section_buffer (&data->eh_frame);
9291a0cd 16330 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16331
16332 for (ix = 0;
16333 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16334 ++ix)
16335 munmap_section_buffer (section);
16336
16337 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16338}
16339
16340\f
ae2de4f8 16341/* The "save gdb-index" command. */
9291a0cd
TT
16342
16343/* The contents of the hash table we create when building the string
16344 table. */
16345struct strtab_entry
16346{
16347 offset_type offset;
16348 const char *str;
16349};
16350
559a7a62
JK
16351/* Hash function for a strtab_entry.
16352
16353 Function is used only during write_hash_table so no index format backward
16354 compatibility is needed. */
b89be57b 16355
9291a0cd
TT
16356static hashval_t
16357hash_strtab_entry (const void *e)
16358{
16359 const struct strtab_entry *entry = e;
559a7a62 16360 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16361}
16362
16363/* Equality function for a strtab_entry. */
b89be57b 16364
9291a0cd
TT
16365static int
16366eq_strtab_entry (const void *a, const void *b)
16367{
16368 const struct strtab_entry *ea = a;
16369 const struct strtab_entry *eb = b;
16370 return !strcmp (ea->str, eb->str);
16371}
16372
16373/* Create a strtab_entry hash table. */
b89be57b 16374
9291a0cd
TT
16375static htab_t
16376create_strtab (void)
16377{
16378 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16379 xfree, xcalloc, xfree);
16380}
16381
16382/* Add a string to the constant pool. Return the string's offset in
16383 host order. */
b89be57b 16384
9291a0cd
TT
16385static offset_type
16386add_string (htab_t table, struct obstack *cpool, const char *str)
16387{
16388 void **slot;
16389 struct strtab_entry entry;
16390 struct strtab_entry *result;
16391
16392 entry.str = str;
16393 slot = htab_find_slot (table, &entry, INSERT);
16394 if (*slot)
16395 result = *slot;
16396 else
16397 {
16398 result = XNEW (struct strtab_entry);
16399 result->offset = obstack_object_size (cpool);
16400 result->str = str;
16401 obstack_grow_str0 (cpool, str);
16402 *slot = result;
16403 }
16404 return result->offset;
16405}
16406
16407/* An entry in the symbol table. */
16408struct symtab_index_entry
16409{
16410 /* The name of the symbol. */
16411 const char *name;
16412 /* The offset of the name in the constant pool. */
16413 offset_type index_offset;
16414 /* A sorted vector of the indices of all the CUs that hold an object
16415 of this name. */
16416 VEC (offset_type) *cu_indices;
16417};
16418
16419/* The symbol table. This is a power-of-2-sized hash table. */
16420struct mapped_symtab
16421{
16422 offset_type n_elements;
16423 offset_type size;
16424 struct symtab_index_entry **data;
16425};
16426
16427/* Hash function for a symtab_index_entry. */
b89be57b 16428
9291a0cd
TT
16429static hashval_t
16430hash_symtab_entry (const void *e)
16431{
16432 const struct symtab_index_entry *entry = e;
16433 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16434 sizeof (offset_type) * VEC_length (offset_type,
16435 entry->cu_indices),
16436 0);
16437}
16438
16439/* Equality function for a symtab_index_entry. */
b89be57b 16440
9291a0cd
TT
16441static int
16442eq_symtab_entry (const void *a, const void *b)
16443{
16444 const struct symtab_index_entry *ea = a;
16445 const struct symtab_index_entry *eb = b;
16446 int len = VEC_length (offset_type, ea->cu_indices);
16447 if (len != VEC_length (offset_type, eb->cu_indices))
16448 return 0;
16449 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16450 VEC_address (offset_type, eb->cu_indices),
16451 sizeof (offset_type) * len);
16452}
16453
16454/* Destroy a symtab_index_entry. */
b89be57b 16455
9291a0cd
TT
16456static void
16457delete_symtab_entry (void *p)
16458{
16459 struct symtab_index_entry *entry = p;
16460 VEC_free (offset_type, entry->cu_indices);
16461 xfree (entry);
16462}
16463
16464/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16465
9291a0cd 16466static htab_t
3876f04e 16467create_symbol_hash_table (void)
9291a0cd
TT
16468{
16469 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16470 delete_symtab_entry, xcalloc, xfree);
16471}
16472
16473/* Create a new mapped symtab object. */
b89be57b 16474
9291a0cd
TT
16475static struct mapped_symtab *
16476create_mapped_symtab (void)
16477{
16478 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16479 symtab->n_elements = 0;
16480 symtab->size = 1024;
16481 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16482 return symtab;
16483}
16484
16485/* Destroy a mapped_symtab. */
b89be57b 16486
9291a0cd
TT
16487static void
16488cleanup_mapped_symtab (void *p)
16489{
16490 struct mapped_symtab *symtab = p;
16491 /* The contents of the array are freed when the other hash table is
16492 destroyed. */
16493 xfree (symtab->data);
16494 xfree (symtab);
16495}
16496
16497/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16498 the slot.
16499
16500 Function is used only during write_hash_table so no index format backward
16501 compatibility is needed. */
b89be57b 16502
9291a0cd
TT
16503static struct symtab_index_entry **
16504find_slot (struct mapped_symtab *symtab, const char *name)
16505{
559a7a62 16506 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16507
16508 index = hash & (symtab->size - 1);
16509 step = ((hash * 17) & (symtab->size - 1)) | 1;
16510
16511 for (;;)
16512 {
16513 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16514 return &symtab->data[index];
16515 index = (index + step) & (symtab->size - 1);
16516 }
16517}
16518
16519/* Expand SYMTAB's hash table. */
b89be57b 16520
9291a0cd
TT
16521static void
16522hash_expand (struct mapped_symtab *symtab)
16523{
16524 offset_type old_size = symtab->size;
16525 offset_type i;
16526 struct symtab_index_entry **old_entries = symtab->data;
16527
16528 symtab->size *= 2;
16529 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16530
16531 for (i = 0; i < old_size; ++i)
16532 {
16533 if (old_entries[i])
16534 {
16535 struct symtab_index_entry **slot = find_slot (symtab,
16536 old_entries[i]->name);
16537 *slot = old_entries[i];
16538 }
16539 }
16540
16541 xfree (old_entries);
16542}
16543
16544/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16545 is the index of the CU in which the symbol appears. */
b89be57b 16546
9291a0cd
TT
16547static void
16548add_index_entry (struct mapped_symtab *symtab, const char *name,
16549 offset_type cu_index)
16550{
16551 struct symtab_index_entry **slot;
16552
16553 ++symtab->n_elements;
16554 if (4 * symtab->n_elements / 3 >= symtab->size)
16555 hash_expand (symtab);
16556
16557 slot = find_slot (symtab, name);
16558 if (!*slot)
16559 {
16560 *slot = XNEW (struct symtab_index_entry);
16561 (*slot)->name = name;
16562 (*slot)->cu_indices = NULL;
16563 }
16564 /* Don't push an index twice. Due to how we add entries we only
16565 have to check the last one. */
16566 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16567 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16568 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16569}
16570
16571/* Add a vector of indices to the constant pool. */
b89be57b 16572
9291a0cd 16573static offset_type
3876f04e 16574add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16575 struct symtab_index_entry *entry)
16576{
16577 void **slot;
16578
3876f04e 16579 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16580 if (!*slot)
16581 {
16582 offset_type len = VEC_length (offset_type, entry->cu_indices);
16583 offset_type val = MAYBE_SWAP (len);
16584 offset_type iter;
16585 int i;
16586
16587 *slot = entry;
16588 entry->index_offset = obstack_object_size (cpool);
16589
16590 obstack_grow (cpool, &val, sizeof (val));
16591 for (i = 0;
16592 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16593 ++i)
16594 {
16595 val = MAYBE_SWAP (iter);
16596 obstack_grow (cpool, &val, sizeof (val));
16597 }
16598 }
16599 else
16600 {
16601 struct symtab_index_entry *old_entry = *slot;
16602 entry->index_offset = old_entry->index_offset;
16603 entry = old_entry;
16604 }
16605 return entry->index_offset;
16606}
16607
16608/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16609 constant pool entries going into the obstack CPOOL. */
b89be57b 16610
9291a0cd
TT
16611static void
16612write_hash_table (struct mapped_symtab *symtab,
16613 struct obstack *output, struct obstack *cpool)
16614{
16615 offset_type i;
3876f04e 16616 htab_t symbol_hash_table;
9291a0cd
TT
16617 htab_t str_table;
16618
3876f04e 16619 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16620 str_table = create_strtab ();
3876f04e 16621
9291a0cd
TT
16622 /* We add all the index vectors to the constant pool first, to
16623 ensure alignment is ok. */
16624 for (i = 0; i < symtab->size; ++i)
16625 {
16626 if (symtab->data[i])
3876f04e 16627 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16628 }
16629
16630 /* Now write out the hash table. */
16631 for (i = 0; i < symtab->size; ++i)
16632 {
16633 offset_type str_off, vec_off;
16634
16635 if (symtab->data[i])
16636 {
16637 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16638 vec_off = symtab->data[i]->index_offset;
16639 }
16640 else
16641 {
16642 /* While 0 is a valid constant pool index, it is not valid
16643 to have 0 for both offsets. */
16644 str_off = 0;
16645 vec_off = 0;
16646 }
16647
16648 str_off = MAYBE_SWAP (str_off);
16649 vec_off = MAYBE_SWAP (vec_off);
16650
16651 obstack_grow (output, &str_off, sizeof (str_off));
16652 obstack_grow (output, &vec_off, sizeof (vec_off));
16653 }
16654
16655 htab_delete (str_table);
3876f04e 16656 htab_delete (symbol_hash_table);
9291a0cd
TT
16657}
16658
0a5429f6
DE
16659/* Struct to map psymtab to CU index in the index file. */
16660struct psymtab_cu_index_map
16661{
16662 struct partial_symtab *psymtab;
16663 unsigned int cu_index;
16664};
16665
16666static hashval_t
16667hash_psymtab_cu_index (const void *item)
16668{
16669 const struct psymtab_cu_index_map *map = item;
16670
16671 return htab_hash_pointer (map->psymtab);
16672}
16673
16674static int
16675eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16676{
16677 const struct psymtab_cu_index_map *lhs = item_lhs;
16678 const struct psymtab_cu_index_map *rhs = item_rhs;
16679
16680 return lhs->psymtab == rhs->psymtab;
16681}
16682
16683/* Helper struct for building the address table. */
16684struct addrmap_index_data
16685{
16686 struct objfile *objfile;
16687 struct obstack *addr_obstack;
16688 htab_t cu_index_htab;
16689
16690 /* Non-zero if the previous_* fields are valid.
16691 We can't write an entry until we see the next entry (since it is only then
16692 that we know the end of the entry). */
16693 int previous_valid;
16694 /* Index of the CU in the table of all CUs in the index file. */
16695 unsigned int previous_cu_index;
0963b4bd 16696 /* Start address of the CU. */
0a5429f6
DE
16697 CORE_ADDR previous_cu_start;
16698};
16699
16700/* Write an address entry to OBSTACK. */
b89be57b 16701
9291a0cd 16702static void
0a5429f6
DE
16703add_address_entry (struct objfile *objfile, struct obstack *obstack,
16704 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16705{
0a5429f6 16706 offset_type cu_index_to_write;
9291a0cd
TT
16707 char addr[8];
16708 CORE_ADDR baseaddr;
16709
16710 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16711
0a5429f6
DE
16712 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16713 obstack_grow (obstack, addr, 8);
16714 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16715 obstack_grow (obstack, addr, 8);
16716 cu_index_to_write = MAYBE_SWAP (cu_index);
16717 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16718}
16719
16720/* Worker function for traversing an addrmap to build the address table. */
16721
16722static int
16723add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16724{
16725 struct addrmap_index_data *data = datap;
16726 struct partial_symtab *pst = obj;
16727 offset_type cu_index;
16728 void **slot;
16729
16730 if (data->previous_valid)
16731 add_address_entry (data->objfile, data->addr_obstack,
16732 data->previous_cu_start, start_addr,
16733 data->previous_cu_index);
16734
16735 data->previous_cu_start = start_addr;
16736 if (pst != NULL)
16737 {
16738 struct psymtab_cu_index_map find_map, *map;
16739 find_map.psymtab = pst;
16740 map = htab_find (data->cu_index_htab, &find_map);
16741 gdb_assert (map != NULL);
16742 data->previous_cu_index = map->cu_index;
16743 data->previous_valid = 1;
16744 }
16745 else
16746 data->previous_valid = 0;
16747
16748 return 0;
16749}
16750
16751/* Write OBJFILE's address map to OBSTACK.
16752 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16753 in the index file. */
16754
16755static void
16756write_address_map (struct objfile *objfile, struct obstack *obstack,
16757 htab_t cu_index_htab)
16758{
16759 struct addrmap_index_data addrmap_index_data;
16760
16761 /* When writing the address table, we have to cope with the fact that
16762 the addrmap iterator only provides the start of a region; we have to
16763 wait until the next invocation to get the start of the next region. */
16764
16765 addrmap_index_data.objfile = objfile;
16766 addrmap_index_data.addr_obstack = obstack;
16767 addrmap_index_data.cu_index_htab = cu_index_htab;
16768 addrmap_index_data.previous_valid = 0;
16769
16770 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16771 &addrmap_index_data);
16772
16773 /* It's highly unlikely the last entry (end address = 0xff...ff)
16774 is valid, but we should still handle it.
16775 The end address is recorded as the start of the next region, but that
16776 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16777 anyway. */
16778 if (addrmap_index_data.previous_valid)
16779 add_address_entry (objfile, obstack,
16780 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16781 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16782}
16783
16784/* Add a list of partial symbols to SYMTAB. */
b89be57b 16785
9291a0cd
TT
16786static void
16787write_psymbols (struct mapped_symtab *symtab,
987d643c 16788 htab_t psyms_seen,
9291a0cd
TT
16789 struct partial_symbol **psymp,
16790 int count,
987d643c
TT
16791 offset_type cu_index,
16792 int is_static)
9291a0cd
TT
16793{
16794 for (; count-- > 0; ++psymp)
16795 {
987d643c
TT
16796 void **slot, *lookup;
16797
9291a0cd
TT
16798 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16799 error (_("Ada is not currently supported by the index"));
987d643c
TT
16800
16801 /* We only want to add a given psymbol once. However, we also
16802 want to account for whether it is global or static. So, we
16803 may add it twice, using slightly different values. */
16804 if (is_static)
16805 {
16806 uintptr_t val = 1 | (uintptr_t) *psymp;
16807
16808 lookup = (void *) val;
16809 }
16810 else
16811 lookup = *psymp;
16812
16813 /* Only add a given psymbol once. */
16814 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16815 if (!*slot)
16816 {
16817 *slot = lookup;
bb2f58dc 16818 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16819 }
9291a0cd
TT
16820 }
16821}
16822
16823/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16824 exception if there is an error. */
b89be57b 16825
9291a0cd
TT
16826static void
16827write_obstack (FILE *file, struct obstack *obstack)
16828{
16829 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16830 file)
16831 != obstack_object_size (obstack))
16832 error (_("couldn't data write to file"));
16833}
16834
16835/* Unlink a file if the argument is not NULL. */
b89be57b 16836
9291a0cd
TT
16837static void
16838unlink_if_set (void *p)
16839{
16840 char **filename = p;
16841 if (*filename)
16842 unlink (*filename);
16843}
16844
1fd400ff
TT
16845/* A helper struct used when iterating over debug_types. */
16846struct signatured_type_index_data
16847{
16848 struct objfile *objfile;
16849 struct mapped_symtab *symtab;
16850 struct obstack *types_list;
987d643c 16851 htab_t psyms_seen;
1fd400ff
TT
16852 int cu_index;
16853};
16854
16855/* A helper function that writes a single signatured_type to an
16856 obstack. */
b89be57b 16857
1fd400ff
TT
16858static int
16859write_one_signatured_type (void **slot, void *d)
16860{
16861 struct signatured_type_index_data *info = d;
16862 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16863 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16864 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16865 gdb_byte val[8];
16866
16867 write_psymbols (info->symtab,
987d643c 16868 info->psyms_seen,
3e43a32a
MS
16869 info->objfile->global_psymbols.list
16870 + psymtab->globals_offset,
987d643c
TT
16871 psymtab->n_global_syms, info->cu_index,
16872 0);
1fd400ff 16873 write_psymbols (info->symtab,
987d643c 16874 info->psyms_seen,
3e43a32a
MS
16875 info->objfile->static_psymbols.list
16876 + psymtab->statics_offset,
987d643c
TT
16877 psymtab->n_static_syms, info->cu_index,
16878 1);
1fd400ff 16879
b3c8eb43 16880 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16881 obstack_grow (info->types_list, val, 8);
16882 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16883 obstack_grow (info->types_list, val, 8);
16884 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16885 obstack_grow (info->types_list, val, 8);
16886
16887 ++info->cu_index;
16888
16889 return 1;
16890}
16891
9291a0cd 16892/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16893
9291a0cd
TT
16894static void
16895write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16896{
16897 struct cleanup *cleanup;
16898 char *filename, *cleanup_filename;
1fd400ff
TT
16899 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16900 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16901 int i;
16902 FILE *out_file;
16903 struct mapped_symtab *symtab;
16904 offset_type val, size_of_contents, total_len;
16905 struct stat st;
16906 char buf[8];
987d643c 16907 htab_t psyms_seen;
0a5429f6
DE
16908 htab_t cu_index_htab;
16909 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16910
b4f2f049 16911 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16912 return;
b4f2f049 16913
9291a0cd
TT
16914 if (dwarf2_per_objfile->using_index)
16915 error (_("Cannot use an index to create the index"));
16916
8b70b953
TT
16917 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16918 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16919
9291a0cd 16920 if (stat (objfile->name, &st) < 0)
7e17e088 16921 perror_with_name (objfile->name);
9291a0cd
TT
16922
16923 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16924 INDEX_SUFFIX, (char *) NULL);
16925 cleanup = make_cleanup (xfree, filename);
16926
16927 out_file = fopen (filename, "wb");
16928 if (!out_file)
16929 error (_("Can't open `%s' for writing"), filename);
16930
16931 cleanup_filename = filename;
16932 make_cleanup (unlink_if_set, &cleanup_filename);
16933
16934 symtab = create_mapped_symtab ();
16935 make_cleanup (cleanup_mapped_symtab, symtab);
16936
16937 obstack_init (&addr_obstack);
16938 make_cleanup_obstack_free (&addr_obstack);
16939
16940 obstack_init (&cu_list);
16941 make_cleanup_obstack_free (&cu_list);
16942
1fd400ff
TT
16943 obstack_init (&types_cu_list);
16944 make_cleanup_obstack_free (&types_cu_list);
16945
987d643c
TT
16946 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16947 NULL, xcalloc, xfree);
96408a79 16948 make_cleanup_htab_delete (psyms_seen);
987d643c 16949
0a5429f6
DE
16950 /* While we're scanning CU's create a table that maps a psymtab pointer
16951 (which is what addrmap records) to its index (which is what is recorded
16952 in the index file). This will later be needed to write the address
16953 table. */
16954 cu_index_htab = htab_create_alloc (100,
16955 hash_psymtab_cu_index,
16956 eq_psymtab_cu_index,
16957 NULL, xcalloc, xfree);
96408a79 16958 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16959 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16960 xmalloc (sizeof (struct psymtab_cu_index_map)
16961 * dwarf2_per_objfile->n_comp_units);
16962 make_cleanup (xfree, psymtab_cu_index_map);
16963
16964 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16965 work here. Also, the debug_types entries do not appear in
16966 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16967 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16968 {
3e43a32a
MS
16969 struct dwarf2_per_cu_data *per_cu
16970 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16971 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16972 gdb_byte val[8];
0a5429f6
DE
16973 struct psymtab_cu_index_map *map;
16974 void **slot;
9291a0cd
TT
16975
16976 write_psymbols (symtab,
987d643c 16977 psyms_seen,
9291a0cd 16978 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16979 psymtab->n_global_syms, i,
16980 0);
9291a0cd 16981 write_psymbols (symtab,
987d643c 16982 psyms_seen,
9291a0cd 16983 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16984 psymtab->n_static_syms, i,
16985 1);
9291a0cd 16986
0a5429f6
DE
16987 map = &psymtab_cu_index_map[i];
16988 map->psymtab = psymtab;
16989 map->cu_index = i;
16990 slot = htab_find_slot (cu_index_htab, map, INSERT);
16991 gdb_assert (slot != NULL);
16992 gdb_assert (*slot == NULL);
16993 *slot = map;
9291a0cd 16994
e254ef6a 16995 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16996 obstack_grow (&cu_list, val, 8);
e254ef6a 16997 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16998 obstack_grow (&cu_list, val, 8);
16999 }
17000
0a5429f6
DE
17001 /* Dump the address map. */
17002 write_address_map (objfile, &addr_obstack, cu_index_htab);
17003
1fd400ff
TT
17004 /* Write out the .debug_type entries, if any. */
17005 if (dwarf2_per_objfile->signatured_types)
17006 {
17007 struct signatured_type_index_data sig_data;
17008
17009 sig_data.objfile = objfile;
17010 sig_data.symtab = symtab;
17011 sig_data.types_list = &types_cu_list;
987d643c 17012 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17013 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17014 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17015 write_one_signatured_type, &sig_data);
17016 }
17017
9291a0cd
TT
17018 obstack_init (&constant_pool);
17019 make_cleanup_obstack_free (&constant_pool);
17020 obstack_init (&symtab_obstack);
17021 make_cleanup_obstack_free (&symtab_obstack);
17022 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17023
17024 obstack_init (&contents);
17025 make_cleanup_obstack_free (&contents);
1fd400ff 17026 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17027 total_len = size_of_contents;
17028
17029 /* The version number. */
559a7a62 17030 val = MAYBE_SWAP (5);
9291a0cd
TT
17031 obstack_grow (&contents, &val, sizeof (val));
17032
17033 /* The offset of the CU list from the start of the file. */
17034 val = MAYBE_SWAP (total_len);
17035 obstack_grow (&contents, &val, sizeof (val));
17036 total_len += obstack_object_size (&cu_list);
17037
1fd400ff
TT
17038 /* The offset of the types CU list from the start of the file. */
17039 val = MAYBE_SWAP (total_len);
17040 obstack_grow (&contents, &val, sizeof (val));
17041 total_len += obstack_object_size (&types_cu_list);
17042
9291a0cd
TT
17043 /* The offset of the address table from the start of the file. */
17044 val = MAYBE_SWAP (total_len);
17045 obstack_grow (&contents, &val, sizeof (val));
17046 total_len += obstack_object_size (&addr_obstack);
17047
17048 /* The offset of the symbol table from the start of the file. */
17049 val = MAYBE_SWAP (total_len);
17050 obstack_grow (&contents, &val, sizeof (val));
17051 total_len += obstack_object_size (&symtab_obstack);
17052
17053 /* The offset of the constant pool from the start of the file. */
17054 val = MAYBE_SWAP (total_len);
17055 obstack_grow (&contents, &val, sizeof (val));
17056 total_len += obstack_object_size (&constant_pool);
17057
17058 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17059
17060 write_obstack (out_file, &contents);
17061 write_obstack (out_file, &cu_list);
1fd400ff 17062 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17063 write_obstack (out_file, &addr_obstack);
17064 write_obstack (out_file, &symtab_obstack);
17065 write_obstack (out_file, &constant_pool);
17066
17067 fclose (out_file);
17068
17069 /* We want to keep the file, so we set cleanup_filename to NULL
17070 here. See unlink_if_set. */
17071 cleanup_filename = NULL;
17072
17073 do_cleanups (cleanup);
17074}
17075
90476074
TT
17076/* Implementation of the `save gdb-index' command.
17077
17078 Note that the file format used by this command is documented in the
17079 GDB manual. Any changes here must be documented there. */
11570e71 17080
9291a0cd
TT
17081static void
17082save_gdb_index_command (char *arg, int from_tty)
17083{
17084 struct objfile *objfile;
17085
17086 if (!arg || !*arg)
96d19272 17087 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17088
17089 ALL_OBJFILES (objfile)
17090 {
17091 struct stat st;
17092
17093 /* If the objfile does not correspond to an actual file, skip it. */
17094 if (stat (objfile->name, &st) < 0)
17095 continue;
17096
17097 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17098 if (dwarf2_per_objfile)
17099 {
17100 volatile struct gdb_exception except;
17101
17102 TRY_CATCH (except, RETURN_MASK_ERROR)
17103 {
17104 write_psymtabs_to_index (objfile, arg);
17105 }
17106 if (except.reason < 0)
17107 exception_fprintf (gdb_stderr, except,
17108 _("Error while writing index for `%s': "),
17109 objfile->name);
17110 }
17111 }
dce234bc
PP
17112}
17113
9291a0cd
TT
17114\f
17115
9eae7c52
TT
17116int dwarf2_always_disassemble;
17117
17118static void
17119show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17120 struct cmd_list_element *c, const char *value)
17121{
3e43a32a
MS
17122 fprintf_filtered (file,
17123 _("Whether to always disassemble "
17124 "DWARF expressions is %s.\n"),
9eae7c52
TT
17125 value);
17126}
17127
900e11f9
JK
17128static void
17129show_check_physname (struct ui_file *file, int from_tty,
17130 struct cmd_list_element *c, const char *value)
17131{
17132 fprintf_filtered (file,
17133 _("Whether to check \"physname\" is %s.\n"),
17134 value);
17135}
17136
6502dd73
DJ
17137void _initialize_dwarf2_read (void);
17138
17139void
17140_initialize_dwarf2_read (void)
17141{
96d19272
JK
17142 struct cmd_list_element *c;
17143
dce234bc 17144 dwarf2_objfile_data_key
c1bd65d0 17145 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17146
1bedd215
AC
17147 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17148Set DWARF 2 specific variables.\n\
17149Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17150 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17151 0/*allow-unknown*/, &maintenance_set_cmdlist);
17152
1bedd215
AC
17153 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17154Show DWARF 2 specific variables\n\
17155Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17156 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17157 0/*allow-unknown*/, &maintenance_show_cmdlist);
17158
17159 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17160 &dwarf2_max_cache_age, _("\
17161Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17162Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17163A higher limit means that cached compilation units will be stored\n\
17164in memory longer, and more total memory will be used. Zero disables\n\
17165caching, which can slow down startup."),
2c5b56ce 17166 NULL,
920d2a44 17167 show_dwarf2_max_cache_age,
2c5b56ce 17168 &set_dwarf2_cmdlist,
ae038cb0 17169 &show_dwarf2_cmdlist);
d97bc12b 17170
9eae7c52
TT
17171 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17172 &dwarf2_always_disassemble, _("\
17173Set whether `info address' always disassembles DWARF expressions."), _("\
17174Show whether `info address' always disassembles DWARF expressions."), _("\
17175When enabled, DWARF expressions are always printed in an assembly-like\n\
17176syntax. When disabled, expressions will be printed in a more\n\
17177conversational style, when possible."),
17178 NULL,
17179 show_dwarf2_always_disassemble,
17180 &set_dwarf2_cmdlist,
17181 &show_dwarf2_cmdlist);
17182
d97bc12b
DE
17183 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17184Set debugging of the dwarf2 DIE reader."), _("\
17185Show debugging of the dwarf2 DIE reader."), _("\
17186When enabled (non-zero), DIEs are dumped after they are read in.\n\
17187The value is the maximum depth to print."),
17188 NULL,
17189 NULL,
17190 &setdebuglist, &showdebuglist);
9291a0cd 17191
900e11f9
JK
17192 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17193Set cross-checking of \"physname\" code against demangler."), _("\
17194Show cross-checking of \"physname\" code against demangler."), _("\
17195When enabled, GDB's internal \"physname\" code is checked against\n\
17196the demangler."),
17197 NULL, show_check_physname,
17198 &setdebuglist, &showdebuglist);
17199
96d19272 17200 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17201 _("\
fc1a9d6e 17202Save a gdb-index file.\n\
11570e71 17203Usage: save gdb-index DIRECTORY"),
96d19272
JK
17204 &save_cmdlist);
17205 set_cmd_completer (c, filename_completer);
6502dd73 17206}
This page took 2.374364 seconds and 4 git commands to generate.