* dwarf2read.c (dwarf2_per_objfile): Add comment.
[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
1228static void read_signatured_type (struct objfile *,
1229 struct signatured_type *type_sig);
1230
c906108c
SS
1231/* memory allocation interface */
1232
7b5a2f43 1233static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1234
f3dd6933 1235static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1236
b60c80d6 1237static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1238
2e276125 1239static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1240 char *, bfd *, struct dwarf2_cu *,
1241 struct dwarf2_section_info *,
1242 int);
2e276125 1243
8e19ed76
PS
1244static int attr_form_is_block (struct attribute *);
1245
3690dd37
JB
1246static int attr_form_is_section_offset (struct attribute *);
1247
1248static int attr_form_is_constant (struct attribute *);
1249
8cf6f0b1
TT
1250static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1251 struct dwarf2_loclist_baton *baton,
1252 struct attribute *attr);
1253
93e7bd98
DJ
1254static void dwarf2_symbol_mark_computed (struct attribute *attr,
1255 struct symbol *sym,
1256 struct dwarf2_cu *cu);
4c2df51b 1257
93311388
DE
1258static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1259 struct abbrev_info *abbrev,
1260 struct dwarf2_cu *cu);
4bb7a0a7 1261
72bf9492
DJ
1262static void free_stack_comp_unit (void *);
1263
72bf9492
DJ
1264static hashval_t partial_die_hash (const void *item);
1265
1266static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1267
ae038cb0 1268static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1269 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1270
1271static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1272 (unsigned int offset, struct objfile *objfile);
ae038cb0 1273
9816fde3
JK
1274static void init_one_comp_unit (struct dwarf2_cu *cu,
1275 struct objfile *objfile);
1276
1277static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1278 struct die_info *comp_unit_die);
93311388 1279
ae038cb0
DJ
1280static void free_one_comp_unit (void *);
1281
1282static void free_cached_comp_units (void *);
1283
1284static void age_cached_comp_units (void);
1285
1286static void free_one_cached_comp_unit (void *);
1287
f792889a
DJ
1288static struct type *set_die_type (struct die_info *, struct type *,
1289 struct dwarf2_cu *);
1c379e20 1290
ae038cb0
DJ
1291static void create_all_comp_units (struct objfile *);
1292
1fd400ff
TT
1293static int create_debug_types_hash_table (struct objfile *objfile);
1294
93311388
DE
1295static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1296 struct objfile *);
10b3939b
DJ
1297
1298static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1299
1300static void dwarf2_add_dependence (struct dwarf2_cu *,
1301 struct dwarf2_per_cu_data *);
1302
ae038cb0
DJ
1303static void dwarf2_mark (struct dwarf2_cu *);
1304
1305static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1306
673bfd45
DE
1307static struct type *get_die_type_at_offset (unsigned int,
1308 struct dwarf2_per_cu_data *per_cu);
1309
f792889a 1310static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1311
9291a0cd
TT
1312static void dwarf2_release_queue (void *dummy);
1313
1314static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1315 struct objfile *objfile);
1316
1317static void process_queue (struct objfile *objfile);
1318
1319static void find_file_and_directory (struct die_info *die,
1320 struct dwarf2_cu *cu,
1321 char **name, char **comp_dir);
1322
1323static char *file_full_name (int file, struct line_header *lh,
1324 const char *comp_dir);
1325
1326static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1327 gdb_byte *info_ptr,
1328 gdb_byte *buffer,
1329 unsigned int buffer_size,
460c1c54 1330 bfd *abfd,
b0df02fd 1331 int is_debug_types_section);
9291a0cd
TT
1332
1333static void init_cu_die_reader (struct die_reader_specs *reader,
1334 struct dwarf2_cu *cu);
1335
673bfd45 1336static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1337
9291a0cd
TT
1338#if WORDS_BIGENDIAN
1339
1340/* Convert VALUE between big- and little-endian. */
1341static offset_type
1342byte_swap (offset_type value)
1343{
1344 offset_type result;
1345
1346 result = (value & 0xff) << 24;
1347 result |= (value & 0xff00) << 8;
1348 result |= (value & 0xff0000) >> 8;
1349 result |= (value & 0xff000000) >> 24;
1350 return result;
1351}
1352
1353#define MAYBE_SWAP(V) byte_swap (V)
1354
1355#else
1356#define MAYBE_SWAP(V) (V)
1357#endif /* WORDS_BIGENDIAN */
1358
1359/* The suffix for an index file. */
1360#define INDEX_SUFFIX ".gdb-index"
1361
3da10d80
KS
1362static const char *dwarf2_physname (char *name, struct die_info *die,
1363 struct dwarf2_cu *cu);
1364
c906108c 1365/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1366 information and return true if we have enough to do something.
1367 NAMES points to the dwarf2 section names, or is NULL if the standard
1368 ELF names are used. */
c906108c
SS
1369
1370int
251d32d9
TG
1371dwarf2_has_info (struct objfile *objfile,
1372 const struct dwarf2_debug_sections *names)
c906108c 1373{
be391dca
TT
1374 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1375 if (!dwarf2_per_objfile)
1376 {
1377 /* Initialize per-objfile state. */
1378 struct dwarf2_per_objfile *data
1379 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1380
be391dca
TT
1381 memset (data, 0, sizeof (*data));
1382 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1383 dwarf2_per_objfile = data;
6502dd73 1384
251d32d9
TG
1385 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1386 (void *) names);
be391dca
TT
1387 dwarf2_per_objfile->objfile = objfile;
1388 }
1389 return (dwarf2_per_objfile->info.asection != NULL
1390 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1391}
1392
251d32d9
TG
1393/* When loading sections, we look either for uncompressed section or for
1394 compressed section names. */
233a11ab
CS
1395
1396static int
251d32d9
TG
1397section_is_p (const char *section_name,
1398 const struct dwarf2_section_names *names)
233a11ab 1399{
251d32d9
TG
1400 if (names->normal != NULL
1401 && strcmp (section_name, names->normal) == 0)
1402 return 1;
1403 if (names->compressed != NULL
1404 && strcmp (section_name, names->compressed) == 0)
1405 return 1;
1406 return 0;
233a11ab
CS
1407}
1408
c906108c
SS
1409/* This function is mapped across the sections and remembers the
1410 offset and size of each of the debugging sections we are interested
1411 in. */
1412
1413static void
251d32d9 1414dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1415{
251d32d9
TG
1416 const struct dwarf2_debug_sections *names;
1417
1418 if (vnames == NULL)
1419 names = &dwarf2_elf_names;
1420 else
1421 names = (const struct dwarf2_debug_sections *) vnames;
1422
1423 if (section_is_p (sectp->name, &names->info))
c906108c 1424 {
dce234bc
PP
1425 dwarf2_per_objfile->info.asection = sectp;
1426 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1427 }
251d32d9 1428 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1429 {
dce234bc
PP
1430 dwarf2_per_objfile->abbrev.asection = sectp;
1431 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1432 }
251d32d9 1433 else if (section_is_p (sectp->name, &names->line))
c906108c 1434 {
dce234bc
PP
1435 dwarf2_per_objfile->line.asection = sectp;
1436 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1437 }
251d32d9 1438 else if (section_is_p (sectp->name, &names->loc))
c906108c 1439 {
dce234bc
PP
1440 dwarf2_per_objfile->loc.asection = sectp;
1441 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1442 }
251d32d9 1443 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1444 {
dce234bc
PP
1445 dwarf2_per_objfile->macinfo.asection = sectp;
1446 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1447 }
cf2c3c16
TT
1448 else if (section_is_p (sectp->name, &names->macro))
1449 {
1450 dwarf2_per_objfile->macro.asection = sectp;
1451 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1452 }
251d32d9 1453 else if (section_is_p (sectp->name, &names->str))
c906108c 1454 {
dce234bc
PP
1455 dwarf2_per_objfile->str.asection = sectp;
1456 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1457 }
251d32d9 1458 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1459 {
dce234bc
PP
1460 dwarf2_per_objfile->frame.asection = sectp;
1461 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1462 }
251d32d9 1463 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1464 {
3799ccc6 1465 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1466
3799ccc6
EZ
1467 if (aflag & SEC_HAS_CONTENTS)
1468 {
dce234bc
PP
1469 dwarf2_per_objfile->eh_frame.asection = sectp;
1470 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1471 }
b6af0555 1472 }
251d32d9 1473 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1474 {
dce234bc
PP
1475 dwarf2_per_objfile->ranges.asection = sectp;
1476 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1477 }
251d32d9 1478 else if (section_is_p (sectp->name, &names->types))
348e048f 1479 {
8b70b953
TT
1480 struct dwarf2_section_info type_section;
1481
1482 memset (&type_section, 0, sizeof (type_section));
1483 type_section.asection = sectp;
1484 type_section.size = bfd_get_section_size (sectp);
1485
1486 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1487 &type_section);
348e048f 1488 }
251d32d9 1489 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1490 {
1491 dwarf2_per_objfile->gdb_index.asection = sectp;
1492 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1493 }
dce234bc 1494
72dca2f5
FR
1495 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1496 && bfd_section_vma (abfd, sectp) == 0)
1497 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1498}
1499
dce234bc
PP
1500/* Decompress a section that was compressed using zlib. Store the
1501 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1502
1503static void
dce234bc
PP
1504zlib_decompress_section (struct objfile *objfile, asection *sectp,
1505 gdb_byte **outbuf, bfd_size_type *outsize)
1506{
1507 bfd *abfd = objfile->obfd;
1508#ifndef HAVE_ZLIB_H
1509 error (_("Support for zlib-compressed DWARF data (from '%s') "
1510 "is disabled in this copy of GDB"),
1511 bfd_get_filename (abfd));
1512#else
1513 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1514 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1515 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1516 bfd_size_type uncompressed_size;
1517 gdb_byte *uncompressed_buffer;
1518 z_stream strm;
1519 int rc;
1520 int header_size = 12;
1521
1522 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1523 || bfd_bread (compressed_buffer,
1524 compressed_size, abfd) != compressed_size)
dce234bc
PP
1525 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1526 bfd_get_filename (abfd));
1527
1528 /* Read the zlib header. In this case, it should be "ZLIB" followed
1529 by the uncompressed section size, 8 bytes in big-endian order. */
1530 if (compressed_size < header_size
1531 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1532 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1533 bfd_get_filename (abfd));
1534 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1535 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1536 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1537 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1538 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1539 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1540 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1541 uncompressed_size += compressed_buffer[11];
1542
1543 /* It is possible the section consists of several compressed
1544 buffers concatenated together, so we uncompress in a loop. */
1545 strm.zalloc = NULL;
1546 strm.zfree = NULL;
1547 strm.opaque = NULL;
1548 strm.avail_in = compressed_size - header_size;
1549 strm.next_in = (Bytef*) compressed_buffer + header_size;
1550 strm.avail_out = uncompressed_size;
1551 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1552 uncompressed_size);
1553 rc = inflateInit (&strm);
1554 while (strm.avail_in > 0)
1555 {
1556 if (rc != Z_OK)
1557 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1558 bfd_get_filename (abfd), rc);
1559 strm.next_out = ((Bytef*) uncompressed_buffer
1560 + (uncompressed_size - strm.avail_out));
1561 rc = inflate (&strm, Z_FINISH);
1562 if (rc != Z_STREAM_END)
1563 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1564 bfd_get_filename (abfd), rc);
1565 rc = inflateReset (&strm);
1566 }
1567 rc = inflateEnd (&strm);
1568 if (rc != Z_OK
1569 || strm.avail_out != 0)
1570 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1571 bfd_get_filename (abfd), rc);
1572
affddf13 1573 do_cleanups (cleanup);
dce234bc
PP
1574 *outbuf = uncompressed_buffer;
1575 *outsize = uncompressed_size;
1576#endif
233a11ab
CS
1577}
1578
9e0ac564
TT
1579/* A helper function that decides whether a section is empty. */
1580
1581static int
1582dwarf2_section_empty_p (struct dwarf2_section_info *info)
1583{
1584 return info->asection == NULL || info->size == 0;
1585}
1586
9cdd5dbd 1587/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1588 OBJFILE, store info about the section into INFO.
1589 If the section is compressed, uncompress it before returning. */
c906108c 1590
dce234bc
PP
1591static void
1592dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1593{
dce234bc
PP
1594 bfd *abfd = objfile->obfd;
1595 asection *sectp = info->asection;
1596 gdb_byte *buf, *retbuf;
1597 unsigned char header[4];
c906108c 1598
be391dca
TT
1599 if (info->readin)
1600 return;
dce234bc 1601 info->buffer = NULL;
b315ab21 1602 info->map_addr = NULL;
be391dca 1603 info->readin = 1;
188dd5d6 1604
9e0ac564 1605 if (dwarf2_section_empty_p (info))
dce234bc 1606 return;
c906108c 1607
dce234bc
PP
1608 /* Check if the file has a 4-byte header indicating compression. */
1609 if (info->size > sizeof (header)
1610 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1611 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1612 {
1613 /* Upon decompression, update the buffer and its size. */
1614 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1615 {
1616 zlib_decompress_section (objfile, sectp, &info->buffer,
1617 &info->size);
1618 return;
1619 }
1620 }
4bdf3d34 1621
dce234bc
PP
1622#ifdef HAVE_MMAP
1623 if (pagesize == 0)
1624 pagesize = getpagesize ();
2e276125 1625
dce234bc
PP
1626 /* Only try to mmap sections which are large enough: we don't want to
1627 waste space due to fragmentation. Also, only try mmap for sections
1628 without relocations. */
1629
1630 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1631 {
b315ab21
TG
1632 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1633 MAP_PRIVATE, sectp->filepos,
1634 &info->map_addr, &info->map_len);
dce234bc 1635
b315ab21 1636 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1637 {
be391dca 1638#if HAVE_POSIX_MADVISE
b315ab21 1639 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1640#endif
dce234bc
PP
1641 return;
1642 }
1643 }
1644#endif
1645
1646 /* If we get here, we are a normal, not-compressed section. */
1647 info->buffer = buf
1648 = obstack_alloc (&objfile->objfile_obstack, info->size);
1649
1650 /* When debugging .o files, we may need to apply relocations; see
1651 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1652 We never compress sections in .o files, so we only need to
1653 try this when the section is not compressed. */
ac8035ab 1654 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1655 if (retbuf != NULL)
1656 {
1657 info->buffer = retbuf;
1658 return;
1659 }
1660
1661 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1662 || bfd_bread (buf, info->size, abfd) != info->size)
1663 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1664 bfd_get_filename (abfd));
1665}
1666
9e0ac564
TT
1667/* A helper function that returns the size of a section in a safe way.
1668 If you are positive that the section has been read before using the
1669 size, then it is safe to refer to the dwarf2_section_info object's
1670 "size" field directly. In other cases, you must call this
1671 function, because for compressed sections the size field is not set
1672 correctly until the section has been read. */
1673
1674static bfd_size_type
1675dwarf2_section_size (struct objfile *objfile,
1676 struct dwarf2_section_info *info)
1677{
1678 if (!info->readin)
1679 dwarf2_read_section (objfile, info);
1680 return info->size;
1681}
1682
dce234bc 1683/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1684 SECTION_NAME. */
af34e669 1685
dce234bc 1686void
3017a003
TG
1687dwarf2_get_section_info (struct objfile *objfile,
1688 enum dwarf2_section_enum sect,
dce234bc
PP
1689 asection **sectp, gdb_byte **bufp,
1690 bfd_size_type *sizep)
1691{
1692 struct dwarf2_per_objfile *data
1693 = objfile_data (objfile, dwarf2_objfile_data_key);
1694 struct dwarf2_section_info *info;
a3b2a86b
TT
1695
1696 /* We may see an objfile without any DWARF, in which case we just
1697 return nothing. */
1698 if (data == NULL)
1699 {
1700 *sectp = NULL;
1701 *bufp = NULL;
1702 *sizep = 0;
1703 return;
1704 }
3017a003
TG
1705 switch (sect)
1706 {
1707 case DWARF2_DEBUG_FRAME:
1708 info = &data->frame;
1709 break;
1710 case DWARF2_EH_FRAME:
1711 info = &data->eh_frame;
1712 break;
1713 default:
1714 gdb_assert_not_reached ("unexpected section");
1715 }
dce234bc 1716
9e0ac564 1717 dwarf2_read_section (objfile, info);
dce234bc
PP
1718
1719 *sectp = info->asection;
1720 *bufp = info->buffer;
1721 *sizep = info->size;
1722}
1723
9291a0cd 1724\f
7b9f3c50
DE
1725/* DWARF quick_symbols_functions support. */
1726
1727/* TUs can share .debug_line entries, and there can be a lot more TUs than
1728 unique line tables, so we maintain a separate table of all .debug_line
1729 derived entries to support the sharing.
1730 All the quick functions need is the list of file names. We discard the
1731 line_header when we're done and don't need to record it here. */
1732struct quick_file_names
1733{
1734 /* The offset in .debug_line of the line table. We hash on this. */
1735 unsigned int offset;
1736
1737 /* The number of entries in file_names, real_names. */
1738 unsigned int num_file_names;
1739
1740 /* The file names from the line table, after being run through
1741 file_full_name. */
1742 const char **file_names;
1743
1744 /* The file names from the line table after being run through
1745 gdb_realpath. These are computed lazily. */
1746 const char **real_names;
1747};
1748
1749/* When using the index (and thus not using psymtabs), each CU has an
1750 object of this type. This is used to hold information needed by
1751 the various "quick" methods. */
1752struct dwarf2_per_cu_quick_data
1753{
1754 /* The file table. This can be NULL if there was no file table
1755 or it's currently not read in.
1756 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1757 struct quick_file_names *file_names;
1758
1759 /* The corresponding symbol table. This is NULL if symbols for this
1760 CU have not yet been read. */
1761 struct symtab *symtab;
1762
1763 /* A temporary mark bit used when iterating over all CUs in
1764 expand_symtabs_matching. */
1765 unsigned int mark : 1;
1766
1767 /* True if we've tried to read the file table and found there isn't one.
1768 There will be no point in trying to read it again next time. */
1769 unsigned int no_file_data : 1;
1770};
1771
1772/* Hash function for a quick_file_names. */
1773
1774static hashval_t
1775hash_file_name_entry (const void *e)
1776{
1777 const struct quick_file_names *file_data = e;
1778
1779 return file_data->offset;
1780}
1781
1782/* Equality function for a quick_file_names. */
1783
1784static int
1785eq_file_name_entry (const void *a, const void *b)
1786{
1787 const struct quick_file_names *ea = a;
1788 const struct quick_file_names *eb = b;
1789
1790 return ea->offset == eb->offset;
1791}
1792
1793/* Delete function for a quick_file_names. */
1794
1795static void
1796delete_file_name_entry (void *e)
1797{
1798 struct quick_file_names *file_data = e;
1799 int i;
1800
1801 for (i = 0; i < file_data->num_file_names; ++i)
1802 {
1803 xfree ((void*) file_data->file_names[i]);
1804 if (file_data->real_names)
1805 xfree ((void*) file_data->real_names[i]);
1806 }
1807
1808 /* The space for the struct itself lives on objfile_obstack,
1809 so we don't free it here. */
1810}
1811
1812/* Create a quick_file_names hash table. */
1813
1814static htab_t
1815create_quick_file_names_table (unsigned int nr_initial_entries)
1816{
1817 return htab_create_alloc (nr_initial_entries,
1818 hash_file_name_entry, eq_file_name_entry,
1819 delete_file_name_entry, xcalloc, xfree);
1820}
9291a0cd 1821
918dd910
JK
1822/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1823 have to be created afterwards. You should call age_cached_comp_units after
1824 processing PER_CU->CU. dw2_setup must have been already called. */
1825
1826static void
1827load_cu (struct dwarf2_per_cu_data *per_cu)
1828{
b0df02fd 1829 if (per_cu->debug_types_section)
8b70b953 1830 read_signatured_type_at_offset (per_cu->objfile,
b0df02fd 1831 per_cu->debug_types_section,
8b70b953 1832 per_cu->offset);
918dd910
JK
1833 else
1834 load_full_comp_unit (per_cu, per_cu->objfile);
1835
1836 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1837
1838 gdb_assert (per_cu->cu != NULL);
1839}
1840
9291a0cd
TT
1841/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1842 this CU came. */
2fdf6df6 1843
9291a0cd
TT
1844static void
1845dw2_do_instantiate_symtab (struct objfile *objfile,
1846 struct dwarf2_per_cu_data *per_cu)
1847{
1848 struct cleanup *back_to;
1849
1850 back_to = make_cleanup (dwarf2_release_queue, NULL);
1851
1852 queue_comp_unit (per_cu, objfile);
1853
918dd910 1854 load_cu (per_cu);
9291a0cd
TT
1855
1856 process_queue (objfile);
1857
1858 /* Age the cache, releasing compilation units that have not
1859 been used recently. */
1860 age_cached_comp_units ();
1861
1862 do_cleanups (back_to);
1863}
1864
1865/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1866 the objfile from which this CU came. Returns the resulting symbol
1867 table. */
2fdf6df6 1868
9291a0cd
TT
1869static struct symtab *
1870dw2_instantiate_symtab (struct objfile *objfile,
1871 struct dwarf2_per_cu_data *per_cu)
1872{
1873 if (!per_cu->v.quick->symtab)
1874 {
1875 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1876 increment_reading_symtab ();
1877 dw2_do_instantiate_symtab (objfile, per_cu);
1878 do_cleanups (back_to);
1879 }
1880 return per_cu->v.quick->symtab;
1881}
1882
1fd400ff 1883/* Return the CU given its index. */
2fdf6df6 1884
1fd400ff
TT
1885static struct dwarf2_per_cu_data *
1886dw2_get_cu (int index)
1887{
1888 if (index >= dwarf2_per_objfile->n_comp_units)
1889 {
1890 index -= dwarf2_per_objfile->n_comp_units;
1891 return dwarf2_per_objfile->type_comp_units[index];
1892 }
1893 return dwarf2_per_objfile->all_comp_units[index];
1894}
1895
9291a0cd
TT
1896/* A helper function that knows how to read a 64-bit value in a way
1897 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1898 otherwise. */
2fdf6df6 1899
9291a0cd
TT
1900static int
1901extract_cu_value (const char *bytes, ULONGEST *result)
1902{
1903 if (sizeof (ULONGEST) < 8)
1904 {
1905 int i;
1906
1907 /* Ignore the upper 4 bytes if they are all zero. */
1908 for (i = 0; i < 4; ++i)
1909 if (bytes[i + 4] != 0)
1910 return 0;
1911
1912 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1913 }
1914 else
1915 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1916 return 1;
1917}
1918
1919/* Read the CU list from the mapped index, and use it to create all
1920 the CU objects for this objfile. Return 0 if something went wrong,
1921 1 if everything went ok. */
2fdf6df6 1922
9291a0cd 1923static int
1fd400ff
TT
1924create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1925 offset_type cu_list_elements)
9291a0cd
TT
1926{
1927 offset_type i;
9291a0cd
TT
1928
1929 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1930 dwarf2_per_objfile->all_comp_units
1931 = obstack_alloc (&objfile->objfile_obstack,
1932 dwarf2_per_objfile->n_comp_units
1933 * sizeof (struct dwarf2_per_cu_data *));
1934
1935 for (i = 0; i < cu_list_elements; i += 2)
1936 {
1937 struct dwarf2_per_cu_data *the_cu;
1938 ULONGEST offset, length;
1939
1940 if (!extract_cu_value (cu_list, &offset)
1941 || !extract_cu_value (cu_list + 8, &length))
1942 return 0;
1943 cu_list += 2 * 8;
1944
1945 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1946 struct dwarf2_per_cu_data);
1947 the_cu->offset = offset;
1948 the_cu->length = length;
1949 the_cu->objfile = objfile;
1950 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1951 struct dwarf2_per_cu_quick_data);
1952 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1953 }
1954
1955 return 1;
1956}
1957
1fd400ff 1958/* Create the signatured type hash table from the index. */
673bfd45 1959
1fd400ff 1960static int
673bfd45 1961create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1962 struct dwarf2_section_info *section,
673bfd45
DE
1963 const gdb_byte *bytes,
1964 offset_type elements)
1fd400ff
TT
1965{
1966 offset_type i;
673bfd45 1967 htab_t sig_types_hash;
1fd400ff
TT
1968
1969 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1970 dwarf2_per_objfile->type_comp_units
1971 = obstack_alloc (&objfile->objfile_obstack,
1972 dwarf2_per_objfile->n_type_comp_units
1973 * sizeof (struct dwarf2_per_cu_data *));
1974
673bfd45 1975 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1976
1977 for (i = 0; i < elements; i += 3)
1978 {
1979 struct signatured_type *type_sig;
1980 ULONGEST offset, type_offset, signature;
1981 void **slot;
1982
1983 if (!extract_cu_value (bytes, &offset)
1984 || !extract_cu_value (bytes + 8, &type_offset))
1985 return 0;
1986 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1987 bytes += 3 * 8;
1988
1989 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1990 struct signatured_type);
1991 type_sig->signature = signature;
1fd400ff 1992 type_sig->type_offset = type_offset;
b0df02fd 1993 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1994 type_sig->per_cu.offset = offset;
1995 type_sig->per_cu.objfile = objfile;
1996 type_sig->per_cu.v.quick
1997 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1998 struct dwarf2_per_cu_quick_data);
1999
673bfd45 2000 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2001 *slot = type_sig;
2002
2003 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2004 }
2005
673bfd45 2006 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2007
2008 return 1;
2009}
2010
9291a0cd
TT
2011/* Read the address map data from the mapped index, and use it to
2012 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2013
9291a0cd
TT
2014static void
2015create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2016{
2017 const gdb_byte *iter, *end;
2018 struct obstack temp_obstack;
2019 struct addrmap *mutable_map;
2020 struct cleanup *cleanup;
2021 CORE_ADDR baseaddr;
2022
2023 obstack_init (&temp_obstack);
2024 cleanup = make_cleanup_obstack_free (&temp_obstack);
2025 mutable_map = addrmap_create_mutable (&temp_obstack);
2026
2027 iter = index->address_table;
2028 end = iter + index->address_table_size;
2029
2030 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2031
2032 while (iter < end)
2033 {
2034 ULONGEST hi, lo, cu_index;
2035 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2036 iter += 8;
2037 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2038 iter += 8;
2039 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2040 iter += 4;
2041
2042 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2043 dw2_get_cu (cu_index));
9291a0cd
TT
2044 }
2045
2046 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2047 &objfile->objfile_obstack);
2048 do_cleanups (cleanup);
2049}
2050
59d7bcaf
JK
2051/* The hash function for strings in the mapped index. This is the same as
2052 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2053 implementation. This is necessary because the hash function is tied to the
2054 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2055 SYMBOL_HASH_NEXT.
2056
2057 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2058
9291a0cd 2059static hashval_t
559a7a62 2060mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2061{
2062 const unsigned char *str = (const unsigned char *) p;
2063 hashval_t r = 0;
2064 unsigned char c;
2065
2066 while ((c = *str++) != 0)
559a7a62
JK
2067 {
2068 if (index_version >= 5)
2069 c = tolower (c);
2070 r = r * 67 + c - 113;
2071 }
9291a0cd
TT
2072
2073 return r;
2074}
2075
2076/* Find a slot in the mapped index INDEX for the object named NAME.
2077 If NAME is found, set *VEC_OUT to point to the CU vector in the
2078 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2079
9291a0cd
TT
2080static int
2081find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2082 offset_type **vec_out)
2083{
0cf03b49
JK
2084 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2085 offset_type hash;
9291a0cd 2086 offset_type slot, step;
559a7a62 2087 int (*cmp) (const char *, const char *);
9291a0cd 2088
0cf03b49
JK
2089 if (current_language->la_language == language_cplus
2090 || current_language->la_language == language_java
2091 || current_language->la_language == language_fortran)
2092 {
2093 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2094 not contain any. */
2095 const char *paren = strchr (name, '(');
2096
2097 if (paren)
2098 {
2099 char *dup;
2100
2101 dup = xmalloc (paren - name + 1);
2102 memcpy (dup, name, paren - name);
2103 dup[paren - name] = 0;
2104
2105 make_cleanup (xfree, dup);
2106 name = dup;
2107 }
2108 }
2109
559a7a62
JK
2110 /* Index version 4 did not support case insensitive searches. But the
2111 indexes for case insensitive languages are built in lowercase, therefore
2112 simulate our NAME being searched is also lowercased. */
2113 hash = mapped_index_string_hash ((index->version == 4
2114 && case_sensitivity == case_sensitive_off
2115 ? 5 : index->version),
2116 name);
2117
3876f04e
DE
2118 slot = hash & (index->symbol_table_slots - 1);
2119 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2120 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2121
2122 for (;;)
2123 {
2124 /* Convert a slot number to an offset into the table. */
2125 offset_type i = 2 * slot;
2126 const char *str;
3876f04e 2127 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2128 {
2129 do_cleanups (back_to);
2130 return 0;
2131 }
9291a0cd 2132
3876f04e 2133 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2134 if (!cmp (name, str))
9291a0cd
TT
2135 {
2136 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2137 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2138 do_cleanups (back_to);
9291a0cd
TT
2139 return 1;
2140 }
2141
3876f04e 2142 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2143 }
2144}
2145
2146/* Read the index file. If everything went ok, initialize the "quick"
2147 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2148
9291a0cd
TT
2149static int
2150dwarf2_read_index (struct objfile *objfile)
2151{
9291a0cd
TT
2152 char *addr;
2153 struct mapped_index *map;
b3b272e1 2154 offset_type *metadata;
ac0b195c
KW
2155 const gdb_byte *cu_list;
2156 const gdb_byte *types_list = NULL;
2157 offset_type version, cu_list_elements;
2158 offset_type types_list_elements = 0;
1fd400ff 2159 int i;
9291a0cd 2160
9e0ac564 2161 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2162 return 0;
82430852
JK
2163
2164 /* Older elfutils strip versions could keep the section in the main
2165 executable while splitting it for the separate debug info file. */
2166 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2167 & SEC_HAS_CONTENTS) == 0)
2168 return 0;
2169
9291a0cd
TT
2170 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2171
2172 addr = dwarf2_per_objfile->gdb_index.buffer;
2173 /* Version check. */
1fd400ff 2174 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2175 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2176 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2177 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2178 indices. Index version 4 uses a different hash function than index
2179 version 5 and later. */
831adc1f 2180 if (version < 4)
9291a0cd 2181 return 0;
594e8718
JK
2182 /* Indexes with higher version than the one supported by GDB may be no
2183 longer backward compatible. */
559a7a62 2184 if (version > 5)
594e8718 2185 return 0;
9291a0cd
TT
2186
2187 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2188 map->version = version;
b3b272e1 2189 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2190
2191 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2192
2193 i = 0;
2194 cu_list = addr + MAYBE_SWAP (metadata[i]);
2195 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2196 / 8);
1fd400ff
TT
2197 ++i;
2198
987d643c
TT
2199 types_list = addr + MAYBE_SWAP (metadata[i]);
2200 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2201 - MAYBE_SWAP (metadata[i]))
2202 / 8);
2203 ++i;
1fd400ff
TT
2204
2205 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2206 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2207 - MAYBE_SWAP (metadata[i]));
2208 ++i;
2209
3876f04e
DE
2210 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2211 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2212 - MAYBE_SWAP (metadata[i]))
2213 / (2 * sizeof (offset_type)));
1fd400ff 2214 ++i;
9291a0cd 2215
1fd400ff
TT
2216 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2217
2218 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2219 return 0;
2220
8b70b953
TT
2221 if (types_list_elements)
2222 {
2223 struct dwarf2_section_info *section;
2224
2225 /* We can only handle a single .debug_types when we have an
2226 index. */
2227 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2228 return 0;
2229
2230 section = VEC_index (dwarf2_section_info_def,
2231 dwarf2_per_objfile->types, 0);
2232
2233 if (!create_signatured_type_table_from_index (objfile, section,
2234 types_list,
2235 types_list_elements))
2236 return 0;
2237 }
9291a0cd
TT
2238
2239 create_addrmap_from_index (objfile, map);
2240
2241 dwarf2_per_objfile->index_table = map;
2242 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2243 dwarf2_per_objfile->quick_file_names_table =
2244 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2245
2246 return 1;
2247}
2248
2249/* A helper for the "quick" functions which sets the global
2250 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2251
9291a0cd
TT
2252static void
2253dw2_setup (struct objfile *objfile)
2254{
2255 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2256 gdb_assert (dwarf2_per_objfile);
2257}
2258
2259/* A helper for the "quick" functions which attempts to read the line
2260 table for THIS_CU. */
2fdf6df6 2261
7b9f3c50
DE
2262static struct quick_file_names *
2263dw2_get_file_names (struct objfile *objfile,
2264 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2265{
2266 bfd *abfd = objfile->obfd;
7b9f3c50 2267 struct line_header *lh;
9291a0cd
TT
2268 struct attribute *attr;
2269 struct cleanup *cleanups;
2270 struct die_info *comp_unit_die;
36374493 2271 struct dwarf2_section_info* sec;
adabb602 2272 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2273 int has_children, i;
2274 struct dwarf2_cu cu;
2275 unsigned int bytes_read, buffer_size;
2276 struct die_reader_specs reader_specs;
2277 char *name, *comp_dir;
7b9f3c50
DE
2278 void **slot;
2279 struct quick_file_names *qfn;
2280 unsigned int line_offset;
9291a0cd 2281
7b9f3c50
DE
2282 if (this_cu->v.quick->file_names != NULL)
2283 return this_cu->v.quick->file_names;
2284 /* If we know there is no line data, no point in looking again. */
2285 if (this_cu->v.quick->no_file_data)
2286 return NULL;
9291a0cd 2287
9816fde3 2288 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2289 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2290
b0df02fd
DE
2291 if (this_cu->debug_types_section)
2292 sec = this_cu->debug_types_section;
36374493
DE
2293 else
2294 sec = &dwarf2_per_objfile->info;
2295 dwarf2_read_section (objfile, sec);
2296 buffer_size = sec->size;
2297 buffer = sec->buffer;
9291a0cd 2298 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2299
2300 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2301 buffer, buffer_size,
460c1c54 2302 abfd,
b0df02fd 2303 this_cu->debug_types_section != NULL);
9291a0cd 2304
6caca83c
CC
2305 /* Skip dummy compilation units. */
2306 if (info_ptr >= buffer + buffer_size
2307 || peek_abbrev_code (abfd, info_ptr) == 0)
2308 {
2309 do_cleanups (cleanups);
2310 return NULL;
2311 }
2312
9291a0cd
TT
2313 this_cu->cu = &cu;
2314 cu.per_cu = this_cu;
2315
2316 dwarf2_read_abbrevs (abfd, &cu);
2317 make_cleanup (dwarf2_free_abbrev_table, &cu);
2318
9291a0cd 2319 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2320 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2321 &has_children);
9291a0cd 2322
7b9f3c50
DE
2323 lh = NULL;
2324 slot = NULL;
2325 line_offset = 0;
9291a0cd
TT
2326 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2327 if (attr)
2328 {
7b9f3c50
DE
2329 struct quick_file_names find_entry;
2330
2331 line_offset = DW_UNSND (attr);
2332
2333 /* We may have already read in this line header (TU line header sharing).
2334 If we have we're done. */
2335 find_entry.offset = line_offset;
2336 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2337 &find_entry, INSERT);
2338 if (*slot != NULL)
2339 {
2340 do_cleanups (cleanups);
2341 this_cu->v.quick->file_names = *slot;
2342 return *slot;
2343 }
2344
9291a0cd
TT
2345 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2346 }
2347 if (lh == NULL)
2348 {
2349 do_cleanups (cleanups);
7b9f3c50
DE
2350 this_cu->v.quick->no_file_data = 1;
2351 return NULL;
9291a0cd
TT
2352 }
2353
7b9f3c50
DE
2354 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2355 qfn->offset = line_offset;
2356 gdb_assert (slot != NULL);
2357 *slot = qfn;
9291a0cd 2358
7b9f3c50 2359 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2360
7b9f3c50
DE
2361 qfn->num_file_names = lh->num_file_names;
2362 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2363 lh->num_file_names * sizeof (char *));
9291a0cd 2364 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2365 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2366 qfn->real_names = NULL;
9291a0cd 2367
7b9f3c50 2368 free_line_header (lh);
9291a0cd 2369 do_cleanups (cleanups);
7b9f3c50
DE
2370
2371 this_cu->v.quick->file_names = qfn;
2372 return qfn;
9291a0cd
TT
2373}
2374
2375/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2376 real path for a given file name from the line table. */
2fdf6df6 2377
9291a0cd 2378static const char *
7b9f3c50
DE
2379dw2_get_real_path (struct objfile *objfile,
2380 struct quick_file_names *qfn, int index)
9291a0cd 2381{
7b9f3c50
DE
2382 if (qfn->real_names == NULL)
2383 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2384 qfn->num_file_names, sizeof (char *));
9291a0cd 2385
7b9f3c50
DE
2386 if (qfn->real_names[index] == NULL)
2387 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2388
7b9f3c50 2389 return qfn->real_names[index];
9291a0cd
TT
2390}
2391
2392static struct symtab *
2393dw2_find_last_source_symtab (struct objfile *objfile)
2394{
2395 int index;
ae2de4f8 2396
9291a0cd
TT
2397 dw2_setup (objfile);
2398 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2399 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2400}
2401
7b9f3c50
DE
2402/* Traversal function for dw2_forget_cached_source_info. */
2403
2404static int
2405dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2406{
7b9f3c50 2407 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2408
7b9f3c50 2409 if (file_data->real_names)
9291a0cd 2410 {
7b9f3c50 2411 int i;
9291a0cd 2412
7b9f3c50 2413 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2414 {
7b9f3c50
DE
2415 xfree ((void*) file_data->real_names[i]);
2416 file_data->real_names[i] = NULL;
9291a0cd
TT
2417 }
2418 }
7b9f3c50
DE
2419
2420 return 1;
2421}
2422
2423static void
2424dw2_forget_cached_source_info (struct objfile *objfile)
2425{
2426 dw2_setup (objfile);
2427
2428 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2429 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2430}
2431
f8eba3c6
TT
2432/* Helper function for dw2_map_symtabs_matching_filename that expands
2433 the symtabs and calls the iterator. */
2434
2435static int
2436dw2_map_expand_apply (struct objfile *objfile,
2437 struct dwarf2_per_cu_data *per_cu,
2438 const char *name,
2439 const char *full_path, const char *real_path,
2440 int (*callback) (struct symtab *, void *),
2441 void *data)
2442{
2443 struct symtab *last_made = objfile->symtabs;
2444
2445 /* Don't visit already-expanded CUs. */
2446 if (per_cu->v.quick->symtab)
2447 return 0;
2448
2449 /* This may expand more than one symtab, and we want to iterate over
2450 all of them. */
2451 dw2_instantiate_symtab (objfile, per_cu);
2452
2453 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2454 objfile->symtabs, last_made);
2455}
2456
2457/* Implementation of the map_symtabs_matching_filename method. */
2458
9291a0cd 2459static int
f8eba3c6
TT
2460dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2461 const char *full_path, const char *real_path,
2462 int (*callback) (struct symtab *, void *),
2463 void *data)
9291a0cd
TT
2464{
2465 int i;
c011a4f4
DE
2466 const char *name_basename = lbasename (name);
2467 int check_basename = name_basename == name;
9291a0cd
TT
2468 struct dwarf2_per_cu_data *base_cu = NULL;
2469
2470 dw2_setup (objfile);
ae2de4f8 2471
1fd400ff
TT
2472 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2473 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2474 {
2475 int j;
e254ef6a 2476 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2477 struct quick_file_names *file_data;
9291a0cd 2478
3d7bb9d9 2479 /* We only need to look at symtabs not already expanded. */
e254ef6a 2480 if (per_cu->v.quick->symtab)
9291a0cd
TT
2481 continue;
2482
7b9f3c50
DE
2483 file_data = dw2_get_file_names (objfile, per_cu);
2484 if (file_data == NULL)
9291a0cd
TT
2485 continue;
2486
7b9f3c50 2487 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2488 {
7b9f3c50 2489 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2490
2491 if (FILENAME_CMP (name, this_name) == 0)
2492 {
f8eba3c6
TT
2493 if (dw2_map_expand_apply (objfile, per_cu,
2494 name, full_path, real_path,
2495 callback, data))
2496 return 1;
9291a0cd
TT
2497 }
2498
2499 if (check_basename && ! base_cu
2500 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2501 base_cu = per_cu;
9291a0cd 2502
c011a4f4
DE
2503 /* Before we invoke realpath, which can get expensive when many
2504 files are involved, do a quick comparison of the basenames. */
2505 if (! basenames_may_differ
2506 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2507 continue;
2508
9291a0cd
TT
2509 if (full_path != NULL)
2510 {
7b9f3c50
DE
2511 const char *this_real_name = dw2_get_real_path (objfile,
2512 file_data, j);
9291a0cd 2513
7b9f3c50
DE
2514 if (this_real_name != NULL
2515 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2516 {
f8eba3c6
TT
2517 if (dw2_map_expand_apply (objfile, per_cu,
2518 name, full_path, real_path,
2519 callback, data))
2520 return 1;
9291a0cd
TT
2521 }
2522 }
2523
2524 if (real_path != NULL)
2525 {
7b9f3c50
DE
2526 const char *this_real_name = dw2_get_real_path (objfile,
2527 file_data, j);
9291a0cd 2528
7b9f3c50
DE
2529 if (this_real_name != NULL
2530 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2531 {
f8eba3c6
TT
2532 if (dw2_map_expand_apply (objfile, per_cu,
2533 name, full_path, real_path,
2534 callback, data))
2535 return 1;
9291a0cd
TT
2536 }
2537 }
2538 }
2539 }
2540
2541 if (base_cu)
2542 {
f8eba3c6
TT
2543 if (dw2_map_expand_apply (objfile, base_cu,
2544 name, full_path, real_path,
2545 callback, data))
2546 return 1;
9291a0cd
TT
2547 }
2548
2549 return 0;
2550}
2551
2552static struct symtab *
2553dw2_lookup_symbol (struct objfile *objfile, int block_index,
2554 const char *name, domain_enum domain)
2555{
774b6a14 2556 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2557 instead. */
2558 return NULL;
2559}
2560
2561/* A helper function that expands all symtabs that hold an object
2562 named NAME. */
2fdf6df6 2563
9291a0cd
TT
2564static void
2565dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2566{
2567 dw2_setup (objfile);
2568
ae2de4f8 2569 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2570 if (dwarf2_per_objfile->index_table)
2571 {
2572 offset_type *vec;
2573
2574 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2575 name, &vec))
2576 {
2577 offset_type i, len = MAYBE_SWAP (*vec);
2578 for (i = 0; i < len; ++i)
2579 {
2580 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2581 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2582
e254ef6a 2583 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2584 }
2585 }
2586 }
2587}
2588
774b6a14
TT
2589static void
2590dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2591 enum block_enum block_kind, const char *name,
774b6a14 2592 domain_enum domain)
9291a0cd 2593{
774b6a14 2594 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2595}
2596
2597static void
2598dw2_print_stats (struct objfile *objfile)
2599{
2600 int i, count;
2601
2602 dw2_setup (objfile);
2603 count = 0;
1fd400ff
TT
2604 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2605 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2606 {
e254ef6a 2607 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2608
e254ef6a 2609 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2610 ++count;
2611 }
2612 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2613}
2614
2615static void
2616dw2_dump (struct objfile *objfile)
2617{
2618 /* Nothing worth printing. */
2619}
2620
2621static void
2622dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2623 struct section_offsets *delta)
2624{
2625 /* There's nothing to relocate here. */
2626}
2627
2628static void
2629dw2_expand_symtabs_for_function (struct objfile *objfile,
2630 const char *func_name)
2631{
2632 dw2_do_expand_symtabs_matching (objfile, func_name);
2633}
2634
2635static void
2636dw2_expand_all_symtabs (struct objfile *objfile)
2637{
2638 int i;
2639
2640 dw2_setup (objfile);
1fd400ff
TT
2641
2642 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2643 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2644 {
e254ef6a 2645 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2646
e254ef6a 2647 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2648 }
2649}
2650
2651static void
2652dw2_expand_symtabs_with_filename (struct objfile *objfile,
2653 const char *filename)
2654{
2655 int i;
2656
2657 dw2_setup (objfile);
d4637a04
DE
2658
2659 /* We don't need to consider type units here.
2660 This is only called for examining code, e.g. expand_line_sal.
2661 There can be an order of magnitude (or more) more type units
2662 than comp units, and we avoid them if we can. */
2663
2664 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2665 {
2666 int j;
e254ef6a 2667 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2668 struct quick_file_names *file_data;
9291a0cd 2669
3d7bb9d9 2670 /* We only need to look at symtabs not already expanded. */
e254ef6a 2671 if (per_cu->v.quick->symtab)
9291a0cd
TT
2672 continue;
2673
7b9f3c50
DE
2674 file_data = dw2_get_file_names (objfile, per_cu);
2675 if (file_data == NULL)
9291a0cd
TT
2676 continue;
2677
7b9f3c50 2678 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2679 {
7b9f3c50 2680 const char *this_name = file_data->file_names[j];
1ef75ecc 2681 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2682 {
e254ef6a 2683 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2684 break;
2685 }
2686 }
2687 }
2688}
2689
dd786858 2690static const char *
9291a0cd
TT
2691dw2_find_symbol_file (struct objfile *objfile, const char *name)
2692{
e254ef6a 2693 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2694 offset_type *vec;
7b9f3c50 2695 struct quick_file_names *file_data;
9291a0cd
TT
2696
2697 dw2_setup (objfile);
2698
ae2de4f8 2699 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2700 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2701 {
2702 struct symtab *s;
2703
2704 ALL_OBJFILE_SYMTABS (objfile, s)
2705 if (s->primary)
2706 {
2707 struct blockvector *bv = BLOCKVECTOR (s);
2708 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2709 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2710
2711 if (sym)
2712 return sym->symtab->filename;
2713 }
2714 return NULL;
2715 }
9291a0cd
TT
2716
2717 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2718 name, &vec))
2719 return NULL;
2720
2721 /* Note that this just looks at the very first one named NAME -- but
2722 actually we are looking for a function. find_main_filename
2723 should be rewritten so that it doesn't require a custom hook. It
2724 could just use the ordinary symbol tables. */
2725 /* vec[0] is the length, which must always be >0. */
e254ef6a 2726 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2727
7b9f3c50
DE
2728 file_data = dw2_get_file_names (objfile, per_cu);
2729 if (file_data == NULL)
9291a0cd
TT
2730 return NULL;
2731
7b9f3c50 2732 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2733}
2734
2735static void
40658b94
PH
2736dw2_map_matching_symbols (const char * name, domain_enum namespace,
2737 struct objfile *objfile, int global,
2738 int (*callback) (struct block *,
2739 struct symbol *, void *),
2edb89d3
JK
2740 void *data, symbol_compare_ftype *match,
2741 symbol_compare_ftype *ordered_compare)
9291a0cd 2742{
40658b94 2743 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2744 current language is Ada for a non-Ada objfile using GNU index. As Ada
2745 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2746}
2747
2748static void
f8eba3c6
TT
2749dw2_expand_symtabs_matching
2750 (struct objfile *objfile,
2751 int (*file_matcher) (const char *, void *),
2752 int (*name_matcher) (const struct language_defn *, const char *, void *),
2753 enum search_domain kind,
2754 void *data)
9291a0cd
TT
2755{
2756 int i;
2757 offset_type iter;
4b5246aa 2758 struct mapped_index *index;
9291a0cd
TT
2759
2760 dw2_setup (objfile);
ae2de4f8
DE
2761
2762 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2763 if (!dwarf2_per_objfile->index_table)
2764 return;
4b5246aa 2765 index = dwarf2_per_objfile->index_table;
9291a0cd 2766
7b08b9eb
JK
2767 if (file_matcher != NULL)
2768 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2769 + dwarf2_per_objfile->n_type_comp_units); ++i)
2770 {
2771 int j;
2772 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2773 struct quick_file_names *file_data;
2774
2775 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2776
2777 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2778 if (per_cu->v.quick->symtab)
2779 continue;
2780
2781 file_data = dw2_get_file_names (objfile, per_cu);
2782 if (file_data == NULL)
2783 continue;
2784
2785 for (j = 0; j < file_data->num_file_names; ++j)
2786 {
2787 if (file_matcher (file_data->file_names[j], data))
2788 {
2789 per_cu->v.quick->mark = 1;
2790 break;
2791 }
2792 }
2793 }
9291a0cd 2794
3876f04e 2795 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2796 {
2797 offset_type idx = 2 * iter;
2798 const char *name;
2799 offset_type *vec, vec_len, vec_idx;
2800
3876f04e 2801 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2802 continue;
2803
3876f04e 2804 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2805
f8eba3c6 2806 if (! (*name_matcher) (current_language, name, data))
9291a0cd
TT
2807 continue;
2808
2809 /* The name was matched, now expand corresponding CUs that were
2810 marked. */
4b5246aa 2811 vec = (offset_type *) (index->constant_pool
3876f04e 2812 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2813 vec_len = MAYBE_SWAP (vec[0]);
2814 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2815 {
e254ef6a 2816 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2817
e254ef6a 2818 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2819 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2820 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2821 }
2822 }
2823}
2824
2825static struct symtab *
2826dw2_find_pc_sect_symtab (struct objfile *objfile,
2827 struct minimal_symbol *msymbol,
2828 CORE_ADDR pc,
2829 struct obj_section *section,
2830 int warn_if_readin)
2831{
2832 struct dwarf2_per_cu_data *data;
2833
2834 dw2_setup (objfile);
2835
2836 if (!objfile->psymtabs_addrmap)
2837 return NULL;
2838
2839 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2840 if (!data)
2841 return NULL;
2842
2843 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2844 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2845 paddress (get_objfile_arch (objfile), pc));
2846
2847 return dw2_instantiate_symtab (objfile, data);
2848}
2849
9291a0cd 2850static void
44b13c5a 2851dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2852 void *data, int need_fullname)
9291a0cd
TT
2853{
2854 int i;
2855
2856 dw2_setup (objfile);
ae2de4f8 2857
1fd400ff
TT
2858 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2859 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2860 {
2861 int j;
e254ef6a 2862 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2863 struct quick_file_names *file_data;
9291a0cd 2864
3d7bb9d9 2865 /* We only need to look at symtabs not already expanded. */
e254ef6a 2866 if (per_cu->v.quick->symtab)
9291a0cd
TT
2867 continue;
2868
7b9f3c50
DE
2869 file_data = dw2_get_file_names (objfile, per_cu);
2870 if (file_data == NULL)
9291a0cd
TT
2871 continue;
2872
7b9f3c50 2873 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2874 {
74e2f255
DE
2875 const char *this_real_name;
2876
2877 if (need_fullname)
2878 this_real_name = dw2_get_real_path (objfile, file_data, j);
2879 else
2880 this_real_name = NULL;
7b9f3c50 2881 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2882 }
2883 }
2884}
2885
2886static int
2887dw2_has_symbols (struct objfile *objfile)
2888{
2889 return 1;
2890}
2891
2892const struct quick_symbol_functions dwarf2_gdb_index_functions =
2893{
2894 dw2_has_symbols,
2895 dw2_find_last_source_symtab,
2896 dw2_forget_cached_source_info,
f8eba3c6 2897 dw2_map_symtabs_matching_filename,
9291a0cd 2898 dw2_lookup_symbol,
774b6a14 2899 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2900 dw2_print_stats,
2901 dw2_dump,
2902 dw2_relocate,
2903 dw2_expand_symtabs_for_function,
2904 dw2_expand_all_symtabs,
2905 dw2_expand_symtabs_with_filename,
2906 dw2_find_symbol_file,
40658b94 2907 dw2_map_matching_symbols,
9291a0cd
TT
2908 dw2_expand_symtabs_matching,
2909 dw2_find_pc_sect_symtab,
9291a0cd
TT
2910 dw2_map_symbol_filenames
2911};
2912
2913/* Initialize for reading DWARF for this objfile. Return 0 if this
2914 file will use psymtabs, or 1 if using the GNU index. */
2915
2916int
2917dwarf2_initialize_objfile (struct objfile *objfile)
2918{
2919 /* If we're about to read full symbols, don't bother with the
2920 indices. In this case we also don't care if some other debug
2921 format is making psymtabs, because they are all about to be
2922 expanded anyway. */
2923 if ((objfile->flags & OBJF_READNOW))
2924 {
2925 int i;
2926
2927 dwarf2_per_objfile->using_index = 1;
2928 create_all_comp_units (objfile);
1fd400ff 2929 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2930 dwarf2_per_objfile->quick_file_names_table =
2931 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2932
1fd400ff
TT
2933 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2934 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2935 {
e254ef6a 2936 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2937
e254ef6a
DE
2938 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2939 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2940 }
2941
2942 /* Return 1 so that gdb sees the "quick" functions. However,
2943 these functions will be no-ops because we will have expanded
2944 all symtabs. */
2945 return 1;
2946 }
2947
2948 if (dwarf2_read_index (objfile))
2949 return 1;
2950
9291a0cd
TT
2951 return 0;
2952}
2953
2954\f
2955
dce234bc
PP
2956/* Build a partial symbol table. */
2957
2958void
f29dff0a 2959dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2960{
f29dff0a 2961 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2962 {
2963 init_psymbol_list (objfile, 1024);
2964 }
2965
d146bf1e 2966 dwarf2_build_psymtabs_hard (objfile);
c906108c 2967}
c906108c 2968
45452591
DE
2969/* Return TRUE if OFFSET is within CU_HEADER. */
2970
2971static inline int
2972offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2973{
2974 unsigned int bottom = cu_header->offset;
2975 unsigned int top = (cu_header->offset
2976 + cu_header->length
2977 + cu_header->initial_length_size);
9a619af0 2978
45452591
DE
2979 return (offset >= bottom && offset < top);
2980}
2981
93311388
DE
2982/* Read in the comp unit header information from the debug_info at info_ptr.
2983 NOTE: This leaves members offset, first_die_offset to be filled in
2984 by the caller. */
107d2387 2985
fe1b8b76 2986static gdb_byte *
107d2387 2987read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2988 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2989{
2990 int signed_addr;
891d2f0b 2991 unsigned int bytes_read;
c764a876
DE
2992
2993 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2994 cu_header->initial_length_size = bytes_read;
2995 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2996 info_ptr += bytes_read;
107d2387
AC
2997 cu_header->version = read_2_bytes (abfd, info_ptr);
2998 info_ptr += 2;
613e1657 2999 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 3000 &bytes_read);
613e1657 3001 info_ptr += bytes_read;
107d2387
AC
3002 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3003 info_ptr += 1;
3004 signed_addr = bfd_get_sign_extend_vma (abfd);
3005 if (signed_addr < 0)
8e65ff28 3006 internal_error (__FILE__, __LINE__,
e2e0b3e5 3007 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3008 cu_header->signed_addr_p = signed_addr;
c764a876 3009
107d2387
AC
3010 return info_ptr;
3011}
3012
adabb602
DE
3013/* Read in a CU header and perform some basic error checking. */
3014
fe1b8b76
JB
3015static gdb_byte *
3016partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 3017 gdb_byte *buffer, unsigned int buffer_size,
b0df02fd 3018 bfd *abfd, int is_debug_types_section)
72bf9492 3019{
fe1b8b76 3020 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 3021
adabb602
DE
3022 header->offset = beg_of_comp_unit - buffer;
3023
72bf9492
DJ
3024 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3025
460c1c54
CC
3026 /* If we're reading a type unit, skip over the signature and
3027 type_offset fields. */
b0df02fd 3028 if (is_debug_types_section)
460c1c54
CC
3029 info_ptr += 8 /*signature*/ + header->offset_size;
3030
adabb602
DE
3031 header->first_die_offset = info_ptr - beg_of_comp_unit;
3032
2dc7f7b3 3033 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 3034 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
3035 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3036 bfd_get_filename (abfd));
72bf9492 3037
9e0ac564
TT
3038 if (header->abbrev_offset
3039 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3040 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
3041 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3042 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 3043 (long) header->abbrev_offset,
93311388 3044 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3045 bfd_get_filename (abfd));
3046
3047 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 3048 > buffer + buffer_size)
8a3fe4f8
AC
3049 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3050 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3051 (long) header->length,
93311388 3052 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3053 bfd_get_filename (abfd));
3054
3055 return info_ptr;
3056}
3057
348e048f
DE
3058/* Read in the types comp unit header information from .debug_types entry at
3059 types_ptr. The result is a pointer to one past the end of the header. */
3060
3061static gdb_byte *
3062read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3063 struct dwarf2_section_info *section,
348e048f
DE
3064 ULONGEST *signature,
3065 gdb_byte *types_ptr, bfd *abfd)
3066{
348e048f
DE
3067 gdb_byte *initial_types_ptr = types_ptr;
3068
8b70b953
TT
3069 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3070 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3071
3072 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3073
3074 *signature = read_8_bytes (abfd, types_ptr);
3075 types_ptr += 8;
3076 types_ptr += cu_header->offset_size;
3077 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3078
3079 return types_ptr;
3080}
3081
aaa75496
JB
3082/* Allocate a new partial symtab for file named NAME and mark this new
3083 partial symtab as being an include of PST. */
3084
3085static void
3086dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3087 struct objfile *objfile)
3088{
3089 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3090
3091 subpst->section_offsets = pst->section_offsets;
3092 subpst->textlow = 0;
3093 subpst->texthigh = 0;
3094
3095 subpst->dependencies = (struct partial_symtab **)
3096 obstack_alloc (&objfile->objfile_obstack,
3097 sizeof (struct partial_symtab *));
3098 subpst->dependencies[0] = pst;
3099 subpst->number_of_dependencies = 1;
3100
3101 subpst->globals_offset = 0;
3102 subpst->n_global_syms = 0;
3103 subpst->statics_offset = 0;
3104 subpst->n_static_syms = 0;
3105 subpst->symtab = NULL;
3106 subpst->read_symtab = pst->read_symtab;
3107 subpst->readin = 0;
3108
3109 /* No private part is necessary for include psymtabs. This property
3110 can be used to differentiate between such include psymtabs and
10b3939b 3111 the regular ones. */
58a9656e 3112 subpst->read_symtab_private = NULL;
aaa75496
JB
3113}
3114
3115/* Read the Line Number Program data and extract the list of files
3116 included by the source file represented by PST. Build an include
d85a05f0 3117 partial symtab for each of these included files. */
aaa75496
JB
3118
3119static void
3120dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3121 struct die_info *die,
aaa75496
JB
3122 struct partial_symtab *pst)
3123{
3124 struct objfile *objfile = cu->objfile;
3125 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3126 struct line_header *lh = NULL;
3127 struct attribute *attr;
aaa75496 3128
d85a05f0
DJ
3129 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3130 if (attr)
3131 {
3132 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3133
d85a05f0
DJ
3134 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3135 }
aaa75496
JB
3136 if (lh == NULL)
3137 return; /* No linetable, so no includes. */
3138
c6da4cef
DE
3139 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3140 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3141
3142 free_line_header (lh);
3143}
3144
348e048f
DE
3145static hashval_t
3146hash_type_signature (const void *item)
3147{
3148 const struct signatured_type *type_sig = item;
9a619af0 3149
348e048f
DE
3150 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3151 return type_sig->signature;
3152}
3153
3154static int
3155eq_type_signature (const void *item_lhs, const void *item_rhs)
3156{
3157 const struct signatured_type *lhs = item_lhs;
3158 const struct signatured_type *rhs = item_rhs;
9a619af0 3159
348e048f
DE
3160 return lhs->signature == rhs->signature;
3161}
3162
1fd400ff
TT
3163/* Allocate a hash table for signatured types. */
3164
3165static htab_t
673bfd45 3166allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3167{
3168 return htab_create_alloc_ex (41,
3169 hash_type_signature,
3170 eq_type_signature,
3171 NULL,
3172 &objfile->objfile_obstack,
3173 hashtab_obstack_allocate,
3174 dummy_obstack_deallocate);
3175}
3176
3177/* A helper function to add a signatured type CU to a list. */
3178
3179static int
3180add_signatured_type_cu_to_list (void **slot, void *datum)
3181{
3182 struct signatured_type *sigt = *slot;
3183 struct dwarf2_per_cu_data ***datap = datum;
3184
3185 **datap = &sigt->per_cu;
3186 ++*datap;
3187
3188 return 1;
3189}
3190
348e048f
DE
3191/* Create the hash table of all entries in the .debug_types section.
3192 The result is zero if there is an error (e.g. missing .debug_types section),
3193 otherwise non-zero. */
3194
3195static int
3196create_debug_types_hash_table (struct objfile *objfile)
3197{
8b70b953 3198 htab_t types_htab = NULL;
1fd400ff 3199 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3200 int ix;
3201 struct dwarf2_section_info *section;
348e048f 3202
8b70b953 3203 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3204 {
3205 dwarf2_per_objfile->signatured_types = NULL;
3206 return 0;
3207 }
3208
8b70b953
TT
3209 for (ix = 0;
3210 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3211 ix, section);
3212 ++ix)
3213 {
3214 gdb_byte *info_ptr, *end_ptr;
348e048f 3215
8b70b953
TT
3216 dwarf2_read_section (objfile, section);
3217 info_ptr = section->buffer;
348e048f 3218
8b70b953
TT
3219 if (info_ptr == NULL)
3220 continue;
348e048f 3221
8b70b953
TT
3222 if (types_htab == NULL)
3223 types_htab = allocate_signatured_type_table (objfile);
348e048f 3224
8b70b953
TT
3225 if (dwarf2_die_debug)
3226 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3227
3228 end_ptr = info_ptr + section->size;
3229 while (info_ptr < end_ptr)
3230 {
3231 unsigned int offset;
3232 unsigned int offset_size;
3233 unsigned int type_offset;
3234 unsigned int length, initial_length_size;
3235 unsigned short version;
3236 ULONGEST signature;
3237 struct signatured_type *type_sig;
3238 void **slot;
3239 gdb_byte *ptr = info_ptr;
348e048f 3240
8b70b953 3241 offset = ptr - section->buffer;
348e048f 3242
8b70b953
TT
3243 /* We need to read the type's signature in order to build the hash
3244 table, but we don't need to read anything else just yet. */
348e048f 3245
8b70b953
TT
3246 /* Sanity check to ensure entire cu is present. */
3247 length = read_initial_length (objfile->obfd, ptr,
3248 &initial_length_size);
3249 if (ptr + length + initial_length_size > end_ptr)
3250 {
3251 complaint (&symfile_complaints,
3252 _("debug type entry runs off end "
3253 "of `.debug_types' section, ignored"));
3254 break;
3255 }
348e048f 3256
8b70b953
TT
3257 offset_size = initial_length_size == 4 ? 4 : 8;
3258 ptr += initial_length_size;
3259 version = bfd_get_16 (objfile->obfd, ptr);
3260 ptr += 2;
3261 ptr += offset_size; /* abbrev offset */
3262 ptr += 1; /* address size */
3263 signature = bfd_get_64 (objfile->obfd, ptr);
3264 ptr += 8;
3265 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3266 ptr += offset_size;
6caca83c
CC
3267
3268 /* Skip dummy type units. */
3269 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3270 {
3271 info_ptr = info_ptr + initial_length_size + length;
3272 continue;
3273 }
8b70b953
TT
3274
3275 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3276 memset (type_sig, 0, sizeof (*type_sig));
3277 type_sig->signature = signature;
3278 type_sig->type_offset = type_offset;
3279 type_sig->per_cu.objfile = objfile;
b0df02fd 3280 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3281 type_sig->per_cu.offset = offset;
3282
3283 slot = htab_find_slot (types_htab, type_sig, INSERT);
3284 gdb_assert (slot != NULL);
3285 if (*slot != NULL)
3286 {
3287 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3288
8b70b953
TT
3289 complaint (&symfile_complaints,
3290 _("debug type entry at offset 0x%x is duplicate to the "
3291 "entry at offset 0x%x, signature 0x%s"),
3292 offset, dup_sig->per_cu.offset,
3293 phex (signature, sizeof (signature)));
3294 gdb_assert (signature == dup_sig->signature);
3295 }
3296 *slot = type_sig;
348e048f 3297
8b70b953
TT
3298 if (dwarf2_die_debug)
3299 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3300 offset, phex (signature, sizeof (signature)));
348e048f 3301
8b70b953
TT
3302 info_ptr = info_ptr + initial_length_size + length;
3303 }
348e048f
DE
3304 }
3305
3306 dwarf2_per_objfile->signatured_types = types_htab;
3307
1fd400ff
TT
3308 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3309 dwarf2_per_objfile->type_comp_units
3310 = obstack_alloc (&objfile->objfile_obstack,
3311 dwarf2_per_objfile->n_type_comp_units
3312 * sizeof (struct dwarf2_per_cu_data *));
3313 iter = &dwarf2_per_objfile->type_comp_units[0];
3314 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3315 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3316 == dwarf2_per_objfile->n_type_comp_units);
3317
348e048f
DE
3318 return 1;
3319}
3320
3321/* Lookup a signature based type.
3322 Returns NULL if SIG is not present in the table. */
3323
3324static struct signatured_type *
3325lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3326{
3327 struct signatured_type find_entry, *entry;
3328
3329 if (dwarf2_per_objfile->signatured_types == NULL)
3330 {
3331 complaint (&symfile_complaints,
55f1336d 3332 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3333 return 0;
3334 }
3335
3336 find_entry.signature = sig;
3337 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3338 return entry;
3339}
3340
d85a05f0
DJ
3341/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3342
3343static void
3344init_cu_die_reader (struct die_reader_specs *reader,
3345 struct dwarf2_cu *cu)
3346{
3347 reader->abfd = cu->objfile->obfd;
3348 reader->cu = cu;
b0df02fd 3349 if (cu->per_cu->debug_types_section)
be391dca 3350 {
b0df02fd
DE
3351 gdb_assert (cu->per_cu->debug_types_section->readin);
3352 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3353 }
d85a05f0 3354 else
be391dca
TT
3355 {
3356 gdb_assert (dwarf2_per_objfile->info.readin);
3357 reader->buffer = dwarf2_per_objfile->info.buffer;
3358 }
d85a05f0
DJ
3359}
3360
3361/* Find the base address of the compilation unit for range lists and
3362 location lists. It will normally be specified by DW_AT_low_pc.
3363 In DWARF-3 draft 4, the base address could be overridden by
3364 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3365 compilation units with discontinuous ranges. */
3366
3367static void
3368dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3369{
3370 struct attribute *attr;
3371
3372 cu->base_known = 0;
3373 cu->base_address = 0;
3374
3375 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3376 if (attr)
3377 {
3378 cu->base_address = DW_ADDR (attr);
3379 cu->base_known = 1;
3380 }
3381 else
3382 {
3383 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3384 if (attr)
3385 {
3386 cu->base_address = DW_ADDR (attr);
3387 cu->base_known = 1;
3388 }
3389 }
3390}
3391
348e048f
DE
3392/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3393 to combine the common parts.
93311388 3394 Process a compilation unit for a psymtab.
348e048f
DE
3395 BUFFER is a pointer to the beginning of the dwarf section buffer,
3396 either .debug_info or debug_types.
93311388
DE
3397 INFO_PTR is a pointer to the start of the CU.
3398 Returns a pointer to the next CU. */
aaa75496 3399
93311388
DE
3400static gdb_byte *
3401process_psymtab_comp_unit (struct objfile *objfile,
3402 struct dwarf2_per_cu_data *this_cu,
3403 gdb_byte *buffer, gdb_byte *info_ptr,
3404 unsigned int buffer_size)
c906108c 3405{
c906108c 3406 bfd *abfd = objfile->obfd;
93311388 3407 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3408 struct die_info *comp_unit_die;
c906108c 3409 struct partial_symtab *pst;
5734ee8b 3410 CORE_ADDR baseaddr;
93311388
DE
3411 struct cleanup *back_to_inner;
3412 struct dwarf2_cu cu;
d85a05f0
DJ
3413 int has_children, has_pc_info;
3414 struct attribute *attr;
d85a05f0
DJ
3415 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3416 struct die_reader_specs reader_specs;
3e2a0cee 3417 const char *filename;
c906108c 3418
9816fde3 3419 init_one_comp_unit (&cu, objfile);
93311388 3420 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3421
93311388
DE
3422 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3423 buffer, buffer_size,
460c1c54 3424 abfd,
b0df02fd 3425 this_cu->debug_types_section != NULL);
10b3939b 3426
6caca83c
CC
3427 /* Skip dummy compilation units. */
3428 if (info_ptr >= buffer + buffer_size
3429 || peek_abbrev_code (abfd, info_ptr) == 0)
3430 {
3431 info_ptr = (beg_of_comp_unit + cu.header.length
3432 + cu.header.initial_length_size);
3433 do_cleanups (back_to_inner);
3434 return info_ptr;
3435 }
3436
93311388 3437 cu.list_in_scope = &file_symbols;
af703f96 3438
328c9494
DJ
3439 /* If this compilation unit was already read in, free the
3440 cached copy in order to read it in again. This is
3441 necessary because we skipped some symbols when we first
3442 read in the compilation unit (see load_partial_dies).
3443 This problem could be avoided, but the benefit is
3444 unclear. */
3445 if (this_cu->cu != NULL)
3446 free_one_cached_comp_unit (this_cu->cu);
3447
3448 /* Note that this is a pointer to our stack frame, being
3449 added to a global data structure. It will be cleaned up
3450 in free_stack_comp_unit when we finish with this
3451 compilation unit. */
3452 this_cu->cu = &cu;
d85a05f0
DJ
3453 cu.per_cu = this_cu;
3454
93311388
DE
3455 /* Read the abbrevs for this compilation unit into a table. */
3456 dwarf2_read_abbrevs (abfd, &cu);
3457 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3458
93311388 3459 /* Read the compilation unit die. */
d85a05f0
DJ
3460 init_cu_die_reader (&reader_specs, &cu);
3461 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3462 &has_children);
93311388 3463
b0df02fd 3464 if (this_cu->debug_types_section)
348e048f 3465 {
b3c8eb43
JK
3466 /* LENGTH has not been set yet for type units. */
3467 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3468 this_cu->length = cu.header.length + cu.header.initial_length_size;
3469 }
d85a05f0 3470 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3471 {
93311388
DE
3472 info_ptr = (beg_of_comp_unit + cu.header.length
3473 + cu.header.initial_length_size);
3474 do_cleanups (back_to_inner);
3475 return info_ptr;
3476 }
72bf9492 3477
9816fde3 3478 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3479
93311388 3480 /* Allocate a new partial symbol table structure. */
d85a05f0 3481 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3482 if (attr == NULL || !DW_STRING (attr))
3483 filename = "";
3484 else
3485 filename = DW_STRING (attr);
93311388 3486 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3487 filename,
93311388
DE
3488 /* TEXTLOW and TEXTHIGH are set below. */
3489 0,
3490 objfile->global_psymbols.next,
3491 objfile->static_psymbols.next);
9750bca9 3492 pst->psymtabs_addrmap_supported = 1;
72bf9492 3493
d85a05f0
DJ
3494 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3495 if (attr != NULL)
3496 pst->dirname = DW_STRING (attr);
72bf9492 3497
e38df1d0 3498 pst->read_symtab_private = this_cu;
72bf9492 3499
93311388 3500 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3501
0963b4bd 3502 /* Store the function that reads in the rest of the symbol table. */
93311388 3503 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3504
9291a0cd 3505 this_cu->v.psymtab = pst;
c906108c 3506
d85a05f0
DJ
3507 dwarf2_find_base_address (comp_unit_die, &cu);
3508
93311388
DE
3509 /* Possibly set the default values of LOWPC and HIGHPC from
3510 `DW_AT_ranges'. */
d85a05f0
DJ
3511 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3512 &best_highpc, &cu, pst);
3513 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3514 /* Store the contiguous range if it is not empty; it can be empty for
3515 CUs with no code. */
3516 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3517 best_lowpc + baseaddr,
3518 best_highpc + baseaddr - 1, pst);
93311388
DE
3519
3520 /* Check if comp unit has_children.
3521 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3522 If not, there's no more debug_info for this comp unit. */
d85a05f0 3523 if (has_children)
93311388
DE
3524 {
3525 struct partial_die_info *first_die;
3526 CORE_ADDR lowpc, highpc;
31ffec48 3527
93311388
DE
3528 lowpc = ((CORE_ADDR) -1);
3529 highpc = ((CORE_ADDR) 0);
c906108c 3530
93311388 3531 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3532
93311388 3533 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3534 ! has_pc_info, &cu);
57c22c6c 3535
93311388
DE
3536 /* If we didn't find a lowpc, set it to highpc to avoid
3537 complaints from `maint check'. */
3538 if (lowpc == ((CORE_ADDR) -1))
3539 lowpc = highpc;
10b3939b 3540
93311388
DE
3541 /* If the compilation unit didn't have an explicit address range,
3542 then use the information extracted from its child dies. */
d85a05f0 3543 if (! has_pc_info)
93311388 3544 {
d85a05f0
DJ
3545 best_lowpc = lowpc;
3546 best_highpc = highpc;
93311388
DE
3547 }
3548 }
d85a05f0
DJ
3549 pst->textlow = best_lowpc + baseaddr;
3550 pst->texthigh = best_highpc + baseaddr;
c906108c 3551
93311388
DE
3552 pst->n_global_syms = objfile->global_psymbols.next -
3553 (objfile->global_psymbols.list + pst->globals_offset);
3554 pst->n_static_syms = objfile->static_psymbols.next -
3555 (objfile->static_psymbols.list + pst->statics_offset);
3556 sort_pst_symbols (pst);
c906108c 3557
93311388
DE
3558 info_ptr = (beg_of_comp_unit + cu.header.length
3559 + cu.header.initial_length_size);
ae038cb0 3560
b0df02fd 3561 if (this_cu->debug_types_section)
348e048f
DE
3562 {
3563 /* It's not clear we want to do anything with stmt lists here.
3564 Waiting to see what gcc ultimately does. */
3565 }
d85a05f0 3566 else
93311388
DE
3567 {
3568 /* Get the list of files included in the current compilation unit,
3569 and build a psymtab for each of them. */
d85a05f0 3570 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3571 }
ae038cb0 3572
93311388 3573 do_cleanups (back_to_inner);
ae038cb0 3574
93311388
DE
3575 return info_ptr;
3576}
ff013f42 3577
348e048f
DE
3578/* Traversal function for htab_traverse_noresize.
3579 Process one .debug_types comp-unit. */
3580
3581static int
3582process_type_comp_unit (void **slot, void *info)
3583{
3584 struct signatured_type *entry = (struct signatured_type *) *slot;
3585 struct objfile *objfile = (struct objfile *) info;
3586 struct dwarf2_per_cu_data *this_cu;
3587
3588 this_cu = &entry->per_cu;
348e048f 3589
b0df02fd 3590 gdb_assert (this_cu->debug_types_section->readin);
348e048f 3591 process_psymtab_comp_unit (objfile, this_cu,
b0df02fd
DE
3592 this_cu->debug_types_section->buffer,
3593 (this_cu->debug_types_section->buffer
8b70b953 3594 + this_cu->offset),
b0df02fd 3595 this_cu->debug_types_section->size);
348e048f
DE
3596
3597 return 1;
3598}
3599
3600/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3601 Build partial symbol tables for the .debug_types comp-units. */
3602
3603static void
3604build_type_psymtabs (struct objfile *objfile)
3605{
3606 if (! create_debug_types_hash_table (objfile))
3607 return;
3608
3609 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3610 process_type_comp_unit, objfile);
3611}
3612
60606b2c
TT
3613/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3614
3615static void
3616psymtabs_addrmap_cleanup (void *o)
3617{
3618 struct objfile *objfile = o;
ec61707d 3619
60606b2c
TT
3620 objfile->psymtabs_addrmap = NULL;
3621}
3622
93311388
DE
3623/* Build the partial symbol table by doing a quick pass through the
3624 .debug_info and .debug_abbrev sections. */
72bf9492 3625
93311388 3626static void
c67a9c90 3627dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3628{
93311388 3629 gdb_byte *info_ptr;
60606b2c
TT
3630 struct cleanup *back_to, *addrmap_cleanup;
3631 struct obstack temp_obstack;
93311388 3632
98bfdba5
PA
3633 dwarf2_per_objfile->reading_partial_symbols = 1;
3634
be391dca 3635 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3636 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3637
93311388
DE
3638 /* Any cached compilation units will be linked by the per-objfile
3639 read_in_chain. Make sure to free them when we're done. */
3640 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3641
348e048f
DE
3642 build_type_psymtabs (objfile);
3643
93311388 3644 create_all_comp_units (objfile);
c906108c 3645
60606b2c
TT
3646 /* Create a temporary address map on a temporary obstack. We later
3647 copy this to the final obstack. */
3648 obstack_init (&temp_obstack);
3649 make_cleanup_obstack_free (&temp_obstack);
3650 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3651 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3652
93311388
DE
3653 /* Since the objects we're extracting from .debug_info vary in
3654 length, only the individual functions to extract them (like
3655 read_comp_unit_head and load_partial_die) can really know whether
3656 the buffer is large enough to hold another complete object.
c906108c 3657
93311388
DE
3658 At the moment, they don't actually check that. If .debug_info
3659 holds just one extra byte after the last compilation unit's dies,
3660 then read_comp_unit_head will happily read off the end of the
3661 buffer. read_partial_die is similarly casual. Those functions
3662 should be fixed.
c906108c 3663
93311388
DE
3664 For this loop condition, simply checking whether there's any data
3665 left at all should be sufficient. */
c906108c 3666
93311388
DE
3667 while (info_ptr < (dwarf2_per_objfile->info.buffer
3668 + dwarf2_per_objfile->info.size))
3669 {
3670 struct dwarf2_per_cu_data *this_cu;
dd373385 3671
3e43a32a
MS
3672 this_cu = dwarf2_find_comp_unit (info_ptr
3673 - dwarf2_per_objfile->info.buffer,
93311388 3674 objfile);
aaa75496 3675
93311388
DE
3676 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3677 dwarf2_per_objfile->info.buffer,
3678 info_ptr,
3679 dwarf2_per_objfile->info.size);
c906108c 3680 }
ff013f42
JK
3681
3682 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3683 &objfile->objfile_obstack);
60606b2c 3684 discard_cleanups (addrmap_cleanup);
ff013f42 3685
ae038cb0
DJ
3686 do_cleanups (back_to);
3687}
3688
93311388 3689/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3690
3691static void
93311388
DE
3692load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3693 struct objfile *objfile)
ae038cb0
DJ
3694{
3695 bfd *abfd = objfile->obfd;
adabb602 3696 gdb_byte *info_ptr;
d85a05f0 3697 struct die_info *comp_unit_die;
ae038cb0 3698 struct dwarf2_cu *cu;
1d9ec526 3699 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3700 int has_children;
3701 struct die_reader_specs reader_specs;
98bfdba5 3702 int read_cu = 0;
ae038cb0 3703
b0df02fd 3704 gdb_assert (! this_cu->debug_types_section);
348e048f 3705
be391dca 3706 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3707 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3708
98bfdba5
PA
3709 if (this_cu->cu == NULL)
3710 {
9816fde3
JK
3711 cu = xmalloc (sizeof (*cu));
3712 init_one_comp_unit (cu, objfile);
ae038cb0 3713
98bfdba5 3714 read_cu = 1;
ae038cb0 3715
98bfdba5
PA
3716 /* If an error occurs while loading, release our storage. */
3717 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3718
98bfdba5
PA
3719 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3720 dwarf2_per_objfile->info.buffer,
3721 dwarf2_per_objfile->info.size,
460c1c54 3722 abfd, 0);
ae038cb0 3723
6caca83c
CC
3724 /* Skip dummy compilation units. */
3725 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3726 + dwarf2_per_objfile->info.size)
3727 || peek_abbrev_code (abfd, info_ptr) == 0)
3728 {
3729 do_cleanups (free_cu_cleanup);
3730 return;
3731 }
3732
98bfdba5
PA
3733 /* Link this compilation unit into the compilation unit tree. */
3734 this_cu->cu = cu;
3735 cu->per_cu = this_cu;
98bfdba5
PA
3736
3737 /* Link this CU into read_in_chain. */
3738 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3739 dwarf2_per_objfile->read_in_chain = this_cu;
3740 }
3741 else
3742 {
3743 cu = this_cu->cu;
3744 info_ptr += cu->header.first_die_offset;
3745 }
ae038cb0
DJ
3746
3747 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3748 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3749 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3750 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3751
3752 /* Read the compilation unit die. */
d85a05f0
DJ
3753 init_cu_die_reader (&reader_specs, cu);
3754 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3755 &has_children);
ae038cb0 3756
9816fde3 3757 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3758
ae038cb0
DJ
3759 /* Check if comp unit has_children.
3760 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3761 If not, there's no more debug_info for this comp unit. */
d85a05f0 3762 if (has_children)
93311388 3763 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3764
98bfdba5
PA
3765 do_cleanups (free_abbrevs_cleanup);
3766
3767 if (read_cu)
3768 {
3769 /* We've successfully allocated this compilation unit. Let our
3770 caller clean it up when finished with it. */
3771 discard_cleanups (free_cu_cleanup);
3772 }
ae038cb0
DJ
3773}
3774
9cdd5dbd
DE
3775/* Create a list of all compilation units in OBJFILE.
3776 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3777
3778static void
3779create_all_comp_units (struct objfile *objfile)
3780{
3781 int n_allocated;
3782 int n_comp_units;
3783 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3784 gdb_byte *info_ptr;
3785
3786 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3787 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3788
3789 n_comp_units = 0;
3790 n_allocated = 10;
3791 all_comp_units = xmalloc (n_allocated
3792 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3793
3e43a32a
MS
3794 while (info_ptr < dwarf2_per_objfile->info.buffer
3795 + dwarf2_per_objfile->info.size)
ae038cb0 3796 {
c764a876 3797 unsigned int length, initial_length_size;
ae038cb0 3798 struct dwarf2_per_cu_data *this_cu;
c764a876 3799 unsigned int offset;
ae038cb0 3800
dce234bc 3801 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3802
3803 /* Read just enough information to find out where the next
3804 compilation unit is. */
c764a876
DE
3805 length = read_initial_length (objfile->obfd, info_ptr,
3806 &initial_length_size);
ae038cb0
DJ
3807
3808 /* Save the compilation unit for later lookup. */
3809 this_cu = obstack_alloc (&objfile->objfile_obstack,
3810 sizeof (struct dwarf2_per_cu_data));
3811 memset (this_cu, 0, sizeof (*this_cu));
3812 this_cu->offset = offset;
c764a876 3813 this_cu->length = length + initial_length_size;
9291a0cd 3814 this_cu->objfile = objfile;
ae038cb0
DJ
3815
3816 if (n_comp_units == n_allocated)
3817 {
3818 n_allocated *= 2;
3819 all_comp_units = xrealloc (all_comp_units,
3820 n_allocated
3821 * sizeof (struct dwarf2_per_cu_data *));
3822 }
3823 all_comp_units[n_comp_units++] = this_cu;
3824
3825 info_ptr = info_ptr + this_cu->length;
3826 }
3827
3828 dwarf2_per_objfile->all_comp_units
3829 = obstack_alloc (&objfile->objfile_obstack,
3830 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3831 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3832 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3833 xfree (all_comp_units);
3834 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3835}
3836
5734ee8b
DJ
3837/* Process all loaded DIEs for compilation unit CU, starting at
3838 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3839 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3840 DW_AT_ranges). If NEED_PC is set, then this function will set
3841 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3842 and record the covered ranges in the addrmap. */
c906108c 3843
72bf9492
DJ
3844static void
3845scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3846 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3847{
72bf9492 3848 struct partial_die_info *pdi;
c906108c 3849
91c24f0a
DC
3850 /* Now, march along the PDI's, descending into ones which have
3851 interesting children but skipping the children of the other ones,
3852 until we reach the end of the compilation unit. */
c906108c 3853
72bf9492 3854 pdi = first_die;
91c24f0a 3855
72bf9492
DJ
3856 while (pdi != NULL)
3857 {
3858 fixup_partial_die (pdi, cu);
c906108c 3859
f55ee35c 3860 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3861 children, so we need to look at them. Ditto for anonymous
3862 enums. */
933c6fe4 3863
72bf9492 3864 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3865 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3866 {
72bf9492 3867 switch (pdi->tag)
c906108c
SS
3868 {
3869 case DW_TAG_subprogram:
5734ee8b 3870 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3871 break;
72929c62 3872 case DW_TAG_constant:
c906108c
SS
3873 case DW_TAG_variable:
3874 case DW_TAG_typedef:
91c24f0a 3875 case DW_TAG_union_type:
72bf9492 3876 if (!pdi->is_declaration)
63d06c5c 3877 {
72bf9492 3878 add_partial_symbol (pdi, cu);
63d06c5c
DC
3879 }
3880 break;
c906108c 3881 case DW_TAG_class_type:
680b30c7 3882 case DW_TAG_interface_type:
c906108c 3883 case DW_TAG_structure_type:
72bf9492 3884 if (!pdi->is_declaration)
c906108c 3885 {
72bf9492 3886 add_partial_symbol (pdi, cu);
c906108c
SS
3887 }
3888 break;
91c24f0a 3889 case DW_TAG_enumeration_type:
72bf9492
DJ
3890 if (!pdi->is_declaration)
3891 add_partial_enumeration (pdi, cu);
c906108c
SS
3892 break;
3893 case DW_TAG_base_type:
a02abb62 3894 case DW_TAG_subrange_type:
c906108c 3895 /* File scope base type definitions are added to the partial
c5aa993b 3896 symbol table. */
72bf9492 3897 add_partial_symbol (pdi, cu);
c906108c 3898 break;
d9fa45fe 3899 case DW_TAG_namespace:
5734ee8b 3900 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3901 break;
5d7cb8df
JK
3902 case DW_TAG_module:
3903 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3904 break;
c906108c
SS
3905 default:
3906 break;
3907 }
3908 }
3909
72bf9492
DJ
3910 /* If the die has a sibling, skip to the sibling. */
3911
3912 pdi = pdi->die_sibling;
3913 }
3914}
3915
3916/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3917
72bf9492 3918 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3919 name is concatenated with "::" and the partial DIE's name. For
3920 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3921 Enumerators are an exception; they use the scope of their parent
3922 enumeration type, i.e. the name of the enumeration type is not
3923 prepended to the enumerator.
91c24f0a 3924
72bf9492
DJ
3925 There are two complexities. One is DW_AT_specification; in this
3926 case "parent" means the parent of the target of the specification,
3927 instead of the direct parent of the DIE. The other is compilers
3928 which do not emit DW_TAG_namespace; in this case we try to guess
3929 the fully qualified name of structure types from their members'
3930 linkage names. This must be done using the DIE's children rather
3931 than the children of any DW_AT_specification target. We only need
3932 to do this for structures at the top level, i.e. if the target of
3933 any DW_AT_specification (if any; otherwise the DIE itself) does not
3934 have a parent. */
3935
3936/* Compute the scope prefix associated with PDI's parent, in
3937 compilation unit CU. The result will be allocated on CU's
3938 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3939 field. NULL is returned if no prefix is necessary. */
3940static char *
3941partial_die_parent_scope (struct partial_die_info *pdi,
3942 struct dwarf2_cu *cu)
3943{
3944 char *grandparent_scope;
3945 struct partial_die_info *parent, *real_pdi;
91c24f0a 3946
72bf9492
DJ
3947 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3948 then this means the parent of the specification DIE. */
3949
3950 real_pdi = pdi;
72bf9492 3951 while (real_pdi->has_specification)
10b3939b 3952 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3953
3954 parent = real_pdi->die_parent;
3955 if (parent == NULL)
3956 return NULL;
3957
3958 if (parent->scope_set)
3959 return parent->scope;
3960
3961 fixup_partial_die (parent, cu);
3962
10b3939b 3963 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3964
acebe513
UW
3965 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3966 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3967 Work around this problem here. */
3968 if (cu->language == language_cplus
6e70227d 3969 && parent->tag == DW_TAG_namespace
acebe513
UW
3970 && strcmp (parent->name, "::") == 0
3971 && grandparent_scope == NULL)
3972 {
3973 parent->scope = NULL;
3974 parent->scope_set = 1;
3975 return NULL;
3976 }
3977
9c6c53f7
SA
3978 if (pdi->tag == DW_TAG_enumerator)
3979 /* Enumerators should not get the name of the enumeration as a prefix. */
3980 parent->scope = grandparent_scope;
3981 else if (parent->tag == DW_TAG_namespace
f55ee35c 3982 || parent->tag == DW_TAG_module
72bf9492
DJ
3983 || parent->tag == DW_TAG_structure_type
3984 || parent->tag == DW_TAG_class_type
680b30c7 3985 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3986 || parent->tag == DW_TAG_union_type
3987 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3988 {
3989 if (grandparent_scope == NULL)
3990 parent->scope = parent->name;
3991 else
3e43a32a
MS
3992 parent->scope = typename_concat (&cu->comp_unit_obstack,
3993 grandparent_scope,
f55ee35c 3994 parent->name, 0, cu);
72bf9492 3995 }
72bf9492
DJ
3996 else
3997 {
3998 /* FIXME drow/2004-04-01: What should we be doing with
3999 function-local names? For partial symbols, we should probably be
4000 ignoring them. */
4001 complaint (&symfile_complaints,
e2e0b3e5 4002 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
4003 parent->tag, pdi->offset);
4004 parent->scope = grandparent_scope;
c906108c
SS
4005 }
4006
72bf9492
DJ
4007 parent->scope_set = 1;
4008 return parent->scope;
4009}
4010
4011/* Return the fully scoped name associated with PDI, from compilation unit
4012 CU. The result will be allocated with malloc. */
4013static char *
4014partial_die_full_name (struct partial_die_info *pdi,
4015 struct dwarf2_cu *cu)
4016{
4017 char *parent_scope;
4018
98bfdba5
PA
4019 /* If this is a template instantiation, we can not work out the
4020 template arguments from partial DIEs. So, unfortunately, we have
4021 to go through the full DIEs. At least any work we do building
4022 types here will be reused if full symbols are loaded later. */
4023 if (pdi->has_template_arguments)
4024 {
4025 fixup_partial_die (pdi, cu);
4026
4027 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4028 {
4029 struct die_info *die;
4030 struct attribute attr;
4031 struct dwarf2_cu *ref_cu = cu;
4032
4033 attr.name = 0;
4034 attr.form = DW_FORM_ref_addr;
4035 attr.u.addr = pdi->offset;
4036 die = follow_die_ref (NULL, &attr, &ref_cu);
4037
4038 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4039 }
4040 }
4041
72bf9492
DJ
4042 parent_scope = partial_die_parent_scope (pdi, cu);
4043 if (parent_scope == NULL)
4044 return NULL;
4045 else
f55ee35c 4046 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4047}
4048
4049static void
72bf9492 4050add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4051{
e7c27a73 4052 struct objfile *objfile = cu->objfile;
c906108c 4053 CORE_ADDR addr = 0;
decbce07 4054 char *actual_name = NULL;
5c4e30ca 4055 const struct partial_symbol *psym = NULL;
e142c38c 4056 CORE_ADDR baseaddr;
72bf9492 4057 int built_actual_name = 0;
e142c38c
DJ
4058
4059 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4060
94af9270
KS
4061 actual_name = partial_die_full_name (pdi, cu);
4062 if (actual_name)
4063 built_actual_name = 1;
63d06c5c 4064
72bf9492
DJ
4065 if (actual_name == NULL)
4066 actual_name = pdi->name;
4067
c906108c
SS
4068 switch (pdi->tag)
4069 {
4070 case DW_TAG_subprogram:
2cfa0c8d 4071 if (pdi->is_external || cu->language == language_ada)
c906108c 4072 {
2cfa0c8d
JB
4073 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4074 of the global scope. But in Ada, we want to be able to access
4075 nested procedures globally. So all Ada subprograms are stored
4076 in the global scope. */
f47fb265 4077 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4078 mst_text, objfile); */
f47fb265
MS
4079 add_psymbol_to_list (actual_name, strlen (actual_name),
4080 built_actual_name,
4081 VAR_DOMAIN, LOC_BLOCK,
4082 &objfile->global_psymbols,
4083 0, pdi->lowpc + baseaddr,
4084 cu->language, objfile);
c906108c
SS
4085 }
4086 else
4087 {
f47fb265 4088 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4089 mst_file_text, objfile); */
f47fb265
MS
4090 add_psymbol_to_list (actual_name, strlen (actual_name),
4091 built_actual_name,
4092 VAR_DOMAIN, LOC_BLOCK,
4093 &objfile->static_psymbols,
4094 0, pdi->lowpc + baseaddr,
4095 cu->language, objfile);
c906108c
SS
4096 }
4097 break;
72929c62
JB
4098 case DW_TAG_constant:
4099 {
4100 struct psymbol_allocation_list *list;
4101
4102 if (pdi->is_external)
4103 list = &objfile->global_psymbols;
4104 else
4105 list = &objfile->static_psymbols;
f47fb265
MS
4106 add_psymbol_to_list (actual_name, strlen (actual_name),
4107 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4108 list, 0, 0, cu->language, objfile);
72929c62
JB
4109 }
4110 break;
c906108c 4111 case DW_TAG_variable:
caac4577
JG
4112 if (pdi->locdesc)
4113 addr = decode_locdesc (pdi->locdesc, cu);
4114
4115 if (pdi->locdesc
4116 && addr == 0
4117 && !dwarf2_per_objfile->has_section_at_zero)
4118 {
4119 /* A global or static variable may also have been stripped
4120 out by the linker if unused, in which case its address
4121 will be nullified; do not add such variables into partial
4122 symbol table then. */
4123 }
4124 else if (pdi->is_external)
c906108c
SS
4125 {
4126 /* Global Variable.
4127 Don't enter into the minimal symbol tables as there is
4128 a minimal symbol table entry from the ELF symbols already.
4129 Enter into partial symbol table if it has a location
4130 descriptor or a type.
4131 If the location descriptor is missing, new_symbol will create
4132 a LOC_UNRESOLVED symbol, the address of the variable will then
4133 be determined from the minimal symbol table whenever the variable
4134 is referenced.
4135 The address for the partial symbol table entry is not
4136 used by GDB, but it comes in handy for debugging partial symbol
4137 table building. */
4138
c906108c 4139 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4140 add_psymbol_to_list (actual_name, strlen (actual_name),
4141 built_actual_name,
4142 VAR_DOMAIN, LOC_STATIC,
4143 &objfile->global_psymbols,
4144 0, addr + baseaddr,
4145 cu->language, objfile);
c906108c
SS
4146 }
4147 else
4148 {
0963b4bd 4149 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4150 if (pdi->locdesc == NULL)
decbce07
MS
4151 {
4152 if (built_actual_name)
4153 xfree (actual_name);
4154 return;
4155 }
f47fb265 4156 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4157 mst_file_data, objfile); */
f47fb265
MS
4158 add_psymbol_to_list (actual_name, strlen (actual_name),
4159 built_actual_name,
4160 VAR_DOMAIN, LOC_STATIC,
4161 &objfile->static_psymbols,
4162 0, addr + baseaddr,
4163 cu->language, objfile);
c906108c
SS
4164 }
4165 break;
4166 case DW_TAG_typedef:
4167 case DW_TAG_base_type:
a02abb62 4168 case DW_TAG_subrange_type:
38d518c9 4169 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4170 built_actual_name,
176620f1 4171 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4172 &objfile->static_psymbols,
e142c38c 4173 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4174 break;
72bf9492
DJ
4175 case DW_TAG_namespace:
4176 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4177 built_actual_name,
72bf9492
DJ
4178 VAR_DOMAIN, LOC_TYPEDEF,
4179 &objfile->global_psymbols,
4180 0, (CORE_ADDR) 0, cu->language, objfile);
4181 break;
c906108c 4182 case DW_TAG_class_type:
680b30c7 4183 case DW_TAG_interface_type:
c906108c
SS
4184 case DW_TAG_structure_type:
4185 case DW_TAG_union_type:
4186 case DW_TAG_enumeration_type:
fa4028e9
JB
4187 /* Skip external references. The DWARF standard says in the section
4188 about "Structure, Union, and Class Type Entries": "An incomplete
4189 structure, union or class type is represented by a structure,
4190 union or class entry that does not have a byte size attribute
4191 and that has a DW_AT_declaration attribute." */
4192 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4193 {
4194 if (built_actual_name)
4195 xfree (actual_name);
4196 return;
4197 }
fa4028e9 4198
63d06c5c
DC
4199 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4200 static vs. global. */
38d518c9 4201 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4202 built_actual_name,
176620f1 4203 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4204 (cu->language == language_cplus
4205 || cu->language == language_java)
63d06c5c
DC
4206 ? &objfile->global_psymbols
4207 : &objfile->static_psymbols,
e142c38c 4208 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4209
c906108c
SS
4210 break;
4211 case DW_TAG_enumerator:
38d518c9 4212 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4213 built_actual_name,
176620f1 4214 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4215 (cu->language == language_cplus
4216 || cu->language == language_java)
f6fe98ef
DJ
4217 ? &objfile->global_psymbols
4218 : &objfile->static_psymbols,
e142c38c 4219 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4220 break;
4221 default:
4222 break;
4223 }
5c4e30ca 4224
72bf9492
DJ
4225 if (built_actual_name)
4226 xfree (actual_name);
c906108c
SS
4227}
4228
5c4e30ca
DC
4229/* Read a partial die corresponding to a namespace; also, add a symbol
4230 corresponding to that namespace to the symbol table. NAMESPACE is
4231 the name of the enclosing namespace. */
91c24f0a 4232
72bf9492
DJ
4233static void
4234add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4236 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4237{
72bf9492 4238 /* Add a symbol for the namespace. */
e7c27a73 4239
72bf9492 4240 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4241
4242 /* Now scan partial symbols in that namespace. */
4243
91c24f0a 4244 if (pdi->has_children)
5734ee8b 4245 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4246}
4247
5d7cb8df
JK
4248/* Read a partial die corresponding to a Fortran module. */
4249
4250static void
4251add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4252 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4253{
f55ee35c 4254 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4255
4256 if (pdi->has_children)
4257 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4258}
4259
bc30ff58
JB
4260/* Read a partial die corresponding to a subprogram and create a partial
4261 symbol for that subprogram. When the CU language allows it, this
4262 routine also defines a partial symbol for each nested subprogram
4263 that this subprogram contains.
6e70227d 4264
bc30ff58
JB
4265 DIE my also be a lexical block, in which case we simply search
4266 recursively for suprograms defined inside that lexical block.
4267 Again, this is only performed when the CU language allows this
4268 type of definitions. */
4269
4270static void
4271add_partial_subprogram (struct partial_die_info *pdi,
4272 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4273 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4274{
4275 if (pdi->tag == DW_TAG_subprogram)
4276 {
4277 if (pdi->has_pc_info)
4278 {
4279 if (pdi->lowpc < *lowpc)
4280 *lowpc = pdi->lowpc;
4281 if (pdi->highpc > *highpc)
4282 *highpc = pdi->highpc;
5734ee8b
DJ
4283 if (need_pc)
4284 {
4285 CORE_ADDR baseaddr;
4286 struct objfile *objfile = cu->objfile;
4287
4288 baseaddr = ANOFFSET (objfile->section_offsets,
4289 SECT_OFF_TEXT (objfile));
4290 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4291 pdi->lowpc + baseaddr,
4292 pdi->highpc - 1 + baseaddr,
9291a0cd 4293 cu->per_cu->v.psymtab);
5734ee8b 4294 }
bc30ff58 4295 if (!pdi->is_declaration)
e8d05480
JB
4296 /* Ignore subprogram DIEs that do not have a name, they are
4297 illegal. Do not emit a complaint at this point, we will
4298 do so when we convert this psymtab into a symtab. */
4299 if (pdi->name)
4300 add_partial_symbol (pdi, cu);
bc30ff58
JB
4301 }
4302 }
6e70227d 4303
bc30ff58
JB
4304 if (! pdi->has_children)
4305 return;
4306
4307 if (cu->language == language_ada)
4308 {
4309 pdi = pdi->die_child;
4310 while (pdi != NULL)
4311 {
4312 fixup_partial_die (pdi, cu);
4313 if (pdi->tag == DW_TAG_subprogram
4314 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4315 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4316 pdi = pdi->die_sibling;
4317 }
4318 }
4319}
4320
91c24f0a
DC
4321/* Read a partial die corresponding to an enumeration type. */
4322
72bf9492
DJ
4323static void
4324add_partial_enumeration (struct partial_die_info *enum_pdi,
4325 struct dwarf2_cu *cu)
91c24f0a 4326{
72bf9492 4327 struct partial_die_info *pdi;
91c24f0a
DC
4328
4329 if (enum_pdi->name != NULL)
72bf9492
DJ
4330 add_partial_symbol (enum_pdi, cu);
4331
4332 pdi = enum_pdi->die_child;
4333 while (pdi)
91c24f0a 4334 {
72bf9492 4335 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4336 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4337 else
72bf9492
DJ
4338 add_partial_symbol (pdi, cu);
4339 pdi = pdi->die_sibling;
91c24f0a 4340 }
91c24f0a
DC
4341}
4342
6caca83c
CC
4343/* Return the initial uleb128 in the die at INFO_PTR. */
4344
4345static unsigned int
4346peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4347{
4348 unsigned int bytes_read;
4349
4350 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4351}
4352
4bb7a0a7
DJ
4353/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4354 Return the corresponding abbrev, or NULL if the number is zero (indicating
4355 an empty DIE). In either case *BYTES_READ will be set to the length of
4356 the initial number. */
4357
4358static struct abbrev_info *
fe1b8b76 4359peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4360 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4361{
4362 bfd *abfd = cu->objfile->obfd;
4363 unsigned int abbrev_number;
4364 struct abbrev_info *abbrev;
4365
4366 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4367
4368 if (abbrev_number == 0)
4369 return NULL;
4370
4371 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4372 if (!abbrev)
4373 {
3e43a32a
MS
4374 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4375 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4376 }
4377
4378 return abbrev;
4379}
4380
93311388
DE
4381/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4382 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4383 DIE. Any children of the skipped DIEs will also be skipped. */
4384
fe1b8b76 4385static gdb_byte *
93311388 4386skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4387{
4388 struct abbrev_info *abbrev;
4389 unsigned int bytes_read;
4390
4391 while (1)
4392 {
4393 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4394 if (abbrev == NULL)
4395 return info_ptr + bytes_read;
4396 else
93311388 4397 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4398 }
4399}
4400
93311388
DE
4401/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4402 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4403 abbrev corresponding to that skipped uleb128 should be passed in
4404 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4405 children. */
4406
fe1b8b76 4407static gdb_byte *
93311388
DE
4408skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4409 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4410{
4411 unsigned int bytes_read;
4412 struct attribute attr;
4413 bfd *abfd = cu->objfile->obfd;
4414 unsigned int form, i;
4415
4416 for (i = 0; i < abbrev->num_attrs; i++)
4417 {
4418 /* The only abbrev we care about is DW_AT_sibling. */
4419 if (abbrev->attrs[i].name == DW_AT_sibling)
4420 {
4421 read_attribute (&attr, &abbrev->attrs[i],
4422 abfd, info_ptr, cu);
4423 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4424 complaint (&symfile_complaints,
4425 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4426 else
93311388 4427 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4428 }
4429
4430 /* If it isn't DW_AT_sibling, skip this attribute. */
4431 form = abbrev->attrs[i].form;
4432 skip_attribute:
4433 switch (form)
4434 {
4bb7a0a7 4435 case DW_FORM_ref_addr:
ae411497
TT
4436 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4437 and later it is offset sized. */
4438 if (cu->header.version == 2)
4439 info_ptr += cu->header.addr_size;
4440 else
4441 info_ptr += cu->header.offset_size;
4442 break;
4443 case DW_FORM_addr:
4bb7a0a7
DJ
4444 info_ptr += cu->header.addr_size;
4445 break;
4446 case DW_FORM_data1:
4447 case DW_FORM_ref1:
4448 case DW_FORM_flag:
4449 info_ptr += 1;
4450 break;
2dc7f7b3
TT
4451 case DW_FORM_flag_present:
4452 break;
4bb7a0a7
DJ
4453 case DW_FORM_data2:
4454 case DW_FORM_ref2:
4455 info_ptr += 2;
4456 break;
4457 case DW_FORM_data4:
4458 case DW_FORM_ref4:
4459 info_ptr += 4;
4460 break;
4461 case DW_FORM_data8:
4462 case DW_FORM_ref8:
55f1336d 4463 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4464 info_ptr += 8;
4465 break;
4466 case DW_FORM_string:
9b1c24c8 4467 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4468 info_ptr += bytes_read;
4469 break;
2dc7f7b3 4470 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4471 case DW_FORM_strp:
4472 info_ptr += cu->header.offset_size;
4473 break;
2dc7f7b3 4474 case DW_FORM_exprloc:
4bb7a0a7
DJ
4475 case DW_FORM_block:
4476 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4477 info_ptr += bytes_read;
4478 break;
4479 case DW_FORM_block1:
4480 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4481 break;
4482 case DW_FORM_block2:
4483 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4484 break;
4485 case DW_FORM_block4:
4486 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4487 break;
4488 case DW_FORM_sdata:
4489 case DW_FORM_udata:
4490 case DW_FORM_ref_udata:
4491 info_ptr = skip_leb128 (abfd, info_ptr);
4492 break;
4493 case DW_FORM_indirect:
4494 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4495 info_ptr += bytes_read;
4496 /* We need to continue parsing from here, so just go back to
4497 the top. */
4498 goto skip_attribute;
4499
4500 default:
3e43a32a
MS
4501 error (_("Dwarf Error: Cannot handle %s "
4502 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4503 dwarf_form_name (form),
4504 bfd_get_filename (abfd));
4505 }
4506 }
4507
4508 if (abbrev->has_children)
93311388 4509 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4510 else
4511 return info_ptr;
4512}
4513
93311388
DE
4514/* Locate ORIG_PDI's sibling.
4515 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4516 in BUFFER. */
91c24f0a 4517
fe1b8b76 4518static gdb_byte *
93311388
DE
4519locate_pdi_sibling (struct partial_die_info *orig_pdi,
4520 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4521 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4522{
4523 /* Do we know the sibling already? */
72bf9492 4524
91c24f0a
DC
4525 if (orig_pdi->sibling)
4526 return orig_pdi->sibling;
4527
4528 /* Are there any children to deal with? */
4529
4530 if (!orig_pdi->has_children)
4531 return info_ptr;
4532
4bb7a0a7 4533 /* Skip the children the long way. */
91c24f0a 4534
93311388 4535 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4536}
4537
c906108c
SS
4538/* Expand this partial symbol table into a full symbol table. */
4539
4540static void
fba45db2 4541dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4542{
c906108c
SS
4543 if (pst != NULL)
4544 {
4545 if (pst->readin)
4546 {
3e43a32a
MS
4547 warning (_("bug: psymtab for %s is already read in."),
4548 pst->filename);
c906108c
SS
4549 }
4550 else
4551 {
4552 if (info_verbose)
4553 {
3e43a32a
MS
4554 printf_filtered (_("Reading in symbols for %s..."),
4555 pst->filename);
c906108c
SS
4556 gdb_flush (gdb_stdout);
4557 }
4558
10b3939b
DJ
4559 /* Restore our global data. */
4560 dwarf2_per_objfile = objfile_data (pst->objfile,
4561 dwarf2_objfile_data_key);
4562
b2ab525c
KB
4563 /* If this psymtab is constructed from a debug-only objfile, the
4564 has_section_at_zero flag will not necessarily be correct. We
4565 can get the correct value for this flag by looking at the data
4566 associated with the (presumably stripped) associated objfile. */
4567 if (pst->objfile->separate_debug_objfile_backlink)
4568 {
4569 struct dwarf2_per_objfile *dpo_backlink
4570 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4571 dwarf2_objfile_data_key);
9a619af0 4572
b2ab525c
KB
4573 dwarf2_per_objfile->has_section_at_zero
4574 = dpo_backlink->has_section_at_zero;
4575 }
4576
98bfdba5
PA
4577 dwarf2_per_objfile->reading_partial_symbols = 0;
4578
c906108c
SS
4579 psymtab_to_symtab_1 (pst);
4580
4581 /* Finish up the debug error message. */
4582 if (info_verbose)
a3f17187 4583 printf_filtered (_("done.\n"));
c906108c
SS
4584 }
4585 }
4586}
9cdd5dbd
DE
4587\f
4588/* Reading in full CUs. */
c906108c 4589
10b3939b
DJ
4590/* Add PER_CU to the queue. */
4591
4592static void
03dd20cc 4593queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4594{
4595 struct dwarf2_queue_item *item;
4596
4597 per_cu->queued = 1;
4598 item = xmalloc (sizeof (*item));
4599 item->per_cu = per_cu;
4600 item->next = NULL;
4601
4602 if (dwarf2_queue == NULL)
4603 dwarf2_queue = item;
4604 else
4605 dwarf2_queue_tail->next = item;
4606
4607 dwarf2_queue_tail = item;
4608}
4609
4610/* Process the queue. */
4611
4612static void
4613process_queue (struct objfile *objfile)
4614{
4615 struct dwarf2_queue_item *item, *next_item;
4616
03dd20cc
DJ
4617 /* The queue starts out with one item, but following a DIE reference
4618 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4619 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4620 {
9291a0cd
TT
4621 if (dwarf2_per_objfile->using_index
4622 ? !item->per_cu->v.quick->symtab
4623 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4624 process_full_comp_unit (item->per_cu);
4625
4626 item->per_cu->queued = 0;
4627 next_item = item->next;
4628 xfree (item);
4629 }
4630
4631 dwarf2_queue_tail = NULL;
4632}
4633
4634/* Free all allocated queue entries. This function only releases anything if
4635 an error was thrown; if the queue was processed then it would have been
4636 freed as we went along. */
4637
4638static void
4639dwarf2_release_queue (void *dummy)
4640{
4641 struct dwarf2_queue_item *item, *last;
4642
4643 item = dwarf2_queue;
4644 while (item)
4645 {
4646 /* Anything still marked queued is likely to be in an
4647 inconsistent state, so discard it. */
4648 if (item->per_cu->queued)
4649 {
4650 if (item->per_cu->cu != NULL)
4651 free_one_cached_comp_unit (item->per_cu->cu);
4652 item->per_cu->queued = 0;
4653 }
4654
4655 last = item;
4656 item = item->next;
4657 xfree (last);
4658 }
4659
4660 dwarf2_queue = dwarf2_queue_tail = NULL;
4661}
4662
4663/* Read in full symbols for PST, and anything it depends on. */
4664
c906108c 4665static void
fba45db2 4666psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4667{
10b3939b 4668 struct dwarf2_per_cu_data *per_cu;
c906108c 4669 struct cleanup *back_to;
aaa75496
JB
4670 int i;
4671
4672 for (i = 0; i < pst->number_of_dependencies; i++)
4673 if (!pst->dependencies[i]->readin)
4674 {
4675 /* Inform about additional files that need to be read in. */
4676 if (info_verbose)
4677 {
a3f17187 4678 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4679 fputs_filtered (" ", gdb_stdout);
4680 wrap_here ("");
4681 fputs_filtered ("and ", gdb_stdout);
4682 wrap_here ("");
4683 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4684 wrap_here (""); /* Flush output. */
aaa75496
JB
4685 gdb_flush (gdb_stdout);
4686 }
4687 psymtab_to_symtab_1 (pst->dependencies[i]);
4688 }
4689
e38df1d0 4690 per_cu = pst->read_symtab_private;
10b3939b
DJ
4691
4692 if (per_cu == NULL)
aaa75496
JB
4693 {
4694 /* It's an include file, no symbols to read for it.
4695 Everything is in the parent symtab. */
4696 pst->readin = 1;
4697 return;
4698 }
c906108c 4699
9291a0cd 4700 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4701}
4702
93311388 4703/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4704
93311388 4705static void
3e43a32a
MS
4706load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4707 struct objfile *objfile)
10b3939b 4708{
31ffec48 4709 bfd *abfd = objfile->obfd;
10b3939b 4710 struct dwarf2_cu *cu;
c764a876 4711 unsigned int offset;
93311388 4712 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4713 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4714 struct attribute *attr;
98bfdba5 4715 int read_cu = 0;
6502dd73 4716
b0df02fd 4717 gdb_assert (! per_cu->debug_types_section);
348e048f 4718
c906108c 4719 /* Set local variables from the partial symbol table info. */
10b3939b 4720 offset = per_cu->offset;
6502dd73 4721
be391dca 4722 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4723 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4724 beg_of_comp_unit = info_ptr;
63d06c5c 4725
98bfdba5
PA
4726 if (per_cu->cu == NULL)
4727 {
9816fde3
JK
4728 cu = xmalloc (sizeof (*cu));
4729 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4730
4731 read_cu = 1;
c906108c 4732
98bfdba5
PA
4733 /* If an error occurs while loading, release our storage. */
4734 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4735
98bfdba5
PA
4736 /* Read in the comp_unit header. */
4737 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4738
6caca83c
CC
4739 /* Skip dummy compilation units. */
4740 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4741 + dwarf2_per_objfile->info.size)
4742 || peek_abbrev_code (abfd, info_ptr) == 0)
4743 {
4744 do_cleanups (free_cu_cleanup);
4745 return;
4746 }
4747
98bfdba5
PA
4748 /* Complete the cu_header. */
4749 cu->header.offset = offset;
4750 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4751
98bfdba5
PA
4752 /* Read the abbrevs for this compilation unit. */
4753 dwarf2_read_abbrevs (abfd, cu);
4754 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4755
98bfdba5
PA
4756 /* Link this compilation unit into the compilation unit tree. */
4757 per_cu->cu = cu;
4758 cu->per_cu = per_cu;
98bfdba5
PA
4759
4760 /* Link this CU into read_in_chain. */
4761 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4762 dwarf2_per_objfile->read_in_chain = per_cu;
4763 }
4764 else
4765 {
4766 cu = per_cu->cu;
4767 info_ptr += cu->header.first_die_offset;
4768 }
e142c38c 4769
93311388 4770 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4771
4772 /* We try not to read any attributes in this function, because not
9cdd5dbd 4773 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4774 table processing isn't initialized. But we have to set the CU language,
4775 or we won't be able to build types correctly. */
9816fde3 4776 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4777
a6c727b2
DJ
4778 /* Similarly, if we do not read the producer, we can not apply
4779 producer-specific interpretation. */
4780 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4781 if (attr)
4782 cu->producer = DW_STRING (attr);
4783
98bfdba5
PA
4784 if (read_cu)
4785 {
4786 do_cleanups (free_abbrevs_cleanup);
e142c38c 4787
98bfdba5
PA
4788 /* We've successfully allocated this compilation unit. Let our
4789 caller clean it up when finished with it. */
4790 discard_cleanups (free_cu_cleanup);
4791 }
10b3939b
DJ
4792}
4793
3da10d80
KS
4794/* Add a DIE to the delayed physname list. */
4795
4796static void
4797add_to_method_list (struct type *type, int fnfield_index, int index,
4798 const char *name, struct die_info *die,
4799 struct dwarf2_cu *cu)
4800{
4801 struct delayed_method_info mi;
4802 mi.type = type;
4803 mi.fnfield_index = fnfield_index;
4804 mi.index = index;
4805 mi.name = name;
4806 mi.die = die;
4807 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4808}
4809
4810/* A cleanup for freeing the delayed method list. */
4811
4812static void
4813free_delayed_list (void *ptr)
4814{
4815 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4816 if (cu->method_list != NULL)
4817 {
4818 VEC_free (delayed_method_info, cu->method_list);
4819 cu->method_list = NULL;
4820 }
4821}
4822
4823/* Compute the physnames of any methods on the CU's method list.
4824
4825 The computation of method physnames is delayed in order to avoid the
4826 (bad) condition that one of the method's formal parameters is of an as yet
4827 incomplete type. */
4828
4829static void
4830compute_delayed_physnames (struct dwarf2_cu *cu)
4831{
4832 int i;
4833 struct delayed_method_info *mi;
4834 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4835 {
1d06ead6 4836 const char *physname;
3da10d80
KS
4837 struct fn_fieldlist *fn_flp
4838 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4839 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4840 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4841 }
4842}
4843
9cdd5dbd 4844/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4845 already been loaded into memory. */
4846
4847static void
4848process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4849{
10b3939b 4850 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4851 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4852 CORE_ADDR lowpc, highpc;
4853 struct symtab *symtab;
3da10d80 4854 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4855 CORE_ADDR baseaddr;
4856
4857 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4858
10b3939b
DJ
4859 buildsym_init ();
4860 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4861 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4862
4863 cu->list_in_scope = &file_symbols;
c906108c
SS
4864
4865 /* Do line number decoding in read_file_scope () */
10b3939b 4866 process_die (cu->dies, cu);
c906108c 4867
3da10d80
KS
4868 /* Now that we have processed all the DIEs in the CU, all the types
4869 should be complete, and it should now be safe to compute all of the
4870 physnames. */
4871 compute_delayed_physnames (cu);
4872 do_cleanups (delayed_list_cleanup);
4873
fae299cd
DC
4874 /* Some compilers don't define a DW_AT_high_pc attribute for the
4875 compilation unit. If the DW_AT_high_pc is missing, synthesize
4876 it, by scanning the DIE's below the compilation unit. */
10b3939b 4877 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4878
613e1657 4879 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4880
8be455d7 4881 if (symtab != NULL)
c906108c 4882 {
df15bd07 4883 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4884
8be455d7
JK
4885 /* Set symtab language to language from DW_AT_language. If the
4886 compilation is from a C file generated by language preprocessors, do
4887 not set the language if it was already deduced by start_subfile. */
4888 if (!(cu->language == language_c && symtab->language != language_c))
4889 symtab->language = cu->language;
4890
4891 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4892 produce DW_AT_location with location lists but it can be possibly
4893 invalid without -fvar-tracking.
4894
4895 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4896 needed, it would be wrong due to missing DW_AT_producer there.
4897
4898 Still one can confuse GDB by using non-standard GCC compilation
4899 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4900 */
4632c0d0 4901 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4902 symtab->locations_valid = 1;
e0d00bc7
JK
4903
4904 if (gcc_4_minor >= 5)
4905 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4906
4907 symtab->call_site_htab = cu->call_site_htab;
c906108c 4908 }
9291a0cd
TT
4909
4910 if (dwarf2_per_objfile->using_index)
4911 per_cu->v.quick->symtab = symtab;
4912 else
4913 {
4914 struct partial_symtab *pst = per_cu->v.psymtab;
4915 pst->symtab = symtab;
4916 pst->readin = 1;
4917 }
c906108c
SS
4918
4919 do_cleanups (back_to);
4920}
4921
4922/* Process a die and its children. */
4923
4924static void
e7c27a73 4925process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4926{
4927 switch (die->tag)
4928 {
4929 case DW_TAG_padding:
4930 break;
4931 case DW_TAG_compile_unit:
e7c27a73 4932 read_file_scope (die, cu);
c906108c 4933 break;
348e048f
DE
4934 case DW_TAG_type_unit:
4935 read_type_unit_scope (die, cu);
4936 break;
c906108c 4937 case DW_TAG_subprogram:
c906108c 4938 case DW_TAG_inlined_subroutine:
edb3359d 4939 read_func_scope (die, cu);
c906108c
SS
4940 break;
4941 case DW_TAG_lexical_block:
14898363
L
4942 case DW_TAG_try_block:
4943 case DW_TAG_catch_block:
e7c27a73 4944 read_lexical_block_scope (die, cu);
c906108c 4945 break;
96408a79
SA
4946 case DW_TAG_GNU_call_site:
4947 read_call_site_scope (die, cu);
4948 break;
c906108c 4949 case DW_TAG_class_type:
680b30c7 4950 case DW_TAG_interface_type:
c906108c
SS
4951 case DW_TAG_structure_type:
4952 case DW_TAG_union_type:
134d01f1 4953 process_structure_scope (die, cu);
c906108c
SS
4954 break;
4955 case DW_TAG_enumeration_type:
134d01f1 4956 process_enumeration_scope (die, cu);
c906108c 4957 break;
134d01f1 4958
f792889a
DJ
4959 /* These dies have a type, but processing them does not create
4960 a symbol or recurse to process the children. Therefore we can
4961 read them on-demand through read_type_die. */
c906108c 4962 case DW_TAG_subroutine_type:
72019c9c 4963 case DW_TAG_set_type:
c906108c 4964 case DW_TAG_array_type:
c906108c 4965 case DW_TAG_pointer_type:
c906108c 4966 case DW_TAG_ptr_to_member_type:
c906108c 4967 case DW_TAG_reference_type:
c906108c 4968 case DW_TAG_string_type:
c906108c 4969 break;
134d01f1 4970
c906108c 4971 case DW_TAG_base_type:
a02abb62 4972 case DW_TAG_subrange_type:
cb249c71 4973 case DW_TAG_typedef:
134d01f1
DJ
4974 /* Add a typedef symbol for the type definition, if it has a
4975 DW_AT_name. */
f792889a 4976 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4977 break;
c906108c 4978 case DW_TAG_common_block:
e7c27a73 4979 read_common_block (die, cu);
c906108c
SS
4980 break;
4981 case DW_TAG_common_inclusion:
4982 break;
d9fa45fe 4983 case DW_TAG_namespace:
63d06c5c 4984 processing_has_namespace_info = 1;
e7c27a73 4985 read_namespace (die, cu);
d9fa45fe 4986 break;
5d7cb8df 4987 case DW_TAG_module:
f55ee35c 4988 processing_has_namespace_info = 1;
5d7cb8df
JK
4989 read_module (die, cu);
4990 break;
d9fa45fe
DC
4991 case DW_TAG_imported_declaration:
4992 case DW_TAG_imported_module:
63d06c5c 4993 processing_has_namespace_info = 1;
27aa8d6a
SW
4994 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4995 || cu->language != language_fortran))
4996 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4997 dwarf_tag_name (die->tag));
4998 read_import_statement (die, cu);
d9fa45fe 4999 break;
c906108c 5000 default:
e7c27a73 5001 new_symbol (die, NULL, cu);
c906108c
SS
5002 break;
5003 }
5004}
5005
94af9270
KS
5006/* A helper function for dwarf2_compute_name which determines whether DIE
5007 needs to have the name of the scope prepended to the name listed in the
5008 die. */
5009
5010static int
5011die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5012{
1c809c68
TT
5013 struct attribute *attr;
5014
94af9270
KS
5015 switch (die->tag)
5016 {
5017 case DW_TAG_namespace:
5018 case DW_TAG_typedef:
5019 case DW_TAG_class_type:
5020 case DW_TAG_interface_type:
5021 case DW_TAG_structure_type:
5022 case DW_TAG_union_type:
5023 case DW_TAG_enumeration_type:
5024 case DW_TAG_enumerator:
5025 case DW_TAG_subprogram:
5026 case DW_TAG_member:
5027 return 1;
5028
5029 case DW_TAG_variable:
c2b0a229 5030 case DW_TAG_constant:
94af9270
KS
5031 /* We only need to prefix "globally" visible variables. These include
5032 any variable marked with DW_AT_external or any variable that
5033 lives in a namespace. [Variables in anonymous namespaces
5034 require prefixing, but they are not DW_AT_external.] */
5035
5036 if (dwarf2_attr (die, DW_AT_specification, cu))
5037 {
5038 struct dwarf2_cu *spec_cu = cu;
9a619af0 5039
94af9270
KS
5040 return die_needs_namespace (die_specification (die, &spec_cu),
5041 spec_cu);
5042 }
5043
1c809c68 5044 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5045 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5046 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5047 return 0;
5048 /* A variable in a lexical block of some kind does not need a
5049 namespace, even though in C++ such variables may be external
5050 and have a mangled name. */
5051 if (die->parent->tag == DW_TAG_lexical_block
5052 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5053 || die->parent->tag == DW_TAG_catch_block
5054 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5055 return 0;
5056 return 1;
94af9270
KS
5057
5058 default:
5059 return 0;
5060 }
5061}
5062
98bfdba5
PA
5063/* Retrieve the last character from a mem_file. */
5064
5065static void
5066do_ui_file_peek_last (void *object, const char *buffer, long length)
5067{
5068 char *last_char_p = (char *) object;
5069
5070 if (length > 0)
5071 *last_char_p = buffer[length - 1];
5072}
5073
94af9270
KS
5074/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5075 compute the physname for the object, which include a method's
5076 formal parameters (C++/Java) and return type (Java).
5077
af6b7be1
JB
5078 For Ada, return the DIE's linkage name rather than the fully qualified
5079 name. PHYSNAME is ignored..
5080
94af9270
KS
5081 The result is allocated on the objfile_obstack and canonicalized. */
5082
5083static const char *
5084dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5085 int physname)
5086{
5087 if (name == NULL)
5088 name = dwarf2_name (die, cu);
5089
f55ee35c
JK
5090 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5091 compute it by typename_concat inside GDB. */
5092 if (cu->language == language_ada
5093 || (cu->language == language_fortran && physname))
5094 {
5095 /* For Ada unit, we prefer the linkage name over the name, as
5096 the former contains the exported name, which the user expects
5097 to be able to reference. Ideally, we want the user to be able
5098 to reference this entity using either natural or linkage name,
5099 but we haven't started looking at this enhancement yet. */
5100 struct attribute *attr;
5101
5102 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5103 if (attr == NULL)
5104 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5105 if (attr && DW_STRING (attr))
5106 return DW_STRING (attr);
5107 }
5108
94af9270
KS
5109 /* These are the only languages we know how to qualify names in. */
5110 if (name != NULL
f55ee35c
JK
5111 && (cu->language == language_cplus || cu->language == language_java
5112 || cu->language == language_fortran))
94af9270
KS
5113 {
5114 if (die_needs_namespace (die, cu))
5115 {
5116 long length;
5117 char *prefix;
5118 struct ui_file *buf;
5119
5120 prefix = determine_prefix (die, cu);
5121 buf = mem_fileopen ();
5122 if (*prefix != '\0')
5123 {
f55ee35c
JK
5124 char *prefixed_name = typename_concat (NULL, prefix, name,
5125 physname, cu);
9a619af0 5126
94af9270
KS
5127 fputs_unfiltered (prefixed_name, buf);
5128 xfree (prefixed_name);
5129 }
5130 else
62d5b8da 5131 fputs_unfiltered (name, buf);
94af9270 5132
98bfdba5
PA
5133 /* Template parameters may be specified in the DIE's DW_AT_name, or
5134 as children with DW_TAG_template_type_param or
5135 DW_TAG_value_type_param. If the latter, add them to the name
5136 here. If the name already has template parameters, then
5137 skip this step; some versions of GCC emit both, and
5138 it is more efficient to use the pre-computed name.
5139
5140 Something to keep in mind about this process: it is very
5141 unlikely, or in some cases downright impossible, to produce
5142 something that will match the mangled name of a function.
5143 If the definition of the function has the same debug info,
5144 we should be able to match up with it anyway. But fallbacks
5145 using the minimal symbol, for instance to find a method
5146 implemented in a stripped copy of libstdc++, will not work.
5147 If we do not have debug info for the definition, we will have to
5148 match them up some other way.
5149
5150 When we do name matching there is a related problem with function
5151 templates; two instantiated function templates are allowed to
5152 differ only by their return types, which we do not add here. */
5153
5154 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5155 {
5156 struct attribute *attr;
5157 struct die_info *child;
5158 int first = 1;
5159
5160 die->building_fullname = 1;
5161
5162 for (child = die->child; child != NULL; child = child->sibling)
5163 {
5164 struct type *type;
5165 long value;
5166 gdb_byte *bytes;
5167 struct dwarf2_locexpr_baton *baton;
5168 struct value *v;
5169
5170 if (child->tag != DW_TAG_template_type_param
5171 && child->tag != DW_TAG_template_value_param)
5172 continue;
5173
5174 if (first)
5175 {
5176 fputs_unfiltered ("<", buf);
5177 first = 0;
5178 }
5179 else
5180 fputs_unfiltered (", ", buf);
5181
5182 attr = dwarf2_attr (child, DW_AT_type, cu);
5183 if (attr == NULL)
5184 {
5185 complaint (&symfile_complaints,
5186 _("template parameter missing DW_AT_type"));
5187 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5188 continue;
5189 }
5190 type = die_type (child, cu);
5191
5192 if (child->tag == DW_TAG_template_type_param)
5193 {
5194 c_print_type (type, "", buf, -1, 0);
5195 continue;
5196 }
5197
5198 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5199 if (attr == NULL)
5200 {
5201 complaint (&symfile_complaints,
3e43a32a
MS
5202 _("template parameter missing "
5203 "DW_AT_const_value"));
98bfdba5
PA
5204 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5205 continue;
5206 }
5207
5208 dwarf2_const_value_attr (attr, type, name,
5209 &cu->comp_unit_obstack, cu,
5210 &value, &bytes, &baton);
5211
5212 if (TYPE_NOSIGN (type))
5213 /* GDB prints characters as NUMBER 'CHAR'. If that's
5214 changed, this can use value_print instead. */
5215 c_printchar (value, type, buf);
5216 else
5217 {
5218 struct value_print_options opts;
5219
5220 if (baton != NULL)
5221 v = dwarf2_evaluate_loc_desc (type, NULL,
5222 baton->data,
5223 baton->size,
5224 baton->per_cu);
5225 else if (bytes != NULL)
5226 {
5227 v = allocate_value (type);
5228 memcpy (value_contents_writeable (v), bytes,
5229 TYPE_LENGTH (type));
5230 }
5231 else
5232 v = value_from_longest (type, value);
5233
3e43a32a
MS
5234 /* Specify decimal so that we do not depend on
5235 the radix. */
98bfdba5
PA
5236 get_formatted_print_options (&opts, 'd');
5237 opts.raw = 1;
5238 value_print (v, buf, &opts);
5239 release_value (v);
5240 value_free (v);
5241 }
5242 }
5243
5244 die->building_fullname = 0;
5245
5246 if (!first)
5247 {
5248 /* Close the argument list, with a space if necessary
5249 (nested templates). */
5250 char last_char = '\0';
5251 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5252 if (last_char == '>')
5253 fputs_unfiltered (" >", buf);
5254 else
5255 fputs_unfiltered (">", buf);
5256 }
5257 }
5258
94af9270
KS
5259 /* For Java and C++ methods, append formal parameter type
5260 information, if PHYSNAME. */
6e70227d 5261
94af9270
KS
5262 if (physname && die->tag == DW_TAG_subprogram
5263 && (cu->language == language_cplus
5264 || cu->language == language_java))
5265 {
5266 struct type *type = read_type_die (die, cu);
5267
3167638f 5268 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5269
5270 if (cu->language == language_java)
5271 {
5272 /* For java, we must append the return type to method
0963b4bd 5273 names. */
94af9270
KS
5274 if (die->tag == DW_TAG_subprogram)
5275 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5276 0, 0);
5277 }
5278 else if (cu->language == language_cplus)
5279 {
60430eff
DJ
5280 /* Assume that an artificial first parameter is
5281 "this", but do not crash if it is not. RealView
5282 marks unnamed (and thus unused) parameters as
5283 artificial; there is no way to differentiate
5284 the two cases. */
94af9270
KS
5285 if (TYPE_NFIELDS (type) > 0
5286 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5287 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5288 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5289 0))))
94af9270
KS
5290 fputs_unfiltered (" const", buf);
5291 }
5292 }
5293
5294 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5295 &length);
5296 ui_file_delete (buf);
5297
5298 if (cu->language == language_cplus)
5299 {
5300 char *cname
5301 = dwarf2_canonicalize_name (name, cu,
5302 &cu->objfile->objfile_obstack);
9a619af0 5303
94af9270
KS
5304 if (cname != NULL)
5305 name = cname;
5306 }
5307 }
5308 }
5309
5310 return name;
5311}
5312
0114d602
DJ
5313/* Return the fully qualified name of DIE, based on its DW_AT_name.
5314 If scope qualifiers are appropriate they will be added. The result
5315 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5316 not have a name. NAME may either be from a previous call to
5317 dwarf2_name or NULL.
5318
0963b4bd 5319 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5320
5321static const char *
94af9270 5322dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5323{
94af9270
KS
5324 return dwarf2_compute_name (name, die, cu, 0);
5325}
0114d602 5326
94af9270
KS
5327/* Construct a physname for the given DIE in CU. NAME may either be
5328 from a previous call to dwarf2_name or NULL. The result will be
5329 allocated on the objfile_objstack or NULL if the DIE does not have a
5330 name.
0114d602 5331
94af9270 5332 The output string will be canonicalized (if C++/Java). */
0114d602 5333
94af9270
KS
5334static const char *
5335dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5336{
900e11f9
JK
5337 struct attribute *attr;
5338 const char *retval, *mangled = NULL, *canon = NULL;
5339 struct cleanup *back_to;
5340 int need_copy = 1;
5341
5342 /* In this case dwarf2_compute_name is just a shortcut not building anything
5343 on its own. */
5344 if (!die_needs_namespace (die, cu))
5345 return dwarf2_compute_name (name, die, cu, 1);
5346
5347 back_to = make_cleanup (null_cleanup, NULL);
5348
5349 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5350 if (!attr)
5351 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5352
5353 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5354 has computed. */
5355 if (attr && DW_STRING (attr))
5356 {
5357 char *demangled;
5358
5359 mangled = DW_STRING (attr);
5360
5361 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5362 type. It is easier for GDB users to search for such functions as
5363 `name(params)' than `long name(params)'. In such case the minimal
5364 symbol names do not match the full symbol names but for template
5365 functions there is never a need to look up their definition from their
5366 declaration so the only disadvantage remains the minimal symbol
5367 variant `long name(params)' does not have the proper inferior type.
5368 */
5369
5370 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5371 | (cu->language == language_java
5372 ? DMGL_JAVA | DMGL_RET_POSTFIX
5373 : DMGL_RET_DROP)));
5374 if (demangled)
5375 {
5376 make_cleanup (xfree, demangled);
5377 canon = demangled;
5378 }
5379 else
5380 {
5381 canon = mangled;
5382 need_copy = 0;
5383 }
5384 }
5385
5386 if (canon == NULL || check_physname)
5387 {
5388 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5389
5390 if (canon != NULL && strcmp (physname, canon) != 0)
5391 {
5392 /* It may not mean a bug in GDB. The compiler could also
5393 compute DW_AT_linkage_name incorrectly. But in such case
5394 GDB would need to be bug-to-bug compatible. */
5395
5396 complaint (&symfile_complaints,
5397 _("Computed physname <%s> does not match demangled <%s> "
5398 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5399 physname, canon, mangled, die->offset, cu->objfile->name);
5400
5401 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5402 is available here - over computed PHYSNAME. It is safer
5403 against both buggy GDB and buggy compilers. */
5404
5405 retval = canon;
5406 }
5407 else
5408 {
5409 retval = physname;
5410 need_copy = 0;
5411 }
5412 }
5413 else
5414 retval = canon;
5415
5416 if (need_copy)
5417 retval = obsavestring (retval, strlen (retval),
5418 &cu->objfile->objfile_obstack);
5419
5420 do_cleanups (back_to);
5421 return retval;
0114d602
DJ
5422}
5423
27aa8d6a
SW
5424/* Read the import statement specified by the given die and record it. */
5425
5426static void
5427read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5428{
5429 struct attribute *import_attr;
32019081 5430 struct die_info *imported_die, *child_die;
de4affc9 5431 struct dwarf2_cu *imported_cu;
27aa8d6a 5432 const char *imported_name;
794684b6 5433 const char *imported_name_prefix;
13387711
SW
5434 const char *canonical_name;
5435 const char *import_alias;
5436 const char *imported_declaration = NULL;
794684b6 5437 const char *import_prefix;
32019081
JK
5438 VEC (const_char_ptr) *excludes = NULL;
5439 struct cleanup *cleanups;
13387711
SW
5440
5441 char *temp;
27aa8d6a
SW
5442
5443 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5444 if (import_attr == NULL)
5445 {
5446 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5447 dwarf_tag_name (die->tag));
5448 return;
5449 }
5450
de4affc9
CC
5451 imported_cu = cu;
5452 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5453 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5454 if (imported_name == NULL)
5455 {
5456 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5457
5458 The import in the following code:
5459 namespace A
5460 {
5461 typedef int B;
5462 }
5463
5464 int main ()
5465 {
5466 using A::B;
5467 B b;
5468 return b;
5469 }
5470
5471 ...
5472 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5473 <52> DW_AT_decl_file : 1
5474 <53> DW_AT_decl_line : 6
5475 <54> DW_AT_import : <0x75>
5476 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5477 <59> DW_AT_name : B
5478 <5b> DW_AT_decl_file : 1
5479 <5c> DW_AT_decl_line : 2
5480 <5d> DW_AT_type : <0x6e>
5481 ...
5482 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5483 <76> DW_AT_byte_size : 4
5484 <77> DW_AT_encoding : 5 (signed)
5485
5486 imports the wrong die ( 0x75 instead of 0x58 ).
5487 This case will be ignored until the gcc bug is fixed. */
5488 return;
5489 }
5490
82856980
SW
5491 /* Figure out the local name after import. */
5492 import_alias = dwarf2_name (die, cu);
27aa8d6a 5493
794684b6
SW
5494 /* Figure out where the statement is being imported to. */
5495 import_prefix = determine_prefix (die, cu);
5496
5497 /* Figure out what the scope of the imported die is and prepend it
5498 to the name of the imported die. */
de4affc9 5499 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5500
f55ee35c
JK
5501 if (imported_die->tag != DW_TAG_namespace
5502 && imported_die->tag != DW_TAG_module)
794684b6 5503 {
13387711
SW
5504 imported_declaration = imported_name;
5505 canonical_name = imported_name_prefix;
794684b6 5506 }
13387711 5507 else if (strlen (imported_name_prefix) > 0)
794684b6 5508 {
13387711
SW
5509 temp = alloca (strlen (imported_name_prefix)
5510 + 2 + strlen (imported_name) + 1);
5511 strcpy (temp, imported_name_prefix);
5512 strcat (temp, "::");
5513 strcat (temp, imported_name);
5514 canonical_name = temp;
794684b6 5515 }
13387711
SW
5516 else
5517 canonical_name = imported_name;
794684b6 5518
32019081
JK
5519 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5520
5521 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5522 for (child_die = die->child; child_die && child_die->tag;
5523 child_die = sibling_die (child_die))
5524 {
5525 /* DWARF-4: A Fortran use statement with a “rename list” may be
5526 represented by an imported module entry with an import attribute
5527 referring to the module and owned entries corresponding to those
5528 entities that are renamed as part of being imported. */
5529
5530 if (child_die->tag != DW_TAG_imported_declaration)
5531 {
5532 complaint (&symfile_complaints,
5533 _("child DW_TAG_imported_declaration expected "
5534 "- DIE at 0x%x [in module %s]"),
5535 child_die->offset, cu->objfile->name);
5536 continue;
5537 }
5538
5539 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5540 if (import_attr == NULL)
5541 {
5542 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5543 dwarf_tag_name (child_die->tag));
5544 continue;
5545 }
5546
5547 imported_cu = cu;
5548 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5549 &imported_cu);
5550 imported_name = dwarf2_name (imported_die, imported_cu);
5551 if (imported_name == NULL)
5552 {
5553 complaint (&symfile_complaints,
5554 _("child DW_TAG_imported_declaration has unknown "
5555 "imported name - DIE at 0x%x [in module %s]"),
5556 child_die->offset, cu->objfile->name);
5557 continue;
5558 }
5559
5560 VEC_safe_push (const_char_ptr, excludes, imported_name);
5561
5562 process_die (child_die, cu);
5563 }
5564
c0cc3a76
SW
5565 cp_add_using_directive (import_prefix,
5566 canonical_name,
5567 import_alias,
13387711 5568 imported_declaration,
32019081 5569 excludes,
c0cc3a76 5570 &cu->objfile->objfile_obstack);
32019081
JK
5571
5572 do_cleanups (cleanups);
27aa8d6a
SW
5573}
5574
ae2de4f8
DE
5575/* Cleanup function for read_file_scope. */
5576
cb1df416
DJ
5577static void
5578free_cu_line_header (void *arg)
5579{
5580 struct dwarf2_cu *cu = arg;
5581
5582 free_line_header (cu->line_header);
5583 cu->line_header = NULL;
5584}
5585
9291a0cd
TT
5586static void
5587find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5588 char **name, char **comp_dir)
5589{
5590 struct attribute *attr;
5591
5592 *name = NULL;
5593 *comp_dir = NULL;
5594
5595 /* Find the filename. Do not use dwarf2_name here, since the filename
5596 is not a source language identifier. */
5597 attr = dwarf2_attr (die, DW_AT_name, cu);
5598 if (attr)
5599 {
5600 *name = DW_STRING (attr);
5601 }
5602
5603 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5604 if (attr)
5605 *comp_dir = DW_STRING (attr);
5606 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5607 {
5608 *comp_dir = ldirname (*name);
5609 if (*comp_dir != NULL)
5610 make_cleanup (xfree, *comp_dir);
5611 }
5612 if (*comp_dir != NULL)
5613 {
5614 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5615 directory, get rid of it. */
5616 char *cp = strchr (*comp_dir, ':');
5617
5618 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5619 *comp_dir = cp + 1;
5620 }
5621
5622 if (*name == NULL)
5623 *name = "<unknown>";
5624}
5625
2ab95328
TT
5626/* Handle DW_AT_stmt_list for a compilation unit. */
5627
5628static void
5629handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5630 const char *comp_dir)
5631{
5632 struct attribute *attr;
5633 struct objfile *objfile = cu->objfile;
5634 bfd *abfd = objfile->obfd;
5635
5636 /* Decode line number information if present. We do this before
5637 processing child DIEs, so that the line header table is available
5638 for DW_AT_decl_file. */
5639 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5640 if (attr)
5641 {
5642 unsigned int line_offset = DW_UNSND (attr);
5643 struct line_header *line_header
5644 = dwarf_decode_line_header (line_offset, abfd, cu);
5645
5646 if (line_header)
5647 {
5648 cu->line_header = line_header;
5649 make_cleanup (free_cu_line_header, cu);
5650 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5651 }
5652 }
5653}
5654
ae2de4f8
DE
5655/* Process DW_TAG_compile_unit. */
5656
c906108c 5657static void
e7c27a73 5658read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5659{
e7c27a73 5660 struct objfile *objfile = cu->objfile;
debd256d 5661 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5662 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5663 CORE_ADDR highpc = ((CORE_ADDR) 0);
5664 struct attribute *attr;
e1024ff1 5665 char *name = NULL;
c906108c
SS
5666 char *comp_dir = NULL;
5667 struct die_info *child_die;
5668 bfd *abfd = objfile->obfd;
e142c38c 5669 CORE_ADDR baseaddr;
6e70227d 5670
e142c38c 5671 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5672
fae299cd 5673 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5674
5675 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5676 from finish_block. */
2acceee2 5677 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5678 lowpc = highpc;
5679 lowpc += baseaddr;
5680 highpc += baseaddr;
5681
9291a0cd 5682 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5683
e142c38c 5684 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5685 if (attr)
5686 {
e142c38c 5687 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5688 }
5689
b0f35d58 5690 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5691 if (attr)
b0f35d58 5692 cu->producer = DW_STRING (attr);
303b6f5d 5693
f4b8a18d
KW
5694 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5695 standardised yet. As a workaround for the language detection we fall
5696 back to the DW_AT_producer string. */
5697 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5698 cu->language = language_opencl;
5699
0963b4bd 5700 /* We assume that we're processing GCC output. */
c906108c 5701 processing_gcc_compilation = 2;
c906108c 5702
df8a16a1
DJ
5703 processing_has_namespace_info = 0;
5704
c906108c
SS
5705 start_symtab (name, comp_dir, lowpc);
5706 record_debugformat ("DWARF 2");
303b6f5d 5707 record_producer (cu->producer);
c906108c 5708
2ab95328 5709 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5710
cb1df416
DJ
5711 /* Process all dies in compilation unit. */
5712 if (die->child != NULL)
5713 {
5714 child_die = die->child;
5715 while (child_die && child_die->tag)
5716 {
5717 process_die (child_die, cu);
5718 child_die = sibling_die (child_die);
5719 }
5720 }
5721
2e276125
JB
5722 /* Decode macro information, if present. Dwarf 2 macro information
5723 refers to information in the line number info statement program
5724 header, so we can only read it if we've read the header
5725 successfully. */
cf2c3c16 5726 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5727 if (attr && cu->line_header)
2e276125 5728 {
cf2c3c16
TT
5729 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5730 complaint (&symfile_complaints,
5731 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5732
5733 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5734 comp_dir, abfd, cu,
5735 &dwarf2_per_objfile->macro, 1);
5736 }
5737 else
5738 {
5739 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5740 if (attr && cu->line_header)
5741 {
5742 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5743
cf2c3c16
TT
5744 dwarf_decode_macros (cu->line_header, macro_offset,
5745 comp_dir, abfd, cu,
5746 &dwarf2_per_objfile->macinfo, 0);
5747 }
2e276125 5748 }
9cdd5dbd 5749
debd256d 5750 do_cleanups (back_to);
5fb290d7
DJ
5751}
5752
ae2de4f8
DE
5753/* Process DW_TAG_type_unit.
5754 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5755 actual type being defined by this TU. In this case the first top
5756 level sibling is there to provide context only. */
5757
5758static void
5759read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5760{
5761 struct objfile *objfile = cu->objfile;
5762 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5763 CORE_ADDR lowpc;
5764 struct attribute *attr;
5765 char *name = NULL;
5766 char *comp_dir = NULL;
5767 struct die_info *child_die;
5768 bfd *abfd = objfile->obfd;
348e048f
DE
5769
5770 /* start_symtab needs a low pc, but we don't really have one.
5771 Do what read_file_scope would do in the absence of such info. */
5772 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5773
5774 /* Find the filename. Do not use dwarf2_name here, since the filename
5775 is not a source language identifier. */
5776 attr = dwarf2_attr (die, DW_AT_name, cu);
5777 if (attr)
5778 name = DW_STRING (attr);
5779
5780 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5781 if (attr)
5782 comp_dir = DW_STRING (attr);
5783 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5784 {
5785 comp_dir = ldirname (name);
5786 if (comp_dir != NULL)
5787 make_cleanup (xfree, comp_dir);
5788 }
5789
5790 if (name == NULL)
5791 name = "<unknown>";
5792
5793 attr = dwarf2_attr (die, DW_AT_language, cu);
5794 if (attr)
5795 set_cu_language (DW_UNSND (attr), cu);
5796
5797 /* This isn't technically needed today. It is done for symmetry
5798 with read_file_scope. */
5799 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5800 if (attr)
348e048f
DE
5801 cu->producer = DW_STRING (attr);
5802
0963b4bd 5803 /* We assume that we're processing GCC output. */
348e048f
DE
5804 processing_gcc_compilation = 2;
5805
5806 processing_has_namespace_info = 0;
5807
5808 start_symtab (name, comp_dir, lowpc);
5809 record_debugformat ("DWARF 2");
5810 record_producer (cu->producer);
5811
2ab95328
TT
5812 handle_DW_AT_stmt_list (die, cu, comp_dir);
5813
348e048f
DE
5814 /* Process the dies in the type unit. */
5815 if (die->child == NULL)
5816 {
5817 dump_die_for_error (die);
5818 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5819 bfd_get_filename (abfd));
5820 }
5821
5822 child_die = die->child;
5823
5824 while (child_die && child_die->tag)
5825 {
5826 process_die (child_die, cu);
5827
5828 child_die = sibling_die (child_die);
5829 }
5830
5831 do_cleanups (back_to);
5832}
5833
d389af10
JK
5834/* qsort helper for inherit_abstract_dies. */
5835
5836static int
5837unsigned_int_compar (const void *ap, const void *bp)
5838{
5839 unsigned int a = *(unsigned int *) ap;
5840 unsigned int b = *(unsigned int *) bp;
5841
5842 return (a > b) - (b > a);
5843}
5844
5845/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5846 Inherit only the children of the DW_AT_abstract_origin DIE not being
5847 already referenced by DW_AT_abstract_origin from the children of the
5848 current DIE. */
d389af10
JK
5849
5850static void
5851inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5852{
5853 struct die_info *child_die;
5854 unsigned die_children_count;
5855 /* CU offsets which were referenced by children of the current DIE. */
5856 unsigned *offsets;
5857 unsigned *offsets_end, *offsetp;
5858 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5859 struct die_info *origin_die;
5860 /* Iterator of the ORIGIN_DIE children. */
5861 struct die_info *origin_child_die;
5862 struct cleanup *cleanups;
5863 struct attribute *attr;
cd02d79d
PA
5864 struct dwarf2_cu *origin_cu;
5865 struct pending **origin_previous_list_in_scope;
d389af10
JK
5866
5867 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5868 if (!attr)
5869 return;
5870
cd02d79d
PA
5871 /* Note that following die references may follow to a die in a
5872 different cu. */
5873
5874 origin_cu = cu;
5875 origin_die = follow_die_ref (die, attr, &origin_cu);
5876
5877 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5878 symbols in. */
5879 origin_previous_list_in_scope = origin_cu->list_in_scope;
5880 origin_cu->list_in_scope = cu->list_in_scope;
5881
edb3359d
DJ
5882 if (die->tag != origin_die->tag
5883 && !(die->tag == DW_TAG_inlined_subroutine
5884 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5885 complaint (&symfile_complaints,
5886 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5887 die->offset, origin_die->offset);
5888
5889 child_die = die->child;
5890 die_children_count = 0;
5891 while (child_die && child_die->tag)
5892 {
5893 child_die = sibling_die (child_die);
5894 die_children_count++;
5895 }
5896 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5897 cleanups = make_cleanup (xfree, offsets);
5898
5899 offsets_end = offsets;
5900 child_die = die->child;
5901 while (child_die && child_die->tag)
5902 {
c38f313d
DJ
5903 /* For each CHILD_DIE, find the corresponding child of
5904 ORIGIN_DIE. If there is more than one layer of
5905 DW_AT_abstract_origin, follow them all; there shouldn't be,
5906 but GCC versions at least through 4.4 generate this (GCC PR
5907 40573). */
5908 struct die_info *child_origin_die = child_die;
cd02d79d 5909 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5910
c38f313d
DJ
5911 while (1)
5912 {
cd02d79d
PA
5913 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5914 child_origin_cu);
c38f313d
DJ
5915 if (attr == NULL)
5916 break;
cd02d79d
PA
5917 child_origin_die = follow_die_ref (child_origin_die, attr,
5918 &child_origin_cu);
c38f313d
DJ
5919 }
5920
d389af10
JK
5921 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5922 counterpart may exist. */
c38f313d 5923 if (child_origin_die != child_die)
d389af10 5924 {
edb3359d
DJ
5925 if (child_die->tag != child_origin_die->tag
5926 && !(child_die->tag == DW_TAG_inlined_subroutine
5927 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5928 complaint (&symfile_complaints,
5929 _("Child DIE 0x%x and its abstract origin 0x%x have "
5930 "different tags"), child_die->offset,
5931 child_origin_die->offset);
c38f313d
DJ
5932 if (child_origin_die->parent != origin_die)
5933 complaint (&symfile_complaints,
5934 _("Child DIE 0x%x and its abstract origin 0x%x have "
5935 "different parents"), child_die->offset,
5936 child_origin_die->offset);
5937 else
5938 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5939 }
5940 child_die = sibling_die (child_die);
5941 }
5942 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5943 unsigned_int_compar);
5944 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5945 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5946 complaint (&symfile_complaints,
5947 _("Multiple children of DIE 0x%x refer "
5948 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5949 die->offset, *offsetp);
5950
5951 offsetp = offsets;
5952 origin_child_die = origin_die->child;
5953 while (origin_child_die && origin_child_die->tag)
5954 {
5955 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5956 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5957 offsetp++;
5958 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5959 {
5960 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5961 process_die (origin_child_die, origin_cu);
d389af10
JK
5962 }
5963 origin_child_die = sibling_die (origin_child_die);
5964 }
cd02d79d 5965 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5966
5967 do_cleanups (cleanups);
5968}
5969
c906108c 5970static void
e7c27a73 5971read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5972{
e7c27a73 5973 struct objfile *objfile = cu->objfile;
52f0bd74 5974 struct context_stack *new;
c906108c
SS
5975 CORE_ADDR lowpc;
5976 CORE_ADDR highpc;
5977 struct die_info *child_die;
edb3359d 5978 struct attribute *attr, *call_line, *call_file;
c906108c 5979 char *name;
e142c38c 5980 CORE_ADDR baseaddr;
801e3a5b 5981 struct block *block;
edb3359d 5982 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5983 VEC (symbolp) *template_args = NULL;
5984 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5985
5986 if (inlined_func)
5987 {
5988 /* If we do not have call site information, we can't show the
5989 caller of this inlined function. That's too confusing, so
5990 only use the scope for local variables. */
5991 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5992 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5993 if (call_line == NULL || call_file == NULL)
5994 {
5995 read_lexical_block_scope (die, cu);
5996 return;
5997 }
5998 }
c906108c 5999
e142c38c
DJ
6000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6001
94af9270 6002 name = dwarf2_name (die, cu);
c906108c 6003
e8d05480
JB
6004 /* Ignore functions with missing or empty names. These are actually
6005 illegal according to the DWARF standard. */
6006 if (name == NULL)
6007 {
6008 complaint (&symfile_complaints,
6009 _("missing name for subprogram DIE at %d"), die->offset);
6010 return;
6011 }
6012
6013 /* Ignore functions with missing or invalid low and high pc attributes. */
6014 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6015 {
ae4d0c03
PM
6016 attr = dwarf2_attr (die, DW_AT_external, cu);
6017 if (!attr || !DW_UNSND (attr))
6018 complaint (&symfile_complaints,
3e43a32a
MS
6019 _("cannot get low and high bounds "
6020 "for subprogram DIE at %d"),
ae4d0c03 6021 die->offset);
e8d05480
JB
6022 return;
6023 }
c906108c
SS
6024
6025 lowpc += baseaddr;
6026 highpc += baseaddr;
6027
34eaf542
TT
6028 /* If we have any template arguments, then we must allocate a
6029 different sort of symbol. */
6030 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6031 {
6032 if (child_die->tag == DW_TAG_template_type_param
6033 || child_die->tag == DW_TAG_template_value_param)
6034 {
6035 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6036 struct template_symbol);
6037 templ_func->base.is_cplus_template_function = 1;
6038 break;
6039 }
6040 }
6041
c906108c 6042 new = push_context (0, lowpc);
34eaf542
TT
6043 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6044 (struct symbol *) templ_func);
4c2df51b 6045
4cecd739
DJ
6046 /* If there is a location expression for DW_AT_frame_base, record
6047 it. */
e142c38c 6048 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6049 if (attr)
c034e007
AC
6050 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6051 expression is being recorded directly in the function's symbol
6052 and not in a separate frame-base object. I guess this hack is
6053 to avoid adding some sort of frame-base adjunct/annex to the
6054 function's symbol :-(. The problem with doing this is that it
6055 results in a function symbol with a location expression that
6056 has nothing to do with the location of the function, ouch! The
6057 relationship should be: a function's symbol has-a frame base; a
6058 frame-base has-a location expression. */
e7c27a73 6059 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6060
e142c38c 6061 cu->list_in_scope = &local_symbols;
c906108c 6062
639d11d3 6063 if (die->child != NULL)
c906108c 6064 {
639d11d3 6065 child_die = die->child;
c906108c
SS
6066 while (child_die && child_die->tag)
6067 {
34eaf542
TT
6068 if (child_die->tag == DW_TAG_template_type_param
6069 || child_die->tag == DW_TAG_template_value_param)
6070 {
6071 struct symbol *arg = new_symbol (child_die, NULL, cu);
6072
f1078f66
DJ
6073 if (arg != NULL)
6074 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6075 }
6076 else
6077 process_die (child_die, cu);
c906108c
SS
6078 child_die = sibling_die (child_die);
6079 }
6080 }
6081
d389af10
JK
6082 inherit_abstract_dies (die, cu);
6083
4a811a97
UW
6084 /* If we have a DW_AT_specification, we might need to import using
6085 directives from the context of the specification DIE. See the
6086 comment in determine_prefix. */
6087 if (cu->language == language_cplus
6088 && dwarf2_attr (die, DW_AT_specification, cu))
6089 {
6090 struct dwarf2_cu *spec_cu = cu;
6091 struct die_info *spec_die = die_specification (die, &spec_cu);
6092
6093 while (spec_die)
6094 {
6095 child_die = spec_die->child;
6096 while (child_die && child_die->tag)
6097 {
6098 if (child_die->tag == DW_TAG_imported_module)
6099 process_die (child_die, spec_cu);
6100 child_die = sibling_die (child_die);
6101 }
6102
6103 /* In some cases, GCC generates specification DIEs that
6104 themselves contain DW_AT_specification attributes. */
6105 spec_die = die_specification (spec_die, &spec_cu);
6106 }
6107 }
6108
c906108c
SS
6109 new = pop_context ();
6110 /* Make a block for the local symbols within. */
801e3a5b
JB
6111 block = finish_block (new->name, &local_symbols, new->old_blocks,
6112 lowpc, highpc, objfile);
6113
df8a16a1 6114 /* For C++, set the block's scope. */
f55ee35c 6115 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6116 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6117 determine_prefix (die, cu),
df8a16a1
DJ
6118 processing_has_namespace_info);
6119
801e3a5b
JB
6120 /* If we have address ranges, record them. */
6121 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6122
34eaf542
TT
6123 /* Attach template arguments to function. */
6124 if (! VEC_empty (symbolp, template_args))
6125 {
6126 gdb_assert (templ_func != NULL);
6127
6128 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6129 templ_func->template_arguments
6130 = obstack_alloc (&objfile->objfile_obstack,
6131 (templ_func->n_template_arguments
6132 * sizeof (struct symbol *)));
6133 memcpy (templ_func->template_arguments,
6134 VEC_address (symbolp, template_args),
6135 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6136 VEC_free (symbolp, template_args);
6137 }
6138
208d8187
JB
6139 /* In C++, we can have functions nested inside functions (e.g., when
6140 a function declares a class that has methods). This means that
6141 when we finish processing a function scope, we may need to go
6142 back to building a containing block's symbol lists. */
6143 local_symbols = new->locals;
6144 param_symbols = new->params;
27aa8d6a 6145 using_directives = new->using_directives;
208d8187 6146
921e78cf
JB
6147 /* If we've finished processing a top-level function, subsequent
6148 symbols go in the file symbol list. */
6149 if (outermost_context_p ())
e142c38c 6150 cu->list_in_scope = &file_symbols;
c906108c
SS
6151}
6152
6153/* Process all the DIES contained within a lexical block scope. Start
6154 a new scope, process the dies, and then close the scope. */
6155
6156static void
e7c27a73 6157read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6158{
e7c27a73 6159 struct objfile *objfile = cu->objfile;
52f0bd74 6160 struct context_stack *new;
c906108c
SS
6161 CORE_ADDR lowpc, highpc;
6162 struct die_info *child_die;
e142c38c
DJ
6163 CORE_ADDR baseaddr;
6164
6165 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6166
6167 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6168 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6169 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6170 be nasty. Might be easier to properly extend generic blocks to
af34e669 6171 describe ranges. */
d85a05f0 6172 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6173 return;
6174 lowpc += baseaddr;
6175 highpc += baseaddr;
6176
6177 push_context (0, lowpc);
639d11d3 6178 if (die->child != NULL)
c906108c 6179 {
639d11d3 6180 child_die = die->child;
c906108c
SS
6181 while (child_die && child_die->tag)
6182 {
e7c27a73 6183 process_die (child_die, cu);
c906108c
SS
6184 child_die = sibling_die (child_die);
6185 }
6186 }
6187 new = pop_context ();
6188
8540c487 6189 if (local_symbols != NULL || using_directives != NULL)
c906108c 6190 {
801e3a5b
JB
6191 struct block *block
6192 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6193 highpc, objfile);
6194
6195 /* Note that recording ranges after traversing children, as we
6196 do here, means that recording a parent's ranges entails
6197 walking across all its children's ranges as they appear in
6198 the address map, which is quadratic behavior.
6199
6200 It would be nicer to record the parent's ranges before
6201 traversing its children, simply overriding whatever you find
6202 there. But since we don't even decide whether to create a
6203 block until after we've traversed its children, that's hard
6204 to do. */
6205 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6206 }
6207 local_symbols = new->locals;
27aa8d6a 6208 using_directives = new->using_directives;
c906108c
SS
6209}
6210
96408a79
SA
6211/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6212
6213static void
6214read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6215{
6216 struct objfile *objfile = cu->objfile;
6217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6218 CORE_ADDR pc, baseaddr;
6219 struct attribute *attr;
6220 struct call_site *call_site, call_site_local;
6221 void **slot;
6222 int nparams;
6223 struct die_info *child_die;
6224
6225 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6226
6227 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6228 if (!attr)
6229 {
6230 complaint (&symfile_complaints,
6231 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6232 "DIE 0x%x [in module %s]"),
6233 die->offset, cu->objfile->name);
6234 return;
6235 }
6236 pc = DW_ADDR (attr) + baseaddr;
6237
6238 if (cu->call_site_htab == NULL)
6239 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6240 NULL, &objfile->objfile_obstack,
6241 hashtab_obstack_allocate, NULL);
6242 call_site_local.pc = pc;
6243 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6244 if (*slot != NULL)
6245 {
6246 complaint (&symfile_complaints,
6247 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6248 "DIE 0x%x [in module %s]"),
6249 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6250 return;
6251 }
6252
6253 /* Count parameters at the caller. */
6254
6255 nparams = 0;
6256 for (child_die = die->child; child_die && child_die->tag;
6257 child_die = sibling_die (child_die))
6258 {
6259 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6260 {
6261 complaint (&symfile_complaints,
6262 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6263 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6264 child_die->tag, child_die->offset, cu->objfile->name);
6265 continue;
6266 }
6267
6268 nparams++;
6269 }
6270
6271 call_site = obstack_alloc (&objfile->objfile_obstack,
6272 (sizeof (*call_site)
6273 + (sizeof (*call_site->parameter)
6274 * (nparams - 1))));
6275 *slot = call_site;
6276 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6277 call_site->pc = pc;
6278
6279 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6280 {
6281 struct die_info *func_die;
6282
6283 /* Skip also over DW_TAG_inlined_subroutine. */
6284 for (func_die = die->parent;
6285 func_die && func_die->tag != DW_TAG_subprogram
6286 && func_die->tag != DW_TAG_subroutine_type;
6287 func_die = func_die->parent);
6288
6289 /* DW_AT_GNU_all_call_sites is a superset
6290 of DW_AT_GNU_all_tail_call_sites. */
6291 if (func_die
6292 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6293 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6294 {
6295 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6296 not complete. But keep CALL_SITE for look ups via call_site_htab,
6297 both the initial caller containing the real return address PC and
6298 the final callee containing the current PC of a chain of tail
6299 calls do not need to have the tail call list complete. But any
6300 function candidate for a virtual tail call frame searched via
6301 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6302 determined unambiguously. */
6303 }
6304 else
6305 {
6306 struct type *func_type = NULL;
6307
6308 if (func_die)
6309 func_type = get_die_type (func_die, cu);
6310 if (func_type != NULL)
6311 {
6312 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6313
6314 /* Enlist this call site to the function. */
6315 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6316 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6317 }
6318 else
6319 complaint (&symfile_complaints,
6320 _("Cannot find function owning DW_TAG_GNU_call_site "
6321 "DIE 0x%x [in module %s]"),
6322 die->offset, cu->objfile->name);
6323 }
6324 }
6325
6326 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6327 if (attr == NULL)
6328 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6329 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6330 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6331 /* Keep NULL DWARF_BLOCK. */;
6332 else if (attr_form_is_block (attr))
6333 {
6334 struct dwarf2_locexpr_baton *dlbaton;
6335
6336 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6337 dlbaton->data = DW_BLOCK (attr)->data;
6338 dlbaton->size = DW_BLOCK (attr)->size;
6339 dlbaton->per_cu = cu->per_cu;
6340
6341 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6342 }
6343 else if (is_ref_attr (attr))
6344 {
6345 struct objfile *objfile = cu->objfile;
6346 struct dwarf2_cu *target_cu = cu;
6347 struct die_info *target_die;
6348
6349 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6350 gdb_assert (target_cu->objfile == objfile);
6351 if (die_is_declaration (target_die, target_cu))
6352 {
6353 const char *target_physname;
6354
6355 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6356 if (target_physname == NULL)
6357 complaint (&symfile_complaints,
6358 _("DW_AT_GNU_call_site_target target DIE has invalid "
6359 "physname, for referencing DIE 0x%x [in module %s]"),
6360 die->offset, cu->objfile->name);
6361 else
6362 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6363 }
6364 else
6365 {
6366 CORE_ADDR lowpc;
6367
6368 /* DW_AT_entry_pc should be preferred. */
6369 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6370 complaint (&symfile_complaints,
6371 _("DW_AT_GNU_call_site_target target DIE has invalid "
6372 "low pc, for referencing DIE 0x%x [in module %s]"),
6373 die->offset, cu->objfile->name);
6374 else
6375 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6376 }
6377 }
6378 else
6379 complaint (&symfile_complaints,
6380 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6381 "block nor reference, for DIE 0x%x [in module %s]"),
6382 die->offset, cu->objfile->name);
6383
6384 call_site->per_cu = cu->per_cu;
6385
6386 for (child_die = die->child;
6387 child_die && child_die->tag;
6388 child_die = sibling_die (child_die))
6389 {
6390 struct dwarf2_locexpr_baton *dlbaton;
6391 struct call_site_parameter *parameter;
6392
6393 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6394 {
6395 /* Already printed the complaint above. */
6396 continue;
6397 }
6398
6399 gdb_assert (call_site->parameter_count < nparams);
6400 parameter = &call_site->parameter[call_site->parameter_count];
6401
6402 /* DW_AT_location specifies the register number. Value of the data
6403 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6404
6405 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6406 if (!attr || !attr_form_is_block (attr))
6407 {
6408 complaint (&symfile_complaints,
6409 _("No DW_FORM_block* DW_AT_location for "
6410 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6411 child_die->offset, cu->objfile->name);
6412 continue;
6413 }
6414 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6415 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6416 if (parameter->dwarf_reg == -1
6417 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6418 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6419 &parameter->fb_offset))
6420 {
6421 complaint (&symfile_complaints,
6422 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6423 "for DW_FORM_block* DW_AT_location for "
6424 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6425 child_die->offset, cu->objfile->name);
6426 continue;
6427 }
6428
6429 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6430 if (!attr_form_is_block (attr))
6431 {
6432 complaint (&symfile_complaints,
6433 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6434 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6435 child_die->offset, cu->objfile->name);
6436 continue;
6437 }
6438 parameter->value = DW_BLOCK (attr)->data;
6439 parameter->value_size = DW_BLOCK (attr)->size;
6440
6441 /* Parameters are not pre-cleared by memset above. */
6442 parameter->data_value = NULL;
6443 parameter->data_value_size = 0;
6444 call_site->parameter_count++;
6445
6446 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6447 if (attr)
6448 {
6449 if (!attr_form_is_block (attr))
6450 complaint (&symfile_complaints,
6451 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6452 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6453 child_die->offset, cu->objfile->name);
6454 else
6455 {
6456 parameter->data_value = DW_BLOCK (attr)->data;
6457 parameter->data_value_size = DW_BLOCK (attr)->size;
6458 }
6459 }
6460 }
6461}
6462
43039443 6463/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6464 Return 1 if the attributes are present and valid, otherwise, return 0.
6465 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6466
6467static int
6468dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6469 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6470 struct partial_symtab *ranges_pst)
43039443
JK
6471{
6472 struct objfile *objfile = cu->objfile;
6473 struct comp_unit_head *cu_header = &cu->header;
6474 bfd *obfd = objfile->obfd;
6475 unsigned int addr_size = cu_header->addr_size;
6476 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6477 /* Base address selection entry. */
6478 CORE_ADDR base;
6479 int found_base;
6480 unsigned int dummy;
6481 gdb_byte *buffer;
6482 CORE_ADDR marker;
6483 int low_set;
6484 CORE_ADDR low = 0;
6485 CORE_ADDR high = 0;
ff013f42 6486 CORE_ADDR baseaddr;
43039443 6487
d00adf39
DE
6488 found_base = cu->base_known;
6489 base = cu->base_address;
43039443 6490
be391dca 6491 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6492 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6493 {
6494 complaint (&symfile_complaints,
6495 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6496 offset);
6497 return 0;
6498 }
dce234bc 6499 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6500
6501 /* Read in the largest possible address. */
6502 marker = read_address (obfd, buffer, cu, &dummy);
6503 if ((marker & mask) == mask)
6504 {
6505 /* If we found the largest possible address, then
6506 read the base address. */
6507 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6508 buffer += 2 * addr_size;
6509 offset += 2 * addr_size;
6510 found_base = 1;
6511 }
6512
6513 low_set = 0;
6514
e7030f15 6515 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6516
43039443
JK
6517 while (1)
6518 {
6519 CORE_ADDR range_beginning, range_end;
6520
6521 range_beginning = read_address (obfd, buffer, cu, &dummy);
6522 buffer += addr_size;
6523 range_end = read_address (obfd, buffer, cu, &dummy);
6524 buffer += addr_size;
6525 offset += 2 * addr_size;
6526
6527 /* An end of list marker is a pair of zero addresses. */
6528 if (range_beginning == 0 && range_end == 0)
6529 /* Found the end of list entry. */
6530 break;
6531
6532 /* Each base address selection entry is a pair of 2 values.
6533 The first is the largest possible address, the second is
6534 the base address. Check for a base address here. */
6535 if ((range_beginning & mask) == mask)
6536 {
6537 /* If we found the largest possible address, then
6538 read the base address. */
6539 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6540 found_base = 1;
6541 continue;
6542 }
6543
6544 if (!found_base)
6545 {
6546 /* We have no valid base address for the ranges
6547 data. */
6548 complaint (&symfile_complaints,
6549 _("Invalid .debug_ranges data (no base address)"));
6550 return 0;
6551 }
6552
9277c30c
UW
6553 if (range_beginning > range_end)
6554 {
6555 /* Inverted range entries are invalid. */
6556 complaint (&symfile_complaints,
6557 _("Invalid .debug_ranges data (inverted range)"));
6558 return 0;
6559 }
6560
6561 /* Empty range entries have no effect. */
6562 if (range_beginning == range_end)
6563 continue;
6564
43039443
JK
6565 range_beginning += base;
6566 range_end += base;
6567
9277c30c 6568 if (ranges_pst != NULL)
ff013f42 6569 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6570 range_beginning + baseaddr,
6571 range_end - 1 + baseaddr,
ff013f42
JK
6572 ranges_pst);
6573
43039443
JK
6574 /* FIXME: This is recording everything as a low-high
6575 segment of consecutive addresses. We should have a
6576 data structure for discontiguous block ranges
6577 instead. */
6578 if (! low_set)
6579 {
6580 low = range_beginning;
6581 high = range_end;
6582 low_set = 1;
6583 }
6584 else
6585 {
6586 if (range_beginning < low)
6587 low = range_beginning;
6588 if (range_end > high)
6589 high = range_end;
6590 }
6591 }
6592
6593 if (! low_set)
6594 /* If the first entry is an end-of-list marker, the range
6595 describes an empty scope, i.e. no instructions. */
6596 return 0;
6597
6598 if (low_return)
6599 *low_return = low;
6600 if (high_return)
6601 *high_return = high;
6602 return 1;
6603}
6604
af34e669
DJ
6605/* Get low and high pc attributes from a die. Return 1 if the attributes
6606 are present and valid, otherwise, return 0. Return -1 if the range is
6607 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6608static int
af34e669 6609dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6610 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6611 struct partial_symtab *pst)
c906108c
SS
6612{
6613 struct attribute *attr;
af34e669
DJ
6614 CORE_ADDR low = 0;
6615 CORE_ADDR high = 0;
6616 int ret = 0;
c906108c 6617
e142c38c 6618 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6619 if (attr)
af34e669
DJ
6620 {
6621 high = DW_ADDR (attr);
e142c38c 6622 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6623 if (attr)
6624 low = DW_ADDR (attr);
6625 else
6626 /* Found high w/o low attribute. */
6627 return 0;
6628
6629 /* Found consecutive range of addresses. */
6630 ret = 1;
6631 }
c906108c 6632 else
af34e669 6633 {
e142c38c 6634 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6635 if (attr != NULL)
6636 {
af34e669 6637 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6638 .debug_ranges section. */
d85a05f0 6639 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6640 return 0;
43039443 6641 /* Found discontinuous range of addresses. */
af34e669
DJ
6642 ret = -1;
6643 }
6644 }
c906108c 6645
9373cf26
JK
6646 /* read_partial_die has also the strict LOW < HIGH requirement. */
6647 if (high <= low)
c906108c
SS
6648 return 0;
6649
6650 /* When using the GNU linker, .gnu.linkonce. sections are used to
6651 eliminate duplicate copies of functions and vtables and such.
6652 The linker will arbitrarily choose one and discard the others.
6653 The AT_*_pc values for such functions refer to local labels in
6654 these sections. If the section from that file was discarded, the
6655 labels are not in the output, so the relocs get a value of 0.
6656 If this is a discarded function, mark the pc bounds as invalid,
6657 so that GDB will ignore it. */
72dca2f5 6658 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6659 return 0;
6660
6661 *lowpc = low;
96408a79
SA
6662 if (highpc)
6663 *highpc = high;
af34e669 6664 return ret;
c906108c
SS
6665}
6666
b084d499
JB
6667/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6668 its low and high PC addresses. Do nothing if these addresses could not
6669 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6670 and HIGHPC to the high address if greater than HIGHPC. */
6671
6672static void
6673dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6674 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6675 struct dwarf2_cu *cu)
6676{
6677 CORE_ADDR low, high;
6678 struct die_info *child = die->child;
6679
d85a05f0 6680 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6681 {
6682 *lowpc = min (*lowpc, low);
6683 *highpc = max (*highpc, high);
6684 }
6685
6686 /* If the language does not allow nested subprograms (either inside
6687 subprograms or lexical blocks), we're done. */
6688 if (cu->language != language_ada)
6689 return;
6e70227d 6690
b084d499
JB
6691 /* Check all the children of the given DIE. If it contains nested
6692 subprograms, then check their pc bounds. Likewise, we need to
6693 check lexical blocks as well, as they may also contain subprogram
6694 definitions. */
6695 while (child && child->tag)
6696 {
6697 if (child->tag == DW_TAG_subprogram
6698 || child->tag == DW_TAG_lexical_block)
6699 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6700 child = sibling_die (child);
6701 }
6702}
6703
fae299cd
DC
6704/* Get the low and high pc's represented by the scope DIE, and store
6705 them in *LOWPC and *HIGHPC. If the correct values can't be
6706 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6707
6708static void
6709get_scope_pc_bounds (struct die_info *die,
6710 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6711 struct dwarf2_cu *cu)
6712{
6713 CORE_ADDR best_low = (CORE_ADDR) -1;
6714 CORE_ADDR best_high = (CORE_ADDR) 0;
6715 CORE_ADDR current_low, current_high;
6716
d85a05f0 6717 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6718 {
6719 best_low = current_low;
6720 best_high = current_high;
6721 }
6722 else
6723 {
6724 struct die_info *child = die->child;
6725
6726 while (child && child->tag)
6727 {
6728 switch (child->tag) {
6729 case DW_TAG_subprogram:
b084d499 6730 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6731 break;
6732 case DW_TAG_namespace:
f55ee35c 6733 case DW_TAG_module:
fae299cd
DC
6734 /* FIXME: carlton/2004-01-16: Should we do this for
6735 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6736 that current GCC's always emit the DIEs corresponding
6737 to definitions of methods of classes as children of a
6738 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6739 the DIEs giving the declarations, which could be
6740 anywhere). But I don't see any reason why the
6741 standards says that they have to be there. */
6742 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6743
6744 if (current_low != ((CORE_ADDR) -1))
6745 {
6746 best_low = min (best_low, current_low);
6747 best_high = max (best_high, current_high);
6748 }
6749 break;
6750 default:
0963b4bd 6751 /* Ignore. */
fae299cd
DC
6752 break;
6753 }
6754
6755 child = sibling_die (child);
6756 }
6757 }
6758
6759 *lowpc = best_low;
6760 *highpc = best_high;
6761}
6762
801e3a5b
JB
6763/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6764 in DIE. */
6765static void
6766dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6767 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6768{
6769 struct attribute *attr;
6770
6771 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6772 if (attr)
6773 {
6774 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6775
801e3a5b
JB
6776 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6777 if (attr)
6778 {
6779 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6780
801e3a5b
JB
6781 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6782 }
6783 }
6784
6785 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6786 if (attr)
6787 {
6788 bfd *obfd = cu->objfile->obfd;
6789
6790 /* The value of the DW_AT_ranges attribute is the offset of the
6791 address range list in the .debug_ranges section. */
6792 unsigned long offset = DW_UNSND (attr);
dce234bc 6793 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6794
6795 /* For some target architectures, but not others, the
6796 read_address function sign-extends the addresses it returns.
6797 To recognize base address selection entries, we need a
6798 mask. */
6799 unsigned int addr_size = cu->header.addr_size;
6800 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6801
6802 /* The base address, to which the next pair is relative. Note
6803 that this 'base' is a DWARF concept: most entries in a range
6804 list are relative, to reduce the number of relocs against the
6805 debugging information. This is separate from this function's
6806 'baseaddr' argument, which GDB uses to relocate debugging
6807 information from a shared library based on the address at
6808 which the library was loaded. */
d00adf39
DE
6809 CORE_ADDR base = cu->base_address;
6810 int base_known = cu->base_known;
801e3a5b 6811
be391dca 6812 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6813 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6814 {
6815 complaint (&symfile_complaints,
6816 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6817 offset);
6818 return;
6819 }
6820
6821 for (;;)
6822 {
6823 unsigned int bytes_read;
6824 CORE_ADDR start, end;
6825
6826 start = read_address (obfd, buffer, cu, &bytes_read);
6827 buffer += bytes_read;
6828 end = read_address (obfd, buffer, cu, &bytes_read);
6829 buffer += bytes_read;
6830
6831 /* Did we find the end of the range list? */
6832 if (start == 0 && end == 0)
6833 break;
6834
6835 /* Did we find a base address selection entry? */
6836 else if ((start & base_select_mask) == base_select_mask)
6837 {
6838 base = end;
6839 base_known = 1;
6840 }
6841
6842 /* We found an ordinary address range. */
6843 else
6844 {
6845 if (!base_known)
6846 {
6847 complaint (&symfile_complaints,
3e43a32a
MS
6848 _("Invalid .debug_ranges data "
6849 "(no base address)"));
801e3a5b
JB
6850 return;
6851 }
6852
9277c30c
UW
6853 if (start > end)
6854 {
6855 /* Inverted range entries are invalid. */
6856 complaint (&symfile_complaints,
6857 _("Invalid .debug_ranges data "
6858 "(inverted range)"));
6859 return;
6860 }
6861
6862 /* Empty range entries have no effect. */
6863 if (start == end)
6864 continue;
6865
6e70227d
DE
6866 record_block_range (block,
6867 baseaddr + base + start,
801e3a5b
JB
6868 baseaddr + base + end - 1);
6869 }
6870 }
6871 }
6872}
6873
60d5a603
JK
6874/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6875 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6876 during 4.6.0 experimental. */
6877
6878static int
6879producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6880{
6881 const char *cs;
6882 int major, minor, release;
6883
6884 if (cu->producer == NULL)
6885 {
6886 /* For unknown compilers expect their behavior is DWARF version
6887 compliant.
6888
6889 GCC started to support .debug_types sections by -gdwarf-4 since
6890 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6891 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6892 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6893 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6894
6895 return 0;
6896 }
6897
6898 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6899
6900 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6901 {
6902 /* For non-GCC compilers expect their behavior is DWARF version
6903 compliant. */
6904
6905 return 0;
6906 }
6907 cs = &cu->producer[strlen ("GNU ")];
6908 while (*cs && !isdigit (*cs))
6909 cs++;
6910 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6911 {
6912 /* Not recognized as GCC. */
6913
6914 return 0;
6915 }
6916
6917 return major < 4 || (major == 4 && minor < 6);
6918}
6919
6920/* Return the default accessibility type if it is not overriden by
6921 DW_AT_accessibility. */
6922
6923static enum dwarf_access_attribute
6924dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6925{
6926 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6927 {
6928 /* The default DWARF 2 accessibility for members is public, the default
6929 accessibility for inheritance is private. */
6930
6931 if (die->tag != DW_TAG_inheritance)
6932 return DW_ACCESS_public;
6933 else
6934 return DW_ACCESS_private;
6935 }
6936 else
6937 {
6938 /* DWARF 3+ defines the default accessibility a different way. The same
6939 rules apply now for DW_TAG_inheritance as for the members and it only
6940 depends on the container kind. */
6941
6942 if (die->parent->tag == DW_TAG_class_type)
6943 return DW_ACCESS_private;
6944 else
6945 return DW_ACCESS_public;
6946 }
6947}
6948
74ac6d43
TT
6949/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6950 offset. If the attribute was not found return 0, otherwise return
6951 1. If it was found but could not properly be handled, set *OFFSET
6952 to 0. */
6953
6954static int
6955handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6956 LONGEST *offset)
6957{
6958 struct attribute *attr;
6959
6960 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6961 if (attr != NULL)
6962 {
6963 *offset = 0;
6964
6965 /* Note that we do not check for a section offset first here.
6966 This is because DW_AT_data_member_location is new in DWARF 4,
6967 so if we see it, we can assume that a constant form is really
6968 a constant and not a section offset. */
6969 if (attr_form_is_constant (attr))
6970 *offset = dwarf2_get_attr_constant_value (attr, 0);
6971 else if (attr_form_is_section_offset (attr))
6972 dwarf2_complex_location_expr_complaint ();
6973 else if (attr_form_is_block (attr))
6974 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6975 else
6976 dwarf2_complex_location_expr_complaint ();
6977
6978 return 1;
6979 }
6980
6981 return 0;
6982}
6983
c906108c
SS
6984/* Add an aggregate field to the field list. */
6985
6986static void
107d2387 6987dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6988 struct dwarf2_cu *cu)
6e70227d 6989{
e7c27a73 6990 struct objfile *objfile = cu->objfile;
5e2b427d 6991 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6992 struct nextfield *new_field;
6993 struct attribute *attr;
6994 struct field *fp;
6995 char *fieldname = "";
6996
6997 /* Allocate a new field list entry and link it in. */
6998 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6999 make_cleanup (xfree, new_field);
c906108c 7000 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7001
7002 if (die->tag == DW_TAG_inheritance)
7003 {
7004 new_field->next = fip->baseclasses;
7005 fip->baseclasses = new_field;
7006 }
7007 else
7008 {
7009 new_field->next = fip->fields;
7010 fip->fields = new_field;
7011 }
c906108c
SS
7012 fip->nfields++;
7013
e142c38c 7014 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7015 if (attr)
7016 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7017 else
7018 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7019 if (new_field->accessibility != DW_ACCESS_public)
7020 fip->non_public_fields = 1;
60d5a603 7021
e142c38c 7022 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7023 if (attr)
7024 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7025 else
7026 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7027
7028 fp = &new_field->field;
a9a9bd0f 7029
e142c38c 7030 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7031 {
74ac6d43
TT
7032 LONGEST offset;
7033
a9a9bd0f 7034 /* Data member other than a C++ static data member. */
6e70227d 7035
c906108c 7036 /* Get type of field. */
e7c27a73 7037 fp->type = die_type (die, cu);
c906108c 7038
d6a843b5 7039 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7040
c906108c 7041 /* Get bit size of field (zero if none). */
e142c38c 7042 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7043 if (attr)
7044 {
7045 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7046 }
7047 else
7048 {
7049 FIELD_BITSIZE (*fp) = 0;
7050 }
7051
7052 /* Get bit offset of field. */
74ac6d43
TT
7053 if (handle_data_member_location (die, cu, &offset))
7054 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7055 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7056 if (attr)
7057 {
5e2b427d 7058 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7059 {
7060 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7061 additional bit offset from the MSB of the containing
7062 anonymous object to the MSB of the field. We don't
7063 have to do anything special since we don't need to
7064 know the size of the anonymous object. */
c906108c
SS
7065 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7066 }
7067 else
7068 {
7069 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7070 MSB of the anonymous object, subtract off the number of
7071 bits from the MSB of the field to the MSB of the
7072 object, and then subtract off the number of bits of
7073 the field itself. The result is the bit offset of
7074 the LSB of the field. */
c906108c
SS
7075 int anonymous_size;
7076 int bit_offset = DW_UNSND (attr);
7077
e142c38c 7078 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7079 if (attr)
7080 {
7081 /* The size of the anonymous object containing
7082 the bit field is explicit, so use the
7083 indicated size (in bytes). */
7084 anonymous_size = DW_UNSND (attr);
7085 }
7086 else
7087 {
7088 /* The size of the anonymous object containing
7089 the bit field must be inferred from the type
7090 attribute of the data member containing the
7091 bit field. */
7092 anonymous_size = TYPE_LENGTH (fp->type);
7093 }
7094 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7095 - bit_offset - FIELD_BITSIZE (*fp);
7096 }
7097 }
7098
7099 /* Get name of field. */
39cbfefa
DJ
7100 fieldname = dwarf2_name (die, cu);
7101 if (fieldname == NULL)
7102 fieldname = "";
d8151005
DJ
7103
7104 /* The name is already allocated along with this objfile, so we don't
7105 need to duplicate it for the type. */
7106 fp->name = fieldname;
c906108c
SS
7107
7108 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7109 pointer or virtual base class pointer) to private. */
e142c38c 7110 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7111 {
d48cc9dd 7112 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7113 new_field->accessibility = DW_ACCESS_private;
7114 fip->non_public_fields = 1;
7115 }
7116 }
a9a9bd0f 7117 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7118 {
a9a9bd0f
DC
7119 /* C++ static member. */
7120
7121 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7122 is a declaration, but all versions of G++ as of this writing
7123 (so through at least 3.2.1) incorrectly generate
7124 DW_TAG_variable tags. */
6e70227d 7125
ff355380 7126 const char *physname;
c906108c 7127
a9a9bd0f 7128 /* Get name of field. */
39cbfefa
DJ
7129 fieldname = dwarf2_name (die, cu);
7130 if (fieldname == NULL)
c906108c
SS
7131 return;
7132
254e6b9e 7133 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7134 if (attr
7135 /* Only create a symbol if this is an external value.
7136 new_symbol checks this and puts the value in the global symbol
7137 table, which we want. If it is not external, new_symbol
7138 will try to put the value in cu->list_in_scope which is wrong. */
7139 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7140 {
7141 /* A static const member, not much different than an enum as far as
7142 we're concerned, except that we can support more types. */
7143 new_symbol (die, NULL, cu);
7144 }
7145
2df3850c 7146 /* Get physical name. */
ff355380 7147 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7148
d8151005
DJ
7149 /* The name is already allocated along with this objfile, so we don't
7150 need to duplicate it for the type. */
7151 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7152 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7153 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7154 }
7155 else if (die->tag == DW_TAG_inheritance)
7156 {
74ac6d43 7157 LONGEST offset;
d4b96c9a 7158
74ac6d43
TT
7159 /* C++ base class field. */
7160 if (handle_data_member_location (die, cu, &offset))
7161 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7162 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7163 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7164 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7165 fip->nbaseclasses++;
7166 }
7167}
7168
98751a41
JK
7169/* Add a typedef defined in the scope of the FIP's class. */
7170
7171static void
7172dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7173 struct dwarf2_cu *cu)
6e70227d 7174{
98751a41 7175 struct objfile *objfile = cu->objfile;
98751a41
JK
7176 struct typedef_field_list *new_field;
7177 struct attribute *attr;
7178 struct typedef_field *fp;
7179 char *fieldname = "";
7180
7181 /* Allocate a new field list entry and link it in. */
7182 new_field = xzalloc (sizeof (*new_field));
7183 make_cleanup (xfree, new_field);
7184
7185 gdb_assert (die->tag == DW_TAG_typedef);
7186
7187 fp = &new_field->field;
7188
7189 /* Get name of field. */
7190 fp->name = dwarf2_name (die, cu);
7191 if (fp->name == NULL)
7192 return;
7193
7194 fp->type = read_type_die (die, cu);
7195
7196 new_field->next = fip->typedef_field_list;
7197 fip->typedef_field_list = new_field;
7198 fip->typedef_field_list_count++;
7199}
7200
c906108c
SS
7201/* Create the vector of fields, and attach it to the type. */
7202
7203static void
fba45db2 7204dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7205 struct dwarf2_cu *cu)
c906108c
SS
7206{
7207 int nfields = fip->nfields;
7208
7209 /* Record the field count, allocate space for the array of fields,
7210 and create blank accessibility bitfields if necessary. */
7211 TYPE_NFIELDS (type) = nfields;
7212 TYPE_FIELDS (type) = (struct field *)
7213 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7214 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7215
b4ba55a1 7216 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7217 {
7218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7219
7220 TYPE_FIELD_PRIVATE_BITS (type) =
7221 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7222 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7223
7224 TYPE_FIELD_PROTECTED_BITS (type) =
7225 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7226 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7227
774b6a14
TT
7228 TYPE_FIELD_IGNORE_BITS (type) =
7229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7230 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7231 }
7232
7233 /* If the type has baseclasses, allocate and clear a bit vector for
7234 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7235 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7236 {
7237 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7238 unsigned char *pointer;
c906108c
SS
7239
7240 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7241 pointer = TYPE_ALLOC (type, num_bytes);
7242 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7243 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7244 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7245 }
7246
3e43a32a
MS
7247 /* Copy the saved-up fields into the field vector. Start from the head of
7248 the list, adding to the tail of the field array, so that they end up in
7249 the same order in the array in which they were added to the list. */
c906108c
SS
7250 while (nfields-- > 0)
7251 {
7d0ccb61
DJ
7252 struct nextfield *fieldp;
7253
7254 if (fip->fields)
7255 {
7256 fieldp = fip->fields;
7257 fip->fields = fieldp->next;
7258 }
7259 else
7260 {
7261 fieldp = fip->baseclasses;
7262 fip->baseclasses = fieldp->next;
7263 }
7264
7265 TYPE_FIELD (type, nfields) = fieldp->field;
7266 switch (fieldp->accessibility)
c906108c 7267 {
c5aa993b 7268 case DW_ACCESS_private:
b4ba55a1
JB
7269 if (cu->language != language_ada)
7270 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7271 break;
c906108c 7272
c5aa993b 7273 case DW_ACCESS_protected:
b4ba55a1
JB
7274 if (cu->language != language_ada)
7275 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7276 break;
c906108c 7277
c5aa993b
JM
7278 case DW_ACCESS_public:
7279 break;
c906108c 7280
c5aa993b
JM
7281 default:
7282 /* Unknown accessibility. Complain and treat it as public. */
7283 {
e2e0b3e5 7284 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7285 fieldp->accessibility);
c5aa993b
JM
7286 }
7287 break;
c906108c
SS
7288 }
7289 if (nfields < fip->nbaseclasses)
7290 {
7d0ccb61 7291 switch (fieldp->virtuality)
c906108c 7292 {
c5aa993b
JM
7293 case DW_VIRTUALITY_virtual:
7294 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7295 if (cu->language == language_ada)
a73c6dcd 7296 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7297 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7298 break;
c906108c
SS
7299 }
7300 }
c906108c
SS
7301 }
7302}
7303
c906108c
SS
7304/* Add a member function to the proper fieldlist. */
7305
7306static void
107d2387 7307dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7308 struct type *type, struct dwarf2_cu *cu)
c906108c 7309{
e7c27a73 7310 struct objfile *objfile = cu->objfile;
c906108c
SS
7311 struct attribute *attr;
7312 struct fnfieldlist *flp;
7313 int i;
7314 struct fn_field *fnp;
7315 char *fieldname;
c906108c 7316 struct nextfnfield *new_fnfield;
f792889a 7317 struct type *this_type;
60d5a603 7318 enum dwarf_access_attribute accessibility;
c906108c 7319
b4ba55a1 7320 if (cu->language == language_ada)
a73c6dcd 7321 error (_("unexpected member function in Ada type"));
b4ba55a1 7322
2df3850c 7323 /* Get name of member function. */
39cbfefa
DJ
7324 fieldname = dwarf2_name (die, cu);
7325 if (fieldname == NULL)
2df3850c 7326 return;
c906108c 7327
c906108c
SS
7328 /* Look up member function name in fieldlist. */
7329 for (i = 0; i < fip->nfnfields; i++)
7330 {
27bfe10e 7331 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7332 break;
7333 }
7334
7335 /* Create new list element if necessary. */
7336 if (i < fip->nfnfields)
7337 flp = &fip->fnfieldlists[i];
7338 else
7339 {
7340 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7341 {
7342 fip->fnfieldlists = (struct fnfieldlist *)
7343 xrealloc (fip->fnfieldlists,
7344 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7345 * sizeof (struct fnfieldlist));
c906108c 7346 if (fip->nfnfields == 0)
c13c43fd 7347 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7348 }
7349 flp = &fip->fnfieldlists[fip->nfnfields];
7350 flp->name = fieldname;
7351 flp->length = 0;
7352 flp->head = NULL;
3da10d80 7353 i = fip->nfnfields++;
c906108c
SS
7354 }
7355
7356 /* Create a new member function field and chain it to the field list
0963b4bd 7357 entry. */
c906108c 7358 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7359 make_cleanup (xfree, new_fnfield);
c906108c
SS
7360 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7361 new_fnfield->next = flp->head;
7362 flp->head = new_fnfield;
7363 flp->length++;
7364
7365 /* Fill in the member function field info. */
7366 fnp = &new_fnfield->fnfield;
3da10d80
KS
7367
7368 /* Delay processing of the physname until later. */
7369 if (cu->language == language_cplus || cu->language == language_java)
7370 {
7371 add_to_method_list (type, i, flp->length - 1, fieldname,
7372 die, cu);
7373 }
7374 else
7375 {
1d06ead6 7376 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7377 fnp->physname = physname ? physname : "";
7378 }
7379
c906108c 7380 fnp->type = alloc_type (objfile);
f792889a
DJ
7381 this_type = read_type_die (die, cu);
7382 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7383 {
f792889a 7384 int nparams = TYPE_NFIELDS (this_type);
c906108c 7385
f792889a 7386 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7387 of the method itself (TYPE_CODE_METHOD). */
7388 smash_to_method_type (fnp->type, type,
f792889a
DJ
7389 TYPE_TARGET_TYPE (this_type),
7390 TYPE_FIELDS (this_type),
7391 TYPE_NFIELDS (this_type),
7392 TYPE_VARARGS (this_type));
c906108c
SS
7393
7394 /* Handle static member functions.
c5aa993b 7395 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7396 member functions. G++ helps GDB by marking the first
7397 parameter for non-static member functions (which is the this
7398 pointer) as artificial. We obtain this information from
7399 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7400 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7401 fnp->voffset = VOFFSET_STATIC;
7402 }
7403 else
e2e0b3e5 7404 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7405 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7406
7407 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7408 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7409 fnp->fcontext = die_containing_type (die, cu);
c906108c 7410
3e43a32a
MS
7411 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7412 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7413
7414 /* Get accessibility. */
e142c38c 7415 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7416 if (attr)
60d5a603
JK
7417 accessibility = DW_UNSND (attr);
7418 else
7419 accessibility = dwarf2_default_access_attribute (die, cu);
7420 switch (accessibility)
c906108c 7421 {
60d5a603
JK
7422 case DW_ACCESS_private:
7423 fnp->is_private = 1;
7424 break;
7425 case DW_ACCESS_protected:
7426 fnp->is_protected = 1;
7427 break;
c906108c
SS
7428 }
7429
b02dede2 7430 /* Check for artificial methods. */
e142c38c 7431 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7432 if (attr && DW_UNSND (attr) != 0)
7433 fnp->is_artificial = 1;
7434
0d564a31 7435 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7436 function. For older versions of GCC, this is an offset in the
7437 appropriate virtual table, as specified by DW_AT_containing_type.
7438 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7439 to the object address. */
7440
e142c38c 7441 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7442 if (attr)
8e19ed76 7443 {
aec5aa8b 7444 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7445 {
aec5aa8b
TT
7446 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7447 {
7448 /* Old-style GCC. */
7449 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7450 }
7451 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7452 || (DW_BLOCK (attr)->size > 1
7453 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7454 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7455 {
7456 struct dwarf_block blk;
7457 int offset;
7458
7459 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7460 ? 1 : 2);
7461 blk.size = DW_BLOCK (attr)->size - offset;
7462 blk.data = DW_BLOCK (attr)->data + offset;
7463 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7464 if ((fnp->voffset % cu->header.addr_size) != 0)
7465 dwarf2_complex_location_expr_complaint ();
7466 else
7467 fnp->voffset /= cu->header.addr_size;
7468 fnp->voffset += 2;
7469 }
7470 else
7471 dwarf2_complex_location_expr_complaint ();
7472
7473 if (!fnp->fcontext)
7474 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7475 }
3690dd37 7476 else if (attr_form_is_section_offset (attr))
8e19ed76 7477 {
4d3c2250 7478 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7479 }
7480 else
7481 {
4d3c2250
KB
7482 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7483 fieldname);
8e19ed76 7484 }
0d564a31 7485 }
d48cc9dd
DJ
7486 else
7487 {
7488 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7489 if (attr && DW_UNSND (attr))
7490 {
7491 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7492 complaint (&symfile_complaints,
3e43a32a
MS
7493 _("Member function \"%s\" (offset %d) is virtual "
7494 "but the vtable offset is not specified"),
d48cc9dd 7495 fieldname, die->offset);
9655fd1a 7496 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7497 TYPE_CPLUS_DYNAMIC (type) = 1;
7498 }
7499 }
c906108c
SS
7500}
7501
7502/* Create the vector of member function fields, and attach it to the type. */
7503
7504static void
fba45db2 7505dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7506 struct dwarf2_cu *cu)
c906108c
SS
7507{
7508 struct fnfieldlist *flp;
7509 int total_length = 0;
7510 int i;
7511
b4ba55a1 7512 if (cu->language == language_ada)
a73c6dcd 7513 error (_("unexpected member functions in Ada type"));
b4ba55a1 7514
c906108c
SS
7515 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7516 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7517 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7518
7519 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7520 {
7521 struct nextfnfield *nfp = flp->head;
7522 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7523 int k;
7524
7525 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7526 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7527 fn_flp->fn_fields = (struct fn_field *)
7528 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7529 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7530 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7531
7532 total_length += flp->length;
7533 }
7534
7535 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7536 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7537}
7538
1168df01
JB
7539/* Returns non-zero if NAME is the name of a vtable member in CU's
7540 language, zero otherwise. */
7541static int
7542is_vtable_name (const char *name, struct dwarf2_cu *cu)
7543{
7544 static const char vptr[] = "_vptr";
987504bb 7545 static const char vtable[] = "vtable";
1168df01 7546
987504bb
JJ
7547 /* Look for the C++ and Java forms of the vtable. */
7548 if ((cu->language == language_java
7549 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7550 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7551 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7552 return 1;
7553
7554 return 0;
7555}
7556
c0dd20ea 7557/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7558 functions, with the ABI-specified layout. If TYPE describes
7559 such a structure, smash it into a member function type.
61049d3b
DJ
7560
7561 GCC shouldn't do this; it should just output pointer to member DIEs.
7562 This is GCC PR debug/28767. */
c0dd20ea 7563
0b92b5bb
TT
7564static void
7565quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7566{
0b92b5bb 7567 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7568
7569 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7570 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7571 return;
c0dd20ea
DJ
7572
7573 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7574 if (TYPE_FIELD_NAME (type, 0) == NULL
7575 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7576 || TYPE_FIELD_NAME (type, 1) == NULL
7577 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7578 return;
c0dd20ea
DJ
7579
7580 /* Find the type of the method. */
0b92b5bb 7581 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7582 if (pfn_type == NULL
7583 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7584 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7585 return;
c0dd20ea
DJ
7586
7587 /* Look for the "this" argument. */
7588 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7589 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7590 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7591 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7592 return;
c0dd20ea
DJ
7593
7594 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7595 new_type = alloc_type (objfile);
7596 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7597 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7598 TYPE_VARARGS (pfn_type));
0b92b5bb 7599 smash_to_methodptr_type (type, new_type);
c0dd20ea 7600}
1168df01 7601
c906108c 7602/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7603 (definition) to create a type for the structure or union. Fill in
7604 the type's name and general properties; the members will not be
7605 processed until process_structure_type.
c906108c 7606
c767944b
DJ
7607 NOTE: we need to call these functions regardless of whether or not the
7608 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7609 structure or union. This gets the type entered into our set of
7610 user defined types.
7611
7612 However, if the structure is incomplete (an opaque struct/union)
7613 then suppress creating a symbol table entry for it since gdb only
7614 wants to find the one with the complete definition. Note that if
7615 it is complete, we just call new_symbol, which does it's own
7616 checking about whether the struct/union is anonymous or not (and
7617 suppresses creating a symbol table entry itself). */
7618
f792889a 7619static struct type *
134d01f1 7620read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7621{
e7c27a73 7622 struct objfile *objfile = cu->objfile;
c906108c
SS
7623 struct type *type;
7624 struct attribute *attr;
39cbfefa 7625 char *name;
c906108c 7626
348e048f
DE
7627 /* If the definition of this type lives in .debug_types, read that type.
7628 Don't follow DW_AT_specification though, that will take us back up
7629 the chain and we want to go down. */
7630 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7631 if (attr)
7632 {
7633 struct dwarf2_cu *type_cu = cu;
7634 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7635
348e048f
DE
7636 /* We could just recurse on read_structure_type, but we need to call
7637 get_die_type to ensure only one type for this DIE is created.
7638 This is important, for example, because for c++ classes we need
7639 TYPE_NAME set which is only done by new_symbol. Blech. */
7640 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7641
7642 /* TYPE_CU may not be the same as CU.
7643 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7644 return set_die_type (die, type, cu);
7645 }
7646
c0dd20ea 7647 type = alloc_type (objfile);
c906108c 7648 INIT_CPLUS_SPECIFIC (type);
93311388 7649
39cbfefa
DJ
7650 name = dwarf2_name (die, cu);
7651 if (name != NULL)
c906108c 7652 {
987504bb
JJ
7653 if (cu->language == language_cplus
7654 || cu->language == language_java)
63d06c5c 7655 {
3da10d80
KS
7656 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7657
7658 /* dwarf2_full_name might have already finished building the DIE's
7659 type. If so, there is no need to continue. */
7660 if (get_die_type (die, cu) != NULL)
7661 return get_die_type (die, cu);
7662
7663 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7664 if (die->tag == DW_TAG_structure_type
7665 || die->tag == DW_TAG_class_type)
7666 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7667 }
7668 else
7669 {
d8151005
DJ
7670 /* The name is already allocated along with this objfile, so
7671 we don't need to duplicate it for the type. */
94af9270
KS
7672 TYPE_TAG_NAME (type) = (char *) name;
7673 if (die->tag == DW_TAG_class_type)
7674 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7675 }
c906108c
SS
7676 }
7677
7678 if (die->tag == DW_TAG_structure_type)
7679 {
7680 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7681 }
7682 else if (die->tag == DW_TAG_union_type)
7683 {
7684 TYPE_CODE (type) = TYPE_CODE_UNION;
7685 }
7686 else
7687 {
c906108c
SS
7688 TYPE_CODE (type) = TYPE_CODE_CLASS;
7689 }
7690
0cc2414c
TT
7691 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7692 TYPE_DECLARED_CLASS (type) = 1;
7693
e142c38c 7694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7695 if (attr)
7696 {
7697 TYPE_LENGTH (type) = DW_UNSND (attr);
7698 }
7699 else
7700 {
7701 TYPE_LENGTH (type) = 0;
7702 }
7703
876cecd0 7704 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7705 if (die_is_declaration (die, cu))
876cecd0 7706 TYPE_STUB (type) = 1;
a6c727b2
DJ
7707 else if (attr == NULL && die->child == NULL
7708 && producer_is_realview (cu->producer))
7709 /* RealView does not output the required DW_AT_declaration
7710 on incomplete types. */
7711 TYPE_STUB (type) = 1;
dc718098 7712
c906108c
SS
7713 /* We need to add the type field to the die immediately so we don't
7714 infinitely recurse when dealing with pointers to the structure
0963b4bd 7715 type within the structure itself. */
1c379e20 7716 set_die_type (die, type, cu);
c906108c 7717
7e314c57
JK
7718 /* set_die_type should be already done. */
7719 set_descriptive_type (type, die, cu);
7720
c767944b
DJ
7721 return type;
7722}
7723
7724/* Finish creating a structure or union type, including filling in
7725 its members and creating a symbol for it. */
7726
7727static void
7728process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7729{
7730 struct objfile *objfile = cu->objfile;
7731 struct die_info *child_die = die->child;
7732 struct type *type;
7733
7734 type = get_die_type (die, cu);
7735 if (type == NULL)
7736 type = read_structure_type (die, cu);
7737
e142c38c 7738 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7739 {
7740 struct field_info fi;
7741 struct die_info *child_die;
34eaf542 7742 VEC (symbolp) *template_args = NULL;
c767944b 7743 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7744
7745 memset (&fi, 0, sizeof (struct field_info));
7746
639d11d3 7747 child_die = die->child;
c906108c
SS
7748
7749 while (child_die && child_die->tag)
7750 {
a9a9bd0f
DC
7751 if (child_die->tag == DW_TAG_member
7752 || child_die->tag == DW_TAG_variable)
c906108c 7753 {
a9a9bd0f
DC
7754 /* NOTE: carlton/2002-11-05: A C++ static data member
7755 should be a DW_TAG_member that is a declaration, but
7756 all versions of G++ as of this writing (so through at
7757 least 3.2.1) incorrectly generate DW_TAG_variable
7758 tags for them instead. */
e7c27a73 7759 dwarf2_add_field (&fi, child_die, cu);
c906108c 7760 }
8713b1b1 7761 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7762 {
0963b4bd 7763 /* C++ member function. */
e7c27a73 7764 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7765 }
7766 else if (child_die->tag == DW_TAG_inheritance)
7767 {
7768 /* C++ base class field. */
e7c27a73 7769 dwarf2_add_field (&fi, child_die, cu);
c906108c 7770 }
98751a41
JK
7771 else if (child_die->tag == DW_TAG_typedef)
7772 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7773 else if (child_die->tag == DW_TAG_template_type_param
7774 || child_die->tag == DW_TAG_template_value_param)
7775 {
7776 struct symbol *arg = new_symbol (child_die, NULL, cu);
7777
f1078f66
DJ
7778 if (arg != NULL)
7779 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7780 }
7781
c906108c
SS
7782 child_die = sibling_die (child_die);
7783 }
7784
34eaf542
TT
7785 /* Attach template arguments to type. */
7786 if (! VEC_empty (symbolp, template_args))
7787 {
7788 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7789 TYPE_N_TEMPLATE_ARGUMENTS (type)
7790 = VEC_length (symbolp, template_args);
7791 TYPE_TEMPLATE_ARGUMENTS (type)
7792 = obstack_alloc (&objfile->objfile_obstack,
7793 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7794 * sizeof (struct symbol *)));
7795 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7796 VEC_address (symbolp, template_args),
7797 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7798 * sizeof (struct symbol *)));
7799 VEC_free (symbolp, template_args);
7800 }
7801
c906108c
SS
7802 /* Attach fields and member functions to the type. */
7803 if (fi.nfields)
e7c27a73 7804 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7805 if (fi.nfnfields)
7806 {
e7c27a73 7807 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7808
c5aa993b 7809 /* Get the type which refers to the base class (possibly this
c906108c 7810 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7811 class from the DW_AT_containing_type attribute. This use of
7812 DW_AT_containing_type is a GNU extension. */
c906108c 7813
e142c38c 7814 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7815 {
e7c27a73 7816 struct type *t = die_containing_type (die, cu);
c906108c
SS
7817
7818 TYPE_VPTR_BASETYPE (type) = t;
7819 if (type == t)
7820 {
c906108c
SS
7821 int i;
7822
7823 /* Our own class provides vtbl ptr. */
7824 for (i = TYPE_NFIELDS (t) - 1;
7825 i >= TYPE_N_BASECLASSES (t);
7826 --i)
7827 {
7828 char *fieldname = TYPE_FIELD_NAME (t, i);
7829
1168df01 7830 if (is_vtable_name (fieldname, cu))
c906108c
SS
7831 {
7832 TYPE_VPTR_FIELDNO (type) = i;
7833 break;
7834 }
7835 }
7836
7837 /* Complain if virtual function table field not found. */
7838 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7839 complaint (&symfile_complaints,
3e43a32a
MS
7840 _("virtual function table pointer "
7841 "not found when defining class '%s'"),
4d3c2250
KB
7842 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7843 "");
c906108c
SS
7844 }
7845 else
7846 {
7847 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7848 }
7849 }
f6235d4c
EZ
7850 else if (cu->producer
7851 && strncmp (cu->producer,
7852 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7853 {
7854 /* The IBM XLC compiler does not provide direct indication
7855 of the containing type, but the vtable pointer is
7856 always named __vfp. */
7857
7858 int i;
7859
7860 for (i = TYPE_NFIELDS (type) - 1;
7861 i >= TYPE_N_BASECLASSES (type);
7862 --i)
7863 {
7864 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7865 {
7866 TYPE_VPTR_FIELDNO (type) = i;
7867 TYPE_VPTR_BASETYPE (type) = type;
7868 break;
7869 }
7870 }
7871 }
c906108c 7872 }
98751a41
JK
7873
7874 /* Copy fi.typedef_field_list linked list elements content into the
7875 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7876 if (fi.typedef_field_list)
7877 {
7878 int i = fi.typedef_field_list_count;
7879
a0d7a4ff 7880 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7881 TYPE_TYPEDEF_FIELD_ARRAY (type)
7882 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7883 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7884
7885 /* Reverse the list order to keep the debug info elements order. */
7886 while (--i >= 0)
7887 {
7888 struct typedef_field *dest, *src;
6e70227d 7889
98751a41
JK
7890 dest = &TYPE_TYPEDEF_FIELD (type, i);
7891 src = &fi.typedef_field_list->field;
7892 fi.typedef_field_list = fi.typedef_field_list->next;
7893 *dest = *src;
7894 }
7895 }
c767944b
DJ
7896
7897 do_cleanups (back_to);
eb2a6f42
TT
7898
7899 if (HAVE_CPLUS_STRUCT (type))
7900 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7901 }
63d06c5c 7902
0b92b5bb
TT
7903 quirk_gcc_member_function_pointer (type, cu->objfile);
7904
90aeadfc
DC
7905 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7906 snapshots) has been known to create a die giving a declaration
7907 for a class that has, as a child, a die giving a definition for a
7908 nested class. So we have to process our children even if the
7909 current die is a declaration. Normally, of course, a declaration
7910 won't have any children at all. */
134d01f1 7911
90aeadfc
DC
7912 while (child_die != NULL && child_die->tag)
7913 {
7914 if (child_die->tag == DW_TAG_member
7915 || child_die->tag == DW_TAG_variable
34eaf542
TT
7916 || child_die->tag == DW_TAG_inheritance
7917 || child_die->tag == DW_TAG_template_value_param
7918 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7919 {
90aeadfc 7920 /* Do nothing. */
134d01f1 7921 }
90aeadfc
DC
7922 else
7923 process_die (child_die, cu);
134d01f1 7924
90aeadfc 7925 child_die = sibling_die (child_die);
134d01f1
DJ
7926 }
7927
fa4028e9
JB
7928 /* Do not consider external references. According to the DWARF standard,
7929 these DIEs are identified by the fact that they have no byte_size
7930 attribute, and a declaration attribute. */
7931 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7932 || !die_is_declaration (die, cu))
c767944b 7933 new_symbol (die, type, cu);
134d01f1
DJ
7934}
7935
7936/* Given a DW_AT_enumeration_type die, set its type. We do not
7937 complete the type's fields yet, or create any symbols. */
c906108c 7938
f792889a 7939static struct type *
134d01f1 7940read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7941{
e7c27a73 7942 struct objfile *objfile = cu->objfile;
c906108c 7943 struct type *type;
c906108c 7944 struct attribute *attr;
0114d602 7945 const char *name;
134d01f1 7946
348e048f
DE
7947 /* If the definition of this type lives in .debug_types, read that type.
7948 Don't follow DW_AT_specification though, that will take us back up
7949 the chain and we want to go down. */
7950 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7951 if (attr)
7952 {
7953 struct dwarf2_cu *type_cu = cu;
7954 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7955
348e048f 7956 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7957
7958 /* TYPE_CU may not be the same as CU.
7959 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7960 return set_die_type (die, type, cu);
7961 }
7962
c906108c
SS
7963 type = alloc_type (objfile);
7964
7965 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7966 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7967 if (name != NULL)
0114d602 7968 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7969
e142c38c 7970 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7971 if (attr)
7972 {
7973 TYPE_LENGTH (type) = DW_UNSND (attr);
7974 }
7975 else
7976 {
7977 TYPE_LENGTH (type) = 0;
7978 }
7979
137033e9
JB
7980 /* The enumeration DIE can be incomplete. In Ada, any type can be
7981 declared as private in the package spec, and then defined only
7982 inside the package body. Such types are known as Taft Amendment
7983 Types. When another package uses such a type, an incomplete DIE
7984 may be generated by the compiler. */
02eb380e 7985 if (die_is_declaration (die, cu))
876cecd0 7986 TYPE_STUB (type) = 1;
02eb380e 7987
f792889a 7988 return set_die_type (die, type, cu);
134d01f1
DJ
7989}
7990
7991/* Given a pointer to a die which begins an enumeration, process all
7992 the dies that define the members of the enumeration, and create the
7993 symbol for the enumeration type.
7994
7995 NOTE: We reverse the order of the element list. */
7996
7997static void
7998process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7999{
f792889a 8000 struct type *this_type;
134d01f1 8001
f792889a
DJ
8002 this_type = get_die_type (die, cu);
8003 if (this_type == NULL)
8004 this_type = read_enumeration_type (die, cu);
9dc481d3 8005
639d11d3 8006 if (die->child != NULL)
c906108c 8007 {
9dc481d3
DE
8008 struct die_info *child_die;
8009 struct symbol *sym;
8010 struct field *fields = NULL;
8011 int num_fields = 0;
8012 int unsigned_enum = 1;
8013 char *name;
8014
639d11d3 8015 child_die = die->child;
c906108c
SS
8016 while (child_die && child_die->tag)
8017 {
8018 if (child_die->tag != DW_TAG_enumerator)
8019 {
e7c27a73 8020 process_die (child_die, cu);
c906108c
SS
8021 }
8022 else
8023 {
39cbfefa
DJ
8024 name = dwarf2_name (child_die, cu);
8025 if (name)
c906108c 8026 {
f792889a 8027 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
8028 if (SYMBOL_VALUE (sym) < 0)
8029 unsigned_enum = 0;
8030
8031 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8032 {
8033 fields = (struct field *)
8034 xrealloc (fields,
8035 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8036 * sizeof (struct field));
c906108c
SS
8037 }
8038
3567439c 8039 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8040 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8041 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8042 FIELD_BITSIZE (fields[num_fields]) = 0;
8043
8044 num_fields++;
8045 }
8046 }
8047
8048 child_die = sibling_die (child_die);
8049 }
8050
8051 if (num_fields)
8052 {
f792889a
DJ
8053 TYPE_NFIELDS (this_type) = num_fields;
8054 TYPE_FIELDS (this_type) = (struct field *)
8055 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8056 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8057 sizeof (struct field) * num_fields);
b8c9b27d 8058 xfree (fields);
c906108c
SS
8059 }
8060 if (unsigned_enum)
876cecd0 8061 TYPE_UNSIGNED (this_type) = 1;
c906108c 8062 }
134d01f1 8063
6c83ed52
TT
8064 /* If we are reading an enum from a .debug_types unit, and the enum
8065 is a declaration, and the enum is not the signatured type in the
8066 unit, then we do not want to add a symbol for it. Adding a
8067 symbol would in some cases obscure the true definition of the
8068 enum, giving users an incomplete type when the definition is
8069 actually available. Note that we do not want to do this for all
8070 enums which are just declarations, because C++0x allows forward
8071 enum declarations. */
b0df02fd 8072 if (cu->per_cu->debug_types_section
6c83ed52
TT
8073 && die_is_declaration (die, cu))
8074 {
8075 struct signatured_type *type_sig;
8076
8077 type_sig
8078 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8079 cu->per_cu->debug_types_section,
6c83ed52
TT
8080 cu->per_cu->offset);
8081 if (type_sig->type_offset != die->offset)
8082 return;
8083 }
8084
f792889a 8085 new_symbol (die, this_type, cu);
c906108c
SS
8086}
8087
8088/* Extract all information from a DW_TAG_array_type DIE and put it in
8089 the DIE's type field. For now, this only handles one dimensional
8090 arrays. */
8091
f792889a 8092static struct type *
e7c27a73 8093read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8094{
e7c27a73 8095 struct objfile *objfile = cu->objfile;
c906108c 8096 struct die_info *child_die;
7e314c57 8097 struct type *type;
c906108c
SS
8098 struct type *element_type, *range_type, *index_type;
8099 struct type **range_types = NULL;
8100 struct attribute *attr;
8101 int ndim = 0;
8102 struct cleanup *back_to;
39cbfefa 8103 char *name;
c906108c 8104
e7c27a73 8105 element_type = die_type (die, cu);
c906108c 8106
7e314c57
JK
8107 /* The die_type call above may have already set the type for this DIE. */
8108 type = get_die_type (die, cu);
8109 if (type)
8110 return type;
8111
c906108c
SS
8112 /* Irix 6.2 native cc creates array types without children for
8113 arrays with unspecified length. */
639d11d3 8114 if (die->child == NULL)
c906108c 8115 {
46bf5051 8116 index_type = objfile_type (objfile)->builtin_int;
c906108c 8117 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8118 type = create_array_type (NULL, element_type, range_type);
8119 return set_die_type (die, type, cu);
c906108c
SS
8120 }
8121
8122 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8123 child_die = die->child;
c906108c
SS
8124 while (child_die && child_die->tag)
8125 {
8126 if (child_die->tag == DW_TAG_subrange_type)
8127 {
f792889a 8128 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8129
f792889a 8130 if (child_type != NULL)
a02abb62 8131 {
0963b4bd
MS
8132 /* The range type was succesfully read. Save it for the
8133 array type creation. */
a02abb62
JB
8134 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8135 {
8136 range_types = (struct type **)
8137 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8138 * sizeof (struct type *));
8139 if (ndim == 0)
8140 make_cleanup (free_current_contents, &range_types);
8141 }
f792889a 8142 range_types[ndim++] = child_type;
a02abb62 8143 }
c906108c
SS
8144 }
8145 child_die = sibling_die (child_die);
8146 }
8147
8148 /* Dwarf2 dimensions are output from left to right, create the
8149 necessary array types in backwards order. */
7ca2d3a3 8150
c906108c 8151 type = element_type;
7ca2d3a3
DL
8152
8153 if (read_array_order (die, cu) == DW_ORD_col_major)
8154 {
8155 int i = 0;
9a619af0 8156
7ca2d3a3
DL
8157 while (i < ndim)
8158 type = create_array_type (NULL, type, range_types[i++]);
8159 }
8160 else
8161 {
8162 while (ndim-- > 0)
8163 type = create_array_type (NULL, type, range_types[ndim]);
8164 }
c906108c 8165
f5f8a009
EZ
8166 /* Understand Dwarf2 support for vector types (like they occur on
8167 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8168 array type. This is not part of the Dwarf2/3 standard yet, but a
8169 custom vendor extension. The main difference between a regular
8170 array and the vector variant is that vectors are passed by value
8171 to functions. */
e142c38c 8172 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8173 if (attr)
ea37ba09 8174 make_vector_type (type);
f5f8a009 8175
dbc98a8b
KW
8176 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8177 implementation may choose to implement triple vectors using this
8178 attribute. */
8179 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8180 if (attr)
8181 {
8182 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8183 TYPE_LENGTH (type) = DW_UNSND (attr);
8184 else
3e43a32a
MS
8185 complaint (&symfile_complaints,
8186 _("DW_AT_byte_size for array type smaller "
8187 "than the total size of elements"));
dbc98a8b
KW
8188 }
8189
39cbfefa
DJ
8190 name = dwarf2_name (die, cu);
8191 if (name)
8192 TYPE_NAME (type) = name;
6e70227d 8193
0963b4bd 8194 /* Install the type in the die. */
7e314c57
JK
8195 set_die_type (die, type, cu);
8196
8197 /* set_die_type should be already done. */
b4ba55a1
JB
8198 set_descriptive_type (type, die, cu);
8199
c906108c
SS
8200 do_cleanups (back_to);
8201
7e314c57 8202 return type;
c906108c
SS
8203}
8204
7ca2d3a3 8205static enum dwarf_array_dim_ordering
6e70227d 8206read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8207{
8208 struct attribute *attr;
8209
8210 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8211
8212 if (attr) return DW_SND (attr);
8213
0963b4bd
MS
8214 /* GNU F77 is a special case, as at 08/2004 array type info is the
8215 opposite order to the dwarf2 specification, but data is still
8216 laid out as per normal fortran.
7ca2d3a3 8217
0963b4bd
MS
8218 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8219 version checking. */
7ca2d3a3 8220
905e0470
PM
8221 if (cu->language == language_fortran
8222 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8223 {
8224 return DW_ORD_row_major;
8225 }
8226
6e70227d 8227 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8228 {
8229 case array_column_major:
8230 return DW_ORD_col_major;
8231 case array_row_major:
8232 default:
8233 return DW_ORD_row_major;
8234 };
8235}
8236
72019c9c 8237/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8238 the DIE's type field. */
72019c9c 8239
f792889a 8240static struct type *
72019c9c
GM
8241read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8242{
7e314c57
JK
8243 struct type *domain_type, *set_type;
8244 struct attribute *attr;
f792889a 8245
7e314c57
JK
8246 domain_type = die_type (die, cu);
8247
8248 /* The die_type call above may have already set the type for this DIE. */
8249 set_type = get_die_type (die, cu);
8250 if (set_type)
8251 return set_type;
8252
8253 set_type = create_set_type (NULL, domain_type);
8254
8255 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8256 if (attr)
8257 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8258
f792889a 8259 return set_die_type (die, set_type, cu);
72019c9c 8260}
7ca2d3a3 8261
c906108c
SS
8262/* First cut: install each common block member as a global variable. */
8263
8264static void
e7c27a73 8265read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8266{
8267 struct die_info *child_die;
8268 struct attribute *attr;
8269 struct symbol *sym;
8270 CORE_ADDR base = (CORE_ADDR) 0;
8271
e142c38c 8272 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8273 if (attr)
8274 {
0963b4bd 8275 /* Support the .debug_loc offsets. */
8e19ed76
PS
8276 if (attr_form_is_block (attr))
8277 {
e7c27a73 8278 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8279 }
3690dd37 8280 else if (attr_form_is_section_offset (attr))
8e19ed76 8281 {
4d3c2250 8282 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8283 }
8284 else
8285 {
4d3c2250
KB
8286 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8287 "common block member");
8e19ed76 8288 }
c906108c 8289 }
639d11d3 8290 if (die->child != NULL)
c906108c 8291 {
639d11d3 8292 child_die = die->child;
c906108c
SS
8293 while (child_die && child_die->tag)
8294 {
74ac6d43
TT
8295 LONGEST offset;
8296
e7c27a73 8297 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8298 if (sym != NULL
8299 && handle_data_member_location (child_die, cu, &offset))
c906108c 8300 {
74ac6d43 8301 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8302 add_symbol_to_list (sym, &global_symbols);
8303 }
8304 child_die = sibling_die (child_die);
8305 }
8306 }
8307}
8308
0114d602 8309/* Create a type for a C++ namespace. */
d9fa45fe 8310
0114d602
DJ
8311static struct type *
8312read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8313{
e7c27a73 8314 struct objfile *objfile = cu->objfile;
0114d602 8315 const char *previous_prefix, *name;
9219021c 8316 int is_anonymous;
0114d602
DJ
8317 struct type *type;
8318
8319 /* For extensions, reuse the type of the original namespace. */
8320 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8321 {
8322 struct die_info *ext_die;
8323 struct dwarf2_cu *ext_cu = cu;
9a619af0 8324
0114d602
DJ
8325 ext_die = dwarf2_extension (die, &ext_cu);
8326 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8327
8328 /* EXT_CU may not be the same as CU.
8329 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8330 return set_die_type (die, type, cu);
8331 }
9219021c 8332
e142c38c 8333 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8334
8335 /* Now build the name of the current namespace. */
8336
0114d602
DJ
8337 previous_prefix = determine_prefix (die, cu);
8338 if (previous_prefix[0] != '\0')
8339 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8340 previous_prefix, name, 0, cu);
0114d602
DJ
8341
8342 /* Create the type. */
8343 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8344 objfile);
8345 TYPE_NAME (type) = (char *) name;
8346 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8347
60531b24 8348 return set_die_type (die, type, cu);
0114d602
DJ
8349}
8350
8351/* Read a C++ namespace. */
8352
8353static void
8354read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8355{
8356 struct objfile *objfile = cu->objfile;
0114d602 8357 int is_anonymous;
9219021c 8358
5c4e30ca
DC
8359 /* Add a symbol associated to this if we haven't seen the namespace
8360 before. Also, add a using directive if it's an anonymous
8361 namespace. */
9219021c 8362
f2f0e013 8363 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8364 {
8365 struct type *type;
8366
0114d602 8367 type = read_type_die (die, cu);
e7c27a73 8368 new_symbol (die, type, cu);
5c4e30ca 8369
e8e80198 8370 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8371 if (is_anonymous)
0114d602
DJ
8372 {
8373 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8374
c0cc3a76 8375 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8376 NULL, NULL, &objfile->objfile_obstack);
0114d602 8377 }
5c4e30ca 8378 }
9219021c 8379
639d11d3 8380 if (die->child != NULL)
d9fa45fe 8381 {
639d11d3 8382 struct die_info *child_die = die->child;
6e70227d 8383
d9fa45fe
DC
8384 while (child_die && child_die->tag)
8385 {
e7c27a73 8386 process_die (child_die, cu);
d9fa45fe
DC
8387 child_die = sibling_die (child_die);
8388 }
8389 }
38d518c9
EZ
8390}
8391
f55ee35c
JK
8392/* Read a Fortran module as type. This DIE can be only a declaration used for
8393 imported module. Still we need that type as local Fortran "use ... only"
8394 declaration imports depend on the created type in determine_prefix. */
8395
8396static struct type *
8397read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8398{
8399 struct objfile *objfile = cu->objfile;
8400 char *module_name;
8401 struct type *type;
8402
8403 module_name = dwarf2_name (die, cu);
8404 if (!module_name)
3e43a32a
MS
8405 complaint (&symfile_complaints,
8406 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8407 die->offset);
8408 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8409
8410 /* determine_prefix uses TYPE_TAG_NAME. */
8411 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8412
8413 return set_die_type (die, type, cu);
8414}
8415
5d7cb8df
JK
8416/* Read a Fortran module. */
8417
8418static void
8419read_module (struct die_info *die, struct dwarf2_cu *cu)
8420{
8421 struct die_info *child_die = die->child;
8422
5d7cb8df
JK
8423 while (child_die && child_die->tag)
8424 {
8425 process_die (child_die, cu);
8426 child_die = sibling_die (child_die);
8427 }
8428}
8429
38d518c9
EZ
8430/* Return the name of the namespace represented by DIE. Set
8431 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8432 namespace. */
8433
8434static const char *
e142c38c 8435namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8436{
8437 struct die_info *current_die;
8438 const char *name = NULL;
8439
8440 /* Loop through the extensions until we find a name. */
8441
8442 for (current_die = die;
8443 current_die != NULL;
f2f0e013 8444 current_die = dwarf2_extension (die, &cu))
38d518c9 8445 {
e142c38c 8446 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8447 if (name != NULL)
8448 break;
8449 }
8450
8451 /* Is it an anonymous namespace? */
8452
8453 *is_anonymous = (name == NULL);
8454 if (*is_anonymous)
2b1dbab0 8455 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8456
8457 return name;
d9fa45fe
DC
8458}
8459
c906108c
SS
8460/* Extract all information from a DW_TAG_pointer_type DIE and add to
8461 the user defined type vector. */
8462
f792889a 8463static struct type *
e7c27a73 8464read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8465{
5e2b427d 8466 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8467 struct comp_unit_head *cu_header = &cu->header;
c906108c 8468 struct type *type;
8b2dbe47
KB
8469 struct attribute *attr_byte_size;
8470 struct attribute *attr_address_class;
8471 int byte_size, addr_class;
7e314c57
JK
8472 struct type *target_type;
8473
8474 target_type = die_type (die, cu);
c906108c 8475
7e314c57
JK
8476 /* The die_type call above may have already set the type for this DIE. */
8477 type = get_die_type (die, cu);
8478 if (type)
8479 return type;
8480
8481 type = lookup_pointer_type (target_type);
8b2dbe47 8482
e142c38c 8483 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8484 if (attr_byte_size)
8485 byte_size = DW_UNSND (attr_byte_size);
c906108c 8486 else
8b2dbe47
KB
8487 byte_size = cu_header->addr_size;
8488
e142c38c 8489 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8490 if (attr_address_class)
8491 addr_class = DW_UNSND (attr_address_class);
8492 else
8493 addr_class = DW_ADDR_none;
8494
8495 /* If the pointer size or address class is different than the
8496 default, create a type variant marked as such and set the
8497 length accordingly. */
8498 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8499 {
5e2b427d 8500 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8501 {
8502 int type_flags;
8503
849957d9 8504 type_flags = gdbarch_address_class_type_flags
5e2b427d 8505 (gdbarch, byte_size, addr_class);
876cecd0
TT
8506 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8507 == 0);
8b2dbe47
KB
8508 type = make_type_with_address_space (type, type_flags);
8509 }
8510 else if (TYPE_LENGTH (type) != byte_size)
8511 {
3e43a32a
MS
8512 complaint (&symfile_complaints,
8513 _("invalid pointer size %d"), byte_size);
8b2dbe47 8514 }
6e70227d 8515 else
9a619af0
MS
8516 {
8517 /* Should we also complain about unhandled address classes? */
8518 }
c906108c 8519 }
8b2dbe47
KB
8520
8521 TYPE_LENGTH (type) = byte_size;
f792889a 8522 return set_die_type (die, type, cu);
c906108c
SS
8523}
8524
8525/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8526 the user defined type vector. */
8527
f792889a 8528static struct type *
e7c27a73 8529read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8530{
8531 struct type *type;
8532 struct type *to_type;
8533 struct type *domain;
8534
e7c27a73
DJ
8535 to_type = die_type (die, cu);
8536 domain = die_containing_type (die, cu);
0d5de010 8537
7e314c57
JK
8538 /* The calls above may have already set the type for this DIE. */
8539 type = get_die_type (die, cu);
8540 if (type)
8541 return type;
8542
0d5de010
DJ
8543 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8544 type = lookup_methodptr_type (to_type);
8545 else
8546 type = lookup_memberptr_type (to_type, domain);
c906108c 8547
f792889a 8548 return set_die_type (die, type, cu);
c906108c
SS
8549}
8550
8551/* Extract all information from a DW_TAG_reference_type DIE and add to
8552 the user defined type vector. */
8553
f792889a 8554static struct type *
e7c27a73 8555read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8556{
e7c27a73 8557 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8558 struct type *type, *target_type;
c906108c
SS
8559 struct attribute *attr;
8560
7e314c57
JK
8561 target_type = die_type (die, cu);
8562
8563 /* The die_type call above may have already set the type for this DIE. */
8564 type = get_die_type (die, cu);
8565 if (type)
8566 return type;
8567
8568 type = lookup_reference_type (target_type);
e142c38c 8569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8570 if (attr)
8571 {
8572 TYPE_LENGTH (type) = DW_UNSND (attr);
8573 }
8574 else
8575 {
107d2387 8576 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8577 }
f792889a 8578 return set_die_type (die, type, cu);
c906108c
SS
8579}
8580
f792889a 8581static struct type *
e7c27a73 8582read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8583{
f792889a 8584 struct type *base_type, *cv_type;
c906108c 8585
e7c27a73 8586 base_type = die_type (die, cu);
7e314c57
JK
8587
8588 /* The die_type call above may have already set the type for this DIE. */
8589 cv_type = get_die_type (die, cu);
8590 if (cv_type)
8591 return cv_type;
8592
2f608a3a
KW
8593 /* In case the const qualifier is applied to an array type, the element type
8594 is so qualified, not the array type (section 6.7.3 of C99). */
8595 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8596 {
8597 struct type *el_type, *inner_array;
8598
8599 base_type = copy_type (base_type);
8600 inner_array = base_type;
8601
8602 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8603 {
8604 TYPE_TARGET_TYPE (inner_array) =
8605 copy_type (TYPE_TARGET_TYPE (inner_array));
8606 inner_array = TYPE_TARGET_TYPE (inner_array);
8607 }
8608
8609 el_type = TYPE_TARGET_TYPE (inner_array);
8610 TYPE_TARGET_TYPE (inner_array) =
8611 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8612
8613 return set_die_type (die, base_type, cu);
8614 }
8615
f792889a
DJ
8616 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8617 return set_die_type (die, cv_type, cu);
c906108c
SS
8618}
8619
f792889a 8620static struct type *
e7c27a73 8621read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8622{
f792889a 8623 struct type *base_type, *cv_type;
c906108c 8624
e7c27a73 8625 base_type = die_type (die, cu);
7e314c57
JK
8626
8627 /* The die_type call above may have already set the type for this DIE. */
8628 cv_type = get_die_type (die, cu);
8629 if (cv_type)
8630 return cv_type;
8631
f792889a
DJ
8632 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8633 return set_die_type (die, cv_type, cu);
c906108c
SS
8634}
8635
8636/* Extract all information from a DW_TAG_string_type DIE and add to
8637 the user defined type vector. It isn't really a user defined type,
8638 but it behaves like one, with other DIE's using an AT_user_def_type
8639 attribute to reference it. */
8640
f792889a 8641static struct type *
e7c27a73 8642read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8643{
e7c27a73 8644 struct objfile *objfile = cu->objfile;
3b7538c0 8645 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8646 struct type *type, *range_type, *index_type, *char_type;
8647 struct attribute *attr;
8648 unsigned int length;
8649
e142c38c 8650 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8651 if (attr)
8652 {
8653 length = DW_UNSND (attr);
8654 }
8655 else
8656 {
0963b4bd 8657 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8658 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8659 if (attr)
8660 {
8661 length = DW_UNSND (attr);
8662 }
8663 else
8664 {
8665 length = 1;
8666 }
c906108c 8667 }
6ccb9162 8668
46bf5051 8669 index_type = objfile_type (objfile)->builtin_int;
c906108c 8670 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8671 char_type = language_string_char_type (cu->language_defn, gdbarch);
8672 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8673
f792889a 8674 return set_die_type (die, type, cu);
c906108c
SS
8675}
8676
8677/* Handle DIES due to C code like:
8678
8679 struct foo
c5aa993b
JM
8680 {
8681 int (*funcp)(int a, long l);
8682 int b;
8683 };
c906108c 8684
0963b4bd 8685 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8686
f792889a 8687static struct type *
e7c27a73 8688read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8689{
0963b4bd
MS
8690 struct type *type; /* Type that this function returns. */
8691 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8692 struct attribute *attr;
8693
e7c27a73 8694 type = die_type (die, cu);
7e314c57
JK
8695
8696 /* The die_type call above may have already set the type for this DIE. */
8697 ftype = get_die_type (die, cu);
8698 if (ftype)
8699 return ftype;
8700
0c8b41f1 8701 ftype = lookup_function_type (type);
c906108c 8702
5b8101ae 8703 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8704 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8705 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8706 || cu->language == language_cplus
5b8101ae
PM
8707 || cu->language == language_java
8708 || cu->language == language_pascal)
876cecd0 8709 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8710 else if (producer_is_realview (cu->producer))
8711 /* RealView does not emit DW_AT_prototyped. We can not
8712 distinguish prototyped and unprototyped functions; default to
8713 prototyped, since that is more common in modern code (and
8714 RealView warns about unprototyped functions). */
8715 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8716
c055b101
CV
8717 /* Store the calling convention in the type if it's available in
8718 the subroutine die. Otherwise set the calling convention to
8719 the default value DW_CC_normal. */
8720 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8721 if (attr)
8722 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8723 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8724 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8725 else
8726 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8727
8728 /* We need to add the subroutine type to the die immediately so
8729 we don't infinitely recurse when dealing with parameters
0963b4bd 8730 declared as the same subroutine type. */
76c10ea2 8731 set_die_type (die, ftype, cu);
6e70227d 8732
639d11d3 8733 if (die->child != NULL)
c906108c 8734 {
8072405b 8735 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8736 struct die_info *child_die;
8072405b 8737 int nparams, iparams;
c906108c
SS
8738
8739 /* Count the number of parameters.
8740 FIXME: GDB currently ignores vararg functions, but knows about
8741 vararg member functions. */
8072405b 8742 nparams = 0;
639d11d3 8743 child_die = die->child;
c906108c
SS
8744 while (child_die && child_die->tag)
8745 {
8746 if (child_die->tag == DW_TAG_formal_parameter)
8747 nparams++;
8748 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8749 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8750 child_die = sibling_die (child_die);
8751 }
8752
8753 /* Allocate storage for parameters and fill them in. */
8754 TYPE_NFIELDS (ftype) = nparams;
8755 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8756 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8757
8072405b
JK
8758 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8759 even if we error out during the parameters reading below. */
8760 for (iparams = 0; iparams < nparams; iparams++)
8761 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8762
8763 iparams = 0;
639d11d3 8764 child_die = die->child;
c906108c
SS
8765 while (child_die && child_die->tag)
8766 {
8767 if (child_die->tag == DW_TAG_formal_parameter)
8768 {
3ce3b1ba
PA
8769 struct type *arg_type;
8770
8771 /* DWARF version 2 has no clean way to discern C++
8772 static and non-static member functions. G++ helps
8773 GDB by marking the first parameter for non-static
8774 member functions (which is the this pointer) as
8775 artificial. We pass this information to
8776 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8777
8778 DWARF version 3 added DW_AT_object_pointer, which GCC
8779 4.5 does not yet generate. */
e142c38c 8780 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8781 if (attr)
8782 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8783 else
418835cc
KS
8784 {
8785 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8786
8787 /* GCC/43521: In java, the formal parameter
8788 "this" is sometimes not marked with DW_AT_artificial. */
8789 if (cu->language == language_java)
8790 {
8791 const char *name = dwarf2_name (child_die, cu);
9a619af0 8792
418835cc
KS
8793 if (name && !strcmp (name, "this"))
8794 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8795 }
8796 }
3ce3b1ba
PA
8797 arg_type = die_type (child_die, cu);
8798
8799 /* RealView does not mark THIS as const, which the testsuite
8800 expects. GCC marks THIS as const in method definitions,
8801 but not in the class specifications (GCC PR 43053). */
8802 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8803 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8804 {
8805 int is_this = 0;
8806 struct dwarf2_cu *arg_cu = cu;
8807 const char *name = dwarf2_name (child_die, cu);
8808
8809 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8810 if (attr)
8811 {
8812 /* If the compiler emits this, use it. */
8813 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8814 is_this = 1;
8815 }
8816 else if (name && strcmp (name, "this") == 0)
8817 /* Function definitions will have the argument names. */
8818 is_this = 1;
8819 else if (name == NULL && iparams == 0)
8820 /* Declarations may not have the names, so like
8821 elsewhere in GDB, assume an artificial first
8822 argument is "this". */
8823 is_this = 1;
8824
8825 if (is_this)
8826 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8827 arg_type, 0);
8828 }
8829
8830 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8831 iparams++;
8832 }
8833 child_die = sibling_die (child_die);
8834 }
8835 }
8836
76c10ea2 8837 return ftype;
c906108c
SS
8838}
8839
f792889a 8840static struct type *
e7c27a73 8841read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8842{
e7c27a73 8843 struct objfile *objfile = cu->objfile;
0114d602 8844 const char *name = NULL;
3c8e0968 8845 struct type *this_type, *target_type;
c906108c 8846
94af9270 8847 name = dwarf2_full_name (NULL, die, cu);
f792889a 8848 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8849 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8850 TYPE_NAME (this_type) = (char *) name;
f792889a 8851 set_die_type (die, this_type, cu);
3c8e0968
DE
8852 target_type = die_type (die, cu);
8853 if (target_type != this_type)
8854 TYPE_TARGET_TYPE (this_type) = target_type;
8855 else
8856 {
8857 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8858 spec and cause infinite loops in GDB. */
8859 complaint (&symfile_complaints,
8860 _("Self-referential DW_TAG_typedef "
8861 "- DIE at 0x%x [in module %s]"),
8862 die->offset, cu->objfile->name);
8863 TYPE_TARGET_TYPE (this_type) = NULL;
8864 }
f792889a 8865 return this_type;
c906108c
SS
8866}
8867
8868/* Find a representation of a given base type and install
8869 it in the TYPE field of the die. */
8870
f792889a 8871static struct type *
e7c27a73 8872read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8873{
e7c27a73 8874 struct objfile *objfile = cu->objfile;
c906108c
SS
8875 struct type *type;
8876 struct attribute *attr;
8877 int encoding = 0, size = 0;
39cbfefa 8878 char *name;
6ccb9162
UW
8879 enum type_code code = TYPE_CODE_INT;
8880 int type_flags = 0;
8881 struct type *target_type = NULL;
c906108c 8882
e142c38c 8883 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8884 if (attr)
8885 {
8886 encoding = DW_UNSND (attr);
8887 }
e142c38c 8888 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8889 if (attr)
8890 {
8891 size = DW_UNSND (attr);
8892 }
39cbfefa 8893 name = dwarf2_name (die, cu);
6ccb9162 8894 if (!name)
c906108c 8895 {
6ccb9162
UW
8896 complaint (&symfile_complaints,
8897 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8898 }
6ccb9162
UW
8899
8900 switch (encoding)
c906108c 8901 {
6ccb9162
UW
8902 case DW_ATE_address:
8903 /* Turn DW_ATE_address into a void * pointer. */
8904 code = TYPE_CODE_PTR;
8905 type_flags |= TYPE_FLAG_UNSIGNED;
8906 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8907 break;
8908 case DW_ATE_boolean:
8909 code = TYPE_CODE_BOOL;
8910 type_flags |= TYPE_FLAG_UNSIGNED;
8911 break;
8912 case DW_ATE_complex_float:
8913 code = TYPE_CODE_COMPLEX;
8914 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8915 break;
8916 case DW_ATE_decimal_float:
8917 code = TYPE_CODE_DECFLOAT;
8918 break;
8919 case DW_ATE_float:
8920 code = TYPE_CODE_FLT;
8921 break;
8922 case DW_ATE_signed:
8923 break;
8924 case DW_ATE_unsigned:
8925 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8926 if (cu->language == language_fortran
8927 && name
8928 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8929 code = TYPE_CODE_CHAR;
6ccb9162
UW
8930 break;
8931 case DW_ATE_signed_char:
6e70227d 8932 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8933 || cu->language == language_pascal
8934 || cu->language == language_fortran)
6ccb9162
UW
8935 code = TYPE_CODE_CHAR;
8936 break;
8937 case DW_ATE_unsigned_char:
868a0084 8938 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8939 || cu->language == language_pascal
8940 || cu->language == language_fortran)
6ccb9162
UW
8941 code = TYPE_CODE_CHAR;
8942 type_flags |= TYPE_FLAG_UNSIGNED;
8943 break;
75079b2b
TT
8944 case DW_ATE_UTF:
8945 /* We just treat this as an integer and then recognize the
8946 type by name elsewhere. */
8947 break;
8948
6ccb9162
UW
8949 default:
8950 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8951 dwarf_type_encoding_name (encoding));
8952 break;
c906108c 8953 }
6ccb9162 8954
0114d602
DJ
8955 type = init_type (code, size, type_flags, NULL, objfile);
8956 TYPE_NAME (type) = name;
6ccb9162
UW
8957 TYPE_TARGET_TYPE (type) = target_type;
8958
0114d602 8959 if (name && strcmp (name, "char") == 0)
876cecd0 8960 TYPE_NOSIGN (type) = 1;
0114d602 8961
f792889a 8962 return set_die_type (die, type, cu);
c906108c
SS
8963}
8964
a02abb62
JB
8965/* Read the given DW_AT_subrange DIE. */
8966
f792889a 8967static struct type *
a02abb62
JB
8968read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8969{
8970 struct type *base_type;
8971 struct type *range_type;
8972 struct attribute *attr;
43bbcdc2
PH
8973 LONGEST low = 0;
8974 LONGEST high = -1;
39cbfefa 8975 char *name;
43bbcdc2 8976 LONGEST negative_mask;
e77813c8 8977
a02abb62 8978 base_type = die_type (die, cu);
953ac07e
JK
8979 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8980 check_typedef (base_type);
a02abb62 8981
7e314c57
JK
8982 /* The die_type call above may have already set the type for this DIE. */
8983 range_type = get_die_type (die, cu);
8984 if (range_type)
8985 return range_type;
8986
e142c38c 8987 if (cu->language == language_fortran)
6e70227d 8988 {
a02abb62
JB
8989 /* FORTRAN implies a lower bound of 1, if not given. */
8990 low = 1;
8991 }
8992
dd5e6932
DJ
8993 /* FIXME: For variable sized arrays either of these could be
8994 a variable rather than a constant value. We'll allow it,
8995 but we don't know how to handle it. */
e142c38c 8996 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8997 if (attr)
8998 low = dwarf2_get_attr_constant_value (attr, 0);
8999
e142c38c 9000 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9001 if (attr)
6e70227d 9002 {
d48323d8 9003 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9004 {
9005 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9006 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9007 FIXME: GDB does not yet know how to handle dynamic
9008 arrays properly, treat them as arrays with unspecified
9009 length for now.
9010
9011 FIXME: jimb/2003-09-22: GDB does not really know
9012 how to handle arrays of unspecified length
9013 either; we just represent them as zero-length
9014 arrays. Choose an appropriate upper bound given
9015 the lower bound we've computed above. */
9016 high = low - 1;
9017 }
9018 else
9019 high = dwarf2_get_attr_constant_value (attr, 1);
9020 }
e77813c8
PM
9021 else
9022 {
9023 attr = dwarf2_attr (die, DW_AT_count, cu);
9024 if (attr)
9025 {
9026 int count = dwarf2_get_attr_constant_value (attr, 1);
9027 high = low + count - 1;
9028 }
c2ff108b
JK
9029 else
9030 {
9031 /* Unspecified array length. */
9032 high = low - 1;
9033 }
e77813c8
PM
9034 }
9035
9036 /* Dwarf-2 specifications explicitly allows to create subrange types
9037 without specifying a base type.
9038 In that case, the base type must be set to the type of
9039 the lower bound, upper bound or count, in that order, if any of these
9040 three attributes references an object that has a type.
9041 If no base type is found, the Dwarf-2 specifications say that
9042 a signed integer type of size equal to the size of an address should
9043 be used.
9044 For the following C code: `extern char gdb_int [];'
9045 GCC produces an empty range DIE.
9046 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9047 high bound or count are not yet handled by this code. */
e77813c8
PM
9048 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9049 {
9050 struct objfile *objfile = cu->objfile;
9051 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9052 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9053 struct type *int_type = objfile_type (objfile)->builtin_int;
9054
9055 /* Test "int", "long int", and "long long int" objfile types,
9056 and select the first one having a size above or equal to the
9057 architecture address size. */
9058 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9059 base_type = int_type;
9060 else
9061 {
9062 int_type = objfile_type (objfile)->builtin_long;
9063 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9064 base_type = int_type;
9065 else
9066 {
9067 int_type = objfile_type (objfile)->builtin_long_long;
9068 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9069 base_type = int_type;
9070 }
9071 }
9072 }
a02abb62 9073
6e70227d 9074 negative_mask =
43bbcdc2
PH
9075 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9076 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9077 low |= negative_mask;
9078 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9079 high |= negative_mask;
9080
a02abb62
JB
9081 range_type = create_range_type (NULL, base_type, low, high);
9082
bbb0eef6
JK
9083 /* Mark arrays with dynamic length at least as an array of unspecified
9084 length. GDB could check the boundary but before it gets implemented at
9085 least allow accessing the array elements. */
d48323d8 9086 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9087 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9088
c2ff108b
JK
9089 /* Ada expects an empty array on no boundary attributes. */
9090 if (attr == NULL && cu->language != language_ada)
9091 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9092
39cbfefa
DJ
9093 name = dwarf2_name (die, cu);
9094 if (name)
9095 TYPE_NAME (range_type) = name;
6e70227d 9096
e142c38c 9097 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9098 if (attr)
9099 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9100
7e314c57
JK
9101 set_die_type (die, range_type, cu);
9102
9103 /* set_die_type should be already done. */
b4ba55a1
JB
9104 set_descriptive_type (range_type, die, cu);
9105
7e314c57 9106 return range_type;
a02abb62 9107}
6e70227d 9108
f792889a 9109static struct type *
81a17f79
JB
9110read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9111{
9112 struct type *type;
81a17f79 9113
81a17f79
JB
9114 /* For now, we only support the C meaning of an unspecified type: void. */
9115
0114d602
DJ
9116 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9117 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9118
f792889a 9119 return set_die_type (die, type, cu);
81a17f79 9120}
a02abb62 9121
51545339
DJ
9122/* Trivial hash function for die_info: the hash value of a DIE
9123 is its offset in .debug_info for this objfile. */
9124
9125static hashval_t
9126die_hash (const void *item)
9127{
9128 const struct die_info *die = item;
9a619af0 9129
51545339
DJ
9130 return die->offset;
9131}
9132
9133/* Trivial comparison function for die_info structures: two DIEs
9134 are equal if they have the same offset. */
9135
9136static int
9137die_eq (const void *item_lhs, const void *item_rhs)
9138{
9139 const struct die_info *die_lhs = item_lhs;
9140 const struct die_info *die_rhs = item_rhs;
9a619af0 9141
51545339
DJ
9142 return die_lhs->offset == die_rhs->offset;
9143}
9144
c906108c
SS
9145/* Read a whole compilation unit into a linked list of dies. */
9146
f9aca02d 9147static struct die_info *
93311388 9148read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9149{
93311388 9150 struct die_reader_specs reader_specs;
98bfdba5 9151 int read_abbrevs = 0;
1d9ec526 9152 struct cleanup *back_to = NULL;
98bfdba5
PA
9153 struct die_info *die;
9154
9155 if (cu->dwarf2_abbrevs == NULL)
9156 {
9157 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9158 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9159 read_abbrevs = 1;
9160 }
93311388 9161
348e048f 9162 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9163 cu->die_hash
9164 = htab_create_alloc_ex (cu->header.length / 12,
9165 die_hash,
9166 die_eq,
9167 NULL,
9168 &cu->comp_unit_obstack,
9169 hashtab_obstack_allocate,
9170 dummy_obstack_deallocate);
9171
93311388
DE
9172 init_cu_die_reader (&reader_specs, cu);
9173
98bfdba5
PA
9174 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9175
9176 if (read_abbrevs)
9177 do_cleanups (back_to);
9178
9179 return die;
639d11d3
DC
9180}
9181
d97bc12b
DE
9182/* Main entry point for reading a DIE and all children.
9183 Read the DIE and dump it if requested. */
9184
9185static struct die_info *
93311388
DE
9186read_die_and_children (const struct die_reader_specs *reader,
9187 gdb_byte *info_ptr,
d97bc12b
DE
9188 gdb_byte **new_info_ptr,
9189 struct die_info *parent)
9190{
93311388 9191 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9192 new_info_ptr, parent);
9193
9194 if (dwarf2_die_debug)
9195 {
348e048f
DE
9196 fprintf_unfiltered (gdb_stdlog,
9197 "\nRead die from %s of %s:\n",
b0df02fd 9198 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9199 ? ".debug_types"
9200 : ".debug_info"),
348e048f 9201 reader->abfd->filename);
d97bc12b
DE
9202 dump_die (result, dwarf2_die_debug);
9203 }
9204
9205 return result;
9206}
9207
639d11d3
DC
9208/* Read a single die and all its descendents. Set the die's sibling
9209 field to NULL; set other fields in the die correctly, and set all
9210 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9211 location of the info_ptr after reading all of those dies. PARENT
9212 is the parent of the die in question. */
9213
9214static struct die_info *
93311388
DE
9215read_die_and_children_1 (const struct die_reader_specs *reader,
9216 gdb_byte *info_ptr,
d97bc12b
DE
9217 gdb_byte **new_info_ptr,
9218 struct die_info *parent)
639d11d3
DC
9219{
9220 struct die_info *die;
fe1b8b76 9221 gdb_byte *cur_ptr;
639d11d3
DC
9222 int has_children;
9223
93311388 9224 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9225 if (die == NULL)
9226 {
9227 *new_info_ptr = cur_ptr;
9228 return NULL;
9229 }
93311388 9230 store_in_ref_table (die, reader->cu);
639d11d3
DC
9231
9232 if (has_children)
348e048f 9233 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9234 else
9235 {
9236 die->child = NULL;
9237 *new_info_ptr = cur_ptr;
9238 }
9239
9240 die->sibling = NULL;
9241 die->parent = parent;
9242 return die;
9243}
9244
9245/* Read a die, all of its descendents, and all of its siblings; set
9246 all of the fields of all of the dies correctly. Arguments are as
9247 in read_die_and_children. */
9248
9249static struct die_info *
93311388
DE
9250read_die_and_siblings (const struct die_reader_specs *reader,
9251 gdb_byte *info_ptr,
fe1b8b76 9252 gdb_byte **new_info_ptr,
639d11d3
DC
9253 struct die_info *parent)
9254{
9255 struct die_info *first_die, *last_sibling;
fe1b8b76 9256 gdb_byte *cur_ptr;
639d11d3 9257
c906108c 9258 cur_ptr = info_ptr;
639d11d3
DC
9259 first_die = last_sibling = NULL;
9260
9261 while (1)
c906108c 9262 {
639d11d3 9263 struct die_info *die
93311388 9264 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9265
1d325ec1 9266 if (die == NULL)
c906108c 9267 {
639d11d3
DC
9268 *new_info_ptr = cur_ptr;
9269 return first_die;
c906108c 9270 }
1d325ec1
DJ
9271
9272 if (!first_die)
9273 first_die = die;
c906108c 9274 else
1d325ec1
DJ
9275 last_sibling->sibling = die;
9276
9277 last_sibling = die;
c906108c 9278 }
c906108c
SS
9279}
9280
93311388
DE
9281/* Read the die from the .debug_info section buffer. Set DIEP to
9282 point to a newly allocated die with its information, except for its
9283 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9284 whether the die has children or not. */
9285
9286static gdb_byte *
9287read_full_die (const struct die_reader_specs *reader,
9288 struct die_info **diep, gdb_byte *info_ptr,
9289 int *has_children)
9290{
9291 unsigned int abbrev_number, bytes_read, i, offset;
9292 struct abbrev_info *abbrev;
9293 struct die_info *die;
9294 struct dwarf2_cu *cu = reader->cu;
9295 bfd *abfd = reader->abfd;
9296
9297 offset = info_ptr - reader->buffer;
9298 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9299 info_ptr += bytes_read;
9300 if (!abbrev_number)
9301 {
9302 *diep = NULL;
9303 *has_children = 0;
9304 return info_ptr;
9305 }
9306
9307 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9308 if (!abbrev)
348e048f
DE
9309 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9310 abbrev_number,
9311 bfd_get_filename (abfd));
9312
93311388
DE
9313 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9314 die->offset = offset;
9315 die->tag = abbrev->tag;
9316 die->abbrev = abbrev_number;
9317
9318 die->num_attrs = abbrev->num_attrs;
9319
9320 for (i = 0; i < abbrev->num_attrs; ++i)
9321 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9322 abfd, info_ptr, cu);
9323
9324 *diep = die;
9325 *has_children = abbrev->has_children;
9326 return info_ptr;
9327}
9328
c906108c
SS
9329/* In DWARF version 2, the description of the debugging information is
9330 stored in a separate .debug_abbrev section. Before we read any
9331 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9332 in a hash table. This function also sets flags in CU describing
9333 the data found in the abbrev table. */
c906108c
SS
9334
9335static void
e7c27a73 9336dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9337{
e7c27a73 9338 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9339 gdb_byte *abbrev_ptr;
c906108c
SS
9340 struct abbrev_info *cur_abbrev;
9341 unsigned int abbrev_number, bytes_read, abbrev_name;
9342 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9343 struct attr_abbrev *cur_attrs;
9344 unsigned int allocated_attrs;
c906108c 9345
0963b4bd 9346 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9347 obstack_init (&cu->abbrev_obstack);
9348 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9349 (ABBREV_HASH_SIZE
9350 * sizeof (struct abbrev_info *)));
9351 memset (cu->dwarf2_abbrevs, 0,
9352 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9353
be391dca
TT
9354 dwarf2_read_section (dwarf2_per_objfile->objfile,
9355 &dwarf2_per_objfile->abbrev);
dce234bc 9356 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9357 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9358 abbrev_ptr += bytes_read;
9359
f3dd6933
DJ
9360 allocated_attrs = ATTR_ALLOC_CHUNK;
9361 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9362
0963b4bd 9363 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9364 while (abbrev_number)
9365 {
f3dd6933 9366 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9367
9368 /* read in abbrev header */
9369 cur_abbrev->number = abbrev_number;
9370 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9371 abbrev_ptr += bytes_read;
9372 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9373 abbrev_ptr += 1;
9374
72bf9492
DJ
9375 if (cur_abbrev->tag == DW_TAG_namespace)
9376 cu->has_namespace_info = 1;
9377
c906108c
SS
9378 /* now read in declarations */
9379 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9380 abbrev_ptr += bytes_read;
9381 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9382 abbrev_ptr += bytes_read;
9383 while (abbrev_name)
9384 {
f3dd6933 9385 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9386 {
f3dd6933
DJ
9387 allocated_attrs += ATTR_ALLOC_CHUNK;
9388 cur_attrs
9389 = xrealloc (cur_attrs, (allocated_attrs
9390 * sizeof (struct attr_abbrev)));
c906108c 9391 }
ae038cb0
DJ
9392
9393 /* Record whether this compilation unit might have
9394 inter-compilation-unit references. If we don't know what form
9395 this attribute will have, then it might potentially be a
9396 DW_FORM_ref_addr, so we conservatively expect inter-CU
9397 references. */
9398
9399 if (abbrev_form == DW_FORM_ref_addr
9400 || abbrev_form == DW_FORM_indirect)
9401 cu->has_form_ref_addr = 1;
9402
f3dd6933
DJ
9403 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9404 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9405 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9406 abbrev_ptr += bytes_read;
9407 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9408 abbrev_ptr += bytes_read;
9409 }
9410
f3dd6933
DJ
9411 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9412 (cur_abbrev->num_attrs
9413 * sizeof (struct attr_abbrev)));
9414 memcpy (cur_abbrev->attrs, cur_attrs,
9415 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9416
c906108c 9417 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9418 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9419 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9420
9421 /* Get next abbreviation.
9422 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9423 always properly terminated with an abbrev number of 0.
9424 Exit loop if we encounter an abbreviation which we have
9425 already read (which means we are about to read the abbreviations
9426 for the next compile unit) or if the end of the abbreviation
9427 table is reached. */
dce234bc
PP
9428 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9429 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9430 break;
9431 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9432 abbrev_ptr += bytes_read;
e7c27a73 9433 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9434 break;
9435 }
f3dd6933
DJ
9436
9437 xfree (cur_attrs);
c906108c
SS
9438}
9439
f3dd6933 9440/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9441
c906108c 9442static void
f3dd6933 9443dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9444{
f3dd6933 9445 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9446
f3dd6933
DJ
9447 obstack_free (&cu->abbrev_obstack, NULL);
9448 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9449}
9450
9451/* Lookup an abbrev_info structure in the abbrev hash table. */
9452
9453static struct abbrev_info *
e7c27a73 9454dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9455{
9456 unsigned int hash_number;
9457 struct abbrev_info *abbrev;
9458
9459 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9460 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9461
9462 while (abbrev)
9463 {
9464 if (abbrev->number == number)
9465 return abbrev;
9466 else
9467 abbrev = abbrev->next;
9468 }
9469 return NULL;
9470}
9471
72bf9492
DJ
9472/* Returns nonzero if TAG represents a type that we might generate a partial
9473 symbol for. */
9474
9475static int
9476is_type_tag_for_partial (int tag)
9477{
9478 switch (tag)
9479 {
9480#if 0
9481 /* Some types that would be reasonable to generate partial symbols for,
9482 that we don't at present. */
9483 case DW_TAG_array_type:
9484 case DW_TAG_file_type:
9485 case DW_TAG_ptr_to_member_type:
9486 case DW_TAG_set_type:
9487 case DW_TAG_string_type:
9488 case DW_TAG_subroutine_type:
9489#endif
9490 case DW_TAG_base_type:
9491 case DW_TAG_class_type:
680b30c7 9492 case DW_TAG_interface_type:
72bf9492
DJ
9493 case DW_TAG_enumeration_type:
9494 case DW_TAG_structure_type:
9495 case DW_TAG_subrange_type:
9496 case DW_TAG_typedef:
9497 case DW_TAG_union_type:
9498 return 1;
9499 default:
9500 return 0;
9501 }
9502}
9503
9504/* Load all DIEs that are interesting for partial symbols into memory. */
9505
9506static struct partial_die_info *
93311388
DE
9507load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9508 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9509{
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
DJ
9636 VAR_DOMAIN, LOC_TYPEDEF,
9637 &cu->objfile->static_psymbols,
9638 0, (CORE_ADDR) 0, cu->language, cu->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]"),
9655 part_die->offset, cu->objfile->name);
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)
72bf9492
DJ
9675 ? &cu->objfile->global_psymbols
9676 : &cu->objfile->static_psymbols,
9677 0, (CORE_ADDR) 0, cu->language, cu->objfile);
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{
fa238c03 9787 unsigned int i;
c906108c 9788 struct attribute attr;
c5aa993b 9789 int has_low_pc_attr = 0;
c906108c
SS
9790 int has_high_pc_attr = 0;
9791
72bf9492 9792 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9793
93311388 9794 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9795
9796 info_ptr += abbrev_len;
9797
9798 if (abbrev == NULL)
9799 return info_ptr;
9800
c906108c
SS
9801 part_die->tag = abbrev->tag;
9802 part_die->has_children = abbrev->has_children;
c906108c
SS
9803
9804 for (i = 0; i < abbrev->num_attrs; ++i)
9805 {
e7c27a73 9806 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9807
9808 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9809 partial symbol table. */
c906108c
SS
9810 switch (attr.name)
9811 {
9812 case DW_AT_name:
71c25dea
TT
9813 switch (part_die->tag)
9814 {
9815 case DW_TAG_compile_unit:
348e048f 9816 case DW_TAG_type_unit:
71c25dea
TT
9817 /* Compilation units have a DW_AT_name that is a filename, not
9818 a source language identifier. */
9819 case DW_TAG_enumeration_type:
9820 case DW_TAG_enumerator:
9821 /* These tags always have simple identifiers already; no need
9822 to canonicalize them. */
9823 part_die->name = DW_STRING (&attr);
9824 break;
9825 default:
9826 part_die->name
9827 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9828 &cu->objfile->objfile_obstack);
71c25dea
TT
9829 break;
9830 }
c906108c 9831 break;
31ef98ae 9832 case DW_AT_linkage_name:
c906108c 9833 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9834 /* Note that both forms of linkage name might appear. We
9835 assume they will be the same, and we only store the last
9836 one we see. */
94af9270
KS
9837 if (cu->language == language_ada)
9838 part_die->name = DW_STRING (&attr);
abc72ce4 9839 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9840 break;
9841 case DW_AT_low_pc:
9842 has_low_pc_attr = 1;
9843 part_die->lowpc = DW_ADDR (&attr);
9844 break;
9845 case DW_AT_high_pc:
9846 has_high_pc_attr = 1;
9847 part_die->highpc = DW_ADDR (&attr);
9848 break;
9849 case DW_AT_location:
0963b4bd 9850 /* Support the .debug_loc offsets. */
8e19ed76
PS
9851 if (attr_form_is_block (&attr))
9852 {
9853 part_die->locdesc = DW_BLOCK (&attr);
9854 }
3690dd37 9855 else if (attr_form_is_section_offset (&attr))
8e19ed76 9856 {
4d3c2250 9857 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9858 }
9859 else
9860 {
4d3c2250
KB
9861 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9862 "partial symbol information");
8e19ed76 9863 }
c906108c 9864 break;
c906108c
SS
9865 case DW_AT_external:
9866 part_die->is_external = DW_UNSND (&attr);
9867 break;
9868 case DW_AT_declaration:
9869 part_die->is_declaration = DW_UNSND (&attr);
9870 break;
9871 case DW_AT_type:
9872 part_die->has_type = 1;
9873 break;
9874 case DW_AT_abstract_origin:
9875 case DW_AT_specification:
72bf9492
DJ
9876 case DW_AT_extension:
9877 part_die->has_specification = 1;
c764a876 9878 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9879 break;
9880 case DW_AT_sibling:
9881 /* Ignore absolute siblings, they might point outside of
9882 the current compile unit. */
9883 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9884 complaint (&symfile_complaints,
9885 _("ignoring absolute DW_AT_sibling"));
c906108c 9886 else
93311388 9887 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9888 break;
fa4028e9
JB
9889 case DW_AT_byte_size:
9890 part_die->has_byte_size = 1;
9891 break;
68511cec
CES
9892 case DW_AT_calling_convention:
9893 /* DWARF doesn't provide a way to identify a program's source-level
9894 entry point. DW_AT_calling_convention attributes are only meant
9895 to describe functions' calling conventions.
9896
9897 However, because it's a necessary piece of information in
9898 Fortran, and because DW_CC_program is the only piece of debugging
9899 information whose definition refers to a 'main program' at all,
9900 several compilers have begun marking Fortran main programs with
9901 DW_CC_program --- even when those functions use the standard
9902 calling conventions.
9903
9904 So until DWARF specifies a way to provide this information and
9905 compilers pick up the new representation, we'll support this
9906 practice. */
9907 if (DW_UNSND (&attr) == DW_CC_program
9908 && cu->language == language_fortran)
01f8c46d
JK
9909 {
9910 set_main_name (part_die->name);
9911
9912 /* As this DIE has a static linkage the name would be difficult
9913 to look up later. */
9914 language_of_main = language_fortran;
9915 }
68511cec 9916 break;
c906108c
SS
9917 default:
9918 break;
9919 }
9920 }
9921
9373cf26
JK
9922 if (has_low_pc_attr && has_high_pc_attr)
9923 {
9924 /* When using the GNU linker, .gnu.linkonce. sections are used to
9925 eliminate duplicate copies of functions and vtables and such.
9926 The linker will arbitrarily choose one and discard the others.
9927 The AT_*_pc values for such functions refer to local labels in
9928 these sections. If the section from that file was discarded, the
9929 labels are not in the output, so the relocs get a value of 0.
9930 If this is a discarded function, mark the pc bounds as invalid,
9931 so that GDB will ignore it. */
9932 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9933 {
9934 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9935
9936 complaint (&symfile_complaints,
9937 _("DW_AT_low_pc %s is zero "
9938 "for DIE at 0x%x [in module %s]"),
9939 paddress (gdbarch, part_die->lowpc),
9940 part_die->offset, cu->objfile->name);
9941 }
9942 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9943 else if (part_die->lowpc >= part_die->highpc)
9944 {
9945 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9946
9947 complaint (&symfile_complaints,
9948 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9949 "for DIE at 0x%x [in module %s]"),
9950 paddress (gdbarch, part_die->lowpc),
9951 paddress (gdbarch, part_die->highpc),
9952 part_die->offset, cu->objfile->name);
9953 }
9954 else
9955 part_die->has_pc_info = 1;
9956 }
85cbf3d3 9957
c906108c
SS
9958 return info_ptr;
9959}
9960
72bf9492
DJ
9961/* Find a cached partial DIE at OFFSET in CU. */
9962
9963static struct partial_die_info *
c764a876 9964find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9965{
9966 struct partial_die_info *lookup_die = NULL;
9967 struct partial_die_info part_die;
9968
9969 part_die.offset = offset;
9970 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9971
72bf9492
DJ
9972 return lookup_die;
9973}
9974
348e048f
DE
9975/* Find a partial DIE at OFFSET, which may or may not be in CU,
9976 except in the case of .debug_types DIEs which do not reference
9977 outside their CU (they do however referencing other types via
55f1336d 9978 DW_FORM_ref_sig8). */
72bf9492
DJ
9979
9980static struct partial_die_info *
c764a876 9981find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9982{
5afb4e99
DJ
9983 struct dwarf2_per_cu_data *per_cu = NULL;
9984 struct partial_die_info *pd = NULL;
72bf9492 9985
b0df02fd 9986 if (cu->per_cu->debug_types_section)
348e048f
DE
9987 {
9988 pd = find_partial_die_in_comp_unit (offset, cu);
9989 if (pd != NULL)
9990 return pd;
9991 goto not_found;
9992 }
9993
45452591 9994 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9995 {
9996 pd = find_partial_die_in_comp_unit (offset, cu);
9997 if (pd != NULL)
9998 return pd;
9999 }
72bf9492 10000
ae038cb0
DJ
10001 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
10002
98bfdba5
PA
10003 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10004 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
10005
10006 per_cu->cu->last_used = 0;
5afb4e99
DJ
10007 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10008
10009 if (pd == NULL && per_cu->load_all_dies == 0)
10010 {
10011 struct cleanup *back_to;
10012 struct partial_die_info comp_unit_die;
10013 struct abbrev_info *abbrev;
10014 unsigned int bytes_read;
10015 char *info_ptr;
10016
10017 per_cu->load_all_dies = 1;
10018
10019 /* Re-read the DIEs. */
10020 back_to = make_cleanup (null_cleanup, 0);
10021 if (per_cu->cu->dwarf2_abbrevs == NULL)
10022 {
10023 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 10024 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10025 }
dce234bc 10026 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
10027 + per_cu->cu->header.offset
10028 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10029 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10030 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
10031 per_cu->cu->objfile->obfd,
10032 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10033 per_cu->cu);
10034 if (comp_unit_die.has_children)
93311388
DE
10035 load_partial_dies (per_cu->cu->objfile->obfd,
10036 dwarf2_per_objfile->info.buffer, info_ptr,
10037 0, per_cu->cu);
5afb4e99
DJ
10038 do_cleanups (back_to);
10039
10040 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10041 }
10042
348e048f
DE
10043 not_found:
10044
5afb4e99
DJ
10045 if (pd == NULL)
10046 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10047 _("could not find partial DIE 0x%x "
10048 "in cache [from module %s]\n"),
5afb4e99
DJ
10049 offset, bfd_get_filename (cu->objfile->obfd));
10050 return pd;
72bf9492
DJ
10051}
10052
abc72ce4
DE
10053/* See if we can figure out if the class lives in a namespace. We do
10054 this by looking for a member function; its demangled name will
10055 contain namespace info, if there is any. */
10056
10057static void
10058guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10059 struct dwarf2_cu *cu)
10060{
10061 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10062 what template types look like, because the demangler
10063 frequently doesn't give the same name as the debug info. We
10064 could fix this by only using the demangled name to get the
10065 prefix (but see comment in read_structure_type). */
10066
10067 struct partial_die_info *real_pdi;
10068 struct partial_die_info *child_pdi;
10069
10070 /* If this DIE (this DIE's specification, if any) has a parent, then
10071 we should not do this. We'll prepend the parent's fully qualified
10072 name when we create the partial symbol. */
10073
10074 real_pdi = struct_pdi;
10075 while (real_pdi->has_specification)
10076 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10077
10078 if (real_pdi->die_parent != NULL)
10079 return;
10080
10081 for (child_pdi = struct_pdi->die_child;
10082 child_pdi != NULL;
10083 child_pdi = child_pdi->die_sibling)
10084 {
10085 if (child_pdi->tag == DW_TAG_subprogram
10086 && child_pdi->linkage_name != NULL)
10087 {
10088 char *actual_class_name
10089 = language_class_name_from_physname (cu->language_defn,
10090 child_pdi->linkage_name);
10091 if (actual_class_name != NULL)
10092 {
10093 struct_pdi->name
10094 = obsavestring (actual_class_name,
10095 strlen (actual_class_name),
10096 &cu->objfile->objfile_obstack);
10097 xfree (actual_class_name);
10098 }
10099 break;
10100 }
10101 }
10102}
10103
72bf9492
DJ
10104/* Adjust PART_DIE before generating a symbol for it. This function
10105 may set the is_external flag or change the DIE's name. */
10106
10107static void
10108fixup_partial_die (struct partial_die_info *part_die,
10109 struct dwarf2_cu *cu)
10110{
abc72ce4
DE
10111 /* Once we've fixed up a die, there's no point in doing so again.
10112 This also avoids a memory leak if we were to call
10113 guess_partial_die_structure_name multiple times. */
10114 if (part_die->fixup_called)
10115 return;
10116
72bf9492
DJ
10117 /* If we found a reference attribute and the DIE has no name, try
10118 to find a name in the referred to DIE. */
10119
10120 if (part_die->name == NULL && part_die->has_specification)
10121 {
10122 struct partial_die_info *spec_die;
72bf9492 10123
10b3939b 10124 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10125
10b3939b 10126 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10127
10128 if (spec_die->name)
10129 {
10130 part_die->name = spec_die->name;
10131
10132 /* Copy DW_AT_external attribute if it is set. */
10133 if (spec_die->is_external)
10134 part_die->is_external = spec_die->is_external;
10135 }
10136 }
10137
10138 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10139
10140 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10141 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10142
abc72ce4
DE
10143 /* If there is no parent die to provide a namespace, and there are
10144 children, see if we can determine the namespace from their linkage
10145 name.
10146 NOTE: We need to do this even if cu->has_namespace_info != 0.
10147 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10148 if (cu->language == language_cplus
8b70b953 10149 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10150 && part_die->die_parent == NULL
10151 && part_die->has_children
10152 && (part_die->tag == DW_TAG_class_type
10153 || part_die->tag == DW_TAG_structure_type
10154 || part_die->tag == DW_TAG_union_type))
10155 guess_partial_die_structure_name (part_die, cu);
10156
53832f31
TT
10157 /* GCC might emit a nameless struct or union that has a linkage
10158 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10159 if (part_die->name == NULL
96408a79
SA
10160 && (part_die->tag == DW_TAG_class_type
10161 || part_die->tag == DW_TAG_interface_type
10162 || part_die->tag == DW_TAG_structure_type
10163 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10164 && part_die->linkage_name != NULL)
10165 {
10166 char *demangled;
10167
10168 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10169 if (demangled)
10170 {
96408a79
SA
10171 const char *base;
10172
10173 /* Strip any leading namespaces/classes, keep only the base name.
10174 DW_AT_name for named DIEs does not contain the prefixes. */
10175 base = strrchr (demangled, ':');
10176 if (base && base > demangled && base[-1] == ':')
10177 base++;
10178 else
10179 base = demangled;
10180
10181 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10182 &cu->objfile->objfile_obstack);
10183 xfree (demangled);
10184 }
10185 }
10186
abc72ce4 10187 part_die->fixup_called = 1;
72bf9492
DJ
10188}
10189
a8329558 10190/* Read an attribute value described by an attribute form. */
c906108c 10191
fe1b8b76 10192static gdb_byte *
a8329558 10193read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10194 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10195 struct dwarf2_cu *cu)
c906108c 10196{
e7c27a73 10197 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10198 unsigned int bytes_read;
10199 struct dwarf_block *blk;
10200
a8329558
KW
10201 attr->form = form;
10202 switch (form)
c906108c 10203 {
c906108c 10204 case DW_FORM_ref_addr:
ae411497
TT
10205 if (cu->header.version == 2)
10206 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10207 else
3e43a32a
MS
10208 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10209 &cu->header, &bytes_read);
ae411497
TT
10210 info_ptr += bytes_read;
10211 break;
10212 case DW_FORM_addr:
e7c27a73 10213 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10214 info_ptr += bytes_read;
c906108c
SS
10215 break;
10216 case DW_FORM_block2:
7b5a2f43 10217 blk = dwarf_alloc_block (cu);
c906108c
SS
10218 blk->size = read_2_bytes (abfd, info_ptr);
10219 info_ptr += 2;
10220 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10221 info_ptr += blk->size;
10222 DW_BLOCK (attr) = blk;
10223 break;
10224 case DW_FORM_block4:
7b5a2f43 10225 blk = dwarf_alloc_block (cu);
c906108c
SS
10226 blk->size = read_4_bytes (abfd, info_ptr);
10227 info_ptr += 4;
10228 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10229 info_ptr += blk->size;
10230 DW_BLOCK (attr) = blk;
10231 break;
10232 case DW_FORM_data2:
10233 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10234 info_ptr += 2;
10235 break;
10236 case DW_FORM_data4:
10237 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10238 info_ptr += 4;
10239 break;
10240 case DW_FORM_data8:
10241 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10242 info_ptr += 8;
10243 break;
2dc7f7b3
TT
10244 case DW_FORM_sec_offset:
10245 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10246 info_ptr += bytes_read;
10247 break;
c906108c 10248 case DW_FORM_string:
9b1c24c8 10249 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10250 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10251 info_ptr += bytes_read;
10252 break;
4bdf3d34
JJ
10253 case DW_FORM_strp:
10254 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10255 &bytes_read);
8285870a 10256 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10257 info_ptr += bytes_read;
10258 break;
2dc7f7b3 10259 case DW_FORM_exprloc:
c906108c 10260 case DW_FORM_block:
7b5a2f43 10261 blk = dwarf_alloc_block (cu);
c906108c
SS
10262 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10263 info_ptr += bytes_read;
10264 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10265 info_ptr += blk->size;
10266 DW_BLOCK (attr) = blk;
10267 break;
10268 case DW_FORM_block1:
7b5a2f43 10269 blk = dwarf_alloc_block (cu);
c906108c
SS
10270 blk->size = read_1_byte (abfd, info_ptr);
10271 info_ptr += 1;
10272 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10273 info_ptr += blk->size;
10274 DW_BLOCK (attr) = blk;
10275 break;
10276 case DW_FORM_data1:
10277 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10278 info_ptr += 1;
10279 break;
10280 case DW_FORM_flag:
10281 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10282 info_ptr += 1;
10283 break;
2dc7f7b3
TT
10284 case DW_FORM_flag_present:
10285 DW_UNSND (attr) = 1;
10286 break;
c906108c
SS
10287 case DW_FORM_sdata:
10288 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10289 info_ptr += bytes_read;
10290 break;
10291 case DW_FORM_udata:
10292 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10293 info_ptr += bytes_read;
10294 break;
10295 case DW_FORM_ref1:
10b3939b 10296 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10297 info_ptr += 1;
10298 break;
10299 case DW_FORM_ref2:
10b3939b 10300 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10301 info_ptr += 2;
10302 break;
10303 case DW_FORM_ref4:
10b3939b 10304 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10305 info_ptr += 4;
10306 break;
613e1657 10307 case DW_FORM_ref8:
10b3939b 10308 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10309 info_ptr += 8;
10310 break;
55f1336d 10311 case DW_FORM_ref_sig8:
348e048f
DE
10312 /* Convert the signature to something we can record in DW_UNSND
10313 for later lookup.
10314 NOTE: This is NULL if the type wasn't found. */
10315 DW_SIGNATURED_TYPE (attr) =
10316 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10317 info_ptr += 8;
10318 break;
c906108c 10319 case DW_FORM_ref_udata:
10b3939b
DJ
10320 DW_ADDR (attr) = (cu->header.offset
10321 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10322 info_ptr += bytes_read;
10323 break;
c906108c 10324 case DW_FORM_indirect:
a8329558
KW
10325 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10326 info_ptr += bytes_read;
e7c27a73 10327 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10328 break;
c906108c 10329 default:
8a3fe4f8 10330 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10331 dwarf_form_name (form),
10332 bfd_get_filename (abfd));
c906108c 10333 }
28e94949
JB
10334
10335 /* We have seen instances where the compiler tried to emit a byte
10336 size attribute of -1 which ended up being encoded as an unsigned
10337 0xffffffff. Although 0xffffffff is technically a valid size value,
10338 an object of this size seems pretty unlikely so we can relatively
10339 safely treat these cases as if the size attribute was invalid and
10340 treat them as zero by default. */
10341 if (attr->name == DW_AT_byte_size
10342 && form == DW_FORM_data4
10343 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10344 {
10345 complaint
10346 (&symfile_complaints,
43bbcdc2
PH
10347 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10348 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10349 DW_UNSND (attr) = 0;
10350 }
28e94949 10351
c906108c
SS
10352 return info_ptr;
10353}
10354
a8329558
KW
10355/* Read an attribute described by an abbreviated attribute. */
10356
fe1b8b76 10357static gdb_byte *
a8329558 10358read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10359 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10360{
10361 attr->name = abbrev->name;
e7c27a73 10362 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10363}
10364
0963b4bd 10365/* Read dwarf information from a buffer. */
c906108c
SS
10366
10367static unsigned int
fe1b8b76 10368read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10369{
fe1b8b76 10370 return bfd_get_8 (abfd, buf);
c906108c
SS
10371}
10372
10373static int
fe1b8b76 10374read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10375{
fe1b8b76 10376 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10377}
10378
10379static unsigned int
fe1b8b76 10380read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10381{
fe1b8b76 10382 return bfd_get_16 (abfd, buf);
c906108c
SS
10383}
10384
21ae7a4d
JK
10385static int
10386read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10387{
10388 return bfd_get_signed_16 (abfd, buf);
10389}
10390
c906108c 10391static unsigned int
fe1b8b76 10392read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10393{
fe1b8b76 10394 return bfd_get_32 (abfd, buf);
c906108c
SS
10395}
10396
21ae7a4d
JK
10397static int
10398read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10399{
10400 return bfd_get_signed_32 (abfd, buf);
10401}
10402
93311388 10403static ULONGEST
fe1b8b76 10404read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10405{
fe1b8b76 10406 return bfd_get_64 (abfd, buf);
c906108c
SS
10407}
10408
10409static CORE_ADDR
fe1b8b76 10410read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10411 unsigned int *bytes_read)
c906108c 10412{
e7c27a73 10413 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10414 CORE_ADDR retval = 0;
10415
107d2387 10416 if (cu_header->signed_addr_p)
c906108c 10417 {
107d2387
AC
10418 switch (cu_header->addr_size)
10419 {
10420 case 2:
fe1b8b76 10421 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10422 break;
10423 case 4:
fe1b8b76 10424 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10425 break;
10426 case 8:
fe1b8b76 10427 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10428 break;
10429 default:
8e65ff28 10430 internal_error (__FILE__, __LINE__,
e2e0b3e5 10431 _("read_address: bad switch, signed [in module %s]"),
659b0389 10432 bfd_get_filename (abfd));
107d2387
AC
10433 }
10434 }
10435 else
10436 {
10437 switch (cu_header->addr_size)
10438 {
10439 case 2:
fe1b8b76 10440 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10441 break;
10442 case 4:
fe1b8b76 10443 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10444 break;
10445 case 8:
fe1b8b76 10446 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10447 break;
10448 default:
8e65ff28 10449 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10450 _("read_address: bad switch, "
10451 "unsigned [in module %s]"),
659b0389 10452 bfd_get_filename (abfd));
107d2387 10453 }
c906108c 10454 }
64367e0a 10455
107d2387
AC
10456 *bytes_read = cu_header->addr_size;
10457 return retval;
c906108c
SS
10458}
10459
f7ef9339 10460/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10461 specification allows the initial length to take up either 4 bytes
10462 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10463 bytes describe the length and all offsets will be 8 bytes in length
10464 instead of 4.
10465
f7ef9339
KB
10466 An older, non-standard 64-bit format is also handled by this
10467 function. The older format in question stores the initial length
10468 as an 8-byte quantity without an escape value. Lengths greater
10469 than 2^32 aren't very common which means that the initial 4 bytes
10470 is almost always zero. Since a length value of zero doesn't make
10471 sense for the 32-bit format, this initial zero can be considered to
10472 be an escape value which indicates the presence of the older 64-bit
10473 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10474 greater than 4GB. If it becomes necessary to handle lengths
10475 somewhat larger than 4GB, we could allow other small values (such
10476 as the non-sensical values of 1, 2, and 3) to also be used as
10477 escape values indicating the presence of the old format.
f7ef9339 10478
917c78fc
MK
10479 The value returned via bytes_read should be used to increment the
10480 relevant pointer after calling read_initial_length().
c764a876 10481
613e1657
KB
10482 [ Note: read_initial_length() and read_offset() are based on the
10483 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10484 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10485 from:
10486
f7ef9339 10487 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10488
613e1657
KB
10489 This document is only a draft and is subject to change. (So beware.)
10490
f7ef9339 10491 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10492 determined empirically by examining 64-bit ELF files produced by
10493 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10494
10495 - Kevin, July 16, 2002
613e1657
KB
10496 ] */
10497
10498static LONGEST
c764a876 10499read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10500{
fe1b8b76 10501 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10502
dd373385 10503 if (length == 0xffffffff)
613e1657 10504 {
fe1b8b76 10505 length = bfd_get_64 (abfd, buf + 4);
613e1657 10506 *bytes_read = 12;
613e1657 10507 }
dd373385 10508 else if (length == 0)
f7ef9339 10509 {
dd373385 10510 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10511 length = bfd_get_64 (abfd, buf);
f7ef9339 10512 *bytes_read = 8;
f7ef9339 10513 }
613e1657
KB
10514 else
10515 {
10516 *bytes_read = 4;
613e1657
KB
10517 }
10518
c764a876
DE
10519 return length;
10520}
dd373385 10521
c764a876
DE
10522/* Cover function for read_initial_length.
10523 Returns the length of the object at BUF, and stores the size of the
10524 initial length in *BYTES_READ and stores the size that offsets will be in
10525 *OFFSET_SIZE.
10526 If the initial length size is not equivalent to that specified in
10527 CU_HEADER then issue a complaint.
10528 This is useful when reading non-comp-unit headers. */
dd373385 10529
c764a876
DE
10530static LONGEST
10531read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10532 const struct comp_unit_head *cu_header,
10533 unsigned int *bytes_read,
10534 unsigned int *offset_size)
10535{
10536 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10537
10538 gdb_assert (cu_header->initial_length_size == 4
10539 || cu_header->initial_length_size == 8
10540 || cu_header->initial_length_size == 12);
10541
10542 if (cu_header->initial_length_size != *bytes_read)
10543 complaint (&symfile_complaints,
10544 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10545
c764a876 10546 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10547 return length;
613e1657
KB
10548}
10549
10550/* Read an offset from the data stream. The size of the offset is
917c78fc 10551 given by cu_header->offset_size. */
613e1657
KB
10552
10553static LONGEST
fe1b8b76 10554read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10555 unsigned int *bytes_read)
c764a876
DE
10556{
10557 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10558
c764a876
DE
10559 *bytes_read = cu_header->offset_size;
10560 return offset;
10561}
10562
10563/* Read an offset from the data stream. */
10564
10565static LONGEST
10566read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10567{
10568 LONGEST retval = 0;
10569
c764a876 10570 switch (offset_size)
613e1657
KB
10571 {
10572 case 4:
fe1b8b76 10573 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10574 break;
10575 case 8:
fe1b8b76 10576 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10577 break;
10578 default:
8e65ff28 10579 internal_error (__FILE__, __LINE__,
c764a876 10580 _("read_offset_1: bad switch [in module %s]"),
659b0389 10581 bfd_get_filename (abfd));
613e1657
KB
10582 }
10583
917c78fc 10584 return retval;
613e1657
KB
10585}
10586
fe1b8b76
JB
10587static gdb_byte *
10588read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10589{
10590 /* If the size of a host char is 8 bits, we can return a pointer
10591 to the buffer, otherwise we have to copy the data to a buffer
10592 allocated on the temporary obstack. */
4bdf3d34 10593 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10594 return buf;
c906108c
SS
10595}
10596
10597static char *
9b1c24c8 10598read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10599{
10600 /* If the size of a host char is 8 bits, we can return a pointer
10601 to the string, otherwise we have to copy the string to a buffer
10602 allocated on the temporary obstack. */
4bdf3d34 10603 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10604 if (*buf == '\0')
10605 {
10606 *bytes_read_ptr = 1;
10607 return NULL;
10608 }
fe1b8b76
JB
10609 *bytes_read_ptr = strlen ((char *) buf) + 1;
10610 return (char *) buf;
4bdf3d34
JJ
10611}
10612
10613static char *
cf2c3c16 10614read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10615{
be391dca 10616 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10617 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10618 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10619 bfd_get_filename (abfd));
dce234bc 10620 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10621 error (_("DW_FORM_strp pointing outside of "
10622 ".debug_str section [in module %s]"),
10623 bfd_get_filename (abfd));
4bdf3d34 10624 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10625 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10626 return NULL;
dce234bc 10627 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10628}
10629
cf2c3c16
TT
10630static char *
10631read_indirect_string (bfd *abfd, gdb_byte *buf,
10632 const struct comp_unit_head *cu_header,
10633 unsigned int *bytes_read_ptr)
10634{
10635 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10636
10637 return read_indirect_string_at_offset (abfd, str_offset);
10638}
10639
ce5d95e1 10640static unsigned long
fe1b8b76 10641read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10642{
ce5d95e1
JB
10643 unsigned long result;
10644 unsigned int num_read;
c906108c
SS
10645 int i, shift;
10646 unsigned char byte;
10647
10648 result = 0;
10649 shift = 0;
10650 num_read = 0;
10651 i = 0;
10652 while (1)
10653 {
fe1b8b76 10654 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10655 buf++;
10656 num_read++;
ce5d95e1 10657 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10658 if ((byte & 128) == 0)
10659 {
10660 break;
10661 }
10662 shift += 7;
10663 }
10664 *bytes_read_ptr = num_read;
10665 return result;
10666}
10667
ce5d95e1 10668static long
fe1b8b76 10669read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10670{
ce5d95e1 10671 long result;
77e0b926 10672 int i, shift, num_read;
c906108c
SS
10673 unsigned char byte;
10674
10675 result = 0;
10676 shift = 0;
c906108c
SS
10677 num_read = 0;
10678 i = 0;
10679 while (1)
10680 {
fe1b8b76 10681 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10682 buf++;
10683 num_read++;
ce5d95e1 10684 result |= ((long)(byte & 127) << shift);
c906108c
SS
10685 shift += 7;
10686 if ((byte & 128) == 0)
10687 {
10688 break;
10689 }
10690 }
77e0b926
DJ
10691 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10692 result |= -(((long)1) << shift);
c906108c
SS
10693 *bytes_read_ptr = num_read;
10694 return result;
10695}
10696
4bb7a0a7
DJ
10697/* Return a pointer to just past the end of an LEB128 number in BUF. */
10698
fe1b8b76
JB
10699static gdb_byte *
10700skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10701{
10702 int byte;
10703
10704 while (1)
10705 {
fe1b8b76 10706 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10707 buf++;
10708 if ((byte & 128) == 0)
10709 return buf;
10710 }
10711}
10712
c906108c 10713static void
e142c38c 10714set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10715{
10716 switch (lang)
10717 {
10718 case DW_LANG_C89:
76bee0cc 10719 case DW_LANG_C99:
c906108c 10720 case DW_LANG_C:
e142c38c 10721 cu->language = language_c;
c906108c
SS
10722 break;
10723 case DW_LANG_C_plus_plus:
e142c38c 10724 cu->language = language_cplus;
c906108c 10725 break;
6aecb9c2
JB
10726 case DW_LANG_D:
10727 cu->language = language_d;
10728 break;
c906108c
SS
10729 case DW_LANG_Fortran77:
10730 case DW_LANG_Fortran90:
b21b22e0 10731 case DW_LANG_Fortran95:
e142c38c 10732 cu->language = language_fortran;
c906108c
SS
10733 break;
10734 case DW_LANG_Mips_Assembler:
e142c38c 10735 cu->language = language_asm;
c906108c 10736 break;
bebd888e 10737 case DW_LANG_Java:
e142c38c 10738 cu->language = language_java;
bebd888e 10739 break;
c906108c 10740 case DW_LANG_Ada83:
8aaf0b47 10741 case DW_LANG_Ada95:
bc5f45f8
JB
10742 cu->language = language_ada;
10743 break;
72019c9c
GM
10744 case DW_LANG_Modula2:
10745 cu->language = language_m2;
10746 break;
fe8e67fd
PM
10747 case DW_LANG_Pascal83:
10748 cu->language = language_pascal;
10749 break;
22566fbd
DJ
10750 case DW_LANG_ObjC:
10751 cu->language = language_objc;
10752 break;
c906108c
SS
10753 case DW_LANG_Cobol74:
10754 case DW_LANG_Cobol85:
c906108c 10755 default:
e142c38c 10756 cu->language = language_minimal;
c906108c
SS
10757 break;
10758 }
e142c38c 10759 cu->language_defn = language_def (cu->language);
c906108c
SS
10760}
10761
10762/* Return the named attribute or NULL if not there. */
10763
10764static struct attribute *
e142c38c 10765dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10766{
10767 unsigned int i;
10768 struct attribute *spec = NULL;
10769
10770 for (i = 0; i < die->num_attrs; ++i)
10771 {
10772 if (die->attrs[i].name == name)
10b3939b 10773 return &die->attrs[i];
c906108c
SS
10774 if (die->attrs[i].name == DW_AT_specification
10775 || die->attrs[i].name == DW_AT_abstract_origin)
10776 spec = &die->attrs[i];
10777 }
c906108c 10778
10b3939b 10779 if (spec)
f2f0e013
DJ
10780 {
10781 die = follow_die_ref (die, spec, &cu);
10782 return dwarf2_attr (die, name, cu);
10783 }
c5aa993b 10784
c906108c
SS
10785 return NULL;
10786}
10787
348e048f
DE
10788/* Return the named attribute or NULL if not there,
10789 but do not follow DW_AT_specification, etc.
10790 This is for use in contexts where we're reading .debug_types dies.
10791 Following DW_AT_specification, DW_AT_abstract_origin will take us
10792 back up the chain, and we want to go down. */
10793
10794static struct attribute *
10795dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10796 struct dwarf2_cu *cu)
10797{
10798 unsigned int i;
10799
10800 for (i = 0; i < die->num_attrs; ++i)
10801 if (die->attrs[i].name == name)
10802 return &die->attrs[i];
10803
10804 return NULL;
10805}
10806
05cf31d1
JB
10807/* Return non-zero iff the attribute NAME is defined for the given DIE,
10808 and holds a non-zero value. This function should only be used for
2dc7f7b3 10809 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10810
10811static int
10812dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10813{
10814 struct attribute *attr = dwarf2_attr (die, name, cu);
10815
10816 return (attr && DW_UNSND (attr));
10817}
10818
3ca72b44 10819static int
e142c38c 10820die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10821{
05cf31d1
JB
10822 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10823 which value is non-zero. However, we have to be careful with
10824 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10825 (via dwarf2_flag_true_p) follows this attribute. So we may
10826 end up accidently finding a declaration attribute that belongs
10827 to a different DIE referenced by the specification attribute,
10828 even though the given DIE does not have a declaration attribute. */
10829 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10830 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10831}
10832
63d06c5c 10833/* Return the die giving the specification for DIE, if there is
f2f0e013 10834 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10835 containing the return value on output. If there is no
10836 specification, but there is an abstract origin, that is
10837 returned. */
63d06c5c
DC
10838
10839static struct die_info *
f2f0e013 10840die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10841{
f2f0e013
DJ
10842 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10843 *spec_cu);
63d06c5c 10844
edb3359d
DJ
10845 if (spec_attr == NULL)
10846 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10847
63d06c5c
DC
10848 if (spec_attr == NULL)
10849 return NULL;
10850 else
f2f0e013 10851 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10852}
c906108c 10853
debd256d 10854/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10855 refers to.
10856 NOTE: This is also used as a "cleanup" function. */
10857
debd256d
JB
10858static void
10859free_line_header (struct line_header *lh)
10860{
10861 if (lh->standard_opcode_lengths)
a8bc7b56 10862 xfree (lh->standard_opcode_lengths);
debd256d
JB
10863
10864 /* Remember that all the lh->file_names[i].name pointers are
10865 pointers into debug_line_buffer, and don't need to be freed. */
10866 if (lh->file_names)
a8bc7b56 10867 xfree (lh->file_names);
debd256d
JB
10868
10869 /* Similarly for the include directory names. */
10870 if (lh->include_dirs)
a8bc7b56 10871 xfree (lh->include_dirs);
debd256d 10872
a8bc7b56 10873 xfree (lh);
debd256d
JB
10874}
10875
debd256d 10876/* Add an entry to LH's include directory table. */
ae2de4f8 10877
debd256d
JB
10878static void
10879add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10880{
debd256d
JB
10881 /* Grow the array if necessary. */
10882 if (lh->include_dirs_size == 0)
c5aa993b 10883 {
debd256d
JB
10884 lh->include_dirs_size = 1; /* for testing */
10885 lh->include_dirs = xmalloc (lh->include_dirs_size
10886 * sizeof (*lh->include_dirs));
10887 }
10888 else if (lh->num_include_dirs >= lh->include_dirs_size)
10889 {
10890 lh->include_dirs_size *= 2;
10891 lh->include_dirs = xrealloc (lh->include_dirs,
10892 (lh->include_dirs_size
10893 * sizeof (*lh->include_dirs)));
c5aa993b 10894 }
c906108c 10895
debd256d
JB
10896 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10897}
6e70227d 10898
debd256d 10899/* Add an entry to LH's file name table. */
ae2de4f8 10900
debd256d
JB
10901static void
10902add_file_name (struct line_header *lh,
10903 char *name,
10904 unsigned int dir_index,
10905 unsigned int mod_time,
10906 unsigned int length)
10907{
10908 struct file_entry *fe;
10909
10910 /* Grow the array if necessary. */
10911 if (lh->file_names_size == 0)
10912 {
10913 lh->file_names_size = 1; /* for testing */
10914 lh->file_names = xmalloc (lh->file_names_size
10915 * sizeof (*lh->file_names));
10916 }
10917 else if (lh->num_file_names >= lh->file_names_size)
10918 {
10919 lh->file_names_size *= 2;
10920 lh->file_names = xrealloc (lh->file_names,
10921 (lh->file_names_size
10922 * sizeof (*lh->file_names)));
10923 }
10924
10925 fe = &lh->file_names[lh->num_file_names++];
10926 fe->name = name;
10927 fe->dir_index = dir_index;
10928 fe->mod_time = mod_time;
10929 fe->length = length;
aaa75496 10930 fe->included_p = 0;
cb1df416 10931 fe->symtab = NULL;
debd256d 10932}
6e70227d 10933
debd256d 10934/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10935 .debug_line, according to the endianness of ABFD. Return a pointer
10936 to a struct line_header, allocated using xmalloc.
debd256d
JB
10937
10938 NOTE: the strings in the include directory and file name tables of
10939 the returned object point into debug_line_buffer, and must not be
10940 freed. */
ae2de4f8 10941
debd256d
JB
10942static struct line_header *
10943dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10944 struct dwarf2_cu *cu)
debd256d
JB
10945{
10946 struct cleanup *back_to;
10947 struct line_header *lh;
fe1b8b76 10948 gdb_byte *line_ptr;
c764a876 10949 unsigned int bytes_read, offset_size;
debd256d
JB
10950 int i;
10951 char *cur_dir, *cur_file;
10952
be391dca 10953 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10954 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10955 {
e2e0b3e5 10956 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10957 return 0;
10958 }
10959
a738430d
MK
10960 /* Make sure that at least there's room for the total_length field.
10961 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10962 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10963 {
4d3c2250 10964 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10965 return 0;
10966 }
10967
10968 lh = xmalloc (sizeof (*lh));
10969 memset (lh, 0, sizeof (*lh));
10970 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10971 (void *) lh);
10972
dce234bc 10973 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10974
a738430d 10975 /* Read in the header. */
6e70227d 10976 lh->total_length =
c764a876
DE
10977 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10978 &bytes_read, &offset_size);
debd256d 10979 line_ptr += bytes_read;
dce234bc
PP
10980 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10981 + dwarf2_per_objfile->line.size))
debd256d 10982 {
4d3c2250 10983 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10984 return 0;
10985 }
10986 lh->statement_program_end = line_ptr + lh->total_length;
10987 lh->version = read_2_bytes (abfd, line_ptr);
10988 line_ptr += 2;
c764a876
DE
10989 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10990 line_ptr += offset_size;
debd256d
JB
10991 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10992 line_ptr += 1;
2dc7f7b3
TT
10993 if (lh->version >= 4)
10994 {
10995 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10996 line_ptr += 1;
10997 }
10998 else
10999 lh->maximum_ops_per_instruction = 1;
11000
11001 if (lh->maximum_ops_per_instruction == 0)
11002 {
11003 lh->maximum_ops_per_instruction = 1;
11004 complaint (&symfile_complaints,
3e43a32a
MS
11005 _("invalid maximum_ops_per_instruction "
11006 "in `.debug_line' section"));
2dc7f7b3
TT
11007 }
11008
debd256d
JB
11009 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11010 line_ptr += 1;
11011 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11012 line_ptr += 1;
11013 lh->line_range = read_1_byte (abfd, line_ptr);
11014 line_ptr += 1;
11015 lh->opcode_base = read_1_byte (abfd, line_ptr);
11016 line_ptr += 1;
11017 lh->standard_opcode_lengths
fe1b8b76 11018 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11019
11020 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11021 for (i = 1; i < lh->opcode_base; ++i)
11022 {
11023 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11024 line_ptr += 1;
11025 }
11026
a738430d 11027 /* Read directory table. */
9b1c24c8 11028 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11029 {
11030 line_ptr += bytes_read;
11031 add_include_dir (lh, cur_dir);
11032 }
11033 line_ptr += bytes_read;
11034
a738430d 11035 /* Read file name table. */
9b1c24c8 11036 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11037 {
11038 unsigned int dir_index, mod_time, length;
11039
11040 line_ptr += bytes_read;
11041 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11042 line_ptr += bytes_read;
11043 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11044 line_ptr += bytes_read;
11045 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11046 line_ptr += bytes_read;
11047
11048 add_file_name (lh, cur_file, dir_index, mod_time, length);
11049 }
11050 line_ptr += bytes_read;
6e70227d 11051 lh->statement_program_start = line_ptr;
debd256d 11052
dce234bc
PP
11053 if (line_ptr > (dwarf2_per_objfile->line.buffer
11054 + dwarf2_per_objfile->line.size))
4d3c2250 11055 complaint (&symfile_complaints,
3e43a32a
MS
11056 _("line number info header doesn't "
11057 "fit in `.debug_line' section"));
debd256d
JB
11058
11059 discard_cleanups (back_to);
11060 return lh;
11061}
c906108c 11062
c6da4cef
DE
11063/* Subroutine of dwarf_decode_lines to simplify it.
11064 Return the file name of the psymtab for included file FILE_INDEX
11065 in line header LH of PST.
11066 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11067 If space for the result is malloc'd, it will be freed by a cleanup.
11068 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11069
11070static char *
11071psymtab_include_file_name (const struct line_header *lh, int file_index,
11072 const struct partial_symtab *pst,
11073 const char *comp_dir)
11074{
11075 const struct file_entry fe = lh->file_names [file_index];
11076 char *include_name = fe.name;
11077 char *include_name_to_compare = include_name;
11078 char *dir_name = NULL;
72b9f47f
TT
11079 const char *pst_filename;
11080 char *copied_name = NULL;
c6da4cef
DE
11081 int file_is_pst;
11082
11083 if (fe.dir_index)
11084 dir_name = lh->include_dirs[fe.dir_index - 1];
11085
11086 if (!IS_ABSOLUTE_PATH (include_name)
11087 && (dir_name != NULL || comp_dir != NULL))
11088 {
11089 /* Avoid creating a duplicate psymtab for PST.
11090 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11091 Before we do the comparison, however, we need to account
11092 for DIR_NAME and COMP_DIR.
11093 First prepend dir_name (if non-NULL). If we still don't
11094 have an absolute path prepend comp_dir (if non-NULL).
11095 However, the directory we record in the include-file's
11096 psymtab does not contain COMP_DIR (to match the
11097 corresponding symtab(s)).
11098
11099 Example:
11100
11101 bash$ cd /tmp
11102 bash$ gcc -g ./hello.c
11103 include_name = "hello.c"
11104 dir_name = "."
11105 DW_AT_comp_dir = comp_dir = "/tmp"
11106 DW_AT_name = "./hello.c" */
11107
11108 if (dir_name != NULL)
11109 {
11110 include_name = concat (dir_name, SLASH_STRING,
11111 include_name, (char *)NULL);
11112 include_name_to_compare = include_name;
11113 make_cleanup (xfree, include_name);
11114 }
11115 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11116 {
11117 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11118 include_name, (char *)NULL);
11119 }
11120 }
11121
11122 pst_filename = pst->filename;
11123 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11124 {
72b9f47f
TT
11125 copied_name = concat (pst->dirname, SLASH_STRING,
11126 pst_filename, (char *)NULL);
11127 pst_filename = copied_name;
c6da4cef
DE
11128 }
11129
1e3fad37 11130 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11131
11132 if (include_name_to_compare != include_name)
11133 xfree (include_name_to_compare);
72b9f47f
TT
11134 if (copied_name != NULL)
11135 xfree (copied_name);
c6da4cef
DE
11136
11137 if (file_is_pst)
11138 return NULL;
11139 return include_name;
11140}
11141
c91513d8
PP
11142/* Ignore this record_line request. */
11143
11144static void
11145noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11146{
11147 return;
11148}
11149
aaa75496
JB
11150/* Decode the Line Number Program (LNP) for the given line_header
11151 structure and CU. The actual information extracted and the type
11152 of structures created from the LNP depends on the value of PST.
11153
11154 1. If PST is NULL, then this procedure uses the data from the program
11155 to create all necessary symbol tables, and their linetables.
6e70227d 11156
aaa75496
JB
11157 2. If PST is not NULL, this procedure reads the program to determine
11158 the list of files included by the unit represented by PST, and
c6da4cef
DE
11159 builds all the associated partial symbol tables.
11160
11161 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11162 It is used for relative paths in the line table.
11163 NOTE: When processing partial symtabs (pst != NULL),
11164 comp_dir == pst->dirname.
11165
11166 NOTE: It is important that psymtabs have the same file name (via strcmp)
11167 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11168 symtab we don't use it in the name of the psymtabs we create.
11169 E.g. expand_line_sal requires this when finding psymtabs to expand.
11170 A good testcase for this is mb-inline.exp. */
debd256d 11171
c906108c 11172static void
72b9f47f 11173dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11174 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11175{
a8c50c1f 11176 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11177 gdb_byte *line_end;
a8c50c1f 11178 unsigned int bytes_read, extended_len;
c906108c 11179 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11180 CORE_ADDR baseaddr;
11181 struct objfile *objfile = cu->objfile;
fbf65064 11182 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11183 const int decode_for_pst_p = (pst != NULL);
cb1df416 11184 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11185 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11186 = record_line;
e142c38c
DJ
11187
11188 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11189
debd256d
JB
11190 line_ptr = lh->statement_program_start;
11191 line_end = lh->statement_program_end;
c906108c
SS
11192
11193 /* Read the statement sequences until there's nothing left. */
11194 while (line_ptr < line_end)
11195 {
11196 /* state machine registers */
11197 CORE_ADDR address = 0;
11198 unsigned int file = 1;
11199 unsigned int line = 1;
11200 unsigned int column = 0;
debd256d 11201 int is_stmt = lh->default_is_stmt;
c906108c
SS
11202 int basic_block = 0;
11203 int end_sequence = 0;
fbf65064 11204 CORE_ADDR addr;
2dc7f7b3 11205 unsigned char op_index = 0;
c906108c 11206
aaa75496 11207 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11208 {
aaa75496 11209 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11210 /* lh->include_dirs and lh->file_names are 0-based, but the
11211 directory and file name numbers in the statement program
11212 are 1-based. */
11213 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11214 char *dir = NULL;
a738430d 11215
debd256d
JB
11216 if (fe->dir_index)
11217 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11218
11219 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11220 }
11221
a738430d 11222 /* Decode the table. */
c5aa993b 11223 while (!end_sequence)
c906108c
SS
11224 {
11225 op_code = read_1_byte (abfd, line_ptr);
11226 line_ptr += 1;
59205f5a
JB
11227 if (line_ptr > line_end)
11228 {
11229 dwarf2_debug_line_missing_end_sequence_complaint ();
11230 break;
11231 }
9aa1fe7e 11232
debd256d 11233 if (op_code >= lh->opcode_base)
6e70227d 11234 {
a738430d 11235 /* Special operand. */
debd256d 11236 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11237 address += (((op_index + (adj_opcode / lh->line_range))
11238 / lh->maximum_ops_per_instruction)
11239 * lh->minimum_instruction_length);
11240 op_index = ((op_index + (adj_opcode / lh->line_range))
11241 % lh->maximum_ops_per_instruction);
debd256d 11242 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11243 if (lh->num_file_names < file || file == 0)
25e43795 11244 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11245 /* For now we ignore lines not starting on an
11246 instruction boundary. */
11247 else if (op_index == 0)
25e43795
DJ
11248 {
11249 lh->file_names[file - 1].included_p = 1;
ca5f395d 11250 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11251 {
11252 if (last_subfile != current_subfile)
11253 {
11254 addr = gdbarch_addr_bits_remove (gdbarch, address);
11255 if (last_subfile)
c91513d8 11256 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11257 last_subfile = current_subfile;
11258 }
25e43795 11259 /* Append row to matrix using current values. */
7019d805 11260 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11261 (*p_record_line) (current_subfile, line, addr);
366da635 11262 }
25e43795 11263 }
ca5f395d 11264 basic_block = 0;
9aa1fe7e
GK
11265 }
11266 else switch (op_code)
c906108c
SS
11267 {
11268 case DW_LNS_extended_op:
3e43a32a
MS
11269 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11270 &bytes_read);
473b7be6 11271 line_ptr += bytes_read;
a8c50c1f 11272 extended_end = line_ptr + extended_len;
c906108c
SS
11273 extended_op = read_1_byte (abfd, line_ptr);
11274 line_ptr += 1;
11275 switch (extended_op)
11276 {
11277 case DW_LNE_end_sequence:
c91513d8 11278 p_record_line = record_line;
c906108c 11279 end_sequence = 1;
c906108c
SS
11280 break;
11281 case DW_LNE_set_address:
e7c27a73 11282 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11283
11284 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11285 {
11286 /* This line table is for a function which has been
11287 GCd by the linker. Ignore it. PR gdb/12528 */
11288
11289 long line_offset
11290 = line_ptr - dwarf2_per_objfile->line.buffer;
11291
11292 complaint (&symfile_complaints,
11293 _(".debug_line address at offset 0x%lx is 0 "
11294 "[in module %s]"),
11295 line_offset, cu->objfile->name);
11296 p_record_line = noop_record_line;
11297 }
11298
2dc7f7b3 11299 op_index = 0;
107d2387
AC
11300 line_ptr += bytes_read;
11301 address += baseaddr;
c906108c
SS
11302 break;
11303 case DW_LNE_define_file:
debd256d
JB
11304 {
11305 char *cur_file;
11306 unsigned int dir_index, mod_time, length;
6e70227d 11307
3e43a32a
MS
11308 cur_file = read_direct_string (abfd, line_ptr,
11309 &bytes_read);
debd256d
JB
11310 line_ptr += bytes_read;
11311 dir_index =
11312 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11313 line_ptr += bytes_read;
11314 mod_time =
11315 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11316 line_ptr += bytes_read;
11317 length =
11318 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11319 line_ptr += bytes_read;
11320 add_file_name (lh, cur_file, dir_index, mod_time, length);
11321 }
c906108c 11322 break;
d0c6ba3d
CC
11323 case DW_LNE_set_discriminator:
11324 /* The discriminator is not interesting to the debugger;
11325 just ignore it. */
11326 line_ptr = extended_end;
11327 break;
c906108c 11328 default:
4d3c2250 11329 complaint (&symfile_complaints,
e2e0b3e5 11330 _("mangled .debug_line section"));
debd256d 11331 return;
c906108c 11332 }
a8c50c1f
DJ
11333 /* Make sure that we parsed the extended op correctly. If e.g.
11334 we expected a different address size than the producer used,
11335 we may have read the wrong number of bytes. */
11336 if (line_ptr != extended_end)
11337 {
11338 complaint (&symfile_complaints,
11339 _("mangled .debug_line section"));
11340 return;
11341 }
c906108c
SS
11342 break;
11343 case DW_LNS_copy:
59205f5a 11344 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11345 dwarf2_debug_line_missing_file_complaint ();
11346 else
366da635 11347 {
25e43795 11348 lh->file_names[file - 1].included_p = 1;
ca5f395d 11349 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11350 {
11351 if (last_subfile != current_subfile)
11352 {
11353 addr = gdbarch_addr_bits_remove (gdbarch, address);
11354 if (last_subfile)
c91513d8 11355 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11356 last_subfile = current_subfile;
11357 }
7019d805 11358 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11359 (*p_record_line) (current_subfile, line, addr);
fbf65064 11360 }
366da635 11361 }
c906108c
SS
11362 basic_block = 0;
11363 break;
11364 case DW_LNS_advance_pc:
2dc7f7b3
TT
11365 {
11366 CORE_ADDR adjust
11367 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11368
11369 address += (((op_index + adjust)
11370 / lh->maximum_ops_per_instruction)
11371 * lh->minimum_instruction_length);
11372 op_index = ((op_index + adjust)
11373 % lh->maximum_ops_per_instruction);
11374 line_ptr += bytes_read;
11375 }
c906108c
SS
11376 break;
11377 case DW_LNS_advance_line:
11378 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11379 line_ptr += bytes_read;
11380 break;
11381 case DW_LNS_set_file:
debd256d 11382 {
a738430d
MK
11383 /* The arrays lh->include_dirs and lh->file_names are
11384 0-based, but the directory and file name numbers in
11385 the statement program are 1-based. */
debd256d 11386 struct file_entry *fe;
4f1520fb 11387 char *dir = NULL;
a738430d 11388
debd256d
JB
11389 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11390 line_ptr += bytes_read;
59205f5a 11391 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11392 dwarf2_debug_line_missing_file_complaint ();
11393 else
11394 {
11395 fe = &lh->file_names[file - 1];
11396 if (fe->dir_index)
11397 dir = lh->include_dirs[fe->dir_index - 1];
11398 if (!decode_for_pst_p)
11399 {
11400 last_subfile = current_subfile;
11401 dwarf2_start_subfile (fe->name, dir, comp_dir);
11402 }
11403 }
debd256d 11404 }
c906108c
SS
11405 break;
11406 case DW_LNS_set_column:
11407 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11408 line_ptr += bytes_read;
11409 break;
11410 case DW_LNS_negate_stmt:
11411 is_stmt = (!is_stmt);
11412 break;
11413 case DW_LNS_set_basic_block:
11414 basic_block = 1;
11415 break;
c2c6d25f
JM
11416 /* Add to the address register of the state machine the
11417 address increment value corresponding to special opcode
a738430d
MK
11418 255. I.e., this value is scaled by the minimum
11419 instruction length since special opcode 255 would have
b021a221 11420 scaled the increment. */
c906108c 11421 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11422 {
11423 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11424
11425 address += (((op_index + adjust)
11426 / lh->maximum_ops_per_instruction)
11427 * lh->minimum_instruction_length);
11428 op_index = ((op_index + adjust)
11429 % lh->maximum_ops_per_instruction);
11430 }
c906108c
SS
11431 break;
11432 case DW_LNS_fixed_advance_pc:
11433 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11434 op_index = 0;
c906108c
SS
11435 line_ptr += 2;
11436 break;
9aa1fe7e 11437 default:
a738430d
MK
11438 {
11439 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11440 int i;
a738430d 11441
debd256d 11442 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11443 {
11444 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11445 line_ptr += bytes_read;
11446 }
11447 }
c906108c
SS
11448 }
11449 }
59205f5a
JB
11450 if (lh->num_file_names < file || file == 0)
11451 dwarf2_debug_line_missing_file_complaint ();
11452 else
11453 {
11454 lh->file_names[file - 1].included_p = 1;
11455 if (!decode_for_pst_p)
fbf65064
UW
11456 {
11457 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11458 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11459 }
59205f5a 11460 }
c906108c 11461 }
aaa75496
JB
11462
11463 if (decode_for_pst_p)
11464 {
11465 int file_index;
11466
11467 /* Now that we're done scanning the Line Header Program, we can
11468 create the psymtab of each included file. */
11469 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11470 if (lh->file_names[file_index].included_p == 1)
11471 {
c6da4cef
DE
11472 char *include_name =
11473 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11474 if (include_name != NULL)
aaa75496
JB
11475 dwarf2_create_include_psymtab (include_name, pst, objfile);
11476 }
11477 }
cb1df416
DJ
11478 else
11479 {
11480 /* Make sure a symtab is created for every file, even files
11481 which contain only variables (i.e. no code with associated
11482 line numbers). */
11483
11484 int i;
11485 struct file_entry *fe;
11486
11487 for (i = 0; i < lh->num_file_names; i++)
11488 {
11489 char *dir = NULL;
9a619af0 11490
cb1df416
DJ
11491 fe = &lh->file_names[i];
11492 if (fe->dir_index)
11493 dir = lh->include_dirs[fe->dir_index - 1];
11494 dwarf2_start_subfile (fe->name, dir, comp_dir);
11495
11496 /* Skip the main file; we don't need it, and it must be
11497 allocated last, so that it will show up before the
11498 non-primary symtabs in the objfile's symtab list. */
11499 if (current_subfile == first_subfile)
11500 continue;
11501
11502 if (current_subfile->symtab == NULL)
11503 current_subfile->symtab = allocate_symtab (current_subfile->name,
11504 cu->objfile);
11505 fe->symtab = current_subfile->symtab;
11506 }
11507 }
c906108c
SS
11508}
11509
11510/* Start a subfile for DWARF. FILENAME is the name of the file and
11511 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11512 or NULL if not known. COMP_DIR is the compilation directory for the
11513 linetable's compilation unit or NULL if not known.
c906108c
SS
11514 This routine tries to keep line numbers from identical absolute and
11515 relative file names in a common subfile.
11516
11517 Using the `list' example from the GDB testsuite, which resides in
11518 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11519 of /srcdir/list0.c yields the following debugging information for list0.c:
11520
c5aa993b
JM
11521 DW_AT_name: /srcdir/list0.c
11522 DW_AT_comp_dir: /compdir
357e46e7 11523 files.files[0].name: list0.h
c5aa993b 11524 files.files[0].dir: /srcdir
357e46e7 11525 files.files[1].name: list0.c
c5aa993b 11526 files.files[1].dir: /srcdir
c906108c
SS
11527
11528 The line number information for list0.c has to end up in a single
4f1520fb
FR
11529 subfile, so that `break /srcdir/list0.c:1' works as expected.
11530 start_subfile will ensure that this happens provided that we pass the
11531 concatenation of files.files[1].dir and files.files[1].name as the
11532 subfile's name. */
c906108c
SS
11533
11534static void
3e43a32a
MS
11535dwarf2_start_subfile (char *filename, const char *dirname,
11536 const char *comp_dir)
c906108c 11537{
4f1520fb
FR
11538 char *fullname;
11539
11540 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11541 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11542 second argument to start_subfile. To be consistent, we do the
11543 same here. In order not to lose the line information directory,
11544 we concatenate it to the filename when it makes sense.
11545 Note that the Dwarf3 standard says (speaking of filenames in line
11546 information): ``The directory index is ignored for file names
11547 that represent full path names''. Thus ignoring dirname in the
11548 `else' branch below isn't an issue. */
c906108c 11549
d5166ae1 11550 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11551 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11552 else
11553 fullname = filename;
c906108c 11554
4f1520fb
FR
11555 start_subfile (fullname, comp_dir);
11556
11557 if (fullname != filename)
11558 xfree (fullname);
c906108c
SS
11559}
11560
4c2df51b
DJ
11561static void
11562var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11563 struct dwarf2_cu *cu)
4c2df51b 11564{
e7c27a73
DJ
11565 struct objfile *objfile = cu->objfile;
11566 struct comp_unit_head *cu_header = &cu->header;
11567
4c2df51b
DJ
11568 /* NOTE drow/2003-01-30: There used to be a comment and some special
11569 code here to turn a symbol with DW_AT_external and a
11570 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11571 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11572 with some versions of binutils) where shared libraries could have
11573 relocations against symbols in their debug information - the
11574 minimal symbol would have the right address, but the debug info
11575 would not. It's no longer necessary, because we will explicitly
11576 apply relocations when we read in the debug information now. */
11577
11578 /* A DW_AT_location attribute with no contents indicates that a
11579 variable has been optimized away. */
11580 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11581 {
11582 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11583 return;
11584 }
11585
11586 /* Handle one degenerate form of location expression specially, to
11587 preserve GDB's previous behavior when section offsets are
11588 specified. If this is just a DW_OP_addr then mark this symbol
11589 as LOC_STATIC. */
11590
11591 if (attr_form_is_block (attr)
11592 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11593 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11594 {
891d2f0b 11595 unsigned int dummy;
4c2df51b
DJ
11596
11597 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11598 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11599 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11600 fixup_symbol_section (sym, objfile);
11601 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11602 SYMBOL_SECTION (sym));
4c2df51b
DJ
11603 return;
11604 }
11605
11606 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11607 expression evaluator, and use LOC_COMPUTED only when necessary
11608 (i.e. when the value of a register or memory location is
11609 referenced, or a thread-local block, etc.). Then again, it might
11610 not be worthwhile. I'm assuming that it isn't unless performance
11611 or memory numbers show me otherwise. */
11612
e7c27a73 11613 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11614 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11615
11616 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11617 cu->has_loclist = 1;
4c2df51b
DJ
11618}
11619
c906108c
SS
11620/* Given a pointer to a DWARF information entry, figure out if we need
11621 to make a symbol table entry for it, and if so, create a new entry
11622 and return a pointer to it.
11623 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11624 used the passed type.
11625 If SPACE is not NULL, use it to hold the new symbol. If it is
11626 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11627
11628static struct symbol *
34eaf542
TT
11629new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11630 struct symbol *space)
c906108c 11631{
e7c27a73 11632 struct objfile *objfile = cu->objfile;
c906108c
SS
11633 struct symbol *sym = NULL;
11634 char *name;
11635 struct attribute *attr = NULL;
11636 struct attribute *attr2 = NULL;
e142c38c 11637 CORE_ADDR baseaddr;
e37fd15a
SW
11638 struct pending **list_to_add = NULL;
11639
edb3359d 11640 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11641
11642 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11643
94af9270 11644 name = dwarf2_name (die, cu);
c906108c
SS
11645 if (name)
11646 {
94af9270 11647 const char *linkagename;
34eaf542 11648 int suppress_add = 0;
94af9270 11649
34eaf542
TT
11650 if (space)
11651 sym = space;
11652 else
11653 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11654 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11655
11656 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11657 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11658 linkagename = dwarf2_physname (name, die, cu);
11659 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11660
f55ee35c
JK
11661 /* Fortran does not have mangling standard and the mangling does differ
11662 between gfortran, iFort etc. */
11663 if (cu->language == language_fortran
b250c185 11664 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11665 symbol_set_demangled_name (&(sym->ginfo),
11666 (char *) dwarf2_full_name (name, die, cu),
11667 NULL);
f55ee35c 11668
c906108c 11669 /* Default assumptions.
c5aa993b 11670 Use the passed type or decode it from the die. */
176620f1 11671 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11672 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11673 if (type != NULL)
11674 SYMBOL_TYPE (sym) = type;
11675 else
e7c27a73 11676 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11677 attr = dwarf2_attr (die,
11678 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11679 cu);
c906108c
SS
11680 if (attr)
11681 {
11682 SYMBOL_LINE (sym) = DW_UNSND (attr);
11683 }
cb1df416 11684
edb3359d
DJ
11685 attr = dwarf2_attr (die,
11686 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11687 cu);
cb1df416
DJ
11688 if (attr)
11689 {
11690 int file_index = DW_UNSND (attr);
9a619af0 11691
cb1df416
DJ
11692 if (cu->line_header == NULL
11693 || file_index > cu->line_header->num_file_names)
11694 complaint (&symfile_complaints,
11695 _("file index out of range"));
1c3d648d 11696 else if (file_index > 0)
cb1df416
DJ
11697 {
11698 struct file_entry *fe;
9a619af0 11699
cb1df416
DJ
11700 fe = &cu->line_header->file_names[file_index - 1];
11701 SYMBOL_SYMTAB (sym) = fe->symtab;
11702 }
11703 }
11704
c906108c
SS
11705 switch (die->tag)
11706 {
11707 case DW_TAG_label:
e142c38c 11708 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11709 if (attr)
11710 {
11711 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11712 }
0f5238ed
TT
11713 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11714 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11715 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11716 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11717 break;
11718 case DW_TAG_subprogram:
11719 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11720 finish_block. */
11721 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11722 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11723 if ((attr2 && (DW_UNSND (attr2) != 0))
11724 || cu->language == language_ada)
c906108c 11725 {
2cfa0c8d
JB
11726 /* Subprograms marked external are stored as a global symbol.
11727 Ada subprograms, whether marked external or not, are always
11728 stored as a global symbol, because we want to be able to
11729 access them globally. For instance, we want to be able
11730 to break on a nested subprogram without having to
11731 specify the context. */
e37fd15a 11732 list_to_add = &global_symbols;
c906108c
SS
11733 }
11734 else
11735 {
e37fd15a 11736 list_to_add = cu->list_in_scope;
c906108c
SS
11737 }
11738 break;
edb3359d
DJ
11739 case DW_TAG_inlined_subroutine:
11740 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11741 finish_block. */
11742 SYMBOL_CLASS (sym) = LOC_BLOCK;
11743 SYMBOL_INLINED (sym) = 1;
11744 /* Do not add the symbol to any lists. It will be found via
11745 BLOCK_FUNCTION from the blockvector. */
11746 break;
34eaf542
TT
11747 case DW_TAG_template_value_param:
11748 suppress_add = 1;
11749 /* Fall through. */
72929c62 11750 case DW_TAG_constant:
c906108c 11751 case DW_TAG_variable:
254e6b9e 11752 case DW_TAG_member:
0963b4bd
MS
11753 /* Compilation with minimal debug info may result in
11754 variables with missing type entries. Change the
11755 misleading `void' type to something sensible. */
c906108c 11756 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11757 SYMBOL_TYPE (sym)
46bf5051 11758 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11759
e142c38c 11760 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11761 /* In the case of DW_TAG_member, we should only be called for
11762 static const members. */
11763 if (die->tag == DW_TAG_member)
11764 {
3863f96c
DE
11765 /* dwarf2_add_field uses die_is_declaration,
11766 so we do the same. */
254e6b9e
DE
11767 gdb_assert (die_is_declaration (die, cu));
11768 gdb_assert (attr);
11769 }
c906108c
SS
11770 if (attr)
11771 {
e7c27a73 11772 dwarf2_const_value (attr, sym, cu);
e142c38c 11773 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11774 if (!suppress_add)
34eaf542
TT
11775 {
11776 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11777 list_to_add = &global_symbols;
34eaf542 11778 else
e37fd15a 11779 list_to_add = cu->list_in_scope;
34eaf542 11780 }
c906108c
SS
11781 break;
11782 }
e142c38c 11783 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11784 if (attr)
11785 {
e7c27a73 11786 var_decode_location (attr, sym, cu);
e142c38c 11787 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11788 if (SYMBOL_CLASS (sym) == LOC_STATIC
11789 && SYMBOL_VALUE_ADDRESS (sym) == 0
11790 && !dwarf2_per_objfile->has_section_at_zero)
11791 {
11792 /* When a static variable is eliminated by the linker,
11793 the corresponding debug information is not stripped
11794 out, but the variable address is set to null;
11795 do not add such variables into symbol table. */
11796 }
11797 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11798 {
f55ee35c
JK
11799 /* Workaround gfortran PR debug/40040 - it uses
11800 DW_AT_location for variables in -fPIC libraries which may
11801 get overriden by other libraries/executable and get
11802 a different address. Resolve it by the minimal symbol
11803 which may come from inferior's executable using copy
11804 relocation. Make this workaround only for gfortran as for
11805 other compilers GDB cannot guess the minimal symbol
11806 Fortran mangling kind. */
11807 if (cu->language == language_fortran && die->parent
11808 && die->parent->tag == DW_TAG_module
11809 && cu->producer
11810 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11811 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11812
1c809c68
TT
11813 /* A variable with DW_AT_external is never static,
11814 but it may be block-scoped. */
11815 list_to_add = (cu->list_in_scope == &file_symbols
11816 ? &global_symbols : cu->list_in_scope);
1c809c68 11817 }
c906108c 11818 else
e37fd15a 11819 list_to_add = cu->list_in_scope;
c906108c
SS
11820 }
11821 else
11822 {
11823 /* We do not know the address of this symbol.
c5aa993b
JM
11824 If it is an external symbol and we have type information
11825 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11826 The address of the variable will then be determined from
11827 the minimal symbol table whenever the variable is
11828 referenced. */
e142c38c 11829 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11830 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11831 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11832 {
0fe7935b
DJ
11833 /* A variable with DW_AT_external is never static, but it
11834 may be block-scoped. */
11835 list_to_add = (cu->list_in_scope == &file_symbols
11836 ? &global_symbols : cu->list_in_scope);
11837
c906108c 11838 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11839 }
442ddf59
JK
11840 else if (!die_is_declaration (die, cu))
11841 {
11842 /* Use the default LOC_OPTIMIZED_OUT class. */
11843 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11844 if (!suppress_add)
11845 list_to_add = cu->list_in_scope;
442ddf59 11846 }
c906108c
SS
11847 }
11848 break;
11849 case DW_TAG_formal_parameter:
edb3359d
DJ
11850 /* If we are inside a function, mark this as an argument. If
11851 not, we might be looking at an argument to an inlined function
11852 when we do not have enough information to show inlined frames;
11853 pretend it's a local variable in that case so that the user can
11854 still see it. */
11855 if (context_stack_depth > 0
11856 && context_stack[context_stack_depth - 1].name != NULL)
11857 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11858 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11859 if (attr)
11860 {
e7c27a73 11861 var_decode_location (attr, sym, cu);
c906108c 11862 }
e142c38c 11863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11864 if (attr)
11865 {
e7c27a73 11866 dwarf2_const_value (attr, sym, cu);
c906108c 11867 }
f346a30d 11868
e37fd15a 11869 list_to_add = cu->list_in_scope;
c906108c
SS
11870 break;
11871 case DW_TAG_unspecified_parameters:
11872 /* From varargs functions; gdb doesn't seem to have any
11873 interest in this information, so just ignore it for now.
11874 (FIXME?) */
11875 break;
34eaf542
TT
11876 case DW_TAG_template_type_param:
11877 suppress_add = 1;
11878 /* Fall through. */
c906108c 11879 case DW_TAG_class_type:
680b30c7 11880 case DW_TAG_interface_type:
c906108c
SS
11881 case DW_TAG_structure_type:
11882 case DW_TAG_union_type:
72019c9c 11883 case DW_TAG_set_type:
c906108c
SS
11884 case DW_TAG_enumeration_type:
11885 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11886 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11887
63d06c5c 11888 {
987504bb 11889 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11890 really ever be static objects: otherwise, if you try
11891 to, say, break of a class's method and you're in a file
11892 which doesn't mention that class, it won't work unless
11893 the check for all static symbols in lookup_symbol_aux
11894 saves you. See the OtherFileClass tests in
11895 gdb.c++/namespace.exp. */
11896
e37fd15a 11897 if (!suppress_add)
34eaf542 11898 {
34eaf542
TT
11899 list_to_add = (cu->list_in_scope == &file_symbols
11900 && (cu->language == language_cplus
11901 || cu->language == language_java)
11902 ? &global_symbols : cu->list_in_scope);
63d06c5c 11903
64382290
TT
11904 /* The semantics of C++ state that "struct foo {
11905 ... }" also defines a typedef for "foo". A Java
11906 class declaration also defines a typedef for the
11907 class. */
11908 if (cu->language == language_cplus
11909 || cu->language == language_java
11910 || cu->language == language_ada)
11911 {
11912 /* The symbol's name is already allocated along
11913 with this objfile, so we don't need to
11914 duplicate it for the type. */
11915 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11916 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11917 }
63d06c5c
DC
11918 }
11919 }
c906108c
SS
11920 break;
11921 case DW_TAG_typedef:
63d06c5c
DC
11922 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11923 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11924 list_to_add = cu->list_in_scope;
63d06c5c 11925 break;
c906108c 11926 case DW_TAG_base_type:
a02abb62 11927 case DW_TAG_subrange_type:
c906108c 11928 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11929 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11930 list_to_add = cu->list_in_scope;
c906108c
SS
11931 break;
11932 case DW_TAG_enumerator:
e142c38c 11933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11934 if (attr)
11935 {
e7c27a73 11936 dwarf2_const_value (attr, sym, cu);
c906108c 11937 }
63d06c5c
DC
11938 {
11939 /* NOTE: carlton/2003-11-10: See comment above in the
11940 DW_TAG_class_type, etc. block. */
11941
e142c38c 11942 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11943 && (cu->language == language_cplus
11944 || cu->language == language_java)
e142c38c 11945 ? &global_symbols : cu->list_in_scope);
63d06c5c 11946 }
c906108c 11947 break;
5c4e30ca
DC
11948 case DW_TAG_namespace:
11949 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11950 list_to_add = &global_symbols;
5c4e30ca 11951 break;
c906108c
SS
11952 default:
11953 /* Not a tag we recognize. Hopefully we aren't processing
11954 trash data, but since we must specifically ignore things
11955 we don't recognize, there is nothing else we should do at
0963b4bd 11956 this point. */
e2e0b3e5 11957 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11958 dwarf_tag_name (die->tag));
c906108c
SS
11959 break;
11960 }
df8a16a1 11961
e37fd15a
SW
11962 if (suppress_add)
11963 {
11964 sym->hash_next = objfile->template_symbols;
11965 objfile->template_symbols = sym;
11966 list_to_add = NULL;
11967 }
11968
11969 if (list_to_add != NULL)
11970 add_symbol_to_list (sym, list_to_add);
11971
df8a16a1
DJ
11972 /* For the benefit of old versions of GCC, check for anonymous
11973 namespaces based on the demangled name. */
11974 if (!processing_has_namespace_info
94af9270 11975 && cu->language == language_cplus)
a10964d1 11976 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11977 }
11978 return (sym);
11979}
11980
34eaf542
TT
11981/* A wrapper for new_symbol_full that always allocates a new symbol. */
11982
11983static struct symbol *
11984new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11985{
11986 return new_symbol_full (die, type, cu, NULL);
11987}
11988
98bfdba5
PA
11989/* Given an attr with a DW_FORM_dataN value in host byte order,
11990 zero-extend it as appropriate for the symbol's type. The DWARF
11991 standard (v4) is not entirely clear about the meaning of using
11992 DW_FORM_dataN for a constant with a signed type, where the type is
11993 wider than the data. The conclusion of a discussion on the DWARF
11994 list was that this is unspecified. We choose to always zero-extend
11995 because that is the interpretation long in use by GCC. */
c906108c 11996
98bfdba5
PA
11997static gdb_byte *
11998dwarf2_const_value_data (struct attribute *attr, struct type *type,
11999 const char *name, struct obstack *obstack,
12000 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12001{
e7c27a73 12002 struct objfile *objfile = cu->objfile;
e17a4113
UW
12003 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12004 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12005 LONGEST l = DW_UNSND (attr);
12006
12007 if (bits < sizeof (*value) * 8)
12008 {
12009 l &= ((LONGEST) 1 << bits) - 1;
12010 *value = l;
12011 }
12012 else if (bits == sizeof (*value) * 8)
12013 *value = l;
12014 else
12015 {
12016 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12017 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12018 return bytes;
12019 }
12020
12021 return NULL;
12022}
12023
12024/* Read a constant value from an attribute. Either set *VALUE, or if
12025 the value does not fit in *VALUE, set *BYTES - either already
12026 allocated on the objfile obstack, or newly allocated on OBSTACK,
12027 or, set *BATON, if we translated the constant to a location
12028 expression. */
12029
12030static void
12031dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12032 const char *name, struct obstack *obstack,
12033 struct dwarf2_cu *cu,
12034 long *value, gdb_byte **bytes,
12035 struct dwarf2_locexpr_baton **baton)
12036{
12037 struct objfile *objfile = cu->objfile;
12038 struct comp_unit_head *cu_header = &cu->header;
c906108c 12039 struct dwarf_block *blk;
98bfdba5
PA
12040 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12041 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12042
12043 *value = 0;
12044 *bytes = NULL;
12045 *baton = NULL;
c906108c
SS
12046
12047 switch (attr->form)
12048 {
12049 case DW_FORM_addr:
ac56253d 12050 {
ac56253d
TT
12051 gdb_byte *data;
12052
98bfdba5
PA
12053 if (TYPE_LENGTH (type) != cu_header->addr_size)
12054 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12055 cu_header->addr_size,
98bfdba5 12056 TYPE_LENGTH (type));
ac56253d
TT
12057 /* Symbols of this form are reasonably rare, so we just
12058 piggyback on the existing location code rather than writing
12059 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12060 *baton = obstack_alloc (&objfile->objfile_obstack,
12061 sizeof (struct dwarf2_locexpr_baton));
12062 (*baton)->per_cu = cu->per_cu;
12063 gdb_assert ((*baton)->per_cu);
ac56253d 12064
98bfdba5
PA
12065 (*baton)->size = 2 + cu_header->addr_size;
12066 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12067 (*baton)->data = data;
ac56253d
TT
12068
12069 data[0] = DW_OP_addr;
12070 store_unsigned_integer (&data[1], cu_header->addr_size,
12071 byte_order, DW_ADDR (attr));
12072 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12073 }
c906108c 12074 break;
4ac36638 12075 case DW_FORM_string:
93b5768b 12076 case DW_FORM_strp:
98bfdba5
PA
12077 /* DW_STRING is already allocated on the objfile obstack, point
12078 directly to it. */
12079 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12080 break;
c906108c
SS
12081 case DW_FORM_block1:
12082 case DW_FORM_block2:
12083 case DW_FORM_block4:
12084 case DW_FORM_block:
2dc7f7b3 12085 case DW_FORM_exprloc:
c906108c 12086 blk = DW_BLOCK (attr);
98bfdba5
PA
12087 if (TYPE_LENGTH (type) != blk->size)
12088 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12089 TYPE_LENGTH (type));
12090 *bytes = blk->data;
c906108c 12091 break;
2df3850c
JM
12092
12093 /* The DW_AT_const_value attributes are supposed to carry the
12094 symbol's value "represented as it would be on the target
12095 architecture." By the time we get here, it's already been
12096 converted to host endianness, so we just need to sign- or
12097 zero-extend it as appropriate. */
12098 case DW_FORM_data1:
3e43a32a
MS
12099 *bytes = dwarf2_const_value_data (attr, type, name,
12100 obstack, cu, value, 8);
2df3850c 12101 break;
c906108c 12102 case DW_FORM_data2:
3e43a32a
MS
12103 *bytes = dwarf2_const_value_data (attr, type, name,
12104 obstack, cu, value, 16);
2df3850c 12105 break;
c906108c 12106 case DW_FORM_data4:
3e43a32a
MS
12107 *bytes = dwarf2_const_value_data (attr, type, name,
12108 obstack, cu, value, 32);
2df3850c 12109 break;
c906108c 12110 case DW_FORM_data8:
3e43a32a
MS
12111 *bytes = dwarf2_const_value_data (attr, type, name,
12112 obstack, cu, value, 64);
2df3850c
JM
12113 break;
12114
c906108c 12115 case DW_FORM_sdata:
98bfdba5 12116 *value = DW_SND (attr);
2df3850c
JM
12117 break;
12118
c906108c 12119 case DW_FORM_udata:
98bfdba5 12120 *value = DW_UNSND (attr);
c906108c 12121 break;
2df3850c 12122
c906108c 12123 default:
4d3c2250 12124 complaint (&symfile_complaints,
e2e0b3e5 12125 _("unsupported const value attribute form: '%s'"),
4d3c2250 12126 dwarf_form_name (attr->form));
98bfdba5 12127 *value = 0;
c906108c
SS
12128 break;
12129 }
12130}
12131
2df3850c 12132
98bfdba5
PA
12133/* Copy constant value from an attribute to a symbol. */
12134
2df3850c 12135static void
98bfdba5
PA
12136dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12137 struct dwarf2_cu *cu)
2df3850c 12138{
98bfdba5
PA
12139 struct objfile *objfile = cu->objfile;
12140 struct comp_unit_head *cu_header = &cu->header;
12141 long value;
12142 gdb_byte *bytes;
12143 struct dwarf2_locexpr_baton *baton;
2df3850c 12144
98bfdba5
PA
12145 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12146 SYMBOL_PRINT_NAME (sym),
12147 &objfile->objfile_obstack, cu,
12148 &value, &bytes, &baton);
2df3850c 12149
98bfdba5
PA
12150 if (baton != NULL)
12151 {
12152 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12153 SYMBOL_LOCATION_BATON (sym) = baton;
12154 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12155 }
12156 else if (bytes != NULL)
12157 {
12158 SYMBOL_VALUE_BYTES (sym) = bytes;
12159 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12160 }
12161 else
12162 {
12163 SYMBOL_VALUE (sym) = value;
12164 SYMBOL_CLASS (sym) = LOC_CONST;
12165 }
2df3850c
JM
12166}
12167
c906108c
SS
12168/* Return the type of the die in question using its DW_AT_type attribute. */
12169
12170static struct type *
e7c27a73 12171die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12172{
c906108c 12173 struct attribute *type_attr;
c906108c 12174
e142c38c 12175 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12176 if (!type_attr)
12177 {
12178 /* A missing DW_AT_type represents a void type. */
46bf5051 12179 return objfile_type (cu->objfile)->builtin_void;
c906108c 12180 }
348e048f 12181
673bfd45 12182 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12183}
12184
b4ba55a1
JB
12185/* True iff CU's producer generates GNAT Ada auxiliary information
12186 that allows to find parallel types through that information instead
12187 of having to do expensive parallel lookups by type name. */
12188
12189static int
12190need_gnat_info (struct dwarf2_cu *cu)
12191{
12192 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12193 of GNAT produces this auxiliary information, without any indication
12194 that it is produced. Part of enhancing the FSF version of GNAT
12195 to produce that information will be to put in place an indicator
12196 that we can use in order to determine whether the descriptive type
12197 info is available or not. One suggestion that has been made is
12198 to use a new attribute, attached to the CU die. For now, assume
12199 that the descriptive type info is not available. */
12200 return 0;
12201}
12202
b4ba55a1
JB
12203/* Return the auxiliary type of the die in question using its
12204 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12205 attribute is not present. */
12206
12207static struct type *
12208die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12209{
b4ba55a1 12210 struct attribute *type_attr;
b4ba55a1
JB
12211
12212 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12213 if (!type_attr)
12214 return NULL;
12215
673bfd45 12216 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12217}
12218
12219/* If DIE has a descriptive_type attribute, then set the TYPE's
12220 descriptive type accordingly. */
12221
12222static void
12223set_descriptive_type (struct type *type, struct die_info *die,
12224 struct dwarf2_cu *cu)
12225{
12226 struct type *descriptive_type = die_descriptive_type (die, cu);
12227
12228 if (descriptive_type)
12229 {
12230 ALLOCATE_GNAT_AUX_TYPE (type);
12231 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12232 }
12233}
12234
c906108c
SS
12235/* Return the containing type of the die in question using its
12236 DW_AT_containing_type attribute. */
12237
12238static struct type *
e7c27a73 12239die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12240{
c906108c 12241 struct attribute *type_attr;
c906108c 12242
e142c38c 12243 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12244 if (!type_attr)
12245 error (_("Dwarf Error: Problem turning containing type into gdb type "
12246 "[in module %s]"), cu->objfile->name);
12247
673bfd45 12248 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12249}
12250
673bfd45
DE
12251/* Look up the type of DIE in CU using its type attribute ATTR.
12252 If there is no type substitute an error marker. */
12253
c906108c 12254static struct type *
673bfd45
DE
12255lookup_die_type (struct die_info *die, struct attribute *attr,
12256 struct dwarf2_cu *cu)
c906108c 12257{
f792889a
DJ
12258 struct type *this_type;
12259
673bfd45
DE
12260 /* First see if we have it cached. */
12261
12262 if (is_ref_attr (attr))
12263 {
12264 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12265
12266 this_type = get_die_type_at_offset (offset, cu->per_cu);
12267 }
55f1336d 12268 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12269 {
12270 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12271 struct dwarf2_cu *sig_cu;
12272 unsigned int offset;
12273
12274 /* sig_type will be NULL if the signatured type is missing from
12275 the debug info. */
12276 if (sig_type == NULL)
12277 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12278 "at 0x%x [in module %s]"),
12279 die->offset, cu->objfile->name);
12280
b0df02fd 12281 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12282 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12283 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12284 }
12285 else
12286 {
12287 dump_die_for_error (die);
12288 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12289 dwarf_attr_name (attr->name), cu->objfile->name);
12290 }
12291
12292 /* If not cached we need to read it in. */
12293
12294 if (this_type == NULL)
12295 {
12296 struct die_info *type_die;
12297 struct dwarf2_cu *type_cu = cu;
12298
12299 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12300 /* If the type is cached, we should have found it above. */
12301 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12302 this_type = read_type_die_1 (type_die, type_cu);
12303 }
12304
12305 /* If we still don't have a type use an error marker. */
12306
12307 if (this_type == NULL)
c906108c 12308 {
b00fdb78
TT
12309 char *message, *saved;
12310
12311 /* read_type_die already issued a complaint. */
12312 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12313 cu->objfile->name,
12314 cu->header.offset,
12315 die->offset);
12316 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12317 message, strlen (message));
12318 xfree (message);
12319
12320 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12321 }
673bfd45 12322
f792889a 12323 return this_type;
c906108c
SS
12324}
12325
673bfd45
DE
12326/* Return the type in DIE, CU.
12327 Returns NULL for invalid types.
12328
12329 This first does a lookup in the appropriate type_hash table,
12330 and only reads the die in if necessary.
12331
12332 NOTE: This can be called when reading in partial or full symbols. */
12333
f792889a 12334static struct type *
e7c27a73 12335read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12336{
f792889a
DJ
12337 struct type *this_type;
12338
12339 this_type = get_die_type (die, cu);
12340 if (this_type)
12341 return this_type;
12342
673bfd45
DE
12343 return read_type_die_1 (die, cu);
12344}
12345
12346/* Read the type in DIE, CU.
12347 Returns NULL for invalid types. */
12348
12349static struct type *
12350read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12351{
12352 struct type *this_type = NULL;
12353
c906108c
SS
12354 switch (die->tag)
12355 {
12356 case DW_TAG_class_type:
680b30c7 12357 case DW_TAG_interface_type:
c906108c
SS
12358 case DW_TAG_structure_type:
12359 case DW_TAG_union_type:
f792889a 12360 this_type = read_structure_type (die, cu);
c906108c
SS
12361 break;
12362 case DW_TAG_enumeration_type:
f792889a 12363 this_type = read_enumeration_type (die, cu);
c906108c
SS
12364 break;
12365 case DW_TAG_subprogram:
12366 case DW_TAG_subroutine_type:
edb3359d 12367 case DW_TAG_inlined_subroutine:
f792889a 12368 this_type = read_subroutine_type (die, cu);
c906108c
SS
12369 break;
12370 case DW_TAG_array_type:
f792889a 12371 this_type = read_array_type (die, cu);
c906108c 12372 break;
72019c9c 12373 case DW_TAG_set_type:
f792889a 12374 this_type = read_set_type (die, cu);
72019c9c 12375 break;
c906108c 12376 case DW_TAG_pointer_type:
f792889a 12377 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12378 break;
12379 case DW_TAG_ptr_to_member_type:
f792889a 12380 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12381 break;
12382 case DW_TAG_reference_type:
f792889a 12383 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12384 break;
12385 case DW_TAG_const_type:
f792889a 12386 this_type = read_tag_const_type (die, cu);
c906108c
SS
12387 break;
12388 case DW_TAG_volatile_type:
f792889a 12389 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12390 break;
12391 case DW_TAG_string_type:
f792889a 12392 this_type = read_tag_string_type (die, cu);
c906108c
SS
12393 break;
12394 case DW_TAG_typedef:
f792889a 12395 this_type = read_typedef (die, cu);
c906108c 12396 break;
a02abb62 12397 case DW_TAG_subrange_type:
f792889a 12398 this_type = read_subrange_type (die, cu);
a02abb62 12399 break;
c906108c 12400 case DW_TAG_base_type:
f792889a 12401 this_type = read_base_type (die, cu);
c906108c 12402 break;
81a17f79 12403 case DW_TAG_unspecified_type:
f792889a 12404 this_type = read_unspecified_type (die, cu);
81a17f79 12405 break;
0114d602
DJ
12406 case DW_TAG_namespace:
12407 this_type = read_namespace_type (die, cu);
12408 break;
f55ee35c
JK
12409 case DW_TAG_module:
12410 this_type = read_module_type (die, cu);
12411 break;
c906108c 12412 default:
3e43a32a
MS
12413 complaint (&symfile_complaints,
12414 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12415 dwarf_tag_name (die->tag));
c906108c
SS
12416 break;
12417 }
63d06c5c 12418
f792889a 12419 return this_type;
63d06c5c
DC
12420}
12421
abc72ce4
DE
12422/* See if we can figure out if the class lives in a namespace. We do
12423 this by looking for a member function; its demangled name will
12424 contain namespace info, if there is any.
12425 Return the computed name or NULL.
12426 Space for the result is allocated on the objfile's obstack.
12427 This is the full-die version of guess_partial_die_structure_name.
12428 In this case we know DIE has no useful parent. */
12429
12430static char *
12431guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12432{
12433 struct die_info *spec_die;
12434 struct dwarf2_cu *spec_cu;
12435 struct die_info *child;
12436
12437 spec_cu = cu;
12438 spec_die = die_specification (die, &spec_cu);
12439 if (spec_die != NULL)
12440 {
12441 die = spec_die;
12442 cu = spec_cu;
12443 }
12444
12445 for (child = die->child;
12446 child != NULL;
12447 child = child->sibling)
12448 {
12449 if (child->tag == DW_TAG_subprogram)
12450 {
12451 struct attribute *attr;
12452
12453 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12454 if (attr == NULL)
12455 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12456 if (attr != NULL)
12457 {
12458 char *actual_name
12459 = language_class_name_from_physname (cu->language_defn,
12460 DW_STRING (attr));
12461 char *name = NULL;
12462
12463 if (actual_name != NULL)
12464 {
12465 char *die_name = dwarf2_name (die, cu);
12466
12467 if (die_name != NULL
12468 && strcmp (die_name, actual_name) != 0)
12469 {
12470 /* Strip off the class name from the full name.
12471 We want the prefix. */
12472 int die_name_len = strlen (die_name);
12473 int actual_name_len = strlen (actual_name);
12474
12475 /* Test for '::' as a sanity check. */
12476 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12477 && actual_name[actual_name_len
12478 - die_name_len - 1] == ':')
abc72ce4
DE
12479 name =
12480 obsavestring (actual_name,
12481 actual_name_len - die_name_len - 2,
12482 &cu->objfile->objfile_obstack);
12483 }
12484 }
12485 xfree (actual_name);
12486 return name;
12487 }
12488 }
12489 }
12490
12491 return NULL;
12492}
12493
96408a79
SA
12494/* GCC might emit a nameless typedef that has a linkage name. Determine the
12495 prefix part in such case. See
12496 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12497
12498static char *
12499anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12500{
12501 struct attribute *attr;
12502 char *base;
12503
12504 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12505 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12506 return NULL;
12507
12508 attr = dwarf2_attr (die, DW_AT_name, cu);
12509 if (attr != NULL && DW_STRING (attr) != NULL)
12510 return NULL;
12511
12512 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12513 if (attr == NULL)
12514 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12515 if (attr == NULL || DW_STRING (attr) == NULL)
12516 return NULL;
12517
12518 /* dwarf2_name had to be already called. */
12519 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12520
12521 /* Strip the base name, keep any leading namespaces/classes. */
12522 base = strrchr (DW_STRING (attr), ':');
12523 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12524 return "";
12525
12526 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12527 &cu->objfile->objfile_obstack);
12528}
12529
fdde2d81 12530/* Return the name of the namespace/class that DIE is defined within,
0114d602 12531 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12532
0114d602
DJ
12533 For example, if we're within the method foo() in the following
12534 code:
12535
12536 namespace N {
12537 class C {
12538 void foo () {
12539 }
12540 };
12541 }
12542
12543 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12544
12545static char *
e142c38c 12546determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12547{
0114d602
DJ
12548 struct die_info *parent, *spec_die;
12549 struct dwarf2_cu *spec_cu;
12550 struct type *parent_type;
96408a79 12551 char *retval;
63d06c5c 12552
f55ee35c
JK
12553 if (cu->language != language_cplus && cu->language != language_java
12554 && cu->language != language_fortran)
0114d602
DJ
12555 return "";
12556
96408a79
SA
12557 retval = anonymous_struct_prefix (die, cu);
12558 if (retval)
12559 return retval;
12560
0114d602
DJ
12561 /* We have to be careful in the presence of DW_AT_specification.
12562 For example, with GCC 3.4, given the code
12563
12564 namespace N {
12565 void foo() {
12566 // Definition of N::foo.
12567 }
12568 }
12569
12570 then we'll have a tree of DIEs like this:
12571
12572 1: DW_TAG_compile_unit
12573 2: DW_TAG_namespace // N
12574 3: DW_TAG_subprogram // declaration of N::foo
12575 4: DW_TAG_subprogram // definition of N::foo
12576 DW_AT_specification // refers to die #3
12577
12578 Thus, when processing die #4, we have to pretend that we're in
12579 the context of its DW_AT_specification, namely the contex of die
12580 #3. */
12581 spec_cu = cu;
12582 spec_die = die_specification (die, &spec_cu);
12583 if (spec_die == NULL)
12584 parent = die->parent;
12585 else
63d06c5c 12586 {
0114d602
DJ
12587 parent = spec_die->parent;
12588 cu = spec_cu;
63d06c5c 12589 }
0114d602
DJ
12590
12591 if (parent == NULL)
12592 return "";
98bfdba5
PA
12593 else if (parent->building_fullname)
12594 {
12595 const char *name;
12596 const char *parent_name;
12597
12598 /* It has been seen on RealView 2.2 built binaries,
12599 DW_TAG_template_type_param types actually _defined_ as
12600 children of the parent class:
12601
12602 enum E {};
12603 template class <class Enum> Class{};
12604 Class<enum E> class_e;
12605
12606 1: DW_TAG_class_type (Class)
12607 2: DW_TAG_enumeration_type (E)
12608 3: DW_TAG_enumerator (enum1:0)
12609 3: DW_TAG_enumerator (enum2:1)
12610 ...
12611 2: DW_TAG_template_type_param
12612 DW_AT_type DW_FORM_ref_udata (E)
12613
12614 Besides being broken debug info, it can put GDB into an
12615 infinite loop. Consider:
12616
12617 When we're building the full name for Class<E>, we'll start
12618 at Class, and go look over its template type parameters,
12619 finding E. We'll then try to build the full name of E, and
12620 reach here. We're now trying to build the full name of E,
12621 and look over the parent DIE for containing scope. In the
12622 broken case, if we followed the parent DIE of E, we'd again
12623 find Class, and once again go look at its template type
12624 arguments, etc., etc. Simply don't consider such parent die
12625 as source-level parent of this die (it can't be, the language
12626 doesn't allow it), and break the loop here. */
12627 name = dwarf2_name (die, cu);
12628 parent_name = dwarf2_name (parent, cu);
12629 complaint (&symfile_complaints,
12630 _("template param type '%s' defined within parent '%s'"),
12631 name ? name : "<unknown>",
12632 parent_name ? parent_name : "<unknown>");
12633 return "";
12634 }
63d06c5c 12635 else
0114d602
DJ
12636 switch (parent->tag)
12637 {
63d06c5c 12638 case DW_TAG_namespace:
0114d602 12639 parent_type = read_type_die (parent, cu);
acebe513
UW
12640 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12641 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12642 Work around this problem here. */
12643 if (cu->language == language_cplus
12644 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12645 return "";
0114d602
DJ
12646 /* We give a name to even anonymous namespaces. */
12647 return TYPE_TAG_NAME (parent_type);
63d06c5c 12648 case DW_TAG_class_type:
680b30c7 12649 case DW_TAG_interface_type:
63d06c5c 12650 case DW_TAG_structure_type:
0114d602 12651 case DW_TAG_union_type:
f55ee35c 12652 case DW_TAG_module:
0114d602
DJ
12653 parent_type = read_type_die (parent, cu);
12654 if (TYPE_TAG_NAME (parent_type) != NULL)
12655 return TYPE_TAG_NAME (parent_type);
12656 else
12657 /* An anonymous structure is only allowed non-static data
12658 members; no typedefs, no member functions, et cetera.
12659 So it does not need a prefix. */
12660 return "";
abc72ce4
DE
12661 case DW_TAG_compile_unit:
12662 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12663 if (cu->language == language_cplus
8b70b953 12664 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12665 && die->child != NULL
12666 && (die->tag == DW_TAG_class_type
12667 || die->tag == DW_TAG_structure_type
12668 || die->tag == DW_TAG_union_type))
12669 {
12670 char *name = guess_full_die_structure_name (die, cu);
12671 if (name != NULL)
12672 return name;
12673 }
12674 return "";
63d06c5c 12675 default:
8176b9b8 12676 return determine_prefix (parent, cu);
63d06c5c 12677 }
63d06c5c
DC
12678}
12679
3e43a32a
MS
12680/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12681 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12682 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12683 an obconcat, otherwise allocate storage for the result. The CU argument is
12684 used to determine the language and hence, the appropriate separator. */
987504bb 12685
f55ee35c 12686#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12687
12688static char *
f55ee35c
JK
12689typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12690 int physname, struct dwarf2_cu *cu)
63d06c5c 12691{
f55ee35c 12692 const char *lead = "";
5c315b68 12693 const char *sep;
63d06c5c 12694
3e43a32a
MS
12695 if (suffix == NULL || suffix[0] == '\0'
12696 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12697 sep = "";
12698 else if (cu->language == language_java)
12699 sep = ".";
f55ee35c
JK
12700 else if (cu->language == language_fortran && physname)
12701 {
12702 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12703 DW_AT_MIPS_linkage_name is preferred and used instead. */
12704
12705 lead = "__";
12706 sep = "_MOD_";
12707 }
987504bb
JJ
12708 else
12709 sep = "::";
63d06c5c 12710
6dd47d34
DE
12711 if (prefix == NULL)
12712 prefix = "";
12713 if (suffix == NULL)
12714 suffix = "";
12715
987504bb
JJ
12716 if (obs == NULL)
12717 {
3e43a32a
MS
12718 char *retval
12719 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12720
f55ee35c
JK
12721 strcpy (retval, lead);
12722 strcat (retval, prefix);
6dd47d34
DE
12723 strcat (retval, sep);
12724 strcat (retval, suffix);
63d06c5c
DC
12725 return retval;
12726 }
987504bb
JJ
12727 else
12728 {
12729 /* We have an obstack. */
f55ee35c 12730 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12731 }
63d06c5c
DC
12732}
12733
c906108c
SS
12734/* Return sibling of die, NULL if no sibling. */
12735
f9aca02d 12736static struct die_info *
fba45db2 12737sibling_die (struct die_info *die)
c906108c 12738{
639d11d3 12739 return die->sibling;
c906108c
SS
12740}
12741
71c25dea
TT
12742/* Get name of a die, return NULL if not found. */
12743
12744static char *
12745dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12746 struct obstack *obstack)
12747{
12748 if (name && cu->language == language_cplus)
12749 {
12750 char *canon_name = cp_canonicalize_string (name);
12751
12752 if (canon_name != NULL)
12753 {
12754 if (strcmp (canon_name, name) != 0)
12755 name = obsavestring (canon_name, strlen (canon_name),
12756 obstack);
12757 xfree (canon_name);
12758 }
12759 }
12760
12761 return name;
c906108c
SS
12762}
12763
9219021c
DC
12764/* Get name of a die, return NULL if not found. */
12765
12766static char *
e142c38c 12767dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12768{
12769 struct attribute *attr;
12770
e142c38c 12771 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12772 if ((!attr || !DW_STRING (attr))
12773 && die->tag != DW_TAG_class_type
12774 && die->tag != DW_TAG_interface_type
12775 && die->tag != DW_TAG_structure_type
12776 && die->tag != DW_TAG_union_type)
71c25dea
TT
12777 return NULL;
12778
12779 switch (die->tag)
12780 {
12781 case DW_TAG_compile_unit:
12782 /* Compilation units have a DW_AT_name that is a filename, not
12783 a source language identifier. */
12784 case DW_TAG_enumeration_type:
12785 case DW_TAG_enumerator:
12786 /* These tags always have simple identifiers already; no need
12787 to canonicalize them. */
12788 return DW_STRING (attr);
907af001 12789
418835cc
KS
12790 case DW_TAG_subprogram:
12791 /* Java constructors will all be named "<init>", so return
12792 the class name when we see this special case. */
12793 if (cu->language == language_java
12794 && DW_STRING (attr) != NULL
12795 && strcmp (DW_STRING (attr), "<init>") == 0)
12796 {
12797 struct dwarf2_cu *spec_cu = cu;
12798 struct die_info *spec_die;
12799
12800 /* GCJ will output '<init>' for Java constructor names.
12801 For this special case, return the name of the parent class. */
12802
12803 /* GCJ may output suprogram DIEs with AT_specification set.
12804 If so, use the name of the specified DIE. */
12805 spec_die = die_specification (die, &spec_cu);
12806 if (spec_die != NULL)
12807 return dwarf2_name (spec_die, spec_cu);
12808
12809 do
12810 {
12811 die = die->parent;
12812 if (die->tag == DW_TAG_class_type)
12813 return dwarf2_name (die, cu);
12814 }
12815 while (die->tag != DW_TAG_compile_unit);
12816 }
907af001
UW
12817 break;
12818
12819 case DW_TAG_class_type:
12820 case DW_TAG_interface_type:
12821 case DW_TAG_structure_type:
12822 case DW_TAG_union_type:
12823 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12824 structures or unions. These were of the form "._%d" in GCC 4.1,
12825 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12826 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12827 if (attr && DW_STRING (attr)
12828 && (strncmp (DW_STRING (attr), "._", 2) == 0
12829 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12830 return NULL;
53832f31
TT
12831
12832 /* GCC might emit a nameless typedef that has a linkage name. See
12833 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12834 if (!attr || DW_STRING (attr) == NULL)
12835 {
df5c6c50 12836 char *demangled = NULL;
53832f31
TT
12837
12838 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12839 if (attr == NULL)
12840 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12841
12842 if (attr == NULL || DW_STRING (attr) == NULL)
12843 return NULL;
12844
df5c6c50
JK
12845 /* Avoid demangling DW_STRING (attr) the second time on a second
12846 call for the same DIE. */
12847 if (!DW_STRING_IS_CANONICAL (attr))
12848 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12849
12850 if (demangled)
12851 {
96408a79
SA
12852 char *base;
12853
53832f31 12854 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12855 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12856 &cu->objfile->objfile_obstack);
53832f31
TT
12857 DW_STRING_IS_CANONICAL (attr) = 1;
12858 xfree (demangled);
96408a79
SA
12859
12860 /* Strip any leading namespaces/classes, keep only the base name.
12861 DW_AT_name for named DIEs does not contain the prefixes. */
12862 base = strrchr (DW_STRING (attr), ':');
12863 if (base && base > DW_STRING (attr) && base[-1] == ':')
12864 return &base[1];
12865 else
12866 return DW_STRING (attr);
53832f31
TT
12867 }
12868 }
907af001
UW
12869 break;
12870
71c25dea 12871 default:
907af001
UW
12872 break;
12873 }
12874
12875 if (!DW_STRING_IS_CANONICAL (attr))
12876 {
12877 DW_STRING (attr)
12878 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12879 &cu->objfile->objfile_obstack);
12880 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12881 }
907af001 12882 return DW_STRING (attr);
9219021c
DC
12883}
12884
12885/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12886 is none. *EXT_CU is the CU containing DIE on input, and the CU
12887 containing the return value on output. */
9219021c
DC
12888
12889static struct die_info *
f2f0e013 12890dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12891{
12892 struct attribute *attr;
9219021c 12893
f2f0e013 12894 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12895 if (attr == NULL)
12896 return NULL;
12897
f2f0e013 12898 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12899}
12900
c906108c
SS
12901/* Convert a DIE tag into its string name. */
12902
12903static char *
aa1ee363 12904dwarf_tag_name (unsigned tag)
c906108c
SS
12905{
12906 switch (tag)
12907 {
12908 case DW_TAG_padding:
12909 return "DW_TAG_padding";
12910 case DW_TAG_array_type:
12911 return "DW_TAG_array_type";
12912 case DW_TAG_class_type:
12913 return "DW_TAG_class_type";
12914 case DW_TAG_entry_point:
12915 return "DW_TAG_entry_point";
12916 case DW_TAG_enumeration_type:
12917 return "DW_TAG_enumeration_type";
12918 case DW_TAG_formal_parameter:
12919 return "DW_TAG_formal_parameter";
12920 case DW_TAG_imported_declaration:
12921 return "DW_TAG_imported_declaration";
12922 case DW_TAG_label:
12923 return "DW_TAG_label";
12924 case DW_TAG_lexical_block:
12925 return "DW_TAG_lexical_block";
12926 case DW_TAG_member:
12927 return "DW_TAG_member";
12928 case DW_TAG_pointer_type:
12929 return "DW_TAG_pointer_type";
12930 case DW_TAG_reference_type:
12931 return "DW_TAG_reference_type";
12932 case DW_TAG_compile_unit:
12933 return "DW_TAG_compile_unit";
12934 case DW_TAG_string_type:
12935 return "DW_TAG_string_type";
12936 case DW_TAG_structure_type:
12937 return "DW_TAG_structure_type";
12938 case DW_TAG_subroutine_type:
12939 return "DW_TAG_subroutine_type";
12940 case DW_TAG_typedef:
12941 return "DW_TAG_typedef";
12942 case DW_TAG_union_type:
12943 return "DW_TAG_union_type";
12944 case DW_TAG_unspecified_parameters:
12945 return "DW_TAG_unspecified_parameters";
12946 case DW_TAG_variant:
12947 return "DW_TAG_variant";
12948 case DW_TAG_common_block:
12949 return "DW_TAG_common_block";
12950 case DW_TAG_common_inclusion:
12951 return "DW_TAG_common_inclusion";
12952 case DW_TAG_inheritance:
12953 return "DW_TAG_inheritance";
12954 case DW_TAG_inlined_subroutine:
12955 return "DW_TAG_inlined_subroutine";
12956 case DW_TAG_module:
12957 return "DW_TAG_module";
12958 case DW_TAG_ptr_to_member_type:
12959 return "DW_TAG_ptr_to_member_type";
12960 case DW_TAG_set_type:
12961 return "DW_TAG_set_type";
12962 case DW_TAG_subrange_type:
12963 return "DW_TAG_subrange_type";
12964 case DW_TAG_with_stmt:
12965 return "DW_TAG_with_stmt";
12966 case DW_TAG_access_declaration:
12967 return "DW_TAG_access_declaration";
12968 case DW_TAG_base_type:
12969 return "DW_TAG_base_type";
12970 case DW_TAG_catch_block:
12971 return "DW_TAG_catch_block";
12972 case DW_TAG_const_type:
12973 return "DW_TAG_const_type";
12974 case DW_TAG_constant:
12975 return "DW_TAG_constant";
12976 case DW_TAG_enumerator:
12977 return "DW_TAG_enumerator";
12978 case DW_TAG_file_type:
12979 return "DW_TAG_file_type";
12980 case DW_TAG_friend:
12981 return "DW_TAG_friend";
12982 case DW_TAG_namelist:
12983 return "DW_TAG_namelist";
12984 case DW_TAG_namelist_item:
12985 return "DW_TAG_namelist_item";
12986 case DW_TAG_packed_type:
12987 return "DW_TAG_packed_type";
12988 case DW_TAG_subprogram:
12989 return "DW_TAG_subprogram";
12990 case DW_TAG_template_type_param:
12991 return "DW_TAG_template_type_param";
12992 case DW_TAG_template_value_param:
12993 return "DW_TAG_template_value_param";
12994 case DW_TAG_thrown_type:
12995 return "DW_TAG_thrown_type";
12996 case DW_TAG_try_block:
12997 return "DW_TAG_try_block";
12998 case DW_TAG_variant_part:
12999 return "DW_TAG_variant_part";
13000 case DW_TAG_variable:
13001 return "DW_TAG_variable";
13002 case DW_TAG_volatile_type:
13003 return "DW_TAG_volatile_type";
d9fa45fe
DC
13004 case DW_TAG_dwarf_procedure:
13005 return "DW_TAG_dwarf_procedure";
13006 case DW_TAG_restrict_type:
13007 return "DW_TAG_restrict_type";
13008 case DW_TAG_interface_type:
13009 return "DW_TAG_interface_type";
13010 case DW_TAG_namespace:
13011 return "DW_TAG_namespace";
13012 case DW_TAG_imported_module:
13013 return "DW_TAG_imported_module";
13014 case DW_TAG_unspecified_type:
13015 return "DW_TAG_unspecified_type";
13016 case DW_TAG_partial_unit:
13017 return "DW_TAG_partial_unit";
13018 case DW_TAG_imported_unit:
13019 return "DW_TAG_imported_unit";
b7619582
GF
13020 case DW_TAG_condition:
13021 return "DW_TAG_condition";
13022 case DW_TAG_shared_type:
13023 return "DW_TAG_shared_type";
348e048f
DE
13024 case DW_TAG_type_unit:
13025 return "DW_TAG_type_unit";
c906108c
SS
13026 case DW_TAG_MIPS_loop:
13027 return "DW_TAG_MIPS_loop";
b7619582
GF
13028 case DW_TAG_HP_array_descriptor:
13029 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13030 case DW_TAG_format_label:
13031 return "DW_TAG_format_label";
13032 case DW_TAG_function_template:
13033 return "DW_TAG_function_template";
13034 case DW_TAG_class_template:
13035 return "DW_TAG_class_template";
b7619582
GF
13036 case DW_TAG_GNU_BINCL:
13037 return "DW_TAG_GNU_BINCL";
13038 case DW_TAG_GNU_EINCL:
13039 return "DW_TAG_GNU_EINCL";
13040 case DW_TAG_upc_shared_type:
13041 return "DW_TAG_upc_shared_type";
13042 case DW_TAG_upc_strict_type:
13043 return "DW_TAG_upc_strict_type";
13044 case DW_TAG_upc_relaxed_type:
13045 return "DW_TAG_upc_relaxed_type";
13046 case DW_TAG_PGI_kanji_type:
13047 return "DW_TAG_PGI_kanji_type";
13048 case DW_TAG_PGI_interface_block:
13049 return "DW_TAG_PGI_interface_block";
96408a79
SA
13050 case DW_TAG_GNU_call_site:
13051 return "DW_TAG_GNU_call_site";
c906108c
SS
13052 default:
13053 return "DW_TAG_<unknown>";
13054 }
13055}
13056
13057/* Convert a DWARF attribute code into its string name. */
13058
13059static char *
aa1ee363 13060dwarf_attr_name (unsigned attr)
c906108c
SS
13061{
13062 switch (attr)
13063 {
13064 case DW_AT_sibling:
13065 return "DW_AT_sibling";
13066 case DW_AT_location:
13067 return "DW_AT_location";
13068 case DW_AT_name:
13069 return "DW_AT_name";
13070 case DW_AT_ordering:
13071 return "DW_AT_ordering";
13072 case DW_AT_subscr_data:
13073 return "DW_AT_subscr_data";
13074 case DW_AT_byte_size:
13075 return "DW_AT_byte_size";
13076 case DW_AT_bit_offset:
13077 return "DW_AT_bit_offset";
13078 case DW_AT_bit_size:
13079 return "DW_AT_bit_size";
13080 case DW_AT_element_list:
13081 return "DW_AT_element_list";
13082 case DW_AT_stmt_list:
13083 return "DW_AT_stmt_list";
13084 case DW_AT_low_pc:
13085 return "DW_AT_low_pc";
13086 case DW_AT_high_pc:
13087 return "DW_AT_high_pc";
13088 case DW_AT_language:
13089 return "DW_AT_language";
13090 case DW_AT_member:
13091 return "DW_AT_member";
13092 case DW_AT_discr:
13093 return "DW_AT_discr";
13094 case DW_AT_discr_value:
13095 return "DW_AT_discr_value";
13096 case DW_AT_visibility:
13097 return "DW_AT_visibility";
13098 case DW_AT_import:
13099 return "DW_AT_import";
13100 case DW_AT_string_length:
13101 return "DW_AT_string_length";
13102 case DW_AT_common_reference:
13103 return "DW_AT_common_reference";
13104 case DW_AT_comp_dir:
13105 return "DW_AT_comp_dir";
13106 case DW_AT_const_value:
13107 return "DW_AT_const_value";
13108 case DW_AT_containing_type:
13109 return "DW_AT_containing_type";
13110 case DW_AT_default_value:
13111 return "DW_AT_default_value";
13112 case DW_AT_inline:
13113 return "DW_AT_inline";
13114 case DW_AT_is_optional:
13115 return "DW_AT_is_optional";
13116 case DW_AT_lower_bound:
13117 return "DW_AT_lower_bound";
13118 case DW_AT_producer:
13119 return "DW_AT_producer";
13120 case DW_AT_prototyped:
13121 return "DW_AT_prototyped";
13122 case DW_AT_return_addr:
13123 return "DW_AT_return_addr";
13124 case DW_AT_start_scope:
13125 return "DW_AT_start_scope";
09fa0d7c
JK
13126 case DW_AT_bit_stride:
13127 return "DW_AT_bit_stride";
c906108c
SS
13128 case DW_AT_upper_bound:
13129 return "DW_AT_upper_bound";
13130 case DW_AT_abstract_origin:
13131 return "DW_AT_abstract_origin";
13132 case DW_AT_accessibility:
13133 return "DW_AT_accessibility";
13134 case DW_AT_address_class:
13135 return "DW_AT_address_class";
13136 case DW_AT_artificial:
13137 return "DW_AT_artificial";
13138 case DW_AT_base_types:
13139 return "DW_AT_base_types";
13140 case DW_AT_calling_convention:
13141 return "DW_AT_calling_convention";
13142 case DW_AT_count:
13143 return "DW_AT_count";
13144 case DW_AT_data_member_location:
13145 return "DW_AT_data_member_location";
13146 case DW_AT_decl_column:
13147 return "DW_AT_decl_column";
13148 case DW_AT_decl_file:
13149 return "DW_AT_decl_file";
13150 case DW_AT_decl_line:
13151 return "DW_AT_decl_line";
13152 case DW_AT_declaration:
13153 return "DW_AT_declaration";
13154 case DW_AT_discr_list:
13155 return "DW_AT_discr_list";
13156 case DW_AT_encoding:
13157 return "DW_AT_encoding";
13158 case DW_AT_external:
13159 return "DW_AT_external";
13160 case DW_AT_frame_base:
13161 return "DW_AT_frame_base";
13162 case DW_AT_friend:
13163 return "DW_AT_friend";
13164 case DW_AT_identifier_case:
13165 return "DW_AT_identifier_case";
13166 case DW_AT_macro_info:
13167 return "DW_AT_macro_info";
13168 case DW_AT_namelist_items:
13169 return "DW_AT_namelist_items";
13170 case DW_AT_priority:
13171 return "DW_AT_priority";
13172 case DW_AT_segment:
13173 return "DW_AT_segment";
13174 case DW_AT_specification:
13175 return "DW_AT_specification";
13176 case DW_AT_static_link:
13177 return "DW_AT_static_link";
13178 case DW_AT_type:
13179 return "DW_AT_type";
13180 case DW_AT_use_location:
13181 return "DW_AT_use_location";
13182 case DW_AT_variable_parameter:
13183 return "DW_AT_variable_parameter";
13184 case DW_AT_virtuality:
13185 return "DW_AT_virtuality";
13186 case DW_AT_vtable_elem_location:
13187 return "DW_AT_vtable_elem_location";
b7619582 13188 /* DWARF 3 values. */
d9fa45fe
DC
13189 case DW_AT_allocated:
13190 return "DW_AT_allocated";
13191 case DW_AT_associated:
13192 return "DW_AT_associated";
13193 case DW_AT_data_location:
13194 return "DW_AT_data_location";
09fa0d7c
JK
13195 case DW_AT_byte_stride:
13196 return "DW_AT_byte_stride";
d9fa45fe
DC
13197 case DW_AT_entry_pc:
13198 return "DW_AT_entry_pc";
13199 case DW_AT_use_UTF8:
13200 return "DW_AT_use_UTF8";
13201 case DW_AT_extension:
13202 return "DW_AT_extension";
13203 case DW_AT_ranges:
13204 return "DW_AT_ranges";
13205 case DW_AT_trampoline:
13206 return "DW_AT_trampoline";
13207 case DW_AT_call_column:
13208 return "DW_AT_call_column";
13209 case DW_AT_call_file:
13210 return "DW_AT_call_file";
13211 case DW_AT_call_line:
13212 return "DW_AT_call_line";
b7619582
GF
13213 case DW_AT_description:
13214 return "DW_AT_description";
13215 case DW_AT_binary_scale:
13216 return "DW_AT_binary_scale";
13217 case DW_AT_decimal_scale:
13218 return "DW_AT_decimal_scale";
13219 case DW_AT_small:
13220 return "DW_AT_small";
13221 case DW_AT_decimal_sign:
13222 return "DW_AT_decimal_sign";
13223 case DW_AT_digit_count:
13224 return "DW_AT_digit_count";
13225 case DW_AT_picture_string:
13226 return "DW_AT_picture_string";
13227 case DW_AT_mutable:
13228 return "DW_AT_mutable";
13229 case DW_AT_threads_scaled:
13230 return "DW_AT_threads_scaled";
13231 case DW_AT_explicit:
13232 return "DW_AT_explicit";
13233 case DW_AT_object_pointer:
13234 return "DW_AT_object_pointer";
13235 case DW_AT_endianity:
13236 return "DW_AT_endianity";
13237 case DW_AT_elemental:
13238 return "DW_AT_elemental";
13239 case DW_AT_pure:
13240 return "DW_AT_pure";
13241 case DW_AT_recursive:
13242 return "DW_AT_recursive";
348e048f
DE
13243 /* DWARF 4 values. */
13244 case DW_AT_signature:
13245 return "DW_AT_signature";
31ef98ae
TT
13246 case DW_AT_linkage_name:
13247 return "DW_AT_linkage_name";
b7619582 13248 /* SGI/MIPS extensions. */
c764a876 13249#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13250 case DW_AT_MIPS_fde:
13251 return "DW_AT_MIPS_fde";
c764a876 13252#endif
c906108c
SS
13253 case DW_AT_MIPS_loop_begin:
13254 return "DW_AT_MIPS_loop_begin";
13255 case DW_AT_MIPS_tail_loop_begin:
13256 return "DW_AT_MIPS_tail_loop_begin";
13257 case DW_AT_MIPS_epilog_begin:
13258 return "DW_AT_MIPS_epilog_begin";
13259 case DW_AT_MIPS_loop_unroll_factor:
13260 return "DW_AT_MIPS_loop_unroll_factor";
13261 case DW_AT_MIPS_software_pipeline_depth:
13262 return "DW_AT_MIPS_software_pipeline_depth";
13263 case DW_AT_MIPS_linkage_name:
13264 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13265 case DW_AT_MIPS_stride:
13266 return "DW_AT_MIPS_stride";
13267 case DW_AT_MIPS_abstract_name:
13268 return "DW_AT_MIPS_abstract_name";
13269 case DW_AT_MIPS_clone_origin:
13270 return "DW_AT_MIPS_clone_origin";
13271 case DW_AT_MIPS_has_inlines:
13272 return "DW_AT_MIPS_has_inlines";
b7619582 13273 /* HP extensions. */
c764a876 13274#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13275 case DW_AT_HP_block_index:
13276 return "DW_AT_HP_block_index";
c764a876 13277#endif
b7619582
GF
13278 case DW_AT_HP_unmodifiable:
13279 return "DW_AT_HP_unmodifiable";
13280 case DW_AT_HP_actuals_stmt_list:
13281 return "DW_AT_HP_actuals_stmt_list";
13282 case DW_AT_HP_proc_per_section:
13283 return "DW_AT_HP_proc_per_section";
13284 case DW_AT_HP_raw_data_ptr:
13285 return "DW_AT_HP_raw_data_ptr";
13286 case DW_AT_HP_pass_by_reference:
13287 return "DW_AT_HP_pass_by_reference";
13288 case DW_AT_HP_opt_level:
13289 return "DW_AT_HP_opt_level";
13290 case DW_AT_HP_prof_version_id:
13291 return "DW_AT_HP_prof_version_id";
13292 case DW_AT_HP_opt_flags:
13293 return "DW_AT_HP_opt_flags";
13294 case DW_AT_HP_cold_region_low_pc:
13295 return "DW_AT_HP_cold_region_low_pc";
13296 case DW_AT_HP_cold_region_high_pc:
13297 return "DW_AT_HP_cold_region_high_pc";
13298 case DW_AT_HP_all_variables_modifiable:
13299 return "DW_AT_HP_all_variables_modifiable";
13300 case DW_AT_HP_linkage_name:
13301 return "DW_AT_HP_linkage_name";
13302 case DW_AT_HP_prof_flags:
13303 return "DW_AT_HP_prof_flags";
13304 /* GNU extensions. */
c906108c
SS
13305 case DW_AT_sf_names:
13306 return "DW_AT_sf_names";
13307 case DW_AT_src_info:
13308 return "DW_AT_src_info";
13309 case DW_AT_mac_info:
13310 return "DW_AT_mac_info";
13311 case DW_AT_src_coords:
13312 return "DW_AT_src_coords";
13313 case DW_AT_body_begin:
13314 return "DW_AT_body_begin";
13315 case DW_AT_body_end:
13316 return "DW_AT_body_end";
f5f8a009
EZ
13317 case DW_AT_GNU_vector:
13318 return "DW_AT_GNU_vector";
2de00c64
DE
13319 case DW_AT_GNU_odr_signature:
13320 return "DW_AT_GNU_odr_signature";
b7619582
GF
13321 /* VMS extensions. */
13322 case DW_AT_VMS_rtnbeg_pd_address:
13323 return "DW_AT_VMS_rtnbeg_pd_address";
13324 /* UPC extension. */
13325 case DW_AT_upc_threads_scaled:
13326 return "DW_AT_upc_threads_scaled";
13327 /* PGI (STMicroelectronics) extensions. */
13328 case DW_AT_PGI_lbase:
13329 return "DW_AT_PGI_lbase";
13330 case DW_AT_PGI_soffset:
13331 return "DW_AT_PGI_soffset";
13332 case DW_AT_PGI_lstride:
13333 return "DW_AT_PGI_lstride";
c906108c
SS
13334 default:
13335 return "DW_AT_<unknown>";
13336 }
13337}
13338
13339/* Convert a DWARF value form code into its string name. */
13340
13341static char *
aa1ee363 13342dwarf_form_name (unsigned form)
c906108c
SS
13343{
13344 switch (form)
13345 {
13346 case DW_FORM_addr:
13347 return "DW_FORM_addr";
13348 case DW_FORM_block2:
13349 return "DW_FORM_block2";
13350 case DW_FORM_block4:
13351 return "DW_FORM_block4";
13352 case DW_FORM_data2:
13353 return "DW_FORM_data2";
13354 case DW_FORM_data4:
13355 return "DW_FORM_data4";
13356 case DW_FORM_data8:
13357 return "DW_FORM_data8";
13358 case DW_FORM_string:
13359 return "DW_FORM_string";
13360 case DW_FORM_block:
13361 return "DW_FORM_block";
13362 case DW_FORM_block1:
13363 return "DW_FORM_block1";
13364 case DW_FORM_data1:
13365 return "DW_FORM_data1";
13366 case DW_FORM_flag:
13367 return "DW_FORM_flag";
13368 case DW_FORM_sdata:
13369 return "DW_FORM_sdata";
13370 case DW_FORM_strp:
13371 return "DW_FORM_strp";
13372 case DW_FORM_udata:
13373 return "DW_FORM_udata";
13374 case DW_FORM_ref_addr:
13375 return "DW_FORM_ref_addr";
13376 case DW_FORM_ref1:
13377 return "DW_FORM_ref1";
13378 case DW_FORM_ref2:
13379 return "DW_FORM_ref2";
13380 case DW_FORM_ref4:
13381 return "DW_FORM_ref4";
13382 case DW_FORM_ref8:
13383 return "DW_FORM_ref8";
13384 case DW_FORM_ref_udata:
13385 return "DW_FORM_ref_udata";
13386 case DW_FORM_indirect:
13387 return "DW_FORM_indirect";
348e048f
DE
13388 case DW_FORM_sec_offset:
13389 return "DW_FORM_sec_offset";
13390 case DW_FORM_exprloc:
13391 return "DW_FORM_exprloc";
13392 case DW_FORM_flag_present:
13393 return "DW_FORM_flag_present";
55f1336d
TT
13394 case DW_FORM_ref_sig8:
13395 return "DW_FORM_ref_sig8";
c906108c
SS
13396 default:
13397 return "DW_FORM_<unknown>";
13398 }
13399}
13400
13401/* Convert a DWARF stack opcode into its string name. */
13402
9eae7c52 13403const char *
b1bfef65 13404dwarf_stack_op_name (unsigned op)
c906108c
SS
13405{
13406 switch (op)
13407 {
13408 case DW_OP_addr:
13409 return "DW_OP_addr";
13410 case DW_OP_deref:
13411 return "DW_OP_deref";
13412 case DW_OP_const1u:
13413 return "DW_OP_const1u";
13414 case DW_OP_const1s:
13415 return "DW_OP_const1s";
13416 case DW_OP_const2u:
13417 return "DW_OP_const2u";
13418 case DW_OP_const2s:
13419 return "DW_OP_const2s";
13420 case DW_OP_const4u:
13421 return "DW_OP_const4u";
13422 case DW_OP_const4s:
13423 return "DW_OP_const4s";
13424 case DW_OP_const8u:
13425 return "DW_OP_const8u";
13426 case DW_OP_const8s:
13427 return "DW_OP_const8s";
13428 case DW_OP_constu:
13429 return "DW_OP_constu";
13430 case DW_OP_consts:
13431 return "DW_OP_consts";
13432 case DW_OP_dup:
13433 return "DW_OP_dup";
13434 case DW_OP_drop:
13435 return "DW_OP_drop";
13436 case DW_OP_over:
13437 return "DW_OP_over";
13438 case DW_OP_pick:
13439 return "DW_OP_pick";
13440 case DW_OP_swap:
13441 return "DW_OP_swap";
13442 case DW_OP_rot:
13443 return "DW_OP_rot";
13444 case DW_OP_xderef:
13445 return "DW_OP_xderef";
13446 case DW_OP_abs:
13447 return "DW_OP_abs";
13448 case DW_OP_and:
13449 return "DW_OP_and";
13450 case DW_OP_div:
13451 return "DW_OP_div";
13452 case DW_OP_minus:
13453 return "DW_OP_minus";
13454 case DW_OP_mod:
13455 return "DW_OP_mod";
13456 case DW_OP_mul:
13457 return "DW_OP_mul";
13458 case DW_OP_neg:
13459 return "DW_OP_neg";
13460 case DW_OP_not:
13461 return "DW_OP_not";
13462 case DW_OP_or:
13463 return "DW_OP_or";
13464 case DW_OP_plus:
13465 return "DW_OP_plus";
13466 case DW_OP_plus_uconst:
13467 return "DW_OP_plus_uconst";
13468 case DW_OP_shl:
13469 return "DW_OP_shl";
13470 case DW_OP_shr:
13471 return "DW_OP_shr";
13472 case DW_OP_shra:
13473 return "DW_OP_shra";
13474 case DW_OP_xor:
13475 return "DW_OP_xor";
13476 case DW_OP_bra:
13477 return "DW_OP_bra";
13478 case DW_OP_eq:
13479 return "DW_OP_eq";
13480 case DW_OP_ge:
13481 return "DW_OP_ge";
13482 case DW_OP_gt:
13483 return "DW_OP_gt";
13484 case DW_OP_le:
13485 return "DW_OP_le";
13486 case DW_OP_lt:
13487 return "DW_OP_lt";
13488 case DW_OP_ne:
13489 return "DW_OP_ne";
13490 case DW_OP_skip:
13491 return "DW_OP_skip";
13492 case DW_OP_lit0:
13493 return "DW_OP_lit0";
13494 case DW_OP_lit1:
13495 return "DW_OP_lit1";
13496 case DW_OP_lit2:
13497 return "DW_OP_lit2";
13498 case DW_OP_lit3:
13499 return "DW_OP_lit3";
13500 case DW_OP_lit4:
13501 return "DW_OP_lit4";
13502 case DW_OP_lit5:
13503 return "DW_OP_lit5";
13504 case DW_OP_lit6:
13505 return "DW_OP_lit6";
13506 case DW_OP_lit7:
13507 return "DW_OP_lit7";
13508 case DW_OP_lit8:
13509 return "DW_OP_lit8";
13510 case DW_OP_lit9:
13511 return "DW_OP_lit9";
13512 case DW_OP_lit10:
13513 return "DW_OP_lit10";
13514 case DW_OP_lit11:
13515 return "DW_OP_lit11";
13516 case DW_OP_lit12:
13517 return "DW_OP_lit12";
13518 case DW_OP_lit13:
13519 return "DW_OP_lit13";
13520 case DW_OP_lit14:
13521 return "DW_OP_lit14";
13522 case DW_OP_lit15:
13523 return "DW_OP_lit15";
13524 case DW_OP_lit16:
13525 return "DW_OP_lit16";
13526 case DW_OP_lit17:
13527 return "DW_OP_lit17";
13528 case DW_OP_lit18:
13529 return "DW_OP_lit18";
13530 case DW_OP_lit19:
13531 return "DW_OP_lit19";
13532 case DW_OP_lit20:
13533 return "DW_OP_lit20";
13534 case DW_OP_lit21:
13535 return "DW_OP_lit21";
13536 case DW_OP_lit22:
13537 return "DW_OP_lit22";
13538 case DW_OP_lit23:
13539 return "DW_OP_lit23";
13540 case DW_OP_lit24:
13541 return "DW_OP_lit24";
13542 case DW_OP_lit25:
13543 return "DW_OP_lit25";
13544 case DW_OP_lit26:
13545 return "DW_OP_lit26";
13546 case DW_OP_lit27:
13547 return "DW_OP_lit27";
13548 case DW_OP_lit28:
13549 return "DW_OP_lit28";
13550 case DW_OP_lit29:
13551 return "DW_OP_lit29";
13552 case DW_OP_lit30:
13553 return "DW_OP_lit30";
13554 case DW_OP_lit31:
13555 return "DW_OP_lit31";
13556 case DW_OP_reg0:
13557 return "DW_OP_reg0";
13558 case DW_OP_reg1:
13559 return "DW_OP_reg1";
13560 case DW_OP_reg2:
13561 return "DW_OP_reg2";
13562 case DW_OP_reg3:
13563 return "DW_OP_reg3";
13564 case DW_OP_reg4:
13565 return "DW_OP_reg4";
13566 case DW_OP_reg5:
13567 return "DW_OP_reg5";
13568 case DW_OP_reg6:
13569 return "DW_OP_reg6";
13570 case DW_OP_reg7:
13571 return "DW_OP_reg7";
13572 case DW_OP_reg8:
13573 return "DW_OP_reg8";
13574 case DW_OP_reg9:
13575 return "DW_OP_reg9";
13576 case DW_OP_reg10:
13577 return "DW_OP_reg10";
13578 case DW_OP_reg11:
13579 return "DW_OP_reg11";
13580 case DW_OP_reg12:
13581 return "DW_OP_reg12";
13582 case DW_OP_reg13:
13583 return "DW_OP_reg13";
13584 case DW_OP_reg14:
13585 return "DW_OP_reg14";
13586 case DW_OP_reg15:
13587 return "DW_OP_reg15";
13588 case DW_OP_reg16:
13589 return "DW_OP_reg16";
13590 case DW_OP_reg17:
13591 return "DW_OP_reg17";
13592 case DW_OP_reg18:
13593 return "DW_OP_reg18";
13594 case DW_OP_reg19:
13595 return "DW_OP_reg19";
13596 case DW_OP_reg20:
13597 return "DW_OP_reg20";
13598 case DW_OP_reg21:
13599 return "DW_OP_reg21";
13600 case DW_OP_reg22:
13601 return "DW_OP_reg22";
13602 case DW_OP_reg23:
13603 return "DW_OP_reg23";
13604 case DW_OP_reg24:
13605 return "DW_OP_reg24";
13606 case DW_OP_reg25:
13607 return "DW_OP_reg25";
13608 case DW_OP_reg26:
13609 return "DW_OP_reg26";
13610 case DW_OP_reg27:
13611 return "DW_OP_reg27";
13612 case DW_OP_reg28:
13613 return "DW_OP_reg28";
13614 case DW_OP_reg29:
13615 return "DW_OP_reg29";
13616 case DW_OP_reg30:
13617 return "DW_OP_reg30";
13618 case DW_OP_reg31:
13619 return "DW_OP_reg31";
13620 case DW_OP_breg0:
13621 return "DW_OP_breg0";
13622 case DW_OP_breg1:
13623 return "DW_OP_breg1";
13624 case DW_OP_breg2:
13625 return "DW_OP_breg2";
13626 case DW_OP_breg3:
13627 return "DW_OP_breg3";
13628 case DW_OP_breg4:
13629 return "DW_OP_breg4";
13630 case DW_OP_breg5:
13631 return "DW_OP_breg5";
13632 case DW_OP_breg6:
13633 return "DW_OP_breg6";
13634 case DW_OP_breg7:
13635 return "DW_OP_breg7";
13636 case DW_OP_breg8:
13637 return "DW_OP_breg8";
13638 case DW_OP_breg9:
13639 return "DW_OP_breg9";
13640 case DW_OP_breg10:
13641 return "DW_OP_breg10";
13642 case DW_OP_breg11:
13643 return "DW_OP_breg11";
13644 case DW_OP_breg12:
13645 return "DW_OP_breg12";
13646 case DW_OP_breg13:
13647 return "DW_OP_breg13";
13648 case DW_OP_breg14:
13649 return "DW_OP_breg14";
13650 case DW_OP_breg15:
13651 return "DW_OP_breg15";
13652 case DW_OP_breg16:
13653 return "DW_OP_breg16";
13654 case DW_OP_breg17:
13655 return "DW_OP_breg17";
13656 case DW_OP_breg18:
13657 return "DW_OP_breg18";
13658 case DW_OP_breg19:
13659 return "DW_OP_breg19";
13660 case DW_OP_breg20:
13661 return "DW_OP_breg20";
13662 case DW_OP_breg21:
13663 return "DW_OP_breg21";
13664 case DW_OP_breg22:
13665 return "DW_OP_breg22";
13666 case DW_OP_breg23:
13667 return "DW_OP_breg23";
13668 case DW_OP_breg24:
13669 return "DW_OP_breg24";
13670 case DW_OP_breg25:
13671 return "DW_OP_breg25";
13672 case DW_OP_breg26:
13673 return "DW_OP_breg26";
13674 case DW_OP_breg27:
13675 return "DW_OP_breg27";
13676 case DW_OP_breg28:
13677 return "DW_OP_breg28";
13678 case DW_OP_breg29:
13679 return "DW_OP_breg29";
13680 case DW_OP_breg30:
13681 return "DW_OP_breg30";
13682 case DW_OP_breg31:
13683 return "DW_OP_breg31";
13684 case DW_OP_regx:
13685 return "DW_OP_regx";
13686 case DW_OP_fbreg:
13687 return "DW_OP_fbreg";
13688 case DW_OP_bregx:
13689 return "DW_OP_bregx";
13690 case DW_OP_piece:
13691 return "DW_OP_piece";
13692 case DW_OP_deref_size:
13693 return "DW_OP_deref_size";
13694 case DW_OP_xderef_size:
13695 return "DW_OP_xderef_size";
13696 case DW_OP_nop:
13697 return "DW_OP_nop";
b7619582 13698 /* DWARF 3 extensions. */
ed348acc
EZ
13699 case DW_OP_push_object_address:
13700 return "DW_OP_push_object_address";
13701 case DW_OP_call2:
13702 return "DW_OP_call2";
13703 case DW_OP_call4:
13704 return "DW_OP_call4";
13705 case DW_OP_call_ref:
13706 return "DW_OP_call_ref";
b7619582
GF
13707 case DW_OP_form_tls_address:
13708 return "DW_OP_form_tls_address";
13709 case DW_OP_call_frame_cfa:
13710 return "DW_OP_call_frame_cfa";
13711 case DW_OP_bit_piece:
13712 return "DW_OP_bit_piece";
9eae7c52
TT
13713 /* DWARF 4 extensions. */
13714 case DW_OP_implicit_value:
13715 return "DW_OP_implicit_value";
13716 case DW_OP_stack_value:
13717 return "DW_OP_stack_value";
13718 /* GNU extensions. */
ed348acc
EZ
13719 case DW_OP_GNU_push_tls_address:
13720 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13721 case DW_OP_GNU_uninit:
13722 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13723 case DW_OP_GNU_implicit_pointer:
13724 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13725 case DW_OP_GNU_entry_value:
13726 return "DW_OP_GNU_entry_value";
13727 case DW_OP_GNU_const_type:
13728 return "DW_OP_GNU_const_type";
13729 case DW_OP_GNU_regval_type:
13730 return "DW_OP_GNU_regval_type";
13731 case DW_OP_GNU_deref_type:
13732 return "DW_OP_GNU_deref_type";
13733 case DW_OP_GNU_convert:
13734 return "DW_OP_GNU_convert";
13735 case DW_OP_GNU_reinterpret:
13736 return "DW_OP_GNU_reinterpret";
c906108c 13737 default:
b1bfef65 13738 return NULL;
c906108c
SS
13739 }
13740}
13741
13742static char *
fba45db2 13743dwarf_bool_name (unsigned mybool)
c906108c
SS
13744{
13745 if (mybool)
13746 return "TRUE";
13747 else
13748 return "FALSE";
13749}
13750
13751/* Convert a DWARF type code into its string name. */
13752
13753static char *
aa1ee363 13754dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13755{
13756 switch (enc)
13757 {
b7619582
GF
13758 case DW_ATE_void:
13759 return "DW_ATE_void";
c906108c
SS
13760 case DW_ATE_address:
13761 return "DW_ATE_address";
13762 case DW_ATE_boolean:
13763 return "DW_ATE_boolean";
13764 case DW_ATE_complex_float:
13765 return "DW_ATE_complex_float";
13766 case DW_ATE_float:
13767 return "DW_ATE_float";
13768 case DW_ATE_signed:
13769 return "DW_ATE_signed";
13770 case DW_ATE_signed_char:
13771 return "DW_ATE_signed_char";
13772 case DW_ATE_unsigned:
13773 return "DW_ATE_unsigned";
13774 case DW_ATE_unsigned_char:
13775 return "DW_ATE_unsigned_char";
b7619582 13776 /* DWARF 3. */
d9fa45fe
DC
13777 case DW_ATE_imaginary_float:
13778 return "DW_ATE_imaginary_float";
b7619582
GF
13779 case DW_ATE_packed_decimal:
13780 return "DW_ATE_packed_decimal";
13781 case DW_ATE_numeric_string:
13782 return "DW_ATE_numeric_string";
13783 case DW_ATE_edited:
13784 return "DW_ATE_edited";
13785 case DW_ATE_signed_fixed:
13786 return "DW_ATE_signed_fixed";
13787 case DW_ATE_unsigned_fixed:
13788 return "DW_ATE_unsigned_fixed";
13789 case DW_ATE_decimal_float:
13790 return "DW_ATE_decimal_float";
75079b2b
TT
13791 /* DWARF 4. */
13792 case DW_ATE_UTF:
13793 return "DW_ATE_UTF";
b7619582
GF
13794 /* HP extensions. */
13795 case DW_ATE_HP_float80:
13796 return "DW_ATE_HP_float80";
13797 case DW_ATE_HP_complex_float80:
13798 return "DW_ATE_HP_complex_float80";
13799 case DW_ATE_HP_float128:
13800 return "DW_ATE_HP_float128";
13801 case DW_ATE_HP_complex_float128:
13802 return "DW_ATE_HP_complex_float128";
13803 case DW_ATE_HP_floathpintel:
13804 return "DW_ATE_HP_floathpintel";
13805 case DW_ATE_HP_imaginary_float80:
13806 return "DW_ATE_HP_imaginary_float80";
13807 case DW_ATE_HP_imaginary_float128:
13808 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13809 default:
13810 return "DW_ATE_<unknown>";
13811 }
13812}
13813
0963b4bd 13814/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13815
13816#if 0
13817static char *
aa1ee363 13818dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13819{
13820 switch (cfi_opc)
13821 {
13822 case DW_CFA_advance_loc:
13823 return "DW_CFA_advance_loc";
13824 case DW_CFA_offset:
13825 return "DW_CFA_offset";
13826 case DW_CFA_restore:
13827 return "DW_CFA_restore";
13828 case DW_CFA_nop:
13829 return "DW_CFA_nop";
13830 case DW_CFA_set_loc:
13831 return "DW_CFA_set_loc";
13832 case DW_CFA_advance_loc1:
13833 return "DW_CFA_advance_loc1";
13834 case DW_CFA_advance_loc2:
13835 return "DW_CFA_advance_loc2";
13836 case DW_CFA_advance_loc4:
13837 return "DW_CFA_advance_loc4";
13838 case DW_CFA_offset_extended:
13839 return "DW_CFA_offset_extended";
13840 case DW_CFA_restore_extended:
13841 return "DW_CFA_restore_extended";
13842 case DW_CFA_undefined:
13843 return "DW_CFA_undefined";
13844 case DW_CFA_same_value:
13845 return "DW_CFA_same_value";
13846 case DW_CFA_register:
13847 return "DW_CFA_register";
13848 case DW_CFA_remember_state:
13849 return "DW_CFA_remember_state";
13850 case DW_CFA_restore_state:
13851 return "DW_CFA_restore_state";
13852 case DW_CFA_def_cfa:
13853 return "DW_CFA_def_cfa";
13854 case DW_CFA_def_cfa_register:
13855 return "DW_CFA_def_cfa_register";
13856 case DW_CFA_def_cfa_offset:
13857 return "DW_CFA_def_cfa_offset";
b7619582 13858 /* DWARF 3. */
985cb1a3
JM
13859 case DW_CFA_def_cfa_expression:
13860 return "DW_CFA_def_cfa_expression";
13861 case DW_CFA_expression:
13862 return "DW_CFA_expression";
13863 case DW_CFA_offset_extended_sf:
13864 return "DW_CFA_offset_extended_sf";
13865 case DW_CFA_def_cfa_sf:
13866 return "DW_CFA_def_cfa_sf";
13867 case DW_CFA_def_cfa_offset_sf:
13868 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13869 case DW_CFA_val_offset:
13870 return "DW_CFA_val_offset";
13871 case DW_CFA_val_offset_sf:
13872 return "DW_CFA_val_offset_sf";
13873 case DW_CFA_val_expression:
13874 return "DW_CFA_val_expression";
13875 /* SGI/MIPS specific. */
c906108c
SS
13876 case DW_CFA_MIPS_advance_loc8:
13877 return "DW_CFA_MIPS_advance_loc8";
b7619582 13878 /* GNU extensions. */
985cb1a3
JM
13879 case DW_CFA_GNU_window_save:
13880 return "DW_CFA_GNU_window_save";
13881 case DW_CFA_GNU_args_size:
13882 return "DW_CFA_GNU_args_size";
13883 case DW_CFA_GNU_negative_offset_extended:
13884 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13885 default:
13886 return "DW_CFA_<unknown>";
13887 }
13888}
13889#endif
13890
f9aca02d 13891static void
d97bc12b 13892dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13893{
13894 unsigned int i;
13895
d97bc12b
DE
13896 print_spaces (indent, f);
13897 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13898 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13899
13900 if (die->parent != NULL)
13901 {
13902 print_spaces (indent, f);
13903 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13904 die->parent->offset);
13905 }
13906
13907 print_spaces (indent, f);
13908 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13909 dwarf_bool_name (die->child != NULL));
c906108c 13910
d97bc12b
DE
13911 print_spaces (indent, f);
13912 fprintf_unfiltered (f, " attributes:\n");
13913
c906108c
SS
13914 for (i = 0; i < die->num_attrs; ++i)
13915 {
d97bc12b
DE
13916 print_spaces (indent, f);
13917 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13918 dwarf_attr_name (die->attrs[i].name),
13919 dwarf_form_name (die->attrs[i].form));
d97bc12b 13920
c906108c
SS
13921 switch (die->attrs[i].form)
13922 {
13923 case DW_FORM_ref_addr:
13924 case DW_FORM_addr:
d97bc12b 13925 fprintf_unfiltered (f, "address: ");
5af949e3 13926 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13927 break;
13928 case DW_FORM_block2:
13929 case DW_FORM_block4:
13930 case DW_FORM_block:
13931 case DW_FORM_block1:
3e43a32a
MS
13932 fprintf_unfiltered (f, "block: size %d",
13933 DW_BLOCK (&die->attrs[i])->size);
c906108c 13934 break;
2dc7f7b3
TT
13935 case DW_FORM_exprloc:
13936 fprintf_unfiltered (f, "expression: size %u",
13937 DW_BLOCK (&die->attrs[i])->size);
13938 break;
10b3939b
DJ
13939 case DW_FORM_ref1:
13940 case DW_FORM_ref2:
13941 case DW_FORM_ref4:
d97bc12b 13942 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13943 (long) (DW_ADDR (&die->attrs[i])));
13944 break;
c906108c
SS
13945 case DW_FORM_data1:
13946 case DW_FORM_data2:
13947 case DW_FORM_data4:
ce5d95e1 13948 case DW_FORM_data8:
c906108c
SS
13949 case DW_FORM_udata:
13950 case DW_FORM_sdata:
43bbcdc2
PH
13951 fprintf_unfiltered (f, "constant: %s",
13952 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13953 break;
2dc7f7b3
TT
13954 case DW_FORM_sec_offset:
13955 fprintf_unfiltered (f, "section offset: %s",
13956 pulongest (DW_UNSND (&die->attrs[i])));
13957 break;
55f1336d 13958 case DW_FORM_ref_sig8:
348e048f
DE
13959 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13960 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13961 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13962 else
13963 fprintf_unfiltered (f, "signatured type, offset: unknown");
13964 break;
c906108c 13965 case DW_FORM_string:
4bdf3d34 13966 case DW_FORM_strp:
8285870a 13967 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13968 DW_STRING (&die->attrs[i])
8285870a
JK
13969 ? DW_STRING (&die->attrs[i]) : "",
13970 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13971 break;
13972 case DW_FORM_flag:
13973 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13974 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13975 else
d97bc12b 13976 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13977 break;
2dc7f7b3
TT
13978 case DW_FORM_flag_present:
13979 fprintf_unfiltered (f, "flag: TRUE");
13980 break;
a8329558 13981 case DW_FORM_indirect:
0963b4bd
MS
13982 /* The reader will have reduced the indirect form to
13983 the "base form" so this form should not occur. */
3e43a32a
MS
13984 fprintf_unfiltered (f,
13985 "unexpected attribute form: DW_FORM_indirect");
a8329558 13986 break;
c906108c 13987 default:
d97bc12b 13988 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13989 die->attrs[i].form);
d97bc12b 13990 break;
c906108c 13991 }
d97bc12b 13992 fprintf_unfiltered (f, "\n");
c906108c
SS
13993 }
13994}
13995
f9aca02d 13996static void
d97bc12b 13997dump_die_for_error (struct die_info *die)
c906108c 13998{
d97bc12b
DE
13999 dump_die_shallow (gdb_stderr, 0, die);
14000}
14001
14002static void
14003dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14004{
14005 int indent = level * 4;
14006
14007 gdb_assert (die != NULL);
14008
14009 if (level >= max_level)
14010 return;
14011
14012 dump_die_shallow (f, indent, die);
14013
14014 if (die->child != NULL)
c906108c 14015 {
d97bc12b
DE
14016 print_spaces (indent, f);
14017 fprintf_unfiltered (f, " Children:");
14018 if (level + 1 < max_level)
14019 {
14020 fprintf_unfiltered (f, "\n");
14021 dump_die_1 (f, level + 1, max_level, die->child);
14022 }
14023 else
14024 {
3e43a32a
MS
14025 fprintf_unfiltered (f,
14026 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14027 }
14028 }
14029
14030 if (die->sibling != NULL && level > 0)
14031 {
14032 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14033 }
14034}
14035
d97bc12b
DE
14036/* This is called from the pdie macro in gdbinit.in.
14037 It's not static so gcc will keep a copy callable from gdb. */
14038
14039void
14040dump_die (struct die_info *die, int max_level)
14041{
14042 dump_die_1 (gdb_stdlog, 0, max_level, die);
14043}
14044
f9aca02d 14045static void
51545339 14046store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14047{
51545339 14048 void **slot;
c906108c 14049
51545339
DJ
14050 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14051
14052 *slot = die;
c906108c
SS
14053}
14054
93311388
DE
14055static int
14056is_ref_attr (struct attribute *attr)
c906108c 14057{
c906108c
SS
14058 switch (attr->form)
14059 {
14060 case DW_FORM_ref_addr:
c906108c
SS
14061 case DW_FORM_ref1:
14062 case DW_FORM_ref2:
14063 case DW_FORM_ref4:
613e1657 14064 case DW_FORM_ref8:
c906108c 14065 case DW_FORM_ref_udata:
93311388 14066 return 1;
c906108c 14067 default:
93311388 14068 return 0;
c906108c 14069 }
93311388
DE
14070}
14071
14072static unsigned int
14073dwarf2_get_ref_die_offset (struct attribute *attr)
14074{
14075 if (is_ref_attr (attr))
14076 return DW_ADDR (attr);
14077
14078 complaint (&symfile_complaints,
14079 _("unsupported die ref attribute form: '%s'"),
14080 dwarf_form_name (attr->form));
14081 return 0;
c906108c
SS
14082}
14083
43bbcdc2
PH
14084/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14085 * the value held by the attribute is not constant. */
a02abb62 14086
43bbcdc2 14087static LONGEST
a02abb62
JB
14088dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14089{
14090 if (attr->form == DW_FORM_sdata)
14091 return DW_SND (attr);
14092 else if (attr->form == DW_FORM_udata
14093 || attr->form == DW_FORM_data1
14094 || attr->form == DW_FORM_data2
14095 || attr->form == DW_FORM_data4
14096 || attr->form == DW_FORM_data8)
14097 return DW_UNSND (attr);
14098 else
14099 {
3e43a32a
MS
14100 complaint (&symfile_complaints,
14101 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14102 dwarf_form_name (attr->form));
14103 return default_value;
14104 }
14105}
14106
03dd20cc 14107/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14108 unit and add it to our queue.
14109 The result is non-zero if PER_CU was queued, otherwise the result is zero
14110 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14111
348e048f 14112static int
03dd20cc
DJ
14113maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14114 struct dwarf2_per_cu_data *per_cu)
14115{
98bfdba5
PA
14116 /* We may arrive here during partial symbol reading, if we need full
14117 DIEs to process an unusual case (e.g. template arguments). Do
14118 not queue PER_CU, just tell our caller to load its DIEs. */
14119 if (dwarf2_per_objfile->reading_partial_symbols)
14120 {
14121 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14122 return 1;
14123 return 0;
14124 }
14125
03dd20cc
DJ
14126 /* Mark the dependence relation so that we don't flush PER_CU
14127 too early. */
14128 dwarf2_add_dependence (this_cu, per_cu);
14129
14130 /* If it's already on the queue, we have nothing to do. */
14131 if (per_cu->queued)
348e048f 14132 return 0;
03dd20cc
DJ
14133
14134 /* If the compilation unit is already loaded, just mark it as
14135 used. */
14136 if (per_cu->cu != NULL)
14137 {
14138 per_cu->cu->last_used = 0;
348e048f 14139 return 0;
03dd20cc
DJ
14140 }
14141
14142 /* Add it to the queue. */
14143 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14144
14145 return 1;
14146}
14147
14148/* Follow reference or signature attribute ATTR of SRC_DIE.
14149 On entry *REF_CU is the CU of SRC_DIE.
14150 On exit *REF_CU is the CU of the result. */
14151
14152static struct die_info *
14153follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14154 struct dwarf2_cu **ref_cu)
14155{
14156 struct die_info *die;
14157
14158 if (is_ref_attr (attr))
14159 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14160 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14161 die = follow_die_sig (src_die, attr, ref_cu);
14162 else
14163 {
14164 dump_die_for_error (src_die);
14165 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14166 (*ref_cu)->objfile->name);
14167 }
14168
14169 return die;
03dd20cc
DJ
14170}
14171
5c631832 14172/* Follow reference OFFSET.
673bfd45
DE
14173 On entry *REF_CU is the CU of the source die referencing OFFSET.
14174 On exit *REF_CU is the CU of the result.
14175 Returns NULL if OFFSET is invalid. */
f504f079 14176
f9aca02d 14177static struct die_info *
5c631832 14178follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14179{
10b3939b 14180 struct die_info temp_die;
f2f0e013 14181 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14182
348e048f
DE
14183 gdb_assert (cu->per_cu != NULL);
14184
98bfdba5
PA
14185 target_cu = cu;
14186
b0df02fd 14187 if (cu->per_cu->debug_types_section)
348e048f
DE
14188 {
14189 /* .debug_types CUs cannot reference anything outside their CU.
14190 If they need to, they have to reference a signatured type via
55f1336d 14191 DW_FORM_ref_sig8. */
348e048f 14192 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14193 return NULL;
348e048f
DE
14194 }
14195 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14196 {
14197 struct dwarf2_per_cu_data *per_cu;
9a619af0 14198
45452591 14199 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14200
14201 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14202 if (maybe_queue_comp_unit (cu, per_cu))
14203 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14204
10b3939b
DJ
14205 target_cu = per_cu->cu;
14206 }
98bfdba5
PA
14207 else if (cu->dies == NULL)
14208 {
14209 /* We're loading full DIEs during partial symbol reading. */
14210 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14211 load_full_comp_unit (cu->per_cu, cu->objfile);
14212 }
c906108c 14213
f2f0e013 14214 *ref_cu = target_cu;
51545339 14215 temp_die.offset = offset;
5c631832
JK
14216 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14217}
10b3939b 14218
5c631832
JK
14219/* Follow reference attribute ATTR of SRC_DIE.
14220 On entry *REF_CU is the CU of SRC_DIE.
14221 On exit *REF_CU is the CU of the result. */
14222
14223static struct die_info *
14224follow_die_ref (struct die_info *src_die, struct attribute *attr,
14225 struct dwarf2_cu **ref_cu)
14226{
14227 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14228 struct dwarf2_cu *cu = *ref_cu;
14229 struct die_info *die;
14230
14231 die = follow_die_offset (offset, ref_cu);
14232 if (!die)
14233 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14234 "at 0x%x [in module %s]"),
14235 offset, src_die->offset, cu->objfile->name);
348e048f 14236
5c631832
JK
14237 return die;
14238}
14239
d83e736b
JK
14240/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14241 Returned value is intended for DW_OP_call*. Returned
14242 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14243
14244struct dwarf2_locexpr_baton
14245dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14246 struct dwarf2_per_cu_data *per_cu,
14247 CORE_ADDR (*get_frame_pc) (void *baton),
14248 void *baton)
5c631832 14249{
918dd910 14250 struct dwarf2_cu *cu;
5c631832
JK
14251 struct die_info *die;
14252 struct attribute *attr;
14253 struct dwarf2_locexpr_baton retval;
14254
8cf6f0b1
TT
14255 dw2_setup (per_cu->objfile);
14256
918dd910
JK
14257 if (per_cu->cu == NULL)
14258 load_cu (per_cu);
14259 cu = per_cu->cu;
14260
5c631832
JK
14261 die = follow_die_offset (offset, &cu);
14262 if (!die)
14263 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14264 offset, per_cu->cu->objfile->name);
14265
14266 attr = dwarf2_attr (die, DW_AT_location, cu);
14267 if (!attr)
14268 {
e103e986
JK
14269 /* DWARF: "If there is no such attribute, then there is no effect.".
14270 DATA is ignored if SIZE is 0. */
5c631832 14271
e103e986 14272 retval.data = NULL;
5c631832
JK
14273 retval.size = 0;
14274 }
8cf6f0b1
TT
14275 else if (attr_form_is_section_offset (attr))
14276 {
14277 struct dwarf2_loclist_baton loclist_baton;
14278 CORE_ADDR pc = (*get_frame_pc) (baton);
14279 size_t size;
14280
14281 fill_in_loclist_baton (cu, &loclist_baton, attr);
14282
14283 retval.data = dwarf2_find_location_expression (&loclist_baton,
14284 &size, pc);
14285 retval.size = size;
14286 }
5c631832
JK
14287 else
14288 {
14289 if (!attr_form_is_block (attr))
14290 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14291 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14292 offset, per_cu->cu->objfile->name);
14293
14294 retval.data = DW_BLOCK (attr)->data;
14295 retval.size = DW_BLOCK (attr)->size;
14296 }
14297 retval.per_cu = cu->per_cu;
918dd910 14298
918dd910
JK
14299 age_cached_comp_units ();
14300
5c631832 14301 return retval;
348e048f
DE
14302}
14303
8a9b8146
TT
14304/* Return the type of the DIE at DIE_OFFSET in the CU named by
14305 PER_CU. */
14306
14307struct type *
14308dwarf2_get_die_type (unsigned int die_offset,
14309 struct dwarf2_per_cu_data *per_cu)
14310{
8a9b8146 14311 dw2_setup (per_cu->objfile);
9ff3b74f 14312 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14313}
14314
348e048f
DE
14315/* Follow the signature attribute ATTR in SRC_DIE.
14316 On entry *REF_CU is the CU of SRC_DIE.
14317 On exit *REF_CU is the CU of the result. */
14318
14319static struct die_info *
14320follow_die_sig (struct die_info *src_die, struct attribute *attr,
14321 struct dwarf2_cu **ref_cu)
14322{
14323 struct objfile *objfile = (*ref_cu)->objfile;
14324 struct die_info temp_die;
14325 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14326 struct dwarf2_cu *sig_cu;
14327 struct die_info *die;
14328
14329 /* sig_type will be NULL if the signatured type is missing from
14330 the debug info. */
14331 if (sig_type == NULL)
14332 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14333 "at 0x%x [in module %s]"),
14334 src_die->offset, objfile->name);
14335
14336 /* If necessary, add it to the queue and load its DIEs. */
14337
14338 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14339 read_signatured_type (objfile, sig_type);
14340
14341 gdb_assert (sig_type->per_cu.cu != NULL);
14342
14343 sig_cu = sig_type->per_cu.cu;
14344 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14345 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14346 if (die)
14347 {
14348 *ref_cu = sig_cu;
14349 return die;
14350 }
14351
3e43a32a
MS
14352 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14353 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14354 sig_type->type_offset, src_die->offset, objfile->name);
14355}
14356
14357/* Given an offset of a signatured type, return its signatured_type. */
14358
14359static struct signatured_type *
8b70b953
TT
14360lookup_signatured_type_at_offset (struct objfile *objfile,
14361 struct dwarf2_section_info *section,
14362 unsigned int offset)
348e048f 14363{
8b70b953 14364 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14365 unsigned int length, initial_length_size;
14366 unsigned int sig_offset;
14367 struct signatured_type find_entry, *type_sig;
14368
14369 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14370 sig_offset = (initial_length_size
14371 + 2 /*version*/
14372 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14373 + 1 /*address_size*/);
14374 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14375 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14376
14377 /* This is only used to lookup previously recorded types.
14378 If we didn't find it, it's our bug. */
14379 gdb_assert (type_sig != NULL);
b3c8eb43 14380 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14381
14382 return type_sig;
14383}
14384
14385/* Read in signatured type at OFFSET and build its CU and die(s). */
14386
14387static void
14388read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14389 struct dwarf2_section_info *sect,
348e048f
DE
14390 unsigned int offset)
14391{
14392 struct signatured_type *type_sig;
14393
8b70b953 14394 dwarf2_read_section (objfile, sect);
be391dca 14395
348e048f
DE
14396 /* We have the section offset, but we need the signature to do the
14397 hash table lookup. */
8b70b953 14398 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14399
14400 gdb_assert (type_sig->per_cu.cu == NULL);
14401
14402 read_signatured_type (objfile, type_sig);
14403
14404 gdb_assert (type_sig->per_cu.cu != NULL);
14405}
14406
14407/* Read in a signatured type and build its CU and DIEs. */
14408
14409static void
14410read_signatured_type (struct objfile *objfile,
14411 struct signatured_type *type_sig)
14412{
1fd400ff 14413 gdb_byte *types_ptr;
348e048f
DE
14414 struct die_reader_specs reader_specs;
14415 struct dwarf2_cu *cu;
14416 ULONGEST signature;
14417 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14418 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14419
8b70b953
TT
14420 dwarf2_read_section (objfile, section);
14421 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14422
348e048f
DE
14423 gdb_assert (type_sig->per_cu.cu == NULL);
14424
9816fde3
JK
14425 cu = xmalloc (sizeof (*cu));
14426 init_one_comp_unit (cu, objfile);
14427
348e048f
DE
14428 type_sig->per_cu.cu = cu;
14429 cu->per_cu = &type_sig->per_cu;
14430
14431 /* If an error occurs while loading, release our storage. */
14432 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14433
8b70b953 14434 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14435 types_ptr, objfile->obfd);
14436 gdb_assert (signature == type_sig->signature);
14437
14438 cu->die_hash
14439 = htab_create_alloc_ex (cu->header.length / 12,
14440 die_hash,
14441 die_eq,
14442 NULL,
14443 &cu->comp_unit_obstack,
14444 hashtab_obstack_allocate,
14445 dummy_obstack_deallocate);
14446
14447 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14448 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14449
14450 init_cu_die_reader (&reader_specs, cu);
14451
14452 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14453 NULL /*parent*/);
14454
14455 /* We try not to read any attributes in this function, because not
9cdd5dbd 14456 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14457 table processing isn't initialized. But we have to set the CU language,
14458 or we won't be able to build types correctly. */
9816fde3 14459 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14460
14461 do_cleanups (back_to);
14462
14463 /* We've successfully allocated this compilation unit. Let our caller
14464 clean it up when finished with it. */
14465 discard_cleanups (free_cu_cleanup);
14466
14467 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14468 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14469}
14470
c906108c
SS
14471/* Decode simple location descriptions.
14472 Given a pointer to a dwarf block that defines a location, compute
14473 the location and return the value.
14474
4cecd739
DJ
14475 NOTE drow/2003-11-18: This function is called in two situations
14476 now: for the address of static or global variables (partial symbols
14477 only) and for offsets into structures which are expected to be
14478 (more or less) constant. The partial symbol case should go away,
14479 and only the constant case should remain. That will let this
14480 function complain more accurately. A few special modes are allowed
14481 without complaint for global variables (for instance, global
14482 register values and thread-local values).
c906108c
SS
14483
14484 A location description containing no operations indicates that the
4cecd739 14485 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14486 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14487 callers will only want a very basic result and this can become a
21ae7a4d
JK
14488 complaint.
14489
14490 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14491
14492static CORE_ADDR
e7c27a73 14493decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14494{
e7c27a73 14495 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14496 int i;
14497 int size = blk->size;
14498 gdb_byte *data = blk->data;
14499 CORE_ADDR stack[64];
14500 int stacki;
14501 unsigned int bytes_read, unsnd;
14502 gdb_byte op;
c906108c 14503
21ae7a4d
JK
14504 i = 0;
14505 stacki = 0;
14506 stack[stacki] = 0;
14507 stack[++stacki] = 0;
14508
14509 while (i < size)
14510 {
14511 op = data[i++];
14512 switch (op)
14513 {
14514 case DW_OP_lit0:
14515 case DW_OP_lit1:
14516 case DW_OP_lit2:
14517 case DW_OP_lit3:
14518 case DW_OP_lit4:
14519 case DW_OP_lit5:
14520 case DW_OP_lit6:
14521 case DW_OP_lit7:
14522 case DW_OP_lit8:
14523 case DW_OP_lit9:
14524 case DW_OP_lit10:
14525 case DW_OP_lit11:
14526 case DW_OP_lit12:
14527 case DW_OP_lit13:
14528 case DW_OP_lit14:
14529 case DW_OP_lit15:
14530 case DW_OP_lit16:
14531 case DW_OP_lit17:
14532 case DW_OP_lit18:
14533 case DW_OP_lit19:
14534 case DW_OP_lit20:
14535 case DW_OP_lit21:
14536 case DW_OP_lit22:
14537 case DW_OP_lit23:
14538 case DW_OP_lit24:
14539 case DW_OP_lit25:
14540 case DW_OP_lit26:
14541 case DW_OP_lit27:
14542 case DW_OP_lit28:
14543 case DW_OP_lit29:
14544 case DW_OP_lit30:
14545 case DW_OP_lit31:
14546 stack[++stacki] = op - DW_OP_lit0;
14547 break;
f1bea926 14548
21ae7a4d
JK
14549 case DW_OP_reg0:
14550 case DW_OP_reg1:
14551 case DW_OP_reg2:
14552 case DW_OP_reg3:
14553 case DW_OP_reg4:
14554 case DW_OP_reg5:
14555 case DW_OP_reg6:
14556 case DW_OP_reg7:
14557 case DW_OP_reg8:
14558 case DW_OP_reg9:
14559 case DW_OP_reg10:
14560 case DW_OP_reg11:
14561 case DW_OP_reg12:
14562 case DW_OP_reg13:
14563 case DW_OP_reg14:
14564 case DW_OP_reg15:
14565 case DW_OP_reg16:
14566 case DW_OP_reg17:
14567 case DW_OP_reg18:
14568 case DW_OP_reg19:
14569 case DW_OP_reg20:
14570 case DW_OP_reg21:
14571 case DW_OP_reg22:
14572 case DW_OP_reg23:
14573 case DW_OP_reg24:
14574 case DW_OP_reg25:
14575 case DW_OP_reg26:
14576 case DW_OP_reg27:
14577 case DW_OP_reg28:
14578 case DW_OP_reg29:
14579 case DW_OP_reg30:
14580 case DW_OP_reg31:
14581 stack[++stacki] = op - DW_OP_reg0;
14582 if (i < size)
14583 dwarf2_complex_location_expr_complaint ();
14584 break;
c906108c 14585
21ae7a4d
JK
14586 case DW_OP_regx:
14587 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14588 i += bytes_read;
14589 stack[++stacki] = unsnd;
14590 if (i < size)
14591 dwarf2_complex_location_expr_complaint ();
14592 break;
c906108c 14593
21ae7a4d
JK
14594 case DW_OP_addr:
14595 stack[++stacki] = read_address (objfile->obfd, &data[i],
14596 cu, &bytes_read);
14597 i += bytes_read;
14598 break;
d53d4ac5 14599
21ae7a4d
JK
14600 case DW_OP_const1u:
14601 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14602 i += 1;
14603 break;
14604
14605 case DW_OP_const1s:
14606 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14607 i += 1;
14608 break;
14609
14610 case DW_OP_const2u:
14611 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14612 i += 2;
14613 break;
14614
14615 case DW_OP_const2s:
14616 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14617 i += 2;
14618 break;
d53d4ac5 14619
21ae7a4d
JK
14620 case DW_OP_const4u:
14621 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14622 i += 4;
14623 break;
14624
14625 case DW_OP_const4s:
14626 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14627 i += 4;
14628 break;
14629
585861ea
JK
14630 case DW_OP_const8u:
14631 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14632 i += 8;
14633 break;
14634
21ae7a4d
JK
14635 case DW_OP_constu:
14636 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14637 &bytes_read);
14638 i += bytes_read;
14639 break;
14640
14641 case DW_OP_consts:
14642 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14643 i += bytes_read;
14644 break;
14645
14646 case DW_OP_dup:
14647 stack[stacki + 1] = stack[stacki];
14648 stacki++;
14649 break;
14650
14651 case DW_OP_plus:
14652 stack[stacki - 1] += stack[stacki];
14653 stacki--;
14654 break;
14655
14656 case DW_OP_plus_uconst:
14657 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14658 &bytes_read);
14659 i += bytes_read;
14660 break;
14661
14662 case DW_OP_minus:
14663 stack[stacki - 1] -= stack[stacki];
14664 stacki--;
14665 break;
14666
14667 case DW_OP_deref:
14668 /* If we're not the last op, then we definitely can't encode
14669 this using GDB's address_class enum. This is valid for partial
14670 global symbols, although the variable's address will be bogus
14671 in the psymtab. */
14672 if (i < size)
14673 dwarf2_complex_location_expr_complaint ();
14674 break;
14675
14676 case DW_OP_GNU_push_tls_address:
14677 /* The top of the stack has the offset from the beginning
14678 of the thread control block at which the variable is located. */
14679 /* Nothing should follow this operator, so the top of stack would
14680 be returned. */
14681 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14682 address will be bogus in the psymtab. Make it always at least
14683 non-zero to not look as a variable garbage collected by linker
14684 which have DW_OP_addr 0. */
21ae7a4d
JK
14685 if (i < size)
14686 dwarf2_complex_location_expr_complaint ();
585861ea 14687 stack[stacki]++;
21ae7a4d
JK
14688 break;
14689
14690 case DW_OP_GNU_uninit:
14691 break;
14692
14693 default:
14694 {
14695 const char *name = dwarf_stack_op_name (op);
14696
14697 if (name)
14698 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14699 name);
14700 else
14701 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14702 op);
14703 }
14704
14705 return (stack[stacki]);
d53d4ac5 14706 }
3c6e0cb3 14707
21ae7a4d
JK
14708 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14709 outside of the allocated space. Also enforce minimum>0. */
14710 if (stacki >= ARRAY_SIZE (stack) - 1)
14711 {
14712 complaint (&symfile_complaints,
14713 _("location description stack overflow"));
14714 return 0;
14715 }
14716
14717 if (stacki <= 0)
14718 {
14719 complaint (&symfile_complaints,
14720 _("location description stack underflow"));
14721 return 0;
14722 }
14723 }
14724 return (stack[stacki]);
c906108c
SS
14725}
14726
14727/* memory allocation interface */
14728
c906108c 14729static struct dwarf_block *
7b5a2f43 14730dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14731{
14732 struct dwarf_block *blk;
14733
14734 blk = (struct dwarf_block *)
7b5a2f43 14735 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14736 return (blk);
14737}
14738
14739static struct abbrev_info *
f3dd6933 14740dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14741{
14742 struct abbrev_info *abbrev;
14743
f3dd6933
DJ
14744 abbrev = (struct abbrev_info *)
14745 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14746 memset (abbrev, 0, sizeof (struct abbrev_info));
14747 return (abbrev);
14748}
14749
14750static struct die_info *
b60c80d6 14751dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14752{
14753 struct die_info *die;
b60c80d6
DJ
14754 size_t size = sizeof (struct die_info);
14755
14756 if (num_attrs > 1)
14757 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14758
b60c80d6 14759 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14760 memset (die, 0, sizeof (struct die_info));
14761 return (die);
14762}
2e276125
JB
14763
14764\f
14765/* Macro support. */
14766
2e276125
JB
14767/* Return the full name of file number I in *LH's file name table.
14768 Use COMP_DIR as the name of the current directory of the
14769 compilation. The result is allocated using xmalloc; the caller is
14770 responsible for freeing it. */
14771static char *
14772file_full_name (int file, struct line_header *lh, const char *comp_dir)
14773{
6a83a1e6
EZ
14774 /* Is the file number a valid index into the line header's file name
14775 table? Remember that file numbers start with one, not zero. */
14776 if (1 <= file && file <= lh->num_file_names)
14777 {
14778 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14779
6a83a1e6
EZ
14780 if (IS_ABSOLUTE_PATH (fe->name))
14781 return xstrdup (fe->name);
14782 else
14783 {
14784 const char *dir;
14785 int dir_len;
14786 char *full_name;
14787
14788 if (fe->dir_index)
14789 dir = lh->include_dirs[fe->dir_index - 1];
14790 else
14791 dir = comp_dir;
14792
14793 if (dir)
14794 {
14795 dir_len = strlen (dir);
14796 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14797 strcpy (full_name, dir);
14798 full_name[dir_len] = '/';
14799 strcpy (full_name + dir_len + 1, fe->name);
14800 return full_name;
14801 }
14802 else
14803 return xstrdup (fe->name);
14804 }
14805 }
2e276125
JB
14806 else
14807 {
6a83a1e6
EZ
14808 /* The compiler produced a bogus file number. We can at least
14809 record the macro definitions made in the file, even if we
14810 won't be able to find the file by name. */
14811 char fake_name[80];
9a619af0 14812
6a83a1e6 14813 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14814
6e70227d 14815 complaint (&symfile_complaints,
6a83a1e6
EZ
14816 _("bad file number in macro information (%d)"),
14817 file);
2e276125 14818
6a83a1e6 14819 return xstrdup (fake_name);
2e276125
JB
14820 }
14821}
14822
14823
14824static struct macro_source_file *
14825macro_start_file (int file, int line,
14826 struct macro_source_file *current_file,
14827 const char *comp_dir,
14828 struct line_header *lh, struct objfile *objfile)
14829{
14830 /* The full name of this source file. */
14831 char *full_name = file_full_name (file, lh, comp_dir);
14832
14833 /* We don't create a macro table for this compilation unit
14834 at all until we actually get a filename. */
14835 if (! pending_macros)
4a146b47 14836 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14837 objfile->macro_cache);
2e276125
JB
14838
14839 if (! current_file)
14840 /* If we have no current file, then this must be the start_file
14841 directive for the compilation unit's main source file. */
14842 current_file = macro_set_main (pending_macros, full_name);
14843 else
14844 current_file = macro_include (current_file, line, full_name);
14845
14846 xfree (full_name);
6e70227d 14847
2e276125
JB
14848 return current_file;
14849}
14850
14851
14852/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14853 followed by a null byte. */
14854static char *
14855copy_string (const char *buf, int len)
14856{
14857 char *s = xmalloc (len + 1);
9a619af0 14858
2e276125
JB
14859 memcpy (s, buf, len);
14860 s[len] = '\0';
2e276125
JB
14861 return s;
14862}
14863
14864
14865static const char *
14866consume_improper_spaces (const char *p, const char *body)
14867{
14868 if (*p == ' ')
14869 {
4d3c2250 14870 complaint (&symfile_complaints,
3e43a32a
MS
14871 _("macro definition contains spaces "
14872 "in formal argument list:\n`%s'"),
4d3c2250 14873 body);
2e276125
JB
14874
14875 while (*p == ' ')
14876 p++;
14877 }
14878
14879 return p;
14880}
14881
14882
14883static void
14884parse_macro_definition (struct macro_source_file *file, int line,
14885 const char *body)
14886{
14887 const char *p;
14888
14889 /* The body string takes one of two forms. For object-like macro
14890 definitions, it should be:
14891
14892 <macro name> " " <definition>
14893
14894 For function-like macro definitions, it should be:
14895
14896 <macro name> "() " <definition>
14897 or
14898 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14899
14900 Spaces may appear only where explicitly indicated, and in the
14901 <definition>.
14902
14903 The Dwarf 2 spec says that an object-like macro's name is always
14904 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14905 the space when the macro's definition is the empty string.
2e276125
JB
14906
14907 The Dwarf 2 spec says that there should be no spaces between the
14908 formal arguments in a function-like macro's formal argument list,
14909 but versions of GCC around March 2002 include spaces after the
14910 commas. */
14911
14912
14913 /* Find the extent of the macro name. The macro name is terminated
14914 by either a space or null character (for an object-like macro) or
14915 an opening paren (for a function-like macro). */
14916 for (p = body; *p; p++)
14917 if (*p == ' ' || *p == '(')
14918 break;
14919
14920 if (*p == ' ' || *p == '\0')
14921 {
14922 /* It's an object-like macro. */
14923 int name_len = p - body;
14924 char *name = copy_string (body, name_len);
14925 const char *replacement;
14926
14927 if (*p == ' ')
14928 replacement = body + name_len + 1;
14929 else
14930 {
4d3c2250 14931 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14932 replacement = body + name_len;
14933 }
6e70227d 14934
2e276125
JB
14935 macro_define_object (file, line, name, replacement);
14936
14937 xfree (name);
14938 }
14939 else if (*p == '(')
14940 {
14941 /* It's a function-like macro. */
14942 char *name = copy_string (body, p - body);
14943 int argc = 0;
14944 int argv_size = 1;
14945 char **argv = xmalloc (argv_size * sizeof (*argv));
14946
14947 p++;
14948
14949 p = consume_improper_spaces (p, body);
14950
14951 /* Parse the formal argument list. */
14952 while (*p && *p != ')')
14953 {
14954 /* Find the extent of the current argument name. */
14955 const char *arg_start = p;
14956
14957 while (*p && *p != ',' && *p != ')' && *p != ' ')
14958 p++;
14959
14960 if (! *p || p == arg_start)
4d3c2250 14961 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14962 else
14963 {
14964 /* Make sure argv has room for the new argument. */
14965 if (argc >= argv_size)
14966 {
14967 argv_size *= 2;
14968 argv = xrealloc (argv, argv_size * sizeof (*argv));
14969 }
14970
14971 argv[argc++] = copy_string (arg_start, p - arg_start);
14972 }
14973
14974 p = consume_improper_spaces (p, body);
14975
14976 /* Consume the comma, if present. */
14977 if (*p == ',')
14978 {
14979 p++;
14980
14981 p = consume_improper_spaces (p, body);
14982 }
14983 }
14984
14985 if (*p == ')')
14986 {
14987 p++;
14988
14989 if (*p == ' ')
14990 /* Perfectly formed definition, no complaints. */
14991 macro_define_function (file, line, name,
6e70227d 14992 argc, (const char **) argv,
2e276125
JB
14993 p + 1);
14994 else if (*p == '\0')
14995 {
14996 /* Complain, but do define it. */
4d3c2250 14997 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14998 macro_define_function (file, line, name,
6e70227d 14999 argc, (const char **) argv,
2e276125
JB
15000 p);
15001 }
15002 else
15003 /* Just complain. */
4d3c2250 15004 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15005 }
15006 else
15007 /* Just complain. */
4d3c2250 15008 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15009
15010 xfree (name);
15011 {
15012 int i;
15013
15014 for (i = 0; i < argc; i++)
15015 xfree (argv[i]);
15016 }
15017 xfree (argv);
15018 }
15019 else
4d3c2250 15020 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15021}
15022
cf2c3c16
TT
15023/* Skip some bytes from BYTES according to the form given in FORM.
15024 Returns the new pointer. */
2e276125 15025
cf2c3c16
TT
15026static gdb_byte *
15027skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15028 enum dwarf_form form,
15029 unsigned int offset_size,
15030 struct dwarf2_section_info *section)
2e276125 15031{
cf2c3c16 15032 unsigned int bytes_read;
2e276125 15033
cf2c3c16 15034 switch (form)
2e276125 15035 {
cf2c3c16
TT
15036 case DW_FORM_data1:
15037 case DW_FORM_flag:
15038 ++bytes;
15039 break;
15040
15041 case DW_FORM_data2:
15042 bytes += 2;
15043 break;
15044
15045 case DW_FORM_data4:
15046 bytes += 4;
15047 break;
15048
15049 case DW_FORM_data8:
15050 bytes += 8;
15051 break;
15052
15053 case DW_FORM_string:
15054 read_direct_string (abfd, bytes, &bytes_read);
15055 bytes += bytes_read;
15056 break;
15057
15058 case DW_FORM_sec_offset:
15059 case DW_FORM_strp:
15060 bytes += offset_size;
15061 break;
15062
15063 case DW_FORM_block:
15064 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15065 bytes += bytes_read;
15066 break;
15067
15068 case DW_FORM_block1:
15069 bytes += 1 + read_1_byte (abfd, bytes);
15070 break;
15071 case DW_FORM_block2:
15072 bytes += 2 + read_2_bytes (abfd, bytes);
15073 break;
15074 case DW_FORM_block4:
15075 bytes += 4 + read_4_bytes (abfd, bytes);
15076 break;
15077
15078 case DW_FORM_sdata:
15079 case DW_FORM_udata:
15080 bytes = skip_leb128 (abfd, bytes);
15081 break;
15082
15083 default:
15084 {
15085 complain:
15086 complaint (&symfile_complaints,
15087 _("invalid form 0x%x in `%s'"),
15088 form,
15089 section->asection->name);
15090 return NULL;
15091 }
2e276125
JB
15092 }
15093
cf2c3c16
TT
15094 return bytes;
15095}
757a13d0 15096
cf2c3c16
TT
15097/* A helper for dwarf_decode_macros that handles skipping an unknown
15098 opcode. Returns an updated pointer to the macro data buffer; or,
15099 on error, issues a complaint and returns NULL. */
757a13d0 15100
cf2c3c16
TT
15101static gdb_byte *
15102skip_unknown_opcode (unsigned int opcode,
15103 gdb_byte **opcode_definitions,
15104 gdb_byte *mac_ptr,
15105 bfd *abfd,
15106 unsigned int offset_size,
15107 struct dwarf2_section_info *section)
15108{
15109 unsigned int bytes_read, i;
15110 unsigned long arg;
15111 gdb_byte *defn;
2e276125 15112
cf2c3c16 15113 if (opcode_definitions[opcode] == NULL)
2e276125 15114 {
cf2c3c16
TT
15115 complaint (&symfile_complaints,
15116 _("unrecognized DW_MACFINO opcode 0x%x"),
15117 opcode);
15118 return NULL;
15119 }
2e276125 15120
cf2c3c16
TT
15121 defn = opcode_definitions[opcode];
15122 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15123 defn += bytes_read;
2e276125 15124
cf2c3c16
TT
15125 for (i = 0; i < arg; ++i)
15126 {
15127 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15128 if (mac_ptr == NULL)
15129 {
15130 /* skip_form_bytes already issued the complaint. */
15131 return NULL;
15132 }
15133 }
757a13d0 15134
cf2c3c16
TT
15135 return mac_ptr;
15136}
757a13d0 15137
cf2c3c16
TT
15138/* A helper function which parses the header of a macro section.
15139 If the macro section is the extended (for now called "GNU") type,
15140 then this updates *OFFSET_SIZE. Returns a pointer to just after
15141 the header, or issues a complaint and returns NULL on error. */
757a13d0 15142
cf2c3c16
TT
15143static gdb_byte *
15144dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15145 bfd *abfd,
15146 gdb_byte *mac_ptr,
15147 unsigned int *offset_size,
15148 int section_is_gnu)
15149{
15150 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15151
cf2c3c16
TT
15152 if (section_is_gnu)
15153 {
15154 unsigned int version, flags;
757a13d0 15155
cf2c3c16
TT
15156 version = read_2_bytes (abfd, mac_ptr);
15157 if (version != 4)
15158 {
15159 complaint (&symfile_complaints,
15160 _("unrecognized version `%d' in .debug_macro section"),
15161 version);
15162 return NULL;
15163 }
15164 mac_ptr += 2;
757a13d0 15165
cf2c3c16
TT
15166 flags = read_1_byte (abfd, mac_ptr);
15167 ++mac_ptr;
15168 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15169
cf2c3c16
TT
15170 if ((flags & 2) != 0)
15171 /* We don't need the line table offset. */
15172 mac_ptr += *offset_size;
757a13d0 15173
cf2c3c16
TT
15174 /* Vendor opcode descriptions. */
15175 if ((flags & 4) != 0)
15176 {
15177 unsigned int i, count;
757a13d0 15178
cf2c3c16
TT
15179 count = read_1_byte (abfd, mac_ptr);
15180 ++mac_ptr;
15181 for (i = 0; i < count; ++i)
15182 {
15183 unsigned int opcode, bytes_read;
15184 unsigned long arg;
15185
15186 opcode = read_1_byte (abfd, mac_ptr);
15187 ++mac_ptr;
15188 opcode_definitions[opcode] = mac_ptr;
15189 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15190 mac_ptr += bytes_read;
15191 mac_ptr += arg;
15192 }
757a13d0 15193 }
cf2c3c16 15194 }
757a13d0 15195
cf2c3c16
TT
15196 return mac_ptr;
15197}
757a13d0 15198
cf2c3c16
TT
15199/* A helper for dwarf_decode_macros that handles the GNU extensions,
15200 including DW_GNU_MACINFO_transparent_include. */
15201
15202static void
15203dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15204 struct macro_source_file *current_file,
15205 struct line_header *lh, char *comp_dir,
15206 struct dwarf2_section_info *section,
15207 int section_is_gnu,
15208 unsigned int offset_size,
15209 struct objfile *objfile)
15210{
15211 enum dwarf_macro_record_type macinfo_type;
15212 int at_commandline;
15213 gdb_byte *opcode_definitions[256];
757a13d0 15214
cf2c3c16
TT
15215 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15216 &offset_size, section_is_gnu);
15217 if (mac_ptr == NULL)
15218 {
15219 /* We already issued a complaint. */
15220 return;
15221 }
757a13d0
JK
15222
15223 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15224 GDB is still reading the definitions from command line. First
15225 DW_MACINFO_start_file will need to be ignored as it was already executed
15226 to create CURRENT_FILE for the main source holding also the command line
15227 definitions. On first met DW_MACINFO_start_file this flag is reset to
15228 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15229
15230 at_commandline = 1;
15231
15232 do
15233 {
15234 /* Do we at least have room for a macinfo type byte? */
15235 if (mac_ptr >= mac_end)
15236 {
cf2c3c16 15237 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15238 break;
15239 }
15240
15241 macinfo_type = read_1_byte (abfd, mac_ptr);
15242 mac_ptr++;
15243
cf2c3c16
TT
15244 /* Note that we rely on the fact that the corresponding GNU and
15245 DWARF constants are the same. */
757a13d0
JK
15246 switch (macinfo_type)
15247 {
15248 /* A zero macinfo type indicates the end of the macro
15249 information. */
15250 case 0:
15251 break;
2e276125 15252
cf2c3c16
TT
15253 case DW_MACRO_GNU_define:
15254 case DW_MACRO_GNU_undef:
15255 case DW_MACRO_GNU_define_indirect:
15256 case DW_MACRO_GNU_undef_indirect:
2e276125 15257 {
891d2f0b 15258 unsigned int bytes_read;
2e276125
JB
15259 int line;
15260 char *body;
cf2c3c16 15261 int is_define;
2e276125 15262
cf2c3c16
TT
15263 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15264 mac_ptr += bytes_read;
15265
15266 if (macinfo_type == DW_MACRO_GNU_define
15267 || macinfo_type == DW_MACRO_GNU_undef)
15268 {
15269 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15270 mac_ptr += bytes_read;
15271 }
15272 else
15273 {
15274 LONGEST str_offset;
15275
15276 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15277 mac_ptr += offset_size;
2e276125 15278
cf2c3c16
TT
15279 body = read_indirect_string_at_offset (abfd, str_offset);
15280 }
15281
15282 is_define = (macinfo_type == DW_MACRO_GNU_define
15283 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15284 if (! current_file)
757a13d0
JK
15285 {
15286 /* DWARF violation as no main source is present. */
15287 complaint (&symfile_complaints,
15288 _("debug info with no main source gives macro %s "
15289 "on line %d: %s"),
cf2c3c16
TT
15290 is_define ? _("definition") : _("undefinition"),
15291 line, body);
757a13d0
JK
15292 break;
15293 }
3e43a32a
MS
15294 if ((line == 0 && !at_commandline)
15295 || (line != 0 && at_commandline))
4d3c2250 15296 complaint (&symfile_complaints,
757a13d0
JK
15297 _("debug info gives %s macro %s with %s line %d: %s"),
15298 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15299 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15300 line == 0 ? _("zero") : _("non-zero"), line, body);
15301
cf2c3c16 15302 if (is_define)
757a13d0 15303 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15304 else
15305 {
15306 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15307 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15308 macro_undef (current_file, line, body);
15309 }
2e276125
JB
15310 }
15311 break;
15312
cf2c3c16 15313 case DW_MACRO_GNU_start_file:
2e276125 15314 {
891d2f0b 15315 unsigned int bytes_read;
2e276125
JB
15316 int line, file;
15317
15318 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15319 mac_ptr += bytes_read;
15320 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15321 mac_ptr += bytes_read;
15322
3e43a32a
MS
15323 if ((line == 0 && !at_commandline)
15324 || (line != 0 && at_commandline))
757a13d0
JK
15325 complaint (&symfile_complaints,
15326 _("debug info gives source %d included "
15327 "from %s at %s line %d"),
15328 file, at_commandline ? _("command-line") : _("file"),
15329 line == 0 ? _("zero") : _("non-zero"), line);
15330
15331 if (at_commandline)
15332 {
cf2c3c16
TT
15333 /* This DW_MACRO_GNU_start_file was executed in the
15334 pass one. */
757a13d0
JK
15335 at_commandline = 0;
15336 }
15337 else
15338 current_file = macro_start_file (file, line,
15339 current_file, comp_dir,
cf2c3c16 15340 lh, objfile);
2e276125
JB
15341 }
15342 break;
15343
cf2c3c16 15344 case DW_MACRO_GNU_end_file:
2e276125 15345 if (! current_file)
4d3c2250 15346 complaint (&symfile_complaints,
3e43a32a
MS
15347 _("macro debug info has an unmatched "
15348 "`close_file' directive"));
2e276125
JB
15349 else
15350 {
15351 current_file = current_file->included_by;
15352 if (! current_file)
15353 {
cf2c3c16 15354 enum dwarf_macro_record_type next_type;
2e276125
JB
15355
15356 /* GCC circa March 2002 doesn't produce the zero
15357 type byte marking the end of the compilation
15358 unit. Complain if it's not there, but exit no
15359 matter what. */
15360
15361 /* Do we at least have room for a macinfo type byte? */
15362 if (mac_ptr >= mac_end)
15363 {
cf2c3c16 15364 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15365 return;
15366 }
15367
15368 /* We don't increment mac_ptr here, so this is just
15369 a look-ahead. */
15370 next_type = read_1_byte (abfd, mac_ptr);
15371 if (next_type != 0)
4d3c2250 15372 complaint (&symfile_complaints,
3e43a32a
MS
15373 _("no terminating 0-type entry for "
15374 "macros in `.debug_macinfo' section"));
2e276125
JB
15375
15376 return;
15377 }
15378 }
15379 break;
15380
cf2c3c16
TT
15381 case DW_MACRO_GNU_transparent_include:
15382 {
15383 LONGEST offset;
15384
15385 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15386 mac_ptr += offset_size;
15387
15388 dwarf_decode_macro_bytes (abfd,
15389 section->buffer + offset,
15390 mac_end, current_file,
15391 lh, comp_dir,
15392 section, section_is_gnu,
15393 offset_size, objfile);
15394 }
15395 break;
15396
2e276125 15397 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15398 if (!section_is_gnu)
15399 {
15400 unsigned int bytes_read;
15401 int constant;
2e276125 15402
cf2c3c16
TT
15403 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15404 mac_ptr += bytes_read;
15405 read_direct_string (abfd, mac_ptr, &bytes_read);
15406 mac_ptr += bytes_read;
2e276125 15407
cf2c3c16
TT
15408 /* We don't recognize any vendor extensions. */
15409 break;
15410 }
15411 /* FALLTHROUGH */
15412
15413 default:
15414 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15415 mac_ptr, abfd, offset_size,
15416 section);
15417 if (mac_ptr == NULL)
15418 return;
15419 break;
2e276125 15420 }
757a13d0 15421 } while (macinfo_type != 0);
2e276125 15422}
8e19ed76 15423
cf2c3c16
TT
15424static void
15425dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15426 char *comp_dir, bfd *abfd,
15427 struct dwarf2_cu *cu,
15428 struct dwarf2_section_info *section,
15429 int section_is_gnu)
15430{
15431 gdb_byte *mac_ptr, *mac_end;
15432 struct macro_source_file *current_file = 0;
15433 enum dwarf_macro_record_type macinfo_type;
15434 unsigned int offset_size = cu->header.offset_size;
15435 gdb_byte *opcode_definitions[256];
15436
15437 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15438 if (section->buffer == NULL)
15439 {
15440 complaint (&symfile_complaints, _("missing %s section"),
15441 section->asection->name);
15442 return;
15443 }
15444
15445 /* First pass: Find the name of the base filename.
15446 This filename is needed in order to process all macros whose definition
15447 (or undefinition) comes from the command line. These macros are defined
15448 before the first DW_MACINFO_start_file entry, and yet still need to be
15449 associated to the base file.
15450
15451 To determine the base file name, we scan the macro definitions until we
15452 reach the first DW_MACINFO_start_file entry. We then initialize
15453 CURRENT_FILE accordingly so that any macro definition found before the
15454 first DW_MACINFO_start_file can still be associated to the base file. */
15455
15456 mac_ptr = section->buffer + offset;
15457 mac_end = section->buffer + section->size;
15458
15459 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15460 &offset_size, section_is_gnu);
15461 if (mac_ptr == NULL)
15462 {
15463 /* We already issued a complaint. */
15464 return;
15465 }
15466
15467 do
15468 {
15469 /* Do we at least have room for a macinfo type byte? */
15470 if (mac_ptr >= mac_end)
15471 {
15472 /* Complaint is printed during the second pass as GDB will probably
15473 stop the first pass earlier upon finding
15474 DW_MACINFO_start_file. */
15475 break;
15476 }
15477
15478 macinfo_type = read_1_byte (abfd, mac_ptr);
15479 mac_ptr++;
15480
15481 /* Note that we rely on the fact that the corresponding GNU and
15482 DWARF constants are the same. */
15483 switch (macinfo_type)
15484 {
15485 /* A zero macinfo type indicates the end of the macro
15486 information. */
15487 case 0:
15488 break;
15489
15490 case DW_MACRO_GNU_define:
15491 case DW_MACRO_GNU_undef:
15492 /* Only skip the data by MAC_PTR. */
15493 {
15494 unsigned int bytes_read;
15495
15496 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15497 mac_ptr += bytes_read;
15498 read_direct_string (abfd, mac_ptr, &bytes_read);
15499 mac_ptr += bytes_read;
15500 }
15501 break;
15502
15503 case DW_MACRO_GNU_start_file:
15504 {
15505 unsigned int bytes_read;
15506 int line, file;
15507
15508 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15509 mac_ptr += bytes_read;
15510 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15511 mac_ptr += bytes_read;
15512
15513 current_file = macro_start_file (file, line, current_file,
15514 comp_dir, lh, cu->objfile);
15515 }
15516 break;
15517
15518 case DW_MACRO_GNU_end_file:
15519 /* No data to skip by MAC_PTR. */
15520 break;
15521
15522 case DW_MACRO_GNU_define_indirect:
15523 case DW_MACRO_GNU_undef_indirect:
15524 {
15525 unsigned int bytes_read;
15526
15527 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15528 mac_ptr += bytes_read;
15529 mac_ptr += offset_size;
15530 }
15531 break;
15532
15533 case DW_MACRO_GNU_transparent_include:
15534 /* Note that, according to the spec, a transparent include
15535 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15536 skip this opcode. */
15537 mac_ptr += offset_size;
15538 break;
15539
15540 case DW_MACINFO_vendor_ext:
15541 /* Only skip the data by MAC_PTR. */
15542 if (!section_is_gnu)
15543 {
15544 unsigned int bytes_read;
15545
15546 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15547 mac_ptr += bytes_read;
15548 read_direct_string (abfd, mac_ptr, &bytes_read);
15549 mac_ptr += bytes_read;
15550 }
15551 /* FALLTHROUGH */
15552
15553 default:
15554 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15555 mac_ptr, abfd, offset_size,
15556 section);
15557 if (mac_ptr == NULL)
15558 return;
15559 break;
15560 }
15561 } while (macinfo_type != 0 && current_file == NULL);
15562
15563 /* Second pass: Process all entries.
15564
15565 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15566 command-line macro definitions/undefinitions. This flag is unset when we
15567 reach the first DW_MACINFO_start_file entry. */
15568
15569 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15570 current_file, lh, comp_dir, section, section_is_gnu,
15571 offset_size, cu->objfile);
15572}
15573
8e19ed76 15574/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15575 if so return true else false. */
8e19ed76
PS
15576static int
15577attr_form_is_block (struct attribute *attr)
15578{
15579 return (attr == NULL ? 0 :
15580 attr->form == DW_FORM_block1
15581 || attr->form == DW_FORM_block2
15582 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15583 || attr->form == DW_FORM_block
15584 || attr->form == DW_FORM_exprloc);
8e19ed76 15585}
4c2df51b 15586
c6a0999f
JB
15587/* Return non-zero if ATTR's value is a section offset --- classes
15588 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15589 You may use DW_UNSND (attr) to retrieve such offsets.
15590
15591 Section 7.5.4, "Attribute Encodings", explains that no attribute
15592 may have a value that belongs to more than one of these classes; it
15593 would be ambiguous if we did, because we use the same forms for all
15594 of them. */
3690dd37
JB
15595static int
15596attr_form_is_section_offset (struct attribute *attr)
15597{
15598 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15599 || attr->form == DW_FORM_data8
15600 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15601}
15602
15603
15604/* Return non-zero if ATTR's value falls in the 'constant' class, or
15605 zero otherwise. When this function returns true, you can apply
15606 dwarf2_get_attr_constant_value to it.
15607
15608 However, note that for some attributes you must check
15609 attr_form_is_section_offset before using this test. DW_FORM_data4
15610 and DW_FORM_data8 are members of both the constant class, and of
15611 the classes that contain offsets into other debug sections
15612 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15613 that, if an attribute's can be either a constant or one of the
15614 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15615 taken as section offsets, not constants. */
15616static int
15617attr_form_is_constant (struct attribute *attr)
15618{
15619 switch (attr->form)
15620 {
15621 case DW_FORM_sdata:
15622 case DW_FORM_udata:
15623 case DW_FORM_data1:
15624 case DW_FORM_data2:
15625 case DW_FORM_data4:
15626 case DW_FORM_data8:
15627 return 1;
15628 default:
15629 return 0;
15630 }
15631}
15632
8cf6f0b1
TT
15633/* A helper function that fills in a dwarf2_loclist_baton. */
15634
15635static void
15636fill_in_loclist_baton (struct dwarf2_cu *cu,
15637 struct dwarf2_loclist_baton *baton,
15638 struct attribute *attr)
15639{
15640 dwarf2_read_section (dwarf2_per_objfile->objfile,
15641 &dwarf2_per_objfile->loc);
15642
15643 baton->per_cu = cu->per_cu;
15644 gdb_assert (baton->per_cu);
15645 /* We don't know how long the location list is, but make sure we
15646 don't run off the edge of the section. */
15647 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15648 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15649 baton->base_address = cu->base_address;
15650}
15651
4c2df51b
DJ
15652static void
15653dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15654 struct dwarf2_cu *cu)
4c2df51b 15655{
3690dd37 15656 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15657 /* ".debug_loc" may not exist at all, or the offset may be outside
15658 the section. If so, fall through to the complaint in the
15659 other branch. */
9e0ac564
TT
15660 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15661 &dwarf2_per_objfile->loc))
4c2df51b 15662 {
0d53c4c4 15663 struct dwarf2_loclist_baton *baton;
4c2df51b 15664
4a146b47 15665 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15666 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15667
8cf6f0b1 15668 fill_in_loclist_baton (cu, baton, attr);
be391dca 15669
d00adf39 15670 if (cu->base_known == 0)
0d53c4c4 15671 complaint (&symfile_complaints,
3e43a32a
MS
15672 _("Location list used without "
15673 "specifying the CU base address."));
4c2df51b 15674
768a979c 15675 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15676 SYMBOL_LOCATION_BATON (sym) = baton;
15677 }
15678 else
15679 {
15680 struct dwarf2_locexpr_baton *baton;
15681
4a146b47 15682 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15683 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15684 baton->per_cu = cu->per_cu;
15685 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15686
15687 if (attr_form_is_block (attr))
15688 {
15689 /* Note that we're just copying the block's data pointer
15690 here, not the actual data. We're still pointing into the
6502dd73
DJ
15691 info_buffer for SYM's objfile; right now we never release
15692 that buffer, but when we do clean up properly this may
15693 need to change. */
0d53c4c4
DJ
15694 baton->size = DW_BLOCK (attr)->size;
15695 baton->data = DW_BLOCK (attr)->data;
15696 }
15697 else
15698 {
15699 dwarf2_invalid_attrib_class_complaint ("location description",
15700 SYMBOL_NATURAL_NAME (sym));
15701 baton->size = 0;
0d53c4c4 15702 }
6e70227d 15703
768a979c 15704 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15705 SYMBOL_LOCATION_BATON (sym) = baton;
15706 }
4c2df51b 15707}
6502dd73 15708
9aa1f1e3
TT
15709/* Return the OBJFILE associated with the compilation unit CU. If CU
15710 came from a separate debuginfo file, then the master objfile is
15711 returned. */
ae0d2f24
UW
15712
15713struct objfile *
15714dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15715{
9291a0cd 15716 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15717
15718 /* Return the master objfile, so that we can report and look up the
15719 correct file containing this variable. */
15720 if (objfile->separate_debug_objfile_backlink)
15721 objfile = objfile->separate_debug_objfile_backlink;
15722
15723 return objfile;
15724}
15725
96408a79
SA
15726/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15727 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15728 CU_HEADERP first. */
15729
15730static const struct comp_unit_head *
15731per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15732 struct dwarf2_per_cu_data *per_cu)
15733{
15734 struct objfile *objfile;
15735 struct dwarf2_per_objfile *per_objfile;
15736 gdb_byte *info_ptr;
15737
15738 if (per_cu->cu)
15739 return &per_cu->cu->header;
15740
15741 objfile = per_cu->objfile;
15742 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15743 info_ptr = per_objfile->info.buffer + per_cu->offset;
15744
15745 memset (cu_headerp, 0, sizeof (*cu_headerp));
15746 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15747
15748 return cu_headerp;
15749}
15750
ae0d2f24
UW
15751/* Return the address size given in the compilation unit header for CU. */
15752
15753CORE_ADDR
15754dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15755{
96408a79
SA
15756 struct comp_unit_head cu_header_local;
15757 const struct comp_unit_head *cu_headerp;
c471e790 15758
96408a79
SA
15759 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15760
15761 return cu_headerp->addr_size;
ae0d2f24
UW
15762}
15763
9eae7c52
TT
15764/* Return the offset size given in the compilation unit header for CU. */
15765
15766int
15767dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15768{
96408a79
SA
15769 struct comp_unit_head cu_header_local;
15770 const struct comp_unit_head *cu_headerp;
9c6c53f7 15771
96408a79
SA
15772 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15773
15774 return cu_headerp->offset_size;
15775}
15776
15777/* See its dwarf2loc.h declaration. */
15778
15779int
15780dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15781{
15782 struct comp_unit_head cu_header_local;
15783 const struct comp_unit_head *cu_headerp;
15784
15785 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15786
15787 if (cu_headerp->version == 2)
15788 return cu_headerp->addr_size;
15789 else
15790 return cu_headerp->offset_size;
181cebd4
JK
15791}
15792
9aa1f1e3
TT
15793/* Return the text offset of the CU. The returned offset comes from
15794 this CU's objfile. If this objfile came from a separate debuginfo
15795 file, then the offset may be different from the corresponding
15796 offset in the parent objfile. */
15797
15798CORE_ADDR
15799dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15800{
bb3fa9d0 15801 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15802
15803 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15804}
15805
348e048f
DE
15806/* Locate the .debug_info compilation unit from CU's objfile which contains
15807 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15808
15809static struct dwarf2_per_cu_data *
c764a876 15810dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15811 struct objfile *objfile)
15812{
15813 struct dwarf2_per_cu_data *this_cu;
15814 int low, high;
15815
ae038cb0
DJ
15816 low = 0;
15817 high = dwarf2_per_objfile->n_comp_units - 1;
15818 while (high > low)
15819 {
15820 int mid = low + (high - low) / 2;
9a619af0 15821
ae038cb0
DJ
15822 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15823 high = mid;
15824 else
15825 low = mid + 1;
15826 }
15827 gdb_assert (low == high);
15828 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15829 {
10b3939b 15830 if (low == 0)
8a3fe4f8
AC
15831 error (_("Dwarf Error: could not find partial DIE containing "
15832 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15833 (long) offset, bfd_get_filename (objfile->obfd));
15834
ae038cb0
DJ
15835 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15836 return dwarf2_per_objfile->all_comp_units[low-1];
15837 }
15838 else
15839 {
15840 this_cu = dwarf2_per_objfile->all_comp_units[low];
15841 if (low == dwarf2_per_objfile->n_comp_units - 1
15842 && offset >= this_cu->offset + this_cu->length)
c764a876 15843 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15844 gdb_assert (offset < this_cu->offset + this_cu->length);
15845 return this_cu;
15846 }
15847}
15848
10b3939b
DJ
15849/* Locate the compilation unit from OBJFILE which is located at exactly
15850 OFFSET. Raises an error on failure. */
15851
ae038cb0 15852static struct dwarf2_per_cu_data *
c764a876 15853dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15854{
15855 struct dwarf2_per_cu_data *this_cu;
9a619af0 15856
ae038cb0
DJ
15857 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15858 if (this_cu->offset != offset)
c764a876 15859 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15860 return this_cu;
15861}
15862
9816fde3 15863/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15864
9816fde3
JK
15865static void
15866init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15867{
9816fde3 15868 memset (cu, 0, sizeof (*cu));
93311388
DE
15869 cu->objfile = objfile;
15870 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15871}
15872
15873/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15874
15875static void
15876prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15877{
15878 struct attribute *attr;
15879
15880 /* Set the language we're debugging. */
15881 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15882 if (attr)
15883 set_cu_language (DW_UNSND (attr), cu);
15884 else
9cded63f
TT
15885 {
15886 cu->language = language_minimal;
15887 cu->language_defn = language_def (cu->language);
15888 }
93311388
DE
15889}
15890
ae038cb0
DJ
15891/* Release one cached compilation unit, CU. We unlink it from the tree
15892 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15893 the caller is responsible for that.
15894 NOTE: DATA is a void * because this function is also used as a
15895 cleanup routine. */
ae038cb0
DJ
15896
15897static void
15898free_one_comp_unit (void *data)
15899{
15900 struct dwarf2_cu *cu = data;
15901
15902 if (cu->per_cu != NULL)
15903 cu->per_cu->cu = NULL;
15904 cu->per_cu = NULL;
15905
15906 obstack_free (&cu->comp_unit_obstack, NULL);
15907
15908 xfree (cu);
15909}
15910
72bf9492 15911/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15912 when we're finished with it. We can't free the pointer itself, but be
15913 sure to unlink it from the cache. Also release any associated storage
15914 and perform cache maintenance.
72bf9492
DJ
15915
15916 Only used during partial symbol parsing. */
15917
15918static void
15919free_stack_comp_unit (void *data)
15920{
15921 struct dwarf2_cu *cu = data;
15922
15923 obstack_free (&cu->comp_unit_obstack, NULL);
15924 cu->partial_dies = NULL;
ae038cb0
DJ
15925
15926 if (cu->per_cu != NULL)
15927 {
15928 /* This compilation unit is on the stack in our caller, so we
15929 should not xfree it. Just unlink it. */
15930 cu->per_cu->cu = NULL;
15931 cu->per_cu = NULL;
15932
15933 /* If we had a per-cu pointer, then we may have other compilation
15934 units loaded, so age them now. */
15935 age_cached_comp_units ();
15936 }
15937}
15938
15939/* Free all cached compilation units. */
15940
15941static void
15942free_cached_comp_units (void *data)
15943{
15944 struct dwarf2_per_cu_data *per_cu, **last_chain;
15945
15946 per_cu = dwarf2_per_objfile->read_in_chain;
15947 last_chain = &dwarf2_per_objfile->read_in_chain;
15948 while (per_cu != NULL)
15949 {
15950 struct dwarf2_per_cu_data *next_cu;
15951
15952 next_cu = per_cu->cu->read_in_chain;
15953
15954 free_one_comp_unit (per_cu->cu);
15955 *last_chain = next_cu;
15956
15957 per_cu = next_cu;
15958 }
15959}
15960
15961/* Increase the age counter on each cached compilation unit, and free
15962 any that are too old. */
15963
15964static void
15965age_cached_comp_units (void)
15966{
15967 struct dwarf2_per_cu_data *per_cu, **last_chain;
15968
15969 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15970 per_cu = dwarf2_per_objfile->read_in_chain;
15971 while (per_cu != NULL)
15972 {
15973 per_cu->cu->last_used ++;
15974 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15975 dwarf2_mark (per_cu->cu);
15976 per_cu = per_cu->cu->read_in_chain;
15977 }
15978
15979 per_cu = dwarf2_per_objfile->read_in_chain;
15980 last_chain = &dwarf2_per_objfile->read_in_chain;
15981 while (per_cu != NULL)
15982 {
15983 struct dwarf2_per_cu_data *next_cu;
15984
15985 next_cu = per_cu->cu->read_in_chain;
15986
15987 if (!per_cu->cu->mark)
15988 {
15989 free_one_comp_unit (per_cu->cu);
15990 *last_chain = next_cu;
15991 }
15992 else
15993 last_chain = &per_cu->cu->read_in_chain;
15994
15995 per_cu = next_cu;
15996 }
15997}
15998
15999/* Remove a single compilation unit from the cache. */
16000
16001static void
16002free_one_cached_comp_unit (void *target_cu)
16003{
16004 struct dwarf2_per_cu_data *per_cu, **last_chain;
16005
16006 per_cu = dwarf2_per_objfile->read_in_chain;
16007 last_chain = &dwarf2_per_objfile->read_in_chain;
16008 while (per_cu != NULL)
16009 {
16010 struct dwarf2_per_cu_data *next_cu;
16011
16012 next_cu = per_cu->cu->read_in_chain;
16013
16014 if (per_cu->cu == target_cu)
16015 {
16016 free_one_comp_unit (per_cu->cu);
16017 *last_chain = next_cu;
16018 break;
16019 }
16020 else
16021 last_chain = &per_cu->cu->read_in_chain;
16022
16023 per_cu = next_cu;
16024 }
16025}
16026
fe3e1990
DJ
16027/* Release all extra memory associated with OBJFILE. */
16028
16029void
16030dwarf2_free_objfile (struct objfile *objfile)
16031{
16032 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16033
16034 if (dwarf2_per_objfile == NULL)
16035 return;
16036
16037 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16038 free_cached_comp_units (NULL);
16039
7b9f3c50
DE
16040 if (dwarf2_per_objfile->quick_file_names_table)
16041 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16042
fe3e1990
DJ
16043 /* Everything else should be on the objfile obstack. */
16044}
16045
1c379e20
DJ
16046/* A pair of DIE offset and GDB type pointer. We store these
16047 in a hash table separate from the DIEs, and preserve them
16048 when the DIEs are flushed out of cache. */
16049
16050struct dwarf2_offset_and_type
16051{
16052 unsigned int offset;
16053 struct type *type;
16054};
16055
16056/* Hash function for a dwarf2_offset_and_type. */
16057
16058static hashval_t
16059offset_and_type_hash (const void *item)
16060{
16061 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16062
1c379e20
DJ
16063 return ofs->offset;
16064}
16065
16066/* Equality function for a dwarf2_offset_and_type. */
16067
16068static int
16069offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16070{
16071 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16072 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16073
1c379e20
DJ
16074 return ofs_lhs->offset == ofs_rhs->offset;
16075}
16076
16077/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16078 table if necessary. For convenience, return TYPE.
16079
16080 The DIEs reading must have careful ordering to:
16081 * Not cause infite loops trying to read in DIEs as a prerequisite for
16082 reading current DIE.
16083 * Not trying to dereference contents of still incompletely read in types
16084 while reading in other DIEs.
16085 * Enable referencing still incompletely read in types just by a pointer to
16086 the type without accessing its fields.
16087
16088 Therefore caller should follow these rules:
16089 * Try to fetch any prerequisite types we may need to build this DIE type
16090 before building the type and calling set_die_type.
e71ec853 16091 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16092 possible before fetching more types to complete the current type.
16093 * Make the type as complete as possible before fetching more types. */
1c379e20 16094
f792889a 16095static struct type *
1c379e20
DJ
16096set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16097{
16098 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16099 struct objfile *objfile = cu->objfile;
16100 htab_t *type_hash_ptr;
1c379e20 16101
b4ba55a1
JB
16102 /* For Ada types, make sure that the gnat-specific data is always
16103 initialized (if not already set). There are a few types where
16104 we should not be doing so, because the type-specific area is
16105 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16106 where the type-specific area is used to store the floatformat).
16107 But this is not a problem, because the gnat-specific information
16108 is actually not needed for these types. */
16109 if (need_gnat_info (cu)
16110 && TYPE_CODE (type) != TYPE_CODE_FUNC
16111 && TYPE_CODE (type) != TYPE_CODE_FLT
16112 && !HAVE_GNAT_AUX_INFO (type))
16113 INIT_GNAT_SPECIFIC (type);
16114
b0df02fd 16115 if (cu->per_cu->debug_types_section)
673bfd45
DE
16116 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16117 else
16118 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16119
16120 if (*type_hash_ptr == NULL)
f792889a 16121 {
673bfd45
DE
16122 *type_hash_ptr
16123 = htab_create_alloc_ex (127,
f792889a
DJ
16124 offset_and_type_hash,
16125 offset_and_type_eq,
16126 NULL,
673bfd45 16127 &objfile->objfile_obstack,
f792889a
DJ
16128 hashtab_obstack_allocate,
16129 dummy_obstack_deallocate);
f792889a 16130 }
1c379e20
DJ
16131
16132 ofs.offset = die->offset;
16133 ofs.type = type;
16134 slot = (struct dwarf2_offset_and_type **)
673bfd45 16135 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16136 if (*slot)
16137 complaint (&symfile_complaints,
16138 _("A problem internal to GDB: DIE 0x%x has type already set"),
16139 die->offset);
673bfd45 16140 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16141 **slot = ofs;
f792889a 16142 return type;
1c379e20
DJ
16143}
16144
673bfd45
DE
16145/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16146 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16147
16148static struct type *
673bfd45
DE
16149get_die_type_at_offset (unsigned int offset,
16150 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16151{
16152 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16153 htab_t type_hash;
f792889a 16154
b0df02fd 16155 if (per_cu->debug_types_section)
673bfd45
DE
16156 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16157 else
16158 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16159 if (type_hash == NULL)
16160 return NULL;
1c379e20 16161
673bfd45 16162 ofs.offset = offset;
1c379e20
DJ
16163 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16164 if (slot)
16165 return slot->type;
16166 else
16167 return NULL;
16168}
16169
673bfd45
DE
16170/* Look up the type for DIE in the appropriate type_hash table,
16171 or return NULL if DIE does not have a saved type. */
16172
16173static struct type *
16174get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16175{
16176 return get_die_type_at_offset (die->offset, cu->per_cu);
16177}
16178
10b3939b
DJ
16179/* Add a dependence relationship from CU to REF_PER_CU. */
16180
16181static void
16182dwarf2_add_dependence (struct dwarf2_cu *cu,
16183 struct dwarf2_per_cu_data *ref_per_cu)
16184{
16185 void **slot;
16186
16187 if (cu->dependencies == NULL)
16188 cu->dependencies
16189 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16190 NULL, &cu->comp_unit_obstack,
16191 hashtab_obstack_allocate,
16192 dummy_obstack_deallocate);
16193
16194 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16195 if (*slot == NULL)
16196 *slot = ref_per_cu;
16197}
1c379e20 16198
f504f079
DE
16199/* Subroutine of dwarf2_mark to pass to htab_traverse.
16200 Set the mark field in every compilation unit in the
ae038cb0
DJ
16201 cache that we must keep because we are keeping CU. */
16202
10b3939b
DJ
16203static int
16204dwarf2_mark_helper (void **slot, void *data)
16205{
16206 struct dwarf2_per_cu_data *per_cu;
16207
16208 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16209
16210 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16211 reading of the chain. As such dependencies remain valid it is not much
16212 useful to track and undo them during QUIT cleanups. */
16213 if (per_cu->cu == NULL)
16214 return 1;
16215
10b3939b
DJ
16216 if (per_cu->cu->mark)
16217 return 1;
16218 per_cu->cu->mark = 1;
16219
16220 if (per_cu->cu->dependencies != NULL)
16221 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16222
16223 return 1;
16224}
16225
f504f079
DE
16226/* Set the mark field in CU and in every other compilation unit in the
16227 cache that we must keep because we are keeping CU. */
16228
ae038cb0
DJ
16229static void
16230dwarf2_mark (struct dwarf2_cu *cu)
16231{
16232 if (cu->mark)
16233 return;
16234 cu->mark = 1;
10b3939b
DJ
16235 if (cu->dependencies != NULL)
16236 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16237}
16238
16239static void
16240dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16241{
16242 while (per_cu)
16243 {
16244 per_cu->cu->mark = 0;
16245 per_cu = per_cu->cu->read_in_chain;
16246 }
72bf9492
DJ
16247}
16248
72bf9492
DJ
16249/* Trivial hash function for partial_die_info: the hash value of a DIE
16250 is its offset in .debug_info for this objfile. */
16251
16252static hashval_t
16253partial_die_hash (const void *item)
16254{
16255 const struct partial_die_info *part_die = item;
9a619af0 16256
72bf9492
DJ
16257 return part_die->offset;
16258}
16259
16260/* Trivial comparison function for partial_die_info structures: two DIEs
16261 are equal if they have the same offset. */
16262
16263static int
16264partial_die_eq (const void *item_lhs, const void *item_rhs)
16265{
16266 const struct partial_die_info *part_die_lhs = item_lhs;
16267 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16268
72bf9492
DJ
16269 return part_die_lhs->offset == part_die_rhs->offset;
16270}
16271
ae038cb0
DJ
16272static struct cmd_list_element *set_dwarf2_cmdlist;
16273static struct cmd_list_element *show_dwarf2_cmdlist;
16274
16275static void
16276set_dwarf2_cmd (char *args, int from_tty)
16277{
16278 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16279}
16280
16281static void
16282show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16283{
ae038cb0
DJ
16284 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16285}
16286
dce234bc
PP
16287/* If section described by INFO was mmapped, munmap it now. */
16288
16289static void
16290munmap_section_buffer (struct dwarf2_section_info *info)
16291{
b315ab21 16292 if (info->map_addr != NULL)
dce234bc
PP
16293 {
16294#ifdef HAVE_MMAP
b315ab21 16295 int res;
9a619af0 16296
b315ab21
TG
16297 res = munmap (info->map_addr, info->map_len);
16298 gdb_assert (res == 0);
dce234bc
PP
16299#else
16300 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16301 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16302#endif
16303 }
16304}
16305
16306/* munmap debug sections for OBJFILE, if necessary. */
16307
16308static void
c1bd65d0 16309dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16310{
16311 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16312 int ix;
16313 struct dwarf2_section_info *section;
9a619af0 16314
16be1145
DE
16315 /* This is sorted according to the order they're defined in to make it easier
16316 to keep in sync. */
dce234bc
PP
16317 munmap_section_buffer (&data->info);
16318 munmap_section_buffer (&data->abbrev);
16319 munmap_section_buffer (&data->line);
16be1145 16320 munmap_section_buffer (&data->loc);
dce234bc 16321 munmap_section_buffer (&data->macinfo);
cf2c3c16 16322 munmap_section_buffer (&data->macro);
16be1145 16323 munmap_section_buffer (&data->str);
dce234bc 16324 munmap_section_buffer (&data->ranges);
dce234bc
PP
16325 munmap_section_buffer (&data->frame);
16326 munmap_section_buffer (&data->eh_frame);
9291a0cd 16327 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16328
16329 for (ix = 0;
16330 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16331 ++ix)
16332 munmap_section_buffer (section);
16333
16334 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16335}
16336
16337\f
ae2de4f8 16338/* The "save gdb-index" command. */
9291a0cd
TT
16339
16340/* The contents of the hash table we create when building the string
16341 table. */
16342struct strtab_entry
16343{
16344 offset_type offset;
16345 const char *str;
16346};
16347
559a7a62
JK
16348/* Hash function for a strtab_entry.
16349
16350 Function is used only during write_hash_table so no index format backward
16351 compatibility is needed. */
b89be57b 16352
9291a0cd
TT
16353static hashval_t
16354hash_strtab_entry (const void *e)
16355{
16356 const struct strtab_entry *entry = e;
559a7a62 16357 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16358}
16359
16360/* Equality function for a strtab_entry. */
b89be57b 16361
9291a0cd
TT
16362static int
16363eq_strtab_entry (const void *a, const void *b)
16364{
16365 const struct strtab_entry *ea = a;
16366 const struct strtab_entry *eb = b;
16367 return !strcmp (ea->str, eb->str);
16368}
16369
16370/* Create a strtab_entry hash table. */
b89be57b 16371
9291a0cd
TT
16372static htab_t
16373create_strtab (void)
16374{
16375 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16376 xfree, xcalloc, xfree);
16377}
16378
16379/* Add a string to the constant pool. Return the string's offset in
16380 host order. */
b89be57b 16381
9291a0cd
TT
16382static offset_type
16383add_string (htab_t table, struct obstack *cpool, const char *str)
16384{
16385 void **slot;
16386 struct strtab_entry entry;
16387 struct strtab_entry *result;
16388
16389 entry.str = str;
16390 slot = htab_find_slot (table, &entry, INSERT);
16391 if (*slot)
16392 result = *slot;
16393 else
16394 {
16395 result = XNEW (struct strtab_entry);
16396 result->offset = obstack_object_size (cpool);
16397 result->str = str;
16398 obstack_grow_str0 (cpool, str);
16399 *slot = result;
16400 }
16401 return result->offset;
16402}
16403
16404/* An entry in the symbol table. */
16405struct symtab_index_entry
16406{
16407 /* The name of the symbol. */
16408 const char *name;
16409 /* The offset of the name in the constant pool. */
16410 offset_type index_offset;
16411 /* A sorted vector of the indices of all the CUs that hold an object
16412 of this name. */
16413 VEC (offset_type) *cu_indices;
16414};
16415
16416/* The symbol table. This is a power-of-2-sized hash table. */
16417struct mapped_symtab
16418{
16419 offset_type n_elements;
16420 offset_type size;
16421 struct symtab_index_entry **data;
16422};
16423
16424/* Hash function for a symtab_index_entry. */
b89be57b 16425
9291a0cd
TT
16426static hashval_t
16427hash_symtab_entry (const void *e)
16428{
16429 const struct symtab_index_entry *entry = e;
16430 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16431 sizeof (offset_type) * VEC_length (offset_type,
16432 entry->cu_indices),
16433 0);
16434}
16435
16436/* Equality function for a symtab_index_entry. */
b89be57b 16437
9291a0cd
TT
16438static int
16439eq_symtab_entry (const void *a, const void *b)
16440{
16441 const struct symtab_index_entry *ea = a;
16442 const struct symtab_index_entry *eb = b;
16443 int len = VEC_length (offset_type, ea->cu_indices);
16444 if (len != VEC_length (offset_type, eb->cu_indices))
16445 return 0;
16446 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16447 VEC_address (offset_type, eb->cu_indices),
16448 sizeof (offset_type) * len);
16449}
16450
16451/* Destroy a symtab_index_entry. */
b89be57b 16452
9291a0cd
TT
16453static void
16454delete_symtab_entry (void *p)
16455{
16456 struct symtab_index_entry *entry = p;
16457 VEC_free (offset_type, entry->cu_indices);
16458 xfree (entry);
16459}
16460
16461/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16462
9291a0cd 16463static htab_t
3876f04e 16464create_symbol_hash_table (void)
9291a0cd
TT
16465{
16466 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16467 delete_symtab_entry, xcalloc, xfree);
16468}
16469
16470/* Create a new mapped symtab object. */
b89be57b 16471
9291a0cd
TT
16472static struct mapped_symtab *
16473create_mapped_symtab (void)
16474{
16475 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16476 symtab->n_elements = 0;
16477 symtab->size = 1024;
16478 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16479 return symtab;
16480}
16481
16482/* Destroy a mapped_symtab. */
b89be57b 16483
9291a0cd
TT
16484static void
16485cleanup_mapped_symtab (void *p)
16486{
16487 struct mapped_symtab *symtab = p;
16488 /* The contents of the array are freed when the other hash table is
16489 destroyed. */
16490 xfree (symtab->data);
16491 xfree (symtab);
16492}
16493
16494/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16495 the slot.
16496
16497 Function is used only during write_hash_table so no index format backward
16498 compatibility is needed. */
b89be57b 16499
9291a0cd
TT
16500static struct symtab_index_entry **
16501find_slot (struct mapped_symtab *symtab, const char *name)
16502{
559a7a62 16503 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16504
16505 index = hash & (symtab->size - 1);
16506 step = ((hash * 17) & (symtab->size - 1)) | 1;
16507
16508 for (;;)
16509 {
16510 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16511 return &symtab->data[index];
16512 index = (index + step) & (symtab->size - 1);
16513 }
16514}
16515
16516/* Expand SYMTAB's hash table. */
b89be57b 16517
9291a0cd
TT
16518static void
16519hash_expand (struct mapped_symtab *symtab)
16520{
16521 offset_type old_size = symtab->size;
16522 offset_type i;
16523 struct symtab_index_entry **old_entries = symtab->data;
16524
16525 symtab->size *= 2;
16526 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16527
16528 for (i = 0; i < old_size; ++i)
16529 {
16530 if (old_entries[i])
16531 {
16532 struct symtab_index_entry **slot = find_slot (symtab,
16533 old_entries[i]->name);
16534 *slot = old_entries[i];
16535 }
16536 }
16537
16538 xfree (old_entries);
16539}
16540
16541/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16542 is the index of the CU in which the symbol appears. */
b89be57b 16543
9291a0cd
TT
16544static void
16545add_index_entry (struct mapped_symtab *symtab, const char *name,
16546 offset_type cu_index)
16547{
16548 struct symtab_index_entry **slot;
16549
16550 ++symtab->n_elements;
16551 if (4 * symtab->n_elements / 3 >= symtab->size)
16552 hash_expand (symtab);
16553
16554 slot = find_slot (symtab, name);
16555 if (!*slot)
16556 {
16557 *slot = XNEW (struct symtab_index_entry);
16558 (*slot)->name = name;
16559 (*slot)->cu_indices = NULL;
16560 }
16561 /* Don't push an index twice. Due to how we add entries we only
16562 have to check the last one. */
16563 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16564 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16565 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16566}
16567
16568/* Add a vector of indices to the constant pool. */
b89be57b 16569
9291a0cd 16570static offset_type
3876f04e 16571add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16572 struct symtab_index_entry *entry)
16573{
16574 void **slot;
16575
3876f04e 16576 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16577 if (!*slot)
16578 {
16579 offset_type len = VEC_length (offset_type, entry->cu_indices);
16580 offset_type val = MAYBE_SWAP (len);
16581 offset_type iter;
16582 int i;
16583
16584 *slot = entry;
16585 entry->index_offset = obstack_object_size (cpool);
16586
16587 obstack_grow (cpool, &val, sizeof (val));
16588 for (i = 0;
16589 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16590 ++i)
16591 {
16592 val = MAYBE_SWAP (iter);
16593 obstack_grow (cpool, &val, sizeof (val));
16594 }
16595 }
16596 else
16597 {
16598 struct symtab_index_entry *old_entry = *slot;
16599 entry->index_offset = old_entry->index_offset;
16600 entry = old_entry;
16601 }
16602 return entry->index_offset;
16603}
16604
16605/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16606 constant pool entries going into the obstack CPOOL. */
b89be57b 16607
9291a0cd
TT
16608static void
16609write_hash_table (struct mapped_symtab *symtab,
16610 struct obstack *output, struct obstack *cpool)
16611{
16612 offset_type i;
3876f04e 16613 htab_t symbol_hash_table;
9291a0cd
TT
16614 htab_t str_table;
16615
3876f04e 16616 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16617 str_table = create_strtab ();
3876f04e 16618
9291a0cd
TT
16619 /* We add all the index vectors to the constant pool first, to
16620 ensure alignment is ok. */
16621 for (i = 0; i < symtab->size; ++i)
16622 {
16623 if (symtab->data[i])
3876f04e 16624 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16625 }
16626
16627 /* Now write out the hash table. */
16628 for (i = 0; i < symtab->size; ++i)
16629 {
16630 offset_type str_off, vec_off;
16631
16632 if (symtab->data[i])
16633 {
16634 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16635 vec_off = symtab->data[i]->index_offset;
16636 }
16637 else
16638 {
16639 /* While 0 is a valid constant pool index, it is not valid
16640 to have 0 for both offsets. */
16641 str_off = 0;
16642 vec_off = 0;
16643 }
16644
16645 str_off = MAYBE_SWAP (str_off);
16646 vec_off = MAYBE_SWAP (vec_off);
16647
16648 obstack_grow (output, &str_off, sizeof (str_off));
16649 obstack_grow (output, &vec_off, sizeof (vec_off));
16650 }
16651
16652 htab_delete (str_table);
3876f04e 16653 htab_delete (symbol_hash_table);
9291a0cd
TT
16654}
16655
0a5429f6
DE
16656/* Struct to map psymtab to CU index in the index file. */
16657struct psymtab_cu_index_map
16658{
16659 struct partial_symtab *psymtab;
16660 unsigned int cu_index;
16661};
16662
16663static hashval_t
16664hash_psymtab_cu_index (const void *item)
16665{
16666 const struct psymtab_cu_index_map *map = item;
16667
16668 return htab_hash_pointer (map->psymtab);
16669}
16670
16671static int
16672eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16673{
16674 const struct psymtab_cu_index_map *lhs = item_lhs;
16675 const struct psymtab_cu_index_map *rhs = item_rhs;
16676
16677 return lhs->psymtab == rhs->psymtab;
16678}
16679
16680/* Helper struct for building the address table. */
16681struct addrmap_index_data
16682{
16683 struct objfile *objfile;
16684 struct obstack *addr_obstack;
16685 htab_t cu_index_htab;
16686
16687 /* Non-zero if the previous_* fields are valid.
16688 We can't write an entry until we see the next entry (since it is only then
16689 that we know the end of the entry). */
16690 int previous_valid;
16691 /* Index of the CU in the table of all CUs in the index file. */
16692 unsigned int previous_cu_index;
0963b4bd 16693 /* Start address of the CU. */
0a5429f6
DE
16694 CORE_ADDR previous_cu_start;
16695};
16696
16697/* Write an address entry to OBSTACK. */
b89be57b 16698
9291a0cd 16699static void
0a5429f6
DE
16700add_address_entry (struct objfile *objfile, struct obstack *obstack,
16701 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16702{
0a5429f6 16703 offset_type cu_index_to_write;
9291a0cd
TT
16704 char addr[8];
16705 CORE_ADDR baseaddr;
16706
16707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16708
0a5429f6
DE
16709 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16710 obstack_grow (obstack, addr, 8);
16711 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16712 obstack_grow (obstack, addr, 8);
16713 cu_index_to_write = MAYBE_SWAP (cu_index);
16714 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16715}
16716
16717/* Worker function for traversing an addrmap to build the address table. */
16718
16719static int
16720add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16721{
16722 struct addrmap_index_data *data = datap;
16723 struct partial_symtab *pst = obj;
16724 offset_type cu_index;
16725 void **slot;
16726
16727 if (data->previous_valid)
16728 add_address_entry (data->objfile, data->addr_obstack,
16729 data->previous_cu_start, start_addr,
16730 data->previous_cu_index);
16731
16732 data->previous_cu_start = start_addr;
16733 if (pst != NULL)
16734 {
16735 struct psymtab_cu_index_map find_map, *map;
16736 find_map.psymtab = pst;
16737 map = htab_find (data->cu_index_htab, &find_map);
16738 gdb_assert (map != NULL);
16739 data->previous_cu_index = map->cu_index;
16740 data->previous_valid = 1;
16741 }
16742 else
16743 data->previous_valid = 0;
16744
16745 return 0;
16746}
16747
16748/* Write OBJFILE's address map to OBSTACK.
16749 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16750 in the index file. */
16751
16752static void
16753write_address_map (struct objfile *objfile, struct obstack *obstack,
16754 htab_t cu_index_htab)
16755{
16756 struct addrmap_index_data addrmap_index_data;
16757
16758 /* When writing the address table, we have to cope with the fact that
16759 the addrmap iterator only provides the start of a region; we have to
16760 wait until the next invocation to get the start of the next region. */
16761
16762 addrmap_index_data.objfile = objfile;
16763 addrmap_index_data.addr_obstack = obstack;
16764 addrmap_index_data.cu_index_htab = cu_index_htab;
16765 addrmap_index_data.previous_valid = 0;
16766
16767 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16768 &addrmap_index_data);
16769
16770 /* It's highly unlikely the last entry (end address = 0xff...ff)
16771 is valid, but we should still handle it.
16772 The end address is recorded as the start of the next region, but that
16773 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16774 anyway. */
16775 if (addrmap_index_data.previous_valid)
16776 add_address_entry (objfile, obstack,
16777 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16778 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16779}
16780
16781/* Add a list of partial symbols to SYMTAB. */
b89be57b 16782
9291a0cd
TT
16783static void
16784write_psymbols (struct mapped_symtab *symtab,
987d643c 16785 htab_t psyms_seen,
9291a0cd
TT
16786 struct partial_symbol **psymp,
16787 int count,
987d643c
TT
16788 offset_type cu_index,
16789 int is_static)
9291a0cd
TT
16790{
16791 for (; count-- > 0; ++psymp)
16792 {
987d643c
TT
16793 void **slot, *lookup;
16794
9291a0cd
TT
16795 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16796 error (_("Ada is not currently supported by the index"));
987d643c
TT
16797
16798 /* We only want to add a given psymbol once. However, we also
16799 want to account for whether it is global or static. So, we
16800 may add it twice, using slightly different values. */
16801 if (is_static)
16802 {
16803 uintptr_t val = 1 | (uintptr_t) *psymp;
16804
16805 lookup = (void *) val;
16806 }
16807 else
16808 lookup = *psymp;
16809
16810 /* Only add a given psymbol once. */
16811 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16812 if (!*slot)
16813 {
16814 *slot = lookup;
bb2f58dc 16815 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16816 }
9291a0cd
TT
16817 }
16818}
16819
16820/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16821 exception if there is an error. */
b89be57b 16822
9291a0cd
TT
16823static void
16824write_obstack (FILE *file, struct obstack *obstack)
16825{
16826 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16827 file)
16828 != obstack_object_size (obstack))
16829 error (_("couldn't data write to file"));
16830}
16831
16832/* Unlink a file if the argument is not NULL. */
b89be57b 16833
9291a0cd
TT
16834static void
16835unlink_if_set (void *p)
16836{
16837 char **filename = p;
16838 if (*filename)
16839 unlink (*filename);
16840}
16841
1fd400ff
TT
16842/* A helper struct used when iterating over debug_types. */
16843struct signatured_type_index_data
16844{
16845 struct objfile *objfile;
16846 struct mapped_symtab *symtab;
16847 struct obstack *types_list;
987d643c 16848 htab_t psyms_seen;
1fd400ff
TT
16849 int cu_index;
16850};
16851
16852/* A helper function that writes a single signatured_type to an
16853 obstack. */
b89be57b 16854
1fd400ff
TT
16855static int
16856write_one_signatured_type (void **slot, void *d)
16857{
16858 struct signatured_type_index_data *info = d;
16859 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16860 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16861 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16862 gdb_byte val[8];
16863
16864 write_psymbols (info->symtab,
987d643c 16865 info->psyms_seen,
3e43a32a
MS
16866 info->objfile->global_psymbols.list
16867 + psymtab->globals_offset,
987d643c
TT
16868 psymtab->n_global_syms, info->cu_index,
16869 0);
1fd400ff 16870 write_psymbols (info->symtab,
987d643c 16871 info->psyms_seen,
3e43a32a
MS
16872 info->objfile->static_psymbols.list
16873 + psymtab->statics_offset,
987d643c
TT
16874 psymtab->n_static_syms, info->cu_index,
16875 1);
1fd400ff 16876
b3c8eb43 16877 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16878 obstack_grow (info->types_list, val, 8);
16879 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16880 obstack_grow (info->types_list, val, 8);
16881 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16882 obstack_grow (info->types_list, val, 8);
16883
16884 ++info->cu_index;
16885
16886 return 1;
16887}
16888
9291a0cd 16889/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16890
9291a0cd
TT
16891static void
16892write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16893{
16894 struct cleanup *cleanup;
16895 char *filename, *cleanup_filename;
1fd400ff
TT
16896 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16897 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16898 int i;
16899 FILE *out_file;
16900 struct mapped_symtab *symtab;
16901 offset_type val, size_of_contents, total_len;
16902 struct stat st;
16903 char buf[8];
987d643c 16904 htab_t psyms_seen;
0a5429f6
DE
16905 htab_t cu_index_htab;
16906 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16907
b4f2f049 16908 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16909 return;
b4f2f049 16910
9291a0cd
TT
16911 if (dwarf2_per_objfile->using_index)
16912 error (_("Cannot use an index to create the index"));
16913
8b70b953
TT
16914 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16915 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16916
9291a0cd 16917 if (stat (objfile->name, &st) < 0)
7e17e088 16918 perror_with_name (objfile->name);
9291a0cd
TT
16919
16920 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16921 INDEX_SUFFIX, (char *) NULL);
16922 cleanup = make_cleanup (xfree, filename);
16923
16924 out_file = fopen (filename, "wb");
16925 if (!out_file)
16926 error (_("Can't open `%s' for writing"), filename);
16927
16928 cleanup_filename = filename;
16929 make_cleanup (unlink_if_set, &cleanup_filename);
16930
16931 symtab = create_mapped_symtab ();
16932 make_cleanup (cleanup_mapped_symtab, symtab);
16933
16934 obstack_init (&addr_obstack);
16935 make_cleanup_obstack_free (&addr_obstack);
16936
16937 obstack_init (&cu_list);
16938 make_cleanup_obstack_free (&cu_list);
16939
1fd400ff
TT
16940 obstack_init (&types_cu_list);
16941 make_cleanup_obstack_free (&types_cu_list);
16942
987d643c
TT
16943 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16944 NULL, xcalloc, xfree);
96408a79 16945 make_cleanup_htab_delete (psyms_seen);
987d643c 16946
0a5429f6
DE
16947 /* While we're scanning CU's create a table that maps a psymtab pointer
16948 (which is what addrmap records) to its index (which is what is recorded
16949 in the index file). This will later be needed to write the address
16950 table. */
16951 cu_index_htab = htab_create_alloc (100,
16952 hash_psymtab_cu_index,
16953 eq_psymtab_cu_index,
16954 NULL, xcalloc, xfree);
96408a79 16955 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16956 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16957 xmalloc (sizeof (struct psymtab_cu_index_map)
16958 * dwarf2_per_objfile->n_comp_units);
16959 make_cleanup (xfree, psymtab_cu_index_map);
16960
16961 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16962 work here. Also, the debug_types entries do not appear in
16963 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16964 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16965 {
3e43a32a
MS
16966 struct dwarf2_per_cu_data *per_cu
16967 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16968 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16969 gdb_byte val[8];
0a5429f6
DE
16970 struct psymtab_cu_index_map *map;
16971 void **slot;
9291a0cd
TT
16972
16973 write_psymbols (symtab,
987d643c 16974 psyms_seen,
9291a0cd 16975 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16976 psymtab->n_global_syms, i,
16977 0);
9291a0cd 16978 write_psymbols (symtab,
987d643c 16979 psyms_seen,
9291a0cd 16980 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16981 psymtab->n_static_syms, i,
16982 1);
9291a0cd 16983
0a5429f6
DE
16984 map = &psymtab_cu_index_map[i];
16985 map->psymtab = psymtab;
16986 map->cu_index = i;
16987 slot = htab_find_slot (cu_index_htab, map, INSERT);
16988 gdb_assert (slot != NULL);
16989 gdb_assert (*slot == NULL);
16990 *slot = map;
9291a0cd 16991
e254ef6a 16992 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16993 obstack_grow (&cu_list, val, 8);
e254ef6a 16994 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16995 obstack_grow (&cu_list, val, 8);
16996 }
16997
0a5429f6
DE
16998 /* Dump the address map. */
16999 write_address_map (objfile, &addr_obstack, cu_index_htab);
17000
1fd400ff
TT
17001 /* Write out the .debug_type entries, if any. */
17002 if (dwarf2_per_objfile->signatured_types)
17003 {
17004 struct signatured_type_index_data sig_data;
17005
17006 sig_data.objfile = objfile;
17007 sig_data.symtab = symtab;
17008 sig_data.types_list = &types_cu_list;
987d643c 17009 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17010 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17011 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17012 write_one_signatured_type, &sig_data);
17013 }
17014
9291a0cd
TT
17015 obstack_init (&constant_pool);
17016 make_cleanup_obstack_free (&constant_pool);
17017 obstack_init (&symtab_obstack);
17018 make_cleanup_obstack_free (&symtab_obstack);
17019 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17020
17021 obstack_init (&contents);
17022 make_cleanup_obstack_free (&contents);
1fd400ff 17023 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17024 total_len = size_of_contents;
17025
17026 /* The version number. */
559a7a62 17027 val = MAYBE_SWAP (5);
9291a0cd
TT
17028 obstack_grow (&contents, &val, sizeof (val));
17029
17030 /* The offset of the CU list from the start of the file. */
17031 val = MAYBE_SWAP (total_len);
17032 obstack_grow (&contents, &val, sizeof (val));
17033 total_len += obstack_object_size (&cu_list);
17034
1fd400ff
TT
17035 /* The offset of the types CU list from the start of the file. */
17036 val = MAYBE_SWAP (total_len);
17037 obstack_grow (&contents, &val, sizeof (val));
17038 total_len += obstack_object_size (&types_cu_list);
17039
9291a0cd
TT
17040 /* The offset of the address table from the start of the file. */
17041 val = MAYBE_SWAP (total_len);
17042 obstack_grow (&contents, &val, sizeof (val));
17043 total_len += obstack_object_size (&addr_obstack);
17044
17045 /* The offset of the symbol table from the start of the file. */
17046 val = MAYBE_SWAP (total_len);
17047 obstack_grow (&contents, &val, sizeof (val));
17048 total_len += obstack_object_size (&symtab_obstack);
17049
17050 /* The offset of the constant pool from the start of the file. */
17051 val = MAYBE_SWAP (total_len);
17052 obstack_grow (&contents, &val, sizeof (val));
17053 total_len += obstack_object_size (&constant_pool);
17054
17055 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17056
17057 write_obstack (out_file, &contents);
17058 write_obstack (out_file, &cu_list);
1fd400ff 17059 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17060 write_obstack (out_file, &addr_obstack);
17061 write_obstack (out_file, &symtab_obstack);
17062 write_obstack (out_file, &constant_pool);
17063
17064 fclose (out_file);
17065
17066 /* We want to keep the file, so we set cleanup_filename to NULL
17067 here. See unlink_if_set. */
17068 cleanup_filename = NULL;
17069
17070 do_cleanups (cleanup);
17071}
17072
90476074
TT
17073/* Implementation of the `save gdb-index' command.
17074
17075 Note that the file format used by this command is documented in the
17076 GDB manual. Any changes here must be documented there. */
11570e71 17077
9291a0cd
TT
17078static void
17079save_gdb_index_command (char *arg, int from_tty)
17080{
17081 struct objfile *objfile;
17082
17083 if (!arg || !*arg)
96d19272 17084 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17085
17086 ALL_OBJFILES (objfile)
17087 {
17088 struct stat st;
17089
17090 /* If the objfile does not correspond to an actual file, skip it. */
17091 if (stat (objfile->name, &st) < 0)
17092 continue;
17093
17094 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17095 if (dwarf2_per_objfile)
17096 {
17097 volatile struct gdb_exception except;
17098
17099 TRY_CATCH (except, RETURN_MASK_ERROR)
17100 {
17101 write_psymtabs_to_index (objfile, arg);
17102 }
17103 if (except.reason < 0)
17104 exception_fprintf (gdb_stderr, except,
17105 _("Error while writing index for `%s': "),
17106 objfile->name);
17107 }
17108 }
dce234bc
PP
17109}
17110
9291a0cd
TT
17111\f
17112
9eae7c52
TT
17113int dwarf2_always_disassemble;
17114
17115static void
17116show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17117 struct cmd_list_element *c, const char *value)
17118{
3e43a32a
MS
17119 fprintf_filtered (file,
17120 _("Whether to always disassemble "
17121 "DWARF expressions is %s.\n"),
9eae7c52
TT
17122 value);
17123}
17124
900e11f9
JK
17125static void
17126show_check_physname (struct ui_file *file, int from_tty,
17127 struct cmd_list_element *c, const char *value)
17128{
17129 fprintf_filtered (file,
17130 _("Whether to check \"physname\" is %s.\n"),
17131 value);
17132}
17133
6502dd73
DJ
17134void _initialize_dwarf2_read (void);
17135
17136void
17137_initialize_dwarf2_read (void)
17138{
96d19272
JK
17139 struct cmd_list_element *c;
17140
dce234bc 17141 dwarf2_objfile_data_key
c1bd65d0 17142 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17143
1bedd215
AC
17144 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17145Set DWARF 2 specific variables.\n\
17146Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17147 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17148 0/*allow-unknown*/, &maintenance_set_cmdlist);
17149
1bedd215
AC
17150 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17151Show DWARF 2 specific variables\n\
17152Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17153 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17154 0/*allow-unknown*/, &maintenance_show_cmdlist);
17155
17156 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17157 &dwarf2_max_cache_age, _("\
17158Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17159Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17160A higher limit means that cached compilation units will be stored\n\
17161in memory longer, and more total memory will be used. Zero disables\n\
17162caching, which can slow down startup."),
2c5b56ce 17163 NULL,
920d2a44 17164 show_dwarf2_max_cache_age,
2c5b56ce 17165 &set_dwarf2_cmdlist,
ae038cb0 17166 &show_dwarf2_cmdlist);
d97bc12b 17167
9eae7c52
TT
17168 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17169 &dwarf2_always_disassemble, _("\
17170Set whether `info address' always disassembles DWARF expressions."), _("\
17171Show whether `info address' always disassembles DWARF expressions."), _("\
17172When enabled, DWARF expressions are always printed in an assembly-like\n\
17173syntax. When disabled, expressions will be printed in a more\n\
17174conversational style, when possible."),
17175 NULL,
17176 show_dwarf2_always_disassemble,
17177 &set_dwarf2_cmdlist,
17178 &show_dwarf2_cmdlist);
17179
d97bc12b
DE
17180 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17181Set debugging of the dwarf2 DIE reader."), _("\
17182Show debugging of the dwarf2 DIE reader."), _("\
17183When enabled (non-zero), DIEs are dumped after they are read in.\n\
17184The value is the maximum depth to print."),
17185 NULL,
17186 NULL,
17187 &setdebuglist, &showdebuglist);
9291a0cd 17188
900e11f9
JK
17189 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17190Set cross-checking of \"physname\" code against demangler."), _("\
17191Show cross-checking of \"physname\" code against demangler."), _("\
17192When enabled, GDB's internal \"physname\" code is checked against\n\
17193the demangler."),
17194 NULL, show_check_physname,
17195 &setdebuglist, &showdebuglist);
17196
96d19272 17197 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17198 _("\
fc1a9d6e 17199Save a gdb-index file.\n\
11570e71 17200Usage: save gdb-index DIRECTORY"),
96d19272
JK
17201 &save_cmdlist);
17202 set_cmd_completer (c, filename_completer);
6502dd73 17203}
This page took 2.994397 seconds and 4 git commands to generate.