* dwarf2read.c (dwarf2_per_cu_addr_size): Make result type an int.
[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
SS
26
27#include "defs.h"
28#include "bfd.h"
c906108c
SS
29#include "symtab.h"
30#include "gdbtypes.h"
c906108c 31#include "objfiles.h"
fa8f86ff 32#include "dwarf2.h"
c906108c
SS
33#include "buildsym.h"
34#include "demangle.h"
50f182aa 35#include "gdb-demangle.h"
c906108c 36#include "expression.h"
d5166ae1 37#include "filenames.h" /* for DOSish file names */
2e276125 38#include "macrotab.h"
c906108c
SS
39#include "language.h"
40#include "complaints.h"
357e46e7 41#include "bcache.h"
4c2df51b
DJ
42#include "dwarf2expr.h"
43#include "dwarf2loc.h"
9219021c 44#include "cp-support.h"
72bf9492 45#include "hashtab.h"
ae038cb0
DJ
46#include "command.h"
47#include "gdbcmd.h"
edb3359d 48#include "block.h"
ff013f42 49#include "addrmap.h"
94af9270
KS
50#include "typeprint.h"
51#include "jv-lang.h"
ccefe4c4 52#include "psympriv.h"
9291a0cd
TT
53#include "exceptions.h"
54#include "gdb_stat.h"
96d19272 55#include "completer.h"
34eaf542 56#include "vec.h"
98bfdba5
PA
57#include "c-lang.h"
58#include "valprint.h"
60d5a603 59#include <ctype.h>
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
c906108c
SS
78/* .debug_line statement program prologue
79 Because of alignment constraints, this structure has padding and cannot
80 be mapped directly onto the beginning of the .debug_info section. */
81typedef struct statement_prologue
82 {
83 unsigned int total_length; /* byte length of the statement
84 information */
85 unsigned short version; /* version number -- 2 for DWARF
86 version 2 */
87 unsigned int prologue_length; /* # bytes between prologue &
88 stmt program */
89 unsigned char minimum_instruction_length; /* byte size of
90 smallest instr */
91 unsigned char default_is_stmt; /* initial value of is_stmt
92 register */
93 char line_base;
94 unsigned char line_range;
95 unsigned char opcode_base; /* number assigned to first special
96 opcode */
97 unsigned char *standard_opcode_lengths;
98 }
99_STATEMENT_PROLOGUE;
100
d97bc12b
DE
101/* When non-zero, dump DIEs after they are read in. */
102static int dwarf2_die_debug = 0;
103
900e11f9
JK
104/* When non-zero, cross-check physname against demangler. */
105static int check_physname = 0;
106
dce234bc
PP
107static int pagesize;
108
df8a16a1
DJ
109/* When set, the file that we're processing is known to have debugging
110 info for C++ namespaces. GCC 3.3.x did not produce this information,
111 but later versions do. */
112
113static int processing_has_namespace_info;
114
6502dd73
DJ
115static const struct objfile_data *dwarf2_objfile_data_key;
116
dce234bc
PP
117struct dwarf2_section_info
118{
119 asection *asection;
120 gdb_byte *buffer;
121 bfd_size_type size;
b315ab21
TG
122 /* Not NULL if the section was actually mmapped. */
123 void *map_addr;
124 /* Page aligned size of mmapped area. */
125 bfd_size_type map_len;
be391dca
TT
126 /* True if we have tried to read this section. */
127 int readin;
dce234bc
PP
128};
129
8b70b953
TT
130typedef struct dwarf2_section_info dwarf2_section_info_def;
131DEF_VEC_O (dwarf2_section_info_def);
132
9291a0cd
TT
133/* All offsets in the index are of this type. It must be
134 architecture-independent. */
135typedef uint32_t offset_type;
136
137DEF_VEC_I (offset_type);
138
139/* A description of the mapped index. The file format is described in
140 a comment by the code that writes the index. */
141struct mapped_index
142{
559a7a62
JK
143 /* Index data format version. */
144 int version;
145
9291a0cd
TT
146 /* The total length of the buffer. */
147 off_t total_size;
b11b1f88 148
9291a0cd
TT
149 /* A pointer to the address table data. */
150 const gdb_byte *address_table;
b11b1f88 151
9291a0cd
TT
152 /* Size of the address table data in bytes. */
153 offset_type address_table_size;
b11b1f88 154
3876f04e
DE
155 /* The symbol table, implemented as a hash table. */
156 const offset_type *symbol_table;
b11b1f88 157
9291a0cd 158 /* Size in slots, each slot is 2 offset_types. */
3876f04e 159 offset_type symbol_table_slots;
b11b1f88 160
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
9cdd5dbd
DE
165/* Collection of data recorded per objfile.
166 This hangs off of dwarf2_objfile_data_key. */
167
6502dd73
DJ
168struct dwarf2_per_objfile
169{
dce234bc
PP
170 struct dwarf2_section_info info;
171 struct dwarf2_section_info abbrev;
172 struct dwarf2_section_info line;
dce234bc
PP
173 struct dwarf2_section_info loc;
174 struct dwarf2_section_info macinfo;
cf2c3c16 175 struct dwarf2_section_info macro;
dce234bc
PP
176 struct dwarf2_section_info str;
177 struct dwarf2_section_info ranges;
178 struct dwarf2_section_info frame;
179 struct dwarf2_section_info eh_frame;
9291a0cd 180 struct dwarf2_section_info gdb_index;
ae038cb0 181
8b70b953
TT
182 VEC (dwarf2_section_info_def) *types;
183
be391dca
TT
184 /* Back link. */
185 struct objfile *objfile;
186
10b3939b
DJ
187 /* A list of all the compilation units. This is used to locate
188 the target compilation unit of a particular reference. */
ae038cb0
DJ
189 struct dwarf2_per_cu_data **all_comp_units;
190
191 /* The number of compilation units in ALL_COMP_UNITS. */
192 int n_comp_units;
193
1fd400ff
TT
194 /* The number of .debug_types-related CUs. */
195 int n_type_comp_units;
196
197 /* The .debug_types-related CUs. */
198 struct dwarf2_per_cu_data **type_comp_units;
199
ae038cb0
DJ
200 /* A chain of compilation units that are currently read in, so that
201 they can be freed later. */
202 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 203
348e048f
DE
204 /* A table mapping .debug_types signatures to its signatured_type entry.
205 This is NULL if the .debug_types section hasn't been read in yet. */
206 htab_t signatured_types;
207
72dca2f5
FR
208 /* A flag indicating wether this objfile has a section loaded at a
209 VMA of 0. */
210 int has_section_at_zero;
9291a0cd 211
ae2de4f8
DE
212 /* True if we are using the mapped index,
213 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
214 unsigned char using_index;
215
ae2de4f8 216 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 217 struct mapped_index *index_table;
98bfdba5 218
7b9f3c50
DE
219 /* When using index_table, this keeps track of all quick_file_names entries.
220 TUs can share line table entries with CUs or other TUs, and there can be
221 a lot more TUs than unique line tables, so we maintain a separate table
222 of all line table entries to support the sharing. */
223 htab_t quick_file_names_table;
224
98bfdba5
PA
225 /* Set during partial symbol reading, to prevent queueing of full
226 symbols. */
227 int reading_partial_symbols;
673bfd45
DE
228
229 /* Table mapping type .debug_info DIE offsets to types.
230 This is NULL if not allocated yet.
231 It (currently) makes sense to allocate debug_types_type_hash lazily.
232 To keep things simple we allocate both lazily. */
233 htab_t debug_info_type_hash;
234
235 /* Table mapping type .debug_types DIE offsets to types.
236 This is NULL if not allocated yet. */
237 htab_t debug_types_type_hash;
6502dd73
DJ
238};
239
240static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 241
251d32d9 242/* Default names of the debugging sections. */
c906108c 243
233a11ab
CS
244/* Note that if the debugging section has been compressed, it might
245 have a name like .zdebug_info. */
246
9cdd5dbd
DE
247static const struct dwarf2_debug_sections dwarf2_elf_names =
248{
251d32d9
TG
249 { ".debug_info", ".zdebug_info" },
250 { ".debug_abbrev", ".zdebug_abbrev" },
251 { ".debug_line", ".zdebug_line" },
252 { ".debug_loc", ".zdebug_loc" },
253 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 254 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
255 { ".debug_str", ".zdebug_str" },
256 { ".debug_ranges", ".zdebug_ranges" },
257 { ".debug_types", ".zdebug_types" },
258 { ".debug_frame", ".zdebug_frame" },
259 { ".eh_frame", NULL },
24d3216f
TT
260 { ".gdb_index", ".zgdb_index" },
261 23
251d32d9 262};
c906108c
SS
263
264/* local data types */
265
0963b4bd 266/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
267#ifndef ABBREV_HASH_SIZE
268#define ABBREV_HASH_SIZE 121
269#endif
270
107d2387
AC
271/* The data in a compilation unit header, after target2host
272 translation, looks like this. */
c906108c 273struct comp_unit_head
a738430d 274{
c764a876 275 unsigned int length;
a738430d 276 short version;
a738430d
MK
277 unsigned char addr_size;
278 unsigned char signed_addr_p;
9cbfa09e 279 unsigned int abbrev_offset;
57349743 280
a738430d
MK
281 /* Size of file offsets; either 4 or 8. */
282 unsigned int offset_size;
57349743 283
a738430d
MK
284 /* Size of the length field; either 4 or 12. */
285 unsigned int initial_length_size;
57349743 286
a738430d
MK
287 /* Offset to the first byte of this compilation unit header in the
288 .debug_info section, for resolving relative reference dies. */
289 unsigned int offset;
57349743 290
d00adf39
DE
291 /* Offset to first die in this cu from the start of the cu.
292 This will be the first byte following the compilation unit header. */
293 unsigned int first_die_offset;
a738430d 294};
c906108c 295
3da10d80
KS
296/* Type used for delaying computation of method physnames.
297 See comments for compute_delayed_physnames. */
298struct delayed_method_info
299{
300 /* The type to which the method is attached, i.e., its parent class. */
301 struct type *type;
302
303 /* The index of the method in the type's function fieldlists. */
304 int fnfield_index;
305
306 /* The index of the method in the fieldlist. */
307 int index;
308
309 /* The name of the DIE. */
310 const char *name;
311
312 /* The DIE associated with this method. */
313 struct die_info *die;
314};
315
316typedef struct delayed_method_info delayed_method_info;
317DEF_VEC_O (delayed_method_info);
318
e7c27a73
DJ
319/* Internal state when decoding a particular compilation unit. */
320struct dwarf2_cu
321{
322 /* The objfile containing this compilation unit. */
323 struct objfile *objfile;
324
d00adf39 325 /* The header of the compilation unit. */
e7c27a73 326 struct comp_unit_head header;
e142c38c 327
d00adf39
DE
328 /* Base address of this compilation unit. */
329 CORE_ADDR base_address;
330
331 /* Non-zero if base_address has been set. */
332 int base_known;
333
e142c38c
DJ
334 /* The language we are debugging. */
335 enum language language;
336 const struct language_defn *language_defn;
337
b0f35d58
DL
338 const char *producer;
339
e142c38c
DJ
340 /* The generic symbol table building routines have separate lists for
341 file scope symbols and all all other scopes (local scopes). So
342 we need to select the right one to pass to add_symbol_to_list().
343 We do it by keeping a pointer to the correct list in list_in_scope.
344
345 FIXME: The original dwarf code just treated the file scope as the
346 first local scope, and all other local scopes as nested local
347 scopes, and worked fine. Check to see if we really need to
348 distinguish these in buildsym.c. */
349 struct pending **list_in_scope;
350
f3dd6933
DJ
351 /* DWARF abbreviation table associated with this compilation unit. */
352 struct abbrev_info **dwarf2_abbrevs;
353
354 /* Storage for the abbrev table. */
355 struct obstack abbrev_obstack;
72bf9492
DJ
356
357 /* Hash table holding all the loaded partial DIEs. */
358 htab_t partial_dies;
359
360 /* Storage for things with the same lifetime as this read-in compilation
361 unit, including partial DIEs. */
362 struct obstack comp_unit_obstack;
363
ae038cb0
DJ
364 /* When multiple dwarf2_cu structures are living in memory, this field
365 chains them all together, so that they can be released efficiently.
366 We will probably also want a generation counter so that most-recently-used
367 compilation units are cached... */
368 struct dwarf2_per_cu_data *read_in_chain;
369
370 /* Backchain to our per_cu entry if the tree has been built. */
371 struct dwarf2_per_cu_data *per_cu;
372
373 /* How many compilation units ago was this CU last referenced? */
374 int last_used;
375
10b3939b 376 /* A hash table of die offsets for following references. */
51545339 377 htab_t die_hash;
10b3939b
DJ
378
379 /* Full DIEs if read in. */
380 struct die_info *dies;
381
382 /* A set of pointers to dwarf2_per_cu_data objects for compilation
383 units referenced by this one. Only set during full symbol processing;
384 partial symbol tables do not have dependencies. */
385 htab_t dependencies;
386
cb1df416
DJ
387 /* Header data from the line table, during full symbol processing. */
388 struct line_header *line_header;
389
3da10d80
KS
390 /* A list of methods which need to have physnames computed
391 after all type information has been read. */
392 VEC (delayed_method_info) *method_list;
393
96408a79
SA
394 /* To be copied to symtab->call_site_htab. */
395 htab_t call_site_htab;
396
ae038cb0
DJ
397 /* Mark used when releasing cached dies. */
398 unsigned int mark : 1;
399
400 /* This flag will be set if this compilation unit might include
401 inter-compilation-unit references. */
402 unsigned int has_form_ref_addr : 1;
403
72bf9492
DJ
404 /* This flag will be set if this compilation unit includes any
405 DW_TAG_namespace DIEs. If we know that there are explicit
406 DIEs for namespaces, we don't need to try to infer them
407 from mangled names. */
408 unsigned int has_namespace_info : 1;
8be455d7
JK
409
410 /* This CU references .debug_loc. See the symtab->locations_valid field.
411 This test is imperfect as there may exist optimized debug code not using
412 any location list and still facing inlining issues if handled as
413 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 414 unsigned int has_loclist : 1;
e7c27a73
DJ
415};
416
10b3939b
DJ
417/* Persistent data held for a compilation unit, even when not
418 processing it. We put a pointer to this structure in the
28dee7f5 419 read_symtab_private field of the psymtab. */
10b3939b 420
ae038cb0
DJ
421struct dwarf2_per_cu_data
422{
348e048f 423 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 424 bytes should suffice to store the length of any compilation unit
45452591
DE
425 - if it doesn't, GDB will fall over anyway.
426 NOTE: Unlike comp_unit_head.length, this length includes
427 initial_length_size. */
c764a876 428 unsigned int offset;
348e048f 429 unsigned int length : 29;
ae038cb0
DJ
430
431 /* Flag indicating this compilation unit will be read in before
432 any of the current compilation units are processed. */
c764a876 433 unsigned int queued : 1;
ae038cb0 434
5afb4e99
DJ
435 /* This flag will be set if we need to load absolutely all DIEs
436 for this compilation unit, instead of just the ones we think
437 are interesting. It gets set if we look for a DIE in the
438 hash table and don't find it. */
439 unsigned int load_all_dies : 1;
440
8b70b953
TT
441 /* Non-null if this CU is from .debug_types; in which case it points
442 to the section. Otherwise it's from .debug_info. */
b0df02fd 443 struct dwarf2_section_info *debug_types_section;
348e048f 444
17ea53c3
JK
445 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
446 of the CU cache it gets reset to NULL again. */
ae038cb0 447 struct dwarf2_cu *cu;
1c379e20 448
9cdd5dbd
DE
449 /* The corresponding objfile.
450 Normally we can get the objfile from dwarf2_per_objfile.
451 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
452 struct objfile *objfile;
453
454 /* When using partial symbol tables, the 'psymtab' field is active.
455 Otherwise the 'quick' field is active. */
456 union
457 {
458 /* The partial symbol table associated with this compilation unit,
459 or NULL for partial units (which do not have an associated
460 symtab). */
461 struct partial_symtab *psymtab;
462
463 /* Data needed by the "quick" functions. */
464 struct dwarf2_per_cu_quick_data *quick;
465 } v;
ae038cb0
DJ
466};
467
348e048f
DE
468/* Entry in the signatured_types hash table. */
469
470struct signatured_type
471{
472 ULONGEST signature;
473
348e048f
DE
474 /* Offset in .debug_types of the type defined by this TU. */
475 unsigned int type_offset;
476
477 /* The CU(/TU) of this type. */
478 struct dwarf2_per_cu_data per_cu;
479};
480
0963b4bd
MS
481/* Struct used to pass misc. parameters to read_die_and_children, et
482 al. which are used for both .debug_info and .debug_types dies.
483 All parameters here are unchanging for the life of the call. This
484 struct exists to abstract away the constant parameters of die
485 reading. */
93311388
DE
486
487struct die_reader_specs
488{
489 /* The bfd of this objfile. */
490 bfd* abfd;
491
492 /* The CU of the DIE we are parsing. */
493 struct dwarf2_cu *cu;
494
495 /* Pointer to start of section buffer.
496 This is either the start of .debug_info or .debug_types. */
497 const gdb_byte *buffer;
498};
499
debd256d
JB
500/* The line number information for a compilation unit (found in the
501 .debug_line section) begins with a "statement program header",
502 which contains the following information. */
503struct line_header
504{
505 unsigned int total_length;
506 unsigned short version;
507 unsigned int header_length;
508 unsigned char minimum_instruction_length;
2dc7f7b3 509 unsigned char maximum_ops_per_instruction;
debd256d
JB
510 unsigned char default_is_stmt;
511 int line_base;
512 unsigned char line_range;
513 unsigned char opcode_base;
514
515 /* standard_opcode_lengths[i] is the number of operands for the
516 standard opcode whose value is i. This means that
517 standard_opcode_lengths[0] is unused, and the last meaningful
518 element is standard_opcode_lengths[opcode_base - 1]. */
519 unsigned char *standard_opcode_lengths;
520
521 /* The include_directories table. NOTE! These strings are not
522 allocated with xmalloc; instead, they are pointers into
523 debug_line_buffer. If you try to free them, `free' will get
524 indigestion. */
525 unsigned int num_include_dirs, include_dirs_size;
526 char **include_dirs;
527
528 /* The file_names table. NOTE! These strings are not allocated
529 with xmalloc; instead, they are pointers into debug_line_buffer.
530 Don't try to free them directly. */
531 unsigned int num_file_names, file_names_size;
532 struct file_entry
c906108c 533 {
debd256d
JB
534 char *name;
535 unsigned int dir_index;
536 unsigned int mod_time;
537 unsigned int length;
aaa75496 538 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 539 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
540 } *file_names;
541
542 /* The start and end of the statement program following this
6502dd73 543 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 544 gdb_byte *statement_program_start, *statement_program_end;
debd256d 545};
c906108c
SS
546
547/* When we construct a partial symbol table entry we only
0963b4bd 548 need this much information. */
c906108c
SS
549struct partial_die_info
550 {
72bf9492 551 /* Offset of this DIE. */
c906108c 552 unsigned int offset;
72bf9492
DJ
553
554 /* DWARF-2 tag for this DIE. */
555 ENUM_BITFIELD(dwarf_tag) tag : 16;
556
72bf9492
DJ
557 /* Assorted flags describing the data found in this DIE. */
558 unsigned int has_children : 1;
559 unsigned int is_external : 1;
560 unsigned int is_declaration : 1;
561 unsigned int has_type : 1;
562 unsigned int has_specification : 1;
563 unsigned int has_pc_info : 1;
564
565 /* Flag set if the SCOPE field of this structure has been
566 computed. */
567 unsigned int scope_set : 1;
568
fa4028e9
JB
569 /* Flag set if the DIE has a byte_size attribute. */
570 unsigned int has_byte_size : 1;
571
98bfdba5
PA
572 /* Flag set if any of the DIE's children are template arguments. */
573 unsigned int has_template_arguments : 1;
574
abc72ce4
DE
575 /* Flag set if fixup_partial_die has been called on this die. */
576 unsigned int fixup_called : 1;
577
72bf9492 578 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 579 sometimes a default name for unnamed DIEs. */
c906108c 580 char *name;
72bf9492 581
abc72ce4
DE
582 /* The linkage name, if present. */
583 const char *linkage_name;
584
72bf9492
DJ
585 /* The scope to prepend to our children. This is generally
586 allocated on the comp_unit_obstack, so will disappear
587 when this compilation unit leaves the cache. */
588 char *scope;
589
590 /* The location description associated with this DIE, if any. */
591 struct dwarf_block *locdesc;
592
593 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
594 CORE_ADDR lowpc;
595 CORE_ADDR highpc;
72bf9492 596
93311388 597 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 598 DW_AT_sibling, if any. */
abc72ce4
DE
599 /* NOTE: This member isn't strictly necessary, read_partial_die could
600 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 601 gdb_byte *sibling;
72bf9492
DJ
602
603 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
604 DW_AT_specification (or DW_AT_abstract_origin or
605 DW_AT_extension). */
606 unsigned int spec_offset;
607
608 /* Pointers to this DIE's parent, first child, and next sibling,
609 if any. */
610 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
611 };
612
0963b4bd 613/* This data structure holds the information of an abbrev. */
c906108c
SS
614struct abbrev_info
615 {
616 unsigned int number; /* number identifying abbrev */
617 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
618 unsigned short has_children; /* boolean */
619 unsigned short num_attrs; /* number of attributes */
c906108c
SS
620 struct attr_abbrev *attrs; /* an array of attribute descriptions */
621 struct abbrev_info *next; /* next in chain */
622 };
623
624struct attr_abbrev
625 {
9d25dd43
DE
626 ENUM_BITFIELD(dwarf_attribute) name : 16;
627 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
628 };
629
0963b4bd 630/* Attributes have a name and a value. */
b60c80d6
DJ
631struct attribute
632 {
9d25dd43 633 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
634 ENUM_BITFIELD(dwarf_form) form : 15;
635
636 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
637 field should be in u.str (existing only for DW_STRING) but it is kept
638 here for better struct attribute alignment. */
639 unsigned int string_is_canonical : 1;
640
b60c80d6
DJ
641 union
642 {
643 char *str;
644 struct dwarf_block *blk;
43bbcdc2
PH
645 ULONGEST unsnd;
646 LONGEST snd;
b60c80d6 647 CORE_ADDR addr;
348e048f 648 struct signatured_type *signatured_type;
b60c80d6
DJ
649 }
650 u;
651 };
652
0963b4bd 653/* This data structure holds a complete die structure. */
c906108c
SS
654struct die_info
655 {
76815b17
DE
656 /* DWARF-2 tag for this DIE. */
657 ENUM_BITFIELD(dwarf_tag) tag : 16;
658
659 /* Number of attributes */
98bfdba5
PA
660 unsigned char num_attrs;
661
662 /* True if we're presently building the full type name for the
663 type derived from this DIE. */
664 unsigned char building_fullname : 1;
76815b17
DE
665
666 /* Abbrev number */
667 unsigned int abbrev;
668
93311388 669 /* Offset in .debug_info or .debug_types section. */
76815b17 670 unsigned int offset;
78ba4af6
JB
671
672 /* The dies in a compilation unit form an n-ary tree. PARENT
673 points to this die's parent; CHILD points to the first child of
674 this node; and all the children of a given node are chained
4950bc1c 675 together via their SIBLING fields. */
639d11d3
DC
676 struct die_info *child; /* Its first child, if any. */
677 struct die_info *sibling; /* Its next sibling, if any. */
678 struct die_info *parent; /* Its parent, if any. */
c906108c 679
b60c80d6
DJ
680 /* An array of attributes, with NUM_ATTRS elements. There may be
681 zero, but it's not common and zero-sized arrays are not
682 sufficiently portable C. */
683 struct attribute attrs[1];
c906108c
SS
684 };
685
0963b4bd 686/* Get at parts of an attribute structure. */
c906108c
SS
687
688#define DW_STRING(attr) ((attr)->u.str)
8285870a 689#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
690#define DW_UNSND(attr) ((attr)->u.unsnd)
691#define DW_BLOCK(attr) ((attr)->u.blk)
692#define DW_SND(attr) ((attr)->u.snd)
693#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 694#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 695
0963b4bd 696/* Blocks are a bunch of untyped bytes. */
c906108c
SS
697struct dwarf_block
698 {
699 unsigned int size;
1d6edc3c
JK
700
701 /* Valid only if SIZE is not zero. */
fe1b8b76 702 gdb_byte *data;
c906108c
SS
703 };
704
c906108c
SS
705#ifndef ATTR_ALLOC_CHUNK
706#define ATTR_ALLOC_CHUNK 4
707#endif
708
c906108c
SS
709/* Allocate fields for structs, unions and enums in this size. */
710#ifndef DW_FIELD_ALLOC_CHUNK
711#define DW_FIELD_ALLOC_CHUNK 4
712#endif
713
c906108c
SS
714/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
715 but this would require a corresponding change in unpack_field_as_long
716 and friends. */
717static int bits_per_byte = 8;
718
719/* The routines that read and process dies for a C struct or C++ class
720 pass lists of data member fields and lists of member function fields
721 in an instance of a field_info structure, as defined below. */
722struct field_info
c5aa993b 723 {
0963b4bd 724 /* List of data member and baseclasses fields. */
c5aa993b
JM
725 struct nextfield
726 {
727 struct nextfield *next;
728 int accessibility;
729 int virtuality;
730 struct field field;
731 }
7d0ccb61 732 *fields, *baseclasses;
c906108c 733
7d0ccb61 734 /* Number of fields (including baseclasses). */
c5aa993b 735 int nfields;
c906108c 736
c5aa993b
JM
737 /* Number of baseclasses. */
738 int nbaseclasses;
c906108c 739
c5aa993b
JM
740 /* Set if the accesibility of one of the fields is not public. */
741 int non_public_fields;
c906108c 742
c5aa993b
JM
743 /* Member function fields array, entries are allocated in the order they
744 are encountered in the object file. */
745 struct nextfnfield
746 {
747 struct nextfnfield *next;
748 struct fn_field fnfield;
749 }
750 *fnfields;
c906108c 751
c5aa993b
JM
752 /* Member function fieldlist array, contains name of possibly overloaded
753 member function, number of overloaded member functions and a pointer
754 to the head of the member function field chain. */
755 struct fnfieldlist
756 {
757 char *name;
758 int length;
759 struct nextfnfield *head;
760 }
761 *fnfieldlists;
c906108c 762
c5aa993b
JM
763 /* Number of entries in the fnfieldlists array. */
764 int nfnfields;
98751a41
JK
765
766 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
767 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
768 struct typedef_field_list
769 {
770 struct typedef_field field;
771 struct typedef_field_list *next;
772 }
773 *typedef_field_list;
774 unsigned typedef_field_list_count;
c5aa993b 775 };
c906108c 776
10b3939b
DJ
777/* One item on the queue of compilation units to read in full symbols
778 for. */
779struct dwarf2_queue_item
780{
781 struct dwarf2_per_cu_data *per_cu;
782 struct dwarf2_queue_item *next;
783};
784
785/* The current queue. */
786static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
787
ae038cb0
DJ
788/* Loaded secondary compilation units are kept in memory until they
789 have not been referenced for the processing of this many
790 compilation units. Set this to zero to disable caching. Cache
791 sizes of up to at least twenty will improve startup time for
792 typical inter-CU-reference binaries, at an obvious memory cost. */
793static int dwarf2_max_cache_age = 5;
920d2a44
AC
794static void
795show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
796 struct cmd_list_element *c, const char *value)
797{
3e43a32a
MS
798 fprintf_filtered (file, _("The upper bound on the age of cached "
799 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
800 value);
801}
802
ae038cb0 803
0963b4bd 804/* Various complaints about symbol reading that don't abort the process. */
c906108c 805
4d3c2250
KB
806static void
807dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 808{
4d3c2250 809 complaint (&symfile_complaints,
e2e0b3e5 810 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
811}
812
25e43795
DJ
813static void
814dwarf2_debug_line_missing_file_complaint (void)
815{
816 complaint (&symfile_complaints,
817 _(".debug_line section has line data without a file"));
818}
819
59205f5a
JB
820static void
821dwarf2_debug_line_missing_end_sequence_complaint (void)
822{
823 complaint (&symfile_complaints,
3e43a32a
MS
824 _(".debug_line section has line "
825 "program sequence without an end"));
59205f5a
JB
826}
827
4d3c2250
KB
828static void
829dwarf2_complex_location_expr_complaint (void)
2e276125 830{
e2e0b3e5 831 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
832}
833
4d3c2250
KB
834static void
835dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
836 int arg3)
2e276125 837{
4d3c2250 838 complaint (&symfile_complaints,
3e43a32a
MS
839 _("const value length mismatch for '%s', got %d, expected %d"),
840 arg1, arg2, arg3);
4d3c2250
KB
841}
842
843static void
cf2c3c16 844dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 845{
4d3c2250 846 complaint (&symfile_complaints,
cf2c3c16
TT
847 _("macro info runs off end of `%s' section"),
848 section->asection->name);
4d3c2250
KB
849}
850
851static void
852dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 853{
4d3c2250 854 complaint (&symfile_complaints,
3e43a32a
MS
855 _("macro debug info contains a "
856 "malformed macro definition:\n`%s'"),
4d3c2250
KB
857 arg1);
858}
859
860static void
861dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 862{
4d3c2250 863 complaint (&symfile_complaints,
3e43a32a
MS
864 _("invalid attribute class or form for '%s' in '%s'"),
865 arg1, arg2);
4d3c2250 866}
c906108c 867
c906108c
SS
868/* local function prototypes */
869
4efb68b1 870static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 871
aaa75496
JB
872static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
918dd910
JK
875static void dwarf2_find_base_address (struct die_info *die,
876 struct dwarf2_cu *cu);
877
c67a9c90 878static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 879
72bf9492
DJ
880static void scan_partial_symbols (struct partial_die_info *,
881 CORE_ADDR *, CORE_ADDR *,
5734ee8b 882 int, struct dwarf2_cu *);
c906108c 883
72bf9492
DJ
884static void add_partial_symbol (struct partial_die_info *,
885 struct dwarf2_cu *);
63d06c5c 886
72bf9492
DJ
887static void add_partial_namespace (struct partial_die_info *pdi,
888 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 889 int need_pc, struct dwarf2_cu *cu);
63d06c5c 890
5d7cb8df
JK
891static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
892 CORE_ADDR *highpc, int need_pc,
893 struct dwarf2_cu *cu);
894
72bf9492
DJ
895static void add_partial_enumeration (struct partial_die_info *enum_pdi,
896 struct dwarf2_cu *cu);
91c24f0a 897
bc30ff58
JB
898static void add_partial_subprogram (struct partial_die_info *pdi,
899 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 900 int need_pc, struct dwarf2_cu *cu);
bc30ff58 901
fe1b8b76 902static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
903 gdb_byte *buffer, gdb_byte *info_ptr,
904 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 905
a14ed312 906static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 907
a14ed312 908static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 909
e5fe5e75 910static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 911
f3dd6933 912static void dwarf2_free_abbrev_table (void *);
c906108c 913
6caca83c
CC
914static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
915
fe1b8b76 916static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 917 struct dwarf2_cu *);
72bf9492 918
57349743 919static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 920 struct dwarf2_cu *);
c906108c 921
93311388
DE
922static struct partial_die_info *load_partial_dies (bfd *,
923 gdb_byte *, gdb_byte *,
924 int, struct dwarf2_cu *);
72bf9492 925
fe1b8b76 926static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
927 struct abbrev_info *abbrev,
928 unsigned int, bfd *,
929 gdb_byte *, gdb_byte *,
930 struct dwarf2_cu *);
c906108c 931
c764a876 932static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 933 struct dwarf2_cu *);
72bf9492
DJ
934
935static void fixup_partial_die (struct partial_die_info *,
936 struct dwarf2_cu *);
937
fe1b8b76
JB
938static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
939 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 940
fe1b8b76
JB
941static gdb_byte *read_attribute_value (struct attribute *, unsigned,
942 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 943
fe1b8b76 944static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 945
fe1b8b76 946static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 951
93311388 952static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 953
fe1b8b76 954static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 955 unsigned int *);
c906108c 956
c764a876
DE
957static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
958
959static LONGEST read_checked_initial_length_and_offset
960 (bfd *, gdb_byte *, const struct comp_unit_head *,
961 unsigned int *, unsigned int *);
613e1657 962
fe1b8b76 963static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
964 unsigned int *);
965
966static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 967
fe1b8b76 968static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 969
9b1c24c8 970static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 971
fe1b8b76
JB
972static char *read_indirect_string (bfd *, gdb_byte *,
973 const struct comp_unit_head *,
974 unsigned int *);
4bdf3d34 975
fe1b8b76 976static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 977
fe1b8b76 978static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 981
e142c38c 982static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 983
e142c38c
DJ
984static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
985 struct dwarf2_cu *);
c906108c 986
348e048f
DE
987static struct attribute *dwarf2_attr_no_follow (struct die_info *,
988 unsigned int,
989 struct dwarf2_cu *);
990
05cf31d1
JB
991static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
992 struct dwarf2_cu *cu);
993
e142c38c 994static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 995
e142c38c 996static struct die_info *die_specification (struct die_info *die,
f2f0e013 997 struct dwarf2_cu **);
63d06c5c 998
debd256d
JB
999static void free_line_header (struct line_header *lh);
1000
aaa75496
JB
1001static void add_file_name (struct line_header *, char *, unsigned int,
1002 unsigned int, unsigned int);
1003
debd256d
JB
1004static struct line_header *(dwarf_decode_line_header
1005 (unsigned int offset,
e7c27a73 1006 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1007
72b9f47f 1008static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1009 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1010
72b9f47f 1011static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1012
a14ed312 1013static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1014 struct dwarf2_cu *);
c906108c 1015
34eaf542
TT
1016static struct symbol *new_symbol_full (struct die_info *, struct type *,
1017 struct dwarf2_cu *, struct symbol *);
1018
a14ed312 1019static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1020 struct dwarf2_cu *);
c906108c 1021
98bfdba5
PA
1022static void dwarf2_const_value_attr (struct attribute *attr,
1023 struct type *type,
1024 const char *name,
1025 struct obstack *obstack,
1026 struct dwarf2_cu *cu, long *value,
1027 gdb_byte **bytes,
1028 struct dwarf2_locexpr_baton **baton);
2df3850c 1029
e7c27a73 1030static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1031
b4ba55a1
JB
1032static int need_gnat_info (struct dwarf2_cu *);
1033
3e43a32a
MS
1034static struct type *die_descriptive_type (struct die_info *,
1035 struct dwarf2_cu *);
b4ba55a1
JB
1036
1037static void set_descriptive_type (struct type *, struct die_info *,
1038 struct dwarf2_cu *);
1039
e7c27a73
DJ
1040static struct type *die_containing_type (struct die_info *,
1041 struct dwarf2_cu *);
c906108c 1042
673bfd45
DE
1043static struct type *lookup_die_type (struct die_info *, struct attribute *,
1044 struct dwarf2_cu *);
c906108c 1045
f792889a 1046static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1047
673bfd45
DE
1048static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1049
086ed43d 1050static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1051
6e70227d 1052static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1053 const char *suffix, int physname,
1054 struct dwarf2_cu *cu);
63d06c5c 1055
e7c27a73 1056static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1057
348e048f
DE
1058static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1059
e7c27a73 1060static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1061
e7c27a73 1062static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1063
96408a79
SA
1064static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1065
ff013f42
JK
1066static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *, struct partial_symtab *);
1068
a14ed312 1069static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1070 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1071 struct partial_symtab *);
c906108c 1072
fae299cd
DC
1073static void get_scope_pc_bounds (struct die_info *,
1074 CORE_ADDR *, CORE_ADDR *,
1075 struct dwarf2_cu *);
1076
801e3a5b
JB
1077static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1078 CORE_ADDR, struct dwarf2_cu *);
1079
a14ed312 1080static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1081 struct dwarf2_cu *);
c906108c 1082
a14ed312 1083static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1084 struct type *, struct dwarf2_cu *);
c906108c 1085
a14ed312 1086static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1087 struct die_info *, struct type *,
e7c27a73 1088 struct dwarf2_cu *);
c906108c 1089
a14ed312 1090static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1091 struct type *,
1092 struct dwarf2_cu *);
c906108c 1093
134d01f1 1094static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1097
e7c27a73 1098static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1099
5d7cb8df
JK
1100static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1101
27aa8d6a
SW
1102static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1103
f55ee35c
JK
1104static struct type *read_module_type (struct die_info *die,
1105 struct dwarf2_cu *cu);
1106
38d518c9 1107static const char *namespace_name (struct die_info *die,
e142c38c 1108 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1109
134d01f1 1110static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1111
e7c27a73 1112static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1113
6e70227d 1114static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1115 struct dwarf2_cu *);
1116
93311388 1117static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1118
93311388
DE
1119static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1120 gdb_byte *info_ptr,
d97bc12b
DE
1121 gdb_byte **new_info_ptr,
1122 struct die_info *parent);
1123
93311388
DE
1124static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
fe1b8b76 1126 gdb_byte **new_info_ptr,
639d11d3
DC
1127 struct die_info *parent);
1128
93311388
DE
1129static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1130 gdb_byte *info_ptr,
fe1b8b76 1131 gdb_byte **new_info_ptr,
639d11d3
DC
1132 struct die_info *parent);
1133
93311388
DE
1134static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1135 struct die_info **, gdb_byte *,
1136 int *);
1137
e7c27a73 1138static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1139
71c25dea
TT
1140static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1141 struct obstack *);
1142
e142c38c 1143static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1144
98bfdba5
PA
1145static const char *dwarf2_full_name (char *name,
1146 struct die_info *die,
1147 struct dwarf2_cu *cu);
1148
e142c38c 1149static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1150 struct dwarf2_cu **);
9219021c 1151
a14ed312 1152static char *dwarf_tag_name (unsigned int);
c906108c 1153
a14ed312 1154static char *dwarf_attr_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_form_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_bool_name (unsigned int);
c906108c 1159
a14ed312 1160static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1161
1162#if 0
a14ed312 1163static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1164#endif
1165
f9aca02d 1166static struct die_info *sibling_die (struct die_info *);
c906108c 1167
d97bc12b
DE
1168static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1169
1170static void dump_die_for_error (struct die_info *);
1171
1172static void dump_die_1 (struct ui_file *, int level, int max_level,
1173 struct die_info *);
c906108c 1174
d97bc12b 1175/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1176
51545339 1177static void store_in_ref_table (struct die_info *,
10b3939b 1178 struct dwarf2_cu *);
c906108c 1179
93311388
DE
1180static int is_ref_attr (struct attribute *);
1181
c764a876 1182static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1183
43bbcdc2 1184static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1185
348e048f
DE
1186static struct die_info *follow_die_ref_or_sig (struct die_info *,
1187 struct attribute *,
1188 struct dwarf2_cu **);
1189
10b3939b
DJ
1190static struct die_info *follow_die_ref (struct die_info *,
1191 struct attribute *,
f2f0e013 1192 struct dwarf2_cu **);
c906108c 1193
348e048f
DE
1194static struct die_info *follow_die_sig (struct die_info *,
1195 struct attribute *,
1196 struct dwarf2_cu **);
1197
6c83ed52
TT
1198static struct signatured_type *lookup_signatured_type_at_offset
1199 (struct objfile *objfile,
1200 struct dwarf2_section_info *section,
1201 unsigned int offset);
1202
e5fe5e75 1203static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1204
a0f42c21 1205static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1206
c906108c
SS
1207/* memory allocation interface */
1208
7b5a2f43 1209static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1210
f3dd6933 1211static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1212
b60c80d6 1213static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1214
2e276125 1215static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1216 char *, bfd *, struct dwarf2_cu *,
1217 struct dwarf2_section_info *,
1218 int);
2e276125 1219
8e19ed76
PS
1220static int attr_form_is_block (struct attribute *);
1221
3690dd37
JB
1222static int attr_form_is_section_offset (struct attribute *);
1223
1224static int attr_form_is_constant (struct attribute *);
1225
8cf6f0b1
TT
1226static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1227 struct dwarf2_loclist_baton *baton,
1228 struct attribute *attr);
1229
93e7bd98
DJ
1230static void dwarf2_symbol_mark_computed (struct attribute *attr,
1231 struct symbol *sym,
1232 struct dwarf2_cu *cu);
4c2df51b 1233
93311388
DE
1234static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1235 struct abbrev_info *abbrev,
1236 struct dwarf2_cu *cu);
4bb7a0a7 1237
72bf9492
DJ
1238static void free_stack_comp_unit (void *);
1239
72bf9492
DJ
1240static hashval_t partial_die_hash (const void *item);
1241
1242static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1243
ae038cb0 1244static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1245 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1246
1247static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1248 (unsigned int offset, struct objfile *objfile);
ae038cb0 1249
9816fde3 1250static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1251 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1252
1253static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1254 struct die_info *comp_unit_die);
93311388 1255
68dc6402 1256static void free_heap_comp_unit (void *);
ae038cb0
DJ
1257
1258static void free_cached_comp_units (void *);
1259
1260static void age_cached_comp_units (void);
1261
1262static void free_one_cached_comp_unit (void *);
1263
f792889a
DJ
1264static struct type *set_die_type (struct die_info *, struct type *,
1265 struct dwarf2_cu *);
1c379e20 1266
ae038cb0
DJ
1267static void create_all_comp_units (struct objfile *);
1268
1fd400ff
TT
1269static int create_debug_types_hash_table (struct objfile *objfile);
1270
a0f42c21 1271static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1272
1273static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275static void dwarf2_add_dependence (struct dwarf2_cu *,
1276 struct dwarf2_per_cu_data *);
1277
ae038cb0
DJ
1278static void dwarf2_mark (struct dwarf2_cu *);
1279
1280static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
673bfd45
DE
1282static struct type *get_die_type_at_offset (unsigned int,
1283 struct dwarf2_per_cu_data *per_cu);
1284
f792889a 1285static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1286
9291a0cd
TT
1287static void dwarf2_release_queue (void *dummy);
1288
a0f42c21 1289static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1290
a0f42c21 1291static void process_queue (void);
9291a0cd
TT
1292
1293static void find_file_and_directory (struct die_info *die,
1294 struct dwarf2_cu *cu,
1295 char **name, char **comp_dir);
1296
1297static char *file_full_name (int file, struct line_header *lh,
1298 const char *comp_dir);
1299
1300static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1301 gdb_byte *info_ptr,
1302 gdb_byte *buffer,
1303 unsigned int buffer_size,
460c1c54 1304 bfd *abfd,
b0df02fd 1305 int is_debug_types_section);
9291a0cd
TT
1306
1307static void init_cu_die_reader (struct die_reader_specs *reader,
1308 struct dwarf2_cu *cu);
1309
673bfd45 1310static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1311
9291a0cd
TT
1312#if WORDS_BIGENDIAN
1313
1314/* Convert VALUE between big- and little-endian. */
1315static offset_type
1316byte_swap (offset_type value)
1317{
1318 offset_type result;
1319
1320 result = (value & 0xff) << 24;
1321 result |= (value & 0xff00) << 8;
1322 result |= (value & 0xff0000) >> 8;
1323 result |= (value & 0xff000000) >> 24;
1324 return result;
1325}
1326
1327#define MAYBE_SWAP(V) byte_swap (V)
1328
1329#else
1330#define MAYBE_SWAP(V) (V)
1331#endif /* WORDS_BIGENDIAN */
1332
1333/* The suffix for an index file. */
1334#define INDEX_SUFFIX ".gdb-index"
1335
3da10d80
KS
1336static const char *dwarf2_physname (char *name, struct die_info *die,
1337 struct dwarf2_cu *cu);
1338
c906108c 1339/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1340 information and return true if we have enough to do something.
1341 NAMES points to the dwarf2 section names, or is NULL if the standard
1342 ELF names are used. */
c906108c
SS
1343
1344int
251d32d9
TG
1345dwarf2_has_info (struct objfile *objfile,
1346 const struct dwarf2_debug_sections *names)
c906108c 1347{
be391dca
TT
1348 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1349 if (!dwarf2_per_objfile)
1350 {
1351 /* Initialize per-objfile state. */
1352 struct dwarf2_per_objfile *data
1353 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1354
be391dca
TT
1355 memset (data, 0, sizeof (*data));
1356 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1357 dwarf2_per_objfile = data;
6502dd73 1358
251d32d9
TG
1359 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1360 (void *) names);
be391dca
TT
1361 dwarf2_per_objfile->objfile = objfile;
1362 }
1363 return (dwarf2_per_objfile->info.asection != NULL
1364 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1365}
1366
251d32d9
TG
1367/* When loading sections, we look either for uncompressed section or for
1368 compressed section names. */
233a11ab
CS
1369
1370static int
251d32d9
TG
1371section_is_p (const char *section_name,
1372 const struct dwarf2_section_names *names)
233a11ab 1373{
251d32d9
TG
1374 if (names->normal != NULL
1375 && strcmp (section_name, names->normal) == 0)
1376 return 1;
1377 if (names->compressed != NULL
1378 && strcmp (section_name, names->compressed) == 0)
1379 return 1;
1380 return 0;
233a11ab
CS
1381}
1382
c906108c
SS
1383/* This function is mapped across the sections and remembers the
1384 offset and size of each of the debugging sections we are interested
1385 in. */
1386
1387static void
251d32d9 1388dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1389{
251d32d9
TG
1390 const struct dwarf2_debug_sections *names;
1391
1392 if (vnames == NULL)
1393 names = &dwarf2_elf_names;
1394 else
1395 names = (const struct dwarf2_debug_sections *) vnames;
1396
1397 if (section_is_p (sectp->name, &names->info))
c906108c 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->info.asection = sectp;
1400 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1401 }
251d32d9 1402 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1403 {
dce234bc
PP
1404 dwarf2_per_objfile->abbrev.asection = sectp;
1405 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1406 }
251d32d9 1407 else if (section_is_p (sectp->name, &names->line))
c906108c 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->line.asection = sectp;
1410 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1411 }
251d32d9 1412 else if (section_is_p (sectp->name, &names->loc))
c906108c 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->loc.asection = sectp;
1415 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1416 }
251d32d9 1417 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1418 {
dce234bc
PP
1419 dwarf2_per_objfile->macinfo.asection = sectp;
1420 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1421 }
cf2c3c16
TT
1422 else if (section_is_p (sectp->name, &names->macro))
1423 {
1424 dwarf2_per_objfile->macro.asection = sectp;
1425 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1426 }
251d32d9 1427 else if (section_is_p (sectp->name, &names->str))
c906108c 1428 {
dce234bc
PP
1429 dwarf2_per_objfile->str.asection = sectp;
1430 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1431 }
251d32d9 1432 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1433 {
dce234bc
PP
1434 dwarf2_per_objfile->frame.asection = sectp;
1435 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1436 }
251d32d9 1437 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1438 {
3799ccc6 1439 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1440
3799ccc6
EZ
1441 if (aflag & SEC_HAS_CONTENTS)
1442 {
dce234bc
PP
1443 dwarf2_per_objfile->eh_frame.asection = sectp;
1444 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1445 }
b6af0555 1446 }
251d32d9 1447 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1448 {
dce234bc
PP
1449 dwarf2_per_objfile->ranges.asection = sectp;
1450 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1451 }
251d32d9 1452 else if (section_is_p (sectp->name, &names->types))
348e048f 1453 {
8b70b953
TT
1454 struct dwarf2_section_info type_section;
1455
1456 memset (&type_section, 0, sizeof (type_section));
1457 type_section.asection = sectp;
1458 type_section.size = bfd_get_section_size (sectp);
1459
1460 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1461 &type_section);
348e048f 1462 }
251d32d9 1463 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1464 {
1465 dwarf2_per_objfile->gdb_index.asection = sectp;
1466 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1467 }
dce234bc 1468
72dca2f5
FR
1469 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1470 && bfd_section_vma (abfd, sectp) == 0)
1471 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1472}
1473
dce234bc
PP
1474/* Decompress a section that was compressed using zlib. Store the
1475 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1476
1477static void
dce234bc
PP
1478zlib_decompress_section (struct objfile *objfile, asection *sectp,
1479 gdb_byte **outbuf, bfd_size_type *outsize)
1480{
1481 bfd *abfd = objfile->obfd;
1482#ifndef HAVE_ZLIB_H
1483 error (_("Support for zlib-compressed DWARF data (from '%s') "
1484 "is disabled in this copy of GDB"),
1485 bfd_get_filename (abfd));
1486#else
1487 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1488 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1489 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1490 bfd_size_type uncompressed_size;
1491 gdb_byte *uncompressed_buffer;
1492 z_stream strm;
1493 int rc;
1494 int header_size = 12;
1495
1496 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1497 || bfd_bread (compressed_buffer,
1498 compressed_size, abfd) != compressed_size)
dce234bc
PP
1499 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1500 bfd_get_filename (abfd));
1501
1502 /* Read the zlib header. In this case, it should be "ZLIB" followed
1503 by the uncompressed section size, 8 bytes in big-endian order. */
1504 if (compressed_size < header_size
1505 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1506 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1507 bfd_get_filename (abfd));
1508 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1509 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1510 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1511 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1512 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1513 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1514 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1515 uncompressed_size += compressed_buffer[11];
1516
1517 /* It is possible the section consists of several compressed
1518 buffers concatenated together, so we uncompress in a loop. */
1519 strm.zalloc = NULL;
1520 strm.zfree = NULL;
1521 strm.opaque = NULL;
1522 strm.avail_in = compressed_size - header_size;
1523 strm.next_in = (Bytef*) compressed_buffer + header_size;
1524 strm.avail_out = uncompressed_size;
1525 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1526 uncompressed_size);
1527 rc = inflateInit (&strm);
1528 while (strm.avail_in > 0)
1529 {
1530 if (rc != Z_OK)
1531 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1532 bfd_get_filename (abfd), rc);
1533 strm.next_out = ((Bytef*) uncompressed_buffer
1534 + (uncompressed_size - strm.avail_out));
1535 rc = inflate (&strm, Z_FINISH);
1536 if (rc != Z_STREAM_END)
1537 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1538 bfd_get_filename (abfd), rc);
1539 rc = inflateReset (&strm);
1540 }
1541 rc = inflateEnd (&strm);
1542 if (rc != Z_OK
1543 || strm.avail_out != 0)
1544 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1545 bfd_get_filename (abfd), rc);
1546
affddf13 1547 do_cleanups (cleanup);
dce234bc
PP
1548 *outbuf = uncompressed_buffer;
1549 *outsize = uncompressed_size;
1550#endif
233a11ab
CS
1551}
1552
9e0ac564
TT
1553/* A helper function that decides whether a section is empty. */
1554
1555static int
1556dwarf2_section_empty_p (struct dwarf2_section_info *info)
1557{
1558 return info->asection == NULL || info->size == 0;
1559}
1560
9cdd5dbd 1561/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1562 OBJFILE, store info about the section into INFO.
1563 If the section is compressed, uncompress it before returning. */
c906108c 1564
dce234bc
PP
1565static void
1566dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1567{
dce234bc
PP
1568 bfd *abfd = objfile->obfd;
1569 asection *sectp = info->asection;
1570 gdb_byte *buf, *retbuf;
1571 unsigned char header[4];
c906108c 1572
be391dca
TT
1573 if (info->readin)
1574 return;
dce234bc 1575 info->buffer = NULL;
b315ab21 1576 info->map_addr = NULL;
be391dca 1577 info->readin = 1;
188dd5d6 1578
9e0ac564 1579 if (dwarf2_section_empty_p (info))
dce234bc 1580 return;
c906108c 1581
dce234bc
PP
1582 /* Check if the file has a 4-byte header indicating compression. */
1583 if (info->size > sizeof (header)
1584 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1585 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1586 {
1587 /* Upon decompression, update the buffer and its size. */
1588 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1589 {
1590 zlib_decompress_section (objfile, sectp, &info->buffer,
1591 &info->size);
1592 return;
1593 }
1594 }
4bdf3d34 1595
dce234bc
PP
1596#ifdef HAVE_MMAP
1597 if (pagesize == 0)
1598 pagesize = getpagesize ();
2e276125 1599
dce234bc
PP
1600 /* Only try to mmap sections which are large enough: we don't want to
1601 waste space due to fragmentation. Also, only try mmap for sections
1602 without relocations. */
1603
1604 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1605 {
b315ab21
TG
1606 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1607 MAP_PRIVATE, sectp->filepos,
1608 &info->map_addr, &info->map_len);
dce234bc 1609
b315ab21 1610 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1611 {
be391dca 1612#if HAVE_POSIX_MADVISE
b315ab21 1613 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1614#endif
dce234bc
PP
1615 return;
1616 }
1617 }
1618#endif
1619
1620 /* If we get here, we are a normal, not-compressed section. */
1621 info->buffer = buf
1622 = obstack_alloc (&objfile->objfile_obstack, info->size);
1623
1624 /* When debugging .o files, we may need to apply relocations; see
1625 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1626 We never compress sections in .o files, so we only need to
1627 try this when the section is not compressed. */
ac8035ab 1628 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1629 if (retbuf != NULL)
1630 {
1631 info->buffer = retbuf;
1632 return;
1633 }
1634
1635 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1636 || bfd_bread (buf, info->size, abfd) != info->size)
1637 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1638 bfd_get_filename (abfd));
1639}
1640
9e0ac564
TT
1641/* A helper function that returns the size of a section in a safe way.
1642 If you are positive that the section has been read before using the
1643 size, then it is safe to refer to the dwarf2_section_info object's
1644 "size" field directly. In other cases, you must call this
1645 function, because for compressed sections the size field is not set
1646 correctly until the section has been read. */
1647
1648static bfd_size_type
1649dwarf2_section_size (struct objfile *objfile,
1650 struct dwarf2_section_info *info)
1651{
1652 if (!info->readin)
1653 dwarf2_read_section (objfile, info);
1654 return info->size;
1655}
1656
dce234bc 1657/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1658 SECTION_NAME. */
af34e669 1659
dce234bc 1660void
3017a003
TG
1661dwarf2_get_section_info (struct objfile *objfile,
1662 enum dwarf2_section_enum sect,
dce234bc
PP
1663 asection **sectp, gdb_byte **bufp,
1664 bfd_size_type *sizep)
1665{
1666 struct dwarf2_per_objfile *data
1667 = objfile_data (objfile, dwarf2_objfile_data_key);
1668 struct dwarf2_section_info *info;
a3b2a86b
TT
1669
1670 /* We may see an objfile without any DWARF, in which case we just
1671 return nothing. */
1672 if (data == NULL)
1673 {
1674 *sectp = NULL;
1675 *bufp = NULL;
1676 *sizep = 0;
1677 return;
1678 }
3017a003
TG
1679 switch (sect)
1680 {
1681 case DWARF2_DEBUG_FRAME:
1682 info = &data->frame;
1683 break;
1684 case DWARF2_EH_FRAME:
1685 info = &data->eh_frame;
1686 break;
1687 default:
1688 gdb_assert_not_reached ("unexpected section");
1689 }
dce234bc 1690
9e0ac564 1691 dwarf2_read_section (objfile, info);
dce234bc
PP
1692
1693 *sectp = info->asection;
1694 *bufp = info->buffer;
1695 *sizep = info->size;
1696}
1697
9291a0cd 1698\f
7b9f3c50
DE
1699/* DWARF quick_symbols_functions support. */
1700
1701/* TUs can share .debug_line entries, and there can be a lot more TUs than
1702 unique line tables, so we maintain a separate table of all .debug_line
1703 derived entries to support the sharing.
1704 All the quick functions need is the list of file names. We discard the
1705 line_header when we're done and don't need to record it here. */
1706struct quick_file_names
1707{
1708 /* The offset in .debug_line of the line table. We hash on this. */
1709 unsigned int offset;
1710
1711 /* The number of entries in file_names, real_names. */
1712 unsigned int num_file_names;
1713
1714 /* The file names from the line table, after being run through
1715 file_full_name. */
1716 const char **file_names;
1717
1718 /* The file names from the line table after being run through
1719 gdb_realpath. These are computed lazily. */
1720 const char **real_names;
1721};
1722
1723/* When using the index (and thus not using psymtabs), each CU has an
1724 object of this type. This is used to hold information needed by
1725 the various "quick" methods. */
1726struct dwarf2_per_cu_quick_data
1727{
1728 /* The file table. This can be NULL if there was no file table
1729 or it's currently not read in.
1730 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1731 struct quick_file_names *file_names;
1732
1733 /* The corresponding symbol table. This is NULL if symbols for this
1734 CU have not yet been read. */
1735 struct symtab *symtab;
1736
1737 /* A temporary mark bit used when iterating over all CUs in
1738 expand_symtabs_matching. */
1739 unsigned int mark : 1;
1740
1741 /* True if we've tried to read the file table and found there isn't one.
1742 There will be no point in trying to read it again next time. */
1743 unsigned int no_file_data : 1;
1744};
1745
1746/* Hash function for a quick_file_names. */
1747
1748static hashval_t
1749hash_file_name_entry (const void *e)
1750{
1751 const struct quick_file_names *file_data = e;
1752
1753 return file_data->offset;
1754}
1755
1756/* Equality function for a quick_file_names. */
1757
1758static int
1759eq_file_name_entry (const void *a, const void *b)
1760{
1761 const struct quick_file_names *ea = a;
1762 const struct quick_file_names *eb = b;
1763
1764 return ea->offset == eb->offset;
1765}
1766
1767/* Delete function for a quick_file_names. */
1768
1769static void
1770delete_file_name_entry (void *e)
1771{
1772 struct quick_file_names *file_data = e;
1773 int i;
1774
1775 for (i = 0; i < file_data->num_file_names; ++i)
1776 {
1777 xfree ((void*) file_data->file_names[i]);
1778 if (file_data->real_names)
1779 xfree ((void*) file_data->real_names[i]);
1780 }
1781
1782 /* The space for the struct itself lives on objfile_obstack,
1783 so we don't free it here. */
1784}
1785
1786/* Create a quick_file_names hash table. */
1787
1788static htab_t
1789create_quick_file_names_table (unsigned int nr_initial_entries)
1790{
1791 return htab_create_alloc (nr_initial_entries,
1792 hash_file_name_entry, eq_file_name_entry,
1793 delete_file_name_entry, xcalloc, xfree);
1794}
9291a0cd 1795
918dd910
JK
1796/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1797 have to be created afterwards. You should call age_cached_comp_units after
1798 processing PER_CU->CU. dw2_setup must have been already called. */
1799
1800static void
1801load_cu (struct dwarf2_per_cu_data *per_cu)
1802{
b0df02fd 1803 if (per_cu->debug_types_section)
e5fe5e75 1804 load_full_type_unit (per_cu);
918dd910 1805 else
a0f42c21 1806 load_full_comp_unit (per_cu);
918dd910 1807
918dd910 1808 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1809
1810 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1811}
1812
a0f42c21 1813/* Read in the symbols for PER_CU. */
2fdf6df6 1814
9291a0cd 1815static void
a0f42c21 1816dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1817{
1818 struct cleanup *back_to;
1819
1820 back_to = make_cleanup (dwarf2_release_queue, NULL);
1821
a0f42c21 1822 queue_comp_unit (per_cu);
9291a0cd 1823
918dd910 1824 load_cu (per_cu);
9291a0cd 1825
a0f42c21 1826 process_queue ();
9291a0cd
TT
1827
1828 /* Age the cache, releasing compilation units that have not
1829 been used recently. */
1830 age_cached_comp_units ();
1831
1832 do_cleanups (back_to);
1833}
1834
1835/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1836 the objfile from which this CU came. Returns the resulting symbol
1837 table. */
2fdf6df6 1838
9291a0cd 1839static struct symtab *
a0f42c21 1840dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1841{
1842 if (!per_cu->v.quick->symtab)
1843 {
1844 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1845 increment_reading_symtab ();
a0f42c21 1846 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1847 do_cleanups (back_to);
1848 }
1849 return per_cu->v.quick->symtab;
1850}
1851
1fd400ff 1852/* Return the CU given its index. */
2fdf6df6 1853
1fd400ff
TT
1854static struct dwarf2_per_cu_data *
1855dw2_get_cu (int index)
1856{
1857 if (index >= dwarf2_per_objfile->n_comp_units)
1858 {
1859 index -= dwarf2_per_objfile->n_comp_units;
1860 return dwarf2_per_objfile->type_comp_units[index];
1861 }
1862 return dwarf2_per_objfile->all_comp_units[index];
1863}
1864
9291a0cd
TT
1865/* A helper function that knows how to read a 64-bit value in a way
1866 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1867 otherwise. */
2fdf6df6 1868
9291a0cd
TT
1869static int
1870extract_cu_value (const char *bytes, ULONGEST *result)
1871{
1872 if (sizeof (ULONGEST) < 8)
1873 {
1874 int i;
1875
1876 /* Ignore the upper 4 bytes if they are all zero. */
1877 for (i = 0; i < 4; ++i)
1878 if (bytes[i + 4] != 0)
1879 return 0;
1880
1881 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1882 }
1883 else
1884 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1885 return 1;
1886}
1887
1888/* Read the CU list from the mapped index, and use it to create all
1889 the CU objects for this objfile. Return 0 if something went wrong,
1890 1 if everything went ok. */
2fdf6df6 1891
9291a0cd 1892static int
1fd400ff
TT
1893create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1894 offset_type cu_list_elements)
9291a0cd
TT
1895{
1896 offset_type i;
9291a0cd
TT
1897
1898 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1899 dwarf2_per_objfile->all_comp_units
1900 = obstack_alloc (&objfile->objfile_obstack,
1901 dwarf2_per_objfile->n_comp_units
1902 * sizeof (struct dwarf2_per_cu_data *));
1903
1904 for (i = 0; i < cu_list_elements; i += 2)
1905 {
1906 struct dwarf2_per_cu_data *the_cu;
1907 ULONGEST offset, length;
1908
1909 if (!extract_cu_value (cu_list, &offset)
1910 || !extract_cu_value (cu_list + 8, &length))
1911 return 0;
1912 cu_list += 2 * 8;
1913
1914 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1915 struct dwarf2_per_cu_data);
1916 the_cu->offset = offset;
1917 the_cu->length = length;
1918 the_cu->objfile = objfile;
1919 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1920 struct dwarf2_per_cu_quick_data);
1921 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1922 }
1923
1924 return 1;
1925}
1926
1fd400ff 1927/* Create the signatured type hash table from the index. */
673bfd45 1928
1fd400ff 1929static int
673bfd45 1930create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1931 struct dwarf2_section_info *section,
673bfd45
DE
1932 const gdb_byte *bytes,
1933 offset_type elements)
1fd400ff
TT
1934{
1935 offset_type i;
673bfd45 1936 htab_t sig_types_hash;
1fd400ff
TT
1937
1938 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1939 dwarf2_per_objfile->type_comp_units
1940 = obstack_alloc (&objfile->objfile_obstack,
1941 dwarf2_per_objfile->n_type_comp_units
1942 * sizeof (struct dwarf2_per_cu_data *));
1943
673bfd45 1944 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1945
1946 for (i = 0; i < elements; i += 3)
1947 {
1948 struct signatured_type *type_sig;
1949 ULONGEST offset, type_offset, signature;
1950 void **slot;
1951
1952 if (!extract_cu_value (bytes, &offset)
1953 || !extract_cu_value (bytes + 8, &type_offset))
1954 return 0;
1955 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1956 bytes += 3 * 8;
1957
1958 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1959 struct signatured_type);
1960 type_sig->signature = signature;
1fd400ff 1961 type_sig->type_offset = type_offset;
b0df02fd 1962 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1963 type_sig->per_cu.offset = offset;
1964 type_sig->per_cu.objfile = objfile;
1965 type_sig->per_cu.v.quick
1966 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1967 struct dwarf2_per_cu_quick_data);
1968
673bfd45 1969 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1970 *slot = type_sig;
1971
1972 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1973 }
1974
673bfd45 1975 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1976
1977 return 1;
1978}
1979
9291a0cd
TT
1980/* Read the address map data from the mapped index, and use it to
1981 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1982
9291a0cd
TT
1983static void
1984create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1985{
1986 const gdb_byte *iter, *end;
1987 struct obstack temp_obstack;
1988 struct addrmap *mutable_map;
1989 struct cleanup *cleanup;
1990 CORE_ADDR baseaddr;
1991
1992 obstack_init (&temp_obstack);
1993 cleanup = make_cleanup_obstack_free (&temp_obstack);
1994 mutable_map = addrmap_create_mutable (&temp_obstack);
1995
1996 iter = index->address_table;
1997 end = iter + index->address_table_size;
1998
1999 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2000
2001 while (iter < end)
2002 {
2003 ULONGEST hi, lo, cu_index;
2004 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2005 iter += 8;
2006 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2007 iter += 8;
2008 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2009 iter += 4;
2010
2011 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2012 dw2_get_cu (cu_index));
9291a0cd
TT
2013 }
2014
2015 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2016 &objfile->objfile_obstack);
2017 do_cleanups (cleanup);
2018}
2019
59d7bcaf
JK
2020/* The hash function for strings in the mapped index. This is the same as
2021 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2022 implementation. This is necessary because the hash function is tied to the
2023 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2024 SYMBOL_HASH_NEXT.
2025
2026 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2027
9291a0cd 2028static hashval_t
559a7a62 2029mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2030{
2031 const unsigned char *str = (const unsigned char *) p;
2032 hashval_t r = 0;
2033 unsigned char c;
2034
2035 while ((c = *str++) != 0)
559a7a62
JK
2036 {
2037 if (index_version >= 5)
2038 c = tolower (c);
2039 r = r * 67 + c - 113;
2040 }
9291a0cd
TT
2041
2042 return r;
2043}
2044
2045/* Find a slot in the mapped index INDEX for the object named NAME.
2046 If NAME is found, set *VEC_OUT to point to the CU vector in the
2047 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2048
9291a0cd
TT
2049static int
2050find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2051 offset_type **vec_out)
2052{
0cf03b49
JK
2053 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2054 offset_type hash;
9291a0cd 2055 offset_type slot, step;
559a7a62 2056 int (*cmp) (const char *, const char *);
9291a0cd 2057
0cf03b49
JK
2058 if (current_language->la_language == language_cplus
2059 || current_language->la_language == language_java
2060 || current_language->la_language == language_fortran)
2061 {
2062 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2063 not contain any. */
2064 const char *paren = strchr (name, '(');
2065
2066 if (paren)
2067 {
2068 char *dup;
2069
2070 dup = xmalloc (paren - name + 1);
2071 memcpy (dup, name, paren - name);
2072 dup[paren - name] = 0;
2073
2074 make_cleanup (xfree, dup);
2075 name = dup;
2076 }
2077 }
2078
559a7a62
JK
2079 /* Index version 4 did not support case insensitive searches. But the
2080 indexes for case insensitive languages are built in lowercase, therefore
2081 simulate our NAME being searched is also lowercased. */
2082 hash = mapped_index_string_hash ((index->version == 4
2083 && case_sensitivity == case_sensitive_off
2084 ? 5 : index->version),
2085 name);
2086
3876f04e
DE
2087 slot = hash & (index->symbol_table_slots - 1);
2088 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2089 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2090
2091 for (;;)
2092 {
2093 /* Convert a slot number to an offset into the table. */
2094 offset_type i = 2 * slot;
2095 const char *str;
3876f04e 2096 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2097 {
2098 do_cleanups (back_to);
2099 return 0;
2100 }
9291a0cd 2101
3876f04e 2102 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2103 if (!cmp (name, str))
9291a0cd
TT
2104 {
2105 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2106 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2107 do_cleanups (back_to);
9291a0cd
TT
2108 return 1;
2109 }
2110
3876f04e 2111 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2112 }
2113}
2114
2115/* Read the index file. If everything went ok, initialize the "quick"
2116 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2117
9291a0cd
TT
2118static int
2119dwarf2_read_index (struct objfile *objfile)
2120{
9291a0cd
TT
2121 char *addr;
2122 struct mapped_index *map;
b3b272e1 2123 offset_type *metadata;
ac0b195c
KW
2124 const gdb_byte *cu_list;
2125 const gdb_byte *types_list = NULL;
2126 offset_type version, cu_list_elements;
2127 offset_type types_list_elements = 0;
1fd400ff 2128 int i;
9291a0cd 2129
9e0ac564 2130 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2131 return 0;
82430852
JK
2132
2133 /* Older elfutils strip versions could keep the section in the main
2134 executable while splitting it for the separate debug info file. */
2135 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2136 & SEC_HAS_CONTENTS) == 0)
2137 return 0;
2138
9291a0cd
TT
2139 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2140
2141 addr = dwarf2_per_objfile->gdb_index.buffer;
2142 /* Version check. */
1fd400ff 2143 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2144 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2145 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2146 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2147 indices. Index version 4 uses a different hash function than index
2148 version 5 and later. */
831adc1f 2149 if (version < 4)
9291a0cd 2150 return 0;
594e8718
JK
2151 /* Indexes with higher version than the one supported by GDB may be no
2152 longer backward compatible. */
559a7a62 2153 if (version > 5)
594e8718 2154 return 0;
9291a0cd
TT
2155
2156 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2157 map->version = version;
b3b272e1 2158 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2159
2160 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2161
2162 i = 0;
2163 cu_list = addr + MAYBE_SWAP (metadata[i]);
2164 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2165 / 8);
1fd400ff
TT
2166 ++i;
2167
987d643c
TT
2168 types_list = addr + MAYBE_SWAP (metadata[i]);
2169 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2170 - MAYBE_SWAP (metadata[i]))
2171 / 8);
2172 ++i;
1fd400ff
TT
2173
2174 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2175 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2176 - MAYBE_SWAP (metadata[i]));
2177 ++i;
2178
3876f04e
DE
2179 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2180 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2181 - MAYBE_SWAP (metadata[i]))
2182 / (2 * sizeof (offset_type)));
1fd400ff 2183 ++i;
9291a0cd 2184
1fd400ff
TT
2185 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2186
2187 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2188 return 0;
2189
8b70b953
TT
2190 if (types_list_elements)
2191 {
2192 struct dwarf2_section_info *section;
2193
2194 /* We can only handle a single .debug_types when we have an
2195 index. */
2196 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2197 return 0;
2198
2199 section = VEC_index (dwarf2_section_info_def,
2200 dwarf2_per_objfile->types, 0);
2201
2202 if (!create_signatured_type_table_from_index (objfile, section,
2203 types_list,
2204 types_list_elements))
2205 return 0;
2206 }
9291a0cd
TT
2207
2208 create_addrmap_from_index (objfile, map);
2209
2210 dwarf2_per_objfile->index_table = map;
2211 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2212 dwarf2_per_objfile->quick_file_names_table =
2213 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2214
2215 return 1;
2216}
2217
2218/* A helper for the "quick" functions which sets the global
2219 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2220
9291a0cd
TT
2221static void
2222dw2_setup (struct objfile *objfile)
2223{
2224 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2225 gdb_assert (dwarf2_per_objfile);
2226}
2227
2228/* A helper for the "quick" functions which attempts to read the line
2229 table for THIS_CU. */
2fdf6df6 2230
7b9f3c50
DE
2231static struct quick_file_names *
2232dw2_get_file_names (struct objfile *objfile,
2233 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2234{
2235 bfd *abfd = objfile->obfd;
7b9f3c50 2236 struct line_header *lh;
9291a0cd
TT
2237 struct attribute *attr;
2238 struct cleanup *cleanups;
2239 struct die_info *comp_unit_die;
36374493 2240 struct dwarf2_section_info* sec;
adabb602 2241 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2242 int has_children, i;
2243 struct dwarf2_cu cu;
2244 unsigned int bytes_read, buffer_size;
2245 struct die_reader_specs reader_specs;
2246 char *name, *comp_dir;
7b9f3c50
DE
2247 void **slot;
2248 struct quick_file_names *qfn;
2249 unsigned int line_offset;
9291a0cd 2250
7b9f3c50
DE
2251 if (this_cu->v.quick->file_names != NULL)
2252 return this_cu->v.quick->file_names;
2253 /* If we know there is no line data, no point in looking again. */
2254 if (this_cu->v.quick->no_file_data)
2255 return NULL;
9291a0cd 2256
23745b47 2257 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2258 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2259
b0df02fd
DE
2260 if (this_cu->debug_types_section)
2261 sec = this_cu->debug_types_section;
36374493
DE
2262 else
2263 sec = &dwarf2_per_objfile->info;
2264 dwarf2_read_section (objfile, sec);
2265 buffer_size = sec->size;
2266 buffer = sec->buffer;
9291a0cd 2267 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2268
2269 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2270 buffer, buffer_size,
460c1c54 2271 abfd,
b0df02fd 2272 this_cu->debug_types_section != NULL);
9291a0cd 2273
6caca83c
CC
2274 /* Skip dummy compilation units. */
2275 if (info_ptr >= buffer + buffer_size
2276 || peek_abbrev_code (abfd, info_ptr) == 0)
2277 {
2278 do_cleanups (cleanups);
2279 return NULL;
2280 }
2281
e5fe5e75 2282 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2283 make_cleanup (dwarf2_free_abbrev_table, &cu);
2284
9291a0cd 2285 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2286 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2287 &has_children);
9291a0cd 2288
7b9f3c50
DE
2289 lh = NULL;
2290 slot = NULL;
2291 line_offset = 0;
9291a0cd
TT
2292 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2293 if (attr)
2294 {
7b9f3c50
DE
2295 struct quick_file_names find_entry;
2296
2297 line_offset = DW_UNSND (attr);
2298
2299 /* We may have already read in this line header (TU line header sharing).
2300 If we have we're done. */
2301 find_entry.offset = line_offset;
2302 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2303 &find_entry, INSERT);
2304 if (*slot != NULL)
2305 {
2306 do_cleanups (cleanups);
2307 this_cu->v.quick->file_names = *slot;
2308 return *slot;
2309 }
2310
9291a0cd
TT
2311 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2312 }
2313 if (lh == NULL)
2314 {
2315 do_cleanups (cleanups);
7b9f3c50
DE
2316 this_cu->v.quick->no_file_data = 1;
2317 return NULL;
9291a0cd
TT
2318 }
2319
7b9f3c50
DE
2320 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2321 qfn->offset = line_offset;
2322 gdb_assert (slot != NULL);
2323 *slot = qfn;
9291a0cd 2324
7b9f3c50 2325 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2326
7b9f3c50
DE
2327 qfn->num_file_names = lh->num_file_names;
2328 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2329 lh->num_file_names * sizeof (char *));
9291a0cd 2330 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2331 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2332 qfn->real_names = NULL;
9291a0cd 2333
7b9f3c50 2334 free_line_header (lh);
9291a0cd 2335 do_cleanups (cleanups);
7b9f3c50
DE
2336
2337 this_cu->v.quick->file_names = qfn;
2338 return qfn;
9291a0cd
TT
2339}
2340
2341/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2342 real path for a given file name from the line table. */
2fdf6df6 2343
9291a0cd 2344static const char *
7b9f3c50
DE
2345dw2_get_real_path (struct objfile *objfile,
2346 struct quick_file_names *qfn, int index)
9291a0cd 2347{
7b9f3c50
DE
2348 if (qfn->real_names == NULL)
2349 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2350 qfn->num_file_names, sizeof (char *));
9291a0cd 2351
7b9f3c50
DE
2352 if (qfn->real_names[index] == NULL)
2353 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2354
7b9f3c50 2355 return qfn->real_names[index];
9291a0cd
TT
2356}
2357
2358static struct symtab *
2359dw2_find_last_source_symtab (struct objfile *objfile)
2360{
2361 int index;
ae2de4f8 2362
9291a0cd
TT
2363 dw2_setup (objfile);
2364 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2365 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2366}
2367
7b9f3c50
DE
2368/* Traversal function for dw2_forget_cached_source_info. */
2369
2370static int
2371dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2372{
7b9f3c50 2373 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2374
7b9f3c50 2375 if (file_data->real_names)
9291a0cd 2376 {
7b9f3c50 2377 int i;
9291a0cd 2378
7b9f3c50 2379 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2380 {
7b9f3c50
DE
2381 xfree ((void*) file_data->real_names[i]);
2382 file_data->real_names[i] = NULL;
9291a0cd
TT
2383 }
2384 }
7b9f3c50
DE
2385
2386 return 1;
2387}
2388
2389static void
2390dw2_forget_cached_source_info (struct objfile *objfile)
2391{
2392 dw2_setup (objfile);
2393
2394 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2395 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2396}
2397
f8eba3c6
TT
2398/* Helper function for dw2_map_symtabs_matching_filename that expands
2399 the symtabs and calls the iterator. */
2400
2401static int
2402dw2_map_expand_apply (struct objfile *objfile,
2403 struct dwarf2_per_cu_data *per_cu,
2404 const char *name,
2405 const char *full_path, const char *real_path,
2406 int (*callback) (struct symtab *, void *),
2407 void *data)
2408{
2409 struct symtab *last_made = objfile->symtabs;
2410
2411 /* Don't visit already-expanded CUs. */
2412 if (per_cu->v.quick->symtab)
2413 return 0;
2414
2415 /* This may expand more than one symtab, and we want to iterate over
2416 all of them. */
a0f42c21 2417 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2418
2419 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2420 objfile->symtabs, last_made);
2421}
2422
2423/* Implementation of the map_symtabs_matching_filename method. */
2424
9291a0cd 2425static int
f8eba3c6
TT
2426dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2427 const char *full_path, const char *real_path,
2428 int (*callback) (struct symtab *, void *),
2429 void *data)
9291a0cd
TT
2430{
2431 int i;
c011a4f4
DE
2432 const char *name_basename = lbasename (name);
2433 int check_basename = name_basename == name;
9291a0cd
TT
2434 struct dwarf2_per_cu_data *base_cu = NULL;
2435
2436 dw2_setup (objfile);
ae2de4f8 2437
1fd400ff
TT
2438 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2439 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2440 {
2441 int j;
e254ef6a 2442 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2443 struct quick_file_names *file_data;
9291a0cd 2444
3d7bb9d9 2445 /* We only need to look at symtabs not already expanded. */
e254ef6a 2446 if (per_cu->v.quick->symtab)
9291a0cd
TT
2447 continue;
2448
7b9f3c50
DE
2449 file_data = dw2_get_file_names (objfile, per_cu);
2450 if (file_data == NULL)
9291a0cd
TT
2451 continue;
2452
7b9f3c50 2453 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2454 {
7b9f3c50 2455 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2456
2457 if (FILENAME_CMP (name, this_name) == 0)
2458 {
f8eba3c6
TT
2459 if (dw2_map_expand_apply (objfile, per_cu,
2460 name, full_path, real_path,
2461 callback, data))
2462 return 1;
9291a0cd
TT
2463 }
2464
2465 if (check_basename && ! base_cu
2466 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2467 base_cu = per_cu;
9291a0cd 2468
c011a4f4
DE
2469 /* Before we invoke realpath, which can get expensive when many
2470 files are involved, do a quick comparison of the basenames. */
2471 if (! basenames_may_differ
2472 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2473 continue;
2474
9291a0cd
TT
2475 if (full_path != NULL)
2476 {
7b9f3c50
DE
2477 const char *this_real_name = dw2_get_real_path (objfile,
2478 file_data, j);
9291a0cd 2479
7b9f3c50
DE
2480 if (this_real_name != NULL
2481 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2482 {
f8eba3c6
TT
2483 if (dw2_map_expand_apply (objfile, per_cu,
2484 name, full_path, real_path,
2485 callback, data))
2486 return 1;
9291a0cd
TT
2487 }
2488 }
2489
2490 if (real_path != NULL)
2491 {
7b9f3c50
DE
2492 const char *this_real_name = dw2_get_real_path (objfile,
2493 file_data, j);
9291a0cd 2494
7b9f3c50
DE
2495 if (this_real_name != NULL
2496 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2497 {
f8eba3c6
TT
2498 if (dw2_map_expand_apply (objfile, per_cu,
2499 name, full_path, real_path,
2500 callback, data))
2501 return 1;
9291a0cd
TT
2502 }
2503 }
2504 }
2505 }
2506
2507 if (base_cu)
2508 {
f8eba3c6
TT
2509 if (dw2_map_expand_apply (objfile, base_cu,
2510 name, full_path, real_path,
2511 callback, data))
2512 return 1;
9291a0cd
TT
2513 }
2514
2515 return 0;
2516}
2517
2518static struct symtab *
2519dw2_lookup_symbol (struct objfile *objfile, int block_index,
2520 const char *name, domain_enum domain)
2521{
774b6a14 2522 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2523 instead. */
2524 return NULL;
2525}
2526
2527/* A helper function that expands all symtabs that hold an object
2528 named NAME. */
2fdf6df6 2529
9291a0cd
TT
2530static void
2531dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2532{
2533 dw2_setup (objfile);
2534
ae2de4f8 2535 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2536 if (dwarf2_per_objfile->index_table)
2537 {
2538 offset_type *vec;
2539
2540 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2541 name, &vec))
2542 {
2543 offset_type i, len = MAYBE_SWAP (*vec);
2544 for (i = 0; i < len; ++i)
2545 {
2546 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2547 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2548
a0f42c21 2549 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2550 }
2551 }
2552 }
2553}
2554
774b6a14
TT
2555static void
2556dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2557 enum block_enum block_kind, const char *name,
774b6a14 2558 domain_enum domain)
9291a0cd 2559{
774b6a14 2560 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2561}
2562
2563static void
2564dw2_print_stats (struct objfile *objfile)
2565{
2566 int i, count;
2567
2568 dw2_setup (objfile);
2569 count = 0;
1fd400ff
TT
2570 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2571 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2572 {
e254ef6a 2573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2574
e254ef6a 2575 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2576 ++count;
2577 }
2578 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2579}
2580
2581static void
2582dw2_dump (struct objfile *objfile)
2583{
2584 /* Nothing worth printing. */
2585}
2586
2587static void
2588dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2589 struct section_offsets *delta)
2590{
2591 /* There's nothing to relocate here. */
2592}
2593
2594static void
2595dw2_expand_symtabs_for_function (struct objfile *objfile,
2596 const char *func_name)
2597{
2598 dw2_do_expand_symtabs_matching (objfile, func_name);
2599}
2600
2601static void
2602dw2_expand_all_symtabs (struct objfile *objfile)
2603{
2604 int i;
2605
2606 dw2_setup (objfile);
1fd400ff
TT
2607
2608 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2609 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2610 {
e254ef6a 2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2612
a0f42c21 2613 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2614 }
2615}
2616
2617static void
2618dw2_expand_symtabs_with_filename (struct objfile *objfile,
2619 const char *filename)
2620{
2621 int i;
2622
2623 dw2_setup (objfile);
d4637a04
DE
2624
2625 /* We don't need to consider type units here.
2626 This is only called for examining code, e.g. expand_line_sal.
2627 There can be an order of magnitude (or more) more type units
2628 than comp units, and we avoid them if we can. */
2629
2630 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2631 {
2632 int j;
e254ef6a 2633 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2634 struct quick_file_names *file_data;
9291a0cd 2635
3d7bb9d9 2636 /* We only need to look at symtabs not already expanded. */
e254ef6a 2637 if (per_cu->v.quick->symtab)
9291a0cd
TT
2638 continue;
2639
7b9f3c50
DE
2640 file_data = dw2_get_file_names (objfile, per_cu);
2641 if (file_data == NULL)
9291a0cd
TT
2642 continue;
2643
7b9f3c50 2644 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2645 {
7b9f3c50 2646 const char *this_name = file_data->file_names[j];
1ef75ecc 2647 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2648 {
a0f42c21 2649 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2650 break;
2651 }
2652 }
2653 }
2654}
2655
dd786858 2656static const char *
9291a0cd
TT
2657dw2_find_symbol_file (struct objfile *objfile, const char *name)
2658{
e254ef6a 2659 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2660 offset_type *vec;
7b9f3c50 2661 struct quick_file_names *file_data;
9291a0cd
TT
2662
2663 dw2_setup (objfile);
2664
ae2de4f8 2665 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2666 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2667 {
2668 struct symtab *s;
2669
2670 ALL_OBJFILE_SYMTABS (objfile, s)
2671 if (s->primary)
2672 {
2673 struct blockvector *bv = BLOCKVECTOR (s);
2674 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2675 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2676
2677 if (sym)
2678 return sym->symtab->filename;
2679 }
2680 return NULL;
2681 }
9291a0cd
TT
2682
2683 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2684 name, &vec))
2685 return NULL;
2686
2687 /* Note that this just looks at the very first one named NAME -- but
2688 actually we are looking for a function. find_main_filename
2689 should be rewritten so that it doesn't require a custom hook. It
2690 could just use the ordinary symbol tables. */
2691 /* vec[0] is the length, which must always be >0. */
e254ef6a 2692 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2693
7b9f3c50
DE
2694 file_data = dw2_get_file_names (objfile, per_cu);
2695 if (file_data == NULL)
9291a0cd
TT
2696 return NULL;
2697
7b9f3c50 2698 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2699}
2700
2701static void
40658b94
PH
2702dw2_map_matching_symbols (const char * name, domain_enum namespace,
2703 struct objfile *objfile, int global,
2704 int (*callback) (struct block *,
2705 struct symbol *, void *),
2edb89d3
JK
2706 void *data, symbol_compare_ftype *match,
2707 symbol_compare_ftype *ordered_compare)
9291a0cd 2708{
40658b94 2709 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2710 current language is Ada for a non-Ada objfile using GNU index. As Ada
2711 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2712}
2713
2714static void
f8eba3c6
TT
2715dw2_expand_symtabs_matching
2716 (struct objfile *objfile,
2717 int (*file_matcher) (const char *, void *),
2718 int (*name_matcher) (const struct language_defn *, const char *, void *),
2719 enum search_domain kind,
2720 void *data)
9291a0cd
TT
2721{
2722 int i;
2723 offset_type iter;
4b5246aa 2724 struct mapped_index *index;
9291a0cd
TT
2725
2726 dw2_setup (objfile);
ae2de4f8
DE
2727
2728 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2729 if (!dwarf2_per_objfile->index_table)
2730 return;
4b5246aa 2731 index = dwarf2_per_objfile->index_table;
9291a0cd 2732
7b08b9eb
JK
2733 if (file_matcher != NULL)
2734 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2735 + dwarf2_per_objfile->n_type_comp_units); ++i)
2736 {
2737 int j;
2738 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2739 struct quick_file_names *file_data;
2740
2741 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2742
2743 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2744 if (per_cu->v.quick->symtab)
2745 continue;
2746
2747 file_data = dw2_get_file_names (objfile, per_cu);
2748 if (file_data == NULL)
2749 continue;
2750
2751 for (j = 0; j < file_data->num_file_names; ++j)
2752 {
2753 if (file_matcher (file_data->file_names[j], data))
2754 {
2755 per_cu->v.quick->mark = 1;
2756 break;
2757 }
2758 }
2759 }
9291a0cd 2760
3876f04e 2761 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2762 {
2763 offset_type idx = 2 * iter;
2764 const char *name;
2765 offset_type *vec, vec_len, vec_idx;
2766
3876f04e 2767 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2768 continue;
2769
3876f04e 2770 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2771
f8eba3c6 2772 if (! (*name_matcher) (current_language, name, data))
9291a0cd
TT
2773 continue;
2774
2775 /* The name was matched, now expand corresponding CUs that were
2776 marked. */
4b5246aa 2777 vec = (offset_type *) (index->constant_pool
3876f04e 2778 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2779 vec_len = MAYBE_SWAP (vec[0]);
2780 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2781 {
e254ef6a 2782 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2783
e254ef6a 2784 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2785 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2786 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2787 }
2788 }
2789}
2790
2791static struct symtab *
2792dw2_find_pc_sect_symtab (struct objfile *objfile,
2793 struct minimal_symbol *msymbol,
2794 CORE_ADDR pc,
2795 struct obj_section *section,
2796 int warn_if_readin)
2797{
2798 struct dwarf2_per_cu_data *data;
2799
2800 dw2_setup (objfile);
2801
2802 if (!objfile->psymtabs_addrmap)
2803 return NULL;
2804
2805 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2806 if (!data)
2807 return NULL;
2808
2809 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2810 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2811 paddress (get_objfile_arch (objfile), pc));
2812
a0f42c21 2813 return dw2_instantiate_symtab (data);
9291a0cd
TT
2814}
2815
9291a0cd 2816static void
44b13c5a 2817dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2818 void *data, int need_fullname)
9291a0cd
TT
2819{
2820 int i;
2821
2822 dw2_setup (objfile);
ae2de4f8 2823
1fd400ff
TT
2824 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2825 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2826 {
2827 int j;
e254ef6a 2828 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2829 struct quick_file_names *file_data;
9291a0cd 2830
3d7bb9d9 2831 /* We only need to look at symtabs not already expanded. */
e254ef6a 2832 if (per_cu->v.quick->symtab)
9291a0cd
TT
2833 continue;
2834
7b9f3c50
DE
2835 file_data = dw2_get_file_names (objfile, per_cu);
2836 if (file_data == NULL)
9291a0cd
TT
2837 continue;
2838
7b9f3c50 2839 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2840 {
74e2f255
DE
2841 const char *this_real_name;
2842
2843 if (need_fullname)
2844 this_real_name = dw2_get_real_path (objfile, file_data, j);
2845 else
2846 this_real_name = NULL;
7b9f3c50 2847 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2848 }
2849 }
2850}
2851
2852static int
2853dw2_has_symbols (struct objfile *objfile)
2854{
2855 return 1;
2856}
2857
2858const struct quick_symbol_functions dwarf2_gdb_index_functions =
2859{
2860 dw2_has_symbols,
2861 dw2_find_last_source_symtab,
2862 dw2_forget_cached_source_info,
f8eba3c6 2863 dw2_map_symtabs_matching_filename,
9291a0cd 2864 dw2_lookup_symbol,
774b6a14 2865 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2866 dw2_print_stats,
2867 dw2_dump,
2868 dw2_relocate,
2869 dw2_expand_symtabs_for_function,
2870 dw2_expand_all_symtabs,
2871 dw2_expand_symtabs_with_filename,
2872 dw2_find_symbol_file,
40658b94 2873 dw2_map_matching_symbols,
9291a0cd
TT
2874 dw2_expand_symtabs_matching,
2875 dw2_find_pc_sect_symtab,
9291a0cd
TT
2876 dw2_map_symbol_filenames
2877};
2878
2879/* Initialize for reading DWARF for this objfile. Return 0 if this
2880 file will use psymtabs, or 1 if using the GNU index. */
2881
2882int
2883dwarf2_initialize_objfile (struct objfile *objfile)
2884{
2885 /* If we're about to read full symbols, don't bother with the
2886 indices. In this case we also don't care if some other debug
2887 format is making psymtabs, because they are all about to be
2888 expanded anyway. */
2889 if ((objfile->flags & OBJF_READNOW))
2890 {
2891 int i;
2892
2893 dwarf2_per_objfile->using_index = 1;
2894 create_all_comp_units (objfile);
1fd400ff 2895 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2896 dwarf2_per_objfile->quick_file_names_table =
2897 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2898
1fd400ff
TT
2899 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2900 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2901 {
e254ef6a 2902 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2903
e254ef6a
DE
2904 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2905 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2906 }
2907
2908 /* Return 1 so that gdb sees the "quick" functions. However,
2909 these functions will be no-ops because we will have expanded
2910 all symtabs. */
2911 return 1;
2912 }
2913
2914 if (dwarf2_read_index (objfile))
2915 return 1;
2916
9291a0cd
TT
2917 return 0;
2918}
2919
2920\f
2921
dce234bc
PP
2922/* Build a partial symbol table. */
2923
2924void
f29dff0a 2925dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2926{
f29dff0a 2927 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2928 {
2929 init_psymbol_list (objfile, 1024);
2930 }
2931
d146bf1e 2932 dwarf2_build_psymtabs_hard (objfile);
c906108c 2933}
c906108c 2934
45452591
DE
2935/* Return TRUE if OFFSET is within CU_HEADER. */
2936
2937static inline int
2938offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2939{
2940 unsigned int bottom = cu_header->offset;
2941 unsigned int top = (cu_header->offset
2942 + cu_header->length
2943 + cu_header->initial_length_size);
9a619af0 2944
45452591
DE
2945 return (offset >= bottom && offset < top);
2946}
2947
93311388
DE
2948/* Read in the comp unit header information from the debug_info at info_ptr.
2949 NOTE: This leaves members offset, first_die_offset to be filled in
2950 by the caller. */
107d2387 2951
fe1b8b76 2952static gdb_byte *
107d2387 2953read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2954 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2955{
2956 int signed_addr;
891d2f0b 2957 unsigned int bytes_read;
c764a876
DE
2958
2959 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2960 cu_header->initial_length_size = bytes_read;
2961 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2962 info_ptr += bytes_read;
107d2387
AC
2963 cu_header->version = read_2_bytes (abfd, info_ptr);
2964 info_ptr += 2;
613e1657 2965 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2966 &bytes_read);
613e1657 2967 info_ptr += bytes_read;
107d2387
AC
2968 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2969 info_ptr += 1;
2970 signed_addr = bfd_get_sign_extend_vma (abfd);
2971 if (signed_addr < 0)
8e65ff28 2972 internal_error (__FILE__, __LINE__,
e2e0b3e5 2973 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2974 cu_header->signed_addr_p = signed_addr;
c764a876 2975
107d2387
AC
2976 return info_ptr;
2977}
2978
adabb602
DE
2979/* Read in a CU header and perform some basic error checking. */
2980
fe1b8b76
JB
2981static gdb_byte *
2982partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2983 gdb_byte *buffer, unsigned int buffer_size,
b0df02fd 2984 bfd *abfd, int is_debug_types_section)
72bf9492 2985{
fe1b8b76 2986 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 2987
adabb602
DE
2988 header->offset = beg_of_comp_unit - buffer;
2989
72bf9492
DJ
2990 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2991
460c1c54
CC
2992 /* If we're reading a type unit, skip over the signature and
2993 type_offset fields. */
b0df02fd 2994 if (is_debug_types_section)
460c1c54
CC
2995 info_ptr += 8 /*signature*/ + header->offset_size;
2996
adabb602
DE
2997 header->first_die_offset = info_ptr - beg_of_comp_unit;
2998
2dc7f7b3 2999 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 3000 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
3001 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3002 bfd_get_filename (abfd));
72bf9492 3003
9e0ac564
TT
3004 if (header->abbrev_offset
3005 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3006 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
3007 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3008 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 3009 (long) header->abbrev_offset,
93311388 3010 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3011 bfd_get_filename (abfd));
3012
3013 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 3014 > buffer + buffer_size)
8a3fe4f8
AC
3015 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3016 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3017 (long) header->length,
93311388 3018 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3019 bfd_get_filename (abfd));
3020
3021 return info_ptr;
3022}
3023
348e048f
DE
3024/* Read in the types comp unit header information from .debug_types entry at
3025 types_ptr. The result is a pointer to one past the end of the header. */
3026
3027static gdb_byte *
3028read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3029 struct dwarf2_section_info *section,
348e048f
DE
3030 ULONGEST *signature,
3031 gdb_byte *types_ptr, bfd *abfd)
3032{
348e048f
DE
3033 gdb_byte *initial_types_ptr = types_ptr;
3034
8b70b953
TT
3035 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3036 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3037
3038 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3039
3040 *signature = read_8_bytes (abfd, types_ptr);
3041 types_ptr += 8;
3042 types_ptr += cu_header->offset_size;
3043 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3044
3045 return types_ptr;
3046}
3047
aaa75496
JB
3048/* Allocate a new partial symtab for file named NAME and mark this new
3049 partial symtab as being an include of PST. */
3050
3051static void
3052dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3053 struct objfile *objfile)
3054{
3055 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3056
3057 subpst->section_offsets = pst->section_offsets;
3058 subpst->textlow = 0;
3059 subpst->texthigh = 0;
3060
3061 subpst->dependencies = (struct partial_symtab **)
3062 obstack_alloc (&objfile->objfile_obstack,
3063 sizeof (struct partial_symtab *));
3064 subpst->dependencies[0] = pst;
3065 subpst->number_of_dependencies = 1;
3066
3067 subpst->globals_offset = 0;
3068 subpst->n_global_syms = 0;
3069 subpst->statics_offset = 0;
3070 subpst->n_static_syms = 0;
3071 subpst->symtab = NULL;
3072 subpst->read_symtab = pst->read_symtab;
3073 subpst->readin = 0;
3074
3075 /* No private part is necessary for include psymtabs. This property
3076 can be used to differentiate between such include psymtabs and
10b3939b 3077 the regular ones. */
58a9656e 3078 subpst->read_symtab_private = NULL;
aaa75496
JB
3079}
3080
3081/* Read the Line Number Program data and extract the list of files
3082 included by the source file represented by PST. Build an include
d85a05f0 3083 partial symtab for each of these included files. */
aaa75496
JB
3084
3085static void
3086dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3087 struct die_info *die,
aaa75496
JB
3088 struct partial_symtab *pst)
3089{
3090 struct objfile *objfile = cu->objfile;
3091 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3092 struct line_header *lh = NULL;
3093 struct attribute *attr;
aaa75496 3094
d85a05f0
DJ
3095 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3096 if (attr)
3097 {
3098 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3099
d85a05f0
DJ
3100 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3101 }
aaa75496
JB
3102 if (lh == NULL)
3103 return; /* No linetable, so no includes. */
3104
c6da4cef
DE
3105 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3106 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3107
3108 free_line_header (lh);
3109}
3110
348e048f
DE
3111static hashval_t
3112hash_type_signature (const void *item)
3113{
3114 const struct signatured_type *type_sig = item;
9a619af0 3115
348e048f
DE
3116 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3117 return type_sig->signature;
3118}
3119
3120static int
3121eq_type_signature (const void *item_lhs, const void *item_rhs)
3122{
3123 const struct signatured_type *lhs = item_lhs;
3124 const struct signatured_type *rhs = item_rhs;
9a619af0 3125
348e048f
DE
3126 return lhs->signature == rhs->signature;
3127}
3128
1fd400ff
TT
3129/* Allocate a hash table for signatured types. */
3130
3131static htab_t
673bfd45 3132allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3133{
3134 return htab_create_alloc_ex (41,
3135 hash_type_signature,
3136 eq_type_signature,
3137 NULL,
3138 &objfile->objfile_obstack,
3139 hashtab_obstack_allocate,
3140 dummy_obstack_deallocate);
3141}
3142
3143/* A helper function to add a signatured type CU to a list. */
3144
3145static int
3146add_signatured_type_cu_to_list (void **slot, void *datum)
3147{
3148 struct signatured_type *sigt = *slot;
3149 struct dwarf2_per_cu_data ***datap = datum;
3150
3151 **datap = &sigt->per_cu;
3152 ++*datap;
3153
3154 return 1;
3155}
3156
348e048f
DE
3157/* Create the hash table of all entries in the .debug_types section.
3158 The result is zero if there is an error (e.g. missing .debug_types section),
3159 otherwise non-zero. */
3160
3161static int
3162create_debug_types_hash_table (struct objfile *objfile)
3163{
8b70b953 3164 htab_t types_htab = NULL;
1fd400ff 3165 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3166 int ix;
3167 struct dwarf2_section_info *section;
348e048f 3168
8b70b953 3169 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3170 {
3171 dwarf2_per_objfile->signatured_types = NULL;
3172 return 0;
3173 }
3174
8b70b953
TT
3175 for (ix = 0;
3176 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3177 ix, section);
3178 ++ix)
3179 {
3180 gdb_byte *info_ptr, *end_ptr;
348e048f 3181
8b70b953
TT
3182 dwarf2_read_section (objfile, section);
3183 info_ptr = section->buffer;
348e048f 3184
8b70b953
TT
3185 if (info_ptr == NULL)
3186 continue;
348e048f 3187
8b70b953
TT
3188 if (types_htab == NULL)
3189 types_htab = allocate_signatured_type_table (objfile);
348e048f 3190
8b70b953
TT
3191 if (dwarf2_die_debug)
3192 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3193
3194 end_ptr = info_ptr + section->size;
3195 while (info_ptr < end_ptr)
3196 {
3197 unsigned int offset;
3198 unsigned int offset_size;
3199 unsigned int type_offset;
3200 unsigned int length, initial_length_size;
3201 unsigned short version;
3202 ULONGEST signature;
3203 struct signatured_type *type_sig;
3204 void **slot;
3205 gdb_byte *ptr = info_ptr;
348e048f 3206
8b70b953 3207 offset = ptr - section->buffer;
348e048f 3208
8b70b953
TT
3209 /* We need to read the type's signature in order to build the hash
3210 table, but we don't need to read anything else just yet. */
348e048f 3211
8b70b953
TT
3212 /* Sanity check to ensure entire cu is present. */
3213 length = read_initial_length (objfile->obfd, ptr,
3214 &initial_length_size);
3215 if (ptr + length + initial_length_size > end_ptr)
3216 {
3217 complaint (&symfile_complaints,
3218 _("debug type entry runs off end "
3219 "of `.debug_types' section, ignored"));
3220 break;
3221 }
348e048f 3222
8b70b953
TT
3223 offset_size = initial_length_size == 4 ? 4 : 8;
3224 ptr += initial_length_size;
3225 version = bfd_get_16 (objfile->obfd, ptr);
3226 ptr += 2;
3227 ptr += offset_size; /* abbrev offset */
3228 ptr += 1; /* address size */
3229 signature = bfd_get_64 (objfile->obfd, ptr);
3230 ptr += 8;
3231 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3232 ptr += offset_size;
6caca83c
CC
3233
3234 /* Skip dummy type units. */
3235 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3236 {
3237 info_ptr = info_ptr + initial_length_size + length;
3238 continue;
3239 }
8b70b953
TT
3240
3241 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3242 memset (type_sig, 0, sizeof (*type_sig));
3243 type_sig->signature = signature;
3244 type_sig->type_offset = type_offset;
3245 type_sig->per_cu.objfile = objfile;
b0df02fd 3246 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3247 type_sig->per_cu.offset = offset;
3248
3249 slot = htab_find_slot (types_htab, type_sig, INSERT);
3250 gdb_assert (slot != NULL);
3251 if (*slot != NULL)
3252 {
3253 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3254
8b70b953
TT
3255 complaint (&symfile_complaints,
3256 _("debug type entry at offset 0x%x is duplicate to the "
3257 "entry at offset 0x%x, signature 0x%s"),
3258 offset, dup_sig->per_cu.offset,
3259 phex (signature, sizeof (signature)));
3260 gdb_assert (signature == dup_sig->signature);
3261 }
3262 *slot = type_sig;
348e048f 3263
8b70b953
TT
3264 if (dwarf2_die_debug)
3265 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3266 offset, phex (signature, sizeof (signature)));
348e048f 3267
8b70b953
TT
3268 info_ptr = info_ptr + initial_length_size + length;
3269 }
348e048f
DE
3270 }
3271
3272 dwarf2_per_objfile->signatured_types = types_htab;
3273
1fd400ff
TT
3274 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3275 dwarf2_per_objfile->type_comp_units
3276 = obstack_alloc (&objfile->objfile_obstack,
3277 dwarf2_per_objfile->n_type_comp_units
3278 * sizeof (struct dwarf2_per_cu_data *));
3279 iter = &dwarf2_per_objfile->type_comp_units[0];
3280 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3281 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3282 == dwarf2_per_objfile->n_type_comp_units);
3283
348e048f
DE
3284 return 1;
3285}
3286
3287/* Lookup a signature based type.
3288 Returns NULL if SIG is not present in the table. */
3289
3290static struct signatured_type *
3291lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3292{
3293 struct signatured_type find_entry, *entry;
3294
3295 if (dwarf2_per_objfile->signatured_types == NULL)
3296 {
3297 complaint (&symfile_complaints,
55f1336d 3298 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3299 return 0;
3300 }
3301
3302 find_entry.signature = sig;
3303 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3304 return entry;
3305}
3306
d85a05f0
DJ
3307/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3308
3309static void
3310init_cu_die_reader (struct die_reader_specs *reader,
3311 struct dwarf2_cu *cu)
3312{
3313 reader->abfd = cu->objfile->obfd;
3314 reader->cu = cu;
b0df02fd 3315 if (cu->per_cu->debug_types_section)
be391dca 3316 {
b0df02fd
DE
3317 gdb_assert (cu->per_cu->debug_types_section->readin);
3318 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3319 }
d85a05f0 3320 else
be391dca
TT
3321 {
3322 gdb_assert (dwarf2_per_objfile->info.readin);
3323 reader->buffer = dwarf2_per_objfile->info.buffer;
3324 }
d85a05f0
DJ
3325}
3326
3327/* Find the base address of the compilation unit for range lists and
3328 location lists. It will normally be specified by DW_AT_low_pc.
3329 In DWARF-3 draft 4, the base address could be overridden by
3330 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3331 compilation units with discontinuous ranges. */
3332
3333static void
3334dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3335{
3336 struct attribute *attr;
3337
3338 cu->base_known = 0;
3339 cu->base_address = 0;
3340
3341 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3342 if (attr)
3343 {
3344 cu->base_address = DW_ADDR (attr);
3345 cu->base_known = 1;
3346 }
3347 else
3348 {
3349 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3350 if (attr)
3351 {
3352 cu->base_address = DW_ADDR (attr);
3353 cu->base_known = 1;
3354 }
3355 }
3356}
3357
348e048f
DE
3358/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3359 to combine the common parts.
93311388 3360 Process a compilation unit for a psymtab.
348e048f
DE
3361 BUFFER is a pointer to the beginning of the dwarf section buffer,
3362 either .debug_info or debug_types.
93311388
DE
3363 INFO_PTR is a pointer to the start of the CU.
3364 Returns a pointer to the next CU. */
aaa75496 3365
93311388 3366static gdb_byte *
a0f42c21 3367process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
93311388
DE
3368 gdb_byte *buffer, gdb_byte *info_ptr,
3369 unsigned int buffer_size)
c906108c 3370{
a0f42c21 3371 struct objfile *objfile = this_cu->objfile;
c906108c 3372 bfd *abfd = objfile->obfd;
93311388 3373 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3374 struct die_info *comp_unit_die;
c906108c 3375 struct partial_symtab *pst;
5734ee8b 3376 CORE_ADDR baseaddr;
93311388
DE
3377 struct cleanup *back_to_inner;
3378 struct dwarf2_cu cu;
d85a05f0
DJ
3379 int has_children, has_pc_info;
3380 struct attribute *attr;
d85a05f0
DJ
3381 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3382 struct die_reader_specs reader_specs;
3e2a0cee 3383 const char *filename;
c906108c 3384
23745b47
DE
3385 /* If this compilation unit was already read in, free the
3386 cached copy in order to read it in again. This is
3387 necessary because we skipped some symbols when we first
3388 read in the compilation unit (see load_partial_dies).
3389 This problem could be avoided, but the benefit is
3390 unclear. */
3391 if (this_cu->cu != NULL)
3392 free_one_cached_comp_unit (this_cu->cu);
3393
3394 /* Note that this is a pointer to our stack frame, being
3395 added to a global data structure. It will be cleaned up
3396 in free_stack_comp_unit when we finish with this
3397 compilation unit. */
3398 init_one_comp_unit (&cu, this_cu);
93311388 3399 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3400
93311388
DE
3401 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3402 buffer, buffer_size,
460c1c54 3403 abfd,
b0df02fd 3404 this_cu->debug_types_section != NULL);
10b3939b 3405
6caca83c
CC
3406 /* Skip dummy compilation units. */
3407 if (info_ptr >= buffer + buffer_size
3408 || peek_abbrev_code (abfd, info_ptr) == 0)
3409 {
3410 info_ptr = (beg_of_comp_unit + cu.header.length
3411 + cu.header.initial_length_size);
3412 do_cleanups (back_to_inner);
3413 return info_ptr;
3414 }
3415
93311388 3416 cu.list_in_scope = &file_symbols;
af703f96 3417
93311388 3418 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3419 dwarf2_read_abbrevs (&cu);
93311388 3420 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3421
93311388 3422 /* Read the compilation unit die. */
d85a05f0
DJ
3423 init_cu_die_reader (&reader_specs, &cu);
3424 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3425 &has_children);
93311388 3426
b0df02fd 3427 if (this_cu->debug_types_section)
348e048f 3428 {
b3c8eb43
JK
3429 /* LENGTH has not been set yet for type units. */
3430 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3431 this_cu->length = cu.header.length + cu.header.initial_length_size;
3432 }
d85a05f0 3433 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3434 {
93311388
DE
3435 info_ptr = (beg_of_comp_unit + cu.header.length
3436 + cu.header.initial_length_size);
3437 do_cleanups (back_to_inner);
3438 return info_ptr;
3439 }
72bf9492 3440
9816fde3 3441 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3442
93311388 3443 /* Allocate a new partial symbol table structure. */
d85a05f0 3444 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3445 if (attr == NULL || !DW_STRING (attr))
3446 filename = "";
3447 else
3448 filename = DW_STRING (attr);
93311388 3449 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3450 filename,
93311388
DE
3451 /* TEXTLOW and TEXTHIGH are set below. */
3452 0,
3453 objfile->global_psymbols.next,
3454 objfile->static_psymbols.next);
9750bca9 3455 pst->psymtabs_addrmap_supported = 1;
72bf9492 3456
d85a05f0
DJ
3457 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3458 if (attr != NULL)
3459 pst->dirname = DW_STRING (attr);
72bf9492 3460
e38df1d0 3461 pst->read_symtab_private = this_cu;
72bf9492 3462
93311388 3463 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3464
0963b4bd 3465 /* Store the function that reads in the rest of the symbol table. */
93311388 3466 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3467
9291a0cd 3468 this_cu->v.psymtab = pst;
c906108c 3469
d85a05f0
DJ
3470 dwarf2_find_base_address (comp_unit_die, &cu);
3471
93311388
DE
3472 /* Possibly set the default values of LOWPC and HIGHPC from
3473 `DW_AT_ranges'. */
d85a05f0
DJ
3474 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3475 &best_highpc, &cu, pst);
3476 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3477 /* Store the contiguous range if it is not empty; it can be empty for
3478 CUs with no code. */
3479 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3480 best_lowpc + baseaddr,
3481 best_highpc + baseaddr - 1, pst);
93311388
DE
3482
3483 /* Check if comp unit has_children.
3484 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3485 If not, there's no more debug_info for this comp unit. */
d85a05f0 3486 if (has_children)
93311388
DE
3487 {
3488 struct partial_die_info *first_die;
3489 CORE_ADDR lowpc, highpc;
31ffec48 3490
93311388
DE
3491 lowpc = ((CORE_ADDR) -1);
3492 highpc = ((CORE_ADDR) 0);
c906108c 3493
93311388 3494 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3495
93311388 3496 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3497 ! has_pc_info, &cu);
57c22c6c 3498
93311388
DE
3499 /* If we didn't find a lowpc, set it to highpc to avoid
3500 complaints from `maint check'. */
3501 if (lowpc == ((CORE_ADDR) -1))
3502 lowpc = highpc;
10b3939b 3503
93311388
DE
3504 /* If the compilation unit didn't have an explicit address range,
3505 then use the information extracted from its child dies. */
d85a05f0 3506 if (! has_pc_info)
93311388 3507 {
d85a05f0
DJ
3508 best_lowpc = lowpc;
3509 best_highpc = highpc;
93311388
DE
3510 }
3511 }
d85a05f0
DJ
3512 pst->textlow = best_lowpc + baseaddr;
3513 pst->texthigh = best_highpc + baseaddr;
c906108c 3514
93311388
DE
3515 pst->n_global_syms = objfile->global_psymbols.next -
3516 (objfile->global_psymbols.list + pst->globals_offset);
3517 pst->n_static_syms = objfile->static_psymbols.next -
3518 (objfile->static_psymbols.list + pst->statics_offset);
3519 sort_pst_symbols (pst);
c906108c 3520
93311388
DE
3521 info_ptr = (beg_of_comp_unit + cu.header.length
3522 + cu.header.initial_length_size);
ae038cb0 3523
b0df02fd 3524 if (this_cu->debug_types_section)
348e048f
DE
3525 {
3526 /* It's not clear we want to do anything with stmt lists here.
3527 Waiting to see what gcc ultimately does. */
3528 }
d85a05f0 3529 else
93311388
DE
3530 {
3531 /* Get the list of files included in the current compilation unit,
3532 and build a psymtab for each of them. */
d85a05f0 3533 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3534 }
ae038cb0 3535
93311388 3536 do_cleanups (back_to_inner);
ae038cb0 3537
93311388
DE
3538 return info_ptr;
3539}
ff013f42 3540
348e048f
DE
3541/* Traversal function for htab_traverse_noresize.
3542 Process one .debug_types comp-unit. */
3543
3544static int
3545process_type_comp_unit (void **slot, void *info)
3546{
3547 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3548 struct dwarf2_per_cu_data *this_cu;
3549
a0f42c21 3550 gdb_assert (info == NULL);
348e048f 3551 this_cu = &entry->per_cu;
348e048f 3552
b0df02fd 3553 gdb_assert (this_cu->debug_types_section->readin);
a0f42c21 3554 process_psymtab_comp_unit (this_cu,
b0df02fd
DE
3555 this_cu->debug_types_section->buffer,
3556 (this_cu->debug_types_section->buffer
8b70b953 3557 + this_cu->offset),
b0df02fd 3558 this_cu->debug_types_section->size);
348e048f
DE
3559
3560 return 1;
3561}
3562
3563/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3564 Build partial symbol tables for the .debug_types comp-units. */
3565
3566static void
3567build_type_psymtabs (struct objfile *objfile)
3568{
3569 if (! create_debug_types_hash_table (objfile))
3570 return;
3571
3572 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3573 process_type_comp_unit, NULL);
348e048f
DE
3574}
3575
60606b2c
TT
3576/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3577
3578static void
3579psymtabs_addrmap_cleanup (void *o)
3580{
3581 struct objfile *objfile = o;
ec61707d 3582
60606b2c
TT
3583 objfile->psymtabs_addrmap = NULL;
3584}
3585
93311388
DE
3586/* Build the partial symbol table by doing a quick pass through the
3587 .debug_info and .debug_abbrev sections. */
72bf9492 3588
93311388 3589static void
c67a9c90 3590dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3591{
93311388 3592 gdb_byte *info_ptr;
60606b2c
TT
3593 struct cleanup *back_to, *addrmap_cleanup;
3594 struct obstack temp_obstack;
93311388 3595
98bfdba5
PA
3596 dwarf2_per_objfile->reading_partial_symbols = 1;
3597
be391dca 3598 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3599 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3600
93311388
DE
3601 /* Any cached compilation units will be linked by the per-objfile
3602 read_in_chain. Make sure to free them when we're done. */
3603 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3604
348e048f
DE
3605 build_type_psymtabs (objfile);
3606
93311388 3607 create_all_comp_units (objfile);
c906108c 3608
60606b2c
TT
3609 /* Create a temporary address map on a temporary obstack. We later
3610 copy this to the final obstack. */
3611 obstack_init (&temp_obstack);
3612 make_cleanup_obstack_free (&temp_obstack);
3613 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3614 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3615
93311388
DE
3616 /* Since the objects we're extracting from .debug_info vary in
3617 length, only the individual functions to extract them (like
3618 read_comp_unit_head and load_partial_die) can really know whether
3619 the buffer is large enough to hold another complete object.
c906108c 3620
93311388
DE
3621 At the moment, they don't actually check that. If .debug_info
3622 holds just one extra byte after the last compilation unit's dies,
3623 then read_comp_unit_head will happily read off the end of the
3624 buffer. read_partial_die is similarly casual. Those functions
3625 should be fixed.
c906108c 3626
93311388
DE
3627 For this loop condition, simply checking whether there's any data
3628 left at all should be sufficient. */
c906108c 3629
93311388
DE
3630 while (info_ptr < (dwarf2_per_objfile->info.buffer
3631 + dwarf2_per_objfile->info.size))
3632 {
3633 struct dwarf2_per_cu_data *this_cu;
dd373385 3634
3e43a32a
MS
3635 this_cu = dwarf2_find_comp_unit (info_ptr
3636 - dwarf2_per_objfile->info.buffer,
93311388 3637 objfile);
aaa75496 3638
a0f42c21 3639 info_ptr = process_psymtab_comp_unit (this_cu,
93311388
DE
3640 dwarf2_per_objfile->info.buffer,
3641 info_ptr,
3642 dwarf2_per_objfile->info.size);
c906108c 3643 }
ff013f42
JK
3644
3645 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3646 &objfile->objfile_obstack);
60606b2c 3647 discard_cleanups (addrmap_cleanup);
ff013f42 3648
ae038cb0
DJ
3649 do_cleanups (back_to);
3650}
3651
93311388 3652/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3653
3654static void
a0f42c21 3655load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3656{
a0f42c21 3657 struct objfile *objfile = this_cu->objfile;
ae038cb0 3658 bfd *abfd = objfile->obfd;
adabb602 3659 gdb_byte *info_ptr;
d85a05f0 3660 struct die_info *comp_unit_die;
ae038cb0 3661 struct dwarf2_cu *cu;
1d9ec526 3662 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3663 int has_children;
3664 struct die_reader_specs reader_specs;
98bfdba5 3665 int read_cu = 0;
ae038cb0 3666
b0df02fd 3667 gdb_assert (! this_cu->debug_types_section);
348e048f 3668
be391dca 3669 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3670 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3671
98bfdba5
PA
3672 if (this_cu->cu == NULL)
3673 {
9816fde3 3674 cu = xmalloc (sizeof (*cu));
23745b47 3675 init_one_comp_unit (cu, this_cu);
ae038cb0 3676
98bfdba5 3677 read_cu = 1;
ae038cb0 3678
98bfdba5 3679 /* If an error occurs while loading, release our storage. */
68dc6402 3680 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3681
98bfdba5
PA
3682 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3683 dwarf2_per_objfile->info.buffer,
3684 dwarf2_per_objfile->info.size,
460c1c54 3685 abfd, 0);
ae038cb0 3686
6caca83c
CC
3687 /* Skip dummy compilation units. */
3688 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3689 + dwarf2_per_objfile->info.size)
3690 || peek_abbrev_code (abfd, info_ptr) == 0)
3691 {
3692 do_cleanups (free_cu_cleanup);
3693 return;
3694 }
3695
98bfdba5
PA
3696 /* Link this CU into read_in_chain. */
3697 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3698 dwarf2_per_objfile->read_in_chain = this_cu;
3699 }
3700 else
3701 {
3702 cu = this_cu->cu;
3703 info_ptr += cu->header.first_die_offset;
3704 }
ae038cb0
DJ
3705
3706 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3707 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3708 dwarf2_read_abbrevs (cu);
98bfdba5 3709 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3710
3711 /* Read the compilation unit die. */
d85a05f0
DJ
3712 init_cu_die_reader (&reader_specs, cu);
3713 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3714 &has_children);
ae038cb0 3715
9816fde3 3716 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3717
ae038cb0
DJ
3718 /* Check if comp unit has_children.
3719 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3720 If not, there's no more debug_info for this comp unit. */
d85a05f0 3721 if (has_children)
93311388 3722 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3723
98bfdba5
PA
3724 do_cleanups (free_abbrevs_cleanup);
3725
3726 if (read_cu)
3727 {
3728 /* We've successfully allocated this compilation unit. Let our
3729 caller clean it up when finished with it. */
3730 discard_cleanups (free_cu_cleanup);
3731 }
ae038cb0
DJ
3732}
3733
9cdd5dbd
DE
3734/* Create a list of all compilation units in OBJFILE.
3735 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3736
3737static void
3738create_all_comp_units (struct objfile *objfile)
3739{
3740 int n_allocated;
3741 int n_comp_units;
3742 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3743 gdb_byte *info_ptr;
3744
3745 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3746 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3747
3748 n_comp_units = 0;
3749 n_allocated = 10;
3750 all_comp_units = xmalloc (n_allocated
3751 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3752
3e43a32a
MS
3753 while (info_ptr < dwarf2_per_objfile->info.buffer
3754 + dwarf2_per_objfile->info.size)
ae038cb0 3755 {
c764a876 3756 unsigned int length, initial_length_size;
ae038cb0 3757 struct dwarf2_per_cu_data *this_cu;
c764a876 3758 unsigned int offset;
ae038cb0 3759
dce234bc 3760 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3761
3762 /* Read just enough information to find out where the next
3763 compilation unit is. */
c764a876
DE
3764 length = read_initial_length (objfile->obfd, info_ptr,
3765 &initial_length_size);
ae038cb0
DJ
3766
3767 /* Save the compilation unit for later lookup. */
3768 this_cu = obstack_alloc (&objfile->objfile_obstack,
3769 sizeof (struct dwarf2_per_cu_data));
3770 memset (this_cu, 0, sizeof (*this_cu));
3771 this_cu->offset = offset;
c764a876 3772 this_cu->length = length + initial_length_size;
9291a0cd 3773 this_cu->objfile = objfile;
ae038cb0
DJ
3774
3775 if (n_comp_units == n_allocated)
3776 {
3777 n_allocated *= 2;
3778 all_comp_units = xrealloc (all_comp_units,
3779 n_allocated
3780 * sizeof (struct dwarf2_per_cu_data *));
3781 }
3782 all_comp_units[n_comp_units++] = this_cu;
3783
3784 info_ptr = info_ptr + this_cu->length;
3785 }
3786
3787 dwarf2_per_objfile->all_comp_units
3788 = obstack_alloc (&objfile->objfile_obstack,
3789 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3790 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3791 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3792 xfree (all_comp_units);
3793 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3794}
3795
5734ee8b
DJ
3796/* Process all loaded DIEs for compilation unit CU, starting at
3797 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3798 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3799 DW_AT_ranges). If NEED_PC is set, then this function will set
3800 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3801 and record the covered ranges in the addrmap. */
c906108c 3802
72bf9492
DJ
3803static void
3804scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3805 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3806{
72bf9492 3807 struct partial_die_info *pdi;
c906108c 3808
91c24f0a
DC
3809 /* Now, march along the PDI's, descending into ones which have
3810 interesting children but skipping the children of the other ones,
3811 until we reach the end of the compilation unit. */
c906108c 3812
72bf9492 3813 pdi = first_die;
91c24f0a 3814
72bf9492
DJ
3815 while (pdi != NULL)
3816 {
3817 fixup_partial_die (pdi, cu);
c906108c 3818
f55ee35c 3819 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3820 children, so we need to look at them. Ditto for anonymous
3821 enums. */
933c6fe4 3822
72bf9492 3823 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3824 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3825 {
72bf9492 3826 switch (pdi->tag)
c906108c
SS
3827 {
3828 case DW_TAG_subprogram:
5734ee8b 3829 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3830 break;
72929c62 3831 case DW_TAG_constant:
c906108c
SS
3832 case DW_TAG_variable:
3833 case DW_TAG_typedef:
91c24f0a 3834 case DW_TAG_union_type:
72bf9492 3835 if (!pdi->is_declaration)
63d06c5c 3836 {
72bf9492 3837 add_partial_symbol (pdi, cu);
63d06c5c
DC
3838 }
3839 break;
c906108c 3840 case DW_TAG_class_type:
680b30c7 3841 case DW_TAG_interface_type:
c906108c 3842 case DW_TAG_structure_type:
72bf9492 3843 if (!pdi->is_declaration)
c906108c 3844 {
72bf9492 3845 add_partial_symbol (pdi, cu);
c906108c
SS
3846 }
3847 break;
91c24f0a 3848 case DW_TAG_enumeration_type:
72bf9492
DJ
3849 if (!pdi->is_declaration)
3850 add_partial_enumeration (pdi, cu);
c906108c
SS
3851 break;
3852 case DW_TAG_base_type:
a02abb62 3853 case DW_TAG_subrange_type:
c906108c 3854 /* File scope base type definitions are added to the partial
c5aa993b 3855 symbol table. */
72bf9492 3856 add_partial_symbol (pdi, cu);
c906108c 3857 break;
d9fa45fe 3858 case DW_TAG_namespace:
5734ee8b 3859 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3860 break;
5d7cb8df
JK
3861 case DW_TAG_module:
3862 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3863 break;
c906108c
SS
3864 default:
3865 break;
3866 }
3867 }
3868
72bf9492
DJ
3869 /* If the die has a sibling, skip to the sibling. */
3870
3871 pdi = pdi->die_sibling;
3872 }
3873}
3874
3875/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3876
72bf9492 3877 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3878 name is concatenated with "::" and the partial DIE's name. For
3879 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3880 Enumerators are an exception; they use the scope of their parent
3881 enumeration type, i.e. the name of the enumeration type is not
3882 prepended to the enumerator.
91c24f0a 3883
72bf9492
DJ
3884 There are two complexities. One is DW_AT_specification; in this
3885 case "parent" means the parent of the target of the specification,
3886 instead of the direct parent of the DIE. The other is compilers
3887 which do not emit DW_TAG_namespace; in this case we try to guess
3888 the fully qualified name of structure types from their members'
3889 linkage names. This must be done using the DIE's children rather
3890 than the children of any DW_AT_specification target. We only need
3891 to do this for structures at the top level, i.e. if the target of
3892 any DW_AT_specification (if any; otherwise the DIE itself) does not
3893 have a parent. */
3894
3895/* Compute the scope prefix associated with PDI's parent, in
3896 compilation unit CU. The result will be allocated on CU's
3897 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3898 field. NULL is returned if no prefix is necessary. */
3899static char *
3900partial_die_parent_scope (struct partial_die_info *pdi,
3901 struct dwarf2_cu *cu)
3902{
3903 char *grandparent_scope;
3904 struct partial_die_info *parent, *real_pdi;
91c24f0a 3905
72bf9492
DJ
3906 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3907 then this means the parent of the specification DIE. */
3908
3909 real_pdi = pdi;
72bf9492 3910 while (real_pdi->has_specification)
10b3939b 3911 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3912
3913 parent = real_pdi->die_parent;
3914 if (parent == NULL)
3915 return NULL;
3916
3917 if (parent->scope_set)
3918 return parent->scope;
3919
3920 fixup_partial_die (parent, cu);
3921
10b3939b 3922 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3923
acebe513
UW
3924 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3925 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3926 Work around this problem here. */
3927 if (cu->language == language_cplus
6e70227d 3928 && parent->tag == DW_TAG_namespace
acebe513
UW
3929 && strcmp (parent->name, "::") == 0
3930 && grandparent_scope == NULL)
3931 {
3932 parent->scope = NULL;
3933 parent->scope_set = 1;
3934 return NULL;
3935 }
3936
9c6c53f7
SA
3937 if (pdi->tag == DW_TAG_enumerator)
3938 /* Enumerators should not get the name of the enumeration as a prefix. */
3939 parent->scope = grandparent_scope;
3940 else if (parent->tag == DW_TAG_namespace
f55ee35c 3941 || parent->tag == DW_TAG_module
72bf9492
DJ
3942 || parent->tag == DW_TAG_structure_type
3943 || parent->tag == DW_TAG_class_type
680b30c7 3944 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3945 || parent->tag == DW_TAG_union_type
3946 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3947 {
3948 if (grandparent_scope == NULL)
3949 parent->scope = parent->name;
3950 else
3e43a32a
MS
3951 parent->scope = typename_concat (&cu->comp_unit_obstack,
3952 grandparent_scope,
f55ee35c 3953 parent->name, 0, cu);
72bf9492 3954 }
72bf9492
DJ
3955 else
3956 {
3957 /* FIXME drow/2004-04-01: What should we be doing with
3958 function-local names? For partial symbols, we should probably be
3959 ignoring them. */
3960 complaint (&symfile_complaints,
e2e0b3e5 3961 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3962 parent->tag, pdi->offset);
3963 parent->scope = grandparent_scope;
c906108c
SS
3964 }
3965
72bf9492
DJ
3966 parent->scope_set = 1;
3967 return parent->scope;
3968}
3969
3970/* Return the fully scoped name associated with PDI, from compilation unit
3971 CU. The result will be allocated with malloc. */
3972static char *
3973partial_die_full_name (struct partial_die_info *pdi,
3974 struct dwarf2_cu *cu)
3975{
3976 char *parent_scope;
3977
98bfdba5
PA
3978 /* If this is a template instantiation, we can not work out the
3979 template arguments from partial DIEs. So, unfortunately, we have
3980 to go through the full DIEs. At least any work we do building
3981 types here will be reused if full symbols are loaded later. */
3982 if (pdi->has_template_arguments)
3983 {
3984 fixup_partial_die (pdi, cu);
3985
3986 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3987 {
3988 struct die_info *die;
3989 struct attribute attr;
3990 struct dwarf2_cu *ref_cu = cu;
3991
3992 attr.name = 0;
3993 attr.form = DW_FORM_ref_addr;
3994 attr.u.addr = pdi->offset;
3995 die = follow_die_ref (NULL, &attr, &ref_cu);
3996
3997 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3998 }
3999 }
4000
72bf9492
DJ
4001 parent_scope = partial_die_parent_scope (pdi, cu);
4002 if (parent_scope == NULL)
4003 return NULL;
4004 else
f55ee35c 4005 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4006}
4007
4008static void
72bf9492 4009add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4010{
e7c27a73 4011 struct objfile *objfile = cu->objfile;
c906108c 4012 CORE_ADDR addr = 0;
decbce07 4013 char *actual_name = NULL;
5c4e30ca 4014 const struct partial_symbol *psym = NULL;
e142c38c 4015 CORE_ADDR baseaddr;
72bf9492 4016 int built_actual_name = 0;
e142c38c
DJ
4017
4018 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4019
94af9270
KS
4020 actual_name = partial_die_full_name (pdi, cu);
4021 if (actual_name)
4022 built_actual_name = 1;
63d06c5c 4023
72bf9492
DJ
4024 if (actual_name == NULL)
4025 actual_name = pdi->name;
4026
c906108c
SS
4027 switch (pdi->tag)
4028 {
4029 case DW_TAG_subprogram:
2cfa0c8d 4030 if (pdi->is_external || cu->language == language_ada)
c906108c 4031 {
2cfa0c8d
JB
4032 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4033 of the global scope. But in Ada, we want to be able to access
4034 nested procedures globally. So all Ada subprograms are stored
4035 in the global scope. */
f47fb265 4036 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4037 mst_text, objfile); */
f47fb265
MS
4038 add_psymbol_to_list (actual_name, strlen (actual_name),
4039 built_actual_name,
4040 VAR_DOMAIN, LOC_BLOCK,
4041 &objfile->global_psymbols,
4042 0, pdi->lowpc + baseaddr,
4043 cu->language, objfile);
c906108c
SS
4044 }
4045 else
4046 {
f47fb265 4047 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4048 mst_file_text, objfile); */
f47fb265
MS
4049 add_psymbol_to_list (actual_name, strlen (actual_name),
4050 built_actual_name,
4051 VAR_DOMAIN, LOC_BLOCK,
4052 &objfile->static_psymbols,
4053 0, pdi->lowpc + baseaddr,
4054 cu->language, objfile);
c906108c
SS
4055 }
4056 break;
72929c62
JB
4057 case DW_TAG_constant:
4058 {
4059 struct psymbol_allocation_list *list;
4060
4061 if (pdi->is_external)
4062 list = &objfile->global_psymbols;
4063 else
4064 list = &objfile->static_psymbols;
f47fb265
MS
4065 add_psymbol_to_list (actual_name, strlen (actual_name),
4066 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4067 list, 0, 0, cu->language, objfile);
72929c62
JB
4068 }
4069 break;
c906108c 4070 case DW_TAG_variable:
caac4577
JG
4071 if (pdi->locdesc)
4072 addr = decode_locdesc (pdi->locdesc, cu);
4073
4074 if (pdi->locdesc
4075 && addr == 0
4076 && !dwarf2_per_objfile->has_section_at_zero)
4077 {
4078 /* A global or static variable may also have been stripped
4079 out by the linker if unused, in which case its address
4080 will be nullified; do not add such variables into partial
4081 symbol table then. */
4082 }
4083 else if (pdi->is_external)
c906108c
SS
4084 {
4085 /* Global Variable.
4086 Don't enter into the minimal symbol tables as there is
4087 a minimal symbol table entry from the ELF symbols already.
4088 Enter into partial symbol table if it has a location
4089 descriptor or a type.
4090 If the location descriptor is missing, new_symbol will create
4091 a LOC_UNRESOLVED symbol, the address of the variable will then
4092 be determined from the minimal symbol table whenever the variable
4093 is referenced.
4094 The address for the partial symbol table entry is not
4095 used by GDB, but it comes in handy for debugging partial symbol
4096 table building. */
4097
c906108c 4098 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4099 add_psymbol_to_list (actual_name, strlen (actual_name),
4100 built_actual_name,
4101 VAR_DOMAIN, LOC_STATIC,
4102 &objfile->global_psymbols,
4103 0, addr + baseaddr,
4104 cu->language, objfile);
c906108c
SS
4105 }
4106 else
4107 {
0963b4bd 4108 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4109 if (pdi->locdesc == NULL)
decbce07
MS
4110 {
4111 if (built_actual_name)
4112 xfree (actual_name);
4113 return;
4114 }
f47fb265 4115 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4116 mst_file_data, objfile); */
f47fb265
MS
4117 add_psymbol_to_list (actual_name, strlen (actual_name),
4118 built_actual_name,
4119 VAR_DOMAIN, LOC_STATIC,
4120 &objfile->static_psymbols,
4121 0, addr + baseaddr,
4122 cu->language, objfile);
c906108c
SS
4123 }
4124 break;
4125 case DW_TAG_typedef:
4126 case DW_TAG_base_type:
a02abb62 4127 case DW_TAG_subrange_type:
38d518c9 4128 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4129 built_actual_name,
176620f1 4130 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4131 &objfile->static_psymbols,
e142c38c 4132 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4133 break;
72bf9492
DJ
4134 case DW_TAG_namespace:
4135 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4136 built_actual_name,
72bf9492
DJ
4137 VAR_DOMAIN, LOC_TYPEDEF,
4138 &objfile->global_psymbols,
4139 0, (CORE_ADDR) 0, cu->language, objfile);
4140 break;
c906108c 4141 case DW_TAG_class_type:
680b30c7 4142 case DW_TAG_interface_type:
c906108c
SS
4143 case DW_TAG_structure_type:
4144 case DW_TAG_union_type:
4145 case DW_TAG_enumeration_type:
fa4028e9
JB
4146 /* Skip external references. The DWARF standard says in the section
4147 about "Structure, Union, and Class Type Entries": "An incomplete
4148 structure, union or class type is represented by a structure,
4149 union or class entry that does not have a byte size attribute
4150 and that has a DW_AT_declaration attribute." */
4151 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4152 {
4153 if (built_actual_name)
4154 xfree (actual_name);
4155 return;
4156 }
fa4028e9 4157
63d06c5c
DC
4158 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4159 static vs. global. */
38d518c9 4160 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4161 built_actual_name,
176620f1 4162 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4163 (cu->language == language_cplus
4164 || cu->language == language_java)
63d06c5c
DC
4165 ? &objfile->global_psymbols
4166 : &objfile->static_psymbols,
e142c38c 4167 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4168
c906108c
SS
4169 break;
4170 case DW_TAG_enumerator:
38d518c9 4171 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4172 built_actual_name,
176620f1 4173 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4174 (cu->language == language_cplus
4175 || cu->language == language_java)
f6fe98ef
DJ
4176 ? &objfile->global_psymbols
4177 : &objfile->static_psymbols,
e142c38c 4178 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4179 break;
4180 default:
4181 break;
4182 }
5c4e30ca 4183
72bf9492
DJ
4184 if (built_actual_name)
4185 xfree (actual_name);
c906108c
SS
4186}
4187
5c4e30ca
DC
4188/* Read a partial die corresponding to a namespace; also, add a symbol
4189 corresponding to that namespace to the symbol table. NAMESPACE is
4190 the name of the enclosing namespace. */
91c24f0a 4191
72bf9492
DJ
4192static void
4193add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4195 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4196{
72bf9492 4197 /* Add a symbol for the namespace. */
e7c27a73 4198
72bf9492 4199 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4200
4201 /* Now scan partial symbols in that namespace. */
4202
91c24f0a 4203 if (pdi->has_children)
5734ee8b 4204 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4205}
4206
5d7cb8df
JK
4207/* Read a partial die corresponding to a Fortran module. */
4208
4209static void
4210add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4211 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4212{
f55ee35c 4213 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4214
4215 if (pdi->has_children)
4216 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4217}
4218
bc30ff58
JB
4219/* Read a partial die corresponding to a subprogram and create a partial
4220 symbol for that subprogram. When the CU language allows it, this
4221 routine also defines a partial symbol for each nested subprogram
4222 that this subprogram contains.
6e70227d 4223
bc30ff58
JB
4224 DIE my also be a lexical block, in which case we simply search
4225 recursively for suprograms defined inside that lexical block.
4226 Again, this is only performed when the CU language allows this
4227 type of definitions. */
4228
4229static void
4230add_partial_subprogram (struct partial_die_info *pdi,
4231 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4232 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4233{
4234 if (pdi->tag == DW_TAG_subprogram)
4235 {
4236 if (pdi->has_pc_info)
4237 {
4238 if (pdi->lowpc < *lowpc)
4239 *lowpc = pdi->lowpc;
4240 if (pdi->highpc > *highpc)
4241 *highpc = pdi->highpc;
5734ee8b
DJ
4242 if (need_pc)
4243 {
4244 CORE_ADDR baseaddr;
4245 struct objfile *objfile = cu->objfile;
4246
4247 baseaddr = ANOFFSET (objfile->section_offsets,
4248 SECT_OFF_TEXT (objfile));
4249 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4250 pdi->lowpc + baseaddr,
4251 pdi->highpc - 1 + baseaddr,
9291a0cd 4252 cu->per_cu->v.psymtab);
5734ee8b 4253 }
bc30ff58 4254 if (!pdi->is_declaration)
e8d05480
JB
4255 /* Ignore subprogram DIEs that do not have a name, they are
4256 illegal. Do not emit a complaint at this point, we will
4257 do so when we convert this psymtab into a symtab. */
4258 if (pdi->name)
4259 add_partial_symbol (pdi, cu);
bc30ff58
JB
4260 }
4261 }
6e70227d 4262
bc30ff58
JB
4263 if (! pdi->has_children)
4264 return;
4265
4266 if (cu->language == language_ada)
4267 {
4268 pdi = pdi->die_child;
4269 while (pdi != NULL)
4270 {
4271 fixup_partial_die (pdi, cu);
4272 if (pdi->tag == DW_TAG_subprogram
4273 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4274 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4275 pdi = pdi->die_sibling;
4276 }
4277 }
4278}
4279
91c24f0a
DC
4280/* Read a partial die corresponding to an enumeration type. */
4281
72bf9492
DJ
4282static void
4283add_partial_enumeration (struct partial_die_info *enum_pdi,
4284 struct dwarf2_cu *cu)
91c24f0a 4285{
72bf9492 4286 struct partial_die_info *pdi;
91c24f0a
DC
4287
4288 if (enum_pdi->name != NULL)
72bf9492
DJ
4289 add_partial_symbol (enum_pdi, cu);
4290
4291 pdi = enum_pdi->die_child;
4292 while (pdi)
91c24f0a 4293 {
72bf9492 4294 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4295 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4296 else
72bf9492
DJ
4297 add_partial_symbol (pdi, cu);
4298 pdi = pdi->die_sibling;
91c24f0a 4299 }
91c24f0a
DC
4300}
4301
6caca83c
CC
4302/* Return the initial uleb128 in the die at INFO_PTR. */
4303
4304static unsigned int
4305peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4306{
4307 unsigned int bytes_read;
4308
4309 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4310}
4311
4bb7a0a7
DJ
4312/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4313 Return the corresponding abbrev, or NULL if the number is zero (indicating
4314 an empty DIE). In either case *BYTES_READ will be set to the length of
4315 the initial number. */
4316
4317static struct abbrev_info *
fe1b8b76 4318peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4319 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4320{
4321 bfd *abfd = cu->objfile->obfd;
4322 unsigned int abbrev_number;
4323 struct abbrev_info *abbrev;
4324
4325 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4326
4327 if (abbrev_number == 0)
4328 return NULL;
4329
4330 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4331 if (!abbrev)
4332 {
3e43a32a
MS
4333 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4334 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4335 }
4336
4337 return abbrev;
4338}
4339
93311388
DE
4340/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4341 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4342 DIE. Any children of the skipped DIEs will also be skipped. */
4343
fe1b8b76 4344static gdb_byte *
93311388 4345skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4346{
4347 struct abbrev_info *abbrev;
4348 unsigned int bytes_read;
4349
4350 while (1)
4351 {
4352 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4353 if (abbrev == NULL)
4354 return info_ptr + bytes_read;
4355 else
93311388 4356 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4357 }
4358}
4359
93311388
DE
4360/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4361 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4362 abbrev corresponding to that skipped uleb128 should be passed in
4363 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4364 children. */
4365
fe1b8b76 4366static gdb_byte *
93311388
DE
4367skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4368 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4369{
4370 unsigned int bytes_read;
4371 struct attribute attr;
4372 bfd *abfd = cu->objfile->obfd;
4373 unsigned int form, i;
4374
4375 for (i = 0; i < abbrev->num_attrs; i++)
4376 {
4377 /* The only abbrev we care about is DW_AT_sibling. */
4378 if (abbrev->attrs[i].name == DW_AT_sibling)
4379 {
4380 read_attribute (&attr, &abbrev->attrs[i],
4381 abfd, info_ptr, cu);
4382 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4383 complaint (&symfile_complaints,
4384 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4385 else
93311388 4386 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4387 }
4388
4389 /* If it isn't DW_AT_sibling, skip this attribute. */
4390 form = abbrev->attrs[i].form;
4391 skip_attribute:
4392 switch (form)
4393 {
4bb7a0a7 4394 case DW_FORM_ref_addr:
ae411497
TT
4395 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4396 and later it is offset sized. */
4397 if (cu->header.version == 2)
4398 info_ptr += cu->header.addr_size;
4399 else
4400 info_ptr += cu->header.offset_size;
4401 break;
4402 case DW_FORM_addr:
4bb7a0a7
DJ
4403 info_ptr += cu->header.addr_size;
4404 break;
4405 case DW_FORM_data1:
4406 case DW_FORM_ref1:
4407 case DW_FORM_flag:
4408 info_ptr += 1;
4409 break;
2dc7f7b3
TT
4410 case DW_FORM_flag_present:
4411 break;
4bb7a0a7
DJ
4412 case DW_FORM_data2:
4413 case DW_FORM_ref2:
4414 info_ptr += 2;
4415 break;
4416 case DW_FORM_data4:
4417 case DW_FORM_ref4:
4418 info_ptr += 4;
4419 break;
4420 case DW_FORM_data8:
4421 case DW_FORM_ref8:
55f1336d 4422 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4423 info_ptr += 8;
4424 break;
4425 case DW_FORM_string:
9b1c24c8 4426 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4427 info_ptr += bytes_read;
4428 break;
2dc7f7b3 4429 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4430 case DW_FORM_strp:
4431 info_ptr += cu->header.offset_size;
4432 break;
2dc7f7b3 4433 case DW_FORM_exprloc:
4bb7a0a7
DJ
4434 case DW_FORM_block:
4435 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4436 info_ptr += bytes_read;
4437 break;
4438 case DW_FORM_block1:
4439 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4440 break;
4441 case DW_FORM_block2:
4442 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4443 break;
4444 case DW_FORM_block4:
4445 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4446 break;
4447 case DW_FORM_sdata:
4448 case DW_FORM_udata:
4449 case DW_FORM_ref_udata:
4450 info_ptr = skip_leb128 (abfd, info_ptr);
4451 break;
4452 case DW_FORM_indirect:
4453 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4454 info_ptr += bytes_read;
4455 /* We need to continue parsing from here, so just go back to
4456 the top. */
4457 goto skip_attribute;
4458
4459 default:
3e43a32a
MS
4460 error (_("Dwarf Error: Cannot handle %s "
4461 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4462 dwarf_form_name (form),
4463 bfd_get_filename (abfd));
4464 }
4465 }
4466
4467 if (abbrev->has_children)
93311388 4468 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4469 else
4470 return info_ptr;
4471}
4472
93311388
DE
4473/* Locate ORIG_PDI's sibling.
4474 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4475 in BUFFER. */
91c24f0a 4476
fe1b8b76 4477static gdb_byte *
93311388
DE
4478locate_pdi_sibling (struct partial_die_info *orig_pdi,
4479 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4480 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4481{
4482 /* Do we know the sibling already? */
72bf9492 4483
91c24f0a
DC
4484 if (orig_pdi->sibling)
4485 return orig_pdi->sibling;
4486
4487 /* Are there any children to deal with? */
4488
4489 if (!orig_pdi->has_children)
4490 return info_ptr;
4491
4bb7a0a7 4492 /* Skip the children the long way. */
91c24f0a 4493
93311388 4494 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4495}
4496
c906108c
SS
4497/* Expand this partial symbol table into a full symbol table. */
4498
4499static void
fba45db2 4500dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4501{
c906108c
SS
4502 if (pst != NULL)
4503 {
4504 if (pst->readin)
4505 {
3e43a32a
MS
4506 warning (_("bug: psymtab for %s is already read in."),
4507 pst->filename);
c906108c
SS
4508 }
4509 else
4510 {
4511 if (info_verbose)
4512 {
3e43a32a
MS
4513 printf_filtered (_("Reading in symbols for %s..."),
4514 pst->filename);
c906108c
SS
4515 gdb_flush (gdb_stdout);
4516 }
4517
10b3939b
DJ
4518 /* Restore our global data. */
4519 dwarf2_per_objfile = objfile_data (pst->objfile,
4520 dwarf2_objfile_data_key);
4521
b2ab525c
KB
4522 /* If this psymtab is constructed from a debug-only objfile, the
4523 has_section_at_zero flag will not necessarily be correct. We
4524 can get the correct value for this flag by looking at the data
4525 associated with the (presumably stripped) associated objfile. */
4526 if (pst->objfile->separate_debug_objfile_backlink)
4527 {
4528 struct dwarf2_per_objfile *dpo_backlink
4529 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4530 dwarf2_objfile_data_key);
9a619af0 4531
b2ab525c
KB
4532 dwarf2_per_objfile->has_section_at_zero
4533 = dpo_backlink->has_section_at_zero;
4534 }
4535
98bfdba5
PA
4536 dwarf2_per_objfile->reading_partial_symbols = 0;
4537
c906108c
SS
4538 psymtab_to_symtab_1 (pst);
4539
4540 /* Finish up the debug error message. */
4541 if (info_verbose)
a3f17187 4542 printf_filtered (_("done.\n"));
c906108c
SS
4543 }
4544 }
4545}
9cdd5dbd
DE
4546\f
4547/* Reading in full CUs. */
c906108c 4548
10b3939b
DJ
4549/* Add PER_CU to the queue. */
4550
4551static void
a0f42c21 4552queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4553{
4554 struct dwarf2_queue_item *item;
4555
4556 per_cu->queued = 1;
4557 item = xmalloc (sizeof (*item));
4558 item->per_cu = per_cu;
4559 item->next = NULL;
4560
4561 if (dwarf2_queue == NULL)
4562 dwarf2_queue = item;
4563 else
4564 dwarf2_queue_tail->next = item;
4565
4566 dwarf2_queue_tail = item;
4567}
4568
4569/* Process the queue. */
4570
4571static void
a0f42c21 4572process_queue (void)
10b3939b
DJ
4573{
4574 struct dwarf2_queue_item *item, *next_item;
4575
03dd20cc
DJ
4576 /* The queue starts out with one item, but following a DIE reference
4577 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4578 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4579 {
9291a0cd
TT
4580 if (dwarf2_per_objfile->using_index
4581 ? !item->per_cu->v.quick->symtab
4582 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4583 process_full_comp_unit (item->per_cu);
4584
4585 item->per_cu->queued = 0;
4586 next_item = item->next;
4587 xfree (item);
4588 }
4589
4590 dwarf2_queue_tail = NULL;
4591}
4592
4593/* Free all allocated queue entries. This function only releases anything if
4594 an error was thrown; if the queue was processed then it would have been
4595 freed as we went along. */
4596
4597static void
4598dwarf2_release_queue (void *dummy)
4599{
4600 struct dwarf2_queue_item *item, *last;
4601
4602 item = dwarf2_queue;
4603 while (item)
4604 {
4605 /* Anything still marked queued is likely to be in an
4606 inconsistent state, so discard it. */
4607 if (item->per_cu->queued)
4608 {
4609 if (item->per_cu->cu != NULL)
4610 free_one_cached_comp_unit (item->per_cu->cu);
4611 item->per_cu->queued = 0;
4612 }
4613
4614 last = item;
4615 item = item->next;
4616 xfree (last);
4617 }
4618
4619 dwarf2_queue = dwarf2_queue_tail = NULL;
4620}
4621
4622/* Read in full symbols for PST, and anything it depends on. */
4623
c906108c 4624static void
fba45db2 4625psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4626{
10b3939b 4627 struct dwarf2_per_cu_data *per_cu;
c906108c 4628 struct cleanup *back_to;
aaa75496
JB
4629 int i;
4630
4631 for (i = 0; i < pst->number_of_dependencies; i++)
4632 if (!pst->dependencies[i]->readin)
4633 {
4634 /* Inform about additional files that need to be read in. */
4635 if (info_verbose)
4636 {
a3f17187 4637 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4638 fputs_filtered (" ", gdb_stdout);
4639 wrap_here ("");
4640 fputs_filtered ("and ", gdb_stdout);
4641 wrap_here ("");
4642 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4643 wrap_here (""); /* Flush output. */
aaa75496
JB
4644 gdb_flush (gdb_stdout);
4645 }
4646 psymtab_to_symtab_1 (pst->dependencies[i]);
4647 }
4648
e38df1d0 4649 per_cu = pst->read_symtab_private;
10b3939b
DJ
4650
4651 if (per_cu == NULL)
aaa75496
JB
4652 {
4653 /* It's an include file, no symbols to read for it.
4654 Everything is in the parent symtab. */
4655 pst->readin = 1;
4656 return;
4657 }
c906108c 4658
a0f42c21 4659 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4660}
4661
93311388 4662/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4663
93311388 4664static void
a0f42c21 4665load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4666{
a0f42c21 4667 struct objfile *objfile = per_cu->objfile;
31ffec48 4668 bfd *abfd = objfile->obfd;
10b3939b 4669 struct dwarf2_cu *cu;
c764a876 4670 unsigned int offset;
93311388 4671 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4672 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4673 struct attribute *attr;
98bfdba5 4674 int read_cu = 0;
6502dd73 4675
b0df02fd 4676 gdb_assert (! per_cu->debug_types_section);
348e048f 4677
c906108c 4678 /* Set local variables from the partial symbol table info. */
10b3939b 4679 offset = per_cu->offset;
6502dd73 4680
be391dca 4681 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4682 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4683 beg_of_comp_unit = info_ptr;
63d06c5c 4684
98bfdba5
PA
4685 if (per_cu->cu == NULL)
4686 {
9816fde3 4687 cu = xmalloc (sizeof (*cu));
23745b47 4688 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4689
4690 read_cu = 1;
c906108c 4691
98bfdba5 4692 /* If an error occurs while loading, release our storage. */
68dc6402 4693 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4694
98bfdba5
PA
4695 /* Read in the comp_unit header. */
4696 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4697
6caca83c
CC
4698 /* Skip dummy compilation units. */
4699 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4700 + dwarf2_per_objfile->info.size)
4701 || peek_abbrev_code (abfd, info_ptr) == 0)
4702 {
4703 do_cleanups (free_cu_cleanup);
4704 return;
4705 }
4706
98bfdba5
PA
4707 /* Complete the cu_header. */
4708 cu->header.offset = offset;
4709 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4710
98bfdba5 4711 /* Read the abbrevs for this compilation unit. */
e5fe5e75 4712 dwarf2_read_abbrevs (cu);
98bfdba5 4713 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4714
98bfdba5
PA
4715 /* Link this CU into read_in_chain. */
4716 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4717 dwarf2_per_objfile->read_in_chain = per_cu;
4718 }
4719 else
4720 {
4721 cu = per_cu->cu;
4722 info_ptr += cu->header.first_die_offset;
4723 }
e142c38c 4724
93311388 4725 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4726
4727 /* We try not to read any attributes in this function, because not
9cdd5dbd 4728 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4729 table processing isn't initialized. But we have to set the CU language,
4730 or we won't be able to build types correctly. */
9816fde3 4731 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4732
a6c727b2
DJ
4733 /* Similarly, if we do not read the producer, we can not apply
4734 producer-specific interpretation. */
4735 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4736 if (attr)
4737 cu->producer = DW_STRING (attr);
4738
98bfdba5
PA
4739 if (read_cu)
4740 {
4741 do_cleanups (free_abbrevs_cleanup);
e142c38c 4742
98bfdba5
PA
4743 /* We've successfully allocated this compilation unit. Let our
4744 caller clean it up when finished with it. */
4745 discard_cleanups (free_cu_cleanup);
4746 }
10b3939b
DJ
4747}
4748
3da10d80
KS
4749/* Add a DIE to the delayed physname list. */
4750
4751static void
4752add_to_method_list (struct type *type, int fnfield_index, int index,
4753 const char *name, struct die_info *die,
4754 struct dwarf2_cu *cu)
4755{
4756 struct delayed_method_info mi;
4757 mi.type = type;
4758 mi.fnfield_index = fnfield_index;
4759 mi.index = index;
4760 mi.name = name;
4761 mi.die = die;
4762 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4763}
4764
4765/* A cleanup for freeing the delayed method list. */
4766
4767static void
4768free_delayed_list (void *ptr)
4769{
4770 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4771 if (cu->method_list != NULL)
4772 {
4773 VEC_free (delayed_method_info, cu->method_list);
4774 cu->method_list = NULL;
4775 }
4776}
4777
4778/* Compute the physnames of any methods on the CU's method list.
4779
4780 The computation of method physnames is delayed in order to avoid the
4781 (bad) condition that one of the method's formal parameters is of an as yet
4782 incomplete type. */
4783
4784static void
4785compute_delayed_physnames (struct dwarf2_cu *cu)
4786{
4787 int i;
4788 struct delayed_method_info *mi;
4789 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4790 {
1d06ead6 4791 const char *physname;
3da10d80
KS
4792 struct fn_fieldlist *fn_flp
4793 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4794 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4795 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4796 }
4797}
4798
9cdd5dbd 4799/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4800 already been loaded into memory. */
4801
4802static void
4803process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4804{
10b3939b 4805 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4806 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4807 CORE_ADDR lowpc, highpc;
4808 struct symtab *symtab;
3da10d80 4809 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4810 CORE_ADDR baseaddr;
4811
4812 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4813
10b3939b
DJ
4814 buildsym_init ();
4815 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4816 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4817
4818 cu->list_in_scope = &file_symbols;
c906108c
SS
4819
4820 /* Do line number decoding in read_file_scope () */
10b3939b 4821 process_die (cu->dies, cu);
c906108c 4822
3da10d80
KS
4823 /* Now that we have processed all the DIEs in the CU, all the types
4824 should be complete, and it should now be safe to compute all of the
4825 physnames. */
4826 compute_delayed_physnames (cu);
4827 do_cleanups (delayed_list_cleanup);
4828
fae299cd
DC
4829 /* Some compilers don't define a DW_AT_high_pc attribute for the
4830 compilation unit. If the DW_AT_high_pc is missing, synthesize
4831 it, by scanning the DIE's below the compilation unit. */
10b3939b 4832 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4833
613e1657 4834 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4835
8be455d7 4836 if (symtab != NULL)
c906108c 4837 {
df15bd07 4838 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4839
8be455d7
JK
4840 /* Set symtab language to language from DW_AT_language. If the
4841 compilation is from a C file generated by language preprocessors, do
4842 not set the language if it was already deduced by start_subfile. */
4843 if (!(cu->language == language_c && symtab->language != language_c))
4844 symtab->language = cu->language;
4845
4846 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4847 produce DW_AT_location with location lists but it can be possibly
4848 invalid without -fvar-tracking.
4849
4850 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4851 needed, it would be wrong due to missing DW_AT_producer there.
4852
4853 Still one can confuse GDB by using non-standard GCC compilation
4854 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4855 */
4632c0d0 4856 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4857 symtab->locations_valid = 1;
e0d00bc7
JK
4858
4859 if (gcc_4_minor >= 5)
4860 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4861
4862 symtab->call_site_htab = cu->call_site_htab;
c906108c 4863 }
9291a0cd
TT
4864
4865 if (dwarf2_per_objfile->using_index)
4866 per_cu->v.quick->symtab = symtab;
4867 else
4868 {
4869 struct partial_symtab *pst = per_cu->v.psymtab;
4870 pst->symtab = symtab;
4871 pst->readin = 1;
4872 }
c906108c
SS
4873
4874 do_cleanups (back_to);
4875}
4876
4877/* Process a die and its children. */
4878
4879static void
e7c27a73 4880process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4881{
4882 switch (die->tag)
4883 {
4884 case DW_TAG_padding:
4885 break;
4886 case DW_TAG_compile_unit:
e7c27a73 4887 read_file_scope (die, cu);
c906108c 4888 break;
348e048f
DE
4889 case DW_TAG_type_unit:
4890 read_type_unit_scope (die, cu);
4891 break;
c906108c 4892 case DW_TAG_subprogram:
c906108c 4893 case DW_TAG_inlined_subroutine:
edb3359d 4894 read_func_scope (die, cu);
c906108c
SS
4895 break;
4896 case DW_TAG_lexical_block:
14898363
L
4897 case DW_TAG_try_block:
4898 case DW_TAG_catch_block:
e7c27a73 4899 read_lexical_block_scope (die, cu);
c906108c 4900 break;
96408a79
SA
4901 case DW_TAG_GNU_call_site:
4902 read_call_site_scope (die, cu);
4903 break;
c906108c 4904 case DW_TAG_class_type:
680b30c7 4905 case DW_TAG_interface_type:
c906108c
SS
4906 case DW_TAG_structure_type:
4907 case DW_TAG_union_type:
134d01f1 4908 process_structure_scope (die, cu);
c906108c
SS
4909 break;
4910 case DW_TAG_enumeration_type:
134d01f1 4911 process_enumeration_scope (die, cu);
c906108c 4912 break;
134d01f1 4913
f792889a
DJ
4914 /* These dies have a type, but processing them does not create
4915 a symbol or recurse to process the children. Therefore we can
4916 read them on-demand through read_type_die. */
c906108c 4917 case DW_TAG_subroutine_type:
72019c9c 4918 case DW_TAG_set_type:
c906108c 4919 case DW_TAG_array_type:
c906108c 4920 case DW_TAG_pointer_type:
c906108c 4921 case DW_TAG_ptr_to_member_type:
c906108c 4922 case DW_TAG_reference_type:
c906108c 4923 case DW_TAG_string_type:
c906108c 4924 break;
134d01f1 4925
c906108c 4926 case DW_TAG_base_type:
a02abb62 4927 case DW_TAG_subrange_type:
cb249c71 4928 case DW_TAG_typedef:
134d01f1
DJ
4929 /* Add a typedef symbol for the type definition, if it has a
4930 DW_AT_name. */
f792889a 4931 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4932 break;
c906108c 4933 case DW_TAG_common_block:
e7c27a73 4934 read_common_block (die, cu);
c906108c
SS
4935 break;
4936 case DW_TAG_common_inclusion:
4937 break;
d9fa45fe 4938 case DW_TAG_namespace:
63d06c5c 4939 processing_has_namespace_info = 1;
e7c27a73 4940 read_namespace (die, cu);
d9fa45fe 4941 break;
5d7cb8df 4942 case DW_TAG_module:
f55ee35c 4943 processing_has_namespace_info = 1;
5d7cb8df
JK
4944 read_module (die, cu);
4945 break;
d9fa45fe
DC
4946 case DW_TAG_imported_declaration:
4947 case DW_TAG_imported_module:
63d06c5c 4948 processing_has_namespace_info = 1;
27aa8d6a
SW
4949 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4950 || cu->language != language_fortran))
4951 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4952 dwarf_tag_name (die->tag));
4953 read_import_statement (die, cu);
d9fa45fe 4954 break;
c906108c 4955 default:
e7c27a73 4956 new_symbol (die, NULL, cu);
c906108c
SS
4957 break;
4958 }
4959}
4960
94af9270
KS
4961/* A helper function for dwarf2_compute_name which determines whether DIE
4962 needs to have the name of the scope prepended to the name listed in the
4963 die. */
4964
4965static int
4966die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4967{
1c809c68
TT
4968 struct attribute *attr;
4969
94af9270
KS
4970 switch (die->tag)
4971 {
4972 case DW_TAG_namespace:
4973 case DW_TAG_typedef:
4974 case DW_TAG_class_type:
4975 case DW_TAG_interface_type:
4976 case DW_TAG_structure_type:
4977 case DW_TAG_union_type:
4978 case DW_TAG_enumeration_type:
4979 case DW_TAG_enumerator:
4980 case DW_TAG_subprogram:
4981 case DW_TAG_member:
4982 return 1;
4983
4984 case DW_TAG_variable:
c2b0a229 4985 case DW_TAG_constant:
94af9270
KS
4986 /* We only need to prefix "globally" visible variables. These include
4987 any variable marked with DW_AT_external or any variable that
4988 lives in a namespace. [Variables in anonymous namespaces
4989 require prefixing, but they are not DW_AT_external.] */
4990
4991 if (dwarf2_attr (die, DW_AT_specification, cu))
4992 {
4993 struct dwarf2_cu *spec_cu = cu;
9a619af0 4994
94af9270
KS
4995 return die_needs_namespace (die_specification (die, &spec_cu),
4996 spec_cu);
4997 }
4998
1c809c68 4999 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5000 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5001 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5002 return 0;
5003 /* A variable in a lexical block of some kind does not need a
5004 namespace, even though in C++ such variables may be external
5005 and have a mangled name. */
5006 if (die->parent->tag == DW_TAG_lexical_block
5007 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5008 || die->parent->tag == DW_TAG_catch_block
5009 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5010 return 0;
5011 return 1;
94af9270
KS
5012
5013 default:
5014 return 0;
5015 }
5016}
5017
98bfdba5
PA
5018/* Retrieve the last character from a mem_file. */
5019
5020static void
5021do_ui_file_peek_last (void *object, const char *buffer, long length)
5022{
5023 char *last_char_p = (char *) object;
5024
5025 if (length > 0)
5026 *last_char_p = buffer[length - 1];
5027}
5028
94af9270
KS
5029/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5030 compute the physname for the object, which include a method's
5031 formal parameters (C++/Java) and return type (Java).
5032
af6b7be1
JB
5033 For Ada, return the DIE's linkage name rather than the fully qualified
5034 name. PHYSNAME is ignored..
5035
94af9270
KS
5036 The result is allocated on the objfile_obstack and canonicalized. */
5037
5038static const char *
5039dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5040 int physname)
5041{
bb5ed363
DE
5042 struct objfile *objfile = cu->objfile;
5043
94af9270
KS
5044 if (name == NULL)
5045 name = dwarf2_name (die, cu);
5046
f55ee35c
JK
5047 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5048 compute it by typename_concat inside GDB. */
5049 if (cu->language == language_ada
5050 || (cu->language == language_fortran && physname))
5051 {
5052 /* For Ada unit, we prefer the linkage name over the name, as
5053 the former contains the exported name, which the user expects
5054 to be able to reference. Ideally, we want the user to be able
5055 to reference this entity using either natural or linkage name,
5056 but we haven't started looking at this enhancement yet. */
5057 struct attribute *attr;
5058
5059 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5060 if (attr == NULL)
5061 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5062 if (attr && DW_STRING (attr))
5063 return DW_STRING (attr);
5064 }
5065
94af9270
KS
5066 /* These are the only languages we know how to qualify names in. */
5067 if (name != NULL
f55ee35c
JK
5068 && (cu->language == language_cplus || cu->language == language_java
5069 || cu->language == language_fortran))
94af9270
KS
5070 {
5071 if (die_needs_namespace (die, cu))
5072 {
5073 long length;
5074 char *prefix;
5075 struct ui_file *buf;
5076
5077 prefix = determine_prefix (die, cu);
5078 buf = mem_fileopen ();
5079 if (*prefix != '\0')
5080 {
f55ee35c
JK
5081 char *prefixed_name = typename_concat (NULL, prefix, name,
5082 physname, cu);
9a619af0 5083
94af9270
KS
5084 fputs_unfiltered (prefixed_name, buf);
5085 xfree (prefixed_name);
5086 }
5087 else
62d5b8da 5088 fputs_unfiltered (name, buf);
94af9270 5089
98bfdba5
PA
5090 /* Template parameters may be specified in the DIE's DW_AT_name, or
5091 as children with DW_TAG_template_type_param or
5092 DW_TAG_value_type_param. If the latter, add them to the name
5093 here. If the name already has template parameters, then
5094 skip this step; some versions of GCC emit both, and
5095 it is more efficient to use the pre-computed name.
5096
5097 Something to keep in mind about this process: it is very
5098 unlikely, or in some cases downright impossible, to produce
5099 something that will match the mangled name of a function.
5100 If the definition of the function has the same debug info,
5101 we should be able to match up with it anyway. But fallbacks
5102 using the minimal symbol, for instance to find a method
5103 implemented in a stripped copy of libstdc++, will not work.
5104 If we do not have debug info for the definition, we will have to
5105 match them up some other way.
5106
5107 When we do name matching there is a related problem with function
5108 templates; two instantiated function templates are allowed to
5109 differ only by their return types, which we do not add here. */
5110
5111 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5112 {
5113 struct attribute *attr;
5114 struct die_info *child;
5115 int first = 1;
5116
5117 die->building_fullname = 1;
5118
5119 for (child = die->child; child != NULL; child = child->sibling)
5120 {
5121 struct type *type;
5122 long value;
5123 gdb_byte *bytes;
5124 struct dwarf2_locexpr_baton *baton;
5125 struct value *v;
5126
5127 if (child->tag != DW_TAG_template_type_param
5128 && child->tag != DW_TAG_template_value_param)
5129 continue;
5130
5131 if (first)
5132 {
5133 fputs_unfiltered ("<", buf);
5134 first = 0;
5135 }
5136 else
5137 fputs_unfiltered (", ", buf);
5138
5139 attr = dwarf2_attr (child, DW_AT_type, cu);
5140 if (attr == NULL)
5141 {
5142 complaint (&symfile_complaints,
5143 _("template parameter missing DW_AT_type"));
5144 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5145 continue;
5146 }
5147 type = die_type (child, cu);
5148
5149 if (child->tag == DW_TAG_template_type_param)
5150 {
5151 c_print_type (type, "", buf, -1, 0);
5152 continue;
5153 }
5154
5155 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5156 if (attr == NULL)
5157 {
5158 complaint (&symfile_complaints,
3e43a32a
MS
5159 _("template parameter missing "
5160 "DW_AT_const_value"));
98bfdba5
PA
5161 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5162 continue;
5163 }
5164
5165 dwarf2_const_value_attr (attr, type, name,
5166 &cu->comp_unit_obstack, cu,
5167 &value, &bytes, &baton);
5168
5169 if (TYPE_NOSIGN (type))
5170 /* GDB prints characters as NUMBER 'CHAR'. If that's
5171 changed, this can use value_print instead. */
5172 c_printchar (value, type, buf);
5173 else
5174 {
5175 struct value_print_options opts;
5176
5177 if (baton != NULL)
5178 v = dwarf2_evaluate_loc_desc (type, NULL,
5179 baton->data,
5180 baton->size,
5181 baton->per_cu);
5182 else if (bytes != NULL)
5183 {
5184 v = allocate_value (type);
5185 memcpy (value_contents_writeable (v), bytes,
5186 TYPE_LENGTH (type));
5187 }
5188 else
5189 v = value_from_longest (type, value);
5190
3e43a32a
MS
5191 /* Specify decimal so that we do not depend on
5192 the radix. */
98bfdba5
PA
5193 get_formatted_print_options (&opts, 'd');
5194 opts.raw = 1;
5195 value_print (v, buf, &opts);
5196 release_value (v);
5197 value_free (v);
5198 }
5199 }
5200
5201 die->building_fullname = 0;
5202
5203 if (!first)
5204 {
5205 /* Close the argument list, with a space if necessary
5206 (nested templates). */
5207 char last_char = '\0';
5208 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5209 if (last_char == '>')
5210 fputs_unfiltered (" >", buf);
5211 else
5212 fputs_unfiltered (">", buf);
5213 }
5214 }
5215
94af9270
KS
5216 /* For Java and C++ methods, append formal parameter type
5217 information, if PHYSNAME. */
6e70227d 5218
94af9270
KS
5219 if (physname && die->tag == DW_TAG_subprogram
5220 && (cu->language == language_cplus
5221 || cu->language == language_java))
5222 {
5223 struct type *type = read_type_die (die, cu);
5224
3167638f 5225 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5226
5227 if (cu->language == language_java)
5228 {
5229 /* For java, we must append the return type to method
0963b4bd 5230 names. */
94af9270
KS
5231 if (die->tag == DW_TAG_subprogram)
5232 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5233 0, 0);
5234 }
5235 else if (cu->language == language_cplus)
5236 {
60430eff
DJ
5237 /* Assume that an artificial first parameter is
5238 "this", but do not crash if it is not. RealView
5239 marks unnamed (and thus unused) parameters as
5240 artificial; there is no way to differentiate
5241 the two cases. */
94af9270
KS
5242 if (TYPE_NFIELDS (type) > 0
5243 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5244 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5245 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5246 0))))
94af9270
KS
5247 fputs_unfiltered (" const", buf);
5248 }
5249 }
5250
bb5ed363 5251 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5252 &length);
5253 ui_file_delete (buf);
5254
5255 if (cu->language == language_cplus)
5256 {
5257 char *cname
5258 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5259 &objfile->objfile_obstack);
9a619af0 5260
94af9270
KS
5261 if (cname != NULL)
5262 name = cname;
5263 }
5264 }
5265 }
5266
5267 return name;
5268}
5269
0114d602
DJ
5270/* Return the fully qualified name of DIE, based on its DW_AT_name.
5271 If scope qualifiers are appropriate they will be added. The result
5272 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5273 not have a name. NAME may either be from a previous call to
5274 dwarf2_name or NULL.
5275
0963b4bd 5276 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5277
5278static const char *
94af9270 5279dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5280{
94af9270
KS
5281 return dwarf2_compute_name (name, die, cu, 0);
5282}
0114d602 5283
94af9270
KS
5284/* Construct a physname for the given DIE in CU. NAME may either be
5285 from a previous call to dwarf2_name or NULL. The result will be
5286 allocated on the objfile_objstack or NULL if the DIE does not have a
5287 name.
0114d602 5288
94af9270 5289 The output string will be canonicalized (if C++/Java). */
0114d602 5290
94af9270
KS
5291static const char *
5292dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5293{
bb5ed363 5294 struct objfile *objfile = cu->objfile;
900e11f9
JK
5295 struct attribute *attr;
5296 const char *retval, *mangled = NULL, *canon = NULL;
5297 struct cleanup *back_to;
5298 int need_copy = 1;
5299
5300 /* In this case dwarf2_compute_name is just a shortcut not building anything
5301 on its own. */
5302 if (!die_needs_namespace (die, cu))
5303 return dwarf2_compute_name (name, die, cu, 1);
5304
5305 back_to = make_cleanup (null_cleanup, NULL);
5306
5307 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5308 if (!attr)
5309 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5310
5311 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5312 has computed. */
5313 if (attr && DW_STRING (attr))
5314 {
5315 char *demangled;
5316
5317 mangled = DW_STRING (attr);
5318
5319 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5320 type. It is easier for GDB users to search for such functions as
5321 `name(params)' than `long name(params)'. In such case the minimal
5322 symbol names do not match the full symbol names but for template
5323 functions there is never a need to look up their definition from their
5324 declaration so the only disadvantage remains the minimal symbol
5325 variant `long name(params)' does not have the proper inferior type.
5326 */
5327
5328 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5329 | (cu->language == language_java
5330 ? DMGL_JAVA | DMGL_RET_POSTFIX
5331 : DMGL_RET_DROP)));
5332 if (demangled)
5333 {
5334 make_cleanup (xfree, demangled);
5335 canon = demangled;
5336 }
5337 else
5338 {
5339 canon = mangled;
5340 need_copy = 0;
5341 }
5342 }
5343
5344 if (canon == NULL || check_physname)
5345 {
5346 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5347
5348 if (canon != NULL && strcmp (physname, canon) != 0)
5349 {
5350 /* It may not mean a bug in GDB. The compiler could also
5351 compute DW_AT_linkage_name incorrectly. But in such case
5352 GDB would need to be bug-to-bug compatible. */
5353
5354 complaint (&symfile_complaints,
5355 _("Computed physname <%s> does not match demangled <%s> "
5356 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
bb5ed363 5357 physname, canon, mangled, die->offset, objfile->name);
900e11f9
JK
5358
5359 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5360 is available here - over computed PHYSNAME. It is safer
5361 against both buggy GDB and buggy compilers. */
5362
5363 retval = canon;
5364 }
5365 else
5366 {
5367 retval = physname;
5368 need_copy = 0;
5369 }
5370 }
5371 else
5372 retval = canon;
5373
5374 if (need_copy)
5375 retval = obsavestring (retval, strlen (retval),
bb5ed363 5376 &objfile->objfile_obstack);
900e11f9
JK
5377
5378 do_cleanups (back_to);
5379 return retval;
0114d602
DJ
5380}
5381
27aa8d6a
SW
5382/* Read the import statement specified by the given die and record it. */
5383
5384static void
5385read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5386{
bb5ed363 5387 struct objfile *objfile = cu->objfile;
27aa8d6a 5388 struct attribute *import_attr;
32019081 5389 struct die_info *imported_die, *child_die;
de4affc9 5390 struct dwarf2_cu *imported_cu;
27aa8d6a 5391 const char *imported_name;
794684b6 5392 const char *imported_name_prefix;
13387711
SW
5393 const char *canonical_name;
5394 const char *import_alias;
5395 const char *imported_declaration = NULL;
794684b6 5396 const char *import_prefix;
32019081
JK
5397 VEC (const_char_ptr) *excludes = NULL;
5398 struct cleanup *cleanups;
13387711
SW
5399
5400 char *temp;
27aa8d6a
SW
5401
5402 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5403 if (import_attr == NULL)
5404 {
5405 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5406 dwarf_tag_name (die->tag));
5407 return;
5408 }
5409
de4affc9
CC
5410 imported_cu = cu;
5411 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5412 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5413 if (imported_name == NULL)
5414 {
5415 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5416
5417 The import in the following code:
5418 namespace A
5419 {
5420 typedef int B;
5421 }
5422
5423 int main ()
5424 {
5425 using A::B;
5426 B b;
5427 return b;
5428 }
5429
5430 ...
5431 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5432 <52> DW_AT_decl_file : 1
5433 <53> DW_AT_decl_line : 6
5434 <54> DW_AT_import : <0x75>
5435 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5436 <59> DW_AT_name : B
5437 <5b> DW_AT_decl_file : 1
5438 <5c> DW_AT_decl_line : 2
5439 <5d> DW_AT_type : <0x6e>
5440 ...
5441 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5442 <76> DW_AT_byte_size : 4
5443 <77> DW_AT_encoding : 5 (signed)
5444
5445 imports the wrong die ( 0x75 instead of 0x58 ).
5446 This case will be ignored until the gcc bug is fixed. */
5447 return;
5448 }
5449
82856980
SW
5450 /* Figure out the local name after import. */
5451 import_alias = dwarf2_name (die, cu);
27aa8d6a 5452
794684b6
SW
5453 /* Figure out where the statement is being imported to. */
5454 import_prefix = determine_prefix (die, cu);
5455
5456 /* Figure out what the scope of the imported die is and prepend it
5457 to the name of the imported die. */
de4affc9 5458 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5459
f55ee35c
JK
5460 if (imported_die->tag != DW_TAG_namespace
5461 && imported_die->tag != DW_TAG_module)
794684b6 5462 {
13387711
SW
5463 imported_declaration = imported_name;
5464 canonical_name = imported_name_prefix;
794684b6 5465 }
13387711 5466 else if (strlen (imported_name_prefix) > 0)
794684b6 5467 {
13387711
SW
5468 temp = alloca (strlen (imported_name_prefix)
5469 + 2 + strlen (imported_name) + 1);
5470 strcpy (temp, imported_name_prefix);
5471 strcat (temp, "::");
5472 strcat (temp, imported_name);
5473 canonical_name = temp;
794684b6 5474 }
13387711
SW
5475 else
5476 canonical_name = imported_name;
794684b6 5477
32019081
JK
5478 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5479
5480 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5481 for (child_die = die->child; child_die && child_die->tag;
5482 child_die = sibling_die (child_die))
5483 {
5484 /* DWARF-4: A Fortran use statement with a “rename list” may be
5485 represented by an imported module entry with an import attribute
5486 referring to the module and owned entries corresponding to those
5487 entities that are renamed as part of being imported. */
5488
5489 if (child_die->tag != DW_TAG_imported_declaration)
5490 {
5491 complaint (&symfile_complaints,
5492 _("child DW_TAG_imported_declaration expected "
5493 "- DIE at 0x%x [in module %s]"),
bb5ed363 5494 child_die->offset, objfile->name);
32019081
JK
5495 continue;
5496 }
5497
5498 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5499 if (import_attr == NULL)
5500 {
5501 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5502 dwarf_tag_name (child_die->tag));
5503 continue;
5504 }
5505
5506 imported_cu = cu;
5507 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5508 &imported_cu);
5509 imported_name = dwarf2_name (imported_die, imported_cu);
5510 if (imported_name == NULL)
5511 {
5512 complaint (&symfile_complaints,
5513 _("child DW_TAG_imported_declaration has unknown "
5514 "imported name - DIE at 0x%x [in module %s]"),
bb5ed363 5515 child_die->offset, objfile->name);
32019081
JK
5516 continue;
5517 }
5518
5519 VEC_safe_push (const_char_ptr, excludes, imported_name);
5520
5521 process_die (child_die, cu);
5522 }
5523
c0cc3a76
SW
5524 cp_add_using_directive (import_prefix,
5525 canonical_name,
5526 import_alias,
13387711 5527 imported_declaration,
32019081 5528 excludes,
bb5ed363 5529 &objfile->objfile_obstack);
32019081
JK
5530
5531 do_cleanups (cleanups);
27aa8d6a
SW
5532}
5533
ae2de4f8
DE
5534/* Cleanup function for read_file_scope. */
5535
cb1df416
DJ
5536static void
5537free_cu_line_header (void *arg)
5538{
5539 struct dwarf2_cu *cu = arg;
5540
5541 free_line_header (cu->line_header);
5542 cu->line_header = NULL;
5543}
5544
9291a0cd
TT
5545static void
5546find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5547 char **name, char **comp_dir)
5548{
5549 struct attribute *attr;
5550
5551 *name = NULL;
5552 *comp_dir = NULL;
5553
5554 /* Find the filename. Do not use dwarf2_name here, since the filename
5555 is not a source language identifier. */
5556 attr = dwarf2_attr (die, DW_AT_name, cu);
5557 if (attr)
5558 {
5559 *name = DW_STRING (attr);
5560 }
5561
5562 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5563 if (attr)
5564 *comp_dir = DW_STRING (attr);
5565 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5566 {
5567 *comp_dir = ldirname (*name);
5568 if (*comp_dir != NULL)
5569 make_cleanup (xfree, *comp_dir);
5570 }
5571 if (*comp_dir != NULL)
5572 {
5573 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5574 directory, get rid of it. */
5575 char *cp = strchr (*comp_dir, ':');
5576
5577 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5578 *comp_dir = cp + 1;
5579 }
5580
5581 if (*name == NULL)
5582 *name = "<unknown>";
5583}
5584
2ab95328
TT
5585/* Handle DW_AT_stmt_list for a compilation unit. */
5586
5587static void
5588handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5589 const char *comp_dir)
5590{
5591 struct attribute *attr;
5592 struct objfile *objfile = cu->objfile;
5593 bfd *abfd = objfile->obfd;
5594
5595 /* Decode line number information if present. We do this before
5596 processing child DIEs, so that the line header table is available
5597 for DW_AT_decl_file. */
5598 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5599 if (attr)
5600 {
5601 unsigned int line_offset = DW_UNSND (attr);
5602 struct line_header *line_header
5603 = dwarf_decode_line_header (line_offset, abfd, cu);
5604
5605 if (line_header)
5606 {
5607 cu->line_header = line_header;
5608 make_cleanup (free_cu_line_header, cu);
5609 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5610 }
5611 }
5612}
5613
ae2de4f8
DE
5614/* Process DW_TAG_compile_unit. */
5615
c906108c 5616static void
e7c27a73 5617read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5618{
e7c27a73 5619 struct objfile *objfile = cu->objfile;
debd256d 5620 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5621 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5622 CORE_ADDR highpc = ((CORE_ADDR) 0);
5623 struct attribute *attr;
e1024ff1 5624 char *name = NULL;
c906108c
SS
5625 char *comp_dir = NULL;
5626 struct die_info *child_die;
5627 bfd *abfd = objfile->obfd;
e142c38c 5628 CORE_ADDR baseaddr;
6e70227d 5629
e142c38c 5630 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5631
fae299cd 5632 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5633
5634 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5635 from finish_block. */
2acceee2 5636 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5637 lowpc = highpc;
5638 lowpc += baseaddr;
5639 highpc += baseaddr;
5640
9291a0cd 5641 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5642
e142c38c 5643 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5644 if (attr)
5645 {
e142c38c 5646 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5647 }
5648
b0f35d58 5649 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5650 if (attr)
b0f35d58 5651 cu->producer = DW_STRING (attr);
303b6f5d 5652
f4b8a18d
KW
5653 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5654 standardised yet. As a workaround for the language detection we fall
5655 back to the DW_AT_producer string. */
5656 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5657 cu->language = language_opencl;
5658
0963b4bd 5659 /* We assume that we're processing GCC output. */
c906108c 5660 processing_gcc_compilation = 2;
c906108c 5661
df8a16a1
DJ
5662 processing_has_namespace_info = 0;
5663
c906108c
SS
5664 start_symtab (name, comp_dir, lowpc);
5665 record_debugformat ("DWARF 2");
303b6f5d 5666 record_producer (cu->producer);
c906108c 5667
2ab95328 5668 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5669
cb1df416
DJ
5670 /* Process all dies in compilation unit. */
5671 if (die->child != NULL)
5672 {
5673 child_die = die->child;
5674 while (child_die && child_die->tag)
5675 {
5676 process_die (child_die, cu);
5677 child_die = sibling_die (child_die);
5678 }
5679 }
5680
2e276125
JB
5681 /* Decode macro information, if present. Dwarf 2 macro information
5682 refers to information in the line number info statement program
5683 header, so we can only read it if we've read the header
5684 successfully. */
cf2c3c16 5685 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5686 if (attr && cu->line_header)
2e276125 5687 {
cf2c3c16
TT
5688 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5689 complaint (&symfile_complaints,
5690 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5691
5692 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5693 comp_dir, abfd, cu,
5694 &dwarf2_per_objfile->macro, 1);
5695 }
5696 else
5697 {
5698 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5699 if (attr && cu->line_header)
5700 {
5701 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5702
cf2c3c16
TT
5703 dwarf_decode_macros (cu->line_header, macro_offset,
5704 comp_dir, abfd, cu,
5705 &dwarf2_per_objfile->macinfo, 0);
5706 }
2e276125 5707 }
9cdd5dbd 5708
debd256d 5709 do_cleanups (back_to);
5fb290d7
DJ
5710}
5711
ae2de4f8
DE
5712/* Process DW_TAG_type_unit.
5713 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5714 actual type being defined by this TU. In this case the first top
5715 level sibling is there to provide context only. */
5716
5717static void
5718read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5719{
5720 struct objfile *objfile = cu->objfile;
5721 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5722 CORE_ADDR lowpc;
5723 struct attribute *attr;
5724 char *name = NULL;
5725 char *comp_dir = NULL;
5726 struct die_info *child_die;
5727 bfd *abfd = objfile->obfd;
348e048f
DE
5728
5729 /* start_symtab needs a low pc, but we don't really have one.
5730 Do what read_file_scope would do in the absence of such info. */
5731 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5732
5733 /* Find the filename. Do not use dwarf2_name here, since the filename
5734 is not a source language identifier. */
5735 attr = dwarf2_attr (die, DW_AT_name, cu);
5736 if (attr)
5737 name = DW_STRING (attr);
5738
5739 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5740 if (attr)
5741 comp_dir = DW_STRING (attr);
5742 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5743 {
5744 comp_dir = ldirname (name);
5745 if (comp_dir != NULL)
5746 make_cleanup (xfree, comp_dir);
5747 }
5748
5749 if (name == NULL)
5750 name = "<unknown>";
5751
5752 attr = dwarf2_attr (die, DW_AT_language, cu);
5753 if (attr)
5754 set_cu_language (DW_UNSND (attr), cu);
5755
5756 /* This isn't technically needed today. It is done for symmetry
5757 with read_file_scope. */
5758 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5759 if (attr)
348e048f
DE
5760 cu->producer = DW_STRING (attr);
5761
0963b4bd 5762 /* We assume that we're processing GCC output. */
348e048f
DE
5763 processing_gcc_compilation = 2;
5764
5765 processing_has_namespace_info = 0;
5766
5767 start_symtab (name, comp_dir, lowpc);
5768 record_debugformat ("DWARF 2");
5769 record_producer (cu->producer);
5770
2ab95328
TT
5771 handle_DW_AT_stmt_list (die, cu, comp_dir);
5772
348e048f
DE
5773 /* Process the dies in the type unit. */
5774 if (die->child == NULL)
5775 {
5776 dump_die_for_error (die);
5777 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5778 bfd_get_filename (abfd));
5779 }
5780
5781 child_die = die->child;
5782
5783 while (child_die && child_die->tag)
5784 {
5785 process_die (child_die, cu);
5786
5787 child_die = sibling_die (child_die);
5788 }
5789
5790 do_cleanups (back_to);
5791}
5792
d389af10
JK
5793/* qsort helper for inherit_abstract_dies. */
5794
5795static int
5796unsigned_int_compar (const void *ap, const void *bp)
5797{
5798 unsigned int a = *(unsigned int *) ap;
5799 unsigned int b = *(unsigned int *) bp;
5800
5801 return (a > b) - (b > a);
5802}
5803
5804/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5805 Inherit only the children of the DW_AT_abstract_origin DIE not being
5806 already referenced by DW_AT_abstract_origin from the children of the
5807 current DIE. */
d389af10
JK
5808
5809static void
5810inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5811{
5812 struct die_info *child_die;
5813 unsigned die_children_count;
5814 /* CU offsets which were referenced by children of the current DIE. */
5815 unsigned *offsets;
5816 unsigned *offsets_end, *offsetp;
5817 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5818 struct die_info *origin_die;
5819 /* Iterator of the ORIGIN_DIE children. */
5820 struct die_info *origin_child_die;
5821 struct cleanup *cleanups;
5822 struct attribute *attr;
cd02d79d
PA
5823 struct dwarf2_cu *origin_cu;
5824 struct pending **origin_previous_list_in_scope;
d389af10
JK
5825
5826 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5827 if (!attr)
5828 return;
5829
cd02d79d
PA
5830 /* Note that following die references may follow to a die in a
5831 different cu. */
5832
5833 origin_cu = cu;
5834 origin_die = follow_die_ref (die, attr, &origin_cu);
5835
5836 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5837 symbols in. */
5838 origin_previous_list_in_scope = origin_cu->list_in_scope;
5839 origin_cu->list_in_scope = cu->list_in_scope;
5840
edb3359d
DJ
5841 if (die->tag != origin_die->tag
5842 && !(die->tag == DW_TAG_inlined_subroutine
5843 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5844 complaint (&symfile_complaints,
5845 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5846 die->offset, origin_die->offset);
5847
5848 child_die = die->child;
5849 die_children_count = 0;
5850 while (child_die && child_die->tag)
5851 {
5852 child_die = sibling_die (child_die);
5853 die_children_count++;
5854 }
5855 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5856 cleanups = make_cleanup (xfree, offsets);
5857
5858 offsets_end = offsets;
5859 child_die = die->child;
5860 while (child_die && child_die->tag)
5861 {
c38f313d
DJ
5862 /* For each CHILD_DIE, find the corresponding child of
5863 ORIGIN_DIE. If there is more than one layer of
5864 DW_AT_abstract_origin, follow them all; there shouldn't be,
5865 but GCC versions at least through 4.4 generate this (GCC PR
5866 40573). */
5867 struct die_info *child_origin_die = child_die;
cd02d79d 5868 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5869
c38f313d
DJ
5870 while (1)
5871 {
cd02d79d
PA
5872 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5873 child_origin_cu);
c38f313d
DJ
5874 if (attr == NULL)
5875 break;
cd02d79d
PA
5876 child_origin_die = follow_die_ref (child_origin_die, attr,
5877 &child_origin_cu);
c38f313d
DJ
5878 }
5879
d389af10
JK
5880 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5881 counterpart may exist. */
c38f313d 5882 if (child_origin_die != child_die)
d389af10 5883 {
edb3359d
DJ
5884 if (child_die->tag != child_origin_die->tag
5885 && !(child_die->tag == DW_TAG_inlined_subroutine
5886 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5887 complaint (&symfile_complaints,
5888 _("Child DIE 0x%x and its abstract origin 0x%x have "
5889 "different tags"), child_die->offset,
5890 child_origin_die->offset);
c38f313d
DJ
5891 if (child_origin_die->parent != origin_die)
5892 complaint (&symfile_complaints,
5893 _("Child DIE 0x%x and its abstract origin 0x%x have "
5894 "different parents"), child_die->offset,
5895 child_origin_die->offset);
5896 else
5897 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5898 }
5899 child_die = sibling_die (child_die);
5900 }
5901 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5902 unsigned_int_compar);
5903 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5904 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5905 complaint (&symfile_complaints,
5906 _("Multiple children of DIE 0x%x refer "
5907 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5908 die->offset, *offsetp);
5909
5910 offsetp = offsets;
5911 origin_child_die = origin_die->child;
5912 while (origin_child_die && origin_child_die->tag)
5913 {
5914 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5915 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5916 offsetp++;
5917 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5918 {
5919 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5920 process_die (origin_child_die, origin_cu);
d389af10
JK
5921 }
5922 origin_child_die = sibling_die (origin_child_die);
5923 }
cd02d79d 5924 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5925
5926 do_cleanups (cleanups);
5927}
5928
c906108c 5929static void
e7c27a73 5930read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5931{
e7c27a73 5932 struct objfile *objfile = cu->objfile;
52f0bd74 5933 struct context_stack *new;
c906108c
SS
5934 CORE_ADDR lowpc;
5935 CORE_ADDR highpc;
5936 struct die_info *child_die;
edb3359d 5937 struct attribute *attr, *call_line, *call_file;
c906108c 5938 char *name;
e142c38c 5939 CORE_ADDR baseaddr;
801e3a5b 5940 struct block *block;
edb3359d 5941 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5942 VEC (symbolp) *template_args = NULL;
5943 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5944
5945 if (inlined_func)
5946 {
5947 /* If we do not have call site information, we can't show the
5948 caller of this inlined function. That's too confusing, so
5949 only use the scope for local variables. */
5950 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5951 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5952 if (call_line == NULL || call_file == NULL)
5953 {
5954 read_lexical_block_scope (die, cu);
5955 return;
5956 }
5957 }
c906108c 5958
e142c38c
DJ
5959 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5960
94af9270 5961 name = dwarf2_name (die, cu);
c906108c 5962
e8d05480
JB
5963 /* Ignore functions with missing or empty names. These are actually
5964 illegal according to the DWARF standard. */
5965 if (name == NULL)
5966 {
5967 complaint (&symfile_complaints,
5968 _("missing name for subprogram DIE at %d"), die->offset);
5969 return;
5970 }
5971
5972 /* Ignore functions with missing or invalid low and high pc attributes. */
5973 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5974 {
ae4d0c03
PM
5975 attr = dwarf2_attr (die, DW_AT_external, cu);
5976 if (!attr || !DW_UNSND (attr))
5977 complaint (&symfile_complaints,
3e43a32a
MS
5978 _("cannot get low and high bounds "
5979 "for subprogram DIE at %d"),
ae4d0c03 5980 die->offset);
e8d05480
JB
5981 return;
5982 }
c906108c
SS
5983
5984 lowpc += baseaddr;
5985 highpc += baseaddr;
5986
34eaf542
TT
5987 /* If we have any template arguments, then we must allocate a
5988 different sort of symbol. */
5989 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5990 {
5991 if (child_die->tag == DW_TAG_template_type_param
5992 || child_die->tag == DW_TAG_template_value_param)
5993 {
5994 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5995 struct template_symbol);
5996 templ_func->base.is_cplus_template_function = 1;
5997 break;
5998 }
5999 }
6000
c906108c 6001 new = push_context (0, lowpc);
34eaf542
TT
6002 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6003 (struct symbol *) templ_func);
4c2df51b 6004
4cecd739
DJ
6005 /* If there is a location expression for DW_AT_frame_base, record
6006 it. */
e142c38c 6007 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6008 if (attr)
c034e007
AC
6009 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6010 expression is being recorded directly in the function's symbol
6011 and not in a separate frame-base object. I guess this hack is
6012 to avoid adding some sort of frame-base adjunct/annex to the
6013 function's symbol :-(. The problem with doing this is that it
6014 results in a function symbol with a location expression that
6015 has nothing to do with the location of the function, ouch! The
6016 relationship should be: a function's symbol has-a frame base; a
6017 frame-base has-a location expression. */
e7c27a73 6018 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6019
e142c38c 6020 cu->list_in_scope = &local_symbols;
c906108c 6021
639d11d3 6022 if (die->child != NULL)
c906108c 6023 {
639d11d3 6024 child_die = die->child;
c906108c
SS
6025 while (child_die && child_die->tag)
6026 {
34eaf542
TT
6027 if (child_die->tag == DW_TAG_template_type_param
6028 || child_die->tag == DW_TAG_template_value_param)
6029 {
6030 struct symbol *arg = new_symbol (child_die, NULL, cu);
6031
f1078f66
DJ
6032 if (arg != NULL)
6033 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6034 }
6035 else
6036 process_die (child_die, cu);
c906108c
SS
6037 child_die = sibling_die (child_die);
6038 }
6039 }
6040
d389af10
JK
6041 inherit_abstract_dies (die, cu);
6042
4a811a97
UW
6043 /* If we have a DW_AT_specification, we might need to import using
6044 directives from the context of the specification DIE. See the
6045 comment in determine_prefix. */
6046 if (cu->language == language_cplus
6047 && dwarf2_attr (die, DW_AT_specification, cu))
6048 {
6049 struct dwarf2_cu *spec_cu = cu;
6050 struct die_info *spec_die = die_specification (die, &spec_cu);
6051
6052 while (spec_die)
6053 {
6054 child_die = spec_die->child;
6055 while (child_die && child_die->tag)
6056 {
6057 if (child_die->tag == DW_TAG_imported_module)
6058 process_die (child_die, spec_cu);
6059 child_die = sibling_die (child_die);
6060 }
6061
6062 /* In some cases, GCC generates specification DIEs that
6063 themselves contain DW_AT_specification attributes. */
6064 spec_die = die_specification (spec_die, &spec_cu);
6065 }
6066 }
6067
c906108c
SS
6068 new = pop_context ();
6069 /* Make a block for the local symbols within. */
801e3a5b
JB
6070 block = finish_block (new->name, &local_symbols, new->old_blocks,
6071 lowpc, highpc, objfile);
6072
df8a16a1 6073 /* For C++, set the block's scope. */
f55ee35c 6074 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6075 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6076 determine_prefix (die, cu),
df8a16a1
DJ
6077 processing_has_namespace_info);
6078
801e3a5b
JB
6079 /* If we have address ranges, record them. */
6080 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6081
34eaf542
TT
6082 /* Attach template arguments to function. */
6083 if (! VEC_empty (symbolp, template_args))
6084 {
6085 gdb_assert (templ_func != NULL);
6086
6087 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6088 templ_func->template_arguments
6089 = obstack_alloc (&objfile->objfile_obstack,
6090 (templ_func->n_template_arguments
6091 * sizeof (struct symbol *)));
6092 memcpy (templ_func->template_arguments,
6093 VEC_address (symbolp, template_args),
6094 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6095 VEC_free (symbolp, template_args);
6096 }
6097
208d8187
JB
6098 /* In C++, we can have functions nested inside functions (e.g., when
6099 a function declares a class that has methods). This means that
6100 when we finish processing a function scope, we may need to go
6101 back to building a containing block's symbol lists. */
6102 local_symbols = new->locals;
6103 param_symbols = new->params;
27aa8d6a 6104 using_directives = new->using_directives;
208d8187 6105
921e78cf
JB
6106 /* If we've finished processing a top-level function, subsequent
6107 symbols go in the file symbol list. */
6108 if (outermost_context_p ())
e142c38c 6109 cu->list_in_scope = &file_symbols;
c906108c
SS
6110}
6111
6112/* Process all the DIES contained within a lexical block scope. Start
6113 a new scope, process the dies, and then close the scope. */
6114
6115static void
e7c27a73 6116read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6117{
e7c27a73 6118 struct objfile *objfile = cu->objfile;
52f0bd74 6119 struct context_stack *new;
c906108c
SS
6120 CORE_ADDR lowpc, highpc;
6121 struct die_info *child_die;
e142c38c
DJ
6122 CORE_ADDR baseaddr;
6123
6124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6125
6126 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6127 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6128 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6129 be nasty. Might be easier to properly extend generic blocks to
af34e669 6130 describe ranges. */
d85a05f0 6131 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6132 return;
6133 lowpc += baseaddr;
6134 highpc += baseaddr;
6135
6136 push_context (0, lowpc);
639d11d3 6137 if (die->child != NULL)
c906108c 6138 {
639d11d3 6139 child_die = die->child;
c906108c
SS
6140 while (child_die && child_die->tag)
6141 {
e7c27a73 6142 process_die (child_die, cu);
c906108c
SS
6143 child_die = sibling_die (child_die);
6144 }
6145 }
6146 new = pop_context ();
6147
8540c487 6148 if (local_symbols != NULL || using_directives != NULL)
c906108c 6149 {
801e3a5b
JB
6150 struct block *block
6151 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6152 highpc, objfile);
6153
6154 /* Note that recording ranges after traversing children, as we
6155 do here, means that recording a parent's ranges entails
6156 walking across all its children's ranges as they appear in
6157 the address map, which is quadratic behavior.
6158
6159 It would be nicer to record the parent's ranges before
6160 traversing its children, simply overriding whatever you find
6161 there. But since we don't even decide whether to create a
6162 block until after we've traversed its children, that's hard
6163 to do. */
6164 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6165 }
6166 local_symbols = new->locals;
27aa8d6a 6167 using_directives = new->using_directives;
c906108c
SS
6168}
6169
96408a79
SA
6170/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6171
6172static void
6173read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6174{
6175 struct objfile *objfile = cu->objfile;
6176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6177 CORE_ADDR pc, baseaddr;
6178 struct attribute *attr;
6179 struct call_site *call_site, call_site_local;
6180 void **slot;
6181 int nparams;
6182 struct die_info *child_die;
6183
6184 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6185
6186 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6187 if (!attr)
6188 {
6189 complaint (&symfile_complaints,
6190 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6191 "DIE 0x%x [in module %s]"),
bb5ed363 6192 die->offset, objfile->name);
96408a79
SA
6193 return;
6194 }
6195 pc = DW_ADDR (attr) + baseaddr;
6196
6197 if (cu->call_site_htab == NULL)
6198 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6199 NULL, &objfile->objfile_obstack,
6200 hashtab_obstack_allocate, NULL);
6201 call_site_local.pc = pc;
6202 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6203 if (*slot != NULL)
6204 {
6205 complaint (&symfile_complaints,
6206 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6207 "DIE 0x%x [in module %s]"),
bb5ed363 6208 paddress (gdbarch, pc), die->offset, objfile->name);
96408a79
SA
6209 return;
6210 }
6211
6212 /* Count parameters at the caller. */
6213
6214 nparams = 0;
6215 for (child_die = die->child; child_die && child_die->tag;
6216 child_die = sibling_die (child_die))
6217 {
6218 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6219 {
6220 complaint (&symfile_complaints,
6221 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6222 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6223 child_die->tag, child_die->offset, objfile->name);
96408a79
SA
6224 continue;
6225 }
6226
6227 nparams++;
6228 }
6229
6230 call_site = obstack_alloc (&objfile->objfile_obstack,
6231 (sizeof (*call_site)
6232 + (sizeof (*call_site->parameter)
6233 * (nparams - 1))));
6234 *slot = call_site;
6235 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6236 call_site->pc = pc;
6237
6238 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6239 {
6240 struct die_info *func_die;
6241
6242 /* Skip also over DW_TAG_inlined_subroutine. */
6243 for (func_die = die->parent;
6244 func_die && func_die->tag != DW_TAG_subprogram
6245 && func_die->tag != DW_TAG_subroutine_type;
6246 func_die = func_die->parent);
6247
6248 /* DW_AT_GNU_all_call_sites is a superset
6249 of DW_AT_GNU_all_tail_call_sites. */
6250 if (func_die
6251 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6252 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6253 {
6254 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6255 not complete. But keep CALL_SITE for look ups via call_site_htab,
6256 both the initial caller containing the real return address PC and
6257 the final callee containing the current PC of a chain of tail
6258 calls do not need to have the tail call list complete. But any
6259 function candidate for a virtual tail call frame searched via
6260 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6261 determined unambiguously. */
6262 }
6263 else
6264 {
6265 struct type *func_type = NULL;
6266
6267 if (func_die)
6268 func_type = get_die_type (func_die, cu);
6269 if (func_type != NULL)
6270 {
6271 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6272
6273 /* Enlist this call site to the function. */
6274 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6275 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6276 }
6277 else
6278 complaint (&symfile_complaints,
6279 _("Cannot find function owning DW_TAG_GNU_call_site "
6280 "DIE 0x%x [in module %s]"),
bb5ed363 6281 die->offset, objfile->name);
96408a79
SA
6282 }
6283 }
6284
6285 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6286 if (attr == NULL)
6287 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6288 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6289 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6290 /* Keep NULL DWARF_BLOCK. */;
6291 else if (attr_form_is_block (attr))
6292 {
6293 struct dwarf2_locexpr_baton *dlbaton;
6294
6295 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6296 dlbaton->data = DW_BLOCK (attr)->data;
6297 dlbaton->size = DW_BLOCK (attr)->size;
6298 dlbaton->per_cu = cu->per_cu;
6299
6300 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6301 }
6302 else if (is_ref_attr (attr))
6303 {
96408a79
SA
6304 struct dwarf2_cu *target_cu = cu;
6305 struct die_info *target_die;
6306
6307 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6308 gdb_assert (target_cu->objfile == objfile);
6309 if (die_is_declaration (target_die, target_cu))
6310 {
6311 const char *target_physname;
6312
6313 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6314 if (target_physname == NULL)
6315 complaint (&symfile_complaints,
6316 _("DW_AT_GNU_call_site_target target DIE has invalid "
6317 "physname, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6318 die->offset, objfile->name);
96408a79
SA
6319 else
6320 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6321 }
6322 else
6323 {
6324 CORE_ADDR lowpc;
6325
6326 /* DW_AT_entry_pc should be preferred. */
6327 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6328 complaint (&symfile_complaints,
6329 _("DW_AT_GNU_call_site_target target DIE has invalid "
6330 "low pc, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6331 die->offset, objfile->name);
96408a79
SA
6332 else
6333 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6334 }
6335 }
6336 else
6337 complaint (&symfile_complaints,
6338 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6339 "block nor reference, for DIE 0x%x [in module %s]"),
bb5ed363 6340 die->offset, objfile->name);
96408a79
SA
6341
6342 call_site->per_cu = cu->per_cu;
6343
6344 for (child_die = die->child;
6345 child_die && child_die->tag;
6346 child_die = sibling_die (child_die))
6347 {
6348 struct dwarf2_locexpr_baton *dlbaton;
6349 struct call_site_parameter *parameter;
6350
6351 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6352 {
6353 /* Already printed the complaint above. */
6354 continue;
6355 }
6356
6357 gdb_assert (call_site->parameter_count < nparams);
6358 parameter = &call_site->parameter[call_site->parameter_count];
6359
6360 /* DW_AT_location specifies the register number. Value of the data
6361 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6362
6363 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6364 if (!attr || !attr_form_is_block (attr))
6365 {
6366 complaint (&symfile_complaints,
6367 _("No DW_FORM_block* DW_AT_location for "
6368 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6369 child_die->offset, objfile->name);
96408a79
SA
6370 continue;
6371 }
6372 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6373 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6374 if (parameter->dwarf_reg == -1
6375 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6376 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6377 &parameter->fb_offset))
6378 {
6379 complaint (&symfile_complaints,
6380 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6381 "for DW_FORM_block* DW_AT_location for "
6382 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6383 child_die->offset, objfile->name);
96408a79
SA
6384 continue;
6385 }
6386
6387 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6388 if (!attr_form_is_block (attr))
6389 {
6390 complaint (&symfile_complaints,
6391 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6392 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6393 child_die->offset, objfile->name);
96408a79
SA
6394 continue;
6395 }
6396 parameter->value = DW_BLOCK (attr)->data;
6397 parameter->value_size = DW_BLOCK (attr)->size;
6398
6399 /* Parameters are not pre-cleared by memset above. */
6400 parameter->data_value = NULL;
6401 parameter->data_value_size = 0;
6402 call_site->parameter_count++;
6403
6404 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6405 if (attr)
6406 {
6407 if (!attr_form_is_block (attr))
6408 complaint (&symfile_complaints,
6409 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6410 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6411 child_die->offset, objfile->name);
96408a79
SA
6412 else
6413 {
6414 parameter->data_value = DW_BLOCK (attr)->data;
6415 parameter->data_value_size = DW_BLOCK (attr)->size;
6416 }
6417 }
6418 }
6419}
6420
43039443 6421/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6422 Return 1 if the attributes are present and valid, otherwise, return 0.
6423 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6424
6425static int
6426dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6427 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6428 struct partial_symtab *ranges_pst)
43039443
JK
6429{
6430 struct objfile *objfile = cu->objfile;
6431 struct comp_unit_head *cu_header = &cu->header;
6432 bfd *obfd = objfile->obfd;
6433 unsigned int addr_size = cu_header->addr_size;
6434 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6435 /* Base address selection entry. */
6436 CORE_ADDR base;
6437 int found_base;
6438 unsigned int dummy;
6439 gdb_byte *buffer;
6440 CORE_ADDR marker;
6441 int low_set;
6442 CORE_ADDR low = 0;
6443 CORE_ADDR high = 0;
ff013f42 6444 CORE_ADDR baseaddr;
43039443 6445
d00adf39
DE
6446 found_base = cu->base_known;
6447 base = cu->base_address;
43039443 6448
be391dca 6449 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6450 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6451 {
6452 complaint (&symfile_complaints,
6453 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6454 offset);
6455 return 0;
6456 }
dce234bc 6457 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6458
6459 /* Read in the largest possible address. */
6460 marker = read_address (obfd, buffer, cu, &dummy);
6461 if ((marker & mask) == mask)
6462 {
6463 /* If we found the largest possible address, then
6464 read the base address. */
6465 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6466 buffer += 2 * addr_size;
6467 offset += 2 * addr_size;
6468 found_base = 1;
6469 }
6470
6471 low_set = 0;
6472
e7030f15 6473 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6474
43039443
JK
6475 while (1)
6476 {
6477 CORE_ADDR range_beginning, range_end;
6478
6479 range_beginning = read_address (obfd, buffer, cu, &dummy);
6480 buffer += addr_size;
6481 range_end = read_address (obfd, buffer, cu, &dummy);
6482 buffer += addr_size;
6483 offset += 2 * addr_size;
6484
6485 /* An end of list marker is a pair of zero addresses. */
6486 if (range_beginning == 0 && range_end == 0)
6487 /* Found the end of list entry. */
6488 break;
6489
6490 /* Each base address selection entry is a pair of 2 values.
6491 The first is the largest possible address, the second is
6492 the base address. Check for a base address here. */
6493 if ((range_beginning & mask) == mask)
6494 {
6495 /* If we found the largest possible address, then
6496 read the base address. */
6497 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6498 found_base = 1;
6499 continue;
6500 }
6501
6502 if (!found_base)
6503 {
6504 /* We have no valid base address for the ranges
6505 data. */
6506 complaint (&symfile_complaints,
6507 _("Invalid .debug_ranges data (no base address)"));
6508 return 0;
6509 }
6510
9277c30c
UW
6511 if (range_beginning > range_end)
6512 {
6513 /* Inverted range entries are invalid. */
6514 complaint (&symfile_complaints,
6515 _("Invalid .debug_ranges data (inverted range)"));
6516 return 0;
6517 }
6518
6519 /* Empty range entries have no effect. */
6520 if (range_beginning == range_end)
6521 continue;
6522
43039443
JK
6523 range_beginning += base;
6524 range_end += base;
6525
9277c30c 6526 if (ranges_pst != NULL)
ff013f42 6527 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6528 range_beginning + baseaddr,
6529 range_end - 1 + baseaddr,
ff013f42
JK
6530 ranges_pst);
6531
43039443
JK
6532 /* FIXME: This is recording everything as a low-high
6533 segment of consecutive addresses. We should have a
6534 data structure for discontiguous block ranges
6535 instead. */
6536 if (! low_set)
6537 {
6538 low = range_beginning;
6539 high = range_end;
6540 low_set = 1;
6541 }
6542 else
6543 {
6544 if (range_beginning < low)
6545 low = range_beginning;
6546 if (range_end > high)
6547 high = range_end;
6548 }
6549 }
6550
6551 if (! low_set)
6552 /* If the first entry is an end-of-list marker, the range
6553 describes an empty scope, i.e. no instructions. */
6554 return 0;
6555
6556 if (low_return)
6557 *low_return = low;
6558 if (high_return)
6559 *high_return = high;
6560 return 1;
6561}
6562
af34e669
DJ
6563/* Get low and high pc attributes from a die. Return 1 if the attributes
6564 are present and valid, otherwise, return 0. Return -1 if the range is
6565 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6566static int
af34e669 6567dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6568 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6569 struct partial_symtab *pst)
c906108c
SS
6570{
6571 struct attribute *attr;
af34e669
DJ
6572 CORE_ADDR low = 0;
6573 CORE_ADDR high = 0;
6574 int ret = 0;
c906108c 6575
e142c38c 6576 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6577 if (attr)
af34e669
DJ
6578 {
6579 high = DW_ADDR (attr);
e142c38c 6580 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6581 if (attr)
6582 low = DW_ADDR (attr);
6583 else
6584 /* Found high w/o low attribute. */
6585 return 0;
6586
6587 /* Found consecutive range of addresses. */
6588 ret = 1;
6589 }
c906108c 6590 else
af34e669 6591 {
e142c38c 6592 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6593 if (attr != NULL)
6594 {
af34e669 6595 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6596 .debug_ranges section. */
d85a05f0 6597 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6598 return 0;
43039443 6599 /* Found discontinuous range of addresses. */
af34e669
DJ
6600 ret = -1;
6601 }
6602 }
c906108c 6603
9373cf26
JK
6604 /* read_partial_die has also the strict LOW < HIGH requirement. */
6605 if (high <= low)
c906108c
SS
6606 return 0;
6607
6608 /* When using the GNU linker, .gnu.linkonce. sections are used to
6609 eliminate duplicate copies of functions and vtables and such.
6610 The linker will arbitrarily choose one and discard the others.
6611 The AT_*_pc values for such functions refer to local labels in
6612 these sections. If the section from that file was discarded, the
6613 labels are not in the output, so the relocs get a value of 0.
6614 If this is a discarded function, mark the pc bounds as invalid,
6615 so that GDB will ignore it. */
72dca2f5 6616 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6617 return 0;
6618
6619 *lowpc = low;
96408a79
SA
6620 if (highpc)
6621 *highpc = high;
af34e669 6622 return ret;
c906108c
SS
6623}
6624
b084d499
JB
6625/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6626 its low and high PC addresses. Do nothing if these addresses could not
6627 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6628 and HIGHPC to the high address if greater than HIGHPC. */
6629
6630static void
6631dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6632 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6633 struct dwarf2_cu *cu)
6634{
6635 CORE_ADDR low, high;
6636 struct die_info *child = die->child;
6637
d85a05f0 6638 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6639 {
6640 *lowpc = min (*lowpc, low);
6641 *highpc = max (*highpc, high);
6642 }
6643
6644 /* If the language does not allow nested subprograms (either inside
6645 subprograms or lexical blocks), we're done. */
6646 if (cu->language != language_ada)
6647 return;
6e70227d 6648
b084d499
JB
6649 /* Check all the children of the given DIE. If it contains nested
6650 subprograms, then check their pc bounds. Likewise, we need to
6651 check lexical blocks as well, as they may also contain subprogram
6652 definitions. */
6653 while (child && child->tag)
6654 {
6655 if (child->tag == DW_TAG_subprogram
6656 || child->tag == DW_TAG_lexical_block)
6657 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6658 child = sibling_die (child);
6659 }
6660}
6661
fae299cd
DC
6662/* Get the low and high pc's represented by the scope DIE, and store
6663 them in *LOWPC and *HIGHPC. If the correct values can't be
6664 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6665
6666static void
6667get_scope_pc_bounds (struct die_info *die,
6668 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6669 struct dwarf2_cu *cu)
6670{
6671 CORE_ADDR best_low = (CORE_ADDR) -1;
6672 CORE_ADDR best_high = (CORE_ADDR) 0;
6673 CORE_ADDR current_low, current_high;
6674
d85a05f0 6675 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6676 {
6677 best_low = current_low;
6678 best_high = current_high;
6679 }
6680 else
6681 {
6682 struct die_info *child = die->child;
6683
6684 while (child && child->tag)
6685 {
6686 switch (child->tag) {
6687 case DW_TAG_subprogram:
b084d499 6688 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6689 break;
6690 case DW_TAG_namespace:
f55ee35c 6691 case DW_TAG_module:
fae299cd
DC
6692 /* FIXME: carlton/2004-01-16: Should we do this for
6693 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6694 that current GCC's always emit the DIEs corresponding
6695 to definitions of methods of classes as children of a
6696 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6697 the DIEs giving the declarations, which could be
6698 anywhere). But I don't see any reason why the
6699 standards says that they have to be there. */
6700 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6701
6702 if (current_low != ((CORE_ADDR) -1))
6703 {
6704 best_low = min (best_low, current_low);
6705 best_high = max (best_high, current_high);
6706 }
6707 break;
6708 default:
0963b4bd 6709 /* Ignore. */
fae299cd
DC
6710 break;
6711 }
6712
6713 child = sibling_die (child);
6714 }
6715 }
6716
6717 *lowpc = best_low;
6718 *highpc = best_high;
6719}
6720
801e3a5b
JB
6721/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6722 in DIE. */
6723static void
6724dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6725 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6726{
bb5ed363 6727 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6728 struct attribute *attr;
6729
6730 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6731 if (attr)
6732 {
6733 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6734
801e3a5b
JB
6735 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6736 if (attr)
6737 {
6738 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6739
801e3a5b
JB
6740 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6741 }
6742 }
6743
6744 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6745 if (attr)
6746 {
bb5ed363 6747 bfd *obfd = objfile->obfd;
801e3a5b
JB
6748
6749 /* The value of the DW_AT_ranges attribute is the offset of the
6750 address range list in the .debug_ranges section. */
6751 unsigned long offset = DW_UNSND (attr);
dce234bc 6752 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6753
6754 /* For some target architectures, but not others, the
6755 read_address function sign-extends the addresses it returns.
6756 To recognize base address selection entries, we need a
6757 mask. */
6758 unsigned int addr_size = cu->header.addr_size;
6759 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6760
6761 /* The base address, to which the next pair is relative. Note
6762 that this 'base' is a DWARF concept: most entries in a range
6763 list are relative, to reduce the number of relocs against the
6764 debugging information. This is separate from this function's
6765 'baseaddr' argument, which GDB uses to relocate debugging
6766 information from a shared library based on the address at
6767 which the library was loaded. */
d00adf39
DE
6768 CORE_ADDR base = cu->base_address;
6769 int base_known = cu->base_known;
801e3a5b 6770
be391dca 6771 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6772 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6773 {
6774 complaint (&symfile_complaints,
6775 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6776 offset);
6777 return;
6778 }
6779
6780 for (;;)
6781 {
6782 unsigned int bytes_read;
6783 CORE_ADDR start, end;
6784
6785 start = read_address (obfd, buffer, cu, &bytes_read);
6786 buffer += bytes_read;
6787 end = read_address (obfd, buffer, cu, &bytes_read);
6788 buffer += bytes_read;
6789
6790 /* Did we find the end of the range list? */
6791 if (start == 0 && end == 0)
6792 break;
6793
6794 /* Did we find a base address selection entry? */
6795 else if ((start & base_select_mask) == base_select_mask)
6796 {
6797 base = end;
6798 base_known = 1;
6799 }
6800
6801 /* We found an ordinary address range. */
6802 else
6803 {
6804 if (!base_known)
6805 {
6806 complaint (&symfile_complaints,
3e43a32a
MS
6807 _("Invalid .debug_ranges data "
6808 "(no base address)"));
801e3a5b
JB
6809 return;
6810 }
6811
9277c30c
UW
6812 if (start > end)
6813 {
6814 /* Inverted range entries are invalid. */
6815 complaint (&symfile_complaints,
6816 _("Invalid .debug_ranges data "
6817 "(inverted range)"));
6818 return;
6819 }
6820
6821 /* Empty range entries have no effect. */
6822 if (start == end)
6823 continue;
6824
6e70227d
DE
6825 record_block_range (block,
6826 baseaddr + base + start,
801e3a5b
JB
6827 baseaddr + base + end - 1);
6828 }
6829 }
6830 }
6831}
6832
60d5a603
JK
6833/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6834 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6835 during 4.6.0 experimental. */
6836
6837static int
6838producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6839{
6840 const char *cs;
6841 int major, minor, release;
6842
6843 if (cu->producer == NULL)
6844 {
6845 /* For unknown compilers expect their behavior is DWARF version
6846 compliant.
6847
6848 GCC started to support .debug_types sections by -gdwarf-4 since
6849 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6850 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6851 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6852 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6853
6854 return 0;
6855 }
6856
6857 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6858
6859 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6860 {
6861 /* For non-GCC compilers expect their behavior is DWARF version
6862 compliant. */
6863
6864 return 0;
6865 }
6866 cs = &cu->producer[strlen ("GNU ")];
6867 while (*cs && !isdigit (*cs))
6868 cs++;
6869 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6870 {
6871 /* Not recognized as GCC. */
6872
6873 return 0;
6874 }
6875
6876 return major < 4 || (major == 4 && minor < 6);
6877}
6878
6879/* Return the default accessibility type if it is not overriden by
6880 DW_AT_accessibility. */
6881
6882static enum dwarf_access_attribute
6883dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6884{
6885 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6886 {
6887 /* The default DWARF 2 accessibility for members is public, the default
6888 accessibility for inheritance is private. */
6889
6890 if (die->tag != DW_TAG_inheritance)
6891 return DW_ACCESS_public;
6892 else
6893 return DW_ACCESS_private;
6894 }
6895 else
6896 {
6897 /* DWARF 3+ defines the default accessibility a different way. The same
6898 rules apply now for DW_TAG_inheritance as for the members and it only
6899 depends on the container kind. */
6900
6901 if (die->parent->tag == DW_TAG_class_type)
6902 return DW_ACCESS_private;
6903 else
6904 return DW_ACCESS_public;
6905 }
6906}
6907
74ac6d43
TT
6908/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6909 offset. If the attribute was not found return 0, otherwise return
6910 1. If it was found but could not properly be handled, set *OFFSET
6911 to 0. */
6912
6913static int
6914handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6915 LONGEST *offset)
6916{
6917 struct attribute *attr;
6918
6919 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6920 if (attr != NULL)
6921 {
6922 *offset = 0;
6923
6924 /* Note that we do not check for a section offset first here.
6925 This is because DW_AT_data_member_location is new in DWARF 4,
6926 so if we see it, we can assume that a constant form is really
6927 a constant and not a section offset. */
6928 if (attr_form_is_constant (attr))
6929 *offset = dwarf2_get_attr_constant_value (attr, 0);
6930 else if (attr_form_is_section_offset (attr))
6931 dwarf2_complex_location_expr_complaint ();
6932 else if (attr_form_is_block (attr))
6933 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6934 else
6935 dwarf2_complex_location_expr_complaint ();
6936
6937 return 1;
6938 }
6939
6940 return 0;
6941}
6942
c906108c
SS
6943/* Add an aggregate field to the field list. */
6944
6945static void
107d2387 6946dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6947 struct dwarf2_cu *cu)
6e70227d 6948{
e7c27a73 6949 struct objfile *objfile = cu->objfile;
5e2b427d 6950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6951 struct nextfield *new_field;
6952 struct attribute *attr;
6953 struct field *fp;
6954 char *fieldname = "";
6955
6956 /* Allocate a new field list entry and link it in. */
6957 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6958 make_cleanup (xfree, new_field);
c906108c 6959 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6960
6961 if (die->tag == DW_TAG_inheritance)
6962 {
6963 new_field->next = fip->baseclasses;
6964 fip->baseclasses = new_field;
6965 }
6966 else
6967 {
6968 new_field->next = fip->fields;
6969 fip->fields = new_field;
6970 }
c906108c
SS
6971 fip->nfields++;
6972
e142c38c 6973 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6974 if (attr)
6975 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6976 else
6977 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6978 if (new_field->accessibility != DW_ACCESS_public)
6979 fip->non_public_fields = 1;
60d5a603 6980
e142c38c 6981 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6982 if (attr)
6983 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6984 else
6985 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6986
6987 fp = &new_field->field;
a9a9bd0f 6988
e142c38c 6989 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6990 {
74ac6d43
TT
6991 LONGEST offset;
6992
a9a9bd0f 6993 /* Data member other than a C++ static data member. */
6e70227d 6994
c906108c 6995 /* Get type of field. */
e7c27a73 6996 fp->type = die_type (die, cu);
c906108c 6997
d6a843b5 6998 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6999
c906108c 7000 /* Get bit size of field (zero if none). */
e142c38c 7001 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7002 if (attr)
7003 {
7004 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7005 }
7006 else
7007 {
7008 FIELD_BITSIZE (*fp) = 0;
7009 }
7010
7011 /* Get bit offset of field. */
74ac6d43
TT
7012 if (handle_data_member_location (die, cu, &offset))
7013 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7014 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7015 if (attr)
7016 {
5e2b427d 7017 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7018 {
7019 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7020 additional bit offset from the MSB of the containing
7021 anonymous object to the MSB of the field. We don't
7022 have to do anything special since we don't need to
7023 know the size of the anonymous object. */
c906108c
SS
7024 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7025 }
7026 else
7027 {
7028 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7029 MSB of the anonymous object, subtract off the number of
7030 bits from the MSB of the field to the MSB of the
7031 object, and then subtract off the number of bits of
7032 the field itself. The result is the bit offset of
7033 the LSB of the field. */
c906108c
SS
7034 int anonymous_size;
7035 int bit_offset = DW_UNSND (attr);
7036
e142c38c 7037 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7038 if (attr)
7039 {
7040 /* The size of the anonymous object containing
7041 the bit field is explicit, so use the
7042 indicated size (in bytes). */
7043 anonymous_size = DW_UNSND (attr);
7044 }
7045 else
7046 {
7047 /* The size of the anonymous object containing
7048 the bit field must be inferred from the type
7049 attribute of the data member containing the
7050 bit field. */
7051 anonymous_size = TYPE_LENGTH (fp->type);
7052 }
7053 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7054 - bit_offset - FIELD_BITSIZE (*fp);
7055 }
7056 }
7057
7058 /* Get name of field. */
39cbfefa
DJ
7059 fieldname = dwarf2_name (die, cu);
7060 if (fieldname == NULL)
7061 fieldname = "";
d8151005
DJ
7062
7063 /* The name is already allocated along with this objfile, so we don't
7064 need to duplicate it for the type. */
7065 fp->name = fieldname;
c906108c
SS
7066
7067 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7068 pointer or virtual base class pointer) to private. */
e142c38c 7069 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7070 {
d48cc9dd 7071 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7072 new_field->accessibility = DW_ACCESS_private;
7073 fip->non_public_fields = 1;
7074 }
7075 }
a9a9bd0f 7076 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7077 {
a9a9bd0f
DC
7078 /* C++ static member. */
7079
7080 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7081 is a declaration, but all versions of G++ as of this writing
7082 (so through at least 3.2.1) incorrectly generate
7083 DW_TAG_variable tags. */
6e70227d 7084
ff355380 7085 const char *physname;
c906108c 7086
a9a9bd0f 7087 /* Get name of field. */
39cbfefa
DJ
7088 fieldname = dwarf2_name (die, cu);
7089 if (fieldname == NULL)
c906108c
SS
7090 return;
7091
254e6b9e 7092 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7093 if (attr
7094 /* Only create a symbol if this is an external value.
7095 new_symbol checks this and puts the value in the global symbol
7096 table, which we want. If it is not external, new_symbol
7097 will try to put the value in cu->list_in_scope which is wrong. */
7098 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7099 {
7100 /* A static const member, not much different than an enum as far as
7101 we're concerned, except that we can support more types. */
7102 new_symbol (die, NULL, cu);
7103 }
7104
2df3850c 7105 /* Get physical name. */
ff355380 7106 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7107
d8151005
DJ
7108 /* The name is already allocated along with this objfile, so we don't
7109 need to duplicate it for the type. */
7110 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7111 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7112 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7113 }
7114 else if (die->tag == DW_TAG_inheritance)
7115 {
74ac6d43 7116 LONGEST offset;
d4b96c9a 7117
74ac6d43
TT
7118 /* C++ base class field. */
7119 if (handle_data_member_location (die, cu, &offset))
7120 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7121 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7122 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7123 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7124 fip->nbaseclasses++;
7125 }
7126}
7127
98751a41
JK
7128/* Add a typedef defined in the scope of the FIP's class. */
7129
7130static void
7131dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7132 struct dwarf2_cu *cu)
6e70227d 7133{
98751a41 7134 struct objfile *objfile = cu->objfile;
98751a41
JK
7135 struct typedef_field_list *new_field;
7136 struct attribute *attr;
7137 struct typedef_field *fp;
7138 char *fieldname = "";
7139
7140 /* Allocate a new field list entry and link it in. */
7141 new_field = xzalloc (sizeof (*new_field));
7142 make_cleanup (xfree, new_field);
7143
7144 gdb_assert (die->tag == DW_TAG_typedef);
7145
7146 fp = &new_field->field;
7147
7148 /* Get name of field. */
7149 fp->name = dwarf2_name (die, cu);
7150 if (fp->name == NULL)
7151 return;
7152
7153 fp->type = read_type_die (die, cu);
7154
7155 new_field->next = fip->typedef_field_list;
7156 fip->typedef_field_list = new_field;
7157 fip->typedef_field_list_count++;
7158}
7159
c906108c
SS
7160/* Create the vector of fields, and attach it to the type. */
7161
7162static void
fba45db2 7163dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7164 struct dwarf2_cu *cu)
c906108c
SS
7165{
7166 int nfields = fip->nfields;
7167
7168 /* Record the field count, allocate space for the array of fields,
7169 and create blank accessibility bitfields if necessary. */
7170 TYPE_NFIELDS (type) = nfields;
7171 TYPE_FIELDS (type) = (struct field *)
7172 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7173 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7174
b4ba55a1 7175 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7176 {
7177 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7178
7179 TYPE_FIELD_PRIVATE_BITS (type) =
7180 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7181 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7182
7183 TYPE_FIELD_PROTECTED_BITS (type) =
7184 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7185 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7186
774b6a14
TT
7187 TYPE_FIELD_IGNORE_BITS (type) =
7188 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7189 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7190 }
7191
7192 /* If the type has baseclasses, allocate and clear a bit vector for
7193 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7194 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7195 {
7196 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7197 unsigned char *pointer;
c906108c
SS
7198
7199 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7200 pointer = TYPE_ALLOC (type, num_bytes);
7201 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7202 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7203 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7204 }
7205
3e43a32a
MS
7206 /* Copy the saved-up fields into the field vector. Start from the head of
7207 the list, adding to the tail of the field array, so that they end up in
7208 the same order in the array in which they were added to the list. */
c906108c
SS
7209 while (nfields-- > 0)
7210 {
7d0ccb61
DJ
7211 struct nextfield *fieldp;
7212
7213 if (fip->fields)
7214 {
7215 fieldp = fip->fields;
7216 fip->fields = fieldp->next;
7217 }
7218 else
7219 {
7220 fieldp = fip->baseclasses;
7221 fip->baseclasses = fieldp->next;
7222 }
7223
7224 TYPE_FIELD (type, nfields) = fieldp->field;
7225 switch (fieldp->accessibility)
c906108c 7226 {
c5aa993b 7227 case DW_ACCESS_private:
b4ba55a1
JB
7228 if (cu->language != language_ada)
7229 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7230 break;
c906108c 7231
c5aa993b 7232 case DW_ACCESS_protected:
b4ba55a1
JB
7233 if (cu->language != language_ada)
7234 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7235 break;
c906108c 7236
c5aa993b
JM
7237 case DW_ACCESS_public:
7238 break;
c906108c 7239
c5aa993b
JM
7240 default:
7241 /* Unknown accessibility. Complain and treat it as public. */
7242 {
e2e0b3e5 7243 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7244 fieldp->accessibility);
c5aa993b
JM
7245 }
7246 break;
c906108c
SS
7247 }
7248 if (nfields < fip->nbaseclasses)
7249 {
7d0ccb61 7250 switch (fieldp->virtuality)
c906108c 7251 {
c5aa993b
JM
7252 case DW_VIRTUALITY_virtual:
7253 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7254 if (cu->language == language_ada)
a73c6dcd 7255 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7256 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7257 break;
c906108c
SS
7258 }
7259 }
c906108c
SS
7260 }
7261}
7262
c906108c
SS
7263/* Add a member function to the proper fieldlist. */
7264
7265static void
107d2387 7266dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7267 struct type *type, struct dwarf2_cu *cu)
c906108c 7268{
e7c27a73 7269 struct objfile *objfile = cu->objfile;
c906108c
SS
7270 struct attribute *attr;
7271 struct fnfieldlist *flp;
7272 int i;
7273 struct fn_field *fnp;
7274 char *fieldname;
c906108c 7275 struct nextfnfield *new_fnfield;
f792889a 7276 struct type *this_type;
60d5a603 7277 enum dwarf_access_attribute accessibility;
c906108c 7278
b4ba55a1 7279 if (cu->language == language_ada)
a73c6dcd 7280 error (_("unexpected member function in Ada type"));
b4ba55a1 7281
2df3850c 7282 /* Get name of member function. */
39cbfefa
DJ
7283 fieldname = dwarf2_name (die, cu);
7284 if (fieldname == NULL)
2df3850c 7285 return;
c906108c 7286
c906108c
SS
7287 /* Look up member function name in fieldlist. */
7288 for (i = 0; i < fip->nfnfields; i++)
7289 {
27bfe10e 7290 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7291 break;
7292 }
7293
7294 /* Create new list element if necessary. */
7295 if (i < fip->nfnfields)
7296 flp = &fip->fnfieldlists[i];
7297 else
7298 {
7299 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7300 {
7301 fip->fnfieldlists = (struct fnfieldlist *)
7302 xrealloc (fip->fnfieldlists,
7303 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7304 * sizeof (struct fnfieldlist));
c906108c 7305 if (fip->nfnfields == 0)
c13c43fd 7306 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7307 }
7308 flp = &fip->fnfieldlists[fip->nfnfields];
7309 flp->name = fieldname;
7310 flp->length = 0;
7311 flp->head = NULL;
3da10d80 7312 i = fip->nfnfields++;
c906108c
SS
7313 }
7314
7315 /* Create a new member function field and chain it to the field list
0963b4bd 7316 entry. */
c906108c 7317 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7318 make_cleanup (xfree, new_fnfield);
c906108c
SS
7319 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7320 new_fnfield->next = flp->head;
7321 flp->head = new_fnfield;
7322 flp->length++;
7323
7324 /* Fill in the member function field info. */
7325 fnp = &new_fnfield->fnfield;
3da10d80
KS
7326
7327 /* Delay processing of the physname until later. */
7328 if (cu->language == language_cplus || cu->language == language_java)
7329 {
7330 add_to_method_list (type, i, flp->length - 1, fieldname,
7331 die, cu);
7332 }
7333 else
7334 {
1d06ead6 7335 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7336 fnp->physname = physname ? physname : "";
7337 }
7338
c906108c 7339 fnp->type = alloc_type (objfile);
f792889a
DJ
7340 this_type = read_type_die (die, cu);
7341 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7342 {
f792889a 7343 int nparams = TYPE_NFIELDS (this_type);
c906108c 7344
f792889a 7345 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7346 of the method itself (TYPE_CODE_METHOD). */
7347 smash_to_method_type (fnp->type, type,
f792889a
DJ
7348 TYPE_TARGET_TYPE (this_type),
7349 TYPE_FIELDS (this_type),
7350 TYPE_NFIELDS (this_type),
7351 TYPE_VARARGS (this_type));
c906108c
SS
7352
7353 /* Handle static member functions.
c5aa993b 7354 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7355 member functions. G++ helps GDB by marking the first
7356 parameter for non-static member functions (which is the this
7357 pointer) as artificial. We obtain this information from
7358 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7359 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7360 fnp->voffset = VOFFSET_STATIC;
7361 }
7362 else
e2e0b3e5 7363 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7364 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7365
7366 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7367 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7368 fnp->fcontext = die_containing_type (die, cu);
c906108c 7369
3e43a32a
MS
7370 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7371 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7372
7373 /* Get accessibility. */
e142c38c 7374 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7375 if (attr)
60d5a603
JK
7376 accessibility = DW_UNSND (attr);
7377 else
7378 accessibility = dwarf2_default_access_attribute (die, cu);
7379 switch (accessibility)
c906108c 7380 {
60d5a603
JK
7381 case DW_ACCESS_private:
7382 fnp->is_private = 1;
7383 break;
7384 case DW_ACCESS_protected:
7385 fnp->is_protected = 1;
7386 break;
c906108c
SS
7387 }
7388
b02dede2 7389 /* Check for artificial methods. */
e142c38c 7390 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7391 if (attr && DW_UNSND (attr) != 0)
7392 fnp->is_artificial = 1;
7393
0d564a31 7394 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7395 function. For older versions of GCC, this is an offset in the
7396 appropriate virtual table, as specified by DW_AT_containing_type.
7397 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7398 to the object address. */
7399
e142c38c 7400 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7401 if (attr)
8e19ed76 7402 {
aec5aa8b 7403 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7404 {
aec5aa8b
TT
7405 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7406 {
7407 /* Old-style GCC. */
7408 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7409 }
7410 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7411 || (DW_BLOCK (attr)->size > 1
7412 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7413 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7414 {
7415 struct dwarf_block blk;
7416 int offset;
7417
7418 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7419 ? 1 : 2);
7420 blk.size = DW_BLOCK (attr)->size - offset;
7421 blk.data = DW_BLOCK (attr)->data + offset;
7422 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7423 if ((fnp->voffset % cu->header.addr_size) != 0)
7424 dwarf2_complex_location_expr_complaint ();
7425 else
7426 fnp->voffset /= cu->header.addr_size;
7427 fnp->voffset += 2;
7428 }
7429 else
7430 dwarf2_complex_location_expr_complaint ();
7431
7432 if (!fnp->fcontext)
7433 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7434 }
3690dd37 7435 else if (attr_form_is_section_offset (attr))
8e19ed76 7436 {
4d3c2250 7437 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7438 }
7439 else
7440 {
4d3c2250
KB
7441 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7442 fieldname);
8e19ed76 7443 }
0d564a31 7444 }
d48cc9dd
DJ
7445 else
7446 {
7447 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7448 if (attr && DW_UNSND (attr))
7449 {
7450 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7451 complaint (&symfile_complaints,
3e43a32a
MS
7452 _("Member function \"%s\" (offset %d) is virtual "
7453 "but the vtable offset is not specified"),
d48cc9dd 7454 fieldname, die->offset);
9655fd1a 7455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7456 TYPE_CPLUS_DYNAMIC (type) = 1;
7457 }
7458 }
c906108c
SS
7459}
7460
7461/* Create the vector of member function fields, and attach it to the type. */
7462
7463static void
fba45db2 7464dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7465 struct dwarf2_cu *cu)
c906108c
SS
7466{
7467 struct fnfieldlist *flp;
7468 int total_length = 0;
7469 int i;
7470
b4ba55a1 7471 if (cu->language == language_ada)
a73c6dcd 7472 error (_("unexpected member functions in Ada type"));
b4ba55a1 7473
c906108c
SS
7474 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7475 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7476 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7477
7478 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7479 {
7480 struct nextfnfield *nfp = flp->head;
7481 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7482 int k;
7483
7484 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7485 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7486 fn_flp->fn_fields = (struct fn_field *)
7487 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7488 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7489 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7490
7491 total_length += flp->length;
7492 }
7493
7494 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7495 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7496}
7497
1168df01
JB
7498/* Returns non-zero if NAME is the name of a vtable member in CU's
7499 language, zero otherwise. */
7500static int
7501is_vtable_name (const char *name, struct dwarf2_cu *cu)
7502{
7503 static const char vptr[] = "_vptr";
987504bb 7504 static const char vtable[] = "vtable";
1168df01 7505
987504bb
JJ
7506 /* Look for the C++ and Java forms of the vtable. */
7507 if ((cu->language == language_java
7508 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7509 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7510 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7511 return 1;
7512
7513 return 0;
7514}
7515
c0dd20ea 7516/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7517 functions, with the ABI-specified layout. If TYPE describes
7518 such a structure, smash it into a member function type.
61049d3b
DJ
7519
7520 GCC shouldn't do this; it should just output pointer to member DIEs.
7521 This is GCC PR debug/28767. */
c0dd20ea 7522
0b92b5bb
TT
7523static void
7524quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7525{
0b92b5bb 7526 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7527
7528 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7529 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7530 return;
c0dd20ea
DJ
7531
7532 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7533 if (TYPE_FIELD_NAME (type, 0) == NULL
7534 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7535 || TYPE_FIELD_NAME (type, 1) == NULL
7536 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7537 return;
c0dd20ea
DJ
7538
7539 /* Find the type of the method. */
0b92b5bb 7540 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7541 if (pfn_type == NULL
7542 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7543 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7544 return;
c0dd20ea
DJ
7545
7546 /* Look for the "this" argument. */
7547 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7548 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7549 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7550 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7551 return;
c0dd20ea
DJ
7552
7553 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7554 new_type = alloc_type (objfile);
7555 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7556 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7557 TYPE_VARARGS (pfn_type));
0b92b5bb 7558 smash_to_methodptr_type (type, new_type);
c0dd20ea 7559}
1168df01 7560
c906108c 7561/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7562 (definition) to create a type for the structure or union. Fill in
7563 the type's name and general properties; the members will not be
7564 processed until process_structure_type.
c906108c 7565
c767944b
DJ
7566 NOTE: we need to call these functions regardless of whether or not the
7567 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7568 structure or union. This gets the type entered into our set of
7569 user defined types.
7570
7571 However, if the structure is incomplete (an opaque struct/union)
7572 then suppress creating a symbol table entry for it since gdb only
7573 wants to find the one with the complete definition. Note that if
7574 it is complete, we just call new_symbol, which does it's own
7575 checking about whether the struct/union is anonymous or not (and
7576 suppresses creating a symbol table entry itself). */
7577
f792889a 7578static struct type *
134d01f1 7579read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7580{
e7c27a73 7581 struct objfile *objfile = cu->objfile;
c906108c
SS
7582 struct type *type;
7583 struct attribute *attr;
39cbfefa 7584 char *name;
c906108c 7585
348e048f
DE
7586 /* If the definition of this type lives in .debug_types, read that type.
7587 Don't follow DW_AT_specification though, that will take us back up
7588 the chain and we want to go down. */
7589 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7590 if (attr)
7591 {
7592 struct dwarf2_cu *type_cu = cu;
7593 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7594
348e048f
DE
7595 /* We could just recurse on read_structure_type, but we need to call
7596 get_die_type to ensure only one type for this DIE is created.
7597 This is important, for example, because for c++ classes we need
7598 TYPE_NAME set which is only done by new_symbol. Blech. */
7599 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7600
7601 /* TYPE_CU may not be the same as CU.
7602 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7603 return set_die_type (die, type, cu);
7604 }
7605
c0dd20ea 7606 type = alloc_type (objfile);
c906108c 7607 INIT_CPLUS_SPECIFIC (type);
93311388 7608
39cbfefa
DJ
7609 name = dwarf2_name (die, cu);
7610 if (name != NULL)
c906108c 7611 {
987504bb
JJ
7612 if (cu->language == language_cplus
7613 || cu->language == language_java)
63d06c5c 7614 {
3da10d80
KS
7615 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7616
7617 /* dwarf2_full_name might have already finished building the DIE's
7618 type. If so, there is no need to continue. */
7619 if (get_die_type (die, cu) != NULL)
7620 return get_die_type (die, cu);
7621
7622 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7623 if (die->tag == DW_TAG_structure_type
7624 || die->tag == DW_TAG_class_type)
7625 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7626 }
7627 else
7628 {
d8151005
DJ
7629 /* The name is already allocated along with this objfile, so
7630 we don't need to duplicate it for the type. */
94af9270
KS
7631 TYPE_TAG_NAME (type) = (char *) name;
7632 if (die->tag == DW_TAG_class_type)
7633 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7634 }
c906108c
SS
7635 }
7636
7637 if (die->tag == DW_TAG_structure_type)
7638 {
7639 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7640 }
7641 else if (die->tag == DW_TAG_union_type)
7642 {
7643 TYPE_CODE (type) = TYPE_CODE_UNION;
7644 }
7645 else
7646 {
c906108c
SS
7647 TYPE_CODE (type) = TYPE_CODE_CLASS;
7648 }
7649
0cc2414c
TT
7650 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7651 TYPE_DECLARED_CLASS (type) = 1;
7652
e142c38c 7653 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7654 if (attr)
7655 {
7656 TYPE_LENGTH (type) = DW_UNSND (attr);
7657 }
7658 else
7659 {
7660 TYPE_LENGTH (type) = 0;
7661 }
7662
876cecd0 7663 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7664 if (die_is_declaration (die, cu))
876cecd0 7665 TYPE_STUB (type) = 1;
a6c727b2
DJ
7666 else if (attr == NULL && die->child == NULL
7667 && producer_is_realview (cu->producer))
7668 /* RealView does not output the required DW_AT_declaration
7669 on incomplete types. */
7670 TYPE_STUB (type) = 1;
dc718098 7671
c906108c
SS
7672 /* We need to add the type field to the die immediately so we don't
7673 infinitely recurse when dealing with pointers to the structure
0963b4bd 7674 type within the structure itself. */
1c379e20 7675 set_die_type (die, type, cu);
c906108c 7676
7e314c57
JK
7677 /* set_die_type should be already done. */
7678 set_descriptive_type (type, die, cu);
7679
c767944b
DJ
7680 return type;
7681}
7682
7683/* Finish creating a structure or union type, including filling in
7684 its members and creating a symbol for it. */
7685
7686static void
7687process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7688{
7689 struct objfile *objfile = cu->objfile;
7690 struct die_info *child_die = die->child;
7691 struct type *type;
7692
7693 type = get_die_type (die, cu);
7694 if (type == NULL)
7695 type = read_structure_type (die, cu);
7696
e142c38c 7697 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7698 {
7699 struct field_info fi;
7700 struct die_info *child_die;
34eaf542 7701 VEC (symbolp) *template_args = NULL;
c767944b 7702 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7703
7704 memset (&fi, 0, sizeof (struct field_info));
7705
639d11d3 7706 child_die = die->child;
c906108c
SS
7707
7708 while (child_die && child_die->tag)
7709 {
a9a9bd0f
DC
7710 if (child_die->tag == DW_TAG_member
7711 || child_die->tag == DW_TAG_variable)
c906108c 7712 {
a9a9bd0f
DC
7713 /* NOTE: carlton/2002-11-05: A C++ static data member
7714 should be a DW_TAG_member that is a declaration, but
7715 all versions of G++ as of this writing (so through at
7716 least 3.2.1) incorrectly generate DW_TAG_variable
7717 tags for them instead. */
e7c27a73 7718 dwarf2_add_field (&fi, child_die, cu);
c906108c 7719 }
8713b1b1 7720 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7721 {
0963b4bd 7722 /* C++ member function. */
e7c27a73 7723 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7724 }
7725 else if (child_die->tag == DW_TAG_inheritance)
7726 {
7727 /* C++ base class field. */
e7c27a73 7728 dwarf2_add_field (&fi, child_die, cu);
c906108c 7729 }
98751a41
JK
7730 else if (child_die->tag == DW_TAG_typedef)
7731 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7732 else if (child_die->tag == DW_TAG_template_type_param
7733 || child_die->tag == DW_TAG_template_value_param)
7734 {
7735 struct symbol *arg = new_symbol (child_die, NULL, cu);
7736
f1078f66
DJ
7737 if (arg != NULL)
7738 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7739 }
7740
c906108c
SS
7741 child_die = sibling_die (child_die);
7742 }
7743
34eaf542
TT
7744 /* Attach template arguments to type. */
7745 if (! VEC_empty (symbolp, template_args))
7746 {
7747 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7748 TYPE_N_TEMPLATE_ARGUMENTS (type)
7749 = VEC_length (symbolp, template_args);
7750 TYPE_TEMPLATE_ARGUMENTS (type)
7751 = obstack_alloc (&objfile->objfile_obstack,
7752 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7753 * sizeof (struct symbol *)));
7754 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7755 VEC_address (symbolp, template_args),
7756 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7757 * sizeof (struct symbol *)));
7758 VEC_free (symbolp, template_args);
7759 }
7760
c906108c
SS
7761 /* Attach fields and member functions to the type. */
7762 if (fi.nfields)
e7c27a73 7763 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7764 if (fi.nfnfields)
7765 {
e7c27a73 7766 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7767
c5aa993b 7768 /* Get the type which refers to the base class (possibly this
c906108c 7769 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7770 class from the DW_AT_containing_type attribute. This use of
7771 DW_AT_containing_type is a GNU extension. */
c906108c 7772
e142c38c 7773 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7774 {
e7c27a73 7775 struct type *t = die_containing_type (die, cu);
c906108c
SS
7776
7777 TYPE_VPTR_BASETYPE (type) = t;
7778 if (type == t)
7779 {
c906108c
SS
7780 int i;
7781
7782 /* Our own class provides vtbl ptr. */
7783 for (i = TYPE_NFIELDS (t) - 1;
7784 i >= TYPE_N_BASECLASSES (t);
7785 --i)
7786 {
7787 char *fieldname = TYPE_FIELD_NAME (t, i);
7788
1168df01 7789 if (is_vtable_name (fieldname, cu))
c906108c
SS
7790 {
7791 TYPE_VPTR_FIELDNO (type) = i;
7792 break;
7793 }
7794 }
7795
7796 /* Complain if virtual function table field not found. */
7797 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7798 complaint (&symfile_complaints,
3e43a32a
MS
7799 _("virtual function table pointer "
7800 "not found when defining class '%s'"),
4d3c2250
KB
7801 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7802 "");
c906108c
SS
7803 }
7804 else
7805 {
7806 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7807 }
7808 }
f6235d4c
EZ
7809 else if (cu->producer
7810 && strncmp (cu->producer,
7811 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7812 {
7813 /* The IBM XLC compiler does not provide direct indication
7814 of the containing type, but the vtable pointer is
7815 always named __vfp. */
7816
7817 int i;
7818
7819 for (i = TYPE_NFIELDS (type) - 1;
7820 i >= TYPE_N_BASECLASSES (type);
7821 --i)
7822 {
7823 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7824 {
7825 TYPE_VPTR_FIELDNO (type) = i;
7826 TYPE_VPTR_BASETYPE (type) = type;
7827 break;
7828 }
7829 }
7830 }
c906108c 7831 }
98751a41
JK
7832
7833 /* Copy fi.typedef_field_list linked list elements content into the
7834 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7835 if (fi.typedef_field_list)
7836 {
7837 int i = fi.typedef_field_list_count;
7838
a0d7a4ff 7839 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7840 TYPE_TYPEDEF_FIELD_ARRAY (type)
7841 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7842 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7843
7844 /* Reverse the list order to keep the debug info elements order. */
7845 while (--i >= 0)
7846 {
7847 struct typedef_field *dest, *src;
6e70227d 7848
98751a41
JK
7849 dest = &TYPE_TYPEDEF_FIELD (type, i);
7850 src = &fi.typedef_field_list->field;
7851 fi.typedef_field_list = fi.typedef_field_list->next;
7852 *dest = *src;
7853 }
7854 }
c767944b
DJ
7855
7856 do_cleanups (back_to);
eb2a6f42
TT
7857
7858 if (HAVE_CPLUS_STRUCT (type))
7859 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7860 }
63d06c5c 7861
bb5ed363 7862 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7863
90aeadfc
DC
7864 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7865 snapshots) has been known to create a die giving a declaration
7866 for a class that has, as a child, a die giving a definition for a
7867 nested class. So we have to process our children even if the
7868 current die is a declaration. Normally, of course, a declaration
7869 won't have any children at all. */
134d01f1 7870
90aeadfc
DC
7871 while (child_die != NULL && child_die->tag)
7872 {
7873 if (child_die->tag == DW_TAG_member
7874 || child_die->tag == DW_TAG_variable
34eaf542
TT
7875 || child_die->tag == DW_TAG_inheritance
7876 || child_die->tag == DW_TAG_template_value_param
7877 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7878 {
90aeadfc 7879 /* Do nothing. */
134d01f1 7880 }
90aeadfc
DC
7881 else
7882 process_die (child_die, cu);
134d01f1 7883
90aeadfc 7884 child_die = sibling_die (child_die);
134d01f1
DJ
7885 }
7886
fa4028e9
JB
7887 /* Do not consider external references. According to the DWARF standard,
7888 these DIEs are identified by the fact that they have no byte_size
7889 attribute, and a declaration attribute. */
7890 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7891 || !die_is_declaration (die, cu))
c767944b 7892 new_symbol (die, type, cu);
134d01f1
DJ
7893}
7894
7895/* Given a DW_AT_enumeration_type die, set its type. We do not
7896 complete the type's fields yet, or create any symbols. */
c906108c 7897
f792889a 7898static struct type *
134d01f1 7899read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7900{
e7c27a73 7901 struct objfile *objfile = cu->objfile;
c906108c 7902 struct type *type;
c906108c 7903 struct attribute *attr;
0114d602 7904 const char *name;
134d01f1 7905
348e048f
DE
7906 /* If the definition of this type lives in .debug_types, read that type.
7907 Don't follow DW_AT_specification though, that will take us back up
7908 the chain and we want to go down. */
7909 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7910 if (attr)
7911 {
7912 struct dwarf2_cu *type_cu = cu;
7913 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7914
348e048f 7915 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7916
7917 /* TYPE_CU may not be the same as CU.
7918 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7919 return set_die_type (die, type, cu);
7920 }
7921
c906108c
SS
7922 type = alloc_type (objfile);
7923
7924 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7925 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7926 if (name != NULL)
0114d602 7927 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7928
e142c38c 7929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7930 if (attr)
7931 {
7932 TYPE_LENGTH (type) = DW_UNSND (attr);
7933 }
7934 else
7935 {
7936 TYPE_LENGTH (type) = 0;
7937 }
7938
137033e9
JB
7939 /* The enumeration DIE can be incomplete. In Ada, any type can be
7940 declared as private in the package spec, and then defined only
7941 inside the package body. Such types are known as Taft Amendment
7942 Types. When another package uses such a type, an incomplete DIE
7943 may be generated by the compiler. */
02eb380e 7944 if (die_is_declaration (die, cu))
876cecd0 7945 TYPE_STUB (type) = 1;
02eb380e 7946
f792889a 7947 return set_die_type (die, type, cu);
134d01f1
DJ
7948}
7949
7950/* Given a pointer to a die which begins an enumeration, process all
7951 the dies that define the members of the enumeration, and create the
7952 symbol for the enumeration type.
7953
7954 NOTE: We reverse the order of the element list. */
7955
7956static void
7957process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7958{
f792889a 7959 struct type *this_type;
134d01f1 7960
f792889a
DJ
7961 this_type = get_die_type (die, cu);
7962 if (this_type == NULL)
7963 this_type = read_enumeration_type (die, cu);
9dc481d3 7964
639d11d3 7965 if (die->child != NULL)
c906108c 7966 {
9dc481d3
DE
7967 struct die_info *child_die;
7968 struct symbol *sym;
7969 struct field *fields = NULL;
7970 int num_fields = 0;
7971 int unsigned_enum = 1;
7972 char *name;
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
SS
7987 if (SYMBOL_VALUE (sym) < 0)
7988 unsigned_enum = 0;
7989
7990 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7991 {
7992 fields = (struct field *)
7993 xrealloc (fields,
7994 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7995 * sizeof (struct field));
c906108c
SS
7996 }
7997
3567439c 7998 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7999 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8000 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8001 FIELD_BITSIZE (fields[num_fields]) = 0;
8002
8003 num_fields++;
8004 }
8005 }
8006
8007 child_die = sibling_die (child_die);
8008 }
8009
8010 if (num_fields)
8011 {
f792889a
DJ
8012 TYPE_NFIELDS (this_type) = num_fields;
8013 TYPE_FIELDS (this_type) = (struct field *)
8014 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8015 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8016 sizeof (struct field) * num_fields);
b8c9b27d 8017 xfree (fields);
c906108c
SS
8018 }
8019 if (unsigned_enum)
876cecd0 8020 TYPE_UNSIGNED (this_type) = 1;
c906108c 8021 }
134d01f1 8022
6c83ed52
TT
8023 /* If we are reading an enum from a .debug_types unit, and the enum
8024 is a declaration, and the enum is not the signatured type in the
8025 unit, then we do not want to add a symbol for it. Adding a
8026 symbol would in some cases obscure the true definition of the
8027 enum, giving users an incomplete type when the definition is
8028 actually available. Note that we do not want to do this for all
8029 enums which are just declarations, because C++0x allows forward
8030 enum declarations. */
b0df02fd 8031 if (cu->per_cu->debug_types_section
6c83ed52
TT
8032 && die_is_declaration (die, cu))
8033 {
8034 struct signatured_type *type_sig;
8035
8036 type_sig
8037 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8038 cu->per_cu->debug_types_section,
6c83ed52
TT
8039 cu->per_cu->offset);
8040 if (type_sig->type_offset != die->offset)
8041 return;
8042 }
8043
f792889a 8044 new_symbol (die, this_type, cu);
c906108c
SS
8045}
8046
8047/* Extract all information from a DW_TAG_array_type DIE and put it in
8048 the DIE's type field. For now, this only handles one dimensional
8049 arrays. */
8050
f792889a 8051static struct type *
e7c27a73 8052read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8053{
e7c27a73 8054 struct objfile *objfile = cu->objfile;
c906108c 8055 struct die_info *child_die;
7e314c57 8056 struct type *type;
c906108c
SS
8057 struct type *element_type, *range_type, *index_type;
8058 struct type **range_types = NULL;
8059 struct attribute *attr;
8060 int ndim = 0;
8061 struct cleanup *back_to;
39cbfefa 8062 char *name;
c906108c 8063
e7c27a73 8064 element_type = die_type (die, cu);
c906108c 8065
7e314c57
JK
8066 /* The die_type call above may have already set the type for this DIE. */
8067 type = get_die_type (die, cu);
8068 if (type)
8069 return type;
8070
c906108c
SS
8071 /* Irix 6.2 native cc creates array types without children for
8072 arrays with unspecified length. */
639d11d3 8073 if (die->child == NULL)
c906108c 8074 {
46bf5051 8075 index_type = objfile_type (objfile)->builtin_int;
c906108c 8076 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8077 type = create_array_type (NULL, element_type, range_type);
8078 return set_die_type (die, type, cu);
c906108c
SS
8079 }
8080
8081 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8082 child_die = die->child;
c906108c
SS
8083 while (child_die && child_die->tag)
8084 {
8085 if (child_die->tag == DW_TAG_subrange_type)
8086 {
f792889a 8087 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8088
f792889a 8089 if (child_type != NULL)
a02abb62 8090 {
0963b4bd
MS
8091 /* The range type was succesfully read. Save it for the
8092 array type creation. */
a02abb62
JB
8093 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8094 {
8095 range_types = (struct type **)
8096 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8097 * sizeof (struct type *));
8098 if (ndim == 0)
8099 make_cleanup (free_current_contents, &range_types);
8100 }
f792889a 8101 range_types[ndim++] = child_type;
a02abb62 8102 }
c906108c
SS
8103 }
8104 child_die = sibling_die (child_die);
8105 }
8106
8107 /* Dwarf2 dimensions are output from left to right, create the
8108 necessary array types in backwards order. */
7ca2d3a3 8109
c906108c 8110 type = element_type;
7ca2d3a3
DL
8111
8112 if (read_array_order (die, cu) == DW_ORD_col_major)
8113 {
8114 int i = 0;
9a619af0 8115
7ca2d3a3
DL
8116 while (i < ndim)
8117 type = create_array_type (NULL, type, range_types[i++]);
8118 }
8119 else
8120 {
8121 while (ndim-- > 0)
8122 type = create_array_type (NULL, type, range_types[ndim]);
8123 }
c906108c 8124
f5f8a009
EZ
8125 /* Understand Dwarf2 support for vector types (like they occur on
8126 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8127 array type. This is not part of the Dwarf2/3 standard yet, but a
8128 custom vendor extension. The main difference between a regular
8129 array and the vector variant is that vectors are passed by value
8130 to functions. */
e142c38c 8131 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8132 if (attr)
ea37ba09 8133 make_vector_type (type);
f5f8a009 8134
dbc98a8b
KW
8135 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8136 implementation may choose to implement triple vectors using this
8137 attribute. */
8138 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8139 if (attr)
8140 {
8141 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8142 TYPE_LENGTH (type) = DW_UNSND (attr);
8143 else
3e43a32a
MS
8144 complaint (&symfile_complaints,
8145 _("DW_AT_byte_size for array type smaller "
8146 "than the total size of elements"));
dbc98a8b
KW
8147 }
8148
39cbfefa
DJ
8149 name = dwarf2_name (die, cu);
8150 if (name)
8151 TYPE_NAME (type) = name;
6e70227d 8152
0963b4bd 8153 /* Install the type in the die. */
7e314c57
JK
8154 set_die_type (die, type, cu);
8155
8156 /* set_die_type should be already done. */
b4ba55a1
JB
8157 set_descriptive_type (type, die, cu);
8158
c906108c
SS
8159 do_cleanups (back_to);
8160
7e314c57 8161 return type;
c906108c
SS
8162}
8163
7ca2d3a3 8164static enum dwarf_array_dim_ordering
6e70227d 8165read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8166{
8167 struct attribute *attr;
8168
8169 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8170
8171 if (attr) return DW_SND (attr);
8172
0963b4bd
MS
8173 /* GNU F77 is a special case, as at 08/2004 array type info is the
8174 opposite order to the dwarf2 specification, but data is still
8175 laid out as per normal fortran.
7ca2d3a3 8176
0963b4bd
MS
8177 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8178 version checking. */
7ca2d3a3 8179
905e0470
PM
8180 if (cu->language == language_fortran
8181 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8182 {
8183 return DW_ORD_row_major;
8184 }
8185
6e70227d 8186 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8187 {
8188 case array_column_major:
8189 return DW_ORD_col_major;
8190 case array_row_major:
8191 default:
8192 return DW_ORD_row_major;
8193 };
8194}
8195
72019c9c 8196/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8197 the DIE's type field. */
72019c9c 8198
f792889a 8199static struct type *
72019c9c
GM
8200read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8201{
7e314c57
JK
8202 struct type *domain_type, *set_type;
8203 struct attribute *attr;
f792889a 8204
7e314c57
JK
8205 domain_type = die_type (die, cu);
8206
8207 /* The die_type call above may have already set the type for this DIE. */
8208 set_type = get_die_type (die, cu);
8209 if (set_type)
8210 return set_type;
8211
8212 set_type = create_set_type (NULL, domain_type);
8213
8214 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8215 if (attr)
8216 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8217
f792889a 8218 return set_die_type (die, set_type, cu);
72019c9c 8219}
7ca2d3a3 8220
c906108c
SS
8221/* First cut: install each common block member as a global variable. */
8222
8223static void
e7c27a73 8224read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8225{
8226 struct die_info *child_die;
8227 struct attribute *attr;
8228 struct symbol *sym;
8229 CORE_ADDR base = (CORE_ADDR) 0;
8230
e142c38c 8231 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8232 if (attr)
8233 {
0963b4bd 8234 /* Support the .debug_loc offsets. */
8e19ed76
PS
8235 if (attr_form_is_block (attr))
8236 {
e7c27a73 8237 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8238 }
3690dd37 8239 else if (attr_form_is_section_offset (attr))
8e19ed76 8240 {
4d3c2250 8241 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8242 }
8243 else
8244 {
4d3c2250
KB
8245 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8246 "common block member");
8e19ed76 8247 }
c906108c 8248 }
639d11d3 8249 if (die->child != NULL)
c906108c 8250 {
639d11d3 8251 child_die = die->child;
c906108c
SS
8252 while (child_die && child_die->tag)
8253 {
74ac6d43
TT
8254 LONGEST offset;
8255
e7c27a73 8256 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8257 if (sym != NULL
8258 && handle_data_member_location (child_die, cu, &offset))
c906108c 8259 {
74ac6d43 8260 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8261 add_symbol_to_list (sym, &global_symbols);
8262 }
8263 child_die = sibling_die (child_die);
8264 }
8265 }
8266}
8267
0114d602 8268/* Create a type for a C++ namespace. */
d9fa45fe 8269
0114d602
DJ
8270static struct type *
8271read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8272{
e7c27a73 8273 struct objfile *objfile = cu->objfile;
0114d602 8274 const char *previous_prefix, *name;
9219021c 8275 int is_anonymous;
0114d602
DJ
8276 struct type *type;
8277
8278 /* For extensions, reuse the type of the original namespace. */
8279 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8280 {
8281 struct die_info *ext_die;
8282 struct dwarf2_cu *ext_cu = cu;
9a619af0 8283
0114d602
DJ
8284 ext_die = dwarf2_extension (die, &ext_cu);
8285 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8286
8287 /* EXT_CU may not be the same as CU.
8288 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8289 return set_die_type (die, type, cu);
8290 }
9219021c 8291
e142c38c 8292 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8293
8294 /* Now build the name of the current namespace. */
8295
0114d602
DJ
8296 previous_prefix = determine_prefix (die, cu);
8297 if (previous_prefix[0] != '\0')
8298 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8299 previous_prefix, name, 0, cu);
0114d602
DJ
8300
8301 /* Create the type. */
8302 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8303 objfile);
8304 TYPE_NAME (type) = (char *) name;
8305 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8306
60531b24 8307 return set_die_type (die, type, cu);
0114d602
DJ
8308}
8309
8310/* Read a C++ namespace. */
8311
8312static void
8313read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8314{
8315 struct objfile *objfile = cu->objfile;
0114d602 8316 int is_anonymous;
9219021c 8317
5c4e30ca
DC
8318 /* Add a symbol associated to this if we haven't seen the namespace
8319 before. Also, add a using directive if it's an anonymous
8320 namespace. */
9219021c 8321
f2f0e013 8322 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8323 {
8324 struct type *type;
8325
0114d602 8326 type = read_type_die (die, cu);
e7c27a73 8327 new_symbol (die, type, cu);
5c4e30ca 8328
e8e80198 8329 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8330 if (is_anonymous)
0114d602
DJ
8331 {
8332 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8333
c0cc3a76 8334 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8335 NULL, NULL, &objfile->objfile_obstack);
0114d602 8336 }
5c4e30ca 8337 }
9219021c 8338
639d11d3 8339 if (die->child != NULL)
d9fa45fe 8340 {
639d11d3 8341 struct die_info *child_die = die->child;
6e70227d 8342
d9fa45fe
DC
8343 while (child_die && child_die->tag)
8344 {
e7c27a73 8345 process_die (child_die, cu);
d9fa45fe
DC
8346 child_die = sibling_die (child_die);
8347 }
8348 }
38d518c9
EZ
8349}
8350
f55ee35c
JK
8351/* Read a Fortran module as type. This DIE can be only a declaration used for
8352 imported module. Still we need that type as local Fortran "use ... only"
8353 declaration imports depend on the created type in determine_prefix. */
8354
8355static struct type *
8356read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8357{
8358 struct objfile *objfile = cu->objfile;
8359 char *module_name;
8360 struct type *type;
8361
8362 module_name = dwarf2_name (die, cu);
8363 if (!module_name)
3e43a32a
MS
8364 complaint (&symfile_complaints,
8365 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8366 die->offset);
8367 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8368
8369 /* determine_prefix uses TYPE_TAG_NAME. */
8370 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8371
8372 return set_die_type (die, type, cu);
8373}
8374
5d7cb8df
JK
8375/* Read a Fortran module. */
8376
8377static void
8378read_module (struct die_info *die, struct dwarf2_cu *cu)
8379{
8380 struct die_info *child_die = die->child;
8381
5d7cb8df
JK
8382 while (child_die && child_die->tag)
8383 {
8384 process_die (child_die, cu);
8385 child_die = sibling_die (child_die);
8386 }
8387}
8388
38d518c9
EZ
8389/* Return the name of the namespace represented by DIE. Set
8390 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8391 namespace. */
8392
8393static const char *
e142c38c 8394namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8395{
8396 struct die_info *current_die;
8397 const char *name = NULL;
8398
8399 /* Loop through the extensions until we find a name. */
8400
8401 for (current_die = die;
8402 current_die != NULL;
f2f0e013 8403 current_die = dwarf2_extension (die, &cu))
38d518c9 8404 {
e142c38c 8405 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8406 if (name != NULL)
8407 break;
8408 }
8409
8410 /* Is it an anonymous namespace? */
8411
8412 *is_anonymous = (name == NULL);
8413 if (*is_anonymous)
2b1dbab0 8414 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8415
8416 return name;
d9fa45fe
DC
8417}
8418
c906108c
SS
8419/* Extract all information from a DW_TAG_pointer_type DIE and add to
8420 the user defined type vector. */
8421
f792889a 8422static struct type *
e7c27a73 8423read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8424{
5e2b427d 8425 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8426 struct comp_unit_head *cu_header = &cu->header;
c906108c 8427 struct type *type;
8b2dbe47
KB
8428 struct attribute *attr_byte_size;
8429 struct attribute *attr_address_class;
8430 int byte_size, addr_class;
7e314c57
JK
8431 struct type *target_type;
8432
8433 target_type = die_type (die, cu);
c906108c 8434
7e314c57
JK
8435 /* The die_type call above may have already set the type for this DIE. */
8436 type = get_die_type (die, cu);
8437 if (type)
8438 return type;
8439
8440 type = lookup_pointer_type (target_type);
8b2dbe47 8441
e142c38c 8442 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8443 if (attr_byte_size)
8444 byte_size = DW_UNSND (attr_byte_size);
c906108c 8445 else
8b2dbe47
KB
8446 byte_size = cu_header->addr_size;
8447
e142c38c 8448 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8449 if (attr_address_class)
8450 addr_class = DW_UNSND (attr_address_class);
8451 else
8452 addr_class = DW_ADDR_none;
8453
8454 /* If the pointer size or address class is different than the
8455 default, create a type variant marked as such and set the
8456 length accordingly. */
8457 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8458 {
5e2b427d 8459 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8460 {
8461 int type_flags;
8462
849957d9 8463 type_flags = gdbarch_address_class_type_flags
5e2b427d 8464 (gdbarch, byte_size, addr_class);
876cecd0
TT
8465 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8466 == 0);
8b2dbe47
KB
8467 type = make_type_with_address_space (type, type_flags);
8468 }
8469 else if (TYPE_LENGTH (type) != byte_size)
8470 {
3e43a32a
MS
8471 complaint (&symfile_complaints,
8472 _("invalid pointer size %d"), byte_size);
8b2dbe47 8473 }
6e70227d 8474 else
9a619af0
MS
8475 {
8476 /* Should we also complain about unhandled address classes? */
8477 }
c906108c 8478 }
8b2dbe47
KB
8479
8480 TYPE_LENGTH (type) = byte_size;
f792889a 8481 return set_die_type (die, type, cu);
c906108c
SS
8482}
8483
8484/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8485 the user defined type vector. */
8486
f792889a 8487static struct type *
e7c27a73 8488read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8489{
8490 struct type *type;
8491 struct type *to_type;
8492 struct type *domain;
8493
e7c27a73
DJ
8494 to_type = die_type (die, cu);
8495 domain = die_containing_type (die, cu);
0d5de010 8496
7e314c57
JK
8497 /* The calls above may have already set the type for this DIE. */
8498 type = get_die_type (die, cu);
8499 if (type)
8500 return type;
8501
0d5de010
DJ
8502 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8503 type = lookup_methodptr_type (to_type);
8504 else
8505 type = lookup_memberptr_type (to_type, domain);
c906108c 8506
f792889a 8507 return set_die_type (die, type, cu);
c906108c
SS
8508}
8509
8510/* Extract all information from a DW_TAG_reference_type DIE and add to
8511 the user defined type vector. */
8512
f792889a 8513static struct type *
e7c27a73 8514read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8515{
e7c27a73 8516 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8517 struct type *type, *target_type;
c906108c
SS
8518 struct attribute *attr;
8519
7e314c57
JK
8520 target_type = die_type (die, cu);
8521
8522 /* The die_type call above may have already set the type for this DIE. */
8523 type = get_die_type (die, cu);
8524 if (type)
8525 return type;
8526
8527 type = lookup_reference_type (target_type);
e142c38c 8528 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8529 if (attr)
8530 {
8531 TYPE_LENGTH (type) = DW_UNSND (attr);
8532 }
8533 else
8534 {
107d2387 8535 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8536 }
f792889a 8537 return set_die_type (die, type, cu);
c906108c
SS
8538}
8539
f792889a 8540static struct type *
e7c27a73 8541read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8542{
f792889a 8543 struct type *base_type, *cv_type;
c906108c 8544
e7c27a73 8545 base_type = die_type (die, cu);
7e314c57
JK
8546
8547 /* The die_type call above may have already set the type for this DIE. */
8548 cv_type = get_die_type (die, cu);
8549 if (cv_type)
8550 return cv_type;
8551
2f608a3a
KW
8552 /* In case the const qualifier is applied to an array type, the element type
8553 is so qualified, not the array type (section 6.7.3 of C99). */
8554 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8555 {
8556 struct type *el_type, *inner_array;
8557
8558 base_type = copy_type (base_type);
8559 inner_array = base_type;
8560
8561 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8562 {
8563 TYPE_TARGET_TYPE (inner_array) =
8564 copy_type (TYPE_TARGET_TYPE (inner_array));
8565 inner_array = TYPE_TARGET_TYPE (inner_array);
8566 }
8567
8568 el_type = TYPE_TARGET_TYPE (inner_array);
8569 TYPE_TARGET_TYPE (inner_array) =
8570 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8571
8572 return set_die_type (die, base_type, cu);
8573 }
8574
f792889a
DJ
8575 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8576 return set_die_type (die, cv_type, cu);
c906108c
SS
8577}
8578
f792889a 8579static struct type *
e7c27a73 8580read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8581{
f792889a 8582 struct type *base_type, *cv_type;
c906108c 8583
e7c27a73 8584 base_type = die_type (die, cu);
7e314c57
JK
8585
8586 /* The die_type call above may have already set the type for this DIE. */
8587 cv_type = get_die_type (die, cu);
8588 if (cv_type)
8589 return cv_type;
8590
f792889a
DJ
8591 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8592 return set_die_type (die, cv_type, cu);
c906108c
SS
8593}
8594
8595/* Extract all information from a DW_TAG_string_type DIE and add to
8596 the user defined type vector. It isn't really a user defined type,
8597 but it behaves like one, with other DIE's using an AT_user_def_type
8598 attribute to reference it. */
8599
f792889a 8600static struct type *
e7c27a73 8601read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8602{
e7c27a73 8603 struct objfile *objfile = cu->objfile;
3b7538c0 8604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8605 struct type *type, *range_type, *index_type, *char_type;
8606 struct attribute *attr;
8607 unsigned int length;
8608
e142c38c 8609 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8610 if (attr)
8611 {
8612 length = DW_UNSND (attr);
8613 }
8614 else
8615 {
0963b4bd 8616 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8617 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8618 if (attr)
8619 {
8620 length = DW_UNSND (attr);
8621 }
8622 else
8623 {
8624 length = 1;
8625 }
c906108c 8626 }
6ccb9162 8627
46bf5051 8628 index_type = objfile_type (objfile)->builtin_int;
c906108c 8629 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8630 char_type = language_string_char_type (cu->language_defn, gdbarch);
8631 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8632
f792889a 8633 return set_die_type (die, type, cu);
c906108c
SS
8634}
8635
8636/* Handle DIES due to C code like:
8637
8638 struct foo
c5aa993b
JM
8639 {
8640 int (*funcp)(int a, long l);
8641 int b;
8642 };
c906108c 8643
0963b4bd 8644 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8645
f792889a 8646static struct type *
e7c27a73 8647read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8648{
bb5ed363 8649 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8650 struct type *type; /* Type that this function returns. */
8651 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8652 struct attribute *attr;
8653
e7c27a73 8654 type = die_type (die, cu);
7e314c57
JK
8655
8656 /* The die_type call above may have already set the type for this DIE. */
8657 ftype = get_die_type (die, cu);
8658 if (ftype)
8659 return ftype;
8660
0c8b41f1 8661 ftype = lookup_function_type (type);
c906108c 8662
5b8101ae 8663 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8664 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8665 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8666 || cu->language == language_cplus
5b8101ae
PM
8667 || cu->language == language_java
8668 || cu->language == language_pascal)
876cecd0 8669 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8670 else if (producer_is_realview (cu->producer))
8671 /* RealView does not emit DW_AT_prototyped. We can not
8672 distinguish prototyped and unprototyped functions; default to
8673 prototyped, since that is more common in modern code (and
8674 RealView warns about unprototyped functions). */
8675 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8676
c055b101
CV
8677 /* Store the calling convention in the type if it's available in
8678 the subroutine die. Otherwise set the calling convention to
8679 the default value DW_CC_normal. */
8680 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8681 if (attr)
8682 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8683 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8684 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8685 else
8686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8687
8688 /* We need to add the subroutine type to the die immediately so
8689 we don't infinitely recurse when dealing with parameters
0963b4bd 8690 declared as the same subroutine type. */
76c10ea2 8691 set_die_type (die, ftype, cu);
6e70227d 8692
639d11d3 8693 if (die->child != NULL)
c906108c 8694 {
bb5ed363 8695 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8696 struct die_info *child_die;
8072405b 8697 int nparams, iparams;
c906108c
SS
8698
8699 /* Count the number of parameters.
8700 FIXME: GDB currently ignores vararg functions, but knows about
8701 vararg member functions. */
8072405b 8702 nparams = 0;
639d11d3 8703 child_die = die->child;
c906108c
SS
8704 while (child_die && child_die->tag)
8705 {
8706 if (child_die->tag == DW_TAG_formal_parameter)
8707 nparams++;
8708 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8709 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8710 child_die = sibling_die (child_die);
8711 }
8712
8713 /* Allocate storage for parameters and fill them in. */
8714 TYPE_NFIELDS (ftype) = nparams;
8715 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8716 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8717
8072405b
JK
8718 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8719 even if we error out during the parameters reading below. */
8720 for (iparams = 0; iparams < nparams; iparams++)
8721 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8722
8723 iparams = 0;
639d11d3 8724 child_die = die->child;
c906108c
SS
8725 while (child_die && child_die->tag)
8726 {
8727 if (child_die->tag == DW_TAG_formal_parameter)
8728 {
3ce3b1ba
PA
8729 struct type *arg_type;
8730
8731 /* DWARF version 2 has no clean way to discern C++
8732 static and non-static member functions. G++ helps
8733 GDB by marking the first parameter for non-static
8734 member functions (which is the this pointer) as
8735 artificial. We pass this information to
8736 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8737
8738 DWARF version 3 added DW_AT_object_pointer, which GCC
8739 4.5 does not yet generate. */
e142c38c 8740 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8741 if (attr)
8742 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8743 else
418835cc
KS
8744 {
8745 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8746
8747 /* GCC/43521: In java, the formal parameter
8748 "this" is sometimes not marked with DW_AT_artificial. */
8749 if (cu->language == language_java)
8750 {
8751 const char *name = dwarf2_name (child_die, cu);
9a619af0 8752
418835cc
KS
8753 if (name && !strcmp (name, "this"))
8754 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8755 }
8756 }
3ce3b1ba
PA
8757 arg_type = die_type (child_die, cu);
8758
8759 /* RealView does not mark THIS as const, which the testsuite
8760 expects. GCC marks THIS as const in method definitions,
8761 but not in the class specifications (GCC PR 43053). */
8762 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8763 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8764 {
8765 int is_this = 0;
8766 struct dwarf2_cu *arg_cu = cu;
8767 const char *name = dwarf2_name (child_die, cu);
8768
8769 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8770 if (attr)
8771 {
8772 /* If the compiler emits this, use it. */
8773 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8774 is_this = 1;
8775 }
8776 else if (name && strcmp (name, "this") == 0)
8777 /* Function definitions will have the argument names. */
8778 is_this = 1;
8779 else if (name == NULL && iparams == 0)
8780 /* Declarations may not have the names, so like
8781 elsewhere in GDB, assume an artificial first
8782 argument is "this". */
8783 is_this = 1;
8784
8785 if (is_this)
8786 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8787 arg_type, 0);
8788 }
8789
8790 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8791 iparams++;
8792 }
8793 child_die = sibling_die (child_die);
8794 }
8795 }
8796
76c10ea2 8797 return ftype;
c906108c
SS
8798}
8799
f792889a 8800static struct type *
e7c27a73 8801read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8802{
e7c27a73 8803 struct objfile *objfile = cu->objfile;
0114d602 8804 const char *name = NULL;
3c8e0968 8805 struct type *this_type, *target_type;
c906108c 8806
94af9270 8807 name = dwarf2_full_name (NULL, die, cu);
f792889a 8808 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8809 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8810 TYPE_NAME (this_type) = (char *) name;
f792889a 8811 set_die_type (die, this_type, cu);
3c8e0968
DE
8812 target_type = die_type (die, cu);
8813 if (target_type != this_type)
8814 TYPE_TARGET_TYPE (this_type) = target_type;
8815 else
8816 {
8817 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8818 spec and cause infinite loops in GDB. */
8819 complaint (&symfile_complaints,
8820 _("Self-referential DW_TAG_typedef "
8821 "- DIE at 0x%x [in module %s]"),
bb5ed363 8822 die->offset, objfile->name);
3c8e0968
DE
8823 TYPE_TARGET_TYPE (this_type) = NULL;
8824 }
f792889a 8825 return this_type;
c906108c
SS
8826}
8827
8828/* Find a representation of a given base type and install
8829 it in the TYPE field of the die. */
8830
f792889a 8831static struct type *
e7c27a73 8832read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8833{
e7c27a73 8834 struct objfile *objfile = cu->objfile;
c906108c
SS
8835 struct type *type;
8836 struct attribute *attr;
8837 int encoding = 0, size = 0;
39cbfefa 8838 char *name;
6ccb9162
UW
8839 enum type_code code = TYPE_CODE_INT;
8840 int type_flags = 0;
8841 struct type *target_type = NULL;
c906108c 8842
e142c38c 8843 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8844 if (attr)
8845 {
8846 encoding = DW_UNSND (attr);
8847 }
e142c38c 8848 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8849 if (attr)
8850 {
8851 size = DW_UNSND (attr);
8852 }
39cbfefa 8853 name = dwarf2_name (die, cu);
6ccb9162 8854 if (!name)
c906108c 8855 {
6ccb9162
UW
8856 complaint (&symfile_complaints,
8857 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8858 }
6ccb9162
UW
8859
8860 switch (encoding)
c906108c 8861 {
6ccb9162
UW
8862 case DW_ATE_address:
8863 /* Turn DW_ATE_address into a void * pointer. */
8864 code = TYPE_CODE_PTR;
8865 type_flags |= TYPE_FLAG_UNSIGNED;
8866 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8867 break;
8868 case DW_ATE_boolean:
8869 code = TYPE_CODE_BOOL;
8870 type_flags |= TYPE_FLAG_UNSIGNED;
8871 break;
8872 case DW_ATE_complex_float:
8873 code = TYPE_CODE_COMPLEX;
8874 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8875 break;
8876 case DW_ATE_decimal_float:
8877 code = TYPE_CODE_DECFLOAT;
8878 break;
8879 case DW_ATE_float:
8880 code = TYPE_CODE_FLT;
8881 break;
8882 case DW_ATE_signed:
8883 break;
8884 case DW_ATE_unsigned:
8885 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8886 if (cu->language == language_fortran
8887 && name
8888 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8889 code = TYPE_CODE_CHAR;
6ccb9162
UW
8890 break;
8891 case DW_ATE_signed_char:
6e70227d 8892 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8893 || cu->language == language_pascal
8894 || cu->language == language_fortran)
6ccb9162
UW
8895 code = TYPE_CODE_CHAR;
8896 break;
8897 case DW_ATE_unsigned_char:
868a0084 8898 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8899 || cu->language == language_pascal
8900 || cu->language == language_fortran)
6ccb9162
UW
8901 code = TYPE_CODE_CHAR;
8902 type_flags |= TYPE_FLAG_UNSIGNED;
8903 break;
75079b2b
TT
8904 case DW_ATE_UTF:
8905 /* We just treat this as an integer and then recognize the
8906 type by name elsewhere. */
8907 break;
8908
6ccb9162
UW
8909 default:
8910 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8911 dwarf_type_encoding_name (encoding));
8912 break;
c906108c 8913 }
6ccb9162 8914
0114d602
DJ
8915 type = init_type (code, size, type_flags, NULL, objfile);
8916 TYPE_NAME (type) = name;
6ccb9162
UW
8917 TYPE_TARGET_TYPE (type) = target_type;
8918
0114d602 8919 if (name && strcmp (name, "char") == 0)
876cecd0 8920 TYPE_NOSIGN (type) = 1;
0114d602 8921
f792889a 8922 return set_die_type (die, type, cu);
c906108c
SS
8923}
8924
a02abb62
JB
8925/* Read the given DW_AT_subrange DIE. */
8926
f792889a 8927static struct type *
a02abb62
JB
8928read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8929{
8930 struct type *base_type;
8931 struct type *range_type;
8932 struct attribute *attr;
43bbcdc2
PH
8933 LONGEST low = 0;
8934 LONGEST high = -1;
39cbfefa 8935 char *name;
43bbcdc2 8936 LONGEST negative_mask;
e77813c8 8937
a02abb62 8938 base_type = die_type (die, cu);
953ac07e
JK
8939 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8940 check_typedef (base_type);
a02abb62 8941
7e314c57
JK
8942 /* The die_type call above may have already set the type for this DIE. */
8943 range_type = get_die_type (die, cu);
8944 if (range_type)
8945 return range_type;
8946
e142c38c 8947 if (cu->language == language_fortran)
6e70227d 8948 {
a02abb62
JB
8949 /* FORTRAN implies a lower bound of 1, if not given. */
8950 low = 1;
8951 }
8952
dd5e6932
DJ
8953 /* FIXME: For variable sized arrays either of these could be
8954 a variable rather than a constant value. We'll allow it,
8955 but we don't know how to handle it. */
e142c38c 8956 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8957 if (attr)
8958 low = dwarf2_get_attr_constant_value (attr, 0);
8959
e142c38c 8960 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8961 if (attr)
6e70227d 8962 {
d48323d8 8963 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8964 {
8965 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8966 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8967 FIXME: GDB does not yet know how to handle dynamic
8968 arrays properly, treat them as arrays with unspecified
8969 length for now.
8970
8971 FIXME: jimb/2003-09-22: GDB does not really know
8972 how to handle arrays of unspecified length
8973 either; we just represent them as zero-length
8974 arrays. Choose an appropriate upper bound given
8975 the lower bound we've computed above. */
8976 high = low - 1;
8977 }
8978 else
8979 high = dwarf2_get_attr_constant_value (attr, 1);
8980 }
e77813c8
PM
8981 else
8982 {
8983 attr = dwarf2_attr (die, DW_AT_count, cu);
8984 if (attr)
8985 {
8986 int count = dwarf2_get_attr_constant_value (attr, 1);
8987 high = low + count - 1;
8988 }
c2ff108b
JK
8989 else
8990 {
8991 /* Unspecified array length. */
8992 high = low - 1;
8993 }
e77813c8
PM
8994 }
8995
8996 /* Dwarf-2 specifications explicitly allows to create subrange types
8997 without specifying a base type.
8998 In that case, the base type must be set to the type of
8999 the lower bound, upper bound or count, in that order, if any of these
9000 three attributes references an object that has a type.
9001 If no base type is found, the Dwarf-2 specifications say that
9002 a signed integer type of size equal to the size of an address should
9003 be used.
9004 For the following C code: `extern char gdb_int [];'
9005 GCC produces an empty range DIE.
9006 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9007 high bound or count are not yet handled by this code. */
e77813c8
PM
9008 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9009 {
9010 struct objfile *objfile = cu->objfile;
9011 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9012 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9013 struct type *int_type = objfile_type (objfile)->builtin_int;
9014
9015 /* Test "int", "long int", and "long long int" objfile types,
9016 and select the first one having a size above or equal to the
9017 architecture address size. */
9018 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9019 base_type = int_type;
9020 else
9021 {
9022 int_type = objfile_type (objfile)->builtin_long;
9023 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9024 base_type = int_type;
9025 else
9026 {
9027 int_type = objfile_type (objfile)->builtin_long_long;
9028 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9029 base_type = int_type;
9030 }
9031 }
9032 }
a02abb62 9033
6e70227d 9034 negative_mask =
43bbcdc2
PH
9035 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9036 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9037 low |= negative_mask;
9038 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9039 high |= negative_mask;
9040
a02abb62
JB
9041 range_type = create_range_type (NULL, base_type, low, high);
9042
bbb0eef6
JK
9043 /* Mark arrays with dynamic length at least as an array of unspecified
9044 length. GDB could check the boundary but before it gets implemented at
9045 least allow accessing the array elements. */
d48323d8 9046 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9047 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9048
c2ff108b
JK
9049 /* Ada expects an empty array on no boundary attributes. */
9050 if (attr == NULL && cu->language != language_ada)
9051 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9052
39cbfefa
DJ
9053 name = dwarf2_name (die, cu);
9054 if (name)
9055 TYPE_NAME (range_type) = name;
6e70227d 9056
e142c38c 9057 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9058 if (attr)
9059 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9060
7e314c57
JK
9061 set_die_type (die, range_type, cu);
9062
9063 /* set_die_type should be already done. */
b4ba55a1
JB
9064 set_descriptive_type (range_type, die, cu);
9065
7e314c57 9066 return range_type;
a02abb62 9067}
6e70227d 9068
f792889a 9069static struct type *
81a17f79
JB
9070read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9071{
9072 struct type *type;
81a17f79 9073
81a17f79
JB
9074 /* For now, we only support the C meaning of an unspecified type: void. */
9075
0114d602
DJ
9076 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9077 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9078
f792889a 9079 return set_die_type (die, type, cu);
81a17f79 9080}
a02abb62 9081
51545339
DJ
9082/* Trivial hash function for die_info: the hash value of a DIE
9083 is its offset in .debug_info for this objfile. */
9084
9085static hashval_t
9086die_hash (const void *item)
9087{
9088 const struct die_info *die = item;
9a619af0 9089
51545339
DJ
9090 return die->offset;
9091}
9092
9093/* Trivial comparison function for die_info structures: two DIEs
9094 are equal if they have the same offset. */
9095
9096static int
9097die_eq (const void *item_lhs, const void *item_rhs)
9098{
9099 const struct die_info *die_lhs = item_lhs;
9100 const struct die_info *die_rhs = item_rhs;
9a619af0 9101
51545339
DJ
9102 return die_lhs->offset == die_rhs->offset;
9103}
9104
c906108c
SS
9105/* Read a whole compilation unit into a linked list of dies. */
9106
f9aca02d 9107static struct die_info *
93311388 9108read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9109{
93311388 9110 struct die_reader_specs reader_specs;
98bfdba5 9111 int read_abbrevs = 0;
1d9ec526 9112 struct cleanup *back_to = NULL;
98bfdba5
PA
9113 struct die_info *die;
9114
9115 if (cu->dwarf2_abbrevs == NULL)
9116 {
e5fe5e75 9117 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9118 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9119 read_abbrevs = 1;
9120 }
93311388 9121
348e048f 9122 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9123 cu->die_hash
9124 = htab_create_alloc_ex (cu->header.length / 12,
9125 die_hash,
9126 die_eq,
9127 NULL,
9128 &cu->comp_unit_obstack,
9129 hashtab_obstack_allocate,
9130 dummy_obstack_deallocate);
9131
93311388
DE
9132 init_cu_die_reader (&reader_specs, cu);
9133
98bfdba5
PA
9134 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9135
9136 if (read_abbrevs)
9137 do_cleanups (back_to);
9138
9139 return die;
639d11d3
DC
9140}
9141
d97bc12b
DE
9142/* Main entry point for reading a DIE and all children.
9143 Read the DIE and dump it if requested. */
9144
9145static struct die_info *
93311388
DE
9146read_die_and_children (const struct die_reader_specs *reader,
9147 gdb_byte *info_ptr,
d97bc12b
DE
9148 gdb_byte **new_info_ptr,
9149 struct die_info *parent)
9150{
93311388 9151 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9152 new_info_ptr, parent);
9153
9154 if (dwarf2_die_debug)
9155 {
348e048f
DE
9156 fprintf_unfiltered (gdb_stdlog,
9157 "\nRead die from %s of %s:\n",
b0df02fd 9158 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9159 ? ".debug_types"
9160 : ".debug_info"),
348e048f 9161 reader->abfd->filename);
d97bc12b
DE
9162 dump_die (result, dwarf2_die_debug);
9163 }
9164
9165 return result;
9166}
9167
639d11d3
DC
9168/* Read a single die and all its descendents. Set the die's sibling
9169 field to NULL; set other fields in the die correctly, and set all
9170 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9171 location of the info_ptr after reading all of those dies. PARENT
9172 is the parent of the die in question. */
9173
9174static struct die_info *
93311388
DE
9175read_die_and_children_1 (const struct die_reader_specs *reader,
9176 gdb_byte *info_ptr,
d97bc12b
DE
9177 gdb_byte **new_info_ptr,
9178 struct die_info *parent)
639d11d3
DC
9179{
9180 struct die_info *die;
fe1b8b76 9181 gdb_byte *cur_ptr;
639d11d3
DC
9182 int has_children;
9183
93311388 9184 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9185 if (die == NULL)
9186 {
9187 *new_info_ptr = cur_ptr;
9188 return NULL;
9189 }
93311388 9190 store_in_ref_table (die, reader->cu);
639d11d3
DC
9191
9192 if (has_children)
348e048f 9193 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9194 else
9195 {
9196 die->child = NULL;
9197 *new_info_ptr = cur_ptr;
9198 }
9199
9200 die->sibling = NULL;
9201 die->parent = parent;
9202 return die;
9203}
9204
9205/* Read a die, all of its descendents, and all of its siblings; set
9206 all of the fields of all of the dies correctly. Arguments are as
9207 in read_die_and_children. */
9208
9209static struct die_info *
93311388
DE
9210read_die_and_siblings (const struct die_reader_specs *reader,
9211 gdb_byte *info_ptr,
fe1b8b76 9212 gdb_byte **new_info_ptr,
639d11d3
DC
9213 struct die_info *parent)
9214{
9215 struct die_info *first_die, *last_sibling;
fe1b8b76 9216 gdb_byte *cur_ptr;
639d11d3 9217
c906108c 9218 cur_ptr = info_ptr;
639d11d3
DC
9219 first_die = last_sibling = NULL;
9220
9221 while (1)
c906108c 9222 {
639d11d3 9223 struct die_info *die
93311388 9224 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9225
1d325ec1 9226 if (die == NULL)
c906108c 9227 {
639d11d3
DC
9228 *new_info_ptr = cur_ptr;
9229 return first_die;
c906108c 9230 }
1d325ec1
DJ
9231
9232 if (!first_die)
9233 first_die = die;
c906108c 9234 else
1d325ec1
DJ
9235 last_sibling->sibling = die;
9236
9237 last_sibling = die;
c906108c 9238 }
c906108c
SS
9239}
9240
93311388
DE
9241/* Read the die from the .debug_info section buffer. Set DIEP to
9242 point to a newly allocated die with its information, except for its
9243 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9244 whether the die has children or not. */
9245
9246static gdb_byte *
9247read_full_die (const struct die_reader_specs *reader,
9248 struct die_info **diep, gdb_byte *info_ptr,
9249 int *has_children)
9250{
9251 unsigned int abbrev_number, bytes_read, i, offset;
9252 struct abbrev_info *abbrev;
9253 struct die_info *die;
9254 struct dwarf2_cu *cu = reader->cu;
9255 bfd *abfd = reader->abfd;
9256
9257 offset = info_ptr - reader->buffer;
9258 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9259 info_ptr += bytes_read;
9260 if (!abbrev_number)
9261 {
9262 *diep = NULL;
9263 *has_children = 0;
9264 return info_ptr;
9265 }
9266
9267 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9268 if (!abbrev)
348e048f
DE
9269 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9270 abbrev_number,
9271 bfd_get_filename (abfd));
9272
93311388
DE
9273 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9274 die->offset = offset;
9275 die->tag = abbrev->tag;
9276 die->abbrev = abbrev_number;
9277
9278 die->num_attrs = abbrev->num_attrs;
9279
9280 for (i = 0; i < abbrev->num_attrs; ++i)
9281 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9282 abfd, info_ptr, cu);
9283
9284 *diep = die;
9285 *has_children = abbrev->has_children;
9286 return info_ptr;
9287}
9288
c906108c
SS
9289/* In DWARF version 2, the description of the debugging information is
9290 stored in a separate .debug_abbrev section. Before we read any
9291 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9292 in a hash table. This function also sets flags in CU describing
9293 the data found in the abbrev table. */
c906108c
SS
9294
9295static void
e5fe5e75 9296dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9297{
e5fe5e75 9298 bfd *abfd = cu->objfile->obfd;
e7c27a73 9299 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9300 gdb_byte *abbrev_ptr;
c906108c
SS
9301 struct abbrev_info *cur_abbrev;
9302 unsigned int abbrev_number, bytes_read, abbrev_name;
9303 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9304 struct attr_abbrev *cur_attrs;
9305 unsigned int allocated_attrs;
c906108c 9306
0963b4bd 9307 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9308 obstack_init (&cu->abbrev_obstack);
9309 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9310 (ABBREV_HASH_SIZE
9311 * sizeof (struct abbrev_info *)));
9312 memset (cu->dwarf2_abbrevs, 0,
9313 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9314
be391dca
TT
9315 dwarf2_read_section (dwarf2_per_objfile->objfile,
9316 &dwarf2_per_objfile->abbrev);
dce234bc 9317 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9318 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9319 abbrev_ptr += bytes_read;
9320
f3dd6933
DJ
9321 allocated_attrs = ATTR_ALLOC_CHUNK;
9322 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9323
0963b4bd 9324 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9325 while (abbrev_number)
9326 {
f3dd6933 9327 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9328
9329 /* read in abbrev header */
9330 cur_abbrev->number = abbrev_number;
9331 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9332 abbrev_ptr += bytes_read;
9333 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9334 abbrev_ptr += 1;
9335
72bf9492
DJ
9336 if (cur_abbrev->tag == DW_TAG_namespace)
9337 cu->has_namespace_info = 1;
9338
c906108c
SS
9339 /* now read in declarations */
9340 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9341 abbrev_ptr += bytes_read;
9342 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9343 abbrev_ptr += bytes_read;
9344 while (abbrev_name)
9345 {
f3dd6933 9346 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9347 {
f3dd6933
DJ
9348 allocated_attrs += ATTR_ALLOC_CHUNK;
9349 cur_attrs
9350 = xrealloc (cur_attrs, (allocated_attrs
9351 * sizeof (struct attr_abbrev)));
c906108c 9352 }
ae038cb0
DJ
9353
9354 /* Record whether this compilation unit might have
9355 inter-compilation-unit references. If we don't know what form
9356 this attribute will have, then it might potentially be a
9357 DW_FORM_ref_addr, so we conservatively expect inter-CU
9358 references. */
9359
9360 if (abbrev_form == DW_FORM_ref_addr
9361 || abbrev_form == DW_FORM_indirect)
9362 cu->has_form_ref_addr = 1;
9363
f3dd6933
DJ
9364 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9365 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9366 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9367 abbrev_ptr += bytes_read;
9368 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9369 abbrev_ptr += bytes_read;
9370 }
9371
f3dd6933
DJ
9372 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9373 (cur_abbrev->num_attrs
9374 * sizeof (struct attr_abbrev)));
9375 memcpy (cur_abbrev->attrs, cur_attrs,
9376 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9377
c906108c 9378 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9379 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9380 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9381
9382 /* Get next abbreviation.
9383 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9384 always properly terminated with an abbrev number of 0.
9385 Exit loop if we encounter an abbreviation which we have
9386 already read (which means we are about to read the abbreviations
9387 for the next compile unit) or if the end of the abbreviation
9388 table is reached. */
dce234bc
PP
9389 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9390 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9391 break;
9392 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9393 abbrev_ptr += bytes_read;
e7c27a73 9394 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9395 break;
9396 }
f3dd6933
DJ
9397
9398 xfree (cur_attrs);
c906108c
SS
9399}
9400
f3dd6933 9401/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9402
c906108c 9403static void
f3dd6933 9404dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9405{
f3dd6933 9406 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9407
f3dd6933
DJ
9408 obstack_free (&cu->abbrev_obstack, NULL);
9409 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9410}
9411
9412/* Lookup an abbrev_info structure in the abbrev hash table. */
9413
9414static struct abbrev_info *
e7c27a73 9415dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9416{
9417 unsigned int hash_number;
9418 struct abbrev_info *abbrev;
9419
9420 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9421 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9422
9423 while (abbrev)
9424 {
9425 if (abbrev->number == number)
9426 return abbrev;
9427 else
9428 abbrev = abbrev->next;
9429 }
9430 return NULL;
9431}
9432
72bf9492
DJ
9433/* Returns nonzero if TAG represents a type that we might generate a partial
9434 symbol for. */
9435
9436static int
9437is_type_tag_for_partial (int tag)
9438{
9439 switch (tag)
9440 {
9441#if 0
9442 /* Some types that would be reasonable to generate partial symbols for,
9443 that we don't at present. */
9444 case DW_TAG_array_type:
9445 case DW_TAG_file_type:
9446 case DW_TAG_ptr_to_member_type:
9447 case DW_TAG_set_type:
9448 case DW_TAG_string_type:
9449 case DW_TAG_subroutine_type:
9450#endif
9451 case DW_TAG_base_type:
9452 case DW_TAG_class_type:
680b30c7 9453 case DW_TAG_interface_type:
72bf9492
DJ
9454 case DW_TAG_enumeration_type:
9455 case DW_TAG_structure_type:
9456 case DW_TAG_subrange_type:
9457 case DW_TAG_typedef:
9458 case DW_TAG_union_type:
9459 return 1;
9460 default:
9461 return 0;
9462 }
9463}
9464
9465/* Load all DIEs that are interesting for partial symbols into memory. */
9466
9467static struct partial_die_info *
93311388
DE
9468load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9469 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9470{
bb5ed363 9471 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9472 struct partial_die_info *part_die;
9473 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9474 struct abbrev_info *abbrev;
9475 unsigned int bytes_read;
5afb4e99 9476 unsigned int load_all = 0;
72bf9492
DJ
9477
9478 int nesting_level = 1;
9479
9480 parent_die = NULL;
9481 last_die = NULL;
9482
5afb4e99
DJ
9483 if (cu->per_cu && cu->per_cu->load_all_dies)
9484 load_all = 1;
9485
72bf9492
DJ
9486 cu->partial_dies
9487 = htab_create_alloc_ex (cu->header.length / 12,
9488 partial_die_hash,
9489 partial_die_eq,
9490 NULL,
9491 &cu->comp_unit_obstack,
9492 hashtab_obstack_allocate,
9493 dummy_obstack_deallocate);
9494
9495 part_die = obstack_alloc (&cu->comp_unit_obstack,
9496 sizeof (struct partial_die_info));
9497
9498 while (1)
9499 {
9500 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9501
9502 /* A NULL abbrev means the end of a series of children. */
9503 if (abbrev == NULL)
9504 {
9505 if (--nesting_level == 0)
9506 {
9507 /* PART_DIE was probably the last thing allocated on the
9508 comp_unit_obstack, so we could call obstack_free
9509 here. We don't do that because the waste is small,
9510 and will be cleaned up when we're done with this
9511 compilation unit. This way, we're also more robust
9512 against other users of the comp_unit_obstack. */
9513 return first_die;
9514 }
9515 info_ptr += bytes_read;
9516 last_die = parent_die;
9517 parent_die = parent_die->die_parent;
9518 continue;
9519 }
9520
98bfdba5
PA
9521 /* Check for template arguments. We never save these; if
9522 they're seen, we just mark the parent, and go on our way. */
9523 if (parent_die != NULL
9524 && cu->language == language_cplus
9525 && (abbrev->tag == DW_TAG_template_type_param
9526 || abbrev->tag == DW_TAG_template_value_param))
9527 {
9528 parent_die->has_template_arguments = 1;
9529
9530 if (!load_all)
9531 {
9532 /* We don't need a partial DIE for the template argument. */
9533 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9534 cu);
9535 continue;
9536 }
9537 }
9538
9539 /* We only recurse into subprograms looking for template arguments.
9540 Skip their other children. */
9541 if (!load_all
9542 && cu->language == language_cplus
9543 && parent_die != NULL
9544 && parent_die->tag == DW_TAG_subprogram)
9545 {
9546 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9547 continue;
9548 }
9549
5afb4e99
DJ
9550 /* Check whether this DIE is interesting enough to save. Normally
9551 we would not be interested in members here, but there may be
9552 later variables referencing them via DW_AT_specification (for
9553 static members). */
9554 if (!load_all
9555 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9556 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9557 && abbrev->tag != DW_TAG_enumerator
9558 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9559 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9560 && abbrev->tag != DW_TAG_variable
5afb4e99 9561 && abbrev->tag != DW_TAG_namespace
f55ee35c 9562 && abbrev->tag != DW_TAG_module
5afb4e99 9563 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9564 {
9565 /* Otherwise we skip to the next sibling, if any. */
93311388 9566 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9567 continue;
9568 }
9569
93311388
DE
9570 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9571 buffer, info_ptr, cu);
72bf9492
DJ
9572
9573 /* This two-pass algorithm for processing partial symbols has a
9574 high cost in cache pressure. Thus, handle some simple cases
9575 here which cover the majority of C partial symbols. DIEs
9576 which neither have specification tags in them, nor could have
9577 specification tags elsewhere pointing at them, can simply be
9578 processed and discarded.
9579
9580 This segment is also optional; scan_partial_symbols and
9581 add_partial_symbol will handle these DIEs if we chain
9582 them in normally. When compilers which do not emit large
9583 quantities of duplicate debug information are more common,
9584 this code can probably be removed. */
9585
9586 /* Any complete simple types at the top level (pretty much all
9587 of them, for a language without namespaces), can be processed
9588 directly. */
9589 if (parent_die == NULL
9590 && part_die->has_specification == 0
9591 && part_die->is_declaration == 0
d8228535 9592 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9593 || part_die->tag == DW_TAG_base_type
9594 || part_die->tag == DW_TAG_subrange_type))
9595 {
9596 if (building_psymtab && part_die->name != NULL)
04a679b8 9597 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9598 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9599 &objfile->static_psymbols,
9600 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9601 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9602 continue;
9603 }
9604
d8228535
JK
9605 /* The exception for DW_TAG_typedef with has_children above is
9606 a workaround of GCC PR debug/47510. In the case of this complaint
9607 type_name_no_tag_or_error will error on such types later.
9608
9609 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9610 it could not find the child DIEs referenced later, this is checked
9611 above. In correct DWARF DW_TAG_typedef should have no children. */
9612
9613 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9614 complaint (&symfile_complaints,
9615 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9616 "- DIE at 0x%x [in module %s]"),
bb5ed363 9617 part_die->offset, objfile->name);
d8228535 9618
72bf9492
DJ
9619 /* If we're at the second level, and we're an enumerator, and
9620 our parent has no specification (meaning possibly lives in a
9621 namespace elsewhere), then we can add the partial symbol now
9622 instead of queueing it. */
9623 if (part_die->tag == DW_TAG_enumerator
9624 && parent_die != NULL
9625 && parent_die->die_parent == NULL
9626 && parent_die->tag == DW_TAG_enumeration_type
9627 && parent_die->has_specification == 0)
9628 {
9629 if (part_die->name == NULL)
3e43a32a
MS
9630 complaint (&symfile_complaints,
9631 _("malformed enumerator DIE ignored"));
72bf9492 9632 else if (building_psymtab)
04a679b8 9633 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9634 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9635 (cu->language == language_cplus
9636 || cu->language == language_java)
bb5ed363
DE
9637 ? &objfile->global_psymbols
9638 : &objfile->static_psymbols,
9639 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9640
93311388 9641 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9642 continue;
9643 }
9644
9645 /* We'll save this DIE so link it in. */
9646 part_die->die_parent = parent_die;
9647 part_die->die_sibling = NULL;
9648 part_die->die_child = NULL;
9649
9650 if (last_die && last_die == parent_die)
9651 last_die->die_child = part_die;
9652 else if (last_die)
9653 last_die->die_sibling = part_die;
9654
9655 last_die = part_die;
9656
9657 if (first_die == NULL)
9658 first_die = part_die;
9659
9660 /* Maybe add the DIE to the hash table. Not all DIEs that we
9661 find interesting need to be in the hash table, because we
9662 also have the parent/sibling/child chains; only those that we
9663 might refer to by offset later during partial symbol reading.
9664
9665 For now this means things that might have be the target of a
9666 DW_AT_specification, DW_AT_abstract_origin, or
9667 DW_AT_extension. DW_AT_extension will refer only to
9668 namespaces; DW_AT_abstract_origin refers to functions (and
9669 many things under the function DIE, but we do not recurse
9670 into function DIEs during partial symbol reading) and
9671 possibly variables as well; DW_AT_specification refers to
9672 declarations. Declarations ought to have the DW_AT_declaration
9673 flag. It happens that GCC forgets to put it in sometimes, but
9674 only for functions, not for types.
9675
9676 Adding more things than necessary to the hash table is harmless
9677 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9678 wasted time in find_partial_die, when we reread the compilation
9679 unit with load_all_dies set. */
72bf9492 9680
5afb4e99 9681 if (load_all
72929c62 9682 || abbrev->tag == DW_TAG_constant
5afb4e99 9683 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9684 || abbrev->tag == DW_TAG_variable
9685 || abbrev->tag == DW_TAG_namespace
9686 || part_die->is_declaration)
9687 {
9688 void **slot;
9689
9690 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9691 part_die->offset, INSERT);
9692 *slot = part_die;
9693 }
9694
9695 part_die = obstack_alloc (&cu->comp_unit_obstack,
9696 sizeof (struct partial_die_info));
9697
9698 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9699 we have no reason to follow the children of structures; for other
98bfdba5
PA
9700 languages we have to, so that we can get at method physnames
9701 to infer fully qualified class names, for DW_AT_specification,
9702 and for C++ template arguments. For C++, we also look one level
9703 inside functions to find template arguments (if the name of the
9704 function does not already contain the template arguments).
bc30ff58
JB
9705
9706 For Ada, we need to scan the children of subprograms and lexical
9707 blocks as well because Ada allows the definition of nested
9708 entities that could be interesting for the debugger, such as
9709 nested subprograms for instance. */
72bf9492 9710 if (last_die->has_children
5afb4e99
DJ
9711 && (load_all
9712 || last_die->tag == DW_TAG_namespace
f55ee35c 9713 || last_die->tag == DW_TAG_module
72bf9492 9714 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9715 || (cu->language == language_cplus
9716 && last_die->tag == DW_TAG_subprogram
9717 && (last_die->name == NULL
9718 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9719 || (cu->language != language_c
9720 && (last_die->tag == DW_TAG_class_type
680b30c7 9721 || last_die->tag == DW_TAG_interface_type
72bf9492 9722 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9723 || last_die->tag == DW_TAG_union_type))
9724 || (cu->language == language_ada
9725 && (last_die->tag == DW_TAG_subprogram
9726 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9727 {
9728 nesting_level++;
9729 parent_die = last_die;
9730 continue;
9731 }
9732
9733 /* Otherwise we skip to the next sibling, if any. */
93311388 9734 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9735
9736 /* Back to the top, do it again. */
9737 }
9738}
9739
c906108c
SS
9740/* Read a minimal amount of information into the minimal die structure. */
9741
fe1b8b76 9742static gdb_byte *
72bf9492
DJ
9743read_partial_die (struct partial_die_info *part_die,
9744 struct abbrev_info *abbrev,
9745 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9746 gdb_byte *buffer, gdb_byte *info_ptr,
9747 struct dwarf2_cu *cu)
c906108c 9748{
bb5ed363 9749 struct objfile *objfile = cu->objfile;
fa238c03 9750 unsigned int i;
c906108c 9751 struct attribute attr;
c5aa993b 9752 int has_low_pc_attr = 0;
c906108c
SS
9753 int has_high_pc_attr = 0;
9754
72bf9492 9755 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9756
93311388 9757 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9758
9759 info_ptr += abbrev_len;
9760
9761 if (abbrev == NULL)
9762 return info_ptr;
9763
c906108c
SS
9764 part_die->tag = abbrev->tag;
9765 part_die->has_children = abbrev->has_children;
c906108c
SS
9766
9767 for (i = 0; i < abbrev->num_attrs; ++i)
9768 {
e7c27a73 9769 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9770
9771 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9772 partial symbol table. */
c906108c
SS
9773 switch (attr.name)
9774 {
9775 case DW_AT_name:
71c25dea
TT
9776 switch (part_die->tag)
9777 {
9778 case DW_TAG_compile_unit:
348e048f 9779 case DW_TAG_type_unit:
71c25dea
TT
9780 /* Compilation units have a DW_AT_name that is a filename, not
9781 a source language identifier. */
9782 case DW_TAG_enumeration_type:
9783 case DW_TAG_enumerator:
9784 /* These tags always have simple identifiers already; no need
9785 to canonicalize them. */
9786 part_die->name = DW_STRING (&attr);
9787 break;
9788 default:
9789 part_die->name
9790 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9791 &objfile->objfile_obstack);
71c25dea
TT
9792 break;
9793 }
c906108c 9794 break;
31ef98ae 9795 case DW_AT_linkage_name:
c906108c 9796 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9797 /* Note that both forms of linkage name might appear. We
9798 assume they will be the same, and we only store the last
9799 one we see. */
94af9270
KS
9800 if (cu->language == language_ada)
9801 part_die->name = DW_STRING (&attr);
abc72ce4 9802 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9803 break;
9804 case DW_AT_low_pc:
9805 has_low_pc_attr = 1;
9806 part_die->lowpc = DW_ADDR (&attr);
9807 break;
9808 case DW_AT_high_pc:
9809 has_high_pc_attr = 1;
9810 part_die->highpc = DW_ADDR (&attr);
9811 break;
9812 case DW_AT_location:
0963b4bd 9813 /* Support the .debug_loc offsets. */
8e19ed76
PS
9814 if (attr_form_is_block (&attr))
9815 {
9816 part_die->locdesc = DW_BLOCK (&attr);
9817 }
3690dd37 9818 else if (attr_form_is_section_offset (&attr))
8e19ed76 9819 {
4d3c2250 9820 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9821 }
9822 else
9823 {
4d3c2250
KB
9824 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9825 "partial symbol information");
8e19ed76 9826 }
c906108c 9827 break;
c906108c
SS
9828 case DW_AT_external:
9829 part_die->is_external = DW_UNSND (&attr);
9830 break;
9831 case DW_AT_declaration:
9832 part_die->is_declaration = DW_UNSND (&attr);
9833 break;
9834 case DW_AT_type:
9835 part_die->has_type = 1;
9836 break;
9837 case DW_AT_abstract_origin:
9838 case DW_AT_specification:
72bf9492
DJ
9839 case DW_AT_extension:
9840 part_die->has_specification = 1;
c764a876 9841 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9842 break;
9843 case DW_AT_sibling:
9844 /* Ignore absolute siblings, they might point outside of
9845 the current compile unit. */
9846 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9847 complaint (&symfile_complaints,
9848 _("ignoring absolute DW_AT_sibling"));
c906108c 9849 else
93311388 9850 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9851 break;
fa4028e9
JB
9852 case DW_AT_byte_size:
9853 part_die->has_byte_size = 1;
9854 break;
68511cec
CES
9855 case DW_AT_calling_convention:
9856 /* DWARF doesn't provide a way to identify a program's source-level
9857 entry point. DW_AT_calling_convention attributes are only meant
9858 to describe functions' calling conventions.
9859
9860 However, because it's a necessary piece of information in
9861 Fortran, and because DW_CC_program is the only piece of debugging
9862 information whose definition refers to a 'main program' at all,
9863 several compilers have begun marking Fortran main programs with
9864 DW_CC_program --- even when those functions use the standard
9865 calling conventions.
9866
9867 So until DWARF specifies a way to provide this information and
9868 compilers pick up the new representation, we'll support this
9869 practice. */
9870 if (DW_UNSND (&attr) == DW_CC_program
9871 && cu->language == language_fortran)
01f8c46d
JK
9872 {
9873 set_main_name (part_die->name);
9874
9875 /* As this DIE has a static linkage the name would be difficult
9876 to look up later. */
9877 language_of_main = language_fortran;
9878 }
68511cec 9879 break;
c906108c
SS
9880 default:
9881 break;
9882 }
9883 }
9884
9373cf26
JK
9885 if (has_low_pc_attr && has_high_pc_attr)
9886 {
9887 /* When using the GNU linker, .gnu.linkonce. sections are used to
9888 eliminate duplicate copies of functions and vtables and such.
9889 The linker will arbitrarily choose one and discard the others.
9890 The AT_*_pc values for such functions refer to local labels in
9891 these sections. If the section from that file was discarded, the
9892 labels are not in the output, so the relocs get a value of 0.
9893 If this is a discarded function, mark the pc bounds as invalid,
9894 so that GDB will ignore it. */
9895 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9896 {
bb5ed363 9897 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9898
9899 complaint (&symfile_complaints,
9900 _("DW_AT_low_pc %s is zero "
9901 "for DIE at 0x%x [in module %s]"),
9902 paddress (gdbarch, part_die->lowpc),
bb5ed363 9903 part_die->offset, objfile->name);
9373cf26
JK
9904 }
9905 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9906 else if (part_die->lowpc >= part_die->highpc)
9907 {
bb5ed363 9908 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9909
9910 complaint (&symfile_complaints,
9911 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9912 "for DIE at 0x%x [in module %s]"),
9913 paddress (gdbarch, part_die->lowpc),
9914 paddress (gdbarch, part_die->highpc),
bb5ed363 9915 part_die->offset, objfile->name);
9373cf26
JK
9916 }
9917 else
9918 part_die->has_pc_info = 1;
9919 }
85cbf3d3 9920
c906108c
SS
9921 return info_ptr;
9922}
9923
72bf9492
DJ
9924/* Find a cached partial DIE at OFFSET in CU. */
9925
9926static struct partial_die_info *
c764a876 9927find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9928{
9929 struct partial_die_info *lookup_die = NULL;
9930 struct partial_die_info part_die;
9931
9932 part_die.offset = offset;
9933 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9934
72bf9492
DJ
9935 return lookup_die;
9936}
9937
348e048f
DE
9938/* Find a partial DIE at OFFSET, which may or may not be in CU,
9939 except in the case of .debug_types DIEs which do not reference
9940 outside their CU (they do however referencing other types via
55f1336d 9941 DW_FORM_ref_sig8). */
72bf9492
DJ
9942
9943static struct partial_die_info *
c764a876 9944find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9945{
bb5ed363 9946 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9947 struct dwarf2_per_cu_data *per_cu = NULL;
9948 struct partial_die_info *pd = NULL;
72bf9492 9949
b0df02fd 9950 if (cu->per_cu->debug_types_section)
348e048f
DE
9951 {
9952 pd = find_partial_die_in_comp_unit (offset, cu);
9953 if (pd != NULL)
9954 return pd;
9955 goto not_found;
9956 }
9957
45452591 9958 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9959 {
9960 pd = find_partial_die_in_comp_unit (offset, cu);
9961 if (pd != NULL)
9962 return pd;
9963 }
72bf9492 9964
bb5ed363 9965 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 9966
98bfdba5 9967 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 9968 load_partial_comp_unit (per_cu);
ae038cb0
DJ
9969
9970 per_cu->cu->last_used = 0;
5afb4e99
DJ
9971 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9972
9973 if (pd == NULL && per_cu->load_all_dies == 0)
9974 {
9975 struct cleanup *back_to;
9976 struct partial_die_info comp_unit_die;
9977 struct abbrev_info *abbrev;
9978 unsigned int bytes_read;
9979 char *info_ptr;
9980
9981 per_cu->load_all_dies = 1;
9982
9983 /* Re-read the DIEs. */
9984 back_to = make_cleanup (null_cleanup, 0);
9985 if (per_cu->cu->dwarf2_abbrevs == NULL)
9986 {
e5fe5e75 9987 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 9988 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9989 }
dce234bc 9990 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9991 + per_cu->cu->header.offset
9992 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9993 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9994 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 9995 objfile->obfd,
93311388 9996 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9997 per_cu->cu);
9998 if (comp_unit_die.has_children)
bb5ed363 9999 load_partial_dies (objfile->obfd,
93311388
DE
10000 dwarf2_per_objfile->info.buffer, info_ptr,
10001 0, per_cu->cu);
5afb4e99
DJ
10002 do_cleanups (back_to);
10003
10004 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10005 }
10006
348e048f
DE
10007 not_found:
10008
5afb4e99
DJ
10009 if (pd == NULL)
10010 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10011 _("could not find partial DIE 0x%x "
10012 "in cache [from module %s]\n"),
bb5ed363 10013 offset, bfd_get_filename (objfile->obfd));
5afb4e99 10014 return pd;
72bf9492
DJ
10015}
10016
abc72ce4
DE
10017/* See if we can figure out if the class lives in a namespace. We do
10018 this by looking for a member function; its demangled name will
10019 contain namespace info, if there is any. */
10020
10021static void
10022guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10023 struct dwarf2_cu *cu)
10024{
10025 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10026 what template types look like, because the demangler
10027 frequently doesn't give the same name as the debug info. We
10028 could fix this by only using the demangled name to get the
10029 prefix (but see comment in read_structure_type). */
10030
10031 struct partial_die_info *real_pdi;
10032 struct partial_die_info *child_pdi;
10033
10034 /* If this DIE (this DIE's specification, if any) has a parent, then
10035 we should not do this. We'll prepend the parent's fully qualified
10036 name when we create the partial symbol. */
10037
10038 real_pdi = struct_pdi;
10039 while (real_pdi->has_specification)
10040 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10041
10042 if (real_pdi->die_parent != NULL)
10043 return;
10044
10045 for (child_pdi = struct_pdi->die_child;
10046 child_pdi != NULL;
10047 child_pdi = child_pdi->die_sibling)
10048 {
10049 if (child_pdi->tag == DW_TAG_subprogram
10050 && child_pdi->linkage_name != NULL)
10051 {
10052 char *actual_class_name
10053 = language_class_name_from_physname (cu->language_defn,
10054 child_pdi->linkage_name);
10055 if (actual_class_name != NULL)
10056 {
10057 struct_pdi->name
10058 = obsavestring (actual_class_name,
10059 strlen (actual_class_name),
10060 &cu->objfile->objfile_obstack);
10061 xfree (actual_class_name);
10062 }
10063 break;
10064 }
10065 }
10066}
10067
72bf9492
DJ
10068/* Adjust PART_DIE before generating a symbol for it. This function
10069 may set the is_external flag or change the DIE's name. */
10070
10071static void
10072fixup_partial_die (struct partial_die_info *part_die,
10073 struct dwarf2_cu *cu)
10074{
abc72ce4
DE
10075 /* Once we've fixed up a die, there's no point in doing so again.
10076 This also avoids a memory leak if we were to call
10077 guess_partial_die_structure_name multiple times. */
10078 if (part_die->fixup_called)
10079 return;
10080
72bf9492
DJ
10081 /* If we found a reference attribute and the DIE has no name, try
10082 to find a name in the referred to DIE. */
10083
10084 if (part_die->name == NULL && part_die->has_specification)
10085 {
10086 struct partial_die_info *spec_die;
72bf9492 10087
10b3939b 10088 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10089
10b3939b 10090 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10091
10092 if (spec_die->name)
10093 {
10094 part_die->name = spec_die->name;
10095
10096 /* Copy DW_AT_external attribute if it is set. */
10097 if (spec_die->is_external)
10098 part_die->is_external = spec_die->is_external;
10099 }
10100 }
10101
10102 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10103
10104 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10105 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10106
abc72ce4
DE
10107 /* If there is no parent die to provide a namespace, and there are
10108 children, see if we can determine the namespace from their linkage
10109 name.
10110 NOTE: We need to do this even if cu->has_namespace_info != 0.
10111 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10112 if (cu->language == language_cplus
8b70b953 10113 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10114 && part_die->die_parent == NULL
10115 && part_die->has_children
10116 && (part_die->tag == DW_TAG_class_type
10117 || part_die->tag == DW_TAG_structure_type
10118 || part_die->tag == DW_TAG_union_type))
10119 guess_partial_die_structure_name (part_die, cu);
10120
53832f31
TT
10121 /* GCC might emit a nameless struct or union that has a linkage
10122 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10123 if (part_die->name == NULL
96408a79
SA
10124 && (part_die->tag == DW_TAG_class_type
10125 || part_die->tag == DW_TAG_interface_type
10126 || part_die->tag == DW_TAG_structure_type
10127 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10128 && part_die->linkage_name != NULL)
10129 {
10130 char *demangled;
10131
10132 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10133 if (demangled)
10134 {
96408a79
SA
10135 const char *base;
10136
10137 /* Strip any leading namespaces/classes, keep only the base name.
10138 DW_AT_name for named DIEs does not contain the prefixes. */
10139 base = strrchr (demangled, ':');
10140 if (base && base > demangled && base[-1] == ':')
10141 base++;
10142 else
10143 base = demangled;
10144
10145 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10146 &cu->objfile->objfile_obstack);
10147 xfree (demangled);
10148 }
10149 }
10150
abc72ce4 10151 part_die->fixup_called = 1;
72bf9492
DJ
10152}
10153
a8329558 10154/* Read an attribute value described by an attribute form. */
c906108c 10155
fe1b8b76 10156static gdb_byte *
a8329558 10157read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10158 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10159 struct dwarf2_cu *cu)
c906108c 10160{
e7c27a73 10161 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10162 unsigned int bytes_read;
10163 struct dwarf_block *blk;
10164
a8329558
KW
10165 attr->form = form;
10166 switch (form)
c906108c 10167 {
c906108c 10168 case DW_FORM_ref_addr:
ae411497
TT
10169 if (cu->header.version == 2)
10170 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10171 else
3e43a32a
MS
10172 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10173 &cu->header, &bytes_read);
ae411497
TT
10174 info_ptr += bytes_read;
10175 break;
10176 case DW_FORM_addr:
e7c27a73 10177 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10178 info_ptr += bytes_read;
c906108c
SS
10179 break;
10180 case DW_FORM_block2:
7b5a2f43 10181 blk = dwarf_alloc_block (cu);
c906108c
SS
10182 blk->size = read_2_bytes (abfd, info_ptr);
10183 info_ptr += 2;
10184 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10185 info_ptr += blk->size;
10186 DW_BLOCK (attr) = blk;
10187 break;
10188 case DW_FORM_block4:
7b5a2f43 10189 blk = dwarf_alloc_block (cu);
c906108c
SS
10190 blk->size = read_4_bytes (abfd, info_ptr);
10191 info_ptr += 4;
10192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10193 info_ptr += blk->size;
10194 DW_BLOCK (attr) = blk;
10195 break;
10196 case DW_FORM_data2:
10197 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10198 info_ptr += 2;
10199 break;
10200 case DW_FORM_data4:
10201 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10202 info_ptr += 4;
10203 break;
10204 case DW_FORM_data8:
10205 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10206 info_ptr += 8;
10207 break;
2dc7f7b3
TT
10208 case DW_FORM_sec_offset:
10209 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10210 info_ptr += bytes_read;
10211 break;
c906108c 10212 case DW_FORM_string:
9b1c24c8 10213 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10214 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10215 info_ptr += bytes_read;
10216 break;
4bdf3d34
JJ
10217 case DW_FORM_strp:
10218 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10219 &bytes_read);
8285870a 10220 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10221 info_ptr += bytes_read;
10222 break;
2dc7f7b3 10223 case DW_FORM_exprloc:
c906108c 10224 case DW_FORM_block:
7b5a2f43 10225 blk = dwarf_alloc_block (cu);
c906108c
SS
10226 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10227 info_ptr += bytes_read;
10228 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10229 info_ptr += blk->size;
10230 DW_BLOCK (attr) = blk;
10231 break;
10232 case DW_FORM_block1:
7b5a2f43 10233 blk = dwarf_alloc_block (cu);
c906108c
SS
10234 blk->size = read_1_byte (abfd, info_ptr);
10235 info_ptr += 1;
10236 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10237 info_ptr += blk->size;
10238 DW_BLOCK (attr) = blk;
10239 break;
10240 case DW_FORM_data1:
10241 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10242 info_ptr += 1;
10243 break;
10244 case DW_FORM_flag:
10245 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10246 info_ptr += 1;
10247 break;
2dc7f7b3
TT
10248 case DW_FORM_flag_present:
10249 DW_UNSND (attr) = 1;
10250 break;
c906108c
SS
10251 case DW_FORM_sdata:
10252 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10253 info_ptr += bytes_read;
10254 break;
10255 case DW_FORM_udata:
10256 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10257 info_ptr += bytes_read;
10258 break;
10259 case DW_FORM_ref1:
10b3939b 10260 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10261 info_ptr += 1;
10262 break;
10263 case DW_FORM_ref2:
10b3939b 10264 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10265 info_ptr += 2;
10266 break;
10267 case DW_FORM_ref4:
10b3939b 10268 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10269 info_ptr += 4;
10270 break;
613e1657 10271 case DW_FORM_ref8:
10b3939b 10272 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10273 info_ptr += 8;
10274 break;
55f1336d 10275 case DW_FORM_ref_sig8:
348e048f
DE
10276 /* Convert the signature to something we can record in DW_UNSND
10277 for later lookup.
10278 NOTE: This is NULL if the type wasn't found. */
10279 DW_SIGNATURED_TYPE (attr) =
10280 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10281 info_ptr += 8;
10282 break;
c906108c 10283 case DW_FORM_ref_udata:
10b3939b
DJ
10284 DW_ADDR (attr) = (cu->header.offset
10285 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10286 info_ptr += bytes_read;
10287 break;
c906108c 10288 case DW_FORM_indirect:
a8329558
KW
10289 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10290 info_ptr += bytes_read;
e7c27a73 10291 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10292 break;
c906108c 10293 default:
8a3fe4f8 10294 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10295 dwarf_form_name (form),
10296 bfd_get_filename (abfd));
c906108c 10297 }
28e94949
JB
10298
10299 /* We have seen instances where the compiler tried to emit a byte
10300 size attribute of -1 which ended up being encoded as an unsigned
10301 0xffffffff. Although 0xffffffff is technically a valid size value,
10302 an object of this size seems pretty unlikely so we can relatively
10303 safely treat these cases as if the size attribute was invalid and
10304 treat them as zero by default. */
10305 if (attr->name == DW_AT_byte_size
10306 && form == DW_FORM_data4
10307 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10308 {
10309 complaint
10310 (&symfile_complaints,
43bbcdc2
PH
10311 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10312 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10313 DW_UNSND (attr) = 0;
10314 }
28e94949 10315
c906108c
SS
10316 return info_ptr;
10317}
10318
a8329558
KW
10319/* Read an attribute described by an abbreviated attribute. */
10320
fe1b8b76 10321static gdb_byte *
a8329558 10322read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10323 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10324{
10325 attr->name = abbrev->name;
e7c27a73 10326 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10327}
10328
0963b4bd 10329/* Read dwarf information from a buffer. */
c906108c
SS
10330
10331static unsigned int
fe1b8b76 10332read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10333{
fe1b8b76 10334 return bfd_get_8 (abfd, buf);
c906108c
SS
10335}
10336
10337static int
fe1b8b76 10338read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10339{
fe1b8b76 10340 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10341}
10342
10343static unsigned int
fe1b8b76 10344read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10345{
fe1b8b76 10346 return bfd_get_16 (abfd, buf);
c906108c
SS
10347}
10348
21ae7a4d
JK
10349static int
10350read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10351{
10352 return bfd_get_signed_16 (abfd, buf);
10353}
10354
c906108c 10355static unsigned int
fe1b8b76 10356read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10357{
fe1b8b76 10358 return bfd_get_32 (abfd, buf);
c906108c
SS
10359}
10360
21ae7a4d
JK
10361static int
10362read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10363{
10364 return bfd_get_signed_32 (abfd, buf);
10365}
10366
93311388 10367static ULONGEST
fe1b8b76 10368read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10369{
fe1b8b76 10370 return bfd_get_64 (abfd, buf);
c906108c
SS
10371}
10372
10373static CORE_ADDR
fe1b8b76 10374read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10375 unsigned int *bytes_read)
c906108c 10376{
e7c27a73 10377 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10378 CORE_ADDR retval = 0;
10379
107d2387 10380 if (cu_header->signed_addr_p)
c906108c 10381 {
107d2387
AC
10382 switch (cu_header->addr_size)
10383 {
10384 case 2:
fe1b8b76 10385 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10386 break;
10387 case 4:
fe1b8b76 10388 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10389 break;
10390 case 8:
fe1b8b76 10391 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10392 break;
10393 default:
8e65ff28 10394 internal_error (__FILE__, __LINE__,
e2e0b3e5 10395 _("read_address: bad switch, signed [in module %s]"),
659b0389 10396 bfd_get_filename (abfd));
107d2387
AC
10397 }
10398 }
10399 else
10400 {
10401 switch (cu_header->addr_size)
10402 {
10403 case 2:
fe1b8b76 10404 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10405 break;
10406 case 4:
fe1b8b76 10407 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10408 break;
10409 case 8:
fe1b8b76 10410 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10411 break;
10412 default:
8e65ff28 10413 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10414 _("read_address: bad switch, "
10415 "unsigned [in module %s]"),
659b0389 10416 bfd_get_filename (abfd));
107d2387 10417 }
c906108c 10418 }
64367e0a 10419
107d2387
AC
10420 *bytes_read = cu_header->addr_size;
10421 return retval;
c906108c
SS
10422}
10423
f7ef9339 10424/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10425 specification allows the initial length to take up either 4 bytes
10426 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10427 bytes describe the length and all offsets will be 8 bytes in length
10428 instead of 4.
10429
f7ef9339
KB
10430 An older, non-standard 64-bit format is also handled by this
10431 function. The older format in question stores the initial length
10432 as an 8-byte quantity without an escape value. Lengths greater
10433 than 2^32 aren't very common which means that the initial 4 bytes
10434 is almost always zero. Since a length value of zero doesn't make
10435 sense for the 32-bit format, this initial zero can be considered to
10436 be an escape value which indicates the presence of the older 64-bit
10437 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10438 greater than 4GB. If it becomes necessary to handle lengths
10439 somewhat larger than 4GB, we could allow other small values (such
10440 as the non-sensical values of 1, 2, and 3) to also be used as
10441 escape values indicating the presence of the old format.
f7ef9339 10442
917c78fc
MK
10443 The value returned via bytes_read should be used to increment the
10444 relevant pointer after calling read_initial_length().
c764a876 10445
613e1657
KB
10446 [ Note: read_initial_length() and read_offset() are based on the
10447 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10448 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10449 from:
10450
f7ef9339 10451 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10452
613e1657
KB
10453 This document is only a draft and is subject to change. (So beware.)
10454
f7ef9339 10455 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10456 determined empirically by examining 64-bit ELF files produced by
10457 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10458
10459 - Kevin, July 16, 2002
613e1657
KB
10460 ] */
10461
10462static LONGEST
c764a876 10463read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10464{
fe1b8b76 10465 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10466
dd373385 10467 if (length == 0xffffffff)
613e1657 10468 {
fe1b8b76 10469 length = bfd_get_64 (abfd, buf + 4);
613e1657 10470 *bytes_read = 12;
613e1657 10471 }
dd373385 10472 else if (length == 0)
f7ef9339 10473 {
dd373385 10474 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10475 length = bfd_get_64 (abfd, buf);
f7ef9339 10476 *bytes_read = 8;
f7ef9339 10477 }
613e1657
KB
10478 else
10479 {
10480 *bytes_read = 4;
613e1657
KB
10481 }
10482
c764a876
DE
10483 return length;
10484}
dd373385 10485
c764a876
DE
10486/* Cover function for read_initial_length.
10487 Returns the length of the object at BUF, and stores the size of the
10488 initial length in *BYTES_READ and stores the size that offsets will be in
10489 *OFFSET_SIZE.
10490 If the initial length size is not equivalent to that specified in
10491 CU_HEADER then issue a complaint.
10492 This is useful when reading non-comp-unit headers. */
dd373385 10493
c764a876
DE
10494static LONGEST
10495read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10496 const struct comp_unit_head *cu_header,
10497 unsigned int *bytes_read,
10498 unsigned int *offset_size)
10499{
10500 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10501
10502 gdb_assert (cu_header->initial_length_size == 4
10503 || cu_header->initial_length_size == 8
10504 || cu_header->initial_length_size == 12);
10505
10506 if (cu_header->initial_length_size != *bytes_read)
10507 complaint (&symfile_complaints,
10508 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10509
c764a876 10510 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10511 return length;
613e1657
KB
10512}
10513
10514/* Read an offset from the data stream. The size of the offset is
917c78fc 10515 given by cu_header->offset_size. */
613e1657
KB
10516
10517static LONGEST
fe1b8b76 10518read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10519 unsigned int *bytes_read)
c764a876
DE
10520{
10521 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10522
c764a876
DE
10523 *bytes_read = cu_header->offset_size;
10524 return offset;
10525}
10526
10527/* Read an offset from the data stream. */
10528
10529static LONGEST
10530read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10531{
10532 LONGEST retval = 0;
10533
c764a876 10534 switch (offset_size)
613e1657
KB
10535 {
10536 case 4:
fe1b8b76 10537 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10538 break;
10539 case 8:
fe1b8b76 10540 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10541 break;
10542 default:
8e65ff28 10543 internal_error (__FILE__, __LINE__,
c764a876 10544 _("read_offset_1: bad switch [in module %s]"),
659b0389 10545 bfd_get_filename (abfd));
613e1657
KB
10546 }
10547
917c78fc 10548 return retval;
613e1657
KB
10549}
10550
fe1b8b76
JB
10551static gdb_byte *
10552read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10553{
10554 /* If the size of a host char is 8 bits, we can return a pointer
10555 to the buffer, otherwise we have to copy the data to a buffer
10556 allocated on the temporary obstack. */
4bdf3d34 10557 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10558 return buf;
c906108c
SS
10559}
10560
10561static char *
9b1c24c8 10562read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10563{
10564 /* If the size of a host char is 8 bits, we can return a pointer
10565 to the string, otherwise we have to copy the string to a buffer
10566 allocated on the temporary obstack. */
4bdf3d34 10567 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10568 if (*buf == '\0')
10569 {
10570 *bytes_read_ptr = 1;
10571 return NULL;
10572 }
fe1b8b76
JB
10573 *bytes_read_ptr = strlen ((char *) buf) + 1;
10574 return (char *) buf;
4bdf3d34
JJ
10575}
10576
10577static char *
cf2c3c16 10578read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10579{
be391dca 10580 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10581 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10582 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10583 bfd_get_filename (abfd));
dce234bc 10584 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10585 error (_("DW_FORM_strp pointing outside of "
10586 ".debug_str section [in module %s]"),
10587 bfd_get_filename (abfd));
4bdf3d34 10588 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10589 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10590 return NULL;
dce234bc 10591 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10592}
10593
cf2c3c16
TT
10594static char *
10595read_indirect_string (bfd *abfd, gdb_byte *buf,
10596 const struct comp_unit_head *cu_header,
10597 unsigned int *bytes_read_ptr)
10598{
10599 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10600
10601 return read_indirect_string_at_offset (abfd, str_offset);
10602}
10603
ce5d95e1 10604static unsigned long
fe1b8b76 10605read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10606{
ce5d95e1
JB
10607 unsigned long result;
10608 unsigned int num_read;
c906108c
SS
10609 int i, shift;
10610 unsigned char byte;
10611
10612 result = 0;
10613 shift = 0;
10614 num_read = 0;
10615 i = 0;
10616 while (1)
10617 {
fe1b8b76 10618 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10619 buf++;
10620 num_read++;
ce5d95e1 10621 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10622 if ((byte & 128) == 0)
10623 {
10624 break;
10625 }
10626 shift += 7;
10627 }
10628 *bytes_read_ptr = num_read;
10629 return result;
10630}
10631
ce5d95e1 10632static long
fe1b8b76 10633read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10634{
ce5d95e1 10635 long result;
77e0b926 10636 int i, shift, num_read;
c906108c
SS
10637 unsigned char byte;
10638
10639 result = 0;
10640 shift = 0;
c906108c
SS
10641 num_read = 0;
10642 i = 0;
10643 while (1)
10644 {
fe1b8b76 10645 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10646 buf++;
10647 num_read++;
ce5d95e1 10648 result |= ((long)(byte & 127) << shift);
c906108c
SS
10649 shift += 7;
10650 if ((byte & 128) == 0)
10651 {
10652 break;
10653 }
10654 }
77e0b926
DJ
10655 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10656 result |= -(((long)1) << shift);
c906108c
SS
10657 *bytes_read_ptr = num_read;
10658 return result;
10659}
10660
4bb7a0a7
DJ
10661/* Return a pointer to just past the end of an LEB128 number in BUF. */
10662
fe1b8b76
JB
10663static gdb_byte *
10664skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10665{
10666 int byte;
10667
10668 while (1)
10669 {
fe1b8b76 10670 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10671 buf++;
10672 if ((byte & 128) == 0)
10673 return buf;
10674 }
10675}
10676
c906108c 10677static void
e142c38c 10678set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10679{
10680 switch (lang)
10681 {
10682 case DW_LANG_C89:
76bee0cc 10683 case DW_LANG_C99:
c906108c 10684 case DW_LANG_C:
e142c38c 10685 cu->language = language_c;
c906108c
SS
10686 break;
10687 case DW_LANG_C_plus_plus:
e142c38c 10688 cu->language = language_cplus;
c906108c 10689 break;
6aecb9c2
JB
10690 case DW_LANG_D:
10691 cu->language = language_d;
10692 break;
c906108c
SS
10693 case DW_LANG_Fortran77:
10694 case DW_LANG_Fortran90:
b21b22e0 10695 case DW_LANG_Fortran95:
e142c38c 10696 cu->language = language_fortran;
c906108c
SS
10697 break;
10698 case DW_LANG_Mips_Assembler:
e142c38c 10699 cu->language = language_asm;
c906108c 10700 break;
bebd888e 10701 case DW_LANG_Java:
e142c38c 10702 cu->language = language_java;
bebd888e 10703 break;
c906108c 10704 case DW_LANG_Ada83:
8aaf0b47 10705 case DW_LANG_Ada95:
bc5f45f8
JB
10706 cu->language = language_ada;
10707 break;
72019c9c
GM
10708 case DW_LANG_Modula2:
10709 cu->language = language_m2;
10710 break;
fe8e67fd
PM
10711 case DW_LANG_Pascal83:
10712 cu->language = language_pascal;
10713 break;
22566fbd
DJ
10714 case DW_LANG_ObjC:
10715 cu->language = language_objc;
10716 break;
c906108c
SS
10717 case DW_LANG_Cobol74:
10718 case DW_LANG_Cobol85:
c906108c 10719 default:
e142c38c 10720 cu->language = language_minimal;
c906108c
SS
10721 break;
10722 }
e142c38c 10723 cu->language_defn = language_def (cu->language);
c906108c
SS
10724}
10725
10726/* Return the named attribute or NULL if not there. */
10727
10728static struct attribute *
e142c38c 10729dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10730{
10731 unsigned int i;
10732 struct attribute *spec = NULL;
10733
10734 for (i = 0; i < die->num_attrs; ++i)
10735 {
10736 if (die->attrs[i].name == name)
10b3939b 10737 return &die->attrs[i];
c906108c
SS
10738 if (die->attrs[i].name == DW_AT_specification
10739 || die->attrs[i].name == DW_AT_abstract_origin)
10740 spec = &die->attrs[i];
10741 }
c906108c 10742
10b3939b 10743 if (spec)
f2f0e013
DJ
10744 {
10745 die = follow_die_ref (die, spec, &cu);
10746 return dwarf2_attr (die, name, cu);
10747 }
c5aa993b 10748
c906108c
SS
10749 return NULL;
10750}
10751
348e048f
DE
10752/* Return the named attribute or NULL if not there,
10753 but do not follow DW_AT_specification, etc.
10754 This is for use in contexts where we're reading .debug_types dies.
10755 Following DW_AT_specification, DW_AT_abstract_origin will take us
10756 back up the chain, and we want to go down. */
10757
10758static struct attribute *
10759dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10760 struct dwarf2_cu *cu)
10761{
10762 unsigned int i;
10763
10764 for (i = 0; i < die->num_attrs; ++i)
10765 if (die->attrs[i].name == name)
10766 return &die->attrs[i];
10767
10768 return NULL;
10769}
10770
05cf31d1
JB
10771/* Return non-zero iff the attribute NAME is defined for the given DIE,
10772 and holds a non-zero value. This function should only be used for
2dc7f7b3 10773 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10774
10775static int
10776dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10777{
10778 struct attribute *attr = dwarf2_attr (die, name, cu);
10779
10780 return (attr && DW_UNSND (attr));
10781}
10782
3ca72b44 10783static int
e142c38c 10784die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10785{
05cf31d1
JB
10786 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10787 which value is non-zero. However, we have to be careful with
10788 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10789 (via dwarf2_flag_true_p) follows this attribute. So we may
10790 end up accidently finding a declaration attribute that belongs
10791 to a different DIE referenced by the specification attribute,
10792 even though the given DIE does not have a declaration attribute. */
10793 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10794 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10795}
10796
63d06c5c 10797/* Return the die giving the specification for DIE, if there is
f2f0e013 10798 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10799 containing the return value on output. If there is no
10800 specification, but there is an abstract origin, that is
10801 returned. */
63d06c5c
DC
10802
10803static struct die_info *
f2f0e013 10804die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10805{
f2f0e013
DJ
10806 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10807 *spec_cu);
63d06c5c 10808
edb3359d
DJ
10809 if (spec_attr == NULL)
10810 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10811
63d06c5c
DC
10812 if (spec_attr == NULL)
10813 return NULL;
10814 else
f2f0e013 10815 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10816}
c906108c 10817
debd256d 10818/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10819 refers to.
10820 NOTE: This is also used as a "cleanup" function. */
10821
debd256d
JB
10822static void
10823free_line_header (struct line_header *lh)
10824{
10825 if (lh->standard_opcode_lengths)
a8bc7b56 10826 xfree (lh->standard_opcode_lengths);
debd256d
JB
10827
10828 /* Remember that all the lh->file_names[i].name pointers are
10829 pointers into debug_line_buffer, and don't need to be freed. */
10830 if (lh->file_names)
a8bc7b56 10831 xfree (lh->file_names);
debd256d
JB
10832
10833 /* Similarly for the include directory names. */
10834 if (lh->include_dirs)
a8bc7b56 10835 xfree (lh->include_dirs);
debd256d 10836
a8bc7b56 10837 xfree (lh);
debd256d
JB
10838}
10839
debd256d 10840/* Add an entry to LH's include directory table. */
ae2de4f8 10841
debd256d
JB
10842static void
10843add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10844{
debd256d
JB
10845 /* Grow the array if necessary. */
10846 if (lh->include_dirs_size == 0)
c5aa993b 10847 {
debd256d
JB
10848 lh->include_dirs_size = 1; /* for testing */
10849 lh->include_dirs = xmalloc (lh->include_dirs_size
10850 * sizeof (*lh->include_dirs));
10851 }
10852 else if (lh->num_include_dirs >= lh->include_dirs_size)
10853 {
10854 lh->include_dirs_size *= 2;
10855 lh->include_dirs = xrealloc (lh->include_dirs,
10856 (lh->include_dirs_size
10857 * sizeof (*lh->include_dirs)));
c5aa993b 10858 }
c906108c 10859
debd256d
JB
10860 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10861}
6e70227d 10862
debd256d 10863/* Add an entry to LH's file name table. */
ae2de4f8 10864
debd256d
JB
10865static void
10866add_file_name (struct line_header *lh,
10867 char *name,
10868 unsigned int dir_index,
10869 unsigned int mod_time,
10870 unsigned int length)
10871{
10872 struct file_entry *fe;
10873
10874 /* Grow the array if necessary. */
10875 if (lh->file_names_size == 0)
10876 {
10877 lh->file_names_size = 1; /* for testing */
10878 lh->file_names = xmalloc (lh->file_names_size
10879 * sizeof (*lh->file_names));
10880 }
10881 else if (lh->num_file_names >= lh->file_names_size)
10882 {
10883 lh->file_names_size *= 2;
10884 lh->file_names = xrealloc (lh->file_names,
10885 (lh->file_names_size
10886 * sizeof (*lh->file_names)));
10887 }
10888
10889 fe = &lh->file_names[lh->num_file_names++];
10890 fe->name = name;
10891 fe->dir_index = dir_index;
10892 fe->mod_time = mod_time;
10893 fe->length = length;
aaa75496 10894 fe->included_p = 0;
cb1df416 10895 fe->symtab = NULL;
debd256d 10896}
6e70227d 10897
debd256d 10898/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10899 .debug_line, according to the endianness of ABFD. Return a pointer
10900 to a struct line_header, allocated using xmalloc.
debd256d
JB
10901
10902 NOTE: the strings in the include directory and file name tables of
10903 the returned object point into debug_line_buffer, and must not be
10904 freed. */
ae2de4f8 10905
debd256d
JB
10906static struct line_header *
10907dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10908 struct dwarf2_cu *cu)
debd256d
JB
10909{
10910 struct cleanup *back_to;
10911 struct line_header *lh;
fe1b8b76 10912 gdb_byte *line_ptr;
c764a876 10913 unsigned int bytes_read, offset_size;
debd256d
JB
10914 int i;
10915 char *cur_dir, *cur_file;
10916
be391dca 10917 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10918 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10919 {
e2e0b3e5 10920 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10921 return 0;
10922 }
10923
a738430d
MK
10924 /* Make sure that at least there's room for the total_length field.
10925 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10926 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10927 {
4d3c2250 10928 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10929 return 0;
10930 }
10931
10932 lh = xmalloc (sizeof (*lh));
10933 memset (lh, 0, sizeof (*lh));
10934 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10935 (void *) lh);
10936
dce234bc 10937 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10938
a738430d 10939 /* Read in the header. */
6e70227d 10940 lh->total_length =
c764a876
DE
10941 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10942 &bytes_read, &offset_size);
debd256d 10943 line_ptr += bytes_read;
dce234bc
PP
10944 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10945 + dwarf2_per_objfile->line.size))
debd256d 10946 {
4d3c2250 10947 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10948 return 0;
10949 }
10950 lh->statement_program_end = line_ptr + lh->total_length;
10951 lh->version = read_2_bytes (abfd, line_ptr);
10952 line_ptr += 2;
c764a876
DE
10953 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10954 line_ptr += offset_size;
debd256d
JB
10955 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10956 line_ptr += 1;
2dc7f7b3
TT
10957 if (lh->version >= 4)
10958 {
10959 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10960 line_ptr += 1;
10961 }
10962 else
10963 lh->maximum_ops_per_instruction = 1;
10964
10965 if (lh->maximum_ops_per_instruction == 0)
10966 {
10967 lh->maximum_ops_per_instruction = 1;
10968 complaint (&symfile_complaints,
3e43a32a
MS
10969 _("invalid maximum_ops_per_instruction "
10970 "in `.debug_line' section"));
2dc7f7b3
TT
10971 }
10972
debd256d
JB
10973 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10974 line_ptr += 1;
10975 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10976 line_ptr += 1;
10977 lh->line_range = read_1_byte (abfd, line_ptr);
10978 line_ptr += 1;
10979 lh->opcode_base = read_1_byte (abfd, line_ptr);
10980 line_ptr += 1;
10981 lh->standard_opcode_lengths
fe1b8b76 10982 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10983
10984 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10985 for (i = 1; i < lh->opcode_base; ++i)
10986 {
10987 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10988 line_ptr += 1;
10989 }
10990
a738430d 10991 /* Read directory table. */
9b1c24c8 10992 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10993 {
10994 line_ptr += bytes_read;
10995 add_include_dir (lh, cur_dir);
10996 }
10997 line_ptr += bytes_read;
10998
a738430d 10999 /* Read file name table. */
9b1c24c8 11000 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11001 {
11002 unsigned int dir_index, mod_time, length;
11003
11004 line_ptr += bytes_read;
11005 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11006 line_ptr += bytes_read;
11007 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11008 line_ptr += bytes_read;
11009 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11010 line_ptr += bytes_read;
11011
11012 add_file_name (lh, cur_file, dir_index, mod_time, length);
11013 }
11014 line_ptr += bytes_read;
6e70227d 11015 lh->statement_program_start = line_ptr;
debd256d 11016
dce234bc
PP
11017 if (line_ptr > (dwarf2_per_objfile->line.buffer
11018 + dwarf2_per_objfile->line.size))
4d3c2250 11019 complaint (&symfile_complaints,
3e43a32a
MS
11020 _("line number info header doesn't "
11021 "fit in `.debug_line' section"));
debd256d
JB
11022
11023 discard_cleanups (back_to);
11024 return lh;
11025}
c906108c 11026
c6da4cef
DE
11027/* Subroutine of dwarf_decode_lines to simplify it.
11028 Return the file name of the psymtab for included file FILE_INDEX
11029 in line header LH of PST.
11030 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11031 If space for the result is malloc'd, it will be freed by a cleanup.
11032 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11033
11034static char *
11035psymtab_include_file_name (const struct line_header *lh, int file_index,
11036 const struct partial_symtab *pst,
11037 const char *comp_dir)
11038{
11039 const struct file_entry fe = lh->file_names [file_index];
11040 char *include_name = fe.name;
11041 char *include_name_to_compare = include_name;
11042 char *dir_name = NULL;
72b9f47f
TT
11043 const char *pst_filename;
11044 char *copied_name = NULL;
c6da4cef
DE
11045 int file_is_pst;
11046
11047 if (fe.dir_index)
11048 dir_name = lh->include_dirs[fe.dir_index - 1];
11049
11050 if (!IS_ABSOLUTE_PATH (include_name)
11051 && (dir_name != NULL || comp_dir != NULL))
11052 {
11053 /* Avoid creating a duplicate psymtab for PST.
11054 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11055 Before we do the comparison, however, we need to account
11056 for DIR_NAME and COMP_DIR.
11057 First prepend dir_name (if non-NULL). If we still don't
11058 have an absolute path prepend comp_dir (if non-NULL).
11059 However, the directory we record in the include-file's
11060 psymtab does not contain COMP_DIR (to match the
11061 corresponding symtab(s)).
11062
11063 Example:
11064
11065 bash$ cd /tmp
11066 bash$ gcc -g ./hello.c
11067 include_name = "hello.c"
11068 dir_name = "."
11069 DW_AT_comp_dir = comp_dir = "/tmp"
11070 DW_AT_name = "./hello.c" */
11071
11072 if (dir_name != NULL)
11073 {
11074 include_name = concat (dir_name, SLASH_STRING,
11075 include_name, (char *)NULL);
11076 include_name_to_compare = include_name;
11077 make_cleanup (xfree, include_name);
11078 }
11079 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11080 {
11081 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11082 include_name, (char *)NULL);
11083 }
11084 }
11085
11086 pst_filename = pst->filename;
11087 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11088 {
72b9f47f
TT
11089 copied_name = concat (pst->dirname, SLASH_STRING,
11090 pst_filename, (char *)NULL);
11091 pst_filename = copied_name;
c6da4cef
DE
11092 }
11093
1e3fad37 11094 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11095
11096 if (include_name_to_compare != include_name)
11097 xfree (include_name_to_compare);
72b9f47f
TT
11098 if (copied_name != NULL)
11099 xfree (copied_name);
c6da4cef
DE
11100
11101 if (file_is_pst)
11102 return NULL;
11103 return include_name;
11104}
11105
c91513d8
PP
11106/* Ignore this record_line request. */
11107
11108static void
11109noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11110{
11111 return;
11112}
11113
aaa75496
JB
11114/* Decode the Line Number Program (LNP) for the given line_header
11115 structure and CU. The actual information extracted and the type
11116 of structures created from the LNP depends on the value of PST.
11117
11118 1. If PST is NULL, then this procedure uses the data from the program
11119 to create all necessary symbol tables, and their linetables.
6e70227d 11120
aaa75496
JB
11121 2. If PST is not NULL, this procedure reads the program to determine
11122 the list of files included by the unit represented by PST, and
c6da4cef
DE
11123 builds all the associated partial symbol tables.
11124
11125 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11126 It is used for relative paths in the line table.
11127 NOTE: When processing partial symtabs (pst != NULL),
11128 comp_dir == pst->dirname.
11129
11130 NOTE: It is important that psymtabs have the same file name (via strcmp)
11131 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11132 symtab we don't use it in the name of the psymtabs we create.
11133 E.g. expand_line_sal requires this when finding psymtabs to expand.
11134 A good testcase for this is mb-inline.exp. */
debd256d 11135
c906108c 11136static void
72b9f47f 11137dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11138 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11139{
a8c50c1f 11140 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11141 gdb_byte *line_end;
a8c50c1f 11142 unsigned int bytes_read, extended_len;
c906108c 11143 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11144 CORE_ADDR baseaddr;
11145 struct objfile *objfile = cu->objfile;
fbf65064 11146 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11147 const int decode_for_pst_p = (pst != NULL);
cb1df416 11148 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11149 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11150 = record_line;
e142c38c
DJ
11151
11152 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11153
debd256d
JB
11154 line_ptr = lh->statement_program_start;
11155 line_end = lh->statement_program_end;
c906108c
SS
11156
11157 /* Read the statement sequences until there's nothing left. */
11158 while (line_ptr < line_end)
11159 {
11160 /* state machine registers */
11161 CORE_ADDR address = 0;
11162 unsigned int file = 1;
11163 unsigned int line = 1;
11164 unsigned int column = 0;
debd256d 11165 int is_stmt = lh->default_is_stmt;
c906108c
SS
11166 int basic_block = 0;
11167 int end_sequence = 0;
fbf65064 11168 CORE_ADDR addr;
2dc7f7b3 11169 unsigned char op_index = 0;
c906108c 11170
aaa75496 11171 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11172 {
aaa75496 11173 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11174 /* lh->include_dirs and lh->file_names are 0-based, but the
11175 directory and file name numbers in the statement program
11176 are 1-based. */
11177 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11178 char *dir = NULL;
a738430d 11179
debd256d
JB
11180 if (fe->dir_index)
11181 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11182
11183 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11184 }
11185
a738430d 11186 /* Decode the table. */
c5aa993b 11187 while (!end_sequence)
c906108c
SS
11188 {
11189 op_code = read_1_byte (abfd, line_ptr);
11190 line_ptr += 1;
59205f5a
JB
11191 if (line_ptr > line_end)
11192 {
11193 dwarf2_debug_line_missing_end_sequence_complaint ();
11194 break;
11195 }
9aa1fe7e 11196
debd256d 11197 if (op_code >= lh->opcode_base)
6e70227d 11198 {
a738430d 11199 /* Special operand. */
debd256d 11200 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11201 address += (((op_index + (adj_opcode / lh->line_range))
11202 / lh->maximum_ops_per_instruction)
11203 * lh->minimum_instruction_length);
11204 op_index = ((op_index + (adj_opcode / lh->line_range))
11205 % lh->maximum_ops_per_instruction);
debd256d 11206 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11207 if (lh->num_file_names < file || file == 0)
25e43795 11208 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11209 /* For now we ignore lines not starting on an
11210 instruction boundary. */
11211 else if (op_index == 0)
25e43795
DJ
11212 {
11213 lh->file_names[file - 1].included_p = 1;
ca5f395d 11214 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11215 {
11216 if (last_subfile != current_subfile)
11217 {
11218 addr = gdbarch_addr_bits_remove (gdbarch, address);
11219 if (last_subfile)
c91513d8 11220 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11221 last_subfile = current_subfile;
11222 }
25e43795 11223 /* Append row to matrix using current values. */
7019d805 11224 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11225 (*p_record_line) (current_subfile, line, addr);
366da635 11226 }
25e43795 11227 }
ca5f395d 11228 basic_block = 0;
9aa1fe7e
GK
11229 }
11230 else switch (op_code)
c906108c
SS
11231 {
11232 case DW_LNS_extended_op:
3e43a32a
MS
11233 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11234 &bytes_read);
473b7be6 11235 line_ptr += bytes_read;
a8c50c1f 11236 extended_end = line_ptr + extended_len;
c906108c
SS
11237 extended_op = read_1_byte (abfd, line_ptr);
11238 line_ptr += 1;
11239 switch (extended_op)
11240 {
11241 case DW_LNE_end_sequence:
c91513d8 11242 p_record_line = record_line;
c906108c 11243 end_sequence = 1;
c906108c
SS
11244 break;
11245 case DW_LNE_set_address:
e7c27a73 11246 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11247
11248 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11249 {
11250 /* This line table is for a function which has been
11251 GCd by the linker. Ignore it. PR gdb/12528 */
11252
11253 long line_offset
11254 = line_ptr - dwarf2_per_objfile->line.buffer;
11255
11256 complaint (&symfile_complaints,
11257 _(".debug_line address at offset 0x%lx is 0 "
11258 "[in module %s]"),
bb5ed363 11259 line_offset, objfile->name);
c91513d8
PP
11260 p_record_line = noop_record_line;
11261 }
11262
2dc7f7b3 11263 op_index = 0;
107d2387
AC
11264 line_ptr += bytes_read;
11265 address += baseaddr;
c906108c
SS
11266 break;
11267 case DW_LNE_define_file:
debd256d
JB
11268 {
11269 char *cur_file;
11270 unsigned int dir_index, mod_time, length;
6e70227d 11271
3e43a32a
MS
11272 cur_file = read_direct_string (abfd, line_ptr,
11273 &bytes_read);
debd256d
JB
11274 line_ptr += bytes_read;
11275 dir_index =
11276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11277 line_ptr += bytes_read;
11278 mod_time =
11279 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11280 line_ptr += bytes_read;
11281 length =
11282 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11283 line_ptr += bytes_read;
11284 add_file_name (lh, cur_file, dir_index, mod_time, length);
11285 }
c906108c 11286 break;
d0c6ba3d
CC
11287 case DW_LNE_set_discriminator:
11288 /* The discriminator is not interesting to the debugger;
11289 just ignore it. */
11290 line_ptr = extended_end;
11291 break;
c906108c 11292 default:
4d3c2250 11293 complaint (&symfile_complaints,
e2e0b3e5 11294 _("mangled .debug_line section"));
debd256d 11295 return;
c906108c 11296 }
a8c50c1f
DJ
11297 /* Make sure that we parsed the extended op correctly. If e.g.
11298 we expected a different address size than the producer used,
11299 we may have read the wrong number of bytes. */
11300 if (line_ptr != extended_end)
11301 {
11302 complaint (&symfile_complaints,
11303 _("mangled .debug_line section"));
11304 return;
11305 }
c906108c
SS
11306 break;
11307 case DW_LNS_copy:
59205f5a 11308 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11309 dwarf2_debug_line_missing_file_complaint ();
11310 else
366da635 11311 {
25e43795 11312 lh->file_names[file - 1].included_p = 1;
ca5f395d 11313 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11314 {
11315 if (last_subfile != current_subfile)
11316 {
11317 addr = gdbarch_addr_bits_remove (gdbarch, address);
11318 if (last_subfile)
c91513d8 11319 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11320 last_subfile = current_subfile;
11321 }
7019d805 11322 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11323 (*p_record_line) (current_subfile, line, addr);
fbf65064 11324 }
366da635 11325 }
c906108c
SS
11326 basic_block = 0;
11327 break;
11328 case DW_LNS_advance_pc:
2dc7f7b3
TT
11329 {
11330 CORE_ADDR adjust
11331 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11332
11333 address += (((op_index + adjust)
11334 / lh->maximum_ops_per_instruction)
11335 * lh->minimum_instruction_length);
11336 op_index = ((op_index + adjust)
11337 % lh->maximum_ops_per_instruction);
11338 line_ptr += bytes_read;
11339 }
c906108c
SS
11340 break;
11341 case DW_LNS_advance_line:
11342 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11343 line_ptr += bytes_read;
11344 break;
11345 case DW_LNS_set_file:
debd256d 11346 {
a738430d
MK
11347 /* The arrays lh->include_dirs and lh->file_names are
11348 0-based, but the directory and file name numbers in
11349 the statement program are 1-based. */
debd256d 11350 struct file_entry *fe;
4f1520fb 11351 char *dir = NULL;
a738430d 11352
debd256d
JB
11353 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11354 line_ptr += bytes_read;
59205f5a 11355 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11356 dwarf2_debug_line_missing_file_complaint ();
11357 else
11358 {
11359 fe = &lh->file_names[file - 1];
11360 if (fe->dir_index)
11361 dir = lh->include_dirs[fe->dir_index - 1];
11362 if (!decode_for_pst_p)
11363 {
11364 last_subfile = current_subfile;
11365 dwarf2_start_subfile (fe->name, dir, comp_dir);
11366 }
11367 }
debd256d 11368 }
c906108c
SS
11369 break;
11370 case DW_LNS_set_column:
11371 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11372 line_ptr += bytes_read;
11373 break;
11374 case DW_LNS_negate_stmt:
11375 is_stmt = (!is_stmt);
11376 break;
11377 case DW_LNS_set_basic_block:
11378 basic_block = 1;
11379 break;
c2c6d25f
JM
11380 /* Add to the address register of the state machine the
11381 address increment value corresponding to special opcode
a738430d
MK
11382 255. I.e., this value is scaled by the minimum
11383 instruction length since special opcode 255 would have
b021a221 11384 scaled the increment. */
c906108c 11385 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11386 {
11387 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11388
11389 address += (((op_index + adjust)
11390 / lh->maximum_ops_per_instruction)
11391 * lh->minimum_instruction_length);
11392 op_index = ((op_index + adjust)
11393 % lh->maximum_ops_per_instruction);
11394 }
c906108c
SS
11395 break;
11396 case DW_LNS_fixed_advance_pc:
11397 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11398 op_index = 0;
c906108c
SS
11399 line_ptr += 2;
11400 break;
9aa1fe7e 11401 default:
a738430d
MK
11402 {
11403 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11404 int i;
a738430d 11405
debd256d 11406 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11407 {
11408 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11409 line_ptr += bytes_read;
11410 }
11411 }
c906108c
SS
11412 }
11413 }
59205f5a
JB
11414 if (lh->num_file_names < file || file == 0)
11415 dwarf2_debug_line_missing_file_complaint ();
11416 else
11417 {
11418 lh->file_names[file - 1].included_p = 1;
11419 if (!decode_for_pst_p)
fbf65064
UW
11420 {
11421 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11422 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11423 }
59205f5a 11424 }
c906108c 11425 }
aaa75496
JB
11426
11427 if (decode_for_pst_p)
11428 {
11429 int file_index;
11430
11431 /* Now that we're done scanning the Line Header Program, we can
11432 create the psymtab of each included file. */
11433 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11434 if (lh->file_names[file_index].included_p == 1)
11435 {
c6da4cef
DE
11436 char *include_name =
11437 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11438 if (include_name != NULL)
aaa75496
JB
11439 dwarf2_create_include_psymtab (include_name, pst, objfile);
11440 }
11441 }
cb1df416
DJ
11442 else
11443 {
11444 /* Make sure a symtab is created for every file, even files
11445 which contain only variables (i.e. no code with associated
11446 line numbers). */
11447
11448 int i;
11449 struct file_entry *fe;
11450
11451 for (i = 0; i < lh->num_file_names; i++)
11452 {
11453 char *dir = NULL;
9a619af0 11454
cb1df416
DJ
11455 fe = &lh->file_names[i];
11456 if (fe->dir_index)
11457 dir = lh->include_dirs[fe->dir_index - 1];
11458 dwarf2_start_subfile (fe->name, dir, comp_dir);
11459
11460 /* Skip the main file; we don't need it, and it must be
11461 allocated last, so that it will show up before the
11462 non-primary symtabs in the objfile's symtab list. */
11463 if (current_subfile == first_subfile)
11464 continue;
11465
11466 if (current_subfile->symtab == NULL)
11467 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11468 objfile);
cb1df416
DJ
11469 fe->symtab = current_subfile->symtab;
11470 }
11471 }
c906108c
SS
11472}
11473
11474/* Start a subfile for DWARF. FILENAME is the name of the file and
11475 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11476 or NULL if not known. COMP_DIR is the compilation directory for the
11477 linetable's compilation unit or NULL if not known.
c906108c
SS
11478 This routine tries to keep line numbers from identical absolute and
11479 relative file names in a common subfile.
11480
11481 Using the `list' example from the GDB testsuite, which resides in
11482 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11483 of /srcdir/list0.c yields the following debugging information for list0.c:
11484
c5aa993b
JM
11485 DW_AT_name: /srcdir/list0.c
11486 DW_AT_comp_dir: /compdir
357e46e7 11487 files.files[0].name: list0.h
c5aa993b 11488 files.files[0].dir: /srcdir
357e46e7 11489 files.files[1].name: list0.c
c5aa993b 11490 files.files[1].dir: /srcdir
c906108c
SS
11491
11492 The line number information for list0.c has to end up in a single
4f1520fb
FR
11493 subfile, so that `break /srcdir/list0.c:1' works as expected.
11494 start_subfile will ensure that this happens provided that we pass the
11495 concatenation of files.files[1].dir and files.files[1].name as the
11496 subfile's name. */
c906108c
SS
11497
11498static void
3e43a32a
MS
11499dwarf2_start_subfile (char *filename, const char *dirname,
11500 const char *comp_dir)
c906108c 11501{
4f1520fb
FR
11502 char *fullname;
11503
11504 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11505 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11506 second argument to start_subfile. To be consistent, we do the
11507 same here. In order not to lose the line information directory,
11508 we concatenate it to the filename when it makes sense.
11509 Note that the Dwarf3 standard says (speaking of filenames in line
11510 information): ``The directory index is ignored for file names
11511 that represent full path names''. Thus ignoring dirname in the
11512 `else' branch below isn't an issue. */
c906108c 11513
d5166ae1 11514 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11515 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11516 else
11517 fullname = filename;
c906108c 11518
4f1520fb
FR
11519 start_subfile (fullname, comp_dir);
11520
11521 if (fullname != filename)
11522 xfree (fullname);
c906108c
SS
11523}
11524
4c2df51b
DJ
11525static void
11526var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11527 struct dwarf2_cu *cu)
4c2df51b 11528{
e7c27a73
DJ
11529 struct objfile *objfile = cu->objfile;
11530 struct comp_unit_head *cu_header = &cu->header;
11531
4c2df51b
DJ
11532 /* NOTE drow/2003-01-30: There used to be a comment and some special
11533 code here to turn a symbol with DW_AT_external and a
11534 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11535 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11536 with some versions of binutils) where shared libraries could have
11537 relocations against symbols in their debug information - the
11538 minimal symbol would have the right address, but the debug info
11539 would not. It's no longer necessary, because we will explicitly
11540 apply relocations when we read in the debug information now. */
11541
11542 /* A DW_AT_location attribute with no contents indicates that a
11543 variable has been optimized away. */
11544 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11545 {
11546 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11547 return;
11548 }
11549
11550 /* Handle one degenerate form of location expression specially, to
11551 preserve GDB's previous behavior when section offsets are
11552 specified. If this is just a DW_OP_addr then mark this symbol
11553 as LOC_STATIC. */
11554
11555 if (attr_form_is_block (attr)
11556 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11557 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11558 {
891d2f0b 11559 unsigned int dummy;
4c2df51b
DJ
11560
11561 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11562 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11563 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11564 fixup_symbol_section (sym, objfile);
11565 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11566 SYMBOL_SECTION (sym));
4c2df51b
DJ
11567 return;
11568 }
11569
11570 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11571 expression evaluator, and use LOC_COMPUTED only when necessary
11572 (i.e. when the value of a register or memory location is
11573 referenced, or a thread-local block, etc.). Then again, it might
11574 not be worthwhile. I'm assuming that it isn't unless performance
11575 or memory numbers show me otherwise. */
11576
e7c27a73 11577 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11578 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11579
11580 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11581 cu->has_loclist = 1;
4c2df51b
DJ
11582}
11583
c906108c
SS
11584/* Given a pointer to a DWARF information entry, figure out if we need
11585 to make a symbol table entry for it, and if so, create a new entry
11586 and return a pointer to it.
11587 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11588 used the passed type.
11589 If SPACE is not NULL, use it to hold the new symbol. If it is
11590 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11591
11592static struct symbol *
34eaf542
TT
11593new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11594 struct symbol *space)
c906108c 11595{
e7c27a73 11596 struct objfile *objfile = cu->objfile;
c906108c
SS
11597 struct symbol *sym = NULL;
11598 char *name;
11599 struct attribute *attr = NULL;
11600 struct attribute *attr2 = NULL;
e142c38c 11601 CORE_ADDR baseaddr;
e37fd15a
SW
11602 struct pending **list_to_add = NULL;
11603
edb3359d 11604 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11605
11606 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11607
94af9270 11608 name = dwarf2_name (die, cu);
c906108c
SS
11609 if (name)
11610 {
94af9270 11611 const char *linkagename;
34eaf542 11612 int suppress_add = 0;
94af9270 11613
34eaf542
TT
11614 if (space)
11615 sym = space;
11616 else
11617 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11618 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11619
11620 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11621 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11622 linkagename = dwarf2_physname (name, die, cu);
11623 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11624
f55ee35c
JK
11625 /* Fortran does not have mangling standard and the mangling does differ
11626 between gfortran, iFort etc. */
11627 if (cu->language == language_fortran
b250c185 11628 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11629 symbol_set_demangled_name (&(sym->ginfo),
11630 (char *) dwarf2_full_name (name, die, cu),
11631 NULL);
f55ee35c 11632
c906108c 11633 /* Default assumptions.
c5aa993b 11634 Use the passed type or decode it from the die. */
176620f1 11635 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11636 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11637 if (type != NULL)
11638 SYMBOL_TYPE (sym) = type;
11639 else
e7c27a73 11640 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11641 attr = dwarf2_attr (die,
11642 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11643 cu);
c906108c
SS
11644 if (attr)
11645 {
11646 SYMBOL_LINE (sym) = DW_UNSND (attr);
11647 }
cb1df416 11648
edb3359d
DJ
11649 attr = dwarf2_attr (die,
11650 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11651 cu);
cb1df416
DJ
11652 if (attr)
11653 {
11654 int file_index = DW_UNSND (attr);
9a619af0 11655
cb1df416
DJ
11656 if (cu->line_header == NULL
11657 || file_index > cu->line_header->num_file_names)
11658 complaint (&symfile_complaints,
11659 _("file index out of range"));
1c3d648d 11660 else if (file_index > 0)
cb1df416
DJ
11661 {
11662 struct file_entry *fe;
9a619af0 11663
cb1df416
DJ
11664 fe = &cu->line_header->file_names[file_index - 1];
11665 SYMBOL_SYMTAB (sym) = fe->symtab;
11666 }
11667 }
11668
c906108c
SS
11669 switch (die->tag)
11670 {
11671 case DW_TAG_label:
e142c38c 11672 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11673 if (attr)
11674 {
11675 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11676 }
0f5238ed
TT
11677 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11678 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11679 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11680 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11681 break;
11682 case DW_TAG_subprogram:
11683 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11684 finish_block. */
11685 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11686 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11687 if ((attr2 && (DW_UNSND (attr2) != 0))
11688 || cu->language == language_ada)
c906108c 11689 {
2cfa0c8d
JB
11690 /* Subprograms marked external are stored as a global symbol.
11691 Ada subprograms, whether marked external or not, are always
11692 stored as a global symbol, because we want to be able to
11693 access them globally. For instance, we want to be able
11694 to break on a nested subprogram without having to
11695 specify the context. */
e37fd15a 11696 list_to_add = &global_symbols;
c906108c
SS
11697 }
11698 else
11699 {
e37fd15a 11700 list_to_add = cu->list_in_scope;
c906108c
SS
11701 }
11702 break;
edb3359d
DJ
11703 case DW_TAG_inlined_subroutine:
11704 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11705 finish_block. */
11706 SYMBOL_CLASS (sym) = LOC_BLOCK;
11707 SYMBOL_INLINED (sym) = 1;
11708 /* Do not add the symbol to any lists. It will be found via
11709 BLOCK_FUNCTION from the blockvector. */
11710 break;
34eaf542
TT
11711 case DW_TAG_template_value_param:
11712 suppress_add = 1;
11713 /* Fall through. */
72929c62 11714 case DW_TAG_constant:
c906108c 11715 case DW_TAG_variable:
254e6b9e 11716 case DW_TAG_member:
0963b4bd
MS
11717 /* Compilation with minimal debug info may result in
11718 variables with missing type entries. Change the
11719 misleading `void' type to something sensible. */
c906108c 11720 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11721 SYMBOL_TYPE (sym)
46bf5051 11722 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11723
e142c38c 11724 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11725 /* In the case of DW_TAG_member, we should only be called for
11726 static const members. */
11727 if (die->tag == DW_TAG_member)
11728 {
3863f96c
DE
11729 /* dwarf2_add_field uses die_is_declaration,
11730 so we do the same. */
254e6b9e
DE
11731 gdb_assert (die_is_declaration (die, cu));
11732 gdb_assert (attr);
11733 }
c906108c
SS
11734 if (attr)
11735 {
e7c27a73 11736 dwarf2_const_value (attr, sym, cu);
e142c38c 11737 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11738 if (!suppress_add)
34eaf542
TT
11739 {
11740 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11741 list_to_add = &global_symbols;
34eaf542 11742 else
e37fd15a 11743 list_to_add = cu->list_in_scope;
34eaf542 11744 }
c906108c
SS
11745 break;
11746 }
e142c38c 11747 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11748 if (attr)
11749 {
e7c27a73 11750 var_decode_location (attr, sym, cu);
e142c38c 11751 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11752 if (SYMBOL_CLASS (sym) == LOC_STATIC
11753 && SYMBOL_VALUE_ADDRESS (sym) == 0
11754 && !dwarf2_per_objfile->has_section_at_zero)
11755 {
11756 /* When a static variable is eliminated by the linker,
11757 the corresponding debug information is not stripped
11758 out, but the variable address is set to null;
11759 do not add such variables into symbol table. */
11760 }
11761 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11762 {
f55ee35c
JK
11763 /* Workaround gfortran PR debug/40040 - it uses
11764 DW_AT_location for variables in -fPIC libraries which may
11765 get overriden by other libraries/executable and get
11766 a different address. Resolve it by the minimal symbol
11767 which may come from inferior's executable using copy
11768 relocation. Make this workaround only for gfortran as for
11769 other compilers GDB cannot guess the minimal symbol
11770 Fortran mangling kind. */
11771 if (cu->language == language_fortran && die->parent
11772 && die->parent->tag == DW_TAG_module
11773 && cu->producer
11774 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11775 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11776
1c809c68
TT
11777 /* A variable with DW_AT_external is never static,
11778 but it may be block-scoped. */
11779 list_to_add = (cu->list_in_scope == &file_symbols
11780 ? &global_symbols : cu->list_in_scope);
1c809c68 11781 }
c906108c 11782 else
e37fd15a 11783 list_to_add = cu->list_in_scope;
c906108c
SS
11784 }
11785 else
11786 {
11787 /* We do not know the address of this symbol.
c5aa993b
JM
11788 If it is an external symbol and we have type information
11789 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11790 The address of the variable will then be determined from
11791 the minimal symbol table whenever the variable is
11792 referenced. */
e142c38c 11793 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11794 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11795 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11796 {
0fe7935b
DJ
11797 /* A variable with DW_AT_external is never static, but it
11798 may be block-scoped. */
11799 list_to_add = (cu->list_in_scope == &file_symbols
11800 ? &global_symbols : cu->list_in_scope);
11801
c906108c 11802 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11803 }
442ddf59
JK
11804 else if (!die_is_declaration (die, cu))
11805 {
11806 /* Use the default LOC_OPTIMIZED_OUT class. */
11807 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11808 if (!suppress_add)
11809 list_to_add = cu->list_in_scope;
442ddf59 11810 }
c906108c
SS
11811 }
11812 break;
11813 case DW_TAG_formal_parameter:
edb3359d
DJ
11814 /* If we are inside a function, mark this as an argument. If
11815 not, we might be looking at an argument to an inlined function
11816 when we do not have enough information to show inlined frames;
11817 pretend it's a local variable in that case so that the user can
11818 still see it. */
11819 if (context_stack_depth > 0
11820 && context_stack[context_stack_depth - 1].name != NULL)
11821 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11822 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11823 if (attr)
11824 {
e7c27a73 11825 var_decode_location (attr, sym, cu);
c906108c 11826 }
e142c38c 11827 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11828 if (attr)
11829 {
e7c27a73 11830 dwarf2_const_value (attr, sym, cu);
c906108c 11831 }
f346a30d 11832
e37fd15a 11833 list_to_add = cu->list_in_scope;
c906108c
SS
11834 break;
11835 case DW_TAG_unspecified_parameters:
11836 /* From varargs functions; gdb doesn't seem to have any
11837 interest in this information, so just ignore it for now.
11838 (FIXME?) */
11839 break;
34eaf542
TT
11840 case DW_TAG_template_type_param:
11841 suppress_add = 1;
11842 /* Fall through. */
c906108c 11843 case DW_TAG_class_type:
680b30c7 11844 case DW_TAG_interface_type:
c906108c
SS
11845 case DW_TAG_structure_type:
11846 case DW_TAG_union_type:
72019c9c 11847 case DW_TAG_set_type:
c906108c
SS
11848 case DW_TAG_enumeration_type:
11849 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11850 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11851
63d06c5c 11852 {
987504bb 11853 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11854 really ever be static objects: otherwise, if you try
11855 to, say, break of a class's method and you're in a file
11856 which doesn't mention that class, it won't work unless
11857 the check for all static symbols in lookup_symbol_aux
11858 saves you. See the OtherFileClass tests in
11859 gdb.c++/namespace.exp. */
11860
e37fd15a 11861 if (!suppress_add)
34eaf542 11862 {
34eaf542
TT
11863 list_to_add = (cu->list_in_scope == &file_symbols
11864 && (cu->language == language_cplus
11865 || cu->language == language_java)
11866 ? &global_symbols : cu->list_in_scope);
63d06c5c 11867
64382290
TT
11868 /* The semantics of C++ state that "struct foo {
11869 ... }" also defines a typedef for "foo". A Java
11870 class declaration also defines a typedef for the
11871 class. */
11872 if (cu->language == language_cplus
11873 || cu->language == language_java
11874 || cu->language == language_ada)
11875 {
11876 /* The symbol's name is already allocated along
11877 with this objfile, so we don't need to
11878 duplicate it for the type. */
11879 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11880 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11881 }
63d06c5c
DC
11882 }
11883 }
c906108c
SS
11884 break;
11885 case DW_TAG_typedef:
63d06c5c
DC
11886 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11887 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11888 list_to_add = cu->list_in_scope;
63d06c5c 11889 break;
c906108c 11890 case DW_TAG_base_type:
a02abb62 11891 case DW_TAG_subrange_type:
c906108c 11892 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11893 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11894 list_to_add = cu->list_in_scope;
c906108c
SS
11895 break;
11896 case DW_TAG_enumerator:
e142c38c 11897 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11898 if (attr)
11899 {
e7c27a73 11900 dwarf2_const_value (attr, sym, cu);
c906108c 11901 }
63d06c5c
DC
11902 {
11903 /* NOTE: carlton/2003-11-10: See comment above in the
11904 DW_TAG_class_type, etc. block. */
11905
e142c38c 11906 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11907 && (cu->language == language_cplus
11908 || cu->language == language_java)
e142c38c 11909 ? &global_symbols : cu->list_in_scope);
63d06c5c 11910 }
c906108c 11911 break;
5c4e30ca
DC
11912 case DW_TAG_namespace:
11913 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11914 list_to_add = &global_symbols;
5c4e30ca 11915 break;
c906108c
SS
11916 default:
11917 /* Not a tag we recognize. Hopefully we aren't processing
11918 trash data, but since we must specifically ignore things
11919 we don't recognize, there is nothing else we should do at
0963b4bd 11920 this point. */
e2e0b3e5 11921 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11922 dwarf_tag_name (die->tag));
c906108c
SS
11923 break;
11924 }
df8a16a1 11925
e37fd15a
SW
11926 if (suppress_add)
11927 {
11928 sym->hash_next = objfile->template_symbols;
11929 objfile->template_symbols = sym;
11930 list_to_add = NULL;
11931 }
11932
11933 if (list_to_add != NULL)
11934 add_symbol_to_list (sym, list_to_add);
11935
df8a16a1
DJ
11936 /* For the benefit of old versions of GCC, check for anonymous
11937 namespaces based on the demangled name. */
11938 if (!processing_has_namespace_info
94af9270 11939 && cu->language == language_cplus)
a10964d1 11940 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11941 }
11942 return (sym);
11943}
11944
34eaf542
TT
11945/* A wrapper for new_symbol_full that always allocates a new symbol. */
11946
11947static struct symbol *
11948new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11949{
11950 return new_symbol_full (die, type, cu, NULL);
11951}
11952
98bfdba5
PA
11953/* Given an attr with a DW_FORM_dataN value in host byte order,
11954 zero-extend it as appropriate for the symbol's type. The DWARF
11955 standard (v4) is not entirely clear about the meaning of using
11956 DW_FORM_dataN for a constant with a signed type, where the type is
11957 wider than the data. The conclusion of a discussion on the DWARF
11958 list was that this is unspecified. We choose to always zero-extend
11959 because that is the interpretation long in use by GCC. */
c906108c 11960
98bfdba5
PA
11961static gdb_byte *
11962dwarf2_const_value_data (struct attribute *attr, struct type *type,
11963 const char *name, struct obstack *obstack,
11964 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11965{
e7c27a73 11966 struct objfile *objfile = cu->objfile;
e17a4113
UW
11967 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11968 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11969 LONGEST l = DW_UNSND (attr);
11970
11971 if (bits < sizeof (*value) * 8)
11972 {
11973 l &= ((LONGEST) 1 << bits) - 1;
11974 *value = l;
11975 }
11976 else if (bits == sizeof (*value) * 8)
11977 *value = l;
11978 else
11979 {
11980 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11981 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11982 return bytes;
11983 }
11984
11985 return NULL;
11986}
11987
11988/* Read a constant value from an attribute. Either set *VALUE, or if
11989 the value does not fit in *VALUE, set *BYTES - either already
11990 allocated on the objfile obstack, or newly allocated on OBSTACK,
11991 or, set *BATON, if we translated the constant to a location
11992 expression. */
11993
11994static void
11995dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11996 const char *name, struct obstack *obstack,
11997 struct dwarf2_cu *cu,
11998 long *value, gdb_byte **bytes,
11999 struct dwarf2_locexpr_baton **baton)
12000{
12001 struct objfile *objfile = cu->objfile;
12002 struct comp_unit_head *cu_header = &cu->header;
c906108c 12003 struct dwarf_block *blk;
98bfdba5
PA
12004 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12005 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12006
12007 *value = 0;
12008 *bytes = NULL;
12009 *baton = NULL;
c906108c
SS
12010
12011 switch (attr->form)
12012 {
12013 case DW_FORM_addr:
ac56253d 12014 {
ac56253d
TT
12015 gdb_byte *data;
12016
98bfdba5
PA
12017 if (TYPE_LENGTH (type) != cu_header->addr_size)
12018 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12019 cu_header->addr_size,
98bfdba5 12020 TYPE_LENGTH (type));
ac56253d
TT
12021 /* Symbols of this form are reasonably rare, so we just
12022 piggyback on the existing location code rather than writing
12023 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12024 *baton = obstack_alloc (&objfile->objfile_obstack,
12025 sizeof (struct dwarf2_locexpr_baton));
12026 (*baton)->per_cu = cu->per_cu;
12027 gdb_assert ((*baton)->per_cu);
ac56253d 12028
98bfdba5
PA
12029 (*baton)->size = 2 + cu_header->addr_size;
12030 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12031 (*baton)->data = data;
ac56253d
TT
12032
12033 data[0] = DW_OP_addr;
12034 store_unsigned_integer (&data[1], cu_header->addr_size,
12035 byte_order, DW_ADDR (attr));
12036 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12037 }
c906108c 12038 break;
4ac36638 12039 case DW_FORM_string:
93b5768b 12040 case DW_FORM_strp:
98bfdba5
PA
12041 /* DW_STRING is already allocated on the objfile obstack, point
12042 directly to it. */
12043 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12044 break;
c906108c
SS
12045 case DW_FORM_block1:
12046 case DW_FORM_block2:
12047 case DW_FORM_block4:
12048 case DW_FORM_block:
2dc7f7b3 12049 case DW_FORM_exprloc:
c906108c 12050 blk = DW_BLOCK (attr);
98bfdba5
PA
12051 if (TYPE_LENGTH (type) != blk->size)
12052 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12053 TYPE_LENGTH (type));
12054 *bytes = blk->data;
c906108c 12055 break;
2df3850c
JM
12056
12057 /* The DW_AT_const_value attributes are supposed to carry the
12058 symbol's value "represented as it would be on the target
12059 architecture." By the time we get here, it's already been
12060 converted to host endianness, so we just need to sign- or
12061 zero-extend it as appropriate. */
12062 case DW_FORM_data1:
3e43a32a
MS
12063 *bytes = dwarf2_const_value_data (attr, type, name,
12064 obstack, cu, value, 8);
2df3850c 12065 break;
c906108c 12066 case DW_FORM_data2:
3e43a32a
MS
12067 *bytes = dwarf2_const_value_data (attr, type, name,
12068 obstack, cu, value, 16);
2df3850c 12069 break;
c906108c 12070 case DW_FORM_data4:
3e43a32a
MS
12071 *bytes = dwarf2_const_value_data (attr, type, name,
12072 obstack, cu, value, 32);
2df3850c 12073 break;
c906108c 12074 case DW_FORM_data8:
3e43a32a
MS
12075 *bytes = dwarf2_const_value_data (attr, type, name,
12076 obstack, cu, value, 64);
2df3850c
JM
12077 break;
12078
c906108c 12079 case DW_FORM_sdata:
98bfdba5 12080 *value = DW_SND (attr);
2df3850c
JM
12081 break;
12082
c906108c 12083 case DW_FORM_udata:
98bfdba5 12084 *value = DW_UNSND (attr);
c906108c 12085 break;
2df3850c 12086
c906108c 12087 default:
4d3c2250 12088 complaint (&symfile_complaints,
e2e0b3e5 12089 _("unsupported const value attribute form: '%s'"),
4d3c2250 12090 dwarf_form_name (attr->form));
98bfdba5 12091 *value = 0;
c906108c
SS
12092 break;
12093 }
12094}
12095
2df3850c 12096
98bfdba5
PA
12097/* Copy constant value from an attribute to a symbol. */
12098
2df3850c 12099static void
98bfdba5
PA
12100dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12101 struct dwarf2_cu *cu)
2df3850c 12102{
98bfdba5
PA
12103 struct objfile *objfile = cu->objfile;
12104 struct comp_unit_head *cu_header = &cu->header;
12105 long value;
12106 gdb_byte *bytes;
12107 struct dwarf2_locexpr_baton *baton;
2df3850c 12108
98bfdba5
PA
12109 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12110 SYMBOL_PRINT_NAME (sym),
12111 &objfile->objfile_obstack, cu,
12112 &value, &bytes, &baton);
2df3850c 12113
98bfdba5
PA
12114 if (baton != NULL)
12115 {
12116 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12117 SYMBOL_LOCATION_BATON (sym) = baton;
12118 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12119 }
12120 else if (bytes != NULL)
12121 {
12122 SYMBOL_VALUE_BYTES (sym) = bytes;
12123 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12124 }
12125 else
12126 {
12127 SYMBOL_VALUE (sym) = value;
12128 SYMBOL_CLASS (sym) = LOC_CONST;
12129 }
2df3850c
JM
12130}
12131
c906108c
SS
12132/* Return the type of the die in question using its DW_AT_type attribute. */
12133
12134static struct type *
e7c27a73 12135die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12136{
c906108c 12137 struct attribute *type_attr;
c906108c 12138
e142c38c 12139 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12140 if (!type_attr)
12141 {
12142 /* A missing DW_AT_type represents a void type. */
46bf5051 12143 return objfile_type (cu->objfile)->builtin_void;
c906108c 12144 }
348e048f 12145
673bfd45 12146 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12147}
12148
b4ba55a1
JB
12149/* True iff CU's producer generates GNAT Ada auxiliary information
12150 that allows to find parallel types through that information instead
12151 of having to do expensive parallel lookups by type name. */
12152
12153static int
12154need_gnat_info (struct dwarf2_cu *cu)
12155{
12156 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12157 of GNAT produces this auxiliary information, without any indication
12158 that it is produced. Part of enhancing the FSF version of GNAT
12159 to produce that information will be to put in place an indicator
12160 that we can use in order to determine whether the descriptive type
12161 info is available or not. One suggestion that has been made is
12162 to use a new attribute, attached to the CU die. For now, assume
12163 that the descriptive type info is not available. */
12164 return 0;
12165}
12166
b4ba55a1
JB
12167/* Return the auxiliary type of the die in question using its
12168 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12169 attribute is not present. */
12170
12171static struct type *
12172die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12173{
b4ba55a1 12174 struct attribute *type_attr;
b4ba55a1
JB
12175
12176 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12177 if (!type_attr)
12178 return NULL;
12179
673bfd45 12180 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12181}
12182
12183/* If DIE has a descriptive_type attribute, then set the TYPE's
12184 descriptive type accordingly. */
12185
12186static void
12187set_descriptive_type (struct type *type, struct die_info *die,
12188 struct dwarf2_cu *cu)
12189{
12190 struct type *descriptive_type = die_descriptive_type (die, cu);
12191
12192 if (descriptive_type)
12193 {
12194 ALLOCATE_GNAT_AUX_TYPE (type);
12195 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12196 }
12197}
12198
c906108c
SS
12199/* Return the containing type of the die in question using its
12200 DW_AT_containing_type attribute. */
12201
12202static struct type *
e7c27a73 12203die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12204{
c906108c 12205 struct attribute *type_attr;
c906108c 12206
e142c38c 12207 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12208 if (!type_attr)
12209 error (_("Dwarf Error: Problem turning containing type into gdb type "
12210 "[in module %s]"), cu->objfile->name);
12211
673bfd45 12212 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12213}
12214
673bfd45
DE
12215/* Look up the type of DIE in CU using its type attribute ATTR.
12216 If there is no type substitute an error marker. */
12217
c906108c 12218static struct type *
673bfd45
DE
12219lookup_die_type (struct die_info *die, struct attribute *attr,
12220 struct dwarf2_cu *cu)
c906108c 12221{
bb5ed363 12222 struct objfile *objfile = cu->objfile;
f792889a
DJ
12223 struct type *this_type;
12224
673bfd45
DE
12225 /* First see if we have it cached. */
12226
12227 if (is_ref_attr (attr))
12228 {
12229 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12230
12231 this_type = get_die_type_at_offset (offset, cu->per_cu);
12232 }
55f1336d 12233 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12234 {
12235 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12236 struct dwarf2_cu *sig_cu;
12237 unsigned int offset;
12238
12239 /* sig_type will be NULL if the signatured type is missing from
12240 the debug info. */
12241 if (sig_type == NULL)
12242 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12243 "at 0x%x [in module %s]"),
bb5ed363 12244 die->offset, objfile->name);
673bfd45 12245
b0df02fd 12246 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12247 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12248 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12249 }
12250 else
12251 {
12252 dump_die_for_error (die);
12253 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12254 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12255 }
12256
12257 /* If not cached we need to read it in. */
12258
12259 if (this_type == NULL)
12260 {
12261 struct die_info *type_die;
12262 struct dwarf2_cu *type_cu = cu;
12263
12264 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12265 /* If the type is cached, we should have found it above. */
12266 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12267 this_type = read_type_die_1 (type_die, type_cu);
12268 }
12269
12270 /* If we still don't have a type use an error marker. */
12271
12272 if (this_type == NULL)
c906108c 12273 {
b00fdb78
TT
12274 char *message, *saved;
12275
12276 /* read_type_die already issued a complaint. */
12277 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12278 objfile->name,
b00fdb78
TT
12279 cu->header.offset,
12280 die->offset);
bb5ed363 12281 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12282 message, strlen (message));
12283 xfree (message);
12284
bb5ed363 12285 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12286 }
673bfd45 12287
f792889a 12288 return this_type;
c906108c
SS
12289}
12290
673bfd45
DE
12291/* Return the type in DIE, CU.
12292 Returns NULL for invalid types.
12293
12294 This first does a lookup in the appropriate type_hash table,
12295 and only reads the die in if necessary.
12296
12297 NOTE: This can be called when reading in partial or full symbols. */
12298
f792889a 12299static struct type *
e7c27a73 12300read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12301{
f792889a
DJ
12302 struct type *this_type;
12303
12304 this_type = get_die_type (die, cu);
12305 if (this_type)
12306 return this_type;
12307
673bfd45
DE
12308 return read_type_die_1 (die, cu);
12309}
12310
12311/* Read the type in DIE, CU.
12312 Returns NULL for invalid types. */
12313
12314static struct type *
12315read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12316{
12317 struct type *this_type = NULL;
12318
c906108c
SS
12319 switch (die->tag)
12320 {
12321 case DW_TAG_class_type:
680b30c7 12322 case DW_TAG_interface_type:
c906108c
SS
12323 case DW_TAG_structure_type:
12324 case DW_TAG_union_type:
f792889a 12325 this_type = read_structure_type (die, cu);
c906108c
SS
12326 break;
12327 case DW_TAG_enumeration_type:
f792889a 12328 this_type = read_enumeration_type (die, cu);
c906108c
SS
12329 break;
12330 case DW_TAG_subprogram:
12331 case DW_TAG_subroutine_type:
edb3359d 12332 case DW_TAG_inlined_subroutine:
f792889a 12333 this_type = read_subroutine_type (die, cu);
c906108c
SS
12334 break;
12335 case DW_TAG_array_type:
f792889a 12336 this_type = read_array_type (die, cu);
c906108c 12337 break;
72019c9c 12338 case DW_TAG_set_type:
f792889a 12339 this_type = read_set_type (die, cu);
72019c9c 12340 break;
c906108c 12341 case DW_TAG_pointer_type:
f792889a 12342 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12343 break;
12344 case DW_TAG_ptr_to_member_type:
f792889a 12345 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12346 break;
12347 case DW_TAG_reference_type:
f792889a 12348 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12349 break;
12350 case DW_TAG_const_type:
f792889a 12351 this_type = read_tag_const_type (die, cu);
c906108c
SS
12352 break;
12353 case DW_TAG_volatile_type:
f792889a 12354 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12355 break;
12356 case DW_TAG_string_type:
f792889a 12357 this_type = read_tag_string_type (die, cu);
c906108c
SS
12358 break;
12359 case DW_TAG_typedef:
f792889a 12360 this_type = read_typedef (die, cu);
c906108c 12361 break;
a02abb62 12362 case DW_TAG_subrange_type:
f792889a 12363 this_type = read_subrange_type (die, cu);
a02abb62 12364 break;
c906108c 12365 case DW_TAG_base_type:
f792889a 12366 this_type = read_base_type (die, cu);
c906108c 12367 break;
81a17f79 12368 case DW_TAG_unspecified_type:
f792889a 12369 this_type = read_unspecified_type (die, cu);
81a17f79 12370 break;
0114d602
DJ
12371 case DW_TAG_namespace:
12372 this_type = read_namespace_type (die, cu);
12373 break;
f55ee35c
JK
12374 case DW_TAG_module:
12375 this_type = read_module_type (die, cu);
12376 break;
c906108c 12377 default:
3e43a32a
MS
12378 complaint (&symfile_complaints,
12379 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12380 dwarf_tag_name (die->tag));
c906108c
SS
12381 break;
12382 }
63d06c5c 12383
f792889a 12384 return this_type;
63d06c5c
DC
12385}
12386
abc72ce4
DE
12387/* See if we can figure out if the class lives in a namespace. We do
12388 this by looking for a member function; its demangled name will
12389 contain namespace info, if there is any.
12390 Return the computed name or NULL.
12391 Space for the result is allocated on the objfile's obstack.
12392 This is the full-die version of guess_partial_die_structure_name.
12393 In this case we know DIE has no useful parent. */
12394
12395static char *
12396guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12397{
12398 struct die_info *spec_die;
12399 struct dwarf2_cu *spec_cu;
12400 struct die_info *child;
12401
12402 spec_cu = cu;
12403 spec_die = die_specification (die, &spec_cu);
12404 if (spec_die != NULL)
12405 {
12406 die = spec_die;
12407 cu = spec_cu;
12408 }
12409
12410 for (child = die->child;
12411 child != NULL;
12412 child = child->sibling)
12413 {
12414 if (child->tag == DW_TAG_subprogram)
12415 {
12416 struct attribute *attr;
12417
12418 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12419 if (attr == NULL)
12420 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12421 if (attr != NULL)
12422 {
12423 char *actual_name
12424 = language_class_name_from_physname (cu->language_defn,
12425 DW_STRING (attr));
12426 char *name = NULL;
12427
12428 if (actual_name != NULL)
12429 {
12430 char *die_name = dwarf2_name (die, cu);
12431
12432 if (die_name != NULL
12433 && strcmp (die_name, actual_name) != 0)
12434 {
12435 /* Strip off the class name from the full name.
12436 We want the prefix. */
12437 int die_name_len = strlen (die_name);
12438 int actual_name_len = strlen (actual_name);
12439
12440 /* Test for '::' as a sanity check. */
12441 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12442 && actual_name[actual_name_len
12443 - die_name_len - 1] == ':')
abc72ce4
DE
12444 name =
12445 obsavestring (actual_name,
12446 actual_name_len - die_name_len - 2,
12447 &cu->objfile->objfile_obstack);
12448 }
12449 }
12450 xfree (actual_name);
12451 return name;
12452 }
12453 }
12454 }
12455
12456 return NULL;
12457}
12458
96408a79
SA
12459/* GCC might emit a nameless typedef that has a linkage name. Determine the
12460 prefix part in such case. See
12461 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12462
12463static char *
12464anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12465{
12466 struct attribute *attr;
12467 char *base;
12468
12469 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12470 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12471 return NULL;
12472
12473 attr = dwarf2_attr (die, DW_AT_name, cu);
12474 if (attr != NULL && DW_STRING (attr) != NULL)
12475 return NULL;
12476
12477 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12478 if (attr == NULL)
12479 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12480 if (attr == NULL || DW_STRING (attr) == NULL)
12481 return NULL;
12482
12483 /* dwarf2_name had to be already called. */
12484 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12485
12486 /* Strip the base name, keep any leading namespaces/classes. */
12487 base = strrchr (DW_STRING (attr), ':');
12488 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12489 return "";
12490
12491 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12492 &cu->objfile->objfile_obstack);
12493}
12494
fdde2d81 12495/* Return the name of the namespace/class that DIE is defined within,
0114d602 12496 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12497
0114d602
DJ
12498 For example, if we're within the method foo() in the following
12499 code:
12500
12501 namespace N {
12502 class C {
12503 void foo () {
12504 }
12505 };
12506 }
12507
12508 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12509
12510static char *
e142c38c 12511determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12512{
0114d602
DJ
12513 struct die_info *parent, *spec_die;
12514 struct dwarf2_cu *spec_cu;
12515 struct type *parent_type;
96408a79 12516 char *retval;
63d06c5c 12517
f55ee35c
JK
12518 if (cu->language != language_cplus && cu->language != language_java
12519 && cu->language != language_fortran)
0114d602
DJ
12520 return "";
12521
96408a79
SA
12522 retval = anonymous_struct_prefix (die, cu);
12523 if (retval)
12524 return retval;
12525
0114d602
DJ
12526 /* We have to be careful in the presence of DW_AT_specification.
12527 For example, with GCC 3.4, given the code
12528
12529 namespace N {
12530 void foo() {
12531 // Definition of N::foo.
12532 }
12533 }
12534
12535 then we'll have a tree of DIEs like this:
12536
12537 1: DW_TAG_compile_unit
12538 2: DW_TAG_namespace // N
12539 3: DW_TAG_subprogram // declaration of N::foo
12540 4: DW_TAG_subprogram // definition of N::foo
12541 DW_AT_specification // refers to die #3
12542
12543 Thus, when processing die #4, we have to pretend that we're in
12544 the context of its DW_AT_specification, namely the contex of die
12545 #3. */
12546 spec_cu = cu;
12547 spec_die = die_specification (die, &spec_cu);
12548 if (spec_die == NULL)
12549 parent = die->parent;
12550 else
63d06c5c 12551 {
0114d602
DJ
12552 parent = spec_die->parent;
12553 cu = spec_cu;
63d06c5c 12554 }
0114d602
DJ
12555
12556 if (parent == NULL)
12557 return "";
98bfdba5
PA
12558 else if (parent->building_fullname)
12559 {
12560 const char *name;
12561 const char *parent_name;
12562
12563 /* It has been seen on RealView 2.2 built binaries,
12564 DW_TAG_template_type_param types actually _defined_ as
12565 children of the parent class:
12566
12567 enum E {};
12568 template class <class Enum> Class{};
12569 Class<enum E> class_e;
12570
12571 1: DW_TAG_class_type (Class)
12572 2: DW_TAG_enumeration_type (E)
12573 3: DW_TAG_enumerator (enum1:0)
12574 3: DW_TAG_enumerator (enum2:1)
12575 ...
12576 2: DW_TAG_template_type_param
12577 DW_AT_type DW_FORM_ref_udata (E)
12578
12579 Besides being broken debug info, it can put GDB into an
12580 infinite loop. Consider:
12581
12582 When we're building the full name for Class<E>, we'll start
12583 at Class, and go look over its template type parameters,
12584 finding E. We'll then try to build the full name of E, and
12585 reach here. We're now trying to build the full name of E,
12586 and look over the parent DIE for containing scope. In the
12587 broken case, if we followed the parent DIE of E, we'd again
12588 find Class, and once again go look at its template type
12589 arguments, etc., etc. Simply don't consider such parent die
12590 as source-level parent of this die (it can't be, the language
12591 doesn't allow it), and break the loop here. */
12592 name = dwarf2_name (die, cu);
12593 parent_name = dwarf2_name (parent, cu);
12594 complaint (&symfile_complaints,
12595 _("template param type '%s' defined within parent '%s'"),
12596 name ? name : "<unknown>",
12597 parent_name ? parent_name : "<unknown>");
12598 return "";
12599 }
63d06c5c 12600 else
0114d602
DJ
12601 switch (parent->tag)
12602 {
63d06c5c 12603 case DW_TAG_namespace:
0114d602 12604 parent_type = read_type_die (parent, cu);
acebe513
UW
12605 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12606 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12607 Work around this problem here. */
12608 if (cu->language == language_cplus
12609 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12610 return "";
0114d602
DJ
12611 /* We give a name to even anonymous namespaces. */
12612 return TYPE_TAG_NAME (parent_type);
63d06c5c 12613 case DW_TAG_class_type:
680b30c7 12614 case DW_TAG_interface_type:
63d06c5c 12615 case DW_TAG_structure_type:
0114d602 12616 case DW_TAG_union_type:
f55ee35c 12617 case DW_TAG_module:
0114d602
DJ
12618 parent_type = read_type_die (parent, cu);
12619 if (TYPE_TAG_NAME (parent_type) != NULL)
12620 return TYPE_TAG_NAME (parent_type);
12621 else
12622 /* An anonymous structure is only allowed non-static data
12623 members; no typedefs, no member functions, et cetera.
12624 So it does not need a prefix. */
12625 return "";
abc72ce4
DE
12626 case DW_TAG_compile_unit:
12627 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12628 if (cu->language == language_cplus
8b70b953 12629 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12630 && die->child != NULL
12631 && (die->tag == DW_TAG_class_type
12632 || die->tag == DW_TAG_structure_type
12633 || die->tag == DW_TAG_union_type))
12634 {
12635 char *name = guess_full_die_structure_name (die, cu);
12636 if (name != NULL)
12637 return name;
12638 }
12639 return "";
63d06c5c 12640 default:
8176b9b8 12641 return determine_prefix (parent, cu);
63d06c5c 12642 }
63d06c5c
DC
12643}
12644
3e43a32a
MS
12645/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12646 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12647 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12648 an obconcat, otherwise allocate storage for the result. The CU argument is
12649 used to determine the language and hence, the appropriate separator. */
987504bb 12650
f55ee35c 12651#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12652
12653static char *
f55ee35c
JK
12654typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12655 int physname, struct dwarf2_cu *cu)
63d06c5c 12656{
f55ee35c 12657 const char *lead = "";
5c315b68 12658 const char *sep;
63d06c5c 12659
3e43a32a
MS
12660 if (suffix == NULL || suffix[0] == '\0'
12661 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12662 sep = "";
12663 else if (cu->language == language_java)
12664 sep = ".";
f55ee35c
JK
12665 else if (cu->language == language_fortran && physname)
12666 {
12667 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12668 DW_AT_MIPS_linkage_name is preferred and used instead. */
12669
12670 lead = "__";
12671 sep = "_MOD_";
12672 }
987504bb
JJ
12673 else
12674 sep = "::";
63d06c5c 12675
6dd47d34
DE
12676 if (prefix == NULL)
12677 prefix = "";
12678 if (suffix == NULL)
12679 suffix = "";
12680
987504bb
JJ
12681 if (obs == NULL)
12682 {
3e43a32a
MS
12683 char *retval
12684 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12685
f55ee35c
JK
12686 strcpy (retval, lead);
12687 strcat (retval, prefix);
6dd47d34
DE
12688 strcat (retval, sep);
12689 strcat (retval, suffix);
63d06c5c
DC
12690 return retval;
12691 }
987504bb
JJ
12692 else
12693 {
12694 /* We have an obstack. */
f55ee35c 12695 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12696 }
63d06c5c
DC
12697}
12698
c906108c
SS
12699/* Return sibling of die, NULL if no sibling. */
12700
f9aca02d 12701static struct die_info *
fba45db2 12702sibling_die (struct die_info *die)
c906108c 12703{
639d11d3 12704 return die->sibling;
c906108c
SS
12705}
12706
71c25dea
TT
12707/* Get name of a die, return NULL if not found. */
12708
12709static char *
12710dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12711 struct obstack *obstack)
12712{
12713 if (name && cu->language == language_cplus)
12714 {
12715 char *canon_name = cp_canonicalize_string (name);
12716
12717 if (canon_name != NULL)
12718 {
12719 if (strcmp (canon_name, name) != 0)
12720 name = obsavestring (canon_name, strlen (canon_name),
12721 obstack);
12722 xfree (canon_name);
12723 }
12724 }
12725
12726 return name;
c906108c
SS
12727}
12728
9219021c
DC
12729/* Get name of a die, return NULL if not found. */
12730
12731static char *
e142c38c 12732dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12733{
12734 struct attribute *attr;
12735
e142c38c 12736 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12737 if ((!attr || !DW_STRING (attr))
12738 && die->tag != DW_TAG_class_type
12739 && die->tag != DW_TAG_interface_type
12740 && die->tag != DW_TAG_structure_type
12741 && die->tag != DW_TAG_union_type)
71c25dea
TT
12742 return NULL;
12743
12744 switch (die->tag)
12745 {
12746 case DW_TAG_compile_unit:
12747 /* Compilation units have a DW_AT_name that is a filename, not
12748 a source language identifier. */
12749 case DW_TAG_enumeration_type:
12750 case DW_TAG_enumerator:
12751 /* These tags always have simple identifiers already; no need
12752 to canonicalize them. */
12753 return DW_STRING (attr);
907af001 12754
418835cc
KS
12755 case DW_TAG_subprogram:
12756 /* Java constructors will all be named "<init>", so return
12757 the class name when we see this special case. */
12758 if (cu->language == language_java
12759 && DW_STRING (attr) != NULL
12760 && strcmp (DW_STRING (attr), "<init>") == 0)
12761 {
12762 struct dwarf2_cu *spec_cu = cu;
12763 struct die_info *spec_die;
12764
12765 /* GCJ will output '<init>' for Java constructor names.
12766 For this special case, return the name of the parent class. */
12767
12768 /* GCJ may output suprogram DIEs with AT_specification set.
12769 If so, use the name of the specified DIE. */
12770 spec_die = die_specification (die, &spec_cu);
12771 if (spec_die != NULL)
12772 return dwarf2_name (spec_die, spec_cu);
12773
12774 do
12775 {
12776 die = die->parent;
12777 if (die->tag == DW_TAG_class_type)
12778 return dwarf2_name (die, cu);
12779 }
12780 while (die->tag != DW_TAG_compile_unit);
12781 }
907af001
UW
12782 break;
12783
12784 case DW_TAG_class_type:
12785 case DW_TAG_interface_type:
12786 case DW_TAG_structure_type:
12787 case DW_TAG_union_type:
12788 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12789 structures or unions. These were of the form "._%d" in GCC 4.1,
12790 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12791 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12792 if (attr && DW_STRING (attr)
12793 && (strncmp (DW_STRING (attr), "._", 2) == 0
12794 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12795 return NULL;
53832f31
TT
12796
12797 /* GCC might emit a nameless typedef that has a linkage name. See
12798 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12799 if (!attr || DW_STRING (attr) == NULL)
12800 {
df5c6c50 12801 char *demangled = NULL;
53832f31
TT
12802
12803 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12804 if (attr == NULL)
12805 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12806
12807 if (attr == NULL || DW_STRING (attr) == NULL)
12808 return NULL;
12809
df5c6c50
JK
12810 /* Avoid demangling DW_STRING (attr) the second time on a second
12811 call for the same DIE. */
12812 if (!DW_STRING_IS_CANONICAL (attr))
12813 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12814
12815 if (demangled)
12816 {
96408a79
SA
12817 char *base;
12818
53832f31 12819 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12820 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12821 &cu->objfile->objfile_obstack);
53832f31
TT
12822 DW_STRING_IS_CANONICAL (attr) = 1;
12823 xfree (demangled);
96408a79
SA
12824
12825 /* Strip any leading namespaces/classes, keep only the base name.
12826 DW_AT_name for named DIEs does not contain the prefixes. */
12827 base = strrchr (DW_STRING (attr), ':');
12828 if (base && base > DW_STRING (attr) && base[-1] == ':')
12829 return &base[1];
12830 else
12831 return DW_STRING (attr);
53832f31
TT
12832 }
12833 }
907af001
UW
12834 break;
12835
71c25dea 12836 default:
907af001
UW
12837 break;
12838 }
12839
12840 if (!DW_STRING_IS_CANONICAL (attr))
12841 {
12842 DW_STRING (attr)
12843 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12844 &cu->objfile->objfile_obstack);
12845 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12846 }
907af001 12847 return DW_STRING (attr);
9219021c
DC
12848}
12849
12850/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12851 is none. *EXT_CU is the CU containing DIE on input, and the CU
12852 containing the return value on output. */
9219021c
DC
12853
12854static struct die_info *
f2f0e013 12855dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12856{
12857 struct attribute *attr;
9219021c 12858
f2f0e013 12859 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12860 if (attr == NULL)
12861 return NULL;
12862
f2f0e013 12863 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12864}
12865
c906108c
SS
12866/* Convert a DIE tag into its string name. */
12867
12868static char *
aa1ee363 12869dwarf_tag_name (unsigned tag)
c906108c
SS
12870{
12871 switch (tag)
12872 {
12873 case DW_TAG_padding:
12874 return "DW_TAG_padding";
12875 case DW_TAG_array_type:
12876 return "DW_TAG_array_type";
12877 case DW_TAG_class_type:
12878 return "DW_TAG_class_type";
12879 case DW_TAG_entry_point:
12880 return "DW_TAG_entry_point";
12881 case DW_TAG_enumeration_type:
12882 return "DW_TAG_enumeration_type";
12883 case DW_TAG_formal_parameter:
12884 return "DW_TAG_formal_parameter";
12885 case DW_TAG_imported_declaration:
12886 return "DW_TAG_imported_declaration";
12887 case DW_TAG_label:
12888 return "DW_TAG_label";
12889 case DW_TAG_lexical_block:
12890 return "DW_TAG_lexical_block";
12891 case DW_TAG_member:
12892 return "DW_TAG_member";
12893 case DW_TAG_pointer_type:
12894 return "DW_TAG_pointer_type";
12895 case DW_TAG_reference_type:
12896 return "DW_TAG_reference_type";
12897 case DW_TAG_compile_unit:
12898 return "DW_TAG_compile_unit";
12899 case DW_TAG_string_type:
12900 return "DW_TAG_string_type";
12901 case DW_TAG_structure_type:
12902 return "DW_TAG_structure_type";
12903 case DW_TAG_subroutine_type:
12904 return "DW_TAG_subroutine_type";
12905 case DW_TAG_typedef:
12906 return "DW_TAG_typedef";
12907 case DW_TAG_union_type:
12908 return "DW_TAG_union_type";
12909 case DW_TAG_unspecified_parameters:
12910 return "DW_TAG_unspecified_parameters";
12911 case DW_TAG_variant:
12912 return "DW_TAG_variant";
12913 case DW_TAG_common_block:
12914 return "DW_TAG_common_block";
12915 case DW_TAG_common_inclusion:
12916 return "DW_TAG_common_inclusion";
12917 case DW_TAG_inheritance:
12918 return "DW_TAG_inheritance";
12919 case DW_TAG_inlined_subroutine:
12920 return "DW_TAG_inlined_subroutine";
12921 case DW_TAG_module:
12922 return "DW_TAG_module";
12923 case DW_TAG_ptr_to_member_type:
12924 return "DW_TAG_ptr_to_member_type";
12925 case DW_TAG_set_type:
12926 return "DW_TAG_set_type";
12927 case DW_TAG_subrange_type:
12928 return "DW_TAG_subrange_type";
12929 case DW_TAG_with_stmt:
12930 return "DW_TAG_with_stmt";
12931 case DW_TAG_access_declaration:
12932 return "DW_TAG_access_declaration";
12933 case DW_TAG_base_type:
12934 return "DW_TAG_base_type";
12935 case DW_TAG_catch_block:
12936 return "DW_TAG_catch_block";
12937 case DW_TAG_const_type:
12938 return "DW_TAG_const_type";
12939 case DW_TAG_constant:
12940 return "DW_TAG_constant";
12941 case DW_TAG_enumerator:
12942 return "DW_TAG_enumerator";
12943 case DW_TAG_file_type:
12944 return "DW_TAG_file_type";
12945 case DW_TAG_friend:
12946 return "DW_TAG_friend";
12947 case DW_TAG_namelist:
12948 return "DW_TAG_namelist";
12949 case DW_TAG_namelist_item:
12950 return "DW_TAG_namelist_item";
12951 case DW_TAG_packed_type:
12952 return "DW_TAG_packed_type";
12953 case DW_TAG_subprogram:
12954 return "DW_TAG_subprogram";
12955 case DW_TAG_template_type_param:
12956 return "DW_TAG_template_type_param";
12957 case DW_TAG_template_value_param:
12958 return "DW_TAG_template_value_param";
12959 case DW_TAG_thrown_type:
12960 return "DW_TAG_thrown_type";
12961 case DW_TAG_try_block:
12962 return "DW_TAG_try_block";
12963 case DW_TAG_variant_part:
12964 return "DW_TAG_variant_part";
12965 case DW_TAG_variable:
12966 return "DW_TAG_variable";
12967 case DW_TAG_volatile_type:
12968 return "DW_TAG_volatile_type";
d9fa45fe
DC
12969 case DW_TAG_dwarf_procedure:
12970 return "DW_TAG_dwarf_procedure";
12971 case DW_TAG_restrict_type:
12972 return "DW_TAG_restrict_type";
12973 case DW_TAG_interface_type:
12974 return "DW_TAG_interface_type";
12975 case DW_TAG_namespace:
12976 return "DW_TAG_namespace";
12977 case DW_TAG_imported_module:
12978 return "DW_TAG_imported_module";
12979 case DW_TAG_unspecified_type:
12980 return "DW_TAG_unspecified_type";
12981 case DW_TAG_partial_unit:
12982 return "DW_TAG_partial_unit";
12983 case DW_TAG_imported_unit:
12984 return "DW_TAG_imported_unit";
b7619582
GF
12985 case DW_TAG_condition:
12986 return "DW_TAG_condition";
12987 case DW_TAG_shared_type:
12988 return "DW_TAG_shared_type";
348e048f
DE
12989 case DW_TAG_type_unit:
12990 return "DW_TAG_type_unit";
c906108c
SS
12991 case DW_TAG_MIPS_loop:
12992 return "DW_TAG_MIPS_loop";
b7619582
GF
12993 case DW_TAG_HP_array_descriptor:
12994 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12995 case DW_TAG_format_label:
12996 return "DW_TAG_format_label";
12997 case DW_TAG_function_template:
12998 return "DW_TAG_function_template";
12999 case DW_TAG_class_template:
13000 return "DW_TAG_class_template";
b7619582
GF
13001 case DW_TAG_GNU_BINCL:
13002 return "DW_TAG_GNU_BINCL";
13003 case DW_TAG_GNU_EINCL:
13004 return "DW_TAG_GNU_EINCL";
13005 case DW_TAG_upc_shared_type:
13006 return "DW_TAG_upc_shared_type";
13007 case DW_TAG_upc_strict_type:
13008 return "DW_TAG_upc_strict_type";
13009 case DW_TAG_upc_relaxed_type:
13010 return "DW_TAG_upc_relaxed_type";
13011 case DW_TAG_PGI_kanji_type:
13012 return "DW_TAG_PGI_kanji_type";
13013 case DW_TAG_PGI_interface_block:
13014 return "DW_TAG_PGI_interface_block";
96408a79
SA
13015 case DW_TAG_GNU_call_site:
13016 return "DW_TAG_GNU_call_site";
c906108c
SS
13017 default:
13018 return "DW_TAG_<unknown>";
13019 }
13020}
13021
13022/* Convert a DWARF attribute code into its string name. */
13023
13024static char *
aa1ee363 13025dwarf_attr_name (unsigned attr)
c906108c
SS
13026{
13027 switch (attr)
13028 {
13029 case DW_AT_sibling:
13030 return "DW_AT_sibling";
13031 case DW_AT_location:
13032 return "DW_AT_location";
13033 case DW_AT_name:
13034 return "DW_AT_name";
13035 case DW_AT_ordering:
13036 return "DW_AT_ordering";
13037 case DW_AT_subscr_data:
13038 return "DW_AT_subscr_data";
13039 case DW_AT_byte_size:
13040 return "DW_AT_byte_size";
13041 case DW_AT_bit_offset:
13042 return "DW_AT_bit_offset";
13043 case DW_AT_bit_size:
13044 return "DW_AT_bit_size";
13045 case DW_AT_element_list:
13046 return "DW_AT_element_list";
13047 case DW_AT_stmt_list:
13048 return "DW_AT_stmt_list";
13049 case DW_AT_low_pc:
13050 return "DW_AT_low_pc";
13051 case DW_AT_high_pc:
13052 return "DW_AT_high_pc";
13053 case DW_AT_language:
13054 return "DW_AT_language";
13055 case DW_AT_member:
13056 return "DW_AT_member";
13057 case DW_AT_discr:
13058 return "DW_AT_discr";
13059 case DW_AT_discr_value:
13060 return "DW_AT_discr_value";
13061 case DW_AT_visibility:
13062 return "DW_AT_visibility";
13063 case DW_AT_import:
13064 return "DW_AT_import";
13065 case DW_AT_string_length:
13066 return "DW_AT_string_length";
13067 case DW_AT_common_reference:
13068 return "DW_AT_common_reference";
13069 case DW_AT_comp_dir:
13070 return "DW_AT_comp_dir";
13071 case DW_AT_const_value:
13072 return "DW_AT_const_value";
13073 case DW_AT_containing_type:
13074 return "DW_AT_containing_type";
13075 case DW_AT_default_value:
13076 return "DW_AT_default_value";
13077 case DW_AT_inline:
13078 return "DW_AT_inline";
13079 case DW_AT_is_optional:
13080 return "DW_AT_is_optional";
13081 case DW_AT_lower_bound:
13082 return "DW_AT_lower_bound";
13083 case DW_AT_producer:
13084 return "DW_AT_producer";
13085 case DW_AT_prototyped:
13086 return "DW_AT_prototyped";
13087 case DW_AT_return_addr:
13088 return "DW_AT_return_addr";
13089 case DW_AT_start_scope:
13090 return "DW_AT_start_scope";
09fa0d7c
JK
13091 case DW_AT_bit_stride:
13092 return "DW_AT_bit_stride";
c906108c
SS
13093 case DW_AT_upper_bound:
13094 return "DW_AT_upper_bound";
13095 case DW_AT_abstract_origin:
13096 return "DW_AT_abstract_origin";
13097 case DW_AT_accessibility:
13098 return "DW_AT_accessibility";
13099 case DW_AT_address_class:
13100 return "DW_AT_address_class";
13101 case DW_AT_artificial:
13102 return "DW_AT_artificial";
13103 case DW_AT_base_types:
13104 return "DW_AT_base_types";
13105 case DW_AT_calling_convention:
13106 return "DW_AT_calling_convention";
13107 case DW_AT_count:
13108 return "DW_AT_count";
13109 case DW_AT_data_member_location:
13110 return "DW_AT_data_member_location";
13111 case DW_AT_decl_column:
13112 return "DW_AT_decl_column";
13113 case DW_AT_decl_file:
13114 return "DW_AT_decl_file";
13115 case DW_AT_decl_line:
13116 return "DW_AT_decl_line";
13117 case DW_AT_declaration:
13118 return "DW_AT_declaration";
13119 case DW_AT_discr_list:
13120 return "DW_AT_discr_list";
13121 case DW_AT_encoding:
13122 return "DW_AT_encoding";
13123 case DW_AT_external:
13124 return "DW_AT_external";
13125 case DW_AT_frame_base:
13126 return "DW_AT_frame_base";
13127 case DW_AT_friend:
13128 return "DW_AT_friend";
13129 case DW_AT_identifier_case:
13130 return "DW_AT_identifier_case";
13131 case DW_AT_macro_info:
13132 return "DW_AT_macro_info";
13133 case DW_AT_namelist_items:
13134 return "DW_AT_namelist_items";
13135 case DW_AT_priority:
13136 return "DW_AT_priority";
13137 case DW_AT_segment:
13138 return "DW_AT_segment";
13139 case DW_AT_specification:
13140 return "DW_AT_specification";
13141 case DW_AT_static_link:
13142 return "DW_AT_static_link";
13143 case DW_AT_type:
13144 return "DW_AT_type";
13145 case DW_AT_use_location:
13146 return "DW_AT_use_location";
13147 case DW_AT_variable_parameter:
13148 return "DW_AT_variable_parameter";
13149 case DW_AT_virtuality:
13150 return "DW_AT_virtuality";
13151 case DW_AT_vtable_elem_location:
13152 return "DW_AT_vtable_elem_location";
b7619582 13153 /* DWARF 3 values. */
d9fa45fe
DC
13154 case DW_AT_allocated:
13155 return "DW_AT_allocated";
13156 case DW_AT_associated:
13157 return "DW_AT_associated";
13158 case DW_AT_data_location:
13159 return "DW_AT_data_location";
09fa0d7c
JK
13160 case DW_AT_byte_stride:
13161 return "DW_AT_byte_stride";
d9fa45fe
DC
13162 case DW_AT_entry_pc:
13163 return "DW_AT_entry_pc";
13164 case DW_AT_use_UTF8:
13165 return "DW_AT_use_UTF8";
13166 case DW_AT_extension:
13167 return "DW_AT_extension";
13168 case DW_AT_ranges:
13169 return "DW_AT_ranges";
13170 case DW_AT_trampoline:
13171 return "DW_AT_trampoline";
13172 case DW_AT_call_column:
13173 return "DW_AT_call_column";
13174 case DW_AT_call_file:
13175 return "DW_AT_call_file";
13176 case DW_AT_call_line:
13177 return "DW_AT_call_line";
b7619582
GF
13178 case DW_AT_description:
13179 return "DW_AT_description";
13180 case DW_AT_binary_scale:
13181 return "DW_AT_binary_scale";
13182 case DW_AT_decimal_scale:
13183 return "DW_AT_decimal_scale";
13184 case DW_AT_small:
13185 return "DW_AT_small";
13186 case DW_AT_decimal_sign:
13187 return "DW_AT_decimal_sign";
13188 case DW_AT_digit_count:
13189 return "DW_AT_digit_count";
13190 case DW_AT_picture_string:
13191 return "DW_AT_picture_string";
13192 case DW_AT_mutable:
13193 return "DW_AT_mutable";
13194 case DW_AT_threads_scaled:
13195 return "DW_AT_threads_scaled";
13196 case DW_AT_explicit:
13197 return "DW_AT_explicit";
13198 case DW_AT_object_pointer:
13199 return "DW_AT_object_pointer";
13200 case DW_AT_endianity:
13201 return "DW_AT_endianity";
13202 case DW_AT_elemental:
13203 return "DW_AT_elemental";
13204 case DW_AT_pure:
13205 return "DW_AT_pure";
13206 case DW_AT_recursive:
13207 return "DW_AT_recursive";
348e048f
DE
13208 /* DWARF 4 values. */
13209 case DW_AT_signature:
13210 return "DW_AT_signature";
31ef98ae
TT
13211 case DW_AT_linkage_name:
13212 return "DW_AT_linkage_name";
b7619582 13213 /* SGI/MIPS extensions. */
c764a876 13214#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13215 case DW_AT_MIPS_fde:
13216 return "DW_AT_MIPS_fde";
c764a876 13217#endif
c906108c
SS
13218 case DW_AT_MIPS_loop_begin:
13219 return "DW_AT_MIPS_loop_begin";
13220 case DW_AT_MIPS_tail_loop_begin:
13221 return "DW_AT_MIPS_tail_loop_begin";
13222 case DW_AT_MIPS_epilog_begin:
13223 return "DW_AT_MIPS_epilog_begin";
13224 case DW_AT_MIPS_loop_unroll_factor:
13225 return "DW_AT_MIPS_loop_unroll_factor";
13226 case DW_AT_MIPS_software_pipeline_depth:
13227 return "DW_AT_MIPS_software_pipeline_depth";
13228 case DW_AT_MIPS_linkage_name:
13229 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13230 case DW_AT_MIPS_stride:
13231 return "DW_AT_MIPS_stride";
13232 case DW_AT_MIPS_abstract_name:
13233 return "DW_AT_MIPS_abstract_name";
13234 case DW_AT_MIPS_clone_origin:
13235 return "DW_AT_MIPS_clone_origin";
13236 case DW_AT_MIPS_has_inlines:
13237 return "DW_AT_MIPS_has_inlines";
b7619582 13238 /* HP extensions. */
c764a876 13239#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13240 case DW_AT_HP_block_index:
13241 return "DW_AT_HP_block_index";
c764a876 13242#endif
b7619582
GF
13243 case DW_AT_HP_unmodifiable:
13244 return "DW_AT_HP_unmodifiable";
13245 case DW_AT_HP_actuals_stmt_list:
13246 return "DW_AT_HP_actuals_stmt_list";
13247 case DW_AT_HP_proc_per_section:
13248 return "DW_AT_HP_proc_per_section";
13249 case DW_AT_HP_raw_data_ptr:
13250 return "DW_AT_HP_raw_data_ptr";
13251 case DW_AT_HP_pass_by_reference:
13252 return "DW_AT_HP_pass_by_reference";
13253 case DW_AT_HP_opt_level:
13254 return "DW_AT_HP_opt_level";
13255 case DW_AT_HP_prof_version_id:
13256 return "DW_AT_HP_prof_version_id";
13257 case DW_AT_HP_opt_flags:
13258 return "DW_AT_HP_opt_flags";
13259 case DW_AT_HP_cold_region_low_pc:
13260 return "DW_AT_HP_cold_region_low_pc";
13261 case DW_AT_HP_cold_region_high_pc:
13262 return "DW_AT_HP_cold_region_high_pc";
13263 case DW_AT_HP_all_variables_modifiable:
13264 return "DW_AT_HP_all_variables_modifiable";
13265 case DW_AT_HP_linkage_name:
13266 return "DW_AT_HP_linkage_name";
13267 case DW_AT_HP_prof_flags:
13268 return "DW_AT_HP_prof_flags";
13269 /* GNU extensions. */
c906108c
SS
13270 case DW_AT_sf_names:
13271 return "DW_AT_sf_names";
13272 case DW_AT_src_info:
13273 return "DW_AT_src_info";
13274 case DW_AT_mac_info:
13275 return "DW_AT_mac_info";
13276 case DW_AT_src_coords:
13277 return "DW_AT_src_coords";
13278 case DW_AT_body_begin:
13279 return "DW_AT_body_begin";
13280 case DW_AT_body_end:
13281 return "DW_AT_body_end";
f5f8a009
EZ
13282 case DW_AT_GNU_vector:
13283 return "DW_AT_GNU_vector";
2de00c64
DE
13284 case DW_AT_GNU_odr_signature:
13285 return "DW_AT_GNU_odr_signature";
b7619582
GF
13286 /* VMS extensions. */
13287 case DW_AT_VMS_rtnbeg_pd_address:
13288 return "DW_AT_VMS_rtnbeg_pd_address";
13289 /* UPC extension. */
13290 case DW_AT_upc_threads_scaled:
13291 return "DW_AT_upc_threads_scaled";
13292 /* PGI (STMicroelectronics) extensions. */
13293 case DW_AT_PGI_lbase:
13294 return "DW_AT_PGI_lbase";
13295 case DW_AT_PGI_soffset:
13296 return "DW_AT_PGI_soffset";
13297 case DW_AT_PGI_lstride:
13298 return "DW_AT_PGI_lstride";
c906108c
SS
13299 default:
13300 return "DW_AT_<unknown>";
13301 }
13302}
13303
13304/* Convert a DWARF value form code into its string name. */
13305
13306static char *
aa1ee363 13307dwarf_form_name (unsigned form)
c906108c
SS
13308{
13309 switch (form)
13310 {
13311 case DW_FORM_addr:
13312 return "DW_FORM_addr";
13313 case DW_FORM_block2:
13314 return "DW_FORM_block2";
13315 case DW_FORM_block4:
13316 return "DW_FORM_block4";
13317 case DW_FORM_data2:
13318 return "DW_FORM_data2";
13319 case DW_FORM_data4:
13320 return "DW_FORM_data4";
13321 case DW_FORM_data8:
13322 return "DW_FORM_data8";
13323 case DW_FORM_string:
13324 return "DW_FORM_string";
13325 case DW_FORM_block:
13326 return "DW_FORM_block";
13327 case DW_FORM_block1:
13328 return "DW_FORM_block1";
13329 case DW_FORM_data1:
13330 return "DW_FORM_data1";
13331 case DW_FORM_flag:
13332 return "DW_FORM_flag";
13333 case DW_FORM_sdata:
13334 return "DW_FORM_sdata";
13335 case DW_FORM_strp:
13336 return "DW_FORM_strp";
13337 case DW_FORM_udata:
13338 return "DW_FORM_udata";
13339 case DW_FORM_ref_addr:
13340 return "DW_FORM_ref_addr";
13341 case DW_FORM_ref1:
13342 return "DW_FORM_ref1";
13343 case DW_FORM_ref2:
13344 return "DW_FORM_ref2";
13345 case DW_FORM_ref4:
13346 return "DW_FORM_ref4";
13347 case DW_FORM_ref8:
13348 return "DW_FORM_ref8";
13349 case DW_FORM_ref_udata:
13350 return "DW_FORM_ref_udata";
13351 case DW_FORM_indirect:
13352 return "DW_FORM_indirect";
348e048f
DE
13353 case DW_FORM_sec_offset:
13354 return "DW_FORM_sec_offset";
13355 case DW_FORM_exprloc:
13356 return "DW_FORM_exprloc";
13357 case DW_FORM_flag_present:
13358 return "DW_FORM_flag_present";
55f1336d
TT
13359 case DW_FORM_ref_sig8:
13360 return "DW_FORM_ref_sig8";
c906108c
SS
13361 default:
13362 return "DW_FORM_<unknown>";
13363 }
13364}
13365
13366/* Convert a DWARF stack opcode into its string name. */
13367
9eae7c52 13368const char *
b1bfef65 13369dwarf_stack_op_name (unsigned op)
c906108c
SS
13370{
13371 switch (op)
13372 {
13373 case DW_OP_addr:
13374 return "DW_OP_addr";
13375 case DW_OP_deref:
13376 return "DW_OP_deref";
13377 case DW_OP_const1u:
13378 return "DW_OP_const1u";
13379 case DW_OP_const1s:
13380 return "DW_OP_const1s";
13381 case DW_OP_const2u:
13382 return "DW_OP_const2u";
13383 case DW_OP_const2s:
13384 return "DW_OP_const2s";
13385 case DW_OP_const4u:
13386 return "DW_OP_const4u";
13387 case DW_OP_const4s:
13388 return "DW_OP_const4s";
13389 case DW_OP_const8u:
13390 return "DW_OP_const8u";
13391 case DW_OP_const8s:
13392 return "DW_OP_const8s";
13393 case DW_OP_constu:
13394 return "DW_OP_constu";
13395 case DW_OP_consts:
13396 return "DW_OP_consts";
13397 case DW_OP_dup:
13398 return "DW_OP_dup";
13399 case DW_OP_drop:
13400 return "DW_OP_drop";
13401 case DW_OP_over:
13402 return "DW_OP_over";
13403 case DW_OP_pick:
13404 return "DW_OP_pick";
13405 case DW_OP_swap:
13406 return "DW_OP_swap";
13407 case DW_OP_rot:
13408 return "DW_OP_rot";
13409 case DW_OP_xderef:
13410 return "DW_OP_xderef";
13411 case DW_OP_abs:
13412 return "DW_OP_abs";
13413 case DW_OP_and:
13414 return "DW_OP_and";
13415 case DW_OP_div:
13416 return "DW_OP_div";
13417 case DW_OP_minus:
13418 return "DW_OP_minus";
13419 case DW_OP_mod:
13420 return "DW_OP_mod";
13421 case DW_OP_mul:
13422 return "DW_OP_mul";
13423 case DW_OP_neg:
13424 return "DW_OP_neg";
13425 case DW_OP_not:
13426 return "DW_OP_not";
13427 case DW_OP_or:
13428 return "DW_OP_or";
13429 case DW_OP_plus:
13430 return "DW_OP_plus";
13431 case DW_OP_plus_uconst:
13432 return "DW_OP_plus_uconst";
13433 case DW_OP_shl:
13434 return "DW_OP_shl";
13435 case DW_OP_shr:
13436 return "DW_OP_shr";
13437 case DW_OP_shra:
13438 return "DW_OP_shra";
13439 case DW_OP_xor:
13440 return "DW_OP_xor";
13441 case DW_OP_bra:
13442 return "DW_OP_bra";
13443 case DW_OP_eq:
13444 return "DW_OP_eq";
13445 case DW_OP_ge:
13446 return "DW_OP_ge";
13447 case DW_OP_gt:
13448 return "DW_OP_gt";
13449 case DW_OP_le:
13450 return "DW_OP_le";
13451 case DW_OP_lt:
13452 return "DW_OP_lt";
13453 case DW_OP_ne:
13454 return "DW_OP_ne";
13455 case DW_OP_skip:
13456 return "DW_OP_skip";
13457 case DW_OP_lit0:
13458 return "DW_OP_lit0";
13459 case DW_OP_lit1:
13460 return "DW_OP_lit1";
13461 case DW_OP_lit2:
13462 return "DW_OP_lit2";
13463 case DW_OP_lit3:
13464 return "DW_OP_lit3";
13465 case DW_OP_lit4:
13466 return "DW_OP_lit4";
13467 case DW_OP_lit5:
13468 return "DW_OP_lit5";
13469 case DW_OP_lit6:
13470 return "DW_OP_lit6";
13471 case DW_OP_lit7:
13472 return "DW_OP_lit7";
13473 case DW_OP_lit8:
13474 return "DW_OP_lit8";
13475 case DW_OP_lit9:
13476 return "DW_OP_lit9";
13477 case DW_OP_lit10:
13478 return "DW_OP_lit10";
13479 case DW_OP_lit11:
13480 return "DW_OP_lit11";
13481 case DW_OP_lit12:
13482 return "DW_OP_lit12";
13483 case DW_OP_lit13:
13484 return "DW_OP_lit13";
13485 case DW_OP_lit14:
13486 return "DW_OP_lit14";
13487 case DW_OP_lit15:
13488 return "DW_OP_lit15";
13489 case DW_OP_lit16:
13490 return "DW_OP_lit16";
13491 case DW_OP_lit17:
13492 return "DW_OP_lit17";
13493 case DW_OP_lit18:
13494 return "DW_OP_lit18";
13495 case DW_OP_lit19:
13496 return "DW_OP_lit19";
13497 case DW_OP_lit20:
13498 return "DW_OP_lit20";
13499 case DW_OP_lit21:
13500 return "DW_OP_lit21";
13501 case DW_OP_lit22:
13502 return "DW_OP_lit22";
13503 case DW_OP_lit23:
13504 return "DW_OP_lit23";
13505 case DW_OP_lit24:
13506 return "DW_OP_lit24";
13507 case DW_OP_lit25:
13508 return "DW_OP_lit25";
13509 case DW_OP_lit26:
13510 return "DW_OP_lit26";
13511 case DW_OP_lit27:
13512 return "DW_OP_lit27";
13513 case DW_OP_lit28:
13514 return "DW_OP_lit28";
13515 case DW_OP_lit29:
13516 return "DW_OP_lit29";
13517 case DW_OP_lit30:
13518 return "DW_OP_lit30";
13519 case DW_OP_lit31:
13520 return "DW_OP_lit31";
13521 case DW_OP_reg0:
13522 return "DW_OP_reg0";
13523 case DW_OP_reg1:
13524 return "DW_OP_reg1";
13525 case DW_OP_reg2:
13526 return "DW_OP_reg2";
13527 case DW_OP_reg3:
13528 return "DW_OP_reg3";
13529 case DW_OP_reg4:
13530 return "DW_OP_reg4";
13531 case DW_OP_reg5:
13532 return "DW_OP_reg5";
13533 case DW_OP_reg6:
13534 return "DW_OP_reg6";
13535 case DW_OP_reg7:
13536 return "DW_OP_reg7";
13537 case DW_OP_reg8:
13538 return "DW_OP_reg8";
13539 case DW_OP_reg9:
13540 return "DW_OP_reg9";
13541 case DW_OP_reg10:
13542 return "DW_OP_reg10";
13543 case DW_OP_reg11:
13544 return "DW_OP_reg11";
13545 case DW_OP_reg12:
13546 return "DW_OP_reg12";
13547 case DW_OP_reg13:
13548 return "DW_OP_reg13";
13549 case DW_OP_reg14:
13550 return "DW_OP_reg14";
13551 case DW_OP_reg15:
13552 return "DW_OP_reg15";
13553 case DW_OP_reg16:
13554 return "DW_OP_reg16";
13555 case DW_OP_reg17:
13556 return "DW_OP_reg17";
13557 case DW_OP_reg18:
13558 return "DW_OP_reg18";
13559 case DW_OP_reg19:
13560 return "DW_OP_reg19";
13561 case DW_OP_reg20:
13562 return "DW_OP_reg20";
13563 case DW_OP_reg21:
13564 return "DW_OP_reg21";
13565 case DW_OP_reg22:
13566 return "DW_OP_reg22";
13567 case DW_OP_reg23:
13568 return "DW_OP_reg23";
13569 case DW_OP_reg24:
13570 return "DW_OP_reg24";
13571 case DW_OP_reg25:
13572 return "DW_OP_reg25";
13573 case DW_OP_reg26:
13574 return "DW_OP_reg26";
13575 case DW_OP_reg27:
13576 return "DW_OP_reg27";
13577 case DW_OP_reg28:
13578 return "DW_OP_reg28";
13579 case DW_OP_reg29:
13580 return "DW_OP_reg29";
13581 case DW_OP_reg30:
13582 return "DW_OP_reg30";
13583 case DW_OP_reg31:
13584 return "DW_OP_reg31";
13585 case DW_OP_breg0:
13586 return "DW_OP_breg0";
13587 case DW_OP_breg1:
13588 return "DW_OP_breg1";
13589 case DW_OP_breg2:
13590 return "DW_OP_breg2";
13591 case DW_OP_breg3:
13592 return "DW_OP_breg3";
13593 case DW_OP_breg4:
13594 return "DW_OP_breg4";
13595 case DW_OP_breg5:
13596 return "DW_OP_breg5";
13597 case DW_OP_breg6:
13598 return "DW_OP_breg6";
13599 case DW_OP_breg7:
13600 return "DW_OP_breg7";
13601 case DW_OP_breg8:
13602 return "DW_OP_breg8";
13603 case DW_OP_breg9:
13604 return "DW_OP_breg9";
13605 case DW_OP_breg10:
13606 return "DW_OP_breg10";
13607 case DW_OP_breg11:
13608 return "DW_OP_breg11";
13609 case DW_OP_breg12:
13610 return "DW_OP_breg12";
13611 case DW_OP_breg13:
13612 return "DW_OP_breg13";
13613 case DW_OP_breg14:
13614 return "DW_OP_breg14";
13615 case DW_OP_breg15:
13616 return "DW_OP_breg15";
13617 case DW_OP_breg16:
13618 return "DW_OP_breg16";
13619 case DW_OP_breg17:
13620 return "DW_OP_breg17";
13621 case DW_OP_breg18:
13622 return "DW_OP_breg18";
13623 case DW_OP_breg19:
13624 return "DW_OP_breg19";
13625 case DW_OP_breg20:
13626 return "DW_OP_breg20";
13627 case DW_OP_breg21:
13628 return "DW_OP_breg21";
13629 case DW_OP_breg22:
13630 return "DW_OP_breg22";
13631 case DW_OP_breg23:
13632 return "DW_OP_breg23";
13633 case DW_OP_breg24:
13634 return "DW_OP_breg24";
13635 case DW_OP_breg25:
13636 return "DW_OP_breg25";
13637 case DW_OP_breg26:
13638 return "DW_OP_breg26";
13639 case DW_OP_breg27:
13640 return "DW_OP_breg27";
13641 case DW_OP_breg28:
13642 return "DW_OP_breg28";
13643 case DW_OP_breg29:
13644 return "DW_OP_breg29";
13645 case DW_OP_breg30:
13646 return "DW_OP_breg30";
13647 case DW_OP_breg31:
13648 return "DW_OP_breg31";
13649 case DW_OP_regx:
13650 return "DW_OP_regx";
13651 case DW_OP_fbreg:
13652 return "DW_OP_fbreg";
13653 case DW_OP_bregx:
13654 return "DW_OP_bregx";
13655 case DW_OP_piece:
13656 return "DW_OP_piece";
13657 case DW_OP_deref_size:
13658 return "DW_OP_deref_size";
13659 case DW_OP_xderef_size:
13660 return "DW_OP_xderef_size";
13661 case DW_OP_nop:
13662 return "DW_OP_nop";
b7619582 13663 /* DWARF 3 extensions. */
ed348acc
EZ
13664 case DW_OP_push_object_address:
13665 return "DW_OP_push_object_address";
13666 case DW_OP_call2:
13667 return "DW_OP_call2";
13668 case DW_OP_call4:
13669 return "DW_OP_call4";
13670 case DW_OP_call_ref:
13671 return "DW_OP_call_ref";
b7619582
GF
13672 case DW_OP_form_tls_address:
13673 return "DW_OP_form_tls_address";
13674 case DW_OP_call_frame_cfa:
13675 return "DW_OP_call_frame_cfa";
13676 case DW_OP_bit_piece:
13677 return "DW_OP_bit_piece";
9eae7c52
TT
13678 /* DWARF 4 extensions. */
13679 case DW_OP_implicit_value:
13680 return "DW_OP_implicit_value";
13681 case DW_OP_stack_value:
13682 return "DW_OP_stack_value";
13683 /* GNU extensions. */
ed348acc
EZ
13684 case DW_OP_GNU_push_tls_address:
13685 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13686 case DW_OP_GNU_uninit:
13687 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13688 case DW_OP_GNU_implicit_pointer:
13689 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13690 case DW_OP_GNU_entry_value:
13691 return "DW_OP_GNU_entry_value";
13692 case DW_OP_GNU_const_type:
13693 return "DW_OP_GNU_const_type";
13694 case DW_OP_GNU_regval_type:
13695 return "DW_OP_GNU_regval_type";
13696 case DW_OP_GNU_deref_type:
13697 return "DW_OP_GNU_deref_type";
13698 case DW_OP_GNU_convert:
13699 return "DW_OP_GNU_convert";
13700 case DW_OP_GNU_reinterpret:
13701 return "DW_OP_GNU_reinterpret";
c906108c 13702 default:
b1bfef65 13703 return NULL;
c906108c
SS
13704 }
13705}
13706
13707static char *
fba45db2 13708dwarf_bool_name (unsigned mybool)
c906108c
SS
13709{
13710 if (mybool)
13711 return "TRUE";
13712 else
13713 return "FALSE";
13714}
13715
13716/* Convert a DWARF type code into its string name. */
13717
13718static char *
aa1ee363 13719dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13720{
13721 switch (enc)
13722 {
b7619582
GF
13723 case DW_ATE_void:
13724 return "DW_ATE_void";
c906108c
SS
13725 case DW_ATE_address:
13726 return "DW_ATE_address";
13727 case DW_ATE_boolean:
13728 return "DW_ATE_boolean";
13729 case DW_ATE_complex_float:
13730 return "DW_ATE_complex_float";
13731 case DW_ATE_float:
13732 return "DW_ATE_float";
13733 case DW_ATE_signed:
13734 return "DW_ATE_signed";
13735 case DW_ATE_signed_char:
13736 return "DW_ATE_signed_char";
13737 case DW_ATE_unsigned:
13738 return "DW_ATE_unsigned";
13739 case DW_ATE_unsigned_char:
13740 return "DW_ATE_unsigned_char";
b7619582 13741 /* DWARF 3. */
d9fa45fe
DC
13742 case DW_ATE_imaginary_float:
13743 return "DW_ATE_imaginary_float";
b7619582
GF
13744 case DW_ATE_packed_decimal:
13745 return "DW_ATE_packed_decimal";
13746 case DW_ATE_numeric_string:
13747 return "DW_ATE_numeric_string";
13748 case DW_ATE_edited:
13749 return "DW_ATE_edited";
13750 case DW_ATE_signed_fixed:
13751 return "DW_ATE_signed_fixed";
13752 case DW_ATE_unsigned_fixed:
13753 return "DW_ATE_unsigned_fixed";
13754 case DW_ATE_decimal_float:
13755 return "DW_ATE_decimal_float";
75079b2b
TT
13756 /* DWARF 4. */
13757 case DW_ATE_UTF:
13758 return "DW_ATE_UTF";
b7619582
GF
13759 /* HP extensions. */
13760 case DW_ATE_HP_float80:
13761 return "DW_ATE_HP_float80";
13762 case DW_ATE_HP_complex_float80:
13763 return "DW_ATE_HP_complex_float80";
13764 case DW_ATE_HP_float128:
13765 return "DW_ATE_HP_float128";
13766 case DW_ATE_HP_complex_float128:
13767 return "DW_ATE_HP_complex_float128";
13768 case DW_ATE_HP_floathpintel:
13769 return "DW_ATE_HP_floathpintel";
13770 case DW_ATE_HP_imaginary_float80:
13771 return "DW_ATE_HP_imaginary_float80";
13772 case DW_ATE_HP_imaginary_float128:
13773 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13774 default:
13775 return "DW_ATE_<unknown>";
13776 }
13777}
13778
0963b4bd 13779/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13780
13781#if 0
13782static char *
aa1ee363 13783dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13784{
13785 switch (cfi_opc)
13786 {
13787 case DW_CFA_advance_loc:
13788 return "DW_CFA_advance_loc";
13789 case DW_CFA_offset:
13790 return "DW_CFA_offset";
13791 case DW_CFA_restore:
13792 return "DW_CFA_restore";
13793 case DW_CFA_nop:
13794 return "DW_CFA_nop";
13795 case DW_CFA_set_loc:
13796 return "DW_CFA_set_loc";
13797 case DW_CFA_advance_loc1:
13798 return "DW_CFA_advance_loc1";
13799 case DW_CFA_advance_loc2:
13800 return "DW_CFA_advance_loc2";
13801 case DW_CFA_advance_loc4:
13802 return "DW_CFA_advance_loc4";
13803 case DW_CFA_offset_extended:
13804 return "DW_CFA_offset_extended";
13805 case DW_CFA_restore_extended:
13806 return "DW_CFA_restore_extended";
13807 case DW_CFA_undefined:
13808 return "DW_CFA_undefined";
13809 case DW_CFA_same_value:
13810 return "DW_CFA_same_value";
13811 case DW_CFA_register:
13812 return "DW_CFA_register";
13813 case DW_CFA_remember_state:
13814 return "DW_CFA_remember_state";
13815 case DW_CFA_restore_state:
13816 return "DW_CFA_restore_state";
13817 case DW_CFA_def_cfa:
13818 return "DW_CFA_def_cfa";
13819 case DW_CFA_def_cfa_register:
13820 return "DW_CFA_def_cfa_register";
13821 case DW_CFA_def_cfa_offset:
13822 return "DW_CFA_def_cfa_offset";
b7619582 13823 /* DWARF 3. */
985cb1a3
JM
13824 case DW_CFA_def_cfa_expression:
13825 return "DW_CFA_def_cfa_expression";
13826 case DW_CFA_expression:
13827 return "DW_CFA_expression";
13828 case DW_CFA_offset_extended_sf:
13829 return "DW_CFA_offset_extended_sf";
13830 case DW_CFA_def_cfa_sf:
13831 return "DW_CFA_def_cfa_sf";
13832 case DW_CFA_def_cfa_offset_sf:
13833 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13834 case DW_CFA_val_offset:
13835 return "DW_CFA_val_offset";
13836 case DW_CFA_val_offset_sf:
13837 return "DW_CFA_val_offset_sf";
13838 case DW_CFA_val_expression:
13839 return "DW_CFA_val_expression";
13840 /* SGI/MIPS specific. */
c906108c
SS
13841 case DW_CFA_MIPS_advance_loc8:
13842 return "DW_CFA_MIPS_advance_loc8";
b7619582 13843 /* GNU extensions. */
985cb1a3
JM
13844 case DW_CFA_GNU_window_save:
13845 return "DW_CFA_GNU_window_save";
13846 case DW_CFA_GNU_args_size:
13847 return "DW_CFA_GNU_args_size";
13848 case DW_CFA_GNU_negative_offset_extended:
13849 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13850 default:
13851 return "DW_CFA_<unknown>";
13852 }
13853}
13854#endif
13855
f9aca02d 13856static void
d97bc12b 13857dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13858{
13859 unsigned int i;
13860
d97bc12b
DE
13861 print_spaces (indent, f);
13862 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13863 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13864
13865 if (die->parent != NULL)
13866 {
13867 print_spaces (indent, f);
13868 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13869 die->parent->offset);
13870 }
13871
13872 print_spaces (indent, f);
13873 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13874 dwarf_bool_name (die->child != NULL));
c906108c 13875
d97bc12b
DE
13876 print_spaces (indent, f);
13877 fprintf_unfiltered (f, " attributes:\n");
13878
c906108c
SS
13879 for (i = 0; i < die->num_attrs; ++i)
13880 {
d97bc12b
DE
13881 print_spaces (indent, f);
13882 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13883 dwarf_attr_name (die->attrs[i].name),
13884 dwarf_form_name (die->attrs[i].form));
d97bc12b 13885
c906108c
SS
13886 switch (die->attrs[i].form)
13887 {
13888 case DW_FORM_ref_addr:
13889 case DW_FORM_addr:
d97bc12b 13890 fprintf_unfiltered (f, "address: ");
5af949e3 13891 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13892 break;
13893 case DW_FORM_block2:
13894 case DW_FORM_block4:
13895 case DW_FORM_block:
13896 case DW_FORM_block1:
3e43a32a
MS
13897 fprintf_unfiltered (f, "block: size %d",
13898 DW_BLOCK (&die->attrs[i])->size);
c906108c 13899 break;
2dc7f7b3
TT
13900 case DW_FORM_exprloc:
13901 fprintf_unfiltered (f, "expression: size %u",
13902 DW_BLOCK (&die->attrs[i])->size);
13903 break;
10b3939b
DJ
13904 case DW_FORM_ref1:
13905 case DW_FORM_ref2:
13906 case DW_FORM_ref4:
d97bc12b 13907 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13908 (long) (DW_ADDR (&die->attrs[i])));
13909 break;
c906108c
SS
13910 case DW_FORM_data1:
13911 case DW_FORM_data2:
13912 case DW_FORM_data4:
ce5d95e1 13913 case DW_FORM_data8:
c906108c
SS
13914 case DW_FORM_udata:
13915 case DW_FORM_sdata:
43bbcdc2
PH
13916 fprintf_unfiltered (f, "constant: %s",
13917 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13918 break;
2dc7f7b3
TT
13919 case DW_FORM_sec_offset:
13920 fprintf_unfiltered (f, "section offset: %s",
13921 pulongest (DW_UNSND (&die->attrs[i])));
13922 break;
55f1336d 13923 case DW_FORM_ref_sig8:
348e048f
DE
13924 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13925 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13926 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13927 else
13928 fprintf_unfiltered (f, "signatured type, offset: unknown");
13929 break;
c906108c 13930 case DW_FORM_string:
4bdf3d34 13931 case DW_FORM_strp:
8285870a 13932 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13933 DW_STRING (&die->attrs[i])
8285870a
JK
13934 ? DW_STRING (&die->attrs[i]) : "",
13935 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13936 break;
13937 case DW_FORM_flag:
13938 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13939 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13940 else
d97bc12b 13941 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13942 break;
2dc7f7b3
TT
13943 case DW_FORM_flag_present:
13944 fprintf_unfiltered (f, "flag: TRUE");
13945 break;
a8329558 13946 case DW_FORM_indirect:
0963b4bd
MS
13947 /* The reader will have reduced the indirect form to
13948 the "base form" so this form should not occur. */
3e43a32a
MS
13949 fprintf_unfiltered (f,
13950 "unexpected attribute form: DW_FORM_indirect");
a8329558 13951 break;
c906108c 13952 default:
d97bc12b 13953 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13954 die->attrs[i].form);
d97bc12b 13955 break;
c906108c 13956 }
d97bc12b 13957 fprintf_unfiltered (f, "\n");
c906108c
SS
13958 }
13959}
13960
f9aca02d 13961static void
d97bc12b 13962dump_die_for_error (struct die_info *die)
c906108c 13963{
d97bc12b
DE
13964 dump_die_shallow (gdb_stderr, 0, die);
13965}
13966
13967static void
13968dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13969{
13970 int indent = level * 4;
13971
13972 gdb_assert (die != NULL);
13973
13974 if (level >= max_level)
13975 return;
13976
13977 dump_die_shallow (f, indent, die);
13978
13979 if (die->child != NULL)
c906108c 13980 {
d97bc12b
DE
13981 print_spaces (indent, f);
13982 fprintf_unfiltered (f, " Children:");
13983 if (level + 1 < max_level)
13984 {
13985 fprintf_unfiltered (f, "\n");
13986 dump_die_1 (f, level + 1, max_level, die->child);
13987 }
13988 else
13989 {
3e43a32a
MS
13990 fprintf_unfiltered (f,
13991 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13992 }
13993 }
13994
13995 if (die->sibling != NULL && level > 0)
13996 {
13997 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13998 }
13999}
14000
d97bc12b
DE
14001/* This is called from the pdie macro in gdbinit.in.
14002 It's not static so gcc will keep a copy callable from gdb. */
14003
14004void
14005dump_die (struct die_info *die, int max_level)
14006{
14007 dump_die_1 (gdb_stdlog, 0, max_level, die);
14008}
14009
f9aca02d 14010static void
51545339 14011store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14012{
51545339 14013 void **slot;
c906108c 14014
51545339
DJ
14015 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14016
14017 *slot = die;
c906108c
SS
14018}
14019
93311388
DE
14020static int
14021is_ref_attr (struct attribute *attr)
c906108c 14022{
c906108c
SS
14023 switch (attr->form)
14024 {
14025 case DW_FORM_ref_addr:
c906108c
SS
14026 case DW_FORM_ref1:
14027 case DW_FORM_ref2:
14028 case DW_FORM_ref4:
613e1657 14029 case DW_FORM_ref8:
c906108c 14030 case DW_FORM_ref_udata:
93311388 14031 return 1;
c906108c 14032 default:
93311388 14033 return 0;
c906108c 14034 }
93311388
DE
14035}
14036
14037static unsigned int
14038dwarf2_get_ref_die_offset (struct attribute *attr)
14039{
14040 if (is_ref_attr (attr))
14041 return DW_ADDR (attr);
14042
14043 complaint (&symfile_complaints,
14044 _("unsupported die ref attribute form: '%s'"),
14045 dwarf_form_name (attr->form));
14046 return 0;
c906108c
SS
14047}
14048
43bbcdc2
PH
14049/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14050 * the value held by the attribute is not constant. */
a02abb62 14051
43bbcdc2 14052static LONGEST
a02abb62
JB
14053dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14054{
14055 if (attr->form == DW_FORM_sdata)
14056 return DW_SND (attr);
14057 else if (attr->form == DW_FORM_udata
14058 || attr->form == DW_FORM_data1
14059 || attr->form == DW_FORM_data2
14060 || attr->form == DW_FORM_data4
14061 || attr->form == DW_FORM_data8)
14062 return DW_UNSND (attr);
14063 else
14064 {
3e43a32a
MS
14065 complaint (&symfile_complaints,
14066 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14067 dwarf_form_name (attr->form));
14068 return default_value;
14069 }
14070}
14071
03dd20cc 14072/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14073 unit and add it to our queue.
14074 The result is non-zero if PER_CU was queued, otherwise the result is zero
14075 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14076
348e048f 14077static int
03dd20cc
DJ
14078maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14079 struct dwarf2_per_cu_data *per_cu)
14080{
98bfdba5
PA
14081 /* We may arrive here during partial symbol reading, if we need full
14082 DIEs to process an unusual case (e.g. template arguments). Do
14083 not queue PER_CU, just tell our caller to load its DIEs. */
14084 if (dwarf2_per_objfile->reading_partial_symbols)
14085 {
14086 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14087 return 1;
14088 return 0;
14089 }
14090
03dd20cc
DJ
14091 /* Mark the dependence relation so that we don't flush PER_CU
14092 too early. */
14093 dwarf2_add_dependence (this_cu, per_cu);
14094
14095 /* If it's already on the queue, we have nothing to do. */
14096 if (per_cu->queued)
348e048f 14097 return 0;
03dd20cc
DJ
14098
14099 /* If the compilation unit is already loaded, just mark it as
14100 used. */
14101 if (per_cu->cu != NULL)
14102 {
14103 per_cu->cu->last_used = 0;
348e048f 14104 return 0;
03dd20cc
DJ
14105 }
14106
14107 /* Add it to the queue. */
a0f42c21 14108 queue_comp_unit (per_cu);
348e048f
DE
14109
14110 return 1;
14111}
14112
14113/* Follow reference or signature attribute ATTR of SRC_DIE.
14114 On entry *REF_CU is the CU of SRC_DIE.
14115 On exit *REF_CU is the CU of the result. */
14116
14117static struct die_info *
14118follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14119 struct dwarf2_cu **ref_cu)
14120{
14121 struct die_info *die;
14122
14123 if (is_ref_attr (attr))
14124 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14125 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14126 die = follow_die_sig (src_die, attr, ref_cu);
14127 else
14128 {
14129 dump_die_for_error (src_die);
14130 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14131 (*ref_cu)->objfile->name);
14132 }
14133
14134 return die;
03dd20cc
DJ
14135}
14136
5c631832 14137/* Follow reference OFFSET.
673bfd45
DE
14138 On entry *REF_CU is the CU of the source die referencing OFFSET.
14139 On exit *REF_CU is the CU of the result.
14140 Returns NULL if OFFSET is invalid. */
f504f079 14141
f9aca02d 14142static struct die_info *
5c631832 14143follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14144{
10b3939b 14145 struct die_info temp_die;
f2f0e013 14146 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14147
348e048f
DE
14148 gdb_assert (cu->per_cu != NULL);
14149
98bfdba5
PA
14150 target_cu = cu;
14151
b0df02fd 14152 if (cu->per_cu->debug_types_section)
348e048f
DE
14153 {
14154 /* .debug_types CUs cannot reference anything outside their CU.
14155 If they need to, they have to reference a signatured type via
55f1336d 14156 DW_FORM_ref_sig8. */
348e048f 14157 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14158 return NULL;
348e048f
DE
14159 }
14160 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14161 {
14162 struct dwarf2_per_cu_data *per_cu;
9a619af0 14163
45452591 14164 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14165
14166 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14167 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14168 load_full_comp_unit (per_cu);
03dd20cc 14169
10b3939b
DJ
14170 target_cu = per_cu->cu;
14171 }
98bfdba5
PA
14172 else if (cu->dies == NULL)
14173 {
14174 /* We're loading full DIEs during partial symbol reading. */
14175 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14176 load_full_comp_unit (cu->per_cu);
98bfdba5 14177 }
c906108c 14178
f2f0e013 14179 *ref_cu = target_cu;
51545339 14180 temp_die.offset = offset;
5c631832
JK
14181 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14182}
10b3939b 14183
5c631832
JK
14184/* Follow reference attribute ATTR of SRC_DIE.
14185 On entry *REF_CU is the CU of SRC_DIE.
14186 On exit *REF_CU is the CU of the result. */
14187
14188static struct die_info *
14189follow_die_ref (struct die_info *src_die, struct attribute *attr,
14190 struct dwarf2_cu **ref_cu)
14191{
14192 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14193 struct dwarf2_cu *cu = *ref_cu;
14194 struct die_info *die;
14195
14196 die = follow_die_offset (offset, ref_cu);
14197 if (!die)
14198 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14199 "at 0x%x [in module %s]"),
14200 offset, src_die->offset, cu->objfile->name);
348e048f 14201
5c631832
JK
14202 return die;
14203}
14204
d83e736b
JK
14205/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14206 Returned value is intended for DW_OP_call*. Returned
14207 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14208
14209struct dwarf2_locexpr_baton
14210dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14211 struct dwarf2_per_cu_data *per_cu,
14212 CORE_ADDR (*get_frame_pc) (void *baton),
14213 void *baton)
5c631832 14214{
918dd910 14215 struct dwarf2_cu *cu;
5c631832
JK
14216 struct die_info *die;
14217 struct attribute *attr;
14218 struct dwarf2_locexpr_baton retval;
14219
8cf6f0b1
TT
14220 dw2_setup (per_cu->objfile);
14221
918dd910
JK
14222 if (per_cu->cu == NULL)
14223 load_cu (per_cu);
14224 cu = per_cu->cu;
14225
5c631832
JK
14226 die = follow_die_offset (offset, &cu);
14227 if (!die)
14228 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
bb5ed363 14229 offset, per_cu->objfile->name);
5c631832
JK
14230
14231 attr = dwarf2_attr (die, DW_AT_location, cu);
14232 if (!attr)
14233 {
e103e986
JK
14234 /* DWARF: "If there is no such attribute, then there is no effect.".
14235 DATA is ignored if SIZE is 0. */
5c631832 14236
e103e986 14237 retval.data = NULL;
5c631832
JK
14238 retval.size = 0;
14239 }
8cf6f0b1
TT
14240 else if (attr_form_is_section_offset (attr))
14241 {
14242 struct dwarf2_loclist_baton loclist_baton;
14243 CORE_ADDR pc = (*get_frame_pc) (baton);
14244 size_t size;
14245
14246 fill_in_loclist_baton (cu, &loclist_baton, attr);
14247
14248 retval.data = dwarf2_find_location_expression (&loclist_baton,
14249 &size, pc);
14250 retval.size = size;
14251 }
5c631832
JK
14252 else
14253 {
14254 if (!attr_form_is_block (attr))
14255 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14256 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
bb5ed363 14257 offset, per_cu->objfile->name);
5c631832
JK
14258
14259 retval.data = DW_BLOCK (attr)->data;
14260 retval.size = DW_BLOCK (attr)->size;
14261 }
14262 retval.per_cu = cu->per_cu;
918dd910 14263
918dd910
JK
14264 age_cached_comp_units ();
14265
5c631832 14266 return retval;
348e048f
DE
14267}
14268
8a9b8146
TT
14269/* Return the type of the DIE at DIE_OFFSET in the CU named by
14270 PER_CU. */
14271
14272struct type *
14273dwarf2_get_die_type (unsigned int die_offset,
14274 struct dwarf2_per_cu_data *per_cu)
14275{
8a9b8146 14276 dw2_setup (per_cu->objfile);
9ff3b74f 14277 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14278}
14279
348e048f
DE
14280/* Follow the signature attribute ATTR in SRC_DIE.
14281 On entry *REF_CU is the CU of SRC_DIE.
14282 On exit *REF_CU is the CU of the result. */
14283
14284static struct die_info *
14285follow_die_sig (struct die_info *src_die, struct attribute *attr,
14286 struct dwarf2_cu **ref_cu)
14287{
14288 struct objfile *objfile = (*ref_cu)->objfile;
14289 struct die_info temp_die;
14290 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14291 struct dwarf2_cu *sig_cu;
14292 struct die_info *die;
14293
14294 /* sig_type will be NULL if the signatured type is missing from
14295 the debug info. */
14296 if (sig_type == NULL)
14297 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14298 "at 0x%x [in module %s]"),
14299 src_die->offset, objfile->name);
14300
14301 /* If necessary, add it to the queue and load its DIEs. */
14302
14303 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14304 read_signatured_type (sig_type);
348e048f
DE
14305
14306 gdb_assert (sig_type->per_cu.cu != NULL);
14307
14308 sig_cu = sig_type->per_cu.cu;
14309 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14310 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14311 if (die)
14312 {
14313 *ref_cu = sig_cu;
14314 return die;
14315 }
14316
3e43a32a
MS
14317 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14318 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14319 sig_type->type_offset, src_die->offset, objfile->name);
14320}
14321
14322/* Given an offset of a signatured type, return its signatured_type. */
14323
14324static struct signatured_type *
8b70b953
TT
14325lookup_signatured_type_at_offset (struct objfile *objfile,
14326 struct dwarf2_section_info *section,
14327 unsigned int offset)
348e048f 14328{
8b70b953 14329 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14330 unsigned int length, initial_length_size;
14331 unsigned int sig_offset;
14332 struct signatured_type find_entry, *type_sig;
14333
14334 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14335 sig_offset = (initial_length_size
14336 + 2 /*version*/
14337 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14338 + 1 /*address_size*/);
14339 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14340 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14341
14342 /* This is only used to lookup previously recorded types.
14343 If we didn't find it, it's our bug. */
14344 gdb_assert (type_sig != NULL);
b3c8eb43 14345 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14346
14347 return type_sig;
14348}
14349
e5fe5e75 14350/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14351
14352static void
e5fe5e75 14353load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14354{
e5fe5e75
DE
14355 struct objfile *objfile = per_cu->objfile;
14356 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14357 unsigned int offset = per_cu->offset;
348e048f
DE
14358 struct signatured_type *type_sig;
14359
8b70b953 14360 dwarf2_read_section (objfile, sect);
be391dca 14361
348e048f 14362 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14363 hash table lookup. */
14364 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14365 the signature to assert we found the right one.
14366 Ok, but it's a lot of work. We should simplify things so any needed
14367 assert doesn't require all this clumsiness. */
8b70b953 14368 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14369
14370 gdb_assert (type_sig->per_cu.cu == NULL);
14371
a0f42c21 14372 read_signatured_type (type_sig);
348e048f
DE
14373
14374 gdb_assert (type_sig->per_cu.cu != NULL);
14375}
14376
14377/* Read in a signatured type and build its CU and DIEs. */
14378
14379static void
a0f42c21 14380read_signatured_type (struct signatured_type *type_sig)
348e048f 14381{
a0f42c21 14382 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14383 gdb_byte *types_ptr;
348e048f
DE
14384 struct die_reader_specs reader_specs;
14385 struct dwarf2_cu *cu;
14386 ULONGEST signature;
14387 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14388 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14389
8b70b953
TT
14390 dwarf2_read_section (objfile, section);
14391 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14392
348e048f
DE
14393 gdb_assert (type_sig->per_cu.cu == NULL);
14394
9816fde3 14395 cu = xmalloc (sizeof (*cu));
23745b47 14396 init_one_comp_unit (cu, &type_sig->per_cu);
348e048f
DE
14397
14398 /* If an error occurs while loading, release our storage. */
68dc6402 14399 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14400
8b70b953 14401 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14402 types_ptr, objfile->obfd);
14403 gdb_assert (signature == type_sig->signature);
14404
14405 cu->die_hash
14406 = htab_create_alloc_ex (cu->header.length / 12,
14407 die_hash,
14408 die_eq,
14409 NULL,
14410 &cu->comp_unit_obstack,
14411 hashtab_obstack_allocate,
14412 dummy_obstack_deallocate);
14413
e5fe5e75 14414 dwarf2_read_abbrevs (cu);
348e048f
DE
14415 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14416
14417 init_cu_die_reader (&reader_specs, cu);
14418
14419 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14420 NULL /*parent*/);
14421
14422 /* We try not to read any attributes in this function, because not
9cdd5dbd 14423 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14424 table processing isn't initialized. But we have to set the CU language,
14425 or we won't be able to build types correctly. */
9816fde3 14426 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14427
14428 do_cleanups (back_to);
14429
14430 /* We've successfully allocated this compilation unit. Let our caller
14431 clean it up when finished with it. */
14432 discard_cleanups (free_cu_cleanup);
14433
14434 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14435 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14436}
14437
c906108c
SS
14438/* Decode simple location descriptions.
14439 Given a pointer to a dwarf block that defines a location, compute
14440 the location and return the value.
14441
4cecd739
DJ
14442 NOTE drow/2003-11-18: This function is called in two situations
14443 now: for the address of static or global variables (partial symbols
14444 only) and for offsets into structures which are expected to be
14445 (more or less) constant. The partial symbol case should go away,
14446 and only the constant case should remain. That will let this
14447 function complain more accurately. A few special modes are allowed
14448 without complaint for global variables (for instance, global
14449 register values and thread-local values).
c906108c
SS
14450
14451 A location description containing no operations indicates that the
4cecd739 14452 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14453 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14454 callers will only want a very basic result and this can become a
21ae7a4d
JK
14455 complaint.
14456
14457 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14458
14459static CORE_ADDR
e7c27a73 14460decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14461{
e7c27a73 14462 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14463 int i;
14464 int size = blk->size;
14465 gdb_byte *data = blk->data;
14466 CORE_ADDR stack[64];
14467 int stacki;
14468 unsigned int bytes_read, unsnd;
14469 gdb_byte op;
c906108c 14470
21ae7a4d
JK
14471 i = 0;
14472 stacki = 0;
14473 stack[stacki] = 0;
14474 stack[++stacki] = 0;
14475
14476 while (i < size)
14477 {
14478 op = data[i++];
14479 switch (op)
14480 {
14481 case DW_OP_lit0:
14482 case DW_OP_lit1:
14483 case DW_OP_lit2:
14484 case DW_OP_lit3:
14485 case DW_OP_lit4:
14486 case DW_OP_lit5:
14487 case DW_OP_lit6:
14488 case DW_OP_lit7:
14489 case DW_OP_lit8:
14490 case DW_OP_lit9:
14491 case DW_OP_lit10:
14492 case DW_OP_lit11:
14493 case DW_OP_lit12:
14494 case DW_OP_lit13:
14495 case DW_OP_lit14:
14496 case DW_OP_lit15:
14497 case DW_OP_lit16:
14498 case DW_OP_lit17:
14499 case DW_OP_lit18:
14500 case DW_OP_lit19:
14501 case DW_OP_lit20:
14502 case DW_OP_lit21:
14503 case DW_OP_lit22:
14504 case DW_OP_lit23:
14505 case DW_OP_lit24:
14506 case DW_OP_lit25:
14507 case DW_OP_lit26:
14508 case DW_OP_lit27:
14509 case DW_OP_lit28:
14510 case DW_OP_lit29:
14511 case DW_OP_lit30:
14512 case DW_OP_lit31:
14513 stack[++stacki] = op - DW_OP_lit0;
14514 break;
f1bea926 14515
21ae7a4d
JK
14516 case DW_OP_reg0:
14517 case DW_OP_reg1:
14518 case DW_OP_reg2:
14519 case DW_OP_reg3:
14520 case DW_OP_reg4:
14521 case DW_OP_reg5:
14522 case DW_OP_reg6:
14523 case DW_OP_reg7:
14524 case DW_OP_reg8:
14525 case DW_OP_reg9:
14526 case DW_OP_reg10:
14527 case DW_OP_reg11:
14528 case DW_OP_reg12:
14529 case DW_OP_reg13:
14530 case DW_OP_reg14:
14531 case DW_OP_reg15:
14532 case DW_OP_reg16:
14533 case DW_OP_reg17:
14534 case DW_OP_reg18:
14535 case DW_OP_reg19:
14536 case DW_OP_reg20:
14537 case DW_OP_reg21:
14538 case DW_OP_reg22:
14539 case DW_OP_reg23:
14540 case DW_OP_reg24:
14541 case DW_OP_reg25:
14542 case DW_OP_reg26:
14543 case DW_OP_reg27:
14544 case DW_OP_reg28:
14545 case DW_OP_reg29:
14546 case DW_OP_reg30:
14547 case DW_OP_reg31:
14548 stack[++stacki] = op - DW_OP_reg0;
14549 if (i < size)
14550 dwarf2_complex_location_expr_complaint ();
14551 break;
c906108c 14552
21ae7a4d
JK
14553 case DW_OP_regx:
14554 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14555 i += bytes_read;
14556 stack[++stacki] = unsnd;
14557 if (i < size)
14558 dwarf2_complex_location_expr_complaint ();
14559 break;
c906108c 14560
21ae7a4d
JK
14561 case DW_OP_addr:
14562 stack[++stacki] = read_address (objfile->obfd, &data[i],
14563 cu, &bytes_read);
14564 i += bytes_read;
14565 break;
d53d4ac5 14566
21ae7a4d
JK
14567 case DW_OP_const1u:
14568 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14569 i += 1;
14570 break;
14571
14572 case DW_OP_const1s:
14573 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14574 i += 1;
14575 break;
14576
14577 case DW_OP_const2u:
14578 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14579 i += 2;
14580 break;
14581
14582 case DW_OP_const2s:
14583 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14584 i += 2;
14585 break;
d53d4ac5 14586
21ae7a4d
JK
14587 case DW_OP_const4u:
14588 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14589 i += 4;
14590 break;
14591
14592 case DW_OP_const4s:
14593 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14594 i += 4;
14595 break;
14596
585861ea
JK
14597 case DW_OP_const8u:
14598 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14599 i += 8;
14600 break;
14601
21ae7a4d
JK
14602 case DW_OP_constu:
14603 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14604 &bytes_read);
14605 i += bytes_read;
14606 break;
14607
14608 case DW_OP_consts:
14609 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14610 i += bytes_read;
14611 break;
14612
14613 case DW_OP_dup:
14614 stack[stacki + 1] = stack[stacki];
14615 stacki++;
14616 break;
14617
14618 case DW_OP_plus:
14619 stack[stacki - 1] += stack[stacki];
14620 stacki--;
14621 break;
14622
14623 case DW_OP_plus_uconst:
14624 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14625 &bytes_read);
14626 i += bytes_read;
14627 break;
14628
14629 case DW_OP_minus:
14630 stack[stacki - 1] -= stack[stacki];
14631 stacki--;
14632 break;
14633
14634 case DW_OP_deref:
14635 /* If we're not the last op, then we definitely can't encode
14636 this using GDB's address_class enum. This is valid for partial
14637 global symbols, although the variable's address will be bogus
14638 in the psymtab. */
14639 if (i < size)
14640 dwarf2_complex_location_expr_complaint ();
14641 break;
14642
14643 case DW_OP_GNU_push_tls_address:
14644 /* The top of the stack has the offset from the beginning
14645 of the thread control block at which the variable is located. */
14646 /* Nothing should follow this operator, so the top of stack would
14647 be returned. */
14648 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14649 address will be bogus in the psymtab. Make it always at least
14650 non-zero to not look as a variable garbage collected by linker
14651 which have DW_OP_addr 0. */
21ae7a4d
JK
14652 if (i < size)
14653 dwarf2_complex_location_expr_complaint ();
585861ea 14654 stack[stacki]++;
21ae7a4d
JK
14655 break;
14656
14657 case DW_OP_GNU_uninit:
14658 break;
14659
14660 default:
14661 {
14662 const char *name = dwarf_stack_op_name (op);
14663
14664 if (name)
14665 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14666 name);
14667 else
14668 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14669 op);
14670 }
14671
14672 return (stack[stacki]);
d53d4ac5 14673 }
3c6e0cb3 14674
21ae7a4d
JK
14675 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14676 outside of the allocated space. Also enforce minimum>0. */
14677 if (stacki >= ARRAY_SIZE (stack) - 1)
14678 {
14679 complaint (&symfile_complaints,
14680 _("location description stack overflow"));
14681 return 0;
14682 }
14683
14684 if (stacki <= 0)
14685 {
14686 complaint (&symfile_complaints,
14687 _("location description stack underflow"));
14688 return 0;
14689 }
14690 }
14691 return (stack[stacki]);
c906108c
SS
14692}
14693
14694/* memory allocation interface */
14695
c906108c 14696static struct dwarf_block *
7b5a2f43 14697dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14698{
14699 struct dwarf_block *blk;
14700
14701 blk = (struct dwarf_block *)
7b5a2f43 14702 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14703 return (blk);
14704}
14705
14706static struct abbrev_info *
f3dd6933 14707dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14708{
14709 struct abbrev_info *abbrev;
14710
f3dd6933
DJ
14711 abbrev = (struct abbrev_info *)
14712 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14713 memset (abbrev, 0, sizeof (struct abbrev_info));
14714 return (abbrev);
14715}
14716
14717static struct die_info *
b60c80d6 14718dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14719{
14720 struct die_info *die;
b60c80d6
DJ
14721 size_t size = sizeof (struct die_info);
14722
14723 if (num_attrs > 1)
14724 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14725
b60c80d6 14726 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14727 memset (die, 0, sizeof (struct die_info));
14728 return (die);
14729}
2e276125
JB
14730
14731\f
14732/* Macro support. */
14733
2e276125
JB
14734/* Return the full name of file number I in *LH's file name table.
14735 Use COMP_DIR as the name of the current directory of the
14736 compilation. The result is allocated using xmalloc; the caller is
14737 responsible for freeing it. */
14738static char *
14739file_full_name (int file, struct line_header *lh, const char *comp_dir)
14740{
6a83a1e6
EZ
14741 /* Is the file number a valid index into the line header's file name
14742 table? Remember that file numbers start with one, not zero. */
14743 if (1 <= file && file <= lh->num_file_names)
14744 {
14745 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14746
6a83a1e6
EZ
14747 if (IS_ABSOLUTE_PATH (fe->name))
14748 return xstrdup (fe->name);
14749 else
14750 {
14751 const char *dir;
14752 int dir_len;
14753 char *full_name;
14754
14755 if (fe->dir_index)
14756 dir = lh->include_dirs[fe->dir_index - 1];
14757 else
14758 dir = comp_dir;
14759
14760 if (dir)
14761 {
14762 dir_len = strlen (dir);
14763 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14764 strcpy (full_name, dir);
14765 full_name[dir_len] = '/';
14766 strcpy (full_name + dir_len + 1, fe->name);
14767 return full_name;
14768 }
14769 else
14770 return xstrdup (fe->name);
14771 }
14772 }
2e276125
JB
14773 else
14774 {
6a83a1e6
EZ
14775 /* The compiler produced a bogus file number. We can at least
14776 record the macro definitions made in the file, even if we
14777 won't be able to find the file by name. */
14778 char fake_name[80];
9a619af0 14779
6a83a1e6 14780 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14781
6e70227d 14782 complaint (&symfile_complaints,
6a83a1e6
EZ
14783 _("bad file number in macro information (%d)"),
14784 file);
2e276125 14785
6a83a1e6 14786 return xstrdup (fake_name);
2e276125
JB
14787 }
14788}
14789
14790
14791static struct macro_source_file *
14792macro_start_file (int file, int line,
14793 struct macro_source_file *current_file,
14794 const char *comp_dir,
14795 struct line_header *lh, struct objfile *objfile)
14796{
14797 /* The full name of this source file. */
14798 char *full_name = file_full_name (file, lh, comp_dir);
14799
14800 /* We don't create a macro table for this compilation unit
14801 at all until we actually get a filename. */
14802 if (! pending_macros)
4a146b47 14803 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14804 objfile->macro_cache);
2e276125
JB
14805
14806 if (! current_file)
14807 /* If we have no current file, then this must be the start_file
14808 directive for the compilation unit's main source file. */
14809 current_file = macro_set_main (pending_macros, full_name);
14810 else
14811 current_file = macro_include (current_file, line, full_name);
14812
14813 xfree (full_name);
6e70227d 14814
2e276125
JB
14815 return current_file;
14816}
14817
14818
14819/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14820 followed by a null byte. */
14821static char *
14822copy_string (const char *buf, int len)
14823{
14824 char *s = xmalloc (len + 1);
9a619af0 14825
2e276125
JB
14826 memcpy (s, buf, len);
14827 s[len] = '\0';
2e276125
JB
14828 return s;
14829}
14830
14831
14832static const char *
14833consume_improper_spaces (const char *p, const char *body)
14834{
14835 if (*p == ' ')
14836 {
4d3c2250 14837 complaint (&symfile_complaints,
3e43a32a
MS
14838 _("macro definition contains spaces "
14839 "in formal argument list:\n`%s'"),
4d3c2250 14840 body);
2e276125
JB
14841
14842 while (*p == ' ')
14843 p++;
14844 }
14845
14846 return p;
14847}
14848
14849
14850static void
14851parse_macro_definition (struct macro_source_file *file, int line,
14852 const char *body)
14853{
14854 const char *p;
14855
14856 /* The body string takes one of two forms. For object-like macro
14857 definitions, it should be:
14858
14859 <macro name> " " <definition>
14860
14861 For function-like macro definitions, it should be:
14862
14863 <macro name> "() " <definition>
14864 or
14865 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14866
14867 Spaces may appear only where explicitly indicated, and in the
14868 <definition>.
14869
14870 The Dwarf 2 spec says that an object-like macro's name is always
14871 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14872 the space when the macro's definition is the empty string.
2e276125
JB
14873
14874 The Dwarf 2 spec says that there should be no spaces between the
14875 formal arguments in a function-like macro's formal argument list,
14876 but versions of GCC around March 2002 include spaces after the
14877 commas. */
14878
14879
14880 /* Find the extent of the macro name. The macro name is terminated
14881 by either a space or null character (for an object-like macro) or
14882 an opening paren (for a function-like macro). */
14883 for (p = body; *p; p++)
14884 if (*p == ' ' || *p == '(')
14885 break;
14886
14887 if (*p == ' ' || *p == '\0')
14888 {
14889 /* It's an object-like macro. */
14890 int name_len = p - body;
14891 char *name = copy_string (body, name_len);
14892 const char *replacement;
14893
14894 if (*p == ' ')
14895 replacement = body + name_len + 1;
14896 else
14897 {
4d3c2250 14898 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14899 replacement = body + name_len;
14900 }
6e70227d 14901
2e276125
JB
14902 macro_define_object (file, line, name, replacement);
14903
14904 xfree (name);
14905 }
14906 else if (*p == '(')
14907 {
14908 /* It's a function-like macro. */
14909 char *name = copy_string (body, p - body);
14910 int argc = 0;
14911 int argv_size = 1;
14912 char **argv = xmalloc (argv_size * sizeof (*argv));
14913
14914 p++;
14915
14916 p = consume_improper_spaces (p, body);
14917
14918 /* Parse the formal argument list. */
14919 while (*p && *p != ')')
14920 {
14921 /* Find the extent of the current argument name. */
14922 const char *arg_start = p;
14923
14924 while (*p && *p != ',' && *p != ')' && *p != ' ')
14925 p++;
14926
14927 if (! *p || p == arg_start)
4d3c2250 14928 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14929 else
14930 {
14931 /* Make sure argv has room for the new argument. */
14932 if (argc >= argv_size)
14933 {
14934 argv_size *= 2;
14935 argv = xrealloc (argv, argv_size * sizeof (*argv));
14936 }
14937
14938 argv[argc++] = copy_string (arg_start, p - arg_start);
14939 }
14940
14941 p = consume_improper_spaces (p, body);
14942
14943 /* Consume the comma, if present. */
14944 if (*p == ',')
14945 {
14946 p++;
14947
14948 p = consume_improper_spaces (p, body);
14949 }
14950 }
14951
14952 if (*p == ')')
14953 {
14954 p++;
14955
14956 if (*p == ' ')
14957 /* Perfectly formed definition, no complaints. */
14958 macro_define_function (file, line, name,
6e70227d 14959 argc, (const char **) argv,
2e276125
JB
14960 p + 1);
14961 else if (*p == '\0')
14962 {
14963 /* Complain, but do define it. */
4d3c2250 14964 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14965 macro_define_function (file, line, name,
6e70227d 14966 argc, (const char **) argv,
2e276125
JB
14967 p);
14968 }
14969 else
14970 /* Just complain. */
4d3c2250 14971 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14972 }
14973 else
14974 /* Just complain. */
4d3c2250 14975 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14976
14977 xfree (name);
14978 {
14979 int i;
14980
14981 for (i = 0; i < argc; i++)
14982 xfree (argv[i]);
14983 }
14984 xfree (argv);
14985 }
14986 else
4d3c2250 14987 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14988}
14989
cf2c3c16
TT
14990/* Skip some bytes from BYTES according to the form given in FORM.
14991 Returns the new pointer. */
2e276125 14992
cf2c3c16
TT
14993static gdb_byte *
14994skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14995 enum dwarf_form form,
14996 unsigned int offset_size,
14997 struct dwarf2_section_info *section)
2e276125 14998{
cf2c3c16 14999 unsigned int bytes_read;
2e276125 15000
cf2c3c16 15001 switch (form)
2e276125 15002 {
cf2c3c16
TT
15003 case DW_FORM_data1:
15004 case DW_FORM_flag:
15005 ++bytes;
15006 break;
15007
15008 case DW_FORM_data2:
15009 bytes += 2;
15010 break;
15011
15012 case DW_FORM_data4:
15013 bytes += 4;
15014 break;
15015
15016 case DW_FORM_data8:
15017 bytes += 8;
15018 break;
15019
15020 case DW_FORM_string:
15021 read_direct_string (abfd, bytes, &bytes_read);
15022 bytes += bytes_read;
15023 break;
15024
15025 case DW_FORM_sec_offset:
15026 case DW_FORM_strp:
15027 bytes += offset_size;
15028 break;
15029
15030 case DW_FORM_block:
15031 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15032 bytes += bytes_read;
15033 break;
15034
15035 case DW_FORM_block1:
15036 bytes += 1 + read_1_byte (abfd, bytes);
15037 break;
15038 case DW_FORM_block2:
15039 bytes += 2 + read_2_bytes (abfd, bytes);
15040 break;
15041 case DW_FORM_block4:
15042 bytes += 4 + read_4_bytes (abfd, bytes);
15043 break;
15044
15045 case DW_FORM_sdata:
15046 case DW_FORM_udata:
15047 bytes = skip_leb128 (abfd, bytes);
15048 break;
15049
15050 default:
15051 {
15052 complain:
15053 complaint (&symfile_complaints,
15054 _("invalid form 0x%x in `%s'"),
15055 form,
15056 section->asection->name);
15057 return NULL;
15058 }
2e276125
JB
15059 }
15060
cf2c3c16
TT
15061 return bytes;
15062}
757a13d0 15063
cf2c3c16
TT
15064/* A helper for dwarf_decode_macros that handles skipping an unknown
15065 opcode. Returns an updated pointer to the macro data buffer; or,
15066 on error, issues a complaint and returns NULL. */
757a13d0 15067
cf2c3c16
TT
15068static gdb_byte *
15069skip_unknown_opcode (unsigned int opcode,
15070 gdb_byte **opcode_definitions,
15071 gdb_byte *mac_ptr,
15072 bfd *abfd,
15073 unsigned int offset_size,
15074 struct dwarf2_section_info *section)
15075{
15076 unsigned int bytes_read, i;
15077 unsigned long arg;
15078 gdb_byte *defn;
2e276125 15079
cf2c3c16 15080 if (opcode_definitions[opcode] == NULL)
2e276125 15081 {
cf2c3c16
TT
15082 complaint (&symfile_complaints,
15083 _("unrecognized DW_MACFINO opcode 0x%x"),
15084 opcode);
15085 return NULL;
15086 }
2e276125 15087
cf2c3c16
TT
15088 defn = opcode_definitions[opcode];
15089 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15090 defn += bytes_read;
2e276125 15091
cf2c3c16
TT
15092 for (i = 0; i < arg; ++i)
15093 {
15094 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15095 if (mac_ptr == NULL)
15096 {
15097 /* skip_form_bytes already issued the complaint. */
15098 return NULL;
15099 }
15100 }
757a13d0 15101
cf2c3c16
TT
15102 return mac_ptr;
15103}
757a13d0 15104
cf2c3c16
TT
15105/* A helper function which parses the header of a macro section.
15106 If the macro section is the extended (for now called "GNU") type,
15107 then this updates *OFFSET_SIZE. Returns a pointer to just after
15108 the header, or issues a complaint and returns NULL on error. */
757a13d0 15109
cf2c3c16
TT
15110static gdb_byte *
15111dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15112 bfd *abfd,
15113 gdb_byte *mac_ptr,
15114 unsigned int *offset_size,
15115 int section_is_gnu)
15116{
15117 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15118
cf2c3c16
TT
15119 if (section_is_gnu)
15120 {
15121 unsigned int version, flags;
757a13d0 15122
cf2c3c16
TT
15123 version = read_2_bytes (abfd, mac_ptr);
15124 if (version != 4)
15125 {
15126 complaint (&symfile_complaints,
15127 _("unrecognized version `%d' in .debug_macro section"),
15128 version);
15129 return NULL;
15130 }
15131 mac_ptr += 2;
757a13d0 15132
cf2c3c16
TT
15133 flags = read_1_byte (abfd, mac_ptr);
15134 ++mac_ptr;
15135 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15136
cf2c3c16
TT
15137 if ((flags & 2) != 0)
15138 /* We don't need the line table offset. */
15139 mac_ptr += *offset_size;
757a13d0 15140
cf2c3c16
TT
15141 /* Vendor opcode descriptions. */
15142 if ((flags & 4) != 0)
15143 {
15144 unsigned int i, count;
757a13d0 15145
cf2c3c16
TT
15146 count = read_1_byte (abfd, mac_ptr);
15147 ++mac_ptr;
15148 for (i = 0; i < count; ++i)
15149 {
15150 unsigned int opcode, bytes_read;
15151 unsigned long arg;
15152
15153 opcode = read_1_byte (abfd, mac_ptr);
15154 ++mac_ptr;
15155 opcode_definitions[opcode] = mac_ptr;
15156 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15157 mac_ptr += bytes_read;
15158 mac_ptr += arg;
15159 }
757a13d0 15160 }
cf2c3c16 15161 }
757a13d0 15162
cf2c3c16
TT
15163 return mac_ptr;
15164}
757a13d0 15165
cf2c3c16
TT
15166/* A helper for dwarf_decode_macros that handles the GNU extensions,
15167 including DW_GNU_MACINFO_transparent_include. */
15168
15169static void
15170dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15171 struct macro_source_file *current_file,
15172 struct line_header *lh, char *comp_dir,
15173 struct dwarf2_section_info *section,
15174 int section_is_gnu,
15175 unsigned int offset_size,
15176 struct objfile *objfile)
15177{
15178 enum dwarf_macro_record_type macinfo_type;
15179 int at_commandline;
15180 gdb_byte *opcode_definitions[256];
757a13d0 15181
cf2c3c16
TT
15182 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15183 &offset_size, section_is_gnu);
15184 if (mac_ptr == NULL)
15185 {
15186 /* We already issued a complaint. */
15187 return;
15188 }
757a13d0
JK
15189
15190 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15191 GDB is still reading the definitions from command line. First
15192 DW_MACINFO_start_file will need to be ignored as it was already executed
15193 to create CURRENT_FILE for the main source holding also the command line
15194 definitions. On first met DW_MACINFO_start_file this flag is reset to
15195 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15196
15197 at_commandline = 1;
15198
15199 do
15200 {
15201 /* Do we at least have room for a macinfo type byte? */
15202 if (mac_ptr >= mac_end)
15203 {
cf2c3c16 15204 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15205 break;
15206 }
15207
15208 macinfo_type = read_1_byte (abfd, mac_ptr);
15209 mac_ptr++;
15210
cf2c3c16
TT
15211 /* Note that we rely on the fact that the corresponding GNU and
15212 DWARF constants are the same. */
757a13d0
JK
15213 switch (macinfo_type)
15214 {
15215 /* A zero macinfo type indicates the end of the macro
15216 information. */
15217 case 0:
15218 break;
2e276125 15219
cf2c3c16
TT
15220 case DW_MACRO_GNU_define:
15221 case DW_MACRO_GNU_undef:
15222 case DW_MACRO_GNU_define_indirect:
15223 case DW_MACRO_GNU_undef_indirect:
2e276125 15224 {
891d2f0b 15225 unsigned int bytes_read;
2e276125
JB
15226 int line;
15227 char *body;
cf2c3c16 15228 int is_define;
2e276125 15229
cf2c3c16
TT
15230 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15231 mac_ptr += bytes_read;
15232
15233 if (macinfo_type == DW_MACRO_GNU_define
15234 || macinfo_type == DW_MACRO_GNU_undef)
15235 {
15236 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15237 mac_ptr += bytes_read;
15238 }
15239 else
15240 {
15241 LONGEST str_offset;
15242
15243 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15244 mac_ptr += offset_size;
2e276125 15245
cf2c3c16
TT
15246 body = read_indirect_string_at_offset (abfd, str_offset);
15247 }
15248
15249 is_define = (macinfo_type == DW_MACRO_GNU_define
15250 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15251 if (! current_file)
757a13d0
JK
15252 {
15253 /* DWARF violation as no main source is present. */
15254 complaint (&symfile_complaints,
15255 _("debug info with no main source gives macro %s "
15256 "on line %d: %s"),
cf2c3c16
TT
15257 is_define ? _("definition") : _("undefinition"),
15258 line, body);
757a13d0
JK
15259 break;
15260 }
3e43a32a
MS
15261 if ((line == 0 && !at_commandline)
15262 || (line != 0 && at_commandline))
4d3c2250 15263 complaint (&symfile_complaints,
757a13d0
JK
15264 _("debug info gives %s macro %s with %s line %d: %s"),
15265 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15266 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15267 line == 0 ? _("zero") : _("non-zero"), line, body);
15268
cf2c3c16 15269 if (is_define)
757a13d0 15270 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15271 else
15272 {
15273 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15274 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15275 macro_undef (current_file, line, body);
15276 }
2e276125
JB
15277 }
15278 break;
15279
cf2c3c16 15280 case DW_MACRO_GNU_start_file:
2e276125 15281 {
891d2f0b 15282 unsigned int bytes_read;
2e276125
JB
15283 int line, file;
15284
15285 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15286 mac_ptr += bytes_read;
15287 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15288 mac_ptr += bytes_read;
15289
3e43a32a
MS
15290 if ((line == 0 && !at_commandline)
15291 || (line != 0 && at_commandline))
757a13d0
JK
15292 complaint (&symfile_complaints,
15293 _("debug info gives source %d included "
15294 "from %s at %s line %d"),
15295 file, at_commandline ? _("command-line") : _("file"),
15296 line == 0 ? _("zero") : _("non-zero"), line);
15297
15298 if (at_commandline)
15299 {
cf2c3c16
TT
15300 /* This DW_MACRO_GNU_start_file was executed in the
15301 pass one. */
757a13d0
JK
15302 at_commandline = 0;
15303 }
15304 else
15305 current_file = macro_start_file (file, line,
15306 current_file, comp_dir,
cf2c3c16 15307 lh, objfile);
2e276125
JB
15308 }
15309 break;
15310
cf2c3c16 15311 case DW_MACRO_GNU_end_file:
2e276125 15312 if (! current_file)
4d3c2250 15313 complaint (&symfile_complaints,
3e43a32a
MS
15314 _("macro debug info has an unmatched "
15315 "`close_file' directive"));
2e276125
JB
15316 else
15317 {
15318 current_file = current_file->included_by;
15319 if (! current_file)
15320 {
cf2c3c16 15321 enum dwarf_macro_record_type next_type;
2e276125
JB
15322
15323 /* GCC circa March 2002 doesn't produce the zero
15324 type byte marking the end of the compilation
15325 unit. Complain if it's not there, but exit no
15326 matter what. */
15327
15328 /* Do we at least have room for a macinfo type byte? */
15329 if (mac_ptr >= mac_end)
15330 {
cf2c3c16 15331 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15332 return;
15333 }
15334
15335 /* We don't increment mac_ptr here, so this is just
15336 a look-ahead. */
15337 next_type = read_1_byte (abfd, mac_ptr);
15338 if (next_type != 0)
4d3c2250 15339 complaint (&symfile_complaints,
3e43a32a
MS
15340 _("no terminating 0-type entry for "
15341 "macros in `.debug_macinfo' section"));
2e276125
JB
15342
15343 return;
15344 }
15345 }
15346 break;
15347
cf2c3c16
TT
15348 case DW_MACRO_GNU_transparent_include:
15349 {
15350 LONGEST offset;
15351
15352 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15353 mac_ptr += offset_size;
15354
15355 dwarf_decode_macro_bytes (abfd,
15356 section->buffer + offset,
15357 mac_end, current_file,
15358 lh, comp_dir,
15359 section, section_is_gnu,
15360 offset_size, objfile);
15361 }
15362 break;
15363
2e276125 15364 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15365 if (!section_is_gnu)
15366 {
15367 unsigned int bytes_read;
15368 int constant;
2e276125 15369
cf2c3c16
TT
15370 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15371 mac_ptr += bytes_read;
15372 read_direct_string (abfd, mac_ptr, &bytes_read);
15373 mac_ptr += bytes_read;
2e276125 15374
cf2c3c16
TT
15375 /* We don't recognize any vendor extensions. */
15376 break;
15377 }
15378 /* FALLTHROUGH */
15379
15380 default:
15381 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15382 mac_ptr, abfd, offset_size,
15383 section);
15384 if (mac_ptr == NULL)
15385 return;
15386 break;
2e276125 15387 }
757a13d0 15388 } while (macinfo_type != 0);
2e276125 15389}
8e19ed76 15390
cf2c3c16
TT
15391static void
15392dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15393 char *comp_dir, bfd *abfd,
15394 struct dwarf2_cu *cu,
15395 struct dwarf2_section_info *section,
15396 int section_is_gnu)
15397{
bb5ed363 15398 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15399 gdb_byte *mac_ptr, *mac_end;
15400 struct macro_source_file *current_file = 0;
15401 enum dwarf_macro_record_type macinfo_type;
15402 unsigned int offset_size = cu->header.offset_size;
15403 gdb_byte *opcode_definitions[256];
15404
bb5ed363 15405 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15406 if (section->buffer == NULL)
15407 {
15408 complaint (&symfile_complaints, _("missing %s section"),
15409 section->asection->name);
15410 return;
15411 }
15412
15413 /* First pass: Find the name of the base filename.
15414 This filename is needed in order to process all macros whose definition
15415 (or undefinition) comes from the command line. These macros are defined
15416 before the first DW_MACINFO_start_file entry, and yet still need to be
15417 associated to the base file.
15418
15419 To determine the base file name, we scan the macro definitions until we
15420 reach the first DW_MACINFO_start_file entry. We then initialize
15421 CURRENT_FILE accordingly so that any macro definition found before the
15422 first DW_MACINFO_start_file can still be associated to the base file. */
15423
15424 mac_ptr = section->buffer + offset;
15425 mac_end = section->buffer + section->size;
15426
15427 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15428 &offset_size, section_is_gnu);
15429 if (mac_ptr == NULL)
15430 {
15431 /* We already issued a complaint. */
15432 return;
15433 }
15434
15435 do
15436 {
15437 /* Do we at least have room for a macinfo type byte? */
15438 if (mac_ptr >= mac_end)
15439 {
15440 /* Complaint is printed during the second pass as GDB will probably
15441 stop the first pass earlier upon finding
15442 DW_MACINFO_start_file. */
15443 break;
15444 }
15445
15446 macinfo_type = read_1_byte (abfd, mac_ptr);
15447 mac_ptr++;
15448
15449 /* Note that we rely on the fact that the corresponding GNU and
15450 DWARF constants are the same. */
15451 switch (macinfo_type)
15452 {
15453 /* A zero macinfo type indicates the end of the macro
15454 information. */
15455 case 0:
15456 break;
15457
15458 case DW_MACRO_GNU_define:
15459 case DW_MACRO_GNU_undef:
15460 /* Only skip the data by MAC_PTR. */
15461 {
15462 unsigned int bytes_read;
15463
15464 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15465 mac_ptr += bytes_read;
15466 read_direct_string (abfd, mac_ptr, &bytes_read);
15467 mac_ptr += bytes_read;
15468 }
15469 break;
15470
15471 case DW_MACRO_GNU_start_file:
15472 {
15473 unsigned int bytes_read;
15474 int line, file;
15475
15476 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15477 mac_ptr += bytes_read;
15478 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15479 mac_ptr += bytes_read;
15480
15481 current_file = macro_start_file (file, line, current_file,
bb5ed363 15482 comp_dir, lh, objfile);
cf2c3c16
TT
15483 }
15484 break;
15485
15486 case DW_MACRO_GNU_end_file:
15487 /* No data to skip by MAC_PTR. */
15488 break;
15489
15490 case DW_MACRO_GNU_define_indirect:
15491 case DW_MACRO_GNU_undef_indirect:
15492 {
15493 unsigned int bytes_read;
15494
15495 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15496 mac_ptr += bytes_read;
15497 mac_ptr += offset_size;
15498 }
15499 break;
15500
15501 case DW_MACRO_GNU_transparent_include:
15502 /* Note that, according to the spec, a transparent include
15503 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15504 skip this opcode. */
15505 mac_ptr += offset_size;
15506 break;
15507
15508 case DW_MACINFO_vendor_ext:
15509 /* Only skip the data by MAC_PTR. */
15510 if (!section_is_gnu)
15511 {
15512 unsigned int bytes_read;
15513
15514 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15515 mac_ptr += bytes_read;
15516 read_direct_string (abfd, mac_ptr, &bytes_read);
15517 mac_ptr += bytes_read;
15518 }
15519 /* FALLTHROUGH */
15520
15521 default:
15522 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15523 mac_ptr, abfd, offset_size,
15524 section);
15525 if (mac_ptr == NULL)
15526 return;
15527 break;
15528 }
15529 } while (macinfo_type != 0 && current_file == NULL);
15530
15531 /* Second pass: Process all entries.
15532
15533 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15534 command-line macro definitions/undefinitions. This flag is unset when we
15535 reach the first DW_MACINFO_start_file entry. */
15536
15537 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15538 current_file, lh, comp_dir, section, section_is_gnu,
bb5ed363 15539 offset_size, objfile);
cf2c3c16
TT
15540}
15541
8e19ed76 15542/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15543 if so return true else false. */
8e19ed76
PS
15544static int
15545attr_form_is_block (struct attribute *attr)
15546{
15547 return (attr == NULL ? 0 :
15548 attr->form == DW_FORM_block1
15549 || attr->form == DW_FORM_block2
15550 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15551 || attr->form == DW_FORM_block
15552 || attr->form == DW_FORM_exprloc);
8e19ed76 15553}
4c2df51b 15554
c6a0999f
JB
15555/* Return non-zero if ATTR's value is a section offset --- classes
15556 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15557 You may use DW_UNSND (attr) to retrieve such offsets.
15558
15559 Section 7.5.4, "Attribute Encodings", explains that no attribute
15560 may have a value that belongs to more than one of these classes; it
15561 would be ambiguous if we did, because we use the same forms for all
15562 of them. */
3690dd37
JB
15563static int
15564attr_form_is_section_offset (struct attribute *attr)
15565{
15566 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15567 || attr->form == DW_FORM_data8
15568 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15569}
15570
15571
15572/* Return non-zero if ATTR's value falls in the 'constant' class, or
15573 zero otherwise. When this function returns true, you can apply
15574 dwarf2_get_attr_constant_value to it.
15575
15576 However, note that for some attributes you must check
15577 attr_form_is_section_offset before using this test. DW_FORM_data4
15578 and DW_FORM_data8 are members of both the constant class, and of
15579 the classes that contain offsets into other debug sections
15580 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15581 that, if an attribute's can be either a constant or one of the
15582 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15583 taken as section offsets, not constants. */
15584static int
15585attr_form_is_constant (struct attribute *attr)
15586{
15587 switch (attr->form)
15588 {
15589 case DW_FORM_sdata:
15590 case DW_FORM_udata:
15591 case DW_FORM_data1:
15592 case DW_FORM_data2:
15593 case DW_FORM_data4:
15594 case DW_FORM_data8:
15595 return 1;
15596 default:
15597 return 0;
15598 }
15599}
15600
8cf6f0b1
TT
15601/* A helper function that fills in a dwarf2_loclist_baton. */
15602
15603static void
15604fill_in_loclist_baton (struct dwarf2_cu *cu,
15605 struct dwarf2_loclist_baton *baton,
15606 struct attribute *attr)
15607{
15608 dwarf2_read_section (dwarf2_per_objfile->objfile,
15609 &dwarf2_per_objfile->loc);
15610
15611 baton->per_cu = cu->per_cu;
15612 gdb_assert (baton->per_cu);
15613 /* We don't know how long the location list is, but make sure we
15614 don't run off the edge of the section. */
15615 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15616 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15617 baton->base_address = cu->base_address;
15618}
15619
4c2df51b
DJ
15620static void
15621dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15622 struct dwarf2_cu *cu)
4c2df51b 15623{
bb5ed363
DE
15624 struct objfile *objfile = dwarf2_per_objfile->objfile;
15625
3690dd37 15626 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15627 /* ".debug_loc" may not exist at all, or the offset may be outside
15628 the section. If so, fall through to the complaint in the
15629 other branch. */
bb5ed363 15630 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15631 &dwarf2_per_objfile->loc))
4c2df51b 15632 {
0d53c4c4 15633 struct dwarf2_loclist_baton *baton;
4c2df51b 15634
bb5ed363 15635 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15636 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15637
8cf6f0b1 15638 fill_in_loclist_baton (cu, baton, attr);
be391dca 15639
d00adf39 15640 if (cu->base_known == 0)
0d53c4c4 15641 complaint (&symfile_complaints,
3e43a32a
MS
15642 _("Location list used without "
15643 "specifying the CU base address."));
4c2df51b 15644
768a979c 15645 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15646 SYMBOL_LOCATION_BATON (sym) = baton;
15647 }
15648 else
15649 {
15650 struct dwarf2_locexpr_baton *baton;
15651
bb5ed363 15652 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15653 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15654 baton->per_cu = cu->per_cu;
15655 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15656
15657 if (attr_form_is_block (attr))
15658 {
15659 /* Note that we're just copying the block's data pointer
15660 here, not the actual data. We're still pointing into the
6502dd73
DJ
15661 info_buffer for SYM's objfile; right now we never release
15662 that buffer, but when we do clean up properly this may
15663 need to change. */
0d53c4c4
DJ
15664 baton->size = DW_BLOCK (attr)->size;
15665 baton->data = DW_BLOCK (attr)->data;
15666 }
15667 else
15668 {
15669 dwarf2_invalid_attrib_class_complaint ("location description",
15670 SYMBOL_NATURAL_NAME (sym));
15671 baton->size = 0;
0d53c4c4 15672 }
6e70227d 15673
768a979c 15674 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15675 SYMBOL_LOCATION_BATON (sym) = baton;
15676 }
4c2df51b 15677}
6502dd73 15678
9aa1f1e3
TT
15679/* Return the OBJFILE associated with the compilation unit CU. If CU
15680 came from a separate debuginfo file, then the master objfile is
15681 returned. */
ae0d2f24
UW
15682
15683struct objfile *
15684dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15685{
9291a0cd 15686 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15687
15688 /* Return the master objfile, so that we can report and look up the
15689 correct file containing this variable. */
15690 if (objfile->separate_debug_objfile_backlink)
15691 objfile = objfile->separate_debug_objfile_backlink;
15692
15693 return objfile;
15694}
15695
96408a79
SA
15696/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15697 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15698 CU_HEADERP first. */
15699
15700static const struct comp_unit_head *
15701per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15702 struct dwarf2_per_cu_data *per_cu)
15703{
15704 struct objfile *objfile;
15705 struct dwarf2_per_objfile *per_objfile;
15706 gdb_byte *info_ptr;
15707
15708 if (per_cu->cu)
15709 return &per_cu->cu->header;
15710
15711 objfile = per_cu->objfile;
15712 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15713 info_ptr = per_objfile->info.buffer + per_cu->offset;
15714
15715 memset (cu_headerp, 0, sizeof (*cu_headerp));
15716 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15717
15718 return cu_headerp;
15719}
15720
ae0d2f24
UW
15721/* Return the address size given in the compilation unit header for CU. */
15722
98714339 15723int
ae0d2f24
UW
15724dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15725{
96408a79
SA
15726 struct comp_unit_head cu_header_local;
15727 const struct comp_unit_head *cu_headerp;
c471e790 15728
96408a79
SA
15729 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15730
15731 return cu_headerp->addr_size;
ae0d2f24
UW
15732}
15733
9eae7c52
TT
15734/* Return the offset size given in the compilation unit header for CU. */
15735
15736int
15737dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15738{
96408a79
SA
15739 struct comp_unit_head cu_header_local;
15740 const struct comp_unit_head *cu_headerp;
9c6c53f7 15741
96408a79
SA
15742 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15743
15744 return cu_headerp->offset_size;
15745}
15746
15747/* See its dwarf2loc.h declaration. */
15748
15749int
15750dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15751{
15752 struct comp_unit_head cu_header_local;
15753 const struct comp_unit_head *cu_headerp;
15754
15755 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15756
15757 if (cu_headerp->version == 2)
15758 return cu_headerp->addr_size;
15759 else
15760 return cu_headerp->offset_size;
181cebd4
JK
15761}
15762
9aa1f1e3
TT
15763/* Return the text offset of the CU. The returned offset comes from
15764 this CU's objfile. If this objfile came from a separate debuginfo
15765 file, then the offset may be different from the corresponding
15766 offset in the parent objfile. */
15767
15768CORE_ADDR
15769dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15770{
bb3fa9d0 15771 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15772
15773 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15774}
15775
348e048f
DE
15776/* Locate the .debug_info compilation unit from CU's objfile which contains
15777 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15778
15779static struct dwarf2_per_cu_data *
c764a876 15780dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15781 struct objfile *objfile)
15782{
15783 struct dwarf2_per_cu_data *this_cu;
15784 int low, high;
15785
ae038cb0
DJ
15786 low = 0;
15787 high = dwarf2_per_objfile->n_comp_units - 1;
15788 while (high > low)
15789 {
15790 int mid = low + (high - low) / 2;
9a619af0 15791
ae038cb0
DJ
15792 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15793 high = mid;
15794 else
15795 low = mid + 1;
15796 }
15797 gdb_assert (low == high);
15798 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15799 {
10b3939b 15800 if (low == 0)
8a3fe4f8
AC
15801 error (_("Dwarf Error: could not find partial DIE containing "
15802 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15803 (long) offset, bfd_get_filename (objfile->obfd));
15804
ae038cb0
DJ
15805 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15806 return dwarf2_per_objfile->all_comp_units[low-1];
15807 }
15808 else
15809 {
15810 this_cu = dwarf2_per_objfile->all_comp_units[low];
15811 if (low == dwarf2_per_objfile->n_comp_units - 1
15812 && offset >= this_cu->offset + this_cu->length)
c764a876 15813 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15814 gdb_assert (offset < this_cu->offset + this_cu->length);
15815 return this_cu;
15816 }
15817}
15818
10b3939b
DJ
15819/* Locate the compilation unit from OBJFILE which is located at exactly
15820 OFFSET. Raises an error on failure. */
15821
ae038cb0 15822static struct dwarf2_per_cu_data *
c764a876 15823dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15824{
15825 struct dwarf2_per_cu_data *this_cu;
9a619af0 15826
ae038cb0
DJ
15827 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15828 if (this_cu->offset != offset)
c764a876 15829 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15830 return this_cu;
15831}
15832
23745b47 15833/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15834
9816fde3 15835static void
23745b47 15836init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15837{
9816fde3 15838 memset (cu, 0, sizeof (*cu));
23745b47
DE
15839 per_cu->cu = cu;
15840 cu->per_cu = per_cu;
15841 cu->objfile = per_cu->objfile;
93311388 15842 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15843}
15844
15845/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15846
15847static void
15848prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15849{
15850 struct attribute *attr;
15851
15852 /* Set the language we're debugging. */
15853 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15854 if (attr)
15855 set_cu_language (DW_UNSND (attr), cu);
15856 else
9cded63f
TT
15857 {
15858 cu->language = language_minimal;
15859 cu->language_defn = language_def (cu->language);
15860 }
93311388
DE
15861}
15862
ae038cb0
DJ
15863/* Release one cached compilation unit, CU. We unlink it from the tree
15864 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15865 the caller is responsible for that.
15866 NOTE: DATA is a void * because this function is also used as a
15867 cleanup routine. */
ae038cb0
DJ
15868
15869static void
68dc6402 15870free_heap_comp_unit (void *data)
ae038cb0
DJ
15871{
15872 struct dwarf2_cu *cu = data;
15873
23745b47
DE
15874 gdb_assert (cu->per_cu != NULL);
15875 cu->per_cu->cu = NULL;
ae038cb0
DJ
15876 cu->per_cu = NULL;
15877
15878 obstack_free (&cu->comp_unit_obstack, NULL);
15879
15880 xfree (cu);
15881}
15882
72bf9492 15883/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15884 when we're finished with it. We can't free the pointer itself, but be
15885 sure to unlink it from the cache. Also release any associated storage
15886 and perform cache maintenance.
72bf9492
DJ
15887
15888 Only used during partial symbol parsing. */
15889
15890static void
15891free_stack_comp_unit (void *data)
15892{
15893 struct dwarf2_cu *cu = data;
15894
23745b47
DE
15895 gdb_assert (cu->per_cu != NULL);
15896 cu->per_cu->cu = NULL;
15897 cu->per_cu = NULL;
15898
72bf9492
DJ
15899 obstack_free (&cu->comp_unit_obstack, NULL);
15900 cu->partial_dies = NULL;
ae038cb0 15901
23745b47
DE
15902 /* The previous code only did this if per_cu != NULL.
15903 But that would always succeed, so now we just unconditionally do
15904 the aging. This seems like the wrong place to do such aging,
15905 but cleaning that up is left for later. */
15906 age_cached_comp_units ();
ae038cb0
DJ
15907}
15908
15909/* Free all cached compilation units. */
15910
15911static void
15912free_cached_comp_units (void *data)
15913{
15914 struct dwarf2_per_cu_data *per_cu, **last_chain;
15915
15916 per_cu = dwarf2_per_objfile->read_in_chain;
15917 last_chain = &dwarf2_per_objfile->read_in_chain;
15918 while (per_cu != NULL)
15919 {
15920 struct dwarf2_per_cu_data *next_cu;
15921
15922 next_cu = per_cu->cu->read_in_chain;
15923
68dc6402 15924 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15925 *last_chain = next_cu;
15926
15927 per_cu = next_cu;
15928 }
15929}
15930
15931/* Increase the age counter on each cached compilation unit, and free
15932 any that are too old. */
15933
15934static void
15935age_cached_comp_units (void)
15936{
15937 struct dwarf2_per_cu_data *per_cu, **last_chain;
15938
15939 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15940 per_cu = dwarf2_per_objfile->read_in_chain;
15941 while (per_cu != NULL)
15942 {
15943 per_cu->cu->last_used ++;
15944 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15945 dwarf2_mark (per_cu->cu);
15946 per_cu = per_cu->cu->read_in_chain;
15947 }
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
15957 if (!per_cu->cu->mark)
15958 {
68dc6402 15959 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15960 *last_chain = next_cu;
15961 }
15962 else
15963 last_chain = &per_cu->cu->read_in_chain;
15964
15965 per_cu = next_cu;
15966 }
15967}
15968
15969/* Remove a single compilation unit from the cache. */
15970
15971static void
15972free_one_cached_comp_unit (void *target_cu)
15973{
15974 struct dwarf2_per_cu_data *per_cu, **last_chain;
15975
15976 per_cu = dwarf2_per_objfile->read_in_chain;
15977 last_chain = &dwarf2_per_objfile->read_in_chain;
15978 while (per_cu != NULL)
15979 {
15980 struct dwarf2_per_cu_data *next_cu;
15981
15982 next_cu = per_cu->cu->read_in_chain;
15983
15984 if (per_cu->cu == target_cu)
15985 {
68dc6402 15986 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15987 *last_chain = next_cu;
15988 break;
15989 }
15990 else
15991 last_chain = &per_cu->cu->read_in_chain;
15992
15993 per_cu = next_cu;
15994 }
15995}
15996
fe3e1990
DJ
15997/* Release all extra memory associated with OBJFILE. */
15998
15999void
16000dwarf2_free_objfile (struct objfile *objfile)
16001{
16002 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16003
16004 if (dwarf2_per_objfile == NULL)
16005 return;
16006
16007 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16008 free_cached_comp_units (NULL);
16009
7b9f3c50
DE
16010 if (dwarf2_per_objfile->quick_file_names_table)
16011 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16012
fe3e1990
DJ
16013 /* Everything else should be on the objfile obstack. */
16014}
16015
1c379e20
DJ
16016/* A pair of DIE offset and GDB type pointer. We store these
16017 in a hash table separate from the DIEs, and preserve them
16018 when the DIEs are flushed out of cache. */
16019
16020struct dwarf2_offset_and_type
16021{
16022 unsigned int offset;
16023 struct type *type;
16024};
16025
16026/* Hash function for a dwarf2_offset_and_type. */
16027
16028static hashval_t
16029offset_and_type_hash (const void *item)
16030{
16031 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16032
1c379e20
DJ
16033 return ofs->offset;
16034}
16035
16036/* Equality function for a dwarf2_offset_and_type. */
16037
16038static int
16039offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16040{
16041 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16042 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16043
1c379e20
DJ
16044 return ofs_lhs->offset == ofs_rhs->offset;
16045}
16046
16047/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16048 table if necessary. For convenience, return TYPE.
16049
16050 The DIEs reading must have careful ordering to:
16051 * Not cause infite loops trying to read in DIEs as a prerequisite for
16052 reading current DIE.
16053 * Not trying to dereference contents of still incompletely read in types
16054 while reading in other DIEs.
16055 * Enable referencing still incompletely read in types just by a pointer to
16056 the type without accessing its fields.
16057
16058 Therefore caller should follow these rules:
16059 * Try to fetch any prerequisite types we may need to build this DIE type
16060 before building the type and calling set_die_type.
e71ec853 16061 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16062 possible before fetching more types to complete the current type.
16063 * Make the type as complete as possible before fetching more types. */
1c379e20 16064
f792889a 16065static struct type *
1c379e20
DJ
16066set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16067{
16068 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16069 struct objfile *objfile = cu->objfile;
16070 htab_t *type_hash_ptr;
1c379e20 16071
b4ba55a1
JB
16072 /* For Ada types, make sure that the gnat-specific data is always
16073 initialized (if not already set). There are a few types where
16074 we should not be doing so, because the type-specific area is
16075 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16076 where the type-specific area is used to store the floatformat).
16077 But this is not a problem, because the gnat-specific information
16078 is actually not needed for these types. */
16079 if (need_gnat_info (cu)
16080 && TYPE_CODE (type) != TYPE_CODE_FUNC
16081 && TYPE_CODE (type) != TYPE_CODE_FLT
16082 && !HAVE_GNAT_AUX_INFO (type))
16083 INIT_GNAT_SPECIFIC (type);
16084
b0df02fd 16085 if (cu->per_cu->debug_types_section)
673bfd45
DE
16086 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16087 else
16088 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16089
16090 if (*type_hash_ptr == NULL)
f792889a 16091 {
673bfd45
DE
16092 *type_hash_ptr
16093 = htab_create_alloc_ex (127,
f792889a
DJ
16094 offset_and_type_hash,
16095 offset_and_type_eq,
16096 NULL,
673bfd45 16097 &objfile->objfile_obstack,
f792889a
DJ
16098 hashtab_obstack_allocate,
16099 dummy_obstack_deallocate);
f792889a 16100 }
1c379e20
DJ
16101
16102 ofs.offset = die->offset;
16103 ofs.type = type;
16104 slot = (struct dwarf2_offset_and_type **)
673bfd45 16105 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16106 if (*slot)
16107 complaint (&symfile_complaints,
16108 _("A problem internal to GDB: DIE 0x%x has type already set"),
16109 die->offset);
673bfd45 16110 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16111 **slot = ofs;
f792889a 16112 return type;
1c379e20
DJ
16113}
16114
673bfd45
DE
16115/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16116 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16117
16118static struct type *
673bfd45
DE
16119get_die_type_at_offset (unsigned int offset,
16120 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16121{
16122 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16123 htab_t type_hash;
f792889a 16124
b0df02fd 16125 if (per_cu->debug_types_section)
673bfd45
DE
16126 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16127 else
16128 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16129 if (type_hash == NULL)
16130 return NULL;
1c379e20 16131
673bfd45 16132 ofs.offset = offset;
1c379e20
DJ
16133 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16134 if (slot)
16135 return slot->type;
16136 else
16137 return NULL;
16138}
16139
673bfd45
DE
16140/* Look up the type for DIE in the appropriate type_hash table,
16141 or return NULL if DIE does not have a saved type. */
16142
16143static struct type *
16144get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16145{
16146 return get_die_type_at_offset (die->offset, cu->per_cu);
16147}
16148
10b3939b
DJ
16149/* Add a dependence relationship from CU to REF_PER_CU. */
16150
16151static void
16152dwarf2_add_dependence (struct dwarf2_cu *cu,
16153 struct dwarf2_per_cu_data *ref_per_cu)
16154{
16155 void **slot;
16156
16157 if (cu->dependencies == NULL)
16158 cu->dependencies
16159 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16160 NULL, &cu->comp_unit_obstack,
16161 hashtab_obstack_allocate,
16162 dummy_obstack_deallocate);
16163
16164 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16165 if (*slot == NULL)
16166 *slot = ref_per_cu;
16167}
1c379e20 16168
f504f079
DE
16169/* Subroutine of dwarf2_mark to pass to htab_traverse.
16170 Set the mark field in every compilation unit in the
ae038cb0
DJ
16171 cache that we must keep because we are keeping CU. */
16172
10b3939b
DJ
16173static int
16174dwarf2_mark_helper (void **slot, void *data)
16175{
16176 struct dwarf2_per_cu_data *per_cu;
16177
16178 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16179
16180 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16181 reading of the chain. As such dependencies remain valid it is not much
16182 useful to track and undo them during QUIT cleanups. */
16183 if (per_cu->cu == NULL)
16184 return 1;
16185
10b3939b
DJ
16186 if (per_cu->cu->mark)
16187 return 1;
16188 per_cu->cu->mark = 1;
16189
16190 if (per_cu->cu->dependencies != NULL)
16191 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16192
16193 return 1;
16194}
16195
f504f079
DE
16196/* Set the mark field in CU and in every other compilation unit in the
16197 cache that we must keep because we are keeping CU. */
16198
ae038cb0
DJ
16199static void
16200dwarf2_mark (struct dwarf2_cu *cu)
16201{
16202 if (cu->mark)
16203 return;
16204 cu->mark = 1;
10b3939b
DJ
16205 if (cu->dependencies != NULL)
16206 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16207}
16208
16209static void
16210dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16211{
16212 while (per_cu)
16213 {
16214 per_cu->cu->mark = 0;
16215 per_cu = per_cu->cu->read_in_chain;
16216 }
72bf9492
DJ
16217}
16218
72bf9492
DJ
16219/* Trivial hash function for partial_die_info: the hash value of a DIE
16220 is its offset in .debug_info for this objfile. */
16221
16222static hashval_t
16223partial_die_hash (const void *item)
16224{
16225 const struct partial_die_info *part_die = item;
9a619af0 16226
72bf9492
DJ
16227 return part_die->offset;
16228}
16229
16230/* Trivial comparison function for partial_die_info structures: two DIEs
16231 are equal if they have the same offset. */
16232
16233static int
16234partial_die_eq (const void *item_lhs, const void *item_rhs)
16235{
16236 const struct partial_die_info *part_die_lhs = item_lhs;
16237 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16238
72bf9492
DJ
16239 return part_die_lhs->offset == part_die_rhs->offset;
16240}
16241
ae038cb0
DJ
16242static struct cmd_list_element *set_dwarf2_cmdlist;
16243static struct cmd_list_element *show_dwarf2_cmdlist;
16244
16245static void
16246set_dwarf2_cmd (char *args, int from_tty)
16247{
16248 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16249}
16250
16251static void
16252show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16253{
ae038cb0
DJ
16254 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16255}
16256
dce234bc
PP
16257/* If section described by INFO was mmapped, munmap it now. */
16258
16259static void
16260munmap_section_buffer (struct dwarf2_section_info *info)
16261{
b315ab21 16262 if (info->map_addr != NULL)
dce234bc
PP
16263 {
16264#ifdef HAVE_MMAP
b315ab21 16265 int res;
9a619af0 16266
b315ab21
TG
16267 res = munmap (info->map_addr, info->map_len);
16268 gdb_assert (res == 0);
dce234bc
PP
16269#else
16270 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16271 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16272#endif
16273 }
16274}
16275
16276/* munmap debug sections for OBJFILE, if necessary. */
16277
16278static void
c1bd65d0 16279dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16280{
16281 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16282 int ix;
16283 struct dwarf2_section_info *section;
9a619af0 16284
16be1145
DE
16285 /* This is sorted according to the order they're defined in to make it easier
16286 to keep in sync. */
dce234bc
PP
16287 munmap_section_buffer (&data->info);
16288 munmap_section_buffer (&data->abbrev);
16289 munmap_section_buffer (&data->line);
16be1145 16290 munmap_section_buffer (&data->loc);
dce234bc 16291 munmap_section_buffer (&data->macinfo);
cf2c3c16 16292 munmap_section_buffer (&data->macro);
16be1145 16293 munmap_section_buffer (&data->str);
dce234bc 16294 munmap_section_buffer (&data->ranges);
dce234bc
PP
16295 munmap_section_buffer (&data->frame);
16296 munmap_section_buffer (&data->eh_frame);
9291a0cd 16297 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16298
16299 for (ix = 0;
16300 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16301 ++ix)
16302 munmap_section_buffer (section);
16303
16304 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16305}
16306
16307\f
ae2de4f8 16308/* The "save gdb-index" command. */
9291a0cd
TT
16309
16310/* The contents of the hash table we create when building the string
16311 table. */
16312struct strtab_entry
16313{
16314 offset_type offset;
16315 const char *str;
16316};
16317
559a7a62
JK
16318/* Hash function for a strtab_entry.
16319
16320 Function is used only during write_hash_table so no index format backward
16321 compatibility is needed. */
b89be57b 16322
9291a0cd
TT
16323static hashval_t
16324hash_strtab_entry (const void *e)
16325{
16326 const struct strtab_entry *entry = e;
559a7a62 16327 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16328}
16329
16330/* Equality function for a strtab_entry. */
b89be57b 16331
9291a0cd
TT
16332static int
16333eq_strtab_entry (const void *a, const void *b)
16334{
16335 const struct strtab_entry *ea = a;
16336 const struct strtab_entry *eb = b;
16337 return !strcmp (ea->str, eb->str);
16338}
16339
16340/* Create a strtab_entry hash table. */
b89be57b 16341
9291a0cd
TT
16342static htab_t
16343create_strtab (void)
16344{
16345 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16346 xfree, xcalloc, xfree);
16347}
16348
16349/* Add a string to the constant pool. Return the string's offset in
16350 host order. */
b89be57b 16351
9291a0cd
TT
16352static offset_type
16353add_string (htab_t table, struct obstack *cpool, const char *str)
16354{
16355 void **slot;
16356 struct strtab_entry entry;
16357 struct strtab_entry *result;
16358
16359 entry.str = str;
16360 slot = htab_find_slot (table, &entry, INSERT);
16361 if (*slot)
16362 result = *slot;
16363 else
16364 {
16365 result = XNEW (struct strtab_entry);
16366 result->offset = obstack_object_size (cpool);
16367 result->str = str;
16368 obstack_grow_str0 (cpool, str);
16369 *slot = result;
16370 }
16371 return result->offset;
16372}
16373
16374/* An entry in the symbol table. */
16375struct symtab_index_entry
16376{
16377 /* The name of the symbol. */
16378 const char *name;
16379 /* The offset of the name in the constant pool. */
16380 offset_type index_offset;
16381 /* A sorted vector of the indices of all the CUs that hold an object
16382 of this name. */
16383 VEC (offset_type) *cu_indices;
16384};
16385
16386/* The symbol table. This is a power-of-2-sized hash table. */
16387struct mapped_symtab
16388{
16389 offset_type n_elements;
16390 offset_type size;
16391 struct symtab_index_entry **data;
16392};
16393
16394/* Hash function for a symtab_index_entry. */
b89be57b 16395
9291a0cd
TT
16396static hashval_t
16397hash_symtab_entry (const void *e)
16398{
16399 const struct symtab_index_entry *entry = e;
16400 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16401 sizeof (offset_type) * VEC_length (offset_type,
16402 entry->cu_indices),
16403 0);
16404}
16405
16406/* Equality function for a symtab_index_entry. */
b89be57b 16407
9291a0cd
TT
16408static int
16409eq_symtab_entry (const void *a, const void *b)
16410{
16411 const struct symtab_index_entry *ea = a;
16412 const struct symtab_index_entry *eb = b;
16413 int len = VEC_length (offset_type, ea->cu_indices);
16414 if (len != VEC_length (offset_type, eb->cu_indices))
16415 return 0;
16416 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16417 VEC_address (offset_type, eb->cu_indices),
16418 sizeof (offset_type) * len);
16419}
16420
16421/* Destroy a symtab_index_entry. */
b89be57b 16422
9291a0cd
TT
16423static void
16424delete_symtab_entry (void *p)
16425{
16426 struct symtab_index_entry *entry = p;
16427 VEC_free (offset_type, entry->cu_indices);
16428 xfree (entry);
16429}
16430
16431/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16432
9291a0cd 16433static htab_t
3876f04e 16434create_symbol_hash_table (void)
9291a0cd
TT
16435{
16436 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16437 delete_symtab_entry, xcalloc, xfree);
16438}
16439
16440/* Create a new mapped symtab object. */
b89be57b 16441
9291a0cd
TT
16442static struct mapped_symtab *
16443create_mapped_symtab (void)
16444{
16445 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16446 symtab->n_elements = 0;
16447 symtab->size = 1024;
16448 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16449 return symtab;
16450}
16451
16452/* Destroy a mapped_symtab. */
b89be57b 16453
9291a0cd
TT
16454static void
16455cleanup_mapped_symtab (void *p)
16456{
16457 struct mapped_symtab *symtab = p;
16458 /* The contents of the array are freed when the other hash table is
16459 destroyed. */
16460 xfree (symtab->data);
16461 xfree (symtab);
16462}
16463
16464/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16465 the slot.
16466
16467 Function is used only during write_hash_table so no index format backward
16468 compatibility is needed. */
b89be57b 16469
9291a0cd
TT
16470static struct symtab_index_entry **
16471find_slot (struct mapped_symtab *symtab, const char *name)
16472{
559a7a62 16473 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16474
16475 index = hash & (symtab->size - 1);
16476 step = ((hash * 17) & (symtab->size - 1)) | 1;
16477
16478 for (;;)
16479 {
16480 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16481 return &symtab->data[index];
16482 index = (index + step) & (symtab->size - 1);
16483 }
16484}
16485
16486/* Expand SYMTAB's hash table. */
b89be57b 16487
9291a0cd
TT
16488static void
16489hash_expand (struct mapped_symtab *symtab)
16490{
16491 offset_type old_size = symtab->size;
16492 offset_type i;
16493 struct symtab_index_entry **old_entries = symtab->data;
16494
16495 symtab->size *= 2;
16496 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16497
16498 for (i = 0; i < old_size; ++i)
16499 {
16500 if (old_entries[i])
16501 {
16502 struct symtab_index_entry **slot = find_slot (symtab,
16503 old_entries[i]->name);
16504 *slot = old_entries[i];
16505 }
16506 }
16507
16508 xfree (old_entries);
16509}
16510
16511/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16512 is the index of the CU in which the symbol appears. */
b89be57b 16513
9291a0cd
TT
16514static void
16515add_index_entry (struct mapped_symtab *symtab, const char *name,
16516 offset_type cu_index)
16517{
16518 struct symtab_index_entry **slot;
16519
16520 ++symtab->n_elements;
16521 if (4 * symtab->n_elements / 3 >= symtab->size)
16522 hash_expand (symtab);
16523
16524 slot = find_slot (symtab, name);
16525 if (!*slot)
16526 {
16527 *slot = XNEW (struct symtab_index_entry);
16528 (*slot)->name = name;
16529 (*slot)->cu_indices = NULL;
16530 }
16531 /* Don't push an index twice. Due to how we add entries we only
16532 have to check the last one. */
16533 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16534 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16535 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16536}
16537
16538/* Add a vector of indices to the constant pool. */
b89be57b 16539
9291a0cd 16540static offset_type
3876f04e 16541add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16542 struct symtab_index_entry *entry)
16543{
16544 void **slot;
16545
3876f04e 16546 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16547 if (!*slot)
16548 {
16549 offset_type len = VEC_length (offset_type, entry->cu_indices);
16550 offset_type val = MAYBE_SWAP (len);
16551 offset_type iter;
16552 int i;
16553
16554 *slot = entry;
16555 entry->index_offset = obstack_object_size (cpool);
16556
16557 obstack_grow (cpool, &val, sizeof (val));
16558 for (i = 0;
16559 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16560 ++i)
16561 {
16562 val = MAYBE_SWAP (iter);
16563 obstack_grow (cpool, &val, sizeof (val));
16564 }
16565 }
16566 else
16567 {
16568 struct symtab_index_entry *old_entry = *slot;
16569 entry->index_offset = old_entry->index_offset;
16570 entry = old_entry;
16571 }
16572 return entry->index_offset;
16573}
16574
16575/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16576 constant pool entries going into the obstack CPOOL. */
b89be57b 16577
9291a0cd
TT
16578static void
16579write_hash_table (struct mapped_symtab *symtab,
16580 struct obstack *output, struct obstack *cpool)
16581{
16582 offset_type i;
3876f04e 16583 htab_t symbol_hash_table;
9291a0cd
TT
16584 htab_t str_table;
16585
3876f04e 16586 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16587 str_table = create_strtab ();
3876f04e 16588
9291a0cd
TT
16589 /* We add all the index vectors to the constant pool first, to
16590 ensure alignment is ok. */
16591 for (i = 0; i < symtab->size; ++i)
16592 {
16593 if (symtab->data[i])
3876f04e 16594 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16595 }
16596
16597 /* Now write out the hash table. */
16598 for (i = 0; i < symtab->size; ++i)
16599 {
16600 offset_type str_off, vec_off;
16601
16602 if (symtab->data[i])
16603 {
16604 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16605 vec_off = symtab->data[i]->index_offset;
16606 }
16607 else
16608 {
16609 /* While 0 is a valid constant pool index, it is not valid
16610 to have 0 for both offsets. */
16611 str_off = 0;
16612 vec_off = 0;
16613 }
16614
16615 str_off = MAYBE_SWAP (str_off);
16616 vec_off = MAYBE_SWAP (vec_off);
16617
16618 obstack_grow (output, &str_off, sizeof (str_off));
16619 obstack_grow (output, &vec_off, sizeof (vec_off));
16620 }
16621
16622 htab_delete (str_table);
3876f04e 16623 htab_delete (symbol_hash_table);
9291a0cd
TT
16624}
16625
0a5429f6
DE
16626/* Struct to map psymtab to CU index in the index file. */
16627struct psymtab_cu_index_map
16628{
16629 struct partial_symtab *psymtab;
16630 unsigned int cu_index;
16631};
16632
16633static hashval_t
16634hash_psymtab_cu_index (const void *item)
16635{
16636 const struct psymtab_cu_index_map *map = item;
16637
16638 return htab_hash_pointer (map->psymtab);
16639}
16640
16641static int
16642eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16643{
16644 const struct psymtab_cu_index_map *lhs = item_lhs;
16645 const struct psymtab_cu_index_map *rhs = item_rhs;
16646
16647 return lhs->psymtab == rhs->psymtab;
16648}
16649
16650/* Helper struct for building the address table. */
16651struct addrmap_index_data
16652{
16653 struct objfile *objfile;
16654 struct obstack *addr_obstack;
16655 htab_t cu_index_htab;
16656
16657 /* Non-zero if the previous_* fields are valid.
16658 We can't write an entry until we see the next entry (since it is only then
16659 that we know the end of the entry). */
16660 int previous_valid;
16661 /* Index of the CU in the table of all CUs in the index file. */
16662 unsigned int previous_cu_index;
0963b4bd 16663 /* Start address of the CU. */
0a5429f6
DE
16664 CORE_ADDR previous_cu_start;
16665};
16666
16667/* Write an address entry to OBSTACK. */
b89be57b 16668
9291a0cd 16669static void
0a5429f6
DE
16670add_address_entry (struct objfile *objfile, struct obstack *obstack,
16671 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16672{
0a5429f6 16673 offset_type cu_index_to_write;
9291a0cd
TT
16674 char addr[8];
16675 CORE_ADDR baseaddr;
16676
16677 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16678
0a5429f6
DE
16679 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16680 obstack_grow (obstack, addr, 8);
16681 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16682 obstack_grow (obstack, addr, 8);
16683 cu_index_to_write = MAYBE_SWAP (cu_index);
16684 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16685}
16686
16687/* Worker function for traversing an addrmap to build the address table. */
16688
16689static int
16690add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16691{
16692 struct addrmap_index_data *data = datap;
16693 struct partial_symtab *pst = obj;
16694 offset_type cu_index;
16695 void **slot;
16696
16697 if (data->previous_valid)
16698 add_address_entry (data->objfile, data->addr_obstack,
16699 data->previous_cu_start, start_addr,
16700 data->previous_cu_index);
16701
16702 data->previous_cu_start = start_addr;
16703 if (pst != NULL)
16704 {
16705 struct psymtab_cu_index_map find_map, *map;
16706 find_map.psymtab = pst;
16707 map = htab_find (data->cu_index_htab, &find_map);
16708 gdb_assert (map != NULL);
16709 data->previous_cu_index = map->cu_index;
16710 data->previous_valid = 1;
16711 }
16712 else
16713 data->previous_valid = 0;
16714
16715 return 0;
16716}
16717
16718/* Write OBJFILE's address map to OBSTACK.
16719 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16720 in the index file. */
16721
16722static void
16723write_address_map (struct objfile *objfile, struct obstack *obstack,
16724 htab_t cu_index_htab)
16725{
16726 struct addrmap_index_data addrmap_index_data;
16727
16728 /* When writing the address table, we have to cope with the fact that
16729 the addrmap iterator only provides the start of a region; we have to
16730 wait until the next invocation to get the start of the next region. */
16731
16732 addrmap_index_data.objfile = objfile;
16733 addrmap_index_data.addr_obstack = obstack;
16734 addrmap_index_data.cu_index_htab = cu_index_htab;
16735 addrmap_index_data.previous_valid = 0;
16736
16737 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16738 &addrmap_index_data);
16739
16740 /* It's highly unlikely the last entry (end address = 0xff...ff)
16741 is valid, but we should still handle it.
16742 The end address is recorded as the start of the next region, but that
16743 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16744 anyway. */
16745 if (addrmap_index_data.previous_valid)
16746 add_address_entry (objfile, obstack,
16747 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16748 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16749}
16750
16751/* Add a list of partial symbols to SYMTAB. */
b89be57b 16752
9291a0cd
TT
16753static void
16754write_psymbols (struct mapped_symtab *symtab,
987d643c 16755 htab_t psyms_seen,
9291a0cd
TT
16756 struct partial_symbol **psymp,
16757 int count,
987d643c
TT
16758 offset_type cu_index,
16759 int is_static)
9291a0cd
TT
16760{
16761 for (; count-- > 0; ++psymp)
16762 {
987d643c
TT
16763 void **slot, *lookup;
16764
9291a0cd
TT
16765 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16766 error (_("Ada is not currently supported by the index"));
987d643c
TT
16767
16768 /* We only want to add a given psymbol once. However, we also
16769 want to account for whether it is global or static. So, we
16770 may add it twice, using slightly different values. */
16771 if (is_static)
16772 {
16773 uintptr_t val = 1 | (uintptr_t) *psymp;
16774
16775 lookup = (void *) val;
16776 }
16777 else
16778 lookup = *psymp;
16779
16780 /* Only add a given psymbol once. */
16781 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16782 if (!*slot)
16783 {
16784 *slot = lookup;
bb2f58dc 16785 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16786 }
9291a0cd
TT
16787 }
16788}
16789
16790/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16791 exception if there is an error. */
b89be57b 16792
9291a0cd
TT
16793static void
16794write_obstack (FILE *file, struct obstack *obstack)
16795{
16796 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16797 file)
16798 != obstack_object_size (obstack))
16799 error (_("couldn't data write to file"));
16800}
16801
16802/* Unlink a file if the argument is not NULL. */
b89be57b 16803
9291a0cd
TT
16804static void
16805unlink_if_set (void *p)
16806{
16807 char **filename = p;
16808 if (*filename)
16809 unlink (*filename);
16810}
16811
1fd400ff
TT
16812/* A helper struct used when iterating over debug_types. */
16813struct signatured_type_index_data
16814{
16815 struct objfile *objfile;
16816 struct mapped_symtab *symtab;
16817 struct obstack *types_list;
987d643c 16818 htab_t psyms_seen;
1fd400ff
TT
16819 int cu_index;
16820};
16821
16822/* A helper function that writes a single signatured_type to an
16823 obstack. */
b89be57b 16824
1fd400ff
TT
16825static int
16826write_one_signatured_type (void **slot, void *d)
16827{
16828 struct signatured_type_index_data *info = d;
16829 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16830 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16831 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16832 gdb_byte val[8];
16833
16834 write_psymbols (info->symtab,
987d643c 16835 info->psyms_seen,
3e43a32a
MS
16836 info->objfile->global_psymbols.list
16837 + psymtab->globals_offset,
987d643c
TT
16838 psymtab->n_global_syms, info->cu_index,
16839 0);
1fd400ff 16840 write_psymbols (info->symtab,
987d643c 16841 info->psyms_seen,
3e43a32a
MS
16842 info->objfile->static_psymbols.list
16843 + psymtab->statics_offset,
987d643c
TT
16844 psymtab->n_static_syms, info->cu_index,
16845 1);
1fd400ff 16846
b3c8eb43 16847 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16848 obstack_grow (info->types_list, val, 8);
16849 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16850 obstack_grow (info->types_list, val, 8);
16851 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16852 obstack_grow (info->types_list, val, 8);
16853
16854 ++info->cu_index;
16855
16856 return 1;
16857}
16858
9291a0cd 16859/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16860
9291a0cd
TT
16861static void
16862write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16863{
16864 struct cleanup *cleanup;
16865 char *filename, *cleanup_filename;
1fd400ff
TT
16866 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16867 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16868 int i;
16869 FILE *out_file;
16870 struct mapped_symtab *symtab;
16871 offset_type val, size_of_contents, total_len;
16872 struct stat st;
16873 char buf[8];
987d643c 16874 htab_t psyms_seen;
0a5429f6
DE
16875 htab_t cu_index_htab;
16876 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16877
b4f2f049 16878 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16879 return;
b4f2f049 16880
9291a0cd
TT
16881 if (dwarf2_per_objfile->using_index)
16882 error (_("Cannot use an index to create the index"));
16883
8b70b953
TT
16884 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16885 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16886
9291a0cd 16887 if (stat (objfile->name, &st) < 0)
7e17e088 16888 perror_with_name (objfile->name);
9291a0cd
TT
16889
16890 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16891 INDEX_SUFFIX, (char *) NULL);
16892 cleanup = make_cleanup (xfree, filename);
16893
16894 out_file = fopen (filename, "wb");
16895 if (!out_file)
16896 error (_("Can't open `%s' for writing"), filename);
16897
16898 cleanup_filename = filename;
16899 make_cleanup (unlink_if_set, &cleanup_filename);
16900
16901 symtab = create_mapped_symtab ();
16902 make_cleanup (cleanup_mapped_symtab, symtab);
16903
16904 obstack_init (&addr_obstack);
16905 make_cleanup_obstack_free (&addr_obstack);
16906
16907 obstack_init (&cu_list);
16908 make_cleanup_obstack_free (&cu_list);
16909
1fd400ff
TT
16910 obstack_init (&types_cu_list);
16911 make_cleanup_obstack_free (&types_cu_list);
16912
987d643c
TT
16913 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16914 NULL, xcalloc, xfree);
96408a79 16915 make_cleanup_htab_delete (psyms_seen);
987d643c 16916
0a5429f6
DE
16917 /* While we're scanning CU's create a table that maps a psymtab pointer
16918 (which is what addrmap records) to its index (which is what is recorded
16919 in the index file). This will later be needed to write the address
16920 table. */
16921 cu_index_htab = htab_create_alloc (100,
16922 hash_psymtab_cu_index,
16923 eq_psymtab_cu_index,
16924 NULL, xcalloc, xfree);
96408a79 16925 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16926 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16927 xmalloc (sizeof (struct psymtab_cu_index_map)
16928 * dwarf2_per_objfile->n_comp_units);
16929 make_cleanup (xfree, psymtab_cu_index_map);
16930
16931 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16932 work here. Also, the debug_types entries do not appear in
16933 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16934 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16935 {
3e43a32a
MS
16936 struct dwarf2_per_cu_data *per_cu
16937 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16938 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16939 gdb_byte val[8];
0a5429f6
DE
16940 struct psymtab_cu_index_map *map;
16941 void **slot;
9291a0cd
TT
16942
16943 write_psymbols (symtab,
987d643c 16944 psyms_seen,
9291a0cd 16945 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16946 psymtab->n_global_syms, i,
16947 0);
9291a0cd 16948 write_psymbols (symtab,
987d643c 16949 psyms_seen,
9291a0cd 16950 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16951 psymtab->n_static_syms, i,
16952 1);
9291a0cd 16953
0a5429f6
DE
16954 map = &psymtab_cu_index_map[i];
16955 map->psymtab = psymtab;
16956 map->cu_index = i;
16957 slot = htab_find_slot (cu_index_htab, map, INSERT);
16958 gdb_assert (slot != NULL);
16959 gdb_assert (*slot == NULL);
16960 *slot = map;
9291a0cd 16961
e254ef6a 16962 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16963 obstack_grow (&cu_list, val, 8);
e254ef6a 16964 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16965 obstack_grow (&cu_list, val, 8);
16966 }
16967
0a5429f6
DE
16968 /* Dump the address map. */
16969 write_address_map (objfile, &addr_obstack, cu_index_htab);
16970
1fd400ff
TT
16971 /* Write out the .debug_type entries, if any. */
16972 if (dwarf2_per_objfile->signatured_types)
16973 {
16974 struct signatured_type_index_data sig_data;
16975
16976 sig_data.objfile = objfile;
16977 sig_data.symtab = symtab;
16978 sig_data.types_list = &types_cu_list;
987d643c 16979 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16980 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16981 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16982 write_one_signatured_type, &sig_data);
16983 }
16984
9291a0cd
TT
16985 obstack_init (&constant_pool);
16986 make_cleanup_obstack_free (&constant_pool);
16987 obstack_init (&symtab_obstack);
16988 make_cleanup_obstack_free (&symtab_obstack);
16989 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16990
16991 obstack_init (&contents);
16992 make_cleanup_obstack_free (&contents);
1fd400ff 16993 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16994 total_len = size_of_contents;
16995
16996 /* The version number. */
559a7a62 16997 val = MAYBE_SWAP (5);
9291a0cd
TT
16998 obstack_grow (&contents, &val, sizeof (val));
16999
17000 /* The offset of the CU list from the start of the file. */
17001 val = MAYBE_SWAP (total_len);
17002 obstack_grow (&contents, &val, sizeof (val));
17003 total_len += obstack_object_size (&cu_list);
17004
1fd400ff
TT
17005 /* The offset of the types CU list from the start of the file. */
17006 val = MAYBE_SWAP (total_len);
17007 obstack_grow (&contents, &val, sizeof (val));
17008 total_len += obstack_object_size (&types_cu_list);
17009
9291a0cd
TT
17010 /* The offset of the address table from the start of the file. */
17011 val = MAYBE_SWAP (total_len);
17012 obstack_grow (&contents, &val, sizeof (val));
17013 total_len += obstack_object_size (&addr_obstack);
17014
17015 /* The offset of the symbol table from the start of the file. */
17016 val = MAYBE_SWAP (total_len);
17017 obstack_grow (&contents, &val, sizeof (val));
17018 total_len += obstack_object_size (&symtab_obstack);
17019
17020 /* The offset of the constant pool from the start of the file. */
17021 val = MAYBE_SWAP (total_len);
17022 obstack_grow (&contents, &val, sizeof (val));
17023 total_len += obstack_object_size (&constant_pool);
17024
17025 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17026
17027 write_obstack (out_file, &contents);
17028 write_obstack (out_file, &cu_list);
1fd400ff 17029 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17030 write_obstack (out_file, &addr_obstack);
17031 write_obstack (out_file, &symtab_obstack);
17032 write_obstack (out_file, &constant_pool);
17033
17034 fclose (out_file);
17035
17036 /* We want to keep the file, so we set cleanup_filename to NULL
17037 here. See unlink_if_set. */
17038 cleanup_filename = NULL;
17039
17040 do_cleanups (cleanup);
17041}
17042
90476074
TT
17043/* Implementation of the `save gdb-index' command.
17044
17045 Note that the file format used by this command is documented in the
17046 GDB manual. Any changes here must be documented there. */
11570e71 17047
9291a0cd
TT
17048static void
17049save_gdb_index_command (char *arg, int from_tty)
17050{
17051 struct objfile *objfile;
17052
17053 if (!arg || !*arg)
96d19272 17054 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17055
17056 ALL_OBJFILES (objfile)
17057 {
17058 struct stat st;
17059
17060 /* If the objfile does not correspond to an actual file, skip it. */
17061 if (stat (objfile->name, &st) < 0)
17062 continue;
17063
17064 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17065 if (dwarf2_per_objfile)
17066 {
17067 volatile struct gdb_exception except;
17068
17069 TRY_CATCH (except, RETURN_MASK_ERROR)
17070 {
17071 write_psymtabs_to_index (objfile, arg);
17072 }
17073 if (except.reason < 0)
17074 exception_fprintf (gdb_stderr, except,
17075 _("Error while writing index for `%s': "),
17076 objfile->name);
17077 }
17078 }
dce234bc
PP
17079}
17080
9291a0cd
TT
17081\f
17082
9eae7c52
TT
17083int dwarf2_always_disassemble;
17084
17085static void
17086show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17087 struct cmd_list_element *c, const char *value)
17088{
3e43a32a
MS
17089 fprintf_filtered (file,
17090 _("Whether to always disassemble "
17091 "DWARF expressions is %s.\n"),
9eae7c52
TT
17092 value);
17093}
17094
900e11f9
JK
17095static void
17096show_check_physname (struct ui_file *file, int from_tty,
17097 struct cmd_list_element *c, const char *value)
17098{
17099 fprintf_filtered (file,
17100 _("Whether to check \"physname\" is %s.\n"),
17101 value);
17102}
17103
6502dd73
DJ
17104void _initialize_dwarf2_read (void);
17105
17106void
17107_initialize_dwarf2_read (void)
17108{
96d19272
JK
17109 struct cmd_list_element *c;
17110
dce234bc 17111 dwarf2_objfile_data_key
c1bd65d0 17112 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17113
1bedd215
AC
17114 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17115Set DWARF 2 specific variables.\n\
17116Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17117 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17118 0/*allow-unknown*/, &maintenance_set_cmdlist);
17119
1bedd215
AC
17120 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17121Show DWARF 2 specific variables\n\
17122Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17123 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17124 0/*allow-unknown*/, &maintenance_show_cmdlist);
17125
17126 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17127 &dwarf2_max_cache_age, _("\
17128Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17129Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17130A higher limit means that cached compilation units will be stored\n\
17131in memory longer, and more total memory will be used. Zero disables\n\
17132caching, which can slow down startup."),
2c5b56ce 17133 NULL,
920d2a44 17134 show_dwarf2_max_cache_age,
2c5b56ce 17135 &set_dwarf2_cmdlist,
ae038cb0 17136 &show_dwarf2_cmdlist);
d97bc12b 17137
9eae7c52
TT
17138 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17139 &dwarf2_always_disassemble, _("\
17140Set whether `info address' always disassembles DWARF expressions."), _("\
17141Show whether `info address' always disassembles DWARF expressions."), _("\
17142When enabled, DWARF expressions are always printed in an assembly-like\n\
17143syntax. When disabled, expressions will be printed in a more\n\
17144conversational style, when possible."),
17145 NULL,
17146 show_dwarf2_always_disassemble,
17147 &set_dwarf2_cmdlist,
17148 &show_dwarf2_cmdlist);
17149
d97bc12b
DE
17150 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17151Set debugging of the dwarf2 DIE reader."), _("\
17152Show debugging of the dwarf2 DIE reader."), _("\
17153When enabled (non-zero), DIEs are dumped after they are read in.\n\
17154The value is the maximum depth to print."),
17155 NULL,
17156 NULL,
17157 &setdebuglist, &showdebuglist);
9291a0cd 17158
900e11f9
JK
17159 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17160Set cross-checking of \"physname\" code against demangler."), _("\
17161Show cross-checking of \"physname\" code against demangler."), _("\
17162When enabled, GDB's internal \"physname\" code is checked against\n\
17163the demangler."),
17164 NULL, show_check_physname,
17165 &setdebuglist, &showdebuglist);
17166
96d19272 17167 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17168 _("\
fc1a9d6e 17169Save a gdb-index file.\n\
11570e71 17170Usage: save gdb-index DIRECTORY"),
96d19272
JK
17171 &save_cmdlist);
17172 set_cmd_completer (c, filename_completer);
6502dd73 17173}
This page took 2.401321 seconds and 4 git commands to generate.