* parser-defs.h (namecopy): Delete.
[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
72b9f47f 989static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 990 struct dwarf2_cu *, struct partial_symtab *);
c906108c 991
72b9f47f 992static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 993
a14ed312 994static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 995 struct dwarf2_cu *);
c906108c 996
34eaf542
TT
997static struct symbol *new_symbol_full (struct die_info *, struct type *,
998 struct dwarf2_cu *, struct symbol *);
999
a14ed312 1000static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1001 struct dwarf2_cu *);
c906108c 1002
98bfdba5
PA
1003static void dwarf2_const_value_attr (struct attribute *attr,
1004 struct type *type,
1005 const char *name,
1006 struct obstack *obstack,
1007 struct dwarf2_cu *cu, long *value,
1008 gdb_byte **bytes,
1009 struct dwarf2_locexpr_baton **baton);
2df3850c 1010
e7c27a73 1011static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1012
b4ba55a1
JB
1013static int need_gnat_info (struct dwarf2_cu *);
1014
3e43a32a
MS
1015static struct type *die_descriptive_type (struct die_info *,
1016 struct dwarf2_cu *);
b4ba55a1
JB
1017
1018static void set_descriptive_type (struct type *, struct die_info *,
1019 struct dwarf2_cu *);
1020
e7c27a73
DJ
1021static struct type *die_containing_type (struct die_info *,
1022 struct dwarf2_cu *);
c906108c 1023
673bfd45
DE
1024static struct type *lookup_die_type (struct die_info *, struct attribute *,
1025 struct dwarf2_cu *);
c906108c 1026
f792889a 1027static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1028
673bfd45
DE
1029static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1030
086ed43d 1031static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1032
6e70227d 1033static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1034 const char *suffix, int physname,
1035 struct dwarf2_cu *cu);
63d06c5c 1036
e7c27a73 1037static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1038
348e048f
DE
1039static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1040
e7c27a73 1041static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1042
e7c27a73 1043static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1044
96408a79
SA
1045static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1046
ff013f42
JK
1047static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1048 struct dwarf2_cu *, struct partial_symtab *);
1049
a14ed312 1050static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1051 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1052 struct partial_symtab *);
c906108c 1053
fae299cd
DC
1054static void get_scope_pc_bounds (struct die_info *,
1055 CORE_ADDR *, CORE_ADDR *,
1056 struct dwarf2_cu *);
1057
801e3a5b
JB
1058static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1059 CORE_ADDR, struct dwarf2_cu *);
1060
a14ed312 1061static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1062 struct dwarf2_cu *);
c906108c 1063
a14ed312 1064static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1065 struct type *, struct dwarf2_cu *);
c906108c 1066
a14ed312 1067static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1068 struct die_info *, struct type *,
e7c27a73 1069 struct dwarf2_cu *);
c906108c 1070
a14ed312 1071static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1072 struct type *,
1073 struct dwarf2_cu *);
c906108c 1074
134d01f1 1075static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1076
e7c27a73 1077static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1078
e7c27a73 1079static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1080
5d7cb8df
JK
1081static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1082
27aa8d6a
SW
1083static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1084
f55ee35c
JK
1085static struct type *read_module_type (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
38d518c9 1088static const char *namespace_name (struct die_info *die,
e142c38c 1089 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1090
134d01f1 1091static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1092
e7c27a73 1093static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1094
6e70227d 1095static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1096 struct dwarf2_cu *);
1097
93311388 1098static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1099
93311388
DE
1100static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1101 gdb_byte *info_ptr,
d97bc12b
DE
1102 gdb_byte **new_info_ptr,
1103 struct die_info *parent);
1104
93311388
DE
1105static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1106 gdb_byte *info_ptr,
fe1b8b76 1107 gdb_byte **new_info_ptr,
639d11d3
DC
1108 struct die_info *parent);
1109
93311388
DE
1110static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
fe1b8b76 1112 gdb_byte **new_info_ptr,
639d11d3
DC
1113 struct die_info *parent);
1114
93311388
DE
1115static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1116 struct die_info **, gdb_byte *,
1117 int *);
1118
e7c27a73 1119static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1120
71c25dea
TT
1121static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1122 struct obstack *);
1123
e142c38c 1124static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1125
98bfdba5
PA
1126static const char *dwarf2_full_name (char *name,
1127 struct die_info *die,
1128 struct dwarf2_cu *cu);
1129
e142c38c 1130static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1131 struct dwarf2_cu **);
9219021c 1132
a14ed312 1133static char *dwarf_tag_name (unsigned int);
c906108c 1134
a14ed312 1135static char *dwarf_attr_name (unsigned int);
c906108c 1136
a14ed312 1137static char *dwarf_form_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_bool_name (unsigned int);
c906108c 1140
a14ed312 1141static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1142
1143#if 0
a14ed312 1144static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1145#endif
1146
f9aca02d 1147static struct die_info *sibling_die (struct die_info *);
c906108c 1148
d97bc12b
DE
1149static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1150
1151static void dump_die_for_error (struct die_info *);
1152
1153static void dump_die_1 (struct ui_file *, int level, int max_level,
1154 struct die_info *);
c906108c 1155
d97bc12b 1156/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1157
51545339 1158static void store_in_ref_table (struct die_info *,
10b3939b 1159 struct dwarf2_cu *);
c906108c 1160
93311388
DE
1161static int is_ref_attr (struct attribute *);
1162
c764a876 1163static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1164
43bbcdc2 1165static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1166
348e048f
DE
1167static struct die_info *follow_die_ref_or_sig (struct die_info *,
1168 struct attribute *,
1169 struct dwarf2_cu **);
1170
10b3939b
DJ
1171static struct die_info *follow_die_ref (struct die_info *,
1172 struct attribute *,
f2f0e013 1173 struct dwarf2_cu **);
c906108c 1174
348e048f
DE
1175static struct die_info *follow_die_sig (struct die_info *,
1176 struct attribute *,
1177 struct dwarf2_cu **);
1178
6c83ed52
TT
1179static struct signatured_type *lookup_signatured_type_at_offset
1180 (struct objfile *objfile,
1181 struct dwarf2_section_info *section,
1182 unsigned int offset);
1183
e5fe5e75 1184static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1185
a0f42c21 1186static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1187
c906108c
SS
1188/* memory allocation interface */
1189
7b5a2f43 1190static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1191
f3dd6933 1192static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1193
b60c80d6 1194static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1195
2e276125 1196static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1197 char *, bfd *, struct dwarf2_cu *,
1198 struct dwarf2_section_info *,
1199 int);
2e276125 1200
8e19ed76
PS
1201static int attr_form_is_block (struct attribute *);
1202
3690dd37
JB
1203static int attr_form_is_section_offset (struct attribute *);
1204
1205static int attr_form_is_constant (struct attribute *);
1206
8cf6f0b1
TT
1207static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1208 struct dwarf2_loclist_baton *baton,
1209 struct attribute *attr);
1210
93e7bd98
DJ
1211static void dwarf2_symbol_mark_computed (struct attribute *attr,
1212 struct symbol *sym,
1213 struct dwarf2_cu *cu);
4c2df51b 1214
93311388
DE
1215static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1216 struct abbrev_info *abbrev,
1217 struct dwarf2_cu *cu);
4bb7a0a7 1218
72bf9492
DJ
1219static void free_stack_comp_unit (void *);
1220
72bf9492
DJ
1221static hashval_t partial_die_hash (const void *item);
1222
1223static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1224
ae038cb0 1225static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1226 (unsigned int offset, struct objfile *objfile);
ae038cb0 1227
9816fde3 1228static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1229 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1230
1231static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1232 struct die_info *comp_unit_die);
93311388 1233
68dc6402 1234static void free_heap_comp_unit (void *);
ae038cb0
DJ
1235
1236static void free_cached_comp_units (void *);
1237
1238static void age_cached_comp_units (void);
1239
1240static void free_one_cached_comp_unit (void *);
1241
f792889a
DJ
1242static struct type *set_die_type (struct die_info *, struct type *,
1243 struct dwarf2_cu *);
1c379e20 1244
ae038cb0
DJ
1245static void create_all_comp_units (struct objfile *);
1246
1fd400ff
TT
1247static int create_debug_types_hash_table (struct objfile *objfile);
1248
a0f42c21 1249static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1250
1251static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1252
1253static void dwarf2_add_dependence (struct dwarf2_cu *,
1254 struct dwarf2_per_cu_data *);
1255
ae038cb0
DJ
1256static void dwarf2_mark (struct dwarf2_cu *);
1257
1258static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1259
673bfd45
DE
1260static struct type *get_die_type_at_offset (unsigned int,
1261 struct dwarf2_per_cu_data *per_cu);
1262
f792889a 1263static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1264
9291a0cd
TT
1265static void dwarf2_release_queue (void *dummy);
1266
a0f42c21 1267static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1268
a0f42c21 1269static void process_queue (void);
9291a0cd
TT
1270
1271static void find_file_and_directory (struct die_info *die,
1272 struct dwarf2_cu *cu,
1273 char **name, char **comp_dir);
1274
1275static char *file_full_name (int file, struct line_header *lh,
1276 const char *comp_dir);
1277
1278static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1279 gdb_byte *info_ptr,
1280 gdb_byte *buffer,
1281 unsigned int buffer_size,
460c1c54 1282 bfd *abfd,
b0df02fd 1283 int is_debug_types_section);
9291a0cd
TT
1284
1285static void init_cu_die_reader (struct die_reader_specs *reader,
1286 struct dwarf2_cu *cu);
1287
673bfd45 1288static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1289
9291a0cd
TT
1290#if WORDS_BIGENDIAN
1291
1292/* Convert VALUE between big- and little-endian. */
1293static offset_type
1294byte_swap (offset_type value)
1295{
1296 offset_type result;
1297
1298 result = (value & 0xff) << 24;
1299 result |= (value & 0xff00) << 8;
1300 result |= (value & 0xff0000) >> 8;
1301 result |= (value & 0xff000000) >> 24;
1302 return result;
1303}
1304
1305#define MAYBE_SWAP(V) byte_swap (V)
1306
1307#else
1308#define MAYBE_SWAP(V) (V)
1309#endif /* WORDS_BIGENDIAN */
1310
1311/* The suffix for an index file. */
1312#define INDEX_SUFFIX ".gdb-index"
1313
3da10d80
KS
1314static const char *dwarf2_physname (char *name, struct die_info *die,
1315 struct dwarf2_cu *cu);
1316
c906108c 1317/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1318 information and return true if we have enough to do something.
1319 NAMES points to the dwarf2 section names, or is NULL if the standard
1320 ELF names are used. */
c906108c
SS
1321
1322int
251d32d9
TG
1323dwarf2_has_info (struct objfile *objfile,
1324 const struct dwarf2_debug_sections *names)
c906108c 1325{
be391dca
TT
1326 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1327 if (!dwarf2_per_objfile)
1328 {
1329 /* Initialize per-objfile state. */
1330 struct dwarf2_per_objfile *data
1331 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1332
be391dca
TT
1333 memset (data, 0, sizeof (*data));
1334 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1335 dwarf2_per_objfile = data;
6502dd73 1336
251d32d9
TG
1337 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1338 (void *) names);
be391dca
TT
1339 dwarf2_per_objfile->objfile = objfile;
1340 }
1341 return (dwarf2_per_objfile->info.asection != NULL
1342 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1343}
1344
251d32d9
TG
1345/* When loading sections, we look either for uncompressed section or for
1346 compressed section names. */
233a11ab
CS
1347
1348static int
251d32d9
TG
1349section_is_p (const char *section_name,
1350 const struct dwarf2_section_names *names)
233a11ab 1351{
251d32d9
TG
1352 if (names->normal != NULL
1353 && strcmp (section_name, names->normal) == 0)
1354 return 1;
1355 if (names->compressed != NULL
1356 && strcmp (section_name, names->compressed) == 0)
1357 return 1;
1358 return 0;
233a11ab
CS
1359}
1360
c906108c
SS
1361/* This function is mapped across the sections and remembers the
1362 offset and size of each of the debugging sections we are interested
1363 in. */
1364
1365static void
251d32d9 1366dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1367{
251d32d9
TG
1368 const struct dwarf2_debug_sections *names;
1369
1370 if (vnames == NULL)
1371 names = &dwarf2_elf_names;
1372 else
1373 names = (const struct dwarf2_debug_sections *) vnames;
1374
1375 if (section_is_p (sectp->name, &names->info))
c906108c 1376 {
dce234bc
PP
1377 dwarf2_per_objfile->info.asection = sectp;
1378 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1379 }
251d32d9 1380 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1381 {
dce234bc
PP
1382 dwarf2_per_objfile->abbrev.asection = sectp;
1383 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1384 }
251d32d9 1385 else if (section_is_p (sectp->name, &names->line))
c906108c 1386 {
dce234bc
PP
1387 dwarf2_per_objfile->line.asection = sectp;
1388 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1389 }
251d32d9 1390 else if (section_is_p (sectp->name, &names->loc))
c906108c 1391 {
dce234bc
PP
1392 dwarf2_per_objfile->loc.asection = sectp;
1393 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1394 }
251d32d9 1395 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1396 {
dce234bc
PP
1397 dwarf2_per_objfile->macinfo.asection = sectp;
1398 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1399 }
cf2c3c16
TT
1400 else if (section_is_p (sectp->name, &names->macro))
1401 {
1402 dwarf2_per_objfile->macro.asection = sectp;
1403 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1404 }
251d32d9 1405 else if (section_is_p (sectp->name, &names->str))
c906108c 1406 {
dce234bc
PP
1407 dwarf2_per_objfile->str.asection = sectp;
1408 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1409 }
251d32d9 1410 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1411 {
dce234bc
PP
1412 dwarf2_per_objfile->frame.asection = sectp;
1413 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1414 }
251d32d9 1415 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1416 {
3799ccc6 1417 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1418
3799ccc6
EZ
1419 if (aflag & SEC_HAS_CONTENTS)
1420 {
dce234bc
PP
1421 dwarf2_per_objfile->eh_frame.asection = sectp;
1422 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1423 }
b6af0555 1424 }
251d32d9 1425 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1426 {
dce234bc
PP
1427 dwarf2_per_objfile->ranges.asection = sectp;
1428 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1429 }
251d32d9 1430 else if (section_is_p (sectp->name, &names->types))
348e048f 1431 {
8b70b953
TT
1432 struct dwarf2_section_info type_section;
1433
1434 memset (&type_section, 0, sizeof (type_section));
1435 type_section.asection = sectp;
1436 type_section.size = bfd_get_section_size (sectp);
1437
1438 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1439 &type_section);
348e048f 1440 }
251d32d9 1441 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1442 {
1443 dwarf2_per_objfile->gdb_index.asection = sectp;
1444 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1445 }
dce234bc 1446
72dca2f5
FR
1447 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1448 && bfd_section_vma (abfd, sectp) == 0)
1449 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1450}
1451
dce234bc
PP
1452/* Decompress a section that was compressed using zlib. Store the
1453 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1454
1455static void
dce234bc
PP
1456zlib_decompress_section (struct objfile *objfile, asection *sectp,
1457 gdb_byte **outbuf, bfd_size_type *outsize)
1458{
1459 bfd *abfd = objfile->obfd;
1460#ifndef HAVE_ZLIB_H
1461 error (_("Support for zlib-compressed DWARF data (from '%s') "
1462 "is disabled in this copy of GDB"),
1463 bfd_get_filename (abfd));
1464#else
1465 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1466 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1467 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1468 bfd_size_type uncompressed_size;
1469 gdb_byte *uncompressed_buffer;
1470 z_stream strm;
1471 int rc;
1472 int header_size = 12;
1473
1474 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1475 || bfd_bread (compressed_buffer,
1476 compressed_size, abfd) != compressed_size)
dce234bc
PP
1477 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1478 bfd_get_filename (abfd));
1479
1480 /* Read the zlib header. In this case, it should be "ZLIB" followed
1481 by the uncompressed section size, 8 bytes in big-endian order. */
1482 if (compressed_size < header_size
1483 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1484 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1485 bfd_get_filename (abfd));
1486 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[11];
1494
1495 /* It is possible the section consists of several compressed
1496 buffers concatenated together, so we uncompress in a loop. */
1497 strm.zalloc = NULL;
1498 strm.zfree = NULL;
1499 strm.opaque = NULL;
1500 strm.avail_in = compressed_size - header_size;
1501 strm.next_in = (Bytef*) compressed_buffer + header_size;
1502 strm.avail_out = uncompressed_size;
1503 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1504 uncompressed_size);
1505 rc = inflateInit (&strm);
1506 while (strm.avail_in > 0)
1507 {
1508 if (rc != Z_OK)
1509 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 strm.next_out = ((Bytef*) uncompressed_buffer
1512 + (uncompressed_size - strm.avail_out));
1513 rc = inflate (&strm, Z_FINISH);
1514 if (rc != Z_STREAM_END)
1515 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1516 bfd_get_filename (abfd), rc);
1517 rc = inflateReset (&strm);
1518 }
1519 rc = inflateEnd (&strm);
1520 if (rc != Z_OK
1521 || strm.avail_out != 0)
1522 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1523 bfd_get_filename (abfd), rc);
1524
affddf13 1525 do_cleanups (cleanup);
dce234bc
PP
1526 *outbuf = uncompressed_buffer;
1527 *outsize = uncompressed_size;
1528#endif
233a11ab
CS
1529}
1530
9e0ac564
TT
1531/* A helper function that decides whether a section is empty. */
1532
1533static int
1534dwarf2_section_empty_p (struct dwarf2_section_info *info)
1535{
1536 return info->asection == NULL || info->size == 0;
1537}
1538
9cdd5dbd 1539/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1540 OBJFILE, store info about the section into INFO.
1541 If the section is compressed, uncompress it before returning. */
c906108c 1542
dce234bc
PP
1543static void
1544dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1545{
dce234bc
PP
1546 bfd *abfd = objfile->obfd;
1547 asection *sectp = info->asection;
1548 gdb_byte *buf, *retbuf;
1549 unsigned char header[4];
c906108c 1550
be391dca
TT
1551 if (info->readin)
1552 return;
dce234bc 1553 info->buffer = NULL;
b315ab21 1554 info->map_addr = NULL;
be391dca 1555 info->readin = 1;
188dd5d6 1556
9e0ac564 1557 if (dwarf2_section_empty_p (info))
dce234bc 1558 return;
c906108c 1559
dce234bc
PP
1560 /* Check if the file has a 4-byte header indicating compression. */
1561 if (info->size > sizeof (header)
1562 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1563 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1564 {
1565 /* Upon decompression, update the buffer and its size. */
1566 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1567 {
1568 zlib_decompress_section (objfile, sectp, &info->buffer,
1569 &info->size);
1570 return;
1571 }
1572 }
4bdf3d34 1573
dce234bc
PP
1574#ifdef HAVE_MMAP
1575 if (pagesize == 0)
1576 pagesize = getpagesize ();
2e276125 1577
dce234bc
PP
1578 /* Only try to mmap sections which are large enough: we don't want to
1579 waste space due to fragmentation. Also, only try mmap for sections
1580 without relocations. */
1581
1582 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1583 {
b315ab21
TG
1584 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1585 MAP_PRIVATE, sectp->filepos,
1586 &info->map_addr, &info->map_len);
dce234bc 1587
b315ab21 1588 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1589 {
be391dca 1590#if HAVE_POSIX_MADVISE
b315ab21 1591 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1592#endif
dce234bc
PP
1593 return;
1594 }
1595 }
1596#endif
1597
1598 /* If we get here, we are a normal, not-compressed section. */
1599 info->buffer = buf
1600 = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602 /* When debugging .o files, we may need to apply relocations; see
1603 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604 We never compress sections in .o files, so we only need to
1605 try this when the section is not compressed. */
ac8035ab 1606 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1607 if (retbuf != NULL)
1608 {
1609 info->buffer = retbuf;
1610 return;
1611 }
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614 || bfd_bread (buf, info->size, abfd) != info->size)
1615 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616 bfd_get_filename (abfd));
1617}
1618
9e0ac564
TT
1619/* A helper function that returns the size of a section in a safe way.
1620 If you are positive that the section has been read before using the
1621 size, then it is safe to refer to the dwarf2_section_info object's
1622 "size" field directly. In other cases, you must call this
1623 function, because for compressed sections the size field is not set
1624 correctly until the section has been read. */
1625
1626static bfd_size_type
1627dwarf2_section_size (struct objfile *objfile,
1628 struct dwarf2_section_info *info)
1629{
1630 if (!info->readin)
1631 dwarf2_read_section (objfile, info);
1632 return info->size;
1633}
1634
dce234bc 1635/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1636 SECTION_NAME. */
af34e669 1637
dce234bc 1638void
3017a003
TG
1639dwarf2_get_section_info (struct objfile *objfile,
1640 enum dwarf2_section_enum sect,
dce234bc
PP
1641 asection **sectp, gdb_byte **bufp,
1642 bfd_size_type *sizep)
1643{
1644 struct dwarf2_per_objfile *data
1645 = objfile_data (objfile, dwarf2_objfile_data_key);
1646 struct dwarf2_section_info *info;
a3b2a86b
TT
1647
1648 /* We may see an objfile without any DWARF, in which case we just
1649 return nothing. */
1650 if (data == NULL)
1651 {
1652 *sectp = NULL;
1653 *bufp = NULL;
1654 *sizep = 0;
1655 return;
1656 }
3017a003
TG
1657 switch (sect)
1658 {
1659 case DWARF2_DEBUG_FRAME:
1660 info = &data->frame;
1661 break;
1662 case DWARF2_EH_FRAME:
1663 info = &data->eh_frame;
1664 break;
1665 default:
1666 gdb_assert_not_reached ("unexpected section");
1667 }
dce234bc 1668
9e0ac564 1669 dwarf2_read_section (objfile, info);
dce234bc
PP
1670
1671 *sectp = info->asection;
1672 *bufp = info->buffer;
1673 *sizep = info->size;
1674}
1675
9291a0cd 1676\f
7b9f3c50
DE
1677/* DWARF quick_symbols_functions support. */
1678
1679/* TUs can share .debug_line entries, and there can be a lot more TUs than
1680 unique line tables, so we maintain a separate table of all .debug_line
1681 derived entries to support the sharing.
1682 All the quick functions need is the list of file names. We discard the
1683 line_header when we're done and don't need to record it here. */
1684struct quick_file_names
1685{
1686 /* The offset in .debug_line of the line table. We hash on this. */
1687 unsigned int offset;
1688
1689 /* The number of entries in file_names, real_names. */
1690 unsigned int num_file_names;
1691
1692 /* The file names from the line table, after being run through
1693 file_full_name. */
1694 const char **file_names;
1695
1696 /* The file names from the line table after being run through
1697 gdb_realpath. These are computed lazily. */
1698 const char **real_names;
1699};
1700
1701/* When using the index (and thus not using psymtabs), each CU has an
1702 object of this type. This is used to hold information needed by
1703 the various "quick" methods. */
1704struct dwarf2_per_cu_quick_data
1705{
1706 /* The file table. This can be NULL if there was no file table
1707 or it's currently not read in.
1708 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1709 struct quick_file_names *file_names;
1710
1711 /* The corresponding symbol table. This is NULL if symbols for this
1712 CU have not yet been read. */
1713 struct symtab *symtab;
1714
1715 /* A temporary mark bit used when iterating over all CUs in
1716 expand_symtabs_matching. */
1717 unsigned int mark : 1;
1718
1719 /* True if we've tried to read the file table and found there isn't one.
1720 There will be no point in trying to read it again next time. */
1721 unsigned int no_file_data : 1;
1722};
1723
1724/* Hash function for a quick_file_names. */
1725
1726static hashval_t
1727hash_file_name_entry (const void *e)
1728{
1729 const struct quick_file_names *file_data = e;
1730
1731 return file_data->offset;
1732}
1733
1734/* Equality function for a quick_file_names. */
1735
1736static int
1737eq_file_name_entry (const void *a, const void *b)
1738{
1739 const struct quick_file_names *ea = a;
1740 const struct quick_file_names *eb = b;
1741
1742 return ea->offset == eb->offset;
1743}
1744
1745/* Delete function for a quick_file_names. */
1746
1747static void
1748delete_file_name_entry (void *e)
1749{
1750 struct quick_file_names *file_data = e;
1751 int i;
1752
1753 for (i = 0; i < file_data->num_file_names; ++i)
1754 {
1755 xfree ((void*) file_data->file_names[i]);
1756 if (file_data->real_names)
1757 xfree ((void*) file_data->real_names[i]);
1758 }
1759
1760 /* The space for the struct itself lives on objfile_obstack,
1761 so we don't free it here. */
1762}
1763
1764/* Create a quick_file_names hash table. */
1765
1766static htab_t
1767create_quick_file_names_table (unsigned int nr_initial_entries)
1768{
1769 return htab_create_alloc (nr_initial_entries,
1770 hash_file_name_entry, eq_file_name_entry,
1771 delete_file_name_entry, xcalloc, xfree);
1772}
9291a0cd 1773
918dd910
JK
1774/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1775 have to be created afterwards. You should call age_cached_comp_units after
1776 processing PER_CU->CU. dw2_setup must have been already called. */
1777
1778static void
1779load_cu (struct dwarf2_per_cu_data *per_cu)
1780{
b0df02fd 1781 if (per_cu->debug_types_section)
e5fe5e75 1782 load_full_type_unit (per_cu);
918dd910 1783 else
a0f42c21 1784 load_full_comp_unit (per_cu);
918dd910 1785
918dd910 1786 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1787
1788 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1789}
1790
a0f42c21 1791/* Read in the symbols for PER_CU. */
2fdf6df6 1792
9291a0cd 1793static void
a0f42c21 1794dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1795{
1796 struct cleanup *back_to;
1797
1798 back_to = make_cleanup (dwarf2_release_queue, NULL);
1799
a0f42c21 1800 queue_comp_unit (per_cu);
9291a0cd 1801
918dd910 1802 load_cu (per_cu);
9291a0cd 1803
a0f42c21 1804 process_queue ();
9291a0cd
TT
1805
1806 /* Age the cache, releasing compilation units that have not
1807 been used recently. */
1808 age_cached_comp_units ();
1809
1810 do_cleanups (back_to);
1811}
1812
1813/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1814 the objfile from which this CU came. Returns the resulting symbol
1815 table. */
2fdf6df6 1816
9291a0cd 1817static struct symtab *
a0f42c21 1818dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1819{
1820 if (!per_cu->v.quick->symtab)
1821 {
1822 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1823 increment_reading_symtab ();
a0f42c21 1824 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1825 do_cleanups (back_to);
1826 }
1827 return per_cu->v.quick->symtab;
1828}
1829
1fd400ff 1830/* Return the CU given its index. */
2fdf6df6 1831
1fd400ff
TT
1832static struct dwarf2_per_cu_data *
1833dw2_get_cu (int index)
1834{
1835 if (index >= dwarf2_per_objfile->n_comp_units)
1836 {
1837 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1838 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1839 }
1840 return dwarf2_per_objfile->all_comp_units[index];
1841}
1842
9291a0cd
TT
1843/* A helper function that knows how to read a 64-bit value in a way
1844 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1845 otherwise. */
2fdf6df6 1846
9291a0cd
TT
1847static int
1848extract_cu_value (const char *bytes, ULONGEST *result)
1849{
1850 if (sizeof (ULONGEST) < 8)
1851 {
1852 int i;
1853
1854 /* Ignore the upper 4 bytes if they are all zero. */
1855 for (i = 0; i < 4; ++i)
1856 if (bytes[i + 4] != 0)
1857 return 0;
1858
1859 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1860 }
1861 else
1862 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1863 return 1;
1864}
1865
1866/* Read the CU list from the mapped index, and use it to create all
1867 the CU objects for this objfile. Return 0 if something went wrong,
1868 1 if everything went ok. */
2fdf6df6 1869
9291a0cd 1870static int
1fd400ff
TT
1871create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1872 offset_type cu_list_elements)
9291a0cd
TT
1873{
1874 offset_type i;
9291a0cd
TT
1875
1876 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1877 dwarf2_per_objfile->all_comp_units
1878 = obstack_alloc (&objfile->objfile_obstack,
1879 dwarf2_per_objfile->n_comp_units
1880 * sizeof (struct dwarf2_per_cu_data *));
1881
1882 for (i = 0; i < cu_list_elements; i += 2)
1883 {
1884 struct dwarf2_per_cu_data *the_cu;
1885 ULONGEST offset, length;
1886
1887 if (!extract_cu_value (cu_list, &offset)
1888 || !extract_cu_value (cu_list + 8, &length))
1889 return 0;
1890 cu_list += 2 * 8;
1891
1892 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1893 struct dwarf2_per_cu_data);
1894 the_cu->offset = offset;
1895 the_cu->length = length;
1896 the_cu->objfile = objfile;
1897 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1898 struct dwarf2_per_cu_quick_data);
1899 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1900 }
1901
1902 return 1;
1903}
1904
1fd400ff 1905/* Create the signatured type hash table from the index. */
673bfd45 1906
1fd400ff 1907static int
673bfd45 1908create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1909 struct dwarf2_section_info *section,
673bfd45
DE
1910 const gdb_byte *bytes,
1911 offset_type elements)
1fd400ff
TT
1912{
1913 offset_type i;
673bfd45 1914 htab_t sig_types_hash;
1fd400ff 1915
d467dd73
DE
1916 dwarf2_per_objfile->n_type_units = elements / 3;
1917 dwarf2_per_objfile->all_type_units
1fd400ff 1918 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1919 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1920 * sizeof (struct dwarf2_per_cu_data *));
1921
673bfd45 1922 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1923
1924 for (i = 0; i < elements; i += 3)
1925 {
1926 struct signatured_type *type_sig;
1927 ULONGEST offset, type_offset, signature;
1928 void **slot;
1929
1930 if (!extract_cu_value (bytes, &offset)
1931 || !extract_cu_value (bytes + 8, &type_offset))
1932 return 0;
1933 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1934 bytes += 3 * 8;
1935
1936 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1937 struct signatured_type);
1938 type_sig->signature = signature;
1fd400ff 1939 type_sig->type_offset = type_offset;
b0df02fd 1940 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1941 type_sig->per_cu.offset = offset;
1942 type_sig->per_cu.objfile = objfile;
1943 type_sig->per_cu.v.quick
1944 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_quick_data);
1946
673bfd45 1947 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1948 *slot = type_sig;
1949
d467dd73 1950 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1fd400ff
TT
1951 }
1952
673bfd45 1953 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1954
1955 return 1;
1956}
1957
9291a0cd
TT
1958/* Read the address map data from the mapped index, and use it to
1959 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1960
9291a0cd
TT
1961static void
1962create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1963{
1964 const gdb_byte *iter, *end;
1965 struct obstack temp_obstack;
1966 struct addrmap *mutable_map;
1967 struct cleanup *cleanup;
1968 CORE_ADDR baseaddr;
1969
1970 obstack_init (&temp_obstack);
1971 cleanup = make_cleanup_obstack_free (&temp_obstack);
1972 mutable_map = addrmap_create_mutable (&temp_obstack);
1973
1974 iter = index->address_table;
1975 end = iter + index->address_table_size;
1976
1977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1978
1979 while (iter < end)
1980 {
1981 ULONGEST hi, lo, cu_index;
1982 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1983 iter += 8;
1984 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1987 iter += 4;
1988
1989 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1990 dw2_get_cu (cu_index));
9291a0cd
TT
1991 }
1992
1993 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1994 &objfile->objfile_obstack);
1995 do_cleanups (cleanup);
1996}
1997
59d7bcaf
JK
1998/* The hash function for strings in the mapped index. This is the same as
1999 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2000 implementation. This is necessary because the hash function is tied to the
2001 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2002 SYMBOL_HASH_NEXT.
2003
2004 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2005
9291a0cd 2006static hashval_t
559a7a62 2007mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2008{
2009 const unsigned char *str = (const unsigned char *) p;
2010 hashval_t r = 0;
2011 unsigned char c;
2012
2013 while ((c = *str++) != 0)
559a7a62
JK
2014 {
2015 if (index_version >= 5)
2016 c = tolower (c);
2017 r = r * 67 + c - 113;
2018 }
9291a0cd
TT
2019
2020 return r;
2021}
2022
2023/* Find a slot in the mapped index INDEX for the object named NAME.
2024 If NAME is found, set *VEC_OUT to point to the CU vector in the
2025 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2026
9291a0cd
TT
2027static int
2028find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2029 offset_type **vec_out)
2030{
0cf03b49
JK
2031 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2032 offset_type hash;
9291a0cd 2033 offset_type slot, step;
559a7a62 2034 int (*cmp) (const char *, const char *);
9291a0cd 2035
0cf03b49
JK
2036 if (current_language->la_language == language_cplus
2037 || current_language->la_language == language_java
2038 || current_language->la_language == language_fortran)
2039 {
2040 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2041 not contain any. */
2042 const char *paren = strchr (name, '(');
2043
2044 if (paren)
2045 {
2046 char *dup;
2047
2048 dup = xmalloc (paren - name + 1);
2049 memcpy (dup, name, paren - name);
2050 dup[paren - name] = 0;
2051
2052 make_cleanup (xfree, dup);
2053 name = dup;
2054 }
2055 }
2056
559a7a62
JK
2057 /* Index version 4 did not support case insensitive searches. But the
2058 indexes for case insensitive languages are built in lowercase, therefore
2059 simulate our NAME being searched is also lowercased. */
2060 hash = mapped_index_string_hash ((index->version == 4
2061 && case_sensitivity == case_sensitive_off
2062 ? 5 : index->version),
2063 name);
2064
3876f04e
DE
2065 slot = hash & (index->symbol_table_slots - 1);
2066 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2067 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2068
2069 for (;;)
2070 {
2071 /* Convert a slot number to an offset into the table. */
2072 offset_type i = 2 * slot;
2073 const char *str;
3876f04e 2074 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2075 {
2076 do_cleanups (back_to);
2077 return 0;
2078 }
9291a0cd 2079
3876f04e 2080 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2081 if (!cmp (name, str))
9291a0cd
TT
2082 {
2083 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2084 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2085 do_cleanups (back_to);
9291a0cd
TT
2086 return 1;
2087 }
2088
3876f04e 2089 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2090 }
2091}
2092
2093/* Read the index file. If everything went ok, initialize the "quick"
2094 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2095
9291a0cd
TT
2096static int
2097dwarf2_read_index (struct objfile *objfile)
2098{
9291a0cd
TT
2099 char *addr;
2100 struct mapped_index *map;
b3b272e1 2101 offset_type *metadata;
ac0b195c
KW
2102 const gdb_byte *cu_list;
2103 const gdb_byte *types_list = NULL;
2104 offset_type version, cu_list_elements;
2105 offset_type types_list_elements = 0;
1fd400ff 2106 int i;
9291a0cd 2107
9e0ac564 2108 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2109 return 0;
82430852
JK
2110
2111 /* Older elfutils strip versions could keep the section in the main
2112 executable while splitting it for the separate debug info file. */
2113 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2114 & SEC_HAS_CONTENTS) == 0)
2115 return 0;
2116
9291a0cd
TT
2117 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2118
2119 addr = dwarf2_per_objfile->gdb_index.buffer;
2120 /* Version check. */
1fd400ff 2121 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2122 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2123 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2124 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2125 indices. Index version 4 uses a different hash function than index
2126 version 5 and later. */
831adc1f 2127 if (version < 4)
9291a0cd 2128 return 0;
594e8718
JK
2129 /* Indexes with higher version than the one supported by GDB may be no
2130 longer backward compatible. */
559a7a62 2131 if (version > 5)
594e8718 2132 return 0;
9291a0cd
TT
2133
2134 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2135 map->version = version;
b3b272e1 2136 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2137
2138 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2139
2140 i = 0;
2141 cu_list = addr + MAYBE_SWAP (metadata[i]);
2142 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2143 / 8);
1fd400ff
TT
2144 ++i;
2145
987d643c
TT
2146 types_list = addr + MAYBE_SWAP (metadata[i]);
2147 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2148 - MAYBE_SWAP (metadata[i]))
2149 / 8);
2150 ++i;
1fd400ff
TT
2151
2152 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2153 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2154 - MAYBE_SWAP (metadata[i]));
2155 ++i;
2156
3876f04e
DE
2157 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2158 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2159 - MAYBE_SWAP (metadata[i]))
2160 / (2 * sizeof (offset_type)));
1fd400ff 2161 ++i;
9291a0cd 2162
1fd400ff
TT
2163 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2164
2165 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2166 return 0;
2167
8b70b953
TT
2168 if (types_list_elements)
2169 {
2170 struct dwarf2_section_info *section;
2171
2172 /* We can only handle a single .debug_types when we have an
2173 index. */
2174 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2175 return 0;
2176
2177 section = VEC_index (dwarf2_section_info_def,
2178 dwarf2_per_objfile->types, 0);
2179
2180 if (!create_signatured_type_table_from_index (objfile, section,
2181 types_list,
2182 types_list_elements))
2183 return 0;
2184 }
9291a0cd
TT
2185
2186 create_addrmap_from_index (objfile, map);
2187
2188 dwarf2_per_objfile->index_table = map;
2189 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2190 dwarf2_per_objfile->quick_file_names_table =
2191 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2192
2193 return 1;
2194}
2195
2196/* A helper for the "quick" functions which sets the global
2197 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2198
9291a0cd
TT
2199static void
2200dw2_setup (struct objfile *objfile)
2201{
2202 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2203 gdb_assert (dwarf2_per_objfile);
2204}
2205
2206/* A helper for the "quick" functions which attempts to read the line
2207 table for THIS_CU. */
2fdf6df6 2208
7b9f3c50
DE
2209static struct quick_file_names *
2210dw2_get_file_names (struct objfile *objfile,
2211 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2212{
2213 bfd *abfd = objfile->obfd;
7b9f3c50 2214 struct line_header *lh;
9291a0cd
TT
2215 struct attribute *attr;
2216 struct cleanup *cleanups;
2217 struct die_info *comp_unit_die;
36374493 2218 struct dwarf2_section_info* sec;
adabb602 2219 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2220 int has_children, i;
2221 struct dwarf2_cu cu;
2222 unsigned int bytes_read, buffer_size;
2223 struct die_reader_specs reader_specs;
2224 char *name, *comp_dir;
7b9f3c50
DE
2225 void **slot;
2226 struct quick_file_names *qfn;
2227 unsigned int line_offset;
9291a0cd 2228
7b9f3c50
DE
2229 if (this_cu->v.quick->file_names != NULL)
2230 return this_cu->v.quick->file_names;
2231 /* If we know there is no line data, no point in looking again. */
2232 if (this_cu->v.quick->no_file_data)
2233 return NULL;
9291a0cd 2234
23745b47 2235 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2236 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2237
b0df02fd
DE
2238 if (this_cu->debug_types_section)
2239 sec = this_cu->debug_types_section;
36374493
DE
2240 else
2241 sec = &dwarf2_per_objfile->info;
2242 dwarf2_read_section (objfile, sec);
2243 buffer_size = sec->size;
2244 buffer = sec->buffer;
9291a0cd 2245 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2246
2247 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2248 buffer, buffer_size,
460c1c54 2249 abfd,
b0df02fd 2250 this_cu->debug_types_section != NULL);
9291a0cd 2251
6caca83c
CC
2252 /* Skip dummy compilation units. */
2253 if (info_ptr >= buffer + buffer_size
2254 || peek_abbrev_code (abfd, info_ptr) == 0)
2255 {
2256 do_cleanups (cleanups);
2257 return NULL;
2258 }
2259
e5fe5e75 2260 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2261 make_cleanup (dwarf2_free_abbrev_table, &cu);
2262
9291a0cd 2263 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2264 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2265 &has_children);
9291a0cd 2266
7b9f3c50
DE
2267 lh = NULL;
2268 slot = NULL;
2269 line_offset = 0;
9291a0cd
TT
2270 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2271 if (attr)
2272 {
7b9f3c50
DE
2273 struct quick_file_names find_entry;
2274
2275 line_offset = DW_UNSND (attr);
2276
2277 /* We may have already read in this line header (TU line header sharing).
2278 If we have we're done. */
2279 find_entry.offset = line_offset;
2280 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2281 &find_entry, INSERT);
2282 if (*slot != NULL)
2283 {
2284 do_cleanups (cleanups);
2285 this_cu->v.quick->file_names = *slot;
2286 return *slot;
2287 }
2288
9291a0cd
TT
2289 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2290 }
2291 if (lh == NULL)
2292 {
2293 do_cleanups (cleanups);
7b9f3c50
DE
2294 this_cu->v.quick->no_file_data = 1;
2295 return NULL;
9291a0cd
TT
2296 }
2297
7b9f3c50
DE
2298 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2299 qfn->offset = line_offset;
2300 gdb_assert (slot != NULL);
2301 *slot = qfn;
9291a0cd 2302
7b9f3c50 2303 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2304
7b9f3c50
DE
2305 qfn->num_file_names = lh->num_file_names;
2306 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2307 lh->num_file_names * sizeof (char *));
9291a0cd 2308 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2309 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2310 qfn->real_names = NULL;
9291a0cd 2311
7b9f3c50 2312 free_line_header (lh);
9291a0cd 2313 do_cleanups (cleanups);
7b9f3c50
DE
2314
2315 this_cu->v.quick->file_names = qfn;
2316 return qfn;
9291a0cd
TT
2317}
2318
2319/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2320 real path for a given file name from the line table. */
2fdf6df6 2321
9291a0cd 2322static const char *
7b9f3c50
DE
2323dw2_get_real_path (struct objfile *objfile,
2324 struct quick_file_names *qfn, int index)
9291a0cd 2325{
7b9f3c50
DE
2326 if (qfn->real_names == NULL)
2327 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2328 qfn->num_file_names, sizeof (char *));
9291a0cd 2329
7b9f3c50
DE
2330 if (qfn->real_names[index] == NULL)
2331 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2332
7b9f3c50 2333 return qfn->real_names[index];
9291a0cd
TT
2334}
2335
2336static struct symtab *
2337dw2_find_last_source_symtab (struct objfile *objfile)
2338{
2339 int index;
ae2de4f8 2340
9291a0cd
TT
2341 dw2_setup (objfile);
2342 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2343 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2344}
2345
7b9f3c50
DE
2346/* Traversal function for dw2_forget_cached_source_info. */
2347
2348static int
2349dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2350{
7b9f3c50 2351 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2352
7b9f3c50 2353 if (file_data->real_names)
9291a0cd 2354 {
7b9f3c50 2355 int i;
9291a0cd 2356
7b9f3c50 2357 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2358 {
7b9f3c50
DE
2359 xfree ((void*) file_data->real_names[i]);
2360 file_data->real_names[i] = NULL;
9291a0cd
TT
2361 }
2362 }
7b9f3c50
DE
2363
2364 return 1;
2365}
2366
2367static void
2368dw2_forget_cached_source_info (struct objfile *objfile)
2369{
2370 dw2_setup (objfile);
2371
2372 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2373 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2374}
2375
f8eba3c6
TT
2376/* Helper function for dw2_map_symtabs_matching_filename that expands
2377 the symtabs and calls the iterator. */
2378
2379static int
2380dw2_map_expand_apply (struct objfile *objfile,
2381 struct dwarf2_per_cu_data *per_cu,
2382 const char *name,
2383 const char *full_path, const char *real_path,
2384 int (*callback) (struct symtab *, void *),
2385 void *data)
2386{
2387 struct symtab *last_made = objfile->symtabs;
2388
2389 /* Don't visit already-expanded CUs. */
2390 if (per_cu->v.quick->symtab)
2391 return 0;
2392
2393 /* This may expand more than one symtab, and we want to iterate over
2394 all of them. */
a0f42c21 2395 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2396
2397 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2398 objfile->symtabs, last_made);
2399}
2400
2401/* Implementation of the map_symtabs_matching_filename method. */
2402
9291a0cd 2403static int
f8eba3c6
TT
2404dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2405 const char *full_path, const char *real_path,
2406 int (*callback) (struct symtab *, void *),
2407 void *data)
9291a0cd
TT
2408{
2409 int i;
c011a4f4
DE
2410 const char *name_basename = lbasename (name);
2411 int check_basename = name_basename == name;
9291a0cd
TT
2412 struct dwarf2_per_cu_data *base_cu = NULL;
2413
2414 dw2_setup (objfile);
ae2de4f8 2415
1fd400ff 2416 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2417 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2418 {
2419 int j;
e254ef6a 2420 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2421 struct quick_file_names *file_data;
9291a0cd 2422
3d7bb9d9 2423 /* We only need to look at symtabs not already expanded. */
e254ef6a 2424 if (per_cu->v.quick->symtab)
9291a0cd
TT
2425 continue;
2426
7b9f3c50
DE
2427 file_data = dw2_get_file_names (objfile, per_cu);
2428 if (file_data == NULL)
9291a0cd
TT
2429 continue;
2430
7b9f3c50 2431 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2432 {
7b9f3c50 2433 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2434
2435 if (FILENAME_CMP (name, this_name) == 0)
2436 {
f8eba3c6
TT
2437 if (dw2_map_expand_apply (objfile, per_cu,
2438 name, full_path, real_path,
2439 callback, data))
2440 return 1;
9291a0cd
TT
2441 }
2442
2443 if (check_basename && ! base_cu
2444 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2445 base_cu = per_cu;
9291a0cd 2446
c011a4f4
DE
2447 /* Before we invoke realpath, which can get expensive when many
2448 files are involved, do a quick comparison of the basenames. */
2449 if (! basenames_may_differ
2450 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2451 continue;
2452
9291a0cd
TT
2453 if (full_path != NULL)
2454 {
7b9f3c50
DE
2455 const char *this_real_name = dw2_get_real_path (objfile,
2456 file_data, j);
9291a0cd 2457
7b9f3c50
DE
2458 if (this_real_name != NULL
2459 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2460 {
f8eba3c6
TT
2461 if (dw2_map_expand_apply (objfile, per_cu,
2462 name, full_path, real_path,
2463 callback, data))
2464 return 1;
9291a0cd
TT
2465 }
2466 }
2467
2468 if (real_path != NULL)
2469 {
7b9f3c50
DE
2470 const char *this_real_name = dw2_get_real_path (objfile,
2471 file_data, j);
9291a0cd 2472
7b9f3c50
DE
2473 if (this_real_name != NULL
2474 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2475 {
f8eba3c6
TT
2476 if (dw2_map_expand_apply (objfile, per_cu,
2477 name, full_path, real_path,
2478 callback, data))
2479 return 1;
9291a0cd
TT
2480 }
2481 }
2482 }
2483 }
2484
2485 if (base_cu)
2486 {
f8eba3c6
TT
2487 if (dw2_map_expand_apply (objfile, base_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
9291a0cd
TT
2491 }
2492
2493 return 0;
2494}
2495
2496static struct symtab *
2497dw2_lookup_symbol (struct objfile *objfile, int block_index,
2498 const char *name, domain_enum domain)
2499{
774b6a14 2500 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2501 instead. */
2502 return NULL;
2503}
2504
2505/* A helper function that expands all symtabs that hold an object
2506 named NAME. */
2fdf6df6 2507
9291a0cd
TT
2508static void
2509dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2510{
2511 dw2_setup (objfile);
2512
ae2de4f8 2513 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2514 if (dwarf2_per_objfile->index_table)
2515 {
2516 offset_type *vec;
2517
2518 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2519 name, &vec))
2520 {
2521 offset_type i, len = MAYBE_SWAP (*vec);
2522 for (i = 0; i < len; ++i)
2523 {
2524 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2525 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2526
a0f42c21 2527 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2528 }
2529 }
2530 }
2531}
2532
774b6a14
TT
2533static void
2534dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2535 enum block_enum block_kind, const char *name,
774b6a14 2536 domain_enum domain)
9291a0cd 2537{
774b6a14 2538 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2539}
2540
2541static void
2542dw2_print_stats (struct objfile *objfile)
2543{
2544 int i, count;
2545
2546 dw2_setup (objfile);
2547 count = 0;
1fd400ff 2548 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2549 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2550 {
e254ef6a 2551 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2552
e254ef6a 2553 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2554 ++count;
2555 }
2556 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2557}
2558
2559static void
2560dw2_dump (struct objfile *objfile)
2561{
2562 /* Nothing worth printing. */
2563}
2564
2565static void
2566dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2567 struct section_offsets *delta)
2568{
2569 /* There's nothing to relocate here. */
2570}
2571
2572static void
2573dw2_expand_symtabs_for_function (struct objfile *objfile,
2574 const char *func_name)
2575{
2576 dw2_do_expand_symtabs_matching (objfile, func_name);
2577}
2578
2579static void
2580dw2_expand_all_symtabs (struct objfile *objfile)
2581{
2582 int i;
2583
2584 dw2_setup (objfile);
1fd400ff
TT
2585
2586 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2587 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2588 {
e254ef6a 2589 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2590
a0f42c21 2591 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2592 }
2593}
2594
2595static void
2596dw2_expand_symtabs_with_filename (struct objfile *objfile,
2597 const char *filename)
2598{
2599 int i;
2600
2601 dw2_setup (objfile);
d4637a04
DE
2602
2603 /* We don't need to consider type units here.
2604 This is only called for examining code, e.g. expand_line_sal.
2605 There can be an order of magnitude (or more) more type units
2606 than comp units, and we avoid them if we can. */
2607
2608 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2609 {
2610 int j;
e254ef6a 2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2612 struct quick_file_names *file_data;
9291a0cd 2613
3d7bb9d9 2614 /* We only need to look at symtabs not already expanded. */
e254ef6a 2615 if (per_cu->v.quick->symtab)
9291a0cd
TT
2616 continue;
2617
7b9f3c50
DE
2618 file_data = dw2_get_file_names (objfile, per_cu);
2619 if (file_data == NULL)
9291a0cd
TT
2620 continue;
2621
7b9f3c50 2622 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2623 {
7b9f3c50 2624 const char *this_name = file_data->file_names[j];
1ef75ecc 2625 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2626 {
a0f42c21 2627 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2628 break;
2629 }
2630 }
2631 }
2632}
2633
dd786858 2634static const char *
9291a0cd
TT
2635dw2_find_symbol_file (struct objfile *objfile, const char *name)
2636{
e254ef6a 2637 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2638 offset_type *vec;
7b9f3c50 2639 struct quick_file_names *file_data;
9291a0cd
TT
2640
2641 dw2_setup (objfile);
2642
ae2de4f8 2643 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2644 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2645 {
2646 struct symtab *s;
2647
2648 ALL_OBJFILE_SYMTABS (objfile, s)
2649 if (s->primary)
2650 {
2651 struct blockvector *bv = BLOCKVECTOR (s);
2652 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2653 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2654
2655 if (sym)
2656 return sym->symtab->filename;
2657 }
2658 return NULL;
2659 }
9291a0cd
TT
2660
2661 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2662 name, &vec))
2663 return NULL;
2664
2665 /* Note that this just looks at the very first one named NAME -- but
2666 actually we are looking for a function. find_main_filename
2667 should be rewritten so that it doesn't require a custom hook. It
2668 could just use the ordinary symbol tables. */
2669 /* vec[0] is the length, which must always be >0. */
e254ef6a 2670 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2671
7b9f3c50
DE
2672 file_data = dw2_get_file_names (objfile, per_cu);
2673 if (file_data == NULL)
9291a0cd
TT
2674 return NULL;
2675
7b9f3c50 2676 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2677}
2678
2679static void
40658b94
PH
2680dw2_map_matching_symbols (const char * name, domain_enum namespace,
2681 struct objfile *objfile, int global,
2682 int (*callback) (struct block *,
2683 struct symbol *, void *),
2edb89d3
JK
2684 void *data, symbol_compare_ftype *match,
2685 symbol_compare_ftype *ordered_compare)
9291a0cd 2686{
40658b94 2687 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2688 current language is Ada for a non-Ada objfile using GNU index. As Ada
2689 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2690}
2691
2692static void
f8eba3c6
TT
2693dw2_expand_symtabs_matching
2694 (struct objfile *objfile,
2695 int (*file_matcher) (const char *, void *),
2696 int (*name_matcher) (const struct language_defn *, const char *, void *),
2697 enum search_domain kind,
2698 void *data)
9291a0cd
TT
2699{
2700 int i;
2701 offset_type iter;
4b5246aa 2702 struct mapped_index *index;
9291a0cd
TT
2703
2704 dw2_setup (objfile);
ae2de4f8
DE
2705
2706 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2707 if (!dwarf2_per_objfile->index_table)
2708 return;
4b5246aa 2709 index = dwarf2_per_objfile->index_table;
9291a0cd 2710
7b08b9eb
JK
2711 if (file_matcher != NULL)
2712 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2713 + dwarf2_per_objfile->n_type_units); ++i)
7b08b9eb
JK
2714 {
2715 int j;
2716 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2717 struct quick_file_names *file_data;
2718
2719 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2720
2721 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2722 if (per_cu->v.quick->symtab)
2723 continue;
2724
2725 file_data = dw2_get_file_names (objfile, per_cu);
2726 if (file_data == NULL)
2727 continue;
2728
2729 for (j = 0; j < file_data->num_file_names; ++j)
2730 {
2731 if (file_matcher (file_data->file_names[j], data))
2732 {
2733 per_cu->v.quick->mark = 1;
2734 break;
2735 }
2736 }
2737 }
9291a0cd 2738
3876f04e 2739 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2740 {
2741 offset_type idx = 2 * iter;
2742 const char *name;
2743 offset_type *vec, vec_len, vec_idx;
2744
3876f04e 2745 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2746 continue;
2747
3876f04e 2748 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2749
f8eba3c6 2750 if (! (*name_matcher) (current_language, name, data))
9291a0cd
TT
2751 continue;
2752
2753 /* The name was matched, now expand corresponding CUs that were
2754 marked. */
4b5246aa 2755 vec = (offset_type *) (index->constant_pool
3876f04e 2756 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2757 vec_len = MAYBE_SWAP (vec[0]);
2758 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2759 {
e254ef6a 2760 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2761
e254ef6a 2762 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2763 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2764 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2765 }
2766 }
2767}
2768
2769static struct symtab *
2770dw2_find_pc_sect_symtab (struct objfile *objfile,
2771 struct minimal_symbol *msymbol,
2772 CORE_ADDR pc,
2773 struct obj_section *section,
2774 int warn_if_readin)
2775{
2776 struct dwarf2_per_cu_data *data;
2777
2778 dw2_setup (objfile);
2779
2780 if (!objfile->psymtabs_addrmap)
2781 return NULL;
2782
2783 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2784 if (!data)
2785 return NULL;
2786
2787 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2788 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2789 paddress (get_objfile_arch (objfile), pc));
2790
a0f42c21 2791 return dw2_instantiate_symtab (data);
9291a0cd
TT
2792}
2793
9291a0cd 2794static void
44b13c5a 2795dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2796 void *data, int need_fullname)
9291a0cd
TT
2797{
2798 int i;
2799
2800 dw2_setup (objfile);
ae2de4f8 2801
1fd400ff 2802 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2803 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2804 {
2805 int j;
e254ef6a 2806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2807 struct quick_file_names *file_data;
9291a0cd 2808
3d7bb9d9 2809 /* We only need to look at symtabs not already expanded. */
e254ef6a 2810 if (per_cu->v.quick->symtab)
9291a0cd
TT
2811 continue;
2812
7b9f3c50
DE
2813 file_data = dw2_get_file_names (objfile, per_cu);
2814 if (file_data == NULL)
9291a0cd
TT
2815 continue;
2816
7b9f3c50 2817 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2818 {
74e2f255
DE
2819 const char *this_real_name;
2820
2821 if (need_fullname)
2822 this_real_name = dw2_get_real_path (objfile, file_data, j);
2823 else
2824 this_real_name = NULL;
7b9f3c50 2825 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2826 }
2827 }
2828}
2829
2830static int
2831dw2_has_symbols (struct objfile *objfile)
2832{
2833 return 1;
2834}
2835
2836const struct quick_symbol_functions dwarf2_gdb_index_functions =
2837{
2838 dw2_has_symbols,
2839 dw2_find_last_source_symtab,
2840 dw2_forget_cached_source_info,
f8eba3c6 2841 dw2_map_symtabs_matching_filename,
9291a0cd 2842 dw2_lookup_symbol,
774b6a14 2843 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2844 dw2_print_stats,
2845 dw2_dump,
2846 dw2_relocate,
2847 dw2_expand_symtabs_for_function,
2848 dw2_expand_all_symtabs,
2849 dw2_expand_symtabs_with_filename,
2850 dw2_find_symbol_file,
40658b94 2851 dw2_map_matching_symbols,
9291a0cd
TT
2852 dw2_expand_symtabs_matching,
2853 dw2_find_pc_sect_symtab,
9291a0cd
TT
2854 dw2_map_symbol_filenames
2855};
2856
2857/* Initialize for reading DWARF for this objfile. Return 0 if this
2858 file will use psymtabs, or 1 if using the GNU index. */
2859
2860int
2861dwarf2_initialize_objfile (struct objfile *objfile)
2862{
2863 /* If we're about to read full symbols, don't bother with the
2864 indices. In this case we also don't care if some other debug
2865 format is making psymtabs, because they are all about to be
2866 expanded anyway. */
2867 if ((objfile->flags & OBJF_READNOW))
2868 {
2869 int i;
2870
2871 dwarf2_per_objfile->using_index = 1;
2872 create_all_comp_units (objfile);
1fd400ff 2873 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2874 dwarf2_per_objfile->quick_file_names_table =
2875 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2876
1fd400ff 2877 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2878 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2879 {
e254ef6a 2880 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2881
e254ef6a
DE
2882 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2883 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2884 }
2885
2886 /* Return 1 so that gdb sees the "quick" functions. However,
2887 these functions will be no-ops because we will have expanded
2888 all symtabs. */
2889 return 1;
2890 }
2891
2892 if (dwarf2_read_index (objfile))
2893 return 1;
2894
9291a0cd
TT
2895 return 0;
2896}
2897
2898\f
2899
dce234bc
PP
2900/* Build a partial symbol table. */
2901
2902void
f29dff0a 2903dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2904{
f29dff0a 2905 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2906 {
2907 init_psymbol_list (objfile, 1024);
2908 }
2909
d146bf1e 2910 dwarf2_build_psymtabs_hard (objfile);
c906108c 2911}
c906108c 2912
45452591
DE
2913/* Return TRUE if OFFSET is within CU_HEADER. */
2914
2915static inline int
2916offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2917{
2918 unsigned int bottom = cu_header->offset;
2919 unsigned int top = (cu_header->offset
2920 + cu_header->length
2921 + cu_header->initial_length_size);
9a619af0 2922
45452591
DE
2923 return (offset >= bottom && offset < top);
2924}
2925
93311388
DE
2926/* Read in the comp unit header information from the debug_info at info_ptr.
2927 NOTE: This leaves members offset, first_die_offset to be filled in
2928 by the caller. */
107d2387 2929
fe1b8b76 2930static gdb_byte *
107d2387 2931read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2932 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2933{
2934 int signed_addr;
891d2f0b 2935 unsigned int bytes_read;
c764a876
DE
2936
2937 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2938 cu_header->initial_length_size = bytes_read;
2939 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2940 info_ptr += bytes_read;
107d2387
AC
2941 cu_header->version = read_2_bytes (abfd, info_ptr);
2942 info_ptr += 2;
613e1657 2943 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2944 &bytes_read);
613e1657 2945 info_ptr += bytes_read;
107d2387
AC
2946 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2947 info_ptr += 1;
2948 signed_addr = bfd_get_sign_extend_vma (abfd);
2949 if (signed_addr < 0)
8e65ff28 2950 internal_error (__FILE__, __LINE__,
e2e0b3e5 2951 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2952 cu_header->signed_addr_p = signed_addr;
c764a876 2953
107d2387
AC
2954 return info_ptr;
2955}
2956
adabb602
DE
2957/* Read in a CU header and perform some basic error checking. */
2958
fe1b8b76
JB
2959static gdb_byte *
2960partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2961 gdb_byte *buffer, unsigned int buffer_size,
b0df02fd 2962 bfd *abfd, int is_debug_types_section)
72bf9492 2963{
fe1b8b76 2964 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 2965
adabb602
DE
2966 header->offset = beg_of_comp_unit - buffer;
2967
72bf9492
DJ
2968 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2969
460c1c54
CC
2970 /* If we're reading a type unit, skip over the signature and
2971 type_offset fields. */
b0df02fd 2972 if (is_debug_types_section)
460c1c54
CC
2973 info_ptr += 8 /*signature*/ + header->offset_size;
2974
adabb602
DE
2975 header->first_die_offset = info_ptr - beg_of_comp_unit;
2976
2dc7f7b3 2977 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2978 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2979 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2980 bfd_get_filename (abfd));
72bf9492 2981
9e0ac564
TT
2982 if (header->abbrev_offset
2983 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2984 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2985 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2986 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2987 (long) header->abbrev_offset,
93311388 2988 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2989 bfd_get_filename (abfd));
2990
2991 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2992 > buffer + buffer_size)
8a3fe4f8
AC
2993 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2994 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2995 (long) header->length,
93311388 2996 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2997 bfd_get_filename (abfd));
2998
2999 return info_ptr;
3000}
3001
348e048f
DE
3002/* Read in the types comp unit header information from .debug_types entry at
3003 types_ptr. The result is a pointer to one past the end of the header. */
3004
3005static gdb_byte *
3006read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3007 struct dwarf2_section_info *section,
348e048f
DE
3008 ULONGEST *signature,
3009 gdb_byte *types_ptr, bfd *abfd)
3010{
348e048f
DE
3011 gdb_byte *initial_types_ptr = types_ptr;
3012
8b70b953
TT
3013 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3014 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3015
3016 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3017
3018 *signature = read_8_bytes (abfd, types_ptr);
3019 types_ptr += 8;
3020 types_ptr += cu_header->offset_size;
3021 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3022
3023 return types_ptr;
3024}
3025
aaa75496
JB
3026/* Allocate a new partial symtab for file named NAME and mark this new
3027 partial symtab as being an include of PST. */
3028
3029static void
3030dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3031 struct objfile *objfile)
3032{
3033 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3034
3035 subpst->section_offsets = pst->section_offsets;
3036 subpst->textlow = 0;
3037 subpst->texthigh = 0;
3038
3039 subpst->dependencies = (struct partial_symtab **)
3040 obstack_alloc (&objfile->objfile_obstack,
3041 sizeof (struct partial_symtab *));
3042 subpst->dependencies[0] = pst;
3043 subpst->number_of_dependencies = 1;
3044
3045 subpst->globals_offset = 0;
3046 subpst->n_global_syms = 0;
3047 subpst->statics_offset = 0;
3048 subpst->n_static_syms = 0;
3049 subpst->symtab = NULL;
3050 subpst->read_symtab = pst->read_symtab;
3051 subpst->readin = 0;
3052
3053 /* No private part is necessary for include psymtabs. This property
3054 can be used to differentiate between such include psymtabs and
10b3939b 3055 the regular ones. */
58a9656e 3056 subpst->read_symtab_private = NULL;
aaa75496
JB
3057}
3058
3059/* Read the Line Number Program data and extract the list of files
3060 included by the source file represented by PST. Build an include
d85a05f0 3061 partial symtab for each of these included files. */
aaa75496
JB
3062
3063static void
3064dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3065 struct die_info *die,
aaa75496
JB
3066 struct partial_symtab *pst)
3067{
3068 struct objfile *objfile = cu->objfile;
3069 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3070 struct line_header *lh = NULL;
3071 struct attribute *attr;
aaa75496 3072
d85a05f0
DJ
3073 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3074 if (attr)
3075 {
3076 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3077
d85a05f0
DJ
3078 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3079 }
aaa75496
JB
3080 if (lh == NULL)
3081 return; /* No linetable, so no includes. */
3082
c6da4cef
DE
3083 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3084 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3085
3086 free_line_header (lh);
3087}
3088
348e048f
DE
3089static hashval_t
3090hash_type_signature (const void *item)
3091{
3092 const struct signatured_type *type_sig = item;
9a619af0 3093
348e048f
DE
3094 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3095 return type_sig->signature;
3096}
3097
3098static int
3099eq_type_signature (const void *item_lhs, const void *item_rhs)
3100{
3101 const struct signatured_type *lhs = item_lhs;
3102 const struct signatured_type *rhs = item_rhs;
9a619af0 3103
348e048f
DE
3104 return lhs->signature == rhs->signature;
3105}
3106
1fd400ff
TT
3107/* Allocate a hash table for signatured types. */
3108
3109static htab_t
673bfd45 3110allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3111{
3112 return htab_create_alloc_ex (41,
3113 hash_type_signature,
3114 eq_type_signature,
3115 NULL,
3116 &objfile->objfile_obstack,
3117 hashtab_obstack_allocate,
3118 dummy_obstack_deallocate);
3119}
3120
d467dd73 3121/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3122
3123static int
d467dd73 3124add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3125{
3126 struct signatured_type *sigt = *slot;
3127 struct dwarf2_per_cu_data ***datap = datum;
3128
3129 **datap = &sigt->per_cu;
3130 ++*datap;
3131
3132 return 1;
3133}
3134
d467dd73 3135/* Create the hash table of all entries in the .debug_types section(s).
348e048f
DE
3136 The result is zero if there is an error (e.g. missing .debug_types section),
3137 otherwise non-zero. */
3138
3139static int
3140create_debug_types_hash_table (struct objfile *objfile)
3141{
8b70b953 3142 htab_t types_htab = NULL;
1fd400ff 3143 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3144 int ix;
3145 struct dwarf2_section_info *section;
348e048f 3146
8b70b953 3147 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3148 {
3149 dwarf2_per_objfile->signatured_types = NULL;
3150 return 0;
3151 }
3152
8b70b953
TT
3153 for (ix = 0;
3154 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3155 ix, section);
3156 ++ix)
3157 {
3158 gdb_byte *info_ptr, *end_ptr;
348e048f 3159
8b70b953
TT
3160 dwarf2_read_section (objfile, section);
3161 info_ptr = section->buffer;
348e048f 3162
8b70b953
TT
3163 if (info_ptr == NULL)
3164 continue;
348e048f 3165
8b70b953
TT
3166 if (types_htab == NULL)
3167 types_htab = allocate_signatured_type_table (objfile);
348e048f 3168
8b70b953
TT
3169 if (dwarf2_die_debug)
3170 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3171
3172 end_ptr = info_ptr + section->size;
3173 while (info_ptr < end_ptr)
3174 {
3175 unsigned int offset;
3176 unsigned int offset_size;
3177 unsigned int type_offset;
3178 unsigned int length, initial_length_size;
3179 unsigned short version;
3180 ULONGEST signature;
3181 struct signatured_type *type_sig;
3182 void **slot;
3183 gdb_byte *ptr = info_ptr;
348e048f 3184
8b70b953 3185 offset = ptr - section->buffer;
348e048f 3186
8b70b953
TT
3187 /* We need to read the type's signature in order to build the hash
3188 table, but we don't need to read anything else just yet. */
348e048f 3189
8b70b953
TT
3190 /* Sanity check to ensure entire cu is present. */
3191 length = read_initial_length (objfile->obfd, ptr,
3192 &initial_length_size);
3193 if (ptr + length + initial_length_size > end_ptr)
3194 {
3195 complaint (&symfile_complaints,
3196 _("debug type entry runs off end "
3197 "of `.debug_types' section, ignored"));
3198 break;
3199 }
348e048f 3200
8b70b953
TT
3201 offset_size = initial_length_size == 4 ? 4 : 8;
3202 ptr += initial_length_size;
3203 version = bfd_get_16 (objfile->obfd, ptr);
3204 ptr += 2;
3205 ptr += offset_size; /* abbrev offset */
3206 ptr += 1; /* address size */
3207 signature = bfd_get_64 (objfile->obfd, ptr);
3208 ptr += 8;
3209 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3210 ptr += offset_size;
6caca83c
CC
3211
3212 /* Skip dummy type units. */
3213 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3214 {
3215 info_ptr = info_ptr + initial_length_size + length;
3216 continue;
3217 }
8b70b953
TT
3218
3219 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3220 memset (type_sig, 0, sizeof (*type_sig));
3221 type_sig->signature = signature;
3222 type_sig->type_offset = type_offset;
3223 type_sig->per_cu.objfile = objfile;
b0df02fd 3224 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3225 type_sig->per_cu.offset = offset;
3226
3227 slot = htab_find_slot (types_htab, type_sig, INSERT);
3228 gdb_assert (slot != NULL);
3229 if (*slot != NULL)
3230 {
3231 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3232
8b70b953
TT
3233 complaint (&symfile_complaints,
3234 _("debug type entry at offset 0x%x is duplicate to the "
3235 "entry at offset 0x%x, signature 0x%s"),
3236 offset, dup_sig->per_cu.offset,
3237 phex (signature, sizeof (signature)));
3238 gdb_assert (signature == dup_sig->signature);
3239 }
3240 *slot = type_sig;
348e048f 3241
8b70b953
TT
3242 if (dwarf2_die_debug)
3243 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3244 offset, phex (signature, sizeof (signature)));
348e048f 3245
8b70b953
TT
3246 info_ptr = info_ptr + initial_length_size + length;
3247 }
348e048f
DE
3248 }
3249
3250 dwarf2_per_objfile->signatured_types = types_htab;
3251
d467dd73
DE
3252 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3253 dwarf2_per_objfile->all_type_units
1fd400ff 3254 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3255 dwarf2_per_objfile->n_type_units
1fd400ff 3256 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3257 iter = &dwarf2_per_objfile->all_type_units[0];
3258 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3259 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3260 == dwarf2_per_objfile->n_type_units);
1fd400ff 3261
348e048f
DE
3262 return 1;
3263}
3264
3265/* Lookup a signature based type.
3266 Returns NULL if SIG is not present in the table. */
3267
3268static struct signatured_type *
3269lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3270{
3271 struct signatured_type find_entry, *entry;
3272
3273 if (dwarf2_per_objfile->signatured_types == NULL)
3274 {
3275 complaint (&symfile_complaints,
55f1336d 3276 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3277 return 0;
3278 }
3279
3280 find_entry.signature = sig;
3281 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3282 return entry;
3283}
3284
d85a05f0
DJ
3285/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3286
3287static void
3288init_cu_die_reader (struct die_reader_specs *reader,
3289 struct dwarf2_cu *cu)
3290{
3291 reader->abfd = cu->objfile->obfd;
3292 reader->cu = cu;
b0df02fd 3293 if (cu->per_cu->debug_types_section)
be391dca 3294 {
b0df02fd
DE
3295 gdb_assert (cu->per_cu->debug_types_section->readin);
3296 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3297 }
d85a05f0 3298 else
be391dca
TT
3299 {
3300 gdb_assert (dwarf2_per_objfile->info.readin);
3301 reader->buffer = dwarf2_per_objfile->info.buffer;
3302 }
d85a05f0
DJ
3303}
3304
3305/* Find the base address of the compilation unit for range lists and
3306 location lists. It will normally be specified by DW_AT_low_pc.
3307 In DWARF-3 draft 4, the base address could be overridden by
3308 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3309 compilation units with discontinuous ranges. */
3310
3311static void
3312dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3313{
3314 struct attribute *attr;
3315
3316 cu->base_known = 0;
3317 cu->base_address = 0;
3318
3319 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3320 if (attr)
3321 {
3322 cu->base_address = DW_ADDR (attr);
3323 cu->base_known = 1;
3324 }
3325 else
3326 {
3327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3328 if (attr)
3329 {
3330 cu->base_address = DW_ADDR (attr);
3331 cu->base_known = 1;
3332 }
3333 }
3334}
3335
348e048f
DE
3336/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3337 to combine the common parts.
21b2bd31
DE
3338 Process compilation unit THIS_CU for a psymtab.
3339 SECTION is the section the CU/TU comes from,
3340 either .debug_info or .debug_types. */
aaa75496 3341
21b2bd31 3342void
a0f42c21 3343process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3344 struct dwarf2_section_info *section,
3345 int is_debug_types_section)
c906108c 3346{
a0f42c21 3347 struct objfile *objfile = this_cu->objfile;
c906108c 3348 bfd *abfd = objfile->obfd;
21b2bd31
DE
3349 gdb_byte *buffer = section->buffer;
3350 gdb_byte *info_ptr = buffer + this_cu->offset;
3351 unsigned int buffer_size = section->size;
93311388 3352 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3353 struct die_info *comp_unit_die;
c906108c 3354 struct partial_symtab *pst;
5734ee8b 3355 CORE_ADDR baseaddr;
93311388
DE
3356 struct cleanup *back_to_inner;
3357 struct dwarf2_cu cu;
d85a05f0
DJ
3358 int has_children, has_pc_info;
3359 struct attribute *attr;
d85a05f0
DJ
3360 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3361 struct die_reader_specs reader_specs;
3e2a0cee 3362 const char *filename;
c906108c 3363
23745b47
DE
3364 /* If this compilation unit was already read in, free the
3365 cached copy in order to read it in again. This is
3366 necessary because we skipped some symbols when we first
3367 read in the compilation unit (see load_partial_dies).
3368 This problem could be avoided, but the benefit is
3369 unclear. */
3370 if (this_cu->cu != NULL)
3371 free_one_cached_comp_unit (this_cu->cu);
3372
3373 /* Note that this is a pointer to our stack frame, being
3374 added to a global data structure. It will be cleaned up
3375 in free_stack_comp_unit when we finish with this
3376 compilation unit. */
3377 init_one_comp_unit (&cu, this_cu);
93311388 3378 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3379
93311388
DE
3380 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3381 buffer, buffer_size,
460c1c54 3382 abfd,
21b2bd31 3383 is_debug_types_section);
10b3939b 3384
6caca83c
CC
3385 /* Skip dummy compilation units. */
3386 if (info_ptr >= buffer + buffer_size
3387 || peek_abbrev_code (abfd, info_ptr) == 0)
3388 {
6caca83c 3389 do_cleanups (back_to_inner);
21b2bd31 3390 return;
6caca83c
CC
3391 }
3392
93311388 3393 cu.list_in_scope = &file_symbols;
af703f96 3394
93311388 3395 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3396 dwarf2_read_abbrevs (&cu);
93311388 3397 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3398
93311388 3399 /* Read the compilation unit die. */
d85a05f0
DJ
3400 init_cu_die_reader (&reader_specs, &cu);
3401 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3402 &has_children);
93311388 3403
21b2bd31 3404 if (is_debug_types_section)
348e048f 3405 {
b3c8eb43
JK
3406 /* LENGTH has not been set yet for type units. */
3407 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3408 this_cu->length = cu.header.length + cu.header.initial_length_size;
3409 }
d85a05f0 3410 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3411 {
93311388 3412 do_cleanups (back_to_inner);
21b2bd31 3413 return;
93311388 3414 }
72bf9492 3415
9816fde3 3416 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3417
93311388 3418 /* Allocate a new partial symbol table structure. */
d85a05f0 3419 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3420 if (attr == NULL || !DW_STRING (attr))
3421 filename = "";
3422 else
3423 filename = DW_STRING (attr);
93311388 3424 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3425 filename,
93311388
DE
3426 /* TEXTLOW and TEXTHIGH are set below. */
3427 0,
3428 objfile->global_psymbols.next,
3429 objfile->static_psymbols.next);
9750bca9 3430 pst->psymtabs_addrmap_supported = 1;
72bf9492 3431
d85a05f0
DJ
3432 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3433 if (attr != NULL)
3434 pst->dirname = DW_STRING (attr);
72bf9492 3435
e38df1d0 3436 pst->read_symtab_private = this_cu;
72bf9492 3437
93311388 3438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3439
0963b4bd 3440 /* Store the function that reads in the rest of the symbol table. */
93311388 3441 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3442
9291a0cd 3443 this_cu->v.psymtab = pst;
c906108c 3444
d85a05f0
DJ
3445 dwarf2_find_base_address (comp_unit_die, &cu);
3446
93311388
DE
3447 /* Possibly set the default values of LOWPC and HIGHPC from
3448 `DW_AT_ranges'. */
d85a05f0
DJ
3449 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3450 &best_highpc, &cu, pst);
3451 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3452 /* Store the contiguous range if it is not empty; it can be empty for
3453 CUs with no code. */
3454 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3455 best_lowpc + baseaddr,
3456 best_highpc + baseaddr - 1, pst);
93311388
DE
3457
3458 /* Check if comp unit has_children.
3459 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3460 If not, there's no more debug_info for this comp unit. */
d85a05f0 3461 if (has_children)
93311388
DE
3462 {
3463 struct partial_die_info *first_die;
3464 CORE_ADDR lowpc, highpc;
31ffec48 3465
93311388
DE
3466 lowpc = ((CORE_ADDR) -1);
3467 highpc = ((CORE_ADDR) 0);
c906108c 3468
93311388 3469 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3470
93311388 3471 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3472 ! has_pc_info, &cu);
57c22c6c 3473
93311388
DE
3474 /* If we didn't find a lowpc, set it to highpc to avoid
3475 complaints from `maint check'. */
3476 if (lowpc == ((CORE_ADDR) -1))
3477 lowpc = highpc;
10b3939b 3478
93311388
DE
3479 /* If the compilation unit didn't have an explicit address range,
3480 then use the information extracted from its child dies. */
d85a05f0 3481 if (! has_pc_info)
93311388 3482 {
d85a05f0
DJ
3483 best_lowpc = lowpc;
3484 best_highpc = highpc;
93311388
DE
3485 }
3486 }
d85a05f0
DJ
3487 pst->textlow = best_lowpc + baseaddr;
3488 pst->texthigh = best_highpc + baseaddr;
c906108c 3489
93311388
DE
3490 pst->n_global_syms = objfile->global_psymbols.next -
3491 (objfile->global_psymbols.list + pst->globals_offset);
3492 pst->n_static_syms = objfile->static_psymbols.next -
3493 (objfile->static_psymbols.list + pst->statics_offset);
3494 sort_pst_symbols (pst);
c906108c 3495
21b2bd31 3496 if (is_debug_types_section)
348e048f
DE
3497 {
3498 /* It's not clear we want to do anything with stmt lists here.
3499 Waiting to see what gcc ultimately does. */
3500 }
d85a05f0 3501 else
93311388
DE
3502 {
3503 /* Get the list of files included in the current compilation unit,
3504 and build a psymtab for each of them. */
d85a05f0 3505 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3506 }
ae038cb0 3507
93311388 3508 do_cleanups (back_to_inner);
93311388 3509}
ff013f42 3510
348e048f
DE
3511/* Traversal function for htab_traverse_noresize.
3512 Process one .debug_types comp-unit. */
3513
3514static int
3515process_type_comp_unit (void **slot, void *info)
3516{
3517 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3518 struct dwarf2_per_cu_data *this_cu;
3519
a0f42c21 3520 gdb_assert (info == NULL);
348e048f 3521 this_cu = &entry->per_cu;
348e048f 3522
b0df02fd 3523 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3524 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3525
3526 return 1;
3527}
3528
3529/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3530 Build partial symbol tables for the .debug_types comp-units. */
3531
3532static void
3533build_type_psymtabs (struct objfile *objfile)
3534{
3535 if (! create_debug_types_hash_table (objfile))
3536 return;
3537
3538 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3539 process_type_comp_unit, NULL);
348e048f
DE
3540}
3541
60606b2c
TT
3542/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3543
3544static void
3545psymtabs_addrmap_cleanup (void *o)
3546{
3547 struct objfile *objfile = o;
ec61707d 3548
60606b2c
TT
3549 objfile->psymtabs_addrmap = NULL;
3550}
3551
93311388
DE
3552/* Build the partial symbol table by doing a quick pass through the
3553 .debug_info and .debug_abbrev sections. */
72bf9492 3554
93311388 3555static void
c67a9c90 3556dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3557{
60606b2c
TT
3558 struct cleanup *back_to, *addrmap_cleanup;
3559 struct obstack temp_obstack;
21b2bd31 3560 int i;
93311388 3561
98bfdba5
PA
3562 dwarf2_per_objfile->reading_partial_symbols = 1;
3563
be391dca 3564 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3565
93311388
DE
3566 /* Any cached compilation units will be linked by the per-objfile
3567 read_in_chain. Make sure to free them when we're done. */
3568 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3569
348e048f
DE
3570 build_type_psymtabs (objfile);
3571
93311388 3572 create_all_comp_units (objfile);
c906108c 3573
60606b2c
TT
3574 /* Create a temporary address map on a temporary obstack. We later
3575 copy this to the final obstack. */
3576 obstack_init (&temp_obstack);
3577 make_cleanup_obstack_free (&temp_obstack);
3578 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3579 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3580
21b2bd31 3581 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3582 {
21b2bd31 3583 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3584
21b2bd31 3585 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3586 }
ff013f42
JK
3587
3588 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3589 &objfile->objfile_obstack);
60606b2c 3590 discard_cleanups (addrmap_cleanup);
ff013f42 3591
ae038cb0
DJ
3592 do_cleanups (back_to);
3593}
3594
93311388 3595/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3596
3597static void
a0f42c21 3598load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3599{
a0f42c21 3600 struct objfile *objfile = this_cu->objfile;
ae038cb0 3601 bfd *abfd = objfile->obfd;
adabb602 3602 gdb_byte *info_ptr;
d85a05f0 3603 struct die_info *comp_unit_die;
ae038cb0 3604 struct dwarf2_cu *cu;
1d9ec526 3605 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3606 int has_children;
3607 struct die_reader_specs reader_specs;
98bfdba5 3608 int read_cu = 0;
ae038cb0 3609
b0df02fd 3610 gdb_assert (! this_cu->debug_types_section);
348e048f 3611
be391dca 3612 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3613 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3614
98bfdba5
PA
3615 if (this_cu->cu == NULL)
3616 {
9816fde3 3617 cu = xmalloc (sizeof (*cu));
23745b47 3618 init_one_comp_unit (cu, this_cu);
ae038cb0 3619
98bfdba5 3620 read_cu = 1;
ae038cb0 3621
98bfdba5 3622 /* If an error occurs while loading, release our storage. */
68dc6402 3623 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3624
98bfdba5
PA
3625 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3626 dwarf2_per_objfile->info.buffer,
3627 dwarf2_per_objfile->info.size,
460c1c54 3628 abfd, 0);
ae038cb0 3629
6caca83c
CC
3630 /* Skip dummy compilation units. */
3631 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3632 + dwarf2_per_objfile->info.size)
3633 || peek_abbrev_code (abfd, info_ptr) == 0)
3634 {
3635 do_cleanups (free_cu_cleanup);
3636 return;
3637 }
3638
98bfdba5
PA
3639 /* Link this CU into read_in_chain. */
3640 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3641 dwarf2_per_objfile->read_in_chain = this_cu;
3642 }
3643 else
3644 {
3645 cu = this_cu->cu;
3646 info_ptr += cu->header.first_die_offset;
3647 }
ae038cb0
DJ
3648
3649 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3650 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3651 dwarf2_read_abbrevs (cu);
98bfdba5 3652 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3653
3654 /* Read the compilation unit die. */
d85a05f0
DJ
3655 init_cu_die_reader (&reader_specs, cu);
3656 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3657 &has_children);
ae038cb0 3658
9816fde3 3659 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3660
ae038cb0
DJ
3661 /* Check if comp unit has_children.
3662 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3663 If not, there's no more debug_info for this comp unit. */
d85a05f0 3664 if (has_children)
93311388 3665 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3666
98bfdba5
PA
3667 do_cleanups (free_abbrevs_cleanup);
3668
3669 if (read_cu)
3670 {
3671 /* We've successfully allocated this compilation unit. Let our
3672 caller clean it up when finished with it. */
3673 discard_cleanups (free_cu_cleanup);
3674 }
ae038cb0
DJ
3675}
3676
9cdd5dbd
DE
3677/* Create a list of all compilation units in OBJFILE.
3678 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3679
3680static void
3681create_all_comp_units (struct objfile *objfile)
3682{
3683 int n_allocated;
3684 int n_comp_units;
3685 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3686 gdb_byte *info_ptr;
3687
3688 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3689 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3690
3691 n_comp_units = 0;
3692 n_allocated = 10;
3693 all_comp_units = xmalloc (n_allocated
3694 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3695
3e43a32a
MS
3696 while (info_ptr < dwarf2_per_objfile->info.buffer
3697 + dwarf2_per_objfile->info.size)
ae038cb0 3698 {
c764a876 3699 unsigned int length, initial_length_size;
ae038cb0 3700 struct dwarf2_per_cu_data *this_cu;
c764a876 3701 unsigned int offset;
ae038cb0 3702
dce234bc 3703 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3704
3705 /* Read just enough information to find out where the next
3706 compilation unit is. */
c764a876
DE
3707 length = read_initial_length (objfile->obfd, info_ptr,
3708 &initial_length_size);
ae038cb0
DJ
3709
3710 /* Save the compilation unit for later lookup. */
3711 this_cu = obstack_alloc (&objfile->objfile_obstack,
3712 sizeof (struct dwarf2_per_cu_data));
3713 memset (this_cu, 0, sizeof (*this_cu));
3714 this_cu->offset = offset;
c764a876 3715 this_cu->length = length + initial_length_size;
9291a0cd 3716 this_cu->objfile = objfile;
ae038cb0
DJ
3717
3718 if (n_comp_units == n_allocated)
3719 {
3720 n_allocated *= 2;
3721 all_comp_units = xrealloc (all_comp_units,
3722 n_allocated
3723 * sizeof (struct dwarf2_per_cu_data *));
3724 }
3725 all_comp_units[n_comp_units++] = this_cu;
3726
3727 info_ptr = info_ptr + this_cu->length;
3728 }
3729
3730 dwarf2_per_objfile->all_comp_units
3731 = obstack_alloc (&objfile->objfile_obstack,
3732 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3733 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3734 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3735 xfree (all_comp_units);
3736 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3737}
3738
5734ee8b
DJ
3739/* Process all loaded DIEs for compilation unit CU, starting at
3740 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3741 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3742 DW_AT_ranges). If NEED_PC is set, then this function will set
3743 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3744 and record the covered ranges in the addrmap. */
c906108c 3745
72bf9492
DJ
3746static void
3747scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3748 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3749{
72bf9492 3750 struct partial_die_info *pdi;
c906108c 3751
91c24f0a
DC
3752 /* Now, march along the PDI's, descending into ones which have
3753 interesting children but skipping the children of the other ones,
3754 until we reach the end of the compilation unit. */
c906108c 3755
72bf9492 3756 pdi = first_die;
91c24f0a 3757
72bf9492
DJ
3758 while (pdi != NULL)
3759 {
3760 fixup_partial_die (pdi, cu);
c906108c 3761
f55ee35c 3762 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3763 children, so we need to look at them. Ditto for anonymous
3764 enums. */
933c6fe4 3765
72bf9492 3766 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3767 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3768 {
72bf9492 3769 switch (pdi->tag)
c906108c
SS
3770 {
3771 case DW_TAG_subprogram:
5734ee8b 3772 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3773 break;
72929c62 3774 case DW_TAG_constant:
c906108c
SS
3775 case DW_TAG_variable:
3776 case DW_TAG_typedef:
91c24f0a 3777 case DW_TAG_union_type:
72bf9492 3778 if (!pdi->is_declaration)
63d06c5c 3779 {
72bf9492 3780 add_partial_symbol (pdi, cu);
63d06c5c
DC
3781 }
3782 break;
c906108c 3783 case DW_TAG_class_type:
680b30c7 3784 case DW_TAG_interface_type:
c906108c 3785 case DW_TAG_structure_type:
72bf9492 3786 if (!pdi->is_declaration)
c906108c 3787 {
72bf9492 3788 add_partial_symbol (pdi, cu);
c906108c
SS
3789 }
3790 break;
91c24f0a 3791 case DW_TAG_enumeration_type:
72bf9492
DJ
3792 if (!pdi->is_declaration)
3793 add_partial_enumeration (pdi, cu);
c906108c
SS
3794 break;
3795 case DW_TAG_base_type:
a02abb62 3796 case DW_TAG_subrange_type:
c906108c 3797 /* File scope base type definitions are added to the partial
c5aa993b 3798 symbol table. */
72bf9492 3799 add_partial_symbol (pdi, cu);
c906108c 3800 break;
d9fa45fe 3801 case DW_TAG_namespace:
5734ee8b 3802 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3803 break;
5d7cb8df
JK
3804 case DW_TAG_module:
3805 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3806 break;
c906108c
SS
3807 default:
3808 break;
3809 }
3810 }
3811
72bf9492
DJ
3812 /* If the die has a sibling, skip to the sibling. */
3813
3814 pdi = pdi->die_sibling;
3815 }
3816}
3817
3818/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3819
72bf9492 3820 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3821 name is concatenated with "::" and the partial DIE's name. For
3822 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3823 Enumerators are an exception; they use the scope of their parent
3824 enumeration type, i.e. the name of the enumeration type is not
3825 prepended to the enumerator.
91c24f0a 3826
72bf9492
DJ
3827 There are two complexities. One is DW_AT_specification; in this
3828 case "parent" means the parent of the target of the specification,
3829 instead of the direct parent of the DIE. The other is compilers
3830 which do not emit DW_TAG_namespace; in this case we try to guess
3831 the fully qualified name of structure types from their members'
3832 linkage names. This must be done using the DIE's children rather
3833 than the children of any DW_AT_specification target. We only need
3834 to do this for structures at the top level, i.e. if the target of
3835 any DW_AT_specification (if any; otherwise the DIE itself) does not
3836 have a parent. */
3837
3838/* Compute the scope prefix associated with PDI's parent, in
3839 compilation unit CU. The result will be allocated on CU's
3840 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3841 field. NULL is returned if no prefix is necessary. */
3842static char *
3843partial_die_parent_scope (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845{
3846 char *grandparent_scope;
3847 struct partial_die_info *parent, *real_pdi;
91c24f0a 3848
72bf9492
DJ
3849 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3850 then this means the parent of the specification DIE. */
3851
3852 real_pdi = pdi;
72bf9492 3853 while (real_pdi->has_specification)
10b3939b 3854 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3855
3856 parent = real_pdi->die_parent;
3857 if (parent == NULL)
3858 return NULL;
3859
3860 if (parent->scope_set)
3861 return parent->scope;
3862
3863 fixup_partial_die (parent, cu);
3864
10b3939b 3865 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3866
acebe513
UW
3867 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3868 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3869 Work around this problem here. */
3870 if (cu->language == language_cplus
6e70227d 3871 && parent->tag == DW_TAG_namespace
acebe513
UW
3872 && strcmp (parent->name, "::") == 0
3873 && grandparent_scope == NULL)
3874 {
3875 parent->scope = NULL;
3876 parent->scope_set = 1;
3877 return NULL;
3878 }
3879
9c6c53f7
SA
3880 if (pdi->tag == DW_TAG_enumerator)
3881 /* Enumerators should not get the name of the enumeration as a prefix. */
3882 parent->scope = grandparent_scope;
3883 else if (parent->tag == DW_TAG_namespace
f55ee35c 3884 || parent->tag == DW_TAG_module
72bf9492
DJ
3885 || parent->tag == DW_TAG_structure_type
3886 || parent->tag == DW_TAG_class_type
680b30c7 3887 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3888 || parent->tag == DW_TAG_union_type
3889 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3890 {
3891 if (grandparent_scope == NULL)
3892 parent->scope = parent->name;
3893 else
3e43a32a
MS
3894 parent->scope = typename_concat (&cu->comp_unit_obstack,
3895 grandparent_scope,
f55ee35c 3896 parent->name, 0, cu);
72bf9492 3897 }
72bf9492
DJ
3898 else
3899 {
3900 /* FIXME drow/2004-04-01: What should we be doing with
3901 function-local names? For partial symbols, we should probably be
3902 ignoring them. */
3903 complaint (&symfile_complaints,
e2e0b3e5 3904 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3905 parent->tag, pdi->offset);
3906 parent->scope = grandparent_scope;
c906108c
SS
3907 }
3908
72bf9492
DJ
3909 parent->scope_set = 1;
3910 return parent->scope;
3911}
3912
3913/* Return the fully scoped name associated with PDI, from compilation unit
3914 CU. The result will be allocated with malloc. */
3915static char *
3916partial_die_full_name (struct partial_die_info *pdi,
3917 struct dwarf2_cu *cu)
3918{
3919 char *parent_scope;
3920
98bfdba5
PA
3921 /* If this is a template instantiation, we can not work out the
3922 template arguments from partial DIEs. So, unfortunately, we have
3923 to go through the full DIEs. At least any work we do building
3924 types here will be reused if full symbols are loaded later. */
3925 if (pdi->has_template_arguments)
3926 {
3927 fixup_partial_die (pdi, cu);
3928
3929 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3930 {
3931 struct die_info *die;
3932 struct attribute attr;
3933 struct dwarf2_cu *ref_cu = cu;
3934
3935 attr.name = 0;
3936 attr.form = DW_FORM_ref_addr;
3937 attr.u.addr = pdi->offset;
3938 die = follow_die_ref (NULL, &attr, &ref_cu);
3939
3940 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3941 }
3942 }
3943
72bf9492
DJ
3944 parent_scope = partial_die_parent_scope (pdi, cu);
3945 if (parent_scope == NULL)
3946 return NULL;
3947 else
f55ee35c 3948 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3949}
3950
3951static void
72bf9492 3952add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3953{
e7c27a73 3954 struct objfile *objfile = cu->objfile;
c906108c 3955 CORE_ADDR addr = 0;
decbce07 3956 char *actual_name = NULL;
5c4e30ca 3957 const struct partial_symbol *psym = NULL;
e142c38c 3958 CORE_ADDR baseaddr;
72bf9492 3959 int built_actual_name = 0;
e142c38c
DJ
3960
3961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3962
94af9270
KS
3963 actual_name = partial_die_full_name (pdi, cu);
3964 if (actual_name)
3965 built_actual_name = 1;
63d06c5c 3966
72bf9492
DJ
3967 if (actual_name == NULL)
3968 actual_name = pdi->name;
3969
c906108c
SS
3970 switch (pdi->tag)
3971 {
3972 case DW_TAG_subprogram:
2cfa0c8d 3973 if (pdi->is_external || cu->language == language_ada)
c906108c 3974 {
2cfa0c8d
JB
3975 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3976 of the global scope. But in Ada, we want to be able to access
3977 nested procedures globally. So all Ada subprograms are stored
3978 in the global scope. */
f47fb265 3979 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3980 mst_text, objfile); */
f47fb265
MS
3981 add_psymbol_to_list (actual_name, strlen (actual_name),
3982 built_actual_name,
3983 VAR_DOMAIN, LOC_BLOCK,
3984 &objfile->global_psymbols,
3985 0, pdi->lowpc + baseaddr,
3986 cu->language, objfile);
c906108c
SS
3987 }
3988 else
3989 {
f47fb265 3990 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3991 mst_file_text, objfile); */
f47fb265
MS
3992 add_psymbol_to_list (actual_name, strlen (actual_name),
3993 built_actual_name,
3994 VAR_DOMAIN, LOC_BLOCK,
3995 &objfile->static_psymbols,
3996 0, pdi->lowpc + baseaddr,
3997 cu->language, objfile);
c906108c
SS
3998 }
3999 break;
72929c62
JB
4000 case DW_TAG_constant:
4001 {
4002 struct psymbol_allocation_list *list;
4003
4004 if (pdi->is_external)
4005 list = &objfile->global_psymbols;
4006 else
4007 list = &objfile->static_psymbols;
f47fb265
MS
4008 add_psymbol_to_list (actual_name, strlen (actual_name),
4009 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4010 list, 0, 0, cu->language, objfile);
72929c62
JB
4011 }
4012 break;
c906108c 4013 case DW_TAG_variable:
caac4577
JG
4014 if (pdi->locdesc)
4015 addr = decode_locdesc (pdi->locdesc, cu);
4016
4017 if (pdi->locdesc
4018 && addr == 0
4019 && !dwarf2_per_objfile->has_section_at_zero)
4020 {
4021 /* A global or static variable may also have been stripped
4022 out by the linker if unused, in which case its address
4023 will be nullified; do not add such variables into partial
4024 symbol table then. */
4025 }
4026 else if (pdi->is_external)
c906108c
SS
4027 {
4028 /* Global Variable.
4029 Don't enter into the minimal symbol tables as there is
4030 a minimal symbol table entry from the ELF symbols already.
4031 Enter into partial symbol table if it has a location
4032 descriptor or a type.
4033 If the location descriptor is missing, new_symbol will create
4034 a LOC_UNRESOLVED symbol, the address of the variable will then
4035 be determined from the minimal symbol table whenever the variable
4036 is referenced.
4037 The address for the partial symbol table entry is not
4038 used by GDB, but it comes in handy for debugging partial symbol
4039 table building. */
4040
c906108c 4041 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_STATIC,
4045 &objfile->global_psymbols,
4046 0, addr + baseaddr,
4047 cu->language, objfile);
c906108c
SS
4048 }
4049 else
4050 {
0963b4bd 4051 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4052 if (pdi->locdesc == NULL)
decbce07
MS
4053 {
4054 if (built_actual_name)
4055 xfree (actual_name);
4056 return;
4057 }
f47fb265 4058 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4059 mst_file_data, objfile); */
f47fb265
MS
4060 add_psymbol_to_list (actual_name, strlen (actual_name),
4061 built_actual_name,
4062 VAR_DOMAIN, LOC_STATIC,
4063 &objfile->static_psymbols,
4064 0, addr + baseaddr,
4065 cu->language, objfile);
c906108c
SS
4066 }
4067 break;
4068 case DW_TAG_typedef:
4069 case DW_TAG_base_type:
a02abb62 4070 case DW_TAG_subrange_type:
38d518c9 4071 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4072 built_actual_name,
176620f1 4073 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4074 &objfile->static_psymbols,
e142c38c 4075 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4076 break;
72bf9492
DJ
4077 case DW_TAG_namespace:
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4079 built_actual_name,
72bf9492
DJ
4080 VAR_DOMAIN, LOC_TYPEDEF,
4081 &objfile->global_psymbols,
4082 0, (CORE_ADDR) 0, cu->language, objfile);
4083 break;
c906108c 4084 case DW_TAG_class_type:
680b30c7 4085 case DW_TAG_interface_type:
c906108c
SS
4086 case DW_TAG_structure_type:
4087 case DW_TAG_union_type:
4088 case DW_TAG_enumeration_type:
fa4028e9
JB
4089 /* Skip external references. The DWARF standard says in the section
4090 about "Structure, Union, and Class Type Entries": "An incomplete
4091 structure, union or class type is represented by a structure,
4092 union or class entry that does not have a byte size attribute
4093 and that has a DW_AT_declaration attribute." */
4094 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4095 {
4096 if (built_actual_name)
4097 xfree (actual_name);
4098 return;
4099 }
fa4028e9 4100
63d06c5c
DC
4101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4102 static vs. global. */
38d518c9 4103 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4104 built_actual_name,
176620f1 4105 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4106 (cu->language == language_cplus
4107 || cu->language == language_java)
63d06c5c
DC
4108 ? &objfile->global_psymbols
4109 : &objfile->static_psymbols,
e142c38c 4110 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4111
c906108c
SS
4112 break;
4113 case DW_TAG_enumerator:
38d518c9 4114 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4115 built_actual_name,
176620f1 4116 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4117 (cu->language == language_cplus
4118 || cu->language == language_java)
f6fe98ef
DJ
4119 ? &objfile->global_psymbols
4120 : &objfile->static_psymbols,
e142c38c 4121 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4122 break;
4123 default:
4124 break;
4125 }
5c4e30ca 4126
72bf9492
DJ
4127 if (built_actual_name)
4128 xfree (actual_name);
c906108c
SS
4129}
4130
5c4e30ca
DC
4131/* Read a partial die corresponding to a namespace; also, add a symbol
4132 corresponding to that namespace to the symbol table. NAMESPACE is
4133 the name of the enclosing namespace. */
91c24f0a 4134
72bf9492
DJ
4135static void
4136add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4137 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4138 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4139{
72bf9492 4140 /* Add a symbol for the namespace. */
e7c27a73 4141
72bf9492 4142 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4143
4144 /* Now scan partial symbols in that namespace. */
4145
91c24f0a 4146 if (pdi->has_children)
5734ee8b 4147 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4148}
4149
5d7cb8df
JK
4150/* Read a partial die corresponding to a Fortran module. */
4151
4152static void
4153add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4155{
f55ee35c 4156 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4157
4158 if (pdi->has_children)
4159 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4160}
4161
bc30ff58
JB
4162/* Read a partial die corresponding to a subprogram and create a partial
4163 symbol for that subprogram. When the CU language allows it, this
4164 routine also defines a partial symbol for each nested subprogram
4165 that this subprogram contains.
6e70227d 4166
bc30ff58
JB
4167 DIE my also be a lexical block, in which case we simply search
4168 recursively for suprograms defined inside that lexical block.
4169 Again, this is only performed when the CU language allows this
4170 type of definitions. */
4171
4172static void
4173add_partial_subprogram (struct partial_die_info *pdi,
4174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4175 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4176{
4177 if (pdi->tag == DW_TAG_subprogram)
4178 {
4179 if (pdi->has_pc_info)
4180 {
4181 if (pdi->lowpc < *lowpc)
4182 *lowpc = pdi->lowpc;
4183 if (pdi->highpc > *highpc)
4184 *highpc = pdi->highpc;
5734ee8b
DJ
4185 if (need_pc)
4186 {
4187 CORE_ADDR baseaddr;
4188 struct objfile *objfile = cu->objfile;
4189
4190 baseaddr = ANOFFSET (objfile->section_offsets,
4191 SECT_OFF_TEXT (objfile));
4192 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4193 pdi->lowpc + baseaddr,
4194 pdi->highpc - 1 + baseaddr,
9291a0cd 4195 cu->per_cu->v.psymtab);
5734ee8b 4196 }
bc30ff58 4197 if (!pdi->is_declaration)
e8d05480
JB
4198 /* Ignore subprogram DIEs that do not have a name, they are
4199 illegal. Do not emit a complaint at this point, we will
4200 do so when we convert this psymtab into a symtab. */
4201 if (pdi->name)
4202 add_partial_symbol (pdi, cu);
bc30ff58
JB
4203 }
4204 }
6e70227d 4205
bc30ff58
JB
4206 if (! pdi->has_children)
4207 return;
4208
4209 if (cu->language == language_ada)
4210 {
4211 pdi = pdi->die_child;
4212 while (pdi != NULL)
4213 {
4214 fixup_partial_die (pdi, cu);
4215 if (pdi->tag == DW_TAG_subprogram
4216 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4217 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4218 pdi = pdi->die_sibling;
4219 }
4220 }
4221}
4222
91c24f0a
DC
4223/* Read a partial die corresponding to an enumeration type. */
4224
72bf9492
DJ
4225static void
4226add_partial_enumeration (struct partial_die_info *enum_pdi,
4227 struct dwarf2_cu *cu)
91c24f0a 4228{
72bf9492 4229 struct partial_die_info *pdi;
91c24f0a
DC
4230
4231 if (enum_pdi->name != NULL)
72bf9492
DJ
4232 add_partial_symbol (enum_pdi, cu);
4233
4234 pdi = enum_pdi->die_child;
4235 while (pdi)
91c24f0a 4236 {
72bf9492 4237 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4238 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4239 else
72bf9492
DJ
4240 add_partial_symbol (pdi, cu);
4241 pdi = pdi->die_sibling;
91c24f0a 4242 }
91c24f0a
DC
4243}
4244
6caca83c
CC
4245/* Return the initial uleb128 in the die at INFO_PTR. */
4246
4247static unsigned int
4248peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4249{
4250 unsigned int bytes_read;
4251
4252 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4253}
4254
4bb7a0a7
DJ
4255/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4256 Return the corresponding abbrev, or NULL if the number is zero (indicating
4257 an empty DIE). In either case *BYTES_READ will be set to the length of
4258 the initial number. */
4259
4260static struct abbrev_info *
fe1b8b76 4261peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4262 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4263{
4264 bfd *abfd = cu->objfile->obfd;
4265 unsigned int abbrev_number;
4266 struct abbrev_info *abbrev;
4267
4268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4269
4270 if (abbrev_number == 0)
4271 return NULL;
4272
4273 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4274 if (!abbrev)
4275 {
3e43a32a
MS
4276 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4277 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4278 }
4279
4280 return abbrev;
4281}
4282
93311388
DE
4283/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4284 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4285 DIE. Any children of the skipped DIEs will also be skipped. */
4286
fe1b8b76 4287static gdb_byte *
93311388 4288skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4289{
4290 struct abbrev_info *abbrev;
4291 unsigned int bytes_read;
4292
4293 while (1)
4294 {
4295 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4296 if (abbrev == NULL)
4297 return info_ptr + bytes_read;
4298 else
93311388 4299 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4300 }
4301}
4302
93311388
DE
4303/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4304 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4305 abbrev corresponding to that skipped uleb128 should be passed in
4306 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4307 children. */
4308
fe1b8b76 4309static gdb_byte *
93311388
DE
4310skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4311 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4312{
4313 unsigned int bytes_read;
4314 struct attribute attr;
4315 bfd *abfd = cu->objfile->obfd;
4316 unsigned int form, i;
4317
4318 for (i = 0; i < abbrev->num_attrs; i++)
4319 {
4320 /* The only abbrev we care about is DW_AT_sibling. */
4321 if (abbrev->attrs[i].name == DW_AT_sibling)
4322 {
4323 read_attribute (&attr, &abbrev->attrs[i],
4324 abfd, info_ptr, cu);
4325 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4326 complaint (&symfile_complaints,
4327 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4328 else
93311388 4329 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4330 }
4331
4332 /* If it isn't DW_AT_sibling, skip this attribute. */
4333 form = abbrev->attrs[i].form;
4334 skip_attribute:
4335 switch (form)
4336 {
4bb7a0a7 4337 case DW_FORM_ref_addr:
ae411497
TT
4338 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4339 and later it is offset sized. */
4340 if (cu->header.version == 2)
4341 info_ptr += cu->header.addr_size;
4342 else
4343 info_ptr += cu->header.offset_size;
4344 break;
4345 case DW_FORM_addr:
4bb7a0a7
DJ
4346 info_ptr += cu->header.addr_size;
4347 break;
4348 case DW_FORM_data1:
4349 case DW_FORM_ref1:
4350 case DW_FORM_flag:
4351 info_ptr += 1;
4352 break;
2dc7f7b3
TT
4353 case DW_FORM_flag_present:
4354 break;
4bb7a0a7
DJ
4355 case DW_FORM_data2:
4356 case DW_FORM_ref2:
4357 info_ptr += 2;
4358 break;
4359 case DW_FORM_data4:
4360 case DW_FORM_ref4:
4361 info_ptr += 4;
4362 break;
4363 case DW_FORM_data8:
4364 case DW_FORM_ref8:
55f1336d 4365 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4366 info_ptr += 8;
4367 break;
4368 case DW_FORM_string:
9b1c24c8 4369 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4370 info_ptr += bytes_read;
4371 break;
2dc7f7b3 4372 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4373 case DW_FORM_strp:
4374 info_ptr += cu->header.offset_size;
4375 break;
2dc7f7b3 4376 case DW_FORM_exprloc:
4bb7a0a7
DJ
4377 case DW_FORM_block:
4378 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4379 info_ptr += bytes_read;
4380 break;
4381 case DW_FORM_block1:
4382 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4383 break;
4384 case DW_FORM_block2:
4385 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4386 break;
4387 case DW_FORM_block4:
4388 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4389 break;
4390 case DW_FORM_sdata:
4391 case DW_FORM_udata:
4392 case DW_FORM_ref_udata:
4393 info_ptr = skip_leb128 (abfd, info_ptr);
4394 break;
4395 case DW_FORM_indirect:
4396 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4397 info_ptr += bytes_read;
4398 /* We need to continue parsing from here, so just go back to
4399 the top. */
4400 goto skip_attribute;
4401
4402 default:
3e43a32a
MS
4403 error (_("Dwarf Error: Cannot handle %s "
4404 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4405 dwarf_form_name (form),
4406 bfd_get_filename (abfd));
4407 }
4408 }
4409
4410 if (abbrev->has_children)
93311388 4411 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4412 else
4413 return info_ptr;
4414}
4415
93311388
DE
4416/* Locate ORIG_PDI's sibling.
4417 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4418 in BUFFER. */
91c24f0a 4419
fe1b8b76 4420static gdb_byte *
93311388
DE
4421locate_pdi_sibling (struct partial_die_info *orig_pdi,
4422 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4423 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4424{
4425 /* Do we know the sibling already? */
72bf9492 4426
91c24f0a
DC
4427 if (orig_pdi->sibling)
4428 return orig_pdi->sibling;
4429
4430 /* Are there any children to deal with? */
4431
4432 if (!orig_pdi->has_children)
4433 return info_ptr;
4434
4bb7a0a7 4435 /* Skip the children the long way. */
91c24f0a 4436
93311388 4437 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4438}
4439
c906108c
SS
4440/* Expand this partial symbol table into a full symbol table. */
4441
4442static void
fba45db2 4443dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4444{
c906108c
SS
4445 if (pst != NULL)
4446 {
4447 if (pst->readin)
4448 {
3e43a32a
MS
4449 warning (_("bug: psymtab for %s is already read in."),
4450 pst->filename);
c906108c
SS
4451 }
4452 else
4453 {
4454 if (info_verbose)
4455 {
3e43a32a
MS
4456 printf_filtered (_("Reading in symbols for %s..."),
4457 pst->filename);
c906108c
SS
4458 gdb_flush (gdb_stdout);
4459 }
4460
10b3939b
DJ
4461 /* Restore our global data. */
4462 dwarf2_per_objfile = objfile_data (pst->objfile,
4463 dwarf2_objfile_data_key);
4464
b2ab525c
KB
4465 /* If this psymtab is constructed from a debug-only objfile, the
4466 has_section_at_zero flag will not necessarily be correct. We
4467 can get the correct value for this flag by looking at the data
4468 associated with the (presumably stripped) associated objfile. */
4469 if (pst->objfile->separate_debug_objfile_backlink)
4470 {
4471 struct dwarf2_per_objfile *dpo_backlink
4472 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4473 dwarf2_objfile_data_key);
9a619af0 4474
b2ab525c
KB
4475 dwarf2_per_objfile->has_section_at_zero
4476 = dpo_backlink->has_section_at_zero;
4477 }
4478
98bfdba5
PA
4479 dwarf2_per_objfile->reading_partial_symbols = 0;
4480
c906108c
SS
4481 psymtab_to_symtab_1 (pst);
4482
4483 /* Finish up the debug error message. */
4484 if (info_verbose)
a3f17187 4485 printf_filtered (_("done.\n"));
c906108c
SS
4486 }
4487 }
4488}
9cdd5dbd
DE
4489\f
4490/* Reading in full CUs. */
c906108c 4491
10b3939b
DJ
4492/* Add PER_CU to the queue. */
4493
4494static void
a0f42c21 4495queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4496{
4497 struct dwarf2_queue_item *item;
4498
4499 per_cu->queued = 1;
4500 item = xmalloc (sizeof (*item));
4501 item->per_cu = per_cu;
4502 item->next = NULL;
4503
4504 if (dwarf2_queue == NULL)
4505 dwarf2_queue = item;
4506 else
4507 dwarf2_queue_tail->next = item;
4508
4509 dwarf2_queue_tail = item;
4510}
4511
4512/* Process the queue. */
4513
4514static void
a0f42c21 4515process_queue (void)
10b3939b
DJ
4516{
4517 struct dwarf2_queue_item *item, *next_item;
4518
03dd20cc
DJ
4519 /* The queue starts out with one item, but following a DIE reference
4520 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4521 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4522 {
9291a0cd
TT
4523 if (dwarf2_per_objfile->using_index
4524 ? !item->per_cu->v.quick->symtab
4525 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4526 process_full_comp_unit (item->per_cu);
4527
4528 item->per_cu->queued = 0;
4529 next_item = item->next;
4530 xfree (item);
4531 }
4532
4533 dwarf2_queue_tail = NULL;
4534}
4535
4536/* Free all allocated queue entries. This function only releases anything if
4537 an error was thrown; if the queue was processed then it would have been
4538 freed as we went along. */
4539
4540static void
4541dwarf2_release_queue (void *dummy)
4542{
4543 struct dwarf2_queue_item *item, *last;
4544
4545 item = dwarf2_queue;
4546 while (item)
4547 {
4548 /* Anything still marked queued is likely to be in an
4549 inconsistent state, so discard it. */
4550 if (item->per_cu->queued)
4551 {
4552 if (item->per_cu->cu != NULL)
4553 free_one_cached_comp_unit (item->per_cu->cu);
4554 item->per_cu->queued = 0;
4555 }
4556
4557 last = item;
4558 item = item->next;
4559 xfree (last);
4560 }
4561
4562 dwarf2_queue = dwarf2_queue_tail = NULL;
4563}
4564
4565/* Read in full symbols for PST, and anything it depends on. */
4566
c906108c 4567static void
fba45db2 4568psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4569{
10b3939b 4570 struct dwarf2_per_cu_data *per_cu;
c906108c 4571 struct cleanup *back_to;
aaa75496
JB
4572 int i;
4573
4574 for (i = 0; i < pst->number_of_dependencies; i++)
4575 if (!pst->dependencies[i]->readin)
4576 {
4577 /* Inform about additional files that need to be read in. */
4578 if (info_verbose)
4579 {
a3f17187 4580 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4581 fputs_filtered (" ", gdb_stdout);
4582 wrap_here ("");
4583 fputs_filtered ("and ", gdb_stdout);
4584 wrap_here ("");
4585 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4586 wrap_here (""); /* Flush output. */
aaa75496
JB
4587 gdb_flush (gdb_stdout);
4588 }
4589 psymtab_to_symtab_1 (pst->dependencies[i]);
4590 }
4591
e38df1d0 4592 per_cu = pst->read_symtab_private;
10b3939b
DJ
4593
4594 if (per_cu == NULL)
aaa75496
JB
4595 {
4596 /* It's an include file, no symbols to read for it.
4597 Everything is in the parent symtab. */
4598 pst->readin = 1;
4599 return;
4600 }
c906108c 4601
a0f42c21 4602 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4603}
4604
93311388 4605/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4606
93311388 4607static void
a0f42c21 4608load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4609{
a0f42c21 4610 struct objfile *objfile = per_cu->objfile;
31ffec48 4611 bfd *abfd = objfile->obfd;
10b3939b 4612 struct dwarf2_cu *cu;
c764a876 4613 unsigned int offset;
93311388 4614 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4615 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4616 struct attribute *attr;
98bfdba5 4617 int read_cu = 0;
6502dd73 4618
b0df02fd 4619 gdb_assert (! per_cu->debug_types_section);
348e048f 4620
c906108c 4621 /* Set local variables from the partial symbol table info. */
10b3939b 4622 offset = per_cu->offset;
6502dd73 4623
be391dca 4624 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4625 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4626 beg_of_comp_unit = info_ptr;
63d06c5c 4627
98bfdba5
PA
4628 if (per_cu->cu == NULL)
4629 {
9816fde3 4630 cu = xmalloc (sizeof (*cu));
23745b47 4631 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4632
4633 read_cu = 1;
c906108c 4634
98bfdba5 4635 /* If an error occurs while loading, release our storage. */
68dc6402 4636 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4637
98bfdba5
PA
4638 /* Read in the comp_unit header. */
4639 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4640
6caca83c
CC
4641 /* Skip dummy compilation units. */
4642 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4643 + dwarf2_per_objfile->info.size)
4644 || peek_abbrev_code (abfd, info_ptr) == 0)
4645 {
4646 do_cleanups (free_cu_cleanup);
4647 return;
4648 }
4649
98bfdba5
PA
4650 /* Complete the cu_header. */
4651 cu->header.offset = offset;
4652 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4653
98bfdba5 4654 /* Read the abbrevs for this compilation unit. */
e5fe5e75 4655 dwarf2_read_abbrevs (cu);
98bfdba5 4656 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4657
98bfdba5
PA
4658 /* Link this CU into read_in_chain. */
4659 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4660 dwarf2_per_objfile->read_in_chain = per_cu;
4661 }
4662 else
4663 {
4664 cu = per_cu->cu;
4665 info_ptr += cu->header.first_die_offset;
4666 }
e142c38c 4667
93311388 4668 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4669
4670 /* We try not to read any attributes in this function, because not
9cdd5dbd 4671 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4672 table processing isn't initialized. But we have to set the CU language,
4673 or we won't be able to build types correctly. */
9816fde3 4674 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4675
a6c727b2
DJ
4676 /* Similarly, if we do not read the producer, we can not apply
4677 producer-specific interpretation. */
4678 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4679 if (attr)
4680 cu->producer = DW_STRING (attr);
4681
98bfdba5
PA
4682 if (read_cu)
4683 {
4684 do_cleanups (free_abbrevs_cleanup);
e142c38c 4685
98bfdba5
PA
4686 /* We've successfully allocated this compilation unit. Let our
4687 caller clean it up when finished with it. */
4688 discard_cleanups (free_cu_cleanup);
4689 }
10b3939b
DJ
4690}
4691
3da10d80
KS
4692/* Add a DIE to the delayed physname list. */
4693
4694static void
4695add_to_method_list (struct type *type, int fnfield_index, int index,
4696 const char *name, struct die_info *die,
4697 struct dwarf2_cu *cu)
4698{
4699 struct delayed_method_info mi;
4700 mi.type = type;
4701 mi.fnfield_index = fnfield_index;
4702 mi.index = index;
4703 mi.name = name;
4704 mi.die = die;
4705 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4706}
4707
4708/* A cleanup for freeing the delayed method list. */
4709
4710static void
4711free_delayed_list (void *ptr)
4712{
4713 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4714 if (cu->method_list != NULL)
4715 {
4716 VEC_free (delayed_method_info, cu->method_list);
4717 cu->method_list = NULL;
4718 }
4719}
4720
4721/* Compute the physnames of any methods on the CU's method list.
4722
4723 The computation of method physnames is delayed in order to avoid the
4724 (bad) condition that one of the method's formal parameters is of an as yet
4725 incomplete type. */
4726
4727static void
4728compute_delayed_physnames (struct dwarf2_cu *cu)
4729{
4730 int i;
4731 struct delayed_method_info *mi;
4732 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4733 {
1d06ead6 4734 const char *physname;
3da10d80
KS
4735 struct fn_fieldlist *fn_flp
4736 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4737 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4738 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4739 }
4740}
4741
9cdd5dbd 4742/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4743 already been loaded into memory. */
4744
4745static void
4746process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4747{
10b3939b 4748 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4749 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4750 CORE_ADDR lowpc, highpc;
4751 struct symtab *symtab;
3da10d80 4752 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4753 CORE_ADDR baseaddr;
4754
4755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4756
10b3939b
DJ
4757 buildsym_init ();
4758 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4759 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4760
4761 cu->list_in_scope = &file_symbols;
c906108c
SS
4762
4763 /* Do line number decoding in read_file_scope () */
10b3939b 4764 process_die (cu->dies, cu);
c906108c 4765
3da10d80
KS
4766 /* Now that we have processed all the DIEs in the CU, all the types
4767 should be complete, and it should now be safe to compute all of the
4768 physnames. */
4769 compute_delayed_physnames (cu);
4770 do_cleanups (delayed_list_cleanup);
4771
fae299cd
DC
4772 /* Some compilers don't define a DW_AT_high_pc attribute for the
4773 compilation unit. If the DW_AT_high_pc is missing, synthesize
4774 it, by scanning the DIE's below the compilation unit. */
10b3939b 4775 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4776
613e1657 4777 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4778
8be455d7 4779 if (symtab != NULL)
c906108c 4780 {
df15bd07 4781 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4782
8be455d7
JK
4783 /* Set symtab language to language from DW_AT_language. If the
4784 compilation is from a C file generated by language preprocessors, do
4785 not set the language if it was already deduced by start_subfile. */
4786 if (!(cu->language == language_c && symtab->language != language_c))
4787 symtab->language = cu->language;
4788
4789 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4790 produce DW_AT_location with location lists but it can be possibly
4791 invalid without -fvar-tracking.
4792
4793 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4794 needed, it would be wrong due to missing DW_AT_producer there.
4795
4796 Still one can confuse GDB by using non-standard GCC compilation
4797 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4798 */
4632c0d0 4799 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4800 symtab->locations_valid = 1;
e0d00bc7
JK
4801
4802 if (gcc_4_minor >= 5)
4803 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4804
4805 symtab->call_site_htab = cu->call_site_htab;
c906108c 4806 }
9291a0cd
TT
4807
4808 if (dwarf2_per_objfile->using_index)
4809 per_cu->v.quick->symtab = symtab;
4810 else
4811 {
4812 struct partial_symtab *pst = per_cu->v.psymtab;
4813 pst->symtab = symtab;
4814 pst->readin = 1;
4815 }
c906108c
SS
4816
4817 do_cleanups (back_to);
4818}
4819
4820/* Process a die and its children. */
4821
4822static void
e7c27a73 4823process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4824{
4825 switch (die->tag)
4826 {
4827 case DW_TAG_padding:
4828 break;
4829 case DW_TAG_compile_unit:
e7c27a73 4830 read_file_scope (die, cu);
c906108c 4831 break;
348e048f
DE
4832 case DW_TAG_type_unit:
4833 read_type_unit_scope (die, cu);
4834 break;
c906108c 4835 case DW_TAG_subprogram:
c906108c 4836 case DW_TAG_inlined_subroutine:
edb3359d 4837 read_func_scope (die, cu);
c906108c
SS
4838 break;
4839 case DW_TAG_lexical_block:
14898363
L
4840 case DW_TAG_try_block:
4841 case DW_TAG_catch_block:
e7c27a73 4842 read_lexical_block_scope (die, cu);
c906108c 4843 break;
96408a79
SA
4844 case DW_TAG_GNU_call_site:
4845 read_call_site_scope (die, cu);
4846 break;
c906108c 4847 case DW_TAG_class_type:
680b30c7 4848 case DW_TAG_interface_type:
c906108c
SS
4849 case DW_TAG_structure_type:
4850 case DW_TAG_union_type:
134d01f1 4851 process_structure_scope (die, cu);
c906108c
SS
4852 break;
4853 case DW_TAG_enumeration_type:
134d01f1 4854 process_enumeration_scope (die, cu);
c906108c 4855 break;
134d01f1 4856
f792889a
DJ
4857 /* These dies have a type, but processing them does not create
4858 a symbol or recurse to process the children. Therefore we can
4859 read them on-demand through read_type_die. */
c906108c 4860 case DW_TAG_subroutine_type:
72019c9c 4861 case DW_TAG_set_type:
c906108c 4862 case DW_TAG_array_type:
c906108c 4863 case DW_TAG_pointer_type:
c906108c 4864 case DW_TAG_ptr_to_member_type:
c906108c 4865 case DW_TAG_reference_type:
c906108c 4866 case DW_TAG_string_type:
c906108c 4867 break;
134d01f1 4868
c906108c 4869 case DW_TAG_base_type:
a02abb62 4870 case DW_TAG_subrange_type:
cb249c71 4871 case DW_TAG_typedef:
134d01f1
DJ
4872 /* Add a typedef symbol for the type definition, if it has a
4873 DW_AT_name. */
f792889a 4874 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4875 break;
c906108c 4876 case DW_TAG_common_block:
e7c27a73 4877 read_common_block (die, cu);
c906108c
SS
4878 break;
4879 case DW_TAG_common_inclusion:
4880 break;
d9fa45fe 4881 case DW_TAG_namespace:
63d06c5c 4882 processing_has_namespace_info = 1;
e7c27a73 4883 read_namespace (die, cu);
d9fa45fe 4884 break;
5d7cb8df 4885 case DW_TAG_module:
f55ee35c 4886 processing_has_namespace_info = 1;
5d7cb8df
JK
4887 read_module (die, cu);
4888 break;
d9fa45fe
DC
4889 case DW_TAG_imported_declaration:
4890 case DW_TAG_imported_module:
63d06c5c 4891 processing_has_namespace_info = 1;
27aa8d6a
SW
4892 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4893 || cu->language != language_fortran))
4894 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4895 dwarf_tag_name (die->tag));
4896 read_import_statement (die, cu);
d9fa45fe 4897 break;
c906108c 4898 default:
e7c27a73 4899 new_symbol (die, NULL, cu);
c906108c
SS
4900 break;
4901 }
4902}
4903
94af9270
KS
4904/* A helper function for dwarf2_compute_name which determines whether DIE
4905 needs to have the name of the scope prepended to the name listed in the
4906 die. */
4907
4908static int
4909die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4910{
1c809c68
TT
4911 struct attribute *attr;
4912
94af9270
KS
4913 switch (die->tag)
4914 {
4915 case DW_TAG_namespace:
4916 case DW_TAG_typedef:
4917 case DW_TAG_class_type:
4918 case DW_TAG_interface_type:
4919 case DW_TAG_structure_type:
4920 case DW_TAG_union_type:
4921 case DW_TAG_enumeration_type:
4922 case DW_TAG_enumerator:
4923 case DW_TAG_subprogram:
4924 case DW_TAG_member:
4925 return 1;
4926
4927 case DW_TAG_variable:
c2b0a229 4928 case DW_TAG_constant:
94af9270
KS
4929 /* We only need to prefix "globally" visible variables. These include
4930 any variable marked with DW_AT_external or any variable that
4931 lives in a namespace. [Variables in anonymous namespaces
4932 require prefixing, but they are not DW_AT_external.] */
4933
4934 if (dwarf2_attr (die, DW_AT_specification, cu))
4935 {
4936 struct dwarf2_cu *spec_cu = cu;
9a619af0 4937
94af9270
KS
4938 return die_needs_namespace (die_specification (die, &spec_cu),
4939 spec_cu);
4940 }
4941
1c809c68 4942 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4943 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4944 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4945 return 0;
4946 /* A variable in a lexical block of some kind does not need a
4947 namespace, even though in C++ such variables may be external
4948 and have a mangled name. */
4949 if (die->parent->tag == DW_TAG_lexical_block
4950 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4951 || die->parent->tag == DW_TAG_catch_block
4952 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4953 return 0;
4954 return 1;
94af9270
KS
4955
4956 default:
4957 return 0;
4958 }
4959}
4960
98bfdba5
PA
4961/* Retrieve the last character from a mem_file. */
4962
4963static void
4964do_ui_file_peek_last (void *object, const char *buffer, long length)
4965{
4966 char *last_char_p = (char *) object;
4967
4968 if (length > 0)
4969 *last_char_p = buffer[length - 1];
4970}
4971
94af9270
KS
4972/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4973 compute the physname for the object, which include a method's
4974 formal parameters (C++/Java) and return type (Java).
4975
af6b7be1
JB
4976 For Ada, return the DIE's linkage name rather than the fully qualified
4977 name. PHYSNAME is ignored..
4978
94af9270
KS
4979 The result is allocated on the objfile_obstack and canonicalized. */
4980
4981static const char *
4982dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4983 int physname)
4984{
bb5ed363
DE
4985 struct objfile *objfile = cu->objfile;
4986
94af9270
KS
4987 if (name == NULL)
4988 name = dwarf2_name (die, cu);
4989
f55ee35c
JK
4990 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4991 compute it by typename_concat inside GDB. */
4992 if (cu->language == language_ada
4993 || (cu->language == language_fortran && physname))
4994 {
4995 /* For Ada unit, we prefer the linkage name over the name, as
4996 the former contains the exported name, which the user expects
4997 to be able to reference. Ideally, we want the user to be able
4998 to reference this entity using either natural or linkage name,
4999 but we haven't started looking at this enhancement yet. */
5000 struct attribute *attr;
5001
5002 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5003 if (attr == NULL)
5004 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5005 if (attr && DW_STRING (attr))
5006 return DW_STRING (attr);
5007 }
5008
94af9270
KS
5009 /* These are the only languages we know how to qualify names in. */
5010 if (name != NULL
f55ee35c
JK
5011 && (cu->language == language_cplus || cu->language == language_java
5012 || cu->language == language_fortran))
94af9270
KS
5013 {
5014 if (die_needs_namespace (die, cu))
5015 {
5016 long length;
5017 char *prefix;
5018 struct ui_file *buf;
5019
5020 prefix = determine_prefix (die, cu);
5021 buf = mem_fileopen ();
5022 if (*prefix != '\0')
5023 {
f55ee35c
JK
5024 char *prefixed_name = typename_concat (NULL, prefix, name,
5025 physname, cu);
9a619af0 5026
94af9270
KS
5027 fputs_unfiltered (prefixed_name, buf);
5028 xfree (prefixed_name);
5029 }
5030 else
62d5b8da 5031 fputs_unfiltered (name, buf);
94af9270 5032
98bfdba5
PA
5033 /* Template parameters may be specified in the DIE's DW_AT_name, or
5034 as children with DW_TAG_template_type_param or
5035 DW_TAG_value_type_param. If the latter, add them to the name
5036 here. If the name already has template parameters, then
5037 skip this step; some versions of GCC emit both, and
5038 it is more efficient to use the pre-computed name.
5039
5040 Something to keep in mind about this process: it is very
5041 unlikely, or in some cases downright impossible, to produce
5042 something that will match the mangled name of a function.
5043 If the definition of the function has the same debug info,
5044 we should be able to match up with it anyway. But fallbacks
5045 using the minimal symbol, for instance to find a method
5046 implemented in a stripped copy of libstdc++, will not work.
5047 If we do not have debug info for the definition, we will have to
5048 match them up some other way.
5049
5050 When we do name matching there is a related problem with function
5051 templates; two instantiated function templates are allowed to
5052 differ only by their return types, which we do not add here. */
5053
5054 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5055 {
5056 struct attribute *attr;
5057 struct die_info *child;
5058 int first = 1;
5059
5060 die->building_fullname = 1;
5061
5062 for (child = die->child; child != NULL; child = child->sibling)
5063 {
5064 struct type *type;
5065 long value;
5066 gdb_byte *bytes;
5067 struct dwarf2_locexpr_baton *baton;
5068 struct value *v;
5069
5070 if (child->tag != DW_TAG_template_type_param
5071 && child->tag != DW_TAG_template_value_param)
5072 continue;
5073
5074 if (first)
5075 {
5076 fputs_unfiltered ("<", buf);
5077 first = 0;
5078 }
5079 else
5080 fputs_unfiltered (", ", buf);
5081
5082 attr = dwarf2_attr (child, DW_AT_type, cu);
5083 if (attr == NULL)
5084 {
5085 complaint (&symfile_complaints,
5086 _("template parameter missing DW_AT_type"));
5087 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5088 continue;
5089 }
5090 type = die_type (child, cu);
5091
5092 if (child->tag == DW_TAG_template_type_param)
5093 {
5094 c_print_type (type, "", buf, -1, 0);
5095 continue;
5096 }
5097
5098 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5099 if (attr == NULL)
5100 {
5101 complaint (&symfile_complaints,
3e43a32a
MS
5102 _("template parameter missing "
5103 "DW_AT_const_value"));
98bfdba5
PA
5104 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5105 continue;
5106 }
5107
5108 dwarf2_const_value_attr (attr, type, name,
5109 &cu->comp_unit_obstack, cu,
5110 &value, &bytes, &baton);
5111
5112 if (TYPE_NOSIGN (type))
5113 /* GDB prints characters as NUMBER 'CHAR'. If that's
5114 changed, this can use value_print instead. */
5115 c_printchar (value, type, buf);
5116 else
5117 {
5118 struct value_print_options opts;
5119
5120 if (baton != NULL)
5121 v = dwarf2_evaluate_loc_desc (type, NULL,
5122 baton->data,
5123 baton->size,
5124 baton->per_cu);
5125 else if (bytes != NULL)
5126 {
5127 v = allocate_value (type);
5128 memcpy (value_contents_writeable (v), bytes,
5129 TYPE_LENGTH (type));
5130 }
5131 else
5132 v = value_from_longest (type, value);
5133
3e43a32a
MS
5134 /* Specify decimal so that we do not depend on
5135 the radix. */
98bfdba5
PA
5136 get_formatted_print_options (&opts, 'd');
5137 opts.raw = 1;
5138 value_print (v, buf, &opts);
5139 release_value (v);
5140 value_free (v);
5141 }
5142 }
5143
5144 die->building_fullname = 0;
5145
5146 if (!first)
5147 {
5148 /* Close the argument list, with a space if necessary
5149 (nested templates). */
5150 char last_char = '\0';
5151 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5152 if (last_char == '>')
5153 fputs_unfiltered (" >", buf);
5154 else
5155 fputs_unfiltered (">", buf);
5156 }
5157 }
5158
94af9270
KS
5159 /* For Java and C++ methods, append formal parameter type
5160 information, if PHYSNAME. */
6e70227d 5161
94af9270
KS
5162 if (physname && die->tag == DW_TAG_subprogram
5163 && (cu->language == language_cplus
5164 || cu->language == language_java))
5165 {
5166 struct type *type = read_type_die (die, cu);
5167
3167638f 5168 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5169
5170 if (cu->language == language_java)
5171 {
5172 /* For java, we must append the return type to method
0963b4bd 5173 names. */
94af9270
KS
5174 if (die->tag == DW_TAG_subprogram)
5175 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5176 0, 0);
5177 }
5178 else if (cu->language == language_cplus)
5179 {
60430eff
DJ
5180 /* Assume that an artificial first parameter is
5181 "this", but do not crash if it is not. RealView
5182 marks unnamed (and thus unused) parameters as
5183 artificial; there is no way to differentiate
5184 the two cases. */
94af9270
KS
5185 if (TYPE_NFIELDS (type) > 0
5186 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5187 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5188 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5189 0))))
94af9270
KS
5190 fputs_unfiltered (" const", buf);
5191 }
5192 }
5193
bb5ed363 5194 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5195 &length);
5196 ui_file_delete (buf);
5197
5198 if (cu->language == language_cplus)
5199 {
5200 char *cname
5201 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5202 &objfile->objfile_obstack);
9a619af0 5203
94af9270
KS
5204 if (cname != NULL)
5205 name = cname;
5206 }
5207 }
5208 }
5209
5210 return name;
5211}
5212
0114d602
DJ
5213/* Return the fully qualified name of DIE, based on its DW_AT_name.
5214 If scope qualifiers are appropriate they will be added. The result
5215 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5216 not have a name. NAME may either be from a previous call to
5217 dwarf2_name or NULL.
5218
0963b4bd 5219 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5220
5221static const char *
94af9270 5222dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5223{
94af9270
KS
5224 return dwarf2_compute_name (name, die, cu, 0);
5225}
0114d602 5226
94af9270
KS
5227/* Construct a physname for the given DIE in CU. NAME may either be
5228 from a previous call to dwarf2_name or NULL. The result will be
5229 allocated on the objfile_objstack or NULL if the DIE does not have a
5230 name.
0114d602 5231
94af9270 5232 The output string will be canonicalized (if C++/Java). */
0114d602 5233
94af9270
KS
5234static const char *
5235dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5236{
bb5ed363 5237 struct objfile *objfile = cu->objfile;
900e11f9
JK
5238 struct attribute *attr;
5239 const char *retval, *mangled = NULL, *canon = NULL;
5240 struct cleanup *back_to;
5241 int need_copy = 1;
5242
5243 /* In this case dwarf2_compute_name is just a shortcut not building anything
5244 on its own. */
5245 if (!die_needs_namespace (die, cu))
5246 return dwarf2_compute_name (name, die, cu, 1);
5247
5248 back_to = make_cleanup (null_cleanup, NULL);
5249
5250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5251 if (!attr)
5252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5253
5254 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5255 has computed. */
5256 if (attr && DW_STRING (attr))
5257 {
5258 char *demangled;
5259
5260 mangled = DW_STRING (attr);
5261
5262 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5263 type. It is easier for GDB users to search for such functions as
5264 `name(params)' than `long name(params)'. In such case the minimal
5265 symbol names do not match the full symbol names but for template
5266 functions there is never a need to look up their definition from their
5267 declaration so the only disadvantage remains the minimal symbol
5268 variant `long name(params)' does not have the proper inferior type.
5269 */
5270
5271 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5272 | (cu->language == language_java
5273 ? DMGL_JAVA | DMGL_RET_POSTFIX
5274 : DMGL_RET_DROP)));
5275 if (demangled)
5276 {
5277 make_cleanup (xfree, demangled);
5278 canon = demangled;
5279 }
5280 else
5281 {
5282 canon = mangled;
5283 need_copy = 0;
5284 }
5285 }
5286
5287 if (canon == NULL || check_physname)
5288 {
5289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5290
5291 if (canon != NULL && strcmp (physname, canon) != 0)
5292 {
5293 /* It may not mean a bug in GDB. The compiler could also
5294 compute DW_AT_linkage_name incorrectly. But in such case
5295 GDB would need to be bug-to-bug compatible. */
5296
5297 complaint (&symfile_complaints,
5298 _("Computed physname <%s> does not match demangled <%s> "
5299 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
bb5ed363 5300 physname, canon, mangled, die->offset, objfile->name);
900e11f9
JK
5301
5302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5303 is available here - over computed PHYSNAME. It is safer
5304 against both buggy GDB and buggy compilers. */
5305
5306 retval = canon;
5307 }
5308 else
5309 {
5310 retval = physname;
5311 need_copy = 0;
5312 }
5313 }
5314 else
5315 retval = canon;
5316
5317 if (need_copy)
5318 retval = obsavestring (retval, strlen (retval),
bb5ed363 5319 &objfile->objfile_obstack);
900e11f9
JK
5320
5321 do_cleanups (back_to);
5322 return retval;
0114d602
DJ
5323}
5324
27aa8d6a
SW
5325/* Read the import statement specified by the given die and record it. */
5326
5327static void
5328read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5329{
bb5ed363 5330 struct objfile *objfile = cu->objfile;
27aa8d6a 5331 struct attribute *import_attr;
32019081 5332 struct die_info *imported_die, *child_die;
de4affc9 5333 struct dwarf2_cu *imported_cu;
27aa8d6a 5334 const char *imported_name;
794684b6 5335 const char *imported_name_prefix;
13387711
SW
5336 const char *canonical_name;
5337 const char *import_alias;
5338 const char *imported_declaration = NULL;
794684b6 5339 const char *import_prefix;
32019081
JK
5340 VEC (const_char_ptr) *excludes = NULL;
5341 struct cleanup *cleanups;
13387711
SW
5342
5343 char *temp;
27aa8d6a
SW
5344
5345 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5346 if (import_attr == NULL)
5347 {
5348 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5349 dwarf_tag_name (die->tag));
5350 return;
5351 }
5352
de4affc9
CC
5353 imported_cu = cu;
5354 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5355 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5356 if (imported_name == NULL)
5357 {
5358 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5359
5360 The import in the following code:
5361 namespace A
5362 {
5363 typedef int B;
5364 }
5365
5366 int main ()
5367 {
5368 using A::B;
5369 B b;
5370 return b;
5371 }
5372
5373 ...
5374 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5375 <52> DW_AT_decl_file : 1
5376 <53> DW_AT_decl_line : 6
5377 <54> DW_AT_import : <0x75>
5378 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5379 <59> DW_AT_name : B
5380 <5b> DW_AT_decl_file : 1
5381 <5c> DW_AT_decl_line : 2
5382 <5d> DW_AT_type : <0x6e>
5383 ...
5384 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5385 <76> DW_AT_byte_size : 4
5386 <77> DW_AT_encoding : 5 (signed)
5387
5388 imports the wrong die ( 0x75 instead of 0x58 ).
5389 This case will be ignored until the gcc bug is fixed. */
5390 return;
5391 }
5392
82856980
SW
5393 /* Figure out the local name after import. */
5394 import_alias = dwarf2_name (die, cu);
27aa8d6a 5395
794684b6
SW
5396 /* Figure out where the statement is being imported to. */
5397 import_prefix = determine_prefix (die, cu);
5398
5399 /* Figure out what the scope of the imported die is and prepend it
5400 to the name of the imported die. */
de4affc9 5401 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5402
f55ee35c
JK
5403 if (imported_die->tag != DW_TAG_namespace
5404 && imported_die->tag != DW_TAG_module)
794684b6 5405 {
13387711
SW
5406 imported_declaration = imported_name;
5407 canonical_name = imported_name_prefix;
794684b6 5408 }
13387711 5409 else if (strlen (imported_name_prefix) > 0)
794684b6 5410 {
13387711
SW
5411 temp = alloca (strlen (imported_name_prefix)
5412 + 2 + strlen (imported_name) + 1);
5413 strcpy (temp, imported_name_prefix);
5414 strcat (temp, "::");
5415 strcat (temp, imported_name);
5416 canonical_name = temp;
794684b6 5417 }
13387711
SW
5418 else
5419 canonical_name = imported_name;
794684b6 5420
32019081
JK
5421 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5422
5423 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5424 for (child_die = die->child; child_die && child_die->tag;
5425 child_die = sibling_die (child_die))
5426 {
5427 /* DWARF-4: A Fortran use statement with a “rename list” may be
5428 represented by an imported module entry with an import attribute
5429 referring to the module and owned entries corresponding to those
5430 entities that are renamed as part of being imported. */
5431
5432 if (child_die->tag != DW_TAG_imported_declaration)
5433 {
5434 complaint (&symfile_complaints,
5435 _("child DW_TAG_imported_declaration expected "
5436 "- DIE at 0x%x [in module %s]"),
bb5ed363 5437 child_die->offset, objfile->name);
32019081
JK
5438 continue;
5439 }
5440
5441 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5442 if (import_attr == NULL)
5443 {
5444 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5445 dwarf_tag_name (child_die->tag));
5446 continue;
5447 }
5448
5449 imported_cu = cu;
5450 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5451 &imported_cu);
5452 imported_name = dwarf2_name (imported_die, imported_cu);
5453 if (imported_name == NULL)
5454 {
5455 complaint (&symfile_complaints,
5456 _("child DW_TAG_imported_declaration has unknown "
5457 "imported name - DIE at 0x%x [in module %s]"),
bb5ed363 5458 child_die->offset, objfile->name);
32019081
JK
5459 continue;
5460 }
5461
5462 VEC_safe_push (const_char_ptr, excludes, imported_name);
5463
5464 process_die (child_die, cu);
5465 }
5466
c0cc3a76
SW
5467 cp_add_using_directive (import_prefix,
5468 canonical_name,
5469 import_alias,
13387711 5470 imported_declaration,
32019081 5471 excludes,
bb5ed363 5472 &objfile->objfile_obstack);
32019081
JK
5473
5474 do_cleanups (cleanups);
27aa8d6a
SW
5475}
5476
ae2de4f8
DE
5477/* Cleanup function for read_file_scope. */
5478
cb1df416
DJ
5479static void
5480free_cu_line_header (void *arg)
5481{
5482 struct dwarf2_cu *cu = arg;
5483
5484 free_line_header (cu->line_header);
5485 cu->line_header = NULL;
5486}
5487
9291a0cd
TT
5488static void
5489find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5490 char **name, char **comp_dir)
5491{
5492 struct attribute *attr;
5493
5494 *name = NULL;
5495 *comp_dir = NULL;
5496
5497 /* Find the filename. Do not use dwarf2_name here, since the filename
5498 is not a source language identifier. */
5499 attr = dwarf2_attr (die, DW_AT_name, cu);
5500 if (attr)
5501 {
5502 *name = DW_STRING (attr);
5503 }
5504
5505 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5506 if (attr)
5507 *comp_dir = DW_STRING (attr);
5508 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5509 {
5510 *comp_dir = ldirname (*name);
5511 if (*comp_dir != NULL)
5512 make_cleanup (xfree, *comp_dir);
5513 }
5514 if (*comp_dir != NULL)
5515 {
5516 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5517 directory, get rid of it. */
5518 char *cp = strchr (*comp_dir, ':');
5519
5520 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5521 *comp_dir = cp + 1;
5522 }
5523
5524 if (*name == NULL)
5525 *name = "<unknown>";
5526}
5527
2ab95328
TT
5528/* Handle DW_AT_stmt_list for a compilation unit. */
5529
5530static void
5531handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5532 const char *comp_dir)
5533{
5534 struct attribute *attr;
5535 struct objfile *objfile = cu->objfile;
5536 bfd *abfd = objfile->obfd;
5537
5538 /* Decode line number information if present. We do this before
5539 processing child DIEs, so that the line header table is available
5540 for DW_AT_decl_file. */
5541 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5542 if (attr)
5543 {
5544 unsigned int line_offset = DW_UNSND (attr);
5545 struct line_header *line_header
5546 = dwarf_decode_line_header (line_offset, abfd, cu);
5547
5548 if (line_header)
5549 {
5550 cu->line_header = line_header;
5551 make_cleanup (free_cu_line_header, cu);
5552 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5553 }
5554 }
5555}
5556
ae2de4f8
DE
5557/* Process DW_TAG_compile_unit. */
5558
c906108c 5559static void
e7c27a73 5560read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5561{
e7c27a73 5562 struct objfile *objfile = cu->objfile;
debd256d 5563 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5564 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5565 CORE_ADDR highpc = ((CORE_ADDR) 0);
5566 struct attribute *attr;
e1024ff1 5567 char *name = NULL;
c906108c
SS
5568 char *comp_dir = NULL;
5569 struct die_info *child_die;
5570 bfd *abfd = objfile->obfd;
e142c38c 5571 CORE_ADDR baseaddr;
6e70227d 5572
e142c38c 5573 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5574
fae299cd 5575 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5576
5577 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5578 from finish_block. */
2acceee2 5579 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5580 lowpc = highpc;
5581 lowpc += baseaddr;
5582 highpc += baseaddr;
5583
9291a0cd 5584 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5585
e142c38c 5586 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5587 if (attr)
5588 {
e142c38c 5589 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5590 }
5591
b0f35d58 5592 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5593 if (attr)
b0f35d58 5594 cu->producer = DW_STRING (attr);
303b6f5d 5595
f4b8a18d
KW
5596 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5597 standardised yet. As a workaround for the language detection we fall
5598 back to the DW_AT_producer string. */
5599 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5600 cu->language = language_opencl;
5601
0963b4bd 5602 /* We assume that we're processing GCC output. */
c906108c 5603 processing_gcc_compilation = 2;
c906108c 5604
df8a16a1
DJ
5605 processing_has_namespace_info = 0;
5606
c906108c
SS
5607 start_symtab (name, comp_dir, lowpc);
5608 record_debugformat ("DWARF 2");
303b6f5d 5609 record_producer (cu->producer);
c906108c 5610
2ab95328 5611 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5612
cb1df416
DJ
5613 /* Process all dies in compilation unit. */
5614 if (die->child != NULL)
5615 {
5616 child_die = die->child;
5617 while (child_die && child_die->tag)
5618 {
5619 process_die (child_die, cu);
5620 child_die = sibling_die (child_die);
5621 }
5622 }
5623
2e276125
JB
5624 /* Decode macro information, if present. Dwarf 2 macro information
5625 refers to information in the line number info statement program
5626 header, so we can only read it if we've read the header
5627 successfully. */
cf2c3c16 5628 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5629 if (attr && cu->line_header)
2e276125 5630 {
cf2c3c16
TT
5631 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5632 complaint (&symfile_complaints,
5633 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5634
5635 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5636 comp_dir, abfd, cu,
5637 &dwarf2_per_objfile->macro, 1);
5638 }
5639 else
5640 {
5641 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5642 if (attr && cu->line_header)
5643 {
5644 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5645
cf2c3c16
TT
5646 dwarf_decode_macros (cu->line_header, macro_offset,
5647 comp_dir, abfd, cu,
5648 &dwarf2_per_objfile->macinfo, 0);
5649 }
2e276125 5650 }
9cdd5dbd 5651
debd256d 5652 do_cleanups (back_to);
5fb290d7
DJ
5653}
5654
ae2de4f8
DE
5655/* Process DW_TAG_type_unit.
5656 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5657 actual type being defined by this TU. In this case the first top
5658 level sibling is there to provide context only. */
5659
5660static void
5661read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5662{
5663 struct objfile *objfile = cu->objfile;
5664 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5665 CORE_ADDR lowpc;
5666 struct attribute *attr;
5667 char *name = NULL;
5668 char *comp_dir = NULL;
5669 struct die_info *child_die;
5670 bfd *abfd = objfile->obfd;
348e048f
DE
5671
5672 /* start_symtab needs a low pc, but we don't really have one.
5673 Do what read_file_scope would do in the absence of such info. */
5674 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 name = DW_STRING (attr);
5681
5682 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5683 if (attr)
5684 comp_dir = DW_STRING (attr);
5685 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5686 {
5687 comp_dir = ldirname (name);
5688 if (comp_dir != NULL)
5689 make_cleanup (xfree, comp_dir);
5690 }
5691
5692 if (name == NULL)
5693 name = "<unknown>";
5694
5695 attr = dwarf2_attr (die, DW_AT_language, cu);
5696 if (attr)
5697 set_cu_language (DW_UNSND (attr), cu);
5698
5699 /* This isn't technically needed today. It is done for symmetry
5700 with read_file_scope. */
5701 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5702 if (attr)
348e048f
DE
5703 cu->producer = DW_STRING (attr);
5704
0963b4bd 5705 /* We assume that we're processing GCC output. */
348e048f
DE
5706 processing_gcc_compilation = 2;
5707
5708 processing_has_namespace_info = 0;
5709
5710 start_symtab (name, comp_dir, lowpc);
5711 record_debugformat ("DWARF 2");
5712 record_producer (cu->producer);
5713
2ab95328
TT
5714 handle_DW_AT_stmt_list (die, cu, comp_dir);
5715
348e048f
DE
5716 /* Process the dies in the type unit. */
5717 if (die->child == NULL)
5718 {
5719 dump_die_for_error (die);
5720 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5721 bfd_get_filename (abfd));
5722 }
5723
5724 child_die = die->child;
5725
5726 while (child_die && child_die->tag)
5727 {
5728 process_die (child_die, cu);
5729
5730 child_die = sibling_die (child_die);
5731 }
5732
5733 do_cleanups (back_to);
5734}
5735
d389af10
JK
5736/* qsort helper for inherit_abstract_dies. */
5737
5738static int
5739unsigned_int_compar (const void *ap, const void *bp)
5740{
5741 unsigned int a = *(unsigned int *) ap;
5742 unsigned int b = *(unsigned int *) bp;
5743
5744 return (a > b) - (b > a);
5745}
5746
5747/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5748 Inherit only the children of the DW_AT_abstract_origin DIE not being
5749 already referenced by DW_AT_abstract_origin from the children of the
5750 current DIE. */
d389af10
JK
5751
5752static void
5753inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5754{
5755 struct die_info *child_die;
5756 unsigned die_children_count;
5757 /* CU offsets which were referenced by children of the current DIE. */
5758 unsigned *offsets;
5759 unsigned *offsets_end, *offsetp;
5760 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5761 struct die_info *origin_die;
5762 /* Iterator of the ORIGIN_DIE children. */
5763 struct die_info *origin_child_die;
5764 struct cleanup *cleanups;
5765 struct attribute *attr;
cd02d79d
PA
5766 struct dwarf2_cu *origin_cu;
5767 struct pending **origin_previous_list_in_scope;
d389af10
JK
5768
5769 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5770 if (!attr)
5771 return;
5772
cd02d79d
PA
5773 /* Note that following die references may follow to a die in a
5774 different cu. */
5775
5776 origin_cu = cu;
5777 origin_die = follow_die_ref (die, attr, &origin_cu);
5778
5779 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5780 symbols in. */
5781 origin_previous_list_in_scope = origin_cu->list_in_scope;
5782 origin_cu->list_in_scope = cu->list_in_scope;
5783
edb3359d
DJ
5784 if (die->tag != origin_die->tag
5785 && !(die->tag == DW_TAG_inlined_subroutine
5786 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5787 complaint (&symfile_complaints,
5788 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5789 die->offset, origin_die->offset);
5790
5791 child_die = die->child;
5792 die_children_count = 0;
5793 while (child_die && child_die->tag)
5794 {
5795 child_die = sibling_die (child_die);
5796 die_children_count++;
5797 }
5798 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5799 cleanups = make_cleanup (xfree, offsets);
5800
5801 offsets_end = offsets;
5802 child_die = die->child;
5803 while (child_die && child_die->tag)
5804 {
c38f313d
DJ
5805 /* For each CHILD_DIE, find the corresponding child of
5806 ORIGIN_DIE. If there is more than one layer of
5807 DW_AT_abstract_origin, follow them all; there shouldn't be,
5808 but GCC versions at least through 4.4 generate this (GCC PR
5809 40573). */
5810 struct die_info *child_origin_die = child_die;
cd02d79d 5811 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5812
c38f313d
DJ
5813 while (1)
5814 {
cd02d79d
PA
5815 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5816 child_origin_cu);
c38f313d
DJ
5817 if (attr == NULL)
5818 break;
cd02d79d
PA
5819 child_origin_die = follow_die_ref (child_origin_die, attr,
5820 &child_origin_cu);
c38f313d
DJ
5821 }
5822
d389af10
JK
5823 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5824 counterpart may exist. */
c38f313d 5825 if (child_origin_die != child_die)
d389af10 5826 {
edb3359d
DJ
5827 if (child_die->tag != child_origin_die->tag
5828 && !(child_die->tag == DW_TAG_inlined_subroutine
5829 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5830 complaint (&symfile_complaints,
5831 _("Child DIE 0x%x and its abstract origin 0x%x have "
5832 "different tags"), child_die->offset,
5833 child_origin_die->offset);
c38f313d
DJ
5834 if (child_origin_die->parent != origin_die)
5835 complaint (&symfile_complaints,
5836 _("Child DIE 0x%x and its abstract origin 0x%x have "
5837 "different parents"), child_die->offset,
5838 child_origin_die->offset);
5839 else
5840 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5841 }
5842 child_die = sibling_die (child_die);
5843 }
5844 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5845 unsigned_int_compar);
5846 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5847 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5848 complaint (&symfile_complaints,
5849 _("Multiple children of DIE 0x%x refer "
5850 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5851 die->offset, *offsetp);
5852
5853 offsetp = offsets;
5854 origin_child_die = origin_die->child;
5855 while (origin_child_die && origin_child_die->tag)
5856 {
5857 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5858 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5859 offsetp++;
5860 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5861 {
5862 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5863 process_die (origin_child_die, origin_cu);
d389af10
JK
5864 }
5865 origin_child_die = sibling_die (origin_child_die);
5866 }
cd02d79d 5867 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5868
5869 do_cleanups (cleanups);
5870}
5871
c906108c 5872static void
e7c27a73 5873read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5874{
e7c27a73 5875 struct objfile *objfile = cu->objfile;
52f0bd74 5876 struct context_stack *new;
c906108c
SS
5877 CORE_ADDR lowpc;
5878 CORE_ADDR highpc;
5879 struct die_info *child_die;
edb3359d 5880 struct attribute *attr, *call_line, *call_file;
c906108c 5881 char *name;
e142c38c 5882 CORE_ADDR baseaddr;
801e3a5b 5883 struct block *block;
edb3359d 5884 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5885 VEC (symbolp) *template_args = NULL;
5886 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5887
5888 if (inlined_func)
5889 {
5890 /* If we do not have call site information, we can't show the
5891 caller of this inlined function. That's too confusing, so
5892 only use the scope for local variables. */
5893 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5894 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5895 if (call_line == NULL || call_file == NULL)
5896 {
5897 read_lexical_block_scope (die, cu);
5898 return;
5899 }
5900 }
c906108c 5901
e142c38c
DJ
5902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5903
94af9270 5904 name = dwarf2_name (die, cu);
c906108c 5905
e8d05480
JB
5906 /* Ignore functions with missing or empty names. These are actually
5907 illegal according to the DWARF standard. */
5908 if (name == NULL)
5909 {
5910 complaint (&symfile_complaints,
5911 _("missing name for subprogram DIE at %d"), die->offset);
5912 return;
5913 }
5914
5915 /* Ignore functions with missing or invalid low and high pc attributes. */
5916 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5917 {
ae4d0c03
PM
5918 attr = dwarf2_attr (die, DW_AT_external, cu);
5919 if (!attr || !DW_UNSND (attr))
5920 complaint (&symfile_complaints,
3e43a32a
MS
5921 _("cannot get low and high bounds "
5922 "for subprogram DIE at %d"),
ae4d0c03 5923 die->offset);
e8d05480
JB
5924 return;
5925 }
c906108c
SS
5926
5927 lowpc += baseaddr;
5928 highpc += baseaddr;
5929
34eaf542
TT
5930 /* If we have any template arguments, then we must allocate a
5931 different sort of symbol. */
5932 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5933 {
5934 if (child_die->tag == DW_TAG_template_type_param
5935 || child_die->tag == DW_TAG_template_value_param)
5936 {
5937 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5938 struct template_symbol);
5939 templ_func->base.is_cplus_template_function = 1;
5940 break;
5941 }
5942 }
5943
c906108c 5944 new = push_context (0, lowpc);
34eaf542
TT
5945 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5946 (struct symbol *) templ_func);
4c2df51b 5947
4cecd739
DJ
5948 /* If there is a location expression for DW_AT_frame_base, record
5949 it. */
e142c38c 5950 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5951 if (attr)
c034e007
AC
5952 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5953 expression is being recorded directly in the function's symbol
5954 and not in a separate frame-base object. I guess this hack is
5955 to avoid adding some sort of frame-base adjunct/annex to the
5956 function's symbol :-(. The problem with doing this is that it
5957 results in a function symbol with a location expression that
5958 has nothing to do with the location of the function, ouch! The
5959 relationship should be: a function's symbol has-a frame base; a
5960 frame-base has-a location expression. */
e7c27a73 5961 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5962
e142c38c 5963 cu->list_in_scope = &local_symbols;
c906108c 5964
639d11d3 5965 if (die->child != NULL)
c906108c 5966 {
639d11d3 5967 child_die = die->child;
c906108c
SS
5968 while (child_die && child_die->tag)
5969 {
34eaf542
TT
5970 if (child_die->tag == DW_TAG_template_type_param
5971 || child_die->tag == DW_TAG_template_value_param)
5972 {
5973 struct symbol *arg = new_symbol (child_die, NULL, cu);
5974
f1078f66
DJ
5975 if (arg != NULL)
5976 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5977 }
5978 else
5979 process_die (child_die, cu);
c906108c
SS
5980 child_die = sibling_die (child_die);
5981 }
5982 }
5983
d389af10
JK
5984 inherit_abstract_dies (die, cu);
5985
4a811a97
UW
5986 /* If we have a DW_AT_specification, we might need to import using
5987 directives from the context of the specification DIE. See the
5988 comment in determine_prefix. */
5989 if (cu->language == language_cplus
5990 && dwarf2_attr (die, DW_AT_specification, cu))
5991 {
5992 struct dwarf2_cu *spec_cu = cu;
5993 struct die_info *spec_die = die_specification (die, &spec_cu);
5994
5995 while (spec_die)
5996 {
5997 child_die = spec_die->child;
5998 while (child_die && child_die->tag)
5999 {
6000 if (child_die->tag == DW_TAG_imported_module)
6001 process_die (child_die, spec_cu);
6002 child_die = sibling_die (child_die);
6003 }
6004
6005 /* In some cases, GCC generates specification DIEs that
6006 themselves contain DW_AT_specification attributes. */
6007 spec_die = die_specification (spec_die, &spec_cu);
6008 }
6009 }
6010
c906108c
SS
6011 new = pop_context ();
6012 /* Make a block for the local symbols within. */
801e3a5b
JB
6013 block = finish_block (new->name, &local_symbols, new->old_blocks,
6014 lowpc, highpc, objfile);
6015
df8a16a1 6016 /* For C++, set the block's scope. */
f55ee35c 6017 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6018 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6019 determine_prefix (die, cu),
df8a16a1
DJ
6020 processing_has_namespace_info);
6021
801e3a5b
JB
6022 /* If we have address ranges, record them. */
6023 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6024
34eaf542
TT
6025 /* Attach template arguments to function. */
6026 if (! VEC_empty (symbolp, template_args))
6027 {
6028 gdb_assert (templ_func != NULL);
6029
6030 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6031 templ_func->template_arguments
6032 = obstack_alloc (&objfile->objfile_obstack,
6033 (templ_func->n_template_arguments
6034 * sizeof (struct symbol *)));
6035 memcpy (templ_func->template_arguments,
6036 VEC_address (symbolp, template_args),
6037 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6038 VEC_free (symbolp, template_args);
6039 }
6040
208d8187
JB
6041 /* In C++, we can have functions nested inside functions (e.g., when
6042 a function declares a class that has methods). This means that
6043 when we finish processing a function scope, we may need to go
6044 back to building a containing block's symbol lists. */
6045 local_symbols = new->locals;
6046 param_symbols = new->params;
27aa8d6a 6047 using_directives = new->using_directives;
208d8187 6048
921e78cf
JB
6049 /* If we've finished processing a top-level function, subsequent
6050 symbols go in the file symbol list. */
6051 if (outermost_context_p ())
e142c38c 6052 cu->list_in_scope = &file_symbols;
c906108c
SS
6053}
6054
6055/* Process all the DIES contained within a lexical block scope. Start
6056 a new scope, process the dies, and then close the scope. */
6057
6058static void
e7c27a73 6059read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6060{
e7c27a73 6061 struct objfile *objfile = cu->objfile;
52f0bd74 6062 struct context_stack *new;
c906108c
SS
6063 CORE_ADDR lowpc, highpc;
6064 struct die_info *child_die;
e142c38c
DJ
6065 CORE_ADDR baseaddr;
6066
6067 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6068
6069 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6070 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6071 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6072 be nasty. Might be easier to properly extend generic blocks to
af34e669 6073 describe ranges. */
d85a05f0 6074 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6075 return;
6076 lowpc += baseaddr;
6077 highpc += baseaddr;
6078
6079 push_context (0, lowpc);
639d11d3 6080 if (die->child != NULL)
c906108c 6081 {
639d11d3 6082 child_die = die->child;
c906108c
SS
6083 while (child_die && child_die->tag)
6084 {
e7c27a73 6085 process_die (child_die, cu);
c906108c
SS
6086 child_die = sibling_die (child_die);
6087 }
6088 }
6089 new = pop_context ();
6090
8540c487 6091 if (local_symbols != NULL || using_directives != NULL)
c906108c 6092 {
801e3a5b
JB
6093 struct block *block
6094 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6095 highpc, objfile);
6096
6097 /* Note that recording ranges after traversing children, as we
6098 do here, means that recording a parent's ranges entails
6099 walking across all its children's ranges as they appear in
6100 the address map, which is quadratic behavior.
6101
6102 It would be nicer to record the parent's ranges before
6103 traversing its children, simply overriding whatever you find
6104 there. But since we don't even decide whether to create a
6105 block until after we've traversed its children, that's hard
6106 to do. */
6107 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6108 }
6109 local_symbols = new->locals;
27aa8d6a 6110 using_directives = new->using_directives;
c906108c
SS
6111}
6112
96408a79
SA
6113/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6114
6115static void
6116read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6117{
6118 struct objfile *objfile = cu->objfile;
6119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6120 CORE_ADDR pc, baseaddr;
6121 struct attribute *attr;
6122 struct call_site *call_site, call_site_local;
6123 void **slot;
6124 int nparams;
6125 struct die_info *child_die;
6126
6127 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6128
6129 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6130 if (!attr)
6131 {
6132 complaint (&symfile_complaints,
6133 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6134 "DIE 0x%x [in module %s]"),
bb5ed363 6135 die->offset, objfile->name);
96408a79
SA
6136 return;
6137 }
6138 pc = DW_ADDR (attr) + baseaddr;
6139
6140 if (cu->call_site_htab == NULL)
6141 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6142 NULL, &objfile->objfile_obstack,
6143 hashtab_obstack_allocate, NULL);
6144 call_site_local.pc = pc;
6145 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6146 if (*slot != NULL)
6147 {
6148 complaint (&symfile_complaints,
6149 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6150 "DIE 0x%x [in module %s]"),
bb5ed363 6151 paddress (gdbarch, pc), die->offset, objfile->name);
96408a79
SA
6152 return;
6153 }
6154
6155 /* Count parameters at the caller. */
6156
6157 nparams = 0;
6158 for (child_die = die->child; child_die && child_die->tag;
6159 child_die = sibling_die (child_die))
6160 {
6161 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6162 {
6163 complaint (&symfile_complaints,
6164 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6165 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6166 child_die->tag, child_die->offset, objfile->name);
96408a79
SA
6167 continue;
6168 }
6169
6170 nparams++;
6171 }
6172
6173 call_site = obstack_alloc (&objfile->objfile_obstack,
6174 (sizeof (*call_site)
6175 + (sizeof (*call_site->parameter)
6176 * (nparams - 1))));
6177 *slot = call_site;
6178 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6179 call_site->pc = pc;
6180
6181 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6182 {
6183 struct die_info *func_die;
6184
6185 /* Skip also over DW_TAG_inlined_subroutine. */
6186 for (func_die = die->parent;
6187 func_die && func_die->tag != DW_TAG_subprogram
6188 && func_die->tag != DW_TAG_subroutine_type;
6189 func_die = func_die->parent);
6190
6191 /* DW_AT_GNU_all_call_sites is a superset
6192 of DW_AT_GNU_all_tail_call_sites. */
6193 if (func_die
6194 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6195 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6196 {
6197 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6198 not complete. But keep CALL_SITE for look ups via call_site_htab,
6199 both the initial caller containing the real return address PC and
6200 the final callee containing the current PC of a chain of tail
6201 calls do not need to have the tail call list complete. But any
6202 function candidate for a virtual tail call frame searched via
6203 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6204 determined unambiguously. */
6205 }
6206 else
6207 {
6208 struct type *func_type = NULL;
6209
6210 if (func_die)
6211 func_type = get_die_type (func_die, cu);
6212 if (func_type != NULL)
6213 {
6214 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6215
6216 /* Enlist this call site to the function. */
6217 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6218 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6219 }
6220 else
6221 complaint (&symfile_complaints,
6222 _("Cannot find function owning DW_TAG_GNU_call_site "
6223 "DIE 0x%x [in module %s]"),
bb5ed363 6224 die->offset, objfile->name);
96408a79
SA
6225 }
6226 }
6227
6228 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6229 if (attr == NULL)
6230 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6231 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6232 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6233 /* Keep NULL DWARF_BLOCK. */;
6234 else if (attr_form_is_block (attr))
6235 {
6236 struct dwarf2_locexpr_baton *dlbaton;
6237
6238 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6239 dlbaton->data = DW_BLOCK (attr)->data;
6240 dlbaton->size = DW_BLOCK (attr)->size;
6241 dlbaton->per_cu = cu->per_cu;
6242
6243 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6244 }
6245 else if (is_ref_attr (attr))
6246 {
96408a79
SA
6247 struct dwarf2_cu *target_cu = cu;
6248 struct die_info *target_die;
6249
6250 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6251 gdb_assert (target_cu->objfile == objfile);
6252 if (die_is_declaration (target_die, target_cu))
6253 {
6254 const char *target_physname;
6255
6256 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6257 if (target_physname == NULL)
6258 complaint (&symfile_complaints,
6259 _("DW_AT_GNU_call_site_target target DIE has invalid "
6260 "physname, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6261 die->offset, objfile->name);
96408a79
SA
6262 else
6263 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6264 }
6265 else
6266 {
6267 CORE_ADDR lowpc;
6268
6269 /* DW_AT_entry_pc should be preferred. */
6270 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6271 complaint (&symfile_complaints,
6272 _("DW_AT_GNU_call_site_target target DIE has invalid "
6273 "low pc, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6274 die->offset, objfile->name);
96408a79
SA
6275 else
6276 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6277 }
6278 }
6279 else
6280 complaint (&symfile_complaints,
6281 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6282 "block nor reference, for DIE 0x%x [in module %s]"),
bb5ed363 6283 die->offset, objfile->name);
96408a79
SA
6284
6285 call_site->per_cu = cu->per_cu;
6286
6287 for (child_die = die->child;
6288 child_die && child_die->tag;
6289 child_die = sibling_die (child_die))
6290 {
6291 struct dwarf2_locexpr_baton *dlbaton;
6292 struct call_site_parameter *parameter;
6293
6294 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6295 {
6296 /* Already printed the complaint above. */
6297 continue;
6298 }
6299
6300 gdb_assert (call_site->parameter_count < nparams);
6301 parameter = &call_site->parameter[call_site->parameter_count];
6302
6303 /* DW_AT_location specifies the register number. Value of the data
6304 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6305
6306 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6307 if (!attr || !attr_form_is_block (attr))
6308 {
6309 complaint (&symfile_complaints,
6310 _("No DW_FORM_block* DW_AT_location for "
6311 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6312 child_die->offset, objfile->name);
96408a79
SA
6313 continue;
6314 }
6315 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6316 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6317 if (parameter->dwarf_reg == -1
6318 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6319 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6320 &parameter->fb_offset))
6321 {
6322 complaint (&symfile_complaints,
6323 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6324 "for DW_FORM_block* DW_AT_location for "
6325 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6326 child_die->offset, objfile->name);
96408a79
SA
6327 continue;
6328 }
6329
6330 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6331 if (!attr_form_is_block (attr))
6332 {
6333 complaint (&symfile_complaints,
6334 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6335 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6336 child_die->offset, objfile->name);
96408a79
SA
6337 continue;
6338 }
6339 parameter->value = DW_BLOCK (attr)->data;
6340 parameter->value_size = DW_BLOCK (attr)->size;
6341
6342 /* Parameters are not pre-cleared by memset above. */
6343 parameter->data_value = NULL;
6344 parameter->data_value_size = 0;
6345 call_site->parameter_count++;
6346
6347 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6348 if (attr)
6349 {
6350 if (!attr_form_is_block (attr))
6351 complaint (&symfile_complaints,
6352 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6353 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6354 child_die->offset, objfile->name);
96408a79
SA
6355 else
6356 {
6357 parameter->data_value = DW_BLOCK (attr)->data;
6358 parameter->data_value_size = DW_BLOCK (attr)->size;
6359 }
6360 }
6361 }
6362}
6363
43039443 6364/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6365 Return 1 if the attributes are present and valid, otherwise, return 0.
6366 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6367
6368static int
6369dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6370 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6371 struct partial_symtab *ranges_pst)
43039443
JK
6372{
6373 struct objfile *objfile = cu->objfile;
6374 struct comp_unit_head *cu_header = &cu->header;
6375 bfd *obfd = objfile->obfd;
6376 unsigned int addr_size = cu_header->addr_size;
6377 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6378 /* Base address selection entry. */
6379 CORE_ADDR base;
6380 int found_base;
6381 unsigned int dummy;
6382 gdb_byte *buffer;
6383 CORE_ADDR marker;
6384 int low_set;
6385 CORE_ADDR low = 0;
6386 CORE_ADDR high = 0;
ff013f42 6387 CORE_ADDR baseaddr;
43039443 6388
d00adf39
DE
6389 found_base = cu->base_known;
6390 base = cu->base_address;
43039443 6391
be391dca 6392 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6393 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6394 {
6395 complaint (&symfile_complaints,
6396 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6397 offset);
6398 return 0;
6399 }
dce234bc 6400 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6401
6402 /* Read in the largest possible address. */
6403 marker = read_address (obfd, buffer, cu, &dummy);
6404 if ((marker & mask) == mask)
6405 {
6406 /* If we found the largest possible address, then
6407 read the base address. */
6408 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6409 buffer += 2 * addr_size;
6410 offset += 2 * addr_size;
6411 found_base = 1;
6412 }
6413
6414 low_set = 0;
6415
e7030f15 6416 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6417
43039443
JK
6418 while (1)
6419 {
6420 CORE_ADDR range_beginning, range_end;
6421
6422 range_beginning = read_address (obfd, buffer, cu, &dummy);
6423 buffer += addr_size;
6424 range_end = read_address (obfd, buffer, cu, &dummy);
6425 buffer += addr_size;
6426 offset += 2 * addr_size;
6427
6428 /* An end of list marker is a pair of zero addresses. */
6429 if (range_beginning == 0 && range_end == 0)
6430 /* Found the end of list entry. */
6431 break;
6432
6433 /* Each base address selection entry is a pair of 2 values.
6434 The first is the largest possible address, the second is
6435 the base address. Check for a base address here. */
6436 if ((range_beginning & mask) == mask)
6437 {
6438 /* If we found the largest possible address, then
6439 read the base address. */
6440 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6441 found_base = 1;
6442 continue;
6443 }
6444
6445 if (!found_base)
6446 {
6447 /* We have no valid base address for the ranges
6448 data. */
6449 complaint (&symfile_complaints,
6450 _("Invalid .debug_ranges data (no base address)"));
6451 return 0;
6452 }
6453
9277c30c
UW
6454 if (range_beginning > range_end)
6455 {
6456 /* Inverted range entries are invalid. */
6457 complaint (&symfile_complaints,
6458 _("Invalid .debug_ranges data (inverted range)"));
6459 return 0;
6460 }
6461
6462 /* Empty range entries have no effect. */
6463 if (range_beginning == range_end)
6464 continue;
6465
43039443
JK
6466 range_beginning += base;
6467 range_end += base;
6468
9277c30c 6469 if (ranges_pst != NULL)
ff013f42 6470 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6471 range_beginning + baseaddr,
6472 range_end - 1 + baseaddr,
ff013f42
JK
6473 ranges_pst);
6474
43039443
JK
6475 /* FIXME: This is recording everything as a low-high
6476 segment of consecutive addresses. We should have a
6477 data structure for discontiguous block ranges
6478 instead. */
6479 if (! low_set)
6480 {
6481 low = range_beginning;
6482 high = range_end;
6483 low_set = 1;
6484 }
6485 else
6486 {
6487 if (range_beginning < low)
6488 low = range_beginning;
6489 if (range_end > high)
6490 high = range_end;
6491 }
6492 }
6493
6494 if (! low_set)
6495 /* If the first entry is an end-of-list marker, the range
6496 describes an empty scope, i.e. no instructions. */
6497 return 0;
6498
6499 if (low_return)
6500 *low_return = low;
6501 if (high_return)
6502 *high_return = high;
6503 return 1;
6504}
6505
af34e669
DJ
6506/* Get low and high pc attributes from a die. Return 1 if the attributes
6507 are present and valid, otherwise, return 0. Return -1 if the range is
6508 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6509static int
af34e669 6510dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6511 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6512 struct partial_symtab *pst)
c906108c
SS
6513{
6514 struct attribute *attr;
af34e669
DJ
6515 CORE_ADDR low = 0;
6516 CORE_ADDR high = 0;
6517 int ret = 0;
c906108c 6518
e142c38c 6519 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6520 if (attr)
af34e669
DJ
6521 {
6522 high = DW_ADDR (attr);
e142c38c 6523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6524 if (attr)
6525 low = DW_ADDR (attr);
6526 else
6527 /* Found high w/o low attribute. */
6528 return 0;
6529
6530 /* Found consecutive range of addresses. */
6531 ret = 1;
6532 }
c906108c 6533 else
af34e669 6534 {
e142c38c 6535 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6536 if (attr != NULL)
6537 {
af34e669 6538 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6539 .debug_ranges section. */
d85a05f0 6540 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6541 return 0;
43039443 6542 /* Found discontinuous range of addresses. */
af34e669
DJ
6543 ret = -1;
6544 }
6545 }
c906108c 6546
9373cf26
JK
6547 /* read_partial_die has also the strict LOW < HIGH requirement. */
6548 if (high <= low)
c906108c
SS
6549 return 0;
6550
6551 /* When using the GNU linker, .gnu.linkonce. sections are used to
6552 eliminate duplicate copies of functions and vtables and such.
6553 The linker will arbitrarily choose one and discard the others.
6554 The AT_*_pc values for such functions refer to local labels in
6555 these sections. If the section from that file was discarded, the
6556 labels are not in the output, so the relocs get a value of 0.
6557 If this is a discarded function, mark the pc bounds as invalid,
6558 so that GDB will ignore it. */
72dca2f5 6559 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6560 return 0;
6561
6562 *lowpc = low;
96408a79
SA
6563 if (highpc)
6564 *highpc = high;
af34e669 6565 return ret;
c906108c
SS
6566}
6567
b084d499
JB
6568/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6569 its low and high PC addresses. Do nothing if these addresses could not
6570 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6571 and HIGHPC to the high address if greater than HIGHPC. */
6572
6573static void
6574dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6576 struct dwarf2_cu *cu)
6577{
6578 CORE_ADDR low, high;
6579 struct die_info *child = die->child;
6580
d85a05f0 6581 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6582 {
6583 *lowpc = min (*lowpc, low);
6584 *highpc = max (*highpc, high);
6585 }
6586
6587 /* If the language does not allow nested subprograms (either inside
6588 subprograms or lexical blocks), we're done. */
6589 if (cu->language != language_ada)
6590 return;
6e70227d 6591
b084d499
JB
6592 /* Check all the children of the given DIE. If it contains nested
6593 subprograms, then check their pc bounds. Likewise, we need to
6594 check lexical blocks as well, as they may also contain subprogram
6595 definitions. */
6596 while (child && child->tag)
6597 {
6598 if (child->tag == DW_TAG_subprogram
6599 || child->tag == DW_TAG_lexical_block)
6600 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6601 child = sibling_die (child);
6602 }
6603}
6604
fae299cd
DC
6605/* Get the low and high pc's represented by the scope DIE, and store
6606 them in *LOWPC and *HIGHPC. If the correct values can't be
6607 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6608
6609static void
6610get_scope_pc_bounds (struct die_info *die,
6611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6612 struct dwarf2_cu *cu)
6613{
6614 CORE_ADDR best_low = (CORE_ADDR) -1;
6615 CORE_ADDR best_high = (CORE_ADDR) 0;
6616 CORE_ADDR current_low, current_high;
6617
d85a05f0 6618 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6619 {
6620 best_low = current_low;
6621 best_high = current_high;
6622 }
6623 else
6624 {
6625 struct die_info *child = die->child;
6626
6627 while (child && child->tag)
6628 {
6629 switch (child->tag) {
6630 case DW_TAG_subprogram:
b084d499 6631 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6632 break;
6633 case DW_TAG_namespace:
f55ee35c 6634 case DW_TAG_module:
fae299cd
DC
6635 /* FIXME: carlton/2004-01-16: Should we do this for
6636 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6637 that current GCC's always emit the DIEs corresponding
6638 to definitions of methods of classes as children of a
6639 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6640 the DIEs giving the declarations, which could be
6641 anywhere). But I don't see any reason why the
6642 standards says that they have to be there. */
6643 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6644
6645 if (current_low != ((CORE_ADDR) -1))
6646 {
6647 best_low = min (best_low, current_low);
6648 best_high = max (best_high, current_high);
6649 }
6650 break;
6651 default:
0963b4bd 6652 /* Ignore. */
fae299cd
DC
6653 break;
6654 }
6655
6656 child = sibling_die (child);
6657 }
6658 }
6659
6660 *lowpc = best_low;
6661 *highpc = best_high;
6662}
6663
801e3a5b
JB
6664/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6665 in DIE. */
6666static void
6667dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6668 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6669{
bb5ed363 6670 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6671 struct attribute *attr;
6672
6673 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6674 if (attr)
6675 {
6676 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6677
801e3a5b
JB
6678 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6679 if (attr)
6680 {
6681 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6682
801e3a5b
JB
6683 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6684 }
6685 }
6686
6687 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6688 if (attr)
6689 {
bb5ed363 6690 bfd *obfd = objfile->obfd;
801e3a5b
JB
6691
6692 /* The value of the DW_AT_ranges attribute is the offset of the
6693 address range list in the .debug_ranges section. */
6694 unsigned long offset = DW_UNSND (attr);
dce234bc 6695 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6696
6697 /* For some target architectures, but not others, the
6698 read_address function sign-extends the addresses it returns.
6699 To recognize base address selection entries, we need a
6700 mask. */
6701 unsigned int addr_size = cu->header.addr_size;
6702 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6703
6704 /* The base address, to which the next pair is relative. Note
6705 that this 'base' is a DWARF concept: most entries in a range
6706 list are relative, to reduce the number of relocs against the
6707 debugging information. This is separate from this function's
6708 'baseaddr' argument, which GDB uses to relocate debugging
6709 information from a shared library based on the address at
6710 which the library was loaded. */
d00adf39
DE
6711 CORE_ADDR base = cu->base_address;
6712 int base_known = cu->base_known;
801e3a5b 6713
be391dca 6714 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6715 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6716 {
6717 complaint (&symfile_complaints,
6718 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6719 offset);
6720 return;
6721 }
6722
6723 for (;;)
6724 {
6725 unsigned int bytes_read;
6726 CORE_ADDR start, end;
6727
6728 start = read_address (obfd, buffer, cu, &bytes_read);
6729 buffer += bytes_read;
6730 end = read_address (obfd, buffer, cu, &bytes_read);
6731 buffer += bytes_read;
6732
6733 /* Did we find the end of the range list? */
6734 if (start == 0 && end == 0)
6735 break;
6736
6737 /* Did we find a base address selection entry? */
6738 else if ((start & base_select_mask) == base_select_mask)
6739 {
6740 base = end;
6741 base_known = 1;
6742 }
6743
6744 /* We found an ordinary address range. */
6745 else
6746 {
6747 if (!base_known)
6748 {
6749 complaint (&symfile_complaints,
3e43a32a
MS
6750 _("Invalid .debug_ranges data "
6751 "(no base address)"));
801e3a5b
JB
6752 return;
6753 }
6754
9277c30c
UW
6755 if (start > end)
6756 {
6757 /* Inverted range entries are invalid. */
6758 complaint (&symfile_complaints,
6759 _("Invalid .debug_ranges data "
6760 "(inverted range)"));
6761 return;
6762 }
6763
6764 /* Empty range entries have no effect. */
6765 if (start == end)
6766 continue;
6767
6e70227d
DE
6768 record_block_range (block,
6769 baseaddr + base + start,
801e3a5b
JB
6770 baseaddr + base + end - 1);
6771 }
6772 }
6773 }
6774}
6775
60d5a603
JK
6776/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6777 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6778 during 4.6.0 experimental. */
6779
6780static int
6781producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6782{
6783 const char *cs;
6784 int major, minor, release;
6785
6786 if (cu->producer == NULL)
6787 {
6788 /* For unknown compilers expect their behavior is DWARF version
6789 compliant.
6790
6791 GCC started to support .debug_types sections by -gdwarf-4 since
6792 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6793 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6794 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6795 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6796
6797 return 0;
6798 }
6799
6800 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6801
6802 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6803 {
6804 /* For non-GCC compilers expect their behavior is DWARF version
6805 compliant. */
6806
6807 return 0;
6808 }
6809 cs = &cu->producer[strlen ("GNU ")];
6810 while (*cs && !isdigit (*cs))
6811 cs++;
6812 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6813 {
6814 /* Not recognized as GCC. */
6815
6816 return 0;
6817 }
6818
6819 return major < 4 || (major == 4 && minor < 6);
6820}
6821
6822/* Return the default accessibility type if it is not overriden by
6823 DW_AT_accessibility. */
6824
6825static enum dwarf_access_attribute
6826dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6827{
6828 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6829 {
6830 /* The default DWARF 2 accessibility for members is public, the default
6831 accessibility for inheritance is private. */
6832
6833 if (die->tag != DW_TAG_inheritance)
6834 return DW_ACCESS_public;
6835 else
6836 return DW_ACCESS_private;
6837 }
6838 else
6839 {
6840 /* DWARF 3+ defines the default accessibility a different way. The same
6841 rules apply now for DW_TAG_inheritance as for the members and it only
6842 depends on the container kind. */
6843
6844 if (die->parent->tag == DW_TAG_class_type)
6845 return DW_ACCESS_private;
6846 else
6847 return DW_ACCESS_public;
6848 }
6849}
6850
74ac6d43
TT
6851/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6852 offset. If the attribute was not found return 0, otherwise return
6853 1. If it was found but could not properly be handled, set *OFFSET
6854 to 0. */
6855
6856static int
6857handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6858 LONGEST *offset)
6859{
6860 struct attribute *attr;
6861
6862 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6863 if (attr != NULL)
6864 {
6865 *offset = 0;
6866
6867 /* Note that we do not check for a section offset first here.
6868 This is because DW_AT_data_member_location is new in DWARF 4,
6869 so if we see it, we can assume that a constant form is really
6870 a constant and not a section offset. */
6871 if (attr_form_is_constant (attr))
6872 *offset = dwarf2_get_attr_constant_value (attr, 0);
6873 else if (attr_form_is_section_offset (attr))
6874 dwarf2_complex_location_expr_complaint ();
6875 else if (attr_form_is_block (attr))
6876 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6877 else
6878 dwarf2_complex_location_expr_complaint ();
6879
6880 return 1;
6881 }
6882
6883 return 0;
6884}
6885
c906108c
SS
6886/* Add an aggregate field to the field list. */
6887
6888static void
107d2387 6889dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6890 struct dwarf2_cu *cu)
6e70227d 6891{
e7c27a73 6892 struct objfile *objfile = cu->objfile;
5e2b427d 6893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6894 struct nextfield *new_field;
6895 struct attribute *attr;
6896 struct field *fp;
6897 char *fieldname = "";
6898
6899 /* Allocate a new field list entry and link it in. */
6900 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6901 make_cleanup (xfree, new_field);
c906108c 6902 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6903
6904 if (die->tag == DW_TAG_inheritance)
6905 {
6906 new_field->next = fip->baseclasses;
6907 fip->baseclasses = new_field;
6908 }
6909 else
6910 {
6911 new_field->next = fip->fields;
6912 fip->fields = new_field;
6913 }
c906108c
SS
6914 fip->nfields++;
6915
e142c38c 6916 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6917 if (attr)
6918 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6919 else
6920 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6921 if (new_field->accessibility != DW_ACCESS_public)
6922 fip->non_public_fields = 1;
60d5a603 6923
e142c38c 6924 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6925 if (attr)
6926 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6927 else
6928 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6929
6930 fp = &new_field->field;
a9a9bd0f 6931
e142c38c 6932 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6933 {
74ac6d43
TT
6934 LONGEST offset;
6935
a9a9bd0f 6936 /* Data member other than a C++ static data member. */
6e70227d 6937
c906108c 6938 /* Get type of field. */
e7c27a73 6939 fp->type = die_type (die, cu);
c906108c 6940
d6a843b5 6941 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6942
c906108c 6943 /* Get bit size of field (zero if none). */
e142c38c 6944 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6945 if (attr)
6946 {
6947 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6948 }
6949 else
6950 {
6951 FIELD_BITSIZE (*fp) = 0;
6952 }
6953
6954 /* Get bit offset of field. */
74ac6d43
TT
6955 if (handle_data_member_location (die, cu, &offset))
6956 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6957 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6958 if (attr)
6959 {
5e2b427d 6960 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6961 {
6962 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6963 additional bit offset from the MSB of the containing
6964 anonymous object to the MSB of the field. We don't
6965 have to do anything special since we don't need to
6966 know the size of the anonymous object. */
c906108c
SS
6967 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6968 }
6969 else
6970 {
6971 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6972 MSB of the anonymous object, subtract off the number of
6973 bits from the MSB of the field to the MSB of the
6974 object, and then subtract off the number of bits of
6975 the field itself. The result is the bit offset of
6976 the LSB of the field. */
c906108c
SS
6977 int anonymous_size;
6978 int bit_offset = DW_UNSND (attr);
6979
e142c38c 6980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6981 if (attr)
6982 {
6983 /* The size of the anonymous object containing
6984 the bit field is explicit, so use the
6985 indicated size (in bytes). */
6986 anonymous_size = DW_UNSND (attr);
6987 }
6988 else
6989 {
6990 /* The size of the anonymous object containing
6991 the bit field must be inferred from the type
6992 attribute of the data member containing the
6993 bit field. */
6994 anonymous_size = TYPE_LENGTH (fp->type);
6995 }
6996 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6997 - bit_offset - FIELD_BITSIZE (*fp);
6998 }
6999 }
7000
7001 /* Get name of field. */
39cbfefa
DJ
7002 fieldname = dwarf2_name (die, cu);
7003 if (fieldname == NULL)
7004 fieldname = "";
d8151005
DJ
7005
7006 /* The name is already allocated along with this objfile, so we don't
7007 need to duplicate it for the type. */
7008 fp->name = fieldname;
c906108c
SS
7009
7010 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7011 pointer or virtual base class pointer) to private. */
e142c38c 7012 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7013 {
d48cc9dd 7014 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7015 new_field->accessibility = DW_ACCESS_private;
7016 fip->non_public_fields = 1;
7017 }
7018 }
a9a9bd0f 7019 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7020 {
a9a9bd0f
DC
7021 /* C++ static member. */
7022
7023 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7024 is a declaration, but all versions of G++ as of this writing
7025 (so through at least 3.2.1) incorrectly generate
7026 DW_TAG_variable tags. */
6e70227d 7027
ff355380 7028 const char *physname;
c906108c 7029
a9a9bd0f 7030 /* Get name of field. */
39cbfefa
DJ
7031 fieldname = dwarf2_name (die, cu);
7032 if (fieldname == NULL)
c906108c
SS
7033 return;
7034
254e6b9e 7035 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7036 if (attr
7037 /* Only create a symbol if this is an external value.
7038 new_symbol checks this and puts the value in the global symbol
7039 table, which we want. If it is not external, new_symbol
7040 will try to put the value in cu->list_in_scope which is wrong. */
7041 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7042 {
7043 /* A static const member, not much different than an enum as far as
7044 we're concerned, except that we can support more types. */
7045 new_symbol (die, NULL, cu);
7046 }
7047
2df3850c 7048 /* Get physical name. */
ff355380 7049 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7050
d8151005
DJ
7051 /* The name is already allocated along with this objfile, so we don't
7052 need to duplicate it for the type. */
7053 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7054 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7055 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7056 }
7057 else if (die->tag == DW_TAG_inheritance)
7058 {
74ac6d43 7059 LONGEST offset;
d4b96c9a 7060
74ac6d43
TT
7061 /* C++ base class field. */
7062 if (handle_data_member_location (die, cu, &offset))
7063 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7064 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7065 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7066 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7067 fip->nbaseclasses++;
7068 }
7069}
7070
98751a41
JK
7071/* Add a typedef defined in the scope of the FIP's class. */
7072
7073static void
7074dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7075 struct dwarf2_cu *cu)
6e70227d 7076{
98751a41 7077 struct objfile *objfile = cu->objfile;
98751a41
JK
7078 struct typedef_field_list *new_field;
7079 struct attribute *attr;
7080 struct typedef_field *fp;
7081 char *fieldname = "";
7082
7083 /* Allocate a new field list entry and link it in. */
7084 new_field = xzalloc (sizeof (*new_field));
7085 make_cleanup (xfree, new_field);
7086
7087 gdb_assert (die->tag == DW_TAG_typedef);
7088
7089 fp = &new_field->field;
7090
7091 /* Get name of field. */
7092 fp->name = dwarf2_name (die, cu);
7093 if (fp->name == NULL)
7094 return;
7095
7096 fp->type = read_type_die (die, cu);
7097
7098 new_field->next = fip->typedef_field_list;
7099 fip->typedef_field_list = new_field;
7100 fip->typedef_field_list_count++;
7101}
7102
c906108c
SS
7103/* Create the vector of fields, and attach it to the type. */
7104
7105static void
fba45db2 7106dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7107 struct dwarf2_cu *cu)
c906108c
SS
7108{
7109 int nfields = fip->nfields;
7110
7111 /* Record the field count, allocate space for the array of fields,
7112 and create blank accessibility bitfields if necessary. */
7113 TYPE_NFIELDS (type) = nfields;
7114 TYPE_FIELDS (type) = (struct field *)
7115 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7116 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7117
b4ba55a1 7118 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7119 {
7120 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7121
7122 TYPE_FIELD_PRIVATE_BITS (type) =
7123 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7124 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7125
7126 TYPE_FIELD_PROTECTED_BITS (type) =
7127 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7128 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7129
774b6a14
TT
7130 TYPE_FIELD_IGNORE_BITS (type) =
7131 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7132 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7133 }
7134
7135 /* If the type has baseclasses, allocate and clear a bit vector for
7136 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7137 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7138 {
7139 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7140 unsigned char *pointer;
c906108c
SS
7141
7142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7143 pointer = TYPE_ALLOC (type, num_bytes);
7144 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7145 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7146 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7147 }
7148
3e43a32a
MS
7149 /* Copy the saved-up fields into the field vector. Start from the head of
7150 the list, adding to the tail of the field array, so that they end up in
7151 the same order in the array in which they were added to the list. */
c906108c
SS
7152 while (nfields-- > 0)
7153 {
7d0ccb61
DJ
7154 struct nextfield *fieldp;
7155
7156 if (fip->fields)
7157 {
7158 fieldp = fip->fields;
7159 fip->fields = fieldp->next;
7160 }
7161 else
7162 {
7163 fieldp = fip->baseclasses;
7164 fip->baseclasses = fieldp->next;
7165 }
7166
7167 TYPE_FIELD (type, nfields) = fieldp->field;
7168 switch (fieldp->accessibility)
c906108c 7169 {
c5aa993b 7170 case DW_ACCESS_private:
b4ba55a1
JB
7171 if (cu->language != language_ada)
7172 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7173 break;
c906108c 7174
c5aa993b 7175 case DW_ACCESS_protected:
b4ba55a1
JB
7176 if (cu->language != language_ada)
7177 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7178 break;
c906108c 7179
c5aa993b
JM
7180 case DW_ACCESS_public:
7181 break;
c906108c 7182
c5aa993b
JM
7183 default:
7184 /* Unknown accessibility. Complain and treat it as public. */
7185 {
e2e0b3e5 7186 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7187 fieldp->accessibility);
c5aa993b
JM
7188 }
7189 break;
c906108c
SS
7190 }
7191 if (nfields < fip->nbaseclasses)
7192 {
7d0ccb61 7193 switch (fieldp->virtuality)
c906108c 7194 {
c5aa993b
JM
7195 case DW_VIRTUALITY_virtual:
7196 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7197 if (cu->language == language_ada)
a73c6dcd 7198 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7199 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7200 break;
c906108c
SS
7201 }
7202 }
c906108c
SS
7203 }
7204}
7205
c906108c
SS
7206/* Add a member function to the proper fieldlist. */
7207
7208static void
107d2387 7209dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7210 struct type *type, struct dwarf2_cu *cu)
c906108c 7211{
e7c27a73 7212 struct objfile *objfile = cu->objfile;
c906108c
SS
7213 struct attribute *attr;
7214 struct fnfieldlist *flp;
7215 int i;
7216 struct fn_field *fnp;
7217 char *fieldname;
c906108c 7218 struct nextfnfield *new_fnfield;
f792889a 7219 struct type *this_type;
60d5a603 7220 enum dwarf_access_attribute accessibility;
c906108c 7221
b4ba55a1 7222 if (cu->language == language_ada)
a73c6dcd 7223 error (_("unexpected member function in Ada type"));
b4ba55a1 7224
2df3850c 7225 /* Get name of member function. */
39cbfefa
DJ
7226 fieldname = dwarf2_name (die, cu);
7227 if (fieldname == NULL)
2df3850c 7228 return;
c906108c 7229
c906108c
SS
7230 /* Look up member function name in fieldlist. */
7231 for (i = 0; i < fip->nfnfields; i++)
7232 {
27bfe10e 7233 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7234 break;
7235 }
7236
7237 /* Create new list element if necessary. */
7238 if (i < fip->nfnfields)
7239 flp = &fip->fnfieldlists[i];
7240 else
7241 {
7242 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7243 {
7244 fip->fnfieldlists = (struct fnfieldlist *)
7245 xrealloc (fip->fnfieldlists,
7246 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7247 * sizeof (struct fnfieldlist));
c906108c 7248 if (fip->nfnfields == 0)
c13c43fd 7249 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7250 }
7251 flp = &fip->fnfieldlists[fip->nfnfields];
7252 flp->name = fieldname;
7253 flp->length = 0;
7254 flp->head = NULL;
3da10d80 7255 i = fip->nfnfields++;
c906108c
SS
7256 }
7257
7258 /* Create a new member function field and chain it to the field list
0963b4bd 7259 entry. */
c906108c 7260 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7261 make_cleanup (xfree, new_fnfield);
c906108c
SS
7262 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7263 new_fnfield->next = flp->head;
7264 flp->head = new_fnfield;
7265 flp->length++;
7266
7267 /* Fill in the member function field info. */
7268 fnp = &new_fnfield->fnfield;
3da10d80
KS
7269
7270 /* Delay processing of the physname until later. */
7271 if (cu->language == language_cplus || cu->language == language_java)
7272 {
7273 add_to_method_list (type, i, flp->length - 1, fieldname,
7274 die, cu);
7275 }
7276 else
7277 {
1d06ead6 7278 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7279 fnp->physname = physname ? physname : "";
7280 }
7281
c906108c 7282 fnp->type = alloc_type (objfile);
f792889a
DJ
7283 this_type = read_type_die (die, cu);
7284 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7285 {
f792889a 7286 int nparams = TYPE_NFIELDS (this_type);
c906108c 7287
f792889a 7288 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7289 of the method itself (TYPE_CODE_METHOD). */
7290 smash_to_method_type (fnp->type, type,
f792889a
DJ
7291 TYPE_TARGET_TYPE (this_type),
7292 TYPE_FIELDS (this_type),
7293 TYPE_NFIELDS (this_type),
7294 TYPE_VARARGS (this_type));
c906108c
SS
7295
7296 /* Handle static member functions.
c5aa993b 7297 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7298 member functions. G++ helps GDB by marking the first
7299 parameter for non-static member functions (which is the this
7300 pointer) as artificial. We obtain this information from
7301 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7302 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7303 fnp->voffset = VOFFSET_STATIC;
7304 }
7305 else
e2e0b3e5 7306 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7307 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7308
7309 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7310 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7311 fnp->fcontext = die_containing_type (die, cu);
c906108c 7312
3e43a32a
MS
7313 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7314 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7315
7316 /* Get accessibility. */
e142c38c 7317 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7318 if (attr)
60d5a603
JK
7319 accessibility = DW_UNSND (attr);
7320 else
7321 accessibility = dwarf2_default_access_attribute (die, cu);
7322 switch (accessibility)
c906108c 7323 {
60d5a603
JK
7324 case DW_ACCESS_private:
7325 fnp->is_private = 1;
7326 break;
7327 case DW_ACCESS_protected:
7328 fnp->is_protected = 1;
7329 break;
c906108c
SS
7330 }
7331
b02dede2 7332 /* Check for artificial methods. */
e142c38c 7333 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7334 if (attr && DW_UNSND (attr) != 0)
7335 fnp->is_artificial = 1;
7336
0d564a31 7337 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7338 function. For older versions of GCC, this is an offset in the
7339 appropriate virtual table, as specified by DW_AT_containing_type.
7340 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7341 to the object address. */
7342
e142c38c 7343 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7344 if (attr)
8e19ed76 7345 {
aec5aa8b 7346 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7347 {
aec5aa8b
TT
7348 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7349 {
7350 /* Old-style GCC. */
7351 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7352 }
7353 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7354 || (DW_BLOCK (attr)->size > 1
7355 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7356 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7357 {
7358 struct dwarf_block blk;
7359 int offset;
7360
7361 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7362 ? 1 : 2);
7363 blk.size = DW_BLOCK (attr)->size - offset;
7364 blk.data = DW_BLOCK (attr)->data + offset;
7365 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7366 if ((fnp->voffset % cu->header.addr_size) != 0)
7367 dwarf2_complex_location_expr_complaint ();
7368 else
7369 fnp->voffset /= cu->header.addr_size;
7370 fnp->voffset += 2;
7371 }
7372 else
7373 dwarf2_complex_location_expr_complaint ();
7374
7375 if (!fnp->fcontext)
7376 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7377 }
3690dd37 7378 else if (attr_form_is_section_offset (attr))
8e19ed76 7379 {
4d3c2250 7380 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7381 }
7382 else
7383 {
4d3c2250
KB
7384 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7385 fieldname);
8e19ed76 7386 }
0d564a31 7387 }
d48cc9dd
DJ
7388 else
7389 {
7390 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7391 if (attr && DW_UNSND (attr))
7392 {
7393 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7394 complaint (&symfile_complaints,
3e43a32a
MS
7395 _("Member function \"%s\" (offset %d) is virtual "
7396 "but the vtable offset is not specified"),
d48cc9dd 7397 fieldname, die->offset);
9655fd1a 7398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7399 TYPE_CPLUS_DYNAMIC (type) = 1;
7400 }
7401 }
c906108c
SS
7402}
7403
7404/* Create the vector of member function fields, and attach it to the type. */
7405
7406static void
fba45db2 7407dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7408 struct dwarf2_cu *cu)
c906108c
SS
7409{
7410 struct fnfieldlist *flp;
c906108c
SS
7411 int i;
7412
b4ba55a1 7413 if (cu->language == language_ada)
a73c6dcd 7414 error (_("unexpected member functions in Ada type"));
b4ba55a1 7415
c906108c
SS
7416 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7417 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7418 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7419
7420 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7421 {
7422 struct nextfnfield *nfp = flp->head;
7423 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7424 int k;
7425
7426 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7427 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7428 fn_flp->fn_fields = (struct fn_field *)
7429 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7430 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7431 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7432 }
7433
7434 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7435}
7436
1168df01
JB
7437/* Returns non-zero if NAME is the name of a vtable member in CU's
7438 language, zero otherwise. */
7439static int
7440is_vtable_name (const char *name, struct dwarf2_cu *cu)
7441{
7442 static const char vptr[] = "_vptr";
987504bb 7443 static const char vtable[] = "vtable";
1168df01 7444
987504bb
JJ
7445 /* Look for the C++ and Java forms of the vtable. */
7446 if ((cu->language == language_java
7447 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7448 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7449 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7450 return 1;
7451
7452 return 0;
7453}
7454
c0dd20ea 7455/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7456 functions, with the ABI-specified layout. If TYPE describes
7457 such a structure, smash it into a member function type.
61049d3b
DJ
7458
7459 GCC shouldn't do this; it should just output pointer to member DIEs.
7460 This is GCC PR debug/28767. */
c0dd20ea 7461
0b92b5bb
TT
7462static void
7463quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7464{
0b92b5bb 7465 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7466
7467 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7468 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7469 return;
c0dd20ea
DJ
7470
7471 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7472 if (TYPE_FIELD_NAME (type, 0) == NULL
7473 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7474 || TYPE_FIELD_NAME (type, 1) == NULL
7475 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7476 return;
c0dd20ea
DJ
7477
7478 /* Find the type of the method. */
0b92b5bb 7479 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7480 if (pfn_type == NULL
7481 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7482 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7483 return;
c0dd20ea
DJ
7484
7485 /* Look for the "this" argument. */
7486 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7487 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7488 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7489 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7490 return;
c0dd20ea
DJ
7491
7492 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7493 new_type = alloc_type (objfile);
7494 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7495 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7496 TYPE_VARARGS (pfn_type));
0b92b5bb 7497 smash_to_methodptr_type (type, new_type);
c0dd20ea 7498}
1168df01 7499
c906108c 7500/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7501 (definition) to create a type for the structure or union. Fill in
7502 the type's name and general properties; the members will not be
7503 processed until process_structure_type.
c906108c 7504
c767944b
DJ
7505 NOTE: we need to call these functions regardless of whether or not the
7506 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7507 structure or union. This gets the type entered into our set of
7508 user defined types.
7509
7510 However, if the structure is incomplete (an opaque struct/union)
7511 then suppress creating a symbol table entry for it since gdb only
7512 wants to find the one with the complete definition. Note that if
7513 it is complete, we just call new_symbol, which does it's own
7514 checking about whether the struct/union is anonymous or not (and
7515 suppresses creating a symbol table entry itself). */
7516
f792889a 7517static struct type *
134d01f1 7518read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7519{
e7c27a73 7520 struct objfile *objfile = cu->objfile;
c906108c
SS
7521 struct type *type;
7522 struct attribute *attr;
39cbfefa 7523 char *name;
c906108c 7524
348e048f
DE
7525 /* If the definition of this type lives in .debug_types, read that type.
7526 Don't follow DW_AT_specification though, that will take us back up
7527 the chain and we want to go down. */
7528 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7529 if (attr)
7530 {
7531 struct dwarf2_cu *type_cu = cu;
7532 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7533
348e048f
DE
7534 /* We could just recurse on read_structure_type, but we need to call
7535 get_die_type to ensure only one type for this DIE is created.
7536 This is important, for example, because for c++ classes we need
7537 TYPE_NAME set which is only done by new_symbol. Blech. */
7538 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7539
7540 /* TYPE_CU may not be the same as CU.
7541 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7542 return set_die_type (die, type, cu);
7543 }
7544
c0dd20ea 7545 type = alloc_type (objfile);
c906108c 7546 INIT_CPLUS_SPECIFIC (type);
93311388 7547
39cbfefa
DJ
7548 name = dwarf2_name (die, cu);
7549 if (name != NULL)
c906108c 7550 {
987504bb
JJ
7551 if (cu->language == language_cplus
7552 || cu->language == language_java)
63d06c5c 7553 {
3da10d80
KS
7554 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7555
7556 /* dwarf2_full_name might have already finished building the DIE's
7557 type. If so, there is no need to continue. */
7558 if (get_die_type (die, cu) != NULL)
7559 return get_die_type (die, cu);
7560
7561 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7562 if (die->tag == DW_TAG_structure_type
7563 || die->tag == DW_TAG_class_type)
7564 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7565 }
7566 else
7567 {
d8151005
DJ
7568 /* The name is already allocated along with this objfile, so
7569 we don't need to duplicate it for the type. */
94af9270
KS
7570 TYPE_TAG_NAME (type) = (char *) name;
7571 if (die->tag == DW_TAG_class_type)
7572 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7573 }
c906108c
SS
7574 }
7575
7576 if (die->tag == DW_TAG_structure_type)
7577 {
7578 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7579 }
7580 else if (die->tag == DW_TAG_union_type)
7581 {
7582 TYPE_CODE (type) = TYPE_CODE_UNION;
7583 }
7584 else
7585 {
c906108c
SS
7586 TYPE_CODE (type) = TYPE_CODE_CLASS;
7587 }
7588
0cc2414c
TT
7589 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7590 TYPE_DECLARED_CLASS (type) = 1;
7591
e142c38c 7592 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7593 if (attr)
7594 {
7595 TYPE_LENGTH (type) = DW_UNSND (attr);
7596 }
7597 else
7598 {
7599 TYPE_LENGTH (type) = 0;
7600 }
7601
876cecd0 7602 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7603 if (die_is_declaration (die, cu))
876cecd0 7604 TYPE_STUB (type) = 1;
a6c727b2
DJ
7605 else if (attr == NULL && die->child == NULL
7606 && producer_is_realview (cu->producer))
7607 /* RealView does not output the required DW_AT_declaration
7608 on incomplete types. */
7609 TYPE_STUB (type) = 1;
dc718098 7610
c906108c
SS
7611 /* We need to add the type field to the die immediately so we don't
7612 infinitely recurse when dealing with pointers to the structure
0963b4bd 7613 type within the structure itself. */
1c379e20 7614 set_die_type (die, type, cu);
c906108c 7615
7e314c57
JK
7616 /* set_die_type should be already done. */
7617 set_descriptive_type (type, die, cu);
7618
c767944b
DJ
7619 return type;
7620}
7621
7622/* Finish creating a structure or union type, including filling in
7623 its members and creating a symbol for it. */
7624
7625static void
7626process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7627{
7628 struct objfile *objfile = cu->objfile;
7629 struct die_info *child_die = die->child;
7630 struct type *type;
7631
7632 type = get_die_type (die, cu);
7633 if (type == NULL)
7634 type = read_structure_type (die, cu);
7635
e142c38c 7636 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7637 {
7638 struct field_info fi;
7639 struct die_info *child_die;
34eaf542 7640 VEC (symbolp) *template_args = NULL;
c767944b 7641 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7642
7643 memset (&fi, 0, sizeof (struct field_info));
7644
639d11d3 7645 child_die = die->child;
c906108c
SS
7646
7647 while (child_die && child_die->tag)
7648 {
a9a9bd0f
DC
7649 if (child_die->tag == DW_TAG_member
7650 || child_die->tag == DW_TAG_variable)
c906108c 7651 {
a9a9bd0f
DC
7652 /* NOTE: carlton/2002-11-05: A C++ static data member
7653 should be a DW_TAG_member that is a declaration, but
7654 all versions of G++ as of this writing (so through at
7655 least 3.2.1) incorrectly generate DW_TAG_variable
7656 tags for them instead. */
e7c27a73 7657 dwarf2_add_field (&fi, child_die, cu);
c906108c 7658 }
8713b1b1 7659 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7660 {
0963b4bd 7661 /* C++ member function. */
e7c27a73 7662 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7663 }
7664 else if (child_die->tag == DW_TAG_inheritance)
7665 {
7666 /* C++ base class field. */
e7c27a73 7667 dwarf2_add_field (&fi, child_die, cu);
c906108c 7668 }
98751a41
JK
7669 else if (child_die->tag == DW_TAG_typedef)
7670 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7671 else if (child_die->tag == DW_TAG_template_type_param
7672 || child_die->tag == DW_TAG_template_value_param)
7673 {
7674 struct symbol *arg = new_symbol (child_die, NULL, cu);
7675
f1078f66
DJ
7676 if (arg != NULL)
7677 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7678 }
7679
c906108c
SS
7680 child_die = sibling_die (child_die);
7681 }
7682
34eaf542
TT
7683 /* Attach template arguments to type. */
7684 if (! VEC_empty (symbolp, template_args))
7685 {
7686 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7687 TYPE_N_TEMPLATE_ARGUMENTS (type)
7688 = VEC_length (symbolp, template_args);
7689 TYPE_TEMPLATE_ARGUMENTS (type)
7690 = obstack_alloc (&objfile->objfile_obstack,
7691 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7692 * sizeof (struct symbol *)));
7693 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7694 VEC_address (symbolp, template_args),
7695 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7696 * sizeof (struct symbol *)));
7697 VEC_free (symbolp, template_args);
7698 }
7699
c906108c
SS
7700 /* Attach fields and member functions to the type. */
7701 if (fi.nfields)
e7c27a73 7702 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7703 if (fi.nfnfields)
7704 {
e7c27a73 7705 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7706
c5aa993b 7707 /* Get the type which refers to the base class (possibly this
c906108c 7708 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7709 class from the DW_AT_containing_type attribute. This use of
7710 DW_AT_containing_type is a GNU extension. */
c906108c 7711
e142c38c 7712 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7713 {
e7c27a73 7714 struct type *t = die_containing_type (die, cu);
c906108c
SS
7715
7716 TYPE_VPTR_BASETYPE (type) = t;
7717 if (type == t)
7718 {
c906108c
SS
7719 int i;
7720
7721 /* Our own class provides vtbl ptr. */
7722 for (i = TYPE_NFIELDS (t) - 1;
7723 i >= TYPE_N_BASECLASSES (t);
7724 --i)
7725 {
7726 char *fieldname = TYPE_FIELD_NAME (t, i);
7727
1168df01 7728 if (is_vtable_name (fieldname, cu))
c906108c
SS
7729 {
7730 TYPE_VPTR_FIELDNO (type) = i;
7731 break;
7732 }
7733 }
7734
7735 /* Complain if virtual function table field not found. */
7736 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7737 complaint (&symfile_complaints,
3e43a32a
MS
7738 _("virtual function table pointer "
7739 "not found when defining class '%s'"),
4d3c2250
KB
7740 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7741 "");
c906108c
SS
7742 }
7743 else
7744 {
7745 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7746 }
7747 }
f6235d4c
EZ
7748 else if (cu->producer
7749 && strncmp (cu->producer,
7750 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7751 {
7752 /* The IBM XLC compiler does not provide direct indication
7753 of the containing type, but the vtable pointer is
7754 always named __vfp. */
7755
7756 int i;
7757
7758 for (i = TYPE_NFIELDS (type) - 1;
7759 i >= TYPE_N_BASECLASSES (type);
7760 --i)
7761 {
7762 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7763 {
7764 TYPE_VPTR_FIELDNO (type) = i;
7765 TYPE_VPTR_BASETYPE (type) = type;
7766 break;
7767 }
7768 }
7769 }
c906108c 7770 }
98751a41
JK
7771
7772 /* Copy fi.typedef_field_list linked list elements content into the
7773 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7774 if (fi.typedef_field_list)
7775 {
7776 int i = fi.typedef_field_list_count;
7777
a0d7a4ff 7778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7779 TYPE_TYPEDEF_FIELD_ARRAY (type)
7780 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7781 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7782
7783 /* Reverse the list order to keep the debug info elements order. */
7784 while (--i >= 0)
7785 {
7786 struct typedef_field *dest, *src;
6e70227d 7787
98751a41
JK
7788 dest = &TYPE_TYPEDEF_FIELD (type, i);
7789 src = &fi.typedef_field_list->field;
7790 fi.typedef_field_list = fi.typedef_field_list->next;
7791 *dest = *src;
7792 }
7793 }
c767944b
DJ
7794
7795 do_cleanups (back_to);
eb2a6f42
TT
7796
7797 if (HAVE_CPLUS_STRUCT (type))
7798 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7799 }
63d06c5c 7800
bb5ed363 7801 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7802
90aeadfc
DC
7803 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7804 snapshots) has been known to create a die giving a declaration
7805 for a class that has, as a child, a die giving a definition for a
7806 nested class. So we have to process our children even if the
7807 current die is a declaration. Normally, of course, a declaration
7808 won't have any children at all. */
134d01f1 7809
90aeadfc
DC
7810 while (child_die != NULL && child_die->tag)
7811 {
7812 if (child_die->tag == DW_TAG_member
7813 || child_die->tag == DW_TAG_variable
34eaf542
TT
7814 || child_die->tag == DW_TAG_inheritance
7815 || child_die->tag == DW_TAG_template_value_param
7816 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7817 {
90aeadfc 7818 /* Do nothing. */
134d01f1 7819 }
90aeadfc
DC
7820 else
7821 process_die (child_die, cu);
134d01f1 7822
90aeadfc 7823 child_die = sibling_die (child_die);
134d01f1
DJ
7824 }
7825
fa4028e9
JB
7826 /* Do not consider external references. According to the DWARF standard,
7827 these DIEs are identified by the fact that they have no byte_size
7828 attribute, and a declaration attribute. */
7829 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7830 || !die_is_declaration (die, cu))
c767944b 7831 new_symbol (die, type, cu);
134d01f1
DJ
7832}
7833
7834/* Given a DW_AT_enumeration_type die, set its type. We do not
7835 complete the type's fields yet, or create any symbols. */
c906108c 7836
f792889a 7837static struct type *
134d01f1 7838read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7839{
e7c27a73 7840 struct objfile *objfile = cu->objfile;
c906108c 7841 struct type *type;
c906108c 7842 struct attribute *attr;
0114d602 7843 const char *name;
134d01f1 7844
348e048f
DE
7845 /* If the definition of this type lives in .debug_types, read that type.
7846 Don't follow DW_AT_specification though, that will take us back up
7847 the chain and we want to go down. */
7848 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7849 if (attr)
7850 {
7851 struct dwarf2_cu *type_cu = cu;
7852 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7853
348e048f 7854 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7855
7856 /* TYPE_CU may not be the same as CU.
7857 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7858 return set_die_type (die, type, cu);
7859 }
7860
c906108c
SS
7861 type = alloc_type (objfile);
7862
7863 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7864 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7865 if (name != NULL)
0114d602 7866 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7867
e142c38c 7868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7869 if (attr)
7870 {
7871 TYPE_LENGTH (type) = DW_UNSND (attr);
7872 }
7873 else
7874 {
7875 TYPE_LENGTH (type) = 0;
7876 }
7877
137033e9
JB
7878 /* The enumeration DIE can be incomplete. In Ada, any type can be
7879 declared as private in the package spec, and then defined only
7880 inside the package body. Such types are known as Taft Amendment
7881 Types. When another package uses such a type, an incomplete DIE
7882 may be generated by the compiler. */
02eb380e 7883 if (die_is_declaration (die, cu))
876cecd0 7884 TYPE_STUB (type) = 1;
02eb380e 7885
f792889a 7886 return set_die_type (die, type, cu);
134d01f1
DJ
7887}
7888
7889/* Given a pointer to a die which begins an enumeration, process all
7890 the dies that define the members of the enumeration, and create the
7891 symbol for the enumeration type.
7892
7893 NOTE: We reverse the order of the element list. */
7894
7895static void
7896process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7897{
f792889a 7898 struct type *this_type;
134d01f1 7899
f792889a
DJ
7900 this_type = get_die_type (die, cu);
7901 if (this_type == NULL)
7902 this_type = read_enumeration_type (die, cu);
9dc481d3 7903
639d11d3 7904 if (die->child != NULL)
c906108c 7905 {
9dc481d3
DE
7906 struct die_info *child_die;
7907 struct symbol *sym;
7908 struct field *fields = NULL;
7909 int num_fields = 0;
7910 int unsigned_enum = 1;
7911 char *name;
7912
639d11d3 7913 child_die = die->child;
c906108c
SS
7914 while (child_die && child_die->tag)
7915 {
7916 if (child_die->tag != DW_TAG_enumerator)
7917 {
e7c27a73 7918 process_die (child_die, cu);
c906108c
SS
7919 }
7920 else
7921 {
39cbfefa
DJ
7922 name = dwarf2_name (child_die, cu);
7923 if (name)
c906108c 7924 {
f792889a 7925 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7926 if (SYMBOL_VALUE (sym) < 0)
7927 unsigned_enum = 0;
7928
7929 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7930 {
7931 fields = (struct field *)
7932 xrealloc (fields,
7933 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7934 * sizeof (struct field));
c906108c
SS
7935 }
7936
3567439c 7937 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7938 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7939 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7940 FIELD_BITSIZE (fields[num_fields]) = 0;
7941
7942 num_fields++;
7943 }
7944 }
7945
7946 child_die = sibling_die (child_die);
7947 }
7948
7949 if (num_fields)
7950 {
f792889a
DJ
7951 TYPE_NFIELDS (this_type) = num_fields;
7952 TYPE_FIELDS (this_type) = (struct field *)
7953 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7954 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7955 sizeof (struct field) * num_fields);
b8c9b27d 7956 xfree (fields);
c906108c
SS
7957 }
7958 if (unsigned_enum)
876cecd0 7959 TYPE_UNSIGNED (this_type) = 1;
c906108c 7960 }
134d01f1 7961
6c83ed52
TT
7962 /* If we are reading an enum from a .debug_types unit, and the enum
7963 is a declaration, and the enum is not the signatured type in the
7964 unit, then we do not want to add a symbol for it. Adding a
7965 symbol would in some cases obscure the true definition of the
7966 enum, giving users an incomplete type when the definition is
7967 actually available. Note that we do not want to do this for all
7968 enums which are just declarations, because C++0x allows forward
7969 enum declarations. */
b0df02fd 7970 if (cu->per_cu->debug_types_section
6c83ed52
TT
7971 && die_is_declaration (die, cu))
7972 {
7973 struct signatured_type *type_sig;
7974
7975 type_sig
7976 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 7977 cu->per_cu->debug_types_section,
6c83ed52
TT
7978 cu->per_cu->offset);
7979 if (type_sig->type_offset != die->offset)
7980 return;
7981 }
7982
f792889a 7983 new_symbol (die, this_type, cu);
c906108c
SS
7984}
7985
7986/* Extract all information from a DW_TAG_array_type DIE and put it in
7987 the DIE's type field. For now, this only handles one dimensional
7988 arrays. */
7989
f792889a 7990static struct type *
e7c27a73 7991read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7992{
e7c27a73 7993 struct objfile *objfile = cu->objfile;
c906108c 7994 struct die_info *child_die;
7e314c57 7995 struct type *type;
c906108c
SS
7996 struct type *element_type, *range_type, *index_type;
7997 struct type **range_types = NULL;
7998 struct attribute *attr;
7999 int ndim = 0;
8000 struct cleanup *back_to;
39cbfefa 8001 char *name;
c906108c 8002
e7c27a73 8003 element_type = die_type (die, cu);
c906108c 8004
7e314c57
JK
8005 /* The die_type call above may have already set the type for this DIE. */
8006 type = get_die_type (die, cu);
8007 if (type)
8008 return type;
8009
c906108c
SS
8010 /* Irix 6.2 native cc creates array types without children for
8011 arrays with unspecified length. */
639d11d3 8012 if (die->child == NULL)
c906108c 8013 {
46bf5051 8014 index_type = objfile_type (objfile)->builtin_int;
c906108c 8015 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8016 type = create_array_type (NULL, element_type, range_type);
8017 return set_die_type (die, type, cu);
c906108c
SS
8018 }
8019
8020 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8021 child_die = die->child;
c906108c
SS
8022 while (child_die && child_die->tag)
8023 {
8024 if (child_die->tag == DW_TAG_subrange_type)
8025 {
f792889a 8026 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8027
f792889a 8028 if (child_type != NULL)
a02abb62 8029 {
0963b4bd
MS
8030 /* The range type was succesfully read. Save it for the
8031 array type creation. */
a02abb62
JB
8032 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8033 {
8034 range_types = (struct type **)
8035 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8036 * sizeof (struct type *));
8037 if (ndim == 0)
8038 make_cleanup (free_current_contents, &range_types);
8039 }
f792889a 8040 range_types[ndim++] = child_type;
a02abb62 8041 }
c906108c
SS
8042 }
8043 child_die = sibling_die (child_die);
8044 }
8045
8046 /* Dwarf2 dimensions are output from left to right, create the
8047 necessary array types in backwards order. */
7ca2d3a3 8048
c906108c 8049 type = element_type;
7ca2d3a3
DL
8050
8051 if (read_array_order (die, cu) == DW_ORD_col_major)
8052 {
8053 int i = 0;
9a619af0 8054
7ca2d3a3
DL
8055 while (i < ndim)
8056 type = create_array_type (NULL, type, range_types[i++]);
8057 }
8058 else
8059 {
8060 while (ndim-- > 0)
8061 type = create_array_type (NULL, type, range_types[ndim]);
8062 }
c906108c 8063
f5f8a009
EZ
8064 /* Understand Dwarf2 support for vector types (like they occur on
8065 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8066 array type. This is not part of the Dwarf2/3 standard yet, but a
8067 custom vendor extension. The main difference between a regular
8068 array and the vector variant is that vectors are passed by value
8069 to functions. */
e142c38c 8070 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8071 if (attr)
ea37ba09 8072 make_vector_type (type);
f5f8a009 8073
dbc98a8b
KW
8074 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8075 implementation may choose to implement triple vectors using this
8076 attribute. */
8077 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8078 if (attr)
8079 {
8080 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8081 TYPE_LENGTH (type) = DW_UNSND (attr);
8082 else
3e43a32a
MS
8083 complaint (&symfile_complaints,
8084 _("DW_AT_byte_size for array type smaller "
8085 "than the total size of elements"));
dbc98a8b
KW
8086 }
8087
39cbfefa
DJ
8088 name = dwarf2_name (die, cu);
8089 if (name)
8090 TYPE_NAME (type) = name;
6e70227d 8091
0963b4bd 8092 /* Install the type in the die. */
7e314c57
JK
8093 set_die_type (die, type, cu);
8094
8095 /* set_die_type should be already done. */
b4ba55a1
JB
8096 set_descriptive_type (type, die, cu);
8097
c906108c
SS
8098 do_cleanups (back_to);
8099
7e314c57 8100 return type;
c906108c
SS
8101}
8102
7ca2d3a3 8103static enum dwarf_array_dim_ordering
6e70227d 8104read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8105{
8106 struct attribute *attr;
8107
8108 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8109
8110 if (attr) return DW_SND (attr);
8111
0963b4bd
MS
8112 /* GNU F77 is a special case, as at 08/2004 array type info is the
8113 opposite order to the dwarf2 specification, but data is still
8114 laid out as per normal fortran.
7ca2d3a3 8115
0963b4bd
MS
8116 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8117 version checking. */
7ca2d3a3 8118
905e0470
PM
8119 if (cu->language == language_fortran
8120 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8121 {
8122 return DW_ORD_row_major;
8123 }
8124
6e70227d 8125 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8126 {
8127 case array_column_major:
8128 return DW_ORD_col_major;
8129 case array_row_major:
8130 default:
8131 return DW_ORD_row_major;
8132 };
8133}
8134
72019c9c 8135/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8136 the DIE's type field. */
72019c9c 8137
f792889a 8138static struct type *
72019c9c
GM
8139read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8140{
7e314c57
JK
8141 struct type *domain_type, *set_type;
8142 struct attribute *attr;
f792889a 8143
7e314c57
JK
8144 domain_type = die_type (die, cu);
8145
8146 /* The die_type call above may have already set the type for this DIE. */
8147 set_type = get_die_type (die, cu);
8148 if (set_type)
8149 return set_type;
8150
8151 set_type = create_set_type (NULL, domain_type);
8152
8153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8154 if (attr)
8155 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8156
f792889a 8157 return set_die_type (die, set_type, cu);
72019c9c 8158}
7ca2d3a3 8159
c906108c
SS
8160/* First cut: install each common block member as a global variable. */
8161
8162static void
e7c27a73 8163read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8164{
8165 struct die_info *child_die;
8166 struct attribute *attr;
8167 struct symbol *sym;
8168 CORE_ADDR base = (CORE_ADDR) 0;
8169
e142c38c 8170 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8171 if (attr)
8172 {
0963b4bd 8173 /* Support the .debug_loc offsets. */
8e19ed76
PS
8174 if (attr_form_is_block (attr))
8175 {
e7c27a73 8176 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8177 }
3690dd37 8178 else if (attr_form_is_section_offset (attr))
8e19ed76 8179 {
4d3c2250 8180 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8181 }
8182 else
8183 {
4d3c2250
KB
8184 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8185 "common block member");
8e19ed76 8186 }
c906108c 8187 }
639d11d3 8188 if (die->child != NULL)
c906108c 8189 {
639d11d3 8190 child_die = die->child;
c906108c
SS
8191 while (child_die && child_die->tag)
8192 {
74ac6d43
TT
8193 LONGEST offset;
8194
e7c27a73 8195 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8196 if (sym != NULL
8197 && handle_data_member_location (child_die, cu, &offset))
c906108c 8198 {
74ac6d43 8199 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8200 add_symbol_to_list (sym, &global_symbols);
8201 }
8202 child_die = sibling_die (child_die);
8203 }
8204 }
8205}
8206
0114d602 8207/* Create a type for a C++ namespace. */
d9fa45fe 8208
0114d602
DJ
8209static struct type *
8210read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8211{
e7c27a73 8212 struct objfile *objfile = cu->objfile;
0114d602 8213 const char *previous_prefix, *name;
9219021c 8214 int is_anonymous;
0114d602
DJ
8215 struct type *type;
8216
8217 /* For extensions, reuse the type of the original namespace. */
8218 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8219 {
8220 struct die_info *ext_die;
8221 struct dwarf2_cu *ext_cu = cu;
9a619af0 8222
0114d602
DJ
8223 ext_die = dwarf2_extension (die, &ext_cu);
8224 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8225
8226 /* EXT_CU may not be the same as CU.
8227 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8228 return set_die_type (die, type, cu);
8229 }
9219021c 8230
e142c38c 8231 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8232
8233 /* Now build the name of the current namespace. */
8234
0114d602
DJ
8235 previous_prefix = determine_prefix (die, cu);
8236 if (previous_prefix[0] != '\0')
8237 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8238 previous_prefix, name, 0, cu);
0114d602
DJ
8239
8240 /* Create the type. */
8241 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8242 objfile);
8243 TYPE_NAME (type) = (char *) name;
8244 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8245
60531b24 8246 return set_die_type (die, type, cu);
0114d602
DJ
8247}
8248
8249/* Read a C++ namespace. */
8250
8251static void
8252read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8253{
8254 struct objfile *objfile = cu->objfile;
0114d602 8255 int is_anonymous;
9219021c 8256
5c4e30ca
DC
8257 /* Add a symbol associated to this if we haven't seen the namespace
8258 before. Also, add a using directive if it's an anonymous
8259 namespace. */
9219021c 8260
f2f0e013 8261 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8262 {
8263 struct type *type;
8264
0114d602 8265 type = read_type_die (die, cu);
e7c27a73 8266 new_symbol (die, type, cu);
5c4e30ca 8267
e8e80198 8268 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8269 if (is_anonymous)
0114d602
DJ
8270 {
8271 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8272
c0cc3a76 8273 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8274 NULL, NULL, &objfile->objfile_obstack);
0114d602 8275 }
5c4e30ca 8276 }
9219021c 8277
639d11d3 8278 if (die->child != NULL)
d9fa45fe 8279 {
639d11d3 8280 struct die_info *child_die = die->child;
6e70227d 8281
d9fa45fe
DC
8282 while (child_die && child_die->tag)
8283 {
e7c27a73 8284 process_die (child_die, cu);
d9fa45fe
DC
8285 child_die = sibling_die (child_die);
8286 }
8287 }
38d518c9
EZ
8288}
8289
f55ee35c
JK
8290/* Read a Fortran module as type. This DIE can be only a declaration used for
8291 imported module. Still we need that type as local Fortran "use ... only"
8292 declaration imports depend on the created type in determine_prefix. */
8293
8294static struct type *
8295read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8296{
8297 struct objfile *objfile = cu->objfile;
8298 char *module_name;
8299 struct type *type;
8300
8301 module_name = dwarf2_name (die, cu);
8302 if (!module_name)
3e43a32a
MS
8303 complaint (&symfile_complaints,
8304 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8305 die->offset);
8306 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8307
8308 /* determine_prefix uses TYPE_TAG_NAME. */
8309 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8310
8311 return set_die_type (die, type, cu);
8312}
8313
5d7cb8df
JK
8314/* Read a Fortran module. */
8315
8316static void
8317read_module (struct die_info *die, struct dwarf2_cu *cu)
8318{
8319 struct die_info *child_die = die->child;
8320
5d7cb8df
JK
8321 while (child_die && child_die->tag)
8322 {
8323 process_die (child_die, cu);
8324 child_die = sibling_die (child_die);
8325 }
8326}
8327
38d518c9
EZ
8328/* Return the name of the namespace represented by DIE. Set
8329 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8330 namespace. */
8331
8332static const char *
e142c38c 8333namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8334{
8335 struct die_info *current_die;
8336 const char *name = NULL;
8337
8338 /* Loop through the extensions until we find a name. */
8339
8340 for (current_die = die;
8341 current_die != NULL;
f2f0e013 8342 current_die = dwarf2_extension (die, &cu))
38d518c9 8343 {
e142c38c 8344 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8345 if (name != NULL)
8346 break;
8347 }
8348
8349 /* Is it an anonymous namespace? */
8350
8351 *is_anonymous = (name == NULL);
8352 if (*is_anonymous)
2b1dbab0 8353 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8354
8355 return name;
d9fa45fe
DC
8356}
8357
c906108c
SS
8358/* Extract all information from a DW_TAG_pointer_type DIE and add to
8359 the user defined type vector. */
8360
f792889a 8361static struct type *
e7c27a73 8362read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8363{
5e2b427d 8364 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8365 struct comp_unit_head *cu_header = &cu->header;
c906108c 8366 struct type *type;
8b2dbe47
KB
8367 struct attribute *attr_byte_size;
8368 struct attribute *attr_address_class;
8369 int byte_size, addr_class;
7e314c57
JK
8370 struct type *target_type;
8371
8372 target_type = die_type (die, cu);
c906108c 8373
7e314c57
JK
8374 /* The die_type call above may have already set the type for this DIE. */
8375 type = get_die_type (die, cu);
8376 if (type)
8377 return type;
8378
8379 type = lookup_pointer_type (target_type);
8b2dbe47 8380
e142c38c 8381 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8382 if (attr_byte_size)
8383 byte_size = DW_UNSND (attr_byte_size);
c906108c 8384 else
8b2dbe47
KB
8385 byte_size = cu_header->addr_size;
8386
e142c38c 8387 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8388 if (attr_address_class)
8389 addr_class = DW_UNSND (attr_address_class);
8390 else
8391 addr_class = DW_ADDR_none;
8392
8393 /* If the pointer size or address class is different than the
8394 default, create a type variant marked as such and set the
8395 length accordingly. */
8396 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8397 {
5e2b427d 8398 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8399 {
8400 int type_flags;
8401
849957d9 8402 type_flags = gdbarch_address_class_type_flags
5e2b427d 8403 (gdbarch, byte_size, addr_class);
876cecd0
TT
8404 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8405 == 0);
8b2dbe47
KB
8406 type = make_type_with_address_space (type, type_flags);
8407 }
8408 else if (TYPE_LENGTH (type) != byte_size)
8409 {
3e43a32a
MS
8410 complaint (&symfile_complaints,
8411 _("invalid pointer size %d"), byte_size);
8b2dbe47 8412 }
6e70227d 8413 else
9a619af0
MS
8414 {
8415 /* Should we also complain about unhandled address classes? */
8416 }
c906108c 8417 }
8b2dbe47
KB
8418
8419 TYPE_LENGTH (type) = byte_size;
f792889a 8420 return set_die_type (die, type, cu);
c906108c
SS
8421}
8422
8423/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8424 the user defined type vector. */
8425
f792889a 8426static struct type *
e7c27a73 8427read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8428{
8429 struct type *type;
8430 struct type *to_type;
8431 struct type *domain;
8432
e7c27a73
DJ
8433 to_type = die_type (die, cu);
8434 domain = die_containing_type (die, cu);
0d5de010 8435
7e314c57
JK
8436 /* The calls above may have already set the type for this DIE. */
8437 type = get_die_type (die, cu);
8438 if (type)
8439 return type;
8440
0d5de010
DJ
8441 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8442 type = lookup_methodptr_type (to_type);
8443 else
8444 type = lookup_memberptr_type (to_type, domain);
c906108c 8445
f792889a 8446 return set_die_type (die, type, cu);
c906108c
SS
8447}
8448
8449/* Extract all information from a DW_TAG_reference_type DIE and add to
8450 the user defined type vector. */
8451
f792889a 8452static struct type *
e7c27a73 8453read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8454{
e7c27a73 8455 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8456 struct type *type, *target_type;
c906108c
SS
8457 struct attribute *attr;
8458
7e314c57
JK
8459 target_type = die_type (die, cu);
8460
8461 /* The die_type call above may have already set the type for this DIE. */
8462 type = get_die_type (die, cu);
8463 if (type)
8464 return type;
8465
8466 type = lookup_reference_type (target_type);
e142c38c 8467 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8468 if (attr)
8469 {
8470 TYPE_LENGTH (type) = DW_UNSND (attr);
8471 }
8472 else
8473 {
107d2387 8474 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8475 }
f792889a 8476 return set_die_type (die, type, cu);
c906108c
SS
8477}
8478
f792889a 8479static struct type *
e7c27a73 8480read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8481{
f792889a 8482 struct type *base_type, *cv_type;
c906108c 8483
e7c27a73 8484 base_type = die_type (die, cu);
7e314c57
JK
8485
8486 /* The die_type call above may have already set the type for this DIE. */
8487 cv_type = get_die_type (die, cu);
8488 if (cv_type)
8489 return cv_type;
8490
2f608a3a
KW
8491 /* In case the const qualifier is applied to an array type, the element type
8492 is so qualified, not the array type (section 6.7.3 of C99). */
8493 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8494 {
8495 struct type *el_type, *inner_array;
8496
8497 base_type = copy_type (base_type);
8498 inner_array = base_type;
8499
8500 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8501 {
8502 TYPE_TARGET_TYPE (inner_array) =
8503 copy_type (TYPE_TARGET_TYPE (inner_array));
8504 inner_array = TYPE_TARGET_TYPE (inner_array);
8505 }
8506
8507 el_type = TYPE_TARGET_TYPE (inner_array);
8508 TYPE_TARGET_TYPE (inner_array) =
8509 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8510
8511 return set_die_type (die, base_type, cu);
8512 }
8513
f792889a
DJ
8514 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8515 return set_die_type (die, cv_type, cu);
c906108c
SS
8516}
8517
f792889a 8518static struct type *
e7c27a73 8519read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8520{
f792889a 8521 struct type *base_type, *cv_type;
c906108c 8522
e7c27a73 8523 base_type = die_type (die, cu);
7e314c57
JK
8524
8525 /* The die_type call above may have already set the type for this DIE. */
8526 cv_type = get_die_type (die, cu);
8527 if (cv_type)
8528 return cv_type;
8529
f792889a
DJ
8530 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8531 return set_die_type (die, cv_type, cu);
c906108c
SS
8532}
8533
8534/* Extract all information from a DW_TAG_string_type DIE and add to
8535 the user defined type vector. It isn't really a user defined type,
8536 but it behaves like one, with other DIE's using an AT_user_def_type
8537 attribute to reference it. */
8538
f792889a 8539static struct type *
e7c27a73 8540read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8541{
e7c27a73 8542 struct objfile *objfile = cu->objfile;
3b7538c0 8543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8544 struct type *type, *range_type, *index_type, *char_type;
8545 struct attribute *attr;
8546 unsigned int length;
8547
e142c38c 8548 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8549 if (attr)
8550 {
8551 length = DW_UNSND (attr);
8552 }
8553 else
8554 {
0963b4bd 8555 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8556 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8557 if (attr)
8558 {
8559 length = DW_UNSND (attr);
8560 }
8561 else
8562 {
8563 length = 1;
8564 }
c906108c 8565 }
6ccb9162 8566
46bf5051 8567 index_type = objfile_type (objfile)->builtin_int;
c906108c 8568 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8569 char_type = language_string_char_type (cu->language_defn, gdbarch);
8570 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8571
f792889a 8572 return set_die_type (die, type, cu);
c906108c
SS
8573}
8574
8575/* Handle DIES due to C code like:
8576
8577 struct foo
c5aa993b
JM
8578 {
8579 int (*funcp)(int a, long l);
8580 int b;
8581 };
c906108c 8582
0963b4bd 8583 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8584
f792889a 8585static struct type *
e7c27a73 8586read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8587{
bb5ed363 8588 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8589 struct type *type; /* Type that this function returns. */
8590 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8591 struct attribute *attr;
8592
e7c27a73 8593 type = die_type (die, cu);
7e314c57
JK
8594
8595 /* The die_type call above may have already set the type for this DIE. */
8596 ftype = get_die_type (die, cu);
8597 if (ftype)
8598 return ftype;
8599
0c8b41f1 8600 ftype = lookup_function_type (type);
c906108c 8601
5b8101ae 8602 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8603 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8604 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8605 || cu->language == language_cplus
5b8101ae
PM
8606 || cu->language == language_java
8607 || cu->language == language_pascal)
876cecd0 8608 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8609 else if (producer_is_realview (cu->producer))
8610 /* RealView does not emit DW_AT_prototyped. We can not
8611 distinguish prototyped and unprototyped functions; default to
8612 prototyped, since that is more common in modern code (and
8613 RealView warns about unprototyped functions). */
8614 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8615
c055b101
CV
8616 /* Store the calling convention in the type if it's available in
8617 the subroutine die. Otherwise set the calling convention to
8618 the default value DW_CC_normal. */
8619 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8620 if (attr)
8621 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8622 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8623 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8624 else
8625 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8626
8627 /* We need to add the subroutine type to the die immediately so
8628 we don't infinitely recurse when dealing with parameters
0963b4bd 8629 declared as the same subroutine type. */
76c10ea2 8630 set_die_type (die, ftype, cu);
6e70227d 8631
639d11d3 8632 if (die->child != NULL)
c906108c 8633 {
bb5ed363 8634 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8635 struct die_info *child_die;
8072405b 8636 int nparams, iparams;
c906108c
SS
8637
8638 /* Count the number of parameters.
8639 FIXME: GDB currently ignores vararg functions, but knows about
8640 vararg member functions. */
8072405b 8641 nparams = 0;
639d11d3 8642 child_die = die->child;
c906108c
SS
8643 while (child_die && child_die->tag)
8644 {
8645 if (child_die->tag == DW_TAG_formal_parameter)
8646 nparams++;
8647 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8648 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8649 child_die = sibling_die (child_die);
8650 }
8651
8652 /* Allocate storage for parameters and fill them in. */
8653 TYPE_NFIELDS (ftype) = nparams;
8654 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8655 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8656
8072405b
JK
8657 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8658 even if we error out during the parameters reading below. */
8659 for (iparams = 0; iparams < nparams; iparams++)
8660 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8661
8662 iparams = 0;
639d11d3 8663 child_die = die->child;
c906108c
SS
8664 while (child_die && child_die->tag)
8665 {
8666 if (child_die->tag == DW_TAG_formal_parameter)
8667 {
3ce3b1ba
PA
8668 struct type *arg_type;
8669
8670 /* DWARF version 2 has no clean way to discern C++
8671 static and non-static member functions. G++ helps
8672 GDB by marking the first parameter for non-static
8673 member functions (which is the this pointer) as
8674 artificial. We pass this information to
8675 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8676
8677 DWARF version 3 added DW_AT_object_pointer, which GCC
8678 4.5 does not yet generate. */
e142c38c 8679 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8680 if (attr)
8681 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8682 else
418835cc
KS
8683 {
8684 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8685
8686 /* GCC/43521: In java, the formal parameter
8687 "this" is sometimes not marked with DW_AT_artificial. */
8688 if (cu->language == language_java)
8689 {
8690 const char *name = dwarf2_name (child_die, cu);
9a619af0 8691
418835cc
KS
8692 if (name && !strcmp (name, "this"))
8693 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8694 }
8695 }
3ce3b1ba
PA
8696 arg_type = die_type (child_die, cu);
8697
8698 /* RealView does not mark THIS as const, which the testsuite
8699 expects. GCC marks THIS as const in method definitions,
8700 but not in the class specifications (GCC PR 43053). */
8701 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8702 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8703 {
8704 int is_this = 0;
8705 struct dwarf2_cu *arg_cu = cu;
8706 const char *name = dwarf2_name (child_die, cu);
8707
8708 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8709 if (attr)
8710 {
8711 /* If the compiler emits this, use it. */
8712 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8713 is_this = 1;
8714 }
8715 else if (name && strcmp (name, "this") == 0)
8716 /* Function definitions will have the argument names. */
8717 is_this = 1;
8718 else if (name == NULL && iparams == 0)
8719 /* Declarations may not have the names, so like
8720 elsewhere in GDB, assume an artificial first
8721 argument is "this". */
8722 is_this = 1;
8723
8724 if (is_this)
8725 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8726 arg_type, 0);
8727 }
8728
8729 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8730 iparams++;
8731 }
8732 child_die = sibling_die (child_die);
8733 }
8734 }
8735
76c10ea2 8736 return ftype;
c906108c
SS
8737}
8738
f792889a 8739static struct type *
e7c27a73 8740read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8741{
e7c27a73 8742 struct objfile *objfile = cu->objfile;
0114d602 8743 const char *name = NULL;
3c8e0968 8744 struct type *this_type, *target_type;
c906108c 8745
94af9270 8746 name = dwarf2_full_name (NULL, die, cu);
f792889a 8747 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8748 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8749 TYPE_NAME (this_type) = (char *) name;
f792889a 8750 set_die_type (die, this_type, cu);
3c8e0968
DE
8751 target_type = die_type (die, cu);
8752 if (target_type != this_type)
8753 TYPE_TARGET_TYPE (this_type) = target_type;
8754 else
8755 {
8756 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8757 spec and cause infinite loops in GDB. */
8758 complaint (&symfile_complaints,
8759 _("Self-referential DW_TAG_typedef "
8760 "- DIE at 0x%x [in module %s]"),
bb5ed363 8761 die->offset, objfile->name);
3c8e0968
DE
8762 TYPE_TARGET_TYPE (this_type) = NULL;
8763 }
f792889a 8764 return this_type;
c906108c
SS
8765}
8766
8767/* Find a representation of a given base type and install
8768 it in the TYPE field of the die. */
8769
f792889a 8770static struct type *
e7c27a73 8771read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8772{
e7c27a73 8773 struct objfile *objfile = cu->objfile;
c906108c
SS
8774 struct type *type;
8775 struct attribute *attr;
8776 int encoding = 0, size = 0;
39cbfefa 8777 char *name;
6ccb9162
UW
8778 enum type_code code = TYPE_CODE_INT;
8779 int type_flags = 0;
8780 struct type *target_type = NULL;
c906108c 8781
e142c38c 8782 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8783 if (attr)
8784 {
8785 encoding = DW_UNSND (attr);
8786 }
e142c38c 8787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8788 if (attr)
8789 {
8790 size = DW_UNSND (attr);
8791 }
39cbfefa 8792 name = dwarf2_name (die, cu);
6ccb9162 8793 if (!name)
c906108c 8794 {
6ccb9162
UW
8795 complaint (&symfile_complaints,
8796 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8797 }
6ccb9162
UW
8798
8799 switch (encoding)
c906108c 8800 {
6ccb9162
UW
8801 case DW_ATE_address:
8802 /* Turn DW_ATE_address into a void * pointer. */
8803 code = TYPE_CODE_PTR;
8804 type_flags |= TYPE_FLAG_UNSIGNED;
8805 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8806 break;
8807 case DW_ATE_boolean:
8808 code = TYPE_CODE_BOOL;
8809 type_flags |= TYPE_FLAG_UNSIGNED;
8810 break;
8811 case DW_ATE_complex_float:
8812 code = TYPE_CODE_COMPLEX;
8813 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8814 break;
8815 case DW_ATE_decimal_float:
8816 code = TYPE_CODE_DECFLOAT;
8817 break;
8818 case DW_ATE_float:
8819 code = TYPE_CODE_FLT;
8820 break;
8821 case DW_ATE_signed:
8822 break;
8823 case DW_ATE_unsigned:
8824 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8825 if (cu->language == language_fortran
8826 && name
8827 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8828 code = TYPE_CODE_CHAR;
6ccb9162
UW
8829 break;
8830 case DW_ATE_signed_char:
6e70227d 8831 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8832 || cu->language == language_pascal
8833 || cu->language == language_fortran)
6ccb9162
UW
8834 code = TYPE_CODE_CHAR;
8835 break;
8836 case DW_ATE_unsigned_char:
868a0084 8837 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8838 || cu->language == language_pascal
8839 || cu->language == language_fortran)
6ccb9162
UW
8840 code = TYPE_CODE_CHAR;
8841 type_flags |= TYPE_FLAG_UNSIGNED;
8842 break;
75079b2b
TT
8843 case DW_ATE_UTF:
8844 /* We just treat this as an integer and then recognize the
8845 type by name elsewhere. */
8846 break;
8847
6ccb9162
UW
8848 default:
8849 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8850 dwarf_type_encoding_name (encoding));
8851 break;
c906108c 8852 }
6ccb9162 8853
0114d602
DJ
8854 type = init_type (code, size, type_flags, NULL, objfile);
8855 TYPE_NAME (type) = name;
6ccb9162
UW
8856 TYPE_TARGET_TYPE (type) = target_type;
8857
0114d602 8858 if (name && strcmp (name, "char") == 0)
876cecd0 8859 TYPE_NOSIGN (type) = 1;
0114d602 8860
f792889a 8861 return set_die_type (die, type, cu);
c906108c
SS
8862}
8863
a02abb62
JB
8864/* Read the given DW_AT_subrange DIE. */
8865
f792889a 8866static struct type *
a02abb62
JB
8867read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8868{
8869 struct type *base_type;
8870 struct type *range_type;
8871 struct attribute *attr;
43bbcdc2
PH
8872 LONGEST low = 0;
8873 LONGEST high = -1;
39cbfefa 8874 char *name;
43bbcdc2 8875 LONGEST negative_mask;
e77813c8 8876
a02abb62 8877 base_type = die_type (die, cu);
953ac07e
JK
8878 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8879 check_typedef (base_type);
a02abb62 8880
7e314c57
JK
8881 /* The die_type call above may have already set the type for this DIE. */
8882 range_type = get_die_type (die, cu);
8883 if (range_type)
8884 return range_type;
8885
e142c38c 8886 if (cu->language == language_fortran)
6e70227d 8887 {
a02abb62
JB
8888 /* FORTRAN implies a lower bound of 1, if not given. */
8889 low = 1;
8890 }
8891
dd5e6932
DJ
8892 /* FIXME: For variable sized arrays either of these could be
8893 a variable rather than a constant value. We'll allow it,
8894 but we don't know how to handle it. */
e142c38c 8895 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8896 if (attr)
8897 low = dwarf2_get_attr_constant_value (attr, 0);
8898
e142c38c 8899 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8900 if (attr)
6e70227d 8901 {
d48323d8 8902 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8903 {
8904 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8905 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8906 FIXME: GDB does not yet know how to handle dynamic
8907 arrays properly, treat them as arrays with unspecified
8908 length for now.
8909
8910 FIXME: jimb/2003-09-22: GDB does not really know
8911 how to handle arrays of unspecified length
8912 either; we just represent them as zero-length
8913 arrays. Choose an appropriate upper bound given
8914 the lower bound we've computed above. */
8915 high = low - 1;
8916 }
8917 else
8918 high = dwarf2_get_attr_constant_value (attr, 1);
8919 }
e77813c8
PM
8920 else
8921 {
8922 attr = dwarf2_attr (die, DW_AT_count, cu);
8923 if (attr)
8924 {
8925 int count = dwarf2_get_attr_constant_value (attr, 1);
8926 high = low + count - 1;
8927 }
c2ff108b
JK
8928 else
8929 {
8930 /* Unspecified array length. */
8931 high = low - 1;
8932 }
e77813c8
PM
8933 }
8934
8935 /* Dwarf-2 specifications explicitly allows to create subrange types
8936 without specifying a base type.
8937 In that case, the base type must be set to the type of
8938 the lower bound, upper bound or count, in that order, if any of these
8939 three attributes references an object that has a type.
8940 If no base type is found, the Dwarf-2 specifications say that
8941 a signed integer type of size equal to the size of an address should
8942 be used.
8943 For the following C code: `extern char gdb_int [];'
8944 GCC produces an empty range DIE.
8945 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8946 high bound or count are not yet handled by this code. */
e77813c8
PM
8947 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8948 {
8949 struct objfile *objfile = cu->objfile;
8950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8951 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8952 struct type *int_type = objfile_type (objfile)->builtin_int;
8953
8954 /* Test "int", "long int", and "long long int" objfile types,
8955 and select the first one having a size above or equal to the
8956 architecture address size. */
8957 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8958 base_type = int_type;
8959 else
8960 {
8961 int_type = objfile_type (objfile)->builtin_long;
8962 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8963 base_type = int_type;
8964 else
8965 {
8966 int_type = objfile_type (objfile)->builtin_long_long;
8967 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8968 base_type = int_type;
8969 }
8970 }
8971 }
a02abb62 8972
6e70227d 8973 negative_mask =
43bbcdc2
PH
8974 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8975 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8976 low |= negative_mask;
8977 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8978 high |= negative_mask;
8979
a02abb62
JB
8980 range_type = create_range_type (NULL, base_type, low, high);
8981
bbb0eef6
JK
8982 /* Mark arrays with dynamic length at least as an array of unspecified
8983 length. GDB could check the boundary but before it gets implemented at
8984 least allow accessing the array elements. */
d48323d8 8985 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
8986 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8987
c2ff108b
JK
8988 /* Ada expects an empty array on no boundary attributes. */
8989 if (attr == NULL && cu->language != language_ada)
8990 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8991
39cbfefa
DJ
8992 name = dwarf2_name (die, cu);
8993 if (name)
8994 TYPE_NAME (range_type) = name;
6e70227d 8995
e142c38c 8996 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8997 if (attr)
8998 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8999
7e314c57
JK
9000 set_die_type (die, range_type, cu);
9001
9002 /* set_die_type should be already done. */
b4ba55a1
JB
9003 set_descriptive_type (range_type, die, cu);
9004
7e314c57 9005 return range_type;
a02abb62 9006}
6e70227d 9007
f792889a 9008static struct type *
81a17f79
JB
9009read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9010{
9011 struct type *type;
81a17f79 9012
81a17f79
JB
9013 /* For now, we only support the C meaning of an unspecified type: void. */
9014
0114d602
DJ
9015 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9016 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9017
f792889a 9018 return set_die_type (die, type, cu);
81a17f79 9019}
a02abb62 9020
51545339
DJ
9021/* Trivial hash function for die_info: the hash value of a DIE
9022 is its offset in .debug_info for this objfile. */
9023
9024static hashval_t
9025die_hash (const void *item)
9026{
9027 const struct die_info *die = item;
9a619af0 9028
51545339
DJ
9029 return die->offset;
9030}
9031
9032/* Trivial comparison function for die_info structures: two DIEs
9033 are equal if they have the same offset. */
9034
9035static int
9036die_eq (const void *item_lhs, const void *item_rhs)
9037{
9038 const struct die_info *die_lhs = item_lhs;
9039 const struct die_info *die_rhs = item_rhs;
9a619af0 9040
51545339
DJ
9041 return die_lhs->offset == die_rhs->offset;
9042}
9043
c906108c
SS
9044/* Read a whole compilation unit into a linked list of dies. */
9045
f9aca02d 9046static struct die_info *
93311388 9047read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9048{
93311388 9049 struct die_reader_specs reader_specs;
98bfdba5 9050 int read_abbrevs = 0;
1d9ec526 9051 struct cleanup *back_to = NULL;
98bfdba5
PA
9052 struct die_info *die;
9053
9054 if (cu->dwarf2_abbrevs == NULL)
9055 {
e5fe5e75 9056 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9057 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9058 read_abbrevs = 1;
9059 }
93311388 9060
348e048f 9061 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9062 cu->die_hash
9063 = htab_create_alloc_ex (cu->header.length / 12,
9064 die_hash,
9065 die_eq,
9066 NULL,
9067 &cu->comp_unit_obstack,
9068 hashtab_obstack_allocate,
9069 dummy_obstack_deallocate);
9070
93311388
DE
9071 init_cu_die_reader (&reader_specs, cu);
9072
98bfdba5
PA
9073 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9074
9075 if (read_abbrevs)
9076 do_cleanups (back_to);
9077
9078 return die;
639d11d3
DC
9079}
9080
d97bc12b
DE
9081/* Main entry point for reading a DIE and all children.
9082 Read the DIE and dump it if requested. */
9083
9084static struct die_info *
93311388
DE
9085read_die_and_children (const struct die_reader_specs *reader,
9086 gdb_byte *info_ptr,
d97bc12b
DE
9087 gdb_byte **new_info_ptr,
9088 struct die_info *parent)
9089{
93311388 9090 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9091 new_info_ptr, parent);
9092
9093 if (dwarf2_die_debug)
9094 {
348e048f
DE
9095 fprintf_unfiltered (gdb_stdlog,
9096 "\nRead die from %s of %s:\n",
b0df02fd 9097 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9098 ? ".debug_types"
9099 : ".debug_info"),
348e048f 9100 reader->abfd->filename);
d97bc12b
DE
9101 dump_die (result, dwarf2_die_debug);
9102 }
9103
9104 return result;
9105}
9106
639d11d3
DC
9107/* Read a single die and all its descendents. Set the die's sibling
9108 field to NULL; set other fields in the die correctly, and set all
9109 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9110 location of the info_ptr after reading all of those dies. PARENT
9111 is the parent of the die in question. */
9112
9113static struct die_info *
93311388
DE
9114read_die_and_children_1 (const struct die_reader_specs *reader,
9115 gdb_byte *info_ptr,
d97bc12b
DE
9116 gdb_byte **new_info_ptr,
9117 struct die_info *parent)
639d11d3
DC
9118{
9119 struct die_info *die;
fe1b8b76 9120 gdb_byte *cur_ptr;
639d11d3
DC
9121 int has_children;
9122
93311388 9123 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9124 if (die == NULL)
9125 {
9126 *new_info_ptr = cur_ptr;
9127 return NULL;
9128 }
93311388 9129 store_in_ref_table (die, reader->cu);
639d11d3
DC
9130
9131 if (has_children)
348e048f 9132 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9133 else
9134 {
9135 die->child = NULL;
9136 *new_info_ptr = cur_ptr;
9137 }
9138
9139 die->sibling = NULL;
9140 die->parent = parent;
9141 return die;
9142}
9143
9144/* Read a die, all of its descendents, and all of its siblings; set
9145 all of the fields of all of the dies correctly. Arguments are as
9146 in read_die_and_children. */
9147
9148static struct die_info *
93311388
DE
9149read_die_and_siblings (const struct die_reader_specs *reader,
9150 gdb_byte *info_ptr,
fe1b8b76 9151 gdb_byte **new_info_ptr,
639d11d3
DC
9152 struct die_info *parent)
9153{
9154 struct die_info *first_die, *last_sibling;
fe1b8b76 9155 gdb_byte *cur_ptr;
639d11d3 9156
c906108c 9157 cur_ptr = info_ptr;
639d11d3
DC
9158 first_die = last_sibling = NULL;
9159
9160 while (1)
c906108c 9161 {
639d11d3 9162 struct die_info *die
93311388 9163 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9164
1d325ec1 9165 if (die == NULL)
c906108c 9166 {
639d11d3
DC
9167 *new_info_ptr = cur_ptr;
9168 return first_die;
c906108c 9169 }
1d325ec1
DJ
9170
9171 if (!first_die)
9172 first_die = die;
c906108c 9173 else
1d325ec1
DJ
9174 last_sibling->sibling = die;
9175
9176 last_sibling = die;
c906108c 9177 }
c906108c
SS
9178}
9179
93311388
DE
9180/* Read the die from the .debug_info section buffer. Set DIEP to
9181 point to a newly allocated die with its information, except for its
9182 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9183 whether the die has children or not. */
9184
9185static gdb_byte *
9186read_full_die (const struct die_reader_specs *reader,
9187 struct die_info **diep, gdb_byte *info_ptr,
9188 int *has_children)
9189{
9190 unsigned int abbrev_number, bytes_read, i, offset;
9191 struct abbrev_info *abbrev;
9192 struct die_info *die;
9193 struct dwarf2_cu *cu = reader->cu;
9194 bfd *abfd = reader->abfd;
9195
9196 offset = info_ptr - reader->buffer;
9197 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9198 info_ptr += bytes_read;
9199 if (!abbrev_number)
9200 {
9201 *diep = NULL;
9202 *has_children = 0;
9203 return info_ptr;
9204 }
9205
9206 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9207 if (!abbrev)
348e048f
DE
9208 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9209 abbrev_number,
9210 bfd_get_filename (abfd));
9211
93311388
DE
9212 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9213 die->offset = offset;
9214 die->tag = abbrev->tag;
9215 die->abbrev = abbrev_number;
9216
9217 die->num_attrs = abbrev->num_attrs;
9218
9219 for (i = 0; i < abbrev->num_attrs; ++i)
9220 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9221 abfd, info_ptr, cu);
9222
9223 *diep = die;
9224 *has_children = abbrev->has_children;
9225 return info_ptr;
9226}
9227
c906108c
SS
9228/* In DWARF version 2, the description of the debugging information is
9229 stored in a separate .debug_abbrev section. Before we read any
9230 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9231 in a hash table. This function also sets flags in CU describing
9232 the data found in the abbrev table. */
c906108c
SS
9233
9234static void
e5fe5e75 9235dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9236{
e5fe5e75 9237 bfd *abfd = cu->objfile->obfd;
e7c27a73 9238 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9239 gdb_byte *abbrev_ptr;
c906108c
SS
9240 struct abbrev_info *cur_abbrev;
9241 unsigned int abbrev_number, bytes_read, abbrev_name;
9242 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9243 struct attr_abbrev *cur_attrs;
9244 unsigned int allocated_attrs;
c906108c 9245
0963b4bd 9246 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9247 obstack_init (&cu->abbrev_obstack);
9248 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9249 (ABBREV_HASH_SIZE
9250 * sizeof (struct abbrev_info *)));
9251 memset (cu->dwarf2_abbrevs, 0,
9252 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9253
be391dca
TT
9254 dwarf2_read_section (dwarf2_per_objfile->objfile,
9255 &dwarf2_per_objfile->abbrev);
dce234bc 9256 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9257 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9258 abbrev_ptr += bytes_read;
9259
f3dd6933
DJ
9260 allocated_attrs = ATTR_ALLOC_CHUNK;
9261 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9262
0963b4bd 9263 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9264 while (abbrev_number)
9265 {
f3dd6933 9266 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9267
9268 /* read in abbrev header */
9269 cur_abbrev->number = abbrev_number;
9270 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9271 abbrev_ptr += bytes_read;
9272 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9273 abbrev_ptr += 1;
9274
72bf9492
DJ
9275 if (cur_abbrev->tag == DW_TAG_namespace)
9276 cu->has_namespace_info = 1;
9277
c906108c
SS
9278 /* now read in declarations */
9279 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9280 abbrev_ptr += bytes_read;
9281 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9282 abbrev_ptr += bytes_read;
9283 while (abbrev_name)
9284 {
f3dd6933 9285 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9286 {
f3dd6933
DJ
9287 allocated_attrs += ATTR_ALLOC_CHUNK;
9288 cur_attrs
9289 = xrealloc (cur_attrs, (allocated_attrs
9290 * sizeof (struct attr_abbrev)));
c906108c 9291 }
ae038cb0
DJ
9292
9293 /* Record whether this compilation unit might have
9294 inter-compilation-unit references. If we don't know what form
9295 this attribute will have, then it might potentially be a
9296 DW_FORM_ref_addr, so we conservatively expect inter-CU
9297 references. */
9298
9299 if (abbrev_form == DW_FORM_ref_addr
9300 || abbrev_form == DW_FORM_indirect)
9301 cu->has_form_ref_addr = 1;
9302
f3dd6933
DJ
9303 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9304 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9305 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9306 abbrev_ptr += bytes_read;
9307 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9308 abbrev_ptr += bytes_read;
9309 }
9310
f3dd6933
DJ
9311 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9312 (cur_abbrev->num_attrs
9313 * sizeof (struct attr_abbrev)));
9314 memcpy (cur_abbrev->attrs, cur_attrs,
9315 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9316
c906108c 9317 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9318 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9319 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9320
9321 /* Get next abbreviation.
9322 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9323 always properly terminated with an abbrev number of 0.
9324 Exit loop if we encounter an abbreviation which we have
9325 already read (which means we are about to read the abbreviations
9326 for the next compile unit) or if the end of the abbreviation
9327 table is reached. */
dce234bc
PP
9328 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9329 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9330 break;
9331 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9332 abbrev_ptr += bytes_read;
e7c27a73 9333 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9334 break;
9335 }
f3dd6933
DJ
9336
9337 xfree (cur_attrs);
c906108c
SS
9338}
9339
f3dd6933 9340/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9341
c906108c 9342static void
f3dd6933 9343dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9344{
f3dd6933 9345 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9346
f3dd6933
DJ
9347 obstack_free (&cu->abbrev_obstack, NULL);
9348 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9349}
9350
9351/* Lookup an abbrev_info structure in the abbrev hash table. */
9352
9353static struct abbrev_info *
e7c27a73 9354dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9355{
9356 unsigned int hash_number;
9357 struct abbrev_info *abbrev;
9358
9359 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9360 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9361
9362 while (abbrev)
9363 {
9364 if (abbrev->number == number)
9365 return abbrev;
9366 else
9367 abbrev = abbrev->next;
9368 }
9369 return NULL;
9370}
9371
72bf9492
DJ
9372/* Returns nonzero if TAG represents a type that we might generate a partial
9373 symbol for. */
9374
9375static int
9376is_type_tag_for_partial (int tag)
9377{
9378 switch (tag)
9379 {
9380#if 0
9381 /* Some types that would be reasonable to generate partial symbols for,
9382 that we don't at present. */
9383 case DW_TAG_array_type:
9384 case DW_TAG_file_type:
9385 case DW_TAG_ptr_to_member_type:
9386 case DW_TAG_set_type:
9387 case DW_TAG_string_type:
9388 case DW_TAG_subroutine_type:
9389#endif
9390 case DW_TAG_base_type:
9391 case DW_TAG_class_type:
680b30c7 9392 case DW_TAG_interface_type:
72bf9492
DJ
9393 case DW_TAG_enumeration_type:
9394 case DW_TAG_structure_type:
9395 case DW_TAG_subrange_type:
9396 case DW_TAG_typedef:
9397 case DW_TAG_union_type:
9398 return 1;
9399 default:
9400 return 0;
9401 }
9402}
9403
9404/* Load all DIEs that are interesting for partial symbols into memory. */
9405
9406static struct partial_die_info *
93311388
DE
9407load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9408 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9409{
bb5ed363 9410 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9411 struct partial_die_info *part_die;
9412 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9413 struct abbrev_info *abbrev;
9414 unsigned int bytes_read;
5afb4e99 9415 unsigned int load_all = 0;
72bf9492
DJ
9416
9417 int nesting_level = 1;
9418
9419 parent_die = NULL;
9420 last_die = NULL;
9421
5afb4e99
DJ
9422 if (cu->per_cu && cu->per_cu->load_all_dies)
9423 load_all = 1;
9424
72bf9492
DJ
9425 cu->partial_dies
9426 = htab_create_alloc_ex (cu->header.length / 12,
9427 partial_die_hash,
9428 partial_die_eq,
9429 NULL,
9430 &cu->comp_unit_obstack,
9431 hashtab_obstack_allocate,
9432 dummy_obstack_deallocate);
9433
9434 part_die = obstack_alloc (&cu->comp_unit_obstack,
9435 sizeof (struct partial_die_info));
9436
9437 while (1)
9438 {
9439 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9440
9441 /* A NULL abbrev means the end of a series of children. */
9442 if (abbrev == NULL)
9443 {
9444 if (--nesting_level == 0)
9445 {
9446 /* PART_DIE was probably the last thing allocated on the
9447 comp_unit_obstack, so we could call obstack_free
9448 here. We don't do that because the waste is small,
9449 and will be cleaned up when we're done with this
9450 compilation unit. This way, we're also more robust
9451 against other users of the comp_unit_obstack. */
9452 return first_die;
9453 }
9454 info_ptr += bytes_read;
9455 last_die = parent_die;
9456 parent_die = parent_die->die_parent;
9457 continue;
9458 }
9459
98bfdba5
PA
9460 /* Check for template arguments. We never save these; if
9461 they're seen, we just mark the parent, and go on our way. */
9462 if (parent_die != NULL
9463 && cu->language == language_cplus
9464 && (abbrev->tag == DW_TAG_template_type_param
9465 || abbrev->tag == DW_TAG_template_value_param))
9466 {
9467 parent_die->has_template_arguments = 1;
9468
9469 if (!load_all)
9470 {
9471 /* We don't need a partial DIE for the template argument. */
9472 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9473 cu);
9474 continue;
9475 }
9476 }
9477
9478 /* We only recurse into subprograms looking for template arguments.
9479 Skip their other children. */
9480 if (!load_all
9481 && cu->language == language_cplus
9482 && parent_die != NULL
9483 && parent_die->tag == DW_TAG_subprogram)
9484 {
9485 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9486 continue;
9487 }
9488
5afb4e99
DJ
9489 /* Check whether this DIE is interesting enough to save. Normally
9490 we would not be interested in members here, but there may be
9491 later variables referencing them via DW_AT_specification (for
9492 static members). */
9493 if (!load_all
9494 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9495 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9496 && abbrev->tag != DW_TAG_enumerator
9497 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9498 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9499 && abbrev->tag != DW_TAG_variable
5afb4e99 9500 && abbrev->tag != DW_TAG_namespace
f55ee35c 9501 && abbrev->tag != DW_TAG_module
5afb4e99 9502 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9503 {
9504 /* Otherwise we skip to the next sibling, if any. */
93311388 9505 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9506 continue;
9507 }
9508
93311388
DE
9509 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9510 buffer, info_ptr, cu);
72bf9492
DJ
9511
9512 /* This two-pass algorithm for processing partial symbols has a
9513 high cost in cache pressure. Thus, handle some simple cases
9514 here which cover the majority of C partial symbols. DIEs
9515 which neither have specification tags in them, nor could have
9516 specification tags elsewhere pointing at them, can simply be
9517 processed and discarded.
9518
9519 This segment is also optional; scan_partial_symbols and
9520 add_partial_symbol will handle these DIEs if we chain
9521 them in normally. When compilers which do not emit large
9522 quantities of duplicate debug information are more common,
9523 this code can probably be removed. */
9524
9525 /* Any complete simple types at the top level (pretty much all
9526 of them, for a language without namespaces), can be processed
9527 directly. */
9528 if (parent_die == NULL
9529 && part_die->has_specification == 0
9530 && part_die->is_declaration == 0
d8228535 9531 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9532 || part_die->tag == DW_TAG_base_type
9533 || part_die->tag == DW_TAG_subrange_type))
9534 {
9535 if (building_psymtab && part_die->name != NULL)
04a679b8 9536 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9537 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9538 &objfile->static_psymbols,
9539 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9540 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9541 continue;
9542 }
9543
d8228535
JK
9544 /* The exception for DW_TAG_typedef with has_children above is
9545 a workaround of GCC PR debug/47510. In the case of this complaint
9546 type_name_no_tag_or_error will error on such types later.
9547
9548 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9549 it could not find the child DIEs referenced later, this is checked
9550 above. In correct DWARF DW_TAG_typedef should have no children. */
9551
9552 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9553 complaint (&symfile_complaints,
9554 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9555 "- DIE at 0x%x [in module %s]"),
bb5ed363 9556 part_die->offset, objfile->name);
d8228535 9557
72bf9492
DJ
9558 /* If we're at the second level, and we're an enumerator, and
9559 our parent has no specification (meaning possibly lives in a
9560 namespace elsewhere), then we can add the partial symbol now
9561 instead of queueing it. */
9562 if (part_die->tag == DW_TAG_enumerator
9563 && parent_die != NULL
9564 && parent_die->die_parent == NULL
9565 && parent_die->tag == DW_TAG_enumeration_type
9566 && parent_die->has_specification == 0)
9567 {
9568 if (part_die->name == NULL)
3e43a32a
MS
9569 complaint (&symfile_complaints,
9570 _("malformed enumerator DIE ignored"));
72bf9492 9571 else if (building_psymtab)
04a679b8 9572 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9573 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9574 (cu->language == language_cplus
9575 || cu->language == language_java)
bb5ed363
DE
9576 ? &objfile->global_psymbols
9577 : &objfile->static_psymbols,
9578 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9579
93311388 9580 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9581 continue;
9582 }
9583
9584 /* We'll save this DIE so link it in. */
9585 part_die->die_parent = parent_die;
9586 part_die->die_sibling = NULL;
9587 part_die->die_child = NULL;
9588
9589 if (last_die && last_die == parent_die)
9590 last_die->die_child = part_die;
9591 else if (last_die)
9592 last_die->die_sibling = part_die;
9593
9594 last_die = part_die;
9595
9596 if (first_die == NULL)
9597 first_die = part_die;
9598
9599 /* Maybe add the DIE to the hash table. Not all DIEs that we
9600 find interesting need to be in the hash table, because we
9601 also have the parent/sibling/child chains; only those that we
9602 might refer to by offset later during partial symbol reading.
9603
9604 For now this means things that might have be the target of a
9605 DW_AT_specification, DW_AT_abstract_origin, or
9606 DW_AT_extension. DW_AT_extension will refer only to
9607 namespaces; DW_AT_abstract_origin refers to functions (and
9608 many things under the function DIE, but we do not recurse
9609 into function DIEs during partial symbol reading) and
9610 possibly variables as well; DW_AT_specification refers to
9611 declarations. Declarations ought to have the DW_AT_declaration
9612 flag. It happens that GCC forgets to put it in sometimes, but
9613 only for functions, not for types.
9614
9615 Adding more things than necessary to the hash table is harmless
9616 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9617 wasted time in find_partial_die, when we reread the compilation
9618 unit with load_all_dies set. */
72bf9492 9619
5afb4e99 9620 if (load_all
72929c62 9621 || abbrev->tag == DW_TAG_constant
5afb4e99 9622 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9623 || abbrev->tag == DW_TAG_variable
9624 || abbrev->tag == DW_TAG_namespace
9625 || part_die->is_declaration)
9626 {
9627 void **slot;
9628
9629 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9630 part_die->offset, INSERT);
9631 *slot = part_die;
9632 }
9633
9634 part_die = obstack_alloc (&cu->comp_unit_obstack,
9635 sizeof (struct partial_die_info));
9636
9637 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9638 we have no reason to follow the children of structures; for other
98bfdba5
PA
9639 languages we have to, so that we can get at method physnames
9640 to infer fully qualified class names, for DW_AT_specification,
9641 and for C++ template arguments. For C++, we also look one level
9642 inside functions to find template arguments (if the name of the
9643 function does not already contain the template arguments).
bc30ff58
JB
9644
9645 For Ada, we need to scan the children of subprograms and lexical
9646 blocks as well because Ada allows the definition of nested
9647 entities that could be interesting for the debugger, such as
9648 nested subprograms for instance. */
72bf9492 9649 if (last_die->has_children
5afb4e99
DJ
9650 && (load_all
9651 || last_die->tag == DW_TAG_namespace
f55ee35c 9652 || last_die->tag == DW_TAG_module
72bf9492 9653 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9654 || (cu->language == language_cplus
9655 && last_die->tag == DW_TAG_subprogram
9656 && (last_die->name == NULL
9657 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9658 || (cu->language != language_c
9659 && (last_die->tag == DW_TAG_class_type
680b30c7 9660 || last_die->tag == DW_TAG_interface_type
72bf9492 9661 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9662 || last_die->tag == DW_TAG_union_type))
9663 || (cu->language == language_ada
9664 && (last_die->tag == DW_TAG_subprogram
9665 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9666 {
9667 nesting_level++;
9668 parent_die = last_die;
9669 continue;
9670 }
9671
9672 /* Otherwise we skip to the next sibling, if any. */
93311388 9673 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9674
9675 /* Back to the top, do it again. */
9676 }
9677}
9678
c906108c
SS
9679/* Read a minimal amount of information into the minimal die structure. */
9680
fe1b8b76 9681static gdb_byte *
72bf9492
DJ
9682read_partial_die (struct partial_die_info *part_die,
9683 struct abbrev_info *abbrev,
9684 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9685 gdb_byte *buffer, gdb_byte *info_ptr,
9686 struct dwarf2_cu *cu)
c906108c 9687{
bb5ed363 9688 struct objfile *objfile = cu->objfile;
fa238c03 9689 unsigned int i;
c906108c 9690 struct attribute attr;
c5aa993b 9691 int has_low_pc_attr = 0;
c906108c
SS
9692 int has_high_pc_attr = 0;
9693
72bf9492 9694 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9695
93311388 9696 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9697
9698 info_ptr += abbrev_len;
9699
9700 if (abbrev == NULL)
9701 return info_ptr;
9702
c906108c
SS
9703 part_die->tag = abbrev->tag;
9704 part_die->has_children = abbrev->has_children;
c906108c
SS
9705
9706 for (i = 0; i < abbrev->num_attrs; ++i)
9707 {
e7c27a73 9708 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9709
9710 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9711 partial symbol table. */
c906108c
SS
9712 switch (attr.name)
9713 {
9714 case DW_AT_name:
71c25dea
TT
9715 switch (part_die->tag)
9716 {
9717 case DW_TAG_compile_unit:
348e048f 9718 case DW_TAG_type_unit:
71c25dea
TT
9719 /* Compilation units have a DW_AT_name that is a filename, not
9720 a source language identifier. */
9721 case DW_TAG_enumeration_type:
9722 case DW_TAG_enumerator:
9723 /* These tags always have simple identifiers already; no need
9724 to canonicalize them. */
9725 part_die->name = DW_STRING (&attr);
9726 break;
9727 default:
9728 part_die->name
9729 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9730 &objfile->objfile_obstack);
71c25dea
TT
9731 break;
9732 }
c906108c 9733 break;
31ef98ae 9734 case DW_AT_linkage_name:
c906108c 9735 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9736 /* Note that both forms of linkage name might appear. We
9737 assume they will be the same, and we only store the last
9738 one we see. */
94af9270
KS
9739 if (cu->language == language_ada)
9740 part_die->name = DW_STRING (&attr);
abc72ce4 9741 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9742 break;
9743 case DW_AT_low_pc:
9744 has_low_pc_attr = 1;
9745 part_die->lowpc = DW_ADDR (&attr);
9746 break;
9747 case DW_AT_high_pc:
9748 has_high_pc_attr = 1;
9749 part_die->highpc = DW_ADDR (&attr);
9750 break;
9751 case DW_AT_location:
0963b4bd 9752 /* Support the .debug_loc offsets. */
8e19ed76
PS
9753 if (attr_form_is_block (&attr))
9754 {
9755 part_die->locdesc = DW_BLOCK (&attr);
9756 }
3690dd37 9757 else if (attr_form_is_section_offset (&attr))
8e19ed76 9758 {
4d3c2250 9759 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9760 }
9761 else
9762 {
4d3c2250
KB
9763 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9764 "partial symbol information");
8e19ed76 9765 }
c906108c 9766 break;
c906108c
SS
9767 case DW_AT_external:
9768 part_die->is_external = DW_UNSND (&attr);
9769 break;
9770 case DW_AT_declaration:
9771 part_die->is_declaration = DW_UNSND (&attr);
9772 break;
9773 case DW_AT_type:
9774 part_die->has_type = 1;
9775 break;
9776 case DW_AT_abstract_origin:
9777 case DW_AT_specification:
72bf9492
DJ
9778 case DW_AT_extension:
9779 part_die->has_specification = 1;
c764a876 9780 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9781 break;
9782 case DW_AT_sibling:
9783 /* Ignore absolute siblings, they might point outside of
9784 the current compile unit. */
9785 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9786 complaint (&symfile_complaints,
9787 _("ignoring absolute DW_AT_sibling"));
c906108c 9788 else
93311388 9789 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9790 break;
fa4028e9
JB
9791 case DW_AT_byte_size:
9792 part_die->has_byte_size = 1;
9793 break;
68511cec
CES
9794 case DW_AT_calling_convention:
9795 /* DWARF doesn't provide a way to identify a program's source-level
9796 entry point. DW_AT_calling_convention attributes are only meant
9797 to describe functions' calling conventions.
9798
9799 However, because it's a necessary piece of information in
9800 Fortran, and because DW_CC_program is the only piece of debugging
9801 information whose definition refers to a 'main program' at all,
9802 several compilers have begun marking Fortran main programs with
9803 DW_CC_program --- even when those functions use the standard
9804 calling conventions.
9805
9806 So until DWARF specifies a way to provide this information and
9807 compilers pick up the new representation, we'll support this
9808 practice. */
9809 if (DW_UNSND (&attr) == DW_CC_program
9810 && cu->language == language_fortran)
01f8c46d
JK
9811 {
9812 set_main_name (part_die->name);
9813
9814 /* As this DIE has a static linkage the name would be difficult
9815 to look up later. */
9816 language_of_main = language_fortran;
9817 }
68511cec 9818 break;
c906108c
SS
9819 default:
9820 break;
9821 }
9822 }
9823
9373cf26
JK
9824 if (has_low_pc_attr && has_high_pc_attr)
9825 {
9826 /* When using the GNU linker, .gnu.linkonce. sections are used to
9827 eliminate duplicate copies of functions and vtables and such.
9828 The linker will arbitrarily choose one and discard the others.
9829 The AT_*_pc values for such functions refer to local labels in
9830 these sections. If the section from that file was discarded, the
9831 labels are not in the output, so the relocs get a value of 0.
9832 If this is a discarded function, mark the pc bounds as invalid,
9833 so that GDB will ignore it. */
9834 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9835 {
bb5ed363 9836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9837
9838 complaint (&symfile_complaints,
9839 _("DW_AT_low_pc %s is zero "
9840 "for DIE at 0x%x [in module %s]"),
9841 paddress (gdbarch, part_die->lowpc),
bb5ed363 9842 part_die->offset, objfile->name);
9373cf26
JK
9843 }
9844 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9845 else if (part_die->lowpc >= part_die->highpc)
9846 {
bb5ed363 9847 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9848
9849 complaint (&symfile_complaints,
9850 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9851 "for DIE at 0x%x [in module %s]"),
9852 paddress (gdbarch, part_die->lowpc),
9853 paddress (gdbarch, part_die->highpc),
bb5ed363 9854 part_die->offset, objfile->name);
9373cf26
JK
9855 }
9856 else
9857 part_die->has_pc_info = 1;
9858 }
85cbf3d3 9859
c906108c
SS
9860 return info_ptr;
9861}
9862
72bf9492
DJ
9863/* Find a cached partial DIE at OFFSET in CU. */
9864
9865static struct partial_die_info *
c764a876 9866find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9867{
9868 struct partial_die_info *lookup_die = NULL;
9869 struct partial_die_info part_die;
9870
9871 part_die.offset = offset;
9872 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9873
72bf9492
DJ
9874 return lookup_die;
9875}
9876
348e048f
DE
9877/* Find a partial DIE at OFFSET, which may or may not be in CU,
9878 except in the case of .debug_types DIEs which do not reference
9879 outside their CU (they do however referencing other types via
55f1336d 9880 DW_FORM_ref_sig8). */
72bf9492
DJ
9881
9882static struct partial_die_info *
c764a876 9883find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9884{
bb5ed363 9885 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9886 struct dwarf2_per_cu_data *per_cu = NULL;
9887 struct partial_die_info *pd = NULL;
72bf9492 9888
b0df02fd 9889 if (cu->per_cu->debug_types_section)
348e048f
DE
9890 {
9891 pd = find_partial_die_in_comp_unit (offset, cu);
9892 if (pd != NULL)
9893 return pd;
9894 goto not_found;
9895 }
9896
45452591 9897 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9898 {
9899 pd = find_partial_die_in_comp_unit (offset, cu);
9900 if (pd != NULL)
9901 return pd;
9902 }
72bf9492 9903
bb5ed363 9904 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 9905
98bfdba5 9906 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 9907 load_partial_comp_unit (per_cu);
ae038cb0
DJ
9908
9909 per_cu->cu->last_used = 0;
5afb4e99
DJ
9910 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9911
9912 if (pd == NULL && per_cu->load_all_dies == 0)
9913 {
9914 struct cleanup *back_to;
9915 struct partial_die_info comp_unit_die;
9916 struct abbrev_info *abbrev;
9917 unsigned int bytes_read;
9918 char *info_ptr;
9919
9920 per_cu->load_all_dies = 1;
9921
9922 /* Re-read the DIEs. */
9923 back_to = make_cleanup (null_cleanup, 0);
9924 if (per_cu->cu->dwarf2_abbrevs == NULL)
9925 {
e5fe5e75 9926 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 9927 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9928 }
dce234bc 9929 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9930 + per_cu->cu->header.offset
9931 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9932 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9933 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 9934 objfile->obfd,
93311388 9935 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9936 per_cu->cu);
9937 if (comp_unit_die.has_children)
bb5ed363 9938 load_partial_dies (objfile->obfd,
93311388
DE
9939 dwarf2_per_objfile->info.buffer, info_ptr,
9940 0, per_cu->cu);
5afb4e99
DJ
9941 do_cleanups (back_to);
9942
9943 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9944 }
9945
348e048f
DE
9946 not_found:
9947
5afb4e99
DJ
9948 if (pd == NULL)
9949 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9950 _("could not find partial DIE 0x%x "
9951 "in cache [from module %s]\n"),
bb5ed363 9952 offset, bfd_get_filename (objfile->obfd));
5afb4e99 9953 return pd;
72bf9492
DJ
9954}
9955
abc72ce4
DE
9956/* See if we can figure out if the class lives in a namespace. We do
9957 this by looking for a member function; its demangled name will
9958 contain namespace info, if there is any. */
9959
9960static void
9961guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9962 struct dwarf2_cu *cu)
9963{
9964 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9965 what template types look like, because the demangler
9966 frequently doesn't give the same name as the debug info. We
9967 could fix this by only using the demangled name to get the
9968 prefix (but see comment in read_structure_type). */
9969
9970 struct partial_die_info *real_pdi;
9971 struct partial_die_info *child_pdi;
9972
9973 /* If this DIE (this DIE's specification, if any) has a parent, then
9974 we should not do this. We'll prepend the parent's fully qualified
9975 name when we create the partial symbol. */
9976
9977 real_pdi = struct_pdi;
9978 while (real_pdi->has_specification)
9979 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9980
9981 if (real_pdi->die_parent != NULL)
9982 return;
9983
9984 for (child_pdi = struct_pdi->die_child;
9985 child_pdi != NULL;
9986 child_pdi = child_pdi->die_sibling)
9987 {
9988 if (child_pdi->tag == DW_TAG_subprogram
9989 && child_pdi->linkage_name != NULL)
9990 {
9991 char *actual_class_name
9992 = language_class_name_from_physname (cu->language_defn,
9993 child_pdi->linkage_name);
9994 if (actual_class_name != NULL)
9995 {
9996 struct_pdi->name
9997 = obsavestring (actual_class_name,
9998 strlen (actual_class_name),
9999 &cu->objfile->objfile_obstack);
10000 xfree (actual_class_name);
10001 }
10002 break;
10003 }
10004 }
10005}
10006
72bf9492
DJ
10007/* Adjust PART_DIE before generating a symbol for it. This function
10008 may set the is_external flag or change the DIE's name. */
10009
10010static void
10011fixup_partial_die (struct partial_die_info *part_die,
10012 struct dwarf2_cu *cu)
10013{
abc72ce4
DE
10014 /* Once we've fixed up a die, there's no point in doing so again.
10015 This also avoids a memory leak if we were to call
10016 guess_partial_die_structure_name multiple times. */
10017 if (part_die->fixup_called)
10018 return;
10019
72bf9492
DJ
10020 /* If we found a reference attribute and the DIE has no name, try
10021 to find a name in the referred to DIE. */
10022
10023 if (part_die->name == NULL && part_die->has_specification)
10024 {
10025 struct partial_die_info *spec_die;
72bf9492 10026
10b3939b 10027 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10028
10b3939b 10029 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10030
10031 if (spec_die->name)
10032 {
10033 part_die->name = spec_die->name;
10034
10035 /* Copy DW_AT_external attribute if it is set. */
10036 if (spec_die->is_external)
10037 part_die->is_external = spec_die->is_external;
10038 }
10039 }
10040
10041 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10042
10043 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10044 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10045
abc72ce4
DE
10046 /* If there is no parent die to provide a namespace, and there are
10047 children, see if we can determine the namespace from their linkage
10048 name.
10049 NOTE: We need to do this even if cu->has_namespace_info != 0.
10050 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10051 if (cu->language == language_cplus
8b70b953 10052 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10053 && part_die->die_parent == NULL
10054 && part_die->has_children
10055 && (part_die->tag == DW_TAG_class_type
10056 || part_die->tag == DW_TAG_structure_type
10057 || part_die->tag == DW_TAG_union_type))
10058 guess_partial_die_structure_name (part_die, cu);
10059
53832f31
TT
10060 /* GCC might emit a nameless struct or union that has a linkage
10061 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10062 if (part_die->name == NULL
96408a79
SA
10063 && (part_die->tag == DW_TAG_class_type
10064 || part_die->tag == DW_TAG_interface_type
10065 || part_die->tag == DW_TAG_structure_type
10066 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10067 && part_die->linkage_name != NULL)
10068 {
10069 char *demangled;
10070
10071 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10072 if (demangled)
10073 {
96408a79
SA
10074 const char *base;
10075
10076 /* Strip any leading namespaces/classes, keep only the base name.
10077 DW_AT_name for named DIEs does not contain the prefixes. */
10078 base = strrchr (demangled, ':');
10079 if (base && base > demangled && base[-1] == ':')
10080 base++;
10081 else
10082 base = demangled;
10083
10084 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10085 &cu->objfile->objfile_obstack);
10086 xfree (demangled);
10087 }
10088 }
10089
abc72ce4 10090 part_die->fixup_called = 1;
72bf9492
DJ
10091}
10092
a8329558 10093/* Read an attribute value described by an attribute form. */
c906108c 10094
fe1b8b76 10095static gdb_byte *
a8329558 10096read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10097 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10098 struct dwarf2_cu *cu)
c906108c 10099{
e7c27a73 10100 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10101 unsigned int bytes_read;
10102 struct dwarf_block *blk;
10103
a8329558
KW
10104 attr->form = form;
10105 switch (form)
c906108c 10106 {
c906108c 10107 case DW_FORM_ref_addr:
ae411497
TT
10108 if (cu->header.version == 2)
10109 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10110 else
3e43a32a
MS
10111 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10112 &cu->header, &bytes_read);
ae411497
TT
10113 info_ptr += bytes_read;
10114 break;
10115 case DW_FORM_addr:
e7c27a73 10116 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10117 info_ptr += bytes_read;
c906108c
SS
10118 break;
10119 case DW_FORM_block2:
7b5a2f43 10120 blk = dwarf_alloc_block (cu);
c906108c
SS
10121 blk->size = read_2_bytes (abfd, info_ptr);
10122 info_ptr += 2;
10123 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10124 info_ptr += blk->size;
10125 DW_BLOCK (attr) = blk;
10126 break;
10127 case DW_FORM_block4:
7b5a2f43 10128 blk = dwarf_alloc_block (cu);
c906108c
SS
10129 blk->size = read_4_bytes (abfd, info_ptr);
10130 info_ptr += 4;
10131 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10132 info_ptr += blk->size;
10133 DW_BLOCK (attr) = blk;
10134 break;
10135 case DW_FORM_data2:
10136 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10137 info_ptr += 2;
10138 break;
10139 case DW_FORM_data4:
10140 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10141 info_ptr += 4;
10142 break;
10143 case DW_FORM_data8:
10144 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10145 info_ptr += 8;
10146 break;
2dc7f7b3
TT
10147 case DW_FORM_sec_offset:
10148 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10149 info_ptr += bytes_read;
10150 break;
c906108c 10151 case DW_FORM_string:
9b1c24c8 10152 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10153 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10154 info_ptr += bytes_read;
10155 break;
4bdf3d34
JJ
10156 case DW_FORM_strp:
10157 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10158 &bytes_read);
8285870a 10159 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10160 info_ptr += bytes_read;
10161 break;
2dc7f7b3 10162 case DW_FORM_exprloc:
c906108c 10163 case DW_FORM_block:
7b5a2f43 10164 blk = dwarf_alloc_block (cu);
c906108c
SS
10165 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10166 info_ptr += bytes_read;
10167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10168 info_ptr += blk->size;
10169 DW_BLOCK (attr) = blk;
10170 break;
10171 case DW_FORM_block1:
7b5a2f43 10172 blk = dwarf_alloc_block (cu);
c906108c
SS
10173 blk->size = read_1_byte (abfd, info_ptr);
10174 info_ptr += 1;
10175 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10176 info_ptr += blk->size;
10177 DW_BLOCK (attr) = blk;
10178 break;
10179 case DW_FORM_data1:
10180 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10181 info_ptr += 1;
10182 break;
10183 case DW_FORM_flag:
10184 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10185 info_ptr += 1;
10186 break;
2dc7f7b3
TT
10187 case DW_FORM_flag_present:
10188 DW_UNSND (attr) = 1;
10189 break;
c906108c
SS
10190 case DW_FORM_sdata:
10191 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10192 info_ptr += bytes_read;
10193 break;
10194 case DW_FORM_udata:
10195 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10196 info_ptr += bytes_read;
10197 break;
10198 case DW_FORM_ref1:
10b3939b 10199 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10200 info_ptr += 1;
10201 break;
10202 case DW_FORM_ref2:
10b3939b 10203 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10204 info_ptr += 2;
10205 break;
10206 case DW_FORM_ref4:
10b3939b 10207 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10208 info_ptr += 4;
10209 break;
613e1657 10210 case DW_FORM_ref8:
10b3939b 10211 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10212 info_ptr += 8;
10213 break;
55f1336d 10214 case DW_FORM_ref_sig8:
348e048f
DE
10215 /* Convert the signature to something we can record in DW_UNSND
10216 for later lookup.
10217 NOTE: This is NULL if the type wasn't found. */
10218 DW_SIGNATURED_TYPE (attr) =
10219 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10220 info_ptr += 8;
10221 break;
c906108c 10222 case DW_FORM_ref_udata:
10b3939b
DJ
10223 DW_ADDR (attr) = (cu->header.offset
10224 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10225 info_ptr += bytes_read;
10226 break;
c906108c 10227 case DW_FORM_indirect:
a8329558
KW
10228 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10229 info_ptr += bytes_read;
e7c27a73 10230 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10231 break;
c906108c 10232 default:
8a3fe4f8 10233 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10234 dwarf_form_name (form),
10235 bfd_get_filename (abfd));
c906108c 10236 }
28e94949
JB
10237
10238 /* We have seen instances where the compiler tried to emit a byte
10239 size attribute of -1 which ended up being encoded as an unsigned
10240 0xffffffff. Although 0xffffffff is technically a valid size value,
10241 an object of this size seems pretty unlikely so we can relatively
10242 safely treat these cases as if the size attribute was invalid and
10243 treat them as zero by default. */
10244 if (attr->name == DW_AT_byte_size
10245 && form == DW_FORM_data4
10246 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10247 {
10248 complaint
10249 (&symfile_complaints,
43bbcdc2
PH
10250 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10251 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10252 DW_UNSND (attr) = 0;
10253 }
28e94949 10254
c906108c
SS
10255 return info_ptr;
10256}
10257
a8329558
KW
10258/* Read an attribute described by an abbreviated attribute. */
10259
fe1b8b76 10260static gdb_byte *
a8329558 10261read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10262 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10263{
10264 attr->name = abbrev->name;
e7c27a73 10265 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10266}
10267
0963b4bd 10268/* Read dwarf information from a buffer. */
c906108c
SS
10269
10270static unsigned int
fe1b8b76 10271read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10272{
fe1b8b76 10273 return bfd_get_8 (abfd, buf);
c906108c
SS
10274}
10275
10276static int
fe1b8b76 10277read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10278{
fe1b8b76 10279 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10280}
10281
10282static unsigned int
fe1b8b76 10283read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10284{
fe1b8b76 10285 return bfd_get_16 (abfd, buf);
c906108c
SS
10286}
10287
21ae7a4d
JK
10288static int
10289read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10290{
10291 return bfd_get_signed_16 (abfd, buf);
10292}
10293
c906108c 10294static unsigned int
fe1b8b76 10295read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10296{
fe1b8b76 10297 return bfd_get_32 (abfd, buf);
c906108c
SS
10298}
10299
21ae7a4d
JK
10300static int
10301read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10302{
10303 return bfd_get_signed_32 (abfd, buf);
10304}
10305
93311388 10306static ULONGEST
fe1b8b76 10307read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10308{
fe1b8b76 10309 return bfd_get_64 (abfd, buf);
c906108c
SS
10310}
10311
10312static CORE_ADDR
fe1b8b76 10313read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10314 unsigned int *bytes_read)
c906108c 10315{
e7c27a73 10316 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10317 CORE_ADDR retval = 0;
10318
107d2387 10319 if (cu_header->signed_addr_p)
c906108c 10320 {
107d2387
AC
10321 switch (cu_header->addr_size)
10322 {
10323 case 2:
fe1b8b76 10324 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10325 break;
10326 case 4:
fe1b8b76 10327 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10328 break;
10329 case 8:
fe1b8b76 10330 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10331 break;
10332 default:
8e65ff28 10333 internal_error (__FILE__, __LINE__,
e2e0b3e5 10334 _("read_address: bad switch, signed [in module %s]"),
659b0389 10335 bfd_get_filename (abfd));
107d2387
AC
10336 }
10337 }
10338 else
10339 {
10340 switch (cu_header->addr_size)
10341 {
10342 case 2:
fe1b8b76 10343 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10344 break;
10345 case 4:
fe1b8b76 10346 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10347 break;
10348 case 8:
fe1b8b76 10349 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10350 break;
10351 default:
8e65ff28 10352 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10353 _("read_address: bad switch, "
10354 "unsigned [in module %s]"),
659b0389 10355 bfd_get_filename (abfd));
107d2387 10356 }
c906108c 10357 }
64367e0a 10358
107d2387
AC
10359 *bytes_read = cu_header->addr_size;
10360 return retval;
c906108c
SS
10361}
10362
f7ef9339 10363/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10364 specification allows the initial length to take up either 4 bytes
10365 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10366 bytes describe the length and all offsets will be 8 bytes in length
10367 instead of 4.
10368
f7ef9339
KB
10369 An older, non-standard 64-bit format is also handled by this
10370 function. The older format in question stores the initial length
10371 as an 8-byte quantity without an escape value. Lengths greater
10372 than 2^32 aren't very common which means that the initial 4 bytes
10373 is almost always zero. Since a length value of zero doesn't make
10374 sense for the 32-bit format, this initial zero can be considered to
10375 be an escape value which indicates the presence of the older 64-bit
10376 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10377 greater than 4GB. If it becomes necessary to handle lengths
10378 somewhat larger than 4GB, we could allow other small values (such
10379 as the non-sensical values of 1, 2, and 3) to also be used as
10380 escape values indicating the presence of the old format.
f7ef9339 10381
917c78fc
MK
10382 The value returned via bytes_read should be used to increment the
10383 relevant pointer after calling read_initial_length().
c764a876 10384
613e1657
KB
10385 [ Note: read_initial_length() and read_offset() are based on the
10386 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10387 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10388 from:
10389
f7ef9339 10390 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10391
613e1657
KB
10392 This document is only a draft and is subject to change. (So beware.)
10393
f7ef9339 10394 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10395 determined empirically by examining 64-bit ELF files produced by
10396 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10397
10398 - Kevin, July 16, 2002
613e1657
KB
10399 ] */
10400
10401static LONGEST
c764a876 10402read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10403{
fe1b8b76 10404 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10405
dd373385 10406 if (length == 0xffffffff)
613e1657 10407 {
fe1b8b76 10408 length = bfd_get_64 (abfd, buf + 4);
613e1657 10409 *bytes_read = 12;
613e1657 10410 }
dd373385 10411 else if (length == 0)
f7ef9339 10412 {
dd373385 10413 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10414 length = bfd_get_64 (abfd, buf);
f7ef9339 10415 *bytes_read = 8;
f7ef9339 10416 }
613e1657
KB
10417 else
10418 {
10419 *bytes_read = 4;
613e1657
KB
10420 }
10421
c764a876
DE
10422 return length;
10423}
dd373385 10424
c764a876
DE
10425/* Cover function for read_initial_length.
10426 Returns the length of the object at BUF, and stores the size of the
10427 initial length in *BYTES_READ and stores the size that offsets will be in
10428 *OFFSET_SIZE.
10429 If the initial length size is not equivalent to that specified in
10430 CU_HEADER then issue a complaint.
10431 This is useful when reading non-comp-unit headers. */
dd373385 10432
c764a876
DE
10433static LONGEST
10434read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10435 const struct comp_unit_head *cu_header,
10436 unsigned int *bytes_read,
10437 unsigned int *offset_size)
10438{
10439 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10440
10441 gdb_assert (cu_header->initial_length_size == 4
10442 || cu_header->initial_length_size == 8
10443 || cu_header->initial_length_size == 12);
10444
10445 if (cu_header->initial_length_size != *bytes_read)
10446 complaint (&symfile_complaints,
10447 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10448
c764a876 10449 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10450 return length;
613e1657
KB
10451}
10452
10453/* Read an offset from the data stream. The size of the offset is
917c78fc 10454 given by cu_header->offset_size. */
613e1657
KB
10455
10456static LONGEST
fe1b8b76 10457read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10458 unsigned int *bytes_read)
c764a876
DE
10459{
10460 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10461
c764a876
DE
10462 *bytes_read = cu_header->offset_size;
10463 return offset;
10464}
10465
10466/* Read an offset from the data stream. */
10467
10468static LONGEST
10469read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10470{
10471 LONGEST retval = 0;
10472
c764a876 10473 switch (offset_size)
613e1657
KB
10474 {
10475 case 4:
fe1b8b76 10476 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10477 break;
10478 case 8:
fe1b8b76 10479 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10480 break;
10481 default:
8e65ff28 10482 internal_error (__FILE__, __LINE__,
c764a876 10483 _("read_offset_1: bad switch [in module %s]"),
659b0389 10484 bfd_get_filename (abfd));
613e1657
KB
10485 }
10486
917c78fc 10487 return retval;
613e1657
KB
10488}
10489
fe1b8b76
JB
10490static gdb_byte *
10491read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10492{
10493 /* If the size of a host char is 8 bits, we can return a pointer
10494 to the buffer, otherwise we have to copy the data to a buffer
10495 allocated on the temporary obstack. */
4bdf3d34 10496 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10497 return buf;
c906108c
SS
10498}
10499
10500static char *
9b1c24c8 10501read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10502{
10503 /* If the size of a host char is 8 bits, we can return a pointer
10504 to the string, otherwise we have to copy the string to a buffer
10505 allocated on the temporary obstack. */
4bdf3d34 10506 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10507 if (*buf == '\0')
10508 {
10509 *bytes_read_ptr = 1;
10510 return NULL;
10511 }
fe1b8b76
JB
10512 *bytes_read_ptr = strlen ((char *) buf) + 1;
10513 return (char *) buf;
4bdf3d34
JJ
10514}
10515
10516static char *
cf2c3c16 10517read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10518{
be391dca 10519 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10520 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10521 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10522 bfd_get_filename (abfd));
dce234bc 10523 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10524 error (_("DW_FORM_strp pointing outside of "
10525 ".debug_str section [in module %s]"),
10526 bfd_get_filename (abfd));
4bdf3d34 10527 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10528 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10529 return NULL;
dce234bc 10530 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10531}
10532
cf2c3c16
TT
10533static char *
10534read_indirect_string (bfd *abfd, gdb_byte *buf,
10535 const struct comp_unit_head *cu_header,
10536 unsigned int *bytes_read_ptr)
10537{
10538 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10539
10540 return read_indirect_string_at_offset (abfd, str_offset);
10541}
10542
ce5d95e1 10543static unsigned long
fe1b8b76 10544read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10545{
ce5d95e1
JB
10546 unsigned long result;
10547 unsigned int num_read;
c906108c
SS
10548 int i, shift;
10549 unsigned char byte;
10550
10551 result = 0;
10552 shift = 0;
10553 num_read = 0;
10554 i = 0;
10555 while (1)
10556 {
fe1b8b76 10557 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10558 buf++;
10559 num_read++;
ce5d95e1 10560 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10561 if ((byte & 128) == 0)
10562 {
10563 break;
10564 }
10565 shift += 7;
10566 }
10567 *bytes_read_ptr = num_read;
10568 return result;
10569}
10570
ce5d95e1 10571static long
fe1b8b76 10572read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10573{
ce5d95e1 10574 long result;
77e0b926 10575 int i, shift, num_read;
c906108c
SS
10576 unsigned char byte;
10577
10578 result = 0;
10579 shift = 0;
c906108c
SS
10580 num_read = 0;
10581 i = 0;
10582 while (1)
10583 {
fe1b8b76 10584 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10585 buf++;
10586 num_read++;
ce5d95e1 10587 result |= ((long)(byte & 127) << shift);
c906108c
SS
10588 shift += 7;
10589 if ((byte & 128) == 0)
10590 {
10591 break;
10592 }
10593 }
77e0b926
DJ
10594 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10595 result |= -(((long)1) << shift);
c906108c
SS
10596 *bytes_read_ptr = num_read;
10597 return result;
10598}
10599
4bb7a0a7
DJ
10600/* Return a pointer to just past the end of an LEB128 number in BUF. */
10601
fe1b8b76
JB
10602static gdb_byte *
10603skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10604{
10605 int byte;
10606
10607 while (1)
10608 {
fe1b8b76 10609 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10610 buf++;
10611 if ((byte & 128) == 0)
10612 return buf;
10613 }
10614}
10615
c906108c 10616static void
e142c38c 10617set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10618{
10619 switch (lang)
10620 {
10621 case DW_LANG_C89:
76bee0cc 10622 case DW_LANG_C99:
c906108c 10623 case DW_LANG_C:
e142c38c 10624 cu->language = language_c;
c906108c
SS
10625 break;
10626 case DW_LANG_C_plus_plus:
e142c38c 10627 cu->language = language_cplus;
c906108c 10628 break;
6aecb9c2
JB
10629 case DW_LANG_D:
10630 cu->language = language_d;
10631 break;
c906108c
SS
10632 case DW_LANG_Fortran77:
10633 case DW_LANG_Fortran90:
b21b22e0 10634 case DW_LANG_Fortran95:
e142c38c 10635 cu->language = language_fortran;
c906108c
SS
10636 break;
10637 case DW_LANG_Mips_Assembler:
e142c38c 10638 cu->language = language_asm;
c906108c 10639 break;
bebd888e 10640 case DW_LANG_Java:
e142c38c 10641 cu->language = language_java;
bebd888e 10642 break;
c906108c 10643 case DW_LANG_Ada83:
8aaf0b47 10644 case DW_LANG_Ada95:
bc5f45f8
JB
10645 cu->language = language_ada;
10646 break;
72019c9c
GM
10647 case DW_LANG_Modula2:
10648 cu->language = language_m2;
10649 break;
fe8e67fd
PM
10650 case DW_LANG_Pascal83:
10651 cu->language = language_pascal;
10652 break;
22566fbd
DJ
10653 case DW_LANG_ObjC:
10654 cu->language = language_objc;
10655 break;
c906108c
SS
10656 case DW_LANG_Cobol74:
10657 case DW_LANG_Cobol85:
c906108c 10658 default:
e142c38c 10659 cu->language = language_minimal;
c906108c
SS
10660 break;
10661 }
e142c38c 10662 cu->language_defn = language_def (cu->language);
c906108c
SS
10663}
10664
10665/* Return the named attribute or NULL if not there. */
10666
10667static struct attribute *
e142c38c 10668dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10669{
10670 unsigned int i;
10671 struct attribute *spec = NULL;
10672
10673 for (i = 0; i < die->num_attrs; ++i)
10674 {
10675 if (die->attrs[i].name == name)
10b3939b 10676 return &die->attrs[i];
c906108c
SS
10677 if (die->attrs[i].name == DW_AT_specification
10678 || die->attrs[i].name == DW_AT_abstract_origin)
10679 spec = &die->attrs[i];
10680 }
c906108c 10681
10b3939b 10682 if (spec)
f2f0e013
DJ
10683 {
10684 die = follow_die_ref (die, spec, &cu);
10685 return dwarf2_attr (die, name, cu);
10686 }
c5aa993b 10687
c906108c
SS
10688 return NULL;
10689}
10690
348e048f
DE
10691/* Return the named attribute or NULL if not there,
10692 but do not follow DW_AT_specification, etc.
10693 This is for use in contexts where we're reading .debug_types dies.
10694 Following DW_AT_specification, DW_AT_abstract_origin will take us
10695 back up the chain, and we want to go down. */
10696
10697static struct attribute *
10698dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10699 struct dwarf2_cu *cu)
10700{
10701 unsigned int i;
10702
10703 for (i = 0; i < die->num_attrs; ++i)
10704 if (die->attrs[i].name == name)
10705 return &die->attrs[i];
10706
10707 return NULL;
10708}
10709
05cf31d1
JB
10710/* Return non-zero iff the attribute NAME is defined for the given DIE,
10711 and holds a non-zero value. This function should only be used for
2dc7f7b3 10712 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10713
10714static int
10715dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10716{
10717 struct attribute *attr = dwarf2_attr (die, name, cu);
10718
10719 return (attr && DW_UNSND (attr));
10720}
10721
3ca72b44 10722static int
e142c38c 10723die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10724{
05cf31d1
JB
10725 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10726 which value is non-zero. However, we have to be careful with
10727 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10728 (via dwarf2_flag_true_p) follows this attribute. So we may
10729 end up accidently finding a declaration attribute that belongs
10730 to a different DIE referenced by the specification attribute,
10731 even though the given DIE does not have a declaration attribute. */
10732 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10733 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10734}
10735
63d06c5c 10736/* Return the die giving the specification for DIE, if there is
f2f0e013 10737 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10738 containing the return value on output. If there is no
10739 specification, but there is an abstract origin, that is
10740 returned. */
63d06c5c
DC
10741
10742static struct die_info *
f2f0e013 10743die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10744{
f2f0e013
DJ
10745 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10746 *spec_cu);
63d06c5c 10747
edb3359d
DJ
10748 if (spec_attr == NULL)
10749 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10750
63d06c5c
DC
10751 if (spec_attr == NULL)
10752 return NULL;
10753 else
f2f0e013 10754 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10755}
c906108c 10756
debd256d 10757/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10758 refers to.
10759 NOTE: This is also used as a "cleanup" function. */
10760
debd256d
JB
10761static void
10762free_line_header (struct line_header *lh)
10763{
10764 if (lh->standard_opcode_lengths)
a8bc7b56 10765 xfree (lh->standard_opcode_lengths);
debd256d
JB
10766
10767 /* Remember that all the lh->file_names[i].name pointers are
10768 pointers into debug_line_buffer, and don't need to be freed. */
10769 if (lh->file_names)
a8bc7b56 10770 xfree (lh->file_names);
debd256d
JB
10771
10772 /* Similarly for the include directory names. */
10773 if (lh->include_dirs)
a8bc7b56 10774 xfree (lh->include_dirs);
debd256d 10775
a8bc7b56 10776 xfree (lh);
debd256d
JB
10777}
10778
debd256d 10779/* Add an entry to LH's include directory table. */
ae2de4f8 10780
debd256d
JB
10781static void
10782add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10783{
debd256d
JB
10784 /* Grow the array if necessary. */
10785 if (lh->include_dirs_size == 0)
c5aa993b 10786 {
debd256d
JB
10787 lh->include_dirs_size = 1; /* for testing */
10788 lh->include_dirs = xmalloc (lh->include_dirs_size
10789 * sizeof (*lh->include_dirs));
10790 }
10791 else if (lh->num_include_dirs >= lh->include_dirs_size)
10792 {
10793 lh->include_dirs_size *= 2;
10794 lh->include_dirs = xrealloc (lh->include_dirs,
10795 (lh->include_dirs_size
10796 * sizeof (*lh->include_dirs)));
c5aa993b 10797 }
c906108c 10798
debd256d
JB
10799 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10800}
6e70227d 10801
debd256d 10802/* Add an entry to LH's file name table. */
ae2de4f8 10803
debd256d
JB
10804static void
10805add_file_name (struct line_header *lh,
10806 char *name,
10807 unsigned int dir_index,
10808 unsigned int mod_time,
10809 unsigned int length)
10810{
10811 struct file_entry *fe;
10812
10813 /* Grow the array if necessary. */
10814 if (lh->file_names_size == 0)
10815 {
10816 lh->file_names_size = 1; /* for testing */
10817 lh->file_names = xmalloc (lh->file_names_size
10818 * sizeof (*lh->file_names));
10819 }
10820 else if (lh->num_file_names >= lh->file_names_size)
10821 {
10822 lh->file_names_size *= 2;
10823 lh->file_names = xrealloc (lh->file_names,
10824 (lh->file_names_size
10825 * sizeof (*lh->file_names)));
10826 }
10827
10828 fe = &lh->file_names[lh->num_file_names++];
10829 fe->name = name;
10830 fe->dir_index = dir_index;
10831 fe->mod_time = mod_time;
10832 fe->length = length;
aaa75496 10833 fe->included_p = 0;
cb1df416 10834 fe->symtab = NULL;
debd256d 10835}
6e70227d 10836
debd256d 10837/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10838 .debug_line, according to the endianness of ABFD. Return a pointer
10839 to a struct line_header, allocated using xmalloc.
debd256d
JB
10840
10841 NOTE: the strings in the include directory and file name tables of
10842 the returned object point into debug_line_buffer, and must not be
10843 freed. */
ae2de4f8 10844
debd256d
JB
10845static struct line_header *
10846dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10847 struct dwarf2_cu *cu)
debd256d
JB
10848{
10849 struct cleanup *back_to;
10850 struct line_header *lh;
fe1b8b76 10851 gdb_byte *line_ptr;
c764a876 10852 unsigned int bytes_read, offset_size;
debd256d
JB
10853 int i;
10854 char *cur_dir, *cur_file;
10855
be391dca 10856 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10857 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10858 {
e2e0b3e5 10859 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10860 return 0;
10861 }
10862
a738430d
MK
10863 /* Make sure that at least there's room for the total_length field.
10864 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10865 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10866 {
4d3c2250 10867 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10868 return 0;
10869 }
10870
10871 lh = xmalloc (sizeof (*lh));
10872 memset (lh, 0, sizeof (*lh));
10873 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10874 (void *) lh);
10875
dce234bc 10876 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10877
a738430d 10878 /* Read in the header. */
6e70227d 10879 lh->total_length =
c764a876
DE
10880 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10881 &bytes_read, &offset_size);
debd256d 10882 line_ptr += bytes_read;
dce234bc
PP
10883 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10884 + dwarf2_per_objfile->line.size))
debd256d 10885 {
4d3c2250 10886 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10887 return 0;
10888 }
10889 lh->statement_program_end = line_ptr + lh->total_length;
10890 lh->version = read_2_bytes (abfd, line_ptr);
10891 line_ptr += 2;
c764a876
DE
10892 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10893 line_ptr += offset_size;
debd256d
JB
10894 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10895 line_ptr += 1;
2dc7f7b3
TT
10896 if (lh->version >= 4)
10897 {
10898 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10899 line_ptr += 1;
10900 }
10901 else
10902 lh->maximum_ops_per_instruction = 1;
10903
10904 if (lh->maximum_ops_per_instruction == 0)
10905 {
10906 lh->maximum_ops_per_instruction = 1;
10907 complaint (&symfile_complaints,
3e43a32a
MS
10908 _("invalid maximum_ops_per_instruction "
10909 "in `.debug_line' section"));
2dc7f7b3
TT
10910 }
10911
debd256d
JB
10912 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10913 line_ptr += 1;
10914 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10915 line_ptr += 1;
10916 lh->line_range = read_1_byte (abfd, line_ptr);
10917 line_ptr += 1;
10918 lh->opcode_base = read_1_byte (abfd, line_ptr);
10919 line_ptr += 1;
10920 lh->standard_opcode_lengths
fe1b8b76 10921 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10922
10923 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10924 for (i = 1; i < lh->opcode_base; ++i)
10925 {
10926 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10927 line_ptr += 1;
10928 }
10929
a738430d 10930 /* Read directory table. */
9b1c24c8 10931 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10932 {
10933 line_ptr += bytes_read;
10934 add_include_dir (lh, cur_dir);
10935 }
10936 line_ptr += bytes_read;
10937
a738430d 10938 /* Read file name table. */
9b1c24c8 10939 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10940 {
10941 unsigned int dir_index, mod_time, length;
10942
10943 line_ptr += bytes_read;
10944 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10945 line_ptr += bytes_read;
10946 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10947 line_ptr += bytes_read;
10948 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10949 line_ptr += bytes_read;
10950
10951 add_file_name (lh, cur_file, dir_index, mod_time, length);
10952 }
10953 line_ptr += bytes_read;
6e70227d 10954 lh->statement_program_start = line_ptr;
debd256d 10955
dce234bc
PP
10956 if (line_ptr > (dwarf2_per_objfile->line.buffer
10957 + dwarf2_per_objfile->line.size))
4d3c2250 10958 complaint (&symfile_complaints,
3e43a32a
MS
10959 _("line number info header doesn't "
10960 "fit in `.debug_line' section"));
debd256d
JB
10961
10962 discard_cleanups (back_to);
10963 return lh;
10964}
c906108c 10965
c6da4cef
DE
10966/* Subroutine of dwarf_decode_lines to simplify it.
10967 Return the file name of the psymtab for included file FILE_INDEX
10968 in line header LH of PST.
10969 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10970 If space for the result is malloc'd, it will be freed by a cleanup.
10971 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10972
10973static char *
10974psymtab_include_file_name (const struct line_header *lh, int file_index,
10975 const struct partial_symtab *pst,
10976 const char *comp_dir)
10977{
10978 const struct file_entry fe = lh->file_names [file_index];
10979 char *include_name = fe.name;
10980 char *include_name_to_compare = include_name;
10981 char *dir_name = NULL;
72b9f47f
TT
10982 const char *pst_filename;
10983 char *copied_name = NULL;
c6da4cef
DE
10984 int file_is_pst;
10985
10986 if (fe.dir_index)
10987 dir_name = lh->include_dirs[fe.dir_index - 1];
10988
10989 if (!IS_ABSOLUTE_PATH (include_name)
10990 && (dir_name != NULL || comp_dir != NULL))
10991 {
10992 /* Avoid creating a duplicate psymtab for PST.
10993 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10994 Before we do the comparison, however, we need to account
10995 for DIR_NAME and COMP_DIR.
10996 First prepend dir_name (if non-NULL). If we still don't
10997 have an absolute path prepend comp_dir (if non-NULL).
10998 However, the directory we record in the include-file's
10999 psymtab does not contain COMP_DIR (to match the
11000 corresponding symtab(s)).
11001
11002 Example:
11003
11004 bash$ cd /tmp
11005 bash$ gcc -g ./hello.c
11006 include_name = "hello.c"
11007 dir_name = "."
11008 DW_AT_comp_dir = comp_dir = "/tmp"
11009 DW_AT_name = "./hello.c" */
11010
11011 if (dir_name != NULL)
11012 {
11013 include_name = concat (dir_name, SLASH_STRING,
11014 include_name, (char *)NULL);
11015 include_name_to_compare = include_name;
11016 make_cleanup (xfree, include_name);
11017 }
11018 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11019 {
11020 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11021 include_name, (char *)NULL);
11022 }
11023 }
11024
11025 pst_filename = pst->filename;
11026 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11027 {
72b9f47f
TT
11028 copied_name = concat (pst->dirname, SLASH_STRING,
11029 pst_filename, (char *)NULL);
11030 pst_filename = copied_name;
c6da4cef
DE
11031 }
11032
1e3fad37 11033 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11034
11035 if (include_name_to_compare != include_name)
11036 xfree (include_name_to_compare);
72b9f47f
TT
11037 if (copied_name != NULL)
11038 xfree (copied_name);
c6da4cef
DE
11039
11040 if (file_is_pst)
11041 return NULL;
11042 return include_name;
11043}
11044
c91513d8
PP
11045/* Ignore this record_line request. */
11046
11047static void
11048noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11049{
11050 return;
11051}
11052
aaa75496
JB
11053/* Decode the Line Number Program (LNP) for the given line_header
11054 structure and CU. The actual information extracted and the type
11055 of structures created from the LNP depends on the value of PST.
11056
11057 1. If PST is NULL, then this procedure uses the data from the program
11058 to create all necessary symbol tables, and their linetables.
6e70227d 11059
aaa75496
JB
11060 2. If PST is not NULL, this procedure reads the program to determine
11061 the list of files included by the unit represented by PST, and
c6da4cef
DE
11062 builds all the associated partial symbol tables.
11063
11064 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11065 It is used for relative paths in the line table.
11066 NOTE: When processing partial symtabs (pst != NULL),
11067 comp_dir == pst->dirname.
11068
11069 NOTE: It is important that psymtabs have the same file name (via strcmp)
11070 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11071 symtab we don't use it in the name of the psymtabs we create.
11072 E.g. expand_line_sal requires this when finding psymtabs to expand.
11073 A good testcase for this is mb-inline.exp. */
debd256d 11074
c906108c 11075static void
72b9f47f 11076dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11077 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11078{
a8c50c1f 11079 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11080 gdb_byte *line_end;
a8c50c1f 11081 unsigned int bytes_read, extended_len;
c906108c 11082 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11083 CORE_ADDR baseaddr;
11084 struct objfile *objfile = cu->objfile;
fbf65064 11085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11086 const int decode_for_pst_p = (pst != NULL);
cb1df416 11087 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11088 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11089 = record_line;
e142c38c
DJ
11090
11091 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11092
debd256d
JB
11093 line_ptr = lh->statement_program_start;
11094 line_end = lh->statement_program_end;
c906108c
SS
11095
11096 /* Read the statement sequences until there's nothing left. */
11097 while (line_ptr < line_end)
11098 {
11099 /* state machine registers */
11100 CORE_ADDR address = 0;
11101 unsigned int file = 1;
11102 unsigned int line = 1;
11103 unsigned int column = 0;
debd256d 11104 int is_stmt = lh->default_is_stmt;
c906108c
SS
11105 int basic_block = 0;
11106 int end_sequence = 0;
fbf65064 11107 CORE_ADDR addr;
2dc7f7b3 11108 unsigned char op_index = 0;
c906108c 11109
aaa75496 11110 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11111 {
aaa75496 11112 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11113 /* lh->include_dirs and lh->file_names are 0-based, but the
11114 directory and file name numbers in the statement program
11115 are 1-based. */
11116 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11117 char *dir = NULL;
a738430d 11118
debd256d
JB
11119 if (fe->dir_index)
11120 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11121
11122 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11123 }
11124
a738430d 11125 /* Decode the table. */
c5aa993b 11126 while (!end_sequence)
c906108c
SS
11127 {
11128 op_code = read_1_byte (abfd, line_ptr);
11129 line_ptr += 1;
59205f5a
JB
11130 if (line_ptr > line_end)
11131 {
11132 dwarf2_debug_line_missing_end_sequence_complaint ();
11133 break;
11134 }
9aa1fe7e 11135
debd256d 11136 if (op_code >= lh->opcode_base)
6e70227d 11137 {
a738430d 11138 /* Special operand. */
debd256d 11139 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11140 address += (((op_index + (adj_opcode / lh->line_range))
11141 / lh->maximum_ops_per_instruction)
11142 * lh->minimum_instruction_length);
11143 op_index = ((op_index + (adj_opcode / lh->line_range))
11144 % lh->maximum_ops_per_instruction);
debd256d 11145 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11146 if (lh->num_file_names < file || file == 0)
25e43795 11147 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11148 /* For now we ignore lines not starting on an
11149 instruction boundary. */
11150 else if (op_index == 0)
25e43795
DJ
11151 {
11152 lh->file_names[file - 1].included_p = 1;
ca5f395d 11153 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11154 {
11155 if (last_subfile != current_subfile)
11156 {
11157 addr = gdbarch_addr_bits_remove (gdbarch, address);
11158 if (last_subfile)
c91513d8 11159 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11160 last_subfile = current_subfile;
11161 }
25e43795 11162 /* Append row to matrix using current values. */
7019d805 11163 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11164 (*p_record_line) (current_subfile, line, addr);
366da635 11165 }
25e43795 11166 }
ca5f395d 11167 basic_block = 0;
9aa1fe7e
GK
11168 }
11169 else switch (op_code)
c906108c
SS
11170 {
11171 case DW_LNS_extended_op:
3e43a32a
MS
11172 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11173 &bytes_read);
473b7be6 11174 line_ptr += bytes_read;
a8c50c1f 11175 extended_end = line_ptr + extended_len;
c906108c
SS
11176 extended_op = read_1_byte (abfd, line_ptr);
11177 line_ptr += 1;
11178 switch (extended_op)
11179 {
11180 case DW_LNE_end_sequence:
c91513d8 11181 p_record_line = record_line;
c906108c 11182 end_sequence = 1;
c906108c
SS
11183 break;
11184 case DW_LNE_set_address:
e7c27a73 11185 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11186
11187 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11188 {
11189 /* This line table is for a function which has been
11190 GCd by the linker. Ignore it. PR gdb/12528 */
11191
11192 long line_offset
11193 = line_ptr - dwarf2_per_objfile->line.buffer;
11194
11195 complaint (&symfile_complaints,
11196 _(".debug_line address at offset 0x%lx is 0 "
11197 "[in module %s]"),
bb5ed363 11198 line_offset, objfile->name);
c91513d8
PP
11199 p_record_line = noop_record_line;
11200 }
11201
2dc7f7b3 11202 op_index = 0;
107d2387
AC
11203 line_ptr += bytes_read;
11204 address += baseaddr;
c906108c
SS
11205 break;
11206 case DW_LNE_define_file:
debd256d
JB
11207 {
11208 char *cur_file;
11209 unsigned int dir_index, mod_time, length;
6e70227d 11210
3e43a32a
MS
11211 cur_file = read_direct_string (abfd, line_ptr,
11212 &bytes_read);
debd256d
JB
11213 line_ptr += bytes_read;
11214 dir_index =
11215 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11216 line_ptr += bytes_read;
11217 mod_time =
11218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11219 line_ptr += bytes_read;
11220 length =
11221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11222 line_ptr += bytes_read;
11223 add_file_name (lh, cur_file, dir_index, mod_time, length);
11224 }
c906108c 11225 break;
d0c6ba3d
CC
11226 case DW_LNE_set_discriminator:
11227 /* The discriminator is not interesting to the debugger;
11228 just ignore it. */
11229 line_ptr = extended_end;
11230 break;
c906108c 11231 default:
4d3c2250 11232 complaint (&symfile_complaints,
e2e0b3e5 11233 _("mangled .debug_line section"));
debd256d 11234 return;
c906108c 11235 }
a8c50c1f
DJ
11236 /* Make sure that we parsed the extended op correctly. If e.g.
11237 we expected a different address size than the producer used,
11238 we may have read the wrong number of bytes. */
11239 if (line_ptr != extended_end)
11240 {
11241 complaint (&symfile_complaints,
11242 _("mangled .debug_line section"));
11243 return;
11244 }
c906108c
SS
11245 break;
11246 case DW_LNS_copy:
59205f5a 11247 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11248 dwarf2_debug_line_missing_file_complaint ();
11249 else
366da635 11250 {
25e43795 11251 lh->file_names[file - 1].included_p = 1;
ca5f395d 11252 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11253 {
11254 if (last_subfile != current_subfile)
11255 {
11256 addr = gdbarch_addr_bits_remove (gdbarch, address);
11257 if (last_subfile)
c91513d8 11258 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11259 last_subfile = current_subfile;
11260 }
7019d805 11261 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11262 (*p_record_line) (current_subfile, line, addr);
fbf65064 11263 }
366da635 11264 }
c906108c
SS
11265 basic_block = 0;
11266 break;
11267 case DW_LNS_advance_pc:
2dc7f7b3
TT
11268 {
11269 CORE_ADDR adjust
11270 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11271
11272 address += (((op_index + adjust)
11273 / lh->maximum_ops_per_instruction)
11274 * lh->minimum_instruction_length);
11275 op_index = ((op_index + adjust)
11276 % lh->maximum_ops_per_instruction);
11277 line_ptr += bytes_read;
11278 }
c906108c
SS
11279 break;
11280 case DW_LNS_advance_line:
11281 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11282 line_ptr += bytes_read;
11283 break;
11284 case DW_LNS_set_file:
debd256d 11285 {
a738430d
MK
11286 /* The arrays lh->include_dirs and lh->file_names are
11287 0-based, but the directory and file name numbers in
11288 the statement program are 1-based. */
debd256d 11289 struct file_entry *fe;
4f1520fb 11290 char *dir = NULL;
a738430d 11291
debd256d
JB
11292 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11293 line_ptr += bytes_read;
59205f5a 11294 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11295 dwarf2_debug_line_missing_file_complaint ();
11296 else
11297 {
11298 fe = &lh->file_names[file - 1];
11299 if (fe->dir_index)
11300 dir = lh->include_dirs[fe->dir_index - 1];
11301 if (!decode_for_pst_p)
11302 {
11303 last_subfile = current_subfile;
11304 dwarf2_start_subfile (fe->name, dir, comp_dir);
11305 }
11306 }
debd256d 11307 }
c906108c
SS
11308 break;
11309 case DW_LNS_set_column:
11310 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11311 line_ptr += bytes_read;
11312 break;
11313 case DW_LNS_negate_stmt:
11314 is_stmt = (!is_stmt);
11315 break;
11316 case DW_LNS_set_basic_block:
11317 basic_block = 1;
11318 break;
c2c6d25f
JM
11319 /* Add to the address register of the state machine the
11320 address increment value corresponding to special opcode
a738430d
MK
11321 255. I.e., this value is scaled by the minimum
11322 instruction length since special opcode 255 would have
b021a221 11323 scaled the increment. */
c906108c 11324 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11325 {
11326 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11327
11328 address += (((op_index + adjust)
11329 / lh->maximum_ops_per_instruction)
11330 * lh->minimum_instruction_length);
11331 op_index = ((op_index + adjust)
11332 % lh->maximum_ops_per_instruction);
11333 }
c906108c
SS
11334 break;
11335 case DW_LNS_fixed_advance_pc:
11336 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11337 op_index = 0;
c906108c
SS
11338 line_ptr += 2;
11339 break;
9aa1fe7e 11340 default:
a738430d
MK
11341 {
11342 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11343 int i;
a738430d 11344
debd256d 11345 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11346 {
11347 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11348 line_ptr += bytes_read;
11349 }
11350 }
c906108c
SS
11351 }
11352 }
59205f5a
JB
11353 if (lh->num_file_names < file || file == 0)
11354 dwarf2_debug_line_missing_file_complaint ();
11355 else
11356 {
11357 lh->file_names[file - 1].included_p = 1;
11358 if (!decode_for_pst_p)
fbf65064
UW
11359 {
11360 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11361 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11362 }
59205f5a 11363 }
c906108c 11364 }
aaa75496
JB
11365
11366 if (decode_for_pst_p)
11367 {
11368 int file_index;
11369
11370 /* Now that we're done scanning the Line Header Program, we can
11371 create the psymtab of each included file. */
11372 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11373 if (lh->file_names[file_index].included_p == 1)
11374 {
c6da4cef
DE
11375 char *include_name =
11376 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11377 if (include_name != NULL)
aaa75496
JB
11378 dwarf2_create_include_psymtab (include_name, pst, objfile);
11379 }
11380 }
cb1df416
DJ
11381 else
11382 {
11383 /* Make sure a symtab is created for every file, even files
11384 which contain only variables (i.e. no code with associated
11385 line numbers). */
11386
11387 int i;
11388 struct file_entry *fe;
11389
11390 for (i = 0; i < lh->num_file_names; i++)
11391 {
11392 char *dir = NULL;
9a619af0 11393
cb1df416
DJ
11394 fe = &lh->file_names[i];
11395 if (fe->dir_index)
11396 dir = lh->include_dirs[fe->dir_index - 1];
11397 dwarf2_start_subfile (fe->name, dir, comp_dir);
11398
11399 /* Skip the main file; we don't need it, and it must be
11400 allocated last, so that it will show up before the
11401 non-primary symtabs in the objfile's symtab list. */
11402 if (current_subfile == first_subfile)
11403 continue;
11404
11405 if (current_subfile->symtab == NULL)
11406 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11407 objfile);
cb1df416
DJ
11408 fe->symtab = current_subfile->symtab;
11409 }
11410 }
c906108c
SS
11411}
11412
11413/* Start a subfile for DWARF. FILENAME is the name of the file and
11414 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11415 or NULL if not known. COMP_DIR is the compilation directory for the
11416 linetable's compilation unit or NULL if not known.
c906108c
SS
11417 This routine tries to keep line numbers from identical absolute and
11418 relative file names in a common subfile.
11419
11420 Using the `list' example from the GDB testsuite, which resides in
11421 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11422 of /srcdir/list0.c yields the following debugging information for list0.c:
11423
c5aa993b
JM
11424 DW_AT_name: /srcdir/list0.c
11425 DW_AT_comp_dir: /compdir
357e46e7 11426 files.files[0].name: list0.h
c5aa993b 11427 files.files[0].dir: /srcdir
357e46e7 11428 files.files[1].name: list0.c
c5aa993b 11429 files.files[1].dir: /srcdir
c906108c
SS
11430
11431 The line number information for list0.c has to end up in a single
4f1520fb
FR
11432 subfile, so that `break /srcdir/list0.c:1' works as expected.
11433 start_subfile will ensure that this happens provided that we pass the
11434 concatenation of files.files[1].dir and files.files[1].name as the
11435 subfile's name. */
c906108c
SS
11436
11437static void
3e43a32a
MS
11438dwarf2_start_subfile (char *filename, const char *dirname,
11439 const char *comp_dir)
c906108c 11440{
4f1520fb
FR
11441 char *fullname;
11442
11443 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11444 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11445 second argument to start_subfile. To be consistent, we do the
11446 same here. In order not to lose the line information directory,
11447 we concatenate it to the filename when it makes sense.
11448 Note that the Dwarf3 standard says (speaking of filenames in line
11449 information): ``The directory index is ignored for file names
11450 that represent full path names''. Thus ignoring dirname in the
11451 `else' branch below isn't an issue. */
c906108c 11452
d5166ae1 11453 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11454 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11455 else
11456 fullname = filename;
c906108c 11457
4f1520fb
FR
11458 start_subfile (fullname, comp_dir);
11459
11460 if (fullname != filename)
11461 xfree (fullname);
c906108c
SS
11462}
11463
4c2df51b
DJ
11464static void
11465var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11466 struct dwarf2_cu *cu)
4c2df51b 11467{
e7c27a73
DJ
11468 struct objfile *objfile = cu->objfile;
11469 struct comp_unit_head *cu_header = &cu->header;
11470
4c2df51b
DJ
11471 /* NOTE drow/2003-01-30: There used to be a comment and some special
11472 code here to turn a symbol with DW_AT_external and a
11473 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11474 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11475 with some versions of binutils) where shared libraries could have
11476 relocations against symbols in their debug information - the
11477 minimal symbol would have the right address, but the debug info
11478 would not. It's no longer necessary, because we will explicitly
11479 apply relocations when we read in the debug information now. */
11480
11481 /* A DW_AT_location attribute with no contents indicates that a
11482 variable has been optimized away. */
11483 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11484 {
11485 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11486 return;
11487 }
11488
11489 /* Handle one degenerate form of location expression specially, to
11490 preserve GDB's previous behavior when section offsets are
11491 specified. If this is just a DW_OP_addr then mark this symbol
11492 as LOC_STATIC. */
11493
11494 if (attr_form_is_block (attr)
11495 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11496 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11497 {
891d2f0b 11498 unsigned int dummy;
4c2df51b
DJ
11499
11500 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11501 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11502 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11503 fixup_symbol_section (sym, objfile);
11504 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11505 SYMBOL_SECTION (sym));
4c2df51b
DJ
11506 return;
11507 }
11508
11509 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11510 expression evaluator, and use LOC_COMPUTED only when necessary
11511 (i.e. when the value of a register or memory location is
11512 referenced, or a thread-local block, etc.). Then again, it might
11513 not be worthwhile. I'm assuming that it isn't unless performance
11514 or memory numbers show me otherwise. */
11515
e7c27a73 11516 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11517 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11518
11519 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11520 cu->has_loclist = 1;
4c2df51b
DJ
11521}
11522
c906108c
SS
11523/* Given a pointer to a DWARF information entry, figure out if we need
11524 to make a symbol table entry for it, and if so, create a new entry
11525 and return a pointer to it.
11526 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11527 used the passed type.
11528 If SPACE is not NULL, use it to hold the new symbol. If it is
11529 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11530
11531static struct symbol *
34eaf542
TT
11532new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11533 struct symbol *space)
c906108c 11534{
e7c27a73 11535 struct objfile *objfile = cu->objfile;
c906108c
SS
11536 struct symbol *sym = NULL;
11537 char *name;
11538 struct attribute *attr = NULL;
11539 struct attribute *attr2 = NULL;
e142c38c 11540 CORE_ADDR baseaddr;
e37fd15a
SW
11541 struct pending **list_to_add = NULL;
11542
edb3359d 11543 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11544
11545 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11546
94af9270 11547 name = dwarf2_name (die, cu);
c906108c
SS
11548 if (name)
11549 {
94af9270 11550 const char *linkagename;
34eaf542 11551 int suppress_add = 0;
94af9270 11552
34eaf542
TT
11553 if (space)
11554 sym = space;
11555 else
11556 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11557 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11558
11559 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11560 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11561 linkagename = dwarf2_physname (name, die, cu);
11562 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11563
f55ee35c
JK
11564 /* Fortran does not have mangling standard and the mangling does differ
11565 between gfortran, iFort etc. */
11566 if (cu->language == language_fortran
b250c185 11567 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11568 symbol_set_demangled_name (&(sym->ginfo),
11569 (char *) dwarf2_full_name (name, die, cu),
11570 NULL);
f55ee35c 11571
c906108c 11572 /* Default assumptions.
c5aa993b 11573 Use the passed type or decode it from the die. */
176620f1 11574 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11575 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11576 if (type != NULL)
11577 SYMBOL_TYPE (sym) = type;
11578 else
e7c27a73 11579 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11580 attr = dwarf2_attr (die,
11581 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11582 cu);
c906108c
SS
11583 if (attr)
11584 {
11585 SYMBOL_LINE (sym) = DW_UNSND (attr);
11586 }
cb1df416 11587
edb3359d
DJ
11588 attr = dwarf2_attr (die,
11589 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11590 cu);
cb1df416
DJ
11591 if (attr)
11592 {
11593 int file_index = DW_UNSND (attr);
9a619af0 11594
cb1df416
DJ
11595 if (cu->line_header == NULL
11596 || file_index > cu->line_header->num_file_names)
11597 complaint (&symfile_complaints,
11598 _("file index out of range"));
1c3d648d 11599 else if (file_index > 0)
cb1df416
DJ
11600 {
11601 struct file_entry *fe;
9a619af0 11602
cb1df416
DJ
11603 fe = &cu->line_header->file_names[file_index - 1];
11604 SYMBOL_SYMTAB (sym) = fe->symtab;
11605 }
11606 }
11607
c906108c
SS
11608 switch (die->tag)
11609 {
11610 case DW_TAG_label:
e142c38c 11611 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11612 if (attr)
11613 {
11614 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11615 }
0f5238ed
TT
11616 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11617 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11618 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11619 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11620 break;
11621 case DW_TAG_subprogram:
11622 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11623 finish_block. */
11624 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11625 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11626 if ((attr2 && (DW_UNSND (attr2) != 0))
11627 || cu->language == language_ada)
c906108c 11628 {
2cfa0c8d
JB
11629 /* Subprograms marked external are stored as a global symbol.
11630 Ada subprograms, whether marked external or not, are always
11631 stored as a global symbol, because we want to be able to
11632 access them globally. For instance, we want to be able
11633 to break on a nested subprogram without having to
11634 specify the context. */
e37fd15a 11635 list_to_add = &global_symbols;
c906108c
SS
11636 }
11637 else
11638 {
e37fd15a 11639 list_to_add = cu->list_in_scope;
c906108c
SS
11640 }
11641 break;
edb3359d
DJ
11642 case DW_TAG_inlined_subroutine:
11643 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11644 finish_block. */
11645 SYMBOL_CLASS (sym) = LOC_BLOCK;
11646 SYMBOL_INLINED (sym) = 1;
11647 /* Do not add the symbol to any lists. It will be found via
11648 BLOCK_FUNCTION from the blockvector. */
11649 break;
34eaf542
TT
11650 case DW_TAG_template_value_param:
11651 suppress_add = 1;
11652 /* Fall through. */
72929c62 11653 case DW_TAG_constant:
c906108c 11654 case DW_TAG_variable:
254e6b9e 11655 case DW_TAG_member:
0963b4bd
MS
11656 /* Compilation with minimal debug info may result in
11657 variables with missing type entries. Change the
11658 misleading `void' type to something sensible. */
c906108c 11659 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11660 SYMBOL_TYPE (sym)
46bf5051 11661 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11662
e142c38c 11663 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11664 /* In the case of DW_TAG_member, we should only be called for
11665 static const members. */
11666 if (die->tag == DW_TAG_member)
11667 {
3863f96c
DE
11668 /* dwarf2_add_field uses die_is_declaration,
11669 so we do the same. */
254e6b9e
DE
11670 gdb_assert (die_is_declaration (die, cu));
11671 gdb_assert (attr);
11672 }
c906108c
SS
11673 if (attr)
11674 {
e7c27a73 11675 dwarf2_const_value (attr, sym, cu);
e142c38c 11676 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11677 if (!suppress_add)
34eaf542
TT
11678 {
11679 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11680 list_to_add = &global_symbols;
34eaf542 11681 else
e37fd15a 11682 list_to_add = cu->list_in_scope;
34eaf542 11683 }
c906108c
SS
11684 break;
11685 }
e142c38c 11686 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11687 if (attr)
11688 {
e7c27a73 11689 var_decode_location (attr, sym, cu);
e142c38c 11690 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11691 if (SYMBOL_CLASS (sym) == LOC_STATIC
11692 && SYMBOL_VALUE_ADDRESS (sym) == 0
11693 && !dwarf2_per_objfile->has_section_at_zero)
11694 {
11695 /* When a static variable is eliminated by the linker,
11696 the corresponding debug information is not stripped
11697 out, but the variable address is set to null;
11698 do not add such variables into symbol table. */
11699 }
11700 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11701 {
f55ee35c
JK
11702 /* Workaround gfortran PR debug/40040 - it uses
11703 DW_AT_location for variables in -fPIC libraries which may
11704 get overriden by other libraries/executable and get
11705 a different address. Resolve it by the minimal symbol
11706 which may come from inferior's executable using copy
11707 relocation. Make this workaround only for gfortran as for
11708 other compilers GDB cannot guess the minimal symbol
11709 Fortran mangling kind. */
11710 if (cu->language == language_fortran && die->parent
11711 && die->parent->tag == DW_TAG_module
11712 && cu->producer
11713 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11714 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11715
1c809c68
TT
11716 /* A variable with DW_AT_external is never static,
11717 but it may be block-scoped. */
11718 list_to_add = (cu->list_in_scope == &file_symbols
11719 ? &global_symbols : cu->list_in_scope);
1c809c68 11720 }
c906108c 11721 else
e37fd15a 11722 list_to_add = cu->list_in_scope;
c906108c
SS
11723 }
11724 else
11725 {
11726 /* We do not know the address of this symbol.
c5aa993b
JM
11727 If it is an external symbol and we have type information
11728 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11729 The address of the variable will then be determined from
11730 the minimal symbol table whenever the variable is
11731 referenced. */
e142c38c 11732 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11733 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11734 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11735 {
0fe7935b
DJ
11736 /* A variable with DW_AT_external is never static, but it
11737 may be block-scoped. */
11738 list_to_add = (cu->list_in_scope == &file_symbols
11739 ? &global_symbols : cu->list_in_scope);
11740
c906108c 11741 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11742 }
442ddf59
JK
11743 else if (!die_is_declaration (die, cu))
11744 {
11745 /* Use the default LOC_OPTIMIZED_OUT class. */
11746 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11747 if (!suppress_add)
11748 list_to_add = cu->list_in_scope;
442ddf59 11749 }
c906108c
SS
11750 }
11751 break;
11752 case DW_TAG_formal_parameter:
edb3359d
DJ
11753 /* If we are inside a function, mark this as an argument. If
11754 not, we might be looking at an argument to an inlined function
11755 when we do not have enough information to show inlined frames;
11756 pretend it's a local variable in that case so that the user can
11757 still see it. */
11758 if (context_stack_depth > 0
11759 && context_stack[context_stack_depth - 1].name != NULL)
11760 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11761 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11762 if (attr)
11763 {
e7c27a73 11764 var_decode_location (attr, sym, cu);
c906108c 11765 }
e142c38c 11766 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11767 if (attr)
11768 {
e7c27a73 11769 dwarf2_const_value (attr, sym, cu);
c906108c 11770 }
f346a30d 11771
e37fd15a 11772 list_to_add = cu->list_in_scope;
c906108c
SS
11773 break;
11774 case DW_TAG_unspecified_parameters:
11775 /* From varargs functions; gdb doesn't seem to have any
11776 interest in this information, so just ignore it for now.
11777 (FIXME?) */
11778 break;
34eaf542
TT
11779 case DW_TAG_template_type_param:
11780 suppress_add = 1;
11781 /* Fall through. */
c906108c 11782 case DW_TAG_class_type:
680b30c7 11783 case DW_TAG_interface_type:
c906108c
SS
11784 case DW_TAG_structure_type:
11785 case DW_TAG_union_type:
72019c9c 11786 case DW_TAG_set_type:
c906108c
SS
11787 case DW_TAG_enumeration_type:
11788 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11789 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11790
63d06c5c 11791 {
987504bb 11792 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11793 really ever be static objects: otherwise, if you try
11794 to, say, break of a class's method and you're in a file
11795 which doesn't mention that class, it won't work unless
11796 the check for all static symbols in lookup_symbol_aux
11797 saves you. See the OtherFileClass tests in
11798 gdb.c++/namespace.exp. */
11799
e37fd15a 11800 if (!suppress_add)
34eaf542 11801 {
34eaf542
TT
11802 list_to_add = (cu->list_in_scope == &file_symbols
11803 && (cu->language == language_cplus
11804 || cu->language == language_java)
11805 ? &global_symbols : cu->list_in_scope);
63d06c5c 11806
64382290
TT
11807 /* The semantics of C++ state that "struct foo {
11808 ... }" also defines a typedef for "foo". A Java
11809 class declaration also defines a typedef for the
11810 class. */
11811 if (cu->language == language_cplus
11812 || cu->language == language_java
11813 || cu->language == language_ada)
11814 {
11815 /* The symbol's name is already allocated along
11816 with this objfile, so we don't need to
11817 duplicate it for the type. */
11818 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11819 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11820 }
63d06c5c
DC
11821 }
11822 }
c906108c
SS
11823 break;
11824 case DW_TAG_typedef:
63d06c5c
DC
11825 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11826 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11827 list_to_add = cu->list_in_scope;
63d06c5c 11828 break;
c906108c 11829 case DW_TAG_base_type:
a02abb62 11830 case DW_TAG_subrange_type:
c906108c 11831 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11832 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11833 list_to_add = cu->list_in_scope;
c906108c
SS
11834 break;
11835 case DW_TAG_enumerator:
e142c38c 11836 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11837 if (attr)
11838 {
e7c27a73 11839 dwarf2_const_value (attr, sym, cu);
c906108c 11840 }
63d06c5c
DC
11841 {
11842 /* NOTE: carlton/2003-11-10: See comment above in the
11843 DW_TAG_class_type, etc. block. */
11844
e142c38c 11845 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11846 && (cu->language == language_cplus
11847 || cu->language == language_java)
e142c38c 11848 ? &global_symbols : cu->list_in_scope);
63d06c5c 11849 }
c906108c 11850 break;
5c4e30ca
DC
11851 case DW_TAG_namespace:
11852 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11853 list_to_add = &global_symbols;
5c4e30ca 11854 break;
c906108c
SS
11855 default:
11856 /* Not a tag we recognize. Hopefully we aren't processing
11857 trash data, but since we must specifically ignore things
11858 we don't recognize, there is nothing else we should do at
0963b4bd 11859 this point. */
e2e0b3e5 11860 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11861 dwarf_tag_name (die->tag));
c906108c
SS
11862 break;
11863 }
df8a16a1 11864
e37fd15a
SW
11865 if (suppress_add)
11866 {
11867 sym->hash_next = objfile->template_symbols;
11868 objfile->template_symbols = sym;
11869 list_to_add = NULL;
11870 }
11871
11872 if (list_to_add != NULL)
11873 add_symbol_to_list (sym, list_to_add);
11874
df8a16a1
DJ
11875 /* For the benefit of old versions of GCC, check for anonymous
11876 namespaces based on the demangled name. */
11877 if (!processing_has_namespace_info
94af9270 11878 && cu->language == language_cplus)
a10964d1 11879 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11880 }
11881 return (sym);
11882}
11883
34eaf542
TT
11884/* A wrapper for new_symbol_full that always allocates a new symbol. */
11885
11886static struct symbol *
11887new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11888{
11889 return new_symbol_full (die, type, cu, NULL);
11890}
11891
98bfdba5
PA
11892/* Given an attr with a DW_FORM_dataN value in host byte order,
11893 zero-extend it as appropriate for the symbol's type. The DWARF
11894 standard (v4) is not entirely clear about the meaning of using
11895 DW_FORM_dataN for a constant with a signed type, where the type is
11896 wider than the data. The conclusion of a discussion on the DWARF
11897 list was that this is unspecified. We choose to always zero-extend
11898 because that is the interpretation long in use by GCC. */
c906108c 11899
98bfdba5
PA
11900static gdb_byte *
11901dwarf2_const_value_data (struct attribute *attr, struct type *type,
11902 const char *name, struct obstack *obstack,
11903 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11904{
e7c27a73 11905 struct objfile *objfile = cu->objfile;
e17a4113
UW
11906 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11907 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11908 LONGEST l = DW_UNSND (attr);
11909
11910 if (bits < sizeof (*value) * 8)
11911 {
11912 l &= ((LONGEST) 1 << bits) - 1;
11913 *value = l;
11914 }
11915 else if (bits == sizeof (*value) * 8)
11916 *value = l;
11917 else
11918 {
11919 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11920 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11921 return bytes;
11922 }
11923
11924 return NULL;
11925}
11926
11927/* Read a constant value from an attribute. Either set *VALUE, or if
11928 the value does not fit in *VALUE, set *BYTES - either already
11929 allocated on the objfile obstack, or newly allocated on OBSTACK,
11930 or, set *BATON, if we translated the constant to a location
11931 expression. */
11932
11933static void
11934dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11935 const char *name, struct obstack *obstack,
11936 struct dwarf2_cu *cu,
11937 long *value, gdb_byte **bytes,
11938 struct dwarf2_locexpr_baton **baton)
11939{
11940 struct objfile *objfile = cu->objfile;
11941 struct comp_unit_head *cu_header = &cu->header;
c906108c 11942 struct dwarf_block *blk;
98bfdba5
PA
11943 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11944 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11945
11946 *value = 0;
11947 *bytes = NULL;
11948 *baton = NULL;
c906108c
SS
11949
11950 switch (attr->form)
11951 {
11952 case DW_FORM_addr:
ac56253d 11953 {
ac56253d
TT
11954 gdb_byte *data;
11955
98bfdba5
PA
11956 if (TYPE_LENGTH (type) != cu_header->addr_size)
11957 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11958 cu_header->addr_size,
98bfdba5 11959 TYPE_LENGTH (type));
ac56253d
TT
11960 /* Symbols of this form are reasonably rare, so we just
11961 piggyback on the existing location code rather than writing
11962 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11963 *baton = obstack_alloc (&objfile->objfile_obstack,
11964 sizeof (struct dwarf2_locexpr_baton));
11965 (*baton)->per_cu = cu->per_cu;
11966 gdb_assert ((*baton)->per_cu);
ac56253d 11967
98bfdba5
PA
11968 (*baton)->size = 2 + cu_header->addr_size;
11969 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11970 (*baton)->data = data;
ac56253d
TT
11971
11972 data[0] = DW_OP_addr;
11973 store_unsigned_integer (&data[1], cu_header->addr_size,
11974 byte_order, DW_ADDR (attr));
11975 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11976 }
c906108c 11977 break;
4ac36638 11978 case DW_FORM_string:
93b5768b 11979 case DW_FORM_strp:
98bfdba5
PA
11980 /* DW_STRING is already allocated on the objfile obstack, point
11981 directly to it. */
11982 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11983 break;
c906108c
SS
11984 case DW_FORM_block1:
11985 case DW_FORM_block2:
11986 case DW_FORM_block4:
11987 case DW_FORM_block:
2dc7f7b3 11988 case DW_FORM_exprloc:
c906108c 11989 blk = DW_BLOCK (attr);
98bfdba5
PA
11990 if (TYPE_LENGTH (type) != blk->size)
11991 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11992 TYPE_LENGTH (type));
11993 *bytes = blk->data;
c906108c 11994 break;
2df3850c
JM
11995
11996 /* The DW_AT_const_value attributes are supposed to carry the
11997 symbol's value "represented as it would be on the target
11998 architecture." By the time we get here, it's already been
11999 converted to host endianness, so we just need to sign- or
12000 zero-extend it as appropriate. */
12001 case DW_FORM_data1:
3e43a32a
MS
12002 *bytes = dwarf2_const_value_data (attr, type, name,
12003 obstack, cu, value, 8);
2df3850c 12004 break;
c906108c 12005 case DW_FORM_data2:
3e43a32a
MS
12006 *bytes = dwarf2_const_value_data (attr, type, name,
12007 obstack, cu, value, 16);
2df3850c 12008 break;
c906108c 12009 case DW_FORM_data4:
3e43a32a
MS
12010 *bytes = dwarf2_const_value_data (attr, type, name,
12011 obstack, cu, value, 32);
2df3850c 12012 break;
c906108c 12013 case DW_FORM_data8:
3e43a32a
MS
12014 *bytes = dwarf2_const_value_data (attr, type, name,
12015 obstack, cu, value, 64);
2df3850c
JM
12016 break;
12017
c906108c 12018 case DW_FORM_sdata:
98bfdba5 12019 *value = DW_SND (attr);
2df3850c
JM
12020 break;
12021
c906108c 12022 case DW_FORM_udata:
98bfdba5 12023 *value = DW_UNSND (attr);
c906108c 12024 break;
2df3850c 12025
c906108c 12026 default:
4d3c2250 12027 complaint (&symfile_complaints,
e2e0b3e5 12028 _("unsupported const value attribute form: '%s'"),
4d3c2250 12029 dwarf_form_name (attr->form));
98bfdba5 12030 *value = 0;
c906108c
SS
12031 break;
12032 }
12033}
12034
2df3850c 12035
98bfdba5
PA
12036/* Copy constant value from an attribute to a symbol. */
12037
2df3850c 12038static void
98bfdba5
PA
12039dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12040 struct dwarf2_cu *cu)
2df3850c 12041{
98bfdba5
PA
12042 struct objfile *objfile = cu->objfile;
12043 struct comp_unit_head *cu_header = &cu->header;
12044 long value;
12045 gdb_byte *bytes;
12046 struct dwarf2_locexpr_baton *baton;
2df3850c 12047
98bfdba5
PA
12048 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12049 SYMBOL_PRINT_NAME (sym),
12050 &objfile->objfile_obstack, cu,
12051 &value, &bytes, &baton);
2df3850c 12052
98bfdba5
PA
12053 if (baton != NULL)
12054 {
12055 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12056 SYMBOL_LOCATION_BATON (sym) = baton;
12057 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12058 }
12059 else if (bytes != NULL)
12060 {
12061 SYMBOL_VALUE_BYTES (sym) = bytes;
12062 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12063 }
12064 else
12065 {
12066 SYMBOL_VALUE (sym) = value;
12067 SYMBOL_CLASS (sym) = LOC_CONST;
12068 }
2df3850c
JM
12069}
12070
c906108c
SS
12071/* Return the type of the die in question using its DW_AT_type attribute. */
12072
12073static struct type *
e7c27a73 12074die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12075{
c906108c 12076 struct attribute *type_attr;
c906108c 12077
e142c38c 12078 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12079 if (!type_attr)
12080 {
12081 /* A missing DW_AT_type represents a void type. */
46bf5051 12082 return objfile_type (cu->objfile)->builtin_void;
c906108c 12083 }
348e048f 12084
673bfd45 12085 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12086}
12087
b4ba55a1
JB
12088/* True iff CU's producer generates GNAT Ada auxiliary information
12089 that allows to find parallel types through that information instead
12090 of having to do expensive parallel lookups by type name. */
12091
12092static int
12093need_gnat_info (struct dwarf2_cu *cu)
12094{
12095 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12096 of GNAT produces this auxiliary information, without any indication
12097 that it is produced. Part of enhancing the FSF version of GNAT
12098 to produce that information will be to put in place an indicator
12099 that we can use in order to determine whether the descriptive type
12100 info is available or not. One suggestion that has been made is
12101 to use a new attribute, attached to the CU die. For now, assume
12102 that the descriptive type info is not available. */
12103 return 0;
12104}
12105
b4ba55a1
JB
12106/* Return the auxiliary type of the die in question using its
12107 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12108 attribute is not present. */
12109
12110static struct type *
12111die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12112{
b4ba55a1 12113 struct attribute *type_attr;
b4ba55a1
JB
12114
12115 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12116 if (!type_attr)
12117 return NULL;
12118
673bfd45 12119 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12120}
12121
12122/* If DIE has a descriptive_type attribute, then set the TYPE's
12123 descriptive type accordingly. */
12124
12125static void
12126set_descriptive_type (struct type *type, struct die_info *die,
12127 struct dwarf2_cu *cu)
12128{
12129 struct type *descriptive_type = die_descriptive_type (die, cu);
12130
12131 if (descriptive_type)
12132 {
12133 ALLOCATE_GNAT_AUX_TYPE (type);
12134 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12135 }
12136}
12137
c906108c
SS
12138/* Return the containing type of the die in question using its
12139 DW_AT_containing_type attribute. */
12140
12141static struct type *
e7c27a73 12142die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12143{
c906108c 12144 struct attribute *type_attr;
c906108c 12145
e142c38c 12146 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12147 if (!type_attr)
12148 error (_("Dwarf Error: Problem turning containing type into gdb type "
12149 "[in module %s]"), cu->objfile->name);
12150
673bfd45 12151 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12152}
12153
673bfd45
DE
12154/* Look up the type of DIE in CU using its type attribute ATTR.
12155 If there is no type substitute an error marker. */
12156
c906108c 12157static struct type *
673bfd45
DE
12158lookup_die_type (struct die_info *die, struct attribute *attr,
12159 struct dwarf2_cu *cu)
c906108c 12160{
bb5ed363 12161 struct objfile *objfile = cu->objfile;
f792889a
DJ
12162 struct type *this_type;
12163
673bfd45
DE
12164 /* First see if we have it cached. */
12165
12166 if (is_ref_attr (attr))
12167 {
12168 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12169
12170 this_type = get_die_type_at_offset (offset, cu->per_cu);
12171 }
55f1336d 12172 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12173 {
12174 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12175 struct dwarf2_cu *sig_cu;
12176 unsigned int offset;
12177
12178 /* sig_type will be NULL if the signatured type is missing from
12179 the debug info. */
12180 if (sig_type == NULL)
12181 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12182 "at 0x%x [in module %s]"),
bb5ed363 12183 die->offset, objfile->name);
673bfd45 12184
b0df02fd 12185 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12186 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12187 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12188 }
12189 else
12190 {
12191 dump_die_for_error (die);
12192 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12193 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12194 }
12195
12196 /* If not cached we need to read it in. */
12197
12198 if (this_type == NULL)
12199 {
12200 struct die_info *type_die;
12201 struct dwarf2_cu *type_cu = cu;
12202
12203 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12204 /* If the type is cached, we should have found it above. */
12205 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12206 this_type = read_type_die_1 (type_die, type_cu);
12207 }
12208
12209 /* If we still don't have a type use an error marker. */
12210
12211 if (this_type == NULL)
c906108c 12212 {
b00fdb78
TT
12213 char *message, *saved;
12214
12215 /* read_type_die already issued a complaint. */
12216 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12217 objfile->name,
b00fdb78
TT
12218 cu->header.offset,
12219 die->offset);
bb5ed363 12220 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12221 message, strlen (message));
12222 xfree (message);
12223
bb5ed363 12224 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12225 }
673bfd45 12226
f792889a 12227 return this_type;
c906108c
SS
12228}
12229
673bfd45
DE
12230/* Return the type in DIE, CU.
12231 Returns NULL for invalid types.
12232
12233 This first does a lookup in the appropriate type_hash table,
12234 and only reads the die in if necessary.
12235
12236 NOTE: This can be called when reading in partial or full symbols. */
12237
f792889a 12238static struct type *
e7c27a73 12239read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12240{
f792889a
DJ
12241 struct type *this_type;
12242
12243 this_type = get_die_type (die, cu);
12244 if (this_type)
12245 return this_type;
12246
673bfd45
DE
12247 return read_type_die_1 (die, cu);
12248}
12249
12250/* Read the type in DIE, CU.
12251 Returns NULL for invalid types. */
12252
12253static struct type *
12254read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12255{
12256 struct type *this_type = NULL;
12257
c906108c
SS
12258 switch (die->tag)
12259 {
12260 case DW_TAG_class_type:
680b30c7 12261 case DW_TAG_interface_type:
c906108c
SS
12262 case DW_TAG_structure_type:
12263 case DW_TAG_union_type:
f792889a 12264 this_type = read_structure_type (die, cu);
c906108c
SS
12265 break;
12266 case DW_TAG_enumeration_type:
f792889a 12267 this_type = read_enumeration_type (die, cu);
c906108c
SS
12268 break;
12269 case DW_TAG_subprogram:
12270 case DW_TAG_subroutine_type:
edb3359d 12271 case DW_TAG_inlined_subroutine:
f792889a 12272 this_type = read_subroutine_type (die, cu);
c906108c
SS
12273 break;
12274 case DW_TAG_array_type:
f792889a 12275 this_type = read_array_type (die, cu);
c906108c 12276 break;
72019c9c 12277 case DW_TAG_set_type:
f792889a 12278 this_type = read_set_type (die, cu);
72019c9c 12279 break;
c906108c 12280 case DW_TAG_pointer_type:
f792889a 12281 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12282 break;
12283 case DW_TAG_ptr_to_member_type:
f792889a 12284 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12285 break;
12286 case DW_TAG_reference_type:
f792889a 12287 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12288 break;
12289 case DW_TAG_const_type:
f792889a 12290 this_type = read_tag_const_type (die, cu);
c906108c
SS
12291 break;
12292 case DW_TAG_volatile_type:
f792889a 12293 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12294 break;
12295 case DW_TAG_string_type:
f792889a 12296 this_type = read_tag_string_type (die, cu);
c906108c
SS
12297 break;
12298 case DW_TAG_typedef:
f792889a 12299 this_type = read_typedef (die, cu);
c906108c 12300 break;
a02abb62 12301 case DW_TAG_subrange_type:
f792889a 12302 this_type = read_subrange_type (die, cu);
a02abb62 12303 break;
c906108c 12304 case DW_TAG_base_type:
f792889a 12305 this_type = read_base_type (die, cu);
c906108c 12306 break;
81a17f79 12307 case DW_TAG_unspecified_type:
f792889a 12308 this_type = read_unspecified_type (die, cu);
81a17f79 12309 break;
0114d602
DJ
12310 case DW_TAG_namespace:
12311 this_type = read_namespace_type (die, cu);
12312 break;
f55ee35c
JK
12313 case DW_TAG_module:
12314 this_type = read_module_type (die, cu);
12315 break;
c906108c 12316 default:
3e43a32a
MS
12317 complaint (&symfile_complaints,
12318 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12319 dwarf_tag_name (die->tag));
c906108c
SS
12320 break;
12321 }
63d06c5c 12322
f792889a 12323 return this_type;
63d06c5c
DC
12324}
12325
abc72ce4
DE
12326/* See if we can figure out if the class lives in a namespace. We do
12327 this by looking for a member function; its demangled name will
12328 contain namespace info, if there is any.
12329 Return the computed name or NULL.
12330 Space for the result is allocated on the objfile's obstack.
12331 This is the full-die version of guess_partial_die_structure_name.
12332 In this case we know DIE has no useful parent. */
12333
12334static char *
12335guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12336{
12337 struct die_info *spec_die;
12338 struct dwarf2_cu *spec_cu;
12339 struct die_info *child;
12340
12341 spec_cu = cu;
12342 spec_die = die_specification (die, &spec_cu);
12343 if (spec_die != NULL)
12344 {
12345 die = spec_die;
12346 cu = spec_cu;
12347 }
12348
12349 for (child = die->child;
12350 child != NULL;
12351 child = child->sibling)
12352 {
12353 if (child->tag == DW_TAG_subprogram)
12354 {
12355 struct attribute *attr;
12356
12357 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12358 if (attr == NULL)
12359 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12360 if (attr != NULL)
12361 {
12362 char *actual_name
12363 = language_class_name_from_physname (cu->language_defn,
12364 DW_STRING (attr));
12365 char *name = NULL;
12366
12367 if (actual_name != NULL)
12368 {
12369 char *die_name = dwarf2_name (die, cu);
12370
12371 if (die_name != NULL
12372 && strcmp (die_name, actual_name) != 0)
12373 {
12374 /* Strip off the class name from the full name.
12375 We want the prefix. */
12376 int die_name_len = strlen (die_name);
12377 int actual_name_len = strlen (actual_name);
12378
12379 /* Test for '::' as a sanity check. */
12380 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12381 && actual_name[actual_name_len
12382 - die_name_len - 1] == ':')
abc72ce4
DE
12383 name =
12384 obsavestring (actual_name,
12385 actual_name_len - die_name_len - 2,
12386 &cu->objfile->objfile_obstack);
12387 }
12388 }
12389 xfree (actual_name);
12390 return name;
12391 }
12392 }
12393 }
12394
12395 return NULL;
12396}
12397
96408a79
SA
12398/* GCC might emit a nameless typedef that has a linkage name. Determine the
12399 prefix part in such case. See
12400 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12401
12402static char *
12403anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12404{
12405 struct attribute *attr;
12406 char *base;
12407
12408 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12409 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12410 return NULL;
12411
12412 attr = dwarf2_attr (die, DW_AT_name, cu);
12413 if (attr != NULL && DW_STRING (attr) != NULL)
12414 return NULL;
12415
12416 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12417 if (attr == NULL)
12418 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12419 if (attr == NULL || DW_STRING (attr) == NULL)
12420 return NULL;
12421
12422 /* dwarf2_name had to be already called. */
12423 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12424
12425 /* Strip the base name, keep any leading namespaces/classes. */
12426 base = strrchr (DW_STRING (attr), ':');
12427 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12428 return "";
12429
12430 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12431 &cu->objfile->objfile_obstack);
12432}
12433
fdde2d81 12434/* Return the name of the namespace/class that DIE is defined within,
0114d602 12435 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12436
0114d602
DJ
12437 For example, if we're within the method foo() in the following
12438 code:
12439
12440 namespace N {
12441 class C {
12442 void foo () {
12443 }
12444 };
12445 }
12446
12447 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12448
12449static char *
e142c38c 12450determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12451{
0114d602
DJ
12452 struct die_info *parent, *spec_die;
12453 struct dwarf2_cu *spec_cu;
12454 struct type *parent_type;
96408a79 12455 char *retval;
63d06c5c 12456
f55ee35c
JK
12457 if (cu->language != language_cplus && cu->language != language_java
12458 && cu->language != language_fortran)
0114d602
DJ
12459 return "";
12460
96408a79
SA
12461 retval = anonymous_struct_prefix (die, cu);
12462 if (retval)
12463 return retval;
12464
0114d602
DJ
12465 /* We have to be careful in the presence of DW_AT_specification.
12466 For example, with GCC 3.4, given the code
12467
12468 namespace N {
12469 void foo() {
12470 // Definition of N::foo.
12471 }
12472 }
12473
12474 then we'll have a tree of DIEs like this:
12475
12476 1: DW_TAG_compile_unit
12477 2: DW_TAG_namespace // N
12478 3: DW_TAG_subprogram // declaration of N::foo
12479 4: DW_TAG_subprogram // definition of N::foo
12480 DW_AT_specification // refers to die #3
12481
12482 Thus, when processing die #4, we have to pretend that we're in
12483 the context of its DW_AT_specification, namely the contex of die
12484 #3. */
12485 spec_cu = cu;
12486 spec_die = die_specification (die, &spec_cu);
12487 if (spec_die == NULL)
12488 parent = die->parent;
12489 else
63d06c5c 12490 {
0114d602
DJ
12491 parent = spec_die->parent;
12492 cu = spec_cu;
63d06c5c 12493 }
0114d602
DJ
12494
12495 if (parent == NULL)
12496 return "";
98bfdba5
PA
12497 else if (parent->building_fullname)
12498 {
12499 const char *name;
12500 const char *parent_name;
12501
12502 /* It has been seen on RealView 2.2 built binaries,
12503 DW_TAG_template_type_param types actually _defined_ as
12504 children of the parent class:
12505
12506 enum E {};
12507 template class <class Enum> Class{};
12508 Class<enum E> class_e;
12509
12510 1: DW_TAG_class_type (Class)
12511 2: DW_TAG_enumeration_type (E)
12512 3: DW_TAG_enumerator (enum1:0)
12513 3: DW_TAG_enumerator (enum2:1)
12514 ...
12515 2: DW_TAG_template_type_param
12516 DW_AT_type DW_FORM_ref_udata (E)
12517
12518 Besides being broken debug info, it can put GDB into an
12519 infinite loop. Consider:
12520
12521 When we're building the full name for Class<E>, we'll start
12522 at Class, and go look over its template type parameters,
12523 finding E. We'll then try to build the full name of E, and
12524 reach here. We're now trying to build the full name of E,
12525 and look over the parent DIE for containing scope. In the
12526 broken case, if we followed the parent DIE of E, we'd again
12527 find Class, and once again go look at its template type
12528 arguments, etc., etc. Simply don't consider such parent die
12529 as source-level parent of this die (it can't be, the language
12530 doesn't allow it), and break the loop here. */
12531 name = dwarf2_name (die, cu);
12532 parent_name = dwarf2_name (parent, cu);
12533 complaint (&symfile_complaints,
12534 _("template param type '%s' defined within parent '%s'"),
12535 name ? name : "<unknown>",
12536 parent_name ? parent_name : "<unknown>");
12537 return "";
12538 }
63d06c5c 12539 else
0114d602
DJ
12540 switch (parent->tag)
12541 {
63d06c5c 12542 case DW_TAG_namespace:
0114d602 12543 parent_type = read_type_die (parent, cu);
acebe513
UW
12544 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12545 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12546 Work around this problem here. */
12547 if (cu->language == language_cplus
12548 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12549 return "";
0114d602
DJ
12550 /* We give a name to even anonymous namespaces. */
12551 return TYPE_TAG_NAME (parent_type);
63d06c5c 12552 case DW_TAG_class_type:
680b30c7 12553 case DW_TAG_interface_type:
63d06c5c 12554 case DW_TAG_structure_type:
0114d602 12555 case DW_TAG_union_type:
f55ee35c 12556 case DW_TAG_module:
0114d602
DJ
12557 parent_type = read_type_die (parent, cu);
12558 if (TYPE_TAG_NAME (parent_type) != NULL)
12559 return TYPE_TAG_NAME (parent_type);
12560 else
12561 /* An anonymous structure is only allowed non-static data
12562 members; no typedefs, no member functions, et cetera.
12563 So it does not need a prefix. */
12564 return "";
abc72ce4
DE
12565 case DW_TAG_compile_unit:
12566 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12567 if (cu->language == language_cplus
8b70b953 12568 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12569 && die->child != NULL
12570 && (die->tag == DW_TAG_class_type
12571 || die->tag == DW_TAG_structure_type
12572 || die->tag == DW_TAG_union_type))
12573 {
12574 char *name = guess_full_die_structure_name (die, cu);
12575 if (name != NULL)
12576 return name;
12577 }
12578 return "";
63d06c5c 12579 default:
8176b9b8 12580 return determine_prefix (parent, cu);
63d06c5c 12581 }
63d06c5c
DC
12582}
12583
3e43a32a
MS
12584/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12585 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12586 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12587 an obconcat, otherwise allocate storage for the result. The CU argument is
12588 used to determine the language and hence, the appropriate separator. */
987504bb 12589
f55ee35c 12590#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12591
12592static char *
f55ee35c
JK
12593typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12594 int physname, struct dwarf2_cu *cu)
63d06c5c 12595{
f55ee35c 12596 const char *lead = "";
5c315b68 12597 const char *sep;
63d06c5c 12598
3e43a32a
MS
12599 if (suffix == NULL || suffix[0] == '\0'
12600 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12601 sep = "";
12602 else if (cu->language == language_java)
12603 sep = ".";
f55ee35c
JK
12604 else if (cu->language == language_fortran && physname)
12605 {
12606 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12607 DW_AT_MIPS_linkage_name is preferred and used instead. */
12608
12609 lead = "__";
12610 sep = "_MOD_";
12611 }
987504bb
JJ
12612 else
12613 sep = "::";
63d06c5c 12614
6dd47d34
DE
12615 if (prefix == NULL)
12616 prefix = "";
12617 if (suffix == NULL)
12618 suffix = "";
12619
987504bb
JJ
12620 if (obs == NULL)
12621 {
3e43a32a
MS
12622 char *retval
12623 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12624
f55ee35c
JK
12625 strcpy (retval, lead);
12626 strcat (retval, prefix);
6dd47d34
DE
12627 strcat (retval, sep);
12628 strcat (retval, suffix);
63d06c5c
DC
12629 return retval;
12630 }
987504bb
JJ
12631 else
12632 {
12633 /* We have an obstack. */
f55ee35c 12634 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12635 }
63d06c5c
DC
12636}
12637
c906108c
SS
12638/* Return sibling of die, NULL if no sibling. */
12639
f9aca02d 12640static struct die_info *
fba45db2 12641sibling_die (struct die_info *die)
c906108c 12642{
639d11d3 12643 return die->sibling;
c906108c
SS
12644}
12645
71c25dea
TT
12646/* Get name of a die, return NULL if not found. */
12647
12648static char *
12649dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12650 struct obstack *obstack)
12651{
12652 if (name && cu->language == language_cplus)
12653 {
12654 char *canon_name = cp_canonicalize_string (name);
12655
12656 if (canon_name != NULL)
12657 {
12658 if (strcmp (canon_name, name) != 0)
12659 name = obsavestring (canon_name, strlen (canon_name),
12660 obstack);
12661 xfree (canon_name);
12662 }
12663 }
12664
12665 return name;
c906108c
SS
12666}
12667
9219021c
DC
12668/* Get name of a die, return NULL if not found. */
12669
12670static char *
e142c38c 12671dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12672{
12673 struct attribute *attr;
12674
e142c38c 12675 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12676 if ((!attr || !DW_STRING (attr))
12677 && die->tag != DW_TAG_class_type
12678 && die->tag != DW_TAG_interface_type
12679 && die->tag != DW_TAG_structure_type
12680 && die->tag != DW_TAG_union_type)
71c25dea
TT
12681 return NULL;
12682
12683 switch (die->tag)
12684 {
12685 case DW_TAG_compile_unit:
12686 /* Compilation units have a DW_AT_name that is a filename, not
12687 a source language identifier. */
12688 case DW_TAG_enumeration_type:
12689 case DW_TAG_enumerator:
12690 /* These tags always have simple identifiers already; no need
12691 to canonicalize them. */
12692 return DW_STRING (attr);
907af001 12693
418835cc
KS
12694 case DW_TAG_subprogram:
12695 /* Java constructors will all be named "<init>", so return
12696 the class name when we see this special case. */
12697 if (cu->language == language_java
12698 && DW_STRING (attr) != NULL
12699 && strcmp (DW_STRING (attr), "<init>") == 0)
12700 {
12701 struct dwarf2_cu *spec_cu = cu;
12702 struct die_info *spec_die;
12703
12704 /* GCJ will output '<init>' for Java constructor names.
12705 For this special case, return the name of the parent class. */
12706
12707 /* GCJ may output suprogram DIEs with AT_specification set.
12708 If so, use the name of the specified DIE. */
12709 spec_die = die_specification (die, &spec_cu);
12710 if (spec_die != NULL)
12711 return dwarf2_name (spec_die, spec_cu);
12712
12713 do
12714 {
12715 die = die->parent;
12716 if (die->tag == DW_TAG_class_type)
12717 return dwarf2_name (die, cu);
12718 }
12719 while (die->tag != DW_TAG_compile_unit);
12720 }
907af001
UW
12721 break;
12722
12723 case DW_TAG_class_type:
12724 case DW_TAG_interface_type:
12725 case DW_TAG_structure_type:
12726 case DW_TAG_union_type:
12727 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12728 structures or unions. These were of the form "._%d" in GCC 4.1,
12729 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12730 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12731 if (attr && DW_STRING (attr)
12732 && (strncmp (DW_STRING (attr), "._", 2) == 0
12733 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12734 return NULL;
53832f31
TT
12735
12736 /* GCC might emit a nameless typedef that has a linkage name. See
12737 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12738 if (!attr || DW_STRING (attr) == NULL)
12739 {
df5c6c50 12740 char *demangled = NULL;
53832f31
TT
12741
12742 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12743 if (attr == NULL)
12744 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12745
12746 if (attr == NULL || DW_STRING (attr) == NULL)
12747 return NULL;
12748
df5c6c50
JK
12749 /* Avoid demangling DW_STRING (attr) the second time on a second
12750 call for the same DIE. */
12751 if (!DW_STRING_IS_CANONICAL (attr))
12752 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12753
12754 if (demangled)
12755 {
96408a79
SA
12756 char *base;
12757
53832f31 12758 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12759 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12760 &cu->objfile->objfile_obstack);
53832f31
TT
12761 DW_STRING_IS_CANONICAL (attr) = 1;
12762 xfree (demangled);
96408a79
SA
12763
12764 /* Strip any leading namespaces/classes, keep only the base name.
12765 DW_AT_name for named DIEs does not contain the prefixes. */
12766 base = strrchr (DW_STRING (attr), ':');
12767 if (base && base > DW_STRING (attr) && base[-1] == ':')
12768 return &base[1];
12769 else
12770 return DW_STRING (attr);
53832f31
TT
12771 }
12772 }
907af001
UW
12773 break;
12774
71c25dea 12775 default:
907af001
UW
12776 break;
12777 }
12778
12779 if (!DW_STRING_IS_CANONICAL (attr))
12780 {
12781 DW_STRING (attr)
12782 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12783 &cu->objfile->objfile_obstack);
12784 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12785 }
907af001 12786 return DW_STRING (attr);
9219021c
DC
12787}
12788
12789/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12790 is none. *EXT_CU is the CU containing DIE on input, and the CU
12791 containing the return value on output. */
9219021c
DC
12792
12793static struct die_info *
f2f0e013 12794dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12795{
12796 struct attribute *attr;
9219021c 12797
f2f0e013 12798 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12799 if (attr == NULL)
12800 return NULL;
12801
f2f0e013 12802 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12803}
12804
c906108c
SS
12805/* Convert a DIE tag into its string name. */
12806
12807static char *
aa1ee363 12808dwarf_tag_name (unsigned tag)
c906108c
SS
12809{
12810 switch (tag)
12811 {
12812 case DW_TAG_padding:
12813 return "DW_TAG_padding";
12814 case DW_TAG_array_type:
12815 return "DW_TAG_array_type";
12816 case DW_TAG_class_type:
12817 return "DW_TAG_class_type";
12818 case DW_TAG_entry_point:
12819 return "DW_TAG_entry_point";
12820 case DW_TAG_enumeration_type:
12821 return "DW_TAG_enumeration_type";
12822 case DW_TAG_formal_parameter:
12823 return "DW_TAG_formal_parameter";
12824 case DW_TAG_imported_declaration:
12825 return "DW_TAG_imported_declaration";
12826 case DW_TAG_label:
12827 return "DW_TAG_label";
12828 case DW_TAG_lexical_block:
12829 return "DW_TAG_lexical_block";
12830 case DW_TAG_member:
12831 return "DW_TAG_member";
12832 case DW_TAG_pointer_type:
12833 return "DW_TAG_pointer_type";
12834 case DW_TAG_reference_type:
12835 return "DW_TAG_reference_type";
12836 case DW_TAG_compile_unit:
12837 return "DW_TAG_compile_unit";
12838 case DW_TAG_string_type:
12839 return "DW_TAG_string_type";
12840 case DW_TAG_structure_type:
12841 return "DW_TAG_structure_type";
12842 case DW_TAG_subroutine_type:
12843 return "DW_TAG_subroutine_type";
12844 case DW_TAG_typedef:
12845 return "DW_TAG_typedef";
12846 case DW_TAG_union_type:
12847 return "DW_TAG_union_type";
12848 case DW_TAG_unspecified_parameters:
12849 return "DW_TAG_unspecified_parameters";
12850 case DW_TAG_variant:
12851 return "DW_TAG_variant";
12852 case DW_TAG_common_block:
12853 return "DW_TAG_common_block";
12854 case DW_TAG_common_inclusion:
12855 return "DW_TAG_common_inclusion";
12856 case DW_TAG_inheritance:
12857 return "DW_TAG_inheritance";
12858 case DW_TAG_inlined_subroutine:
12859 return "DW_TAG_inlined_subroutine";
12860 case DW_TAG_module:
12861 return "DW_TAG_module";
12862 case DW_TAG_ptr_to_member_type:
12863 return "DW_TAG_ptr_to_member_type";
12864 case DW_TAG_set_type:
12865 return "DW_TAG_set_type";
12866 case DW_TAG_subrange_type:
12867 return "DW_TAG_subrange_type";
12868 case DW_TAG_with_stmt:
12869 return "DW_TAG_with_stmt";
12870 case DW_TAG_access_declaration:
12871 return "DW_TAG_access_declaration";
12872 case DW_TAG_base_type:
12873 return "DW_TAG_base_type";
12874 case DW_TAG_catch_block:
12875 return "DW_TAG_catch_block";
12876 case DW_TAG_const_type:
12877 return "DW_TAG_const_type";
12878 case DW_TAG_constant:
12879 return "DW_TAG_constant";
12880 case DW_TAG_enumerator:
12881 return "DW_TAG_enumerator";
12882 case DW_TAG_file_type:
12883 return "DW_TAG_file_type";
12884 case DW_TAG_friend:
12885 return "DW_TAG_friend";
12886 case DW_TAG_namelist:
12887 return "DW_TAG_namelist";
12888 case DW_TAG_namelist_item:
12889 return "DW_TAG_namelist_item";
12890 case DW_TAG_packed_type:
12891 return "DW_TAG_packed_type";
12892 case DW_TAG_subprogram:
12893 return "DW_TAG_subprogram";
12894 case DW_TAG_template_type_param:
12895 return "DW_TAG_template_type_param";
12896 case DW_TAG_template_value_param:
12897 return "DW_TAG_template_value_param";
12898 case DW_TAG_thrown_type:
12899 return "DW_TAG_thrown_type";
12900 case DW_TAG_try_block:
12901 return "DW_TAG_try_block";
12902 case DW_TAG_variant_part:
12903 return "DW_TAG_variant_part";
12904 case DW_TAG_variable:
12905 return "DW_TAG_variable";
12906 case DW_TAG_volatile_type:
12907 return "DW_TAG_volatile_type";
d9fa45fe
DC
12908 case DW_TAG_dwarf_procedure:
12909 return "DW_TAG_dwarf_procedure";
12910 case DW_TAG_restrict_type:
12911 return "DW_TAG_restrict_type";
12912 case DW_TAG_interface_type:
12913 return "DW_TAG_interface_type";
12914 case DW_TAG_namespace:
12915 return "DW_TAG_namespace";
12916 case DW_TAG_imported_module:
12917 return "DW_TAG_imported_module";
12918 case DW_TAG_unspecified_type:
12919 return "DW_TAG_unspecified_type";
12920 case DW_TAG_partial_unit:
12921 return "DW_TAG_partial_unit";
12922 case DW_TAG_imported_unit:
12923 return "DW_TAG_imported_unit";
b7619582
GF
12924 case DW_TAG_condition:
12925 return "DW_TAG_condition";
12926 case DW_TAG_shared_type:
12927 return "DW_TAG_shared_type";
348e048f
DE
12928 case DW_TAG_type_unit:
12929 return "DW_TAG_type_unit";
c906108c
SS
12930 case DW_TAG_MIPS_loop:
12931 return "DW_TAG_MIPS_loop";
b7619582
GF
12932 case DW_TAG_HP_array_descriptor:
12933 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12934 case DW_TAG_format_label:
12935 return "DW_TAG_format_label";
12936 case DW_TAG_function_template:
12937 return "DW_TAG_function_template";
12938 case DW_TAG_class_template:
12939 return "DW_TAG_class_template";
b7619582
GF
12940 case DW_TAG_GNU_BINCL:
12941 return "DW_TAG_GNU_BINCL";
12942 case DW_TAG_GNU_EINCL:
12943 return "DW_TAG_GNU_EINCL";
12944 case DW_TAG_upc_shared_type:
12945 return "DW_TAG_upc_shared_type";
12946 case DW_TAG_upc_strict_type:
12947 return "DW_TAG_upc_strict_type";
12948 case DW_TAG_upc_relaxed_type:
12949 return "DW_TAG_upc_relaxed_type";
12950 case DW_TAG_PGI_kanji_type:
12951 return "DW_TAG_PGI_kanji_type";
12952 case DW_TAG_PGI_interface_block:
12953 return "DW_TAG_PGI_interface_block";
96408a79
SA
12954 case DW_TAG_GNU_call_site:
12955 return "DW_TAG_GNU_call_site";
c906108c
SS
12956 default:
12957 return "DW_TAG_<unknown>";
12958 }
12959}
12960
12961/* Convert a DWARF attribute code into its string name. */
12962
12963static char *
aa1ee363 12964dwarf_attr_name (unsigned attr)
c906108c
SS
12965{
12966 switch (attr)
12967 {
12968 case DW_AT_sibling:
12969 return "DW_AT_sibling";
12970 case DW_AT_location:
12971 return "DW_AT_location";
12972 case DW_AT_name:
12973 return "DW_AT_name";
12974 case DW_AT_ordering:
12975 return "DW_AT_ordering";
12976 case DW_AT_subscr_data:
12977 return "DW_AT_subscr_data";
12978 case DW_AT_byte_size:
12979 return "DW_AT_byte_size";
12980 case DW_AT_bit_offset:
12981 return "DW_AT_bit_offset";
12982 case DW_AT_bit_size:
12983 return "DW_AT_bit_size";
12984 case DW_AT_element_list:
12985 return "DW_AT_element_list";
12986 case DW_AT_stmt_list:
12987 return "DW_AT_stmt_list";
12988 case DW_AT_low_pc:
12989 return "DW_AT_low_pc";
12990 case DW_AT_high_pc:
12991 return "DW_AT_high_pc";
12992 case DW_AT_language:
12993 return "DW_AT_language";
12994 case DW_AT_member:
12995 return "DW_AT_member";
12996 case DW_AT_discr:
12997 return "DW_AT_discr";
12998 case DW_AT_discr_value:
12999 return "DW_AT_discr_value";
13000 case DW_AT_visibility:
13001 return "DW_AT_visibility";
13002 case DW_AT_import:
13003 return "DW_AT_import";
13004 case DW_AT_string_length:
13005 return "DW_AT_string_length";
13006 case DW_AT_common_reference:
13007 return "DW_AT_common_reference";
13008 case DW_AT_comp_dir:
13009 return "DW_AT_comp_dir";
13010 case DW_AT_const_value:
13011 return "DW_AT_const_value";
13012 case DW_AT_containing_type:
13013 return "DW_AT_containing_type";
13014 case DW_AT_default_value:
13015 return "DW_AT_default_value";
13016 case DW_AT_inline:
13017 return "DW_AT_inline";
13018 case DW_AT_is_optional:
13019 return "DW_AT_is_optional";
13020 case DW_AT_lower_bound:
13021 return "DW_AT_lower_bound";
13022 case DW_AT_producer:
13023 return "DW_AT_producer";
13024 case DW_AT_prototyped:
13025 return "DW_AT_prototyped";
13026 case DW_AT_return_addr:
13027 return "DW_AT_return_addr";
13028 case DW_AT_start_scope:
13029 return "DW_AT_start_scope";
09fa0d7c
JK
13030 case DW_AT_bit_stride:
13031 return "DW_AT_bit_stride";
c906108c
SS
13032 case DW_AT_upper_bound:
13033 return "DW_AT_upper_bound";
13034 case DW_AT_abstract_origin:
13035 return "DW_AT_abstract_origin";
13036 case DW_AT_accessibility:
13037 return "DW_AT_accessibility";
13038 case DW_AT_address_class:
13039 return "DW_AT_address_class";
13040 case DW_AT_artificial:
13041 return "DW_AT_artificial";
13042 case DW_AT_base_types:
13043 return "DW_AT_base_types";
13044 case DW_AT_calling_convention:
13045 return "DW_AT_calling_convention";
13046 case DW_AT_count:
13047 return "DW_AT_count";
13048 case DW_AT_data_member_location:
13049 return "DW_AT_data_member_location";
13050 case DW_AT_decl_column:
13051 return "DW_AT_decl_column";
13052 case DW_AT_decl_file:
13053 return "DW_AT_decl_file";
13054 case DW_AT_decl_line:
13055 return "DW_AT_decl_line";
13056 case DW_AT_declaration:
13057 return "DW_AT_declaration";
13058 case DW_AT_discr_list:
13059 return "DW_AT_discr_list";
13060 case DW_AT_encoding:
13061 return "DW_AT_encoding";
13062 case DW_AT_external:
13063 return "DW_AT_external";
13064 case DW_AT_frame_base:
13065 return "DW_AT_frame_base";
13066 case DW_AT_friend:
13067 return "DW_AT_friend";
13068 case DW_AT_identifier_case:
13069 return "DW_AT_identifier_case";
13070 case DW_AT_macro_info:
13071 return "DW_AT_macro_info";
13072 case DW_AT_namelist_items:
13073 return "DW_AT_namelist_items";
13074 case DW_AT_priority:
13075 return "DW_AT_priority";
13076 case DW_AT_segment:
13077 return "DW_AT_segment";
13078 case DW_AT_specification:
13079 return "DW_AT_specification";
13080 case DW_AT_static_link:
13081 return "DW_AT_static_link";
13082 case DW_AT_type:
13083 return "DW_AT_type";
13084 case DW_AT_use_location:
13085 return "DW_AT_use_location";
13086 case DW_AT_variable_parameter:
13087 return "DW_AT_variable_parameter";
13088 case DW_AT_virtuality:
13089 return "DW_AT_virtuality";
13090 case DW_AT_vtable_elem_location:
13091 return "DW_AT_vtable_elem_location";
b7619582 13092 /* DWARF 3 values. */
d9fa45fe
DC
13093 case DW_AT_allocated:
13094 return "DW_AT_allocated";
13095 case DW_AT_associated:
13096 return "DW_AT_associated";
13097 case DW_AT_data_location:
13098 return "DW_AT_data_location";
09fa0d7c
JK
13099 case DW_AT_byte_stride:
13100 return "DW_AT_byte_stride";
d9fa45fe
DC
13101 case DW_AT_entry_pc:
13102 return "DW_AT_entry_pc";
13103 case DW_AT_use_UTF8:
13104 return "DW_AT_use_UTF8";
13105 case DW_AT_extension:
13106 return "DW_AT_extension";
13107 case DW_AT_ranges:
13108 return "DW_AT_ranges";
13109 case DW_AT_trampoline:
13110 return "DW_AT_trampoline";
13111 case DW_AT_call_column:
13112 return "DW_AT_call_column";
13113 case DW_AT_call_file:
13114 return "DW_AT_call_file";
13115 case DW_AT_call_line:
13116 return "DW_AT_call_line";
b7619582
GF
13117 case DW_AT_description:
13118 return "DW_AT_description";
13119 case DW_AT_binary_scale:
13120 return "DW_AT_binary_scale";
13121 case DW_AT_decimal_scale:
13122 return "DW_AT_decimal_scale";
13123 case DW_AT_small:
13124 return "DW_AT_small";
13125 case DW_AT_decimal_sign:
13126 return "DW_AT_decimal_sign";
13127 case DW_AT_digit_count:
13128 return "DW_AT_digit_count";
13129 case DW_AT_picture_string:
13130 return "DW_AT_picture_string";
13131 case DW_AT_mutable:
13132 return "DW_AT_mutable";
13133 case DW_AT_threads_scaled:
13134 return "DW_AT_threads_scaled";
13135 case DW_AT_explicit:
13136 return "DW_AT_explicit";
13137 case DW_AT_object_pointer:
13138 return "DW_AT_object_pointer";
13139 case DW_AT_endianity:
13140 return "DW_AT_endianity";
13141 case DW_AT_elemental:
13142 return "DW_AT_elemental";
13143 case DW_AT_pure:
13144 return "DW_AT_pure";
13145 case DW_AT_recursive:
13146 return "DW_AT_recursive";
348e048f
DE
13147 /* DWARF 4 values. */
13148 case DW_AT_signature:
13149 return "DW_AT_signature";
31ef98ae
TT
13150 case DW_AT_linkage_name:
13151 return "DW_AT_linkage_name";
b7619582 13152 /* SGI/MIPS extensions. */
c764a876 13153#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13154 case DW_AT_MIPS_fde:
13155 return "DW_AT_MIPS_fde";
c764a876 13156#endif
c906108c
SS
13157 case DW_AT_MIPS_loop_begin:
13158 return "DW_AT_MIPS_loop_begin";
13159 case DW_AT_MIPS_tail_loop_begin:
13160 return "DW_AT_MIPS_tail_loop_begin";
13161 case DW_AT_MIPS_epilog_begin:
13162 return "DW_AT_MIPS_epilog_begin";
13163 case DW_AT_MIPS_loop_unroll_factor:
13164 return "DW_AT_MIPS_loop_unroll_factor";
13165 case DW_AT_MIPS_software_pipeline_depth:
13166 return "DW_AT_MIPS_software_pipeline_depth";
13167 case DW_AT_MIPS_linkage_name:
13168 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13169 case DW_AT_MIPS_stride:
13170 return "DW_AT_MIPS_stride";
13171 case DW_AT_MIPS_abstract_name:
13172 return "DW_AT_MIPS_abstract_name";
13173 case DW_AT_MIPS_clone_origin:
13174 return "DW_AT_MIPS_clone_origin";
13175 case DW_AT_MIPS_has_inlines:
13176 return "DW_AT_MIPS_has_inlines";
b7619582 13177 /* HP extensions. */
c764a876 13178#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13179 case DW_AT_HP_block_index:
13180 return "DW_AT_HP_block_index";
c764a876 13181#endif
b7619582
GF
13182 case DW_AT_HP_unmodifiable:
13183 return "DW_AT_HP_unmodifiable";
13184 case DW_AT_HP_actuals_stmt_list:
13185 return "DW_AT_HP_actuals_stmt_list";
13186 case DW_AT_HP_proc_per_section:
13187 return "DW_AT_HP_proc_per_section";
13188 case DW_AT_HP_raw_data_ptr:
13189 return "DW_AT_HP_raw_data_ptr";
13190 case DW_AT_HP_pass_by_reference:
13191 return "DW_AT_HP_pass_by_reference";
13192 case DW_AT_HP_opt_level:
13193 return "DW_AT_HP_opt_level";
13194 case DW_AT_HP_prof_version_id:
13195 return "DW_AT_HP_prof_version_id";
13196 case DW_AT_HP_opt_flags:
13197 return "DW_AT_HP_opt_flags";
13198 case DW_AT_HP_cold_region_low_pc:
13199 return "DW_AT_HP_cold_region_low_pc";
13200 case DW_AT_HP_cold_region_high_pc:
13201 return "DW_AT_HP_cold_region_high_pc";
13202 case DW_AT_HP_all_variables_modifiable:
13203 return "DW_AT_HP_all_variables_modifiable";
13204 case DW_AT_HP_linkage_name:
13205 return "DW_AT_HP_linkage_name";
13206 case DW_AT_HP_prof_flags:
13207 return "DW_AT_HP_prof_flags";
13208 /* GNU extensions. */
c906108c
SS
13209 case DW_AT_sf_names:
13210 return "DW_AT_sf_names";
13211 case DW_AT_src_info:
13212 return "DW_AT_src_info";
13213 case DW_AT_mac_info:
13214 return "DW_AT_mac_info";
13215 case DW_AT_src_coords:
13216 return "DW_AT_src_coords";
13217 case DW_AT_body_begin:
13218 return "DW_AT_body_begin";
13219 case DW_AT_body_end:
13220 return "DW_AT_body_end";
f5f8a009
EZ
13221 case DW_AT_GNU_vector:
13222 return "DW_AT_GNU_vector";
2de00c64
DE
13223 case DW_AT_GNU_odr_signature:
13224 return "DW_AT_GNU_odr_signature";
b7619582
GF
13225 /* VMS extensions. */
13226 case DW_AT_VMS_rtnbeg_pd_address:
13227 return "DW_AT_VMS_rtnbeg_pd_address";
13228 /* UPC extension. */
13229 case DW_AT_upc_threads_scaled:
13230 return "DW_AT_upc_threads_scaled";
13231 /* PGI (STMicroelectronics) extensions. */
13232 case DW_AT_PGI_lbase:
13233 return "DW_AT_PGI_lbase";
13234 case DW_AT_PGI_soffset:
13235 return "DW_AT_PGI_soffset";
13236 case DW_AT_PGI_lstride:
13237 return "DW_AT_PGI_lstride";
c906108c
SS
13238 default:
13239 return "DW_AT_<unknown>";
13240 }
13241}
13242
13243/* Convert a DWARF value form code into its string name. */
13244
13245static char *
aa1ee363 13246dwarf_form_name (unsigned form)
c906108c
SS
13247{
13248 switch (form)
13249 {
13250 case DW_FORM_addr:
13251 return "DW_FORM_addr";
13252 case DW_FORM_block2:
13253 return "DW_FORM_block2";
13254 case DW_FORM_block4:
13255 return "DW_FORM_block4";
13256 case DW_FORM_data2:
13257 return "DW_FORM_data2";
13258 case DW_FORM_data4:
13259 return "DW_FORM_data4";
13260 case DW_FORM_data8:
13261 return "DW_FORM_data8";
13262 case DW_FORM_string:
13263 return "DW_FORM_string";
13264 case DW_FORM_block:
13265 return "DW_FORM_block";
13266 case DW_FORM_block1:
13267 return "DW_FORM_block1";
13268 case DW_FORM_data1:
13269 return "DW_FORM_data1";
13270 case DW_FORM_flag:
13271 return "DW_FORM_flag";
13272 case DW_FORM_sdata:
13273 return "DW_FORM_sdata";
13274 case DW_FORM_strp:
13275 return "DW_FORM_strp";
13276 case DW_FORM_udata:
13277 return "DW_FORM_udata";
13278 case DW_FORM_ref_addr:
13279 return "DW_FORM_ref_addr";
13280 case DW_FORM_ref1:
13281 return "DW_FORM_ref1";
13282 case DW_FORM_ref2:
13283 return "DW_FORM_ref2";
13284 case DW_FORM_ref4:
13285 return "DW_FORM_ref4";
13286 case DW_FORM_ref8:
13287 return "DW_FORM_ref8";
13288 case DW_FORM_ref_udata:
13289 return "DW_FORM_ref_udata";
13290 case DW_FORM_indirect:
13291 return "DW_FORM_indirect";
348e048f
DE
13292 case DW_FORM_sec_offset:
13293 return "DW_FORM_sec_offset";
13294 case DW_FORM_exprloc:
13295 return "DW_FORM_exprloc";
13296 case DW_FORM_flag_present:
13297 return "DW_FORM_flag_present";
55f1336d
TT
13298 case DW_FORM_ref_sig8:
13299 return "DW_FORM_ref_sig8";
c906108c
SS
13300 default:
13301 return "DW_FORM_<unknown>";
13302 }
13303}
13304
13305/* Convert a DWARF stack opcode into its string name. */
13306
9eae7c52 13307const char *
b1bfef65 13308dwarf_stack_op_name (unsigned op)
c906108c
SS
13309{
13310 switch (op)
13311 {
13312 case DW_OP_addr:
13313 return "DW_OP_addr";
13314 case DW_OP_deref:
13315 return "DW_OP_deref";
13316 case DW_OP_const1u:
13317 return "DW_OP_const1u";
13318 case DW_OP_const1s:
13319 return "DW_OP_const1s";
13320 case DW_OP_const2u:
13321 return "DW_OP_const2u";
13322 case DW_OP_const2s:
13323 return "DW_OP_const2s";
13324 case DW_OP_const4u:
13325 return "DW_OP_const4u";
13326 case DW_OP_const4s:
13327 return "DW_OP_const4s";
13328 case DW_OP_const8u:
13329 return "DW_OP_const8u";
13330 case DW_OP_const8s:
13331 return "DW_OP_const8s";
13332 case DW_OP_constu:
13333 return "DW_OP_constu";
13334 case DW_OP_consts:
13335 return "DW_OP_consts";
13336 case DW_OP_dup:
13337 return "DW_OP_dup";
13338 case DW_OP_drop:
13339 return "DW_OP_drop";
13340 case DW_OP_over:
13341 return "DW_OP_over";
13342 case DW_OP_pick:
13343 return "DW_OP_pick";
13344 case DW_OP_swap:
13345 return "DW_OP_swap";
13346 case DW_OP_rot:
13347 return "DW_OP_rot";
13348 case DW_OP_xderef:
13349 return "DW_OP_xderef";
13350 case DW_OP_abs:
13351 return "DW_OP_abs";
13352 case DW_OP_and:
13353 return "DW_OP_and";
13354 case DW_OP_div:
13355 return "DW_OP_div";
13356 case DW_OP_minus:
13357 return "DW_OP_minus";
13358 case DW_OP_mod:
13359 return "DW_OP_mod";
13360 case DW_OP_mul:
13361 return "DW_OP_mul";
13362 case DW_OP_neg:
13363 return "DW_OP_neg";
13364 case DW_OP_not:
13365 return "DW_OP_not";
13366 case DW_OP_or:
13367 return "DW_OP_or";
13368 case DW_OP_plus:
13369 return "DW_OP_plus";
13370 case DW_OP_plus_uconst:
13371 return "DW_OP_plus_uconst";
13372 case DW_OP_shl:
13373 return "DW_OP_shl";
13374 case DW_OP_shr:
13375 return "DW_OP_shr";
13376 case DW_OP_shra:
13377 return "DW_OP_shra";
13378 case DW_OP_xor:
13379 return "DW_OP_xor";
13380 case DW_OP_bra:
13381 return "DW_OP_bra";
13382 case DW_OP_eq:
13383 return "DW_OP_eq";
13384 case DW_OP_ge:
13385 return "DW_OP_ge";
13386 case DW_OP_gt:
13387 return "DW_OP_gt";
13388 case DW_OP_le:
13389 return "DW_OP_le";
13390 case DW_OP_lt:
13391 return "DW_OP_lt";
13392 case DW_OP_ne:
13393 return "DW_OP_ne";
13394 case DW_OP_skip:
13395 return "DW_OP_skip";
13396 case DW_OP_lit0:
13397 return "DW_OP_lit0";
13398 case DW_OP_lit1:
13399 return "DW_OP_lit1";
13400 case DW_OP_lit2:
13401 return "DW_OP_lit2";
13402 case DW_OP_lit3:
13403 return "DW_OP_lit3";
13404 case DW_OP_lit4:
13405 return "DW_OP_lit4";
13406 case DW_OP_lit5:
13407 return "DW_OP_lit5";
13408 case DW_OP_lit6:
13409 return "DW_OP_lit6";
13410 case DW_OP_lit7:
13411 return "DW_OP_lit7";
13412 case DW_OP_lit8:
13413 return "DW_OP_lit8";
13414 case DW_OP_lit9:
13415 return "DW_OP_lit9";
13416 case DW_OP_lit10:
13417 return "DW_OP_lit10";
13418 case DW_OP_lit11:
13419 return "DW_OP_lit11";
13420 case DW_OP_lit12:
13421 return "DW_OP_lit12";
13422 case DW_OP_lit13:
13423 return "DW_OP_lit13";
13424 case DW_OP_lit14:
13425 return "DW_OP_lit14";
13426 case DW_OP_lit15:
13427 return "DW_OP_lit15";
13428 case DW_OP_lit16:
13429 return "DW_OP_lit16";
13430 case DW_OP_lit17:
13431 return "DW_OP_lit17";
13432 case DW_OP_lit18:
13433 return "DW_OP_lit18";
13434 case DW_OP_lit19:
13435 return "DW_OP_lit19";
13436 case DW_OP_lit20:
13437 return "DW_OP_lit20";
13438 case DW_OP_lit21:
13439 return "DW_OP_lit21";
13440 case DW_OP_lit22:
13441 return "DW_OP_lit22";
13442 case DW_OP_lit23:
13443 return "DW_OP_lit23";
13444 case DW_OP_lit24:
13445 return "DW_OP_lit24";
13446 case DW_OP_lit25:
13447 return "DW_OP_lit25";
13448 case DW_OP_lit26:
13449 return "DW_OP_lit26";
13450 case DW_OP_lit27:
13451 return "DW_OP_lit27";
13452 case DW_OP_lit28:
13453 return "DW_OP_lit28";
13454 case DW_OP_lit29:
13455 return "DW_OP_lit29";
13456 case DW_OP_lit30:
13457 return "DW_OP_lit30";
13458 case DW_OP_lit31:
13459 return "DW_OP_lit31";
13460 case DW_OP_reg0:
13461 return "DW_OP_reg0";
13462 case DW_OP_reg1:
13463 return "DW_OP_reg1";
13464 case DW_OP_reg2:
13465 return "DW_OP_reg2";
13466 case DW_OP_reg3:
13467 return "DW_OP_reg3";
13468 case DW_OP_reg4:
13469 return "DW_OP_reg4";
13470 case DW_OP_reg5:
13471 return "DW_OP_reg5";
13472 case DW_OP_reg6:
13473 return "DW_OP_reg6";
13474 case DW_OP_reg7:
13475 return "DW_OP_reg7";
13476 case DW_OP_reg8:
13477 return "DW_OP_reg8";
13478 case DW_OP_reg9:
13479 return "DW_OP_reg9";
13480 case DW_OP_reg10:
13481 return "DW_OP_reg10";
13482 case DW_OP_reg11:
13483 return "DW_OP_reg11";
13484 case DW_OP_reg12:
13485 return "DW_OP_reg12";
13486 case DW_OP_reg13:
13487 return "DW_OP_reg13";
13488 case DW_OP_reg14:
13489 return "DW_OP_reg14";
13490 case DW_OP_reg15:
13491 return "DW_OP_reg15";
13492 case DW_OP_reg16:
13493 return "DW_OP_reg16";
13494 case DW_OP_reg17:
13495 return "DW_OP_reg17";
13496 case DW_OP_reg18:
13497 return "DW_OP_reg18";
13498 case DW_OP_reg19:
13499 return "DW_OP_reg19";
13500 case DW_OP_reg20:
13501 return "DW_OP_reg20";
13502 case DW_OP_reg21:
13503 return "DW_OP_reg21";
13504 case DW_OP_reg22:
13505 return "DW_OP_reg22";
13506 case DW_OP_reg23:
13507 return "DW_OP_reg23";
13508 case DW_OP_reg24:
13509 return "DW_OP_reg24";
13510 case DW_OP_reg25:
13511 return "DW_OP_reg25";
13512 case DW_OP_reg26:
13513 return "DW_OP_reg26";
13514 case DW_OP_reg27:
13515 return "DW_OP_reg27";
13516 case DW_OP_reg28:
13517 return "DW_OP_reg28";
13518 case DW_OP_reg29:
13519 return "DW_OP_reg29";
13520 case DW_OP_reg30:
13521 return "DW_OP_reg30";
13522 case DW_OP_reg31:
13523 return "DW_OP_reg31";
13524 case DW_OP_breg0:
13525 return "DW_OP_breg0";
13526 case DW_OP_breg1:
13527 return "DW_OP_breg1";
13528 case DW_OP_breg2:
13529 return "DW_OP_breg2";
13530 case DW_OP_breg3:
13531 return "DW_OP_breg3";
13532 case DW_OP_breg4:
13533 return "DW_OP_breg4";
13534 case DW_OP_breg5:
13535 return "DW_OP_breg5";
13536 case DW_OP_breg6:
13537 return "DW_OP_breg6";
13538 case DW_OP_breg7:
13539 return "DW_OP_breg7";
13540 case DW_OP_breg8:
13541 return "DW_OP_breg8";
13542 case DW_OP_breg9:
13543 return "DW_OP_breg9";
13544 case DW_OP_breg10:
13545 return "DW_OP_breg10";
13546 case DW_OP_breg11:
13547 return "DW_OP_breg11";
13548 case DW_OP_breg12:
13549 return "DW_OP_breg12";
13550 case DW_OP_breg13:
13551 return "DW_OP_breg13";
13552 case DW_OP_breg14:
13553 return "DW_OP_breg14";
13554 case DW_OP_breg15:
13555 return "DW_OP_breg15";
13556 case DW_OP_breg16:
13557 return "DW_OP_breg16";
13558 case DW_OP_breg17:
13559 return "DW_OP_breg17";
13560 case DW_OP_breg18:
13561 return "DW_OP_breg18";
13562 case DW_OP_breg19:
13563 return "DW_OP_breg19";
13564 case DW_OP_breg20:
13565 return "DW_OP_breg20";
13566 case DW_OP_breg21:
13567 return "DW_OP_breg21";
13568 case DW_OP_breg22:
13569 return "DW_OP_breg22";
13570 case DW_OP_breg23:
13571 return "DW_OP_breg23";
13572 case DW_OP_breg24:
13573 return "DW_OP_breg24";
13574 case DW_OP_breg25:
13575 return "DW_OP_breg25";
13576 case DW_OP_breg26:
13577 return "DW_OP_breg26";
13578 case DW_OP_breg27:
13579 return "DW_OP_breg27";
13580 case DW_OP_breg28:
13581 return "DW_OP_breg28";
13582 case DW_OP_breg29:
13583 return "DW_OP_breg29";
13584 case DW_OP_breg30:
13585 return "DW_OP_breg30";
13586 case DW_OP_breg31:
13587 return "DW_OP_breg31";
13588 case DW_OP_regx:
13589 return "DW_OP_regx";
13590 case DW_OP_fbreg:
13591 return "DW_OP_fbreg";
13592 case DW_OP_bregx:
13593 return "DW_OP_bregx";
13594 case DW_OP_piece:
13595 return "DW_OP_piece";
13596 case DW_OP_deref_size:
13597 return "DW_OP_deref_size";
13598 case DW_OP_xderef_size:
13599 return "DW_OP_xderef_size";
13600 case DW_OP_nop:
13601 return "DW_OP_nop";
b7619582 13602 /* DWARF 3 extensions. */
ed348acc
EZ
13603 case DW_OP_push_object_address:
13604 return "DW_OP_push_object_address";
13605 case DW_OP_call2:
13606 return "DW_OP_call2";
13607 case DW_OP_call4:
13608 return "DW_OP_call4";
13609 case DW_OP_call_ref:
13610 return "DW_OP_call_ref";
b7619582
GF
13611 case DW_OP_form_tls_address:
13612 return "DW_OP_form_tls_address";
13613 case DW_OP_call_frame_cfa:
13614 return "DW_OP_call_frame_cfa";
13615 case DW_OP_bit_piece:
13616 return "DW_OP_bit_piece";
9eae7c52
TT
13617 /* DWARF 4 extensions. */
13618 case DW_OP_implicit_value:
13619 return "DW_OP_implicit_value";
13620 case DW_OP_stack_value:
13621 return "DW_OP_stack_value";
13622 /* GNU extensions. */
ed348acc
EZ
13623 case DW_OP_GNU_push_tls_address:
13624 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13625 case DW_OP_GNU_uninit:
13626 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13627 case DW_OP_GNU_implicit_pointer:
13628 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13629 case DW_OP_GNU_entry_value:
13630 return "DW_OP_GNU_entry_value";
13631 case DW_OP_GNU_const_type:
13632 return "DW_OP_GNU_const_type";
13633 case DW_OP_GNU_regval_type:
13634 return "DW_OP_GNU_regval_type";
13635 case DW_OP_GNU_deref_type:
13636 return "DW_OP_GNU_deref_type";
13637 case DW_OP_GNU_convert:
13638 return "DW_OP_GNU_convert";
13639 case DW_OP_GNU_reinterpret:
13640 return "DW_OP_GNU_reinterpret";
c906108c 13641 default:
b1bfef65 13642 return NULL;
c906108c
SS
13643 }
13644}
13645
13646static char *
fba45db2 13647dwarf_bool_name (unsigned mybool)
c906108c
SS
13648{
13649 if (mybool)
13650 return "TRUE";
13651 else
13652 return "FALSE";
13653}
13654
13655/* Convert a DWARF type code into its string name. */
13656
13657static char *
aa1ee363 13658dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13659{
13660 switch (enc)
13661 {
b7619582
GF
13662 case DW_ATE_void:
13663 return "DW_ATE_void";
c906108c
SS
13664 case DW_ATE_address:
13665 return "DW_ATE_address";
13666 case DW_ATE_boolean:
13667 return "DW_ATE_boolean";
13668 case DW_ATE_complex_float:
13669 return "DW_ATE_complex_float";
13670 case DW_ATE_float:
13671 return "DW_ATE_float";
13672 case DW_ATE_signed:
13673 return "DW_ATE_signed";
13674 case DW_ATE_signed_char:
13675 return "DW_ATE_signed_char";
13676 case DW_ATE_unsigned:
13677 return "DW_ATE_unsigned";
13678 case DW_ATE_unsigned_char:
13679 return "DW_ATE_unsigned_char";
b7619582 13680 /* DWARF 3. */
d9fa45fe
DC
13681 case DW_ATE_imaginary_float:
13682 return "DW_ATE_imaginary_float";
b7619582
GF
13683 case DW_ATE_packed_decimal:
13684 return "DW_ATE_packed_decimal";
13685 case DW_ATE_numeric_string:
13686 return "DW_ATE_numeric_string";
13687 case DW_ATE_edited:
13688 return "DW_ATE_edited";
13689 case DW_ATE_signed_fixed:
13690 return "DW_ATE_signed_fixed";
13691 case DW_ATE_unsigned_fixed:
13692 return "DW_ATE_unsigned_fixed";
13693 case DW_ATE_decimal_float:
13694 return "DW_ATE_decimal_float";
75079b2b
TT
13695 /* DWARF 4. */
13696 case DW_ATE_UTF:
13697 return "DW_ATE_UTF";
b7619582
GF
13698 /* HP extensions. */
13699 case DW_ATE_HP_float80:
13700 return "DW_ATE_HP_float80";
13701 case DW_ATE_HP_complex_float80:
13702 return "DW_ATE_HP_complex_float80";
13703 case DW_ATE_HP_float128:
13704 return "DW_ATE_HP_float128";
13705 case DW_ATE_HP_complex_float128:
13706 return "DW_ATE_HP_complex_float128";
13707 case DW_ATE_HP_floathpintel:
13708 return "DW_ATE_HP_floathpintel";
13709 case DW_ATE_HP_imaginary_float80:
13710 return "DW_ATE_HP_imaginary_float80";
13711 case DW_ATE_HP_imaginary_float128:
13712 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13713 default:
13714 return "DW_ATE_<unknown>";
13715 }
13716}
13717
0963b4bd 13718/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13719
13720#if 0
13721static char *
aa1ee363 13722dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13723{
13724 switch (cfi_opc)
13725 {
13726 case DW_CFA_advance_loc:
13727 return "DW_CFA_advance_loc";
13728 case DW_CFA_offset:
13729 return "DW_CFA_offset";
13730 case DW_CFA_restore:
13731 return "DW_CFA_restore";
13732 case DW_CFA_nop:
13733 return "DW_CFA_nop";
13734 case DW_CFA_set_loc:
13735 return "DW_CFA_set_loc";
13736 case DW_CFA_advance_loc1:
13737 return "DW_CFA_advance_loc1";
13738 case DW_CFA_advance_loc2:
13739 return "DW_CFA_advance_loc2";
13740 case DW_CFA_advance_loc4:
13741 return "DW_CFA_advance_loc4";
13742 case DW_CFA_offset_extended:
13743 return "DW_CFA_offset_extended";
13744 case DW_CFA_restore_extended:
13745 return "DW_CFA_restore_extended";
13746 case DW_CFA_undefined:
13747 return "DW_CFA_undefined";
13748 case DW_CFA_same_value:
13749 return "DW_CFA_same_value";
13750 case DW_CFA_register:
13751 return "DW_CFA_register";
13752 case DW_CFA_remember_state:
13753 return "DW_CFA_remember_state";
13754 case DW_CFA_restore_state:
13755 return "DW_CFA_restore_state";
13756 case DW_CFA_def_cfa:
13757 return "DW_CFA_def_cfa";
13758 case DW_CFA_def_cfa_register:
13759 return "DW_CFA_def_cfa_register";
13760 case DW_CFA_def_cfa_offset:
13761 return "DW_CFA_def_cfa_offset";
b7619582 13762 /* DWARF 3. */
985cb1a3
JM
13763 case DW_CFA_def_cfa_expression:
13764 return "DW_CFA_def_cfa_expression";
13765 case DW_CFA_expression:
13766 return "DW_CFA_expression";
13767 case DW_CFA_offset_extended_sf:
13768 return "DW_CFA_offset_extended_sf";
13769 case DW_CFA_def_cfa_sf:
13770 return "DW_CFA_def_cfa_sf";
13771 case DW_CFA_def_cfa_offset_sf:
13772 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13773 case DW_CFA_val_offset:
13774 return "DW_CFA_val_offset";
13775 case DW_CFA_val_offset_sf:
13776 return "DW_CFA_val_offset_sf";
13777 case DW_CFA_val_expression:
13778 return "DW_CFA_val_expression";
13779 /* SGI/MIPS specific. */
c906108c
SS
13780 case DW_CFA_MIPS_advance_loc8:
13781 return "DW_CFA_MIPS_advance_loc8";
b7619582 13782 /* GNU extensions. */
985cb1a3
JM
13783 case DW_CFA_GNU_window_save:
13784 return "DW_CFA_GNU_window_save";
13785 case DW_CFA_GNU_args_size:
13786 return "DW_CFA_GNU_args_size";
13787 case DW_CFA_GNU_negative_offset_extended:
13788 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13789 default:
13790 return "DW_CFA_<unknown>";
13791 }
13792}
13793#endif
13794
f9aca02d 13795static void
d97bc12b 13796dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13797{
13798 unsigned int i;
13799
d97bc12b
DE
13800 print_spaces (indent, f);
13801 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13802 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13803
13804 if (die->parent != NULL)
13805 {
13806 print_spaces (indent, f);
13807 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13808 die->parent->offset);
13809 }
13810
13811 print_spaces (indent, f);
13812 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13813 dwarf_bool_name (die->child != NULL));
c906108c 13814
d97bc12b
DE
13815 print_spaces (indent, f);
13816 fprintf_unfiltered (f, " attributes:\n");
13817
c906108c
SS
13818 for (i = 0; i < die->num_attrs; ++i)
13819 {
d97bc12b
DE
13820 print_spaces (indent, f);
13821 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13822 dwarf_attr_name (die->attrs[i].name),
13823 dwarf_form_name (die->attrs[i].form));
d97bc12b 13824
c906108c
SS
13825 switch (die->attrs[i].form)
13826 {
13827 case DW_FORM_ref_addr:
13828 case DW_FORM_addr:
d97bc12b 13829 fprintf_unfiltered (f, "address: ");
5af949e3 13830 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13831 break;
13832 case DW_FORM_block2:
13833 case DW_FORM_block4:
13834 case DW_FORM_block:
13835 case DW_FORM_block1:
3e43a32a
MS
13836 fprintf_unfiltered (f, "block: size %d",
13837 DW_BLOCK (&die->attrs[i])->size);
c906108c 13838 break;
2dc7f7b3
TT
13839 case DW_FORM_exprloc:
13840 fprintf_unfiltered (f, "expression: size %u",
13841 DW_BLOCK (&die->attrs[i])->size);
13842 break;
10b3939b
DJ
13843 case DW_FORM_ref1:
13844 case DW_FORM_ref2:
13845 case DW_FORM_ref4:
d97bc12b 13846 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13847 (long) (DW_ADDR (&die->attrs[i])));
13848 break;
c906108c
SS
13849 case DW_FORM_data1:
13850 case DW_FORM_data2:
13851 case DW_FORM_data4:
ce5d95e1 13852 case DW_FORM_data8:
c906108c
SS
13853 case DW_FORM_udata:
13854 case DW_FORM_sdata:
43bbcdc2
PH
13855 fprintf_unfiltered (f, "constant: %s",
13856 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13857 break;
2dc7f7b3
TT
13858 case DW_FORM_sec_offset:
13859 fprintf_unfiltered (f, "section offset: %s",
13860 pulongest (DW_UNSND (&die->attrs[i])));
13861 break;
55f1336d 13862 case DW_FORM_ref_sig8:
348e048f
DE
13863 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13864 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13865 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13866 else
13867 fprintf_unfiltered (f, "signatured type, offset: unknown");
13868 break;
c906108c 13869 case DW_FORM_string:
4bdf3d34 13870 case DW_FORM_strp:
8285870a 13871 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13872 DW_STRING (&die->attrs[i])
8285870a
JK
13873 ? DW_STRING (&die->attrs[i]) : "",
13874 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13875 break;
13876 case DW_FORM_flag:
13877 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13878 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13879 else
d97bc12b 13880 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13881 break;
2dc7f7b3
TT
13882 case DW_FORM_flag_present:
13883 fprintf_unfiltered (f, "flag: TRUE");
13884 break;
a8329558 13885 case DW_FORM_indirect:
0963b4bd
MS
13886 /* The reader will have reduced the indirect form to
13887 the "base form" so this form should not occur. */
3e43a32a
MS
13888 fprintf_unfiltered (f,
13889 "unexpected attribute form: DW_FORM_indirect");
a8329558 13890 break;
c906108c 13891 default:
d97bc12b 13892 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13893 die->attrs[i].form);
d97bc12b 13894 break;
c906108c 13895 }
d97bc12b 13896 fprintf_unfiltered (f, "\n");
c906108c
SS
13897 }
13898}
13899
f9aca02d 13900static void
d97bc12b 13901dump_die_for_error (struct die_info *die)
c906108c 13902{
d97bc12b
DE
13903 dump_die_shallow (gdb_stderr, 0, die);
13904}
13905
13906static void
13907dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13908{
13909 int indent = level * 4;
13910
13911 gdb_assert (die != NULL);
13912
13913 if (level >= max_level)
13914 return;
13915
13916 dump_die_shallow (f, indent, die);
13917
13918 if (die->child != NULL)
c906108c 13919 {
d97bc12b
DE
13920 print_spaces (indent, f);
13921 fprintf_unfiltered (f, " Children:");
13922 if (level + 1 < max_level)
13923 {
13924 fprintf_unfiltered (f, "\n");
13925 dump_die_1 (f, level + 1, max_level, die->child);
13926 }
13927 else
13928 {
3e43a32a
MS
13929 fprintf_unfiltered (f,
13930 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13931 }
13932 }
13933
13934 if (die->sibling != NULL && level > 0)
13935 {
13936 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13937 }
13938}
13939
d97bc12b
DE
13940/* This is called from the pdie macro in gdbinit.in.
13941 It's not static so gcc will keep a copy callable from gdb. */
13942
13943void
13944dump_die (struct die_info *die, int max_level)
13945{
13946 dump_die_1 (gdb_stdlog, 0, max_level, die);
13947}
13948
f9aca02d 13949static void
51545339 13950store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13951{
51545339 13952 void **slot;
c906108c 13953
51545339
DJ
13954 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13955
13956 *slot = die;
c906108c
SS
13957}
13958
93311388
DE
13959static int
13960is_ref_attr (struct attribute *attr)
c906108c 13961{
c906108c
SS
13962 switch (attr->form)
13963 {
13964 case DW_FORM_ref_addr:
c906108c
SS
13965 case DW_FORM_ref1:
13966 case DW_FORM_ref2:
13967 case DW_FORM_ref4:
613e1657 13968 case DW_FORM_ref8:
c906108c 13969 case DW_FORM_ref_udata:
93311388 13970 return 1;
c906108c 13971 default:
93311388 13972 return 0;
c906108c 13973 }
93311388
DE
13974}
13975
13976static unsigned int
13977dwarf2_get_ref_die_offset (struct attribute *attr)
13978{
13979 if (is_ref_attr (attr))
13980 return DW_ADDR (attr);
13981
13982 complaint (&symfile_complaints,
13983 _("unsupported die ref attribute form: '%s'"),
13984 dwarf_form_name (attr->form));
13985 return 0;
c906108c
SS
13986}
13987
43bbcdc2
PH
13988/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13989 * the value held by the attribute is not constant. */
a02abb62 13990
43bbcdc2 13991static LONGEST
a02abb62
JB
13992dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13993{
13994 if (attr->form == DW_FORM_sdata)
13995 return DW_SND (attr);
13996 else if (attr->form == DW_FORM_udata
13997 || attr->form == DW_FORM_data1
13998 || attr->form == DW_FORM_data2
13999 || attr->form == DW_FORM_data4
14000 || attr->form == DW_FORM_data8)
14001 return DW_UNSND (attr);
14002 else
14003 {
3e43a32a
MS
14004 complaint (&symfile_complaints,
14005 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14006 dwarf_form_name (attr->form));
14007 return default_value;
14008 }
14009}
14010
03dd20cc 14011/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14012 unit and add it to our queue.
14013 The result is non-zero if PER_CU was queued, otherwise the result is zero
14014 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14015
348e048f 14016static int
03dd20cc
DJ
14017maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14018 struct dwarf2_per_cu_data *per_cu)
14019{
98bfdba5
PA
14020 /* We may arrive here during partial symbol reading, if we need full
14021 DIEs to process an unusual case (e.g. template arguments). Do
14022 not queue PER_CU, just tell our caller to load its DIEs. */
14023 if (dwarf2_per_objfile->reading_partial_symbols)
14024 {
14025 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14026 return 1;
14027 return 0;
14028 }
14029
03dd20cc
DJ
14030 /* Mark the dependence relation so that we don't flush PER_CU
14031 too early. */
14032 dwarf2_add_dependence (this_cu, per_cu);
14033
14034 /* If it's already on the queue, we have nothing to do. */
14035 if (per_cu->queued)
348e048f 14036 return 0;
03dd20cc
DJ
14037
14038 /* If the compilation unit is already loaded, just mark it as
14039 used. */
14040 if (per_cu->cu != NULL)
14041 {
14042 per_cu->cu->last_used = 0;
348e048f 14043 return 0;
03dd20cc
DJ
14044 }
14045
14046 /* Add it to the queue. */
a0f42c21 14047 queue_comp_unit (per_cu);
348e048f
DE
14048
14049 return 1;
14050}
14051
14052/* Follow reference or signature attribute ATTR of SRC_DIE.
14053 On entry *REF_CU is the CU of SRC_DIE.
14054 On exit *REF_CU is the CU of the result. */
14055
14056static struct die_info *
14057follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14058 struct dwarf2_cu **ref_cu)
14059{
14060 struct die_info *die;
14061
14062 if (is_ref_attr (attr))
14063 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14064 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14065 die = follow_die_sig (src_die, attr, ref_cu);
14066 else
14067 {
14068 dump_die_for_error (src_die);
14069 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14070 (*ref_cu)->objfile->name);
14071 }
14072
14073 return die;
03dd20cc
DJ
14074}
14075
5c631832 14076/* Follow reference OFFSET.
673bfd45
DE
14077 On entry *REF_CU is the CU of the source die referencing OFFSET.
14078 On exit *REF_CU is the CU of the result.
14079 Returns NULL if OFFSET is invalid. */
f504f079 14080
f9aca02d 14081static struct die_info *
5c631832 14082follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14083{
10b3939b 14084 struct die_info temp_die;
f2f0e013 14085 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14086
348e048f
DE
14087 gdb_assert (cu->per_cu != NULL);
14088
98bfdba5
PA
14089 target_cu = cu;
14090
b0df02fd 14091 if (cu->per_cu->debug_types_section)
348e048f
DE
14092 {
14093 /* .debug_types CUs cannot reference anything outside their CU.
14094 If they need to, they have to reference a signatured type via
55f1336d 14095 DW_FORM_ref_sig8. */
348e048f 14096 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14097 return NULL;
348e048f
DE
14098 }
14099 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14100 {
14101 struct dwarf2_per_cu_data *per_cu;
9a619af0 14102
45452591 14103 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14104
14105 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14106 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14107 load_full_comp_unit (per_cu);
03dd20cc 14108
10b3939b
DJ
14109 target_cu = per_cu->cu;
14110 }
98bfdba5
PA
14111 else if (cu->dies == NULL)
14112 {
14113 /* We're loading full DIEs during partial symbol reading. */
14114 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14115 load_full_comp_unit (cu->per_cu);
98bfdba5 14116 }
c906108c 14117
f2f0e013 14118 *ref_cu = target_cu;
51545339 14119 temp_die.offset = offset;
5c631832
JK
14120 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14121}
10b3939b 14122
5c631832
JK
14123/* Follow reference attribute ATTR of SRC_DIE.
14124 On entry *REF_CU is the CU of SRC_DIE.
14125 On exit *REF_CU is the CU of the result. */
14126
14127static struct die_info *
14128follow_die_ref (struct die_info *src_die, struct attribute *attr,
14129 struct dwarf2_cu **ref_cu)
14130{
14131 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14132 struct dwarf2_cu *cu = *ref_cu;
14133 struct die_info *die;
14134
14135 die = follow_die_offset (offset, ref_cu);
14136 if (!die)
14137 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14138 "at 0x%x [in module %s]"),
14139 offset, src_die->offset, cu->objfile->name);
348e048f 14140
5c631832
JK
14141 return die;
14142}
14143
d83e736b
JK
14144/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14145 Returned value is intended for DW_OP_call*. Returned
14146 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14147
14148struct dwarf2_locexpr_baton
14149dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14150 struct dwarf2_per_cu_data *per_cu,
14151 CORE_ADDR (*get_frame_pc) (void *baton),
14152 void *baton)
5c631832 14153{
918dd910 14154 struct dwarf2_cu *cu;
5c631832
JK
14155 struct die_info *die;
14156 struct attribute *attr;
14157 struct dwarf2_locexpr_baton retval;
14158
8cf6f0b1
TT
14159 dw2_setup (per_cu->objfile);
14160
918dd910
JK
14161 if (per_cu->cu == NULL)
14162 load_cu (per_cu);
14163 cu = per_cu->cu;
14164
5c631832
JK
14165 die = follow_die_offset (offset, &cu);
14166 if (!die)
14167 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
bb5ed363 14168 offset, per_cu->objfile->name);
5c631832
JK
14169
14170 attr = dwarf2_attr (die, DW_AT_location, cu);
14171 if (!attr)
14172 {
e103e986
JK
14173 /* DWARF: "If there is no such attribute, then there is no effect.".
14174 DATA is ignored if SIZE is 0. */
5c631832 14175
e103e986 14176 retval.data = NULL;
5c631832
JK
14177 retval.size = 0;
14178 }
8cf6f0b1
TT
14179 else if (attr_form_is_section_offset (attr))
14180 {
14181 struct dwarf2_loclist_baton loclist_baton;
14182 CORE_ADDR pc = (*get_frame_pc) (baton);
14183 size_t size;
14184
14185 fill_in_loclist_baton (cu, &loclist_baton, attr);
14186
14187 retval.data = dwarf2_find_location_expression (&loclist_baton,
14188 &size, pc);
14189 retval.size = size;
14190 }
5c631832
JK
14191 else
14192 {
14193 if (!attr_form_is_block (attr))
14194 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14195 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
bb5ed363 14196 offset, per_cu->objfile->name);
5c631832
JK
14197
14198 retval.data = DW_BLOCK (attr)->data;
14199 retval.size = DW_BLOCK (attr)->size;
14200 }
14201 retval.per_cu = cu->per_cu;
918dd910 14202
918dd910
JK
14203 age_cached_comp_units ();
14204
5c631832 14205 return retval;
348e048f
DE
14206}
14207
8a9b8146
TT
14208/* Return the type of the DIE at DIE_OFFSET in the CU named by
14209 PER_CU. */
14210
14211struct type *
14212dwarf2_get_die_type (unsigned int die_offset,
14213 struct dwarf2_per_cu_data *per_cu)
14214{
8a9b8146 14215 dw2_setup (per_cu->objfile);
9ff3b74f 14216 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14217}
14218
348e048f
DE
14219/* Follow the signature attribute ATTR in SRC_DIE.
14220 On entry *REF_CU is the CU of SRC_DIE.
14221 On exit *REF_CU is the CU of the result. */
14222
14223static struct die_info *
14224follow_die_sig (struct die_info *src_die, struct attribute *attr,
14225 struct dwarf2_cu **ref_cu)
14226{
14227 struct objfile *objfile = (*ref_cu)->objfile;
14228 struct die_info temp_die;
14229 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14230 struct dwarf2_cu *sig_cu;
14231 struct die_info *die;
14232
14233 /* sig_type will be NULL if the signatured type is missing from
14234 the debug info. */
14235 if (sig_type == NULL)
14236 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14237 "at 0x%x [in module %s]"),
14238 src_die->offset, objfile->name);
14239
14240 /* If necessary, add it to the queue and load its DIEs. */
14241
14242 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14243 read_signatured_type (sig_type);
348e048f
DE
14244
14245 gdb_assert (sig_type->per_cu.cu != NULL);
14246
14247 sig_cu = sig_type->per_cu.cu;
14248 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14249 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14250 if (die)
14251 {
14252 *ref_cu = sig_cu;
14253 return die;
14254 }
14255
3e43a32a
MS
14256 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14257 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14258 sig_type->type_offset, src_die->offset, objfile->name);
14259}
14260
14261/* Given an offset of a signatured type, return its signatured_type. */
14262
14263static struct signatured_type *
8b70b953
TT
14264lookup_signatured_type_at_offset (struct objfile *objfile,
14265 struct dwarf2_section_info *section,
14266 unsigned int offset)
348e048f 14267{
8b70b953 14268 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14269 unsigned int length, initial_length_size;
14270 unsigned int sig_offset;
14271 struct signatured_type find_entry, *type_sig;
14272
14273 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14274 sig_offset = (initial_length_size
14275 + 2 /*version*/
14276 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14277 + 1 /*address_size*/);
14278 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14279 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14280
14281 /* This is only used to lookup previously recorded types.
14282 If we didn't find it, it's our bug. */
14283 gdb_assert (type_sig != NULL);
b3c8eb43 14284 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14285
14286 return type_sig;
14287}
14288
e5fe5e75 14289/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14290
14291static void
e5fe5e75 14292load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14293{
e5fe5e75
DE
14294 struct objfile *objfile = per_cu->objfile;
14295 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14296 unsigned int offset = per_cu->offset;
348e048f
DE
14297 struct signatured_type *type_sig;
14298
8b70b953 14299 dwarf2_read_section (objfile, sect);
be391dca 14300
348e048f 14301 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14302 hash table lookup. */
14303 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14304 the signature to assert we found the right one.
14305 Ok, but it's a lot of work. We should simplify things so any needed
14306 assert doesn't require all this clumsiness. */
8b70b953 14307 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14308
14309 gdb_assert (type_sig->per_cu.cu == NULL);
14310
a0f42c21 14311 read_signatured_type (type_sig);
348e048f
DE
14312
14313 gdb_assert (type_sig->per_cu.cu != NULL);
14314}
14315
14316/* Read in a signatured type and build its CU and DIEs. */
14317
14318static void
a0f42c21 14319read_signatured_type (struct signatured_type *type_sig)
348e048f 14320{
a0f42c21 14321 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14322 gdb_byte *types_ptr;
348e048f
DE
14323 struct die_reader_specs reader_specs;
14324 struct dwarf2_cu *cu;
14325 ULONGEST signature;
14326 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14327 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14328
8b70b953
TT
14329 dwarf2_read_section (objfile, section);
14330 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14331
348e048f
DE
14332 gdb_assert (type_sig->per_cu.cu == NULL);
14333
9816fde3 14334 cu = xmalloc (sizeof (*cu));
23745b47 14335 init_one_comp_unit (cu, &type_sig->per_cu);
348e048f
DE
14336
14337 /* If an error occurs while loading, release our storage. */
68dc6402 14338 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14339
8b70b953 14340 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14341 types_ptr, objfile->obfd);
14342 gdb_assert (signature == type_sig->signature);
14343
14344 cu->die_hash
14345 = htab_create_alloc_ex (cu->header.length / 12,
14346 die_hash,
14347 die_eq,
14348 NULL,
14349 &cu->comp_unit_obstack,
14350 hashtab_obstack_allocate,
14351 dummy_obstack_deallocate);
14352
e5fe5e75 14353 dwarf2_read_abbrevs (cu);
348e048f
DE
14354 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14355
14356 init_cu_die_reader (&reader_specs, cu);
14357
14358 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14359 NULL /*parent*/);
14360
14361 /* We try not to read any attributes in this function, because not
9cdd5dbd 14362 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14363 table processing isn't initialized. But we have to set the CU language,
14364 or we won't be able to build types correctly. */
9816fde3 14365 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14366
14367 do_cleanups (back_to);
14368
14369 /* We've successfully allocated this compilation unit. Let our caller
14370 clean it up when finished with it. */
14371 discard_cleanups (free_cu_cleanup);
14372
14373 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14374 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14375}
14376
c906108c
SS
14377/* Decode simple location descriptions.
14378 Given a pointer to a dwarf block that defines a location, compute
14379 the location and return the value.
14380
4cecd739
DJ
14381 NOTE drow/2003-11-18: This function is called in two situations
14382 now: for the address of static or global variables (partial symbols
14383 only) and for offsets into structures which are expected to be
14384 (more or less) constant. The partial symbol case should go away,
14385 and only the constant case should remain. That will let this
14386 function complain more accurately. A few special modes are allowed
14387 without complaint for global variables (for instance, global
14388 register values and thread-local values).
c906108c
SS
14389
14390 A location description containing no operations indicates that the
4cecd739 14391 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14392 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14393 callers will only want a very basic result and this can become a
21ae7a4d
JK
14394 complaint.
14395
14396 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14397
14398static CORE_ADDR
e7c27a73 14399decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14400{
e7c27a73 14401 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14402 int i;
14403 int size = blk->size;
14404 gdb_byte *data = blk->data;
14405 CORE_ADDR stack[64];
14406 int stacki;
14407 unsigned int bytes_read, unsnd;
14408 gdb_byte op;
c906108c 14409
21ae7a4d
JK
14410 i = 0;
14411 stacki = 0;
14412 stack[stacki] = 0;
14413 stack[++stacki] = 0;
14414
14415 while (i < size)
14416 {
14417 op = data[i++];
14418 switch (op)
14419 {
14420 case DW_OP_lit0:
14421 case DW_OP_lit1:
14422 case DW_OP_lit2:
14423 case DW_OP_lit3:
14424 case DW_OP_lit4:
14425 case DW_OP_lit5:
14426 case DW_OP_lit6:
14427 case DW_OP_lit7:
14428 case DW_OP_lit8:
14429 case DW_OP_lit9:
14430 case DW_OP_lit10:
14431 case DW_OP_lit11:
14432 case DW_OP_lit12:
14433 case DW_OP_lit13:
14434 case DW_OP_lit14:
14435 case DW_OP_lit15:
14436 case DW_OP_lit16:
14437 case DW_OP_lit17:
14438 case DW_OP_lit18:
14439 case DW_OP_lit19:
14440 case DW_OP_lit20:
14441 case DW_OP_lit21:
14442 case DW_OP_lit22:
14443 case DW_OP_lit23:
14444 case DW_OP_lit24:
14445 case DW_OP_lit25:
14446 case DW_OP_lit26:
14447 case DW_OP_lit27:
14448 case DW_OP_lit28:
14449 case DW_OP_lit29:
14450 case DW_OP_lit30:
14451 case DW_OP_lit31:
14452 stack[++stacki] = op - DW_OP_lit0;
14453 break;
f1bea926 14454
21ae7a4d
JK
14455 case DW_OP_reg0:
14456 case DW_OP_reg1:
14457 case DW_OP_reg2:
14458 case DW_OP_reg3:
14459 case DW_OP_reg4:
14460 case DW_OP_reg5:
14461 case DW_OP_reg6:
14462 case DW_OP_reg7:
14463 case DW_OP_reg8:
14464 case DW_OP_reg9:
14465 case DW_OP_reg10:
14466 case DW_OP_reg11:
14467 case DW_OP_reg12:
14468 case DW_OP_reg13:
14469 case DW_OP_reg14:
14470 case DW_OP_reg15:
14471 case DW_OP_reg16:
14472 case DW_OP_reg17:
14473 case DW_OP_reg18:
14474 case DW_OP_reg19:
14475 case DW_OP_reg20:
14476 case DW_OP_reg21:
14477 case DW_OP_reg22:
14478 case DW_OP_reg23:
14479 case DW_OP_reg24:
14480 case DW_OP_reg25:
14481 case DW_OP_reg26:
14482 case DW_OP_reg27:
14483 case DW_OP_reg28:
14484 case DW_OP_reg29:
14485 case DW_OP_reg30:
14486 case DW_OP_reg31:
14487 stack[++stacki] = op - DW_OP_reg0;
14488 if (i < size)
14489 dwarf2_complex_location_expr_complaint ();
14490 break;
c906108c 14491
21ae7a4d
JK
14492 case DW_OP_regx:
14493 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14494 i += bytes_read;
14495 stack[++stacki] = unsnd;
14496 if (i < size)
14497 dwarf2_complex_location_expr_complaint ();
14498 break;
c906108c 14499
21ae7a4d
JK
14500 case DW_OP_addr:
14501 stack[++stacki] = read_address (objfile->obfd, &data[i],
14502 cu, &bytes_read);
14503 i += bytes_read;
14504 break;
d53d4ac5 14505
21ae7a4d
JK
14506 case DW_OP_const1u:
14507 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14508 i += 1;
14509 break;
14510
14511 case DW_OP_const1s:
14512 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14513 i += 1;
14514 break;
14515
14516 case DW_OP_const2u:
14517 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14518 i += 2;
14519 break;
14520
14521 case DW_OP_const2s:
14522 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14523 i += 2;
14524 break;
d53d4ac5 14525
21ae7a4d
JK
14526 case DW_OP_const4u:
14527 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14528 i += 4;
14529 break;
14530
14531 case DW_OP_const4s:
14532 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14533 i += 4;
14534 break;
14535
585861ea
JK
14536 case DW_OP_const8u:
14537 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14538 i += 8;
14539 break;
14540
21ae7a4d
JK
14541 case DW_OP_constu:
14542 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14543 &bytes_read);
14544 i += bytes_read;
14545 break;
14546
14547 case DW_OP_consts:
14548 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14549 i += bytes_read;
14550 break;
14551
14552 case DW_OP_dup:
14553 stack[stacki + 1] = stack[stacki];
14554 stacki++;
14555 break;
14556
14557 case DW_OP_plus:
14558 stack[stacki - 1] += stack[stacki];
14559 stacki--;
14560 break;
14561
14562 case DW_OP_plus_uconst:
14563 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14564 &bytes_read);
14565 i += bytes_read;
14566 break;
14567
14568 case DW_OP_minus:
14569 stack[stacki - 1] -= stack[stacki];
14570 stacki--;
14571 break;
14572
14573 case DW_OP_deref:
14574 /* If we're not the last op, then we definitely can't encode
14575 this using GDB's address_class enum. This is valid for partial
14576 global symbols, although the variable's address will be bogus
14577 in the psymtab. */
14578 if (i < size)
14579 dwarf2_complex_location_expr_complaint ();
14580 break;
14581
14582 case DW_OP_GNU_push_tls_address:
14583 /* The top of the stack has the offset from the beginning
14584 of the thread control block at which the variable is located. */
14585 /* Nothing should follow this operator, so the top of stack would
14586 be returned. */
14587 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14588 address will be bogus in the psymtab. Make it always at least
14589 non-zero to not look as a variable garbage collected by linker
14590 which have DW_OP_addr 0. */
21ae7a4d
JK
14591 if (i < size)
14592 dwarf2_complex_location_expr_complaint ();
585861ea 14593 stack[stacki]++;
21ae7a4d
JK
14594 break;
14595
14596 case DW_OP_GNU_uninit:
14597 break;
14598
14599 default:
14600 {
14601 const char *name = dwarf_stack_op_name (op);
14602
14603 if (name)
14604 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14605 name);
14606 else
14607 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14608 op);
14609 }
14610
14611 return (stack[stacki]);
d53d4ac5 14612 }
3c6e0cb3 14613
21ae7a4d
JK
14614 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14615 outside of the allocated space. Also enforce minimum>0. */
14616 if (stacki >= ARRAY_SIZE (stack) - 1)
14617 {
14618 complaint (&symfile_complaints,
14619 _("location description stack overflow"));
14620 return 0;
14621 }
14622
14623 if (stacki <= 0)
14624 {
14625 complaint (&symfile_complaints,
14626 _("location description stack underflow"));
14627 return 0;
14628 }
14629 }
14630 return (stack[stacki]);
c906108c
SS
14631}
14632
14633/* memory allocation interface */
14634
c906108c 14635static struct dwarf_block *
7b5a2f43 14636dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14637{
14638 struct dwarf_block *blk;
14639
14640 blk = (struct dwarf_block *)
7b5a2f43 14641 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14642 return (blk);
14643}
14644
14645static struct abbrev_info *
f3dd6933 14646dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14647{
14648 struct abbrev_info *abbrev;
14649
f3dd6933
DJ
14650 abbrev = (struct abbrev_info *)
14651 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14652 memset (abbrev, 0, sizeof (struct abbrev_info));
14653 return (abbrev);
14654}
14655
14656static struct die_info *
b60c80d6 14657dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14658{
14659 struct die_info *die;
b60c80d6
DJ
14660 size_t size = sizeof (struct die_info);
14661
14662 if (num_attrs > 1)
14663 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14664
b60c80d6 14665 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14666 memset (die, 0, sizeof (struct die_info));
14667 return (die);
14668}
2e276125
JB
14669
14670\f
14671/* Macro support. */
14672
2e276125
JB
14673/* Return the full name of file number I in *LH's file name table.
14674 Use COMP_DIR as the name of the current directory of the
14675 compilation. The result is allocated using xmalloc; the caller is
14676 responsible for freeing it. */
14677static char *
14678file_full_name (int file, struct line_header *lh, const char *comp_dir)
14679{
6a83a1e6
EZ
14680 /* Is the file number a valid index into the line header's file name
14681 table? Remember that file numbers start with one, not zero. */
14682 if (1 <= file && file <= lh->num_file_names)
14683 {
14684 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14685
6a83a1e6
EZ
14686 if (IS_ABSOLUTE_PATH (fe->name))
14687 return xstrdup (fe->name);
14688 else
14689 {
14690 const char *dir;
14691 int dir_len;
14692 char *full_name;
14693
14694 if (fe->dir_index)
14695 dir = lh->include_dirs[fe->dir_index - 1];
14696 else
14697 dir = comp_dir;
14698
14699 if (dir)
14700 {
14701 dir_len = strlen (dir);
14702 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14703 strcpy (full_name, dir);
14704 full_name[dir_len] = '/';
14705 strcpy (full_name + dir_len + 1, fe->name);
14706 return full_name;
14707 }
14708 else
14709 return xstrdup (fe->name);
14710 }
14711 }
2e276125
JB
14712 else
14713 {
6a83a1e6
EZ
14714 /* The compiler produced a bogus file number. We can at least
14715 record the macro definitions made in the file, even if we
14716 won't be able to find the file by name. */
14717 char fake_name[80];
9a619af0 14718
6a83a1e6 14719 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14720
6e70227d 14721 complaint (&symfile_complaints,
6a83a1e6
EZ
14722 _("bad file number in macro information (%d)"),
14723 file);
2e276125 14724
6a83a1e6 14725 return xstrdup (fake_name);
2e276125
JB
14726 }
14727}
14728
14729
14730static struct macro_source_file *
14731macro_start_file (int file, int line,
14732 struct macro_source_file *current_file,
14733 const char *comp_dir,
14734 struct line_header *lh, struct objfile *objfile)
14735{
14736 /* The full name of this source file. */
14737 char *full_name = file_full_name (file, lh, comp_dir);
14738
14739 /* We don't create a macro table for this compilation unit
14740 at all until we actually get a filename. */
14741 if (! pending_macros)
4a146b47 14742 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14743 objfile->macro_cache);
2e276125
JB
14744
14745 if (! current_file)
14746 /* If we have no current file, then this must be the start_file
14747 directive for the compilation unit's main source file. */
14748 current_file = macro_set_main (pending_macros, full_name);
14749 else
14750 current_file = macro_include (current_file, line, full_name);
14751
14752 xfree (full_name);
6e70227d 14753
2e276125
JB
14754 return current_file;
14755}
14756
14757
14758/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14759 followed by a null byte. */
14760static char *
14761copy_string (const char *buf, int len)
14762{
14763 char *s = xmalloc (len + 1);
9a619af0 14764
2e276125
JB
14765 memcpy (s, buf, len);
14766 s[len] = '\0';
2e276125
JB
14767 return s;
14768}
14769
14770
14771static const char *
14772consume_improper_spaces (const char *p, const char *body)
14773{
14774 if (*p == ' ')
14775 {
4d3c2250 14776 complaint (&symfile_complaints,
3e43a32a
MS
14777 _("macro definition contains spaces "
14778 "in formal argument list:\n`%s'"),
4d3c2250 14779 body);
2e276125
JB
14780
14781 while (*p == ' ')
14782 p++;
14783 }
14784
14785 return p;
14786}
14787
14788
14789static void
14790parse_macro_definition (struct macro_source_file *file, int line,
14791 const char *body)
14792{
14793 const char *p;
14794
14795 /* The body string takes one of two forms. For object-like macro
14796 definitions, it should be:
14797
14798 <macro name> " " <definition>
14799
14800 For function-like macro definitions, it should be:
14801
14802 <macro name> "() " <definition>
14803 or
14804 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14805
14806 Spaces may appear only where explicitly indicated, and in the
14807 <definition>.
14808
14809 The Dwarf 2 spec says that an object-like macro's name is always
14810 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14811 the space when the macro's definition is the empty string.
2e276125
JB
14812
14813 The Dwarf 2 spec says that there should be no spaces between the
14814 formal arguments in a function-like macro's formal argument list,
14815 but versions of GCC around March 2002 include spaces after the
14816 commas. */
14817
14818
14819 /* Find the extent of the macro name. The macro name is terminated
14820 by either a space or null character (for an object-like macro) or
14821 an opening paren (for a function-like macro). */
14822 for (p = body; *p; p++)
14823 if (*p == ' ' || *p == '(')
14824 break;
14825
14826 if (*p == ' ' || *p == '\0')
14827 {
14828 /* It's an object-like macro. */
14829 int name_len = p - body;
14830 char *name = copy_string (body, name_len);
14831 const char *replacement;
14832
14833 if (*p == ' ')
14834 replacement = body + name_len + 1;
14835 else
14836 {
4d3c2250 14837 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14838 replacement = body + name_len;
14839 }
6e70227d 14840
2e276125
JB
14841 macro_define_object (file, line, name, replacement);
14842
14843 xfree (name);
14844 }
14845 else if (*p == '(')
14846 {
14847 /* It's a function-like macro. */
14848 char *name = copy_string (body, p - body);
14849 int argc = 0;
14850 int argv_size = 1;
14851 char **argv = xmalloc (argv_size * sizeof (*argv));
14852
14853 p++;
14854
14855 p = consume_improper_spaces (p, body);
14856
14857 /* Parse the formal argument list. */
14858 while (*p && *p != ')')
14859 {
14860 /* Find the extent of the current argument name. */
14861 const char *arg_start = p;
14862
14863 while (*p && *p != ',' && *p != ')' && *p != ' ')
14864 p++;
14865
14866 if (! *p || p == arg_start)
4d3c2250 14867 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14868 else
14869 {
14870 /* Make sure argv has room for the new argument. */
14871 if (argc >= argv_size)
14872 {
14873 argv_size *= 2;
14874 argv = xrealloc (argv, argv_size * sizeof (*argv));
14875 }
14876
14877 argv[argc++] = copy_string (arg_start, p - arg_start);
14878 }
14879
14880 p = consume_improper_spaces (p, body);
14881
14882 /* Consume the comma, if present. */
14883 if (*p == ',')
14884 {
14885 p++;
14886
14887 p = consume_improper_spaces (p, body);
14888 }
14889 }
14890
14891 if (*p == ')')
14892 {
14893 p++;
14894
14895 if (*p == ' ')
14896 /* Perfectly formed definition, no complaints. */
14897 macro_define_function (file, line, name,
6e70227d 14898 argc, (const char **) argv,
2e276125
JB
14899 p + 1);
14900 else if (*p == '\0')
14901 {
14902 /* Complain, but do define it. */
4d3c2250 14903 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14904 macro_define_function (file, line, name,
6e70227d 14905 argc, (const char **) argv,
2e276125
JB
14906 p);
14907 }
14908 else
14909 /* Just complain. */
4d3c2250 14910 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14911 }
14912 else
14913 /* Just complain. */
4d3c2250 14914 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14915
14916 xfree (name);
14917 {
14918 int i;
14919
14920 for (i = 0; i < argc; i++)
14921 xfree (argv[i]);
14922 }
14923 xfree (argv);
14924 }
14925 else
4d3c2250 14926 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14927}
14928
cf2c3c16
TT
14929/* Skip some bytes from BYTES according to the form given in FORM.
14930 Returns the new pointer. */
2e276125 14931
cf2c3c16
TT
14932static gdb_byte *
14933skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14934 enum dwarf_form form,
14935 unsigned int offset_size,
14936 struct dwarf2_section_info *section)
2e276125 14937{
cf2c3c16 14938 unsigned int bytes_read;
2e276125 14939
cf2c3c16 14940 switch (form)
2e276125 14941 {
cf2c3c16
TT
14942 case DW_FORM_data1:
14943 case DW_FORM_flag:
14944 ++bytes;
14945 break;
14946
14947 case DW_FORM_data2:
14948 bytes += 2;
14949 break;
14950
14951 case DW_FORM_data4:
14952 bytes += 4;
14953 break;
14954
14955 case DW_FORM_data8:
14956 bytes += 8;
14957 break;
14958
14959 case DW_FORM_string:
14960 read_direct_string (abfd, bytes, &bytes_read);
14961 bytes += bytes_read;
14962 break;
14963
14964 case DW_FORM_sec_offset:
14965 case DW_FORM_strp:
14966 bytes += offset_size;
14967 break;
14968
14969 case DW_FORM_block:
14970 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14971 bytes += bytes_read;
14972 break;
14973
14974 case DW_FORM_block1:
14975 bytes += 1 + read_1_byte (abfd, bytes);
14976 break;
14977 case DW_FORM_block2:
14978 bytes += 2 + read_2_bytes (abfd, bytes);
14979 break;
14980 case DW_FORM_block4:
14981 bytes += 4 + read_4_bytes (abfd, bytes);
14982 break;
14983
14984 case DW_FORM_sdata:
14985 case DW_FORM_udata:
14986 bytes = skip_leb128 (abfd, bytes);
14987 break;
14988
14989 default:
14990 {
14991 complain:
14992 complaint (&symfile_complaints,
14993 _("invalid form 0x%x in `%s'"),
14994 form,
14995 section->asection->name);
14996 return NULL;
14997 }
2e276125
JB
14998 }
14999
cf2c3c16
TT
15000 return bytes;
15001}
757a13d0 15002
cf2c3c16
TT
15003/* A helper for dwarf_decode_macros that handles skipping an unknown
15004 opcode. Returns an updated pointer to the macro data buffer; or,
15005 on error, issues a complaint and returns NULL. */
757a13d0 15006
cf2c3c16
TT
15007static gdb_byte *
15008skip_unknown_opcode (unsigned int opcode,
15009 gdb_byte **opcode_definitions,
15010 gdb_byte *mac_ptr,
15011 bfd *abfd,
15012 unsigned int offset_size,
15013 struct dwarf2_section_info *section)
15014{
15015 unsigned int bytes_read, i;
15016 unsigned long arg;
15017 gdb_byte *defn;
2e276125 15018
cf2c3c16 15019 if (opcode_definitions[opcode] == NULL)
2e276125 15020 {
cf2c3c16
TT
15021 complaint (&symfile_complaints,
15022 _("unrecognized DW_MACFINO opcode 0x%x"),
15023 opcode);
15024 return NULL;
15025 }
2e276125 15026
cf2c3c16
TT
15027 defn = opcode_definitions[opcode];
15028 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15029 defn += bytes_read;
2e276125 15030
cf2c3c16
TT
15031 for (i = 0; i < arg; ++i)
15032 {
15033 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15034 if (mac_ptr == NULL)
15035 {
15036 /* skip_form_bytes already issued the complaint. */
15037 return NULL;
15038 }
15039 }
757a13d0 15040
cf2c3c16
TT
15041 return mac_ptr;
15042}
757a13d0 15043
cf2c3c16
TT
15044/* A helper function which parses the header of a macro section.
15045 If the macro section is the extended (for now called "GNU") type,
15046 then this updates *OFFSET_SIZE. Returns a pointer to just after
15047 the header, or issues a complaint and returns NULL on error. */
757a13d0 15048
cf2c3c16
TT
15049static gdb_byte *
15050dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15051 bfd *abfd,
15052 gdb_byte *mac_ptr,
15053 unsigned int *offset_size,
15054 int section_is_gnu)
15055{
15056 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15057
cf2c3c16
TT
15058 if (section_is_gnu)
15059 {
15060 unsigned int version, flags;
757a13d0 15061
cf2c3c16
TT
15062 version = read_2_bytes (abfd, mac_ptr);
15063 if (version != 4)
15064 {
15065 complaint (&symfile_complaints,
15066 _("unrecognized version `%d' in .debug_macro section"),
15067 version);
15068 return NULL;
15069 }
15070 mac_ptr += 2;
757a13d0 15071
cf2c3c16
TT
15072 flags = read_1_byte (abfd, mac_ptr);
15073 ++mac_ptr;
15074 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15075
cf2c3c16
TT
15076 if ((flags & 2) != 0)
15077 /* We don't need the line table offset. */
15078 mac_ptr += *offset_size;
757a13d0 15079
cf2c3c16
TT
15080 /* Vendor opcode descriptions. */
15081 if ((flags & 4) != 0)
15082 {
15083 unsigned int i, count;
757a13d0 15084
cf2c3c16
TT
15085 count = read_1_byte (abfd, mac_ptr);
15086 ++mac_ptr;
15087 for (i = 0; i < count; ++i)
15088 {
15089 unsigned int opcode, bytes_read;
15090 unsigned long arg;
15091
15092 opcode = read_1_byte (abfd, mac_ptr);
15093 ++mac_ptr;
15094 opcode_definitions[opcode] = mac_ptr;
15095 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15096 mac_ptr += bytes_read;
15097 mac_ptr += arg;
15098 }
757a13d0 15099 }
cf2c3c16 15100 }
757a13d0 15101
cf2c3c16
TT
15102 return mac_ptr;
15103}
757a13d0 15104
cf2c3c16
TT
15105/* A helper for dwarf_decode_macros that handles the GNU extensions,
15106 including DW_GNU_MACINFO_transparent_include. */
15107
15108static void
15109dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15110 struct macro_source_file *current_file,
15111 struct line_header *lh, char *comp_dir,
15112 struct dwarf2_section_info *section,
15113 int section_is_gnu,
15114 unsigned int offset_size,
15115 struct objfile *objfile)
15116{
15117 enum dwarf_macro_record_type macinfo_type;
15118 int at_commandline;
15119 gdb_byte *opcode_definitions[256];
757a13d0 15120
cf2c3c16
TT
15121 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15122 &offset_size, section_is_gnu);
15123 if (mac_ptr == NULL)
15124 {
15125 /* We already issued a complaint. */
15126 return;
15127 }
757a13d0
JK
15128
15129 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15130 GDB is still reading the definitions from command line. First
15131 DW_MACINFO_start_file will need to be ignored as it was already executed
15132 to create CURRENT_FILE for the main source holding also the command line
15133 definitions. On first met DW_MACINFO_start_file this flag is reset to
15134 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15135
15136 at_commandline = 1;
15137
15138 do
15139 {
15140 /* Do we at least have room for a macinfo type byte? */
15141 if (mac_ptr >= mac_end)
15142 {
cf2c3c16 15143 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15144 break;
15145 }
15146
15147 macinfo_type = read_1_byte (abfd, mac_ptr);
15148 mac_ptr++;
15149
cf2c3c16
TT
15150 /* Note that we rely on the fact that the corresponding GNU and
15151 DWARF constants are the same. */
757a13d0
JK
15152 switch (macinfo_type)
15153 {
15154 /* A zero macinfo type indicates the end of the macro
15155 information. */
15156 case 0:
15157 break;
2e276125 15158
cf2c3c16
TT
15159 case DW_MACRO_GNU_define:
15160 case DW_MACRO_GNU_undef:
15161 case DW_MACRO_GNU_define_indirect:
15162 case DW_MACRO_GNU_undef_indirect:
2e276125 15163 {
891d2f0b 15164 unsigned int bytes_read;
2e276125
JB
15165 int line;
15166 char *body;
cf2c3c16 15167 int is_define;
2e276125 15168
cf2c3c16
TT
15169 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15170 mac_ptr += bytes_read;
15171
15172 if (macinfo_type == DW_MACRO_GNU_define
15173 || macinfo_type == DW_MACRO_GNU_undef)
15174 {
15175 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15176 mac_ptr += bytes_read;
15177 }
15178 else
15179 {
15180 LONGEST str_offset;
15181
15182 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15183 mac_ptr += offset_size;
2e276125 15184
cf2c3c16
TT
15185 body = read_indirect_string_at_offset (abfd, str_offset);
15186 }
15187
15188 is_define = (macinfo_type == DW_MACRO_GNU_define
15189 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15190 if (! current_file)
757a13d0
JK
15191 {
15192 /* DWARF violation as no main source is present. */
15193 complaint (&symfile_complaints,
15194 _("debug info with no main source gives macro %s "
15195 "on line %d: %s"),
cf2c3c16
TT
15196 is_define ? _("definition") : _("undefinition"),
15197 line, body);
757a13d0
JK
15198 break;
15199 }
3e43a32a
MS
15200 if ((line == 0 && !at_commandline)
15201 || (line != 0 && at_commandline))
4d3c2250 15202 complaint (&symfile_complaints,
757a13d0
JK
15203 _("debug info gives %s macro %s with %s line %d: %s"),
15204 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15205 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15206 line == 0 ? _("zero") : _("non-zero"), line, body);
15207
cf2c3c16 15208 if (is_define)
757a13d0 15209 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15210 else
15211 {
15212 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15213 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15214 macro_undef (current_file, line, body);
15215 }
2e276125
JB
15216 }
15217 break;
15218
cf2c3c16 15219 case DW_MACRO_GNU_start_file:
2e276125 15220 {
891d2f0b 15221 unsigned int bytes_read;
2e276125
JB
15222 int line, file;
15223
15224 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15225 mac_ptr += bytes_read;
15226 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15227 mac_ptr += bytes_read;
15228
3e43a32a
MS
15229 if ((line == 0 && !at_commandline)
15230 || (line != 0 && at_commandline))
757a13d0
JK
15231 complaint (&symfile_complaints,
15232 _("debug info gives source %d included "
15233 "from %s at %s line %d"),
15234 file, at_commandline ? _("command-line") : _("file"),
15235 line == 0 ? _("zero") : _("non-zero"), line);
15236
15237 if (at_commandline)
15238 {
cf2c3c16
TT
15239 /* This DW_MACRO_GNU_start_file was executed in the
15240 pass one. */
757a13d0
JK
15241 at_commandline = 0;
15242 }
15243 else
15244 current_file = macro_start_file (file, line,
15245 current_file, comp_dir,
cf2c3c16 15246 lh, objfile);
2e276125
JB
15247 }
15248 break;
15249
cf2c3c16 15250 case DW_MACRO_GNU_end_file:
2e276125 15251 if (! current_file)
4d3c2250 15252 complaint (&symfile_complaints,
3e43a32a
MS
15253 _("macro debug info has an unmatched "
15254 "`close_file' directive"));
2e276125
JB
15255 else
15256 {
15257 current_file = current_file->included_by;
15258 if (! current_file)
15259 {
cf2c3c16 15260 enum dwarf_macro_record_type next_type;
2e276125
JB
15261
15262 /* GCC circa March 2002 doesn't produce the zero
15263 type byte marking the end of the compilation
15264 unit. Complain if it's not there, but exit no
15265 matter what. */
15266
15267 /* Do we at least have room for a macinfo type byte? */
15268 if (mac_ptr >= mac_end)
15269 {
cf2c3c16 15270 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15271 return;
15272 }
15273
15274 /* We don't increment mac_ptr here, so this is just
15275 a look-ahead. */
15276 next_type = read_1_byte (abfd, mac_ptr);
15277 if (next_type != 0)
4d3c2250 15278 complaint (&symfile_complaints,
3e43a32a
MS
15279 _("no terminating 0-type entry for "
15280 "macros in `.debug_macinfo' section"));
2e276125
JB
15281
15282 return;
15283 }
15284 }
15285 break;
15286
cf2c3c16
TT
15287 case DW_MACRO_GNU_transparent_include:
15288 {
15289 LONGEST offset;
15290
15291 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15292 mac_ptr += offset_size;
15293
15294 dwarf_decode_macro_bytes (abfd,
15295 section->buffer + offset,
15296 mac_end, current_file,
15297 lh, comp_dir,
15298 section, section_is_gnu,
15299 offset_size, objfile);
15300 }
15301 break;
15302
2e276125 15303 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15304 if (!section_is_gnu)
15305 {
15306 unsigned int bytes_read;
15307 int constant;
2e276125 15308
cf2c3c16
TT
15309 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15310 mac_ptr += bytes_read;
15311 read_direct_string (abfd, mac_ptr, &bytes_read);
15312 mac_ptr += bytes_read;
2e276125 15313
cf2c3c16
TT
15314 /* We don't recognize any vendor extensions. */
15315 break;
15316 }
15317 /* FALLTHROUGH */
15318
15319 default:
15320 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15321 mac_ptr, abfd, offset_size,
15322 section);
15323 if (mac_ptr == NULL)
15324 return;
15325 break;
2e276125 15326 }
757a13d0 15327 } while (macinfo_type != 0);
2e276125 15328}
8e19ed76 15329
cf2c3c16
TT
15330static void
15331dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15332 char *comp_dir, bfd *abfd,
15333 struct dwarf2_cu *cu,
15334 struct dwarf2_section_info *section,
15335 int section_is_gnu)
15336{
bb5ed363 15337 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15338 gdb_byte *mac_ptr, *mac_end;
15339 struct macro_source_file *current_file = 0;
15340 enum dwarf_macro_record_type macinfo_type;
15341 unsigned int offset_size = cu->header.offset_size;
15342 gdb_byte *opcode_definitions[256];
15343
bb5ed363 15344 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15345 if (section->buffer == NULL)
15346 {
15347 complaint (&symfile_complaints, _("missing %s section"),
15348 section->asection->name);
15349 return;
15350 }
15351
15352 /* First pass: Find the name of the base filename.
15353 This filename is needed in order to process all macros whose definition
15354 (or undefinition) comes from the command line. These macros are defined
15355 before the first DW_MACINFO_start_file entry, and yet still need to be
15356 associated to the base file.
15357
15358 To determine the base file name, we scan the macro definitions until we
15359 reach the first DW_MACINFO_start_file entry. We then initialize
15360 CURRENT_FILE accordingly so that any macro definition found before the
15361 first DW_MACINFO_start_file can still be associated to the base file. */
15362
15363 mac_ptr = section->buffer + offset;
15364 mac_end = section->buffer + section->size;
15365
15366 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15367 &offset_size, section_is_gnu);
15368 if (mac_ptr == NULL)
15369 {
15370 /* We already issued a complaint. */
15371 return;
15372 }
15373
15374 do
15375 {
15376 /* Do we at least have room for a macinfo type byte? */
15377 if (mac_ptr >= mac_end)
15378 {
15379 /* Complaint is printed during the second pass as GDB will probably
15380 stop the first pass earlier upon finding
15381 DW_MACINFO_start_file. */
15382 break;
15383 }
15384
15385 macinfo_type = read_1_byte (abfd, mac_ptr);
15386 mac_ptr++;
15387
15388 /* Note that we rely on the fact that the corresponding GNU and
15389 DWARF constants are the same. */
15390 switch (macinfo_type)
15391 {
15392 /* A zero macinfo type indicates the end of the macro
15393 information. */
15394 case 0:
15395 break;
15396
15397 case DW_MACRO_GNU_define:
15398 case DW_MACRO_GNU_undef:
15399 /* Only skip the data by MAC_PTR. */
15400 {
15401 unsigned int bytes_read;
15402
15403 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15404 mac_ptr += bytes_read;
15405 read_direct_string (abfd, mac_ptr, &bytes_read);
15406 mac_ptr += bytes_read;
15407 }
15408 break;
15409
15410 case DW_MACRO_GNU_start_file:
15411 {
15412 unsigned int bytes_read;
15413 int line, file;
15414
15415 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15416 mac_ptr += bytes_read;
15417 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15418 mac_ptr += bytes_read;
15419
15420 current_file = macro_start_file (file, line, current_file,
bb5ed363 15421 comp_dir, lh, objfile);
cf2c3c16
TT
15422 }
15423 break;
15424
15425 case DW_MACRO_GNU_end_file:
15426 /* No data to skip by MAC_PTR. */
15427 break;
15428
15429 case DW_MACRO_GNU_define_indirect:
15430 case DW_MACRO_GNU_undef_indirect:
15431 {
15432 unsigned int bytes_read;
15433
15434 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15435 mac_ptr += bytes_read;
15436 mac_ptr += offset_size;
15437 }
15438 break;
15439
15440 case DW_MACRO_GNU_transparent_include:
15441 /* Note that, according to the spec, a transparent include
15442 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15443 skip this opcode. */
15444 mac_ptr += offset_size;
15445 break;
15446
15447 case DW_MACINFO_vendor_ext:
15448 /* Only skip the data by MAC_PTR. */
15449 if (!section_is_gnu)
15450 {
15451 unsigned int bytes_read;
15452
15453 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15454 mac_ptr += bytes_read;
15455 read_direct_string (abfd, mac_ptr, &bytes_read);
15456 mac_ptr += bytes_read;
15457 }
15458 /* FALLTHROUGH */
15459
15460 default:
15461 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15462 mac_ptr, abfd, offset_size,
15463 section);
15464 if (mac_ptr == NULL)
15465 return;
15466 break;
15467 }
15468 } while (macinfo_type != 0 && current_file == NULL);
15469
15470 /* Second pass: Process all entries.
15471
15472 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15473 command-line macro definitions/undefinitions. This flag is unset when we
15474 reach the first DW_MACINFO_start_file entry. */
15475
15476 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15477 current_file, lh, comp_dir, section, section_is_gnu,
bb5ed363 15478 offset_size, objfile);
cf2c3c16
TT
15479}
15480
8e19ed76 15481/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15482 if so return true else false. */
8e19ed76
PS
15483static int
15484attr_form_is_block (struct attribute *attr)
15485{
15486 return (attr == NULL ? 0 :
15487 attr->form == DW_FORM_block1
15488 || attr->form == DW_FORM_block2
15489 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15490 || attr->form == DW_FORM_block
15491 || attr->form == DW_FORM_exprloc);
8e19ed76 15492}
4c2df51b 15493
c6a0999f
JB
15494/* Return non-zero if ATTR's value is a section offset --- classes
15495 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15496 You may use DW_UNSND (attr) to retrieve such offsets.
15497
15498 Section 7.5.4, "Attribute Encodings", explains that no attribute
15499 may have a value that belongs to more than one of these classes; it
15500 would be ambiguous if we did, because we use the same forms for all
15501 of them. */
3690dd37
JB
15502static int
15503attr_form_is_section_offset (struct attribute *attr)
15504{
15505 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15506 || attr->form == DW_FORM_data8
15507 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15508}
15509
15510
15511/* Return non-zero if ATTR's value falls in the 'constant' class, or
15512 zero otherwise. When this function returns true, you can apply
15513 dwarf2_get_attr_constant_value to it.
15514
15515 However, note that for some attributes you must check
15516 attr_form_is_section_offset before using this test. DW_FORM_data4
15517 and DW_FORM_data8 are members of both the constant class, and of
15518 the classes that contain offsets into other debug sections
15519 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15520 that, if an attribute's can be either a constant or one of the
15521 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15522 taken as section offsets, not constants. */
15523static int
15524attr_form_is_constant (struct attribute *attr)
15525{
15526 switch (attr->form)
15527 {
15528 case DW_FORM_sdata:
15529 case DW_FORM_udata:
15530 case DW_FORM_data1:
15531 case DW_FORM_data2:
15532 case DW_FORM_data4:
15533 case DW_FORM_data8:
15534 return 1;
15535 default:
15536 return 0;
15537 }
15538}
15539
8cf6f0b1
TT
15540/* A helper function that fills in a dwarf2_loclist_baton. */
15541
15542static void
15543fill_in_loclist_baton (struct dwarf2_cu *cu,
15544 struct dwarf2_loclist_baton *baton,
15545 struct attribute *attr)
15546{
15547 dwarf2_read_section (dwarf2_per_objfile->objfile,
15548 &dwarf2_per_objfile->loc);
15549
15550 baton->per_cu = cu->per_cu;
15551 gdb_assert (baton->per_cu);
15552 /* We don't know how long the location list is, but make sure we
15553 don't run off the edge of the section. */
15554 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15555 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15556 baton->base_address = cu->base_address;
15557}
15558
4c2df51b
DJ
15559static void
15560dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15561 struct dwarf2_cu *cu)
4c2df51b 15562{
bb5ed363
DE
15563 struct objfile *objfile = dwarf2_per_objfile->objfile;
15564
3690dd37 15565 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15566 /* ".debug_loc" may not exist at all, or the offset may be outside
15567 the section. If so, fall through to the complaint in the
15568 other branch. */
bb5ed363 15569 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15570 &dwarf2_per_objfile->loc))
4c2df51b 15571 {
0d53c4c4 15572 struct dwarf2_loclist_baton *baton;
4c2df51b 15573
bb5ed363 15574 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15575 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15576
8cf6f0b1 15577 fill_in_loclist_baton (cu, baton, attr);
be391dca 15578
d00adf39 15579 if (cu->base_known == 0)
0d53c4c4 15580 complaint (&symfile_complaints,
3e43a32a
MS
15581 _("Location list used without "
15582 "specifying the CU base address."));
4c2df51b 15583
768a979c 15584 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15585 SYMBOL_LOCATION_BATON (sym) = baton;
15586 }
15587 else
15588 {
15589 struct dwarf2_locexpr_baton *baton;
15590
bb5ed363 15591 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15592 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15593 baton->per_cu = cu->per_cu;
15594 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15595
15596 if (attr_form_is_block (attr))
15597 {
15598 /* Note that we're just copying the block's data pointer
15599 here, not the actual data. We're still pointing into the
6502dd73
DJ
15600 info_buffer for SYM's objfile; right now we never release
15601 that buffer, but when we do clean up properly this may
15602 need to change. */
0d53c4c4
DJ
15603 baton->size = DW_BLOCK (attr)->size;
15604 baton->data = DW_BLOCK (attr)->data;
15605 }
15606 else
15607 {
15608 dwarf2_invalid_attrib_class_complaint ("location description",
15609 SYMBOL_NATURAL_NAME (sym));
15610 baton->size = 0;
0d53c4c4 15611 }
6e70227d 15612
768a979c 15613 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15614 SYMBOL_LOCATION_BATON (sym) = baton;
15615 }
4c2df51b 15616}
6502dd73 15617
9aa1f1e3
TT
15618/* Return the OBJFILE associated with the compilation unit CU. If CU
15619 came from a separate debuginfo file, then the master objfile is
15620 returned. */
ae0d2f24
UW
15621
15622struct objfile *
15623dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15624{
9291a0cd 15625 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15626
15627 /* Return the master objfile, so that we can report and look up the
15628 correct file containing this variable. */
15629 if (objfile->separate_debug_objfile_backlink)
15630 objfile = objfile->separate_debug_objfile_backlink;
15631
15632 return objfile;
15633}
15634
96408a79
SA
15635/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15636 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15637 CU_HEADERP first. */
15638
15639static const struct comp_unit_head *
15640per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15641 struct dwarf2_per_cu_data *per_cu)
15642{
15643 struct objfile *objfile;
15644 struct dwarf2_per_objfile *per_objfile;
15645 gdb_byte *info_ptr;
15646
15647 if (per_cu->cu)
15648 return &per_cu->cu->header;
15649
15650 objfile = per_cu->objfile;
15651 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15652 info_ptr = per_objfile->info.buffer + per_cu->offset;
15653
15654 memset (cu_headerp, 0, sizeof (*cu_headerp));
15655 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15656
15657 return cu_headerp;
15658}
15659
ae0d2f24
UW
15660/* Return the address size given in the compilation unit header for CU. */
15661
98714339 15662int
ae0d2f24
UW
15663dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15664{
96408a79
SA
15665 struct comp_unit_head cu_header_local;
15666 const struct comp_unit_head *cu_headerp;
c471e790 15667
96408a79
SA
15668 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15669
15670 return cu_headerp->addr_size;
ae0d2f24
UW
15671}
15672
9eae7c52
TT
15673/* Return the offset size given in the compilation unit header for CU. */
15674
15675int
15676dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15677{
96408a79
SA
15678 struct comp_unit_head cu_header_local;
15679 const struct comp_unit_head *cu_headerp;
9c6c53f7 15680
96408a79
SA
15681 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15682
15683 return cu_headerp->offset_size;
15684}
15685
15686/* See its dwarf2loc.h declaration. */
15687
15688int
15689dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15690{
15691 struct comp_unit_head cu_header_local;
15692 const struct comp_unit_head *cu_headerp;
15693
15694 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15695
15696 if (cu_headerp->version == 2)
15697 return cu_headerp->addr_size;
15698 else
15699 return cu_headerp->offset_size;
181cebd4
JK
15700}
15701
9aa1f1e3
TT
15702/* Return the text offset of the CU. The returned offset comes from
15703 this CU's objfile. If this objfile came from a separate debuginfo
15704 file, then the offset may be different from the corresponding
15705 offset in the parent objfile. */
15706
15707CORE_ADDR
15708dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15709{
bb3fa9d0 15710 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15711
15712 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15713}
15714
348e048f
DE
15715/* Locate the .debug_info compilation unit from CU's objfile which contains
15716 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15717
15718static struct dwarf2_per_cu_data *
c764a876 15719dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15720 struct objfile *objfile)
15721{
15722 struct dwarf2_per_cu_data *this_cu;
15723 int low, high;
15724
ae038cb0
DJ
15725 low = 0;
15726 high = dwarf2_per_objfile->n_comp_units - 1;
15727 while (high > low)
15728 {
15729 int mid = low + (high - low) / 2;
9a619af0 15730
ae038cb0
DJ
15731 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15732 high = mid;
15733 else
15734 low = mid + 1;
15735 }
15736 gdb_assert (low == high);
15737 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15738 {
10b3939b 15739 if (low == 0)
8a3fe4f8
AC
15740 error (_("Dwarf Error: could not find partial DIE containing "
15741 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15742 (long) offset, bfd_get_filename (objfile->obfd));
15743
ae038cb0
DJ
15744 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15745 return dwarf2_per_objfile->all_comp_units[low-1];
15746 }
15747 else
15748 {
15749 this_cu = dwarf2_per_objfile->all_comp_units[low];
15750 if (low == dwarf2_per_objfile->n_comp_units - 1
15751 && offset >= this_cu->offset + this_cu->length)
c764a876 15752 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15753 gdb_assert (offset < this_cu->offset + this_cu->length);
15754 return this_cu;
15755 }
15756}
15757
23745b47 15758/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15759
9816fde3 15760static void
23745b47 15761init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15762{
9816fde3 15763 memset (cu, 0, sizeof (*cu));
23745b47
DE
15764 per_cu->cu = cu;
15765 cu->per_cu = per_cu;
15766 cu->objfile = per_cu->objfile;
93311388 15767 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15768}
15769
15770/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15771
15772static void
15773prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15774{
15775 struct attribute *attr;
15776
15777 /* Set the language we're debugging. */
15778 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15779 if (attr)
15780 set_cu_language (DW_UNSND (attr), cu);
15781 else
9cded63f
TT
15782 {
15783 cu->language = language_minimal;
15784 cu->language_defn = language_def (cu->language);
15785 }
93311388
DE
15786}
15787
ae038cb0
DJ
15788/* Release one cached compilation unit, CU. We unlink it from the tree
15789 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15790 the caller is responsible for that.
15791 NOTE: DATA is a void * because this function is also used as a
15792 cleanup routine. */
ae038cb0
DJ
15793
15794static void
68dc6402 15795free_heap_comp_unit (void *data)
ae038cb0
DJ
15796{
15797 struct dwarf2_cu *cu = data;
15798
23745b47
DE
15799 gdb_assert (cu->per_cu != NULL);
15800 cu->per_cu->cu = NULL;
ae038cb0
DJ
15801 cu->per_cu = NULL;
15802
15803 obstack_free (&cu->comp_unit_obstack, NULL);
15804
15805 xfree (cu);
15806}
15807
72bf9492 15808/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15809 when we're finished with it. We can't free the pointer itself, but be
15810 sure to unlink it from the cache. Also release any associated storage
15811 and perform cache maintenance.
72bf9492
DJ
15812
15813 Only used during partial symbol parsing. */
15814
15815static void
15816free_stack_comp_unit (void *data)
15817{
15818 struct dwarf2_cu *cu = data;
15819
23745b47
DE
15820 gdb_assert (cu->per_cu != NULL);
15821 cu->per_cu->cu = NULL;
15822 cu->per_cu = NULL;
15823
72bf9492
DJ
15824 obstack_free (&cu->comp_unit_obstack, NULL);
15825 cu->partial_dies = NULL;
ae038cb0 15826
23745b47
DE
15827 /* The previous code only did this if per_cu != NULL.
15828 But that would always succeed, so now we just unconditionally do
15829 the aging. This seems like the wrong place to do such aging,
15830 but cleaning that up is left for later. */
15831 age_cached_comp_units ();
ae038cb0
DJ
15832}
15833
15834/* Free all cached compilation units. */
15835
15836static void
15837free_cached_comp_units (void *data)
15838{
15839 struct dwarf2_per_cu_data *per_cu, **last_chain;
15840
15841 per_cu = dwarf2_per_objfile->read_in_chain;
15842 last_chain = &dwarf2_per_objfile->read_in_chain;
15843 while (per_cu != NULL)
15844 {
15845 struct dwarf2_per_cu_data *next_cu;
15846
15847 next_cu = per_cu->cu->read_in_chain;
15848
68dc6402 15849 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15850 *last_chain = next_cu;
15851
15852 per_cu = next_cu;
15853 }
15854}
15855
15856/* Increase the age counter on each cached compilation unit, and free
15857 any that are too old. */
15858
15859static void
15860age_cached_comp_units (void)
15861{
15862 struct dwarf2_per_cu_data *per_cu, **last_chain;
15863
15864 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15865 per_cu = dwarf2_per_objfile->read_in_chain;
15866 while (per_cu != NULL)
15867 {
15868 per_cu->cu->last_used ++;
15869 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15870 dwarf2_mark (per_cu->cu);
15871 per_cu = per_cu->cu->read_in_chain;
15872 }
15873
15874 per_cu = dwarf2_per_objfile->read_in_chain;
15875 last_chain = &dwarf2_per_objfile->read_in_chain;
15876 while (per_cu != NULL)
15877 {
15878 struct dwarf2_per_cu_data *next_cu;
15879
15880 next_cu = per_cu->cu->read_in_chain;
15881
15882 if (!per_cu->cu->mark)
15883 {
68dc6402 15884 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15885 *last_chain = next_cu;
15886 }
15887 else
15888 last_chain = &per_cu->cu->read_in_chain;
15889
15890 per_cu = next_cu;
15891 }
15892}
15893
15894/* Remove a single compilation unit from the cache. */
15895
15896static void
15897free_one_cached_comp_unit (void *target_cu)
15898{
15899 struct dwarf2_per_cu_data *per_cu, **last_chain;
15900
15901 per_cu = dwarf2_per_objfile->read_in_chain;
15902 last_chain = &dwarf2_per_objfile->read_in_chain;
15903 while (per_cu != NULL)
15904 {
15905 struct dwarf2_per_cu_data *next_cu;
15906
15907 next_cu = per_cu->cu->read_in_chain;
15908
15909 if (per_cu->cu == target_cu)
15910 {
68dc6402 15911 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15912 *last_chain = next_cu;
15913 break;
15914 }
15915 else
15916 last_chain = &per_cu->cu->read_in_chain;
15917
15918 per_cu = next_cu;
15919 }
15920}
15921
fe3e1990
DJ
15922/* Release all extra memory associated with OBJFILE. */
15923
15924void
15925dwarf2_free_objfile (struct objfile *objfile)
15926{
15927 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15928
15929 if (dwarf2_per_objfile == NULL)
15930 return;
15931
15932 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15933 free_cached_comp_units (NULL);
15934
7b9f3c50
DE
15935 if (dwarf2_per_objfile->quick_file_names_table)
15936 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15937
fe3e1990
DJ
15938 /* Everything else should be on the objfile obstack. */
15939}
15940
1c379e20
DJ
15941/* A pair of DIE offset and GDB type pointer. We store these
15942 in a hash table separate from the DIEs, and preserve them
15943 when the DIEs are flushed out of cache. */
15944
15945struct dwarf2_offset_and_type
15946{
15947 unsigned int offset;
15948 struct type *type;
15949};
15950
15951/* Hash function for a dwarf2_offset_and_type. */
15952
15953static hashval_t
15954offset_and_type_hash (const void *item)
15955{
15956 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15957
1c379e20
DJ
15958 return ofs->offset;
15959}
15960
15961/* Equality function for a dwarf2_offset_and_type. */
15962
15963static int
15964offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15965{
15966 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15967 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15968
1c379e20
DJ
15969 return ofs_lhs->offset == ofs_rhs->offset;
15970}
15971
15972/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15973 table if necessary. For convenience, return TYPE.
15974
15975 The DIEs reading must have careful ordering to:
15976 * Not cause infite loops trying to read in DIEs as a prerequisite for
15977 reading current DIE.
15978 * Not trying to dereference contents of still incompletely read in types
15979 while reading in other DIEs.
15980 * Enable referencing still incompletely read in types just by a pointer to
15981 the type without accessing its fields.
15982
15983 Therefore caller should follow these rules:
15984 * Try to fetch any prerequisite types we may need to build this DIE type
15985 before building the type and calling set_die_type.
e71ec853 15986 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15987 possible before fetching more types to complete the current type.
15988 * Make the type as complete as possible before fetching more types. */
1c379e20 15989
f792889a 15990static struct type *
1c379e20
DJ
15991set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15992{
15993 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15994 struct objfile *objfile = cu->objfile;
15995 htab_t *type_hash_ptr;
1c379e20 15996
b4ba55a1
JB
15997 /* For Ada types, make sure that the gnat-specific data is always
15998 initialized (if not already set). There are a few types where
15999 we should not be doing so, because the type-specific area is
16000 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16001 where the type-specific area is used to store the floatformat).
16002 But this is not a problem, because the gnat-specific information
16003 is actually not needed for these types. */
16004 if (need_gnat_info (cu)
16005 && TYPE_CODE (type) != TYPE_CODE_FUNC
16006 && TYPE_CODE (type) != TYPE_CODE_FLT
16007 && !HAVE_GNAT_AUX_INFO (type))
16008 INIT_GNAT_SPECIFIC (type);
16009
b0df02fd 16010 if (cu->per_cu->debug_types_section)
673bfd45
DE
16011 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16012 else
16013 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16014
16015 if (*type_hash_ptr == NULL)
f792889a 16016 {
673bfd45
DE
16017 *type_hash_ptr
16018 = htab_create_alloc_ex (127,
f792889a
DJ
16019 offset_and_type_hash,
16020 offset_and_type_eq,
16021 NULL,
673bfd45 16022 &objfile->objfile_obstack,
f792889a
DJ
16023 hashtab_obstack_allocate,
16024 dummy_obstack_deallocate);
f792889a 16025 }
1c379e20
DJ
16026
16027 ofs.offset = die->offset;
16028 ofs.type = type;
16029 slot = (struct dwarf2_offset_and_type **)
673bfd45 16030 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16031 if (*slot)
16032 complaint (&symfile_complaints,
16033 _("A problem internal to GDB: DIE 0x%x has type already set"),
16034 die->offset);
673bfd45 16035 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16036 **slot = ofs;
f792889a 16037 return type;
1c379e20
DJ
16038}
16039
673bfd45
DE
16040/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16041 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16042
16043static struct type *
673bfd45
DE
16044get_die_type_at_offset (unsigned int offset,
16045 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16046{
16047 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16048 htab_t type_hash;
f792889a 16049
b0df02fd 16050 if (per_cu->debug_types_section)
673bfd45
DE
16051 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16052 else
16053 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16054 if (type_hash == NULL)
16055 return NULL;
1c379e20 16056
673bfd45 16057 ofs.offset = offset;
1c379e20
DJ
16058 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16059 if (slot)
16060 return slot->type;
16061 else
16062 return NULL;
16063}
16064
673bfd45
DE
16065/* Look up the type for DIE in the appropriate type_hash table,
16066 or return NULL if DIE does not have a saved type. */
16067
16068static struct type *
16069get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16070{
16071 return get_die_type_at_offset (die->offset, cu->per_cu);
16072}
16073
10b3939b
DJ
16074/* Add a dependence relationship from CU to REF_PER_CU. */
16075
16076static void
16077dwarf2_add_dependence (struct dwarf2_cu *cu,
16078 struct dwarf2_per_cu_data *ref_per_cu)
16079{
16080 void **slot;
16081
16082 if (cu->dependencies == NULL)
16083 cu->dependencies
16084 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16085 NULL, &cu->comp_unit_obstack,
16086 hashtab_obstack_allocate,
16087 dummy_obstack_deallocate);
16088
16089 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16090 if (*slot == NULL)
16091 *slot = ref_per_cu;
16092}
1c379e20 16093
f504f079
DE
16094/* Subroutine of dwarf2_mark to pass to htab_traverse.
16095 Set the mark field in every compilation unit in the
ae038cb0
DJ
16096 cache that we must keep because we are keeping CU. */
16097
10b3939b
DJ
16098static int
16099dwarf2_mark_helper (void **slot, void *data)
16100{
16101 struct dwarf2_per_cu_data *per_cu;
16102
16103 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16104
16105 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16106 reading of the chain. As such dependencies remain valid it is not much
16107 useful to track and undo them during QUIT cleanups. */
16108 if (per_cu->cu == NULL)
16109 return 1;
16110
10b3939b
DJ
16111 if (per_cu->cu->mark)
16112 return 1;
16113 per_cu->cu->mark = 1;
16114
16115 if (per_cu->cu->dependencies != NULL)
16116 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16117
16118 return 1;
16119}
16120
f504f079
DE
16121/* Set the mark field in CU and in every other compilation unit in the
16122 cache that we must keep because we are keeping CU. */
16123
ae038cb0
DJ
16124static void
16125dwarf2_mark (struct dwarf2_cu *cu)
16126{
16127 if (cu->mark)
16128 return;
16129 cu->mark = 1;
10b3939b
DJ
16130 if (cu->dependencies != NULL)
16131 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16132}
16133
16134static void
16135dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16136{
16137 while (per_cu)
16138 {
16139 per_cu->cu->mark = 0;
16140 per_cu = per_cu->cu->read_in_chain;
16141 }
72bf9492
DJ
16142}
16143
72bf9492
DJ
16144/* Trivial hash function for partial_die_info: the hash value of a DIE
16145 is its offset in .debug_info for this objfile. */
16146
16147static hashval_t
16148partial_die_hash (const void *item)
16149{
16150 const struct partial_die_info *part_die = item;
9a619af0 16151
72bf9492
DJ
16152 return part_die->offset;
16153}
16154
16155/* Trivial comparison function for partial_die_info structures: two DIEs
16156 are equal if they have the same offset. */
16157
16158static int
16159partial_die_eq (const void *item_lhs, const void *item_rhs)
16160{
16161 const struct partial_die_info *part_die_lhs = item_lhs;
16162 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16163
72bf9492
DJ
16164 return part_die_lhs->offset == part_die_rhs->offset;
16165}
16166
ae038cb0
DJ
16167static struct cmd_list_element *set_dwarf2_cmdlist;
16168static struct cmd_list_element *show_dwarf2_cmdlist;
16169
16170static void
16171set_dwarf2_cmd (char *args, int from_tty)
16172{
16173 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16174}
16175
16176static void
16177show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16178{
ae038cb0
DJ
16179 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16180}
16181
dce234bc
PP
16182/* If section described by INFO was mmapped, munmap it now. */
16183
16184static void
16185munmap_section_buffer (struct dwarf2_section_info *info)
16186{
b315ab21 16187 if (info->map_addr != NULL)
dce234bc
PP
16188 {
16189#ifdef HAVE_MMAP
b315ab21 16190 int res;
9a619af0 16191
b315ab21
TG
16192 res = munmap (info->map_addr, info->map_len);
16193 gdb_assert (res == 0);
dce234bc
PP
16194#else
16195 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16196 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16197#endif
16198 }
16199}
16200
16201/* munmap debug sections for OBJFILE, if necessary. */
16202
16203static void
c1bd65d0 16204dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16205{
16206 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16207 int ix;
16208 struct dwarf2_section_info *section;
9a619af0 16209
16be1145
DE
16210 /* This is sorted according to the order they're defined in to make it easier
16211 to keep in sync. */
dce234bc
PP
16212 munmap_section_buffer (&data->info);
16213 munmap_section_buffer (&data->abbrev);
16214 munmap_section_buffer (&data->line);
16be1145 16215 munmap_section_buffer (&data->loc);
dce234bc 16216 munmap_section_buffer (&data->macinfo);
cf2c3c16 16217 munmap_section_buffer (&data->macro);
16be1145 16218 munmap_section_buffer (&data->str);
dce234bc 16219 munmap_section_buffer (&data->ranges);
dce234bc
PP
16220 munmap_section_buffer (&data->frame);
16221 munmap_section_buffer (&data->eh_frame);
9291a0cd 16222 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16223
16224 for (ix = 0;
16225 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16226 ++ix)
16227 munmap_section_buffer (section);
16228
16229 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16230}
16231
16232\f
ae2de4f8 16233/* The "save gdb-index" command. */
9291a0cd
TT
16234
16235/* The contents of the hash table we create when building the string
16236 table. */
16237struct strtab_entry
16238{
16239 offset_type offset;
16240 const char *str;
16241};
16242
559a7a62
JK
16243/* Hash function for a strtab_entry.
16244
16245 Function is used only during write_hash_table so no index format backward
16246 compatibility is needed. */
b89be57b 16247
9291a0cd
TT
16248static hashval_t
16249hash_strtab_entry (const void *e)
16250{
16251 const struct strtab_entry *entry = e;
559a7a62 16252 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16253}
16254
16255/* Equality function for a strtab_entry. */
b89be57b 16256
9291a0cd
TT
16257static int
16258eq_strtab_entry (const void *a, const void *b)
16259{
16260 const struct strtab_entry *ea = a;
16261 const struct strtab_entry *eb = b;
16262 return !strcmp (ea->str, eb->str);
16263}
16264
16265/* Create a strtab_entry hash table. */
b89be57b 16266
9291a0cd
TT
16267static htab_t
16268create_strtab (void)
16269{
16270 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16271 xfree, xcalloc, xfree);
16272}
16273
16274/* Add a string to the constant pool. Return the string's offset in
16275 host order. */
b89be57b 16276
9291a0cd
TT
16277static offset_type
16278add_string (htab_t table, struct obstack *cpool, const char *str)
16279{
16280 void **slot;
16281 struct strtab_entry entry;
16282 struct strtab_entry *result;
16283
16284 entry.str = str;
16285 slot = htab_find_slot (table, &entry, INSERT);
16286 if (*slot)
16287 result = *slot;
16288 else
16289 {
16290 result = XNEW (struct strtab_entry);
16291 result->offset = obstack_object_size (cpool);
16292 result->str = str;
16293 obstack_grow_str0 (cpool, str);
16294 *slot = result;
16295 }
16296 return result->offset;
16297}
16298
16299/* An entry in the symbol table. */
16300struct symtab_index_entry
16301{
16302 /* The name of the symbol. */
16303 const char *name;
16304 /* The offset of the name in the constant pool. */
16305 offset_type index_offset;
16306 /* A sorted vector of the indices of all the CUs that hold an object
16307 of this name. */
16308 VEC (offset_type) *cu_indices;
16309};
16310
16311/* The symbol table. This is a power-of-2-sized hash table. */
16312struct mapped_symtab
16313{
16314 offset_type n_elements;
16315 offset_type size;
16316 struct symtab_index_entry **data;
16317};
16318
16319/* Hash function for a symtab_index_entry. */
b89be57b 16320
9291a0cd
TT
16321static hashval_t
16322hash_symtab_entry (const void *e)
16323{
16324 const struct symtab_index_entry *entry = e;
16325 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16326 sizeof (offset_type) * VEC_length (offset_type,
16327 entry->cu_indices),
16328 0);
16329}
16330
16331/* Equality function for a symtab_index_entry. */
b89be57b 16332
9291a0cd
TT
16333static int
16334eq_symtab_entry (const void *a, const void *b)
16335{
16336 const struct symtab_index_entry *ea = a;
16337 const struct symtab_index_entry *eb = b;
16338 int len = VEC_length (offset_type, ea->cu_indices);
16339 if (len != VEC_length (offset_type, eb->cu_indices))
16340 return 0;
16341 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16342 VEC_address (offset_type, eb->cu_indices),
16343 sizeof (offset_type) * len);
16344}
16345
16346/* Destroy a symtab_index_entry. */
b89be57b 16347
9291a0cd
TT
16348static void
16349delete_symtab_entry (void *p)
16350{
16351 struct symtab_index_entry *entry = p;
16352 VEC_free (offset_type, entry->cu_indices);
16353 xfree (entry);
16354}
16355
16356/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16357
9291a0cd 16358static htab_t
3876f04e 16359create_symbol_hash_table (void)
9291a0cd
TT
16360{
16361 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16362 delete_symtab_entry, xcalloc, xfree);
16363}
16364
16365/* Create a new mapped symtab object. */
b89be57b 16366
9291a0cd
TT
16367static struct mapped_symtab *
16368create_mapped_symtab (void)
16369{
16370 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16371 symtab->n_elements = 0;
16372 symtab->size = 1024;
16373 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16374 return symtab;
16375}
16376
16377/* Destroy a mapped_symtab. */
b89be57b 16378
9291a0cd
TT
16379static void
16380cleanup_mapped_symtab (void *p)
16381{
16382 struct mapped_symtab *symtab = p;
16383 /* The contents of the array are freed when the other hash table is
16384 destroyed. */
16385 xfree (symtab->data);
16386 xfree (symtab);
16387}
16388
16389/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16390 the slot.
16391
16392 Function is used only during write_hash_table so no index format backward
16393 compatibility is needed. */
b89be57b 16394
9291a0cd
TT
16395static struct symtab_index_entry **
16396find_slot (struct mapped_symtab *symtab, const char *name)
16397{
559a7a62 16398 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16399
16400 index = hash & (symtab->size - 1);
16401 step = ((hash * 17) & (symtab->size - 1)) | 1;
16402
16403 for (;;)
16404 {
16405 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16406 return &symtab->data[index];
16407 index = (index + step) & (symtab->size - 1);
16408 }
16409}
16410
16411/* Expand SYMTAB's hash table. */
b89be57b 16412
9291a0cd
TT
16413static void
16414hash_expand (struct mapped_symtab *symtab)
16415{
16416 offset_type old_size = symtab->size;
16417 offset_type i;
16418 struct symtab_index_entry **old_entries = symtab->data;
16419
16420 symtab->size *= 2;
16421 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16422
16423 for (i = 0; i < old_size; ++i)
16424 {
16425 if (old_entries[i])
16426 {
16427 struct symtab_index_entry **slot = find_slot (symtab,
16428 old_entries[i]->name);
16429 *slot = old_entries[i];
16430 }
16431 }
16432
16433 xfree (old_entries);
16434}
16435
16436/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16437 is the index of the CU in which the symbol appears. */
b89be57b 16438
9291a0cd
TT
16439static void
16440add_index_entry (struct mapped_symtab *symtab, const char *name,
16441 offset_type cu_index)
16442{
16443 struct symtab_index_entry **slot;
16444
16445 ++symtab->n_elements;
16446 if (4 * symtab->n_elements / 3 >= symtab->size)
16447 hash_expand (symtab);
16448
16449 slot = find_slot (symtab, name);
16450 if (!*slot)
16451 {
16452 *slot = XNEW (struct symtab_index_entry);
16453 (*slot)->name = name;
16454 (*slot)->cu_indices = NULL;
16455 }
16456 /* Don't push an index twice. Due to how we add entries we only
16457 have to check the last one. */
16458 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16459 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16460 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16461}
16462
16463/* Add a vector of indices to the constant pool. */
b89be57b 16464
9291a0cd 16465static offset_type
3876f04e 16466add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16467 struct symtab_index_entry *entry)
16468{
16469 void **slot;
16470
3876f04e 16471 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16472 if (!*slot)
16473 {
16474 offset_type len = VEC_length (offset_type, entry->cu_indices);
16475 offset_type val = MAYBE_SWAP (len);
16476 offset_type iter;
16477 int i;
16478
16479 *slot = entry;
16480 entry->index_offset = obstack_object_size (cpool);
16481
16482 obstack_grow (cpool, &val, sizeof (val));
16483 for (i = 0;
16484 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16485 ++i)
16486 {
16487 val = MAYBE_SWAP (iter);
16488 obstack_grow (cpool, &val, sizeof (val));
16489 }
16490 }
16491 else
16492 {
16493 struct symtab_index_entry *old_entry = *slot;
16494 entry->index_offset = old_entry->index_offset;
16495 entry = old_entry;
16496 }
16497 return entry->index_offset;
16498}
16499
16500/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16501 constant pool entries going into the obstack CPOOL. */
b89be57b 16502
9291a0cd
TT
16503static void
16504write_hash_table (struct mapped_symtab *symtab,
16505 struct obstack *output, struct obstack *cpool)
16506{
16507 offset_type i;
3876f04e 16508 htab_t symbol_hash_table;
9291a0cd
TT
16509 htab_t str_table;
16510
3876f04e 16511 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16512 str_table = create_strtab ();
3876f04e 16513
9291a0cd
TT
16514 /* We add all the index vectors to the constant pool first, to
16515 ensure alignment is ok. */
16516 for (i = 0; i < symtab->size; ++i)
16517 {
16518 if (symtab->data[i])
3876f04e 16519 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16520 }
16521
16522 /* Now write out the hash table. */
16523 for (i = 0; i < symtab->size; ++i)
16524 {
16525 offset_type str_off, vec_off;
16526
16527 if (symtab->data[i])
16528 {
16529 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16530 vec_off = symtab->data[i]->index_offset;
16531 }
16532 else
16533 {
16534 /* While 0 is a valid constant pool index, it is not valid
16535 to have 0 for both offsets. */
16536 str_off = 0;
16537 vec_off = 0;
16538 }
16539
16540 str_off = MAYBE_SWAP (str_off);
16541 vec_off = MAYBE_SWAP (vec_off);
16542
16543 obstack_grow (output, &str_off, sizeof (str_off));
16544 obstack_grow (output, &vec_off, sizeof (vec_off));
16545 }
16546
16547 htab_delete (str_table);
3876f04e 16548 htab_delete (symbol_hash_table);
9291a0cd
TT
16549}
16550
0a5429f6
DE
16551/* Struct to map psymtab to CU index in the index file. */
16552struct psymtab_cu_index_map
16553{
16554 struct partial_symtab *psymtab;
16555 unsigned int cu_index;
16556};
16557
16558static hashval_t
16559hash_psymtab_cu_index (const void *item)
16560{
16561 const struct psymtab_cu_index_map *map = item;
16562
16563 return htab_hash_pointer (map->psymtab);
16564}
16565
16566static int
16567eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16568{
16569 const struct psymtab_cu_index_map *lhs = item_lhs;
16570 const struct psymtab_cu_index_map *rhs = item_rhs;
16571
16572 return lhs->psymtab == rhs->psymtab;
16573}
16574
16575/* Helper struct for building the address table. */
16576struct addrmap_index_data
16577{
16578 struct objfile *objfile;
16579 struct obstack *addr_obstack;
16580 htab_t cu_index_htab;
16581
16582 /* Non-zero if the previous_* fields are valid.
16583 We can't write an entry until we see the next entry (since it is only then
16584 that we know the end of the entry). */
16585 int previous_valid;
16586 /* Index of the CU in the table of all CUs in the index file. */
16587 unsigned int previous_cu_index;
0963b4bd 16588 /* Start address of the CU. */
0a5429f6
DE
16589 CORE_ADDR previous_cu_start;
16590};
16591
16592/* Write an address entry to OBSTACK. */
b89be57b 16593
9291a0cd 16594static void
0a5429f6
DE
16595add_address_entry (struct objfile *objfile, struct obstack *obstack,
16596 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16597{
0a5429f6 16598 offset_type cu_index_to_write;
9291a0cd
TT
16599 char addr[8];
16600 CORE_ADDR baseaddr;
16601
16602 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16603
0a5429f6
DE
16604 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16605 obstack_grow (obstack, addr, 8);
16606 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16607 obstack_grow (obstack, addr, 8);
16608 cu_index_to_write = MAYBE_SWAP (cu_index);
16609 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16610}
16611
16612/* Worker function for traversing an addrmap to build the address table. */
16613
16614static int
16615add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16616{
16617 struct addrmap_index_data *data = datap;
16618 struct partial_symtab *pst = obj;
16619 offset_type cu_index;
16620 void **slot;
16621
16622 if (data->previous_valid)
16623 add_address_entry (data->objfile, data->addr_obstack,
16624 data->previous_cu_start, start_addr,
16625 data->previous_cu_index);
16626
16627 data->previous_cu_start = start_addr;
16628 if (pst != NULL)
16629 {
16630 struct psymtab_cu_index_map find_map, *map;
16631 find_map.psymtab = pst;
16632 map = htab_find (data->cu_index_htab, &find_map);
16633 gdb_assert (map != NULL);
16634 data->previous_cu_index = map->cu_index;
16635 data->previous_valid = 1;
16636 }
16637 else
16638 data->previous_valid = 0;
16639
16640 return 0;
16641}
16642
16643/* Write OBJFILE's address map to OBSTACK.
16644 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16645 in the index file. */
16646
16647static void
16648write_address_map (struct objfile *objfile, struct obstack *obstack,
16649 htab_t cu_index_htab)
16650{
16651 struct addrmap_index_data addrmap_index_data;
16652
16653 /* When writing the address table, we have to cope with the fact that
16654 the addrmap iterator only provides the start of a region; we have to
16655 wait until the next invocation to get the start of the next region. */
16656
16657 addrmap_index_data.objfile = objfile;
16658 addrmap_index_data.addr_obstack = obstack;
16659 addrmap_index_data.cu_index_htab = cu_index_htab;
16660 addrmap_index_data.previous_valid = 0;
16661
16662 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16663 &addrmap_index_data);
16664
16665 /* It's highly unlikely the last entry (end address = 0xff...ff)
16666 is valid, but we should still handle it.
16667 The end address is recorded as the start of the next region, but that
16668 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16669 anyway. */
16670 if (addrmap_index_data.previous_valid)
16671 add_address_entry (objfile, obstack,
16672 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16673 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16674}
16675
16676/* Add a list of partial symbols to SYMTAB. */
b89be57b 16677
9291a0cd
TT
16678static void
16679write_psymbols (struct mapped_symtab *symtab,
987d643c 16680 htab_t psyms_seen,
9291a0cd
TT
16681 struct partial_symbol **psymp,
16682 int count,
987d643c
TT
16683 offset_type cu_index,
16684 int is_static)
9291a0cd
TT
16685{
16686 for (; count-- > 0; ++psymp)
16687 {
987d643c
TT
16688 void **slot, *lookup;
16689
9291a0cd
TT
16690 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16691 error (_("Ada is not currently supported by the index"));
987d643c
TT
16692
16693 /* We only want to add a given psymbol once. However, we also
16694 want to account for whether it is global or static. So, we
16695 may add it twice, using slightly different values. */
16696 if (is_static)
16697 {
16698 uintptr_t val = 1 | (uintptr_t) *psymp;
16699
16700 lookup = (void *) val;
16701 }
16702 else
16703 lookup = *psymp;
16704
16705 /* Only add a given psymbol once. */
16706 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16707 if (!*slot)
16708 {
16709 *slot = lookup;
bb2f58dc 16710 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16711 }
9291a0cd
TT
16712 }
16713}
16714
16715/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16716 exception if there is an error. */
b89be57b 16717
9291a0cd
TT
16718static void
16719write_obstack (FILE *file, struct obstack *obstack)
16720{
16721 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16722 file)
16723 != obstack_object_size (obstack))
16724 error (_("couldn't data write to file"));
16725}
16726
16727/* Unlink a file if the argument is not NULL. */
b89be57b 16728
9291a0cd
TT
16729static void
16730unlink_if_set (void *p)
16731{
16732 char **filename = p;
16733 if (*filename)
16734 unlink (*filename);
16735}
16736
1fd400ff
TT
16737/* A helper struct used when iterating over debug_types. */
16738struct signatured_type_index_data
16739{
16740 struct objfile *objfile;
16741 struct mapped_symtab *symtab;
16742 struct obstack *types_list;
987d643c 16743 htab_t psyms_seen;
1fd400ff
TT
16744 int cu_index;
16745};
16746
16747/* A helper function that writes a single signatured_type to an
16748 obstack. */
b89be57b 16749
1fd400ff
TT
16750static int
16751write_one_signatured_type (void **slot, void *d)
16752{
16753 struct signatured_type_index_data *info = d;
16754 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16755 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16756 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16757 gdb_byte val[8];
16758
16759 write_psymbols (info->symtab,
987d643c 16760 info->psyms_seen,
3e43a32a
MS
16761 info->objfile->global_psymbols.list
16762 + psymtab->globals_offset,
987d643c
TT
16763 psymtab->n_global_syms, info->cu_index,
16764 0);
1fd400ff 16765 write_psymbols (info->symtab,
987d643c 16766 info->psyms_seen,
3e43a32a
MS
16767 info->objfile->static_psymbols.list
16768 + psymtab->statics_offset,
987d643c
TT
16769 psymtab->n_static_syms, info->cu_index,
16770 1);
1fd400ff 16771
b3c8eb43 16772 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16773 obstack_grow (info->types_list, val, 8);
16774 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16775 obstack_grow (info->types_list, val, 8);
16776 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16777 obstack_grow (info->types_list, val, 8);
16778
16779 ++info->cu_index;
16780
16781 return 1;
16782}
16783
9291a0cd 16784/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16785
9291a0cd
TT
16786static void
16787write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16788{
16789 struct cleanup *cleanup;
16790 char *filename, *cleanup_filename;
1fd400ff
TT
16791 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16792 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16793 int i;
16794 FILE *out_file;
16795 struct mapped_symtab *symtab;
16796 offset_type val, size_of_contents, total_len;
16797 struct stat st;
16798 char buf[8];
987d643c 16799 htab_t psyms_seen;
0a5429f6
DE
16800 htab_t cu_index_htab;
16801 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16802
b4f2f049 16803 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16804 return;
b4f2f049 16805
9291a0cd
TT
16806 if (dwarf2_per_objfile->using_index)
16807 error (_("Cannot use an index to create the index"));
16808
8b70b953
TT
16809 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16810 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16811
9291a0cd 16812 if (stat (objfile->name, &st) < 0)
7e17e088 16813 perror_with_name (objfile->name);
9291a0cd
TT
16814
16815 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16816 INDEX_SUFFIX, (char *) NULL);
16817 cleanup = make_cleanup (xfree, filename);
16818
16819 out_file = fopen (filename, "wb");
16820 if (!out_file)
16821 error (_("Can't open `%s' for writing"), filename);
16822
16823 cleanup_filename = filename;
16824 make_cleanup (unlink_if_set, &cleanup_filename);
16825
16826 symtab = create_mapped_symtab ();
16827 make_cleanup (cleanup_mapped_symtab, symtab);
16828
16829 obstack_init (&addr_obstack);
16830 make_cleanup_obstack_free (&addr_obstack);
16831
16832 obstack_init (&cu_list);
16833 make_cleanup_obstack_free (&cu_list);
16834
1fd400ff
TT
16835 obstack_init (&types_cu_list);
16836 make_cleanup_obstack_free (&types_cu_list);
16837
987d643c
TT
16838 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16839 NULL, xcalloc, xfree);
96408a79 16840 make_cleanup_htab_delete (psyms_seen);
987d643c 16841
0a5429f6
DE
16842 /* While we're scanning CU's create a table that maps a psymtab pointer
16843 (which is what addrmap records) to its index (which is what is recorded
16844 in the index file). This will later be needed to write the address
16845 table. */
16846 cu_index_htab = htab_create_alloc (100,
16847 hash_psymtab_cu_index,
16848 eq_psymtab_cu_index,
16849 NULL, xcalloc, xfree);
96408a79 16850 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16851 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16852 xmalloc (sizeof (struct psymtab_cu_index_map)
16853 * dwarf2_per_objfile->n_comp_units);
16854 make_cleanup (xfree, psymtab_cu_index_map);
16855
16856 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16857 work here. Also, the debug_types entries do not appear in
16858 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16859 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16860 {
3e43a32a
MS
16861 struct dwarf2_per_cu_data *per_cu
16862 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16863 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16864 gdb_byte val[8];
0a5429f6
DE
16865 struct psymtab_cu_index_map *map;
16866 void **slot;
9291a0cd
TT
16867
16868 write_psymbols (symtab,
987d643c 16869 psyms_seen,
9291a0cd 16870 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16871 psymtab->n_global_syms, i,
16872 0);
9291a0cd 16873 write_psymbols (symtab,
987d643c 16874 psyms_seen,
9291a0cd 16875 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16876 psymtab->n_static_syms, i,
16877 1);
9291a0cd 16878
0a5429f6
DE
16879 map = &psymtab_cu_index_map[i];
16880 map->psymtab = psymtab;
16881 map->cu_index = i;
16882 slot = htab_find_slot (cu_index_htab, map, INSERT);
16883 gdb_assert (slot != NULL);
16884 gdb_assert (*slot == NULL);
16885 *slot = map;
9291a0cd 16886
e254ef6a 16887 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16888 obstack_grow (&cu_list, val, 8);
e254ef6a 16889 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16890 obstack_grow (&cu_list, val, 8);
16891 }
16892
0a5429f6
DE
16893 /* Dump the address map. */
16894 write_address_map (objfile, &addr_obstack, cu_index_htab);
16895
1fd400ff
TT
16896 /* Write out the .debug_type entries, if any. */
16897 if (dwarf2_per_objfile->signatured_types)
16898 {
16899 struct signatured_type_index_data sig_data;
16900
16901 sig_data.objfile = objfile;
16902 sig_data.symtab = symtab;
16903 sig_data.types_list = &types_cu_list;
987d643c 16904 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16905 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16906 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16907 write_one_signatured_type, &sig_data);
16908 }
16909
9291a0cd
TT
16910 obstack_init (&constant_pool);
16911 make_cleanup_obstack_free (&constant_pool);
16912 obstack_init (&symtab_obstack);
16913 make_cleanup_obstack_free (&symtab_obstack);
16914 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16915
16916 obstack_init (&contents);
16917 make_cleanup_obstack_free (&contents);
1fd400ff 16918 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16919 total_len = size_of_contents;
16920
16921 /* The version number. */
559a7a62 16922 val = MAYBE_SWAP (5);
9291a0cd
TT
16923 obstack_grow (&contents, &val, sizeof (val));
16924
16925 /* The offset of the CU list from the start of the file. */
16926 val = MAYBE_SWAP (total_len);
16927 obstack_grow (&contents, &val, sizeof (val));
16928 total_len += obstack_object_size (&cu_list);
16929
1fd400ff
TT
16930 /* The offset of the types CU list from the start of the file. */
16931 val = MAYBE_SWAP (total_len);
16932 obstack_grow (&contents, &val, sizeof (val));
16933 total_len += obstack_object_size (&types_cu_list);
16934
9291a0cd
TT
16935 /* The offset of the address table from the start of the file. */
16936 val = MAYBE_SWAP (total_len);
16937 obstack_grow (&contents, &val, sizeof (val));
16938 total_len += obstack_object_size (&addr_obstack);
16939
16940 /* The offset of the symbol table from the start of the file. */
16941 val = MAYBE_SWAP (total_len);
16942 obstack_grow (&contents, &val, sizeof (val));
16943 total_len += obstack_object_size (&symtab_obstack);
16944
16945 /* The offset of the constant pool from the start of the file. */
16946 val = MAYBE_SWAP (total_len);
16947 obstack_grow (&contents, &val, sizeof (val));
16948 total_len += obstack_object_size (&constant_pool);
16949
16950 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16951
16952 write_obstack (out_file, &contents);
16953 write_obstack (out_file, &cu_list);
1fd400ff 16954 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16955 write_obstack (out_file, &addr_obstack);
16956 write_obstack (out_file, &symtab_obstack);
16957 write_obstack (out_file, &constant_pool);
16958
16959 fclose (out_file);
16960
16961 /* We want to keep the file, so we set cleanup_filename to NULL
16962 here. See unlink_if_set. */
16963 cleanup_filename = NULL;
16964
16965 do_cleanups (cleanup);
16966}
16967
90476074
TT
16968/* Implementation of the `save gdb-index' command.
16969
16970 Note that the file format used by this command is documented in the
16971 GDB manual. Any changes here must be documented there. */
11570e71 16972
9291a0cd
TT
16973static void
16974save_gdb_index_command (char *arg, int from_tty)
16975{
16976 struct objfile *objfile;
16977
16978 if (!arg || !*arg)
96d19272 16979 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16980
16981 ALL_OBJFILES (objfile)
16982 {
16983 struct stat st;
16984
16985 /* If the objfile does not correspond to an actual file, skip it. */
16986 if (stat (objfile->name, &st) < 0)
16987 continue;
16988
16989 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16990 if (dwarf2_per_objfile)
16991 {
16992 volatile struct gdb_exception except;
16993
16994 TRY_CATCH (except, RETURN_MASK_ERROR)
16995 {
16996 write_psymtabs_to_index (objfile, arg);
16997 }
16998 if (except.reason < 0)
16999 exception_fprintf (gdb_stderr, except,
17000 _("Error while writing index for `%s': "),
17001 objfile->name);
17002 }
17003 }
dce234bc
PP
17004}
17005
9291a0cd
TT
17006\f
17007
9eae7c52
TT
17008int dwarf2_always_disassemble;
17009
17010static void
17011show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17012 struct cmd_list_element *c, const char *value)
17013{
3e43a32a
MS
17014 fprintf_filtered (file,
17015 _("Whether to always disassemble "
17016 "DWARF expressions is %s.\n"),
9eae7c52
TT
17017 value);
17018}
17019
900e11f9
JK
17020static void
17021show_check_physname (struct ui_file *file, int from_tty,
17022 struct cmd_list_element *c, const char *value)
17023{
17024 fprintf_filtered (file,
17025 _("Whether to check \"physname\" is %s.\n"),
17026 value);
17027}
17028
6502dd73
DJ
17029void _initialize_dwarf2_read (void);
17030
17031void
17032_initialize_dwarf2_read (void)
17033{
96d19272
JK
17034 struct cmd_list_element *c;
17035
dce234bc 17036 dwarf2_objfile_data_key
c1bd65d0 17037 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17038
1bedd215
AC
17039 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17040Set DWARF 2 specific variables.\n\
17041Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17042 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17043 0/*allow-unknown*/, &maintenance_set_cmdlist);
17044
1bedd215
AC
17045 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17046Show DWARF 2 specific variables\n\
17047Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17048 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17049 0/*allow-unknown*/, &maintenance_show_cmdlist);
17050
17051 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17052 &dwarf2_max_cache_age, _("\
17053Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17054Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17055A higher limit means that cached compilation units will be stored\n\
17056in memory longer, and more total memory will be used. Zero disables\n\
17057caching, which can slow down startup."),
2c5b56ce 17058 NULL,
920d2a44 17059 show_dwarf2_max_cache_age,
2c5b56ce 17060 &set_dwarf2_cmdlist,
ae038cb0 17061 &show_dwarf2_cmdlist);
d97bc12b 17062
9eae7c52
TT
17063 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17064 &dwarf2_always_disassemble, _("\
17065Set whether `info address' always disassembles DWARF expressions."), _("\
17066Show whether `info address' always disassembles DWARF expressions."), _("\
17067When enabled, DWARF expressions are always printed in an assembly-like\n\
17068syntax. When disabled, expressions will be printed in a more\n\
17069conversational style, when possible."),
17070 NULL,
17071 show_dwarf2_always_disassemble,
17072 &set_dwarf2_cmdlist,
17073 &show_dwarf2_cmdlist);
17074
d97bc12b
DE
17075 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17076Set debugging of the dwarf2 DIE reader."), _("\
17077Show debugging of the dwarf2 DIE reader."), _("\
17078When enabled (non-zero), DIEs are dumped after they are read in.\n\
17079The value is the maximum depth to print."),
17080 NULL,
17081 NULL,
17082 &setdebuglist, &showdebuglist);
9291a0cd 17083
900e11f9
JK
17084 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17085Set cross-checking of \"physname\" code against demangler."), _("\
17086Show cross-checking of \"physname\" code against demangler."), _("\
17087When enabled, GDB's internal \"physname\" code is checked against\n\
17088the demangler."),
17089 NULL, show_check_physname,
17090 &setdebuglist, &showdebuglist);
17091
96d19272 17092 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17093 _("\
fc1a9d6e 17094Save a gdb-index file.\n\
11570e71 17095Usage: save gdb-index DIRECTORY"),
96d19272
JK
17096 &save_cmdlist);
17097 set_cmd_completer (c, filename_completer);
6502dd73 17098}
This page took 2.39255 seconds and 4 git commands to generate.