2012-02-21 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5
PA
61#include "c-lang.h"
62#include "valprint.h"
60d5a603 63#include <ctype.h>
4c2df51b 64
c906108c
SS
65#include <fcntl.h>
66#include "gdb_string.h"
4bdf3d34 67#include "gdb_assert.h"
c906108c 68#include <sys/types.h>
233a11ab
CS
69#ifdef HAVE_ZLIB_H
70#include <zlib.h>
71#endif
dce234bc
PP
72#ifdef HAVE_MMAP
73#include <sys/mman.h>
85d9bd0e
TT
74#ifndef MAP_FAILED
75#define MAP_FAILED ((void *) -1)
76#endif
dce234bc 77#endif
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
d97bc12b
DE
82/* When non-zero, dump DIEs after they are read in. */
83static int dwarf2_die_debug = 0;
84
900e11f9
JK
85/* When non-zero, cross-check physname against demangler. */
86static int check_physname = 0;
87
dce234bc
PP
88static int pagesize;
89
df8a16a1
DJ
90/* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94static int processing_has_namespace_info;
95
6502dd73
DJ
96static const struct objfile_data *dwarf2_objfile_data_key;
97
dce234bc
PP
98struct dwarf2_section_info
99{
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
b315ab21
TG
103 /* Not NULL if the section was actually mmapped. */
104 void *map_addr;
105 /* Page aligned size of mmapped area. */
106 bfd_size_type map_len;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
120/* A description of the mapped index. The file format is described in
121 a comment by the code that writes the index. */
122struct mapped_index
123{
559a7a62
JK
124 /* Index data format version. */
125 int version;
126
9291a0cd
TT
127 /* The total length of the buffer. */
128 off_t total_size;
b11b1f88 129
9291a0cd
TT
130 /* A pointer to the address table data. */
131 const gdb_byte *address_table;
b11b1f88 132
9291a0cd
TT
133 /* Size of the address table data in bytes. */
134 offset_type address_table_size;
b11b1f88 135
3876f04e
DE
136 /* The symbol table, implemented as a hash table. */
137 const offset_type *symbol_table;
b11b1f88 138
9291a0cd 139 /* Size in slots, each slot is 2 offset_types. */
3876f04e 140 offset_type symbol_table_slots;
b11b1f88 141
9291a0cd
TT
142 /* A pointer to the constant pool. */
143 const char *constant_pool;
144};
145
9cdd5dbd
DE
146/* Collection of data recorded per objfile.
147 This hangs off of dwarf2_objfile_data_key. */
148
6502dd73
DJ
149struct dwarf2_per_objfile
150{
dce234bc
PP
151 struct dwarf2_section_info info;
152 struct dwarf2_section_info abbrev;
153 struct dwarf2_section_info line;
dce234bc
PP
154 struct dwarf2_section_info loc;
155 struct dwarf2_section_info macinfo;
cf2c3c16 156 struct dwarf2_section_info macro;
dce234bc
PP
157 struct dwarf2_section_info str;
158 struct dwarf2_section_info ranges;
159 struct dwarf2_section_info frame;
160 struct dwarf2_section_info eh_frame;
9291a0cd 161 struct dwarf2_section_info gdb_index;
ae038cb0 162
8b70b953
TT
163 VEC (dwarf2_section_info_def) *types;
164
be391dca
TT
165 /* Back link. */
166 struct objfile *objfile;
167
d467dd73 168 /* Table of all the compilation units. This is used to locate
10b3939b 169 the target compilation unit of a particular reference. */
ae038cb0
DJ
170 struct dwarf2_per_cu_data **all_comp_units;
171
172 /* The number of compilation units in ALL_COMP_UNITS. */
173 int n_comp_units;
174
1fd400ff 175 /* The number of .debug_types-related CUs. */
d467dd73 176 int n_type_units;
1fd400ff 177
d467dd73
DE
178 /* The .debug_types-related CUs (TUs). */
179 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 180
ae038cb0
DJ
181 /* A chain of compilation units that are currently read in, so that
182 they can be freed later. */
183 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 184
348e048f
DE
185 /* A table mapping .debug_types signatures to its signatured_type entry.
186 This is NULL if the .debug_types section hasn't been read in yet. */
187 htab_t signatured_types;
188
72dca2f5
FR
189 /* A flag indicating wether this objfile has a section loaded at a
190 VMA of 0. */
191 int has_section_at_zero;
9291a0cd 192
ae2de4f8
DE
193 /* True if we are using the mapped index,
194 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
195 unsigned char using_index;
196
ae2de4f8 197 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 198 struct mapped_index *index_table;
98bfdba5 199
7b9f3c50
DE
200 /* When using index_table, this keeps track of all quick_file_names entries.
201 TUs can share line table entries with CUs or other TUs, and there can be
202 a lot more TUs than unique line tables, so we maintain a separate table
203 of all line table entries to support the sharing. */
204 htab_t quick_file_names_table;
205
98bfdba5
PA
206 /* Set during partial symbol reading, to prevent queueing of full
207 symbols. */
208 int reading_partial_symbols;
673bfd45
DE
209
210 /* Table mapping type .debug_info DIE offsets to types.
211 This is NULL if not allocated yet.
212 It (currently) makes sense to allocate debug_types_type_hash lazily.
213 To keep things simple we allocate both lazily. */
214 htab_t debug_info_type_hash;
215
216 /* Table mapping type .debug_types DIE offsets to types.
217 This is NULL if not allocated yet. */
218 htab_t debug_types_type_hash;
6502dd73
DJ
219};
220
221static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 222
251d32d9 223/* Default names of the debugging sections. */
c906108c 224
233a11ab
CS
225/* Note that if the debugging section has been compressed, it might
226 have a name like .zdebug_info. */
227
9cdd5dbd
DE
228static const struct dwarf2_debug_sections dwarf2_elf_names =
229{
251d32d9
TG
230 { ".debug_info", ".zdebug_info" },
231 { ".debug_abbrev", ".zdebug_abbrev" },
232 { ".debug_line", ".zdebug_line" },
233 { ".debug_loc", ".zdebug_loc" },
234 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 235 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
236 { ".debug_str", ".zdebug_str" },
237 { ".debug_ranges", ".zdebug_ranges" },
238 { ".debug_types", ".zdebug_types" },
239 { ".debug_frame", ".zdebug_frame" },
240 { ".eh_frame", NULL },
24d3216f
TT
241 { ".gdb_index", ".zgdb_index" },
242 23
251d32d9 243};
c906108c
SS
244
245/* local data types */
246
0963b4bd 247/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
248#ifndef ABBREV_HASH_SIZE
249#define ABBREV_HASH_SIZE 121
250#endif
251
107d2387
AC
252/* The data in a compilation unit header, after target2host
253 translation, looks like this. */
c906108c 254struct comp_unit_head
a738430d 255{
c764a876 256 unsigned int length;
a738430d 257 short version;
a738430d
MK
258 unsigned char addr_size;
259 unsigned char signed_addr_p;
9cbfa09e 260 unsigned int abbrev_offset;
57349743 261
a738430d
MK
262 /* Size of file offsets; either 4 or 8. */
263 unsigned int offset_size;
57349743 264
a738430d
MK
265 /* Size of the length field; either 4 or 12. */
266 unsigned int initial_length_size;
57349743 267
a738430d
MK
268 /* Offset to the first byte of this compilation unit header in the
269 .debug_info section, for resolving relative reference dies. */
270 unsigned int offset;
57349743 271
d00adf39
DE
272 /* Offset to first die in this cu from the start of the cu.
273 This will be the first byte following the compilation unit header. */
274 unsigned int first_die_offset;
a738430d 275};
c906108c 276
3da10d80
KS
277/* Type used for delaying computation of method physnames.
278 See comments for compute_delayed_physnames. */
279struct delayed_method_info
280{
281 /* The type to which the method is attached, i.e., its parent class. */
282 struct type *type;
283
284 /* The index of the method in the type's function fieldlists. */
285 int fnfield_index;
286
287 /* The index of the method in the fieldlist. */
288 int index;
289
290 /* The name of the DIE. */
291 const char *name;
292
293 /* The DIE associated with this method. */
294 struct die_info *die;
295};
296
297typedef struct delayed_method_info delayed_method_info;
298DEF_VEC_O (delayed_method_info);
299
e7c27a73
DJ
300/* Internal state when decoding a particular compilation unit. */
301struct dwarf2_cu
302{
303 /* The objfile containing this compilation unit. */
304 struct objfile *objfile;
305
d00adf39 306 /* The header of the compilation unit. */
e7c27a73 307 struct comp_unit_head header;
e142c38c 308
d00adf39
DE
309 /* Base address of this compilation unit. */
310 CORE_ADDR base_address;
311
312 /* Non-zero if base_address has been set. */
313 int base_known;
314
e142c38c
DJ
315 /* The language we are debugging. */
316 enum language language;
317 const struct language_defn *language_defn;
318
b0f35d58
DL
319 const char *producer;
320
e142c38c
DJ
321 /* The generic symbol table building routines have separate lists for
322 file scope symbols and all all other scopes (local scopes). So
323 we need to select the right one to pass to add_symbol_to_list().
324 We do it by keeping a pointer to the correct list in list_in_scope.
325
326 FIXME: The original dwarf code just treated the file scope as the
327 first local scope, and all other local scopes as nested local
328 scopes, and worked fine. Check to see if we really need to
329 distinguish these in buildsym.c. */
330 struct pending **list_in_scope;
331
f3dd6933
DJ
332 /* DWARF abbreviation table associated with this compilation unit. */
333 struct abbrev_info **dwarf2_abbrevs;
334
335 /* Storage for the abbrev table. */
336 struct obstack abbrev_obstack;
72bf9492
DJ
337
338 /* Hash table holding all the loaded partial DIEs. */
339 htab_t partial_dies;
340
341 /* Storage for things with the same lifetime as this read-in compilation
342 unit, including partial DIEs. */
343 struct obstack comp_unit_obstack;
344
ae038cb0
DJ
345 /* When multiple dwarf2_cu structures are living in memory, this field
346 chains them all together, so that they can be released efficiently.
347 We will probably also want a generation counter so that most-recently-used
348 compilation units are cached... */
349 struct dwarf2_per_cu_data *read_in_chain;
350
351 /* Backchain to our per_cu entry if the tree has been built. */
352 struct dwarf2_per_cu_data *per_cu;
353
354 /* How many compilation units ago was this CU last referenced? */
355 int last_used;
356
10b3939b 357 /* A hash table of die offsets for following references. */
51545339 358 htab_t die_hash;
10b3939b
DJ
359
360 /* Full DIEs if read in. */
361 struct die_info *dies;
362
363 /* A set of pointers to dwarf2_per_cu_data objects for compilation
364 units referenced by this one. Only set during full symbol processing;
365 partial symbol tables do not have dependencies. */
366 htab_t dependencies;
367
cb1df416
DJ
368 /* Header data from the line table, during full symbol processing. */
369 struct line_header *line_header;
370
3da10d80
KS
371 /* A list of methods which need to have physnames computed
372 after all type information has been read. */
373 VEC (delayed_method_info) *method_list;
374
96408a79
SA
375 /* To be copied to symtab->call_site_htab. */
376 htab_t call_site_htab;
377
ae038cb0
DJ
378 /* Mark used when releasing cached dies. */
379 unsigned int mark : 1;
380
381 /* This flag will be set if this compilation unit might include
382 inter-compilation-unit references. */
383 unsigned int has_form_ref_addr : 1;
384
72bf9492
DJ
385 /* This flag will be set if this compilation unit includes any
386 DW_TAG_namespace DIEs. If we know that there are explicit
387 DIEs for namespaces, we don't need to try to infer them
388 from mangled names. */
389 unsigned int has_namespace_info : 1;
8be455d7
JK
390
391 /* This CU references .debug_loc. See the symtab->locations_valid field.
392 This test is imperfect as there may exist optimized debug code not using
393 any location list and still facing inlining issues if handled as
394 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 395 unsigned int has_loclist : 1;
e7c27a73
DJ
396};
397
10b3939b
DJ
398/* Persistent data held for a compilation unit, even when not
399 processing it. We put a pointer to this structure in the
28dee7f5 400 read_symtab_private field of the psymtab. */
10b3939b 401
ae038cb0
DJ
402struct dwarf2_per_cu_data
403{
348e048f 404 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 405 bytes should suffice to store the length of any compilation unit
45452591
DE
406 - if it doesn't, GDB will fall over anyway.
407 NOTE: Unlike comp_unit_head.length, this length includes
408 initial_length_size. */
c764a876 409 unsigned int offset;
348e048f 410 unsigned int length : 29;
ae038cb0
DJ
411
412 /* Flag indicating this compilation unit will be read in before
413 any of the current compilation units are processed. */
c764a876 414 unsigned int queued : 1;
ae038cb0 415
5afb4e99
DJ
416 /* This flag will be set if we need to load absolutely all DIEs
417 for this compilation unit, instead of just the ones we think
418 are interesting. It gets set if we look for a DIE in the
419 hash table and don't find it. */
420 unsigned int load_all_dies : 1;
421
8b70b953
TT
422 /* Non-null if this CU is from .debug_types; in which case it points
423 to the section. Otherwise it's from .debug_info. */
b0df02fd 424 struct dwarf2_section_info *debug_types_section;
348e048f 425
17ea53c3
JK
426 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
427 of the CU cache it gets reset to NULL again. */
ae038cb0 428 struct dwarf2_cu *cu;
1c379e20 429
9cdd5dbd
DE
430 /* The corresponding objfile.
431 Normally we can get the objfile from dwarf2_per_objfile.
432 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
433 struct objfile *objfile;
434
435 /* When using partial symbol tables, the 'psymtab' field is active.
436 Otherwise the 'quick' field is active. */
437 union
438 {
439 /* The partial symbol table associated with this compilation unit,
440 or NULL for partial units (which do not have an associated
441 symtab). */
442 struct partial_symtab *psymtab;
443
444 /* Data needed by the "quick" functions. */
445 struct dwarf2_per_cu_quick_data *quick;
446 } v;
ae038cb0
DJ
447};
448
348e048f
DE
449/* Entry in the signatured_types hash table. */
450
451struct signatured_type
452{
453 ULONGEST signature;
454
348e048f
DE
455 /* Offset in .debug_types of the type defined by this TU. */
456 unsigned int type_offset;
457
458 /* The CU(/TU) of this type. */
459 struct dwarf2_per_cu_data per_cu;
460};
461
0963b4bd
MS
462/* Struct used to pass misc. parameters to read_die_and_children, et
463 al. which are used for both .debug_info and .debug_types dies.
464 All parameters here are unchanging for the life of the call. This
465 struct exists to abstract away the constant parameters of die
466 reading. */
93311388
DE
467
468struct die_reader_specs
469{
470 /* The bfd of this objfile. */
471 bfd* abfd;
472
473 /* The CU of the DIE we are parsing. */
474 struct dwarf2_cu *cu;
475
476 /* Pointer to start of section buffer.
477 This is either the start of .debug_info or .debug_types. */
478 const gdb_byte *buffer;
479};
480
debd256d
JB
481/* The line number information for a compilation unit (found in the
482 .debug_line section) begins with a "statement program header",
483 which contains the following information. */
484struct line_header
485{
486 unsigned int total_length;
487 unsigned short version;
488 unsigned int header_length;
489 unsigned char minimum_instruction_length;
2dc7f7b3 490 unsigned char maximum_ops_per_instruction;
debd256d
JB
491 unsigned char default_is_stmt;
492 int line_base;
493 unsigned char line_range;
494 unsigned char opcode_base;
495
496 /* standard_opcode_lengths[i] is the number of operands for the
497 standard opcode whose value is i. This means that
498 standard_opcode_lengths[0] is unused, and the last meaningful
499 element is standard_opcode_lengths[opcode_base - 1]. */
500 unsigned char *standard_opcode_lengths;
501
502 /* The include_directories table. NOTE! These strings are not
503 allocated with xmalloc; instead, they are pointers into
504 debug_line_buffer. If you try to free them, `free' will get
505 indigestion. */
506 unsigned int num_include_dirs, include_dirs_size;
507 char **include_dirs;
508
509 /* The file_names table. NOTE! These strings are not allocated
510 with xmalloc; instead, they are pointers into debug_line_buffer.
511 Don't try to free them directly. */
512 unsigned int num_file_names, file_names_size;
513 struct file_entry
c906108c 514 {
debd256d
JB
515 char *name;
516 unsigned int dir_index;
517 unsigned int mod_time;
518 unsigned int length;
aaa75496 519 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 520 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
521 } *file_names;
522
523 /* The start and end of the statement program following this
6502dd73 524 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 525 gdb_byte *statement_program_start, *statement_program_end;
debd256d 526};
c906108c
SS
527
528/* When we construct a partial symbol table entry we only
0963b4bd 529 need this much information. */
c906108c
SS
530struct partial_die_info
531 {
72bf9492 532 /* Offset of this DIE. */
c906108c 533 unsigned int offset;
72bf9492
DJ
534
535 /* DWARF-2 tag for this DIE. */
536 ENUM_BITFIELD(dwarf_tag) tag : 16;
537
72bf9492
DJ
538 /* Assorted flags describing the data found in this DIE. */
539 unsigned int has_children : 1;
540 unsigned int is_external : 1;
541 unsigned int is_declaration : 1;
542 unsigned int has_type : 1;
543 unsigned int has_specification : 1;
544 unsigned int has_pc_info : 1;
545
546 /* Flag set if the SCOPE field of this structure has been
547 computed. */
548 unsigned int scope_set : 1;
549
fa4028e9
JB
550 /* Flag set if the DIE has a byte_size attribute. */
551 unsigned int has_byte_size : 1;
552
98bfdba5
PA
553 /* Flag set if any of the DIE's children are template arguments. */
554 unsigned int has_template_arguments : 1;
555
abc72ce4
DE
556 /* Flag set if fixup_partial_die has been called on this die. */
557 unsigned int fixup_called : 1;
558
72bf9492 559 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 560 sometimes a default name for unnamed DIEs. */
c906108c 561 char *name;
72bf9492 562
abc72ce4
DE
563 /* The linkage name, if present. */
564 const char *linkage_name;
565
72bf9492
DJ
566 /* The scope to prepend to our children. This is generally
567 allocated on the comp_unit_obstack, so will disappear
568 when this compilation unit leaves the cache. */
569 char *scope;
570
571 /* The location description associated with this DIE, if any. */
572 struct dwarf_block *locdesc;
573
574 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
575 CORE_ADDR lowpc;
576 CORE_ADDR highpc;
72bf9492 577
93311388 578 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 579 DW_AT_sibling, if any. */
abc72ce4
DE
580 /* NOTE: This member isn't strictly necessary, read_partial_die could
581 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 582 gdb_byte *sibling;
72bf9492
DJ
583
584 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
585 DW_AT_specification (or DW_AT_abstract_origin or
586 DW_AT_extension). */
587 unsigned int spec_offset;
588
589 /* Pointers to this DIE's parent, first child, and next sibling,
590 if any. */
591 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
592 };
593
0963b4bd 594/* This data structure holds the information of an abbrev. */
c906108c
SS
595struct abbrev_info
596 {
597 unsigned int number; /* number identifying abbrev */
598 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
599 unsigned short has_children; /* boolean */
600 unsigned short num_attrs; /* number of attributes */
c906108c
SS
601 struct attr_abbrev *attrs; /* an array of attribute descriptions */
602 struct abbrev_info *next; /* next in chain */
603 };
604
605struct attr_abbrev
606 {
9d25dd43
DE
607 ENUM_BITFIELD(dwarf_attribute) name : 16;
608 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
609 };
610
0963b4bd 611/* Attributes have a name and a value. */
b60c80d6
DJ
612struct attribute
613 {
9d25dd43 614 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
615 ENUM_BITFIELD(dwarf_form) form : 15;
616
617 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
618 field should be in u.str (existing only for DW_STRING) but it is kept
619 here for better struct attribute alignment. */
620 unsigned int string_is_canonical : 1;
621
b60c80d6
DJ
622 union
623 {
624 char *str;
625 struct dwarf_block *blk;
43bbcdc2
PH
626 ULONGEST unsnd;
627 LONGEST snd;
b60c80d6 628 CORE_ADDR addr;
348e048f 629 struct signatured_type *signatured_type;
b60c80d6
DJ
630 }
631 u;
632 };
633
0963b4bd 634/* This data structure holds a complete die structure. */
c906108c
SS
635struct die_info
636 {
76815b17
DE
637 /* DWARF-2 tag for this DIE. */
638 ENUM_BITFIELD(dwarf_tag) tag : 16;
639
640 /* Number of attributes */
98bfdba5
PA
641 unsigned char num_attrs;
642
643 /* True if we're presently building the full type name for the
644 type derived from this DIE. */
645 unsigned char building_fullname : 1;
76815b17
DE
646
647 /* Abbrev number */
648 unsigned int abbrev;
649
93311388 650 /* Offset in .debug_info or .debug_types section. */
76815b17 651 unsigned int offset;
78ba4af6
JB
652
653 /* The dies in a compilation unit form an n-ary tree. PARENT
654 points to this die's parent; CHILD points to the first child of
655 this node; and all the children of a given node are chained
4950bc1c 656 together via their SIBLING fields. */
639d11d3
DC
657 struct die_info *child; /* Its first child, if any. */
658 struct die_info *sibling; /* Its next sibling, if any. */
659 struct die_info *parent; /* Its parent, if any. */
c906108c 660
b60c80d6
DJ
661 /* An array of attributes, with NUM_ATTRS elements. There may be
662 zero, but it's not common and zero-sized arrays are not
663 sufficiently portable C. */
664 struct attribute attrs[1];
c906108c
SS
665 };
666
0963b4bd 667/* Get at parts of an attribute structure. */
c906108c
SS
668
669#define DW_STRING(attr) ((attr)->u.str)
8285870a 670#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
671#define DW_UNSND(attr) ((attr)->u.unsnd)
672#define DW_BLOCK(attr) ((attr)->u.blk)
673#define DW_SND(attr) ((attr)->u.snd)
674#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 675#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 676
0963b4bd 677/* Blocks are a bunch of untyped bytes. */
c906108c
SS
678struct dwarf_block
679 {
680 unsigned int size;
1d6edc3c
JK
681
682 /* Valid only if SIZE is not zero. */
fe1b8b76 683 gdb_byte *data;
c906108c
SS
684 };
685
c906108c
SS
686#ifndef ATTR_ALLOC_CHUNK
687#define ATTR_ALLOC_CHUNK 4
688#endif
689
c906108c
SS
690/* Allocate fields for structs, unions and enums in this size. */
691#ifndef DW_FIELD_ALLOC_CHUNK
692#define DW_FIELD_ALLOC_CHUNK 4
693#endif
694
c906108c
SS
695/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
696 but this would require a corresponding change in unpack_field_as_long
697 and friends. */
698static int bits_per_byte = 8;
699
700/* The routines that read and process dies for a C struct or C++ class
701 pass lists of data member fields and lists of member function fields
702 in an instance of a field_info structure, as defined below. */
703struct field_info
c5aa993b 704 {
0963b4bd 705 /* List of data member and baseclasses fields. */
c5aa993b
JM
706 struct nextfield
707 {
708 struct nextfield *next;
709 int accessibility;
710 int virtuality;
711 struct field field;
712 }
7d0ccb61 713 *fields, *baseclasses;
c906108c 714
7d0ccb61 715 /* Number of fields (including baseclasses). */
c5aa993b 716 int nfields;
c906108c 717
c5aa993b
JM
718 /* Number of baseclasses. */
719 int nbaseclasses;
c906108c 720
c5aa993b
JM
721 /* Set if the accesibility of one of the fields is not public. */
722 int non_public_fields;
c906108c 723
c5aa993b
JM
724 /* Member function fields array, entries are allocated in the order they
725 are encountered in the object file. */
726 struct nextfnfield
727 {
728 struct nextfnfield *next;
729 struct fn_field fnfield;
730 }
731 *fnfields;
c906108c 732
c5aa993b
JM
733 /* Member function fieldlist array, contains name of possibly overloaded
734 member function, number of overloaded member functions and a pointer
735 to the head of the member function field chain. */
736 struct fnfieldlist
737 {
738 char *name;
739 int length;
740 struct nextfnfield *head;
741 }
742 *fnfieldlists;
c906108c 743
c5aa993b
JM
744 /* Number of entries in the fnfieldlists array. */
745 int nfnfields;
98751a41
JK
746
747 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
748 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
749 struct typedef_field_list
750 {
751 struct typedef_field field;
752 struct typedef_field_list *next;
753 }
754 *typedef_field_list;
755 unsigned typedef_field_list_count;
c5aa993b 756 };
c906108c 757
10b3939b
DJ
758/* One item on the queue of compilation units to read in full symbols
759 for. */
760struct dwarf2_queue_item
761{
762 struct dwarf2_per_cu_data *per_cu;
763 struct dwarf2_queue_item *next;
764};
765
766/* The current queue. */
767static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
768
ae038cb0
DJ
769/* Loaded secondary compilation units are kept in memory until they
770 have not been referenced for the processing of this many
771 compilation units. Set this to zero to disable caching. Cache
772 sizes of up to at least twenty will improve startup time for
773 typical inter-CU-reference binaries, at an obvious memory cost. */
774static int dwarf2_max_cache_age = 5;
920d2a44
AC
775static void
776show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
777 struct cmd_list_element *c, const char *value)
778{
3e43a32a
MS
779 fprintf_filtered (file, _("The upper bound on the age of cached "
780 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
781 value);
782}
783
ae038cb0 784
0963b4bd 785/* Various complaints about symbol reading that don't abort the process. */
c906108c 786
4d3c2250
KB
787static void
788dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 789{
4d3c2250 790 complaint (&symfile_complaints,
e2e0b3e5 791 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
792}
793
25e43795
DJ
794static void
795dwarf2_debug_line_missing_file_complaint (void)
796{
797 complaint (&symfile_complaints,
798 _(".debug_line section has line data without a file"));
799}
800
59205f5a
JB
801static void
802dwarf2_debug_line_missing_end_sequence_complaint (void)
803{
804 complaint (&symfile_complaints,
3e43a32a
MS
805 _(".debug_line section has line "
806 "program sequence without an end"));
59205f5a
JB
807}
808
4d3c2250
KB
809static void
810dwarf2_complex_location_expr_complaint (void)
2e276125 811{
e2e0b3e5 812 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
813}
814
4d3c2250
KB
815static void
816dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
817 int arg3)
2e276125 818{
4d3c2250 819 complaint (&symfile_complaints,
3e43a32a
MS
820 _("const value length mismatch for '%s', got %d, expected %d"),
821 arg1, arg2, arg3);
4d3c2250
KB
822}
823
824static void
cf2c3c16 825dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 826{
4d3c2250 827 complaint (&symfile_complaints,
cf2c3c16
TT
828 _("macro info runs off end of `%s' section"),
829 section->asection->name);
4d3c2250
KB
830}
831
832static void
833dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 834{
4d3c2250 835 complaint (&symfile_complaints,
3e43a32a
MS
836 _("macro debug info contains a "
837 "malformed macro definition:\n`%s'"),
4d3c2250
KB
838 arg1);
839}
840
841static void
842dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 843{
4d3c2250 844 complaint (&symfile_complaints,
3e43a32a
MS
845 _("invalid attribute class or form for '%s' in '%s'"),
846 arg1, arg2);
4d3c2250 847}
c906108c 848
c906108c
SS
849/* local function prototypes */
850
4efb68b1 851static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 852
aaa75496
JB
853static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
854 struct objfile *);
855
918dd910
JK
856static void dwarf2_find_base_address (struct die_info *die,
857 struct dwarf2_cu *cu);
858
c67a9c90 859static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 860
72bf9492
DJ
861static void scan_partial_symbols (struct partial_die_info *,
862 CORE_ADDR *, CORE_ADDR *,
5734ee8b 863 int, struct dwarf2_cu *);
c906108c 864
72bf9492
DJ
865static void add_partial_symbol (struct partial_die_info *,
866 struct dwarf2_cu *);
63d06c5c 867
72bf9492
DJ
868static void add_partial_namespace (struct partial_die_info *pdi,
869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 870 int need_pc, struct dwarf2_cu *cu);
63d06c5c 871
5d7cb8df
JK
872static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
873 CORE_ADDR *highpc, int need_pc,
874 struct dwarf2_cu *cu);
875
72bf9492
DJ
876static void add_partial_enumeration (struct partial_die_info *enum_pdi,
877 struct dwarf2_cu *cu);
91c24f0a 878
bc30ff58
JB
879static void add_partial_subprogram (struct partial_die_info *pdi,
880 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 881 int need_pc, struct dwarf2_cu *cu);
bc30ff58 882
fe1b8b76 883static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
884 gdb_byte *buffer, gdb_byte *info_ptr,
885 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 886
a14ed312 887static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 888
a14ed312 889static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 890
e5fe5e75 891static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 892
f3dd6933 893static void dwarf2_free_abbrev_table (void *);
c906108c 894
6caca83c
CC
895static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
896
fe1b8b76 897static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 898 struct dwarf2_cu *);
72bf9492 899
57349743 900static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 901 struct dwarf2_cu *);
c906108c 902
93311388
DE
903static struct partial_die_info *load_partial_dies (bfd *,
904 gdb_byte *, gdb_byte *,
905 int, struct dwarf2_cu *);
72bf9492 906
fe1b8b76 907static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
908 struct abbrev_info *abbrev,
909 unsigned int, bfd *,
910 gdb_byte *, gdb_byte *,
911 struct dwarf2_cu *);
c906108c 912
c764a876 913static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 914 struct dwarf2_cu *);
72bf9492
DJ
915
916static void fixup_partial_die (struct partial_die_info *,
917 struct dwarf2_cu *);
918
fe1b8b76
JB
919static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
920 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 921
fe1b8b76
JB
922static gdb_byte *read_attribute_value (struct attribute *, unsigned,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 924
fe1b8b76 925static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 926
fe1b8b76 927static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 928
fe1b8b76 929static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 930
fe1b8b76 931static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 932
93311388 933static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 934
fe1b8b76 935static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 936 unsigned int *);
c906108c 937
c764a876
DE
938static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
939
940static LONGEST read_checked_initial_length_and_offset
941 (bfd *, gdb_byte *, const struct comp_unit_head *,
942 unsigned int *, unsigned int *);
613e1657 943
fe1b8b76 944static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
945 unsigned int *);
946
947static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 948
fe1b8b76 949static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 950
9b1c24c8 951static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 952
fe1b8b76
JB
953static char *read_indirect_string (bfd *, gdb_byte *,
954 const struct comp_unit_head *,
955 unsigned int *);
4bdf3d34 956
fe1b8b76 957static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 958
fe1b8b76 959static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 960
fe1b8b76 961static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 962
e142c38c 963static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 964
e142c38c
DJ
965static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
966 struct dwarf2_cu *);
c906108c 967
348e048f
DE
968static struct attribute *dwarf2_attr_no_follow (struct die_info *,
969 unsigned int,
970 struct dwarf2_cu *);
971
05cf31d1
JB
972static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
973 struct dwarf2_cu *cu);
974
e142c38c 975static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 976
e142c38c 977static struct die_info *die_specification (struct die_info *die,
f2f0e013 978 struct dwarf2_cu **);
63d06c5c 979
debd256d
JB
980static void free_line_header (struct line_header *lh);
981
aaa75496
JB
982static void add_file_name (struct line_header *, char *, unsigned int,
983 unsigned int, unsigned int);
984
debd256d
JB
985static struct line_header *(dwarf_decode_line_header
986 (unsigned int offset,
e7c27a73 987 bfd *abfd, struct dwarf2_cu *cu));
debd256d 988
f3f5162e
DE
989static void dwarf_decode_lines (struct line_header *, const char *,
990 struct dwarf2_cu *, struct partial_symtab *,
991 int);
c906108c 992
72b9f47f 993static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 994
a14ed312 995static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 996 struct dwarf2_cu *);
c906108c 997
34eaf542
TT
998static struct symbol *new_symbol_full (struct die_info *, struct type *,
999 struct dwarf2_cu *, struct symbol *);
1000
a14ed312 1001static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1002 struct dwarf2_cu *);
c906108c 1003
98bfdba5
PA
1004static void dwarf2_const_value_attr (struct attribute *attr,
1005 struct type *type,
1006 const char *name,
1007 struct obstack *obstack,
1008 struct dwarf2_cu *cu, long *value,
1009 gdb_byte **bytes,
1010 struct dwarf2_locexpr_baton **baton);
2df3850c 1011
e7c27a73 1012static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1013
b4ba55a1
JB
1014static int need_gnat_info (struct dwarf2_cu *);
1015
3e43a32a
MS
1016static struct type *die_descriptive_type (struct die_info *,
1017 struct dwarf2_cu *);
b4ba55a1
JB
1018
1019static void set_descriptive_type (struct type *, struct die_info *,
1020 struct dwarf2_cu *);
1021
e7c27a73
DJ
1022static struct type *die_containing_type (struct die_info *,
1023 struct dwarf2_cu *);
c906108c 1024
673bfd45
DE
1025static struct type *lookup_die_type (struct die_info *, struct attribute *,
1026 struct dwarf2_cu *);
c906108c 1027
f792889a 1028static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1029
673bfd45
DE
1030static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1031
0d5cff50 1032static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1033
6e70227d 1034static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1035 const char *suffix, int physname,
1036 struct dwarf2_cu *cu);
63d06c5c 1037
e7c27a73 1038static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1039
348e048f
DE
1040static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1041
e7c27a73 1042static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
e7c27a73 1044static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1045
96408a79
SA
1046static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1047
ff013f42
JK
1048static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1049 struct dwarf2_cu *, struct partial_symtab *);
1050
a14ed312 1051static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1052 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1053 struct partial_symtab *);
c906108c 1054
fae299cd
DC
1055static void get_scope_pc_bounds (struct die_info *,
1056 CORE_ADDR *, CORE_ADDR *,
1057 struct dwarf2_cu *);
1058
801e3a5b
JB
1059static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1060 CORE_ADDR, struct dwarf2_cu *);
1061
a14ed312 1062static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1063 struct dwarf2_cu *);
c906108c 1064
a14ed312 1065static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1066 struct type *, struct dwarf2_cu *);
c906108c 1067
a14ed312 1068static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1069 struct die_info *, struct type *,
e7c27a73 1070 struct dwarf2_cu *);
c906108c 1071
a14ed312 1072static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1073 struct type *,
1074 struct dwarf2_cu *);
c906108c 1075
134d01f1 1076static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1077
e7c27a73 1078static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1079
e7c27a73 1080static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1081
5d7cb8df
JK
1082static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1083
27aa8d6a
SW
1084static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1085
f55ee35c
JK
1086static struct type *read_module_type (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
38d518c9 1089static const char *namespace_name (struct die_info *die,
e142c38c 1090 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1091
134d01f1 1092static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1093
e7c27a73 1094static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1095
6e70227d 1096static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1097 struct dwarf2_cu *);
1098
93311388 1099static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1100
93311388
DE
1101static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1102 gdb_byte *info_ptr,
d97bc12b
DE
1103 gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1105
93311388
DE
1106static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1107 gdb_byte *info_ptr,
fe1b8b76 1108 gdb_byte **new_info_ptr,
639d11d3
DC
1109 struct die_info *parent);
1110
93311388
DE
1111static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1112 gdb_byte *info_ptr,
fe1b8b76 1113 gdb_byte **new_info_ptr,
639d11d3
DC
1114 struct die_info *parent);
1115
93311388
DE
1116static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1117 struct die_info **, gdb_byte *,
1118 int *);
1119
e7c27a73 1120static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1121
71c25dea
TT
1122static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1123 struct obstack *);
1124
e142c38c 1125static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1126
98bfdba5
PA
1127static const char *dwarf2_full_name (char *name,
1128 struct die_info *die,
1129 struct dwarf2_cu *cu);
1130
e142c38c 1131static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1132 struct dwarf2_cu **);
9219021c 1133
a14ed312 1134static char *dwarf_tag_name (unsigned int);
c906108c 1135
a14ed312 1136static char *dwarf_attr_name (unsigned int);
c906108c 1137
a14ed312 1138static char *dwarf_form_name (unsigned int);
c906108c 1139
a14ed312 1140static char *dwarf_bool_name (unsigned int);
c906108c 1141
a14ed312 1142static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1143
1144#if 0
a14ed312 1145static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1146#endif
1147
f9aca02d 1148static struct die_info *sibling_die (struct die_info *);
c906108c 1149
d97bc12b
DE
1150static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1151
1152static void dump_die_for_error (struct die_info *);
1153
1154static void dump_die_1 (struct ui_file *, int level, int max_level,
1155 struct die_info *);
c906108c 1156
d97bc12b 1157/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1158
51545339 1159static void store_in_ref_table (struct die_info *,
10b3939b 1160 struct dwarf2_cu *);
c906108c 1161
93311388
DE
1162static int is_ref_attr (struct attribute *);
1163
c764a876 1164static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1165
43bbcdc2 1166static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1167
348e048f
DE
1168static struct die_info *follow_die_ref_or_sig (struct die_info *,
1169 struct attribute *,
1170 struct dwarf2_cu **);
1171
10b3939b
DJ
1172static struct die_info *follow_die_ref (struct die_info *,
1173 struct attribute *,
f2f0e013 1174 struct dwarf2_cu **);
c906108c 1175
348e048f
DE
1176static struct die_info *follow_die_sig (struct die_info *,
1177 struct attribute *,
1178 struct dwarf2_cu **);
1179
6c83ed52
TT
1180static struct signatured_type *lookup_signatured_type_at_offset
1181 (struct objfile *objfile,
1182 struct dwarf2_section_info *section,
1183 unsigned int offset);
1184
e5fe5e75 1185static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1186
a0f42c21 1187static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1188
c906108c
SS
1189/* memory allocation interface */
1190
7b5a2f43 1191static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1192
f3dd6933 1193static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1194
b60c80d6 1195static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1196
2e276125 1197static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1198 char *, bfd *, struct dwarf2_cu *,
1199 struct dwarf2_section_info *,
1200 int);
2e276125 1201
8e19ed76
PS
1202static int attr_form_is_block (struct attribute *);
1203
3690dd37
JB
1204static int attr_form_is_section_offset (struct attribute *);
1205
1206static int attr_form_is_constant (struct attribute *);
1207
8cf6f0b1
TT
1208static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1209 struct dwarf2_loclist_baton *baton,
1210 struct attribute *attr);
1211
93e7bd98
DJ
1212static void dwarf2_symbol_mark_computed (struct attribute *attr,
1213 struct symbol *sym,
1214 struct dwarf2_cu *cu);
4c2df51b 1215
93311388
DE
1216static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1217 struct abbrev_info *abbrev,
1218 struct dwarf2_cu *cu);
4bb7a0a7 1219
72bf9492
DJ
1220static void free_stack_comp_unit (void *);
1221
72bf9492
DJ
1222static hashval_t partial_die_hash (const void *item);
1223
1224static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1225
ae038cb0 1226static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1227 (unsigned int offset, struct objfile *objfile);
ae038cb0 1228
9816fde3 1229static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1230 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1231
1232static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1233 struct die_info *comp_unit_die);
93311388 1234
68dc6402 1235static void free_heap_comp_unit (void *);
ae038cb0
DJ
1236
1237static void free_cached_comp_units (void *);
1238
1239static void age_cached_comp_units (void);
1240
1241static void free_one_cached_comp_unit (void *);
1242
f792889a
DJ
1243static struct type *set_die_type (struct die_info *, struct type *,
1244 struct dwarf2_cu *);
1c379e20 1245
ae038cb0
DJ
1246static void create_all_comp_units (struct objfile *);
1247
1fd400ff
TT
1248static int create_debug_types_hash_table (struct objfile *objfile);
1249
a0f42c21 1250static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1251
1252static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1253
1254static void dwarf2_add_dependence (struct dwarf2_cu *,
1255 struct dwarf2_per_cu_data *);
1256
ae038cb0
DJ
1257static void dwarf2_mark (struct dwarf2_cu *);
1258
1259static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1260
673bfd45
DE
1261static struct type *get_die_type_at_offset (unsigned int,
1262 struct dwarf2_per_cu_data *per_cu);
1263
f792889a 1264static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1265
9291a0cd
TT
1266static void dwarf2_release_queue (void *dummy);
1267
a0f42c21 1268static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1269
a0f42c21 1270static void process_queue (void);
9291a0cd
TT
1271
1272static void find_file_and_directory (struct die_info *die,
1273 struct dwarf2_cu *cu,
1274 char **name, char **comp_dir);
1275
1276static char *file_full_name (int file, struct line_header *lh,
1277 const char *comp_dir);
1278
9ff913ba
DE
1279static gdb_byte *read_and_check_comp_unit_head
1280 (struct comp_unit_head *header,
1281 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1282 int is_debug_types_section);
9291a0cd
TT
1283
1284static void init_cu_die_reader (struct die_reader_specs *reader,
1285 struct dwarf2_cu *cu);
1286
673bfd45 1287static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1288
9291a0cd
TT
1289#if WORDS_BIGENDIAN
1290
1291/* Convert VALUE between big- and little-endian. */
1292static offset_type
1293byte_swap (offset_type value)
1294{
1295 offset_type result;
1296
1297 result = (value & 0xff) << 24;
1298 result |= (value & 0xff00) << 8;
1299 result |= (value & 0xff0000) >> 8;
1300 result |= (value & 0xff000000) >> 24;
1301 return result;
1302}
1303
1304#define MAYBE_SWAP(V) byte_swap (V)
1305
1306#else
1307#define MAYBE_SWAP(V) (V)
1308#endif /* WORDS_BIGENDIAN */
1309
1310/* The suffix for an index file. */
1311#define INDEX_SUFFIX ".gdb-index"
1312
3da10d80
KS
1313static const char *dwarf2_physname (char *name, struct die_info *die,
1314 struct dwarf2_cu *cu);
1315
c906108c 1316/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1317 information and return true if we have enough to do something.
1318 NAMES points to the dwarf2 section names, or is NULL if the standard
1319 ELF names are used. */
c906108c
SS
1320
1321int
251d32d9
TG
1322dwarf2_has_info (struct objfile *objfile,
1323 const struct dwarf2_debug_sections *names)
c906108c 1324{
be391dca
TT
1325 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1326 if (!dwarf2_per_objfile)
1327 {
1328 /* Initialize per-objfile state. */
1329 struct dwarf2_per_objfile *data
1330 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1331
be391dca
TT
1332 memset (data, 0, sizeof (*data));
1333 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1334 dwarf2_per_objfile = data;
6502dd73 1335
251d32d9
TG
1336 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1337 (void *) names);
be391dca
TT
1338 dwarf2_per_objfile->objfile = objfile;
1339 }
1340 return (dwarf2_per_objfile->info.asection != NULL
1341 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1342}
1343
251d32d9
TG
1344/* When loading sections, we look either for uncompressed section or for
1345 compressed section names. */
233a11ab
CS
1346
1347static int
251d32d9
TG
1348section_is_p (const char *section_name,
1349 const struct dwarf2_section_names *names)
233a11ab 1350{
251d32d9
TG
1351 if (names->normal != NULL
1352 && strcmp (section_name, names->normal) == 0)
1353 return 1;
1354 if (names->compressed != NULL
1355 && strcmp (section_name, names->compressed) == 0)
1356 return 1;
1357 return 0;
233a11ab
CS
1358}
1359
c906108c
SS
1360/* This function is mapped across the sections and remembers the
1361 offset and size of each of the debugging sections we are interested
1362 in. */
1363
1364static void
251d32d9 1365dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1366{
251d32d9
TG
1367 const struct dwarf2_debug_sections *names;
1368
1369 if (vnames == NULL)
1370 names = &dwarf2_elf_names;
1371 else
1372 names = (const struct dwarf2_debug_sections *) vnames;
1373
1374 if (section_is_p (sectp->name, &names->info))
c906108c 1375 {
dce234bc
PP
1376 dwarf2_per_objfile->info.asection = sectp;
1377 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1378 }
251d32d9 1379 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1380 {
dce234bc
PP
1381 dwarf2_per_objfile->abbrev.asection = sectp;
1382 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1383 }
251d32d9 1384 else if (section_is_p (sectp->name, &names->line))
c906108c 1385 {
dce234bc
PP
1386 dwarf2_per_objfile->line.asection = sectp;
1387 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1388 }
251d32d9 1389 else if (section_is_p (sectp->name, &names->loc))
c906108c 1390 {
dce234bc
PP
1391 dwarf2_per_objfile->loc.asection = sectp;
1392 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1393 }
251d32d9 1394 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1395 {
dce234bc
PP
1396 dwarf2_per_objfile->macinfo.asection = sectp;
1397 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1398 }
cf2c3c16
TT
1399 else if (section_is_p (sectp->name, &names->macro))
1400 {
1401 dwarf2_per_objfile->macro.asection = sectp;
1402 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1403 }
251d32d9 1404 else if (section_is_p (sectp->name, &names->str))
c906108c 1405 {
dce234bc
PP
1406 dwarf2_per_objfile->str.asection = sectp;
1407 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1408 }
251d32d9 1409 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1410 {
dce234bc
PP
1411 dwarf2_per_objfile->frame.asection = sectp;
1412 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1413 }
251d32d9 1414 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1415 {
3799ccc6 1416 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1417
3799ccc6
EZ
1418 if (aflag & SEC_HAS_CONTENTS)
1419 {
dce234bc
PP
1420 dwarf2_per_objfile->eh_frame.asection = sectp;
1421 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1422 }
b6af0555 1423 }
251d32d9 1424 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1425 {
dce234bc
PP
1426 dwarf2_per_objfile->ranges.asection = sectp;
1427 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1428 }
251d32d9 1429 else if (section_is_p (sectp->name, &names->types))
348e048f 1430 {
8b70b953
TT
1431 struct dwarf2_section_info type_section;
1432
1433 memset (&type_section, 0, sizeof (type_section));
1434 type_section.asection = sectp;
1435 type_section.size = bfd_get_section_size (sectp);
1436
1437 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1438 &type_section);
348e048f 1439 }
251d32d9 1440 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1441 {
1442 dwarf2_per_objfile->gdb_index.asection = sectp;
1443 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1444 }
dce234bc 1445
72dca2f5
FR
1446 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1447 && bfd_section_vma (abfd, sectp) == 0)
1448 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1449}
1450
dce234bc
PP
1451/* Decompress a section that was compressed using zlib. Store the
1452 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1453
1454static void
dce234bc
PP
1455zlib_decompress_section (struct objfile *objfile, asection *sectp,
1456 gdb_byte **outbuf, bfd_size_type *outsize)
1457{
1458 bfd *abfd = objfile->obfd;
1459#ifndef HAVE_ZLIB_H
1460 error (_("Support for zlib-compressed DWARF data (from '%s') "
1461 "is disabled in this copy of GDB"),
1462 bfd_get_filename (abfd));
1463#else
1464 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1465 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1466 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1467 bfd_size_type uncompressed_size;
1468 gdb_byte *uncompressed_buffer;
1469 z_stream strm;
1470 int rc;
1471 int header_size = 12;
1472
1473 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1474 || bfd_bread (compressed_buffer,
1475 compressed_size, abfd) != compressed_size)
dce234bc
PP
1476 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1477 bfd_get_filename (abfd));
1478
1479 /* Read the zlib header. In this case, it should be "ZLIB" followed
1480 by the uncompressed section size, 8 bytes in big-endian order. */
1481 if (compressed_size < header_size
1482 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1483 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1484 bfd_get_filename (abfd));
1485 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[11];
1493
1494 /* It is possible the section consists of several compressed
1495 buffers concatenated together, so we uncompress in a loop. */
1496 strm.zalloc = NULL;
1497 strm.zfree = NULL;
1498 strm.opaque = NULL;
1499 strm.avail_in = compressed_size - header_size;
1500 strm.next_in = (Bytef*) compressed_buffer + header_size;
1501 strm.avail_out = uncompressed_size;
1502 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1503 uncompressed_size);
1504 rc = inflateInit (&strm);
1505 while (strm.avail_in > 0)
1506 {
1507 if (rc != Z_OK)
1508 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1509 bfd_get_filename (abfd), rc);
1510 strm.next_out = ((Bytef*) uncompressed_buffer
1511 + (uncompressed_size - strm.avail_out));
1512 rc = inflate (&strm, Z_FINISH);
1513 if (rc != Z_STREAM_END)
1514 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1515 bfd_get_filename (abfd), rc);
1516 rc = inflateReset (&strm);
1517 }
1518 rc = inflateEnd (&strm);
1519 if (rc != Z_OK
1520 || strm.avail_out != 0)
1521 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1522 bfd_get_filename (abfd), rc);
1523
affddf13 1524 do_cleanups (cleanup);
dce234bc
PP
1525 *outbuf = uncompressed_buffer;
1526 *outsize = uncompressed_size;
1527#endif
233a11ab
CS
1528}
1529
9e0ac564
TT
1530/* A helper function that decides whether a section is empty. */
1531
1532static int
1533dwarf2_section_empty_p (struct dwarf2_section_info *info)
1534{
1535 return info->asection == NULL || info->size == 0;
1536}
1537
9cdd5dbd 1538/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1539 OBJFILE, store info about the section into INFO.
1540 If the section is compressed, uncompress it before returning. */
c906108c 1541
dce234bc
PP
1542static void
1543dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1544{
dce234bc
PP
1545 bfd *abfd = objfile->obfd;
1546 asection *sectp = info->asection;
1547 gdb_byte *buf, *retbuf;
1548 unsigned char header[4];
c906108c 1549
be391dca
TT
1550 if (info->readin)
1551 return;
dce234bc 1552 info->buffer = NULL;
b315ab21 1553 info->map_addr = NULL;
be391dca 1554 info->readin = 1;
188dd5d6 1555
9e0ac564 1556 if (dwarf2_section_empty_p (info))
dce234bc 1557 return;
c906108c 1558
dce234bc
PP
1559 /* Check if the file has a 4-byte header indicating compression. */
1560 if (info->size > sizeof (header)
1561 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1562 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1563 {
1564 /* Upon decompression, update the buffer and its size. */
1565 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1566 {
1567 zlib_decompress_section (objfile, sectp, &info->buffer,
1568 &info->size);
1569 return;
1570 }
1571 }
4bdf3d34 1572
dce234bc
PP
1573#ifdef HAVE_MMAP
1574 if (pagesize == 0)
1575 pagesize = getpagesize ();
2e276125 1576
dce234bc
PP
1577 /* Only try to mmap sections which are large enough: we don't want to
1578 waste space due to fragmentation. Also, only try mmap for sections
1579 without relocations. */
1580
1581 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1582 {
b315ab21
TG
1583 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1584 MAP_PRIVATE, sectp->filepos,
1585 &info->map_addr, &info->map_len);
dce234bc 1586
b315ab21 1587 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1588 {
be391dca 1589#if HAVE_POSIX_MADVISE
b315ab21 1590 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1591#endif
dce234bc
PP
1592 return;
1593 }
1594 }
1595#endif
1596
1597 /* If we get here, we are a normal, not-compressed section. */
1598 info->buffer = buf
1599 = obstack_alloc (&objfile->objfile_obstack, info->size);
1600
1601 /* When debugging .o files, we may need to apply relocations; see
1602 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1603 We never compress sections in .o files, so we only need to
1604 try this when the section is not compressed. */
ac8035ab 1605 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1606 if (retbuf != NULL)
1607 {
1608 info->buffer = retbuf;
1609 return;
1610 }
1611
1612 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1613 || bfd_bread (buf, info->size, abfd) != info->size)
1614 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1615 bfd_get_filename (abfd));
1616}
1617
9e0ac564
TT
1618/* A helper function that returns the size of a section in a safe way.
1619 If you are positive that the section has been read before using the
1620 size, then it is safe to refer to the dwarf2_section_info object's
1621 "size" field directly. In other cases, you must call this
1622 function, because for compressed sections the size field is not set
1623 correctly until the section has been read. */
1624
1625static bfd_size_type
1626dwarf2_section_size (struct objfile *objfile,
1627 struct dwarf2_section_info *info)
1628{
1629 if (!info->readin)
1630 dwarf2_read_section (objfile, info);
1631 return info->size;
1632}
1633
dce234bc 1634/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1635 SECTION_NAME. */
af34e669 1636
dce234bc 1637void
3017a003
TG
1638dwarf2_get_section_info (struct objfile *objfile,
1639 enum dwarf2_section_enum sect,
dce234bc
PP
1640 asection **sectp, gdb_byte **bufp,
1641 bfd_size_type *sizep)
1642{
1643 struct dwarf2_per_objfile *data
1644 = objfile_data (objfile, dwarf2_objfile_data_key);
1645 struct dwarf2_section_info *info;
a3b2a86b
TT
1646
1647 /* We may see an objfile without any DWARF, in which case we just
1648 return nothing. */
1649 if (data == NULL)
1650 {
1651 *sectp = NULL;
1652 *bufp = NULL;
1653 *sizep = 0;
1654 return;
1655 }
3017a003
TG
1656 switch (sect)
1657 {
1658 case DWARF2_DEBUG_FRAME:
1659 info = &data->frame;
1660 break;
1661 case DWARF2_EH_FRAME:
1662 info = &data->eh_frame;
1663 break;
1664 default:
1665 gdb_assert_not_reached ("unexpected section");
1666 }
dce234bc 1667
9e0ac564 1668 dwarf2_read_section (objfile, info);
dce234bc
PP
1669
1670 *sectp = info->asection;
1671 *bufp = info->buffer;
1672 *sizep = info->size;
1673}
1674
9291a0cd 1675\f
7b9f3c50
DE
1676/* DWARF quick_symbols_functions support. */
1677
1678/* TUs can share .debug_line entries, and there can be a lot more TUs than
1679 unique line tables, so we maintain a separate table of all .debug_line
1680 derived entries to support the sharing.
1681 All the quick functions need is the list of file names. We discard the
1682 line_header when we're done and don't need to record it here. */
1683struct quick_file_names
1684{
1685 /* The offset in .debug_line of the line table. We hash on this. */
1686 unsigned int offset;
1687
1688 /* The number of entries in file_names, real_names. */
1689 unsigned int num_file_names;
1690
1691 /* The file names from the line table, after being run through
1692 file_full_name. */
1693 const char **file_names;
1694
1695 /* The file names from the line table after being run through
1696 gdb_realpath. These are computed lazily. */
1697 const char **real_names;
1698};
1699
1700/* When using the index (and thus not using psymtabs), each CU has an
1701 object of this type. This is used to hold information needed by
1702 the various "quick" methods. */
1703struct dwarf2_per_cu_quick_data
1704{
1705 /* The file table. This can be NULL if there was no file table
1706 or it's currently not read in.
1707 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1708 struct quick_file_names *file_names;
1709
1710 /* The corresponding symbol table. This is NULL if symbols for this
1711 CU have not yet been read. */
1712 struct symtab *symtab;
1713
1714 /* A temporary mark bit used when iterating over all CUs in
1715 expand_symtabs_matching. */
1716 unsigned int mark : 1;
1717
1718 /* True if we've tried to read the file table and found there isn't one.
1719 There will be no point in trying to read it again next time. */
1720 unsigned int no_file_data : 1;
1721};
1722
1723/* Hash function for a quick_file_names. */
1724
1725static hashval_t
1726hash_file_name_entry (const void *e)
1727{
1728 const struct quick_file_names *file_data = e;
1729
1730 return file_data->offset;
1731}
1732
1733/* Equality function for a quick_file_names. */
1734
1735static int
1736eq_file_name_entry (const void *a, const void *b)
1737{
1738 const struct quick_file_names *ea = a;
1739 const struct quick_file_names *eb = b;
1740
1741 return ea->offset == eb->offset;
1742}
1743
1744/* Delete function for a quick_file_names. */
1745
1746static void
1747delete_file_name_entry (void *e)
1748{
1749 struct quick_file_names *file_data = e;
1750 int i;
1751
1752 for (i = 0; i < file_data->num_file_names; ++i)
1753 {
1754 xfree ((void*) file_data->file_names[i]);
1755 if (file_data->real_names)
1756 xfree ((void*) file_data->real_names[i]);
1757 }
1758
1759 /* The space for the struct itself lives on objfile_obstack,
1760 so we don't free it here. */
1761}
1762
1763/* Create a quick_file_names hash table. */
1764
1765static htab_t
1766create_quick_file_names_table (unsigned int nr_initial_entries)
1767{
1768 return htab_create_alloc (nr_initial_entries,
1769 hash_file_name_entry, eq_file_name_entry,
1770 delete_file_name_entry, xcalloc, xfree);
1771}
9291a0cd 1772
918dd910
JK
1773/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1774 have to be created afterwards. You should call age_cached_comp_units after
1775 processing PER_CU->CU. dw2_setup must have been already called. */
1776
1777static void
1778load_cu (struct dwarf2_per_cu_data *per_cu)
1779{
b0df02fd 1780 if (per_cu->debug_types_section)
e5fe5e75 1781 load_full_type_unit (per_cu);
918dd910 1782 else
a0f42c21 1783 load_full_comp_unit (per_cu);
918dd910 1784
918dd910 1785 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1786
1787 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1788}
1789
a0f42c21 1790/* Read in the symbols for PER_CU. */
2fdf6df6 1791
9291a0cd 1792static void
a0f42c21 1793dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1794{
1795 struct cleanup *back_to;
1796
1797 back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
a0f42c21 1799 queue_comp_unit (per_cu);
9291a0cd 1800
918dd910 1801 load_cu (per_cu);
9291a0cd 1802
a0f42c21 1803 process_queue ();
9291a0cd
TT
1804
1805 /* Age the cache, releasing compilation units that have not
1806 been used recently. */
1807 age_cached_comp_units ();
1808
1809 do_cleanups (back_to);
1810}
1811
1812/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1813 the objfile from which this CU came. Returns the resulting symbol
1814 table. */
2fdf6df6 1815
9291a0cd 1816static struct symtab *
a0f42c21 1817dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1818{
1819 if (!per_cu->v.quick->symtab)
1820 {
1821 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1822 increment_reading_symtab ();
a0f42c21 1823 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1824 do_cleanups (back_to);
1825 }
1826 return per_cu->v.quick->symtab;
1827}
1828
1fd400ff 1829/* Return the CU given its index. */
2fdf6df6 1830
1fd400ff
TT
1831static struct dwarf2_per_cu_data *
1832dw2_get_cu (int index)
1833{
1834 if (index >= dwarf2_per_objfile->n_comp_units)
1835 {
1836 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1837 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1838 }
1839 return dwarf2_per_objfile->all_comp_units[index];
1840}
1841
9291a0cd
TT
1842/* A helper function that knows how to read a 64-bit value in a way
1843 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1844 otherwise. */
2fdf6df6 1845
9291a0cd
TT
1846static int
1847extract_cu_value (const char *bytes, ULONGEST *result)
1848{
1849 if (sizeof (ULONGEST) < 8)
1850 {
1851 int i;
1852
1853 /* Ignore the upper 4 bytes if they are all zero. */
1854 for (i = 0; i < 4; ++i)
1855 if (bytes[i + 4] != 0)
1856 return 0;
1857
1858 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1859 }
1860 else
1861 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1862 return 1;
1863}
1864
1865/* Read the CU list from the mapped index, and use it to create all
1866 the CU objects for this objfile. Return 0 if something went wrong,
1867 1 if everything went ok. */
2fdf6df6 1868
9291a0cd 1869static int
1fd400ff
TT
1870create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1871 offset_type cu_list_elements)
9291a0cd
TT
1872{
1873 offset_type i;
9291a0cd
TT
1874
1875 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1876 dwarf2_per_objfile->all_comp_units
1877 = obstack_alloc (&objfile->objfile_obstack,
1878 dwarf2_per_objfile->n_comp_units
1879 * sizeof (struct dwarf2_per_cu_data *));
1880
1881 for (i = 0; i < cu_list_elements; i += 2)
1882 {
1883 struct dwarf2_per_cu_data *the_cu;
1884 ULONGEST offset, length;
1885
1886 if (!extract_cu_value (cu_list, &offset)
1887 || !extract_cu_value (cu_list + 8, &length))
1888 return 0;
1889 cu_list += 2 * 8;
1890
1891 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1892 struct dwarf2_per_cu_data);
1893 the_cu->offset = offset;
1894 the_cu->length = length;
1895 the_cu->objfile = objfile;
1896 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1897 struct dwarf2_per_cu_quick_data);
1898 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1899 }
1900
1901 return 1;
1902}
1903
1fd400ff 1904/* Create the signatured type hash table from the index. */
673bfd45 1905
1fd400ff 1906static int
673bfd45 1907create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1908 struct dwarf2_section_info *section,
673bfd45
DE
1909 const gdb_byte *bytes,
1910 offset_type elements)
1fd400ff
TT
1911{
1912 offset_type i;
673bfd45 1913 htab_t sig_types_hash;
1fd400ff 1914
d467dd73
DE
1915 dwarf2_per_objfile->n_type_units = elements / 3;
1916 dwarf2_per_objfile->all_type_units
1fd400ff 1917 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1918 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1919 * sizeof (struct dwarf2_per_cu_data *));
1920
673bfd45 1921 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1922
1923 for (i = 0; i < elements; i += 3)
1924 {
1925 struct signatured_type *type_sig;
1926 ULONGEST offset, type_offset, signature;
1927 void **slot;
1928
1929 if (!extract_cu_value (bytes, &offset)
1930 || !extract_cu_value (bytes + 8, &type_offset))
1931 return 0;
1932 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1933 bytes += 3 * 8;
1934
1935 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1936 struct signatured_type);
1937 type_sig->signature = signature;
1fd400ff 1938 type_sig->type_offset = type_offset;
b0df02fd 1939 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1940 type_sig->per_cu.offset = offset;
1941 type_sig->per_cu.objfile = objfile;
1942 type_sig->per_cu.v.quick
1943 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1944 struct dwarf2_per_cu_quick_data);
1945
673bfd45 1946 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1947 *slot = type_sig;
1948
d467dd73 1949 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1fd400ff
TT
1950 }
1951
673bfd45 1952 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1953
1954 return 1;
1955}
1956
9291a0cd
TT
1957/* Read the address map data from the mapped index, and use it to
1958 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1959
9291a0cd
TT
1960static void
1961create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1962{
1963 const gdb_byte *iter, *end;
1964 struct obstack temp_obstack;
1965 struct addrmap *mutable_map;
1966 struct cleanup *cleanup;
1967 CORE_ADDR baseaddr;
1968
1969 obstack_init (&temp_obstack);
1970 cleanup = make_cleanup_obstack_free (&temp_obstack);
1971 mutable_map = addrmap_create_mutable (&temp_obstack);
1972
1973 iter = index->address_table;
1974 end = iter + index->address_table_size;
1975
1976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1977
1978 while (iter < end)
1979 {
1980 ULONGEST hi, lo, cu_index;
1981 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1982 iter += 8;
1983 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984 iter += 8;
1985 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1986 iter += 4;
1987
1988 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1989 dw2_get_cu (cu_index));
9291a0cd
TT
1990 }
1991
1992 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1993 &objfile->objfile_obstack);
1994 do_cleanups (cleanup);
1995}
1996
59d7bcaf
JK
1997/* The hash function for strings in the mapped index. This is the same as
1998 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1999 implementation. This is necessary because the hash function is tied to the
2000 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2001 SYMBOL_HASH_NEXT.
2002
2003 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2004
9291a0cd 2005static hashval_t
559a7a62 2006mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2007{
2008 const unsigned char *str = (const unsigned char *) p;
2009 hashval_t r = 0;
2010 unsigned char c;
2011
2012 while ((c = *str++) != 0)
559a7a62
JK
2013 {
2014 if (index_version >= 5)
2015 c = tolower (c);
2016 r = r * 67 + c - 113;
2017 }
9291a0cd
TT
2018
2019 return r;
2020}
2021
2022/* Find a slot in the mapped index INDEX for the object named NAME.
2023 If NAME is found, set *VEC_OUT to point to the CU vector in the
2024 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2025
9291a0cd
TT
2026static int
2027find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2028 offset_type **vec_out)
2029{
0cf03b49
JK
2030 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2031 offset_type hash;
9291a0cd 2032 offset_type slot, step;
559a7a62 2033 int (*cmp) (const char *, const char *);
9291a0cd 2034
0cf03b49
JK
2035 if (current_language->la_language == language_cplus
2036 || current_language->la_language == language_java
2037 || current_language->la_language == language_fortran)
2038 {
2039 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2040 not contain any. */
2041 const char *paren = strchr (name, '(');
2042
2043 if (paren)
2044 {
2045 char *dup;
2046
2047 dup = xmalloc (paren - name + 1);
2048 memcpy (dup, name, paren - name);
2049 dup[paren - name] = 0;
2050
2051 make_cleanup (xfree, dup);
2052 name = dup;
2053 }
2054 }
2055
559a7a62
JK
2056 /* Index version 4 did not support case insensitive searches. But the
2057 indexes for case insensitive languages are built in lowercase, therefore
2058 simulate our NAME being searched is also lowercased. */
2059 hash = mapped_index_string_hash ((index->version == 4
2060 && case_sensitivity == case_sensitive_off
2061 ? 5 : index->version),
2062 name);
2063
3876f04e
DE
2064 slot = hash & (index->symbol_table_slots - 1);
2065 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2066 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2067
2068 for (;;)
2069 {
2070 /* Convert a slot number to an offset into the table. */
2071 offset_type i = 2 * slot;
2072 const char *str;
3876f04e 2073 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2074 {
2075 do_cleanups (back_to);
2076 return 0;
2077 }
9291a0cd 2078
3876f04e 2079 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2080 if (!cmp (name, str))
9291a0cd
TT
2081 {
2082 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2083 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2084 do_cleanups (back_to);
9291a0cd
TT
2085 return 1;
2086 }
2087
3876f04e 2088 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2089 }
2090}
2091
2092/* Read the index file. If everything went ok, initialize the "quick"
2093 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2094
9291a0cd
TT
2095static int
2096dwarf2_read_index (struct objfile *objfile)
2097{
9291a0cd
TT
2098 char *addr;
2099 struct mapped_index *map;
b3b272e1 2100 offset_type *metadata;
ac0b195c
KW
2101 const gdb_byte *cu_list;
2102 const gdb_byte *types_list = NULL;
2103 offset_type version, cu_list_elements;
2104 offset_type types_list_elements = 0;
1fd400ff 2105 int i;
9291a0cd 2106
9e0ac564 2107 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2108 return 0;
82430852
JK
2109
2110 /* Older elfutils strip versions could keep the section in the main
2111 executable while splitting it for the separate debug info file. */
2112 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2113 & SEC_HAS_CONTENTS) == 0)
2114 return 0;
2115
9291a0cd
TT
2116 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2117
2118 addr = dwarf2_per_objfile->gdb_index.buffer;
2119 /* Version check. */
1fd400ff 2120 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2121 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2122 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2123 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2124 indices. Index version 4 uses a different hash function than index
2125 version 5 and later. */
831adc1f 2126 if (version < 4)
9291a0cd 2127 return 0;
594e8718
JK
2128 /* Indexes with higher version than the one supported by GDB may be no
2129 longer backward compatible. */
559a7a62 2130 if (version > 5)
594e8718 2131 return 0;
9291a0cd
TT
2132
2133 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2134 map->version = version;
b3b272e1 2135 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2136
2137 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2138
2139 i = 0;
2140 cu_list = addr + MAYBE_SWAP (metadata[i]);
2141 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2142 / 8);
1fd400ff
TT
2143 ++i;
2144
987d643c
TT
2145 types_list = addr + MAYBE_SWAP (metadata[i]);
2146 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2147 - MAYBE_SWAP (metadata[i]))
2148 / 8);
2149 ++i;
1fd400ff
TT
2150
2151 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2152 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2153 - MAYBE_SWAP (metadata[i]));
2154 ++i;
2155
3876f04e
DE
2156 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2157 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2158 - MAYBE_SWAP (metadata[i]))
2159 / (2 * sizeof (offset_type)));
1fd400ff 2160 ++i;
9291a0cd 2161
1fd400ff
TT
2162 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2163
2164 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2165 return 0;
2166
8b70b953
TT
2167 if (types_list_elements)
2168 {
2169 struct dwarf2_section_info *section;
2170
2171 /* We can only handle a single .debug_types when we have an
2172 index. */
2173 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2174 return 0;
2175
2176 section = VEC_index (dwarf2_section_info_def,
2177 dwarf2_per_objfile->types, 0);
2178
2179 if (!create_signatured_type_table_from_index (objfile, section,
2180 types_list,
2181 types_list_elements))
2182 return 0;
2183 }
9291a0cd
TT
2184
2185 create_addrmap_from_index (objfile, map);
2186
2187 dwarf2_per_objfile->index_table = map;
2188 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2189 dwarf2_per_objfile->quick_file_names_table =
2190 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2191
2192 return 1;
2193}
2194
2195/* A helper for the "quick" functions which sets the global
2196 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2197
9291a0cd
TT
2198static void
2199dw2_setup (struct objfile *objfile)
2200{
2201 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2202 gdb_assert (dwarf2_per_objfile);
2203}
2204
2205/* A helper for the "quick" functions which attempts to read the line
2206 table for THIS_CU. */
2fdf6df6 2207
7b9f3c50
DE
2208static struct quick_file_names *
2209dw2_get_file_names (struct objfile *objfile,
2210 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2211{
2212 bfd *abfd = objfile->obfd;
7b9f3c50 2213 struct line_header *lh;
9291a0cd
TT
2214 struct attribute *attr;
2215 struct cleanup *cleanups;
2216 struct die_info *comp_unit_die;
36374493 2217 struct dwarf2_section_info* sec;
9ff913ba 2218 gdb_byte *info_ptr;
9291a0cd
TT
2219 int has_children, i;
2220 struct dwarf2_cu cu;
9ff913ba 2221 unsigned int bytes_read;
9291a0cd
TT
2222 struct die_reader_specs reader_specs;
2223 char *name, *comp_dir;
7b9f3c50
DE
2224 void **slot;
2225 struct quick_file_names *qfn;
2226 unsigned int line_offset;
9291a0cd 2227
7b9f3c50
DE
2228 if (this_cu->v.quick->file_names != NULL)
2229 return this_cu->v.quick->file_names;
2230 /* If we know there is no line data, no point in looking again. */
2231 if (this_cu->v.quick->no_file_data)
2232 return NULL;
9291a0cd 2233
23745b47 2234 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2235 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2236
b0df02fd
DE
2237 if (this_cu->debug_types_section)
2238 sec = this_cu->debug_types_section;
36374493
DE
2239 else
2240 sec = &dwarf2_per_objfile->info;
2241 dwarf2_read_section (objfile, sec);
9ff913ba 2242 info_ptr = sec->buffer + this_cu->offset;
9291a0cd 2243
9ff913ba
DE
2244 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2245 this_cu->debug_types_section != NULL);
9291a0cd 2246
6caca83c 2247 /* Skip dummy compilation units. */
9ff913ba 2248 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2249 || peek_abbrev_code (abfd, info_ptr) == 0)
2250 {
2251 do_cleanups (cleanups);
2252 return NULL;
2253 }
2254
e5fe5e75 2255 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2256 make_cleanup (dwarf2_free_abbrev_table, &cu);
2257
9291a0cd 2258 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2259 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2260 &has_children);
9291a0cd 2261
7b9f3c50
DE
2262 lh = NULL;
2263 slot = NULL;
2264 line_offset = 0;
9291a0cd
TT
2265 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2266 if (attr)
2267 {
7b9f3c50
DE
2268 struct quick_file_names find_entry;
2269
2270 line_offset = DW_UNSND (attr);
2271
2272 /* We may have already read in this line header (TU line header sharing).
2273 If we have we're done. */
2274 find_entry.offset = line_offset;
2275 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2276 &find_entry, INSERT);
2277 if (*slot != NULL)
2278 {
2279 do_cleanups (cleanups);
2280 this_cu->v.quick->file_names = *slot;
2281 return *slot;
2282 }
2283
9291a0cd
TT
2284 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2285 }
2286 if (lh == NULL)
2287 {
2288 do_cleanups (cleanups);
7b9f3c50
DE
2289 this_cu->v.quick->no_file_data = 1;
2290 return NULL;
9291a0cd
TT
2291 }
2292
7b9f3c50
DE
2293 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2294 qfn->offset = line_offset;
2295 gdb_assert (slot != NULL);
2296 *slot = qfn;
9291a0cd 2297
7b9f3c50 2298 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2299
7b9f3c50
DE
2300 qfn->num_file_names = lh->num_file_names;
2301 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2302 lh->num_file_names * sizeof (char *));
9291a0cd 2303 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2304 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2305 qfn->real_names = NULL;
9291a0cd 2306
7b9f3c50 2307 free_line_header (lh);
9291a0cd 2308 do_cleanups (cleanups);
7b9f3c50
DE
2309
2310 this_cu->v.quick->file_names = qfn;
2311 return qfn;
9291a0cd
TT
2312}
2313
2314/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2315 real path for a given file name from the line table. */
2fdf6df6 2316
9291a0cd 2317static const char *
7b9f3c50
DE
2318dw2_get_real_path (struct objfile *objfile,
2319 struct quick_file_names *qfn, int index)
9291a0cd 2320{
7b9f3c50
DE
2321 if (qfn->real_names == NULL)
2322 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2323 qfn->num_file_names, sizeof (char *));
9291a0cd 2324
7b9f3c50
DE
2325 if (qfn->real_names[index] == NULL)
2326 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2327
7b9f3c50 2328 return qfn->real_names[index];
9291a0cd
TT
2329}
2330
2331static struct symtab *
2332dw2_find_last_source_symtab (struct objfile *objfile)
2333{
2334 int index;
ae2de4f8 2335
9291a0cd
TT
2336 dw2_setup (objfile);
2337 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2338 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2339}
2340
7b9f3c50
DE
2341/* Traversal function for dw2_forget_cached_source_info. */
2342
2343static int
2344dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2345{
7b9f3c50 2346 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2347
7b9f3c50 2348 if (file_data->real_names)
9291a0cd 2349 {
7b9f3c50 2350 int i;
9291a0cd 2351
7b9f3c50 2352 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2353 {
7b9f3c50
DE
2354 xfree ((void*) file_data->real_names[i]);
2355 file_data->real_names[i] = NULL;
9291a0cd
TT
2356 }
2357 }
7b9f3c50
DE
2358
2359 return 1;
2360}
2361
2362static void
2363dw2_forget_cached_source_info (struct objfile *objfile)
2364{
2365 dw2_setup (objfile);
2366
2367 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2368 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2369}
2370
f8eba3c6
TT
2371/* Helper function for dw2_map_symtabs_matching_filename that expands
2372 the symtabs and calls the iterator. */
2373
2374static int
2375dw2_map_expand_apply (struct objfile *objfile,
2376 struct dwarf2_per_cu_data *per_cu,
2377 const char *name,
2378 const char *full_path, const char *real_path,
2379 int (*callback) (struct symtab *, void *),
2380 void *data)
2381{
2382 struct symtab *last_made = objfile->symtabs;
2383
2384 /* Don't visit already-expanded CUs. */
2385 if (per_cu->v.quick->symtab)
2386 return 0;
2387
2388 /* This may expand more than one symtab, and we want to iterate over
2389 all of them. */
a0f42c21 2390 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2391
2392 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2393 objfile->symtabs, last_made);
2394}
2395
2396/* Implementation of the map_symtabs_matching_filename method. */
2397
9291a0cd 2398static int
f8eba3c6
TT
2399dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2400 const char *full_path, const char *real_path,
2401 int (*callback) (struct symtab *, void *),
2402 void *data)
9291a0cd
TT
2403{
2404 int i;
c011a4f4 2405 const char *name_basename = lbasename (name);
4aac40c8
TT
2406 int name_len = strlen (name);
2407 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2408
2409 dw2_setup (objfile);
ae2de4f8 2410
1fd400ff 2411 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2412 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2413 {
2414 int j;
e254ef6a 2415 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2416 struct quick_file_names *file_data;
9291a0cd 2417
3d7bb9d9 2418 /* We only need to look at symtabs not already expanded. */
e254ef6a 2419 if (per_cu->v.quick->symtab)
9291a0cd
TT
2420 continue;
2421
7b9f3c50
DE
2422 file_data = dw2_get_file_names (objfile, per_cu);
2423 if (file_data == NULL)
9291a0cd
TT
2424 continue;
2425
7b9f3c50 2426 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2427 {
7b9f3c50 2428 const char *this_name = file_data->file_names[j];
9291a0cd 2429
4aac40c8
TT
2430 if (FILENAME_CMP (name, this_name) == 0
2431 || (!is_abs && compare_filenames_for_search (this_name,
2432 name, name_len)))
9291a0cd 2433 {
f8eba3c6
TT
2434 if (dw2_map_expand_apply (objfile, per_cu,
2435 name, full_path, real_path,
2436 callback, data))
2437 return 1;
4aac40c8 2438 }
9291a0cd 2439
c011a4f4
DE
2440 /* Before we invoke realpath, which can get expensive when many
2441 files are involved, do a quick comparison of the basenames. */
2442 if (! basenames_may_differ
2443 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2444 continue;
2445
9291a0cd
TT
2446 if (full_path != NULL)
2447 {
7b9f3c50
DE
2448 const char *this_real_name = dw2_get_real_path (objfile,
2449 file_data, j);
9291a0cd 2450
7b9f3c50 2451 if (this_real_name != NULL
4aac40c8
TT
2452 && (FILENAME_CMP (full_path, this_real_name) == 0
2453 || (!is_abs
2454 && compare_filenames_for_search (this_real_name,
2455 name, name_len))))
9291a0cd 2456 {
f8eba3c6
TT
2457 if (dw2_map_expand_apply (objfile, per_cu,
2458 name, full_path, real_path,
2459 callback, data))
2460 return 1;
9291a0cd
TT
2461 }
2462 }
2463
2464 if (real_path != NULL)
2465 {
7b9f3c50
DE
2466 const char *this_real_name = dw2_get_real_path (objfile,
2467 file_data, j);
9291a0cd 2468
7b9f3c50 2469 if (this_real_name != NULL
4aac40c8
TT
2470 && (FILENAME_CMP (real_path, this_real_name) == 0
2471 || (!is_abs
2472 && compare_filenames_for_search (this_real_name,
2473 name, name_len))))
9291a0cd 2474 {
f8eba3c6
TT
2475 if (dw2_map_expand_apply (objfile, per_cu,
2476 name, full_path, real_path,
2477 callback, data))
2478 return 1;
9291a0cd
TT
2479 }
2480 }
2481 }
2482 }
2483
9291a0cd
TT
2484 return 0;
2485}
2486
2487static struct symtab *
2488dw2_lookup_symbol (struct objfile *objfile, int block_index,
2489 const char *name, domain_enum domain)
2490{
774b6a14 2491 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2492 instead. */
2493 return NULL;
2494}
2495
2496/* A helper function that expands all symtabs that hold an object
2497 named NAME. */
2fdf6df6 2498
9291a0cd
TT
2499static void
2500dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2501{
2502 dw2_setup (objfile);
2503
ae2de4f8 2504 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2505 if (dwarf2_per_objfile->index_table)
2506 {
2507 offset_type *vec;
2508
2509 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2510 name, &vec))
2511 {
2512 offset_type i, len = MAYBE_SWAP (*vec);
2513 for (i = 0; i < len; ++i)
2514 {
2515 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2516 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2517
a0f42c21 2518 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2519 }
2520 }
2521 }
2522}
2523
774b6a14
TT
2524static void
2525dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2526 enum block_enum block_kind, const char *name,
774b6a14 2527 domain_enum domain)
9291a0cd 2528{
774b6a14 2529 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2530}
2531
2532static void
2533dw2_print_stats (struct objfile *objfile)
2534{
2535 int i, count;
2536
2537 dw2_setup (objfile);
2538 count = 0;
1fd400ff 2539 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2540 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2541 {
e254ef6a 2542 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2543
e254ef6a 2544 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2545 ++count;
2546 }
2547 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2548}
2549
2550static void
2551dw2_dump (struct objfile *objfile)
2552{
2553 /* Nothing worth printing. */
2554}
2555
2556static void
2557dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2558 struct section_offsets *delta)
2559{
2560 /* There's nothing to relocate here. */
2561}
2562
2563static void
2564dw2_expand_symtabs_for_function (struct objfile *objfile,
2565 const char *func_name)
2566{
2567 dw2_do_expand_symtabs_matching (objfile, func_name);
2568}
2569
2570static void
2571dw2_expand_all_symtabs (struct objfile *objfile)
2572{
2573 int i;
2574
2575 dw2_setup (objfile);
1fd400ff
TT
2576
2577 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2578 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2579 {
e254ef6a 2580 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2581
a0f42c21 2582 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2583 }
2584}
2585
2586static void
2587dw2_expand_symtabs_with_filename (struct objfile *objfile,
2588 const char *filename)
2589{
2590 int i;
2591
2592 dw2_setup (objfile);
d4637a04
DE
2593
2594 /* We don't need to consider type units here.
2595 This is only called for examining code, e.g. expand_line_sal.
2596 There can be an order of magnitude (or more) more type units
2597 than comp units, and we avoid them if we can. */
2598
2599 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2600 {
2601 int j;
e254ef6a 2602 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2603 struct quick_file_names *file_data;
9291a0cd 2604
3d7bb9d9 2605 /* We only need to look at symtabs not already expanded. */
e254ef6a 2606 if (per_cu->v.quick->symtab)
9291a0cd
TT
2607 continue;
2608
7b9f3c50
DE
2609 file_data = dw2_get_file_names (objfile, per_cu);
2610 if (file_data == NULL)
9291a0cd
TT
2611 continue;
2612
7b9f3c50 2613 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2614 {
7b9f3c50 2615 const char *this_name = file_data->file_names[j];
1ef75ecc 2616 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2617 {
a0f42c21 2618 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2619 break;
2620 }
2621 }
2622 }
2623}
2624
dd786858 2625static const char *
9291a0cd
TT
2626dw2_find_symbol_file (struct objfile *objfile, const char *name)
2627{
e254ef6a 2628 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2629 offset_type *vec;
7b9f3c50 2630 struct quick_file_names *file_data;
9291a0cd
TT
2631
2632 dw2_setup (objfile);
2633
ae2de4f8 2634 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2635 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2636 {
2637 struct symtab *s;
2638
2639 ALL_OBJFILE_SYMTABS (objfile, s)
2640 if (s->primary)
2641 {
2642 struct blockvector *bv = BLOCKVECTOR (s);
2643 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2644 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2645
2646 if (sym)
2647 return sym->symtab->filename;
2648 }
2649 return NULL;
2650 }
9291a0cd
TT
2651
2652 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2653 name, &vec))
2654 return NULL;
2655
2656 /* Note that this just looks at the very first one named NAME -- but
2657 actually we are looking for a function. find_main_filename
2658 should be rewritten so that it doesn't require a custom hook. It
2659 could just use the ordinary symbol tables. */
2660 /* vec[0] is the length, which must always be >0. */
e254ef6a 2661 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2662
7b9f3c50
DE
2663 file_data = dw2_get_file_names (objfile, per_cu);
2664 if (file_data == NULL)
9291a0cd
TT
2665 return NULL;
2666
7b9f3c50 2667 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2668}
2669
2670static void
40658b94
PH
2671dw2_map_matching_symbols (const char * name, domain_enum namespace,
2672 struct objfile *objfile, int global,
2673 int (*callback) (struct block *,
2674 struct symbol *, void *),
2edb89d3
JK
2675 void *data, symbol_compare_ftype *match,
2676 symbol_compare_ftype *ordered_compare)
9291a0cd 2677{
40658b94 2678 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2679 current language is Ada for a non-Ada objfile using GNU index. As Ada
2680 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2681}
2682
2683static void
f8eba3c6
TT
2684dw2_expand_symtabs_matching
2685 (struct objfile *objfile,
2686 int (*file_matcher) (const char *, void *),
e078317b 2687 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2688 enum search_domain kind,
2689 void *data)
9291a0cd
TT
2690{
2691 int i;
2692 offset_type iter;
4b5246aa 2693 struct mapped_index *index;
9291a0cd
TT
2694
2695 dw2_setup (objfile);
ae2de4f8
DE
2696
2697 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2698 if (!dwarf2_per_objfile->index_table)
2699 return;
4b5246aa 2700 index = dwarf2_per_objfile->index_table;
9291a0cd 2701
7b08b9eb 2702 if (file_matcher != NULL)
24c79950
TT
2703 {
2704 struct cleanup *cleanup;
2705 htab_t visited_found, visited_not_found;
2706
2707 visited_found = htab_create_alloc (10,
2708 htab_hash_pointer, htab_eq_pointer,
2709 NULL, xcalloc, xfree);
2710 cleanup = make_cleanup_htab_delete (visited_found);
2711 visited_not_found = htab_create_alloc (10,
2712 htab_hash_pointer, htab_eq_pointer,
2713 NULL, xcalloc, xfree);
2714 make_cleanup_htab_delete (visited_not_found);
2715
2716 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2717 + dwarf2_per_objfile->n_type_units); ++i)
2718 {
2719 int j;
2720 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2721 struct quick_file_names *file_data;
2722 void **slot;
7b08b9eb 2723
24c79950 2724 per_cu->v.quick->mark = 0;
3d7bb9d9 2725
24c79950
TT
2726 /* We only need to look at symtabs not already expanded. */
2727 if (per_cu->v.quick->symtab)
2728 continue;
7b08b9eb 2729
24c79950
TT
2730 file_data = dw2_get_file_names (objfile, per_cu);
2731 if (file_data == NULL)
2732 continue;
7b08b9eb 2733
24c79950
TT
2734 if (htab_find (visited_not_found, file_data) != NULL)
2735 continue;
2736 else if (htab_find (visited_found, file_data) != NULL)
2737 {
2738 per_cu->v.quick->mark = 1;
2739 continue;
2740 }
2741
2742 for (j = 0; j < file_data->num_file_names; ++j)
2743 {
2744 if (file_matcher (file_data->file_names[j], data))
2745 {
2746 per_cu->v.quick->mark = 1;
2747 break;
2748 }
2749 }
2750
2751 slot = htab_find_slot (per_cu->v.quick->mark
2752 ? visited_found
2753 : visited_not_found,
2754 file_data, INSERT);
2755 *slot = file_data;
2756 }
2757
2758 do_cleanups (cleanup);
2759 }
9291a0cd 2760
3876f04e 2761 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2762 {
2763 offset_type idx = 2 * iter;
2764 const char *name;
2765 offset_type *vec, vec_len, vec_idx;
2766
3876f04e 2767 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2768 continue;
2769
3876f04e 2770 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2771
e078317b 2772 if (! (*name_matcher) (name, data))
9291a0cd
TT
2773 continue;
2774
2775 /* The name was matched, now expand corresponding CUs that were
2776 marked. */
4b5246aa 2777 vec = (offset_type *) (index->constant_pool
3876f04e 2778 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2779 vec_len = MAYBE_SWAP (vec[0]);
2780 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2781 {
e254ef6a 2782 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2783
e254ef6a 2784 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2785 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2786 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2787 }
2788 }
2789}
2790
2791static struct symtab *
2792dw2_find_pc_sect_symtab (struct objfile *objfile,
2793 struct minimal_symbol *msymbol,
2794 CORE_ADDR pc,
2795 struct obj_section *section,
2796 int warn_if_readin)
2797{
2798 struct dwarf2_per_cu_data *data;
2799
2800 dw2_setup (objfile);
2801
2802 if (!objfile->psymtabs_addrmap)
2803 return NULL;
2804
2805 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2806 if (!data)
2807 return NULL;
2808
2809 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2810 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2811 paddress (get_objfile_arch (objfile), pc));
2812
a0f42c21 2813 return dw2_instantiate_symtab (data);
9291a0cd
TT
2814}
2815
9291a0cd 2816static void
44b13c5a 2817dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2818 void *data, int need_fullname)
9291a0cd
TT
2819{
2820 int i;
24c79950
TT
2821 struct cleanup *cleanup;
2822 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2823 NULL, xcalloc, xfree);
9291a0cd 2824
24c79950 2825 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2826 dw2_setup (objfile);
ae2de4f8 2827
24c79950
TT
2828 /* We can ignore file names coming from already-expanded CUs. */
2829 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2830 + dwarf2_per_objfile->n_type_units); ++i)
2831 {
2832 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2833
2834 if (per_cu->v.quick->symtab)
2835 {
2836 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2837 INSERT);
2838
2839 *slot = per_cu->v.quick->file_names;
2840 }
2841 }
2842
1fd400ff 2843 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2844 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2845 {
2846 int j;
e254ef6a 2847 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2848 struct quick_file_names *file_data;
24c79950 2849 void **slot;
9291a0cd 2850
3d7bb9d9 2851 /* We only need to look at symtabs not already expanded. */
e254ef6a 2852 if (per_cu->v.quick->symtab)
9291a0cd
TT
2853 continue;
2854
7b9f3c50
DE
2855 file_data = dw2_get_file_names (objfile, per_cu);
2856 if (file_data == NULL)
9291a0cd
TT
2857 continue;
2858
24c79950
TT
2859 slot = htab_find_slot (visited, file_data, INSERT);
2860 if (*slot)
2861 {
2862 /* Already visited. */
2863 continue;
2864 }
2865 *slot = file_data;
2866
7b9f3c50 2867 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2868 {
74e2f255
DE
2869 const char *this_real_name;
2870
2871 if (need_fullname)
2872 this_real_name = dw2_get_real_path (objfile, file_data, j);
2873 else
2874 this_real_name = NULL;
7b9f3c50 2875 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2876 }
2877 }
24c79950
TT
2878
2879 do_cleanups (cleanup);
9291a0cd
TT
2880}
2881
2882static int
2883dw2_has_symbols (struct objfile *objfile)
2884{
2885 return 1;
2886}
2887
2888const struct quick_symbol_functions dwarf2_gdb_index_functions =
2889{
2890 dw2_has_symbols,
2891 dw2_find_last_source_symtab,
2892 dw2_forget_cached_source_info,
f8eba3c6 2893 dw2_map_symtabs_matching_filename,
9291a0cd 2894 dw2_lookup_symbol,
774b6a14 2895 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2896 dw2_print_stats,
2897 dw2_dump,
2898 dw2_relocate,
2899 dw2_expand_symtabs_for_function,
2900 dw2_expand_all_symtabs,
2901 dw2_expand_symtabs_with_filename,
2902 dw2_find_symbol_file,
40658b94 2903 dw2_map_matching_symbols,
9291a0cd
TT
2904 dw2_expand_symtabs_matching,
2905 dw2_find_pc_sect_symtab,
9291a0cd
TT
2906 dw2_map_symbol_filenames
2907};
2908
2909/* Initialize for reading DWARF for this objfile. Return 0 if this
2910 file will use psymtabs, or 1 if using the GNU index. */
2911
2912int
2913dwarf2_initialize_objfile (struct objfile *objfile)
2914{
2915 /* If we're about to read full symbols, don't bother with the
2916 indices. In this case we also don't care if some other debug
2917 format is making psymtabs, because they are all about to be
2918 expanded anyway. */
2919 if ((objfile->flags & OBJF_READNOW))
2920 {
2921 int i;
2922
2923 dwarf2_per_objfile->using_index = 1;
2924 create_all_comp_units (objfile);
1fd400ff 2925 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2926 dwarf2_per_objfile->quick_file_names_table =
2927 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2928
1fd400ff 2929 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2930 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2931 {
e254ef6a 2932 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2933
e254ef6a
DE
2934 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2935 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2936 }
2937
2938 /* Return 1 so that gdb sees the "quick" functions. However,
2939 these functions will be no-ops because we will have expanded
2940 all symtabs. */
2941 return 1;
2942 }
2943
2944 if (dwarf2_read_index (objfile))
2945 return 1;
2946
9291a0cd
TT
2947 return 0;
2948}
2949
2950\f
2951
dce234bc
PP
2952/* Build a partial symbol table. */
2953
2954void
f29dff0a 2955dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2956{
f29dff0a 2957 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2958 {
2959 init_psymbol_list (objfile, 1024);
2960 }
2961
d146bf1e 2962 dwarf2_build_psymtabs_hard (objfile);
c906108c 2963}
c906108c 2964
45452591
DE
2965/* Return TRUE if OFFSET is within CU_HEADER. */
2966
2967static inline int
2968offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2969{
2970 unsigned int bottom = cu_header->offset;
2971 unsigned int top = (cu_header->offset
2972 + cu_header->length
2973 + cu_header->initial_length_size);
9a619af0 2974
45452591
DE
2975 return (offset >= bottom && offset < top);
2976}
2977
93311388
DE
2978/* Read in the comp unit header information from the debug_info at info_ptr.
2979 NOTE: This leaves members offset, first_die_offset to be filled in
2980 by the caller. */
107d2387 2981
fe1b8b76 2982static gdb_byte *
107d2387 2983read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2984 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2985{
2986 int signed_addr;
891d2f0b 2987 unsigned int bytes_read;
c764a876
DE
2988
2989 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2990 cu_header->initial_length_size = bytes_read;
2991 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2992 info_ptr += bytes_read;
107d2387
AC
2993 cu_header->version = read_2_bytes (abfd, info_ptr);
2994 info_ptr += 2;
613e1657 2995 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2996 &bytes_read);
613e1657 2997 info_ptr += bytes_read;
107d2387
AC
2998 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2999 info_ptr += 1;
3000 signed_addr = bfd_get_sign_extend_vma (abfd);
3001 if (signed_addr < 0)
8e65ff28 3002 internal_error (__FILE__, __LINE__,
e2e0b3e5 3003 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3004 cu_header->signed_addr_p = signed_addr;
c764a876 3005
107d2387
AC
3006 return info_ptr;
3007}
3008
9ff913ba
DE
3009/* Subroutine of read_and_check_comp_unit_head and
3010 read_and_check_type_unit_head to simplify them.
3011 Perform various error checking on the header. */
3012
3013static void
3014error_check_comp_unit_head (struct comp_unit_head *header,
3015 struct dwarf2_section_info *section)
3016{
3017 bfd *abfd = section->asection->owner;
3018 const char *filename = bfd_get_filename (abfd);
3019
3020 if (header->version != 2 && header->version != 3 && header->version != 4)
3021 error (_("Dwarf Error: wrong version in compilation unit header "
3022 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3023 filename);
3024
3025 if (header->abbrev_offset
3026 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3027 &dwarf2_per_objfile->abbrev))
3028 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3029 "(offset 0x%lx + 6) [in module %s]"),
3030 (long) header->abbrev_offset, (long) header->offset,
3031 filename);
3032
3033 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3034 avoid potential 32-bit overflow. */
3035 if (((unsigned long) header->offset
3036 + header->length + header->initial_length_size)
3037 > section->size)
3038 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3039 "(offset 0x%lx + 0) [in module %s]"),
3040 (long) header->length, (long) header->offset,
3041 filename);
3042}
3043
3044/* Read in a CU/TU header and perform some basic error checking.
3045 The contents of the header are stored in HEADER.
3046 The result is a pointer to the start of the first DIE. */
adabb602 3047
fe1b8b76 3048static gdb_byte *
9ff913ba
DE
3049read_and_check_comp_unit_head (struct comp_unit_head *header,
3050 struct dwarf2_section_info *section,
3051 gdb_byte *info_ptr,
3052 int is_debug_types_section)
72bf9492 3053{
fe1b8b76 3054 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3055 bfd *abfd = section->asection->owner;
72bf9492 3056
9ff913ba 3057 header->offset = beg_of_comp_unit - section->buffer;
adabb602 3058
72bf9492
DJ
3059 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3060
460c1c54
CC
3061 /* If we're reading a type unit, skip over the signature and
3062 type_offset fields. */
b0df02fd 3063 if (is_debug_types_section)
460c1c54
CC
3064 info_ptr += 8 /*signature*/ + header->offset_size;
3065
adabb602
DE
3066 header->first_die_offset = info_ptr - beg_of_comp_unit;
3067
9ff913ba 3068 error_check_comp_unit_head (header, section);
72bf9492
DJ
3069
3070 return info_ptr;
3071}
3072
348e048f
DE
3073/* Read in the types comp unit header information from .debug_types entry at
3074 types_ptr. The result is a pointer to one past the end of the header. */
3075
3076static gdb_byte *
9ff913ba
DE
3077read_and_check_type_unit_head (struct comp_unit_head *header,
3078 struct dwarf2_section_info *section,
3079 gdb_byte *info_ptr,
3080 ULONGEST *signature, unsigned int *type_offset)
348e048f 3081{
9ff913ba
DE
3082 gdb_byte *beg_of_comp_unit = info_ptr;
3083 bfd *abfd = section->asection->owner;
348e048f 3084
9ff913ba 3085 header->offset = beg_of_comp_unit - section->buffer;
348e048f 3086
9ff913ba 3087 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3088
9ff913ba
DE
3089 /* If we're reading a type unit, skip over the signature and
3090 type_offset fields. */
3091 if (signature != NULL)
3092 *signature = read_8_bytes (abfd, info_ptr);
3093 info_ptr += 8;
3094 if (type_offset != NULL)
3095 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3096 info_ptr += header->offset_size;
3097
3098 header->first_die_offset = info_ptr - beg_of_comp_unit;
348e048f 3099
9ff913ba
DE
3100 error_check_comp_unit_head (header, section);
3101
3102 return info_ptr;
348e048f
DE
3103}
3104
aaa75496
JB
3105/* Allocate a new partial symtab for file named NAME and mark this new
3106 partial symtab as being an include of PST. */
3107
3108static void
3109dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3110 struct objfile *objfile)
3111{
3112 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3113
3114 subpst->section_offsets = pst->section_offsets;
3115 subpst->textlow = 0;
3116 subpst->texthigh = 0;
3117
3118 subpst->dependencies = (struct partial_symtab **)
3119 obstack_alloc (&objfile->objfile_obstack,
3120 sizeof (struct partial_symtab *));
3121 subpst->dependencies[0] = pst;
3122 subpst->number_of_dependencies = 1;
3123
3124 subpst->globals_offset = 0;
3125 subpst->n_global_syms = 0;
3126 subpst->statics_offset = 0;
3127 subpst->n_static_syms = 0;
3128 subpst->symtab = NULL;
3129 subpst->read_symtab = pst->read_symtab;
3130 subpst->readin = 0;
3131
3132 /* No private part is necessary for include psymtabs. This property
3133 can be used to differentiate between such include psymtabs and
10b3939b 3134 the regular ones. */
58a9656e 3135 subpst->read_symtab_private = NULL;
aaa75496
JB
3136}
3137
3138/* Read the Line Number Program data and extract the list of files
3139 included by the source file represented by PST. Build an include
d85a05f0 3140 partial symtab for each of these included files. */
aaa75496
JB
3141
3142static void
3143dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3144 struct die_info *die,
aaa75496
JB
3145 struct partial_symtab *pst)
3146{
3147 struct objfile *objfile = cu->objfile;
3148 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3149 struct line_header *lh = NULL;
3150 struct attribute *attr;
aaa75496 3151
d85a05f0
DJ
3152 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3153 if (attr)
3154 {
3155 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3156
d85a05f0
DJ
3157 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3158 }
aaa75496
JB
3159 if (lh == NULL)
3160 return; /* No linetable, so no includes. */
3161
c6da4cef 3162 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3163 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3164
3165 free_line_header (lh);
3166}
3167
348e048f
DE
3168static hashval_t
3169hash_type_signature (const void *item)
3170{
3171 const struct signatured_type *type_sig = item;
9a619af0 3172
348e048f
DE
3173 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3174 return type_sig->signature;
3175}
3176
3177static int
3178eq_type_signature (const void *item_lhs, const void *item_rhs)
3179{
3180 const struct signatured_type *lhs = item_lhs;
3181 const struct signatured_type *rhs = item_rhs;
9a619af0 3182
348e048f
DE
3183 return lhs->signature == rhs->signature;
3184}
3185
1fd400ff
TT
3186/* Allocate a hash table for signatured types. */
3187
3188static htab_t
673bfd45 3189allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3190{
3191 return htab_create_alloc_ex (41,
3192 hash_type_signature,
3193 eq_type_signature,
3194 NULL,
3195 &objfile->objfile_obstack,
3196 hashtab_obstack_allocate,
3197 dummy_obstack_deallocate);
3198}
3199
d467dd73 3200/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3201
3202static int
d467dd73 3203add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3204{
3205 struct signatured_type *sigt = *slot;
3206 struct dwarf2_per_cu_data ***datap = datum;
3207
3208 **datap = &sigt->per_cu;
3209 ++*datap;
3210
3211 return 1;
3212}
3213
d467dd73 3214/* Create the hash table of all entries in the .debug_types section(s).
348e048f
DE
3215 The result is zero if there is an error (e.g. missing .debug_types section),
3216 otherwise non-zero. */
3217
3218static int
3219create_debug_types_hash_table (struct objfile *objfile)
3220{
8b70b953 3221 htab_t types_htab = NULL;
1fd400ff 3222 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3223 int ix;
3224 struct dwarf2_section_info *section;
348e048f 3225
8b70b953 3226 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3227 {
3228 dwarf2_per_objfile->signatured_types = NULL;
3229 return 0;
3230 }
3231
8b70b953
TT
3232 for (ix = 0;
3233 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3234 ix, section);
3235 ++ix)
3236 {
3237 gdb_byte *info_ptr, *end_ptr;
348e048f 3238
8b70b953
TT
3239 dwarf2_read_section (objfile, section);
3240 info_ptr = section->buffer;
348e048f 3241
8b70b953
TT
3242 if (info_ptr == NULL)
3243 continue;
348e048f 3244
8b70b953
TT
3245 if (types_htab == NULL)
3246 types_htab = allocate_signatured_type_table (objfile);
348e048f 3247
8b70b953
TT
3248 if (dwarf2_die_debug)
3249 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3250
3251 end_ptr = info_ptr + section->size;
3252 while (info_ptr < end_ptr)
3253 {
3254 unsigned int offset;
8b70b953 3255 unsigned int type_offset;
8b70b953
TT
3256 ULONGEST signature;
3257 struct signatured_type *type_sig;
3258 void **slot;
3259 gdb_byte *ptr = info_ptr;
9ff913ba 3260 struct comp_unit_head header;
348e048f 3261
8b70b953 3262 offset = ptr - section->buffer;
348e048f 3263
8b70b953 3264 /* We need to read the type's signature in order to build the hash
9ff913ba 3265 table, but we don't need anything else just yet. */
348e048f 3266
9ff913ba
DE
3267 ptr = read_and_check_type_unit_head (&header, section, ptr,
3268 &signature, &type_offset);
6caca83c
CC
3269
3270 /* Skip dummy type units. */
3271 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3272 {
9ff913ba 3273 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3274 continue;
3275 }
8b70b953
TT
3276
3277 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3278 memset (type_sig, 0, sizeof (*type_sig));
3279 type_sig->signature = signature;
3280 type_sig->type_offset = type_offset;
3281 type_sig->per_cu.objfile = objfile;
b0df02fd 3282 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3283 type_sig->per_cu.offset = offset;
3284
3285 slot = htab_find_slot (types_htab, type_sig, INSERT);
3286 gdb_assert (slot != NULL);
3287 if (*slot != NULL)
3288 {
3289 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3290
8b70b953
TT
3291 complaint (&symfile_complaints,
3292 _("debug type entry at offset 0x%x is duplicate to the "
3293 "entry at offset 0x%x, signature 0x%s"),
3294 offset, dup_sig->per_cu.offset,
3295 phex (signature, sizeof (signature)));
3296 gdb_assert (signature == dup_sig->signature);
3297 }
3298 *slot = type_sig;
348e048f 3299
8b70b953
TT
3300 if (dwarf2_die_debug)
3301 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3302 offset, phex (signature, sizeof (signature)));
348e048f 3303
9ff913ba 3304 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3305 }
348e048f
DE
3306 }
3307
3308 dwarf2_per_objfile->signatured_types = types_htab;
3309
d467dd73
DE
3310 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3311 dwarf2_per_objfile->all_type_units
1fd400ff 3312 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3313 dwarf2_per_objfile->n_type_units
1fd400ff 3314 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3315 iter = &dwarf2_per_objfile->all_type_units[0];
3316 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3317 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3318 == dwarf2_per_objfile->n_type_units);
1fd400ff 3319
348e048f
DE
3320 return 1;
3321}
3322
3323/* Lookup a signature based type.
3324 Returns NULL if SIG is not present in the table. */
3325
3326static struct signatured_type *
3327lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3328{
3329 struct signatured_type find_entry, *entry;
3330
3331 if (dwarf2_per_objfile->signatured_types == NULL)
3332 {
3333 complaint (&symfile_complaints,
55f1336d 3334 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3335 return 0;
3336 }
3337
3338 find_entry.signature = sig;
3339 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3340 return entry;
3341}
3342
d85a05f0
DJ
3343/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3344
3345static void
3346init_cu_die_reader (struct die_reader_specs *reader,
3347 struct dwarf2_cu *cu)
3348{
3349 reader->abfd = cu->objfile->obfd;
3350 reader->cu = cu;
b0df02fd 3351 if (cu->per_cu->debug_types_section)
be391dca 3352 {
b0df02fd
DE
3353 gdb_assert (cu->per_cu->debug_types_section->readin);
3354 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3355 }
d85a05f0 3356 else
be391dca
TT
3357 {
3358 gdb_assert (dwarf2_per_objfile->info.readin);
3359 reader->buffer = dwarf2_per_objfile->info.buffer;
3360 }
d85a05f0
DJ
3361}
3362
3363/* Find the base address of the compilation unit for range lists and
3364 location lists. It will normally be specified by DW_AT_low_pc.
3365 In DWARF-3 draft 4, the base address could be overridden by
3366 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3367 compilation units with discontinuous ranges. */
3368
3369static void
3370dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3371{
3372 struct attribute *attr;
3373
3374 cu->base_known = 0;
3375 cu->base_address = 0;
3376
3377 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3378 if (attr)
3379 {
3380 cu->base_address = DW_ADDR (attr);
3381 cu->base_known = 1;
3382 }
3383 else
3384 {
3385 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3386 if (attr)
3387 {
3388 cu->base_address = DW_ADDR (attr);
3389 cu->base_known = 1;
3390 }
3391 }
3392}
3393
348e048f
DE
3394/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3395 to combine the common parts.
21b2bd31
DE
3396 Process compilation unit THIS_CU for a psymtab.
3397 SECTION is the section the CU/TU comes from,
3398 either .debug_info or .debug_types. */
aaa75496 3399
21b2bd31 3400void
a0f42c21 3401process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3402 struct dwarf2_section_info *section,
3403 int is_debug_types_section)
c906108c 3404{
a0f42c21 3405 struct objfile *objfile = this_cu->objfile;
c906108c 3406 bfd *abfd = objfile->obfd;
21b2bd31
DE
3407 gdb_byte *buffer = section->buffer;
3408 gdb_byte *info_ptr = buffer + this_cu->offset;
3409 unsigned int buffer_size = section->size;
93311388 3410 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3411 struct die_info *comp_unit_die;
c906108c 3412 struct partial_symtab *pst;
5734ee8b 3413 CORE_ADDR baseaddr;
93311388
DE
3414 struct cleanup *back_to_inner;
3415 struct dwarf2_cu cu;
d85a05f0
DJ
3416 int has_children, has_pc_info;
3417 struct attribute *attr;
d85a05f0
DJ
3418 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3419 struct die_reader_specs reader_specs;
3e2a0cee 3420 const char *filename;
c906108c 3421
23745b47
DE
3422 /* If this compilation unit was already read in, free the
3423 cached copy in order to read it in again. This is
3424 necessary because we skipped some symbols when we first
3425 read in the compilation unit (see load_partial_dies).
3426 This problem could be avoided, but the benefit is
3427 unclear. */
3428 if (this_cu->cu != NULL)
3429 free_one_cached_comp_unit (this_cu->cu);
3430
3431 /* Note that this is a pointer to our stack frame, being
3432 added to a global data structure. It will be cleaned up
3433 in free_stack_comp_unit when we finish with this
3434 compilation unit. */
3435 init_one_comp_unit (&cu, this_cu);
93311388 3436 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3437
9ff913ba
DE
3438 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3439 is_debug_types_section);
10b3939b 3440
6caca83c
CC
3441 /* Skip dummy compilation units. */
3442 if (info_ptr >= buffer + buffer_size
3443 || peek_abbrev_code (abfd, info_ptr) == 0)
3444 {
6caca83c 3445 do_cleanups (back_to_inner);
21b2bd31 3446 return;
6caca83c
CC
3447 }
3448
93311388 3449 cu.list_in_scope = &file_symbols;
af703f96 3450
93311388 3451 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3452 dwarf2_read_abbrevs (&cu);
93311388 3453 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3454
93311388 3455 /* Read the compilation unit die. */
d85a05f0
DJ
3456 init_cu_die_reader (&reader_specs, &cu);
3457 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3458 &has_children);
93311388 3459
21b2bd31 3460 if (is_debug_types_section)
348e048f 3461 {
b3c8eb43
JK
3462 /* LENGTH has not been set yet for type units. */
3463 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3464 this_cu->length = cu.header.length + cu.header.initial_length_size;
3465 }
d85a05f0 3466 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3467 {
93311388 3468 do_cleanups (back_to_inner);
21b2bd31 3469 return;
93311388 3470 }
72bf9492 3471
9816fde3 3472 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3473
93311388 3474 /* Allocate a new partial symbol table structure. */
d85a05f0 3475 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3476 if (attr == NULL || !DW_STRING (attr))
3477 filename = "";
3478 else
3479 filename = DW_STRING (attr);
93311388 3480 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3481 filename,
93311388
DE
3482 /* TEXTLOW and TEXTHIGH are set below. */
3483 0,
3484 objfile->global_psymbols.next,
3485 objfile->static_psymbols.next);
9750bca9 3486 pst->psymtabs_addrmap_supported = 1;
72bf9492 3487
d85a05f0
DJ
3488 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3489 if (attr != NULL)
3490 pst->dirname = DW_STRING (attr);
72bf9492 3491
e38df1d0 3492 pst->read_symtab_private = this_cu;
72bf9492 3493
93311388 3494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3495
0963b4bd 3496 /* Store the function that reads in the rest of the symbol table. */
93311388 3497 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3498
9291a0cd 3499 this_cu->v.psymtab = pst;
c906108c 3500
d85a05f0
DJ
3501 dwarf2_find_base_address (comp_unit_die, &cu);
3502
93311388
DE
3503 /* Possibly set the default values of LOWPC and HIGHPC from
3504 `DW_AT_ranges'. */
d85a05f0
DJ
3505 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3506 &best_highpc, &cu, pst);
3507 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3508 /* Store the contiguous range if it is not empty; it can be empty for
3509 CUs with no code. */
3510 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3511 best_lowpc + baseaddr,
3512 best_highpc + baseaddr - 1, pst);
93311388
DE
3513
3514 /* Check if comp unit has_children.
3515 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3516 If not, there's no more debug_info for this comp unit. */
d85a05f0 3517 if (has_children)
93311388
DE
3518 {
3519 struct partial_die_info *first_die;
3520 CORE_ADDR lowpc, highpc;
31ffec48 3521
93311388
DE
3522 lowpc = ((CORE_ADDR) -1);
3523 highpc = ((CORE_ADDR) 0);
c906108c 3524
93311388 3525 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3526
93311388 3527 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3528 ! has_pc_info, &cu);
57c22c6c 3529
93311388
DE
3530 /* If we didn't find a lowpc, set it to highpc to avoid
3531 complaints from `maint check'. */
3532 if (lowpc == ((CORE_ADDR) -1))
3533 lowpc = highpc;
10b3939b 3534
93311388
DE
3535 /* If the compilation unit didn't have an explicit address range,
3536 then use the information extracted from its child dies. */
d85a05f0 3537 if (! has_pc_info)
93311388 3538 {
d85a05f0
DJ
3539 best_lowpc = lowpc;
3540 best_highpc = highpc;
93311388
DE
3541 }
3542 }
d85a05f0
DJ
3543 pst->textlow = best_lowpc + baseaddr;
3544 pst->texthigh = best_highpc + baseaddr;
c906108c 3545
93311388
DE
3546 pst->n_global_syms = objfile->global_psymbols.next -
3547 (objfile->global_psymbols.list + pst->globals_offset);
3548 pst->n_static_syms = objfile->static_psymbols.next -
3549 (objfile->static_psymbols.list + pst->statics_offset);
3550 sort_pst_symbols (pst);
c906108c 3551
21b2bd31 3552 if (is_debug_types_section)
348e048f
DE
3553 {
3554 /* It's not clear we want to do anything with stmt lists here.
3555 Waiting to see what gcc ultimately does. */
3556 }
d85a05f0 3557 else
93311388
DE
3558 {
3559 /* Get the list of files included in the current compilation unit,
3560 and build a psymtab for each of them. */
d85a05f0 3561 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3562 }
ae038cb0 3563
93311388 3564 do_cleanups (back_to_inner);
93311388 3565}
ff013f42 3566
348e048f
DE
3567/* Traversal function for htab_traverse_noresize.
3568 Process one .debug_types comp-unit. */
3569
3570static int
3571process_type_comp_unit (void **slot, void *info)
3572{
3573 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3574 struct dwarf2_per_cu_data *this_cu;
3575
a0f42c21 3576 gdb_assert (info == NULL);
348e048f 3577 this_cu = &entry->per_cu;
348e048f 3578
b0df02fd 3579 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3580 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3581
3582 return 1;
3583}
3584
3585/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3586 Build partial symbol tables for the .debug_types comp-units. */
3587
3588static void
3589build_type_psymtabs (struct objfile *objfile)
3590{
3591 if (! create_debug_types_hash_table (objfile))
3592 return;
3593
3594 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3595 process_type_comp_unit, NULL);
348e048f
DE
3596}
3597
60606b2c
TT
3598/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3599
3600static void
3601psymtabs_addrmap_cleanup (void *o)
3602{
3603 struct objfile *objfile = o;
ec61707d 3604
60606b2c
TT
3605 objfile->psymtabs_addrmap = NULL;
3606}
3607
93311388
DE
3608/* Build the partial symbol table by doing a quick pass through the
3609 .debug_info and .debug_abbrev sections. */
72bf9492 3610
93311388 3611static void
c67a9c90 3612dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3613{
60606b2c
TT
3614 struct cleanup *back_to, *addrmap_cleanup;
3615 struct obstack temp_obstack;
21b2bd31 3616 int i;
93311388 3617
98bfdba5
PA
3618 dwarf2_per_objfile->reading_partial_symbols = 1;
3619
be391dca 3620 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3621
93311388
DE
3622 /* Any cached compilation units will be linked by the per-objfile
3623 read_in_chain. Make sure to free them when we're done. */
3624 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3625
348e048f
DE
3626 build_type_psymtabs (objfile);
3627
93311388 3628 create_all_comp_units (objfile);
c906108c 3629
60606b2c
TT
3630 /* Create a temporary address map on a temporary obstack. We later
3631 copy this to the final obstack. */
3632 obstack_init (&temp_obstack);
3633 make_cleanup_obstack_free (&temp_obstack);
3634 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3635 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3636
21b2bd31 3637 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3638 {
21b2bd31 3639 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3640
21b2bd31 3641 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3642 }
ff013f42
JK
3643
3644 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3645 &objfile->objfile_obstack);
60606b2c 3646 discard_cleanups (addrmap_cleanup);
ff013f42 3647
ae038cb0
DJ
3648 do_cleanups (back_to);
3649}
3650
93311388 3651/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3652
3653static void
a0f42c21 3654load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3655{
a0f42c21 3656 struct objfile *objfile = this_cu->objfile;
ae038cb0 3657 bfd *abfd = objfile->obfd;
adabb602 3658 gdb_byte *info_ptr;
d85a05f0 3659 struct die_info *comp_unit_die;
ae038cb0 3660 struct dwarf2_cu *cu;
1d9ec526 3661 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3662 int has_children;
3663 struct die_reader_specs reader_specs;
98bfdba5 3664 int read_cu = 0;
9ff913ba 3665 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3666
b0df02fd 3667 gdb_assert (! this_cu->debug_types_section);
348e048f 3668
9ff913ba
DE
3669 gdb_assert (section->readin);
3670 info_ptr = section->buffer + this_cu->offset;
ae038cb0 3671
98bfdba5
PA
3672 if (this_cu->cu == NULL)
3673 {
9816fde3 3674 cu = xmalloc (sizeof (*cu));
23745b47 3675 init_one_comp_unit (cu, this_cu);
ae038cb0 3676
98bfdba5 3677 read_cu = 1;
ae038cb0 3678
98bfdba5 3679 /* If an error occurs while loading, release our storage. */
68dc6402 3680 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3681
9ff913ba
DE
3682 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3683 0);
ae038cb0 3684
6caca83c 3685 /* Skip dummy compilation units. */
9ff913ba 3686 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3687 || peek_abbrev_code (abfd, info_ptr) == 0)
3688 {
3689 do_cleanups (free_cu_cleanup);
3690 return;
3691 }
3692
98bfdba5
PA
3693 /* Link this CU into read_in_chain. */
3694 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3695 dwarf2_per_objfile->read_in_chain = this_cu;
3696 }
3697 else
3698 {
3699 cu = this_cu->cu;
3700 info_ptr += cu->header.first_die_offset;
3701 }
ae038cb0
DJ
3702
3703 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3704 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3705 dwarf2_read_abbrevs (cu);
98bfdba5 3706 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3707
3708 /* Read the compilation unit die. */
d85a05f0
DJ
3709 init_cu_die_reader (&reader_specs, cu);
3710 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3711 &has_children);
ae038cb0 3712
9816fde3 3713 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3714
ae038cb0
DJ
3715 /* Check if comp unit has_children.
3716 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3717 If not, there's no more debug_info for this comp unit. */
d85a05f0 3718 if (has_children)
9ff913ba 3719 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3720
98bfdba5
PA
3721 do_cleanups (free_abbrevs_cleanup);
3722
3723 if (read_cu)
3724 {
3725 /* We've successfully allocated this compilation unit. Let our
3726 caller clean it up when finished with it. */
3727 discard_cleanups (free_cu_cleanup);
3728 }
ae038cb0
DJ
3729}
3730
9cdd5dbd
DE
3731/* Create a list of all compilation units in OBJFILE.
3732 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3733
3734static void
3735create_all_comp_units (struct objfile *objfile)
3736{
3737 int n_allocated;
3738 int n_comp_units;
3739 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3740 gdb_byte *info_ptr;
3741
3742 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3743 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3744
3745 n_comp_units = 0;
3746 n_allocated = 10;
3747 all_comp_units = xmalloc (n_allocated
3748 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3749
3e43a32a
MS
3750 while (info_ptr < dwarf2_per_objfile->info.buffer
3751 + dwarf2_per_objfile->info.size)
ae038cb0 3752 {
c764a876 3753 unsigned int length, initial_length_size;
ae038cb0 3754 struct dwarf2_per_cu_data *this_cu;
c764a876 3755 unsigned int offset;
ae038cb0 3756
dce234bc 3757 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3758
3759 /* Read just enough information to find out where the next
3760 compilation unit is. */
c764a876
DE
3761 length = read_initial_length (objfile->obfd, info_ptr,
3762 &initial_length_size);
ae038cb0
DJ
3763
3764 /* Save the compilation unit for later lookup. */
3765 this_cu = obstack_alloc (&objfile->objfile_obstack,
3766 sizeof (struct dwarf2_per_cu_data));
3767 memset (this_cu, 0, sizeof (*this_cu));
3768 this_cu->offset = offset;
c764a876 3769 this_cu->length = length + initial_length_size;
9291a0cd 3770 this_cu->objfile = objfile;
ae038cb0
DJ
3771
3772 if (n_comp_units == n_allocated)
3773 {
3774 n_allocated *= 2;
3775 all_comp_units = xrealloc (all_comp_units,
3776 n_allocated
3777 * sizeof (struct dwarf2_per_cu_data *));
3778 }
3779 all_comp_units[n_comp_units++] = this_cu;
3780
3781 info_ptr = info_ptr + this_cu->length;
3782 }
3783
3784 dwarf2_per_objfile->all_comp_units
3785 = obstack_alloc (&objfile->objfile_obstack,
3786 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3787 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3788 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3789 xfree (all_comp_units);
3790 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3791}
3792
5734ee8b
DJ
3793/* Process all loaded DIEs for compilation unit CU, starting at
3794 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3795 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3796 DW_AT_ranges). If NEED_PC is set, then this function will set
3797 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3798 and record the covered ranges in the addrmap. */
c906108c 3799
72bf9492
DJ
3800static void
3801scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3802 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3803{
72bf9492 3804 struct partial_die_info *pdi;
c906108c 3805
91c24f0a
DC
3806 /* Now, march along the PDI's, descending into ones which have
3807 interesting children but skipping the children of the other ones,
3808 until we reach the end of the compilation unit. */
c906108c 3809
72bf9492 3810 pdi = first_die;
91c24f0a 3811
72bf9492
DJ
3812 while (pdi != NULL)
3813 {
3814 fixup_partial_die (pdi, cu);
c906108c 3815
f55ee35c 3816 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3817 children, so we need to look at them. Ditto for anonymous
3818 enums. */
933c6fe4 3819
72bf9492 3820 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3821 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3822 {
72bf9492 3823 switch (pdi->tag)
c906108c
SS
3824 {
3825 case DW_TAG_subprogram:
5734ee8b 3826 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3827 break;
72929c62 3828 case DW_TAG_constant:
c906108c
SS
3829 case DW_TAG_variable:
3830 case DW_TAG_typedef:
91c24f0a 3831 case DW_TAG_union_type:
72bf9492 3832 if (!pdi->is_declaration)
63d06c5c 3833 {
72bf9492 3834 add_partial_symbol (pdi, cu);
63d06c5c
DC
3835 }
3836 break;
c906108c 3837 case DW_TAG_class_type:
680b30c7 3838 case DW_TAG_interface_type:
c906108c 3839 case DW_TAG_structure_type:
72bf9492 3840 if (!pdi->is_declaration)
c906108c 3841 {
72bf9492 3842 add_partial_symbol (pdi, cu);
c906108c
SS
3843 }
3844 break;
91c24f0a 3845 case DW_TAG_enumeration_type:
72bf9492
DJ
3846 if (!pdi->is_declaration)
3847 add_partial_enumeration (pdi, cu);
c906108c
SS
3848 break;
3849 case DW_TAG_base_type:
a02abb62 3850 case DW_TAG_subrange_type:
c906108c 3851 /* File scope base type definitions are added to the partial
c5aa993b 3852 symbol table. */
72bf9492 3853 add_partial_symbol (pdi, cu);
c906108c 3854 break;
d9fa45fe 3855 case DW_TAG_namespace:
5734ee8b 3856 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3857 break;
5d7cb8df
JK
3858 case DW_TAG_module:
3859 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3860 break;
c906108c
SS
3861 default:
3862 break;
3863 }
3864 }
3865
72bf9492
DJ
3866 /* If the die has a sibling, skip to the sibling. */
3867
3868 pdi = pdi->die_sibling;
3869 }
3870}
3871
3872/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3873
72bf9492 3874 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3875 name is concatenated with "::" and the partial DIE's name. For
3876 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3877 Enumerators are an exception; they use the scope of their parent
3878 enumeration type, i.e. the name of the enumeration type is not
3879 prepended to the enumerator.
91c24f0a 3880
72bf9492
DJ
3881 There are two complexities. One is DW_AT_specification; in this
3882 case "parent" means the parent of the target of the specification,
3883 instead of the direct parent of the DIE. The other is compilers
3884 which do not emit DW_TAG_namespace; in this case we try to guess
3885 the fully qualified name of structure types from their members'
3886 linkage names. This must be done using the DIE's children rather
3887 than the children of any DW_AT_specification target. We only need
3888 to do this for structures at the top level, i.e. if the target of
3889 any DW_AT_specification (if any; otherwise the DIE itself) does not
3890 have a parent. */
3891
3892/* Compute the scope prefix associated with PDI's parent, in
3893 compilation unit CU. The result will be allocated on CU's
3894 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3895 field. NULL is returned if no prefix is necessary. */
3896static char *
3897partial_die_parent_scope (struct partial_die_info *pdi,
3898 struct dwarf2_cu *cu)
3899{
3900 char *grandparent_scope;
3901 struct partial_die_info *parent, *real_pdi;
91c24f0a 3902
72bf9492
DJ
3903 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3904 then this means the parent of the specification DIE. */
3905
3906 real_pdi = pdi;
72bf9492 3907 while (real_pdi->has_specification)
10b3939b 3908 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3909
3910 parent = real_pdi->die_parent;
3911 if (parent == NULL)
3912 return NULL;
3913
3914 if (parent->scope_set)
3915 return parent->scope;
3916
3917 fixup_partial_die (parent, cu);
3918
10b3939b 3919 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3920
acebe513
UW
3921 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3922 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3923 Work around this problem here. */
3924 if (cu->language == language_cplus
6e70227d 3925 && parent->tag == DW_TAG_namespace
acebe513
UW
3926 && strcmp (parent->name, "::") == 0
3927 && grandparent_scope == NULL)
3928 {
3929 parent->scope = NULL;
3930 parent->scope_set = 1;
3931 return NULL;
3932 }
3933
9c6c53f7
SA
3934 if (pdi->tag == DW_TAG_enumerator)
3935 /* Enumerators should not get the name of the enumeration as a prefix. */
3936 parent->scope = grandparent_scope;
3937 else if (parent->tag == DW_TAG_namespace
f55ee35c 3938 || parent->tag == DW_TAG_module
72bf9492
DJ
3939 || parent->tag == DW_TAG_structure_type
3940 || parent->tag == DW_TAG_class_type
680b30c7 3941 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3942 || parent->tag == DW_TAG_union_type
3943 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3944 {
3945 if (grandparent_scope == NULL)
3946 parent->scope = parent->name;
3947 else
3e43a32a
MS
3948 parent->scope = typename_concat (&cu->comp_unit_obstack,
3949 grandparent_scope,
f55ee35c 3950 parent->name, 0, cu);
72bf9492 3951 }
72bf9492
DJ
3952 else
3953 {
3954 /* FIXME drow/2004-04-01: What should we be doing with
3955 function-local names? For partial symbols, we should probably be
3956 ignoring them. */
3957 complaint (&symfile_complaints,
e2e0b3e5 3958 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3959 parent->tag, pdi->offset);
3960 parent->scope = grandparent_scope;
c906108c
SS
3961 }
3962
72bf9492
DJ
3963 parent->scope_set = 1;
3964 return parent->scope;
3965}
3966
3967/* Return the fully scoped name associated with PDI, from compilation unit
3968 CU. The result will be allocated with malloc. */
3969static char *
3970partial_die_full_name (struct partial_die_info *pdi,
3971 struct dwarf2_cu *cu)
3972{
3973 char *parent_scope;
3974
98bfdba5
PA
3975 /* If this is a template instantiation, we can not work out the
3976 template arguments from partial DIEs. So, unfortunately, we have
3977 to go through the full DIEs. At least any work we do building
3978 types here will be reused if full symbols are loaded later. */
3979 if (pdi->has_template_arguments)
3980 {
3981 fixup_partial_die (pdi, cu);
3982
3983 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3984 {
3985 struct die_info *die;
3986 struct attribute attr;
3987 struct dwarf2_cu *ref_cu = cu;
3988
3989 attr.name = 0;
3990 attr.form = DW_FORM_ref_addr;
3991 attr.u.addr = pdi->offset;
3992 die = follow_die_ref (NULL, &attr, &ref_cu);
3993
3994 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3995 }
3996 }
3997
72bf9492
DJ
3998 parent_scope = partial_die_parent_scope (pdi, cu);
3999 if (parent_scope == NULL)
4000 return NULL;
4001 else
f55ee35c 4002 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4003}
4004
4005static void
72bf9492 4006add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4007{
e7c27a73 4008 struct objfile *objfile = cu->objfile;
c906108c 4009 CORE_ADDR addr = 0;
decbce07 4010 char *actual_name = NULL;
e142c38c 4011 CORE_ADDR baseaddr;
72bf9492 4012 int built_actual_name = 0;
e142c38c
DJ
4013
4014 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4015
94af9270
KS
4016 actual_name = partial_die_full_name (pdi, cu);
4017 if (actual_name)
4018 built_actual_name = 1;
63d06c5c 4019
72bf9492
DJ
4020 if (actual_name == NULL)
4021 actual_name = pdi->name;
4022
c906108c
SS
4023 switch (pdi->tag)
4024 {
4025 case DW_TAG_subprogram:
2cfa0c8d 4026 if (pdi->is_external || cu->language == language_ada)
c906108c 4027 {
2cfa0c8d
JB
4028 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4029 of the global scope. But in Ada, we want to be able to access
4030 nested procedures globally. So all Ada subprograms are stored
4031 in the global scope. */
f47fb265 4032 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4033 mst_text, objfile); */
f47fb265
MS
4034 add_psymbol_to_list (actual_name, strlen (actual_name),
4035 built_actual_name,
4036 VAR_DOMAIN, LOC_BLOCK,
4037 &objfile->global_psymbols,
4038 0, pdi->lowpc + baseaddr,
4039 cu->language, objfile);
c906108c
SS
4040 }
4041 else
4042 {
f47fb265 4043 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4044 mst_file_text, objfile); */
f47fb265
MS
4045 add_psymbol_to_list (actual_name, strlen (actual_name),
4046 built_actual_name,
4047 VAR_DOMAIN, LOC_BLOCK,
4048 &objfile->static_psymbols,
4049 0, pdi->lowpc + baseaddr,
4050 cu->language, objfile);
c906108c
SS
4051 }
4052 break;
72929c62
JB
4053 case DW_TAG_constant:
4054 {
4055 struct psymbol_allocation_list *list;
4056
4057 if (pdi->is_external)
4058 list = &objfile->global_psymbols;
4059 else
4060 list = &objfile->static_psymbols;
f47fb265
MS
4061 add_psymbol_to_list (actual_name, strlen (actual_name),
4062 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4063 list, 0, 0, cu->language, objfile);
72929c62
JB
4064 }
4065 break;
c906108c 4066 case DW_TAG_variable:
caac4577
JG
4067 if (pdi->locdesc)
4068 addr = decode_locdesc (pdi->locdesc, cu);
4069
4070 if (pdi->locdesc
4071 && addr == 0
4072 && !dwarf2_per_objfile->has_section_at_zero)
4073 {
4074 /* A global or static variable may also have been stripped
4075 out by the linker if unused, in which case its address
4076 will be nullified; do not add such variables into partial
4077 symbol table then. */
4078 }
4079 else if (pdi->is_external)
c906108c
SS
4080 {
4081 /* Global Variable.
4082 Don't enter into the minimal symbol tables as there is
4083 a minimal symbol table entry from the ELF symbols already.
4084 Enter into partial symbol table if it has a location
4085 descriptor or a type.
4086 If the location descriptor is missing, new_symbol will create
4087 a LOC_UNRESOLVED symbol, the address of the variable will then
4088 be determined from the minimal symbol table whenever the variable
4089 is referenced.
4090 The address for the partial symbol table entry is not
4091 used by GDB, but it comes in handy for debugging partial symbol
4092 table building. */
4093
c906108c 4094 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4095 add_psymbol_to_list (actual_name, strlen (actual_name),
4096 built_actual_name,
4097 VAR_DOMAIN, LOC_STATIC,
4098 &objfile->global_psymbols,
4099 0, addr + baseaddr,
4100 cu->language, objfile);
c906108c
SS
4101 }
4102 else
4103 {
0963b4bd 4104 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4105 if (pdi->locdesc == NULL)
decbce07
MS
4106 {
4107 if (built_actual_name)
4108 xfree (actual_name);
4109 return;
4110 }
f47fb265 4111 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4112 mst_file_data, objfile); */
f47fb265
MS
4113 add_psymbol_to_list (actual_name, strlen (actual_name),
4114 built_actual_name,
4115 VAR_DOMAIN, LOC_STATIC,
4116 &objfile->static_psymbols,
4117 0, addr + baseaddr,
4118 cu->language, objfile);
c906108c
SS
4119 }
4120 break;
4121 case DW_TAG_typedef:
4122 case DW_TAG_base_type:
a02abb62 4123 case DW_TAG_subrange_type:
38d518c9 4124 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4125 built_actual_name,
176620f1 4126 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4127 &objfile->static_psymbols,
e142c38c 4128 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4129 break;
72bf9492
DJ
4130 case DW_TAG_namespace:
4131 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4132 built_actual_name,
72bf9492
DJ
4133 VAR_DOMAIN, LOC_TYPEDEF,
4134 &objfile->global_psymbols,
4135 0, (CORE_ADDR) 0, cu->language, objfile);
4136 break;
c906108c 4137 case DW_TAG_class_type:
680b30c7 4138 case DW_TAG_interface_type:
c906108c
SS
4139 case DW_TAG_structure_type:
4140 case DW_TAG_union_type:
4141 case DW_TAG_enumeration_type:
fa4028e9
JB
4142 /* Skip external references. The DWARF standard says in the section
4143 about "Structure, Union, and Class Type Entries": "An incomplete
4144 structure, union or class type is represented by a structure,
4145 union or class entry that does not have a byte size attribute
4146 and that has a DW_AT_declaration attribute." */
4147 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4148 {
4149 if (built_actual_name)
4150 xfree (actual_name);
4151 return;
4152 }
fa4028e9 4153
63d06c5c
DC
4154 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4155 static vs. global. */
38d518c9 4156 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4157 built_actual_name,
176620f1 4158 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4159 (cu->language == language_cplus
4160 || cu->language == language_java)
63d06c5c
DC
4161 ? &objfile->global_psymbols
4162 : &objfile->static_psymbols,
e142c38c 4163 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4164
c906108c
SS
4165 break;
4166 case DW_TAG_enumerator:
38d518c9 4167 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4168 built_actual_name,
176620f1 4169 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4170 (cu->language == language_cplus
4171 || cu->language == language_java)
f6fe98ef
DJ
4172 ? &objfile->global_psymbols
4173 : &objfile->static_psymbols,
e142c38c 4174 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4175 break;
4176 default:
4177 break;
4178 }
5c4e30ca 4179
72bf9492
DJ
4180 if (built_actual_name)
4181 xfree (actual_name);
c906108c
SS
4182}
4183
5c4e30ca
DC
4184/* Read a partial die corresponding to a namespace; also, add a symbol
4185 corresponding to that namespace to the symbol table. NAMESPACE is
4186 the name of the enclosing namespace. */
91c24f0a 4187
72bf9492
DJ
4188static void
4189add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4190 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4191 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4192{
72bf9492 4193 /* Add a symbol for the namespace. */
e7c27a73 4194
72bf9492 4195 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4196
4197 /* Now scan partial symbols in that namespace. */
4198
91c24f0a 4199 if (pdi->has_children)
5734ee8b 4200 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4201}
4202
5d7cb8df
JK
4203/* Read a partial die corresponding to a Fortran module. */
4204
4205static void
4206add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4207 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4208{
f55ee35c 4209 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4210
4211 if (pdi->has_children)
4212 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4213}
4214
bc30ff58
JB
4215/* Read a partial die corresponding to a subprogram and create a partial
4216 symbol for that subprogram. When the CU language allows it, this
4217 routine also defines a partial symbol for each nested subprogram
4218 that this subprogram contains.
6e70227d 4219
bc30ff58
JB
4220 DIE my also be a lexical block, in which case we simply search
4221 recursively for suprograms defined inside that lexical block.
4222 Again, this is only performed when the CU language allows this
4223 type of definitions. */
4224
4225static void
4226add_partial_subprogram (struct partial_die_info *pdi,
4227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4228 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4229{
4230 if (pdi->tag == DW_TAG_subprogram)
4231 {
4232 if (pdi->has_pc_info)
4233 {
4234 if (pdi->lowpc < *lowpc)
4235 *lowpc = pdi->lowpc;
4236 if (pdi->highpc > *highpc)
4237 *highpc = pdi->highpc;
5734ee8b
DJ
4238 if (need_pc)
4239 {
4240 CORE_ADDR baseaddr;
4241 struct objfile *objfile = cu->objfile;
4242
4243 baseaddr = ANOFFSET (objfile->section_offsets,
4244 SECT_OFF_TEXT (objfile));
4245 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4246 pdi->lowpc + baseaddr,
4247 pdi->highpc - 1 + baseaddr,
9291a0cd 4248 cu->per_cu->v.psymtab);
5734ee8b 4249 }
bc30ff58 4250 if (!pdi->is_declaration)
e8d05480
JB
4251 /* Ignore subprogram DIEs that do not have a name, they are
4252 illegal. Do not emit a complaint at this point, we will
4253 do so when we convert this psymtab into a symtab. */
4254 if (pdi->name)
4255 add_partial_symbol (pdi, cu);
bc30ff58
JB
4256 }
4257 }
6e70227d 4258
bc30ff58
JB
4259 if (! pdi->has_children)
4260 return;
4261
4262 if (cu->language == language_ada)
4263 {
4264 pdi = pdi->die_child;
4265 while (pdi != NULL)
4266 {
4267 fixup_partial_die (pdi, cu);
4268 if (pdi->tag == DW_TAG_subprogram
4269 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4270 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4271 pdi = pdi->die_sibling;
4272 }
4273 }
4274}
4275
91c24f0a
DC
4276/* Read a partial die corresponding to an enumeration type. */
4277
72bf9492
DJ
4278static void
4279add_partial_enumeration (struct partial_die_info *enum_pdi,
4280 struct dwarf2_cu *cu)
91c24f0a 4281{
72bf9492 4282 struct partial_die_info *pdi;
91c24f0a
DC
4283
4284 if (enum_pdi->name != NULL)
72bf9492
DJ
4285 add_partial_symbol (enum_pdi, cu);
4286
4287 pdi = enum_pdi->die_child;
4288 while (pdi)
91c24f0a 4289 {
72bf9492 4290 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4291 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4292 else
72bf9492
DJ
4293 add_partial_symbol (pdi, cu);
4294 pdi = pdi->die_sibling;
91c24f0a 4295 }
91c24f0a
DC
4296}
4297
6caca83c
CC
4298/* Return the initial uleb128 in the die at INFO_PTR. */
4299
4300static unsigned int
4301peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4302{
4303 unsigned int bytes_read;
4304
4305 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4306}
4307
4bb7a0a7
DJ
4308/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4309 Return the corresponding abbrev, or NULL if the number is zero (indicating
4310 an empty DIE). In either case *BYTES_READ will be set to the length of
4311 the initial number. */
4312
4313static struct abbrev_info *
fe1b8b76 4314peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4315 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4316{
4317 bfd *abfd = cu->objfile->obfd;
4318 unsigned int abbrev_number;
4319 struct abbrev_info *abbrev;
4320
4321 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4322
4323 if (abbrev_number == 0)
4324 return NULL;
4325
4326 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4327 if (!abbrev)
4328 {
3e43a32a
MS
4329 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4330 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4331 }
4332
4333 return abbrev;
4334}
4335
93311388
DE
4336/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4337 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4338 DIE. Any children of the skipped DIEs will also be skipped. */
4339
fe1b8b76 4340static gdb_byte *
93311388 4341skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4342{
4343 struct abbrev_info *abbrev;
4344 unsigned int bytes_read;
4345
4346 while (1)
4347 {
4348 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4349 if (abbrev == NULL)
4350 return info_ptr + bytes_read;
4351 else
93311388 4352 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4353 }
4354}
4355
93311388
DE
4356/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4357 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4358 abbrev corresponding to that skipped uleb128 should be passed in
4359 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4360 children. */
4361
fe1b8b76 4362static gdb_byte *
93311388
DE
4363skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4364 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4365{
4366 unsigned int bytes_read;
4367 struct attribute attr;
4368 bfd *abfd = cu->objfile->obfd;
4369 unsigned int form, i;
4370
4371 for (i = 0; i < abbrev->num_attrs; i++)
4372 {
4373 /* The only abbrev we care about is DW_AT_sibling. */
4374 if (abbrev->attrs[i].name == DW_AT_sibling)
4375 {
4376 read_attribute (&attr, &abbrev->attrs[i],
4377 abfd, info_ptr, cu);
4378 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4379 complaint (&symfile_complaints,
4380 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4381 else
93311388 4382 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4383 }
4384
4385 /* If it isn't DW_AT_sibling, skip this attribute. */
4386 form = abbrev->attrs[i].form;
4387 skip_attribute:
4388 switch (form)
4389 {
4bb7a0a7 4390 case DW_FORM_ref_addr:
ae411497
TT
4391 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4392 and later it is offset sized. */
4393 if (cu->header.version == 2)
4394 info_ptr += cu->header.addr_size;
4395 else
4396 info_ptr += cu->header.offset_size;
4397 break;
4398 case DW_FORM_addr:
4bb7a0a7
DJ
4399 info_ptr += cu->header.addr_size;
4400 break;
4401 case DW_FORM_data1:
4402 case DW_FORM_ref1:
4403 case DW_FORM_flag:
4404 info_ptr += 1;
4405 break;
2dc7f7b3
TT
4406 case DW_FORM_flag_present:
4407 break;
4bb7a0a7
DJ
4408 case DW_FORM_data2:
4409 case DW_FORM_ref2:
4410 info_ptr += 2;
4411 break;
4412 case DW_FORM_data4:
4413 case DW_FORM_ref4:
4414 info_ptr += 4;
4415 break;
4416 case DW_FORM_data8:
4417 case DW_FORM_ref8:
55f1336d 4418 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4419 info_ptr += 8;
4420 break;
4421 case DW_FORM_string:
9b1c24c8 4422 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4423 info_ptr += bytes_read;
4424 break;
2dc7f7b3 4425 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4426 case DW_FORM_strp:
4427 info_ptr += cu->header.offset_size;
4428 break;
2dc7f7b3 4429 case DW_FORM_exprloc:
4bb7a0a7
DJ
4430 case DW_FORM_block:
4431 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4432 info_ptr += bytes_read;
4433 break;
4434 case DW_FORM_block1:
4435 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4436 break;
4437 case DW_FORM_block2:
4438 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4439 break;
4440 case DW_FORM_block4:
4441 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4442 break;
4443 case DW_FORM_sdata:
4444 case DW_FORM_udata:
4445 case DW_FORM_ref_udata:
4446 info_ptr = skip_leb128 (abfd, info_ptr);
4447 break;
4448 case DW_FORM_indirect:
4449 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4450 info_ptr += bytes_read;
4451 /* We need to continue parsing from here, so just go back to
4452 the top. */
4453 goto skip_attribute;
4454
4455 default:
3e43a32a
MS
4456 error (_("Dwarf Error: Cannot handle %s "
4457 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4458 dwarf_form_name (form),
4459 bfd_get_filename (abfd));
4460 }
4461 }
4462
4463 if (abbrev->has_children)
93311388 4464 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4465 else
4466 return info_ptr;
4467}
4468
93311388
DE
4469/* Locate ORIG_PDI's sibling.
4470 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4471 in BUFFER. */
91c24f0a 4472
fe1b8b76 4473static gdb_byte *
93311388
DE
4474locate_pdi_sibling (struct partial_die_info *orig_pdi,
4475 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4476 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4477{
4478 /* Do we know the sibling already? */
72bf9492 4479
91c24f0a
DC
4480 if (orig_pdi->sibling)
4481 return orig_pdi->sibling;
4482
4483 /* Are there any children to deal with? */
4484
4485 if (!orig_pdi->has_children)
4486 return info_ptr;
4487
4bb7a0a7 4488 /* Skip the children the long way. */
91c24f0a 4489
93311388 4490 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4491}
4492
c906108c
SS
4493/* Expand this partial symbol table into a full symbol table. */
4494
4495static void
fba45db2 4496dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4497{
c906108c
SS
4498 if (pst != NULL)
4499 {
4500 if (pst->readin)
4501 {
3e43a32a
MS
4502 warning (_("bug: psymtab for %s is already read in."),
4503 pst->filename);
c906108c
SS
4504 }
4505 else
4506 {
4507 if (info_verbose)
4508 {
3e43a32a
MS
4509 printf_filtered (_("Reading in symbols for %s..."),
4510 pst->filename);
c906108c
SS
4511 gdb_flush (gdb_stdout);
4512 }
4513
10b3939b
DJ
4514 /* Restore our global data. */
4515 dwarf2_per_objfile = objfile_data (pst->objfile,
4516 dwarf2_objfile_data_key);
4517
b2ab525c
KB
4518 /* If this psymtab is constructed from a debug-only objfile, the
4519 has_section_at_zero flag will not necessarily be correct. We
4520 can get the correct value for this flag by looking at the data
4521 associated with the (presumably stripped) associated objfile. */
4522 if (pst->objfile->separate_debug_objfile_backlink)
4523 {
4524 struct dwarf2_per_objfile *dpo_backlink
4525 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4526 dwarf2_objfile_data_key);
9a619af0 4527
b2ab525c
KB
4528 dwarf2_per_objfile->has_section_at_zero
4529 = dpo_backlink->has_section_at_zero;
4530 }
4531
98bfdba5
PA
4532 dwarf2_per_objfile->reading_partial_symbols = 0;
4533
c906108c
SS
4534 psymtab_to_symtab_1 (pst);
4535
4536 /* Finish up the debug error message. */
4537 if (info_verbose)
a3f17187 4538 printf_filtered (_("done.\n"));
c906108c
SS
4539 }
4540 }
4541}
9cdd5dbd
DE
4542\f
4543/* Reading in full CUs. */
c906108c 4544
10b3939b
DJ
4545/* Add PER_CU to the queue. */
4546
4547static void
a0f42c21 4548queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4549{
4550 struct dwarf2_queue_item *item;
4551
4552 per_cu->queued = 1;
4553 item = xmalloc (sizeof (*item));
4554 item->per_cu = per_cu;
4555 item->next = NULL;
4556
4557 if (dwarf2_queue == NULL)
4558 dwarf2_queue = item;
4559 else
4560 dwarf2_queue_tail->next = item;
4561
4562 dwarf2_queue_tail = item;
4563}
4564
4565/* Process the queue. */
4566
4567static void
a0f42c21 4568process_queue (void)
10b3939b
DJ
4569{
4570 struct dwarf2_queue_item *item, *next_item;
4571
03dd20cc
DJ
4572 /* The queue starts out with one item, but following a DIE reference
4573 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4574 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4575 {
9291a0cd
TT
4576 if (dwarf2_per_objfile->using_index
4577 ? !item->per_cu->v.quick->symtab
4578 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4579 process_full_comp_unit (item->per_cu);
4580
4581 item->per_cu->queued = 0;
4582 next_item = item->next;
4583 xfree (item);
4584 }
4585
4586 dwarf2_queue_tail = NULL;
4587}
4588
4589/* Free all allocated queue entries. This function only releases anything if
4590 an error was thrown; if the queue was processed then it would have been
4591 freed as we went along. */
4592
4593static void
4594dwarf2_release_queue (void *dummy)
4595{
4596 struct dwarf2_queue_item *item, *last;
4597
4598 item = dwarf2_queue;
4599 while (item)
4600 {
4601 /* Anything still marked queued is likely to be in an
4602 inconsistent state, so discard it. */
4603 if (item->per_cu->queued)
4604 {
4605 if (item->per_cu->cu != NULL)
4606 free_one_cached_comp_unit (item->per_cu->cu);
4607 item->per_cu->queued = 0;
4608 }
4609
4610 last = item;
4611 item = item->next;
4612 xfree (last);
4613 }
4614
4615 dwarf2_queue = dwarf2_queue_tail = NULL;
4616}
4617
4618/* Read in full symbols for PST, and anything it depends on. */
4619
c906108c 4620static void
fba45db2 4621psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4622{
10b3939b 4623 struct dwarf2_per_cu_data *per_cu;
c906108c 4624 struct cleanup *back_to;
aaa75496
JB
4625 int i;
4626
4627 for (i = 0; i < pst->number_of_dependencies; i++)
4628 if (!pst->dependencies[i]->readin)
4629 {
4630 /* Inform about additional files that need to be read in. */
4631 if (info_verbose)
4632 {
a3f17187 4633 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4634 fputs_filtered (" ", gdb_stdout);
4635 wrap_here ("");
4636 fputs_filtered ("and ", gdb_stdout);
4637 wrap_here ("");
4638 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4639 wrap_here (""); /* Flush output. */
aaa75496
JB
4640 gdb_flush (gdb_stdout);
4641 }
4642 psymtab_to_symtab_1 (pst->dependencies[i]);
4643 }
4644
e38df1d0 4645 per_cu = pst->read_symtab_private;
10b3939b
DJ
4646
4647 if (per_cu == NULL)
aaa75496
JB
4648 {
4649 /* It's an include file, no symbols to read for it.
4650 Everything is in the parent symtab. */
4651 pst->readin = 1;
4652 return;
4653 }
c906108c 4654
a0f42c21 4655 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4656}
4657
93311388 4658/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4659
93311388 4660static void
a0f42c21 4661load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4662{
a0f42c21 4663 struct objfile *objfile = per_cu->objfile;
31ffec48 4664 bfd *abfd = objfile->obfd;
10b3939b 4665 struct dwarf2_cu *cu;
c764a876 4666 unsigned int offset;
93311388 4667 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4668 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4669 struct attribute *attr;
98bfdba5 4670 int read_cu = 0;
6502dd73 4671
b0df02fd 4672 gdb_assert (! per_cu->debug_types_section);
348e048f 4673
c906108c 4674 /* Set local variables from the partial symbol table info. */
10b3939b 4675 offset = per_cu->offset;
6502dd73 4676
be391dca 4677 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4678 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4679 beg_of_comp_unit = info_ptr;
63d06c5c 4680
98bfdba5
PA
4681 if (per_cu->cu == NULL)
4682 {
9816fde3 4683 cu = xmalloc (sizeof (*cu));
23745b47 4684 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4685
4686 read_cu = 1;
c906108c 4687
98bfdba5 4688 /* If an error occurs while loading, release our storage. */
68dc6402 4689 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4690
98bfdba5
PA
4691 /* Read in the comp_unit header. */
4692 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4693
6caca83c
CC
4694 /* Skip dummy compilation units. */
4695 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4696 + dwarf2_per_objfile->info.size)
4697 || peek_abbrev_code (abfd, info_ptr) == 0)
4698 {
4699 do_cleanups (free_cu_cleanup);
4700 return;
4701 }
4702
98bfdba5
PA
4703 /* Complete the cu_header. */
4704 cu->header.offset = offset;
4705 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4706
98bfdba5 4707 /* Read the abbrevs for this compilation unit. */
e5fe5e75 4708 dwarf2_read_abbrevs (cu);
98bfdba5 4709 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4710
98bfdba5
PA
4711 /* Link this CU into read_in_chain. */
4712 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4713 dwarf2_per_objfile->read_in_chain = per_cu;
4714 }
4715 else
4716 {
4717 cu = per_cu->cu;
4718 info_ptr += cu->header.first_die_offset;
4719 }
e142c38c 4720
93311388 4721 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4722
4723 /* We try not to read any attributes in this function, because not
9cdd5dbd 4724 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4725 table processing isn't initialized. But we have to set the CU language,
4726 or we won't be able to build types correctly. */
9816fde3 4727 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4728
a6c727b2
DJ
4729 /* Similarly, if we do not read the producer, we can not apply
4730 producer-specific interpretation. */
4731 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4732 if (attr)
4733 cu->producer = DW_STRING (attr);
4734
98bfdba5
PA
4735 if (read_cu)
4736 {
4737 do_cleanups (free_abbrevs_cleanup);
e142c38c 4738
98bfdba5
PA
4739 /* We've successfully allocated this compilation unit. Let our
4740 caller clean it up when finished with it. */
4741 discard_cleanups (free_cu_cleanup);
4742 }
10b3939b
DJ
4743}
4744
3da10d80
KS
4745/* Add a DIE to the delayed physname list. */
4746
4747static void
4748add_to_method_list (struct type *type, int fnfield_index, int index,
4749 const char *name, struct die_info *die,
4750 struct dwarf2_cu *cu)
4751{
4752 struct delayed_method_info mi;
4753 mi.type = type;
4754 mi.fnfield_index = fnfield_index;
4755 mi.index = index;
4756 mi.name = name;
4757 mi.die = die;
4758 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4759}
4760
4761/* A cleanup for freeing the delayed method list. */
4762
4763static void
4764free_delayed_list (void *ptr)
4765{
4766 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4767 if (cu->method_list != NULL)
4768 {
4769 VEC_free (delayed_method_info, cu->method_list);
4770 cu->method_list = NULL;
4771 }
4772}
4773
4774/* Compute the physnames of any methods on the CU's method list.
4775
4776 The computation of method physnames is delayed in order to avoid the
4777 (bad) condition that one of the method's formal parameters is of an as yet
4778 incomplete type. */
4779
4780static void
4781compute_delayed_physnames (struct dwarf2_cu *cu)
4782{
4783 int i;
4784 struct delayed_method_info *mi;
4785 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4786 {
1d06ead6 4787 const char *physname;
3da10d80
KS
4788 struct fn_fieldlist *fn_flp
4789 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4790 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4791 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4792 }
4793}
4794
9cdd5dbd 4795/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4796 already been loaded into memory. */
4797
4798static void
4799process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4800{
10b3939b 4801 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4802 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4803 CORE_ADDR lowpc, highpc;
4804 struct symtab *symtab;
3da10d80 4805 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4806 CORE_ADDR baseaddr;
4807
4808 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4809
10b3939b
DJ
4810 buildsym_init ();
4811 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4812 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4813
4814 cu->list_in_scope = &file_symbols;
c906108c
SS
4815
4816 /* Do line number decoding in read_file_scope () */
10b3939b 4817 process_die (cu->dies, cu);
c906108c 4818
3da10d80
KS
4819 /* Now that we have processed all the DIEs in the CU, all the types
4820 should be complete, and it should now be safe to compute all of the
4821 physnames. */
4822 compute_delayed_physnames (cu);
4823 do_cleanups (delayed_list_cleanup);
4824
fae299cd
DC
4825 /* Some compilers don't define a DW_AT_high_pc attribute for the
4826 compilation unit. If the DW_AT_high_pc is missing, synthesize
4827 it, by scanning the DIE's below the compilation unit. */
10b3939b 4828 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4829
613e1657 4830 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4831
8be455d7 4832 if (symtab != NULL)
c906108c 4833 {
df15bd07 4834 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4835
8be455d7
JK
4836 /* Set symtab language to language from DW_AT_language. If the
4837 compilation is from a C file generated by language preprocessors, do
4838 not set the language if it was already deduced by start_subfile. */
4839 if (!(cu->language == language_c && symtab->language != language_c))
4840 symtab->language = cu->language;
4841
4842 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4843 produce DW_AT_location with location lists but it can be possibly
4844 invalid without -fvar-tracking.
4845
4846 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4847 needed, it would be wrong due to missing DW_AT_producer there.
4848
4849 Still one can confuse GDB by using non-standard GCC compilation
4850 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4851 */
4632c0d0 4852 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4853 symtab->locations_valid = 1;
e0d00bc7
JK
4854
4855 if (gcc_4_minor >= 5)
4856 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4857
4858 symtab->call_site_htab = cu->call_site_htab;
c906108c 4859 }
9291a0cd
TT
4860
4861 if (dwarf2_per_objfile->using_index)
4862 per_cu->v.quick->symtab = symtab;
4863 else
4864 {
4865 struct partial_symtab *pst = per_cu->v.psymtab;
4866 pst->symtab = symtab;
4867 pst->readin = 1;
4868 }
c906108c
SS
4869
4870 do_cleanups (back_to);
4871}
4872
4873/* Process a die and its children. */
4874
4875static void
e7c27a73 4876process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4877{
4878 switch (die->tag)
4879 {
4880 case DW_TAG_padding:
4881 break;
4882 case DW_TAG_compile_unit:
e7c27a73 4883 read_file_scope (die, cu);
c906108c 4884 break;
348e048f
DE
4885 case DW_TAG_type_unit:
4886 read_type_unit_scope (die, cu);
4887 break;
c906108c 4888 case DW_TAG_subprogram:
c906108c 4889 case DW_TAG_inlined_subroutine:
edb3359d 4890 read_func_scope (die, cu);
c906108c
SS
4891 break;
4892 case DW_TAG_lexical_block:
14898363
L
4893 case DW_TAG_try_block:
4894 case DW_TAG_catch_block:
e7c27a73 4895 read_lexical_block_scope (die, cu);
c906108c 4896 break;
96408a79
SA
4897 case DW_TAG_GNU_call_site:
4898 read_call_site_scope (die, cu);
4899 break;
c906108c 4900 case DW_TAG_class_type:
680b30c7 4901 case DW_TAG_interface_type:
c906108c
SS
4902 case DW_TAG_structure_type:
4903 case DW_TAG_union_type:
134d01f1 4904 process_structure_scope (die, cu);
c906108c
SS
4905 break;
4906 case DW_TAG_enumeration_type:
134d01f1 4907 process_enumeration_scope (die, cu);
c906108c 4908 break;
134d01f1 4909
f792889a
DJ
4910 /* These dies have a type, but processing them does not create
4911 a symbol or recurse to process the children. Therefore we can
4912 read them on-demand through read_type_die. */
c906108c 4913 case DW_TAG_subroutine_type:
72019c9c 4914 case DW_TAG_set_type:
c906108c 4915 case DW_TAG_array_type:
c906108c 4916 case DW_TAG_pointer_type:
c906108c 4917 case DW_TAG_ptr_to_member_type:
c906108c 4918 case DW_TAG_reference_type:
c906108c 4919 case DW_TAG_string_type:
c906108c 4920 break;
134d01f1 4921
c906108c 4922 case DW_TAG_base_type:
a02abb62 4923 case DW_TAG_subrange_type:
cb249c71 4924 case DW_TAG_typedef:
134d01f1
DJ
4925 /* Add a typedef symbol for the type definition, if it has a
4926 DW_AT_name. */
f792889a 4927 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4928 break;
c906108c 4929 case DW_TAG_common_block:
e7c27a73 4930 read_common_block (die, cu);
c906108c
SS
4931 break;
4932 case DW_TAG_common_inclusion:
4933 break;
d9fa45fe 4934 case DW_TAG_namespace:
63d06c5c 4935 processing_has_namespace_info = 1;
e7c27a73 4936 read_namespace (die, cu);
d9fa45fe 4937 break;
5d7cb8df 4938 case DW_TAG_module:
f55ee35c 4939 processing_has_namespace_info = 1;
5d7cb8df
JK
4940 read_module (die, cu);
4941 break;
d9fa45fe
DC
4942 case DW_TAG_imported_declaration:
4943 case DW_TAG_imported_module:
63d06c5c 4944 processing_has_namespace_info = 1;
27aa8d6a
SW
4945 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4946 || cu->language != language_fortran))
4947 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4948 dwarf_tag_name (die->tag));
4949 read_import_statement (die, cu);
d9fa45fe 4950 break;
c906108c 4951 default:
e7c27a73 4952 new_symbol (die, NULL, cu);
c906108c
SS
4953 break;
4954 }
4955}
4956
94af9270
KS
4957/* A helper function for dwarf2_compute_name which determines whether DIE
4958 needs to have the name of the scope prepended to the name listed in the
4959 die. */
4960
4961static int
4962die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4963{
1c809c68
TT
4964 struct attribute *attr;
4965
94af9270
KS
4966 switch (die->tag)
4967 {
4968 case DW_TAG_namespace:
4969 case DW_TAG_typedef:
4970 case DW_TAG_class_type:
4971 case DW_TAG_interface_type:
4972 case DW_TAG_structure_type:
4973 case DW_TAG_union_type:
4974 case DW_TAG_enumeration_type:
4975 case DW_TAG_enumerator:
4976 case DW_TAG_subprogram:
4977 case DW_TAG_member:
4978 return 1;
4979
4980 case DW_TAG_variable:
c2b0a229 4981 case DW_TAG_constant:
94af9270
KS
4982 /* We only need to prefix "globally" visible variables. These include
4983 any variable marked with DW_AT_external or any variable that
4984 lives in a namespace. [Variables in anonymous namespaces
4985 require prefixing, but they are not DW_AT_external.] */
4986
4987 if (dwarf2_attr (die, DW_AT_specification, cu))
4988 {
4989 struct dwarf2_cu *spec_cu = cu;
9a619af0 4990
94af9270
KS
4991 return die_needs_namespace (die_specification (die, &spec_cu),
4992 spec_cu);
4993 }
4994
1c809c68 4995 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4996 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4997 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4998 return 0;
4999 /* A variable in a lexical block of some kind does not need a
5000 namespace, even though in C++ such variables may be external
5001 and have a mangled name. */
5002 if (die->parent->tag == DW_TAG_lexical_block
5003 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5004 || die->parent->tag == DW_TAG_catch_block
5005 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5006 return 0;
5007 return 1;
94af9270
KS
5008
5009 default:
5010 return 0;
5011 }
5012}
5013
98bfdba5
PA
5014/* Retrieve the last character from a mem_file. */
5015
5016static void
5017do_ui_file_peek_last (void *object, const char *buffer, long length)
5018{
5019 char *last_char_p = (char *) object;
5020
5021 if (length > 0)
5022 *last_char_p = buffer[length - 1];
5023}
5024
94af9270
KS
5025/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5026 compute the physname for the object, which include a method's
5027 formal parameters (C++/Java) and return type (Java).
5028
af6b7be1
JB
5029 For Ada, return the DIE's linkage name rather than the fully qualified
5030 name. PHYSNAME is ignored..
5031
94af9270
KS
5032 The result is allocated on the objfile_obstack and canonicalized. */
5033
5034static const char *
5035dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5036 int physname)
5037{
bb5ed363
DE
5038 struct objfile *objfile = cu->objfile;
5039
94af9270
KS
5040 if (name == NULL)
5041 name = dwarf2_name (die, cu);
5042
f55ee35c
JK
5043 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5044 compute it by typename_concat inside GDB. */
5045 if (cu->language == language_ada
5046 || (cu->language == language_fortran && physname))
5047 {
5048 /* For Ada unit, we prefer the linkage name over the name, as
5049 the former contains the exported name, which the user expects
5050 to be able to reference. Ideally, we want the user to be able
5051 to reference this entity using either natural or linkage name,
5052 but we haven't started looking at this enhancement yet. */
5053 struct attribute *attr;
5054
5055 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5056 if (attr == NULL)
5057 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5058 if (attr && DW_STRING (attr))
5059 return DW_STRING (attr);
5060 }
5061
94af9270
KS
5062 /* These are the only languages we know how to qualify names in. */
5063 if (name != NULL
f55ee35c
JK
5064 && (cu->language == language_cplus || cu->language == language_java
5065 || cu->language == language_fortran))
94af9270
KS
5066 {
5067 if (die_needs_namespace (die, cu))
5068 {
5069 long length;
0d5cff50 5070 const char *prefix;
94af9270
KS
5071 struct ui_file *buf;
5072
5073 prefix = determine_prefix (die, cu);
5074 buf = mem_fileopen ();
5075 if (*prefix != '\0')
5076 {
f55ee35c
JK
5077 char *prefixed_name = typename_concat (NULL, prefix, name,
5078 physname, cu);
9a619af0 5079
94af9270
KS
5080 fputs_unfiltered (prefixed_name, buf);
5081 xfree (prefixed_name);
5082 }
5083 else
62d5b8da 5084 fputs_unfiltered (name, buf);
94af9270 5085
98bfdba5
PA
5086 /* Template parameters may be specified in the DIE's DW_AT_name, or
5087 as children with DW_TAG_template_type_param or
5088 DW_TAG_value_type_param. If the latter, add them to the name
5089 here. If the name already has template parameters, then
5090 skip this step; some versions of GCC emit both, and
5091 it is more efficient to use the pre-computed name.
5092
5093 Something to keep in mind about this process: it is very
5094 unlikely, or in some cases downright impossible, to produce
5095 something that will match the mangled name of a function.
5096 If the definition of the function has the same debug info,
5097 we should be able to match up with it anyway. But fallbacks
5098 using the minimal symbol, for instance to find a method
5099 implemented in a stripped copy of libstdc++, will not work.
5100 If we do not have debug info for the definition, we will have to
5101 match them up some other way.
5102
5103 When we do name matching there is a related problem with function
5104 templates; two instantiated function templates are allowed to
5105 differ only by their return types, which we do not add here. */
5106
5107 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5108 {
5109 struct attribute *attr;
5110 struct die_info *child;
5111 int first = 1;
5112
5113 die->building_fullname = 1;
5114
5115 for (child = die->child; child != NULL; child = child->sibling)
5116 {
5117 struct type *type;
5118 long value;
5119 gdb_byte *bytes;
5120 struct dwarf2_locexpr_baton *baton;
5121 struct value *v;
5122
5123 if (child->tag != DW_TAG_template_type_param
5124 && child->tag != DW_TAG_template_value_param)
5125 continue;
5126
5127 if (first)
5128 {
5129 fputs_unfiltered ("<", buf);
5130 first = 0;
5131 }
5132 else
5133 fputs_unfiltered (", ", buf);
5134
5135 attr = dwarf2_attr (child, DW_AT_type, cu);
5136 if (attr == NULL)
5137 {
5138 complaint (&symfile_complaints,
5139 _("template parameter missing DW_AT_type"));
5140 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5141 continue;
5142 }
5143 type = die_type (child, cu);
5144
5145 if (child->tag == DW_TAG_template_type_param)
5146 {
5147 c_print_type (type, "", buf, -1, 0);
5148 continue;
5149 }
5150
5151 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5152 if (attr == NULL)
5153 {
5154 complaint (&symfile_complaints,
3e43a32a
MS
5155 _("template parameter missing "
5156 "DW_AT_const_value"));
98bfdba5
PA
5157 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5158 continue;
5159 }
5160
5161 dwarf2_const_value_attr (attr, type, name,
5162 &cu->comp_unit_obstack, cu,
5163 &value, &bytes, &baton);
5164
5165 if (TYPE_NOSIGN (type))
5166 /* GDB prints characters as NUMBER 'CHAR'. If that's
5167 changed, this can use value_print instead. */
5168 c_printchar (value, type, buf);
5169 else
5170 {
5171 struct value_print_options opts;
5172
5173 if (baton != NULL)
5174 v = dwarf2_evaluate_loc_desc (type, NULL,
5175 baton->data,
5176 baton->size,
5177 baton->per_cu);
5178 else if (bytes != NULL)
5179 {
5180 v = allocate_value (type);
5181 memcpy (value_contents_writeable (v), bytes,
5182 TYPE_LENGTH (type));
5183 }
5184 else
5185 v = value_from_longest (type, value);
5186
3e43a32a
MS
5187 /* Specify decimal so that we do not depend on
5188 the radix. */
98bfdba5
PA
5189 get_formatted_print_options (&opts, 'd');
5190 opts.raw = 1;
5191 value_print (v, buf, &opts);
5192 release_value (v);
5193 value_free (v);
5194 }
5195 }
5196
5197 die->building_fullname = 0;
5198
5199 if (!first)
5200 {
5201 /* Close the argument list, with a space if necessary
5202 (nested templates). */
5203 char last_char = '\0';
5204 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5205 if (last_char == '>')
5206 fputs_unfiltered (" >", buf);
5207 else
5208 fputs_unfiltered (">", buf);
5209 }
5210 }
5211
94af9270
KS
5212 /* For Java and C++ methods, append formal parameter type
5213 information, if PHYSNAME. */
6e70227d 5214
94af9270
KS
5215 if (physname && die->tag == DW_TAG_subprogram
5216 && (cu->language == language_cplus
5217 || cu->language == language_java))
5218 {
5219 struct type *type = read_type_die (die, cu);
5220
3167638f 5221 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5222
5223 if (cu->language == language_java)
5224 {
5225 /* For java, we must append the return type to method
0963b4bd 5226 names. */
94af9270
KS
5227 if (die->tag == DW_TAG_subprogram)
5228 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5229 0, 0);
5230 }
5231 else if (cu->language == language_cplus)
5232 {
60430eff
DJ
5233 /* Assume that an artificial first parameter is
5234 "this", but do not crash if it is not. RealView
5235 marks unnamed (and thus unused) parameters as
5236 artificial; there is no way to differentiate
5237 the two cases. */
94af9270
KS
5238 if (TYPE_NFIELDS (type) > 0
5239 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5240 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5241 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5242 0))))
94af9270
KS
5243 fputs_unfiltered (" const", buf);
5244 }
5245 }
5246
bb5ed363 5247 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5248 &length);
5249 ui_file_delete (buf);
5250
5251 if (cu->language == language_cplus)
5252 {
5253 char *cname
5254 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5255 &objfile->objfile_obstack);
9a619af0 5256
94af9270
KS
5257 if (cname != NULL)
5258 name = cname;
5259 }
5260 }
5261 }
5262
5263 return name;
5264}
5265
0114d602
DJ
5266/* Return the fully qualified name of DIE, based on its DW_AT_name.
5267 If scope qualifiers are appropriate they will be added. The result
5268 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5269 not have a name. NAME may either be from a previous call to
5270 dwarf2_name or NULL.
5271
0963b4bd 5272 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5273
5274static const char *
94af9270 5275dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5276{
94af9270
KS
5277 return dwarf2_compute_name (name, die, cu, 0);
5278}
0114d602 5279
94af9270
KS
5280/* Construct a physname for the given DIE in CU. NAME may either be
5281 from a previous call to dwarf2_name or NULL. The result will be
5282 allocated on the objfile_objstack or NULL if the DIE does not have a
5283 name.
0114d602 5284
94af9270 5285 The output string will be canonicalized (if C++/Java). */
0114d602 5286
94af9270
KS
5287static const char *
5288dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5289{
bb5ed363 5290 struct objfile *objfile = cu->objfile;
900e11f9
JK
5291 struct attribute *attr;
5292 const char *retval, *mangled = NULL, *canon = NULL;
5293 struct cleanup *back_to;
5294 int need_copy = 1;
5295
5296 /* In this case dwarf2_compute_name is just a shortcut not building anything
5297 on its own. */
5298 if (!die_needs_namespace (die, cu))
5299 return dwarf2_compute_name (name, die, cu, 1);
5300
5301 back_to = make_cleanup (null_cleanup, NULL);
5302
5303 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5304 if (!attr)
5305 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5306
5307 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5308 has computed. */
5309 if (attr && DW_STRING (attr))
5310 {
5311 char *demangled;
5312
5313 mangled = DW_STRING (attr);
5314
5315 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5316 type. It is easier for GDB users to search for such functions as
5317 `name(params)' than `long name(params)'. In such case the minimal
5318 symbol names do not match the full symbol names but for template
5319 functions there is never a need to look up their definition from their
5320 declaration so the only disadvantage remains the minimal symbol
5321 variant `long name(params)' does not have the proper inferior type.
5322 */
5323
5324 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5325 | (cu->language == language_java
5326 ? DMGL_JAVA | DMGL_RET_POSTFIX
5327 : DMGL_RET_DROP)));
5328 if (demangled)
5329 {
5330 make_cleanup (xfree, demangled);
5331 canon = demangled;
5332 }
5333 else
5334 {
5335 canon = mangled;
5336 need_copy = 0;
5337 }
5338 }
5339
5340 if (canon == NULL || check_physname)
5341 {
5342 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5343
5344 if (canon != NULL && strcmp (physname, canon) != 0)
5345 {
5346 /* It may not mean a bug in GDB. The compiler could also
5347 compute DW_AT_linkage_name incorrectly. But in such case
5348 GDB would need to be bug-to-bug compatible. */
5349
5350 complaint (&symfile_complaints,
5351 _("Computed physname <%s> does not match demangled <%s> "
5352 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
bb5ed363 5353 physname, canon, mangled, die->offset, objfile->name);
900e11f9
JK
5354
5355 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5356 is available here - over computed PHYSNAME. It is safer
5357 against both buggy GDB and buggy compilers. */
5358
5359 retval = canon;
5360 }
5361 else
5362 {
5363 retval = physname;
5364 need_copy = 0;
5365 }
5366 }
5367 else
5368 retval = canon;
5369
5370 if (need_copy)
5371 retval = obsavestring (retval, strlen (retval),
bb5ed363 5372 &objfile->objfile_obstack);
900e11f9
JK
5373
5374 do_cleanups (back_to);
5375 return retval;
0114d602
DJ
5376}
5377
27aa8d6a
SW
5378/* Read the import statement specified by the given die and record it. */
5379
5380static void
5381read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5382{
bb5ed363 5383 struct objfile *objfile = cu->objfile;
27aa8d6a 5384 struct attribute *import_attr;
32019081 5385 struct die_info *imported_die, *child_die;
de4affc9 5386 struct dwarf2_cu *imported_cu;
27aa8d6a 5387 const char *imported_name;
794684b6 5388 const char *imported_name_prefix;
13387711
SW
5389 const char *canonical_name;
5390 const char *import_alias;
5391 const char *imported_declaration = NULL;
794684b6 5392 const char *import_prefix;
32019081
JK
5393 VEC (const_char_ptr) *excludes = NULL;
5394 struct cleanup *cleanups;
13387711
SW
5395
5396 char *temp;
27aa8d6a
SW
5397
5398 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5399 if (import_attr == NULL)
5400 {
5401 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5402 dwarf_tag_name (die->tag));
5403 return;
5404 }
5405
de4affc9
CC
5406 imported_cu = cu;
5407 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5408 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5409 if (imported_name == NULL)
5410 {
5411 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5412
5413 The import in the following code:
5414 namespace A
5415 {
5416 typedef int B;
5417 }
5418
5419 int main ()
5420 {
5421 using A::B;
5422 B b;
5423 return b;
5424 }
5425
5426 ...
5427 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5428 <52> DW_AT_decl_file : 1
5429 <53> DW_AT_decl_line : 6
5430 <54> DW_AT_import : <0x75>
5431 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5432 <59> DW_AT_name : B
5433 <5b> DW_AT_decl_file : 1
5434 <5c> DW_AT_decl_line : 2
5435 <5d> DW_AT_type : <0x6e>
5436 ...
5437 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5438 <76> DW_AT_byte_size : 4
5439 <77> DW_AT_encoding : 5 (signed)
5440
5441 imports the wrong die ( 0x75 instead of 0x58 ).
5442 This case will be ignored until the gcc bug is fixed. */
5443 return;
5444 }
5445
82856980
SW
5446 /* Figure out the local name after import. */
5447 import_alias = dwarf2_name (die, cu);
27aa8d6a 5448
794684b6
SW
5449 /* Figure out where the statement is being imported to. */
5450 import_prefix = determine_prefix (die, cu);
5451
5452 /* Figure out what the scope of the imported die is and prepend it
5453 to the name of the imported die. */
de4affc9 5454 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5455
f55ee35c
JK
5456 if (imported_die->tag != DW_TAG_namespace
5457 && imported_die->tag != DW_TAG_module)
794684b6 5458 {
13387711
SW
5459 imported_declaration = imported_name;
5460 canonical_name = imported_name_prefix;
794684b6 5461 }
13387711 5462 else if (strlen (imported_name_prefix) > 0)
794684b6 5463 {
13387711
SW
5464 temp = alloca (strlen (imported_name_prefix)
5465 + 2 + strlen (imported_name) + 1);
5466 strcpy (temp, imported_name_prefix);
5467 strcat (temp, "::");
5468 strcat (temp, imported_name);
5469 canonical_name = temp;
794684b6 5470 }
13387711
SW
5471 else
5472 canonical_name = imported_name;
794684b6 5473
32019081
JK
5474 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5475
5476 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5477 for (child_die = die->child; child_die && child_die->tag;
5478 child_die = sibling_die (child_die))
5479 {
5480 /* DWARF-4: A Fortran use statement with a “rename list” may be
5481 represented by an imported module entry with an import attribute
5482 referring to the module and owned entries corresponding to those
5483 entities that are renamed as part of being imported. */
5484
5485 if (child_die->tag != DW_TAG_imported_declaration)
5486 {
5487 complaint (&symfile_complaints,
5488 _("child DW_TAG_imported_declaration expected "
5489 "- DIE at 0x%x [in module %s]"),
bb5ed363 5490 child_die->offset, objfile->name);
32019081
JK
5491 continue;
5492 }
5493
5494 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5495 if (import_attr == NULL)
5496 {
5497 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5498 dwarf_tag_name (child_die->tag));
5499 continue;
5500 }
5501
5502 imported_cu = cu;
5503 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5504 &imported_cu);
5505 imported_name = dwarf2_name (imported_die, imported_cu);
5506 if (imported_name == NULL)
5507 {
5508 complaint (&symfile_complaints,
5509 _("child DW_TAG_imported_declaration has unknown "
5510 "imported name - DIE at 0x%x [in module %s]"),
bb5ed363 5511 child_die->offset, objfile->name);
32019081
JK
5512 continue;
5513 }
5514
5515 VEC_safe_push (const_char_ptr, excludes, imported_name);
5516
5517 process_die (child_die, cu);
5518 }
5519
c0cc3a76
SW
5520 cp_add_using_directive (import_prefix,
5521 canonical_name,
5522 import_alias,
13387711 5523 imported_declaration,
32019081 5524 excludes,
bb5ed363 5525 &objfile->objfile_obstack);
32019081
JK
5526
5527 do_cleanups (cleanups);
27aa8d6a
SW
5528}
5529
ae2de4f8
DE
5530/* Cleanup function for read_file_scope. */
5531
cb1df416
DJ
5532static void
5533free_cu_line_header (void *arg)
5534{
5535 struct dwarf2_cu *cu = arg;
5536
5537 free_line_header (cu->line_header);
5538 cu->line_header = NULL;
5539}
5540
9291a0cd
TT
5541static void
5542find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5543 char **name, char **comp_dir)
5544{
5545 struct attribute *attr;
5546
5547 *name = NULL;
5548 *comp_dir = NULL;
5549
5550 /* Find the filename. Do not use dwarf2_name here, since the filename
5551 is not a source language identifier. */
5552 attr = dwarf2_attr (die, DW_AT_name, cu);
5553 if (attr)
5554 {
5555 *name = DW_STRING (attr);
5556 }
5557
5558 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5559 if (attr)
5560 *comp_dir = DW_STRING (attr);
5561 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5562 {
5563 *comp_dir = ldirname (*name);
5564 if (*comp_dir != NULL)
5565 make_cleanup (xfree, *comp_dir);
5566 }
5567 if (*comp_dir != NULL)
5568 {
5569 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5570 directory, get rid of it. */
5571 char *cp = strchr (*comp_dir, ':');
5572
5573 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5574 *comp_dir = cp + 1;
5575 }
5576
5577 if (*name == NULL)
5578 *name = "<unknown>";
5579}
5580
f3f5162e
DE
5581/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5582 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5583 COMP_DIR is the compilation directory.
5584 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5585
5586static void
5587handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5588 const char *comp_dir, int want_line_info)
2ab95328
TT
5589{
5590 struct attribute *attr;
5591 struct objfile *objfile = cu->objfile;
5592 bfd *abfd = objfile->obfd;
5593
2ab95328
TT
5594 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5595 if (attr)
5596 {
5597 unsigned int line_offset = DW_UNSND (attr);
5598 struct line_header *line_header
5599 = dwarf_decode_line_header (line_offset, abfd, cu);
5600
5601 if (line_header)
5602 {
5603 cu->line_header = line_header;
5604 make_cleanup (free_cu_line_header, cu);
f3f5162e 5605 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5606 }
5607 }
5608}
5609
ae2de4f8
DE
5610/* Process DW_TAG_compile_unit. */
5611
c906108c 5612static void
e7c27a73 5613read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5614{
e7c27a73 5615 struct objfile *objfile = cu->objfile;
debd256d 5616 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5617 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5618 CORE_ADDR highpc = ((CORE_ADDR) 0);
5619 struct attribute *attr;
e1024ff1 5620 char *name = NULL;
c906108c
SS
5621 char *comp_dir = NULL;
5622 struct die_info *child_die;
5623 bfd *abfd = objfile->obfd;
e142c38c 5624 CORE_ADDR baseaddr;
6e70227d 5625
e142c38c 5626 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5627
fae299cd 5628 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5629
5630 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5631 from finish_block. */
2acceee2 5632 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5633 lowpc = highpc;
5634 lowpc += baseaddr;
5635 highpc += baseaddr;
5636
9291a0cd 5637 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5638
e142c38c 5639 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5640 if (attr)
5641 {
e142c38c 5642 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5643 }
5644
b0f35d58 5645 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5646 if (attr)
b0f35d58 5647 cu->producer = DW_STRING (attr);
303b6f5d 5648
f4b8a18d
KW
5649 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5650 standardised yet. As a workaround for the language detection we fall
5651 back to the DW_AT_producer string. */
5652 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5653 cu->language = language_opencl;
5654
0963b4bd 5655 /* We assume that we're processing GCC output. */
c906108c 5656 processing_gcc_compilation = 2;
c906108c 5657
df8a16a1
DJ
5658 processing_has_namespace_info = 0;
5659
c906108c
SS
5660 start_symtab (name, comp_dir, lowpc);
5661 record_debugformat ("DWARF 2");
303b6f5d 5662 record_producer (cu->producer);
c906108c 5663
f3f5162e
DE
5664 /* Decode line number information if present. We do this before
5665 processing child DIEs, so that the line header table is available
5666 for DW_AT_decl_file. */
5667 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5668
cb1df416
DJ
5669 /* Process all dies in compilation unit. */
5670 if (die->child != NULL)
5671 {
5672 child_die = die->child;
5673 while (child_die && child_die->tag)
5674 {
5675 process_die (child_die, cu);
5676 child_die = sibling_die (child_die);
5677 }
5678 }
5679
2e276125
JB
5680 /* Decode macro information, if present. Dwarf 2 macro information
5681 refers to information in the line number info statement program
5682 header, so we can only read it if we've read the header
5683 successfully. */
cf2c3c16 5684 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5685 if (attr && cu->line_header)
2e276125 5686 {
cf2c3c16
TT
5687 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5688 complaint (&symfile_complaints,
5689 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5690
5691 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5692 comp_dir, abfd, cu,
5693 &dwarf2_per_objfile->macro, 1);
5694 }
5695 else
5696 {
5697 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5698 if (attr && cu->line_header)
5699 {
5700 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5701
cf2c3c16
TT
5702 dwarf_decode_macros (cu->line_header, macro_offset,
5703 comp_dir, abfd, cu,
5704 &dwarf2_per_objfile->macinfo, 0);
5705 }
2e276125 5706 }
9cdd5dbd 5707
debd256d 5708 do_cleanups (back_to);
5fb290d7
DJ
5709}
5710
ae2de4f8
DE
5711/* Process DW_TAG_type_unit.
5712 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5713 actual type being defined by this TU. In this case the first top
5714 level sibling is there to provide context only. */
5715
5716static void
5717read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5718{
5719 struct objfile *objfile = cu->objfile;
5720 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5721 CORE_ADDR lowpc;
5722 struct attribute *attr;
5723 char *name = NULL;
5724 char *comp_dir = NULL;
5725 struct die_info *child_die;
5726 bfd *abfd = objfile->obfd;
348e048f
DE
5727
5728 /* start_symtab needs a low pc, but we don't really have one.
5729 Do what read_file_scope would do in the absence of such info. */
5730 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5731
5732 /* Find the filename. Do not use dwarf2_name here, since the filename
5733 is not a source language identifier. */
5734 attr = dwarf2_attr (die, DW_AT_name, cu);
5735 if (attr)
5736 name = DW_STRING (attr);
5737
5738 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5739 if (attr)
5740 comp_dir = DW_STRING (attr);
5741 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5742 {
5743 comp_dir = ldirname (name);
5744 if (comp_dir != NULL)
5745 make_cleanup (xfree, comp_dir);
5746 }
5747
5748 if (name == NULL)
5749 name = "<unknown>";
5750
5751 attr = dwarf2_attr (die, DW_AT_language, cu);
5752 if (attr)
5753 set_cu_language (DW_UNSND (attr), cu);
5754
5755 /* This isn't technically needed today. It is done for symmetry
5756 with read_file_scope. */
5757 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5758 if (attr)
348e048f
DE
5759 cu->producer = DW_STRING (attr);
5760
0963b4bd 5761 /* We assume that we're processing GCC output. */
348e048f
DE
5762 processing_gcc_compilation = 2;
5763
5764 processing_has_namespace_info = 0;
5765
5766 start_symtab (name, comp_dir, lowpc);
5767 record_debugformat ("DWARF 2");
5768 record_producer (cu->producer);
5769
f3f5162e
DE
5770 /* Decode line number information if present. We do this before
5771 processing child DIEs, so that the line header table is available
5772 for DW_AT_decl_file.
5773 We don't need the pc/line-number mapping for type units. */
5774 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5775
348e048f
DE
5776 /* Process the dies in the type unit. */
5777 if (die->child == NULL)
5778 {
5779 dump_die_for_error (die);
5780 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5781 bfd_get_filename (abfd));
5782 }
5783
5784 child_die = die->child;
5785
5786 while (child_die && child_die->tag)
5787 {
5788 process_die (child_die, cu);
5789
5790 child_die = sibling_die (child_die);
5791 }
5792
5793 do_cleanups (back_to);
5794}
5795
d389af10
JK
5796/* qsort helper for inherit_abstract_dies. */
5797
5798static int
5799unsigned_int_compar (const void *ap, const void *bp)
5800{
5801 unsigned int a = *(unsigned int *) ap;
5802 unsigned int b = *(unsigned int *) bp;
5803
5804 return (a > b) - (b > a);
5805}
5806
5807/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5808 Inherit only the children of the DW_AT_abstract_origin DIE not being
5809 already referenced by DW_AT_abstract_origin from the children of the
5810 current DIE. */
d389af10
JK
5811
5812static void
5813inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5814{
5815 struct die_info *child_die;
5816 unsigned die_children_count;
5817 /* CU offsets which were referenced by children of the current DIE. */
5818 unsigned *offsets;
5819 unsigned *offsets_end, *offsetp;
5820 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5821 struct die_info *origin_die;
5822 /* Iterator of the ORIGIN_DIE children. */
5823 struct die_info *origin_child_die;
5824 struct cleanup *cleanups;
5825 struct attribute *attr;
cd02d79d
PA
5826 struct dwarf2_cu *origin_cu;
5827 struct pending **origin_previous_list_in_scope;
d389af10
JK
5828
5829 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5830 if (!attr)
5831 return;
5832
cd02d79d
PA
5833 /* Note that following die references may follow to a die in a
5834 different cu. */
5835
5836 origin_cu = cu;
5837 origin_die = follow_die_ref (die, attr, &origin_cu);
5838
5839 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5840 symbols in. */
5841 origin_previous_list_in_scope = origin_cu->list_in_scope;
5842 origin_cu->list_in_scope = cu->list_in_scope;
5843
edb3359d
DJ
5844 if (die->tag != origin_die->tag
5845 && !(die->tag == DW_TAG_inlined_subroutine
5846 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5847 complaint (&symfile_complaints,
5848 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5849 die->offset, origin_die->offset);
5850
5851 child_die = die->child;
5852 die_children_count = 0;
5853 while (child_die && child_die->tag)
5854 {
5855 child_die = sibling_die (child_die);
5856 die_children_count++;
5857 }
5858 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5859 cleanups = make_cleanup (xfree, offsets);
5860
5861 offsets_end = offsets;
5862 child_die = die->child;
5863 while (child_die && child_die->tag)
5864 {
c38f313d
DJ
5865 /* For each CHILD_DIE, find the corresponding child of
5866 ORIGIN_DIE. If there is more than one layer of
5867 DW_AT_abstract_origin, follow them all; there shouldn't be,
5868 but GCC versions at least through 4.4 generate this (GCC PR
5869 40573). */
5870 struct die_info *child_origin_die = child_die;
cd02d79d 5871 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5872
c38f313d
DJ
5873 while (1)
5874 {
cd02d79d
PA
5875 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5876 child_origin_cu);
c38f313d
DJ
5877 if (attr == NULL)
5878 break;
cd02d79d
PA
5879 child_origin_die = follow_die_ref (child_origin_die, attr,
5880 &child_origin_cu);
c38f313d
DJ
5881 }
5882
d389af10
JK
5883 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5884 counterpart may exist. */
c38f313d 5885 if (child_origin_die != child_die)
d389af10 5886 {
edb3359d
DJ
5887 if (child_die->tag != child_origin_die->tag
5888 && !(child_die->tag == DW_TAG_inlined_subroutine
5889 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5890 complaint (&symfile_complaints,
5891 _("Child DIE 0x%x and its abstract origin 0x%x have "
5892 "different tags"), child_die->offset,
5893 child_origin_die->offset);
c38f313d
DJ
5894 if (child_origin_die->parent != origin_die)
5895 complaint (&symfile_complaints,
5896 _("Child DIE 0x%x and its abstract origin 0x%x have "
5897 "different parents"), child_die->offset,
5898 child_origin_die->offset);
5899 else
5900 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5901 }
5902 child_die = sibling_die (child_die);
5903 }
5904 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5905 unsigned_int_compar);
5906 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5907 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5908 complaint (&symfile_complaints,
5909 _("Multiple children of DIE 0x%x refer "
5910 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5911 die->offset, *offsetp);
5912
5913 offsetp = offsets;
5914 origin_child_die = origin_die->child;
5915 while (origin_child_die && origin_child_die->tag)
5916 {
5917 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5918 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5919 offsetp++;
5920 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5921 {
5922 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5923 process_die (origin_child_die, origin_cu);
d389af10
JK
5924 }
5925 origin_child_die = sibling_die (origin_child_die);
5926 }
cd02d79d 5927 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5928
5929 do_cleanups (cleanups);
5930}
5931
c906108c 5932static void
e7c27a73 5933read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5934{
e7c27a73 5935 struct objfile *objfile = cu->objfile;
52f0bd74 5936 struct context_stack *new;
c906108c
SS
5937 CORE_ADDR lowpc;
5938 CORE_ADDR highpc;
5939 struct die_info *child_die;
edb3359d 5940 struct attribute *attr, *call_line, *call_file;
c906108c 5941 char *name;
e142c38c 5942 CORE_ADDR baseaddr;
801e3a5b 5943 struct block *block;
edb3359d 5944 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5945 VEC (symbolp) *template_args = NULL;
5946 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5947
5948 if (inlined_func)
5949 {
5950 /* If we do not have call site information, we can't show the
5951 caller of this inlined function. That's too confusing, so
5952 only use the scope for local variables. */
5953 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5954 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5955 if (call_line == NULL || call_file == NULL)
5956 {
5957 read_lexical_block_scope (die, cu);
5958 return;
5959 }
5960 }
c906108c 5961
e142c38c
DJ
5962 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5963
94af9270 5964 name = dwarf2_name (die, cu);
c906108c 5965
e8d05480
JB
5966 /* Ignore functions with missing or empty names. These are actually
5967 illegal according to the DWARF standard. */
5968 if (name == NULL)
5969 {
5970 complaint (&symfile_complaints,
5971 _("missing name for subprogram DIE at %d"), die->offset);
5972 return;
5973 }
5974
5975 /* Ignore functions with missing or invalid low and high pc attributes. */
5976 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5977 {
ae4d0c03
PM
5978 attr = dwarf2_attr (die, DW_AT_external, cu);
5979 if (!attr || !DW_UNSND (attr))
5980 complaint (&symfile_complaints,
3e43a32a
MS
5981 _("cannot get low and high bounds "
5982 "for subprogram DIE at %d"),
ae4d0c03 5983 die->offset);
e8d05480
JB
5984 return;
5985 }
c906108c
SS
5986
5987 lowpc += baseaddr;
5988 highpc += baseaddr;
5989
34eaf542
TT
5990 /* If we have any template arguments, then we must allocate a
5991 different sort of symbol. */
5992 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5993 {
5994 if (child_die->tag == DW_TAG_template_type_param
5995 || child_die->tag == DW_TAG_template_value_param)
5996 {
5997 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5998 struct template_symbol);
5999 templ_func->base.is_cplus_template_function = 1;
6000 break;
6001 }
6002 }
6003
c906108c 6004 new = push_context (0, lowpc);
34eaf542
TT
6005 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6006 (struct symbol *) templ_func);
4c2df51b 6007
4cecd739
DJ
6008 /* If there is a location expression for DW_AT_frame_base, record
6009 it. */
e142c38c 6010 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6011 if (attr)
c034e007
AC
6012 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6013 expression is being recorded directly in the function's symbol
6014 and not in a separate frame-base object. I guess this hack is
6015 to avoid adding some sort of frame-base adjunct/annex to the
6016 function's symbol :-(. The problem with doing this is that it
6017 results in a function symbol with a location expression that
6018 has nothing to do with the location of the function, ouch! The
6019 relationship should be: a function's symbol has-a frame base; a
6020 frame-base has-a location expression. */
e7c27a73 6021 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6022
e142c38c 6023 cu->list_in_scope = &local_symbols;
c906108c 6024
639d11d3 6025 if (die->child != NULL)
c906108c 6026 {
639d11d3 6027 child_die = die->child;
c906108c
SS
6028 while (child_die && child_die->tag)
6029 {
34eaf542
TT
6030 if (child_die->tag == DW_TAG_template_type_param
6031 || child_die->tag == DW_TAG_template_value_param)
6032 {
6033 struct symbol *arg = new_symbol (child_die, NULL, cu);
6034
f1078f66
DJ
6035 if (arg != NULL)
6036 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6037 }
6038 else
6039 process_die (child_die, cu);
c906108c
SS
6040 child_die = sibling_die (child_die);
6041 }
6042 }
6043
d389af10
JK
6044 inherit_abstract_dies (die, cu);
6045
4a811a97
UW
6046 /* If we have a DW_AT_specification, we might need to import using
6047 directives from the context of the specification DIE. See the
6048 comment in determine_prefix. */
6049 if (cu->language == language_cplus
6050 && dwarf2_attr (die, DW_AT_specification, cu))
6051 {
6052 struct dwarf2_cu *spec_cu = cu;
6053 struct die_info *spec_die = die_specification (die, &spec_cu);
6054
6055 while (spec_die)
6056 {
6057 child_die = spec_die->child;
6058 while (child_die && child_die->tag)
6059 {
6060 if (child_die->tag == DW_TAG_imported_module)
6061 process_die (child_die, spec_cu);
6062 child_die = sibling_die (child_die);
6063 }
6064
6065 /* In some cases, GCC generates specification DIEs that
6066 themselves contain DW_AT_specification attributes. */
6067 spec_die = die_specification (spec_die, &spec_cu);
6068 }
6069 }
6070
c906108c
SS
6071 new = pop_context ();
6072 /* Make a block for the local symbols within. */
801e3a5b
JB
6073 block = finish_block (new->name, &local_symbols, new->old_blocks,
6074 lowpc, highpc, objfile);
6075
df8a16a1 6076 /* For C++, set the block's scope. */
f55ee35c 6077 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6078 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6079 determine_prefix (die, cu),
df8a16a1
DJ
6080 processing_has_namespace_info);
6081
801e3a5b
JB
6082 /* If we have address ranges, record them. */
6083 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6084
34eaf542
TT
6085 /* Attach template arguments to function. */
6086 if (! VEC_empty (symbolp, template_args))
6087 {
6088 gdb_assert (templ_func != NULL);
6089
6090 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6091 templ_func->template_arguments
6092 = obstack_alloc (&objfile->objfile_obstack,
6093 (templ_func->n_template_arguments
6094 * sizeof (struct symbol *)));
6095 memcpy (templ_func->template_arguments,
6096 VEC_address (symbolp, template_args),
6097 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6098 VEC_free (symbolp, template_args);
6099 }
6100
208d8187
JB
6101 /* In C++, we can have functions nested inside functions (e.g., when
6102 a function declares a class that has methods). This means that
6103 when we finish processing a function scope, we may need to go
6104 back to building a containing block's symbol lists. */
6105 local_symbols = new->locals;
6106 param_symbols = new->params;
27aa8d6a 6107 using_directives = new->using_directives;
208d8187 6108
921e78cf
JB
6109 /* If we've finished processing a top-level function, subsequent
6110 symbols go in the file symbol list. */
6111 if (outermost_context_p ())
e142c38c 6112 cu->list_in_scope = &file_symbols;
c906108c
SS
6113}
6114
6115/* Process all the DIES contained within a lexical block scope. Start
6116 a new scope, process the dies, and then close the scope. */
6117
6118static void
e7c27a73 6119read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6120{
e7c27a73 6121 struct objfile *objfile = cu->objfile;
52f0bd74 6122 struct context_stack *new;
c906108c
SS
6123 CORE_ADDR lowpc, highpc;
6124 struct die_info *child_die;
e142c38c
DJ
6125 CORE_ADDR baseaddr;
6126
6127 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6128
6129 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6130 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6131 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6132 be nasty. Might be easier to properly extend generic blocks to
af34e669 6133 describe ranges. */
d85a05f0 6134 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6135 return;
6136 lowpc += baseaddr;
6137 highpc += baseaddr;
6138
6139 push_context (0, lowpc);
639d11d3 6140 if (die->child != NULL)
c906108c 6141 {
639d11d3 6142 child_die = die->child;
c906108c
SS
6143 while (child_die && child_die->tag)
6144 {
e7c27a73 6145 process_die (child_die, cu);
c906108c
SS
6146 child_die = sibling_die (child_die);
6147 }
6148 }
6149 new = pop_context ();
6150
8540c487 6151 if (local_symbols != NULL || using_directives != NULL)
c906108c 6152 {
801e3a5b
JB
6153 struct block *block
6154 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6155 highpc, objfile);
6156
6157 /* Note that recording ranges after traversing children, as we
6158 do here, means that recording a parent's ranges entails
6159 walking across all its children's ranges as they appear in
6160 the address map, which is quadratic behavior.
6161
6162 It would be nicer to record the parent's ranges before
6163 traversing its children, simply overriding whatever you find
6164 there. But since we don't even decide whether to create a
6165 block until after we've traversed its children, that's hard
6166 to do. */
6167 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6168 }
6169 local_symbols = new->locals;
27aa8d6a 6170 using_directives = new->using_directives;
c906108c
SS
6171}
6172
96408a79
SA
6173/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6174
6175static void
6176read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6177{
6178 struct objfile *objfile = cu->objfile;
6179 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6180 CORE_ADDR pc, baseaddr;
6181 struct attribute *attr;
6182 struct call_site *call_site, call_site_local;
6183 void **slot;
6184 int nparams;
6185 struct die_info *child_die;
6186
6187 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6188
6189 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6190 if (!attr)
6191 {
6192 complaint (&symfile_complaints,
6193 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6194 "DIE 0x%x [in module %s]"),
bb5ed363 6195 die->offset, objfile->name);
96408a79
SA
6196 return;
6197 }
6198 pc = DW_ADDR (attr) + baseaddr;
6199
6200 if (cu->call_site_htab == NULL)
6201 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6202 NULL, &objfile->objfile_obstack,
6203 hashtab_obstack_allocate, NULL);
6204 call_site_local.pc = pc;
6205 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6206 if (*slot != NULL)
6207 {
6208 complaint (&symfile_complaints,
6209 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6210 "DIE 0x%x [in module %s]"),
bb5ed363 6211 paddress (gdbarch, pc), die->offset, objfile->name);
96408a79
SA
6212 return;
6213 }
6214
6215 /* Count parameters at the caller. */
6216
6217 nparams = 0;
6218 for (child_die = die->child; child_die && child_die->tag;
6219 child_die = sibling_die (child_die))
6220 {
6221 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6222 {
6223 complaint (&symfile_complaints,
6224 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6225 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6226 child_die->tag, child_die->offset, objfile->name);
96408a79
SA
6227 continue;
6228 }
6229
6230 nparams++;
6231 }
6232
6233 call_site = obstack_alloc (&objfile->objfile_obstack,
6234 (sizeof (*call_site)
6235 + (sizeof (*call_site->parameter)
6236 * (nparams - 1))));
6237 *slot = call_site;
6238 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6239 call_site->pc = pc;
6240
6241 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6242 {
6243 struct die_info *func_die;
6244
6245 /* Skip also over DW_TAG_inlined_subroutine. */
6246 for (func_die = die->parent;
6247 func_die && func_die->tag != DW_TAG_subprogram
6248 && func_die->tag != DW_TAG_subroutine_type;
6249 func_die = func_die->parent);
6250
6251 /* DW_AT_GNU_all_call_sites is a superset
6252 of DW_AT_GNU_all_tail_call_sites. */
6253 if (func_die
6254 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6255 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6256 {
6257 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6258 not complete. But keep CALL_SITE for look ups via call_site_htab,
6259 both the initial caller containing the real return address PC and
6260 the final callee containing the current PC of a chain of tail
6261 calls do not need to have the tail call list complete. But any
6262 function candidate for a virtual tail call frame searched via
6263 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6264 determined unambiguously. */
6265 }
6266 else
6267 {
6268 struct type *func_type = NULL;
6269
6270 if (func_die)
6271 func_type = get_die_type (func_die, cu);
6272 if (func_type != NULL)
6273 {
6274 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6275
6276 /* Enlist this call site to the function. */
6277 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6278 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6279 }
6280 else
6281 complaint (&symfile_complaints,
6282 _("Cannot find function owning DW_TAG_GNU_call_site "
6283 "DIE 0x%x [in module %s]"),
bb5ed363 6284 die->offset, objfile->name);
96408a79
SA
6285 }
6286 }
6287
6288 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6289 if (attr == NULL)
6290 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6291 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6292 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6293 /* Keep NULL DWARF_BLOCK. */;
6294 else if (attr_form_is_block (attr))
6295 {
6296 struct dwarf2_locexpr_baton *dlbaton;
6297
6298 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6299 dlbaton->data = DW_BLOCK (attr)->data;
6300 dlbaton->size = DW_BLOCK (attr)->size;
6301 dlbaton->per_cu = cu->per_cu;
6302
6303 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6304 }
6305 else if (is_ref_attr (attr))
6306 {
96408a79
SA
6307 struct dwarf2_cu *target_cu = cu;
6308 struct die_info *target_die;
6309
6310 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6311 gdb_assert (target_cu->objfile == objfile);
6312 if (die_is_declaration (target_die, target_cu))
6313 {
6314 const char *target_physname;
6315
6316 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6317 if (target_physname == NULL)
6318 complaint (&symfile_complaints,
6319 _("DW_AT_GNU_call_site_target target DIE has invalid "
6320 "physname, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6321 die->offset, objfile->name);
96408a79
SA
6322 else
6323 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6324 }
6325 else
6326 {
6327 CORE_ADDR lowpc;
6328
6329 /* DW_AT_entry_pc should be preferred. */
6330 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6331 complaint (&symfile_complaints,
6332 _("DW_AT_GNU_call_site_target target DIE has invalid "
6333 "low pc, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6334 die->offset, objfile->name);
96408a79
SA
6335 else
6336 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6337 }
6338 }
6339 else
6340 complaint (&symfile_complaints,
6341 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6342 "block nor reference, for DIE 0x%x [in module %s]"),
bb5ed363 6343 die->offset, objfile->name);
96408a79
SA
6344
6345 call_site->per_cu = cu->per_cu;
6346
6347 for (child_die = die->child;
6348 child_die && child_die->tag;
6349 child_die = sibling_die (child_die))
6350 {
6351 struct dwarf2_locexpr_baton *dlbaton;
6352 struct call_site_parameter *parameter;
6353
6354 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6355 {
6356 /* Already printed the complaint above. */
6357 continue;
6358 }
6359
6360 gdb_assert (call_site->parameter_count < nparams);
6361 parameter = &call_site->parameter[call_site->parameter_count];
6362
6363 /* DW_AT_location specifies the register number. Value of the data
6364 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6365
6366 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6367 if (!attr || !attr_form_is_block (attr))
6368 {
6369 complaint (&symfile_complaints,
6370 _("No DW_FORM_block* DW_AT_location for "
6371 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6372 child_die->offset, objfile->name);
96408a79
SA
6373 continue;
6374 }
6375 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6376 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6377 if (parameter->dwarf_reg == -1
6378 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6379 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6380 &parameter->fb_offset))
6381 {
6382 complaint (&symfile_complaints,
6383 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6384 "for DW_FORM_block* DW_AT_location for "
6385 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6386 child_die->offset, objfile->name);
96408a79
SA
6387 continue;
6388 }
6389
6390 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6391 if (!attr_form_is_block (attr))
6392 {
6393 complaint (&symfile_complaints,
6394 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6395 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6396 child_die->offset, objfile->name);
96408a79
SA
6397 continue;
6398 }
6399 parameter->value = DW_BLOCK (attr)->data;
6400 parameter->value_size = DW_BLOCK (attr)->size;
6401
6402 /* Parameters are not pre-cleared by memset above. */
6403 parameter->data_value = NULL;
6404 parameter->data_value_size = 0;
6405 call_site->parameter_count++;
6406
6407 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6408 if (attr)
6409 {
6410 if (!attr_form_is_block (attr))
6411 complaint (&symfile_complaints,
6412 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6413 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6414 child_die->offset, objfile->name);
96408a79
SA
6415 else
6416 {
6417 parameter->data_value = DW_BLOCK (attr)->data;
6418 parameter->data_value_size = DW_BLOCK (attr)->size;
6419 }
6420 }
6421 }
6422}
6423
43039443 6424/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6425 Return 1 if the attributes are present and valid, otherwise, return 0.
6426 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6427
6428static int
6429dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6430 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6431 struct partial_symtab *ranges_pst)
43039443
JK
6432{
6433 struct objfile *objfile = cu->objfile;
6434 struct comp_unit_head *cu_header = &cu->header;
6435 bfd *obfd = objfile->obfd;
6436 unsigned int addr_size = cu_header->addr_size;
6437 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6438 /* Base address selection entry. */
6439 CORE_ADDR base;
6440 int found_base;
6441 unsigned int dummy;
6442 gdb_byte *buffer;
6443 CORE_ADDR marker;
6444 int low_set;
6445 CORE_ADDR low = 0;
6446 CORE_ADDR high = 0;
ff013f42 6447 CORE_ADDR baseaddr;
43039443 6448
d00adf39
DE
6449 found_base = cu->base_known;
6450 base = cu->base_address;
43039443 6451
be391dca 6452 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6453 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6454 {
6455 complaint (&symfile_complaints,
6456 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6457 offset);
6458 return 0;
6459 }
dce234bc 6460 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6461
6462 /* Read in the largest possible address. */
6463 marker = read_address (obfd, buffer, cu, &dummy);
6464 if ((marker & mask) == mask)
6465 {
6466 /* If we found the largest possible address, then
6467 read the base address. */
6468 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6469 buffer += 2 * addr_size;
6470 offset += 2 * addr_size;
6471 found_base = 1;
6472 }
6473
6474 low_set = 0;
6475
e7030f15 6476 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6477
43039443
JK
6478 while (1)
6479 {
6480 CORE_ADDR range_beginning, range_end;
6481
6482 range_beginning = read_address (obfd, buffer, cu, &dummy);
6483 buffer += addr_size;
6484 range_end = read_address (obfd, buffer, cu, &dummy);
6485 buffer += addr_size;
6486 offset += 2 * addr_size;
6487
6488 /* An end of list marker is a pair of zero addresses. */
6489 if (range_beginning == 0 && range_end == 0)
6490 /* Found the end of list entry. */
6491 break;
6492
6493 /* Each base address selection entry is a pair of 2 values.
6494 The first is the largest possible address, the second is
6495 the base address. Check for a base address here. */
6496 if ((range_beginning & mask) == mask)
6497 {
6498 /* If we found the largest possible address, then
6499 read the base address. */
6500 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6501 found_base = 1;
6502 continue;
6503 }
6504
6505 if (!found_base)
6506 {
6507 /* We have no valid base address for the ranges
6508 data. */
6509 complaint (&symfile_complaints,
6510 _("Invalid .debug_ranges data (no base address)"));
6511 return 0;
6512 }
6513
9277c30c
UW
6514 if (range_beginning > range_end)
6515 {
6516 /* Inverted range entries are invalid. */
6517 complaint (&symfile_complaints,
6518 _("Invalid .debug_ranges data (inverted range)"));
6519 return 0;
6520 }
6521
6522 /* Empty range entries have no effect. */
6523 if (range_beginning == range_end)
6524 continue;
6525
43039443
JK
6526 range_beginning += base;
6527 range_end += base;
6528
9277c30c 6529 if (ranges_pst != NULL)
ff013f42 6530 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6531 range_beginning + baseaddr,
6532 range_end - 1 + baseaddr,
ff013f42
JK
6533 ranges_pst);
6534
43039443
JK
6535 /* FIXME: This is recording everything as a low-high
6536 segment of consecutive addresses. We should have a
6537 data structure for discontiguous block ranges
6538 instead. */
6539 if (! low_set)
6540 {
6541 low = range_beginning;
6542 high = range_end;
6543 low_set = 1;
6544 }
6545 else
6546 {
6547 if (range_beginning < low)
6548 low = range_beginning;
6549 if (range_end > high)
6550 high = range_end;
6551 }
6552 }
6553
6554 if (! low_set)
6555 /* If the first entry is an end-of-list marker, the range
6556 describes an empty scope, i.e. no instructions. */
6557 return 0;
6558
6559 if (low_return)
6560 *low_return = low;
6561 if (high_return)
6562 *high_return = high;
6563 return 1;
6564}
6565
af34e669
DJ
6566/* Get low and high pc attributes from a die. Return 1 if the attributes
6567 are present and valid, otherwise, return 0. Return -1 if the range is
6568 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6569static int
af34e669 6570dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6571 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6572 struct partial_symtab *pst)
c906108c
SS
6573{
6574 struct attribute *attr;
af34e669
DJ
6575 CORE_ADDR low = 0;
6576 CORE_ADDR high = 0;
6577 int ret = 0;
c906108c 6578
e142c38c 6579 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6580 if (attr)
af34e669
DJ
6581 {
6582 high = DW_ADDR (attr);
e142c38c 6583 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6584 if (attr)
6585 low = DW_ADDR (attr);
6586 else
6587 /* Found high w/o low attribute. */
6588 return 0;
6589
6590 /* Found consecutive range of addresses. */
6591 ret = 1;
6592 }
c906108c 6593 else
af34e669 6594 {
e142c38c 6595 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6596 if (attr != NULL)
6597 {
af34e669 6598 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6599 .debug_ranges section. */
d85a05f0 6600 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6601 return 0;
43039443 6602 /* Found discontinuous range of addresses. */
af34e669
DJ
6603 ret = -1;
6604 }
6605 }
c906108c 6606
9373cf26
JK
6607 /* read_partial_die has also the strict LOW < HIGH requirement. */
6608 if (high <= low)
c906108c
SS
6609 return 0;
6610
6611 /* When using the GNU linker, .gnu.linkonce. sections are used to
6612 eliminate duplicate copies of functions and vtables and such.
6613 The linker will arbitrarily choose one and discard the others.
6614 The AT_*_pc values for such functions refer to local labels in
6615 these sections. If the section from that file was discarded, the
6616 labels are not in the output, so the relocs get a value of 0.
6617 If this is a discarded function, mark the pc bounds as invalid,
6618 so that GDB will ignore it. */
72dca2f5 6619 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6620 return 0;
6621
6622 *lowpc = low;
96408a79
SA
6623 if (highpc)
6624 *highpc = high;
af34e669 6625 return ret;
c906108c
SS
6626}
6627
b084d499
JB
6628/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6629 its low and high PC addresses. Do nothing if these addresses could not
6630 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6631 and HIGHPC to the high address if greater than HIGHPC. */
6632
6633static void
6634dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6635 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6636 struct dwarf2_cu *cu)
6637{
6638 CORE_ADDR low, high;
6639 struct die_info *child = die->child;
6640
d85a05f0 6641 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6642 {
6643 *lowpc = min (*lowpc, low);
6644 *highpc = max (*highpc, high);
6645 }
6646
6647 /* If the language does not allow nested subprograms (either inside
6648 subprograms or lexical blocks), we're done. */
6649 if (cu->language != language_ada)
6650 return;
6e70227d 6651
b084d499
JB
6652 /* Check all the children of the given DIE. If it contains nested
6653 subprograms, then check their pc bounds. Likewise, we need to
6654 check lexical blocks as well, as they may also contain subprogram
6655 definitions. */
6656 while (child && child->tag)
6657 {
6658 if (child->tag == DW_TAG_subprogram
6659 || child->tag == DW_TAG_lexical_block)
6660 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6661 child = sibling_die (child);
6662 }
6663}
6664
fae299cd
DC
6665/* Get the low and high pc's represented by the scope DIE, and store
6666 them in *LOWPC and *HIGHPC. If the correct values can't be
6667 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6668
6669static void
6670get_scope_pc_bounds (struct die_info *die,
6671 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6672 struct dwarf2_cu *cu)
6673{
6674 CORE_ADDR best_low = (CORE_ADDR) -1;
6675 CORE_ADDR best_high = (CORE_ADDR) 0;
6676 CORE_ADDR current_low, current_high;
6677
d85a05f0 6678 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6679 {
6680 best_low = current_low;
6681 best_high = current_high;
6682 }
6683 else
6684 {
6685 struct die_info *child = die->child;
6686
6687 while (child && child->tag)
6688 {
6689 switch (child->tag) {
6690 case DW_TAG_subprogram:
b084d499 6691 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6692 break;
6693 case DW_TAG_namespace:
f55ee35c 6694 case DW_TAG_module:
fae299cd
DC
6695 /* FIXME: carlton/2004-01-16: Should we do this for
6696 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6697 that current GCC's always emit the DIEs corresponding
6698 to definitions of methods of classes as children of a
6699 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6700 the DIEs giving the declarations, which could be
6701 anywhere). But I don't see any reason why the
6702 standards says that they have to be there. */
6703 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6704
6705 if (current_low != ((CORE_ADDR) -1))
6706 {
6707 best_low = min (best_low, current_low);
6708 best_high = max (best_high, current_high);
6709 }
6710 break;
6711 default:
0963b4bd 6712 /* Ignore. */
fae299cd
DC
6713 break;
6714 }
6715
6716 child = sibling_die (child);
6717 }
6718 }
6719
6720 *lowpc = best_low;
6721 *highpc = best_high;
6722}
6723
801e3a5b
JB
6724/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6725 in DIE. */
6726static void
6727dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6728 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6729{
bb5ed363 6730 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6731 struct attribute *attr;
6732
6733 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6734 if (attr)
6735 {
6736 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6737
801e3a5b
JB
6738 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6739 if (attr)
6740 {
6741 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6742
801e3a5b
JB
6743 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6744 }
6745 }
6746
6747 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6748 if (attr)
6749 {
bb5ed363 6750 bfd *obfd = objfile->obfd;
801e3a5b
JB
6751
6752 /* The value of the DW_AT_ranges attribute is the offset of the
6753 address range list in the .debug_ranges section. */
6754 unsigned long offset = DW_UNSND (attr);
dce234bc 6755 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6756
6757 /* For some target architectures, but not others, the
6758 read_address function sign-extends the addresses it returns.
6759 To recognize base address selection entries, we need a
6760 mask. */
6761 unsigned int addr_size = cu->header.addr_size;
6762 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6763
6764 /* The base address, to which the next pair is relative. Note
6765 that this 'base' is a DWARF concept: most entries in a range
6766 list are relative, to reduce the number of relocs against the
6767 debugging information. This is separate from this function's
6768 'baseaddr' argument, which GDB uses to relocate debugging
6769 information from a shared library based on the address at
6770 which the library was loaded. */
d00adf39
DE
6771 CORE_ADDR base = cu->base_address;
6772 int base_known = cu->base_known;
801e3a5b 6773
be391dca 6774 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6775 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6776 {
6777 complaint (&symfile_complaints,
6778 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6779 offset);
6780 return;
6781 }
6782
6783 for (;;)
6784 {
6785 unsigned int bytes_read;
6786 CORE_ADDR start, end;
6787
6788 start = read_address (obfd, buffer, cu, &bytes_read);
6789 buffer += bytes_read;
6790 end = read_address (obfd, buffer, cu, &bytes_read);
6791 buffer += bytes_read;
6792
6793 /* Did we find the end of the range list? */
6794 if (start == 0 && end == 0)
6795 break;
6796
6797 /* Did we find a base address selection entry? */
6798 else if ((start & base_select_mask) == base_select_mask)
6799 {
6800 base = end;
6801 base_known = 1;
6802 }
6803
6804 /* We found an ordinary address range. */
6805 else
6806 {
6807 if (!base_known)
6808 {
6809 complaint (&symfile_complaints,
3e43a32a
MS
6810 _("Invalid .debug_ranges data "
6811 "(no base address)"));
801e3a5b
JB
6812 return;
6813 }
6814
9277c30c
UW
6815 if (start > end)
6816 {
6817 /* Inverted range entries are invalid. */
6818 complaint (&symfile_complaints,
6819 _("Invalid .debug_ranges data "
6820 "(inverted range)"));
6821 return;
6822 }
6823
6824 /* Empty range entries have no effect. */
6825 if (start == end)
6826 continue;
6827
6e70227d
DE
6828 record_block_range (block,
6829 baseaddr + base + start,
801e3a5b
JB
6830 baseaddr + base + end - 1);
6831 }
6832 }
6833 }
6834}
6835
60d5a603
JK
6836/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6837 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6838 during 4.6.0 experimental. */
6839
6840static int
6841producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6842{
6843 const char *cs;
6844 int major, minor, release;
6845
6846 if (cu->producer == NULL)
6847 {
6848 /* For unknown compilers expect their behavior is DWARF version
6849 compliant.
6850
6851 GCC started to support .debug_types sections by -gdwarf-4 since
6852 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6853 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6854 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6855 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6856
6857 return 0;
6858 }
6859
6860 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6861
6862 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6863 {
6864 /* For non-GCC compilers expect their behavior is DWARF version
6865 compliant. */
6866
6867 return 0;
6868 }
6869 cs = &cu->producer[strlen ("GNU ")];
6870 while (*cs && !isdigit (*cs))
6871 cs++;
6872 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6873 {
6874 /* Not recognized as GCC. */
6875
6876 return 0;
6877 }
6878
6879 return major < 4 || (major == 4 && minor < 6);
6880}
6881
6882/* Return the default accessibility type if it is not overriden by
6883 DW_AT_accessibility. */
6884
6885static enum dwarf_access_attribute
6886dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6887{
6888 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6889 {
6890 /* The default DWARF 2 accessibility for members is public, the default
6891 accessibility for inheritance is private. */
6892
6893 if (die->tag != DW_TAG_inheritance)
6894 return DW_ACCESS_public;
6895 else
6896 return DW_ACCESS_private;
6897 }
6898 else
6899 {
6900 /* DWARF 3+ defines the default accessibility a different way. The same
6901 rules apply now for DW_TAG_inheritance as for the members and it only
6902 depends on the container kind. */
6903
6904 if (die->parent->tag == DW_TAG_class_type)
6905 return DW_ACCESS_private;
6906 else
6907 return DW_ACCESS_public;
6908 }
6909}
6910
74ac6d43
TT
6911/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6912 offset. If the attribute was not found return 0, otherwise return
6913 1. If it was found but could not properly be handled, set *OFFSET
6914 to 0. */
6915
6916static int
6917handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6918 LONGEST *offset)
6919{
6920 struct attribute *attr;
6921
6922 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6923 if (attr != NULL)
6924 {
6925 *offset = 0;
6926
6927 /* Note that we do not check for a section offset first here.
6928 This is because DW_AT_data_member_location is new in DWARF 4,
6929 so if we see it, we can assume that a constant form is really
6930 a constant and not a section offset. */
6931 if (attr_form_is_constant (attr))
6932 *offset = dwarf2_get_attr_constant_value (attr, 0);
6933 else if (attr_form_is_section_offset (attr))
6934 dwarf2_complex_location_expr_complaint ();
6935 else if (attr_form_is_block (attr))
6936 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6937 else
6938 dwarf2_complex_location_expr_complaint ();
6939
6940 return 1;
6941 }
6942
6943 return 0;
6944}
6945
c906108c
SS
6946/* Add an aggregate field to the field list. */
6947
6948static void
107d2387 6949dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6950 struct dwarf2_cu *cu)
6e70227d 6951{
e7c27a73 6952 struct objfile *objfile = cu->objfile;
5e2b427d 6953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6954 struct nextfield *new_field;
6955 struct attribute *attr;
6956 struct field *fp;
6957 char *fieldname = "";
6958
6959 /* Allocate a new field list entry and link it in. */
6960 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6961 make_cleanup (xfree, new_field);
c906108c 6962 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6963
6964 if (die->tag == DW_TAG_inheritance)
6965 {
6966 new_field->next = fip->baseclasses;
6967 fip->baseclasses = new_field;
6968 }
6969 else
6970 {
6971 new_field->next = fip->fields;
6972 fip->fields = new_field;
6973 }
c906108c
SS
6974 fip->nfields++;
6975
e142c38c 6976 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6977 if (attr)
6978 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6979 else
6980 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6981 if (new_field->accessibility != DW_ACCESS_public)
6982 fip->non_public_fields = 1;
60d5a603 6983
e142c38c 6984 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6985 if (attr)
6986 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6987 else
6988 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6989
6990 fp = &new_field->field;
a9a9bd0f 6991
e142c38c 6992 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6993 {
74ac6d43
TT
6994 LONGEST offset;
6995
a9a9bd0f 6996 /* Data member other than a C++ static data member. */
6e70227d 6997
c906108c 6998 /* Get type of field. */
e7c27a73 6999 fp->type = die_type (die, cu);
c906108c 7000
d6a843b5 7001 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7002
c906108c 7003 /* Get bit size of field (zero if none). */
e142c38c 7004 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7005 if (attr)
7006 {
7007 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7008 }
7009 else
7010 {
7011 FIELD_BITSIZE (*fp) = 0;
7012 }
7013
7014 /* Get bit offset of field. */
74ac6d43
TT
7015 if (handle_data_member_location (die, cu, &offset))
7016 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7017 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7018 if (attr)
7019 {
5e2b427d 7020 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7021 {
7022 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7023 additional bit offset from the MSB of the containing
7024 anonymous object to the MSB of the field. We don't
7025 have to do anything special since we don't need to
7026 know the size of the anonymous object. */
c906108c
SS
7027 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7028 }
7029 else
7030 {
7031 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7032 MSB of the anonymous object, subtract off the number of
7033 bits from the MSB of the field to the MSB of the
7034 object, and then subtract off the number of bits of
7035 the field itself. The result is the bit offset of
7036 the LSB of the field. */
c906108c
SS
7037 int anonymous_size;
7038 int bit_offset = DW_UNSND (attr);
7039
e142c38c 7040 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7041 if (attr)
7042 {
7043 /* The size of the anonymous object containing
7044 the bit field is explicit, so use the
7045 indicated size (in bytes). */
7046 anonymous_size = DW_UNSND (attr);
7047 }
7048 else
7049 {
7050 /* The size of the anonymous object containing
7051 the bit field must be inferred from the type
7052 attribute of the data member containing the
7053 bit field. */
7054 anonymous_size = TYPE_LENGTH (fp->type);
7055 }
7056 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7057 - bit_offset - FIELD_BITSIZE (*fp);
7058 }
7059 }
7060
7061 /* Get name of field. */
39cbfefa
DJ
7062 fieldname = dwarf2_name (die, cu);
7063 if (fieldname == NULL)
7064 fieldname = "";
d8151005
DJ
7065
7066 /* The name is already allocated along with this objfile, so we don't
7067 need to duplicate it for the type. */
7068 fp->name = fieldname;
c906108c
SS
7069
7070 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7071 pointer or virtual base class pointer) to private. */
e142c38c 7072 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7073 {
d48cc9dd 7074 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7075 new_field->accessibility = DW_ACCESS_private;
7076 fip->non_public_fields = 1;
7077 }
7078 }
a9a9bd0f 7079 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7080 {
a9a9bd0f
DC
7081 /* C++ static member. */
7082
7083 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7084 is a declaration, but all versions of G++ as of this writing
7085 (so through at least 3.2.1) incorrectly generate
7086 DW_TAG_variable tags. */
6e70227d 7087
ff355380 7088 const char *physname;
c906108c 7089
a9a9bd0f 7090 /* Get name of field. */
39cbfefa
DJ
7091 fieldname = dwarf2_name (die, cu);
7092 if (fieldname == NULL)
c906108c
SS
7093 return;
7094
254e6b9e 7095 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7096 if (attr
7097 /* Only create a symbol if this is an external value.
7098 new_symbol checks this and puts the value in the global symbol
7099 table, which we want. If it is not external, new_symbol
7100 will try to put the value in cu->list_in_scope which is wrong. */
7101 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7102 {
7103 /* A static const member, not much different than an enum as far as
7104 we're concerned, except that we can support more types. */
7105 new_symbol (die, NULL, cu);
7106 }
7107
2df3850c 7108 /* Get physical name. */
ff355380 7109 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7110
d8151005
DJ
7111 /* The name is already allocated along with this objfile, so we don't
7112 need to duplicate it for the type. */
7113 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7114 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7115 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7116 }
7117 else if (die->tag == DW_TAG_inheritance)
7118 {
74ac6d43 7119 LONGEST offset;
d4b96c9a 7120
74ac6d43
TT
7121 /* C++ base class field. */
7122 if (handle_data_member_location (die, cu, &offset))
7123 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7124 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7125 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7126 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7127 fip->nbaseclasses++;
7128 }
7129}
7130
98751a41
JK
7131/* Add a typedef defined in the scope of the FIP's class. */
7132
7133static void
7134dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7135 struct dwarf2_cu *cu)
6e70227d 7136{
98751a41 7137 struct objfile *objfile = cu->objfile;
98751a41
JK
7138 struct typedef_field_list *new_field;
7139 struct attribute *attr;
7140 struct typedef_field *fp;
7141 char *fieldname = "";
7142
7143 /* Allocate a new field list entry and link it in. */
7144 new_field = xzalloc (sizeof (*new_field));
7145 make_cleanup (xfree, new_field);
7146
7147 gdb_assert (die->tag == DW_TAG_typedef);
7148
7149 fp = &new_field->field;
7150
7151 /* Get name of field. */
7152 fp->name = dwarf2_name (die, cu);
7153 if (fp->name == NULL)
7154 return;
7155
7156 fp->type = read_type_die (die, cu);
7157
7158 new_field->next = fip->typedef_field_list;
7159 fip->typedef_field_list = new_field;
7160 fip->typedef_field_list_count++;
7161}
7162
c906108c
SS
7163/* Create the vector of fields, and attach it to the type. */
7164
7165static void
fba45db2 7166dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7167 struct dwarf2_cu *cu)
c906108c
SS
7168{
7169 int nfields = fip->nfields;
7170
7171 /* Record the field count, allocate space for the array of fields,
7172 and create blank accessibility bitfields if necessary. */
7173 TYPE_NFIELDS (type) = nfields;
7174 TYPE_FIELDS (type) = (struct field *)
7175 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7176 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7177
b4ba55a1 7178 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7179 {
7180 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7181
7182 TYPE_FIELD_PRIVATE_BITS (type) =
7183 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7184 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7185
7186 TYPE_FIELD_PROTECTED_BITS (type) =
7187 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7188 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7189
774b6a14
TT
7190 TYPE_FIELD_IGNORE_BITS (type) =
7191 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7192 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7193 }
7194
7195 /* If the type has baseclasses, allocate and clear a bit vector for
7196 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7197 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7198 {
7199 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7200 unsigned char *pointer;
c906108c
SS
7201
7202 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7203 pointer = TYPE_ALLOC (type, num_bytes);
7204 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7205 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7206 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7207 }
7208
3e43a32a
MS
7209 /* Copy the saved-up fields into the field vector. Start from the head of
7210 the list, adding to the tail of the field array, so that they end up in
7211 the same order in the array in which they were added to the list. */
c906108c
SS
7212 while (nfields-- > 0)
7213 {
7d0ccb61
DJ
7214 struct nextfield *fieldp;
7215
7216 if (fip->fields)
7217 {
7218 fieldp = fip->fields;
7219 fip->fields = fieldp->next;
7220 }
7221 else
7222 {
7223 fieldp = fip->baseclasses;
7224 fip->baseclasses = fieldp->next;
7225 }
7226
7227 TYPE_FIELD (type, nfields) = fieldp->field;
7228 switch (fieldp->accessibility)
c906108c 7229 {
c5aa993b 7230 case DW_ACCESS_private:
b4ba55a1
JB
7231 if (cu->language != language_ada)
7232 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7233 break;
c906108c 7234
c5aa993b 7235 case DW_ACCESS_protected:
b4ba55a1
JB
7236 if (cu->language != language_ada)
7237 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7238 break;
c906108c 7239
c5aa993b
JM
7240 case DW_ACCESS_public:
7241 break;
c906108c 7242
c5aa993b
JM
7243 default:
7244 /* Unknown accessibility. Complain and treat it as public. */
7245 {
e2e0b3e5 7246 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7247 fieldp->accessibility);
c5aa993b
JM
7248 }
7249 break;
c906108c
SS
7250 }
7251 if (nfields < fip->nbaseclasses)
7252 {
7d0ccb61 7253 switch (fieldp->virtuality)
c906108c 7254 {
c5aa993b
JM
7255 case DW_VIRTUALITY_virtual:
7256 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7257 if (cu->language == language_ada)
a73c6dcd 7258 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7259 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7260 break;
c906108c
SS
7261 }
7262 }
c906108c
SS
7263 }
7264}
7265
c906108c
SS
7266/* Add a member function to the proper fieldlist. */
7267
7268static void
107d2387 7269dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7270 struct type *type, struct dwarf2_cu *cu)
c906108c 7271{
e7c27a73 7272 struct objfile *objfile = cu->objfile;
c906108c
SS
7273 struct attribute *attr;
7274 struct fnfieldlist *flp;
7275 int i;
7276 struct fn_field *fnp;
7277 char *fieldname;
c906108c 7278 struct nextfnfield *new_fnfield;
f792889a 7279 struct type *this_type;
60d5a603 7280 enum dwarf_access_attribute accessibility;
c906108c 7281
b4ba55a1 7282 if (cu->language == language_ada)
a73c6dcd 7283 error (_("unexpected member function in Ada type"));
b4ba55a1 7284
2df3850c 7285 /* Get name of member function. */
39cbfefa
DJ
7286 fieldname = dwarf2_name (die, cu);
7287 if (fieldname == NULL)
2df3850c 7288 return;
c906108c 7289
c906108c
SS
7290 /* Look up member function name in fieldlist. */
7291 for (i = 0; i < fip->nfnfields; i++)
7292 {
27bfe10e 7293 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7294 break;
7295 }
7296
7297 /* Create new list element if necessary. */
7298 if (i < fip->nfnfields)
7299 flp = &fip->fnfieldlists[i];
7300 else
7301 {
7302 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7303 {
7304 fip->fnfieldlists = (struct fnfieldlist *)
7305 xrealloc (fip->fnfieldlists,
7306 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7307 * sizeof (struct fnfieldlist));
c906108c 7308 if (fip->nfnfields == 0)
c13c43fd 7309 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7310 }
7311 flp = &fip->fnfieldlists[fip->nfnfields];
7312 flp->name = fieldname;
7313 flp->length = 0;
7314 flp->head = NULL;
3da10d80 7315 i = fip->nfnfields++;
c906108c
SS
7316 }
7317
7318 /* Create a new member function field and chain it to the field list
0963b4bd 7319 entry. */
c906108c 7320 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7321 make_cleanup (xfree, new_fnfield);
c906108c
SS
7322 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7323 new_fnfield->next = flp->head;
7324 flp->head = new_fnfield;
7325 flp->length++;
7326
7327 /* Fill in the member function field info. */
7328 fnp = &new_fnfield->fnfield;
3da10d80
KS
7329
7330 /* Delay processing of the physname until later. */
7331 if (cu->language == language_cplus || cu->language == language_java)
7332 {
7333 add_to_method_list (type, i, flp->length - 1, fieldname,
7334 die, cu);
7335 }
7336 else
7337 {
1d06ead6 7338 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7339 fnp->physname = physname ? physname : "";
7340 }
7341
c906108c 7342 fnp->type = alloc_type (objfile);
f792889a
DJ
7343 this_type = read_type_die (die, cu);
7344 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7345 {
f792889a 7346 int nparams = TYPE_NFIELDS (this_type);
c906108c 7347
f792889a 7348 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7349 of the method itself (TYPE_CODE_METHOD). */
7350 smash_to_method_type (fnp->type, type,
f792889a
DJ
7351 TYPE_TARGET_TYPE (this_type),
7352 TYPE_FIELDS (this_type),
7353 TYPE_NFIELDS (this_type),
7354 TYPE_VARARGS (this_type));
c906108c
SS
7355
7356 /* Handle static member functions.
c5aa993b 7357 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7358 member functions. G++ helps GDB by marking the first
7359 parameter for non-static member functions (which is the this
7360 pointer) as artificial. We obtain this information from
7361 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7362 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7363 fnp->voffset = VOFFSET_STATIC;
7364 }
7365 else
e2e0b3e5 7366 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7367 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7368
7369 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7370 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7371 fnp->fcontext = die_containing_type (die, cu);
c906108c 7372
3e43a32a
MS
7373 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7374 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7375
7376 /* Get accessibility. */
e142c38c 7377 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7378 if (attr)
60d5a603
JK
7379 accessibility = DW_UNSND (attr);
7380 else
7381 accessibility = dwarf2_default_access_attribute (die, cu);
7382 switch (accessibility)
c906108c 7383 {
60d5a603
JK
7384 case DW_ACCESS_private:
7385 fnp->is_private = 1;
7386 break;
7387 case DW_ACCESS_protected:
7388 fnp->is_protected = 1;
7389 break;
c906108c
SS
7390 }
7391
b02dede2 7392 /* Check for artificial methods. */
e142c38c 7393 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7394 if (attr && DW_UNSND (attr) != 0)
7395 fnp->is_artificial = 1;
7396
0d564a31 7397 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7398 function. For older versions of GCC, this is an offset in the
7399 appropriate virtual table, as specified by DW_AT_containing_type.
7400 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7401 to the object address. */
7402
e142c38c 7403 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7404 if (attr)
8e19ed76 7405 {
aec5aa8b 7406 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7407 {
aec5aa8b
TT
7408 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7409 {
7410 /* Old-style GCC. */
7411 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7412 }
7413 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7414 || (DW_BLOCK (attr)->size > 1
7415 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7416 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7417 {
7418 struct dwarf_block blk;
7419 int offset;
7420
7421 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7422 ? 1 : 2);
7423 blk.size = DW_BLOCK (attr)->size - offset;
7424 blk.data = DW_BLOCK (attr)->data + offset;
7425 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7426 if ((fnp->voffset % cu->header.addr_size) != 0)
7427 dwarf2_complex_location_expr_complaint ();
7428 else
7429 fnp->voffset /= cu->header.addr_size;
7430 fnp->voffset += 2;
7431 }
7432 else
7433 dwarf2_complex_location_expr_complaint ();
7434
7435 if (!fnp->fcontext)
7436 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7437 }
3690dd37 7438 else if (attr_form_is_section_offset (attr))
8e19ed76 7439 {
4d3c2250 7440 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7441 }
7442 else
7443 {
4d3c2250
KB
7444 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7445 fieldname);
8e19ed76 7446 }
0d564a31 7447 }
d48cc9dd
DJ
7448 else
7449 {
7450 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7451 if (attr && DW_UNSND (attr))
7452 {
7453 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7454 complaint (&symfile_complaints,
3e43a32a
MS
7455 _("Member function \"%s\" (offset %d) is virtual "
7456 "but the vtable offset is not specified"),
d48cc9dd 7457 fieldname, die->offset);
9655fd1a 7458 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7459 TYPE_CPLUS_DYNAMIC (type) = 1;
7460 }
7461 }
c906108c
SS
7462}
7463
7464/* Create the vector of member function fields, and attach it to the type. */
7465
7466static void
fba45db2 7467dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7468 struct dwarf2_cu *cu)
c906108c
SS
7469{
7470 struct fnfieldlist *flp;
c906108c
SS
7471 int i;
7472
b4ba55a1 7473 if (cu->language == language_ada)
a73c6dcd 7474 error (_("unexpected member functions in Ada type"));
b4ba55a1 7475
c906108c
SS
7476 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7477 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7478 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7479
7480 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7481 {
7482 struct nextfnfield *nfp = flp->head;
7483 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7484 int k;
7485
7486 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7487 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7488 fn_flp->fn_fields = (struct fn_field *)
7489 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7490 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7491 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7492 }
7493
7494 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7495}
7496
1168df01
JB
7497/* Returns non-zero if NAME is the name of a vtable member in CU's
7498 language, zero otherwise. */
7499static int
7500is_vtable_name (const char *name, struct dwarf2_cu *cu)
7501{
7502 static const char vptr[] = "_vptr";
987504bb 7503 static const char vtable[] = "vtable";
1168df01 7504
987504bb
JJ
7505 /* Look for the C++ and Java forms of the vtable. */
7506 if ((cu->language == language_java
7507 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7508 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7509 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7510 return 1;
7511
7512 return 0;
7513}
7514
c0dd20ea 7515/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7516 functions, with the ABI-specified layout. If TYPE describes
7517 such a structure, smash it into a member function type.
61049d3b
DJ
7518
7519 GCC shouldn't do this; it should just output pointer to member DIEs.
7520 This is GCC PR debug/28767. */
c0dd20ea 7521
0b92b5bb
TT
7522static void
7523quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7524{
0b92b5bb 7525 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7526
7527 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7528 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7529 return;
c0dd20ea
DJ
7530
7531 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7532 if (TYPE_FIELD_NAME (type, 0) == NULL
7533 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7534 || TYPE_FIELD_NAME (type, 1) == NULL
7535 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7536 return;
c0dd20ea
DJ
7537
7538 /* Find the type of the method. */
0b92b5bb 7539 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7540 if (pfn_type == NULL
7541 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7542 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7543 return;
c0dd20ea
DJ
7544
7545 /* Look for the "this" argument. */
7546 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7547 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7548 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7549 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7550 return;
c0dd20ea
DJ
7551
7552 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7553 new_type = alloc_type (objfile);
7554 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7555 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7556 TYPE_VARARGS (pfn_type));
0b92b5bb 7557 smash_to_methodptr_type (type, new_type);
c0dd20ea 7558}
1168df01 7559
c906108c 7560/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7561 (definition) to create a type for the structure or union. Fill in
7562 the type's name and general properties; the members will not be
7563 processed until process_structure_type.
c906108c 7564
c767944b
DJ
7565 NOTE: we need to call these functions regardless of whether or not the
7566 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7567 structure or union. This gets the type entered into our set of
7568 user defined types.
7569
7570 However, if the structure is incomplete (an opaque struct/union)
7571 then suppress creating a symbol table entry for it since gdb only
7572 wants to find the one with the complete definition. Note that if
7573 it is complete, we just call new_symbol, which does it's own
7574 checking about whether the struct/union is anonymous or not (and
7575 suppresses creating a symbol table entry itself). */
7576
f792889a 7577static struct type *
134d01f1 7578read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7579{
e7c27a73 7580 struct objfile *objfile = cu->objfile;
c906108c
SS
7581 struct type *type;
7582 struct attribute *attr;
39cbfefa 7583 char *name;
c906108c 7584
348e048f
DE
7585 /* If the definition of this type lives in .debug_types, read that type.
7586 Don't follow DW_AT_specification though, that will take us back up
7587 the chain and we want to go down. */
7588 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7589 if (attr)
7590 {
7591 struct dwarf2_cu *type_cu = cu;
7592 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7593
348e048f
DE
7594 /* We could just recurse on read_structure_type, but we need to call
7595 get_die_type to ensure only one type for this DIE is created.
7596 This is important, for example, because for c++ classes we need
7597 TYPE_NAME set which is only done by new_symbol. Blech. */
7598 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7599
7600 /* TYPE_CU may not be the same as CU.
7601 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7602 return set_die_type (die, type, cu);
7603 }
7604
c0dd20ea 7605 type = alloc_type (objfile);
c906108c 7606 INIT_CPLUS_SPECIFIC (type);
93311388 7607
39cbfefa
DJ
7608 name = dwarf2_name (die, cu);
7609 if (name != NULL)
c906108c 7610 {
987504bb
JJ
7611 if (cu->language == language_cplus
7612 || cu->language == language_java)
63d06c5c 7613 {
3da10d80
KS
7614 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7615
7616 /* dwarf2_full_name might have already finished building the DIE's
7617 type. If so, there is no need to continue. */
7618 if (get_die_type (die, cu) != NULL)
7619 return get_die_type (die, cu);
7620
7621 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7622 if (die->tag == DW_TAG_structure_type
7623 || die->tag == DW_TAG_class_type)
7624 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7625 }
7626 else
7627 {
d8151005
DJ
7628 /* The name is already allocated along with this objfile, so
7629 we don't need to duplicate it for the type. */
94af9270
KS
7630 TYPE_TAG_NAME (type) = (char *) name;
7631 if (die->tag == DW_TAG_class_type)
7632 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7633 }
c906108c
SS
7634 }
7635
7636 if (die->tag == DW_TAG_structure_type)
7637 {
7638 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7639 }
7640 else if (die->tag == DW_TAG_union_type)
7641 {
7642 TYPE_CODE (type) = TYPE_CODE_UNION;
7643 }
7644 else
7645 {
c906108c
SS
7646 TYPE_CODE (type) = TYPE_CODE_CLASS;
7647 }
7648
0cc2414c
TT
7649 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7650 TYPE_DECLARED_CLASS (type) = 1;
7651
e142c38c 7652 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7653 if (attr)
7654 {
7655 TYPE_LENGTH (type) = DW_UNSND (attr);
7656 }
7657 else
7658 {
7659 TYPE_LENGTH (type) = 0;
7660 }
7661
876cecd0 7662 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7663 if (die_is_declaration (die, cu))
876cecd0 7664 TYPE_STUB (type) = 1;
a6c727b2
DJ
7665 else if (attr == NULL && die->child == NULL
7666 && producer_is_realview (cu->producer))
7667 /* RealView does not output the required DW_AT_declaration
7668 on incomplete types. */
7669 TYPE_STUB (type) = 1;
dc718098 7670
c906108c
SS
7671 /* We need to add the type field to the die immediately so we don't
7672 infinitely recurse when dealing with pointers to the structure
0963b4bd 7673 type within the structure itself. */
1c379e20 7674 set_die_type (die, type, cu);
c906108c 7675
7e314c57
JK
7676 /* set_die_type should be already done. */
7677 set_descriptive_type (type, die, cu);
7678
c767944b
DJ
7679 return type;
7680}
7681
7682/* Finish creating a structure or union type, including filling in
7683 its members and creating a symbol for it. */
7684
7685static void
7686process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7687{
7688 struct objfile *objfile = cu->objfile;
7689 struct die_info *child_die = die->child;
7690 struct type *type;
7691
7692 type = get_die_type (die, cu);
7693 if (type == NULL)
7694 type = read_structure_type (die, cu);
7695
e142c38c 7696 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7697 {
7698 struct field_info fi;
7699 struct die_info *child_die;
34eaf542 7700 VEC (symbolp) *template_args = NULL;
c767944b 7701 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7702
7703 memset (&fi, 0, sizeof (struct field_info));
7704
639d11d3 7705 child_die = die->child;
c906108c
SS
7706
7707 while (child_die && child_die->tag)
7708 {
a9a9bd0f
DC
7709 if (child_die->tag == DW_TAG_member
7710 || child_die->tag == DW_TAG_variable)
c906108c 7711 {
a9a9bd0f
DC
7712 /* NOTE: carlton/2002-11-05: A C++ static data member
7713 should be a DW_TAG_member that is a declaration, but
7714 all versions of G++ as of this writing (so through at
7715 least 3.2.1) incorrectly generate DW_TAG_variable
7716 tags for them instead. */
e7c27a73 7717 dwarf2_add_field (&fi, child_die, cu);
c906108c 7718 }
8713b1b1 7719 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7720 {
0963b4bd 7721 /* C++ member function. */
e7c27a73 7722 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7723 }
7724 else if (child_die->tag == DW_TAG_inheritance)
7725 {
7726 /* C++ base class field. */
e7c27a73 7727 dwarf2_add_field (&fi, child_die, cu);
c906108c 7728 }
98751a41
JK
7729 else if (child_die->tag == DW_TAG_typedef)
7730 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7731 else if (child_die->tag == DW_TAG_template_type_param
7732 || child_die->tag == DW_TAG_template_value_param)
7733 {
7734 struct symbol *arg = new_symbol (child_die, NULL, cu);
7735
f1078f66
DJ
7736 if (arg != NULL)
7737 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7738 }
7739
c906108c
SS
7740 child_die = sibling_die (child_die);
7741 }
7742
34eaf542
TT
7743 /* Attach template arguments to type. */
7744 if (! VEC_empty (symbolp, template_args))
7745 {
7746 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7747 TYPE_N_TEMPLATE_ARGUMENTS (type)
7748 = VEC_length (symbolp, template_args);
7749 TYPE_TEMPLATE_ARGUMENTS (type)
7750 = obstack_alloc (&objfile->objfile_obstack,
7751 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7752 * sizeof (struct symbol *)));
7753 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7754 VEC_address (symbolp, template_args),
7755 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7756 * sizeof (struct symbol *)));
7757 VEC_free (symbolp, template_args);
7758 }
7759
c906108c
SS
7760 /* Attach fields and member functions to the type. */
7761 if (fi.nfields)
e7c27a73 7762 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7763 if (fi.nfnfields)
7764 {
e7c27a73 7765 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7766
c5aa993b 7767 /* Get the type which refers to the base class (possibly this
c906108c 7768 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7769 class from the DW_AT_containing_type attribute. This use of
7770 DW_AT_containing_type is a GNU extension. */
c906108c 7771
e142c38c 7772 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7773 {
e7c27a73 7774 struct type *t = die_containing_type (die, cu);
c906108c
SS
7775
7776 TYPE_VPTR_BASETYPE (type) = t;
7777 if (type == t)
7778 {
c906108c
SS
7779 int i;
7780
7781 /* Our own class provides vtbl ptr. */
7782 for (i = TYPE_NFIELDS (t) - 1;
7783 i >= TYPE_N_BASECLASSES (t);
7784 --i)
7785 {
0d5cff50 7786 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 7787
1168df01 7788 if (is_vtable_name (fieldname, cu))
c906108c
SS
7789 {
7790 TYPE_VPTR_FIELDNO (type) = i;
7791 break;
7792 }
7793 }
7794
7795 /* Complain if virtual function table field not found. */
7796 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7797 complaint (&symfile_complaints,
3e43a32a
MS
7798 _("virtual function table pointer "
7799 "not found when defining class '%s'"),
4d3c2250
KB
7800 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7801 "");
c906108c
SS
7802 }
7803 else
7804 {
7805 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7806 }
7807 }
f6235d4c
EZ
7808 else if (cu->producer
7809 && strncmp (cu->producer,
7810 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7811 {
7812 /* The IBM XLC compiler does not provide direct indication
7813 of the containing type, but the vtable pointer is
7814 always named __vfp. */
7815
7816 int i;
7817
7818 for (i = TYPE_NFIELDS (type) - 1;
7819 i >= TYPE_N_BASECLASSES (type);
7820 --i)
7821 {
7822 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7823 {
7824 TYPE_VPTR_FIELDNO (type) = i;
7825 TYPE_VPTR_BASETYPE (type) = type;
7826 break;
7827 }
7828 }
7829 }
c906108c 7830 }
98751a41
JK
7831
7832 /* Copy fi.typedef_field_list linked list elements content into the
7833 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7834 if (fi.typedef_field_list)
7835 {
7836 int i = fi.typedef_field_list_count;
7837
a0d7a4ff 7838 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7839 TYPE_TYPEDEF_FIELD_ARRAY (type)
7840 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7841 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7842
7843 /* Reverse the list order to keep the debug info elements order. */
7844 while (--i >= 0)
7845 {
7846 struct typedef_field *dest, *src;
6e70227d 7847
98751a41
JK
7848 dest = &TYPE_TYPEDEF_FIELD (type, i);
7849 src = &fi.typedef_field_list->field;
7850 fi.typedef_field_list = fi.typedef_field_list->next;
7851 *dest = *src;
7852 }
7853 }
c767944b
DJ
7854
7855 do_cleanups (back_to);
eb2a6f42
TT
7856
7857 if (HAVE_CPLUS_STRUCT (type))
7858 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7859 }
63d06c5c 7860
bb5ed363 7861 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7862
90aeadfc
DC
7863 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7864 snapshots) has been known to create a die giving a declaration
7865 for a class that has, as a child, a die giving a definition for a
7866 nested class. So we have to process our children even if the
7867 current die is a declaration. Normally, of course, a declaration
7868 won't have any children at all. */
134d01f1 7869
90aeadfc
DC
7870 while (child_die != NULL && child_die->tag)
7871 {
7872 if (child_die->tag == DW_TAG_member
7873 || child_die->tag == DW_TAG_variable
34eaf542
TT
7874 || child_die->tag == DW_TAG_inheritance
7875 || child_die->tag == DW_TAG_template_value_param
7876 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7877 {
90aeadfc 7878 /* Do nothing. */
134d01f1 7879 }
90aeadfc
DC
7880 else
7881 process_die (child_die, cu);
134d01f1 7882
90aeadfc 7883 child_die = sibling_die (child_die);
134d01f1
DJ
7884 }
7885
fa4028e9
JB
7886 /* Do not consider external references. According to the DWARF standard,
7887 these DIEs are identified by the fact that they have no byte_size
7888 attribute, and a declaration attribute. */
7889 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7890 || !die_is_declaration (die, cu))
c767944b 7891 new_symbol (die, type, cu);
134d01f1
DJ
7892}
7893
7894/* Given a DW_AT_enumeration_type die, set its type. We do not
7895 complete the type's fields yet, or create any symbols. */
c906108c 7896
f792889a 7897static struct type *
134d01f1 7898read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7899{
e7c27a73 7900 struct objfile *objfile = cu->objfile;
c906108c 7901 struct type *type;
c906108c 7902 struct attribute *attr;
0114d602 7903 const char *name;
134d01f1 7904
348e048f
DE
7905 /* If the definition of this type lives in .debug_types, read that type.
7906 Don't follow DW_AT_specification though, that will take us back up
7907 the chain and we want to go down. */
7908 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7909 if (attr)
7910 {
7911 struct dwarf2_cu *type_cu = cu;
7912 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7913
348e048f 7914 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7915
7916 /* TYPE_CU may not be the same as CU.
7917 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7918 return set_die_type (die, type, cu);
7919 }
7920
c906108c
SS
7921 type = alloc_type (objfile);
7922
7923 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7924 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7925 if (name != NULL)
0114d602 7926 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7927
e142c38c 7928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7929 if (attr)
7930 {
7931 TYPE_LENGTH (type) = DW_UNSND (attr);
7932 }
7933 else
7934 {
7935 TYPE_LENGTH (type) = 0;
7936 }
7937
137033e9
JB
7938 /* The enumeration DIE can be incomplete. In Ada, any type can be
7939 declared as private in the package spec, and then defined only
7940 inside the package body. Such types are known as Taft Amendment
7941 Types. When another package uses such a type, an incomplete DIE
7942 may be generated by the compiler. */
02eb380e 7943 if (die_is_declaration (die, cu))
876cecd0 7944 TYPE_STUB (type) = 1;
02eb380e 7945
f792889a 7946 return set_die_type (die, type, cu);
134d01f1
DJ
7947}
7948
7949/* Given a pointer to a die which begins an enumeration, process all
7950 the dies that define the members of the enumeration, and create the
7951 symbol for the enumeration type.
7952
7953 NOTE: We reverse the order of the element list. */
7954
7955static void
7956process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7957{
f792889a 7958 struct type *this_type;
134d01f1 7959
f792889a
DJ
7960 this_type = get_die_type (die, cu);
7961 if (this_type == NULL)
7962 this_type = read_enumeration_type (die, cu);
9dc481d3 7963
639d11d3 7964 if (die->child != NULL)
c906108c 7965 {
9dc481d3
DE
7966 struct die_info *child_die;
7967 struct symbol *sym;
7968 struct field *fields = NULL;
7969 int num_fields = 0;
7970 int unsigned_enum = 1;
7971 char *name;
cafec441
TT
7972 int flag_enum = 1;
7973 ULONGEST mask = 0;
9dc481d3 7974
639d11d3 7975 child_die = die->child;
c906108c
SS
7976 while (child_die && child_die->tag)
7977 {
7978 if (child_die->tag != DW_TAG_enumerator)
7979 {
e7c27a73 7980 process_die (child_die, cu);
c906108c
SS
7981 }
7982 else
7983 {
39cbfefa
DJ
7984 name = dwarf2_name (child_die, cu);
7985 if (name)
c906108c 7986 {
f792889a 7987 sym = new_symbol (child_die, this_type, cu);
c906108c 7988 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
7989 {
7990 unsigned_enum = 0;
7991 flag_enum = 0;
7992 }
7993 else if ((mask & SYMBOL_VALUE (sym)) != 0)
7994 flag_enum = 0;
7995 else
7996 mask |= SYMBOL_VALUE (sym);
c906108c
SS
7997
7998 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7999 {
8000 fields = (struct field *)
8001 xrealloc (fields,
8002 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8003 * sizeof (struct field));
c906108c
SS
8004 }
8005
3567439c 8006 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8007 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8008 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8009 FIELD_BITSIZE (fields[num_fields]) = 0;
8010
8011 num_fields++;
8012 }
8013 }
8014
8015 child_die = sibling_die (child_die);
8016 }
8017
8018 if (num_fields)
8019 {
f792889a
DJ
8020 TYPE_NFIELDS (this_type) = num_fields;
8021 TYPE_FIELDS (this_type) = (struct field *)
8022 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8023 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8024 sizeof (struct field) * num_fields);
b8c9b27d 8025 xfree (fields);
c906108c
SS
8026 }
8027 if (unsigned_enum)
876cecd0 8028 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
8029 if (flag_enum)
8030 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 8031 }
134d01f1 8032
6c83ed52
TT
8033 /* If we are reading an enum from a .debug_types unit, and the enum
8034 is a declaration, and the enum is not the signatured type in the
8035 unit, then we do not want to add a symbol for it. Adding a
8036 symbol would in some cases obscure the true definition of the
8037 enum, giving users an incomplete type when the definition is
8038 actually available. Note that we do not want to do this for all
8039 enums which are just declarations, because C++0x allows forward
8040 enum declarations. */
b0df02fd 8041 if (cu->per_cu->debug_types_section
6c83ed52
TT
8042 && die_is_declaration (die, cu))
8043 {
8044 struct signatured_type *type_sig;
8045
8046 type_sig
8047 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8048 cu->per_cu->debug_types_section,
6c83ed52
TT
8049 cu->per_cu->offset);
8050 if (type_sig->type_offset != die->offset)
8051 return;
8052 }
8053
f792889a 8054 new_symbol (die, this_type, cu);
c906108c
SS
8055}
8056
8057/* Extract all information from a DW_TAG_array_type DIE and put it in
8058 the DIE's type field. For now, this only handles one dimensional
8059 arrays. */
8060
f792889a 8061static struct type *
e7c27a73 8062read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8063{
e7c27a73 8064 struct objfile *objfile = cu->objfile;
c906108c 8065 struct die_info *child_die;
7e314c57 8066 struct type *type;
c906108c
SS
8067 struct type *element_type, *range_type, *index_type;
8068 struct type **range_types = NULL;
8069 struct attribute *attr;
8070 int ndim = 0;
8071 struct cleanup *back_to;
39cbfefa 8072 char *name;
c906108c 8073
e7c27a73 8074 element_type = die_type (die, cu);
c906108c 8075
7e314c57
JK
8076 /* The die_type call above may have already set the type for this DIE. */
8077 type = get_die_type (die, cu);
8078 if (type)
8079 return type;
8080
c906108c
SS
8081 /* Irix 6.2 native cc creates array types without children for
8082 arrays with unspecified length. */
639d11d3 8083 if (die->child == NULL)
c906108c 8084 {
46bf5051 8085 index_type = objfile_type (objfile)->builtin_int;
c906108c 8086 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8087 type = create_array_type (NULL, element_type, range_type);
8088 return set_die_type (die, type, cu);
c906108c
SS
8089 }
8090
8091 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8092 child_die = die->child;
c906108c
SS
8093 while (child_die && child_die->tag)
8094 {
8095 if (child_die->tag == DW_TAG_subrange_type)
8096 {
f792889a 8097 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8098
f792889a 8099 if (child_type != NULL)
a02abb62 8100 {
0963b4bd
MS
8101 /* The range type was succesfully read. Save it for the
8102 array type creation. */
a02abb62
JB
8103 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8104 {
8105 range_types = (struct type **)
8106 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8107 * sizeof (struct type *));
8108 if (ndim == 0)
8109 make_cleanup (free_current_contents, &range_types);
8110 }
f792889a 8111 range_types[ndim++] = child_type;
a02abb62 8112 }
c906108c
SS
8113 }
8114 child_die = sibling_die (child_die);
8115 }
8116
8117 /* Dwarf2 dimensions are output from left to right, create the
8118 necessary array types in backwards order. */
7ca2d3a3 8119
c906108c 8120 type = element_type;
7ca2d3a3
DL
8121
8122 if (read_array_order (die, cu) == DW_ORD_col_major)
8123 {
8124 int i = 0;
9a619af0 8125
7ca2d3a3
DL
8126 while (i < ndim)
8127 type = create_array_type (NULL, type, range_types[i++]);
8128 }
8129 else
8130 {
8131 while (ndim-- > 0)
8132 type = create_array_type (NULL, type, range_types[ndim]);
8133 }
c906108c 8134
f5f8a009
EZ
8135 /* Understand Dwarf2 support for vector types (like they occur on
8136 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8137 array type. This is not part of the Dwarf2/3 standard yet, but a
8138 custom vendor extension. The main difference between a regular
8139 array and the vector variant is that vectors are passed by value
8140 to functions. */
e142c38c 8141 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8142 if (attr)
ea37ba09 8143 make_vector_type (type);
f5f8a009 8144
dbc98a8b
KW
8145 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8146 implementation may choose to implement triple vectors using this
8147 attribute. */
8148 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8149 if (attr)
8150 {
8151 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8152 TYPE_LENGTH (type) = DW_UNSND (attr);
8153 else
3e43a32a
MS
8154 complaint (&symfile_complaints,
8155 _("DW_AT_byte_size for array type smaller "
8156 "than the total size of elements"));
dbc98a8b
KW
8157 }
8158
39cbfefa
DJ
8159 name = dwarf2_name (die, cu);
8160 if (name)
8161 TYPE_NAME (type) = name;
6e70227d 8162
0963b4bd 8163 /* Install the type in the die. */
7e314c57
JK
8164 set_die_type (die, type, cu);
8165
8166 /* set_die_type should be already done. */
b4ba55a1
JB
8167 set_descriptive_type (type, die, cu);
8168
c906108c
SS
8169 do_cleanups (back_to);
8170
7e314c57 8171 return type;
c906108c
SS
8172}
8173
7ca2d3a3 8174static enum dwarf_array_dim_ordering
6e70227d 8175read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8176{
8177 struct attribute *attr;
8178
8179 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8180
8181 if (attr) return DW_SND (attr);
8182
0963b4bd
MS
8183 /* GNU F77 is a special case, as at 08/2004 array type info is the
8184 opposite order to the dwarf2 specification, but data is still
8185 laid out as per normal fortran.
7ca2d3a3 8186
0963b4bd
MS
8187 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8188 version checking. */
7ca2d3a3 8189
905e0470
PM
8190 if (cu->language == language_fortran
8191 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8192 {
8193 return DW_ORD_row_major;
8194 }
8195
6e70227d 8196 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8197 {
8198 case array_column_major:
8199 return DW_ORD_col_major;
8200 case array_row_major:
8201 default:
8202 return DW_ORD_row_major;
8203 };
8204}
8205
72019c9c 8206/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8207 the DIE's type field. */
72019c9c 8208
f792889a 8209static struct type *
72019c9c
GM
8210read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8211{
7e314c57
JK
8212 struct type *domain_type, *set_type;
8213 struct attribute *attr;
f792889a 8214
7e314c57
JK
8215 domain_type = die_type (die, cu);
8216
8217 /* The die_type call above may have already set the type for this DIE. */
8218 set_type = get_die_type (die, cu);
8219 if (set_type)
8220 return set_type;
8221
8222 set_type = create_set_type (NULL, domain_type);
8223
8224 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8225 if (attr)
8226 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8227
f792889a 8228 return set_die_type (die, set_type, cu);
72019c9c 8229}
7ca2d3a3 8230
c906108c
SS
8231/* First cut: install each common block member as a global variable. */
8232
8233static void
e7c27a73 8234read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8235{
8236 struct die_info *child_die;
8237 struct attribute *attr;
8238 struct symbol *sym;
8239 CORE_ADDR base = (CORE_ADDR) 0;
8240
e142c38c 8241 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8242 if (attr)
8243 {
0963b4bd 8244 /* Support the .debug_loc offsets. */
8e19ed76
PS
8245 if (attr_form_is_block (attr))
8246 {
e7c27a73 8247 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8248 }
3690dd37 8249 else if (attr_form_is_section_offset (attr))
8e19ed76 8250 {
4d3c2250 8251 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8252 }
8253 else
8254 {
4d3c2250
KB
8255 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8256 "common block member");
8e19ed76 8257 }
c906108c 8258 }
639d11d3 8259 if (die->child != NULL)
c906108c 8260 {
639d11d3 8261 child_die = die->child;
c906108c
SS
8262 while (child_die && child_die->tag)
8263 {
74ac6d43
TT
8264 LONGEST offset;
8265
e7c27a73 8266 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8267 if (sym != NULL
8268 && handle_data_member_location (child_die, cu, &offset))
c906108c 8269 {
74ac6d43 8270 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8271 add_symbol_to_list (sym, &global_symbols);
8272 }
8273 child_die = sibling_die (child_die);
8274 }
8275 }
8276}
8277
0114d602 8278/* Create a type for a C++ namespace. */
d9fa45fe 8279
0114d602
DJ
8280static struct type *
8281read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8282{
e7c27a73 8283 struct objfile *objfile = cu->objfile;
0114d602 8284 const char *previous_prefix, *name;
9219021c 8285 int is_anonymous;
0114d602
DJ
8286 struct type *type;
8287
8288 /* For extensions, reuse the type of the original namespace. */
8289 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8290 {
8291 struct die_info *ext_die;
8292 struct dwarf2_cu *ext_cu = cu;
9a619af0 8293
0114d602
DJ
8294 ext_die = dwarf2_extension (die, &ext_cu);
8295 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8296
8297 /* EXT_CU may not be the same as CU.
8298 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8299 return set_die_type (die, type, cu);
8300 }
9219021c 8301
e142c38c 8302 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8303
8304 /* Now build the name of the current namespace. */
8305
0114d602
DJ
8306 previous_prefix = determine_prefix (die, cu);
8307 if (previous_prefix[0] != '\0')
8308 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8309 previous_prefix, name, 0, cu);
0114d602
DJ
8310
8311 /* Create the type. */
8312 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8313 objfile);
8314 TYPE_NAME (type) = (char *) name;
8315 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8316
60531b24 8317 return set_die_type (die, type, cu);
0114d602
DJ
8318}
8319
8320/* Read a C++ namespace. */
8321
8322static void
8323read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8324{
8325 struct objfile *objfile = cu->objfile;
0114d602 8326 int is_anonymous;
9219021c 8327
5c4e30ca
DC
8328 /* Add a symbol associated to this if we haven't seen the namespace
8329 before. Also, add a using directive if it's an anonymous
8330 namespace. */
9219021c 8331
f2f0e013 8332 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8333 {
8334 struct type *type;
8335
0114d602 8336 type = read_type_die (die, cu);
e7c27a73 8337 new_symbol (die, type, cu);
5c4e30ca 8338
e8e80198 8339 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8340 if (is_anonymous)
0114d602
DJ
8341 {
8342 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8343
c0cc3a76 8344 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8345 NULL, NULL, &objfile->objfile_obstack);
0114d602 8346 }
5c4e30ca 8347 }
9219021c 8348
639d11d3 8349 if (die->child != NULL)
d9fa45fe 8350 {
639d11d3 8351 struct die_info *child_die = die->child;
6e70227d 8352
d9fa45fe
DC
8353 while (child_die && child_die->tag)
8354 {
e7c27a73 8355 process_die (child_die, cu);
d9fa45fe
DC
8356 child_die = sibling_die (child_die);
8357 }
8358 }
38d518c9
EZ
8359}
8360
f55ee35c
JK
8361/* Read a Fortran module as type. This DIE can be only a declaration used for
8362 imported module. Still we need that type as local Fortran "use ... only"
8363 declaration imports depend on the created type in determine_prefix. */
8364
8365static struct type *
8366read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8367{
8368 struct objfile *objfile = cu->objfile;
8369 char *module_name;
8370 struct type *type;
8371
8372 module_name = dwarf2_name (die, cu);
8373 if (!module_name)
3e43a32a
MS
8374 complaint (&symfile_complaints,
8375 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8376 die->offset);
8377 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8378
8379 /* determine_prefix uses TYPE_TAG_NAME. */
8380 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8381
8382 return set_die_type (die, type, cu);
8383}
8384
5d7cb8df
JK
8385/* Read a Fortran module. */
8386
8387static void
8388read_module (struct die_info *die, struct dwarf2_cu *cu)
8389{
8390 struct die_info *child_die = die->child;
8391
5d7cb8df
JK
8392 while (child_die && child_die->tag)
8393 {
8394 process_die (child_die, cu);
8395 child_die = sibling_die (child_die);
8396 }
8397}
8398
38d518c9
EZ
8399/* Return the name of the namespace represented by DIE. Set
8400 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8401 namespace. */
8402
8403static const char *
e142c38c 8404namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8405{
8406 struct die_info *current_die;
8407 const char *name = NULL;
8408
8409 /* Loop through the extensions until we find a name. */
8410
8411 for (current_die = die;
8412 current_die != NULL;
f2f0e013 8413 current_die = dwarf2_extension (die, &cu))
38d518c9 8414 {
e142c38c 8415 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8416 if (name != NULL)
8417 break;
8418 }
8419
8420 /* Is it an anonymous namespace? */
8421
8422 *is_anonymous = (name == NULL);
8423 if (*is_anonymous)
2b1dbab0 8424 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8425
8426 return name;
d9fa45fe
DC
8427}
8428
c906108c
SS
8429/* Extract all information from a DW_TAG_pointer_type DIE and add to
8430 the user defined type vector. */
8431
f792889a 8432static struct type *
e7c27a73 8433read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8434{
5e2b427d 8435 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8436 struct comp_unit_head *cu_header = &cu->header;
c906108c 8437 struct type *type;
8b2dbe47
KB
8438 struct attribute *attr_byte_size;
8439 struct attribute *attr_address_class;
8440 int byte_size, addr_class;
7e314c57
JK
8441 struct type *target_type;
8442
8443 target_type = die_type (die, cu);
c906108c 8444
7e314c57
JK
8445 /* The die_type call above may have already set the type for this DIE. */
8446 type = get_die_type (die, cu);
8447 if (type)
8448 return type;
8449
8450 type = lookup_pointer_type (target_type);
8b2dbe47 8451
e142c38c 8452 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8453 if (attr_byte_size)
8454 byte_size = DW_UNSND (attr_byte_size);
c906108c 8455 else
8b2dbe47
KB
8456 byte_size = cu_header->addr_size;
8457
e142c38c 8458 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8459 if (attr_address_class)
8460 addr_class = DW_UNSND (attr_address_class);
8461 else
8462 addr_class = DW_ADDR_none;
8463
8464 /* If the pointer size or address class is different than the
8465 default, create a type variant marked as such and set the
8466 length accordingly. */
8467 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8468 {
5e2b427d 8469 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8470 {
8471 int type_flags;
8472
849957d9 8473 type_flags = gdbarch_address_class_type_flags
5e2b427d 8474 (gdbarch, byte_size, addr_class);
876cecd0
TT
8475 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8476 == 0);
8b2dbe47
KB
8477 type = make_type_with_address_space (type, type_flags);
8478 }
8479 else if (TYPE_LENGTH (type) != byte_size)
8480 {
3e43a32a
MS
8481 complaint (&symfile_complaints,
8482 _("invalid pointer size %d"), byte_size);
8b2dbe47 8483 }
6e70227d 8484 else
9a619af0
MS
8485 {
8486 /* Should we also complain about unhandled address classes? */
8487 }
c906108c 8488 }
8b2dbe47
KB
8489
8490 TYPE_LENGTH (type) = byte_size;
f792889a 8491 return set_die_type (die, type, cu);
c906108c
SS
8492}
8493
8494/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8495 the user defined type vector. */
8496
f792889a 8497static struct type *
e7c27a73 8498read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8499{
8500 struct type *type;
8501 struct type *to_type;
8502 struct type *domain;
8503
e7c27a73
DJ
8504 to_type = die_type (die, cu);
8505 domain = die_containing_type (die, cu);
0d5de010 8506
7e314c57
JK
8507 /* The calls above may have already set the type for this DIE. */
8508 type = get_die_type (die, cu);
8509 if (type)
8510 return type;
8511
0d5de010
DJ
8512 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8513 type = lookup_methodptr_type (to_type);
8514 else
8515 type = lookup_memberptr_type (to_type, domain);
c906108c 8516
f792889a 8517 return set_die_type (die, type, cu);
c906108c
SS
8518}
8519
8520/* Extract all information from a DW_TAG_reference_type DIE and add to
8521 the user defined type vector. */
8522
f792889a 8523static struct type *
e7c27a73 8524read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8525{
e7c27a73 8526 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8527 struct type *type, *target_type;
c906108c
SS
8528 struct attribute *attr;
8529
7e314c57
JK
8530 target_type = die_type (die, cu);
8531
8532 /* The die_type call above may have already set the type for this DIE. */
8533 type = get_die_type (die, cu);
8534 if (type)
8535 return type;
8536
8537 type = lookup_reference_type (target_type);
e142c38c 8538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8539 if (attr)
8540 {
8541 TYPE_LENGTH (type) = DW_UNSND (attr);
8542 }
8543 else
8544 {
107d2387 8545 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8546 }
f792889a 8547 return set_die_type (die, type, cu);
c906108c
SS
8548}
8549
f792889a 8550static struct type *
e7c27a73 8551read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8552{
f792889a 8553 struct type *base_type, *cv_type;
c906108c 8554
e7c27a73 8555 base_type = die_type (die, cu);
7e314c57
JK
8556
8557 /* The die_type call above may have already set the type for this DIE. */
8558 cv_type = get_die_type (die, cu);
8559 if (cv_type)
8560 return cv_type;
8561
2f608a3a
KW
8562 /* In case the const qualifier is applied to an array type, the element type
8563 is so qualified, not the array type (section 6.7.3 of C99). */
8564 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8565 {
8566 struct type *el_type, *inner_array;
8567
8568 base_type = copy_type (base_type);
8569 inner_array = base_type;
8570
8571 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8572 {
8573 TYPE_TARGET_TYPE (inner_array) =
8574 copy_type (TYPE_TARGET_TYPE (inner_array));
8575 inner_array = TYPE_TARGET_TYPE (inner_array);
8576 }
8577
8578 el_type = TYPE_TARGET_TYPE (inner_array);
8579 TYPE_TARGET_TYPE (inner_array) =
8580 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8581
8582 return set_die_type (die, base_type, cu);
8583 }
8584
f792889a
DJ
8585 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8586 return set_die_type (die, cv_type, cu);
c906108c
SS
8587}
8588
f792889a 8589static struct type *
e7c27a73 8590read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8591{
f792889a 8592 struct type *base_type, *cv_type;
c906108c 8593
e7c27a73 8594 base_type = die_type (die, cu);
7e314c57
JK
8595
8596 /* The die_type call above may have already set the type for this DIE. */
8597 cv_type = get_die_type (die, cu);
8598 if (cv_type)
8599 return cv_type;
8600
f792889a
DJ
8601 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8602 return set_die_type (die, cv_type, cu);
c906108c
SS
8603}
8604
8605/* Extract all information from a DW_TAG_string_type DIE and add to
8606 the user defined type vector. It isn't really a user defined type,
8607 but it behaves like one, with other DIE's using an AT_user_def_type
8608 attribute to reference it. */
8609
f792889a 8610static struct type *
e7c27a73 8611read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8612{
e7c27a73 8613 struct objfile *objfile = cu->objfile;
3b7538c0 8614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8615 struct type *type, *range_type, *index_type, *char_type;
8616 struct attribute *attr;
8617 unsigned int length;
8618
e142c38c 8619 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8620 if (attr)
8621 {
8622 length = DW_UNSND (attr);
8623 }
8624 else
8625 {
0963b4bd 8626 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8628 if (attr)
8629 {
8630 length = DW_UNSND (attr);
8631 }
8632 else
8633 {
8634 length = 1;
8635 }
c906108c 8636 }
6ccb9162 8637
46bf5051 8638 index_type = objfile_type (objfile)->builtin_int;
c906108c 8639 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8640 char_type = language_string_char_type (cu->language_defn, gdbarch);
8641 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8642
f792889a 8643 return set_die_type (die, type, cu);
c906108c
SS
8644}
8645
8646/* Handle DIES due to C code like:
8647
8648 struct foo
c5aa993b
JM
8649 {
8650 int (*funcp)(int a, long l);
8651 int b;
8652 };
c906108c 8653
0963b4bd 8654 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8655
f792889a 8656static struct type *
e7c27a73 8657read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8658{
bb5ed363 8659 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8660 struct type *type; /* Type that this function returns. */
8661 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8662 struct attribute *attr;
8663
e7c27a73 8664 type = die_type (die, cu);
7e314c57
JK
8665
8666 /* The die_type call above may have already set the type for this DIE. */
8667 ftype = get_die_type (die, cu);
8668 if (ftype)
8669 return ftype;
8670
0c8b41f1 8671 ftype = lookup_function_type (type);
c906108c 8672
5b8101ae 8673 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8674 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8675 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8676 || cu->language == language_cplus
5b8101ae
PM
8677 || cu->language == language_java
8678 || cu->language == language_pascal)
876cecd0 8679 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8680 else if (producer_is_realview (cu->producer))
8681 /* RealView does not emit DW_AT_prototyped. We can not
8682 distinguish prototyped and unprototyped functions; default to
8683 prototyped, since that is more common in modern code (and
8684 RealView warns about unprototyped functions). */
8685 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8686
c055b101
CV
8687 /* Store the calling convention in the type if it's available in
8688 the subroutine die. Otherwise set the calling convention to
8689 the default value DW_CC_normal. */
8690 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8691 if (attr)
8692 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8693 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8694 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8695 else
8696 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8697
8698 /* We need to add the subroutine type to the die immediately so
8699 we don't infinitely recurse when dealing with parameters
0963b4bd 8700 declared as the same subroutine type. */
76c10ea2 8701 set_die_type (die, ftype, cu);
6e70227d 8702
639d11d3 8703 if (die->child != NULL)
c906108c 8704 {
bb5ed363 8705 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8706 struct die_info *child_die;
8072405b 8707 int nparams, iparams;
c906108c
SS
8708
8709 /* Count the number of parameters.
8710 FIXME: GDB currently ignores vararg functions, but knows about
8711 vararg member functions. */
8072405b 8712 nparams = 0;
639d11d3 8713 child_die = die->child;
c906108c
SS
8714 while (child_die && child_die->tag)
8715 {
8716 if (child_die->tag == DW_TAG_formal_parameter)
8717 nparams++;
8718 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8719 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8720 child_die = sibling_die (child_die);
8721 }
8722
8723 /* Allocate storage for parameters and fill them in. */
8724 TYPE_NFIELDS (ftype) = nparams;
8725 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8726 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8727
8072405b
JK
8728 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8729 even if we error out during the parameters reading below. */
8730 for (iparams = 0; iparams < nparams; iparams++)
8731 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8732
8733 iparams = 0;
639d11d3 8734 child_die = die->child;
c906108c
SS
8735 while (child_die && child_die->tag)
8736 {
8737 if (child_die->tag == DW_TAG_formal_parameter)
8738 {
3ce3b1ba
PA
8739 struct type *arg_type;
8740
8741 /* DWARF version 2 has no clean way to discern C++
8742 static and non-static member functions. G++ helps
8743 GDB by marking the first parameter for non-static
8744 member functions (which is the this pointer) as
8745 artificial. We pass this information to
8746 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8747
8748 DWARF version 3 added DW_AT_object_pointer, which GCC
8749 4.5 does not yet generate. */
e142c38c 8750 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8751 if (attr)
8752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8753 else
418835cc
KS
8754 {
8755 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8756
8757 /* GCC/43521: In java, the formal parameter
8758 "this" is sometimes not marked with DW_AT_artificial. */
8759 if (cu->language == language_java)
8760 {
8761 const char *name = dwarf2_name (child_die, cu);
9a619af0 8762
418835cc
KS
8763 if (name && !strcmp (name, "this"))
8764 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8765 }
8766 }
3ce3b1ba
PA
8767 arg_type = die_type (child_die, cu);
8768
8769 /* RealView does not mark THIS as const, which the testsuite
8770 expects. GCC marks THIS as const in method definitions,
8771 but not in the class specifications (GCC PR 43053). */
8772 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8773 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8774 {
8775 int is_this = 0;
8776 struct dwarf2_cu *arg_cu = cu;
8777 const char *name = dwarf2_name (child_die, cu);
8778
8779 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8780 if (attr)
8781 {
8782 /* If the compiler emits this, use it. */
8783 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8784 is_this = 1;
8785 }
8786 else if (name && strcmp (name, "this") == 0)
8787 /* Function definitions will have the argument names. */
8788 is_this = 1;
8789 else if (name == NULL && iparams == 0)
8790 /* Declarations may not have the names, so like
8791 elsewhere in GDB, assume an artificial first
8792 argument is "this". */
8793 is_this = 1;
8794
8795 if (is_this)
8796 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8797 arg_type, 0);
8798 }
8799
8800 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8801 iparams++;
8802 }
8803 child_die = sibling_die (child_die);
8804 }
8805 }
8806
76c10ea2 8807 return ftype;
c906108c
SS
8808}
8809
f792889a 8810static struct type *
e7c27a73 8811read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8812{
e7c27a73 8813 struct objfile *objfile = cu->objfile;
0114d602 8814 const char *name = NULL;
3c8e0968 8815 struct type *this_type, *target_type;
c906108c 8816
94af9270 8817 name = dwarf2_full_name (NULL, die, cu);
f792889a 8818 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8819 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8820 TYPE_NAME (this_type) = (char *) name;
f792889a 8821 set_die_type (die, this_type, cu);
3c8e0968
DE
8822 target_type = die_type (die, cu);
8823 if (target_type != this_type)
8824 TYPE_TARGET_TYPE (this_type) = target_type;
8825 else
8826 {
8827 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8828 spec and cause infinite loops in GDB. */
8829 complaint (&symfile_complaints,
8830 _("Self-referential DW_TAG_typedef "
8831 "- DIE at 0x%x [in module %s]"),
bb5ed363 8832 die->offset, objfile->name);
3c8e0968
DE
8833 TYPE_TARGET_TYPE (this_type) = NULL;
8834 }
f792889a 8835 return this_type;
c906108c
SS
8836}
8837
8838/* Find a representation of a given base type and install
8839 it in the TYPE field of the die. */
8840
f792889a 8841static struct type *
e7c27a73 8842read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8843{
e7c27a73 8844 struct objfile *objfile = cu->objfile;
c906108c
SS
8845 struct type *type;
8846 struct attribute *attr;
8847 int encoding = 0, size = 0;
39cbfefa 8848 char *name;
6ccb9162
UW
8849 enum type_code code = TYPE_CODE_INT;
8850 int type_flags = 0;
8851 struct type *target_type = NULL;
c906108c 8852
e142c38c 8853 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8854 if (attr)
8855 {
8856 encoding = DW_UNSND (attr);
8857 }
e142c38c 8858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8859 if (attr)
8860 {
8861 size = DW_UNSND (attr);
8862 }
39cbfefa 8863 name = dwarf2_name (die, cu);
6ccb9162 8864 if (!name)
c906108c 8865 {
6ccb9162
UW
8866 complaint (&symfile_complaints,
8867 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8868 }
6ccb9162
UW
8869
8870 switch (encoding)
c906108c 8871 {
6ccb9162
UW
8872 case DW_ATE_address:
8873 /* Turn DW_ATE_address into a void * pointer. */
8874 code = TYPE_CODE_PTR;
8875 type_flags |= TYPE_FLAG_UNSIGNED;
8876 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8877 break;
8878 case DW_ATE_boolean:
8879 code = TYPE_CODE_BOOL;
8880 type_flags |= TYPE_FLAG_UNSIGNED;
8881 break;
8882 case DW_ATE_complex_float:
8883 code = TYPE_CODE_COMPLEX;
8884 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8885 break;
8886 case DW_ATE_decimal_float:
8887 code = TYPE_CODE_DECFLOAT;
8888 break;
8889 case DW_ATE_float:
8890 code = TYPE_CODE_FLT;
8891 break;
8892 case DW_ATE_signed:
8893 break;
8894 case DW_ATE_unsigned:
8895 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8896 if (cu->language == language_fortran
8897 && name
8898 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8899 code = TYPE_CODE_CHAR;
6ccb9162
UW
8900 break;
8901 case DW_ATE_signed_char:
6e70227d 8902 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8903 || cu->language == language_pascal
8904 || cu->language == language_fortran)
6ccb9162
UW
8905 code = TYPE_CODE_CHAR;
8906 break;
8907 case DW_ATE_unsigned_char:
868a0084 8908 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8909 || cu->language == language_pascal
8910 || cu->language == language_fortran)
6ccb9162
UW
8911 code = TYPE_CODE_CHAR;
8912 type_flags |= TYPE_FLAG_UNSIGNED;
8913 break;
75079b2b
TT
8914 case DW_ATE_UTF:
8915 /* We just treat this as an integer and then recognize the
8916 type by name elsewhere. */
8917 break;
8918
6ccb9162
UW
8919 default:
8920 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8921 dwarf_type_encoding_name (encoding));
8922 break;
c906108c 8923 }
6ccb9162 8924
0114d602
DJ
8925 type = init_type (code, size, type_flags, NULL, objfile);
8926 TYPE_NAME (type) = name;
6ccb9162
UW
8927 TYPE_TARGET_TYPE (type) = target_type;
8928
0114d602 8929 if (name && strcmp (name, "char") == 0)
876cecd0 8930 TYPE_NOSIGN (type) = 1;
0114d602 8931
f792889a 8932 return set_die_type (die, type, cu);
c906108c
SS
8933}
8934
a02abb62
JB
8935/* Read the given DW_AT_subrange DIE. */
8936
f792889a 8937static struct type *
a02abb62
JB
8938read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8939{
8940 struct type *base_type;
8941 struct type *range_type;
8942 struct attribute *attr;
43bbcdc2
PH
8943 LONGEST low = 0;
8944 LONGEST high = -1;
39cbfefa 8945 char *name;
43bbcdc2 8946 LONGEST negative_mask;
e77813c8 8947
a02abb62 8948 base_type = die_type (die, cu);
953ac07e
JK
8949 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8950 check_typedef (base_type);
a02abb62 8951
7e314c57
JK
8952 /* The die_type call above may have already set the type for this DIE. */
8953 range_type = get_die_type (die, cu);
8954 if (range_type)
8955 return range_type;
8956
e142c38c 8957 if (cu->language == language_fortran)
6e70227d 8958 {
a02abb62
JB
8959 /* FORTRAN implies a lower bound of 1, if not given. */
8960 low = 1;
8961 }
8962
dd5e6932
DJ
8963 /* FIXME: For variable sized arrays either of these could be
8964 a variable rather than a constant value. We'll allow it,
8965 but we don't know how to handle it. */
e142c38c 8966 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8967 if (attr)
8968 low = dwarf2_get_attr_constant_value (attr, 0);
8969
e142c38c 8970 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8971 if (attr)
6e70227d 8972 {
d48323d8 8973 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8974 {
8975 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8976 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8977 FIXME: GDB does not yet know how to handle dynamic
8978 arrays properly, treat them as arrays with unspecified
8979 length for now.
8980
8981 FIXME: jimb/2003-09-22: GDB does not really know
8982 how to handle arrays of unspecified length
8983 either; we just represent them as zero-length
8984 arrays. Choose an appropriate upper bound given
8985 the lower bound we've computed above. */
8986 high = low - 1;
8987 }
8988 else
8989 high = dwarf2_get_attr_constant_value (attr, 1);
8990 }
e77813c8
PM
8991 else
8992 {
8993 attr = dwarf2_attr (die, DW_AT_count, cu);
8994 if (attr)
8995 {
8996 int count = dwarf2_get_attr_constant_value (attr, 1);
8997 high = low + count - 1;
8998 }
c2ff108b
JK
8999 else
9000 {
9001 /* Unspecified array length. */
9002 high = low - 1;
9003 }
e77813c8
PM
9004 }
9005
9006 /* Dwarf-2 specifications explicitly allows to create subrange types
9007 without specifying a base type.
9008 In that case, the base type must be set to the type of
9009 the lower bound, upper bound or count, in that order, if any of these
9010 three attributes references an object that has a type.
9011 If no base type is found, the Dwarf-2 specifications say that
9012 a signed integer type of size equal to the size of an address should
9013 be used.
9014 For the following C code: `extern char gdb_int [];'
9015 GCC produces an empty range DIE.
9016 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9017 high bound or count are not yet handled by this code. */
e77813c8
PM
9018 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9019 {
9020 struct objfile *objfile = cu->objfile;
9021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9022 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9023 struct type *int_type = objfile_type (objfile)->builtin_int;
9024
9025 /* Test "int", "long int", and "long long int" objfile types,
9026 and select the first one having a size above or equal to the
9027 architecture address size. */
9028 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9029 base_type = int_type;
9030 else
9031 {
9032 int_type = objfile_type (objfile)->builtin_long;
9033 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9034 base_type = int_type;
9035 else
9036 {
9037 int_type = objfile_type (objfile)->builtin_long_long;
9038 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9039 base_type = int_type;
9040 }
9041 }
9042 }
a02abb62 9043
6e70227d 9044 negative_mask =
43bbcdc2
PH
9045 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9046 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9047 low |= negative_mask;
9048 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9049 high |= negative_mask;
9050
a02abb62
JB
9051 range_type = create_range_type (NULL, base_type, low, high);
9052
bbb0eef6
JK
9053 /* Mark arrays with dynamic length at least as an array of unspecified
9054 length. GDB could check the boundary but before it gets implemented at
9055 least allow accessing the array elements. */
d48323d8 9056 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9057 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9058
c2ff108b
JK
9059 /* Ada expects an empty array on no boundary attributes. */
9060 if (attr == NULL && cu->language != language_ada)
9061 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9062
39cbfefa
DJ
9063 name = dwarf2_name (die, cu);
9064 if (name)
9065 TYPE_NAME (range_type) = name;
6e70227d 9066
e142c38c 9067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9068 if (attr)
9069 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9070
7e314c57
JK
9071 set_die_type (die, range_type, cu);
9072
9073 /* set_die_type should be already done. */
b4ba55a1
JB
9074 set_descriptive_type (range_type, die, cu);
9075
7e314c57 9076 return range_type;
a02abb62 9077}
6e70227d 9078
f792889a 9079static struct type *
81a17f79
JB
9080read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9081{
9082 struct type *type;
81a17f79 9083
81a17f79
JB
9084 /* For now, we only support the C meaning of an unspecified type: void. */
9085
0114d602
DJ
9086 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9087 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9088
f792889a 9089 return set_die_type (die, type, cu);
81a17f79 9090}
a02abb62 9091
51545339
DJ
9092/* Trivial hash function for die_info: the hash value of a DIE
9093 is its offset in .debug_info for this objfile. */
9094
9095static hashval_t
9096die_hash (const void *item)
9097{
9098 const struct die_info *die = item;
9a619af0 9099
51545339
DJ
9100 return die->offset;
9101}
9102
9103/* Trivial comparison function for die_info structures: two DIEs
9104 are equal if they have the same offset. */
9105
9106static int
9107die_eq (const void *item_lhs, const void *item_rhs)
9108{
9109 const struct die_info *die_lhs = item_lhs;
9110 const struct die_info *die_rhs = item_rhs;
9a619af0 9111
51545339
DJ
9112 return die_lhs->offset == die_rhs->offset;
9113}
9114
c906108c
SS
9115/* Read a whole compilation unit into a linked list of dies. */
9116
f9aca02d 9117static struct die_info *
93311388 9118read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9119{
93311388 9120 struct die_reader_specs reader_specs;
98bfdba5 9121 int read_abbrevs = 0;
1d9ec526 9122 struct cleanup *back_to = NULL;
98bfdba5
PA
9123 struct die_info *die;
9124
9125 if (cu->dwarf2_abbrevs == NULL)
9126 {
e5fe5e75 9127 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9128 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9129 read_abbrevs = 1;
9130 }
93311388 9131
348e048f 9132 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9133 cu->die_hash
9134 = htab_create_alloc_ex (cu->header.length / 12,
9135 die_hash,
9136 die_eq,
9137 NULL,
9138 &cu->comp_unit_obstack,
9139 hashtab_obstack_allocate,
9140 dummy_obstack_deallocate);
9141
93311388
DE
9142 init_cu_die_reader (&reader_specs, cu);
9143
98bfdba5
PA
9144 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9145
9146 if (read_abbrevs)
9147 do_cleanups (back_to);
9148
9149 return die;
639d11d3
DC
9150}
9151
d97bc12b
DE
9152/* Main entry point for reading a DIE and all children.
9153 Read the DIE and dump it if requested. */
9154
9155static struct die_info *
93311388
DE
9156read_die_and_children (const struct die_reader_specs *reader,
9157 gdb_byte *info_ptr,
d97bc12b
DE
9158 gdb_byte **new_info_ptr,
9159 struct die_info *parent)
9160{
93311388 9161 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9162 new_info_ptr, parent);
9163
9164 if (dwarf2_die_debug)
9165 {
348e048f
DE
9166 fprintf_unfiltered (gdb_stdlog,
9167 "\nRead die from %s of %s:\n",
b0df02fd 9168 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9169 ? ".debug_types"
9170 : ".debug_info"),
348e048f 9171 reader->abfd->filename);
d97bc12b
DE
9172 dump_die (result, dwarf2_die_debug);
9173 }
9174
9175 return result;
9176}
9177
639d11d3
DC
9178/* Read a single die and all its descendents. Set the die's sibling
9179 field to NULL; set other fields in the die correctly, and set all
9180 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9181 location of the info_ptr after reading all of those dies. PARENT
9182 is the parent of the die in question. */
9183
9184static struct die_info *
93311388
DE
9185read_die_and_children_1 (const struct die_reader_specs *reader,
9186 gdb_byte *info_ptr,
d97bc12b
DE
9187 gdb_byte **new_info_ptr,
9188 struct die_info *parent)
639d11d3
DC
9189{
9190 struct die_info *die;
fe1b8b76 9191 gdb_byte *cur_ptr;
639d11d3
DC
9192 int has_children;
9193
93311388 9194 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9195 if (die == NULL)
9196 {
9197 *new_info_ptr = cur_ptr;
9198 return NULL;
9199 }
93311388 9200 store_in_ref_table (die, reader->cu);
639d11d3
DC
9201
9202 if (has_children)
348e048f 9203 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9204 else
9205 {
9206 die->child = NULL;
9207 *new_info_ptr = cur_ptr;
9208 }
9209
9210 die->sibling = NULL;
9211 die->parent = parent;
9212 return die;
9213}
9214
9215/* Read a die, all of its descendents, and all of its siblings; set
9216 all of the fields of all of the dies correctly. Arguments are as
9217 in read_die_and_children. */
9218
9219static struct die_info *
93311388
DE
9220read_die_and_siblings (const struct die_reader_specs *reader,
9221 gdb_byte *info_ptr,
fe1b8b76 9222 gdb_byte **new_info_ptr,
639d11d3
DC
9223 struct die_info *parent)
9224{
9225 struct die_info *first_die, *last_sibling;
fe1b8b76 9226 gdb_byte *cur_ptr;
639d11d3 9227
c906108c 9228 cur_ptr = info_ptr;
639d11d3
DC
9229 first_die = last_sibling = NULL;
9230
9231 while (1)
c906108c 9232 {
639d11d3 9233 struct die_info *die
93311388 9234 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9235
1d325ec1 9236 if (die == NULL)
c906108c 9237 {
639d11d3
DC
9238 *new_info_ptr = cur_ptr;
9239 return first_die;
c906108c 9240 }
1d325ec1
DJ
9241
9242 if (!first_die)
9243 first_die = die;
c906108c 9244 else
1d325ec1
DJ
9245 last_sibling->sibling = die;
9246
9247 last_sibling = die;
c906108c 9248 }
c906108c
SS
9249}
9250
93311388
DE
9251/* Read the die from the .debug_info section buffer. Set DIEP to
9252 point to a newly allocated die with its information, except for its
9253 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9254 whether the die has children or not. */
9255
9256static gdb_byte *
9257read_full_die (const struct die_reader_specs *reader,
9258 struct die_info **diep, gdb_byte *info_ptr,
9259 int *has_children)
9260{
9261 unsigned int abbrev_number, bytes_read, i, offset;
9262 struct abbrev_info *abbrev;
9263 struct die_info *die;
9264 struct dwarf2_cu *cu = reader->cu;
9265 bfd *abfd = reader->abfd;
9266
9267 offset = info_ptr - reader->buffer;
9268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9269 info_ptr += bytes_read;
9270 if (!abbrev_number)
9271 {
9272 *diep = NULL;
9273 *has_children = 0;
9274 return info_ptr;
9275 }
9276
9277 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9278 if (!abbrev)
348e048f
DE
9279 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9280 abbrev_number,
9281 bfd_get_filename (abfd));
9282
93311388
DE
9283 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9284 die->offset = offset;
9285 die->tag = abbrev->tag;
9286 die->abbrev = abbrev_number;
9287
9288 die->num_attrs = abbrev->num_attrs;
9289
9290 for (i = 0; i < abbrev->num_attrs; ++i)
9291 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9292 abfd, info_ptr, cu);
9293
9294 *diep = die;
9295 *has_children = abbrev->has_children;
9296 return info_ptr;
9297}
9298
c906108c
SS
9299/* In DWARF version 2, the description of the debugging information is
9300 stored in a separate .debug_abbrev section. Before we read any
9301 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9302 in a hash table. This function also sets flags in CU describing
9303 the data found in the abbrev table. */
c906108c
SS
9304
9305static void
e5fe5e75 9306dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9307{
e5fe5e75 9308 bfd *abfd = cu->objfile->obfd;
e7c27a73 9309 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9310 gdb_byte *abbrev_ptr;
c906108c
SS
9311 struct abbrev_info *cur_abbrev;
9312 unsigned int abbrev_number, bytes_read, abbrev_name;
9313 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9314 struct attr_abbrev *cur_attrs;
9315 unsigned int allocated_attrs;
c906108c 9316
0963b4bd 9317 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9318 obstack_init (&cu->abbrev_obstack);
9319 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9320 (ABBREV_HASH_SIZE
9321 * sizeof (struct abbrev_info *)));
9322 memset (cu->dwarf2_abbrevs, 0,
9323 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9324
be391dca
TT
9325 dwarf2_read_section (dwarf2_per_objfile->objfile,
9326 &dwarf2_per_objfile->abbrev);
dce234bc 9327 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9328 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9329 abbrev_ptr += bytes_read;
9330
f3dd6933
DJ
9331 allocated_attrs = ATTR_ALLOC_CHUNK;
9332 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9333
0963b4bd 9334 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9335 while (abbrev_number)
9336 {
f3dd6933 9337 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9338
9339 /* read in abbrev header */
9340 cur_abbrev->number = abbrev_number;
9341 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9342 abbrev_ptr += bytes_read;
9343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9344 abbrev_ptr += 1;
9345
72bf9492
DJ
9346 if (cur_abbrev->tag == DW_TAG_namespace)
9347 cu->has_namespace_info = 1;
9348
c906108c
SS
9349 /* now read in declarations */
9350 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9351 abbrev_ptr += bytes_read;
9352 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9353 abbrev_ptr += bytes_read;
9354 while (abbrev_name)
9355 {
f3dd6933 9356 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9357 {
f3dd6933
DJ
9358 allocated_attrs += ATTR_ALLOC_CHUNK;
9359 cur_attrs
9360 = xrealloc (cur_attrs, (allocated_attrs
9361 * sizeof (struct attr_abbrev)));
c906108c 9362 }
ae038cb0
DJ
9363
9364 /* Record whether this compilation unit might have
9365 inter-compilation-unit references. If we don't know what form
9366 this attribute will have, then it might potentially be a
9367 DW_FORM_ref_addr, so we conservatively expect inter-CU
9368 references. */
9369
9370 if (abbrev_form == DW_FORM_ref_addr
9371 || abbrev_form == DW_FORM_indirect)
9372 cu->has_form_ref_addr = 1;
9373
f3dd6933
DJ
9374 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9375 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9376 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9377 abbrev_ptr += bytes_read;
9378 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9379 abbrev_ptr += bytes_read;
9380 }
9381
f3dd6933
DJ
9382 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9383 (cur_abbrev->num_attrs
9384 * sizeof (struct attr_abbrev)));
9385 memcpy (cur_abbrev->attrs, cur_attrs,
9386 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9387
c906108c 9388 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9389 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9390 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9391
9392 /* Get next abbreviation.
9393 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9394 always properly terminated with an abbrev number of 0.
9395 Exit loop if we encounter an abbreviation which we have
9396 already read (which means we are about to read the abbreviations
9397 for the next compile unit) or if the end of the abbreviation
9398 table is reached. */
dce234bc
PP
9399 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9400 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9401 break;
9402 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9403 abbrev_ptr += bytes_read;
e7c27a73 9404 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9405 break;
9406 }
f3dd6933
DJ
9407
9408 xfree (cur_attrs);
c906108c
SS
9409}
9410
f3dd6933 9411/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9412
c906108c 9413static void
f3dd6933 9414dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9415{
f3dd6933 9416 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9417
f3dd6933
DJ
9418 obstack_free (&cu->abbrev_obstack, NULL);
9419 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9420}
9421
9422/* Lookup an abbrev_info structure in the abbrev hash table. */
9423
9424static struct abbrev_info *
e7c27a73 9425dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9426{
9427 unsigned int hash_number;
9428 struct abbrev_info *abbrev;
9429
9430 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9431 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9432
9433 while (abbrev)
9434 {
9435 if (abbrev->number == number)
9436 return abbrev;
9437 else
9438 abbrev = abbrev->next;
9439 }
9440 return NULL;
9441}
9442
72bf9492
DJ
9443/* Returns nonzero if TAG represents a type that we might generate a partial
9444 symbol for. */
9445
9446static int
9447is_type_tag_for_partial (int tag)
9448{
9449 switch (tag)
9450 {
9451#if 0
9452 /* Some types that would be reasonable to generate partial symbols for,
9453 that we don't at present. */
9454 case DW_TAG_array_type:
9455 case DW_TAG_file_type:
9456 case DW_TAG_ptr_to_member_type:
9457 case DW_TAG_set_type:
9458 case DW_TAG_string_type:
9459 case DW_TAG_subroutine_type:
9460#endif
9461 case DW_TAG_base_type:
9462 case DW_TAG_class_type:
680b30c7 9463 case DW_TAG_interface_type:
72bf9492
DJ
9464 case DW_TAG_enumeration_type:
9465 case DW_TAG_structure_type:
9466 case DW_TAG_subrange_type:
9467 case DW_TAG_typedef:
9468 case DW_TAG_union_type:
9469 return 1;
9470 default:
9471 return 0;
9472 }
9473}
9474
9475/* Load all DIEs that are interesting for partial symbols into memory. */
9476
9477static struct partial_die_info *
93311388
DE
9478load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9479 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9480{
bb5ed363 9481 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9482 struct partial_die_info *part_die;
9483 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9484 struct abbrev_info *abbrev;
9485 unsigned int bytes_read;
5afb4e99 9486 unsigned int load_all = 0;
72bf9492
DJ
9487
9488 int nesting_level = 1;
9489
9490 parent_die = NULL;
9491 last_die = NULL;
9492
5afb4e99
DJ
9493 if (cu->per_cu && cu->per_cu->load_all_dies)
9494 load_all = 1;
9495
72bf9492
DJ
9496 cu->partial_dies
9497 = htab_create_alloc_ex (cu->header.length / 12,
9498 partial_die_hash,
9499 partial_die_eq,
9500 NULL,
9501 &cu->comp_unit_obstack,
9502 hashtab_obstack_allocate,
9503 dummy_obstack_deallocate);
9504
9505 part_die = obstack_alloc (&cu->comp_unit_obstack,
9506 sizeof (struct partial_die_info));
9507
9508 while (1)
9509 {
9510 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9511
9512 /* A NULL abbrev means the end of a series of children. */
9513 if (abbrev == NULL)
9514 {
9515 if (--nesting_level == 0)
9516 {
9517 /* PART_DIE was probably the last thing allocated on the
9518 comp_unit_obstack, so we could call obstack_free
9519 here. We don't do that because the waste is small,
9520 and will be cleaned up when we're done with this
9521 compilation unit. This way, we're also more robust
9522 against other users of the comp_unit_obstack. */
9523 return first_die;
9524 }
9525 info_ptr += bytes_read;
9526 last_die = parent_die;
9527 parent_die = parent_die->die_parent;
9528 continue;
9529 }
9530
98bfdba5
PA
9531 /* Check for template arguments. We never save these; if
9532 they're seen, we just mark the parent, and go on our way. */
9533 if (parent_die != NULL
9534 && cu->language == language_cplus
9535 && (abbrev->tag == DW_TAG_template_type_param
9536 || abbrev->tag == DW_TAG_template_value_param))
9537 {
9538 parent_die->has_template_arguments = 1;
9539
9540 if (!load_all)
9541 {
9542 /* We don't need a partial DIE for the template argument. */
9543 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9544 cu);
9545 continue;
9546 }
9547 }
9548
9549 /* We only recurse into subprograms looking for template arguments.
9550 Skip their other children. */
9551 if (!load_all
9552 && cu->language == language_cplus
9553 && parent_die != NULL
9554 && parent_die->tag == DW_TAG_subprogram)
9555 {
9556 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9557 continue;
9558 }
9559
5afb4e99
DJ
9560 /* Check whether this DIE is interesting enough to save. Normally
9561 we would not be interested in members here, but there may be
9562 later variables referencing them via DW_AT_specification (for
9563 static members). */
9564 if (!load_all
9565 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9566 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9567 && abbrev->tag != DW_TAG_enumerator
9568 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9569 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9570 && abbrev->tag != DW_TAG_variable
5afb4e99 9571 && abbrev->tag != DW_TAG_namespace
f55ee35c 9572 && abbrev->tag != DW_TAG_module
5afb4e99 9573 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9574 {
9575 /* Otherwise we skip to the next sibling, if any. */
93311388 9576 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9577 continue;
9578 }
9579
93311388
DE
9580 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9581 buffer, info_ptr, cu);
72bf9492
DJ
9582
9583 /* This two-pass algorithm for processing partial symbols has a
9584 high cost in cache pressure. Thus, handle some simple cases
9585 here which cover the majority of C partial symbols. DIEs
9586 which neither have specification tags in them, nor could have
9587 specification tags elsewhere pointing at them, can simply be
9588 processed and discarded.
9589
9590 This segment is also optional; scan_partial_symbols and
9591 add_partial_symbol will handle these DIEs if we chain
9592 them in normally. When compilers which do not emit large
9593 quantities of duplicate debug information are more common,
9594 this code can probably be removed. */
9595
9596 /* Any complete simple types at the top level (pretty much all
9597 of them, for a language without namespaces), can be processed
9598 directly. */
9599 if (parent_die == NULL
9600 && part_die->has_specification == 0
9601 && part_die->is_declaration == 0
d8228535 9602 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9603 || part_die->tag == DW_TAG_base_type
9604 || part_die->tag == DW_TAG_subrange_type))
9605 {
9606 if (building_psymtab && part_die->name != NULL)
04a679b8 9607 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9608 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9609 &objfile->static_psymbols,
9610 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9611 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9612 continue;
9613 }
9614
d8228535
JK
9615 /* The exception for DW_TAG_typedef with has_children above is
9616 a workaround of GCC PR debug/47510. In the case of this complaint
9617 type_name_no_tag_or_error will error on such types later.
9618
9619 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9620 it could not find the child DIEs referenced later, this is checked
9621 above. In correct DWARF DW_TAG_typedef should have no children. */
9622
9623 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9624 complaint (&symfile_complaints,
9625 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9626 "- DIE at 0x%x [in module %s]"),
bb5ed363 9627 part_die->offset, objfile->name);
d8228535 9628
72bf9492
DJ
9629 /* If we're at the second level, and we're an enumerator, and
9630 our parent has no specification (meaning possibly lives in a
9631 namespace elsewhere), then we can add the partial symbol now
9632 instead of queueing it. */
9633 if (part_die->tag == DW_TAG_enumerator
9634 && parent_die != NULL
9635 && parent_die->die_parent == NULL
9636 && parent_die->tag == DW_TAG_enumeration_type
9637 && parent_die->has_specification == 0)
9638 {
9639 if (part_die->name == NULL)
3e43a32a
MS
9640 complaint (&symfile_complaints,
9641 _("malformed enumerator DIE ignored"));
72bf9492 9642 else if (building_psymtab)
04a679b8 9643 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9644 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9645 (cu->language == language_cplus
9646 || cu->language == language_java)
bb5ed363
DE
9647 ? &objfile->global_psymbols
9648 : &objfile->static_psymbols,
9649 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9650
93311388 9651 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9652 continue;
9653 }
9654
9655 /* We'll save this DIE so link it in. */
9656 part_die->die_parent = parent_die;
9657 part_die->die_sibling = NULL;
9658 part_die->die_child = NULL;
9659
9660 if (last_die && last_die == parent_die)
9661 last_die->die_child = part_die;
9662 else if (last_die)
9663 last_die->die_sibling = part_die;
9664
9665 last_die = part_die;
9666
9667 if (first_die == NULL)
9668 first_die = part_die;
9669
9670 /* Maybe add the DIE to the hash table. Not all DIEs that we
9671 find interesting need to be in the hash table, because we
9672 also have the parent/sibling/child chains; only those that we
9673 might refer to by offset later during partial symbol reading.
9674
9675 For now this means things that might have be the target of a
9676 DW_AT_specification, DW_AT_abstract_origin, or
9677 DW_AT_extension. DW_AT_extension will refer only to
9678 namespaces; DW_AT_abstract_origin refers to functions (and
9679 many things under the function DIE, but we do not recurse
9680 into function DIEs during partial symbol reading) and
9681 possibly variables as well; DW_AT_specification refers to
9682 declarations. Declarations ought to have the DW_AT_declaration
9683 flag. It happens that GCC forgets to put it in sometimes, but
9684 only for functions, not for types.
9685
9686 Adding more things than necessary to the hash table is harmless
9687 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9688 wasted time in find_partial_die, when we reread the compilation
9689 unit with load_all_dies set. */
72bf9492 9690
5afb4e99 9691 if (load_all
72929c62 9692 || abbrev->tag == DW_TAG_constant
5afb4e99 9693 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9694 || abbrev->tag == DW_TAG_variable
9695 || abbrev->tag == DW_TAG_namespace
9696 || part_die->is_declaration)
9697 {
9698 void **slot;
9699
9700 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9701 part_die->offset, INSERT);
9702 *slot = part_die;
9703 }
9704
9705 part_die = obstack_alloc (&cu->comp_unit_obstack,
9706 sizeof (struct partial_die_info));
9707
9708 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9709 we have no reason to follow the children of structures; for other
98bfdba5
PA
9710 languages we have to, so that we can get at method physnames
9711 to infer fully qualified class names, for DW_AT_specification,
9712 and for C++ template arguments. For C++, we also look one level
9713 inside functions to find template arguments (if the name of the
9714 function does not already contain the template arguments).
bc30ff58
JB
9715
9716 For Ada, we need to scan the children of subprograms and lexical
9717 blocks as well because Ada allows the definition of nested
9718 entities that could be interesting for the debugger, such as
9719 nested subprograms for instance. */
72bf9492 9720 if (last_die->has_children
5afb4e99
DJ
9721 && (load_all
9722 || last_die->tag == DW_TAG_namespace
f55ee35c 9723 || last_die->tag == DW_TAG_module
72bf9492 9724 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9725 || (cu->language == language_cplus
9726 && last_die->tag == DW_TAG_subprogram
9727 && (last_die->name == NULL
9728 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9729 || (cu->language != language_c
9730 && (last_die->tag == DW_TAG_class_type
680b30c7 9731 || last_die->tag == DW_TAG_interface_type
72bf9492 9732 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9733 || last_die->tag == DW_TAG_union_type))
9734 || (cu->language == language_ada
9735 && (last_die->tag == DW_TAG_subprogram
9736 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9737 {
9738 nesting_level++;
9739 parent_die = last_die;
9740 continue;
9741 }
9742
9743 /* Otherwise we skip to the next sibling, if any. */
93311388 9744 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9745
9746 /* Back to the top, do it again. */
9747 }
9748}
9749
c906108c
SS
9750/* Read a minimal amount of information into the minimal die structure. */
9751
fe1b8b76 9752static gdb_byte *
72bf9492
DJ
9753read_partial_die (struct partial_die_info *part_die,
9754 struct abbrev_info *abbrev,
9755 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9756 gdb_byte *buffer, gdb_byte *info_ptr,
9757 struct dwarf2_cu *cu)
c906108c 9758{
bb5ed363 9759 struct objfile *objfile = cu->objfile;
fa238c03 9760 unsigned int i;
c906108c 9761 struct attribute attr;
c5aa993b 9762 int has_low_pc_attr = 0;
c906108c
SS
9763 int has_high_pc_attr = 0;
9764
72bf9492 9765 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9766
93311388 9767 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9768
9769 info_ptr += abbrev_len;
9770
9771 if (abbrev == NULL)
9772 return info_ptr;
9773
c906108c
SS
9774 part_die->tag = abbrev->tag;
9775 part_die->has_children = abbrev->has_children;
c906108c
SS
9776
9777 for (i = 0; i < abbrev->num_attrs; ++i)
9778 {
e7c27a73 9779 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9780
9781 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9782 partial symbol table. */
c906108c
SS
9783 switch (attr.name)
9784 {
9785 case DW_AT_name:
71c25dea
TT
9786 switch (part_die->tag)
9787 {
9788 case DW_TAG_compile_unit:
348e048f 9789 case DW_TAG_type_unit:
71c25dea
TT
9790 /* Compilation units have a DW_AT_name that is a filename, not
9791 a source language identifier. */
9792 case DW_TAG_enumeration_type:
9793 case DW_TAG_enumerator:
9794 /* These tags always have simple identifiers already; no need
9795 to canonicalize them. */
9796 part_die->name = DW_STRING (&attr);
9797 break;
9798 default:
9799 part_die->name
9800 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9801 &objfile->objfile_obstack);
71c25dea
TT
9802 break;
9803 }
c906108c 9804 break;
31ef98ae 9805 case DW_AT_linkage_name:
c906108c 9806 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9807 /* Note that both forms of linkage name might appear. We
9808 assume they will be the same, and we only store the last
9809 one we see. */
94af9270
KS
9810 if (cu->language == language_ada)
9811 part_die->name = DW_STRING (&attr);
abc72ce4 9812 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9813 break;
9814 case DW_AT_low_pc:
9815 has_low_pc_attr = 1;
9816 part_die->lowpc = DW_ADDR (&attr);
9817 break;
9818 case DW_AT_high_pc:
9819 has_high_pc_attr = 1;
9820 part_die->highpc = DW_ADDR (&attr);
9821 break;
9822 case DW_AT_location:
0963b4bd 9823 /* Support the .debug_loc offsets. */
8e19ed76
PS
9824 if (attr_form_is_block (&attr))
9825 {
9826 part_die->locdesc = DW_BLOCK (&attr);
9827 }
3690dd37 9828 else if (attr_form_is_section_offset (&attr))
8e19ed76 9829 {
4d3c2250 9830 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9831 }
9832 else
9833 {
4d3c2250
KB
9834 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9835 "partial symbol information");
8e19ed76 9836 }
c906108c 9837 break;
c906108c
SS
9838 case DW_AT_external:
9839 part_die->is_external = DW_UNSND (&attr);
9840 break;
9841 case DW_AT_declaration:
9842 part_die->is_declaration = DW_UNSND (&attr);
9843 break;
9844 case DW_AT_type:
9845 part_die->has_type = 1;
9846 break;
9847 case DW_AT_abstract_origin:
9848 case DW_AT_specification:
72bf9492
DJ
9849 case DW_AT_extension:
9850 part_die->has_specification = 1;
c764a876 9851 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9852 break;
9853 case DW_AT_sibling:
9854 /* Ignore absolute siblings, they might point outside of
9855 the current compile unit. */
9856 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9857 complaint (&symfile_complaints,
9858 _("ignoring absolute DW_AT_sibling"));
c906108c 9859 else
93311388 9860 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9861 break;
fa4028e9
JB
9862 case DW_AT_byte_size:
9863 part_die->has_byte_size = 1;
9864 break;
68511cec
CES
9865 case DW_AT_calling_convention:
9866 /* DWARF doesn't provide a way to identify a program's source-level
9867 entry point. DW_AT_calling_convention attributes are only meant
9868 to describe functions' calling conventions.
9869
9870 However, because it's a necessary piece of information in
9871 Fortran, and because DW_CC_program is the only piece of debugging
9872 information whose definition refers to a 'main program' at all,
9873 several compilers have begun marking Fortran main programs with
9874 DW_CC_program --- even when those functions use the standard
9875 calling conventions.
9876
9877 So until DWARF specifies a way to provide this information and
9878 compilers pick up the new representation, we'll support this
9879 practice. */
9880 if (DW_UNSND (&attr) == DW_CC_program
9881 && cu->language == language_fortran)
01f8c46d
JK
9882 {
9883 set_main_name (part_die->name);
9884
9885 /* As this DIE has a static linkage the name would be difficult
9886 to look up later. */
9887 language_of_main = language_fortran;
9888 }
68511cec 9889 break;
c906108c
SS
9890 default:
9891 break;
9892 }
9893 }
9894
9373cf26
JK
9895 if (has_low_pc_attr && has_high_pc_attr)
9896 {
9897 /* When using the GNU linker, .gnu.linkonce. sections are used to
9898 eliminate duplicate copies of functions and vtables and such.
9899 The linker will arbitrarily choose one and discard the others.
9900 The AT_*_pc values for such functions refer to local labels in
9901 these sections. If the section from that file was discarded, the
9902 labels are not in the output, so the relocs get a value of 0.
9903 If this is a discarded function, mark the pc bounds as invalid,
9904 so that GDB will ignore it. */
9905 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9906 {
bb5ed363 9907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9908
9909 complaint (&symfile_complaints,
9910 _("DW_AT_low_pc %s is zero "
9911 "for DIE at 0x%x [in module %s]"),
9912 paddress (gdbarch, part_die->lowpc),
bb5ed363 9913 part_die->offset, objfile->name);
9373cf26
JK
9914 }
9915 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9916 else if (part_die->lowpc >= part_die->highpc)
9917 {
bb5ed363 9918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9919
9920 complaint (&symfile_complaints,
9921 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9922 "for DIE at 0x%x [in module %s]"),
9923 paddress (gdbarch, part_die->lowpc),
9924 paddress (gdbarch, part_die->highpc),
bb5ed363 9925 part_die->offset, objfile->name);
9373cf26
JK
9926 }
9927 else
9928 part_die->has_pc_info = 1;
9929 }
85cbf3d3 9930
c906108c
SS
9931 return info_ptr;
9932}
9933
72bf9492
DJ
9934/* Find a cached partial DIE at OFFSET in CU. */
9935
9936static struct partial_die_info *
c764a876 9937find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9938{
9939 struct partial_die_info *lookup_die = NULL;
9940 struct partial_die_info part_die;
9941
9942 part_die.offset = offset;
9943 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9944
72bf9492
DJ
9945 return lookup_die;
9946}
9947
348e048f
DE
9948/* Find a partial DIE at OFFSET, which may or may not be in CU,
9949 except in the case of .debug_types DIEs which do not reference
9950 outside their CU (they do however referencing other types via
55f1336d 9951 DW_FORM_ref_sig8). */
72bf9492
DJ
9952
9953static struct partial_die_info *
c764a876 9954find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9955{
bb5ed363 9956 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9957 struct dwarf2_per_cu_data *per_cu = NULL;
9958 struct partial_die_info *pd = NULL;
72bf9492 9959
b0df02fd 9960 if (cu->per_cu->debug_types_section)
348e048f
DE
9961 {
9962 pd = find_partial_die_in_comp_unit (offset, cu);
9963 if (pd != NULL)
9964 return pd;
9965 goto not_found;
9966 }
9967
45452591 9968 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9969 {
9970 pd = find_partial_die_in_comp_unit (offset, cu);
9971 if (pd != NULL)
9972 return pd;
9973 }
72bf9492 9974
bb5ed363 9975 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 9976
98bfdba5 9977 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 9978 load_partial_comp_unit (per_cu);
ae038cb0
DJ
9979
9980 per_cu->cu->last_used = 0;
5afb4e99
DJ
9981 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9982
9983 if (pd == NULL && per_cu->load_all_dies == 0)
9984 {
9985 struct cleanup *back_to;
9986 struct partial_die_info comp_unit_die;
9987 struct abbrev_info *abbrev;
9988 unsigned int bytes_read;
9989 char *info_ptr;
9990
9991 per_cu->load_all_dies = 1;
9992
9993 /* Re-read the DIEs. */
9994 back_to = make_cleanup (null_cleanup, 0);
9995 if (per_cu->cu->dwarf2_abbrevs == NULL)
9996 {
e5fe5e75 9997 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 9998 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9999 }
dce234bc 10000 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
10001 + per_cu->cu->header.offset
10002 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10003 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10004 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 10005 objfile->obfd,
93311388 10006 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10007 per_cu->cu);
10008 if (comp_unit_die.has_children)
bb5ed363 10009 load_partial_dies (objfile->obfd,
93311388
DE
10010 dwarf2_per_objfile->info.buffer, info_ptr,
10011 0, per_cu->cu);
5afb4e99
DJ
10012 do_cleanups (back_to);
10013
10014 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10015 }
10016
348e048f
DE
10017 not_found:
10018
5afb4e99
DJ
10019 if (pd == NULL)
10020 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10021 _("could not find partial DIE 0x%x "
10022 "in cache [from module %s]\n"),
bb5ed363 10023 offset, bfd_get_filename (objfile->obfd));
5afb4e99 10024 return pd;
72bf9492
DJ
10025}
10026
abc72ce4
DE
10027/* See if we can figure out if the class lives in a namespace. We do
10028 this by looking for a member function; its demangled name will
10029 contain namespace info, if there is any. */
10030
10031static void
10032guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10033 struct dwarf2_cu *cu)
10034{
10035 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10036 what template types look like, because the demangler
10037 frequently doesn't give the same name as the debug info. We
10038 could fix this by only using the demangled name to get the
10039 prefix (but see comment in read_structure_type). */
10040
10041 struct partial_die_info *real_pdi;
10042 struct partial_die_info *child_pdi;
10043
10044 /* If this DIE (this DIE's specification, if any) has a parent, then
10045 we should not do this. We'll prepend the parent's fully qualified
10046 name when we create the partial symbol. */
10047
10048 real_pdi = struct_pdi;
10049 while (real_pdi->has_specification)
10050 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10051
10052 if (real_pdi->die_parent != NULL)
10053 return;
10054
10055 for (child_pdi = struct_pdi->die_child;
10056 child_pdi != NULL;
10057 child_pdi = child_pdi->die_sibling)
10058 {
10059 if (child_pdi->tag == DW_TAG_subprogram
10060 && child_pdi->linkage_name != NULL)
10061 {
10062 char *actual_class_name
10063 = language_class_name_from_physname (cu->language_defn,
10064 child_pdi->linkage_name);
10065 if (actual_class_name != NULL)
10066 {
10067 struct_pdi->name
10068 = obsavestring (actual_class_name,
10069 strlen (actual_class_name),
10070 &cu->objfile->objfile_obstack);
10071 xfree (actual_class_name);
10072 }
10073 break;
10074 }
10075 }
10076}
10077
72bf9492
DJ
10078/* Adjust PART_DIE before generating a symbol for it. This function
10079 may set the is_external flag or change the DIE's name. */
10080
10081static void
10082fixup_partial_die (struct partial_die_info *part_die,
10083 struct dwarf2_cu *cu)
10084{
abc72ce4
DE
10085 /* Once we've fixed up a die, there's no point in doing so again.
10086 This also avoids a memory leak if we were to call
10087 guess_partial_die_structure_name multiple times. */
10088 if (part_die->fixup_called)
10089 return;
10090
72bf9492
DJ
10091 /* If we found a reference attribute and the DIE has no name, try
10092 to find a name in the referred to DIE. */
10093
10094 if (part_die->name == NULL && part_die->has_specification)
10095 {
10096 struct partial_die_info *spec_die;
72bf9492 10097
10b3939b 10098 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10099
10b3939b 10100 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10101
10102 if (spec_die->name)
10103 {
10104 part_die->name = spec_die->name;
10105
10106 /* Copy DW_AT_external attribute if it is set. */
10107 if (spec_die->is_external)
10108 part_die->is_external = spec_die->is_external;
10109 }
10110 }
10111
10112 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10113
10114 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10115 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10116
abc72ce4
DE
10117 /* If there is no parent die to provide a namespace, and there are
10118 children, see if we can determine the namespace from their linkage
10119 name.
10120 NOTE: We need to do this even if cu->has_namespace_info != 0.
10121 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10122 if (cu->language == language_cplus
8b70b953 10123 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10124 && part_die->die_parent == NULL
10125 && part_die->has_children
10126 && (part_die->tag == DW_TAG_class_type
10127 || part_die->tag == DW_TAG_structure_type
10128 || part_die->tag == DW_TAG_union_type))
10129 guess_partial_die_structure_name (part_die, cu);
10130
53832f31
TT
10131 /* GCC might emit a nameless struct or union that has a linkage
10132 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10133 if (part_die->name == NULL
96408a79
SA
10134 && (part_die->tag == DW_TAG_class_type
10135 || part_die->tag == DW_TAG_interface_type
10136 || part_die->tag == DW_TAG_structure_type
10137 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10138 && part_die->linkage_name != NULL)
10139 {
10140 char *demangled;
10141
10142 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10143 if (demangled)
10144 {
96408a79
SA
10145 const char *base;
10146
10147 /* Strip any leading namespaces/classes, keep only the base name.
10148 DW_AT_name for named DIEs does not contain the prefixes. */
10149 base = strrchr (demangled, ':');
10150 if (base && base > demangled && base[-1] == ':')
10151 base++;
10152 else
10153 base = demangled;
10154
10155 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10156 &cu->objfile->objfile_obstack);
10157 xfree (demangled);
10158 }
10159 }
10160
abc72ce4 10161 part_die->fixup_called = 1;
72bf9492
DJ
10162}
10163
a8329558 10164/* Read an attribute value described by an attribute form. */
c906108c 10165
fe1b8b76 10166static gdb_byte *
a8329558 10167read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10168 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10169 struct dwarf2_cu *cu)
c906108c 10170{
e7c27a73 10171 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10172 unsigned int bytes_read;
10173 struct dwarf_block *blk;
10174
a8329558
KW
10175 attr->form = form;
10176 switch (form)
c906108c 10177 {
c906108c 10178 case DW_FORM_ref_addr:
ae411497
TT
10179 if (cu->header.version == 2)
10180 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10181 else
3e43a32a
MS
10182 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10183 &cu->header, &bytes_read);
ae411497
TT
10184 info_ptr += bytes_read;
10185 break;
10186 case DW_FORM_addr:
e7c27a73 10187 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10188 info_ptr += bytes_read;
c906108c
SS
10189 break;
10190 case DW_FORM_block2:
7b5a2f43 10191 blk = dwarf_alloc_block (cu);
c906108c
SS
10192 blk->size = read_2_bytes (abfd, info_ptr);
10193 info_ptr += 2;
10194 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10195 info_ptr += blk->size;
10196 DW_BLOCK (attr) = blk;
10197 break;
10198 case DW_FORM_block4:
7b5a2f43 10199 blk = dwarf_alloc_block (cu);
c906108c
SS
10200 blk->size = read_4_bytes (abfd, info_ptr);
10201 info_ptr += 4;
10202 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10203 info_ptr += blk->size;
10204 DW_BLOCK (attr) = blk;
10205 break;
10206 case DW_FORM_data2:
10207 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10208 info_ptr += 2;
10209 break;
10210 case DW_FORM_data4:
10211 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10212 info_ptr += 4;
10213 break;
10214 case DW_FORM_data8:
10215 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10216 info_ptr += 8;
10217 break;
2dc7f7b3
TT
10218 case DW_FORM_sec_offset:
10219 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10220 info_ptr += bytes_read;
10221 break;
c906108c 10222 case DW_FORM_string:
9b1c24c8 10223 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10224 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10225 info_ptr += bytes_read;
10226 break;
4bdf3d34
JJ
10227 case DW_FORM_strp:
10228 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10229 &bytes_read);
8285870a 10230 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10231 info_ptr += bytes_read;
10232 break;
2dc7f7b3 10233 case DW_FORM_exprloc:
c906108c 10234 case DW_FORM_block:
7b5a2f43 10235 blk = dwarf_alloc_block (cu);
c906108c
SS
10236 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10237 info_ptr += bytes_read;
10238 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10239 info_ptr += blk->size;
10240 DW_BLOCK (attr) = blk;
10241 break;
10242 case DW_FORM_block1:
7b5a2f43 10243 blk = dwarf_alloc_block (cu);
c906108c
SS
10244 blk->size = read_1_byte (abfd, info_ptr);
10245 info_ptr += 1;
10246 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10247 info_ptr += blk->size;
10248 DW_BLOCK (attr) = blk;
10249 break;
10250 case DW_FORM_data1:
10251 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10252 info_ptr += 1;
10253 break;
10254 case DW_FORM_flag:
10255 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10256 info_ptr += 1;
10257 break;
2dc7f7b3
TT
10258 case DW_FORM_flag_present:
10259 DW_UNSND (attr) = 1;
10260 break;
c906108c
SS
10261 case DW_FORM_sdata:
10262 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10263 info_ptr += bytes_read;
10264 break;
10265 case DW_FORM_udata:
10266 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10267 info_ptr += bytes_read;
10268 break;
10269 case DW_FORM_ref1:
10b3939b 10270 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10271 info_ptr += 1;
10272 break;
10273 case DW_FORM_ref2:
10b3939b 10274 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10275 info_ptr += 2;
10276 break;
10277 case DW_FORM_ref4:
10b3939b 10278 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10279 info_ptr += 4;
10280 break;
613e1657 10281 case DW_FORM_ref8:
10b3939b 10282 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10283 info_ptr += 8;
10284 break;
55f1336d 10285 case DW_FORM_ref_sig8:
348e048f
DE
10286 /* Convert the signature to something we can record in DW_UNSND
10287 for later lookup.
10288 NOTE: This is NULL if the type wasn't found. */
10289 DW_SIGNATURED_TYPE (attr) =
10290 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10291 info_ptr += 8;
10292 break;
c906108c 10293 case DW_FORM_ref_udata:
10b3939b
DJ
10294 DW_ADDR (attr) = (cu->header.offset
10295 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10296 info_ptr += bytes_read;
10297 break;
c906108c 10298 case DW_FORM_indirect:
a8329558
KW
10299 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10300 info_ptr += bytes_read;
e7c27a73 10301 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10302 break;
c906108c 10303 default:
8a3fe4f8 10304 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10305 dwarf_form_name (form),
10306 bfd_get_filename (abfd));
c906108c 10307 }
28e94949
JB
10308
10309 /* We have seen instances where the compiler tried to emit a byte
10310 size attribute of -1 which ended up being encoded as an unsigned
10311 0xffffffff. Although 0xffffffff is technically a valid size value,
10312 an object of this size seems pretty unlikely so we can relatively
10313 safely treat these cases as if the size attribute was invalid and
10314 treat them as zero by default. */
10315 if (attr->name == DW_AT_byte_size
10316 && form == DW_FORM_data4
10317 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10318 {
10319 complaint
10320 (&symfile_complaints,
43bbcdc2
PH
10321 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10322 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10323 DW_UNSND (attr) = 0;
10324 }
28e94949 10325
c906108c
SS
10326 return info_ptr;
10327}
10328
a8329558
KW
10329/* Read an attribute described by an abbreviated attribute. */
10330
fe1b8b76 10331static gdb_byte *
a8329558 10332read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10333 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10334{
10335 attr->name = abbrev->name;
e7c27a73 10336 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10337}
10338
0963b4bd 10339/* Read dwarf information from a buffer. */
c906108c
SS
10340
10341static unsigned int
fe1b8b76 10342read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10343{
fe1b8b76 10344 return bfd_get_8 (abfd, buf);
c906108c
SS
10345}
10346
10347static int
fe1b8b76 10348read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10349{
fe1b8b76 10350 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10351}
10352
10353static unsigned int
fe1b8b76 10354read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10355{
fe1b8b76 10356 return bfd_get_16 (abfd, buf);
c906108c
SS
10357}
10358
21ae7a4d
JK
10359static int
10360read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10361{
10362 return bfd_get_signed_16 (abfd, buf);
10363}
10364
c906108c 10365static unsigned int
fe1b8b76 10366read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10367{
fe1b8b76 10368 return bfd_get_32 (abfd, buf);
c906108c
SS
10369}
10370
21ae7a4d
JK
10371static int
10372read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10373{
10374 return bfd_get_signed_32 (abfd, buf);
10375}
10376
93311388 10377static ULONGEST
fe1b8b76 10378read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10379{
fe1b8b76 10380 return bfd_get_64 (abfd, buf);
c906108c
SS
10381}
10382
10383static CORE_ADDR
fe1b8b76 10384read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10385 unsigned int *bytes_read)
c906108c 10386{
e7c27a73 10387 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10388 CORE_ADDR retval = 0;
10389
107d2387 10390 if (cu_header->signed_addr_p)
c906108c 10391 {
107d2387
AC
10392 switch (cu_header->addr_size)
10393 {
10394 case 2:
fe1b8b76 10395 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10396 break;
10397 case 4:
fe1b8b76 10398 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10399 break;
10400 case 8:
fe1b8b76 10401 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10402 break;
10403 default:
8e65ff28 10404 internal_error (__FILE__, __LINE__,
e2e0b3e5 10405 _("read_address: bad switch, signed [in module %s]"),
659b0389 10406 bfd_get_filename (abfd));
107d2387
AC
10407 }
10408 }
10409 else
10410 {
10411 switch (cu_header->addr_size)
10412 {
10413 case 2:
fe1b8b76 10414 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10415 break;
10416 case 4:
fe1b8b76 10417 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10418 break;
10419 case 8:
fe1b8b76 10420 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10421 break;
10422 default:
8e65ff28 10423 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10424 _("read_address: bad switch, "
10425 "unsigned [in module %s]"),
659b0389 10426 bfd_get_filename (abfd));
107d2387 10427 }
c906108c 10428 }
64367e0a 10429
107d2387
AC
10430 *bytes_read = cu_header->addr_size;
10431 return retval;
c906108c
SS
10432}
10433
f7ef9339 10434/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10435 specification allows the initial length to take up either 4 bytes
10436 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10437 bytes describe the length and all offsets will be 8 bytes in length
10438 instead of 4.
10439
f7ef9339
KB
10440 An older, non-standard 64-bit format is also handled by this
10441 function. The older format in question stores the initial length
10442 as an 8-byte quantity without an escape value. Lengths greater
10443 than 2^32 aren't very common which means that the initial 4 bytes
10444 is almost always zero. Since a length value of zero doesn't make
10445 sense for the 32-bit format, this initial zero can be considered to
10446 be an escape value which indicates the presence of the older 64-bit
10447 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10448 greater than 4GB. If it becomes necessary to handle lengths
10449 somewhat larger than 4GB, we could allow other small values (such
10450 as the non-sensical values of 1, 2, and 3) to also be used as
10451 escape values indicating the presence of the old format.
f7ef9339 10452
917c78fc
MK
10453 The value returned via bytes_read should be used to increment the
10454 relevant pointer after calling read_initial_length().
c764a876 10455
613e1657
KB
10456 [ Note: read_initial_length() and read_offset() are based on the
10457 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10458 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10459 from:
10460
f7ef9339 10461 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10462
613e1657
KB
10463 This document is only a draft and is subject to change. (So beware.)
10464
f7ef9339 10465 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10466 determined empirically by examining 64-bit ELF files produced by
10467 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10468
10469 - Kevin, July 16, 2002
613e1657
KB
10470 ] */
10471
10472static LONGEST
c764a876 10473read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10474{
fe1b8b76 10475 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10476
dd373385 10477 if (length == 0xffffffff)
613e1657 10478 {
fe1b8b76 10479 length = bfd_get_64 (abfd, buf + 4);
613e1657 10480 *bytes_read = 12;
613e1657 10481 }
dd373385 10482 else if (length == 0)
f7ef9339 10483 {
dd373385 10484 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10485 length = bfd_get_64 (abfd, buf);
f7ef9339 10486 *bytes_read = 8;
f7ef9339 10487 }
613e1657
KB
10488 else
10489 {
10490 *bytes_read = 4;
613e1657
KB
10491 }
10492
c764a876
DE
10493 return length;
10494}
dd373385 10495
c764a876
DE
10496/* Cover function for read_initial_length.
10497 Returns the length of the object at BUF, and stores the size of the
10498 initial length in *BYTES_READ and stores the size that offsets will be in
10499 *OFFSET_SIZE.
10500 If the initial length size is not equivalent to that specified in
10501 CU_HEADER then issue a complaint.
10502 This is useful when reading non-comp-unit headers. */
dd373385 10503
c764a876
DE
10504static LONGEST
10505read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10506 const struct comp_unit_head *cu_header,
10507 unsigned int *bytes_read,
10508 unsigned int *offset_size)
10509{
10510 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10511
10512 gdb_assert (cu_header->initial_length_size == 4
10513 || cu_header->initial_length_size == 8
10514 || cu_header->initial_length_size == 12);
10515
10516 if (cu_header->initial_length_size != *bytes_read)
10517 complaint (&symfile_complaints,
10518 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10519
c764a876 10520 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10521 return length;
613e1657
KB
10522}
10523
10524/* Read an offset from the data stream. The size of the offset is
917c78fc 10525 given by cu_header->offset_size. */
613e1657
KB
10526
10527static LONGEST
fe1b8b76 10528read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10529 unsigned int *bytes_read)
c764a876
DE
10530{
10531 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10532
c764a876
DE
10533 *bytes_read = cu_header->offset_size;
10534 return offset;
10535}
10536
10537/* Read an offset from the data stream. */
10538
10539static LONGEST
10540read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10541{
10542 LONGEST retval = 0;
10543
c764a876 10544 switch (offset_size)
613e1657
KB
10545 {
10546 case 4:
fe1b8b76 10547 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10548 break;
10549 case 8:
fe1b8b76 10550 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10551 break;
10552 default:
8e65ff28 10553 internal_error (__FILE__, __LINE__,
c764a876 10554 _("read_offset_1: bad switch [in module %s]"),
659b0389 10555 bfd_get_filename (abfd));
613e1657
KB
10556 }
10557
917c78fc 10558 return retval;
613e1657
KB
10559}
10560
fe1b8b76
JB
10561static gdb_byte *
10562read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10563{
10564 /* If the size of a host char is 8 bits, we can return a pointer
10565 to the buffer, otherwise we have to copy the data to a buffer
10566 allocated on the temporary obstack. */
4bdf3d34 10567 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10568 return buf;
c906108c
SS
10569}
10570
10571static char *
9b1c24c8 10572read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10573{
10574 /* If the size of a host char is 8 bits, we can return a pointer
10575 to the string, otherwise we have to copy the string to a buffer
10576 allocated on the temporary obstack. */
4bdf3d34 10577 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10578 if (*buf == '\0')
10579 {
10580 *bytes_read_ptr = 1;
10581 return NULL;
10582 }
fe1b8b76
JB
10583 *bytes_read_ptr = strlen ((char *) buf) + 1;
10584 return (char *) buf;
4bdf3d34
JJ
10585}
10586
10587static char *
cf2c3c16 10588read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10589{
be391dca 10590 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10591 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10592 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10593 bfd_get_filename (abfd));
dce234bc 10594 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10595 error (_("DW_FORM_strp pointing outside of "
10596 ".debug_str section [in module %s]"),
10597 bfd_get_filename (abfd));
4bdf3d34 10598 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10599 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10600 return NULL;
dce234bc 10601 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10602}
10603
cf2c3c16
TT
10604static char *
10605read_indirect_string (bfd *abfd, gdb_byte *buf,
10606 const struct comp_unit_head *cu_header,
10607 unsigned int *bytes_read_ptr)
10608{
10609 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10610
10611 return read_indirect_string_at_offset (abfd, str_offset);
10612}
10613
ce5d95e1 10614static unsigned long
fe1b8b76 10615read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10616{
ce5d95e1
JB
10617 unsigned long result;
10618 unsigned int num_read;
c906108c
SS
10619 int i, shift;
10620 unsigned char byte;
10621
10622 result = 0;
10623 shift = 0;
10624 num_read = 0;
10625 i = 0;
10626 while (1)
10627 {
fe1b8b76 10628 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10629 buf++;
10630 num_read++;
ce5d95e1 10631 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10632 if ((byte & 128) == 0)
10633 {
10634 break;
10635 }
10636 shift += 7;
10637 }
10638 *bytes_read_ptr = num_read;
10639 return result;
10640}
10641
ce5d95e1 10642static long
fe1b8b76 10643read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10644{
ce5d95e1 10645 long result;
77e0b926 10646 int i, shift, num_read;
c906108c
SS
10647 unsigned char byte;
10648
10649 result = 0;
10650 shift = 0;
c906108c
SS
10651 num_read = 0;
10652 i = 0;
10653 while (1)
10654 {
fe1b8b76 10655 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10656 buf++;
10657 num_read++;
ce5d95e1 10658 result |= ((long)(byte & 127) << shift);
c906108c
SS
10659 shift += 7;
10660 if ((byte & 128) == 0)
10661 {
10662 break;
10663 }
10664 }
77e0b926
DJ
10665 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10666 result |= -(((long)1) << shift);
c906108c
SS
10667 *bytes_read_ptr = num_read;
10668 return result;
10669}
10670
4bb7a0a7
DJ
10671/* Return a pointer to just past the end of an LEB128 number in BUF. */
10672
fe1b8b76
JB
10673static gdb_byte *
10674skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10675{
10676 int byte;
10677
10678 while (1)
10679 {
fe1b8b76 10680 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10681 buf++;
10682 if ((byte & 128) == 0)
10683 return buf;
10684 }
10685}
10686
c906108c 10687static void
e142c38c 10688set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10689{
10690 switch (lang)
10691 {
10692 case DW_LANG_C89:
76bee0cc 10693 case DW_LANG_C99:
c906108c 10694 case DW_LANG_C:
e142c38c 10695 cu->language = language_c;
c906108c
SS
10696 break;
10697 case DW_LANG_C_plus_plus:
e142c38c 10698 cu->language = language_cplus;
c906108c 10699 break;
6aecb9c2
JB
10700 case DW_LANG_D:
10701 cu->language = language_d;
10702 break;
c906108c
SS
10703 case DW_LANG_Fortran77:
10704 case DW_LANG_Fortran90:
b21b22e0 10705 case DW_LANG_Fortran95:
e142c38c 10706 cu->language = language_fortran;
c906108c
SS
10707 break;
10708 case DW_LANG_Mips_Assembler:
e142c38c 10709 cu->language = language_asm;
c906108c 10710 break;
bebd888e 10711 case DW_LANG_Java:
e142c38c 10712 cu->language = language_java;
bebd888e 10713 break;
c906108c 10714 case DW_LANG_Ada83:
8aaf0b47 10715 case DW_LANG_Ada95:
bc5f45f8
JB
10716 cu->language = language_ada;
10717 break;
72019c9c
GM
10718 case DW_LANG_Modula2:
10719 cu->language = language_m2;
10720 break;
fe8e67fd
PM
10721 case DW_LANG_Pascal83:
10722 cu->language = language_pascal;
10723 break;
22566fbd
DJ
10724 case DW_LANG_ObjC:
10725 cu->language = language_objc;
10726 break;
c906108c
SS
10727 case DW_LANG_Cobol74:
10728 case DW_LANG_Cobol85:
c906108c 10729 default:
e142c38c 10730 cu->language = language_minimal;
c906108c
SS
10731 break;
10732 }
e142c38c 10733 cu->language_defn = language_def (cu->language);
c906108c
SS
10734}
10735
10736/* Return the named attribute or NULL if not there. */
10737
10738static struct attribute *
e142c38c 10739dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10740{
10741 unsigned int i;
10742 struct attribute *spec = NULL;
10743
10744 for (i = 0; i < die->num_attrs; ++i)
10745 {
10746 if (die->attrs[i].name == name)
10b3939b 10747 return &die->attrs[i];
c906108c
SS
10748 if (die->attrs[i].name == DW_AT_specification
10749 || die->attrs[i].name == DW_AT_abstract_origin)
10750 spec = &die->attrs[i];
10751 }
c906108c 10752
10b3939b 10753 if (spec)
f2f0e013
DJ
10754 {
10755 die = follow_die_ref (die, spec, &cu);
10756 return dwarf2_attr (die, name, cu);
10757 }
c5aa993b 10758
c906108c
SS
10759 return NULL;
10760}
10761
348e048f
DE
10762/* Return the named attribute or NULL if not there,
10763 but do not follow DW_AT_specification, etc.
10764 This is for use in contexts where we're reading .debug_types dies.
10765 Following DW_AT_specification, DW_AT_abstract_origin will take us
10766 back up the chain, and we want to go down. */
10767
10768static struct attribute *
10769dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10770 struct dwarf2_cu *cu)
10771{
10772 unsigned int i;
10773
10774 for (i = 0; i < die->num_attrs; ++i)
10775 if (die->attrs[i].name == name)
10776 return &die->attrs[i];
10777
10778 return NULL;
10779}
10780
05cf31d1
JB
10781/* Return non-zero iff the attribute NAME is defined for the given DIE,
10782 and holds a non-zero value. This function should only be used for
2dc7f7b3 10783 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10784
10785static int
10786dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10787{
10788 struct attribute *attr = dwarf2_attr (die, name, cu);
10789
10790 return (attr && DW_UNSND (attr));
10791}
10792
3ca72b44 10793static int
e142c38c 10794die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10795{
05cf31d1
JB
10796 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10797 which value is non-zero. However, we have to be careful with
10798 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10799 (via dwarf2_flag_true_p) follows this attribute. So we may
10800 end up accidently finding a declaration attribute that belongs
10801 to a different DIE referenced by the specification attribute,
10802 even though the given DIE does not have a declaration attribute. */
10803 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10804 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10805}
10806
63d06c5c 10807/* Return the die giving the specification for DIE, if there is
f2f0e013 10808 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10809 containing the return value on output. If there is no
10810 specification, but there is an abstract origin, that is
10811 returned. */
63d06c5c
DC
10812
10813static struct die_info *
f2f0e013 10814die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10815{
f2f0e013
DJ
10816 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10817 *spec_cu);
63d06c5c 10818
edb3359d
DJ
10819 if (spec_attr == NULL)
10820 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10821
63d06c5c
DC
10822 if (spec_attr == NULL)
10823 return NULL;
10824 else
f2f0e013 10825 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10826}
c906108c 10827
debd256d 10828/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10829 refers to.
10830 NOTE: This is also used as a "cleanup" function. */
10831
debd256d
JB
10832static void
10833free_line_header (struct line_header *lh)
10834{
10835 if (lh->standard_opcode_lengths)
a8bc7b56 10836 xfree (lh->standard_opcode_lengths);
debd256d
JB
10837
10838 /* Remember that all the lh->file_names[i].name pointers are
10839 pointers into debug_line_buffer, and don't need to be freed. */
10840 if (lh->file_names)
a8bc7b56 10841 xfree (lh->file_names);
debd256d
JB
10842
10843 /* Similarly for the include directory names. */
10844 if (lh->include_dirs)
a8bc7b56 10845 xfree (lh->include_dirs);
debd256d 10846
a8bc7b56 10847 xfree (lh);
debd256d
JB
10848}
10849
debd256d 10850/* Add an entry to LH's include directory table. */
ae2de4f8 10851
debd256d
JB
10852static void
10853add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10854{
debd256d
JB
10855 /* Grow the array if necessary. */
10856 if (lh->include_dirs_size == 0)
c5aa993b 10857 {
debd256d
JB
10858 lh->include_dirs_size = 1; /* for testing */
10859 lh->include_dirs = xmalloc (lh->include_dirs_size
10860 * sizeof (*lh->include_dirs));
10861 }
10862 else if (lh->num_include_dirs >= lh->include_dirs_size)
10863 {
10864 lh->include_dirs_size *= 2;
10865 lh->include_dirs = xrealloc (lh->include_dirs,
10866 (lh->include_dirs_size
10867 * sizeof (*lh->include_dirs)));
c5aa993b 10868 }
c906108c 10869
debd256d
JB
10870 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10871}
6e70227d 10872
debd256d 10873/* Add an entry to LH's file name table. */
ae2de4f8 10874
debd256d
JB
10875static void
10876add_file_name (struct line_header *lh,
10877 char *name,
10878 unsigned int dir_index,
10879 unsigned int mod_time,
10880 unsigned int length)
10881{
10882 struct file_entry *fe;
10883
10884 /* Grow the array if necessary. */
10885 if (lh->file_names_size == 0)
10886 {
10887 lh->file_names_size = 1; /* for testing */
10888 lh->file_names = xmalloc (lh->file_names_size
10889 * sizeof (*lh->file_names));
10890 }
10891 else if (lh->num_file_names >= lh->file_names_size)
10892 {
10893 lh->file_names_size *= 2;
10894 lh->file_names = xrealloc (lh->file_names,
10895 (lh->file_names_size
10896 * sizeof (*lh->file_names)));
10897 }
10898
10899 fe = &lh->file_names[lh->num_file_names++];
10900 fe->name = name;
10901 fe->dir_index = dir_index;
10902 fe->mod_time = mod_time;
10903 fe->length = length;
aaa75496 10904 fe->included_p = 0;
cb1df416 10905 fe->symtab = NULL;
debd256d 10906}
6e70227d 10907
debd256d 10908/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10909 .debug_line, according to the endianness of ABFD. Return a pointer
10910 to a struct line_header, allocated using xmalloc.
debd256d
JB
10911
10912 NOTE: the strings in the include directory and file name tables of
10913 the returned object point into debug_line_buffer, and must not be
10914 freed. */
ae2de4f8 10915
debd256d
JB
10916static struct line_header *
10917dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10918 struct dwarf2_cu *cu)
debd256d
JB
10919{
10920 struct cleanup *back_to;
10921 struct line_header *lh;
fe1b8b76 10922 gdb_byte *line_ptr;
c764a876 10923 unsigned int bytes_read, offset_size;
debd256d
JB
10924 int i;
10925 char *cur_dir, *cur_file;
10926
be391dca 10927 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10928 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10929 {
e2e0b3e5 10930 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10931 return 0;
10932 }
10933
a738430d
MK
10934 /* Make sure that at least there's room for the total_length field.
10935 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10936 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10937 {
4d3c2250 10938 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10939 return 0;
10940 }
10941
10942 lh = xmalloc (sizeof (*lh));
10943 memset (lh, 0, sizeof (*lh));
10944 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10945 (void *) lh);
10946
dce234bc 10947 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10948
a738430d 10949 /* Read in the header. */
6e70227d 10950 lh->total_length =
c764a876
DE
10951 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10952 &bytes_read, &offset_size);
debd256d 10953 line_ptr += bytes_read;
dce234bc
PP
10954 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10955 + dwarf2_per_objfile->line.size))
debd256d 10956 {
4d3c2250 10957 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10958 return 0;
10959 }
10960 lh->statement_program_end = line_ptr + lh->total_length;
10961 lh->version = read_2_bytes (abfd, line_ptr);
10962 line_ptr += 2;
c764a876
DE
10963 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10964 line_ptr += offset_size;
debd256d
JB
10965 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10966 line_ptr += 1;
2dc7f7b3
TT
10967 if (lh->version >= 4)
10968 {
10969 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10970 line_ptr += 1;
10971 }
10972 else
10973 lh->maximum_ops_per_instruction = 1;
10974
10975 if (lh->maximum_ops_per_instruction == 0)
10976 {
10977 lh->maximum_ops_per_instruction = 1;
10978 complaint (&symfile_complaints,
3e43a32a
MS
10979 _("invalid maximum_ops_per_instruction "
10980 "in `.debug_line' section"));
2dc7f7b3
TT
10981 }
10982
debd256d
JB
10983 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10984 line_ptr += 1;
10985 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10986 line_ptr += 1;
10987 lh->line_range = read_1_byte (abfd, line_ptr);
10988 line_ptr += 1;
10989 lh->opcode_base = read_1_byte (abfd, line_ptr);
10990 line_ptr += 1;
10991 lh->standard_opcode_lengths
fe1b8b76 10992 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10993
10994 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10995 for (i = 1; i < lh->opcode_base; ++i)
10996 {
10997 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10998 line_ptr += 1;
10999 }
11000
a738430d 11001 /* Read directory table. */
9b1c24c8 11002 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11003 {
11004 line_ptr += bytes_read;
11005 add_include_dir (lh, cur_dir);
11006 }
11007 line_ptr += bytes_read;
11008
a738430d 11009 /* Read file name table. */
9b1c24c8 11010 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11011 {
11012 unsigned int dir_index, mod_time, length;
11013
11014 line_ptr += bytes_read;
11015 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11016 line_ptr += bytes_read;
11017 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11018 line_ptr += bytes_read;
11019 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11020 line_ptr += bytes_read;
11021
11022 add_file_name (lh, cur_file, dir_index, mod_time, length);
11023 }
11024 line_ptr += bytes_read;
6e70227d 11025 lh->statement_program_start = line_ptr;
debd256d 11026
dce234bc
PP
11027 if (line_ptr > (dwarf2_per_objfile->line.buffer
11028 + dwarf2_per_objfile->line.size))
4d3c2250 11029 complaint (&symfile_complaints,
3e43a32a
MS
11030 _("line number info header doesn't "
11031 "fit in `.debug_line' section"));
debd256d
JB
11032
11033 discard_cleanups (back_to);
11034 return lh;
11035}
c906108c 11036
c6da4cef
DE
11037/* Subroutine of dwarf_decode_lines to simplify it.
11038 Return the file name of the psymtab for included file FILE_INDEX
11039 in line header LH of PST.
11040 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11041 If space for the result is malloc'd, it will be freed by a cleanup.
11042 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11043
11044static char *
11045psymtab_include_file_name (const struct line_header *lh, int file_index,
11046 const struct partial_symtab *pst,
11047 const char *comp_dir)
11048{
11049 const struct file_entry fe = lh->file_names [file_index];
11050 char *include_name = fe.name;
11051 char *include_name_to_compare = include_name;
11052 char *dir_name = NULL;
72b9f47f
TT
11053 const char *pst_filename;
11054 char *copied_name = NULL;
c6da4cef
DE
11055 int file_is_pst;
11056
11057 if (fe.dir_index)
11058 dir_name = lh->include_dirs[fe.dir_index - 1];
11059
11060 if (!IS_ABSOLUTE_PATH (include_name)
11061 && (dir_name != NULL || comp_dir != NULL))
11062 {
11063 /* Avoid creating a duplicate psymtab for PST.
11064 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11065 Before we do the comparison, however, we need to account
11066 for DIR_NAME and COMP_DIR.
11067 First prepend dir_name (if non-NULL). If we still don't
11068 have an absolute path prepend comp_dir (if non-NULL).
11069 However, the directory we record in the include-file's
11070 psymtab does not contain COMP_DIR (to match the
11071 corresponding symtab(s)).
11072
11073 Example:
11074
11075 bash$ cd /tmp
11076 bash$ gcc -g ./hello.c
11077 include_name = "hello.c"
11078 dir_name = "."
11079 DW_AT_comp_dir = comp_dir = "/tmp"
11080 DW_AT_name = "./hello.c" */
11081
11082 if (dir_name != NULL)
11083 {
11084 include_name = concat (dir_name, SLASH_STRING,
11085 include_name, (char *)NULL);
11086 include_name_to_compare = include_name;
11087 make_cleanup (xfree, include_name);
11088 }
11089 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11090 {
11091 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11092 include_name, (char *)NULL);
11093 }
11094 }
11095
11096 pst_filename = pst->filename;
11097 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11098 {
72b9f47f
TT
11099 copied_name = concat (pst->dirname, SLASH_STRING,
11100 pst_filename, (char *)NULL);
11101 pst_filename = copied_name;
c6da4cef
DE
11102 }
11103
1e3fad37 11104 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11105
11106 if (include_name_to_compare != include_name)
11107 xfree (include_name_to_compare);
72b9f47f
TT
11108 if (copied_name != NULL)
11109 xfree (copied_name);
c6da4cef
DE
11110
11111 if (file_is_pst)
11112 return NULL;
11113 return include_name;
11114}
11115
c91513d8
PP
11116/* Ignore this record_line request. */
11117
11118static void
11119noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11120{
11121 return;
11122}
11123
f3f5162e
DE
11124/* Subroutine of dwarf_decode_lines to simplify it.
11125 Process the line number information in LH. */
debd256d 11126
c906108c 11127static void
f3f5162e
DE
11128dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11129 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11130{
a8c50c1f 11131 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11132 gdb_byte *line_end;
a8c50c1f 11133 unsigned int bytes_read, extended_len;
c906108c 11134 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11135 CORE_ADDR baseaddr;
11136 struct objfile *objfile = cu->objfile;
f3f5162e 11137 bfd *abfd = objfile->obfd;
fbf65064 11138 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11139 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11140 struct subfile *last_subfile = NULL;
c91513d8
PP
11141 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11142 = record_line;
e142c38c
DJ
11143
11144 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11145
debd256d
JB
11146 line_ptr = lh->statement_program_start;
11147 line_end = lh->statement_program_end;
c906108c
SS
11148
11149 /* Read the statement sequences until there's nothing left. */
11150 while (line_ptr < line_end)
11151 {
11152 /* state machine registers */
11153 CORE_ADDR address = 0;
11154 unsigned int file = 1;
11155 unsigned int line = 1;
11156 unsigned int column = 0;
debd256d 11157 int is_stmt = lh->default_is_stmt;
c906108c
SS
11158 int basic_block = 0;
11159 int end_sequence = 0;
fbf65064 11160 CORE_ADDR addr;
2dc7f7b3 11161 unsigned char op_index = 0;
c906108c 11162
aaa75496 11163 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11164 {
aaa75496 11165 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11166 /* lh->include_dirs and lh->file_names are 0-based, but the
11167 directory and file name numbers in the statement program
11168 are 1-based. */
11169 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11170 char *dir = NULL;
a738430d 11171
debd256d
JB
11172 if (fe->dir_index)
11173 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11174
11175 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11176 }
11177
a738430d 11178 /* Decode the table. */
c5aa993b 11179 while (!end_sequence)
c906108c
SS
11180 {
11181 op_code = read_1_byte (abfd, line_ptr);
11182 line_ptr += 1;
59205f5a
JB
11183 if (line_ptr > line_end)
11184 {
11185 dwarf2_debug_line_missing_end_sequence_complaint ();
11186 break;
11187 }
9aa1fe7e 11188
debd256d 11189 if (op_code >= lh->opcode_base)
6e70227d 11190 {
a738430d 11191 /* Special operand. */
debd256d 11192 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11193 address += (((op_index + (adj_opcode / lh->line_range))
11194 / lh->maximum_ops_per_instruction)
11195 * lh->minimum_instruction_length);
11196 op_index = ((op_index + (adj_opcode / lh->line_range))
11197 % lh->maximum_ops_per_instruction);
debd256d 11198 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11199 if (lh->num_file_names < file || file == 0)
25e43795 11200 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11201 /* For now we ignore lines not starting on an
11202 instruction boundary. */
11203 else if (op_index == 0)
25e43795
DJ
11204 {
11205 lh->file_names[file - 1].included_p = 1;
ca5f395d 11206 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11207 {
11208 if (last_subfile != current_subfile)
11209 {
11210 addr = gdbarch_addr_bits_remove (gdbarch, address);
11211 if (last_subfile)
c91513d8 11212 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11213 last_subfile = current_subfile;
11214 }
25e43795 11215 /* Append row to matrix using current values. */
7019d805 11216 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11217 (*p_record_line) (current_subfile, line, addr);
366da635 11218 }
25e43795 11219 }
ca5f395d 11220 basic_block = 0;
9aa1fe7e
GK
11221 }
11222 else switch (op_code)
c906108c
SS
11223 {
11224 case DW_LNS_extended_op:
3e43a32a
MS
11225 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11226 &bytes_read);
473b7be6 11227 line_ptr += bytes_read;
a8c50c1f 11228 extended_end = line_ptr + extended_len;
c906108c
SS
11229 extended_op = read_1_byte (abfd, line_ptr);
11230 line_ptr += 1;
11231 switch (extended_op)
11232 {
11233 case DW_LNE_end_sequence:
c91513d8 11234 p_record_line = record_line;
c906108c 11235 end_sequence = 1;
c906108c
SS
11236 break;
11237 case DW_LNE_set_address:
e7c27a73 11238 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11239
11240 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11241 {
11242 /* This line table is for a function which has been
11243 GCd by the linker. Ignore it. PR gdb/12528 */
11244
11245 long line_offset
11246 = line_ptr - dwarf2_per_objfile->line.buffer;
11247
11248 complaint (&symfile_complaints,
11249 _(".debug_line address at offset 0x%lx is 0 "
11250 "[in module %s]"),
bb5ed363 11251 line_offset, objfile->name);
c91513d8
PP
11252 p_record_line = noop_record_line;
11253 }
11254
2dc7f7b3 11255 op_index = 0;
107d2387
AC
11256 line_ptr += bytes_read;
11257 address += baseaddr;
c906108c
SS
11258 break;
11259 case DW_LNE_define_file:
debd256d
JB
11260 {
11261 char *cur_file;
11262 unsigned int dir_index, mod_time, length;
6e70227d 11263
3e43a32a
MS
11264 cur_file = read_direct_string (abfd, line_ptr,
11265 &bytes_read);
debd256d
JB
11266 line_ptr += bytes_read;
11267 dir_index =
11268 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11269 line_ptr += bytes_read;
11270 mod_time =
11271 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11272 line_ptr += bytes_read;
11273 length =
11274 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11275 line_ptr += bytes_read;
11276 add_file_name (lh, cur_file, dir_index, mod_time, length);
11277 }
c906108c 11278 break;
d0c6ba3d
CC
11279 case DW_LNE_set_discriminator:
11280 /* The discriminator is not interesting to the debugger;
11281 just ignore it. */
11282 line_ptr = extended_end;
11283 break;
c906108c 11284 default:
4d3c2250 11285 complaint (&symfile_complaints,
e2e0b3e5 11286 _("mangled .debug_line section"));
debd256d 11287 return;
c906108c 11288 }
a8c50c1f
DJ
11289 /* Make sure that we parsed the extended op correctly. If e.g.
11290 we expected a different address size than the producer used,
11291 we may have read the wrong number of bytes. */
11292 if (line_ptr != extended_end)
11293 {
11294 complaint (&symfile_complaints,
11295 _("mangled .debug_line section"));
11296 return;
11297 }
c906108c
SS
11298 break;
11299 case DW_LNS_copy:
59205f5a 11300 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11301 dwarf2_debug_line_missing_file_complaint ();
11302 else
366da635 11303 {
25e43795 11304 lh->file_names[file - 1].included_p = 1;
ca5f395d 11305 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11306 {
11307 if (last_subfile != current_subfile)
11308 {
11309 addr = gdbarch_addr_bits_remove (gdbarch, address);
11310 if (last_subfile)
c91513d8 11311 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11312 last_subfile = current_subfile;
11313 }
7019d805 11314 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11315 (*p_record_line) (current_subfile, line, addr);
fbf65064 11316 }
366da635 11317 }
c906108c
SS
11318 basic_block = 0;
11319 break;
11320 case DW_LNS_advance_pc:
2dc7f7b3
TT
11321 {
11322 CORE_ADDR adjust
11323 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11324
11325 address += (((op_index + adjust)
11326 / lh->maximum_ops_per_instruction)
11327 * lh->minimum_instruction_length);
11328 op_index = ((op_index + adjust)
11329 % lh->maximum_ops_per_instruction);
11330 line_ptr += bytes_read;
11331 }
c906108c
SS
11332 break;
11333 case DW_LNS_advance_line:
11334 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11335 line_ptr += bytes_read;
11336 break;
11337 case DW_LNS_set_file:
debd256d 11338 {
a738430d
MK
11339 /* The arrays lh->include_dirs and lh->file_names are
11340 0-based, but the directory and file name numbers in
11341 the statement program are 1-based. */
debd256d 11342 struct file_entry *fe;
4f1520fb 11343 char *dir = NULL;
a738430d 11344
debd256d
JB
11345 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11346 line_ptr += bytes_read;
59205f5a 11347 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11348 dwarf2_debug_line_missing_file_complaint ();
11349 else
11350 {
11351 fe = &lh->file_names[file - 1];
11352 if (fe->dir_index)
11353 dir = lh->include_dirs[fe->dir_index - 1];
11354 if (!decode_for_pst_p)
11355 {
11356 last_subfile = current_subfile;
11357 dwarf2_start_subfile (fe->name, dir, comp_dir);
11358 }
11359 }
debd256d 11360 }
c906108c
SS
11361 break;
11362 case DW_LNS_set_column:
11363 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11364 line_ptr += bytes_read;
11365 break;
11366 case DW_LNS_negate_stmt:
11367 is_stmt = (!is_stmt);
11368 break;
11369 case DW_LNS_set_basic_block:
11370 basic_block = 1;
11371 break;
c2c6d25f
JM
11372 /* Add to the address register of the state machine the
11373 address increment value corresponding to special opcode
a738430d
MK
11374 255. I.e., this value is scaled by the minimum
11375 instruction length since special opcode 255 would have
b021a221 11376 scaled the increment. */
c906108c 11377 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11378 {
11379 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11380
11381 address += (((op_index + adjust)
11382 / lh->maximum_ops_per_instruction)
11383 * lh->minimum_instruction_length);
11384 op_index = ((op_index + adjust)
11385 % lh->maximum_ops_per_instruction);
11386 }
c906108c
SS
11387 break;
11388 case DW_LNS_fixed_advance_pc:
11389 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11390 op_index = 0;
c906108c
SS
11391 line_ptr += 2;
11392 break;
9aa1fe7e 11393 default:
a738430d
MK
11394 {
11395 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11396 int i;
a738430d 11397
debd256d 11398 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11399 {
11400 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11401 line_ptr += bytes_read;
11402 }
11403 }
c906108c
SS
11404 }
11405 }
59205f5a
JB
11406 if (lh->num_file_names < file || file == 0)
11407 dwarf2_debug_line_missing_file_complaint ();
11408 else
11409 {
11410 lh->file_names[file - 1].included_p = 1;
11411 if (!decode_for_pst_p)
fbf65064
UW
11412 {
11413 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11414 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11415 }
59205f5a 11416 }
c906108c 11417 }
f3f5162e
DE
11418}
11419
11420/* Decode the Line Number Program (LNP) for the given line_header
11421 structure and CU. The actual information extracted and the type
11422 of structures created from the LNP depends on the value of PST.
11423
11424 1. If PST is NULL, then this procedure uses the data from the program
11425 to create all necessary symbol tables, and their linetables.
11426
11427 2. If PST is not NULL, this procedure reads the program to determine
11428 the list of files included by the unit represented by PST, and
11429 builds all the associated partial symbol tables.
11430
11431 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11432 It is used for relative paths in the line table.
11433 NOTE: When processing partial symtabs (pst != NULL),
11434 comp_dir == pst->dirname.
11435
11436 NOTE: It is important that psymtabs have the same file name (via strcmp)
11437 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11438 symtab we don't use it in the name of the psymtabs we create.
11439 E.g. expand_line_sal requires this when finding psymtabs to expand.
11440 A good testcase for this is mb-inline.exp. */
11441
11442static void
11443dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11444 struct dwarf2_cu *cu, struct partial_symtab *pst,
11445 int want_line_info)
11446{
11447 struct objfile *objfile = cu->objfile;
11448 const int decode_for_pst_p = (pst != NULL);
11449 struct subfile *first_subfile = current_subfile;
11450
11451 if (want_line_info)
11452 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11453
11454 if (decode_for_pst_p)
11455 {
11456 int file_index;
11457
11458 /* Now that we're done scanning the Line Header Program, we can
11459 create the psymtab of each included file. */
11460 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11461 if (lh->file_names[file_index].included_p == 1)
11462 {
c6da4cef
DE
11463 char *include_name =
11464 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11465 if (include_name != NULL)
aaa75496
JB
11466 dwarf2_create_include_psymtab (include_name, pst, objfile);
11467 }
11468 }
cb1df416
DJ
11469 else
11470 {
11471 /* Make sure a symtab is created for every file, even files
11472 which contain only variables (i.e. no code with associated
11473 line numbers). */
cb1df416 11474 int i;
cb1df416
DJ
11475
11476 for (i = 0; i < lh->num_file_names; i++)
11477 {
11478 char *dir = NULL;
f3f5162e 11479 struct file_entry *fe;
9a619af0 11480
cb1df416
DJ
11481 fe = &lh->file_names[i];
11482 if (fe->dir_index)
11483 dir = lh->include_dirs[fe->dir_index - 1];
11484 dwarf2_start_subfile (fe->name, dir, comp_dir);
11485
11486 /* Skip the main file; we don't need it, and it must be
11487 allocated last, so that it will show up before the
11488 non-primary symtabs in the objfile's symtab list. */
11489 if (current_subfile == first_subfile)
11490 continue;
11491
11492 if (current_subfile->symtab == NULL)
11493 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11494 objfile);
cb1df416
DJ
11495 fe->symtab = current_subfile->symtab;
11496 }
11497 }
c906108c
SS
11498}
11499
11500/* Start a subfile for DWARF. FILENAME is the name of the file and
11501 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11502 or NULL if not known. COMP_DIR is the compilation directory for the
11503 linetable's compilation unit or NULL if not known.
c906108c
SS
11504 This routine tries to keep line numbers from identical absolute and
11505 relative file names in a common subfile.
11506
11507 Using the `list' example from the GDB testsuite, which resides in
11508 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11509 of /srcdir/list0.c yields the following debugging information for list0.c:
11510
c5aa993b
JM
11511 DW_AT_name: /srcdir/list0.c
11512 DW_AT_comp_dir: /compdir
357e46e7 11513 files.files[0].name: list0.h
c5aa993b 11514 files.files[0].dir: /srcdir
357e46e7 11515 files.files[1].name: list0.c
c5aa993b 11516 files.files[1].dir: /srcdir
c906108c
SS
11517
11518 The line number information for list0.c has to end up in a single
4f1520fb
FR
11519 subfile, so that `break /srcdir/list0.c:1' works as expected.
11520 start_subfile will ensure that this happens provided that we pass the
11521 concatenation of files.files[1].dir and files.files[1].name as the
11522 subfile's name. */
c906108c
SS
11523
11524static void
3e43a32a
MS
11525dwarf2_start_subfile (char *filename, const char *dirname,
11526 const char *comp_dir)
c906108c 11527{
4f1520fb
FR
11528 char *fullname;
11529
11530 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11531 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11532 second argument to start_subfile. To be consistent, we do the
11533 same here. In order not to lose the line information directory,
11534 we concatenate it to the filename when it makes sense.
11535 Note that the Dwarf3 standard says (speaking of filenames in line
11536 information): ``The directory index is ignored for file names
11537 that represent full path names''. Thus ignoring dirname in the
11538 `else' branch below isn't an issue. */
c906108c 11539
d5166ae1 11540 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11541 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11542 else
11543 fullname = filename;
c906108c 11544
4f1520fb
FR
11545 start_subfile (fullname, comp_dir);
11546
11547 if (fullname != filename)
11548 xfree (fullname);
c906108c
SS
11549}
11550
4c2df51b
DJ
11551static void
11552var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11553 struct dwarf2_cu *cu)
4c2df51b 11554{
e7c27a73
DJ
11555 struct objfile *objfile = cu->objfile;
11556 struct comp_unit_head *cu_header = &cu->header;
11557
4c2df51b
DJ
11558 /* NOTE drow/2003-01-30: There used to be a comment and some special
11559 code here to turn a symbol with DW_AT_external and a
11560 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11561 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11562 with some versions of binutils) where shared libraries could have
11563 relocations against symbols in their debug information - the
11564 minimal symbol would have the right address, but the debug info
11565 would not. It's no longer necessary, because we will explicitly
11566 apply relocations when we read in the debug information now. */
11567
11568 /* A DW_AT_location attribute with no contents indicates that a
11569 variable has been optimized away. */
11570 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11571 {
11572 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11573 return;
11574 }
11575
11576 /* Handle one degenerate form of location expression specially, to
11577 preserve GDB's previous behavior when section offsets are
11578 specified. If this is just a DW_OP_addr then mark this symbol
11579 as LOC_STATIC. */
11580
11581 if (attr_form_is_block (attr)
11582 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11583 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11584 {
891d2f0b 11585 unsigned int dummy;
4c2df51b
DJ
11586
11587 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11588 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11589 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11590 fixup_symbol_section (sym, objfile);
11591 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11592 SYMBOL_SECTION (sym));
4c2df51b
DJ
11593 return;
11594 }
11595
11596 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11597 expression evaluator, and use LOC_COMPUTED only when necessary
11598 (i.e. when the value of a register or memory location is
11599 referenced, or a thread-local block, etc.). Then again, it might
11600 not be worthwhile. I'm assuming that it isn't unless performance
11601 or memory numbers show me otherwise. */
11602
e7c27a73 11603 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11604 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11605
11606 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11607 cu->has_loclist = 1;
4c2df51b
DJ
11608}
11609
c906108c
SS
11610/* Given a pointer to a DWARF information entry, figure out if we need
11611 to make a symbol table entry for it, and if so, create a new entry
11612 and return a pointer to it.
11613 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11614 used the passed type.
11615 If SPACE is not NULL, use it to hold the new symbol. If it is
11616 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11617
11618static struct symbol *
34eaf542
TT
11619new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11620 struct symbol *space)
c906108c 11621{
e7c27a73 11622 struct objfile *objfile = cu->objfile;
c906108c
SS
11623 struct symbol *sym = NULL;
11624 char *name;
11625 struct attribute *attr = NULL;
11626 struct attribute *attr2 = NULL;
e142c38c 11627 CORE_ADDR baseaddr;
e37fd15a
SW
11628 struct pending **list_to_add = NULL;
11629
edb3359d 11630 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11631
11632 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11633
94af9270 11634 name = dwarf2_name (die, cu);
c906108c
SS
11635 if (name)
11636 {
94af9270 11637 const char *linkagename;
34eaf542 11638 int suppress_add = 0;
94af9270 11639
34eaf542
TT
11640 if (space)
11641 sym = space;
11642 else
11643 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11644 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11645
11646 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11647 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11648 linkagename = dwarf2_physname (name, die, cu);
11649 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11650
f55ee35c
JK
11651 /* Fortran does not have mangling standard and the mangling does differ
11652 between gfortran, iFort etc. */
11653 if (cu->language == language_fortran
b250c185 11654 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11655 symbol_set_demangled_name (&(sym->ginfo),
11656 (char *) dwarf2_full_name (name, die, cu),
11657 NULL);
f55ee35c 11658
c906108c 11659 /* Default assumptions.
c5aa993b 11660 Use the passed type or decode it from the die. */
176620f1 11661 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11662 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11663 if (type != NULL)
11664 SYMBOL_TYPE (sym) = type;
11665 else
e7c27a73 11666 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11667 attr = dwarf2_attr (die,
11668 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11669 cu);
c906108c
SS
11670 if (attr)
11671 {
11672 SYMBOL_LINE (sym) = DW_UNSND (attr);
11673 }
cb1df416 11674
edb3359d
DJ
11675 attr = dwarf2_attr (die,
11676 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11677 cu);
cb1df416
DJ
11678 if (attr)
11679 {
11680 int file_index = DW_UNSND (attr);
9a619af0 11681
cb1df416
DJ
11682 if (cu->line_header == NULL
11683 || file_index > cu->line_header->num_file_names)
11684 complaint (&symfile_complaints,
11685 _("file index out of range"));
1c3d648d 11686 else if (file_index > 0)
cb1df416
DJ
11687 {
11688 struct file_entry *fe;
9a619af0 11689
cb1df416
DJ
11690 fe = &cu->line_header->file_names[file_index - 1];
11691 SYMBOL_SYMTAB (sym) = fe->symtab;
11692 }
11693 }
11694
c906108c
SS
11695 switch (die->tag)
11696 {
11697 case DW_TAG_label:
e142c38c 11698 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11699 if (attr)
11700 {
11701 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11702 }
0f5238ed
TT
11703 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11704 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11705 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11706 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11707 break;
11708 case DW_TAG_subprogram:
11709 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11710 finish_block. */
11711 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11712 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11713 if ((attr2 && (DW_UNSND (attr2) != 0))
11714 || cu->language == language_ada)
c906108c 11715 {
2cfa0c8d
JB
11716 /* Subprograms marked external are stored as a global symbol.
11717 Ada subprograms, whether marked external or not, are always
11718 stored as a global symbol, because we want to be able to
11719 access them globally. For instance, we want to be able
11720 to break on a nested subprogram without having to
11721 specify the context. */
e37fd15a 11722 list_to_add = &global_symbols;
c906108c
SS
11723 }
11724 else
11725 {
e37fd15a 11726 list_to_add = cu->list_in_scope;
c906108c
SS
11727 }
11728 break;
edb3359d
DJ
11729 case DW_TAG_inlined_subroutine:
11730 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11731 finish_block. */
11732 SYMBOL_CLASS (sym) = LOC_BLOCK;
11733 SYMBOL_INLINED (sym) = 1;
11734 /* Do not add the symbol to any lists. It will be found via
11735 BLOCK_FUNCTION from the blockvector. */
11736 break;
34eaf542
TT
11737 case DW_TAG_template_value_param:
11738 suppress_add = 1;
11739 /* Fall through. */
72929c62 11740 case DW_TAG_constant:
c906108c 11741 case DW_TAG_variable:
254e6b9e 11742 case DW_TAG_member:
0963b4bd
MS
11743 /* Compilation with minimal debug info may result in
11744 variables with missing type entries. Change the
11745 misleading `void' type to something sensible. */
c906108c 11746 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11747 SYMBOL_TYPE (sym)
46bf5051 11748 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11749
e142c38c 11750 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11751 /* In the case of DW_TAG_member, we should only be called for
11752 static const members. */
11753 if (die->tag == DW_TAG_member)
11754 {
3863f96c
DE
11755 /* dwarf2_add_field uses die_is_declaration,
11756 so we do the same. */
254e6b9e
DE
11757 gdb_assert (die_is_declaration (die, cu));
11758 gdb_assert (attr);
11759 }
c906108c
SS
11760 if (attr)
11761 {
e7c27a73 11762 dwarf2_const_value (attr, sym, cu);
e142c38c 11763 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11764 if (!suppress_add)
34eaf542
TT
11765 {
11766 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11767 list_to_add = &global_symbols;
34eaf542 11768 else
e37fd15a 11769 list_to_add = cu->list_in_scope;
34eaf542 11770 }
c906108c
SS
11771 break;
11772 }
e142c38c 11773 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11774 if (attr)
11775 {
e7c27a73 11776 var_decode_location (attr, sym, cu);
e142c38c 11777 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11778 if (SYMBOL_CLASS (sym) == LOC_STATIC
11779 && SYMBOL_VALUE_ADDRESS (sym) == 0
11780 && !dwarf2_per_objfile->has_section_at_zero)
11781 {
11782 /* When a static variable is eliminated by the linker,
11783 the corresponding debug information is not stripped
11784 out, but the variable address is set to null;
11785 do not add such variables into symbol table. */
11786 }
11787 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11788 {
f55ee35c
JK
11789 /* Workaround gfortran PR debug/40040 - it uses
11790 DW_AT_location for variables in -fPIC libraries which may
11791 get overriden by other libraries/executable and get
11792 a different address. Resolve it by the minimal symbol
11793 which may come from inferior's executable using copy
11794 relocation. Make this workaround only for gfortran as for
11795 other compilers GDB cannot guess the minimal symbol
11796 Fortran mangling kind. */
11797 if (cu->language == language_fortran && die->parent
11798 && die->parent->tag == DW_TAG_module
11799 && cu->producer
11800 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11801 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11802
1c809c68
TT
11803 /* A variable with DW_AT_external is never static,
11804 but it may be block-scoped. */
11805 list_to_add = (cu->list_in_scope == &file_symbols
11806 ? &global_symbols : cu->list_in_scope);
1c809c68 11807 }
c906108c 11808 else
e37fd15a 11809 list_to_add = cu->list_in_scope;
c906108c
SS
11810 }
11811 else
11812 {
11813 /* We do not know the address of this symbol.
c5aa993b
JM
11814 If it is an external symbol and we have type information
11815 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11816 The address of the variable will then be determined from
11817 the minimal symbol table whenever the variable is
11818 referenced. */
e142c38c 11819 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11820 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11821 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11822 {
0fe7935b
DJ
11823 /* A variable with DW_AT_external is never static, but it
11824 may be block-scoped. */
11825 list_to_add = (cu->list_in_scope == &file_symbols
11826 ? &global_symbols : cu->list_in_scope);
11827
c906108c 11828 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11829 }
442ddf59
JK
11830 else if (!die_is_declaration (die, cu))
11831 {
11832 /* Use the default LOC_OPTIMIZED_OUT class. */
11833 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11834 if (!suppress_add)
11835 list_to_add = cu->list_in_scope;
442ddf59 11836 }
c906108c
SS
11837 }
11838 break;
11839 case DW_TAG_formal_parameter:
edb3359d
DJ
11840 /* If we are inside a function, mark this as an argument. If
11841 not, we might be looking at an argument to an inlined function
11842 when we do not have enough information to show inlined frames;
11843 pretend it's a local variable in that case so that the user can
11844 still see it. */
11845 if (context_stack_depth > 0
11846 && context_stack[context_stack_depth - 1].name != NULL)
11847 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11848 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11849 if (attr)
11850 {
e7c27a73 11851 var_decode_location (attr, sym, cu);
c906108c 11852 }
e142c38c 11853 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11854 if (attr)
11855 {
e7c27a73 11856 dwarf2_const_value (attr, sym, cu);
c906108c 11857 }
f346a30d 11858
e37fd15a 11859 list_to_add = cu->list_in_scope;
c906108c
SS
11860 break;
11861 case DW_TAG_unspecified_parameters:
11862 /* From varargs functions; gdb doesn't seem to have any
11863 interest in this information, so just ignore it for now.
11864 (FIXME?) */
11865 break;
34eaf542
TT
11866 case DW_TAG_template_type_param:
11867 suppress_add = 1;
11868 /* Fall through. */
c906108c 11869 case DW_TAG_class_type:
680b30c7 11870 case DW_TAG_interface_type:
c906108c
SS
11871 case DW_TAG_structure_type:
11872 case DW_TAG_union_type:
72019c9c 11873 case DW_TAG_set_type:
c906108c
SS
11874 case DW_TAG_enumeration_type:
11875 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11876 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11877
63d06c5c 11878 {
987504bb 11879 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11880 really ever be static objects: otherwise, if you try
11881 to, say, break of a class's method and you're in a file
11882 which doesn't mention that class, it won't work unless
11883 the check for all static symbols in lookup_symbol_aux
11884 saves you. See the OtherFileClass tests in
11885 gdb.c++/namespace.exp. */
11886
e37fd15a 11887 if (!suppress_add)
34eaf542 11888 {
34eaf542
TT
11889 list_to_add = (cu->list_in_scope == &file_symbols
11890 && (cu->language == language_cplus
11891 || cu->language == language_java)
11892 ? &global_symbols : cu->list_in_scope);
63d06c5c 11893
64382290
TT
11894 /* The semantics of C++ state that "struct foo {
11895 ... }" also defines a typedef for "foo". A Java
11896 class declaration also defines a typedef for the
11897 class. */
11898 if (cu->language == language_cplus
11899 || cu->language == language_java
11900 || cu->language == language_ada)
11901 {
11902 /* The symbol's name is already allocated along
11903 with this objfile, so we don't need to
11904 duplicate it for the type. */
11905 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11906 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11907 }
63d06c5c
DC
11908 }
11909 }
c906108c
SS
11910 break;
11911 case DW_TAG_typedef:
63d06c5c
DC
11912 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11913 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11914 list_to_add = cu->list_in_scope;
63d06c5c 11915 break;
c906108c 11916 case DW_TAG_base_type:
a02abb62 11917 case DW_TAG_subrange_type:
c906108c 11918 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11919 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11920 list_to_add = cu->list_in_scope;
c906108c
SS
11921 break;
11922 case DW_TAG_enumerator:
e142c38c 11923 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11924 if (attr)
11925 {
e7c27a73 11926 dwarf2_const_value (attr, sym, cu);
c906108c 11927 }
63d06c5c
DC
11928 {
11929 /* NOTE: carlton/2003-11-10: See comment above in the
11930 DW_TAG_class_type, etc. block. */
11931
e142c38c 11932 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11933 && (cu->language == language_cplus
11934 || cu->language == language_java)
e142c38c 11935 ? &global_symbols : cu->list_in_scope);
63d06c5c 11936 }
c906108c 11937 break;
5c4e30ca
DC
11938 case DW_TAG_namespace:
11939 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11940 list_to_add = &global_symbols;
5c4e30ca 11941 break;
c906108c
SS
11942 default:
11943 /* Not a tag we recognize. Hopefully we aren't processing
11944 trash data, but since we must specifically ignore things
11945 we don't recognize, there is nothing else we should do at
0963b4bd 11946 this point. */
e2e0b3e5 11947 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11948 dwarf_tag_name (die->tag));
c906108c
SS
11949 break;
11950 }
df8a16a1 11951
e37fd15a
SW
11952 if (suppress_add)
11953 {
11954 sym->hash_next = objfile->template_symbols;
11955 objfile->template_symbols = sym;
11956 list_to_add = NULL;
11957 }
11958
11959 if (list_to_add != NULL)
11960 add_symbol_to_list (sym, list_to_add);
11961
df8a16a1
DJ
11962 /* For the benefit of old versions of GCC, check for anonymous
11963 namespaces based on the demangled name. */
11964 if (!processing_has_namespace_info
94af9270 11965 && cu->language == language_cplus)
a10964d1 11966 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11967 }
11968 return (sym);
11969}
11970
34eaf542
TT
11971/* A wrapper for new_symbol_full that always allocates a new symbol. */
11972
11973static struct symbol *
11974new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11975{
11976 return new_symbol_full (die, type, cu, NULL);
11977}
11978
98bfdba5
PA
11979/* Given an attr with a DW_FORM_dataN value in host byte order,
11980 zero-extend it as appropriate for the symbol's type. The DWARF
11981 standard (v4) is not entirely clear about the meaning of using
11982 DW_FORM_dataN for a constant with a signed type, where the type is
11983 wider than the data. The conclusion of a discussion on the DWARF
11984 list was that this is unspecified. We choose to always zero-extend
11985 because that is the interpretation long in use by GCC. */
c906108c 11986
98bfdba5
PA
11987static gdb_byte *
11988dwarf2_const_value_data (struct attribute *attr, struct type *type,
11989 const char *name, struct obstack *obstack,
11990 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11991{
e7c27a73 11992 struct objfile *objfile = cu->objfile;
e17a4113
UW
11993 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11994 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11995 LONGEST l = DW_UNSND (attr);
11996
11997 if (bits < sizeof (*value) * 8)
11998 {
11999 l &= ((LONGEST) 1 << bits) - 1;
12000 *value = l;
12001 }
12002 else if (bits == sizeof (*value) * 8)
12003 *value = l;
12004 else
12005 {
12006 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12007 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12008 return bytes;
12009 }
12010
12011 return NULL;
12012}
12013
12014/* Read a constant value from an attribute. Either set *VALUE, or if
12015 the value does not fit in *VALUE, set *BYTES - either already
12016 allocated on the objfile obstack, or newly allocated on OBSTACK,
12017 or, set *BATON, if we translated the constant to a location
12018 expression. */
12019
12020static void
12021dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12022 const char *name, struct obstack *obstack,
12023 struct dwarf2_cu *cu,
12024 long *value, gdb_byte **bytes,
12025 struct dwarf2_locexpr_baton **baton)
12026{
12027 struct objfile *objfile = cu->objfile;
12028 struct comp_unit_head *cu_header = &cu->header;
c906108c 12029 struct dwarf_block *blk;
98bfdba5
PA
12030 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12031 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12032
12033 *value = 0;
12034 *bytes = NULL;
12035 *baton = NULL;
c906108c
SS
12036
12037 switch (attr->form)
12038 {
12039 case DW_FORM_addr:
ac56253d 12040 {
ac56253d
TT
12041 gdb_byte *data;
12042
98bfdba5
PA
12043 if (TYPE_LENGTH (type) != cu_header->addr_size)
12044 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12045 cu_header->addr_size,
98bfdba5 12046 TYPE_LENGTH (type));
ac56253d
TT
12047 /* Symbols of this form are reasonably rare, so we just
12048 piggyback on the existing location code rather than writing
12049 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12050 *baton = obstack_alloc (&objfile->objfile_obstack,
12051 sizeof (struct dwarf2_locexpr_baton));
12052 (*baton)->per_cu = cu->per_cu;
12053 gdb_assert ((*baton)->per_cu);
ac56253d 12054
98bfdba5
PA
12055 (*baton)->size = 2 + cu_header->addr_size;
12056 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12057 (*baton)->data = data;
ac56253d
TT
12058
12059 data[0] = DW_OP_addr;
12060 store_unsigned_integer (&data[1], cu_header->addr_size,
12061 byte_order, DW_ADDR (attr));
12062 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12063 }
c906108c 12064 break;
4ac36638 12065 case DW_FORM_string:
93b5768b 12066 case DW_FORM_strp:
98bfdba5
PA
12067 /* DW_STRING is already allocated on the objfile obstack, point
12068 directly to it. */
12069 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12070 break;
c906108c
SS
12071 case DW_FORM_block1:
12072 case DW_FORM_block2:
12073 case DW_FORM_block4:
12074 case DW_FORM_block:
2dc7f7b3 12075 case DW_FORM_exprloc:
c906108c 12076 blk = DW_BLOCK (attr);
98bfdba5
PA
12077 if (TYPE_LENGTH (type) != blk->size)
12078 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12079 TYPE_LENGTH (type));
12080 *bytes = blk->data;
c906108c 12081 break;
2df3850c
JM
12082
12083 /* The DW_AT_const_value attributes are supposed to carry the
12084 symbol's value "represented as it would be on the target
12085 architecture." By the time we get here, it's already been
12086 converted to host endianness, so we just need to sign- or
12087 zero-extend it as appropriate. */
12088 case DW_FORM_data1:
3e43a32a
MS
12089 *bytes = dwarf2_const_value_data (attr, type, name,
12090 obstack, cu, value, 8);
2df3850c 12091 break;
c906108c 12092 case DW_FORM_data2:
3e43a32a
MS
12093 *bytes = dwarf2_const_value_data (attr, type, name,
12094 obstack, cu, value, 16);
2df3850c 12095 break;
c906108c 12096 case DW_FORM_data4:
3e43a32a
MS
12097 *bytes = dwarf2_const_value_data (attr, type, name,
12098 obstack, cu, value, 32);
2df3850c 12099 break;
c906108c 12100 case DW_FORM_data8:
3e43a32a
MS
12101 *bytes = dwarf2_const_value_data (attr, type, name,
12102 obstack, cu, value, 64);
2df3850c
JM
12103 break;
12104
c906108c 12105 case DW_FORM_sdata:
98bfdba5 12106 *value = DW_SND (attr);
2df3850c
JM
12107 break;
12108
c906108c 12109 case DW_FORM_udata:
98bfdba5 12110 *value = DW_UNSND (attr);
c906108c 12111 break;
2df3850c 12112
c906108c 12113 default:
4d3c2250 12114 complaint (&symfile_complaints,
e2e0b3e5 12115 _("unsupported const value attribute form: '%s'"),
4d3c2250 12116 dwarf_form_name (attr->form));
98bfdba5 12117 *value = 0;
c906108c
SS
12118 break;
12119 }
12120}
12121
2df3850c 12122
98bfdba5
PA
12123/* Copy constant value from an attribute to a symbol. */
12124
2df3850c 12125static void
98bfdba5
PA
12126dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12127 struct dwarf2_cu *cu)
2df3850c 12128{
98bfdba5
PA
12129 struct objfile *objfile = cu->objfile;
12130 struct comp_unit_head *cu_header = &cu->header;
12131 long value;
12132 gdb_byte *bytes;
12133 struct dwarf2_locexpr_baton *baton;
2df3850c 12134
98bfdba5
PA
12135 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12136 SYMBOL_PRINT_NAME (sym),
12137 &objfile->objfile_obstack, cu,
12138 &value, &bytes, &baton);
2df3850c 12139
98bfdba5
PA
12140 if (baton != NULL)
12141 {
12142 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12143 SYMBOL_LOCATION_BATON (sym) = baton;
12144 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12145 }
12146 else if (bytes != NULL)
12147 {
12148 SYMBOL_VALUE_BYTES (sym) = bytes;
12149 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12150 }
12151 else
12152 {
12153 SYMBOL_VALUE (sym) = value;
12154 SYMBOL_CLASS (sym) = LOC_CONST;
12155 }
2df3850c
JM
12156}
12157
c906108c
SS
12158/* Return the type of the die in question using its DW_AT_type attribute. */
12159
12160static struct type *
e7c27a73 12161die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12162{
c906108c 12163 struct attribute *type_attr;
c906108c 12164
e142c38c 12165 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12166 if (!type_attr)
12167 {
12168 /* A missing DW_AT_type represents a void type. */
46bf5051 12169 return objfile_type (cu->objfile)->builtin_void;
c906108c 12170 }
348e048f 12171
673bfd45 12172 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12173}
12174
b4ba55a1
JB
12175/* True iff CU's producer generates GNAT Ada auxiliary information
12176 that allows to find parallel types through that information instead
12177 of having to do expensive parallel lookups by type name. */
12178
12179static int
12180need_gnat_info (struct dwarf2_cu *cu)
12181{
12182 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12183 of GNAT produces this auxiliary information, without any indication
12184 that it is produced. Part of enhancing the FSF version of GNAT
12185 to produce that information will be to put in place an indicator
12186 that we can use in order to determine whether the descriptive type
12187 info is available or not. One suggestion that has been made is
12188 to use a new attribute, attached to the CU die. For now, assume
12189 that the descriptive type info is not available. */
12190 return 0;
12191}
12192
b4ba55a1
JB
12193/* Return the auxiliary type of the die in question using its
12194 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12195 attribute is not present. */
12196
12197static struct type *
12198die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12199{
b4ba55a1 12200 struct attribute *type_attr;
b4ba55a1
JB
12201
12202 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12203 if (!type_attr)
12204 return NULL;
12205
673bfd45 12206 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12207}
12208
12209/* If DIE has a descriptive_type attribute, then set the TYPE's
12210 descriptive type accordingly. */
12211
12212static void
12213set_descriptive_type (struct type *type, struct die_info *die,
12214 struct dwarf2_cu *cu)
12215{
12216 struct type *descriptive_type = die_descriptive_type (die, cu);
12217
12218 if (descriptive_type)
12219 {
12220 ALLOCATE_GNAT_AUX_TYPE (type);
12221 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12222 }
12223}
12224
c906108c
SS
12225/* Return the containing type of the die in question using its
12226 DW_AT_containing_type attribute. */
12227
12228static struct type *
e7c27a73 12229die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12230{
c906108c 12231 struct attribute *type_attr;
c906108c 12232
e142c38c 12233 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12234 if (!type_attr)
12235 error (_("Dwarf Error: Problem turning containing type into gdb type "
12236 "[in module %s]"), cu->objfile->name);
12237
673bfd45 12238 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12239}
12240
673bfd45
DE
12241/* Look up the type of DIE in CU using its type attribute ATTR.
12242 If there is no type substitute an error marker. */
12243
c906108c 12244static struct type *
673bfd45
DE
12245lookup_die_type (struct die_info *die, struct attribute *attr,
12246 struct dwarf2_cu *cu)
c906108c 12247{
bb5ed363 12248 struct objfile *objfile = cu->objfile;
f792889a
DJ
12249 struct type *this_type;
12250
673bfd45
DE
12251 /* First see if we have it cached. */
12252
12253 if (is_ref_attr (attr))
12254 {
12255 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12256
12257 this_type = get_die_type_at_offset (offset, cu->per_cu);
12258 }
55f1336d 12259 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12260 {
12261 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12262 struct dwarf2_cu *sig_cu;
12263 unsigned int offset;
12264
12265 /* sig_type will be NULL if the signatured type is missing from
12266 the debug info. */
12267 if (sig_type == NULL)
12268 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12269 "at 0x%x [in module %s]"),
bb5ed363 12270 die->offset, objfile->name);
673bfd45 12271
b0df02fd 12272 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12273 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12274 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12275 }
12276 else
12277 {
12278 dump_die_for_error (die);
12279 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12280 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12281 }
12282
12283 /* If not cached we need to read it in. */
12284
12285 if (this_type == NULL)
12286 {
12287 struct die_info *type_die;
12288 struct dwarf2_cu *type_cu = cu;
12289
12290 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12291 /* If the type is cached, we should have found it above. */
12292 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12293 this_type = read_type_die_1 (type_die, type_cu);
12294 }
12295
12296 /* If we still don't have a type use an error marker. */
12297
12298 if (this_type == NULL)
c906108c 12299 {
b00fdb78
TT
12300 char *message, *saved;
12301
12302 /* read_type_die already issued a complaint. */
12303 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12304 objfile->name,
b00fdb78
TT
12305 cu->header.offset,
12306 die->offset);
bb5ed363 12307 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12308 message, strlen (message));
12309 xfree (message);
12310
bb5ed363 12311 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12312 }
673bfd45 12313
f792889a 12314 return this_type;
c906108c
SS
12315}
12316
673bfd45
DE
12317/* Return the type in DIE, CU.
12318 Returns NULL for invalid types.
12319
12320 This first does a lookup in the appropriate type_hash table,
12321 and only reads the die in if necessary.
12322
12323 NOTE: This can be called when reading in partial or full symbols. */
12324
f792889a 12325static struct type *
e7c27a73 12326read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12327{
f792889a
DJ
12328 struct type *this_type;
12329
12330 this_type = get_die_type (die, cu);
12331 if (this_type)
12332 return this_type;
12333
673bfd45
DE
12334 return read_type_die_1 (die, cu);
12335}
12336
12337/* Read the type in DIE, CU.
12338 Returns NULL for invalid types. */
12339
12340static struct type *
12341read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12342{
12343 struct type *this_type = NULL;
12344
c906108c
SS
12345 switch (die->tag)
12346 {
12347 case DW_TAG_class_type:
680b30c7 12348 case DW_TAG_interface_type:
c906108c
SS
12349 case DW_TAG_structure_type:
12350 case DW_TAG_union_type:
f792889a 12351 this_type = read_structure_type (die, cu);
c906108c
SS
12352 break;
12353 case DW_TAG_enumeration_type:
f792889a 12354 this_type = read_enumeration_type (die, cu);
c906108c
SS
12355 break;
12356 case DW_TAG_subprogram:
12357 case DW_TAG_subroutine_type:
edb3359d 12358 case DW_TAG_inlined_subroutine:
f792889a 12359 this_type = read_subroutine_type (die, cu);
c906108c
SS
12360 break;
12361 case DW_TAG_array_type:
f792889a 12362 this_type = read_array_type (die, cu);
c906108c 12363 break;
72019c9c 12364 case DW_TAG_set_type:
f792889a 12365 this_type = read_set_type (die, cu);
72019c9c 12366 break;
c906108c 12367 case DW_TAG_pointer_type:
f792889a 12368 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12369 break;
12370 case DW_TAG_ptr_to_member_type:
f792889a 12371 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12372 break;
12373 case DW_TAG_reference_type:
f792889a 12374 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12375 break;
12376 case DW_TAG_const_type:
f792889a 12377 this_type = read_tag_const_type (die, cu);
c906108c
SS
12378 break;
12379 case DW_TAG_volatile_type:
f792889a 12380 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12381 break;
12382 case DW_TAG_string_type:
f792889a 12383 this_type = read_tag_string_type (die, cu);
c906108c
SS
12384 break;
12385 case DW_TAG_typedef:
f792889a 12386 this_type = read_typedef (die, cu);
c906108c 12387 break;
a02abb62 12388 case DW_TAG_subrange_type:
f792889a 12389 this_type = read_subrange_type (die, cu);
a02abb62 12390 break;
c906108c 12391 case DW_TAG_base_type:
f792889a 12392 this_type = read_base_type (die, cu);
c906108c 12393 break;
81a17f79 12394 case DW_TAG_unspecified_type:
f792889a 12395 this_type = read_unspecified_type (die, cu);
81a17f79 12396 break;
0114d602
DJ
12397 case DW_TAG_namespace:
12398 this_type = read_namespace_type (die, cu);
12399 break;
f55ee35c
JK
12400 case DW_TAG_module:
12401 this_type = read_module_type (die, cu);
12402 break;
c906108c 12403 default:
3e43a32a
MS
12404 complaint (&symfile_complaints,
12405 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12406 dwarf_tag_name (die->tag));
c906108c
SS
12407 break;
12408 }
63d06c5c 12409
f792889a 12410 return this_type;
63d06c5c
DC
12411}
12412
abc72ce4
DE
12413/* See if we can figure out if the class lives in a namespace. We do
12414 this by looking for a member function; its demangled name will
12415 contain namespace info, if there is any.
12416 Return the computed name or NULL.
12417 Space for the result is allocated on the objfile's obstack.
12418 This is the full-die version of guess_partial_die_structure_name.
12419 In this case we know DIE has no useful parent. */
12420
12421static char *
12422guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12423{
12424 struct die_info *spec_die;
12425 struct dwarf2_cu *spec_cu;
12426 struct die_info *child;
12427
12428 spec_cu = cu;
12429 spec_die = die_specification (die, &spec_cu);
12430 if (spec_die != NULL)
12431 {
12432 die = spec_die;
12433 cu = spec_cu;
12434 }
12435
12436 for (child = die->child;
12437 child != NULL;
12438 child = child->sibling)
12439 {
12440 if (child->tag == DW_TAG_subprogram)
12441 {
12442 struct attribute *attr;
12443
12444 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12445 if (attr == NULL)
12446 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12447 if (attr != NULL)
12448 {
12449 char *actual_name
12450 = language_class_name_from_physname (cu->language_defn,
12451 DW_STRING (attr));
12452 char *name = NULL;
12453
12454 if (actual_name != NULL)
12455 {
12456 char *die_name = dwarf2_name (die, cu);
12457
12458 if (die_name != NULL
12459 && strcmp (die_name, actual_name) != 0)
12460 {
12461 /* Strip off the class name from the full name.
12462 We want the prefix. */
12463 int die_name_len = strlen (die_name);
12464 int actual_name_len = strlen (actual_name);
12465
12466 /* Test for '::' as a sanity check. */
12467 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12468 && actual_name[actual_name_len
12469 - die_name_len - 1] == ':')
abc72ce4
DE
12470 name =
12471 obsavestring (actual_name,
12472 actual_name_len - die_name_len - 2,
12473 &cu->objfile->objfile_obstack);
12474 }
12475 }
12476 xfree (actual_name);
12477 return name;
12478 }
12479 }
12480 }
12481
12482 return NULL;
12483}
12484
96408a79
SA
12485/* GCC might emit a nameless typedef that has a linkage name. Determine the
12486 prefix part in such case. See
12487 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12488
12489static char *
12490anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12491{
12492 struct attribute *attr;
12493 char *base;
12494
12495 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12496 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12497 return NULL;
12498
12499 attr = dwarf2_attr (die, DW_AT_name, cu);
12500 if (attr != NULL && DW_STRING (attr) != NULL)
12501 return NULL;
12502
12503 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12504 if (attr == NULL)
12505 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12506 if (attr == NULL || DW_STRING (attr) == NULL)
12507 return NULL;
12508
12509 /* dwarf2_name had to be already called. */
12510 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12511
12512 /* Strip the base name, keep any leading namespaces/classes. */
12513 base = strrchr (DW_STRING (attr), ':');
12514 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12515 return "";
12516
12517 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12518 &cu->objfile->objfile_obstack);
12519}
12520
fdde2d81 12521/* Return the name of the namespace/class that DIE is defined within,
0114d602 12522 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12523
0114d602
DJ
12524 For example, if we're within the method foo() in the following
12525 code:
12526
12527 namespace N {
12528 class C {
12529 void foo () {
12530 }
12531 };
12532 }
12533
12534 then determine_prefix on foo's die will return "N::C". */
fdde2d81 12535
0d5cff50 12536static const char *
e142c38c 12537determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12538{
0114d602
DJ
12539 struct die_info *parent, *spec_die;
12540 struct dwarf2_cu *spec_cu;
12541 struct type *parent_type;
96408a79 12542 char *retval;
63d06c5c 12543
f55ee35c
JK
12544 if (cu->language != language_cplus && cu->language != language_java
12545 && cu->language != language_fortran)
0114d602
DJ
12546 return "";
12547
96408a79
SA
12548 retval = anonymous_struct_prefix (die, cu);
12549 if (retval)
12550 return retval;
12551
0114d602
DJ
12552 /* We have to be careful in the presence of DW_AT_specification.
12553 For example, with GCC 3.4, given the code
12554
12555 namespace N {
12556 void foo() {
12557 // Definition of N::foo.
12558 }
12559 }
12560
12561 then we'll have a tree of DIEs like this:
12562
12563 1: DW_TAG_compile_unit
12564 2: DW_TAG_namespace // N
12565 3: DW_TAG_subprogram // declaration of N::foo
12566 4: DW_TAG_subprogram // definition of N::foo
12567 DW_AT_specification // refers to die #3
12568
12569 Thus, when processing die #4, we have to pretend that we're in
12570 the context of its DW_AT_specification, namely the contex of die
12571 #3. */
12572 spec_cu = cu;
12573 spec_die = die_specification (die, &spec_cu);
12574 if (spec_die == NULL)
12575 parent = die->parent;
12576 else
63d06c5c 12577 {
0114d602
DJ
12578 parent = spec_die->parent;
12579 cu = spec_cu;
63d06c5c 12580 }
0114d602
DJ
12581
12582 if (parent == NULL)
12583 return "";
98bfdba5
PA
12584 else if (parent->building_fullname)
12585 {
12586 const char *name;
12587 const char *parent_name;
12588
12589 /* It has been seen on RealView 2.2 built binaries,
12590 DW_TAG_template_type_param types actually _defined_ as
12591 children of the parent class:
12592
12593 enum E {};
12594 template class <class Enum> Class{};
12595 Class<enum E> class_e;
12596
12597 1: DW_TAG_class_type (Class)
12598 2: DW_TAG_enumeration_type (E)
12599 3: DW_TAG_enumerator (enum1:0)
12600 3: DW_TAG_enumerator (enum2:1)
12601 ...
12602 2: DW_TAG_template_type_param
12603 DW_AT_type DW_FORM_ref_udata (E)
12604
12605 Besides being broken debug info, it can put GDB into an
12606 infinite loop. Consider:
12607
12608 When we're building the full name for Class<E>, we'll start
12609 at Class, and go look over its template type parameters,
12610 finding E. We'll then try to build the full name of E, and
12611 reach here. We're now trying to build the full name of E,
12612 and look over the parent DIE for containing scope. In the
12613 broken case, if we followed the parent DIE of E, we'd again
12614 find Class, and once again go look at its template type
12615 arguments, etc., etc. Simply don't consider such parent die
12616 as source-level parent of this die (it can't be, the language
12617 doesn't allow it), and break the loop here. */
12618 name = dwarf2_name (die, cu);
12619 parent_name = dwarf2_name (parent, cu);
12620 complaint (&symfile_complaints,
12621 _("template param type '%s' defined within parent '%s'"),
12622 name ? name : "<unknown>",
12623 parent_name ? parent_name : "<unknown>");
12624 return "";
12625 }
63d06c5c 12626 else
0114d602
DJ
12627 switch (parent->tag)
12628 {
63d06c5c 12629 case DW_TAG_namespace:
0114d602 12630 parent_type = read_type_die (parent, cu);
acebe513
UW
12631 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12632 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12633 Work around this problem here. */
12634 if (cu->language == language_cplus
12635 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12636 return "";
0114d602
DJ
12637 /* We give a name to even anonymous namespaces. */
12638 return TYPE_TAG_NAME (parent_type);
63d06c5c 12639 case DW_TAG_class_type:
680b30c7 12640 case DW_TAG_interface_type:
63d06c5c 12641 case DW_TAG_structure_type:
0114d602 12642 case DW_TAG_union_type:
f55ee35c 12643 case DW_TAG_module:
0114d602
DJ
12644 parent_type = read_type_die (parent, cu);
12645 if (TYPE_TAG_NAME (parent_type) != NULL)
12646 return TYPE_TAG_NAME (parent_type);
12647 else
12648 /* An anonymous structure is only allowed non-static data
12649 members; no typedefs, no member functions, et cetera.
12650 So it does not need a prefix. */
12651 return "";
abc72ce4
DE
12652 case DW_TAG_compile_unit:
12653 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12654 if (cu->language == language_cplus
8b70b953 12655 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12656 && die->child != NULL
12657 && (die->tag == DW_TAG_class_type
12658 || die->tag == DW_TAG_structure_type
12659 || die->tag == DW_TAG_union_type))
12660 {
12661 char *name = guess_full_die_structure_name (die, cu);
12662 if (name != NULL)
12663 return name;
12664 }
12665 return "";
63d06c5c 12666 default:
8176b9b8 12667 return determine_prefix (parent, cu);
63d06c5c 12668 }
63d06c5c
DC
12669}
12670
3e43a32a
MS
12671/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12672 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12673 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12674 an obconcat, otherwise allocate storage for the result. The CU argument is
12675 used to determine the language and hence, the appropriate separator. */
987504bb 12676
f55ee35c 12677#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12678
12679static char *
f55ee35c
JK
12680typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12681 int physname, struct dwarf2_cu *cu)
63d06c5c 12682{
f55ee35c 12683 const char *lead = "";
5c315b68 12684 const char *sep;
63d06c5c 12685
3e43a32a
MS
12686 if (suffix == NULL || suffix[0] == '\0'
12687 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12688 sep = "";
12689 else if (cu->language == language_java)
12690 sep = ".";
f55ee35c
JK
12691 else if (cu->language == language_fortran && physname)
12692 {
12693 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12694 DW_AT_MIPS_linkage_name is preferred and used instead. */
12695
12696 lead = "__";
12697 sep = "_MOD_";
12698 }
987504bb
JJ
12699 else
12700 sep = "::";
63d06c5c 12701
6dd47d34
DE
12702 if (prefix == NULL)
12703 prefix = "";
12704 if (suffix == NULL)
12705 suffix = "";
12706
987504bb
JJ
12707 if (obs == NULL)
12708 {
3e43a32a
MS
12709 char *retval
12710 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12711
f55ee35c
JK
12712 strcpy (retval, lead);
12713 strcat (retval, prefix);
6dd47d34
DE
12714 strcat (retval, sep);
12715 strcat (retval, suffix);
63d06c5c
DC
12716 return retval;
12717 }
987504bb
JJ
12718 else
12719 {
12720 /* We have an obstack. */
f55ee35c 12721 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12722 }
63d06c5c
DC
12723}
12724
c906108c
SS
12725/* Return sibling of die, NULL if no sibling. */
12726
f9aca02d 12727static struct die_info *
fba45db2 12728sibling_die (struct die_info *die)
c906108c 12729{
639d11d3 12730 return die->sibling;
c906108c
SS
12731}
12732
71c25dea
TT
12733/* Get name of a die, return NULL if not found. */
12734
12735static char *
12736dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12737 struct obstack *obstack)
12738{
12739 if (name && cu->language == language_cplus)
12740 {
12741 char *canon_name = cp_canonicalize_string (name);
12742
12743 if (canon_name != NULL)
12744 {
12745 if (strcmp (canon_name, name) != 0)
12746 name = obsavestring (canon_name, strlen (canon_name),
12747 obstack);
12748 xfree (canon_name);
12749 }
12750 }
12751
12752 return name;
c906108c
SS
12753}
12754
9219021c
DC
12755/* Get name of a die, return NULL if not found. */
12756
12757static char *
e142c38c 12758dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12759{
12760 struct attribute *attr;
12761
e142c38c 12762 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12763 if ((!attr || !DW_STRING (attr))
12764 && die->tag != DW_TAG_class_type
12765 && die->tag != DW_TAG_interface_type
12766 && die->tag != DW_TAG_structure_type
12767 && die->tag != DW_TAG_union_type)
71c25dea
TT
12768 return NULL;
12769
12770 switch (die->tag)
12771 {
12772 case DW_TAG_compile_unit:
12773 /* Compilation units have a DW_AT_name that is a filename, not
12774 a source language identifier. */
12775 case DW_TAG_enumeration_type:
12776 case DW_TAG_enumerator:
12777 /* These tags always have simple identifiers already; no need
12778 to canonicalize them. */
12779 return DW_STRING (attr);
907af001 12780
418835cc
KS
12781 case DW_TAG_subprogram:
12782 /* Java constructors will all be named "<init>", so return
12783 the class name when we see this special case. */
12784 if (cu->language == language_java
12785 && DW_STRING (attr) != NULL
12786 && strcmp (DW_STRING (attr), "<init>") == 0)
12787 {
12788 struct dwarf2_cu *spec_cu = cu;
12789 struct die_info *spec_die;
12790
12791 /* GCJ will output '<init>' for Java constructor names.
12792 For this special case, return the name of the parent class. */
12793
12794 /* GCJ may output suprogram DIEs with AT_specification set.
12795 If so, use the name of the specified DIE. */
12796 spec_die = die_specification (die, &spec_cu);
12797 if (spec_die != NULL)
12798 return dwarf2_name (spec_die, spec_cu);
12799
12800 do
12801 {
12802 die = die->parent;
12803 if (die->tag == DW_TAG_class_type)
12804 return dwarf2_name (die, cu);
12805 }
12806 while (die->tag != DW_TAG_compile_unit);
12807 }
907af001
UW
12808 break;
12809
12810 case DW_TAG_class_type:
12811 case DW_TAG_interface_type:
12812 case DW_TAG_structure_type:
12813 case DW_TAG_union_type:
12814 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12815 structures or unions. These were of the form "._%d" in GCC 4.1,
12816 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12817 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12818 if (attr && DW_STRING (attr)
12819 && (strncmp (DW_STRING (attr), "._", 2) == 0
12820 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12821 return NULL;
53832f31
TT
12822
12823 /* GCC might emit a nameless typedef that has a linkage name. See
12824 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12825 if (!attr || DW_STRING (attr) == NULL)
12826 {
df5c6c50 12827 char *demangled = NULL;
53832f31
TT
12828
12829 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12830 if (attr == NULL)
12831 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12832
12833 if (attr == NULL || DW_STRING (attr) == NULL)
12834 return NULL;
12835
df5c6c50
JK
12836 /* Avoid demangling DW_STRING (attr) the second time on a second
12837 call for the same DIE. */
12838 if (!DW_STRING_IS_CANONICAL (attr))
12839 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12840
12841 if (demangled)
12842 {
96408a79
SA
12843 char *base;
12844
53832f31 12845 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12846 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12847 &cu->objfile->objfile_obstack);
53832f31
TT
12848 DW_STRING_IS_CANONICAL (attr) = 1;
12849 xfree (demangled);
96408a79
SA
12850
12851 /* Strip any leading namespaces/classes, keep only the base name.
12852 DW_AT_name for named DIEs does not contain the prefixes. */
12853 base = strrchr (DW_STRING (attr), ':');
12854 if (base && base > DW_STRING (attr) && base[-1] == ':')
12855 return &base[1];
12856 else
12857 return DW_STRING (attr);
53832f31
TT
12858 }
12859 }
907af001
UW
12860 break;
12861
71c25dea 12862 default:
907af001
UW
12863 break;
12864 }
12865
12866 if (!DW_STRING_IS_CANONICAL (attr))
12867 {
12868 DW_STRING (attr)
12869 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12870 &cu->objfile->objfile_obstack);
12871 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12872 }
907af001 12873 return DW_STRING (attr);
9219021c
DC
12874}
12875
12876/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12877 is none. *EXT_CU is the CU containing DIE on input, and the CU
12878 containing the return value on output. */
9219021c
DC
12879
12880static struct die_info *
f2f0e013 12881dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12882{
12883 struct attribute *attr;
9219021c 12884
f2f0e013 12885 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12886 if (attr == NULL)
12887 return NULL;
12888
f2f0e013 12889 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12890}
12891
c906108c
SS
12892/* Convert a DIE tag into its string name. */
12893
12894static char *
aa1ee363 12895dwarf_tag_name (unsigned tag)
c906108c
SS
12896{
12897 switch (tag)
12898 {
12899 case DW_TAG_padding:
12900 return "DW_TAG_padding";
12901 case DW_TAG_array_type:
12902 return "DW_TAG_array_type";
12903 case DW_TAG_class_type:
12904 return "DW_TAG_class_type";
12905 case DW_TAG_entry_point:
12906 return "DW_TAG_entry_point";
12907 case DW_TAG_enumeration_type:
12908 return "DW_TAG_enumeration_type";
12909 case DW_TAG_formal_parameter:
12910 return "DW_TAG_formal_parameter";
12911 case DW_TAG_imported_declaration:
12912 return "DW_TAG_imported_declaration";
12913 case DW_TAG_label:
12914 return "DW_TAG_label";
12915 case DW_TAG_lexical_block:
12916 return "DW_TAG_lexical_block";
12917 case DW_TAG_member:
12918 return "DW_TAG_member";
12919 case DW_TAG_pointer_type:
12920 return "DW_TAG_pointer_type";
12921 case DW_TAG_reference_type:
12922 return "DW_TAG_reference_type";
12923 case DW_TAG_compile_unit:
12924 return "DW_TAG_compile_unit";
12925 case DW_TAG_string_type:
12926 return "DW_TAG_string_type";
12927 case DW_TAG_structure_type:
12928 return "DW_TAG_structure_type";
12929 case DW_TAG_subroutine_type:
12930 return "DW_TAG_subroutine_type";
12931 case DW_TAG_typedef:
12932 return "DW_TAG_typedef";
12933 case DW_TAG_union_type:
12934 return "DW_TAG_union_type";
12935 case DW_TAG_unspecified_parameters:
12936 return "DW_TAG_unspecified_parameters";
12937 case DW_TAG_variant:
12938 return "DW_TAG_variant";
12939 case DW_TAG_common_block:
12940 return "DW_TAG_common_block";
12941 case DW_TAG_common_inclusion:
12942 return "DW_TAG_common_inclusion";
12943 case DW_TAG_inheritance:
12944 return "DW_TAG_inheritance";
12945 case DW_TAG_inlined_subroutine:
12946 return "DW_TAG_inlined_subroutine";
12947 case DW_TAG_module:
12948 return "DW_TAG_module";
12949 case DW_TAG_ptr_to_member_type:
12950 return "DW_TAG_ptr_to_member_type";
12951 case DW_TAG_set_type:
12952 return "DW_TAG_set_type";
12953 case DW_TAG_subrange_type:
12954 return "DW_TAG_subrange_type";
12955 case DW_TAG_with_stmt:
12956 return "DW_TAG_with_stmt";
12957 case DW_TAG_access_declaration:
12958 return "DW_TAG_access_declaration";
12959 case DW_TAG_base_type:
12960 return "DW_TAG_base_type";
12961 case DW_TAG_catch_block:
12962 return "DW_TAG_catch_block";
12963 case DW_TAG_const_type:
12964 return "DW_TAG_const_type";
12965 case DW_TAG_constant:
12966 return "DW_TAG_constant";
12967 case DW_TAG_enumerator:
12968 return "DW_TAG_enumerator";
12969 case DW_TAG_file_type:
12970 return "DW_TAG_file_type";
12971 case DW_TAG_friend:
12972 return "DW_TAG_friend";
12973 case DW_TAG_namelist:
12974 return "DW_TAG_namelist";
12975 case DW_TAG_namelist_item:
12976 return "DW_TAG_namelist_item";
12977 case DW_TAG_packed_type:
12978 return "DW_TAG_packed_type";
12979 case DW_TAG_subprogram:
12980 return "DW_TAG_subprogram";
12981 case DW_TAG_template_type_param:
12982 return "DW_TAG_template_type_param";
12983 case DW_TAG_template_value_param:
12984 return "DW_TAG_template_value_param";
12985 case DW_TAG_thrown_type:
12986 return "DW_TAG_thrown_type";
12987 case DW_TAG_try_block:
12988 return "DW_TAG_try_block";
12989 case DW_TAG_variant_part:
12990 return "DW_TAG_variant_part";
12991 case DW_TAG_variable:
12992 return "DW_TAG_variable";
12993 case DW_TAG_volatile_type:
12994 return "DW_TAG_volatile_type";
d9fa45fe
DC
12995 case DW_TAG_dwarf_procedure:
12996 return "DW_TAG_dwarf_procedure";
12997 case DW_TAG_restrict_type:
12998 return "DW_TAG_restrict_type";
12999 case DW_TAG_interface_type:
13000 return "DW_TAG_interface_type";
13001 case DW_TAG_namespace:
13002 return "DW_TAG_namespace";
13003 case DW_TAG_imported_module:
13004 return "DW_TAG_imported_module";
13005 case DW_TAG_unspecified_type:
13006 return "DW_TAG_unspecified_type";
13007 case DW_TAG_partial_unit:
13008 return "DW_TAG_partial_unit";
13009 case DW_TAG_imported_unit:
13010 return "DW_TAG_imported_unit";
b7619582
GF
13011 case DW_TAG_condition:
13012 return "DW_TAG_condition";
13013 case DW_TAG_shared_type:
13014 return "DW_TAG_shared_type";
348e048f
DE
13015 case DW_TAG_type_unit:
13016 return "DW_TAG_type_unit";
c906108c
SS
13017 case DW_TAG_MIPS_loop:
13018 return "DW_TAG_MIPS_loop";
b7619582
GF
13019 case DW_TAG_HP_array_descriptor:
13020 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13021 case DW_TAG_format_label:
13022 return "DW_TAG_format_label";
13023 case DW_TAG_function_template:
13024 return "DW_TAG_function_template";
13025 case DW_TAG_class_template:
13026 return "DW_TAG_class_template";
b7619582
GF
13027 case DW_TAG_GNU_BINCL:
13028 return "DW_TAG_GNU_BINCL";
13029 case DW_TAG_GNU_EINCL:
13030 return "DW_TAG_GNU_EINCL";
13031 case DW_TAG_upc_shared_type:
13032 return "DW_TAG_upc_shared_type";
13033 case DW_TAG_upc_strict_type:
13034 return "DW_TAG_upc_strict_type";
13035 case DW_TAG_upc_relaxed_type:
13036 return "DW_TAG_upc_relaxed_type";
13037 case DW_TAG_PGI_kanji_type:
13038 return "DW_TAG_PGI_kanji_type";
13039 case DW_TAG_PGI_interface_block:
13040 return "DW_TAG_PGI_interface_block";
96408a79
SA
13041 case DW_TAG_GNU_call_site:
13042 return "DW_TAG_GNU_call_site";
c906108c
SS
13043 default:
13044 return "DW_TAG_<unknown>";
13045 }
13046}
13047
13048/* Convert a DWARF attribute code into its string name. */
13049
13050static char *
aa1ee363 13051dwarf_attr_name (unsigned attr)
c906108c
SS
13052{
13053 switch (attr)
13054 {
13055 case DW_AT_sibling:
13056 return "DW_AT_sibling";
13057 case DW_AT_location:
13058 return "DW_AT_location";
13059 case DW_AT_name:
13060 return "DW_AT_name";
13061 case DW_AT_ordering:
13062 return "DW_AT_ordering";
13063 case DW_AT_subscr_data:
13064 return "DW_AT_subscr_data";
13065 case DW_AT_byte_size:
13066 return "DW_AT_byte_size";
13067 case DW_AT_bit_offset:
13068 return "DW_AT_bit_offset";
13069 case DW_AT_bit_size:
13070 return "DW_AT_bit_size";
13071 case DW_AT_element_list:
13072 return "DW_AT_element_list";
13073 case DW_AT_stmt_list:
13074 return "DW_AT_stmt_list";
13075 case DW_AT_low_pc:
13076 return "DW_AT_low_pc";
13077 case DW_AT_high_pc:
13078 return "DW_AT_high_pc";
13079 case DW_AT_language:
13080 return "DW_AT_language";
13081 case DW_AT_member:
13082 return "DW_AT_member";
13083 case DW_AT_discr:
13084 return "DW_AT_discr";
13085 case DW_AT_discr_value:
13086 return "DW_AT_discr_value";
13087 case DW_AT_visibility:
13088 return "DW_AT_visibility";
13089 case DW_AT_import:
13090 return "DW_AT_import";
13091 case DW_AT_string_length:
13092 return "DW_AT_string_length";
13093 case DW_AT_common_reference:
13094 return "DW_AT_common_reference";
13095 case DW_AT_comp_dir:
13096 return "DW_AT_comp_dir";
13097 case DW_AT_const_value:
13098 return "DW_AT_const_value";
13099 case DW_AT_containing_type:
13100 return "DW_AT_containing_type";
13101 case DW_AT_default_value:
13102 return "DW_AT_default_value";
13103 case DW_AT_inline:
13104 return "DW_AT_inline";
13105 case DW_AT_is_optional:
13106 return "DW_AT_is_optional";
13107 case DW_AT_lower_bound:
13108 return "DW_AT_lower_bound";
13109 case DW_AT_producer:
13110 return "DW_AT_producer";
13111 case DW_AT_prototyped:
13112 return "DW_AT_prototyped";
13113 case DW_AT_return_addr:
13114 return "DW_AT_return_addr";
13115 case DW_AT_start_scope:
13116 return "DW_AT_start_scope";
09fa0d7c
JK
13117 case DW_AT_bit_stride:
13118 return "DW_AT_bit_stride";
c906108c
SS
13119 case DW_AT_upper_bound:
13120 return "DW_AT_upper_bound";
13121 case DW_AT_abstract_origin:
13122 return "DW_AT_abstract_origin";
13123 case DW_AT_accessibility:
13124 return "DW_AT_accessibility";
13125 case DW_AT_address_class:
13126 return "DW_AT_address_class";
13127 case DW_AT_artificial:
13128 return "DW_AT_artificial";
13129 case DW_AT_base_types:
13130 return "DW_AT_base_types";
13131 case DW_AT_calling_convention:
13132 return "DW_AT_calling_convention";
13133 case DW_AT_count:
13134 return "DW_AT_count";
13135 case DW_AT_data_member_location:
13136 return "DW_AT_data_member_location";
13137 case DW_AT_decl_column:
13138 return "DW_AT_decl_column";
13139 case DW_AT_decl_file:
13140 return "DW_AT_decl_file";
13141 case DW_AT_decl_line:
13142 return "DW_AT_decl_line";
13143 case DW_AT_declaration:
13144 return "DW_AT_declaration";
13145 case DW_AT_discr_list:
13146 return "DW_AT_discr_list";
13147 case DW_AT_encoding:
13148 return "DW_AT_encoding";
13149 case DW_AT_external:
13150 return "DW_AT_external";
13151 case DW_AT_frame_base:
13152 return "DW_AT_frame_base";
13153 case DW_AT_friend:
13154 return "DW_AT_friend";
13155 case DW_AT_identifier_case:
13156 return "DW_AT_identifier_case";
13157 case DW_AT_macro_info:
13158 return "DW_AT_macro_info";
13159 case DW_AT_namelist_items:
13160 return "DW_AT_namelist_items";
13161 case DW_AT_priority:
13162 return "DW_AT_priority";
13163 case DW_AT_segment:
13164 return "DW_AT_segment";
13165 case DW_AT_specification:
13166 return "DW_AT_specification";
13167 case DW_AT_static_link:
13168 return "DW_AT_static_link";
13169 case DW_AT_type:
13170 return "DW_AT_type";
13171 case DW_AT_use_location:
13172 return "DW_AT_use_location";
13173 case DW_AT_variable_parameter:
13174 return "DW_AT_variable_parameter";
13175 case DW_AT_virtuality:
13176 return "DW_AT_virtuality";
13177 case DW_AT_vtable_elem_location:
13178 return "DW_AT_vtable_elem_location";
b7619582 13179 /* DWARF 3 values. */
d9fa45fe
DC
13180 case DW_AT_allocated:
13181 return "DW_AT_allocated";
13182 case DW_AT_associated:
13183 return "DW_AT_associated";
13184 case DW_AT_data_location:
13185 return "DW_AT_data_location";
09fa0d7c
JK
13186 case DW_AT_byte_stride:
13187 return "DW_AT_byte_stride";
d9fa45fe
DC
13188 case DW_AT_entry_pc:
13189 return "DW_AT_entry_pc";
13190 case DW_AT_use_UTF8:
13191 return "DW_AT_use_UTF8";
13192 case DW_AT_extension:
13193 return "DW_AT_extension";
13194 case DW_AT_ranges:
13195 return "DW_AT_ranges";
13196 case DW_AT_trampoline:
13197 return "DW_AT_trampoline";
13198 case DW_AT_call_column:
13199 return "DW_AT_call_column";
13200 case DW_AT_call_file:
13201 return "DW_AT_call_file";
13202 case DW_AT_call_line:
13203 return "DW_AT_call_line";
b7619582
GF
13204 case DW_AT_description:
13205 return "DW_AT_description";
13206 case DW_AT_binary_scale:
13207 return "DW_AT_binary_scale";
13208 case DW_AT_decimal_scale:
13209 return "DW_AT_decimal_scale";
13210 case DW_AT_small:
13211 return "DW_AT_small";
13212 case DW_AT_decimal_sign:
13213 return "DW_AT_decimal_sign";
13214 case DW_AT_digit_count:
13215 return "DW_AT_digit_count";
13216 case DW_AT_picture_string:
13217 return "DW_AT_picture_string";
13218 case DW_AT_mutable:
13219 return "DW_AT_mutable";
13220 case DW_AT_threads_scaled:
13221 return "DW_AT_threads_scaled";
13222 case DW_AT_explicit:
13223 return "DW_AT_explicit";
13224 case DW_AT_object_pointer:
13225 return "DW_AT_object_pointer";
13226 case DW_AT_endianity:
13227 return "DW_AT_endianity";
13228 case DW_AT_elemental:
13229 return "DW_AT_elemental";
13230 case DW_AT_pure:
13231 return "DW_AT_pure";
13232 case DW_AT_recursive:
13233 return "DW_AT_recursive";
348e048f
DE
13234 /* DWARF 4 values. */
13235 case DW_AT_signature:
13236 return "DW_AT_signature";
31ef98ae
TT
13237 case DW_AT_linkage_name:
13238 return "DW_AT_linkage_name";
b7619582 13239 /* SGI/MIPS extensions. */
c764a876 13240#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13241 case DW_AT_MIPS_fde:
13242 return "DW_AT_MIPS_fde";
c764a876 13243#endif
c906108c
SS
13244 case DW_AT_MIPS_loop_begin:
13245 return "DW_AT_MIPS_loop_begin";
13246 case DW_AT_MIPS_tail_loop_begin:
13247 return "DW_AT_MIPS_tail_loop_begin";
13248 case DW_AT_MIPS_epilog_begin:
13249 return "DW_AT_MIPS_epilog_begin";
13250 case DW_AT_MIPS_loop_unroll_factor:
13251 return "DW_AT_MIPS_loop_unroll_factor";
13252 case DW_AT_MIPS_software_pipeline_depth:
13253 return "DW_AT_MIPS_software_pipeline_depth";
13254 case DW_AT_MIPS_linkage_name:
13255 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13256 case DW_AT_MIPS_stride:
13257 return "DW_AT_MIPS_stride";
13258 case DW_AT_MIPS_abstract_name:
13259 return "DW_AT_MIPS_abstract_name";
13260 case DW_AT_MIPS_clone_origin:
13261 return "DW_AT_MIPS_clone_origin";
13262 case DW_AT_MIPS_has_inlines:
13263 return "DW_AT_MIPS_has_inlines";
b7619582 13264 /* HP extensions. */
c764a876 13265#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13266 case DW_AT_HP_block_index:
13267 return "DW_AT_HP_block_index";
c764a876 13268#endif
b7619582
GF
13269 case DW_AT_HP_unmodifiable:
13270 return "DW_AT_HP_unmodifiable";
13271 case DW_AT_HP_actuals_stmt_list:
13272 return "DW_AT_HP_actuals_stmt_list";
13273 case DW_AT_HP_proc_per_section:
13274 return "DW_AT_HP_proc_per_section";
13275 case DW_AT_HP_raw_data_ptr:
13276 return "DW_AT_HP_raw_data_ptr";
13277 case DW_AT_HP_pass_by_reference:
13278 return "DW_AT_HP_pass_by_reference";
13279 case DW_AT_HP_opt_level:
13280 return "DW_AT_HP_opt_level";
13281 case DW_AT_HP_prof_version_id:
13282 return "DW_AT_HP_prof_version_id";
13283 case DW_AT_HP_opt_flags:
13284 return "DW_AT_HP_opt_flags";
13285 case DW_AT_HP_cold_region_low_pc:
13286 return "DW_AT_HP_cold_region_low_pc";
13287 case DW_AT_HP_cold_region_high_pc:
13288 return "DW_AT_HP_cold_region_high_pc";
13289 case DW_AT_HP_all_variables_modifiable:
13290 return "DW_AT_HP_all_variables_modifiable";
13291 case DW_AT_HP_linkage_name:
13292 return "DW_AT_HP_linkage_name";
13293 case DW_AT_HP_prof_flags:
13294 return "DW_AT_HP_prof_flags";
13295 /* GNU extensions. */
c906108c
SS
13296 case DW_AT_sf_names:
13297 return "DW_AT_sf_names";
13298 case DW_AT_src_info:
13299 return "DW_AT_src_info";
13300 case DW_AT_mac_info:
13301 return "DW_AT_mac_info";
13302 case DW_AT_src_coords:
13303 return "DW_AT_src_coords";
13304 case DW_AT_body_begin:
13305 return "DW_AT_body_begin";
13306 case DW_AT_body_end:
13307 return "DW_AT_body_end";
f5f8a009
EZ
13308 case DW_AT_GNU_vector:
13309 return "DW_AT_GNU_vector";
2de00c64
DE
13310 case DW_AT_GNU_odr_signature:
13311 return "DW_AT_GNU_odr_signature";
b7619582
GF
13312 /* VMS extensions. */
13313 case DW_AT_VMS_rtnbeg_pd_address:
13314 return "DW_AT_VMS_rtnbeg_pd_address";
13315 /* UPC extension. */
13316 case DW_AT_upc_threads_scaled:
13317 return "DW_AT_upc_threads_scaled";
13318 /* PGI (STMicroelectronics) extensions. */
13319 case DW_AT_PGI_lbase:
13320 return "DW_AT_PGI_lbase";
13321 case DW_AT_PGI_soffset:
13322 return "DW_AT_PGI_soffset";
13323 case DW_AT_PGI_lstride:
13324 return "DW_AT_PGI_lstride";
c906108c
SS
13325 default:
13326 return "DW_AT_<unknown>";
13327 }
13328}
13329
13330/* Convert a DWARF value form code into its string name. */
13331
13332static char *
aa1ee363 13333dwarf_form_name (unsigned form)
c906108c
SS
13334{
13335 switch (form)
13336 {
13337 case DW_FORM_addr:
13338 return "DW_FORM_addr";
13339 case DW_FORM_block2:
13340 return "DW_FORM_block2";
13341 case DW_FORM_block4:
13342 return "DW_FORM_block4";
13343 case DW_FORM_data2:
13344 return "DW_FORM_data2";
13345 case DW_FORM_data4:
13346 return "DW_FORM_data4";
13347 case DW_FORM_data8:
13348 return "DW_FORM_data8";
13349 case DW_FORM_string:
13350 return "DW_FORM_string";
13351 case DW_FORM_block:
13352 return "DW_FORM_block";
13353 case DW_FORM_block1:
13354 return "DW_FORM_block1";
13355 case DW_FORM_data1:
13356 return "DW_FORM_data1";
13357 case DW_FORM_flag:
13358 return "DW_FORM_flag";
13359 case DW_FORM_sdata:
13360 return "DW_FORM_sdata";
13361 case DW_FORM_strp:
13362 return "DW_FORM_strp";
13363 case DW_FORM_udata:
13364 return "DW_FORM_udata";
13365 case DW_FORM_ref_addr:
13366 return "DW_FORM_ref_addr";
13367 case DW_FORM_ref1:
13368 return "DW_FORM_ref1";
13369 case DW_FORM_ref2:
13370 return "DW_FORM_ref2";
13371 case DW_FORM_ref4:
13372 return "DW_FORM_ref4";
13373 case DW_FORM_ref8:
13374 return "DW_FORM_ref8";
13375 case DW_FORM_ref_udata:
13376 return "DW_FORM_ref_udata";
13377 case DW_FORM_indirect:
13378 return "DW_FORM_indirect";
348e048f
DE
13379 case DW_FORM_sec_offset:
13380 return "DW_FORM_sec_offset";
13381 case DW_FORM_exprloc:
13382 return "DW_FORM_exprloc";
13383 case DW_FORM_flag_present:
13384 return "DW_FORM_flag_present";
55f1336d
TT
13385 case DW_FORM_ref_sig8:
13386 return "DW_FORM_ref_sig8";
c906108c
SS
13387 default:
13388 return "DW_FORM_<unknown>";
13389 }
13390}
13391
13392/* Convert a DWARF stack opcode into its string name. */
13393
9eae7c52 13394const char *
b1bfef65 13395dwarf_stack_op_name (unsigned op)
c906108c
SS
13396{
13397 switch (op)
13398 {
13399 case DW_OP_addr:
13400 return "DW_OP_addr";
13401 case DW_OP_deref:
13402 return "DW_OP_deref";
13403 case DW_OP_const1u:
13404 return "DW_OP_const1u";
13405 case DW_OP_const1s:
13406 return "DW_OP_const1s";
13407 case DW_OP_const2u:
13408 return "DW_OP_const2u";
13409 case DW_OP_const2s:
13410 return "DW_OP_const2s";
13411 case DW_OP_const4u:
13412 return "DW_OP_const4u";
13413 case DW_OP_const4s:
13414 return "DW_OP_const4s";
13415 case DW_OP_const8u:
13416 return "DW_OP_const8u";
13417 case DW_OP_const8s:
13418 return "DW_OP_const8s";
13419 case DW_OP_constu:
13420 return "DW_OP_constu";
13421 case DW_OP_consts:
13422 return "DW_OP_consts";
13423 case DW_OP_dup:
13424 return "DW_OP_dup";
13425 case DW_OP_drop:
13426 return "DW_OP_drop";
13427 case DW_OP_over:
13428 return "DW_OP_over";
13429 case DW_OP_pick:
13430 return "DW_OP_pick";
13431 case DW_OP_swap:
13432 return "DW_OP_swap";
13433 case DW_OP_rot:
13434 return "DW_OP_rot";
13435 case DW_OP_xderef:
13436 return "DW_OP_xderef";
13437 case DW_OP_abs:
13438 return "DW_OP_abs";
13439 case DW_OP_and:
13440 return "DW_OP_and";
13441 case DW_OP_div:
13442 return "DW_OP_div";
13443 case DW_OP_minus:
13444 return "DW_OP_minus";
13445 case DW_OP_mod:
13446 return "DW_OP_mod";
13447 case DW_OP_mul:
13448 return "DW_OP_mul";
13449 case DW_OP_neg:
13450 return "DW_OP_neg";
13451 case DW_OP_not:
13452 return "DW_OP_not";
13453 case DW_OP_or:
13454 return "DW_OP_or";
13455 case DW_OP_plus:
13456 return "DW_OP_plus";
13457 case DW_OP_plus_uconst:
13458 return "DW_OP_plus_uconst";
13459 case DW_OP_shl:
13460 return "DW_OP_shl";
13461 case DW_OP_shr:
13462 return "DW_OP_shr";
13463 case DW_OP_shra:
13464 return "DW_OP_shra";
13465 case DW_OP_xor:
13466 return "DW_OP_xor";
13467 case DW_OP_bra:
13468 return "DW_OP_bra";
13469 case DW_OP_eq:
13470 return "DW_OP_eq";
13471 case DW_OP_ge:
13472 return "DW_OP_ge";
13473 case DW_OP_gt:
13474 return "DW_OP_gt";
13475 case DW_OP_le:
13476 return "DW_OP_le";
13477 case DW_OP_lt:
13478 return "DW_OP_lt";
13479 case DW_OP_ne:
13480 return "DW_OP_ne";
13481 case DW_OP_skip:
13482 return "DW_OP_skip";
13483 case DW_OP_lit0:
13484 return "DW_OP_lit0";
13485 case DW_OP_lit1:
13486 return "DW_OP_lit1";
13487 case DW_OP_lit2:
13488 return "DW_OP_lit2";
13489 case DW_OP_lit3:
13490 return "DW_OP_lit3";
13491 case DW_OP_lit4:
13492 return "DW_OP_lit4";
13493 case DW_OP_lit5:
13494 return "DW_OP_lit5";
13495 case DW_OP_lit6:
13496 return "DW_OP_lit6";
13497 case DW_OP_lit7:
13498 return "DW_OP_lit7";
13499 case DW_OP_lit8:
13500 return "DW_OP_lit8";
13501 case DW_OP_lit9:
13502 return "DW_OP_lit9";
13503 case DW_OP_lit10:
13504 return "DW_OP_lit10";
13505 case DW_OP_lit11:
13506 return "DW_OP_lit11";
13507 case DW_OP_lit12:
13508 return "DW_OP_lit12";
13509 case DW_OP_lit13:
13510 return "DW_OP_lit13";
13511 case DW_OP_lit14:
13512 return "DW_OP_lit14";
13513 case DW_OP_lit15:
13514 return "DW_OP_lit15";
13515 case DW_OP_lit16:
13516 return "DW_OP_lit16";
13517 case DW_OP_lit17:
13518 return "DW_OP_lit17";
13519 case DW_OP_lit18:
13520 return "DW_OP_lit18";
13521 case DW_OP_lit19:
13522 return "DW_OP_lit19";
13523 case DW_OP_lit20:
13524 return "DW_OP_lit20";
13525 case DW_OP_lit21:
13526 return "DW_OP_lit21";
13527 case DW_OP_lit22:
13528 return "DW_OP_lit22";
13529 case DW_OP_lit23:
13530 return "DW_OP_lit23";
13531 case DW_OP_lit24:
13532 return "DW_OP_lit24";
13533 case DW_OP_lit25:
13534 return "DW_OP_lit25";
13535 case DW_OP_lit26:
13536 return "DW_OP_lit26";
13537 case DW_OP_lit27:
13538 return "DW_OP_lit27";
13539 case DW_OP_lit28:
13540 return "DW_OP_lit28";
13541 case DW_OP_lit29:
13542 return "DW_OP_lit29";
13543 case DW_OP_lit30:
13544 return "DW_OP_lit30";
13545 case DW_OP_lit31:
13546 return "DW_OP_lit31";
13547 case DW_OP_reg0:
13548 return "DW_OP_reg0";
13549 case DW_OP_reg1:
13550 return "DW_OP_reg1";
13551 case DW_OP_reg2:
13552 return "DW_OP_reg2";
13553 case DW_OP_reg3:
13554 return "DW_OP_reg3";
13555 case DW_OP_reg4:
13556 return "DW_OP_reg4";
13557 case DW_OP_reg5:
13558 return "DW_OP_reg5";
13559 case DW_OP_reg6:
13560 return "DW_OP_reg6";
13561 case DW_OP_reg7:
13562 return "DW_OP_reg7";
13563 case DW_OP_reg8:
13564 return "DW_OP_reg8";
13565 case DW_OP_reg9:
13566 return "DW_OP_reg9";
13567 case DW_OP_reg10:
13568 return "DW_OP_reg10";
13569 case DW_OP_reg11:
13570 return "DW_OP_reg11";
13571 case DW_OP_reg12:
13572 return "DW_OP_reg12";
13573 case DW_OP_reg13:
13574 return "DW_OP_reg13";
13575 case DW_OP_reg14:
13576 return "DW_OP_reg14";
13577 case DW_OP_reg15:
13578 return "DW_OP_reg15";
13579 case DW_OP_reg16:
13580 return "DW_OP_reg16";
13581 case DW_OP_reg17:
13582 return "DW_OP_reg17";
13583 case DW_OP_reg18:
13584 return "DW_OP_reg18";
13585 case DW_OP_reg19:
13586 return "DW_OP_reg19";
13587 case DW_OP_reg20:
13588 return "DW_OP_reg20";
13589 case DW_OP_reg21:
13590 return "DW_OP_reg21";
13591 case DW_OP_reg22:
13592 return "DW_OP_reg22";
13593 case DW_OP_reg23:
13594 return "DW_OP_reg23";
13595 case DW_OP_reg24:
13596 return "DW_OP_reg24";
13597 case DW_OP_reg25:
13598 return "DW_OP_reg25";
13599 case DW_OP_reg26:
13600 return "DW_OP_reg26";
13601 case DW_OP_reg27:
13602 return "DW_OP_reg27";
13603 case DW_OP_reg28:
13604 return "DW_OP_reg28";
13605 case DW_OP_reg29:
13606 return "DW_OP_reg29";
13607 case DW_OP_reg30:
13608 return "DW_OP_reg30";
13609 case DW_OP_reg31:
13610 return "DW_OP_reg31";
13611 case DW_OP_breg0:
13612 return "DW_OP_breg0";
13613 case DW_OP_breg1:
13614 return "DW_OP_breg1";
13615 case DW_OP_breg2:
13616 return "DW_OP_breg2";
13617 case DW_OP_breg3:
13618 return "DW_OP_breg3";
13619 case DW_OP_breg4:
13620 return "DW_OP_breg4";
13621 case DW_OP_breg5:
13622 return "DW_OP_breg5";
13623 case DW_OP_breg6:
13624 return "DW_OP_breg6";
13625 case DW_OP_breg7:
13626 return "DW_OP_breg7";
13627 case DW_OP_breg8:
13628 return "DW_OP_breg8";
13629 case DW_OP_breg9:
13630 return "DW_OP_breg9";
13631 case DW_OP_breg10:
13632 return "DW_OP_breg10";
13633 case DW_OP_breg11:
13634 return "DW_OP_breg11";
13635 case DW_OP_breg12:
13636 return "DW_OP_breg12";
13637 case DW_OP_breg13:
13638 return "DW_OP_breg13";
13639 case DW_OP_breg14:
13640 return "DW_OP_breg14";
13641 case DW_OP_breg15:
13642 return "DW_OP_breg15";
13643 case DW_OP_breg16:
13644 return "DW_OP_breg16";
13645 case DW_OP_breg17:
13646 return "DW_OP_breg17";
13647 case DW_OP_breg18:
13648 return "DW_OP_breg18";
13649 case DW_OP_breg19:
13650 return "DW_OP_breg19";
13651 case DW_OP_breg20:
13652 return "DW_OP_breg20";
13653 case DW_OP_breg21:
13654 return "DW_OP_breg21";
13655 case DW_OP_breg22:
13656 return "DW_OP_breg22";
13657 case DW_OP_breg23:
13658 return "DW_OP_breg23";
13659 case DW_OP_breg24:
13660 return "DW_OP_breg24";
13661 case DW_OP_breg25:
13662 return "DW_OP_breg25";
13663 case DW_OP_breg26:
13664 return "DW_OP_breg26";
13665 case DW_OP_breg27:
13666 return "DW_OP_breg27";
13667 case DW_OP_breg28:
13668 return "DW_OP_breg28";
13669 case DW_OP_breg29:
13670 return "DW_OP_breg29";
13671 case DW_OP_breg30:
13672 return "DW_OP_breg30";
13673 case DW_OP_breg31:
13674 return "DW_OP_breg31";
13675 case DW_OP_regx:
13676 return "DW_OP_regx";
13677 case DW_OP_fbreg:
13678 return "DW_OP_fbreg";
13679 case DW_OP_bregx:
13680 return "DW_OP_bregx";
13681 case DW_OP_piece:
13682 return "DW_OP_piece";
13683 case DW_OP_deref_size:
13684 return "DW_OP_deref_size";
13685 case DW_OP_xderef_size:
13686 return "DW_OP_xderef_size";
13687 case DW_OP_nop:
13688 return "DW_OP_nop";
b7619582 13689 /* DWARF 3 extensions. */
ed348acc
EZ
13690 case DW_OP_push_object_address:
13691 return "DW_OP_push_object_address";
13692 case DW_OP_call2:
13693 return "DW_OP_call2";
13694 case DW_OP_call4:
13695 return "DW_OP_call4";
13696 case DW_OP_call_ref:
13697 return "DW_OP_call_ref";
b7619582
GF
13698 case DW_OP_form_tls_address:
13699 return "DW_OP_form_tls_address";
13700 case DW_OP_call_frame_cfa:
13701 return "DW_OP_call_frame_cfa";
13702 case DW_OP_bit_piece:
13703 return "DW_OP_bit_piece";
9eae7c52
TT
13704 /* DWARF 4 extensions. */
13705 case DW_OP_implicit_value:
13706 return "DW_OP_implicit_value";
13707 case DW_OP_stack_value:
13708 return "DW_OP_stack_value";
13709 /* GNU extensions. */
ed348acc
EZ
13710 case DW_OP_GNU_push_tls_address:
13711 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13712 case DW_OP_GNU_uninit:
13713 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13714 case DW_OP_GNU_implicit_pointer:
13715 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13716 case DW_OP_GNU_entry_value:
13717 return "DW_OP_GNU_entry_value";
13718 case DW_OP_GNU_const_type:
13719 return "DW_OP_GNU_const_type";
13720 case DW_OP_GNU_regval_type:
13721 return "DW_OP_GNU_regval_type";
13722 case DW_OP_GNU_deref_type:
13723 return "DW_OP_GNU_deref_type";
13724 case DW_OP_GNU_convert:
13725 return "DW_OP_GNU_convert";
13726 case DW_OP_GNU_reinterpret:
13727 return "DW_OP_GNU_reinterpret";
c906108c 13728 default:
b1bfef65 13729 return NULL;
c906108c
SS
13730 }
13731}
13732
13733static char *
fba45db2 13734dwarf_bool_name (unsigned mybool)
c906108c
SS
13735{
13736 if (mybool)
13737 return "TRUE";
13738 else
13739 return "FALSE";
13740}
13741
13742/* Convert a DWARF type code into its string name. */
13743
13744static char *
aa1ee363 13745dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13746{
13747 switch (enc)
13748 {
b7619582
GF
13749 case DW_ATE_void:
13750 return "DW_ATE_void";
c906108c
SS
13751 case DW_ATE_address:
13752 return "DW_ATE_address";
13753 case DW_ATE_boolean:
13754 return "DW_ATE_boolean";
13755 case DW_ATE_complex_float:
13756 return "DW_ATE_complex_float";
13757 case DW_ATE_float:
13758 return "DW_ATE_float";
13759 case DW_ATE_signed:
13760 return "DW_ATE_signed";
13761 case DW_ATE_signed_char:
13762 return "DW_ATE_signed_char";
13763 case DW_ATE_unsigned:
13764 return "DW_ATE_unsigned";
13765 case DW_ATE_unsigned_char:
13766 return "DW_ATE_unsigned_char";
b7619582 13767 /* DWARF 3. */
d9fa45fe
DC
13768 case DW_ATE_imaginary_float:
13769 return "DW_ATE_imaginary_float";
b7619582
GF
13770 case DW_ATE_packed_decimal:
13771 return "DW_ATE_packed_decimal";
13772 case DW_ATE_numeric_string:
13773 return "DW_ATE_numeric_string";
13774 case DW_ATE_edited:
13775 return "DW_ATE_edited";
13776 case DW_ATE_signed_fixed:
13777 return "DW_ATE_signed_fixed";
13778 case DW_ATE_unsigned_fixed:
13779 return "DW_ATE_unsigned_fixed";
13780 case DW_ATE_decimal_float:
13781 return "DW_ATE_decimal_float";
75079b2b
TT
13782 /* DWARF 4. */
13783 case DW_ATE_UTF:
13784 return "DW_ATE_UTF";
b7619582
GF
13785 /* HP extensions. */
13786 case DW_ATE_HP_float80:
13787 return "DW_ATE_HP_float80";
13788 case DW_ATE_HP_complex_float80:
13789 return "DW_ATE_HP_complex_float80";
13790 case DW_ATE_HP_float128:
13791 return "DW_ATE_HP_float128";
13792 case DW_ATE_HP_complex_float128:
13793 return "DW_ATE_HP_complex_float128";
13794 case DW_ATE_HP_floathpintel:
13795 return "DW_ATE_HP_floathpintel";
13796 case DW_ATE_HP_imaginary_float80:
13797 return "DW_ATE_HP_imaginary_float80";
13798 case DW_ATE_HP_imaginary_float128:
13799 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13800 default:
13801 return "DW_ATE_<unknown>";
13802 }
13803}
13804
0963b4bd 13805/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13806
13807#if 0
13808static char *
aa1ee363 13809dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13810{
13811 switch (cfi_opc)
13812 {
13813 case DW_CFA_advance_loc:
13814 return "DW_CFA_advance_loc";
13815 case DW_CFA_offset:
13816 return "DW_CFA_offset";
13817 case DW_CFA_restore:
13818 return "DW_CFA_restore";
13819 case DW_CFA_nop:
13820 return "DW_CFA_nop";
13821 case DW_CFA_set_loc:
13822 return "DW_CFA_set_loc";
13823 case DW_CFA_advance_loc1:
13824 return "DW_CFA_advance_loc1";
13825 case DW_CFA_advance_loc2:
13826 return "DW_CFA_advance_loc2";
13827 case DW_CFA_advance_loc4:
13828 return "DW_CFA_advance_loc4";
13829 case DW_CFA_offset_extended:
13830 return "DW_CFA_offset_extended";
13831 case DW_CFA_restore_extended:
13832 return "DW_CFA_restore_extended";
13833 case DW_CFA_undefined:
13834 return "DW_CFA_undefined";
13835 case DW_CFA_same_value:
13836 return "DW_CFA_same_value";
13837 case DW_CFA_register:
13838 return "DW_CFA_register";
13839 case DW_CFA_remember_state:
13840 return "DW_CFA_remember_state";
13841 case DW_CFA_restore_state:
13842 return "DW_CFA_restore_state";
13843 case DW_CFA_def_cfa:
13844 return "DW_CFA_def_cfa";
13845 case DW_CFA_def_cfa_register:
13846 return "DW_CFA_def_cfa_register";
13847 case DW_CFA_def_cfa_offset:
13848 return "DW_CFA_def_cfa_offset";
b7619582 13849 /* DWARF 3. */
985cb1a3
JM
13850 case DW_CFA_def_cfa_expression:
13851 return "DW_CFA_def_cfa_expression";
13852 case DW_CFA_expression:
13853 return "DW_CFA_expression";
13854 case DW_CFA_offset_extended_sf:
13855 return "DW_CFA_offset_extended_sf";
13856 case DW_CFA_def_cfa_sf:
13857 return "DW_CFA_def_cfa_sf";
13858 case DW_CFA_def_cfa_offset_sf:
13859 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13860 case DW_CFA_val_offset:
13861 return "DW_CFA_val_offset";
13862 case DW_CFA_val_offset_sf:
13863 return "DW_CFA_val_offset_sf";
13864 case DW_CFA_val_expression:
13865 return "DW_CFA_val_expression";
13866 /* SGI/MIPS specific. */
c906108c
SS
13867 case DW_CFA_MIPS_advance_loc8:
13868 return "DW_CFA_MIPS_advance_loc8";
b7619582 13869 /* GNU extensions. */
985cb1a3
JM
13870 case DW_CFA_GNU_window_save:
13871 return "DW_CFA_GNU_window_save";
13872 case DW_CFA_GNU_args_size:
13873 return "DW_CFA_GNU_args_size";
13874 case DW_CFA_GNU_negative_offset_extended:
13875 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13876 default:
13877 return "DW_CFA_<unknown>";
13878 }
13879}
13880#endif
13881
f9aca02d 13882static void
d97bc12b 13883dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13884{
13885 unsigned int i;
13886
d97bc12b
DE
13887 print_spaces (indent, f);
13888 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13889 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13890
13891 if (die->parent != NULL)
13892 {
13893 print_spaces (indent, f);
13894 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13895 die->parent->offset);
13896 }
13897
13898 print_spaces (indent, f);
13899 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13900 dwarf_bool_name (die->child != NULL));
c906108c 13901
d97bc12b
DE
13902 print_spaces (indent, f);
13903 fprintf_unfiltered (f, " attributes:\n");
13904
c906108c
SS
13905 for (i = 0; i < die->num_attrs; ++i)
13906 {
d97bc12b
DE
13907 print_spaces (indent, f);
13908 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13909 dwarf_attr_name (die->attrs[i].name),
13910 dwarf_form_name (die->attrs[i].form));
d97bc12b 13911
c906108c
SS
13912 switch (die->attrs[i].form)
13913 {
13914 case DW_FORM_ref_addr:
13915 case DW_FORM_addr:
d97bc12b 13916 fprintf_unfiltered (f, "address: ");
5af949e3 13917 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13918 break;
13919 case DW_FORM_block2:
13920 case DW_FORM_block4:
13921 case DW_FORM_block:
13922 case DW_FORM_block1:
3e43a32a
MS
13923 fprintf_unfiltered (f, "block: size %d",
13924 DW_BLOCK (&die->attrs[i])->size);
c906108c 13925 break;
2dc7f7b3
TT
13926 case DW_FORM_exprloc:
13927 fprintf_unfiltered (f, "expression: size %u",
13928 DW_BLOCK (&die->attrs[i])->size);
13929 break;
10b3939b
DJ
13930 case DW_FORM_ref1:
13931 case DW_FORM_ref2:
13932 case DW_FORM_ref4:
d97bc12b 13933 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13934 (long) (DW_ADDR (&die->attrs[i])));
13935 break;
c906108c
SS
13936 case DW_FORM_data1:
13937 case DW_FORM_data2:
13938 case DW_FORM_data4:
ce5d95e1 13939 case DW_FORM_data8:
c906108c
SS
13940 case DW_FORM_udata:
13941 case DW_FORM_sdata:
43bbcdc2
PH
13942 fprintf_unfiltered (f, "constant: %s",
13943 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13944 break;
2dc7f7b3
TT
13945 case DW_FORM_sec_offset:
13946 fprintf_unfiltered (f, "section offset: %s",
13947 pulongest (DW_UNSND (&die->attrs[i])));
13948 break;
55f1336d 13949 case DW_FORM_ref_sig8:
348e048f
DE
13950 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13951 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13952 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13953 else
13954 fprintf_unfiltered (f, "signatured type, offset: unknown");
13955 break;
c906108c 13956 case DW_FORM_string:
4bdf3d34 13957 case DW_FORM_strp:
8285870a 13958 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13959 DW_STRING (&die->attrs[i])
8285870a
JK
13960 ? DW_STRING (&die->attrs[i]) : "",
13961 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13962 break;
13963 case DW_FORM_flag:
13964 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13965 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13966 else
d97bc12b 13967 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13968 break;
2dc7f7b3
TT
13969 case DW_FORM_flag_present:
13970 fprintf_unfiltered (f, "flag: TRUE");
13971 break;
a8329558 13972 case DW_FORM_indirect:
0963b4bd
MS
13973 /* The reader will have reduced the indirect form to
13974 the "base form" so this form should not occur. */
3e43a32a
MS
13975 fprintf_unfiltered (f,
13976 "unexpected attribute form: DW_FORM_indirect");
a8329558 13977 break;
c906108c 13978 default:
d97bc12b 13979 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13980 die->attrs[i].form);
d97bc12b 13981 break;
c906108c 13982 }
d97bc12b 13983 fprintf_unfiltered (f, "\n");
c906108c
SS
13984 }
13985}
13986
f9aca02d 13987static void
d97bc12b 13988dump_die_for_error (struct die_info *die)
c906108c 13989{
d97bc12b
DE
13990 dump_die_shallow (gdb_stderr, 0, die);
13991}
13992
13993static void
13994dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13995{
13996 int indent = level * 4;
13997
13998 gdb_assert (die != NULL);
13999
14000 if (level >= max_level)
14001 return;
14002
14003 dump_die_shallow (f, indent, die);
14004
14005 if (die->child != NULL)
c906108c 14006 {
d97bc12b
DE
14007 print_spaces (indent, f);
14008 fprintf_unfiltered (f, " Children:");
14009 if (level + 1 < max_level)
14010 {
14011 fprintf_unfiltered (f, "\n");
14012 dump_die_1 (f, level + 1, max_level, die->child);
14013 }
14014 else
14015 {
3e43a32a
MS
14016 fprintf_unfiltered (f,
14017 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14018 }
14019 }
14020
14021 if (die->sibling != NULL && level > 0)
14022 {
14023 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14024 }
14025}
14026
d97bc12b
DE
14027/* This is called from the pdie macro in gdbinit.in.
14028 It's not static so gcc will keep a copy callable from gdb. */
14029
14030void
14031dump_die (struct die_info *die, int max_level)
14032{
14033 dump_die_1 (gdb_stdlog, 0, max_level, die);
14034}
14035
f9aca02d 14036static void
51545339 14037store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14038{
51545339 14039 void **slot;
c906108c 14040
51545339
DJ
14041 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14042
14043 *slot = die;
c906108c
SS
14044}
14045
93311388
DE
14046static int
14047is_ref_attr (struct attribute *attr)
c906108c 14048{
c906108c
SS
14049 switch (attr->form)
14050 {
14051 case DW_FORM_ref_addr:
c906108c
SS
14052 case DW_FORM_ref1:
14053 case DW_FORM_ref2:
14054 case DW_FORM_ref4:
613e1657 14055 case DW_FORM_ref8:
c906108c 14056 case DW_FORM_ref_udata:
93311388 14057 return 1;
c906108c 14058 default:
93311388 14059 return 0;
c906108c 14060 }
93311388
DE
14061}
14062
14063static unsigned int
14064dwarf2_get_ref_die_offset (struct attribute *attr)
14065{
14066 if (is_ref_attr (attr))
14067 return DW_ADDR (attr);
14068
14069 complaint (&symfile_complaints,
14070 _("unsupported die ref attribute form: '%s'"),
14071 dwarf_form_name (attr->form));
14072 return 0;
c906108c
SS
14073}
14074
43bbcdc2
PH
14075/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14076 * the value held by the attribute is not constant. */
a02abb62 14077
43bbcdc2 14078static LONGEST
a02abb62
JB
14079dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14080{
14081 if (attr->form == DW_FORM_sdata)
14082 return DW_SND (attr);
14083 else if (attr->form == DW_FORM_udata
14084 || attr->form == DW_FORM_data1
14085 || attr->form == DW_FORM_data2
14086 || attr->form == DW_FORM_data4
14087 || attr->form == DW_FORM_data8)
14088 return DW_UNSND (attr);
14089 else
14090 {
3e43a32a
MS
14091 complaint (&symfile_complaints,
14092 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14093 dwarf_form_name (attr->form));
14094 return default_value;
14095 }
14096}
14097
03dd20cc 14098/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14099 unit and add it to our queue.
14100 The result is non-zero if PER_CU was queued, otherwise the result is zero
14101 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14102
348e048f 14103static int
03dd20cc
DJ
14104maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14105 struct dwarf2_per_cu_data *per_cu)
14106{
98bfdba5
PA
14107 /* We may arrive here during partial symbol reading, if we need full
14108 DIEs to process an unusual case (e.g. template arguments). Do
14109 not queue PER_CU, just tell our caller to load its DIEs. */
14110 if (dwarf2_per_objfile->reading_partial_symbols)
14111 {
14112 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14113 return 1;
14114 return 0;
14115 }
14116
03dd20cc
DJ
14117 /* Mark the dependence relation so that we don't flush PER_CU
14118 too early. */
14119 dwarf2_add_dependence (this_cu, per_cu);
14120
14121 /* If it's already on the queue, we have nothing to do. */
14122 if (per_cu->queued)
348e048f 14123 return 0;
03dd20cc
DJ
14124
14125 /* If the compilation unit is already loaded, just mark it as
14126 used. */
14127 if (per_cu->cu != NULL)
14128 {
14129 per_cu->cu->last_used = 0;
348e048f 14130 return 0;
03dd20cc
DJ
14131 }
14132
14133 /* Add it to the queue. */
a0f42c21 14134 queue_comp_unit (per_cu);
348e048f
DE
14135
14136 return 1;
14137}
14138
14139/* Follow reference or signature attribute ATTR of SRC_DIE.
14140 On entry *REF_CU is the CU of SRC_DIE.
14141 On exit *REF_CU is the CU of the result. */
14142
14143static struct die_info *
14144follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14145 struct dwarf2_cu **ref_cu)
14146{
14147 struct die_info *die;
14148
14149 if (is_ref_attr (attr))
14150 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14151 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14152 die = follow_die_sig (src_die, attr, ref_cu);
14153 else
14154 {
14155 dump_die_for_error (src_die);
14156 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14157 (*ref_cu)->objfile->name);
14158 }
14159
14160 return die;
03dd20cc
DJ
14161}
14162
5c631832 14163/* Follow reference OFFSET.
673bfd45
DE
14164 On entry *REF_CU is the CU of the source die referencing OFFSET.
14165 On exit *REF_CU is the CU of the result.
14166 Returns NULL if OFFSET is invalid. */
f504f079 14167
f9aca02d 14168static struct die_info *
5c631832 14169follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14170{
10b3939b 14171 struct die_info temp_die;
f2f0e013 14172 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14173
348e048f
DE
14174 gdb_assert (cu->per_cu != NULL);
14175
98bfdba5
PA
14176 target_cu = cu;
14177
b0df02fd 14178 if (cu->per_cu->debug_types_section)
348e048f
DE
14179 {
14180 /* .debug_types CUs cannot reference anything outside their CU.
14181 If they need to, they have to reference a signatured type via
55f1336d 14182 DW_FORM_ref_sig8. */
348e048f 14183 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14184 return NULL;
348e048f
DE
14185 }
14186 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14187 {
14188 struct dwarf2_per_cu_data *per_cu;
9a619af0 14189
45452591 14190 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14191
14192 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14193 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14194 load_full_comp_unit (per_cu);
03dd20cc 14195
10b3939b
DJ
14196 target_cu = per_cu->cu;
14197 }
98bfdba5
PA
14198 else if (cu->dies == NULL)
14199 {
14200 /* We're loading full DIEs during partial symbol reading. */
14201 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14202 load_full_comp_unit (cu->per_cu);
98bfdba5 14203 }
c906108c 14204
f2f0e013 14205 *ref_cu = target_cu;
51545339 14206 temp_die.offset = offset;
5c631832
JK
14207 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14208}
10b3939b 14209
5c631832
JK
14210/* Follow reference attribute ATTR of SRC_DIE.
14211 On entry *REF_CU is the CU of SRC_DIE.
14212 On exit *REF_CU is the CU of the result. */
14213
14214static struct die_info *
14215follow_die_ref (struct die_info *src_die, struct attribute *attr,
14216 struct dwarf2_cu **ref_cu)
14217{
14218 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14219 struct dwarf2_cu *cu = *ref_cu;
14220 struct die_info *die;
14221
14222 die = follow_die_offset (offset, ref_cu);
14223 if (!die)
14224 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14225 "at 0x%x [in module %s]"),
14226 offset, src_die->offset, cu->objfile->name);
348e048f 14227
5c631832
JK
14228 return die;
14229}
14230
d83e736b
JK
14231/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14232 Returned value is intended for DW_OP_call*. Returned
14233 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14234
14235struct dwarf2_locexpr_baton
14236dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14237 struct dwarf2_per_cu_data *per_cu,
14238 CORE_ADDR (*get_frame_pc) (void *baton),
14239 void *baton)
5c631832 14240{
918dd910 14241 struct dwarf2_cu *cu;
5c631832
JK
14242 struct die_info *die;
14243 struct attribute *attr;
14244 struct dwarf2_locexpr_baton retval;
14245
8cf6f0b1
TT
14246 dw2_setup (per_cu->objfile);
14247
918dd910
JK
14248 if (per_cu->cu == NULL)
14249 load_cu (per_cu);
14250 cu = per_cu->cu;
14251
5c631832
JK
14252 die = follow_die_offset (offset, &cu);
14253 if (!die)
14254 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
bb5ed363 14255 offset, per_cu->objfile->name);
5c631832
JK
14256
14257 attr = dwarf2_attr (die, DW_AT_location, cu);
14258 if (!attr)
14259 {
e103e986
JK
14260 /* DWARF: "If there is no such attribute, then there is no effect.".
14261 DATA is ignored if SIZE is 0. */
5c631832 14262
e103e986 14263 retval.data = NULL;
5c631832
JK
14264 retval.size = 0;
14265 }
8cf6f0b1
TT
14266 else if (attr_form_is_section_offset (attr))
14267 {
14268 struct dwarf2_loclist_baton loclist_baton;
14269 CORE_ADDR pc = (*get_frame_pc) (baton);
14270 size_t size;
14271
14272 fill_in_loclist_baton (cu, &loclist_baton, attr);
14273
14274 retval.data = dwarf2_find_location_expression (&loclist_baton,
14275 &size, pc);
14276 retval.size = size;
14277 }
5c631832
JK
14278 else
14279 {
14280 if (!attr_form_is_block (attr))
14281 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14282 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
bb5ed363 14283 offset, per_cu->objfile->name);
5c631832
JK
14284
14285 retval.data = DW_BLOCK (attr)->data;
14286 retval.size = DW_BLOCK (attr)->size;
14287 }
14288 retval.per_cu = cu->per_cu;
918dd910 14289
918dd910
JK
14290 age_cached_comp_units ();
14291
5c631832 14292 return retval;
348e048f
DE
14293}
14294
8a9b8146
TT
14295/* Return the type of the DIE at DIE_OFFSET in the CU named by
14296 PER_CU. */
14297
14298struct type *
14299dwarf2_get_die_type (unsigned int die_offset,
14300 struct dwarf2_per_cu_data *per_cu)
14301{
8a9b8146 14302 dw2_setup (per_cu->objfile);
9ff3b74f 14303 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14304}
14305
348e048f
DE
14306/* Follow the signature attribute ATTR in SRC_DIE.
14307 On entry *REF_CU is the CU of SRC_DIE.
14308 On exit *REF_CU is the CU of the result. */
14309
14310static struct die_info *
14311follow_die_sig (struct die_info *src_die, struct attribute *attr,
14312 struct dwarf2_cu **ref_cu)
14313{
14314 struct objfile *objfile = (*ref_cu)->objfile;
14315 struct die_info temp_die;
14316 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14317 struct dwarf2_cu *sig_cu;
14318 struct die_info *die;
14319
14320 /* sig_type will be NULL if the signatured type is missing from
14321 the debug info. */
14322 if (sig_type == NULL)
14323 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14324 "at 0x%x [in module %s]"),
14325 src_die->offset, objfile->name);
14326
14327 /* If necessary, add it to the queue and load its DIEs. */
14328
14329 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14330 read_signatured_type (sig_type);
348e048f
DE
14331
14332 gdb_assert (sig_type->per_cu.cu != NULL);
14333
14334 sig_cu = sig_type->per_cu.cu;
14335 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14336 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14337 if (die)
14338 {
14339 *ref_cu = sig_cu;
14340 return die;
14341 }
14342
3e43a32a
MS
14343 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14344 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14345 sig_type->type_offset, src_die->offset, objfile->name);
14346}
14347
14348/* Given an offset of a signatured type, return its signatured_type. */
14349
14350static struct signatured_type *
8b70b953
TT
14351lookup_signatured_type_at_offset (struct objfile *objfile,
14352 struct dwarf2_section_info *section,
14353 unsigned int offset)
348e048f 14354{
8b70b953 14355 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14356 unsigned int length, initial_length_size;
14357 unsigned int sig_offset;
14358 struct signatured_type find_entry, *type_sig;
14359
14360 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14361 sig_offset = (initial_length_size
14362 + 2 /*version*/
14363 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14364 + 1 /*address_size*/);
14365 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14366 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14367
14368 /* This is only used to lookup previously recorded types.
14369 If we didn't find it, it's our bug. */
14370 gdb_assert (type_sig != NULL);
b3c8eb43 14371 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14372
14373 return type_sig;
14374}
14375
e5fe5e75 14376/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14377
14378static void
e5fe5e75 14379load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14380{
e5fe5e75
DE
14381 struct objfile *objfile = per_cu->objfile;
14382 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14383 unsigned int offset = per_cu->offset;
348e048f
DE
14384 struct signatured_type *type_sig;
14385
8b70b953 14386 dwarf2_read_section (objfile, sect);
be391dca 14387
348e048f 14388 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14389 hash table lookup. */
14390 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14391 the signature to assert we found the right one.
14392 Ok, but it's a lot of work. We should simplify things so any needed
14393 assert doesn't require all this clumsiness. */
8b70b953 14394 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14395
14396 gdb_assert (type_sig->per_cu.cu == NULL);
14397
a0f42c21 14398 read_signatured_type (type_sig);
348e048f
DE
14399
14400 gdb_assert (type_sig->per_cu.cu != NULL);
14401}
14402
14403/* Read in a signatured type and build its CU and DIEs. */
14404
14405static void
a0f42c21 14406read_signatured_type (struct signatured_type *type_sig)
348e048f 14407{
a0f42c21 14408 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14409 gdb_byte *types_ptr;
348e048f
DE
14410 struct die_reader_specs reader_specs;
14411 struct dwarf2_cu *cu;
14412 ULONGEST signature;
14413 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14414 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14415
8b70b953
TT
14416 dwarf2_read_section (objfile, section);
14417 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14418
348e048f
DE
14419 gdb_assert (type_sig->per_cu.cu == NULL);
14420
9816fde3 14421 cu = xmalloc (sizeof (*cu));
23745b47 14422 init_one_comp_unit (cu, &type_sig->per_cu);
348e048f
DE
14423
14424 /* If an error occurs while loading, release our storage. */
68dc6402 14425 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14426
9ff913ba
DE
14427 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14428 &signature, NULL);
348e048f
DE
14429 gdb_assert (signature == type_sig->signature);
14430
14431 cu->die_hash
14432 = htab_create_alloc_ex (cu->header.length / 12,
14433 die_hash,
14434 die_eq,
14435 NULL,
14436 &cu->comp_unit_obstack,
14437 hashtab_obstack_allocate,
14438 dummy_obstack_deallocate);
14439
e5fe5e75 14440 dwarf2_read_abbrevs (cu);
348e048f
DE
14441 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14442
14443 init_cu_die_reader (&reader_specs, cu);
14444
14445 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14446 NULL /*parent*/);
14447
14448 /* We try not to read any attributes in this function, because not
9cdd5dbd 14449 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14450 table processing isn't initialized. But we have to set the CU language,
14451 or we won't be able to build types correctly. */
9816fde3 14452 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14453
14454 do_cleanups (back_to);
14455
14456 /* We've successfully allocated this compilation unit. Let our caller
14457 clean it up when finished with it. */
14458 discard_cleanups (free_cu_cleanup);
14459
14460 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14461 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14462}
14463
c906108c
SS
14464/* Decode simple location descriptions.
14465 Given a pointer to a dwarf block that defines a location, compute
14466 the location and return the value.
14467
4cecd739
DJ
14468 NOTE drow/2003-11-18: This function is called in two situations
14469 now: for the address of static or global variables (partial symbols
14470 only) and for offsets into structures which are expected to be
14471 (more or less) constant. The partial symbol case should go away,
14472 and only the constant case should remain. That will let this
14473 function complain more accurately. A few special modes are allowed
14474 without complaint for global variables (for instance, global
14475 register values and thread-local values).
c906108c
SS
14476
14477 A location description containing no operations indicates that the
4cecd739 14478 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14479 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14480 callers will only want a very basic result and this can become a
21ae7a4d
JK
14481 complaint.
14482
14483 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14484
14485static CORE_ADDR
e7c27a73 14486decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14487{
e7c27a73 14488 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14489 int i;
14490 int size = blk->size;
14491 gdb_byte *data = blk->data;
14492 CORE_ADDR stack[64];
14493 int stacki;
14494 unsigned int bytes_read, unsnd;
14495 gdb_byte op;
c906108c 14496
21ae7a4d
JK
14497 i = 0;
14498 stacki = 0;
14499 stack[stacki] = 0;
14500 stack[++stacki] = 0;
14501
14502 while (i < size)
14503 {
14504 op = data[i++];
14505 switch (op)
14506 {
14507 case DW_OP_lit0:
14508 case DW_OP_lit1:
14509 case DW_OP_lit2:
14510 case DW_OP_lit3:
14511 case DW_OP_lit4:
14512 case DW_OP_lit5:
14513 case DW_OP_lit6:
14514 case DW_OP_lit7:
14515 case DW_OP_lit8:
14516 case DW_OP_lit9:
14517 case DW_OP_lit10:
14518 case DW_OP_lit11:
14519 case DW_OP_lit12:
14520 case DW_OP_lit13:
14521 case DW_OP_lit14:
14522 case DW_OP_lit15:
14523 case DW_OP_lit16:
14524 case DW_OP_lit17:
14525 case DW_OP_lit18:
14526 case DW_OP_lit19:
14527 case DW_OP_lit20:
14528 case DW_OP_lit21:
14529 case DW_OP_lit22:
14530 case DW_OP_lit23:
14531 case DW_OP_lit24:
14532 case DW_OP_lit25:
14533 case DW_OP_lit26:
14534 case DW_OP_lit27:
14535 case DW_OP_lit28:
14536 case DW_OP_lit29:
14537 case DW_OP_lit30:
14538 case DW_OP_lit31:
14539 stack[++stacki] = op - DW_OP_lit0;
14540 break;
f1bea926 14541
21ae7a4d
JK
14542 case DW_OP_reg0:
14543 case DW_OP_reg1:
14544 case DW_OP_reg2:
14545 case DW_OP_reg3:
14546 case DW_OP_reg4:
14547 case DW_OP_reg5:
14548 case DW_OP_reg6:
14549 case DW_OP_reg7:
14550 case DW_OP_reg8:
14551 case DW_OP_reg9:
14552 case DW_OP_reg10:
14553 case DW_OP_reg11:
14554 case DW_OP_reg12:
14555 case DW_OP_reg13:
14556 case DW_OP_reg14:
14557 case DW_OP_reg15:
14558 case DW_OP_reg16:
14559 case DW_OP_reg17:
14560 case DW_OP_reg18:
14561 case DW_OP_reg19:
14562 case DW_OP_reg20:
14563 case DW_OP_reg21:
14564 case DW_OP_reg22:
14565 case DW_OP_reg23:
14566 case DW_OP_reg24:
14567 case DW_OP_reg25:
14568 case DW_OP_reg26:
14569 case DW_OP_reg27:
14570 case DW_OP_reg28:
14571 case DW_OP_reg29:
14572 case DW_OP_reg30:
14573 case DW_OP_reg31:
14574 stack[++stacki] = op - DW_OP_reg0;
14575 if (i < size)
14576 dwarf2_complex_location_expr_complaint ();
14577 break;
c906108c 14578
21ae7a4d
JK
14579 case DW_OP_regx:
14580 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14581 i += bytes_read;
14582 stack[++stacki] = unsnd;
14583 if (i < size)
14584 dwarf2_complex_location_expr_complaint ();
14585 break;
c906108c 14586
21ae7a4d
JK
14587 case DW_OP_addr:
14588 stack[++stacki] = read_address (objfile->obfd, &data[i],
14589 cu, &bytes_read);
14590 i += bytes_read;
14591 break;
d53d4ac5 14592
21ae7a4d
JK
14593 case DW_OP_const1u:
14594 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14595 i += 1;
14596 break;
14597
14598 case DW_OP_const1s:
14599 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14600 i += 1;
14601 break;
14602
14603 case DW_OP_const2u:
14604 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14605 i += 2;
14606 break;
14607
14608 case DW_OP_const2s:
14609 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14610 i += 2;
14611 break;
d53d4ac5 14612
21ae7a4d
JK
14613 case DW_OP_const4u:
14614 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14615 i += 4;
14616 break;
14617
14618 case DW_OP_const4s:
14619 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14620 i += 4;
14621 break;
14622
585861ea
JK
14623 case DW_OP_const8u:
14624 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14625 i += 8;
14626 break;
14627
21ae7a4d
JK
14628 case DW_OP_constu:
14629 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14630 &bytes_read);
14631 i += bytes_read;
14632 break;
14633
14634 case DW_OP_consts:
14635 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14636 i += bytes_read;
14637 break;
14638
14639 case DW_OP_dup:
14640 stack[stacki + 1] = stack[stacki];
14641 stacki++;
14642 break;
14643
14644 case DW_OP_plus:
14645 stack[stacki - 1] += stack[stacki];
14646 stacki--;
14647 break;
14648
14649 case DW_OP_plus_uconst:
14650 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14651 &bytes_read);
14652 i += bytes_read;
14653 break;
14654
14655 case DW_OP_minus:
14656 stack[stacki - 1] -= stack[stacki];
14657 stacki--;
14658 break;
14659
14660 case DW_OP_deref:
14661 /* If we're not the last op, then we definitely can't encode
14662 this using GDB's address_class enum. This is valid for partial
14663 global symbols, although the variable's address will be bogus
14664 in the psymtab. */
14665 if (i < size)
14666 dwarf2_complex_location_expr_complaint ();
14667 break;
14668
14669 case DW_OP_GNU_push_tls_address:
14670 /* The top of the stack has the offset from the beginning
14671 of the thread control block at which the variable is located. */
14672 /* Nothing should follow this operator, so the top of stack would
14673 be returned. */
14674 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14675 address will be bogus in the psymtab. Make it always at least
14676 non-zero to not look as a variable garbage collected by linker
14677 which have DW_OP_addr 0. */
21ae7a4d
JK
14678 if (i < size)
14679 dwarf2_complex_location_expr_complaint ();
585861ea 14680 stack[stacki]++;
21ae7a4d
JK
14681 break;
14682
14683 case DW_OP_GNU_uninit:
14684 break;
14685
14686 default:
14687 {
14688 const char *name = dwarf_stack_op_name (op);
14689
14690 if (name)
14691 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14692 name);
14693 else
14694 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14695 op);
14696 }
14697
14698 return (stack[stacki]);
d53d4ac5 14699 }
3c6e0cb3 14700
21ae7a4d
JK
14701 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14702 outside of the allocated space. Also enforce minimum>0. */
14703 if (stacki >= ARRAY_SIZE (stack) - 1)
14704 {
14705 complaint (&symfile_complaints,
14706 _("location description stack overflow"));
14707 return 0;
14708 }
14709
14710 if (stacki <= 0)
14711 {
14712 complaint (&symfile_complaints,
14713 _("location description stack underflow"));
14714 return 0;
14715 }
14716 }
14717 return (stack[stacki]);
c906108c
SS
14718}
14719
14720/* memory allocation interface */
14721
c906108c 14722static struct dwarf_block *
7b5a2f43 14723dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14724{
14725 struct dwarf_block *blk;
14726
14727 blk = (struct dwarf_block *)
7b5a2f43 14728 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14729 return (blk);
14730}
14731
14732static struct abbrev_info *
f3dd6933 14733dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14734{
14735 struct abbrev_info *abbrev;
14736
f3dd6933
DJ
14737 abbrev = (struct abbrev_info *)
14738 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14739 memset (abbrev, 0, sizeof (struct abbrev_info));
14740 return (abbrev);
14741}
14742
14743static struct die_info *
b60c80d6 14744dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14745{
14746 struct die_info *die;
b60c80d6
DJ
14747 size_t size = sizeof (struct die_info);
14748
14749 if (num_attrs > 1)
14750 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14751
b60c80d6 14752 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14753 memset (die, 0, sizeof (struct die_info));
14754 return (die);
14755}
2e276125
JB
14756
14757\f
14758/* Macro support. */
14759
2e276125
JB
14760/* Return the full name of file number I in *LH's file name table.
14761 Use COMP_DIR as the name of the current directory of the
14762 compilation. The result is allocated using xmalloc; the caller is
14763 responsible for freeing it. */
14764static char *
14765file_full_name (int file, struct line_header *lh, const char *comp_dir)
14766{
6a83a1e6
EZ
14767 /* Is the file number a valid index into the line header's file name
14768 table? Remember that file numbers start with one, not zero. */
14769 if (1 <= file && file <= lh->num_file_names)
14770 {
14771 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14772
6a83a1e6
EZ
14773 if (IS_ABSOLUTE_PATH (fe->name))
14774 return xstrdup (fe->name);
14775 else
14776 {
14777 const char *dir;
14778 int dir_len;
14779 char *full_name;
14780
14781 if (fe->dir_index)
14782 dir = lh->include_dirs[fe->dir_index - 1];
14783 else
14784 dir = comp_dir;
14785
14786 if (dir)
14787 {
14788 dir_len = strlen (dir);
14789 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14790 strcpy (full_name, dir);
14791 full_name[dir_len] = '/';
14792 strcpy (full_name + dir_len + 1, fe->name);
14793 return full_name;
14794 }
14795 else
14796 return xstrdup (fe->name);
14797 }
14798 }
2e276125
JB
14799 else
14800 {
6a83a1e6
EZ
14801 /* The compiler produced a bogus file number. We can at least
14802 record the macro definitions made in the file, even if we
14803 won't be able to find the file by name. */
14804 char fake_name[80];
9a619af0 14805
6a83a1e6 14806 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14807
6e70227d 14808 complaint (&symfile_complaints,
6a83a1e6
EZ
14809 _("bad file number in macro information (%d)"),
14810 file);
2e276125 14811
6a83a1e6 14812 return xstrdup (fake_name);
2e276125
JB
14813 }
14814}
14815
14816
14817static struct macro_source_file *
14818macro_start_file (int file, int line,
14819 struct macro_source_file *current_file,
14820 const char *comp_dir,
14821 struct line_header *lh, struct objfile *objfile)
14822{
14823 /* The full name of this source file. */
14824 char *full_name = file_full_name (file, lh, comp_dir);
14825
14826 /* We don't create a macro table for this compilation unit
14827 at all until we actually get a filename. */
14828 if (! pending_macros)
4a146b47 14829 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14830 objfile->macro_cache);
2e276125
JB
14831
14832 if (! current_file)
14833 /* If we have no current file, then this must be the start_file
14834 directive for the compilation unit's main source file. */
14835 current_file = macro_set_main (pending_macros, full_name);
14836 else
14837 current_file = macro_include (current_file, line, full_name);
14838
14839 xfree (full_name);
6e70227d 14840
2e276125
JB
14841 return current_file;
14842}
14843
14844
14845/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14846 followed by a null byte. */
14847static char *
14848copy_string (const char *buf, int len)
14849{
14850 char *s = xmalloc (len + 1);
9a619af0 14851
2e276125
JB
14852 memcpy (s, buf, len);
14853 s[len] = '\0';
2e276125
JB
14854 return s;
14855}
14856
14857
14858static const char *
14859consume_improper_spaces (const char *p, const char *body)
14860{
14861 if (*p == ' ')
14862 {
4d3c2250 14863 complaint (&symfile_complaints,
3e43a32a
MS
14864 _("macro definition contains spaces "
14865 "in formal argument list:\n`%s'"),
4d3c2250 14866 body);
2e276125
JB
14867
14868 while (*p == ' ')
14869 p++;
14870 }
14871
14872 return p;
14873}
14874
14875
14876static void
14877parse_macro_definition (struct macro_source_file *file, int line,
14878 const char *body)
14879{
14880 const char *p;
14881
14882 /* The body string takes one of two forms. For object-like macro
14883 definitions, it should be:
14884
14885 <macro name> " " <definition>
14886
14887 For function-like macro definitions, it should be:
14888
14889 <macro name> "() " <definition>
14890 or
14891 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14892
14893 Spaces may appear only where explicitly indicated, and in the
14894 <definition>.
14895
14896 The Dwarf 2 spec says that an object-like macro's name is always
14897 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14898 the space when the macro's definition is the empty string.
2e276125
JB
14899
14900 The Dwarf 2 spec says that there should be no spaces between the
14901 formal arguments in a function-like macro's formal argument list,
14902 but versions of GCC around March 2002 include spaces after the
14903 commas. */
14904
14905
14906 /* Find the extent of the macro name. The macro name is terminated
14907 by either a space or null character (for an object-like macro) or
14908 an opening paren (for a function-like macro). */
14909 for (p = body; *p; p++)
14910 if (*p == ' ' || *p == '(')
14911 break;
14912
14913 if (*p == ' ' || *p == '\0')
14914 {
14915 /* It's an object-like macro. */
14916 int name_len = p - body;
14917 char *name = copy_string (body, name_len);
14918 const char *replacement;
14919
14920 if (*p == ' ')
14921 replacement = body + name_len + 1;
14922 else
14923 {
4d3c2250 14924 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14925 replacement = body + name_len;
14926 }
6e70227d 14927
2e276125
JB
14928 macro_define_object (file, line, name, replacement);
14929
14930 xfree (name);
14931 }
14932 else if (*p == '(')
14933 {
14934 /* It's a function-like macro. */
14935 char *name = copy_string (body, p - body);
14936 int argc = 0;
14937 int argv_size = 1;
14938 char **argv = xmalloc (argv_size * sizeof (*argv));
14939
14940 p++;
14941
14942 p = consume_improper_spaces (p, body);
14943
14944 /* Parse the formal argument list. */
14945 while (*p && *p != ')')
14946 {
14947 /* Find the extent of the current argument name. */
14948 const char *arg_start = p;
14949
14950 while (*p && *p != ',' && *p != ')' && *p != ' ')
14951 p++;
14952
14953 if (! *p || p == arg_start)
4d3c2250 14954 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14955 else
14956 {
14957 /* Make sure argv has room for the new argument. */
14958 if (argc >= argv_size)
14959 {
14960 argv_size *= 2;
14961 argv = xrealloc (argv, argv_size * sizeof (*argv));
14962 }
14963
14964 argv[argc++] = copy_string (arg_start, p - arg_start);
14965 }
14966
14967 p = consume_improper_spaces (p, body);
14968
14969 /* Consume the comma, if present. */
14970 if (*p == ',')
14971 {
14972 p++;
14973
14974 p = consume_improper_spaces (p, body);
14975 }
14976 }
14977
14978 if (*p == ')')
14979 {
14980 p++;
14981
14982 if (*p == ' ')
14983 /* Perfectly formed definition, no complaints. */
14984 macro_define_function (file, line, name,
6e70227d 14985 argc, (const char **) argv,
2e276125
JB
14986 p + 1);
14987 else if (*p == '\0')
14988 {
14989 /* Complain, but do define it. */
4d3c2250 14990 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14991 macro_define_function (file, line, name,
6e70227d 14992 argc, (const char **) argv,
2e276125
JB
14993 p);
14994 }
14995 else
14996 /* Just complain. */
4d3c2250 14997 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14998 }
14999 else
15000 /* Just complain. */
4d3c2250 15001 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15002
15003 xfree (name);
15004 {
15005 int i;
15006
15007 for (i = 0; i < argc; i++)
15008 xfree (argv[i]);
15009 }
15010 xfree (argv);
15011 }
15012 else
4d3c2250 15013 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15014}
15015
cf2c3c16
TT
15016/* Skip some bytes from BYTES according to the form given in FORM.
15017 Returns the new pointer. */
2e276125 15018
cf2c3c16
TT
15019static gdb_byte *
15020skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15021 enum dwarf_form form,
15022 unsigned int offset_size,
15023 struct dwarf2_section_info *section)
2e276125 15024{
cf2c3c16 15025 unsigned int bytes_read;
2e276125 15026
cf2c3c16 15027 switch (form)
2e276125 15028 {
cf2c3c16
TT
15029 case DW_FORM_data1:
15030 case DW_FORM_flag:
15031 ++bytes;
15032 break;
15033
15034 case DW_FORM_data2:
15035 bytes += 2;
15036 break;
15037
15038 case DW_FORM_data4:
15039 bytes += 4;
15040 break;
15041
15042 case DW_FORM_data8:
15043 bytes += 8;
15044 break;
15045
15046 case DW_FORM_string:
15047 read_direct_string (abfd, bytes, &bytes_read);
15048 bytes += bytes_read;
15049 break;
15050
15051 case DW_FORM_sec_offset:
15052 case DW_FORM_strp:
15053 bytes += offset_size;
15054 break;
15055
15056 case DW_FORM_block:
15057 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15058 bytes += bytes_read;
15059 break;
15060
15061 case DW_FORM_block1:
15062 bytes += 1 + read_1_byte (abfd, bytes);
15063 break;
15064 case DW_FORM_block2:
15065 bytes += 2 + read_2_bytes (abfd, bytes);
15066 break;
15067 case DW_FORM_block4:
15068 bytes += 4 + read_4_bytes (abfd, bytes);
15069 break;
15070
15071 case DW_FORM_sdata:
15072 case DW_FORM_udata:
15073 bytes = skip_leb128 (abfd, bytes);
15074 break;
15075
15076 default:
15077 {
15078 complain:
15079 complaint (&symfile_complaints,
15080 _("invalid form 0x%x in `%s'"),
15081 form,
15082 section->asection->name);
15083 return NULL;
15084 }
2e276125
JB
15085 }
15086
cf2c3c16
TT
15087 return bytes;
15088}
757a13d0 15089
cf2c3c16
TT
15090/* A helper for dwarf_decode_macros that handles skipping an unknown
15091 opcode. Returns an updated pointer to the macro data buffer; or,
15092 on error, issues a complaint and returns NULL. */
757a13d0 15093
cf2c3c16
TT
15094static gdb_byte *
15095skip_unknown_opcode (unsigned int opcode,
15096 gdb_byte **opcode_definitions,
15097 gdb_byte *mac_ptr,
15098 bfd *abfd,
15099 unsigned int offset_size,
15100 struct dwarf2_section_info *section)
15101{
15102 unsigned int bytes_read, i;
15103 unsigned long arg;
15104 gdb_byte *defn;
2e276125 15105
cf2c3c16 15106 if (opcode_definitions[opcode] == NULL)
2e276125 15107 {
cf2c3c16
TT
15108 complaint (&symfile_complaints,
15109 _("unrecognized DW_MACFINO opcode 0x%x"),
15110 opcode);
15111 return NULL;
15112 }
2e276125 15113
cf2c3c16
TT
15114 defn = opcode_definitions[opcode];
15115 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15116 defn += bytes_read;
2e276125 15117
cf2c3c16
TT
15118 for (i = 0; i < arg; ++i)
15119 {
15120 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15121 if (mac_ptr == NULL)
15122 {
15123 /* skip_form_bytes already issued the complaint. */
15124 return NULL;
15125 }
15126 }
757a13d0 15127
cf2c3c16
TT
15128 return mac_ptr;
15129}
757a13d0 15130
cf2c3c16
TT
15131/* A helper function which parses the header of a macro section.
15132 If the macro section is the extended (for now called "GNU") type,
15133 then this updates *OFFSET_SIZE. Returns a pointer to just after
15134 the header, or issues a complaint and returns NULL on error. */
757a13d0 15135
cf2c3c16
TT
15136static gdb_byte *
15137dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15138 bfd *abfd,
15139 gdb_byte *mac_ptr,
15140 unsigned int *offset_size,
15141 int section_is_gnu)
15142{
15143 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15144
cf2c3c16
TT
15145 if (section_is_gnu)
15146 {
15147 unsigned int version, flags;
757a13d0 15148
cf2c3c16
TT
15149 version = read_2_bytes (abfd, mac_ptr);
15150 if (version != 4)
15151 {
15152 complaint (&symfile_complaints,
15153 _("unrecognized version `%d' in .debug_macro section"),
15154 version);
15155 return NULL;
15156 }
15157 mac_ptr += 2;
757a13d0 15158
cf2c3c16
TT
15159 flags = read_1_byte (abfd, mac_ptr);
15160 ++mac_ptr;
15161 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15162
cf2c3c16
TT
15163 if ((flags & 2) != 0)
15164 /* We don't need the line table offset. */
15165 mac_ptr += *offset_size;
757a13d0 15166
cf2c3c16
TT
15167 /* Vendor opcode descriptions. */
15168 if ((flags & 4) != 0)
15169 {
15170 unsigned int i, count;
757a13d0 15171
cf2c3c16
TT
15172 count = read_1_byte (abfd, mac_ptr);
15173 ++mac_ptr;
15174 for (i = 0; i < count; ++i)
15175 {
15176 unsigned int opcode, bytes_read;
15177 unsigned long arg;
15178
15179 opcode = read_1_byte (abfd, mac_ptr);
15180 ++mac_ptr;
15181 opcode_definitions[opcode] = mac_ptr;
15182 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15183 mac_ptr += bytes_read;
15184 mac_ptr += arg;
15185 }
757a13d0 15186 }
cf2c3c16 15187 }
757a13d0 15188
cf2c3c16
TT
15189 return mac_ptr;
15190}
757a13d0 15191
cf2c3c16 15192/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15193 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15194
15195static void
15196dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15197 struct macro_source_file *current_file,
15198 struct line_header *lh, char *comp_dir,
15199 struct dwarf2_section_info *section,
15200 int section_is_gnu,
15201 unsigned int offset_size,
8fc3fc34
TT
15202 struct objfile *objfile,
15203 htab_t include_hash)
cf2c3c16
TT
15204{
15205 enum dwarf_macro_record_type macinfo_type;
15206 int at_commandline;
15207 gdb_byte *opcode_definitions[256];
757a13d0 15208
cf2c3c16
TT
15209 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15210 &offset_size, section_is_gnu);
15211 if (mac_ptr == NULL)
15212 {
15213 /* We already issued a complaint. */
15214 return;
15215 }
757a13d0
JK
15216
15217 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15218 GDB is still reading the definitions from command line. First
15219 DW_MACINFO_start_file will need to be ignored as it was already executed
15220 to create CURRENT_FILE for the main source holding also the command line
15221 definitions. On first met DW_MACINFO_start_file this flag is reset to
15222 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15223
15224 at_commandline = 1;
15225
15226 do
15227 {
15228 /* Do we at least have room for a macinfo type byte? */
15229 if (mac_ptr >= mac_end)
15230 {
cf2c3c16 15231 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15232 break;
15233 }
15234
15235 macinfo_type = read_1_byte (abfd, mac_ptr);
15236 mac_ptr++;
15237
cf2c3c16
TT
15238 /* Note that we rely on the fact that the corresponding GNU and
15239 DWARF constants are the same. */
757a13d0
JK
15240 switch (macinfo_type)
15241 {
15242 /* A zero macinfo type indicates the end of the macro
15243 information. */
15244 case 0:
15245 break;
2e276125 15246
cf2c3c16
TT
15247 case DW_MACRO_GNU_define:
15248 case DW_MACRO_GNU_undef:
15249 case DW_MACRO_GNU_define_indirect:
15250 case DW_MACRO_GNU_undef_indirect:
2e276125 15251 {
891d2f0b 15252 unsigned int bytes_read;
2e276125
JB
15253 int line;
15254 char *body;
cf2c3c16 15255 int is_define;
2e276125 15256
cf2c3c16
TT
15257 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15258 mac_ptr += bytes_read;
15259
15260 if (macinfo_type == DW_MACRO_GNU_define
15261 || macinfo_type == DW_MACRO_GNU_undef)
15262 {
15263 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15264 mac_ptr += bytes_read;
15265 }
15266 else
15267 {
15268 LONGEST str_offset;
15269
15270 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15271 mac_ptr += offset_size;
2e276125 15272
cf2c3c16
TT
15273 body = read_indirect_string_at_offset (abfd, str_offset);
15274 }
15275
15276 is_define = (macinfo_type == DW_MACRO_GNU_define
15277 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15278 if (! current_file)
757a13d0
JK
15279 {
15280 /* DWARF violation as no main source is present. */
15281 complaint (&symfile_complaints,
15282 _("debug info with no main source gives macro %s "
15283 "on line %d: %s"),
cf2c3c16
TT
15284 is_define ? _("definition") : _("undefinition"),
15285 line, body);
757a13d0
JK
15286 break;
15287 }
3e43a32a
MS
15288 if ((line == 0 && !at_commandline)
15289 || (line != 0 && at_commandline))
4d3c2250 15290 complaint (&symfile_complaints,
757a13d0
JK
15291 _("debug info gives %s macro %s with %s line %d: %s"),
15292 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15293 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15294 line == 0 ? _("zero") : _("non-zero"), line, body);
15295
cf2c3c16 15296 if (is_define)
757a13d0 15297 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15298 else
15299 {
15300 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15301 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15302 macro_undef (current_file, line, body);
15303 }
2e276125
JB
15304 }
15305 break;
15306
cf2c3c16 15307 case DW_MACRO_GNU_start_file:
2e276125 15308 {
891d2f0b 15309 unsigned int bytes_read;
2e276125
JB
15310 int line, file;
15311
15312 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15313 mac_ptr += bytes_read;
15314 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15315 mac_ptr += bytes_read;
15316
3e43a32a
MS
15317 if ((line == 0 && !at_commandline)
15318 || (line != 0 && at_commandline))
757a13d0
JK
15319 complaint (&symfile_complaints,
15320 _("debug info gives source %d included "
15321 "from %s at %s line %d"),
15322 file, at_commandline ? _("command-line") : _("file"),
15323 line == 0 ? _("zero") : _("non-zero"), line);
15324
15325 if (at_commandline)
15326 {
cf2c3c16
TT
15327 /* This DW_MACRO_GNU_start_file was executed in the
15328 pass one. */
757a13d0
JK
15329 at_commandline = 0;
15330 }
15331 else
15332 current_file = macro_start_file (file, line,
15333 current_file, comp_dir,
cf2c3c16 15334 lh, objfile);
2e276125
JB
15335 }
15336 break;
15337
cf2c3c16 15338 case DW_MACRO_GNU_end_file:
2e276125 15339 if (! current_file)
4d3c2250 15340 complaint (&symfile_complaints,
3e43a32a
MS
15341 _("macro debug info has an unmatched "
15342 "`close_file' directive"));
2e276125
JB
15343 else
15344 {
15345 current_file = current_file->included_by;
15346 if (! current_file)
15347 {
cf2c3c16 15348 enum dwarf_macro_record_type next_type;
2e276125
JB
15349
15350 /* GCC circa March 2002 doesn't produce the zero
15351 type byte marking the end of the compilation
15352 unit. Complain if it's not there, but exit no
15353 matter what. */
15354
15355 /* Do we at least have room for a macinfo type byte? */
15356 if (mac_ptr >= mac_end)
15357 {
cf2c3c16 15358 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15359 return;
15360 }
15361
15362 /* We don't increment mac_ptr here, so this is just
15363 a look-ahead. */
15364 next_type = read_1_byte (abfd, mac_ptr);
15365 if (next_type != 0)
4d3c2250 15366 complaint (&symfile_complaints,
3e43a32a
MS
15367 _("no terminating 0-type entry for "
15368 "macros in `.debug_macinfo' section"));
2e276125
JB
15369
15370 return;
15371 }
15372 }
15373 break;
15374
cf2c3c16
TT
15375 case DW_MACRO_GNU_transparent_include:
15376 {
15377 LONGEST offset;
8fc3fc34 15378 void **slot;
cf2c3c16
TT
15379
15380 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15381 mac_ptr += offset_size;
15382
8fc3fc34
TT
15383 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15384 if (*slot != NULL)
15385 {
15386 /* This has actually happened; see
15387 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15388 complaint (&symfile_complaints,
15389 _("recursive DW_MACRO_GNU_transparent_include in "
15390 ".debug_macro section"));
15391 }
15392 else
15393 {
15394 *slot = mac_ptr;
15395
15396 dwarf_decode_macro_bytes (abfd,
15397 section->buffer + offset,
15398 mac_end, current_file,
15399 lh, comp_dir,
15400 section, section_is_gnu,
15401 offset_size, objfile, include_hash);
15402
15403 htab_remove_elt (include_hash, mac_ptr);
15404 }
cf2c3c16
TT
15405 }
15406 break;
15407
2e276125 15408 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15409 if (!section_is_gnu)
15410 {
15411 unsigned int bytes_read;
15412 int constant;
2e276125 15413
cf2c3c16
TT
15414 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15415 mac_ptr += bytes_read;
15416 read_direct_string (abfd, mac_ptr, &bytes_read);
15417 mac_ptr += bytes_read;
2e276125 15418
cf2c3c16
TT
15419 /* We don't recognize any vendor extensions. */
15420 break;
15421 }
15422 /* FALLTHROUGH */
15423
15424 default:
15425 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15426 mac_ptr, abfd, offset_size,
15427 section);
15428 if (mac_ptr == NULL)
15429 return;
15430 break;
2e276125 15431 }
757a13d0 15432 } while (macinfo_type != 0);
2e276125 15433}
8e19ed76 15434
cf2c3c16
TT
15435static void
15436dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15437 char *comp_dir, bfd *abfd,
15438 struct dwarf2_cu *cu,
15439 struct dwarf2_section_info *section,
15440 int section_is_gnu)
15441{
bb5ed363 15442 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15443 gdb_byte *mac_ptr, *mac_end;
15444 struct macro_source_file *current_file = 0;
15445 enum dwarf_macro_record_type macinfo_type;
15446 unsigned int offset_size = cu->header.offset_size;
15447 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15448 struct cleanup *cleanup;
15449 htab_t include_hash;
15450 void **slot;
cf2c3c16 15451
bb5ed363 15452 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15453 if (section->buffer == NULL)
15454 {
15455 complaint (&symfile_complaints, _("missing %s section"),
15456 section->asection->name);
15457 return;
15458 }
15459
15460 /* First pass: Find the name of the base filename.
15461 This filename is needed in order to process all macros whose definition
15462 (or undefinition) comes from the command line. These macros are defined
15463 before the first DW_MACINFO_start_file entry, and yet still need to be
15464 associated to the base file.
15465
15466 To determine the base file name, we scan the macro definitions until we
15467 reach the first DW_MACINFO_start_file entry. We then initialize
15468 CURRENT_FILE accordingly so that any macro definition found before the
15469 first DW_MACINFO_start_file can still be associated to the base file. */
15470
15471 mac_ptr = section->buffer + offset;
15472 mac_end = section->buffer + section->size;
15473
15474 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15475 &offset_size, section_is_gnu);
15476 if (mac_ptr == NULL)
15477 {
15478 /* We already issued a complaint. */
15479 return;
15480 }
15481
15482 do
15483 {
15484 /* Do we at least have room for a macinfo type byte? */
15485 if (mac_ptr >= mac_end)
15486 {
15487 /* Complaint is printed during the second pass as GDB will probably
15488 stop the first pass earlier upon finding
15489 DW_MACINFO_start_file. */
15490 break;
15491 }
15492
15493 macinfo_type = read_1_byte (abfd, mac_ptr);
15494 mac_ptr++;
15495
15496 /* Note that we rely on the fact that the corresponding GNU and
15497 DWARF constants are the same. */
15498 switch (macinfo_type)
15499 {
15500 /* A zero macinfo type indicates the end of the macro
15501 information. */
15502 case 0:
15503 break;
15504
15505 case DW_MACRO_GNU_define:
15506 case DW_MACRO_GNU_undef:
15507 /* Only skip the data by MAC_PTR. */
15508 {
15509 unsigned int bytes_read;
15510
15511 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15512 mac_ptr += bytes_read;
15513 read_direct_string (abfd, mac_ptr, &bytes_read);
15514 mac_ptr += bytes_read;
15515 }
15516 break;
15517
15518 case DW_MACRO_GNU_start_file:
15519 {
15520 unsigned int bytes_read;
15521 int line, file;
15522
15523 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15524 mac_ptr += bytes_read;
15525 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15526 mac_ptr += bytes_read;
15527
15528 current_file = macro_start_file (file, line, current_file,
bb5ed363 15529 comp_dir, lh, objfile);
cf2c3c16
TT
15530 }
15531 break;
15532
15533 case DW_MACRO_GNU_end_file:
15534 /* No data to skip by MAC_PTR. */
15535 break;
15536
15537 case DW_MACRO_GNU_define_indirect:
15538 case DW_MACRO_GNU_undef_indirect:
15539 {
15540 unsigned int bytes_read;
15541
15542 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15543 mac_ptr += bytes_read;
15544 mac_ptr += offset_size;
15545 }
15546 break;
15547
15548 case DW_MACRO_GNU_transparent_include:
15549 /* Note that, according to the spec, a transparent include
15550 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15551 skip this opcode. */
15552 mac_ptr += offset_size;
15553 break;
15554
15555 case DW_MACINFO_vendor_ext:
15556 /* Only skip the data by MAC_PTR. */
15557 if (!section_is_gnu)
15558 {
15559 unsigned int bytes_read;
15560
15561 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15562 mac_ptr += bytes_read;
15563 read_direct_string (abfd, mac_ptr, &bytes_read);
15564 mac_ptr += bytes_read;
15565 }
15566 /* FALLTHROUGH */
15567
15568 default:
15569 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15570 mac_ptr, abfd, offset_size,
15571 section);
15572 if (mac_ptr == NULL)
15573 return;
15574 break;
15575 }
15576 } while (macinfo_type != 0 && current_file == NULL);
15577
15578 /* Second pass: Process all entries.
15579
15580 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15581 command-line macro definitions/undefinitions. This flag is unset when we
15582 reach the first DW_MACINFO_start_file entry. */
15583
8fc3fc34
TT
15584 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15585 NULL, xcalloc, xfree);
15586 cleanup = make_cleanup_htab_delete (include_hash);
15587 mac_ptr = section->buffer + offset;
15588 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15589 *slot = mac_ptr;
15590 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 15591 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
15592 offset_size, objfile, include_hash);
15593 do_cleanups (cleanup);
cf2c3c16
TT
15594}
15595
8e19ed76 15596/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15597 if so return true else false. */
8e19ed76
PS
15598static int
15599attr_form_is_block (struct attribute *attr)
15600{
15601 return (attr == NULL ? 0 :
15602 attr->form == DW_FORM_block1
15603 || attr->form == DW_FORM_block2
15604 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15605 || attr->form == DW_FORM_block
15606 || attr->form == DW_FORM_exprloc);
8e19ed76 15607}
4c2df51b 15608
c6a0999f
JB
15609/* Return non-zero if ATTR's value is a section offset --- classes
15610 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15611 You may use DW_UNSND (attr) to retrieve such offsets.
15612
15613 Section 7.5.4, "Attribute Encodings", explains that no attribute
15614 may have a value that belongs to more than one of these classes; it
15615 would be ambiguous if we did, because we use the same forms for all
15616 of them. */
3690dd37
JB
15617static int
15618attr_form_is_section_offset (struct attribute *attr)
15619{
15620 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15621 || attr->form == DW_FORM_data8
15622 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15623}
15624
15625
15626/* Return non-zero if ATTR's value falls in the 'constant' class, or
15627 zero otherwise. When this function returns true, you can apply
15628 dwarf2_get_attr_constant_value to it.
15629
15630 However, note that for some attributes you must check
15631 attr_form_is_section_offset before using this test. DW_FORM_data4
15632 and DW_FORM_data8 are members of both the constant class, and of
15633 the classes that contain offsets into other debug sections
15634 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15635 that, if an attribute's can be either a constant or one of the
15636 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15637 taken as section offsets, not constants. */
15638static int
15639attr_form_is_constant (struct attribute *attr)
15640{
15641 switch (attr->form)
15642 {
15643 case DW_FORM_sdata:
15644 case DW_FORM_udata:
15645 case DW_FORM_data1:
15646 case DW_FORM_data2:
15647 case DW_FORM_data4:
15648 case DW_FORM_data8:
15649 return 1;
15650 default:
15651 return 0;
15652 }
15653}
15654
8cf6f0b1
TT
15655/* A helper function that fills in a dwarf2_loclist_baton. */
15656
15657static void
15658fill_in_loclist_baton (struct dwarf2_cu *cu,
15659 struct dwarf2_loclist_baton *baton,
15660 struct attribute *attr)
15661{
15662 dwarf2_read_section (dwarf2_per_objfile->objfile,
15663 &dwarf2_per_objfile->loc);
15664
15665 baton->per_cu = cu->per_cu;
15666 gdb_assert (baton->per_cu);
15667 /* We don't know how long the location list is, but make sure we
15668 don't run off the edge of the section. */
15669 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15670 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15671 baton->base_address = cu->base_address;
15672}
15673
4c2df51b
DJ
15674static void
15675dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15676 struct dwarf2_cu *cu)
4c2df51b 15677{
bb5ed363
DE
15678 struct objfile *objfile = dwarf2_per_objfile->objfile;
15679
3690dd37 15680 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15681 /* ".debug_loc" may not exist at all, or the offset may be outside
15682 the section. If so, fall through to the complaint in the
15683 other branch. */
bb5ed363 15684 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15685 &dwarf2_per_objfile->loc))
4c2df51b 15686 {
0d53c4c4 15687 struct dwarf2_loclist_baton *baton;
4c2df51b 15688
bb5ed363 15689 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15690 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15691
8cf6f0b1 15692 fill_in_loclist_baton (cu, baton, attr);
be391dca 15693
d00adf39 15694 if (cu->base_known == 0)
0d53c4c4 15695 complaint (&symfile_complaints,
3e43a32a
MS
15696 _("Location list used without "
15697 "specifying the CU base address."));
4c2df51b 15698
768a979c 15699 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15700 SYMBOL_LOCATION_BATON (sym) = baton;
15701 }
15702 else
15703 {
15704 struct dwarf2_locexpr_baton *baton;
15705
bb5ed363 15706 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15707 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15708 baton->per_cu = cu->per_cu;
15709 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15710
15711 if (attr_form_is_block (attr))
15712 {
15713 /* Note that we're just copying the block's data pointer
15714 here, not the actual data. We're still pointing into the
6502dd73
DJ
15715 info_buffer for SYM's objfile; right now we never release
15716 that buffer, but when we do clean up properly this may
15717 need to change. */
0d53c4c4
DJ
15718 baton->size = DW_BLOCK (attr)->size;
15719 baton->data = DW_BLOCK (attr)->data;
15720 }
15721 else
15722 {
15723 dwarf2_invalid_attrib_class_complaint ("location description",
15724 SYMBOL_NATURAL_NAME (sym));
15725 baton->size = 0;
0d53c4c4 15726 }
6e70227d 15727
768a979c 15728 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15729 SYMBOL_LOCATION_BATON (sym) = baton;
15730 }
4c2df51b 15731}
6502dd73 15732
9aa1f1e3
TT
15733/* Return the OBJFILE associated with the compilation unit CU. If CU
15734 came from a separate debuginfo file, then the master objfile is
15735 returned. */
ae0d2f24
UW
15736
15737struct objfile *
15738dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15739{
9291a0cd 15740 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15741
15742 /* Return the master objfile, so that we can report and look up the
15743 correct file containing this variable. */
15744 if (objfile->separate_debug_objfile_backlink)
15745 objfile = objfile->separate_debug_objfile_backlink;
15746
15747 return objfile;
15748}
15749
96408a79
SA
15750/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15751 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15752 CU_HEADERP first. */
15753
15754static const struct comp_unit_head *
15755per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15756 struct dwarf2_per_cu_data *per_cu)
15757{
15758 struct objfile *objfile;
15759 struct dwarf2_per_objfile *per_objfile;
15760 gdb_byte *info_ptr;
15761
15762 if (per_cu->cu)
15763 return &per_cu->cu->header;
15764
15765 objfile = per_cu->objfile;
15766 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15767 info_ptr = per_objfile->info.buffer + per_cu->offset;
15768
15769 memset (cu_headerp, 0, sizeof (*cu_headerp));
15770 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15771
15772 return cu_headerp;
15773}
15774
ae0d2f24
UW
15775/* Return the address size given in the compilation unit header for CU. */
15776
98714339 15777int
ae0d2f24
UW
15778dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15779{
96408a79
SA
15780 struct comp_unit_head cu_header_local;
15781 const struct comp_unit_head *cu_headerp;
c471e790 15782
96408a79
SA
15783 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15784
15785 return cu_headerp->addr_size;
ae0d2f24
UW
15786}
15787
9eae7c52
TT
15788/* Return the offset size given in the compilation unit header for CU. */
15789
15790int
15791dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15792{
96408a79
SA
15793 struct comp_unit_head cu_header_local;
15794 const struct comp_unit_head *cu_headerp;
9c6c53f7 15795
96408a79
SA
15796 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15797
15798 return cu_headerp->offset_size;
15799}
15800
15801/* See its dwarf2loc.h declaration. */
15802
15803int
15804dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15805{
15806 struct comp_unit_head cu_header_local;
15807 const struct comp_unit_head *cu_headerp;
15808
15809 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15810
15811 if (cu_headerp->version == 2)
15812 return cu_headerp->addr_size;
15813 else
15814 return cu_headerp->offset_size;
181cebd4
JK
15815}
15816
9aa1f1e3
TT
15817/* Return the text offset of the CU. The returned offset comes from
15818 this CU's objfile. If this objfile came from a separate debuginfo
15819 file, then the offset may be different from the corresponding
15820 offset in the parent objfile. */
15821
15822CORE_ADDR
15823dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15824{
bb3fa9d0 15825 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15826
15827 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15828}
15829
348e048f
DE
15830/* Locate the .debug_info compilation unit from CU's objfile which contains
15831 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15832
15833static struct dwarf2_per_cu_data *
c764a876 15834dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15835 struct objfile *objfile)
15836{
15837 struct dwarf2_per_cu_data *this_cu;
15838 int low, high;
15839
ae038cb0
DJ
15840 low = 0;
15841 high = dwarf2_per_objfile->n_comp_units - 1;
15842 while (high > low)
15843 {
15844 int mid = low + (high - low) / 2;
9a619af0 15845
ae038cb0
DJ
15846 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15847 high = mid;
15848 else
15849 low = mid + 1;
15850 }
15851 gdb_assert (low == high);
15852 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15853 {
10b3939b 15854 if (low == 0)
8a3fe4f8
AC
15855 error (_("Dwarf Error: could not find partial DIE containing "
15856 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15857 (long) offset, bfd_get_filename (objfile->obfd));
15858
ae038cb0
DJ
15859 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15860 return dwarf2_per_objfile->all_comp_units[low-1];
15861 }
15862 else
15863 {
15864 this_cu = dwarf2_per_objfile->all_comp_units[low];
15865 if (low == dwarf2_per_objfile->n_comp_units - 1
15866 && offset >= this_cu->offset + this_cu->length)
c764a876 15867 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15868 gdb_assert (offset < this_cu->offset + this_cu->length);
15869 return this_cu;
15870 }
15871}
15872
23745b47 15873/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15874
9816fde3 15875static void
23745b47 15876init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15877{
9816fde3 15878 memset (cu, 0, sizeof (*cu));
23745b47
DE
15879 per_cu->cu = cu;
15880 cu->per_cu = per_cu;
15881 cu->objfile = per_cu->objfile;
93311388 15882 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15883}
15884
15885/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15886
15887static void
15888prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15889{
15890 struct attribute *attr;
15891
15892 /* Set the language we're debugging. */
15893 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15894 if (attr)
15895 set_cu_language (DW_UNSND (attr), cu);
15896 else
9cded63f
TT
15897 {
15898 cu->language = language_minimal;
15899 cu->language_defn = language_def (cu->language);
15900 }
93311388
DE
15901}
15902
ae038cb0
DJ
15903/* Release one cached compilation unit, CU. We unlink it from the tree
15904 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15905 the caller is responsible for that.
15906 NOTE: DATA is a void * because this function is also used as a
15907 cleanup routine. */
ae038cb0
DJ
15908
15909static void
68dc6402 15910free_heap_comp_unit (void *data)
ae038cb0
DJ
15911{
15912 struct dwarf2_cu *cu = data;
15913
23745b47
DE
15914 gdb_assert (cu->per_cu != NULL);
15915 cu->per_cu->cu = NULL;
ae038cb0
DJ
15916 cu->per_cu = NULL;
15917
15918 obstack_free (&cu->comp_unit_obstack, NULL);
15919
15920 xfree (cu);
15921}
15922
72bf9492 15923/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15924 when we're finished with it. We can't free the pointer itself, but be
15925 sure to unlink it from the cache. Also release any associated storage
15926 and perform cache maintenance.
72bf9492
DJ
15927
15928 Only used during partial symbol parsing. */
15929
15930static void
15931free_stack_comp_unit (void *data)
15932{
15933 struct dwarf2_cu *cu = data;
15934
23745b47
DE
15935 gdb_assert (cu->per_cu != NULL);
15936 cu->per_cu->cu = NULL;
15937 cu->per_cu = NULL;
15938
72bf9492
DJ
15939 obstack_free (&cu->comp_unit_obstack, NULL);
15940 cu->partial_dies = NULL;
ae038cb0 15941
23745b47
DE
15942 /* The previous code only did this if per_cu != NULL.
15943 But that would always succeed, so now we just unconditionally do
15944 the aging. This seems like the wrong place to do such aging,
15945 but cleaning that up is left for later. */
15946 age_cached_comp_units ();
ae038cb0
DJ
15947}
15948
15949/* Free all cached compilation units. */
15950
15951static void
15952free_cached_comp_units (void *data)
15953{
15954 struct dwarf2_per_cu_data *per_cu, **last_chain;
15955
15956 per_cu = dwarf2_per_objfile->read_in_chain;
15957 last_chain = &dwarf2_per_objfile->read_in_chain;
15958 while (per_cu != NULL)
15959 {
15960 struct dwarf2_per_cu_data *next_cu;
15961
15962 next_cu = per_cu->cu->read_in_chain;
15963
68dc6402 15964 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15965 *last_chain = next_cu;
15966
15967 per_cu = next_cu;
15968 }
15969}
15970
15971/* Increase the age counter on each cached compilation unit, and free
15972 any that are too old. */
15973
15974static void
15975age_cached_comp_units (void)
15976{
15977 struct dwarf2_per_cu_data *per_cu, **last_chain;
15978
15979 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15980 per_cu = dwarf2_per_objfile->read_in_chain;
15981 while (per_cu != NULL)
15982 {
15983 per_cu->cu->last_used ++;
15984 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15985 dwarf2_mark (per_cu->cu);
15986 per_cu = per_cu->cu->read_in_chain;
15987 }
15988
15989 per_cu = dwarf2_per_objfile->read_in_chain;
15990 last_chain = &dwarf2_per_objfile->read_in_chain;
15991 while (per_cu != NULL)
15992 {
15993 struct dwarf2_per_cu_data *next_cu;
15994
15995 next_cu = per_cu->cu->read_in_chain;
15996
15997 if (!per_cu->cu->mark)
15998 {
68dc6402 15999 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16000 *last_chain = next_cu;
16001 }
16002 else
16003 last_chain = &per_cu->cu->read_in_chain;
16004
16005 per_cu = next_cu;
16006 }
16007}
16008
16009/* Remove a single compilation unit from the cache. */
16010
16011static void
16012free_one_cached_comp_unit (void *target_cu)
16013{
16014 struct dwarf2_per_cu_data *per_cu, **last_chain;
16015
16016 per_cu = dwarf2_per_objfile->read_in_chain;
16017 last_chain = &dwarf2_per_objfile->read_in_chain;
16018 while (per_cu != NULL)
16019 {
16020 struct dwarf2_per_cu_data *next_cu;
16021
16022 next_cu = per_cu->cu->read_in_chain;
16023
16024 if (per_cu->cu == target_cu)
16025 {
68dc6402 16026 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16027 *last_chain = next_cu;
16028 break;
16029 }
16030 else
16031 last_chain = &per_cu->cu->read_in_chain;
16032
16033 per_cu = next_cu;
16034 }
16035}
16036
fe3e1990
DJ
16037/* Release all extra memory associated with OBJFILE. */
16038
16039void
16040dwarf2_free_objfile (struct objfile *objfile)
16041{
16042 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16043
16044 if (dwarf2_per_objfile == NULL)
16045 return;
16046
16047 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16048 free_cached_comp_units (NULL);
16049
7b9f3c50
DE
16050 if (dwarf2_per_objfile->quick_file_names_table)
16051 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16052
fe3e1990
DJ
16053 /* Everything else should be on the objfile obstack. */
16054}
16055
1c379e20
DJ
16056/* A pair of DIE offset and GDB type pointer. We store these
16057 in a hash table separate from the DIEs, and preserve them
16058 when the DIEs are flushed out of cache. */
16059
16060struct dwarf2_offset_and_type
16061{
16062 unsigned int offset;
16063 struct type *type;
16064};
16065
16066/* Hash function for a dwarf2_offset_and_type. */
16067
16068static hashval_t
16069offset_and_type_hash (const void *item)
16070{
16071 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16072
1c379e20
DJ
16073 return ofs->offset;
16074}
16075
16076/* Equality function for a dwarf2_offset_and_type. */
16077
16078static int
16079offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16080{
16081 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16082 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16083
1c379e20
DJ
16084 return ofs_lhs->offset == ofs_rhs->offset;
16085}
16086
16087/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16088 table if necessary. For convenience, return TYPE.
16089
16090 The DIEs reading must have careful ordering to:
16091 * Not cause infite loops trying to read in DIEs as a prerequisite for
16092 reading current DIE.
16093 * Not trying to dereference contents of still incompletely read in types
16094 while reading in other DIEs.
16095 * Enable referencing still incompletely read in types just by a pointer to
16096 the type without accessing its fields.
16097
16098 Therefore caller should follow these rules:
16099 * Try to fetch any prerequisite types we may need to build this DIE type
16100 before building the type and calling set_die_type.
e71ec853 16101 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16102 possible before fetching more types to complete the current type.
16103 * Make the type as complete as possible before fetching more types. */
1c379e20 16104
f792889a 16105static struct type *
1c379e20
DJ
16106set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16107{
16108 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16109 struct objfile *objfile = cu->objfile;
16110 htab_t *type_hash_ptr;
1c379e20 16111
b4ba55a1
JB
16112 /* For Ada types, make sure that the gnat-specific data is always
16113 initialized (if not already set). There are a few types where
16114 we should not be doing so, because the type-specific area is
16115 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16116 where the type-specific area is used to store the floatformat).
16117 But this is not a problem, because the gnat-specific information
16118 is actually not needed for these types. */
16119 if (need_gnat_info (cu)
16120 && TYPE_CODE (type) != TYPE_CODE_FUNC
16121 && TYPE_CODE (type) != TYPE_CODE_FLT
16122 && !HAVE_GNAT_AUX_INFO (type))
16123 INIT_GNAT_SPECIFIC (type);
16124
b0df02fd 16125 if (cu->per_cu->debug_types_section)
673bfd45
DE
16126 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16127 else
16128 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16129
16130 if (*type_hash_ptr == NULL)
f792889a 16131 {
673bfd45
DE
16132 *type_hash_ptr
16133 = htab_create_alloc_ex (127,
f792889a
DJ
16134 offset_and_type_hash,
16135 offset_and_type_eq,
16136 NULL,
673bfd45 16137 &objfile->objfile_obstack,
f792889a
DJ
16138 hashtab_obstack_allocate,
16139 dummy_obstack_deallocate);
f792889a 16140 }
1c379e20
DJ
16141
16142 ofs.offset = die->offset;
16143 ofs.type = type;
16144 slot = (struct dwarf2_offset_and_type **)
673bfd45 16145 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16146 if (*slot)
16147 complaint (&symfile_complaints,
16148 _("A problem internal to GDB: DIE 0x%x has type already set"),
16149 die->offset);
673bfd45 16150 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16151 **slot = ofs;
f792889a 16152 return type;
1c379e20
DJ
16153}
16154
673bfd45
DE
16155/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16156 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16157
16158static struct type *
673bfd45
DE
16159get_die_type_at_offset (unsigned int offset,
16160 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16161{
16162 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16163 htab_t type_hash;
f792889a 16164
b0df02fd 16165 if (per_cu->debug_types_section)
673bfd45
DE
16166 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16167 else
16168 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16169 if (type_hash == NULL)
16170 return NULL;
1c379e20 16171
673bfd45 16172 ofs.offset = offset;
1c379e20
DJ
16173 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16174 if (slot)
16175 return slot->type;
16176 else
16177 return NULL;
16178}
16179
673bfd45
DE
16180/* Look up the type for DIE in the appropriate type_hash table,
16181 or return NULL if DIE does not have a saved type. */
16182
16183static struct type *
16184get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16185{
16186 return get_die_type_at_offset (die->offset, cu->per_cu);
16187}
16188
10b3939b
DJ
16189/* Add a dependence relationship from CU to REF_PER_CU. */
16190
16191static void
16192dwarf2_add_dependence (struct dwarf2_cu *cu,
16193 struct dwarf2_per_cu_data *ref_per_cu)
16194{
16195 void **slot;
16196
16197 if (cu->dependencies == NULL)
16198 cu->dependencies
16199 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16200 NULL, &cu->comp_unit_obstack,
16201 hashtab_obstack_allocate,
16202 dummy_obstack_deallocate);
16203
16204 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16205 if (*slot == NULL)
16206 *slot = ref_per_cu;
16207}
1c379e20 16208
f504f079
DE
16209/* Subroutine of dwarf2_mark to pass to htab_traverse.
16210 Set the mark field in every compilation unit in the
ae038cb0
DJ
16211 cache that we must keep because we are keeping CU. */
16212
10b3939b
DJ
16213static int
16214dwarf2_mark_helper (void **slot, void *data)
16215{
16216 struct dwarf2_per_cu_data *per_cu;
16217
16218 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16219
16220 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16221 reading of the chain. As such dependencies remain valid it is not much
16222 useful to track and undo them during QUIT cleanups. */
16223 if (per_cu->cu == NULL)
16224 return 1;
16225
10b3939b
DJ
16226 if (per_cu->cu->mark)
16227 return 1;
16228 per_cu->cu->mark = 1;
16229
16230 if (per_cu->cu->dependencies != NULL)
16231 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16232
16233 return 1;
16234}
16235
f504f079
DE
16236/* Set the mark field in CU and in every other compilation unit in the
16237 cache that we must keep because we are keeping CU. */
16238
ae038cb0
DJ
16239static void
16240dwarf2_mark (struct dwarf2_cu *cu)
16241{
16242 if (cu->mark)
16243 return;
16244 cu->mark = 1;
10b3939b
DJ
16245 if (cu->dependencies != NULL)
16246 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16247}
16248
16249static void
16250dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16251{
16252 while (per_cu)
16253 {
16254 per_cu->cu->mark = 0;
16255 per_cu = per_cu->cu->read_in_chain;
16256 }
72bf9492
DJ
16257}
16258
72bf9492
DJ
16259/* Trivial hash function for partial_die_info: the hash value of a DIE
16260 is its offset in .debug_info for this objfile. */
16261
16262static hashval_t
16263partial_die_hash (const void *item)
16264{
16265 const struct partial_die_info *part_die = item;
9a619af0 16266
72bf9492
DJ
16267 return part_die->offset;
16268}
16269
16270/* Trivial comparison function for partial_die_info structures: two DIEs
16271 are equal if they have the same offset. */
16272
16273static int
16274partial_die_eq (const void *item_lhs, const void *item_rhs)
16275{
16276 const struct partial_die_info *part_die_lhs = item_lhs;
16277 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16278
72bf9492
DJ
16279 return part_die_lhs->offset == part_die_rhs->offset;
16280}
16281
ae038cb0
DJ
16282static struct cmd_list_element *set_dwarf2_cmdlist;
16283static struct cmd_list_element *show_dwarf2_cmdlist;
16284
16285static void
16286set_dwarf2_cmd (char *args, int from_tty)
16287{
16288 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16289}
16290
16291static void
16292show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16293{
ae038cb0
DJ
16294 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16295}
16296
dce234bc
PP
16297/* If section described by INFO was mmapped, munmap it now. */
16298
16299static void
16300munmap_section_buffer (struct dwarf2_section_info *info)
16301{
b315ab21 16302 if (info->map_addr != NULL)
dce234bc
PP
16303 {
16304#ifdef HAVE_MMAP
b315ab21 16305 int res;
9a619af0 16306
b315ab21
TG
16307 res = munmap (info->map_addr, info->map_len);
16308 gdb_assert (res == 0);
dce234bc
PP
16309#else
16310 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16311 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16312#endif
16313 }
16314}
16315
16316/* munmap debug sections for OBJFILE, if necessary. */
16317
16318static void
c1bd65d0 16319dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16320{
16321 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16322 int ix;
16323 struct dwarf2_section_info *section;
9a619af0 16324
16be1145
DE
16325 /* This is sorted according to the order they're defined in to make it easier
16326 to keep in sync. */
dce234bc
PP
16327 munmap_section_buffer (&data->info);
16328 munmap_section_buffer (&data->abbrev);
16329 munmap_section_buffer (&data->line);
16be1145 16330 munmap_section_buffer (&data->loc);
dce234bc 16331 munmap_section_buffer (&data->macinfo);
cf2c3c16 16332 munmap_section_buffer (&data->macro);
16be1145 16333 munmap_section_buffer (&data->str);
dce234bc 16334 munmap_section_buffer (&data->ranges);
dce234bc
PP
16335 munmap_section_buffer (&data->frame);
16336 munmap_section_buffer (&data->eh_frame);
9291a0cd 16337 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16338
16339 for (ix = 0;
16340 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16341 ++ix)
16342 munmap_section_buffer (section);
16343
16344 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16345}
16346
16347\f
ae2de4f8 16348/* The "save gdb-index" command. */
9291a0cd
TT
16349
16350/* The contents of the hash table we create when building the string
16351 table. */
16352struct strtab_entry
16353{
16354 offset_type offset;
16355 const char *str;
16356};
16357
559a7a62
JK
16358/* Hash function for a strtab_entry.
16359
16360 Function is used only during write_hash_table so no index format backward
16361 compatibility is needed. */
b89be57b 16362
9291a0cd
TT
16363static hashval_t
16364hash_strtab_entry (const void *e)
16365{
16366 const struct strtab_entry *entry = e;
559a7a62 16367 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16368}
16369
16370/* Equality function for a strtab_entry. */
b89be57b 16371
9291a0cd
TT
16372static int
16373eq_strtab_entry (const void *a, const void *b)
16374{
16375 const struct strtab_entry *ea = a;
16376 const struct strtab_entry *eb = b;
16377 return !strcmp (ea->str, eb->str);
16378}
16379
16380/* Create a strtab_entry hash table. */
b89be57b 16381
9291a0cd
TT
16382static htab_t
16383create_strtab (void)
16384{
16385 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16386 xfree, xcalloc, xfree);
16387}
16388
16389/* Add a string to the constant pool. Return the string's offset in
16390 host order. */
b89be57b 16391
9291a0cd
TT
16392static offset_type
16393add_string (htab_t table, struct obstack *cpool, const char *str)
16394{
16395 void **slot;
16396 struct strtab_entry entry;
16397 struct strtab_entry *result;
16398
16399 entry.str = str;
16400 slot = htab_find_slot (table, &entry, INSERT);
16401 if (*slot)
16402 result = *slot;
16403 else
16404 {
16405 result = XNEW (struct strtab_entry);
16406 result->offset = obstack_object_size (cpool);
16407 result->str = str;
16408 obstack_grow_str0 (cpool, str);
16409 *slot = result;
16410 }
16411 return result->offset;
16412}
16413
16414/* An entry in the symbol table. */
16415struct symtab_index_entry
16416{
16417 /* The name of the symbol. */
16418 const char *name;
16419 /* The offset of the name in the constant pool. */
16420 offset_type index_offset;
16421 /* A sorted vector of the indices of all the CUs that hold an object
16422 of this name. */
16423 VEC (offset_type) *cu_indices;
16424};
16425
16426/* The symbol table. This is a power-of-2-sized hash table. */
16427struct mapped_symtab
16428{
16429 offset_type n_elements;
16430 offset_type size;
16431 struct symtab_index_entry **data;
16432};
16433
16434/* Hash function for a symtab_index_entry. */
b89be57b 16435
9291a0cd
TT
16436static hashval_t
16437hash_symtab_entry (const void *e)
16438{
16439 const struct symtab_index_entry *entry = e;
16440 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16441 sizeof (offset_type) * VEC_length (offset_type,
16442 entry->cu_indices),
16443 0);
16444}
16445
16446/* Equality function for a symtab_index_entry. */
b89be57b 16447
9291a0cd
TT
16448static int
16449eq_symtab_entry (const void *a, const void *b)
16450{
16451 const struct symtab_index_entry *ea = a;
16452 const struct symtab_index_entry *eb = b;
16453 int len = VEC_length (offset_type, ea->cu_indices);
16454 if (len != VEC_length (offset_type, eb->cu_indices))
16455 return 0;
16456 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16457 VEC_address (offset_type, eb->cu_indices),
16458 sizeof (offset_type) * len);
16459}
16460
16461/* Destroy a symtab_index_entry. */
b89be57b 16462
9291a0cd
TT
16463static void
16464delete_symtab_entry (void *p)
16465{
16466 struct symtab_index_entry *entry = p;
16467 VEC_free (offset_type, entry->cu_indices);
16468 xfree (entry);
16469}
16470
16471/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16472
9291a0cd 16473static htab_t
3876f04e 16474create_symbol_hash_table (void)
9291a0cd
TT
16475{
16476 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16477 delete_symtab_entry, xcalloc, xfree);
16478}
16479
16480/* Create a new mapped symtab object. */
b89be57b 16481
9291a0cd
TT
16482static struct mapped_symtab *
16483create_mapped_symtab (void)
16484{
16485 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16486 symtab->n_elements = 0;
16487 symtab->size = 1024;
16488 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16489 return symtab;
16490}
16491
16492/* Destroy a mapped_symtab. */
b89be57b 16493
9291a0cd
TT
16494static void
16495cleanup_mapped_symtab (void *p)
16496{
16497 struct mapped_symtab *symtab = p;
16498 /* The contents of the array are freed when the other hash table is
16499 destroyed. */
16500 xfree (symtab->data);
16501 xfree (symtab);
16502}
16503
16504/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16505 the slot.
16506
16507 Function is used only during write_hash_table so no index format backward
16508 compatibility is needed. */
b89be57b 16509
9291a0cd
TT
16510static struct symtab_index_entry **
16511find_slot (struct mapped_symtab *symtab, const char *name)
16512{
559a7a62 16513 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16514
16515 index = hash & (symtab->size - 1);
16516 step = ((hash * 17) & (symtab->size - 1)) | 1;
16517
16518 for (;;)
16519 {
16520 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16521 return &symtab->data[index];
16522 index = (index + step) & (symtab->size - 1);
16523 }
16524}
16525
16526/* Expand SYMTAB's hash table. */
b89be57b 16527
9291a0cd
TT
16528static void
16529hash_expand (struct mapped_symtab *symtab)
16530{
16531 offset_type old_size = symtab->size;
16532 offset_type i;
16533 struct symtab_index_entry **old_entries = symtab->data;
16534
16535 symtab->size *= 2;
16536 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16537
16538 for (i = 0; i < old_size; ++i)
16539 {
16540 if (old_entries[i])
16541 {
16542 struct symtab_index_entry **slot = find_slot (symtab,
16543 old_entries[i]->name);
16544 *slot = old_entries[i];
16545 }
16546 }
16547
16548 xfree (old_entries);
16549}
16550
16551/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16552 is the index of the CU in which the symbol appears. */
b89be57b 16553
9291a0cd
TT
16554static void
16555add_index_entry (struct mapped_symtab *symtab, const char *name,
16556 offset_type cu_index)
16557{
16558 struct symtab_index_entry **slot;
16559
16560 ++symtab->n_elements;
16561 if (4 * symtab->n_elements / 3 >= symtab->size)
16562 hash_expand (symtab);
16563
16564 slot = find_slot (symtab, name);
16565 if (!*slot)
16566 {
16567 *slot = XNEW (struct symtab_index_entry);
16568 (*slot)->name = name;
16569 (*slot)->cu_indices = NULL;
16570 }
16571 /* Don't push an index twice. Due to how we add entries we only
16572 have to check the last one. */
16573 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16574 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16575 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16576}
16577
16578/* Add a vector of indices to the constant pool. */
b89be57b 16579
9291a0cd 16580static offset_type
3876f04e 16581add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16582 struct symtab_index_entry *entry)
16583{
16584 void **slot;
16585
3876f04e 16586 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16587 if (!*slot)
16588 {
16589 offset_type len = VEC_length (offset_type, entry->cu_indices);
16590 offset_type val = MAYBE_SWAP (len);
16591 offset_type iter;
16592 int i;
16593
16594 *slot = entry;
16595 entry->index_offset = obstack_object_size (cpool);
16596
16597 obstack_grow (cpool, &val, sizeof (val));
16598 for (i = 0;
16599 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16600 ++i)
16601 {
16602 val = MAYBE_SWAP (iter);
16603 obstack_grow (cpool, &val, sizeof (val));
16604 }
16605 }
16606 else
16607 {
16608 struct symtab_index_entry *old_entry = *slot;
16609 entry->index_offset = old_entry->index_offset;
16610 entry = old_entry;
16611 }
16612 return entry->index_offset;
16613}
16614
16615/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16616 constant pool entries going into the obstack CPOOL. */
b89be57b 16617
9291a0cd
TT
16618static void
16619write_hash_table (struct mapped_symtab *symtab,
16620 struct obstack *output, struct obstack *cpool)
16621{
16622 offset_type i;
3876f04e 16623 htab_t symbol_hash_table;
9291a0cd
TT
16624 htab_t str_table;
16625
3876f04e 16626 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16627 str_table = create_strtab ();
3876f04e 16628
9291a0cd
TT
16629 /* We add all the index vectors to the constant pool first, to
16630 ensure alignment is ok. */
16631 for (i = 0; i < symtab->size; ++i)
16632 {
16633 if (symtab->data[i])
3876f04e 16634 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16635 }
16636
16637 /* Now write out the hash table. */
16638 for (i = 0; i < symtab->size; ++i)
16639 {
16640 offset_type str_off, vec_off;
16641
16642 if (symtab->data[i])
16643 {
16644 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16645 vec_off = symtab->data[i]->index_offset;
16646 }
16647 else
16648 {
16649 /* While 0 is a valid constant pool index, it is not valid
16650 to have 0 for both offsets. */
16651 str_off = 0;
16652 vec_off = 0;
16653 }
16654
16655 str_off = MAYBE_SWAP (str_off);
16656 vec_off = MAYBE_SWAP (vec_off);
16657
16658 obstack_grow (output, &str_off, sizeof (str_off));
16659 obstack_grow (output, &vec_off, sizeof (vec_off));
16660 }
16661
16662 htab_delete (str_table);
3876f04e 16663 htab_delete (symbol_hash_table);
9291a0cd
TT
16664}
16665
0a5429f6
DE
16666/* Struct to map psymtab to CU index in the index file. */
16667struct psymtab_cu_index_map
16668{
16669 struct partial_symtab *psymtab;
16670 unsigned int cu_index;
16671};
16672
16673static hashval_t
16674hash_psymtab_cu_index (const void *item)
16675{
16676 const struct psymtab_cu_index_map *map = item;
16677
16678 return htab_hash_pointer (map->psymtab);
16679}
16680
16681static int
16682eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16683{
16684 const struct psymtab_cu_index_map *lhs = item_lhs;
16685 const struct psymtab_cu_index_map *rhs = item_rhs;
16686
16687 return lhs->psymtab == rhs->psymtab;
16688}
16689
16690/* Helper struct for building the address table. */
16691struct addrmap_index_data
16692{
16693 struct objfile *objfile;
16694 struct obstack *addr_obstack;
16695 htab_t cu_index_htab;
16696
16697 /* Non-zero if the previous_* fields are valid.
16698 We can't write an entry until we see the next entry (since it is only then
16699 that we know the end of the entry). */
16700 int previous_valid;
16701 /* Index of the CU in the table of all CUs in the index file. */
16702 unsigned int previous_cu_index;
0963b4bd 16703 /* Start address of the CU. */
0a5429f6
DE
16704 CORE_ADDR previous_cu_start;
16705};
16706
16707/* Write an address entry to OBSTACK. */
b89be57b 16708
9291a0cd 16709static void
0a5429f6
DE
16710add_address_entry (struct objfile *objfile, struct obstack *obstack,
16711 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16712{
0a5429f6 16713 offset_type cu_index_to_write;
9291a0cd
TT
16714 char addr[8];
16715 CORE_ADDR baseaddr;
16716
16717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16718
0a5429f6
DE
16719 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16720 obstack_grow (obstack, addr, 8);
16721 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16722 obstack_grow (obstack, addr, 8);
16723 cu_index_to_write = MAYBE_SWAP (cu_index);
16724 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16725}
16726
16727/* Worker function for traversing an addrmap to build the address table. */
16728
16729static int
16730add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16731{
16732 struct addrmap_index_data *data = datap;
16733 struct partial_symtab *pst = obj;
16734 offset_type cu_index;
16735 void **slot;
16736
16737 if (data->previous_valid)
16738 add_address_entry (data->objfile, data->addr_obstack,
16739 data->previous_cu_start, start_addr,
16740 data->previous_cu_index);
16741
16742 data->previous_cu_start = start_addr;
16743 if (pst != NULL)
16744 {
16745 struct psymtab_cu_index_map find_map, *map;
16746 find_map.psymtab = pst;
16747 map = htab_find (data->cu_index_htab, &find_map);
16748 gdb_assert (map != NULL);
16749 data->previous_cu_index = map->cu_index;
16750 data->previous_valid = 1;
16751 }
16752 else
16753 data->previous_valid = 0;
16754
16755 return 0;
16756}
16757
16758/* Write OBJFILE's address map to OBSTACK.
16759 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16760 in the index file. */
16761
16762static void
16763write_address_map (struct objfile *objfile, struct obstack *obstack,
16764 htab_t cu_index_htab)
16765{
16766 struct addrmap_index_data addrmap_index_data;
16767
16768 /* When writing the address table, we have to cope with the fact that
16769 the addrmap iterator only provides the start of a region; we have to
16770 wait until the next invocation to get the start of the next region. */
16771
16772 addrmap_index_data.objfile = objfile;
16773 addrmap_index_data.addr_obstack = obstack;
16774 addrmap_index_data.cu_index_htab = cu_index_htab;
16775 addrmap_index_data.previous_valid = 0;
16776
16777 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16778 &addrmap_index_data);
16779
16780 /* It's highly unlikely the last entry (end address = 0xff...ff)
16781 is valid, but we should still handle it.
16782 The end address is recorded as the start of the next region, but that
16783 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16784 anyway. */
16785 if (addrmap_index_data.previous_valid)
16786 add_address_entry (objfile, obstack,
16787 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16788 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16789}
16790
16791/* Add a list of partial symbols to SYMTAB. */
b89be57b 16792
9291a0cd
TT
16793static void
16794write_psymbols (struct mapped_symtab *symtab,
987d643c 16795 htab_t psyms_seen,
9291a0cd
TT
16796 struct partial_symbol **psymp,
16797 int count,
987d643c
TT
16798 offset_type cu_index,
16799 int is_static)
9291a0cd
TT
16800{
16801 for (; count-- > 0; ++psymp)
16802 {
987d643c
TT
16803 void **slot, *lookup;
16804
9291a0cd
TT
16805 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16806 error (_("Ada is not currently supported by the index"));
987d643c
TT
16807
16808 /* We only want to add a given psymbol once. However, we also
16809 want to account for whether it is global or static. So, we
16810 may add it twice, using slightly different values. */
16811 if (is_static)
16812 {
16813 uintptr_t val = 1 | (uintptr_t) *psymp;
16814
16815 lookup = (void *) val;
16816 }
16817 else
16818 lookup = *psymp;
16819
16820 /* Only add a given psymbol once. */
16821 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16822 if (!*slot)
16823 {
16824 *slot = lookup;
bb2f58dc 16825 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16826 }
9291a0cd
TT
16827 }
16828}
16829
16830/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16831 exception if there is an error. */
b89be57b 16832
9291a0cd
TT
16833static void
16834write_obstack (FILE *file, struct obstack *obstack)
16835{
16836 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16837 file)
16838 != obstack_object_size (obstack))
16839 error (_("couldn't data write to file"));
16840}
16841
16842/* Unlink a file if the argument is not NULL. */
b89be57b 16843
9291a0cd
TT
16844static void
16845unlink_if_set (void *p)
16846{
16847 char **filename = p;
16848 if (*filename)
16849 unlink (*filename);
16850}
16851
1fd400ff
TT
16852/* A helper struct used when iterating over debug_types. */
16853struct signatured_type_index_data
16854{
16855 struct objfile *objfile;
16856 struct mapped_symtab *symtab;
16857 struct obstack *types_list;
987d643c 16858 htab_t psyms_seen;
1fd400ff
TT
16859 int cu_index;
16860};
16861
16862/* A helper function that writes a single signatured_type to an
16863 obstack. */
b89be57b 16864
1fd400ff
TT
16865static int
16866write_one_signatured_type (void **slot, void *d)
16867{
16868 struct signatured_type_index_data *info = d;
16869 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16870 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16871 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16872 gdb_byte val[8];
16873
16874 write_psymbols (info->symtab,
987d643c 16875 info->psyms_seen,
3e43a32a
MS
16876 info->objfile->global_psymbols.list
16877 + psymtab->globals_offset,
987d643c
TT
16878 psymtab->n_global_syms, info->cu_index,
16879 0);
1fd400ff 16880 write_psymbols (info->symtab,
987d643c 16881 info->psyms_seen,
3e43a32a
MS
16882 info->objfile->static_psymbols.list
16883 + psymtab->statics_offset,
987d643c
TT
16884 psymtab->n_static_syms, info->cu_index,
16885 1);
1fd400ff 16886
b3c8eb43 16887 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16888 obstack_grow (info->types_list, val, 8);
16889 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16890 obstack_grow (info->types_list, val, 8);
16891 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16892 obstack_grow (info->types_list, val, 8);
16893
16894 ++info->cu_index;
16895
16896 return 1;
16897}
16898
9291a0cd 16899/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16900
9291a0cd
TT
16901static void
16902write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16903{
16904 struct cleanup *cleanup;
16905 char *filename, *cleanup_filename;
1fd400ff
TT
16906 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16907 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16908 int i;
16909 FILE *out_file;
16910 struct mapped_symtab *symtab;
16911 offset_type val, size_of_contents, total_len;
16912 struct stat st;
16913 char buf[8];
987d643c 16914 htab_t psyms_seen;
0a5429f6
DE
16915 htab_t cu_index_htab;
16916 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16917
b4f2f049 16918 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16919 return;
b4f2f049 16920
9291a0cd
TT
16921 if (dwarf2_per_objfile->using_index)
16922 error (_("Cannot use an index to create the index"));
16923
8b70b953
TT
16924 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16925 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16926
9291a0cd 16927 if (stat (objfile->name, &st) < 0)
7e17e088 16928 perror_with_name (objfile->name);
9291a0cd
TT
16929
16930 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16931 INDEX_SUFFIX, (char *) NULL);
16932 cleanup = make_cleanup (xfree, filename);
16933
16934 out_file = fopen (filename, "wb");
16935 if (!out_file)
16936 error (_("Can't open `%s' for writing"), filename);
16937
16938 cleanup_filename = filename;
16939 make_cleanup (unlink_if_set, &cleanup_filename);
16940
16941 symtab = create_mapped_symtab ();
16942 make_cleanup (cleanup_mapped_symtab, symtab);
16943
16944 obstack_init (&addr_obstack);
16945 make_cleanup_obstack_free (&addr_obstack);
16946
16947 obstack_init (&cu_list);
16948 make_cleanup_obstack_free (&cu_list);
16949
1fd400ff
TT
16950 obstack_init (&types_cu_list);
16951 make_cleanup_obstack_free (&types_cu_list);
16952
987d643c
TT
16953 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16954 NULL, xcalloc, xfree);
96408a79 16955 make_cleanup_htab_delete (psyms_seen);
987d643c 16956
0a5429f6
DE
16957 /* While we're scanning CU's create a table that maps a psymtab pointer
16958 (which is what addrmap records) to its index (which is what is recorded
16959 in the index file). This will later be needed to write the address
16960 table. */
16961 cu_index_htab = htab_create_alloc (100,
16962 hash_psymtab_cu_index,
16963 eq_psymtab_cu_index,
16964 NULL, xcalloc, xfree);
96408a79 16965 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16966 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16967 xmalloc (sizeof (struct psymtab_cu_index_map)
16968 * dwarf2_per_objfile->n_comp_units);
16969 make_cleanup (xfree, psymtab_cu_index_map);
16970
16971 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16972 work here. Also, the debug_types entries do not appear in
16973 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16974 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16975 {
3e43a32a
MS
16976 struct dwarf2_per_cu_data *per_cu
16977 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16978 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16979 gdb_byte val[8];
0a5429f6
DE
16980 struct psymtab_cu_index_map *map;
16981 void **slot;
9291a0cd
TT
16982
16983 write_psymbols (symtab,
987d643c 16984 psyms_seen,
9291a0cd 16985 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16986 psymtab->n_global_syms, i,
16987 0);
9291a0cd 16988 write_psymbols (symtab,
987d643c 16989 psyms_seen,
9291a0cd 16990 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16991 psymtab->n_static_syms, i,
16992 1);
9291a0cd 16993
0a5429f6
DE
16994 map = &psymtab_cu_index_map[i];
16995 map->psymtab = psymtab;
16996 map->cu_index = i;
16997 slot = htab_find_slot (cu_index_htab, map, INSERT);
16998 gdb_assert (slot != NULL);
16999 gdb_assert (*slot == NULL);
17000 *slot = map;
9291a0cd 17001
e254ef6a 17002 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 17003 obstack_grow (&cu_list, val, 8);
e254ef6a 17004 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17005 obstack_grow (&cu_list, val, 8);
17006 }
17007
0a5429f6
DE
17008 /* Dump the address map. */
17009 write_address_map (objfile, &addr_obstack, cu_index_htab);
17010
1fd400ff
TT
17011 /* Write out the .debug_type entries, if any. */
17012 if (dwarf2_per_objfile->signatured_types)
17013 {
17014 struct signatured_type_index_data sig_data;
17015
17016 sig_data.objfile = objfile;
17017 sig_data.symtab = symtab;
17018 sig_data.types_list = &types_cu_list;
987d643c 17019 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17020 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17021 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17022 write_one_signatured_type, &sig_data);
17023 }
17024
9291a0cd
TT
17025 obstack_init (&constant_pool);
17026 make_cleanup_obstack_free (&constant_pool);
17027 obstack_init (&symtab_obstack);
17028 make_cleanup_obstack_free (&symtab_obstack);
17029 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17030
17031 obstack_init (&contents);
17032 make_cleanup_obstack_free (&contents);
1fd400ff 17033 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17034 total_len = size_of_contents;
17035
17036 /* The version number. */
559a7a62 17037 val = MAYBE_SWAP (5);
9291a0cd
TT
17038 obstack_grow (&contents, &val, sizeof (val));
17039
17040 /* The offset of the CU list from the start of the file. */
17041 val = MAYBE_SWAP (total_len);
17042 obstack_grow (&contents, &val, sizeof (val));
17043 total_len += obstack_object_size (&cu_list);
17044
1fd400ff
TT
17045 /* The offset of the types CU list from the start of the file. */
17046 val = MAYBE_SWAP (total_len);
17047 obstack_grow (&contents, &val, sizeof (val));
17048 total_len += obstack_object_size (&types_cu_list);
17049
9291a0cd
TT
17050 /* The offset of the address table from the start of the file. */
17051 val = MAYBE_SWAP (total_len);
17052 obstack_grow (&contents, &val, sizeof (val));
17053 total_len += obstack_object_size (&addr_obstack);
17054
17055 /* The offset of the symbol table from the start of the file. */
17056 val = MAYBE_SWAP (total_len);
17057 obstack_grow (&contents, &val, sizeof (val));
17058 total_len += obstack_object_size (&symtab_obstack);
17059
17060 /* The offset of the constant pool from the start of the file. */
17061 val = MAYBE_SWAP (total_len);
17062 obstack_grow (&contents, &val, sizeof (val));
17063 total_len += obstack_object_size (&constant_pool);
17064
17065 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17066
17067 write_obstack (out_file, &contents);
17068 write_obstack (out_file, &cu_list);
1fd400ff 17069 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17070 write_obstack (out_file, &addr_obstack);
17071 write_obstack (out_file, &symtab_obstack);
17072 write_obstack (out_file, &constant_pool);
17073
17074 fclose (out_file);
17075
17076 /* We want to keep the file, so we set cleanup_filename to NULL
17077 here. See unlink_if_set. */
17078 cleanup_filename = NULL;
17079
17080 do_cleanups (cleanup);
17081}
17082
90476074
TT
17083/* Implementation of the `save gdb-index' command.
17084
17085 Note that the file format used by this command is documented in the
17086 GDB manual. Any changes here must be documented there. */
11570e71 17087
9291a0cd
TT
17088static void
17089save_gdb_index_command (char *arg, int from_tty)
17090{
17091 struct objfile *objfile;
17092
17093 if (!arg || !*arg)
96d19272 17094 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17095
17096 ALL_OBJFILES (objfile)
17097 {
17098 struct stat st;
17099
17100 /* If the objfile does not correspond to an actual file, skip it. */
17101 if (stat (objfile->name, &st) < 0)
17102 continue;
17103
17104 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17105 if (dwarf2_per_objfile)
17106 {
17107 volatile struct gdb_exception except;
17108
17109 TRY_CATCH (except, RETURN_MASK_ERROR)
17110 {
17111 write_psymtabs_to_index (objfile, arg);
17112 }
17113 if (except.reason < 0)
17114 exception_fprintf (gdb_stderr, except,
17115 _("Error while writing index for `%s': "),
17116 objfile->name);
17117 }
17118 }
dce234bc
PP
17119}
17120
9291a0cd
TT
17121\f
17122
9eae7c52
TT
17123int dwarf2_always_disassemble;
17124
17125static void
17126show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17127 struct cmd_list_element *c, const char *value)
17128{
3e43a32a
MS
17129 fprintf_filtered (file,
17130 _("Whether to always disassemble "
17131 "DWARF expressions is %s.\n"),
9eae7c52
TT
17132 value);
17133}
17134
900e11f9
JK
17135static void
17136show_check_physname (struct ui_file *file, int from_tty,
17137 struct cmd_list_element *c, const char *value)
17138{
17139 fprintf_filtered (file,
17140 _("Whether to check \"physname\" is %s.\n"),
17141 value);
17142}
17143
6502dd73
DJ
17144void _initialize_dwarf2_read (void);
17145
17146void
17147_initialize_dwarf2_read (void)
17148{
96d19272
JK
17149 struct cmd_list_element *c;
17150
dce234bc 17151 dwarf2_objfile_data_key
c1bd65d0 17152 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17153
1bedd215
AC
17154 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17155Set DWARF 2 specific variables.\n\
17156Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17157 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17158 0/*allow-unknown*/, &maintenance_set_cmdlist);
17159
1bedd215
AC
17160 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17161Show DWARF 2 specific variables\n\
17162Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17163 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17164 0/*allow-unknown*/, &maintenance_show_cmdlist);
17165
17166 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17167 &dwarf2_max_cache_age, _("\
17168Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17169Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17170A higher limit means that cached compilation units will be stored\n\
17171in memory longer, and more total memory will be used. Zero disables\n\
17172caching, which can slow down startup."),
2c5b56ce 17173 NULL,
920d2a44 17174 show_dwarf2_max_cache_age,
2c5b56ce 17175 &set_dwarf2_cmdlist,
ae038cb0 17176 &show_dwarf2_cmdlist);
d97bc12b 17177
9eae7c52
TT
17178 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17179 &dwarf2_always_disassemble, _("\
17180Set whether `info address' always disassembles DWARF expressions."), _("\
17181Show whether `info address' always disassembles DWARF expressions."), _("\
17182When enabled, DWARF expressions are always printed in an assembly-like\n\
17183syntax. When disabled, expressions will be printed in a more\n\
17184conversational style, when possible."),
17185 NULL,
17186 show_dwarf2_always_disassemble,
17187 &set_dwarf2_cmdlist,
17188 &show_dwarf2_cmdlist);
17189
d97bc12b
DE
17190 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17191Set debugging of the dwarf2 DIE reader."), _("\
17192Show debugging of the dwarf2 DIE reader."), _("\
17193When enabled (non-zero), DIEs are dumped after they are read in.\n\
17194The value is the maximum depth to print."),
17195 NULL,
17196 NULL,
17197 &setdebuglist, &showdebuglist);
9291a0cd 17198
900e11f9
JK
17199 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17200Set cross-checking of \"physname\" code against demangler."), _("\
17201Show cross-checking of \"physname\" code against demangler."), _("\
17202When enabled, GDB's internal \"physname\" code is checked against\n\
17203the demangler."),
17204 NULL, show_check_physname,
17205 &setdebuglist, &showdebuglist);
17206
96d19272 17207 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17208 _("\
fc1a9d6e 17209Save a gdb-index file.\n\
11570e71 17210Usage: save gdb-index DIRECTORY"),
96d19272
JK
17211 &save_cmdlist);
17212 set_cmd_completer (c, filename_completer);
6502dd73 17213}
This page took 2.418411 seconds and 4 git commands to generate.