* dwarf2read.c (dwarf2_compute_name): Simplify objfile references.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
50f182aa 37#include "gdb-demangle.h"
c906108c 38#include "expression.h"
d5166ae1 39#include "filenames.h" /* for DOSish file names */
2e276125 40#include "macrotab.h"
c906108c
SS
41#include "language.h"
42#include "complaints.h"
357e46e7 43#include "bcache.h"
4c2df51b
DJ
44#include "dwarf2expr.h"
45#include "dwarf2loc.h"
9219021c 46#include "cp-support.h"
72bf9492 47#include "hashtab.h"
ae038cb0
DJ
48#include "command.h"
49#include "gdbcmd.h"
edb3359d 50#include "block.h"
ff013f42 51#include "addrmap.h"
94af9270
KS
52#include "typeprint.h"
53#include "jv-lang.h"
ccefe4c4 54#include "psympriv.h"
9291a0cd
TT
55#include "exceptions.h"
56#include "gdb_stat.h"
96d19272 57#include "completer.h"
34eaf542 58#include "vec.h"
98bfdba5
PA
59#include "c-lang.h"
60#include "valprint.h"
60d5a603 61#include <ctype.h>
4c2df51b 62
c906108c
SS
63#include <fcntl.h>
64#include "gdb_string.h"
4bdf3d34 65#include "gdb_assert.h"
c906108c 66#include <sys/types.h>
233a11ab
CS
67#ifdef HAVE_ZLIB_H
68#include <zlib.h>
69#endif
dce234bc
PP
70#ifdef HAVE_MMAP
71#include <sys/mman.h>
85d9bd0e
TT
72#ifndef MAP_FAILED
73#define MAP_FAILED ((void *) -1)
74#endif
dce234bc 75#endif
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
107d2387 80#if 0
357e46e7 81/* .debug_info header for a compilation unit
c906108c
SS
82 Because of alignment constraints, this structure has padding and cannot
83 be mapped directly onto the beginning of the .debug_info section. */
84typedef struct comp_unit_header
85 {
86 unsigned int length; /* length of the .debug_info
87 contribution */
88 unsigned short version; /* version number -- 2 for DWARF
89 version 2 */
90 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
91 unsigned char addr_size; /* byte size of an address -- 4 */
92 }
93_COMP_UNIT_HEADER;
94#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 95#endif
c906108c 96
c906108c
SS
97/* .debug_line statement program prologue
98 Because of alignment constraints, this structure has padding and cannot
99 be mapped directly onto the beginning of the .debug_info section. */
100typedef struct statement_prologue
101 {
102 unsigned int total_length; /* byte length of the statement
103 information */
104 unsigned short version; /* version number -- 2 for DWARF
105 version 2 */
106 unsigned int prologue_length; /* # bytes between prologue &
107 stmt program */
108 unsigned char minimum_instruction_length; /* byte size of
109 smallest instr */
110 unsigned char default_is_stmt; /* initial value of is_stmt
111 register */
112 char line_base;
113 unsigned char line_range;
114 unsigned char opcode_base; /* number assigned to first special
115 opcode */
116 unsigned char *standard_opcode_lengths;
117 }
118_STATEMENT_PROLOGUE;
119
d97bc12b
DE
120/* When non-zero, dump DIEs after they are read in. */
121static int dwarf2_die_debug = 0;
122
900e11f9
JK
123/* When non-zero, cross-check physname against demangler. */
124static int check_physname = 0;
125
dce234bc
PP
126static int pagesize;
127
df8a16a1
DJ
128/* When set, the file that we're processing is known to have debugging
129 info for C++ namespaces. GCC 3.3.x did not produce this information,
130 but later versions do. */
131
132static int processing_has_namespace_info;
133
6502dd73
DJ
134static const struct objfile_data *dwarf2_objfile_data_key;
135
dce234bc
PP
136struct dwarf2_section_info
137{
138 asection *asection;
139 gdb_byte *buffer;
140 bfd_size_type size;
b315ab21
TG
141 /* Not NULL if the section was actually mmapped. */
142 void *map_addr;
143 /* Page aligned size of mmapped area. */
144 bfd_size_type map_len;
be391dca
TT
145 /* True if we have tried to read this section. */
146 int readin;
dce234bc
PP
147};
148
8b70b953
TT
149typedef struct dwarf2_section_info dwarf2_section_info_def;
150DEF_VEC_O (dwarf2_section_info_def);
151
9291a0cd
TT
152/* All offsets in the index are of this type. It must be
153 architecture-independent. */
154typedef uint32_t offset_type;
155
156DEF_VEC_I (offset_type);
157
158/* A description of the mapped index. The file format is described in
159 a comment by the code that writes the index. */
160struct mapped_index
161{
559a7a62
JK
162 /* Index data format version. */
163 int version;
164
9291a0cd
TT
165 /* The total length of the buffer. */
166 off_t total_size;
b11b1f88 167
9291a0cd
TT
168 /* A pointer to the address table data. */
169 const gdb_byte *address_table;
b11b1f88 170
9291a0cd
TT
171 /* Size of the address table data in bytes. */
172 offset_type address_table_size;
b11b1f88 173
3876f04e
DE
174 /* The symbol table, implemented as a hash table. */
175 const offset_type *symbol_table;
b11b1f88 176
9291a0cd 177 /* Size in slots, each slot is 2 offset_types. */
3876f04e 178 offset_type symbol_table_slots;
b11b1f88 179
9291a0cd
TT
180 /* A pointer to the constant pool. */
181 const char *constant_pool;
182};
183
9cdd5dbd
DE
184/* Collection of data recorded per objfile.
185 This hangs off of dwarf2_objfile_data_key. */
186
6502dd73
DJ
187struct dwarf2_per_objfile
188{
dce234bc
PP
189 struct dwarf2_section_info info;
190 struct dwarf2_section_info abbrev;
191 struct dwarf2_section_info line;
dce234bc
PP
192 struct dwarf2_section_info loc;
193 struct dwarf2_section_info macinfo;
cf2c3c16 194 struct dwarf2_section_info macro;
dce234bc
PP
195 struct dwarf2_section_info str;
196 struct dwarf2_section_info ranges;
197 struct dwarf2_section_info frame;
198 struct dwarf2_section_info eh_frame;
9291a0cd 199 struct dwarf2_section_info gdb_index;
ae038cb0 200
8b70b953
TT
201 VEC (dwarf2_section_info_def) *types;
202
be391dca
TT
203 /* Back link. */
204 struct objfile *objfile;
205
10b3939b
DJ
206 /* A list of all the compilation units. This is used to locate
207 the target compilation unit of a particular reference. */
ae038cb0
DJ
208 struct dwarf2_per_cu_data **all_comp_units;
209
210 /* The number of compilation units in ALL_COMP_UNITS. */
211 int n_comp_units;
212
1fd400ff
TT
213 /* The number of .debug_types-related CUs. */
214 int n_type_comp_units;
215
216 /* The .debug_types-related CUs. */
217 struct dwarf2_per_cu_data **type_comp_units;
218
ae038cb0
DJ
219 /* A chain of compilation units that are currently read in, so that
220 they can be freed later. */
221 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 222
348e048f
DE
223 /* A table mapping .debug_types signatures to its signatured_type entry.
224 This is NULL if the .debug_types section hasn't been read in yet. */
225 htab_t signatured_types;
226
72dca2f5
FR
227 /* A flag indicating wether this objfile has a section loaded at a
228 VMA of 0. */
229 int has_section_at_zero;
9291a0cd 230
ae2de4f8
DE
231 /* True if we are using the mapped index,
232 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
233 unsigned char using_index;
234
ae2de4f8 235 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 236 struct mapped_index *index_table;
98bfdba5 237
7b9f3c50
DE
238 /* When using index_table, this keeps track of all quick_file_names entries.
239 TUs can share line table entries with CUs or other TUs, and there can be
240 a lot more TUs than unique line tables, so we maintain a separate table
241 of all line table entries to support the sharing. */
242 htab_t quick_file_names_table;
243
98bfdba5
PA
244 /* Set during partial symbol reading, to prevent queueing of full
245 symbols. */
246 int reading_partial_symbols;
673bfd45
DE
247
248 /* Table mapping type .debug_info DIE offsets to types.
249 This is NULL if not allocated yet.
250 It (currently) makes sense to allocate debug_types_type_hash lazily.
251 To keep things simple we allocate both lazily. */
252 htab_t debug_info_type_hash;
253
254 /* Table mapping type .debug_types DIE offsets to types.
255 This is NULL if not allocated yet. */
256 htab_t debug_types_type_hash;
6502dd73
DJ
257};
258
259static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 260
251d32d9 261/* Default names of the debugging sections. */
c906108c 262
233a11ab
CS
263/* Note that if the debugging section has been compressed, it might
264 have a name like .zdebug_info. */
265
9cdd5dbd
DE
266static const struct dwarf2_debug_sections dwarf2_elf_names =
267{
251d32d9
TG
268 { ".debug_info", ".zdebug_info" },
269 { ".debug_abbrev", ".zdebug_abbrev" },
270 { ".debug_line", ".zdebug_line" },
271 { ".debug_loc", ".zdebug_loc" },
272 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 273 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
274 { ".debug_str", ".zdebug_str" },
275 { ".debug_ranges", ".zdebug_ranges" },
276 { ".debug_types", ".zdebug_types" },
277 { ".debug_frame", ".zdebug_frame" },
278 { ".eh_frame", NULL },
24d3216f
TT
279 { ".gdb_index", ".zgdb_index" },
280 23
251d32d9 281};
c906108c
SS
282
283/* local data types */
284
0963b4bd 285/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
286#ifndef ABBREV_HASH_SIZE
287#define ABBREV_HASH_SIZE 121
288#endif
289
107d2387
AC
290/* The data in a compilation unit header, after target2host
291 translation, looks like this. */
c906108c 292struct comp_unit_head
a738430d 293{
c764a876 294 unsigned int length;
a738430d 295 short version;
a738430d
MK
296 unsigned char addr_size;
297 unsigned char signed_addr_p;
9cbfa09e 298 unsigned int abbrev_offset;
57349743 299
a738430d
MK
300 /* Size of file offsets; either 4 or 8. */
301 unsigned int offset_size;
57349743 302
a738430d
MK
303 /* Size of the length field; either 4 or 12. */
304 unsigned int initial_length_size;
57349743 305
a738430d
MK
306 /* Offset to the first byte of this compilation unit header in the
307 .debug_info section, for resolving relative reference dies. */
308 unsigned int offset;
57349743 309
d00adf39
DE
310 /* Offset to first die in this cu from the start of the cu.
311 This will be the first byte following the compilation unit header. */
312 unsigned int first_die_offset;
a738430d 313};
c906108c 314
3da10d80
KS
315/* Type used for delaying computation of method physnames.
316 See comments for compute_delayed_physnames. */
317struct delayed_method_info
318{
319 /* The type to which the method is attached, i.e., its parent class. */
320 struct type *type;
321
322 /* The index of the method in the type's function fieldlists. */
323 int fnfield_index;
324
325 /* The index of the method in the fieldlist. */
326 int index;
327
328 /* The name of the DIE. */
329 const char *name;
330
331 /* The DIE associated with this method. */
332 struct die_info *die;
333};
334
335typedef struct delayed_method_info delayed_method_info;
336DEF_VEC_O (delayed_method_info);
337
e7c27a73
DJ
338/* Internal state when decoding a particular compilation unit. */
339struct dwarf2_cu
340{
341 /* The objfile containing this compilation unit. */
342 struct objfile *objfile;
343
d00adf39 344 /* The header of the compilation unit. */
e7c27a73 345 struct comp_unit_head header;
e142c38c 346
d00adf39
DE
347 /* Base address of this compilation unit. */
348 CORE_ADDR base_address;
349
350 /* Non-zero if base_address has been set. */
351 int base_known;
352
e142c38c
DJ
353 /* The language we are debugging. */
354 enum language language;
355 const struct language_defn *language_defn;
356
b0f35d58
DL
357 const char *producer;
358
e142c38c
DJ
359 /* The generic symbol table building routines have separate lists for
360 file scope symbols and all all other scopes (local scopes). So
361 we need to select the right one to pass to add_symbol_to_list().
362 We do it by keeping a pointer to the correct list in list_in_scope.
363
364 FIXME: The original dwarf code just treated the file scope as the
365 first local scope, and all other local scopes as nested local
366 scopes, and worked fine. Check to see if we really need to
367 distinguish these in buildsym.c. */
368 struct pending **list_in_scope;
369
f3dd6933
DJ
370 /* DWARF abbreviation table associated with this compilation unit. */
371 struct abbrev_info **dwarf2_abbrevs;
372
373 /* Storage for the abbrev table. */
374 struct obstack abbrev_obstack;
72bf9492
DJ
375
376 /* Hash table holding all the loaded partial DIEs. */
377 htab_t partial_dies;
378
379 /* Storage for things with the same lifetime as this read-in compilation
380 unit, including partial DIEs. */
381 struct obstack comp_unit_obstack;
382
ae038cb0
DJ
383 /* When multiple dwarf2_cu structures are living in memory, this field
384 chains them all together, so that they can be released efficiently.
385 We will probably also want a generation counter so that most-recently-used
386 compilation units are cached... */
387 struct dwarf2_per_cu_data *read_in_chain;
388
389 /* Backchain to our per_cu entry if the tree has been built. */
390 struct dwarf2_per_cu_data *per_cu;
391
392 /* How many compilation units ago was this CU last referenced? */
393 int last_used;
394
10b3939b 395 /* A hash table of die offsets for following references. */
51545339 396 htab_t die_hash;
10b3939b
DJ
397
398 /* Full DIEs if read in. */
399 struct die_info *dies;
400
401 /* A set of pointers to dwarf2_per_cu_data objects for compilation
402 units referenced by this one. Only set during full symbol processing;
403 partial symbol tables do not have dependencies. */
404 htab_t dependencies;
405
cb1df416
DJ
406 /* Header data from the line table, during full symbol processing. */
407 struct line_header *line_header;
408
3da10d80
KS
409 /* A list of methods which need to have physnames computed
410 after all type information has been read. */
411 VEC (delayed_method_info) *method_list;
412
96408a79
SA
413 /* To be copied to symtab->call_site_htab. */
414 htab_t call_site_htab;
415
ae038cb0
DJ
416 /* Mark used when releasing cached dies. */
417 unsigned int mark : 1;
418
419 /* This flag will be set if this compilation unit might include
420 inter-compilation-unit references. */
421 unsigned int has_form_ref_addr : 1;
422
72bf9492
DJ
423 /* This flag will be set if this compilation unit includes any
424 DW_TAG_namespace DIEs. If we know that there are explicit
425 DIEs for namespaces, we don't need to try to infer them
426 from mangled names. */
427 unsigned int has_namespace_info : 1;
8be455d7
JK
428
429 /* This CU references .debug_loc. See the symtab->locations_valid field.
430 This test is imperfect as there may exist optimized debug code not using
431 any location list and still facing inlining issues if handled as
432 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 433 unsigned int has_loclist : 1;
e7c27a73
DJ
434};
435
10b3939b
DJ
436/* Persistent data held for a compilation unit, even when not
437 processing it. We put a pointer to this structure in the
438 read_symtab_private field of the psymtab. If we encounter
439 inter-compilation-unit references, we also maintain a sorted
440 list of all compilation units. */
441
ae038cb0
DJ
442struct dwarf2_per_cu_data
443{
348e048f 444 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 445 bytes should suffice to store the length of any compilation unit
45452591
DE
446 - if it doesn't, GDB will fall over anyway.
447 NOTE: Unlike comp_unit_head.length, this length includes
448 initial_length_size. */
c764a876 449 unsigned int offset;
348e048f 450 unsigned int length : 29;
ae038cb0
DJ
451
452 /* Flag indicating this compilation unit will be read in before
453 any of the current compilation units are processed. */
c764a876 454 unsigned int queued : 1;
ae038cb0 455
5afb4e99
DJ
456 /* This flag will be set if we need to load absolutely all DIEs
457 for this compilation unit, instead of just the ones we think
458 are interesting. It gets set if we look for a DIE in the
459 hash table and don't find it. */
460 unsigned int load_all_dies : 1;
461
8b70b953
TT
462 /* Non-null if this CU is from .debug_types; in which case it points
463 to the section. Otherwise it's from .debug_info. */
b0df02fd 464 struct dwarf2_section_info *debug_types_section;
348e048f 465
17ea53c3
JK
466 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
467 of the CU cache it gets reset to NULL again. */
ae038cb0 468 struct dwarf2_cu *cu;
1c379e20 469
9cdd5dbd
DE
470 /* The corresponding objfile.
471 Normally we can get the objfile from dwarf2_per_objfile.
472 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
473 struct objfile *objfile;
474
475 /* When using partial symbol tables, the 'psymtab' field is active.
476 Otherwise the 'quick' field is active. */
477 union
478 {
479 /* The partial symbol table associated with this compilation unit,
480 or NULL for partial units (which do not have an associated
481 symtab). */
482 struct partial_symtab *psymtab;
483
484 /* Data needed by the "quick" functions. */
485 struct dwarf2_per_cu_quick_data *quick;
486 } v;
ae038cb0
DJ
487};
488
348e048f
DE
489/* Entry in the signatured_types hash table. */
490
491struct signatured_type
492{
493 ULONGEST signature;
494
348e048f
DE
495 /* Offset in .debug_types of the type defined by this TU. */
496 unsigned int type_offset;
497
498 /* The CU(/TU) of this type. */
499 struct dwarf2_per_cu_data per_cu;
500};
501
0963b4bd
MS
502/* Struct used to pass misc. parameters to read_die_and_children, et
503 al. which are used for both .debug_info and .debug_types dies.
504 All parameters here are unchanging for the life of the call. This
505 struct exists to abstract away the constant parameters of die
506 reading. */
93311388
DE
507
508struct die_reader_specs
509{
510 /* The bfd of this objfile. */
511 bfd* abfd;
512
513 /* The CU of the DIE we are parsing. */
514 struct dwarf2_cu *cu;
515
516 /* Pointer to start of section buffer.
517 This is either the start of .debug_info or .debug_types. */
518 const gdb_byte *buffer;
519};
520
debd256d
JB
521/* The line number information for a compilation unit (found in the
522 .debug_line section) begins with a "statement program header",
523 which contains the following information. */
524struct line_header
525{
526 unsigned int total_length;
527 unsigned short version;
528 unsigned int header_length;
529 unsigned char minimum_instruction_length;
2dc7f7b3 530 unsigned char maximum_ops_per_instruction;
debd256d
JB
531 unsigned char default_is_stmt;
532 int line_base;
533 unsigned char line_range;
534 unsigned char opcode_base;
535
536 /* standard_opcode_lengths[i] is the number of operands for the
537 standard opcode whose value is i. This means that
538 standard_opcode_lengths[0] is unused, and the last meaningful
539 element is standard_opcode_lengths[opcode_base - 1]. */
540 unsigned char *standard_opcode_lengths;
541
542 /* The include_directories table. NOTE! These strings are not
543 allocated with xmalloc; instead, they are pointers into
544 debug_line_buffer. If you try to free them, `free' will get
545 indigestion. */
546 unsigned int num_include_dirs, include_dirs_size;
547 char **include_dirs;
548
549 /* The file_names table. NOTE! These strings are not allocated
550 with xmalloc; instead, they are pointers into debug_line_buffer.
551 Don't try to free them directly. */
552 unsigned int num_file_names, file_names_size;
553 struct file_entry
c906108c 554 {
debd256d
JB
555 char *name;
556 unsigned int dir_index;
557 unsigned int mod_time;
558 unsigned int length;
aaa75496 559 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 560 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
561 } *file_names;
562
563 /* The start and end of the statement program following this
6502dd73 564 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 565 gdb_byte *statement_program_start, *statement_program_end;
debd256d 566};
c906108c
SS
567
568/* When we construct a partial symbol table entry we only
0963b4bd 569 need this much information. */
c906108c
SS
570struct partial_die_info
571 {
72bf9492 572 /* Offset of this DIE. */
c906108c 573 unsigned int offset;
72bf9492
DJ
574
575 /* DWARF-2 tag for this DIE. */
576 ENUM_BITFIELD(dwarf_tag) tag : 16;
577
72bf9492
DJ
578 /* Assorted flags describing the data found in this DIE. */
579 unsigned int has_children : 1;
580 unsigned int is_external : 1;
581 unsigned int is_declaration : 1;
582 unsigned int has_type : 1;
583 unsigned int has_specification : 1;
584 unsigned int has_pc_info : 1;
585
586 /* Flag set if the SCOPE field of this structure has been
587 computed. */
588 unsigned int scope_set : 1;
589
fa4028e9
JB
590 /* Flag set if the DIE has a byte_size attribute. */
591 unsigned int has_byte_size : 1;
592
98bfdba5
PA
593 /* Flag set if any of the DIE's children are template arguments. */
594 unsigned int has_template_arguments : 1;
595
abc72ce4
DE
596 /* Flag set if fixup_partial_die has been called on this die. */
597 unsigned int fixup_called : 1;
598
72bf9492 599 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 600 sometimes a default name for unnamed DIEs. */
c906108c 601 char *name;
72bf9492 602
abc72ce4
DE
603 /* The linkage name, if present. */
604 const char *linkage_name;
605
72bf9492
DJ
606 /* The scope to prepend to our children. This is generally
607 allocated on the comp_unit_obstack, so will disappear
608 when this compilation unit leaves the cache. */
609 char *scope;
610
611 /* The location description associated with this DIE, if any. */
612 struct dwarf_block *locdesc;
613
614 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
615 CORE_ADDR lowpc;
616 CORE_ADDR highpc;
72bf9492 617
93311388 618 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 619 DW_AT_sibling, if any. */
abc72ce4
DE
620 /* NOTE: This member isn't strictly necessary, read_partial_die could
621 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 622 gdb_byte *sibling;
72bf9492
DJ
623
624 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
625 DW_AT_specification (or DW_AT_abstract_origin or
626 DW_AT_extension). */
627 unsigned int spec_offset;
628
629 /* Pointers to this DIE's parent, first child, and next sibling,
630 if any. */
631 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
632 };
633
0963b4bd 634/* This data structure holds the information of an abbrev. */
c906108c
SS
635struct abbrev_info
636 {
637 unsigned int number; /* number identifying abbrev */
638 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
639 unsigned short has_children; /* boolean */
640 unsigned short num_attrs; /* number of attributes */
c906108c
SS
641 struct attr_abbrev *attrs; /* an array of attribute descriptions */
642 struct abbrev_info *next; /* next in chain */
643 };
644
645struct attr_abbrev
646 {
9d25dd43
DE
647 ENUM_BITFIELD(dwarf_attribute) name : 16;
648 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
649 };
650
0963b4bd 651/* Attributes have a name and a value. */
b60c80d6
DJ
652struct attribute
653 {
9d25dd43 654 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
655 ENUM_BITFIELD(dwarf_form) form : 15;
656
657 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
658 field should be in u.str (existing only for DW_STRING) but it is kept
659 here for better struct attribute alignment. */
660 unsigned int string_is_canonical : 1;
661
b60c80d6
DJ
662 union
663 {
664 char *str;
665 struct dwarf_block *blk;
43bbcdc2
PH
666 ULONGEST unsnd;
667 LONGEST snd;
b60c80d6 668 CORE_ADDR addr;
348e048f 669 struct signatured_type *signatured_type;
b60c80d6
DJ
670 }
671 u;
672 };
673
0963b4bd 674/* This data structure holds a complete die structure. */
c906108c
SS
675struct die_info
676 {
76815b17
DE
677 /* DWARF-2 tag for this DIE. */
678 ENUM_BITFIELD(dwarf_tag) tag : 16;
679
680 /* Number of attributes */
98bfdba5
PA
681 unsigned char num_attrs;
682
683 /* True if we're presently building the full type name for the
684 type derived from this DIE. */
685 unsigned char building_fullname : 1;
76815b17
DE
686
687 /* Abbrev number */
688 unsigned int abbrev;
689
93311388 690 /* Offset in .debug_info or .debug_types section. */
76815b17 691 unsigned int offset;
78ba4af6
JB
692
693 /* The dies in a compilation unit form an n-ary tree. PARENT
694 points to this die's parent; CHILD points to the first child of
695 this node; and all the children of a given node are chained
4950bc1c 696 together via their SIBLING fields. */
639d11d3
DC
697 struct die_info *child; /* Its first child, if any. */
698 struct die_info *sibling; /* Its next sibling, if any. */
699 struct die_info *parent; /* Its parent, if any. */
c906108c 700
b60c80d6
DJ
701 /* An array of attributes, with NUM_ATTRS elements. There may be
702 zero, but it's not common and zero-sized arrays are not
703 sufficiently portable C. */
704 struct attribute attrs[1];
c906108c
SS
705 };
706
0963b4bd 707/* Get at parts of an attribute structure. */
c906108c
SS
708
709#define DW_STRING(attr) ((attr)->u.str)
8285870a 710#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
711#define DW_UNSND(attr) ((attr)->u.unsnd)
712#define DW_BLOCK(attr) ((attr)->u.blk)
713#define DW_SND(attr) ((attr)->u.snd)
714#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 715#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 716
0963b4bd 717/* Blocks are a bunch of untyped bytes. */
c906108c
SS
718struct dwarf_block
719 {
720 unsigned int size;
1d6edc3c
JK
721
722 /* Valid only if SIZE is not zero. */
fe1b8b76 723 gdb_byte *data;
c906108c
SS
724 };
725
c906108c
SS
726#ifndef ATTR_ALLOC_CHUNK
727#define ATTR_ALLOC_CHUNK 4
728#endif
729
c906108c
SS
730/* Allocate fields for structs, unions and enums in this size. */
731#ifndef DW_FIELD_ALLOC_CHUNK
732#define DW_FIELD_ALLOC_CHUNK 4
733#endif
734
c906108c
SS
735/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
736 but this would require a corresponding change in unpack_field_as_long
737 and friends. */
738static int bits_per_byte = 8;
739
740/* The routines that read and process dies for a C struct or C++ class
741 pass lists of data member fields and lists of member function fields
742 in an instance of a field_info structure, as defined below. */
743struct field_info
c5aa993b 744 {
0963b4bd 745 /* List of data member and baseclasses fields. */
c5aa993b
JM
746 struct nextfield
747 {
748 struct nextfield *next;
749 int accessibility;
750 int virtuality;
751 struct field field;
752 }
7d0ccb61 753 *fields, *baseclasses;
c906108c 754
7d0ccb61 755 /* Number of fields (including baseclasses). */
c5aa993b 756 int nfields;
c906108c 757
c5aa993b
JM
758 /* Number of baseclasses. */
759 int nbaseclasses;
c906108c 760
c5aa993b
JM
761 /* Set if the accesibility of one of the fields is not public. */
762 int non_public_fields;
c906108c 763
c5aa993b
JM
764 /* Member function fields array, entries are allocated in the order they
765 are encountered in the object file. */
766 struct nextfnfield
767 {
768 struct nextfnfield *next;
769 struct fn_field fnfield;
770 }
771 *fnfields;
c906108c 772
c5aa993b
JM
773 /* Member function fieldlist array, contains name of possibly overloaded
774 member function, number of overloaded member functions and a pointer
775 to the head of the member function field chain. */
776 struct fnfieldlist
777 {
778 char *name;
779 int length;
780 struct nextfnfield *head;
781 }
782 *fnfieldlists;
c906108c 783
c5aa993b
JM
784 /* Number of entries in the fnfieldlists array. */
785 int nfnfields;
98751a41
JK
786
787 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
788 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
789 struct typedef_field_list
790 {
791 struct typedef_field field;
792 struct typedef_field_list *next;
793 }
794 *typedef_field_list;
795 unsigned typedef_field_list_count;
c5aa993b 796 };
c906108c 797
10b3939b
DJ
798/* One item on the queue of compilation units to read in full symbols
799 for. */
800struct dwarf2_queue_item
801{
802 struct dwarf2_per_cu_data *per_cu;
803 struct dwarf2_queue_item *next;
804};
805
806/* The current queue. */
807static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
808
ae038cb0
DJ
809/* Loaded secondary compilation units are kept in memory until they
810 have not been referenced for the processing of this many
811 compilation units. Set this to zero to disable caching. Cache
812 sizes of up to at least twenty will improve startup time for
813 typical inter-CU-reference binaries, at an obvious memory cost. */
814static int dwarf2_max_cache_age = 5;
920d2a44
AC
815static void
816show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
817 struct cmd_list_element *c, const char *value)
818{
3e43a32a
MS
819 fprintf_filtered (file, _("The upper bound on the age of cached "
820 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
821 value);
822}
823
ae038cb0 824
0963b4bd 825/* Various complaints about symbol reading that don't abort the process. */
c906108c 826
4d3c2250
KB
827static void
828dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
e2e0b3e5 831 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
832}
833
25e43795
DJ
834static void
835dwarf2_debug_line_missing_file_complaint (void)
836{
837 complaint (&symfile_complaints,
838 _(".debug_line section has line data without a file"));
839}
840
59205f5a
JB
841static void
842dwarf2_debug_line_missing_end_sequence_complaint (void)
843{
844 complaint (&symfile_complaints,
3e43a32a
MS
845 _(".debug_line section has line "
846 "program sequence without an end"));
59205f5a
JB
847}
848
4d3c2250
KB
849static void
850dwarf2_complex_location_expr_complaint (void)
2e276125 851{
e2e0b3e5 852 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
853}
854
4d3c2250
KB
855static void
856dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
857 int arg3)
2e276125 858{
4d3c2250 859 complaint (&symfile_complaints,
3e43a32a
MS
860 _("const value length mismatch for '%s', got %d, expected %d"),
861 arg1, arg2, arg3);
4d3c2250
KB
862}
863
864static void
cf2c3c16 865dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 866{
4d3c2250 867 complaint (&symfile_complaints,
cf2c3c16
TT
868 _("macro info runs off end of `%s' section"),
869 section->asection->name);
4d3c2250
KB
870}
871
872static void
873dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 874{
4d3c2250 875 complaint (&symfile_complaints,
3e43a32a
MS
876 _("macro debug info contains a "
877 "malformed macro definition:\n`%s'"),
4d3c2250
KB
878 arg1);
879}
880
881static void
882dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 883{
4d3c2250 884 complaint (&symfile_complaints,
3e43a32a
MS
885 _("invalid attribute class or form for '%s' in '%s'"),
886 arg1, arg2);
4d3c2250 887}
c906108c 888
c906108c
SS
889/* local function prototypes */
890
4efb68b1 891static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 892
aaa75496
JB
893static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
894 struct objfile *);
895
918dd910
JK
896static void dwarf2_find_base_address (struct die_info *die,
897 struct dwarf2_cu *cu);
898
c67a9c90 899static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 900
72bf9492
DJ
901static void scan_partial_symbols (struct partial_die_info *,
902 CORE_ADDR *, CORE_ADDR *,
5734ee8b 903 int, struct dwarf2_cu *);
c906108c 904
72bf9492
DJ
905static void add_partial_symbol (struct partial_die_info *,
906 struct dwarf2_cu *);
63d06c5c 907
72bf9492
DJ
908static void add_partial_namespace (struct partial_die_info *pdi,
909 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 910 int need_pc, struct dwarf2_cu *cu);
63d06c5c 911
5d7cb8df
JK
912static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
913 CORE_ADDR *highpc, int need_pc,
914 struct dwarf2_cu *cu);
915
72bf9492
DJ
916static void add_partial_enumeration (struct partial_die_info *enum_pdi,
917 struct dwarf2_cu *cu);
91c24f0a 918
bc30ff58
JB
919static void add_partial_subprogram (struct partial_die_info *pdi,
920 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 921 int need_pc, struct dwarf2_cu *cu);
bc30ff58 922
fe1b8b76 923static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
924 gdb_byte *buffer, gdb_byte *info_ptr,
925 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 926
a14ed312 927static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 928
a14ed312 929static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 930
e7c27a73 931static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 932
f3dd6933 933static void dwarf2_free_abbrev_table (void *);
c906108c 934
6caca83c
CC
935static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
936
fe1b8b76 937static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 938 struct dwarf2_cu *);
72bf9492 939
57349743 940static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 941 struct dwarf2_cu *);
c906108c 942
93311388
DE
943static struct partial_die_info *load_partial_dies (bfd *,
944 gdb_byte *, gdb_byte *,
945 int, struct dwarf2_cu *);
72bf9492 946
fe1b8b76 947static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
948 struct abbrev_info *abbrev,
949 unsigned int, bfd *,
950 gdb_byte *, gdb_byte *,
951 struct dwarf2_cu *);
c906108c 952
c764a876 953static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 954 struct dwarf2_cu *);
72bf9492
DJ
955
956static void fixup_partial_die (struct partial_die_info *,
957 struct dwarf2_cu *);
958
fe1b8b76
JB
959static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
960 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 961
fe1b8b76
JB
962static gdb_byte *read_attribute_value (struct attribute *, unsigned,
963 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 964
fe1b8b76 965static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 966
fe1b8b76 967static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 968
fe1b8b76 969static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 970
fe1b8b76 971static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 972
93311388 973static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 974
fe1b8b76 975static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 976 unsigned int *);
c906108c 977
c764a876
DE
978static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
979
980static LONGEST read_checked_initial_length_and_offset
981 (bfd *, gdb_byte *, const struct comp_unit_head *,
982 unsigned int *, unsigned int *);
613e1657 983
fe1b8b76 984static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
985 unsigned int *);
986
987static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 988
fe1b8b76 989static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 990
9b1c24c8 991static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 992
fe1b8b76
JB
993static char *read_indirect_string (bfd *, gdb_byte *,
994 const struct comp_unit_head *,
995 unsigned int *);
4bdf3d34 996
fe1b8b76 997static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 998
fe1b8b76 999static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1000
fe1b8b76 1001static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1002
e142c38c 1003static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1004
e142c38c
DJ
1005static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1006 struct dwarf2_cu *);
c906108c 1007
348e048f
DE
1008static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1009 unsigned int,
1010 struct dwarf2_cu *);
1011
05cf31d1
JB
1012static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1013 struct dwarf2_cu *cu);
1014
e142c38c 1015static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1016
e142c38c 1017static struct die_info *die_specification (struct die_info *die,
f2f0e013 1018 struct dwarf2_cu **);
63d06c5c 1019
debd256d
JB
1020static void free_line_header (struct line_header *lh);
1021
aaa75496
JB
1022static void add_file_name (struct line_header *, char *, unsigned int,
1023 unsigned int, unsigned int);
1024
debd256d
JB
1025static struct line_header *(dwarf_decode_line_header
1026 (unsigned int offset,
e7c27a73 1027 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1028
72b9f47f 1029static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1030 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1031
72b9f47f 1032static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1033
a14ed312 1034static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1035 struct dwarf2_cu *);
c906108c 1036
34eaf542
TT
1037static struct symbol *new_symbol_full (struct die_info *, struct type *,
1038 struct dwarf2_cu *, struct symbol *);
1039
a14ed312 1040static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1041 struct dwarf2_cu *);
c906108c 1042
98bfdba5
PA
1043static void dwarf2_const_value_attr (struct attribute *attr,
1044 struct type *type,
1045 const char *name,
1046 struct obstack *obstack,
1047 struct dwarf2_cu *cu, long *value,
1048 gdb_byte **bytes,
1049 struct dwarf2_locexpr_baton **baton);
2df3850c 1050
e7c27a73 1051static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1052
b4ba55a1
JB
1053static int need_gnat_info (struct dwarf2_cu *);
1054
3e43a32a
MS
1055static struct type *die_descriptive_type (struct die_info *,
1056 struct dwarf2_cu *);
b4ba55a1
JB
1057
1058static void set_descriptive_type (struct type *, struct die_info *,
1059 struct dwarf2_cu *);
1060
e7c27a73
DJ
1061static struct type *die_containing_type (struct die_info *,
1062 struct dwarf2_cu *);
c906108c 1063
673bfd45
DE
1064static struct type *lookup_die_type (struct die_info *, struct attribute *,
1065 struct dwarf2_cu *);
c906108c 1066
f792889a 1067static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1068
673bfd45
DE
1069static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1070
086ed43d 1071static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1072
6e70227d 1073static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1074 const char *suffix, int physname,
1075 struct dwarf2_cu *cu);
63d06c5c 1076
e7c27a73 1077static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1078
348e048f
DE
1079static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1080
e7c27a73 1081static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1082
e7c27a73 1083static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1084
96408a79
SA
1085static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1086
ff013f42
JK
1087static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1088 struct dwarf2_cu *, struct partial_symtab *);
1089
a14ed312 1090static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1091 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1092 struct partial_symtab *);
c906108c 1093
fae299cd
DC
1094static void get_scope_pc_bounds (struct die_info *,
1095 CORE_ADDR *, CORE_ADDR *,
1096 struct dwarf2_cu *);
1097
801e3a5b
JB
1098static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1099 CORE_ADDR, struct dwarf2_cu *);
1100
a14ed312 1101static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1102 struct dwarf2_cu *);
c906108c 1103
a14ed312 1104static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1105 struct type *, struct dwarf2_cu *);
c906108c 1106
a14ed312 1107static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1108 struct die_info *, struct type *,
e7c27a73 1109 struct dwarf2_cu *);
c906108c 1110
a14ed312 1111static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1112 struct type *,
1113 struct dwarf2_cu *);
c906108c 1114
134d01f1 1115static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1116
e7c27a73 1117static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1118
e7c27a73 1119static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1120
5d7cb8df
JK
1121static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1122
27aa8d6a
SW
1123static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1124
f55ee35c
JK
1125static struct type *read_module_type (struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
38d518c9 1128static const char *namespace_name (struct die_info *die,
e142c38c 1129 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1130
134d01f1 1131static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1132
e7c27a73 1133static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1134
6e70227d 1135static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1136 struct dwarf2_cu *);
1137
93311388 1138static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1139
93311388
DE
1140static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1141 gdb_byte *info_ptr,
d97bc12b
DE
1142 gdb_byte **new_info_ptr,
1143 struct die_info *parent);
1144
93311388
DE
1145static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1146 gdb_byte *info_ptr,
fe1b8b76 1147 gdb_byte **new_info_ptr,
639d11d3
DC
1148 struct die_info *parent);
1149
93311388
DE
1150static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1151 gdb_byte *info_ptr,
fe1b8b76 1152 gdb_byte **new_info_ptr,
639d11d3
DC
1153 struct die_info *parent);
1154
93311388
DE
1155static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1156 struct die_info **, gdb_byte *,
1157 int *);
1158
e7c27a73 1159static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1160
71c25dea
TT
1161static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1162 struct obstack *);
1163
e142c38c 1164static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1165
98bfdba5
PA
1166static const char *dwarf2_full_name (char *name,
1167 struct die_info *die,
1168 struct dwarf2_cu *cu);
1169
e142c38c 1170static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1171 struct dwarf2_cu **);
9219021c 1172
a14ed312 1173static char *dwarf_tag_name (unsigned int);
c906108c 1174
a14ed312 1175static char *dwarf_attr_name (unsigned int);
c906108c 1176
a14ed312 1177static char *dwarf_form_name (unsigned int);
c906108c 1178
a14ed312 1179static char *dwarf_bool_name (unsigned int);
c906108c 1180
a14ed312 1181static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1182
1183#if 0
a14ed312 1184static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1185#endif
1186
f9aca02d 1187static struct die_info *sibling_die (struct die_info *);
c906108c 1188
d97bc12b
DE
1189static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1190
1191static void dump_die_for_error (struct die_info *);
1192
1193static void dump_die_1 (struct ui_file *, int level, int max_level,
1194 struct die_info *);
c906108c 1195
d97bc12b 1196/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1197
51545339 1198static void store_in_ref_table (struct die_info *,
10b3939b 1199 struct dwarf2_cu *);
c906108c 1200
93311388
DE
1201static int is_ref_attr (struct attribute *);
1202
c764a876 1203static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1204
43bbcdc2 1205static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1206
348e048f
DE
1207static struct die_info *follow_die_ref_or_sig (struct die_info *,
1208 struct attribute *,
1209 struct dwarf2_cu **);
1210
10b3939b
DJ
1211static struct die_info *follow_die_ref (struct die_info *,
1212 struct attribute *,
f2f0e013 1213 struct dwarf2_cu **);
c906108c 1214
348e048f
DE
1215static struct die_info *follow_die_sig (struct die_info *,
1216 struct attribute *,
1217 struct dwarf2_cu **);
1218
6c83ed52
TT
1219static struct signatured_type *lookup_signatured_type_at_offset
1220 (struct objfile *objfile,
1221 struct dwarf2_section_info *section,
1222 unsigned int offset);
1223
348e048f 1224static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1225 struct dwarf2_section_info *sect,
348e048f
DE
1226 unsigned int offset);
1227
a0f42c21 1228static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1229
c906108c
SS
1230/* memory allocation interface */
1231
7b5a2f43 1232static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1233
f3dd6933 1234static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1235
b60c80d6 1236static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1237
2e276125 1238static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1239 char *, bfd *, struct dwarf2_cu *,
1240 struct dwarf2_section_info *,
1241 int);
2e276125 1242
8e19ed76
PS
1243static int attr_form_is_block (struct attribute *);
1244
3690dd37
JB
1245static int attr_form_is_section_offset (struct attribute *);
1246
1247static int attr_form_is_constant (struct attribute *);
1248
8cf6f0b1
TT
1249static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1250 struct dwarf2_loclist_baton *baton,
1251 struct attribute *attr);
1252
93e7bd98
DJ
1253static void dwarf2_symbol_mark_computed (struct attribute *attr,
1254 struct symbol *sym,
1255 struct dwarf2_cu *cu);
4c2df51b 1256
93311388
DE
1257static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1258 struct abbrev_info *abbrev,
1259 struct dwarf2_cu *cu);
4bb7a0a7 1260
72bf9492
DJ
1261static void free_stack_comp_unit (void *);
1262
72bf9492
DJ
1263static hashval_t partial_die_hash (const void *item);
1264
1265static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1266
ae038cb0 1267static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1268 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1269
1270static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1271 (unsigned int offset, struct objfile *objfile);
ae038cb0 1272
9816fde3
JK
1273static void init_one_comp_unit (struct dwarf2_cu *cu,
1274 struct objfile *objfile);
1275
1276static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1277 struct die_info *comp_unit_die);
93311388 1278
68dc6402 1279static void free_heap_comp_unit (void *);
ae038cb0
DJ
1280
1281static void free_cached_comp_units (void *);
1282
1283static void age_cached_comp_units (void);
1284
1285static void free_one_cached_comp_unit (void *);
1286
f792889a
DJ
1287static struct type *set_die_type (struct die_info *, struct type *,
1288 struct dwarf2_cu *);
1c379e20 1289
ae038cb0
DJ
1290static void create_all_comp_units (struct objfile *);
1291
1fd400ff
TT
1292static int create_debug_types_hash_table (struct objfile *objfile);
1293
a0f42c21 1294static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1295
1296static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1297
1298static void dwarf2_add_dependence (struct dwarf2_cu *,
1299 struct dwarf2_per_cu_data *);
1300
ae038cb0
DJ
1301static void dwarf2_mark (struct dwarf2_cu *);
1302
1303static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1304
673bfd45
DE
1305static struct type *get_die_type_at_offset (unsigned int,
1306 struct dwarf2_per_cu_data *per_cu);
1307
f792889a 1308static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1309
9291a0cd
TT
1310static void dwarf2_release_queue (void *dummy);
1311
a0f42c21 1312static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1313
a0f42c21 1314static void process_queue (void);
9291a0cd
TT
1315
1316static void find_file_and_directory (struct die_info *die,
1317 struct dwarf2_cu *cu,
1318 char **name, char **comp_dir);
1319
1320static char *file_full_name (int file, struct line_header *lh,
1321 const char *comp_dir);
1322
1323static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1324 gdb_byte *info_ptr,
1325 gdb_byte *buffer,
1326 unsigned int buffer_size,
460c1c54 1327 bfd *abfd,
b0df02fd 1328 int is_debug_types_section);
9291a0cd
TT
1329
1330static void init_cu_die_reader (struct die_reader_specs *reader,
1331 struct dwarf2_cu *cu);
1332
673bfd45 1333static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1334
9291a0cd
TT
1335#if WORDS_BIGENDIAN
1336
1337/* Convert VALUE between big- and little-endian. */
1338static offset_type
1339byte_swap (offset_type value)
1340{
1341 offset_type result;
1342
1343 result = (value & 0xff) << 24;
1344 result |= (value & 0xff00) << 8;
1345 result |= (value & 0xff0000) >> 8;
1346 result |= (value & 0xff000000) >> 24;
1347 return result;
1348}
1349
1350#define MAYBE_SWAP(V) byte_swap (V)
1351
1352#else
1353#define MAYBE_SWAP(V) (V)
1354#endif /* WORDS_BIGENDIAN */
1355
1356/* The suffix for an index file. */
1357#define INDEX_SUFFIX ".gdb-index"
1358
3da10d80
KS
1359static const char *dwarf2_physname (char *name, struct die_info *die,
1360 struct dwarf2_cu *cu);
1361
c906108c 1362/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1363 information and return true if we have enough to do something.
1364 NAMES points to the dwarf2 section names, or is NULL if the standard
1365 ELF names are used. */
c906108c
SS
1366
1367int
251d32d9
TG
1368dwarf2_has_info (struct objfile *objfile,
1369 const struct dwarf2_debug_sections *names)
c906108c 1370{
be391dca
TT
1371 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1372 if (!dwarf2_per_objfile)
1373 {
1374 /* Initialize per-objfile state. */
1375 struct dwarf2_per_objfile *data
1376 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1377
be391dca
TT
1378 memset (data, 0, sizeof (*data));
1379 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1380 dwarf2_per_objfile = data;
6502dd73 1381
251d32d9
TG
1382 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1383 (void *) names);
be391dca
TT
1384 dwarf2_per_objfile->objfile = objfile;
1385 }
1386 return (dwarf2_per_objfile->info.asection != NULL
1387 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1388}
1389
251d32d9
TG
1390/* When loading sections, we look either for uncompressed section or for
1391 compressed section names. */
233a11ab
CS
1392
1393static int
251d32d9
TG
1394section_is_p (const char *section_name,
1395 const struct dwarf2_section_names *names)
233a11ab 1396{
251d32d9
TG
1397 if (names->normal != NULL
1398 && strcmp (section_name, names->normal) == 0)
1399 return 1;
1400 if (names->compressed != NULL
1401 && strcmp (section_name, names->compressed) == 0)
1402 return 1;
1403 return 0;
233a11ab
CS
1404}
1405
c906108c
SS
1406/* This function is mapped across the sections and remembers the
1407 offset and size of each of the debugging sections we are interested
1408 in. */
1409
1410static void
251d32d9 1411dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1412{
251d32d9
TG
1413 const struct dwarf2_debug_sections *names;
1414
1415 if (vnames == NULL)
1416 names = &dwarf2_elf_names;
1417 else
1418 names = (const struct dwarf2_debug_sections *) vnames;
1419
1420 if (section_is_p (sectp->name, &names->info))
c906108c 1421 {
dce234bc
PP
1422 dwarf2_per_objfile->info.asection = sectp;
1423 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1424 }
251d32d9 1425 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1426 {
dce234bc
PP
1427 dwarf2_per_objfile->abbrev.asection = sectp;
1428 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1429 }
251d32d9 1430 else if (section_is_p (sectp->name, &names->line))
c906108c 1431 {
dce234bc
PP
1432 dwarf2_per_objfile->line.asection = sectp;
1433 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1434 }
251d32d9 1435 else if (section_is_p (sectp->name, &names->loc))
c906108c 1436 {
dce234bc
PP
1437 dwarf2_per_objfile->loc.asection = sectp;
1438 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1439 }
251d32d9 1440 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1441 {
dce234bc
PP
1442 dwarf2_per_objfile->macinfo.asection = sectp;
1443 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1444 }
cf2c3c16
TT
1445 else if (section_is_p (sectp->name, &names->macro))
1446 {
1447 dwarf2_per_objfile->macro.asection = sectp;
1448 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1449 }
251d32d9 1450 else if (section_is_p (sectp->name, &names->str))
c906108c 1451 {
dce234bc
PP
1452 dwarf2_per_objfile->str.asection = sectp;
1453 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1454 }
251d32d9 1455 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1456 {
dce234bc
PP
1457 dwarf2_per_objfile->frame.asection = sectp;
1458 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1459 }
251d32d9 1460 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1461 {
3799ccc6 1462 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1463
3799ccc6
EZ
1464 if (aflag & SEC_HAS_CONTENTS)
1465 {
dce234bc
PP
1466 dwarf2_per_objfile->eh_frame.asection = sectp;
1467 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1468 }
b6af0555 1469 }
251d32d9 1470 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1471 {
dce234bc
PP
1472 dwarf2_per_objfile->ranges.asection = sectp;
1473 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1474 }
251d32d9 1475 else if (section_is_p (sectp->name, &names->types))
348e048f 1476 {
8b70b953
TT
1477 struct dwarf2_section_info type_section;
1478
1479 memset (&type_section, 0, sizeof (type_section));
1480 type_section.asection = sectp;
1481 type_section.size = bfd_get_section_size (sectp);
1482
1483 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1484 &type_section);
348e048f 1485 }
251d32d9 1486 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1487 {
1488 dwarf2_per_objfile->gdb_index.asection = sectp;
1489 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1490 }
dce234bc 1491
72dca2f5
FR
1492 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1493 && bfd_section_vma (abfd, sectp) == 0)
1494 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1495}
1496
dce234bc
PP
1497/* Decompress a section that was compressed using zlib. Store the
1498 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1499
1500static void
dce234bc
PP
1501zlib_decompress_section (struct objfile *objfile, asection *sectp,
1502 gdb_byte **outbuf, bfd_size_type *outsize)
1503{
1504 bfd *abfd = objfile->obfd;
1505#ifndef HAVE_ZLIB_H
1506 error (_("Support for zlib-compressed DWARF data (from '%s') "
1507 "is disabled in this copy of GDB"),
1508 bfd_get_filename (abfd));
1509#else
1510 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1511 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1512 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1513 bfd_size_type uncompressed_size;
1514 gdb_byte *uncompressed_buffer;
1515 z_stream strm;
1516 int rc;
1517 int header_size = 12;
1518
1519 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1520 || bfd_bread (compressed_buffer,
1521 compressed_size, abfd) != compressed_size)
dce234bc
PP
1522 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1523 bfd_get_filename (abfd));
1524
1525 /* Read the zlib header. In this case, it should be "ZLIB" followed
1526 by the uncompressed section size, 8 bytes in big-endian order. */
1527 if (compressed_size < header_size
1528 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1529 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1530 bfd_get_filename (abfd));
1531 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1532 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1533 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1534 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1535 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1536 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1537 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1538 uncompressed_size += compressed_buffer[11];
1539
1540 /* It is possible the section consists of several compressed
1541 buffers concatenated together, so we uncompress in a loop. */
1542 strm.zalloc = NULL;
1543 strm.zfree = NULL;
1544 strm.opaque = NULL;
1545 strm.avail_in = compressed_size - header_size;
1546 strm.next_in = (Bytef*) compressed_buffer + header_size;
1547 strm.avail_out = uncompressed_size;
1548 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1549 uncompressed_size);
1550 rc = inflateInit (&strm);
1551 while (strm.avail_in > 0)
1552 {
1553 if (rc != Z_OK)
1554 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1555 bfd_get_filename (abfd), rc);
1556 strm.next_out = ((Bytef*) uncompressed_buffer
1557 + (uncompressed_size - strm.avail_out));
1558 rc = inflate (&strm, Z_FINISH);
1559 if (rc != Z_STREAM_END)
1560 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1561 bfd_get_filename (abfd), rc);
1562 rc = inflateReset (&strm);
1563 }
1564 rc = inflateEnd (&strm);
1565 if (rc != Z_OK
1566 || strm.avail_out != 0)
1567 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1568 bfd_get_filename (abfd), rc);
1569
affddf13 1570 do_cleanups (cleanup);
dce234bc
PP
1571 *outbuf = uncompressed_buffer;
1572 *outsize = uncompressed_size;
1573#endif
233a11ab
CS
1574}
1575
9e0ac564
TT
1576/* A helper function that decides whether a section is empty. */
1577
1578static int
1579dwarf2_section_empty_p (struct dwarf2_section_info *info)
1580{
1581 return info->asection == NULL || info->size == 0;
1582}
1583
9cdd5dbd 1584/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1585 OBJFILE, store info about the section into INFO.
1586 If the section is compressed, uncompress it before returning. */
c906108c 1587
dce234bc
PP
1588static void
1589dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1590{
dce234bc
PP
1591 bfd *abfd = objfile->obfd;
1592 asection *sectp = info->asection;
1593 gdb_byte *buf, *retbuf;
1594 unsigned char header[4];
c906108c 1595
be391dca
TT
1596 if (info->readin)
1597 return;
dce234bc 1598 info->buffer = NULL;
b315ab21 1599 info->map_addr = NULL;
be391dca 1600 info->readin = 1;
188dd5d6 1601
9e0ac564 1602 if (dwarf2_section_empty_p (info))
dce234bc 1603 return;
c906108c 1604
dce234bc
PP
1605 /* Check if the file has a 4-byte header indicating compression. */
1606 if (info->size > sizeof (header)
1607 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1608 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1609 {
1610 /* Upon decompression, update the buffer and its size. */
1611 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1612 {
1613 zlib_decompress_section (objfile, sectp, &info->buffer,
1614 &info->size);
1615 return;
1616 }
1617 }
4bdf3d34 1618
dce234bc
PP
1619#ifdef HAVE_MMAP
1620 if (pagesize == 0)
1621 pagesize = getpagesize ();
2e276125 1622
dce234bc
PP
1623 /* Only try to mmap sections which are large enough: we don't want to
1624 waste space due to fragmentation. Also, only try mmap for sections
1625 without relocations. */
1626
1627 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1628 {
b315ab21
TG
1629 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1630 MAP_PRIVATE, sectp->filepos,
1631 &info->map_addr, &info->map_len);
dce234bc 1632
b315ab21 1633 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1634 {
be391dca 1635#if HAVE_POSIX_MADVISE
b315ab21 1636 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1637#endif
dce234bc
PP
1638 return;
1639 }
1640 }
1641#endif
1642
1643 /* If we get here, we are a normal, not-compressed section. */
1644 info->buffer = buf
1645 = obstack_alloc (&objfile->objfile_obstack, info->size);
1646
1647 /* When debugging .o files, we may need to apply relocations; see
1648 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1649 We never compress sections in .o files, so we only need to
1650 try this when the section is not compressed. */
ac8035ab 1651 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1652 if (retbuf != NULL)
1653 {
1654 info->buffer = retbuf;
1655 return;
1656 }
1657
1658 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1659 || bfd_bread (buf, info->size, abfd) != info->size)
1660 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1661 bfd_get_filename (abfd));
1662}
1663
9e0ac564
TT
1664/* A helper function that returns the size of a section in a safe way.
1665 If you are positive that the section has been read before using the
1666 size, then it is safe to refer to the dwarf2_section_info object's
1667 "size" field directly. In other cases, you must call this
1668 function, because for compressed sections the size field is not set
1669 correctly until the section has been read. */
1670
1671static bfd_size_type
1672dwarf2_section_size (struct objfile *objfile,
1673 struct dwarf2_section_info *info)
1674{
1675 if (!info->readin)
1676 dwarf2_read_section (objfile, info);
1677 return info->size;
1678}
1679
dce234bc 1680/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1681 SECTION_NAME. */
af34e669 1682
dce234bc 1683void
3017a003
TG
1684dwarf2_get_section_info (struct objfile *objfile,
1685 enum dwarf2_section_enum sect,
dce234bc
PP
1686 asection **sectp, gdb_byte **bufp,
1687 bfd_size_type *sizep)
1688{
1689 struct dwarf2_per_objfile *data
1690 = objfile_data (objfile, dwarf2_objfile_data_key);
1691 struct dwarf2_section_info *info;
a3b2a86b
TT
1692
1693 /* We may see an objfile without any DWARF, in which case we just
1694 return nothing. */
1695 if (data == NULL)
1696 {
1697 *sectp = NULL;
1698 *bufp = NULL;
1699 *sizep = 0;
1700 return;
1701 }
3017a003
TG
1702 switch (sect)
1703 {
1704 case DWARF2_DEBUG_FRAME:
1705 info = &data->frame;
1706 break;
1707 case DWARF2_EH_FRAME:
1708 info = &data->eh_frame;
1709 break;
1710 default:
1711 gdb_assert_not_reached ("unexpected section");
1712 }
dce234bc 1713
9e0ac564 1714 dwarf2_read_section (objfile, info);
dce234bc
PP
1715
1716 *sectp = info->asection;
1717 *bufp = info->buffer;
1718 *sizep = info->size;
1719}
1720
9291a0cd 1721\f
7b9f3c50
DE
1722/* DWARF quick_symbols_functions support. */
1723
1724/* TUs can share .debug_line entries, and there can be a lot more TUs than
1725 unique line tables, so we maintain a separate table of all .debug_line
1726 derived entries to support the sharing.
1727 All the quick functions need is the list of file names. We discard the
1728 line_header when we're done and don't need to record it here. */
1729struct quick_file_names
1730{
1731 /* The offset in .debug_line of the line table. We hash on this. */
1732 unsigned int offset;
1733
1734 /* The number of entries in file_names, real_names. */
1735 unsigned int num_file_names;
1736
1737 /* The file names from the line table, after being run through
1738 file_full_name. */
1739 const char **file_names;
1740
1741 /* The file names from the line table after being run through
1742 gdb_realpath. These are computed lazily. */
1743 const char **real_names;
1744};
1745
1746/* When using the index (and thus not using psymtabs), each CU has an
1747 object of this type. This is used to hold information needed by
1748 the various "quick" methods. */
1749struct dwarf2_per_cu_quick_data
1750{
1751 /* The file table. This can be NULL if there was no file table
1752 or it's currently not read in.
1753 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1754 struct quick_file_names *file_names;
1755
1756 /* The corresponding symbol table. This is NULL if symbols for this
1757 CU have not yet been read. */
1758 struct symtab *symtab;
1759
1760 /* A temporary mark bit used when iterating over all CUs in
1761 expand_symtabs_matching. */
1762 unsigned int mark : 1;
1763
1764 /* True if we've tried to read the file table and found there isn't one.
1765 There will be no point in trying to read it again next time. */
1766 unsigned int no_file_data : 1;
1767};
1768
1769/* Hash function for a quick_file_names. */
1770
1771static hashval_t
1772hash_file_name_entry (const void *e)
1773{
1774 const struct quick_file_names *file_data = e;
1775
1776 return file_data->offset;
1777}
1778
1779/* Equality function for a quick_file_names. */
1780
1781static int
1782eq_file_name_entry (const void *a, const void *b)
1783{
1784 const struct quick_file_names *ea = a;
1785 const struct quick_file_names *eb = b;
1786
1787 return ea->offset == eb->offset;
1788}
1789
1790/* Delete function for a quick_file_names. */
1791
1792static void
1793delete_file_name_entry (void *e)
1794{
1795 struct quick_file_names *file_data = e;
1796 int i;
1797
1798 for (i = 0; i < file_data->num_file_names; ++i)
1799 {
1800 xfree ((void*) file_data->file_names[i]);
1801 if (file_data->real_names)
1802 xfree ((void*) file_data->real_names[i]);
1803 }
1804
1805 /* The space for the struct itself lives on objfile_obstack,
1806 so we don't free it here. */
1807}
1808
1809/* Create a quick_file_names hash table. */
1810
1811static htab_t
1812create_quick_file_names_table (unsigned int nr_initial_entries)
1813{
1814 return htab_create_alloc (nr_initial_entries,
1815 hash_file_name_entry, eq_file_name_entry,
1816 delete_file_name_entry, xcalloc, xfree);
1817}
9291a0cd 1818
918dd910
JK
1819/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1820 have to be created afterwards. You should call age_cached_comp_units after
1821 processing PER_CU->CU. dw2_setup must have been already called. */
1822
1823static void
1824load_cu (struct dwarf2_per_cu_data *per_cu)
1825{
b0df02fd 1826 if (per_cu->debug_types_section)
8b70b953 1827 read_signatured_type_at_offset (per_cu->objfile,
b0df02fd 1828 per_cu->debug_types_section,
8b70b953 1829 per_cu->offset);
918dd910 1830 else
a0f42c21 1831 load_full_comp_unit (per_cu);
918dd910 1832
918dd910 1833 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1834
1835 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1836}
1837
a0f42c21 1838/* Read in the symbols for PER_CU. */
2fdf6df6 1839
9291a0cd 1840static void
a0f42c21 1841dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1842{
1843 struct cleanup *back_to;
1844
1845 back_to = make_cleanup (dwarf2_release_queue, NULL);
1846
a0f42c21 1847 queue_comp_unit (per_cu);
9291a0cd 1848
918dd910 1849 load_cu (per_cu);
9291a0cd 1850
a0f42c21 1851 process_queue ();
9291a0cd
TT
1852
1853 /* Age the cache, releasing compilation units that have not
1854 been used recently. */
1855 age_cached_comp_units ();
1856
1857 do_cleanups (back_to);
1858}
1859
1860/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1861 the objfile from which this CU came. Returns the resulting symbol
1862 table. */
2fdf6df6 1863
9291a0cd 1864static struct symtab *
a0f42c21 1865dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1866{
1867 if (!per_cu->v.quick->symtab)
1868 {
1869 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1870 increment_reading_symtab ();
a0f42c21 1871 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1872 do_cleanups (back_to);
1873 }
1874 return per_cu->v.quick->symtab;
1875}
1876
1fd400ff 1877/* Return the CU given its index. */
2fdf6df6 1878
1fd400ff
TT
1879static struct dwarf2_per_cu_data *
1880dw2_get_cu (int index)
1881{
1882 if (index >= dwarf2_per_objfile->n_comp_units)
1883 {
1884 index -= dwarf2_per_objfile->n_comp_units;
1885 return dwarf2_per_objfile->type_comp_units[index];
1886 }
1887 return dwarf2_per_objfile->all_comp_units[index];
1888}
1889
9291a0cd
TT
1890/* A helper function that knows how to read a 64-bit value in a way
1891 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1892 otherwise. */
2fdf6df6 1893
9291a0cd
TT
1894static int
1895extract_cu_value (const char *bytes, ULONGEST *result)
1896{
1897 if (sizeof (ULONGEST) < 8)
1898 {
1899 int i;
1900
1901 /* Ignore the upper 4 bytes if they are all zero. */
1902 for (i = 0; i < 4; ++i)
1903 if (bytes[i + 4] != 0)
1904 return 0;
1905
1906 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1907 }
1908 else
1909 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1910 return 1;
1911}
1912
1913/* Read the CU list from the mapped index, and use it to create all
1914 the CU objects for this objfile. Return 0 if something went wrong,
1915 1 if everything went ok. */
2fdf6df6 1916
9291a0cd 1917static int
1fd400ff
TT
1918create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1919 offset_type cu_list_elements)
9291a0cd
TT
1920{
1921 offset_type i;
9291a0cd
TT
1922
1923 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1924 dwarf2_per_objfile->all_comp_units
1925 = obstack_alloc (&objfile->objfile_obstack,
1926 dwarf2_per_objfile->n_comp_units
1927 * sizeof (struct dwarf2_per_cu_data *));
1928
1929 for (i = 0; i < cu_list_elements; i += 2)
1930 {
1931 struct dwarf2_per_cu_data *the_cu;
1932 ULONGEST offset, length;
1933
1934 if (!extract_cu_value (cu_list, &offset)
1935 || !extract_cu_value (cu_list + 8, &length))
1936 return 0;
1937 cu_list += 2 * 8;
1938
1939 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1940 struct dwarf2_per_cu_data);
1941 the_cu->offset = offset;
1942 the_cu->length = length;
1943 the_cu->objfile = objfile;
1944 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_quick_data);
1946 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1947 }
1948
1949 return 1;
1950}
1951
1fd400ff 1952/* Create the signatured type hash table from the index. */
673bfd45 1953
1fd400ff 1954static int
673bfd45 1955create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1956 struct dwarf2_section_info *section,
673bfd45
DE
1957 const gdb_byte *bytes,
1958 offset_type elements)
1fd400ff
TT
1959{
1960 offset_type i;
673bfd45 1961 htab_t sig_types_hash;
1fd400ff
TT
1962
1963 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1964 dwarf2_per_objfile->type_comp_units
1965 = obstack_alloc (&objfile->objfile_obstack,
1966 dwarf2_per_objfile->n_type_comp_units
1967 * sizeof (struct dwarf2_per_cu_data *));
1968
673bfd45 1969 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1970
1971 for (i = 0; i < elements; i += 3)
1972 {
1973 struct signatured_type *type_sig;
1974 ULONGEST offset, type_offset, signature;
1975 void **slot;
1976
1977 if (!extract_cu_value (bytes, &offset)
1978 || !extract_cu_value (bytes + 8, &type_offset))
1979 return 0;
1980 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1981 bytes += 3 * 8;
1982
1983 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1984 struct signatured_type);
1985 type_sig->signature = signature;
1fd400ff 1986 type_sig->type_offset = type_offset;
b0df02fd 1987 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1988 type_sig->per_cu.offset = offset;
1989 type_sig->per_cu.objfile = objfile;
1990 type_sig->per_cu.v.quick
1991 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1992 struct dwarf2_per_cu_quick_data);
1993
673bfd45 1994 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1995 *slot = type_sig;
1996
1997 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1998 }
1999
673bfd45 2000 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2001
2002 return 1;
2003}
2004
9291a0cd
TT
2005/* Read the address map data from the mapped index, and use it to
2006 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2007
9291a0cd
TT
2008static void
2009create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2010{
2011 const gdb_byte *iter, *end;
2012 struct obstack temp_obstack;
2013 struct addrmap *mutable_map;
2014 struct cleanup *cleanup;
2015 CORE_ADDR baseaddr;
2016
2017 obstack_init (&temp_obstack);
2018 cleanup = make_cleanup_obstack_free (&temp_obstack);
2019 mutable_map = addrmap_create_mutable (&temp_obstack);
2020
2021 iter = index->address_table;
2022 end = iter + index->address_table_size;
2023
2024 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2025
2026 while (iter < end)
2027 {
2028 ULONGEST hi, lo, cu_index;
2029 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2030 iter += 8;
2031 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2032 iter += 8;
2033 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2034 iter += 4;
2035
2036 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2037 dw2_get_cu (cu_index));
9291a0cd
TT
2038 }
2039
2040 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2041 &objfile->objfile_obstack);
2042 do_cleanups (cleanup);
2043}
2044
59d7bcaf
JK
2045/* The hash function for strings in the mapped index. This is the same as
2046 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2047 implementation. This is necessary because the hash function is tied to the
2048 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2049 SYMBOL_HASH_NEXT.
2050
2051 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2052
9291a0cd 2053static hashval_t
559a7a62 2054mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2055{
2056 const unsigned char *str = (const unsigned char *) p;
2057 hashval_t r = 0;
2058 unsigned char c;
2059
2060 while ((c = *str++) != 0)
559a7a62
JK
2061 {
2062 if (index_version >= 5)
2063 c = tolower (c);
2064 r = r * 67 + c - 113;
2065 }
9291a0cd
TT
2066
2067 return r;
2068}
2069
2070/* Find a slot in the mapped index INDEX for the object named NAME.
2071 If NAME is found, set *VEC_OUT to point to the CU vector in the
2072 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2073
9291a0cd
TT
2074static int
2075find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2076 offset_type **vec_out)
2077{
0cf03b49
JK
2078 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2079 offset_type hash;
9291a0cd 2080 offset_type slot, step;
559a7a62 2081 int (*cmp) (const char *, const char *);
9291a0cd 2082
0cf03b49
JK
2083 if (current_language->la_language == language_cplus
2084 || current_language->la_language == language_java
2085 || current_language->la_language == language_fortran)
2086 {
2087 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2088 not contain any. */
2089 const char *paren = strchr (name, '(');
2090
2091 if (paren)
2092 {
2093 char *dup;
2094
2095 dup = xmalloc (paren - name + 1);
2096 memcpy (dup, name, paren - name);
2097 dup[paren - name] = 0;
2098
2099 make_cleanup (xfree, dup);
2100 name = dup;
2101 }
2102 }
2103
559a7a62
JK
2104 /* Index version 4 did not support case insensitive searches. But the
2105 indexes for case insensitive languages are built in lowercase, therefore
2106 simulate our NAME being searched is also lowercased. */
2107 hash = mapped_index_string_hash ((index->version == 4
2108 && case_sensitivity == case_sensitive_off
2109 ? 5 : index->version),
2110 name);
2111
3876f04e
DE
2112 slot = hash & (index->symbol_table_slots - 1);
2113 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2114 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2115
2116 for (;;)
2117 {
2118 /* Convert a slot number to an offset into the table. */
2119 offset_type i = 2 * slot;
2120 const char *str;
3876f04e 2121 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2122 {
2123 do_cleanups (back_to);
2124 return 0;
2125 }
9291a0cd 2126
3876f04e 2127 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2128 if (!cmp (name, str))
9291a0cd
TT
2129 {
2130 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2131 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2132 do_cleanups (back_to);
9291a0cd
TT
2133 return 1;
2134 }
2135
3876f04e 2136 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2137 }
2138}
2139
2140/* Read the index file. If everything went ok, initialize the "quick"
2141 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2142
9291a0cd
TT
2143static int
2144dwarf2_read_index (struct objfile *objfile)
2145{
9291a0cd
TT
2146 char *addr;
2147 struct mapped_index *map;
b3b272e1 2148 offset_type *metadata;
ac0b195c
KW
2149 const gdb_byte *cu_list;
2150 const gdb_byte *types_list = NULL;
2151 offset_type version, cu_list_elements;
2152 offset_type types_list_elements = 0;
1fd400ff 2153 int i;
9291a0cd 2154
9e0ac564 2155 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2156 return 0;
82430852
JK
2157
2158 /* Older elfutils strip versions could keep the section in the main
2159 executable while splitting it for the separate debug info file. */
2160 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2161 & SEC_HAS_CONTENTS) == 0)
2162 return 0;
2163
9291a0cd
TT
2164 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2165
2166 addr = dwarf2_per_objfile->gdb_index.buffer;
2167 /* Version check. */
1fd400ff 2168 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2169 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2170 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2171 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2172 indices. Index version 4 uses a different hash function than index
2173 version 5 and later. */
831adc1f 2174 if (version < 4)
9291a0cd 2175 return 0;
594e8718
JK
2176 /* Indexes with higher version than the one supported by GDB may be no
2177 longer backward compatible. */
559a7a62 2178 if (version > 5)
594e8718 2179 return 0;
9291a0cd
TT
2180
2181 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2182 map->version = version;
b3b272e1 2183 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2184
2185 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2186
2187 i = 0;
2188 cu_list = addr + MAYBE_SWAP (metadata[i]);
2189 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2190 / 8);
1fd400ff
TT
2191 ++i;
2192
987d643c
TT
2193 types_list = addr + MAYBE_SWAP (metadata[i]);
2194 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2195 - MAYBE_SWAP (metadata[i]))
2196 / 8);
2197 ++i;
1fd400ff
TT
2198
2199 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2200 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2201 - MAYBE_SWAP (metadata[i]));
2202 ++i;
2203
3876f04e
DE
2204 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2205 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2206 - MAYBE_SWAP (metadata[i]))
2207 / (2 * sizeof (offset_type)));
1fd400ff 2208 ++i;
9291a0cd 2209
1fd400ff
TT
2210 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2211
2212 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2213 return 0;
2214
8b70b953
TT
2215 if (types_list_elements)
2216 {
2217 struct dwarf2_section_info *section;
2218
2219 /* We can only handle a single .debug_types when we have an
2220 index. */
2221 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2222 return 0;
2223
2224 section = VEC_index (dwarf2_section_info_def,
2225 dwarf2_per_objfile->types, 0);
2226
2227 if (!create_signatured_type_table_from_index (objfile, section,
2228 types_list,
2229 types_list_elements))
2230 return 0;
2231 }
9291a0cd
TT
2232
2233 create_addrmap_from_index (objfile, map);
2234
2235 dwarf2_per_objfile->index_table = map;
2236 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2237 dwarf2_per_objfile->quick_file_names_table =
2238 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2239
2240 return 1;
2241}
2242
2243/* A helper for the "quick" functions which sets the global
2244 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2245
9291a0cd
TT
2246static void
2247dw2_setup (struct objfile *objfile)
2248{
2249 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2250 gdb_assert (dwarf2_per_objfile);
2251}
2252
2253/* A helper for the "quick" functions which attempts to read the line
2254 table for THIS_CU. */
2fdf6df6 2255
7b9f3c50
DE
2256static struct quick_file_names *
2257dw2_get_file_names (struct objfile *objfile,
2258 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2259{
2260 bfd *abfd = objfile->obfd;
7b9f3c50 2261 struct line_header *lh;
9291a0cd
TT
2262 struct attribute *attr;
2263 struct cleanup *cleanups;
2264 struct die_info *comp_unit_die;
36374493 2265 struct dwarf2_section_info* sec;
adabb602 2266 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2267 int has_children, i;
2268 struct dwarf2_cu cu;
2269 unsigned int bytes_read, buffer_size;
2270 struct die_reader_specs reader_specs;
2271 char *name, *comp_dir;
7b9f3c50
DE
2272 void **slot;
2273 struct quick_file_names *qfn;
2274 unsigned int line_offset;
9291a0cd 2275
7b9f3c50
DE
2276 if (this_cu->v.quick->file_names != NULL)
2277 return this_cu->v.quick->file_names;
2278 /* If we know there is no line data, no point in looking again. */
2279 if (this_cu->v.quick->no_file_data)
2280 return NULL;
9291a0cd 2281
9816fde3 2282 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2283 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2284
b0df02fd
DE
2285 if (this_cu->debug_types_section)
2286 sec = this_cu->debug_types_section;
36374493
DE
2287 else
2288 sec = &dwarf2_per_objfile->info;
2289 dwarf2_read_section (objfile, sec);
2290 buffer_size = sec->size;
2291 buffer = sec->buffer;
9291a0cd 2292 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2293
2294 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2295 buffer, buffer_size,
460c1c54 2296 abfd,
b0df02fd 2297 this_cu->debug_types_section != NULL);
9291a0cd 2298
6caca83c
CC
2299 /* Skip dummy compilation units. */
2300 if (info_ptr >= buffer + buffer_size
2301 || peek_abbrev_code (abfd, info_ptr) == 0)
2302 {
2303 do_cleanups (cleanups);
2304 return NULL;
2305 }
2306
9291a0cd
TT
2307 this_cu->cu = &cu;
2308 cu.per_cu = this_cu;
2309
2310 dwarf2_read_abbrevs (abfd, &cu);
2311 make_cleanup (dwarf2_free_abbrev_table, &cu);
2312
9291a0cd 2313 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2314 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2315 &has_children);
9291a0cd 2316
7b9f3c50
DE
2317 lh = NULL;
2318 slot = NULL;
2319 line_offset = 0;
9291a0cd
TT
2320 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2321 if (attr)
2322 {
7b9f3c50
DE
2323 struct quick_file_names find_entry;
2324
2325 line_offset = DW_UNSND (attr);
2326
2327 /* We may have already read in this line header (TU line header sharing).
2328 If we have we're done. */
2329 find_entry.offset = line_offset;
2330 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2331 &find_entry, INSERT);
2332 if (*slot != NULL)
2333 {
2334 do_cleanups (cleanups);
2335 this_cu->v.quick->file_names = *slot;
2336 return *slot;
2337 }
2338
9291a0cd
TT
2339 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2340 }
2341 if (lh == NULL)
2342 {
2343 do_cleanups (cleanups);
7b9f3c50
DE
2344 this_cu->v.quick->no_file_data = 1;
2345 return NULL;
9291a0cd
TT
2346 }
2347
7b9f3c50
DE
2348 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2349 qfn->offset = line_offset;
2350 gdb_assert (slot != NULL);
2351 *slot = qfn;
9291a0cd 2352
7b9f3c50 2353 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2354
7b9f3c50
DE
2355 qfn->num_file_names = lh->num_file_names;
2356 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2357 lh->num_file_names * sizeof (char *));
9291a0cd 2358 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2359 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2360 qfn->real_names = NULL;
9291a0cd 2361
7b9f3c50 2362 free_line_header (lh);
9291a0cd 2363 do_cleanups (cleanups);
7b9f3c50
DE
2364
2365 this_cu->v.quick->file_names = qfn;
2366 return qfn;
9291a0cd
TT
2367}
2368
2369/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2370 real path for a given file name from the line table. */
2fdf6df6 2371
9291a0cd 2372static const char *
7b9f3c50
DE
2373dw2_get_real_path (struct objfile *objfile,
2374 struct quick_file_names *qfn, int index)
9291a0cd 2375{
7b9f3c50
DE
2376 if (qfn->real_names == NULL)
2377 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2378 qfn->num_file_names, sizeof (char *));
9291a0cd 2379
7b9f3c50
DE
2380 if (qfn->real_names[index] == NULL)
2381 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2382
7b9f3c50 2383 return qfn->real_names[index];
9291a0cd
TT
2384}
2385
2386static struct symtab *
2387dw2_find_last_source_symtab (struct objfile *objfile)
2388{
2389 int index;
ae2de4f8 2390
9291a0cd
TT
2391 dw2_setup (objfile);
2392 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2393 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2394}
2395
7b9f3c50
DE
2396/* Traversal function for dw2_forget_cached_source_info. */
2397
2398static int
2399dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2400{
7b9f3c50 2401 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2402
7b9f3c50 2403 if (file_data->real_names)
9291a0cd 2404 {
7b9f3c50 2405 int i;
9291a0cd 2406
7b9f3c50 2407 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2408 {
7b9f3c50
DE
2409 xfree ((void*) file_data->real_names[i]);
2410 file_data->real_names[i] = NULL;
9291a0cd
TT
2411 }
2412 }
7b9f3c50
DE
2413
2414 return 1;
2415}
2416
2417static void
2418dw2_forget_cached_source_info (struct objfile *objfile)
2419{
2420 dw2_setup (objfile);
2421
2422 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2423 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2424}
2425
f8eba3c6
TT
2426/* Helper function for dw2_map_symtabs_matching_filename that expands
2427 the symtabs and calls the iterator. */
2428
2429static int
2430dw2_map_expand_apply (struct objfile *objfile,
2431 struct dwarf2_per_cu_data *per_cu,
2432 const char *name,
2433 const char *full_path, const char *real_path,
2434 int (*callback) (struct symtab *, void *),
2435 void *data)
2436{
2437 struct symtab *last_made = objfile->symtabs;
2438
2439 /* Don't visit already-expanded CUs. */
2440 if (per_cu->v.quick->symtab)
2441 return 0;
2442
2443 /* This may expand more than one symtab, and we want to iterate over
2444 all of them. */
a0f42c21 2445 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2446
2447 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2448 objfile->symtabs, last_made);
2449}
2450
2451/* Implementation of the map_symtabs_matching_filename method. */
2452
9291a0cd 2453static int
f8eba3c6
TT
2454dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2455 const char *full_path, const char *real_path,
2456 int (*callback) (struct symtab *, void *),
2457 void *data)
9291a0cd
TT
2458{
2459 int i;
c011a4f4
DE
2460 const char *name_basename = lbasename (name);
2461 int check_basename = name_basename == name;
9291a0cd
TT
2462 struct dwarf2_per_cu_data *base_cu = NULL;
2463
2464 dw2_setup (objfile);
ae2de4f8 2465
1fd400ff
TT
2466 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2467 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2468 {
2469 int j;
e254ef6a 2470 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2471 struct quick_file_names *file_data;
9291a0cd 2472
3d7bb9d9 2473 /* We only need to look at symtabs not already expanded. */
e254ef6a 2474 if (per_cu->v.quick->symtab)
9291a0cd
TT
2475 continue;
2476
7b9f3c50
DE
2477 file_data = dw2_get_file_names (objfile, per_cu);
2478 if (file_data == NULL)
9291a0cd
TT
2479 continue;
2480
7b9f3c50 2481 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2482 {
7b9f3c50 2483 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2484
2485 if (FILENAME_CMP (name, this_name) == 0)
2486 {
f8eba3c6
TT
2487 if (dw2_map_expand_apply (objfile, per_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
9291a0cd
TT
2491 }
2492
2493 if (check_basename && ! base_cu
2494 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2495 base_cu = per_cu;
9291a0cd 2496
c011a4f4
DE
2497 /* Before we invoke realpath, which can get expensive when many
2498 files are involved, do a quick comparison of the basenames. */
2499 if (! basenames_may_differ
2500 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2501 continue;
2502
9291a0cd
TT
2503 if (full_path != NULL)
2504 {
7b9f3c50
DE
2505 const char *this_real_name = dw2_get_real_path (objfile,
2506 file_data, j);
9291a0cd 2507
7b9f3c50
DE
2508 if (this_real_name != NULL
2509 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2510 {
f8eba3c6
TT
2511 if (dw2_map_expand_apply (objfile, per_cu,
2512 name, full_path, real_path,
2513 callback, data))
2514 return 1;
9291a0cd
TT
2515 }
2516 }
2517
2518 if (real_path != NULL)
2519 {
7b9f3c50
DE
2520 const char *this_real_name = dw2_get_real_path (objfile,
2521 file_data, j);
9291a0cd 2522
7b9f3c50
DE
2523 if (this_real_name != NULL
2524 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2525 {
f8eba3c6
TT
2526 if (dw2_map_expand_apply (objfile, per_cu,
2527 name, full_path, real_path,
2528 callback, data))
2529 return 1;
9291a0cd
TT
2530 }
2531 }
2532 }
2533 }
2534
2535 if (base_cu)
2536 {
f8eba3c6
TT
2537 if (dw2_map_expand_apply (objfile, base_cu,
2538 name, full_path, real_path,
2539 callback, data))
2540 return 1;
9291a0cd
TT
2541 }
2542
2543 return 0;
2544}
2545
2546static struct symtab *
2547dw2_lookup_symbol (struct objfile *objfile, int block_index,
2548 const char *name, domain_enum domain)
2549{
774b6a14 2550 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2551 instead. */
2552 return NULL;
2553}
2554
2555/* A helper function that expands all symtabs that hold an object
2556 named NAME. */
2fdf6df6 2557
9291a0cd
TT
2558static void
2559dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2560{
2561 dw2_setup (objfile);
2562
ae2de4f8 2563 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2564 if (dwarf2_per_objfile->index_table)
2565 {
2566 offset_type *vec;
2567
2568 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2569 name, &vec))
2570 {
2571 offset_type i, len = MAYBE_SWAP (*vec);
2572 for (i = 0; i < len; ++i)
2573 {
2574 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2575 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2576
a0f42c21 2577 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2578 }
2579 }
2580 }
2581}
2582
774b6a14
TT
2583static void
2584dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2585 enum block_enum block_kind, const char *name,
774b6a14 2586 domain_enum domain)
9291a0cd 2587{
774b6a14 2588 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2589}
2590
2591static void
2592dw2_print_stats (struct objfile *objfile)
2593{
2594 int i, count;
2595
2596 dw2_setup (objfile);
2597 count = 0;
1fd400ff
TT
2598 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2599 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2600 {
e254ef6a 2601 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2602
e254ef6a 2603 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2604 ++count;
2605 }
2606 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2607}
2608
2609static void
2610dw2_dump (struct objfile *objfile)
2611{
2612 /* Nothing worth printing. */
2613}
2614
2615static void
2616dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2617 struct section_offsets *delta)
2618{
2619 /* There's nothing to relocate here. */
2620}
2621
2622static void
2623dw2_expand_symtabs_for_function (struct objfile *objfile,
2624 const char *func_name)
2625{
2626 dw2_do_expand_symtabs_matching (objfile, func_name);
2627}
2628
2629static void
2630dw2_expand_all_symtabs (struct objfile *objfile)
2631{
2632 int i;
2633
2634 dw2_setup (objfile);
1fd400ff
TT
2635
2636 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2637 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2638 {
e254ef6a 2639 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2640
a0f42c21 2641 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2642 }
2643}
2644
2645static void
2646dw2_expand_symtabs_with_filename (struct objfile *objfile,
2647 const char *filename)
2648{
2649 int i;
2650
2651 dw2_setup (objfile);
d4637a04
DE
2652
2653 /* We don't need to consider type units here.
2654 This is only called for examining code, e.g. expand_line_sal.
2655 There can be an order of magnitude (or more) more type units
2656 than comp units, and we avoid them if we can. */
2657
2658 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2659 {
2660 int j;
e254ef6a 2661 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2662 struct quick_file_names *file_data;
9291a0cd 2663
3d7bb9d9 2664 /* We only need to look at symtabs not already expanded. */
e254ef6a 2665 if (per_cu->v.quick->symtab)
9291a0cd
TT
2666 continue;
2667
7b9f3c50
DE
2668 file_data = dw2_get_file_names (objfile, per_cu);
2669 if (file_data == NULL)
9291a0cd
TT
2670 continue;
2671
7b9f3c50 2672 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2673 {
7b9f3c50 2674 const char *this_name = file_data->file_names[j];
1ef75ecc 2675 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2676 {
a0f42c21 2677 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2678 break;
2679 }
2680 }
2681 }
2682}
2683
dd786858 2684static const char *
9291a0cd
TT
2685dw2_find_symbol_file (struct objfile *objfile, const char *name)
2686{
e254ef6a 2687 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2688 offset_type *vec;
7b9f3c50 2689 struct quick_file_names *file_data;
9291a0cd
TT
2690
2691 dw2_setup (objfile);
2692
ae2de4f8 2693 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2694 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2695 {
2696 struct symtab *s;
2697
2698 ALL_OBJFILE_SYMTABS (objfile, s)
2699 if (s->primary)
2700 {
2701 struct blockvector *bv = BLOCKVECTOR (s);
2702 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2703 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2704
2705 if (sym)
2706 return sym->symtab->filename;
2707 }
2708 return NULL;
2709 }
9291a0cd
TT
2710
2711 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2712 name, &vec))
2713 return NULL;
2714
2715 /* Note that this just looks at the very first one named NAME -- but
2716 actually we are looking for a function. find_main_filename
2717 should be rewritten so that it doesn't require a custom hook. It
2718 could just use the ordinary symbol tables. */
2719 /* vec[0] is the length, which must always be >0. */
e254ef6a 2720 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2721
7b9f3c50
DE
2722 file_data = dw2_get_file_names (objfile, per_cu);
2723 if (file_data == NULL)
9291a0cd
TT
2724 return NULL;
2725
7b9f3c50 2726 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2727}
2728
2729static void
40658b94
PH
2730dw2_map_matching_symbols (const char * name, domain_enum namespace,
2731 struct objfile *objfile, int global,
2732 int (*callback) (struct block *,
2733 struct symbol *, void *),
2edb89d3
JK
2734 void *data, symbol_compare_ftype *match,
2735 symbol_compare_ftype *ordered_compare)
9291a0cd 2736{
40658b94 2737 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2738 current language is Ada for a non-Ada objfile using GNU index. As Ada
2739 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2740}
2741
2742static void
f8eba3c6
TT
2743dw2_expand_symtabs_matching
2744 (struct objfile *objfile,
2745 int (*file_matcher) (const char *, void *),
2746 int (*name_matcher) (const struct language_defn *, const char *, void *),
2747 enum search_domain kind,
2748 void *data)
9291a0cd
TT
2749{
2750 int i;
2751 offset_type iter;
4b5246aa 2752 struct mapped_index *index;
9291a0cd
TT
2753
2754 dw2_setup (objfile);
ae2de4f8
DE
2755
2756 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2757 if (!dwarf2_per_objfile->index_table)
2758 return;
4b5246aa 2759 index = dwarf2_per_objfile->index_table;
9291a0cd 2760
7b08b9eb
JK
2761 if (file_matcher != NULL)
2762 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2763 + dwarf2_per_objfile->n_type_comp_units); ++i)
2764 {
2765 int j;
2766 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2767 struct quick_file_names *file_data;
2768
2769 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2770
2771 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2772 if (per_cu->v.quick->symtab)
2773 continue;
2774
2775 file_data = dw2_get_file_names (objfile, per_cu);
2776 if (file_data == NULL)
2777 continue;
2778
2779 for (j = 0; j < file_data->num_file_names; ++j)
2780 {
2781 if (file_matcher (file_data->file_names[j], data))
2782 {
2783 per_cu->v.quick->mark = 1;
2784 break;
2785 }
2786 }
2787 }
9291a0cd 2788
3876f04e 2789 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2790 {
2791 offset_type idx = 2 * iter;
2792 const char *name;
2793 offset_type *vec, vec_len, vec_idx;
2794
3876f04e 2795 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2796 continue;
2797
3876f04e 2798 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2799
f8eba3c6 2800 if (! (*name_matcher) (current_language, name, data))
9291a0cd
TT
2801 continue;
2802
2803 /* The name was matched, now expand corresponding CUs that were
2804 marked. */
4b5246aa 2805 vec = (offset_type *) (index->constant_pool
3876f04e 2806 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2807 vec_len = MAYBE_SWAP (vec[0]);
2808 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2809 {
e254ef6a 2810 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2811
e254ef6a 2812 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2813 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2814 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2815 }
2816 }
2817}
2818
2819static struct symtab *
2820dw2_find_pc_sect_symtab (struct objfile *objfile,
2821 struct minimal_symbol *msymbol,
2822 CORE_ADDR pc,
2823 struct obj_section *section,
2824 int warn_if_readin)
2825{
2826 struct dwarf2_per_cu_data *data;
2827
2828 dw2_setup (objfile);
2829
2830 if (!objfile->psymtabs_addrmap)
2831 return NULL;
2832
2833 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2834 if (!data)
2835 return NULL;
2836
2837 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2838 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2839 paddress (get_objfile_arch (objfile), pc));
2840
a0f42c21 2841 return dw2_instantiate_symtab (data);
9291a0cd
TT
2842}
2843
9291a0cd 2844static void
44b13c5a 2845dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2846 void *data, int need_fullname)
9291a0cd
TT
2847{
2848 int i;
2849
2850 dw2_setup (objfile);
ae2de4f8 2851
1fd400ff
TT
2852 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2853 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2854 {
2855 int j;
e254ef6a 2856 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2857 struct quick_file_names *file_data;
9291a0cd 2858
3d7bb9d9 2859 /* We only need to look at symtabs not already expanded. */
e254ef6a 2860 if (per_cu->v.quick->symtab)
9291a0cd
TT
2861 continue;
2862
7b9f3c50
DE
2863 file_data = dw2_get_file_names (objfile, per_cu);
2864 if (file_data == NULL)
9291a0cd
TT
2865 continue;
2866
7b9f3c50 2867 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2868 {
74e2f255
DE
2869 const char *this_real_name;
2870
2871 if (need_fullname)
2872 this_real_name = dw2_get_real_path (objfile, file_data, j);
2873 else
2874 this_real_name = NULL;
7b9f3c50 2875 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2876 }
2877 }
2878}
2879
2880static int
2881dw2_has_symbols (struct objfile *objfile)
2882{
2883 return 1;
2884}
2885
2886const struct quick_symbol_functions dwarf2_gdb_index_functions =
2887{
2888 dw2_has_symbols,
2889 dw2_find_last_source_symtab,
2890 dw2_forget_cached_source_info,
f8eba3c6 2891 dw2_map_symtabs_matching_filename,
9291a0cd 2892 dw2_lookup_symbol,
774b6a14 2893 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2894 dw2_print_stats,
2895 dw2_dump,
2896 dw2_relocate,
2897 dw2_expand_symtabs_for_function,
2898 dw2_expand_all_symtabs,
2899 dw2_expand_symtabs_with_filename,
2900 dw2_find_symbol_file,
40658b94 2901 dw2_map_matching_symbols,
9291a0cd
TT
2902 dw2_expand_symtabs_matching,
2903 dw2_find_pc_sect_symtab,
9291a0cd
TT
2904 dw2_map_symbol_filenames
2905};
2906
2907/* Initialize for reading DWARF for this objfile. Return 0 if this
2908 file will use psymtabs, or 1 if using the GNU index. */
2909
2910int
2911dwarf2_initialize_objfile (struct objfile *objfile)
2912{
2913 /* If we're about to read full symbols, don't bother with the
2914 indices. In this case we also don't care if some other debug
2915 format is making psymtabs, because they are all about to be
2916 expanded anyway. */
2917 if ((objfile->flags & OBJF_READNOW))
2918 {
2919 int i;
2920
2921 dwarf2_per_objfile->using_index = 1;
2922 create_all_comp_units (objfile);
1fd400ff 2923 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2924 dwarf2_per_objfile->quick_file_names_table =
2925 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2926
1fd400ff
TT
2927 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2928 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2929 {
e254ef6a 2930 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2931
e254ef6a
DE
2932 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2933 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2934 }
2935
2936 /* Return 1 so that gdb sees the "quick" functions. However,
2937 these functions will be no-ops because we will have expanded
2938 all symtabs. */
2939 return 1;
2940 }
2941
2942 if (dwarf2_read_index (objfile))
2943 return 1;
2944
9291a0cd
TT
2945 return 0;
2946}
2947
2948\f
2949
dce234bc
PP
2950/* Build a partial symbol table. */
2951
2952void
f29dff0a 2953dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2954{
f29dff0a 2955 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2956 {
2957 init_psymbol_list (objfile, 1024);
2958 }
2959
d146bf1e 2960 dwarf2_build_psymtabs_hard (objfile);
c906108c 2961}
c906108c 2962
45452591
DE
2963/* Return TRUE if OFFSET is within CU_HEADER. */
2964
2965static inline int
2966offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2967{
2968 unsigned int bottom = cu_header->offset;
2969 unsigned int top = (cu_header->offset
2970 + cu_header->length
2971 + cu_header->initial_length_size);
9a619af0 2972
45452591
DE
2973 return (offset >= bottom && offset < top);
2974}
2975
93311388
DE
2976/* Read in the comp unit header information from the debug_info at info_ptr.
2977 NOTE: This leaves members offset, first_die_offset to be filled in
2978 by the caller. */
107d2387 2979
fe1b8b76 2980static gdb_byte *
107d2387 2981read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2982 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2983{
2984 int signed_addr;
891d2f0b 2985 unsigned int bytes_read;
c764a876
DE
2986
2987 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2988 cu_header->initial_length_size = bytes_read;
2989 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2990 info_ptr += bytes_read;
107d2387
AC
2991 cu_header->version = read_2_bytes (abfd, info_ptr);
2992 info_ptr += 2;
613e1657 2993 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2994 &bytes_read);
613e1657 2995 info_ptr += bytes_read;
107d2387
AC
2996 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2997 info_ptr += 1;
2998 signed_addr = bfd_get_sign_extend_vma (abfd);
2999 if (signed_addr < 0)
8e65ff28 3000 internal_error (__FILE__, __LINE__,
e2e0b3e5 3001 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3002 cu_header->signed_addr_p = signed_addr;
c764a876 3003
107d2387
AC
3004 return info_ptr;
3005}
3006
adabb602
DE
3007/* Read in a CU header and perform some basic error checking. */
3008
fe1b8b76
JB
3009static gdb_byte *
3010partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 3011 gdb_byte *buffer, unsigned int buffer_size,
b0df02fd 3012 bfd *abfd, int is_debug_types_section)
72bf9492 3013{
fe1b8b76 3014 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 3015
adabb602
DE
3016 header->offset = beg_of_comp_unit - buffer;
3017
72bf9492
DJ
3018 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3019
460c1c54
CC
3020 /* If we're reading a type unit, skip over the signature and
3021 type_offset fields. */
b0df02fd 3022 if (is_debug_types_section)
460c1c54
CC
3023 info_ptr += 8 /*signature*/ + header->offset_size;
3024
adabb602
DE
3025 header->first_die_offset = info_ptr - beg_of_comp_unit;
3026
2dc7f7b3 3027 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 3028 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
3029 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3030 bfd_get_filename (abfd));
72bf9492 3031
9e0ac564
TT
3032 if (header->abbrev_offset
3033 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3034 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
3035 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3036 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 3037 (long) header->abbrev_offset,
93311388 3038 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3039 bfd_get_filename (abfd));
3040
3041 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 3042 > buffer + buffer_size)
8a3fe4f8
AC
3043 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3044 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3045 (long) header->length,
93311388 3046 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3047 bfd_get_filename (abfd));
3048
3049 return info_ptr;
3050}
3051
348e048f
DE
3052/* Read in the types comp unit header information from .debug_types entry at
3053 types_ptr. The result is a pointer to one past the end of the header. */
3054
3055static gdb_byte *
3056read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3057 struct dwarf2_section_info *section,
348e048f
DE
3058 ULONGEST *signature,
3059 gdb_byte *types_ptr, bfd *abfd)
3060{
348e048f
DE
3061 gdb_byte *initial_types_ptr = types_ptr;
3062
8b70b953
TT
3063 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3064 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3065
3066 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3067
3068 *signature = read_8_bytes (abfd, types_ptr);
3069 types_ptr += 8;
3070 types_ptr += cu_header->offset_size;
3071 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3072
3073 return types_ptr;
3074}
3075
aaa75496
JB
3076/* Allocate a new partial symtab for file named NAME and mark this new
3077 partial symtab as being an include of PST. */
3078
3079static void
3080dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3081 struct objfile *objfile)
3082{
3083 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3084
3085 subpst->section_offsets = pst->section_offsets;
3086 subpst->textlow = 0;
3087 subpst->texthigh = 0;
3088
3089 subpst->dependencies = (struct partial_symtab **)
3090 obstack_alloc (&objfile->objfile_obstack,
3091 sizeof (struct partial_symtab *));
3092 subpst->dependencies[0] = pst;
3093 subpst->number_of_dependencies = 1;
3094
3095 subpst->globals_offset = 0;
3096 subpst->n_global_syms = 0;
3097 subpst->statics_offset = 0;
3098 subpst->n_static_syms = 0;
3099 subpst->symtab = NULL;
3100 subpst->read_symtab = pst->read_symtab;
3101 subpst->readin = 0;
3102
3103 /* No private part is necessary for include psymtabs. This property
3104 can be used to differentiate between such include psymtabs and
10b3939b 3105 the regular ones. */
58a9656e 3106 subpst->read_symtab_private = NULL;
aaa75496
JB
3107}
3108
3109/* Read the Line Number Program data and extract the list of files
3110 included by the source file represented by PST. Build an include
d85a05f0 3111 partial symtab for each of these included files. */
aaa75496
JB
3112
3113static void
3114dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3115 struct die_info *die,
aaa75496
JB
3116 struct partial_symtab *pst)
3117{
3118 struct objfile *objfile = cu->objfile;
3119 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3120 struct line_header *lh = NULL;
3121 struct attribute *attr;
aaa75496 3122
d85a05f0
DJ
3123 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3124 if (attr)
3125 {
3126 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3127
d85a05f0
DJ
3128 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3129 }
aaa75496
JB
3130 if (lh == NULL)
3131 return; /* No linetable, so no includes. */
3132
c6da4cef
DE
3133 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3134 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3135
3136 free_line_header (lh);
3137}
3138
348e048f
DE
3139static hashval_t
3140hash_type_signature (const void *item)
3141{
3142 const struct signatured_type *type_sig = item;
9a619af0 3143
348e048f
DE
3144 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3145 return type_sig->signature;
3146}
3147
3148static int
3149eq_type_signature (const void *item_lhs, const void *item_rhs)
3150{
3151 const struct signatured_type *lhs = item_lhs;
3152 const struct signatured_type *rhs = item_rhs;
9a619af0 3153
348e048f
DE
3154 return lhs->signature == rhs->signature;
3155}
3156
1fd400ff
TT
3157/* Allocate a hash table for signatured types. */
3158
3159static htab_t
673bfd45 3160allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3161{
3162 return htab_create_alloc_ex (41,
3163 hash_type_signature,
3164 eq_type_signature,
3165 NULL,
3166 &objfile->objfile_obstack,
3167 hashtab_obstack_allocate,
3168 dummy_obstack_deallocate);
3169}
3170
3171/* A helper function to add a signatured type CU to a list. */
3172
3173static int
3174add_signatured_type_cu_to_list (void **slot, void *datum)
3175{
3176 struct signatured_type *sigt = *slot;
3177 struct dwarf2_per_cu_data ***datap = datum;
3178
3179 **datap = &sigt->per_cu;
3180 ++*datap;
3181
3182 return 1;
3183}
3184
348e048f
DE
3185/* Create the hash table of all entries in the .debug_types section.
3186 The result is zero if there is an error (e.g. missing .debug_types section),
3187 otherwise non-zero. */
3188
3189static int
3190create_debug_types_hash_table (struct objfile *objfile)
3191{
8b70b953 3192 htab_t types_htab = NULL;
1fd400ff 3193 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3194 int ix;
3195 struct dwarf2_section_info *section;
348e048f 3196
8b70b953 3197 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3198 {
3199 dwarf2_per_objfile->signatured_types = NULL;
3200 return 0;
3201 }
3202
8b70b953
TT
3203 for (ix = 0;
3204 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3205 ix, section);
3206 ++ix)
3207 {
3208 gdb_byte *info_ptr, *end_ptr;
348e048f 3209
8b70b953
TT
3210 dwarf2_read_section (objfile, section);
3211 info_ptr = section->buffer;
348e048f 3212
8b70b953
TT
3213 if (info_ptr == NULL)
3214 continue;
348e048f 3215
8b70b953
TT
3216 if (types_htab == NULL)
3217 types_htab = allocate_signatured_type_table (objfile);
348e048f 3218
8b70b953
TT
3219 if (dwarf2_die_debug)
3220 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3221
3222 end_ptr = info_ptr + section->size;
3223 while (info_ptr < end_ptr)
3224 {
3225 unsigned int offset;
3226 unsigned int offset_size;
3227 unsigned int type_offset;
3228 unsigned int length, initial_length_size;
3229 unsigned short version;
3230 ULONGEST signature;
3231 struct signatured_type *type_sig;
3232 void **slot;
3233 gdb_byte *ptr = info_ptr;
348e048f 3234
8b70b953 3235 offset = ptr - section->buffer;
348e048f 3236
8b70b953
TT
3237 /* We need to read the type's signature in order to build the hash
3238 table, but we don't need to read anything else just yet. */
348e048f 3239
8b70b953
TT
3240 /* Sanity check to ensure entire cu is present. */
3241 length = read_initial_length (objfile->obfd, ptr,
3242 &initial_length_size);
3243 if (ptr + length + initial_length_size > end_ptr)
3244 {
3245 complaint (&symfile_complaints,
3246 _("debug type entry runs off end "
3247 "of `.debug_types' section, ignored"));
3248 break;
3249 }
348e048f 3250
8b70b953
TT
3251 offset_size = initial_length_size == 4 ? 4 : 8;
3252 ptr += initial_length_size;
3253 version = bfd_get_16 (objfile->obfd, ptr);
3254 ptr += 2;
3255 ptr += offset_size; /* abbrev offset */
3256 ptr += 1; /* address size */
3257 signature = bfd_get_64 (objfile->obfd, ptr);
3258 ptr += 8;
3259 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3260 ptr += offset_size;
6caca83c
CC
3261
3262 /* Skip dummy type units. */
3263 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3264 {
3265 info_ptr = info_ptr + initial_length_size + length;
3266 continue;
3267 }
8b70b953
TT
3268
3269 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3270 memset (type_sig, 0, sizeof (*type_sig));
3271 type_sig->signature = signature;
3272 type_sig->type_offset = type_offset;
3273 type_sig->per_cu.objfile = objfile;
b0df02fd 3274 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3275 type_sig->per_cu.offset = offset;
3276
3277 slot = htab_find_slot (types_htab, type_sig, INSERT);
3278 gdb_assert (slot != NULL);
3279 if (*slot != NULL)
3280 {
3281 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3282
8b70b953
TT
3283 complaint (&symfile_complaints,
3284 _("debug type entry at offset 0x%x is duplicate to the "
3285 "entry at offset 0x%x, signature 0x%s"),
3286 offset, dup_sig->per_cu.offset,
3287 phex (signature, sizeof (signature)));
3288 gdb_assert (signature == dup_sig->signature);
3289 }
3290 *slot = type_sig;
348e048f 3291
8b70b953
TT
3292 if (dwarf2_die_debug)
3293 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3294 offset, phex (signature, sizeof (signature)));
348e048f 3295
8b70b953
TT
3296 info_ptr = info_ptr + initial_length_size + length;
3297 }
348e048f
DE
3298 }
3299
3300 dwarf2_per_objfile->signatured_types = types_htab;
3301
1fd400ff
TT
3302 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3303 dwarf2_per_objfile->type_comp_units
3304 = obstack_alloc (&objfile->objfile_obstack,
3305 dwarf2_per_objfile->n_type_comp_units
3306 * sizeof (struct dwarf2_per_cu_data *));
3307 iter = &dwarf2_per_objfile->type_comp_units[0];
3308 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3309 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3310 == dwarf2_per_objfile->n_type_comp_units);
3311
348e048f
DE
3312 return 1;
3313}
3314
3315/* Lookup a signature based type.
3316 Returns NULL if SIG is not present in the table. */
3317
3318static struct signatured_type *
3319lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3320{
3321 struct signatured_type find_entry, *entry;
3322
3323 if (dwarf2_per_objfile->signatured_types == NULL)
3324 {
3325 complaint (&symfile_complaints,
55f1336d 3326 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3327 return 0;
3328 }
3329
3330 find_entry.signature = sig;
3331 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3332 return entry;
3333}
3334
d85a05f0
DJ
3335/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3336
3337static void
3338init_cu_die_reader (struct die_reader_specs *reader,
3339 struct dwarf2_cu *cu)
3340{
3341 reader->abfd = cu->objfile->obfd;
3342 reader->cu = cu;
b0df02fd 3343 if (cu->per_cu->debug_types_section)
be391dca 3344 {
b0df02fd
DE
3345 gdb_assert (cu->per_cu->debug_types_section->readin);
3346 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3347 }
d85a05f0 3348 else
be391dca
TT
3349 {
3350 gdb_assert (dwarf2_per_objfile->info.readin);
3351 reader->buffer = dwarf2_per_objfile->info.buffer;
3352 }
d85a05f0
DJ
3353}
3354
3355/* Find the base address of the compilation unit for range lists and
3356 location lists. It will normally be specified by DW_AT_low_pc.
3357 In DWARF-3 draft 4, the base address could be overridden by
3358 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3359 compilation units with discontinuous ranges. */
3360
3361static void
3362dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3363{
3364 struct attribute *attr;
3365
3366 cu->base_known = 0;
3367 cu->base_address = 0;
3368
3369 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3370 if (attr)
3371 {
3372 cu->base_address = DW_ADDR (attr);
3373 cu->base_known = 1;
3374 }
3375 else
3376 {
3377 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3378 if (attr)
3379 {
3380 cu->base_address = DW_ADDR (attr);
3381 cu->base_known = 1;
3382 }
3383 }
3384}
3385
348e048f
DE
3386/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3387 to combine the common parts.
93311388 3388 Process a compilation unit for a psymtab.
348e048f
DE
3389 BUFFER is a pointer to the beginning of the dwarf section buffer,
3390 either .debug_info or debug_types.
93311388
DE
3391 INFO_PTR is a pointer to the start of the CU.
3392 Returns a pointer to the next CU. */
aaa75496 3393
93311388 3394static gdb_byte *
a0f42c21 3395process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
93311388
DE
3396 gdb_byte *buffer, gdb_byte *info_ptr,
3397 unsigned int buffer_size)
c906108c 3398{
a0f42c21 3399 struct objfile *objfile = this_cu->objfile;
c906108c 3400 bfd *abfd = objfile->obfd;
93311388 3401 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3402 struct die_info *comp_unit_die;
c906108c 3403 struct partial_symtab *pst;
5734ee8b 3404 CORE_ADDR baseaddr;
93311388
DE
3405 struct cleanup *back_to_inner;
3406 struct dwarf2_cu cu;
d85a05f0
DJ
3407 int has_children, has_pc_info;
3408 struct attribute *attr;
d85a05f0
DJ
3409 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3410 struct die_reader_specs reader_specs;
3e2a0cee 3411 const char *filename;
c906108c 3412
9816fde3 3413 init_one_comp_unit (&cu, objfile);
93311388 3414 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3415
93311388
DE
3416 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3417 buffer, buffer_size,
460c1c54 3418 abfd,
b0df02fd 3419 this_cu->debug_types_section != NULL);
10b3939b 3420
6caca83c
CC
3421 /* Skip dummy compilation units. */
3422 if (info_ptr >= buffer + buffer_size
3423 || peek_abbrev_code (abfd, info_ptr) == 0)
3424 {
3425 info_ptr = (beg_of_comp_unit + cu.header.length
3426 + cu.header.initial_length_size);
3427 do_cleanups (back_to_inner);
3428 return info_ptr;
3429 }
3430
93311388 3431 cu.list_in_scope = &file_symbols;
af703f96 3432
328c9494
DJ
3433 /* If this compilation unit was already read in, free the
3434 cached copy in order to read it in again. This is
3435 necessary because we skipped some symbols when we first
3436 read in the compilation unit (see load_partial_dies).
3437 This problem could be avoided, but the benefit is
3438 unclear. */
3439 if (this_cu->cu != NULL)
3440 free_one_cached_comp_unit (this_cu->cu);
3441
3442 /* Note that this is a pointer to our stack frame, being
3443 added to a global data structure. It will be cleaned up
3444 in free_stack_comp_unit when we finish with this
3445 compilation unit. */
3446 this_cu->cu = &cu;
d85a05f0
DJ
3447 cu.per_cu = this_cu;
3448
93311388
DE
3449 /* Read the abbrevs for this compilation unit into a table. */
3450 dwarf2_read_abbrevs (abfd, &cu);
3451 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3452
93311388 3453 /* Read the compilation unit die. */
d85a05f0
DJ
3454 init_cu_die_reader (&reader_specs, &cu);
3455 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3456 &has_children);
93311388 3457
b0df02fd 3458 if (this_cu->debug_types_section)
348e048f 3459 {
b3c8eb43
JK
3460 /* LENGTH has not been set yet for type units. */
3461 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3462 this_cu->length = cu.header.length + cu.header.initial_length_size;
3463 }
d85a05f0 3464 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3465 {
93311388
DE
3466 info_ptr = (beg_of_comp_unit + cu.header.length
3467 + cu.header.initial_length_size);
3468 do_cleanups (back_to_inner);
3469 return info_ptr;
3470 }
72bf9492 3471
9816fde3 3472 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3473
93311388 3474 /* Allocate a new partial symbol table structure. */
d85a05f0 3475 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3476 if (attr == NULL || !DW_STRING (attr))
3477 filename = "";
3478 else
3479 filename = DW_STRING (attr);
93311388 3480 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3481 filename,
93311388
DE
3482 /* TEXTLOW and TEXTHIGH are set below. */
3483 0,
3484 objfile->global_psymbols.next,
3485 objfile->static_psymbols.next);
9750bca9 3486 pst->psymtabs_addrmap_supported = 1;
72bf9492 3487
d85a05f0
DJ
3488 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3489 if (attr != NULL)
3490 pst->dirname = DW_STRING (attr);
72bf9492 3491
e38df1d0 3492 pst->read_symtab_private = this_cu;
72bf9492 3493
93311388 3494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3495
0963b4bd 3496 /* Store the function that reads in the rest of the symbol table. */
93311388 3497 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3498
9291a0cd 3499 this_cu->v.psymtab = pst;
c906108c 3500
d85a05f0
DJ
3501 dwarf2_find_base_address (comp_unit_die, &cu);
3502
93311388
DE
3503 /* Possibly set the default values of LOWPC and HIGHPC from
3504 `DW_AT_ranges'. */
d85a05f0
DJ
3505 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3506 &best_highpc, &cu, pst);
3507 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3508 /* Store the contiguous range if it is not empty; it can be empty for
3509 CUs with no code. */
3510 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3511 best_lowpc + baseaddr,
3512 best_highpc + baseaddr - 1, pst);
93311388
DE
3513
3514 /* Check if comp unit has_children.
3515 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3516 If not, there's no more debug_info for this comp unit. */
d85a05f0 3517 if (has_children)
93311388
DE
3518 {
3519 struct partial_die_info *first_die;
3520 CORE_ADDR lowpc, highpc;
31ffec48 3521
93311388
DE
3522 lowpc = ((CORE_ADDR) -1);
3523 highpc = ((CORE_ADDR) 0);
c906108c 3524
93311388 3525 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3526
93311388 3527 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3528 ! has_pc_info, &cu);
57c22c6c 3529
93311388
DE
3530 /* If we didn't find a lowpc, set it to highpc to avoid
3531 complaints from `maint check'. */
3532 if (lowpc == ((CORE_ADDR) -1))
3533 lowpc = highpc;
10b3939b 3534
93311388
DE
3535 /* If the compilation unit didn't have an explicit address range,
3536 then use the information extracted from its child dies. */
d85a05f0 3537 if (! has_pc_info)
93311388 3538 {
d85a05f0
DJ
3539 best_lowpc = lowpc;
3540 best_highpc = highpc;
93311388
DE
3541 }
3542 }
d85a05f0
DJ
3543 pst->textlow = best_lowpc + baseaddr;
3544 pst->texthigh = best_highpc + baseaddr;
c906108c 3545
93311388
DE
3546 pst->n_global_syms = objfile->global_psymbols.next -
3547 (objfile->global_psymbols.list + pst->globals_offset);
3548 pst->n_static_syms = objfile->static_psymbols.next -
3549 (objfile->static_psymbols.list + pst->statics_offset);
3550 sort_pst_symbols (pst);
c906108c 3551
93311388
DE
3552 info_ptr = (beg_of_comp_unit + cu.header.length
3553 + cu.header.initial_length_size);
ae038cb0 3554
b0df02fd 3555 if (this_cu->debug_types_section)
348e048f
DE
3556 {
3557 /* It's not clear we want to do anything with stmt lists here.
3558 Waiting to see what gcc ultimately does. */
3559 }
d85a05f0 3560 else
93311388
DE
3561 {
3562 /* Get the list of files included in the current compilation unit,
3563 and build a psymtab for each of them. */
d85a05f0 3564 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3565 }
ae038cb0 3566
93311388 3567 do_cleanups (back_to_inner);
ae038cb0 3568
93311388
DE
3569 return info_ptr;
3570}
ff013f42 3571
348e048f
DE
3572/* Traversal function for htab_traverse_noresize.
3573 Process one .debug_types comp-unit. */
3574
3575static int
3576process_type_comp_unit (void **slot, void *info)
3577{
3578 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3579 struct dwarf2_per_cu_data *this_cu;
3580
a0f42c21 3581 gdb_assert (info == NULL);
348e048f 3582 this_cu = &entry->per_cu;
348e048f 3583
b0df02fd 3584 gdb_assert (this_cu->debug_types_section->readin);
a0f42c21 3585 process_psymtab_comp_unit (this_cu,
b0df02fd
DE
3586 this_cu->debug_types_section->buffer,
3587 (this_cu->debug_types_section->buffer
8b70b953 3588 + this_cu->offset),
b0df02fd 3589 this_cu->debug_types_section->size);
348e048f
DE
3590
3591 return 1;
3592}
3593
3594/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3595 Build partial symbol tables for the .debug_types comp-units. */
3596
3597static void
3598build_type_psymtabs (struct objfile *objfile)
3599{
3600 if (! create_debug_types_hash_table (objfile))
3601 return;
3602
3603 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3604 process_type_comp_unit, NULL);
348e048f
DE
3605}
3606
60606b2c
TT
3607/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3608
3609static void
3610psymtabs_addrmap_cleanup (void *o)
3611{
3612 struct objfile *objfile = o;
ec61707d 3613
60606b2c
TT
3614 objfile->psymtabs_addrmap = NULL;
3615}
3616
93311388
DE
3617/* Build the partial symbol table by doing a quick pass through the
3618 .debug_info and .debug_abbrev sections. */
72bf9492 3619
93311388 3620static void
c67a9c90 3621dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3622{
93311388 3623 gdb_byte *info_ptr;
60606b2c
TT
3624 struct cleanup *back_to, *addrmap_cleanup;
3625 struct obstack temp_obstack;
93311388 3626
98bfdba5
PA
3627 dwarf2_per_objfile->reading_partial_symbols = 1;
3628
be391dca 3629 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3630 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3631
93311388
DE
3632 /* Any cached compilation units will be linked by the per-objfile
3633 read_in_chain. Make sure to free them when we're done. */
3634 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3635
348e048f
DE
3636 build_type_psymtabs (objfile);
3637
93311388 3638 create_all_comp_units (objfile);
c906108c 3639
60606b2c
TT
3640 /* Create a temporary address map on a temporary obstack. We later
3641 copy this to the final obstack. */
3642 obstack_init (&temp_obstack);
3643 make_cleanup_obstack_free (&temp_obstack);
3644 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3645 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3646
93311388
DE
3647 /* Since the objects we're extracting from .debug_info vary in
3648 length, only the individual functions to extract them (like
3649 read_comp_unit_head and load_partial_die) can really know whether
3650 the buffer is large enough to hold another complete object.
c906108c 3651
93311388
DE
3652 At the moment, they don't actually check that. If .debug_info
3653 holds just one extra byte after the last compilation unit's dies,
3654 then read_comp_unit_head will happily read off the end of the
3655 buffer. read_partial_die is similarly casual. Those functions
3656 should be fixed.
c906108c 3657
93311388
DE
3658 For this loop condition, simply checking whether there's any data
3659 left at all should be sufficient. */
c906108c 3660
93311388
DE
3661 while (info_ptr < (dwarf2_per_objfile->info.buffer
3662 + dwarf2_per_objfile->info.size))
3663 {
3664 struct dwarf2_per_cu_data *this_cu;
dd373385 3665
3e43a32a
MS
3666 this_cu = dwarf2_find_comp_unit (info_ptr
3667 - dwarf2_per_objfile->info.buffer,
93311388 3668 objfile);
aaa75496 3669
a0f42c21 3670 info_ptr = process_psymtab_comp_unit (this_cu,
93311388
DE
3671 dwarf2_per_objfile->info.buffer,
3672 info_ptr,
3673 dwarf2_per_objfile->info.size);
c906108c 3674 }
ff013f42
JK
3675
3676 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3677 &objfile->objfile_obstack);
60606b2c 3678 discard_cleanups (addrmap_cleanup);
ff013f42 3679
ae038cb0
DJ
3680 do_cleanups (back_to);
3681}
3682
93311388 3683/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3684
3685static void
a0f42c21 3686load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3687{
a0f42c21 3688 struct objfile *objfile = this_cu->objfile;
ae038cb0 3689 bfd *abfd = objfile->obfd;
adabb602 3690 gdb_byte *info_ptr;
d85a05f0 3691 struct die_info *comp_unit_die;
ae038cb0 3692 struct dwarf2_cu *cu;
1d9ec526 3693 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3694 int has_children;
3695 struct die_reader_specs reader_specs;
98bfdba5 3696 int read_cu = 0;
ae038cb0 3697
b0df02fd 3698 gdb_assert (! this_cu->debug_types_section);
348e048f 3699
be391dca 3700 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3701 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3702
98bfdba5
PA
3703 if (this_cu->cu == NULL)
3704 {
9816fde3
JK
3705 cu = xmalloc (sizeof (*cu));
3706 init_one_comp_unit (cu, objfile);
ae038cb0 3707
98bfdba5 3708 read_cu = 1;
ae038cb0 3709
98bfdba5 3710 /* If an error occurs while loading, release our storage. */
68dc6402 3711 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3712
98bfdba5
PA
3713 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3714 dwarf2_per_objfile->info.buffer,
3715 dwarf2_per_objfile->info.size,
460c1c54 3716 abfd, 0);
ae038cb0 3717
6caca83c
CC
3718 /* Skip dummy compilation units. */
3719 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3720 + dwarf2_per_objfile->info.size)
3721 || peek_abbrev_code (abfd, info_ptr) == 0)
3722 {
3723 do_cleanups (free_cu_cleanup);
3724 return;
3725 }
3726
98bfdba5
PA
3727 /* Link this compilation unit into the compilation unit tree. */
3728 this_cu->cu = cu;
3729 cu->per_cu = this_cu;
98bfdba5
PA
3730
3731 /* Link this CU into read_in_chain. */
3732 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3733 dwarf2_per_objfile->read_in_chain = this_cu;
3734 }
3735 else
3736 {
3737 cu = this_cu->cu;
3738 info_ptr += cu->header.first_die_offset;
3739 }
ae038cb0
DJ
3740
3741 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3742 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3743 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3744 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3745
3746 /* Read the compilation unit die. */
d85a05f0
DJ
3747 init_cu_die_reader (&reader_specs, cu);
3748 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3749 &has_children);
ae038cb0 3750
9816fde3 3751 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3752
ae038cb0
DJ
3753 /* Check if comp unit has_children.
3754 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3755 If not, there's no more debug_info for this comp unit. */
d85a05f0 3756 if (has_children)
93311388 3757 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3758
98bfdba5
PA
3759 do_cleanups (free_abbrevs_cleanup);
3760
3761 if (read_cu)
3762 {
3763 /* We've successfully allocated this compilation unit. Let our
3764 caller clean it up when finished with it. */
3765 discard_cleanups (free_cu_cleanup);
3766 }
ae038cb0
DJ
3767}
3768
9cdd5dbd
DE
3769/* Create a list of all compilation units in OBJFILE.
3770 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3771
3772static void
3773create_all_comp_units (struct objfile *objfile)
3774{
3775 int n_allocated;
3776 int n_comp_units;
3777 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3778 gdb_byte *info_ptr;
3779
3780 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3781 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3782
3783 n_comp_units = 0;
3784 n_allocated = 10;
3785 all_comp_units = xmalloc (n_allocated
3786 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3787
3e43a32a
MS
3788 while (info_ptr < dwarf2_per_objfile->info.buffer
3789 + dwarf2_per_objfile->info.size)
ae038cb0 3790 {
c764a876 3791 unsigned int length, initial_length_size;
ae038cb0 3792 struct dwarf2_per_cu_data *this_cu;
c764a876 3793 unsigned int offset;
ae038cb0 3794
dce234bc 3795 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3796
3797 /* Read just enough information to find out where the next
3798 compilation unit is. */
c764a876
DE
3799 length = read_initial_length (objfile->obfd, info_ptr,
3800 &initial_length_size);
ae038cb0
DJ
3801
3802 /* Save the compilation unit for later lookup. */
3803 this_cu = obstack_alloc (&objfile->objfile_obstack,
3804 sizeof (struct dwarf2_per_cu_data));
3805 memset (this_cu, 0, sizeof (*this_cu));
3806 this_cu->offset = offset;
c764a876 3807 this_cu->length = length + initial_length_size;
9291a0cd 3808 this_cu->objfile = objfile;
ae038cb0
DJ
3809
3810 if (n_comp_units == n_allocated)
3811 {
3812 n_allocated *= 2;
3813 all_comp_units = xrealloc (all_comp_units,
3814 n_allocated
3815 * sizeof (struct dwarf2_per_cu_data *));
3816 }
3817 all_comp_units[n_comp_units++] = this_cu;
3818
3819 info_ptr = info_ptr + this_cu->length;
3820 }
3821
3822 dwarf2_per_objfile->all_comp_units
3823 = obstack_alloc (&objfile->objfile_obstack,
3824 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3825 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3826 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3827 xfree (all_comp_units);
3828 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3829}
3830
5734ee8b
DJ
3831/* Process all loaded DIEs for compilation unit CU, starting at
3832 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3833 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3834 DW_AT_ranges). If NEED_PC is set, then this function will set
3835 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3836 and record the covered ranges in the addrmap. */
c906108c 3837
72bf9492
DJ
3838static void
3839scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3840 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3841{
72bf9492 3842 struct partial_die_info *pdi;
c906108c 3843
91c24f0a
DC
3844 /* Now, march along the PDI's, descending into ones which have
3845 interesting children but skipping the children of the other ones,
3846 until we reach the end of the compilation unit. */
c906108c 3847
72bf9492 3848 pdi = first_die;
91c24f0a 3849
72bf9492
DJ
3850 while (pdi != NULL)
3851 {
3852 fixup_partial_die (pdi, cu);
c906108c 3853
f55ee35c 3854 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3855 children, so we need to look at them. Ditto for anonymous
3856 enums. */
933c6fe4 3857
72bf9492 3858 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3859 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3860 {
72bf9492 3861 switch (pdi->tag)
c906108c
SS
3862 {
3863 case DW_TAG_subprogram:
5734ee8b 3864 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3865 break;
72929c62 3866 case DW_TAG_constant:
c906108c
SS
3867 case DW_TAG_variable:
3868 case DW_TAG_typedef:
91c24f0a 3869 case DW_TAG_union_type:
72bf9492 3870 if (!pdi->is_declaration)
63d06c5c 3871 {
72bf9492 3872 add_partial_symbol (pdi, cu);
63d06c5c
DC
3873 }
3874 break;
c906108c 3875 case DW_TAG_class_type:
680b30c7 3876 case DW_TAG_interface_type:
c906108c 3877 case DW_TAG_structure_type:
72bf9492 3878 if (!pdi->is_declaration)
c906108c 3879 {
72bf9492 3880 add_partial_symbol (pdi, cu);
c906108c
SS
3881 }
3882 break;
91c24f0a 3883 case DW_TAG_enumeration_type:
72bf9492
DJ
3884 if (!pdi->is_declaration)
3885 add_partial_enumeration (pdi, cu);
c906108c
SS
3886 break;
3887 case DW_TAG_base_type:
a02abb62 3888 case DW_TAG_subrange_type:
c906108c 3889 /* File scope base type definitions are added to the partial
c5aa993b 3890 symbol table. */
72bf9492 3891 add_partial_symbol (pdi, cu);
c906108c 3892 break;
d9fa45fe 3893 case DW_TAG_namespace:
5734ee8b 3894 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3895 break;
5d7cb8df
JK
3896 case DW_TAG_module:
3897 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3898 break;
c906108c
SS
3899 default:
3900 break;
3901 }
3902 }
3903
72bf9492
DJ
3904 /* If the die has a sibling, skip to the sibling. */
3905
3906 pdi = pdi->die_sibling;
3907 }
3908}
3909
3910/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3911
72bf9492 3912 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3913 name is concatenated with "::" and the partial DIE's name. For
3914 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3915 Enumerators are an exception; they use the scope of their parent
3916 enumeration type, i.e. the name of the enumeration type is not
3917 prepended to the enumerator.
91c24f0a 3918
72bf9492
DJ
3919 There are two complexities. One is DW_AT_specification; in this
3920 case "parent" means the parent of the target of the specification,
3921 instead of the direct parent of the DIE. The other is compilers
3922 which do not emit DW_TAG_namespace; in this case we try to guess
3923 the fully qualified name of structure types from their members'
3924 linkage names. This must be done using the DIE's children rather
3925 than the children of any DW_AT_specification target. We only need
3926 to do this for structures at the top level, i.e. if the target of
3927 any DW_AT_specification (if any; otherwise the DIE itself) does not
3928 have a parent. */
3929
3930/* Compute the scope prefix associated with PDI's parent, in
3931 compilation unit CU. The result will be allocated on CU's
3932 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3933 field. NULL is returned if no prefix is necessary. */
3934static char *
3935partial_die_parent_scope (struct partial_die_info *pdi,
3936 struct dwarf2_cu *cu)
3937{
3938 char *grandparent_scope;
3939 struct partial_die_info *parent, *real_pdi;
91c24f0a 3940
72bf9492
DJ
3941 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3942 then this means the parent of the specification DIE. */
3943
3944 real_pdi = pdi;
72bf9492 3945 while (real_pdi->has_specification)
10b3939b 3946 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3947
3948 parent = real_pdi->die_parent;
3949 if (parent == NULL)
3950 return NULL;
3951
3952 if (parent->scope_set)
3953 return parent->scope;
3954
3955 fixup_partial_die (parent, cu);
3956
10b3939b 3957 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3958
acebe513
UW
3959 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3960 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3961 Work around this problem here. */
3962 if (cu->language == language_cplus
6e70227d 3963 && parent->tag == DW_TAG_namespace
acebe513
UW
3964 && strcmp (parent->name, "::") == 0
3965 && grandparent_scope == NULL)
3966 {
3967 parent->scope = NULL;
3968 parent->scope_set = 1;
3969 return NULL;
3970 }
3971
9c6c53f7
SA
3972 if (pdi->tag == DW_TAG_enumerator)
3973 /* Enumerators should not get the name of the enumeration as a prefix. */
3974 parent->scope = grandparent_scope;
3975 else if (parent->tag == DW_TAG_namespace
f55ee35c 3976 || parent->tag == DW_TAG_module
72bf9492
DJ
3977 || parent->tag == DW_TAG_structure_type
3978 || parent->tag == DW_TAG_class_type
680b30c7 3979 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3980 || parent->tag == DW_TAG_union_type
3981 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3982 {
3983 if (grandparent_scope == NULL)
3984 parent->scope = parent->name;
3985 else
3e43a32a
MS
3986 parent->scope = typename_concat (&cu->comp_unit_obstack,
3987 grandparent_scope,
f55ee35c 3988 parent->name, 0, cu);
72bf9492 3989 }
72bf9492
DJ
3990 else
3991 {
3992 /* FIXME drow/2004-04-01: What should we be doing with
3993 function-local names? For partial symbols, we should probably be
3994 ignoring them. */
3995 complaint (&symfile_complaints,
e2e0b3e5 3996 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3997 parent->tag, pdi->offset);
3998 parent->scope = grandparent_scope;
c906108c
SS
3999 }
4000
72bf9492
DJ
4001 parent->scope_set = 1;
4002 return parent->scope;
4003}
4004
4005/* Return the fully scoped name associated with PDI, from compilation unit
4006 CU. The result will be allocated with malloc. */
4007static char *
4008partial_die_full_name (struct partial_die_info *pdi,
4009 struct dwarf2_cu *cu)
4010{
4011 char *parent_scope;
4012
98bfdba5
PA
4013 /* If this is a template instantiation, we can not work out the
4014 template arguments from partial DIEs. So, unfortunately, we have
4015 to go through the full DIEs. At least any work we do building
4016 types here will be reused if full symbols are loaded later. */
4017 if (pdi->has_template_arguments)
4018 {
4019 fixup_partial_die (pdi, cu);
4020
4021 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4022 {
4023 struct die_info *die;
4024 struct attribute attr;
4025 struct dwarf2_cu *ref_cu = cu;
4026
4027 attr.name = 0;
4028 attr.form = DW_FORM_ref_addr;
4029 attr.u.addr = pdi->offset;
4030 die = follow_die_ref (NULL, &attr, &ref_cu);
4031
4032 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4033 }
4034 }
4035
72bf9492
DJ
4036 parent_scope = partial_die_parent_scope (pdi, cu);
4037 if (parent_scope == NULL)
4038 return NULL;
4039 else
f55ee35c 4040 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4041}
4042
4043static void
72bf9492 4044add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4045{
e7c27a73 4046 struct objfile *objfile = cu->objfile;
c906108c 4047 CORE_ADDR addr = 0;
decbce07 4048 char *actual_name = NULL;
5c4e30ca 4049 const struct partial_symbol *psym = NULL;
e142c38c 4050 CORE_ADDR baseaddr;
72bf9492 4051 int built_actual_name = 0;
e142c38c
DJ
4052
4053 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4054
94af9270
KS
4055 actual_name = partial_die_full_name (pdi, cu);
4056 if (actual_name)
4057 built_actual_name = 1;
63d06c5c 4058
72bf9492
DJ
4059 if (actual_name == NULL)
4060 actual_name = pdi->name;
4061
c906108c
SS
4062 switch (pdi->tag)
4063 {
4064 case DW_TAG_subprogram:
2cfa0c8d 4065 if (pdi->is_external || cu->language == language_ada)
c906108c 4066 {
2cfa0c8d
JB
4067 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4068 of the global scope. But in Ada, we want to be able to access
4069 nested procedures globally. So all Ada subprograms are stored
4070 in the global scope. */
f47fb265 4071 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4072 mst_text, objfile); */
f47fb265
MS
4073 add_psymbol_to_list (actual_name, strlen (actual_name),
4074 built_actual_name,
4075 VAR_DOMAIN, LOC_BLOCK,
4076 &objfile->global_psymbols,
4077 0, pdi->lowpc + baseaddr,
4078 cu->language, objfile);
c906108c
SS
4079 }
4080 else
4081 {
f47fb265 4082 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4083 mst_file_text, objfile); */
f47fb265
MS
4084 add_psymbol_to_list (actual_name, strlen (actual_name),
4085 built_actual_name,
4086 VAR_DOMAIN, LOC_BLOCK,
4087 &objfile->static_psymbols,
4088 0, pdi->lowpc + baseaddr,
4089 cu->language, objfile);
c906108c
SS
4090 }
4091 break;
72929c62
JB
4092 case DW_TAG_constant:
4093 {
4094 struct psymbol_allocation_list *list;
4095
4096 if (pdi->is_external)
4097 list = &objfile->global_psymbols;
4098 else
4099 list = &objfile->static_psymbols;
f47fb265
MS
4100 add_psymbol_to_list (actual_name, strlen (actual_name),
4101 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4102 list, 0, 0, cu->language, objfile);
72929c62
JB
4103 }
4104 break;
c906108c 4105 case DW_TAG_variable:
caac4577
JG
4106 if (pdi->locdesc)
4107 addr = decode_locdesc (pdi->locdesc, cu);
4108
4109 if (pdi->locdesc
4110 && addr == 0
4111 && !dwarf2_per_objfile->has_section_at_zero)
4112 {
4113 /* A global or static variable may also have been stripped
4114 out by the linker if unused, in which case its address
4115 will be nullified; do not add such variables into partial
4116 symbol table then. */
4117 }
4118 else if (pdi->is_external)
c906108c
SS
4119 {
4120 /* Global Variable.
4121 Don't enter into the minimal symbol tables as there is
4122 a minimal symbol table entry from the ELF symbols already.
4123 Enter into partial symbol table if it has a location
4124 descriptor or a type.
4125 If the location descriptor is missing, new_symbol will create
4126 a LOC_UNRESOLVED symbol, the address of the variable will then
4127 be determined from the minimal symbol table whenever the variable
4128 is referenced.
4129 The address for the partial symbol table entry is not
4130 used by GDB, but it comes in handy for debugging partial symbol
4131 table building. */
4132
c906108c 4133 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4134 add_psymbol_to_list (actual_name, strlen (actual_name),
4135 built_actual_name,
4136 VAR_DOMAIN, LOC_STATIC,
4137 &objfile->global_psymbols,
4138 0, addr + baseaddr,
4139 cu->language, objfile);
c906108c
SS
4140 }
4141 else
4142 {
0963b4bd 4143 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4144 if (pdi->locdesc == NULL)
decbce07
MS
4145 {
4146 if (built_actual_name)
4147 xfree (actual_name);
4148 return;
4149 }
f47fb265 4150 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4151 mst_file_data, objfile); */
f47fb265
MS
4152 add_psymbol_to_list (actual_name, strlen (actual_name),
4153 built_actual_name,
4154 VAR_DOMAIN, LOC_STATIC,
4155 &objfile->static_psymbols,
4156 0, addr + baseaddr,
4157 cu->language, objfile);
c906108c
SS
4158 }
4159 break;
4160 case DW_TAG_typedef:
4161 case DW_TAG_base_type:
a02abb62 4162 case DW_TAG_subrange_type:
38d518c9 4163 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4164 built_actual_name,
176620f1 4165 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4166 &objfile->static_psymbols,
e142c38c 4167 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4168 break;
72bf9492
DJ
4169 case DW_TAG_namespace:
4170 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4171 built_actual_name,
72bf9492
DJ
4172 VAR_DOMAIN, LOC_TYPEDEF,
4173 &objfile->global_psymbols,
4174 0, (CORE_ADDR) 0, cu->language, objfile);
4175 break;
c906108c 4176 case DW_TAG_class_type:
680b30c7 4177 case DW_TAG_interface_type:
c906108c
SS
4178 case DW_TAG_structure_type:
4179 case DW_TAG_union_type:
4180 case DW_TAG_enumeration_type:
fa4028e9
JB
4181 /* Skip external references. The DWARF standard says in the section
4182 about "Structure, Union, and Class Type Entries": "An incomplete
4183 structure, union or class type is represented by a structure,
4184 union or class entry that does not have a byte size attribute
4185 and that has a DW_AT_declaration attribute." */
4186 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4187 {
4188 if (built_actual_name)
4189 xfree (actual_name);
4190 return;
4191 }
fa4028e9 4192
63d06c5c
DC
4193 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4194 static vs. global. */
38d518c9 4195 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4196 built_actual_name,
176620f1 4197 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4198 (cu->language == language_cplus
4199 || cu->language == language_java)
63d06c5c
DC
4200 ? &objfile->global_psymbols
4201 : &objfile->static_psymbols,
e142c38c 4202 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4203
c906108c
SS
4204 break;
4205 case DW_TAG_enumerator:
38d518c9 4206 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4207 built_actual_name,
176620f1 4208 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4209 (cu->language == language_cplus
4210 || cu->language == language_java)
f6fe98ef
DJ
4211 ? &objfile->global_psymbols
4212 : &objfile->static_psymbols,
e142c38c 4213 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4214 break;
4215 default:
4216 break;
4217 }
5c4e30ca 4218
72bf9492
DJ
4219 if (built_actual_name)
4220 xfree (actual_name);
c906108c
SS
4221}
4222
5c4e30ca
DC
4223/* Read a partial die corresponding to a namespace; also, add a symbol
4224 corresponding to that namespace to the symbol table. NAMESPACE is
4225 the name of the enclosing namespace. */
91c24f0a 4226
72bf9492
DJ
4227static void
4228add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4229 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4230 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4231{
72bf9492 4232 /* Add a symbol for the namespace. */
e7c27a73 4233
72bf9492 4234 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4235
4236 /* Now scan partial symbols in that namespace. */
4237
91c24f0a 4238 if (pdi->has_children)
5734ee8b 4239 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4240}
4241
5d7cb8df
JK
4242/* Read a partial die corresponding to a Fortran module. */
4243
4244static void
4245add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4246 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4247{
f55ee35c 4248 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4249
4250 if (pdi->has_children)
4251 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4252}
4253
bc30ff58
JB
4254/* Read a partial die corresponding to a subprogram and create a partial
4255 symbol for that subprogram. When the CU language allows it, this
4256 routine also defines a partial symbol for each nested subprogram
4257 that this subprogram contains.
6e70227d 4258
bc30ff58
JB
4259 DIE my also be a lexical block, in which case we simply search
4260 recursively for suprograms defined inside that lexical block.
4261 Again, this is only performed when the CU language allows this
4262 type of definitions. */
4263
4264static void
4265add_partial_subprogram (struct partial_die_info *pdi,
4266 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4267 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4268{
4269 if (pdi->tag == DW_TAG_subprogram)
4270 {
4271 if (pdi->has_pc_info)
4272 {
4273 if (pdi->lowpc < *lowpc)
4274 *lowpc = pdi->lowpc;
4275 if (pdi->highpc > *highpc)
4276 *highpc = pdi->highpc;
5734ee8b
DJ
4277 if (need_pc)
4278 {
4279 CORE_ADDR baseaddr;
4280 struct objfile *objfile = cu->objfile;
4281
4282 baseaddr = ANOFFSET (objfile->section_offsets,
4283 SECT_OFF_TEXT (objfile));
4284 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4285 pdi->lowpc + baseaddr,
4286 pdi->highpc - 1 + baseaddr,
9291a0cd 4287 cu->per_cu->v.psymtab);
5734ee8b 4288 }
bc30ff58 4289 if (!pdi->is_declaration)
e8d05480
JB
4290 /* Ignore subprogram DIEs that do not have a name, they are
4291 illegal. Do not emit a complaint at this point, we will
4292 do so when we convert this psymtab into a symtab. */
4293 if (pdi->name)
4294 add_partial_symbol (pdi, cu);
bc30ff58
JB
4295 }
4296 }
6e70227d 4297
bc30ff58
JB
4298 if (! pdi->has_children)
4299 return;
4300
4301 if (cu->language == language_ada)
4302 {
4303 pdi = pdi->die_child;
4304 while (pdi != NULL)
4305 {
4306 fixup_partial_die (pdi, cu);
4307 if (pdi->tag == DW_TAG_subprogram
4308 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4309 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4310 pdi = pdi->die_sibling;
4311 }
4312 }
4313}
4314
91c24f0a
DC
4315/* Read a partial die corresponding to an enumeration type. */
4316
72bf9492
DJ
4317static void
4318add_partial_enumeration (struct partial_die_info *enum_pdi,
4319 struct dwarf2_cu *cu)
91c24f0a 4320{
72bf9492 4321 struct partial_die_info *pdi;
91c24f0a
DC
4322
4323 if (enum_pdi->name != NULL)
72bf9492
DJ
4324 add_partial_symbol (enum_pdi, cu);
4325
4326 pdi = enum_pdi->die_child;
4327 while (pdi)
91c24f0a 4328 {
72bf9492 4329 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4330 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4331 else
72bf9492
DJ
4332 add_partial_symbol (pdi, cu);
4333 pdi = pdi->die_sibling;
91c24f0a 4334 }
91c24f0a
DC
4335}
4336
6caca83c
CC
4337/* Return the initial uleb128 in the die at INFO_PTR. */
4338
4339static unsigned int
4340peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4341{
4342 unsigned int bytes_read;
4343
4344 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4345}
4346
4bb7a0a7
DJ
4347/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4348 Return the corresponding abbrev, or NULL if the number is zero (indicating
4349 an empty DIE). In either case *BYTES_READ will be set to the length of
4350 the initial number. */
4351
4352static struct abbrev_info *
fe1b8b76 4353peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4354 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4355{
4356 bfd *abfd = cu->objfile->obfd;
4357 unsigned int abbrev_number;
4358 struct abbrev_info *abbrev;
4359
4360 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4361
4362 if (abbrev_number == 0)
4363 return NULL;
4364
4365 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4366 if (!abbrev)
4367 {
3e43a32a
MS
4368 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4369 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4370 }
4371
4372 return abbrev;
4373}
4374
93311388
DE
4375/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4376 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4377 DIE. Any children of the skipped DIEs will also be skipped. */
4378
fe1b8b76 4379static gdb_byte *
93311388 4380skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4381{
4382 struct abbrev_info *abbrev;
4383 unsigned int bytes_read;
4384
4385 while (1)
4386 {
4387 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4388 if (abbrev == NULL)
4389 return info_ptr + bytes_read;
4390 else
93311388 4391 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4392 }
4393}
4394
93311388
DE
4395/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4396 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4397 abbrev corresponding to that skipped uleb128 should be passed in
4398 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4399 children. */
4400
fe1b8b76 4401static gdb_byte *
93311388
DE
4402skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4403 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4404{
4405 unsigned int bytes_read;
4406 struct attribute attr;
4407 bfd *abfd = cu->objfile->obfd;
4408 unsigned int form, i;
4409
4410 for (i = 0; i < abbrev->num_attrs; i++)
4411 {
4412 /* The only abbrev we care about is DW_AT_sibling. */
4413 if (abbrev->attrs[i].name == DW_AT_sibling)
4414 {
4415 read_attribute (&attr, &abbrev->attrs[i],
4416 abfd, info_ptr, cu);
4417 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4418 complaint (&symfile_complaints,
4419 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4420 else
93311388 4421 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4422 }
4423
4424 /* If it isn't DW_AT_sibling, skip this attribute. */
4425 form = abbrev->attrs[i].form;
4426 skip_attribute:
4427 switch (form)
4428 {
4bb7a0a7 4429 case DW_FORM_ref_addr:
ae411497
TT
4430 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4431 and later it is offset sized. */
4432 if (cu->header.version == 2)
4433 info_ptr += cu->header.addr_size;
4434 else
4435 info_ptr += cu->header.offset_size;
4436 break;
4437 case DW_FORM_addr:
4bb7a0a7
DJ
4438 info_ptr += cu->header.addr_size;
4439 break;
4440 case DW_FORM_data1:
4441 case DW_FORM_ref1:
4442 case DW_FORM_flag:
4443 info_ptr += 1;
4444 break;
2dc7f7b3
TT
4445 case DW_FORM_flag_present:
4446 break;
4bb7a0a7
DJ
4447 case DW_FORM_data2:
4448 case DW_FORM_ref2:
4449 info_ptr += 2;
4450 break;
4451 case DW_FORM_data4:
4452 case DW_FORM_ref4:
4453 info_ptr += 4;
4454 break;
4455 case DW_FORM_data8:
4456 case DW_FORM_ref8:
55f1336d 4457 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4458 info_ptr += 8;
4459 break;
4460 case DW_FORM_string:
9b1c24c8 4461 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4462 info_ptr += bytes_read;
4463 break;
2dc7f7b3 4464 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4465 case DW_FORM_strp:
4466 info_ptr += cu->header.offset_size;
4467 break;
2dc7f7b3 4468 case DW_FORM_exprloc:
4bb7a0a7
DJ
4469 case DW_FORM_block:
4470 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4471 info_ptr += bytes_read;
4472 break;
4473 case DW_FORM_block1:
4474 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4475 break;
4476 case DW_FORM_block2:
4477 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4478 break;
4479 case DW_FORM_block4:
4480 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4481 break;
4482 case DW_FORM_sdata:
4483 case DW_FORM_udata:
4484 case DW_FORM_ref_udata:
4485 info_ptr = skip_leb128 (abfd, info_ptr);
4486 break;
4487 case DW_FORM_indirect:
4488 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4489 info_ptr += bytes_read;
4490 /* We need to continue parsing from here, so just go back to
4491 the top. */
4492 goto skip_attribute;
4493
4494 default:
3e43a32a
MS
4495 error (_("Dwarf Error: Cannot handle %s "
4496 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4497 dwarf_form_name (form),
4498 bfd_get_filename (abfd));
4499 }
4500 }
4501
4502 if (abbrev->has_children)
93311388 4503 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4504 else
4505 return info_ptr;
4506}
4507
93311388
DE
4508/* Locate ORIG_PDI's sibling.
4509 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4510 in BUFFER. */
91c24f0a 4511
fe1b8b76 4512static gdb_byte *
93311388
DE
4513locate_pdi_sibling (struct partial_die_info *orig_pdi,
4514 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4515 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4516{
4517 /* Do we know the sibling already? */
72bf9492 4518
91c24f0a
DC
4519 if (orig_pdi->sibling)
4520 return orig_pdi->sibling;
4521
4522 /* Are there any children to deal with? */
4523
4524 if (!orig_pdi->has_children)
4525 return info_ptr;
4526
4bb7a0a7 4527 /* Skip the children the long way. */
91c24f0a 4528
93311388 4529 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4530}
4531
c906108c
SS
4532/* Expand this partial symbol table into a full symbol table. */
4533
4534static void
fba45db2 4535dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4536{
c906108c
SS
4537 if (pst != NULL)
4538 {
4539 if (pst->readin)
4540 {
3e43a32a
MS
4541 warning (_("bug: psymtab for %s is already read in."),
4542 pst->filename);
c906108c
SS
4543 }
4544 else
4545 {
4546 if (info_verbose)
4547 {
3e43a32a
MS
4548 printf_filtered (_("Reading in symbols for %s..."),
4549 pst->filename);
c906108c
SS
4550 gdb_flush (gdb_stdout);
4551 }
4552
10b3939b
DJ
4553 /* Restore our global data. */
4554 dwarf2_per_objfile = objfile_data (pst->objfile,
4555 dwarf2_objfile_data_key);
4556
b2ab525c
KB
4557 /* If this psymtab is constructed from a debug-only objfile, the
4558 has_section_at_zero flag will not necessarily be correct. We
4559 can get the correct value for this flag by looking at the data
4560 associated with the (presumably stripped) associated objfile. */
4561 if (pst->objfile->separate_debug_objfile_backlink)
4562 {
4563 struct dwarf2_per_objfile *dpo_backlink
4564 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4565 dwarf2_objfile_data_key);
9a619af0 4566
b2ab525c
KB
4567 dwarf2_per_objfile->has_section_at_zero
4568 = dpo_backlink->has_section_at_zero;
4569 }
4570
98bfdba5
PA
4571 dwarf2_per_objfile->reading_partial_symbols = 0;
4572
c906108c
SS
4573 psymtab_to_symtab_1 (pst);
4574
4575 /* Finish up the debug error message. */
4576 if (info_verbose)
a3f17187 4577 printf_filtered (_("done.\n"));
c906108c
SS
4578 }
4579 }
4580}
9cdd5dbd
DE
4581\f
4582/* Reading in full CUs. */
c906108c 4583
10b3939b
DJ
4584/* Add PER_CU to the queue. */
4585
4586static void
a0f42c21 4587queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4588{
4589 struct dwarf2_queue_item *item;
4590
4591 per_cu->queued = 1;
4592 item = xmalloc (sizeof (*item));
4593 item->per_cu = per_cu;
4594 item->next = NULL;
4595
4596 if (dwarf2_queue == NULL)
4597 dwarf2_queue = item;
4598 else
4599 dwarf2_queue_tail->next = item;
4600
4601 dwarf2_queue_tail = item;
4602}
4603
4604/* Process the queue. */
4605
4606static void
a0f42c21 4607process_queue (void)
10b3939b
DJ
4608{
4609 struct dwarf2_queue_item *item, *next_item;
4610
03dd20cc
DJ
4611 /* The queue starts out with one item, but following a DIE reference
4612 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4613 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4614 {
9291a0cd
TT
4615 if (dwarf2_per_objfile->using_index
4616 ? !item->per_cu->v.quick->symtab
4617 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4618 process_full_comp_unit (item->per_cu);
4619
4620 item->per_cu->queued = 0;
4621 next_item = item->next;
4622 xfree (item);
4623 }
4624
4625 dwarf2_queue_tail = NULL;
4626}
4627
4628/* Free all allocated queue entries. This function only releases anything if
4629 an error was thrown; if the queue was processed then it would have been
4630 freed as we went along. */
4631
4632static void
4633dwarf2_release_queue (void *dummy)
4634{
4635 struct dwarf2_queue_item *item, *last;
4636
4637 item = dwarf2_queue;
4638 while (item)
4639 {
4640 /* Anything still marked queued is likely to be in an
4641 inconsistent state, so discard it. */
4642 if (item->per_cu->queued)
4643 {
4644 if (item->per_cu->cu != NULL)
4645 free_one_cached_comp_unit (item->per_cu->cu);
4646 item->per_cu->queued = 0;
4647 }
4648
4649 last = item;
4650 item = item->next;
4651 xfree (last);
4652 }
4653
4654 dwarf2_queue = dwarf2_queue_tail = NULL;
4655}
4656
4657/* Read in full symbols for PST, and anything it depends on. */
4658
c906108c 4659static void
fba45db2 4660psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4661{
10b3939b 4662 struct dwarf2_per_cu_data *per_cu;
c906108c 4663 struct cleanup *back_to;
aaa75496
JB
4664 int i;
4665
4666 for (i = 0; i < pst->number_of_dependencies; i++)
4667 if (!pst->dependencies[i]->readin)
4668 {
4669 /* Inform about additional files that need to be read in. */
4670 if (info_verbose)
4671 {
a3f17187 4672 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4673 fputs_filtered (" ", gdb_stdout);
4674 wrap_here ("");
4675 fputs_filtered ("and ", gdb_stdout);
4676 wrap_here ("");
4677 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4678 wrap_here (""); /* Flush output. */
aaa75496
JB
4679 gdb_flush (gdb_stdout);
4680 }
4681 psymtab_to_symtab_1 (pst->dependencies[i]);
4682 }
4683
e38df1d0 4684 per_cu = pst->read_symtab_private;
10b3939b
DJ
4685
4686 if (per_cu == NULL)
aaa75496
JB
4687 {
4688 /* It's an include file, no symbols to read for it.
4689 Everything is in the parent symtab. */
4690 pst->readin = 1;
4691 return;
4692 }
c906108c 4693
a0f42c21 4694 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4695}
4696
93311388 4697/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4698
93311388 4699static void
a0f42c21 4700load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4701{
a0f42c21 4702 struct objfile *objfile = per_cu->objfile;
31ffec48 4703 bfd *abfd = objfile->obfd;
10b3939b 4704 struct dwarf2_cu *cu;
c764a876 4705 unsigned int offset;
93311388 4706 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4707 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4708 struct attribute *attr;
98bfdba5 4709 int read_cu = 0;
6502dd73 4710
b0df02fd 4711 gdb_assert (! per_cu->debug_types_section);
348e048f 4712
c906108c 4713 /* Set local variables from the partial symbol table info. */
10b3939b 4714 offset = per_cu->offset;
6502dd73 4715
be391dca 4716 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4717 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4718 beg_of_comp_unit = info_ptr;
63d06c5c 4719
98bfdba5
PA
4720 if (per_cu->cu == NULL)
4721 {
9816fde3
JK
4722 cu = xmalloc (sizeof (*cu));
4723 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4724
4725 read_cu = 1;
c906108c 4726
98bfdba5 4727 /* If an error occurs while loading, release our storage. */
68dc6402 4728 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4729
98bfdba5
PA
4730 /* Read in the comp_unit header. */
4731 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4732
6caca83c
CC
4733 /* Skip dummy compilation units. */
4734 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4735 + dwarf2_per_objfile->info.size)
4736 || peek_abbrev_code (abfd, info_ptr) == 0)
4737 {
4738 do_cleanups (free_cu_cleanup);
4739 return;
4740 }
4741
98bfdba5
PA
4742 /* Complete the cu_header. */
4743 cu->header.offset = offset;
4744 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4745
98bfdba5
PA
4746 /* Read the abbrevs for this compilation unit. */
4747 dwarf2_read_abbrevs (abfd, cu);
4748 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4749
98bfdba5
PA
4750 /* Link this compilation unit into the compilation unit tree. */
4751 per_cu->cu = cu;
4752 cu->per_cu = per_cu;
98bfdba5
PA
4753
4754 /* Link this CU into read_in_chain. */
4755 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4756 dwarf2_per_objfile->read_in_chain = per_cu;
4757 }
4758 else
4759 {
4760 cu = per_cu->cu;
4761 info_ptr += cu->header.first_die_offset;
4762 }
e142c38c 4763
93311388 4764 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4765
4766 /* We try not to read any attributes in this function, because not
9cdd5dbd 4767 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4768 table processing isn't initialized. But we have to set the CU language,
4769 or we won't be able to build types correctly. */
9816fde3 4770 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4771
a6c727b2
DJ
4772 /* Similarly, if we do not read the producer, we can not apply
4773 producer-specific interpretation. */
4774 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4775 if (attr)
4776 cu->producer = DW_STRING (attr);
4777
98bfdba5
PA
4778 if (read_cu)
4779 {
4780 do_cleanups (free_abbrevs_cleanup);
e142c38c 4781
98bfdba5
PA
4782 /* We've successfully allocated this compilation unit. Let our
4783 caller clean it up when finished with it. */
4784 discard_cleanups (free_cu_cleanup);
4785 }
10b3939b
DJ
4786}
4787
3da10d80
KS
4788/* Add a DIE to the delayed physname list. */
4789
4790static void
4791add_to_method_list (struct type *type, int fnfield_index, int index,
4792 const char *name, struct die_info *die,
4793 struct dwarf2_cu *cu)
4794{
4795 struct delayed_method_info mi;
4796 mi.type = type;
4797 mi.fnfield_index = fnfield_index;
4798 mi.index = index;
4799 mi.name = name;
4800 mi.die = die;
4801 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4802}
4803
4804/* A cleanup for freeing the delayed method list. */
4805
4806static void
4807free_delayed_list (void *ptr)
4808{
4809 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4810 if (cu->method_list != NULL)
4811 {
4812 VEC_free (delayed_method_info, cu->method_list);
4813 cu->method_list = NULL;
4814 }
4815}
4816
4817/* Compute the physnames of any methods on the CU's method list.
4818
4819 The computation of method physnames is delayed in order to avoid the
4820 (bad) condition that one of the method's formal parameters is of an as yet
4821 incomplete type. */
4822
4823static void
4824compute_delayed_physnames (struct dwarf2_cu *cu)
4825{
4826 int i;
4827 struct delayed_method_info *mi;
4828 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4829 {
1d06ead6 4830 const char *physname;
3da10d80
KS
4831 struct fn_fieldlist *fn_flp
4832 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4833 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4834 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4835 }
4836}
4837
9cdd5dbd 4838/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4839 already been loaded into memory. */
4840
4841static void
4842process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4843{
10b3939b 4844 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4845 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4846 CORE_ADDR lowpc, highpc;
4847 struct symtab *symtab;
3da10d80 4848 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4849 CORE_ADDR baseaddr;
4850
4851 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4852
10b3939b
DJ
4853 buildsym_init ();
4854 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4855 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4856
4857 cu->list_in_scope = &file_symbols;
c906108c
SS
4858
4859 /* Do line number decoding in read_file_scope () */
10b3939b 4860 process_die (cu->dies, cu);
c906108c 4861
3da10d80
KS
4862 /* Now that we have processed all the DIEs in the CU, all the types
4863 should be complete, and it should now be safe to compute all of the
4864 physnames. */
4865 compute_delayed_physnames (cu);
4866 do_cleanups (delayed_list_cleanup);
4867
fae299cd
DC
4868 /* Some compilers don't define a DW_AT_high_pc attribute for the
4869 compilation unit. If the DW_AT_high_pc is missing, synthesize
4870 it, by scanning the DIE's below the compilation unit. */
10b3939b 4871 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4872
613e1657 4873 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4874
8be455d7 4875 if (symtab != NULL)
c906108c 4876 {
df15bd07 4877 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4878
8be455d7
JK
4879 /* Set symtab language to language from DW_AT_language. If the
4880 compilation is from a C file generated by language preprocessors, do
4881 not set the language if it was already deduced by start_subfile. */
4882 if (!(cu->language == language_c && symtab->language != language_c))
4883 symtab->language = cu->language;
4884
4885 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4886 produce DW_AT_location with location lists but it can be possibly
4887 invalid without -fvar-tracking.
4888
4889 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4890 needed, it would be wrong due to missing DW_AT_producer there.
4891
4892 Still one can confuse GDB by using non-standard GCC compilation
4893 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4894 */
4632c0d0 4895 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4896 symtab->locations_valid = 1;
e0d00bc7
JK
4897
4898 if (gcc_4_minor >= 5)
4899 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4900
4901 symtab->call_site_htab = cu->call_site_htab;
c906108c 4902 }
9291a0cd
TT
4903
4904 if (dwarf2_per_objfile->using_index)
4905 per_cu->v.quick->symtab = symtab;
4906 else
4907 {
4908 struct partial_symtab *pst = per_cu->v.psymtab;
4909 pst->symtab = symtab;
4910 pst->readin = 1;
4911 }
c906108c
SS
4912
4913 do_cleanups (back_to);
4914}
4915
4916/* Process a die and its children. */
4917
4918static void
e7c27a73 4919process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4920{
4921 switch (die->tag)
4922 {
4923 case DW_TAG_padding:
4924 break;
4925 case DW_TAG_compile_unit:
e7c27a73 4926 read_file_scope (die, cu);
c906108c 4927 break;
348e048f
DE
4928 case DW_TAG_type_unit:
4929 read_type_unit_scope (die, cu);
4930 break;
c906108c 4931 case DW_TAG_subprogram:
c906108c 4932 case DW_TAG_inlined_subroutine:
edb3359d 4933 read_func_scope (die, cu);
c906108c
SS
4934 break;
4935 case DW_TAG_lexical_block:
14898363
L
4936 case DW_TAG_try_block:
4937 case DW_TAG_catch_block:
e7c27a73 4938 read_lexical_block_scope (die, cu);
c906108c 4939 break;
96408a79
SA
4940 case DW_TAG_GNU_call_site:
4941 read_call_site_scope (die, cu);
4942 break;
c906108c 4943 case DW_TAG_class_type:
680b30c7 4944 case DW_TAG_interface_type:
c906108c
SS
4945 case DW_TAG_structure_type:
4946 case DW_TAG_union_type:
134d01f1 4947 process_structure_scope (die, cu);
c906108c
SS
4948 break;
4949 case DW_TAG_enumeration_type:
134d01f1 4950 process_enumeration_scope (die, cu);
c906108c 4951 break;
134d01f1 4952
f792889a
DJ
4953 /* These dies have a type, but processing them does not create
4954 a symbol or recurse to process the children. Therefore we can
4955 read them on-demand through read_type_die. */
c906108c 4956 case DW_TAG_subroutine_type:
72019c9c 4957 case DW_TAG_set_type:
c906108c 4958 case DW_TAG_array_type:
c906108c 4959 case DW_TAG_pointer_type:
c906108c 4960 case DW_TAG_ptr_to_member_type:
c906108c 4961 case DW_TAG_reference_type:
c906108c 4962 case DW_TAG_string_type:
c906108c 4963 break;
134d01f1 4964
c906108c 4965 case DW_TAG_base_type:
a02abb62 4966 case DW_TAG_subrange_type:
cb249c71 4967 case DW_TAG_typedef:
134d01f1
DJ
4968 /* Add a typedef symbol for the type definition, if it has a
4969 DW_AT_name. */
f792889a 4970 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4971 break;
c906108c 4972 case DW_TAG_common_block:
e7c27a73 4973 read_common_block (die, cu);
c906108c
SS
4974 break;
4975 case DW_TAG_common_inclusion:
4976 break;
d9fa45fe 4977 case DW_TAG_namespace:
63d06c5c 4978 processing_has_namespace_info = 1;
e7c27a73 4979 read_namespace (die, cu);
d9fa45fe 4980 break;
5d7cb8df 4981 case DW_TAG_module:
f55ee35c 4982 processing_has_namespace_info = 1;
5d7cb8df
JK
4983 read_module (die, cu);
4984 break;
d9fa45fe
DC
4985 case DW_TAG_imported_declaration:
4986 case DW_TAG_imported_module:
63d06c5c 4987 processing_has_namespace_info = 1;
27aa8d6a
SW
4988 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4989 || cu->language != language_fortran))
4990 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4991 dwarf_tag_name (die->tag));
4992 read_import_statement (die, cu);
d9fa45fe 4993 break;
c906108c 4994 default:
e7c27a73 4995 new_symbol (die, NULL, cu);
c906108c
SS
4996 break;
4997 }
4998}
4999
94af9270
KS
5000/* A helper function for dwarf2_compute_name which determines whether DIE
5001 needs to have the name of the scope prepended to the name listed in the
5002 die. */
5003
5004static int
5005die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5006{
1c809c68
TT
5007 struct attribute *attr;
5008
94af9270
KS
5009 switch (die->tag)
5010 {
5011 case DW_TAG_namespace:
5012 case DW_TAG_typedef:
5013 case DW_TAG_class_type:
5014 case DW_TAG_interface_type:
5015 case DW_TAG_structure_type:
5016 case DW_TAG_union_type:
5017 case DW_TAG_enumeration_type:
5018 case DW_TAG_enumerator:
5019 case DW_TAG_subprogram:
5020 case DW_TAG_member:
5021 return 1;
5022
5023 case DW_TAG_variable:
c2b0a229 5024 case DW_TAG_constant:
94af9270
KS
5025 /* We only need to prefix "globally" visible variables. These include
5026 any variable marked with DW_AT_external or any variable that
5027 lives in a namespace. [Variables in anonymous namespaces
5028 require prefixing, but they are not DW_AT_external.] */
5029
5030 if (dwarf2_attr (die, DW_AT_specification, cu))
5031 {
5032 struct dwarf2_cu *spec_cu = cu;
9a619af0 5033
94af9270
KS
5034 return die_needs_namespace (die_specification (die, &spec_cu),
5035 spec_cu);
5036 }
5037
1c809c68 5038 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5039 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5040 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5041 return 0;
5042 /* A variable in a lexical block of some kind does not need a
5043 namespace, even though in C++ such variables may be external
5044 and have a mangled name. */
5045 if (die->parent->tag == DW_TAG_lexical_block
5046 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5047 || die->parent->tag == DW_TAG_catch_block
5048 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5049 return 0;
5050 return 1;
94af9270
KS
5051
5052 default:
5053 return 0;
5054 }
5055}
5056
98bfdba5
PA
5057/* Retrieve the last character from a mem_file. */
5058
5059static void
5060do_ui_file_peek_last (void *object, const char *buffer, long length)
5061{
5062 char *last_char_p = (char *) object;
5063
5064 if (length > 0)
5065 *last_char_p = buffer[length - 1];
5066}
5067
94af9270
KS
5068/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5069 compute the physname for the object, which include a method's
5070 formal parameters (C++/Java) and return type (Java).
5071
af6b7be1
JB
5072 For Ada, return the DIE's linkage name rather than the fully qualified
5073 name. PHYSNAME is ignored..
5074
94af9270
KS
5075 The result is allocated on the objfile_obstack and canonicalized. */
5076
5077static const char *
5078dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5079 int physname)
5080{
bb5ed363
DE
5081 struct objfile *objfile = cu->objfile;
5082
94af9270
KS
5083 if (name == NULL)
5084 name = dwarf2_name (die, cu);
5085
f55ee35c
JK
5086 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5087 compute it by typename_concat inside GDB. */
5088 if (cu->language == language_ada
5089 || (cu->language == language_fortran && physname))
5090 {
5091 /* For Ada unit, we prefer the linkage name over the name, as
5092 the former contains the exported name, which the user expects
5093 to be able to reference. Ideally, we want the user to be able
5094 to reference this entity using either natural or linkage name,
5095 but we haven't started looking at this enhancement yet. */
5096 struct attribute *attr;
5097
5098 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5099 if (attr == NULL)
5100 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5101 if (attr && DW_STRING (attr))
5102 return DW_STRING (attr);
5103 }
5104
94af9270
KS
5105 /* These are the only languages we know how to qualify names in. */
5106 if (name != NULL
f55ee35c
JK
5107 && (cu->language == language_cplus || cu->language == language_java
5108 || cu->language == language_fortran))
94af9270
KS
5109 {
5110 if (die_needs_namespace (die, cu))
5111 {
5112 long length;
5113 char *prefix;
5114 struct ui_file *buf;
5115
5116 prefix = determine_prefix (die, cu);
5117 buf = mem_fileopen ();
5118 if (*prefix != '\0')
5119 {
f55ee35c
JK
5120 char *prefixed_name = typename_concat (NULL, prefix, name,
5121 physname, cu);
9a619af0 5122
94af9270
KS
5123 fputs_unfiltered (prefixed_name, buf);
5124 xfree (prefixed_name);
5125 }
5126 else
62d5b8da 5127 fputs_unfiltered (name, buf);
94af9270 5128
98bfdba5
PA
5129 /* Template parameters may be specified in the DIE's DW_AT_name, or
5130 as children with DW_TAG_template_type_param or
5131 DW_TAG_value_type_param. If the latter, add them to the name
5132 here. If the name already has template parameters, then
5133 skip this step; some versions of GCC emit both, and
5134 it is more efficient to use the pre-computed name.
5135
5136 Something to keep in mind about this process: it is very
5137 unlikely, or in some cases downright impossible, to produce
5138 something that will match the mangled name of a function.
5139 If the definition of the function has the same debug info,
5140 we should be able to match up with it anyway. But fallbacks
5141 using the minimal symbol, for instance to find a method
5142 implemented in a stripped copy of libstdc++, will not work.
5143 If we do not have debug info for the definition, we will have to
5144 match them up some other way.
5145
5146 When we do name matching there is a related problem with function
5147 templates; two instantiated function templates are allowed to
5148 differ only by their return types, which we do not add here. */
5149
5150 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5151 {
5152 struct attribute *attr;
5153 struct die_info *child;
5154 int first = 1;
5155
5156 die->building_fullname = 1;
5157
5158 for (child = die->child; child != NULL; child = child->sibling)
5159 {
5160 struct type *type;
5161 long value;
5162 gdb_byte *bytes;
5163 struct dwarf2_locexpr_baton *baton;
5164 struct value *v;
5165
5166 if (child->tag != DW_TAG_template_type_param
5167 && child->tag != DW_TAG_template_value_param)
5168 continue;
5169
5170 if (first)
5171 {
5172 fputs_unfiltered ("<", buf);
5173 first = 0;
5174 }
5175 else
5176 fputs_unfiltered (", ", buf);
5177
5178 attr = dwarf2_attr (child, DW_AT_type, cu);
5179 if (attr == NULL)
5180 {
5181 complaint (&symfile_complaints,
5182 _("template parameter missing DW_AT_type"));
5183 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5184 continue;
5185 }
5186 type = die_type (child, cu);
5187
5188 if (child->tag == DW_TAG_template_type_param)
5189 {
5190 c_print_type (type, "", buf, -1, 0);
5191 continue;
5192 }
5193
5194 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5195 if (attr == NULL)
5196 {
5197 complaint (&symfile_complaints,
3e43a32a
MS
5198 _("template parameter missing "
5199 "DW_AT_const_value"));
98bfdba5
PA
5200 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5201 continue;
5202 }
5203
5204 dwarf2_const_value_attr (attr, type, name,
5205 &cu->comp_unit_obstack, cu,
5206 &value, &bytes, &baton);
5207
5208 if (TYPE_NOSIGN (type))
5209 /* GDB prints characters as NUMBER 'CHAR'. If that's
5210 changed, this can use value_print instead. */
5211 c_printchar (value, type, buf);
5212 else
5213 {
5214 struct value_print_options opts;
5215
5216 if (baton != NULL)
5217 v = dwarf2_evaluate_loc_desc (type, NULL,
5218 baton->data,
5219 baton->size,
5220 baton->per_cu);
5221 else if (bytes != NULL)
5222 {
5223 v = allocate_value (type);
5224 memcpy (value_contents_writeable (v), bytes,
5225 TYPE_LENGTH (type));
5226 }
5227 else
5228 v = value_from_longest (type, value);
5229
3e43a32a
MS
5230 /* Specify decimal so that we do not depend on
5231 the radix. */
98bfdba5
PA
5232 get_formatted_print_options (&opts, 'd');
5233 opts.raw = 1;
5234 value_print (v, buf, &opts);
5235 release_value (v);
5236 value_free (v);
5237 }
5238 }
5239
5240 die->building_fullname = 0;
5241
5242 if (!first)
5243 {
5244 /* Close the argument list, with a space if necessary
5245 (nested templates). */
5246 char last_char = '\0';
5247 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5248 if (last_char == '>')
5249 fputs_unfiltered (" >", buf);
5250 else
5251 fputs_unfiltered (">", buf);
5252 }
5253 }
5254
94af9270
KS
5255 /* For Java and C++ methods, append formal parameter type
5256 information, if PHYSNAME. */
6e70227d 5257
94af9270
KS
5258 if (physname && die->tag == DW_TAG_subprogram
5259 && (cu->language == language_cplus
5260 || cu->language == language_java))
5261 {
5262 struct type *type = read_type_die (die, cu);
5263
3167638f 5264 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5265
5266 if (cu->language == language_java)
5267 {
5268 /* For java, we must append the return type to method
0963b4bd 5269 names. */
94af9270
KS
5270 if (die->tag == DW_TAG_subprogram)
5271 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5272 0, 0);
5273 }
5274 else if (cu->language == language_cplus)
5275 {
60430eff
DJ
5276 /* Assume that an artificial first parameter is
5277 "this", but do not crash if it is not. RealView
5278 marks unnamed (and thus unused) parameters as
5279 artificial; there is no way to differentiate
5280 the two cases. */
94af9270
KS
5281 if (TYPE_NFIELDS (type) > 0
5282 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5283 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5284 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5285 0))))
94af9270
KS
5286 fputs_unfiltered (" const", buf);
5287 }
5288 }
5289
bb5ed363 5290 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5291 &length);
5292 ui_file_delete (buf);
5293
5294 if (cu->language == language_cplus)
5295 {
5296 char *cname
5297 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5298 &objfile->objfile_obstack);
9a619af0 5299
94af9270
KS
5300 if (cname != NULL)
5301 name = cname;
5302 }
5303 }
5304 }
5305
5306 return name;
5307}
5308
0114d602
DJ
5309/* Return the fully qualified name of DIE, based on its DW_AT_name.
5310 If scope qualifiers are appropriate they will be added. The result
5311 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5312 not have a name. NAME may either be from a previous call to
5313 dwarf2_name or NULL.
5314
0963b4bd 5315 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5316
5317static const char *
94af9270 5318dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5319{
94af9270
KS
5320 return dwarf2_compute_name (name, die, cu, 0);
5321}
0114d602 5322
94af9270
KS
5323/* Construct a physname for the given DIE in CU. NAME may either be
5324 from a previous call to dwarf2_name or NULL. The result will be
5325 allocated on the objfile_objstack or NULL if the DIE does not have a
5326 name.
0114d602 5327
94af9270 5328 The output string will be canonicalized (if C++/Java). */
0114d602 5329
94af9270
KS
5330static const char *
5331dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5332{
bb5ed363 5333 struct objfile *objfile = cu->objfile;
900e11f9
JK
5334 struct attribute *attr;
5335 const char *retval, *mangled = NULL, *canon = NULL;
5336 struct cleanup *back_to;
5337 int need_copy = 1;
5338
5339 /* In this case dwarf2_compute_name is just a shortcut not building anything
5340 on its own. */
5341 if (!die_needs_namespace (die, cu))
5342 return dwarf2_compute_name (name, die, cu, 1);
5343
5344 back_to = make_cleanup (null_cleanup, NULL);
5345
5346 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5347 if (!attr)
5348 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5349
5350 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5351 has computed. */
5352 if (attr && DW_STRING (attr))
5353 {
5354 char *demangled;
5355
5356 mangled = DW_STRING (attr);
5357
5358 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5359 type. It is easier for GDB users to search for such functions as
5360 `name(params)' than `long name(params)'. In such case the minimal
5361 symbol names do not match the full symbol names but for template
5362 functions there is never a need to look up their definition from their
5363 declaration so the only disadvantage remains the minimal symbol
5364 variant `long name(params)' does not have the proper inferior type.
5365 */
5366
5367 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5368 | (cu->language == language_java
5369 ? DMGL_JAVA | DMGL_RET_POSTFIX
5370 : DMGL_RET_DROP)));
5371 if (demangled)
5372 {
5373 make_cleanup (xfree, demangled);
5374 canon = demangled;
5375 }
5376 else
5377 {
5378 canon = mangled;
5379 need_copy = 0;
5380 }
5381 }
5382
5383 if (canon == NULL || check_physname)
5384 {
5385 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5386
5387 if (canon != NULL && strcmp (physname, canon) != 0)
5388 {
5389 /* It may not mean a bug in GDB. The compiler could also
5390 compute DW_AT_linkage_name incorrectly. But in such case
5391 GDB would need to be bug-to-bug compatible. */
5392
5393 complaint (&symfile_complaints,
5394 _("Computed physname <%s> does not match demangled <%s> "
5395 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
bb5ed363 5396 physname, canon, mangled, die->offset, objfile->name);
900e11f9
JK
5397
5398 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5399 is available here - over computed PHYSNAME. It is safer
5400 against both buggy GDB and buggy compilers. */
5401
5402 retval = canon;
5403 }
5404 else
5405 {
5406 retval = physname;
5407 need_copy = 0;
5408 }
5409 }
5410 else
5411 retval = canon;
5412
5413 if (need_copy)
5414 retval = obsavestring (retval, strlen (retval),
bb5ed363 5415 &objfile->objfile_obstack);
900e11f9
JK
5416
5417 do_cleanups (back_to);
5418 return retval;
0114d602
DJ
5419}
5420
27aa8d6a
SW
5421/* Read the import statement specified by the given die and record it. */
5422
5423static void
5424read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5425{
bb5ed363 5426 struct objfile *objfile = cu->objfile;
27aa8d6a 5427 struct attribute *import_attr;
32019081 5428 struct die_info *imported_die, *child_die;
de4affc9 5429 struct dwarf2_cu *imported_cu;
27aa8d6a 5430 const char *imported_name;
794684b6 5431 const char *imported_name_prefix;
13387711
SW
5432 const char *canonical_name;
5433 const char *import_alias;
5434 const char *imported_declaration = NULL;
794684b6 5435 const char *import_prefix;
32019081
JK
5436 VEC (const_char_ptr) *excludes = NULL;
5437 struct cleanup *cleanups;
13387711
SW
5438
5439 char *temp;
27aa8d6a
SW
5440
5441 import_attr = dwarf2_attr (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 (die->tag));
5446 return;
5447 }
5448
de4affc9
CC
5449 imported_cu = cu;
5450 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5451 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5452 if (imported_name == NULL)
5453 {
5454 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5455
5456 The import in the following code:
5457 namespace A
5458 {
5459 typedef int B;
5460 }
5461
5462 int main ()
5463 {
5464 using A::B;
5465 B b;
5466 return b;
5467 }
5468
5469 ...
5470 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5471 <52> DW_AT_decl_file : 1
5472 <53> DW_AT_decl_line : 6
5473 <54> DW_AT_import : <0x75>
5474 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5475 <59> DW_AT_name : B
5476 <5b> DW_AT_decl_file : 1
5477 <5c> DW_AT_decl_line : 2
5478 <5d> DW_AT_type : <0x6e>
5479 ...
5480 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5481 <76> DW_AT_byte_size : 4
5482 <77> DW_AT_encoding : 5 (signed)
5483
5484 imports the wrong die ( 0x75 instead of 0x58 ).
5485 This case will be ignored until the gcc bug is fixed. */
5486 return;
5487 }
5488
82856980
SW
5489 /* Figure out the local name after import. */
5490 import_alias = dwarf2_name (die, cu);
27aa8d6a 5491
794684b6
SW
5492 /* Figure out where the statement is being imported to. */
5493 import_prefix = determine_prefix (die, cu);
5494
5495 /* Figure out what the scope of the imported die is and prepend it
5496 to the name of the imported die. */
de4affc9 5497 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5498
f55ee35c
JK
5499 if (imported_die->tag != DW_TAG_namespace
5500 && imported_die->tag != DW_TAG_module)
794684b6 5501 {
13387711
SW
5502 imported_declaration = imported_name;
5503 canonical_name = imported_name_prefix;
794684b6 5504 }
13387711 5505 else if (strlen (imported_name_prefix) > 0)
794684b6 5506 {
13387711
SW
5507 temp = alloca (strlen (imported_name_prefix)
5508 + 2 + strlen (imported_name) + 1);
5509 strcpy (temp, imported_name_prefix);
5510 strcat (temp, "::");
5511 strcat (temp, imported_name);
5512 canonical_name = temp;
794684b6 5513 }
13387711
SW
5514 else
5515 canonical_name = imported_name;
794684b6 5516
32019081
JK
5517 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5518
5519 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5520 for (child_die = die->child; child_die && child_die->tag;
5521 child_die = sibling_die (child_die))
5522 {
5523 /* DWARF-4: A Fortran use statement with a “rename list” may be
5524 represented by an imported module entry with an import attribute
5525 referring to the module and owned entries corresponding to those
5526 entities that are renamed as part of being imported. */
5527
5528 if (child_die->tag != DW_TAG_imported_declaration)
5529 {
5530 complaint (&symfile_complaints,
5531 _("child DW_TAG_imported_declaration expected "
5532 "- DIE at 0x%x [in module %s]"),
bb5ed363 5533 child_die->offset, objfile->name);
32019081
JK
5534 continue;
5535 }
5536
5537 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5538 if (import_attr == NULL)
5539 {
5540 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5541 dwarf_tag_name (child_die->tag));
5542 continue;
5543 }
5544
5545 imported_cu = cu;
5546 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5547 &imported_cu);
5548 imported_name = dwarf2_name (imported_die, imported_cu);
5549 if (imported_name == NULL)
5550 {
5551 complaint (&symfile_complaints,
5552 _("child DW_TAG_imported_declaration has unknown "
5553 "imported name - DIE at 0x%x [in module %s]"),
bb5ed363 5554 child_die->offset, objfile->name);
32019081
JK
5555 continue;
5556 }
5557
5558 VEC_safe_push (const_char_ptr, excludes, imported_name);
5559
5560 process_die (child_die, cu);
5561 }
5562
c0cc3a76
SW
5563 cp_add_using_directive (import_prefix,
5564 canonical_name,
5565 import_alias,
13387711 5566 imported_declaration,
32019081 5567 excludes,
bb5ed363 5568 &objfile->objfile_obstack);
32019081
JK
5569
5570 do_cleanups (cleanups);
27aa8d6a
SW
5571}
5572
ae2de4f8
DE
5573/* Cleanup function for read_file_scope. */
5574
cb1df416
DJ
5575static void
5576free_cu_line_header (void *arg)
5577{
5578 struct dwarf2_cu *cu = arg;
5579
5580 free_line_header (cu->line_header);
5581 cu->line_header = NULL;
5582}
5583
9291a0cd
TT
5584static void
5585find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5586 char **name, char **comp_dir)
5587{
5588 struct attribute *attr;
5589
5590 *name = NULL;
5591 *comp_dir = NULL;
5592
5593 /* Find the filename. Do not use dwarf2_name here, since the filename
5594 is not a source language identifier. */
5595 attr = dwarf2_attr (die, DW_AT_name, cu);
5596 if (attr)
5597 {
5598 *name = DW_STRING (attr);
5599 }
5600
5601 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5602 if (attr)
5603 *comp_dir = DW_STRING (attr);
5604 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5605 {
5606 *comp_dir = ldirname (*name);
5607 if (*comp_dir != NULL)
5608 make_cleanup (xfree, *comp_dir);
5609 }
5610 if (*comp_dir != NULL)
5611 {
5612 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5613 directory, get rid of it. */
5614 char *cp = strchr (*comp_dir, ':');
5615
5616 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5617 *comp_dir = cp + 1;
5618 }
5619
5620 if (*name == NULL)
5621 *name = "<unknown>";
5622}
5623
2ab95328
TT
5624/* Handle DW_AT_stmt_list for a compilation unit. */
5625
5626static void
5627handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5628 const char *comp_dir)
5629{
5630 struct attribute *attr;
5631 struct objfile *objfile = cu->objfile;
5632 bfd *abfd = objfile->obfd;
5633
5634 /* Decode line number information if present. We do this before
5635 processing child DIEs, so that the line header table is available
5636 for DW_AT_decl_file. */
5637 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5638 if (attr)
5639 {
5640 unsigned int line_offset = DW_UNSND (attr);
5641 struct line_header *line_header
5642 = dwarf_decode_line_header (line_offset, abfd, cu);
5643
5644 if (line_header)
5645 {
5646 cu->line_header = line_header;
5647 make_cleanup (free_cu_line_header, cu);
5648 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5649 }
5650 }
5651}
5652
ae2de4f8
DE
5653/* Process DW_TAG_compile_unit. */
5654
c906108c 5655static void
e7c27a73 5656read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5657{
e7c27a73 5658 struct objfile *objfile = cu->objfile;
debd256d 5659 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5660 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5661 CORE_ADDR highpc = ((CORE_ADDR) 0);
5662 struct attribute *attr;
e1024ff1 5663 char *name = NULL;
c906108c
SS
5664 char *comp_dir = NULL;
5665 struct die_info *child_die;
5666 bfd *abfd = objfile->obfd;
e142c38c 5667 CORE_ADDR baseaddr;
6e70227d 5668
e142c38c 5669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5670
fae299cd 5671 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5672
5673 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5674 from finish_block. */
2acceee2 5675 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5676 lowpc = highpc;
5677 lowpc += baseaddr;
5678 highpc += baseaddr;
5679
9291a0cd 5680 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5681
e142c38c 5682 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5683 if (attr)
5684 {
e142c38c 5685 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5686 }
5687
b0f35d58 5688 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5689 if (attr)
b0f35d58 5690 cu->producer = DW_STRING (attr);
303b6f5d 5691
f4b8a18d
KW
5692 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5693 standardised yet. As a workaround for the language detection we fall
5694 back to the DW_AT_producer string. */
5695 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5696 cu->language = language_opencl;
5697
0963b4bd 5698 /* We assume that we're processing GCC output. */
c906108c 5699 processing_gcc_compilation = 2;
c906108c 5700
df8a16a1
DJ
5701 processing_has_namespace_info = 0;
5702
c906108c
SS
5703 start_symtab (name, comp_dir, lowpc);
5704 record_debugformat ("DWARF 2");
303b6f5d 5705 record_producer (cu->producer);
c906108c 5706
2ab95328 5707 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5708
cb1df416
DJ
5709 /* Process all dies in compilation unit. */
5710 if (die->child != NULL)
5711 {
5712 child_die = die->child;
5713 while (child_die && child_die->tag)
5714 {
5715 process_die (child_die, cu);
5716 child_die = sibling_die (child_die);
5717 }
5718 }
5719
2e276125
JB
5720 /* Decode macro information, if present. Dwarf 2 macro information
5721 refers to information in the line number info statement program
5722 header, so we can only read it if we've read the header
5723 successfully. */
cf2c3c16 5724 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5725 if (attr && cu->line_header)
2e276125 5726 {
cf2c3c16
TT
5727 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5728 complaint (&symfile_complaints,
5729 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5730
5731 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5732 comp_dir, abfd, cu,
5733 &dwarf2_per_objfile->macro, 1);
5734 }
5735 else
5736 {
5737 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5738 if (attr && cu->line_header)
5739 {
5740 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5741
cf2c3c16
TT
5742 dwarf_decode_macros (cu->line_header, macro_offset,
5743 comp_dir, abfd, cu,
5744 &dwarf2_per_objfile->macinfo, 0);
5745 }
2e276125 5746 }
9cdd5dbd 5747
debd256d 5748 do_cleanups (back_to);
5fb290d7
DJ
5749}
5750
ae2de4f8
DE
5751/* Process DW_TAG_type_unit.
5752 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5753 actual type being defined by this TU. In this case the first top
5754 level sibling is there to provide context only. */
5755
5756static void
5757read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5758{
5759 struct objfile *objfile = cu->objfile;
5760 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5761 CORE_ADDR lowpc;
5762 struct attribute *attr;
5763 char *name = NULL;
5764 char *comp_dir = NULL;
5765 struct die_info *child_die;
5766 bfd *abfd = objfile->obfd;
348e048f
DE
5767
5768 /* start_symtab needs a low pc, but we don't really have one.
5769 Do what read_file_scope would do in the absence of such info. */
5770 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5771
5772 /* Find the filename. Do not use dwarf2_name here, since the filename
5773 is not a source language identifier. */
5774 attr = dwarf2_attr (die, DW_AT_name, cu);
5775 if (attr)
5776 name = DW_STRING (attr);
5777
5778 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5779 if (attr)
5780 comp_dir = DW_STRING (attr);
5781 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5782 {
5783 comp_dir = ldirname (name);
5784 if (comp_dir != NULL)
5785 make_cleanup (xfree, comp_dir);
5786 }
5787
5788 if (name == NULL)
5789 name = "<unknown>";
5790
5791 attr = dwarf2_attr (die, DW_AT_language, cu);
5792 if (attr)
5793 set_cu_language (DW_UNSND (attr), cu);
5794
5795 /* This isn't technically needed today. It is done for symmetry
5796 with read_file_scope. */
5797 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5798 if (attr)
348e048f
DE
5799 cu->producer = DW_STRING (attr);
5800
0963b4bd 5801 /* We assume that we're processing GCC output. */
348e048f
DE
5802 processing_gcc_compilation = 2;
5803
5804 processing_has_namespace_info = 0;
5805
5806 start_symtab (name, comp_dir, lowpc);
5807 record_debugformat ("DWARF 2");
5808 record_producer (cu->producer);
5809
2ab95328
TT
5810 handle_DW_AT_stmt_list (die, cu, comp_dir);
5811
348e048f
DE
5812 /* Process the dies in the type unit. */
5813 if (die->child == NULL)
5814 {
5815 dump_die_for_error (die);
5816 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5817 bfd_get_filename (abfd));
5818 }
5819
5820 child_die = die->child;
5821
5822 while (child_die && child_die->tag)
5823 {
5824 process_die (child_die, cu);
5825
5826 child_die = sibling_die (child_die);
5827 }
5828
5829 do_cleanups (back_to);
5830}
5831
d389af10
JK
5832/* qsort helper for inherit_abstract_dies. */
5833
5834static int
5835unsigned_int_compar (const void *ap, const void *bp)
5836{
5837 unsigned int a = *(unsigned int *) ap;
5838 unsigned int b = *(unsigned int *) bp;
5839
5840 return (a > b) - (b > a);
5841}
5842
5843/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5844 Inherit only the children of the DW_AT_abstract_origin DIE not being
5845 already referenced by DW_AT_abstract_origin from the children of the
5846 current DIE. */
d389af10
JK
5847
5848static void
5849inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5850{
5851 struct die_info *child_die;
5852 unsigned die_children_count;
5853 /* CU offsets which were referenced by children of the current DIE. */
5854 unsigned *offsets;
5855 unsigned *offsets_end, *offsetp;
5856 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5857 struct die_info *origin_die;
5858 /* Iterator of the ORIGIN_DIE children. */
5859 struct die_info *origin_child_die;
5860 struct cleanup *cleanups;
5861 struct attribute *attr;
cd02d79d
PA
5862 struct dwarf2_cu *origin_cu;
5863 struct pending **origin_previous_list_in_scope;
d389af10
JK
5864
5865 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5866 if (!attr)
5867 return;
5868
cd02d79d
PA
5869 /* Note that following die references may follow to a die in a
5870 different cu. */
5871
5872 origin_cu = cu;
5873 origin_die = follow_die_ref (die, attr, &origin_cu);
5874
5875 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5876 symbols in. */
5877 origin_previous_list_in_scope = origin_cu->list_in_scope;
5878 origin_cu->list_in_scope = cu->list_in_scope;
5879
edb3359d
DJ
5880 if (die->tag != origin_die->tag
5881 && !(die->tag == DW_TAG_inlined_subroutine
5882 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5883 complaint (&symfile_complaints,
5884 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5885 die->offset, origin_die->offset);
5886
5887 child_die = die->child;
5888 die_children_count = 0;
5889 while (child_die && child_die->tag)
5890 {
5891 child_die = sibling_die (child_die);
5892 die_children_count++;
5893 }
5894 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5895 cleanups = make_cleanup (xfree, offsets);
5896
5897 offsets_end = offsets;
5898 child_die = die->child;
5899 while (child_die && child_die->tag)
5900 {
c38f313d
DJ
5901 /* For each CHILD_DIE, find the corresponding child of
5902 ORIGIN_DIE. If there is more than one layer of
5903 DW_AT_abstract_origin, follow them all; there shouldn't be,
5904 but GCC versions at least through 4.4 generate this (GCC PR
5905 40573). */
5906 struct die_info *child_origin_die = child_die;
cd02d79d 5907 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5908
c38f313d
DJ
5909 while (1)
5910 {
cd02d79d
PA
5911 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5912 child_origin_cu);
c38f313d
DJ
5913 if (attr == NULL)
5914 break;
cd02d79d
PA
5915 child_origin_die = follow_die_ref (child_origin_die, attr,
5916 &child_origin_cu);
c38f313d
DJ
5917 }
5918
d389af10
JK
5919 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5920 counterpart may exist. */
c38f313d 5921 if (child_origin_die != child_die)
d389af10 5922 {
edb3359d
DJ
5923 if (child_die->tag != child_origin_die->tag
5924 && !(child_die->tag == DW_TAG_inlined_subroutine
5925 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5926 complaint (&symfile_complaints,
5927 _("Child DIE 0x%x and its abstract origin 0x%x have "
5928 "different tags"), child_die->offset,
5929 child_origin_die->offset);
c38f313d
DJ
5930 if (child_origin_die->parent != origin_die)
5931 complaint (&symfile_complaints,
5932 _("Child DIE 0x%x and its abstract origin 0x%x have "
5933 "different parents"), child_die->offset,
5934 child_origin_die->offset);
5935 else
5936 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5937 }
5938 child_die = sibling_die (child_die);
5939 }
5940 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5941 unsigned_int_compar);
5942 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5943 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5944 complaint (&symfile_complaints,
5945 _("Multiple children of DIE 0x%x refer "
5946 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5947 die->offset, *offsetp);
5948
5949 offsetp = offsets;
5950 origin_child_die = origin_die->child;
5951 while (origin_child_die && origin_child_die->tag)
5952 {
5953 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5954 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5955 offsetp++;
5956 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5957 {
5958 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5959 process_die (origin_child_die, origin_cu);
d389af10
JK
5960 }
5961 origin_child_die = sibling_die (origin_child_die);
5962 }
cd02d79d 5963 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5964
5965 do_cleanups (cleanups);
5966}
5967
c906108c 5968static void
e7c27a73 5969read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5970{
e7c27a73 5971 struct objfile *objfile = cu->objfile;
52f0bd74 5972 struct context_stack *new;
c906108c
SS
5973 CORE_ADDR lowpc;
5974 CORE_ADDR highpc;
5975 struct die_info *child_die;
edb3359d 5976 struct attribute *attr, *call_line, *call_file;
c906108c 5977 char *name;
e142c38c 5978 CORE_ADDR baseaddr;
801e3a5b 5979 struct block *block;
edb3359d 5980 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5981 VEC (symbolp) *template_args = NULL;
5982 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5983
5984 if (inlined_func)
5985 {
5986 /* If we do not have call site information, we can't show the
5987 caller of this inlined function. That's too confusing, so
5988 only use the scope for local variables. */
5989 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5990 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5991 if (call_line == NULL || call_file == NULL)
5992 {
5993 read_lexical_block_scope (die, cu);
5994 return;
5995 }
5996 }
c906108c 5997
e142c38c
DJ
5998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5999
94af9270 6000 name = dwarf2_name (die, cu);
c906108c 6001
e8d05480
JB
6002 /* Ignore functions with missing or empty names. These are actually
6003 illegal according to the DWARF standard. */
6004 if (name == NULL)
6005 {
6006 complaint (&symfile_complaints,
6007 _("missing name for subprogram DIE at %d"), die->offset);
6008 return;
6009 }
6010
6011 /* Ignore functions with missing or invalid low and high pc attributes. */
6012 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6013 {
ae4d0c03
PM
6014 attr = dwarf2_attr (die, DW_AT_external, cu);
6015 if (!attr || !DW_UNSND (attr))
6016 complaint (&symfile_complaints,
3e43a32a
MS
6017 _("cannot get low and high bounds "
6018 "for subprogram DIE at %d"),
ae4d0c03 6019 die->offset);
e8d05480
JB
6020 return;
6021 }
c906108c
SS
6022
6023 lowpc += baseaddr;
6024 highpc += baseaddr;
6025
34eaf542
TT
6026 /* If we have any template arguments, then we must allocate a
6027 different sort of symbol. */
6028 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6029 {
6030 if (child_die->tag == DW_TAG_template_type_param
6031 || child_die->tag == DW_TAG_template_value_param)
6032 {
6033 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6034 struct template_symbol);
6035 templ_func->base.is_cplus_template_function = 1;
6036 break;
6037 }
6038 }
6039
c906108c 6040 new = push_context (0, lowpc);
34eaf542
TT
6041 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6042 (struct symbol *) templ_func);
4c2df51b 6043
4cecd739
DJ
6044 /* If there is a location expression for DW_AT_frame_base, record
6045 it. */
e142c38c 6046 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6047 if (attr)
c034e007
AC
6048 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6049 expression is being recorded directly in the function's symbol
6050 and not in a separate frame-base object. I guess this hack is
6051 to avoid adding some sort of frame-base adjunct/annex to the
6052 function's symbol :-(. The problem with doing this is that it
6053 results in a function symbol with a location expression that
6054 has nothing to do with the location of the function, ouch! The
6055 relationship should be: a function's symbol has-a frame base; a
6056 frame-base has-a location expression. */
e7c27a73 6057 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6058
e142c38c 6059 cu->list_in_scope = &local_symbols;
c906108c 6060
639d11d3 6061 if (die->child != NULL)
c906108c 6062 {
639d11d3 6063 child_die = die->child;
c906108c
SS
6064 while (child_die && child_die->tag)
6065 {
34eaf542
TT
6066 if (child_die->tag == DW_TAG_template_type_param
6067 || child_die->tag == DW_TAG_template_value_param)
6068 {
6069 struct symbol *arg = new_symbol (child_die, NULL, cu);
6070
f1078f66
DJ
6071 if (arg != NULL)
6072 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6073 }
6074 else
6075 process_die (child_die, cu);
c906108c
SS
6076 child_die = sibling_die (child_die);
6077 }
6078 }
6079
d389af10
JK
6080 inherit_abstract_dies (die, cu);
6081
4a811a97
UW
6082 /* If we have a DW_AT_specification, we might need to import using
6083 directives from the context of the specification DIE. See the
6084 comment in determine_prefix. */
6085 if (cu->language == language_cplus
6086 && dwarf2_attr (die, DW_AT_specification, cu))
6087 {
6088 struct dwarf2_cu *spec_cu = cu;
6089 struct die_info *spec_die = die_specification (die, &spec_cu);
6090
6091 while (spec_die)
6092 {
6093 child_die = spec_die->child;
6094 while (child_die && child_die->tag)
6095 {
6096 if (child_die->tag == DW_TAG_imported_module)
6097 process_die (child_die, spec_cu);
6098 child_die = sibling_die (child_die);
6099 }
6100
6101 /* In some cases, GCC generates specification DIEs that
6102 themselves contain DW_AT_specification attributes. */
6103 spec_die = die_specification (spec_die, &spec_cu);
6104 }
6105 }
6106
c906108c
SS
6107 new = pop_context ();
6108 /* Make a block for the local symbols within. */
801e3a5b
JB
6109 block = finish_block (new->name, &local_symbols, new->old_blocks,
6110 lowpc, highpc, objfile);
6111
df8a16a1 6112 /* For C++, set the block's scope. */
f55ee35c 6113 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6114 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6115 determine_prefix (die, cu),
df8a16a1
DJ
6116 processing_has_namespace_info);
6117
801e3a5b
JB
6118 /* If we have address ranges, record them. */
6119 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6120
34eaf542
TT
6121 /* Attach template arguments to function. */
6122 if (! VEC_empty (symbolp, template_args))
6123 {
6124 gdb_assert (templ_func != NULL);
6125
6126 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6127 templ_func->template_arguments
6128 = obstack_alloc (&objfile->objfile_obstack,
6129 (templ_func->n_template_arguments
6130 * sizeof (struct symbol *)));
6131 memcpy (templ_func->template_arguments,
6132 VEC_address (symbolp, template_args),
6133 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6134 VEC_free (symbolp, template_args);
6135 }
6136
208d8187
JB
6137 /* In C++, we can have functions nested inside functions (e.g., when
6138 a function declares a class that has methods). This means that
6139 when we finish processing a function scope, we may need to go
6140 back to building a containing block's symbol lists. */
6141 local_symbols = new->locals;
6142 param_symbols = new->params;
27aa8d6a 6143 using_directives = new->using_directives;
208d8187 6144
921e78cf
JB
6145 /* If we've finished processing a top-level function, subsequent
6146 symbols go in the file symbol list. */
6147 if (outermost_context_p ())
e142c38c 6148 cu->list_in_scope = &file_symbols;
c906108c
SS
6149}
6150
6151/* Process all the DIES contained within a lexical block scope. Start
6152 a new scope, process the dies, and then close the scope. */
6153
6154static void
e7c27a73 6155read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6156{
e7c27a73 6157 struct objfile *objfile = cu->objfile;
52f0bd74 6158 struct context_stack *new;
c906108c
SS
6159 CORE_ADDR lowpc, highpc;
6160 struct die_info *child_die;
e142c38c
DJ
6161 CORE_ADDR baseaddr;
6162
6163 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6164
6165 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6166 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6167 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6168 be nasty. Might be easier to properly extend generic blocks to
af34e669 6169 describe ranges. */
d85a05f0 6170 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6171 return;
6172 lowpc += baseaddr;
6173 highpc += baseaddr;
6174
6175 push_context (0, lowpc);
639d11d3 6176 if (die->child != NULL)
c906108c 6177 {
639d11d3 6178 child_die = die->child;
c906108c
SS
6179 while (child_die && child_die->tag)
6180 {
e7c27a73 6181 process_die (child_die, cu);
c906108c
SS
6182 child_die = sibling_die (child_die);
6183 }
6184 }
6185 new = pop_context ();
6186
8540c487 6187 if (local_symbols != NULL || using_directives != NULL)
c906108c 6188 {
801e3a5b
JB
6189 struct block *block
6190 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6191 highpc, objfile);
6192
6193 /* Note that recording ranges after traversing children, as we
6194 do here, means that recording a parent's ranges entails
6195 walking across all its children's ranges as they appear in
6196 the address map, which is quadratic behavior.
6197
6198 It would be nicer to record the parent's ranges before
6199 traversing its children, simply overriding whatever you find
6200 there. But since we don't even decide whether to create a
6201 block until after we've traversed its children, that's hard
6202 to do. */
6203 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6204 }
6205 local_symbols = new->locals;
27aa8d6a 6206 using_directives = new->using_directives;
c906108c
SS
6207}
6208
96408a79
SA
6209/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6210
6211static void
6212read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6213{
6214 struct objfile *objfile = cu->objfile;
6215 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6216 CORE_ADDR pc, baseaddr;
6217 struct attribute *attr;
6218 struct call_site *call_site, call_site_local;
6219 void **slot;
6220 int nparams;
6221 struct die_info *child_die;
6222
6223 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6224
6225 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6226 if (!attr)
6227 {
6228 complaint (&symfile_complaints,
6229 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6230 "DIE 0x%x [in module %s]"),
bb5ed363 6231 die->offset, objfile->name);
96408a79
SA
6232 return;
6233 }
6234 pc = DW_ADDR (attr) + baseaddr;
6235
6236 if (cu->call_site_htab == NULL)
6237 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6238 NULL, &objfile->objfile_obstack,
6239 hashtab_obstack_allocate, NULL);
6240 call_site_local.pc = pc;
6241 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6242 if (*slot != NULL)
6243 {
6244 complaint (&symfile_complaints,
6245 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6246 "DIE 0x%x [in module %s]"),
bb5ed363 6247 paddress (gdbarch, pc), die->offset, objfile->name);
96408a79
SA
6248 return;
6249 }
6250
6251 /* Count parameters at the caller. */
6252
6253 nparams = 0;
6254 for (child_die = die->child; child_die && child_die->tag;
6255 child_die = sibling_die (child_die))
6256 {
6257 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6258 {
6259 complaint (&symfile_complaints,
6260 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6261 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6262 child_die->tag, child_die->offset, objfile->name);
96408a79
SA
6263 continue;
6264 }
6265
6266 nparams++;
6267 }
6268
6269 call_site = obstack_alloc (&objfile->objfile_obstack,
6270 (sizeof (*call_site)
6271 + (sizeof (*call_site->parameter)
6272 * (nparams - 1))));
6273 *slot = call_site;
6274 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6275 call_site->pc = pc;
6276
6277 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6278 {
6279 struct die_info *func_die;
6280
6281 /* Skip also over DW_TAG_inlined_subroutine. */
6282 for (func_die = die->parent;
6283 func_die && func_die->tag != DW_TAG_subprogram
6284 && func_die->tag != DW_TAG_subroutine_type;
6285 func_die = func_die->parent);
6286
6287 /* DW_AT_GNU_all_call_sites is a superset
6288 of DW_AT_GNU_all_tail_call_sites. */
6289 if (func_die
6290 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6291 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6292 {
6293 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6294 not complete. But keep CALL_SITE for look ups via call_site_htab,
6295 both the initial caller containing the real return address PC and
6296 the final callee containing the current PC of a chain of tail
6297 calls do not need to have the tail call list complete. But any
6298 function candidate for a virtual tail call frame searched via
6299 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6300 determined unambiguously. */
6301 }
6302 else
6303 {
6304 struct type *func_type = NULL;
6305
6306 if (func_die)
6307 func_type = get_die_type (func_die, cu);
6308 if (func_type != NULL)
6309 {
6310 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6311
6312 /* Enlist this call site to the function. */
6313 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6314 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6315 }
6316 else
6317 complaint (&symfile_complaints,
6318 _("Cannot find function owning DW_TAG_GNU_call_site "
6319 "DIE 0x%x [in module %s]"),
bb5ed363 6320 die->offset, objfile->name);
96408a79
SA
6321 }
6322 }
6323
6324 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6325 if (attr == NULL)
6326 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6327 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6328 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6329 /* Keep NULL DWARF_BLOCK. */;
6330 else if (attr_form_is_block (attr))
6331 {
6332 struct dwarf2_locexpr_baton *dlbaton;
6333
6334 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6335 dlbaton->data = DW_BLOCK (attr)->data;
6336 dlbaton->size = DW_BLOCK (attr)->size;
6337 dlbaton->per_cu = cu->per_cu;
6338
6339 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6340 }
6341 else if (is_ref_attr (attr))
6342 {
96408a79
SA
6343 struct dwarf2_cu *target_cu = cu;
6344 struct die_info *target_die;
6345
6346 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6347 gdb_assert (target_cu->objfile == objfile);
6348 if (die_is_declaration (target_die, target_cu))
6349 {
6350 const char *target_physname;
6351
6352 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6353 if (target_physname == NULL)
6354 complaint (&symfile_complaints,
6355 _("DW_AT_GNU_call_site_target target DIE has invalid "
6356 "physname, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6357 die->offset, objfile->name);
96408a79
SA
6358 else
6359 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6360 }
6361 else
6362 {
6363 CORE_ADDR lowpc;
6364
6365 /* DW_AT_entry_pc should be preferred. */
6366 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6367 complaint (&symfile_complaints,
6368 _("DW_AT_GNU_call_site_target target DIE has invalid "
6369 "low pc, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6370 die->offset, objfile->name);
96408a79
SA
6371 else
6372 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6373 }
6374 }
6375 else
6376 complaint (&symfile_complaints,
6377 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6378 "block nor reference, for DIE 0x%x [in module %s]"),
bb5ed363 6379 die->offset, objfile->name);
96408a79
SA
6380
6381 call_site->per_cu = cu->per_cu;
6382
6383 for (child_die = die->child;
6384 child_die && child_die->tag;
6385 child_die = sibling_die (child_die))
6386 {
6387 struct dwarf2_locexpr_baton *dlbaton;
6388 struct call_site_parameter *parameter;
6389
6390 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6391 {
6392 /* Already printed the complaint above. */
6393 continue;
6394 }
6395
6396 gdb_assert (call_site->parameter_count < nparams);
6397 parameter = &call_site->parameter[call_site->parameter_count];
6398
6399 /* DW_AT_location specifies the register number. Value of the data
6400 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6401
6402 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6403 if (!attr || !attr_form_is_block (attr))
6404 {
6405 complaint (&symfile_complaints,
6406 _("No DW_FORM_block* DW_AT_location for "
6407 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6408 child_die->offset, objfile->name);
96408a79
SA
6409 continue;
6410 }
6411 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6412 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6413 if (parameter->dwarf_reg == -1
6414 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6415 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6416 &parameter->fb_offset))
6417 {
6418 complaint (&symfile_complaints,
6419 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6420 "for DW_FORM_block* DW_AT_location for "
6421 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6422 child_die->offset, objfile->name);
96408a79
SA
6423 continue;
6424 }
6425
6426 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6427 if (!attr_form_is_block (attr))
6428 {
6429 complaint (&symfile_complaints,
6430 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6431 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6432 child_die->offset, objfile->name);
96408a79
SA
6433 continue;
6434 }
6435 parameter->value = DW_BLOCK (attr)->data;
6436 parameter->value_size = DW_BLOCK (attr)->size;
6437
6438 /* Parameters are not pre-cleared by memset above. */
6439 parameter->data_value = NULL;
6440 parameter->data_value_size = 0;
6441 call_site->parameter_count++;
6442
6443 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6444 if (attr)
6445 {
6446 if (!attr_form_is_block (attr))
6447 complaint (&symfile_complaints,
6448 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6449 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6450 child_die->offset, objfile->name);
96408a79
SA
6451 else
6452 {
6453 parameter->data_value = DW_BLOCK (attr)->data;
6454 parameter->data_value_size = DW_BLOCK (attr)->size;
6455 }
6456 }
6457 }
6458}
6459
43039443 6460/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6461 Return 1 if the attributes are present and valid, otherwise, return 0.
6462 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6463
6464static int
6465dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6466 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6467 struct partial_symtab *ranges_pst)
43039443
JK
6468{
6469 struct objfile *objfile = cu->objfile;
6470 struct comp_unit_head *cu_header = &cu->header;
6471 bfd *obfd = objfile->obfd;
6472 unsigned int addr_size = cu_header->addr_size;
6473 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6474 /* Base address selection entry. */
6475 CORE_ADDR base;
6476 int found_base;
6477 unsigned int dummy;
6478 gdb_byte *buffer;
6479 CORE_ADDR marker;
6480 int low_set;
6481 CORE_ADDR low = 0;
6482 CORE_ADDR high = 0;
ff013f42 6483 CORE_ADDR baseaddr;
43039443 6484
d00adf39
DE
6485 found_base = cu->base_known;
6486 base = cu->base_address;
43039443 6487
be391dca 6488 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6489 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6490 {
6491 complaint (&symfile_complaints,
6492 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6493 offset);
6494 return 0;
6495 }
dce234bc 6496 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6497
6498 /* Read in the largest possible address. */
6499 marker = read_address (obfd, buffer, cu, &dummy);
6500 if ((marker & mask) == mask)
6501 {
6502 /* If we found the largest possible address, then
6503 read the base address. */
6504 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6505 buffer += 2 * addr_size;
6506 offset += 2 * addr_size;
6507 found_base = 1;
6508 }
6509
6510 low_set = 0;
6511
e7030f15 6512 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6513
43039443
JK
6514 while (1)
6515 {
6516 CORE_ADDR range_beginning, range_end;
6517
6518 range_beginning = read_address (obfd, buffer, cu, &dummy);
6519 buffer += addr_size;
6520 range_end = read_address (obfd, buffer, cu, &dummy);
6521 buffer += addr_size;
6522 offset += 2 * addr_size;
6523
6524 /* An end of list marker is a pair of zero addresses. */
6525 if (range_beginning == 0 && range_end == 0)
6526 /* Found the end of list entry. */
6527 break;
6528
6529 /* Each base address selection entry is a pair of 2 values.
6530 The first is the largest possible address, the second is
6531 the base address. Check for a base address here. */
6532 if ((range_beginning & mask) == mask)
6533 {
6534 /* If we found the largest possible address, then
6535 read the base address. */
6536 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6537 found_base = 1;
6538 continue;
6539 }
6540
6541 if (!found_base)
6542 {
6543 /* We have no valid base address for the ranges
6544 data. */
6545 complaint (&symfile_complaints,
6546 _("Invalid .debug_ranges data (no base address)"));
6547 return 0;
6548 }
6549
9277c30c
UW
6550 if (range_beginning > range_end)
6551 {
6552 /* Inverted range entries are invalid. */
6553 complaint (&symfile_complaints,
6554 _("Invalid .debug_ranges data (inverted range)"));
6555 return 0;
6556 }
6557
6558 /* Empty range entries have no effect. */
6559 if (range_beginning == range_end)
6560 continue;
6561
43039443
JK
6562 range_beginning += base;
6563 range_end += base;
6564
9277c30c 6565 if (ranges_pst != NULL)
ff013f42 6566 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6567 range_beginning + baseaddr,
6568 range_end - 1 + baseaddr,
ff013f42
JK
6569 ranges_pst);
6570
43039443
JK
6571 /* FIXME: This is recording everything as a low-high
6572 segment of consecutive addresses. We should have a
6573 data structure for discontiguous block ranges
6574 instead. */
6575 if (! low_set)
6576 {
6577 low = range_beginning;
6578 high = range_end;
6579 low_set = 1;
6580 }
6581 else
6582 {
6583 if (range_beginning < low)
6584 low = range_beginning;
6585 if (range_end > high)
6586 high = range_end;
6587 }
6588 }
6589
6590 if (! low_set)
6591 /* If the first entry is an end-of-list marker, the range
6592 describes an empty scope, i.e. no instructions. */
6593 return 0;
6594
6595 if (low_return)
6596 *low_return = low;
6597 if (high_return)
6598 *high_return = high;
6599 return 1;
6600}
6601
af34e669
DJ
6602/* Get low and high pc attributes from a die. Return 1 if the attributes
6603 are present and valid, otherwise, return 0. Return -1 if the range is
6604 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6605static int
af34e669 6606dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6607 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6608 struct partial_symtab *pst)
c906108c
SS
6609{
6610 struct attribute *attr;
af34e669
DJ
6611 CORE_ADDR low = 0;
6612 CORE_ADDR high = 0;
6613 int ret = 0;
c906108c 6614
e142c38c 6615 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6616 if (attr)
af34e669
DJ
6617 {
6618 high = DW_ADDR (attr);
e142c38c 6619 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6620 if (attr)
6621 low = DW_ADDR (attr);
6622 else
6623 /* Found high w/o low attribute. */
6624 return 0;
6625
6626 /* Found consecutive range of addresses. */
6627 ret = 1;
6628 }
c906108c 6629 else
af34e669 6630 {
e142c38c 6631 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6632 if (attr != NULL)
6633 {
af34e669 6634 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6635 .debug_ranges section. */
d85a05f0 6636 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6637 return 0;
43039443 6638 /* Found discontinuous range of addresses. */
af34e669
DJ
6639 ret = -1;
6640 }
6641 }
c906108c 6642
9373cf26
JK
6643 /* read_partial_die has also the strict LOW < HIGH requirement. */
6644 if (high <= low)
c906108c
SS
6645 return 0;
6646
6647 /* When using the GNU linker, .gnu.linkonce. sections are used to
6648 eliminate duplicate copies of functions and vtables and such.
6649 The linker will arbitrarily choose one and discard the others.
6650 The AT_*_pc values for such functions refer to local labels in
6651 these sections. If the section from that file was discarded, the
6652 labels are not in the output, so the relocs get a value of 0.
6653 If this is a discarded function, mark the pc bounds as invalid,
6654 so that GDB will ignore it. */
72dca2f5 6655 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6656 return 0;
6657
6658 *lowpc = low;
96408a79
SA
6659 if (highpc)
6660 *highpc = high;
af34e669 6661 return ret;
c906108c
SS
6662}
6663
b084d499
JB
6664/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6665 its low and high PC addresses. Do nothing if these addresses could not
6666 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6667 and HIGHPC to the high address if greater than HIGHPC. */
6668
6669static void
6670dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6671 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6672 struct dwarf2_cu *cu)
6673{
6674 CORE_ADDR low, high;
6675 struct die_info *child = die->child;
6676
d85a05f0 6677 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6678 {
6679 *lowpc = min (*lowpc, low);
6680 *highpc = max (*highpc, high);
6681 }
6682
6683 /* If the language does not allow nested subprograms (either inside
6684 subprograms or lexical blocks), we're done. */
6685 if (cu->language != language_ada)
6686 return;
6e70227d 6687
b084d499
JB
6688 /* Check all the children of the given DIE. If it contains nested
6689 subprograms, then check their pc bounds. Likewise, we need to
6690 check lexical blocks as well, as they may also contain subprogram
6691 definitions. */
6692 while (child && child->tag)
6693 {
6694 if (child->tag == DW_TAG_subprogram
6695 || child->tag == DW_TAG_lexical_block)
6696 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6697 child = sibling_die (child);
6698 }
6699}
6700
fae299cd
DC
6701/* Get the low and high pc's represented by the scope DIE, and store
6702 them in *LOWPC and *HIGHPC. If the correct values can't be
6703 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6704
6705static void
6706get_scope_pc_bounds (struct die_info *die,
6707 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6708 struct dwarf2_cu *cu)
6709{
6710 CORE_ADDR best_low = (CORE_ADDR) -1;
6711 CORE_ADDR best_high = (CORE_ADDR) 0;
6712 CORE_ADDR current_low, current_high;
6713
d85a05f0 6714 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6715 {
6716 best_low = current_low;
6717 best_high = current_high;
6718 }
6719 else
6720 {
6721 struct die_info *child = die->child;
6722
6723 while (child && child->tag)
6724 {
6725 switch (child->tag) {
6726 case DW_TAG_subprogram:
b084d499 6727 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6728 break;
6729 case DW_TAG_namespace:
f55ee35c 6730 case DW_TAG_module:
fae299cd
DC
6731 /* FIXME: carlton/2004-01-16: Should we do this for
6732 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6733 that current GCC's always emit the DIEs corresponding
6734 to definitions of methods of classes as children of a
6735 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6736 the DIEs giving the declarations, which could be
6737 anywhere). But I don't see any reason why the
6738 standards says that they have to be there. */
6739 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6740
6741 if (current_low != ((CORE_ADDR) -1))
6742 {
6743 best_low = min (best_low, current_low);
6744 best_high = max (best_high, current_high);
6745 }
6746 break;
6747 default:
0963b4bd 6748 /* Ignore. */
fae299cd
DC
6749 break;
6750 }
6751
6752 child = sibling_die (child);
6753 }
6754 }
6755
6756 *lowpc = best_low;
6757 *highpc = best_high;
6758}
6759
801e3a5b
JB
6760/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6761 in DIE. */
6762static void
6763dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6764 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6765{
bb5ed363 6766 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6767 struct attribute *attr;
6768
6769 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6770 if (attr)
6771 {
6772 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6773
801e3a5b
JB
6774 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6775 if (attr)
6776 {
6777 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6778
801e3a5b
JB
6779 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6780 }
6781 }
6782
6783 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6784 if (attr)
6785 {
bb5ed363 6786 bfd *obfd = objfile->obfd;
801e3a5b
JB
6787
6788 /* The value of the DW_AT_ranges attribute is the offset of the
6789 address range list in the .debug_ranges section. */
6790 unsigned long offset = DW_UNSND (attr);
dce234bc 6791 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6792
6793 /* For some target architectures, but not others, the
6794 read_address function sign-extends the addresses it returns.
6795 To recognize base address selection entries, we need a
6796 mask. */
6797 unsigned int addr_size = cu->header.addr_size;
6798 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6799
6800 /* The base address, to which the next pair is relative. Note
6801 that this 'base' is a DWARF concept: most entries in a range
6802 list are relative, to reduce the number of relocs against the
6803 debugging information. This is separate from this function's
6804 'baseaddr' argument, which GDB uses to relocate debugging
6805 information from a shared library based on the address at
6806 which the library was loaded. */
d00adf39
DE
6807 CORE_ADDR base = cu->base_address;
6808 int base_known = cu->base_known;
801e3a5b 6809
be391dca 6810 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6811 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6812 {
6813 complaint (&symfile_complaints,
6814 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6815 offset);
6816 return;
6817 }
6818
6819 for (;;)
6820 {
6821 unsigned int bytes_read;
6822 CORE_ADDR start, end;
6823
6824 start = read_address (obfd, buffer, cu, &bytes_read);
6825 buffer += bytes_read;
6826 end = read_address (obfd, buffer, cu, &bytes_read);
6827 buffer += bytes_read;
6828
6829 /* Did we find the end of the range list? */
6830 if (start == 0 && end == 0)
6831 break;
6832
6833 /* Did we find a base address selection entry? */
6834 else if ((start & base_select_mask) == base_select_mask)
6835 {
6836 base = end;
6837 base_known = 1;
6838 }
6839
6840 /* We found an ordinary address range. */
6841 else
6842 {
6843 if (!base_known)
6844 {
6845 complaint (&symfile_complaints,
3e43a32a
MS
6846 _("Invalid .debug_ranges data "
6847 "(no base address)"));
801e3a5b
JB
6848 return;
6849 }
6850
9277c30c
UW
6851 if (start > end)
6852 {
6853 /* Inverted range entries are invalid. */
6854 complaint (&symfile_complaints,
6855 _("Invalid .debug_ranges data "
6856 "(inverted range)"));
6857 return;
6858 }
6859
6860 /* Empty range entries have no effect. */
6861 if (start == end)
6862 continue;
6863
6e70227d
DE
6864 record_block_range (block,
6865 baseaddr + base + start,
801e3a5b
JB
6866 baseaddr + base + end - 1);
6867 }
6868 }
6869 }
6870}
6871
60d5a603
JK
6872/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6873 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6874 during 4.6.0 experimental. */
6875
6876static int
6877producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6878{
6879 const char *cs;
6880 int major, minor, release;
6881
6882 if (cu->producer == NULL)
6883 {
6884 /* For unknown compilers expect their behavior is DWARF version
6885 compliant.
6886
6887 GCC started to support .debug_types sections by -gdwarf-4 since
6888 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6889 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6890 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6891 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6892
6893 return 0;
6894 }
6895
6896 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6897
6898 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6899 {
6900 /* For non-GCC compilers expect their behavior is DWARF version
6901 compliant. */
6902
6903 return 0;
6904 }
6905 cs = &cu->producer[strlen ("GNU ")];
6906 while (*cs && !isdigit (*cs))
6907 cs++;
6908 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6909 {
6910 /* Not recognized as GCC. */
6911
6912 return 0;
6913 }
6914
6915 return major < 4 || (major == 4 && minor < 6);
6916}
6917
6918/* Return the default accessibility type if it is not overriden by
6919 DW_AT_accessibility. */
6920
6921static enum dwarf_access_attribute
6922dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6923{
6924 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6925 {
6926 /* The default DWARF 2 accessibility for members is public, the default
6927 accessibility for inheritance is private. */
6928
6929 if (die->tag != DW_TAG_inheritance)
6930 return DW_ACCESS_public;
6931 else
6932 return DW_ACCESS_private;
6933 }
6934 else
6935 {
6936 /* DWARF 3+ defines the default accessibility a different way. The same
6937 rules apply now for DW_TAG_inheritance as for the members and it only
6938 depends on the container kind. */
6939
6940 if (die->parent->tag == DW_TAG_class_type)
6941 return DW_ACCESS_private;
6942 else
6943 return DW_ACCESS_public;
6944 }
6945}
6946
74ac6d43
TT
6947/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6948 offset. If the attribute was not found return 0, otherwise return
6949 1. If it was found but could not properly be handled, set *OFFSET
6950 to 0. */
6951
6952static int
6953handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6954 LONGEST *offset)
6955{
6956 struct attribute *attr;
6957
6958 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6959 if (attr != NULL)
6960 {
6961 *offset = 0;
6962
6963 /* Note that we do not check for a section offset first here.
6964 This is because DW_AT_data_member_location is new in DWARF 4,
6965 so if we see it, we can assume that a constant form is really
6966 a constant and not a section offset. */
6967 if (attr_form_is_constant (attr))
6968 *offset = dwarf2_get_attr_constant_value (attr, 0);
6969 else if (attr_form_is_section_offset (attr))
6970 dwarf2_complex_location_expr_complaint ();
6971 else if (attr_form_is_block (attr))
6972 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6973 else
6974 dwarf2_complex_location_expr_complaint ();
6975
6976 return 1;
6977 }
6978
6979 return 0;
6980}
6981
c906108c
SS
6982/* Add an aggregate field to the field list. */
6983
6984static void
107d2387 6985dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6986 struct dwarf2_cu *cu)
6e70227d 6987{
e7c27a73 6988 struct objfile *objfile = cu->objfile;
5e2b427d 6989 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6990 struct nextfield *new_field;
6991 struct attribute *attr;
6992 struct field *fp;
6993 char *fieldname = "";
6994
6995 /* Allocate a new field list entry and link it in. */
6996 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6997 make_cleanup (xfree, new_field);
c906108c 6998 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6999
7000 if (die->tag == DW_TAG_inheritance)
7001 {
7002 new_field->next = fip->baseclasses;
7003 fip->baseclasses = new_field;
7004 }
7005 else
7006 {
7007 new_field->next = fip->fields;
7008 fip->fields = new_field;
7009 }
c906108c
SS
7010 fip->nfields++;
7011
e142c38c 7012 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7013 if (attr)
7014 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7015 else
7016 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7017 if (new_field->accessibility != DW_ACCESS_public)
7018 fip->non_public_fields = 1;
60d5a603 7019
e142c38c 7020 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7021 if (attr)
7022 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7023 else
7024 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7025
7026 fp = &new_field->field;
a9a9bd0f 7027
e142c38c 7028 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7029 {
74ac6d43
TT
7030 LONGEST offset;
7031
a9a9bd0f 7032 /* Data member other than a C++ static data member. */
6e70227d 7033
c906108c 7034 /* Get type of field. */
e7c27a73 7035 fp->type = die_type (die, cu);
c906108c 7036
d6a843b5 7037 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7038
c906108c 7039 /* Get bit size of field (zero if none). */
e142c38c 7040 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7041 if (attr)
7042 {
7043 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7044 }
7045 else
7046 {
7047 FIELD_BITSIZE (*fp) = 0;
7048 }
7049
7050 /* Get bit offset of field. */
74ac6d43
TT
7051 if (handle_data_member_location (die, cu, &offset))
7052 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7053 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7054 if (attr)
7055 {
5e2b427d 7056 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7057 {
7058 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7059 additional bit offset from the MSB of the containing
7060 anonymous object to the MSB of the field. We don't
7061 have to do anything special since we don't need to
7062 know the size of the anonymous object. */
c906108c
SS
7063 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7064 }
7065 else
7066 {
7067 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7068 MSB of the anonymous object, subtract off the number of
7069 bits from the MSB of the field to the MSB of the
7070 object, and then subtract off the number of bits of
7071 the field itself. The result is the bit offset of
7072 the LSB of the field. */
c906108c
SS
7073 int anonymous_size;
7074 int bit_offset = DW_UNSND (attr);
7075
e142c38c 7076 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7077 if (attr)
7078 {
7079 /* The size of the anonymous object containing
7080 the bit field is explicit, so use the
7081 indicated size (in bytes). */
7082 anonymous_size = DW_UNSND (attr);
7083 }
7084 else
7085 {
7086 /* The size of the anonymous object containing
7087 the bit field must be inferred from the type
7088 attribute of the data member containing the
7089 bit field. */
7090 anonymous_size = TYPE_LENGTH (fp->type);
7091 }
7092 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7093 - bit_offset - FIELD_BITSIZE (*fp);
7094 }
7095 }
7096
7097 /* Get name of field. */
39cbfefa
DJ
7098 fieldname = dwarf2_name (die, cu);
7099 if (fieldname == NULL)
7100 fieldname = "";
d8151005
DJ
7101
7102 /* The name is already allocated along with this objfile, so we don't
7103 need to duplicate it for the type. */
7104 fp->name = fieldname;
c906108c
SS
7105
7106 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7107 pointer or virtual base class pointer) to private. */
e142c38c 7108 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7109 {
d48cc9dd 7110 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7111 new_field->accessibility = DW_ACCESS_private;
7112 fip->non_public_fields = 1;
7113 }
7114 }
a9a9bd0f 7115 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7116 {
a9a9bd0f
DC
7117 /* C++ static member. */
7118
7119 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7120 is a declaration, but all versions of G++ as of this writing
7121 (so through at least 3.2.1) incorrectly generate
7122 DW_TAG_variable tags. */
6e70227d 7123
ff355380 7124 const char *physname;
c906108c 7125
a9a9bd0f 7126 /* Get name of field. */
39cbfefa
DJ
7127 fieldname = dwarf2_name (die, cu);
7128 if (fieldname == NULL)
c906108c
SS
7129 return;
7130
254e6b9e 7131 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7132 if (attr
7133 /* Only create a symbol if this is an external value.
7134 new_symbol checks this and puts the value in the global symbol
7135 table, which we want. If it is not external, new_symbol
7136 will try to put the value in cu->list_in_scope which is wrong. */
7137 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7138 {
7139 /* A static const member, not much different than an enum as far as
7140 we're concerned, except that we can support more types. */
7141 new_symbol (die, NULL, cu);
7142 }
7143
2df3850c 7144 /* Get physical name. */
ff355380 7145 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7146
d8151005
DJ
7147 /* The name is already allocated along with this objfile, so we don't
7148 need to duplicate it for the type. */
7149 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7150 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7151 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7152 }
7153 else if (die->tag == DW_TAG_inheritance)
7154 {
74ac6d43 7155 LONGEST offset;
d4b96c9a 7156
74ac6d43
TT
7157 /* C++ base class field. */
7158 if (handle_data_member_location (die, cu, &offset))
7159 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7160 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7161 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7162 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7163 fip->nbaseclasses++;
7164 }
7165}
7166
98751a41
JK
7167/* Add a typedef defined in the scope of the FIP's class. */
7168
7169static void
7170dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7171 struct dwarf2_cu *cu)
6e70227d 7172{
98751a41 7173 struct objfile *objfile = cu->objfile;
98751a41
JK
7174 struct typedef_field_list *new_field;
7175 struct attribute *attr;
7176 struct typedef_field *fp;
7177 char *fieldname = "";
7178
7179 /* Allocate a new field list entry and link it in. */
7180 new_field = xzalloc (sizeof (*new_field));
7181 make_cleanup (xfree, new_field);
7182
7183 gdb_assert (die->tag == DW_TAG_typedef);
7184
7185 fp = &new_field->field;
7186
7187 /* Get name of field. */
7188 fp->name = dwarf2_name (die, cu);
7189 if (fp->name == NULL)
7190 return;
7191
7192 fp->type = read_type_die (die, cu);
7193
7194 new_field->next = fip->typedef_field_list;
7195 fip->typedef_field_list = new_field;
7196 fip->typedef_field_list_count++;
7197}
7198
c906108c
SS
7199/* Create the vector of fields, and attach it to the type. */
7200
7201static void
fba45db2 7202dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7203 struct dwarf2_cu *cu)
c906108c
SS
7204{
7205 int nfields = fip->nfields;
7206
7207 /* Record the field count, allocate space for the array of fields,
7208 and create blank accessibility bitfields if necessary. */
7209 TYPE_NFIELDS (type) = nfields;
7210 TYPE_FIELDS (type) = (struct field *)
7211 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7212 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7213
b4ba55a1 7214 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7215 {
7216 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7217
7218 TYPE_FIELD_PRIVATE_BITS (type) =
7219 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7220 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7221
7222 TYPE_FIELD_PROTECTED_BITS (type) =
7223 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7224 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7225
774b6a14
TT
7226 TYPE_FIELD_IGNORE_BITS (type) =
7227 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7228 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7229 }
7230
7231 /* If the type has baseclasses, allocate and clear a bit vector for
7232 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7233 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7234 {
7235 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7236 unsigned char *pointer;
c906108c
SS
7237
7238 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7239 pointer = TYPE_ALLOC (type, num_bytes);
7240 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7241 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7242 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7243 }
7244
3e43a32a
MS
7245 /* Copy the saved-up fields into the field vector. Start from the head of
7246 the list, adding to the tail of the field array, so that they end up in
7247 the same order in the array in which they were added to the list. */
c906108c
SS
7248 while (nfields-- > 0)
7249 {
7d0ccb61
DJ
7250 struct nextfield *fieldp;
7251
7252 if (fip->fields)
7253 {
7254 fieldp = fip->fields;
7255 fip->fields = fieldp->next;
7256 }
7257 else
7258 {
7259 fieldp = fip->baseclasses;
7260 fip->baseclasses = fieldp->next;
7261 }
7262
7263 TYPE_FIELD (type, nfields) = fieldp->field;
7264 switch (fieldp->accessibility)
c906108c 7265 {
c5aa993b 7266 case DW_ACCESS_private:
b4ba55a1
JB
7267 if (cu->language != language_ada)
7268 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7269 break;
c906108c 7270
c5aa993b 7271 case DW_ACCESS_protected:
b4ba55a1
JB
7272 if (cu->language != language_ada)
7273 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7274 break;
c906108c 7275
c5aa993b
JM
7276 case DW_ACCESS_public:
7277 break;
c906108c 7278
c5aa993b
JM
7279 default:
7280 /* Unknown accessibility. Complain and treat it as public. */
7281 {
e2e0b3e5 7282 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7283 fieldp->accessibility);
c5aa993b
JM
7284 }
7285 break;
c906108c
SS
7286 }
7287 if (nfields < fip->nbaseclasses)
7288 {
7d0ccb61 7289 switch (fieldp->virtuality)
c906108c 7290 {
c5aa993b
JM
7291 case DW_VIRTUALITY_virtual:
7292 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7293 if (cu->language == language_ada)
a73c6dcd 7294 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7295 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7296 break;
c906108c
SS
7297 }
7298 }
c906108c
SS
7299 }
7300}
7301
c906108c
SS
7302/* Add a member function to the proper fieldlist. */
7303
7304static void
107d2387 7305dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7306 struct type *type, struct dwarf2_cu *cu)
c906108c 7307{
e7c27a73 7308 struct objfile *objfile = cu->objfile;
c906108c
SS
7309 struct attribute *attr;
7310 struct fnfieldlist *flp;
7311 int i;
7312 struct fn_field *fnp;
7313 char *fieldname;
c906108c 7314 struct nextfnfield *new_fnfield;
f792889a 7315 struct type *this_type;
60d5a603 7316 enum dwarf_access_attribute accessibility;
c906108c 7317
b4ba55a1 7318 if (cu->language == language_ada)
a73c6dcd 7319 error (_("unexpected member function in Ada type"));
b4ba55a1 7320
2df3850c 7321 /* Get name of member function. */
39cbfefa
DJ
7322 fieldname = dwarf2_name (die, cu);
7323 if (fieldname == NULL)
2df3850c 7324 return;
c906108c 7325
c906108c
SS
7326 /* Look up member function name in fieldlist. */
7327 for (i = 0; i < fip->nfnfields; i++)
7328 {
27bfe10e 7329 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7330 break;
7331 }
7332
7333 /* Create new list element if necessary. */
7334 if (i < fip->nfnfields)
7335 flp = &fip->fnfieldlists[i];
7336 else
7337 {
7338 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7339 {
7340 fip->fnfieldlists = (struct fnfieldlist *)
7341 xrealloc (fip->fnfieldlists,
7342 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7343 * sizeof (struct fnfieldlist));
c906108c 7344 if (fip->nfnfields == 0)
c13c43fd 7345 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7346 }
7347 flp = &fip->fnfieldlists[fip->nfnfields];
7348 flp->name = fieldname;
7349 flp->length = 0;
7350 flp->head = NULL;
3da10d80 7351 i = fip->nfnfields++;
c906108c
SS
7352 }
7353
7354 /* Create a new member function field and chain it to the field list
0963b4bd 7355 entry. */
c906108c 7356 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7357 make_cleanup (xfree, new_fnfield);
c906108c
SS
7358 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7359 new_fnfield->next = flp->head;
7360 flp->head = new_fnfield;
7361 flp->length++;
7362
7363 /* Fill in the member function field info. */
7364 fnp = &new_fnfield->fnfield;
3da10d80
KS
7365
7366 /* Delay processing of the physname until later. */
7367 if (cu->language == language_cplus || cu->language == language_java)
7368 {
7369 add_to_method_list (type, i, flp->length - 1, fieldname,
7370 die, cu);
7371 }
7372 else
7373 {
1d06ead6 7374 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7375 fnp->physname = physname ? physname : "";
7376 }
7377
c906108c 7378 fnp->type = alloc_type (objfile);
f792889a
DJ
7379 this_type = read_type_die (die, cu);
7380 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7381 {
f792889a 7382 int nparams = TYPE_NFIELDS (this_type);
c906108c 7383
f792889a 7384 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7385 of the method itself (TYPE_CODE_METHOD). */
7386 smash_to_method_type (fnp->type, type,
f792889a
DJ
7387 TYPE_TARGET_TYPE (this_type),
7388 TYPE_FIELDS (this_type),
7389 TYPE_NFIELDS (this_type),
7390 TYPE_VARARGS (this_type));
c906108c
SS
7391
7392 /* Handle static member functions.
c5aa993b 7393 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7394 member functions. G++ helps GDB by marking the first
7395 parameter for non-static member functions (which is the this
7396 pointer) as artificial. We obtain this information from
7397 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7398 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7399 fnp->voffset = VOFFSET_STATIC;
7400 }
7401 else
e2e0b3e5 7402 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7403 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7404
7405 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7406 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7407 fnp->fcontext = die_containing_type (die, cu);
c906108c 7408
3e43a32a
MS
7409 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7410 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7411
7412 /* Get accessibility. */
e142c38c 7413 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7414 if (attr)
60d5a603
JK
7415 accessibility = DW_UNSND (attr);
7416 else
7417 accessibility = dwarf2_default_access_attribute (die, cu);
7418 switch (accessibility)
c906108c 7419 {
60d5a603
JK
7420 case DW_ACCESS_private:
7421 fnp->is_private = 1;
7422 break;
7423 case DW_ACCESS_protected:
7424 fnp->is_protected = 1;
7425 break;
c906108c
SS
7426 }
7427
b02dede2 7428 /* Check for artificial methods. */
e142c38c 7429 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7430 if (attr && DW_UNSND (attr) != 0)
7431 fnp->is_artificial = 1;
7432
0d564a31 7433 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7434 function. For older versions of GCC, this is an offset in the
7435 appropriate virtual table, as specified by DW_AT_containing_type.
7436 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7437 to the object address. */
7438
e142c38c 7439 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7440 if (attr)
8e19ed76 7441 {
aec5aa8b 7442 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7443 {
aec5aa8b
TT
7444 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7445 {
7446 /* Old-style GCC. */
7447 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7448 }
7449 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7450 || (DW_BLOCK (attr)->size > 1
7451 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7452 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7453 {
7454 struct dwarf_block blk;
7455 int offset;
7456
7457 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7458 ? 1 : 2);
7459 blk.size = DW_BLOCK (attr)->size - offset;
7460 blk.data = DW_BLOCK (attr)->data + offset;
7461 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7462 if ((fnp->voffset % cu->header.addr_size) != 0)
7463 dwarf2_complex_location_expr_complaint ();
7464 else
7465 fnp->voffset /= cu->header.addr_size;
7466 fnp->voffset += 2;
7467 }
7468 else
7469 dwarf2_complex_location_expr_complaint ();
7470
7471 if (!fnp->fcontext)
7472 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7473 }
3690dd37 7474 else if (attr_form_is_section_offset (attr))
8e19ed76 7475 {
4d3c2250 7476 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7477 }
7478 else
7479 {
4d3c2250
KB
7480 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7481 fieldname);
8e19ed76 7482 }
0d564a31 7483 }
d48cc9dd
DJ
7484 else
7485 {
7486 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7487 if (attr && DW_UNSND (attr))
7488 {
7489 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7490 complaint (&symfile_complaints,
3e43a32a
MS
7491 _("Member function \"%s\" (offset %d) is virtual "
7492 "but the vtable offset is not specified"),
d48cc9dd 7493 fieldname, die->offset);
9655fd1a 7494 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7495 TYPE_CPLUS_DYNAMIC (type) = 1;
7496 }
7497 }
c906108c
SS
7498}
7499
7500/* Create the vector of member function fields, and attach it to the type. */
7501
7502static void
fba45db2 7503dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7504 struct dwarf2_cu *cu)
c906108c
SS
7505{
7506 struct fnfieldlist *flp;
7507 int total_length = 0;
7508 int i;
7509
b4ba55a1 7510 if (cu->language == language_ada)
a73c6dcd 7511 error (_("unexpected member functions in Ada type"));
b4ba55a1 7512
c906108c
SS
7513 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7514 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7515 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7516
7517 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7518 {
7519 struct nextfnfield *nfp = flp->head;
7520 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7521 int k;
7522
7523 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7524 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7525 fn_flp->fn_fields = (struct fn_field *)
7526 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7527 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7528 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7529
7530 total_length += flp->length;
7531 }
7532
7533 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7534 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7535}
7536
1168df01
JB
7537/* Returns non-zero if NAME is the name of a vtable member in CU's
7538 language, zero otherwise. */
7539static int
7540is_vtable_name (const char *name, struct dwarf2_cu *cu)
7541{
7542 static const char vptr[] = "_vptr";
987504bb 7543 static const char vtable[] = "vtable";
1168df01 7544
987504bb
JJ
7545 /* Look for the C++ and Java forms of the vtable. */
7546 if ((cu->language == language_java
7547 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7548 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7549 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7550 return 1;
7551
7552 return 0;
7553}
7554
c0dd20ea 7555/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7556 functions, with the ABI-specified layout. If TYPE describes
7557 such a structure, smash it into a member function type.
61049d3b
DJ
7558
7559 GCC shouldn't do this; it should just output pointer to member DIEs.
7560 This is GCC PR debug/28767. */
c0dd20ea 7561
0b92b5bb
TT
7562static void
7563quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7564{
0b92b5bb 7565 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7566
7567 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7568 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7569 return;
c0dd20ea
DJ
7570
7571 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7572 if (TYPE_FIELD_NAME (type, 0) == NULL
7573 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7574 || TYPE_FIELD_NAME (type, 1) == NULL
7575 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7576 return;
c0dd20ea
DJ
7577
7578 /* Find the type of the method. */
0b92b5bb 7579 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7580 if (pfn_type == NULL
7581 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7582 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7583 return;
c0dd20ea
DJ
7584
7585 /* Look for the "this" argument. */
7586 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7587 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7588 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7589 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7590 return;
c0dd20ea
DJ
7591
7592 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7593 new_type = alloc_type (objfile);
7594 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7595 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7596 TYPE_VARARGS (pfn_type));
0b92b5bb 7597 smash_to_methodptr_type (type, new_type);
c0dd20ea 7598}
1168df01 7599
c906108c 7600/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7601 (definition) to create a type for the structure or union. Fill in
7602 the type's name and general properties; the members will not be
7603 processed until process_structure_type.
c906108c 7604
c767944b
DJ
7605 NOTE: we need to call these functions regardless of whether or not the
7606 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7607 structure or union. This gets the type entered into our set of
7608 user defined types.
7609
7610 However, if the structure is incomplete (an opaque struct/union)
7611 then suppress creating a symbol table entry for it since gdb only
7612 wants to find the one with the complete definition. Note that if
7613 it is complete, we just call new_symbol, which does it's own
7614 checking about whether the struct/union is anonymous or not (and
7615 suppresses creating a symbol table entry itself). */
7616
f792889a 7617static struct type *
134d01f1 7618read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7619{
e7c27a73 7620 struct objfile *objfile = cu->objfile;
c906108c
SS
7621 struct type *type;
7622 struct attribute *attr;
39cbfefa 7623 char *name;
c906108c 7624
348e048f
DE
7625 /* If the definition of this type lives in .debug_types, read that type.
7626 Don't follow DW_AT_specification though, that will take us back up
7627 the chain and we want to go down. */
7628 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7629 if (attr)
7630 {
7631 struct dwarf2_cu *type_cu = cu;
7632 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7633
348e048f
DE
7634 /* We could just recurse on read_structure_type, but we need to call
7635 get_die_type to ensure only one type for this DIE is created.
7636 This is important, for example, because for c++ classes we need
7637 TYPE_NAME set which is only done by new_symbol. Blech. */
7638 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7639
7640 /* TYPE_CU may not be the same as CU.
7641 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7642 return set_die_type (die, type, cu);
7643 }
7644
c0dd20ea 7645 type = alloc_type (objfile);
c906108c 7646 INIT_CPLUS_SPECIFIC (type);
93311388 7647
39cbfefa
DJ
7648 name = dwarf2_name (die, cu);
7649 if (name != NULL)
c906108c 7650 {
987504bb
JJ
7651 if (cu->language == language_cplus
7652 || cu->language == language_java)
63d06c5c 7653 {
3da10d80
KS
7654 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7655
7656 /* dwarf2_full_name might have already finished building the DIE's
7657 type. If so, there is no need to continue. */
7658 if (get_die_type (die, cu) != NULL)
7659 return get_die_type (die, cu);
7660
7661 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7662 if (die->tag == DW_TAG_structure_type
7663 || die->tag == DW_TAG_class_type)
7664 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7665 }
7666 else
7667 {
d8151005
DJ
7668 /* The name is already allocated along with this objfile, so
7669 we don't need to duplicate it for the type. */
94af9270
KS
7670 TYPE_TAG_NAME (type) = (char *) name;
7671 if (die->tag == DW_TAG_class_type)
7672 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7673 }
c906108c
SS
7674 }
7675
7676 if (die->tag == DW_TAG_structure_type)
7677 {
7678 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7679 }
7680 else if (die->tag == DW_TAG_union_type)
7681 {
7682 TYPE_CODE (type) = TYPE_CODE_UNION;
7683 }
7684 else
7685 {
c906108c
SS
7686 TYPE_CODE (type) = TYPE_CODE_CLASS;
7687 }
7688
0cc2414c
TT
7689 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7690 TYPE_DECLARED_CLASS (type) = 1;
7691
e142c38c 7692 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7693 if (attr)
7694 {
7695 TYPE_LENGTH (type) = DW_UNSND (attr);
7696 }
7697 else
7698 {
7699 TYPE_LENGTH (type) = 0;
7700 }
7701
876cecd0 7702 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7703 if (die_is_declaration (die, cu))
876cecd0 7704 TYPE_STUB (type) = 1;
a6c727b2
DJ
7705 else if (attr == NULL && die->child == NULL
7706 && producer_is_realview (cu->producer))
7707 /* RealView does not output the required DW_AT_declaration
7708 on incomplete types. */
7709 TYPE_STUB (type) = 1;
dc718098 7710
c906108c
SS
7711 /* We need to add the type field to the die immediately so we don't
7712 infinitely recurse when dealing with pointers to the structure
0963b4bd 7713 type within the structure itself. */
1c379e20 7714 set_die_type (die, type, cu);
c906108c 7715
7e314c57
JK
7716 /* set_die_type should be already done. */
7717 set_descriptive_type (type, die, cu);
7718
c767944b
DJ
7719 return type;
7720}
7721
7722/* Finish creating a structure or union type, including filling in
7723 its members and creating a symbol for it. */
7724
7725static void
7726process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7727{
7728 struct objfile *objfile = cu->objfile;
7729 struct die_info *child_die = die->child;
7730 struct type *type;
7731
7732 type = get_die_type (die, cu);
7733 if (type == NULL)
7734 type = read_structure_type (die, cu);
7735
e142c38c 7736 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7737 {
7738 struct field_info fi;
7739 struct die_info *child_die;
34eaf542 7740 VEC (symbolp) *template_args = NULL;
c767944b 7741 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7742
7743 memset (&fi, 0, sizeof (struct field_info));
7744
639d11d3 7745 child_die = die->child;
c906108c
SS
7746
7747 while (child_die && child_die->tag)
7748 {
a9a9bd0f
DC
7749 if (child_die->tag == DW_TAG_member
7750 || child_die->tag == DW_TAG_variable)
c906108c 7751 {
a9a9bd0f
DC
7752 /* NOTE: carlton/2002-11-05: A C++ static data member
7753 should be a DW_TAG_member that is a declaration, but
7754 all versions of G++ as of this writing (so through at
7755 least 3.2.1) incorrectly generate DW_TAG_variable
7756 tags for them instead. */
e7c27a73 7757 dwarf2_add_field (&fi, child_die, cu);
c906108c 7758 }
8713b1b1 7759 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7760 {
0963b4bd 7761 /* C++ member function. */
e7c27a73 7762 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7763 }
7764 else if (child_die->tag == DW_TAG_inheritance)
7765 {
7766 /* C++ base class field. */
e7c27a73 7767 dwarf2_add_field (&fi, child_die, cu);
c906108c 7768 }
98751a41
JK
7769 else if (child_die->tag == DW_TAG_typedef)
7770 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7771 else if (child_die->tag == DW_TAG_template_type_param
7772 || child_die->tag == DW_TAG_template_value_param)
7773 {
7774 struct symbol *arg = new_symbol (child_die, NULL, cu);
7775
f1078f66
DJ
7776 if (arg != NULL)
7777 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7778 }
7779
c906108c
SS
7780 child_die = sibling_die (child_die);
7781 }
7782
34eaf542
TT
7783 /* Attach template arguments to type. */
7784 if (! VEC_empty (symbolp, template_args))
7785 {
7786 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7787 TYPE_N_TEMPLATE_ARGUMENTS (type)
7788 = VEC_length (symbolp, template_args);
7789 TYPE_TEMPLATE_ARGUMENTS (type)
7790 = obstack_alloc (&objfile->objfile_obstack,
7791 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7792 * sizeof (struct symbol *)));
7793 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7794 VEC_address (symbolp, template_args),
7795 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7796 * sizeof (struct symbol *)));
7797 VEC_free (symbolp, template_args);
7798 }
7799
c906108c
SS
7800 /* Attach fields and member functions to the type. */
7801 if (fi.nfields)
e7c27a73 7802 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7803 if (fi.nfnfields)
7804 {
e7c27a73 7805 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7806
c5aa993b 7807 /* Get the type which refers to the base class (possibly this
c906108c 7808 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7809 class from the DW_AT_containing_type attribute. This use of
7810 DW_AT_containing_type is a GNU extension. */
c906108c 7811
e142c38c 7812 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7813 {
e7c27a73 7814 struct type *t = die_containing_type (die, cu);
c906108c
SS
7815
7816 TYPE_VPTR_BASETYPE (type) = t;
7817 if (type == t)
7818 {
c906108c
SS
7819 int i;
7820
7821 /* Our own class provides vtbl ptr. */
7822 for (i = TYPE_NFIELDS (t) - 1;
7823 i >= TYPE_N_BASECLASSES (t);
7824 --i)
7825 {
7826 char *fieldname = TYPE_FIELD_NAME (t, i);
7827
1168df01 7828 if (is_vtable_name (fieldname, cu))
c906108c
SS
7829 {
7830 TYPE_VPTR_FIELDNO (type) = i;
7831 break;
7832 }
7833 }
7834
7835 /* Complain if virtual function table field not found. */
7836 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7837 complaint (&symfile_complaints,
3e43a32a
MS
7838 _("virtual function table pointer "
7839 "not found when defining class '%s'"),
4d3c2250
KB
7840 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7841 "");
c906108c
SS
7842 }
7843 else
7844 {
7845 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7846 }
7847 }
f6235d4c
EZ
7848 else if (cu->producer
7849 && strncmp (cu->producer,
7850 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7851 {
7852 /* The IBM XLC compiler does not provide direct indication
7853 of the containing type, but the vtable pointer is
7854 always named __vfp. */
7855
7856 int i;
7857
7858 for (i = TYPE_NFIELDS (type) - 1;
7859 i >= TYPE_N_BASECLASSES (type);
7860 --i)
7861 {
7862 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7863 {
7864 TYPE_VPTR_FIELDNO (type) = i;
7865 TYPE_VPTR_BASETYPE (type) = type;
7866 break;
7867 }
7868 }
7869 }
c906108c 7870 }
98751a41
JK
7871
7872 /* Copy fi.typedef_field_list linked list elements content into the
7873 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7874 if (fi.typedef_field_list)
7875 {
7876 int i = fi.typedef_field_list_count;
7877
a0d7a4ff 7878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7879 TYPE_TYPEDEF_FIELD_ARRAY (type)
7880 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7881 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7882
7883 /* Reverse the list order to keep the debug info elements order. */
7884 while (--i >= 0)
7885 {
7886 struct typedef_field *dest, *src;
6e70227d 7887
98751a41
JK
7888 dest = &TYPE_TYPEDEF_FIELD (type, i);
7889 src = &fi.typedef_field_list->field;
7890 fi.typedef_field_list = fi.typedef_field_list->next;
7891 *dest = *src;
7892 }
7893 }
c767944b
DJ
7894
7895 do_cleanups (back_to);
eb2a6f42
TT
7896
7897 if (HAVE_CPLUS_STRUCT (type))
7898 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7899 }
63d06c5c 7900
bb5ed363 7901 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7902
90aeadfc
DC
7903 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7904 snapshots) has been known to create a die giving a declaration
7905 for a class that has, as a child, a die giving a definition for a
7906 nested class. So we have to process our children even if the
7907 current die is a declaration. Normally, of course, a declaration
7908 won't have any children at all. */
134d01f1 7909
90aeadfc
DC
7910 while (child_die != NULL && child_die->tag)
7911 {
7912 if (child_die->tag == DW_TAG_member
7913 || child_die->tag == DW_TAG_variable
34eaf542
TT
7914 || child_die->tag == DW_TAG_inheritance
7915 || child_die->tag == DW_TAG_template_value_param
7916 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7917 {
90aeadfc 7918 /* Do nothing. */
134d01f1 7919 }
90aeadfc
DC
7920 else
7921 process_die (child_die, cu);
134d01f1 7922
90aeadfc 7923 child_die = sibling_die (child_die);
134d01f1
DJ
7924 }
7925
fa4028e9
JB
7926 /* Do not consider external references. According to the DWARF standard,
7927 these DIEs are identified by the fact that they have no byte_size
7928 attribute, and a declaration attribute. */
7929 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7930 || !die_is_declaration (die, cu))
c767944b 7931 new_symbol (die, type, cu);
134d01f1
DJ
7932}
7933
7934/* Given a DW_AT_enumeration_type die, set its type. We do not
7935 complete the type's fields yet, or create any symbols. */
c906108c 7936
f792889a 7937static struct type *
134d01f1 7938read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7939{
e7c27a73 7940 struct objfile *objfile = cu->objfile;
c906108c 7941 struct type *type;
c906108c 7942 struct attribute *attr;
0114d602 7943 const char *name;
134d01f1 7944
348e048f
DE
7945 /* If the definition of this type lives in .debug_types, read that type.
7946 Don't follow DW_AT_specification though, that will take us back up
7947 the chain and we want to go down. */
7948 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7949 if (attr)
7950 {
7951 struct dwarf2_cu *type_cu = cu;
7952 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7953
348e048f 7954 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7955
7956 /* TYPE_CU may not be the same as CU.
7957 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7958 return set_die_type (die, type, cu);
7959 }
7960
c906108c
SS
7961 type = alloc_type (objfile);
7962
7963 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7964 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7965 if (name != NULL)
0114d602 7966 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7967
e142c38c 7968 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7969 if (attr)
7970 {
7971 TYPE_LENGTH (type) = DW_UNSND (attr);
7972 }
7973 else
7974 {
7975 TYPE_LENGTH (type) = 0;
7976 }
7977
137033e9
JB
7978 /* The enumeration DIE can be incomplete. In Ada, any type can be
7979 declared as private in the package spec, and then defined only
7980 inside the package body. Such types are known as Taft Amendment
7981 Types. When another package uses such a type, an incomplete DIE
7982 may be generated by the compiler. */
02eb380e 7983 if (die_is_declaration (die, cu))
876cecd0 7984 TYPE_STUB (type) = 1;
02eb380e 7985
f792889a 7986 return set_die_type (die, type, cu);
134d01f1
DJ
7987}
7988
7989/* Given a pointer to a die which begins an enumeration, process all
7990 the dies that define the members of the enumeration, and create the
7991 symbol for the enumeration type.
7992
7993 NOTE: We reverse the order of the element list. */
7994
7995static void
7996process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7997{
f792889a 7998 struct type *this_type;
134d01f1 7999
f792889a
DJ
8000 this_type = get_die_type (die, cu);
8001 if (this_type == NULL)
8002 this_type = read_enumeration_type (die, cu);
9dc481d3 8003
639d11d3 8004 if (die->child != NULL)
c906108c 8005 {
9dc481d3
DE
8006 struct die_info *child_die;
8007 struct symbol *sym;
8008 struct field *fields = NULL;
8009 int num_fields = 0;
8010 int unsigned_enum = 1;
8011 char *name;
8012
639d11d3 8013 child_die = die->child;
c906108c
SS
8014 while (child_die && child_die->tag)
8015 {
8016 if (child_die->tag != DW_TAG_enumerator)
8017 {
e7c27a73 8018 process_die (child_die, cu);
c906108c
SS
8019 }
8020 else
8021 {
39cbfefa
DJ
8022 name = dwarf2_name (child_die, cu);
8023 if (name)
c906108c 8024 {
f792889a 8025 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
8026 if (SYMBOL_VALUE (sym) < 0)
8027 unsigned_enum = 0;
8028
8029 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8030 {
8031 fields = (struct field *)
8032 xrealloc (fields,
8033 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8034 * sizeof (struct field));
c906108c
SS
8035 }
8036
3567439c 8037 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8038 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8039 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8040 FIELD_BITSIZE (fields[num_fields]) = 0;
8041
8042 num_fields++;
8043 }
8044 }
8045
8046 child_die = sibling_die (child_die);
8047 }
8048
8049 if (num_fields)
8050 {
f792889a
DJ
8051 TYPE_NFIELDS (this_type) = num_fields;
8052 TYPE_FIELDS (this_type) = (struct field *)
8053 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8054 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8055 sizeof (struct field) * num_fields);
b8c9b27d 8056 xfree (fields);
c906108c
SS
8057 }
8058 if (unsigned_enum)
876cecd0 8059 TYPE_UNSIGNED (this_type) = 1;
c906108c 8060 }
134d01f1 8061
6c83ed52
TT
8062 /* If we are reading an enum from a .debug_types unit, and the enum
8063 is a declaration, and the enum is not the signatured type in the
8064 unit, then we do not want to add a symbol for it. Adding a
8065 symbol would in some cases obscure the true definition of the
8066 enum, giving users an incomplete type when the definition is
8067 actually available. Note that we do not want to do this for all
8068 enums which are just declarations, because C++0x allows forward
8069 enum declarations. */
b0df02fd 8070 if (cu->per_cu->debug_types_section
6c83ed52
TT
8071 && die_is_declaration (die, cu))
8072 {
8073 struct signatured_type *type_sig;
8074
8075 type_sig
8076 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8077 cu->per_cu->debug_types_section,
6c83ed52
TT
8078 cu->per_cu->offset);
8079 if (type_sig->type_offset != die->offset)
8080 return;
8081 }
8082
f792889a 8083 new_symbol (die, this_type, cu);
c906108c
SS
8084}
8085
8086/* Extract all information from a DW_TAG_array_type DIE and put it in
8087 the DIE's type field. For now, this only handles one dimensional
8088 arrays. */
8089
f792889a 8090static struct type *
e7c27a73 8091read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8092{
e7c27a73 8093 struct objfile *objfile = cu->objfile;
c906108c 8094 struct die_info *child_die;
7e314c57 8095 struct type *type;
c906108c
SS
8096 struct type *element_type, *range_type, *index_type;
8097 struct type **range_types = NULL;
8098 struct attribute *attr;
8099 int ndim = 0;
8100 struct cleanup *back_to;
39cbfefa 8101 char *name;
c906108c 8102
e7c27a73 8103 element_type = die_type (die, cu);
c906108c 8104
7e314c57
JK
8105 /* The die_type call above may have already set the type for this DIE. */
8106 type = get_die_type (die, cu);
8107 if (type)
8108 return type;
8109
c906108c
SS
8110 /* Irix 6.2 native cc creates array types without children for
8111 arrays with unspecified length. */
639d11d3 8112 if (die->child == NULL)
c906108c 8113 {
46bf5051 8114 index_type = objfile_type (objfile)->builtin_int;
c906108c 8115 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8116 type = create_array_type (NULL, element_type, range_type);
8117 return set_die_type (die, type, cu);
c906108c
SS
8118 }
8119
8120 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8121 child_die = die->child;
c906108c
SS
8122 while (child_die && child_die->tag)
8123 {
8124 if (child_die->tag == DW_TAG_subrange_type)
8125 {
f792889a 8126 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8127
f792889a 8128 if (child_type != NULL)
a02abb62 8129 {
0963b4bd
MS
8130 /* The range type was succesfully read. Save it for the
8131 array type creation. */
a02abb62
JB
8132 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8133 {
8134 range_types = (struct type **)
8135 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8136 * sizeof (struct type *));
8137 if (ndim == 0)
8138 make_cleanup (free_current_contents, &range_types);
8139 }
f792889a 8140 range_types[ndim++] = child_type;
a02abb62 8141 }
c906108c
SS
8142 }
8143 child_die = sibling_die (child_die);
8144 }
8145
8146 /* Dwarf2 dimensions are output from left to right, create the
8147 necessary array types in backwards order. */
7ca2d3a3 8148
c906108c 8149 type = element_type;
7ca2d3a3
DL
8150
8151 if (read_array_order (die, cu) == DW_ORD_col_major)
8152 {
8153 int i = 0;
9a619af0 8154
7ca2d3a3
DL
8155 while (i < ndim)
8156 type = create_array_type (NULL, type, range_types[i++]);
8157 }
8158 else
8159 {
8160 while (ndim-- > 0)
8161 type = create_array_type (NULL, type, range_types[ndim]);
8162 }
c906108c 8163
f5f8a009
EZ
8164 /* Understand Dwarf2 support for vector types (like they occur on
8165 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8166 array type. This is not part of the Dwarf2/3 standard yet, but a
8167 custom vendor extension. The main difference between a regular
8168 array and the vector variant is that vectors are passed by value
8169 to functions. */
e142c38c 8170 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8171 if (attr)
ea37ba09 8172 make_vector_type (type);
f5f8a009 8173
dbc98a8b
KW
8174 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8175 implementation may choose to implement triple vectors using this
8176 attribute. */
8177 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8178 if (attr)
8179 {
8180 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8181 TYPE_LENGTH (type) = DW_UNSND (attr);
8182 else
3e43a32a
MS
8183 complaint (&symfile_complaints,
8184 _("DW_AT_byte_size for array type smaller "
8185 "than the total size of elements"));
dbc98a8b
KW
8186 }
8187
39cbfefa
DJ
8188 name = dwarf2_name (die, cu);
8189 if (name)
8190 TYPE_NAME (type) = name;
6e70227d 8191
0963b4bd 8192 /* Install the type in the die. */
7e314c57
JK
8193 set_die_type (die, type, cu);
8194
8195 /* set_die_type should be already done. */
b4ba55a1
JB
8196 set_descriptive_type (type, die, cu);
8197
c906108c
SS
8198 do_cleanups (back_to);
8199
7e314c57 8200 return type;
c906108c
SS
8201}
8202
7ca2d3a3 8203static enum dwarf_array_dim_ordering
6e70227d 8204read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8205{
8206 struct attribute *attr;
8207
8208 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8209
8210 if (attr) return DW_SND (attr);
8211
0963b4bd
MS
8212 /* GNU F77 is a special case, as at 08/2004 array type info is the
8213 opposite order to the dwarf2 specification, but data is still
8214 laid out as per normal fortran.
7ca2d3a3 8215
0963b4bd
MS
8216 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8217 version checking. */
7ca2d3a3 8218
905e0470
PM
8219 if (cu->language == language_fortran
8220 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8221 {
8222 return DW_ORD_row_major;
8223 }
8224
6e70227d 8225 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8226 {
8227 case array_column_major:
8228 return DW_ORD_col_major;
8229 case array_row_major:
8230 default:
8231 return DW_ORD_row_major;
8232 };
8233}
8234
72019c9c 8235/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8236 the DIE's type field. */
72019c9c 8237
f792889a 8238static struct type *
72019c9c
GM
8239read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8240{
7e314c57
JK
8241 struct type *domain_type, *set_type;
8242 struct attribute *attr;
f792889a 8243
7e314c57
JK
8244 domain_type = die_type (die, cu);
8245
8246 /* The die_type call above may have already set the type for this DIE. */
8247 set_type = get_die_type (die, cu);
8248 if (set_type)
8249 return set_type;
8250
8251 set_type = create_set_type (NULL, domain_type);
8252
8253 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8254 if (attr)
8255 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8256
f792889a 8257 return set_die_type (die, set_type, cu);
72019c9c 8258}
7ca2d3a3 8259
c906108c
SS
8260/* First cut: install each common block member as a global variable. */
8261
8262static void
e7c27a73 8263read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8264{
8265 struct die_info *child_die;
8266 struct attribute *attr;
8267 struct symbol *sym;
8268 CORE_ADDR base = (CORE_ADDR) 0;
8269
e142c38c 8270 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8271 if (attr)
8272 {
0963b4bd 8273 /* Support the .debug_loc offsets. */
8e19ed76
PS
8274 if (attr_form_is_block (attr))
8275 {
e7c27a73 8276 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8277 }
3690dd37 8278 else if (attr_form_is_section_offset (attr))
8e19ed76 8279 {
4d3c2250 8280 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8281 }
8282 else
8283 {
4d3c2250
KB
8284 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8285 "common block member");
8e19ed76 8286 }
c906108c 8287 }
639d11d3 8288 if (die->child != NULL)
c906108c 8289 {
639d11d3 8290 child_die = die->child;
c906108c
SS
8291 while (child_die && child_die->tag)
8292 {
74ac6d43
TT
8293 LONGEST offset;
8294
e7c27a73 8295 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8296 if (sym != NULL
8297 && handle_data_member_location (child_die, cu, &offset))
c906108c 8298 {
74ac6d43 8299 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8300 add_symbol_to_list (sym, &global_symbols);
8301 }
8302 child_die = sibling_die (child_die);
8303 }
8304 }
8305}
8306
0114d602 8307/* Create a type for a C++ namespace. */
d9fa45fe 8308
0114d602
DJ
8309static struct type *
8310read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8311{
e7c27a73 8312 struct objfile *objfile = cu->objfile;
0114d602 8313 const char *previous_prefix, *name;
9219021c 8314 int is_anonymous;
0114d602
DJ
8315 struct type *type;
8316
8317 /* For extensions, reuse the type of the original namespace. */
8318 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8319 {
8320 struct die_info *ext_die;
8321 struct dwarf2_cu *ext_cu = cu;
9a619af0 8322
0114d602
DJ
8323 ext_die = dwarf2_extension (die, &ext_cu);
8324 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8325
8326 /* EXT_CU may not be the same as CU.
8327 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8328 return set_die_type (die, type, cu);
8329 }
9219021c 8330
e142c38c 8331 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8332
8333 /* Now build the name of the current namespace. */
8334
0114d602
DJ
8335 previous_prefix = determine_prefix (die, cu);
8336 if (previous_prefix[0] != '\0')
8337 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8338 previous_prefix, name, 0, cu);
0114d602
DJ
8339
8340 /* Create the type. */
8341 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8342 objfile);
8343 TYPE_NAME (type) = (char *) name;
8344 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8345
60531b24 8346 return set_die_type (die, type, cu);
0114d602
DJ
8347}
8348
8349/* Read a C++ namespace. */
8350
8351static void
8352read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8353{
8354 struct objfile *objfile = cu->objfile;
0114d602 8355 int is_anonymous;
9219021c 8356
5c4e30ca
DC
8357 /* Add a symbol associated to this if we haven't seen the namespace
8358 before. Also, add a using directive if it's an anonymous
8359 namespace. */
9219021c 8360
f2f0e013 8361 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8362 {
8363 struct type *type;
8364
0114d602 8365 type = read_type_die (die, cu);
e7c27a73 8366 new_symbol (die, type, cu);
5c4e30ca 8367
e8e80198 8368 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8369 if (is_anonymous)
0114d602
DJ
8370 {
8371 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8372
c0cc3a76 8373 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8374 NULL, NULL, &objfile->objfile_obstack);
0114d602 8375 }
5c4e30ca 8376 }
9219021c 8377
639d11d3 8378 if (die->child != NULL)
d9fa45fe 8379 {
639d11d3 8380 struct die_info *child_die = die->child;
6e70227d 8381
d9fa45fe
DC
8382 while (child_die && child_die->tag)
8383 {
e7c27a73 8384 process_die (child_die, cu);
d9fa45fe
DC
8385 child_die = sibling_die (child_die);
8386 }
8387 }
38d518c9
EZ
8388}
8389
f55ee35c
JK
8390/* Read a Fortran module as type. This DIE can be only a declaration used for
8391 imported module. Still we need that type as local Fortran "use ... only"
8392 declaration imports depend on the created type in determine_prefix. */
8393
8394static struct type *
8395read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8396{
8397 struct objfile *objfile = cu->objfile;
8398 char *module_name;
8399 struct type *type;
8400
8401 module_name = dwarf2_name (die, cu);
8402 if (!module_name)
3e43a32a
MS
8403 complaint (&symfile_complaints,
8404 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8405 die->offset);
8406 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8407
8408 /* determine_prefix uses TYPE_TAG_NAME. */
8409 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8410
8411 return set_die_type (die, type, cu);
8412}
8413
5d7cb8df
JK
8414/* Read a Fortran module. */
8415
8416static void
8417read_module (struct die_info *die, struct dwarf2_cu *cu)
8418{
8419 struct die_info *child_die = die->child;
8420
5d7cb8df
JK
8421 while (child_die && child_die->tag)
8422 {
8423 process_die (child_die, cu);
8424 child_die = sibling_die (child_die);
8425 }
8426}
8427
38d518c9
EZ
8428/* Return the name of the namespace represented by DIE. Set
8429 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8430 namespace. */
8431
8432static const char *
e142c38c 8433namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8434{
8435 struct die_info *current_die;
8436 const char *name = NULL;
8437
8438 /* Loop through the extensions until we find a name. */
8439
8440 for (current_die = die;
8441 current_die != NULL;
f2f0e013 8442 current_die = dwarf2_extension (die, &cu))
38d518c9 8443 {
e142c38c 8444 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8445 if (name != NULL)
8446 break;
8447 }
8448
8449 /* Is it an anonymous namespace? */
8450
8451 *is_anonymous = (name == NULL);
8452 if (*is_anonymous)
2b1dbab0 8453 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8454
8455 return name;
d9fa45fe
DC
8456}
8457
c906108c
SS
8458/* Extract all information from a DW_TAG_pointer_type DIE and add to
8459 the user defined type vector. */
8460
f792889a 8461static struct type *
e7c27a73 8462read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8463{
5e2b427d 8464 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8465 struct comp_unit_head *cu_header = &cu->header;
c906108c 8466 struct type *type;
8b2dbe47
KB
8467 struct attribute *attr_byte_size;
8468 struct attribute *attr_address_class;
8469 int byte_size, addr_class;
7e314c57
JK
8470 struct type *target_type;
8471
8472 target_type = die_type (die, cu);
c906108c 8473
7e314c57
JK
8474 /* The die_type call above may have already set the type for this DIE. */
8475 type = get_die_type (die, cu);
8476 if (type)
8477 return type;
8478
8479 type = lookup_pointer_type (target_type);
8b2dbe47 8480
e142c38c 8481 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8482 if (attr_byte_size)
8483 byte_size = DW_UNSND (attr_byte_size);
c906108c 8484 else
8b2dbe47
KB
8485 byte_size = cu_header->addr_size;
8486
e142c38c 8487 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8488 if (attr_address_class)
8489 addr_class = DW_UNSND (attr_address_class);
8490 else
8491 addr_class = DW_ADDR_none;
8492
8493 /* If the pointer size or address class is different than the
8494 default, create a type variant marked as such and set the
8495 length accordingly. */
8496 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8497 {
5e2b427d 8498 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8499 {
8500 int type_flags;
8501
849957d9 8502 type_flags = gdbarch_address_class_type_flags
5e2b427d 8503 (gdbarch, byte_size, addr_class);
876cecd0
TT
8504 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8505 == 0);
8b2dbe47
KB
8506 type = make_type_with_address_space (type, type_flags);
8507 }
8508 else if (TYPE_LENGTH (type) != byte_size)
8509 {
3e43a32a
MS
8510 complaint (&symfile_complaints,
8511 _("invalid pointer size %d"), byte_size);
8b2dbe47 8512 }
6e70227d 8513 else
9a619af0
MS
8514 {
8515 /* Should we also complain about unhandled address classes? */
8516 }
c906108c 8517 }
8b2dbe47
KB
8518
8519 TYPE_LENGTH (type) = byte_size;
f792889a 8520 return set_die_type (die, type, cu);
c906108c
SS
8521}
8522
8523/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8524 the user defined type vector. */
8525
f792889a 8526static struct type *
e7c27a73 8527read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8528{
8529 struct type *type;
8530 struct type *to_type;
8531 struct type *domain;
8532
e7c27a73
DJ
8533 to_type = die_type (die, cu);
8534 domain = die_containing_type (die, cu);
0d5de010 8535
7e314c57
JK
8536 /* The calls above may have already set the type for this DIE. */
8537 type = get_die_type (die, cu);
8538 if (type)
8539 return type;
8540
0d5de010
DJ
8541 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8542 type = lookup_methodptr_type (to_type);
8543 else
8544 type = lookup_memberptr_type (to_type, domain);
c906108c 8545
f792889a 8546 return set_die_type (die, type, cu);
c906108c
SS
8547}
8548
8549/* Extract all information from a DW_TAG_reference_type DIE and add to
8550 the user defined type vector. */
8551
f792889a 8552static struct type *
e7c27a73 8553read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8554{
e7c27a73 8555 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8556 struct type *type, *target_type;
c906108c
SS
8557 struct attribute *attr;
8558
7e314c57
JK
8559 target_type = die_type (die, cu);
8560
8561 /* The die_type call above may have already set the type for this DIE. */
8562 type = get_die_type (die, cu);
8563 if (type)
8564 return type;
8565
8566 type = lookup_reference_type (target_type);
e142c38c 8567 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8568 if (attr)
8569 {
8570 TYPE_LENGTH (type) = DW_UNSND (attr);
8571 }
8572 else
8573 {
107d2387 8574 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8575 }
f792889a 8576 return set_die_type (die, type, cu);
c906108c
SS
8577}
8578
f792889a 8579static struct type *
e7c27a73 8580read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8581{
f792889a 8582 struct type *base_type, *cv_type;
c906108c 8583
e7c27a73 8584 base_type = die_type (die, cu);
7e314c57
JK
8585
8586 /* The die_type call above may have already set the type for this DIE. */
8587 cv_type = get_die_type (die, cu);
8588 if (cv_type)
8589 return cv_type;
8590
2f608a3a
KW
8591 /* In case the const qualifier is applied to an array type, the element type
8592 is so qualified, not the array type (section 6.7.3 of C99). */
8593 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8594 {
8595 struct type *el_type, *inner_array;
8596
8597 base_type = copy_type (base_type);
8598 inner_array = base_type;
8599
8600 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8601 {
8602 TYPE_TARGET_TYPE (inner_array) =
8603 copy_type (TYPE_TARGET_TYPE (inner_array));
8604 inner_array = TYPE_TARGET_TYPE (inner_array);
8605 }
8606
8607 el_type = TYPE_TARGET_TYPE (inner_array);
8608 TYPE_TARGET_TYPE (inner_array) =
8609 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8610
8611 return set_die_type (die, base_type, cu);
8612 }
8613
f792889a
DJ
8614 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8615 return set_die_type (die, cv_type, cu);
c906108c
SS
8616}
8617
f792889a 8618static struct type *
e7c27a73 8619read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8620{
f792889a 8621 struct type *base_type, *cv_type;
c906108c 8622
e7c27a73 8623 base_type = die_type (die, cu);
7e314c57
JK
8624
8625 /* The die_type call above may have already set the type for this DIE. */
8626 cv_type = get_die_type (die, cu);
8627 if (cv_type)
8628 return cv_type;
8629
f792889a
DJ
8630 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8631 return set_die_type (die, cv_type, cu);
c906108c
SS
8632}
8633
8634/* Extract all information from a DW_TAG_string_type DIE and add to
8635 the user defined type vector. It isn't really a user defined type,
8636 but it behaves like one, with other DIE's using an AT_user_def_type
8637 attribute to reference it. */
8638
f792889a 8639static struct type *
e7c27a73 8640read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8641{
e7c27a73 8642 struct objfile *objfile = cu->objfile;
3b7538c0 8643 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8644 struct type *type, *range_type, *index_type, *char_type;
8645 struct attribute *attr;
8646 unsigned int length;
8647
e142c38c 8648 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8649 if (attr)
8650 {
8651 length = DW_UNSND (attr);
8652 }
8653 else
8654 {
0963b4bd 8655 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8656 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8657 if (attr)
8658 {
8659 length = DW_UNSND (attr);
8660 }
8661 else
8662 {
8663 length = 1;
8664 }
c906108c 8665 }
6ccb9162 8666
46bf5051 8667 index_type = objfile_type (objfile)->builtin_int;
c906108c 8668 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8669 char_type = language_string_char_type (cu->language_defn, gdbarch);
8670 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8671
f792889a 8672 return set_die_type (die, type, cu);
c906108c
SS
8673}
8674
8675/* Handle DIES due to C code like:
8676
8677 struct foo
c5aa993b
JM
8678 {
8679 int (*funcp)(int a, long l);
8680 int b;
8681 };
c906108c 8682
0963b4bd 8683 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8684
f792889a 8685static struct type *
e7c27a73 8686read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8687{
bb5ed363 8688 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8689 struct type *type; /* Type that this function returns. */
8690 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8691 struct attribute *attr;
8692
e7c27a73 8693 type = die_type (die, cu);
7e314c57
JK
8694
8695 /* The die_type call above may have already set the type for this DIE. */
8696 ftype = get_die_type (die, cu);
8697 if (ftype)
8698 return ftype;
8699
0c8b41f1 8700 ftype = lookup_function_type (type);
c906108c 8701
5b8101ae 8702 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8703 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8704 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8705 || cu->language == language_cplus
5b8101ae
PM
8706 || cu->language == language_java
8707 || cu->language == language_pascal)
876cecd0 8708 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8709 else if (producer_is_realview (cu->producer))
8710 /* RealView does not emit DW_AT_prototyped. We can not
8711 distinguish prototyped and unprototyped functions; default to
8712 prototyped, since that is more common in modern code (and
8713 RealView warns about unprototyped functions). */
8714 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8715
c055b101
CV
8716 /* Store the calling convention in the type if it's available in
8717 the subroutine die. Otherwise set the calling convention to
8718 the default value DW_CC_normal. */
8719 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8720 if (attr)
8721 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8722 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8723 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8724 else
8725 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8726
8727 /* We need to add the subroutine type to the die immediately so
8728 we don't infinitely recurse when dealing with parameters
0963b4bd 8729 declared as the same subroutine type. */
76c10ea2 8730 set_die_type (die, ftype, cu);
6e70227d 8731
639d11d3 8732 if (die->child != NULL)
c906108c 8733 {
bb5ed363 8734 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8735 struct die_info *child_die;
8072405b 8736 int nparams, iparams;
c906108c
SS
8737
8738 /* Count the number of parameters.
8739 FIXME: GDB currently ignores vararg functions, but knows about
8740 vararg member functions. */
8072405b 8741 nparams = 0;
639d11d3 8742 child_die = die->child;
c906108c
SS
8743 while (child_die && child_die->tag)
8744 {
8745 if (child_die->tag == DW_TAG_formal_parameter)
8746 nparams++;
8747 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8748 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8749 child_die = sibling_die (child_die);
8750 }
8751
8752 /* Allocate storage for parameters and fill them in. */
8753 TYPE_NFIELDS (ftype) = nparams;
8754 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8755 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8756
8072405b
JK
8757 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8758 even if we error out during the parameters reading below. */
8759 for (iparams = 0; iparams < nparams; iparams++)
8760 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8761
8762 iparams = 0;
639d11d3 8763 child_die = die->child;
c906108c
SS
8764 while (child_die && child_die->tag)
8765 {
8766 if (child_die->tag == DW_TAG_formal_parameter)
8767 {
3ce3b1ba
PA
8768 struct type *arg_type;
8769
8770 /* DWARF version 2 has no clean way to discern C++
8771 static and non-static member functions. G++ helps
8772 GDB by marking the first parameter for non-static
8773 member functions (which is the this pointer) as
8774 artificial. We pass this information to
8775 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8776
8777 DWARF version 3 added DW_AT_object_pointer, which GCC
8778 4.5 does not yet generate. */
e142c38c 8779 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8780 if (attr)
8781 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8782 else
418835cc
KS
8783 {
8784 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8785
8786 /* GCC/43521: In java, the formal parameter
8787 "this" is sometimes not marked with DW_AT_artificial. */
8788 if (cu->language == language_java)
8789 {
8790 const char *name = dwarf2_name (child_die, cu);
9a619af0 8791
418835cc
KS
8792 if (name && !strcmp (name, "this"))
8793 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8794 }
8795 }
3ce3b1ba
PA
8796 arg_type = die_type (child_die, cu);
8797
8798 /* RealView does not mark THIS as const, which the testsuite
8799 expects. GCC marks THIS as const in method definitions,
8800 but not in the class specifications (GCC PR 43053). */
8801 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8802 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8803 {
8804 int is_this = 0;
8805 struct dwarf2_cu *arg_cu = cu;
8806 const char *name = dwarf2_name (child_die, cu);
8807
8808 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8809 if (attr)
8810 {
8811 /* If the compiler emits this, use it. */
8812 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8813 is_this = 1;
8814 }
8815 else if (name && strcmp (name, "this") == 0)
8816 /* Function definitions will have the argument names. */
8817 is_this = 1;
8818 else if (name == NULL && iparams == 0)
8819 /* Declarations may not have the names, so like
8820 elsewhere in GDB, assume an artificial first
8821 argument is "this". */
8822 is_this = 1;
8823
8824 if (is_this)
8825 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8826 arg_type, 0);
8827 }
8828
8829 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8830 iparams++;
8831 }
8832 child_die = sibling_die (child_die);
8833 }
8834 }
8835
76c10ea2 8836 return ftype;
c906108c
SS
8837}
8838
f792889a 8839static struct type *
e7c27a73 8840read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8841{
e7c27a73 8842 struct objfile *objfile = cu->objfile;
0114d602 8843 const char *name = NULL;
3c8e0968 8844 struct type *this_type, *target_type;
c906108c 8845
94af9270 8846 name = dwarf2_full_name (NULL, die, cu);
f792889a 8847 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8848 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8849 TYPE_NAME (this_type) = (char *) name;
f792889a 8850 set_die_type (die, this_type, cu);
3c8e0968
DE
8851 target_type = die_type (die, cu);
8852 if (target_type != this_type)
8853 TYPE_TARGET_TYPE (this_type) = target_type;
8854 else
8855 {
8856 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8857 spec and cause infinite loops in GDB. */
8858 complaint (&symfile_complaints,
8859 _("Self-referential DW_TAG_typedef "
8860 "- DIE at 0x%x [in module %s]"),
bb5ed363 8861 die->offset, objfile->name);
3c8e0968
DE
8862 TYPE_TARGET_TYPE (this_type) = NULL;
8863 }
f792889a 8864 return this_type;
c906108c
SS
8865}
8866
8867/* Find a representation of a given base type and install
8868 it in the TYPE field of the die. */
8869
f792889a 8870static struct type *
e7c27a73 8871read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8872{
e7c27a73 8873 struct objfile *objfile = cu->objfile;
c906108c
SS
8874 struct type *type;
8875 struct attribute *attr;
8876 int encoding = 0, size = 0;
39cbfefa 8877 char *name;
6ccb9162
UW
8878 enum type_code code = TYPE_CODE_INT;
8879 int type_flags = 0;
8880 struct type *target_type = NULL;
c906108c 8881
e142c38c 8882 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8883 if (attr)
8884 {
8885 encoding = DW_UNSND (attr);
8886 }
e142c38c 8887 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8888 if (attr)
8889 {
8890 size = DW_UNSND (attr);
8891 }
39cbfefa 8892 name = dwarf2_name (die, cu);
6ccb9162 8893 if (!name)
c906108c 8894 {
6ccb9162
UW
8895 complaint (&symfile_complaints,
8896 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8897 }
6ccb9162
UW
8898
8899 switch (encoding)
c906108c 8900 {
6ccb9162
UW
8901 case DW_ATE_address:
8902 /* Turn DW_ATE_address into a void * pointer. */
8903 code = TYPE_CODE_PTR;
8904 type_flags |= TYPE_FLAG_UNSIGNED;
8905 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8906 break;
8907 case DW_ATE_boolean:
8908 code = TYPE_CODE_BOOL;
8909 type_flags |= TYPE_FLAG_UNSIGNED;
8910 break;
8911 case DW_ATE_complex_float:
8912 code = TYPE_CODE_COMPLEX;
8913 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8914 break;
8915 case DW_ATE_decimal_float:
8916 code = TYPE_CODE_DECFLOAT;
8917 break;
8918 case DW_ATE_float:
8919 code = TYPE_CODE_FLT;
8920 break;
8921 case DW_ATE_signed:
8922 break;
8923 case DW_ATE_unsigned:
8924 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8925 if (cu->language == language_fortran
8926 && name
8927 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8928 code = TYPE_CODE_CHAR;
6ccb9162
UW
8929 break;
8930 case DW_ATE_signed_char:
6e70227d 8931 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8932 || cu->language == language_pascal
8933 || cu->language == language_fortran)
6ccb9162
UW
8934 code = TYPE_CODE_CHAR;
8935 break;
8936 case DW_ATE_unsigned_char:
868a0084 8937 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8938 || cu->language == language_pascal
8939 || cu->language == language_fortran)
6ccb9162
UW
8940 code = TYPE_CODE_CHAR;
8941 type_flags |= TYPE_FLAG_UNSIGNED;
8942 break;
75079b2b
TT
8943 case DW_ATE_UTF:
8944 /* We just treat this as an integer and then recognize the
8945 type by name elsewhere. */
8946 break;
8947
6ccb9162
UW
8948 default:
8949 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8950 dwarf_type_encoding_name (encoding));
8951 break;
c906108c 8952 }
6ccb9162 8953
0114d602
DJ
8954 type = init_type (code, size, type_flags, NULL, objfile);
8955 TYPE_NAME (type) = name;
6ccb9162
UW
8956 TYPE_TARGET_TYPE (type) = target_type;
8957
0114d602 8958 if (name && strcmp (name, "char") == 0)
876cecd0 8959 TYPE_NOSIGN (type) = 1;
0114d602 8960
f792889a 8961 return set_die_type (die, type, cu);
c906108c
SS
8962}
8963
a02abb62
JB
8964/* Read the given DW_AT_subrange DIE. */
8965
f792889a 8966static struct type *
a02abb62
JB
8967read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8968{
8969 struct type *base_type;
8970 struct type *range_type;
8971 struct attribute *attr;
43bbcdc2
PH
8972 LONGEST low = 0;
8973 LONGEST high = -1;
39cbfefa 8974 char *name;
43bbcdc2 8975 LONGEST negative_mask;
e77813c8 8976
a02abb62 8977 base_type = die_type (die, cu);
953ac07e
JK
8978 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8979 check_typedef (base_type);
a02abb62 8980
7e314c57
JK
8981 /* The die_type call above may have already set the type for this DIE. */
8982 range_type = get_die_type (die, cu);
8983 if (range_type)
8984 return range_type;
8985
e142c38c 8986 if (cu->language == language_fortran)
6e70227d 8987 {
a02abb62
JB
8988 /* FORTRAN implies a lower bound of 1, if not given. */
8989 low = 1;
8990 }
8991
dd5e6932
DJ
8992 /* FIXME: For variable sized arrays either of these could be
8993 a variable rather than a constant value. We'll allow it,
8994 but we don't know how to handle it. */
e142c38c 8995 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8996 if (attr)
8997 low = dwarf2_get_attr_constant_value (attr, 0);
8998
e142c38c 8999 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9000 if (attr)
6e70227d 9001 {
d48323d8 9002 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9003 {
9004 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9005 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9006 FIXME: GDB does not yet know how to handle dynamic
9007 arrays properly, treat them as arrays with unspecified
9008 length for now.
9009
9010 FIXME: jimb/2003-09-22: GDB does not really know
9011 how to handle arrays of unspecified length
9012 either; we just represent them as zero-length
9013 arrays. Choose an appropriate upper bound given
9014 the lower bound we've computed above. */
9015 high = low - 1;
9016 }
9017 else
9018 high = dwarf2_get_attr_constant_value (attr, 1);
9019 }
e77813c8
PM
9020 else
9021 {
9022 attr = dwarf2_attr (die, DW_AT_count, cu);
9023 if (attr)
9024 {
9025 int count = dwarf2_get_attr_constant_value (attr, 1);
9026 high = low + count - 1;
9027 }
c2ff108b
JK
9028 else
9029 {
9030 /* Unspecified array length. */
9031 high = low - 1;
9032 }
e77813c8
PM
9033 }
9034
9035 /* Dwarf-2 specifications explicitly allows to create subrange types
9036 without specifying a base type.
9037 In that case, the base type must be set to the type of
9038 the lower bound, upper bound or count, in that order, if any of these
9039 three attributes references an object that has a type.
9040 If no base type is found, the Dwarf-2 specifications say that
9041 a signed integer type of size equal to the size of an address should
9042 be used.
9043 For the following C code: `extern char gdb_int [];'
9044 GCC produces an empty range DIE.
9045 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9046 high bound or count are not yet handled by this code. */
e77813c8
PM
9047 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9048 {
9049 struct objfile *objfile = cu->objfile;
9050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9051 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9052 struct type *int_type = objfile_type (objfile)->builtin_int;
9053
9054 /* Test "int", "long int", and "long long int" objfile types,
9055 and select the first one having a size above or equal to the
9056 architecture address size. */
9057 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9058 base_type = int_type;
9059 else
9060 {
9061 int_type = objfile_type (objfile)->builtin_long;
9062 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9063 base_type = int_type;
9064 else
9065 {
9066 int_type = objfile_type (objfile)->builtin_long_long;
9067 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9068 base_type = int_type;
9069 }
9070 }
9071 }
a02abb62 9072
6e70227d 9073 negative_mask =
43bbcdc2
PH
9074 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9075 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9076 low |= negative_mask;
9077 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9078 high |= negative_mask;
9079
a02abb62
JB
9080 range_type = create_range_type (NULL, base_type, low, high);
9081
bbb0eef6
JK
9082 /* Mark arrays with dynamic length at least as an array of unspecified
9083 length. GDB could check the boundary but before it gets implemented at
9084 least allow accessing the array elements. */
d48323d8 9085 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9086 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9087
c2ff108b
JK
9088 /* Ada expects an empty array on no boundary attributes. */
9089 if (attr == NULL && cu->language != language_ada)
9090 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9091
39cbfefa
DJ
9092 name = dwarf2_name (die, cu);
9093 if (name)
9094 TYPE_NAME (range_type) = name;
6e70227d 9095
e142c38c 9096 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9097 if (attr)
9098 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9099
7e314c57
JK
9100 set_die_type (die, range_type, cu);
9101
9102 /* set_die_type should be already done. */
b4ba55a1
JB
9103 set_descriptive_type (range_type, die, cu);
9104
7e314c57 9105 return range_type;
a02abb62 9106}
6e70227d 9107
f792889a 9108static struct type *
81a17f79
JB
9109read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9110{
9111 struct type *type;
81a17f79 9112
81a17f79
JB
9113 /* For now, we only support the C meaning of an unspecified type: void. */
9114
0114d602
DJ
9115 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9116 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9117
f792889a 9118 return set_die_type (die, type, cu);
81a17f79 9119}
a02abb62 9120
51545339
DJ
9121/* Trivial hash function for die_info: the hash value of a DIE
9122 is its offset in .debug_info for this objfile. */
9123
9124static hashval_t
9125die_hash (const void *item)
9126{
9127 const struct die_info *die = item;
9a619af0 9128
51545339
DJ
9129 return die->offset;
9130}
9131
9132/* Trivial comparison function for die_info structures: two DIEs
9133 are equal if they have the same offset. */
9134
9135static int
9136die_eq (const void *item_lhs, const void *item_rhs)
9137{
9138 const struct die_info *die_lhs = item_lhs;
9139 const struct die_info *die_rhs = item_rhs;
9a619af0 9140
51545339
DJ
9141 return die_lhs->offset == die_rhs->offset;
9142}
9143
c906108c
SS
9144/* Read a whole compilation unit into a linked list of dies. */
9145
f9aca02d 9146static struct die_info *
93311388 9147read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9148{
93311388 9149 struct die_reader_specs reader_specs;
98bfdba5 9150 int read_abbrevs = 0;
1d9ec526 9151 struct cleanup *back_to = NULL;
98bfdba5
PA
9152 struct die_info *die;
9153
9154 if (cu->dwarf2_abbrevs == NULL)
9155 {
9156 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9157 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9158 read_abbrevs = 1;
9159 }
93311388 9160
348e048f 9161 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9162 cu->die_hash
9163 = htab_create_alloc_ex (cu->header.length / 12,
9164 die_hash,
9165 die_eq,
9166 NULL,
9167 &cu->comp_unit_obstack,
9168 hashtab_obstack_allocate,
9169 dummy_obstack_deallocate);
9170
93311388
DE
9171 init_cu_die_reader (&reader_specs, cu);
9172
98bfdba5
PA
9173 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9174
9175 if (read_abbrevs)
9176 do_cleanups (back_to);
9177
9178 return die;
639d11d3
DC
9179}
9180
d97bc12b
DE
9181/* Main entry point for reading a DIE and all children.
9182 Read the DIE and dump it if requested. */
9183
9184static struct die_info *
93311388
DE
9185read_die_and_children (const struct die_reader_specs *reader,
9186 gdb_byte *info_ptr,
d97bc12b
DE
9187 gdb_byte **new_info_ptr,
9188 struct die_info *parent)
9189{
93311388 9190 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9191 new_info_ptr, parent);
9192
9193 if (dwarf2_die_debug)
9194 {
348e048f
DE
9195 fprintf_unfiltered (gdb_stdlog,
9196 "\nRead die from %s of %s:\n",
b0df02fd 9197 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9198 ? ".debug_types"
9199 : ".debug_info"),
348e048f 9200 reader->abfd->filename);
d97bc12b
DE
9201 dump_die (result, dwarf2_die_debug);
9202 }
9203
9204 return result;
9205}
9206
639d11d3
DC
9207/* Read a single die and all its descendents. Set the die's sibling
9208 field to NULL; set other fields in the die correctly, and set all
9209 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9210 location of the info_ptr after reading all of those dies. PARENT
9211 is the parent of the die in question. */
9212
9213static struct die_info *
93311388
DE
9214read_die_and_children_1 (const struct die_reader_specs *reader,
9215 gdb_byte *info_ptr,
d97bc12b
DE
9216 gdb_byte **new_info_ptr,
9217 struct die_info *parent)
639d11d3
DC
9218{
9219 struct die_info *die;
fe1b8b76 9220 gdb_byte *cur_ptr;
639d11d3
DC
9221 int has_children;
9222
93311388 9223 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9224 if (die == NULL)
9225 {
9226 *new_info_ptr = cur_ptr;
9227 return NULL;
9228 }
93311388 9229 store_in_ref_table (die, reader->cu);
639d11d3
DC
9230
9231 if (has_children)
348e048f 9232 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9233 else
9234 {
9235 die->child = NULL;
9236 *new_info_ptr = cur_ptr;
9237 }
9238
9239 die->sibling = NULL;
9240 die->parent = parent;
9241 return die;
9242}
9243
9244/* Read a die, all of its descendents, and all of its siblings; set
9245 all of the fields of all of the dies correctly. Arguments are as
9246 in read_die_and_children. */
9247
9248static struct die_info *
93311388
DE
9249read_die_and_siblings (const struct die_reader_specs *reader,
9250 gdb_byte *info_ptr,
fe1b8b76 9251 gdb_byte **new_info_ptr,
639d11d3
DC
9252 struct die_info *parent)
9253{
9254 struct die_info *first_die, *last_sibling;
fe1b8b76 9255 gdb_byte *cur_ptr;
639d11d3 9256
c906108c 9257 cur_ptr = info_ptr;
639d11d3
DC
9258 first_die = last_sibling = NULL;
9259
9260 while (1)
c906108c 9261 {
639d11d3 9262 struct die_info *die
93311388 9263 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9264
1d325ec1 9265 if (die == NULL)
c906108c 9266 {
639d11d3
DC
9267 *new_info_ptr = cur_ptr;
9268 return first_die;
c906108c 9269 }
1d325ec1
DJ
9270
9271 if (!first_die)
9272 first_die = die;
c906108c 9273 else
1d325ec1
DJ
9274 last_sibling->sibling = die;
9275
9276 last_sibling = die;
c906108c 9277 }
c906108c
SS
9278}
9279
93311388
DE
9280/* Read the die from the .debug_info section buffer. Set DIEP to
9281 point to a newly allocated die with its information, except for its
9282 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9283 whether the die has children or not. */
9284
9285static gdb_byte *
9286read_full_die (const struct die_reader_specs *reader,
9287 struct die_info **diep, gdb_byte *info_ptr,
9288 int *has_children)
9289{
9290 unsigned int abbrev_number, bytes_read, i, offset;
9291 struct abbrev_info *abbrev;
9292 struct die_info *die;
9293 struct dwarf2_cu *cu = reader->cu;
9294 bfd *abfd = reader->abfd;
9295
9296 offset = info_ptr - reader->buffer;
9297 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9298 info_ptr += bytes_read;
9299 if (!abbrev_number)
9300 {
9301 *diep = NULL;
9302 *has_children = 0;
9303 return info_ptr;
9304 }
9305
9306 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9307 if (!abbrev)
348e048f
DE
9308 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9309 abbrev_number,
9310 bfd_get_filename (abfd));
9311
93311388
DE
9312 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9313 die->offset = offset;
9314 die->tag = abbrev->tag;
9315 die->abbrev = abbrev_number;
9316
9317 die->num_attrs = abbrev->num_attrs;
9318
9319 for (i = 0; i < abbrev->num_attrs; ++i)
9320 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9321 abfd, info_ptr, cu);
9322
9323 *diep = die;
9324 *has_children = abbrev->has_children;
9325 return info_ptr;
9326}
9327
c906108c
SS
9328/* In DWARF version 2, the description of the debugging information is
9329 stored in a separate .debug_abbrev section. Before we read any
9330 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9331 in a hash table. This function also sets flags in CU describing
9332 the data found in the abbrev table. */
c906108c
SS
9333
9334static void
e7c27a73 9335dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9336{
e7c27a73 9337 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9338 gdb_byte *abbrev_ptr;
c906108c
SS
9339 struct abbrev_info *cur_abbrev;
9340 unsigned int abbrev_number, bytes_read, abbrev_name;
9341 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9342 struct attr_abbrev *cur_attrs;
9343 unsigned int allocated_attrs;
c906108c 9344
0963b4bd 9345 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9346 obstack_init (&cu->abbrev_obstack);
9347 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9348 (ABBREV_HASH_SIZE
9349 * sizeof (struct abbrev_info *)));
9350 memset (cu->dwarf2_abbrevs, 0,
9351 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9352
be391dca
TT
9353 dwarf2_read_section (dwarf2_per_objfile->objfile,
9354 &dwarf2_per_objfile->abbrev);
dce234bc 9355 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9356 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9357 abbrev_ptr += bytes_read;
9358
f3dd6933
DJ
9359 allocated_attrs = ATTR_ALLOC_CHUNK;
9360 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9361
0963b4bd 9362 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9363 while (abbrev_number)
9364 {
f3dd6933 9365 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9366
9367 /* read in abbrev header */
9368 cur_abbrev->number = abbrev_number;
9369 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9370 abbrev_ptr += bytes_read;
9371 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9372 abbrev_ptr += 1;
9373
72bf9492
DJ
9374 if (cur_abbrev->tag == DW_TAG_namespace)
9375 cu->has_namespace_info = 1;
9376
c906108c
SS
9377 /* now read in declarations */
9378 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9379 abbrev_ptr += bytes_read;
9380 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9381 abbrev_ptr += bytes_read;
9382 while (abbrev_name)
9383 {
f3dd6933 9384 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9385 {
f3dd6933
DJ
9386 allocated_attrs += ATTR_ALLOC_CHUNK;
9387 cur_attrs
9388 = xrealloc (cur_attrs, (allocated_attrs
9389 * sizeof (struct attr_abbrev)));
c906108c 9390 }
ae038cb0
DJ
9391
9392 /* Record whether this compilation unit might have
9393 inter-compilation-unit references. If we don't know what form
9394 this attribute will have, then it might potentially be a
9395 DW_FORM_ref_addr, so we conservatively expect inter-CU
9396 references. */
9397
9398 if (abbrev_form == DW_FORM_ref_addr
9399 || abbrev_form == DW_FORM_indirect)
9400 cu->has_form_ref_addr = 1;
9401
f3dd6933
DJ
9402 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9403 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9404 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9405 abbrev_ptr += bytes_read;
9406 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9407 abbrev_ptr += bytes_read;
9408 }
9409
f3dd6933
DJ
9410 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9411 (cur_abbrev->num_attrs
9412 * sizeof (struct attr_abbrev)));
9413 memcpy (cur_abbrev->attrs, cur_attrs,
9414 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9415
c906108c 9416 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9417 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9418 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9419
9420 /* Get next abbreviation.
9421 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9422 always properly terminated with an abbrev number of 0.
9423 Exit loop if we encounter an abbreviation which we have
9424 already read (which means we are about to read the abbreviations
9425 for the next compile unit) or if the end of the abbreviation
9426 table is reached. */
dce234bc
PP
9427 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9428 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9429 break;
9430 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9431 abbrev_ptr += bytes_read;
e7c27a73 9432 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9433 break;
9434 }
f3dd6933
DJ
9435
9436 xfree (cur_attrs);
c906108c
SS
9437}
9438
f3dd6933 9439/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9440
c906108c 9441static void
f3dd6933 9442dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9443{
f3dd6933 9444 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9445
f3dd6933
DJ
9446 obstack_free (&cu->abbrev_obstack, NULL);
9447 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9448}
9449
9450/* Lookup an abbrev_info structure in the abbrev hash table. */
9451
9452static struct abbrev_info *
e7c27a73 9453dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9454{
9455 unsigned int hash_number;
9456 struct abbrev_info *abbrev;
9457
9458 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9459 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9460
9461 while (abbrev)
9462 {
9463 if (abbrev->number == number)
9464 return abbrev;
9465 else
9466 abbrev = abbrev->next;
9467 }
9468 return NULL;
9469}
9470
72bf9492
DJ
9471/* Returns nonzero if TAG represents a type that we might generate a partial
9472 symbol for. */
9473
9474static int
9475is_type_tag_for_partial (int tag)
9476{
9477 switch (tag)
9478 {
9479#if 0
9480 /* Some types that would be reasonable to generate partial symbols for,
9481 that we don't at present. */
9482 case DW_TAG_array_type:
9483 case DW_TAG_file_type:
9484 case DW_TAG_ptr_to_member_type:
9485 case DW_TAG_set_type:
9486 case DW_TAG_string_type:
9487 case DW_TAG_subroutine_type:
9488#endif
9489 case DW_TAG_base_type:
9490 case DW_TAG_class_type:
680b30c7 9491 case DW_TAG_interface_type:
72bf9492
DJ
9492 case DW_TAG_enumeration_type:
9493 case DW_TAG_structure_type:
9494 case DW_TAG_subrange_type:
9495 case DW_TAG_typedef:
9496 case DW_TAG_union_type:
9497 return 1;
9498 default:
9499 return 0;
9500 }
9501}
9502
9503/* Load all DIEs that are interesting for partial symbols into memory. */
9504
9505static struct partial_die_info *
93311388
DE
9506load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9507 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9508{
bb5ed363 9509 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9510 struct partial_die_info *part_die;
9511 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9512 struct abbrev_info *abbrev;
9513 unsigned int bytes_read;
5afb4e99 9514 unsigned int load_all = 0;
72bf9492
DJ
9515
9516 int nesting_level = 1;
9517
9518 parent_die = NULL;
9519 last_die = NULL;
9520
5afb4e99
DJ
9521 if (cu->per_cu && cu->per_cu->load_all_dies)
9522 load_all = 1;
9523
72bf9492
DJ
9524 cu->partial_dies
9525 = htab_create_alloc_ex (cu->header.length / 12,
9526 partial_die_hash,
9527 partial_die_eq,
9528 NULL,
9529 &cu->comp_unit_obstack,
9530 hashtab_obstack_allocate,
9531 dummy_obstack_deallocate);
9532
9533 part_die = obstack_alloc (&cu->comp_unit_obstack,
9534 sizeof (struct partial_die_info));
9535
9536 while (1)
9537 {
9538 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9539
9540 /* A NULL abbrev means the end of a series of children. */
9541 if (abbrev == NULL)
9542 {
9543 if (--nesting_level == 0)
9544 {
9545 /* PART_DIE was probably the last thing allocated on the
9546 comp_unit_obstack, so we could call obstack_free
9547 here. We don't do that because the waste is small,
9548 and will be cleaned up when we're done with this
9549 compilation unit. This way, we're also more robust
9550 against other users of the comp_unit_obstack. */
9551 return first_die;
9552 }
9553 info_ptr += bytes_read;
9554 last_die = parent_die;
9555 parent_die = parent_die->die_parent;
9556 continue;
9557 }
9558
98bfdba5
PA
9559 /* Check for template arguments. We never save these; if
9560 they're seen, we just mark the parent, and go on our way. */
9561 if (parent_die != NULL
9562 && cu->language == language_cplus
9563 && (abbrev->tag == DW_TAG_template_type_param
9564 || abbrev->tag == DW_TAG_template_value_param))
9565 {
9566 parent_die->has_template_arguments = 1;
9567
9568 if (!load_all)
9569 {
9570 /* We don't need a partial DIE for the template argument. */
9571 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9572 cu);
9573 continue;
9574 }
9575 }
9576
9577 /* We only recurse into subprograms looking for template arguments.
9578 Skip their other children. */
9579 if (!load_all
9580 && cu->language == language_cplus
9581 && parent_die != NULL
9582 && parent_die->tag == DW_TAG_subprogram)
9583 {
9584 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9585 continue;
9586 }
9587
5afb4e99
DJ
9588 /* Check whether this DIE is interesting enough to save. Normally
9589 we would not be interested in members here, but there may be
9590 later variables referencing them via DW_AT_specification (for
9591 static members). */
9592 if (!load_all
9593 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9594 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9595 && abbrev->tag != DW_TAG_enumerator
9596 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9597 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9598 && abbrev->tag != DW_TAG_variable
5afb4e99 9599 && abbrev->tag != DW_TAG_namespace
f55ee35c 9600 && abbrev->tag != DW_TAG_module
5afb4e99 9601 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9602 {
9603 /* Otherwise we skip to the next sibling, if any. */
93311388 9604 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9605 continue;
9606 }
9607
93311388
DE
9608 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9609 buffer, info_ptr, cu);
72bf9492
DJ
9610
9611 /* This two-pass algorithm for processing partial symbols has a
9612 high cost in cache pressure. Thus, handle some simple cases
9613 here which cover the majority of C partial symbols. DIEs
9614 which neither have specification tags in them, nor could have
9615 specification tags elsewhere pointing at them, can simply be
9616 processed and discarded.
9617
9618 This segment is also optional; scan_partial_symbols and
9619 add_partial_symbol will handle these DIEs if we chain
9620 them in normally. When compilers which do not emit large
9621 quantities of duplicate debug information are more common,
9622 this code can probably be removed. */
9623
9624 /* Any complete simple types at the top level (pretty much all
9625 of them, for a language without namespaces), can be processed
9626 directly. */
9627 if (parent_die == NULL
9628 && part_die->has_specification == 0
9629 && part_die->is_declaration == 0
d8228535 9630 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9631 || part_die->tag == DW_TAG_base_type
9632 || part_die->tag == DW_TAG_subrange_type))
9633 {
9634 if (building_psymtab && part_die->name != NULL)
04a679b8 9635 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9636 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9637 &objfile->static_psymbols,
9638 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9639 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9640 continue;
9641 }
9642
d8228535
JK
9643 /* The exception for DW_TAG_typedef with has_children above is
9644 a workaround of GCC PR debug/47510. In the case of this complaint
9645 type_name_no_tag_or_error will error on such types later.
9646
9647 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9648 it could not find the child DIEs referenced later, this is checked
9649 above. In correct DWARF DW_TAG_typedef should have no children. */
9650
9651 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9652 complaint (&symfile_complaints,
9653 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9654 "- DIE at 0x%x [in module %s]"),
bb5ed363 9655 part_die->offset, objfile->name);
d8228535 9656
72bf9492
DJ
9657 /* If we're at the second level, and we're an enumerator, and
9658 our parent has no specification (meaning possibly lives in a
9659 namespace elsewhere), then we can add the partial symbol now
9660 instead of queueing it. */
9661 if (part_die->tag == DW_TAG_enumerator
9662 && parent_die != NULL
9663 && parent_die->die_parent == NULL
9664 && parent_die->tag == DW_TAG_enumeration_type
9665 && parent_die->has_specification == 0)
9666 {
9667 if (part_die->name == NULL)
3e43a32a
MS
9668 complaint (&symfile_complaints,
9669 _("malformed enumerator DIE ignored"));
72bf9492 9670 else if (building_psymtab)
04a679b8 9671 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9672 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9673 (cu->language == language_cplus
9674 || cu->language == language_java)
bb5ed363
DE
9675 ? &objfile->global_psymbols
9676 : &objfile->static_psymbols,
9677 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9678
93311388 9679 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9680 continue;
9681 }
9682
9683 /* We'll save this DIE so link it in. */
9684 part_die->die_parent = parent_die;
9685 part_die->die_sibling = NULL;
9686 part_die->die_child = NULL;
9687
9688 if (last_die && last_die == parent_die)
9689 last_die->die_child = part_die;
9690 else if (last_die)
9691 last_die->die_sibling = part_die;
9692
9693 last_die = part_die;
9694
9695 if (first_die == NULL)
9696 first_die = part_die;
9697
9698 /* Maybe add the DIE to the hash table. Not all DIEs that we
9699 find interesting need to be in the hash table, because we
9700 also have the parent/sibling/child chains; only those that we
9701 might refer to by offset later during partial symbol reading.
9702
9703 For now this means things that might have be the target of a
9704 DW_AT_specification, DW_AT_abstract_origin, or
9705 DW_AT_extension. DW_AT_extension will refer only to
9706 namespaces; DW_AT_abstract_origin refers to functions (and
9707 many things under the function DIE, but we do not recurse
9708 into function DIEs during partial symbol reading) and
9709 possibly variables as well; DW_AT_specification refers to
9710 declarations. Declarations ought to have the DW_AT_declaration
9711 flag. It happens that GCC forgets to put it in sometimes, but
9712 only for functions, not for types.
9713
9714 Adding more things than necessary to the hash table is harmless
9715 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9716 wasted time in find_partial_die, when we reread the compilation
9717 unit with load_all_dies set. */
72bf9492 9718
5afb4e99 9719 if (load_all
72929c62 9720 || abbrev->tag == DW_TAG_constant
5afb4e99 9721 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9722 || abbrev->tag == DW_TAG_variable
9723 || abbrev->tag == DW_TAG_namespace
9724 || part_die->is_declaration)
9725 {
9726 void **slot;
9727
9728 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9729 part_die->offset, INSERT);
9730 *slot = part_die;
9731 }
9732
9733 part_die = obstack_alloc (&cu->comp_unit_obstack,
9734 sizeof (struct partial_die_info));
9735
9736 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9737 we have no reason to follow the children of structures; for other
98bfdba5
PA
9738 languages we have to, so that we can get at method physnames
9739 to infer fully qualified class names, for DW_AT_specification,
9740 and for C++ template arguments. For C++, we also look one level
9741 inside functions to find template arguments (if the name of the
9742 function does not already contain the template arguments).
bc30ff58
JB
9743
9744 For Ada, we need to scan the children of subprograms and lexical
9745 blocks as well because Ada allows the definition of nested
9746 entities that could be interesting for the debugger, such as
9747 nested subprograms for instance. */
72bf9492 9748 if (last_die->has_children
5afb4e99
DJ
9749 && (load_all
9750 || last_die->tag == DW_TAG_namespace
f55ee35c 9751 || last_die->tag == DW_TAG_module
72bf9492 9752 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9753 || (cu->language == language_cplus
9754 && last_die->tag == DW_TAG_subprogram
9755 && (last_die->name == NULL
9756 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9757 || (cu->language != language_c
9758 && (last_die->tag == DW_TAG_class_type
680b30c7 9759 || last_die->tag == DW_TAG_interface_type
72bf9492 9760 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9761 || last_die->tag == DW_TAG_union_type))
9762 || (cu->language == language_ada
9763 && (last_die->tag == DW_TAG_subprogram
9764 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9765 {
9766 nesting_level++;
9767 parent_die = last_die;
9768 continue;
9769 }
9770
9771 /* Otherwise we skip to the next sibling, if any. */
93311388 9772 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9773
9774 /* Back to the top, do it again. */
9775 }
9776}
9777
c906108c
SS
9778/* Read a minimal amount of information into the minimal die structure. */
9779
fe1b8b76 9780static gdb_byte *
72bf9492
DJ
9781read_partial_die (struct partial_die_info *part_die,
9782 struct abbrev_info *abbrev,
9783 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9784 gdb_byte *buffer, gdb_byte *info_ptr,
9785 struct dwarf2_cu *cu)
c906108c 9786{
bb5ed363 9787 struct objfile *objfile = cu->objfile;
fa238c03 9788 unsigned int i;
c906108c 9789 struct attribute attr;
c5aa993b 9790 int has_low_pc_attr = 0;
c906108c
SS
9791 int has_high_pc_attr = 0;
9792
72bf9492 9793 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9794
93311388 9795 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9796
9797 info_ptr += abbrev_len;
9798
9799 if (abbrev == NULL)
9800 return info_ptr;
9801
c906108c
SS
9802 part_die->tag = abbrev->tag;
9803 part_die->has_children = abbrev->has_children;
c906108c
SS
9804
9805 for (i = 0; i < abbrev->num_attrs; ++i)
9806 {
e7c27a73 9807 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9808
9809 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9810 partial symbol table. */
c906108c
SS
9811 switch (attr.name)
9812 {
9813 case DW_AT_name:
71c25dea
TT
9814 switch (part_die->tag)
9815 {
9816 case DW_TAG_compile_unit:
348e048f 9817 case DW_TAG_type_unit:
71c25dea
TT
9818 /* Compilation units have a DW_AT_name that is a filename, not
9819 a source language identifier. */
9820 case DW_TAG_enumeration_type:
9821 case DW_TAG_enumerator:
9822 /* These tags always have simple identifiers already; no need
9823 to canonicalize them. */
9824 part_die->name = DW_STRING (&attr);
9825 break;
9826 default:
9827 part_die->name
9828 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9829 &objfile->objfile_obstack);
71c25dea
TT
9830 break;
9831 }
c906108c 9832 break;
31ef98ae 9833 case DW_AT_linkage_name:
c906108c 9834 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9835 /* Note that both forms of linkage name might appear. We
9836 assume they will be the same, and we only store the last
9837 one we see. */
94af9270
KS
9838 if (cu->language == language_ada)
9839 part_die->name = DW_STRING (&attr);
abc72ce4 9840 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9841 break;
9842 case DW_AT_low_pc:
9843 has_low_pc_attr = 1;
9844 part_die->lowpc = DW_ADDR (&attr);
9845 break;
9846 case DW_AT_high_pc:
9847 has_high_pc_attr = 1;
9848 part_die->highpc = DW_ADDR (&attr);
9849 break;
9850 case DW_AT_location:
0963b4bd 9851 /* Support the .debug_loc offsets. */
8e19ed76
PS
9852 if (attr_form_is_block (&attr))
9853 {
9854 part_die->locdesc = DW_BLOCK (&attr);
9855 }
3690dd37 9856 else if (attr_form_is_section_offset (&attr))
8e19ed76 9857 {
4d3c2250 9858 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9859 }
9860 else
9861 {
4d3c2250
KB
9862 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9863 "partial symbol information");
8e19ed76 9864 }
c906108c 9865 break;
c906108c
SS
9866 case DW_AT_external:
9867 part_die->is_external = DW_UNSND (&attr);
9868 break;
9869 case DW_AT_declaration:
9870 part_die->is_declaration = DW_UNSND (&attr);
9871 break;
9872 case DW_AT_type:
9873 part_die->has_type = 1;
9874 break;
9875 case DW_AT_abstract_origin:
9876 case DW_AT_specification:
72bf9492
DJ
9877 case DW_AT_extension:
9878 part_die->has_specification = 1;
c764a876 9879 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9880 break;
9881 case DW_AT_sibling:
9882 /* Ignore absolute siblings, they might point outside of
9883 the current compile unit. */
9884 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9885 complaint (&symfile_complaints,
9886 _("ignoring absolute DW_AT_sibling"));
c906108c 9887 else
93311388 9888 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9889 break;
fa4028e9
JB
9890 case DW_AT_byte_size:
9891 part_die->has_byte_size = 1;
9892 break;
68511cec
CES
9893 case DW_AT_calling_convention:
9894 /* DWARF doesn't provide a way to identify a program's source-level
9895 entry point. DW_AT_calling_convention attributes are only meant
9896 to describe functions' calling conventions.
9897
9898 However, because it's a necessary piece of information in
9899 Fortran, and because DW_CC_program is the only piece of debugging
9900 information whose definition refers to a 'main program' at all,
9901 several compilers have begun marking Fortran main programs with
9902 DW_CC_program --- even when those functions use the standard
9903 calling conventions.
9904
9905 So until DWARF specifies a way to provide this information and
9906 compilers pick up the new representation, we'll support this
9907 practice. */
9908 if (DW_UNSND (&attr) == DW_CC_program
9909 && cu->language == language_fortran)
01f8c46d
JK
9910 {
9911 set_main_name (part_die->name);
9912
9913 /* As this DIE has a static linkage the name would be difficult
9914 to look up later. */
9915 language_of_main = language_fortran;
9916 }
68511cec 9917 break;
c906108c
SS
9918 default:
9919 break;
9920 }
9921 }
9922
9373cf26
JK
9923 if (has_low_pc_attr && has_high_pc_attr)
9924 {
9925 /* When using the GNU linker, .gnu.linkonce. sections are used to
9926 eliminate duplicate copies of functions and vtables and such.
9927 The linker will arbitrarily choose one and discard the others.
9928 The AT_*_pc values for such functions refer to local labels in
9929 these sections. If the section from that file was discarded, the
9930 labels are not in the output, so the relocs get a value of 0.
9931 If this is a discarded function, mark the pc bounds as invalid,
9932 so that GDB will ignore it. */
9933 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9934 {
bb5ed363 9935 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9936
9937 complaint (&symfile_complaints,
9938 _("DW_AT_low_pc %s is zero "
9939 "for DIE at 0x%x [in module %s]"),
9940 paddress (gdbarch, part_die->lowpc),
bb5ed363 9941 part_die->offset, objfile->name);
9373cf26
JK
9942 }
9943 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9944 else if (part_die->lowpc >= part_die->highpc)
9945 {
bb5ed363 9946 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9947
9948 complaint (&symfile_complaints,
9949 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9950 "for DIE at 0x%x [in module %s]"),
9951 paddress (gdbarch, part_die->lowpc),
9952 paddress (gdbarch, part_die->highpc),
bb5ed363 9953 part_die->offset, objfile->name);
9373cf26
JK
9954 }
9955 else
9956 part_die->has_pc_info = 1;
9957 }
85cbf3d3 9958
c906108c
SS
9959 return info_ptr;
9960}
9961
72bf9492
DJ
9962/* Find a cached partial DIE at OFFSET in CU. */
9963
9964static struct partial_die_info *
c764a876 9965find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9966{
9967 struct partial_die_info *lookup_die = NULL;
9968 struct partial_die_info part_die;
9969
9970 part_die.offset = offset;
9971 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9972
72bf9492
DJ
9973 return lookup_die;
9974}
9975
348e048f
DE
9976/* Find a partial DIE at OFFSET, which may or may not be in CU,
9977 except in the case of .debug_types DIEs which do not reference
9978 outside their CU (they do however referencing other types via
55f1336d 9979 DW_FORM_ref_sig8). */
72bf9492
DJ
9980
9981static struct partial_die_info *
c764a876 9982find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9983{
bb5ed363 9984 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9985 struct dwarf2_per_cu_data *per_cu = NULL;
9986 struct partial_die_info *pd = NULL;
72bf9492 9987
b0df02fd 9988 if (cu->per_cu->debug_types_section)
348e048f
DE
9989 {
9990 pd = find_partial_die_in_comp_unit (offset, cu);
9991 if (pd != NULL)
9992 return pd;
9993 goto not_found;
9994 }
9995
45452591 9996 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9997 {
9998 pd = find_partial_die_in_comp_unit (offset, cu);
9999 if (pd != NULL)
10000 return pd;
10001 }
72bf9492 10002
bb5ed363 10003 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 10004
98bfdba5 10005 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 10006 load_partial_comp_unit (per_cu);
ae038cb0
DJ
10007
10008 per_cu->cu->last_used = 0;
5afb4e99
DJ
10009 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10010
10011 if (pd == NULL && per_cu->load_all_dies == 0)
10012 {
10013 struct cleanup *back_to;
10014 struct partial_die_info comp_unit_die;
10015 struct abbrev_info *abbrev;
10016 unsigned int bytes_read;
10017 char *info_ptr;
10018
10019 per_cu->load_all_dies = 1;
10020
10021 /* Re-read the DIEs. */
10022 back_to = make_cleanup (null_cleanup, 0);
10023 if (per_cu->cu->dwarf2_abbrevs == NULL)
10024 {
bb5ed363 10025 dwarf2_read_abbrevs (objfile->obfd, per_cu->cu);
53d72f98 10026 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10027 }
dce234bc 10028 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
10029 + per_cu->cu->header.offset
10030 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10031 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10032 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 10033 objfile->obfd,
93311388 10034 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10035 per_cu->cu);
10036 if (comp_unit_die.has_children)
bb5ed363 10037 load_partial_dies (objfile->obfd,
93311388
DE
10038 dwarf2_per_objfile->info.buffer, info_ptr,
10039 0, per_cu->cu);
5afb4e99
DJ
10040 do_cleanups (back_to);
10041
10042 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10043 }
10044
348e048f
DE
10045 not_found:
10046
5afb4e99
DJ
10047 if (pd == NULL)
10048 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10049 _("could not find partial DIE 0x%x "
10050 "in cache [from module %s]\n"),
bb5ed363 10051 offset, bfd_get_filename (objfile->obfd));
5afb4e99 10052 return pd;
72bf9492
DJ
10053}
10054
abc72ce4
DE
10055/* See if we can figure out if the class lives in a namespace. We do
10056 this by looking for a member function; its demangled name will
10057 contain namespace info, if there is any. */
10058
10059static void
10060guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10061 struct dwarf2_cu *cu)
10062{
10063 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10064 what template types look like, because the demangler
10065 frequently doesn't give the same name as the debug info. We
10066 could fix this by only using the demangled name to get the
10067 prefix (but see comment in read_structure_type). */
10068
10069 struct partial_die_info *real_pdi;
10070 struct partial_die_info *child_pdi;
10071
10072 /* If this DIE (this DIE's specification, if any) has a parent, then
10073 we should not do this. We'll prepend the parent's fully qualified
10074 name when we create the partial symbol. */
10075
10076 real_pdi = struct_pdi;
10077 while (real_pdi->has_specification)
10078 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10079
10080 if (real_pdi->die_parent != NULL)
10081 return;
10082
10083 for (child_pdi = struct_pdi->die_child;
10084 child_pdi != NULL;
10085 child_pdi = child_pdi->die_sibling)
10086 {
10087 if (child_pdi->tag == DW_TAG_subprogram
10088 && child_pdi->linkage_name != NULL)
10089 {
10090 char *actual_class_name
10091 = language_class_name_from_physname (cu->language_defn,
10092 child_pdi->linkage_name);
10093 if (actual_class_name != NULL)
10094 {
10095 struct_pdi->name
10096 = obsavestring (actual_class_name,
10097 strlen (actual_class_name),
10098 &cu->objfile->objfile_obstack);
10099 xfree (actual_class_name);
10100 }
10101 break;
10102 }
10103 }
10104}
10105
72bf9492
DJ
10106/* Adjust PART_DIE before generating a symbol for it. This function
10107 may set the is_external flag or change the DIE's name. */
10108
10109static void
10110fixup_partial_die (struct partial_die_info *part_die,
10111 struct dwarf2_cu *cu)
10112{
abc72ce4
DE
10113 /* Once we've fixed up a die, there's no point in doing so again.
10114 This also avoids a memory leak if we were to call
10115 guess_partial_die_structure_name multiple times. */
10116 if (part_die->fixup_called)
10117 return;
10118
72bf9492
DJ
10119 /* If we found a reference attribute and the DIE has no name, try
10120 to find a name in the referred to DIE. */
10121
10122 if (part_die->name == NULL && part_die->has_specification)
10123 {
10124 struct partial_die_info *spec_die;
72bf9492 10125
10b3939b 10126 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10127
10b3939b 10128 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10129
10130 if (spec_die->name)
10131 {
10132 part_die->name = spec_die->name;
10133
10134 /* Copy DW_AT_external attribute if it is set. */
10135 if (spec_die->is_external)
10136 part_die->is_external = spec_die->is_external;
10137 }
10138 }
10139
10140 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10141
10142 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10143 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10144
abc72ce4
DE
10145 /* If there is no parent die to provide a namespace, and there are
10146 children, see if we can determine the namespace from their linkage
10147 name.
10148 NOTE: We need to do this even if cu->has_namespace_info != 0.
10149 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10150 if (cu->language == language_cplus
8b70b953 10151 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10152 && part_die->die_parent == NULL
10153 && part_die->has_children
10154 && (part_die->tag == DW_TAG_class_type
10155 || part_die->tag == DW_TAG_structure_type
10156 || part_die->tag == DW_TAG_union_type))
10157 guess_partial_die_structure_name (part_die, cu);
10158
53832f31
TT
10159 /* GCC might emit a nameless struct or union that has a linkage
10160 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10161 if (part_die->name == NULL
96408a79
SA
10162 && (part_die->tag == DW_TAG_class_type
10163 || part_die->tag == DW_TAG_interface_type
10164 || part_die->tag == DW_TAG_structure_type
10165 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10166 && part_die->linkage_name != NULL)
10167 {
10168 char *demangled;
10169
10170 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10171 if (demangled)
10172 {
96408a79
SA
10173 const char *base;
10174
10175 /* Strip any leading namespaces/classes, keep only the base name.
10176 DW_AT_name for named DIEs does not contain the prefixes. */
10177 base = strrchr (demangled, ':');
10178 if (base && base > demangled && base[-1] == ':')
10179 base++;
10180 else
10181 base = demangled;
10182
10183 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10184 &cu->objfile->objfile_obstack);
10185 xfree (demangled);
10186 }
10187 }
10188
abc72ce4 10189 part_die->fixup_called = 1;
72bf9492
DJ
10190}
10191
a8329558 10192/* Read an attribute value described by an attribute form. */
c906108c 10193
fe1b8b76 10194static gdb_byte *
a8329558 10195read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10196 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10197 struct dwarf2_cu *cu)
c906108c 10198{
e7c27a73 10199 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10200 unsigned int bytes_read;
10201 struct dwarf_block *blk;
10202
a8329558
KW
10203 attr->form = form;
10204 switch (form)
c906108c 10205 {
c906108c 10206 case DW_FORM_ref_addr:
ae411497
TT
10207 if (cu->header.version == 2)
10208 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10209 else
3e43a32a
MS
10210 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10211 &cu->header, &bytes_read);
ae411497
TT
10212 info_ptr += bytes_read;
10213 break;
10214 case DW_FORM_addr:
e7c27a73 10215 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10216 info_ptr += bytes_read;
c906108c
SS
10217 break;
10218 case DW_FORM_block2:
7b5a2f43 10219 blk = dwarf_alloc_block (cu);
c906108c
SS
10220 blk->size = read_2_bytes (abfd, info_ptr);
10221 info_ptr += 2;
10222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10223 info_ptr += blk->size;
10224 DW_BLOCK (attr) = blk;
10225 break;
10226 case DW_FORM_block4:
7b5a2f43 10227 blk = dwarf_alloc_block (cu);
c906108c
SS
10228 blk->size = read_4_bytes (abfd, info_ptr);
10229 info_ptr += 4;
10230 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10231 info_ptr += blk->size;
10232 DW_BLOCK (attr) = blk;
10233 break;
10234 case DW_FORM_data2:
10235 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10236 info_ptr += 2;
10237 break;
10238 case DW_FORM_data4:
10239 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10240 info_ptr += 4;
10241 break;
10242 case DW_FORM_data8:
10243 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10244 info_ptr += 8;
10245 break;
2dc7f7b3
TT
10246 case DW_FORM_sec_offset:
10247 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10248 info_ptr += bytes_read;
10249 break;
c906108c 10250 case DW_FORM_string:
9b1c24c8 10251 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10252 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10253 info_ptr += bytes_read;
10254 break;
4bdf3d34
JJ
10255 case DW_FORM_strp:
10256 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10257 &bytes_read);
8285870a 10258 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10259 info_ptr += bytes_read;
10260 break;
2dc7f7b3 10261 case DW_FORM_exprloc:
c906108c 10262 case DW_FORM_block:
7b5a2f43 10263 blk = dwarf_alloc_block (cu);
c906108c
SS
10264 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10265 info_ptr += bytes_read;
10266 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10267 info_ptr += blk->size;
10268 DW_BLOCK (attr) = blk;
10269 break;
10270 case DW_FORM_block1:
7b5a2f43 10271 blk = dwarf_alloc_block (cu);
c906108c
SS
10272 blk->size = read_1_byte (abfd, info_ptr);
10273 info_ptr += 1;
10274 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10275 info_ptr += blk->size;
10276 DW_BLOCK (attr) = blk;
10277 break;
10278 case DW_FORM_data1:
10279 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10280 info_ptr += 1;
10281 break;
10282 case DW_FORM_flag:
10283 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10284 info_ptr += 1;
10285 break;
2dc7f7b3
TT
10286 case DW_FORM_flag_present:
10287 DW_UNSND (attr) = 1;
10288 break;
c906108c
SS
10289 case DW_FORM_sdata:
10290 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10291 info_ptr += bytes_read;
10292 break;
10293 case DW_FORM_udata:
10294 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10295 info_ptr += bytes_read;
10296 break;
10297 case DW_FORM_ref1:
10b3939b 10298 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10299 info_ptr += 1;
10300 break;
10301 case DW_FORM_ref2:
10b3939b 10302 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10303 info_ptr += 2;
10304 break;
10305 case DW_FORM_ref4:
10b3939b 10306 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10307 info_ptr += 4;
10308 break;
613e1657 10309 case DW_FORM_ref8:
10b3939b 10310 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10311 info_ptr += 8;
10312 break;
55f1336d 10313 case DW_FORM_ref_sig8:
348e048f
DE
10314 /* Convert the signature to something we can record in DW_UNSND
10315 for later lookup.
10316 NOTE: This is NULL if the type wasn't found. */
10317 DW_SIGNATURED_TYPE (attr) =
10318 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10319 info_ptr += 8;
10320 break;
c906108c 10321 case DW_FORM_ref_udata:
10b3939b
DJ
10322 DW_ADDR (attr) = (cu->header.offset
10323 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10324 info_ptr += bytes_read;
10325 break;
c906108c 10326 case DW_FORM_indirect:
a8329558
KW
10327 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10328 info_ptr += bytes_read;
e7c27a73 10329 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10330 break;
c906108c 10331 default:
8a3fe4f8 10332 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10333 dwarf_form_name (form),
10334 bfd_get_filename (abfd));
c906108c 10335 }
28e94949
JB
10336
10337 /* We have seen instances where the compiler tried to emit a byte
10338 size attribute of -1 which ended up being encoded as an unsigned
10339 0xffffffff. Although 0xffffffff is technically a valid size value,
10340 an object of this size seems pretty unlikely so we can relatively
10341 safely treat these cases as if the size attribute was invalid and
10342 treat them as zero by default. */
10343 if (attr->name == DW_AT_byte_size
10344 && form == DW_FORM_data4
10345 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10346 {
10347 complaint
10348 (&symfile_complaints,
43bbcdc2
PH
10349 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10350 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10351 DW_UNSND (attr) = 0;
10352 }
28e94949 10353
c906108c
SS
10354 return info_ptr;
10355}
10356
a8329558
KW
10357/* Read an attribute described by an abbreviated attribute. */
10358
fe1b8b76 10359static gdb_byte *
a8329558 10360read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10361 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10362{
10363 attr->name = abbrev->name;
e7c27a73 10364 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10365}
10366
0963b4bd 10367/* Read dwarf information from a buffer. */
c906108c
SS
10368
10369static unsigned int
fe1b8b76 10370read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10371{
fe1b8b76 10372 return bfd_get_8 (abfd, buf);
c906108c
SS
10373}
10374
10375static int
fe1b8b76 10376read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10377{
fe1b8b76 10378 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10379}
10380
10381static unsigned int
fe1b8b76 10382read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10383{
fe1b8b76 10384 return bfd_get_16 (abfd, buf);
c906108c
SS
10385}
10386
21ae7a4d
JK
10387static int
10388read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10389{
10390 return bfd_get_signed_16 (abfd, buf);
10391}
10392
c906108c 10393static unsigned int
fe1b8b76 10394read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10395{
fe1b8b76 10396 return bfd_get_32 (abfd, buf);
c906108c
SS
10397}
10398
21ae7a4d
JK
10399static int
10400read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10401{
10402 return bfd_get_signed_32 (abfd, buf);
10403}
10404
93311388 10405static ULONGEST
fe1b8b76 10406read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10407{
fe1b8b76 10408 return bfd_get_64 (abfd, buf);
c906108c
SS
10409}
10410
10411static CORE_ADDR
fe1b8b76 10412read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10413 unsigned int *bytes_read)
c906108c 10414{
e7c27a73 10415 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10416 CORE_ADDR retval = 0;
10417
107d2387 10418 if (cu_header->signed_addr_p)
c906108c 10419 {
107d2387
AC
10420 switch (cu_header->addr_size)
10421 {
10422 case 2:
fe1b8b76 10423 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10424 break;
10425 case 4:
fe1b8b76 10426 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10427 break;
10428 case 8:
fe1b8b76 10429 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10430 break;
10431 default:
8e65ff28 10432 internal_error (__FILE__, __LINE__,
e2e0b3e5 10433 _("read_address: bad switch, signed [in module %s]"),
659b0389 10434 bfd_get_filename (abfd));
107d2387
AC
10435 }
10436 }
10437 else
10438 {
10439 switch (cu_header->addr_size)
10440 {
10441 case 2:
fe1b8b76 10442 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10443 break;
10444 case 4:
fe1b8b76 10445 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10446 break;
10447 case 8:
fe1b8b76 10448 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10449 break;
10450 default:
8e65ff28 10451 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10452 _("read_address: bad switch, "
10453 "unsigned [in module %s]"),
659b0389 10454 bfd_get_filename (abfd));
107d2387 10455 }
c906108c 10456 }
64367e0a 10457
107d2387
AC
10458 *bytes_read = cu_header->addr_size;
10459 return retval;
c906108c
SS
10460}
10461
f7ef9339 10462/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10463 specification allows the initial length to take up either 4 bytes
10464 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10465 bytes describe the length and all offsets will be 8 bytes in length
10466 instead of 4.
10467
f7ef9339
KB
10468 An older, non-standard 64-bit format is also handled by this
10469 function. The older format in question stores the initial length
10470 as an 8-byte quantity without an escape value. Lengths greater
10471 than 2^32 aren't very common which means that the initial 4 bytes
10472 is almost always zero. Since a length value of zero doesn't make
10473 sense for the 32-bit format, this initial zero can be considered to
10474 be an escape value which indicates the presence of the older 64-bit
10475 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10476 greater than 4GB. If it becomes necessary to handle lengths
10477 somewhat larger than 4GB, we could allow other small values (such
10478 as the non-sensical values of 1, 2, and 3) to also be used as
10479 escape values indicating the presence of the old format.
f7ef9339 10480
917c78fc
MK
10481 The value returned via bytes_read should be used to increment the
10482 relevant pointer after calling read_initial_length().
c764a876 10483
613e1657
KB
10484 [ Note: read_initial_length() and read_offset() are based on the
10485 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10486 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10487 from:
10488
f7ef9339 10489 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10490
613e1657
KB
10491 This document is only a draft and is subject to change. (So beware.)
10492
f7ef9339 10493 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10494 determined empirically by examining 64-bit ELF files produced by
10495 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10496
10497 - Kevin, July 16, 2002
613e1657
KB
10498 ] */
10499
10500static LONGEST
c764a876 10501read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10502{
fe1b8b76 10503 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10504
dd373385 10505 if (length == 0xffffffff)
613e1657 10506 {
fe1b8b76 10507 length = bfd_get_64 (abfd, buf + 4);
613e1657 10508 *bytes_read = 12;
613e1657 10509 }
dd373385 10510 else if (length == 0)
f7ef9339 10511 {
dd373385 10512 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10513 length = bfd_get_64 (abfd, buf);
f7ef9339 10514 *bytes_read = 8;
f7ef9339 10515 }
613e1657
KB
10516 else
10517 {
10518 *bytes_read = 4;
613e1657
KB
10519 }
10520
c764a876
DE
10521 return length;
10522}
dd373385 10523
c764a876
DE
10524/* Cover function for read_initial_length.
10525 Returns the length of the object at BUF, and stores the size of the
10526 initial length in *BYTES_READ and stores the size that offsets will be in
10527 *OFFSET_SIZE.
10528 If the initial length size is not equivalent to that specified in
10529 CU_HEADER then issue a complaint.
10530 This is useful when reading non-comp-unit headers. */
dd373385 10531
c764a876
DE
10532static LONGEST
10533read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10534 const struct comp_unit_head *cu_header,
10535 unsigned int *bytes_read,
10536 unsigned int *offset_size)
10537{
10538 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10539
10540 gdb_assert (cu_header->initial_length_size == 4
10541 || cu_header->initial_length_size == 8
10542 || cu_header->initial_length_size == 12);
10543
10544 if (cu_header->initial_length_size != *bytes_read)
10545 complaint (&symfile_complaints,
10546 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10547
c764a876 10548 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10549 return length;
613e1657
KB
10550}
10551
10552/* Read an offset from the data stream. The size of the offset is
917c78fc 10553 given by cu_header->offset_size. */
613e1657
KB
10554
10555static LONGEST
fe1b8b76 10556read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10557 unsigned int *bytes_read)
c764a876
DE
10558{
10559 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10560
c764a876
DE
10561 *bytes_read = cu_header->offset_size;
10562 return offset;
10563}
10564
10565/* Read an offset from the data stream. */
10566
10567static LONGEST
10568read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10569{
10570 LONGEST retval = 0;
10571
c764a876 10572 switch (offset_size)
613e1657
KB
10573 {
10574 case 4:
fe1b8b76 10575 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10576 break;
10577 case 8:
fe1b8b76 10578 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10579 break;
10580 default:
8e65ff28 10581 internal_error (__FILE__, __LINE__,
c764a876 10582 _("read_offset_1: bad switch [in module %s]"),
659b0389 10583 bfd_get_filename (abfd));
613e1657
KB
10584 }
10585
917c78fc 10586 return retval;
613e1657
KB
10587}
10588
fe1b8b76
JB
10589static gdb_byte *
10590read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10591{
10592 /* If the size of a host char is 8 bits, we can return a pointer
10593 to the buffer, otherwise we have to copy the data to a buffer
10594 allocated on the temporary obstack. */
4bdf3d34 10595 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10596 return buf;
c906108c
SS
10597}
10598
10599static char *
9b1c24c8 10600read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10601{
10602 /* If the size of a host char is 8 bits, we can return a pointer
10603 to the string, otherwise we have to copy the string to a buffer
10604 allocated on the temporary obstack. */
4bdf3d34 10605 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10606 if (*buf == '\0')
10607 {
10608 *bytes_read_ptr = 1;
10609 return NULL;
10610 }
fe1b8b76
JB
10611 *bytes_read_ptr = strlen ((char *) buf) + 1;
10612 return (char *) buf;
4bdf3d34
JJ
10613}
10614
10615static char *
cf2c3c16 10616read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10617{
be391dca 10618 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10619 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10620 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10621 bfd_get_filename (abfd));
dce234bc 10622 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10623 error (_("DW_FORM_strp pointing outside of "
10624 ".debug_str section [in module %s]"),
10625 bfd_get_filename (abfd));
4bdf3d34 10626 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10627 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10628 return NULL;
dce234bc 10629 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10630}
10631
cf2c3c16
TT
10632static char *
10633read_indirect_string (bfd *abfd, gdb_byte *buf,
10634 const struct comp_unit_head *cu_header,
10635 unsigned int *bytes_read_ptr)
10636{
10637 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10638
10639 return read_indirect_string_at_offset (abfd, str_offset);
10640}
10641
ce5d95e1 10642static unsigned long
fe1b8b76 10643read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10644{
ce5d95e1
JB
10645 unsigned long result;
10646 unsigned int num_read;
c906108c
SS
10647 int i, shift;
10648 unsigned char byte;
10649
10650 result = 0;
10651 shift = 0;
10652 num_read = 0;
10653 i = 0;
10654 while (1)
10655 {
fe1b8b76 10656 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10657 buf++;
10658 num_read++;
ce5d95e1 10659 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10660 if ((byte & 128) == 0)
10661 {
10662 break;
10663 }
10664 shift += 7;
10665 }
10666 *bytes_read_ptr = num_read;
10667 return result;
10668}
10669
ce5d95e1 10670static long
fe1b8b76 10671read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10672{
ce5d95e1 10673 long result;
77e0b926 10674 int i, shift, num_read;
c906108c
SS
10675 unsigned char byte;
10676
10677 result = 0;
10678 shift = 0;
c906108c
SS
10679 num_read = 0;
10680 i = 0;
10681 while (1)
10682 {
fe1b8b76 10683 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10684 buf++;
10685 num_read++;
ce5d95e1 10686 result |= ((long)(byte & 127) << shift);
c906108c
SS
10687 shift += 7;
10688 if ((byte & 128) == 0)
10689 {
10690 break;
10691 }
10692 }
77e0b926
DJ
10693 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10694 result |= -(((long)1) << shift);
c906108c
SS
10695 *bytes_read_ptr = num_read;
10696 return result;
10697}
10698
4bb7a0a7
DJ
10699/* Return a pointer to just past the end of an LEB128 number in BUF. */
10700
fe1b8b76
JB
10701static gdb_byte *
10702skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10703{
10704 int byte;
10705
10706 while (1)
10707 {
fe1b8b76 10708 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10709 buf++;
10710 if ((byte & 128) == 0)
10711 return buf;
10712 }
10713}
10714
c906108c 10715static void
e142c38c 10716set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10717{
10718 switch (lang)
10719 {
10720 case DW_LANG_C89:
76bee0cc 10721 case DW_LANG_C99:
c906108c 10722 case DW_LANG_C:
e142c38c 10723 cu->language = language_c;
c906108c
SS
10724 break;
10725 case DW_LANG_C_plus_plus:
e142c38c 10726 cu->language = language_cplus;
c906108c 10727 break;
6aecb9c2
JB
10728 case DW_LANG_D:
10729 cu->language = language_d;
10730 break;
c906108c
SS
10731 case DW_LANG_Fortran77:
10732 case DW_LANG_Fortran90:
b21b22e0 10733 case DW_LANG_Fortran95:
e142c38c 10734 cu->language = language_fortran;
c906108c
SS
10735 break;
10736 case DW_LANG_Mips_Assembler:
e142c38c 10737 cu->language = language_asm;
c906108c 10738 break;
bebd888e 10739 case DW_LANG_Java:
e142c38c 10740 cu->language = language_java;
bebd888e 10741 break;
c906108c 10742 case DW_LANG_Ada83:
8aaf0b47 10743 case DW_LANG_Ada95:
bc5f45f8
JB
10744 cu->language = language_ada;
10745 break;
72019c9c
GM
10746 case DW_LANG_Modula2:
10747 cu->language = language_m2;
10748 break;
fe8e67fd
PM
10749 case DW_LANG_Pascal83:
10750 cu->language = language_pascal;
10751 break;
22566fbd
DJ
10752 case DW_LANG_ObjC:
10753 cu->language = language_objc;
10754 break;
c906108c
SS
10755 case DW_LANG_Cobol74:
10756 case DW_LANG_Cobol85:
c906108c 10757 default:
e142c38c 10758 cu->language = language_minimal;
c906108c
SS
10759 break;
10760 }
e142c38c 10761 cu->language_defn = language_def (cu->language);
c906108c
SS
10762}
10763
10764/* Return the named attribute or NULL if not there. */
10765
10766static struct attribute *
e142c38c 10767dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10768{
10769 unsigned int i;
10770 struct attribute *spec = NULL;
10771
10772 for (i = 0; i < die->num_attrs; ++i)
10773 {
10774 if (die->attrs[i].name == name)
10b3939b 10775 return &die->attrs[i];
c906108c
SS
10776 if (die->attrs[i].name == DW_AT_specification
10777 || die->attrs[i].name == DW_AT_abstract_origin)
10778 spec = &die->attrs[i];
10779 }
c906108c 10780
10b3939b 10781 if (spec)
f2f0e013
DJ
10782 {
10783 die = follow_die_ref (die, spec, &cu);
10784 return dwarf2_attr (die, name, cu);
10785 }
c5aa993b 10786
c906108c
SS
10787 return NULL;
10788}
10789
348e048f
DE
10790/* Return the named attribute or NULL if not there,
10791 but do not follow DW_AT_specification, etc.
10792 This is for use in contexts where we're reading .debug_types dies.
10793 Following DW_AT_specification, DW_AT_abstract_origin will take us
10794 back up the chain, and we want to go down. */
10795
10796static struct attribute *
10797dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10798 struct dwarf2_cu *cu)
10799{
10800 unsigned int i;
10801
10802 for (i = 0; i < die->num_attrs; ++i)
10803 if (die->attrs[i].name == name)
10804 return &die->attrs[i];
10805
10806 return NULL;
10807}
10808
05cf31d1
JB
10809/* Return non-zero iff the attribute NAME is defined for the given DIE,
10810 and holds a non-zero value. This function should only be used for
2dc7f7b3 10811 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10812
10813static int
10814dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10815{
10816 struct attribute *attr = dwarf2_attr (die, name, cu);
10817
10818 return (attr && DW_UNSND (attr));
10819}
10820
3ca72b44 10821static int
e142c38c 10822die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10823{
05cf31d1
JB
10824 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10825 which value is non-zero. However, we have to be careful with
10826 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10827 (via dwarf2_flag_true_p) follows this attribute. So we may
10828 end up accidently finding a declaration attribute that belongs
10829 to a different DIE referenced by the specification attribute,
10830 even though the given DIE does not have a declaration attribute. */
10831 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10832 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10833}
10834
63d06c5c 10835/* Return the die giving the specification for DIE, if there is
f2f0e013 10836 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10837 containing the return value on output. If there is no
10838 specification, but there is an abstract origin, that is
10839 returned. */
63d06c5c
DC
10840
10841static struct die_info *
f2f0e013 10842die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10843{
f2f0e013
DJ
10844 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10845 *spec_cu);
63d06c5c 10846
edb3359d
DJ
10847 if (spec_attr == NULL)
10848 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10849
63d06c5c
DC
10850 if (spec_attr == NULL)
10851 return NULL;
10852 else
f2f0e013 10853 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10854}
c906108c 10855
debd256d 10856/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10857 refers to.
10858 NOTE: This is also used as a "cleanup" function. */
10859
debd256d
JB
10860static void
10861free_line_header (struct line_header *lh)
10862{
10863 if (lh->standard_opcode_lengths)
a8bc7b56 10864 xfree (lh->standard_opcode_lengths);
debd256d
JB
10865
10866 /* Remember that all the lh->file_names[i].name pointers are
10867 pointers into debug_line_buffer, and don't need to be freed. */
10868 if (lh->file_names)
a8bc7b56 10869 xfree (lh->file_names);
debd256d
JB
10870
10871 /* Similarly for the include directory names. */
10872 if (lh->include_dirs)
a8bc7b56 10873 xfree (lh->include_dirs);
debd256d 10874
a8bc7b56 10875 xfree (lh);
debd256d
JB
10876}
10877
debd256d 10878/* Add an entry to LH's include directory table. */
ae2de4f8 10879
debd256d
JB
10880static void
10881add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10882{
debd256d
JB
10883 /* Grow the array if necessary. */
10884 if (lh->include_dirs_size == 0)
c5aa993b 10885 {
debd256d
JB
10886 lh->include_dirs_size = 1; /* for testing */
10887 lh->include_dirs = xmalloc (lh->include_dirs_size
10888 * sizeof (*lh->include_dirs));
10889 }
10890 else if (lh->num_include_dirs >= lh->include_dirs_size)
10891 {
10892 lh->include_dirs_size *= 2;
10893 lh->include_dirs = xrealloc (lh->include_dirs,
10894 (lh->include_dirs_size
10895 * sizeof (*lh->include_dirs)));
c5aa993b 10896 }
c906108c 10897
debd256d
JB
10898 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10899}
6e70227d 10900
debd256d 10901/* Add an entry to LH's file name table. */
ae2de4f8 10902
debd256d
JB
10903static void
10904add_file_name (struct line_header *lh,
10905 char *name,
10906 unsigned int dir_index,
10907 unsigned int mod_time,
10908 unsigned int length)
10909{
10910 struct file_entry *fe;
10911
10912 /* Grow the array if necessary. */
10913 if (lh->file_names_size == 0)
10914 {
10915 lh->file_names_size = 1; /* for testing */
10916 lh->file_names = xmalloc (lh->file_names_size
10917 * sizeof (*lh->file_names));
10918 }
10919 else if (lh->num_file_names >= lh->file_names_size)
10920 {
10921 lh->file_names_size *= 2;
10922 lh->file_names = xrealloc (lh->file_names,
10923 (lh->file_names_size
10924 * sizeof (*lh->file_names)));
10925 }
10926
10927 fe = &lh->file_names[lh->num_file_names++];
10928 fe->name = name;
10929 fe->dir_index = dir_index;
10930 fe->mod_time = mod_time;
10931 fe->length = length;
aaa75496 10932 fe->included_p = 0;
cb1df416 10933 fe->symtab = NULL;
debd256d 10934}
6e70227d 10935
debd256d 10936/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10937 .debug_line, according to the endianness of ABFD. Return a pointer
10938 to a struct line_header, allocated using xmalloc.
debd256d
JB
10939
10940 NOTE: the strings in the include directory and file name tables of
10941 the returned object point into debug_line_buffer, and must not be
10942 freed. */
ae2de4f8 10943
debd256d
JB
10944static struct line_header *
10945dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10946 struct dwarf2_cu *cu)
debd256d
JB
10947{
10948 struct cleanup *back_to;
10949 struct line_header *lh;
fe1b8b76 10950 gdb_byte *line_ptr;
c764a876 10951 unsigned int bytes_read, offset_size;
debd256d
JB
10952 int i;
10953 char *cur_dir, *cur_file;
10954
be391dca 10955 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10956 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10957 {
e2e0b3e5 10958 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10959 return 0;
10960 }
10961
a738430d
MK
10962 /* Make sure that at least there's room for the total_length field.
10963 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10964 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10965 {
4d3c2250 10966 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10967 return 0;
10968 }
10969
10970 lh = xmalloc (sizeof (*lh));
10971 memset (lh, 0, sizeof (*lh));
10972 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10973 (void *) lh);
10974
dce234bc 10975 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10976
a738430d 10977 /* Read in the header. */
6e70227d 10978 lh->total_length =
c764a876
DE
10979 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10980 &bytes_read, &offset_size);
debd256d 10981 line_ptr += bytes_read;
dce234bc
PP
10982 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10983 + dwarf2_per_objfile->line.size))
debd256d 10984 {
4d3c2250 10985 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10986 return 0;
10987 }
10988 lh->statement_program_end = line_ptr + lh->total_length;
10989 lh->version = read_2_bytes (abfd, line_ptr);
10990 line_ptr += 2;
c764a876
DE
10991 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10992 line_ptr += offset_size;
debd256d
JB
10993 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10994 line_ptr += 1;
2dc7f7b3
TT
10995 if (lh->version >= 4)
10996 {
10997 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10998 line_ptr += 1;
10999 }
11000 else
11001 lh->maximum_ops_per_instruction = 1;
11002
11003 if (lh->maximum_ops_per_instruction == 0)
11004 {
11005 lh->maximum_ops_per_instruction = 1;
11006 complaint (&symfile_complaints,
3e43a32a
MS
11007 _("invalid maximum_ops_per_instruction "
11008 "in `.debug_line' section"));
2dc7f7b3
TT
11009 }
11010
debd256d
JB
11011 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11012 line_ptr += 1;
11013 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11014 line_ptr += 1;
11015 lh->line_range = read_1_byte (abfd, line_ptr);
11016 line_ptr += 1;
11017 lh->opcode_base = read_1_byte (abfd, line_ptr);
11018 line_ptr += 1;
11019 lh->standard_opcode_lengths
fe1b8b76 11020 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11021
11022 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11023 for (i = 1; i < lh->opcode_base; ++i)
11024 {
11025 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11026 line_ptr += 1;
11027 }
11028
a738430d 11029 /* Read directory table. */
9b1c24c8 11030 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11031 {
11032 line_ptr += bytes_read;
11033 add_include_dir (lh, cur_dir);
11034 }
11035 line_ptr += bytes_read;
11036
a738430d 11037 /* Read file name table. */
9b1c24c8 11038 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11039 {
11040 unsigned int dir_index, mod_time, length;
11041
11042 line_ptr += bytes_read;
11043 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11044 line_ptr += bytes_read;
11045 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11046 line_ptr += bytes_read;
11047 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11048 line_ptr += bytes_read;
11049
11050 add_file_name (lh, cur_file, dir_index, mod_time, length);
11051 }
11052 line_ptr += bytes_read;
6e70227d 11053 lh->statement_program_start = line_ptr;
debd256d 11054
dce234bc
PP
11055 if (line_ptr > (dwarf2_per_objfile->line.buffer
11056 + dwarf2_per_objfile->line.size))
4d3c2250 11057 complaint (&symfile_complaints,
3e43a32a
MS
11058 _("line number info header doesn't "
11059 "fit in `.debug_line' section"));
debd256d
JB
11060
11061 discard_cleanups (back_to);
11062 return lh;
11063}
c906108c 11064
c6da4cef
DE
11065/* Subroutine of dwarf_decode_lines to simplify it.
11066 Return the file name of the psymtab for included file FILE_INDEX
11067 in line header LH of PST.
11068 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11069 If space for the result is malloc'd, it will be freed by a cleanup.
11070 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11071
11072static char *
11073psymtab_include_file_name (const struct line_header *lh, int file_index,
11074 const struct partial_symtab *pst,
11075 const char *comp_dir)
11076{
11077 const struct file_entry fe = lh->file_names [file_index];
11078 char *include_name = fe.name;
11079 char *include_name_to_compare = include_name;
11080 char *dir_name = NULL;
72b9f47f
TT
11081 const char *pst_filename;
11082 char *copied_name = NULL;
c6da4cef
DE
11083 int file_is_pst;
11084
11085 if (fe.dir_index)
11086 dir_name = lh->include_dirs[fe.dir_index - 1];
11087
11088 if (!IS_ABSOLUTE_PATH (include_name)
11089 && (dir_name != NULL || comp_dir != NULL))
11090 {
11091 /* Avoid creating a duplicate psymtab for PST.
11092 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11093 Before we do the comparison, however, we need to account
11094 for DIR_NAME and COMP_DIR.
11095 First prepend dir_name (if non-NULL). If we still don't
11096 have an absolute path prepend comp_dir (if non-NULL).
11097 However, the directory we record in the include-file's
11098 psymtab does not contain COMP_DIR (to match the
11099 corresponding symtab(s)).
11100
11101 Example:
11102
11103 bash$ cd /tmp
11104 bash$ gcc -g ./hello.c
11105 include_name = "hello.c"
11106 dir_name = "."
11107 DW_AT_comp_dir = comp_dir = "/tmp"
11108 DW_AT_name = "./hello.c" */
11109
11110 if (dir_name != NULL)
11111 {
11112 include_name = concat (dir_name, SLASH_STRING,
11113 include_name, (char *)NULL);
11114 include_name_to_compare = include_name;
11115 make_cleanup (xfree, include_name);
11116 }
11117 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11118 {
11119 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11120 include_name, (char *)NULL);
11121 }
11122 }
11123
11124 pst_filename = pst->filename;
11125 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11126 {
72b9f47f
TT
11127 copied_name = concat (pst->dirname, SLASH_STRING,
11128 pst_filename, (char *)NULL);
11129 pst_filename = copied_name;
c6da4cef
DE
11130 }
11131
1e3fad37 11132 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11133
11134 if (include_name_to_compare != include_name)
11135 xfree (include_name_to_compare);
72b9f47f
TT
11136 if (copied_name != NULL)
11137 xfree (copied_name);
c6da4cef
DE
11138
11139 if (file_is_pst)
11140 return NULL;
11141 return include_name;
11142}
11143
c91513d8
PP
11144/* Ignore this record_line request. */
11145
11146static void
11147noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11148{
11149 return;
11150}
11151
aaa75496
JB
11152/* Decode the Line Number Program (LNP) for the given line_header
11153 structure and CU. The actual information extracted and the type
11154 of structures created from the LNP depends on the value of PST.
11155
11156 1. If PST is NULL, then this procedure uses the data from the program
11157 to create all necessary symbol tables, and their linetables.
6e70227d 11158
aaa75496
JB
11159 2. If PST is not NULL, this procedure reads the program to determine
11160 the list of files included by the unit represented by PST, and
c6da4cef
DE
11161 builds all the associated partial symbol tables.
11162
11163 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11164 It is used for relative paths in the line table.
11165 NOTE: When processing partial symtabs (pst != NULL),
11166 comp_dir == pst->dirname.
11167
11168 NOTE: It is important that psymtabs have the same file name (via strcmp)
11169 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11170 symtab we don't use it in the name of the psymtabs we create.
11171 E.g. expand_line_sal requires this when finding psymtabs to expand.
11172 A good testcase for this is mb-inline.exp. */
debd256d 11173
c906108c 11174static void
72b9f47f 11175dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11176 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11177{
a8c50c1f 11178 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11179 gdb_byte *line_end;
a8c50c1f 11180 unsigned int bytes_read, extended_len;
c906108c 11181 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11182 CORE_ADDR baseaddr;
11183 struct objfile *objfile = cu->objfile;
fbf65064 11184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11185 const int decode_for_pst_p = (pst != NULL);
cb1df416 11186 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11187 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11188 = record_line;
e142c38c
DJ
11189
11190 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11191
debd256d
JB
11192 line_ptr = lh->statement_program_start;
11193 line_end = lh->statement_program_end;
c906108c
SS
11194
11195 /* Read the statement sequences until there's nothing left. */
11196 while (line_ptr < line_end)
11197 {
11198 /* state machine registers */
11199 CORE_ADDR address = 0;
11200 unsigned int file = 1;
11201 unsigned int line = 1;
11202 unsigned int column = 0;
debd256d 11203 int is_stmt = lh->default_is_stmt;
c906108c
SS
11204 int basic_block = 0;
11205 int end_sequence = 0;
fbf65064 11206 CORE_ADDR addr;
2dc7f7b3 11207 unsigned char op_index = 0;
c906108c 11208
aaa75496 11209 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11210 {
aaa75496 11211 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11212 /* lh->include_dirs and lh->file_names are 0-based, but the
11213 directory and file name numbers in the statement program
11214 are 1-based. */
11215 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11216 char *dir = NULL;
a738430d 11217
debd256d
JB
11218 if (fe->dir_index)
11219 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11220
11221 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11222 }
11223
a738430d 11224 /* Decode the table. */
c5aa993b 11225 while (!end_sequence)
c906108c
SS
11226 {
11227 op_code = read_1_byte (abfd, line_ptr);
11228 line_ptr += 1;
59205f5a
JB
11229 if (line_ptr > line_end)
11230 {
11231 dwarf2_debug_line_missing_end_sequence_complaint ();
11232 break;
11233 }
9aa1fe7e 11234
debd256d 11235 if (op_code >= lh->opcode_base)
6e70227d 11236 {
a738430d 11237 /* Special operand. */
debd256d 11238 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11239 address += (((op_index + (adj_opcode / lh->line_range))
11240 / lh->maximum_ops_per_instruction)
11241 * lh->minimum_instruction_length);
11242 op_index = ((op_index + (adj_opcode / lh->line_range))
11243 % lh->maximum_ops_per_instruction);
debd256d 11244 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11245 if (lh->num_file_names < file || file == 0)
25e43795 11246 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11247 /* For now we ignore lines not starting on an
11248 instruction boundary. */
11249 else if (op_index == 0)
25e43795
DJ
11250 {
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 }
25e43795 11261 /* Append row to matrix using current values. */
7019d805 11262 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11263 (*p_record_line) (current_subfile, line, addr);
366da635 11264 }
25e43795 11265 }
ca5f395d 11266 basic_block = 0;
9aa1fe7e
GK
11267 }
11268 else switch (op_code)
c906108c
SS
11269 {
11270 case DW_LNS_extended_op:
3e43a32a
MS
11271 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11272 &bytes_read);
473b7be6 11273 line_ptr += bytes_read;
a8c50c1f 11274 extended_end = line_ptr + extended_len;
c906108c
SS
11275 extended_op = read_1_byte (abfd, line_ptr);
11276 line_ptr += 1;
11277 switch (extended_op)
11278 {
11279 case DW_LNE_end_sequence:
c91513d8 11280 p_record_line = record_line;
c906108c 11281 end_sequence = 1;
c906108c
SS
11282 break;
11283 case DW_LNE_set_address:
e7c27a73 11284 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11285
11286 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11287 {
11288 /* This line table is for a function which has been
11289 GCd by the linker. Ignore it. PR gdb/12528 */
11290
11291 long line_offset
11292 = line_ptr - dwarf2_per_objfile->line.buffer;
11293
11294 complaint (&symfile_complaints,
11295 _(".debug_line address at offset 0x%lx is 0 "
11296 "[in module %s]"),
bb5ed363 11297 line_offset, objfile->name);
c91513d8
PP
11298 p_record_line = noop_record_line;
11299 }
11300
2dc7f7b3 11301 op_index = 0;
107d2387
AC
11302 line_ptr += bytes_read;
11303 address += baseaddr;
c906108c
SS
11304 break;
11305 case DW_LNE_define_file:
debd256d
JB
11306 {
11307 char *cur_file;
11308 unsigned int dir_index, mod_time, length;
6e70227d 11309
3e43a32a
MS
11310 cur_file = read_direct_string (abfd, line_ptr,
11311 &bytes_read);
debd256d
JB
11312 line_ptr += bytes_read;
11313 dir_index =
11314 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11315 line_ptr += bytes_read;
11316 mod_time =
11317 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11318 line_ptr += bytes_read;
11319 length =
11320 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11321 line_ptr += bytes_read;
11322 add_file_name (lh, cur_file, dir_index, mod_time, length);
11323 }
c906108c 11324 break;
d0c6ba3d
CC
11325 case DW_LNE_set_discriminator:
11326 /* The discriminator is not interesting to the debugger;
11327 just ignore it. */
11328 line_ptr = extended_end;
11329 break;
c906108c 11330 default:
4d3c2250 11331 complaint (&symfile_complaints,
e2e0b3e5 11332 _("mangled .debug_line section"));
debd256d 11333 return;
c906108c 11334 }
a8c50c1f
DJ
11335 /* Make sure that we parsed the extended op correctly. If e.g.
11336 we expected a different address size than the producer used,
11337 we may have read the wrong number of bytes. */
11338 if (line_ptr != extended_end)
11339 {
11340 complaint (&symfile_complaints,
11341 _("mangled .debug_line section"));
11342 return;
11343 }
c906108c
SS
11344 break;
11345 case DW_LNS_copy:
59205f5a 11346 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11347 dwarf2_debug_line_missing_file_complaint ();
11348 else
366da635 11349 {
25e43795 11350 lh->file_names[file - 1].included_p = 1;
ca5f395d 11351 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11352 {
11353 if (last_subfile != current_subfile)
11354 {
11355 addr = gdbarch_addr_bits_remove (gdbarch, address);
11356 if (last_subfile)
c91513d8 11357 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11358 last_subfile = current_subfile;
11359 }
7019d805 11360 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11361 (*p_record_line) (current_subfile, line, addr);
fbf65064 11362 }
366da635 11363 }
c906108c
SS
11364 basic_block = 0;
11365 break;
11366 case DW_LNS_advance_pc:
2dc7f7b3
TT
11367 {
11368 CORE_ADDR adjust
11369 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11370
11371 address += (((op_index + adjust)
11372 / lh->maximum_ops_per_instruction)
11373 * lh->minimum_instruction_length);
11374 op_index = ((op_index + adjust)
11375 % lh->maximum_ops_per_instruction);
11376 line_ptr += bytes_read;
11377 }
c906108c
SS
11378 break;
11379 case DW_LNS_advance_line:
11380 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11381 line_ptr += bytes_read;
11382 break;
11383 case DW_LNS_set_file:
debd256d 11384 {
a738430d
MK
11385 /* The arrays lh->include_dirs and lh->file_names are
11386 0-based, but the directory and file name numbers in
11387 the statement program are 1-based. */
debd256d 11388 struct file_entry *fe;
4f1520fb 11389 char *dir = NULL;
a738430d 11390
debd256d
JB
11391 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11392 line_ptr += bytes_read;
59205f5a 11393 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11394 dwarf2_debug_line_missing_file_complaint ();
11395 else
11396 {
11397 fe = &lh->file_names[file - 1];
11398 if (fe->dir_index)
11399 dir = lh->include_dirs[fe->dir_index - 1];
11400 if (!decode_for_pst_p)
11401 {
11402 last_subfile = current_subfile;
11403 dwarf2_start_subfile (fe->name, dir, comp_dir);
11404 }
11405 }
debd256d 11406 }
c906108c
SS
11407 break;
11408 case DW_LNS_set_column:
11409 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11410 line_ptr += bytes_read;
11411 break;
11412 case DW_LNS_negate_stmt:
11413 is_stmt = (!is_stmt);
11414 break;
11415 case DW_LNS_set_basic_block:
11416 basic_block = 1;
11417 break;
c2c6d25f
JM
11418 /* Add to the address register of the state machine the
11419 address increment value corresponding to special opcode
a738430d
MK
11420 255. I.e., this value is scaled by the minimum
11421 instruction length since special opcode 255 would have
b021a221 11422 scaled the increment. */
c906108c 11423 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11424 {
11425 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11426
11427 address += (((op_index + adjust)
11428 / lh->maximum_ops_per_instruction)
11429 * lh->minimum_instruction_length);
11430 op_index = ((op_index + adjust)
11431 % lh->maximum_ops_per_instruction);
11432 }
c906108c
SS
11433 break;
11434 case DW_LNS_fixed_advance_pc:
11435 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11436 op_index = 0;
c906108c
SS
11437 line_ptr += 2;
11438 break;
9aa1fe7e 11439 default:
a738430d
MK
11440 {
11441 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11442 int i;
a738430d 11443
debd256d 11444 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11445 {
11446 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11447 line_ptr += bytes_read;
11448 }
11449 }
c906108c
SS
11450 }
11451 }
59205f5a
JB
11452 if (lh->num_file_names < file || file == 0)
11453 dwarf2_debug_line_missing_file_complaint ();
11454 else
11455 {
11456 lh->file_names[file - 1].included_p = 1;
11457 if (!decode_for_pst_p)
fbf65064
UW
11458 {
11459 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11460 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11461 }
59205f5a 11462 }
c906108c 11463 }
aaa75496
JB
11464
11465 if (decode_for_pst_p)
11466 {
11467 int file_index;
11468
11469 /* Now that we're done scanning the Line Header Program, we can
11470 create the psymtab of each included file. */
11471 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11472 if (lh->file_names[file_index].included_p == 1)
11473 {
c6da4cef
DE
11474 char *include_name =
11475 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11476 if (include_name != NULL)
aaa75496
JB
11477 dwarf2_create_include_psymtab (include_name, pst, objfile);
11478 }
11479 }
cb1df416
DJ
11480 else
11481 {
11482 /* Make sure a symtab is created for every file, even files
11483 which contain only variables (i.e. no code with associated
11484 line numbers). */
11485
11486 int i;
11487 struct file_entry *fe;
11488
11489 for (i = 0; i < lh->num_file_names; i++)
11490 {
11491 char *dir = NULL;
9a619af0 11492
cb1df416
DJ
11493 fe = &lh->file_names[i];
11494 if (fe->dir_index)
11495 dir = lh->include_dirs[fe->dir_index - 1];
11496 dwarf2_start_subfile (fe->name, dir, comp_dir);
11497
11498 /* Skip the main file; we don't need it, and it must be
11499 allocated last, so that it will show up before the
11500 non-primary symtabs in the objfile's symtab list. */
11501 if (current_subfile == first_subfile)
11502 continue;
11503
11504 if (current_subfile->symtab == NULL)
11505 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11506 objfile);
cb1df416
DJ
11507 fe->symtab = current_subfile->symtab;
11508 }
11509 }
c906108c
SS
11510}
11511
11512/* Start a subfile for DWARF. FILENAME is the name of the file and
11513 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11514 or NULL if not known. COMP_DIR is the compilation directory for the
11515 linetable's compilation unit or NULL if not known.
c906108c
SS
11516 This routine tries to keep line numbers from identical absolute and
11517 relative file names in a common subfile.
11518
11519 Using the `list' example from the GDB testsuite, which resides in
11520 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11521 of /srcdir/list0.c yields the following debugging information for list0.c:
11522
c5aa993b
JM
11523 DW_AT_name: /srcdir/list0.c
11524 DW_AT_comp_dir: /compdir
357e46e7 11525 files.files[0].name: list0.h
c5aa993b 11526 files.files[0].dir: /srcdir
357e46e7 11527 files.files[1].name: list0.c
c5aa993b 11528 files.files[1].dir: /srcdir
c906108c
SS
11529
11530 The line number information for list0.c has to end up in a single
4f1520fb
FR
11531 subfile, so that `break /srcdir/list0.c:1' works as expected.
11532 start_subfile will ensure that this happens provided that we pass the
11533 concatenation of files.files[1].dir and files.files[1].name as the
11534 subfile's name. */
c906108c
SS
11535
11536static void
3e43a32a
MS
11537dwarf2_start_subfile (char *filename, const char *dirname,
11538 const char *comp_dir)
c906108c 11539{
4f1520fb
FR
11540 char *fullname;
11541
11542 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11543 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11544 second argument to start_subfile. To be consistent, we do the
11545 same here. In order not to lose the line information directory,
11546 we concatenate it to the filename when it makes sense.
11547 Note that the Dwarf3 standard says (speaking of filenames in line
11548 information): ``The directory index is ignored for file names
11549 that represent full path names''. Thus ignoring dirname in the
11550 `else' branch below isn't an issue. */
c906108c 11551
d5166ae1 11552 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11553 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11554 else
11555 fullname = filename;
c906108c 11556
4f1520fb
FR
11557 start_subfile (fullname, comp_dir);
11558
11559 if (fullname != filename)
11560 xfree (fullname);
c906108c
SS
11561}
11562
4c2df51b
DJ
11563static void
11564var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11565 struct dwarf2_cu *cu)
4c2df51b 11566{
e7c27a73
DJ
11567 struct objfile *objfile = cu->objfile;
11568 struct comp_unit_head *cu_header = &cu->header;
11569
4c2df51b
DJ
11570 /* NOTE drow/2003-01-30: There used to be a comment and some special
11571 code here to turn a symbol with DW_AT_external and a
11572 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11573 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11574 with some versions of binutils) where shared libraries could have
11575 relocations against symbols in their debug information - the
11576 minimal symbol would have the right address, but the debug info
11577 would not. It's no longer necessary, because we will explicitly
11578 apply relocations when we read in the debug information now. */
11579
11580 /* A DW_AT_location attribute with no contents indicates that a
11581 variable has been optimized away. */
11582 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11583 {
11584 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11585 return;
11586 }
11587
11588 /* Handle one degenerate form of location expression specially, to
11589 preserve GDB's previous behavior when section offsets are
11590 specified. If this is just a DW_OP_addr then mark this symbol
11591 as LOC_STATIC. */
11592
11593 if (attr_form_is_block (attr)
11594 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11595 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11596 {
891d2f0b 11597 unsigned int dummy;
4c2df51b
DJ
11598
11599 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11600 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11601 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11602 fixup_symbol_section (sym, objfile);
11603 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11604 SYMBOL_SECTION (sym));
4c2df51b
DJ
11605 return;
11606 }
11607
11608 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11609 expression evaluator, and use LOC_COMPUTED only when necessary
11610 (i.e. when the value of a register or memory location is
11611 referenced, or a thread-local block, etc.). Then again, it might
11612 not be worthwhile. I'm assuming that it isn't unless performance
11613 or memory numbers show me otherwise. */
11614
e7c27a73 11615 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11616 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11617
11618 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11619 cu->has_loclist = 1;
4c2df51b
DJ
11620}
11621
c906108c
SS
11622/* Given a pointer to a DWARF information entry, figure out if we need
11623 to make a symbol table entry for it, and if so, create a new entry
11624 and return a pointer to it.
11625 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11626 used the passed type.
11627 If SPACE is not NULL, use it to hold the new symbol. If it is
11628 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11629
11630static struct symbol *
34eaf542
TT
11631new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11632 struct symbol *space)
c906108c 11633{
e7c27a73 11634 struct objfile *objfile = cu->objfile;
c906108c
SS
11635 struct symbol *sym = NULL;
11636 char *name;
11637 struct attribute *attr = NULL;
11638 struct attribute *attr2 = NULL;
e142c38c 11639 CORE_ADDR baseaddr;
e37fd15a
SW
11640 struct pending **list_to_add = NULL;
11641
edb3359d 11642 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11643
11644 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11645
94af9270 11646 name = dwarf2_name (die, cu);
c906108c
SS
11647 if (name)
11648 {
94af9270 11649 const char *linkagename;
34eaf542 11650 int suppress_add = 0;
94af9270 11651
34eaf542
TT
11652 if (space)
11653 sym = space;
11654 else
11655 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11656 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11657
11658 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11659 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11660 linkagename = dwarf2_physname (name, die, cu);
11661 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11662
f55ee35c
JK
11663 /* Fortran does not have mangling standard and the mangling does differ
11664 between gfortran, iFort etc. */
11665 if (cu->language == language_fortran
b250c185 11666 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11667 symbol_set_demangled_name (&(sym->ginfo),
11668 (char *) dwarf2_full_name (name, die, cu),
11669 NULL);
f55ee35c 11670
c906108c 11671 /* Default assumptions.
c5aa993b 11672 Use the passed type or decode it from the die. */
176620f1 11673 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11674 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11675 if (type != NULL)
11676 SYMBOL_TYPE (sym) = type;
11677 else
e7c27a73 11678 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11679 attr = dwarf2_attr (die,
11680 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11681 cu);
c906108c
SS
11682 if (attr)
11683 {
11684 SYMBOL_LINE (sym) = DW_UNSND (attr);
11685 }
cb1df416 11686
edb3359d
DJ
11687 attr = dwarf2_attr (die,
11688 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11689 cu);
cb1df416
DJ
11690 if (attr)
11691 {
11692 int file_index = DW_UNSND (attr);
9a619af0 11693
cb1df416
DJ
11694 if (cu->line_header == NULL
11695 || file_index > cu->line_header->num_file_names)
11696 complaint (&symfile_complaints,
11697 _("file index out of range"));
1c3d648d 11698 else if (file_index > 0)
cb1df416
DJ
11699 {
11700 struct file_entry *fe;
9a619af0 11701
cb1df416
DJ
11702 fe = &cu->line_header->file_names[file_index - 1];
11703 SYMBOL_SYMTAB (sym) = fe->symtab;
11704 }
11705 }
11706
c906108c
SS
11707 switch (die->tag)
11708 {
11709 case DW_TAG_label:
e142c38c 11710 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11711 if (attr)
11712 {
11713 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11714 }
0f5238ed
TT
11715 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11716 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11717 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11718 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11719 break;
11720 case DW_TAG_subprogram:
11721 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11722 finish_block. */
11723 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11724 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11725 if ((attr2 && (DW_UNSND (attr2) != 0))
11726 || cu->language == language_ada)
c906108c 11727 {
2cfa0c8d
JB
11728 /* Subprograms marked external are stored as a global symbol.
11729 Ada subprograms, whether marked external or not, are always
11730 stored as a global symbol, because we want to be able to
11731 access them globally. For instance, we want to be able
11732 to break on a nested subprogram without having to
11733 specify the context. */
e37fd15a 11734 list_to_add = &global_symbols;
c906108c
SS
11735 }
11736 else
11737 {
e37fd15a 11738 list_to_add = cu->list_in_scope;
c906108c
SS
11739 }
11740 break;
edb3359d
DJ
11741 case DW_TAG_inlined_subroutine:
11742 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11743 finish_block. */
11744 SYMBOL_CLASS (sym) = LOC_BLOCK;
11745 SYMBOL_INLINED (sym) = 1;
11746 /* Do not add the symbol to any lists. It will be found via
11747 BLOCK_FUNCTION from the blockvector. */
11748 break;
34eaf542
TT
11749 case DW_TAG_template_value_param:
11750 suppress_add = 1;
11751 /* Fall through. */
72929c62 11752 case DW_TAG_constant:
c906108c 11753 case DW_TAG_variable:
254e6b9e 11754 case DW_TAG_member:
0963b4bd
MS
11755 /* Compilation with minimal debug info may result in
11756 variables with missing type entries. Change the
11757 misleading `void' type to something sensible. */
c906108c 11758 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11759 SYMBOL_TYPE (sym)
46bf5051 11760 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11761
e142c38c 11762 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11763 /* In the case of DW_TAG_member, we should only be called for
11764 static const members. */
11765 if (die->tag == DW_TAG_member)
11766 {
3863f96c
DE
11767 /* dwarf2_add_field uses die_is_declaration,
11768 so we do the same. */
254e6b9e
DE
11769 gdb_assert (die_is_declaration (die, cu));
11770 gdb_assert (attr);
11771 }
c906108c
SS
11772 if (attr)
11773 {
e7c27a73 11774 dwarf2_const_value (attr, sym, cu);
e142c38c 11775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11776 if (!suppress_add)
34eaf542
TT
11777 {
11778 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11779 list_to_add = &global_symbols;
34eaf542 11780 else
e37fd15a 11781 list_to_add = cu->list_in_scope;
34eaf542 11782 }
c906108c
SS
11783 break;
11784 }
e142c38c 11785 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11786 if (attr)
11787 {
e7c27a73 11788 var_decode_location (attr, sym, cu);
e142c38c 11789 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11790 if (SYMBOL_CLASS (sym) == LOC_STATIC
11791 && SYMBOL_VALUE_ADDRESS (sym) == 0
11792 && !dwarf2_per_objfile->has_section_at_zero)
11793 {
11794 /* When a static variable is eliminated by the linker,
11795 the corresponding debug information is not stripped
11796 out, but the variable address is set to null;
11797 do not add such variables into symbol table. */
11798 }
11799 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11800 {
f55ee35c
JK
11801 /* Workaround gfortran PR debug/40040 - it uses
11802 DW_AT_location for variables in -fPIC libraries which may
11803 get overriden by other libraries/executable and get
11804 a different address. Resolve it by the minimal symbol
11805 which may come from inferior's executable using copy
11806 relocation. Make this workaround only for gfortran as for
11807 other compilers GDB cannot guess the minimal symbol
11808 Fortran mangling kind. */
11809 if (cu->language == language_fortran && die->parent
11810 && die->parent->tag == DW_TAG_module
11811 && cu->producer
11812 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11813 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11814
1c809c68
TT
11815 /* A variable with DW_AT_external is never static,
11816 but it may be block-scoped. */
11817 list_to_add = (cu->list_in_scope == &file_symbols
11818 ? &global_symbols : cu->list_in_scope);
1c809c68 11819 }
c906108c 11820 else
e37fd15a 11821 list_to_add = cu->list_in_scope;
c906108c
SS
11822 }
11823 else
11824 {
11825 /* We do not know the address of this symbol.
c5aa993b
JM
11826 If it is an external symbol and we have type information
11827 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11828 The address of the variable will then be determined from
11829 the minimal symbol table whenever the variable is
11830 referenced. */
e142c38c 11831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11832 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11833 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11834 {
0fe7935b
DJ
11835 /* A variable with DW_AT_external is never static, but it
11836 may be block-scoped. */
11837 list_to_add = (cu->list_in_scope == &file_symbols
11838 ? &global_symbols : cu->list_in_scope);
11839
c906108c 11840 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11841 }
442ddf59
JK
11842 else if (!die_is_declaration (die, cu))
11843 {
11844 /* Use the default LOC_OPTIMIZED_OUT class. */
11845 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11846 if (!suppress_add)
11847 list_to_add = cu->list_in_scope;
442ddf59 11848 }
c906108c
SS
11849 }
11850 break;
11851 case DW_TAG_formal_parameter:
edb3359d
DJ
11852 /* If we are inside a function, mark this as an argument. If
11853 not, we might be looking at an argument to an inlined function
11854 when we do not have enough information to show inlined frames;
11855 pretend it's a local variable in that case so that the user can
11856 still see it. */
11857 if (context_stack_depth > 0
11858 && context_stack[context_stack_depth - 1].name != NULL)
11859 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11860 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11861 if (attr)
11862 {
e7c27a73 11863 var_decode_location (attr, sym, cu);
c906108c 11864 }
e142c38c 11865 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11866 if (attr)
11867 {
e7c27a73 11868 dwarf2_const_value (attr, sym, cu);
c906108c 11869 }
f346a30d 11870
e37fd15a 11871 list_to_add = cu->list_in_scope;
c906108c
SS
11872 break;
11873 case DW_TAG_unspecified_parameters:
11874 /* From varargs functions; gdb doesn't seem to have any
11875 interest in this information, so just ignore it for now.
11876 (FIXME?) */
11877 break;
34eaf542
TT
11878 case DW_TAG_template_type_param:
11879 suppress_add = 1;
11880 /* Fall through. */
c906108c 11881 case DW_TAG_class_type:
680b30c7 11882 case DW_TAG_interface_type:
c906108c
SS
11883 case DW_TAG_structure_type:
11884 case DW_TAG_union_type:
72019c9c 11885 case DW_TAG_set_type:
c906108c
SS
11886 case DW_TAG_enumeration_type:
11887 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11888 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11889
63d06c5c 11890 {
987504bb 11891 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11892 really ever be static objects: otherwise, if you try
11893 to, say, break of a class's method and you're in a file
11894 which doesn't mention that class, it won't work unless
11895 the check for all static symbols in lookup_symbol_aux
11896 saves you. See the OtherFileClass tests in
11897 gdb.c++/namespace.exp. */
11898
e37fd15a 11899 if (!suppress_add)
34eaf542 11900 {
34eaf542
TT
11901 list_to_add = (cu->list_in_scope == &file_symbols
11902 && (cu->language == language_cplus
11903 || cu->language == language_java)
11904 ? &global_symbols : cu->list_in_scope);
63d06c5c 11905
64382290
TT
11906 /* The semantics of C++ state that "struct foo {
11907 ... }" also defines a typedef for "foo". A Java
11908 class declaration also defines a typedef for the
11909 class. */
11910 if (cu->language == language_cplus
11911 || cu->language == language_java
11912 || cu->language == language_ada)
11913 {
11914 /* The symbol's name is already allocated along
11915 with this objfile, so we don't need to
11916 duplicate it for the type. */
11917 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11918 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11919 }
63d06c5c
DC
11920 }
11921 }
c906108c
SS
11922 break;
11923 case DW_TAG_typedef:
63d06c5c
DC
11924 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11925 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11926 list_to_add = cu->list_in_scope;
63d06c5c 11927 break;
c906108c 11928 case DW_TAG_base_type:
a02abb62 11929 case DW_TAG_subrange_type:
c906108c 11930 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11931 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11932 list_to_add = cu->list_in_scope;
c906108c
SS
11933 break;
11934 case DW_TAG_enumerator:
e142c38c 11935 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11936 if (attr)
11937 {
e7c27a73 11938 dwarf2_const_value (attr, sym, cu);
c906108c 11939 }
63d06c5c
DC
11940 {
11941 /* NOTE: carlton/2003-11-10: See comment above in the
11942 DW_TAG_class_type, etc. block. */
11943
e142c38c 11944 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11945 && (cu->language == language_cplus
11946 || cu->language == language_java)
e142c38c 11947 ? &global_symbols : cu->list_in_scope);
63d06c5c 11948 }
c906108c 11949 break;
5c4e30ca
DC
11950 case DW_TAG_namespace:
11951 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11952 list_to_add = &global_symbols;
5c4e30ca 11953 break;
c906108c
SS
11954 default:
11955 /* Not a tag we recognize. Hopefully we aren't processing
11956 trash data, but since we must specifically ignore things
11957 we don't recognize, there is nothing else we should do at
0963b4bd 11958 this point. */
e2e0b3e5 11959 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11960 dwarf_tag_name (die->tag));
c906108c
SS
11961 break;
11962 }
df8a16a1 11963
e37fd15a
SW
11964 if (suppress_add)
11965 {
11966 sym->hash_next = objfile->template_symbols;
11967 objfile->template_symbols = sym;
11968 list_to_add = NULL;
11969 }
11970
11971 if (list_to_add != NULL)
11972 add_symbol_to_list (sym, list_to_add);
11973
df8a16a1
DJ
11974 /* For the benefit of old versions of GCC, check for anonymous
11975 namespaces based on the demangled name. */
11976 if (!processing_has_namespace_info
94af9270 11977 && cu->language == language_cplus)
a10964d1 11978 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11979 }
11980 return (sym);
11981}
11982
34eaf542
TT
11983/* A wrapper for new_symbol_full that always allocates a new symbol. */
11984
11985static struct symbol *
11986new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11987{
11988 return new_symbol_full (die, type, cu, NULL);
11989}
11990
98bfdba5
PA
11991/* Given an attr with a DW_FORM_dataN value in host byte order,
11992 zero-extend it as appropriate for the symbol's type. The DWARF
11993 standard (v4) is not entirely clear about the meaning of using
11994 DW_FORM_dataN for a constant with a signed type, where the type is
11995 wider than the data. The conclusion of a discussion on the DWARF
11996 list was that this is unspecified. We choose to always zero-extend
11997 because that is the interpretation long in use by GCC. */
c906108c 11998
98bfdba5
PA
11999static gdb_byte *
12000dwarf2_const_value_data (struct attribute *attr, struct type *type,
12001 const char *name, struct obstack *obstack,
12002 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12003{
e7c27a73 12004 struct objfile *objfile = cu->objfile;
e17a4113
UW
12005 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12006 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12007 LONGEST l = DW_UNSND (attr);
12008
12009 if (bits < sizeof (*value) * 8)
12010 {
12011 l &= ((LONGEST) 1 << bits) - 1;
12012 *value = l;
12013 }
12014 else if (bits == sizeof (*value) * 8)
12015 *value = l;
12016 else
12017 {
12018 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12019 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12020 return bytes;
12021 }
12022
12023 return NULL;
12024}
12025
12026/* Read a constant value from an attribute. Either set *VALUE, or if
12027 the value does not fit in *VALUE, set *BYTES - either already
12028 allocated on the objfile obstack, or newly allocated on OBSTACK,
12029 or, set *BATON, if we translated the constant to a location
12030 expression. */
12031
12032static void
12033dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12034 const char *name, struct obstack *obstack,
12035 struct dwarf2_cu *cu,
12036 long *value, gdb_byte **bytes,
12037 struct dwarf2_locexpr_baton **baton)
12038{
12039 struct objfile *objfile = cu->objfile;
12040 struct comp_unit_head *cu_header = &cu->header;
c906108c 12041 struct dwarf_block *blk;
98bfdba5
PA
12042 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12043 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12044
12045 *value = 0;
12046 *bytes = NULL;
12047 *baton = NULL;
c906108c
SS
12048
12049 switch (attr->form)
12050 {
12051 case DW_FORM_addr:
ac56253d 12052 {
ac56253d
TT
12053 gdb_byte *data;
12054
98bfdba5
PA
12055 if (TYPE_LENGTH (type) != cu_header->addr_size)
12056 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12057 cu_header->addr_size,
98bfdba5 12058 TYPE_LENGTH (type));
ac56253d
TT
12059 /* Symbols of this form are reasonably rare, so we just
12060 piggyback on the existing location code rather than writing
12061 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12062 *baton = obstack_alloc (&objfile->objfile_obstack,
12063 sizeof (struct dwarf2_locexpr_baton));
12064 (*baton)->per_cu = cu->per_cu;
12065 gdb_assert ((*baton)->per_cu);
ac56253d 12066
98bfdba5
PA
12067 (*baton)->size = 2 + cu_header->addr_size;
12068 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12069 (*baton)->data = data;
ac56253d
TT
12070
12071 data[0] = DW_OP_addr;
12072 store_unsigned_integer (&data[1], cu_header->addr_size,
12073 byte_order, DW_ADDR (attr));
12074 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12075 }
c906108c 12076 break;
4ac36638 12077 case DW_FORM_string:
93b5768b 12078 case DW_FORM_strp:
98bfdba5
PA
12079 /* DW_STRING is already allocated on the objfile obstack, point
12080 directly to it. */
12081 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12082 break;
c906108c
SS
12083 case DW_FORM_block1:
12084 case DW_FORM_block2:
12085 case DW_FORM_block4:
12086 case DW_FORM_block:
2dc7f7b3 12087 case DW_FORM_exprloc:
c906108c 12088 blk = DW_BLOCK (attr);
98bfdba5
PA
12089 if (TYPE_LENGTH (type) != blk->size)
12090 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12091 TYPE_LENGTH (type));
12092 *bytes = blk->data;
c906108c 12093 break;
2df3850c
JM
12094
12095 /* The DW_AT_const_value attributes are supposed to carry the
12096 symbol's value "represented as it would be on the target
12097 architecture." By the time we get here, it's already been
12098 converted to host endianness, so we just need to sign- or
12099 zero-extend it as appropriate. */
12100 case DW_FORM_data1:
3e43a32a
MS
12101 *bytes = dwarf2_const_value_data (attr, type, name,
12102 obstack, cu, value, 8);
2df3850c 12103 break;
c906108c 12104 case DW_FORM_data2:
3e43a32a
MS
12105 *bytes = dwarf2_const_value_data (attr, type, name,
12106 obstack, cu, value, 16);
2df3850c 12107 break;
c906108c 12108 case DW_FORM_data4:
3e43a32a
MS
12109 *bytes = dwarf2_const_value_data (attr, type, name,
12110 obstack, cu, value, 32);
2df3850c 12111 break;
c906108c 12112 case DW_FORM_data8:
3e43a32a
MS
12113 *bytes = dwarf2_const_value_data (attr, type, name,
12114 obstack, cu, value, 64);
2df3850c
JM
12115 break;
12116
c906108c 12117 case DW_FORM_sdata:
98bfdba5 12118 *value = DW_SND (attr);
2df3850c
JM
12119 break;
12120
c906108c 12121 case DW_FORM_udata:
98bfdba5 12122 *value = DW_UNSND (attr);
c906108c 12123 break;
2df3850c 12124
c906108c 12125 default:
4d3c2250 12126 complaint (&symfile_complaints,
e2e0b3e5 12127 _("unsupported const value attribute form: '%s'"),
4d3c2250 12128 dwarf_form_name (attr->form));
98bfdba5 12129 *value = 0;
c906108c
SS
12130 break;
12131 }
12132}
12133
2df3850c 12134
98bfdba5
PA
12135/* Copy constant value from an attribute to a symbol. */
12136
2df3850c 12137static void
98bfdba5
PA
12138dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12139 struct dwarf2_cu *cu)
2df3850c 12140{
98bfdba5
PA
12141 struct objfile *objfile = cu->objfile;
12142 struct comp_unit_head *cu_header = &cu->header;
12143 long value;
12144 gdb_byte *bytes;
12145 struct dwarf2_locexpr_baton *baton;
2df3850c 12146
98bfdba5
PA
12147 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12148 SYMBOL_PRINT_NAME (sym),
12149 &objfile->objfile_obstack, cu,
12150 &value, &bytes, &baton);
2df3850c 12151
98bfdba5
PA
12152 if (baton != NULL)
12153 {
12154 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12155 SYMBOL_LOCATION_BATON (sym) = baton;
12156 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12157 }
12158 else if (bytes != NULL)
12159 {
12160 SYMBOL_VALUE_BYTES (sym) = bytes;
12161 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12162 }
12163 else
12164 {
12165 SYMBOL_VALUE (sym) = value;
12166 SYMBOL_CLASS (sym) = LOC_CONST;
12167 }
2df3850c
JM
12168}
12169
c906108c
SS
12170/* Return the type of the die in question using its DW_AT_type attribute. */
12171
12172static struct type *
e7c27a73 12173die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12174{
c906108c 12175 struct attribute *type_attr;
c906108c 12176
e142c38c 12177 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12178 if (!type_attr)
12179 {
12180 /* A missing DW_AT_type represents a void type. */
46bf5051 12181 return objfile_type (cu->objfile)->builtin_void;
c906108c 12182 }
348e048f 12183
673bfd45 12184 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12185}
12186
b4ba55a1
JB
12187/* True iff CU's producer generates GNAT Ada auxiliary information
12188 that allows to find parallel types through that information instead
12189 of having to do expensive parallel lookups by type name. */
12190
12191static int
12192need_gnat_info (struct dwarf2_cu *cu)
12193{
12194 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12195 of GNAT produces this auxiliary information, without any indication
12196 that it is produced. Part of enhancing the FSF version of GNAT
12197 to produce that information will be to put in place an indicator
12198 that we can use in order to determine whether the descriptive type
12199 info is available or not. One suggestion that has been made is
12200 to use a new attribute, attached to the CU die. For now, assume
12201 that the descriptive type info is not available. */
12202 return 0;
12203}
12204
b4ba55a1
JB
12205/* Return the auxiliary type of the die in question using its
12206 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12207 attribute is not present. */
12208
12209static struct type *
12210die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12211{
b4ba55a1 12212 struct attribute *type_attr;
b4ba55a1
JB
12213
12214 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12215 if (!type_attr)
12216 return NULL;
12217
673bfd45 12218 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12219}
12220
12221/* If DIE has a descriptive_type attribute, then set the TYPE's
12222 descriptive type accordingly. */
12223
12224static void
12225set_descriptive_type (struct type *type, struct die_info *die,
12226 struct dwarf2_cu *cu)
12227{
12228 struct type *descriptive_type = die_descriptive_type (die, cu);
12229
12230 if (descriptive_type)
12231 {
12232 ALLOCATE_GNAT_AUX_TYPE (type);
12233 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12234 }
12235}
12236
c906108c
SS
12237/* Return the containing type of the die in question using its
12238 DW_AT_containing_type attribute. */
12239
12240static struct type *
e7c27a73 12241die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12242{
c906108c 12243 struct attribute *type_attr;
c906108c 12244
e142c38c 12245 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12246 if (!type_attr)
12247 error (_("Dwarf Error: Problem turning containing type into gdb type "
12248 "[in module %s]"), cu->objfile->name);
12249
673bfd45 12250 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12251}
12252
673bfd45
DE
12253/* Look up the type of DIE in CU using its type attribute ATTR.
12254 If there is no type substitute an error marker. */
12255
c906108c 12256static struct type *
673bfd45
DE
12257lookup_die_type (struct die_info *die, struct attribute *attr,
12258 struct dwarf2_cu *cu)
c906108c 12259{
bb5ed363 12260 struct objfile *objfile = cu->objfile;
f792889a
DJ
12261 struct type *this_type;
12262
673bfd45
DE
12263 /* First see if we have it cached. */
12264
12265 if (is_ref_attr (attr))
12266 {
12267 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12268
12269 this_type = get_die_type_at_offset (offset, cu->per_cu);
12270 }
55f1336d 12271 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12272 {
12273 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12274 struct dwarf2_cu *sig_cu;
12275 unsigned int offset;
12276
12277 /* sig_type will be NULL if the signatured type is missing from
12278 the debug info. */
12279 if (sig_type == NULL)
12280 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12281 "at 0x%x [in module %s]"),
bb5ed363 12282 die->offset, objfile->name);
673bfd45 12283
b0df02fd 12284 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12285 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12286 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12287 }
12288 else
12289 {
12290 dump_die_for_error (die);
12291 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12292 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12293 }
12294
12295 /* If not cached we need to read it in. */
12296
12297 if (this_type == NULL)
12298 {
12299 struct die_info *type_die;
12300 struct dwarf2_cu *type_cu = cu;
12301
12302 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12303 /* If the type is cached, we should have found it above. */
12304 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12305 this_type = read_type_die_1 (type_die, type_cu);
12306 }
12307
12308 /* If we still don't have a type use an error marker. */
12309
12310 if (this_type == NULL)
c906108c 12311 {
b00fdb78
TT
12312 char *message, *saved;
12313
12314 /* read_type_die already issued a complaint. */
12315 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12316 objfile->name,
b00fdb78
TT
12317 cu->header.offset,
12318 die->offset);
bb5ed363 12319 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12320 message, strlen (message));
12321 xfree (message);
12322
bb5ed363 12323 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12324 }
673bfd45 12325
f792889a 12326 return this_type;
c906108c
SS
12327}
12328
673bfd45
DE
12329/* Return the type in DIE, CU.
12330 Returns NULL for invalid types.
12331
12332 This first does a lookup in the appropriate type_hash table,
12333 and only reads the die in if necessary.
12334
12335 NOTE: This can be called when reading in partial or full symbols. */
12336
f792889a 12337static struct type *
e7c27a73 12338read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12339{
f792889a
DJ
12340 struct type *this_type;
12341
12342 this_type = get_die_type (die, cu);
12343 if (this_type)
12344 return this_type;
12345
673bfd45
DE
12346 return read_type_die_1 (die, cu);
12347}
12348
12349/* Read the type in DIE, CU.
12350 Returns NULL for invalid types. */
12351
12352static struct type *
12353read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12354{
12355 struct type *this_type = NULL;
12356
c906108c
SS
12357 switch (die->tag)
12358 {
12359 case DW_TAG_class_type:
680b30c7 12360 case DW_TAG_interface_type:
c906108c
SS
12361 case DW_TAG_structure_type:
12362 case DW_TAG_union_type:
f792889a 12363 this_type = read_structure_type (die, cu);
c906108c
SS
12364 break;
12365 case DW_TAG_enumeration_type:
f792889a 12366 this_type = read_enumeration_type (die, cu);
c906108c
SS
12367 break;
12368 case DW_TAG_subprogram:
12369 case DW_TAG_subroutine_type:
edb3359d 12370 case DW_TAG_inlined_subroutine:
f792889a 12371 this_type = read_subroutine_type (die, cu);
c906108c
SS
12372 break;
12373 case DW_TAG_array_type:
f792889a 12374 this_type = read_array_type (die, cu);
c906108c 12375 break;
72019c9c 12376 case DW_TAG_set_type:
f792889a 12377 this_type = read_set_type (die, cu);
72019c9c 12378 break;
c906108c 12379 case DW_TAG_pointer_type:
f792889a 12380 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12381 break;
12382 case DW_TAG_ptr_to_member_type:
f792889a 12383 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12384 break;
12385 case DW_TAG_reference_type:
f792889a 12386 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12387 break;
12388 case DW_TAG_const_type:
f792889a 12389 this_type = read_tag_const_type (die, cu);
c906108c
SS
12390 break;
12391 case DW_TAG_volatile_type:
f792889a 12392 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12393 break;
12394 case DW_TAG_string_type:
f792889a 12395 this_type = read_tag_string_type (die, cu);
c906108c
SS
12396 break;
12397 case DW_TAG_typedef:
f792889a 12398 this_type = read_typedef (die, cu);
c906108c 12399 break;
a02abb62 12400 case DW_TAG_subrange_type:
f792889a 12401 this_type = read_subrange_type (die, cu);
a02abb62 12402 break;
c906108c 12403 case DW_TAG_base_type:
f792889a 12404 this_type = read_base_type (die, cu);
c906108c 12405 break;
81a17f79 12406 case DW_TAG_unspecified_type:
f792889a 12407 this_type = read_unspecified_type (die, cu);
81a17f79 12408 break;
0114d602
DJ
12409 case DW_TAG_namespace:
12410 this_type = read_namespace_type (die, cu);
12411 break;
f55ee35c
JK
12412 case DW_TAG_module:
12413 this_type = read_module_type (die, cu);
12414 break;
c906108c 12415 default:
3e43a32a
MS
12416 complaint (&symfile_complaints,
12417 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12418 dwarf_tag_name (die->tag));
c906108c
SS
12419 break;
12420 }
63d06c5c 12421
f792889a 12422 return this_type;
63d06c5c
DC
12423}
12424
abc72ce4
DE
12425/* See if we can figure out if the class lives in a namespace. We do
12426 this by looking for a member function; its demangled name will
12427 contain namespace info, if there is any.
12428 Return the computed name or NULL.
12429 Space for the result is allocated on the objfile's obstack.
12430 This is the full-die version of guess_partial_die_structure_name.
12431 In this case we know DIE has no useful parent. */
12432
12433static char *
12434guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12435{
12436 struct die_info *spec_die;
12437 struct dwarf2_cu *spec_cu;
12438 struct die_info *child;
12439
12440 spec_cu = cu;
12441 spec_die = die_specification (die, &spec_cu);
12442 if (spec_die != NULL)
12443 {
12444 die = spec_die;
12445 cu = spec_cu;
12446 }
12447
12448 for (child = die->child;
12449 child != NULL;
12450 child = child->sibling)
12451 {
12452 if (child->tag == DW_TAG_subprogram)
12453 {
12454 struct attribute *attr;
12455
12456 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12457 if (attr == NULL)
12458 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12459 if (attr != NULL)
12460 {
12461 char *actual_name
12462 = language_class_name_from_physname (cu->language_defn,
12463 DW_STRING (attr));
12464 char *name = NULL;
12465
12466 if (actual_name != NULL)
12467 {
12468 char *die_name = dwarf2_name (die, cu);
12469
12470 if (die_name != NULL
12471 && strcmp (die_name, actual_name) != 0)
12472 {
12473 /* Strip off the class name from the full name.
12474 We want the prefix. */
12475 int die_name_len = strlen (die_name);
12476 int actual_name_len = strlen (actual_name);
12477
12478 /* Test for '::' as a sanity check. */
12479 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12480 && actual_name[actual_name_len
12481 - die_name_len - 1] == ':')
abc72ce4
DE
12482 name =
12483 obsavestring (actual_name,
12484 actual_name_len - die_name_len - 2,
12485 &cu->objfile->objfile_obstack);
12486 }
12487 }
12488 xfree (actual_name);
12489 return name;
12490 }
12491 }
12492 }
12493
12494 return NULL;
12495}
12496
96408a79
SA
12497/* GCC might emit a nameless typedef that has a linkage name. Determine the
12498 prefix part in such case. See
12499 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12500
12501static char *
12502anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12503{
12504 struct attribute *attr;
12505 char *base;
12506
12507 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12508 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12509 return NULL;
12510
12511 attr = dwarf2_attr (die, DW_AT_name, cu);
12512 if (attr != NULL && DW_STRING (attr) != NULL)
12513 return NULL;
12514
12515 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12516 if (attr == NULL)
12517 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12518 if (attr == NULL || DW_STRING (attr) == NULL)
12519 return NULL;
12520
12521 /* dwarf2_name had to be already called. */
12522 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12523
12524 /* Strip the base name, keep any leading namespaces/classes. */
12525 base = strrchr (DW_STRING (attr), ':');
12526 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12527 return "";
12528
12529 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12530 &cu->objfile->objfile_obstack);
12531}
12532
fdde2d81 12533/* Return the name of the namespace/class that DIE is defined within,
0114d602 12534 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12535
0114d602
DJ
12536 For example, if we're within the method foo() in the following
12537 code:
12538
12539 namespace N {
12540 class C {
12541 void foo () {
12542 }
12543 };
12544 }
12545
12546 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12547
12548static char *
e142c38c 12549determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12550{
0114d602
DJ
12551 struct die_info *parent, *spec_die;
12552 struct dwarf2_cu *spec_cu;
12553 struct type *parent_type;
96408a79 12554 char *retval;
63d06c5c 12555
f55ee35c
JK
12556 if (cu->language != language_cplus && cu->language != language_java
12557 && cu->language != language_fortran)
0114d602
DJ
12558 return "";
12559
96408a79
SA
12560 retval = anonymous_struct_prefix (die, cu);
12561 if (retval)
12562 return retval;
12563
0114d602
DJ
12564 /* We have to be careful in the presence of DW_AT_specification.
12565 For example, with GCC 3.4, given the code
12566
12567 namespace N {
12568 void foo() {
12569 // Definition of N::foo.
12570 }
12571 }
12572
12573 then we'll have a tree of DIEs like this:
12574
12575 1: DW_TAG_compile_unit
12576 2: DW_TAG_namespace // N
12577 3: DW_TAG_subprogram // declaration of N::foo
12578 4: DW_TAG_subprogram // definition of N::foo
12579 DW_AT_specification // refers to die #3
12580
12581 Thus, when processing die #4, we have to pretend that we're in
12582 the context of its DW_AT_specification, namely the contex of die
12583 #3. */
12584 spec_cu = cu;
12585 spec_die = die_specification (die, &spec_cu);
12586 if (spec_die == NULL)
12587 parent = die->parent;
12588 else
63d06c5c 12589 {
0114d602
DJ
12590 parent = spec_die->parent;
12591 cu = spec_cu;
63d06c5c 12592 }
0114d602
DJ
12593
12594 if (parent == NULL)
12595 return "";
98bfdba5
PA
12596 else if (parent->building_fullname)
12597 {
12598 const char *name;
12599 const char *parent_name;
12600
12601 /* It has been seen on RealView 2.2 built binaries,
12602 DW_TAG_template_type_param types actually _defined_ as
12603 children of the parent class:
12604
12605 enum E {};
12606 template class <class Enum> Class{};
12607 Class<enum E> class_e;
12608
12609 1: DW_TAG_class_type (Class)
12610 2: DW_TAG_enumeration_type (E)
12611 3: DW_TAG_enumerator (enum1:0)
12612 3: DW_TAG_enumerator (enum2:1)
12613 ...
12614 2: DW_TAG_template_type_param
12615 DW_AT_type DW_FORM_ref_udata (E)
12616
12617 Besides being broken debug info, it can put GDB into an
12618 infinite loop. Consider:
12619
12620 When we're building the full name for Class<E>, we'll start
12621 at Class, and go look over its template type parameters,
12622 finding E. We'll then try to build the full name of E, and
12623 reach here. We're now trying to build the full name of E,
12624 and look over the parent DIE for containing scope. In the
12625 broken case, if we followed the parent DIE of E, we'd again
12626 find Class, and once again go look at its template type
12627 arguments, etc., etc. Simply don't consider such parent die
12628 as source-level parent of this die (it can't be, the language
12629 doesn't allow it), and break the loop here. */
12630 name = dwarf2_name (die, cu);
12631 parent_name = dwarf2_name (parent, cu);
12632 complaint (&symfile_complaints,
12633 _("template param type '%s' defined within parent '%s'"),
12634 name ? name : "<unknown>",
12635 parent_name ? parent_name : "<unknown>");
12636 return "";
12637 }
63d06c5c 12638 else
0114d602
DJ
12639 switch (parent->tag)
12640 {
63d06c5c 12641 case DW_TAG_namespace:
0114d602 12642 parent_type = read_type_die (parent, cu);
acebe513
UW
12643 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12644 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12645 Work around this problem here. */
12646 if (cu->language == language_cplus
12647 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12648 return "";
0114d602
DJ
12649 /* We give a name to even anonymous namespaces. */
12650 return TYPE_TAG_NAME (parent_type);
63d06c5c 12651 case DW_TAG_class_type:
680b30c7 12652 case DW_TAG_interface_type:
63d06c5c 12653 case DW_TAG_structure_type:
0114d602 12654 case DW_TAG_union_type:
f55ee35c 12655 case DW_TAG_module:
0114d602
DJ
12656 parent_type = read_type_die (parent, cu);
12657 if (TYPE_TAG_NAME (parent_type) != NULL)
12658 return TYPE_TAG_NAME (parent_type);
12659 else
12660 /* An anonymous structure is only allowed non-static data
12661 members; no typedefs, no member functions, et cetera.
12662 So it does not need a prefix. */
12663 return "";
abc72ce4
DE
12664 case DW_TAG_compile_unit:
12665 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12666 if (cu->language == language_cplus
8b70b953 12667 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12668 && die->child != NULL
12669 && (die->tag == DW_TAG_class_type
12670 || die->tag == DW_TAG_structure_type
12671 || die->tag == DW_TAG_union_type))
12672 {
12673 char *name = guess_full_die_structure_name (die, cu);
12674 if (name != NULL)
12675 return name;
12676 }
12677 return "";
63d06c5c 12678 default:
8176b9b8 12679 return determine_prefix (parent, cu);
63d06c5c 12680 }
63d06c5c
DC
12681}
12682
3e43a32a
MS
12683/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12684 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12685 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12686 an obconcat, otherwise allocate storage for the result. The CU argument is
12687 used to determine the language and hence, the appropriate separator. */
987504bb 12688
f55ee35c 12689#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12690
12691static char *
f55ee35c
JK
12692typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12693 int physname, struct dwarf2_cu *cu)
63d06c5c 12694{
f55ee35c 12695 const char *lead = "";
5c315b68 12696 const char *sep;
63d06c5c 12697
3e43a32a
MS
12698 if (suffix == NULL || suffix[0] == '\0'
12699 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12700 sep = "";
12701 else if (cu->language == language_java)
12702 sep = ".";
f55ee35c
JK
12703 else if (cu->language == language_fortran && physname)
12704 {
12705 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12706 DW_AT_MIPS_linkage_name is preferred and used instead. */
12707
12708 lead = "__";
12709 sep = "_MOD_";
12710 }
987504bb
JJ
12711 else
12712 sep = "::";
63d06c5c 12713
6dd47d34
DE
12714 if (prefix == NULL)
12715 prefix = "";
12716 if (suffix == NULL)
12717 suffix = "";
12718
987504bb
JJ
12719 if (obs == NULL)
12720 {
3e43a32a
MS
12721 char *retval
12722 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12723
f55ee35c
JK
12724 strcpy (retval, lead);
12725 strcat (retval, prefix);
6dd47d34
DE
12726 strcat (retval, sep);
12727 strcat (retval, suffix);
63d06c5c
DC
12728 return retval;
12729 }
987504bb
JJ
12730 else
12731 {
12732 /* We have an obstack. */
f55ee35c 12733 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12734 }
63d06c5c
DC
12735}
12736
c906108c
SS
12737/* Return sibling of die, NULL if no sibling. */
12738
f9aca02d 12739static struct die_info *
fba45db2 12740sibling_die (struct die_info *die)
c906108c 12741{
639d11d3 12742 return die->sibling;
c906108c
SS
12743}
12744
71c25dea
TT
12745/* Get name of a die, return NULL if not found. */
12746
12747static char *
12748dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12749 struct obstack *obstack)
12750{
12751 if (name && cu->language == language_cplus)
12752 {
12753 char *canon_name = cp_canonicalize_string (name);
12754
12755 if (canon_name != NULL)
12756 {
12757 if (strcmp (canon_name, name) != 0)
12758 name = obsavestring (canon_name, strlen (canon_name),
12759 obstack);
12760 xfree (canon_name);
12761 }
12762 }
12763
12764 return name;
c906108c
SS
12765}
12766
9219021c
DC
12767/* Get name of a die, return NULL if not found. */
12768
12769static char *
e142c38c 12770dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12771{
12772 struct attribute *attr;
12773
e142c38c 12774 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12775 if ((!attr || !DW_STRING (attr))
12776 && die->tag != DW_TAG_class_type
12777 && die->tag != DW_TAG_interface_type
12778 && die->tag != DW_TAG_structure_type
12779 && die->tag != DW_TAG_union_type)
71c25dea
TT
12780 return NULL;
12781
12782 switch (die->tag)
12783 {
12784 case DW_TAG_compile_unit:
12785 /* Compilation units have a DW_AT_name that is a filename, not
12786 a source language identifier. */
12787 case DW_TAG_enumeration_type:
12788 case DW_TAG_enumerator:
12789 /* These tags always have simple identifiers already; no need
12790 to canonicalize them. */
12791 return DW_STRING (attr);
907af001 12792
418835cc
KS
12793 case DW_TAG_subprogram:
12794 /* Java constructors will all be named "<init>", so return
12795 the class name when we see this special case. */
12796 if (cu->language == language_java
12797 && DW_STRING (attr) != NULL
12798 && strcmp (DW_STRING (attr), "<init>") == 0)
12799 {
12800 struct dwarf2_cu *spec_cu = cu;
12801 struct die_info *spec_die;
12802
12803 /* GCJ will output '<init>' for Java constructor names.
12804 For this special case, return the name of the parent class. */
12805
12806 /* GCJ may output suprogram DIEs with AT_specification set.
12807 If so, use the name of the specified DIE. */
12808 spec_die = die_specification (die, &spec_cu);
12809 if (spec_die != NULL)
12810 return dwarf2_name (spec_die, spec_cu);
12811
12812 do
12813 {
12814 die = die->parent;
12815 if (die->tag == DW_TAG_class_type)
12816 return dwarf2_name (die, cu);
12817 }
12818 while (die->tag != DW_TAG_compile_unit);
12819 }
907af001
UW
12820 break;
12821
12822 case DW_TAG_class_type:
12823 case DW_TAG_interface_type:
12824 case DW_TAG_structure_type:
12825 case DW_TAG_union_type:
12826 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12827 structures or unions. These were of the form "._%d" in GCC 4.1,
12828 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12829 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12830 if (attr && DW_STRING (attr)
12831 && (strncmp (DW_STRING (attr), "._", 2) == 0
12832 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12833 return NULL;
53832f31
TT
12834
12835 /* GCC might emit a nameless typedef that has a linkage name. See
12836 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12837 if (!attr || DW_STRING (attr) == NULL)
12838 {
df5c6c50 12839 char *demangled = NULL;
53832f31
TT
12840
12841 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12842 if (attr == NULL)
12843 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12844
12845 if (attr == NULL || DW_STRING (attr) == NULL)
12846 return NULL;
12847
df5c6c50
JK
12848 /* Avoid demangling DW_STRING (attr) the second time on a second
12849 call for the same DIE. */
12850 if (!DW_STRING_IS_CANONICAL (attr))
12851 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12852
12853 if (demangled)
12854 {
96408a79
SA
12855 char *base;
12856
53832f31 12857 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12858 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12859 &cu->objfile->objfile_obstack);
53832f31
TT
12860 DW_STRING_IS_CANONICAL (attr) = 1;
12861 xfree (demangled);
96408a79
SA
12862
12863 /* Strip any leading namespaces/classes, keep only the base name.
12864 DW_AT_name for named DIEs does not contain the prefixes. */
12865 base = strrchr (DW_STRING (attr), ':');
12866 if (base && base > DW_STRING (attr) && base[-1] == ':')
12867 return &base[1];
12868 else
12869 return DW_STRING (attr);
53832f31
TT
12870 }
12871 }
907af001
UW
12872 break;
12873
71c25dea 12874 default:
907af001
UW
12875 break;
12876 }
12877
12878 if (!DW_STRING_IS_CANONICAL (attr))
12879 {
12880 DW_STRING (attr)
12881 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12882 &cu->objfile->objfile_obstack);
12883 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12884 }
907af001 12885 return DW_STRING (attr);
9219021c
DC
12886}
12887
12888/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12889 is none. *EXT_CU is the CU containing DIE on input, and the CU
12890 containing the return value on output. */
9219021c
DC
12891
12892static struct die_info *
f2f0e013 12893dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12894{
12895 struct attribute *attr;
9219021c 12896
f2f0e013 12897 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12898 if (attr == NULL)
12899 return NULL;
12900
f2f0e013 12901 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12902}
12903
c906108c
SS
12904/* Convert a DIE tag into its string name. */
12905
12906static char *
aa1ee363 12907dwarf_tag_name (unsigned tag)
c906108c
SS
12908{
12909 switch (tag)
12910 {
12911 case DW_TAG_padding:
12912 return "DW_TAG_padding";
12913 case DW_TAG_array_type:
12914 return "DW_TAG_array_type";
12915 case DW_TAG_class_type:
12916 return "DW_TAG_class_type";
12917 case DW_TAG_entry_point:
12918 return "DW_TAG_entry_point";
12919 case DW_TAG_enumeration_type:
12920 return "DW_TAG_enumeration_type";
12921 case DW_TAG_formal_parameter:
12922 return "DW_TAG_formal_parameter";
12923 case DW_TAG_imported_declaration:
12924 return "DW_TAG_imported_declaration";
12925 case DW_TAG_label:
12926 return "DW_TAG_label";
12927 case DW_TAG_lexical_block:
12928 return "DW_TAG_lexical_block";
12929 case DW_TAG_member:
12930 return "DW_TAG_member";
12931 case DW_TAG_pointer_type:
12932 return "DW_TAG_pointer_type";
12933 case DW_TAG_reference_type:
12934 return "DW_TAG_reference_type";
12935 case DW_TAG_compile_unit:
12936 return "DW_TAG_compile_unit";
12937 case DW_TAG_string_type:
12938 return "DW_TAG_string_type";
12939 case DW_TAG_structure_type:
12940 return "DW_TAG_structure_type";
12941 case DW_TAG_subroutine_type:
12942 return "DW_TAG_subroutine_type";
12943 case DW_TAG_typedef:
12944 return "DW_TAG_typedef";
12945 case DW_TAG_union_type:
12946 return "DW_TAG_union_type";
12947 case DW_TAG_unspecified_parameters:
12948 return "DW_TAG_unspecified_parameters";
12949 case DW_TAG_variant:
12950 return "DW_TAG_variant";
12951 case DW_TAG_common_block:
12952 return "DW_TAG_common_block";
12953 case DW_TAG_common_inclusion:
12954 return "DW_TAG_common_inclusion";
12955 case DW_TAG_inheritance:
12956 return "DW_TAG_inheritance";
12957 case DW_TAG_inlined_subroutine:
12958 return "DW_TAG_inlined_subroutine";
12959 case DW_TAG_module:
12960 return "DW_TAG_module";
12961 case DW_TAG_ptr_to_member_type:
12962 return "DW_TAG_ptr_to_member_type";
12963 case DW_TAG_set_type:
12964 return "DW_TAG_set_type";
12965 case DW_TAG_subrange_type:
12966 return "DW_TAG_subrange_type";
12967 case DW_TAG_with_stmt:
12968 return "DW_TAG_with_stmt";
12969 case DW_TAG_access_declaration:
12970 return "DW_TAG_access_declaration";
12971 case DW_TAG_base_type:
12972 return "DW_TAG_base_type";
12973 case DW_TAG_catch_block:
12974 return "DW_TAG_catch_block";
12975 case DW_TAG_const_type:
12976 return "DW_TAG_const_type";
12977 case DW_TAG_constant:
12978 return "DW_TAG_constant";
12979 case DW_TAG_enumerator:
12980 return "DW_TAG_enumerator";
12981 case DW_TAG_file_type:
12982 return "DW_TAG_file_type";
12983 case DW_TAG_friend:
12984 return "DW_TAG_friend";
12985 case DW_TAG_namelist:
12986 return "DW_TAG_namelist";
12987 case DW_TAG_namelist_item:
12988 return "DW_TAG_namelist_item";
12989 case DW_TAG_packed_type:
12990 return "DW_TAG_packed_type";
12991 case DW_TAG_subprogram:
12992 return "DW_TAG_subprogram";
12993 case DW_TAG_template_type_param:
12994 return "DW_TAG_template_type_param";
12995 case DW_TAG_template_value_param:
12996 return "DW_TAG_template_value_param";
12997 case DW_TAG_thrown_type:
12998 return "DW_TAG_thrown_type";
12999 case DW_TAG_try_block:
13000 return "DW_TAG_try_block";
13001 case DW_TAG_variant_part:
13002 return "DW_TAG_variant_part";
13003 case DW_TAG_variable:
13004 return "DW_TAG_variable";
13005 case DW_TAG_volatile_type:
13006 return "DW_TAG_volatile_type";
d9fa45fe
DC
13007 case DW_TAG_dwarf_procedure:
13008 return "DW_TAG_dwarf_procedure";
13009 case DW_TAG_restrict_type:
13010 return "DW_TAG_restrict_type";
13011 case DW_TAG_interface_type:
13012 return "DW_TAG_interface_type";
13013 case DW_TAG_namespace:
13014 return "DW_TAG_namespace";
13015 case DW_TAG_imported_module:
13016 return "DW_TAG_imported_module";
13017 case DW_TAG_unspecified_type:
13018 return "DW_TAG_unspecified_type";
13019 case DW_TAG_partial_unit:
13020 return "DW_TAG_partial_unit";
13021 case DW_TAG_imported_unit:
13022 return "DW_TAG_imported_unit";
b7619582
GF
13023 case DW_TAG_condition:
13024 return "DW_TAG_condition";
13025 case DW_TAG_shared_type:
13026 return "DW_TAG_shared_type";
348e048f
DE
13027 case DW_TAG_type_unit:
13028 return "DW_TAG_type_unit";
c906108c
SS
13029 case DW_TAG_MIPS_loop:
13030 return "DW_TAG_MIPS_loop";
b7619582
GF
13031 case DW_TAG_HP_array_descriptor:
13032 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13033 case DW_TAG_format_label:
13034 return "DW_TAG_format_label";
13035 case DW_TAG_function_template:
13036 return "DW_TAG_function_template";
13037 case DW_TAG_class_template:
13038 return "DW_TAG_class_template";
b7619582
GF
13039 case DW_TAG_GNU_BINCL:
13040 return "DW_TAG_GNU_BINCL";
13041 case DW_TAG_GNU_EINCL:
13042 return "DW_TAG_GNU_EINCL";
13043 case DW_TAG_upc_shared_type:
13044 return "DW_TAG_upc_shared_type";
13045 case DW_TAG_upc_strict_type:
13046 return "DW_TAG_upc_strict_type";
13047 case DW_TAG_upc_relaxed_type:
13048 return "DW_TAG_upc_relaxed_type";
13049 case DW_TAG_PGI_kanji_type:
13050 return "DW_TAG_PGI_kanji_type";
13051 case DW_TAG_PGI_interface_block:
13052 return "DW_TAG_PGI_interface_block";
96408a79
SA
13053 case DW_TAG_GNU_call_site:
13054 return "DW_TAG_GNU_call_site";
c906108c
SS
13055 default:
13056 return "DW_TAG_<unknown>";
13057 }
13058}
13059
13060/* Convert a DWARF attribute code into its string name. */
13061
13062static char *
aa1ee363 13063dwarf_attr_name (unsigned attr)
c906108c
SS
13064{
13065 switch (attr)
13066 {
13067 case DW_AT_sibling:
13068 return "DW_AT_sibling";
13069 case DW_AT_location:
13070 return "DW_AT_location";
13071 case DW_AT_name:
13072 return "DW_AT_name";
13073 case DW_AT_ordering:
13074 return "DW_AT_ordering";
13075 case DW_AT_subscr_data:
13076 return "DW_AT_subscr_data";
13077 case DW_AT_byte_size:
13078 return "DW_AT_byte_size";
13079 case DW_AT_bit_offset:
13080 return "DW_AT_bit_offset";
13081 case DW_AT_bit_size:
13082 return "DW_AT_bit_size";
13083 case DW_AT_element_list:
13084 return "DW_AT_element_list";
13085 case DW_AT_stmt_list:
13086 return "DW_AT_stmt_list";
13087 case DW_AT_low_pc:
13088 return "DW_AT_low_pc";
13089 case DW_AT_high_pc:
13090 return "DW_AT_high_pc";
13091 case DW_AT_language:
13092 return "DW_AT_language";
13093 case DW_AT_member:
13094 return "DW_AT_member";
13095 case DW_AT_discr:
13096 return "DW_AT_discr";
13097 case DW_AT_discr_value:
13098 return "DW_AT_discr_value";
13099 case DW_AT_visibility:
13100 return "DW_AT_visibility";
13101 case DW_AT_import:
13102 return "DW_AT_import";
13103 case DW_AT_string_length:
13104 return "DW_AT_string_length";
13105 case DW_AT_common_reference:
13106 return "DW_AT_common_reference";
13107 case DW_AT_comp_dir:
13108 return "DW_AT_comp_dir";
13109 case DW_AT_const_value:
13110 return "DW_AT_const_value";
13111 case DW_AT_containing_type:
13112 return "DW_AT_containing_type";
13113 case DW_AT_default_value:
13114 return "DW_AT_default_value";
13115 case DW_AT_inline:
13116 return "DW_AT_inline";
13117 case DW_AT_is_optional:
13118 return "DW_AT_is_optional";
13119 case DW_AT_lower_bound:
13120 return "DW_AT_lower_bound";
13121 case DW_AT_producer:
13122 return "DW_AT_producer";
13123 case DW_AT_prototyped:
13124 return "DW_AT_prototyped";
13125 case DW_AT_return_addr:
13126 return "DW_AT_return_addr";
13127 case DW_AT_start_scope:
13128 return "DW_AT_start_scope";
09fa0d7c
JK
13129 case DW_AT_bit_stride:
13130 return "DW_AT_bit_stride";
c906108c
SS
13131 case DW_AT_upper_bound:
13132 return "DW_AT_upper_bound";
13133 case DW_AT_abstract_origin:
13134 return "DW_AT_abstract_origin";
13135 case DW_AT_accessibility:
13136 return "DW_AT_accessibility";
13137 case DW_AT_address_class:
13138 return "DW_AT_address_class";
13139 case DW_AT_artificial:
13140 return "DW_AT_artificial";
13141 case DW_AT_base_types:
13142 return "DW_AT_base_types";
13143 case DW_AT_calling_convention:
13144 return "DW_AT_calling_convention";
13145 case DW_AT_count:
13146 return "DW_AT_count";
13147 case DW_AT_data_member_location:
13148 return "DW_AT_data_member_location";
13149 case DW_AT_decl_column:
13150 return "DW_AT_decl_column";
13151 case DW_AT_decl_file:
13152 return "DW_AT_decl_file";
13153 case DW_AT_decl_line:
13154 return "DW_AT_decl_line";
13155 case DW_AT_declaration:
13156 return "DW_AT_declaration";
13157 case DW_AT_discr_list:
13158 return "DW_AT_discr_list";
13159 case DW_AT_encoding:
13160 return "DW_AT_encoding";
13161 case DW_AT_external:
13162 return "DW_AT_external";
13163 case DW_AT_frame_base:
13164 return "DW_AT_frame_base";
13165 case DW_AT_friend:
13166 return "DW_AT_friend";
13167 case DW_AT_identifier_case:
13168 return "DW_AT_identifier_case";
13169 case DW_AT_macro_info:
13170 return "DW_AT_macro_info";
13171 case DW_AT_namelist_items:
13172 return "DW_AT_namelist_items";
13173 case DW_AT_priority:
13174 return "DW_AT_priority";
13175 case DW_AT_segment:
13176 return "DW_AT_segment";
13177 case DW_AT_specification:
13178 return "DW_AT_specification";
13179 case DW_AT_static_link:
13180 return "DW_AT_static_link";
13181 case DW_AT_type:
13182 return "DW_AT_type";
13183 case DW_AT_use_location:
13184 return "DW_AT_use_location";
13185 case DW_AT_variable_parameter:
13186 return "DW_AT_variable_parameter";
13187 case DW_AT_virtuality:
13188 return "DW_AT_virtuality";
13189 case DW_AT_vtable_elem_location:
13190 return "DW_AT_vtable_elem_location";
b7619582 13191 /* DWARF 3 values. */
d9fa45fe
DC
13192 case DW_AT_allocated:
13193 return "DW_AT_allocated";
13194 case DW_AT_associated:
13195 return "DW_AT_associated";
13196 case DW_AT_data_location:
13197 return "DW_AT_data_location";
09fa0d7c
JK
13198 case DW_AT_byte_stride:
13199 return "DW_AT_byte_stride";
d9fa45fe
DC
13200 case DW_AT_entry_pc:
13201 return "DW_AT_entry_pc";
13202 case DW_AT_use_UTF8:
13203 return "DW_AT_use_UTF8";
13204 case DW_AT_extension:
13205 return "DW_AT_extension";
13206 case DW_AT_ranges:
13207 return "DW_AT_ranges";
13208 case DW_AT_trampoline:
13209 return "DW_AT_trampoline";
13210 case DW_AT_call_column:
13211 return "DW_AT_call_column";
13212 case DW_AT_call_file:
13213 return "DW_AT_call_file";
13214 case DW_AT_call_line:
13215 return "DW_AT_call_line";
b7619582
GF
13216 case DW_AT_description:
13217 return "DW_AT_description";
13218 case DW_AT_binary_scale:
13219 return "DW_AT_binary_scale";
13220 case DW_AT_decimal_scale:
13221 return "DW_AT_decimal_scale";
13222 case DW_AT_small:
13223 return "DW_AT_small";
13224 case DW_AT_decimal_sign:
13225 return "DW_AT_decimal_sign";
13226 case DW_AT_digit_count:
13227 return "DW_AT_digit_count";
13228 case DW_AT_picture_string:
13229 return "DW_AT_picture_string";
13230 case DW_AT_mutable:
13231 return "DW_AT_mutable";
13232 case DW_AT_threads_scaled:
13233 return "DW_AT_threads_scaled";
13234 case DW_AT_explicit:
13235 return "DW_AT_explicit";
13236 case DW_AT_object_pointer:
13237 return "DW_AT_object_pointer";
13238 case DW_AT_endianity:
13239 return "DW_AT_endianity";
13240 case DW_AT_elemental:
13241 return "DW_AT_elemental";
13242 case DW_AT_pure:
13243 return "DW_AT_pure";
13244 case DW_AT_recursive:
13245 return "DW_AT_recursive";
348e048f
DE
13246 /* DWARF 4 values. */
13247 case DW_AT_signature:
13248 return "DW_AT_signature";
31ef98ae
TT
13249 case DW_AT_linkage_name:
13250 return "DW_AT_linkage_name";
b7619582 13251 /* SGI/MIPS extensions. */
c764a876 13252#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13253 case DW_AT_MIPS_fde:
13254 return "DW_AT_MIPS_fde";
c764a876 13255#endif
c906108c
SS
13256 case DW_AT_MIPS_loop_begin:
13257 return "DW_AT_MIPS_loop_begin";
13258 case DW_AT_MIPS_tail_loop_begin:
13259 return "DW_AT_MIPS_tail_loop_begin";
13260 case DW_AT_MIPS_epilog_begin:
13261 return "DW_AT_MIPS_epilog_begin";
13262 case DW_AT_MIPS_loop_unroll_factor:
13263 return "DW_AT_MIPS_loop_unroll_factor";
13264 case DW_AT_MIPS_software_pipeline_depth:
13265 return "DW_AT_MIPS_software_pipeline_depth";
13266 case DW_AT_MIPS_linkage_name:
13267 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13268 case DW_AT_MIPS_stride:
13269 return "DW_AT_MIPS_stride";
13270 case DW_AT_MIPS_abstract_name:
13271 return "DW_AT_MIPS_abstract_name";
13272 case DW_AT_MIPS_clone_origin:
13273 return "DW_AT_MIPS_clone_origin";
13274 case DW_AT_MIPS_has_inlines:
13275 return "DW_AT_MIPS_has_inlines";
b7619582 13276 /* HP extensions. */
c764a876 13277#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13278 case DW_AT_HP_block_index:
13279 return "DW_AT_HP_block_index";
c764a876 13280#endif
b7619582
GF
13281 case DW_AT_HP_unmodifiable:
13282 return "DW_AT_HP_unmodifiable";
13283 case DW_AT_HP_actuals_stmt_list:
13284 return "DW_AT_HP_actuals_stmt_list";
13285 case DW_AT_HP_proc_per_section:
13286 return "DW_AT_HP_proc_per_section";
13287 case DW_AT_HP_raw_data_ptr:
13288 return "DW_AT_HP_raw_data_ptr";
13289 case DW_AT_HP_pass_by_reference:
13290 return "DW_AT_HP_pass_by_reference";
13291 case DW_AT_HP_opt_level:
13292 return "DW_AT_HP_opt_level";
13293 case DW_AT_HP_prof_version_id:
13294 return "DW_AT_HP_prof_version_id";
13295 case DW_AT_HP_opt_flags:
13296 return "DW_AT_HP_opt_flags";
13297 case DW_AT_HP_cold_region_low_pc:
13298 return "DW_AT_HP_cold_region_low_pc";
13299 case DW_AT_HP_cold_region_high_pc:
13300 return "DW_AT_HP_cold_region_high_pc";
13301 case DW_AT_HP_all_variables_modifiable:
13302 return "DW_AT_HP_all_variables_modifiable";
13303 case DW_AT_HP_linkage_name:
13304 return "DW_AT_HP_linkage_name";
13305 case DW_AT_HP_prof_flags:
13306 return "DW_AT_HP_prof_flags";
13307 /* GNU extensions. */
c906108c
SS
13308 case DW_AT_sf_names:
13309 return "DW_AT_sf_names";
13310 case DW_AT_src_info:
13311 return "DW_AT_src_info";
13312 case DW_AT_mac_info:
13313 return "DW_AT_mac_info";
13314 case DW_AT_src_coords:
13315 return "DW_AT_src_coords";
13316 case DW_AT_body_begin:
13317 return "DW_AT_body_begin";
13318 case DW_AT_body_end:
13319 return "DW_AT_body_end";
f5f8a009
EZ
13320 case DW_AT_GNU_vector:
13321 return "DW_AT_GNU_vector";
2de00c64
DE
13322 case DW_AT_GNU_odr_signature:
13323 return "DW_AT_GNU_odr_signature";
b7619582
GF
13324 /* VMS extensions. */
13325 case DW_AT_VMS_rtnbeg_pd_address:
13326 return "DW_AT_VMS_rtnbeg_pd_address";
13327 /* UPC extension. */
13328 case DW_AT_upc_threads_scaled:
13329 return "DW_AT_upc_threads_scaled";
13330 /* PGI (STMicroelectronics) extensions. */
13331 case DW_AT_PGI_lbase:
13332 return "DW_AT_PGI_lbase";
13333 case DW_AT_PGI_soffset:
13334 return "DW_AT_PGI_soffset";
13335 case DW_AT_PGI_lstride:
13336 return "DW_AT_PGI_lstride";
c906108c
SS
13337 default:
13338 return "DW_AT_<unknown>";
13339 }
13340}
13341
13342/* Convert a DWARF value form code into its string name. */
13343
13344static char *
aa1ee363 13345dwarf_form_name (unsigned form)
c906108c
SS
13346{
13347 switch (form)
13348 {
13349 case DW_FORM_addr:
13350 return "DW_FORM_addr";
13351 case DW_FORM_block2:
13352 return "DW_FORM_block2";
13353 case DW_FORM_block4:
13354 return "DW_FORM_block4";
13355 case DW_FORM_data2:
13356 return "DW_FORM_data2";
13357 case DW_FORM_data4:
13358 return "DW_FORM_data4";
13359 case DW_FORM_data8:
13360 return "DW_FORM_data8";
13361 case DW_FORM_string:
13362 return "DW_FORM_string";
13363 case DW_FORM_block:
13364 return "DW_FORM_block";
13365 case DW_FORM_block1:
13366 return "DW_FORM_block1";
13367 case DW_FORM_data1:
13368 return "DW_FORM_data1";
13369 case DW_FORM_flag:
13370 return "DW_FORM_flag";
13371 case DW_FORM_sdata:
13372 return "DW_FORM_sdata";
13373 case DW_FORM_strp:
13374 return "DW_FORM_strp";
13375 case DW_FORM_udata:
13376 return "DW_FORM_udata";
13377 case DW_FORM_ref_addr:
13378 return "DW_FORM_ref_addr";
13379 case DW_FORM_ref1:
13380 return "DW_FORM_ref1";
13381 case DW_FORM_ref2:
13382 return "DW_FORM_ref2";
13383 case DW_FORM_ref4:
13384 return "DW_FORM_ref4";
13385 case DW_FORM_ref8:
13386 return "DW_FORM_ref8";
13387 case DW_FORM_ref_udata:
13388 return "DW_FORM_ref_udata";
13389 case DW_FORM_indirect:
13390 return "DW_FORM_indirect";
348e048f
DE
13391 case DW_FORM_sec_offset:
13392 return "DW_FORM_sec_offset";
13393 case DW_FORM_exprloc:
13394 return "DW_FORM_exprloc";
13395 case DW_FORM_flag_present:
13396 return "DW_FORM_flag_present";
55f1336d
TT
13397 case DW_FORM_ref_sig8:
13398 return "DW_FORM_ref_sig8";
c906108c
SS
13399 default:
13400 return "DW_FORM_<unknown>";
13401 }
13402}
13403
13404/* Convert a DWARF stack opcode into its string name. */
13405
9eae7c52 13406const char *
b1bfef65 13407dwarf_stack_op_name (unsigned op)
c906108c
SS
13408{
13409 switch (op)
13410 {
13411 case DW_OP_addr:
13412 return "DW_OP_addr";
13413 case DW_OP_deref:
13414 return "DW_OP_deref";
13415 case DW_OP_const1u:
13416 return "DW_OP_const1u";
13417 case DW_OP_const1s:
13418 return "DW_OP_const1s";
13419 case DW_OP_const2u:
13420 return "DW_OP_const2u";
13421 case DW_OP_const2s:
13422 return "DW_OP_const2s";
13423 case DW_OP_const4u:
13424 return "DW_OP_const4u";
13425 case DW_OP_const4s:
13426 return "DW_OP_const4s";
13427 case DW_OP_const8u:
13428 return "DW_OP_const8u";
13429 case DW_OP_const8s:
13430 return "DW_OP_const8s";
13431 case DW_OP_constu:
13432 return "DW_OP_constu";
13433 case DW_OP_consts:
13434 return "DW_OP_consts";
13435 case DW_OP_dup:
13436 return "DW_OP_dup";
13437 case DW_OP_drop:
13438 return "DW_OP_drop";
13439 case DW_OP_over:
13440 return "DW_OP_over";
13441 case DW_OP_pick:
13442 return "DW_OP_pick";
13443 case DW_OP_swap:
13444 return "DW_OP_swap";
13445 case DW_OP_rot:
13446 return "DW_OP_rot";
13447 case DW_OP_xderef:
13448 return "DW_OP_xderef";
13449 case DW_OP_abs:
13450 return "DW_OP_abs";
13451 case DW_OP_and:
13452 return "DW_OP_and";
13453 case DW_OP_div:
13454 return "DW_OP_div";
13455 case DW_OP_minus:
13456 return "DW_OP_minus";
13457 case DW_OP_mod:
13458 return "DW_OP_mod";
13459 case DW_OP_mul:
13460 return "DW_OP_mul";
13461 case DW_OP_neg:
13462 return "DW_OP_neg";
13463 case DW_OP_not:
13464 return "DW_OP_not";
13465 case DW_OP_or:
13466 return "DW_OP_or";
13467 case DW_OP_plus:
13468 return "DW_OP_plus";
13469 case DW_OP_plus_uconst:
13470 return "DW_OP_plus_uconst";
13471 case DW_OP_shl:
13472 return "DW_OP_shl";
13473 case DW_OP_shr:
13474 return "DW_OP_shr";
13475 case DW_OP_shra:
13476 return "DW_OP_shra";
13477 case DW_OP_xor:
13478 return "DW_OP_xor";
13479 case DW_OP_bra:
13480 return "DW_OP_bra";
13481 case DW_OP_eq:
13482 return "DW_OP_eq";
13483 case DW_OP_ge:
13484 return "DW_OP_ge";
13485 case DW_OP_gt:
13486 return "DW_OP_gt";
13487 case DW_OP_le:
13488 return "DW_OP_le";
13489 case DW_OP_lt:
13490 return "DW_OP_lt";
13491 case DW_OP_ne:
13492 return "DW_OP_ne";
13493 case DW_OP_skip:
13494 return "DW_OP_skip";
13495 case DW_OP_lit0:
13496 return "DW_OP_lit0";
13497 case DW_OP_lit1:
13498 return "DW_OP_lit1";
13499 case DW_OP_lit2:
13500 return "DW_OP_lit2";
13501 case DW_OP_lit3:
13502 return "DW_OP_lit3";
13503 case DW_OP_lit4:
13504 return "DW_OP_lit4";
13505 case DW_OP_lit5:
13506 return "DW_OP_lit5";
13507 case DW_OP_lit6:
13508 return "DW_OP_lit6";
13509 case DW_OP_lit7:
13510 return "DW_OP_lit7";
13511 case DW_OP_lit8:
13512 return "DW_OP_lit8";
13513 case DW_OP_lit9:
13514 return "DW_OP_lit9";
13515 case DW_OP_lit10:
13516 return "DW_OP_lit10";
13517 case DW_OP_lit11:
13518 return "DW_OP_lit11";
13519 case DW_OP_lit12:
13520 return "DW_OP_lit12";
13521 case DW_OP_lit13:
13522 return "DW_OP_lit13";
13523 case DW_OP_lit14:
13524 return "DW_OP_lit14";
13525 case DW_OP_lit15:
13526 return "DW_OP_lit15";
13527 case DW_OP_lit16:
13528 return "DW_OP_lit16";
13529 case DW_OP_lit17:
13530 return "DW_OP_lit17";
13531 case DW_OP_lit18:
13532 return "DW_OP_lit18";
13533 case DW_OP_lit19:
13534 return "DW_OP_lit19";
13535 case DW_OP_lit20:
13536 return "DW_OP_lit20";
13537 case DW_OP_lit21:
13538 return "DW_OP_lit21";
13539 case DW_OP_lit22:
13540 return "DW_OP_lit22";
13541 case DW_OP_lit23:
13542 return "DW_OP_lit23";
13543 case DW_OP_lit24:
13544 return "DW_OP_lit24";
13545 case DW_OP_lit25:
13546 return "DW_OP_lit25";
13547 case DW_OP_lit26:
13548 return "DW_OP_lit26";
13549 case DW_OP_lit27:
13550 return "DW_OP_lit27";
13551 case DW_OP_lit28:
13552 return "DW_OP_lit28";
13553 case DW_OP_lit29:
13554 return "DW_OP_lit29";
13555 case DW_OP_lit30:
13556 return "DW_OP_lit30";
13557 case DW_OP_lit31:
13558 return "DW_OP_lit31";
13559 case DW_OP_reg0:
13560 return "DW_OP_reg0";
13561 case DW_OP_reg1:
13562 return "DW_OP_reg1";
13563 case DW_OP_reg2:
13564 return "DW_OP_reg2";
13565 case DW_OP_reg3:
13566 return "DW_OP_reg3";
13567 case DW_OP_reg4:
13568 return "DW_OP_reg4";
13569 case DW_OP_reg5:
13570 return "DW_OP_reg5";
13571 case DW_OP_reg6:
13572 return "DW_OP_reg6";
13573 case DW_OP_reg7:
13574 return "DW_OP_reg7";
13575 case DW_OP_reg8:
13576 return "DW_OP_reg8";
13577 case DW_OP_reg9:
13578 return "DW_OP_reg9";
13579 case DW_OP_reg10:
13580 return "DW_OP_reg10";
13581 case DW_OP_reg11:
13582 return "DW_OP_reg11";
13583 case DW_OP_reg12:
13584 return "DW_OP_reg12";
13585 case DW_OP_reg13:
13586 return "DW_OP_reg13";
13587 case DW_OP_reg14:
13588 return "DW_OP_reg14";
13589 case DW_OP_reg15:
13590 return "DW_OP_reg15";
13591 case DW_OP_reg16:
13592 return "DW_OP_reg16";
13593 case DW_OP_reg17:
13594 return "DW_OP_reg17";
13595 case DW_OP_reg18:
13596 return "DW_OP_reg18";
13597 case DW_OP_reg19:
13598 return "DW_OP_reg19";
13599 case DW_OP_reg20:
13600 return "DW_OP_reg20";
13601 case DW_OP_reg21:
13602 return "DW_OP_reg21";
13603 case DW_OP_reg22:
13604 return "DW_OP_reg22";
13605 case DW_OP_reg23:
13606 return "DW_OP_reg23";
13607 case DW_OP_reg24:
13608 return "DW_OP_reg24";
13609 case DW_OP_reg25:
13610 return "DW_OP_reg25";
13611 case DW_OP_reg26:
13612 return "DW_OP_reg26";
13613 case DW_OP_reg27:
13614 return "DW_OP_reg27";
13615 case DW_OP_reg28:
13616 return "DW_OP_reg28";
13617 case DW_OP_reg29:
13618 return "DW_OP_reg29";
13619 case DW_OP_reg30:
13620 return "DW_OP_reg30";
13621 case DW_OP_reg31:
13622 return "DW_OP_reg31";
13623 case DW_OP_breg0:
13624 return "DW_OP_breg0";
13625 case DW_OP_breg1:
13626 return "DW_OP_breg1";
13627 case DW_OP_breg2:
13628 return "DW_OP_breg2";
13629 case DW_OP_breg3:
13630 return "DW_OP_breg3";
13631 case DW_OP_breg4:
13632 return "DW_OP_breg4";
13633 case DW_OP_breg5:
13634 return "DW_OP_breg5";
13635 case DW_OP_breg6:
13636 return "DW_OP_breg6";
13637 case DW_OP_breg7:
13638 return "DW_OP_breg7";
13639 case DW_OP_breg8:
13640 return "DW_OP_breg8";
13641 case DW_OP_breg9:
13642 return "DW_OP_breg9";
13643 case DW_OP_breg10:
13644 return "DW_OP_breg10";
13645 case DW_OP_breg11:
13646 return "DW_OP_breg11";
13647 case DW_OP_breg12:
13648 return "DW_OP_breg12";
13649 case DW_OP_breg13:
13650 return "DW_OP_breg13";
13651 case DW_OP_breg14:
13652 return "DW_OP_breg14";
13653 case DW_OP_breg15:
13654 return "DW_OP_breg15";
13655 case DW_OP_breg16:
13656 return "DW_OP_breg16";
13657 case DW_OP_breg17:
13658 return "DW_OP_breg17";
13659 case DW_OP_breg18:
13660 return "DW_OP_breg18";
13661 case DW_OP_breg19:
13662 return "DW_OP_breg19";
13663 case DW_OP_breg20:
13664 return "DW_OP_breg20";
13665 case DW_OP_breg21:
13666 return "DW_OP_breg21";
13667 case DW_OP_breg22:
13668 return "DW_OP_breg22";
13669 case DW_OP_breg23:
13670 return "DW_OP_breg23";
13671 case DW_OP_breg24:
13672 return "DW_OP_breg24";
13673 case DW_OP_breg25:
13674 return "DW_OP_breg25";
13675 case DW_OP_breg26:
13676 return "DW_OP_breg26";
13677 case DW_OP_breg27:
13678 return "DW_OP_breg27";
13679 case DW_OP_breg28:
13680 return "DW_OP_breg28";
13681 case DW_OP_breg29:
13682 return "DW_OP_breg29";
13683 case DW_OP_breg30:
13684 return "DW_OP_breg30";
13685 case DW_OP_breg31:
13686 return "DW_OP_breg31";
13687 case DW_OP_regx:
13688 return "DW_OP_regx";
13689 case DW_OP_fbreg:
13690 return "DW_OP_fbreg";
13691 case DW_OP_bregx:
13692 return "DW_OP_bregx";
13693 case DW_OP_piece:
13694 return "DW_OP_piece";
13695 case DW_OP_deref_size:
13696 return "DW_OP_deref_size";
13697 case DW_OP_xderef_size:
13698 return "DW_OP_xderef_size";
13699 case DW_OP_nop:
13700 return "DW_OP_nop";
b7619582 13701 /* DWARF 3 extensions. */
ed348acc
EZ
13702 case DW_OP_push_object_address:
13703 return "DW_OP_push_object_address";
13704 case DW_OP_call2:
13705 return "DW_OP_call2";
13706 case DW_OP_call4:
13707 return "DW_OP_call4";
13708 case DW_OP_call_ref:
13709 return "DW_OP_call_ref";
b7619582
GF
13710 case DW_OP_form_tls_address:
13711 return "DW_OP_form_tls_address";
13712 case DW_OP_call_frame_cfa:
13713 return "DW_OP_call_frame_cfa";
13714 case DW_OP_bit_piece:
13715 return "DW_OP_bit_piece";
9eae7c52
TT
13716 /* DWARF 4 extensions. */
13717 case DW_OP_implicit_value:
13718 return "DW_OP_implicit_value";
13719 case DW_OP_stack_value:
13720 return "DW_OP_stack_value";
13721 /* GNU extensions. */
ed348acc
EZ
13722 case DW_OP_GNU_push_tls_address:
13723 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13724 case DW_OP_GNU_uninit:
13725 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13726 case DW_OP_GNU_implicit_pointer:
13727 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13728 case DW_OP_GNU_entry_value:
13729 return "DW_OP_GNU_entry_value";
13730 case DW_OP_GNU_const_type:
13731 return "DW_OP_GNU_const_type";
13732 case DW_OP_GNU_regval_type:
13733 return "DW_OP_GNU_regval_type";
13734 case DW_OP_GNU_deref_type:
13735 return "DW_OP_GNU_deref_type";
13736 case DW_OP_GNU_convert:
13737 return "DW_OP_GNU_convert";
13738 case DW_OP_GNU_reinterpret:
13739 return "DW_OP_GNU_reinterpret";
c906108c 13740 default:
b1bfef65 13741 return NULL;
c906108c
SS
13742 }
13743}
13744
13745static char *
fba45db2 13746dwarf_bool_name (unsigned mybool)
c906108c
SS
13747{
13748 if (mybool)
13749 return "TRUE";
13750 else
13751 return "FALSE";
13752}
13753
13754/* Convert a DWARF type code into its string name. */
13755
13756static char *
aa1ee363 13757dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13758{
13759 switch (enc)
13760 {
b7619582
GF
13761 case DW_ATE_void:
13762 return "DW_ATE_void";
c906108c
SS
13763 case DW_ATE_address:
13764 return "DW_ATE_address";
13765 case DW_ATE_boolean:
13766 return "DW_ATE_boolean";
13767 case DW_ATE_complex_float:
13768 return "DW_ATE_complex_float";
13769 case DW_ATE_float:
13770 return "DW_ATE_float";
13771 case DW_ATE_signed:
13772 return "DW_ATE_signed";
13773 case DW_ATE_signed_char:
13774 return "DW_ATE_signed_char";
13775 case DW_ATE_unsigned:
13776 return "DW_ATE_unsigned";
13777 case DW_ATE_unsigned_char:
13778 return "DW_ATE_unsigned_char";
b7619582 13779 /* DWARF 3. */
d9fa45fe
DC
13780 case DW_ATE_imaginary_float:
13781 return "DW_ATE_imaginary_float";
b7619582
GF
13782 case DW_ATE_packed_decimal:
13783 return "DW_ATE_packed_decimal";
13784 case DW_ATE_numeric_string:
13785 return "DW_ATE_numeric_string";
13786 case DW_ATE_edited:
13787 return "DW_ATE_edited";
13788 case DW_ATE_signed_fixed:
13789 return "DW_ATE_signed_fixed";
13790 case DW_ATE_unsigned_fixed:
13791 return "DW_ATE_unsigned_fixed";
13792 case DW_ATE_decimal_float:
13793 return "DW_ATE_decimal_float";
75079b2b
TT
13794 /* DWARF 4. */
13795 case DW_ATE_UTF:
13796 return "DW_ATE_UTF";
b7619582
GF
13797 /* HP extensions. */
13798 case DW_ATE_HP_float80:
13799 return "DW_ATE_HP_float80";
13800 case DW_ATE_HP_complex_float80:
13801 return "DW_ATE_HP_complex_float80";
13802 case DW_ATE_HP_float128:
13803 return "DW_ATE_HP_float128";
13804 case DW_ATE_HP_complex_float128:
13805 return "DW_ATE_HP_complex_float128";
13806 case DW_ATE_HP_floathpintel:
13807 return "DW_ATE_HP_floathpintel";
13808 case DW_ATE_HP_imaginary_float80:
13809 return "DW_ATE_HP_imaginary_float80";
13810 case DW_ATE_HP_imaginary_float128:
13811 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13812 default:
13813 return "DW_ATE_<unknown>";
13814 }
13815}
13816
0963b4bd 13817/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13818
13819#if 0
13820static char *
aa1ee363 13821dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13822{
13823 switch (cfi_opc)
13824 {
13825 case DW_CFA_advance_loc:
13826 return "DW_CFA_advance_loc";
13827 case DW_CFA_offset:
13828 return "DW_CFA_offset";
13829 case DW_CFA_restore:
13830 return "DW_CFA_restore";
13831 case DW_CFA_nop:
13832 return "DW_CFA_nop";
13833 case DW_CFA_set_loc:
13834 return "DW_CFA_set_loc";
13835 case DW_CFA_advance_loc1:
13836 return "DW_CFA_advance_loc1";
13837 case DW_CFA_advance_loc2:
13838 return "DW_CFA_advance_loc2";
13839 case DW_CFA_advance_loc4:
13840 return "DW_CFA_advance_loc4";
13841 case DW_CFA_offset_extended:
13842 return "DW_CFA_offset_extended";
13843 case DW_CFA_restore_extended:
13844 return "DW_CFA_restore_extended";
13845 case DW_CFA_undefined:
13846 return "DW_CFA_undefined";
13847 case DW_CFA_same_value:
13848 return "DW_CFA_same_value";
13849 case DW_CFA_register:
13850 return "DW_CFA_register";
13851 case DW_CFA_remember_state:
13852 return "DW_CFA_remember_state";
13853 case DW_CFA_restore_state:
13854 return "DW_CFA_restore_state";
13855 case DW_CFA_def_cfa:
13856 return "DW_CFA_def_cfa";
13857 case DW_CFA_def_cfa_register:
13858 return "DW_CFA_def_cfa_register";
13859 case DW_CFA_def_cfa_offset:
13860 return "DW_CFA_def_cfa_offset";
b7619582 13861 /* DWARF 3. */
985cb1a3
JM
13862 case DW_CFA_def_cfa_expression:
13863 return "DW_CFA_def_cfa_expression";
13864 case DW_CFA_expression:
13865 return "DW_CFA_expression";
13866 case DW_CFA_offset_extended_sf:
13867 return "DW_CFA_offset_extended_sf";
13868 case DW_CFA_def_cfa_sf:
13869 return "DW_CFA_def_cfa_sf";
13870 case DW_CFA_def_cfa_offset_sf:
13871 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13872 case DW_CFA_val_offset:
13873 return "DW_CFA_val_offset";
13874 case DW_CFA_val_offset_sf:
13875 return "DW_CFA_val_offset_sf";
13876 case DW_CFA_val_expression:
13877 return "DW_CFA_val_expression";
13878 /* SGI/MIPS specific. */
c906108c
SS
13879 case DW_CFA_MIPS_advance_loc8:
13880 return "DW_CFA_MIPS_advance_loc8";
b7619582 13881 /* GNU extensions. */
985cb1a3
JM
13882 case DW_CFA_GNU_window_save:
13883 return "DW_CFA_GNU_window_save";
13884 case DW_CFA_GNU_args_size:
13885 return "DW_CFA_GNU_args_size";
13886 case DW_CFA_GNU_negative_offset_extended:
13887 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13888 default:
13889 return "DW_CFA_<unknown>";
13890 }
13891}
13892#endif
13893
f9aca02d 13894static void
d97bc12b 13895dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13896{
13897 unsigned int i;
13898
d97bc12b
DE
13899 print_spaces (indent, f);
13900 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13901 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13902
13903 if (die->parent != NULL)
13904 {
13905 print_spaces (indent, f);
13906 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13907 die->parent->offset);
13908 }
13909
13910 print_spaces (indent, f);
13911 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13912 dwarf_bool_name (die->child != NULL));
c906108c 13913
d97bc12b
DE
13914 print_spaces (indent, f);
13915 fprintf_unfiltered (f, " attributes:\n");
13916
c906108c
SS
13917 for (i = 0; i < die->num_attrs; ++i)
13918 {
d97bc12b
DE
13919 print_spaces (indent, f);
13920 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13921 dwarf_attr_name (die->attrs[i].name),
13922 dwarf_form_name (die->attrs[i].form));
d97bc12b 13923
c906108c
SS
13924 switch (die->attrs[i].form)
13925 {
13926 case DW_FORM_ref_addr:
13927 case DW_FORM_addr:
d97bc12b 13928 fprintf_unfiltered (f, "address: ");
5af949e3 13929 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13930 break;
13931 case DW_FORM_block2:
13932 case DW_FORM_block4:
13933 case DW_FORM_block:
13934 case DW_FORM_block1:
3e43a32a
MS
13935 fprintf_unfiltered (f, "block: size %d",
13936 DW_BLOCK (&die->attrs[i])->size);
c906108c 13937 break;
2dc7f7b3
TT
13938 case DW_FORM_exprloc:
13939 fprintf_unfiltered (f, "expression: size %u",
13940 DW_BLOCK (&die->attrs[i])->size);
13941 break;
10b3939b
DJ
13942 case DW_FORM_ref1:
13943 case DW_FORM_ref2:
13944 case DW_FORM_ref4:
d97bc12b 13945 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13946 (long) (DW_ADDR (&die->attrs[i])));
13947 break;
c906108c
SS
13948 case DW_FORM_data1:
13949 case DW_FORM_data2:
13950 case DW_FORM_data4:
ce5d95e1 13951 case DW_FORM_data8:
c906108c
SS
13952 case DW_FORM_udata:
13953 case DW_FORM_sdata:
43bbcdc2
PH
13954 fprintf_unfiltered (f, "constant: %s",
13955 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13956 break;
2dc7f7b3
TT
13957 case DW_FORM_sec_offset:
13958 fprintf_unfiltered (f, "section offset: %s",
13959 pulongest (DW_UNSND (&die->attrs[i])));
13960 break;
55f1336d 13961 case DW_FORM_ref_sig8:
348e048f
DE
13962 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13963 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13964 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13965 else
13966 fprintf_unfiltered (f, "signatured type, offset: unknown");
13967 break;
c906108c 13968 case DW_FORM_string:
4bdf3d34 13969 case DW_FORM_strp:
8285870a 13970 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13971 DW_STRING (&die->attrs[i])
8285870a
JK
13972 ? DW_STRING (&die->attrs[i]) : "",
13973 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13974 break;
13975 case DW_FORM_flag:
13976 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13977 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13978 else
d97bc12b 13979 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13980 break;
2dc7f7b3
TT
13981 case DW_FORM_flag_present:
13982 fprintf_unfiltered (f, "flag: TRUE");
13983 break;
a8329558 13984 case DW_FORM_indirect:
0963b4bd
MS
13985 /* The reader will have reduced the indirect form to
13986 the "base form" so this form should not occur. */
3e43a32a
MS
13987 fprintf_unfiltered (f,
13988 "unexpected attribute form: DW_FORM_indirect");
a8329558 13989 break;
c906108c 13990 default:
d97bc12b 13991 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13992 die->attrs[i].form);
d97bc12b 13993 break;
c906108c 13994 }
d97bc12b 13995 fprintf_unfiltered (f, "\n");
c906108c
SS
13996 }
13997}
13998
f9aca02d 13999static void
d97bc12b 14000dump_die_for_error (struct die_info *die)
c906108c 14001{
d97bc12b
DE
14002 dump_die_shallow (gdb_stderr, 0, die);
14003}
14004
14005static void
14006dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14007{
14008 int indent = level * 4;
14009
14010 gdb_assert (die != NULL);
14011
14012 if (level >= max_level)
14013 return;
14014
14015 dump_die_shallow (f, indent, die);
14016
14017 if (die->child != NULL)
c906108c 14018 {
d97bc12b
DE
14019 print_spaces (indent, f);
14020 fprintf_unfiltered (f, " Children:");
14021 if (level + 1 < max_level)
14022 {
14023 fprintf_unfiltered (f, "\n");
14024 dump_die_1 (f, level + 1, max_level, die->child);
14025 }
14026 else
14027 {
3e43a32a
MS
14028 fprintf_unfiltered (f,
14029 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14030 }
14031 }
14032
14033 if (die->sibling != NULL && level > 0)
14034 {
14035 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14036 }
14037}
14038
d97bc12b
DE
14039/* This is called from the pdie macro in gdbinit.in.
14040 It's not static so gcc will keep a copy callable from gdb. */
14041
14042void
14043dump_die (struct die_info *die, int max_level)
14044{
14045 dump_die_1 (gdb_stdlog, 0, max_level, die);
14046}
14047
f9aca02d 14048static void
51545339 14049store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14050{
51545339 14051 void **slot;
c906108c 14052
51545339
DJ
14053 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14054
14055 *slot = die;
c906108c
SS
14056}
14057
93311388
DE
14058static int
14059is_ref_attr (struct attribute *attr)
c906108c 14060{
c906108c
SS
14061 switch (attr->form)
14062 {
14063 case DW_FORM_ref_addr:
c906108c
SS
14064 case DW_FORM_ref1:
14065 case DW_FORM_ref2:
14066 case DW_FORM_ref4:
613e1657 14067 case DW_FORM_ref8:
c906108c 14068 case DW_FORM_ref_udata:
93311388 14069 return 1;
c906108c 14070 default:
93311388 14071 return 0;
c906108c 14072 }
93311388
DE
14073}
14074
14075static unsigned int
14076dwarf2_get_ref_die_offset (struct attribute *attr)
14077{
14078 if (is_ref_attr (attr))
14079 return DW_ADDR (attr);
14080
14081 complaint (&symfile_complaints,
14082 _("unsupported die ref attribute form: '%s'"),
14083 dwarf_form_name (attr->form));
14084 return 0;
c906108c
SS
14085}
14086
43bbcdc2
PH
14087/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14088 * the value held by the attribute is not constant. */
a02abb62 14089
43bbcdc2 14090static LONGEST
a02abb62
JB
14091dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14092{
14093 if (attr->form == DW_FORM_sdata)
14094 return DW_SND (attr);
14095 else if (attr->form == DW_FORM_udata
14096 || attr->form == DW_FORM_data1
14097 || attr->form == DW_FORM_data2
14098 || attr->form == DW_FORM_data4
14099 || attr->form == DW_FORM_data8)
14100 return DW_UNSND (attr);
14101 else
14102 {
3e43a32a
MS
14103 complaint (&symfile_complaints,
14104 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14105 dwarf_form_name (attr->form));
14106 return default_value;
14107 }
14108}
14109
03dd20cc 14110/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14111 unit and add it to our queue.
14112 The result is non-zero if PER_CU was queued, otherwise the result is zero
14113 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14114
348e048f 14115static int
03dd20cc
DJ
14116maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14117 struct dwarf2_per_cu_data *per_cu)
14118{
98bfdba5
PA
14119 /* We may arrive here during partial symbol reading, if we need full
14120 DIEs to process an unusual case (e.g. template arguments). Do
14121 not queue PER_CU, just tell our caller to load its DIEs. */
14122 if (dwarf2_per_objfile->reading_partial_symbols)
14123 {
14124 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14125 return 1;
14126 return 0;
14127 }
14128
03dd20cc
DJ
14129 /* Mark the dependence relation so that we don't flush PER_CU
14130 too early. */
14131 dwarf2_add_dependence (this_cu, per_cu);
14132
14133 /* If it's already on the queue, we have nothing to do. */
14134 if (per_cu->queued)
348e048f 14135 return 0;
03dd20cc
DJ
14136
14137 /* If the compilation unit is already loaded, just mark it as
14138 used. */
14139 if (per_cu->cu != NULL)
14140 {
14141 per_cu->cu->last_used = 0;
348e048f 14142 return 0;
03dd20cc
DJ
14143 }
14144
14145 /* Add it to the queue. */
a0f42c21 14146 queue_comp_unit (per_cu);
348e048f
DE
14147
14148 return 1;
14149}
14150
14151/* Follow reference or signature attribute ATTR of SRC_DIE.
14152 On entry *REF_CU is the CU of SRC_DIE.
14153 On exit *REF_CU is the CU of the result. */
14154
14155static struct die_info *
14156follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14157 struct dwarf2_cu **ref_cu)
14158{
14159 struct die_info *die;
14160
14161 if (is_ref_attr (attr))
14162 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14163 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14164 die = follow_die_sig (src_die, attr, ref_cu);
14165 else
14166 {
14167 dump_die_for_error (src_die);
14168 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14169 (*ref_cu)->objfile->name);
14170 }
14171
14172 return die;
03dd20cc
DJ
14173}
14174
5c631832 14175/* Follow reference OFFSET.
673bfd45
DE
14176 On entry *REF_CU is the CU of the source die referencing OFFSET.
14177 On exit *REF_CU is the CU of the result.
14178 Returns NULL if OFFSET is invalid. */
f504f079 14179
f9aca02d 14180static struct die_info *
5c631832 14181follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14182{
10b3939b 14183 struct die_info temp_die;
f2f0e013 14184 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14185
348e048f
DE
14186 gdb_assert (cu->per_cu != NULL);
14187
98bfdba5
PA
14188 target_cu = cu;
14189
b0df02fd 14190 if (cu->per_cu->debug_types_section)
348e048f
DE
14191 {
14192 /* .debug_types CUs cannot reference anything outside their CU.
14193 If they need to, they have to reference a signatured type via
55f1336d 14194 DW_FORM_ref_sig8. */
348e048f 14195 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14196 return NULL;
348e048f
DE
14197 }
14198 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14199 {
14200 struct dwarf2_per_cu_data *per_cu;
9a619af0 14201
45452591 14202 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14203
14204 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14205 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14206 load_full_comp_unit (per_cu);
03dd20cc 14207
10b3939b
DJ
14208 target_cu = per_cu->cu;
14209 }
98bfdba5
PA
14210 else if (cu->dies == NULL)
14211 {
14212 /* We're loading full DIEs during partial symbol reading. */
14213 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14214 load_full_comp_unit (cu->per_cu);
98bfdba5 14215 }
c906108c 14216
f2f0e013 14217 *ref_cu = target_cu;
51545339 14218 temp_die.offset = offset;
5c631832
JK
14219 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14220}
10b3939b 14221
5c631832
JK
14222/* Follow reference attribute ATTR of SRC_DIE.
14223 On entry *REF_CU is the CU of SRC_DIE.
14224 On exit *REF_CU is the CU of the result. */
14225
14226static struct die_info *
14227follow_die_ref (struct die_info *src_die, struct attribute *attr,
14228 struct dwarf2_cu **ref_cu)
14229{
14230 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14231 struct dwarf2_cu *cu = *ref_cu;
14232 struct die_info *die;
14233
14234 die = follow_die_offset (offset, ref_cu);
14235 if (!die)
14236 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14237 "at 0x%x [in module %s]"),
14238 offset, src_die->offset, cu->objfile->name);
348e048f 14239
5c631832
JK
14240 return die;
14241}
14242
d83e736b
JK
14243/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14244 Returned value is intended for DW_OP_call*. Returned
14245 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14246
14247struct dwarf2_locexpr_baton
14248dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14249 struct dwarf2_per_cu_data *per_cu,
14250 CORE_ADDR (*get_frame_pc) (void *baton),
14251 void *baton)
5c631832 14252{
918dd910 14253 struct dwarf2_cu *cu;
5c631832
JK
14254 struct die_info *die;
14255 struct attribute *attr;
14256 struct dwarf2_locexpr_baton retval;
14257
8cf6f0b1
TT
14258 dw2_setup (per_cu->objfile);
14259
918dd910
JK
14260 if (per_cu->cu == NULL)
14261 load_cu (per_cu);
14262 cu = per_cu->cu;
14263
5c631832
JK
14264 die = follow_die_offset (offset, &cu);
14265 if (!die)
14266 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
bb5ed363 14267 offset, per_cu->objfile->name);
5c631832
JK
14268
14269 attr = dwarf2_attr (die, DW_AT_location, cu);
14270 if (!attr)
14271 {
e103e986
JK
14272 /* DWARF: "If there is no such attribute, then there is no effect.".
14273 DATA is ignored if SIZE is 0. */
5c631832 14274
e103e986 14275 retval.data = NULL;
5c631832
JK
14276 retval.size = 0;
14277 }
8cf6f0b1
TT
14278 else if (attr_form_is_section_offset (attr))
14279 {
14280 struct dwarf2_loclist_baton loclist_baton;
14281 CORE_ADDR pc = (*get_frame_pc) (baton);
14282 size_t size;
14283
14284 fill_in_loclist_baton (cu, &loclist_baton, attr);
14285
14286 retval.data = dwarf2_find_location_expression (&loclist_baton,
14287 &size, pc);
14288 retval.size = size;
14289 }
5c631832
JK
14290 else
14291 {
14292 if (!attr_form_is_block (attr))
14293 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14294 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
bb5ed363 14295 offset, per_cu->objfile->name);
5c631832
JK
14296
14297 retval.data = DW_BLOCK (attr)->data;
14298 retval.size = DW_BLOCK (attr)->size;
14299 }
14300 retval.per_cu = cu->per_cu;
918dd910 14301
918dd910
JK
14302 age_cached_comp_units ();
14303
5c631832 14304 return retval;
348e048f
DE
14305}
14306
8a9b8146
TT
14307/* Return the type of the DIE at DIE_OFFSET in the CU named by
14308 PER_CU. */
14309
14310struct type *
14311dwarf2_get_die_type (unsigned int die_offset,
14312 struct dwarf2_per_cu_data *per_cu)
14313{
8a9b8146 14314 dw2_setup (per_cu->objfile);
9ff3b74f 14315 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14316}
14317
348e048f
DE
14318/* Follow the signature attribute ATTR in SRC_DIE.
14319 On entry *REF_CU is the CU of SRC_DIE.
14320 On exit *REF_CU is the CU of the result. */
14321
14322static struct die_info *
14323follow_die_sig (struct die_info *src_die, struct attribute *attr,
14324 struct dwarf2_cu **ref_cu)
14325{
14326 struct objfile *objfile = (*ref_cu)->objfile;
14327 struct die_info temp_die;
14328 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14329 struct dwarf2_cu *sig_cu;
14330 struct die_info *die;
14331
14332 /* sig_type will be NULL if the signatured type is missing from
14333 the debug info. */
14334 if (sig_type == NULL)
14335 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14336 "at 0x%x [in module %s]"),
14337 src_die->offset, objfile->name);
14338
14339 /* If necessary, add it to the queue and load its DIEs. */
14340
14341 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14342 read_signatured_type (sig_type);
348e048f
DE
14343
14344 gdb_assert (sig_type->per_cu.cu != NULL);
14345
14346 sig_cu = sig_type->per_cu.cu;
14347 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14348 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14349 if (die)
14350 {
14351 *ref_cu = sig_cu;
14352 return die;
14353 }
14354
3e43a32a
MS
14355 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14356 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14357 sig_type->type_offset, src_die->offset, objfile->name);
14358}
14359
14360/* Given an offset of a signatured type, return its signatured_type. */
14361
14362static struct signatured_type *
8b70b953
TT
14363lookup_signatured_type_at_offset (struct objfile *objfile,
14364 struct dwarf2_section_info *section,
14365 unsigned int offset)
348e048f 14366{
8b70b953 14367 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14368 unsigned int length, initial_length_size;
14369 unsigned int sig_offset;
14370 struct signatured_type find_entry, *type_sig;
14371
14372 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14373 sig_offset = (initial_length_size
14374 + 2 /*version*/
14375 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14376 + 1 /*address_size*/);
14377 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14378 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14379
14380 /* This is only used to lookup previously recorded types.
14381 If we didn't find it, it's our bug. */
14382 gdb_assert (type_sig != NULL);
b3c8eb43 14383 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14384
14385 return type_sig;
14386}
14387
14388/* Read in signatured type at OFFSET and build its CU and die(s). */
14389
14390static void
14391read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14392 struct dwarf2_section_info *sect,
348e048f
DE
14393 unsigned int offset)
14394{
14395 struct signatured_type *type_sig;
14396
8b70b953 14397 dwarf2_read_section (objfile, sect);
be391dca 14398
348e048f
DE
14399 /* We have the section offset, but we need the signature to do the
14400 hash table lookup. */
8b70b953 14401 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14402
14403 gdb_assert (type_sig->per_cu.cu == NULL);
14404
a0f42c21 14405 read_signatured_type (type_sig);
348e048f
DE
14406
14407 gdb_assert (type_sig->per_cu.cu != NULL);
14408}
14409
14410/* Read in a signatured type and build its CU and DIEs. */
14411
14412static void
a0f42c21 14413read_signatured_type (struct signatured_type *type_sig)
348e048f 14414{
a0f42c21 14415 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14416 gdb_byte *types_ptr;
348e048f
DE
14417 struct die_reader_specs reader_specs;
14418 struct dwarf2_cu *cu;
14419 ULONGEST signature;
14420 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14421 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14422
8b70b953
TT
14423 dwarf2_read_section (objfile, section);
14424 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14425
348e048f
DE
14426 gdb_assert (type_sig->per_cu.cu == NULL);
14427
9816fde3
JK
14428 cu = xmalloc (sizeof (*cu));
14429 init_one_comp_unit (cu, objfile);
14430
348e048f
DE
14431 type_sig->per_cu.cu = cu;
14432 cu->per_cu = &type_sig->per_cu;
14433
14434 /* If an error occurs while loading, release our storage. */
68dc6402 14435 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14436
8b70b953 14437 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14438 types_ptr, objfile->obfd);
14439 gdb_assert (signature == type_sig->signature);
14440
14441 cu->die_hash
14442 = htab_create_alloc_ex (cu->header.length / 12,
14443 die_hash,
14444 die_eq,
14445 NULL,
14446 &cu->comp_unit_obstack,
14447 hashtab_obstack_allocate,
14448 dummy_obstack_deallocate);
14449
a0f42c21 14450 dwarf2_read_abbrevs (objfile->obfd, cu);
348e048f
DE
14451 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14452
14453 init_cu_die_reader (&reader_specs, cu);
14454
14455 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14456 NULL /*parent*/);
14457
14458 /* We try not to read any attributes in this function, because not
9cdd5dbd 14459 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14460 table processing isn't initialized. But we have to set the CU language,
14461 or we won't be able to build types correctly. */
9816fde3 14462 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14463
14464 do_cleanups (back_to);
14465
14466 /* We've successfully allocated this compilation unit. Let our caller
14467 clean it up when finished with it. */
14468 discard_cleanups (free_cu_cleanup);
14469
14470 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14471 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14472}
14473
c906108c
SS
14474/* Decode simple location descriptions.
14475 Given a pointer to a dwarf block that defines a location, compute
14476 the location and return the value.
14477
4cecd739
DJ
14478 NOTE drow/2003-11-18: This function is called in two situations
14479 now: for the address of static or global variables (partial symbols
14480 only) and for offsets into structures which are expected to be
14481 (more or less) constant. The partial symbol case should go away,
14482 and only the constant case should remain. That will let this
14483 function complain more accurately. A few special modes are allowed
14484 without complaint for global variables (for instance, global
14485 register values and thread-local values).
c906108c
SS
14486
14487 A location description containing no operations indicates that the
4cecd739 14488 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14489 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14490 callers will only want a very basic result and this can become a
21ae7a4d
JK
14491 complaint.
14492
14493 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14494
14495static CORE_ADDR
e7c27a73 14496decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14497{
e7c27a73 14498 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14499 int i;
14500 int size = blk->size;
14501 gdb_byte *data = blk->data;
14502 CORE_ADDR stack[64];
14503 int stacki;
14504 unsigned int bytes_read, unsnd;
14505 gdb_byte op;
c906108c 14506
21ae7a4d
JK
14507 i = 0;
14508 stacki = 0;
14509 stack[stacki] = 0;
14510 stack[++stacki] = 0;
14511
14512 while (i < size)
14513 {
14514 op = data[i++];
14515 switch (op)
14516 {
14517 case DW_OP_lit0:
14518 case DW_OP_lit1:
14519 case DW_OP_lit2:
14520 case DW_OP_lit3:
14521 case DW_OP_lit4:
14522 case DW_OP_lit5:
14523 case DW_OP_lit6:
14524 case DW_OP_lit7:
14525 case DW_OP_lit8:
14526 case DW_OP_lit9:
14527 case DW_OP_lit10:
14528 case DW_OP_lit11:
14529 case DW_OP_lit12:
14530 case DW_OP_lit13:
14531 case DW_OP_lit14:
14532 case DW_OP_lit15:
14533 case DW_OP_lit16:
14534 case DW_OP_lit17:
14535 case DW_OP_lit18:
14536 case DW_OP_lit19:
14537 case DW_OP_lit20:
14538 case DW_OP_lit21:
14539 case DW_OP_lit22:
14540 case DW_OP_lit23:
14541 case DW_OP_lit24:
14542 case DW_OP_lit25:
14543 case DW_OP_lit26:
14544 case DW_OP_lit27:
14545 case DW_OP_lit28:
14546 case DW_OP_lit29:
14547 case DW_OP_lit30:
14548 case DW_OP_lit31:
14549 stack[++stacki] = op - DW_OP_lit0;
14550 break;
f1bea926 14551
21ae7a4d
JK
14552 case DW_OP_reg0:
14553 case DW_OP_reg1:
14554 case DW_OP_reg2:
14555 case DW_OP_reg3:
14556 case DW_OP_reg4:
14557 case DW_OP_reg5:
14558 case DW_OP_reg6:
14559 case DW_OP_reg7:
14560 case DW_OP_reg8:
14561 case DW_OP_reg9:
14562 case DW_OP_reg10:
14563 case DW_OP_reg11:
14564 case DW_OP_reg12:
14565 case DW_OP_reg13:
14566 case DW_OP_reg14:
14567 case DW_OP_reg15:
14568 case DW_OP_reg16:
14569 case DW_OP_reg17:
14570 case DW_OP_reg18:
14571 case DW_OP_reg19:
14572 case DW_OP_reg20:
14573 case DW_OP_reg21:
14574 case DW_OP_reg22:
14575 case DW_OP_reg23:
14576 case DW_OP_reg24:
14577 case DW_OP_reg25:
14578 case DW_OP_reg26:
14579 case DW_OP_reg27:
14580 case DW_OP_reg28:
14581 case DW_OP_reg29:
14582 case DW_OP_reg30:
14583 case DW_OP_reg31:
14584 stack[++stacki] = op - DW_OP_reg0;
14585 if (i < size)
14586 dwarf2_complex_location_expr_complaint ();
14587 break;
c906108c 14588
21ae7a4d
JK
14589 case DW_OP_regx:
14590 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14591 i += bytes_read;
14592 stack[++stacki] = unsnd;
14593 if (i < size)
14594 dwarf2_complex_location_expr_complaint ();
14595 break;
c906108c 14596
21ae7a4d
JK
14597 case DW_OP_addr:
14598 stack[++stacki] = read_address (objfile->obfd, &data[i],
14599 cu, &bytes_read);
14600 i += bytes_read;
14601 break;
d53d4ac5 14602
21ae7a4d
JK
14603 case DW_OP_const1u:
14604 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14605 i += 1;
14606 break;
14607
14608 case DW_OP_const1s:
14609 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14610 i += 1;
14611 break;
14612
14613 case DW_OP_const2u:
14614 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14615 i += 2;
14616 break;
14617
14618 case DW_OP_const2s:
14619 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14620 i += 2;
14621 break;
d53d4ac5 14622
21ae7a4d
JK
14623 case DW_OP_const4u:
14624 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14625 i += 4;
14626 break;
14627
14628 case DW_OP_const4s:
14629 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14630 i += 4;
14631 break;
14632
585861ea
JK
14633 case DW_OP_const8u:
14634 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14635 i += 8;
14636 break;
14637
21ae7a4d
JK
14638 case DW_OP_constu:
14639 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14640 &bytes_read);
14641 i += bytes_read;
14642 break;
14643
14644 case DW_OP_consts:
14645 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14646 i += bytes_read;
14647 break;
14648
14649 case DW_OP_dup:
14650 stack[stacki + 1] = stack[stacki];
14651 stacki++;
14652 break;
14653
14654 case DW_OP_plus:
14655 stack[stacki - 1] += stack[stacki];
14656 stacki--;
14657 break;
14658
14659 case DW_OP_plus_uconst:
14660 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14661 &bytes_read);
14662 i += bytes_read;
14663 break;
14664
14665 case DW_OP_minus:
14666 stack[stacki - 1] -= stack[stacki];
14667 stacki--;
14668 break;
14669
14670 case DW_OP_deref:
14671 /* If we're not the last op, then we definitely can't encode
14672 this using GDB's address_class enum. This is valid for partial
14673 global symbols, although the variable's address will be bogus
14674 in the psymtab. */
14675 if (i < size)
14676 dwarf2_complex_location_expr_complaint ();
14677 break;
14678
14679 case DW_OP_GNU_push_tls_address:
14680 /* The top of the stack has the offset from the beginning
14681 of the thread control block at which the variable is located. */
14682 /* Nothing should follow this operator, so the top of stack would
14683 be returned. */
14684 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14685 address will be bogus in the psymtab. Make it always at least
14686 non-zero to not look as a variable garbage collected by linker
14687 which have DW_OP_addr 0. */
21ae7a4d
JK
14688 if (i < size)
14689 dwarf2_complex_location_expr_complaint ();
585861ea 14690 stack[stacki]++;
21ae7a4d
JK
14691 break;
14692
14693 case DW_OP_GNU_uninit:
14694 break;
14695
14696 default:
14697 {
14698 const char *name = dwarf_stack_op_name (op);
14699
14700 if (name)
14701 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14702 name);
14703 else
14704 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14705 op);
14706 }
14707
14708 return (stack[stacki]);
d53d4ac5 14709 }
3c6e0cb3 14710
21ae7a4d
JK
14711 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14712 outside of the allocated space. Also enforce minimum>0. */
14713 if (stacki >= ARRAY_SIZE (stack) - 1)
14714 {
14715 complaint (&symfile_complaints,
14716 _("location description stack overflow"));
14717 return 0;
14718 }
14719
14720 if (stacki <= 0)
14721 {
14722 complaint (&symfile_complaints,
14723 _("location description stack underflow"));
14724 return 0;
14725 }
14726 }
14727 return (stack[stacki]);
c906108c
SS
14728}
14729
14730/* memory allocation interface */
14731
c906108c 14732static struct dwarf_block *
7b5a2f43 14733dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14734{
14735 struct dwarf_block *blk;
14736
14737 blk = (struct dwarf_block *)
7b5a2f43 14738 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14739 return (blk);
14740}
14741
14742static struct abbrev_info *
f3dd6933 14743dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14744{
14745 struct abbrev_info *abbrev;
14746
f3dd6933
DJ
14747 abbrev = (struct abbrev_info *)
14748 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14749 memset (abbrev, 0, sizeof (struct abbrev_info));
14750 return (abbrev);
14751}
14752
14753static struct die_info *
b60c80d6 14754dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14755{
14756 struct die_info *die;
b60c80d6
DJ
14757 size_t size = sizeof (struct die_info);
14758
14759 if (num_attrs > 1)
14760 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14761
b60c80d6 14762 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14763 memset (die, 0, sizeof (struct die_info));
14764 return (die);
14765}
2e276125
JB
14766
14767\f
14768/* Macro support. */
14769
2e276125
JB
14770/* Return the full name of file number I in *LH's file name table.
14771 Use COMP_DIR as the name of the current directory of the
14772 compilation. The result is allocated using xmalloc; the caller is
14773 responsible for freeing it. */
14774static char *
14775file_full_name (int file, struct line_header *lh, const char *comp_dir)
14776{
6a83a1e6
EZ
14777 /* Is the file number a valid index into the line header's file name
14778 table? Remember that file numbers start with one, not zero. */
14779 if (1 <= file && file <= lh->num_file_names)
14780 {
14781 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14782
6a83a1e6
EZ
14783 if (IS_ABSOLUTE_PATH (fe->name))
14784 return xstrdup (fe->name);
14785 else
14786 {
14787 const char *dir;
14788 int dir_len;
14789 char *full_name;
14790
14791 if (fe->dir_index)
14792 dir = lh->include_dirs[fe->dir_index - 1];
14793 else
14794 dir = comp_dir;
14795
14796 if (dir)
14797 {
14798 dir_len = strlen (dir);
14799 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14800 strcpy (full_name, dir);
14801 full_name[dir_len] = '/';
14802 strcpy (full_name + dir_len + 1, fe->name);
14803 return full_name;
14804 }
14805 else
14806 return xstrdup (fe->name);
14807 }
14808 }
2e276125
JB
14809 else
14810 {
6a83a1e6
EZ
14811 /* The compiler produced a bogus file number. We can at least
14812 record the macro definitions made in the file, even if we
14813 won't be able to find the file by name. */
14814 char fake_name[80];
9a619af0 14815
6a83a1e6 14816 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14817
6e70227d 14818 complaint (&symfile_complaints,
6a83a1e6
EZ
14819 _("bad file number in macro information (%d)"),
14820 file);
2e276125 14821
6a83a1e6 14822 return xstrdup (fake_name);
2e276125
JB
14823 }
14824}
14825
14826
14827static struct macro_source_file *
14828macro_start_file (int file, int line,
14829 struct macro_source_file *current_file,
14830 const char *comp_dir,
14831 struct line_header *lh, struct objfile *objfile)
14832{
14833 /* The full name of this source file. */
14834 char *full_name = file_full_name (file, lh, comp_dir);
14835
14836 /* We don't create a macro table for this compilation unit
14837 at all until we actually get a filename. */
14838 if (! pending_macros)
4a146b47 14839 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14840 objfile->macro_cache);
2e276125
JB
14841
14842 if (! current_file)
14843 /* If we have no current file, then this must be the start_file
14844 directive for the compilation unit's main source file. */
14845 current_file = macro_set_main (pending_macros, full_name);
14846 else
14847 current_file = macro_include (current_file, line, full_name);
14848
14849 xfree (full_name);
6e70227d 14850
2e276125
JB
14851 return current_file;
14852}
14853
14854
14855/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14856 followed by a null byte. */
14857static char *
14858copy_string (const char *buf, int len)
14859{
14860 char *s = xmalloc (len + 1);
9a619af0 14861
2e276125
JB
14862 memcpy (s, buf, len);
14863 s[len] = '\0';
2e276125
JB
14864 return s;
14865}
14866
14867
14868static const char *
14869consume_improper_spaces (const char *p, const char *body)
14870{
14871 if (*p == ' ')
14872 {
4d3c2250 14873 complaint (&symfile_complaints,
3e43a32a
MS
14874 _("macro definition contains spaces "
14875 "in formal argument list:\n`%s'"),
4d3c2250 14876 body);
2e276125
JB
14877
14878 while (*p == ' ')
14879 p++;
14880 }
14881
14882 return p;
14883}
14884
14885
14886static void
14887parse_macro_definition (struct macro_source_file *file, int line,
14888 const char *body)
14889{
14890 const char *p;
14891
14892 /* The body string takes one of two forms. For object-like macro
14893 definitions, it should be:
14894
14895 <macro name> " " <definition>
14896
14897 For function-like macro definitions, it should be:
14898
14899 <macro name> "() " <definition>
14900 or
14901 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14902
14903 Spaces may appear only where explicitly indicated, and in the
14904 <definition>.
14905
14906 The Dwarf 2 spec says that an object-like macro's name is always
14907 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14908 the space when the macro's definition is the empty string.
2e276125
JB
14909
14910 The Dwarf 2 spec says that there should be no spaces between the
14911 formal arguments in a function-like macro's formal argument list,
14912 but versions of GCC around March 2002 include spaces after the
14913 commas. */
14914
14915
14916 /* Find the extent of the macro name. The macro name is terminated
14917 by either a space or null character (for an object-like macro) or
14918 an opening paren (for a function-like macro). */
14919 for (p = body; *p; p++)
14920 if (*p == ' ' || *p == '(')
14921 break;
14922
14923 if (*p == ' ' || *p == '\0')
14924 {
14925 /* It's an object-like macro. */
14926 int name_len = p - body;
14927 char *name = copy_string (body, name_len);
14928 const char *replacement;
14929
14930 if (*p == ' ')
14931 replacement = body + name_len + 1;
14932 else
14933 {
4d3c2250 14934 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14935 replacement = body + name_len;
14936 }
6e70227d 14937
2e276125
JB
14938 macro_define_object (file, line, name, replacement);
14939
14940 xfree (name);
14941 }
14942 else if (*p == '(')
14943 {
14944 /* It's a function-like macro. */
14945 char *name = copy_string (body, p - body);
14946 int argc = 0;
14947 int argv_size = 1;
14948 char **argv = xmalloc (argv_size * sizeof (*argv));
14949
14950 p++;
14951
14952 p = consume_improper_spaces (p, body);
14953
14954 /* Parse the formal argument list. */
14955 while (*p && *p != ')')
14956 {
14957 /* Find the extent of the current argument name. */
14958 const char *arg_start = p;
14959
14960 while (*p && *p != ',' && *p != ')' && *p != ' ')
14961 p++;
14962
14963 if (! *p || p == arg_start)
4d3c2250 14964 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14965 else
14966 {
14967 /* Make sure argv has room for the new argument. */
14968 if (argc >= argv_size)
14969 {
14970 argv_size *= 2;
14971 argv = xrealloc (argv, argv_size * sizeof (*argv));
14972 }
14973
14974 argv[argc++] = copy_string (arg_start, p - arg_start);
14975 }
14976
14977 p = consume_improper_spaces (p, body);
14978
14979 /* Consume the comma, if present. */
14980 if (*p == ',')
14981 {
14982 p++;
14983
14984 p = consume_improper_spaces (p, body);
14985 }
14986 }
14987
14988 if (*p == ')')
14989 {
14990 p++;
14991
14992 if (*p == ' ')
14993 /* Perfectly formed definition, no complaints. */
14994 macro_define_function (file, line, name,
6e70227d 14995 argc, (const char **) argv,
2e276125
JB
14996 p + 1);
14997 else if (*p == '\0')
14998 {
14999 /* Complain, but do define it. */
4d3c2250 15000 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15001 macro_define_function (file, line, name,
6e70227d 15002 argc, (const char **) argv,
2e276125
JB
15003 p);
15004 }
15005 else
15006 /* Just complain. */
4d3c2250 15007 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15008 }
15009 else
15010 /* Just complain. */
4d3c2250 15011 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15012
15013 xfree (name);
15014 {
15015 int i;
15016
15017 for (i = 0; i < argc; i++)
15018 xfree (argv[i]);
15019 }
15020 xfree (argv);
15021 }
15022 else
4d3c2250 15023 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15024}
15025
cf2c3c16
TT
15026/* Skip some bytes from BYTES according to the form given in FORM.
15027 Returns the new pointer. */
2e276125 15028
cf2c3c16
TT
15029static gdb_byte *
15030skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15031 enum dwarf_form form,
15032 unsigned int offset_size,
15033 struct dwarf2_section_info *section)
2e276125 15034{
cf2c3c16 15035 unsigned int bytes_read;
2e276125 15036
cf2c3c16 15037 switch (form)
2e276125 15038 {
cf2c3c16
TT
15039 case DW_FORM_data1:
15040 case DW_FORM_flag:
15041 ++bytes;
15042 break;
15043
15044 case DW_FORM_data2:
15045 bytes += 2;
15046 break;
15047
15048 case DW_FORM_data4:
15049 bytes += 4;
15050 break;
15051
15052 case DW_FORM_data8:
15053 bytes += 8;
15054 break;
15055
15056 case DW_FORM_string:
15057 read_direct_string (abfd, bytes, &bytes_read);
15058 bytes += bytes_read;
15059 break;
15060
15061 case DW_FORM_sec_offset:
15062 case DW_FORM_strp:
15063 bytes += offset_size;
15064 break;
15065
15066 case DW_FORM_block:
15067 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15068 bytes += bytes_read;
15069 break;
15070
15071 case DW_FORM_block1:
15072 bytes += 1 + read_1_byte (abfd, bytes);
15073 break;
15074 case DW_FORM_block2:
15075 bytes += 2 + read_2_bytes (abfd, bytes);
15076 break;
15077 case DW_FORM_block4:
15078 bytes += 4 + read_4_bytes (abfd, bytes);
15079 break;
15080
15081 case DW_FORM_sdata:
15082 case DW_FORM_udata:
15083 bytes = skip_leb128 (abfd, bytes);
15084 break;
15085
15086 default:
15087 {
15088 complain:
15089 complaint (&symfile_complaints,
15090 _("invalid form 0x%x in `%s'"),
15091 form,
15092 section->asection->name);
15093 return NULL;
15094 }
2e276125
JB
15095 }
15096
cf2c3c16
TT
15097 return bytes;
15098}
757a13d0 15099
cf2c3c16
TT
15100/* A helper for dwarf_decode_macros that handles skipping an unknown
15101 opcode. Returns an updated pointer to the macro data buffer; or,
15102 on error, issues a complaint and returns NULL. */
757a13d0 15103
cf2c3c16
TT
15104static gdb_byte *
15105skip_unknown_opcode (unsigned int opcode,
15106 gdb_byte **opcode_definitions,
15107 gdb_byte *mac_ptr,
15108 bfd *abfd,
15109 unsigned int offset_size,
15110 struct dwarf2_section_info *section)
15111{
15112 unsigned int bytes_read, i;
15113 unsigned long arg;
15114 gdb_byte *defn;
2e276125 15115
cf2c3c16 15116 if (opcode_definitions[opcode] == NULL)
2e276125 15117 {
cf2c3c16
TT
15118 complaint (&symfile_complaints,
15119 _("unrecognized DW_MACFINO opcode 0x%x"),
15120 opcode);
15121 return NULL;
15122 }
2e276125 15123
cf2c3c16
TT
15124 defn = opcode_definitions[opcode];
15125 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15126 defn += bytes_read;
2e276125 15127
cf2c3c16
TT
15128 for (i = 0; i < arg; ++i)
15129 {
15130 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15131 if (mac_ptr == NULL)
15132 {
15133 /* skip_form_bytes already issued the complaint. */
15134 return NULL;
15135 }
15136 }
757a13d0 15137
cf2c3c16
TT
15138 return mac_ptr;
15139}
757a13d0 15140
cf2c3c16
TT
15141/* A helper function which parses the header of a macro section.
15142 If the macro section is the extended (for now called "GNU") type,
15143 then this updates *OFFSET_SIZE. Returns a pointer to just after
15144 the header, or issues a complaint and returns NULL on error. */
757a13d0 15145
cf2c3c16
TT
15146static gdb_byte *
15147dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15148 bfd *abfd,
15149 gdb_byte *mac_ptr,
15150 unsigned int *offset_size,
15151 int section_is_gnu)
15152{
15153 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15154
cf2c3c16
TT
15155 if (section_is_gnu)
15156 {
15157 unsigned int version, flags;
757a13d0 15158
cf2c3c16
TT
15159 version = read_2_bytes (abfd, mac_ptr);
15160 if (version != 4)
15161 {
15162 complaint (&symfile_complaints,
15163 _("unrecognized version `%d' in .debug_macro section"),
15164 version);
15165 return NULL;
15166 }
15167 mac_ptr += 2;
757a13d0 15168
cf2c3c16
TT
15169 flags = read_1_byte (abfd, mac_ptr);
15170 ++mac_ptr;
15171 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15172
cf2c3c16
TT
15173 if ((flags & 2) != 0)
15174 /* We don't need the line table offset. */
15175 mac_ptr += *offset_size;
757a13d0 15176
cf2c3c16
TT
15177 /* Vendor opcode descriptions. */
15178 if ((flags & 4) != 0)
15179 {
15180 unsigned int i, count;
757a13d0 15181
cf2c3c16
TT
15182 count = read_1_byte (abfd, mac_ptr);
15183 ++mac_ptr;
15184 for (i = 0; i < count; ++i)
15185 {
15186 unsigned int opcode, bytes_read;
15187 unsigned long arg;
15188
15189 opcode = read_1_byte (abfd, mac_ptr);
15190 ++mac_ptr;
15191 opcode_definitions[opcode] = mac_ptr;
15192 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15193 mac_ptr += bytes_read;
15194 mac_ptr += arg;
15195 }
757a13d0 15196 }
cf2c3c16 15197 }
757a13d0 15198
cf2c3c16
TT
15199 return mac_ptr;
15200}
757a13d0 15201
cf2c3c16
TT
15202/* A helper for dwarf_decode_macros that handles the GNU extensions,
15203 including DW_GNU_MACINFO_transparent_include. */
15204
15205static void
15206dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15207 struct macro_source_file *current_file,
15208 struct line_header *lh, char *comp_dir,
15209 struct dwarf2_section_info *section,
15210 int section_is_gnu,
15211 unsigned int offset_size,
15212 struct objfile *objfile)
15213{
15214 enum dwarf_macro_record_type macinfo_type;
15215 int at_commandline;
15216 gdb_byte *opcode_definitions[256];
757a13d0 15217
cf2c3c16
TT
15218 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15219 &offset_size, section_is_gnu);
15220 if (mac_ptr == NULL)
15221 {
15222 /* We already issued a complaint. */
15223 return;
15224 }
757a13d0
JK
15225
15226 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15227 GDB is still reading the definitions from command line. First
15228 DW_MACINFO_start_file will need to be ignored as it was already executed
15229 to create CURRENT_FILE for the main source holding also the command line
15230 definitions. On first met DW_MACINFO_start_file this flag is reset to
15231 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15232
15233 at_commandline = 1;
15234
15235 do
15236 {
15237 /* Do we at least have room for a macinfo type byte? */
15238 if (mac_ptr >= mac_end)
15239 {
cf2c3c16 15240 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15241 break;
15242 }
15243
15244 macinfo_type = read_1_byte (abfd, mac_ptr);
15245 mac_ptr++;
15246
cf2c3c16
TT
15247 /* Note that we rely on the fact that the corresponding GNU and
15248 DWARF constants are the same. */
757a13d0
JK
15249 switch (macinfo_type)
15250 {
15251 /* A zero macinfo type indicates the end of the macro
15252 information. */
15253 case 0:
15254 break;
2e276125 15255
cf2c3c16
TT
15256 case DW_MACRO_GNU_define:
15257 case DW_MACRO_GNU_undef:
15258 case DW_MACRO_GNU_define_indirect:
15259 case DW_MACRO_GNU_undef_indirect:
2e276125 15260 {
891d2f0b 15261 unsigned int bytes_read;
2e276125
JB
15262 int line;
15263 char *body;
cf2c3c16 15264 int is_define;
2e276125 15265
cf2c3c16
TT
15266 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15267 mac_ptr += bytes_read;
15268
15269 if (macinfo_type == DW_MACRO_GNU_define
15270 || macinfo_type == DW_MACRO_GNU_undef)
15271 {
15272 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15273 mac_ptr += bytes_read;
15274 }
15275 else
15276 {
15277 LONGEST str_offset;
15278
15279 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15280 mac_ptr += offset_size;
2e276125 15281
cf2c3c16
TT
15282 body = read_indirect_string_at_offset (abfd, str_offset);
15283 }
15284
15285 is_define = (macinfo_type == DW_MACRO_GNU_define
15286 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15287 if (! current_file)
757a13d0
JK
15288 {
15289 /* DWARF violation as no main source is present. */
15290 complaint (&symfile_complaints,
15291 _("debug info with no main source gives macro %s "
15292 "on line %d: %s"),
cf2c3c16
TT
15293 is_define ? _("definition") : _("undefinition"),
15294 line, body);
757a13d0
JK
15295 break;
15296 }
3e43a32a
MS
15297 if ((line == 0 && !at_commandline)
15298 || (line != 0 && at_commandline))
4d3c2250 15299 complaint (&symfile_complaints,
757a13d0
JK
15300 _("debug info gives %s macro %s with %s line %d: %s"),
15301 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15302 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15303 line == 0 ? _("zero") : _("non-zero"), line, body);
15304
cf2c3c16 15305 if (is_define)
757a13d0 15306 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15307 else
15308 {
15309 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15310 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15311 macro_undef (current_file, line, body);
15312 }
2e276125
JB
15313 }
15314 break;
15315
cf2c3c16 15316 case DW_MACRO_GNU_start_file:
2e276125 15317 {
891d2f0b 15318 unsigned int bytes_read;
2e276125
JB
15319 int line, file;
15320
15321 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15322 mac_ptr += bytes_read;
15323 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15324 mac_ptr += bytes_read;
15325
3e43a32a
MS
15326 if ((line == 0 && !at_commandline)
15327 || (line != 0 && at_commandline))
757a13d0
JK
15328 complaint (&symfile_complaints,
15329 _("debug info gives source %d included "
15330 "from %s at %s line %d"),
15331 file, at_commandline ? _("command-line") : _("file"),
15332 line == 0 ? _("zero") : _("non-zero"), line);
15333
15334 if (at_commandline)
15335 {
cf2c3c16
TT
15336 /* This DW_MACRO_GNU_start_file was executed in the
15337 pass one. */
757a13d0
JK
15338 at_commandline = 0;
15339 }
15340 else
15341 current_file = macro_start_file (file, line,
15342 current_file, comp_dir,
cf2c3c16 15343 lh, objfile);
2e276125
JB
15344 }
15345 break;
15346
cf2c3c16 15347 case DW_MACRO_GNU_end_file:
2e276125 15348 if (! current_file)
4d3c2250 15349 complaint (&symfile_complaints,
3e43a32a
MS
15350 _("macro debug info has an unmatched "
15351 "`close_file' directive"));
2e276125
JB
15352 else
15353 {
15354 current_file = current_file->included_by;
15355 if (! current_file)
15356 {
cf2c3c16 15357 enum dwarf_macro_record_type next_type;
2e276125
JB
15358
15359 /* GCC circa March 2002 doesn't produce the zero
15360 type byte marking the end of the compilation
15361 unit. Complain if it's not there, but exit no
15362 matter what. */
15363
15364 /* Do we at least have room for a macinfo type byte? */
15365 if (mac_ptr >= mac_end)
15366 {
cf2c3c16 15367 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15368 return;
15369 }
15370
15371 /* We don't increment mac_ptr here, so this is just
15372 a look-ahead. */
15373 next_type = read_1_byte (abfd, mac_ptr);
15374 if (next_type != 0)
4d3c2250 15375 complaint (&symfile_complaints,
3e43a32a
MS
15376 _("no terminating 0-type entry for "
15377 "macros in `.debug_macinfo' section"));
2e276125
JB
15378
15379 return;
15380 }
15381 }
15382 break;
15383
cf2c3c16
TT
15384 case DW_MACRO_GNU_transparent_include:
15385 {
15386 LONGEST offset;
15387
15388 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15389 mac_ptr += offset_size;
15390
15391 dwarf_decode_macro_bytes (abfd,
15392 section->buffer + offset,
15393 mac_end, current_file,
15394 lh, comp_dir,
15395 section, section_is_gnu,
15396 offset_size, objfile);
15397 }
15398 break;
15399
2e276125 15400 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15401 if (!section_is_gnu)
15402 {
15403 unsigned int bytes_read;
15404 int constant;
2e276125 15405
cf2c3c16
TT
15406 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15407 mac_ptr += bytes_read;
15408 read_direct_string (abfd, mac_ptr, &bytes_read);
15409 mac_ptr += bytes_read;
2e276125 15410
cf2c3c16
TT
15411 /* We don't recognize any vendor extensions. */
15412 break;
15413 }
15414 /* FALLTHROUGH */
15415
15416 default:
15417 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15418 mac_ptr, abfd, offset_size,
15419 section);
15420 if (mac_ptr == NULL)
15421 return;
15422 break;
2e276125 15423 }
757a13d0 15424 } while (macinfo_type != 0);
2e276125 15425}
8e19ed76 15426
cf2c3c16
TT
15427static void
15428dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15429 char *comp_dir, bfd *abfd,
15430 struct dwarf2_cu *cu,
15431 struct dwarf2_section_info *section,
15432 int section_is_gnu)
15433{
bb5ed363 15434 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15435 gdb_byte *mac_ptr, *mac_end;
15436 struct macro_source_file *current_file = 0;
15437 enum dwarf_macro_record_type macinfo_type;
15438 unsigned int offset_size = cu->header.offset_size;
15439 gdb_byte *opcode_definitions[256];
15440
bb5ed363 15441 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15442 if (section->buffer == NULL)
15443 {
15444 complaint (&symfile_complaints, _("missing %s section"),
15445 section->asection->name);
15446 return;
15447 }
15448
15449 /* First pass: Find the name of the base filename.
15450 This filename is needed in order to process all macros whose definition
15451 (or undefinition) comes from the command line. These macros are defined
15452 before the first DW_MACINFO_start_file entry, and yet still need to be
15453 associated to the base file.
15454
15455 To determine the base file name, we scan the macro definitions until we
15456 reach the first DW_MACINFO_start_file entry. We then initialize
15457 CURRENT_FILE accordingly so that any macro definition found before the
15458 first DW_MACINFO_start_file can still be associated to the base file. */
15459
15460 mac_ptr = section->buffer + offset;
15461 mac_end = section->buffer + section->size;
15462
15463 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15464 &offset_size, section_is_gnu);
15465 if (mac_ptr == NULL)
15466 {
15467 /* We already issued a complaint. */
15468 return;
15469 }
15470
15471 do
15472 {
15473 /* Do we at least have room for a macinfo type byte? */
15474 if (mac_ptr >= mac_end)
15475 {
15476 /* Complaint is printed during the second pass as GDB will probably
15477 stop the first pass earlier upon finding
15478 DW_MACINFO_start_file. */
15479 break;
15480 }
15481
15482 macinfo_type = read_1_byte (abfd, mac_ptr);
15483 mac_ptr++;
15484
15485 /* Note that we rely on the fact that the corresponding GNU and
15486 DWARF constants are the same. */
15487 switch (macinfo_type)
15488 {
15489 /* A zero macinfo type indicates the end of the macro
15490 information. */
15491 case 0:
15492 break;
15493
15494 case DW_MACRO_GNU_define:
15495 case DW_MACRO_GNU_undef:
15496 /* Only skip the data by MAC_PTR. */
15497 {
15498 unsigned int bytes_read;
15499
15500 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15501 mac_ptr += bytes_read;
15502 read_direct_string (abfd, mac_ptr, &bytes_read);
15503 mac_ptr += bytes_read;
15504 }
15505 break;
15506
15507 case DW_MACRO_GNU_start_file:
15508 {
15509 unsigned int bytes_read;
15510 int line, file;
15511
15512 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15513 mac_ptr += bytes_read;
15514 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15515 mac_ptr += bytes_read;
15516
15517 current_file = macro_start_file (file, line, current_file,
bb5ed363 15518 comp_dir, lh, objfile);
cf2c3c16
TT
15519 }
15520 break;
15521
15522 case DW_MACRO_GNU_end_file:
15523 /* No data to skip by MAC_PTR. */
15524 break;
15525
15526 case DW_MACRO_GNU_define_indirect:
15527 case DW_MACRO_GNU_undef_indirect:
15528 {
15529 unsigned int bytes_read;
15530
15531 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15532 mac_ptr += bytes_read;
15533 mac_ptr += offset_size;
15534 }
15535 break;
15536
15537 case DW_MACRO_GNU_transparent_include:
15538 /* Note that, according to the spec, a transparent include
15539 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15540 skip this opcode. */
15541 mac_ptr += offset_size;
15542 break;
15543
15544 case DW_MACINFO_vendor_ext:
15545 /* Only skip the data by MAC_PTR. */
15546 if (!section_is_gnu)
15547 {
15548 unsigned int bytes_read;
15549
15550 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15551 mac_ptr += bytes_read;
15552 read_direct_string (abfd, mac_ptr, &bytes_read);
15553 mac_ptr += bytes_read;
15554 }
15555 /* FALLTHROUGH */
15556
15557 default:
15558 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15559 mac_ptr, abfd, offset_size,
15560 section);
15561 if (mac_ptr == NULL)
15562 return;
15563 break;
15564 }
15565 } while (macinfo_type != 0 && current_file == NULL);
15566
15567 /* Second pass: Process all entries.
15568
15569 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15570 command-line macro definitions/undefinitions. This flag is unset when we
15571 reach the first DW_MACINFO_start_file entry. */
15572
15573 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15574 current_file, lh, comp_dir, section, section_is_gnu,
bb5ed363 15575 offset_size, objfile);
cf2c3c16
TT
15576}
15577
8e19ed76 15578/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15579 if so return true else false. */
8e19ed76
PS
15580static int
15581attr_form_is_block (struct attribute *attr)
15582{
15583 return (attr == NULL ? 0 :
15584 attr->form == DW_FORM_block1
15585 || attr->form == DW_FORM_block2
15586 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15587 || attr->form == DW_FORM_block
15588 || attr->form == DW_FORM_exprloc);
8e19ed76 15589}
4c2df51b 15590
c6a0999f
JB
15591/* Return non-zero if ATTR's value is a section offset --- classes
15592 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15593 You may use DW_UNSND (attr) to retrieve such offsets.
15594
15595 Section 7.5.4, "Attribute Encodings", explains that no attribute
15596 may have a value that belongs to more than one of these classes; it
15597 would be ambiguous if we did, because we use the same forms for all
15598 of them. */
3690dd37
JB
15599static int
15600attr_form_is_section_offset (struct attribute *attr)
15601{
15602 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15603 || attr->form == DW_FORM_data8
15604 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15605}
15606
15607
15608/* Return non-zero if ATTR's value falls in the 'constant' class, or
15609 zero otherwise. When this function returns true, you can apply
15610 dwarf2_get_attr_constant_value to it.
15611
15612 However, note that for some attributes you must check
15613 attr_form_is_section_offset before using this test. DW_FORM_data4
15614 and DW_FORM_data8 are members of both the constant class, and of
15615 the classes that contain offsets into other debug sections
15616 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15617 that, if an attribute's can be either a constant or one of the
15618 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15619 taken as section offsets, not constants. */
15620static int
15621attr_form_is_constant (struct attribute *attr)
15622{
15623 switch (attr->form)
15624 {
15625 case DW_FORM_sdata:
15626 case DW_FORM_udata:
15627 case DW_FORM_data1:
15628 case DW_FORM_data2:
15629 case DW_FORM_data4:
15630 case DW_FORM_data8:
15631 return 1;
15632 default:
15633 return 0;
15634 }
15635}
15636
8cf6f0b1
TT
15637/* A helper function that fills in a dwarf2_loclist_baton. */
15638
15639static void
15640fill_in_loclist_baton (struct dwarf2_cu *cu,
15641 struct dwarf2_loclist_baton *baton,
15642 struct attribute *attr)
15643{
15644 dwarf2_read_section (dwarf2_per_objfile->objfile,
15645 &dwarf2_per_objfile->loc);
15646
15647 baton->per_cu = cu->per_cu;
15648 gdb_assert (baton->per_cu);
15649 /* We don't know how long the location list is, but make sure we
15650 don't run off the edge of the section. */
15651 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15652 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15653 baton->base_address = cu->base_address;
15654}
15655
4c2df51b
DJ
15656static void
15657dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15658 struct dwarf2_cu *cu)
4c2df51b 15659{
bb5ed363
DE
15660 struct objfile *objfile = dwarf2_per_objfile->objfile;
15661
3690dd37 15662 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15663 /* ".debug_loc" may not exist at all, or the offset may be outside
15664 the section. If so, fall through to the complaint in the
15665 other branch. */
bb5ed363 15666 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15667 &dwarf2_per_objfile->loc))
4c2df51b 15668 {
0d53c4c4 15669 struct dwarf2_loclist_baton *baton;
4c2df51b 15670
bb5ed363 15671 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15672 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15673
8cf6f0b1 15674 fill_in_loclist_baton (cu, baton, attr);
be391dca 15675
d00adf39 15676 if (cu->base_known == 0)
0d53c4c4 15677 complaint (&symfile_complaints,
3e43a32a
MS
15678 _("Location list used without "
15679 "specifying the CU base address."));
4c2df51b 15680
768a979c 15681 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15682 SYMBOL_LOCATION_BATON (sym) = baton;
15683 }
15684 else
15685 {
15686 struct dwarf2_locexpr_baton *baton;
15687
bb5ed363 15688 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15689 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15690 baton->per_cu = cu->per_cu;
15691 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15692
15693 if (attr_form_is_block (attr))
15694 {
15695 /* Note that we're just copying the block's data pointer
15696 here, not the actual data. We're still pointing into the
6502dd73
DJ
15697 info_buffer for SYM's objfile; right now we never release
15698 that buffer, but when we do clean up properly this may
15699 need to change. */
0d53c4c4
DJ
15700 baton->size = DW_BLOCK (attr)->size;
15701 baton->data = DW_BLOCK (attr)->data;
15702 }
15703 else
15704 {
15705 dwarf2_invalid_attrib_class_complaint ("location description",
15706 SYMBOL_NATURAL_NAME (sym));
15707 baton->size = 0;
0d53c4c4 15708 }
6e70227d 15709
768a979c 15710 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15711 SYMBOL_LOCATION_BATON (sym) = baton;
15712 }
4c2df51b 15713}
6502dd73 15714
9aa1f1e3
TT
15715/* Return the OBJFILE associated with the compilation unit CU. If CU
15716 came from a separate debuginfo file, then the master objfile is
15717 returned. */
ae0d2f24
UW
15718
15719struct objfile *
15720dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15721{
9291a0cd 15722 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15723
15724 /* Return the master objfile, so that we can report and look up the
15725 correct file containing this variable. */
15726 if (objfile->separate_debug_objfile_backlink)
15727 objfile = objfile->separate_debug_objfile_backlink;
15728
15729 return objfile;
15730}
15731
96408a79
SA
15732/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15733 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15734 CU_HEADERP first. */
15735
15736static const struct comp_unit_head *
15737per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15738 struct dwarf2_per_cu_data *per_cu)
15739{
15740 struct objfile *objfile;
15741 struct dwarf2_per_objfile *per_objfile;
15742 gdb_byte *info_ptr;
15743
15744 if (per_cu->cu)
15745 return &per_cu->cu->header;
15746
15747 objfile = per_cu->objfile;
15748 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15749 info_ptr = per_objfile->info.buffer + per_cu->offset;
15750
15751 memset (cu_headerp, 0, sizeof (*cu_headerp));
15752 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15753
15754 return cu_headerp;
15755}
15756
ae0d2f24
UW
15757/* Return the address size given in the compilation unit header for CU. */
15758
15759CORE_ADDR
15760dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15761{
96408a79
SA
15762 struct comp_unit_head cu_header_local;
15763 const struct comp_unit_head *cu_headerp;
c471e790 15764
96408a79
SA
15765 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15766
15767 return cu_headerp->addr_size;
ae0d2f24
UW
15768}
15769
9eae7c52
TT
15770/* Return the offset size given in the compilation unit header for CU. */
15771
15772int
15773dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15774{
96408a79
SA
15775 struct comp_unit_head cu_header_local;
15776 const struct comp_unit_head *cu_headerp;
9c6c53f7 15777
96408a79
SA
15778 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15779
15780 return cu_headerp->offset_size;
15781}
15782
15783/* See its dwarf2loc.h declaration. */
15784
15785int
15786dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15787{
15788 struct comp_unit_head cu_header_local;
15789 const struct comp_unit_head *cu_headerp;
15790
15791 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15792
15793 if (cu_headerp->version == 2)
15794 return cu_headerp->addr_size;
15795 else
15796 return cu_headerp->offset_size;
181cebd4
JK
15797}
15798
9aa1f1e3
TT
15799/* Return the text offset of the CU. The returned offset comes from
15800 this CU's objfile. If this objfile came from a separate debuginfo
15801 file, then the offset may be different from the corresponding
15802 offset in the parent objfile. */
15803
15804CORE_ADDR
15805dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15806{
bb3fa9d0 15807 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15808
15809 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15810}
15811
348e048f
DE
15812/* Locate the .debug_info compilation unit from CU's objfile which contains
15813 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15814
15815static struct dwarf2_per_cu_data *
c764a876 15816dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15817 struct objfile *objfile)
15818{
15819 struct dwarf2_per_cu_data *this_cu;
15820 int low, high;
15821
ae038cb0
DJ
15822 low = 0;
15823 high = dwarf2_per_objfile->n_comp_units - 1;
15824 while (high > low)
15825 {
15826 int mid = low + (high - low) / 2;
9a619af0 15827
ae038cb0
DJ
15828 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15829 high = mid;
15830 else
15831 low = mid + 1;
15832 }
15833 gdb_assert (low == high);
15834 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15835 {
10b3939b 15836 if (low == 0)
8a3fe4f8
AC
15837 error (_("Dwarf Error: could not find partial DIE containing "
15838 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15839 (long) offset, bfd_get_filename (objfile->obfd));
15840
ae038cb0
DJ
15841 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15842 return dwarf2_per_objfile->all_comp_units[low-1];
15843 }
15844 else
15845 {
15846 this_cu = dwarf2_per_objfile->all_comp_units[low];
15847 if (low == dwarf2_per_objfile->n_comp_units - 1
15848 && offset >= this_cu->offset + this_cu->length)
c764a876 15849 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15850 gdb_assert (offset < this_cu->offset + this_cu->length);
15851 return this_cu;
15852 }
15853}
15854
10b3939b
DJ
15855/* Locate the compilation unit from OBJFILE which is located at exactly
15856 OFFSET. Raises an error on failure. */
15857
ae038cb0 15858static struct dwarf2_per_cu_data *
c764a876 15859dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15860{
15861 struct dwarf2_per_cu_data *this_cu;
9a619af0 15862
ae038cb0
DJ
15863 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15864 if (this_cu->offset != offset)
c764a876 15865 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15866 return this_cu;
15867}
15868
9816fde3 15869/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15870
9816fde3
JK
15871static void
15872init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15873{
9816fde3 15874 memset (cu, 0, sizeof (*cu));
93311388
DE
15875 cu->objfile = objfile;
15876 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15877}
15878
15879/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15880
15881static void
15882prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15883{
15884 struct attribute *attr;
15885
15886 /* Set the language we're debugging. */
15887 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15888 if (attr)
15889 set_cu_language (DW_UNSND (attr), cu);
15890 else
9cded63f
TT
15891 {
15892 cu->language = language_minimal;
15893 cu->language_defn = language_def (cu->language);
15894 }
93311388
DE
15895}
15896
ae038cb0
DJ
15897/* Release one cached compilation unit, CU. We unlink it from the tree
15898 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15899 the caller is responsible for that.
15900 NOTE: DATA is a void * because this function is also used as a
15901 cleanup routine. */
ae038cb0
DJ
15902
15903static void
68dc6402 15904free_heap_comp_unit (void *data)
ae038cb0
DJ
15905{
15906 struct dwarf2_cu *cu = data;
15907
15908 if (cu->per_cu != NULL)
15909 cu->per_cu->cu = NULL;
15910 cu->per_cu = NULL;
15911
15912 obstack_free (&cu->comp_unit_obstack, NULL);
15913
15914 xfree (cu);
15915}
15916
72bf9492 15917/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15918 when we're finished with it. We can't free the pointer itself, but be
15919 sure to unlink it from the cache. Also release any associated storage
15920 and perform cache maintenance.
72bf9492
DJ
15921
15922 Only used during partial symbol parsing. */
15923
15924static void
15925free_stack_comp_unit (void *data)
15926{
15927 struct dwarf2_cu *cu = data;
15928
15929 obstack_free (&cu->comp_unit_obstack, NULL);
15930 cu->partial_dies = NULL;
ae038cb0
DJ
15931
15932 if (cu->per_cu != NULL)
15933 {
15934 /* This compilation unit is on the stack in our caller, so we
15935 should not xfree it. Just unlink it. */
15936 cu->per_cu->cu = NULL;
15937 cu->per_cu = NULL;
15938
15939 /* If we had a per-cu pointer, then we may have other compilation
15940 units loaded, so age them now. */
15941 age_cached_comp_units ();
15942 }
15943}
15944
15945/* Free all cached compilation units. */
15946
15947static void
15948free_cached_comp_units (void *data)
15949{
15950 struct dwarf2_per_cu_data *per_cu, **last_chain;
15951
15952 per_cu = dwarf2_per_objfile->read_in_chain;
15953 last_chain = &dwarf2_per_objfile->read_in_chain;
15954 while (per_cu != NULL)
15955 {
15956 struct dwarf2_per_cu_data *next_cu;
15957
15958 next_cu = per_cu->cu->read_in_chain;
15959
68dc6402 15960 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15961 *last_chain = next_cu;
15962
15963 per_cu = next_cu;
15964 }
15965}
15966
15967/* Increase the age counter on each cached compilation unit, and free
15968 any that are too old. */
15969
15970static void
15971age_cached_comp_units (void)
15972{
15973 struct dwarf2_per_cu_data *per_cu, **last_chain;
15974
15975 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15976 per_cu = dwarf2_per_objfile->read_in_chain;
15977 while (per_cu != NULL)
15978 {
15979 per_cu->cu->last_used ++;
15980 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15981 dwarf2_mark (per_cu->cu);
15982 per_cu = per_cu->cu->read_in_chain;
15983 }
15984
15985 per_cu = dwarf2_per_objfile->read_in_chain;
15986 last_chain = &dwarf2_per_objfile->read_in_chain;
15987 while (per_cu != NULL)
15988 {
15989 struct dwarf2_per_cu_data *next_cu;
15990
15991 next_cu = per_cu->cu->read_in_chain;
15992
15993 if (!per_cu->cu->mark)
15994 {
68dc6402 15995 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15996 *last_chain = next_cu;
15997 }
15998 else
15999 last_chain = &per_cu->cu->read_in_chain;
16000
16001 per_cu = next_cu;
16002 }
16003}
16004
16005/* Remove a single compilation unit from the cache. */
16006
16007static void
16008free_one_cached_comp_unit (void *target_cu)
16009{
16010 struct dwarf2_per_cu_data *per_cu, **last_chain;
16011
16012 per_cu = dwarf2_per_objfile->read_in_chain;
16013 last_chain = &dwarf2_per_objfile->read_in_chain;
16014 while (per_cu != NULL)
16015 {
16016 struct dwarf2_per_cu_data *next_cu;
16017
16018 next_cu = per_cu->cu->read_in_chain;
16019
16020 if (per_cu->cu == target_cu)
16021 {
68dc6402 16022 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16023 *last_chain = next_cu;
16024 break;
16025 }
16026 else
16027 last_chain = &per_cu->cu->read_in_chain;
16028
16029 per_cu = next_cu;
16030 }
16031}
16032
fe3e1990
DJ
16033/* Release all extra memory associated with OBJFILE. */
16034
16035void
16036dwarf2_free_objfile (struct objfile *objfile)
16037{
16038 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16039
16040 if (dwarf2_per_objfile == NULL)
16041 return;
16042
16043 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16044 free_cached_comp_units (NULL);
16045
7b9f3c50
DE
16046 if (dwarf2_per_objfile->quick_file_names_table)
16047 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16048
fe3e1990
DJ
16049 /* Everything else should be on the objfile obstack. */
16050}
16051
1c379e20
DJ
16052/* A pair of DIE offset and GDB type pointer. We store these
16053 in a hash table separate from the DIEs, and preserve them
16054 when the DIEs are flushed out of cache. */
16055
16056struct dwarf2_offset_and_type
16057{
16058 unsigned int offset;
16059 struct type *type;
16060};
16061
16062/* Hash function for a dwarf2_offset_and_type. */
16063
16064static hashval_t
16065offset_and_type_hash (const void *item)
16066{
16067 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16068
1c379e20
DJ
16069 return ofs->offset;
16070}
16071
16072/* Equality function for a dwarf2_offset_and_type. */
16073
16074static int
16075offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16076{
16077 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16078 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16079
1c379e20
DJ
16080 return ofs_lhs->offset == ofs_rhs->offset;
16081}
16082
16083/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16084 table if necessary. For convenience, return TYPE.
16085
16086 The DIEs reading must have careful ordering to:
16087 * Not cause infite loops trying to read in DIEs as a prerequisite for
16088 reading current DIE.
16089 * Not trying to dereference contents of still incompletely read in types
16090 while reading in other DIEs.
16091 * Enable referencing still incompletely read in types just by a pointer to
16092 the type without accessing its fields.
16093
16094 Therefore caller should follow these rules:
16095 * Try to fetch any prerequisite types we may need to build this DIE type
16096 before building the type and calling set_die_type.
e71ec853 16097 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16098 possible before fetching more types to complete the current type.
16099 * Make the type as complete as possible before fetching more types. */
1c379e20 16100
f792889a 16101static struct type *
1c379e20
DJ
16102set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16103{
16104 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16105 struct objfile *objfile = cu->objfile;
16106 htab_t *type_hash_ptr;
1c379e20 16107
b4ba55a1
JB
16108 /* For Ada types, make sure that the gnat-specific data is always
16109 initialized (if not already set). There are a few types where
16110 we should not be doing so, because the type-specific area is
16111 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16112 where the type-specific area is used to store the floatformat).
16113 But this is not a problem, because the gnat-specific information
16114 is actually not needed for these types. */
16115 if (need_gnat_info (cu)
16116 && TYPE_CODE (type) != TYPE_CODE_FUNC
16117 && TYPE_CODE (type) != TYPE_CODE_FLT
16118 && !HAVE_GNAT_AUX_INFO (type))
16119 INIT_GNAT_SPECIFIC (type);
16120
b0df02fd 16121 if (cu->per_cu->debug_types_section)
673bfd45
DE
16122 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16123 else
16124 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16125
16126 if (*type_hash_ptr == NULL)
f792889a 16127 {
673bfd45
DE
16128 *type_hash_ptr
16129 = htab_create_alloc_ex (127,
f792889a
DJ
16130 offset_and_type_hash,
16131 offset_and_type_eq,
16132 NULL,
673bfd45 16133 &objfile->objfile_obstack,
f792889a
DJ
16134 hashtab_obstack_allocate,
16135 dummy_obstack_deallocate);
f792889a 16136 }
1c379e20
DJ
16137
16138 ofs.offset = die->offset;
16139 ofs.type = type;
16140 slot = (struct dwarf2_offset_and_type **)
673bfd45 16141 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16142 if (*slot)
16143 complaint (&symfile_complaints,
16144 _("A problem internal to GDB: DIE 0x%x has type already set"),
16145 die->offset);
673bfd45 16146 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16147 **slot = ofs;
f792889a 16148 return type;
1c379e20
DJ
16149}
16150
673bfd45
DE
16151/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16152 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16153
16154static struct type *
673bfd45
DE
16155get_die_type_at_offset (unsigned int offset,
16156 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16157{
16158 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16159 htab_t type_hash;
f792889a 16160
b0df02fd 16161 if (per_cu->debug_types_section)
673bfd45
DE
16162 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16163 else
16164 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16165 if (type_hash == NULL)
16166 return NULL;
1c379e20 16167
673bfd45 16168 ofs.offset = offset;
1c379e20
DJ
16169 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16170 if (slot)
16171 return slot->type;
16172 else
16173 return NULL;
16174}
16175
673bfd45
DE
16176/* Look up the type for DIE in the appropriate type_hash table,
16177 or return NULL if DIE does not have a saved type. */
16178
16179static struct type *
16180get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16181{
16182 return get_die_type_at_offset (die->offset, cu->per_cu);
16183}
16184
10b3939b
DJ
16185/* Add a dependence relationship from CU to REF_PER_CU. */
16186
16187static void
16188dwarf2_add_dependence (struct dwarf2_cu *cu,
16189 struct dwarf2_per_cu_data *ref_per_cu)
16190{
16191 void **slot;
16192
16193 if (cu->dependencies == NULL)
16194 cu->dependencies
16195 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16196 NULL, &cu->comp_unit_obstack,
16197 hashtab_obstack_allocate,
16198 dummy_obstack_deallocate);
16199
16200 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16201 if (*slot == NULL)
16202 *slot = ref_per_cu;
16203}
1c379e20 16204
f504f079
DE
16205/* Subroutine of dwarf2_mark to pass to htab_traverse.
16206 Set the mark field in every compilation unit in the
ae038cb0
DJ
16207 cache that we must keep because we are keeping CU. */
16208
10b3939b
DJ
16209static int
16210dwarf2_mark_helper (void **slot, void *data)
16211{
16212 struct dwarf2_per_cu_data *per_cu;
16213
16214 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16215
16216 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16217 reading of the chain. As such dependencies remain valid it is not much
16218 useful to track and undo them during QUIT cleanups. */
16219 if (per_cu->cu == NULL)
16220 return 1;
16221
10b3939b
DJ
16222 if (per_cu->cu->mark)
16223 return 1;
16224 per_cu->cu->mark = 1;
16225
16226 if (per_cu->cu->dependencies != NULL)
16227 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16228
16229 return 1;
16230}
16231
f504f079
DE
16232/* Set the mark field in CU and in every other compilation unit in the
16233 cache that we must keep because we are keeping CU. */
16234
ae038cb0
DJ
16235static void
16236dwarf2_mark (struct dwarf2_cu *cu)
16237{
16238 if (cu->mark)
16239 return;
16240 cu->mark = 1;
10b3939b
DJ
16241 if (cu->dependencies != NULL)
16242 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16243}
16244
16245static void
16246dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16247{
16248 while (per_cu)
16249 {
16250 per_cu->cu->mark = 0;
16251 per_cu = per_cu->cu->read_in_chain;
16252 }
72bf9492
DJ
16253}
16254
72bf9492
DJ
16255/* Trivial hash function for partial_die_info: the hash value of a DIE
16256 is its offset in .debug_info for this objfile. */
16257
16258static hashval_t
16259partial_die_hash (const void *item)
16260{
16261 const struct partial_die_info *part_die = item;
9a619af0 16262
72bf9492
DJ
16263 return part_die->offset;
16264}
16265
16266/* Trivial comparison function for partial_die_info structures: two DIEs
16267 are equal if they have the same offset. */
16268
16269static int
16270partial_die_eq (const void *item_lhs, const void *item_rhs)
16271{
16272 const struct partial_die_info *part_die_lhs = item_lhs;
16273 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16274
72bf9492
DJ
16275 return part_die_lhs->offset == part_die_rhs->offset;
16276}
16277
ae038cb0
DJ
16278static struct cmd_list_element *set_dwarf2_cmdlist;
16279static struct cmd_list_element *show_dwarf2_cmdlist;
16280
16281static void
16282set_dwarf2_cmd (char *args, int from_tty)
16283{
16284 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16285}
16286
16287static void
16288show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16289{
ae038cb0
DJ
16290 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16291}
16292
dce234bc
PP
16293/* If section described by INFO was mmapped, munmap it now. */
16294
16295static void
16296munmap_section_buffer (struct dwarf2_section_info *info)
16297{
b315ab21 16298 if (info->map_addr != NULL)
dce234bc
PP
16299 {
16300#ifdef HAVE_MMAP
b315ab21 16301 int res;
9a619af0 16302
b315ab21
TG
16303 res = munmap (info->map_addr, info->map_len);
16304 gdb_assert (res == 0);
dce234bc
PP
16305#else
16306 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16307 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16308#endif
16309 }
16310}
16311
16312/* munmap debug sections for OBJFILE, if necessary. */
16313
16314static void
c1bd65d0 16315dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16316{
16317 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16318 int ix;
16319 struct dwarf2_section_info *section;
9a619af0 16320
16be1145
DE
16321 /* This is sorted according to the order they're defined in to make it easier
16322 to keep in sync. */
dce234bc
PP
16323 munmap_section_buffer (&data->info);
16324 munmap_section_buffer (&data->abbrev);
16325 munmap_section_buffer (&data->line);
16be1145 16326 munmap_section_buffer (&data->loc);
dce234bc 16327 munmap_section_buffer (&data->macinfo);
cf2c3c16 16328 munmap_section_buffer (&data->macro);
16be1145 16329 munmap_section_buffer (&data->str);
dce234bc 16330 munmap_section_buffer (&data->ranges);
dce234bc
PP
16331 munmap_section_buffer (&data->frame);
16332 munmap_section_buffer (&data->eh_frame);
9291a0cd 16333 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16334
16335 for (ix = 0;
16336 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16337 ++ix)
16338 munmap_section_buffer (section);
16339
16340 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16341}
16342
16343\f
ae2de4f8 16344/* The "save gdb-index" command. */
9291a0cd
TT
16345
16346/* The contents of the hash table we create when building the string
16347 table. */
16348struct strtab_entry
16349{
16350 offset_type offset;
16351 const char *str;
16352};
16353
559a7a62
JK
16354/* Hash function for a strtab_entry.
16355
16356 Function is used only during write_hash_table so no index format backward
16357 compatibility is needed. */
b89be57b 16358
9291a0cd
TT
16359static hashval_t
16360hash_strtab_entry (const void *e)
16361{
16362 const struct strtab_entry *entry = e;
559a7a62 16363 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16364}
16365
16366/* Equality function for a strtab_entry. */
b89be57b 16367
9291a0cd
TT
16368static int
16369eq_strtab_entry (const void *a, const void *b)
16370{
16371 const struct strtab_entry *ea = a;
16372 const struct strtab_entry *eb = b;
16373 return !strcmp (ea->str, eb->str);
16374}
16375
16376/* Create a strtab_entry hash table. */
b89be57b 16377
9291a0cd
TT
16378static htab_t
16379create_strtab (void)
16380{
16381 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16382 xfree, xcalloc, xfree);
16383}
16384
16385/* Add a string to the constant pool. Return the string's offset in
16386 host order. */
b89be57b 16387
9291a0cd
TT
16388static offset_type
16389add_string (htab_t table, struct obstack *cpool, const char *str)
16390{
16391 void **slot;
16392 struct strtab_entry entry;
16393 struct strtab_entry *result;
16394
16395 entry.str = str;
16396 slot = htab_find_slot (table, &entry, INSERT);
16397 if (*slot)
16398 result = *slot;
16399 else
16400 {
16401 result = XNEW (struct strtab_entry);
16402 result->offset = obstack_object_size (cpool);
16403 result->str = str;
16404 obstack_grow_str0 (cpool, str);
16405 *slot = result;
16406 }
16407 return result->offset;
16408}
16409
16410/* An entry in the symbol table. */
16411struct symtab_index_entry
16412{
16413 /* The name of the symbol. */
16414 const char *name;
16415 /* The offset of the name in the constant pool. */
16416 offset_type index_offset;
16417 /* A sorted vector of the indices of all the CUs that hold an object
16418 of this name. */
16419 VEC (offset_type) *cu_indices;
16420};
16421
16422/* The symbol table. This is a power-of-2-sized hash table. */
16423struct mapped_symtab
16424{
16425 offset_type n_elements;
16426 offset_type size;
16427 struct symtab_index_entry **data;
16428};
16429
16430/* Hash function for a symtab_index_entry. */
b89be57b 16431
9291a0cd
TT
16432static hashval_t
16433hash_symtab_entry (const void *e)
16434{
16435 const struct symtab_index_entry *entry = e;
16436 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16437 sizeof (offset_type) * VEC_length (offset_type,
16438 entry->cu_indices),
16439 0);
16440}
16441
16442/* Equality function for a symtab_index_entry. */
b89be57b 16443
9291a0cd
TT
16444static int
16445eq_symtab_entry (const void *a, const void *b)
16446{
16447 const struct symtab_index_entry *ea = a;
16448 const struct symtab_index_entry *eb = b;
16449 int len = VEC_length (offset_type, ea->cu_indices);
16450 if (len != VEC_length (offset_type, eb->cu_indices))
16451 return 0;
16452 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16453 VEC_address (offset_type, eb->cu_indices),
16454 sizeof (offset_type) * len);
16455}
16456
16457/* Destroy a symtab_index_entry. */
b89be57b 16458
9291a0cd
TT
16459static void
16460delete_symtab_entry (void *p)
16461{
16462 struct symtab_index_entry *entry = p;
16463 VEC_free (offset_type, entry->cu_indices);
16464 xfree (entry);
16465}
16466
16467/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16468
9291a0cd 16469static htab_t
3876f04e 16470create_symbol_hash_table (void)
9291a0cd
TT
16471{
16472 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16473 delete_symtab_entry, xcalloc, xfree);
16474}
16475
16476/* Create a new mapped symtab object. */
b89be57b 16477
9291a0cd
TT
16478static struct mapped_symtab *
16479create_mapped_symtab (void)
16480{
16481 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16482 symtab->n_elements = 0;
16483 symtab->size = 1024;
16484 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16485 return symtab;
16486}
16487
16488/* Destroy a mapped_symtab. */
b89be57b 16489
9291a0cd
TT
16490static void
16491cleanup_mapped_symtab (void *p)
16492{
16493 struct mapped_symtab *symtab = p;
16494 /* The contents of the array are freed when the other hash table is
16495 destroyed. */
16496 xfree (symtab->data);
16497 xfree (symtab);
16498}
16499
16500/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16501 the slot.
16502
16503 Function is used only during write_hash_table so no index format backward
16504 compatibility is needed. */
b89be57b 16505
9291a0cd
TT
16506static struct symtab_index_entry **
16507find_slot (struct mapped_symtab *symtab, const char *name)
16508{
559a7a62 16509 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16510
16511 index = hash & (symtab->size - 1);
16512 step = ((hash * 17) & (symtab->size - 1)) | 1;
16513
16514 for (;;)
16515 {
16516 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16517 return &symtab->data[index];
16518 index = (index + step) & (symtab->size - 1);
16519 }
16520}
16521
16522/* Expand SYMTAB's hash table. */
b89be57b 16523
9291a0cd
TT
16524static void
16525hash_expand (struct mapped_symtab *symtab)
16526{
16527 offset_type old_size = symtab->size;
16528 offset_type i;
16529 struct symtab_index_entry **old_entries = symtab->data;
16530
16531 symtab->size *= 2;
16532 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16533
16534 for (i = 0; i < old_size; ++i)
16535 {
16536 if (old_entries[i])
16537 {
16538 struct symtab_index_entry **slot = find_slot (symtab,
16539 old_entries[i]->name);
16540 *slot = old_entries[i];
16541 }
16542 }
16543
16544 xfree (old_entries);
16545}
16546
16547/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16548 is the index of the CU in which the symbol appears. */
b89be57b 16549
9291a0cd
TT
16550static void
16551add_index_entry (struct mapped_symtab *symtab, const char *name,
16552 offset_type cu_index)
16553{
16554 struct symtab_index_entry **slot;
16555
16556 ++symtab->n_elements;
16557 if (4 * symtab->n_elements / 3 >= symtab->size)
16558 hash_expand (symtab);
16559
16560 slot = find_slot (symtab, name);
16561 if (!*slot)
16562 {
16563 *slot = XNEW (struct symtab_index_entry);
16564 (*slot)->name = name;
16565 (*slot)->cu_indices = NULL;
16566 }
16567 /* Don't push an index twice. Due to how we add entries we only
16568 have to check the last one. */
16569 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16570 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16571 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16572}
16573
16574/* Add a vector of indices to the constant pool. */
b89be57b 16575
9291a0cd 16576static offset_type
3876f04e 16577add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16578 struct symtab_index_entry *entry)
16579{
16580 void **slot;
16581
3876f04e 16582 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16583 if (!*slot)
16584 {
16585 offset_type len = VEC_length (offset_type, entry->cu_indices);
16586 offset_type val = MAYBE_SWAP (len);
16587 offset_type iter;
16588 int i;
16589
16590 *slot = entry;
16591 entry->index_offset = obstack_object_size (cpool);
16592
16593 obstack_grow (cpool, &val, sizeof (val));
16594 for (i = 0;
16595 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16596 ++i)
16597 {
16598 val = MAYBE_SWAP (iter);
16599 obstack_grow (cpool, &val, sizeof (val));
16600 }
16601 }
16602 else
16603 {
16604 struct symtab_index_entry *old_entry = *slot;
16605 entry->index_offset = old_entry->index_offset;
16606 entry = old_entry;
16607 }
16608 return entry->index_offset;
16609}
16610
16611/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16612 constant pool entries going into the obstack CPOOL. */
b89be57b 16613
9291a0cd
TT
16614static void
16615write_hash_table (struct mapped_symtab *symtab,
16616 struct obstack *output, struct obstack *cpool)
16617{
16618 offset_type i;
3876f04e 16619 htab_t symbol_hash_table;
9291a0cd
TT
16620 htab_t str_table;
16621
3876f04e 16622 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16623 str_table = create_strtab ();
3876f04e 16624
9291a0cd
TT
16625 /* We add all the index vectors to the constant pool first, to
16626 ensure alignment is ok. */
16627 for (i = 0; i < symtab->size; ++i)
16628 {
16629 if (symtab->data[i])
3876f04e 16630 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16631 }
16632
16633 /* Now write out the hash table. */
16634 for (i = 0; i < symtab->size; ++i)
16635 {
16636 offset_type str_off, vec_off;
16637
16638 if (symtab->data[i])
16639 {
16640 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16641 vec_off = symtab->data[i]->index_offset;
16642 }
16643 else
16644 {
16645 /* While 0 is a valid constant pool index, it is not valid
16646 to have 0 for both offsets. */
16647 str_off = 0;
16648 vec_off = 0;
16649 }
16650
16651 str_off = MAYBE_SWAP (str_off);
16652 vec_off = MAYBE_SWAP (vec_off);
16653
16654 obstack_grow (output, &str_off, sizeof (str_off));
16655 obstack_grow (output, &vec_off, sizeof (vec_off));
16656 }
16657
16658 htab_delete (str_table);
3876f04e 16659 htab_delete (symbol_hash_table);
9291a0cd
TT
16660}
16661
0a5429f6
DE
16662/* Struct to map psymtab to CU index in the index file. */
16663struct psymtab_cu_index_map
16664{
16665 struct partial_symtab *psymtab;
16666 unsigned int cu_index;
16667};
16668
16669static hashval_t
16670hash_psymtab_cu_index (const void *item)
16671{
16672 const struct psymtab_cu_index_map *map = item;
16673
16674 return htab_hash_pointer (map->psymtab);
16675}
16676
16677static int
16678eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16679{
16680 const struct psymtab_cu_index_map *lhs = item_lhs;
16681 const struct psymtab_cu_index_map *rhs = item_rhs;
16682
16683 return lhs->psymtab == rhs->psymtab;
16684}
16685
16686/* Helper struct for building the address table. */
16687struct addrmap_index_data
16688{
16689 struct objfile *objfile;
16690 struct obstack *addr_obstack;
16691 htab_t cu_index_htab;
16692
16693 /* Non-zero if the previous_* fields are valid.
16694 We can't write an entry until we see the next entry (since it is only then
16695 that we know the end of the entry). */
16696 int previous_valid;
16697 /* Index of the CU in the table of all CUs in the index file. */
16698 unsigned int previous_cu_index;
0963b4bd 16699 /* Start address of the CU. */
0a5429f6
DE
16700 CORE_ADDR previous_cu_start;
16701};
16702
16703/* Write an address entry to OBSTACK. */
b89be57b 16704
9291a0cd 16705static void
0a5429f6
DE
16706add_address_entry (struct objfile *objfile, struct obstack *obstack,
16707 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16708{
0a5429f6 16709 offset_type cu_index_to_write;
9291a0cd
TT
16710 char addr[8];
16711 CORE_ADDR baseaddr;
16712
16713 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16714
0a5429f6
DE
16715 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16716 obstack_grow (obstack, addr, 8);
16717 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16718 obstack_grow (obstack, addr, 8);
16719 cu_index_to_write = MAYBE_SWAP (cu_index);
16720 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16721}
16722
16723/* Worker function for traversing an addrmap to build the address table. */
16724
16725static int
16726add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16727{
16728 struct addrmap_index_data *data = datap;
16729 struct partial_symtab *pst = obj;
16730 offset_type cu_index;
16731 void **slot;
16732
16733 if (data->previous_valid)
16734 add_address_entry (data->objfile, data->addr_obstack,
16735 data->previous_cu_start, start_addr,
16736 data->previous_cu_index);
16737
16738 data->previous_cu_start = start_addr;
16739 if (pst != NULL)
16740 {
16741 struct psymtab_cu_index_map find_map, *map;
16742 find_map.psymtab = pst;
16743 map = htab_find (data->cu_index_htab, &find_map);
16744 gdb_assert (map != NULL);
16745 data->previous_cu_index = map->cu_index;
16746 data->previous_valid = 1;
16747 }
16748 else
16749 data->previous_valid = 0;
16750
16751 return 0;
16752}
16753
16754/* Write OBJFILE's address map to OBSTACK.
16755 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16756 in the index file. */
16757
16758static void
16759write_address_map (struct objfile *objfile, struct obstack *obstack,
16760 htab_t cu_index_htab)
16761{
16762 struct addrmap_index_data addrmap_index_data;
16763
16764 /* When writing the address table, we have to cope with the fact that
16765 the addrmap iterator only provides the start of a region; we have to
16766 wait until the next invocation to get the start of the next region. */
16767
16768 addrmap_index_data.objfile = objfile;
16769 addrmap_index_data.addr_obstack = obstack;
16770 addrmap_index_data.cu_index_htab = cu_index_htab;
16771 addrmap_index_data.previous_valid = 0;
16772
16773 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16774 &addrmap_index_data);
16775
16776 /* It's highly unlikely the last entry (end address = 0xff...ff)
16777 is valid, but we should still handle it.
16778 The end address is recorded as the start of the next region, but that
16779 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16780 anyway. */
16781 if (addrmap_index_data.previous_valid)
16782 add_address_entry (objfile, obstack,
16783 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16784 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16785}
16786
16787/* Add a list of partial symbols to SYMTAB. */
b89be57b 16788
9291a0cd
TT
16789static void
16790write_psymbols (struct mapped_symtab *symtab,
987d643c 16791 htab_t psyms_seen,
9291a0cd
TT
16792 struct partial_symbol **psymp,
16793 int count,
987d643c
TT
16794 offset_type cu_index,
16795 int is_static)
9291a0cd
TT
16796{
16797 for (; count-- > 0; ++psymp)
16798 {
987d643c
TT
16799 void **slot, *lookup;
16800
9291a0cd
TT
16801 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16802 error (_("Ada is not currently supported by the index"));
987d643c
TT
16803
16804 /* We only want to add a given psymbol once. However, we also
16805 want to account for whether it is global or static. So, we
16806 may add it twice, using slightly different values. */
16807 if (is_static)
16808 {
16809 uintptr_t val = 1 | (uintptr_t) *psymp;
16810
16811 lookup = (void *) val;
16812 }
16813 else
16814 lookup = *psymp;
16815
16816 /* Only add a given psymbol once. */
16817 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16818 if (!*slot)
16819 {
16820 *slot = lookup;
bb2f58dc 16821 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16822 }
9291a0cd
TT
16823 }
16824}
16825
16826/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16827 exception if there is an error. */
b89be57b 16828
9291a0cd
TT
16829static void
16830write_obstack (FILE *file, struct obstack *obstack)
16831{
16832 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16833 file)
16834 != obstack_object_size (obstack))
16835 error (_("couldn't data write to file"));
16836}
16837
16838/* Unlink a file if the argument is not NULL. */
b89be57b 16839
9291a0cd
TT
16840static void
16841unlink_if_set (void *p)
16842{
16843 char **filename = p;
16844 if (*filename)
16845 unlink (*filename);
16846}
16847
1fd400ff
TT
16848/* A helper struct used when iterating over debug_types. */
16849struct signatured_type_index_data
16850{
16851 struct objfile *objfile;
16852 struct mapped_symtab *symtab;
16853 struct obstack *types_list;
987d643c 16854 htab_t psyms_seen;
1fd400ff
TT
16855 int cu_index;
16856};
16857
16858/* A helper function that writes a single signatured_type to an
16859 obstack. */
b89be57b 16860
1fd400ff
TT
16861static int
16862write_one_signatured_type (void **slot, void *d)
16863{
16864 struct signatured_type_index_data *info = d;
16865 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16866 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16867 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16868 gdb_byte val[8];
16869
16870 write_psymbols (info->symtab,
987d643c 16871 info->psyms_seen,
3e43a32a
MS
16872 info->objfile->global_psymbols.list
16873 + psymtab->globals_offset,
987d643c
TT
16874 psymtab->n_global_syms, info->cu_index,
16875 0);
1fd400ff 16876 write_psymbols (info->symtab,
987d643c 16877 info->psyms_seen,
3e43a32a
MS
16878 info->objfile->static_psymbols.list
16879 + psymtab->statics_offset,
987d643c
TT
16880 psymtab->n_static_syms, info->cu_index,
16881 1);
1fd400ff 16882
b3c8eb43 16883 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16884 obstack_grow (info->types_list, val, 8);
16885 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16886 obstack_grow (info->types_list, val, 8);
16887 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16888 obstack_grow (info->types_list, val, 8);
16889
16890 ++info->cu_index;
16891
16892 return 1;
16893}
16894
9291a0cd 16895/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16896
9291a0cd
TT
16897static void
16898write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16899{
16900 struct cleanup *cleanup;
16901 char *filename, *cleanup_filename;
1fd400ff
TT
16902 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16903 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16904 int i;
16905 FILE *out_file;
16906 struct mapped_symtab *symtab;
16907 offset_type val, size_of_contents, total_len;
16908 struct stat st;
16909 char buf[8];
987d643c 16910 htab_t psyms_seen;
0a5429f6
DE
16911 htab_t cu_index_htab;
16912 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16913
b4f2f049 16914 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16915 return;
b4f2f049 16916
9291a0cd
TT
16917 if (dwarf2_per_objfile->using_index)
16918 error (_("Cannot use an index to create the index"));
16919
8b70b953
TT
16920 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16921 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16922
9291a0cd 16923 if (stat (objfile->name, &st) < 0)
7e17e088 16924 perror_with_name (objfile->name);
9291a0cd
TT
16925
16926 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16927 INDEX_SUFFIX, (char *) NULL);
16928 cleanup = make_cleanup (xfree, filename);
16929
16930 out_file = fopen (filename, "wb");
16931 if (!out_file)
16932 error (_("Can't open `%s' for writing"), filename);
16933
16934 cleanup_filename = filename;
16935 make_cleanup (unlink_if_set, &cleanup_filename);
16936
16937 symtab = create_mapped_symtab ();
16938 make_cleanup (cleanup_mapped_symtab, symtab);
16939
16940 obstack_init (&addr_obstack);
16941 make_cleanup_obstack_free (&addr_obstack);
16942
16943 obstack_init (&cu_list);
16944 make_cleanup_obstack_free (&cu_list);
16945
1fd400ff
TT
16946 obstack_init (&types_cu_list);
16947 make_cleanup_obstack_free (&types_cu_list);
16948
987d643c
TT
16949 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16950 NULL, xcalloc, xfree);
96408a79 16951 make_cleanup_htab_delete (psyms_seen);
987d643c 16952
0a5429f6
DE
16953 /* While we're scanning CU's create a table that maps a psymtab pointer
16954 (which is what addrmap records) to its index (which is what is recorded
16955 in the index file). This will later be needed to write the address
16956 table. */
16957 cu_index_htab = htab_create_alloc (100,
16958 hash_psymtab_cu_index,
16959 eq_psymtab_cu_index,
16960 NULL, xcalloc, xfree);
96408a79 16961 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16962 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16963 xmalloc (sizeof (struct psymtab_cu_index_map)
16964 * dwarf2_per_objfile->n_comp_units);
16965 make_cleanup (xfree, psymtab_cu_index_map);
16966
16967 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16968 work here. Also, the debug_types entries do not appear in
16969 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16970 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16971 {
3e43a32a
MS
16972 struct dwarf2_per_cu_data *per_cu
16973 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16974 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16975 gdb_byte val[8];
0a5429f6
DE
16976 struct psymtab_cu_index_map *map;
16977 void **slot;
9291a0cd
TT
16978
16979 write_psymbols (symtab,
987d643c 16980 psyms_seen,
9291a0cd 16981 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16982 psymtab->n_global_syms, i,
16983 0);
9291a0cd 16984 write_psymbols (symtab,
987d643c 16985 psyms_seen,
9291a0cd 16986 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16987 psymtab->n_static_syms, i,
16988 1);
9291a0cd 16989
0a5429f6
DE
16990 map = &psymtab_cu_index_map[i];
16991 map->psymtab = psymtab;
16992 map->cu_index = i;
16993 slot = htab_find_slot (cu_index_htab, map, INSERT);
16994 gdb_assert (slot != NULL);
16995 gdb_assert (*slot == NULL);
16996 *slot = map;
9291a0cd 16997
e254ef6a 16998 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16999 obstack_grow (&cu_list, val, 8);
e254ef6a 17000 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17001 obstack_grow (&cu_list, val, 8);
17002 }
17003
0a5429f6
DE
17004 /* Dump the address map. */
17005 write_address_map (objfile, &addr_obstack, cu_index_htab);
17006
1fd400ff
TT
17007 /* Write out the .debug_type entries, if any. */
17008 if (dwarf2_per_objfile->signatured_types)
17009 {
17010 struct signatured_type_index_data sig_data;
17011
17012 sig_data.objfile = objfile;
17013 sig_data.symtab = symtab;
17014 sig_data.types_list = &types_cu_list;
987d643c 17015 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17016 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17017 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17018 write_one_signatured_type, &sig_data);
17019 }
17020
9291a0cd
TT
17021 obstack_init (&constant_pool);
17022 make_cleanup_obstack_free (&constant_pool);
17023 obstack_init (&symtab_obstack);
17024 make_cleanup_obstack_free (&symtab_obstack);
17025 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17026
17027 obstack_init (&contents);
17028 make_cleanup_obstack_free (&contents);
1fd400ff 17029 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17030 total_len = size_of_contents;
17031
17032 /* The version number. */
559a7a62 17033 val = MAYBE_SWAP (5);
9291a0cd
TT
17034 obstack_grow (&contents, &val, sizeof (val));
17035
17036 /* The offset of the CU list from the start of the file. */
17037 val = MAYBE_SWAP (total_len);
17038 obstack_grow (&contents, &val, sizeof (val));
17039 total_len += obstack_object_size (&cu_list);
17040
1fd400ff
TT
17041 /* The offset of the types CU list from the start of the file. */
17042 val = MAYBE_SWAP (total_len);
17043 obstack_grow (&contents, &val, sizeof (val));
17044 total_len += obstack_object_size (&types_cu_list);
17045
9291a0cd
TT
17046 /* The offset of the address table from the start of the file. */
17047 val = MAYBE_SWAP (total_len);
17048 obstack_grow (&contents, &val, sizeof (val));
17049 total_len += obstack_object_size (&addr_obstack);
17050
17051 /* The offset of the symbol table from the start of the file. */
17052 val = MAYBE_SWAP (total_len);
17053 obstack_grow (&contents, &val, sizeof (val));
17054 total_len += obstack_object_size (&symtab_obstack);
17055
17056 /* The offset of the constant pool from the start of the file. */
17057 val = MAYBE_SWAP (total_len);
17058 obstack_grow (&contents, &val, sizeof (val));
17059 total_len += obstack_object_size (&constant_pool);
17060
17061 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17062
17063 write_obstack (out_file, &contents);
17064 write_obstack (out_file, &cu_list);
1fd400ff 17065 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17066 write_obstack (out_file, &addr_obstack);
17067 write_obstack (out_file, &symtab_obstack);
17068 write_obstack (out_file, &constant_pool);
17069
17070 fclose (out_file);
17071
17072 /* We want to keep the file, so we set cleanup_filename to NULL
17073 here. See unlink_if_set. */
17074 cleanup_filename = NULL;
17075
17076 do_cleanups (cleanup);
17077}
17078
90476074
TT
17079/* Implementation of the `save gdb-index' command.
17080
17081 Note that the file format used by this command is documented in the
17082 GDB manual. Any changes here must be documented there. */
11570e71 17083
9291a0cd
TT
17084static void
17085save_gdb_index_command (char *arg, int from_tty)
17086{
17087 struct objfile *objfile;
17088
17089 if (!arg || !*arg)
96d19272 17090 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17091
17092 ALL_OBJFILES (objfile)
17093 {
17094 struct stat st;
17095
17096 /* If the objfile does not correspond to an actual file, skip it. */
17097 if (stat (objfile->name, &st) < 0)
17098 continue;
17099
17100 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17101 if (dwarf2_per_objfile)
17102 {
17103 volatile struct gdb_exception except;
17104
17105 TRY_CATCH (except, RETURN_MASK_ERROR)
17106 {
17107 write_psymtabs_to_index (objfile, arg);
17108 }
17109 if (except.reason < 0)
17110 exception_fprintf (gdb_stderr, except,
17111 _("Error while writing index for `%s': "),
17112 objfile->name);
17113 }
17114 }
dce234bc
PP
17115}
17116
9291a0cd
TT
17117\f
17118
9eae7c52
TT
17119int dwarf2_always_disassemble;
17120
17121static void
17122show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17123 struct cmd_list_element *c, const char *value)
17124{
3e43a32a
MS
17125 fprintf_filtered (file,
17126 _("Whether to always disassemble "
17127 "DWARF expressions is %s.\n"),
9eae7c52
TT
17128 value);
17129}
17130
900e11f9
JK
17131static void
17132show_check_physname (struct ui_file *file, int from_tty,
17133 struct cmd_list_element *c, const char *value)
17134{
17135 fprintf_filtered (file,
17136 _("Whether to check \"physname\" is %s.\n"),
17137 value);
17138}
17139
6502dd73
DJ
17140void _initialize_dwarf2_read (void);
17141
17142void
17143_initialize_dwarf2_read (void)
17144{
96d19272
JK
17145 struct cmd_list_element *c;
17146
dce234bc 17147 dwarf2_objfile_data_key
c1bd65d0 17148 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17149
1bedd215
AC
17150 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17151Set DWARF 2 specific variables.\n\
17152Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17153 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17154 0/*allow-unknown*/, &maintenance_set_cmdlist);
17155
1bedd215
AC
17156 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17157Show DWARF 2 specific variables\n\
17158Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17159 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17160 0/*allow-unknown*/, &maintenance_show_cmdlist);
17161
17162 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17163 &dwarf2_max_cache_age, _("\
17164Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17165Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17166A higher limit means that cached compilation units will be stored\n\
17167in memory longer, and more total memory will be used. Zero disables\n\
17168caching, which can slow down startup."),
2c5b56ce 17169 NULL,
920d2a44 17170 show_dwarf2_max_cache_age,
2c5b56ce 17171 &set_dwarf2_cmdlist,
ae038cb0 17172 &show_dwarf2_cmdlist);
d97bc12b 17173
9eae7c52
TT
17174 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17175 &dwarf2_always_disassemble, _("\
17176Set whether `info address' always disassembles DWARF expressions."), _("\
17177Show whether `info address' always disassembles DWARF expressions."), _("\
17178When enabled, DWARF expressions are always printed in an assembly-like\n\
17179syntax. When disabled, expressions will be printed in a more\n\
17180conversational style, when possible."),
17181 NULL,
17182 show_dwarf2_always_disassemble,
17183 &set_dwarf2_cmdlist,
17184 &show_dwarf2_cmdlist);
17185
d97bc12b
DE
17186 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17187Set debugging of the dwarf2 DIE reader."), _("\
17188Show debugging of the dwarf2 DIE reader."), _("\
17189When enabled (non-zero), DIEs are dumped after they are read in.\n\
17190The value is the maximum depth to print."),
17191 NULL,
17192 NULL,
17193 &setdebuglist, &showdebuglist);
9291a0cd 17194
900e11f9
JK
17195 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17196Set cross-checking of \"physname\" code against demangler."), _("\
17197Show cross-checking of \"physname\" code against demangler."), _("\
17198When enabled, GDB's internal \"physname\" code is checked against\n\
17199the demangler."),
17200 NULL, show_check_physname,
17201 &setdebuglist, &showdebuglist);
17202
96d19272 17203 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17204 _("\
fc1a9d6e 17205Save a gdb-index file.\n\
11570e71 17206Usage: save gdb-index DIRECTORY"),
96d19272
JK
17207 &save_cmdlist);
17208 set_cmd_completer (c, filename_completer);
6502dd73 17209}
This page took 2.341934 seconds and 4 git commands to generate.