merge from gcc
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5
PA
61#include "c-lang.h"
62#include "valprint.h"
60d5a603 63#include <ctype.h>
4c2df51b 64
c906108c
SS
65#include <fcntl.h>
66#include "gdb_string.h"
4bdf3d34 67#include "gdb_assert.h"
c906108c 68#include <sys/types.h>
233a11ab
CS
69#ifdef HAVE_ZLIB_H
70#include <zlib.h>
71#endif
dce234bc
PP
72#ifdef HAVE_MMAP
73#include <sys/mman.h>
85d9bd0e
TT
74#ifndef MAP_FAILED
75#define MAP_FAILED ((void *) -1)
76#endif
dce234bc 77#endif
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
d97bc12b
DE
82/* When non-zero, dump DIEs after they are read in. */
83static int dwarf2_die_debug = 0;
84
900e11f9
JK
85/* When non-zero, cross-check physname against demangler. */
86static int check_physname = 0;
87
481860b3
GB
88/* When non-zero, do not reject deprecated .gdb_index sections. */
89int use_deprecated_index_sections = 0;
90
dce234bc
PP
91static int pagesize;
92
df8a16a1
DJ
93/* When set, the file that we're processing is known to have debugging
94 info for C++ namespaces. GCC 3.3.x did not produce this information,
95 but later versions do. */
96
97static int processing_has_namespace_info;
98
6502dd73
DJ
99static const struct objfile_data *dwarf2_objfile_data_key;
100
dce234bc
PP
101struct dwarf2_section_info
102{
103 asection *asection;
104 gdb_byte *buffer;
105 bfd_size_type size;
b315ab21
TG
106 /* Not NULL if the section was actually mmapped. */
107 void *map_addr;
108 /* Page aligned size of mmapped area. */
109 bfd_size_type map_len;
be391dca
TT
110 /* True if we have tried to read this section. */
111 int readin;
dce234bc
PP
112};
113
8b70b953
TT
114typedef struct dwarf2_section_info dwarf2_section_info_def;
115DEF_VEC_O (dwarf2_section_info_def);
116
9291a0cd
TT
117/* All offsets in the index are of this type. It must be
118 architecture-independent. */
119typedef uint32_t offset_type;
120
121DEF_VEC_I (offset_type);
122
123/* A description of the mapped index. The file format is described in
124 a comment by the code that writes the index. */
125struct mapped_index
126{
559a7a62
JK
127 /* Index data format version. */
128 int version;
129
9291a0cd
TT
130 /* The total length of the buffer. */
131 off_t total_size;
b11b1f88 132
9291a0cd
TT
133 /* A pointer to the address table data. */
134 const gdb_byte *address_table;
b11b1f88 135
9291a0cd
TT
136 /* Size of the address table data in bytes. */
137 offset_type address_table_size;
b11b1f88 138
3876f04e
DE
139 /* The symbol table, implemented as a hash table. */
140 const offset_type *symbol_table;
b11b1f88 141
9291a0cd 142 /* Size in slots, each slot is 2 offset_types. */
3876f04e 143 offset_type symbol_table_slots;
b11b1f88 144
9291a0cd
TT
145 /* A pointer to the constant pool. */
146 const char *constant_pool;
147};
148
9cdd5dbd
DE
149/* Collection of data recorded per objfile.
150 This hangs off of dwarf2_objfile_data_key. */
151
6502dd73
DJ
152struct dwarf2_per_objfile
153{
dce234bc
PP
154 struct dwarf2_section_info info;
155 struct dwarf2_section_info abbrev;
156 struct dwarf2_section_info line;
dce234bc
PP
157 struct dwarf2_section_info loc;
158 struct dwarf2_section_info macinfo;
cf2c3c16 159 struct dwarf2_section_info macro;
dce234bc
PP
160 struct dwarf2_section_info str;
161 struct dwarf2_section_info ranges;
162 struct dwarf2_section_info frame;
163 struct dwarf2_section_info eh_frame;
9291a0cd 164 struct dwarf2_section_info gdb_index;
ae038cb0 165
8b70b953
TT
166 VEC (dwarf2_section_info_def) *types;
167
be391dca
TT
168 /* Back link. */
169 struct objfile *objfile;
170
d467dd73 171 /* Table of all the compilation units. This is used to locate
10b3939b 172 the target compilation unit of a particular reference. */
ae038cb0
DJ
173 struct dwarf2_per_cu_data **all_comp_units;
174
175 /* The number of compilation units in ALL_COMP_UNITS. */
176 int n_comp_units;
177
1fd400ff 178 /* The number of .debug_types-related CUs. */
d467dd73 179 int n_type_units;
1fd400ff 180
d467dd73
DE
181 /* The .debug_types-related CUs (TUs). */
182 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 183
ae038cb0
DJ
184 /* A chain of compilation units that are currently read in, so that
185 they can be freed later. */
186 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 187
348e048f
DE
188 /* A table mapping .debug_types signatures to its signatured_type entry.
189 This is NULL if the .debug_types section hasn't been read in yet. */
190 htab_t signatured_types;
191
72dca2f5
FR
192 /* A flag indicating wether this objfile has a section loaded at a
193 VMA of 0. */
194 int has_section_at_zero;
9291a0cd 195
ae2de4f8
DE
196 /* True if we are using the mapped index,
197 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
198 unsigned char using_index;
199
ae2de4f8 200 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 201 struct mapped_index *index_table;
98bfdba5 202
7b9f3c50
DE
203 /* When using index_table, this keeps track of all quick_file_names entries.
204 TUs can share line table entries with CUs or other TUs, and there can be
205 a lot more TUs than unique line tables, so we maintain a separate table
206 of all line table entries to support the sharing. */
207 htab_t quick_file_names_table;
208
98bfdba5
PA
209 /* Set during partial symbol reading, to prevent queueing of full
210 symbols. */
211 int reading_partial_symbols;
673bfd45
DE
212
213 /* Table mapping type .debug_info DIE offsets to types.
214 This is NULL if not allocated yet.
215 It (currently) makes sense to allocate debug_types_type_hash lazily.
216 To keep things simple we allocate both lazily. */
217 htab_t debug_info_type_hash;
218
b64f50a1 219 /* Table mapping type .debug_types DIE sect_offset to types.
673bfd45
DE
220 This is NULL if not allocated yet. */
221 htab_t debug_types_type_hash;
6502dd73
DJ
222};
223
224static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 225
251d32d9 226/* Default names of the debugging sections. */
c906108c 227
233a11ab
CS
228/* Note that if the debugging section has been compressed, it might
229 have a name like .zdebug_info. */
230
9cdd5dbd
DE
231static const struct dwarf2_debug_sections dwarf2_elf_names =
232{
251d32d9
TG
233 { ".debug_info", ".zdebug_info" },
234 { ".debug_abbrev", ".zdebug_abbrev" },
235 { ".debug_line", ".zdebug_line" },
236 { ".debug_loc", ".zdebug_loc" },
237 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 238 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
239 { ".debug_str", ".zdebug_str" },
240 { ".debug_ranges", ".zdebug_ranges" },
241 { ".debug_types", ".zdebug_types" },
242 { ".debug_frame", ".zdebug_frame" },
243 { ".eh_frame", NULL },
24d3216f
TT
244 { ".gdb_index", ".zgdb_index" },
245 23
251d32d9 246};
c906108c
SS
247
248/* local data types */
249
0963b4bd 250/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
251#ifndef ABBREV_HASH_SIZE
252#define ABBREV_HASH_SIZE 121
253#endif
254
107d2387
AC
255/* The data in a compilation unit header, after target2host
256 translation, looks like this. */
c906108c 257struct comp_unit_head
a738430d 258{
c764a876 259 unsigned int length;
a738430d 260 short version;
a738430d
MK
261 unsigned char addr_size;
262 unsigned char signed_addr_p;
b64f50a1 263 sect_offset abbrev_offset;
57349743 264
a738430d
MK
265 /* Size of file offsets; either 4 or 8. */
266 unsigned int offset_size;
57349743 267
a738430d
MK
268 /* Size of the length field; either 4 or 12. */
269 unsigned int initial_length_size;
57349743 270
a738430d
MK
271 /* Offset to the first byte of this compilation unit header in the
272 .debug_info section, for resolving relative reference dies. */
b64f50a1 273 sect_offset offset;
57349743 274
d00adf39
DE
275 /* Offset to first die in this cu from the start of the cu.
276 This will be the first byte following the compilation unit header. */
b64f50a1 277 cu_offset first_die_offset;
a738430d 278};
c906108c 279
3da10d80
KS
280/* Type used for delaying computation of method physnames.
281 See comments for compute_delayed_physnames. */
282struct delayed_method_info
283{
284 /* The type to which the method is attached, i.e., its parent class. */
285 struct type *type;
286
287 /* The index of the method in the type's function fieldlists. */
288 int fnfield_index;
289
290 /* The index of the method in the fieldlist. */
291 int index;
292
293 /* The name of the DIE. */
294 const char *name;
295
296 /* The DIE associated with this method. */
297 struct die_info *die;
298};
299
300typedef struct delayed_method_info delayed_method_info;
301DEF_VEC_O (delayed_method_info);
302
e7c27a73
DJ
303/* Internal state when decoding a particular compilation unit. */
304struct dwarf2_cu
305{
306 /* The objfile containing this compilation unit. */
307 struct objfile *objfile;
308
d00adf39 309 /* The header of the compilation unit. */
e7c27a73 310 struct comp_unit_head header;
e142c38c 311
d00adf39
DE
312 /* Base address of this compilation unit. */
313 CORE_ADDR base_address;
314
315 /* Non-zero if base_address has been set. */
316 int base_known;
317
e142c38c
DJ
318 /* The language we are debugging. */
319 enum language language;
320 const struct language_defn *language_defn;
321
b0f35d58
DL
322 const char *producer;
323
e142c38c
DJ
324 /* The generic symbol table building routines have separate lists for
325 file scope symbols and all all other scopes (local scopes). So
326 we need to select the right one to pass to add_symbol_to_list().
327 We do it by keeping a pointer to the correct list in list_in_scope.
328
329 FIXME: The original dwarf code just treated the file scope as the
330 first local scope, and all other local scopes as nested local
331 scopes, and worked fine. Check to see if we really need to
332 distinguish these in buildsym.c. */
333 struct pending **list_in_scope;
334
f3dd6933
DJ
335 /* DWARF abbreviation table associated with this compilation unit. */
336 struct abbrev_info **dwarf2_abbrevs;
337
338 /* Storage for the abbrev table. */
339 struct obstack abbrev_obstack;
72bf9492 340
b64f50a1
JK
341 /* Hash table holding all the loaded partial DIEs
342 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
b64f50a1
JK
361 /* A hash table of DIE cu_offset for following references with
362 die_info->offset.sect_off as hash. */
51545339 363 htab_t die_hash;
10b3939b
DJ
364
365 /* Full DIEs if read in. */
366 struct die_info *dies;
367
368 /* A set of pointers to dwarf2_per_cu_data objects for compilation
369 units referenced by this one. Only set during full symbol processing;
370 partial symbol tables do not have dependencies. */
371 htab_t dependencies;
372
cb1df416
DJ
373 /* Header data from the line table, during full symbol processing. */
374 struct line_header *line_header;
375
3da10d80
KS
376 /* A list of methods which need to have physnames computed
377 after all type information has been read. */
378 VEC (delayed_method_info) *method_list;
379
96408a79
SA
380 /* To be copied to symtab->call_site_htab. */
381 htab_t call_site_htab;
382
ae038cb0
DJ
383 /* Mark used when releasing cached dies. */
384 unsigned int mark : 1;
385
8be455d7
JK
386 /* This CU references .debug_loc. See the symtab->locations_valid field.
387 This test is imperfect as there may exist optimized debug code not using
388 any location list and still facing inlining issues if handled as
389 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 390 unsigned int has_loclist : 1;
ba919b58
TT
391
392 /* These cache the results of producer_is_gxx_lt_4_6.
393 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
394 information is cached because profiling CU expansion showed
395 excessive time spent in producer_is_gxx_lt_4_6. */
396 unsigned int checked_producer : 1;
397 unsigned int producer_is_gxx_lt_4_6 : 1;
e7c27a73
DJ
398};
399
10b3939b
DJ
400/* Persistent data held for a compilation unit, even when not
401 processing it. We put a pointer to this structure in the
28dee7f5 402 read_symtab_private field of the psymtab. */
10b3939b 403
ae038cb0
DJ
404struct dwarf2_per_cu_data
405{
348e048f 406 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 407 bytes should suffice to store the length of any compilation unit
45452591
DE
408 - if it doesn't, GDB will fall over anyway.
409 NOTE: Unlike comp_unit_head.length, this length includes
410 initial_length_size. */
b64f50a1 411 sect_offset offset;
348e048f 412 unsigned int length : 29;
ae038cb0
DJ
413
414 /* Flag indicating this compilation unit will be read in before
415 any of the current compilation units are processed. */
c764a876 416 unsigned int queued : 1;
ae038cb0 417
5afb4e99
DJ
418 /* This flag will be set if we need to load absolutely all DIEs
419 for this compilation unit, instead of just the ones we think
420 are interesting. It gets set if we look for a DIE in the
421 hash table and don't find it. */
422 unsigned int load_all_dies : 1;
423
8b70b953
TT
424 /* Non-null if this CU is from .debug_types; in which case it points
425 to the section. Otherwise it's from .debug_info. */
b0df02fd 426 struct dwarf2_section_info *debug_types_section;
348e048f 427
17ea53c3
JK
428 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
429 of the CU cache it gets reset to NULL again. */
ae038cb0 430 struct dwarf2_cu *cu;
1c379e20 431
9cdd5dbd
DE
432 /* The corresponding objfile.
433 Normally we can get the objfile from dwarf2_per_objfile.
434 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
435 struct objfile *objfile;
436
437 /* When using partial symbol tables, the 'psymtab' field is active.
438 Otherwise the 'quick' field is active. */
439 union
440 {
441 /* The partial symbol table associated with this compilation unit,
442 or NULL for partial units (which do not have an associated
443 symtab). */
444 struct partial_symtab *psymtab;
445
446 /* Data needed by the "quick" functions. */
447 struct dwarf2_per_cu_quick_data *quick;
448 } v;
ae038cb0
DJ
449};
450
348e048f
DE
451/* Entry in the signatured_types hash table. */
452
453struct signatured_type
454{
455 ULONGEST signature;
456
b64f50a1
JK
457 /* Offset in this TU of the type defined by this TU. */
458 cu_offset type_offset;
348e048f
DE
459
460 /* The CU(/TU) of this type. */
461 struct dwarf2_per_cu_data per_cu;
462};
463
0963b4bd
MS
464/* Struct used to pass misc. parameters to read_die_and_children, et
465 al. which are used for both .debug_info and .debug_types dies.
466 All parameters here are unchanging for the life of the call. This
467 struct exists to abstract away the constant parameters of die
468 reading. */
93311388
DE
469
470struct die_reader_specs
471{
472 /* The bfd of this objfile. */
473 bfd* abfd;
474
475 /* The CU of the DIE we are parsing. */
476 struct dwarf2_cu *cu;
477
478 /* Pointer to start of section buffer.
479 This is either the start of .debug_info or .debug_types. */
480 const gdb_byte *buffer;
481};
482
debd256d
JB
483/* The line number information for a compilation unit (found in the
484 .debug_line section) begins with a "statement program header",
485 which contains the following information. */
486struct line_header
487{
488 unsigned int total_length;
489 unsigned short version;
490 unsigned int header_length;
491 unsigned char minimum_instruction_length;
2dc7f7b3 492 unsigned char maximum_ops_per_instruction;
debd256d
JB
493 unsigned char default_is_stmt;
494 int line_base;
495 unsigned char line_range;
496 unsigned char opcode_base;
497
498 /* standard_opcode_lengths[i] is the number of operands for the
499 standard opcode whose value is i. This means that
500 standard_opcode_lengths[0] is unused, and the last meaningful
501 element is standard_opcode_lengths[opcode_base - 1]. */
502 unsigned char *standard_opcode_lengths;
503
504 /* The include_directories table. NOTE! These strings are not
505 allocated with xmalloc; instead, they are pointers into
506 debug_line_buffer. If you try to free them, `free' will get
507 indigestion. */
508 unsigned int num_include_dirs, include_dirs_size;
509 char **include_dirs;
510
511 /* The file_names table. NOTE! These strings are not allocated
512 with xmalloc; instead, they are pointers into debug_line_buffer.
513 Don't try to free them directly. */
514 unsigned int num_file_names, file_names_size;
515 struct file_entry
c906108c 516 {
debd256d
JB
517 char *name;
518 unsigned int dir_index;
519 unsigned int mod_time;
520 unsigned int length;
aaa75496 521 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 522 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
523 } *file_names;
524
525 /* The start and end of the statement program following this
6502dd73 526 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 527 gdb_byte *statement_program_start, *statement_program_end;
debd256d 528};
c906108c
SS
529
530/* When we construct a partial symbol table entry we only
0963b4bd 531 need this much information. */
c906108c
SS
532struct partial_die_info
533 {
72bf9492 534 /* Offset of this DIE. */
b64f50a1 535 sect_offset offset;
72bf9492
DJ
536
537 /* DWARF-2 tag for this DIE. */
538 ENUM_BITFIELD(dwarf_tag) tag : 16;
539
72bf9492
DJ
540 /* Assorted flags describing the data found in this DIE. */
541 unsigned int has_children : 1;
542 unsigned int is_external : 1;
543 unsigned int is_declaration : 1;
544 unsigned int has_type : 1;
545 unsigned int has_specification : 1;
546 unsigned int has_pc_info : 1;
481860b3 547 unsigned int may_be_inlined : 1;
72bf9492
DJ
548
549 /* Flag set if the SCOPE field of this structure has been
550 computed. */
551 unsigned int scope_set : 1;
552
fa4028e9
JB
553 /* Flag set if the DIE has a byte_size attribute. */
554 unsigned int has_byte_size : 1;
555
98bfdba5
PA
556 /* Flag set if any of the DIE's children are template arguments. */
557 unsigned int has_template_arguments : 1;
558
abc72ce4
DE
559 /* Flag set if fixup_partial_die has been called on this die. */
560 unsigned int fixup_called : 1;
561
72bf9492 562 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 563 sometimes a default name for unnamed DIEs. */
c906108c 564 char *name;
72bf9492 565
abc72ce4
DE
566 /* The linkage name, if present. */
567 const char *linkage_name;
568
72bf9492
DJ
569 /* The scope to prepend to our children. This is generally
570 allocated on the comp_unit_obstack, so will disappear
571 when this compilation unit leaves the cache. */
572 char *scope;
573
574 /* The location description associated with this DIE, if any. */
575 struct dwarf_block *locdesc;
576
577 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
578 CORE_ADDR lowpc;
579 CORE_ADDR highpc;
72bf9492 580
93311388 581 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 582 DW_AT_sibling, if any. */
abc72ce4
DE
583 /* NOTE: This member isn't strictly necessary, read_partial_die could
584 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 585 gdb_byte *sibling;
72bf9492
DJ
586
587 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
588 DW_AT_specification (or DW_AT_abstract_origin or
589 DW_AT_extension). */
b64f50a1 590 sect_offset spec_offset;
72bf9492
DJ
591
592 /* Pointers to this DIE's parent, first child, and next sibling,
593 if any. */
594 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
595 };
596
0963b4bd 597/* This data structure holds the information of an abbrev. */
c906108c
SS
598struct abbrev_info
599 {
600 unsigned int number; /* number identifying abbrev */
601 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
602 unsigned short has_children; /* boolean */
603 unsigned short num_attrs; /* number of attributes */
c906108c
SS
604 struct attr_abbrev *attrs; /* an array of attribute descriptions */
605 struct abbrev_info *next; /* next in chain */
606 };
607
608struct attr_abbrev
609 {
9d25dd43
DE
610 ENUM_BITFIELD(dwarf_attribute) name : 16;
611 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
612 };
613
0963b4bd 614/* Attributes have a name and a value. */
b60c80d6
DJ
615struct attribute
616 {
9d25dd43 617 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
618 ENUM_BITFIELD(dwarf_form) form : 15;
619
620 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
621 field should be in u.str (existing only for DW_STRING) but it is kept
622 here for better struct attribute alignment. */
623 unsigned int string_is_canonical : 1;
624
b60c80d6
DJ
625 union
626 {
627 char *str;
628 struct dwarf_block *blk;
43bbcdc2
PH
629 ULONGEST unsnd;
630 LONGEST snd;
b60c80d6 631 CORE_ADDR addr;
348e048f 632 struct signatured_type *signatured_type;
b60c80d6
DJ
633 }
634 u;
635 };
636
0963b4bd 637/* This data structure holds a complete die structure. */
c906108c
SS
638struct die_info
639 {
76815b17
DE
640 /* DWARF-2 tag for this DIE. */
641 ENUM_BITFIELD(dwarf_tag) tag : 16;
642
643 /* Number of attributes */
98bfdba5
PA
644 unsigned char num_attrs;
645
646 /* True if we're presently building the full type name for the
647 type derived from this DIE. */
648 unsigned char building_fullname : 1;
76815b17
DE
649
650 /* Abbrev number */
651 unsigned int abbrev;
652
93311388 653 /* Offset in .debug_info or .debug_types section. */
b64f50a1 654 sect_offset offset;
78ba4af6
JB
655
656 /* The dies in a compilation unit form an n-ary tree. PARENT
657 points to this die's parent; CHILD points to the first child of
658 this node; and all the children of a given node are chained
4950bc1c 659 together via their SIBLING fields. */
639d11d3
DC
660 struct die_info *child; /* Its first child, if any. */
661 struct die_info *sibling; /* Its next sibling, if any. */
662 struct die_info *parent; /* Its parent, if any. */
c906108c 663
b60c80d6
DJ
664 /* An array of attributes, with NUM_ATTRS elements. There may be
665 zero, but it's not common and zero-sized arrays are not
666 sufficiently portable C. */
667 struct attribute attrs[1];
c906108c
SS
668 };
669
0963b4bd 670/* Get at parts of an attribute structure. */
c906108c
SS
671
672#define DW_STRING(attr) ((attr)->u.str)
8285870a 673#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
674#define DW_UNSND(attr) ((attr)->u.unsnd)
675#define DW_BLOCK(attr) ((attr)->u.blk)
676#define DW_SND(attr) ((attr)->u.snd)
677#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 678#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 679
0963b4bd 680/* Blocks are a bunch of untyped bytes. */
c906108c
SS
681struct dwarf_block
682 {
683 unsigned int size;
1d6edc3c
JK
684
685 /* Valid only if SIZE is not zero. */
fe1b8b76 686 gdb_byte *data;
c906108c
SS
687 };
688
c906108c
SS
689#ifndef ATTR_ALLOC_CHUNK
690#define ATTR_ALLOC_CHUNK 4
691#endif
692
c906108c
SS
693/* Allocate fields for structs, unions and enums in this size. */
694#ifndef DW_FIELD_ALLOC_CHUNK
695#define DW_FIELD_ALLOC_CHUNK 4
696#endif
697
c906108c
SS
698/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
699 but this would require a corresponding change in unpack_field_as_long
700 and friends. */
701static int bits_per_byte = 8;
702
703/* The routines that read and process dies for a C struct or C++ class
704 pass lists of data member fields and lists of member function fields
705 in an instance of a field_info structure, as defined below. */
706struct field_info
c5aa993b 707 {
0963b4bd 708 /* List of data member and baseclasses fields. */
c5aa993b
JM
709 struct nextfield
710 {
711 struct nextfield *next;
712 int accessibility;
713 int virtuality;
714 struct field field;
715 }
7d0ccb61 716 *fields, *baseclasses;
c906108c 717
7d0ccb61 718 /* Number of fields (including baseclasses). */
c5aa993b 719 int nfields;
c906108c 720
c5aa993b
JM
721 /* Number of baseclasses. */
722 int nbaseclasses;
c906108c 723
c5aa993b
JM
724 /* Set if the accesibility of one of the fields is not public. */
725 int non_public_fields;
c906108c 726
c5aa993b
JM
727 /* Member function fields array, entries are allocated in the order they
728 are encountered in the object file. */
729 struct nextfnfield
730 {
731 struct nextfnfield *next;
732 struct fn_field fnfield;
733 }
734 *fnfields;
c906108c 735
c5aa993b
JM
736 /* Member function fieldlist array, contains name of possibly overloaded
737 member function, number of overloaded member functions and a pointer
738 to the head of the member function field chain. */
739 struct fnfieldlist
740 {
741 char *name;
742 int length;
743 struct nextfnfield *head;
744 }
745 *fnfieldlists;
c906108c 746
c5aa993b
JM
747 /* Number of entries in the fnfieldlists array. */
748 int nfnfields;
98751a41
JK
749
750 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
751 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
752 struct typedef_field_list
753 {
754 struct typedef_field field;
755 struct typedef_field_list *next;
756 }
757 *typedef_field_list;
758 unsigned typedef_field_list_count;
c5aa993b 759 };
c906108c 760
10b3939b
DJ
761/* One item on the queue of compilation units to read in full symbols
762 for. */
763struct dwarf2_queue_item
764{
765 struct dwarf2_per_cu_data *per_cu;
766 struct dwarf2_queue_item *next;
767};
768
769/* The current queue. */
770static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
771
ae038cb0
DJ
772/* Loaded secondary compilation units are kept in memory until they
773 have not been referenced for the processing of this many
774 compilation units. Set this to zero to disable caching. Cache
775 sizes of up to at least twenty will improve startup time for
776 typical inter-CU-reference binaries, at an obvious memory cost. */
777static int dwarf2_max_cache_age = 5;
920d2a44
AC
778static void
779show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
780 struct cmd_list_element *c, const char *value)
781{
3e43a32a
MS
782 fprintf_filtered (file, _("The upper bound on the age of cached "
783 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
784 value);
785}
786
ae038cb0 787
0963b4bd 788/* Various complaints about symbol reading that don't abort the process. */
c906108c 789
4d3c2250
KB
790static void
791dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 792{
4d3c2250 793 complaint (&symfile_complaints,
e2e0b3e5 794 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
795}
796
25e43795
DJ
797static void
798dwarf2_debug_line_missing_file_complaint (void)
799{
800 complaint (&symfile_complaints,
801 _(".debug_line section has line data without a file"));
802}
803
59205f5a
JB
804static void
805dwarf2_debug_line_missing_end_sequence_complaint (void)
806{
807 complaint (&symfile_complaints,
3e43a32a
MS
808 _(".debug_line section has line "
809 "program sequence without an end"));
59205f5a
JB
810}
811
4d3c2250
KB
812static void
813dwarf2_complex_location_expr_complaint (void)
2e276125 814{
e2e0b3e5 815 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
816}
817
4d3c2250
KB
818static void
819dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
820 int arg3)
2e276125 821{
4d3c2250 822 complaint (&symfile_complaints,
3e43a32a
MS
823 _("const value length mismatch for '%s', got %d, expected %d"),
824 arg1, arg2, arg3);
4d3c2250
KB
825}
826
827static void
cf2c3c16 828dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
cf2c3c16
TT
831 _("macro info runs off end of `%s' section"),
832 section->asection->name);
4d3c2250
KB
833}
834
835static void
836dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 837{
4d3c2250 838 complaint (&symfile_complaints,
3e43a32a
MS
839 _("macro debug info contains a "
840 "malformed macro definition:\n`%s'"),
4d3c2250
KB
841 arg1);
842}
843
844static void
845dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 846{
4d3c2250 847 complaint (&symfile_complaints,
3e43a32a
MS
848 _("invalid attribute class or form for '%s' in '%s'"),
849 arg1, arg2);
4d3c2250 850}
c906108c 851
c906108c
SS
852/* local function prototypes */
853
4efb68b1 854static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 855
aaa75496
JB
856static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
857 struct objfile *);
858
918dd910
JK
859static void dwarf2_find_base_address (struct die_info *die,
860 struct dwarf2_cu *cu);
861
c67a9c90 862static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 863
72bf9492
DJ
864static void scan_partial_symbols (struct partial_die_info *,
865 CORE_ADDR *, CORE_ADDR *,
5734ee8b 866 int, struct dwarf2_cu *);
c906108c 867
72bf9492
DJ
868static void add_partial_symbol (struct partial_die_info *,
869 struct dwarf2_cu *);
63d06c5c 870
72bf9492
DJ
871static void add_partial_namespace (struct partial_die_info *pdi,
872 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 873 int need_pc, struct dwarf2_cu *cu);
63d06c5c 874
5d7cb8df
JK
875static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
876 CORE_ADDR *highpc, int need_pc,
877 struct dwarf2_cu *cu);
878
72bf9492
DJ
879static void add_partial_enumeration (struct partial_die_info *enum_pdi,
880 struct dwarf2_cu *cu);
91c24f0a 881
bc30ff58
JB
882static void add_partial_subprogram (struct partial_die_info *pdi,
883 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 884 int need_pc, struct dwarf2_cu *cu);
bc30ff58 885
fe1b8b76 886static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
887 gdb_byte *buffer, gdb_byte *info_ptr,
888 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 889
a14ed312 890static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 891
a14ed312 892static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 893
e5fe5e75 894static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 895
f3dd6933 896static void dwarf2_free_abbrev_table (void *);
c906108c 897
6caca83c
CC
898static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
899
fe1b8b76 900static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 901 struct dwarf2_cu *);
72bf9492 902
57349743 903static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 904 struct dwarf2_cu *);
c906108c 905
93311388
DE
906static struct partial_die_info *load_partial_dies (bfd *,
907 gdb_byte *, gdb_byte *,
908 int, struct dwarf2_cu *);
72bf9492 909
fe1b8b76 910static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
911 struct abbrev_info *abbrev,
912 unsigned int, bfd *,
913 gdb_byte *, gdb_byte *,
914 struct dwarf2_cu *);
c906108c 915
b64f50a1 916static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 917 struct dwarf2_cu *);
72bf9492
DJ
918
919static void fixup_partial_die (struct partial_die_info *,
920 struct dwarf2_cu *);
921
fe1b8b76
JB
922static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 924
fe1b8b76
JB
925static gdb_byte *read_attribute_value (struct attribute *, unsigned,
926 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 927
fe1b8b76 928static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 929
fe1b8b76 930static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 933
fe1b8b76 934static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 935
93311388 936static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 937
fe1b8b76 938static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 939 unsigned int *);
c906108c 940
c764a876
DE
941static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
942
943static LONGEST read_checked_initial_length_and_offset
944 (bfd *, gdb_byte *, const struct comp_unit_head *,
945 unsigned int *, unsigned int *);
613e1657 946
fe1b8b76 947static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
948 unsigned int *);
949
950static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 951
fe1b8b76 952static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 953
9b1c24c8 954static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 955
fe1b8b76
JB
956static char *read_indirect_string (bfd *, gdb_byte *,
957 const struct comp_unit_head *,
958 unsigned int *);
4bdf3d34 959
fe1b8b76 960static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 961
fe1b8b76 962static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 963
fe1b8b76 964static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 965
e142c38c 966static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 967
e142c38c
DJ
968static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
969 struct dwarf2_cu *);
c906108c 970
348e048f
DE
971static struct attribute *dwarf2_attr_no_follow (struct die_info *,
972 unsigned int,
973 struct dwarf2_cu *);
974
05cf31d1
JB
975static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
976 struct dwarf2_cu *cu);
977
e142c38c 978static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 979
e142c38c 980static struct die_info *die_specification (struct die_info *die,
f2f0e013 981 struct dwarf2_cu **);
63d06c5c 982
debd256d
JB
983static void free_line_header (struct line_header *lh);
984
aaa75496
JB
985static void add_file_name (struct line_header *, char *, unsigned int,
986 unsigned int, unsigned int);
987
debd256d
JB
988static struct line_header *(dwarf_decode_line_header
989 (unsigned int offset,
e7c27a73 990 bfd *abfd, struct dwarf2_cu *cu));
debd256d 991
f3f5162e
DE
992static void dwarf_decode_lines (struct line_header *, const char *,
993 struct dwarf2_cu *, struct partial_symtab *,
994 int);
c906108c 995
72b9f47f 996static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 997
a14ed312 998static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 999 struct dwarf2_cu *);
c906108c 1000
34eaf542
TT
1001static struct symbol *new_symbol_full (struct die_info *, struct type *,
1002 struct dwarf2_cu *, struct symbol *);
1003
a14ed312 1004static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1005 struct dwarf2_cu *);
c906108c 1006
98bfdba5
PA
1007static void dwarf2_const_value_attr (struct attribute *attr,
1008 struct type *type,
1009 const char *name,
1010 struct obstack *obstack,
1011 struct dwarf2_cu *cu, long *value,
1012 gdb_byte **bytes,
1013 struct dwarf2_locexpr_baton **baton);
2df3850c 1014
e7c27a73 1015static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1016
b4ba55a1
JB
1017static int need_gnat_info (struct dwarf2_cu *);
1018
3e43a32a
MS
1019static struct type *die_descriptive_type (struct die_info *,
1020 struct dwarf2_cu *);
b4ba55a1
JB
1021
1022static void set_descriptive_type (struct type *, struct die_info *,
1023 struct dwarf2_cu *);
1024
e7c27a73
DJ
1025static struct type *die_containing_type (struct die_info *,
1026 struct dwarf2_cu *);
c906108c 1027
673bfd45
DE
1028static struct type *lookup_die_type (struct die_info *, struct attribute *,
1029 struct dwarf2_cu *);
c906108c 1030
f792889a 1031static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1032
673bfd45
DE
1033static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1034
0d5cff50 1035static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1036
6e70227d 1037static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1038 const char *suffix, int physname,
1039 struct dwarf2_cu *cu);
63d06c5c 1040
e7c27a73 1041static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1042
348e048f
DE
1043static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1044
e7c27a73 1045static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1046
e7c27a73 1047static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1048
96408a79
SA
1049static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1050
ff013f42
JK
1051static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1052 struct dwarf2_cu *, struct partial_symtab *);
1053
a14ed312 1054static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1055 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1056 struct partial_symtab *);
c906108c 1057
fae299cd
DC
1058static void get_scope_pc_bounds (struct die_info *,
1059 CORE_ADDR *, CORE_ADDR *,
1060 struct dwarf2_cu *);
1061
801e3a5b
JB
1062static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1063 CORE_ADDR, struct dwarf2_cu *);
1064
a14ed312 1065static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1066 struct dwarf2_cu *);
c906108c 1067
a14ed312 1068static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1069 struct type *, struct dwarf2_cu *);
c906108c 1070
a14ed312 1071static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1072 struct die_info *, struct type *,
e7c27a73 1073 struct dwarf2_cu *);
c906108c 1074
a14ed312 1075static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1076 struct type *,
1077 struct dwarf2_cu *);
c906108c 1078
134d01f1 1079static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1080
e7c27a73 1081static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1082
e7c27a73 1083static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1084
5d7cb8df
JK
1085static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1086
27aa8d6a
SW
1087static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1088
f55ee35c
JK
1089static struct type *read_module_type (struct die_info *die,
1090 struct dwarf2_cu *cu);
1091
38d518c9 1092static const char *namespace_name (struct die_info *die,
e142c38c 1093 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1094
134d01f1 1095static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1096
e7c27a73 1097static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1098
6e70227d 1099static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1100 struct dwarf2_cu *);
1101
93311388 1102static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1103
93311388
DE
1104static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1105 gdb_byte *info_ptr,
d97bc12b
DE
1106 gdb_byte **new_info_ptr,
1107 struct die_info *parent);
1108
93311388
DE
1109static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
fe1b8b76 1111 gdb_byte **new_info_ptr,
639d11d3
DC
1112 struct die_info *parent);
1113
93311388
DE
1114static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
fe1b8b76 1116 gdb_byte **new_info_ptr,
639d11d3
DC
1117 struct die_info *parent);
1118
93311388
DE
1119static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1120 struct die_info **, gdb_byte *,
1121 int *);
1122
e7c27a73 1123static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1124
71c25dea
TT
1125static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1126 struct obstack *);
1127
e142c38c 1128static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1129
98bfdba5
PA
1130static const char *dwarf2_full_name (char *name,
1131 struct die_info *die,
1132 struct dwarf2_cu *cu);
1133
e142c38c 1134static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1135 struct dwarf2_cu **);
9219021c 1136
a14ed312 1137static char *dwarf_tag_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_attr_name (unsigned int);
c906108c 1140
a14ed312 1141static char *dwarf_form_name (unsigned int);
c906108c 1142
a14ed312 1143static char *dwarf_bool_name (unsigned int);
c906108c 1144
a14ed312 1145static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1146
1147#if 0
a14ed312 1148static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1149#endif
1150
f9aca02d 1151static struct die_info *sibling_die (struct die_info *);
c906108c 1152
d97bc12b
DE
1153static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1154
1155static void dump_die_for_error (struct die_info *);
1156
1157static void dump_die_1 (struct ui_file *, int level, int max_level,
1158 struct die_info *);
c906108c 1159
d97bc12b 1160/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1161
51545339 1162static void store_in_ref_table (struct die_info *,
10b3939b 1163 struct dwarf2_cu *);
c906108c 1164
93311388
DE
1165static int is_ref_attr (struct attribute *);
1166
b64f50a1 1167static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1168
43bbcdc2 1169static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1170
348e048f
DE
1171static struct die_info *follow_die_ref_or_sig (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
10b3939b
DJ
1175static struct die_info *follow_die_ref (struct die_info *,
1176 struct attribute *,
f2f0e013 1177 struct dwarf2_cu **);
c906108c 1178
348e048f
DE
1179static struct die_info *follow_die_sig (struct die_info *,
1180 struct attribute *,
1181 struct dwarf2_cu **);
1182
6c83ed52
TT
1183static struct signatured_type *lookup_signatured_type_at_offset
1184 (struct objfile *objfile,
b64f50a1 1185 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1186
e5fe5e75 1187static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1188
a0f42c21 1189static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1190
c906108c
SS
1191/* memory allocation interface */
1192
7b5a2f43 1193static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1194
f3dd6933 1195static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1196
b60c80d6 1197static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1198
2e276125 1199static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1200 char *, bfd *, struct dwarf2_cu *,
1201 struct dwarf2_section_info *,
1202 int);
2e276125 1203
8e19ed76
PS
1204static int attr_form_is_block (struct attribute *);
1205
3690dd37
JB
1206static int attr_form_is_section_offset (struct attribute *);
1207
1208static int attr_form_is_constant (struct attribute *);
1209
8cf6f0b1
TT
1210static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211 struct dwarf2_loclist_baton *baton,
1212 struct attribute *attr);
1213
93e7bd98
DJ
1214static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215 struct symbol *sym,
1216 struct dwarf2_cu *cu);
4c2df51b 1217
93311388
DE
1218static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219 struct abbrev_info *abbrev,
1220 struct dwarf2_cu *cu);
4bb7a0a7 1221
72bf9492
DJ
1222static void free_stack_comp_unit (void *);
1223
72bf9492
DJ
1224static hashval_t partial_die_hash (const void *item);
1225
1226static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
ae038cb0 1228static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1229 (sect_offset offset, struct objfile *objfile);
ae038cb0 1230
9816fde3 1231static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1232 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1233
1234static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1235 struct die_info *comp_unit_die);
93311388 1236
68dc6402 1237static void free_heap_comp_unit (void *);
ae038cb0
DJ
1238
1239static void free_cached_comp_units (void *);
1240
1241static void age_cached_comp_units (void);
1242
1243static void free_one_cached_comp_unit (void *);
1244
f792889a
DJ
1245static struct type *set_die_type (struct die_info *, struct type *,
1246 struct dwarf2_cu *);
1c379e20 1247
ae038cb0
DJ
1248static void create_all_comp_units (struct objfile *);
1249
1fd400ff
TT
1250static int create_debug_types_hash_table (struct objfile *objfile);
1251
a0f42c21 1252static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1253
1254static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1255
1256static void dwarf2_add_dependence (struct dwarf2_cu *,
1257 struct dwarf2_per_cu_data *);
1258
ae038cb0
DJ
1259static void dwarf2_mark (struct dwarf2_cu *);
1260
1261static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1262
b64f50a1 1263static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1264 struct dwarf2_per_cu_data *per_cu);
1265
f792889a 1266static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1267
9291a0cd
TT
1268static void dwarf2_release_queue (void *dummy);
1269
a0f42c21 1270static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1271
a0f42c21 1272static void process_queue (void);
9291a0cd
TT
1273
1274static void find_file_and_directory (struct die_info *die,
1275 struct dwarf2_cu *cu,
1276 char **name, char **comp_dir);
1277
1278static char *file_full_name (int file, struct line_header *lh,
1279 const char *comp_dir);
1280
9ff913ba
DE
1281static gdb_byte *read_and_check_comp_unit_head
1282 (struct comp_unit_head *header,
1283 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1284 int is_debug_types_section);
9291a0cd
TT
1285
1286static void init_cu_die_reader (struct die_reader_specs *reader,
1287 struct dwarf2_cu *cu);
1288
673bfd45 1289static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1290
9291a0cd
TT
1291#if WORDS_BIGENDIAN
1292
1293/* Convert VALUE between big- and little-endian. */
1294static offset_type
1295byte_swap (offset_type value)
1296{
1297 offset_type result;
1298
1299 result = (value & 0xff) << 24;
1300 result |= (value & 0xff00) << 8;
1301 result |= (value & 0xff0000) >> 8;
1302 result |= (value & 0xff000000) >> 24;
1303 return result;
1304}
1305
1306#define MAYBE_SWAP(V) byte_swap (V)
1307
1308#else
1309#define MAYBE_SWAP(V) (V)
1310#endif /* WORDS_BIGENDIAN */
1311
1312/* The suffix for an index file. */
1313#define INDEX_SUFFIX ".gdb-index"
1314
3da10d80
KS
1315static const char *dwarf2_physname (char *name, struct die_info *die,
1316 struct dwarf2_cu *cu);
1317
c906108c 1318/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1319 information and return true if we have enough to do something.
1320 NAMES points to the dwarf2 section names, or is NULL if the standard
1321 ELF names are used. */
c906108c
SS
1322
1323int
251d32d9
TG
1324dwarf2_has_info (struct objfile *objfile,
1325 const struct dwarf2_debug_sections *names)
c906108c 1326{
be391dca
TT
1327 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1328 if (!dwarf2_per_objfile)
1329 {
1330 /* Initialize per-objfile state. */
1331 struct dwarf2_per_objfile *data
1332 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1333
be391dca
TT
1334 memset (data, 0, sizeof (*data));
1335 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1336 dwarf2_per_objfile = data;
6502dd73 1337
251d32d9
TG
1338 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1339 (void *) names);
be391dca
TT
1340 dwarf2_per_objfile->objfile = objfile;
1341 }
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1344}
1345
251d32d9
TG
1346/* When loading sections, we look either for uncompressed section or for
1347 compressed section names. */
233a11ab
CS
1348
1349static int
251d32d9
TG
1350section_is_p (const char *section_name,
1351 const struct dwarf2_section_names *names)
233a11ab 1352{
251d32d9
TG
1353 if (names->normal != NULL
1354 && strcmp (section_name, names->normal) == 0)
1355 return 1;
1356 if (names->compressed != NULL
1357 && strcmp (section_name, names->compressed) == 0)
1358 return 1;
1359 return 0;
233a11ab
CS
1360}
1361
c906108c
SS
1362/* This function is mapped across the sections and remembers the
1363 offset and size of each of the debugging sections we are interested
1364 in. */
1365
1366static void
251d32d9 1367dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1368{
251d32d9
TG
1369 const struct dwarf2_debug_sections *names;
1370
1371 if (vnames == NULL)
1372 names = &dwarf2_elf_names;
1373 else
1374 names = (const struct dwarf2_debug_sections *) vnames;
1375
1376 if (section_is_p (sectp->name, &names->info))
c906108c 1377 {
dce234bc
PP
1378 dwarf2_per_objfile->info.asection = sectp;
1379 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1380 }
251d32d9 1381 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->abbrev.asection = sectp;
1384 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1385 }
251d32d9 1386 else if (section_is_p (sectp->name, &names->line))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->line.asection = sectp;
1389 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1390 }
251d32d9 1391 else if (section_is_p (sectp->name, &names->loc))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->loc.asection = sectp;
1394 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1395 }
251d32d9 1396 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->macinfo.asection = sectp;
1399 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1400 }
cf2c3c16
TT
1401 else if (section_is_p (sectp->name, &names->macro))
1402 {
1403 dwarf2_per_objfile->macro.asection = sectp;
1404 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1405 }
251d32d9 1406 else if (section_is_p (sectp->name, &names->str))
c906108c 1407 {
dce234bc
PP
1408 dwarf2_per_objfile->str.asection = sectp;
1409 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1410 }
251d32d9 1411 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->frame.asection = sectp;
1414 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1415 }
251d32d9 1416 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1417 {
3799ccc6 1418 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1419
3799ccc6
EZ
1420 if (aflag & SEC_HAS_CONTENTS)
1421 {
dce234bc
PP
1422 dwarf2_per_objfile->eh_frame.asection = sectp;
1423 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1424 }
b6af0555 1425 }
251d32d9 1426 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1427 {
dce234bc
PP
1428 dwarf2_per_objfile->ranges.asection = sectp;
1429 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1430 }
251d32d9 1431 else if (section_is_p (sectp->name, &names->types))
348e048f 1432 {
8b70b953
TT
1433 struct dwarf2_section_info type_section;
1434
1435 memset (&type_section, 0, sizeof (type_section));
1436 type_section.asection = sectp;
1437 type_section.size = bfd_get_section_size (sectp);
1438
1439 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1440 &type_section);
348e048f 1441 }
251d32d9 1442 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1443 {
1444 dwarf2_per_objfile->gdb_index.asection = sectp;
1445 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1446 }
dce234bc 1447
72dca2f5
FR
1448 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1449 && bfd_section_vma (abfd, sectp) == 0)
1450 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1451}
1452
dce234bc
PP
1453/* Decompress a section that was compressed using zlib. Store the
1454 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1455
1456static void
dce234bc
PP
1457zlib_decompress_section (struct objfile *objfile, asection *sectp,
1458 gdb_byte **outbuf, bfd_size_type *outsize)
1459{
1460 bfd *abfd = objfile->obfd;
1461#ifndef HAVE_ZLIB_H
1462 error (_("Support for zlib-compressed DWARF data (from '%s') "
1463 "is disabled in this copy of GDB"),
1464 bfd_get_filename (abfd));
1465#else
1466 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1467 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1468 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1469 bfd_size_type uncompressed_size;
1470 gdb_byte *uncompressed_buffer;
1471 z_stream strm;
1472 int rc;
1473 int header_size = 12;
1474
1475 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1476 || bfd_bread (compressed_buffer,
1477 compressed_size, abfd) != compressed_size)
dce234bc
PP
1478 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1479 bfd_get_filename (abfd));
1480
1481 /* Read the zlib header. In this case, it should be "ZLIB" followed
1482 by the uncompressed section size, 8 bytes in big-endian order. */
1483 if (compressed_size < header_size
1484 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1485 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1486 bfd_get_filename (abfd));
1487 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1494 uncompressed_size += compressed_buffer[11];
1495
1496 /* It is possible the section consists of several compressed
1497 buffers concatenated together, so we uncompress in a loop. */
1498 strm.zalloc = NULL;
1499 strm.zfree = NULL;
1500 strm.opaque = NULL;
1501 strm.avail_in = compressed_size - header_size;
1502 strm.next_in = (Bytef*) compressed_buffer + header_size;
1503 strm.avail_out = uncompressed_size;
1504 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1505 uncompressed_size);
1506 rc = inflateInit (&strm);
1507 while (strm.avail_in > 0)
1508 {
1509 if (rc != Z_OK)
1510 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1511 bfd_get_filename (abfd), rc);
1512 strm.next_out = ((Bytef*) uncompressed_buffer
1513 + (uncompressed_size - strm.avail_out));
1514 rc = inflate (&strm, Z_FINISH);
1515 if (rc != Z_STREAM_END)
1516 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518 rc = inflateReset (&strm);
1519 }
1520 rc = inflateEnd (&strm);
1521 if (rc != Z_OK
1522 || strm.avail_out != 0)
1523 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1524 bfd_get_filename (abfd), rc);
1525
affddf13 1526 do_cleanups (cleanup);
dce234bc
PP
1527 *outbuf = uncompressed_buffer;
1528 *outsize = uncompressed_size;
1529#endif
233a11ab
CS
1530}
1531
9e0ac564
TT
1532/* A helper function that decides whether a section is empty. */
1533
1534static int
1535dwarf2_section_empty_p (struct dwarf2_section_info *info)
1536{
1537 return info->asection == NULL || info->size == 0;
1538}
1539
9cdd5dbd 1540/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1541 OBJFILE, store info about the section into INFO.
1542 If the section is compressed, uncompress it before returning. */
c906108c 1543
dce234bc
PP
1544static void
1545dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1546{
dce234bc
PP
1547 bfd *abfd = objfile->obfd;
1548 asection *sectp = info->asection;
1549 gdb_byte *buf, *retbuf;
1550 unsigned char header[4];
c906108c 1551
be391dca
TT
1552 if (info->readin)
1553 return;
dce234bc 1554 info->buffer = NULL;
b315ab21 1555 info->map_addr = NULL;
be391dca 1556 info->readin = 1;
188dd5d6 1557
9e0ac564 1558 if (dwarf2_section_empty_p (info))
dce234bc 1559 return;
c906108c 1560
dce234bc
PP
1561 /* Check if the file has a 4-byte header indicating compression. */
1562 if (info->size > sizeof (header)
1563 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1564 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1565 {
1566 /* Upon decompression, update the buffer and its size. */
1567 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1568 {
1569 zlib_decompress_section (objfile, sectp, &info->buffer,
1570 &info->size);
1571 return;
1572 }
1573 }
4bdf3d34 1574
dce234bc
PP
1575#ifdef HAVE_MMAP
1576 if (pagesize == 0)
1577 pagesize = getpagesize ();
2e276125 1578
dce234bc
PP
1579 /* Only try to mmap sections which are large enough: we don't want to
1580 waste space due to fragmentation. Also, only try mmap for sections
1581 without relocations. */
1582
1583 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1584 {
b315ab21
TG
1585 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1586 MAP_PRIVATE, sectp->filepos,
1587 &info->map_addr, &info->map_len);
dce234bc 1588
b315ab21 1589 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1590 {
be391dca 1591#if HAVE_POSIX_MADVISE
b315ab21 1592 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1593#endif
dce234bc
PP
1594 return;
1595 }
1596 }
1597#endif
1598
1599 /* If we get here, we are a normal, not-compressed section. */
1600 info->buffer = buf
1601 = obstack_alloc (&objfile->objfile_obstack, info->size);
1602
1603 /* When debugging .o files, we may need to apply relocations; see
1604 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1605 We never compress sections in .o files, so we only need to
1606 try this when the section is not compressed. */
ac8035ab 1607 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1608 if (retbuf != NULL)
1609 {
1610 info->buffer = retbuf;
1611 return;
1612 }
1613
1614 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1615 || bfd_bread (buf, info->size, abfd) != info->size)
1616 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1617 bfd_get_filename (abfd));
1618}
1619
9e0ac564
TT
1620/* A helper function that returns the size of a section in a safe way.
1621 If you are positive that the section has been read before using the
1622 size, then it is safe to refer to the dwarf2_section_info object's
1623 "size" field directly. In other cases, you must call this
1624 function, because for compressed sections the size field is not set
1625 correctly until the section has been read. */
1626
1627static bfd_size_type
1628dwarf2_section_size (struct objfile *objfile,
1629 struct dwarf2_section_info *info)
1630{
1631 if (!info->readin)
1632 dwarf2_read_section (objfile, info);
1633 return info->size;
1634}
1635
dce234bc 1636/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1637 SECTION_NAME. */
af34e669 1638
dce234bc 1639void
3017a003
TG
1640dwarf2_get_section_info (struct objfile *objfile,
1641 enum dwarf2_section_enum sect,
dce234bc
PP
1642 asection **sectp, gdb_byte **bufp,
1643 bfd_size_type *sizep)
1644{
1645 struct dwarf2_per_objfile *data
1646 = objfile_data (objfile, dwarf2_objfile_data_key);
1647 struct dwarf2_section_info *info;
a3b2a86b
TT
1648
1649 /* We may see an objfile without any DWARF, in which case we just
1650 return nothing. */
1651 if (data == NULL)
1652 {
1653 *sectp = NULL;
1654 *bufp = NULL;
1655 *sizep = 0;
1656 return;
1657 }
3017a003
TG
1658 switch (sect)
1659 {
1660 case DWARF2_DEBUG_FRAME:
1661 info = &data->frame;
1662 break;
1663 case DWARF2_EH_FRAME:
1664 info = &data->eh_frame;
1665 break;
1666 default:
1667 gdb_assert_not_reached ("unexpected section");
1668 }
dce234bc 1669
9e0ac564 1670 dwarf2_read_section (objfile, info);
dce234bc
PP
1671
1672 *sectp = info->asection;
1673 *bufp = info->buffer;
1674 *sizep = info->size;
1675}
1676
9291a0cd 1677\f
7b9f3c50
DE
1678/* DWARF quick_symbols_functions support. */
1679
1680/* TUs can share .debug_line entries, and there can be a lot more TUs than
1681 unique line tables, so we maintain a separate table of all .debug_line
1682 derived entries to support the sharing.
1683 All the quick functions need is the list of file names. We discard the
1684 line_header when we're done and don't need to record it here. */
1685struct quick_file_names
1686{
1687 /* The offset in .debug_line of the line table. We hash on this. */
1688 unsigned int offset;
1689
1690 /* The number of entries in file_names, real_names. */
1691 unsigned int num_file_names;
1692
1693 /* The file names from the line table, after being run through
1694 file_full_name. */
1695 const char **file_names;
1696
1697 /* The file names from the line table after being run through
1698 gdb_realpath. These are computed lazily. */
1699 const char **real_names;
1700};
1701
1702/* When using the index (and thus not using psymtabs), each CU has an
1703 object of this type. This is used to hold information needed by
1704 the various "quick" methods. */
1705struct dwarf2_per_cu_quick_data
1706{
1707 /* The file table. This can be NULL if there was no file table
1708 or it's currently not read in.
1709 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1710 struct quick_file_names *file_names;
1711
1712 /* The corresponding symbol table. This is NULL if symbols for this
1713 CU have not yet been read. */
1714 struct symtab *symtab;
1715
1716 /* A temporary mark bit used when iterating over all CUs in
1717 expand_symtabs_matching. */
1718 unsigned int mark : 1;
1719
1720 /* True if we've tried to read the file table and found there isn't one.
1721 There will be no point in trying to read it again next time. */
1722 unsigned int no_file_data : 1;
1723};
1724
1725/* Hash function for a quick_file_names. */
1726
1727static hashval_t
1728hash_file_name_entry (const void *e)
1729{
1730 const struct quick_file_names *file_data = e;
1731
1732 return file_data->offset;
1733}
1734
1735/* Equality function for a quick_file_names. */
1736
1737static int
1738eq_file_name_entry (const void *a, const void *b)
1739{
1740 const struct quick_file_names *ea = a;
1741 const struct quick_file_names *eb = b;
1742
1743 return ea->offset == eb->offset;
1744}
1745
1746/* Delete function for a quick_file_names. */
1747
1748static void
1749delete_file_name_entry (void *e)
1750{
1751 struct quick_file_names *file_data = e;
1752 int i;
1753
1754 for (i = 0; i < file_data->num_file_names; ++i)
1755 {
1756 xfree ((void*) file_data->file_names[i]);
1757 if (file_data->real_names)
1758 xfree ((void*) file_data->real_names[i]);
1759 }
1760
1761 /* The space for the struct itself lives on objfile_obstack,
1762 so we don't free it here. */
1763}
1764
1765/* Create a quick_file_names hash table. */
1766
1767static htab_t
1768create_quick_file_names_table (unsigned int nr_initial_entries)
1769{
1770 return htab_create_alloc (nr_initial_entries,
1771 hash_file_name_entry, eq_file_name_entry,
1772 delete_file_name_entry, xcalloc, xfree);
1773}
9291a0cd 1774
918dd910
JK
1775/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1776 have to be created afterwards. You should call age_cached_comp_units after
1777 processing PER_CU->CU. dw2_setup must have been already called. */
1778
1779static void
1780load_cu (struct dwarf2_per_cu_data *per_cu)
1781{
b0df02fd 1782 if (per_cu->debug_types_section)
e5fe5e75 1783 load_full_type_unit (per_cu);
918dd910 1784 else
a0f42c21 1785 load_full_comp_unit (per_cu);
918dd910 1786
918dd910 1787 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1788
1789 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1790}
1791
a0f42c21 1792/* Read in the symbols for PER_CU. */
2fdf6df6 1793
9291a0cd 1794static void
a0f42c21 1795dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1796{
1797 struct cleanup *back_to;
1798
1799 back_to = make_cleanup (dwarf2_release_queue, NULL);
1800
a0f42c21 1801 queue_comp_unit (per_cu);
9291a0cd 1802
918dd910 1803 load_cu (per_cu);
9291a0cd 1804
a0f42c21 1805 process_queue ();
9291a0cd
TT
1806
1807 /* Age the cache, releasing compilation units that have not
1808 been used recently. */
1809 age_cached_comp_units ();
1810
1811 do_cleanups (back_to);
1812}
1813
1814/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1815 the objfile from which this CU came. Returns the resulting symbol
1816 table. */
2fdf6df6 1817
9291a0cd 1818static struct symtab *
a0f42c21 1819dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1820{
1821 if (!per_cu->v.quick->symtab)
1822 {
1823 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1824 increment_reading_symtab ();
a0f42c21 1825 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1826 do_cleanups (back_to);
1827 }
1828 return per_cu->v.quick->symtab;
1829}
1830
1fd400ff 1831/* Return the CU given its index. */
2fdf6df6 1832
1fd400ff
TT
1833static struct dwarf2_per_cu_data *
1834dw2_get_cu (int index)
1835{
1836 if (index >= dwarf2_per_objfile->n_comp_units)
1837 {
1838 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1839 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1840 }
1841 return dwarf2_per_objfile->all_comp_units[index];
1842}
1843
9291a0cd
TT
1844/* A helper function that knows how to read a 64-bit value in a way
1845 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1846 otherwise. */
2fdf6df6 1847
9291a0cd
TT
1848static int
1849extract_cu_value (const char *bytes, ULONGEST *result)
1850{
1851 if (sizeof (ULONGEST) < 8)
1852 {
1853 int i;
1854
1855 /* Ignore the upper 4 bytes if they are all zero. */
1856 for (i = 0; i < 4; ++i)
1857 if (bytes[i + 4] != 0)
1858 return 0;
1859
1860 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1861 }
1862 else
1863 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1864 return 1;
1865}
1866
1867/* Read the CU list from the mapped index, and use it to create all
1868 the CU objects for this objfile. Return 0 if something went wrong,
1869 1 if everything went ok. */
2fdf6df6 1870
9291a0cd 1871static int
1fd400ff
TT
1872create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1873 offset_type cu_list_elements)
9291a0cd
TT
1874{
1875 offset_type i;
9291a0cd
TT
1876
1877 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1878 dwarf2_per_objfile->all_comp_units
1879 = obstack_alloc (&objfile->objfile_obstack,
1880 dwarf2_per_objfile->n_comp_units
1881 * sizeof (struct dwarf2_per_cu_data *));
1882
1883 for (i = 0; i < cu_list_elements; i += 2)
1884 {
1885 struct dwarf2_per_cu_data *the_cu;
1886 ULONGEST offset, length;
1887
1888 if (!extract_cu_value (cu_list, &offset)
1889 || !extract_cu_value (cu_list + 8, &length))
1890 return 0;
1891 cu_list += 2 * 8;
1892
1893 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1894 struct dwarf2_per_cu_data);
b64f50a1 1895 the_cu->offset.sect_off = offset;
9291a0cd
TT
1896 the_cu->length = length;
1897 the_cu->objfile = objfile;
1898 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1899 struct dwarf2_per_cu_quick_data);
1900 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1901 }
1902
1903 return 1;
1904}
1905
1fd400ff 1906/* Create the signatured type hash table from the index. */
673bfd45 1907
1fd400ff 1908static int
673bfd45 1909create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1910 struct dwarf2_section_info *section,
673bfd45
DE
1911 const gdb_byte *bytes,
1912 offset_type elements)
1fd400ff
TT
1913{
1914 offset_type i;
673bfd45 1915 htab_t sig_types_hash;
1fd400ff 1916
d467dd73
DE
1917 dwarf2_per_objfile->n_type_units = elements / 3;
1918 dwarf2_per_objfile->all_type_units
1fd400ff 1919 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1920 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1921 * sizeof (struct dwarf2_per_cu_data *));
1922
673bfd45 1923 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1924
1925 for (i = 0; i < elements; i += 3)
1926 {
1927 struct signatured_type *type_sig;
1928 ULONGEST offset, type_offset, signature;
1929 void **slot;
1930
1931 if (!extract_cu_value (bytes, &offset)
1932 || !extract_cu_value (bytes + 8, &type_offset))
1933 return 0;
1934 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1935 bytes += 3 * 8;
1936
1937 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1938 struct signatured_type);
1939 type_sig->signature = signature;
b64f50a1 1940 type_sig->type_offset.cu_off = type_offset;
b0df02fd 1941 type_sig->per_cu.debug_types_section = section;
b64f50a1 1942 type_sig->per_cu.offset.sect_off = offset;
1fd400ff
TT
1943 type_sig->per_cu.objfile = objfile;
1944 type_sig->per_cu.v.quick
1945 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1946 struct dwarf2_per_cu_quick_data);
1947
673bfd45 1948 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1949 *slot = type_sig;
1950
d467dd73 1951 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1fd400ff
TT
1952 }
1953
673bfd45 1954 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1955
1956 return 1;
1957}
1958
9291a0cd
TT
1959/* Read the address map data from the mapped index, and use it to
1960 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1961
9291a0cd
TT
1962static void
1963create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1964{
1965 const gdb_byte *iter, *end;
1966 struct obstack temp_obstack;
1967 struct addrmap *mutable_map;
1968 struct cleanup *cleanup;
1969 CORE_ADDR baseaddr;
1970
1971 obstack_init (&temp_obstack);
1972 cleanup = make_cleanup_obstack_free (&temp_obstack);
1973 mutable_map = addrmap_create_mutable (&temp_obstack);
1974
1975 iter = index->address_table;
1976 end = iter + index->address_table_size;
1977
1978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1979
1980 while (iter < end)
1981 {
1982 ULONGEST hi, lo, cu_index;
1983 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984 iter += 8;
1985 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1986 iter += 8;
1987 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1988 iter += 4;
1989
1990 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1991 dw2_get_cu (cu_index));
9291a0cd
TT
1992 }
1993
1994 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1995 &objfile->objfile_obstack);
1996 do_cleanups (cleanup);
1997}
1998
59d7bcaf
JK
1999/* The hash function for strings in the mapped index. This is the same as
2000 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2001 implementation. This is necessary because the hash function is tied to the
2002 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2003 SYMBOL_HASH_NEXT.
2004
2005 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2006
9291a0cd 2007static hashval_t
559a7a62 2008mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2009{
2010 const unsigned char *str = (const unsigned char *) p;
2011 hashval_t r = 0;
2012 unsigned char c;
2013
2014 while ((c = *str++) != 0)
559a7a62
JK
2015 {
2016 if (index_version >= 5)
2017 c = tolower (c);
2018 r = r * 67 + c - 113;
2019 }
9291a0cd
TT
2020
2021 return r;
2022}
2023
2024/* Find a slot in the mapped index INDEX for the object named NAME.
2025 If NAME is found, set *VEC_OUT to point to the CU vector in the
2026 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2027
9291a0cd
TT
2028static int
2029find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2030 offset_type **vec_out)
2031{
0cf03b49
JK
2032 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2033 offset_type hash;
9291a0cd 2034 offset_type slot, step;
559a7a62 2035 int (*cmp) (const char *, const char *);
9291a0cd 2036
0cf03b49
JK
2037 if (current_language->la_language == language_cplus
2038 || current_language->la_language == language_java
2039 || current_language->la_language == language_fortran)
2040 {
2041 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2042 not contain any. */
2043 const char *paren = strchr (name, '(');
2044
2045 if (paren)
2046 {
2047 char *dup;
2048
2049 dup = xmalloc (paren - name + 1);
2050 memcpy (dup, name, paren - name);
2051 dup[paren - name] = 0;
2052
2053 make_cleanup (xfree, dup);
2054 name = dup;
2055 }
2056 }
2057
559a7a62 2058 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2059 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2060 simulate our NAME being searched is also lowercased. */
2061 hash = mapped_index_string_hash ((index->version == 4
2062 && case_sensitivity == case_sensitive_off
2063 ? 5 : index->version),
2064 name);
2065
3876f04e
DE
2066 slot = hash & (index->symbol_table_slots - 1);
2067 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2068 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2069
2070 for (;;)
2071 {
2072 /* Convert a slot number to an offset into the table. */
2073 offset_type i = 2 * slot;
2074 const char *str;
3876f04e 2075 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2076 {
2077 do_cleanups (back_to);
2078 return 0;
2079 }
9291a0cd 2080
3876f04e 2081 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2082 if (!cmp (name, str))
9291a0cd
TT
2083 {
2084 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2085 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2086 do_cleanups (back_to);
9291a0cd
TT
2087 return 1;
2088 }
2089
3876f04e 2090 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2091 }
2092}
2093
2094/* Read the index file. If everything went ok, initialize the "quick"
2095 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2096
9291a0cd
TT
2097static int
2098dwarf2_read_index (struct objfile *objfile)
2099{
9291a0cd
TT
2100 char *addr;
2101 struct mapped_index *map;
b3b272e1 2102 offset_type *metadata;
ac0b195c
KW
2103 const gdb_byte *cu_list;
2104 const gdb_byte *types_list = NULL;
2105 offset_type version, cu_list_elements;
2106 offset_type types_list_elements = 0;
1fd400ff 2107 int i;
9291a0cd 2108
9e0ac564 2109 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2110 return 0;
82430852
JK
2111
2112 /* Older elfutils strip versions could keep the section in the main
2113 executable while splitting it for the separate debug info file. */
2114 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2115 & SEC_HAS_CONTENTS) == 0)
2116 return 0;
2117
9291a0cd
TT
2118 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2119
2120 addr = dwarf2_per_objfile->gdb_index.buffer;
2121 /* Version check. */
1fd400ff 2122 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2123 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2124 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2125 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2126 indices. */
831adc1f 2127 if (version < 4)
481860b3
GB
2128 {
2129 static int warning_printed = 0;
2130 if (!warning_printed)
2131 {
2132 warning (_("Skipping obsolete .gdb_index section in %s."),
2133 objfile->name);
2134 warning_printed = 1;
2135 }
2136 return 0;
2137 }
2138 /* Index version 4 uses a different hash function than index version
2139 5 and later.
2140
2141 Versions earlier than 6 did not emit psymbols for inlined
2142 functions. Using these files will cause GDB not to be able to
2143 set breakpoints on inlined functions by name, so we ignore these
2144 indices unless the --use-deprecated-index-sections command line
2145 option was supplied. */
2146 if (version < 6 && !use_deprecated_index_sections)
2147 {
2148 static int warning_printed = 0;
2149 if (!warning_printed)
2150 {
2151 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2152 "--use-deprecated-index-sections to use them anyway"),
2153 objfile->name);
2154 warning_printed = 1;
2155 }
2156 return 0;
2157 }
2158 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2159 longer backward compatible. */
481860b3 2160 if (version > 6)
594e8718 2161 return 0;
9291a0cd
TT
2162
2163 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2164 map->version = version;
b3b272e1 2165 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2166
2167 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2168
2169 i = 0;
2170 cu_list = addr + MAYBE_SWAP (metadata[i]);
2171 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2172 / 8);
1fd400ff
TT
2173 ++i;
2174
987d643c
TT
2175 types_list = addr + MAYBE_SWAP (metadata[i]);
2176 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2177 - MAYBE_SWAP (metadata[i]))
2178 / 8);
2179 ++i;
1fd400ff
TT
2180
2181 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2182 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2183 - MAYBE_SWAP (metadata[i]));
2184 ++i;
2185
3876f04e
DE
2186 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2187 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2188 - MAYBE_SWAP (metadata[i]))
2189 / (2 * sizeof (offset_type)));
1fd400ff 2190 ++i;
9291a0cd 2191
1fd400ff
TT
2192 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2193
2194 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2195 return 0;
2196
8b70b953
TT
2197 if (types_list_elements)
2198 {
2199 struct dwarf2_section_info *section;
2200
2201 /* We can only handle a single .debug_types when we have an
2202 index. */
2203 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2204 return 0;
2205
2206 section = VEC_index (dwarf2_section_info_def,
2207 dwarf2_per_objfile->types, 0);
2208
2209 if (!create_signatured_type_table_from_index (objfile, section,
2210 types_list,
2211 types_list_elements))
2212 return 0;
2213 }
9291a0cd
TT
2214
2215 create_addrmap_from_index (objfile, map);
2216
2217 dwarf2_per_objfile->index_table = map;
2218 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2219 dwarf2_per_objfile->quick_file_names_table =
2220 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2221
2222 return 1;
2223}
2224
2225/* A helper for the "quick" functions which sets the global
2226 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2227
9291a0cd
TT
2228static void
2229dw2_setup (struct objfile *objfile)
2230{
2231 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2232 gdb_assert (dwarf2_per_objfile);
2233}
2234
2235/* A helper for the "quick" functions which attempts to read the line
2236 table for THIS_CU. */
2fdf6df6 2237
7b9f3c50
DE
2238static struct quick_file_names *
2239dw2_get_file_names (struct objfile *objfile,
2240 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2241{
2242 bfd *abfd = objfile->obfd;
7b9f3c50 2243 struct line_header *lh;
9291a0cd
TT
2244 struct attribute *attr;
2245 struct cleanup *cleanups;
2246 struct die_info *comp_unit_die;
36374493 2247 struct dwarf2_section_info* sec;
9ff913ba 2248 gdb_byte *info_ptr;
9291a0cd
TT
2249 int has_children, i;
2250 struct dwarf2_cu cu;
9ff913ba 2251 unsigned int bytes_read;
9291a0cd
TT
2252 struct die_reader_specs reader_specs;
2253 char *name, *comp_dir;
7b9f3c50
DE
2254 void **slot;
2255 struct quick_file_names *qfn;
2256 unsigned int line_offset;
9291a0cd 2257
7b9f3c50
DE
2258 if (this_cu->v.quick->file_names != NULL)
2259 return this_cu->v.quick->file_names;
2260 /* If we know there is no line data, no point in looking again. */
2261 if (this_cu->v.quick->no_file_data)
2262 return NULL;
9291a0cd 2263
23745b47 2264 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2265 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2266
b0df02fd
DE
2267 if (this_cu->debug_types_section)
2268 sec = this_cu->debug_types_section;
36374493
DE
2269 else
2270 sec = &dwarf2_per_objfile->info;
2271 dwarf2_read_section (objfile, sec);
b64f50a1 2272 info_ptr = sec->buffer + this_cu->offset.sect_off;
9291a0cd 2273
9ff913ba
DE
2274 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2275 this_cu->debug_types_section != NULL);
9291a0cd 2276
6caca83c 2277 /* Skip dummy compilation units. */
9ff913ba 2278 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2279 || peek_abbrev_code (abfd, info_ptr) == 0)
2280 {
2281 do_cleanups (cleanups);
2282 return NULL;
2283 }
2284
e5fe5e75 2285 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2286 make_cleanup (dwarf2_free_abbrev_table, &cu);
2287
9291a0cd 2288 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2289 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2290 &has_children);
9291a0cd 2291
7b9f3c50
DE
2292 lh = NULL;
2293 slot = NULL;
2294 line_offset = 0;
9291a0cd
TT
2295 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2296 if (attr)
2297 {
7b9f3c50
DE
2298 struct quick_file_names find_entry;
2299
2300 line_offset = DW_UNSND (attr);
2301
2302 /* We may have already read in this line header (TU line header sharing).
2303 If we have we're done. */
2304 find_entry.offset = line_offset;
2305 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2306 &find_entry, INSERT);
2307 if (*slot != NULL)
2308 {
2309 do_cleanups (cleanups);
2310 this_cu->v.quick->file_names = *slot;
2311 return *slot;
2312 }
2313
9291a0cd
TT
2314 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2315 }
2316 if (lh == NULL)
2317 {
2318 do_cleanups (cleanups);
7b9f3c50
DE
2319 this_cu->v.quick->no_file_data = 1;
2320 return NULL;
9291a0cd
TT
2321 }
2322
7b9f3c50
DE
2323 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2324 qfn->offset = line_offset;
2325 gdb_assert (slot != NULL);
2326 *slot = qfn;
9291a0cd 2327
7b9f3c50 2328 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2329
7b9f3c50
DE
2330 qfn->num_file_names = lh->num_file_names;
2331 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2332 lh->num_file_names * sizeof (char *));
9291a0cd 2333 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2334 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2335 qfn->real_names = NULL;
9291a0cd 2336
7b9f3c50 2337 free_line_header (lh);
9291a0cd 2338 do_cleanups (cleanups);
7b9f3c50
DE
2339
2340 this_cu->v.quick->file_names = qfn;
2341 return qfn;
9291a0cd
TT
2342}
2343
2344/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2345 real path for a given file name from the line table. */
2fdf6df6 2346
9291a0cd 2347static const char *
7b9f3c50
DE
2348dw2_get_real_path (struct objfile *objfile,
2349 struct quick_file_names *qfn, int index)
9291a0cd 2350{
7b9f3c50
DE
2351 if (qfn->real_names == NULL)
2352 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2353 qfn->num_file_names, sizeof (char *));
9291a0cd 2354
7b9f3c50
DE
2355 if (qfn->real_names[index] == NULL)
2356 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2357
7b9f3c50 2358 return qfn->real_names[index];
9291a0cd
TT
2359}
2360
2361static struct symtab *
2362dw2_find_last_source_symtab (struct objfile *objfile)
2363{
2364 int index;
ae2de4f8 2365
9291a0cd
TT
2366 dw2_setup (objfile);
2367 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2368 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2369}
2370
7b9f3c50
DE
2371/* Traversal function for dw2_forget_cached_source_info. */
2372
2373static int
2374dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2375{
7b9f3c50 2376 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2377
7b9f3c50 2378 if (file_data->real_names)
9291a0cd 2379 {
7b9f3c50 2380 int i;
9291a0cd 2381
7b9f3c50 2382 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2383 {
7b9f3c50
DE
2384 xfree ((void*) file_data->real_names[i]);
2385 file_data->real_names[i] = NULL;
9291a0cd
TT
2386 }
2387 }
7b9f3c50
DE
2388
2389 return 1;
2390}
2391
2392static void
2393dw2_forget_cached_source_info (struct objfile *objfile)
2394{
2395 dw2_setup (objfile);
2396
2397 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2398 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2399}
2400
f8eba3c6
TT
2401/* Helper function for dw2_map_symtabs_matching_filename that expands
2402 the symtabs and calls the iterator. */
2403
2404static int
2405dw2_map_expand_apply (struct objfile *objfile,
2406 struct dwarf2_per_cu_data *per_cu,
2407 const char *name,
2408 const char *full_path, const char *real_path,
2409 int (*callback) (struct symtab *, void *),
2410 void *data)
2411{
2412 struct symtab *last_made = objfile->symtabs;
2413
2414 /* Don't visit already-expanded CUs. */
2415 if (per_cu->v.quick->symtab)
2416 return 0;
2417
2418 /* This may expand more than one symtab, and we want to iterate over
2419 all of them. */
a0f42c21 2420 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2421
2422 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2423 objfile->symtabs, last_made);
2424}
2425
2426/* Implementation of the map_symtabs_matching_filename method. */
2427
9291a0cd 2428static int
f8eba3c6
TT
2429dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2430 const char *full_path, const char *real_path,
2431 int (*callback) (struct symtab *, void *),
2432 void *data)
9291a0cd
TT
2433{
2434 int i;
c011a4f4 2435 const char *name_basename = lbasename (name);
4aac40c8
TT
2436 int name_len = strlen (name);
2437 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2438
2439 dw2_setup (objfile);
ae2de4f8 2440
1fd400ff 2441 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2442 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2443 {
2444 int j;
e254ef6a 2445 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2446 struct quick_file_names *file_data;
9291a0cd 2447
3d7bb9d9 2448 /* We only need to look at symtabs not already expanded. */
e254ef6a 2449 if (per_cu->v.quick->symtab)
9291a0cd
TT
2450 continue;
2451
7b9f3c50
DE
2452 file_data = dw2_get_file_names (objfile, per_cu);
2453 if (file_data == NULL)
9291a0cd
TT
2454 continue;
2455
7b9f3c50 2456 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2457 {
7b9f3c50 2458 const char *this_name = file_data->file_names[j];
9291a0cd 2459
4aac40c8
TT
2460 if (FILENAME_CMP (name, this_name) == 0
2461 || (!is_abs && compare_filenames_for_search (this_name,
2462 name, name_len)))
9291a0cd 2463 {
f8eba3c6
TT
2464 if (dw2_map_expand_apply (objfile, per_cu,
2465 name, full_path, real_path,
2466 callback, data))
2467 return 1;
4aac40c8 2468 }
9291a0cd 2469
c011a4f4
DE
2470 /* Before we invoke realpath, which can get expensive when many
2471 files are involved, do a quick comparison of the basenames. */
2472 if (! basenames_may_differ
2473 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2474 continue;
2475
9291a0cd
TT
2476 if (full_path != NULL)
2477 {
7b9f3c50
DE
2478 const char *this_real_name = dw2_get_real_path (objfile,
2479 file_data, j);
9291a0cd 2480
7b9f3c50 2481 if (this_real_name != NULL
4aac40c8
TT
2482 && (FILENAME_CMP (full_path, this_real_name) == 0
2483 || (!is_abs
2484 && compare_filenames_for_search (this_real_name,
2485 name, name_len))))
9291a0cd 2486 {
f8eba3c6
TT
2487 if (dw2_map_expand_apply (objfile, per_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
9291a0cd
TT
2491 }
2492 }
2493
2494 if (real_path != NULL)
2495 {
7b9f3c50
DE
2496 const char *this_real_name = dw2_get_real_path (objfile,
2497 file_data, j);
9291a0cd 2498
7b9f3c50 2499 if (this_real_name != NULL
4aac40c8
TT
2500 && (FILENAME_CMP (real_path, this_real_name) == 0
2501 || (!is_abs
2502 && compare_filenames_for_search (this_real_name,
2503 name, name_len))))
9291a0cd 2504 {
f8eba3c6
TT
2505 if (dw2_map_expand_apply (objfile, per_cu,
2506 name, full_path, real_path,
2507 callback, data))
2508 return 1;
9291a0cd
TT
2509 }
2510 }
2511 }
2512 }
2513
9291a0cd
TT
2514 return 0;
2515}
2516
2517static struct symtab *
2518dw2_lookup_symbol (struct objfile *objfile, int block_index,
2519 const char *name, domain_enum domain)
2520{
774b6a14 2521 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2522 instead. */
2523 return NULL;
2524}
2525
2526/* A helper function that expands all symtabs that hold an object
2527 named NAME. */
2fdf6df6 2528
9291a0cd
TT
2529static void
2530dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2531{
2532 dw2_setup (objfile);
2533
ae2de4f8 2534 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2535 if (dwarf2_per_objfile->index_table)
2536 {
2537 offset_type *vec;
2538
2539 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2540 name, &vec))
2541 {
2542 offset_type i, len = MAYBE_SWAP (*vec);
2543 for (i = 0; i < len; ++i)
2544 {
2545 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2546 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2547
a0f42c21 2548 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2549 }
2550 }
2551 }
2552}
2553
774b6a14
TT
2554static void
2555dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2556 enum block_enum block_kind, const char *name,
774b6a14 2557 domain_enum domain)
9291a0cd 2558{
774b6a14 2559 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2560}
2561
2562static void
2563dw2_print_stats (struct objfile *objfile)
2564{
2565 int i, count;
2566
2567 dw2_setup (objfile);
2568 count = 0;
1fd400ff 2569 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2570 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2571 {
e254ef6a 2572 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2573
e254ef6a 2574 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2575 ++count;
2576 }
2577 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2578}
2579
2580static void
2581dw2_dump (struct objfile *objfile)
2582{
2583 /* Nothing worth printing. */
2584}
2585
2586static void
2587dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2588 struct section_offsets *delta)
2589{
2590 /* There's nothing to relocate here. */
2591}
2592
2593static void
2594dw2_expand_symtabs_for_function (struct objfile *objfile,
2595 const char *func_name)
2596{
2597 dw2_do_expand_symtabs_matching (objfile, func_name);
2598}
2599
2600static void
2601dw2_expand_all_symtabs (struct objfile *objfile)
2602{
2603 int i;
2604
2605 dw2_setup (objfile);
1fd400ff
TT
2606
2607 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2608 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2609 {
e254ef6a 2610 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2611
a0f42c21 2612 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2613 }
2614}
2615
2616static void
2617dw2_expand_symtabs_with_filename (struct objfile *objfile,
2618 const char *filename)
2619{
2620 int i;
2621
2622 dw2_setup (objfile);
d4637a04
DE
2623
2624 /* We don't need to consider type units here.
2625 This is only called for examining code, e.g. expand_line_sal.
2626 There can be an order of magnitude (or more) more type units
2627 than comp units, and we avoid them if we can. */
2628
2629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2630 {
2631 int j;
e254ef6a 2632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2633 struct quick_file_names *file_data;
9291a0cd 2634
3d7bb9d9 2635 /* We only need to look at symtabs not already expanded. */
e254ef6a 2636 if (per_cu->v.quick->symtab)
9291a0cd
TT
2637 continue;
2638
7b9f3c50
DE
2639 file_data = dw2_get_file_names (objfile, per_cu);
2640 if (file_data == NULL)
9291a0cd
TT
2641 continue;
2642
7b9f3c50 2643 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2644 {
7b9f3c50 2645 const char *this_name = file_data->file_names[j];
1ef75ecc 2646 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2647 {
a0f42c21 2648 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2649 break;
2650 }
2651 }
2652 }
2653}
2654
dd786858 2655static const char *
9291a0cd
TT
2656dw2_find_symbol_file (struct objfile *objfile, const char *name)
2657{
e254ef6a 2658 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2659 offset_type *vec;
7b9f3c50 2660 struct quick_file_names *file_data;
9291a0cd
TT
2661
2662 dw2_setup (objfile);
2663
ae2de4f8 2664 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2665 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2666 {
2667 struct symtab *s;
2668
2669 ALL_OBJFILE_SYMTABS (objfile, s)
2670 if (s->primary)
2671 {
2672 struct blockvector *bv = BLOCKVECTOR (s);
2673 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2674 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2675
2676 if (sym)
2677 return sym->symtab->filename;
2678 }
2679 return NULL;
2680 }
9291a0cd
TT
2681
2682 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2683 name, &vec))
2684 return NULL;
2685
2686 /* Note that this just looks at the very first one named NAME -- but
2687 actually we are looking for a function. find_main_filename
2688 should be rewritten so that it doesn't require a custom hook. It
2689 could just use the ordinary symbol tables. */
2690 /* vec[0] is the length, which must always be >0. */
e254ef6a 2691 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2692
7b9f3c50
DE
2693 file_data = dw2_get_file_names (objfile, per_cu);
2694 if (file_data == NULL)
9291a0cd
TT
2695 return NULL;
2696
7b9f3c50 2697 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2698}
2699
2700static void
40658b94
PH
2701dw2_map_matching_symbols (const char * name, domain_enum namespace,
2702 struct objfile *objfile, int global,
2703 int (*callback) (struct block *,
2704 struct symbol *, void *),
2edb89d3
JK
2705 void *data, symbol_compare_ftype *match,
2706 symbol_compare_ftype *ordered_compare)
9291a0cd 2707{
40658b94 2708 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2709 current language is Ada for a non-Ada objfile using GNU index. As Ada
2710 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2711}
2712
2713static void
f8eba3c6
TT
2714dw2_expand_symtabs_matching
2715 (struct objfile *objfile,
2716 int (*file_matcher) (const char *, void *),
e078317b 2717 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2718 enum search_domain kind,
2719 void *data)
9291a0cd
TT
2720{
2721 int i;
2722 offset_type iter;
4b5246aa 2723 struct mapped_index *index;
9291a0cd
TT
2724
2725 dw2_setup (objfile);
ae2de4f8
DE
2726
2727 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2728 if (!dwarf2_per_objfile->index_table)
2729 return;
4b5246aa 2730 index = dwarf2_per_objfile->index_table;
9291a0cd 2731
7b08b9eb 2732 if (file_matcher != NULL)
24c79950
TT
2733 {
2734 struct cleanup *cleanup;
2735 htab_t visited_found, visited_not_found;
2736
2737 visited_found = htab_create_alloc (10,
2738 htab_hash_pointer, htab_eq_pointer,
2739 NULL, xcalloc, xfree);
2740 cleanup = make_cleanup_htab_delete (visited_found);
2741 visited_not_found = htab_create_alloc (10,
2742 htab_hash_pointer, htab_eq_pointer,
2743 NULL, xcalloc, xfree);
2744 make_cleanup_htab_delete (visited_not_found);
2745
2746 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2747 + dwarf2_per_objfile->n_type_units); ++i)
2748 {
2749 int j;
2750 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2751 struct quick_file_names *file_data;
2752 void **slot;
7b08b9eb 2753
24c79950 2754 per_cu->v.quick->mark = 0;
3d7bb9d9 2755
24c79950
TT
2756 /* We only need to look at symtabs not already expanded. */
2757 if (per_cu->v.quick->symtab)
2758 continue;
7b08b9eb 2759
24c79950
TT
2760 file_data = dw2_get_file_names (objfile, per_cu);
2761 if (file_data == NULL)
2762 continue;
7b08b9eb 2763
24c79950
TT
2764 if (htab_find (visited_not_found, file_data) != NULL)
2765 continue;
2766 else if (htab_find (visited_found, file_data) != NULL)
2767 {
2768 per_cu->v.quick->mark = 1;
2769 continue;
2770 }
2771
2772 for (j = 0; j < file_data->num_file_names; ++j)
2773 {
2774 if (file_matcher (file_data->file_names[j], data))
2775 {
2776 per_cu->v.quick->mark = 1;
2777 break;
2778 }
2779 }
2780
2781 slot = htab_find_slot (per_cu->v.quick->mark
2782 ? visited_found
2783 : visited_not_found,
2784 file_data, INSERT);
2785 *slot = file_data;
2786 }
2787
2788 do_cleanups (cleanup);
2789 }
9291a0cd 2790
3876f04e 2791 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2792 {
2793 offset_type idx = 2 * iter;
2794 const char *name;
2795 offset_type *vec, vec_len, vec_idx;
2796
3876f04e 2797 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2798 continue;
2799
3876f04e 2800 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2801
e078317b 2802 if (! (*name_matcher) (name, data))
9291a0cd
TT
2803 continue;
2804
2805 /* The name was matched, now expand corresponding CUs that were
2806 marked. */
4b5246aa 2807 vec = (offset_type *) (index->constant_pool
3876f04e 2808 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2809 vec_len = MAYBE_SWAP (vec[0]);
2810 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2811 {
e254ef6a 2812 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2813
e254ef6a 2814 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2815 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2816 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2817 }
2818 }
2819}
2820
2821static struct symtab *
2822dw2_find_pc_sect_symtab (struct objfile *objfile,
2823 struct minimal_symbol *msymbol,
2824 CORE_ADDR pc,
2825 struct obj_section *section,
2826 int warn_if_readin)
2827{
2828 struct dwarf2_per_cu_data *data;
2829
2830 dw2_setup (objfile);
2831
2832 if (!objfile->psymtabs_addrmap)
2833 return NULL;
2834
2835 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2836 if (!data)
2837 return NULL;
2838
2839 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2840 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2841 paddress (get_objfile_arch (objfile), pc));
2842
a0f42c21 2843 return dw2_instantiate_symtab (data);
9291a0cd
TT
2844}
2845
9291a0cd 2846static void
44b13c5a 2847dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2848 void *data, int need_fullname)
9291a0cd
TT
2849{
2850 int i;
24c79950
TT
2851 struct cleanup *cleanup;
2852 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2853 NULL, xcalloc, xfree);
9291a0cd 2854
24c79950 2855 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2856 dw2_setup (objfile);
ae2de4f8 2857
24c79950
TT
2858 /* We can ignore file names coming from already-expanded CUs. */
2859 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2860 + dwarf2_per_objfile->n_type_units); ++i)
2861 {
2862 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2863
2864 if (per_cu->v.quick->symtab)
2865 {
2866 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2867 INSERT);
2868
2869 *slot = per_cu->v.quick->file_names;
2870 }
2871 }
2872
1fd400ff 2873 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2874 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2875 {
2876 int j;
e254ef6a 2877 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2878 struct quick_file_names *file_data;
24c79950 2879 void **slot;
9291a0cd 2880
3d7bb9d9 2881 /* We only need to look at symtabs not already expanded. */
e254ef6a 2882 if (per_cu->v.quick->symtab)
9291a0cd
TT
2883 continue;
2884
7b9f3c50
DE
2885 file_data = dw2_get_file_names (objfile, per_cu);
2886 if (file_data == NULL)
9291a0cd
TT
2887 continue;
2888
24c79950
TT
2889 slot = htab_find_slot (visited, file_data, INSERT);
2890 if (*slot)
2891 {
2892 /* Already visited. */
2893 continue;
2894 }
2895 *slot = file_data;
2896
7b9f3c50 2897 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2898 {
74e2f255
DE
2899 const char *this_real_name;
2900
2901 if (need_fullname)
2902 this_real_name = dw2_get_real_path (objfile, file_data, j);
2903 else
2904 this_real_name = NULL;
7b9f3c50 2905 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2906 }
2907 }
24c79950
TT
2908
2909 do_cleanups (cleanup);
9291a0cd
TT
2910}
2911
2912static int
2913dw2_has_symbols (struct objfile *objfile)
2914{
2915 return 1;
2916}
2917
2918const struct quick_symbol_functions dwarf2_gdb_index_functions =
2919{
2920 dw2_has_symbols,
2921 dw2_find_last_source_symtab,
2922 dw2_forget_cached_source_info,
f8eba3c6 2923 dw2_map_symtabs_matching_filename,
9291a0cd 2924 dw2_lookup_symbol,
774b6a14 2925 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2926 dw2_print_stats,
2927 dw2_dump,
2928 dw2_relocate,
2929 dw2_expand_symtabs_for_function,
2930 dw2_expand_all_symtabs,
2931 dw2_expand_symtabs_with_filename,
2932 dw2_find_symbol_file,
40658b94 2933 dw2_map_matching_symbols,
9291a0cd
TT
2934 dw2_expand_symtabs_matching,
2935 dw2_find_pc_sect_symtab,
9291a0cd
TT
2936 dw2_map_symbol_filenames
2937};
2938
2939/* Initialize for reading DWARF for this objfile. Return 0 if this
2940 file will use psymtabs, or 1 if using the GNU index. */
2941
2942int
2943dwarf2_initialize_objfile (struct objfile *objfile)
2944{
2945 /* If we're about to read full symbols, don't bother with the
2946 indices. In this case we also don't care if some other debug
2947 format is making psymtabs, because they are all about to be
2948 expanded anyway. */
2949 if ((objfile->flags & OBJF_READNOW))
2950 {
2951 int i;
2952
2953 dwarf2_per_objfile->using_index = 1;
2954 create_all_comp_units (objfile);
1fd400ff 2955 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2956 dwarf2_per_objfile->quick_file_names_table =
2957 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2958
1fd400ff 2959 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2960 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2961 {
e254ef6a 2962 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2963
e254ef6a
DE
2964 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2966 }
2967
2968 /* Return 1 so that gdb sees the "quick" functions. However,
2969 these functions will be no-ops because we will have expanded
2970 all symtabs. */
2971 return 1;
2972 }
2973
2974 if (dwarf2_read_index (objfile))
2975 return 1;
2976
9291a0cd
TT
2977 return 0;
2978}
2979
2980\f
2981
dce234bc
PP
2982/* Build a partial symbol table. */
2983
2984void
f29dff0a 2985dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2986{
f29dff0a 2987 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2988 {
2989 init_psymbol_list (objfile, 1024);
2990 }
2991
d146bf1e 2992 dwarf2_build_psymtabs_hard (objfile);
c906108c 2993}
c906108c 2994
45452591
DE
2995/* Return TRUE if OFFSET is within CU_HEADER. */
2996
2997static inline int
b64f50a1 2998offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 2999{
b64f50a1
JK
3000 sect_offset bottom = { cu_header->offset.sect_off };
3001 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3002 + cu_header->initial_length_size) };
9a619af0 3003
b64f50a1 3004 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3005}
3006
93311388
DE
3007/* Read in the comp unit header information from the debug_info at info_ptr.
3008 NOTE: This leaves members offset, first_die_offset to be filled in
3009 by the caller. */
107d2387 3010
fe1b8b76 3011static gdb_byte *
107d2387 3012read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3013 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3014{
3015 int signed_addr;
891d2f0b 3016 unsigned int bytes_read;
c764a876
DE
3017
3018 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3019 cu_header->initial_length_size = bytes_read;
3020 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3021 info_ptr += bytes_read;
107d2387
AC
3022 cu_header->version = read_2_bytes (abfd, info_ptr);
3023 info_ptr += 2;
b64f50a1
JK
3024 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3025 &bytes_read);
613e1657 3026 info_ptr += bytes_read;
107d2387
AC
3027 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3028 info_ptr += 1;
3029 signed_addr = bfd_get_sign_extend_vma (abfd);
3030 if (signed_addr < 0)
8e65ff28 3031 internal_error (__FILE__, __LINE__,
e2e0b3e5 3032 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3033 cu_header->signed_addr_p = signed_addr;
c764a876 3034
107d2387
AC
3035 return info_ptr;
3036}
3037
9ff913ba
DE
3038/* Subroutine of read_and_check_comp_unit_head and
3039 read_and_check_type_unit_head to simplify them.
3040 Perform various error checking on the header. */
3041
3042static void
3043error_check_comp_unit_head (struct comp_unit_head *header,
3044 struct dwarf2_section_info *section)
3045{
3046 bfd *abfd = section->asection->owner;
3047 const char *filename = bfd_get_filename (abfd);
3048
3049 if (header->version != 2 && header->version != 3 && header->version != 4)
3050 error (_("Dwarf Error: wrong version in compilation unit header "
3051 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3052 filename);
3053
b64f50a1 3054 if (header->abbrev_offset.sect_off
9ff913ba
DE
3055 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3056 &dwarf2_per_objfile->abbrev))
3057 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3058 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3059 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3060 filename);
3061
3062 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3063 avoid potential 32-bit overflow. */
b64f50a1 3064 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3065 + header->length + header->initial_length_size)
3066 > section->size)
3067 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3068 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3069 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3070 filename);
3071}
3072
3073/* Read in a CU/TU header and perform some basic error checking.
3074 The contents of the header are stored in HEADER.
3075 The result is a pointer to the start of the first DIE. */
adabb602 3076
fe1b8b76 3077static gdb_byte *
9ff913ba
DE
3078read_and_check_comp_unit_head (struct comp_unit_head *header,
3079 struct dwarf2_section_info *section,
3080 gdb_byte *info_ptr,
3081 int is_debug_types_section)
72bf9492 3082{
fe1b8b76 3083 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3084 bfd *abfd = section->asection->owner;
72bf9492 3085
b64f50a1 3086 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3087
72bf9492
DJ
3088 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3089
460c1c54
CC
3090 /* If we're reading a type unit, skip over the signature and
3091 type_offset fields. */
b0df02fd 3092 if (is_debug_types_section)
460c1c54
CC
3093 info_ptr += 8 /*signature*/ + header->offset_size;
3094
b64f50a1 3095 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3096
9ff913ba 3097 error_check_comp_unit_head (header, section);
72bf9492
DJ
3098
3099 return info_ptr;
3100}
3101
348e048f
DE
3102/* Read in the types comp unit header information from .debug_types entry at
3103 types_ptr. The result is a pointer to one past the end of the header. */
3104
3105static gdb_byte *
9ff913ba
DE
3106read_and_check_type_unit_head (struct comp_unit_head *header,
3107 struct dwarf2_section_info *section,
3108 gdb_byte *info_ptr,
b64f50a1 3109 ULONGEST *signature, cu_offset *type_offset)
348e048f 3110{
9ff913ba
DE
3111 gdb_byte *beg_of_comp_unit = info_ptr;
3112 bfd *abfd = section->asection->owner;
348e048f 3113
b64f50a1 3114 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3115
9ff913ba 3116 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3117
9ff913ba
DE
3118 /* If we're reading a type unit, skip over the signature and
3119 type_offset fields. */
3120 if (signature != NULL)
3121 *signature = read_8_bytes (abfd, info_ptr);
3122 info_ptr += 8;
3123 if (type_offset != NULL)
b64f50a1 3124 type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
9ff913ba
DE
3125 info_ptr += header->offset_size;
3126
b64f50a1 3127 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3128
9ff913ba
DE
3129 error_check_comp_unit_head (header, section);
3130
3131 return info_ptr;
348e048f
DE
3132}
3133
aaa75496
JB
3134/* Allocate a new partial symtab for file named NAME and mark this new
3135 partial symtab as being an include of PST. */
3136
3137static void
3138dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3139 struct objfile *objfile)
3140{
3141 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3142
3143 subpst->section_offsets = pst->section_offsets;
3144 subpst->textlow = 0;
3145 subpst->texthigh = 0;
3146
3147 subpst->dependencies = (struct partial_symtab **)
3148 obstack_alloc (&objfile->objfile_obstack,
3149 sizeof (struct partial_symtab *));
3150 subpst->dependencies[0] = pst;
3151 subpst->number_of_dependencies = 1;
3152
3153 subpst->globals_offset = 0;
3154 subpst->n_global_syms = 0;
3155 subpst->statics_offset = 0;
3156 subpst->n_static_syms = 0;
3157 subpst->symtab = NULL;
3158 subpst->read_symtab = pst->read_symtab;
3159 subpst->readin = 0;
3160
3161 /* No private part is necessary for include psymtabs. This property
3162 can be used to differentiate between such include psymtabs and
10b3939b 3163 the regular ones. */
58a9656e 3164 subpst->read_symtab_private = NULL;
aaa75496
JB
3165}
3166
3167/* Read the Line Number Program data and extract the list of files
3168 included by the source file represented by PST. Build an include
d85a05f0 3169 partial symtab for each of these included files. */
aaa75496
JB
3170
3171static void
3172dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3173 struct die_info *die,
aaa75496
JB
3174 struct partial_symtab *pst)
3175{
3176 struct objfile *objfile = cu->objfile;
3177 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3178 struct line_header *lh = NULL;
3179 struct attribute *attr;
aaa75496 3180
d85a05f0
DJ
3181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3182 if (attr)
3183 {
3184 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3185
d85a05f0
DJ
3186 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3187 }
aaa75496
JB
3188 if (lh == NULL)
3189 return; /* No linetable, so no includes. */
3190
c6da4cef 3191 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3192 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3193
3194 free_line_header (lh);
3195}
3196
348e048f
DE
3197static hashval_t
3198hash_type_signature (const void *item)
3199{
3200 const struct signatured_type *type_sig = item;
9a619af0 3201
348e048f
DE
3202 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3203 return type_sig->signature;
3204}
3205
3206static int
3207eq_type_signature (const void *item_lhs, const void *item_rhs)
3208{
3209 const struct signatured_type *lhs = item_lhs;
3210 const struct signatured_type *rhs = item_rhs;
9a619af0 3211
348e048f
DE
3212 return lhs->signature == rhs->signature;
3213}
3214
1fd400ff
TT
3215/* Allocate a hash table for signatured types. */
3216
3217static htab_t
673bfd45 3218allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3219{
3220 return htab_create_alloc_ex (41,
3221 hash_type_signature,
3222 eq_type_signature,
3223 NULL,
3224 &objfile->objfile_obstack,
3225 hashtab_obstack_allocate,
3226 dummy_obstack_deallocate);
3227}
3228
d467dd73 3229/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3230
3231static int
d467dd73 3232add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3233{
3234 struct signatured_type *sigt = *slot;
3235 struct dwarf2_per_cu_data ***datap = datum;
3236
3237 **datap = &sigt->per_cu;
3238 ++*datap;
3239
3240 return 1;
3241}
3242
d467dd73 3243/* Create the hash table of all entries in the .debug_types section(s).
348e048f
DE
3244 The result is zero if there is an error (e.g. missing .debug_types section),
3245 otherwise non-zero. */
3246
3247static int
3248create_debug_types_hash_table (struct objfile *objfile)
3249{
8b70b953 3250 htab_t types_htab = NULL;
1fd400ff 3251 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3252 int ix;
3253 struct dwarf2_section_info *section;
348e048f 3254
8b70b953 3255 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3256 {
3257 dwarf2_per_objfile->signatured_types = NULL;
3258 return 0;
3259 }
3260
8b70b953
TT
3261 for (ix = 0;
3262 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3263 ix, section);
3264 ++ix)
3265 {
3266 gdb_byte *info_ptr, *end_ptr;
348e048f 3267
8b70b953
TT
3268 dwarf2_read_section (objfile, section);
3269 info_ptr = section->buffer;
348e048f 3270
8b70b953
TT
3271 if (info_ptr == NULL)
3272 continue;
348e048f 3273
8b70b953
TT
3274 if (types_htab == NULL)
3275 types_htab = allocate_signatured_type_table (objfile);
348e048f 3276
8b70b953
TT
3277 if (dwarf2_die_debug)
3278 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3279
3280 end_ptr = info_ptr + section->size;
3281 while (info_ptr < end_ptr)
3282 {
b64f50a1
JK
3283 sect_offset offset;
3284 cu_offset type_offset;
8b70b953
TT
3285 ULONGEST signature;
3286 struct signatured_type *type_sig;
3287 void **slot;
3288 gdb_byte *ptr = info_ptr;
9ff913ba 3289 struct comp_unit_head header;
348e048f 3290
b64f50a1 3291 offset.sect_off = ptr - section->buffer;
348e048f 3292
8b70b953 3293 /* We need to read the type's signature in order to build the hash
9ff913ba 3294 table, but we don't need anything else just yet. */
348e048f 3295
9ff913ba
DE
3296 ptr = read_and_check_type_unit_head (&header, section, ptr,
3297 &signature, &type_offset);
6caca83c
CC
3298
3299 /* Skip dummy type units. */
3300 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3301 {
9ff913ba 3302 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3303 continue;
3304 }
8b70b953
TT
3305
3306 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3307 memset (type_sig, 0, sizeof (*type_sig));
3308 type_sig->signature = signature;
3309 type_sig->type_offset = type_offset;
3310 type_sig->per_cu.objfile = objfile;
b0df02fd 3311 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3312 type_sig->per_cu.offset = offset;
3313
3314 slot = htab_find_slot (types_htab, type_sig, INSERT);
3315 gdb_assert (slot != NULL);
3316 if (*slot != NULL)
3317 {
3318 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3319
8b70b953
TT
3320 complaint (&symfile_complaints,
3321 _("debug type entry at offset 0x%x is duplicate to the "
3322 "entry at offset 0x%x, signature 0x%s"),
b64f50a1 3323 offset.sect_off, dup_sig->per_cu.offset.sect_off,
8b70b953
TT
3324 phex (signature, sizeof (signature)));
3325 gdb_assert (signature == dup_sig->signature);
3326 }
3327 *slot = type_sig;
348e048f 3328
8b70b953
TT
3329 if (dwarf2_die_debug)
3330 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3331 offset.sect_off,
3332 phex (signature, sizeof (signature)));
348e048f 3333
9ff913ba 3334 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3335 }
348e048f
DE
3336 }
3337
3338 dwarf2_per_objfile->signatured_types = types_htab;
3339
d467dd73
DE
3340 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3341 dwarf2_per_objfile->all_type_units
1fd400ff 3342 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3343 dwarf2_per_objfile->n_type_units
1fd400ff 3344 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3345 iter = &dwarf2_per_objfile->all_type_units[0];
3346 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3347 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3348 == dwarf2_per_objfile->n_type_units);
1fd400ff 3349
348e048f
DE
3350 return 1;
3351}
3352
3353/* Lookup a signature based type.
3354 Returns NULL if SIG is not present in the table. */
3355
3356static struct signatured_type *
3357lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3358{
3359 struct signatured_type find_entry, *entry;
3360
3361 if (dwarf2_per_objfile->signatured_types == NULL)
3362 {
3363 complaint (&symfile_complaints,
55f1336d 3364 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3365 return 0;
3366 }
3367
3368 find_entry.signature = sig;
3369 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3370 return entry;
3371}
3372
d85a05f0
DJ
3373/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3374
3375static void
3376init_cu_die_reader (struct die_reader_specs *reader,
3377 struct dwarf2_cu *cu)
3378{
3379 reader->abfd = cu->objfile->obfd;
3380 reader->cu = cu;
b0df02fd 3381 if (cu->per_cu->debug_types_section)
be391dca 3382 {
b0df02fd
DE
3383 gdb_assert (cu->per_cu->debug_types_section->readin);
3384 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3385 }
d85a05f0 3386 else
be391dca
TT
3387 {
3388 gdb_assert (dwarf2_per_objfile->info.readin);
3389 reader->buffer = dwarf2_per_objfile->info.buffer;
3390 }
d85a05f0
DJ
3391}
3392
3393/* Find the base address of the compilation unit for range lists and
3394 location lists. It will normally be specified by DW_AT_low_pc.
3395 In DWARF-3 draft 4, the base address could be overridden by
3396 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3397 compilation units with discontinuous ranges. */
3398
3399static void
3400dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3401{
3402 struct attribute *attr;
3403
3404 cu->base_known = 0;
3405 cu->base_address = 0;
3406
3407 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3408 if (attr)
3409 {
3410 cu->base_address = DW_ADDR (attr);
3411 cu->base_known = 1;
3412 }
3413 else
3414 {
3415 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3416 if (attr)
3417 {
3418 cu->base_address = DW_ADDR (attr);
3419 cu->base_known = 1;
3420 }
3421 }
3422}
3423
348e048f
DE
3424/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3425 to combine the common parts.
21b2bd31
DE
3426 Process compilation unit THIS_CU for a psymtab.
3427 SECTION is the section the CU/TU comes from,
3428 either .debug_info or .debug_types. */
aaa75496 3429
70221824 3430static void
a0f42c21 3431process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3432 struct dwarf2_section_info *section,
3433 int is_debug_types_section)
c906108c 3434{
a0f42c21 3435 struct objfile *objfile = this_cu->objfile;
c906108c 3436 bfd *abfd = objfile->obfd;
21b2bd31 3437 gdb_byte *buffer = section->buffer;
b64f50a1 3438 gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
21b2bd31 3439 unsigned int buffer_size = section->size;
93311388 3440 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3441 struct die_info *comp_unit_die;
c906108c 3442 struct partial_symtab *pst;
5734ee8b 3443 CORE_ADDR baseaddr;
93311388
DE
3444 struct cleanup *back_to_inner;
3445 struct dwarf2_cu cu;
d85a05f0
DJ
3446 int has_children, has_pc_info;
3447 struct attribute *attr;
d85a05f0
DJ
3448 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3449 struct die_reader_specs reader_specs;
3e2a0cee 3450 const char *filename;
c906108c 3451
23745b47
DE
3452 /* If this compilation unit was already read in, free the
3453 cached copy in order to read it in again. This is
3454 necessary because we skipped some symbols when we first
3455 read in the compilation unit (see load_partial_dies).
3456 This problem could be avoided, but the benefit is
3457 unclear. */
3458 if (this_cu->cu != NULL)
3459 free_one_cached_comp_unit (this_cu->cu);
3460
3461 /* Note that this is a pointer to our stack frame, being
3462 added to a global data structure. It will be cleaned up
3463 in free_stack_comp_unit when we finish with this
3464 compilation unit. */
3465 init_one_comp_unit (&cu, this_cu);
93311388 3466 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3467
9ff913ba
DE
3468 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3469 is_debug_types_section);
10b3939b 3470
6caca83c
CC
3471 /* Skip dummy compilation units. */
3472 if (info_ptr >= buffer + buffer_size
3473 || peek_abbrev_code (abfd, info_ptr) == 0)
3474 {
6caca83c 3475 do_cleanups (back_to_inner);
21b2bd31 3476 return;
6caca83c
CC
3477 }
3478
93311388 3479 cu.list_in_scope = &file_symbols;
af703f96 3480
93311388 3481 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3482 dwarf2_read_abbrevs (&cu);
93311388 3483 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3484
93311388 3485 /* Read the compilation unit die. */
d85a05f0
DJ
3486 init_cu_die_reader (&reader_specs, &cu);
3487 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3488 &has_children);
93311388 3489
21b2bd31 3490 if (is_debug_types_section)
348e048f 3491 {
b3c8eb43 3492 /* LENGTH has not been set yet for type units. */
b64f50a1 3493 gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
348e048f
DE
3494 this_cu->length = cu.header.length + cu.header.initial_length_size;
3495 }
d85a05f0 3496 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3497 {
93311388 3498 do_cleanups (back_to_inner);
21b2bd31 3499 return;
93311388 3500 }
72bf9492 3501
9816fde3 3502 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3503
93311388 3504 /* Allocate a new partial symbol table structure. */
d85a05f0 3505 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3506 if (attr == NULL || !DW_STRING (attr))
3507 filename = "";
3508 else
3509 filename = DW_STRING (attr);
93311388 3510 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3511 filename,
93311388
DE
3512 /* TEXTLOW and TEXTHIGH are set below. */
3513 0,
3514 objfile->global_psymbols.next,
3515 objfile->static_psymbols.next);
9750bca9 3516 pst->psymtabs_addrmap_supported = 1;
72bf9492 3517
d85a05f0
DJ
3518 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3519 if (attr != NULL)
3520 pst->dirname = DW_STRING (attr);
72bf9492 3521
e38df1d0 3522 pst->read_symtab_private = this_cu;
72bf9492 3523
93311388 3524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3525
0963b4bd 3526 /* Store the function that reads in the rest of the symbol table. */
93311388 3527 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3528
9291a0cd 3529 this_cu->v.psymtab = pst;
c906108c 3530
d85a05f0
DJ
3531 dwarf2_find_base_address (comp_unit_die, &cu);
3532
93311388
DE
3533 /* Possibly set the default values of LOWPC and HIGHPC from
3534 `DW_AT_ranges'. */
d85a05f0
DJ
3535 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3536 &best_highpc, &cu, pst);
3537 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3538 /* Store the contiguous range if it is not empty; it can be empty for
3539 CUs with no code. */
3540 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3541 best_lowpc + baseaddr,
3542 best_highpc + baseaddr - 1, pst);
93311388
DE
3543
3544 /* Check if comp unit has_children.
3545 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3546 If not, there's no more debug_info for this comp unit. */
d85a05f0 3547 if (has_children)
93311388
DE
3548 {
3549 struct partial_die_info *first_die;
3550 CORE_ADDR lowpc, highpc;
31ffec48 3551
93311388
DE
3552 lowpc = ((CORE_ADDR) -1);
3553 highpc = ((CORE_ADDR) 0);
c906108c 3554
93311388 3555 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3556
93311388 3557 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3558 ! has_pc_info, &cu);
57c22c6c 3559
93311388
DE
3560 /* If we didn't find a lowpc, set it to highpc to avoid
3561 complaints from `maint check'. */
3562 if (lowpc == ((CORE_ADDR) -1))
3563 lowpc = highpc;
10b3939b 3564
93311388
DE
3565 /* If the compilation unit didn't have an explicit address range,
3566 then use the information extracted from its child dies. */
d85a05f0 3567 if (! has_pc_info)
93311388 3568 {
d85a05f0
DJ
3569 best_lowpc = lowpc;
3570 best_highpc = highpc;
93311388
DE
3571 }
3572 }
d85a05f0
DJ
3573 pst->textlow = best_lowpc + baseaddr;
3574 pst->texthigh = best_highpc + baseaddr;
c906108c 3575
93311388
DE
3576 pst->n_global_syms = objfile->global_psymbols.next -
3577 (objfile->global_psymbols.list + pst->globals_offset);
3578 pst->n_static_syms = objfile->static_psymbols.next -
3579 (objfile->static_psymbols.list + pst->statics_offset);
3580 sort_pst_symbols (pst);
c906108c 3581
21b2bd31 3582 if (is_debug_types_section)
348e048f
DE
3583 {
3584 /* It's not clear we want to do anything with stmt lists here.
3585 Waiting to see what gcc ultimately does. */
3586 }
d85a05f0 3587 else
93311388
DE
3588 {
3589 /* Get the list of files included in the current compilation unit,
3590 and build a psymtab for each of them. */
d85a05f0 3591 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3592 }
ae038cb0 3593
93311388 3594 do_cleanups (back_to_inner);
93311388 3595}
ff013f42 3596
348e048f
DE
3597/* Traversal function for htab_traverse_noresize.
3598 Process one .debug_types comp-unit. */
3599
3600static int
3601process_type_comp_unit (void **slot, void *info)
3602{
3603 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3604 struct dwarf2_per_cu_data *this_cu;
3605
a0f42c21 3606 gdb_assert (info == NULL);
348e048f 3607 this_cu = &entry->per_cu;
348e048f 3608
b0df02fd 3609 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3610 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3611
3612 return 1;
3613}
3614
3615/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3616 Build partial symbol tables for the .debug_types comp-units. */
3617
3618static void
3619build_type_psymtabs (struct objfile *objfile)
3620{
3621 if (! create_debug_types_hash_table (objfile))
3622 return;
3623
3624 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3625 process_type_comp_unit, NULL);
348e048f
DE
3626}
3627
60606b2c
TT
3628/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3629
3630static void
3631psymtabs_addrmap_cleanup (void *o)
3632{
3633 struct objfile *objfile = o;
ec61707d 3634
60606b2c
TT
3635 objfile->psymtabs_addrmap = NULL;
3636}
3637
93311388
DE
3638/* Build the partial symbol table by doing a quick pass through the
3639 .debug_info and .debug_abbrev sections. */
72bf9492 3640
93311388 3641static void
c67a9c90 3642dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3643{
60606b2c
TT
3644 struct cleanup *back_to, *addrmap_cleanup;
3645 struct obstack temp_obstack;
21b2bd31 3646 int i;
93311388 3647
98bfdba5
PA
3648 dwarf2_per_objfile->reading_partial_symbols = 1;
3649
be391dca 3650 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3651
93311388
DE
3652 /* Any cached compilation units will be linked by the per-objfile
3653 read_in_chain. Make sure to free them when we're done. */
3654 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3655
348e048f
DE
3656 build_type_psymtabs (objfile);
3657
93311388 3658 create_all_comp_units (objfile);
c906108c 3659
60606b2c
TT
3660 /* Create a temporary address map on a temporary obstack. We later
3661 copy this to the final obstack. */
3662 obstack_init (&temp_obstack);
3663 make_cleanup_obstack_free (&temp_obstack);
3664 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3665 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3666
21b2bd31 3667 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3668 {
21b2bd31 3669 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3670
21b2bd31 3671 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3672 }
ff013f42
JK
3673
3674 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3675 &objfile->objfile_obstack);
60606b2c 3676 discard_cleanups (addrmap_cleanup);
ff013f42 3677
ae038cb0
DJ
3678 do_cleanups (back_to);
3679}
3680
93311388 3681/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3682
3683static void
a0f42c21 3684load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3685{
a0f42c21 3686 struct objfile *objfile = this_cu->objfile;
ae038cb0 3687 bfd *abfd = objfile->obfd;
adabb602 3688 gdb_byte *info_ptr;
d85a05f0 3689 struct die_info *comp_unit_die;
ae038cb0 3690 struct dwarf2_cu *cu;
1d9ec526 3691 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3692 int has_children;
3693 struct die_reader_specs reader_specs;
98bfdba5 3694 int read_cu = 0;
9ff913ba 3695 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3696
b0df02fd 3697 gdb_assert (! this_cu->debug_types_section);
348e048f 3698
9ff913ba 3699 gdb_assert (section->readin);
b64f50a1 3700 info_ptr = section->buffer + this_cu->offset.sect_off;
ae038cb0 3701
98bfdba5
PA
3702 if (this_cu->cu == NULL)
3703 {
9816fde3 3704 cu = xmalloc (sizeof (*cu));
23745b47 3705 init_one_comp_unit (cu, this_cu);
ae038cb0 3706
98bfdba5 3707 read_cu = 1;
ae038cb0 3708
98bfdba5 3709 /* If an error occurs while loading, release our storage. */
68dc6402 3710 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3711
9ff913ba
DE
3712 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3713 0);
ae038cb0 3714
6caca83c 3715 /* Skip dummy compilation units. */
9ff913ba 3716 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3717 || peek_abbrev_code (abfd, info_ptr) == 0)
3718 {
3719 do_cleanups (free_cu_cleanup);
3720 return;
3721 }
98bfdba5
PA
3722 }
3723 else
3724 {
3725 cu = this_cu->cu;
b64f50a1 3726 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 3727 }
ae038cb0
DJ
3728
3729 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3730 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3731 dwarf2_read_abbrevs (cu);
98bfdba5 3732 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3733
3734 /* Read the compilation unit die. */
d85a05f0
DJ
3735 init_cu_die_reader (&reader_specs, cu);
3736 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3737 &has_children);
ae038cb0 3738
9816fde3 3739 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3740
ae038cb0
DJ
3741 /* Check if comp unit has_children.
3742 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3743 If not, there's no more debug_info for this comp unit. */
d85a05f0 3744 if (has_children)
9ff913ba 3745 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3746
98bfdba5
PA
3747 do_cleanups (free_abbrevs_cleanup);
3748
3749 if (read_cu)
3750 {
3751 /* We've successfully allocated this compilation unit. Let our
3752 caller clean it up when finished with it. */
3753 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
3754
3755 /* Link this CU into read_in_chain. */
3756 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3757 dwarf2_per_objfile->read_in_chain = this_cu;
98bfdba5 3758 }
ae038cb0
DJ
3759}
3760
9cdd5dbd
DE
3761/* Create a list of all compilation units in OBJFILE.
3762 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3763
3764static void
3765create_all_comp_units (struct objfile *objfile)
3766{
3767 int n_allocated;
3768 int n_comp_units;
3769 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3770 gdb_byte *info_ptr;
3771
3772 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3773 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3774
3775 n_comp_units = 0;
3776 n_allocated = 10;
3777 all_comp_units = xmalloc (n_allocated
3778 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3779
3e43a32a
MS
3780 while (info_ptr < dwarf2_per_objfile->info.buffer
3781 + dwarf2_per_objfile->info.size)
ae038cb0 3782 {
c764a876 3783 unsigned int length, initial_length_size;
ae038cb0 3784 struct dwarf2_per_cu_data *this_cu;
b64f50a1 3785 sect_offset offset;
ae038cb0 3786
b64f50a1 3787 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3788
3789 /* Read just enough information to find out where the next
3790 compilation unit is. */
c764a876
DE
3791 length = read_initial_length (objfile->obfd, info_ptr,
3792 &initial_length_size);
ae038cb0
DJ
3793
3794 /* Save the compilation unit for later lookup. */
3795 this_cu = obstack_alloc (&objfile->objfile_obstack,
3796 sizeof (struct dwarf2_per_cu_data));
3797 memset (this_cu, 0, sizeof (*this_cu));
3798 this_cu->offset = offset;
c764a876 3799 this_cu->length = length + initial_length_size;
9291a0cd 3800 this_cu->objfile = objfile;
ae038cb0
DJ
3801
3802 if (n_comp_units == n_allocated)
3803 {
3804 n_allocated *= 2;
3805 all_comp_units = xrealloc (all_comp_units,
3806 n_allocated
3807 * sizeof (struct dwarf2_per_cu_data *));
3808 }
3809 all_comp_units[n_comp_units++] = this_cu;
3810
3811 info_ptr = info_ptr + this_cu->length;
3812 }
3813
3814 dwarf2_per_objfile->all_comp_units
3815 = obstack_alloc (&objfile->objfile_obstack,
3816 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3817 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3818 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3819 xfree (all_comp_units);
3820 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3821}
3822
5734ee8b
DJ
3823/* Process all loaded DIEs for compilation unit CU, starting at
3824 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3825 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3826 DW_AT_ranges). If NEED_PC is set, then this function will set
3827 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3828 and record the covered ranges in the addrmap. */
c906108c 3829
72bf9492
DJ
3830static void
3831scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3832 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3833{
72bf9492 3834 struct partial_die_info *pdi;
c906108c 3835
91c24f0a
DC
3836 /* Now, march along the PDI's, descending into ones which have
3837 interesting children but skipping the children of the other ones,
3838 until we reach the end of the compilation unit. */
c906108c 3839
72bf9492 3840 pdi = first_die;
91c24f0a 3841
72bf9492
DJ
3842 while (pdi != NULL)
3843 {
3844 fixup_partial_die (pdi, cu);
c906108c 3845
f55ee35c 3846 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3847 children, so we need to look at them. Ditto for anonymous
3848 enums. */
933c6fe4 3849
72bf9492 3850 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3851 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3852 {
72bf9492 3853 switch (pdi->tag)
c906108c
SS
3854 {
3855 case DW_TAG_subprogram:
5734ee8b 3856 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3857 break;
72929c62 3858 case DW_TAG_constant:
c906108c
SS
3859 case DW_TAG_variable:
3860 case DW_TAG_typedef:
91c24f0a 3861 case DW_TAG_union_type:
72bf9492 3862 if (!pdi->is_declaration)
63d06c5c 3863 {
72bf9492 3864 add_partial_symbol (pdi, cu);
63d06c5c
DC
3865 }
3866 break;
c906108c 3867 case DW_TAG_class_type:
680b30c7 3868 case DW_TAG_interface_type:
c906108c 3869 case DW_TAG_structure_type:
72bf9492 3870 if (!pdi->is_declaration)
c906108c 3871 {
72bf9492 3872 add_partial_symbol (pdi, cu);
c906108c
SS
3873 }
3874 break;
91c24f0a 3875 case DW_TAG_enumeration_type:
72bf9492
DJ
3876 if (!pdi->is_declaration)
3877 add_partial_enumeration (pdi, cu);
c906108c
SS
3878 break;
3879 case DW_TAG_base_type:
a02abb62 3880 case DW_TAG_subrange_type:
c906108c 3881 /* File scope base type definitions are added to the partial
c5aa993b 3882 symbol table. */
72bf9492 3883 add_partial_symbol (pdi, cu);
c906108c 3884 break;
d9fa45fe 3885 case DW_TAG_namespace:
5734ee8b 3886 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3887 break;
5d7cb8df
JK
3888 case DW_TAG_module:
3889 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3890 break;
c906108c
SS
3891 default:
3892 break;
3893 }
3894 }
3895
72bf9492
DJ
3896 /* If the die has a sibling, skip to the sibling. */
3897
3898 pdi = pdi->die_sibling;
3899 }
3900}
3901
3902/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3903
72bf9492 3904 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3905 name is concatenated with "::" and the partial DIE's name. For
3906 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3907 Enumerators are an exception; they use the scope of their parent
3908 enumeration type, i.e. the name of the enumeration type is not
3909 prepended to the enumerator.
91c24f0a 3910
72bf9492
DJ
3911 There are two complexities. One is DW_AT_specification; in this
3912 case "parent" means the parent of the target of the specification,
3913 instead of the direct parent of the DIE. The other is compilers
3914 which do not emit DW_TAG_namespace; in this case we try to guess
3915 the fully qualified name of structure types from their members'
3916 linkage names. This must be done using the DIE's children rather
3917 than the children of any DW_AT_specification target. We only need
3918 to do this for structures at the top level, i.e. if the target of
3919 any DW_AT_specification (if any; otherwise the DIE itself) does not
3920 have a parent. */
3921
3922/* Compute the scope prefix associated with PDI's parent, in
3923 compilation unit CU. The result will be allocated on CU's
3924 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3925 field. NULL is returned if no prefix is necessary. */
3926static char *
3927partial_die_parent_scope (struct partial_die_info *pdi,
3928 struct dwarf2_cu *cu)
3929{
3930 char *grandparent_scope;
3931 struct partial_die_info *parent, *real_pdi;
91c24f0a 3932
72bf9492
DJ
3933 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3934 then this means the parent of the specification DIE. */
3935
3936 real_pdi = pdi;
72bf9492 3937 while (real_pdi->has_specification)
10b3939b 3938 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3939
3940 parent = real_pdi->die_parent;
3941 if (parent == NULL)
3942 return NULL;
3943
3944 if (parent->scope_set)
3945 return parent->scope;
3946
3947 fixup_partial_die (parent, cu);
3948
10b3939b 3949 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3950
acebe513
UW
3951 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3952 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3953 Work around this problem here. */
3954 if (cu->language == language_cplus
6e70227d 3955 && parent->tag == DW_TAG_namespace
acebe513
UW
3956 && strcmp (parent->name, "::") == 0
3957 && grandparent_scope == NULL)
3958 {
3959 parent->scope = NULL;
3960 parent->scope_set = 1;
3961 return NULL;
3962 }
3963
9c6c53f7
SA
3964 if (pdi->tag == DW_TAG_enumerator)
3965 /* Enumerators should not get the name of the enumeration as a prefix. */
3966 parent->scope = grandparent_scope;
3967 else if (parent->tag == DW_TAG_namespace
f55ee35c 3968 || parent->tag == DW_TAG_module
72bf9492
DJ
3969 || parent->tag == DW_TAG_structure_type
3970 || parent->tag == DW_TAG_class_type
680b30c7 3971 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3972 || parent->tag == DW_TAG_union_type
3973 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3974 {
3975 if (grandparent_scope == NULL)
3976 parent->scope = parent->name;
3977 else
3e43a32a
MS
3978 parent->scope = typename_concat (&cu->comp_unit_obstack,
3979 grandparent_scope,
f55ee35c 3980 parent->name, 0, cu);
72bf9492 3981 }
72bf9492
DJ
3982 else
3983 {
3984 /* FIXME drow/2004-04-01: What should we be doing with
3985 function-local names? For partial symbols, we should probably be
3986 ignoring them. */
3987 complaint (&symfile_complaints,
e2e0b3e5 3988 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 3989 parent->tag, pdi->offset.sect_off);
72bf9492 3990 parent->scope = grandparent_scope;
c906108c
SS
3991 }
3992
72bf9492
DJ
3993 parent->scope_set = 1;
3994 return parent->scope;
3995}
3996
3997/* Return the fully scoped name associated with PDI, from compilation unit
3998 CU. The result will be allocated with malloc. */
3999static char *
4000partial_die_full_name (struct partial_die_info *pdi,
4001 struct dwarf2_cu *cu)
4002{
4003 char *parent_scope;
4004
98bfdba5
PA
4005 /* If this is a template instantiation, we can not work out the
4006 template arguments from partial DIEs. So, unfortunately, we have
4007 to go through the full DIEs. At least any work we do building
4008 types here will be reused if full symbols are loaded later. */
4009 if (pdi->has_template_arguments)
4010 {
4011 fixup_partial_die (pdi, cu);
4012
4013 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4014 {
4015 struct die_info *die;
4016 struct attribute attr;
4017 struct dwarf2_cu *ref_cu = cu;
4018
b64f50a1 4019 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4020 attr.name = 0;
4021 attr.form = DW_FORM_ref_addr;
b64f50a1 4022 attr.u.addr = pdi->offset.sect_off;
98bfdba5
PA
4023 die = follow_die_ref (NULL, &attr, &ref_cu);
4024
4025 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4026 }
4027 }
4028
72bf9492
DJ
4029 parent_scope = partial_die_parent_scope (pdi, cu);
4030 if (parent_scope == NULL)
4031 return NULL;
4032 else
f55ee35c 4033 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4034}
4035
4036static void
72bf9492 4037add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4038{
e7c27a73 4039 struct objfile *objfile = cu->objfile;
c906108c 4040 CORE_ADDR addr = 0;
decbce07 4041 char *actual_name = NULL;
e142c38c 4042 CORE_ADDR baseaddr;
72bf9492 4043 int built_actual_name = 0;
e142c38c
DJ
4044
4045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4046
94af9270
KS
4047 actual_name = partial_die_full_name (pdi, cu);
4048 if (actual_name)
4049 built_actual_name = 1;
63d06c5c 4050
72bf9492
DJ
4051 if (actual_name == NULL)
4052 actual_name = pdi->name;
4053
c906108c
SS
4054 switch (pdi->tag)
4055 {
4056 case DW_TAG_subprogram:
2cfa0c8d 4057 if (pdi->is_external || cu->language == language_ada)
c906108c 4058 {
2cfa0c8d
JB
4059 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4060 of the global scope. But in Ada, we want to be able to access
4061 nested procedures globally. So all Ada subprograms are stored
4062 in the global scope. */
f47fb265 4063 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4064 mst_text, objfile); */
f47fb265
MS
4065 add_psymbol_to_list (actual_name, strlen (actual_name),
4066 built_actual_name,
4067 VAR_DOMAIN, LOC_BLOCK,
4068 &objfile->global_psymbols,
4069 0, pdi->lowpc + baseaddr,
4070 cu->language, objfile);
c906108c
SS
4071 }
4072 else
4073 {
f47fb265 4074 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4075 mst_file_text, objfile); */
f47fb265
MS
4076 add_psymbol_to_list (actual_name, strlen (actual_name),
4077 built_actual_name,
4078 VAR_DOMAIN, LOC_BLOCK,
4079 &objfile->static_psymbols,
4080 0, pdi->lowpc + baseaddr,
4081 cu->language, objfile);
c906108c
SS
4082 }
4083 break;
72929c62
JB
4084 case DW_TAG_constant:
4085 {
4086 struct psymbol_allocation_list *list;
4087
4088 if (pdi->is_external)
4089 list = &objfile->global_psymbols;
4090 else
4091 list = &objfile->static_psymbols;
f47fb265
MS
4092 add_psymbol_to_list (actual_name, strlen (actual_name),
4093 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4094 list, 0, 0, cu->language, objfile);
72929c62
JB
4095 }
4096 break;
c906108c 4097 case DW_TAG_variable:
caac4577
JG
4098 if (pdi->locdesc)
4099 addr = decode_locdesc (pdi->locdesc, cu);
4100
4101 if (pdi->locdesc
4102 && addr == 0
4103 && !dwarf2_per_objfile->has_section_at_zero)
4104 {
4105 /* A global or static variable may also have been stripped
4106 out by the linker if unused, in which case its address
4107 will be nullified; do not add such variables into partial
4108 symbol table then. */
4109 }
4110 else if (pdi->is_external)
c906108c
SS
4111 {
4112 /* Global Variable.
4113 Don't enter into the minimal symbol tables as there is
4114 a minimal symbol table entry from the ELF symbols already.
4115 Enter into partial symbol table if it has a location
4116 descriptor or a type.
4117 If the location descriptor is missing, new_symbol will create
4118 a LOC_UNRESOLVED symbol, the address of the variable will then
4119 be determined from the minimal symbol table whenever the variable
4120 is referenced.
4121 The address for the partial symbol table entry is not
4122 used by GDB, but it comes in handy for debugging partial symbol
4123 table building. */
4124
c906108c 4125 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4126 add_psymbol_to_list (actual_name, strlen (actual_name),
4127 built_actual_name,
4128 VAR_DOMAIN, LOC_STATIC,
4129 &objfile->global_psymbols,
4130 0, addr + baseaddr,
4131 cu->language, objfile);
c906108c
SS
4132 }
4133 else
4134 {
0963b4bd 4135 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4136 if (pdi->locdesc == NULL)
decbce07
MS
4137 {
4138 if (built_actual_name)
4139 xfree (actual_name);
4140 return;
4141 }
f47fb265 4142 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4143 mst_file_data, objfile); */
f47fb265
MS
4144 add_psymbol_to_list (actual_name, strlen (actual_name),
4145 built_actual_name,
4146 VAR_DOMAIN, LOC_STATIC,
4147 &objfile->static_psymbols,
4148 0, addr + baseaddr,
4149 cu->language, objfile);
c906108c
SS
4150 }
4151 break;
4152 case DW_TAG_typedef:
4153 case DW_TAG_base_type:
a02abb62 4154 case DW_TAG_subrange_type:
38d518c9 4155 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4156 built_actual_name,
176620f1 4157 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4158 &objfile->static_psymbols,
e142c38c 4159 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4160 break;
72bf9492
DJ
4161 case DW_TAG_namespace:
4162 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4163 built_actual_name,
72bf9492
DJ
4164 VAR_DOMAIN, LOC_TYPEDEF,
4165 &objfile->global_psymbols,
4166 0, (CORE_ADDR) 0, cu->language, objfile);
4167 break;
c906108c 4168 case DW_TAG_class_type:
680b30c7 4169 case DW_TAG_interface_type:
c906108c
SS
4170 case DW_TAG_structure_type:
4171 case DW_TAG_union_type:
4172 case DW_TAG_enumeration_type:
fa4028e9
JB
4173 /* Skip external references. The DWARF standard says in the section
4174 about "Structure, Union, and Class Type Entries": "An incomplete
4175 structure, union or class type is represented by a structure,
4176 union or class entry that does not have a byte size attribute
4177 and that has a DW_AT_declaration attribute." */
4178 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4179 {
4180 if (built_actual_name)
4181 xfree (actual_name);
4182 return;
4183 }
fa4028e9 4184
63d06c5c
DC
4185 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4186 static vs. global. */
38d518c9 4187 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4188 built_actual_name,
176620f1 4189 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4190 (cu->language == language_cplus
4191 || cu->language == language_java)
63d06c5c
DC
4192 ? &objfile->global_psymbols
4193 : &objfile->static_psymbols,
e142c38c 4194 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4195
c906108c
SS
4196 break;
4197 case DW_TAG_enumerator:
38d518c9 4198 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4199 built_actual_name,
176620f1 4200 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4201 (cu->language == language_cplus
4202 || cu->language == language_java)
f6fe98ef
DJ
4203 ? &objfile->global_psymbols
4204 : &objfile->static_psymbols,
e142c38c 4205 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4206 break;
4207 default:
4208 break;
4209 }
5c4e30ca 4210
72bf9492
DJ
4211 if (built_actual_name)
4212 xfree (actual_name);
c906108c
SS
4213}
4214
5c4e30ca
DC
4215/* Read a partial die corresponding to a namespace; also, add a symbol
4216 corresponding to that namespace to the symbol table. NAMESPACE is
4217 the name of the enclosing namespace. */
91c24f0a 4218
72bf9492
DJ
4219static void
4220add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4221 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4222 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4223{
72bf9492 4224 /* Add a symbol for the namespace. */
e7c27a73 4225
72bf9492 4226 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4227
4228 /* Now scan partial symbols in that namespace. */
4229
91c24f0a 4230 if (pdi->has_children)
5734ee8b 4231 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4232}
4233
5d7cb8df
JK
4234/* Read a partial die corresponding to a Fortran module. */
4235
4236static void
4237add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4238 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4239{
f55ee35c 4240 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4241
4242 if (pdi->has_children)
4243 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4244}
4245
bc30ff58
JB
4246/* Read a partial die corresponding to a subprogram and create a partial
4247 symbol for that subprogram. When the CU language allows it, this
4248 routine also defines a partial symbol for each nested subprogram
4249 that this subprogram contains.
6e70227d 4250
bc30ff58
JB
4251 DIE my also be a lexical block, in which case we simply search
4252 recursively for suprograms defined inside that lexical block.
4253 Again, this is only performed when the CU language allows this
4254 type of definitions. */
4255
4256static void
4257add_partial_subprogram (struct partial_die_info *pdi,
4258 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4259 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4260{
4261 if (pdi->tag == DW_TAG_subprogram)
4262 {
4263 if (pdi->has_pc_info)
4264 {
4265 if (pdi->lowpc < *lowpc)
4266 *lowpc = pdi->lowpc;
4267 if (pdi->highpc > *highpc)
4268 *highpc = pdi->highpc;
5734ee8b
DJ
4269 if (need_pc)
4270 {
4271 CORE_ADDR baseaddr;
4272 struct objfile *objfile = cu->objfile;
4273
4274 baseaddr = ANOFFSET (objfile->section_offsets,
4275 SECT_OFF_TEXT (objfile));
4276 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4277 pdi->lowpc + baseaddr,
4278 pdi->highpc - 1 + baseaddr,
9291a0cd 4279 cu->per_cu->v.psymtab);
5734ee8b 4280 }
481860b3
GB
4281 }
4282
4283 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4284 {
bc30ff58 4285 if (!pdi->is_declaration)
e8d05480
JB
4286 /* Ignore subprogram DIEs that do not have a name, they are
4287 illegal. Do not emit a complaint at this point, we will
4288 do so when we convert this psymtab into a symtab. */
4289 if (pdi->name)
4290 add_partial_symbol (pdi, cu);
bc30ff58
JB
4291 }
4292 }
6e70227d 4293
bc30ff58
JB
4294 if (! pdi->has_children)
4295 return;
4296
4297 if (cu->language == language_ada)
4298 {
4299 pdi = pdi->die_child;
4300 while (pdi != NULL)
4301 {
4302 fixup_partial_die (pdi, cu);
4303 if (pdi->tag == DW_TAG_subprogram
4304 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4305 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4306 pdi = pdi->die_sibling;
4307 }
4308 }
4309}
4310
91c24f0a
DC
4311/* Read a partial die corresponding to an enumeration type. */
4312
72bf9492
DJ
4313static void
4314add_partial_enumeration (struct partial_die_info *enum_pdi,
4315 struct dwarf2_cu *cu)
91c24f0a 4316{
72bf9492 4317 struct partial_die_info *pdi;
91c24f0a
DC
4318
4319 if (enum_pdi->name != NULL)
72bf9492
DJ
4320 add_partial_symbol (enum_pdi, cu);
4321
4322 pdi = enum_pdi->die_child;
4323 while (pdi)
91c24f0a 4324 {
72bf9492 4325 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4326 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4327 else
72bf9492
DJ
4328 add_partial_symbol (pdi, cu);
4329 pdi = pdi->die_sibling;
91c24f0a 4330 }
91c24f0a
DC
4331}
4332
6caca83c
CC
4333/* Return the initial uleb128 in the die at INFO_PTR. */
4334
4335static unsigned int
4336peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4337{
4338 unsigned int bytes_read;
4339
4340 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4341}
4342
4bb7a0a7
DJ
4343/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4344 Return the corresponding abbrev, or NULL if the number is zero (indicating
4345 an empty DIE). In either case *BYTES_READ will be set to the length of
4346 the initial number. */
4347
4348static struct abbrev_info *
fe1b8b76 4349peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4350 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4351{
4352 bfd *abfd = cu->objfile->obfd;
4353 unsigned int abbrev_number;
4354 struct abbrev_info *abbrev;
4355
4356 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4357
4358 if (abbrev_number == 0)
4359 return NULL;
4360
4361 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4362 if (!abbrev)
4363 {
3e43a32a
MS
4364 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4365 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4366 }
4367
4368 return abbrev;
4369}
4370
93311388
DE
4371/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4372 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4373 DIE. Any children of the skipped DIEs will also be skipped. */
4374
fe1b8b76 4375static gdb_byte *
93311388 4376skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4377{
4378 struct abbrev_info *abbrev;
4379 unsigned int bytes_read;
4380
4381 while (1)
4382 {
4383 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4384 if (abbrev == NULL)
4385 return info_ptr + bytes_read;
4386 else
93311388 4387 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4388 }
4389}
4390
93311388
DE
4391/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4392 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4393 abbrev corresponding to that skipped uleb128 should be passed in
4394 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4395 children. */
4396
fe1b8b76 4397static gdb_byte *
93311388
DE
4398skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4399 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4400{
4401 unsigned int bytes_read;
4402 struct attribute attr;
4403 bfd *abfd = cu->objfile->obfd;
4404 unsigned int form, i;
4405
4406 for (i = 0; i < abbrev->num_attrs; i++)
4407 {
4408 /* The only abbrev we care about is DW_AT_sibling. */
4409 if (abbrev->attrs[i].name == DW_AT_sibling)
4410 {
4411 read_attribute (&attr, &abbrev->attrs[i],
4412 abfd, info_ptr, cu);
4413 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4414 complaint (&symfile_complaints,
4415 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4416 else
b64f50a1 4417 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
4418 }
4419
4420 /* If it isn't DW_AT_sibling, skip this attribute. */
4421 form = abbrev->attrs[i].form;
4422 skip_attribute:
4423 switch (form)
4424 {
4bb7a0a7 4425 case DW_FORM_ref_addr:
ae411497
TT
4426 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4427 and later it is offset sized. */
4428 if (cu->header.version == 2)
4429 info_ptr += cu->header.addr_size;
4430 else
4431 info_ptr += cu->header.offset_size;
4432 break;
4433 case DW_FORM_addr:
4bb7a0a7
DJ
4434 info_ptr += cu->header.addr_size;
4435 break;
4436 case DW_FORM_data1:
4437 case DW_FORM_ref1:
4438 case DW_FORM_flag:
4439 info_ptr += 1;
4440 break;
2dc7f7b3
TT
4441 case DW_FORM_flag_present:
4442 break;
4bb7a0a7
DJ
4443 case DW_FORM_data2:
4444 case DW_FORM_ref2:
4445 info_ptr += 2;
4446 break;
4447 case DW_FORM_data4:
4448 case DW_FORM_ref4:
4449 info_ptr += 4;
4450 break;
4451 case DW_FORM_data8:
4452 case DW_FORM_ref8:
55f1336d 4453 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4454 info_ptr += 8;
4455 break;
4456 case DW_FORM_string:
9b1c24c8 4457 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4458 info_ptr += bytes_read;
4459 break;
2dc7f7b3 4460 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4461 case DW_FORM_strp:
4462 info_ptr += cu->header.offset_size;
4463 break;
2dc7f7b3 4464 case DW_FORM_exprloc:
4bb7a0a7
DJ
4465 case DW_FORM_block:
4466 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4467 info_ptr += bytes_read;
4468 break;
4469 case DW_FORM_block1:
4470 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4471 break;
4472 case DW_FORM_block2:
4473 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4474 break;
4475 case DW_FORM_block4:
4476 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4477 break;
4478 case DW_FORM_sdata:
4479 case DW_FORM_udata:
4480 case DW_FORM_ref_udata:
4481 info_ptr = skip_leb128 (abfd, info_ptr);
4482 break;
4483 case DW_FORM_indirect:
4484 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4485 info_ptr += bytes_read;
4486 /* We need to continue parsing from here, so just go back to
4487 the top. */
4488 goto skip_attribute;
4489
4490 default:
3e43a32a
MS
4491 error (_("Dwarf Error: Cannot handle %s "
4492 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4493 dwarf_form_name (form),
4494 bfd_get_filename (abfd));
4495 }
4496 }
4497
4498 if (abbrev->has_children)
93311388 4499 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4500 else
4501 return info_ptr;
4502}
4503
93311388
DE
4504/* Locate ORIG_PDI's sibling.
4505 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4506 in BUFFER. */
91c24f0a 4507
fe1b8b76 4508static gdb_byte *
93311388
DE
4509locate_pdi_sibling (struct partial_die_info *orig_pdi,
4510 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4511 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4512{
4513 /* Do we know the sibling already? */
72bf9492 4514
91c24f0a
DC
4515 if (orig_pdi->sibling)
4516 return orig_pdi->sibling;
4517
4518 /* Are there any children to deal with? */
4519
4520 if (!orig_pdi->has_children)
4521 return info_ptr;
4522
4bb7a0a7 4523 /* Skip the children the long way. */
91c24f0a 4524
93311388 4525 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4526}
4527
c906108c
SS
4528/* Expand this partial symbol table into a full symbol table. */
4529
4530static void
fba45db2 4531dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4532{
c906108c
SS
4533 if (pst != NULL)
4534 {
4535 if (pst->readin)
4536 {
3e43a32a
MS
4537 warning (_("bug: psymtab for %s is already read in."),
4538 pst->filename);
c906108c
SS
4539 }
4540 else
4541 {
4542 if (info_verbose)
4543 {
3e43a32a
MS
4544 printf_filtered (_("Reading in symbols for %s..."),
4545 pst->filename);
c906108c
SS
4546 gdb_flush (gdb_stdout);
4547 }
4548
10b3939b
DJ
4549 /* Restore our global data. */
4550 dwarf2_per_objfile = objfile_data (pst->objfile,
4551 dwarf2_objfile_data_key);
4552
b2ab525c
KB
4553 /* If this psymtab is constructed from a debug-only objfile, the
4554 has_section_at_zero flag will not necessarily be correct. We
4555 can get the correct value for this flag by looking at the data
4556 associated with the (presumably stripped) associated objfile. */
4557 if (pst->objfile->separate_debug_objfile_backlink)
4558 {
4559 struct dwarf2_per_objfile *dpo_backlink
4560 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4561 dwarf2_objfile_data_key);
9a619af0 4562
b2ab525c
KB
4563 dwarf2_per_objfile->has_section_at_zero
4564 = dpo_backlink->has_section_at_zero;
4565 }
4566
98bfdba5
PA
4567 dwarf2_per_objfile->reading_partial_symbols = 0;
4568
c906108c
SS
4569 psymtab_to_symtab_1 (pst);
4570
4571 /* Finish up the debug error message. */
4572 if (info_verbose)
a3f17187 4573 printf_filtered (_("done.\n"));
c906108c
SS
4574 }
4575 }
4576}
9cdd5dbd
DE
4577\f
4578/* Reading in full CUs. */
c906108c 4579
10b3939b
DJ
4580/* Add PER_CU to the queue. */
4581
4582static void
a0f42c21 4583queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4584{
4585 struct dwarf2_queue_item *item;
4586
4587 per_cu->queued = 1;
4588 item = xmalloc (sizeof (*item));
4589 item->per_cu = per_cu;
4590 item->next = NULL;
4591
4592 if (dwarf2_queue == NULL)
4593 dwarf2_queue = item;
4594 else
4595 dwarf2_queue_tail->next = item;
4596
4597 dwarf2_queue_tail = item;
4598}
4599
4600/* Process the queue. */
4601
4602static void
a0f42c21 4603process_queue (void)
10b3939b
DJ
4604{
4605 struct dwarf2_queue_item *item, *next_item;
4606
03dd20cc
DJ
4607 /* The queue starts out with one item, but following a DIE reference
4608 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4609 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4610 {
9291a0cd
TT
4611 if (dwarf2_per_objfile->using_index
4612 ? !item->per_cu->v.quick->symtab
4613 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4614 process_full_comp_unit (item->per_cu);
4615
4616 item->per_cu->queued = 0;
4617 next_item = item->next;
4618 xfree (item);
4619 }
4620
4621 dwarf2_queue_tail = NULL;
4622}
4623
4624/* Free all allocated queue entries. This function only releases anything if
4625 an error was thrown; if the queue was processed then it would have been
4626 freed as we went along. */
4627
4628static void
4629dwarf2_release_queue (void *dummy)
4630{
4631 struct dwarf2_queue_item *item, *last;
4632
4633 item = dwarf2_queue;
4634 while (item)
4635 {
4636 /* Anything still marked queued is likely to be in an
4637 inconsistent state, so discard it. */
4638 if (item->per_cu->queued)
4639 {
4640 if (item->per_cu->cu != NULL)
4641 free_one_cached_comp_unit (item->per_cu->cu);
4642 item->per_cu->queued = 0;
4643 }
4644
4645 last = item;
4646 item = item->next;
4647 xfree (last);
4648 }
4649
4650 dwarf2_queue = dwarf2_queue_tail = NULL;
4651}
4652
4653/* Read in full symbols for PST, and anything it depends on. */
4654
c906108c 4655static void
fba45db2 4656psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4657{
10b3939b 4658 struct dwarf2_per_cu_data *per_cu;
c906108c 4659 struct cleanup *back_to;
aaa75496
JB
4660 int i;
4661
4662 for (i = 0; i < pst->number_of_dependencies; i++)
4663 if (!pst->dependencies[i]->readin)
4664 {
4665 /* Inform about additional files that need to be read in. */
4666 if (info_verbose)
4667 {
a3f17187 4668 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4669 fputs_filtered (" ", gdb_stdout);
4670 wrap_here ("");
4671 fputs_filtered ("and ", gdb_stdout);
4672 wrap_here ("");
4673 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4674 wrap_here (""); /* Flush output. */
aaa75496
JB
4675 gdb_flush (gdb_stdout);
4676 }
4677 psymtab_to_symtab_1 (pst->dependencies[i]);
4678 }
4679
e38df1d0 4680 per_cu = pst->read_symtab_private;
10b3939b
DJ
4681
4682 if (per_cu == NULL)
aaa75496
JB
4683 {
4684 /* It's an include file, no symbols to read for it.
4685 Everything is in the parent symtab. */
4686 pst->readin = 1;
4687 return;
4688 }
c906108c 4689
a0f42c21 4690 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4691}
4692
93311388 4693/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4694
93311388 4695static void
a0f42c21 4696load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4697{
a0f42c21 4698 struct objfile *objfile = per_cu->objfile;
31ffec48 4699 bfd *abfd = objfile->obfd;
10b3939b 4700 struct dwarf2_cu *cu;
b64f50a1 4701 sect_offset offset;
93311388 4702 gdb_byte *info_ptr, *beg_of_comp_unit;
7fb3ad1f 4703 struct cleanup *free_cu_cleanup = NULL;
10b3939b 4704 struct attribute *attr;
98bfdba5 4705 int read_cu = 0;
6502dd73 4706
b0df02fd 4707 gdb_assert (! per_cu->debug_types_section);
348e048f 4708
c906108c 4709 /* Set local variables from the partial symbol table info. */
10b3939b 4710 offset = per_cu->offset;
6502dd73 4711
be391dca 4712 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
b64f50a1 4713 info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
93311388 4714 beg_of_comp_unit = info_ptr;
63d06c5c 4715
98bfdba5
PA
4716 if (per_cu->cu == NULL)
4717 {
9816fde3 4718 cu = xmalloc (sizeof (*cu));
23745b47 4719 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4720
4721 read_cu = 1;
c906108c 4722
98bfdba5 4723 /* If an error occurs while loading, release our storage. */
68dc6402 4724 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4725
98bfdba5
PA
4726 /* Read in the comp_unit header. */
4727 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4728
6caca83c
CC
4729 /* Skip dummy compilation units. */
4730 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4731 + dwarf2_per_objfile->info.size)
4732 || peek_abbrev_code (abfd, info_ptr) == 0)
4733 {
4734 do_cleanups (free_cu_cleanup);
4735 return;
4736 }
4737
98bfdba5
PA
4738 /* Complete the cu_header. */
4739 cu->header.offset = offset;
b64f50a1 4740 cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
98bfdba5
PA
4741 }
4742 else
4743 {
4744 cu = per_cu->cu;
b64f50a1 4745 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 4746 }
e142c38c 4747
93311388 4748 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4749
4750 /* We try not to read any attributes in this function, because not
9cdd5dbd 4751 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4752 table processing isn't initialized. But we have to set the CU language,
4753 or we won't be able to build types correctly. */
9816fde3 4754 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4755
a6c727b2
DJ
4756 /* Similarly, if we do not read the producer, we can not apply
4757 producer-specific interpretation. */
4758 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4759 if (attr)
4760 cu->producer = DW_STRING (attr);
4761
98bfdba5
PA
4762 if (read_cu)
4763 {
98bfdba5
PA
4764 /* We've successfully allocated this compilation unit. Let our
4765 caller clean it up when finished with it. */
4766 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
4767
4768 /* Link this CU into read_in_chain. */
4769 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4770 dwarf2_per_objfile->read_in_chain = per_cu;
98bfdba5 4771 }
10b3939b
DJ
4772}
4773
3da10d80
KS
4774/* Add a DIE to the delayed physname list. */
4775
4776static void
4777add_to_method_list (struct type *type, int fnfield_index, int index,
4778 const char *name, struct die_info *die,
4779 struct dwarf2_cu *cu)
4780{
4781 struct delayed_method_info mi;
4782 mi.type = type;
4783 mi.fnfield_index = fnfield_index;
4784 mi.index = index;
4785 mi.name = name;
4786 mi.die = die;
4787 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4788}
4789
4790/* A cleanup for freeing the delayed method list. */
4791
4792static void
4793free_delayed_list (void *ptr)
4794{
4795 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4796 if (cu->method_list != NULL)
4797 {
4798 VEC_free (delayed_method_info, cu->method_list);
4799 cu->method_list = NULL;
4800 }
4801}
4802
4803/* Compute the physnames of any methods on the CU's method list.
4804
4805 The computation of method physnames is delayed in order to avoid the
4806 (bad) condition that one of the method's formal parameters is of an as yet
4807 incomplete type. */
4808
4809static void
4810compute_delayed_physnames (struct dwarf2_cu *cu)
4811{
4812 int i;
4813 struct delayed_method_info *mi;
4814 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4815 {
1d06ead6 4816 const char *physname;
3da10d80
KS
4817 struct fn_fieldlist *fn_flp
4818 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4819 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4820 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4821 }
4822}
4823
9cdd5dbd 4824/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4825 already been loaded into memory. */
4826
4827static void
4828process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4829{
10b3939b 4830 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4831 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4832 CORE_ADDR lowpc, highpc;
4833 struct symtab *symtab;
3da10d80 4834 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4835 CORE_ADDR baseaddr;
4836
4837 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4838
10b3939b
DJ
4839 buildsym_init ();
4840 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4841 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4842
4843 cu->list_in_scope = &file_symbols;
c906108c
SS
4844
4845 /* Do line number decoding in read_file_scope () */
10b3939b 4846 process_die (cu->dies, cu);
c906108c 4847
3da10d80
KS
4848 /* Now that we have processed all the DIEs in the CU, all the types
4849 should be complete, and it should now be safe to compute all of the
4850 physnames. */
4851 compute_delayed_physnames (cu);
4852 do_cleanups (delayed_list_cleanup);
4853
fae299cd
DC
4854 /* Some compilers don't define a DW_AT_high_pc attribute for the
4855 compilation unit. If the DW_AT_high_pc is missing, synthesize
4856 it, by scanning the DIE's below the compilation unit. */
10b3939b 4857 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4858
613e1657 4859 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4860
8be455d7 4861 if (symtab != NULL)
c906108c 4862 {
df15bd07 4863 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4864
8be455d7
JK
4865 /* Set symtab language to language from DW_AT_language. If the
4866 compilation is from a C file generated by language preprocessors, do
4867 not set the language if it was already deduced by start_subfile. */
4868 if (!(cu->language == language_c && symtab->language != language_c))
4869 symtab->language = cu->language;
4870
4871 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4872 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
4873 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
4874 there were bugs in prologue debug info, fixed later in GCC-4.5
4875 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
4876
4877 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4878 needed, it would be wrong due to missing DW_AT_producer there.
4879
4880 Still one can confuse GDB by using non-standard GCC compilation
4881 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4882 */
ab260dad 4883 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 4884 symtab->locations_valid = 1;
e0d00bc7
JK
4885
4886 if (gcc_4_minor >= 5)
4887 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4888
4889 symtab->call_site_htab = cu->call_site_htab;
c906108c 4890 }
9291a0cd
TT
4891
4892 if (dwarf2_per_objfile->using_index)
4893 per_cu->v.quick->symtab = symtab;
4894 else
4895 {
4896 struct partial_symtab *pst = per_cu->v.psymtab;
4897 pst->symtab = symtab;
4898 pst->readin = 1;
4899 }
c906108c
SS
4900
4901 do_cleanups (back_to);
4902}
4903
4904/* Process a die and its children. */
4905
4906static void
e7c27a73 4907process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4908{
4909 switch (die->tag)
4910 {
4911 case DW_TAG_padding:
4912 break;
4913 case DW_TAG_compile_unit:
e7c27a73 4914 read_file_scope (die, cu);
c906108c 4915 break;
348e048f
DE
4916 case DW_TAG_type_unit:
4917 read_type_unit_scope (die, cu);
4918 break;
c906108c 4919 case DW_TAG_subprogram:
c906108c 4920 case DW_TAG_inlined_subroutine:
edb3359d 4921 read_func_scope (die, cu);
c906108c
SS
4922 break;
4923 case DW_TAG_lexical_block:
14898363
L
4924 case DW_TAG_try_block:
4925 case DW_TAG_catch_block:
e7c27a73 4926 read_lexical_block_scope (die, cu);
c906108c 4927 break;
96408a79
SA
4928 case DW_TAG_GNU_call_site:
4929 read_call_site_scope (die, cu);
4930 break;
c906108c 4931 case DW_TAG_class_type:
680b30c7 4932 case DW_TAG_interface_type:
c906108c
SS
4933 case DW_TAG_structure_type:
4934 case DW_TAG_union_type:
134d01f1 4935 process_structure_scope (die, cu);
c906108c
SS
4936 break;
4937 case DW_TAG_enumeration_type:
134d01f1 4938 process_enumeration_scope (die, cu);
c906108c 4939 break;
134d01f1 4940
f792889a
DJ
4941 /* These dies have a type, but processing them does not create
4942 a symbol or recurse to process the children. Therefore we can
4943 read them on-demand through read_type_die. */
c906108c 4944 case DW_TAG_subroutine_type:
72019c9c 4945 case DW_TAG_set_type:
c906108c 4946 case DW_TAG_array_type:
c906108c 4947 case DW_TAG_pointer_type:
c906108c 4948 case DW_TAG_ptr_to_member_type:
c906108c 4949 case DW_TAG_reference_type:
c906108c 4950 case DW_TAG_string_type:
c906108c 4951 break;
134d01f1 4952
c906108c 4953 case DW_TAG_base_type:
a02abb62 4954 case DW_TAG_subrange_type:
cb249c71 4955 case DW_TAG_typedef:
134d01f1
DJ
4956 /* Add a typedef symbol for the type definition, if it has a
4957 DW_AT_name. */
f792889a 4958 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4959 break;
c906108c 4960 case DW_TAG_common_block:
e7c27a73 4961 read_common_block (die, cu);
c906108c
SS
4962 break;
4963 case DW_TAG_common_inclusion:
4964 break;
d9fa45fe 4965 case DW_TAG_namespace:
63d06c5c 4966 processing_has_namespace_info = 1;
e7c27a73 4967 read_namespace (die, cu);
d9fa45fe 4968 break;
5d7cb8df 4969 case DW_TAG_module:
f55ee35c 4970 processing_has_namespace_info = 1;
5d7cb8df
JK
4971 read_module (die, cu);
4972 break;
d9fa45fe
DC
4973 case DW_TAG_imported_declaration:
4974 case DW_TAG_imported_module:
63d06c5c 4975 processing_has_namespace_info = 1;
27aa8d6a
SW
4976 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4977 || cu->language != language_fortran))
4978 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4979 dwarf_tag_name (die->tag));
4980 read_import_statement (die, cu);
d9fa45fe 4981 break;
c906108c 4982 default:
e7c27a73 4983 new_symbol (die, NULL, cu);
c906108c
SS
4984 break;
4985 }
4986}
4987
94af9270
KS
4988/* A helper function for dwarf2_compute_name which determines whether DIE
4989 needs to have the name of the scope prepended to the name listed in the
4990 die. */
4991
4992static int
4993die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4994{
1c809c68
TT
4995 struct attribute *attr;
4996
94af9270
KS
4997 switch (die->tag)
4998 {
4999 case DW_TAG_namespace:
5000 case DW_TAG_typedef:
5001 case DW_TAG_class_type:
5002 case DW_TAG_interface_type:
5003 case DW_TAG_structure_type:
5004 case DW_TAG_union_type:
5005 case DW_TAG_enumeration_type:
5006 case DW_TAG_enumerator:
5007 case DW_TAG_subprogram:
5008 case DW_TAG_member:
5009 return 1;
5010
5011 case DW_TAG_variable:
c2b0a229 5012 case DW_TAG_constant:
94af9270
KS
5013 /* We only need to prefix "globally" visible variables. These include
5014 any variable marked with DW_AT_external or any variable that
5015 lives in a namespace. [Variables in anonymous namespaces
5016 require prefixing, but they are not DW_AT_external.] */
5017
5018 if (dwarf2_attr (die, DW_AT_specification, cu))
5019 {
5020 struct dwarf2_cu *spec_cu = cu;
9a619af0 5021
94af9270
KS
5022 return die_needs_namespace (die_specification (die, &spec_cu),
5023 spec_cu);
5024 }
5025
1c809c68 5026 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5027 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5028 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5029 return 0;
5030 /* A variable in a lexical block of some kind does not need a
5031 namespace, even though in C++ such variables may be external
5032 and have a mangled name. */
5033 if (die->parent->tag == DW_TAG_lexical_block
5034 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5035 || die->parent->tag == DW_TAG_catch_block
5036 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5037 return 0;
5038 return 1;
94af9270
KS
5039
5040 default:
5041 return 0;
5042 }
5043}
5044
98bfdba5
PA
5045/* Retrieve the last character from a mem_file. */
5046
5047static void
5048do_ui_file_peek_last (void *object, const char *buffer, long length)
5049{
5050 char *last_char_p = (char *) object;
5051
5052 if (length > 0)
5053 *last_char_p = buffer[length - 1];
5054}
5055
94af9270
KS
5056/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5057 compute the physname for the object, which include a method's
5058 formal parameters (C++/Java) and return type (Java).
5059
af6b7be1
JB
5060 For Ada, return the DIE's linkage name rather than the fully qualified
5061 name. PHYSNAME is ignored..
5062
94af9270
KS
5063 The result is allocated on the objfile_obstack and canonicalized. */
5064
5065static const char *
5066dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5067 int physname)
5068{
bb5ed363
DE
5069 struct objfile *objfile = cu->objfile;
5070
94af9270
KS
5071 if (name == NULL)
5072 name = dwarf2_name (die, cu);
5073
f55ee35c
JK
5074 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5075 compute it by typename_concat inside GDB. */
5076 if (cu->language == language_ada
5077 || (cu->language == language_fortran && physname))
5078 {
5079 /* For Ada unit, we prefer the linkage name over the name, as
5080 the former contains the exported name, which the user expects
5081 to be able to reference. Ideally, we want the user to be able
5082 to reference this entity using either natural or linkage name,
5083 but we haven't started looking at this enhancement yet. */
5084 struct attribute *attr;
5085
5086 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5087 if (attr == NULL)
5088 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5089 if (attr && DW_STRING (attr))
5090 return DW_STRING (attr);
5091 }
5092
94af9270
KS
5093 /* These are the only languages we know how to qualify names in. */
5094 if (name != NULL
f55ee35c
JK
5095 && (cu->language == language_cplus || cu->language == language_java
5096 || cu->language == language_fortran))
94af9270
KS
5097 {
5098 if (die_needs_namespace (die, cu))
5099 {
5100 long length;
0d5cff50 5101 const char *prefix;
94af9270
KS
5102 struct ui_file *buf;
5103
5104 prefix = determine_prefix (die, cu);
5105 buf = mem_fileopen ();
5106 if (*prefix != '\0')
5107 {
f55ee35c
JK
5108 char *prefixed_name = typename_concat (NULL, prefix, name,
5109 physname, cu);
9a619af0 5110
94af9270
KS
5111 fputs_unfiltered (prefixed_name, buf);
5112 xfree (prefixed_name);
5113 }
5114 else
62d5b8da 5115 fputs_unfiltered (name, buf);
94af9270 5116
98bfdba5
PA
5117 /* Template parameters may be specified in the DIE's DW_AT_name, or
5118 as children with DW_TAG_template_type_param or
5119 DW_TAG_value_type_param. If the latter, add them to the name
5120 here. If the name already has template parameters, then
5121 skip this step; some versions of GCC emit both, and
5122 it is more efficient to use the pre-computed name.
5123
5124 Something to keep in mind about this process: it is very
5125 unlikely, or in some cases downright impossible, to produce
5126 something that will match the mangled name of a function.
5127 If the definition of the function has the same debug info,
5128 we should be able to match up with it anyway. But fallbacks
5129 using the minimal symbol, for instance to find a method
5130 implemented in a stripped copy of libstdc++, will not work.
5131 If we do not have debug info for the definition, we will have to
5132 match them up some other way.
5133
5134 When we do name matching there is a related problem with function
5135 templates; two instantiated function templates are allowed to
5136 differ only by their return types, which we do not add here. */
5137
5138 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5139 {
5140 struct attribute *attr;
5141 struct die_info *child;
5142 int first = 1;
5143
5144 die->building_fullname = 1;
5145
5146 for (child = die->child; child != NULL; child = child->sibling)
5147 {
5148 struct type *type;
5149 long value;
5150 gdb_byte *bytes;
5151 struct dwarf2_locexpr_baton *baton;
5152 struct value *v;
5153
5154 if (child->tag != DW_TAG_template_type_param
5155 && child->tag != DW_TAG_template_value_param)
5156 continue;
5157
5158 if (first)
5159 {
5160 fputs_unfiltered ("<", buf);
5161 first = 0;
5162 }
5163 else
5164 fputs_unfiltered (", ", buf);
5165
5166 attr = dwarf2_attr (child, DW_AT_type, cu);
5167 if (attr == NULL)
5168 {
5169 complaint (&symfile_complaints,
5170 _("template parameter missing DW_AT_type"));
5171 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5172 continue;
5173 }
5174 type = die_type (child, cu);
5175
5176 if (child->tag == DW_TAG_template_type_param)
5177 {
5178 c_print_type (type, "", buf, -1, 0);
5179 continue;
5180 }
5181
5182 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5183 if (attr == NULL)
5184 {
5185 complaint (&symfile_complaints,
3e43a32a
MS
5186 _("template parameter missing "
5187 "DW_AT_const_value"));
98bfdba5
PA
5188 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5189 continue;
5190 }
5191
5192 dwarf2_const_value_attr (attr, type, name,
5193 &cu->comp_unit_obstack, cu,
5194 &value, &bytes, &baton);
5195
5196 if (TYPE_NOSIGN (type))
5197 /* GDB prints characters as NUMBER 'CHAR'. If that's
5198 changed, this can use value_print instead. */
5199 c_printchar (value, type, buf);
5200 else
5201 {
5202 struct value_print_options opts;
5203
5204 if (baton != NULL)
5205 v = dwarf2_evaluate_loc_desc (type, NULL,
5206 baton->data,
5207 baton->size,
5208 baton->per_cu);
5209 else if (bytes != NULL)
5210 {
5211 v = allocate_value (type);
5212 memcpy (value_contents_writeable (v), bytes,
5213 TYPE_LENGTH (type));
5214 }
5215 else
5216 v = value_from_longest (type, value);
5217
3e43a32a
MS
5218 /* Specify decimal so that we do not depend on
5219 the radix. */
98bfdba5
PA
5220 get_formatted_print_options (&opts, 'd');
5221 opts.raw = 1;
5222 value_print (v, buf, &opts);
5223 release_value (v);
5224 value_free (v);
5225 }
5226 }
5227
5228 die->building_fullname = 0;
5229
5230 if (!first)
5231 {
5232 /* Close the argument list, with a space if necessary
5233 (nested templates). */
5234 char last_char = '\0';
5235 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5236 if (last_char == '>')
5237 fputs_unfiltered (" >", buf);
5238 else
5239 fputs_unfiltered (">", buf);
5240 }
5241 }
5242
94af9270
KS
5243 /* For Java and C++ methods, append formal parameter type
5244 information, if PHYSNAME. */
6e70227d 5245
94af9270
KS
5246 if (physname && die->tag == DW_TAG_subprogram
5247 && (cu->language == language_cplus
5248 || cu->language == language_java))
5249 {
5250 struct type *type = read_type_die (die, cu);
5251
3167638f 5252 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5253
5254 if (cu->language == language_java)
5255 {
5256 /* For java, we must append the return type to method
0963b4bd 5257 names. */
94af9270
KS
5258 if (die->tag == DW_TAG_subprogram)
5259 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5260 0, 0);
5261 }
5262 else if (cu->language == language_cplus)
5263 {
60430eff
DJ
5264 /* Assume that an artificial first parameter is
5265 "this", but do not crash if it is not. RealView
5266 marks unnamed (and thus unused) parameters as
5267 artificial; there is no way to differentiate
5268 the two cases. */
94af9270
KS
5269 if (TYPE_NFIELDS (type) > 0
5270 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5271 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5272 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5273 0))))
94af9270
KS
5274 fputs_unfiltered (" const", buf);
5275 }
5276 }
5277
bb5ed363 5278 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5279 &length);
5280 ui_file_delete (buf);
5281
5282 if (cu->language == language_cplus)
5283 {
5284 char *cname
5285 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5286 &objfile->objfile_obstack);
9a619af0 5287
94af9270
KS
5288 if (cname != NULL)
5289 name = cname;
5290 }
5291 }
5292 }
5293
5294 return name;
5295}
5296
0114d602
DJ
5297/* Return the fully qualified name of DIE, based on its DW_AT_name.
5298 If scope qualifiers are appropriate they will be added. The result
5299 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5300 not have a name. NAME may either be from a previous call to
5301 dwarf2_name or NULL.
5302
0963b4bd 5303 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5304
5305static const char *
94af9270 5306dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5307{
94af9270
KS
5308 return dwarf2_compute_name (name, die, cu, 0);
5309}
0114d602 5310
94af9270
KS
5311/* Construct a physname for the given DIE in CU. NAME may either be
5312 from a previous call to dwarf2_name or NULL. The result will be
5313 allocated on the objfile_objstack or NULL if the DIE does not have a
5314 name.
0114d602 5315
94af9270 5316 The output string will be canonicalized (if C++/Java). */
0114d602 5317
94af9270
KS
5318static const char *
5319dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5320{
bb5ed363 5321 struct objfile *objfile = cu->objfile;
900e11f9
JK
5322 struct attribute *attr;
5323 const char *retval, *mangled = NULL, *canon = NULL;
5324 struct cleanup *back_to;
5325 int need_copy = 1;
5326
5327 /* In this case dwarf2_compute_name is just a shortcut not building anything
5328 on its own. */
5329 if (!die_needs_namespace (die, cu))
5330 return dwarf2_compute_name (name, die, cu, 1);
5331
5332 back_to = make_cleanup (null_cleanup, NULL);
5333
5334 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5335 if (!attr)
5336 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5337
5338 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5339 has computed. */
5340 if (attr && DW_STRING (attr))
5341 {
5342 char *demangled;
5343
5344 mangled = DW_STRING (attr);
5345
5346 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5347 type. It is easier for GDB users to search for such functions as
5348 `name(params)' than `long name(params)'. In such case the minimal
5349 symbol names do not match the full symbol names but for template
5350 functions there is never a need to look up their definition from their
5351 declaration so the only disadvantage remains the minimal symbol
5352 variant `long name(params)' does not have the proper inferior type.
5353 */
5354
5355 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5356 | (cu->language == language_java
5357 ? DMGL_JAVA | DMGL_RET_POSTFIX
5358 : DMGL_RET_DROP)));
5359 if (demangled)
5360 {
5361 make_cleanup (xfree, demangled);
5362 canon = demangled;
5363 }
5364 else
5365 {
5366 canon = mangled;
5367 need_copy = 0;
5368 }
5369 }
5370
5371 if (canon == NULL || check_physname)
5372 {
5373 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5374
5375 if (canon != NULL && strcmp (physname, canon) != 0)
5376 {
5377 /* It may not mean a bug in GDB. The compiler could also
5378 compute DW_AT_linkage_name incorrectly. But in such case
5379 GDB would need to be bug-to-bug compatible. */
5380
5381 complaint (&symfile_complaints,
5382 _("Computed physname <%s> does not match demangled <%s> "
5383 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 5384 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
5385
5386 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5387 is available here - over computed PHYSNAME. It is safer
5388 against both buggy GDB and buggy compilers. */
5389
5390 retval = canon;
5391 }
5392 else
5393 {
5394 retval = physname;
5395 need_copy = 0;
5396 }
5397 }
5398 else
5399 retval = canon;
5400
5401 if (need_copy)
5402 retval = obsavestring (retval, strlen (retval),
bb5ed363 5403 &objfile->objfile_obstack);
900e11f9
JK
5404
5405 do_cleanups (back_to);
5406 return retval;
0114d602
DJ
5407}
5408
27aa8d6a
SW
5409/* Read the import statement specified by the given die and record it. */
5410
5411static void
5412read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5413{
bb5ed363 5414 struct objfile *objfile = cu->objfile;
27aa8d6a 5415 struct attribute *import_attr;
32019081 5416 struct die_info *imported_die, *child_die;
de4affc9 5417 struct dwarf2_cu *imported_cu;
27aa8d6a 5418 const char *imported_name;
794684b6 5419 const char *imported_name_prefix;
13387711
SW
5420 const char *canonical_name;
5421 const char *import_alias;
5422 const char *imported_declaration = NULL;
794684b6 5423 const char *import_prefix;
32019081
JK
5424 VEC (const_char_ptr) *excludes = NULL;
5425 struct cleanup *cleanups;
13387711
SW
5426
5427 char *temp;
27aa8d6a
SW
5428
5429 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5430 if (import_attr == NULL)
5431 {
5432 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5433 dwarf_tag_name (die->tag));
5434 return;
5435 }
5436
de4affc9
CC
5437 imported_cu = cu;
5438 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5439 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5440 if (imported_name == NULL)
5441 {
5442 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5443
5444 The import in the following code:
5445 namespace A
5446 {
5447 typedef int B;
5448 }
5449
5450 int main ()
5451 {
5452 using A::B;
5453 B b;
5454 return b;
5455 }
5456
5457 ...
5458 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5459 <52> DW_AT_decl_file : 1
5460 <53> DW_AT_decl_line : 6
5461 <54> DW_AT_import : <0x75>
5462 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5463 <59> DW_AT_name : B
5464 <5b> DW_AT_decl_file : 1
5465 <5c> DW_AT_decl_line : 2
5466 <5d> DW_AT_type : <0x6e>
5467 ...
5468 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5469 <76> DW_AT_byte_size : 4
5470 <77> DW_AT_encoding : 5 (signed)
5471
5472 imports the wrong die ( 0x75 instead of 0x58 ).
5473 This case will be ignored until the gcc bug is fixed. */
5474 return;
5475 }
5476
82856980
SW
5477 /* Figure out the local name after import. */
5478 import_alias = dwarf2_name (die, cu);
27aa8d6a 5479
794684b6
SW
5480 /* Figure out where the statement is being imported to. */
5481 import_prefix = determine_prefix (die, cu);
5482
5483 /* Figure out what the scope of the imported die is and prepend it
5484 to the name of the imported die. */
de4affc9 5485 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5486
f55ee35c
JK
5487 if (imported_die->tag != DW_TAG_namespace
5488 && imported_die->tag != DW_TAG_module)
794684b6 5489 {
13387711
SW
5490 imported_declaration = imported_name;
5491 canonical_name = imported_name_prefix;
794684b6 5492 }
13387711 5493 else if (strlen (imported_name_prefix) > 0)
794684b6 5494 {
13387711
SW
5495 temp = alloca (strlen (imported_name_prefix)
5496 + 2 + strlen (imported_name) + 1);
5497 strcpy (temp, imported_name_prefix);
5498 strcat (temp, "::");
5499 strcat (temp, imported_name);
5500 canonical_name = temp;
794684b6 5501 }
13387711
SW
5502 else
5503 canonical_name = imported_name;
794684b6 5504
32019081
JK
5505 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5506
5507 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5508 for (child_die = die->child; child_die && child_die->tag;
5509 child_die = sibling_die (child_die))
5510 {
5511 /* DWARF-4: A Fortran use statement with a “rename list” may be
5512 represented by an imported module entry with an import attribute
5513 referring to the module and owned entries corresponding to those
5514 entities that are renamed as part of being imported. */
5515
5516 if (child_die->tag != DW_TAG_imported_declaration)
5517 {
5518 complaint (&symfile_complaints,
5519 _("child DW_TAG_imported_declaration expected "
5520 "- DIE at 0x%x [in module %s]"),
b64f50a1 5521 child_die->offset.sect_off, objfile->name);
32019081
JK
5522 continue;
5523 }
5524
5525 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5526 if (import_attr == NULL)
5527 {
5528 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5529 dwarf_tag_name (child_die->tag));
5530 continue;
5531 }
5532
5533 imported_cu = cu;
5534 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5535 &imported_cu);
5536 imported_name = dwarf2_name (imported_die, imported_cu);
5537 if (imported_name == NULL)
5538 {
5539 complaint (&symfile_complaints,
5540 _("child DW_TAG_imported_declaration has unknown "
5541 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 5542 child_die->offset.sect_off, objfile->name);
32019081
JK
5543 continue;
5544 }
5545
5546 VEC_safe_push (const_char_ptr, excludes, imported_name);
5547
5548 process_die (child_die, cu);
5549 }
5550
c0cc3a76
SW
5551 cp_add_using_directive (import_prefix,
5552 canonical_name,
5553 import_alias,
13387711 5554 imported_declaration,
32019081 5555 excludes,
bb5ed363 5556 &objfile->objfile_obstack);
32019081
JK
5557
5558 do_cleanups (cleanups);
27aa8d6a
SW
5559}
5560
ae2de4f8
DE
5561/* Cleanup function for read_file_scope. */
5562
cb1df416
DJ
5563static void
5564free_cu_line_header (void *arg)
5565{
5566 struct dwarf2_cu *cu = arg;
5567
5568 free_line_header (cu->line_header);
5569 cu->line_header = NULL;
5570}
5571
9291a0cd
TT
5572static void
5573find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5574 char **name, char **comp_dir)
5575{
5576 struct attribute *attr;
5577
5578 *name = NULL;
5579 *comp_dir = NULL;
5580
5581 /* Find the filename. Do not use dwarf2_name here, since the filename
5582 is not a source language identifier. */
5583 attr = dwarf2_attr (die, DW_AT_name, cu);
5584 if (attr)
5585 {
5586 *name = DW_STRING (attr);
5587 }
5588
5589 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5590 if (attr)
5591 *comp_dir = DW_STRING (attr);
5592 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5593 {
5594 *comp_dir = ldirname (*name);
5595 if (*comp_dir != NULL)
5596 make_cleanup (xfree, *comp_dir);
5597 }
5598 if (*comp_dir != NULL)
5599 {
5600 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5601 directory, get rid of it. */
5602 char *cp = strchr (*comp_dir, ':');
5603
5604 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5605 *comp_dir = cp + 1;
5606 }
5607
5608 if (*name == NULL)
5609 *name = "<unknown>";
5610}
5611
f3f5162e
DE
5612/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5613 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5614 COMP_DIR is the compilation directory.
5615 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5616
5617static void
5618handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5619 const char *comp_dir, int want_line_info)
2ab95328
TT
5620{
5621 struct attribute *attr;
5622 struct objfile *objfile = cu->objfile;
5623 bfd *abfd = objfile->obfd;
5624
2ab95328
TT
5625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5626 if (attr)
5627 {
5628 unsigned int line_offset = DW_UNSND (attr);
5629 struct line_header *line_header
5630 = dwarf_decode_line_header (line_offset, abfd, cu);
5631
5632 if (line_header)
5633 {
5634 cu->line_header = line_header;
5635 make_cleanup (free_cu_line_header, cu);
f3f5162e 5636 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5637 }
5638 }
5639}
5640
ae2de4f8
DE
5641/* Process DW_TAG_compile_unit. */
5642
c906108c 5643static void
e7c27a73 5644read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5645{
e7c27a73 5646 struct objfile *objfile = cu->objfile;
debd256d 5647 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5648 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5649 CORE_ADDR highpc = ((CORE_ADDR) 0);
5650 struct attribute *attr;
e1024ff1 5651 char *name = NULL;
c906108c
SS
5652 char *comp_dir = NULL;
5653 struct die_info *child_die;
5654 bfd *abfd = objfile->obfd;
e142c38c 5655 CORE_ADDR baseaddr;
6e70227d 5656
e142c38c 5657 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5658
fae299cd 5659 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5660
5661 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5662 from finish_block. */
2acceee2 5663 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5664 lowpc = highpc;
5665 lowpc += baseaddr;
5666 highpc += baseaddr;
5667
9291a0cd 5668 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5669
e142c38c 5670 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5671 if (attr)
5672 {
e142c38c 5673 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5674 }
5675
b0f35d58 5676 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5677 if (attr)
b0f35d58 5678 cu->producer = DW_STRING (attr);
303b6f5d 5679
f4b8a18d
KW
5680 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5681 standardised yet. As a workaround for the language detection we fall
5682 back to the DW_AT_producer string. */
5683 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5684 cu->language = language_opencl;
5685
0963b4bd 5686 /* We assume that we're processing GCC output. */
c906108c 5687 processing_gcc_compilation = 2;
c906108c 5688
df8a16a1
DJ
5689 processing_has_namespace_info = 0;
5690
c906108c
SS
5691 start_symtab (name, comp_dir, lowpc);
5692 record_debugformat ("DWARF 2");
303b6f5d 5693 record_producer (cu->producer);
c906108c 5694
f3f5162e
DE
5695 /* Decode line number information if present. We do this before
5696 processing child DIEs, so that the line header table is available
5697 for DW_AT_decl_file. */
5698 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5699
cb1df416
DJ
5700 /* Process all dies in compilation unit. */
5701 if (die->child != NULL)
5702 {
5703 child_die = die->child;
5704 while (child_die && child_die->tag)
5705 {
5706 process_die (child_die, cu);
5707 child_die = sibling_die (child_die);
5708 }
5709 }
5710
2e276125
JB
5711 /* Decode macro information, if present. Dwarf 2 macro information
5712 refers to information in the line number info statement program
5713 header, so we can only read it if we've read the header
5714 successfully. */
cf2c3c16 5715 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5716 if (attr && cu->line_header)
2e276125 5717 {
cf2c3c16
TT
5718 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5719 complaint (&symfile_complaints,
5720 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5721
5722 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5723 comp_dir, abfd, cu,
5724 &dwarf2_per_objfile->macro, 1);
5725 }
5726 else
5727 {
5728 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5729 if (attr && cu->line_header)
5730 {
5731 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5732
cf2c3c16
TT
5733 dwarf_decode_macros (cu->line_header, macro_offset,
5734 comp_dir, abfd, cu,
5735 &dwarf2_per_objfile->macinfo, 0);
5736 }
2e276125 5737 }
9cdd5dbd 5738
debd256d 5739 do_cleanups (back_to);
5fb290d7
DJ
5740}
5741
ae2de4f8
DE
5742/* Process DW_TAG_type_unit.
5743 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5744 actual type being defined by this TU. In this case the first top
5745 level sibling is there to provide context only. */
5746
5747static void
5748read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5749{
5750 struct objfile *objfile = cu->objfile;
5751 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5752 CORE_ADDR lowpc;
5753 struct attribute *attr;
5754 char *name = NULL;
5755 char *comp_dir = NULL;
5756 struct die_info *child_die;
5757 bfd *abfd = objfile->obfd;
348e048f
DE
5758
5759 /* start_symtab needs a low pc, but we don't really have one.
5760 Do what read_file_scope would do in the absence of such info. */
5761 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5762
5763 /* Find the filename. Do not use dwarf2_name here, since the filename
5764 is not a source language identifier. */
5765 attr = dwarf2_attr (die, DW_AT_name, cu);
5766 if (attr)
5767 name = DW_STRING (attr);
5768
5769 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5770 if (attr)
5771 comp_dir = DW_STRING (attr);
5772 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5773 {
5774 comp_dir = ldirname (name);
5775 if (comp_dir != NULL)
5776 make_cleanup (xfree, comp_dir);
5777 }
5778
5779 if (name == NULL)
5780 name = "<unknown>";
5781
5782 attr = dwarf2_attr (die, DW_AT_language, cu);
5783 if (attr)
5784 set_cu_language (DW_UNSND (attr), cu);
5785
5786 /* This isn't technically needed today. It is done for symmetry
5787 with read_file_scope. */
5788 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5789 if (attr)
348e048f
DE
5790 cu->producer = DW_STRING (attr);
5791
0963b4bd 5792 /* We assume that we're processing GCC output. */
348e048f
DE
5793 processing_gcc_compilation = 2;
5794
5795 processing_has_namespace_info = 0;
5796
5797 start_symtab (name, comp_dir, lowpc);
5798 record_debugformat ("DWARF 2");
5799 record_producer (cu->producer);
5800
f3f5162e
DE
5801 /* Decode line number information if present. We do this before
5802 processing child DIEs, so that the line header table is available
5803 for DW_AT_decl_file.
5804 We don't need the pc/line-number mapping for type units. */
5805 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5806
348e048f
DE
5807 /* Process the dies in the type unit. */
5808 if (die->child == NULL)
5809 {
5810 dump_die_for_error (die);
5811 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5812 bfd_get_filename (abfd));
5813 }
5814
5815 child_die = die->child;
5816
5817 while (child_die && child_die->tag)
5818 {
5819 process_die (child_die, cu);
5820
5821 child_die = sibling_die (child_die);
5822 }
5823
5824 do_cleanups (back_to);
5825}
5826
d389af10
JK
5827/* qsort helper for inherit_abstract_dies. */
5828
5829static int
5830unsigned_int_compar (const void *ap, const void *bp)
5831{
5832 unsigned int a = *(unsigned int *) ap;
5833 unsigned int b = *(unsigned int *) bp;
5834
5835 return (a > b) - (b > a);
5836}
5837
5838/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5839 Inherit only the children of the DW_AT_abstract_origin DIE not being
5840 already referenced by DW_AT_abstract_origin from the children of the
5841 current DIE. */
d389af10
JK
5842
5843static void
5844inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5845{
5846 struct die_info *child_die;
5847 unsigned die_children_count;
5848 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
5849 sect_offset *offsets;
5850 sect_offset *offsets_end, *offsetp;
d389af10
JK
5851 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5852 struct die_info *origin_die;
5853 /* Iterator of the ORIGIN_DIE children. */
5854 struct die_info *origin_child_die;
5855 struct cleanup *cleanups;
5856 struct attribute *attr;
cd02d79d
PA
5857 struct dwarf2_cu *origin_cu;
5858 struct pending **origin_previous_list_in_scope;
d389af10
JK
5859
5860 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5861 if (!attr)
5862 return;
5863
cd02d79d
PA
5864 /* Note that following die references may follow to a die in a
5865 different cu. */
5866
5867 origin_cu = cu;
5868 origin_die = follow_die_ref (die, attr, &origin_cu);
5869
5870 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5871 symbols in. */
5872 origin_previous_list_in_scope = origin_cu->list_in_scope;
5873 origin_cu->list_in_scope = cu->list_in_scope;
5874
edb3359d
DJ
5875 if (die->tag != origin_die->tag
5876 && !(die->tag == DW_TAG_inlined_subroutine
5877 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5878 complaint (&symfile_complaints,
5879 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 5880 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
5881
5882 child_die = die->child;
5883 die_children_count = 0;
5884 while (child_die && child_die->tag)
5885 {
5886 child_die = sibling_die (child_die);
5887 die_children_count++;
5888 }
5889 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5890 cleanups = make_cleanup (xfree, offsets);
5891
5892 offsets_end = offsets;
5893 child_die = die->child;
5894 while (child_die && child_die->tag)
5895 {
c38f313d
DJ
5896 /* For each CHILD_DIE, find the corresponding child of
5897 ORIGIN_DIE. If there is more than one layer of
5898 DW_AT_abstract_origin, follow them all; there shouldn't be,
5899 but GCC versions at least through 4.4 generate this (GCC PR
5900 40573). */
5901 struct die_info *child_origin_die = child_die;
cd02d79d 5902 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5903
c38f313d
DJ
5904 while (1)
5905 {
cd02d79d
PA
5906 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5907 child_origin_cu);
c38f313d
DJ
5908 if (attr == NULL)
5909 break;
cd02d79d
PA
5910 child_origin_die = follow_die_ref (child_origin_die, attr,
5911 &child_origin_cu);
c38f313d
DJ
5912 }
5913
d389af10
JK
5914 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5915 counterpart may exist. */
c38f313d 5916 if (child_origin_die != child_die)
d389af10 5917 {
edb3359d
DJ
5918 if (child_die->tag != child_origin_die->tag
5919 && !(child_die->tag == DW_TAG_inlined_subroutine
5920 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5921 complaint (&symfile_complaints,
5922 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
5923 "different tags"), child_die->offset.sect_off,
5924 child_origin_die->offset.sect_off);
c38f313d
DJ
5925 if (child_origin_die->parent != origin_die)
5926 complaint (&symfile_complaints,
5927 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
5928 "different parents"), child_die->offset.sect_off,
5929 child_origin_die->offset.sect_off);
c38f313d
DJ
5930 else
5931 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5932 }
5933 child_die = sibling_die (child_die);
5934 }
5935 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5936 unsigned_int_compar);
5937 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 5938 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
5939 complaint (&symfile_complaints,
5940 _("Multiple children of DIE 0x%x refer "
5941 "to DIE 0x%x as their abstract origin"),
b64f50a1 5942 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
5943
5944 offsetp = offsets;
5945 origin_child_die = origin_die->child;
5946 while (origin_child_die && origin_child_die->tag)
5947 {
5948 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
5949 while (offsetp < offsets_end
5950 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 5951 offsetp++;
b64f50a1
JK
5952 if (offsetp >= offsets_end
5953 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
5954 {
5955 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5956 process_die (origin_child_die, origin_cu);
d389af10
JK
5957 }
5958 origin_child_die = sibling_die (origin_child_die);
5959 }
cd02d79d 5960 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5961
5962 do_cleanups (cleanups);
5963}
5964
c906108c 5965static void
e7c27a73 5966read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5967{
e7c27a73 5968 struct objfile *objfile = cu->objfile;
52f0bd74 5969 struct context_stack *new;
c906108c
SS
5970 CORE_ADDR lowpc;
5971 CORE_ADDR highpc;
5972 struct die_info *child_die;
edb3359d 5973 struct attribute *attr, *call_line, *call_file;
c906108c 5974 char *name;
e142c38c 5975 CORE_ADDR baseaddr;
801e3a5b 5976 struct block *block;
edb3359d 5977 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5978 VEC (symbolp) *template_args = NULL;
5979 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5980
5981 if (inlined_func)
5982 {
5983 /* If we do not have call site information, we can't show the
5984 caller of this inlined function. That's too confusing, so
5985 only use the scope for local variables. */
5986 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5987 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5988 if (call_line == NULL || call_file == NULL)
5989 {
5990 read_lexical_block_scope (die, cu);
5991 return;
5992 }
5993 }
c906108c 5994
e142c38c
DJ
5995 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5996
94af9270 5997 name = dwarf2_name (die, cu);
c906108c 5998
e8d05480
JB
5999 /* Ignore functions with missing or empty names. These are actually
6000 illegal according to the DWARF standard. */
6001 if (name == NULL)
6002 {
6003 complaint (&symfile_complaints,
b64f50a1
JK
6004 _("missing name for subprogram DIE at %d"),
6005 die->offset.sect_off);
e8d05480
JB
6006 return;
6007 }
6008
6009 /* Ignore functions with missing or invalid low and high pc attributes. */
6010 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6011 {
ae4d0c03
PM
6012 attr = dwarf2_attr (die, DW_AT_external, cu);
6013 if (!attr || !DW_UNSND (attr))
6014 complaint (&symfile_complaints,
3e43a32a
MS
6015 _("cannot get low and high bounds "
6016 "for subprogram DIE at %d"),
b64f50a1 6017 die->offset.sect_off);
e8d05480
JB
6018 return;
6019 }
c906108c
SS
6020
6021 lowpc += baseaddr;
6022 highpc += baseaddr;
6023
34eaf542
TT
6024 /* If we have any template arguments, then we must allocate a
6025 different sort of symbol. */
6026 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6027 {
6028 if (child_die->tag == DW_TAG_template_type_param
6029 || child_die->tag == DW_TAG_template_value_param)
6030 {
6031 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6032 struct template_symbol);
6033 templ_func->base.is_cplus_template_function = 1;
6034 break;
6035 }
6036 }
6037
c906108c 6038 new = push_context (0, lowpc);
34eaf542
TT
6039 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6040 (struct symbol *) templ_func);
4c2df51b 6041
4cecd739
DJ
6042 /* If there is a location expression for DW_AT_frame_base, record
6043 it. */
e142c38c 6044 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6045 if (attr)
c034e007
AC
6046 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6047 expression is being recorded directly in the function's symbol
6048 and not in a separate frame-base object. I guess this hack is
6049 to avoid adding some sort of frame-base adjunct/annex to the
6050 function's symbol :-(. The problem with doing this is that it
6051 results in a function symbol with a location expression that
6052 has nothing to do with the location of the function, ouch! The
6053 relationship should be: a function's symbol has-a frame base; a
6054 frame-base has-a location expression. */
e7c27a73 6055 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6056
e142c38c 6057 cu->list_in_scope = &local_symbols;
c906108c 6058
639d11d3 6059 if (die->child != NULL)
c906108c 6060 {
639d11d3 6061 child_die = die->child;
c906108c
SS
6062 while (child_die && child_die->tag)
6063 {
34eaf542
TT
6064 if (child_die->tag == DW_TAG_template_type_param
6065 || child_die->tag == DW_TAG_template_value_param)
6066 {
6067 struct symbol *arg = new_symbol (child_die, NULL, cu);
6068
f1078f66
DJ
6069 if (arg != NULL)
6070 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6071 }
6072 else
6073 process_die (child_die, cu);
c906108c
SS
6074 child_die = sibling_die (child_die);
6075 }
6076 }
6077
d389af10
JK
6078 inherit_abstract_dies (die, cu);
6079
4a811a97
UW
6080 /* If we have a DW_AT_specification, we might need to import using
6081 directives from the context of the specification DIE. See the
6082 comment in determine_prefix. */
6083 if (cu->language == language_cplus
6084 && dwarf2_attr (die, DW_AT_specification, cu))
6085 {
6086 struct dwarf2_cu *spec_cu = cu;
6087 struct die_info *spec_die = die_specification (die, &spec_cu);
6088
6089 while (spec_die)
6090 {
6091 child_die = spec_die->child;
6092 while (child_die && child_die->tag)
6093 {
6094 if (child_die->tag == DW_TAG_imported_module)
6095 process_die (child_die, spec_cu);
6096 child_die = sibling_die (child_die);
6097 }
6098
6099 /* In some cases, GCC generates specification DIEs that
6100 themselves contain DW_AT_specification attributes. */
6101 spec_die = die_specification (spec_die, &spec_cu);
6102 }
6103 }
6104
c906108c
SS
6105 new = pop_context ();
6106 /* Make a block for the local symbols within. */
801e3a5b
JB
6107 block = finish_block (new->name, &local_symbols, new->old_blocks,
6108 lowpc, highpc, objfile);
6109
df8a16a1 6110 /* For C++, set the block's scope. */
f55ee35c 6111 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6112 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6113 determine_prefix (die, cu),
df8a16a1
DJ
6114 processing_has_namespace_info);
6115
801e3a5b
JB
6116 /* If we have address ranges, record them. */
6117 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6118
34eaf542
TT
6119 /* Attach template arguments to function. */
6120 if (! VEC_empty (symbolp, template_args))
6121 {
6122 gdb_assert (templ_func != NULL);
6123
6124 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6125 templ_func->template_arguments
6126 = obstack_alloc (&objfile->objfile_obstack,
6127 (templ_func->n_template_arguments
6128 * sizeof (struct symbol *)));
6129 memcpy (templ_func->template_arguments,
6130 VEC_address (symbolp, template_args),
6131 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6132 VEC_free (symbolp, template_args);
6133 }
6134
208d8187
JB
6135 /* In C++, we can have functions nested inside functions (e.g., when
6136 a function declares a class that has methods). This means that
6137 when we finish processing a function scope, we may need to go
6138 back to building a containing block's symbol lists. */
6139 local_symbols = new->locals;
6140 param_symbols = new->params;
27aa8d6a 6141 using_directives = new->using_directives;
208d8187 6142
921e78cf
JB
6143 /* If we've finished processing a top-level function, subsequent
6144 symbols go in the file symbol list. */
6145 if (outermost_context_p ())
e142c38c 6146 cu->list_in_scope = &file_symbols;
c906108c
SS
6147}
6148
6149/* Process all the DIES contained within a lexical block scope. Start
6150 a new scope, process the dies, and then close the scope. */
6151
6152static void
e7c27a73 6153read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6154{
e7c27a73 6155 struct objfile *objfile = cu->objfile;
52f0bd74 6156 struct context_stack *new;
c906108c
SS
6157 CORE_ADDR lowpc, highpc;
6158 struct die_info *child_die;
e142c38c
DJ
6159 CORE_ADDR baseaddr;
6160
6161 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6162
6163 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6164 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6165 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6166 be nasty. Might be easier to properly extend generic blocks to
af34e669 6167 describe ranges. */
d85a05f0 6168 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6169 return;
6170 lowpc += baseaddr;
6171 highpc += baseaddr;
6172
6173 push_context (0, lowpc);
639d11d3 6174 if (die->child != NULL)
c906108c 6175 {
639d11d3 6176 child_die = die->child;
c906108c
SS
6177 while (child_die && child_die->tag)
6178 {
e7c27a73 6179 process_die (child_die, cu);
c906108c
SS
6180 child_die = sibling_die (child_die);
6181 }
6182 }
6183 new = pop_context ();
6184
8540c487 6185 if (local_symbols != NULL || using_directives != NULL)
c906108c 6186 {
801e3a5b
JB
6187 struct block *block
6188 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6189 highpc, objfile);
6190
6191 /* Note that recording ranges after traversing children, as we
6192 do here, means that recording a parent's ranges entails
6193 walking across all its children's ranges as they appear in
6194 the address map, which is quadratic behavior.
6195
6196 It would be nicer to record the parent's ranges before
6197 traversing its children, simply overriding whatever you find
6198 there. But since we don't even decide whether to create a
6199 block until after we've traversed its children, that's hard
6200 to do. */
6201 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6202 }
6203 local_symbols = new->locals;
27aa8d6a 6204 using_directives = new->using_directives;
c906108c
SS
6205}
6206
96408a79
SA
6207/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6208
6209static void
6210read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6211{
6212 struct objfile *objfile = cu->objfile;
6213 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6214 CORE_ADDR pc, baseaddr;
6215 struct attribute *attr;
6216 struct call_site *call_site, call_site_local;
6217 void **slot;
6218 int nparams;
6219 struct die_info *child_die;
6220
6221 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6222
6223 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6224 if (!attr)
6225 {
6226 complaint (&symfile_complaints,
6227 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6228 "DIE 0x%x [in module %s]"),
b64f50a1 6229 die->offset.sect_off, objfile->name);
96408a79
SA
6230 return;
6231 }
6232 pc = DW_ADDR (attr) + baseaddr;
6233
6234 if (cu->call_site_htab == NULL)
6235 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6236 NULL, &objfile->objfile_obstack,
6237 hashtab_obstack_allocate, NULL);
6238 call_site_local.pc = pc;
6239 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6240 if (*slot != NULL)
6241 {
6242 complaint (&symfile_complaints,
6243 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6244 "DIE 0x%x [in module %s]"),
b64f50a1 6245 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
6246 return;
6247 }
6248
6249 /* Count parameters at the caller. */
6250
6251 nparams = 0;
6252 for (child_die = die->child; child_die && child_die->tag;
6253 child_die = sibling_die (child_die))
6254 {
6255 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6256 {
6257 complaint (&symfile_complaints,
6258 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6259 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6260 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
6261 continue;
6262 }
6263
6264 nparams++;
6265 }
6266
6267 call_site = obstack_alloc (&objfile->objfile_obstack,
6268 (sizeof (*call_site)
6269 + (sizeof (*call_site->parameter)
6270 * (nparams - 1))));
6271 *slot = call_site;
6272 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6273 call_site->pc = pc;
6274
6275 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6276 {
6277 struct die_info *func_die;
6278
6279 /* Skip also over DW_TAG_inlined_subroutine. */
6280 for (func_die = die->parent;
6281 func_die && func_die->tag != DW_TAG_subprogram
6282 && func_die->tag != DW_TAG_subroutine_type;
6283 func_die = func_die->parent);
6284
6285 /* DW_AT_GNU_all_call_sites is a superset
6286 of DW_AT_GNU_all_tail_call_sites. */
6287 if (func_die
6288 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6289 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6290 {
6291 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6292 not complete. But keep CALL_SITE for look ups via call_site_htab,
6293 both the initial caller containing the real return address PC and
6294 the final callee containing the current PC of a chain of tail
6295 calls do not need to have the tail call list complete. But any
6296 function candidate for a virtual tail call frame searched via
6297 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6298 determined unambiguously. */
6299 }
6300 else
6301 {
6302 struct type *func_type = NULL;
6303
6304 if (func_die)
6305 func_type = get_die_type (func_die, cu);
6306 if (func_type != NULL)
6307 {
6308 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6309
6310 /* Enlist this call site to the function. */
6311 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6312 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6313 }
6314 else
6315 complaint (&symfile_complaints,
6316 _("Cannot find function owning DW_TAG_GNU_call_site "
6317 "DIE 0x%x [in module %s]"),
b64f50a1 6318 die->offset.sect_off, objfile->name);
96408a79
SA
6319 }
6320 }
6321
6322 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6323 if (attr == NULL)
6324 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6325 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6326 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6327 /* Keep NULL DWARF_BLOCK. */;
6328 else if (attr_form_is_block (attr))
6329 {
6330 struct dwarf2_locexpr_baton *dlbaton;
6331
6332 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6333 dlbaton->data = DW_BLOCK (attr)->data;
6334 dlbaton->size = DW_BLOCK (attr)->size;
6335 dlbaton->per_cu = cu->per_cu;
6336
6337 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6338 }
6339 else if (is_ref_attr (attr))
6340 {
96408a79
SA
6341 struct dwarf2_cu *target_cu = cu;
6342 struct die_info *target_die;
6343
6344 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6345 gdb_assert (target_cu->objfile == objfile);
6346 if (die_is_declaration (target_die, target_cu))
6347 {
6348 const char *target_physname;
6349
6350 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6351 if (target_physname == NULL)
6352 complaint (&symfile_complaints,
6353 _("DW_AT_GNU_call_site_target target DIE has invalid "
6354 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6355 die->offset.sect_off, objfile->name);
96408a79
SA
6356 else
6357 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6358 }
6359 else
6360 {
6361 CORE_ADDR lowpc;
6362
6363 /* DW_AT_entry_pc should be preferred. */
6364 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6365 complaint (&symfile_complaints,
6366 _("DW_AT_GNU_call_site_target target DIE has invalid "
6367 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6368 die->offset.sect_off, objfile->name);
96408a79
SA
6369 else
6370 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6371 }
6372 }
6373 else
6374 complaint (&symfile_complaints,
6375 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6376 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 6377 die->offset.sect_off, objfile->name);
96408a79
SA
6378
6379 call_site->per_cu = cu->per_cu;
6380
6381 for (child_die = die->child;
6382 child_die && child_die->tag;
6383 child_die = sibling_die (child_die))
6384 {
6385 struct dwarf2_locexpr_baton *dlbaton;
6386 struct call_site_parameter *parameter;
6387
6388 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6389 {
6390 /* Already printed the complaint above. */
6391 continue;
6392 }
6393
6394 gdb_assert (call_site->parameter_count < nparams);
6395 parameter = &call_site->parameter[call_site->parameter_count];
6396
6397 /* DW_AT_location specifies the register number. Value of the data
6398 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6399
6400 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6401 if (!attr || !attr_form_is_block (attr))
6402 {
6403 complaint (&symfile_complaints,
6404 _("No DW_FORM_block* DW_AT_location for "
6405 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6406 child_die->offset.sect_off, objfile->name);
96408a79
SA
6407 continue;
6408 }
6409 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6410 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6411 if (parameter->dwarf_reg == -1
6412 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6413 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6414 &parameter->fb_offset))
6415 {
6416 complaint (&symfile_complaints,
6417 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6418 "for DW_FORM_block* DW_AT_location for "
6419 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6420 child_die->offset.sect_off, objfile->name);
96408a79
SA
6421 continue;
6422 }
6423
6424 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6425 if (!attr_form_is_block (attr))
6426 {
6427 complaint (&symfile_complaints,
6428 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6429 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6430 child_die->offset.sect_off, objfile->name);
96408a79
SA
6431 continue;
6432 }
6433 parameter->value = DW_BLOCK (attr)->data;
6434 parameter->value_size = DW_BLOCK (attr)->size;
6435
6436 /* Parameters are not pre-cleared by memset above. */
6437 parameter->data_value = NULL;
6438 parameter->data_value_size = 0;
6439 call_site->parameter_count++;
6440
6441 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6442 if (attr)
6443 {
6444 if (!attr_form_is_block (attr))
6445 complaint (&symfile_complaints,
6446 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6447 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6448 child_die->offset.sect_off, objfile->name);
96408a79
SA
6449 else
6450 {
6451 parameter->data_value = DW_BLOCK (attr)->data;
6452 parameter->data_value_size = DW_BLOCK (attr)->size;
6453 }
6454 }
6455 }
6456}
6457
43039443 6458/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6459 Return 1 if the attributes are present and valid, otherwise, return 0.
6460 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6461
6462static int
6463dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6464 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6465 struct partial_symtab *ranges_pst)
43039443
JK
6466{
6467 struct objfile *objfile = cu->objfile;
6468 struct comp_unit_head *cu_header = &cu->header;
6469 bfd *obfd = objfile->obfd;
6470 unsigned int addr_size = cu_header->addr_size;
6471 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6472 /* Base address selection entry. */
6473 CORE_ADDR base;
6474 int found_base;
6475 unsigned int dummy;
6476 gdb_byte *buffer;
6477 CORE_ADDR marker;
6478 int low_set;
6479 CORE_ADDR low = 0;
6480 CORE_ADDR high = 0;
ff013f42 6481 CORE_ADDR baseaddr;
43039443 6482
d00adf39
DE
6483 found_base = cu->base_known;
6484 base = cu->base_address;
43039443 6485
be391dca 6486 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6487 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6488 {
6489 complaint (&symfile_complaints,
6490 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6491 offset);
6492 return 0;
6493 }
dce234bc 6494 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6495
6496 /* Read in the largest possible address. */
6497 marker = read_address (obfd, buffer, cu, &dummy);
6498 if ((marker & mask) == mask)
6499 {
6500 /* If we found the largest possible address, then
6501 read the base address. */
6502 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6503 buffer += 2 * addr_size;
6504 offset += 2 * addr_size;
6505 found_base = 1;
6506 }
6507
6508 low_set = 0;
6509
e7030f15 6510 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6511
43039443
JK
6512 while (1)
6513 {
6514 CORE_ADDR range_beginning, range_end;
6515
6516 range_beginning = read_address (obfd, buffer, cu, &dummy);
6517 buffer += addr_size;
6518 range_end = read_address (obfd, buffer, cu, &dummy);
6519 buffer += addr_size;
6520 offset += 2 * addr_size;
6521
6522 /* An end of list marker is a pair of zero addresses. */
6523 if (range_beginning == 0 && range_end == 0)
6524 /* Found the end of list entry. */
6525 break;
6526
6527 /* Each base address selection entry is a pair of 2 values.
6528 The first is the largest possible address, the second is
6529 the base address. Check for a base address here. */
6530 if ((range_beginning & mask) == mask)
6531 {
6532 /* If we found the largest possible address, then
6533 read the base address. */
6534 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6535 found_base = 1;
6536 continue;
6537 }
6538
6539 if (!found_base)
6540 {
6541 /* We have no valid base address for the ranges
6542 data. */
6543 complaint (&symfile_complaints,
6544 _("Invalid .debug_ranges data (no base address)"));
6545 return 0;
6546 }
6547
9277c30c
UW
6548 if (range_beginning > range_end)
6549 {
6550 /* Inverted range entries are invalid. */
6551 complaint (&symfile_complaints,
6552 _("Invalid .debug_ranges data (inverted range)"));
6553 return 0;
6554 }
6555
6556 /* Empty range entries have no effect. */
6557 if (range_beginning == range_end)
6558 continue;
6559
43039443
JK
6560 range_beginning += base;
6561 range_end += base;
6562
9277c30c 6563 if (ranges_pst != NULL)
ff013f42 6564 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6565 range_beginning + baseaddr,
6566 range_end - 1 + baseaddr,
ff013f42
JK
6567 ranges_pst);
6568
43039443
JK
6569 /* FIXME: This is recording everything as a low-high
6570 segment of consecutive addresses. We should have a
6571 data structure for discontiguous block ranges
6572 instead. */
6573 if (! low_set)
6574 {
6575 low = range_beginning;
6576 high = range_end;
6577 low_set = 1;
6578 }
6579 else
6580 {
6581 if (range_beginning < low)
6582 low = range_beginning;
6583 if (range_end > high)
6584 high = range_end;
6585 }
6586 }
6587
6588 if (! low_set)
6589 /* If the first entry is an end-of-list marker, the range
6590 describes an empty scope, i.e. no instructions. */
6591 return 0;
6592
6593 if (low_return)
6594 *low_return = low;
6595 if (high_return)
6596 *high_return = high;
6597 return 1;
6598}
6599
af34e669
DJ
6600/* Get low and high pc attributes from a die. Return 1 if the attributes
6601 are present and valid, otherwise, return 0. Return -1 if the range is
6602 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6603static int
af34e669 6604dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6605 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6606 struct partial_symtab *pst)
c906108c
SS
6607{
6608 struct attribute *attr;
af34e669
DJ
6609 CORE_ADDR low = 0;
6610 CORE_ADDR high = 0;
6611 int ret = 0;
c906108c 6612
e142c38c 6613 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6614 if (attr)
af34e669
DJ
6615 {
6616 high = DW_ADDR (attr);
e142c38c 6617 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6618 if (attr)
6619 low = DW_ADDR (attr);
6620 else
6621 /* Found high w/o low attribute. */
6622 return 0;
6623
6624 /* Found consecutive range of addresses. */
6625 ret = 1;
6626 }
c906108c 6627 else
af34e669 6628 {
e142c38c 6629 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6630 if (attr != NULL)
6631 {
af34e669 6632 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6633 .debug_ranges section. */
d85a05f0 6634 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6635 return 0;
43039443 6636 /* Found discontinuous range of addresses. */
af34e669
DJ
6637 ret = -1;
6638 }
6639 }
c906108c 6640
9373cf26
JK
6641 /* read_partial_die has also the strict LOW < HIGH requirement. */
6642 if (high <= low)
c906108c
SS
6643 return 0;
6644
6645 /* When using the GNU linker, .gnu.linkonce. sections are used to
6646 eliminate duplicate copies of functions and vtables and such.
6647 The linker will arbitrarily choose one and discard the others.
6648 The AT_*_pc values for such functions refer to local labels in
6649 these sections. If the section from that file was discarded, the
6650 labels are not in the output, so the relocs get a value of 0.
6651 If this is a discarded function, mark the pc bounds as invalid,
6652 so that GDB will ignore it. */
72dca2f5 6653 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6654 return 0;
6655
6656 *lowpc = low;
96408a79
SA
6657 if (highpc)
6658 *highpc = high;
af34e669 6659 return ret;
c906108c
SS
6660}
6661
b084d499
JB
6662/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6663 its low and high PC addresses. Do nothing if these addresses could not
6664 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6665 and HIGHPC to the high address if greater than HIGHPC. */
6666
6667static void
6668dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6669 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6670 struct dwarf2_cu *cu)
6671{
6672 CORE_ADDR low, high;
6673 struct die_info *child = die->child;
6674
d85a05f0 6675 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6676 {
6677 *lowpc = min (*lowpc, low);
6678 *highpc = max (*highpc, high);
6679 }
6680
6681 /* If the language does not allow nested subprograms (either inside
6682 subprograms or lexical blocks), we're done. */
6683 if (cu->language != language_ada)
6684 return;
6e70227d 6685
b084d499
JB
6686 /* Check all the children of the given DIE. If it contains nested
6687 subprograms, then check their pc bounds. Likewise, we need to
6688 check lexical blocks as well, as they may also contain subprogram
6689 definitions. */
6690 while (child && child->tag)
6691 {
6692 if (child->tag == DW_TAG_subprogram
6693 || child->tag == DW_TAG_lexical_block)
6694 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6695 child = sibling_die (child);
6696 }
6697}
6698
fae299cd
DC
6699/* Get the low and high pc's represented by the scope DIE, and store
6700 them in *LOWPC and *HIGHPC. If the correct values can't be
6701 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6702
6703static void
6704get_scope_pc_bounds (struct die_info *die,
6705 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6706 struct dwarf2_cu *cu)
6707{
6708 CORE_ADDR best_low = (CORE_ADDR) -1;
6709 CORE_ADDR best_high = (CORE_ADDR) 0;
6710 CORE_ADDR current_low, current_high;
6711
d85a05f0 6712 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6713 {
6714 best_low = current_low;
6715 best_high = current_high;
6716 }
6717 else
6718 {
6719 struct die_info *child = die->child;
6720
6721 while (child && child->tag)
6722 {
6723 switch (child->tag) {
6724 case DW_TAG_subprogram:
b084d499 6725 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6726 break;
6727 case DW_TAG_namespace:
f55ee35c 6728 case DW_TAG_module:
fae299cd
DC
6729 /* FIXME: carlton/2004-01-16: Should we do this for
6730 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6731 that current GCC's always emit the DIEs corresponding
6732 to definitions of methods of classes as children of a
6733 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6734 the DIEs giving the declarations, which could be
6735 anywhere). But I don't see any reason why the
6736 standards says that they have to be there. */
6737 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6738
6739 if (current_low != ((CORE_ADDR) -1))
6740 {
6741 best_low = min (best_low, current_low);
6742 best_high = max (best_high, current_high);
6743 }
6744 break;
6745 default:
0963b4bd 6746 /* Ignore. */
fae299cd
DC
6747 break;
6748 }
6749
6750 child = sibling_die (child);
6751 }
6752 }
6753
6754 *lowpc = best_low;
6755 *highpc = best_high;
6756}
6757
801e3a5b
JB
6758/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6759 in DIE. */
6760static void
6761dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6762 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6763{
bb5ed363 6764 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6765 struct attribute *attr;
6766
6767 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6768 if (attr)
6769 {
6770 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6771
801e3a5b
JB
6772 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6773 if (attr)
6774 {
6775 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6776
801e3a5b
JB
6777 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6778 }
6779 }
6780
6781 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6782 if (attr)
6783 {
bb5ed363 6784 bfd *obfd = objfile->obfd;
801e3a5b
JB
6785
6786 /* The value of the DW_AT_ranges attribute is the offset of the
6787 address range list in the .debug_ranges section. */
6788 unsigned long offset = DW_UNSND (attr);
dce234bc 6789 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6790
6791 /* For some target architectures, but not others, the
6792 read_address function sign-extends the addresses it returns.
6793 To recognize base address selection entries, we need a
6794 mask. */
6795 unsigned int addr_size = cu->header.addr_size;
6796 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6797
6798 /* The base address, to which the next pair is relative. Note
6799 that this 'base' is a DWARF concept: most entries in a range
6800 list are relative, to reduce the number of relocs against the
6801 debugging information. This is separate from this function's
6802 'baseaddr' argument, which GDB uses to relocate debugging
6803 information from a shared library based on the address at
6804 which the library was loaded. */
d00adf39
DE
6805 CORE_ADDR base = cu->base_address;
6806 int base_known = cu->base_known;
801e3a5b 6807
be391dca 6808 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6809 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6810 {
6811 complaint (&symfile_complaints,
6812 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6813 offset);
6814 return;
6815 }
6816
6817 for (;;)
6818 {
6819 unsigned int bytes_read;
6820 CORE_ADDR start, end;
6821
6822 start = read_address (obfd, buffer, cu, &bytes_read);
6823 buffer += bytes_read;
6824 end = read_address (obfd, buffer, cu, &bytes_read);
6825 buffer += bytes_read;
6826
6827 /* Did we find the end of the range list? */
6828 if (start == 0 && end == 0)
6829 break;
6830
6831 /* Did we find a base address selection entry? */
6832 else if ((start & base_select_mask) == base_select_mask)
6833 {
6834 base = end;
6835 base_known = 1;
6836 }
6837
6838 /* We found an ordinary address range. */
6839 else
6840 {
6841 if (!base_known)
6842 {
6843 complaint (&symfile_complaints,
3e43a32a
MS
6844 _("Invalid .debug_ranges data "
6845 "(no base address)"));
801e3a5b
JB
6846 return;
6847 }
6848
9277c30c
UW
6849 if (start > end)
6850 {
6851 /* Inverted range entries are invalid. */
6852 complaint (&symfile_complaints,
6853 _("Invalid .debug_ranges data "
6854 "(inverted range)"));
6855 return;
6856 }
6857
6858 /* Empty range entries have no effect. */
6859 if (start == end)
6860 continue;
6861
6e70227d
DE
6862 record_block_range (block,
6863 baseaddr + base + start,
801e3a5b
JB
6864 baseaddr + base + end - 1);
6865 }
6866 }
6867 }
6868}
6869
60d5a603
JK
6870/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6871 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6872 during 4.6.0 experimental. */
6873
6874static int
6875producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6876{
6877 const char *cs;
6878 int major, minor, release;
ba919b58 6879 int result = 0;
60d5a603
JK
6880
6881 if (cu->producer == NULL)
6882 {
6883 /* For unknown compilers expect their behavior is DWARF version
6884 compliant.
6885
6886 GCC started to support .debug_types sections by -gdwarf-4 since
6887 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6888 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6889 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6890 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6891
6892 return 0;
6893 }
6894
ba919b58
TT
6895 if (cu->checked_producer)
6896 return cu->producer_is_gxx_lt_4_6;
6897
60d5a603
JK
6898 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6899
6900 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6901 {
6902 /* For non-GCC compilers expect their behavior is DWARF version
6903 compliant. */
60d5a603 6904 }
ba919b58 6905 else
60d5a603 6906 {
ba919b58
TT
6907 cs = &cu->producer[strlen ("GNU ")];
6908 while (*cs && !isdigit (*cs))
6909 cs++;
6910 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6911 {
6912 /* Not recognized as GCC. */
6913 }
6914 else
6915 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
6916 }
6917
ba919b58
TT
6918 cu->checked_producer = 1;
6919 cu->producer_is_gxx_lt_4_6 = result;
6920
6921 return result;
60d5a603
JK
6922}
6923
6924/* Return the default accessibility type if it is not overriden by
6925 DW_AT_accessibility. */
6926
6927static enum dwarf_access_attribute
6928dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6929{
6930 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6931 {
6932 /* The default DWARF 2 accessibility for members is public, the default
6933 accessibility for inheritance is private. */
6934
6935 if (die->tag != DW_TAG_inheritance)
6936 return DW_ACCESS_public;
6937 else
6938 return DW_ACCESS_private;
6939 }
6940 else
6941 {
6942 /* DWARF 3+ defines the default accessibility a different way. The same
6943 rules apply now for DW_TAG_inheritance as for the members and it only
6944 depends on the container kind. */
6945
6946 if (die->parent->tag == DW_TAG_class_type)
6947 return DW_ACCESS_private;
6948 else
6949 return DW_ACCESS_public;
6950 }
6951}
6952
74ac6d43
TT
6953/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6954 offset. If the attribute was not found return 0, otherwise return
6955 1. If it was found but could not properly be handled, set *OFFSET
6956 to 0. */
6957
6958static int
6959handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6960 LONGEST *offset)
6961{
6962 struct attribute *attr;
6963
6964 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6965 if (attr != NULL)
6966 {
6967 *offset = 0;
6968
6969 /* Note that we do not check for a section offset first here.
6970 This is because DW_AT_data_member_location is new in DWARF 4,
6971 so if we see it, we can assume that a constant form is really
6972 a constant and not a section offset. */
6973 if (attr_form_is_constant (attr))
6974 *offset = dwarf2_get_attr_constant_value (attr, 0);
6975 else if (attr_form_is_section_offset (attr))
6976 dwarf2_complex_location_expr_complaint ();
6977 else if (attr_form_is_block (attr))
6978 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6979 else
6980 dwarf2_complex_location_expr_complaint ();
6981
6982 return 1;
6983 }
6984
6985 return 0;
6986}
6987
c906108c
SS
6988/* Add an aggregate field to the field list. */
6989
6990static void
107d2387 6991dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6992 struct dwarf2_cu *cu)
6e70227d 6993{
e7c27a73 6994 struct objfile *objfile = cu->objfile;
5e2b427d 6995 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6996 struct nextfield *new_field;
6997 struct attribute *attr;
6998 struct field *fp;
6999 char *fieldname = "";
7000
7001 /* Allocate a new field list entry and link it in. */
7002 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 7003 make_cleanup (xfree, new_field);
c906108c 7004 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7005
7006 if (die->tag == DW_TAG_inheritance)
7007 {
7008 new_field->next = fip->baseclasses;
7009 fip->baseclasses = new_field;
7010 }
7011 else
7012 {
7013 new_field->next = fip->fields;
7014 fip->fields = new_field;
7015 }
c906108c
SS
7016 fip->nfields++;
7017
e142c38c 7018 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7019 if (attr)
7020 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7021 else
7022 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7023 if (new_field->accessibility != DW_ACCESS_public)
7024 fip->non_public_fields = 1;
60d5a603 7025
e142c38c 7026 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7027 if (attr)
7028 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7029 else
7030 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7031
7032 fp = &new_field->field;
a9a9bd0f 7033
e142c38c 7034 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7035 {
74ac6d43
TT
7036 LONGEST offset;
7037
a9a9bd0f 7038 /* Data member other than a C++ static data member. */
6e70227d 7039
c906108c 7040 /* Get type of field. */
e7c27a73 7041 fp->type = die_type (die, cu);
c906108c 7042
d6a843b5 7043 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7044
c906108c 7045 /* Get bit size of field (zero if none). */
e142c38c 7046 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7047 if (attr)
7048 {
7049 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7050 }
7051 else
7052 {
7053 FIELD_BITSIZE (*fp) = 0;
7054 }
7055
7056 /* Get bit offset of field. */
74ac6d43
TT
7057 if (handle_data_member_location (die, cu, &offset))
7058 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7059 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7060 if (attr)
7061 {
5e2b427d 7062 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7063 {
7064 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7065 additional bit offset from the MSB of the containing
7066 anonymous object to the MSB of the field. We don't
7067 have to do anything special since we don't need to
7068 know the size of the anonymous object. */
c906108c
SS
7069 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7070 }
7071 else
7072 {
7073 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7074 MSB of the anonymous object, subtract off the number of
7075 bits from the MSB of the field to the MSB of the
7076 object, and then subtract off the number of bits of
7077 the field itself. The result is the bit offset of
7078 the LSB of the field. */
c906108c
SS
7079 int anonymous_size;
7080 int bit_offset = DW_UNSND (attr);
7081
e142c38c 7082 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7083 if (attr)
7084 {
7085 /* The size of the anonymous object containing
7086 the bit field is explicit, so use the
7087 indicated size (in bytes). */
7088 anonymous_size = DW_UNSND (attr);
7089 }
7090 else
7091 {
7092 /* The size of the anonymous object containing
7093 the bit field must be inferred from the type
7094 attribute of the data member containing the
7095 bit field. */
7096 anonymous_size = TYPE_LENGTH (fp->type);
7097 }
7098 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7099 - bit_offset - FIELD_BITSIZE (*fp);
7100 }
7101 }
7102
7103 /* Get name of field. */
39cbfefa
DJ
7104 fieldname = dwarf2_name (die, cu);
7105 if (fieldname == NULL)
7106 fieldname = "";
d8151005
DJ
7107
7108 /* The name is already allocated along with this objfile, so we don't
7109 need to duplicate it for the type. */
7110 fp->name = fieldname;
c906108c
SS
7111
7112 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7113 pointer or virtual base class pointer) to private. */
e142c38c 7114 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7115 {
d48cc9dd 7116 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7117 new_field->accessibility = DW_ACCESS_private;
7118 fip->non_public_fields = 1;
7119 }
7120 }
a9a9bd0f 7121 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7122 {
a9a9bd0f
DC
7123 /* C++ static member. */
7124
7125 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7126 is a declaration, but all versions of G++ as of this writing
7127 (so through at least 3.2.1) incorrectly generate
7128 DW_TAG_variable tags. */
6e70227d 7129
ff355380 7130 const char *physname;
c906108c 7131
a9a9bd0f 7132 /* Get name of field. */
39cbfefa
DJ
7133 fieldname = dwarf2_name (die, cu);
7134 if (fieldname == NULL)
c906108c
SS
7135 return;
7136
254e6b9e 7137 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7138 if (attr
7139 /* Only create a symbol if this is an external value.
7140 new_symbol checks this and puts the value in the global symbol
7141 table, which we want. If it is not external, new_symbol
7142 will try to put the value in cu->list_in_scope which is wrong. */
7143 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7144 {
7145 /* A static const member, not much different than an enum as far as
7146 we're concerned, except that we can support more types. */
7147 new_symbol (die, NULL, cu);
7148 }
7149
2df3850c 7150 /* Get physical name. */
ff355380 7151 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7152
d8151005
DJ
7153 /* The name is already allocated along with this objfile, so we don't
7154 need to duplicate it for the type. */
7155 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7156 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7157 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7158 }
7159 else if (die->tag == DW_TAG_inheritance)
7160 {
74ac6d43 7161 LONGEST offset;
d4b96c9a 7162
74ac6d43
TT
7163 /* C++ base class field. */
7164 if (handle_data_member_location (die, cu, &offset))
7165 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7166 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7167 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7168 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7169 fip->nbaseclasses++;
7170 }
7171}
7172
98751a41
JK
7173/* Add a typedef defined in the scope of the FIP's class. */
7174
7175static void
7176dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7177 struct dwarf2_cu *cu)
6e70227d 7178{
98751a41 7179 struct objfile *objfile = cu->objfile;
98751a41
JK
7180 struct typedef_field_list *new_field;
7181 struct attribute *attr;
7182 struct typedef_field *fp;
7183 char *fieldname = "";
7184
7185 /* Allocate a new field list entry and link it in. */
7186 new_field = xzalloc (sizeof (*new_field));
7187 make_cleanup (xfree, new_field);
7188
7189 gdb_assert (die->tag == DW_TAG_typedef);
7190
7191 fp = &new_field->field;
7192
7193 /* Get name of field. */
7194 fp->name = dwarf2_name (die, cu);
7195 if (fp->name == NULL)
7196 return;
7197
7198 fp->type = read_type_die (die, cu);
7199
7200 new_field->next = fip->typedef_field_list;
7201 fip->typedef_field_list = new_field;
7202 fip->typedef_field_list_count++;
7203}
7204
c906108c
SS
7205/* Create the vector of fields, and attach it to the type. */
7206
7207static void
fba45db2 7208dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7209 struct dwarf2_cu *cu)
c906108c
SS
7210{
7211 int nfields = fip->nfields;
7212
7213 /* Record the field count, allocate space for the array of fields,
7214 and create blank accessibility bitfields if necessary. */
7215 TYPE_NFIELDS (type) = nfields;
7216 TYPE_FIELDS (type) = (struct field *)
7217 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7218 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7219
b4ba55a1 7220 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7221 {
7222 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7223
7224 TYPE_FIELD_PRIVATE_BITS (type) =
7225 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7226 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7227
7228 TYPE_FIELD_PROTECTED_BITS (type) =
7229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7230 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7231
774b6a14
TT
7232 TYPE_FIELD_IGNORE_BITS (type) =
7233 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7234 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7235 }
7236
7237 /* If the type has baseclasses, allocate and clear a bit vector for
7238 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7239 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7240 {
7241 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7242 unsigned char *pointer;
c906108c
SS
7243
7244 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7245 pointer = TYPE_ALLOC (type, num_bytes);
7246 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7247 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7248 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7249 }
7250
3e43a32a
MS
7251 /* Copy the saved-up fields into the field vector. Start from the head of
7252 the list, adding to the tail of the field array, so that they end up in
7253 the same order in the array in which they were added to the list. */
c906108c
SS
7254 while (nfields-- > 0)
7255 {
7d0ccb61
DJ
7256 struct nextfield *fieldp;
7257
7258 if (fip->fields)
7259 {
7260 fieldp = fip->fields;
7261 fip->fields = fieldp->next;
7262 }
7263 else
7264 {
7265 fieldp = fip->baseclasses;
7266 fip->baseclasses = fieldp->next;
7267 }
7268
7269 TYPE_FIELD (type, nfields) = fieldp->field;
7270 switch (fieldp->accessibility)
c906108c 7271 {
c5aa993b 7272 case DW_ACCESS_private:
b4ba55a1
JB
7273 if (cu->language != language_ada)
7274 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7275 break;
c906108c 7276
c5aa993b 7277 case DW_ACCESS_protected:
b4ba55a1
JB
7278 if (cu->language != language_ada)
7279 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7280 break;
c906108c 7281
c5aa993b
JM
7282 case DW_ACCESS_public:
7283 break;
c906108c 7284
c5aa993b
JM
7285 default:
7286 /* Unknown accessibility. Complain and treat it as public. */
7287 {
e2e0b3e5 7288 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7289 fieldp->accessibility);
c5aa993b
JM
7290 }
7291 break;
c906108c
SS
7292 }
7293 if (nfields < fip->nbaseclasses)
7294 {
7d0ccb61 7295 switch (fieldp->virtuality)
c906108c 7296 {
c5aa993b
JM
7297 case DW_VIRTUALITY_virtual:
7298 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7299 if (cu->language == language_ada)
a73c6dcd 7300 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7301 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7302 break;
c906108c
SS
7303 }
7304 }
c906108c
SS
7305 }
7306}
7307
c906108c
SS
7308/* Add a member function to the proper fieldlist. */
7309
7310static void
107d2387 7311dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7312 struct type *type, struct dwarf2_cu *cu)
c906108c 7313{
e7c27a73 7314 struct objfile *objfile = cu->objfile;
c906108c
SS
7315 struct attribute *attr;
7316 struct fnfieldlist *flp;
7317 int i;
7318 struct fn_field *fnp;
7319 char *fieldname;
c906108c 7320 struct nextfnfield *new_fnfield;
f792889a 7321 struct type *this_type;
60d5a603 7322 enum dwarf_access_attribute accessibility;
c906108c 7323
b4ba55a1 7324 if (cu->language == language_ada)
a73c6dcd 7325 error (_("unexpected member function in Ada type"));
b4ba55a1 7326
2df3850c 7327 /* Get name of member function. */
39cbfefa
DJ
7328 fieldname = dwarf2_name (die, cu);
7329 if (fieldname == NULL)
2df3850c 7330 return;
c906108c 7331
c906108c
SS
7332 /* Look up member function name in fieldlist. */
7333 for (i = 0; i < fip->nfnfields; i++)
7334 {
27bfe10e 7335 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7336 break;
7337 }
7338
7339 /* Create new list element if necessary. */
7340 if (i < fip->nfnfields)
7341 flp = &fip->fnfieldlists[i];
7342 else
7343 {
7344 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7345 {
7346 fip->fnfieldlists = (struct fnfieldlist *)
7347 xrealloc (fip->fnfieldlists,
7348 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7349 * sizeof (struct fnfieldlist));
c906108c 7350 if (fip->nfnfields == 0)
c13c43fd 7351 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7352 }
7353 flp = &fip->fnfieldlists[fip->nfnfields];
7354 flp->name = fieldname;
7355 flp->length = 0;
7356 flp->head = NULL;
3da10d80 7357 i = fip->nfnfields++;
c906108c
SS
7358 }
7359
7360 /* Create a new member function field and chain it to the field list
0963b4bd 7361 entry. */
c906108c 7362 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7363 make_cleanup (xfree, new_fnfield);
c906108c
SS
7364 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7365 new_fnfield->next = flp->head;
7366 flp->head = new_fnfield;
7367 flp->length++;
7368
7369 /* Fill in the member function field info. */
7370 fnp = &new_fnfield->fnfield;
3da10d80
KS
7371
7372 /* Delay processing of the physname until later. */
7373 if (cu->language == language_cplus || cu->language == language_java)
7374 {
7375 add_to_method_list (type, i, flp->length - 1, fieldname,
7376 die, cu);
7377 }
7378 else
7379 {
1d06ead6 7380 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7381 fnp->physname = physname ? physname : "";
7382 }
7383
c906108c 7384 fnp->type = alloc_type (objfile);
f792889a
DJ
7385 this_type = read_type_die (die, cu);
7386 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7387 {
f792889a 7388 int nparams = TYPE_NFIELDS (this_type);
c906108c 7389
f792889a 7390 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7391 of the method itself (TYPE_CODE_METHOD). */
7392 smash_to_method_type (fnp->type, type,
f792889a
DJ
7393 TYPE_TARGET_TYPE (this_type),
7394 TYPE_FIELDS (this_type),
7395 TYPE_NFIELDS (this_type),
7396 TYPE_VARARGS (this_type));
c906108c
SS
7397
7398 /* Handle static member functions.
c5aa993b 7399 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7400 member functions. G++ helps GDB by marking the first
7401 parameter for non-static member functions (which is the this
7402 pointer) as artificial. We obtain this information from
7403 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7404 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7405 fnp->voffset = VOFFSET_STATIC;
7406 }
7407 else
e2e0b3e5 7408 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7409 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7410
7411 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7412 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7413 fnp->fcontext = die_containing_type (die, cu);
c906108c 7414
3e43a32a
MS
7415 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7416 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7417
7418 /* Get accessibility. */
e142c38c 7419 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7420 if (attr)
60d5a603
JK
7421 accessibility = DW_UNSND (attr);
7422 else
7423 accessibility = dwarf2_default_access_attribute (die, cu);
7424 switch (accessibility)
c906108c 7425 {
60d5a603
JK
7426 case DW_ACCESS_private:
7427 fnp->is_private = 1;
7428 break;
7429 case DW_ACCESS_protected:
7430 fnp->is_protected = 1;
7431 break;
c906108c
SS
7432 }
7433
b02dede2 7434 /* Check for artificial methods. */
e142c38c 7435 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7436 if (attr && DW_UNSND (attr) != 0)
7437 fnp->is_artificial = 1;
7438
0d564a31 7439 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7440 function. For older versions of GCC, this is an offset in the
7441 appropriate virtual table, as specified by DW_AT_containing_type.
7442 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7443 to the object address. */
7444
e142c38c 7445 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7446 if (attr)
8e19ed76 7447 {
aec5aa8b 7448 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7449 {
aec5aa8b
TT
7450 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7451 {
7452 /* Old-style GCC. */
7453 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7454 }
7455 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7456 || (DW_BLOCK (attr)->size > 1
7457 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7458 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7459 {
7460 struct dwarf_block blk;
7461 int offset;
7462
7463 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7464 ? 1 : 2);
7465 blk.size = DW_BLOCK (attr)->size - offset;
7466 blk.data = DW_BLOCK (attr)->data + offset;
7467 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7468 if ((fnp->voffset % cu->header.addr_size) != 0)
7469 dwarf2_complex_location_expr_complaint ();
7470 else
7471 fnp->voffset /= cu->header.addr_size;
7472 fnp->voffset += 2;
7473 }
7474 else
7475 dwarf2_complex_location_expr_complaint ();
7476
7477 if (!fnp->fcontext)
7478 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7479 }
3690dd37 7480 else if (attr_form_is_section_offset (attr))
8e19ed76 7481 {
4d3c2250 7482 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7483 }
7484 else
7485 {
4d3c2250
KB
7486 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7487 fieldname);
8e19ed76 7488 }
0d564a31 7489 }
d48cc9dd
DJ
7490 else
7491 {
7492 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7493 if (attr && DW_UNSND (attr))
7494 {
7495 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7496 complaint (&symfile_complaints,
3e43a32a
MS
7497 _("Member function \"%s\" (offset %d) is virtual "
7498 "but the vtable offset is not specified"),
b64f50a1 7499 fieldname, die->offset.sect_off);
9655fd1a 7500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7501 TYPE_CPLUS_DYNAMIC (type) = 1;
7502 }
7503 }
c906108c
SS
7504}
7505
7506/* Create the vector of member function fields, and attach it to the type. */
7507
7508static void
fba45db2 7509dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7510 struct dwarf2_cu *cu)
c906108c
SS
7511{
7512 struct fnfieldlist *flp;
c906108c
SS
7513 int i;
7514
b4ba55a1 7515 if (cu->language == language_ada)
a73c6dcd 7516 error (_("unexpected member functions in Ada type"));
b4ba55a1 7517
c906108c
SS
7518 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7519 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7520 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7521
7522 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7523 {
7524 struct nextfnfield *nfp = flp->head;
7525 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7526 int k;
7527
7528 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7529 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7530 fn_flp->fn_fields = (struct fn_field *)
7531 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7532 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7533 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7534 }
7535
7536 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7537}
7538
1168df01
JB
7539/* Returns non-zero if NAME is the name of a vtable member in CU's
7540 language, zero otherwise. */
7541static int
7542is_vtable_name (const char *name, struct dwarf2_cu *cu)
7543{
7544 static const char vptr[] = "_vptr";
987504bb 7545 static const char vtable[] = "vtable";
1168df01 7546
987504bb
JJ
7547 /* Look for the C++ and Java forms of the vtable. */
7548 if ((cu->language == language_java
7549 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7550 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7551 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7552 return 1;
7553
7554 return 0;
7555}
7556
c0dd20ea 7557/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7558 functions, with the ABI-specified layout. If TYPE describes
7559 such a structure, smash it into a member function type.
61049d3b
DJ
7560
7561 GCC shouldn't do this; it should just output pointer to member DIEs.
7562 This is GCC PR debug/28767. */
c0dd20ea 7563
0b92b5bb
TT
7564static void
7565quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7566{
0b92b5bb 7567 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7568
7569 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7570 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7571 return;
c0dd20ea
DJ
7572
7573 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7574 if (TYPE_FIELD_NAME (type, 0) == NULL
7575 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7576 || TYPE_FIELD_NAME (type, 1) == NULL
7577 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7578 return;
c0dd20ea
DJ
7579
7580 /* Find the type of the method. */
0b92b5bb 7581 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7582 if (pfn_type == NULL
7583 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7584 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7585 return;
c0dd20ea
DJ
7586
7587 /* Look for the "this" argument. */
7588 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7589 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7590 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7591 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7592 return;
c0dd20ea
DJ
7593
7594 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7595 new_type = alloc_type (objfile);
7596 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7597 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7598 TYPE_VARARGS (pfn_type));
0b92b5bb 7599 smash_to_methodptr_type (type, new_type);
c0dd20ea 7600}
1168df01 7601
c906108c 7602/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7603 (definition) to create a type for the structure or union. Fill in
7604 the type's name and general properties; the members will not be
7605 processed until process_structure_type.
c906108c 7606
c767944b
DJ
7607 NOTE: we need to call these functions regardless of whether or not the
7608 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7609 structure or union. This gets the type entered into our set of
7610 user defined types.
7611
7612 However, if the structure is incomplete (an opaque struct/union)
7613 then suppress creating a symbol table entry for it since gdb only
7614 wants to find the one with the complete definition. Note that if
7615 it is complete, we just call new_symbol, which does it's own
7616 checking about whether the struct/union is anonymous or not (and
7617 suppresses creating a symbol table entry itself). */
7618
f792889a 7619static struct type *
134d01f1 7620read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7621{
e7c27a73 7622 struct objfile *objfile = cu->objfile;
c906108c
SS
7623 struct type *type;
7624 struct attribute *attr;
39cbfefa 7625 char *name;
c906108c 7626
348e048f
DE
7627 /* If the definition of this type lives in .debug_types, read that type.
7628 Don't follow DW_AT_specification though, that will take us back up
7629 the chain and we want to go down. */
7630 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7631 if (attr)
7632 {
7633 struct dwarf2_cu *type_cu = cu;
7634 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7635
348e048f
DE
7636 /* We could just recurse on read_structure_type, but we need to call
7637 get_die_type to ensure only one type for this DIE is created.
7638 This is important, for example, because for c++ classes we need
7639 TYPE_NAME set which is only done by new_symbol. Blech. */
7640 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7641
7642 /* TYPE_CU may not be the same as CU.
7643 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7644 return set_die_type (die, type, cu);
7645 }
7646
c0dd20ea 7647 type = alloc_type (objfile);
c906108c 7648 INIT_CPLUS_SPECIFIC (type);
93311388 7649
39cbfefa
DJ
7650 name = dwarf2_name (die, cu);
7651 if (name != NULL)
c906108c 7652 {
987504bb
JJ
7653 if (cu->language == language_cplus
7654 || cu->language == language_java)
63d06c5c 7655 {
3da10d80
KS
7656 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7657
7658 /* dwarf2_full_name might have already finished building the DIE's
7659 type. If so, there is no need to continue. */
7660 if (get_die_type (die, cu) != NULL)
7661 return get_die_type (die, cu);
7662
7663 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7664 if (die->tag == DW_TAG_structure_type
7665 || die->tag == DW_TAG_class_type)
7666 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7667 }
7668 else
7669 {
d8151005
DJ
7670 /* The name is already allocated along with this objfile, so
7671 we don't need to duplicate it for the type. */
94af9270
KS
7672 TYPE_TAG_NAME (type) = (char *) name;
7673 if (die->tag == DW_TAG_class_type)
7674 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7675 }
c906108c
SS
7676 }
7677
7678 if (die->tag == DW_TAG_structure_type)
7679 {
7680 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7681 }
7682 else if (die->tag == DW_TAG_union_type)
7683 {
7684 TYPE_CODE (type) = TYPE_CODE_UNION;
7685 }
7686 else
7687 {
c906108c
SS
7688 TYPE_CODE (type) = TYPE_CODE_CLASS;
7689 }
7690
0cc2414c
TT
7691 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7692 TYPE_DECLARED_CLASS (type) = 1;
7693
e142c38c 7694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7695 if (attr)
7696 {
7697 TYPE_LENGTH (type) = DW_UNSND (attr);
7698 }
7699 else
7700 {
7701 TYPE_LENGTH (type) = 0;
7702 }
7703
876cecd0 7704 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7705 if (die_is_declaration (die, cu))
876cecd0 7706 TYPE_STUB (type) = 1;
a6c727b2
DJ
7707 else if (attr == NULL && die->child == NULL
7708 && producer_is_realview (cu->producer))
7709 /* RealView does not output the required DW_AT_declaration
7710 on incomplete types. */
7711 TYPE_STUB (type) = 1;
dc718098 7712
c906108c
SS
7713 /* We need to add the type field to the die immediately so we don't
7714 infinitely recurse when dealing with pointers to the structure
0963b4bd 7715 type within the structure itself. */
1c379e20 7716 set_die_type (die, type, cu);
c906108c 7717
7e314c57
JK
7718 /* set_die_type should be already done. */
7719 set_descriptive_type (type, die, cu);
7720
c767944b
DJ
7721 return type;
7722}
7723
7724/* Finish creating a structure or union type, including filling in
7725 its members and creating a symbol for it. */
7726
7727static void
7728process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7729{
7730 struct objfile *objfile = cu->objfile;
7731 struct die_info *child_die = die->child;
7732 struct type *type;
7733
7734 type = get_die_type (die, cu);
7735 if (type == NULL)
7736 type = read_structure_type (die, cu);
7737
e142c38c 7738 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7739 {
7740 struct field_info fi;
7741 struct die_info *child_die;
34eaf542 7742 VEC (symbolp) *template_args = NULL;
c767944b 7743 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7744
7745 memset (&fi, 0, sizeof (struct field_info));
7746
639d11d3 7747 child_die = die->child;
c906108c
SS
7748
7749 while (child_die && child_die->tag)
7750 {
a9a9bd0f
DC
7751 if (child_die->tag == DW_TAG_member
7752 || child_die->tag == DW_TAG_variable)
c906108c 7753 {
a9a9bd0f
DC
7754 /* NOTE: carlton/2002-11-05: A C++ static data member
7755 should be a DW_TAG_member that is a declaration, but
7756 all versions of G++ as of this writing (so through at
7757 least 3.2.1) incorrectly generate DW_TAG_variable
7758 tags for them instead. */
e7c27a73 7759 dwarf2_add_field (&fi, child_die, cu);
c906108c 7760 }
8713b1b1 7761 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7762 {
0963b4bd 7763 /* C++ member function. */
e7c27a73 7764 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7765 }
7766 else if (child_die->tag == DW_TAG_inheritance)
7767 {
7768 /* C++ base class field. */
e7c27a73 7769 dwarf2_add_field (&fi, child_die, cu);
c906108c 7770 }
98751a41
JK
7771 else if (child_die->tag == DW_TAG_typedef)
7772 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7773 else if (child_die->tag == DW_TAG_template_type_param
7774 || child_die->tag == DW_TAG_template_value_param)
7775 {
7776 struct symbol *arg = new_symbol (child_die, NULL, cu);
7777
f1078f66
DJ
7778 if (arg != NULL)
7779 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7780 }
7781
c906108c
SS
7782 child_die = sibling_die (child_die);
7783 }
7784
34eaf542
TT
7785 /* Attach template arguments to type. */
7786 if (! VEC_empty (symbolp, template_args))
7787 {
7788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7789 TYPE_N_TEMPLATE_ARGUMENTS (type)
7790 = VEC_length (symbolp, template_args);
7791 TYPE_TEMPLATE_ARGUMENTS (type)
7792 = obstack_alloc (&objfile->objfile_obstack,
7793 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7794 * sizeof (struct symbol *)));
7795 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7796 VEC_address (symbolp, template_args),
7797 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7798 * sizeof (struct symbol *)));
7799 VEC_free (symbolp, template_args);
7800 }
7801
c906108c
SS
7802 /* Attach fields and member functions to the type. */
7803 if (fi.nfields)
e7c27a73 7804 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7805 if (fi.nfnfields)
7806 {
e7c27a73 7807 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7808
c5aa993b 7809 /* Get the type which refers to the base class (possibly this
c906108c 7810 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7811 class from the DW_AT_containing_type attribute. This use of
7812 DW_AT_containing_type is a GNU extension. */
c906108c 7813
e142c38c 7814 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7815 {
e7c27a73 7816 struct type *t = die_containing_type (die, cu);
c906108c
SS
7817
7818 TYPE_VPTR_BASETYPE (type) = t;
7819 if (type == t)
7820 {
c906108c
SS
7821 int i;
7822
7823 /* Our own class provides vtbl ptr. */
7824 for (i = TYPE_NFIELDS (t) - 1;
7825 i >= TYPE_N_BASECLASSES (t);
7826 --i)
7827 {
0d5cff50 7828 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 7829
1168df01 7830 if (is_vtable_name (fieldname, cu))
c906108c
SS
7831 {
7832 TYPE_VPTR_FIELDNO (type) = i;
7833 break;
7834 }
7835 }
7836
7837 /* Complain if virtual function table field not found. */
7838 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7839 complaint (&symfile_complaints,
3e43a32a
MS
7840 _("virtual function table pointer "
7841 "not found when defining class '%s'"),
4d3c2250
KB
7842 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7843 "");
c906108c
SS
7844 }
7845 else
7846 {
7847 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7848 }
7849 }
f6235d4c
EZ
7850 else if (cu->producer
7851 && strncmp (cu->producer,
7852 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7853 {
7854 /* The IBM XLC compiler does not provide direct indication
7855 of the containing type, but the vtable pointer is
7856 always named __vfp. */
7857
7858 int i;
7859
7860 for (i = TYPE_NFIELDS (type) - 1;
7861 i >= TYPE_N_BASECLASSES (type);
7862 --i)
7863 {
7864 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7865 {
7866 TYPE_VPTR_FIELDNO (type) = i;
7867 TYPE_VPTR_BASETYPE (type) = type;
7868 break;
7869 }
7870 }
7871 }
c906108c 7872 }
98751a41
JK
7873
7874 /* Copy fi.typedef_field_list linked list elements content into the
7875 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7876 if (fi.typedef_field_list)
7877 {
7878 int i = fi.typedef_field_list_count;
7879
a0d7a4ff 7880 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7881 TYPE_TYPEDEF_FIELD_ARRAY (type)
7882 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7883 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7884
7885 /* Reverse the list order to keep the debug info elements order. */
7886 while (--i >= 0)
7887 {
7888 struct typedef_field *dest, *src;
6e70227d 7889
98751a41
JK
7890 dest = &TYPE_TYPEDEF_FIELD (type, i);
7891 src = &fi.typedef_field_list->field;
7892 fi.typedef_field_list = fi.typedef_field_list->next;
7893 *dest = *src;
7894 }
7895 }
c767944b
DJ
7896
7897 do_cleanups (back_to);
eb2a6f42
TT
7898
7899 if (HAVE_CPLUS_STRUCT (type))
7900 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7901 }
63d06c5c 7902
bb5ed363 7903 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7904
90aeadfc
DC
7905 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7906 snapshots) has been known to create a die giving a declaration
7907 for a class that has, as a child, a die giving a definition for a
7908 nested class. So we have to process our children even if the
7909 current die is a declaration. Normally, of course, a declaration
7910 won't have any children at all. */
134d01f1 7911
90aeadfc
DC
7912 while (child_die != NULL && child_die->tag)
7913 {
7914 if (child_die->tag == DW_TAG_member
7915 || child_die->tag == DW_TAG_variable
34eaf542
TT
7916 || child_die->tag == DW_TAG_inheritance
7917 || child_die->tag == DW_TAG_template_value_param
7918 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7919 {
90aeadfc 7920 /* Do nothing. */
134d01f1 7921 }
90aeadfc
DC
7922 else
7923 process_die (child_die, cu);
134d01f1 7924
90aeadfc 7925 child_die = sibling_die (child_die);
134d01f1
DJ
7926 }
7927
fa4028e9
JB
7928 /* Do not consider external references. According to the DWARF standard,
7929 these DIEs are identified by the fact that they have no byte_size
7930 attribute, and a declaration attribute. */
7931 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7932 || !die_is_declaration (die, cu))
c767944b 7933 new_symbol (die, type, cu);
134d01f1
DJ
7934}
7935
7936/* Given a DW_AT_enumeration_type die, set its type. We do not
7937 complete the type's fields yet, or create any symbols. */
c906108c 7938
f792889a 7939static struct type *
134d01f1 7940read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7941{
e7c27a73 7942 struct objfile *objfile = cu->objfile;
c906108c 7943 struct type *type;
c906108c 7944 struct attribute *attr;
0114d602 7945 const char *name;
134d01f1 7946
348e048f
DE
7947 /* If the definition of this type lives in .debug_types, read that type.
7948 Don't follow DW_AT_specification though, that will take us back up
7949 the chain and we want to go down. */
7950 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7951 if (attr)
7952 {
7953 struct dwarf2_cu *type_cu = cu;
7954 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7955
348e048f 7956 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7957
7958 /* TYPE_CU may not be the same as CU.
7959 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7960 return set_die_type (die, type, cu);
7961 }
7962
c906108c
SS
7963 type = alloc_type (objfile);
7964
7965 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7966 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7967 if (name != NULL)
0114d602 7968 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7969
e142c38c 7970 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7971 if (attr)
7972 {
7973 TYPE_LENGTH (type) = DW_UNSND (attr);
7974 }
7975 else
7976 {
7977 TYPE_LENGTH (type) = 0;
7978 }
7979
137033e9
JB
7980 /* The enumeration DIE can be incomplete. In Ada, any type can be
7981 declared as private in the package spec, and then defined only
7982 inside the package body. Such types are known as Taft Amendment
7983 Types. When another package uses such a type, an incomplete DIE
7984 may be generated by the compiler. */
02eb380e 7985 if (die_is_declaration (die, cu))
876cecd0 7986 TYPE_STUB (type) = 1;
02eb380e 7987
f792889a 7988 return set_die_type (die, type, cu);
134d01f1
DJ
7989}
7990
7991/* Given a pointer to a die which begins an enumeration, process all
7992 the dies that define the members of the enumeration, and create the
7993 symbol for the enumeration type.
7994
7995 NOTE: We reverse the order of the element list. */
7996
7997static void
7998process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7999{
f792889a 8000 struct type *this_type;
134d01f1 8001
f792889a
DJ
8002 this_type = get_die_type (die, cu);
8003 if (this_type == NULL)
8004 this_type = read_enumeration_type (die, cu);
9dc481d3 8005
639d11d3 8006 if (die->child != NULL)
c906108c 8007 {
9dc481d3
DE
8008 struct die_info *child_die;
8009 struct symbol *sym;
8010 struct field *fields = NULL;
8011 int num_fields = 0;
8012 int unsigned_enum = 1;
8013 char *name;
cafec441
TT
8014 int flag_enum = 1;
8015 ULONGEST mask = 0;
9dc481d3 8016
639d11d3 8017 child_die = die->child;
c906108c
SS
8018 while (child_die && child_die->tag)
8019 {
8020 if (child_die->tag != DW_TAG_enumerator)
8021 {
e7c27a73 8022 process_die (child_die, cu);
c906108c
SS
8023 }
8024 else
8025 {
39cbfefa
DJ
8026 name = dwarf2_name (child_die, cu);
8027 if (name)
c906108c 8028 {
f792889a 8029 sym = new_symbol (child_die, this_type, cu);
c906108c 8030 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
8031 {
8032 unsigned_enum = 0;
8033 flag_enum = 0;
8034 }
8035 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8036 flag_enum = 0;
8037 else
8038 mask |= SYMBOL_VALUE (sym);
c906108c
SS
8039
8040 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8041 {
8042 fields = (struct field *)
8043 xrealloc (fields,
8044 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8045 * sizeof (struct field));
c906108c
SS
8046 }
8047
3567439c 8048 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8049 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8050 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8051 FIELD_BITSIZE (fields[num_fields]) = 0;
8052
8053 num_fields++;
8054 }
8055 }
8056
8057 child_die = sibling_die (child_die);
8058 }
8059
8060 if (num_fields)
8061 {
f792889a
DJ
8062 TYPE_NFIELDS (this_type) = num_fields;
8063 TYPE_FIELDS (this_type) = (struct field *)
8064 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8065 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8066 sizeof (struct field) * num_fields);
b8c9b27d 8067 xfree (fields);
c906108c
SS
8068 }
8069 if (unsigned_enum)
876cecd0 8070 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
8071 if (flag_enum)
8072 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 8073 }
134d01f1 8074
6c83ed52
TT
8075 /* If we are reading an enum from a .debug_types unit, and the enum
8076 is a declaration, and the enum is not the signatured type in the
8077 unit, then we do not want to add a symbol for it. Adding a
8078 symbol would in some cases obscure the true definition of the
8079 enum, giving users an incomplete type when the definition is
8080 actually available. Note that we do not want to do this for all
8081 enums which are just declarations, because C++0x allows forward
8082 enum declarations. */
b0df02fd 8083 if (cu->per_cu->debug_types_section
6c83ed52
TT
8084 && die_is_declaration (die, cu))
8085 {
8086 struct signatured_type *type_sig;
8087
8088 type_sig
8089 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8090 cu->per_cu->debug_types_section,
6c83ed52 8091 cu->per_cu->offset);
b64f50a1
JK
8092 if (type_sig->per_cu.offset.sect_off + type_sig->type_offset.cu_off
8093 != die->offset.sect_off)
6c83ed52
TT
8094 return;
8095 }
8096
f792889a 8097 new_symbol (die, this_type, cu);
c906108c
SS
8098}
8099
8100/* Extract all information from a DW_TAG_array_type DIE and put it in
8101 the DIE's type field. For now, this only handles one dimensional
8102 arrays. */
8103
f792889a 8104static struct type *
e7c27a73 8105read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8106{
e7c27a73 8107 struct objfile *objfile = cu->objfile;
c906108c 8108 struct die_info *child_die;
7e314c57 8109 struct type *type;
c906108c
SS
8110 struct type *element_type, *range_type, *index_type;
8111 struct type **range_types = NULL;
8112 struct attribute *attr;
8113 int ndim = 0;
8114 struct cleanup *back_to;
39cbfefa 8115 char *name;
c906108c 8116
e7c27a73 8117 element_type = die_type (die, cu);
c906108c 8118
7e314c57
JK
8119 /* The die_type call above may have already set the type for this DIE. */
8120 type = get_die_type (die, cu);
8121 if (type)
8122 return type;
8123
c906108c
SS
8124 /* Irix 6.2 native cc creates array types without children for
8125 arrays with unspecified length. */
639d11d3 8126 if (die->child == NULL)
c906108c 8127 {
46bf5051 8128 index_type = objfile_type (objfile)->builtin_int;
c906108c 8129 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8130 type = create_array_type (NULL, element_type, range_type);
8131 return set_die_type (die, type, cu);
c906108c
SS
8132 }
8133
8134 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8135 child_die = die->child;
c906108c
SS
8136 while (child_die && child_die->tag)
8137 {
8138 if (child_die->tag == DW_TAG_subrange_type)
8139 {
f792889a 8140 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8141
f792889a 8142 if (child_type != NULL)
a02abb62 8143 {
0963b4bd
MS
8144 /* The range type was succesfully read. Save it for the
8145 array type creation. */
a02abb62
JB
8146 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8147 {
8148 range_types = (struct type **)
8149 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8150 * sizeof (struct type *));
8151 if (ndim == 0)
8152 make_cleanup (free_current_contents, &range_types);
8153 }
f792889a 8154 range_types[ndim++] = child_type;
a02abb62 8155 }
c906108c
SS
8156 }
8157 child_die = sibling_die (child_die);
8158 }
8159
8160 /* Dwarf2 dimensions are output from left to right, create the
8161 necessary array types in backwards order. */
7ca2d3a3 8162
c906108c 8163 type = element_type;
7ca2d3a3
DL
8164
8165 if (read_array_order (die, cu) == DW_ORD_col_major)
8166 {
8167 int i = 0;
9a619af0 8168
7ca2d3a3
DL
8169 while (i < ndim)
8170 type = create_array_type (NULL, type, range_types[i++]);
8171 }
8172 else
8173 {
8174 while (ndim-- > 0)
8175 type = create_array_type (NULL, type, range_types[ndim]);
8176 }
c906108c 8177
f5f8a009
EZ
8178 /* Understand Dwarf2 support for vector types (like they occur on
8179 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8180 array type. This is not part of the Dwarf2/3 standard yet, but a
8181 custom vendor extension. The main difference between a regular
8182 array and the vector variant is that vectors are passed by value
8183 to functions. */
e142c38c 8184 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8185 if (attr)
ea37ba09 8186 make_vector_type (type);
f5f8a009 8187
dbc98a8b
KW
8188 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8189 implementation may choose to implement triple vectors using this
8190 attribute. */
8191 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8192 if (attr)
8193 {
8194 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8195 TYPE_LENGTH (type) = DW_UNSND (attr);
8196 else
3e43a32a
MS
8197 complaint (&symfile_complaints,
8198 _("DW_AT_byte_size for array type smaller "
8199 "than the total size of elements"));
dbc98a8b
KW
8200 }
8201
39cbfefa
DJ
8202 name = dwarf2_name (die, cu);
8203 if (name)
8204 TYPE_NAME (type) = name;
6e70227d 8205
0963b4bd 8206 /* Install the type in the die. */
7e314c57
JK
8207 set_die_type (die, type, cu);
8208
8209 /* set_die_type should be already done. */
b4ba55a1
JB
8210 set_descriptive_type (type, die, cu);
8211
c906108c
SS
8212 do_cleanups (back_to);
8213
7e314c57 8214 return type;
c906108c
SS
8215}
8216
7ca2d3a3 8217static enum dwarf_array_dim_ordering
6e70227d 8218read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8219{
8220 struct attribute *attr;
8221
8222 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8223
8224 if (attr) return DW_SND (attr);
8225
0963b4bd
MS
8226 /* GNU F77 is a special case, as at 08/2004 array type info is the
8227 opposite order to the dwarf2 specification, but data is still
8228 laid out as per normal fortran.
7ca2d3a3 8229
0963b4bd
MS
8230 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8231 version checking. */
7ca2d3a3 8232
905e0470
PM
8233 if (cu->language == language_fortran
8234 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8235 {
8236 return DW_ORD_row_major;
8237 }
8238
6e70227d 8239 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8240 {
8241 case array_column_major:
8242 return DW_ORD_col_major;
8243 case array_row_major:
8244 default:
8245 return DW_ORD_row_major;
8246 };
8247}
8248
72019c9c 8249/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8250 the DIE's type field. */
72019c9c 8251
f792889a 8252static struct type *
72019c9c
GM
8253read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8254{
7e314c57
JK
8255 struct type *domain_type, *set_type;
8256 struct attribute *attr;
f792889a 8257
7e314c57
JK
8258 domain_type = die_type (die, cu);
8259
8260 /* The die_type call above may have already set the type for this DIE. */
8261 set_type = get_die_type (die, cu);
8262 if (set_type)
8263 return set_type;
8264
8265 set_type = create_set_type (NULL, domain_type);
8266
8267 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8268 if (attr)
8269 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8270
f792889a 8271 return set_die_type (die, set_type, cu);
72019c9c 8272}
7ca2d3a3 8273
c906108c
SS
8274/* First cut: install each common block member as a global variable. */
8275
8276static void
e7c27a73 8277read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8278{
8279 struct die_info *child_die;
8280 struct attribute *attr;
8281 struct symbol *sym;
8282 CORE_ADDR base = (CORE_ADDR) 0;
8283
e142c38c 8284 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8285 if (attr)
8286 {
0963b4bd 8287 /* Support the .debug_loc offsets. */
8e19ed76
PS
8288 if (attr_form_is_block (attr))
8289 {
e7c27a73 8290 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8291 }
3690dd37 8292 else if (attr_form_is_section_offset (attr))
8e19ed76 8293 {
4d3c2250 8294 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8295 }
8296 else
8297 {
4d3c2250
KB
8298 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8299 "common block member");
8e19ed76 8300 }
c906108c 8301 }
639d11d3 8302 if (die->child != NULL)
c906108c 8303 {
639d11d3 8304 child_die = die->child;
c906108c
SS
8305 while (child_die && child_die->tag)
8306 {
74ac6d43
TT
8307 LONGEST offset;
8308
e7c27a73 8309 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8310 if (sym != NULL
8311 && handle_data_member_location (child_die, cu, &offset))
c906108c 8312 {
74ac6d43 8313 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8314 add_symbol_to_list (sym, &global_symbols);
8315 }
8316 child_die = sibling_die (child_die);
8317 }
8318 }
8319}
8320
0114d602 8321/* Create a type for a C++ namespace. */
d9fa45fe 8322
0114d602
DJ
8323static struct type *
8324read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8325{
e7c27a73 8326 struct objfile *objfile = cu->objfile;
0114d602 8327 const char *previous_prefix, *name;
9219021c 8328 int is_anonymous;
0114d602
DJ
8329 struct type *type;
8330
8331 /* For extensions, reuse the type of the original namespace. */
8332 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8333 {
8334 struct die_info *ext_die;
8335 struct dwarf2_cu *ext_cu = cu;
9a619af0 8336
0114d602
DJ
8337 ext_die = dwarf2_extension (die, &ext_cu);
8338 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8339
8340 /* EXT_CU may not be the same as CU.
8341 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8342 return set_die_type (die, type, cu);
8343 }
9219021c 8344
e142c38c 8345 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8346
8347 /* Now build the name of the current namespace. */
8348
0114d602
DJ
8349 previous_prefix = determine_prefix (die, cu);
8350 if (previous_prefix[0] != '\0')
8351 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8352 previous_prefix, name, 0, cu);
0114d602
DJ
8353
8354 /* Create the type. */
8355 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8356 objfile);
8357 TYPE_NAME (type) = (char *) name;
8358 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8359
60531b24 8360 return set_die_type (die, type, cu);
0114d602
DJ
8361}
8362
8363/* Read a C++ namespace. */
8364
8365static void
8366read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8367{
8368 struct objfile *objfile = cu->objfile;
0114d602 8369 int is_anonymous;
9219021c 8370
5c4e30ca
DC
8371 /* Add a symbol associated to this if we haven't seen the namespace
8372 before. Also, add a using directive if it's an anonymous
8373 namespace. */
9219021c 8374
f2f0e013 8375 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8376 {
8377 struct type *type;
8378
0114d602 8379 type = read_type_die (die, cu);
e7c27a73 8380 new_symbol (die, type, cu);
5c4e30ca 8381
e8e80198 8382 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8383 if (is_anonymous)
0114d602
DJ
8384 {
8385 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8386
c0cc3a76 8387 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8388 NULL, NULL, &objfile->objfile_obstack);
0114d602 8389 }
5c4e30ca 8390 }
9219021c 8391
639d11d3 8392 if (die->child != NULL)
d9fa45fe 8393 {
639d11d3 8394 struct die_info *child_die = die->child;
6e70227d 8395
d9fa45fe
DC
8396 while (child_die && child_die->tag)
8397 {
e7c27a73 8398 process_die (child_die, cu);
d9fa45fe
DC
8399 child_die = sibling_die (child_die);
8400 }
8401 }
38d518c9
EZ
8402}
8403
f55ee35c
JK
8404/* Read a Fortran module as type. This DIE can be only a declaration used for
8405 imported module. Still we need that type as local Fortran "use ... only"
8406 declaration imports depend on the created type in determine_prefix. */
8407
8408static struct type *
8409read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8410{
8411 struct objfile *objfile = cu->objfile;
8412 char *module_name;
8413 struct type *type;
8414
8415 module_name = dwarf2_name (die, cu);
8416 if (!module_name)
3e43a32a
MS
8417 complaint (&symfile_complaints,
8418 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 8419 die->offset.sect_off);
f55ee35c
JK
8420 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8421
8422 /* determine_prefix uses TYPE_TAG_NAME. */
8423 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8424
8425 return set_die_type (die, type, cu);
8426}
8427
5d7cb8df
JK
8428/* Read a Fortran module. */
8429
8430static void
8431read_module (struct die_info *die, struct dwarf2_cu *cu)
8432{
8433 struct die_info *child_die = die->child;
8434
5d7cb8df
JK
8435 while (child_die && child_die->tag)
8436 {
8437 process_die (child_die, cu);
8438 child_die = sibling_die (child_die);
8439 }
8440}
8441
38d518c9
EZ
8442/* Return the name of the namespace represented by DIE. Set
8443 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8444 namespace. */
8445
8446static const char *
e142c38c 8447namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8448{
8449 struct die_info *current_die;
8450 const char *name = NULL;
8451
8452 /* Loop through the extensions until we find a name. */
8453
8454 for (current_die = die;
8455 current_die != NULL;
f2f0e013 8456 current_die = dwarf2_extension (die, &cu))
38d518c9 8457 {
e142c38c 8458 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8459 if (name != NULL)
8460 break;
8461 }
8462
8463 /* Is it an anonymous namespace? */
8464
8465 *is_anonymous = (name == NULL);
8466 if (*is_anonymous)
2b1dbab0 8467 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8468
8469 return name;
d9fa45fe
DC
8470}
8471
c906108c
SS
8472/* Extract all information from a DW_TAG_pointer_type DIE and add to
8473 the user defined type vector. */
8474
f792889a 8475static struct type *
e7c27a73 8476read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8477{
5e2b427d 8478 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8479 struct comp_unit_head *cu_header = &cu->header;
c906108c 8480 struct type *type;
8b2dbe47
KB
8481 struct attribute *attr_byte_size;
8482 struct attribute *attr_address_class;
8483 int byte_size, addr_class;
7e314c57
JK
8484 struct type *target_type;
8485
8486 target_type = die_type (die, cu);
c906108c 8487
7e314c57
JK
8488 /* The die_type call above may have already set the type for this DIE. */
8489 type = get_die_type (die, cu);
8490 if (type)
8491 return type;
8492
8493 type = lookup_pointer_type (target_type);
8b2dbe47 8494
e142c38c 8495 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8496 if (attr_byte_size)
8497 byte_size = DW_UNSND (attr_byte_size);
c906108c 8498 else
8b2dbe47
KB
8499 byte_size = cu_header->addr_size;
8500
e142c38c 8501 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8502 if (attr_address_class)
8503 addr_class = DW_UNSND (attr_address_class);
8504 else
8505 addr_class = DW_ADDR_none;
8506
8507 /* If the pointer size or address class is different than the
8508 default, create a type variant marked as such and set the
8509 length accordingly. */
8510 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8511 {
5e2b427d 8512 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8513 {
8514 int type_flags;
8515
849957d9 8516 type_flags = gdbarch_address_class_type_flags
5e2b427d 8517 (gdbarch, byte_size, addr_class);
876cecd0
TT
8518 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8519 == 0);
8b2dbe47
KB
8520 type = make_type_with_address_space (type, type_flags);
8521 }
8522 else if (TYPE_LENGTH (type) != byte_size)
8523 {
3e43a32a
MS
8524 complaint (&symfile_complaints,
8525 _("invalid pointer size %d"), byte_size);
8b2dbe47 8526 }
6e70227d 8527 else
9a619af0
MS
8528 {
8529 /* Should we also complain about unhandled address classes? */
8530 }
c906108c 8531 }
8b2dbe47
KB
8532
8533 TYPE_LENGTH (type) = byte_size;
f792889a 8534 return set_die_type (die, type, cu);
c906108c
SS
8535}
8536
8537/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8538 the user defined type vector. */
8539
f792889a 8540static struct type *
e7c27a73 8541read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8542{
8543 struct type *type;
8544 struct type *to_type;
8545 struct type *domain;
8546
e7c27a73
DJ
8547 to_type = die_type (die, cu);
8548 domain = die_containing_type (die, cu);
0d5de010 8549
7e314c57
JK
8550 /* The calls above may have already set the type for this DIE. */
8551 type = get_die_type (die, cu);
8552 if (type)
8553 return type;
8554
0d5de010
DJ
8555 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8556 type = lookup_methodptr_type (to_type);
8557 else
8558 type = lookup_memberptr_type (to_type, domain);
c906108c 8559
f792889a 8560 return set_die_type (die, type, cu);
c906108c
SS
8561}
8562
8563/* Extract all information from a DW_TAG_reference_type DIE and add to
8564 the user defined type vector. */
8565
f792889a 8566static struct type *
e7c27a73 8567read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8568{
e7c27a73 8569 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8570 struct type *type, *target_type;
c906108c
SS
8571 struct attribute *attr;
8572
7e314c57
JK
8573 target_type = die_type (die, cu);
8574
8575 /* The die_type call above may have already set the type for this DIE. */
8576 type = get_die_type (die, cu);
8577 if (type)
8578 return type;
8579
8580 type = lookup_reference_type (target_type);
e142c38c 8581 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8582 if (attr)
8583 {
8584 TYPE_LENGTH (type) = DW_UNSND (attr);
8585 }
8586 else
8587 {
107d2387 8588 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8589 }
f792889a 8590 return set_die_type (die, type, cu);
c906108c
SS
8591}
8592
f792889a 8593static struct type *
e7c27a73 8594read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8595{
f792889a 8596 struct type *base_type, *cv_type;
c906108c 8597
e7c27a73 8598 base_type = die_type (die, cu);
7e314c57
JK
8599
8600 /* The die_type call above may have already set the type for this DIE. */
8601 cv_type = get_die_type (die, cu);
8602 if (cv_type)
8603 return cv_type;
8604
2f608a3a
KW
8605 /* In case the const qualifier is applied to an array type, the element type
8606 is so qualified, not the array type (section 6.7.3 of C99). */
8607 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8608 {
8609 struct type *el_type, *inner_array;
8610
8611 base_type = copy_type (base_type);
8612 inner_array = base_type;
8613
8614 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8615 {
8616 TYPE_TARGET_TYPE (inner_array) =
8617 copy_type (TYPE_TARGET_TYPE (inner_array));
8618 inner_array = TYPE_TARGET_TYPE (inner_array);
8619 }
8620
8621 el_type = TYPE_TARGET_TYPE (inner_array);
8622 TYPE_TARGET_TYPE (inner_array) =
8623 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8624
8625 return set_die_type (die, base_type, cu);
8626 }
8627
f792889a
DJ
8628 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8629 return set_die_type (die, cv_type, cu);
c906108c
SS
8630}
8631
f792889a 8632static struct type *
e7c27a73 8633read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8634{
f792889a 8635 struct type *base_type, *cv_type;
c906108c 8636
e7c27a73 8637 base_type = die_type (die, cu);
7e314c57
JK
8638
8639 /* The die_type call above may have already set the type for this DIE. */
8640 cv_type = get_die_type (die, cu);
8641 if (cv_type)
8642 return cv_type;
8643
f792889a
DJ
8644 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8645 return set_die_type (die, cv_type, cu);
c906108c
SS
8646}
8647
8648/* Extract all information from a DW_TAG_string_type DIE and add to
8649 the user defined type vector. It isn't really a user defined type,
8650 but it behaves like one, with other DIE's using an AT_user_def_type
8651 attribute to reference it. */
8652
f792889a 8653static struct type *
e7c27a73 8654read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8655{
e7c27a73 8656 struct objfile *objfile = cu->objfile;
3b7538c0 8657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8658 struct type *type, *range_type, *index_type, *char_type;
8659 struct attribute *attr;
8660 unsigned int length;
8661
e142c38c 8662 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8663 if (attr)
8664 {
8665 length = DW_UNSND (attr);
8666 }
8667 else
8668 {
0963b4bd 8669 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8670 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8671 if (attr)
8672 {
8673 length = DW_UNSND (attr);
8674 }
8675 else
8676 {
8677 length = 1;
8678 }
c906108c 8679 }
6ccb9162 8680
46bf5051 8681 index_type = objfile_type (objfile)->builtin_int;
c906108c 8682 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8683 char_type = language_string_char_type (cu->language_defn, gdbarch);
8684 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8685
f792889a 8686 return set_die_type (die, type, cu);
c906108c
SS
8687}
8688
8689/* Handle DIES due to C code like:
8690
8691 struct foo
c5aa993b
JM
8692 {
8693 int (*funcp)(int a, long l);
8694 int b;
8695 };
c906108c 8696
0963b4bd 8697 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8698
f792889a 8699static struct type *
e7c27a73 8700read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8701{
bb5ed363 8702 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8703 struct type *type; /* Type that this function returns. */
8704 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8705 struct attribute *attr;
8706
e7c27a73 8707 type = die_type (die, cu);
7e314c57
JK
8708
8709 /* The die_type call above may have already set the type for this DIE. */
8710 ftype = get_die_type (die, cu);
8711 if (ftype)
8712 return ftype;
8713
0c8b41f1 8714 ftype = lookup_function_type (type);
c906108c 8715
5b8101ae 8716 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8717 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8718 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8719 || cu->language == language_cplus
5b8101ae
PM
8720 || cu->language == language_java
8721 || cu->language == language_pascal)
876cecd0 8722 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8723 else if (producer_is_realview (cu->producer))
8724 /* RealView does not emit DW_AT_prototyped. We can not
8725 distinguish prototyped and unprototyped functions; default to
8726 prototyped, since that is more common in modern code (and
8727 RealView warns about unprototyped functions). */
8728 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8729
c055b101
CV
8730 /* Store the calling convention in the type if it's available in
8731 the subroutine die. Otherwise set the calling convention to
8732 the default value DW_CC_normal. */
8733 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8734 if (attr)
8735 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8736 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8737 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8738 else
8739 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8740
8741 /* We need to add the subroutine type to the die immediately so
8742 we don't infinitely recurse when dealing with parameters
0963b4bd 8743 declared as the same subroutine type. */
76c10ea2 8744 set_die_type (die, ftype, cu);
6e70227d 8745
639d11d3 8746 if (die->child != NULL)
c906108c 8747 {
bb5ed363 8748 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8749 struct die_info *child_die;
8072405b 8750 int nparams, iparams;
c906108c
SS
8751
8752 /* Count the number of parameters.
8753 FIXME: GDB currently ignores vararg functions, but knows about
8754 vararg member functions. */
8072405b 8755 nparams = 0;
639d11d3 8756 child_die = die->child;
c906108c
SS
8757 while (child_die && child_die->tag)
8758 {
8759 if (child_die->tag == DW_TAG_formal_parameter)
8760 nparams++;
8761 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8762 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8763 child_die = sibling_die (child_die);
8764 }
8765
8766 /* Allocate storage for parameters and fill them in. */
8767 TYPE_NFIELDS (ftype) = nparams;
8768 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8769 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8770
8072405b
JK
8771 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8772 even if we error out during the parameters reading below. */
8773 for (iparams = 0; iparams < nparams; iparams++)
8774 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8775
8776 iparams = 0;
639d11d3 8777 child_die = die->child;
c906108c
SS
8778 while (child_die && child_die->tag)
8779 {
8780 if (child_die->tag == DW_TAG_formal_parameter)
8781 {
3ce3b1ba
PA
8782 struct type *arg_type;
8783
8784 /* DWARF version 2 has no clean way to discern C++
8785 static and non-static member functions. G++ helps
8786 GDB by marking the first parameter for non-static
8787 member functions (which is the this pointer) as
8788 artificial. We pass this information to
8789 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8790
8791 DWARF version 3 added DW_AT_object_pointer, which GCC
8792 4.5 does not yet generate. */
e142c38c 8793 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8794 if (attr)
8795 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8796 else
418835cc
KS
8797 {
8798 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8799
8800 /* GCC/43521: In java, the formal parameter
8801 "this" is sometimes not marked with DW_AT_artificial. */
8802 if (cu->language == language_java)
8803 {
8804 const char *name = dwarf2_name (child_die, cu);
9a619af0 8805
418835cc
KS
8806 if (name && !strcmp (name, "this"))
8807 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8808 }
8809 }
3ce3b1ba
PA
8810 arg_type = die_type (child_die, cu);
8811
8812 /* RealView does not mark THIS as const, which the testsuite
8813 expects. GCC marks THIS as const in method definitions,
8814 but not in the class specifications (GCC PR 43053). */
8815 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8816 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8817 {
8818 int is_this = 0;
8819 struct dwarf2_cu *arg_cu = cu;
8820 const char *name = dwarf2_name (child_die, cu);
8821
8822 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8823 if (attr)
8824 {
8825 /* If the compiler emits this, use it. */
8826 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8827 is_this = 1;
8828 }
8829 else if (name && strcmp (name, "this") == 0)
8830 /* Function definitions will have the argument names. */
8831 is_this = 1;
8832 else if (name == NULL && iparams == 0)
8833 /* Declarations may not have the names, so like
8834 elsewhere in GDB, assume an artificial first
8835 argument is "this". */
8836 is_this = 1;
8837
8838 if (is_this)
8839 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8840 arg_type, 0);
8841 }
8842
8843 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8844 iparams++;
8845 }
8846 child_die = sibling_die (child_die);
8847 }
8848 }
8849
76c10ea2 8850 return ftype;
c906108c
SS
8851}
8852
f792889a 8853static struct type *
e7c27a73 8854read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8855{
e7c27a73 8856 struct objfile *objfile = cu->objfile;
0114d602 8857 const char *name = NULL;
3c8e0968 8858 struct type *this_type, *target_type;
c906108c 8859
94af9270 8860 name = dwarf2_full_name (NULL, die, cu);
f792889a 8861 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8862 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8863 TYPE_NAME (this_type) = (char *) name;
f792889a 8864 set_die_type (die, this_type, cu);
3c8e0968
DE
8865 target_type = die_type (die, cu);
8866 if (target_type != this_type)
8867 TYPE_TARGET_TYPE (this_type) = target_type;
8868 else
8869 {
8870 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8871 spec and cause infinite loops in GDB. */
8872 complaint (&symfile_complaints,
8873 _("Self-referential DW_TAG_typedef "
8874 "- DIE at 0x%x [in module %s]"),
b64f50a1 8875 die->offset.sect_off, objfile->name);
3c8e0968
DE
8876 TYPE_TARGET_TYPE (this_type) = NULL;
8877 }
f792889a 8878 return this_type;
c906108c
SS
8879}
8880
8881/* Find a representation of a given base type and install
8882 it in the TYPE field of the die. */
8883
f792889a 8884static struct type *
e7c27a73 8885read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8886{
e7c27a73 8887 struct objfile *objfile = cu->objfile;
c906108c
SS
8888 struct type *type;
8889 struct attribute *attr;
8890 int encoding = 0, size = 0;
39cbfefa 8891 char *name;
6ccb9162
UW
8892 enum type_code code = TYPE_CODE_INT;
8893 int type_flags = 0;
8894 struct type *target_type = NULL;
c906108c 8895
e142c38c 8896 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8897 if (attr)
8898 {
8899 encoding = DW_UNSND (attr);
8900 }
e142c38c 8901 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8902 if (attr)
8903 {
8904 size = DW_UNSND (attr);
8905 }
39cbfefa 8906 name = dwarf2_name (die, cu);
6ccb9162 8907 if (!name)
c906108c 8908 {
6ccb9162
UW
8909 complaint (&symfile_complaints,
8910 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8911 }
6ccb9162
UW
8912
8913 switch (encoding)
c906108c 8914 {
6ccb9162
UW
8915 case DW_ATE_address:
8916 /* Turn DW_ATE_address into a void * pointer. */
8917 code = TYPE_CODE_PTR;
8918 type_flags |= TYPE_FLAG_UNSIGNED;
8919 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8920 break;
8921 case DW_ATE_boolean:
8922 code = TYPE_CODE_BOOL;
8923 type_flags |= TYPE_FLAG_UNSIGNED;
8924 break;
8925 case DW_ATE_complex_float:
8926 code = TYPE_CODE_COMPLEX;
8927 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8928 break;
8929 case DW_ATE_decimal_float:
8930 code = TYPE_CODE_DECFLOAT;
8931 break;
8932 case DW_ATE_float:
8933 code = TYPE_CODE_FLT;
8934 break;
8935 case DW_ATE_signed:
8936 break;
8937 case DW_ATE_unsigned:
8938 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8939 if (cu->language == language_fortran
8940 && name
8941 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8942 code = TYPE_CODE_CHAR;
6ccb9162
UW
8943 break;
8944 case DW_ATE_signed_char:
6e70227d 8945 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8946 || cu->language == language_pascal
8947 || cu->language == language_fortran)
6ccb9162
UW
8948 code = TYPE_CODE_CHAR;
8949 break;
8950 case DW_ATE_unsigned_char:
868a0084 8951 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8952 || cu->language == language_pascal
8953 || cu->language == language_fortran)
6ccb9162
UW
8954 code = TYPE_CODE_CHAR;
8955 type_flags |= TYPE_FLAG_UNSIGNED;
8956 break;
75079b2b
TT
8957 case DW_ATE_UTF:
8958 /* We just treat this as an integer and then recognize the
8959 type by name elsewhere. */
8960 break;
8961
6ccb9162
UW
8962 default:
8963 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8964 dwarf_type_encoding_name (encoding));
8965 break;
c906108c 8966 }
6ccb9162 8967
0114d602
DJ
8968 type = init_type (code, size, type_flags, NULL, objfile);
8969 TYPE_NAME (type) = name;
6ccb9162
UW
8970 TYPE_TARGET_TYPE (type) = target_type;
8971
0114d602 8972 if (name && strcmp (name, "char") == 0)
876cecd0 8973 TYPE_NOSIGN (type) = 1;
0114d602 8974
f792889a 8975 return set_die_type (die, type, cu);
c906108c
SS
8976}
8977
a02abb62
JB
8978/* Read the given DW_AT_subrange DIE. */
8979
f792889a 8980static struct type *
a02abb62
JB
8981read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8982{
8983 struct type *base_type;
8984 struct type *range_type;
8985 struct attribute *attr;
43bbcdc2
PH
8986 LONGEST low = 0;
8987 LONGEST high = -1;
39cbfefa 8988 char *name;
43bbcdc2 8989 LONGEST negative_mask;
e77813c8 8990
a02abb62 8991 base_type = die_type (die, cu);
953ac07e
JK
8992 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8993 check_typedef (base_type);
a02abb62 8994
7e314c57
JK
8995 /* The die_type call above may have already set the type for this DIE. */
8996 range_type = get_die_type (die, cu);
8997 if (range_type)
8998 return range_type;
8999
e142c38c 9000 if (cu->language == language_fortran)
6e70227d 9001 {
a02abb62
JB
9002 /* FORTRAN implies a lower bound of 1, if not given. */
9003 low = 1;
9004 }
9005
dd5e6932
DJ
9006 /* FIXME: For variable sized arrays either of these could be
9007 a variable rather than a constant value. We'll allow it,
9008 but we don't know how to handle it. */
e142c38c 9009 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
9010 if (attr)
9011 low = dwarf2_get_attr_constant_value (attr, 0);
9012
e142c38c 9013 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9014 if (attr)
6e70227d 9015 {
d48323d8 9016 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9017 {
9018 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9019 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9020 FIXME: GDB does not yet know how to handle dynamic
9021 arrays properly, treat them as arrays with unspecified
9022 length for now.
9023
9024 FIXME: jimb/2003-09-22: GDB does not really know
9025 how to handle arrays of unspecified length
9026 either; we just represent them as zero-length
9027 arrays. Choose an appropriate upper bound given
9028 the lower bound we've computed above. */
9029 high = low - 1;
9030 }
9031 else
9032 high = dwarf2_get_attr_constant_value (attr, 1);
9033 }
e77813c8
PM
9034 else
9035 {
9036 attr = dwarf2_attr (die, DW_AT_count, cu);
9037 if (attr)
9038 {
9039 int count = dwarf2_get_attr_constant_value (attr, 1);
9040 high = low + count - 1;
9041 }
c2ff108b
JK
9042 else
9043 {
9044 /* Unspecified array length. */
9045 high = low - 1;
9046 }
e77813c8
PM
9047 }
9048
9049 /* Dwarf-2 specifications explicitly allows to create subrange types
9050 without specifying a base type.
9051 In that case, the base type must be set to the type of
9052 the lower bound, upper bound or count, in that order, if any of these
9053 three attributes references an object that has a type.
9054 If no base type is found, the Dwarf-2 specifications say that
9055 a signed integer type of size equal to the size of an address should
9056 be used.
9057 For the following C code: `extern char gdb_int [];'
9058 GCC produces an empty range DIE.
9059 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9060 high bound or count are not yet handled by this code. */
e77813c8
PM
9061 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9062 {
9063 struct objfile *objfile = cu->objfile;
9064 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9065 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9066 struct type *int_type = objfile_type (objfile)->builtin_int;
9067
9068 /* Test "int", "long int", and "long long int" objfile types,
9069 and select the first one having a size above or equal to the
9070 architecture address size. */
9071 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9072 base_type = int_type;
9073 else
9074 {
9075 int_type = objfile_type (objfile)->builtin_long;
9076 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9077 base_type = int_type;
9078 else
9079 {
9080 int_type = objfile_type (objfile)->builtin_long_long;
9081 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9082 base_type = int_type;
9083 }
9084 }
9085 }
a02abb62 9086
6e70227d 9087 negative_mask =
43bbcdc2
PH
9088 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9089 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9090 low |= negative_mask;
9091 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9092 high |= negative_mask;
9093
a02abb62
JB
9094 range_type = create_range_type (NULL, base_type, low, high);
9095
bbb0eef6
JK
9096 /* Mark arrays with dynamic length at least as an array of unspecified
9097 length. GDB could check the boundary but before it gets implemented at
9098 least allow accessing the array elements. */
d48323d8 9099 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9100 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9101
c2ff108b
JK
9102 /* Ada expects an empty array on no boundary attributes. */
9103 if (attr == NULL && cu->language != language_ada)
9104 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9105
39cbfefa
DJ
9106 name = dwarf2_name (die, cu);
9107 if (name)
9108 TYPE_NAME (range_type) = name;
6e70227d 9109
e142c38c 9110 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9111 if (attr)
9112 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9113
7e314c57
JK
9114 set_die_type (die, range_type, cu);
9115
9116 /* set_die_type should be already done. */
b4ba55a1
JB
9117 set_descriptive_type (range_type, die, cu);
9118
7e314c57 9119 return range_type;
a02abb62 9120}
6e70227d 9121
f792889a 9122static struct type *
81a17f79
JB
9123read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9124{
9125 struct type *type;
81a17f79 9126
81a17f79
JB
9127 /* For now, we only support the C meaning of an unspecified type: void. */
9128
0114d602
DJ
9129 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9130 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9131
f792889a 9132 return set_die_type (die, type, cu);
81a17f79 9133}
a02abb62 9134
51545339
DJ
9135/* Trivial hash function for die_info: the hash value of a DIE
9136 is its offset in .debug_info for this objfile. */
9137
9138static hashval_t
9139die_hash (const void *item)
9140{
9141 const struct die_info *die = item;
9a619af0 9142
b64f50a1 9143 return die->offset.sect_off;
51545339
DJ
9144}
9145
9146/* Trivial comparison function for die_info structures: two DIEs
9147 are equal if they have the same offset. */
9148
9149static int
9150die_eq (const void *item_lhs, const void *item_rhs)
9151{
9152 const struct die_info *die_lhs = item_lhs;
9153 const struct die_info *die_rhs = item_rhs;
9a619af0 9154
b64f50a1 9155 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
51545339
DJ
9156}
9157
c906108c
SS
9158/* Read a whole compilation unit into a linked list of dies. */
9159
f9aca02d 9160static struct die_info *
93311388 9161read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9162{
93311388 9163 struct die_reader_specs reader_specs;
98bfdba5 9164 int read_abbrevs = 0;
1d9ec526 9165 struct cleanup *back_to = NULL;
98bfdba5
PA
9166 struct die_info *die;
9167
9168 if (cu->dwarf2_abbrevs == NULL)
9169 {
e5fe5e75 9170 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9171 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9172 read_abbrevs = 1;
9173 }
93311388 9174
348e048f 9175 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9176 cu->die_hash
9177 = htab_create_alloc_ex (cu->header.length / 12,
9178 die_hash,
9179 die_eq,
9180 NULL,
9181 &cu->comp_unit_obstack,
9182 hashtab_obstack_allocate,
9183 dummy_obstack_deallocate);
9184
93311388
DE
9185 init_cu_die_reader (&reader_specs, cu);
9186
98bfdba5
PA
9187 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9188
9189 if (read_abbrevs)
9190 do_cleanups (back_to);
9191
9192 return die;
639d11d3
DC
9193}
9194
d97bc12b
DE
9195/* Main entry point for reading a DIE and all children.
9196 Read the DIE and dump it if requested. */
9197
9198static struct die_info *
93311388
DE
9199read_die_and_children (const struct die_reader_specs *reader,
9200 gdb_byte *info_ptr,
d97bc12b
DE
9201 gdb_byte **new_info_ptr,
9202 struct die_info *parent)
9203{
93311388 9204 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9205 new_info_ptr, parent);
9206
9207 if (dwarf2_die_debug)
9208 {
348e048f
DE
9209 fprintf_unfiltered (gdb_stdlog,
9210 "\nRead die from %s of %s:\n",
b0df02fd 9211 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9212 ? ".debug_types"
9213 : ".debug_info"),
348e048f 9214 reader->abfd->filename);
d97bc12b
DE
9215 dump_die (result, dwarf2_die_debug);
9216 }
9217
9218 return result;
9219}
9220
639d11d3
DC
9221/* Read a single die and all its descendents. Set the die's sibling
9222 field to NULL; set other fields in the die correctly, and set all
9223 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9224 location of the info_ptr after reading all of those dies. PARENT
9225 is the parent of the die in question. */
9226
9227static struct die_info *
93311388
DE
9228read_die_and_children_1 (const struct die_reader_specs *reader,
9229 gdb_byte *info_ptr,
d97bc12b
DE
9230 gdb_byte **new_info_ptr,
9231 struct die_info *parent)
639d11d3
DC
9232{
9233 struct die_info *die;
fe1b8b76 9234 gdb_byte *cur_ptr;
639d11d3
DC
9235 int has_children;
9236
93311388 9237 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9238 if (die == NULL)
9239 {
9240 *new_info_ptr = cur_ptr;
9241 return NULL;
9242 }
93311388 9243 store_in_ref_table (die, reader->cu);
639d11d3
DC
9244
9245 if (has_children)
348e048f 9246 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9247 else
9248 {
9249 die->child = NULL;
9250 *new_info_ptr = cur_ptr;
9251 }
9252
9253 die->sibling = NULL;
9254 die->parent = parent;
9255 return die;
9256}
9257
9258/* Read a die, all of its descendents, and all of its siblings; set
9259 all of the fields of all of the dies correctly. Arguments are as
9260 in read_die_and_children. */
9261
9262static struct die_info *
93311388
DE
9263read_die_and_siblings (const struct die_reader_specs *reader,
9264 gdb_byte *info_ptr,
fe1b8b76 9265 gdb_byte **new_info_ptr,
639d11d3
DC
9266 struct die_info *parent)
9267{
9268 struct die_info *first_die, *last_sibling;
fe1b8b76 9269 gdb_byte *cur_ptr;
639d11d3 9270
c906108c 9271 cur_ptr = info_ptr;
639d11d3
DC
9272 first_die = last_sibling = NULL;
9273
9274 while (1)
c906108c 9275 {
639d11d3 9276 struct die_info *die
93311388 9277 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9278
1d325ec1 9279 if (die == NULL)
c906108c 9280 {
639d11d3
DC
9281 *new_info_ptr = cur_ptr;
9282 return first_die;
c906108c 9283 }
1d325ec1
DJ
9284
9285 if (!first_die)
9286 first_die = die;
c906108c 9287 else
1d325ec1
DJ
9288 last_sibling->sibling = die;
9289
9290 last_sibling = die;
c906108c 9291 }
c906108c
SS
9292}
9293
93311388
DE
9294/* Read the die from the .debug_info section buffer. Set DIEP to
9295 point to a newly allocated die with its information, except for its
9296 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9297 whether the die has children or not. */
9298
9299static gdb_byte *
9300read_full_die (const struct die_reader_specs *reader,
9301 struct die_info **diep, gdb_byte *info_ptr,
9302 int *has_children)
9303{
b64f50a1
JK
9304 unsigned int abbrev_number, bytes_read, i;
9305 sect_offset offset;
93311388
DE
9306 struct abbrev_info *abbrev;
9307 struct die_info *die;
9308 struct dwarf2_cu *cu = reader->cu;
9309 bfd *abfd = reader->abfd;
9310
b64f50a1 9311 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
9312 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9313 info_ptr += bytes_read;
9314 if (!abbrev_number)
9315 {
9316 *diep = NULL;
9317 *has_children = 0;
9318 return info_ptr;
9319 }
9320
9321 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9322 if (!abbrev)
348e048f
DE
9323 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9324 abbrev_number,
9325 bfd_get_filename (abfd));
9326
93311388
DE
9327 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9328 die->offset = offset;
9329 die->tag = abbrev->tag;
9330 die->abbrev = abbrev_number;
9331
9332 die->num_attrs = abbrev->num_attrs;
9333
9334 for (i = 0; i < abbrev->num_attrs; ++i)
9335 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9336 abfd, info_ptr, cu);
9337
9338 *diep = die;
9339 *has_children = abbrev->has_children;
9340 return info_ptr;
9341}
9342
c906108c
SS
9343/* In DWARF version 2, the description of the debugging information is
9344 stored in a separate .debug_abbrev section. Before we read any
9345 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9346 in a hash table. This function also sets flags in CU describing
9347 the data found in the abbrev table. */
c906108c
SS
9348
9349static void
e5fe5e75 9350dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9351{
e5fe5e75 9352 bfd *abfd = cu->objfile->obfd;
e7c27a73 9353 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9354 gdb_byte *abbrev_ptr;
c906108c
SS
9355 struct abbrev_info *cur_abbrev;
9356 unsigned int abbrev_number, bytes_read, abbrev_name;
9357 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9358 struct attr_abbrev *cur_attrs;
9359 unsigned int allocated_attrs;
c906108c 9360
0963b4bd 9361 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9362 obstack_init (&cu->abbrev_obstack);
9363 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9364 (ABBREV_HASH_SIZE
9365 * sizeof (struct abbrev_info *)));
9366 memset (cu->dwarf2_abbrevs, 0,
9367 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9368
be391dca
TT
9369 dwarf2_read_section (dwarf2_per_objfile->objfile,
9370 &dwarf2_per_objfile->abbrev);
b64f50a1
JK
9371 abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9372 + cu_header->abbrev_offset.sect_off);
c906108c
SS
9373 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9374 abbrev_ptr += bytes_read;
9375
f3dd6933
DJ
9376 allocated_attrs = ATTR_ALLOC_CHUNK;
9377 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9378
0963b4bd 9379 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9380 while (abbrev_number)
9381 {
f3dd6933 9382 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9383
9384 /* read in abbrev header */
9385 cur_abbrev->number = abbrev_number;
9386 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9387 abbrev_ptr += bytes_read;
9388 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9389 abbrev_ptr += 1;
9390
9391 /* now read in declarations */
9392 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9393 abbrev_ptr += bytes_read;
9394 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9395 abbrev_ptr += bytes_read;
9396 while (abbrev_name)
9397 {
f3dd6933 9398 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9399 {
f3dd6933
DJ
9400 allocated_attrs += ATTR_ALLOC_CHUNK;
9401 cur_attrs
9402 = xrealloc (cur_attrs, (allocated_attrs
9403 * sizeof (struct attr_abbrev)));
c906108c 9404 }
ae038cb0 9405
f3dd6933
DJ
9406 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9407 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9408 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9409 abbrev_ptr += bytes_read;
9410 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9411 abbrev_ptr += bytes_read;
9412 }
9413
f3dd6933
DJ
9414 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9415 (cur_abbrev->num_attrs
9416 * sizeof (struct attr_abbrev)));
9417 memcpy (cur_abbrev->attrs, cur_attrs,
9418 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9419
c906108c 9420 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9421 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9422 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9423
9424 /* Get next abbreviation.
9425 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9426 always properly terminated with an abbrev number of 0.
9427 Exit loop if we encounter an abbreviation which we have
9428 already read (which means we are about to read the abbreviations
9429 for the next compile unit) or if the end of the abbreviation
9430 table is reached. */
dce234bc
PP
9431 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9432 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9433 break;
9434 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9435 abbrev_ptr += bytes_read;
e7c27a73 9436 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9437 break;
9438 }
f3dd6933
DJ
9439
9440 xfree (cur_attrs);
c906108c
SS
9441}
9442
f3dd6933 9443/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9444
c906108c 9445static void
f3dd6933 9446dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9447{
f3dd6933 9448 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9449
f3dd6933
DJ
9450 obstack_free (&cu->abbrev_obstack, NULL);
9451 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9452}
9453
9454/* Lookup an abbrev_info structure in the abbrev hash table. */
9455
9456static struct abbrev_info *
e7c27a73 9457dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9458{
9459 unsigned int hash_number;
9460 struct abbrev_info *abbrev;
9461
9462 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9463 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9464
9465 while (abbrev)
9466 {
9467 if (abbrev->number == number)
9468 return abbrev;
9469 else
9470 abbrev = abbrev->next;
9471 }
9472 return NULL;
9473}
9474
72bf9492
DJ
9475/* Returns nonzero if TAG represents a type that we might generate a partial
9476 symbol for. */
9477
9478static int
9479is_type_tag_for_partial (int tag)
9480{
9481 switch (tag)
9482 {
9483#if 0
9484 /* Some types that would be reasonable to generate partial symbols for,
9485 that we don't at present. */
9486 case DW_TAG_array_type:
9487 case DW_TAG_file_type:
9488 case DW_TAG_ptr_to_member_type:
9489 case DW_TAG_set_type:
9490 case DW_TAG_string_type:
9491 case DW_TAG_subroutine_type:
9492#endif
9493 case DW_TAG_base_type:
9494 case DW_TAG_class_type:
680b30c7 9495 case DW_TAG_interface_type:
72bf9492
DJ
9496 case DW_TAG_enumeration_type:
9497 case DW_TAG_structure_type:
9498 case DW_TAG_subrange_type:
9499 case DW_TAG_typedef:
9500 case DW_TAG_union_type:
9501 return 1;
9502 default:
9503 return 0;
9504 }
9505}
9506
9507/* Load all DIEs that are interesting for partial symbols into memory. */
9508
9509static struct partial_die_info *
93311388
DE
9510load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9511 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9512{
bb5ed363 9513 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9514 struct partial_die_info *part_die;
9515 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9516 struct abbrev_info *abbrev;
9517 unsigned int bytes_read;
5afb4e99 9518 unsigned int load_all = 0;
72bf9492
DJ
9519
9520 int nesting_level = 1;
9521
9522 parent_die = NULL;
9523 last_die = NULL;
9524
7adf1e79
DE
9525 gdb_assert (cu->per_cu != NULL);
9526 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
9527 load_all = 1;
9528
72bf9492
DJ
9529 cu->partial_dies
9530 = htab_create_alloc_ex (cu->header.length / 12,
9531 partial_die_hash,
9532 partial_die_eq,
9533 NULL,
9534 &cu->comp_unit_obstack,
9535 hashtab_obstack_allocate,
9536 dummy_obstack_deallocate);
9537
9538 part_die = obstack_alloc (&cu->comp_unit_obstack,
9539 sizeof (struct partial_die_info));
9540
9541 while (1)
9542 {
9543 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9544
9545 /* A NULL abbrev means the end of a series of children. */
9546 if (abbrev == NULL)
9547 {
9548 if (--nesting_level == 0)
9549 {
9550 /* PART_DIE was probably the last thing allocated on the
9551 comp_unit_obstack, so we could call obstack_free
9552 here. We don't do that because the waste is small,
9553 and will be cleaned up when we're done with this
9554 compilation unit. This way, we're also more robust
9555 against other users of the comp_unit_obstack. */
9556 return first_die;
9557 }
9558 info_ptr += bytes_read;
9559 last_die = parent_die;
9560 parent_die = parent_die->die_parent;
9561 continue;
9562 }
9563
98bfdba5
PA
9564 /* Check for template arguments. We never save these; if
9565 they're seen, we just mark the parent, and go on our way. */
9566 if (parent_die != NULL
9567 && cu->language == language_cplus
9568 && (abbrev->tag == DW_TAG_template_type_param
9569 || abbrev->tag == DW_TAG_template_value_param))
9570 {
9571 parent_die->has_template_arguments = 1;
9572
9573 if (!load_all)
9574 {
9575 /* We don't need a partial DIE for the template argument. */
9576 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9577 cu);
9578 continue;
9579 }
9580 }
9581
9582 /* We only recurse into subprograms looking for template arguments.
9583 Skip their other children. */
9584 if (!load_all
9585 && cu->language == language_cplus
9586 && parent_die != NULL
9587 && parent_die->tag == DW_TAG_subprogram)
9588 {
9589 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9590 continue;
9591 }
9592
5afb4e99
DJ
9593 /* Check whether this DIE is interesting enough to save. Normally
9594 we would not be interested in members here, but there may be
9595 later variables referencing them via DW_AT_specification (for
9596 static members). */
9597 if (!load_all
9598 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9599 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9600 && abbrev->tag != DW_TAG_enumerator
9601 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9602 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9603 && abbrev->tag != DW_TAG_variable
5afb4e99 9604 && abbrev->tag != DW_TAG_namespace
f55ee35c 9605 && abbrev->tag != DW_TAG_module
5afb4e99 9606 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9607 {
9608 /* Otherwise we skip to the next sibling, if any. */
93311388 9609 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9610 continue;
9611 }
9612
93311388
DE
9613 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9614 buffer, info_ptr, cu);
72bf9492
DJ
9615
9616 /* This two-pass algorithm for processing partial symbols has a
9617 high cost in cache pressure. Thus, handle some simple cases
9618 here which cover the majority of C partial symbols. DIEs
9619 which neither have specification tags in them, nor could have
9620 specification tags elsewhere pointing at them, can simply be
9621 processed and discarded.
9622
9623 This segment is also optional; scan_partial_symbols and
9624 add_partial_symbol will handle these DIEs if we chain
9625 them in normally. When compilers which do not emit large
9626 quantities of duplicate debug information are more common,
9627 this code can probably be removed. */
9628
9629 /* Any complete simple types at the top level (pretty much all
9630 of them, for a language without namespaces), can be processed
9631 directly. */
9632 if (parent_die == NULL
9633 && part_die->has_specification == 0
9634 && part_die->is_declaration == 0
d8228535 9635 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9636 || part_die->tag == DW_TAG_base_type
9637 || part_die->tag == DW_TAG_subrange_type))
9638 {
9639 if (building_psymtab && part_die->name != NULL)
04a679b8 9640 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9641 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9642 &objfile->static_psymbols,
9643 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9644 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9645 continue;
9646 }
9647
d8228535
JK
9648 /* The exception for DW_TAG_typedef with has_children above is
9649 a workaround of GCC PR debug/47510. In the case of this complaint
9650 type_name_no_tag_or_error will error on such types later.
9651
9652 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9653 it could not find the child DIEs referenced later, this is checked
9654 above. In correct DWARF DW_TAG_typedef should have no children. */
9655
9656 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9657 complaint (&symfile_complaints,
9658 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9659 "- DIE at 0x%x [in module %s]"),
b64f50a1 9660 part_die->offset.sect_off, objfile->name);
d8228535 9661
72bf9492
DJ
9662 /* If we're at the second level, and we're an enumerator, and
9663 our parent has no specification (meaning possibly lives in a
9664 namespace elsewhere), then we can add the partial symbol now
9665 instead of queueing it. */
9666 if (part_die->tag == DW_TAG_enumerator
9667 && parent_die != NULL
9668 && parent_die->die_parent == NULL
9669 && parent_die->tag == DW_TAG_enumeration_type
9670 && parent_die->has_specification == 0)
9671 {
9672 if (part_die->name == NULL)
3e43a32a
MS
9673 complaint (&symfile_complaints,
9674 _("malformed enumerator DIE ignored"));
72bf9492 9675 else if (building_psymtab)
04a679b8 9676 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9677 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9678 (cu->language == language_cplus
9679 || cu->language == language_java)
bb5ed363
DE
9680 ? &objfile->global_psymbols
9681 : &objfile->static_psymbols,
9682 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9683
93311388 9684 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9685 continue;
9686 }
9687
9688 /* We'll save this DIE so link it in. */
9689 part_die->die_parent = parent_die;
9690 part_die->die_sibling = NULL;
9691 part_die->die_child = NULL;
9692
9693 if (last_die && last_die == parent_die)
9694 last_die->die_child = part_die;
9695 else if (last_die)
9696 last_die->die_sibling = part_die;
9697
9698 last_die = part_die;
9699
9700 if (first_die == NULL)
9701 first_die = part_die;
9702
9703 /* Maybe add the DIE to the hash table. Not all DIEs that we
9704 find interesting need to be in the hash table, because we
9705 also have the parent/sibling/child chains; only those that we
9706 might refer to by offset later during partial symbol reading.
9707
9708 For now this means things that might have be the target of a
9709 DW_AT_specification, DW_AT_abstract_origin, or
9710 DW_AT_extension. DW_AT_extension will refer only to
9711 namespaces; DW_AT_abstract_origin refers to functions (and
9712 many things under the function DIE, but we do not recurse
9713 into function DIEs during partial symbol reading) and
9714 possibly variables as well; DW_AT_specification refers to
9715 declarations. Declarations ought to have the DW_AT_declaration
9716 flag. It happens that GCC forgets to put it in sometimes, but
9717 only for functions, not for types.
9718
9719 Adding more things than necessary to the hash table is harmless
9720 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9721 wasted time in find_partial_die, when we reread the compilation
9722 unit with load_all_dies set. */
72bf9492 9723
5afb4e99 9724 if (load_all
72929c62 9725 || abbrev->tag == DW_TAG_constant
5afb4e99 9726 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9727 || abbrev->tag == DW_TAG_variable
9728 || abbrev->tag == DW_TAG_namespace
9729 || part_die->is_declaration)
9730 {
9731 void **slot;
9732
9733 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 9734 part_die->offset.sect_off, INSERT);
72bf9492
DJ
9735 *slot = part_die;
9736 }
9737
9738 part_die = obstack_alloc (&cu->comp_unit_obstack,
9739 sizeof (struct partial_die_info));
9740
9741 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9742 we have no reason to follow the children of structures; for other
98bfdba5
PA
9743 languages we have to, so that we can get at method physnames
9744 to infer fully qualified class names, for DW_AT_specification,
9745 and for C++ template arguments. For C++, we also look one level
9746 inside functions to find template arguments (if the name of the
9747 function does not already contain the template arguments).
bc30ff58
JB
9748
9749 For Ada, we need to scan the children of subprograms and lexical
9750 blocks as well because Ada allows the definition of nested
9751 entities that could be interesting for the debugger, such as
9752 nested subprograms for instance. */
72bf9492 9753 if (last_die->has_children
5afb4e99
DJ
9754 && (load_all
9755 || last_die->tag == DW_TAG_namespace
f55ee35c 9756 || last_die->tag == DW_TAG_module
72bf9492 9757 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9758 || (cu->language == language_cplus
9759 && last_die->tag == DW_TAG_subprogram
9760 && (last_die->name == NULL
9761 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9762 || (cu->language != language_c
9763 && (last_die->tag == DW_TAG_class_type
680b30c7 9764 || last_die->tag == DW_TAG_interface_type
72bf9492 9765 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9766 || last_die->tag == DW_TAG_union_type))
9767 || (cu->language == language_ada
9768 && (last_die->tag == DW_TAG_subprogram
9769 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9770 {
9771 nesting_level++;
9772 parent_die = last_die;
9773 continue;
9774 }
9775
9776 /* Otherwise we skip to the next sibling, if any. */
93311388 9777 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9778
9779 /* Back to the top, do it again. */
9780 }
9781}
9782
c906108c
SS
9783/* Read a minimal amount of information into the minimal die structure. */
9784
fe1b8b76 9785static gdb_byte *
72bf9492
DJ
9786read_partial_die (struct partial_die_info *part_die,
9787 struct abbrev_info *abbrev,
9788 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9789 gdb_byte *buffer, gdb_byte *info_ptr,
9790 struct dwarf2_cu *cu)
c906108c 9791{
bb5ed363 9792 struct objfile *objfile = cu->objfile;
fa238c03 9793 unsigned int i;
c906108c 9794 struct attribute attr;
c5aa993b 9795 int has_low_pc_attr = 0;
c906108c
SS
9796 int has_high_pc_attr = 0;
9797
72bf9492 9798 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9799
b64f50a1 9800 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
9801
9802 info_ptr += abbrev_len;
9803
9804 if (abbrev == NULL)
9805 return info_ptr;
9806
c906108c
SS
9807 part_die->tag = abbrev->tag;
9808 part_die->has_children = abbrev->has_children;
c906108c
SS
9809
9810 for (i = 0; i < abbrev->num_attrs; ++i)
9811 {
e7c27a73 9812 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9813
9814 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9815 partial symbol table. */
c906108c
SS
9816 switch (attr.name)
9817 {
9818 case DW_AT_name:
71c25dea
TT
9819 switch (part_die->tag)
9820 {
9821 case DW_TAG_compile_unit:
348e048f 9822 case DW_TAG_type_unit:
71c25dea
TT
9823 /* Compilation units have a DW_AT_name that is a filename, not
9824 a source language identifier. */
9825 case DW_TAG_enumeration_type:
9826 case DW_TAG_enumerator:
9827 /* These tags always have simple identifiers already; no need
9828 to canonicalize them. */
9829 part_die->name = DW_STRING (&attr);
9830 break;
9831 default:
9832 part_die->name
9833 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9834 &objfile->objfile_obstack);
71c25dea
TT
9835 break;
9836 }
c906108c 9837 break;
31ef98ae 9838 case DW_AT_linkage_name:
c906108c 9839 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9840 /* Note that both forms of linkage name might appear. We
9841 assume they will be the same, and we only store the last
9842 one we see. */
94af9270
KS
9843 if (cu->language == language_ada)
9844 part_die->name = DW_STRING (&attr);
abc72ce4 9845 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9846 break;
9847 case DW_AT_low_pc:
9848 has_low_pc_attr = 1;
9849 part_die->lowpc = DW_ADDR (&attr);
9850 break;
9851 case DW_AT_high_pc:
9852 has_high_pc_attr = 1;
9853 part_die->highpc = DW_ADDR (&attr);
9854 break;
9855 case DW_AT_location:
0963b4bd 9856 /* Support the .debug_loc offsets. */
8e19ed76
PS
9857 if (attr_form_is_block (&attr))
9858 {
9859 part_die->locdesc = DW_BLOCK (&attr);
9860 }
3690dd37 9861 else if (attr_form_is_section_offset (&attr))
8e19ed76 9862 {
4d3c2250 9863 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9864 }
9865 else
9866 {
4d3c2250
KB
9867 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9868 "partial symbol information");
8e19ed76 9869 }
c906108c 9870 break;
c906108c
SS
9871 case DW_AT_external:
9872 part_die->is_external = DW_UNSND (&attr);
9873 break;
9874 case DW_AT_declaration:
9875 part_die->is_declaration = DW_UNSND (&attr);
9876 break;
9877 case DW_AT_type:
9878 part_die->has_type = 1;
9879 break;
9880 case DW_AT_abstract_origin:
9881 case DW_AT_specification:
72bf9492
DJ
9882 case DW_AT_extension:
9883 part_die->has_specification = 1;
c764a876 9884 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9885 break;
9886 case DW_AT_sibling:
9887 /* Ignore absolute siblings, they might point outside of
9888 the current compile unit. */
9889 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9890 complaint (&symfile_complaints,
9891 _("ignoring absolute DW_AT_sibling"));
c906108c 9892 else
b64f50a1 9893 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 9894 break;
fa4028e9
JB
9895 case DW_AT_byte_size:
9896 part_die->has_byte_size = 1;
9897 break;
68511cec
CES
9898 case DW_AT_calling_convention:
9899 /* DWARF doesn't provide a way to identify a program's source-level
9900 entry point. DW_AT_calling_convention attributes are only meant
9901 to describe functions' calling conventions.
9902
9903 However, because it's a necessary piece of information in
9904 Fortran, and because DW_CC_program is the only piece of debugging
9905 information whose definition refers to a 'main program' at all,
9906 several compilers have begun marking Fortran main programs with
9907 DW_CC_program --- even when those functions use the standard
9908 calling conventions.
9909
9910 So until DWARF specifies a way to provide this information and
9911 compilers pick up the new representation, we'll support this
9912 practice. */
9913 if (DW_UNSND (&attr) == DW_CC_program
9914 && cu->language == language_fortran)
01f8c46d
JK
9915 {
9916 set_main_name (part_die->name);
9917
9918 /* As this DIE has a static linkage the name would be difficult
9919 to look up later. */
9920 language_of_main = language_fortran;
9921 }
68511cec 9922 break;
481860b3
GB
9923 case DW_AT_inline:
9924 if (DW_UNSND (&attr) == DW_INL_inlined
9925 || DW_UNSND (&attr) == DW_INL_declared_inlined)
9926 part_die->may_be_inlined = 1;
9927 break;
c906108c
SS
9928 default:
9929 break;
9930 }
9931 }
9932
9373cf26
JK
9933 if (has_low_pc_attr && has_high_pc_attr)
9934 {
9935 /* When using the GNU linker, .gnu.linkonce. sections are used to
9936 eliminate duplicate copies of functions and vtables and such.
9937 The linker will arbitrarily choose one and discard the others.
9938 The AT_*_pc values for such functions refer to local labels in
9939 these sections. If the section from that file was discarded, the
9940 labels are not in the output, so the relocs get a value of 0.
9941 If this is a discarded function, mark the pc bounds as invalid,
9942 so that GDB will ignore it. */
9943 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9944 {
bb5ed363 9945 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9946
9947 complaint (&symfile_complaints,
9948 _("DW_AT_low_pc %s is zero "
9949 "for DIE at 0x%x [in module %s]"),
9950 paddress (gdbarch, part_die->lowpc),
b64f50a1 9951 part_die->offset.sect_off, objfile->name);
9373cf26
JK
9952 }
9953 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9954 else if (part_die->lowpc >= part_die->highpc)
9955 {
bb5ed363 9956 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9957
9958 complaint (&symfile_complaints,
9959 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9960 "for DIE at 0x%x [in module %s]"),
9961 paddress (gdbarch, part_die->lowpc),
9962 paddress (gdbarch, part_die->highpc),
b64f50a1 9963 part_die->offset.sect_off, objfile->name);
9373cf26
JK
9964 }
9965 else
9966 part_die->has_pc_info = 1;
9967 }
85cbf3d3 9968
c906108c
SS
9969 return info_ptr;
9970}
9971
72bf9492
DJ
9972/* Find a cached partial DIE at OFFSET in CU. */
9973
9974static struct partial_die_info *
b64f50a1 9975find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
9976{
9977 struct partial_die_info *lookup_die = NULL;
9978 struct partial_die_info part_die;
9979
9980 part_die.offset = offset;
b64f50a1
JK
9981 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
9982 offset.sect_off);
72bf9492 9983
72bf9492
DJ
9984 return lookup_die;
9985}
9986
348e048f
DE
9987/* Find a partial DIE at OFFSET, which may or may not be in CU,
9988 except in the case of .debug_types DIEs which do not reference
9989 outside their CU (they do however referencing other types via
55f1336d 9990 DW_FORM_ref_sig8). */
72bf9492
DJ
9991
9992static struct partial_die_info *
b64f50a1 9993find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 9994{
bb5ed363 9995 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9996 struct dwarf2_per_cu_data *per_cu = NULL;
9997 struct partial_die_info *pd = NULL;
72bf9492 9998
b0df02fd 9999 if (cu->per_cu->debug_types_section)
348e048f
DE
10000 {
10001 pd = find_partial_die_in_comp_unit (offset, cu);
10002 if (pd != NULL)
10003 return pd;
10004 goto not_found;
10005 }
10006
45452591 10007 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
10008 {
10009 pd = find_partial_die_in_comp_unit (offset, cu);
10010 if (pd != NULL)
10011 return pd;
10012 }
72bf9492 10013
bb5ed363 10014 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 10015
98bfdba5 10016 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 10017 load_partial_comp_unit (per_cu);
ae038cb0
DJ
10018
10019 per_cu->cu->last_used = 0;
5afb4e99
DJ
10020 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10021
10022 if (pd == NULL && per_cu->load_all_dies == 0)
10023 {
10024 struct cleanup *back_to;
10025 struct partial_die_info comp_unit_die;
10026 struct abbrev_info *abbrev;
10027 unsigned int bytes_read;
10028 char *info_ptr;
10029
10030 per_cu->load_all_dies = 1;
10031
10032 /* Re-read the DIEs. */
10033 back_to = make_cleanup (null_cleanup, 0);
10034 if (per_cu->cu->dwarf2_abbrevs == NULL)
10035 {
e5fe5e75 10036 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 10037 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10038 }
dce234bc 10039 info_ptr = (dwarf2_per_objfile->info.buffer
b64f50a1
JK
10040 + per_cu->cu->header.offset.sect_off
10041 + per_cu->cu->header.first_die_offset.cu_off);
5afb4e99
DJ
10042 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10043 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 10044 objfile->obfd,
93311388 10045 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10046 per_cu->cu);
10047 if (comp_unit_die.has_children)
bb5ed363 10048 load_partial_dies (objfile->obfd,
93311388
DE
10049 dwarf2_per_objfile->info.buffer, info_ptr,
10050 0, per_cu->cu);
5afb4e99
DJ
10051 do_cleanups (back_to);
10052
10053 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10054 }
10055
348e048f
DE
10056 not_found:
10057
5afb4e99
DJ
10058 if (pd == NULL)
10059 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10060 _("could not find partial DIE 0x%x "
10061 "in cache [from module %s]\n"),
b64f50a1 10062 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 10063 return pd;
72bf9492
DJ
10064}
10065
abc72ce4
DE
10066/* See if we can figure out if the class lives in a namespace. We do
10067 this by looking for a member function; its demangled name will
10068 contain namespace info, if there is any. */
10069
10070static void
10071guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10072 struct dwarf2_cu *cu)
10073{
10074 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10075 what template types look like, because the demangler
10076 frequently doesn't give the same name as the debug info. We
10077 could fix this by only using the demangled name to get the
10078 prefix (but see comment in read_structure_type). */
10079
10080 struct partial_die_info *real_pdi;
10081 struct partial_die_info *child_pdi;
10082
10083 /* If this DIE (this DIE's specification, if any) has a parent, then
10084 we should not do this. We'll prepend the parent's fully qualified
10085 name when we create the partial symbol. */
10086
10087 real_pdi = struct_pdi;
10088 while (real_pdi->has_specification)
10089 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10090
10091 if (real_pdi->die_parent != NULL)
10092 return;
10093
10094 for (child_pdi = struct_pdi->die_child;
10095 child_pdi != NULL;
10096 child_pdi = child_pdi->die_sibling)
10097 {
10098 if (child_pdi->tag == DW_TAG_subprogram
10099 && child_pdi->linkage_name != NULL)
10100 {
10101 char *actual_class_name
10102 = language_class_name_from_physname (cu->language_defn,
10103 child_pdi->linkage_name);
10104 if (actual_class_name != NULL)
10105 {
10106 struct_pdi->name
10107 = obsavestring (actual_class_name,
10108 strlen (actual_class_name),
10109 &cu->objfile->objfile_obstack);
10110 xfree (actual_class_name);
10111 }
10112 break;
10113 }
10114 }
10115}
10116
72bf9492
DJ
10117/* Adjust PART_DIE before generating a symbol for it. This function
10118 may set the is_external flag or change the DIE's name. */
10119
10120static void
10121fixup_partial_die (struct partial_die_info *part_die,
10122 struct dwarf2_cu *cu)
10123{
abc72ce4
DE
10124 /* Once we've fixed up a die, there's no point in doing so again.
10125 This also avoids a memory leak if we were to call
10126 guess_partial_die_structure_name multiple times. */
10127 if (part_die->fixup_called)
10128 return;
10129
72bf9492
DJ
10130 /* If we found a reference attribute and the DIE has no name, try
10131 to find a name in the referred to DIE. */
10132
10133 if (part_die->name == NULL && part_die->has_specification)
10134 {
10135 struct partial_die_info *spec_die;
72bf9492 10136
10b3939b 10137 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10138
10b3939b 10139 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10140
10141 if (spec_die->name)
10142 {
10143 part_die->name = spec_die->name;
10144
10145 /* Copy DW_AT_external attribute if it is set. */
10146 if (spec_die->is_external)
10147 part_die->is_external = spec_die->is_external;
10148 }
10149 }
10150
10151 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10152
10153 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10154 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10155
abc72ce4
DE
10156 /* If there is no parent die to provide a namespace, and there are
10157 children, see if we can determine the namespace from their linkage
122d1940 10158 name. */
abc72ce4 10159 if (cu->language == language_cplus
8b70b953 10160 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10161 && part_die->die_parent == NULL
10162 && part_die->has_children
10163 && (part_die->tag == DW_TAG_class_type
10164 || part_die->tag == DW_TAG_structure_type
10165 || part_die->tag == DW_TAG_union_type))
10166 guess_partial_die_structure_name (part_die, cu);
10167
53832f31
TT
10168 /* GCC might emit a nameless struct or union that has a linkage
10169 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10170 if (part_die->name == NULL
96408a79
SA
10171 && (part_die->tag == DW_TAG_class_type
10172 || part_die->tag == DW_TAG_interface_type
10173 || part_die->tag == DW_TAG_structure_type
10174 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10175 && part_die->linkage_name != NULL)
10176 {
10177 char *demangled;
10178
10179 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10180 if (demangled)
10181 {
96408a79
SA
10182 const char *base;
10183
10184 /* Strip any leading namespaces/classes, keep only the base name.
10185 DW_AT_name for named DIEs does not contain the prefixes. */
10186 base = strrchr (demangled, ':');
10187 if (base && base > demangled && base[-1] == ':')
10188 base++;
10189 else
10190 base = demangled;
10191
10192 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10193 &cu->objfile->objfile_obstack);
10194 xfree (demangled);
10195 }
10196 }
10197
abc72ce4 10198 part_die->fixup_called = 1;
72bf9492
DJ
10199}
10200
a8329558 10201/* Read an attribute value described by an attribute form. */
c906108c 10202
fe1b8b76 10203static gdb_byte *
a8329558 10204read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10205 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10206 struct dwarf2_cu *cu)
c906108c 10207{
e7c27a73 10208 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10209 unsigned int bytes_read;
10210 struct dwarf_block *blk;
10211
a8329558
KW
10212 attr->form = form;
10213 switch (form)
c906108c 10214 {
c906108c 10215 case DW_FORM_ref_addr:
ae411497
TT
10216 if (cu->header.version == 2)
10217 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10218 else
3e43a32a
MS
10219 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10220 &cu->header, &bytes_read);
ae411497
TT
10221 info_ptr += bytes_read;
10222 break;
10223 case DW_FORM_addr:
e7c27a73 10224 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10225 info_ptr += bytes_read;
c906108c
SS
10226 break;
10227 case DW_FORM_block2:
7b5a2f43 10228 blk = dwarf_alloc_block (cu);
c906108c
SS
10229 blk->size = read_2_bytes (abfd, info_ptr);
10230 info_ptr += 2;
10231 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10232 info_ptr += blk->size;
10233 DW_BLOCK (attr) = blk;
10234 break;
10235 case DW_FORM_block4:
7b5a2f43 10236 blk = dwarf_alloc_block (cu);
c906108c
SS
10237 blk->size = read_4_bytes (abfd, info_ptr);
10238 info_ptr += 4;
10239 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10240 info_ptr += blk->size;
10241 DW_BLOCK (attr) = blk;
10242 break;
10243 case DW_FORM_data2:
10244 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10245 info_ptr += 2;
10246 break;
10247 case DW_FORM_data4:
10248 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10249 info_ptr += 4;
10250 break;
10251 case DW_FORM_data8:
10252 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10253 info_ptr += 8;
10254 break;
2dc7f7b3
TT
10255 case DW_FORM_sec_offset:
10256 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10257 info_ptr += bytes_read;
10258 break;
c906108c 10259 case DW_FORM_string:
9b1c24c8 10260 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10261 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10262 info_ptr += bytes_read;
10263 break;
4bdf3d34
JJ
10264 case DW_FORM_strp:
10265 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10266 &bytes_read);
8285870a 10267 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10268 info_ptr += bytes_read;
10269 break;
2dc7f7b3 10270 case DW_FORM_exprloc:
c906108c 10271 case DW_FORM_block:
7b5a2f43 10272 blk = dwarf_alloc_block (cu);
c906108c
SS
10273 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10274 info_ptr += bytes_read;
10275 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10276 info_ptr += blk->size;
10277 DW_BLOCK (attr) = blk;
10278 break;
10279 case DW_FORM_block1:
7b5a2f43 10280 blk = dwarf_alloc_block (cu);
c906108c
SS
10281 blk->size = read_1_byte (abfd, info_ptr);
10282 info_ptr += 1;
10283 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10284 info_ptr += blk->size;
10285 DW_BLOCK (attr) = blk;
10286 break;
10287 case DW_FORM_data1:
10288 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10289 info_ptr += 1;
10290 break;
10291 case DW_FORM_flag:
10292 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10293 info_ptr += 1;
10294 break;
2dc7f7b3
TT
10295 case DW_FORM_flag_present:
10296 DW_UNSND (attr) = 1;
10297 break;
c906108c
SS
10298 case DW_FORM_sdata:
10299 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10300 info_ptr += bytes_read;
10301 break;
10302 case DW_FORM_udata:
10303 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10304 info_ptr += bytes_read;
10305 break;
10306 case DW_FORM_ref1:
b64f50a1
JK
10307 DW_ADDR (attr) = (cu->header.offset.sect_off
10308 + read_1_byte (abfd, info_ptr));
c906108c
SS
10309 info_ptr += 1;
10310 break;
10311 case DW_FORM_ref2:
b64f50a1
JK
10312 DW_ADDR (attr) = (cu->header.offset.sect_off
10313 + read_2_bytes (abfd, info_ptr));
c906108c
SS
10314 info_ptr += 2;
10315 break;
10316 case DW_FORM_ref4:
b64f50a1
JK
10317 DW_ADDR (attr) = (cu->header.offset.sect_off
10318 + read_4_bytes (abfd, info_ptr));
c906108c
SS
10319 info_ptr += 4;
10320 break;
613e1657 10321 case DW_FORM_ref8:
b64f50a1
JK
10322 DW_ADDR (attr) = (cu->header.offset.sect_off
10323 + read_8_bytes (abfd, info_ptr));
613e1657
KB
10324 info_ptr += 8;
10325 break;
55f1336d 10326 case DW_FORM_ref_sig8:
348e048f
DE
10327 /* Convert the signature to something we can record in DW_UNSND
10328 for later lookup.
10329 NOTE: This is NULL if the type wasn't found. */
10330 DW_SIGNATURED_TYPE (attr) =
10331 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10332 info_ptr += 8;
10333 break;
c906108c 10334 case DW_FORM_ref_udata:
b64f50a1 10335 DW_ADDR (attr) = (cu->header.offset.sect_off
10b3939b 10336 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10337 info_ptr += bytes_read;
10338 break;
c906108c 10339 case DW_FORM_indirect:
a8329558
KW
10340 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10341 info_ptr += bytes_read;
e7c27a73 10342 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10343 break;
c906108c 10344 default:
8a3fe4f8 10345 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10346 dwarf_form_name (form),
10347 bfd_get_filename (abfd));
c906108c 10348 }
28e94949
JB
10349
10350 /* We have seen instances where the compiler tried to emit a byte
10351 size attribute of -1 which ended up being encoded as an unsigned
10352 0xffffffff. Although 0xffffffff is technically a valid size value,
10353 an object of this size seems pretty unlikely so we can relatively
10354 safely treat these cases as if the size attribute was invalid and
10355 treat them as zero by default. */
10356 if (attr->name == DW_AT_byte_size
10357 && form == DW_FORM_data4
10358 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10359 {
10360 complaint
10361 (&symfile_complaints,
43bbcdc2
PH
10362 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10363 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10364 DW_UNSND (attr) = 0;
10365 }
28e94949 10366
c906108c
SS
10367 return info_ptr;
10368}
10369
a8329558
KW
10370/* Read an attribute described by an abbreviated attribute. */
10371
fe1b8b76 10372static gdb_byte *
a8329558 10373read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10374 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10375{
10376 attr->name = abbrev->name;
e7c27a73 10377 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10378}
10379
0963b4bd 10380/* Read dwarf information from a buffer. */
c906108c
SS
10381
10382static unsigned int
fe1b8b76 10383read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10384{
fe1b8b76 10385 return bfd_get_8 (abfd, buf);
c906108c
SS
10386}
10387
10388static int
fe1b8b76 10389read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10390{
fe1b8b76 10391 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10392}
10393
10394static unsigned int
fe1b8b76 10395read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10396{
fe1b8b76 10397 return bfd_get_16 (abfd, buf);
c906108c
SS
10398}
10399
21ae7a4d
JK
10400static int
10401read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10402{
10403 return bfd_get_signed_16 (abfd, buf);
10404}
10405
c906108c 10406static unsigned int
fe1b8b76 10407read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10408{
fe1b8b76 10409 return bfd_get_32 (abfd, buf);
c906108c
SS
10410}
10411
21ae7a4d
JK
10412static int
10413read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10414{
10415 return bfd_get_signed_32 (abfd, buf);
10416}
10417
93311388 10418static ULONGEST
fe1b8b76 10419read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10420{
fe1b8b76 10421 return bfd_get_64 (abfd, buf);
c906108c
SS
10422}
10423
10424static CORE_ADDR
fe1b8b76 10425read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10426 unsigned int *bytes_read)
c906108c 10427{
e7c27a73 10428 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10429 CORE_ADDR retval = 0;
10430
107d2387 10431 if (cu_header->signed_addr_p)
c906108c 10432 {
107d2387
AC
10433 switch (cu_header->addr_size)
10434 {
10435 case 2:
fe1b8b76 10436 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10437 break;
10438 case 4:
fe1b8b76 10439 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10440 break;
10441 case 8:
fe1b8b76 10442 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10443 break;
10444 default:
8e65ff28 10445 internal_error (__FILE__, __LINE__,
e2e0b3e5 10446 _("read_address: bad switch, signed [in module %s]"),
659b0389 10447 bfd_get_filename (abfd));
107d2387
AC
10448 }
10449 }
10450 else
10451 {
10452 switch (cu_header->addr_size)
10453 {
10454 case 2:
fe1b8b76 10455 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10456 break;
10457 case 4:
fe1b8b76 10458 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10459 break;
10460 case 8:
fe1b8b76 10461 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10462 break;
10463 default:
8e65ff28 10464 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10465 _("read_address: bad switch, "
10466 "unsigned [in module %s]"),
659b0389 10467 bfd_get_filename (abfd));
107d2387 10468 }
c906108c 10469 }
64367e0a 10470
107d2387
AC
10471 *bytes_read = cu_header->addr_size;
10472 return retval;
c906108c
SS
10473}
10474
f7ef9339 10475/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10476 specification allows the initial length to take up either 4 bytes
10477 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10478 bytes describe the length and all offsets will be 8 bytes in length
10479 instead of 4.
10480
f7ef9339
KB
10481 An older, non-standard 64-bit format is also handled by this
10482 function. The older format in question stores the initial length
10483 as an 8-byte quantity without an escape value. Lengths greater
10484 than 2^32 aren't very common which means that the initial 4 bytes
10485 is almost always zero. Since a length value of zero doesn't make
10486 sense for the 32-bit format, this initial zero can be considered to
10487 be an escape value which indicates the presence of the older 64-bit
10488 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10489 greater than 4GB. If it becomes necessary to handle lengths
10490 somewhat larger than 4GB, we could allow other small values (such
10491 as the non-sensical values of 1, 2, and 3) to also be used as
10492 escape values indicating the presence of the old format.
f7ef9339 10493
917c78fc
MK
10494 The value returned via bytes_read should be used to increment the
10495 relevant pointer after calling read_initial_length().
c764a876 10496
613e1657
KB
10497 [ Note: read_initial_length() and read_offset() are based on the
10498 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10499 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10500 from:
10501
f7ef9339 10502 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10503
613e1657
KB
10504 This document is only a draft and is subject to change. (So beware.)
10505
f7ef9339 10506 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10507 determined empirically by examining 64-bit ELF files produced by
10508 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10509
10510 - Kevin, July 16, 2002
613e1657
KB
10511 ] */
10512
10513static LONGEST
c764a876 10514read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10515{
fe1b8b76 10516 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10517
dd373385 10518 if (length == 0xffffffff)
613e1657 10519 {
fe1b8b76 10520 length = bfd_get_64 (abfd, buf + 4);
613e1657 10521 *bytes_read = 12;
613e1657 10522 }
dd373385 10523 else if (length == 0)
f7ef9339 10524 {
dd373385 10525 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10526 length = bfd_get_64 (abfd, buf);
f7ef9339 10527 *bytes_read = 8;
f7ef9339 10528 }
613e1657
KB
10529 else
10530 {
10531 *bytes_read = 4;
613e1657
KB
10532 }
10533
c764a876
DE
10534 return length;
10535}
dd373385 10536
c764a876
DE
10537/* Cover function for read_initial_length.
10538 Returns the length of the object at BUF, and stores the size of the
10539 initial length in *BYTES_READ and stores the size that offsets will be in
10540 *OFFSET_SIZE.
10541 If the initial length size is not equivalent to that specified in
10542 CU_HEADER then issue a complaint.
10543 This is useful when reading non-comp-unit headers. */
dd373385 10544
c764a876
DE
10545static LONGEST
10546read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10547 const struct comp_unit_head *cu_header,
10548 unsigned int *bytes_read,
10549 unsigned int *offset_size)
10550{
10551 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10552
10553 gdb_assert (cu_header->initial_length_size == 4
10554 || cu_header->initial_length_size == 8
10555 || cu_header->initial_length_size == 12);
10556
10557 if (cu_header->initial_length_size != *bytes_read)
10558 complaint (&symfile_complaints,
10559 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10560
c764a876 10561 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10562 return length;
613e1657
KB
10563}
10564
10565/* Read an offset from the data stream. The size of the offset is
917c78fc 10566 given by cu_header->offset_size. */
613e1657
KB
10567
10568static LONGEST
fe1b8b76 10569read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10570 unsigned int *bytes_read)
c764a876
DE
10571{
10572 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10573
c764a876
DE
10574 *bytes_read = cu_header->offset_size;
10575 return offset;
10576}
10577
10578/* Read an offset from the data stream. */
10579
10580static LONGEST
10581read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10582{
10583 LONGEST retval = 0;
10584
c764a876 10585 switch (offset_size)
613e1657
KB
10586 {
10587 case 4:
fe1b8b76 10588 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10589 break;
10590 case 8:
fe1b8b76 10591 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10592 break;
10593 default:
8e65ff28 10594 internal_error (__FILE__, __LINE__,
c764a876 10595 _("read_offset_1: bad switch [in module %s]"),
659b0389 10596 bfd_get_filename (abfd));
613e1657
KB
10597 }
10598
917c78fc 10599 return retval;
613e1657
KB
10600}
10601
fe1b8b76
JB
10602static gdb_byte *
10603read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10604{
10605 /* If the size of a host char is 8 bits, we can return a pointer
10606 to the buffer, otherwise we have to copy the data to a buffer
10607 allocated on the temporary obstack. */
4bdf3d34 10608 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10609 return buf;
c906108c
SS
10610}
10611
10612static char *
9b1c24c8 10613read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10614{
10615 /* If the size of a host char is 8 bits, we can return a pointer
10616 to the string, otherwise we have to copy the string to a buffer
10617 allocated on the temporary obstack. */
4bdf3d34 10618 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10619 if (*buf == '\0')
10620 {
10621 *bytes_read_ptr = 1;
10622 return NULL;
10623 }
fe1b8b76
JB
10624 *bytes_read_ptr = strlen ((char *) buf) + 1;
10625 return (char *) buf;
4bdf3d34
JJ
10626}
10627
10628static char *
cf2c3c16 10629read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10630{
be391dca 10631 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10632 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10633 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10634 bfd_get_filename (abfd));
dce234bc 10635 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10636 error (_("DW_FORM_strp pointing outside of "
10637 ".debug_str section [in module %s]"),
10638 bfd_get_filename (abfd));
4bdf3d34 10639 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10640 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10641 return NULL;
dce234bc 10642 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10643}
10644
cf2c3c16
TT
10645static char *
10646read_indirect_string (bfd *abfd, gdb_byte *buf,
10647 const struct comp_unit_head *cu_header,
10648 unsigned int *bytes_read_ptr)
10649{
10650 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10651
10652 return read_indirect_string_at_offset (abfd, str_offset);
10653}
10654
ce5d95e1 10655static unsigned long
fe1b8b76 10656read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10657{
ce5d95e1
JB
10658 unsigned long result;
10659 unsigned int num_read;
c906108c
SS
10660 int i, shift;
10661 unsigned char byte;
10662
10663 result = 0;
10664 shift = 0;
10665 num_read = 0;
10666 i = 0;
10667 while (1)
10668 {
fe1b8b76 10669 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10670 buf++;
10671 num_read++;
ce5d95e1 10672 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10673 if ((byte & 128) == 0)
10674 {
10675 break;
10676 }
10677 shift += 7;
10678 }
10679 *bytes_read_ptr = num_read;
10680 return result;
10681}
10682
ce5d95e1 10683static long
fe1b8b76 10684read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10685{
ce5d95e1 10686 long result;
77e0b926 10687 int i, shift, num_read;
c906108c
SS
10688 unsigned char byte;
10689
10690 result = 0;
10691 shift = 0;
c906108c
SS
10692 num_read = 0;
10693 i = 0;
10694 while (1)
10695 {
fe1b8b76 10696 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10697 buf++;
10698 num_read++;
ce5d95e1 10699 result |= ((long)(byte & 127) << shift);
c906108c
SS
10700 shift += 7;
10701 if ((byte & 128) == 0)
10702 {
10703 break;
10704 }
10705 }
77e0b926
DJ
10706 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10707 result |= -(((long)1) << shift);
c906108c
SS
10708 *bytes_read_ptr = num_read;
10709 return result;
10710}
10711
4bb7a0a7
DJ
10712/* Return a pointer to just past the end of an LEB128 number in BUF. */
10713
fe1b8b76
JB
10714static gdb_byte *
10715skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10716{
10717 int byte;
10718
10719 while (1)
10720 {
fe1b8b76 10721 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10722 buf++;
10723 if ((byte & 128) == 0)
10724 return buf;
10725 }
10726}
10727
c906108c 10728static void
e142c38c 10729set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10730{
10731 switch (lang)
10732 {
10733 case DW_LANG_C89:
76bee0cc 10734 case DW_LANG_C99:
c906108c 10735 case DW_LANG_C:
e142c38c 10736 cu->language = language_c;
c906108c
SS
10737 break;
10738 case DW_LANG_C_plus_plus:
e142c38c 10739 cu->language = language_cplus;
c906108c 10740 break;
6aecb9c2
JB
10741 case DW_LANG_D:
10742 cu->language = language_d;
10743 break;
c906108c
SS
10744 case DW_LANG_Fortran77:
10745 case DW_LANG_Fortran90:
b21b22e0 10746 case DW_LANG_Fortran95:
e142c38c 10747 cu->language = language_fortran;
c906108c
SS
10748 break;
10749 case DW_LANG_Mips_Assembler:
e142c38c 10750 cu->language = language_asm;
c906108c 10751 break;
bebd888e 10752 case DW_LANG_Java:
e142c38c 10753 cu->language = language_java;
bebd888e 10754 break;
c906108c 10755 case DW_LANG_Ada83:
8aaf0b47 10756 case DW_LANG_Ada95:
bc5f45f8
JB
10757 cu->language = language_ada;
10758 break;
72019c9c
GM
10759 case DW_LANG_Modula2:
10760 cu->language = language_m2;
10761 break;
fe8e67fd
PM
10762 case DW_LANG_Pascal83:
10763 cu->language = language_pascal;
10764 break;
22566fbd
DJ
10765 case DW_LANG_ObjC:
10766 cu->language = language_objc;
10767 break;
c906108c
SS
10768 case DW_LANG_Cobol74:
10769 case DW_LANG_Cobol85:
c906108c 10770 default:
e142c38c 10771 cu->language = language_minimal;
c906108c
SS
10772 break;
10773 }
e142c38c 10774 cu->language_defn = language_def (cu->language);
c906108c
SS
10775}
10776
10777/* Return the named attribute or NULL if not there. */
10778
10779static struct attribute *
e142c38c 10780dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 10781{
a48e046c 10782 for (;;)
c906108c 10783 {
a48e046c
TT
10784 unsigned int i;
10785 struct attribute *spec = NULL;
10786
10787 for (i = 0; i < die->num_attrs; ++i)
10788 {
10789 if (die->attrs[i].name == name)
10790 return &die->attrs[i];
10791 if (die->attrs[i].name == DW_AT_specification
10792 || die->attrs[i].name == DW_AT_abstract_origin)
10793 spec = &die->attrs[i];
10794 }
10795
10796 if (!spec)
10797 break;
c906108c 10798
f2f0e013 10799 die = follow_die_ref (die, spec, &cu);
f2f0e013 10800 }
c5aa993b 10801
c906108c
SS
10802 return NULL;
10803}
10804
348e048f
DE
10805/* Return the named attribute or NULL if not there,
10806 but do not follow DW_AT_specification, etc.
10807 This is for use in contexts where we're reading .debug_types dies.
10808 Following DW_AT_specification, DW_AT_abstract_origin will take us
10809 back up the chain, and we want to go down. */
10810
10811static struct attribute *
10812dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10813 struct dwarf2_cu *cu)
10814{
10815 unsigned int i;
10816
10817 for (i = 0; i < die->num_attrs; ++i)
10818 if (die->attrs[i].name == name)
10819 return &die->attrs[i];
10820
10821 return NULL;
10822}
10823
05cf31d1
JB
10824/* Return non-zero iff the attribute NAME is defined for the given DIE,
10825 and holds a non-zero value. This function should only be used for
2dc7f7b3 10826 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10827
10828static int
10829dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10830{
10831 struct attribute *attr = dwarf2_attr (die, name, cu);
10832
10833 return (attr && DW_UNSND (attr));
10834}
10835
3ca72b44 10836static int
e142c38c 10837die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10838{
05cf31d1
JB
10839 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10840 which value is non-zero. However, we have to be careful with
10841 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10842 (via dwarf2_flag_true_p) follows this attribute. So we may
10843 end up accidently finding a declaration attribute that belongs
10844 to a different DIE referenced by the specification attribute,
10845 even though the given DIE does not have a declaration attribute. */
10846 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10847 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10848}
10849
63d06c5c 10850/* Return the die giving the specification for DIE, if there is
f2f0e013 10851 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10852 containing the return value on output. If there is no
10853 specification, but there is an abstract origin, that is
10854 returned. */
63d06c5c
DC
10855
10856static struct die_info *
f2f0e013 10857die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10858{
f2f0e013
DJ
10859 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10860 *spec_cu);
63d06c5c 10861
edb3359d
DJ
10862 if (spec_attr == NULL)
10863 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10864
63d06c5c
DC
10865 if (spec_attr == NULL)
10866 return NULL;
10867 else
f2f0e013 10868 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10869}
c906108c 10870
debd256d 10871/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10872 refers to.
10873 NOTE: This is also used as a "cleanup" function. */
10874
debd256d
JB
10875static void
10876free_line_header (struct line_header *lh)
10877{
10878 if (lh->standard_opcode_lengths)
a8bc7b56 10879 xfree (lh->standard_opcode_lengths);
debd256d
JB
10880
10881 /* Remember that all the lh->file_names[i].name pointers are
10882 pointers into debug_line_buffer, and don't need to be freed. */
10883 if (lh->file_names)
a8bc7b56 10884 xfree (lh->file_names);
debd256d
JB
10885
10886 /* Similarly for the include directory names. */
10887 if (lh->include_dirs)
a8bc7b56 10888 xfree (lh->include_dirs);
debd256d 10889
a8bc7b56 10890 xfree (lh);
debd256d
JB
10891}
10892
debd256d 10893/* Add an entry to LH's include directory table. */
ae2de4f8 10894
debd256d
JB
10895static void
10896add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10897{
debd256d
JB
10898 /* Grow the array if necessary. */
10899 if (lh->include_dirs_size == 0)
c5aa993b 10900 {
debd256d
JB
10901 lh->include_dirs_size = 1; /* for testing */
10902 lh->include_dirs = xmalloc (lh->include_dirs_size
10903 * sizeof (*lh->include_dirs));
10904 }
10905 else if (lh->num_include_dirs >= lh->include_dirs_size)
10906 {
10907 lh->include_dirs_size *= 2;
10908 lh->include_dirs = xrealloc (lh->include_dirs,
10909 (lh->include_dirs_size
10910 * sizeof (*lh->include_dirs)));
c5aa993b 10911 }
c906108c 10912
debd256d
JB
10913 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10914}
6e70227d 10915
debd256d 10916/* Add an entry to LH's file name table. */
ae2de4f8 10917
debd256d
JB
10918static void
10919add_file_name (struct line_header *lh,
10920 char *name,
10921 unsigned int dir_index,
10922 unsigned int mod_time,
10923 unsigned int length)
10924{
10925 struct file_entry *fe;
10926
10927 /* Grow the array if necessary. */
10928 if (lh->file_names_size == 0)
10929 {
10930 lh->file_names_size = 1; /* for testing */
10931 lh->file_names = xmalloc (lh->file_names_size
10932 * sizeof (*lh->file_names));
10933 }
10934 else if (lh->num_file_names >= lh->file_names_size)
10935 {
10936 lh->file_names_size *= 2;
10937 lh->file_names = xrealloc (lh->file_names,
10938 (lh->file_names_size
10939 * sizeof (*lh->file_names)));
10940 }
10941
10942 fe = &lh->file_names[lh->num_file_names++];
10943 fe->name = name;
10944 fe->dir_index = dir_index;
10945 fe->mod_time = mod_time;
10946 fe->length = length;
aaa75496 10947 fe->included_p = 0;
cb1df416 10948 fe->symtab = NULL;
debd256d 10949}
6e70227d 10950
debd256d 10951/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10952 .debug_line, according to the endianness of ABFD. Return a pointer
10953 to a struct line_header, allocated using xmalloc.
debd256d
JB
10954
10955 NOTE: the strings in the include directory and file name tables of
10956 the returned object point into debug_line_buffer, and must not be
10957 freed. */
ae2de4f8 10958
debd256d
JB
10959static struct line_header *
10960dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10961 struct dwarf2_cu *cu)
debd256d
JB
10962{
10963 struct cleanup *back_to;
10964 struct line_header *lh;
fe1b8b76 10965 gdb_byte *line_ptr;
c764a876 10966 unsigned int bytes_read, offset_size;
debd256d
JB
10967 int i;
10968 char *cur_dir, *cur_file;
10969
be391dca 10970 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10971 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10972 {
e2e0b3e5 10973 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10974 return 0;
10975 }
10976
a738430d
MK
10977 /* Make sure that at least there's room for the total_length field.
10978 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10979 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10980 {
4d3c2250 10981 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10982 return 0;
10983 }
10984
10985 lh = xmalloc (sizeof (*lh));
10986 memset (lh, 0, sizeof (*lh));
10987 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10988 (void *) lh);
10989
dce234bc 10990 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10991
a738430d 10992 /* Read in the header. */
6e70227d 10993 lh->total_length =
c764a876
DE
10994 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10995 &bytes_read, &offset_size);
debd256d 10996 line_ptr += bytes_read;
dce234bc
PP
10997 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10998 + dwarf2_per_objfile->line.size))
debd256d 10999 {
4d3c2250 11000 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
11001 return 0;
11002 }
11003 lh->statement_program_end = line_ptr + lh->total_length;
11004 lh->version = read_2_bytes (abfd, line_ptr);
11005 line_ptr += 2;
c764a876
DE
11006 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11007 line_ptr += offset_size;
debd256d
JB
11008 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11009 line_ptr += 1;
2dc7f7b3
TT
11010 if (lh->version >= 4)
11011 {
11012 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11013 line_ptr += 1;
11014 }
11015 else
11016 lh->maximum_ops_per_instruction = 1;
11017
11018 if (lh->maximum_ops_per_instruction == 0)
11019 {
11020 lh->maximum_ops_per_instruction = 1;
11021 complaint (&symfile_complaints,
3e43a32a
MS
11022 _("invalid maximum_ops_per_instruction "
11023 "in `.debug_line' section"));
2dc7f7b3
TT
11024 }
11025
debd256d
JB
11026 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11027 line_ptr += 1;
11028 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11029 line_ptr += 1;
11030 lh->line_range = read_1_byte (abfd, line_ptr);
11031 line_ptr += 1;
11032 lh->opcode_base = read_1_byte (abfd, line_ptr);
11033 line_ptr += 1;
11034 lh->standard_opcode_lengths
fe1b8b76 11035 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11036
11037 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11038 for (i = 1; i < lh->opcode_base; ++i)
11039 {
11040 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11041 line_ptr += 1;
11042 }
11043
a738430d 11044 /* Read directory table. */
9b1c24c8 11045 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11046 {
11047 line_ptr += bytes_read;
11048 add_include_dir (lh, cur_dir);
11049 }
11050 line_ptr += bytes_read;
11051
a738430d 11052 /* Read file name table. */
9b1c24c8 11053 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11054 {
11055 unsigned int dir_index, mod_time, length;
11056
11057 line_ptr += bytes_read;
11058 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11059 line_ptr += bytes_read;
11060 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11061 line_ptr += bytes_read;
11062 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11063 line_ptr += bytes_read;
11064
11065 add_file_name (lh, cur_file, dir_index, mod_time, length);
11066 }
11067 line_ptr += bytes_read;
6e70227d 11068 lh->statement_program_start = line_ptr;
debd256d 11069
dce234bc
PP
11070 if (line_ptr > (dwarf2_per_objfile->line.buffer
11071 + dwarf2_per_objfile->line.size))
4d3c2250 11072 complaint (&symfile_complaints,
3e43a32a
MS
11073 _("line number info header doesn't "
11074 "fit in `.debug_line' section"));
debd256d
JB
11075
11076 discard_cleanups (back_to);
11077 return lh;
11078}
c906108c 11079
c6da4cef
DE
11080/* Subroutine of dwarf_decode_lines to simplify it.
11081 Return the file name of the psymtab for included file FILE_INDEX
11082 in line header LH of PST.
11083 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11084 If space for the result is malloc'd, it will be freed by a cleanup.
11085 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11086
11087static char *
11088psymtab_include_file_name (const struct line_header *lh, int file_index,
11089 const struct partial_symtab *pst,
11090 const char *comp_dir)
11091{
11092 const struct file_entry fe = lh->file_names [file_index];
11093 char *include_name = fe.name;
11094 char *include_name_to_compare = include_name;
11095 char *dir_name = NULL;
72b9f47f
TT
11096 const char *pst_filename;
11097 char *copied_name = NULL;
c6da4cef
DE
11098 int file_is_pst;
11099
11100 if (fe.dir_index)
11101 dir_name = lh->include_dirs[fe.dir_index - 1];
11102
11103 if (!IS_ABSOLUTE_PATH (include_name)
11104 && (dir_name != NULL || comp_dir != NULL))
11105 {
11106 /* Avoid creating a duplicate psymtab for PST.
11107 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11108 Before we do the comparison, however, we need to account
11109 for DIR_NAME and COMP_DIR.
11110 First prepend dir_name (if non-NULL). If we still don't
11111 have an absolute path prepend comp_dir (if non-NULL).
11112 However, the directory we record in the include-file's
11113 psymtab does not contain COMP_DIR (to match the
11114 corresponding symtab(s)).
11115
11116 Example:
11117
11118 bash$ cd /tmp
11119 bash$ gcc -g ./hello.c
11120 include_name = "hello.c"
11121 dir_name = "."
11122 DW_AT_comp_dir = comp_dir = "/tmp"
11123 DW_AT_name = "./hello.c" */
11124
11125 if (dir_name != NULL)
11126 {
11127 include_name = concat (dir_name, SLASH_STRING,
11128 include_name, (char *)NULL);
11129 include_name_to_compare = include_name;
11130 make_cleanup (xfree, include_name);
11131 }
11132 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11133 {
11134 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11135 include_name, (char *)NULL);
11136 }
11137 }
11138
11139 pst_filename = pst->filename;
11140 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11141 {
72b9f47f
TT
11142 copied_name = concat (pst->dirname, SLASH_STRING,
11143 pst_filename, (char *)NULL);
11144 pst_filename = copied_name;
c6da4cef
DE
11145 }
11146
1e3fad37 11147 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11148
11149 if (include_name_to_compare != include_name)
11150 xfree (include_name_to_compare);
72b9f47f
TT
11151 if (copied_name != NULL)
11152 xfree (copied_name);
c6da4cef
DE
11153
11154 if (file_is_pst)
11155 return NULL;
11156 return include_name;
11157}
11158
c91513d8
PP
11159/* Ignore this record_line request. */
11160
11161static void
11162noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11163{
11164 return;
11165}
11166
f3f5162e
DE
11167/* Subroutine of dwarf_decode_lines to simplify it.
11168 Process the line number information in LH. */
debd256d 11169
c906108c 11170static void
f3f5162e
DE
11171dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11172 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11173{
a8c50c1f 11174 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11175 gdb_byte *line_end;
a8c50c1f 11176 unsigned int bytes_read, extended_len;
c906108c 11177 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11178 CORE_ADDR baseaddr;
11179 struct objfile *objfile = cu->objfile;
f3f5162e 11180 bfd *abfd = objfile->obfd;
fbf65064 11181 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11182 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11183 struct subfile *last_subfile = NULL;
c91513d8
PP
11184 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11185 = record_line;
e142c38c
DJ
11186
11187 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11188
debd256d
JB
11189 line_ptr = lh->statement_program_start;
11190 line_end = lh->statement_program_end;
c906108c
SS
11191
11192 /* Read the statement sequences until there's nothing left. */
11193 while (line_ptr < line_end)
11194 {
11195 /* state machine registers */
11196 CORE_ADDR address = 0;
11197 unsigned int file = 1;
11198 unsigned int line = 1;
11199 unsigned int column = 0;
debd256d 11200 int is_stmt = lh->default_is_stmt;
c906108c
SS
11201 int basic_block = 0;
11202 int end_sequence = 0;
fbf65064 11203 CORE_ADDR addr;
2dc7f7b3 11204 unsigned char op_index = 0;
c906108c 11205
aaa75496 11206 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11207 {
aaa75496 11208 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11209 /* lh->include_dirs and lh->file_names are 0-based, but the
11210 directory and file name numbers in the statement program
11211 are 1-based. */
11212 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11213 char *dir = NULL;
a738430d 11214
debd256d
JB
11215 if (fe->dir_index)
11216 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11217
11218 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11219 }
11220
a738430d 11221 /* Decode the table. */
c5aa993b 11222 while (!end_sequence)
c906108c
SS
11223 {
11224 op_code = read_1_byte (abfd, line_ptr);
11225 line_ptr += 1;
59205f5a
JB
11226 if (line_ptr > line_end)
11227 {
11228 dwarf2_debug_line_missing_end_sequence_complaint ();
11229 break;
11230 }
9aa1fe7e 11231
debd256d 11232 if (op_code >= lh->opcode_base)
6e70227d 11233 {
a738430d 11234 /* Special operand. */
debd256d 11235 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11236 address += (((op_index + (adj_opcode / lh->line_range))
11237 / lh->maximum_ops_per_instruction)
11238 * lh->minimum_instruction_length);
11239 op_index = ((op_index + (adj_opcode / lh->line_range))
11240 % lh->maximum_ops_per_instruction);
debd256d 11241 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11242 if (lh->num_file_names < file || file == 0)
25e43795 11243 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11244 /* For now we ignore lines not starting on an
11245 instruction boundary. */
11246 else if (op_index == 0)
25e43795
DJ
11247 {
11248 lh->file_names[file - 1].included_p = 1;
ca5f395d 11249 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11250 {
11251 if (last_subfile != current_subfile)
11252 {
11253 addr = gdbarch_addr_bits_remove (gdbarch, address);
11254 if (last_subfile)
c91513d8 11255 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11256 last_subfile = current_subfile;
11257 }
25e43795 11258 /* Append row to matrix using current values. */
7019d805 11259 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11260 (*p_record_line) (current_subfile, line, addr);
366da635 11261 }
25e43795 11262 }
ca5f395d 11263 basic_block = 0;
9aa1fe7e
GK
11264 }
11265 else switch (op_code)
c906108c
SS
11266 {
11267 case DW_LNS_extended_op:
3e43a32a
MS
11268 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11269 &bytes_read);
473b7be6 11270 line_ptr += bytes_read;
a8c50c1f 11271 extended_end = line_ptr + extended_len;
c906108c
SS
11272 extended_op = read_1_byte (abfd, line_ptr);
11273 line_ptr += 1;
11274 switch (extended_op)
11275 {
11276 case DW_LNE_end_sequence:
c91513d8 11277 p_record_line = record_line;
c906108c 11278 end_sequence = 1;
c906108c
SS
11279 break;
11280 case DW_LNE_set_address:
e7c27a73 11281 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11282
11283 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11284 {
11285 /* This line table is for a function which has been
11286 GCd by the linker. Ignore it. PR gdb/12528 */
11287
11288 long line_offset
11289 = line_ptr - dwarf2_per_objfile->line.buffer;
11290
11291 complaint (&symfile_complaints,
11292 _(".debug_line address at offset 0x%lx is 0 "
11293 "[in module %s]"),
bb5ed363 11294 line_offset, objfile->name);
c91513d8
PP
11295 p_record_line = noop_record_line;
11296 }
11297
2dc7f7b3 11298 op_index = 0;
107d2387
AC
11299 line_ptr += bytes_read;
11300 address += baseaddr;
c906108c
SS
11301 break;
11302 case DW_LNE_define_file:
debd256d
JB
11303 {
11304 char *cur_file;
11305 unsigned int dir_index, mod_time, length;
6e70227d 11306
3e43a32a
MS
11307 cur_file = read_direct_string (abfd, line_ptr,
11308 &bytes_read);
debd256d
JB
11309 line_ptr += bytes_read;
11310 dir_index =
11311 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11312 line_ptr += bytes_read;
11313 mod_time =
11314 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11315 line_ptr += bytes_read;
11316 length =
11317 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11318 line_ptr += bytes_read;
11319 add_file_name (lh, cur_file, dir_index, mod_time, length);
11320 }
c906108c 11321 break;
d0c6ba3d
CC
11322 case DW_LNE_set_discriminator:
11323 /* The discriminator is not interesting to the debugger;
11324 just ignore it. */
11325 line_ptr = extended_end;
11326 break;
c906108c 11327 default:
4d3c2250 11328 complaint (&symfile_complaints,
e2e0b3e5 11329 _("mangled .debug_line section"));
debd256d 11330 return;
c906108c 11331 }
a8c50c1f
DJ
11332 /* Make sure that we parsed the extended op correctly. If e.g.
11333 we expected a different address size than the producer used,
11334 we may have read the wrong number of bytes. */
11335 if (line_ptr != extended_end)
11336 {
11337 complaint (&symfile_complaints,
11338 _("mangled .debug_line section"));
11339 return;
11340 }
c906108c
SS
11341 break;
11342 case DW_LNS_copy:
59205f5a 11343 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11344 dwarf2_debug_line_missing_file_complaint ();
11345 else
366da635 11346 {
25e43795 11347 lh->file_names[file - 1].included_p = 1;
ca5f395d 11348 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11349 {
11350 if (last_subfile != current_subfile)
11351 {
11352 addr = gdbarch_addr_bits_remove (gdbarch, address);
11353 if (last_subfile)
c91513d8 11354 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11355 last_subfile = current_subfile;
11356 }
7019d805 11357 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11358 (*p_record_line) (current_subfile, line, addr);
fbf65064 11359 }
366da635 11360 }
c906108c
SS
11361 basic_block = 0;
11362 break;
11363 case DW_LNS_advance_pc:
2dc7f7b3
TT
11364 {
11365 CORE_ADDR adjust
11366 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11367
11368 address += (((op_index + adjust)
11369 / lh->maximum_ops_per_instruction)
11370 * lh->minimum_instruction_length);
11371 op_index = ((op_index + adjust)
11372 % lh->maximum_ops_per_instruction);
11373 line_ptr += bytes_read;
11374 }
c906108c
SS
11375 break;
11376 case DW_LNS_advance_line:
11377 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11378 line_ptr += bytes_read;
11379 break;
11380 case DW_LNS_set_file:
debd256d 11381 {
a738430d
MK
11382 /* The arrays lh->include_dirs and lh->file_names are
11383 0-based, but the directory and file name numbers in
11384 the statement program are 1-based. */
debd256d 11385 struct file_entry *fe;
4f1520fb 11386 char *dir = NULL;
a738430d 11387
debd256d
JB
11388 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11389 line_ptr += bytes_read;
59205f5a 11390 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11391 dwarf2_debug_line_missing_file_complaint ();
11392 else
11393 {
11394 fe = &lh->file_names[file - 1];
11395 if (fe->dir_index)
11396 dir = lh->include_dirs[fe->dir_index - 1];
11397 if (!decode_for_pst_p)
11398 {
11399 last_subfile = current_subfile;
11400 dwarf2_start_subfile (fe->name, dir, comp_dir);
11401 }
11402 }
debd256d 11403 }
c906108c
SS
11404 break;
11405 case DW_LNS_set_column:
11406 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11407 line_ptr += bytes_read;
11408 break;
11409 case DW_LNS_negate_stmt:
11410 is_stmt = (!is_stmt);
11411 break;
11412 case DW_LNS_set_basic_block:
11413 basic_block = 1;
11414 break;
c2c6d25f
JM
11415 /* Add to the address register of the state machine the
11416 address increment value corresponding to special opcode
a738430d
MK
11417 255. I.e., this value is scaled by the minimum
11418 instruction length since special opcode 255 would have
b021a221 11419 scaled the increment. */
c906108c 11420 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11421 {
11422 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11423
11424 address += (((op_index + adjust)
11425 / lh->maximum_ops_per_instruction)
11426 * lh->minimum_instruction_length);
11427 op_index = ((op_index + adjust)
11428 % lh->maximum_ops_per_instruction);
11429 }
c906108c
SS
11430 break;
11431 case DW_LNS_fixed_advance_pc:
11432 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11433 op_index = 0;
c906108c
SS
11434 line_ptr += 2;
11435 break;
9aa1fe7e 11436 default:
a738430d
MK
11437 {
11438 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11439 int i;
a738430d 11440
debd256d 11441 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11442 {
11443 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11444 line_ptr += bytes_read;
11445 }
11446 }
c906108c
SS
11447 }
11448 }
59205f5a
JB
11449 if (lh->num_file_names < file || file == 0)
11450 dwarf2_debug_line_missing_file_complaint ();
11451 else
11452 {
11453 lh->file_names[file - 1].included_p = 1;
11454 if (!decode_for_pst_p)
fbf65064
UW
11455 {
11456 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11457 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11458 }
59205f5a 11459 }
c906108c 11460 }
f3f5162e
DE
11461}
11462
11463/* Decode the Line Number Program (LNP) for the given line_header
11464 structure and CU. The actual information extracted and the type
11465 of structures created from the LNP depends on the value of PST.
11466
11467 1. If PST is NULL, then this procedure uses the data from the program
11468 to create all necessary symbol tables, and their linetables.
11469
11470 2. If PST is not NULL, this procedure reads the program to determine
11471 the list of files included by the unit represented by PST, and
11472 builds all the associated partial symbol tables.
11473
11474 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11475 It is used for relative paths in the line table.
11476 NOTE: When processing partial symtabs (pst != NULL),
11477 comp_dir == pst->dirname.
11478
11479 NOTE: It is important that psymtabs have the same file name (via strcmp)
11480 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11481 symtab we don't use it in the name of the psymtabs we create.
11482 E.g. expand_line_sal requires this when finding psymtabs to expand.
11483 A good testcase for this is mb-inline.exp. */
11484
11485static void
11486dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11487 struct dwarf2_cu *cu, struct partial_symtab *pst,
11488 int want_line_info)
11489{
11490 struct objfile *objfile = cu->objfile;
11491 const int decode_for_pst_p = (pst != NULL);
11492 struct subfile *first_subfile = current_subfile;
11493
11494 if (want_line_info)
11495 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11496
11497 if (decode_for_pst_p)
11498 {
11499 int file_index;
11500
11501 /* Now that we're done scanning the Line Header Program, we can
11502 create the psymtab of each included file. */
11503 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11504 if (lh->file_names[file_index].included_p == 1)
11505 {
c6da4cef
DE
11506 char *include_name =
11507 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11508 if (include_name != NULL)
aaa75496
JB
11509 dwarf2_create_include_psymtab (include_name, pst, objfile);
11510 }
11511 }
cb1df416
DJ
11512 else
11513 {
11514 /* Make sure a symtab is created for every file, even files
11515 which contain only variables (i.e. no code with associated
11516 line numbers). */
cb1df416 11517 int i;
cb1df416
DJ
11518
11519 for (i = 0; i < lh->num_file_names; i++)
11520 {
11521 char *dir = NULL;
f3f5162e 11522 struct file_entry *fe;
9a619af0 11523
cb1df416
DJ
11524 fe = &lh->file_names[i];
11525 if (fe->dir_index)
11526 dir = lh->include_dirs[fe->dir_index - 1];
11527 dwarf2_start_subfile (fe->name, dir, comp_dir);
11528
11529 /* Skip the main file; we don't need it, and it must be
11530 allocated last, so that it will show up before the
11531 non-primary symtabs in the objfile's symtab list. */
11532 if (current_subfile == first_subfile)
11533 continue;
11534
11535 if (current_subfile->symtab == NULL)
11536 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11537 objfile);
cb1df416
DJ
11538 fe->symtab = current_subfile->symtab;
11539 }
11540 }
c906108c
SS
11541}
11542
11543/* Start a subfile for DWARF. FILENAME is the name of the file and
11544 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11545 or NULL if not known. COMP_DIR is the compilation directory for the
11546 linetable's compilation unit or NULL if not known.
c906108c
SS
11547 This routine tries to keep line numbers from identical absolute and
11548 relative file names in a common subfile.
11549
11550 Using the `list' example from the GDB testsuite, which resides in
11551 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11552 of /srcdir/list0.c yields the following debugging information for list0.c:
11553
c5aa993b
JM
11554 DW_AT_name: /srcdir/list0.c
11555 DW_AT_comp_dir: /compdir
357e46e7 11556 files.files[0].name: list0.h
c5aa993b 11557 files.files[0].dir: /srcdir
357e46e7 11558 files.files[1].name: list0.c
c5aa993b 11559 files.files[1].dir: /srcdir
c906108c
SS
11560
11561 The line number information for list0.c has to end up in a single
4f1520fb
FR
11562 subfile, so that `break /srcdir/list0.c:1' works as expected.
11563 start_subfile will ensure that this happens provided that we pass the
11564 concatenation of files.files[1].dir and files.files[1].name as the
11565 subfile's name. */
c906108c
SS
11566
11567static void
3e43a32a
MS
11568dwarf2_start_subfile (char *filename, const char *dirname,
11569 const char *comp_dir)
c906108c 11570{
4f1520fb
FR
11571 char *fullname;
11572
11573 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11574 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11575 second argument to start_subfile. To be consistent, we do the
11576 same here. In order not to lose the line information directory,
11577 we concatenate it to the filename when it makes sense.
11578 Note that the Dwarf3 standard says (speaking of filenames in line
11579 information): ``The directory index is ignored for file names
11580 that represent full path names''. Thus ignoring dirname in the
11581 `else' branch below isn't an issue. */
c906108c 11582
d5166ae1 11583 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11584 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11585 else
11586 fullname = filename;
c906108c 11587
4f1520fb
FR
11588 start_subfile (fullname, comp_dir);
11589
11590 if (fullname != filename)
11591 xfree (fullname);
c906108c
SS
11592}
11593
4c2df51b
DJ
11594static void
11595var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11596 struct dwarf2_cu *cu)
4c2df51b 11597{
e7c27a73
DJ
11598 struct objfile *objfile = cu->objfile;
11599 struct comp_unit_head *cu_header = &cu->header;
11600
4c2df51b
DJ
11601 /* NOTE drow/2003-01-30: There used to be a comment and some special
11602 code here to turn a symbol with DW_AT_external and a
11603 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11604 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11605 with some versions of binutils) where shared libraries could have
11606 relocations against symbols in their debug information - the
11607 minimal symbol would have the right address, but the debug info
11608 would not. It's no longer necessary, because we will explicitly
11609 apply relocations when we read in the debug information now. */
11610
11611 /* A DW_AT_location attribute with no contents indicates that a
11612 variable has been optimized away. */
11613 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11614 {
11615 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11616 return;
11617 }
11618
11619 /* Handle one degenerate form of location expression specially, to
11620 preserve GDB's previous behavior when section offsets are
11621 specified. If this is just a DW_OP_addr then mark this symbol
11622 as LOC_STATIC. */
11623
11624 if (attr_form_is_block (attr)
11625 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11626 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11627 {
891d2f0b 11628 unsigned int dummy;
4c2df51b
DJ
11629
11630 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11631 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11632 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11633 fixup_symbol_section (sym, objfile);
11634 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11635 SYMBOL_SECTION (sym));
4c2df51b
DJ
11636 return;
11637 }
11638
11639 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11640 expression evaluator, and use LOC_COMPUTED only when necessary
11641 (i.e. when the value of a register or memory location is
11642 referenced, or a thread-local block, etc.). Then again, it might
11643 not be worthwhile. I'm assuming that it isn't unless performance
11644 or memory numbers show me otherwise. */
11645
e7c27a73 11646 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11647 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11648
11649 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11650 cu->has_loclist = 1;
4c2df51b
DJ
11651}
11652
c906108c
SS
11653/* Given a pointer to a DWARF information entry, figure out if we need
11654 to make a symbol table entry for it, and if so, create a new entry
11655 and return a pointer to it.
11656 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11657 used the passed type.
11658 If SPACE is not NULL, use it to hold the new symbol. If it is
11659 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11660
11661static struct symbol *
34eaf542
TT
11662new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11663 struct symbol *space)
c906108c 11664{
e7c27a73 11665 struct objfile *objfile = cu->objfile;
c906108c
SS
11666 struct symbol *sym = NULL;
11667 char *name;
11668 struct attribute *attr = NULL;
11669 struct attribute *attr2 = NULL;
e142c38c 11670 CORE_ADDR baseaddr;
e37fd15a
SW
11671 struct pending **list_to_add = NULL;
11672
edb3359d 11673 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11674
11675 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11676
94af9270 11677 name = dwarf2_name (die, cu);
c906108c
SS
11678 if (name)
11679 {
94af9270 11680 const char *linkagename;
34eaf542 11681 int suppress_add = 0;
94af9270 11682
34eaf542
TT
11683 if (space)
11684 sym = space;
11685 else
11686 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11687 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11688
11689 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11690 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11691 linkagename = dwarf2_physname (name, die, cu);
11692 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11693
f55ee35c
JK
11694 /* Fortran does not have mangling standard and the mangling does differ
11695 between gfortran, iFort etc. */
11696 if (cu->language == language_fortran
b250c185 11697 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11698 symbol_set_demangled_name (&(sym->ginfo),
11699 (char *) dwarf2_full_name (name, die, cu),
11700 NULL);
f55ee35c 11701
c906108c 11702 /* Default assumptions.
c5aa993b 11703 Use the passed type or decode it from the die. */
176620f1 11704 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11705 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11706 if (type != NULL)
11707 SYMBOL_TYPE (sym) = type;
11708 else
e7c27a73 11709 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11710 attr = dwarf2_attr (die,
11711 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11712 cu);
c906108c
SS
11713 if (attr)
11714 {
11715 SYMBOL_LINE (sym) = DW_UNSND (attr);
11716 }
cb1df416 11717
edb3359d
DJ
11718 attr = dwarf2_attr (die,
11719 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11720 cu);
cb1df416
DJ
11721 if (attr)
11722 {
11723 int file_index = DW_UNSND (attr);
9a619af0 11724
cb1df416
DJ
11725 if (cu->line_header == NULL
11726 || file_index > cu->line_header->num_file_names)
11727 complaint (&symfile_complaints,
11728 _("file index out of range"));
1c3d648d 11729 else if (file_index > 0)
cb1df416
DJ
11730 {
11731 struct file_entry *fe;
9a619af0 11732
cb1df416
DJ
11733 fe = &cu->line_header->file_names[file_index - 1];
11734 SYMBOL_SYMTAB (sym) = fe->symtab;
11735 }
11736 }
11737
c906108c
SS
11738 switch (die->tag)
11739 {
11740 case DW_TAG_label:
e142c38c 11741 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11742 if (attr)
11743 {
11744 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11745 }
0f5238ed
TT
11746 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11747 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11748 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11749 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11750 break;
11751 case DW_TAG_subprogram:
11752 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11753 finish_block. */
11754 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11755 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11756 if ((attr2 && (DW_UNSND (attr2) != 0))
11757 || cu->language == language_ada)
c906108c 11758 {
2cfa0c8d
JB
11759 /* Subprograms marked external are stored as a global symbol.
11760 Ada subprograms, whether marked external or not, are always
11761 stored as a global symbol, because we want to be able to
11762 access them globally. For instance, we want to be able
11763 to break on a nested subprogram without having to
11764 specify the context. */
e37fd15a 11765 list_to_add = &global_symbols;
c906108c
SS
11766 }
11767 else
11768 {
e37fd15a 11769 list_to_add = cu->list_in_scope;
c906108c
SS
11770 }
11771 break;
edb3359d
DJ
11772 case DW_TAG_inlined_subroutine:
11773 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11774 finish_block. */
11775 SYMBOL_CLASS (sym) = LOC_BLOCK;
11776 SYMBOL_INLINED (sym) = 1;
481860b3 11777 list_to_add = cu->list_in_scope;
edb3359d 11778 break;
34eaf542
TT
11779 case DW_TAG_template_value_param:
11780 suppress_add = 1;
11781 /* Fall through. */
72929c62 11782 case DW_TAG_constant:
c906108c 11783 case DW_TAG_variable:
254e6b9e 11784 case DW_TAG_member:
0963b4bd
MS
11785 /* Compilation with minimal debug info may result in
11786 variables with missing type entries. Change the
11787 misleading `void' type to something sensible. */
c906108c 11788 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11789 SYMBOL_TYPE (sym)
46bf5051 11790 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11791
e142c38c 11792 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11793 /* In the case of DW_TAG_member, we should only be called for
11794 static const members. */
11795 if (die->tag == DW_TAG_member)
11796 {
3863f96c
DE
11797 /* dwarf2_add_field uses die_is_declaration,
11798 so we do the same. */
254e6b9e
DE
11799 gdb_assert (die_is_declaration (die, cu));
11800 gdb_assert (attr);
11801 }
c906108c
SS
11802 if (attr)
11803 {
e7c27a73 11804 dwarf2_const_value (attr, sym, cu);
e142c38c 11805 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11806 if (!suppress_add)
34eaf542
TT
11807 {
11808 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11809 list_to_add = &global_symbols;
34eaf542 11810 else
e37fd15a 11811 list_to_add = cu->list_in_scope;
34eaf542 11812 }
c906108c
SS
11813 break;
11814 }
e142c38c 11815 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11816 if (attr)
11817 {
e7c27a73 11818 var_decode_location (attr, sym, cu);
e142c38c 11819 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11820 if (SYMBOL_CLASS (sym) == LOC_STATIC
11821 && SYMBOL_VALUE_ADDRESS (sym) == 0
11822 && !dwarf2_per_objfile->has_section_at_zero)
11823 {
11824 /* When a static variable is eliminated by the linker,
11825 the corresponding debug information is not stripped
11826 out, but the variable address is set to null;
11827 do not add such variables into symbol table. */
11828 }
11829 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11830 {
f55ee35c
JK
11831 /* Workaround gfortran PR debug/40040 - it uses
11832 DW_AT_location for variables in -fPIC libraries which may
11833 get overriden by other libraries/executable and get
11834 a different address. Resolve it by the minimal symbol
11835 which may come from inferior's executable using copy
11836 relocation. Make this workaround only for gfortran as for
11837 other compilers GDB cannot guess the minimal symbol
11838 Fortran mangling kind. */
11839 if (cu->language == language_fortran && die->parent
11840 && die->parent->tag == DW_TAG_module
11841 && cu->producer
11842 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11843 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11844
1c809c68
TT
11845 /* A variable with DW_AT_external is never static,
11846 but it may be block-scoped. */
11847 list_to_add = (cu->list_in_scope == &file_symbols
11848 ? &global_symbols : cu->list_in_scope);
1c809c68 11849 }
c906108c 11850 else
e37fd15a 11851 list_to_add = cu->list_in_scope;
c906108c
SS
11852 }
11853 else
11854 {
11855 /* We do not know the address of this symbol.
c5aa993b
JM
11856 If it is an external symbol and we have type information
11857 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11858 The address of the variable will then be determined from
11859 the minimal symbol table whenever the variable is
11860 referenced. */
e142c38c 11861 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11862 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11863 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11864 {
0fe7935b
DJ
11865 /* A variable with DW_AT_external is never static, but it
11866 may be block-scoped. */
11867 list_to_add = (cu->list_in_scope == &file_symbols
11868 ? &global_symbols : cu->list_in_scope);
11869
c906108c 11870 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11871 }
442ddf59
JK
11872 else if (!die_is_declaration (die, cu))
11873 {
11874 /* Use the default LOC_OPTIMIZED_OUT class. */
11875 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11876 if (!suppress_add)
11877 list_to_add = cu->list_in_scope;
442ddf59 11878 }
c906108c
SS
11879 }
11880 break;
11881 case DW_TAG_formal_parameter:
edb3359d
DJ
11882 /* If we are inside a function, mark this as an argument. If
11883 not, we might be looking at an argument to an inlined function
11884 when we do not have enough information to show inlined frames;
11885 pretend it's a local variable in that case so that the user can
11886 still see it. */
11887 if (context_stack_depth > 0
11888 && context_stack[context_stack_depth - 1].name != NULL)
11889 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11890 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11891 if (attr)
11892 {
e7c27a73 11893 var_decode_location (attr, sym, cu);
c906108c 11894 }
e142c38c 11895 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11896 if (attr)
11897 {
e7c27a73 11898 dwarf2_const_value (attr, sym, cu);
c906108c 11899 }
f346a30d 11900
e37fd15a 11901 list_to_add = cu->list_in_scope;
c906108c
SS
11902 break;
11903 case DW_TAG_unspecified_parameters:
11904 /* From varargs functions; gdb doesn't seem to have any
11905 interest in this information, so just ignore it for now.
11906 (FIXME?) */
11907 break;
34eaf542
TT
11908 case DW_TAG_template_type_param:
11909 suppress_add = 1;
11910 /* Fall through. */
c906108c 11911 case DW_TAG_class_type:
680b30c7 11912 case DW_TAG_interface_type:
c906108c
SS
11913 case DW_TAG_structure_type:
11914 case DW_TAG_union_type:
72019c9c 11915 case DW_TAG_set_type:
c906108c
SS
11916 case DW_TAG_enumeration_type:
11917 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11918 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11919
63d06c5c 11920 {
987504bb 11921 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11922 really ever be static objects: otherwise, if you try
11923 to, say, break of a class's method and you're in a file
11924 which doesn't mention that class, it won't work unless
11925 the check for all static symbols in lookup_symbol_aux
11926 saves you. See the OtherFileClass tests in
11927 gdb.c++/namespace.exp. */
11928
e37fd15a 11929 if (!suppress_add)
34eaf542 11930 {
34eaf542
TT
11931 list_to_add = (cu->list_in_scope == &file_symbols
11932 && (cu->language == language_cplus
11933 || cu->language == language_java)
11934 ? &global_symbols : cu->list_in_scope);
63d06c5c 11935
64382290
TT
11936 /* The semantics of C++ state that "struct foo {
11937 ... }" also defines a typedef for "foo". A Java
11938 class declaration also defines a typedef for the
11939 class. */
11940 if (cu->language == language_cplus
11941 || cu->language == language_java
11942 || cu->language == language_ada)
11943 {
11944 /* The symbol's name is already allocated along
11945 with this objfile, so we don't need to
11946 duplicate it for the type. */
11947 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11948 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11949 }
63d06c5c
DC
11950 }
11951 }
c906108c
SS
11952 break;
11953 case DW_TAG_typedef:
63d06c5c
DC
11954 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11955 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11956 list_to_add = cu->list_in_scope;
63d06c5c 11957 break;
c906108c 11958 case DW_TAG_base_type:
a02abb62 11959 case DW_TAG_subrange_type:
c906108c 11960 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11961 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11962 list_to_add = cu->list_in_scope;
c906108c
SS
11963 break;
11964 case DW_TAG_enumerator:
e142c38c 11965 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11966 if (attr)
11967 {
e7c27a73 11968 dwarf2_const_value (attr, sym, cu);
c906108c 11969 }
63d06c5c
DC
11970 {
11971 /* NOTE: carlton/2003-11-10: See comment above in the
11972 DW_TAG_class_type, etc. block. */
11973
e142c38c 11974 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11975 && (cu->language == language_cplus
11976 || cu->language == language_java)
e142c38c 11977 ? &global_symbols : cu->list_in_scope);
63d06c5c 11978 }
c906108c 11979 break;
5c4e30ca
DC
11980 case DW_TAG_namespace:
11981 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11982 list_to_add = &global_symbols;
5c4e30ca 11983 break;
c906108c
SS
11984 default:
11985 /* Not a tag we recognize. Hopefully we aren't processing
11986 trash data, but since we must specifically ignore things
11987 we don't recognize, there is nothing else we should do at
0963b4bd 11988 this point. */
e2e0b3e5 11989 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11990 dwarf_tag_name (die->tag));
c906108c
SS
11991 break;
11992 }
df8a16a1 11993
e37fd15a
SW
11994 if (suppress_add)
11995 {
11996 sym->hash_next = objfile->template_symbols;
11997 objfile->template_symbols = sym;
11998 list_to_add = NULL;
11999 }
12000
12001 if (list_to_add != NULL)
12002 add_symbol_to_list (sym, list_to_add);
12003
df8a16a1
DJ
12004 /* For the benefit of old versions of GCC, check for anonymous
12005 namespaces based on the demangled name. */
12006 if (!processing_has_namespace_info
94af9270 12007 && cu->language == language_cplus)
a10964d1 12008 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12009 }
12010 return (sym);
12011}
12012
34eaf542
TT
12013/* A wrapper for new_symbol_full that always allocates a new symbol. */
12014
12015static struct symbol *
12016new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12017{
12018 return new_symbol_full (die, type, cu, NULL);
12019}
12020
98bfdba5
PA
12021/* Given an attr with a DW_FORM_dataN value in host byte order,
12022 zero-extend it as appropriate for the symbol's type. The DWARF
12023 standard (v4) is not entirely clear about the meaning of using
12024 DW_FORM_dataN for a constant with a signed type, where the type is
12025 wider than the data. The conclusion of a discussion on the DWARF
12026 list was that this is unspecified. We choose to always zero-extend
12027 because that is the interpretation long in use by GCC. */
c906108c 12028
98bfdba5
PA
12029static gdb_byte *
12030dwarf2_const_value_data (struct attribute *attr, struct type *type,
12031 const char *name, struct obstack *obstack,
12032 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12033{
e7c27a73 12034 struct objfile *objfile = cu->objfile;
e17a4113
UW
12035 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12036 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12037 LONGEST l = DW_UNSND (attr);
12038
12039 if (bits < sizeof (*value) * 8)
12040 {
12041 l &= ((LONGEST) 1 << bits) - 1;
12042 *value = l;
12043 }
12044 else if (bits == sizeof (*value) * 8)
12045 *value = l;
12046 else
12047 {
12048 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12049 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12050 return bytes;
12051 }
12052
12053 return NULL;
12054}
12055
12056/* Read a constant value from an attribute. Either set *VALUE, or if
12057 the value does not fit in *VALUE, set *BYTES - either already
12058 allocated on the objfile obstack, or newly allocated on OBSTACK,
12059 or, set *BATON, if we translated the constant to a location
12060 expression. */
12061
12062static void
12063dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12064 const char *name, struct obstack *obstack,
12065 struct dwarf2_cu *cu,
12066 long *value, gdb_byte **bytes,
12067 struct dwarf2_locexpr_baton **baton)
12068{
12069 struct objfile *objfile = cu->objfile;
12070 struct comp_unit_head *cu_header = &cu->header;
c906108c 12071 struct dwarf_block *blk;
98bfdba5
PA
12072 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12073 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12074
12075 *value = 0;
12076 *bytes = NULL;
12077 *baton = NULL;
c906108c
SS
12078
12079 switch (attr->form)
12080 {
12081 case DW_FORM_addr:
ac56253d 12082 {
ac56253d
TT
12083 gdb_byte *data;
12084
98bfdba5
PA
12085 if (TYPE_LENGTH (type) != cu_header->addr_size)
12086 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12087 cu_header->addr_size,
98bfdba5 12088 TYPE_LENGTH (type));
ac56253d
TT
12089 /* Symbols of this form are reasonably rare, so we just
12090 piggyback on the existing location code rather than writing
12091 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12092 *baton = obstack_alloc (&objfile->objfile_obstack,
12093 sizeof (struct dwarf2_locexpr_baton));
12094 (*baton)->per_cu = cu->per_cu;
12095 gdb_assert ((*baton)->per_cu);
ac56253d 12096
98bfdba5
PA
12097 (*baton)->size = 2 + cu_header->addr_size;
12098 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12099 (*baton)->data = data;
ac56253d
TT
12100
12101 data[0] = DW_OP_addr;
12102 store_unsigned_integer (&data[1], cu_header->addr_size,
12103 byte_order, DW_ADDR (attr));
12104 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12105 }
c906108c 12106 break;
4ac36638 12107 case DW_FORM_string:
93b5768b 12108 case DW_FORM_strp:
98bfdba5
PA
12109 /* DW_STRING is already allocated on the objfile obstack, point
12110 directly to it. */
12111 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12112 break;
c906108c
SS
12113 case DW_FORM_block1:
12114 case DW_FORM_block2:
12115 case DW_FORM_block4:
12116 case DW_FORM_block:
2dc7f7b3 12117 case DW_FORM_exprloc:
c906108c 12118 blk = DW_BLOCK (attr);
98bfdba5
PA
12119 if (TYPE_LENGTH (type) != blk->size)
12120 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12121 TYPE_LENGTH (type));
12122 *bytes = blk->data;
c906108c 12123 break;
2df3850c
JM
12124
12125 /* The DW_AT_const_value attributes are supposed to carry the
12126 symbol's value "represented as it would be on the target
12127 architecture." By the time we get here, it's already been
12128 converted to host endianness, so we just need to sign- or
12129 zero-extend it as appropriate. */
12130 case DW_FORM_data1:
3e43a32a
MS
12131 *bytes = dwarf2_const_value_data (attr, type, name,
12132 obstack, cu, value, 8);
2df3850c 12133 break;
c906108c 12134 case DW_FORM_data2:
3e43a32a
MS
12135 *bytes = dwarf2_const_value_data (attr, type, name,
12136 obstack, cu, value, 16);
2df3850c 12137 break;
c906108c 12138 case DW_FORM_data4:
3e43a32a
MS
12139 *bytes = dwarf2_const_value_data (attr, type, name,
12140 obstack, cu, value, 32);
2df3850c 12141 break;
c906108c 12142 case DW_FORM_data8:
3e43a32a
MS
12143 *bytes = dwarf2_const_value_data (attr, type, name,
12144 obstack, cu, value, 64);
2df3850c
JM
12145 break;
12146
c906108c 12147 case DW_FORM_sdata:
98bfdba5 12148 *value = DW_SND (attr);
2df3850c
JM
12149 break;
12150
c906108c 12151 case DW_FORM_udata:
98bfdba5 12152 *value = DW_UNSND (attr);
c906108c 12153 break;
2df3850c 12154
c906108c 12155 default:
4d3c2250 12156 complaint (&symfile_complaints,
e2e0b3e5 12157 _("unsupported const value attribute form: '%s'"),
4d3c2250 12158 dwarf_form_name (attr->form));
98bfdba5 12159 *value = 0;
c906108c
SS
12160 break;
12161 }
12162}
12163
2df3850c 12164
98bfdba5
PA
12165/* Copy constant value from an attribute to a symbol. */
12166
2df3850c 12167static void
98bfdba5
PA
12168dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12169 struct dwarf2_cu *cu)
2df3850c 12170{
98bfdba5
PA
12171 struct objfile *objfile = cu->objfile;
12172 struct comp_unit_head *cu_header = &cu->header;
12173 long value;
12174 gdb_byte *bytes;
12175 struct dwarf2_locexpr_baton *baton;
2df3850c 12176
98bfdba5
PA
12177 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12178 SYMBOL_PRINT_NAME (sym),
12179 &objfile->objfile_obstack, cu,
12180 &value, &bytes, &baton);
2df3850c 12181
98bfdba5
PA
12182 if (baton != NULL)
12183 {
12184 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12185 SYMBOL_LOCATION_BATON (sym) = baton;
12186 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12187 }
12188 else if (bytes != NULL)
12189 {
12190 SYMBOL_VALUE_BYTES (sym) = bytes;
12191 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12192 }
12193 else
12194 {
12195 SYMBOL_VALUE (sym) = value;
12196 SYMBOL_CLASS (sym) = LOC_CONST;
12197 }
2df3850c
JM
12198}
12199
c906108c
SS
12200/* Return the type of the die in question using its DW_AT_type attribute. */
12201
12202static struct type *
e7c27a73 12203die_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_type, cu);
c906108c
SS
12208 if (!type_attr)
12209 {
12210 /* A missing DW_AT_type represents a void type. */
46bf5051 12211 return objfile_type (cu->objfile)->builtin_void;
c906108c 12212 }
348e048f 12213
673bfd45 12214 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12215}
12216
b4ba55a1
JB
12217/* True iff CU's producer generates GNAT Ada auxiliary information
12218 that allows to find parallel types through that information instead
12219 of having to do expensive parallel lookups by type name. */
12220
12221static int
12222need_gnat_info (struct dwarf2_cu *cu)
12223{
12224 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12225 of GNAT produces this auxiliary information, without any indication
12226 that it is produced. Part of enhancing the FSF version of GNAT
12227 to produce that information will be to put in place an indicator
12228 that we can use in order to determine whether the descriptive type
12229 info is available or not. One suggestion that has been made is
12230 to use a new attribute, attached to the CU die. For now, assume
12231 that the descriptive type info is not available. */
12232 return 0;
12233}
12234
b4ba55a1
JB
12235/* Return the auxiliary type of the die in question using its
12236 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12237 attribute is not present. */
12238
12239static struct type *
12240die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12241{
b4ba55a1 12242 struct attribute *type_attr;
b4ba55a1
JB
12243
12244 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12245 if (!type_attr)
12246 return NULL;
12247
673bfd45 12248 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12249}
12250
12251/* If DIE has a descriptive_type attribute, then set the TYPE's
12252 descriptive type accordingly. */
12253
12254static void
12255set_descriptive_type (struct type *type, struct die_info *die,
12256 struct dwarf2_cu *cu)
12257{
12258 struct type *descriptive_type = die_descriptive_type (die, cu);
12259
12260 if (descriptive_type)
12261 {
12262 ALLOCATE_GNAT_AUX_TYPE (type);
12263 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12264 }
12265}
12266
c906108c
SS
12267/* Return the containing type of the die in question using its
12268 DW_AT_containing_type attribute. */
12269
12270static struct type *
e7c27a73 12271die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12272{
c906108c 12273 struct attribute *type_attr;
c906108c 12274
e142c38c 12275 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12276 if (!type_attr)
12277 error (_("Dwarf Error: Problem turning containing type into gdb type "
12278 "[in module %s]"), cu->objfile->name);
12279
673bfd45 12280 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12281}
12282
673bfd45
DE
12283/* Look up the type of DIE in CU using its type attribute ATTR.
12284 If there is no type substitute an error marker. */
12285
c906108c 12286static struct type *
673bfd45
DE
12287lookup_die_type (struct die_info *die, struct attribute *attr,
12288 struct dwarf2_cu *cu)
c906108c 12289{
bb5ed363 12290 struct objfile *objfile = cu->objfile;
f792889a
DJ
12291 struct type *this_type;
12292
673bfd45
DE
12293 /* First see if we have it cached. */
12294
12295 if (is_ref_attr (attr))
12296 {
b64f50a1 12297 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
12298
12299 this_type = get_die_type_at_offset (offset, cu->per_cu);
12300 }
55f1336d 12301 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12302 {
12303 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12304 struct dwarf2_cu *sig_cu;
b64f50a1 12305 sect_offset offset;
673bfd45
DE
12306
12307 /* sig_type will be NULL if the signatured type is missing from
12308 the debug info. */
12309 if (sig_type == NULL)
12310 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12311 "at 0x%x [in module %s]"),
b64f50a1 12312 die->offset.sect_off, objfile->name);
673bfd45 12313
b0df02fd 12314 gdb_assert (sig_type->per_cu.debug_types_section);
b64f50a1
JK
12315 offset.sect_off = (sig_type->per_cu.offset.sect_off
12316 + sig_type->type_offset.cu_off);
673bfd45
DE
12317 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12318 }
12319 else
12320 {
12321 dump_die_for_error (die);
12322 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12323 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12324 }
12325
12326 /* If not cached we need to read it in. */
12327
12328 if (this_type == NULL)
12329 {
12330 struct die_info *type_die;
12331 struct dwarf2_cu *type_cu = cu;
12332
12333 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12334 /* If the type is cached, we should have found it above. */
12335 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12336 this_type = read_type_die_1 (type_die, type_cu);
12337 }
12338
12339 /* If we still don't have a type use an error marker. */
12340
12341 if (this_type == NULL)
c906108c 12342 {
b00fdb78
TT
12343 char *message, *saved;
12344
12345 /* read_type_die already issued a complaint. */
12346 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12347 objfile->name,
b64f50a1
JK
12348 cu->header.offset.sect_off,
12349 die->offset.sect_off);
bb5ed363 12350 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12351 message, strlen (message));
12352 xfree (message);
12353
bb5ed363 12354 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12355 }
673bfd45 12356
f792889a 12357 return this_type;
c906108c
SS
12358}
12359
673bfd45
DE
12360/* Return the type in DIE, CU.
12361 Returns NULL for invalid types.
12362
12363 This first does a lookup in the appropriate type_hash table,
12364 and only reads the die in if necessary.
12365
12366 NOTE: This can be called when reading in partial or full symbols. */
12367
f792889a 12368static struct type *
e7c27a73 12369read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12370{
f792889a
DJ
12371 struct type *this_type;
12372
12373 this_type = get_die_type (die, cu);
12374 if (this_type)
12375 return this_type;
12376
673bfd45
DE
12377 return read_type_die_1 (die, cu);
12378}
12379
12380/* Read the type in DIE, CU.
12381 Returns NULL for invalid types. */
12382
12383static struct type *
12384read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12385{
12386 struct type *this_type = NULL;
12387
c906108c
SS
12388 switch (die->tag)
12389 {
12390 case DW_TAG_class_type:
680b30c7 12391 case DW_TAG_interface_type:
c906108c
SS
12392 case DW_TAG_structure_type:
12393 case DW_TAG_union_type:
f792889a 12394 this_type = read_structure_type (die, cu);
c906108c
SS
12395 break;
12396 case DW_TAG_enumeration_type:
f792889a 12397 this_type = read_enumeration_type (die, cu);
c906108c
SS
12398 break;
12399 case DW_TAG_subprogram:
12400 case DW_TAG_subroutine_type:
edb3359d 12401 case DW_TAG_inlined_subroutine:
f792889a 12402 this_type = read_subroutine_type (die, cu);
c906108c
SS
12403 break;
12404 case DW_TAG_array_type:
f792889a 12405 this_type = read_array_type (die, cu);
c906108c 12406 break;
72019c9c 12407 case DW_TAG_set_type:
f792889a 12408 this_type = read_set_type (die, cu);
72019c9c 12409 break;
c906108c 12410 case DW_TAG_pointer_type:
f792889a 12411 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12412 break;
12413 case DW_TAG_ptr_to_member_type:
f792889a 12414 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12415 break;
12416 case DW_TAG_reference_type:
f792889a 12417 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12418 break;
12419 case DW_TAG_const_type:
f792889a 12420 this_type = read_tag_const_type (die, cu);
c906108c
SS
12421 break;
12422 case DW_TAG_volatile_type:
f792889a 12423 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12424 break;
12425 case DW_TAG_string_type:
f792889a 12426 this_type = read_tag_string_type (die, cu);
c906108c
SS
12427 break;
12428 case DW_TAG_typedef:
f792889a 12429 this_type = read_typedef (die, cu);
c906108c 12430 break;
a02abb62 12431 case DW_TAG_subrange_type:
f792889a 12432 this_type = read_subrange_type (die, cu);
a02abb62 12433 break;
c906108c 12434 case DW_TAG_base_type:
f792889a 12435 this_type = read_base_type (die, cu);
c906108c 12436 break;
81a17f79 12437 case DW_TAG_unspecified_type:
f792889a 12438 this_type = read_unspecified_type (die, cu);
81a17f79 12439 break;
0114d602
DJ
12440 case DW_TAG_namespace:
12441 this_type = read_namespace_type (die, cu);
12442 break;
f55ee35c
JK
12443 case DW_TAG_module:
12444 this_type = read_module_type (die, cu);
12445 break;
c906108c 12446 default:
3e43a32a
MS
12447 complaint (&symfile_complaints,
12448 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12449 dwarf_tag_name (die->tag));
c906108c
SS
12450 break;
12451 }
63d06c5c 12452
f792889a 12453 return this_type;
63d06c5c
DC
12454}
12455
abc72ce4
DE
12456/* See if we can figure out if the class lives in a namespace. We do
12457 this by looking for a member function; its demangled name will
12458 contain namespace info, if there is any.
12459 Return the computed name or NULL.
12460 Space for the result is allocated on the objfile's obstack.
12461 This is the full-die version of guess_partial_die_structure_name.
12462 In this case we know DIE has no useful parent. */
12463
12464static char *
12465guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12466{
12467 struct die_info *spec_die;
12468 struct dwarf2_cu *spec_cu;
12469 struct die_info *child;
12470
12471 spec_cu = cu;
12472 spec_die = die_specification (die, &spec_cu);
12473 if (spec_die != NULL)
12474 {
12475 die = spec_die;
12476 cu = spec_cu;
12477 }
12478
12479 for (child = die->child;
12480 child != NULL;
12481 child = child->sibling)
12482 {
12483 if (child->tag == DW_TAG_subprogram)
12484 {
12485 struct attribute *attr;
12486
12487 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12488 if (attr == NULL)
12489 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12490 if (attr != NULL)
12491 {
12492 char *actual_name
12493 = language_class_name_from_physname (cu->language_defn,
12494 DW_STRING (attr));
12495 char *name = NULL;
12496
12497 if (actual_name != NULL)
12498 {
12499 char *die_name = dwarf2_name (die, cu);
12500
12501 if (die_name != NULL
12502 && strcmp (die_name, actual_name) != 0)
12503 {
12504 /* Strip off the class name from the full name.
12505 We want the prefix. */
12506 int die_name_len = strlen (die_name);
12507 int actual_name_len = strlen (actual_name);
12508
12509 /* Test for '::' as a sanity check. */
12510 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12511 && actual_name[actual_name_len
12512 - die_name_len - 1] == ':')
abc72ce4
DE
12513 name =
12514 obsavestring (actual_name,
12515 actual_name_len - die_name_len - 2,
12516 &cu->objfile->objfile_obstack);
12517 }
12518 }
12519 xfree (actual_name);
12520 return name;
12521 }
12522 }
12523 }
12524
12525 return NULL;
12526}
12527
96408a79
SA
12528/* GCC might emit a nameless typedef that has a linkage name. Determine the
12529 prefix part in such case. See
12530 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12531
12532static char *
12533anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12534{
12535 struct attribute *attr;
12536 char *base;
12537
12538 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12539 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12540 return NULL;
12541
12542 attr = dwarf2_attr (die, DW_AT_name, cu);
12543 if (attr != NULL && DW_STRING (attr) != NULL)
12544 return NULL;
12545
12546 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12547 if (attr == NULL)
12548 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12549 if (attr == NULL || DW_STRING (attr) == NULL)
12550 return NULL;
12551
12552 /* dwarf2_name had to be already called. */
12553 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12554
12555 /* Strip the base name, keep any leading namespaces/classes. */
12556 base = strrchr (DW_STRING (attr), ':');
12557 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12558 return "";
12559
12560 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12561 &cu->objfile->objfile_obstack);
12562}
12563
fdde2d81 12564/* Return the name of the namespace/class that DIE is defined within,
0114d602 12565 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12566
0114d602
DJ
12567 For example, if we're within the method foo() in the following
12568 code:
12569
12570 namespace N {
12571 class C {
12572 void foo () {
12573 }
12574 };
12575 }
12576
12577 then determine_prefix on foo's die will return "N::C". */
fdde2d81 12578
0d5cff50 12579static const char *
e142c38c 12580determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12581{
0114d602
DJ
12582 struct die_info *parent, *spec_die;
12583 struct dwarf2_cu *spec_cu;
12584 struct type *parent_type;
96408a79 12585 char *retval;
63d06c5c 12586
f55ee35c
JK
12587 if (cu->language != language_cplus && cu->language != language_java
12588 && cu->language != language_fortran)
0114d602
DJ
12589 return "";
12590
96408a79
SA
12591 retval = anonymous_struct_prefix (die, cu);
12592 if (retval)
12593 return retval;
12594
0114d602
DJ
12595 /* We have to be careful in the presence of DW_AT_specification.
12596 For example, with GCC 3.4, given the code
12597
12598 namespace N {
12599 void foo() {
12600 // Definition of N::foo.
12601 }
12602 }
12603
12604 then we'll have a tree of DIEs like this:
12605
12606 1: DW_TAG_compile_unit
12607 2: DW_TAG_namespace // N
12608 3: DW_TAG_subprogram // declaration of N::foo
12609 4: DW_TAG_subprogram // definition of N::foo
12610 DW_AT_specification // refers to die #3
12611
12612 Thus, when processing die #4, we have to pretend that we're in
12613 the context of its DW_AT_specification, namely the contex of die
12614 #3. */
12615 spec_cu = cu;
12616 spec_die = die_specification (die, &spec_cu);
12617 if (spec_die == NULL)
12618 parent = die->parent;
12619 else
63d06c5c 12620 {
0114d602
DJ
12621 parent = spec_die->parent;
12622 cu = spec_cu;
63d06c5c 12623 }
0114d602
DJ
12624
12625 if (parent == NULL)
12626 return "";
98bfdba5
PA
12627 else if (parent->building_fullname)
12628 {
12629 const char *name;
12630 const char *parent_name;
12631
12632 /* It has been seen on RealView 2.2 built binaries,
12633 DW_TAG_template_type_param types actually _defined_ as
12634 children of the parent class:
12635
12636 enum E {};
12637 template class <class Enum> Class{};
12638 Class<enum E> class_e;
12639
12640 1: DW_TAG_class_type (Class)
12641 2: DW_TAG_enumeration_type (E)
12642 3: DW_TAG_enumerator (enum1:0)
12643 3: DW_TAG_enumerator (enum2:1)
12644 ...
12645 2: DW_TAG_template_type_param
12646 DW_AT_type DW_FORM_ref_udata (E)
12647
12648 Besides being broken debug info, it can put GDB into an
12649 infinite loop. Consider:
12650
12651 When we're building the full name for Class<E>, we'll start
12652 at Class, and go look over its template type parameters,
12653 finding E. We'll then try to build the full name of E, and
12654 reach here. We're now trying to build the full name of E,
12655 and look over the parent DIE for containing scope. In the
12656 broken case, if we followed the parent DIE of E, we'd again
12657 find Class, and once again go look at its template type
12658 arguments, etc., etc. Simply don't consider such parent die
12659 as source-level parent of this die (it can't be, the language
12660 doesn't allow it), and break the loop here. */
12661 name = dwarf2_name (die, cu);
12662 parent_name = dwarf2_name (parent, cu);
12663 complaint (&symfile_complaints,
12664 _("template param type '%s' defined within parent '%s'"),
12665 name ? name : "<unknown>",
12666 parent_name ? parent_name : "<unknown>");
12667 return "";
12668 }
63d06c5c 12669 else
0114d602
DJ
12670 switch (parent->tag)
12671 {
63d06c5c 12672 case DW_TAG_namespace:
0114d602 12673 parent_type = read_type_die (parent, cu);
acebe513
UW
12674 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12675 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12676 Work around this problem here. */
12677 if (cu->language == language_cplus
12678 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12679 return "";
0114d602
DJ
12680 /* We give a name to even anonymous namespaces. */
12681 return TYPE_TAG_NAME (parent_type);
63d06c5c 12682 case DW_TAG_class_type:
680b30c7 12683 case DW_TAG_interface_type:
63d06c5c 12684 case DW_TAG_structure_type:
0114d602 12685 case DW_TAG_union_type:
f55ee35c 12686 case DW_TAG_module:
0114d602
DJ
12687 parent_type = read_type_die (parent, cu);
12688 if (TYPE_TAG_NAME (parent_type) != NULL)
12689 return TYPE_TAG_NAME (parent_type);
12690 else
12691 /* An anonymous structure is only allowed non-static data
12692 members; no typedefs, no member functions, et cetera.
12693 So it does not need a prefix. */
12694 return "";
abc72ce4
DE
12695 case DW_TAG_compile_unit:
12696 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12697 if (cu->language == language_cplus
8b70b953 12698 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12699 && die->child != NULL
12700 && (die->tag == DW_TAG_class_type
12701 || die->tag == DW_TAG_structure_type
12702 || die->tag == DW_TAG_union_type))
12703 {
12704 char *name = guess_full_die_structure_name (die, cu);
12705 if (name != NULL)
12706 return name;
12707 }
12708 return "";
63d06c5c 12709 default:
8176b9b8 12710 return determine_prefix (parent, cu);
63d06c5c 12711 }
63d06c5c
DC
12712}
12713
3e43a32a
MS
12714/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12715 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12716 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12717 an obconcat, otherwise allocate storage for the result. The CU argument is
12718 used to determine the language and hence, the appropriate separator. */
987504bb 12719
f55ee35c 12720#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12721
12722static char *
f55ee35c
JK
12723typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12724 int physname, struct dwarf2_cu *cu)
63d06c5c 12725{
f55ee35c 12726 const char *lead = "";
5c315b68 12727 const char *sep;
63d06c5c 12728
3e43a32a
MS
12729 if (suffix == NULL || suffix[0] == '\0'
12730 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12731 sep = "";
12732 else if (cu->language == language_java)
12733 sep = ".";
f55ee35c
JK
12734 else if (cu->language == language_fortran && physname)
12735 {
12736 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12737 DW_AT_MIPS_linkage_name is preferred and used instead. */
12738
12739 lead = "__";
12740 sep = "_MOD_";
12741 }
987504bb
JJ
12742 else
12743 sep = "::";
63d06c5c 12744
6dd47d34
DE
12745 if (prefix == NULL)
12746 prefix = "";
12747 if (suffix == NULL)
12748 suffix = "";
12749
987504bb
JJ
12750 if (obs == NULL)
12751 {
3e43a32a
MS
12752 char *retval
12753 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12754
f55ee35c
JK
12755 strcpy (retval, lead);
12756 strcat (retval, prefix);
6dd47d34
DE
12757 strcat (retval, sep);
12758 strcat (retval, suffix);
63d06c5c
DC
12759 return retval;
12760 }
987504bb
JJ
12761 else
12762 {
12763 /* We have an obstack. */
f55ee35c 12764 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12765 }
63d06c5c
DC
12766}
12767
c906108c
SS
12768/* Return sibling of die, NULL if no sibling. */
12769
f9aca02d 12770static struct die_info *
fba45db2 12771sibling_die (struct die_info *die)
c906108c 12772{
639d11d3 12773 return die->sibling;
c906108c
SS
12774}
12775
71c25dea
TT
12776/* Get name of a die, return NULL if not found. */
12777
12778static char *
12779dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12780 struct obstack *obstack)
12781{
12782 if (name && cu->language == language_cplus)
12783 {
12784 char *canon_name = cp_canonicalize_string (name);
12785
12786 if (canon_name != NULL)
12787 {
12788 if (strcmp (canon_name, name) != 0)
12789 name = obsavestring (canon_name, strlen (canon_name),
12790 obstack);
12791 xfree (canon_name);
12792 }
12793 }
12794
12795 return name;
c906108c
SS
12796}
12797
9219021c
DC
12798/* Get name of a die, return NULL if not found. */
12799
12800static char *
e142c38c 12801dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12802{
12803 struct attribute *attr;
12804
e142c38c 12805 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12806 if ((!attr || !DW_STRING (attr))
12807 && die->tag != DW_TAG_class_type
12808 && die->tag != DW_TAG_interface_type
12809 && die->tag != DW_TAG_structure_type
12810 && die->tag != DW_TAG_union_type)
71c25dea
TT
12811 return NULL;
12812
12813 switch (die->tag)
12814 {
12815 case DW_TAG_compile_unit:
12816 /* Compilation units have a DW_AT_name that is a filename, not
12817 a source language identifier. */
12818 case DW_TAG_enumeration_type:
12819 case DW_TAG_enumerator:
12820 /* These tags always have simple identifiers already; no need
12821 to canonicalize them. */
12822 return DW_STRING (attr);
907af001 12823
418835cc
KS
12824 case DW_TAG_subprogram:
12825 /* Java constructors will all be named "<init>", so return
12826 the class name when we see this special case. */
12827 if (cu->language == language_java
12828 && DW_STRING (attr) != NULL
12829 && strcmp (DW_STRING (attr), "<init>") == 0)
12830 {
12831 struct dwarf2_cu *spec_cu = cu;
12832 struct die_info *spec_die;
12833
12834 /* GCJ will output '<init>' for Java constructor names.
12835 For this special case, return the name of the parent class. */
12836
12837 /* GCJ may output suprogram DIEs with AT_specification set.
12838 If so, use the name of the specified DIE. */
12839 spec_die = die_specification (die, &spec_cu);
12840 if (spec_die != NULL)
12841 return dwarf2_name (spec_die, spec_cu);
12842
12843 do
12844 {
12845 die = die->parent;
12846 if (die->tag == DW_TAG_class_type)
12847 return dwarf2_name (die, cu);
12848 }
12849 while (die->tag != DW_TAG_compile_unit);
12850 }
907af001
UW
12851 break;
12852
12853 case DW_TAG_class_type:
12854 case DW_TAG_interface_type:
12855 case DW_TAG_structure_type:
12856 case DW_TAG_union_type:
12857 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12858 structures or unions. These were of the form "._%d" in GCC 4.1,
12859 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12860 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12861 if (attr && DW_STRING (attr)
12862 && (strncmp (DW_STRING (attr), "._", 2) == 0
12863 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12864 return NULL;
53832f31
TT
12865
12866 /* GCC might emit a nameless typedef that has a linkage name. See
12867 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12868 if (!attr || DW_STRING (attr) == NULL)
12869 {
df5c6c50 12870 char *demangled = NULL;
53832f31
TT
12871
12872 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12873 if (attr == NULL)
12874 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12875
12876 if (attr == NULL || DW_STRING (attr) == NULL)
12877 return NULL;
12878
df5c6c50
JK
12879 /* Avoid demangling DW_STRING (attr) the second time on a second
12880 call for the same DIE. */
12881 if (!DW_STRING_IS_CANONICAL (attr))
12882 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12883
12884 if (demangled)
12885 {
96408a79
SA
12886 char *base;
12887
53832f31 12888 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12889 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12890 &cu->objfile->objfile_obstack);
53832f31
TT
12891 DW_STRING_IS_CANONICAL (attr) = 1;
12892 xfree (demangled);
96408a79
SA
12893
12894 /* Strip any leading namespaces/classes, keep only the base name.
12895 DW_AT_name for named DIEs does not contain the prefixes. */
12896 base = strrchr (DW_STRING (attr), ':');
12897 if (base && base > DW_STRING (attr) && base[-1] == ':')
12898 return &base[1];
12899 else
12900 return DW_STRING (attr);
53832f31
TT
12901 }
12902 }
907af001
UW
12903 break;
12904
71c25dea 12905 default:
907af001
UW
12906 break;
12907 }
12908
12909 if (!DW_STRING_IS_CANONICAL (attr))
12910 {
12911 DW_STRING (attr)
12912 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12913 &cu->objfile->objfile_obstack);
12914 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12915 }
907af001 12916 return DW_STRING (attr);
9219021c
DC
12917}
12918
12919/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12920 is none. *EXT_CU is the CU containing DIE on input, and the CU
12921 containing the return value on output. */
9219021c
DC
12922
12923static struct die_info *
f2f0e013 12924dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12925{
12926 struct attribute *attr;
9219021c 12927
f2f0e013 12928 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12929 if (attr == NULL)
12930 return NULL;
12931
f2f0e013 12932 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12933}
12934
c906108c
SS
12935/* Convert a DIE tag into its string name. */
12936
12937static char *
aa1ee363 12938dwarf_tag_name (unsigned tag)
c906108c
SS
12939{
12940 switch (tag)
12941 {
12942 case DW_TAG_padding:
12943 return "DW_TAG_padding";
12944 case DW_TAG_array_type:
12945 return "DW_TAG_array_type";
12946 case DW_TAG_class_type:
12947 return "DW_TAG_class_type";
12948 case DW_TAG_entry_point:
12949 return "DW_TAG_entry_point";
12950 case DW_TAG_enumeration_type:
12951 return "DW_TAG_enumeration_type";
12952 case DW_TAG_formal_parameter:
12953 return "DW_TAG_formal_parameter";
12954 case DW_TAG_imported_declaration:
12955 return "DW_TAG_imported_declaration";
12956 case DW_TAG_label:
12957 return "DW_TAG_label";
12958 case DW_TAG_lexical_block:
12959 return "DW_TAG_lexical_block";
12960 case DW_TAG_member:
12961 return "DW_TAG_member";
12962 case DW_TAG_pointer_type:
12963 return "DW_TAG_pointer_type";
12964 case DW_TAG_reference_type:
12965 return "DW_TAG_reference_type";
12966 case DW_TAG_compile_unit:
12967 return "DW_TAG_compile_unit";
12968 case DW_TAG_string_type:
12969 return "DW_TAG_string_type";
12970 case DW_TAG_structure_type:
12971 return "DW_TAG_structure_type";
12972 case DW_TAG_subroutine_type:
12973 return "DW_TAG_subroutine_type";
12974 case DW_TAG_typedef:
12975 return "DW_TAG_typedef";
12976 case DW_TAG_union_type:
12977 return "DW_TAG_union_type";
12978 case DW_TAG_unspecified_parameters:
12979 return "DW_TAG_unspecified_parameters";
12980 case DW_TAG_variant:
12981 return "DW_TAG_variant";
12982 case DW_TAG_common_block:
12983 return "DW_TAG_common_block";
12984 case DW_TAG_common_inclusion:
12985 return "DW_TAG_common_inclusion";
12986 case DW_TAG_inheritance:
12987 return "DW_TAG_inheritance";
12988 case DW_TAG_inlined_subroutine:
12989 return "DW_TAG_inlined_subroutine";
12990 case DW_TAG_module:
12991 return "DW_TAG_module";
12992 case DW_TAG_ptr_to_member_type:
12993 return "DW_TAG_ptr_to_member_type";
12994 case DW_TAG_set_type:
12995 return "DW_TAG_set_type";
12996 case DW_TAG_subrange_type:
12997 return "DW_TAG_subrange_type";
12998 case DW_TAG_with_stmt:
12999 return "DW_TAG_with_stmt";
13000 case DW_TAG_access_declaration:
13001 return "DW_TAG_access_declaration";
13002 case DW_TAG_base_type:
13003 return "DW_TAG_base_type";
13004 case DW_TAG_catch_block:
13005 return "DW_TAG_catch_block";
13006 case DW_TAG_const_type:
13007 return "DW_TAG_const_type";
13008 case DW_TAG_constant:
13009 return "DW_TAG_constant";
13010 case DW_TAG_enumerator:
13011 return "DW_TAG_enumerator";
13012 case DW_TAG_file_type:
13013 return "DW_TAG_file_type";
13014 case DW_TAG_friend:
13015 return "DW_TAG_friend";
13016 case DW_TAG_namelist:
13017 return "DW_TAG_namelist";
13018 case DW_TAG_namelist_item:
13019 return "DW_TAG_namelist_item";
13020 case DW_TAG_packed_type:
13021 return "DW_TAG_packed_type";
13022 case DW_TAG_subprogram:
13023 return "DW_TAG_subprogram";
13024 case DW_TAG_template_type_param:
13025 return "DW_TAG_template_type_param";
13026 case DW_TAG_template_value_param:
13027 return "DW_TAG_template_value_param";
13028 case DW_TAG_thrown_type:
13029 return "DW_TAG_thrown_type";
13030 case DW_TAG_try_block:
13031 return "DW_TAG_try_block";
13032 case DW_TAG_variant_part:
13033 return "DW_TAG_variant_part";
13034 case DW_TAG_variable:
13035 return "DW_TAG_variable";
13036 case DW_TAG_volatile_type:
13037 return "DW_TAG_volatile_type";
d9fa45fe
DC
13038 case DW_TAG_dwarf_procedure:
13039 return "DW_TAG_dwarf_procedure";
13040 case DW_TAG_restrict_type:
13041 return "DW_TAG_restrict_type";
13042 case DW_TAG_interface_type:
13043 return "DW_TAG_interface_type";
13044 case DW_TAG_namespace:
13045 return "DW_TAG_namespace";
13046 case DW_TAG_imported_module:
13047 return "DW_TAG_imported_module";
13048 case DW_TAG_unspecified_type:
13049 return "DW_TAG_unspecified_type";
13050 case DW_TAG_partial_unit:
13051 return "DW_TAG_partial_unit";
13052 case DW_TAG_imported_unit:
13053 return "DW_TAG_imported_unit";
b7619582
GF
13054 case DW_TAG_condition:
13055 return "DW_TAG_condition";
13056 case DW_TAG_shared_type:
13057 return "DW_TAG_shared_type";
348e048f
DE
13058 case DW_TAG_type_unit:
13059 return "DW_TAG_type_unit";
c906108c
SS
13060 case DW_TAG_MIPS_loop:
13061 return "DW_TAG_MIPS_loop";
b7619582
GF
13062 case DW_TAG_HP_array_descriptor:
13063 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13064 case DW_TAG_format_label:
13065 return "DW_TAG_format_label";
13066 case DW_TAG_function_template:
13067 return "DW_TAG_function_template";
13068 case DW_TAG_class_template:
13069 return "DW_TAG_class_template";
b7619582
GF
13070 case DW_TAG_GNU_BINCL:
13071 return "DW_TAG_GNU_BINCL";
13072 case DW_TAG_GNU_EINCL:
13073 return "DW_TAG_GNU_EINCL";
13074 case DW_TAG_upc_shared_type:
13075 return "DW_TAG_upc_shared_type";
13076 case DW_TAG_upc_strict_type:
13077 return "DW_TAG_upc_strict_type";
13078 case DW_TAG_upc_relaxed_type:
13079 return "DW_TAG_upc_relaxed_type";
13080 case DW_TAG_PGI_kanji_type:
13081 return "DW_TAG_PGI_kanji_type";
13082 case DW_TAG_PGI_interface_block:
13083 return "DW_TAG_PGI_interface_block";
96408a79
SA
13084 case DW_TAG_GNU_call_site:
13085 return "DW_TAG_GNU_call_site";
c906108c
SS
13086 default:
13087 return "DW_TAG_<unknown>";
13088 }
13089}
13090
13091/* Convert a DWARF attribute code into its string name. */
13092
13093static char *
aa1ee363 13094dwarf_attr_name (unsigned attr)
c906108c
SS
13095{
13096 switch (attr)
13097 {
13098 case DW_AT_sibling:
13099 return "DW_AT_sibling";
13100 case DW_AT_location:
13101 return "DW_AT_location";
13102 case DW_AT_name:
13103 return "DW_AT_name";
13104 case DW_AT_ordering:
13105 return "DW_AT_ordering";
13106 case DW_AT_subscr_data:
13107 return "DW_AT_subscr_data";
13108 case DW_AT_byte_size:
13109 return "DW_AT_byte_size";
13110 case DW_AT_bit_offset:
13111 return "DW_AT_bit_offset";
13112 case DW_AT_bit_size:
13113 return "DW_AT_bit_size";
13114 case DW_AT_element_list:
13115 return "DW_AT_element_list";
13116 case DW_AT_stmt_list:
13117 return "DW_AT_stmt_list";
13118 case DW_AT_low_pc:
13119 return "DW_AT_low_pc";
13120 case DW_AT_high_pc:
13121 return "DW_AT_high_pc";
13122 case DW_AT_language:
13123 return "DW_AT_language";
13124 case DW_AT_member:
13125 return "DW_AT_member";
13126 case DW_AT_discr:
13127 return "DW_AT_discr";
13128 case DW_AT_discr_value:
13129 return "DW_AT_discr_value";
13130 case DW_AT_visibility:
13131 return "DW_AT_visibility";
13132 case DW_AT_import:
13133 return "DW_AT_import";
13134 case DW_AT_string_length:
13135 return "DW_AT_string_length";
13136 case DW_AT_common_reference:
13137 return "DW_AT_common_reference";
13138 case DW_AT_comp_dir:
13139 return "DW_AT_comp_dir";
13140 case DW_AT_const_value:
13141 return "DW_AT_const_value";
13142 case DW_AT_containing_type:
13143 return "DW_AT_containing_type";
13144 case DW_AT_default_value:
13145 return "DW_AT_default_value";
13146 case DW_AT_inline:
13147 return "DW_AT_inline";
13148 case DW_AT_is_optional:
13149 return "DW_AT_is_optional";
13150 case DW_AT_lower_bound:
13151 return "DW_AT_lower_bound";
13152 case DW_AT_producer:
13153 return "DW_AT_producer";
13154 case DW_AT_prototyped:
13155 return "DW_AT_prototyped";
13156 case DW_AT_return_addr:
13157 return "DW_AT_return_addr";
13158 case DW_AT_start_scope:
13159 return "DW_AT_start_scope";
09fa0d7c
JK
13160 case DW_AT_bit_stride:
13161 return "DW_AT_bit_stride";
c906108c
SS
13162 case DW_AT_upper_bound:
13163 return "DW_AT_upper_bound";
13164 case DW_AT_abstract_origin:
13165 return "DW_AT_abstract_origin";
13166 case DW_AT_accessibility:
13167 return "DW_AT_accessibility";
13168 case DW_AT_address_class:
13169 return "DW_AT_address_class";
13170 case DW_AT_artificial:
13171 return "DW_AT_artificial";
13172 case DW_AT_base_types:
13173 return "DW_AT_base_types";
13174 case DW_AT_calling_convention:
13175 return "DW_AT_calling_convention";
13176 case DW_AT_count:
13177 return "DW_AT_count";
13178 case DW_AT_data_member_location:
13179 return "DW_AT_data_member_location";
13180 case DW_AT_decl_column:
13181 return "DW_AT_decl_column";
13182 case DW_AT_decl_file:
13183 return "DW_AT_decl_file";
13184 case DW_AT_decl_line:
13185 return "DW_AT_decl_line";
13186 case DW_AT_declaration:
13187 return "DW_AT_declaration";
13188 case DW_AT_discr_list:
13189 return "DW_AT_discr_list";
13190 case DW_AT_encoding:
13191 return "DW_AT_encoding";
13192 case DW_AT_external:
13193 return "DW_AT_external";
13194 case DW_AT_frame_base:
13195 return "DW_AT_frame_base";
13196 case DW_AT_friend:
13197 return "DW_AT_friend";
13198 case DW_AT_identifier_case:
13199 return "DW_AT_identifier_case";
13200 case DW_AT_macro_info:
13201 return "DW_AT_macro_info";
13202 case DW_AT_namelist_items:
13203 return "DW_AT_namelist_items";
13204 case DW_AT_priority:
13205 return "DW_AT_priority";
13206 case DW_AT_segment:
13207 return "DW_AT_segment";
13208 case DW_AT_specification:
13209 return "DW_AT_specification";
13210 case DW_AT_static_link:
13211 return "DW_AT_static_link";
13212 case DW_AT_type:
13213 return "DW_AT_type";
13214 case DW_AT_use_location:
13215 return "DW_AT_use_location";
13216 case DW_AT_variable_parameter:
13217 return "DW_AT_variable_parameter";
13218 case DW_AT_virtuality:
13219 return "DW_AT_virtuality";
13220 case DW_AT_vtable_elem_location:
13221 return "DW_AT_vtable_elem_location";
b7619582 13222 /* DWARF 3 values. */
d9fa45fe
DC
13223 case DW_AT_allocated:
13224 return "DW_AT_allocated";
13225 case DW_AT_associated:
13226 return "DW_AT_associated";
13227 case DW_AT_data_location:
13228 return "DW_AT_data_location";
09fa0d7c
JK
13229 case DW_AT_byte_stride:
13230 return "DW_AT_byte_stride";
d9fa45fe
DC
13231 case DW_AT_entry_pc:
13232 return "DW_AT_entry_pc";
13233 case DW_AT_use_UTF8:
13234 return "DW_AT_use_UTF8";
13235 case DW_AT_extension:
13236 return "DW_AT_extension";
13237 case DW_AT_ranges:
13238 return "DW_AT_ranges";
13239 case DW_AT_trampoline:
13240 return "DW_AT_trampoline";
13241 case DW_AT_call_column:
13242 return "DW_AT_call_column";
13243 case DW_AT_call_file:
13244 return "DW_AT_call_file";
13245 case DW_AT_call_line:
13246 return "DW_AT_call_line";
b7619582
GF
13247 case DW_AT_description:
13248 return "DW_AT_description";
13249 case DW_AT_binary_scale:
13250 return "DW_AT_binary_scale";
13251 case DW_AT_decimal_scale:
13252 return "DW_AT_decimal_scale";
13253 case DW_AT_small:
13254 return "DW_AT_small";
13255 case DW_AT_decimal_sign:
13256 return "DW_AT_decimal_sign";
13257 case DW_AT_digit_count:
13258 return "DW_AT_digit_count";
13259 case DW_AT_picture_string:
13260 return "DW_AT_picture_string";
13261 case DW_AT_mutable:
13262 return "DW_AT_mutable";
13263 case DW_AT_threads_scaled:
13264 return "DW_AT_threads_scaled";
13265 case DW_AT_explicit:
13266 return "DW_AT_explicit";
13267 case DW_AT_object_pointer:
13268 return "DW_AT_object_pointer";
13269 case DW_AT_endianity:
13270 return "DW_AT_endianity";
13271 case DW_AT_elemental:
13272 return "DW_AT_elemental";
13273 case DW_AT_pure:
13274 return "DW_AT_pure";
13275 case DW_AT_recursive:
13276 return "DW_AT_recursive";
348e048f
DE
13277 /* DWARF 4 values. */
13278 case DW_AT_signature:
13279 return "DW_AT_signature";
31ef98ae
TT
13280 case DW_AT_linkage_name:
13281 return "DW_AT_linkage_name";
b7619582 13282 /* SGI/MIPS extensions. */
c764a876 13283#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13284 case DW_AT_MIPS_fde:
13285 return "DW_AT_MIPS_fde";
c764a876 13286#endif
c906108c
SS
13287 case DW_AT_MIPS_loop_begin:
13288 return "DW_AT_MIPS_loop_begin";
13289 case DW_AT_MIPS_tail_loop_begin:
13290 return "DW_AT_MIPS_tail_loop_begin";
13291 case DW_AT_MIPS_epilog_begin:
13292 return "DW_AT_MIPS_epilog_begin";
13293 case DW_AT_MIPS_loop_unroll_factor:
13294 return "DW_AT_MIPS_loop_unroll_factor";
13295 case DW_AT_MIPS_software_pipeline_depth:
13296 return "DW_AT_MIPS_software_pipeline_depth";
13297 case DW_AT_MIPS_linkage_name:
13298 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13299 case DW_AT_MIPS_stride:
13300 return "DW_AT_MIPS_stride";
13301 case DW_AT_MIPS_abstract_name:
13302 return "DW_AT_MIPS_abstract_name";
13303 case DW_AT_MIPS_clone_origin:
13304 return "DW_AT_MIPS_clone_origin";
13305 case DW_AT_MIPS_has_inlines:
13306 return "DW_AT_MIPS_has_inlines";
b7619582 13307 /* HP extensions. */
c764a876 13308#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13309 case DW_AT_HP_block_index:
13310 return "DW_AT_HP_block_index";
c764a876 13311#endif
b7619582
GF
13312 case DW_AT_HP_unmodifiable:
13313 return "DW_AT_HP_unmodifiable";
13314 case DW_AT_HP_actuals_stmt_list:
13315 return "DW_AT_HP_actuals_stmt_list";
13316 case DW_AT_HP_proc_per_section:
13317 return "DW_AT_HP_proc_per_section";
13318 case DW_AT_HP_raw_data_ptr:
13319 return "DW_AT_HP_raw_data_ptr";
13320 case DW_AT_HP_pass_by_reference:
13321 return "DW_AT_HP_pass_by_reference";
13322 case DW_AT_HP_opt_level:
13323 return "DW_AT_HP_opt_level";
13324 case DW_AT_HP_prof_version_id:
13325 return "DW_AT_HP_prof_version_id";
13326 case DW_AT_HP_opt_flags:
13327 return "DW_AT_HP_opt_flags";
13328 case DW_AT_HP_cold_region_low_pc:
13329 return "DW_AT_HP_cold_region_low_pc";
13330 case DW_AT_HP_cold_region_high_pc:
13331 return "DW_AT_HP_cold_region_high_pc";
13332 case DW_AT_HP_all_variables_modifiable:
13333 return "DW_AT_HP_all_variables_modifiable";
13334 case DW_AT_HP_linkage_name:
13335 return "DW_AT_HP_linkage_name";
13336 case DW_AT_HP_prof_flags:
13337 return "DW_AT_HP_prof_flags";
13338 /* GNU extensions. */
c906108c
SS
13339 case DW_AT_sf_names:
13340 return "DW_AT_sf_names";
13341 case DW_AT_src_info:
13342 return "DW_AT_src_info";
13343 case DW_AT_mac_info:
13344 return "DW_AT_mac_info";
13345 case DW_AT_src_coords:
13346 return "DW_AT_src_coords";
13347 case DW_AT_body_begin:
13348 return "DW_AT_body_begin";
13349 case DW_AT_body_end:
13350 return "DW_AT_body_end";
f5f8a009
EZ
13351 case DW_AT_GNU_vector:
13352 return "DW_AT_GNU_vector";
2de00c64
DE
13353 case DW_AT_GNU_odr_signature:
13354 return "DW_AT_GNU_odr_signature";
b7619582
GF
13355 /* VMS extensions. */
13356 case DW_AT_VMS_rtnbeg_pd_address:
13357 return "DW_AT_VMS_rtnbeg_pd_address";
13358 /* UPC extension. */
13359 case DW_AT_upc_threads_scaled:
13360 return "DW_AT_upc_threads_scaled";
13361 /* PGI (STMicroelectronics) extensions. */
13362 case DW_AT_PGI_lbase:
13363 return "DW_AT_PGI_lbase";
13364 case DW_AT_PGI_soffset:
13365 return "DW_AT_PGI_soffset";
13366 case DW_AT_PGI_lstride:
13367 return "DW_AT_PGI_lstride";
c906108c
SS
13368 default:
13369 return "DW_AT_<unknown>";
13370 }
13371}
13372
13373/* Convert a DWARF value form code into its string name. */
13374
13375static char *
aa1ee363 13376dwarf_form_name (unsigned form)
c906108c
SS
13377{
13378 switch (form)
13379 {
13380 case DW_FORM_addr:
13381 return "DW_FORM_addr";
13382 case DW_FORM_block2:
13383 return "DW_FORM_block2";
13384 case DW_FORM_block4:
13385 return "DW_FORM_block4";
13386 case DW_FORM_data2:
13387 return "DW_FORM_data2";
13388 case DW_FORM_data4:
13389 return "DW_FORM_data4";
13390 case DW_FORM_data8:
13391 return "DW_FORM_data8";
13392 case DW_FORM_string:
13393 return "DW_FORM_string";
13394 case DW_FORM_block:
13395 return "DW_FORM_block";
13396 case DW_FORM_block1:
13397 return "DW_FORM_block1";
13398 case DW_FORM_data1:
13399 return "DW_FORM_data1";
13400 case DW_FORM_flag:
13401 return "DW_FORM_flag";
13402 case DW_FORM_sdata:
13403 return "DW_FORM_sdata";
13404 case DW_FORM_strp:
13405 return "DW_FORM_strp";
13406 case DW_FORM_udata:
13407 return "DW_FORM_udata";
13408 case DW_FORM_ref_addr:
13409 return "DW_FORM_ref_addr";
13410 case DW_FORM_ref1:
13411 return "DW_FORM_ref1";
13412 case DW_FORM_ref2:
13413 return "DW_FORM_ref2";
13414 case DW_FORM_ref4:
13415 return "DW_FORM_ref4";
13416 case DW_FORM_ref8:
13417 return "DW_FORM_ref8";
13418 case DW_FORM_ref_udata:
13419 return "DW_FORM_ref_udata";
13420 case DW_FORM_indirect:
13421 return "DW_FORM_indirect";
348e048f
DE
13422 case DW_FORM_sec_offset:
13423 return "DW_FORM_sec_offset";
13424 case DW_FORM_exprloc:
13425 return "DW_FORM_exprloc";
13426 case DW_FORM_flag_present:
13427 return "DW_FORM_flag_present";
55f1336d
TT
13428 case DW_FORM_ref_sig8:
13429 return "DW_FORM_ref_sig8";
c906108c
SS
13430 default:
13431 return "DW_FORM_<unknown>";
13432 }
13433}
13434
13435/* Convert a DWARF stack opcode into its string name. */
13436
9eae7c52 13437const char *
b1bfef65 13438dwarf_stack_op_name (unsigned op)
c906108c
SS
13439{
13440 switch (op)
13441 {
13442 case DW_OP_addr:
13443 return "DW_OP_addr";
13444 case DW_OP_deref:
13445 return "DW_OP_deref";
13446 case DW_OP_const1u:
13447 return "DW_OP_const1u";
13448 case DW_OP_const1s:
13449 return "DW_OP_const1s";
13450 case DW_OP_const2u:
13451 return "DW_OP_const2u";
13452 case DW_OP_const2s:
13453 return "DW_OP_const2s";
13454 case DW_OP_const4u:
13455 return "DW_OP_const4u";
13456 case DW_OP_const4s:
13457 return "DW_OP_const4s";
13458 case DW_OP_const8u:
13459 return "DW_OP_const8u";
13460 case DW_OP_const8s:
13461 return "DW_OP_const8s";
13462 case DW_OP_constu:
13463 return "DW_OP_constu";
13464 case DW_OP_consts:
13465 return "DW_OP_consts";
13466 case DW_OP_dup:
13467 return "DW_OP_dup";
13468 case DW_OP_drop:
13469 return "DW_OP_drop";
13470 case DW_OP_over:
13471 return "DW_OP_over";
13472 case DW_OP_pick:
13473 return "DW_OP_pick";
13474 case DW_OP_swap:
13475 return "DW_OP_swap";
13476 case DW_OP_rot:
13477 return "DW_OP_rot";
13478 case DW_OP_xderef:
13479 return "DW_OP_xderef";
13480 case DW_OP_abs:
13481 return "DW_OP_abs";
13482 case DW_OP_and:
13483 return "DW_OP_and";
13484 case DW_OP_div:
13485 return "DW_OP_div";
13486 case DW_OP_minus:
13487 return "DW_OP_minus";
13488 case DW_OP_mod:
13489 return "DW_OP_mod";
13490 case DW_OP_mul:
13491 return "DW_OP_mul";
13492 case DW_OP_neg:
13493 return "DW_OP_neg";
13494 case DW_OP_not:
13495 return "DW_OP_not";
13496 case DW_OP_or:
13497 return "DW_OP_or";
13498 case DW_OP_plus:
13499 return "DW_OP_plus";
13500 case DW_OP_plus_uconst:
13501 return "DW_OP_plus_uconst";
13502 case DW_OP_shl:
13503 return "DW_OP_shl";
13504 case DW_OP_shr:
13505 return "DW_OP_shr";
13506 case DW_OP_shra:
13507 return "DW_OP_shra";
13508 case DW_OP_xor:
13509 return "DW_OP_xor";
13510 case DW_OP_bra:
13511 return "DW_OP_bra";
13512 case DW_OP_eq:
13513 return "DW_OP_eq";
13514 case DW_OP_ge:
13515 return "DW_OP_ge";
13516 case DW_OP_gt:
13517 return "DW_OP_gt";
13518 case DW_OP_le:
13519 return "DW_OP_le";
13520 case DW_OP_lt:
13521 return "DW_OP_lt";
13522 case DW_OP_ne:
13523 return "DW_OP_ne";
13524 case DW_OP_skip:
13525 return "DW_OP_skip";
13526 case DW_OP_lit0:
13527 return "DW_OP_lit0";
13528 case DW_OP_lit1:
13529 return "DW_OP_lit1";
13530 case DW_OP_lit2:
13531 return "DW_OP_lit2";
13532 case DW_OP_lit3:
13533 return "DW_OP_lit3";
13534 case DW_OP_lit4:
13535 return "DW_OP_lit4";
13536 case DW_OP_lit5:
13537 return "DW_OP_lit5";
13538 case DW_OP_lit6:
13539 return "DW_OP_lit6";
13540 case DW_OP_lit7:
13541 return "DW_OP_lit7";
13542 case DW_OP_lit8:
13543 return "DW_OP_lit8";
13544 case DW_OP_lit9:
13545 return "DW_OP_lit9";
13546 case DW_OP_lit10:
13547 return "DW_OP_lit10";
13548 case DW_OP_lit11:
13549 return "DW_OP_lit11";
13550 case DW_OP_lit12:
13551 return "DW_OP_lit12";
13552 case DW_OP_lit13:
13553 return "DW_OP_lit13";
13554 case DW_OP_lit14:
13555 return "DW_OP_lit14";
13556 case DW_OP_lit15:
13557 return "DW_OP_lit15";
13558 case DW_OP_lit16:
13559 return "DW_OP_lit16";
13560 case DW_OP_lit17:
13561 return "DW_OP_lit17";
13562 case DW_OP_lit18:
13563 return "DW_OP_lit18";
13564 case DW_OP_lit19:
13565 return "DW_OP_lit19";
13566 case DW_OP_lit20:
13567 return "DW_OP_lit20";
13568 case DW_OP_lit21:
13569 return "DW_OP_lit21";
13570 case DW_OP_lit22:
13571 return "DW_OP_lit22";
13572 case DW_OP_lit23:
13573 return "DW_OP_lit23";
13574 case DW_OP_lit24:
13575 return "DW_OP_lit24";
13576 case DW_OP_lit25:
13577 return "DW_OP_lit25";
13578 case DW_OP_lit26:
13579 return "DW_OP_lit26";
13580 case DW_OP_lit27:
13581 return "DW_OP_lit27";
13582 case DW_OP_lit28:
13583 return "DW_OP_lit28";
13584 case DW_OP_lit29:
13585 return "DW_OP_lit29";
13586 case DW_OP_lit30:
13587 return "DW_OP_lit30";
13588 case DW_OP_lit31:
13589 return "DW_OP_lit31";
13590 case DW_OP_reg0:
13591 return "DW_OP_reg0";
13592 case DW_OP_reg1:
13593 return "DW_OP_reg1";
13594 case DW_OP_reg2:
13595 return "DW_OP_reg2";
13596 case DW_OP_reg3:
13597 return "DW_OP_reg3";
13598 case DW_OP_reg4:
13599 return "DW_OP_reg4";
13600 case DW_OP_reg5:
13601 return "DW_OP_reg5";
13602 case DW_OP_reg6:
13603 return "DW_OP_reg6";
13604 case DW_OP_reg7:
13605 return "DW_OP_reg7";
13606 case DW_OP_reg8:
13607 return "DW_OP_reg8";
13608 case DW_OP_reg9:
13609 return "DW_OP_reg9";
13610 case DW_OP_reg10:
13611 return "DW_OP_reg10";
13612 case DW_OP_reg11:
13613 return "DW_OP_reg11";
13614 case DW_OP_reg12:
13615 return "DW_OP_reg12";
13616 case DW_OP_reg13:
13617 return "DW_OP_reg13";
13618 case DW_OP_reg14:
13619 return "DW_OP_reg14";
13620 case DW_OP_reg15:
13621 return "DW_OP_reg15";
13622 case DW_OP_reg16:
13623 return "DW_OP_reg16";
13624 case DW_OP_reg17:
13625 return "DW_OP_reg17";
13626 case DW_OP_reg18:
13627 return "DW_OP_reg18";
13628 case DW_OP_reg19:
13629 return "DW_OP_reg19";
13630 case DW_OP_reg20:
13631 return "DW_OP_reg20";
13632 case DW_OP_reg21:
13633 return "DW_OP_reg21";
13634 case DW_OP_reg22:
13635 return "DW_OP_reg22";
13636 case DW_OP_reg23:
13637 return "DW_OP_reg23";
13638 case DW_OP_reg24:
13639 return "DW_OP_reg24";
13640 case DW_OP_reg25:
13641 return "DW_OP_reg25";
13642 case DW_OP_reg26:
13643 return "DW_OP_reg26";
13644 case DW_OP_reg27:
13645 return "DW_OP_reg27";
13646 case DW_OP_reg28:
13647 return "DW_OP_reg28";
13648 case DW_OP_reg29:
13649 return "DW_OP_reg29";
13650 case DW_OP_reg30:
13651 return "DW_OP_reg30";
13652 case DW_OP_reg31:
13653 return "DW_OP_reg31";
13654 case DW_OP_breg0:
13655 return "DW_OP_breg0";
13656 case DW_OP_breg1:
13657 return "DW_OP_breg1";
13658 case DW_OP_breg2:
13659 return "DW_OP_breg2";
13660 case DW_OP_breg3:
13661 return "DW_OP_breg3";
13662 case DW_OP_breg4:
13663 return "DW_OP_breg4";
13664 case DW_OP_breg5:
13665 return "DW_OP_breg5";
13666 case DW_OP_breg6:
13667 return "DW_OP_breg6";
13668 case DW_OP_breg7:
13669 return "DW_OP_breg7";
13670 case DW_OP_breg8:
13671 return "DW_OP_breg8";
13672 case DW_OP_breg9:
13673 return "DW_OP_breg9";
13674 case DW_OP_breg10:
13675 return "DW_OP_breg10";
13676 case DW_OP_breg11:
13677 return "DW_OP_breg11";
13678 case DW_OP_breg12:
13679 return "DW_OP_breg12";
13680 case DW_OP_breg13:
13681 return "DW_OP_breg13";
13682 case DW_OP_breg14:
13683 return "DW_OP_breg14";
13684 case DW_OP_breg15:
13685 return "DW_OP_breg15";
13686 case DW_OP_breg16:
13687 return "DW_OP_breg16";
13688 case DW_OP_breg17:
13689 return "DW_OP_breg17";
13690 case DW_OP_breg18:
13691 return "DW_OP_breg18";
13692 case DW_OP_breg19:
13693 return "DW_OP_breg19";
13694 case DW_OP_breg20:
13695 return "DW_OP_breg20";
13696 case DW_OP_breg21:
13697 return "DW_OP_breg21";
13698 case DW_OP_breg22:
13699 return "DW_OP_breg22";
13700 case DW_OP_breg23:
13701 return "DW_OP_breg23";
13702 case DW_OP_breg24:
13703 return "DW_OP_breg24";
13704 case DW_OP_breg25:
13705 return "DW_OP_breg25";
13706 case DW_OP_breg26:
13707 return "DW_OP_breg26";
13708 case DW_OP_breg27:
13709 return "DW_OP_breg27";
13710 case DW_OP_breg28:
13711 return "DW_OP_breg28";
13712 case DW_OP_breg29:
13713 return "DW_OP_breg29";
13714 case DW_OP_breg30:
13715 return "DW_OP_breg30";
13716 case DW_OP_breg31:
13717 return "DW_OP_breg31";
13718 case DW_OP_regx:
13719 return "DW_OP_regx";
13720 case DW_OP_fbreg:
13721 return "DW_OP_fbreg";
13722 case DW_OP_bregx:
13723 return "DW_OP_bregx";
13724 case DW_OP_piece:
13725 return "DW_OP_piece";
13726 case DW_OP_deref_size:
13727 return "DW_OP_deref_size";
13728 case DW_OP_xderef_size:
13729 return "DW_OP_xderef_size";
13730 case DW_OP_nop:
13731 return "DW_OP_nop";
b7619582 13732 /* DWARF 3 extensions. */
ed348acc
EZ
13733 case DW_OP_push_object_address:
13734 return "DW_OP_push_object_address";
13735 case DW_OP_call2:
13736 return "DW_OP_call2";
13737 case DW_OP_call4:
13738 return "DW_OP_call4";
13739 case DW_OP_call_ref:
13740 return "DW_OP_call_ref";
b7619582
GF
13741 case DW_OP_form_tls_address:
13742 return "DW_OP_form_tls_address";
13743 case DW_OP_call_frame_cfa:
13744 return "DW_OP_call_frame_cfa";
13745 case DW_OP_bit_piece:
13746 return "DW_OP_bit_piece";
9eae7c52
TT
13747 /* DWARF 4 extensions. */
13748 case DW_OP_implicit_value:
13749 return "DW_OP_implicit_value";
13750 case DW_OP_stack_value:
13751 return "DW_OP_stack_value";
13752 /* GNU extensions. */
ed348acc
EZ
13753 case DW_OP_GNU_push_tls_address:
13754 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13755 case DW_OP_GNU_uninit:
13756 return "DW_OP_GNU_uninit";
589b4a32
DE
13757 case DW_OP_GNU_encoded_addr:
13758 return "DW_OP_GNU_encoded_addr";
8cf6f0b1
TT
13759 case DW_OP_GNU_implicit_pointer:
13760 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13761 case DW_OP_GNU_entry_value:
13762 return "DW_OP_GNU_entry_value";
13763 case DW_OP_GNU_const_type:
13764 return "DW_OP_GNU_const_type";
13765 case DW_OP_GNU_regval_type:
13766 return "DW_OP_GNU_regval_type";
13767 case DW_OP_GNU_deref_type:
13768 return "DW_OP_GNU_deref_type";
13769 case DW_OP_GNU_convert:
13770 return "DW_OP_GNU_convert";
13771 case DW_OP_GNU_reinterpret:
13772 return "DW_OP_GNU_reinterpret";
589b4a32
DE
13773 case DW_OP_GNU_parameter_ref:
13774 return "DW_OP_GNU_parameter_ref";
c906108c 13775 default:
b1bfef65 13776 return NULL;
c906108c
SS
13777 }
13778}
13779
13780static char *
fba45db2 13781dwarf_bool_name (unsigned mybool)
c906108c
SS
13782{
13783 if (mybool)
13784 return "TRUE";
13785 else
13786 return "FALSE";
13787}
13788
13789/* Convert a DWARF type code into its string name. */
13790
13791static char *
aa1ee363 13792dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13793{
13794 switch (enc)
13795 {
b7619582
GF
13796 case DW_ATE_void:
13797 return "DW_ATE_void";
c906108c
SS
13798 case DW_ATE_address:
13799 return "DW_ATE_address";
13800 case DW_ATE_boolean:
13801 return "DW_ATE_boolean";
13802 case DW_ATE_complex_float:
13803 return "DW_ATE_complex_float";
13804 case DW_ATE_float:
13805 return "DW_ATE_float";
13806 case DW_ATE_signed:
13807 return "DW_ATE_signed";
13808 case DW_ATE_signed_char:
13809 return "DW_ATE_signed_char";
13810 case DW_ATE_unsigned:
13811 return "DW_ATE_unsigned";
13812 case DW_ATE_unsigned_char:
13813 return "DW_ATE_unsigned_char";
b7619582 13814 /* DWARF 3. */
d9fa45fe
DC
13815 case DW_ATE_imaginary_float:
13816 return "DW_ATE_imaginary_float";
b7619582
GF
13817 case DW_ATE_packed_decimal:
13818 return "DW_ATE_packed_decimal";
13819 case DW_ATE_numeric_string:
13820 return "DW_ATE_numeric_string";
13821 case DW_ATE_edited:
13822 return "DW_ATE_edited";
13823 case DW_ATE_signed_fixed:
13824 return "DW_ATE_signed_fixed";
13825 case DW_ATE_unsigned_fixed:
13826 return "DW_ATE_unsigned_fixed";
13827 case DW_ATE_decimal_float:
13828 return "DW_ATE_decimal_float";
75079b2b
TT
13829 /* DWARF 4. */
13830 case DW_ATE_UTF:
13831 return "DW_ATE_UTF";
b7619582
GF
13832 /* HP extensions. */
13833 case DW_ATE_HP_float80:
13834 return "DW_ATE_HP_float80";
13835 case DW_ATE_HP_complex_float80:
13836 return "DW_ATE_HP_complex_float80";
13837 case DW_ATE_HP_float128:
13838 return "DW_ATE_HP_float128";
13839 case DW_ATE_HP_complex_float128:
13840 return "DW_ATE_HP_complex_float128";
13841 case DW_ATE_HP_floathpintel:
13842 return "DW_ATE_HP_floathpintel";
13843 case DW_ATE_HP_imaginary_float80:
13844 return "DW_ATE_HP_imaginary_float80";
13845 case DW_ATE_HP_imaginary_float128:
13846 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13847 default:
13848 return "DW_ATE_<unknown>";
13849 }
13850}
13851
0963b4bd 13852/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13853
13854#if 0
13855static char *
aa1ee363 13856dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13857{
13858 switch (cfi_opc)
13859 {
13860 case DW_CFA_advance_loc:
13861 return "DW_CFA_advance_loc";
13862 case DW_CFA_offset:
13863 return "DW_CFA_offset";
13864 case DW_CFA_restore:
13865 return "DW_CFA_restore";
13866 case DW_CFA_nop:
13867 return "DW_CFA_nop";
13868 case DW_CFA_set_loc:
13869 return "DW_CFA_set_loc";
13870 case DW_CFA_advance_loc1:
13871 return "DW_CFA_advance_loc1";
13872 case DW_CFA_advance_loc2:
13873 return "DW_CFA_advance_loc2";
13874 case DW_CFA_advance_loc4:
13875 return "DW_CFA_advance_loc4";
13876 case DW_CFA_offset_extended:
13877 return "DW_CFA_offset_extended";
13878 case DW_CFA_restore_extended:
13879 return "DW_CFA_restore_extended";
13880 case DW_CFA_undefined:
13881 return "DW_CFA_undefined";
13882 case DW_CFA_same_value:
13883 return "DW_CFA_same_value";
13884 case DW_CFA_register:
13885 return "DW_CFA_register";
13886 case DW_CFA_remember_state:
13887 return "DW_CFA_remember_state";
13888 case DW_CFA_restore_state:
13889 return "DW_CFA_restore_state";
13890 case DW_CFA_def_cfa:
13891 return "DW_CFA_def_cfa";
13892 case DW_CFA_def_cfa_register:
13893 return "DW_CFA_def_cfa_register";
13894 case DW_CFA_def_cfa_offset:
13895 return "DW_CFA_def_cfa_offset";
b7619582 13896 /* DWARF 3. */
985cb1a3
JM
13897 case DW_CFA_def_cfa_expression:
13898 return "DW_CFA_def_cfa_expression";
13899 case DW_CFA_expression:
13900 return "DW_CFA_expression";
13901 case DW_CFA_offset_extended_sf:
13902 return "DW_CFA_offset_extended_sf";
13903 case DW_CFA_def_cfa_sf:
13904 return "DW_CFA_def_cfa_sf";
13905 case DW_CFA_def_cfa_offset_sf:
13906 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13907 case DW_CFA_val_offset:
13908 return "DW_CFA_val_offset";
13909 case DW_CFA_val_offset_sf:
13910 return "DW_CFA_val_offset_sf";
13911 case DW_CFA_val_expression:
13912 return "DW_CFA_val_expression";
13913 /* SGI/MIPS specific. */
c906108c
SS
13914 case DW_CFA_MIPS_advance_loc8:
13915 return "DW_CFA_MIPS_advance_loc8";
b7619582 13916 /* GNU extensions. */
985cb1a3
JM
13917 case DW_CFA_GNU_window_save:
13918 return "DW_CFA_GNU_window_save";
13919 case DW_CFA_GNU_args_size:
13920 return "DW_CFA_GNU_args_size";
13921 case DW_CFA_GNU_negative_offset_extended:
13922 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13923 default:
13924 return "DW_CFA_<unknown>";
13925 }
13926}
13927#endif
13928
f9aca02d 13929static void
d97bc12b 13930dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13931{
13932 unsigned int i;
13933
d97bc12b
DE
13934 print_spaces (indent, f);
13935 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 13936 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
13937
13938 if (die->parent != NULL)
13939 {
13940 print_spaces (indent, f);
13941 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 13942 die->parent->offset.sect_off);
d97bc12b
DE
13943 }
13944
13945 print_spaces (indent, f);
13946 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13947 dwarf_bool_name (die->child != NULL));
c906108c 13948
d97bc12b
DE
13949 print_spaces (indent, f);
13950 fprintf_unfiltered (f, " attributes:\n");
13951
c906108c
SS
13952 for (i = 0; i < die->num_attrs; ++i)
13953 {
d97bc12b
DE
13954 print_spaces (indent, f);
13955 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13956 dwarf_attr_name (die->attrs[i].name),
13957 dwarf_form_name (die->attrs[i].form));
d97bc12b 13958
c906108c
SS
13959 switch (die->attrs[i].form)
13960 {
13961 case DW_FORM_ref_addr:
13962 case DW_FORM_addr:
d97bc12b 13963 fprintf_unfiltered (f, "address: ");
5af949e3 13964 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13965 break;
13966 case DW_FORM_block2:
13967 case DW_FORM_block4:
13968 case DW_FORM_block:
13969 case DW_FORM_block1:
3e43a32a
MS
13970 fprintf_unfiltered (f, "block: size %d",
13971 DW_BLOCK (&die->attrs[i])->size);
c906108c 13972 break;
2dc7f7b3
TT
13973 case DW_FORM_exprloc:
13974 fprintf_unfiltered (f, "expression: size %u",
13975 DW_BLOCK (&die->attrs[i])->size);
13976 break;
10b3939b
DJ
13977 case DW_FORM_ref1:
13978 case DW_FORM_ref2:
13979 case DW_FORM_ref4:
d97bc12b 13980 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13981 (long) (DW_ADDR (&die->attrs[i])));
13982 break;
c906108c
SS
13983 case DW_FORM_data1:
13984 case DW_FORM_data2:
13985 case DW_FORM_data4:
ce5d95e1 13986 case DW_FORM_data8:
c906108c
SS
13987 case DW_FORM_udata:
13988 case DW_FORM_sdata:
43bbcdc2
PH
13989 fprintf_unfiltered (f, "constant: %s",
13990 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13991 break;
2dc7f7b3
TT
13992 case DW_FORM_sec_offset:
13993 fprintf_unfiltered (f, "section offset: %s",
13994 pulongest (DW_UNSND (&die->attrs[i])));
13995 break;
55f1336d 13996 case DW_FORM_ref_sig8:
348e048f
DE
13997 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13998 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 13999 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14000 else
14001 fprintf_unfiltered (f, "signatured type, offset: unknown");
14002 break;
c906108c 14003 case DW_FORM_string:
4bdf3d34 14004 case DW_FORM_strp:
8285870a 14005 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14006 DW_STRING (&die->attrs[i])
8285870a
JK
14007 ? DW_STRING (&die->attrs[i]) : "",
14008 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14009 break;
14010 case DW_FORM_flag:
14011 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14012 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14013 else
d97bc12b 14014 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14015 break;
2dc7f7b3
TT
14016 case DW_FORM_flag_present:
14017 fprintf_unfiltered (f, "flag: TRUE");
14018 break;
a8329558 14019 case DW_FORM_indirect:
0963b4bd
MS
14020 /* The reader will have reduced the indirect form to
14021 the "base form" so this form should not occur. */
3e43a32a
MS
14022 fprintf_unfiltered (f,
14023 "unexpected attribute form: DW_FORM_indirect");
a8329558 14024 break;
c906108c 14025 default:
d97bc12b 14026 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14027 die->attrs[i].form);
d97bc12b 14028 break;
c906108c 14029 }
d97bc12b 14030 fprintf_unfiltered (f, "\n");
c906108c
SS
14031 }
14032}
14033
f9aca02d 14034static void
d97bc12b 14035dump_die_for_error (struct die_info *die)
c906108c 14036{
d97bc12b
DE
14037 dump_die_shallow (gdb_stderr, 0, die);
14038}
14039
14040static void
14041dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14042{
14043 int indent = level * 4;
14044
14045 gdb_assert (die != NULL);
14046
14047 if (level >= max_level)
14048 return;
14049
14050 dump_die_shallow (f, indent, die);
14051
14052 if (die->child != NULL)
c906108c 14053 {
d97bc12b
DE
14054 print_spaces (indent, f);
14055 fprintf_unfiltered (f, " Children:");
14056 if (level + 1 < max_level)
14057 {
14058 fprintf_unfiltered (f, "\n");
14059 dump_die_1 (f, level + 1, max_level, die->child);
14060 }
14061 else
14062 {
3e43a32a
MS
14063 fprintf_unfiltered (f,
14064 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14065 }
14066 }
14067
14068 if (die->sibling != NULL && level > 0)
14069 {
14070 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14071 }
14072}
14073
d97bc12b
DE
14074/* This is called from the pdie macro in gdbinit.in.
14075 It's not static so gcc will keep a copy callable from gdb. */
14076
14077void
14078dump_die (struct die_info *die, int max_level)
14079{
14080 dump_die_1 (gdb_stdlog, 0, max_level, die);
14081}
14082
f9aca02d 14083static void
51545339 14084store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14085{
51545339 14086 void **slot;
c906108c 14087
b64f50a1
JK
14088 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14089 INSERT);
51545339
DJ
14090
14091 *slot = die;
c906108c
SS
14092}
14093
b64f50a1
JK
14094/* DW_ADDR is always stored already as sect_offset; despite for the forms
14095 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14096
93311388
DE
14097static int
14098is_ref_attr (struct attribute *attr)
c906108c 14099{
c906108c
SS
14100 switch (attr->form)
14101 {
14102 case DW_FORM_ref_addr:
c906108c
SS
14103 case DW_FORM_ref1:
14104 case DW_FORM_ref2:
14105 case DW_FORM_ref4:
613e1657 14106 case DW_FORM_ref8:
c906108c 14107 case DW_FORM_ref_udata:
93311388 14108 return 1;
c906108c 14109 default:
93311388 14110 return 0;
c906108c 14111 }
93311388
DE
14112}
14113
b64f50a1
JK
14114/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14115 required kind. */
14116
14117static sect_offset
93311388
DE
14118dwarf2_get_ref_die_offset (struct attribute *attr)
14119{
b64f50a1
JK
14120 sect_offset retval = { DW_ADDR (attr) };
14121
93311388 14122 if (is_ref_attr (attr))
b64f50a1 14123 return retval;
93311388 14124
b64f50a1 14125 retval.sect_off = 0;
93311388
DE
14126 complaint (&symfile_complaints,
14127 _("unsupported die ref attribute form: '%s'"),
14128 dwarf_form_name (attr->form));
b64f50a1 14129 return retval;
c906108c
SS
14130}
14131
43bbcdc2
PH
14132/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14133 * the value held by the attribute is not constant. */
a02abb62 14134
43bbcdc2 14135static LONGEST
a02abb62
JB
14136dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14137{
14138 if (attr->form == DW_FORM_sdata)
14139 return DW_SND (attr);
14140 else if (attr->form == DW_FORM_udata
14141 || attr->form == DW_FORM_data1
14142 || attr->form == DW_FORM_data2
14143 || attr->form == DW_FORM_data4
14144 || attr->form == DW_FORM_data8)
14145 return DW_UNSND (attr);
14146 else
14147 {
3e43a32a
MS
14148 complaint (&symfile_complaints,
14149 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14150 dwarf_form_name (attr->form));
14151 return default_value;
14152 }
14153}
14154
03dd20cc 14155/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14156 unit and add it to our queue.
14157 The result is non-zero if PER_CU was queued, otherwise the result is zero
14158 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14159
348e048f 14160static int
03dd20cc
DJ
14161maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14162 struct dwarf2_per_cu_data *per_cu)
14163{
98bfdba5
PA
14164 /* We may arrive here during partial symbol reading, if we need full
14165 DIEs to process an unusual case (e.g. template arguments). Do
14166 not queue PER_CU, just tell our caller to load its DIEs. */
14167 if (dwarf2_per_objfile->reading_partial_symbols)
14168 {
14169 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14170 return 1;
14171 return 0;
14172 }
14173
03dd20cc
DJ
14174 /* Mark the dependence relation so that we don't flush PER_CU
14175 too early. */
14176 dwarf2_add_dependence (this_cu, per_cu);
14177
14178 /* If it's already on the queue, we have nothing to do. */
14179 if (per_cu->queued)
348e048f 14180 return 0;
03dd20cc
DJ
14181
14182 /* If the compilation unit is already loaded, just mark it as
14183 used. */
14184 if (per_cu->cu != NULL)
14185 {
14186 per_cu->cu->last_used = 0;
348e048f 14187 return 0;
03dd20cc
DJ
14188 }
14189
14190 /* Add it to the queue. */
a0f42c21 14191 queue_comp_unit (per_cu);
348e048f
DE
14192
14193 return 1;
14194}
14195
14196/* Follow reference or signature attribute ATTR of SRC_DIE.
14197 On entry *REF_CU is the CU of SRC_DIE.
14198 On exit *REF_CU is the CU of the result. */
14199
14200static struct die_info *
14201follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14202 struct dwarf2_cu **ref_cu)
14203{
14204 struct die_info *die;
14205
14206 if (is_ref_attr (attr))
14207 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14208 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14209 die = follow_die_sig (src_die, attr, ref_cu);
14210 else
14211 {
14212 dump_die_for_error (src_die);
14213 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14214 (*ref_cu)->objfile->name);
14215 }
14216
14217 return die;
03dd20cc
DJ
14218}
14219
5c631832 14220/* Follow reference OFFSET.
673bfd45
DE
14221 On entry *REF_CU is the CU of the source die referencing OFFSET.
14222 On exit *REF_CU is the CU of the result.
14223 Returns NULL if OFFSET is invalid. */
f504f079 14224
f9aca02d 14225static struct die_info *
b64f50a1 14226follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14227{
10b3939b 14228 struct die_info temp_die;
f2f0e013 14229 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14230
348e048f
DE
14231 gdb_assert (cu->per_cu != NULL);
14232
98bfdba5
PA
14233 target_cu = cu;
14234
b0df02fd 14235 if (cu->per_cu->debug_types_section)
348e048f
DE
14236 {
14237 /* .debug_types CUs cannot reference anything outside their CU.
14238 If they need to, they have to reference a signatured type via
55f1336d 14239 DW_FORM_ref_sig8. */
348e048f 14240 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14241 return NULL;
348e048f
DE
14242 }
14243 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14244 {
14245 struct dwarf2_per_cu_data *per_cu;
9a619af0 14246
45452591 14247 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14248
14249 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14250 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14251 load_full_comp_unit (per_cu);
03dd20cc 14252
10b3939b
DJ
14253 target_cu = per_cu->cu;
14254 }
98bfdba5
PA
14255 else if (cu->dies == NULL)
14256 {
14257 /* We're loading full DIEs during partial symbol reading. */
14258 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14259 load_full_comp_unit (cu->per_cu);
98bfdba5 14260 }
c906108c 14261
f2f0e013 14262 *ref_cu = target_cu;
51545339 14263 temp_die.offset = offset;
b64f50a1 14264 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14265}
10b3939b 14266
5c631832
JK
14267/* Follow reference attribute ATTR of SRC_DIE.
14268 On entry *REF_CU is the CU of SRC_DIE.
14269 On exit *REF_CU is the CU of the result. */
14270
14271static struct die_info *
14272follow_die_ref (struct die_info *src_die, struct attribute *attr,
14273 struct dwarf2_cu **ref_cu)
14274{
b64f50a1 14275 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14276 struct dwarf2_cu *cu = *ref_cu;
14277 struct die_info *die;
14278
14279 die = follow_die_offset (offset, ref_cu);
14280 if (!die)
14281 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14282 "at 0x%x [in module %s]"),
b64f50a1 14283 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14284
5c631832
JK
14285 return die;
14286}
14287
d83e736b
JK
14288/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14289 Returned value is intended for DW_OP_call*. Returned
14290 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14291
14292struct dwarf2_locexpr_baton
b64f50a1 14293dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14294 struct dwarf2_per_cu_data *per_cu,
14295 CORE_ADDR (*get_frame_pc) (void *baton),
14296 void *baton)
5c631832 14297{
b64f50a1 14298 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14299 struct dwarf2_cu *cu;
5c631832
JK
14300 struct die_info *die;
14301 struct attribute *attr;
14302 struct dwarf2_locexpr_baton retval;
14303
8cf6f0b1
TT
14304 dw2_setup (per_cu->objfile);
14305
918dd910
JK
14306 if (per_cu->cu == NULL)
14307 load_cu (per_cu);
14308 cu = per_cu->cu;
14309
5c631832
JK
14310 die = follow_die_offset (offset, &cu);
14311 if (!die)
14312 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 14313 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14314
14315 attr = dwarf2_attr (die, DW_AT_location, cu);
14316 if (!attr)
14317 {
e103e986
JK
14318 /* DWARF: "If there is no such attribute, then there is no effect.".
14319 DATA is ignored if SIZE is 0. */
5c631832 14320
e103e986 14321 retval.data = NULL;
5c631832
JK
14322 retval.size = 0;
14323 }
8cf6f0b1
TT
14324 else if (attr_form_is_section_offset (attr))
14325 {
14326 struct dwarf2_loclist_baton loclist_baton;
14327 CORE_ADDR pc = (*get_frame_pc) (baton);
14328 size_t size;
14329
14330 fill_in_loclist_baton (cu, &loclist_baton, attr);
14331
14332 retval.data = dwarf2_find_location_expression (&loclist_baton,
14333 &size, pc);
14334 retval.size = size;
14335 }
5c631832
JK
14336 else
14337 {
14338 if (!attr_form_is_block (attr))
14339 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14340 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 14341 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14342
14343 retval.data = DW_BLOCK (attr)->data;
14344 retval.size = DW_BLOCK (attr)->size;
14345 }
14346 retval.per_cu = cu->per_cu;
918dd910 14347
918dd910
JK
14348 age_cached_comp_units ();
14349
5c631832 14350 return retval;
348e048f
DE
14351}
14352
8a9b8146
TT
14353/* Return the type of the DIE at DIE_OFFSET in the CU named by
14354 PER_CU. */
14355
14356struct type *
b64f50a1 14357dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
14358 struct dwarf2_per_cu_data *per_cu)
14359{
b64f50a1
JK
14360 sect_offset die_offset_sect;
14361
8a9b8146 14362 dw2_setup (per_cu->objfile);
b64f50a1
JK
14363
14364 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14365 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
14366}
14367
348e048f
DE
14368/* Follow the signature attribute ATTR in SRC_DIE.
14369 On entry *REF_CU is the CU of SRC_DIE.
14370 On exit *REF_CU is the CU of the result. */
14371
14372static struct die_info *
14373follow_die_sig (struct die_info *src_die, struct attribute *attr,
14374 struct dwarf2_cu **ref_cu)
14375{
14376 struct objfile *objfile = (*ref_cu)->objfile;
14377 struct die_info temp_die;
14378 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14379 struct dwarf2_cu *sig_cu;
14380 struct die_info *die;
14381
14382 /* sig_type will be NULL if the signatured type is missing from
14383 the debug info. */
14384 if (sig_type == NULL)
14385 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14386 "at 0x%x [in module %s]"),
b64f50a1 14387 src_die->offset.sect_off, objfile->name);
348e048f
DE
14388
14389 /* If necessary, add it to the queue and load its DIEs. */
14390
14391 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14392 read_signatured_type (sig_type);
348e048f
DE
14393
14394 gdb_assert (sig_type->per_cu.cu != NULL);
14395
14396 sig_cu = sig_type->per_cu.cu;
b64f50a1
JK
14397 temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14398 + sig_type->type_offset.cu_off);
14399 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14400 temp_die.offset.sect_off);
348e048f
DE
14401 if (die)
14402 {
14403 *ref_cu = sig_cu;
14404 return die;
14405 }
14406
3e43a32a
MS
14407 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14408 "from DIE at 0x%x [in module %s]"),
b64f50a1 14409 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
14410}
14411
14412/* Given an offset of a signatured type, return its signatured_type. */
14413
14414static struct signatured_type *
8b70b953
TT
14415lookup_signatured_type_at_offset (struct objfile *objfile,
14416 struct dwarf2_section_info *section,
b64f50a1 14417 sect_offset offset)
348e048f 14418{
b64f50a1 14419 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
14420 unsigned int length, initial_length_size;
14421 unsigned int sig_offset;
14422 struct signatured_type find_entry, *type_sig;
14423
14424 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14425 sig_offset = (initial_length_size
14426 + 2 /*version*/
14427 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14428 + 1 /*address_size*/);
14429 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14430 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14431
14432 /* This is only used to lookup previously recorded types.
14433 If we didn't find it, it's our bug. */
14434 gdb_assert (type_sig != NULL);
b64f50a1 14435 gdb_assert (offset.sect_off == type_sig->per_cu.offset.sect_off);
348e048f
DE
14436
14437 return type_sig;
14438}
14439
e5fe5e75 14440/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14441
14442static void
e5fe5e75 14443load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14444{
e5fe5e75
DE
14445 struct objfile *objfile = per_cu->objfile;
14446 struct dwarf2_section_info *sect = per_cu->debug_types_section;
b64f50a1 14447 sect_offset offset = per_cu->offset;
348e048f
DE
14448 struct signatured_type *type_sig;
14449
8b70b953 14450 dwarf2_read_section (objfile, sect);
be391dca 14451
348e048f 14452 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14453 hash table lookup. */
14454 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14455 the signature to assert we found the right one.
14456 Ok, but it's a lot of work. We should simplify things so any needed
14457 assert doesn't require all this clumsiness. */
8b70b953 14458 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14459
14460 gdb_assert (type_sig->per_cu.cu == NULL);
14461
a0f42c21 14462 read_signatured_type (type_sig);
348e048f
DE
14463
14464 gdb_assert (type_sig->per_cu.cu != NULL);
14465}
14466
14467/* Read in a signatured type and build its CU and DIEs. */
14468
14469static void
a0f42c21 14470read_signatured_type (struct signatured_type *type_sig)
348e048f 14471{
a0f42c21 14472 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14473 gdb_byte *types_ptr;
348e048f
DE
14474 struct die_reader_specs reader_specs;
14475 struct dwarf2_cu *cu;
14476 ULONGEST signature;
14477 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14478 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14479
8b70b953 14480 dwarf2_read_section (objfile, section);
b64f50a1 14481 types_ptr = section->buffer + type_sig->per_cu.offset.sect_off;
1fd400ff 14482
348e048f
DE
14483 gdb_assert (type_sig->per_cu.cu == NULL);
14484
9816fde3 14485 cu = xmalloc (sizeof (*cu));
23745b47 14486 init_one_comp_unit (cu, &type_sig->per_cu);
348e048f
DE
14487
14488 /* If an error occurs while loading, release our storage. */
68dc6402 14489 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14490
9ff913ba
DE
14491 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14492 &signature, NULL);
348e048f
DE
14493 gdb_assert (signature == type_sig->signature);
14494
14495 cu->die_hash
14496 = htab_create_alloc_ex (cu->header.length / 12,
14497 die_hash,
14498 die_eq,
14499 NULL,
14500 &cu->comp_unit_obstack,
14501 hashtab_obstack_allocate,
14502 dummy_obstack_deallocate);
14503
e5fe5e75 14504 dwarf2_read_abbrevs (cu);
348e048f
DE
14505 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14506
14507 init_cu_die_reader (&reader_specs, cu);
14508
14509 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14510 NULL /*parent*/);
14511
14512 /* We try not to read any attributes in this function, because not
9cdd5dbd 14513 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14514 table processing isn't initialized. But we have to set the CU language,
14515 or we won't be able to build types correctly. */
9816fde3 14516 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14517
14518 do_cleanups (back_to);
14519
14520 /* We've successfully allocated this compilation unit. Let our caller
14521 clean it up when finished with it. */
14522 discard_cleanups (free_cu_cleanup);
14523
c5b7e1cb 14524 /* Link this TU into read_in_chain. */
348e048f
DE
14525 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14526 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14527}
14528
c906108c
SS
14529/* Decode simple location descriptions.
14530 Given a pointer to a dwarf block that defines a location, compute
14531 the location and return the value.
14532
4cecd739
DJ
14533 NOTE drow/2003-11-18: This function is called in two situations
14534 now: for the address of static or global variables (partial symbols
14535 only) and for offsets into structures which are expected to be
14536 (more or less) constant. The partial symbol case should go away,
14537 and only the constant case should remain. That will let this
14538 function complain more accurately. A few special modes are allowed
14539 without complaint for global variables (for instance, global
14540 register values and thread-local values).
c906108c
SS
14541
14542 A location description containing no operations indicates that the
4cecd739 14543 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14544 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14545 callers will only want a very basic result and this can become a
21ae7a4d
JK
14546 complaint.
14547
14548 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14549
14550static CORE_ADDR
e7c27a73 14551decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14552{
e7c27a73 14553 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14554 int i;
14555 int size = blk->size;
14556 gdb_byte *data = blk->data;
14557 CORE_ADDR stack[64];
14558 int stacki;
14559 unsigned int bytes_read, unsnd;
14560 gdb_byte op;
c906108c 14561
21ae7a4d
JK
14562 i = 0;
14563 stacki = 0;
14564 stack[stacki] = 0;
14565 stack[++stacki] = 0;
14566
14567 while (i < size)
14568 {
14569 op = data[i++];
14570 switch (op)
14571 {
14572 case DW_OP_lit0:
14573 case DW_OP_lit1:
14574 case DW_OP_lit2:
14575 case DW_OP_lit3:
14576 case DW_OP_lit4:
14577 case DW_OP_lit5:
14578 case DW_OP_lit6:
14579 case DW_OP_lit7:
14580 case DW_OP_lit8:
14581 case DW_OP_lit9:
14582 case DW_OP_lit10:
14583 case DW_OP_lit11:
14584 case DW_OP_lit12:
14585 case DW_OP_lit13:
14586 case DW_OP_lit14:
14587 case DW_OP_lit15:
14588 case DW_OP_lit16:
14589 case DW_OP_lit17:
14590 case DW_OP_lit18:
14591 case DW_OP_lit19:
14592 case DW_OP_lit20:
14593 case DW_OP_lit21:
14594 case DW_OP_lit22:
14595 case DW_OP_lit23:
14596 case DW_OP_lit24:
14597 case DW_OP_lit25:
14598 case DW_OP_lit26:
14599 case DW_OP_lit27:
14600 case DW_OP_lit28:
14601 case DW_OP_lit29:
14602 case DW_OP_lit30:
14603 case DW_OP_lit31:
14604 stack[++stacki] = op - DW_OP_lit0;
14605 break;
f1bea926 14606
21ae7a4d
JK
14607 case DW_OP_reg0:
14608 case DW_OP_reg1:
14609 case DW_OP_reg2:
14610 case DW_OP_reg3:
14611 case DW_OP_reg4:
14612 case DW_OP_reg5:
14613 case DW_OP_reg6:
14614 case DW_OP_reg7:
14615 case DW_OP_reg8:
14616 case DW_OP_reg9:
14617 case DW_OP_reg10:
14618 case DW_OP_reg11:
14619 case DW_OP_reg12:
14620 case DW_OP_reg13:
14621 case DW_OP_reg14:
14622 case DW_OP_reg15:
14623 case DW_OP_reg16:
14624 case DW_OP_reg17:
14625 case DW_OP_reg18:
14626 case DW_OP_reg19:
14627 case DW_OP_reg20:
14628 case DW_OP_reg21:
14629 case DW_OP_reg22:
14630 case DW_OP_reg23:
14631 case DW_OP_reg24:
14632 case DW_OP_reg25:
14633 case DW_OP_reg26:
14634 case DW_OP_reg27:
14635 case DW_OP_reg28:
14636 case DW_OP_reg29:
14637 case DW_OP_reg30:
14638 case DW_OP_reg31:
14639 stack[++stacki] = op - DW_OP_reg0;
14640 if (i < size)
14641 dwarf2_complex_location_expr_complaint ();
14642 break;
c906108c 14643
21ae7a4d
JK
14644 case DW_OP_regx:
14645 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14646 i += bytes_read;
14647 stack[++stacki] = unsnd;
14648 if (i < size)
14649 dwarf2_complex_location_expr_complaint ();
14650 break;
c906108c 14651
21ae7a4d
JK
14652 case DW_OP_addr:
14653 stack[++stacki] = read_address (objfile->obfd, &data[i],
14654 cu, &bytes_read);
14655 i += bytes_read;
14656 break;
d53d4ac5 14657
21ae7a4d
JK
14658 case DW_OP_const1u:
14659 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14660 i += 1;
14661 break;
14662
14663 case DW_OP_const1s:
14664 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14665 i += 1;
14666 break;
14667
14668 case DW_OP_const2u:
14669 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14670 i += 2;
14671 break;
14672
14673 case DW_OP_const2s:
14674 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14675 i += 2;
14676 break;
d53d4ac5 14677
21ae7a4d
JK
14678 case DW_OP_const4u:
14679 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14680 i += 4;
14681 break;
14682
14683 case DW_OP_const4s:
14684 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14685 i += 4;
14686 break;
14687
585861ea
JK
14688 case DW_OP_const8u:
14689 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14690 i += 8;
14691 break;
14692
21ae7a4d
JK
14693 case DW_OP_constu:
14694 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14695 &bytes_read);
14696 i += bytes_read;
14697 break;
14698
14699 case DW_OP_consts:
14700 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14701 i += bytes_read;
14702 break;
14703
14704 case DW_OP_dup:
14705 stack[stacki + 1] = stack[stacki];
14706 stacki++;
14707 break;
14708
14709 case DW_OP_plus:
14710 stack[stacki - 1] += stack[stacki];
14711 stacki--;
14712 break;
14713
14714 case DW_OP_plus_uconst:
14715 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14716 &bytes_read);
14717 i += bytes_read;
14718 break;
14719
14720 case DW_OP_minus:
14721 stack[stacki - 1] -= stack[stacki];
14722 stacki--;
14723 break;
14724
14725 case DW_OP_deref:
14726 /* If we're not the last op, then we definitely can't encode
14727 this using GDB's address_class enum. This is valid for partial
14728 global symbols, although the variable's address will be bogus
14729 in the psymtab. */
14730 if (i < size)
14731 dwarf2_complex_location_expr_complaint ();
14732 break;
14733
14734 case DW_OP_GNU_push_tls_address:
14735 /* The top of the stack has the offset from the beginning
14736 of the thread control block at which the variable is located. */
14737 /* Nothing should follow this operator, so the top of stack would
14738 be returned. */
14739 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14740 address will be bogus in the psymtab. Make it always at least
14741 non-zero to not look as a variable garbage collected by linker
14742 which have DW_OP_addr 0. */
21ae7a4d
JK
14743 if (i < size)
14744 dwarf2_complex_location_expr_complaint ();
585861ea 14745 stack[stacki]++;
21ae7a4d
JK
14746 break;
14747
14748 case DW_OP_GNU_uninit:
14749 break;
14750
14751 default:
14752 {
14753 const char *name = dwarf_stack_op_name (op);
14754
14755 if (name)
14756 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14757 name);
14758 else
14759 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14760 op);
14761 }
14762
14763 return (stack[stacki]);
d53d4ac5 14764 }
3c6e0cb3 14765
21ae7a4d
JK
14766 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14767 outside of the allocated space. Also enforce minimum>0. */
14768 if (stacki >= ARRAY_SIZE (stack) - 1)
14769 {
14770 complaint (&symfile_complaints,
14771 _("location description stack overflow"));
14772 return 0;
14773 }
14774
14775 if (stacki <= 0)
14776 {
14777 complaint (&symfile_complaints,
14778 _("location description stack underflow"));
14779 return 0;
14780 }
14781 }
14782 return (stack[stacki]);
c906108c
SS
14783}
14784
14785/* memory allocation interface */
14786
c906108c 14787static struct dwarf_block *
7b5a2f43 14788dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14789{
14790 struct dwarf_block *blk;
14791
14792 blk = (struct dwarf_block *)
7b5a2f43 14793 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14794 return (blk);
14795}
14796
14797static struct abbrev_info *
f3dd6933 14798dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14799{
14800 struct abbrev_info *abbrev;
14801
f3dd6933
DJ
14802 abbrev = (struct abbrev_info *)
14803 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14804 memset (abbrev, 0, sizeof (struct abbrev_info));
14805 return (abbrev);
14806}
14807
14808static struct die_info *
b60c80d6 14809dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14810{
14811 struct die_info *die;
b60c80d6
DJ
14812 size_t size = sizeof (struct die_info);
14813
14814 if (num_attrs > 1)
14815 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14816
b60c80d6 14817 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14818 memset (die, 0, sizeof (struct die_info));
14819 return (die);
14820}
2e276125
JB
14821
14822\f
14823/* Macro support. */
14824
2e276125
JB
14825/* Return the full name of file number I in *LH's file name table.
14826 Use COMP_DIR as the name of the current directory of the
14827 compilation. The result is allocated using xmalloc; the caller is
14828 responsible for freeing it. */
14829static char *
14830file_full_name (int file, struct line_header *lh, const char *comp_dir)
14831{
6a83a1e6
EZ
14832 /* Is the file number a valid index into the line header's file name
14833 table? Remember that file numbers start with one, not zero. */
14834 if (1 <= file && file <= lh->num_file_names)
14835 {
14836 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14837
6a83a1e6
EZ
14838 if (IS_ABSOLUTE_PATH (fe->name))
14839 return xstrdup (fe->name);
14840 else
14841 {
14842 const char *dir;
14843 int dir_len;
14844 char *full_name;
14845
14846 if (fe->dir_index)
14847 dir = lh->include_dirs[fe->dir_index - 1];
14848 else
14849 dir = comp_dir;
14850
14851 if (dir)
14852 {
14853 dir_len = strlen (dir);
14854 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14855 strcpy (full_name, dir);
14856 full_name[dir_len] = '/';
14857 strcpy (full_name + dir_len + 1, fe->name);
14858 return full_name;
14859 }
14860 else
14861 return xstrdup (fe->name);
14862 }
14863 }
2e276125
JB
14864 else
14865 {
6a83a1e6
EZ
14866 /* The compiler produced a bogus file number. We can at least
14867 record the macro definitions made in the file, even if we
14868 won't be able to find the file by name. */
14869 char fake_name[80];
9a619af0 14870
6a83a1e6 14871 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14872
6e70227d 14873 complaint (&symfile_complaints,
6a83a1e6
EZ
14874 _("bad file number in macro information (%d)"),
14875 file);
2e276125 14876
6a83a1e6 14877 return xstrdup (fake_name);
2e276125
JB
14878 }
14879}
14880
14881
14882static struct macro_source_file *
14883macro_start_file (int file, int line,
14884 struct macro_source_file *current_file,
14885 const char *comp_dir,
14886 struct line_header *lh, struct objfile *objfile)
14887{
14888 /* The full name of this source file. */
14889 char *full_name = file_full_name (file, lh, comp_dir);
14890
14891 /* We don't create a macro table for this compilation unit
14892 at all until we actually get a filename. */
14893 if (! pending_macros)
4a146b47 14894 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14895 objfile->macro_cache);
2e276125
JB
14896
14897 if (! current_file)
14898 /* If we have no current file, then this must be the start_file
14899 directive for the compilation unit's main source file. */
14900 current_file = macro_set_main (pending_macros, full_name);
14901 else
14902 current_file = macro_include (current_file, line, full_name);
14903
14904 xfree (full_name);
6e70227d 14905
2e276125
JB
14906 return current_file;
14907}
14908
14909
14910/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14911 followed by a null byte. */
14912static char *
14913copy_string (const char *buf, int len)
14914{
14915 char *s = xmalloc (len + 1);
9a619af0 14916
2e276125
JB
14917 memcpy (s, buf, len);
14918 s[len] = '\0';
2e276125
JB
14919 return s;
14920}
14921
14922
14923static const char *
14924consume_improper_spaces (const char *p, const char *body)
14925{
14926 if (*p == ' ')
14927 {
4d3c2250 14928 complaint (&symfile_complaints,
3e43a32a
MS
14929 _("macro definition contains spaces "
14930 "in formal argument list:\n`%s'"),
4d3c2250 14931 body);
2e276125
JB
14932
14933 while (*p == ' ')
14934 p++;
14935 }
14936
14937 return p;
14938}
14939
14940
14941static void
14942parse_macro_definition (struct macro_source_file *file, int line,
14943 const char *body)
14944{
14945 const char *p;
14946
14947 /* The body string takes one of two forms. For object-like macro
14948 definitions, it should be:
14949
14950 <macro name> " " <definition>
14951
14952 For function-like macro definitions, it should be:
14953
14954 <macro name> "() " <definition>
14955 or
14956 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14957
14958 Spaces may appear only where explicitly indicated, and in the
14959 <definition>.
14960
14961 The Dwarf 2 spec says that an object-like macro's name is always
14962 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14963 the space when the macro's definition is the empty string.
2e276125
JB
14964
14965 The Dwarf 2 spec says that there should be no spaces between the
14966 formal arguments in a function-like macro's formal argument list,
14967 but versions of GCC around March 2002 include spaces after the
14968 commas. */
14969
14970
14971 /* Find the extent of the macro name. The macro name is terminated
14972 by either a space or null character (for an object-like macro) or
14973 an opening paren (for a function-like macro). */
14974 for (p = body; *p; p++)
14975 if (*p == ' ' || *p == '(')
14976 break;
14977
14978 if (*p == ' ' || *p == '\0')
14979 {
14980 /* It's an object-like macro. */
14981 int name_len = p - body;
14982 char *name = copy_string (body, name_len);
14983 const char *replacement;
14984
14985 if (*p == ' ')
14986 replacement = body + name_len + 1;
14987 else
14988 {
4d3c2250 14989 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14990 replacement = body + name_len;
14991 }
6e70227d 14992
2e276125
JB
14993 macro_define_object (file, line, name, replacement);
14994
14995 xfree (name);
14996 }
14997 else if (*p == '(')
14998 {
14999 /* It's a function-like macro. */
15000 char *name = copy_string (body, p - body);
15001 int argc = 0;
15002 int argv_size = 1;
15003 char **argv = xmalloc (argv_size * sizeof (*argv));
15004
15005 p++;
15006
15007 p = consume_improper_spaces (p, body);
15008
15009 /* Parse the formal argument list. */
15010 while (*p && *p != ')')
15011 {
15012 /* Find the extent of the current argument name. */
15013 const char *arg_start = p;
15014
15015 while (*p && *p != ',' && *p != ')' && *p != ' ')
15016 p++;
15017
15018 if (! *p || p == arg_start)
4d3c2250 15019 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15020 else
15021 {
15022 /* Make sure argv has room for the new argument. */
15023 if (argc >= argv_size)
15024 {
15025 argv_size *= 2;
15026 argv = xrealloc (argv, argv_size * sizeof (*argv));
15027 }
15028
15029 argv[argc++] = copy_string (arg_start, p - arg_start);
15030 }
15031
15032 p = consume_improper_spaces (p, body);
15033
15034 /* Consume the comma, if present. */
15035 if (*p == ',')
15036 {
15037 p++;
15038
15039 p = consume_improper_spaces (p, body);
15040 }
15041 }
15042
15043 if (*p == ')')
15044 {
15045 p++;
15046
15047 if (*p == ' ')
15048 /* Perfectly formed definition, no complaints. */
15049 macro_define_function (file, line, name,
6e70227d 15050 argc, (const char **) argv,
2e276125
JB
15051 p + 1);
15052 else if (*p == '\0')
15053 {
15054 /* Complain, but do define it. */
4d3c2250 15055 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15056 macro_define_function (file, line, name,
6e70227d 15057 argc, (const char **) argv,
2e276125
JB
15058 p);
15059 }
15060 else
15061 /* Just complain. */
4d3c2250 15062 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15063 }
15064 else
15065 /* Just complain. */
4d3c2250 15066 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15067
15068 xfree (name);
15069 {
15070 int i;
15071
15072 for (i = 0; i < argc; i++)
15073 xfree (argv[i]);
15074 }
15075 xfree (argv);
15076 }
15077 else
4d3c2250 15078 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15079}
15080
cf2c3c16
TT
15081/* Skip some bytes from BYTES according to the form given in FORM.
15082 Returns the new pointer. */
2e276125 15083
cf2c3c16
TT
15084static gdb_byte *
15085skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15086 enum dwarf_form form,
15087 unsigned int offset_size,
15088 struct dwarf2_section_info *section)
2e276125 15089{
cf2c3c16 15090 unsigned int bytes_read;
2e276125 15091
cf2c3c16 15092 switch (form)
2e276125 15093 {
cf2c3c16
TT
15094 case DW_FORM_data1:
15095 case DW_FORM_flag:
15096 ++bytes;
15097 break;
15098
15099 case DW_FORM_data2:
15100 bytes += 2;
15101 break;
15102
15103 case DW_FORM_data4:
15104 bytes += 4;
15105 break;
15106
15107 case DW_FORM_data8:
15108 bytes += 8;
15109 break;
15110
15111 case DW_FORM_string:
15112 read_direct_string (abfd, bytes, &bytes_read);
15113 bytes += bytes_read;
15114 break;
15115
15116 case DW_FORM_sec_offset:
15117 case DW_FORM_strp:
15118 bytes += offset_size;
15119 break;
15120
15121 case DW_FORM_block:
15122 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15123 bytes += bytes_read;
15124 break;
15125
15126 case DW_FORM_block1:
15127 bytes += 1 + read_1_byte (abfd, bytes);
15128 break;
15129 case DW_FORM_block2:
15130 bytes += 2 + read_2_bytes (abfd, bytes);
15131 break;
15132 case DW_FORM_block4:
15133 bytes += 4 + read_4_bytes (abfd, bytes);
15134 break;
15135
15136 case DW_FORM_sdata:
15137 case DW_FORM_udata:
15138 bytes = skip_leb128 (abfd, bytes);
15139 break;
15140
15141 default:
15142 {
15143 complain:
15144 complaint (&symfile_complaints,
15145 _("invalid form 0x%x in `%s'"),
15146 form,
15147 section->asection->name);
15148 return NULL;
15149 }
2e276125
JB
15150 }
15151
cf2c3c16
TT
15152 return bytes;
15153}
757a13d0 15154
cf2c3c16
TT
15155/* A helper for dwarf_decode_macros that handles skipping an unknown
15156 opcode. Returns an updated pointer to the macro data buffer; or,
15157 on error, issues a complaint and returns NULL. */
757a13d0 15158
cf2c3c16
TT
15159static gdb_byte *
15160skip_unknown_opcode (unsigned int opcode,
15161 gdb_byte **opcode_definitions,
15162 gdb_byte *mac_ptr,
15163 bfd *abfd,
15164 unsigned int offset_size,
15165 struct dwarf2_section_info *section)
15166{
15167 unsigned int bytes_read, i;
15168 unsigned long arg;
15169 gdb_byte *defn;
2e276125 15170
cf2c3c16 15171 if (opcode_definitions[opcode] == NULL)
2e276125 15172 {
cf2c3c16
TT
15173 complaint (&symfile_complaints,
15174 _("unrecognized DW_MACFINO opcode 0x%x"),
15175 opcode);
15176 return NULL;
15177 }
2e276125 15178
cf2c3c16
TT
15179 defn = opcode_definitions[opcode];
15180 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15181 defn += bytes_read;
2e276125 15182
cf2c3c16
TT
15183 for (i = 0; i < arg; ++i)
15184 {
15185 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15186 if (mac_ptr == NULL)
15187 {
15188 /* skip_form_bytes already issued the complaint. */
15189 return NULL;
15190 }
15191 }
757a13d0 15192
cf2c3c16
TT
15193 return mac_ptr;
15194}
757a13d0 15195
cf2c3c16
TT
15196/* A helper function which parses the header of a macro section.
15197 If the macro section is the extended (for now called "GNU") type,
15198 then this updates *OFFSET_SIZE. Returns a pointer to just after
15199 the header, or issues a complaint and returns NULL on error. */
757a13d0 15200
cf2c3c16
TT
15201static gdb_byte *
15202dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15203 bfd *abfd,
15204 gdb_byte *mac_ptr,
15205 unsigned int *offset_size,
15206 int section_is_gnu)
15207{
15208 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15209
cf2c3c16
TT
15210 if (section_is_gnu)
15211 {
15212 unsigned int version, flags;
757a13d0 15213
cf2c3c16
TT
15214 version = read_2_bytes (abfd, mac_ptr);
15215 if (version != 4)
15216 {
15217 complaint (&symfile_complaints,
15218 _("unrecognized version `%d' in .debug_macro section"),
15219 version);
15220 return NULL;
15221 }
15222 mac_ptr += 2;
757a13d0 15223
cf2c3c16
TT
15224 flags = read_1_byte (abfd, mac_ptr);
15225 ++mac_ptr;
15226 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15227
cf2c3c16
TT
15228 if ((flags & 2) != 0)
15229 /* We don't need the line table offset. */
15230 mac_ptr += *offset_size;
757a13d0 15231
cf2c3c16
TT
15232 /* Vendor opcode descriptions. */
15233 if ((flags & 4) != 0)
15234 {
15235 unsigned int i, count;
757a13d0 15236
cf2c3c16
TT
15237 count = read_1_byte (abfd, mac_ptr);
15238 ++mac_ptr;
15239 for (i = 0; i < count; ++i)
15240 {
15241 unsigned int opcode, bytes_read;
15242 unsigned long arg;
15243
15244 opcode = read_1_byte (abfd, mac_ptr);
15245 ++mac_ptr;
15246 opcode_definitions[opcode] = mac_ptr;
15247 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15248 mac_ptr += bytes_read;
15249 mac_ptr += arg;
15250 }
757a13d0 15251 }
cf2c3c16 15252 }
757a13d0 15253
cf2c3c16
TT
15254 return mac_ptr;
15255}
757a13d0 15256
cf2c3c16 15257/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15258 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15259
15260static void
15261dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15262 struct macro_source_file *current_file,
15263 struct line_header *lh, char *comp_dir,
15264 struct dwarf2_section_info *section,
15265 int section_is_gnu,
15266 unsigned int offset_size,
8fc3fc34
TT
15267 struct objfile *objfile,
15268 htab_t include_hash)
cf2c3c16
TT
15269{
15270 enum dwarf_macro_record_type macinfo_type;
15271 int at_commandline;
15272 gdb_byte *opcode_definitions[256];
757a13d0 15273
cf2c3c16
TT
15274 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15275 &offset_size, section_is_gnu);
15276 if (mac_ptr == NULL)
15277 {
15278 /* We already issued a complaint. */
15279 return;
15280 }
757a13d0
JK
15281
15282 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15283 GDB is still reading the definitions from command line. First
15284 DW_MACINFO_start_file will need to be ignored as it was already executed
15285 to create CURRENT_FILE for the main source holding also the command line
15286 definitions. On first met DW_MACINFO_start_file this flag is reset to
15287 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15288
15289 at_commandline = 1;
15290
15291 do
15292 {
15293 /* Do we at least have room for a macinfo type byte? */
15294 if (mac_ptr >= mac_end)
15295 {
cf2c3c16 15296 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15297 break;
15298 }
15299
15300 macinfo_type = read_1_byte (abfd, mac_ptr);
15301 mac_ptr++;
15302
cf2c3c16
TT
15303 /* Note that we rely on the fact that the corresponding GNU and
15304 DWARF constants are the same. */
757a13d0
JK
15305 switch (macinfo_type)
15306 {
15307 /* A zero macinfo type indicates the end of the macro
15308 information. */
15309 case 0:
15310 break;
2e276125 15311
cf2c3c16
TT
15312 case DW_MACRO_GNU_define:
15313 case DW_MACRO_GNU_undef:
15314 case DW_MACRO_GNU_define_indirect:
15315 case DW_MACRO_GNU_undef_indirect:
2e276125 15316 {
891d2f0b 15317 unsigned int bytes_read;
2e276125
JB
15318 int line;
15319 char *body;
cf2c3c16 15320 int is_define;
2e276125 15321
cf2c3c16
TT
15322 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15323 mac_ptr += bytes_read;
15324
15325 if (macinfo_type == DW_MACRO_GNU_define
15326 || macinfo_type == DW_MACRO_GNU_undef)
15327 {
15328 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15329 mac_ptr += bytes_read;
15330 }
15331 else
15332 {
15333 LONGEST str_offset;
15334
15335 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15336 mac_ptr += offset_size;
2e276125 15337
cf2c3c16
TT
15338 body = read_indirect_string_at_offset (abfd, str_offset);
15339 }
15340
15341 is_define = (macinfo_type == DW_MACRO_GNU_define
15342 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15343 if (! current_file)
757a13d0
JK
15344 {
15345 /* DWARF violation as no main source is present. */
15346 complaint (&symfile_complaints,
15347 _("debug info with no main source gives macro %s "
15348 "on line %d: %s"),
cf2c3c16
TT
15349 is_define ? _("definition") : _("undefinition"),
15350 line, body);
757a13d0
JK
15351 break;
15352 }
3e43a32a
MS
15353 if ((line == 0 && !at_commandline)
15354 || (line != 0 && at_commandline))
4d3c2250 15355 complaint (&symfile_complaints,
757a13d0
JK
15356 _("debug info gives %s macro %s with %s line %d: %s"),
15357 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15358 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15359 line == 0 ? _("zero") : _("non-zero"), line, body);
15360
cf2c3c16 15361 if (is_define)
757a13d0 15362 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15363 else
15364 {
15365 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15366 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15367 macro_undef (current_file, line, body);
15368 }
2e276125
JB
15369 }
15370 break;
15371
cf2c3c16 15372 case DW_MACRO_GNU_start_file:
2e276125 15373 {
891d2f0b 15374 unsigned int bytes_read;
2e276125
JB
15375 int line, file;
15376
15377 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15378 mac_ptr += bytes_read;
15379 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15380 mac_ptr += bytes_read;
15381
3e43a32a
MS
15382 if ((line == 0 && !at_commandline)
15383 || (line != 0 && at_commandline))
757a13d0
JK
15384 complaint (&symfile_complaints,
15385 _("debug info gives source %d included "
15386 "from %s at %s line %d"),
15387 file, at_commandline ? _("command-line") : _("file"),
15388 line == 0 ? _("zero") : _("non-zero"), line);
15389
15390 if (at_commandline)
15391 {
cf2c3c16
TT
15392 /* This DW_MACRO_GNU_start_file was executed in the
15393 pass one. */
757a13d0
JK
15394 at_commandline = 0;
15395 }
15396 else
15397 current_file = macro_start_file (file, line,
15398 current_file, comp_dir,
cf2c3c16 15399 lh, objfile);
2e276125
JB
15400 }
15401 break;
15402
cf2c3c16 15403 case DW_MACRO_GNU_end_file:
2e276125 15404 if (! current_file)
4d3c2250 15405 complaint (&symfile_complaints,
3e43a32a
MS
15406 _("macro debug info has an unmatched "
15407 "`close_file' directive"));
2e276125
JB
15408 else
15409 {
15410 current_file = current_file->included_by;
15411 if (! current_file)
15412 {
cf2c3c16 15413 enum dwarf_macro_record_type next_type;
2e276125
JB
15414
15415 /* GCC circa March 2002 doesn't produce the zero
15416 type byte marking the end of the compilation
15417 unit. Complain if it's not there, but exit no
15418 matter what. */
15419
15420 /* Do we at least have room for a macinfo type byte? */
15421 if (mac_ptr >= mac_end)
15422 {
cf2c3c16 15423 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15424 return;
15425 }
15426
15427 /* We don't increment mac_ptr here, so this is just
15428 a look-ahead. */
15429 next_type = read_1_byte (abfd, mac_ptr);
15430 if (next_type != 0)
4d3c2250 15431 complaint (&symfile_complaints,
3e43a32a
MS
15432 _("no terminating 0-type entry for "
15433 "macros in `.debug_macinfo' section"));
2e276125
JB
15434
15435 return;
15436 }
15437 }
15438 break;
15439
cf2c3c16
TT
15440 case DW_MACRO_GNU_transparent_include:
15441 {
15442 LONGEST offset;
8fc3fc34 15443 void **slot;
cf2c3c16
TT
15444
15445 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15446 mac_ptr += offset_size;
15447
8fc3fc34
TT
15448 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15449 if (*slot != NULL)
15450 {
15451 /* This has actually happened; see
15452 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15453 complaint (&symfile_complaints,
15454 _("recursive DW_MACRO_GNU_transparent_include in "
15455 ".debug_macro section"));
15456 }
15457 else
15458 {
15459 *slot = mac_ptr;
15460
15461 dwarf_decode_macro_bytes (abfd,
15462 section->buffer + offset,
15463 mac_end, current_file,
15464 lh, comp_dir,
15465 section, section_is_gnu,
15466 offset_size, objfile, include_hash);
15467
15468 htab_remove_elt (include_hash, mac_ptr);
15469 }
cf2c3c16
TT
15470 }
15471 break;
15472
2e276125 15473 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15474 if (!section_is_gnu)
15475 {
15476 unsigned int bytes_read;
15477 int constant;
2e276125 15478
cf2c3c16
TT
15479 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15480 mac_ptr += bytes_read;
15481 read_direct_string (abfd, mac_ptr, &bytes_read);
15482 mac_ptr += bytes_read;
2e276125 15483
cf2c3c16
TT
15484 /* We don't recognize any vendor extensions. */
15485 break;
15486 }
15487 /* FALLTHROUGH */
15488
15489 default:
15490 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15491 mac_ptr, abfd, offset_size,
15492 section);
15493 if (mac_ptr == NULL)
15494 return;
15495 break;
2e276125 15496 }
757a13d0 15497 } while (macinfo_type != 0);
2e276125 15498}
8e19ed76 15499
cf2c3c16
TT
15500static void
15501dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15502 char *comp_dir, bfd *abfd,
15503 struct dwarf2_cu *cu,
15504 struct dwarf2_section_info *section,
15505 int section_is_gnu)
15506{
bb5ed363 15507 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15508 gdb_byte *mac_ptr, *mac_end;
15509 struct macro_source_file *current_file = 0;
15510 enum dwarf_macro_record_type macinfo_type;
15511 unsigned int offset_size = cu->header.offset_size;
15512 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15513 struct cleanup *cleanup;
15514 htab_t include_hash;
15515 void **slot;
cf2c3c16 15516
bb5ed363 15517 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15518 if (section->buffer == NULL)
15519 {
15520 complaint (&symfile_complaints, _("missing %s section"),
15521 section->asection->name);
15522 return;
15523 }
15524
15525 /* First pass: Find the name of the base filename.
15526 This filename is needed in order to process all macros whose definition
15527 (or undefinition) comes from the command line. These macros are defined
15528 before the first DW_MACINFO_start_file entry, and yet still need to be
15529 associated to the base file.
15530
15531 To determine the base file name, we scan the macro definitions until we
15532 reach the first DW_MACINFO_start_file entry. We then initialize
15533 CURRENT_FILE accordingly so that any macro definition found before the
15534 first DW_MACINFO_start_file can still be associated to the base file. */
15535
15536 mac_ptr = section->buffer + offset;
15537 mac_end = section->buffer + section->size;
15538
15539 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15540 &offset_size, section_is_gnu);
15541 if (mac_ptr == NULL)
15542 {
15543 /* We already issued a complaint. */
15544 return;
15545 }
15546
15547 do
15548 {
15549 /* Do we at least have room for a macinfo type byte? */
15550 if (mac_ptr >= mac_end)
15551 {
15552 /* Complaint is printed during the second pass as GDB will probably
15553 stop the first pass earlier upon finding
15554 DW_MACINFO_start_file. */
15555 break;
15556 }
15557
15558 macinfo_type = read_1_byte (abfd, mac_ptr);
15559 mac_ptr++;
15560
15561 /* Note that we rely on the fact that the corresponding GNU and
15562 DWARF constants are the same. */
15563 switch (macinfo_type)
15564 {
15565 /* A zero macinfo type indicates the end of the macro
15566 information. */
15567 case 0:
15568 break;
15569
15570 case DW_MACRO_GNU_define:
15571 case DW_MACRO_GNU_undef:
15572 /* Only skip the data by MAC_PTR. */
15573 {
15574 unsigned int bytes_read;
15575
15576 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15577 mac_ptr += bytes_read;
15578 read_direct_string (abfd, mac_ptr, &bytes_read);
15579 mac_ptr += bytes_read;
15580 }
15581 break;
15582
15583 case DW_MACRO_GNU_start_file:
15584 {
15585 unsigned int bytes_read;
15586 int line, file;
15587
15588 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15589 mac_ptr += bytes_read;
15590 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15591 mac_ptr += bytes_read;
15592
15593 current_file = macro_start_file (file, line, current_file,
bb5ed363 15594 comp_dir, lh, objfile);
cf2c3c16
TT
15595 }
15596 break;
15597
15598 case DW_MACRO_GNU_end_file:
15599 /* No data to skip by MAC_PTR. */
15600 break;
15601
15602 case DW_MACRO_GNU_define_indirect:
15603 case DW_MACRO_GNU_undef_indirect:
15604 {
15605 unsigned int bytes_read;
15606
15607 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15608 mac_ptr += bytes_read;
15609 mac_ptr += offset_size;
15610 }
15611 break;
15612
15613 case DW_MACRO_GNU_transparent_include:
15614 /* Note that, according to the spec, a transparent include
15615 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15616 skip this opcode. */
15617 mac_ptr += offset_size;
15618 break;
15619
15620 case DW_MACINFO_vendor_ext:
15621 /* Only skip the data by MAC_PTR. */
15622 if (!section_is_gnu)
15623 {
15624 unsigned int bytes_read;
15625
15626 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15627 mac_ptr += bytes_read;
15628 read_direct_string (abfd, mac_ptr, &bytes_read);
15629 mac_ptr += bytes_read;
15630 }
15631 /* FALLTHROUGH */
15632
15633 default:
15634 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15635 mac_ptr, abfd, offset_size,
15636 section);
15637 if (mac_ptr == NULL)
15638 return;
15639 break;
15640 }
15641 } while (macinfo_type != 0 && current_file == NULL);
15642
15643 /* Second pass: Process all entries.
15644
15645 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15646 command-line macro definitions/undefinitions. This flag is unset when we
15647 reach the first DW_MACINFO_start_file entry. */
15648
8fc3fc34
TT
15649 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15650 NULL, xcalloc, xfree);
15651 cleanup = make_cleanup_htab_delete (include_hash);
15652 mac_ptr = section->buffer + offset;
15653 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15654 *slot = mac_ptr;
15655 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 15656 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
15657 offset_size, objfile, include_hash);
15658 do_cleanups (cleanup);
cf2c3c16
TT
15659}
15660
8e19ed76 15661/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15662 if so return true else false. */
8e19ed76
PS
15663static int
15664attr_form_is_block (struct attribute *attr)
15665{
15666 return (attr == NULL ? 0 :
15667 attr->form == DW_FORM_block1
15668 || attr->form == DW_FORM_block2
15669 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15670 || attr->form == DW_FORM_block
15671 || attr->form == DW_FORM_exprloc);
8e19ed76 15672}
4c2df51b 15673
c6a0999f
JB
15674/* Return non-zero if ATTR's value is a section offset --- classes
15675 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15676 You may use DW_UNSND (attr) to retrieve such offsets.
15677
15678 Section 7.5.4, "Attribute Encodings", explains that no attribute
15679 may have a value that belongs to more than one of these classes; it
15680 would be ambiguous if we did, because we use the same forms for all
15681 of them. */
3690dd37
JB
15682static int
15683attr_form_is_section_offset (struct attribute *attr)
15684{
15685 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15686 || attr->form == DW_FORM_data8
15687 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15688}
15689
15690
15691/* Return non-zero if ATTR's value falls in the 'constant' class, or
15692 zero otherwise. When this function returns true, you can apply
15693 dwarf2_get_attr_constant_value to it.
15694
15695 However, note that for some attributes you must check
15696 attr_form_is_section_offset before using this test. DW_FORM_data4
15697 and DW_FORM_data8 are members of both the constant class, and of
15698 the classes that contain offsets into other debug sections
15699 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15700 that, if an attribute's can be either a constant or one of the
15701 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15702 taken as section offsets, not constants. */
15703static int
15704attr_form_is_constant (struct attribute *attr)
15705{
15706 switch (attr->form)
15707 {
15708 case DW_FORM_sdata:
15709 case DW_FORM_udata:
15710 case DW_FORM_data1:
15711 case DW_FORM_data2:
15712 case DW_FORM_data4:
15713 case DW_FORM_data8:
15714 return 1;
15715 default:
15716 return 0;
15717 }
15718}
15719
8cf6f0b1
TT
15720/* A helper function that fills in a dwarf2_loclist_baton. */
15721
15722static void
15723fill_in_loclist_baton (struct dwarf2_cu *cu,
15724 struct dwarf2_loclist_baton *baton,
15725 struct attribute *attr)
15726{
15727 dwarf2_read_section (dwarf2_per_objfile->objfile,
15728 &dwarf2_per_objfile->loc);
15729
15730 baton->per_cu = cu->per_cu;
15731 gdb_assert (baton->per_cu);
15732 /* We don't know how long the location list is, but make sure we
15733 don't run off the edge of the section. */
15734 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15735 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15736 baton->base_address = cu->base_address;
15737}
15738
4c2df51b
DJ
15739static void
15740dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15741 struct dwarf2_cu *cu)
4c2df51b 15742{
bb5ed363
DE
15743 struct objfile *objfile = dwarf2_per_objfile->objfile;
15744
3690dd37 15745 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15746 /* ".debug_loc" may not exist at all, or the offset may be outside
15747 the section. If so, fall through to the complaint in the
15748 other branch. */
bb5ed363 15749 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15750 &dwarf2_per_objfile->loc))
4c2df51b 15751 {
0d53c4c4 15752 struct dwarf2_loclist_baton *baton;
4c2df51b 15753
bb5ed363 15754 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15755 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15756
8cf6f0b1 15757 fill_in_loclist_baton (cu, baton, attr);
be391dca 15758
d00adf39 15759 if (cu->base_known == 0)
0d53c4c4 15760 complaint (&symfile_complaints,
3e43a32a
MS
15761 _("Location list used without "
15762 "specifying the CU base address."));
4c2df51b 15763
768a979c 15764 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15765 SYMBOL_LOCATION_BATON (sym) = baton;
15766 }
15767 else
15768 {
15769 struct dwarf2_locexpr_baton *baton;
15770
bb5ed363 15771 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15772 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15773 baton->per_cu = cu->per_cu;
15774 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15775
15776 if (attr_form_is_block (attr))
15777 {
15778 /* Note that we're just copying the block's data pointer
15779 here, not the actual data. We're still pointing into the
6502dd73
DJ
15780 info_buffer for SYM's objfile; right now we never release
15781 that buffer, but when we do clean up properly this may
15782 need to change. */
0d53c4c4
DJ
15783 baton->size = DW_BLOCK (attr)->size;
15784 baton->data = DW_BLOCK (attr)->data;
15785 }
15786 else
15787 {
15788 dwarf2_invalid_attrib_class_complaint ("location description",
15789 SYMBOL_NATURAL_NAME (sym));
15790 baton->size = 0;
0d53c4c4 15791 }
6e70227d 15792
768a979c 15793 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15794 SYMBOL_LOCATION_BATON (sym) = baton;
15795 }
4c2df51b 15796}
6502dd73 15797
9aa1f1e3
TT
15798/* Return the OBJFILE associated with the compilation unit CU. If CU
15799 came from a separate debuginfo file, then the master objfile is
15800 returned. */
ae0d2f24
UW
15801
15802struct objfile *
15803dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15804{
9291a0cd 15805 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15806
15807 /* Return the master objfile, so that we can report and look up the
15808 correct file containing this variable. */
15809 if (objfile->separate_debug_objfile_backlink)
15810 objfile = objfile->separate_debug_objfile_backlink;
15811
15812 return objfile;
15813}
15814
96408a79
SA
15815/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15816 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15817 CU_HEADERP first. */
15818
15819static const struct comp_unit_head *
15820per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15821 struct dwarf2_per_cu_data *per_cu)
15822{
15823 struct objfile *objfile;
15824 struct dwarf2_per_objfile *per_objfile;
15825 gdb_byte *info_ptr;
15826
15827 if (per_cu->cu)
15828 return &per_cu->cu->header;
15829
15830 objfile = per_cu->objfile;
15831 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 15832 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
15833
15834 memset (cu_headerp, 0, sizeof (*cu_headerp));
15835 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15836
15837 return cu_headerp;
15838}
15839
ae0d2f24
UW
15840/* Return the address size given in the compilation unit header for CU. */
15841
98714339 15842int
ae0d2f24
UW
15843dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15844{
96408a79
SA
15845 struct comp_unit_head cu_header_local;
15846 const struct comp_unit_head *cu_headerp;
c471e790 15847
96408a79
SA
15848 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15849
15850 return cu_headerp->addr_size;
ae0d2f24
UW
15851}
15852
9eae7c52
TT
15853/* Return the offset size given in the compilation unit header for CU. */
15854
15855int
15856dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15857{
96408a79
SA
15858 struct comp_unit_head cu_header_local;
15859 const struct comp_unit_head *cu_headerp;
9c6c53f7 15860
96408a79
SA
15861 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15862
15863 return cu_headerp->offset_size;
15864}
15865
15866/* See its dwarf2loc.h declaration. */
15867
15868int
15869dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15870{
15871 struct comp_unit_head cu_header_local;
15872 const struct comp_unit_head *cu_headerp;
15873
15874 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15875
15876 if (cu_headerp->version == 2)
15877 return cu_headerp->addr_size;
15878 else
15879 return cu_headerp->offset_size;
181cebd4
JK
15880}
15881
9aa1f1e3
TT
15882/* Return the text offset of the CU. The returned offset comes from
15883 this CU's objfile. If this objfile came from a separate debuginfo
15884 file, then the offset may be different from the corresponding
15885 offset in the parent objfile. */
15886
15887CORE_ADDR
15888dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15889{
bb3fa9d0 15890 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15891
15892 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15893}
15894
348e048f
DE
15895/* Locate the .debug_info compilation unit from CU's objfile which contains
15896 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15897
15898static struct dwarf2_per_cu_data *
b64f50a1 15899dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
15900 struct objfile *objfile)
15901{
15902 struct dwarf2_per_cu_data *this_cu;
15903 int low, high;
15904
ae038cb0
DJ
15905 low = 0;
15906 high = dwarf2_per_objfile->n_comp_units - 1;
15907 while (high > low)
15908 {
15909 int mid = low + (high - low) / 2;
9a619af0 15910
b64f50a1
JK
15911 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
15912 >= offset.sect_off)
ae038cb0
DJ
15913 high = mid;
15914 else
15915 low = mid + 1;
15916 }
15917 gdb_assert (low == high);
b64f50a1
JK
15918 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
15919 > offset.sect_off)
ae038cb0 15920 {
10b3939b 15921 if (low == 0)
8a3fe4f8
AC
15922 error (_("Dwarf Error: could not find partial DIE containing "
15923 "offset 0x%lx [in module %s]"),
b64f50a1 15924 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 15925
b64f50a1
JK
15926 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
15927 <= offset.sect_off);
ae038cb0
DJ
15928 return dwarf2_per_objfile->all_comp_units[low-1];
15929 }
15930 else
15931 {
15932 this_cu = dwarf2_per_objfile->all_comp_units[low];
15933 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
15934 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
15935 error (_("invalid dwarf2 offset %u"), offset.sect_off);
15936 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
15937 return this_cu;
15938 }
15939}
15940
23745b47 15941/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15942
9816fde3 15943static void
23745b47 15944init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15945{
9816fde3 15946 memset (cu, 0, sizeof (*cu));
23745b47
DE
15947 per_cu->cu = cu;
15948 cu->per_cu = per_cu;
15949 cu->objfile = per_cu->objfile;
93311388 15950 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15951}
15952
15953/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15954
15955static void
15956prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15957{
15958 struct attribute *attr;
15959
15960 /* Set the language we're debugging. */
15961 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15962 if (attr)
15963 set_cu_language (DW_UNSND (attr), cu);
15964 else
9cded63f
TT
15965 {
15966 cu->language = language_minimal;
15967 cu->language_defn = language_def (cu->language);
15968 }
93311388
DE
15969}
15970
ae038cb0
DJ
15971/* Release one cached compilation unit, CU. We unlink it from the tree
15972 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15973 the caller is responsible for that.
15974 NOTE: DATA is a void * because this function is also used as a
15975 cleanup routine. */
ae038cb0
DJ
15976
15977static void
68dc6402 15978free_heap_comp_unit (void *data)
ae038cb0
DJ
15979{
15980 struct dwarf2_cu *cu = data;
15981
23745b47
DE
15982 gdb_assert (cu->per_cu != NULL);
15983 cu->per_cu->cu = NULL;
ae038cb0
DJ
15984 cu->per_cu = NULL;
15985
15986 obstack_free (&cu->comp_unit_obstack, NULL);
15987
15988 xfree (cu);
15989}
15990
72bf9492 15991/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15992 when we're finished with it. We can't free the pointer itself, but be
15993 sure to unlink it from the cache. Also release any associated storage
15994 and perform cache maintenance.
72bf9492
DJ
15995
15996 Only used during partial symbol parsing. */
15997
15998static void
15999free_stack_comp_unit (void *data)
16000{
16001 struct dwarf2_cu *cu = data;
16002
23745b47
DE
16003 gdb_assert (cu->per_cu != NULL);
16004 cu->per_cu->cu = NULL;
16005 cu->per_cu = NULL;
16006
72bf9492
DJ
16007 obstack_free (&cu->comp_unit_obstack, NULL);
16008 cu->partial_dies = NULL;
ae038cb0 16009
23745b47
DE
16010 /* The previous code only did this if per_cu != NULL.
16011 But that would always succeed, so now we just unconditionally do
16012 the aging. This seems like the wrong place to do such aging,
16013 but cleaning that up is left for later. */
16014 age_cached_comp_units ();
ae038cb0
DJ
16015}
16016
16017/* Free all cached compilation units. */
16018
16019static void
16020free_cached_comp_units (void *data)
16021{
16022 struct dwarf2_per_cu_data *per_cu, **last_chain;
16023
16024 per_cu = dwarf2_per_objfile->read_in_chain;
16025 last_chain = &dwarf2_per_objfile->read_in_chain;
16026 while (per_cu != NULL)
16027 {
16028 struct dwarf2_per_cu_data *next_cu;
16029
16030 next_cu = per_cu->cu->read_in_chain;
16031
68dc6402 16032 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16033 *last_chain = next_cu;
16034
16035 per_cu = next_cu;
16036 }
16037}
16038
16039/* Increase the age counter on each cached compilation unit, and free
16040 any that are too old. */
16041
16042static void
16043age_cached_comp_units (void)
16044{
16045 struct dwarf2_per_cu_data *per_cu, **last_chain;
16046
16047 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16048 per_cu = dwarf2_per_objfile->read_in_chain;
16049 while (per_cu != NULL)
16050 {
16051 per_cu->cu->last_used ++;
16052 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16053 dwarf2_mark (per_cu->cu);
16054 per_cu = per_cu->cu->read_in_chain;
16055 }
16056
16057 per_cu = dwarf2_per_objfile->read_in_chain;
16058 last_chain = &dwarf2_per_objfile->read_in_chain;
16059 while (per_cu != NULL)
16060 {
16061 struct dwarf2_per_cu_data *next_cu;
16062
16063 next_cu = per_cu->cu->read_in_chain;
16064
16065 if (!per_cu->cu->mark)
16066 {
68dc6402 16067 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16068 *last_chain = next_cu;
16069 }
16070 else
16071 last_chain = &per_cu->cu->read_in_chain;
16072
16073 per_cu = next_cu;
16074 }
16075}
16076
16077/* Remove a single compilation unit from the cache. */
16078
16079static void
16080free_one_cached_comp_unit (void *target_cu)
16081{
16082 struct dwarf2_per_cu_data *per_cu, **last_chain;
16083
16084 per_cu = dwarf2_per_objfile->read_in_chain;
16085 last_chain = &dwarf2_per_objfile->read_in_chain;
16086 while (per_cu != NULL)
16087 {
16088 struct dwarf2_per_cu_data *next_cu;
16089
16090 next_cu = per_cu->cu->read_in_chain;
16091
16092 if (per_cu->cu == target_cu)
16093 {
68dc6402 16094 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16095 *last_chain = next_cu;
16096 break;
16097 }
16098 else
16099 last_chain = &per_cu->cu->read_in_chain;
16100
16101 per_cu = next_cu;
16102 }
16103}
16104
fe3e1990
DJ
16105/* Release all extra memory associated with OBJFILE. */
16106
16107void
16108dwarf2_free_objfile (struct objfile *objfile)
16109{
16110 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16111
16112 if (dwarf2_per_objfile == NULL)
16113 return;
16114
16115 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16116 free_cached_comp_units (NULL);
16117
7b9f3c50
DE
16118 if (dwarf2_per_objfile->quick_file_names_table)
16119 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16120
fe3e1990
DJ
16121 /* Everything else should be on the objfile obstack. */
16122}
16123
1c379e20
DJ
16124/* A pair of DIE offset and GDB type pointer. We store these
16125 in a hash table separate from the DIEs, and preserve them
16126 when the DIEs are flushed out of cache. */
16127
16128struct dwarf2_offset_and_type
16129{
b64f50a1 16130 sect_offset offset;
1c379e20
DJ
16131 struct type *type;
16132};
16133
16134/* Hash function for a dwarf2_offset_and_type. */
16135
16136static hashval_t
16137offset_and_type_hash (const void *item)
16138{
16139 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16140
b64f50a1 16141 return ofs->offset.sect_off;
1c379e20
DJ
16142}
16143
16144/* Equality function for a dwarf2_offset_and_type. */
16145
16146static int
16147offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16148{
16149 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16150 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16151
b64f50a1 16152 return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
1c379e20
DJ
16153}
16154
16155/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16156 table if necessary. For convenience, return TYPE.
16157
16158 The DIEs reading must have careful ordering to:
16159 * Not cause infite loops trying to read in DIEs as a prerequisite for
16160 reading current DIE.
16161 * Not trying to dereference contents of still incompletely read in types
16162 while reading in other DIEs.
16163 * Enable referencing still incompletely read in types just by a pointer to
16164 the type without accessing its fields.
16165
16166 Therefore caller should follow these rules:
16167 * Try to fetch any prerequisite types we may need to build this DIE type
16168 before building the type and calling set_die_type.
e71ec853 16169 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16170 possible before fetching more types to complete the current type.
16171 * Make the type as complete as possible before fetching more types. */
1c379e20 16172
f792889a 16173static struct type *
1c379e20
DJ
16174set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16175{
16176 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16177 struct objfile *objfile = cu->objfile;
16178 htab_t *type_hash_ptr;
1c379e20 16179
b4ba55a1
JB
16180 /* For Ada types, make sure that the gnat-specific data is always
16181 initialized (if not already set). There are a few types where
16182 we should not be doing so, because the type-specific area is
16183 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16184 where the type-specific area is used to store the floatformat).
16185 But this is not a problem, because the gnat-specific information
16186 is actually not needed for these types. */
16187 if (need_gnat_info (cu)
16188 && TYPE_CODE (type) != TYPE_CODE_FUNC
16189 && TYPE_CODE (type) != TYPE_CODE_FLT
16190 && !HAVE_GNAT_AUX_INFO (type))
16191 INIT_GNAT_SPECIFIC (type);
16192
b0df02fd 16193 if (cu->per_cu->debug_types_section)
673bfd45
DE
16194 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16195 else
16196 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16197
16198 if (*type_hash_ptr == NULL)
f792889a 16199 {
673bfd45
DE
16200 *type_hash_ptr
16201 = htab_create_alloc_ex (127,
f792889a
DJ
16202 offset_and_type_hash,
16203 offset_and_type_eq,
16204 NULL,
673bfd45 16205 &objfile->objfile_obstack,
f792889a
DJ
16206 hashtab_obstack_allocate,
16207 dummy_obstack_deallocate);
f792889a 16208 }
1c379e20
DJ
16209
16210 ofs.offset = die->offset;
16211 ofs.type = type;
16212 slot = (struct dwarf2_offset_and_type **)
b64f50a1
JK
16213 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16214 INSERT);
7e314c57
JK
16215 if (*slot)
16216 complaint (&symfile_complaints,
16217 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16218 die->offset.sect_off);
673bfd45 16219 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16220 **slot = ofs;
f792889a 16221 return type;
1c379e20
DJ
16222}
16223
673bfd45
DE
16224/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16225 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16226
16227static struct type *
b64f50a1 16228get_die_type_at_offset (sect_offset offset,
673bfd45 16229 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16230{
16231 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16232 htab_t type_hash;
f792889a 16233
b0df02fd 16234 if (per_cu->debug_types_section)
673bfd45
DE
16235 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16236 else
16237 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16238 if (type_hash == NULL)
16239 return NULL;
1c379e20 16240
673bfd45 16241 ofs.offset = offset;
b64f50a1 16242 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
1c379e20
DJ
16243 if (slot)
16244 return slot->type;
16245 else
16246 return NULL;
16247}
16248
673bfd45
DE
16249/* Look up the type for DIE in the appropriate type_hash table,
16250 or return NULL if DIE does not have a saved type. */
16251
16252static struct type *
16253get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16254{
16255 return get_die_type_at_offset (die->offset, cu->per_cu);
16256}
16257
10b3939b
DJ
16258/* Add a dependence relationship from CU to REF_PER_CU. */
16259
16260static void
16261dwarf2_add_dependence (struct dwarf2_cu *cu,
16262 struct dwarf2_per_cu_data *ref_per_cu)
16263{
16264 void **slot;
16265
16266 if (cu->dependencies == NULL)
16267 cu->dependencies
16268 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16269 NULL, &cu->comp_unit_obstack,
16270 hashtab_obstack_allocate,
16271 dummy_obstack_deallocate);
16272
16273 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16274 if (*slot == NULL)
16275 *slot = ref_per_cu;
16276}
1c379e20 16277
f504f079
DE
16278/* Subroutine of dwarf2_mark to pass to htab_traverse.
16279 Set the mark field in every compilation unit in the
ae038cb0
DJ
16280 cache that we must keep because we are keeping CU. */
16281
10b3939b
DJ
16282static int
16283dwarf2_mark_helper (void **slot, void *data)
16284{
16285 struct dwarf2_per_cu_data *per_cu;
16286
16287 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16288
16289 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16290 reading of the chain. As such dependencies remain valid it is not much
16291 useful to track and undo them during QUIT cleanups. */
16292 if (per_cu->cu == NULL)
16293 return 1;
16294
10b3939b
DJ
16295 if (per_cu->cu->mark)
16296 return 1;
16297 per_cu->cu->mark = 1;
16298
16299 if (per_cu->cu->dependencies != NULL)
16300 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16301
16302 return 1;
16303}
16304
f504f079
DE
16305/* Set the mark field in CU and in every other compilation unit in the
16306 cache that we must keep because we are keeping CU. */
16307
ae038cb0
DJ
16308static void
16309dwarf2_mark (struct dwarf2_cu *cu)
16310{
16311 if (cu->mark)
16312 return;
16313 cu->mark = 1;
10b3939b
DJ
16314 if (cu->dependencies != NULL)
16315 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16316}
16317
16318static void
16319dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16320{
16321 while (per_cu)
16322 {
16323 per_cu->cu->mark = 0;
16324 per_cu = per_cu->cu->read_in_chain;
16325 }
72bf9492
DJ
16326}
16327
72bf9492
DJ
16328/* Trivial hash function for partial_die_info: the hash value of a DIE
16329 is its offset in .debug_info for this objfile. */
16330
16331static hashval_t
16332partial_die_hash (const void *item)
16333{
16334 const struct partial_die_info *part_die = item;
9a619af0 16335
b64f50a1 16336 return part_die->offset.sect_off;
72bf9492
DJ
16337}
16338
16339/* Trivial comparison function for partial_die_info structures: two DIEs
16340 are equal if they have the same offset. */
16341
16342static int
16343partial_die_eq (const void *item_lhs, const void *item_rhs)
16344{
16345 const struct partial_die_info *part_die_lhs = item_lhs;
16346 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16347
b64f50a1 16348 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
16349}
16350
ae038cb0
DJ
16351static struct cmd_list_element *set_dwarf2_cmdlist;
16352static struct cmd_list_element *show_dwarf2_cmdlist;
16353
16354static void
16355set_dwarf2_cmd (char *args, int from_tty)
16356{
16357 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16358}
16359
16360static void
16361show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16362{
ae038cb0
DJ
16363 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16364}
16365
dce234bc
PP
16366/* If section described by INFO was mmapped, munmap it now. */
16367
16368static void
16369munmap_section_buffer (struct dwarf2_section_info *info)
16370{
b315ab21 16371 if (info->map_addr != NULL)
dce234bc
PP
16372 {
16373#ifdef HAVE_MMAP
b315ab21 16374 int res;
9a619af0 16375
b315ab21
TG
16376 res = munmap (info->map_addr, info->map_len);
16377 gdb_assert (res == 0);
dce234bc
PP
16378#else
16379 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16380 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16381#endif
16382 }
16383}
16384
16385/* munmap debug sections for OBJFILE, if necessary. */
16386
16387static void
c1bd65d0 16388dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16389{
16390 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16391 int ix;
16392 struct dwarf2_section_info *section;
9a619af0 16393
16be1145
DE
16394 /* This is sorted according to the order they're defined in to make it easier
16395 to keep in sync. */
dce234bc
PP
16396 munmap_section_buffer (&data->info);
16397 munmap_section_buffer (&data->abbrev);
16398 munmap_section_buffer (&data->line);
16be1145 16399 munmap_section_buffer (&data->loc);
dce234bc 16400 munmap_section_buffer (&data->macinfo);
cf2c3c16 16401 munmap_section_buffer (&data->macro);
16be1145 16402 munmap_section_buffer (&data->str);
dce234bc 16403 munmap_section_buffer (&data->ranges);
dce234bc
PP
16404 munmap_section_buffer (&data->frame);
16405 munmap_section_buffer (&data->eh_frame);
9291a0cd 16406 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16407
16408 for (ix = 0;
16409 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16410 ++ix)
16411 munmap_section_buffer (section);
16412
16413 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16414}
16415
16416\f
ae2de4f8 16417/* The "save gdb-index" command. */
9291a0cd
TT
16418
16419/* The contents of the hash table we create when building the string
16420 table. */
16421struct strtab_entry
16422{
16423 offset_type offset;
16424 const char *str;
16425};
16426
559a7a62
JK
16427/* Hash function for a strtab_entry.
16428
16429 Function is used only during write_hash_table so no index format backward
16430 compatibility is needed. */
b89be57b 16431
9291a0cd
TT
16432static hashval_t
16433hash_strtab_entry (const void *e)
16434{
16435 const struct strtab_entry *entry = e;
559a7a62 16436 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16437}
16438
16439/* Equality function for a strtab_entry. */
b89be57b 16440
9291a0cd
TT
16441static int
16442eq_strtab_entry (const void *a, const void *b)
16443{
16444 const struct strtab_entry *ea = a;
16445 const struct strtab_entry *eb = b;
16446 return !strcmp (ea->str, eb->str);
16447}
16448
16449/* Create a strtab_entry hash table. */
b89be57b 16450
9291a0cd
TT
16451static htab_t
16452create_strtab (void)
16453{
16454 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16455 xfree, xcalloc, xfree);
16456}
16457
16458/* Add a string to the constant pool. Return the string's offset in
16459 host order. */
b89be57b 16460
9291a0cd
TT
16461static offset_type
16462add_string (htab_t table, struct obstack *cpool, const char *str)
16463{
16464 void **slot;
16465 struct strtab_entry entry;
16466 struct strtab_entry *result;
16467
16468 entry.str = str;
16469 slot = htab_find_slot (table, &entry, INSERT);
16470 if (*slot)
16471 result = *slot;
16472 else
16473 {
16474 result = XNEW (struct strtab_entry);
16475 result->offset = obstack_object_size (cpool);
16476 result->str = str;
16477 obstack_grow_str0 (cpool, str);
16478 *slot = result;
16479 }
16480 return result->offset;
16481}
16482
16483/* An entry in the symbol table. */
16484struct symtab_index_entry
16485{
16486 /* The name of the symbol. */
16487 const char *name;
16488 /* The offset of the name in the constant pool. */
16489 offset_type index_offset;
16490 /* A sorted vector of the indices of all the CUs that hold an object
16491 of this name. */
16492 VEC (offset_type) *cu_indices;
16493};
16494
16495/* The symbol table. This is a power-of-2-sized hash table. */
16496struct mapped_symtab
16497{
16498 offset_type n_elements;
16499 offset_type size;
16500 struct symtab_index_entry **data;
16501};
16502
16503/* Hash function for a symtab_index_entry. */
b89be57b 16504
9291a0cd
TT
16505static hashval_t
16506hash_symtab_entry (const void *e)
16507{
16508 const struct symtab_index_entry *entry = e;
16509 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16510 sizeof (offset_type) * VEC_length (offset_type,
16511 entry->cu_indices),
16512 0);
16513}
16514
16515/* Equality function for a symtab_index_entry. */
b89be57b 16516
9291a0cd
TT
16517static int
16518eq_symtab_entry (const void *a, const void *b)
16519{
16520 const struct symtab_index_entry *ea = a;
16521 const struct symtab_index_entry *eb = b;
16522 int len = VEC_length (offset_type, ea->cu_indices);
16523 if (len != VEC_length (offset_type, eb->cu_indices))
16524 return 0;
16525 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16526 VEC_address (offset_type, eb->cu_indices),
16527 sizeof (offset_type) * len);
16528}
16529
16530/* Destroy a symtab_index_entry. */
b89be57b 16531
9291a0cd
TT
16532static void
16533delete_symtab_entry (void *p)
16534{
16535 struct symtab_index_entry *entry = p;
16536 VEC_free (offset_type, entry->cu_indices);
16537 xfree (entry);
16538}
16539
16540/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16541
9291a0cd 16542static htab_t
3876f04e 16543create_symbol_hash_table (void)
9291a0cd
TT
16544{
16545 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16546 delete_symtab_entry, xcalloc, xfree);
16547}
16548
16549/* Create a new mapped symtab object. */
b89be57b 16550
9291a0cd
TT
16551static struct mapped_symtab *
16552create_mapped_symtab (void)
16553{
16554 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16555 symtab->n_elements = 0;
16556 symtab->size = 1024;
16557 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16558 return symtab;
16559}
16560
16561/* Destroy a mapped_symtab. */
b89be57b 16562
9291a0cd
TT
16563static void
16564cleanup_mapped_symtab (void *p)
16565{
16566 struct mapped_symtab *symtab = p;
16567 /* The contents of the array are freed when the other hash table is
16568 destroyed. */
16569 xfree (symtab->data);
16570 xfree (symtab);
16571}
16572
16573/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16574 the slot.
16575
16576 Function is used only during write_hash_table so no index format backward
16577 compatibility is needed. */
b89be57b 16578
9291a0cd
TT
16579static struct symtab_index_entry **
16580find_slot (struct mapped_symtab *symtab, const char *name)
16581{
559a7a62 16582 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16583
16584 index = hash & (symtab->size - 1);
16585 step = ((hash * 17) & (symtab->size - 1)) | 1;
16586
16587 for (;;)
16588 {
16589 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16590 return &symtab->data[index];
16591 index = (index + step) & (symtab->size - 1);
16592 }
16593}
16594
16595/* Expand SYMTAB's hash table. */
b89be57b 16596
9291a0cd
TT
16597static void
16598hash_expand (struct mapped_symtab *symtab)
16599{
16600 offset_type old_size = symtab->size;
16601 offset_type i;
16602 struct symtab_index_entry **old_entries = symtab->data;
16603
16604 symtab->size *= 2;
16605 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16606
16607 for (i = 0; i < old_size; ++i)
16608 {
16609 if (old_entries[i])
16610 {
16611 struct symtab_index_entry **slot = find_slot (symtab,
16612 old_entries[i]->name);
16613 *slot = old_entries[i];
16614 }
16615 }
16616
16617 xfree (old_entries);
16618}
16619
16620/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16621 is the index of the CU in which the symbol appears. */
b89be57b 16622
9291a0cd
TT
16623static void
16624add_index_entry (struct mapped_symtab *symtab, const char *name,
16625 offset_type cu_index)
16626{
16627 struct symtab_index_entry **slot;
16628
16629 ++symtab->n_elements;
16630 if (4 * symtab->n_elements / 3 >= symtab->size)
16631 hash_expand (symtab);
16632
16633 slot = find_slot (symtab, name);
16634 if (!*slot)
16635 {
16636 *slot = XNEW (struct symtab_index_entry);
16637 (*slot)->name = name;
16638 (*slot)->cu_indices = NULL;
16639 }
16640 /* Don't push an index twice. Due to how we add entries we only
16641 have to check the last one. */
16642 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16643 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16644 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16645}
16646
16647/* Add a vector of indices to the constant pool. */
b89be57b 16648
9291a0cd 16649static offset_type
3876f04e 16650add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16651 struct symtab_index_entry *entry)
16652{
16653 void **slot;
16654
3876f04e 16655 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16656 if (!*slot)
16657 {
16658 offset_type len = VEC_length (offset_type, entry->cu_indices);
16659 offset_type val = MAYBE_SWAP (len);
16660 offset_type iter;
16661 int i;
16662
16663 *slot = entry;
16664 entry->index_offset = obstack_object_size (cpool);
16665
16666 obstack_grow (cpool, &val, sizeof (val));
16667 for (i = 0;
16668 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16669 ++i)
16670 {
16671 val = MAYBE_SWAP (iter);
16672 obstack_grow (cpool, &val, sizeof (val));
16673 }
16674 }
16675 else
16676 {
16677 struct symtab_index_entry *old_entry = *slot;
16678 entry->index_offset = old_entry->index_offset;
16679 entry = old_entry;
16680 }
16681 return entry->index_offset;
16682}
16683
16684/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16685 constant pool entries going into the obstack CPOOL. */
b89be57b 16686
9291a0cd
TT
16687static void
16688write_hash_table (struct mapped_symtab *symtab,
16689 struct obstack *output, struct obstack *cpool)
16690{
16691 offset_type i;
3876f04e 16692 htab_t symbol_hash_table;
9291a0cd
TT
16693 htab_t str_table;
16694
3876f04e 16695 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16696 str_table = create_strtab ();
3876f04e 16697
9291a0cd
TT
16698 /* We add all the index vectors to the constant pool first, to
16699 ensure alignment is ok. */
16700 for (i = 0; i < symtab->size; ++i)
16701 {
16702 if (symtab->data[i])
3876f04e 16703 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16704 }
16705
16706 /* Now write out the hash table. */
16707 for (i = 0; i < symtab->size; ++i)
16708 {
16709 offset_type str_off, vec_off;
16710
16711 if (symtab->data[i])
16712 {
16713 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16714 vec_off = symtab->data[i]->index_offset;
16715 }
16716 else
16717 {
16718 /* While 0 is a valid constant pool index, it is not valid
16719 to have 0 for both offsets. */
16720 str_off = 0;
16721 vec_off = 0;
16722 }
16723
16724 str_off = MAYBE_SWAP (str_off);
16725 vec_off = MAYBE_SWAP (vec_off);
16726
16727 obstack_grow (output, &str_off, sizeof (str_off));
16728 obstack_grow (output, &vec_off, sizeof (vec_off));
16729 }
16730
16731 htab_delete (str_table);
3876f04e 16732 htab_delete (symbol_hash_table);
9291a0cd
TT
16733}
16734
0a5429f6
DE
16735/* Struct to map psymtab to CU index in the index file. */
16736struct psymtab_cu_index_map
16737{
16738 struct partial_symtab *psymtab;
16739 unsigned int cu_index;
16740};
16741
16742static hashval_t
16743hash_psymtab_cu_index (const void *item)
16744{
16745 const struct psymtab_cu_index_map *map = item;
16746
16747 return htab_hash_pointer (map->psymtab);
16748}
16749
16750static int
16751eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16752{
16753 const struct psymtab_cu_index_map *lhs = item_lhs;
16754 const struct psymtab_cu_index_map *rhs = item_rhs;
16755
16756 return lhs->psymtab == rhs->psymtab;
16757}
16758
16759/* Helper struct for building the address table. */
16760struct addrmap_index_data
16761{
16762 struct objfile *objfile;
16763 struct obstack *addr_obstack;
16764 htab_t cu_index_htab;
16765
16766 /* Non-zero if the previous_* fields are valid.
16767 We can't write an entry until we see the next entry (since it is only then
16768 that we know the end of the entry). */
16769 int previous_valid;
16770 /* Index of the CU in the table of all CUs in the index file. */
16771 unsigned int previous_cu_index;
0963b4bd 16772 /* Start address of the CU. */
0a5429f6
DE
16773 CORE_ADDR previous_cu_start;
16774};
16775
16776/* Write an address entry to OBSTACK. */
b89be57b 16777
9291a0cd 16778static void
0a5429f6
DE
16779add_address_entry (struct objfile *objfile, struct obstack *obstack,
16780 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16781{
0a5429f6 16782 offset_type cu_index_to_write;
9291a0cd
TT
16783 char addr[8];
16784 CORE_ADDR baseaddr;
16785
16786 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16787
0a5429f6
DE
16788 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16789 obstack_grow (obstack, addr, 8);
16790 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16791 obstack_grow (obstack, addr, 8);
16792 cu_index_to_write = MAYBE_SWAP (cu_index);
16793 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16794}
16795
16796/* Worker function for traversing an addrmap to build the address table. */
16797
16798static int
16799add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16800{
16801 struct addrmap_index_data *data = datap;
16802 struct partial_symtab *pst = obj;
16803 offset_type cu_index;
16804 void **slot;
16805
16806 if (data->previous_valid)
16807 add_address_entry (data->objfile, data->addr_obstack,
16808 data->previous_cu_start, start_addr,
16809 data->previous_cu_index);
16810
16811 data->previous_cu_start = start_addr;
16812 if (pst != NULL)
16813 {
16814 struct psymtab_cu_index_map find_map, *map;
16815 find_map.psymtab = pst;
16816 map = htab_find (data->cu_index_htab, &find_map);
16817 gdb_assert (map != NULL);
16818 data->previous_cu_index = map->cu_index;
16819 data->previous_valid = 1;
16820 }
16821 else
16822 data->previous_valid = 0;
16823
16824 return 0;
16825}
16826
16827/* Write OBJFILE's address map to OBSTACK.
16828 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16829 in the index file. */
16830
16831static void
16832write_address_map (struct objfile *objfile, struct obstack *obstack,
16833 htab_t cu_index_htab)
16834{
16835 struct addrmap_index_data addrmap_index_data;
16836
16837 /* When writing the address table, we have to cope with the fact that
16838 the addrmap iterator only provides the start of a region; we have to
16839 wait until the next invocation to get the start of the next region. */
16840
16841 addrmap_index_data.objfile = objfile;
16842 addrmap_index_data.addr_obstack = obstack;
16843 addrmap_index_data.cu_index_htab = cu_index_htab;
16844 addrmap_index_data.previous_valid = 0;
16845
16846 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16847 &addrmap_index_data);
16848
16849 /* It's highly unlikely the last entry (end address = 0xff...ff)
16850 is valid, but we should still handle it.
16851 The end address is recorded as the start of the next region, but that
16852 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16853 anyway. */
16854 if (addrmap_index_data.previous_valid)
16855 add_address_entry (objfile, obstack,
16856 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16857 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16858}
16859
16860/* Add a list of partial symbols to SYMTAB. */
b89be57b 16861
9291a0cd
TT
16862static void
16863write_psymbols (struct mapped_symtab *symtab,
987d643c 16864 htab_t psyms_seen,
9291a0cd
TT
16865 struct partial_symbol **psymp,
16866 int count,
987d643c
TT
16867 offset_type cu_index,
16868 int is_static)
9291a0cd
TT
16869{
16870 for (; count-- > 0; ++psymp)
16871 {
987d643c
TT
16872 void **slot, *lookup;
16873
9291a0cd
TT
16874 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16875 error (_("Ada is not currently supported by the index"));
987d643c
TT
16876
16877 /* We only want to add a given psymbol once. However, we also
16878 want to account for whether it is global or static. So, we
16879 may add it twice, using slightly different values. */
16880 if (is_static)
16881 {
16882 uintptr_t val = 1 | (uintptr_t) *psymp;
16883
16884 lookup = (void *) val;
16885 }
16886 else
16887 lookup = *psymp;
16888
16889 /* Only add a given psymbol once. */
16890 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16891 if (!*slot)
16892 {
16893 *slot = lookup;
bb2f58dc 16894 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16895 }
9291a0cd
TT
16896 }
16897}
16898
16899/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16900 exception if there is an error. */
b89be57b 16901
9291a0cd
TT
16902static void
16903write_obstack (FILE *file, struct obstack *obstack)
16904{
16905 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16906 file)
16907 != obstack_object_size (obstack))
16908 error (_("couldn't data write to file"));
16909}
16910
16911/* Unlink a file if the argument is not NULL. */
b89be57b 16912
9291a0cd
TT
16913static void
16914unlink_if_set (void *p)
16915{
16916 char **filename = p;
16917 if (*filename)
16918 unlink (*filename);
16919}
16920
1fd400ff
TT
16921/* A helper struct used when iterating over debug_types. */
16922struct signatured_type_index_data
16923{
16924 struct objfile *objfile;
16925 struct mapped_symtab *symtab;
16926 struct obstack *types_list;
987d643c 16927 htab_t psyms_seen;
1fd400ff
TT
16928 int cu_index;
16929};
16930
16931/* A helper function that writes a single signatured_type to an
16932 obstack. */
b89be57b 16933
1fd400ff
TT
16934static int
16935write_one_signatured_type (void **slot, void *d)
16936{
16937 struct signatured_type_index_data *info = d;
16938 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16939 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16940 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16941 gdb_byte val[8];
16942
16943 write_psymbols (info->symtab,
987d643c 16944 info->psyms_seen,
3e43a32a
MS
16945 info->objfile->global_psymbols.list
16946 + psymtab->globals_offset,
987d643c
TT
16947 psymtab->n_global_syms, info->cu_index,
16948 0);
1fd400ff 16949 write_psymbols (info->symtab,
987d643c 16950 info->psyms_seen,
3e43a32a
MS
16951 info->objfile->static_psymbols.list
16952 + psymtab->statics_offset,
987d643c
TT
16953 psymtab->n_static_syms, info->cu_index,
16954 1);
1fd400ff 16955
b64f50a1
JK
16956 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
16957 entry->per_cu.offset.sect_off);
1fd400ff 16958 obstack_grow (info->types_list, val, 8);
b64f50a1 16959 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
1fd400ff
TT
16960 obstack_grow (info->types_list, val, 8);
16961 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16962 obstack_grow (info->types_list, val, 8);
16963
16964 ++info->cu_index;
16965
16966 return 1;
16967}
16968
9291a0cd 16969/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16970
9291a0cd
TT
16971static void
16972write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16973{
16974 struct cleanup *cleanup;
16975 char *filename, *cleanup_filename;
1fd400ff
TT
16976 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16977 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16978 int i;
16979 FILE *out_file;
16980 struct mapped_symtab *symtab;
16981 offset_type val, size_of_contents, total_len;
16982 struct stat st;
16983 char buf[8];
987d643c 16984 htab_t psyms_seen;
0a5429f6
DE
16985 htab_t cu_index_htab;
16986 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16987
b4f2f049 16988 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16989 return;
b4f2f049 16990
9291a0cd
TT
16991 if (dwarf2_per_objfile->using_index)
16992 error (_("Cannot use an index to create the index"));
16993
8b70b953
TT
16994 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16995 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16996
9291a0cd 16997 if (stat (objfile->name, &st) < 0)
7e17e088 16998 perror_with_name (objfile->name);
9291a0cd
TT
16999
17000 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17001 INDEX_SUFFIX, (char *) NULL);
17002 cleanup = make_cleanup (xfree, filename);
17003
17004 out_file = fopen (filename, "wb");
17005 if (!out_file)
17006 error (_("Can't open `%s' for writing"), filename);
17007
17008 cleanup_filename = filename;
17009 make_cleanup (unlink_if_set, &cleanup_filename);
17010
17011 symtab = create_mapped_symtab ();
17012 make_cleanup (cleanup_mapped_symtab, symtab);
17013
17014 obstack_init (&addr_obstack);
17015 make_cleanup_obstack_free (&addr_obstack);
17016
17017 obstack_init (&cu_list);
17018 make_cleanup_obstack_free (&cu_list);
17019
1fd400ff
TT
17020 obstack_init (&types_cu_list);
17021 make_cleanup_obstack_free (&types_cu_list);
17022
987d643c
TT
17023 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17024 NULL, xcalloc, xfree);
96408a79 17025 make_cleanup_htab_delete (psyms_seen);
987d643c 17026
0a5429f6
DE
17027 /* While we're scanning CU's create a table that maps a psymtab pointer
17028 (which is what addrmap records) to its index (which is what is recorded
17029 in the index file). This will later be needed to write the address
17030 table. */
17031 cu_index_htab = htab_create_alloc (100,
17032 hash_psymtab_cu_index,
17033 eq_psymtab_cu_index,
17034 NULL, xcalloc, xfree);
96408a79 17035 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17036 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17037 xmalloc (sizeof (struct psymtab_cu_index_map)
17038 * dwarf2_per_objfile->n_comp_units);
17039 make_cleanup (xfree, psymtab_cu_index_map);
17040
17041 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17042 work here. Also, the debug_types entries do not appear in
17043 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17044 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17045 {
3e43a32a
MS
17046 struct dwarf2_per_cu_data *per_cu
17047 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17048 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17049 gdb_byte val[8];
0a5429f6
DE
17050 struct psymtab_cu_index_map *map;
17051 void **slot;
9291a0cd
TT
17052
17053 write_psymbols (symtab,
987d643c 17054 psyms_seen,
9291a0cd 17055 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17056 psymtab->n_global_syms, i,
17057 0);
9291a0cd 17058 write_psymbols (symtab,
987d643c 17059 psyms_seen,
9291a0cd 17060 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17061 psymtab->n_static_syms, i,
17062 1);
9291a0cd 17063
0a5429f6
DE
17064 map = &psymtab_cu_index_map[i];
17065 map->psymtab = psymtab;
17066 map->cu_index = i;
17067 slot = htab_find_slot (cu_index_htab, map, INSERT);
17068 gdb_assert (slot != NULL);
17069 gdb_assert (*slot == NULL);
17070 *slot = map;
9291a0cd 17071
b64f50a1
JK
17072 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17073 per_cu->offset.sect_off);
9291a0cd 17074 obstack_grow (&cu_list, val, 8);
e254ef6a 17075 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17076 obstack_grow (&cu_list, val, 8);
17077 }
17078
0a5429f6
DE
17079 /* Dump the address map. */
17080 write_address_map (objfile, &addr_obstack, cu_index_htab);
17081
1fd400ff
TT
17082 /* Write out the .debug_type entries, if any. */
17083 if (dwarf2_per_objfile->signatured_types)
17084 {
17085 struct signatured_type_index_data sig_data;
17086
17087 sig_data.objfile = objfile;
17088 sig_data.symtab = symtab;
17089 sig_data.types_list = &types_cu_list;
987d643c 17090 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17091 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17092 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17093 write_one_signatured_type, &sig_data);
17094 }
17095
9291a0cd
TT
17096 obstack_init (&constant_pool);
17097 make_cleanup_obstack_free (&constant_pool);
17098 obstack_init (&symtab_obstack);
17099 make_cleanup_obstack_free (&symtab_obstack);
17100 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17101
17102 obstack_init (&contents);
17103 make_cleanup_obstack_free (&contents);
1fd400ff 17104 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17105 total_len = size_of_contents;
17106
17107 /* The version number. */
481860b3 17108 val = MAYBE_SWAP (6);
9291a0cd
TT
17109 obstack_grow (&contents, &val, sizeof (val));
17110
17111 /* The offset of the CU list from the start of the file. */
17112 val = MAYBE_SWAP (total_len);
17113 obstack_grow (&contents, &val, sizeof (val));
17114 total_len += obstack_object_size (&cu_list);
17115
1fd400ff
TT
17116 /* The offset of the types CU list from the start of the file. */
17117 val = MAYBE_SWAP (total_len);
17118 obstack_grow (&contents, &val, sizeof (val));
17119 total_len += obstack_object_size (&types_cu_list);
17120
9291a0cd
TT
17121 /* The offset of the address table from the start of the file. */
17122 val = MAYBE_SWAP (total_len);
17123 obstack_grow (&contents, &val, sizeof (val));
17124 total_len += obstack_object_size (&addr_obstack);
17125
17126 /* The offset of the symbol table from the start of the file. */
17127 val = MAYBE_SWAP (total_len);
17128 obstack_grow (&contents, &val, sizeof (val));
17129 total_len += obstack_object_size (&symtab_obstack);
17130
17131 /* The offset of the constant pool from the start of the file. */
17132 val = MAYBE_SWAP (total_len);
17133 obstack_grow (&contents, &val, sizeof (val));
17134 total_len += obstack_object_size (&constant_pool);
17135
17136 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17137
17138 write_obstack (out_file, &contents);
17139 write_obstack (out_file, &cu_list);
1fd400ff 17140 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17141 write_obstack (out_file, &addr_obstack);
17142 write_obstack (out_file, &symtab_obstack);
17143 write_obstack (out_file, &constant_pool);
17144
17145 fclose (out_file);
17146
17147 /* We want to keep the file, so we set cleanup_filename to NULL
17148 here. See unlink_if_set. */
17149 cleanup_filename = NULL;
17150
17151 do_cleanups (cleanup);
17152}
17153
90476074
TT
17154/* Implementation of the `save gdb-index' command.
17155
17156 Note that the file format used by this command is documented in the
17157 GDB manual. Any changes here must be documented there. */
11570e71 17158
9291a0cd
TT
17159static void
17160save_gdb_index_command (char *arg, int from_tty)
17161{
17162 struct objfile *objfile;
17163
17164 if (!arg || !*arg)
96d19272 17165 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17166
17167 ALL_OBJFILES (objfile)
17168 {
17169 struct stat st;
17170
17171 /* If the objfile does not correspond to an actual file, skip it. */
17172 if (stat (objfile->name, &st) < 0)
17173 continue;
17174
17175 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17176 if (dwarf2_per_objfile)
17177 {
17178 volatile struct gdb_exception except;
17179
17180 TRY_CATCH (except, RETURN_MASK_ERROR)
17181 {
17182 write_psymtabs_to_index (objfile, arg);
17183 }
17184 if (except.reason < 0)
17185 exception_fprintf (gdb_stderr, except,
17186 _("Error while writing index for `%s': "),
17187 objfile->name);
17188 }
17189 }
dce234bc
PP
17190}
17191
9291a0cd
TT
17192\f
17193
9eae7c52
TT
17194int dwarf2_always_disassemble;
17195
17196static void
17197show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17198 struct cmd_list_element *c, const char *value)
17199{
3e43a32a
MS
17200 fprintf_filtered (file,
17201 _("Whether to always disassemble "
17202 "DWARF expressions is %s.\n"),
9eae7c52
TT
17203 value);
17204}
17205
900e11f9
JK
17206static void
17207show_check_physname (struct ui_file *file, int from_tty,
17208 struct cmd_list_element *c, const char *value)
17209{
17210 fprintf_filtered (file,
17211 _("Whether to check \"physname\" is %s.\n"),
17212 value);
17213}
17214
6502dd73
DJ
17215void _initialize_dwarf2_read (void);
17216
17217void
17218_initialize_dwarf2_read (void)
17219{
96d19272
JK
17220 struct cmd_list_element *c;
17221
dce234bc 17222 dwarf2_objfile_data_key
c1bd65d0 17223 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17224
1bedd215
AC
17225 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17226Set DWARF 2 specific variables.\n\
17227Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17228 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17229 0/*allow-unknown*/, &maintenance_set_cmdlist);
17230
1bedd215
AC
17231 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17232Show DWARF 2 specific variables\n\
17233Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17234 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17235 0/*allow-unknown*/, &maintenance_show_cmdlist);
17236
17237 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17238 &dwarf2_max_cache_age, _("\
17239Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17240Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17241A higher limit means that cached compilation units will be stored\n\
17242in memory longer, and more total memory will be used. Zero disables\n\
17243caching, which can slow down startup."),
2c5b56ce 17244 NULL,
920d2a44 17245 show_dwarf2_max_cache_age,
2c5b56ce 17246 &set_dwarf2_cmdlist,
ae038cb0 17247 &show_dwarf2_cmdlist);
d97bc12b 17248
9eae7c52
TT
17249 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17250 &dwarf2_always_disassemble, _("\
17251Set whether `info address' always disassembles DWARF expressions."), _("\
17252Show whether `info address' always disassembles DWARF expressions."), _("\
17253When enabled, DWARF expressions are always printed in an assembly-like\n\
17254syntax. When disabled, expressions will be printed in a more\n\
17255conversational style, when possible."),
17256 NULL,
17257 show_dwarf2_always_disassemble,
17258 &set_dwarf2_cmdlist,
17259 &show_dwarf2_cmdlist);
17260
d97bc12b
DE
17261 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17262Set debugging of the dwarf2 DIE reader."), _("\
17263Show debugging of the dwarf2 DIE reader."), _("\
17264When enabled (non-zero), DIEs are dumped after they are read in.\n\
17265The value is the maximum depth to print."),
17266 NULL,
17267 NULL,
17268 &setdebuglist, &showdebuglist);
9291a0cd 17269
900e11f9
JK
17270 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17271Set cross-checking of \"physname\" code against demangler."), _("\
17272Show cross-checking of \"physname\" code against demangler."), _("\
17273When enabled, GDB's internal \"physname\" code is checked against\n\
17274the demangler."),
17275 NULL, show_check_physname,
17276 &setdebuglist, &showdebuglist);
17277
96d19272 17278 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17279 _("\
fc1a9d6e 17280Save a gdb-index file.\n\
11570e71 17281Usage: save gdb-index DIRECTORY"),
96d19272
JK
17282 &save_cmdlist);
17283 set_cmd_completer (c, filename_completer);
6502dd73 17284}
This page took 3.104021 seconds and 4 git commands to generate.