gdb/testsuite/
[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
6502dd73
DJ
184struct dwarf2_per_objfile
185{
dce234bc
PP
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
dce234bc
PP
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
cf2c3c16 191 struct dwarf2_section_info macro;
dce234bc
PP
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
194 struct dwarf2_section_info frame;
195 struct dwarf2_section_info eh_frame;
9291a0cd 196 struct dwarf2_section_info gdb_index;
ae038cb0 197
8b70b953
TT
198 VEC (dwarf2_section_info_def) *types;
199
be391dca
TT
200 /* Back link. */
201 struct objfile *objfile;
202
10b3939b
DJ
203 /* A list of all the compilation units. This is used to locate
204 the target compilation unit of a particular reference. */
ae038cb0
DJ
205 struct dwarf2_per_cu_data **all_comp_units;
206
207 /* The number of compilation units in ALL_COMP_UNITS. */
208 int n_comp_units;
209
1fd400ff
TT
210 /* The number of .debug_types-related CUs. */
211 int n_type_comp_units;
212
213 /* The .debug_types-related CUs. */
214 struct dwarf2_per_cu_data **type_comp_units;
215
ae038cb0
DJ
216 /* A chain of compilation units that are currently read in, so that
217 they can be freed later. */
218 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
72dca2f5
FR
224 /* A flag indicating wether this objfile has a section loaded at a
225 VMA of 0. */
226 int has_section_at_zero;
9291a0cd 227
ae2de4f8
DE
228 /* True if we are using the mapped index,
229 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
230 unsigned char using_index;
231
ae2de4f8 232 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 233 struct mapped_index *index_table;
98bfdba5 234
7b9f3c50
DE
235 /* When using index_table, this keeps track of all quick_file_names entries.
236 TUs can share line table entries with CUs or other TUs, and there can be
237 a lot more TUs than unique line tables, so we maintain a separate table
238 of all line table entries to support the sharing. */
239 htab_t quick_file_names_table;
240
98bfdba5
PA
241 /* Set during partial symbol reading, to prevent queueing of full
242 symbols. */
243 int reading_partial_symbols;
673bfd45
DE
244
245 /* Table mapping type .debug_info DIE offsets to types.
246 This is NULL if not allocated yet.
247 It (currently) makes sense to allocate debug_types_type_hash lazily.
248 To keep things simple we allocate both lazily. */
249 htab_t debug_info_type_hash;
250
251 /* Table mapping type .debug_types DIE offsets to types.
252 This is NULL if not allocated yet. */
253 htab_t debug_types_type_hash;
6502dd73
DJ
254};
255
256static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 257
251d32d9 258/* Default names of the debugging sections. */
c906108c 259
233a11ab
CS
260/* Note that if the debugging section has been compressed, it might
261 have a name like .zdebug_info. */
262
251d32d9
TG
263static const struct dwarf2_debug_sections dwarf2_elf_names = {
264 { ".debug_info", ".zdebug_info" },
265 { ".debug_abbrev", ".zdebug_abbrev" },
266 { ".debug_line", ".zdebug_line" },
267 { ".debug_loc", ".zdebug_loc" },
268 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 269 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
270 { ".debug_str", ".zdebug_str" },
271 { ".debug_ranges", ".zdebug_ranges" },
272 { ".debug_types", ".zdebug_types" },
273 { ".debug_frame", ".zdebug_frame" },
274 { ".eh_frame", NULL },
24d3216f
TT
275 { ".gdb_index", ".zgdb_index" },
276 23
251d32d9 277};
c906108c
SS
278
279/* local data types */
280
0963b4bd 281/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
282#ifndef ABBREV_HASH_SIZE
283#define ABBREV_HASH_SIZE 121
284#endif
285
107d2387
AC
286/* The data in a compilation unit header, after target2host
287 translation, looks like this. */
c906108c 288struct comp_unit_head
a738430d 289{
c764a876 290 unsigned int length;
a738430d 291 short version;
a738430d
MK
292 unsigned char addr_size;
293 unsigned char signed_addr_p;
9cbfa09e 294 unsigned int abbrev_offset;
57349743 295
a738430d
MK
296 /* Size of file offsets; either 4 or 8. */
297 unsigned int offset_size;
57349743 298
a738430d
MK
299 /* Size of the length field; either 4 or 12. */
300 unsigned int initial_length_size;
57349743 301
a738430d
MK
302 /* Offset to the first byte of this compilation unit header in the
303 .debug_info section, for resolving relative reference dies. */
304 unsigned int offset;
57349743 305
d00adf39
DE
306 /* Offset to first die in this cu from the start of the cu.
307 This will be the first byte following the compilation unit header. */
308 unsigned int first_die_offset;
a738430d 309};
c906108c 310
3da10d80
KS
311/* Type used for delaying computation of method physnames.
312 See comments for compute_delayed_physnames. */
313struct delayed_method_info
314{
315 /* The type to which the method is attached, i.e., its parent class. */
316 struct type *type;
317
318 /* The index of the method in the type's function fieldlists. */
319 int fnfield_index;
320
321 /* The index of the method in the fieldlist. */
322 int index;
323
324 /* The name of the DIE. */
325 const char *name;
326
327 /* The DIE associated with this method. */
328 struct die_info *die;
329};
330
331typedef struct delayed_method_info delayed_method_info;
332DEF_VEC_O (delayed_method_info);
333
e7c27a73
DJ
334/* Internal state when decoding a particular compilation unit. */
335struct dwarf2_cu
336{
337 /* The objfile containing this compilation unit. */
338 struct objfile *objfile;
339
d00adf39 340 /* The header of the compilation unit. */
e7c27a73 341 struct comp_unit_head header;
e142c38c 342
d00adf39
DE
343 /* Base address of this compilation unit. */
344 CORE_ADDR base_address;
345
346 /* Non-zero if base_address has been set. */
347 int base_known;
348
e142c38c
DJ
349 struct function_range *first_fn, *last_fn, *cached_fn;
350
351 /* The language we are debugging. */
352 enum language language;
353 const struct language_defn *language_defn;
354
b0f35d58
DL
355 const char *producer;
356
e142c38c
DJ
357 /* The generic symbol table building routines have separate lists for
358 file scope symbols and all all other scopes (local scopes). So
359 we need to select the right one to pass to add_symbol_to_list().
360 We do it by keeping a pointer to the correct list in list_in_scope.
361
362 FIXME: The original dwarf code just treated the file scope as the
363 first local scope, and all other local scopes as nested local
364 scopes, and worked fine. Check to see if we really need to
365 distinguish these in buildsym.c. */
366 struct pending **list_in_scope;
367
f3dd6933
DJ
368 /* DWARF abbreviation table associated with this compilation unit. */
369 struct abbrev_info **dwarf2_abbrevs;
370
371 /* Storage for the abbrev table. */
372 struct obstack abbrev_obstack;
72bf9492
DJ
373
374 /* Hash table holding all the loaded partial DIEs. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
ae038cb0
DJ
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
10b3939b 393 /* A hash table of die offsets for following references. */
51545339 394 htab_t die_hash;
10b3939b
DJ
395
396 /* Full DIEs if read in. */
397 struct die_info *dies;
398
399 /* A set of pointers to dwarf2_per_cu_data objects for compilation
400 units referenced by this one. Only set during full symbol processing;
401 partial symbol tables do not have dependencies. */
402 htab_t dependencies;
403
cb1df416
DJ
404 /* Header data from the line table, during full symbol processing. */
405 struct line_header *line_header;
406
3da10d80
KS
407 /* A list of methods which need to have physnames computed
408 after all type information has been read. */
409 VEC (delayed_method_info) *method_list;
410
96408a79
SA
411 /* To be copied to symtab->call_site_htab. */
412 htab_t call_site_htab;
413
ae038cb0
DJ
414 /* Mark used when releasing cached dies. */
415 unsigned int mark : 1;
416
417 /* This flag will be set if this compilation unit might include
418 inter-compilation-unit references. */
419 unsigned int has_form_ref_addr : 1;
420
72bf9492
DJ
421 /* This flag will be set if this compilation unit includes any
422 DW_TAG_namespace DIEs. If we know that there are explicit
423 DIEs for namespaces, we don't need to try to infer them
424 from mangled names. */
425 unsigned int has_namespace_info : 1;
8be455d7
JK
426
427 /* This CU references .debug_loc. See the symtab->locations_valid field.
428 This test is imperfect as there may exist optimized debug code not using
429 any location list and still facing inlining issues if handled as
430 unoptimized code. For a future better test see GCC PR other/32998. */
431
432 unsigned int has_loclist : 1;
e7c27a73
DJ
433};
434
10b3939b
DJ
435/* Persistent data held for a compilation unit, even when not
436 processing it. We put a pointer to this structure in the
437 read_symtab_private field of the psymtab. If we encounter
438 inter-compilation-unit references, we also maintain a sorted
439 list of all compilation units. */
440
ae038cb0
DJ
441struct dwarf2_per_cu_data
442{
348e048f 443 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 444 bytes should suffice to store the length of any compilation unit
45452591
DE
445 - if it doesn't, GDB will fall over anyway.
446 NOTE: Unlike comp_unit_head.length, this length includes
447 initial_length_size. */
c764a876 448 unsigned int offset;
348e048f 449 unsigned int length : 29;
ae038cb0
DJ
450
451 /* Flag indicating this compilation unit will be read in before
452 any of the current compilation units are processed. */
c764a876 453 unsigned int queued : 1;
ae038cb0 454
5afb4e99
DJ
455 /* This flag will be set if we need to load absolutely all DIEs
456 for this compilation unit, instead of just the ones we think
457 are interesting. It gets set if we look for a DIE in the
458 hash table and don't find it. */
459 unsigned int load_all_dies : 1;
460
8b70b953
TT
461 /* Non-null if this CU is from .debug_types; in which case it points
462 to the section. Otherwise it's from .debug_info. */
463 struct dwarf2_section_info *debug_type_section;
348e048f 464
17ea53c3
JK
465 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
466 of the CU cache it gets reset to NULL again. */
ae038cb0 467 struct dwarf2_cu *cu;
1c379e20 468
9291a0cd
TT
469 /* The corresponding objfile. */
470 struct objfile *objfile;
471
472 /* When using partial symbol tables, the 'psymtab' field is active.
473 Otherwise the 'quick' field is active. */
474 union
475 {
476 /* The partial symbol table associated with this compilation unit,
477 or NULL for partial units (which do not have an associated
478 symtab). */
479 struct partial_symtab *psymtab;
480
481 /* Data needed by the "quick" functions. */
482 struct dwarf2_per_cu_quick_data *quick;
483 } v;
ae038cb0
DJ
484};
485
348e048f
DE
486/* Entry in the signatured_types hash table. */
487
488struct signatured_type
489{
490 ULONGEST signature;
491
348e048f
DE
492 /* Offset in .debug_types of the type defined by this TU. */
493 unsigned int type_offset;
494
495 /* The CU(/TU) of this type. */
496 struct dwarf2_per_cu_data per_cu;
497};
498
0963b4bd
MS
499/* Struct used to pass misc. parameters to read_die_and_children, et
500 al. which are used for both .debug_info and .debug_types dies.
501 All parameters here are unchanging for the life of the call. This
502 struct exists to abstract away the constant parameters of die
503 reading. */
93311388
DE
504
505struct die_reader_specs
506{
507 /* The bfd of this objfile. */
508 bfd* abfd;
509
510 /* The CU of the DIE we are parsing. */
511 struct dwarf2_cu *cu;
512
513 /* Pointer to start of section buffer.
514 This is either the start of .debug_info or .debug_types. */
515 const gdb_byte *buffer;
516};
517
debd256d
JB
518/* The line number information for a compilation unit (found in the
519 .debug_line section) begins with a "statement program header",
520 which contains the following information. */
521struct line_header
522{
523 unsigned int total_length;
524 unsigned short version;
525 unsigned int header_length;
526 unsigned char minimum_instruction_length;
2dc7f7b3 527 unsigned char maximum_ops_per_instruction;
debd256d
JB
528 unsigned char default_is_stmt;
529 int line_base;
530 unsigned char line_range;
531 unsigned char opcode_base;
532
533 /* standard_opcode_lengths[i] is the number of operands for the
534 standard opcode whose value is i. This means that
535 standard_opcode_lengths[0] is unused, and the last meaningful
536 element is standard_opcode_lengths[opcode_base - 1]. */
537 unsigned char *standard_opcode_lengths;
538
539 /* The include_directories table. NOTE! These strings are not
540 allocated with xmalloc; instead, they are pointers into
541 debug_line_buffer. If you try to free them, `free' will get
542 indigestion. */
543 unsigned int num_include_dirs, include_dirs_size;
544 char **include_dirs;
545
546 /* The file_names table. NOTE! These strings are not allocated
547 with xmalloc; instead, they are pointers into debug_line_buffer.
548 Don't try to free them directly. */
549 unsigned int num_file_names, file_names_size;
550 struct file_entry
c906108c 551 {
debd256d
JB
552 char *name;
553 unsigned int dir_index;
554 unsigned int mod_time;
555 unsigned int length;
aaa75496 556 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 557 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
558 } *file_names;
559
560 /* The start and end of the statement program following this
6502dd73 561 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 562 gdb_byte *statement_program_start, *statement_program_end;
debd256d 563};
c906108c
SS
564
565/* When we construct a partial symbol table entry we only
0963b4bd 566 need this much information. */
c906108c
SS
567struct partial_die_info
568 {
72bf9492 569 /* Offset of this DIE. */
c906108c 570 unsigned int offset;
72bf9492
DJ
571
572 /* DWARF-2 tag for this DIE. */
573 ENUM_BITFIELD(dwarf_tag) tag : 16;
574
72bf9492
DJ
575 /* Assorted flags describing the data found in this DIE. */
576 unsigned int has_children : 1;
577 unsigned int is_external : 1;
578 unsigned int is_declaration : 1;
579 unsigned int has_type : 1;
580 unsigned int has_specification : 1;
581 unsigned int has_pc_info : 1;
582
583 /* Flag set if the SCOPE field of this structure has been
584 computed. */
585 unsigned int scope_set : 1;
586
fa4028e9
JB
587 /* Flag set if the DIE has a byte_size attribute. */
588 unsigned int has_byte_size : 1;
589
98bfdba5
PA
590 /* Flag set if any of the DIE's children are template arguments. */
591 unsigned int has_template_arguments : 1;
592
abc72ce4
DE
593 /* Flag set if fixup_partial_die has been called on this die. */
594 unsigned int fixup_called : 1;
595
72bf9492 596 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 597 sometimes a default name for unnamed DIEs. */
c906108c 598 char *name;
72bf9492 599
abc72ce4
DE
600 /* The linkage name, if present. */
601 const char *linkage_name;
602
72bf9492
DJ
603 /* The scope to prepend to our children. This is generally
604 allocated on the comp_unit_obstack, so will disappear
605 when this compilation unit leaves the cache. */
606 char *scope;
607
608 /* The location description associated with this DIE, if any. */
609 struct dwarf_block *locdesc;
610
611 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
612 CORE_ADDR lowpc;
613 CORE_ADDR highpc;
72bf9492 614
93311388 615 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 616 DW_AT_sibling, if any. */
abc72ce4
DE
617 /* NOTE: This member isn't strictly necessary, read_partial_die could
618 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 619 gdb_byte *sibling;
72bf9492
DJ
620
621 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
622 DW_AT_specification (or DW_AT_abstract_origin or
623 DW_AT_extension). */
624 unsigned int spec_offset;
625
626 /* Pointers to this DIE's parent, first child, and next sibling,
627 if any. */
628 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
629 };
630
0963b4bd 631/* This data structure holds the information of an abbrev. */
c906108c
SS
632struct abbrev_info
633 {
634 unsigned int number; /* number identifying abbrev */
635 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
636 unsigned short has_children; /* boolean */
637 unsigned short num_attrs; /* number of attributes */
c906108c
SS
638 struct attr_abbrev *attrs; /* an array of attribute descriptions */
639 struct abbrev_info *next; /* next in chain */
640 };
641
642struct attr_abbrev
643 {
9d25dd43
DE
644 ENUM_BITFIELD(dwarf_attribute) name : 16;
645 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
646 };
647
0963b4bd 648/* Attributes have a name and a value. */
b60c80d6
DJ
649struct attribute
650 {
9d25dd43 651 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
652 ENUM_BITFIELD(dwarf_form) form : 15;
653
654 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
655 field should be in u.str (existing only for DW_STRING) but it is kept
656 here for better struct attribute alignment. */
657 unsigned int string_is_canonical : 1;
658
b60c80d6
DJ
659 union
660 {
661 char *str;
662 struct dwarf_block *blk;
43bbcdc2
PH
663 ULONGEST unsnd;
664 LONGEST snd;
b60c80d6 665 CORE_ADDR addr;
348e048f 666 struct signatured_type *signatured_type;
b60c80d6
DJ
667 }
668 u;
669 };
670
0963b4bd 671/* This data structure holds a complete die structure. */
c906108c
SS
672struct die_info
673 {
76815b17
DE
674 /* DWARF-2 tag for this DIE. */
675 ENUM_BITFIELD(dwarf_tag) tag : 16;
676
677 /* Number of attributes */
98bfdba5
PA
678 unsigned char num_attrs;
679
680 /* True if we're presently building the full type name for the
681 type derived from this DIE. */
682 unsigned char building_fullname : 1;
76815b17
DE
683
684 /* Abbrev number */
685 unsigned int abbrev;
686
93311388 687 /* Offset in .debug_info or .debug_types section. */
76815b17 688 unsigned int offset;
78ba4af6
JB
689
690 /* The dies in a compilation unit form an n-ary tree. PARENT
691 points to this die's parent; CHILD points to the first child of
692 this node; and all the children of a given node are chained
4950bc1c 693 together via their SIBLING fields. */
639d11d3
DC
694 struct die_info *child; /* Its first child, if any. */
695 struct die_info *sibling; /* Its next sibling, if any. */
696 struct die_info *parent; /* Its parent, if any. */
c906108c 697
b60c80d6
DJ
698 /* An array of attributes, with NUM_ATTRS elements. There may be
699 zero, but it's not common and zero-sized arrays are not
700 sufficiently portable C. */
701 struct attribute attrs[1];
c906108c
SS
702 };
703
5fb290d7
DJ
704struct function_range
705{
706 const char *name;
707 CORE_ADDR lowpc, highpc;
708 int seen_line;
709 struct function_range *next;
710};
711
0963b4bd 712/* Get at parts of an attribute structure. */
c906108c
SS
713
714#define DW_STRING(attr) ((attr)->u.str)
8285870a 715#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
716#define DW_UNSND(attr) ((attr)->u.unsnd)
717#define DW_BLOCK(attr) ((attr)->u.blk)
718#define DW_SND(attr) ((attr)->u.snd)
719#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 720#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 721
0963b4bd 722/* Blocks are a bunch of untyped bytes. */
c906108c
SS
723struct dwarf_block
724 {
725 unsigned int size;
1d6edc3c
JK
726
727 /* Valid only if SIZE is not zero. */
fe1b8b76 728 gdb_byte *data;
c906108c
SS
729 };
730
c906108c
SS
731#ifndef ATTR_ALLOC_CHUNK
732#define ATTR_ALLOC_CHUNK 4
733#endif
734
c906108c
SS
735/* Allocate fields for structs, unions and enums in this size. */
736#ifndef DW_FIELD_ALLOC_CHUNK
737#define DW_FIELD_ALLOC_CHUNK 4
738#endif
739
c906108c
SS
740/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
741 but this would require a corresponding change in unpack_field_as_long
742 and friends. */
743static int bits_per_byte = 8;
744
745/* The routines that read and process dies for a C struct or C++ class
746 pass lists of data member fields and lists of member function fields
747 in an instance of a field_info structure, as defined below. */
748struct field_info
c5aa993b 749 {
0963b4bd 750 /* List of data member and baseclasses fields. */
c5aa993b
JM
751 struct nextfield
752 {
753 struct nextfield *next;
754 int accessibility;
755 int virtuality;
756 struct field field;
757 }
7d0ccb61 758 *fields, *baseclasses;
c906108c 759
7d0ccb61 760 /* Number of fields (including baseclasses). */
c5aa993b 761 int nfields;
c906108c 762
c5aa993b
JM
763 /* Number of baseclasses. */
764 int nbaseclasses;
c906108c 765
c5aa993b
JM
766 /* Set if the accesibility of one of the fields is not public. */
767 int non_public_fields;
c906108c 768
c5aa993b
JM
769 /* Member function fields array, entries are allocated in the order they
770 are encountered in the object file. */
771 struct nextfnfield
772 {
773 struct nextfnfield *next;
774 struct fn_field fnfield;
775 }
776 *fnfields;
c906108c 777
c5aa993b
JM
778 /* Member function fieldlist array, contains name of possibly overloaded
779 member function, number of overloaded member functions and a pointer
780 to the head of the member function field chain. */
781 struct fnfieldlist
782 {
783 char *name;
784 int length;
785 struct nextfnfield *head;
786 }
787 *fnfieldlists;
c906108c 788
c5aa993b
JM
789 /* Number of entries in the fnfieldlists array. */
790 int nfnfields;
98751a41
JK
791
792 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
793 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
794 struct typedef_field_list
795 {
796 struct typedef_field field;
797 struct typedef_field_list *next;
798 }
799 *typedef_field_list;
800 unsigned typedef_field_list_count;
c5aa993b 801 };
c906108c 802
10b3939b
DJ
803/* One item on the queue of compilation units to read in full symbols
804 for. */
805struct dwarf2_queue_item
806{
807 struct dwarf2_per_cu_data *per_cu;
808 struct dwarf2_queue_item *next;
809};
810
811/* The current queue. */
812static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
813
ae038cb0
DJ
814/* Loaded secondary compilation units are kept in memory until they
815 have not been referenced for the processing of this many
816 compilation units. Set this to zero to disable caching. Cache
817 sizes of up to at least twenty will improve startup time for
818 typical inter-CU-reference binaries, at an obvious memory cost. */
819static int dwarf2_max_cache_age = 5;
920d2a44
AC
820static void
821show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
822 struct cmd_list_element *c, const char *value)
823{
3e43a32a
MS
824 fprintf_filtered (file, _("The upper bound on the age of cached "
825 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
826 value);
827}
828
ae038cb0 829
0963b4bd 830/* Various complaints about symbol reading that don't abort the process. */
c906108c 831
4d3c2250
KB
832static void
833dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 834{
4d3c2250 835 complaint (&symfile_complaints,
e2e0b3e5 836 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
837}
838
25e43795
DJ
839static void
840dwarf2_debug_line_missing_file_complaint (void)
841{
842 complaint (&symfile_complaints,
843 _(".debug_line section has line data without a file"));
844}
845
59205f5a
JB
846static void
847dwarf2_debug_line_missing_end_sequence_complaint (void)
848{
849 complaint (&symfile_complaints,
3e43a32a
MS
850 _(".debug_line section has line "
851 "program sequence without an end"));
59205f5a
JB
852}
853
4d3c2250
KB
854static void
855dwarf2_complex_location_expr_complaint (void)
2e276125 856{
e2e0b3e5 857 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
858}
859
4d3c2250
KB
860static void
861dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
862 int arg3)
2e276125 863{
4d3c2250 864 complaint (&symfile_complaints,
3e43a32a
MS
865 _("const value length mismatch for '%s', got %d, expected %d"),
866 arg1, arg2, arg3);
4d3c2250
KB
867}
868
869static void
cf2c3c16 870dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 871{
4d3c2250 872 complaint (&symfile_complaints,
cf2c3c16
TT
873 _("macro info runs off end of `%s' section"),
874 section->asection->name);
4d3c2250
KB
875}
876
877static void
878dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 879{
4d3c2250 880 complaint (&symfile_complaints,
3e43a32a
MS
881 _("macro debug info contains a "
882 "malformed macro definition:\n`%s'"),
4d3c2250
KB
883 arg1);
884}
885
886static void
887dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 888{
4d3c2250 889 complaint (&symfile_complaints,
3e43a32a
MS
890 _("invalid attribute class or form for '%s' in '%s'"),
891 arg1, arg2);
4d3c2250 892}
c906108c 893
c906108c
SS
894/* local function prototypes */
895
4efb68b1 896static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 897
aaa75496
JB
898static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
899 struct objfile *);
900
918dd910
JK
901static void dwarf2_find_base_address (struct die_info *die,
902 struct dwarf2_cu *cu);
903
c67a9c90 904static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 905
72bf9492
DJ
906static void scan_partial_symbols (struct partial_die_info *,
907 CORE_ADDR *, CORE_ADDR *,
5734ee8b 908 int, struct dwarf2_cu *);
c906108c 909
72bf9492
DJ
910static void add_partial_symbol (struct partial_die_info *,
911 struct dwarf2_cu *);
63d06c5c 912
72bf9492
DJ
913static void add_partial_namespace (struct partial_die_info *pdi,
914 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 915 int need_pc, struct dwarf2_cu *cu);
63d06c5c 916
5d7cb8df
JK
917static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
918 CORE_ADDR *highpc, int need_pc,
919 struct dwarf2_cu *cu);
920
72bf9492
DJ
921static void add_partial_enumeration (struct partial_die_info *enum_pdi,
922 struct dwarf2_cu *cu);
91c24f0a 923
bc30ff58
JB
924static void add_partial_subprogram (struct partial_die_info *pdi,
925 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 926 int need_pc, struct dwarf2_cu *cu);
bc30ff58 927
fe1b8b76 928static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
929 gdb_byte *buffer, gdb_byte *info_ptr,
930 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 931
a14ed312 932static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 933
a14ed312 934static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 935
e7c27a73 936static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 937
f3dd6933 938static void dwarf2_free_abbrev_table (void *);
c906108c 939
6caca83c
CC
940static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
941
fe1b8b76 942static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 943 struct dwarf2_cu *);
72bf9492 944
57349743 945static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 946 struct dwarf2_cu *);
c906108c 947
93311388
DE
948static struct partial_die_info *load_partial_dies (bfd *,
949 gdb_byte *, gdb_byte *,
950 int, struct dwarf2_cu *);
72bf9492 951
fe1b8b76 952static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
953 struct abbrev_info *abbrev,
954 unsigned int, bfd *,
955 gdb_byte *, gdb_byte *,
956 struct dwarf2_cu *);
c906108c 957
c764a876 958static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 959 struct dwarf2_cu *);
72bf9492
DJ
960
961static void fixup_partial_die (struct partial_die_info *,
962 struct dwarf2_cu *);
963
fe1b8b76
JB
964static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
965 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 966
fe1b8b76
JB
967static gdb_byte *read_attribute_value (struct attribute *, unsigned,
968 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 969
fe1b8b76 970static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 971
fe1b8b76 972static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 973
fe1b8b76 974static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 975
fe1b8b76 976static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 977
93311388 978static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 979
fe1b8b76 980static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 981 unsigned int *);
c906108c 982
c764a876
DE
983static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
984
985static LONGEST read_checked_initial_length_and_offset
986 (bfd *, gdb_byte *, const struct comp_unit_head *,
987 unsigned int *, unsigned int *);
613e1657 988
fe1b8b76 989static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
990 unsigned int *);
991
992static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 993
fe1b8b76 994static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 995
9b1c24c8 996static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 997
fe1b8b76
JB
998static char *read_indirect_string (bfd *, gdb_byte *,
999 const struct comp_unit_head *,
1000 unsigned int *);
4bdf3d34 1001
fe1b8b76 1002static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1003
fe1b8b76 1004static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1005
fe1b8b76 1006static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1007
e142c38c 1008static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1009
e142c38c
DJ
1010static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1011 struct dwarf2_cu *);
c906108c 1012
348e048f
DE
1013static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1014 unsigned int,
1015 struct dwarf2_cu *);
1016
05cf31d1
JB
1017static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1018 struct dwarf2_cu *cu);
1019
e142c38c 1020static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1021
e142c38c 1022static struct die_info *die_specification (struct die_info *die,
f2f0e013 1023 struct dwarf2_cu **);
63d06c5c 1024
debd256d
JB
1025static void free_line_header (struct line_header *lh);
1026
aaa75496
JB
1027static void add_file_name (struct line_header *, char *, unsigned int,
1028 unsigned int, unsigned int);
1029
debd256d
JB
1030static struct line_header *(dwarf_decode_line_header
1031 (unsigned int offset,
e7c27a73 1032 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1033
72b9f47f 1034static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1035 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1036
72b9f47f 1037static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1038
a14ed312 1039static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1040 struct dwarf2_cu *);
c906108c 1041
34eaf542
TT
1042static struct symbol *new_symbol_full (struct die_info *, struct type *,
1043 struct dwarf2_cu *, struct symbol *);
1044
a14ed312 1045static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1046 struct dwarf2_cu *);
c906108c 1047
98bfdba5
PA
1048static void dwarf2_const_value_attr (struct attribute *attr,
1049 struct type *type,
1050 const char *name,
1051 struct obstack *obstack,
1052 struct dwarf2_cu *cu, long *value,
1053 gdb_byte **bytes,
1054 struct dwarf2_locexpr_baton **baton);
2df3850c 1055
e7c27a73 1056static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1057
b4ba55a1
JB
1058static int need_gnat_info (struct dwarf2_cu *);
1059
3e43a32a
MS
1060static struct type *die_descriptive_type (struct die_info *,
1061 struct dwarf2_cu *);
b4ba55a1
JB
1062
1063static void set_descriptive_type (struct type *, struct die_info *,
1064 struct dwarf2_cu *);
1065
e7c27a73
DJ
1066static struct type *die_containing_type (struct die_info *,
1067 struct dwarf2_cu *);
c906108c 1068
673bfd45
DE
1069static struct type *lookup_die_type (struct die_info *, struct attribute *,
1070 struct dwarf2_cu *);
c906108c 1071
f792889a 1072static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1073
673bfd45
DE
1074static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1075
086ed43d 1076static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1077
6e70227d 1078static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1079 const char *suffix, int physname,
1080 struct dwarf2_cu *cu);
63d06c5c 1081
e7c27a73 1082static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1083
348e048f
DE
1084static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1085
e7c27a73 1086static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1087
e7c27a73 1088static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1089
96408a79
SA
1090static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1091
ff013f42
JK
1092static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1093 struct dwarf2_cu *, struct partial_symtab *);
1094
a14ed312 1095static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1096 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1097 struct partial_symtab *);
c906108c 1098
fae299cd
DC
1099static void get_scope_pc_bounds (struct die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
1101 struct dwarf2_cu *);
1102
801e3a5b
JB
1103static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1104 CORE_ADDR, struct dwarf2_cu *);
1105
a14ed312 1106static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1107 struct dwarf2_cu *);
c906108c 1108
a14ed312 1109static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1110 struct type *, struct dwarf2_cu *);
c906108c 1111
a14ed312 1112static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1113 struct die_info *, struct type *,
e7c27a73 1114 struct dwarf2_cu *);
c906108c 1115
a14ed312 1116static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1117 struct type *,
1118 struct dwarf2_cu *);
c906108c 1119
134d01f1 1120static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1121
e7c27a73 1122static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1123
e7c27a73 1124static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1125
5d7cb8df
JK
1126static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1127
27aa8d6a
SW
1128static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1129
f55ee35c
JK
1130static struct type *read_module_type (struct die_info *die,
1131 struct dwarf2_cu *cu);
1132
38d518c9 1133static const char *namespace_name (struct die_info *die,
e142c38c 1134 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1135
134d01f1 1136static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1137
e7c27a73 1138static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1139
6e70227d 1140static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1141 struct dwarf2_cu *);
1142
93311388 1143static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1144
93311388
DE
1145static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1146 gdb_byte *info_ptr,
d97bc12b
DE
1147 gdb_byte **new_info_ptr,
1148 struct die_info *parent);
1149
93311388
DE
1150static struct die_info *read_die_and_children (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 struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1156 gdb_byte *info_ptr,
fe1b8b76 1157 gdb_byte **new_info_ptr,
639d11d3
DC
1158 struct die_info *parent);
1159
93311388
DE
1160static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1161 struct die_info **, gdb_byte *,
1162 int *);
1163
e7c27a73 1164static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1165
71c25dea
TT
1166static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1167 struct obstack *);
1168
e142c38c 1169static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1170
98bfdba5
PA
1171static const char *dwarf2_full_name (char *name,
1172 struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
e142c38c 1175static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1176 struct dwarf2_cu **);
9219021c 1177
a14ed312 1178static char *dwarf_tag_name (unsigned int);
c906108c 1179
a14ed312 1180static char *dwarf_attr_name (unsigned int);
c906108c 1181
a14ed312 1182static char *dwarf_form_name (unsigned int);
c906108c 1183
a14ed312 1184static char *dwarf_bool_name (unsigned int);
c906108c 1185
a14ed312 1186static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1187
1188#if 0
a14ed312 1189static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1190#endif
1191
f9aca02d 1192static struct die_info *sibling_die (struct die_info *);
c906108c 1193
d97bc12b
DE
1194static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1195
1196static void dump_die_for_error (struct die_info *);
1197
1198static void dump_die_1 (struct ui_file *, int level, int max_level,
1199 struct die_info *);
c906108c 1200
d97bc12b 1201/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1202
51545339 1203static void store_in_ref_table (struct die_info *,
10b3939b 1204 struct dwarf2_cu *);
c906108c 1205
93311388
DE
1206static int is_ref_attr (struct attribute *);
1207
c764a876 1208static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1209
43bbcdc2 1210static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1211
348e048f
DE
1212static struct die_info *follow_die_ref_or_sig (struct die_info *,
1213 struct attribute *,
1214 struct dwarf2_cu **);
1215
10b3939b
DJ
1216static struct die_info *follow_die_ref (struct die_info *,
1217 struct attribute *,
f2f0e013 1218 struct dwarf2_cu **);
c906108c 1219
348e048f
DE
1220static struct die_info *follow_die_sig (struct die_info *,
1221 struct attribute *,
1222 struct dwarf2_cu **);
1223
6c83ed52
TT
1224static struct signatured_type *lookup_signatured_type_at_offset
1225 (struct objfile *objfile,
1226 struct dwarf2_section_info *section,
1227 unsigned int offset);
1228
348e048f 1229static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1230 struct dwarf2_section_info *sect,
348e048f
DE
1231 unsigned int offset);
1232
1233static void read_signatured_type (struct objfile *,
1234 struct signatured_type *type_sig);
1235
c906108c
SS
1236/* memory allocation interface */
1237
7b5a2f43 1238static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1239
f3dd6933 1240static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1241
b60c80d6 1242static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1243
e142c38c 1244static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1245
e142c38c
DJ
1246static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1247 struct dwarf2_cu *);
5fb290d7 1248
2e276125 1249static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1250 char *, bfd *, struct dwarf2_cu *,
1251 struct dwarf2_section_info *,
1252 int);
2e276125 1253
8e19ed76
PS
1254static int attr_form_is_block (struct attribute *);
1255
3690dd37
JB
1256static int attr_form_is_section_offset (struct attribute *);
1257
1258static int attr_form_is_constant (struct attribute *);
1259
8cf6f0b1
TT
1260static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1261 struct dwarf2_loclist_baton *baton,
1262 struct attribute *attr);
1263
93e7bd98
DJ
1264static void dwarf2_symbol_mark_computed (struct attribute *attr,
1265 struct symbol *sym,
1266 struct dwarf2_cu *cu);
4c2df51b 1267
93311388
DE
1268static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1269 struct abbrev_info *abbrev,
1270 struct dwarf2_cu *cu);
4bb7a0a7 1271
72bf9492
DJ
1272static void free_stack_comp_unit (void *);
1273
72bf9492
DJ
1274static hashval_t partial_die_hash (const void *item);
1275
1276static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1277
ae038cb0 1278static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1279 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1280
1281static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1282 (unsigned int offset, struct objfile *objfile);
ae038cb0 1283
9816fde3
JK
1284static void init_one_comp_unit (struct dwarf2_cu *cu,
1285 struct objfile *objfile);
1286
1287static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1288 struct die_info *comp_unit_die);
93311388 1289
ae038cb0
DJ
1290static void free_one_comp_unit (void *);
1291
1292static void free_cached_comp_units (void *);
1293
1294static void age_cached_comp_units (void);
1295
1296static void free_one_cached_comp_unit (void *);
1297
f792889a
DJ
1298static struct type *set_die_type (struct die_info *, struct type *,
1299 struct dwarf2_cu *);
1c379e20 1300
ae038cb0
DJ
1301static void create_all_comp_units (struct objfile *);
1302
1fd400ff
TT
1303static int create_debug_types_hash_table (struct objfile *objfile);
1304
93311388
DE
1305static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1306 struct objfile *);
10b3939b
DJ
1307
1308static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1309
1310static void dwarf2_add_dependence (struct dwarf2_cu *,
1311 struct dwarf2_per_cu_data *);
1312
ae038cb0
DJ
1313static void dwarf2_mark (struct dwarf2_cu *);
1314
1315static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1316
673bfd45
DE
1317static struct type *get_die_type_at_offset (unsigned int,
1318 struct dwarf2_per_cu_data *per_cu);
1319
f792889a 1320static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1321
9291a0cd
TT
1322static void dwarf2_release_queue (void *dummy);
1323
1324static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1325 struct objfile *objfile);
1326
1327static void process_queue (struct objfile *objfile);
1328
1329static void find_file_and_directory (struct die_info *die,
1330 struct dwarf2_cu *cu,
1331 char **name, char **comp_dir);
1332
1333static char *file_full_name (int file, struct line_header *lh,
1334 const char *comp_dir);
1335
1336static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1337 gdb_byte *info_ptr,
1338 gdb_byte *buffer,
1339 unsigned int buffer_size,
460c1c54
CC
1340 bfd *abfd,
1341 int is_debug_type_section);
9291a0cd
TT
1342
1343static void init_cu_die_reader (struct die_reader_specs *reader,
1344 struct dwarf2_cu *cu);
1345
673bfd45 1346static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1347
9291a0cd
TT
1348#if WORDS_BIGENDIAN
1349
1350/* Convert VALUE between big- and little-endian. */
1351static offset_type
1352byte_swap (offset_type value)
1353{
1354 offset_type result;
1355
1356 result = (value & 0xff) << 24;
1357 result |= (value & 0xff00) << 8;
1358 result |= (value & 0xff0000) >> 8;
1359 result |= (value & 0xff000000) >> 24;
1360 return result;
1361}
1362
1363#define MAYBE_SWAP(V) byte_swap (V)
1364
1365#else
1366#define MAYBE_SWAP(V) (V)
1367#endif /* WORDS_BIGENDIAN */
1368
1369/* The suffix for an index file. */
1370#define INDEX_SUFFIX ".gdb-index"
1371
3da10d80
KS
1372static const char *dwarf2_physname (char *name, struct die_info *die,
1373 struct dwarf2_cu *cu);
1374
c906108c 1375/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1376 information and return true if we have enough to do something.
1377 NAMES points to the dwarf2 section names, or is NULL if the standard
1378 ELF names are used. */
c906108c
SS
1379
1380int
251d32d9
TG
1381dwarf2_has_info (struct objfile *objfile,
1382 const struct dwarf2_debug_sections *names)
c906108c 1383{
be391dca
TT
1384 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1385 if (!dwarf2_per_objfile)
1386 {
1387 /* Initialize per-objfile state. */
1388 struct dwarf2_per_objfile *data
1389 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1390
be391dca
TT
1391 memset (data, 0, sizeof (*data));
1392 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1393 dwarf2_per_objfile = data;
6502dd73 1394
251d32d9
TG
1395 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1396 (void *) names);
be391dca
TT
1397 dwarf2_per_objfile->objfile = objfile;
1398 }
1399 return (dwarf2_per_objfile->info.asection != NULL
1400 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1401}
1402
251d32d9
TG
1403/* When loading sections, we look either for uncompressed section or for
1404 compressed section names. */
233a11ab
CS
1405
1406static int
251d32d9
TG
1407section_is_p (const char *section_name,
1408 const struct dwarf2_section_names *names)
233a11ab 1409{
251d32d9
TG
1410 if (names->normal != NULL
1411 && strcmp (section_name, names->normal) == 0)
1412 return 1;
1413 if (names->compressed != NULL
1414 && strcmp (section_name, names->compressed) == 0)
1415 return 1;
1416 return 0;
233a11ab
CS
1417}
1418
c906108c
SS
1419/* This function is mapped across the sections and remembers the
1420 offset and size of each of the debugging sections we are interested
1421 in. */
1422
1423static void
251d32d9 1424dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1425{
251d32d9
TG
1426 const struct dwarf2_debug_sections *names;
1427
1428 if (vnames == NULL)
1429 names = &dwarf2_elf_names;
1430 else
1431 names = (const struct dwarf2_debug_sections *) vnames;
1432
1433 if (section_is_p (sectp->name, &names->info))
c906108c 1434 {
dce234bc
PP
1435 dwarf2_per_objfile->info.asection = sectp;
1436 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1437 }
251d32d9 1438 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1439 {
dce234bc
PP
1440 dwarf2_per_objfile->abbrev.asection = sectp;
1441 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1442 }
251d32d9 1443 else if (section_is_p (sectp->name, &names->line))
c906108c 1444 {
dce234bc
PP
1445 dwarf2_per_objfile->line.asection = sectp;
1446 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1447 }
251d32d9 1448 else if (section_is_p (sectp->name, &names->loc))
c906108c 1449 {
dce234bc
PP
1450 dwarf2_per_objfile->loc.asection = sectp;
1451 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1452 }
251d32d9 1453 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1454 {
dce234bc
PP
1455 dwarf2_per_objfile->macinfo.asection = sectp;
1456 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1457 }
cf2c3c16
TT
1458 else if (section_is_p (sectp->name, &names->macro))
1459 {
1460 dwarf2_per_objfile->macro.asection = sectp;
1461 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1462 }
251d32d9 1463 else if (section_is_p (sectp->name, &names->str))
c906108c 1464 {
dce234bc
PP
1465 dwarf2_per_objfile->str.asection = sectp;
1466 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1467 }
251d32d9 1468 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1469 {
dce234bc
PP
1470 dwarf2_per_objfile->frame.asection = sectp;
1471 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1472 }
251d32d9 1473 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1474 {
3799ccc6 1475 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1476
3799ccc6
EZ
1477 if (aflag & SEC_HAS_CONTENTS)
1478 {
dce234bc
PP
1479 dwarf2_per_objfile->eh_frame.asection = sectp;
1480 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1481 }
b6af0555 1482 }
251d32d9 1483 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1484 {
dce234bc
PP
1485 dwarf2_per_objfile->ranges.asection = sectp;
1486 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1487 }
251d32d9 1488 else if (section_is_p (sectp->name, &names->types))
348e048f 1489 {
8b70b953
TT
1490 struct dwarf2_section_info type_section;
1491
1492 memset (&type_section, 0, sizeof (type_section));
1493 type_section.asection = sectp;
1494 type_section.size = bfd_get_section_size (sectp);
1495
1496 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1497 &type_section);
348e048f 1498 }
251d32d9 1499 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1500 {
1501 dwarf2_per_objfile->gdb_index.asection = sectp;
1502 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1503 }
dce234bc 1504
72dca2f5
FR
1505 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1506 && bfd_section_vma (abfd, sectp) == 0)
1507 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1508}
1509
dce234bc
PP
1510/* Decompress a section that was compressed using zlib. Store the
1511 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1512
1513static void
dce234bc
PP
1514zlib_decompress_section (struct objfile *objfile, asection *sectp,
1515 gdb_byte **outbuf, bfd_size_type *outsize)
1516{
1517 bfd *abfd = objfile->obfd;
1518#ifndef HAVE_ZLIB_H
1519 error (_("Support for zlib-compressed DWARF data (from '%s') "
1520 "is disabled in this copy of GDB"),
1521 bfd_get_filename (abfd));
1522#else
1523 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1524 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1525 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1526 bfd_size_type uncompressed_size;
1527 gdb_byte *uncompressed_buffer;
1528 z_stream strm;
1529 int rc;
1530 int header_size = 12;
1531
1532 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1533 || bfd_bread (compressed_buffer,
1534 compressed_size, abfd) != compressed_size)
dce234bc
PP
1535 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1536 bfd_get_filename (abfd));
1537
1538 /* Read the zlib header. In this case, it should be "ZLIB" followed
1539 by the uncompressed section size, 8 bytes in big-endian order. */
1540 if (compressed_size < header_size
1541 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1542 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1543 bfd_get_filename (abfd));
1544 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1546 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1547 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1548 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1549 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1550 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1551 uncompressed_size += compressed_buffer[11];
1552
1553 /* It is possible the section consists of several compressed
1554 buffers concatenated together, so we uncompress in a loop. */
1555 strm.zalloc = NULL;
1556 strm.zfree = NULL;
1557 strm.opaque = NULL;
1558 strm.avail_in = compressed_size - header_size;
1559 strm.next_in = (Bytef*) compressed_buffer + header_size;
1560 strm.avail_out = uncompressed_size;
1561 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1562 uncompressed_size);
1563 rc = inflateInit (&strm);
1564 while (strm.avail_in > 0)
1565 {
1566 if (rc != Z_OK)
1567 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1568 bfd_get_filename (abfd), rc);
1569 strm.next_out = ((Bytef*) uncompressed_buffer
1570 + (uncompressed_size - strm.avail_out));
1571 rc = inflate (&strm, Z_FINISH);
1572 if (rc != Z_STREAM_END)
1573 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1574 bfd_get_filename (abfd), rc);
1575 rc = inflateReset (&strm);
1576 }
1577 rc = inflateEnd (&strm);
1578 if (rc != Z_OK
1579 || strm.avail_out != 0)
1580 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1581 bfd_get_filename (abfd), rc);
1582
affddf13 1583 do_cleanups (cleanup);
dce234bc
PP
1584 *outbuf = uncompressed_buffer;
1585 *outsize = uncompressed_size;
1586#endif
233a11ab
CS
1587}
1588
9e0ac564
TT
1589/* A helper function that decides whether a section is empty. */
1590
1591static int
1592dwarf2_section_empty_p (struct dwarf2_section_info *info)
1593{
1594 return info->asection == NULL || info->size == 0;
1595}
1596
dce234bc
PP
1597/* Read the contents of the section SECTP from object file specified by
1598 OBJFILE, store info about the section into INFO.
1599 If the section is compressed, uncompress it before returning. */
c906108c 1600
dce234bc
PP
1601static void
1602dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1603{
dce234bc
PP
1604 bfd *abfd = objfile->obfd;
1605 asection *sectp = info->asection;
1606 gdb_byte *buf, *retbuf;
1607 unsigned char header[4];
c906108c 1608
be391dca
TT
1609 if (info->readin)
1610 return;
dce234bc 1611 info->buffer = NULL;
b315ab21 1612 info->map_addr = NULL;
be391dca 1613 info->readin = 1;
188dd5d6 1614
9e0ac564 1615 if (dwarf2_section_empty_p (info))
dce234bc 1616 return;
c906108c 1617
dce234bc
PP
1618 /* Check if the file has a 4-byte header indicating compression. */
1619 if (info->size > sizeof (header)
1620 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1621 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1622 {
1623 /* Upon decompression, update the buffer and its size. */
1624 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1625 {
1626 zlib_decompress_section (objfile, sectp, &info->buffer,
1627 &info->size);
1628 return;
1629 }
1630 }
4bdf3d34 1631
dce234bc
PP
1632#ifdef HAVE_MMAP
1633 if (pagesize == 0)
1634 pagesize = getpagesize ();
2e276125 1635
dce234bc
PP
1636 /* Only try to mmap sections which are large enough: we don't want to
1637 waste space due to fragmentation. Also, only try mmap for sections
1638 without relocations. */
1639
1640 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1641 {
b315ab21
TG
1642 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1643 MAP_PRIVATE, sectp->filepos,
1644 &info->map_addr, &info->map_len);
dce234bc 1645
b315ab21 1646 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1647 {
be391dca 1648#if HAVE_POSIX_MADVISE
b315ab21 1649 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1650#endif
dce234bc
PP
1651 return;
1652 }
1653 }
1654#endif
1655
1656 /* If we get here, we are a normal, not-compressed section. */
1657 info->buffer = buf
1658 = obstack_alloc (&objfile->objfile_obstack, info->size);
1659
1660 /* When debugging .o files, we may need to apply relocations; see
1661 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1662 We never compress sections in .o files, so we only need to
1663 try this when the section is not compressed. */
ac8035ab 1664 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1665 if (retbuf != NULL)
1666 {
1667 info->buffer = retbuf;
1668 return;
1669 }
1670
1671 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1672 || bfd_bread (buf, info->size, abfd) != info->size)
1673 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1674 bfd_get_filename (abfd));
1675}
1676
9e0ac564
TT
1677/* A helper function that returns the size of a section in a safe way.
1678 If you are positive that the section has been read before using the
1679 size, then it is safe to refer to the dwarf2_section_info object's
1680 "size" field directly. In other cases, you must call this
1681 function, because for compressed sections the size field is not set
1682 correctly until the section has been read. */
1683
1684static bfd_size_type
1685dwarf2_section_size (struct objfile *objfile,
1686 struct dwarf2_section_info *info)
1687{
1688 if (!info->readin)
1689 dwarf2_read_section (objfile, info);
1690 return info->size;
1691}
1692
dce234bc 1693/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1694 SECTION_NAME. */
af34e669 1695
dce234bc 1696void
3017a003
TG
1697dwarf2_get_section_info (struct objfile *objfile,
1698 enum dwarf2_section_enum sect,
dce234bc
PP
1699 asection **sectp, gdb_byte **bufp,
1700 bfd_size_type *sizep)
1701{
1702 struct dwarf2_per_objfile *data
1703 = objfile_data (objfile, dwarf2_objfile_data_key);
1704 struct dwarf2_section_info *info;
a3b2a86b
TT
1705
1706 /* We may see an objfile without any DWARF, in which case we just
1707 return nothing. */
1708 if (data == NULL)
1709 {
1710 *sectp = NULL;
1711 *bufp = NULL;
1712 *sizep = 0;
1713 return;
1714 }
3017a003
TG
1715 switch (sect)
1716 {
1717 case DWARF2_DEBUG_FRAME:
1718 info = &data->frame;
1719 break;
1720 case DWARF2_EH_FRAME:
1721 info = &data->eh_frame;
1722 break;
1723 default:
1724 gdb_assert_not_reached ("unexpected section");
1725 }
dce234bc 1726
9e0ac564 1727 dwarf2_read_section (objfile, info);
dce234bc
PP
1728
1729 *sectp = info->asection;
1730 *bufp = info->buffer;
1731 *sizep = info->size;
1732}
1733
9291a0cd 1734\f
7b9f3c50
DE
1735/* DWARF quick_symbols_functions support. */
1736
1737/* TUs can share .debug_line entries, and there can be a lot more TUs than
1738 unique line tables, so we maintain a separate table of all .debug_line
1739 derived entries to support the sharing.
1740 All the quick functions need is the list of file names. We discard the
1741 line_header when we're done and don't need to record it here. */
1742struct quick_file_names
1743{
1744 /* The offset in .debug_line of the line table. We hash on this. */
1745 unsigned int offset;
1746
1747 /* The number of entries in file_names, real_names. */
1748 unsigned int num_file_names;
1749
1750 /* The file names from the line table, after being run through
1751 file_full_name. */
1752 const char **file_names;
1753
1754 /* The file names from the line table after being run through
1755 gdb_realpath. These are computed lazily. */
1756 const char **real_names;
1757};
1758
1759/* When using the index (and thus not using psymtabs), each CU has an
1760 object of this type. This is used to hold information needed by
1761 the various "quick" methods. */
1762struct dwarf2_per_cu_quick_data
1763{
1764 /* The file table. This can be NULL if there was no file table
1765 or it's currently not read in.
1766 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1767 struct quick_file_names *file_names;
1768
1769 /* The corresponding symbol table. This is NULL if symbols for this
1770 CU have not yet been read. */
1771 struct symtab *symtab;
1772
1773 /* A temporary mark bit used when iterating over all CUs in
1774 expand_symtabs_matching. */
1775 unsigned int mark : 1;
1776
1777 /* True if we've tried to read the file table and found there isn't one.
1778 There will be no point in trying to read it again next time. */
1779 unsigned int no_file_data : 1;
1780};
1781
1782/* Hash function for a quick_file_names. */
1783
1784static hashval_t
1785hash_file_name_entry (const void *e)
1786{
1787 const struct quick_file_names *file_data = e;
1788
1789 return file_data->offset;
1790}
1791
1792/* Equality function for a quick_file_names. */
1793
1794static int
1795eq_file_name_entry (const void *a, const void *b)
1796{
1797 const struct quick_file_names *ea = a;
1798 const struct quick_file_names *eb = b;
1799
1800 return ea->offset == eb->offset;
1801}
1802
1803/* Delete function for a quick_file_names. */
1804
1805static void
1806delete_file_name_entry (void *e)
1807{
1808 struct quick_file_names *file_data = e;
1809 int i;
1810
1811 for (i = 0; i < file_data->num_file_names; ++i)
1812 {
1813 xfree ((void*) file_data->file_names[i]);
1814 if (file_data->real_names)
1815 xfree ((void*) file_data->real_names[i]);
1816 }
1817
1818 /* The space for the struct itself lives on objfile_obstack,
1819 so we don't free it here. */
1820}
1821
1822/* Create a quick_file_names hash table. */
1823
1824static htab_t
1825create_quick_file_names_table (unsigned int nr_initial_entries)
1826{
1827 return htab_create_alloc (nr_initial_entries,
1828 hash_file_name_entry, eq_file_name_entry,
1829 delete_file_name_entry, xcalloc, xfree);
1830}
9291a0cd 1831
918dd910
JK
1832/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1833 have to be created afterwards. You should call age_cached_comp_units after
1834 processing PER_CU->CU. dw2_setup must have been already called. */
1835
1836static void
1837load_cu (struct dwarf2_per_cu_data *per_cu)
1838{
8b70b953
TT
1839 if (per_cu->debug_type_section)
1840 read_signatured_type_at_offset (per_cu->objfile,
1841 per_cu->debug_type_section,
1842 per_cu->offset);
918dd910
JK
1843 else
1844 load_full_comp_unit (per_cu, per_cu->objfile);
1845
1846 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1847
1848 gdb_assert (per_cu->cu != NULL);
1849}
1850
9291a0cd
TT
1851/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1852 this CU came. */
2fdf6df6 1853
9291a0cd
TT
1854static void
1855dw2_do_instantiate_symtab (struct objfile *objfile,
1856 struct dwarf2_per_cu_data *per_cu)
1857{
1858 struct cleanup *back_to;
1859
1860 back_to = make_cleanup (dwarf2_release_queue, NULL);
1861
1862 queue_comp_unit (per_cu, objfile);
1863
918dd910 1864 load_cu (per_cu);
9291a0cd
TT
1865
1866 process_queue (objfile);
1867
1868 /* Age the cache, releasing compilation units that have not
1869 been used recently. */
1870 age_cached_comp_units ();
1871
1872 do_cleanups (back_to);
1873}
1874
1875/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1876 the objfile from which this CU came. Returns the resulting symbol
1877 table. */
2fdf6df6 1878
9291a0cd
TT
1879static struct symtab *
1880dw2_instantiate_symtab (struct objfile *objfile,
1881 struct dwarf2_per_cu_data *per_cu)
1882{
1883 if (!per_cu->v.quick->symtab)
1884 {
1885 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1886 increment_reading_symtab ();
1887 dw2_do_instantiate_symtab (objfile, per_cu);
1888 do_cleanups (back_to);
1889 }
1890 return per_cu->v.quick->symtab;
1891}
1892
1fd400ff 1893/* Return the CU given its index. */
2fdf6df6 1894
1fd400ff
TT
1895static struct dwarf2_per_cu_data *
1896dw2_get_cu (int index)
1897{
1898 if (index >= dwarf2_per_objfile->n_comp_units)
1899 {
1900 index -= dwarf2_per_objfile->n_comp_units;
1901 return dwarf2_per_objfile->type_comp_units[index];
1902 }
1903 return dwarf2_per_objfile->all_comp_units[index];
1904}
1905
9291a0cd
TT
1906/* A helper function that knows how to read a 64-bit value in a way
1907 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1908 otherwise. */
2fdf6df6 1909
9291a0cd
TT
1910static int
1911extract_cu_value (const char *bytes, ULONGEST *result)
1912{
1913 if (sizeof (ULONGEST) < 8)
1914 {
1915 int i;
1916
1917 /* Ignore the upper 4 bytes if they are all zero. */
1918 for (i = 0; i < 4; ++i)
1919 if (bytes[i + 4] != 0)
1920 return 0;
1921
1922 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1923 }
1924 else
1925 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1926 return 1;
1927}
1928
1929/* Read the CU list from the mapped index, and use it to create all
1930 the CU objects for this objfile. Return 0 if something went wrong,
1931 1 if everything went ok. */
2fdf6df6 1932
9291a0cd 1933static int
1fd400ff
TT
1934create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1935 offset_type cu_list_elements)
9291a0cd
TT
1936{
1937 offset_type i;
9291a0cd
TT
1938
1939 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1940 dwarf2_per_objfile->all_comp_units
1941 = obstack_alloc (&objfile->objfile_obstack,
1942 dwarf2_per_objfile->n_comp_units
1943 * sizeof (struct dwarf2_per_cu_data *));
1944
1945 for (i = 0; i < cu_list_elements; i += 2)
1946 {
1947 struct dwarf2_per_cu_data *the_cu;
1948 ULONGEST offset, length;
1949
1950 if (!extract_cu_value (cu_list, &offset)
1951 || !extract_cu_value (cu_list + 8, &length))
1952 return 0;
1953 cu_list += 2 * 8;
1954
1955 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1956 struct dwarf2_per_cu_data);
1957 the_cu->offset = offset;
1958 the_cu->length = length;
1959 the_cu->objfile = objfile;
1960 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1961 struct dwarf2_per_cu_quick_data);
1962 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1963 }
1964
1965 return 1;
1966}
1967
1fd400ff 1968/* Create the signatured type hash table from the index. */
673bfd45 1969
1fd400ff 1970static int
673bfd45 1971create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1972 struct dwarf2_section_info *section,
673bfd45
DE
1973 const gdb_byte *bytes,
1974 offset_type elements)
1fd400ff
TT
1975{
1976 offset_type i;
673bfd45 1977 htab_t sig_types_hash;
1fd400ff
TT
1978
1979 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1980 dwarf2_per_objfile->type_comp_units
1981 = obstack_alloc (&objfile->objfile_obstack,
1982 dwarf2_per_objfile->n_type_comp_units
1983 * sizeof (struct dwarf2_per_cu_data *));
1984
673bfd45 1985 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1986
1987 for (i = 0; i < elements; i += 3)
1988 {
1989 struct signatured_type *type_sig;
1990 ULONGEST offset, type_offset, signature;
1991 void **slot;
1992
1993 if (!extract_cu_value (bytes, &offset)
1994 || !extract_cu_value (bytes + 8, &type_offset))
1995 return 0;
1996 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1997 bytes += 3 * 8;
1998
1999 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2000 struct signatured_type);
2001 type_sig->signature = signature;
1fd400ff 2002 type_sig->type_offset = type_offset;
8b70b953 2003 type_sig->per_cu.debug_type_section = section;
1fd400ff
TT
2004 type_sig->per_cu.offset = offset;
2005 type_sig->per_cu.objfile = objfile;
2006 type_sig->per_cu.v.quick
2007 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2008 struct dwarf2_per_cu_quick_data);
2009
673bfd45 2010 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2011 *slot = type_sig;
2012
2013 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2014 }
2015
673bfd45 2016 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2017
2018 return 1;
2019}
2020
9291a0cd
TT
2021/* Read the address map data from the mapped index, and use it to
2022 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2023
9291a0cd
TT
2024static void
2025create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2026{
2027 const gdb_byte *iter, *end;
2028 struct obstack temp_obstack;
2029 struct addrmap *mutable_map;
2030 struct cleanup *cleanup;
2031 CORE_ADDR baseaddr;
2032
2033 obstack_init (&temp_obstack);
2034 cleanup = make_cleanup_obstack_free (&temp_obstack);
2035 mutable_map = addrmap_create_mutable (&temp_obstack);
2036
2037 iter = index->address_table;
2038 end = iter + index->address_table_size;
2039
2040 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2041
2042 while (iter < end)
2043 {
2044 ULONGEST hi, lo, cu_index;
2045 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2046 iter += 8;
2047 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2048 iter += 8;
2049 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2050 iter += 4;
2051
2052 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2053 dw2_get_cu (cu_index));
9291a0cd
TT
2054 }
2055
2056 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2057 &objfile->objfile_obstack);
2058 do_cleanups (cleanup);
2059}
2060
59d7bcaf
JK
2061/* The hash function for strings in the mapped index. This is the same as
2062 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2063 implementation. This is necessary because the hash function is tied to the
2064 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2065 SYMBOL_HASH_NEXT.
2066
2067 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2068
9291a0cd 2069static hashval_t
559a7a62 2070mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2071{
2072 const unsigned char *str = (const unsigned char *) p;
2073 hashval_t r = 0;
2074 unsigned char c;
2075
2076 while ((c = *str++) != 0)
559a7a62
JK
2077 {
2078 if (index_version >= 5)
2079 c = tolower (c);
2080 r = r * 67 + c - 113;
2081 }
9291a0cd
TT
2082
2083 return r;
2084}
2085
2086/* Find a slot in the mapped index INDEX for the object named NAME.
2087 If NAME is found, set *VEC_OUT to point to the CU vector in the
2088 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2089
9291a0cd
TT
2090static int
2091find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2092 offset_type **vec_out)
2093{
0cf03b49
JK
2094 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2095 offset_type hash;
9291a0cd 2096 offset_type slot, step;
559a7a62 2097 int (*cmp) (const char *, const char *);
9291a0cd 2098
0cf03b49
JK
2099 if (current_language->la_language == language_cplus
2100 || current_language->la_language == language_java
2101 || current_language->la_language == language_fortran)
2102 {
2103 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2104 not contain any. */
2105 const char *paren = strchr (name, '(');
2106
2107 if (paren)
2108 {
2109 char *dup;
2110
2111 dup = xmalloc (paren - name + 1);
2112 memcpy (dup, name, paren - name);
2113 dup[paren - name] = 0;
2114
2115 make_cleanup (xfree, dup);
2116 name = dup;
2117 }
2118 }
2119
559a7a62
JK
2120 /* Index version 4 did not support case insensitive searches. But the
2121 indexes for case insensitive languages are built in lowercase, therefore
2122 simulate our NAME being searched is also lowercased. */
2123 hash = mapped_index_string_hash ((index->version == 4
2124 && case_sensitivity == case_sensitive_off
2125 ? 5 : index->version),
2126 name);
2127
3876f04e
DE
2128 slot = hash & (index->symbol_table_slots - 1);
2129 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2130 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2131
2132 for (;;)
2133 {
2134 /* Convert a slot number to an offset into the table. */
2135 offset_type i = 2 * slot;
2136 const char *str;
3876f04e 2137 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2138 {
2139 do_cleanups (back_to);
2140 return 0;
2141 }
9291a0cd 2142
3876f04e 2143 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2144 if (!cmp (name, str))
9291a0cd
TT
2145 {
2146 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2147 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2148 do_cleanups (back_to);
9291a0cd
TT
2149 return 1;
2150 }
2151
3876f04e 2152 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2153 }
2154}
2155
2156/* Read the index file. If everything went ok, initialize the "quick"
2157 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2158
9291a0cd
TT
2159static int
2160dwarf2_read_index (struct objfile *objfile)
2161{
9291a0cd
TT
2162 char *addr;
2163 struct mapped_index *map;
b3b272e1 2164 offset_type *metadata;
ac0b195c
KW
2165 const gdb_byte *cu_list;
2166 const gdb_byte *types_list = NULL;
2167 offset_type version, cu_list_elements;
2168 offset_type types_list_elements = 0;
1fd400ff 2169 int i;
9291a0cd 2170
9e0ac564 2171 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2172 return 0;
82430852
JK
2173
2174 /* Older elfutils strip versions could keep the section in the main
2175 executable while splitting it for the separate debug info file. */
2176 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2177 & SEC_HAS_CONTENTS) == 0)
2178 return 0;
2179
9291a0cd
TT
2180 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2181
2182 addr = dwarf2_per_objfile->gdb_index.buffer;
2183 /* Version check. */
1fd400ff 2184 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2185 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2186 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2187 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2188 indices. Index version 4 uses a different hash function than index
2189 version 5 and later. */
831adc1f 2190 if (version < 4)
9291a0cd 2191 return 0;
594e8718
JK
2192 /* Indexes with higher version than the one supported by GDB may be no
2193 longer backward compatible. */
559a7a62 2194 if (version > 5)
594e8718 2195 return 0;
9291a0cd
TT
2196
2197 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2198 map->version = version;
b3b272e1 2199 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2200
2201 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2202
2203 i = 0;
2204 cu_list = addr + MAYBE_SWAP (metadata[i]);
2205 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2206 / 8);
1fd400ff
TT
2207 ++i;
2208
987d643c
TT
2209 types_list = addr + MAYBE_SWAP (metadata[i]);
2210 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]))
2212 / 8);
2213 ++i;
1fd400ff
TT
2214
2215 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2216 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2217 - MAYBE_SWAP (metadata[i]));
2218 ++i;
2219
3876f04e
DE
2220 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2221 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2222 - MAYBE_SWAP (metadata[i]))
2223 / (2 * sizeof (offset_type)));
1fd400ff 2224 ++i;
9291a0cd 2225
1fd400ff
TT
2226 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2227
2228 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2229 return 0;
2230
8b70b953
TT
2231 if (types_list_elements)
2232 {
2233 struct dwarf2_section_info *section;
2234
2235 /* We can only handle a single .debug_types when we have an
2236 index. */
2237 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2238 return 0;
2239
2240 section = VEC_index (dwarf2_section_info_def,
2241 dwarf2_per_objfile->types, 0);
2242
2243 if (!create_signatured_type_table_from_index (objfile, section,
2244 types_list,
2245 types_list_elements))
2246 return 0;
2247 }
9291a0cd
TT
2248
2249 create_addrmap_from_index (objfile, map);
2250
2251 dwarf2_per_objfile->index_table = map;
2252 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2253 dwarf2_per_objfile->quick_file_names_table =
2254 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2255
2256 return 1;
2257}
2258
2259/* A helper for the "quick" functions which sets the global
2260 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2261
9291a0cd
TT
2262static void
2263dw2_setup (struct objfile *objfile)
2264{
2265 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2266 gdb_assert (dwarf2_per_objfile);
2267}
2268
2269/* A helper for the "quick" functions which attempts to read the line
2270 table for THIS_CU. */
2fdf6df6 2271
7b9f3c50
DE
2272static struct quick_file_names *
2273dw2_get_file_names (struct objfile *objfile,
2274 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2275{
2276 bfd *abfd = objfile->obfd;
7b9f3c50 2277 struct line_header *lh;
9291a0cd
TT
2278 struct attribute *attr;
2279 struct cleanup *cleanups;
2280 struct die_info *comp_unit_die;
36374493 2281 struct dwarf2_section_info* sec;
adabb602 2282 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2283 int has_children, i;
2284 struct dwarf2_cu cu;
2285 unsigned int bytes_read, buffer_size;
2286 struct die_reader_specs reader_specs;
2287 char *name, *comp_dir;
7b9f3c50
DE
2288 void **slot;
2289 struct quick_file_names *qfn;
2290 unsigned int line_offset;
9291a0cd 2291
7b9f3c50
DE
2292 if (this_cu->v.quick->file_names != NULL)
2293 return this_cu->v.quick->file_names;
2294 /* If we know there is no line data, no point in looking again. */
2295 if (this_cu->v.quick->no_file_data)
2296 return NULL;
9291a0cd 2297
9816fde3 2298 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2299 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2300
8b70b953
TT
2301 if (this_cu->debug_type_section)
2302 sec = this_cu->debug_type_section;
36374493
DE
2303 else
2304 sec = &dwarf2_per_objfile->info;
2305 dwarf2_read_section (objfile, sec);
2306 buffer_size = sec->size;
2307 buffer = sec->buffer;
9291a0cd 2308 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2309
2310 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2311 buffer, buffer_size,
460c1c54
CC
2312 abfd,
2313 this_cu->debug_type_section != NULL);
9291a0cd 2314
6caca83c
CC
2315 /* Skip dummy compilation units. */
2316 if (info_ptr >= buffer + buffer_size
2317 || peek_abbrev_code (abfd, info_ptr) == 0)
2318 {
2319 do_cleanups (cleanups);
2320 return NULL;
2321 }
2322
9291a0cd
TT
2323 this_cu->cu = &cu;
2324 cu.per_cu = this_cu;
2325
2326 dwarf2_read_abbrevs (abfd, &cu);
2327 make_cleanup (dwarf2_free_abbrev_table, &cu);
2328
9291a0cd 2329 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2330 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2331 &has_children);
9291a0cd 2332
7b9f3c50
DE
2333 lh = NULL;
2334 slot = NULL;
2335 line_offset = 0;
9291a0cd
TT
2336 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2337 if (attr)
2338 {
7b9f3c50
DE
2339 struct quick_file_names find_entry;
2340
2341 line_offset = DW_UNSND (attr);
2342
2343 /* We may have already read in this line header (TU line header sharing).
2344 If we have we're done. */
2345 find_entry.offset = line_offset;
2346 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2347 &find_entry, INSERT);
2348 if (*slot != NULL)
2349 {
2350 do_cleanups (cleanups);
2351 this_cu->v.quick->file_names = *slot;
2352 return *slot;
2353 }
2354
9291a0cd
TT
2355 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2356 }
2357 if (lh == NULL)
2358 {
2359 do_cleanups (cleanups);
7b9f3c50
DE
2360 this_cu->v.quick->no_file_data = 1;
2361 return NULL;
9291a0cd
TT
2362 }
2363
7b9f3c50
DE
2364 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2365 qfn->offset = line_offset;
2366 gdb_assert (slot != NULL);
2367 *slot = qfn;
9291a0cd 2368
7b9f3c50 2369 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2370
7b9f3c50
DE
2371 qfn->num_file_names = lh->num_file_names;
2372 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2373 lh->num_file_names * sizeof (char *));
9291a0cd 2374 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2375 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2376 qfn->real_names = NULL;
9291a0cd 2377
7b9f3c50 2378 free_line_header (lh);
9291a0cd 2379 do_cleanups (cleanups);
7b9f3c50
DE
2380
2381 this_cu->v.quick->file_names = qfn;
2382 return qfn;
9291a0cd
TT
2383}
2384
2385/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2386 real path for a given file name from the line table. */
2fdf6df6 2387
9291a0cd 2388static const char *
7b9f3c50
DE
2389dw2_get_real_path (struct objfile *objfile,
2390 struct quick_file_names *qfn, int index)
9291a0cd 2391{
7b9f3c50
DE
2392 if (qfn->real_names == NULL)
2393 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2394 qfn->num_file_names, sizeof (char *));
9291a0cd 2395
7b9f3c50
DE
2396 if (qfn->real_names[index] == NULL)
2397 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2398
7b9f3c50 2399 return qfn->real_names[index];
9291a0cd
TT
2400}
2401
2402static struct symtab *
2403dw2_find_last_source_symtab (struct objfile *objfile)
2404{
2405 int index;
ae2de4f8 2406
9291a0cd
TT
2407 dw2_setup (objfile);
2408 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2409 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2410}
2411
7b9f3c50
DE
2412/* Traversal function for dw2_forget_cached_source_info. */
2413
2414static int
2415dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2416{
7b9f3c50 2417 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2418
7b9f3c50 2419 if (file_data->real_names)
9291a0cd 2420 {
7b9f3c50 2421 int i;
9291a0cd 2422
7b9f3c50 2423 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2424 {
7b9f3c50
DE
2425 xfree ((void*) file_data->real_names[i]);
2426 file_data->real_names[i] = NULL;
9291a0cd
TT
2427 }
2428 }
7b9f3c50
DE
2429
2430 return 1;
2431}
2432
2433static void
2434dw2_forget_cached_source_info (struct objfile *objfile)
2435{
2436 dw2_setup (objfile);
2437
2438 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2439 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2440}
2441
2442static int
2443dw2_lookup_symtab (struct objfile *objfile, const char *name,
2444 const char *full_path, const char *real_path,
2445 struct symtab **result)
2446{
2447 int i;
c011a4f4
DE
2448 const char *name_basename = lbasename (name);
2449 int check_basename = name_basename == name;
9291a0cd
TT
2450 struct dwarf2_per_cu_data *base_cu = NULL;
2451
2452 dw2_setup (objfile);
ae2de4f8 2453
1fd400ff
TT
2454 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2455 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2456 {
2457 int j;
e254ef6a 2458 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2459 struct quick_file_names *file_data;
9291a0cd 2460
3d7bb9d9 2461 /* We only need to look at symtabs not already expanded. */
e254ef6a 2462 if (per_cu->v.quick->symtab)
9291a0cd
TT
2463 continue;
2464
7b9f3c50
DE
2465 file_data = dw2_get_file_names (objfile, per_cu);
2466 if (file_data == NULL)
9291a0cd
TT
2467 continue;
2468
7b9f3c50 2469 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2470 {
7b9f3c50 2471 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2472
2473 if (FILENAME_CMP (name, this_name) == 0)
2474 {
e254ef6a 2475 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2476 return 1;
2477 }
2478
2479 if (check_basename && ! base_cu
2480 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2481 base_cu = per_cu;
9291a0cd 2482
c011a4f4
DE
2483 /* Before we invoke realpath, which can get expensive when many
2484 files are involved, do a quick comparison of the basenames. */
2485 if (! basenames_may_differ
2486 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2487 continue;
2488
9291a0cd
TT
2489 if (full_path != NULL)
2490 {
7b9f3c50
DE
2491 const char *this_real_name = dw2_get_real_path (objfile,
2492 file_data, j);
9291a0cd 2493
7b9f3c50
DE
2494 if (this_real_name != NULL
2495 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2496 {
e254ef6a 2497 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2498 return 1;
2499 }
2500 }
2501
2502 if (real_path != NULL)
2503 {
7b9f3c50
DE
2504 const char *this_real_name = dw2_get_real_path (objfile,
2505 file_data, j);
9291a0cd 2506
7b9f3c50
DE
2507 if (this_real_name != NULL
2508 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2509 {
74dd2ca6
DE
2510 *result = dw2_instantiate_symtab (objfile, per_cu);
2511 return 1;
9291a0cd
TT
2512 }
2513 }
2514 }
2515 }
2516
2517 if (base_cu)
2518 {
2519 *result = dw2_instantiate_symtab (objfile, base_cu);
2520 return 1;
2521 }
2522
2523 return 0;
2524}
2525
2526static struct symtab *
2527dw2_lookup_symbol (struct objfile *objfile, int block_index,
2528 const char *name, domain_enum domain)
2529{
774b6a14 2530 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2531 instead. */
2532 return NULL;
2533}
2534
2535/* A helper function that expands all symtabs that hold an object
2536 named NAME. */
2fdf6df6 2537
9291a0cd
TT
2538static void
2539dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2540{
2541 dw2_setup (objfile);
2542
ae2de4f8 2543 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2544 if (dwarf2_per_objfile->index_table)
2545 {
2546 offset_type *vec;
2547
2548 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2549 name, &vec))
2550 {
2551 offset_type i, len = MAYBE_SWAP (*vec);
2552 for (i = 0; i < len; ++i)
2553 {
2554 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2555 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2556
e254ef6a 2557 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2558 }
2559 }
2560 }
2561}
2562
774b6a14
TT
2563static void
2564dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2565 enum block_enum block_kind, const char *name,
774b6a14 2566 domain_enum domain)
9291a0cd 2567{
774b6a14 2568 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2569}
2570
2571static void
2572dw2_print_stats (struct objfile *objfile)
2573{
2574 int i, count;
2575
2576 dw2_setup (objfile);
2577 count = 0;
1fd400ff
TT
2578 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2579 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2580 {
e254ef6a 2581 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2582
e254ef6a 2583 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2584 ++count;
2585 }
2586 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2587}
2588
2589static void
2590dw2_dump (struct objfile *objfile)
2591{
2592 /* Nothing worth printing. */
2593}
2594
2595static void
2596dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2597 struct section_offsets *delta)
2598{
2599 /* There's nothing to relocate here. */
2600}
2601
2602static void
2603dw2_expand_symtabs_for_function (struct objfile *objfile,
2604 const char *func_name)
2605{
2606 dw2_do_expand_symtabs_matching (objfile, func_name);
2607}
2608
2609static void
2610dw2_expand_all_symtabs (struct objfile *objfile)
2611{
2612 int i;
2613
2614 dw2_setup (objfile);
1fd400ff
TT
2615
2616 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2617 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2618 {
e254ef6a 2619 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2620
e254ef6a 2621 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2622 }
2623}
2624
2625static void
2626dw2_expand_symtabs_with_filename (struct objfile *objfile,
2627 const char *filename)
2628{
2629 int i;
2630
2631 dw2_setup (objfile);
d4637a04
DE
2632
2633 /* We don't need to consider type units here.
2634 This is only called for examining code, e.g. expand_line_sal.
2635 There can be an order of magnitude (or more) more type units
2636 than comp units, and we avoid them if we can. */
2637
2638 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2639 {
2640 int j;
e254ef6a 2641 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2642 struct quick_file_names *file_data;
9291a0cd 2643
3d7bb9d9 2644 /* We only need to look at symtabs not already expanded. */
e254ef6a 2645 if (per_cu->v.quick->symtab)
9291a0cd
TT
2646 continue;
2647
7b9f3c50
DE
2648 file_data = dw2_get_file_names (objfile, per_cu);
2649 if (file_data == NULL)
9291a0cd
TT
2650 continue;
2651
7b9f3c50 2652 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2653 {
7b9f3c50 2654 const char *this_name = file_data->file_names[j];
1ef75ecc 2655 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2656 {
e254ef6a 2657 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2658 break;
2659 }
2660 }
2661 }
2662}
2663
dd786858 2664static const char *
9291a0cd
TT
2665dw2_find_symbol_file (struct objfile *objfile, const char *name)
2666{
e254ef6a 2667 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2668 offset_type *vec;
7b9f3c50 2669 struct quick_file_names *file_data;
9291a0cd
TT
2670
2671 dw2_setup (objfile);
2672
ae2de4f8 2673 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2674 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2675 {
2676 struct symtab *s;
2677
2678 ALL_OBJFILE_SYMTABS (objfile, s)
2679 if (s->primary)
2680 {
2681 struct blockvector *bv = BLOCKVECTOR (s);
2682 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2683 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2684
2685 if (sym)
2686 return sym->symtab->filename;
2687 }
2688 return NULL;
2689 }
9291a0cd
TT
2690
2691 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2692 name, &vec))
2693 return NULL;
2694
2695 /* Note that this just looks at the very first one named NAME -- but
2696 actually we are looking for a function. find_main_filename
2697 should be rewritten so that it doesn't require a custom hook. It
2698 could just use the ordinary symbol tables. */
2699 /* vec[0] is the length, which must always be >0. */
e254ef6a 2700 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2701
7b9f3c50
DE
2702 file_data = dw2_get_file_names (objfile, per_cu);
2703 if (file_data == NULL)
9291a0cd
TT
2704 return NULL;
2705
7b9f3c50 2706 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2707}
2708
2709static void
40658b94
PH
2710dw2_map_matching_symbols (const char * name, domain_enum namespace,
2711 struct objfile *objfile, int global,
2712 int (*callback) (struct block *,
2713 struct symbol *, void *),
2edb89d3
JK
2714 void *data, symbol_compare_ftype *match,
2715 symbol_compare_ftype *ordered_compare)
9291a0cd 2716{
40658b94 2717 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2718 current language is Ada for a non-Ada objfile using GNU index. As Ada
2719 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2720}
2721
2722static void
2723dw2_expand_symtabs_matching (struct objfile *objfile,
2724 int (*file_matcher) (const char *, void *),
2725 int (*name_matcher) (const char *, void *),
8903c50d 2726 enum search_domain kind,
9291a0cd
TT
2727 void *data)
2728{
2729 int i;
2730 offset_type iter;
4b5246aa 2731 struct mapped_index *index;
9291a0cd
TT
2732
2733 dw2_setup (objfile);
ae2de4f8
DE
2734
2735 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2736 if (!dwarf2_per_objfile->index_table)
2737 return;
4b5246aa 2738 index = dwarf2_per_objfile->index_table;
9291a0cd 2739
7b08b9eb
JK
2740 if (file_matcher != NULL)
2741 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2742 + dwarf2_per_objfile->n_type_comp_units); ++i)
2743 {
2744 int j;
2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2746 struct quick_file_names *file_data;
2747
2748 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2749
2750 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2751 if (per_cu->v.quick->symtab)
2752 continue;
2753
2754 file_data = dw2_get_file_names (objfile, per_cu);
2755 if (file_data == NULL)
2756 continue;
2757
2758 for (j = 0; j < file_data->num_file_names; ++j)
2759 {
2760 if (file_matcher (file_data->file_names[j], data))
2761 {
2762 per_cu->v.quick->mark = 1;
2763 break;
2764 }
2765 }
2766 }
9291a0cd 2767
3876f04e 2768 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2769 {
2770 offset_type idx = 2 * iter;
2771 const char *name;
2772 offset_type *vec, vec_len, vec_idx;
2773
3876f04e 2774 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2775 continue;
2776
3876f04e 2777 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2778
2779 if (! (*name_matcher) (name, data))
2780 continue;
2781
2782 /* The name was matched, now expand corresponding CUs that were
2783 marked. */
4b5246aa 2784 vec = (offset_type *) (index->constant_pool
3876f04e 2785 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2786 vec_len = MAYBE_SWAP (vec[0]);
2787 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2788 {
e254ef6a 2789 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2790
e254ef6a 2791 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2792 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2793 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2794 }
2795 }
2796}
2797
2798static struct symtab *
2799dw2_find_pc_sect_symtab (struct objfile *objfile,
2800 struct minimal_symbol *msymbol,
2801 CORE_ADDR pc,
2802 struct obj_section *section,
2803 int warn_if_readin)
2804{
2805 struct dwarf2_per_cu_data *data;
2806
2807 dw2_setup (objfile);
2808
2809 if (!objfile->psymtabs_addrmap)
2810 return NULL;
2811
2812 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2813 if (!data)
2814 return NULL;
2815
2816 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2817 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2818 paddress (get_objfile_arch (objfile), pc));
2819
2820 return dw2_instantiate_symtab (objfile, data);
2821}
2822
9291a0cd 2823static void
44b13c5a 2824dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2825 void *data, int need_fullname)
9291a0cd
TT
2826{
2827 int i;
2828
2829 dw2_setup (objfile);
ae2de4f8 2830
1fd400ff
TT
2831 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2832 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2833 {
2834 int j;
e254ef6a 2835 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2836 struct quick_file_names *file_data;
9291a0cd 2837
3d7bb9d9 2838 /* We only need to look at symtabs not already expanded. */
e254ef6a 2839 if (per_cu->v.quick->symtab)
9291a0cd
TT
2840 continue;
2841
7b9f3c50
DE
2842 file_data = dw2_get_file_names (objfile, per_cu);
2843 if (file_data == NULL)
9291a0cd
TT
2844 continue;
2845
7b9f3c50 2846 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2847 {
74e2f255
DE
2848 const char *this_real_name;
2849
2850 if (need_fullname)
2851 this_real_name = dw2_get_real_path (objfile, file_data, j);
2852 else
2853 this_real_name = NULL;
7b9f3c50 2854 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2855 }
2856 }
2857}
2858
2859static int
2860dw2_has_symbols (struct objfile *objfile)
2861{
2862 return 1;
2863}
2864
2865const struct quick_symbol_functions dwarf2_gdb_index_functions =
2866{
2867 dw2_has_symbols,
2868 dw2_find_last_source_symtab,
2869 dw2_forget_cached_source_info,
2870 dw2_lookup_symtab,
2871 dw2_lookup_symbol,
774b6a14 2872 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2873 dw2_print_stats,
2874 dw2_dump,
2875 dw2_relocate,
2876 dw2_expand_symtabs_for_function,
2877 dw2_expand_all_symtabs,
2878 dw2_expand_symtabs_with_filename,
2879 dw2_find_symbol_file,
40658b94 2880 dw2_map_matching_symbols,
9291a0cd
TT
2881 dw2_expand_symtabs_matching,
2882 dw2_find_pc_sect_symtab,
9291a0cd
TT
2883 dw2_map_symbol_filenames
2884};
2885
2886/* Initialize for reading DWARF for this objfile. Return 0 if this
2887 file will use psymtabs, or 1 if using the GNU index. */
2888
2889int
2890dwarf2_initialize_objfile (struct objfile *objfile)
2891{
2892 /* If we're about to read full symbols, don't bother with the
2893 indices. In this case we also don't care if some other debug
2894 format is making psymtabs, because they are all about to be
2895 expanded anyway. */
2896 if ((objfile->flags & OBJF_READNOW))
2897 {
2898 int i;
2899
2900 dwarf2_per_objfile->using_index = 1;
2901 create_all_comp_units (objfile);
1fd400ff 2902 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2903 dwarf2_per_objfile->quick_file_names_table =
2904 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2905
1fd400ff
TT
2906 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2907 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2908 {
e254ef6a 2909 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2910
e254ef6a
DE
2911 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2912 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2913 }
2914
2915 /* Return 1 so that gdb sees the "quick" functions. However,
2916 these functions will be no-ops because we will have expanded
2917 all symtabs. */
2918 return 1;
2919 }
2920
2921 if (dwarf2_read_index (objfile))
2922 return 1;
2923
9291a0cd
TT
2924 return 0;
2925}
2926
2927\f
2928
dce234bc
PP
2929/* Build a partial symbol table. */
2930
2931void
f29dff0a 2932dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2933{
f29dff0a 2934 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2935 {
2936 init_psymbol_list (objfile, 1024);
2937 }
2938
d146bf1e 2939 dwarf2_build_psymtabs_hard (objfile);
c906108c 2940}
c906108c 2941
45452591
DE
2942/* Return TRUE if OFFSET is within CU_HEADER. */
2943
2944static inline int
2945offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2946{
2947 unsigned int bottom = cu_header->offset;
2948 unsigned int top = (cu_header->offset
2949 + cu_header->length
2950 + cu_header->initial_length_size);
9a619af0 2951
45452591
DE
2952 return (offset >= bottom && offset < top);
2953}
2954
93311388
DE
2955/* Read in the comp unit header information from the debug_info at info_ptr.
2956 NOTE: This leaves members offset, first_die_offset to be filled in
2957 by the caller. */
107d2387 2958
fe1b8b76 2959static gdb_byte *
107d2387 2960read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2961 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2962{
2963 int signed_addr;
891d2f0b 2964 unsigned int bytes_read;
c764a876
DE
2965
2966 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2967 cu_header->initial_length_size = bytes_read;
2968 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2969 info_ptr += bytes_read;
107d2387
AC
2970 cu_header->version = read_2_bytes (abfd, info_ptr);
2971 info_ptr += 2;
613e1657 2972 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2973 &bytes_read);
613e1657 2974 info_ptr += bytes_read;
107d2387
AC
2975 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2976 info_ptr += 1;
2977 signed_addr = bfd_get_sign_extend_vma (abfd);
2978 if (signed_addr < 0)
8e65ff28 2979 internal_error (__FILE__, __LINE__,
e2e0b3e5 2980 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2981 cu_header->signed_addr_p = signed_addr;
c764a876 2982
107d2387
AC
2983 return info_ptr;
2984}
2985
adabb602
DE
2986/* Read in a CU header and perform some basic error checking. */
2987
fe1b8b76
JB
2988static gdb_byte *
2989partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2990 gdb_byte *buffer, unsigned int buffer_size,
460c1c54 2991 bfd *abfd, int is_debug_type_section)
72bf9492 2992{
fe1b8b76 2993 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 2994
adabb602
DE
2995 header->offset = beg_of_comp_unit - buffer;
2996
72bf9492
DJ
2997 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2998
460c1c54
CC
2999 /* If we're reading a type unit, skip over the signature and
3000 type_offset fields. */
3001 if (is_debug_type_section)
3002 info_ptr += 8 /*signature*/ + header->offset_size;
3003
adabb602
DE
3004 header->first_die_offset = info_ptr - beg_of_comp_unit;
3005
2dc7f7b3 3006 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 3007 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
3008 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3009 bfd_get_filename (abfd));
72bf9492 3010
9e0ac564
TT
3011 if (header->abbrev_offset
3012 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3013 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
3014 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3015 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 3016 (long) header->abbrev_offset,
93311388 3017 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3018 bfd_get_filename (abfd));
3019
3020 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 3021 > buffer + buffer_size)
8a3fe4f8
AC
3022 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3023 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3024 (long) header->length,
93311388 3025 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3026 bfd_get_filename (abfd));
3027
3028 return info_ptr;
3029}
3030
348e048f
DE
3031/* Read in the types comp unit header information from .debug_types entry at
3032 types_ptr. The result is a pointer to one past the end of the header. */
3033
3034static gdb_byte *
3035read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3036 struct dwarf2_section_info *section,
348e048f
DE
3037 ULONGEST *signature,
3038 gdb_byte *types_ptr, bfd *abfd)
3039{
348e048f
DE
3040 gdb_byte *initial_types_ptr = types_ptr;
3041
8b70b953
TT
3042 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3043 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3044
3045 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3046
3047 *signature = read_8_bytes (abfd, types_ptr);
3048 types_ptr += 8;
3049 types_ptr += cu_header->offset_size;
3050 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3051
3052 return types_ptr;
3053}
3054
aaa75496
JB
3055/* Allocate a new partial symtab for file named NAME and mark this new
3056 partial symtab as being an include of PST. */
3057
3058static void
3059dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3060 struct objfile *objfile)
3061{
3062 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3063
3064 subpst->section_offsets = pst->section_offsets;
3065 subpst->textlow = 0;
3066 subpst->texthigh = 0;
3067
3068 subpst->dependencies = (struct partial_symtab **)
3069 obstack_alloc (&objfile->objfile_obstack,
3070 sizeof (struct partial_symtab *));
3071 subpst->dependencies[0] = pst;
3072 subpst->number_of_dependencies = 1;
3073
3074 subpst->globals_offset = 0;
3075 subpst->n_global_syms = 0;
3076 subpst->statics_offset = 0;
3077 subpst->n_static_syms = 0;
3078 subpst->symtab = NULL;
3079 subpst->read_symtab = pst->read_symtab;
3080 subpst->readin = 0;
3081
3082 /* No private part is necessary for include psymtabs. This property
3083 can be used to differentiate between such include psymtabs and
10b3939b 3084 the regular ones. */
58a9656e 3085 subpst->read_symtab_private = NULL;
aaa75496
JB
3086}
3087
3088/* Read the Line Number Program data and extract the list of files
3089 included by the source file represented by PST. Build an include
d85a05f0 3090 partial symtab for each of these included files. */
aaa75496
JB
3091
3092static void
3093dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3094 struct die_info *die,
aaa75496
JB
3095 struct partial_symtab *pst)
3096{
3097 struct objfile *objfile = cu->objfile;
3098 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3099 struct line_header *lh = NULL;
3100 struct attribute *attr;
aaa75496 3101
d85a05f0
DJ
3102 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3103 if (attr)
3104 {
3105 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3106
d85a05f0
DJ
3107 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3108 }
aaa75496
JB
3109 if (lh == NULL)
3110 return; /* No linetable, so no includes. */
3111
c6da4cef
DE
3112 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3113 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3114
3115 free_line_header (lh);
3116}
3117
348e048f
DE
3118static hashval_t
3119hash_type_signature (const void *item)
3120{
3121 const struct signatured_type *type_sig = item;
9a619af0 3122
348e048f
DE
3123 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3124 return type_sig->signature;
3125}
3126
3127static int
3128eq_type_signature (const void *item_lhs, const void *item_rhs)
3129{
3130 const struct signatured_type *lhs = item_lhs;
3131 const struct signatured_type *rhs = item_rhs;
9a619af0 3132
348e048f
DE
3133 return lhs->signature == rhs->signature;
3134}
3135
1fd400ff
TT
3136/* Allocate a hash table for signatured types. */
3137
3138static htab_t
673bfd45 3139allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3140{
3141 return htab_create_alloc_ex (41,
3142 hash_type_signature,
3143 eq_type_signature,
3144 NULL,
3145 &objfile->objfile_obstack,
3146 hashtab_obstack_allocate,
3147 dummy_obstack_deallocate);
3148}
3149
3150/* A helper function to add a signatured type CU to a list. */
3151
3152static int
3153add_signatured_type_cu_to_list (void **slot, void *datum)
3154{
3155 struct signatured_type *sigt = *slot;
3156 struct dwarf2_per_cu_data ***datap = datum;
3157
3158 **datap = &sigt->per_cu;
3159 ++*datap;
3160
3161 return 1;
3162}
3163
348e048f
DE
3164/* Create the hash table of all entries in the .debug_types section.
3165 The result is zero if there is an error (e.g. missing .debug_types section),
3166 otherwise non-zero. */
3167
3168static int
3169create_debug_types_hash_table (struct objfile *objfile)
3170{
8b70b953 3171 htab_t types_htab = NULL;
1fd400ff 3172 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3173 int ix;
3174 struct dwarf2_section_info *section;
348e048f 3175
8b70b953 3176 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3177 {
3178 dwarf2_per_objfile->signatured_types = NULL;
3179 return 0;
3180 }
3181
8b70b953
TT
3182 for (ix = 0;
3183 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3184 ix, section);
3185 ++ix)
3186 {
3187 gdb_byte *info_ptr, *end_ptr;
348e048f 3188
8b70b953
TT
3189 dwarf2_read_section (objfile, section);
3190 info_ptr = section->buffer;
348e048f 3191
8b70b953
TT
3192 if (info_ptr == NULL)
3193 continue;
348e048f 3194
8b70b953
TT
3195 if (types_htab == NULL)
3196 types_htab = allocate_signatured_type_table (objfile);
348e048f 3197
8b70b953
TT
3198 if (dwarf2_die_debug)
3199 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3200
3201 end_ptr = info_ptr + section->size;
3202 while (info_ptr < end_ptr)
3203 {
3204 unsigned int offset;
3205 unsigned int offset_size;
3206 unsigned int type_offset;
3207 unsigned int length, initial_length_size;
3208 unsigned short version;
3209 ULONGEST signature;
3210 struct signatured_type *type_sig;
3211 void **slot;
3212 gdb_byte *ptr = info_ptr;
348e048f 3213
8b70b953 3214 offset = ptr - section->buffer;
348e048f 3215
8b70b953
TT
3216 /* We need to read the type's signature in order to build the hash
3217 table, but we don't need to read anything else just yet. */
348e048f 3218
8b70b953
TT
3219 /* Sanity check to ensure entire cu is present. */
3220 length = read_initial_length (objfile->obfd, ptr,
3221 &initial_length_size);
3222 if (ptr + length + initial_length_size > end_ptr)
3223 {
3224 complaint (&symfile_complaints,
3225 _("debug type entry runs off end "
3226 "of `.debug_types' section, ignored"));
3227 break;
3228 }
348e048f 3229
8b70b953
TT
3230 offset_size = initial_length_size == 4 ? 4 : 8;
3231 ptr += initial_length_size;
3232 version = bfd_get_16 (objfile->obfd, ptr);
3233 ptr += 2;
3234 ptr += offset_size; /* abbrev offset */
3235 ptr += 1; /* address size */
3236 signature = bfd_get_64 (objfile->obfd, ptr);
3237 ptr += 8;
3238 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3239 ptr += offset_size;
6caca83c
CC
3240
3241 /* Skip dummy type units. */
3242 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3243 {
3244 info_ptr = info_ptr + initial_length_size + length;
3245 continue;
3246 }
8b70b953
TT
3247
3248 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3249 memset (type_sig, 0, sizeof (*type_sig));
3250 type_sig->signature = signature;
3251 type_sig->type_offset = type_offset;
3252 type_sig->per_cu.objfile = objfile;
3253 type_sig->per_cu.debug_type_section = section;
3254 type_sig->per_cu.offset = offset;
3255
3256 slot = htab_find_slot (types_htab, type_sig, INSERT);
3257 gdb_assert (slot != NULL);
3258 if (*slot != NULL)
3259 {
3260 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3261
8b70b953
TT
3262 complaint (&symfile_complaints,
3263 _("debug type entry at offset 0x%x is duplicate to the "
3264 "entry at offset 0x%x, signature 0x%s"),
3265 offset, dup_sig->per_cu.offset,
3266 phex (signature, sizeof (signature)));
3267 gdb_assert (signature == dup_sig->signature);
3268 }
3269 *slot = type_sig;
348e048f 3270
8b70b953
TT
3271 if (dwarf2_die_debug)
3272 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3273 offset, phex (signature, sizeof (signature)));
348e048f 3274
8b70b953
TT
3275 info_ptr = info_ptr + initial_length_size + length;
3276 }
348e048f
DE
3277 }
3278
3279 dwarf2_per_objfile->signatured_types = types_htab;
3280
1fd400ff
TT
3281 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3282 dwarf2_per_objfile->type_comp_units
3283 = obstack_alloc (&objfile->objfile_obstack,
3284 dwarf2_per_objfile->n_type_comp_units
3285 * sizeof (struct dwarf2_per_cu_data *));
3286 iter = &dwarf2_per_objfile->type_comp_units[0];
3287 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3288 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3289 == dwarf2_per_objfile->n_type_comp_units);
3290
348e048f
DE
3291 return 1;
3292}
3293
3294/* Lookup a signature based type.
3295 Returns NULL if SIG is not present in the table. */
3296
3297static struct signatured_type *
3298lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3299{
3300 struct signatured_type find_entry, *entry;
3301
3302 if (dwarf2_per_objfile->signatured_types == NULL)
3303 {
3304 complaint (&symfile_complaints,
55f1336d 3305 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3306 return 0;
3307 }
3308
3309 find_entry.signature = sig;
3310 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3311 return entry;
3312}
3313
d85a05f0
DJ
3314/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3315
3316static void
3317init_cu_die_reader (struct die_reader_specs *reader,
3318 struct dwarf2_cu *cu)
3319{
3320 reader->abfd = cu->objfile->obfd;
3321 reader->cu = cu;
8b70b953 3322 if (cu->per_cu->debug_type_section)
be391dca 3323 {
8b70b953
TT
3324 gdb_assert (cu->per_cu->debug_type_section->readin);
3325 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3326 }
d85a05f0 3327 else
be391dca
TT
3328 {
3329 gdb_assert (dwarf2_per_objfile->info.readin);
3330 reader->buffer = dwarf2_per_objfile->info.buffer;
3331 }
d85a05f0
DJ
3332}
3333
3334/* Find the base address of the compilation unit for range lists and
3335 location lists. It will normally be specified by DW_AT_low_pc.
3336 In DWARF-3 draft 4, the base address could be overridden by
3337 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3338 compilation units with discontinuous ranges. */
3339
3340static void
3341dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3342{
3343 struct attribute *attr;
3344
3345 cu->base_known = 0;
3346 cu->base_address = 0;
3347
3348 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3349 if (attr)
3350 {
3351 cu->base_address = DW_ADDR (attr);
3352 cu->base_known = 1;
3353 }
3354 else
3355 {
3356 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3357 if (attr)
3358 {
3359 cu->base_address = DW_ADDR (attr);
3360 cu->base_known = 1;
3361 }
3362 }
3363}
3364
348e048f
DE
3365/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3366 to combine the common parts.
93311388 3367 Process a compilation unit for a psymtab.
348e048f
DE
3368 BUFFER is a pointer to the beginning of the dwarf section buffer,
3369 either .debug_info or debug_types.
93311388
DE
3370 INFO_PTR is a pointer to the start of the CU.
3371 Returns a pointer to the next CU. */
aaa75496 3372
93311388
DE
3373static gdb_byte *
3374process_psymtab_comp_unit (struct objfile *objfile,
3375 struct dwarf2_per_cu_data *this_cu,
3376 gdb_byte *buffer, gdb_byte *info_ptr,
3377 unsigned int buffer_size)
c906108c 3378{
c906108c 3379 bfd *abfd = objfile->obfd;
93311388 3380 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3381 struct die_info *comp_unit_die;
c906108c 3382 struct partial_symtab *pst;
5734ee8b 3383 CORE_ADDR baseaddr;
93311388
DE
3384 struct cleanup *back_to_inner;
3385 struct dwarf2_cu cu;
d85a05f0
DJ
3386 int has_children, has_pc_info;
3387 struct attribute *attr;
d85a05f0
DJ
3388 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3389 struct die_reader_specs reader_specs;
3e2a0cee 3390 const char *filename;
c906108c 3391
9816fde3 3392 init_one_comp_unit (&cu, objfile);
93311388 3393 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3394
93311388
DE
3395 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3396 buffer, buffer_size,
460c1c54
CC
3397 abfd,
3398 this_cu->debug_type_section != NULL);
10b3939b 3399
6caca83c
CC
3400 /* Skip dummy compilation units. */
3401 if (info_ptr >= buffer + buffer_size
3402 || peek_abbrev_code (abfd, info_ptr) == 0)
3403 {
3404 info_ptr = (beg_of_comp_unit + cu.header.length
3405 + cu.header.initial_length_size);
3406 do_cleanups (back_to_inner);
3407 return info_ptr;
3408 }
3409
93311388 3410 cu.list_in_scope = &file_symbols;
af703f96 3411
328c9494
DJ
3412 /* If this compilation unit was already read in, free the
3413 cached copy in order to read it in again. This is
3414 necessary because we skipped some symbols when we first
3415 read in the compilation unit (see load_partial_dies).
3416 This problem could be avoided, but the benefit is
3417 unclear. */
3418 if (this_cu->cu != NULL)
3419 free_one_cached_comp_unit (this_cu->cu);
3420
3421 /* Note that this is a pointer to our stack frame, being
3422 added to a global data structure. It will be cleaned up
3423 in free_stack_comp_unit when we finish with this
3424 compilation unit. */
3425 this_cu->cu = &cu;
d85a05f0
DJ
3426 cu.per_cu = this_cu;
3427
93311388
DE
3428 /* Read the abbrevs for this compilation unit into a table. */
3429 dwarf2_read_abbrevs (abfd, &cu);
3430 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3431
93311388 3432 /* Read the compilation unit die. */
d85a05f0
DJ
3433 init_cu_die_reader (&reader_specs, &cu);
3434 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3435 &has_children);
93311388 3436
8b70b953 3437 if (this_cu->debug_type_section)
348e048f 3438 {
b3c8eb43
JK
3439 /* LENGTH has not been set yet for type units. */
3440 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3441 this_cu->length = cu.header.length + cu.header.initial_length_size;
3442 }
d85a05f0 3443 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3444 {
93311388
DE
3445 info_ptr = (beg_of_comp_unit + cu.header.length
3446 + cu.header.initial_length_size);
3447 do_cleanups (back_to_inner);
3448 return info_ptr;
3449 }
72bf9492 3450
9816fde3 3451 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3452
93311388 3453 /* Allocate a new partial symbol table structure. */
d85a05f0 3454 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3455 if (attr == NULL || !DW_STRING (attr))
3456 filename = "";
3457 else
3458 filename = DW_STRING (attr);
93311388 3459 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3460 filename,
93311388
DE
3461 /* TEXTLOW and TEXTHIGH are set below. */
3462 0,
3463 objfile->global_psymbols.next,
3464 objfile->static_psymbols.next);
9750bca9 3465 pst->psymtabs_addrmap_supported = 1;
72bf9492 3466
d85a05f0
DJ
3467 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3468 if (attr != NULL)
3469 pst->dirname = DW_STRING (attr);
72bf9492 3470
e38df1d0 3471 pst->read_symtab_private = this_cu;
72bf9492 3472
93311388 3473 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3474
0963b4bd 3475 /* Store the function that reads in the rest of the symbol table. */
93311388 3476 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3477
9291a0cd 3478 this_cu->v.psymtab = pst;
c906108c 3479
d85a05f0
DJ
3480 dwarf2_find_base_address (comp_unit_die, &cu);
3481
93311388
DE
3482 /* Possibly set the default values of LOWPC and HIGHPC from
3483 `DW_AT_ranges'. */
d85a05f0
DJ
3484 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3485 &best_highpc, &cu, pst);
3486 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3487 /* Store the contiguous range if it is not empty; it can be empty for
3488 CUs with no code. */
3489 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3490 best_lowpc + baseaddr,
3491 best_highpc + baseaddr - 1, pst);
93311388
DE
3492
3493 /* Check if comp unit has_children.
3494 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3495 If not, there's no more debug_info for this comp unit. */
d85a05f0 3496 if (has_children)
93311388
DE
3497 {
3498 struct partial_die_info *first_die;
3499 CORE_ADDR lowpc, highpc;
31ffec48 3500
93311388
DE
3501 lowpc = ((CORE_ADDR) -1);
3502 highpc = ((CORE_ADDR) 0);
c906108c 3503
93311388 3504 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3505
93311388 3506 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3507 ! has_pc_info, &cu);
57c22c6c 3508
93311388
DE
3509 /* If we didn't find a lowpc, set it to highpc to avoid
3510 complaints from `maint check'. */
3511 if (lowpc == ((CORE_ADDR) -1))
3512 lowpc = highpc;
10b3939b 3513
93311388
DE
3514 /* If the compilation unit didn't have an explicit address range,
3515 then use the information extracted from its child dies. */
d85a05f0 3516 if (! has_pc_info)
93311388 3517 {
d85a05f0
DJ
3518 best_lowpc = lowpc;
3519 best_highpc = highpc;
93311388
DE
3520 }
3521 }
d85a05f0
DJ
3522 pst->textlow = best_lowpc + baseaddr;
3523 pst->texthigh = best_highpc + baseaddr;
c906108c 3524
93311388
DE
3525 pst->n_global_syms = objfile->global_psymbols.next -
3526 (objfile->global_psymbols.list + pst->globals_offset);
3527 pst->n_static_syms = objfile->static_psymbols.next -
3528 (objfile->static_psymbols.list + pst->statics_offset);
3529 sort_pst_symbols (pst);
c906108c 3530
93311388
DE
3531 info_ptr = (beg_of_comp_unit + cu.header.length
3532 + cu.header.initial_length_size);
ae038cb0 3533
8b70b953 3534 if (this_cu->debug_type_section)
348e048f
DE
3535 {
3536 /* It's not clear we want to do anything with stmt lists here.
3537 Waiting to see what gcc ultimately does. */
3538 }
d85a05f0 3539 else
93311388
DE
3540 {
3541 /* Get the list of files included in the current compilation unit,
3542 and build a psymtab for each of them. */
d85a05f0 3543 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3544 }
ae038cb0 3545
93311388 3546 do_cleanups (back_to_inner);
ae038cb0 3547
93311388
DE
3548 return info_ptr;
3549}
ff013f42 3550
348e048f
DE
3551/* Traversal function for htab_traverse_noresize.
3552 Process one .debug_types comp-unit. */
3553
3554static int
3555process_type_comp_unit (void **slot, void *info)
3556{
3557 struct signatured_type *entry = (struct signatured_type *) *slot;
3558 struct objfile *objfile = (struct objfile *) info;
3559 struct dwarf2_per_cu_data *this_cu;
3560
3561 this_cu = &entry->per_cu;
348e048f 3562
8b70b953 3563 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3564 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3565 this_cu->debug_type_section->buffer,
3566 (this_cu->debug_type_section->buffer
3567 + this_cu->offset),
3568 this_cu->debug_type_section->size);
348e048f
DE
3569
3570 return 1;
3571}
3572
3573/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3574 Build partial symbol tables for the .debug_types comp-units. */
3575
3576static void
3577build_type_psymtabs (struct objfile *objfile)
3578{
3579 if (! create_debug_types_hash_table (objfile))
3580 return;
3581
3582 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3583 process_type_comp_unit, objfile);
3584}
3585
60606b2c
TT
3586/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3587
3588static void
3589psymtabs_addrmap_cleanup (void *o)
3590{
3591 struct objfile *objfile = o;
ec61707d 3592
60606b2c
TT
3593 objfile->psymtabs_addrmap = NULL;
3594}
3595
93311388
DE
3596/* Build the partial symbol table by doing a quick pass through the
3597 .debug_info and .debug_abbrev sections. */
72bf9492 3598
93311388 3599static void
c67a9c90 3600dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3601{
93311388 3602 gdb_byte *info_ptr;
60606b2c
TT
3603 struct cleanup *back_to, *addrmap_cleanup;
3604 struct obstack temp_obstack;
93311388 3605
98bfdba5
PA
3606 dwarf2_per_objfile->reading_partial_symbols = 1;
3607
be391dca 3608 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3609 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3610
93311388
DE
3611 /* Any cached compilation units will be linked by the per-objfile
3612 read_in_chain. Make sure to free them when we're done. */
3613 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3614
348e048f
DE
3615 build_type_psymtabs (objfile);
3616
93311388 3617 create_all_comp_units (objfile);
c906108c 3618
60606b2c
TT
3619 /* Create a temporary address map on a temporary obstack. We later
3620 copy this to the final obstack. */
3621 obstack_init (&temp_obstack);
3622 make_cleanup_obstack_free (&temp_obstack);
3623 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3624 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3625
93311388
DE
3626 /* Since the objects we're extracting from .debug_info vary in
3627 length, only the individual functions to extract them (like
3628 read_comp_unit_head and load_partial_die) can really know whether
3629 the buffer is large enough to hold another complete object.
c906108c 3630
93311388
DE
3631 At the moment, they don't actually check that. If .debug_info
3632 holds just one extra byte after the last compilation unit's dies,
3633 then read_comp_unit_head will happily read off the end of the
3634 buffer. read_partial_die is similarly casual. Those functions
3635 should be fixed.
c906108c 3636
93311388
DE
3637 For this loop condition, simply checking whether there's any data
3638 left at all should be sufficient. */
c906108c 3639
93311388
DE
3640 while (info_ptr < (dwarf2_per_objfile->info.buffer
3641 + dwarf2_per_objfile->info.size))
3642 {
3643 struct dwarf2_per_cu_data *this_cu;
dd373385 3644
3e43a32a
MS
3645 this_cu = dwarf2_find_comp_unit (info_ptr
3646 - dwarf2_per_objfile->info.buffer,
93311388 3647 objfile);
aaa75496 3648
93311388
DE
3649 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3650 dwarf2_per_objfile->info.buffer,
3651 info_ptr,
3652 dwarf2_per_objfile->info.size);
c906108c 3653 }
ff013f42
JK
3654
3655 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3656 &objfile->objfile_obstack);
60606b2c 3657 discard_cleanups (addrmap_cleanup);
ff013f42 3658
ae038cb0
DJ
3659 do_cleanups (back_to);
3660}
3661
93311388 3662/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3663
3664static void
93311388
DE
3665load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3666 struct objfile *objfile)
ae038cb0
DJ
3667{
3668 bfd *abfd = objfile->obfd;
adabb602 3669 gdb_byte *info_ptr;
d85a05f0 3670 struct die_info *comp_unit_die;
ae038cb0 3671 struct dwarf2_cu *cu;
1d9ec526 3672 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3673 int has_children;
3674 struct die_reader_specs reader_specs;
98bfdba5 3675 int read_cu = 0;
ae038cb0 3676
8b70b953 3677 gdb_assert (! this_cu->debug_type_section);
348e048f 3678
be391dca 3679 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3680 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3681
98bfdba5
PA
3682 if (this_cu->cu == NULL)
3683 {
9816fde3
JK
3684 cu = xmalloc (sizeof (*cu));
3685 init_one_comp_unit (cu, objfile);
ae038cb0 3686
98bfdba5 3687 read_cu = 1;
ae038cb0 3688
98bfdba5
PA
3689 /* If an error occurs while loading, release our storage. */
3690 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3691
98bfdba5
PA
3692 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3693 dwarf2_per_objfile->info.buffer,
3694 dwarf2_per_objfile->info.size,
460c1c54 3695 abfd, 0);
ae038cb0 3696
6caca83c
CC
3697 /* Skip dummy compilation units. */
3698 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3699 + dwarf2_per_objfile->info.size)
3700 || peek_abbrev_code (abfd, info_ptr) == 0)
3701 {
3702 do_cleanups (free_cu_cleanup);
3703 return;
3704 }
3705
98bfdba5
PA
3706 /* Link this compilation unit into the compilation unit tree. */
3707 this_cu->cu = cu;
3708 cu->per_cu = this_cu;
98bfdba5
PA
3709
3710 /* Link this CU into read_in_chain. */
3711 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3712 dwarf2_per_objfile->read_in_chain = this_cu;
3713 }
3714 else
3715 {
3716 cu = this_cu->cu;
3717 info_ptr += cu->header.first_die_offset;
3718 }
ae038cb0
DJ
3719
3720 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3721 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3722 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3723 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3724
3725 /* Read the compilation unit die. */
d85a05f0
DJ
3726 init_cu_die_reader (&reader_specs, cu);
3727 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3728 &has_children);
ae038cb0 3729
9816fde3 3730 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3731
ae038cb0
DJ
3732 /* Check if comp unit has_children.
3733 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3734 If not, there's no more debug_info for this comp unit. */
d85a05f0 3735 if (has_children)
93311388 3736 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3737
98bfdba5
PA
3738 do_cleanups (free_abbrevs_cleanup);
3739
3740 if (read_cu)
3741 {
3742 /* We've successfully allocated this compilation unit. Let our
3743 caller clean it up when finished with it. */
3744 discard_cleanups (free_cu_cleanup);
3745 }
ae038cb0
DJ
3746}
3747
3748/* Create a list of all compilation units in OBJFILE. We do this only
3749 if an inter-comp-unit reference is found; presumably if there is one,
3750 there will be many, and one will occur early in the .debug_info section.
3751 So there's no point in building this list incrementally. */
3752
3753static void
3754create_all_comp_units (struct objfile *objfile)
3755{
3756 int n_allocated;
3757 int n_comp_units;
3758 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3759 gdb_byte *info_ptr;
3760
3761 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3762 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3763
3764 n_comp_units = 0;
3765 n_allocated = 10;
3766 all_comp_units = xmalloc (n_allocated
3767 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3768
3e43a32a
MS
3769 while (info_ptr < dwarf2_per_objfile->info.buffer
3770 + dwarf2_per_objfile->info.size)
ae038cb0 3771 {
c764a876 3772 unsigned int length, initial_length_size;
ae038cb0 3773 struct dwarf2_per_cu_data *this_cu;
c764a876 3774 unsigned int offset;
ae038cb0 3775
dce234bc 3776 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3777
3778 /* Read just enough information to find out where the next
3779 compilation unit is. */
c764a876
DE
3780 length = read_initial_length (objfile->obfd, info_ptr,
3781 &initial_length_size);
ae038cb0
DJ
3782
3783 /* Save the compilation unit for later lookup. */
3784 this_cu = obstack_alloc (&objfile->objfile_obstack,
3785 sizeof (struct dwarf2_per_cu_data));
3786 memset (this_cu, 0, sizeof (*this_cu));
3787 this_cu->offset = offset;
c764a876 3788 this_cu->length = length + initial_length_size;
9291a0cd 3789 this_cu->objfile = objfile;
ae038cb0
DJ
3790
3791 if (n_comp_units == n_allocated)
3792 {
3793 n_allocated *= 2;
3794 all_comp_units = xrealloc (all_comp_units,
3795 n_allocated
3796 * sizeof (struct dwarf2_per_cu_data *));
3797 }
3798 all_comp_units[n_comp_units++] = this_cu;
3799
3800 info_ptr = info_ptr + this_cu->length;
3801 }
3802
3803 dwarf2_per_objfile->all_comp_units
3804 = obstack_alloc (&objfile->objfile_obstack,
3805 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3806 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3807 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3808 xfree (all_comp_units);
3809 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3810}
3811
5734ee8b
DJ
3812/* Process all loaded DIEs for compilation unit CU, starting at
3813 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3814 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3815 DW_AT_ranges). If NEED_PC is set, then this function will set
3816 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3817 and record the covered ranges in the addrmap. */
c906108c 3818
72bf9492
DJ
3819static void
3820scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3821 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3822{
72bf9492 3823 struct partial_die_info *pdi;
c906108c 3824
91c24f0a
DC
3825 /* Now, march along the PDI's, descending into ones which have
3826 interesting children but skipping the children of the other ones,
3827 until we reach the end of the compilation unit. */
c906108c 3828
72bf9492 3829 pdi = first_die;
91c24f0a 3830
72bf9492
DJ
3831 while (pdi != NULL)
3832 {
3833 fixup_partial_die (pdi, cu);
c906108c 3834
f55ee35c 3835 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3836 children, so we need to look at them. Ditto for anonymous
3837 enums. */
933c6fe4 3838
72bf9492 3839 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3840 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3841 {
72bf9492 3842 switch (pdi->tag)
c906108c
SS
3843 {
3844 case DW_TAG_subprogram:
5734ee8b 3845 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3846 break;
72929c62 3847 case DW_TAG_constant:
c906108c
SS
3848 case DW_TAG_variable:
3849 case DW_TAG_typedef:
91c24f0a 3850 case DW_TAG_union_type:
72bf9492 3851 if (!pdi->is_declaration)
63d06c5c 3852 {
72bf9492 3853 add_partial_symbol (pdi, cu);
63d06c5c
DC
3854 }
3855 break;
c906108c 3856 case DW_TAG_class_type:
680b30c7 3857 case DW_TAG_interface_type:
c906108c 3858 case DW_TAG_structure_type:
72bf9492 3859 if (!pdi->is_declaration)
c906108c 3860 {
72bf9492 3861 add_partial_symbol (pdi, cu);
c906108c
SS
3862 }
3863 break;
91c24f0a 3864 case DW_TAG_enumeration_type:
72bf9492
DJ
3865 if (!pdi->is_declaration)
3866 add_partial_enumeration (pdi, cu);
c906108c
SS
3867 break;
3868 case DW_TAG_base_type:
a02abb62 3869 case DW_TAG_subrange_type:
c906108c 3870 /* File scope base type definitions are added to the partial
c5aa993b 3871 symbol table. */
72bf9492 3872 add_partial_symbol (pdi, cu);
c906108c 3873 break;
d9fa45fe 3874 case DW_TAG_namespace:
5734ee8b 3875 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3876 break;
5d7cb8df
JK
3877 case DW_TAG_module:
3878 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3879 break;
c906108c
SS
3880 default:
3881 break;
3882 }
3883 }
3884
72bf9492
DJ
3885 /* If the die has a sibling, skip to the sibling. */
3886
3887 pdi = pdi->die_sibling;
3888 }
3889}
3890
3891/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3892
72bf9492 3893 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3894 name is concatenated with "::" and the partial DIE's name. For
3895 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3896 Enumerators are an exception; they use the scope of their parent
3897 enumeration type, i.e. the name of the enumeration type is not
3898 prepended to the enumerator.
91c24f0a 3899
72bf9492
DJ
3900 There are two complexities. One is DW_AT_specification; in this
3901 case "parent" means the parent of the target of the specification,
3902 instead of the direct parent of the DIE. The other is compilers
3903 which do not emit DW_TAG_namespace; in this case we try to guess
3904 the fully qualified name of structure types from their members'
3905 linkage names. This must be done using the DIE's children rather
3906 than the children of any DW_AT_specification target. We only need
3907 to do this for structures at the top level, i.e. if the target of
3908 any DW_AT_specification (if any; otherwise the DIE itself) does not
3909 have a parent. */
3910
3911/* Compute the scope prefix associated with PDI's parent, in
3912 compilation unit CU. The result will be allocated on CU's
3913 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3914 field. NULL is returned if no prefix is necessary. */
3915static char *
3916partial_die_parent_scope (struct partial_die_info *pdi,
3917 struct dwarf2_cu *cu)
3918{
3919 char *grandparent_scope;
3920 struct partial_die_info *parent, *real_pdi;
91c24f0a 3921
72bf9492
DJ
3922 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3923 then this means the parent of the specification DIE. */
3924
3925 real_pdi = pdi;
72bf9492 3926 while (real_pdi->has_specification)
10b3939b 3927 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3928
3929 parent = real_pdi->die_parent;
3930 if (parent == NULL)
3931 return NULL;
3932
3933 if (parent->scope_set)
3934 return parent->scope;
3935
3936 fixup_partial_die (parent, cu);
3937
10b3939b 3938 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3939
acebe513
UW
3940 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3941 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3942 Work around this problem here. */
3943 if (cu->language == language_cplus
6e70227d 3944 && parent->tag == DW_TAG_namespace
acebe513
UW
3945 && strcmp (parent->name, "::") == 0
3946 && grandparent_scope == NULL)
3947 {
3948 parent->scope = NULL;
3949 parent->scope_set = 1;
3950 return NULL;
3951 }
3952
9c6c53f7
SA
3953 if (pdi->tag == DW_TAG_enumerator)
3954 /* Enumerators should not get the name of the enumeration as a prefix. */
3955 parent->scope = grandparent_scope;
3956 else if (parent->tag == DW_TAG_namespace
f55ee35c 3957 || parent->tag == DW_TAG_module
72bf9492
DJ
3958 || parent->tag == DW_TAG_structure_type
3959 || parent->tag == DW_TAG_class_type
680b30c7 3960 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3961 || parent->tag == DW_TAG_union_type
3962 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3963 {
3964 if (grandparent_scope == NULL)
3965 parent->scope = parent->name;
3966 else
3e43a32a
MS
3967 parent->scope = typename_concat (&cu->comp_unit_obstack,
3968 grandparent_scope,
f55ee35c 3969 parent->name, 0, cu);
72bf9492 3970 }
72bf9492
DJ
3971 else
3972 {
3973 /* FIXME drow/2004-04-01: What should we be doing with
3974 function-local names? For partial symbols, we should probably be
3975 ignoring them. */
3976 complaint (&symfile_complaints,
e2e0b3e5 3977 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3978 parent->tag, pdi->offset);
3979 parent->scope = grandparent_scope;
c906108c
SS
3980 }
3981
72bf9492
DJ
3982 parent->scope_set = 1;
3983 return parent->scope;
3984}
3985
3986/* Return the fully scoped name associated with PDI, from compilation unit
3987 CU. The result will be allocated with malloc. */
3988static char *
3989partial_die_full_name (struct partial_die_info *pdi,
3990 struct dwarf2_cu *cu)
3991{
3992 char *parent_scope;
3993
98bfdba5
PA
3994 /* If this is a template instantiation, we can not work out the
3995 template arguments from partial DIEs. So, unfortunately, we have
3996 to go through the full DIEs. At least any work we do building
3997 types here will be reused if full symbols are loaded later. */
3998 if (pdi->has_template_arguments)
3999 {
4000 fixup_partial_die (pdi, cu);
4001
4002 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4003 {
4004 struct die_info *die;
4005 struct attribute attr;
4006 struct dwarf2_cu *ref_cu = cu;
4007
4008 attr.name = 0;
4009 attr.form = DW_FORM_ref_addr;
4010 attr.u.addr = pdi->offset;
4011 die = follow_die_ref (NULL, &attr, &ref_cu);
4012
4013 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4014 }
4015 }
4016
72bf9492
DJ
4017 parent_scope = partial_die_parent_scope (pdi, cu);
4018 if (parent_scope == NULL)
4019 return NULL;
4020 else
f55ee35c 4021 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4022}
4023
4024static void
72bf9492 4025add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4026{
e7c27a73 4027 struct objfile *objfile = cu->objfile;
c906108c 4028 CORE_ADDR addr = 0;
decbce07 4029 char *actual_name = NULL;
5c4e30ca 4030 const struct partial_symbol *psym = NULL;
e142c38c 4031 CORE_ADDR baseaddr;
72bf9492 4032 int built_actual_name = 0;
e142c38c
DJ
4033
4034 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4035
94af9270
KS
4036 actual_name = partial_die_full_name (pdi, cu);
4037 if (actual_name)
4038 built_actual_name = 1;
63d06c5c 4039
72bf9492
DJ
4040 if (actual_name == NULL)
4041 actual_name = pdi->name;
4042
c906108c
SS
4043 switch (pdi->tag)
4044 {
4045 case DW_TAG_subprogram:
2cfa0c8d 4046 if (pdi->is_external || cu->language == language_ada)
c906108c 4047 {
2cfa0c8d
JB
4048 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4049 of the global scope. But in Ada, we want to be able to access
4050 nested procedures globally. So all Ada subprograms are stored
4051 in the global scope. */
f47fb265 4052 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4053 mst_text, objfile); */
f47fb265
MS
4054 add_psymbol_to_list (actual_name, strlen (actual_name),
4055 built_actual_name,
4056 VAR_DOMAIN, LOC_BLOCK,
4057 &objfile->global_psymbols,
4058 0, pdi->lowpc + baseaddr,
4059 cu->language, objfile);
c906108c
SS
4060 }
4061 else
4062 {
f47fb265 4063 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4064 mst_file_text, objfile); */
f47fb265
MS
4065 add_psymbol_to_list (actual_name, strlen (actual_name),
4066 built_actual_name,
4067 VAR_DOMAIN, LOC_BLOCK,
4068 &objfile->static_psymbols,
4069 0, pdi->lowpc + baseaddr,
4070 cu->language, objfile);
c906108c
SS
4071 }
4072 break;
72929c62
JB
4073 case DW_TAG_constant:
4074 {
4075 struct psymbol_allocation_list *list;
4076
4077 if (pdi->is_external)
4078 list = &objfile->global_psymbols;
4079 else
4080 list = &objfile->static_psymbols;
f47fb265
MS
4081 add_psymbol_to_list (actual_name, strlen (actual_name),
4082 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4083 list, 0, 0, cu->language, objfile);
72929c62
JB
4084 }
4085 break;
c906108c 4086 case DW_TAG_variable:
caac4577
JG
4087 if (pdi->locdesc)
4088 addr = decode_locdesc (pdi->locdesc, cu);
4089
4090 if (pdi->locdesc
4091 && addr == 0
4092 && !dwarf2_per_objfile->has_section_at_zero)
4093 {
4094 /* A global or static variable may also have been stripped
4095 out by the linker if unused, in which case its address
4096 will be nullified; do not add such variables into partial
4097 symbol table then. */
4098 }
4099 else if (pdi->is_external)
c906108c
SS
4100 {
4101 /* Global Variable.
4102 Don't enter into the minimal symbol tables as there is
4103 a minimal symbol table entry from the ELF symbols already.
4104 Enter into partial symbol table if it has a location
4105 descriptor or a type.
4106 If the location descriptor is missing, new_symbol will create
4107 a LOC_UNRESOLVED symbol, the address of the variable will then
4108 be determined from the minimal symbol table whenever the variable
4109 is referenced.
4110 The address for the partial symbol table entry is not
4111 used by GDB, but it comes in handy for debugging partial symbol
4112 table building. */
4113
c906108c 4114 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4115 add_psymbol_to_list (actual_name, strlen (actual_name),
4116 built_actual_name,
4117 VAR_DOMAIN, LOC_STATIC,
4118 &objfile->global_psymbols,
4119 0, addr + baseaddr,
4120 cu->language, objfile);
c906108c
SS
4121 }
4122 else
4123 {
0963b4bd 4124 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4125 if (pdi->locdesc == NULL)
decbce07
MS
4126 {
4127 if (built_actual_name)
4128 xfree (actual_name);
4129 return;
4130 }
f47fb265 4131 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4132 mst_file_data, objfile); */
f47fb265
MS
4133 add_psymbol_to_list (actual_name, strlen (actual_name),
4134 built_actual_name,
4135 VAR_DOMAIN, LOC_STATIC,
4136 &objfile->static_psymbols,
4137 0, addr + baseaddr,
4138 cu->language, objfile);
c906108c
SS
4139 }
4140 break;
4141 case DW_TAG_typedef:
4142 case DW_TAG_base_type:
a02abb62 4143 case DW_TAG_subrange_type:
38d518c9 4144 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4145 built_actual_name,
176620f1 4146 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4147 &objfile->static_psymbols,
e142c38c 4148 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4149 break;
72bf9492
DJ
4150 case DW_TAG_namespace:
4151 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4152 built_actual_name,
72bf9492
DJ
4153 VAR_DOMAIN, LOC_TYPEDEF,
4154 &objfile->global_psymbols,
4155 0, (CORE_ADDR) 0, cu->language, objfile);
4156 break;
c906108c 4157 case DW_TAG_class_type:
680b30c7 4158 case DW_TAG_interface_type:
c906108c
SS
4159 case DW_TAG_structure_type:
4160 case DW_TAG_union_type:
4161 case DW_TAG_enumeration_type:
fa4028e9
JB
4162 /* Skip external references. The DWARF standard says in the section
4163 about "Structure, Union, and Class Type Entries": "An incomplete
4164 structure, union or class type is represented by a structure,
4165 union or class entry that does not have a byte size attribute
4166 and that has a DW_AT_declaration attribute." */
4167 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4168 {
4169 if (built_actual_name)
4170 xfree (actual_name);
4171 return;
4172 }
fa4028e9 4173
63d06c5c
DC
4174 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4175 static vs. global. */
38d518c9 4176 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4177 built_actual_name,
176620f1 4178 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4179 (cu->language == language_cplus
4180 || cu->language == language_java)
63d06c5c
DC
4181 ? &objfile->global_psymbols
4182 : &objfile->static_psymbols,
e142c38c 4183 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4184
c906108c
SS
4185 break;
4186 case DW_TAG_enumerator:
38d518c9 4187 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4188 built_actual_name,
176620f1 4189 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4190 (cu->language == language_cplus
4191 || cu->language == language_java)
f6fe98ef
DJ
4192 ? &objfile->global_psymbols
4193 : &objfile->static_psymbols,
e142c38c 4194 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4195 break;
4196 default:
4197 break;
4198 }
5c4e30ca 4199
72bf9492
DJ
4200 if (built_actual_name)
4201 xfree (actual_name);
c906108c
SS
4202}
4203
5c4e30ca
DC
4204/* Read a partial die corresponding to a namespace; also, add a symbol
4205 corresponding to that namespace to the symbol table. NAMESPACE is
4206 the name of the enclosing namespace. */
91c24f0a 4207
72bf9492
DJ
4208static void
4209add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4210 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4211 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4212{
72bf9492 4213 /* Add a symbol for the namespace. */
e7c27a73 4214
72bf9492 4215 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4216
4217 /* Now scan partial symbols in that namespace. */
4218
91c24f0a 4219 if (pdi->has_children)
5734ee8b 4220 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4221}
4222
5d7cb8df
JK
4223/* Read a partial die corresponding to a Fortran module. */
4224
4225static void
4226add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4227 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4228{
f55ee35c 4229 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4230
4231 if (pdi->has_children)
4232 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4233}
4234
bc30ff58
JB
4235/* Read a partial die corresponding to a subprogram and create a partial
4236 symbol for that subprogram. When the CU language allows it, this
4237 routine also defines a partial symbol for each nested subprogram
4238 that this subprogram contains.
6e70227d 4239
bc30ff58
JB
4240 DIE my also be a lexical block, in which case we simply search
4241 recursively for suprograms defined inside that lexical block.
4242 Again, this is only performed when the CU language allows this
4243 type of definitions. */
4244
4245static void
4246add_partial_subprogram (struct partial_die_info *pdi,
4247 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4248 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4249{
4250 if (pdi->tag == DW_TAG_subprogram)
4251 {
4252 if (pdi->has_pc_info)
4253 {
4254 if (pdi->lowpc < *lowpc)
4255 *lowpc = pdi->lowpc;
4256 if (pdi->highpc > *highpc)
4257 *highpc = pdi->highpc;
5734ee8b
DJ
4258 if (need_pc)
4259 {
4260 CORE_ADDR baseaddr;
4261 struct objfile *objfile = cu->objfile;
4262
4263 baseaddr = ANOFFSET (objfile->section_offsets,
4264 SECT_OFF_TEXT (objfile));
4265 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4266 pdi->lowpc + baseaddr,
4267 pdi->highpc - 1 + baseaddr,
9291a0cd 4268 cu->per_cu->v.psymtab);
5734ee8b 4269 }
bc30ff58 4270 if (!pdi->is_declaration)
e8d05480
JB
4271 /* Ignore subprogram DIEs that do not have a name, they are
4272 illegal. Do not emit a complaint at this point, we will
4273 do so when we convert this psymtab into a symtab. */
4274 if (pdi->name)
4275 add_partial_symbol (pdi, cu);
bc30ff58
JB
4276 }
4277 }
6e70227d 4278
bc30ff58
JB
4279 if (! pdi->has_children)
4280 return;
4281
4282 if (cu->language == language_ada)
4283 {
4284 pdi = pdi->die_child;
4285 while (pdi != NULL)
4286 {
4287 fixup_partial_die (pdi, cu);
4288 if (pdi->tag == DW_TAG_subprogram
4289 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4290 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4291 pdi = pdi->die_sibling;
4292 }
4293 }
4294}
4295
91c24f0a
DC
4296/* Read a partial die corresponding to an enumeration type. */
4297
72bf9492
DJ
4298static void
4299add_partial_enumeration (struct partial_die_info *enum_pdi,
4300 struct dwarf2_cu *cu)
91c24f0a 4301{
72bf9492 4302 struct partial_die_info *pdi;
91c24f0a
DC
4303
4304 if (enum_pdi->name != NULL)
72bf9492
DJ
4305 add_partial_symbol (enum_pdi, cu);
4306
4307 pdi = enum_pdi->die_child;
4308 while (pdi)
91c24f0a 4309 {
72bf9492 4310 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4311 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4312 else
72bf9492
DJ
4313 add_partial_symbol (pdi, cu);
4314 pdi = pdi->die_sibling;
91c24f0a 4315 }
91c24f0a
DC
4316}
4317
6caca83c
CC
4318/* Return the initial uleb128 in the die at INFO_PTR. */
4319
4320static unsigned int
4321peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4322{
4323 unsigned int bytes_read;
4324
4325 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4326}
4327
4bb7a0a7
DJ
4328/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4329 Return the corresponding abbrev, or NULL if the number is zero (indicating
4330 an empty DIE). In either case *BYTES_READ will be set to the length of
4331 the initial number. */
4332
4333static struct abbrev_info *
fe1b8b76 4334peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4335 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4336{
4337 bfd *abfd = cu->objfile->obfd;
4338 unsigned int abbrev_number;
4339 struct abbrev_info *abbrev;
4340
4341 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4342
4343 if (abbrev_number == 0)
4344 return NULL;
4345
4346 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4347 if (!abbrev)
4348 {
3e43a32a
MS
4349 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4350 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4351 }
4352
4353 return abbrev;
4354}
4355
93311388
DE
4356/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4357 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4358 DIE. Any children of the skipped DIEs will also be skipped. */
4359
fe1b8b76 4360static gdb_byte *
93311388 4361skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4362{
4363 struct abbrev_info *abbrev;
4364 unsigned int bytes_read;
4365
4366 while (1)
4367 {
4368 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4369 if (abbrev == NULL)
4370 return info_ptr + bytes_read;
4371 else
93311388 4372 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4373 }
4374}
4375
93311388
DE
4376/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4377 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4378 abbrev corresponding to that skipped uleb128 should be passed in
4379 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4380 children. */
4381
fe1b8b76 4382static gdb_byte *
93311388
DE
4383skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4384 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4385{
4386 unsigned int bytes_read;
4387 struct attribute attr;
4388 bfd *abfd = cu->objfile->obfd;
4389 unsigned int form, i;
4390
4391 for (i = 0; i < abbrev->num_attrs; i++)
4392 {
4393 /* The only abbrev we care about is DW_AT_sibling. */
4394 if (abbrev->attrs[i].name == DW_AT_sibling)
4395 {
4396 read_attribute (&attr, &abbrev->attrs[i],
4397 abfd, info_ptr, cu);
4398 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4399 complaint (&symfile_complaints,
4400 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4401 else
93311388 4402 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4403 }
4404
4405 /* If it isn't DW_AT_sibling, skip this attribute. */
4406 form = abbrev->attrs[i].form;
4407 skip_attribute:
4408 switch (form)
4409 {
4bb7a0a7 4410 case DW_FORM_ref_addr:
ae411497
TT
4411 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4412 and later it is offset sized. */
4413 if (cu->header.version == 2)
4414 info_ptr += cu->header.addr_size;
4415 else
4416 info_ptr += cu->header.offset_size;
4417 break;
4418 case DW_FORM_addr:
4bb7a0a7
DJ
4419 info_ptr += cu->header.addr_size;
4420 break;
4421 case DW_FORM_data1:
4422 case DW_FORM_ref1:
4423 case DW_FORM_flag:
4424 info_ptr += 1;
4425 break;
2dc7f7b3
TT
4426 case DW_FORM_flag_present:
4427 break;
4bb7a0a7
DJ
4428 case DW_FORM_data2:
4429 case DW_FORM_ref2:
4430 info_ptr += 2;
4431 break;
4432 case DW_FORM_data4:
4433 case DW_FORM_ref4:
4434 info_ptr += 4;
4435 break;
4436 case DW_FORM_data8:
4437 case DW_FORM_ref8:
55f1336d 4438 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4439 info_ptr += 8;
4440 break;
4441 case DW_FORM_string:
9b1c24c8 4442 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4443 info_ptr += bytes_read;
4444 break;
2dc7f7b3 4445 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4446 case DW_FORM_strp:
4447 info_ptr += cu->header.offset_size;
4448 break;
2dc7f7b3 4449 case DW_FORM_exprloc:
4bb7a0a7
DJ
4450 case DW_FORM_block:
4451 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4452 info_ptr += bytes_read;
4453 break;
4454 case DW_FORM_block1:
4455 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4456 break;
4457 case DW_FORM_block2:
4458 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4459 break;
4460 case DW_FORM_block4:
4461 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4462 break;
4463 case DW_FORM_sdata:
4464 case DW_FORM_udata:
4465 case DW_FORM_ref_udata:
4466 info_ptr = skip_leb128 (abfd, info_ptr);
4467 break;
4468 case DW_FORM_indirect:
4469 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4470 info_ptr += bytes_read;
4471 /* We need to continue parsing from here, so just go back to
4472 the top. */
4473 goto skip_attribute;
4474
4475 default:
3e43a32a
MS
4476 error (_("Dwarf Error: Cannot handle %s "
4477 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4478 dwarf_form_name (form),
4479 bfd_get_filename (abfd));
4480 }
4481 }
4482
4483 if (abbrev->has_children)
93311388 4484 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4485 else
4486 return info_ptr;
4487}
4488
93311388
DE
4489/* Locate ORIG_PDI's sibling.
4490 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4491 in BUFFER. */
91c24f0a 4492
fe1b8b76 4493static gdb_byte *
93311388
DE
4494locate_pdi_sibling (struct partial_die_info *orig_pdi,
4495 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4496 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4497{
4498 /* Do we know the sibling already? */
72bf9492 4499
91c24f0a
DC
4500 if (orig_pdi->sibling)
4501 return orig_pdi->sibling;
4502
4503 /* Are there any children to deal with? */
4504
4505 if (!orig_pdi->has_children)
4506 return info_ptr;
4507
4bb7a0a7 4508 /* Skip the children the long way. */
91c24f0a 4509
93311388 4510 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4511}
4512
c906108c
SS
4513/* Expand this partial symbol table into a full symbol table. */
4514
4515static void
fba45db2 4516dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4517{
c906108c
SS
4518 if (pst != NULL)
4519 {
4520 if (pst->readin)
4521 {
3e43a32a
MS
4522 warning (_("bug: psymtab for %s is already read in."),
4523 pst->filename);
c906108c
SS
4524 }
4525 else
4526 {
4527 if (info_verbose)
4528 {
3e43a32a
MS
4529 printf_filtered (_("Reading in symbols for %s..."),
4530 pst->filename);
c906108c
SS
4531 gdb_flush (gdb_stdout);
4532 }
4533
10b3939b
DJ
4534 /* Restore our global data. */
4535 dwarf2_per_objfile = objfile_data (pst->objfile,
4536 dwarf2_objfile_data_key);
4537
b2ab525c
KB
4538 /* If this psymtab is constructed from a debug-only objfile, the
4539 has_section_at_zero flag will not necessarily be correct. We
4540 can get the correct value for this flag by looking at the data
4541 associated with the (presumably stripped) associated objfile. */
4542 if (pst->objfile->separate_debug_objfile_backlink)
4543 {
4544 struct dwarf2_per_objfile *dpo_backlink
4545 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4546 dwarf2_objfile_data_key);
9a619af0 4547
b2ab525c
KB
4548 dwarf2_per_objfile->has_section_at_zero
4549 = dpo_backlink->has_section_at_zero;
4550 }
4551
98bfdba5
PA
4552 dwarf2_per_objfile->reading_partial_symbols = 0;
4553
c906108c
SS
4554 psymtab_to_symtab_1 (pst);
4555
4556 /* Finish up the debug error message. */
4557 if (info_verbose)
a3f17187 4558 printf_filtered (_("done.\n"));
c906108c
SS
4559 }
4560 }
4561}
4562
10b3939b
DJ
4563/* Add PER_CU to the queue. */
4564
4565static void
03dd20cc 4566queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4567{
4568 struct dwarf2_queue_item *item;
4569
4570 per_cu->queued = 1;
4571 item = xmalloc (sizeof (*item));
4572 item->per_cu = per_cu;
4573 item->next = NULL;
4574
4575 if (dwarf2_queue == NULL)
4576 dwarf2_queue = item;
4577 else
4578 dwarf2_queue_tail->next = item;
4579
4580 dwarf2_queue_tail = item;
4581}
4582
4583/* Process the queue. */
4584
4585static void
4586process_queue (struct objfile *objfile)
4587{
4588 struct dwarf2_queue_item *item, *next_item;
4589
03dd20cc
DJ
4590 /* The queue starts out with one item, but following a DIE reference
4591 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4592 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4593 {
9291a0cd
TT
4594 if (dwarf2_per_objfile->using_index
4595 ? !item->per_cu->v.quick->symtab
4596 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4597 process_full_comp_unit (item->per_cu);
4598
4599 item->per_cu->queued = 0;
4600 next_item = item->next;
4601 xfree (item);
4602 }
4603
4604 dwarf2_queue_tail = NULL;
4605}
4606
4607/* Free all allocated queue entries. This function only releases anything if
4608 an error was thrown; if the queue was processed then it would have been
4609 freed as we went along. */
4610
4611static void
4612dwarf2_release_queue (void *dummy)
4613{
4614 struct dwarf2_queue_item *item, *last;
4615
4616 item = dwarf2_queue;
4617 while (item)
4618 {
4619 /* Anything still marked queued is likely to be in an
4620 inconsistent state, so discard it. */
4621 if (item->per_cu->queued)
4622 {
4623 if (item->per_cu->cu != NULL)
4624 free_one_cached_comp_unit (item->per_cu->cu);
4625 item->per_cu->queued = 0;
4626 }
4627
4628 last = item;
4629 item = item->next;
4630 xfree (last);
4631 }
4632
4633 dwarf2_queue = dwarf2_queue_tail = NULL;
4634}
4635
4636/* Read in full symbols for PST, and anything it depends on. */
4637
c906108c 4638static void
fba45db2 4639psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4640{
10b3939b 4641 struct dwarf2_per_cu_data *per_cu;
c906108c 4642 struct cleanup *back_to;
aaa75496
JB
4643 int i;
4644
4645 for (i = 0; i < pst->number_of_dependencies; i++)
4646 if (!pst->dependencies[i]->readin)
4647 {
4648 /* Inform about additional files that need to be read in. */
4649 if (info_verbose)
4650 {
a3f17187 4651 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4652 fputs_filtered (" ", gdb_stdout);
4653 wrap_here ("");
4654 fputs_filtered ("and ", gdb_stdout);
4655 wrap_here ("");
4656 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4657 wrap_here (""); /* Flush output. */
aaa75496
JB
4658 gdb_flush (gdb_stdout);
4659 }
4660 psymtab_to_symtab_1 (pst->dependencies[i]);
4661 }
4662
e38df1d0 4663 per_cu = pst->read_symtab_private;
10b3939b
DJ
4664
4665 if (per_cu == NULL)
aaa75496
JB
4666 {
4667 /* It's an include file, no symbols to read for it.
4668 Everything is in the parent symtab. */
4669 pst->readin = 1;
4670 return;
4671 }
c906108c 4672
9291a0cd 4673 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4674}
4675
93311388 4676/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4677
93311388 4678static void
3e43a32a
MS
4679load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4680 struct objfile *objfile)
10b3939b 4681{
31ffec48 4682 bfd *abfd = objfile->obfd;
10b3939b 4683 struct dwarf2_cu *cu;
c764a876 4684 unsigned int offset;
93311388 4685 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4686 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4687 struct attribute *attr;
98bfdba5 4688 int read_cu = 0;
6502dd73 4689
8b70b953 4690 gdb_assert (! per_cu->debug_type_section);
348e048f 4691
c906108c 4692 /* Set local variables from the partial symbol table info. */
10b3939b 4693 offset = per_cu->offset;
6502dd73 4694
be391dca 4695 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4696 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4697 beg_of_comp_unit = info_ptr;
63d06c5c 4698
98bfdba5
PA
4699 if (per_cu->cu == NULL)
4700 {
9816fde3
JK
4701 cu = xmalloc (sizeof (*cu));
4702 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4703
4704 read_cu = 1;
c906108c 4705
98bfdba5
PA
4706 /* If an error occurs while loading, release our storage. */
4707 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4708
98bfdba5
PA
4709 /* Read in the comp_unit header. */
4710 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4711
6caca83c
CC
4712 /* Skip dummy compilation units. */
4713 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4714 + dwarf2_per_objfile->info.size)
4715 || peek_abbrev_code (abfd, info_ptr) == 0)
4716 {
4717 do_cleanups (free_cu_cleanup);
4718 return;
4719 }
4720
98bfdba5
PA
4721 /* Complete the cu_header. */
4722 cu->header.offset = offset;
4723 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4724
98bfdba5
PA
4725 /* Read the abbrevs for this compilation unit. */
4726 dwarf2_read_abbrevs (abfd, cu);
4727 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4728
98bfdba5
PA
4729 /* Link this compilation unit into the compilation unit tree. */
4730 per_cu->cu = cu;
4731 cu->per_cu = per_cu;
98bfdba5
PA
4732
4733 /* Link this CU into read_in_chain. */
4734 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4735 dwarf2_per_objfile->read_in_chain = per_cu;
4736 }
4737 else
4738 {
4739 cu = per_cu->cu;
4740 info_ptr += cu->header.first_die_offset;
4741 }
e142c38c 4742
93311388 4743 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4744
4745 /* We try not to read any attributes in this function, because not
4746 all objfiles needed for references have been loaded yet, and symbol
4747 table processing isn't initialized. But we have to set the CU language,
4748 or we won't be able to build types correctly. */
9816fde3 4749 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4750
a6c727b2
DJ
4751 /* Similarly, if we do not read the producer, we can not apply
4752 producer-specific interpretation. */
4753 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4754 if (attr)
4755 cu->producer = DW_STRING (attr);
4756
98bfdba5
PA
4757 if (read_cu)
4758 {
4759 do_cleanups (free_abbrevs_cleanup);
e142c38c 4760
98bfdba5
PA
4761 /* We've successfully allocated this compilation unit. Let our
4762 caller clean it up when finished with it. */
4763 discard_cleanups (free_cu_cleanup);
4764 }
10b3939b
DJ
4765}
4766
3da10d80
KS
4767/* Add a DIE to the delayed physname list. */
4768
4769static void
4770add_to_method_list (struct type *type, int fnfield_index, int index,
4771 const char *name, struct die_info *die,
4772 struct dwarf2_cu *cu)
4773{
4774 struct delayed_method_info mi;
4775 mi.type = type;
4776 mi.fnfield_index = fnfield_index;
4777 mi.index = index;
4778 mi.name = name;
4779 mi.die = die;
4780 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4781}
4782
4783/* A cleanup for freeing the delayed method list. */
4784
4785static void
4786free_delayed_list (void *ptr)
4787{
4788 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4789 if (cu->method_list != NULL)
4790 {
4791 VEC_free (delayed_method_info, cu->method_list);
4792 cu->method_list = NULL;
4793 }
4794}
4795
4796/* Compute the physnames of any methods on the CU's method list.
4797
4798 The computation of method physnames is delayed in order to avoid the
4799 (bad) condition that one of the method's formal parameters is of an as yet
4800 incomplete type. */
4801
4802static void
4803compute_delayed_physnames (struct dwarf2_cu *cu)
4804{
4805 int i;
4806 struct delayed_method_info *mi;
4807 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4808 {
1d06ead6 4809 const char *physname;
3da10d80
KS
4810 struct fn_fieldlist *fn_flp
4811 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4812 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4813 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4814 }
4815}
4816
10b3939b
DJ
4817/* Generate full symbol information for PST and CU, whose DIEs have
4818 already been loaded into memory. */
4819
4820static void
4821process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4822{
10b3939b 4823 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4824 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4825 CORE_ADDR lowpc, highpc;
4826 struct symtab *symtab;
3da10d80 4827 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4828 CORE_ADDR baseaddr;
4829
4830 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4831
10b3939b
DJ
4832 buildsym_init ();
4833 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4834 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4835
4836 cu->list_in_scope = &file_symbols;
c906108c
SS
4837
4838 /* Do line number decoding in read_file_scope () */
10b3939b 4839 process_die (cu->dies, cu);
c906108c 4840
3da10d80
KS
4841 /* Now that we have processed all the DIEs in the CU, all the types
4842 should be complete, and it should now be safe to compute all of the
4843 physnames. */
4844 compute_delayed_physnames (cu);
4845 do_cleanups (delayed_list_cleanup);
4846
fae299cd
DC
4847 /* Some compilers don't define a DW_AT_high_pc attribute for the
4848 compilation unit. If the DW_AT_high_pc is missing, synthesize
4849 it, by scanning the DIE's below the compilation unit. */
10b3939b 4850 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4851
613e1657 4852 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4853
8be455d7 4854 if (symtab != NULL)
c906108c 4855 {
df15bd07 4856 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4857
8be455d7
JK
4858 /* Set symtab language to language from DW_AT_language. If the
4859 compilation is from a C file generated by language preprocessors, do
4860 not set the language if it was already deduced by start_subfile. */
4861 if (!(cu->language == language_c && symtab->language != language_c))
4862 symtab->language = cu->language;
4863
4864 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4865 produce DW_AT_location with location lists but it can be possibly
4866 invalid without -fvar-tracking.
4867
4868 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4869 needed, it would be wrong due to missing DW_AT_producer there.
4870
4871 Still one can confuse GDB by using non-standard GCC compilation
4872 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4873 */
4632c0d0 4874 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4875 symtab->locations_valid = 1;
e0d00bc7
JK
4876
4877 if (gcc_4_minor >= 5)
4878 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4879
4880 symtab->call_site_htab = cu->call_site_htab;
c906108c 4881 }
9291a0cd
TT
4882
4883 if (dwarf2_per_objfile->using_index)
4884 per_cu->v.quick->symtab = symtab;
4885 else
4886 {
4887 struct partial_symtab *pst = per_cu->v.psymtab;
4888 pst->symtab = symtab;
4889 pst->readin = 1;
4890 }
c906108c
SS
4891
4892 do_cleanups (back_to);
4893}
4894
4895/* Process a die and its children. */
4896
4897static void
e7c27a73 4898process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4899{
4900 switch (die->tag)
4901 {
4902 case DW_TAG_padding:
4903 break;
4904 case DW_TAG_compile_unit:
e7c27a73 4905 read_file_scope (die, cu);
c906108c 4906 break;
348e048f
DE
4907 case DW_TAG_type_unit:
4908 read_type_unit_scope (die, cu);
4909 break;
c906108c 4910 case DW_TAG_subprogram:
c906108c 4911 case DW_TAG_inlined_subroutine:
edb3359d 4912 read_func_scope (die, cu);
c906108c
SS
4913 break;
4914 case DW_TAG_lexical_block:
14898363
L
4915 case DW_TAG_try_block:
4916 case DW_TAG_catch_block:
e7c27a73 4917 read_lexical_block_scope (die, cu);
c906108c 4918 break;
96408a79
SA
4919 case DW_TAG_GNU_call_site:
4920 read_call_site_scope (die, cu);
4921 break;
c906108c 4922 case DW_TAG_class_type:
680b30c7 4923 case DW_TAG_interface_type:
c906108c
SS
4924 case DW_TAG_structure_type:
4925 case DW_TAG_union_type:
134d01f1 4926 process_structure_scope (die, cu);
c906108c
SS
4927 break;
4928 case DW_TAG_enumeration_type:
134d01f1 4929 process_enumeration_scope (die, cu);
c906108c 4930 break;
134d01f1 4931
f792889a
DJ
4932 /* These dies have a type, but processing them does not create
4933 a symbol or recurse to process the children. Therefore we can
4934 read them on-demand through read_type_die. */
c906108c 4935 case DW_TAG_subroutine_type:
72019c9c 4936 case DW_TAG_set_type:
c906108c 4937 case DW_TAG_array_type:
c906108c 4938 case DW_TAG_pointer_type:
c906108c 4939 case DW_TAG_ptr_to_member_type:
c906108c 4940 case DW_TAG_reference_type:
c906108c 4941 case DW_TAG_string_type:
c906108c 4942 break;
134d01f1 4943
c906108c 4944 case DW_TAG_base_type:
a02abb62 4945 case DW_TAG_subrange_type:
cb249c71 4946 case DW_TAG_typedef:
134d01f1
DJ
4947 /* Add a typedef symbol for the type definition, if it has a
4948 DW_AT_name. */
f792889a 4949 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4950 break;
c906108c 4951 case DW_TAG_common_block:
e7c27a73 4952 read_common_block (die, cu);
c906108c
SS
4953 break;
4954 case DW_TAG_common_inclusion:
4955 break;
d9fa45fe 4956 case DW_TAG_namespace:
63d06c5c 4957 processing_has_namespace_info = 1;
e7c27a73 4958 read_namespace (die, cu);
d9fa45fe 4959 break;
5d7cb8df 4960 case DW_TAG_module:
f55ee35c 4961 processing_has_namespace_info = 1;
5d7cb8df
JK
4962 read_module (die, cu);
4963 break;
d9fa45fe
DC
4964 case DW_TAG_imported_declaration:
4965 case DW_TAG_imported_module:
63d06c5c 4966 processing_has_namespace_info = 1;
27aa8d6a
SW
4967 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4968 || cu->language != language_fortran))
4969 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4970 dwarf_tag_name (die->tag));
4971 read_import_statement (die, cu);
d9fa45fe 4972 break;
c906108c 4973 default:
e7c27a73 4974 new_symbol (die, NULL, cu);
c906108c
SS
4975 break;
4976 }
4977}
4978
94af9270
KS
4979/* A helper function for dwarf2_compute_name which determines whether DIE
4980 needs to have the name of the scope prepended to the name listed in the
4981 die. */
4982
4983static int
4984die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4985{
1c809c68
TT
4986 struct attribute *attr;
4987
94af9270
KS
4988 switch (die->tag)
4989 {
4990 case DW_TAG_namespace:
4991 case DW_TAG_typedef:
4992 case DW_TAG_class_type:
4993 case DW_TAG_interface_type:
4994 case DW_TAG_structure_type:
4995 case DW_TAG_union_type:
4996 case DW_TAG_enumeration_type:
4997 case DW_TAG_enumerator:
4998 case DW_TAG_subprogram:
4999 case DW_TAG_member:
5000 return 1;
5001
5002 case DW_TAG_variable:
c2b0a229 5003 case DW_TAG_constant:
94af9270
KS
5004 /* We only need to prefix "globally" visible variables. These include
5005 any variable marked with DW_AT_external or any variable that
5006 lives in a namespace. [Variables in anonymous namespaces
5007 require prefixing, but they are not DW_AT_external.] */
5008
5009 if (dwarf2_attr (die, DW_AT_specification, cu))
5010 {
5011 struct dwarf2_cu *spec_cu = cu;
9a619af0 5012
94af9270
KS
5013 return die_needs_namespace (die_specification (die, &spec_cu),
5014 spec_cu);
5015 }
5016
1c809c68 5017 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5018 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5019 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5020 return 0;
5021 /* A variable in a lexical block of some kind does not need a
5022 namespace, even though in C++ such variables may be external
5023 and have a mangled name. */
5024 if (die->parent->tag == DW_TAG_lexical_block
5025 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5026 || die->parent->tag == DW_TAG_catch_block
5027 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5028 return 0;
5029 return 1;
94af9270
KS
5030
5031 default:
5032 return 0;
5033 }
5034}
5035
98bfdba5
PA
5036/* Retrieve the last character from a mem_file. */
5037
5038static void
5039do_ui_file_peek_last (void *object, const char *buffer, long length)
5040{
5041 char *last_char_p = (char *) object;
5042
5043 if (length > 0)
5044 *last_char_p = buffer[length - 1];
5045}
5046
94af9270
KS
5047/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5048 compute the physname for the object, which include a method's
5049 formal parameters (C++/Java) and return type (Java).
5050
af6b7be1
JB
5051 For Ada, return the DIE's linkage name rather than the fully qualified
5052 name. PHYSNAME is ignored..
5053
94af9270
KS
5054 The result is allocated on the objfile_obstack and canonicalized. */
5055
5056static const char *
5057dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5058 int physname)
5059{
5060 if (name == NULL)
5061 name = dwarf2_name (die, cu);
5062
f55ee35c
JK
5063 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5064 compute it by typename_concat inside GDB. */
5065 if (cu->language == language_ada
5066 || (cu->language == language_fortran && physname))
5067 {
5068 /* For Ada unit, we prefer the linkage name over the name, as
5069 the former contains the exported name, which the user expects
5070 to be able to reference. Ideally, we want the user to be able
5071 to reference this entity using either natural or linkage name,
5072 but we haven't started looking at this enhancement yet. */
5073 struct attribute *attr;
5074
5075 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5076 if (attr == NULL)
5077 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5078 if (attr && DW_STRING (attr))
5079 return DW_STRING (attr);
5080 }
5081
94af9270
KS
5082 /* These are the only languages we know how to qualify names in. */
5083 if (name != NULL
f55ee35c
JK
5084 && (cu->language == language_cplus || cu->language == language_java
5085 || cu->language == language_fortran))
94af9270
KS
5086 {
5087 if (die_needs_namespace (die, cu))
5088 {
5089 long length;
5090 char *prefix;
5091 struct ui_file *buf;
5092
5093 prefix = determine_prefix (die, cu);
5094 buf = mem_fileopen ();
5095 if (*prefix != '\0')
5096 {
f55ee35c
JK
5097 char *prefixed_name = typename_concat (NULL, prefix, name,
5098 physname, cu);
9a619af0 5099
94af9270
KS
5100 fputs_unfiltered (prefixed_name, buf);
5101 xfree (prefixed_name);
5102 }
5103 else
62d5b8da 5104 fputs_unfiltered (name, buf);
94af9270 5105
98bfdba5
PA
5106 /* Template parameters may be specified in the DIE's DW_AT_name, or
5107 as children with DW_TAG_template_type_param or
5108 DW_TAG_value_type_param. If the latter, add them to the name
5109 here. If the name already has template parameters, then
5110 skip this step; some versions of GCC emit both, and
5111 it is more efficient to use the pre-computed name.
5112
5113 Something to keep in mind about this process: it is very
5114 unlikely, or in some cases downright impossible, to produce
5115 something that will match the mangled name of a function.
5116 If the definition of the function has the same debug info,
5117 we should be able to match up with it anyway. But fallbacks
5118 using the minimal symbol, for instance to find a method
5119 implemented in a stripped copy of libstdc++, will not work.
5120 If we do not have debug info for the definition, we will have to
5121 match them up some other way.
5122
5123 When we do name matching there is a related problem with function
5124 templates; two instantiated function templates are allowed to
5125 differ only by their return types, which we do not add here. */
5126
5127 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5128 {
5129 struct attribute *attr;
5130 struct die_info *child;
5131 int first = 1;
5132
5133 die->building_fullname = 1;
5134
5135 for (child = die->child; child != NULL; child = child->sibling)
5136 {
5137 struct type *type;
5138 long value;
5139 gdb_byte *bytes;
5140 struct dwarf2_locexpr_baton *baton;
5141 struct value *v;
5142
5143 if (child->tag != DW_TAG_template_type_param
5144 && child->tag != DW_TAG_template_value_param)
5145 continue;
5146
5147 if (first)
5148 {
5149 fputs_unfiltered ("<", buf);
5150 first = 0;
5151 }
5152 else
5153 fputs_unfiltered (", ", buf);
5154
5155 attr = dwarf2_attr (child, DW_AT_type, cu);
5156 if (attr == NULL)
5157 {
5158 complaint (&symfile_complaints,
5159 _("template parameter missing DW_AT_type"));
5160 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5161 continue;
5162 }
5163 type = die_type (child, cu);
5164
5165 if (child->tag == DW_TAG_template_type_param)
5166 {
5167 c_print_type (type, "", buf, -1, 0);
5168 continue;
5169 }
5170
5171 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5172 if (attr == NULL)
5173 {
5174 complaint (&symfile_complaints,
3e43a32a
MS
5175 _("template parameter missing "
5176 "DW_AT_const_value"));
98bfdba5
PA
5177 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5178 continue;
5179 }
5180
5181 dwarf2_const_value_attr (attr, type, name,
5182 &cu->comp_unit_obstack, cu,
5183 &value, &bytes, &baton);
5184
5185 if (TYPE_NOSIGN (type))
5186 /* GDB prints characters as NUMBER 'CHAR'. If that's
5187 changed, this can use value_print instead. */
5188 c_printchar (value, type, buf);
5189 else
5190 {
5191 struct value_print_options opts;
5192
5193 if (baton != NULL)
5194 v = dwarf2_evaluate_loc_desc (type, NULL,
5195 baton->data,
5196 baton->size,
5197 baton->per_cu);
5198 else if (bytes != NULL)
5199 {
5200 v = allocate_value (type);
5201 memcpy (value_contents_writeable (v), bytes,
5202 TYPE_LENGTH (type));
5203 }
5204 else
5205 v = value_from_longest (type, value);
5206
3e43a32a
MS
5207 /* Specify decimal so that we do not depend on
5208 the radix. */
98bfdba5
PA
5209 get_formatted_print_options (&opts, 'd');
5210 opts.raw = 1;
5211 value_print (v, buf, &opts);
5212 release_value (v);
5213 value_free (v);
5214 }
5215 }
5216
5217 die->building_fullname = 0;
5218
5219 if (!first)
5220 {
5221 /* Close the argument list, with a space if necessary
5222 (nested templates). */
5223 char last_char = '\0';
5224 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5225 if (last_char == '>')
5226 fputs_unfiltered (" >", buf);
5227 else
5228 fputs_unfiltered (">", buf);
5229 }
5230 }
5231
94af9270
KS
5232 /* For Java and C++ methods, append formal parameter type
5233 information, if PHYSNAME. */
6e70227d 5234
94af9270
KS
5235 if (physname && die->tag == DW_TAG_subprogram
5236 && (cu->language == language_cplus
5237 || cu->language == language_java))
5238 {
5239 struct type *type = read_type_die (die, cu);
5240
3167638f 5241 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5242
5243 if (cu->language == language_java)
5244 {
5245 /* For java, we must append the return type to method
0963b4bd 5246 names. */
94af9270
KS
5247 if (die->tag == DW_TAG_subprogram)
5248 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5249 0, 0);
5250 }
5251 else if (cu->language == language_cplus)
5252 {
60430eff
DJ
5253 /* Assume that an artificial first parameter is
5254 "this", but do not crash if it is not. RealView
5255 marks unnamed (and thus unused) parameters as
5256 artificial; there is no way to differentiate
5257 the two cases. */
94af9270
KS
5258 if (TYPE_NFIELDS (type) > 0
5259 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5260 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5261 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5262 0))))
94af9270
KS
5263 fputs_unfiltered (" const", buf);
5264 }
5265 }
5266
5267 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5268 &length);
5269 ui_file_delete (buf);
5270
5271 if (cu->language == language_cplus)
5272 {
5273 char *cname
5274 = dwarf2_canonicalize_name (name, cu,
5275 &cu->objfile->objfile_obstack);
9a619af0 5276
94af9270
KS
5277 if (cname != NULL)
5278 name = cname;
5279 }
5280 }
5281 }
5282
5283 return name;
5284}
5285
0114d602
DJ
5286/* Return the fully qualified name of DIE, based on its DW_AT_name.
5287 If scope qualifiers are appropriate they will be added. The result
5288 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5289 not have a name. NAME may either be from a previous call to
5290 dwarf2_name or NULL.
5291
0963b4bd 5292 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5293
5294static const char *
94af9270 5295dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5296{
94af9270
KS
5297 return dwarf2_compute_name (name, die, cu, 0);
5298}
0114d602 5299
94af9270
KS
5300/* Construct a physname for the given DIE in CU. NAME may either be
5301 from a previous call to dwarf2_name or NULL. The result will be
5302 allocated on the objfile_objstack or NULL if the DIE does not have a
5303 name.
0114d602 5304
94af9270 5305 The output string will be canonicalized (if C++/Java). */
0114d602 5306
94af9270
KS
5307static const char *
5308dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5309{
900e11f9
JK
5310 struct attribute *attr;
5311 const char *retval, *mangled = NULL, *canon = NULL;
5312 struct cleanup *back_to;
5313 int need_copy = 1;
5314
5315 /* In this case dwarf2_compute_name is just a shortcut not building anything
5316 on its own. */
5317 if (!die_needs_namespace (die, cu))
5318 return dwarf2_compute_name (name, die, cu, 1);
5319
5320 back_to = make_cleanup (null_cleanup, NULL);
5321
5322 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5323 if (!attr)
5324 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5325
5326 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5327 has computed. */
5328 if (attr && DW_STRING (attr))
5329 {
5330 char *demangled;
5331
5332 mangled = DW_STRING (attr);
5333
5334 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5335 type. It is easier for GDB users to search for such functions as
5336 `name(params)' than `long name(params)'. In such case the minimal
5337 symbol names do not match the full symbol names but for template
5338 functions there is never a need to look up their definition from their
5339 declaration so the only disadvantage remains the minimal symbol
5340 variant `long name(params)' does not have the proper inferior type.
5341 */
5342
5343 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5344 | (cu->language == language_java
5345 ? DMGL_JAVA | DMGL_RET_POSTFIX
5346 : DMGL_RET_DROP)));
5347 if (demangled)
5348 {
5349 make_cleanup (xfree, demangled);
5350 canon = demangled;
5351 }
5352 else
5353 {
5354 canon = mangled;
5355 need_copy = 0;
5356 }
5357 }
5358
5359 if (canon == NULL || check_physname)
5360 {
5361 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5362
5363 if (canon != NULL && strcmp (physname, canon) != 0)
5364 {
5365 /* It may not mean a bug in GDB. The compiler could also
5366 compute DW_AT_linkage_name incorrectly. But in such case
5367 GDB would need to be bug-to-bug compatible. */
5368
5369 complaint (&symfile_complaints,
5370 _("Computed physname <%s> does not match demangled <%s> "
5371 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5372 physname, canon, mangled, die->offset, cu->objfile->name);
5373
5374 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5375 is available here - over computed PHYSNAME. It is safer
5376 against both buggy GDB and buggy compilers. */
5377
5378 retval = canon;
5379 }
5380 else
5381 {
5382 retval = physname;
5383 need_copy = 0;
5384 }
5385 }
5386 else
5387 retval = canon;
5388
5389 if (need_copy)
5390 retval = obsavestring (retval, strlen (retval),
5391 &cu->objfile->objfile_obstack);
5392
5393 do_cleanups (back_to);
5394 return retval;
0114d602
DJ
5395}
5396
27aa8d6a
SW
5397/* Read the import statement specified by the given die and record it. */
5398
5399static void
5400read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5401{
5402 struct attribute *import_attr;
32019081 5403 struct die_info *imported_die, *child_die;
de4affc9 5404 struct dwarf2_cu *imported_cu;
27aa8d6a 5405 const char *imported_name;
794684b6 5406 const char *imported_name_prefix;
13387711
SW
5407 const char *canonical_name;
5408 const char *import_alias;
5409 const char *imported_declaration = NULL;
794684b6 5410 const char *import_prefix;
32019081
JK
5411 VEC (const_char_ptr) *excludes = NULL;
5412 struct cleanup *cleanups;
13387711
SW
5413
5414 char *temp;
27aa8d6a
SW
5415
5416 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5417 if (import_attr == NULL)
5418 {
5419 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5420 dwarf_tag_name (die->tag));
5421 return;
5422 }
5423
de4affc9
CC
5424 imported_cu = cu;
5425 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5426 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5427 if (imported_name == NULL)
5428 {
5429 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5430
5431 The import in the following code:
5432 namespace A
5433 {
5434 typedef int B;
5435 }
5436
5437 int main ()
5438 {
5439 using A::B;
5440 B b;
5441 return b;
5442 }
5443
5444 ...
5445 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5446 <52> DW_AT_decl_file : 1
5447 <53> DW_AT_decl_line : 6
5448 <54> DW_AT_import : <0x75>
5449 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5450 <59> DW_AT_name : B
5451 <5b> DW_AT_decl_file : 1
5452 <5c> DW_AT_decl_line : 2
5453 <5d> DW_AT_type : <0x6e>
5454 ...
5455 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5456 <76> DW_AT_byte_size : 4
5457 <77> DW_AT_encoding : 5 (signed)
5458
5459 imports the wrong die ( 0x75 instead of 0x58 ).
5460 This case will be ignored until the gcc bug is fixed. */
5461 return;
5462 }
5463
82856980
SW
5464 /* Figure out the local name after import. */
5465 import_alias = dwarf2_name (die, cu);
27aa8d6a 5466
794684b6
SW
5467 /* Figure out where the statement is being imported to. */
5468 import_prefix = determine_prefix (die, cu);
5469
5470 /* Figure out what the scope of the imported die is and prepend it
5471 to the name of the imported die. */
de4affc9 5472 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5473
f55ee35c
JK
5474 if (imported_die->tag != DW_TAG_namespace
5475 && imported_die->tag != DW_TAG_module)
794684b6 5476 {
13387711
SW
5477 imported_declaration = imported_name;
5478 canonical_name = imported_name_prefix;
794684b6 5479 }
13387711 5480 else if (strlen (imported_name_prefix) > 0)
794684b6 5481 {
13387711
SW
5482 temp = alloca (strlen (imported_name_prefix)
5483 + 2 + strlen (imported_name) + 1);
5484 strcpy (temp, imported_name_prefix);
5485 strcat (temp, "::");
5486 strcat (temp, imported_name);
5487 canonical_name = temp;
794684b6 5488 }
13387711
SW
5489 else
5490 canonical_name = imported_name;
794684b6 5491
32019081
JK
5492 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5493
5494 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5495 for (child_die = die->child; child_die && child_die->tag;
5496 child_die = sibling_die (child_die))
5497 {
5498 /* DWARF-4: A Fortran use statement with a “rename list” may be
5499 represented by an imported module entry with an import attribute
5500 referring to the module and owned entries corresponding to those
5501 entities that are renamed as part of being imported. */
5502
5503 if (child_die->tag != DW_TAG_imported_declaration)
5504 {
5505 complaint (&symfile_complaints,
5506 _("child DW_TAG_imported_declaration expected "
5507 "- DIE at 0x%x [in module %s]"),
5508 child_die->offset, cu->objfile->name);
5509 continue;
5510 }
5511
5512 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5513 if (import_attr == NULL)
5514 {
5515 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5516 dwarf_tag_name (child_die->tag));
5517 continue;
5518 }
5519
5520 imported_cu = cu;
5521 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5522 &imported_cu);
5523 imported_name = dwarf2_name (imported_die, imported_cu);
5524 if (imported_name == NULL)
5525 {
5526 complaint (&symfile_complaints,
5527 _("child DW_TAG_imported_declaration has unknown "
5528 "imported name - DIE at 0x%x [in module %s]"),
5529 child_die->offset, cu->objfile->name);
5530 continue;
5531 }
5532
5533 VEC_safe_push (const_char_ptr, excludes, imported_name);
5534
5535 process_die (child_die, cu);
5536 }
5537
c0cc3a76
SW
5538 cp_add_using_directive (import_prefix,
5539 canonical_name,
5540 import_alias,
13387711 5541 imported_declaration,
32019081 5542 excludes,
c0cc3a76 5543 &cu->objfile->objfile_obstack);
32019081
JK
5544
5545 do_cleanups (cleanups);
27aa8d6a
SW
5546}
5547
5fb290d7 5548static void
e142c38c 5549initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5550{
e142c38c 5551 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5552}
5553
ae2de4f8
DE
5554/* Cleanup function for read_file_scope. */
5555
cb1df416
DJ
5556static void
5557free_cu_line_header (void *arg)
5558{
5559 struct dwarf2_cu *cu = arg;
5560
5561 free_line_header (cu->line_header);
5562 cu->line_header = NULL;
5563}
5564
9291a0cd
TT
5565static void
5566find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5567 char **name, char **comp_dir)
5568{
5569 struct attribute *attr;
5570
5571 *name = NULL;
5572 *comp_dir = NULL;
5573
5574 /* Find the filename. Do not use dwarf2_name here, since the filename
5575 is not a source language identifier. */
5576 attr = dwarf2_attr (die, DW_AT_name, cu);
5577 if (attr)
5578 {
5579 *name = DW_STRING (attr);
5580 }
5581
5582 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5583 if (attr)
5584 *comp_dir = DW_STRING (attr);
5585 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5586 {
5587 *comp_dir = ldirname (*name);
5588 if (*comp_dir != NULL)
5589 make_cleanup (xfree, *comp_dir);
5590 }
5591 if (*comp_dir != NULL)
5592 {
5593 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5594 directory, get rid of it. */
5595 char *cp = strchr (*comp_dir, ':');
5596
5597 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5598 *comp_dir = cp + 1;
5599 }
5600
5601 if (*name == NULL)
5602 *name = "<unknown>";
5603}
5604
2ab95328
TT
5605/* Handle DW_AT_stmt_list for a compilation unit. */
5606
5607static void
5608handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5609 const char *comp_dir)
5610{
5611 struct attribute *attr;
5612 struct objfile *objfile = cu->objfile;
5613 bfd *abfd = objfile->obfd;
5614
5615 /* Decode line number information if present. We do this before
5616 processing child DIEs, so that the line header table is available
5617 for DW_AT_decl_file. */
5618 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5619 if (attr)
5620 {
5621 unsigned int line_offset = DW_UNSND (attr);
5622 struct line_header *line_header
5623 = dwarf_decode_line_header (line_offset, abfd, cu);
5624
5625 if (line_header)
5626 {
5627 cu->line_header = line_header;
5628 make_cleanup (free_cu_line_header, cu);
5629 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5630 }
5631 }
5632}
5633
ae2de4f8
DE
5634/* Process DW_TAG_compile_unit. */
5635
c906108c 5636static void
e7c27a73 5637read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5638{
e7c27a73 5639 struct objfile *objfile = cu->objfile;
debd256d 5640 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5641 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5642 CORE_ADDR highpc = ((CORE_ADDR) 0);
5643 struct attribute *attr;
e1024ff1 5644 char *name = NULL;
c906108c
SS
5645 char *comp_dir = NULL;
5646 struct die_info *child_die;
5647 bfd *abfd = objfile->obfd;
e142c38c 5648 CORE_ADDR baseaddr;
6e70227d 5649
e142c38c 5650 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5651
fae299cd 5652 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5653
5654 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5655 from finish_block. */
2acceee2 5656 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5657 lowpc = highpc;
5658 lowpc += baseaddr;
5659 highpc += baseaddr;
5660
9291a0cd 5661 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5662
e142c38c 5663 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5664 if (attr)
5665 {
e142c38c 5666 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5667 }
5668
b0f35d58 5669 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5670 if (attr)
b0f35d58 5671 cu->producer = DW_STRING (attr);
303b6f5d 5672
f4b8a18d
KW
5673 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5674 standardised yet. As a workaround for the language detection we fall
5675 back to the DW_AT_producer string. */
5676 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5677 cu->language = language_opencl;
5678
0963b4bd 5679 /* We assume that we're processing GCC output. */
c906108c 5680 processing_gcc_compilation = 2;
c906108c 5681
df8a16a1
DJ
5682 processing_has_namespace_info = 0;
5683
c906108c
SS
5684 start_symtab (name, comp_dir, lowpc);
5685 record_debugformat ("DWARF 2");
303b6f5d 5686 record_producer (cu->producer);
c906108c 5687
e142c38c 5688 initialize_cu_func_list (cu);
c906108c 5689
2ab95328 5690 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5691
cb1df416
DJ
5692 /* Process all dies in compilation unit. */
5693 if (die->child != NULL)
5694 {
5695 child_die = die->child;
5696 while (child_die && child_die->tag)
5697 {
5698 process_die (child_die, cu);
5699 child_die = sibling_die (child_die);
5700 }
5701 }
5702
2e276125
JB
5703 /* Decode macro information, if present. Dwarf 2 macro information
5704 refers to information in the line number info statement program
5705 header, so we can only read it if we've read the header
5706 successfully. */
cf2c3c16 5707 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5708 if (attr && cu->line_header)
2e276125 5709 {
cf2c3c16
TT
5710 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5711 complaint (&symfile_complaints,
5712 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5713
5714 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5715 comp_dir, abfd, cu,
5716 &dwarf2_per_objfile->macro, 1);
5717 }
5718 else
5719 {
5720 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5721 if (attr && cu->line_header)
5722 {
5723 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5724
cf2c3c16
TT
5725 dwarf_decode_macros (cu->line_header, macro_offset,
5726 comp_dir, abfd, cu,
5727 &dwarf2_per_objfile->macinfo, 0);
5728 }
2e276125 5729 }
debd256d 5730 do_cleanups (back_to);
5fb290d7
DJ
5731}
5732
ae2de4f8
DE
5733/* Process DW_TAG_type_unit.
5734 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5735 actual type being defined by this TU. In this case the first top
5736 level sibling is there to provide context only. */
5737
5738static void
5739read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5740{
5741 struct objfile *objfile = cu->objfile;
5742 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5743 CORE_ADDR lowpc;
5744 struct attribute *attr;
5745 char *name = NULL;
5746 char *comp_dir = NULL;
5747 struct die_info *child_die;
5748 bfd *abfd = objfile->obfd;
348e048f
DE
5749
5750 /* start_symtab needs a low pc, but we don't really have one.
5751 Do what read_file_scope would do in the absence of such info. */
5752 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5753
5754 /* Find the filename. Do not use dwarf2_name here, since the filename
5755 is not a source language identifier. */
5756 attr = dwarf2_attr (die, DW_AT_name, cu);
5757 if (attr)
5758 name = DW_STRING (attr);
5759
5760 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5761 if (attr)
5762 comp_dir = DW_STRING (attr);
5763 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5764 {
5765 comp_dir = ldirname (name);
5766 if (comp_dir != NULL)
5767 make_cleanup (xfree, comp_dir);
5768 }
5769
5770 if (name == NULL)
5771 name = "<unknown>";
5772
5773 attr = dwarf2_attr (die, DW_AT_language, cu);
5774 if (attr)
5775 set_cu_language (DW_UNSND (attr), cu);
5776
5777 /* This isn't technically needed today. It is done for symmetry
5778 with read_file_scope. */
5779 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5780 if (attr)
348e048f
DE
5781 cu->producer = DW_STRING (attr);
5782
0963b4bd 5783 /* We assume that we're processing GCC output. */
348e048f
DE
5784 processing_gcc_compilation = 2;
5785
5786 processing_has_namespace_info = 0;
5787
5788 start_symtab (name, comp_dir, lowpc);
5789 record_debugformat ("DWARF 2");
5790 record_producer (cu->producer);
5791
2ab95328
TT
5792 handle_DW_AT_stmt_list (die, cu, comp_dir);
5793
348e048f
DE
5794 /* Process the dies in the type unit. */
5795 if (die->child == NULL)
5796 {
5797 dump_die_for_error (die);
5798 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5799 bfd_get_filename (abfd));
5800 }
5801
5802 child_die = die->child;
5803
5804 while (child_die && child_die->tag)
5805 {
5806 process_die (child_die, cu);
5807
5808 child_die = sibling_die (child_die);
5809 }
5810
5811 do_cleanups (back_to);
5812}
5813
5fb290d7 5814static void
e142c38c
DJ
5815add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5816 struct dwarf2_cu *cu)
5fb290d7
DJ
5817{
5818 struct function_range *thisfn;
5819
5820 thisfn = (struct function_range *)
7b5a2f43 5821 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5822 thisfn->name = name;
5823 thisfn->lowpc = lowpc;
5824 thisfn->highpc = highpc;
5825 thisfn->seen_line = 0;
5826 thisfn->next = NULL;
5827
e142c38c
DJ
5828 if (cu->last_fn == NULL)
5829 cu->first_fn = thisfn;
5fb290d7 5830 else
e142c38c 5831 cu->last_fn->next = thisfn;
5fb290d7 5832
e142c38c 5833 cu->last_fn = thisfn;
c906108c
SS
5834}
5835
d389af10
JK
5836/* qsort helper for inherit_abstract_dies. */
5837
5838static int
5839unsigned_int_compar (const void *ap, const void *bp)
5840{
5841 unsigned int a = *(unsigned int *) ap;
5842 unsigned int b = *(unsigned int *) bp;
5843
5844 return (a > b) - (b > a);
5845}
5846
5847/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5848 Inherit only the children of the DW_AT_abstract_origin DIE not being
5849 already referenced by DW_AT_abstract_origin from the children of the
5850 current DIE. */
d389af10
JK
5851
5852static void
5853inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5854{
5855 struct die_info *child_die;
5856 unsigned die_children_count;
5857 /* CU offsets which were referenced by children of the current DIE. */
5858 unsigned *offsets;
5859 unsigned *offsets_end, *offsetp;
5860 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5861 struct die_info *origin_die;
5862 /* Iterator of the ORIGIN_DIE children. */
5863 struct die_info *origin_child_die;
5864 struct cleanup *cleanups;
5865 struct attribute *attr;
cd02d79d
PA
5866 struct dwarf2_cu *origin_cu;
5867 struct pending **origin_previous_list_in_scope;
d389af10
JK
5868
5869 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5870 if (!attr)
5871 return;
5872
cd02d79d
PA
5873 /* Note that following die references may follow to a die in a
5874 different cu. */
5875
5876 origin_cu = cu;
5877 origin_die = follow_die_ref (die, attr, &origin_cu);
5878
5879 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5880 symbols in. */
5881 origin_previous_list_in_scope = origin_cu->list_in_scope;
5882 origin_cu->list_in_scope = cu->list_in_scope;
5883
edb3359d
DJ
5884 if (die->tag != origin_die->tag
5885 && !(die->tag == DW_TAG_inlined_subroutine
5886 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5887 complaint (&symfile_complaints,
5888 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5889 die->offset, origin_die->offset);
5890
5891 child_die = die->child;
5892 die_children_count = 0;
5893 while (child_die && child_die->tag)
5894 {
5895 child_die = sibling_die (child_die);
5896 die_children_count++;
5897 }
5898 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5899 cleanups = make_cleanup (xfree, offsets);
5900
5901 offsets_end = offsets;
5902 child_die = die->child;
5903 while (child_die && child_die->tag)
5904 {
c38f313d
DJ
5905 /* For each CHILD_DIE, find the corresponding child of
5906 ORIGIN_DIE. If there is more than one layer of
5907 DW_AT_abstract_origin, follow them all; there shouldn't be,
5908 but GCC versions at least through 4.4 generate this (GCC PR
5909 40573). */
5910 struct die_info *child_origin_die = child_die;
cd02d79d 5911 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5912
c38f313d
DJ
5913 while (1)
5914 {
cd02d79d
PA
5915 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5916 child_origin_cu);
c38f313d
DJ
5917 if (attr == NULL)
5918 break;
cd02d79d
PA
5919 child_origin_die = follow_die_ref (child_origin_die, attr,
5920 &child_origin_cu);
c38f313d
DJ
5921 }
5922
d389af10
JK
5923 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5924 counterpart may exist. */
c38f313d 5925 if (child_origin_die != child_die)
d389af10 5926 {
edb3359d
DJ
5927 if (child_die->tag != child_origin_die->tag
5928 && !(child_die->tag == DW_TAG_inlined_subroutine
5929 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5930 complaint (&symfile_complaints,
5931 _("Child DIE 0x%x and its abstract origin 0x%x have "
5932 "different tags"), child_die->offset,
5933 child_origin_die->offset);
c38f313d
DJ
5934 if (child_origin_die->parent != origin_die)
5935 complaint (&symfile_complaints,
5936 _("Child DIE 0x%x and its abstract origin 0x%x have "
5937 "different parents"), child_die->offset,
5938 child_origin_die->offset);
5939 else
5940 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5941 }
5942 child_die = sibling_die (child_die);
5943 }
5944 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5945 unsigned_int_compar);
5946 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5947 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5948 complaint (&symfile_complaints,
5949 _("Multiple children of DIE 0x%x refer "
5950 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5951 die->offset, *offsetp);
5952
5953 offsetp = offsets;
5954 origin_child_die = origin_die->child;
5955 while (origin_child_die && origin_child_die->tag)
5956 {
5957 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5958 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5959 offsetp++;
5960 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5961 {
5962 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5963 process_die (origin_child_die, origin_cu);
d389af10
JK
5964 }
5965 origin_child_die = sibling_die (origin_child_die);
5966 }
cd02d79d 5967 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5968
5969 do_cleanups (cleanups);
5970}
5971
c906108c 5972static void
e7c27a73 5973read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5974{
e7c27a73 5975 struct objfile *objfile = cu->objfile;
52f0bd74 5976 struct context_stack *new;
c906108c
SS
5977 CORE_ADDR lowpc;
5978 CORE_ADDR highpc;
5979 struct die_info *child_die;
edb3359d 5980 struct attribute *attr, *call_line, *call_file;
c906108c 5981 char *name;
e142c38c 5982 CORE_ADDR baseaddr;
801e3a5b 5983 struct block *block;
edb3359d 5984 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5985 VEC (symbolp) *template_args = NULL;
5986 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5987
5988 if (inlined_func)
5989 {
5990 /* If we do not have call site information, we can't show the
5991 caller of this inlined function. That's too confusing, so
5992 only use the scope for local variables. */
5993 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5994 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5995 if (call_line == NULL || call_file == NULL)
5996 {
5997 read_lexical_block_scope (die, cu);
5998 return;
5999 }
6000 }
c906108c 6001
e142c38c
DJ
6002 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6003
94af9270 6004 name = dwarf2_name (die, cu);
c906108c 6005
e8d05480
JB
6006 /* Ignore functions with missing or empty names. These are actually
6007 illegal according to the DWARF standard. */
6008 if (name == NULL)
6009 {
6010 complaint (&symfile_complaints,
6011 _("missing name for subprogram DIE at %d"), die->offset);
6012 return;
6013 }
6014
6015 /* Ignore functions with missing or invalid low and high pc attributes. */
6016 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6017 {
ae4d0c03
PM
6018 attr = dwarf2_attr (die, DW_AT_external, cu);
6019 if (!attr || !DW_UNSND (attr))
6020 complaint (&symfile_complaints,
3e43a32a
MS
6021 _("cannot get low and high bounds "
6022 "for subprogram DIE at %d"),
ae4d0c03 6023 die->offset);
e8d05480
JB
6024 return;
6025 }
c906108c
SS
6026
6027 lowpc += baseaddr;
6028 highpc += baseaddr;
6029
5fb290d7 6030 /* Record the function range for dwarf_decode_lines. */
e142c38c 6031 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 6032
34eaf542
TT
6033 /* If we have any template arguments, then we must allocate a
6034 different sort of symbol. */
6035 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6036 {
6037 if (child_die->tag == DW_TAG_template_type_param
6038 || child_die->tag == DW_TAG_template_value_param)
6039 {
6040 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6041 struct template_symbol);
6042 templ_func->base.is_cplus_template_function = 1;
6043 break;
6044 }
6045 }
6046
c906108c 6047 new = push_context (0, lowpc);
34eaf542
TT
6048 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6049 (struct symbol *) templ_func);
4c2df51b 6050
4cecd739
DJ
6051 /* If there is a location expression for DW_AT_frame_base, record
6052 it. */
e142c38c 6053 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6054 if (attr)
c034e007
AC
6055 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6056 expression is being recorded directly in the function's symbol
6057 and not in a separate frame-base object. I guess this hack is
6058 to avoid adding some sort of frame-base adjunct/annex to the
6059 function's symbol :-(. The problem with doing this is that it
6060 results in a function symbol with a location expression that
6061 has nothing to do with the location of the function, ouch! The
6062 relationship should be: a function's symbol has-a frame base; a
6063 frame-base has-a location expression. */
e7c27a73 6064 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6065
e142c38c 6066 cu->list_in_scope = &local_symbols;
c906108c 6067
639d11d3 6068 if (die->child != NULL)
c906108c 6069 {
639d11d3 6070 child_die = die->child;
c906108c
SS
6071 while (child_die && child_die->tag)
6072 {
34eaf542
TT
6073 if (child_die->tag == DW_TAG_template_type_param
6074 || child_die->tag == DW_TAG_template_value_param)
6075 {
6076 struct symbol *arg = new_symbol (child_die, NULL, cu);
6077
f1078f66
DJ
6078 if (arg != NULL)
6079 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6080 }
6081 else
6082 process_die (child_die, cu);
c906108c
SS
6083 child_die = sibling_die (child_die);
6084 }
6085 }
6086
d389af10
JK
6087 inherit_abstract_dies (die, cu);
6088
4a811a97
UW
6089 /* If we have a DW_AT_specification, we might need to import using
6090 directives from the context of the specification DIE. See the
6091 comment in determine_prefix. */
6092 if (cu->language == language_cplus
6093 && dwarf2_attr (die, DW_AT_specification, cu))
6094 {
6095 struct dwarf2_cu *spec_cu = cu;
6096 struct die_info *spec_die = die_specification (die, &spec_cu);
6097
6098 while (spec_die)
6099 {
6100 child_die = spec_die->child;
6101 while (child_die && child_die->tag)
6102 {
6103 if (child_die->tag == DW_TAG_imported_module)
6104 process_die (child_die, spec_cu);
6105 child_die = sibling_die (child_die);
6106 }
6107
6108 /* In some cases, GCC generates specification DIEs that
6109 themselves contain DW_AT_specification attributes. */
6110 spec_die = die_specification (spec_die, &spec_cu);
6111 }
6112 }
6113
c906108c
SS
6114 new = pop_context ();
6115 /* Make a block for the local symbols within. */
801e3a5b
JB
6116 block = finish_block (new->name, &local_symbols, new->old_blocks,
6117 lowpc, highpc, objfile);
6118
df8a16a1 6119 /* For C++, set the block's scope. */
f55ee35c 6120 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6121 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6122 determine_prefix (die, cu),
df8a16a1
DJ
6123 processing_has_namespace_info);
6124
801e3a5b
JB
6125 /* If we have address ranges, record them. */
6126 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6127
34eaf542
TT
6128 /* Attach template arguments to function. */
6129 if (! VEC_empty (symbolp, template_args))
6130 {
6131 gdb_assert (templ_func != NULL);
6132
6133 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6134 templ_func->template_arguments
6135 = obstack_alloc (&objfile->objfile_obstack,
6136 (templ_func->n_template_arguments
6137 * sizeof (struct symbol *)));
6138 memcpy (templ_func->template_arguments,
6139 VEC_address (symbolp, template_args),
6140 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6141 VEC_free (symbolp, template_args);
6142 }
6143
208d8187
JB
6144 /* In C++, we can have functions nested inside functions (e.g., when
6145 a function declares a class that has methods). This means that
6146 when we finish processing a function scope, we may need to go
6147 back to building a containing block's symbol lists. */
6148 local_symbols = new->locals;
6149 param_symbols = new->params;
27aa8d6a 6150 using_directives = new->using_directives;
208d8187 6151
921e78cf
JB
6152 /* If we've finished processing a top-level function, subsequent
6153 symbols go in the file symbol list. */
6154 if (outermost_context_p ())
e142c38c 6155 cu->list_in_scope = &file_symbols;
c906108c
SS
6156}
6157
6158/* Process all the DIES contained within a lexical block scope. Start
6159 a new scope, process the dies, and then close the scope. */
6160
6161static void
e7c27a73 6162read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6163{
e7c27a73 6164 struct objfile *objfile = cu->objfile;
52f0bd74 6165 struct context_stack *new;
c906108c
SS
6166 CORE_ADDR lowpc, highpc;
6167 struct die_info *child_die;
e142c38c
DJ
6168 CORE_ADDR baseaddr;
6169
6170 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6171
6172 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6173 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6174 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6175 be nasty. Might be easier to properly extend generic blocks to
af34e669 6176 describe ranges. */
d85a05f0 6177 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6178 return;
6179 lowpc += baseaddr;
6180 highpc += baseaddr;
6181
6182 push_context (0, lowpc);
639d11d3 6183 if (die->child != NULL)
c906108c 6184 {
639d11d3 6185 child_die = die->child;
c906108c
SS
6186 while (child_die && child_die->tag)
6187 {
e7c27a73 6188 process_die (child_die, cu);
c906108c
SS
6189 child_die = sibling_die (child_die);
6190 }
6191 }
6192 new = pop_context ();
6193
8540c487 6194 if (local_symbols != NULL || using_directives != NULL)
c906108c 6195 {
801e3a5b
JB
6196 struct block *block
6197 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6198 highpc, objfile);
6199
6200 /* Note that recording ranges after traversing children, as we
6201 do here, means that recording a parent's ranges entails
6202 walking across all its children's ranges as they appear in
6203 the address map, which is quadratic behavior.
6204
6205 It would be nicer to record the parent's ranges before
6206 traversing its children, simply overriding whatever you find
6207 there. But since we don't even decide whether to create a
6208 block until after we've traversed its children, that's hard
6209 to do. */
6210 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6211 }
6212 local_symbols = new->locals;
27aa8d6a 6213 using_directives = new->using_directives;
c906108c
SS
6214}
6215
96408a79
SA
6216/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6217
6218static void
6219read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6220{
6221 struct objfile *objfile = cu->objfile;
6222 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6223 CORE_ADDR pc, baseaddr;
6224 struct attribute *attr;
6225 struct call_site *call_site, call_site_local;
6226 void **slot;
6227 int nparams;
6228 struct die_info *child_die;
6229
6230 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6231
6232 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6233 if (!attr)
6234 {
6235 complaint (&symfile_complaints,
6236 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6237 "DIE 0x%x [in module %s]"),
6238 die->offset, cu->objfile->name);
6239 return;
6240 }
6241 pc = DW_ADDR (attr) + baseaddr;
6242
6243 if (cu->call_site_htab == NULL)
6244 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6245 NULL, &objfile->objfile_obstack,
6246 hashtab_obstack_allocate, NULL);
6247 call_site_local.pc = pc;
6248 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6249 if (*slot != NULL)
6250 {
6251 complaint (&symfile_complaints,
6252 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6253 "DIE 0x%x [in module %s]"),
6254 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6255 return;
6256 }
6257
6258 /* Count parameters at the caller. */
6259
6260 nparams = 0;
6261 for (child_die = die->child; child_die && child_die->tag;
6262 child_die = sibling_die (child_die))
6263 {
6264 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6265 {
6266 complaint (&symfile_complaints,
6267 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6268 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6269 child_die->tag, child_die->offset, cu->objfile->name);
6270 continue;
6271 }
6272
6273 nparams++;
6274 }
6275
6276 call_site = obstack_alloc (&objfile->objfile_obstack,
6277 (sizeof (*call_site)
6278 + (sizeof (*call_site->parameter)
6279 * (nparams - 1))));
6280 *slot = call_site;
6281 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6282 call_site->pc = pc;
6283
6284 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6285 {
6286 struct die_info *func_die;
6287
6288 /* Skip also over DW_TAG_inlined_subroutine. */
6289 for (func_die = die->parent;
6290 func_die && func_die->tag != DW_TAG_subprogram
6291 && func_die->tag != DW_TAG_subroutine_type;
6292 func_die = func_die->parent);
6293
6294 /* DW_AT_GNU_all_call_sites is a superset
6295 of DW_AT_GNU_all_tail_call_sites. */
6296 if (func_die
6297 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6298 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6299 {
6300 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6301 not complete. But keep CALL_SITE for look ups via call_site_htab,
6302 both the initial caller containing the real return address PC and
6303 the final callee containing the current PC of a chain of tail
6304 calls do not need to have the tail call list complete. But any
6305 function candidate for a virtual tail call frame searched via
6306 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6307 determined unambiguously. */
6308 }
6309 else
6310 {
6311 struct type *func_type = NULL;
6312
6313 if (func_die)
6314 func_type = get_die_type (func_die, cu);
6315 if (func_type != NULL)
6316 {
6317 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6318
6319 /* Enlist this call site to the function. */
6320 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6321 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6322 }
6323 else
6324 complaint (&symfile_complaints,
6325 _("Cannot find function owning DW_TAG_GNU_call_site "
6326 "DIE 0x%x [in module %s]"),
6327 die->offset, cu->objfile->name);
6328 }
6329 }
6330
6331 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6332 if (attr == NULL)
6333 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6334 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6335 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6336 /* Keep NULL DWARF_BLOCK. */;
6337 else if (attr_form_is_block (attr))
6338 {
6339 struct dwarf2_locexpr_baton *dlbaton;
6340
6341 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6342 dlbaton->data = DW_BLOCK (attr)->data;
6343 dlbaton->size = DW_BLOCK (attr)->size;
6344 dlbaton->per_cu = cu->per_cu;
6345
6346 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6347 }
6348 else if (is_ref_attr (attr))
6349 {
6350 struct objfile *objfile = cu->objfile;
6351 struct dwarf2_cu *target_cu = cu;
6352 struct die_info *target_die;
6353
6354 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6355 gdb_assert (target_cu->objfile == objfile);
6356 if (die_is_declaration (target_die, target_cu))
6357 {
6358 const char *target_physname;
6359
6360 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6361 if (target_physname == NULL)
6362 complaint (&symfile_complaints,
6363 _("DW_AT_GNU_call_site_target target DIE has invalid "
6364 "physname, for referencing DIE 0x%x [in module %s]"),
6365 die->offset, cu->objfile->name);
6366 else
6367 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6368 }
6369 else
6370 {
6371 CORE_ADDR lowpc;
6372
6373 /* DW_AT_entry_pc should be preferred. */
6374 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6375 complaint (&symfile_complaints,
6376 _("DW_AT_GNU_call_site_target target DIE has invalid "
6377 "low pc, for referencing DIE 0x%x [in module %s]"),
6378 die->offset, cu->objfile->name);
6379 else
6380 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6381 }
6382 }
6383 else
6384 complaint (&symfile_complaints,
6385 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6386 "block nor reference, for DIE 0x%x [in module %s]"),
6387 die->offset, cu->objfile->name);
6388
6389 call_site->per_cu = cu->per_cu;
6390
6391 for (child_die = die->child;
6392 child_die && child_die->tag;
6393 child_die = sibling_die (child_die))
6394 {
6395 struct dwarf2_locexpr_baton *dlbaton;
6396 struct call_site_parameter *parameter;
6397
6398 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6399 {
6400 /* Already printed the complaint above. */
6401 continue;
6402 }
6403
6404 gdb_assert (call_site->parameter_count < nparams);
6405 parameter = &call_site->parameter[call_site->parameter_count];
6406
6407 /* DW_AT_location specifies the register number. Value of the data
6408 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6409
6410 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6411 if (!attr || !attr_form_is_block (attr))
6412 {
6413 complaint (&symfile_complaints,
6414 _("No DW_FORM_block* DW_AT_location for "
6415 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6416 child_die->offset, cu->objfile->name);
6417 continue;
6418 }
6419 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6420 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6421 if (parameter->dwarf_reg == -1
6422 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6423 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6424 &parameter->fb_offset))
6425 {
6426 complaint (&symfile_complaints,
6427 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6428 "for DW_FORM_block* DW_AT_location for "
6429 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6430 child_die->offset, cu->objfile->name);
6431 continue;
6432 }
6433
6434 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6435 if (!attr_form_is_block (attr))
6436 {
6437 complaint (&symfile_complaints,
6438 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6439 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6440 child_die->offset, cu->objfile->name);
6441 continue;
6442 }
6443 parameter->value = DW_BLOCK (attr)->data;
6444 parameter->value_size = DW_BLOCK (attr)->size;
6445
6446 /* Parameters are not pre-cleared by memset above. */
6447 parameter->data_value = NULL;
6448 parameter->data_value_size = 0;
6449 call_site->parameter_count++;
6450
6451 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6452 if (attr)
6453 {
6454 if (!attr_form_is_block (attr))
6455 complaint (&symfile_complaints,
6456 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6457 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6458 child_die->offset, cu->objfile->name);
6459 else
6460 {
6461 parameter->data_value = DW_BLOCK (attr)->data;
6462 parameter->data_value_size = DW_BLOCK (attr)->size;
6463 }
6464 }
6465 }
6466}
6467
43039443 6468/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6469 Return 1 if the attributes are present and valid, otherwise, return 0.
6470 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6471
6472static int
6473dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6474 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6475 struct partial_symtab *ranges_pst)
43039443
JK
6476{
6477 struct objfile *objfile = cu->objfile;
6478 struct comp_unit_head *cu_header = &cu->header;
6479 bfd *obfd = objfile->obfd;
6480 unsigned int addr_size = cu_header->addr_size;
6481 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6482 /* Base address selection entry. */
6483 CORE_ADDR base;
6484 int found_base;
6485 unsigned int dummy;
6486 gdb_byte *buffer;
6487 CORE_ADDR marker;
6488 int low_set;
6489 CORE_ADDR low = 0;
6490 CORE_ADDR high = 0;
ff013f42 6491 CORE_ADDR baseaddr;
43039443 6492
d00adf39
DE
6493 found_base = cu->base_known;
6494 base = cu->base_address;
43039443 6495
be391dca 6496 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6497 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6498 {
6499 complaint (&symfile_complaints,
6500 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6501 offset);
6502 return 0;
6503 }
dce234bc 6504 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6505
6506 /* Read in the largest possible address. */
6507 marker = read_address (obfd, buffer, cu, &dummy);
6508 if ((marker & mask) == mask)
6509 {
6510 /* If we found the largest possible address, then
6511 read the base address. */
6512 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6513 buffer += 2 * addr_size;
6514 offset += 2 * addr_size;
6515 found_base = 1;
6516 }
6517
6518 low_set = 0;
6519
e7030f15 6520 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6521
43039443
JK
6522 while (1)
6523 {
6524 CORE_ADDR range_beginning, range_end;
6525
6526 range_beginning = read_address (obfd, buffer, cu, &dummy);
6527 buffer += addr_size;
6528 range_end = read_address (obfd, buffer, cu, &dummy);
6529 buffer += addr_size;
6530 offset += 2 * addr_size;
6531
6532 /* An end of list marker is a pair of zero addresses. */
6533 if (range_beginning == 0 && range_end == 0)
6534 /* Found the end of list entry. */
6535 break;
6536
6537 /* Each base address selection entry is a pair of 2 values.
6538 The first is the largest possible address, the second is
6539 the base address. Check for a base address here. */
6540 if ((range_beginning & mask) == mask)
6541 {
6542 /* If we found the largest possible address, then
6543 read the base address. */
6544 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6545 found_base = 1;
6546 continue;
6547 }
6548
6549 if (!found_base)
6550 {
6551 /* We have no valid base address for the ranges
6552 data. */
6553 complaint (&symfile_complaints,
6554 _("Invalid .debug_ranges data (no base address)"));
6555 return 0;
6556 }
6557
9277c30c
UW
6558 if (range_beginning > range_end)
6559 {
6560 /* Inverted range entries are invalid. */
6561 complaint (&symfile_complaints,
6562 _("Invalid .debug_ranges data (inverted range)"));
6563 return 0;
6564 }
6565
6566 /* Empty range entries have no effect. */
6567 if (range_beginning == range_end)
6568 continue;
6569
43039443
JK
6570 range_beginning += base;
6571 range_end += base;
6572
9277c30c 6573 if (ranges_pst != NULL)
ff013f42 6574 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6575 range_beginning + baseaddr,
6576 range_end - 1 + baseaddr,
ff013f42
JK
6577 ranges_pst);
6578
43039443
JK
6579 /* FIXME: This is recording everything as a low-high
6580 segment of consecutive addresses. We should have a
6581 data structure for discontiguous block ranges
6582 instead. */
6583 if (! low_set)
6584 {
6585 low = range_beginning;
6586 high = range_end;
6587 low_set = 1;
6588 }
6589 else
6590 {
6591 if (range_beginning < low)
6592 low = range_beginning;
6593 if (range_end > high)
6594 high = range_end;
6595 }
6596 }
6597
6598 if (! low_set)
6599 /* If the first entry is an end-of-list marker, the range
6600 describes an empty scope, i.e. no instructions. */
6601 return 0;
6602
6603 if (low_return)
6604 *low_return = low;
6605 if (high_return)
6606 *high_return = high;
6607 return 1;
6608}
6609
af34e669
DJ
6610/* Get low and high pc attributes from a die. Return 1 if the attributes
6611 are present and valid, otherwise, return 0. Return -1 if the range is
6612 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6613static int
af34e669 6614dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6615 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6616 struct partial_symtab *pst)
c906108c
SS
6617{
6618 struct attribute *attr;
af34e669
DJ
6619 CORE_ADDR low = 0;
6620 CORE_ADDR high = 0;
6621 int ret = 0;
c906108c 6622
e142c38c 6623 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6624 if (attr)
af34e669
DJ
6625 {
6626 high = DW_ADDR (attr);
e142c38c 6627 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6628 if (attr)
6629 low = DW_ADDR (attr);
6630 else
6631 /* Found high w/o low attribute. */
6632 return 0;
6633
6634 /* Found consecutive range of addresses. */
6635 ret = 1;
6636 }
c906108c 6637 else
af34e669 6638 {
e142c38c 6639 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6640 if (attr != NULL)
6641 {
af34e669 6642 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6643 .debug_ranges section. */
d85a05f0 6644 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6645 return 0;
43039443 6646 /* Found discontinuous range of addresses. */
af34e669
DJ
6647 ret = -1;
6648 }
6649 }
c906108c 6650
9373cf26
JK
6651 /* read_partial_die has also the strict LOW < HIGH requirement. */
6652 if (high <= low)
c906108c
SS
6653 return 0;
6654
6655 /* When using the GNU linker, .gnu.linkonce. sections are used to
6656 eliminate duplicate copies of functions and vtables and such.
6657 The linker will arbitrarily choose one and discard the others.
6658 The AT_*_pc values for such functions refer to local labels in
6659 these sections. If the section from that file was discarded, the
6660 labels are not in the output, so the relocs get a value of 0.
6661 If this is a discarded function, mark the pc bounds as invalid,
6662 so that GDB will ignore it. */
72dca2f5 6663 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6664 return 0;
6665
6666 *lowpc = low;
96408a79
SA
6667 if (highpc)
6668 *highpc = high;
af34e669 6669 return ret;
c906108c
SS
6670}
6671
b084d499
JB
6672/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6673 its low and high PC addresses. Do nothing if these addresses could not
6674 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6675 and HIGHPC to the high address if greater than HIGHPC. */
6676
6677static void
6678dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6679 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6680 struct dwarf2_cu *cu)
6681{
6682 CORE_ADDR low, high;
6683 struct die_info *child = die->child;
6684
d85a05f0 6685 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6686 {
6687 *lowpc = min (*lowpc, low);
6688 *highpc = max (*highpc, high);
6689 }
6690
6691 /* If the language does not allow nested subprograms (either inside
6692 subprograms or lexical blocks), we're done. */
6693 if (cu->language != language_ada)
6694 return;
6e70227d 6695
b084d499
JB
6696 /* Check all the children of the given DIE. If it contains nested
6697 subprograms, then check their pc bounds. Likewise, we need to
6698 check lexical blocks as well, as they may also contain subprogram
6699 definitions. */
6700 while (child && child->tag)
6701 {
6702 if (child->tag == DW_TAG_subprogram
6703 || child->tag == DW_TAG_lexical_block)
6704 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6705 child = sibling_die (child);
6706 }
6707}
6708
fae299cd
DC
6709/* Get the low and high pc's represented by the scope DIE, and store
6710 them in *LOWPC and *HIGHPC. If the correct values can't be
6711 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6712
6713static void
6714get_scope_pc_bounds (struct die_info *die,
6715 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6716 struct dwarf2_cu *cu)
6717{
6718 CORE_ADDR best_low = (CORE_ADDR) -1;
6719 CORE_ADDR best_high = (CORE_ADDR) 0;
6720 CORE_ADDR current_low, current_high;
6721
d85a05f0 6722 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6723 {
6724 best_low = current_low;
6725 best_high = current_high;
6726 }
6727 else
6728 {
6729 struct die_info *child = die->child;
6730
6731 while (child && child->tag)
6732 {
6733 switch (child->tag) {
6734 case DW_TAG_subprogram:
b084d499 6735 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6736 break;
6737 case DW_TAG_namespace:
f55ee35c 6738 case DW_TAG_module:
fae299cd
DC
6739 /* FIXME: carlton/2004-01-16: Should we do this for
6740 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6741 that current GCC's always emit the DIEs corresponding
6742 to definitions of methods of classes as children of a
6743 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6744 the DIEs giving the declarations, which could be
6745 anywhere). But I don't see any reason why the
6746 standards says that they have to be there. */
6747 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6748
6749 if (current_low != ((CORE_ADDR) -1))
6750 {
6751 best_low = min (best_low, current_low);
6752 best_high = max (best_high, current_high);
6753 }
6754 break;
6755 default:
0963b4bd 6756 /* Ignore. */
fae299cd
DC
6757 break;
6758 }
6759
6760 child = sibling_die (child);
6761 }
6762 }
6763
6764 *lowpc = best_low;
6765 *highpc = best_high;
6766}
6767
801e3a5b
JB
6768/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6769 in DIE. */
6770static void
6771dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6772 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6773{
6774 struct attribute *attr;
6775
6776 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6777 if (attr)
6778 {
6779 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6780
801e3a5b
JB
6781 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6782 if (attr)
6783 {
6784 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6785
801e3a5b
JB
6786 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6787 }
6788 }
6789
6790 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6791 if (attr)
6792 {
6793 bfd *obfd = cu->objfile->obfd;
6794
6795 /* The value of the DW_AT_ranges attribute is the offset of the
6796 address range list in the .debug_ranges section. */
6797 unsigned long offset = DW_UNSND (attr);
dce234bc 6798 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6799
6800 /* For some target architectures, but not others, the
6801 read_address function sign-extends the addresses it returns.
6802 To recognize base address selection entries, we need a
6803 mask. */
6804 unsigned int addr_size = cu->header.addr_size;
6805 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6806
6807 /* The base address, to which the next pair is relative. Note
6808 that this 'base' is a DWARF concept: most entries in a range
6809 list are relative, to reduce the number of relocs against the
6810 debugging information. This is separate from this function's
6811 'baseaddr' argument, which GDB uses to relocate debugging
6812 information from a shared library based on the address at
6813 which the library was loaded. */
d00adf39
DE
6814 CORE_ADDR base = cu->base_address;
6815 int base_known = cu->base_known;
801e3a5b 6816
be391dca 6817 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6818 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6819 {
6820 complaint (&symfile_complaints,
6821 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6822 offset);
6823 return;
6824 }
6825
6826 for (;;)
6827 {
6828 unsigned int bytes_read;
6829 CORE_ADDR start, end;
6830
6831 start = read_address (obfd, buffer, cu, &bytes_read);
6832 buffer += bytes_read;
6833 end = read_address (obfd, buffer, cu, &bytes_read);
6834 buffer += bytes_read;
6835
6836 /* Did we find the end of the range list? */
6837 if (start == 0 && end == 0)
6838 break;
6839
6840 /* Did we find a base address selection entry? */
6841 else if ((start & base_select_mask) == base_select_mask)
6842 {
6843 base = end;
6844 base_known = 1;
6845 }
6846
6847 /* We found an ordinary address range. */
6848 else
6849 {
6850 if (!base_known)
6851 {
6852 complaint (&symfile_complaints,
3e43a32a
MS
6853 _("Invalid .debug_ranges data "
6854 "(no base address)"));
801e3a5b
JB
6855 return;
6856 }
6857
9277c30c
UW
6858 if (start > end)
6859 {
6860 /* Inverted range entries are invalid. */
6861 complaint (&symfile_complaints,
6862 _("Invalid .debug_ranges data "
6863 "(inverted range)"));
6864 return;
6865 }
6866
6867 /* Empty range entries have no effect. */
6868 if (start == end)
6869 continue;
6870
6e70227d
DE
6871 record_block_range (block,
6872 baseaddr + base + start,
801e3a5b
JB
6873 baseaddr + base + end - 1);
6874 }
6875 }
6876 }
6877}
6878
60d5a603
JK
6879/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6880 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6881 during 4.6.0 experimental. */
6882
6883static int
6884producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6885{
6886 const char *cs;
6887 int major, minor, release;
6888
6889 if (cu->producer == NULL)
6890 {
6891 /* For unknown compilers expect their behavior is DWARF version
6892 compliant.
6893
6894 GCC started to support .debug_types sections by -gdwarf-4 since
6895 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6896 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6897 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6898 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6899
6900 return 0;
6901 }
6902
6903 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6904
6905 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6906 {
6907 /* For non-GCC compilers expect their behavior is DWARF version
6908 compliant. */
6909
6910 return 0;
6911 }
6912 cs = &cu->producer[strlen ("GNU ")];
6913 while (*cs && !isdigit (*cs))
6914 cs++;
6915 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6916 {
6917 /* Not recognized as GCC. */
6918
6919 return 0;
6920 }
6921
6922 return major < 4 || (major == 4 && minor < 6);
6923}
6924
6925/* Return the default accessibility type if it is not overriden by
6926 DW_AT_accessibility. */
6927
6928static enum dwarf_access_attribute
6929dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6930{
6931 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6932 {
6933 /* The default DWARF 2 accessibility for members is public, the default
6934 accessibility for inheritance is private. */
6935
6936 if (die->tag != DW_TAG_inheritance)
6937 return DW_ACCESS_public;
6938 else
6939 return DW_ACCESS_private;
6940 }
6941 else
6942 {
6943 /* DWARF 3+ defines the default accessibility a different way. The same
6944 rules apply now for DW_TAG_inheritance as for the members and it only
6945 depends on the container kind. */
6946
6947 if (die->parent->tag == DW_TAG_class_type)
6948 return DW_ACCESS_private;
6949 else
6950 return DW_ACCESS_public;
6951 }
6952}
6953
74ac6d43
TT
6954/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6955 offset. If the attribute was not found return 0, otherwise return
6956 1. If it was found but could not properly be handled, set *OFFSET
6957 to 0. */
6958
6959static int
6960handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6961 LONGEST *offset)
6962{
6963 struct attribute *attr;
6964
6965 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6966 if (attr != NULL)
6967 {
6968 *offset = 0;
6969
6970 /* Note that we do not check for a section offset first here.
6971 This is because DW_AT_data_member_location is new in DWARF 4,
6972 so if we see it, we can assume that a constant form is really
6973 a constant and not a section offset. */
6974 if (attr_form_is_constant (attr))
6975 *offset = dwarf2_get_attr_constant_value (attr, 0);
6976 else if (attr_form_is_section_offset (attr))
6977 dwarf2_complex_location_expr_complaint ();
6978 else if (attr_form_is_block (attr))
6979 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6980 else
6981 dwarf2_complex_location_expr_complaint ();
6982
6983 return 1;
6984 }
6985
6986 return 0;
6987}
6988
c906108c
SS
6989/* Add an aggregate field to the field list. */
6990
6991static void
107d2387 6992dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6993 struct dwarf2_cu *cu)
6e70227d 6994{
e7c27a73 6995 struct objfile *objfile = cu->objfile;
5e2b427d 6996 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6997 struct nextfield *new_field;
6998 struct attribute *attr;
6999 struct field *fp;
7000 char *fieldname = "";
7001
7002 /* Allocate a new field list entry and link it in. */
7003 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 7004 make_cleanup (xfree, new_field);
c906108c 7005 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7006
7007 if (die->tag == DW_TAG_inheritance)
7008 {
7009 new_field->next = fip->baseclasses;
7010 fip->baseclasses = new_field;
7011 }
7012 else
7013 {
7014 new_field->next = fip->fields;
7015 fip->fields = new_field;
7016 }
c906108c
SS
7017 fip->nfields++;
7018
e142c38c 7019 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7020 if (attr)
7021 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7022 else
7023 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7024 if (new_field->accessibility != DW_ACCESS_public)
7025 fip->non_public_fields = 1;
60d5a603 7026
e142c38c 7027 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7028 if (attr)
7029 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7030 else
7031 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7032
7033 fp = &new_field->field;
a9a9bd0f 7034
e142c38c 7035 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7036 {
74ac6d43
TT
7037 LONGEST offset;
7038
a9a9bd0f 7039 /* Data member other than a C++ static data member. */
6e70227d 7040
c906108c 7041 /* Get type of field. */
e7c27a73 7042 fp->type = die_type (die, cu);
c906108c 7043
d6a843b5 7044 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7045
c906108c 7046 /* Get bit size of field (zero if none). */
e142c38c 7047 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7048 if (attr)
7049 {
7050 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7051 }
7052 else
7053 {
7054 FIELD_BITSIZE (*fp) = 0;
7055 }
7056
7057 /* Get bit offset of field. */
74ac6d43
TT
7058 if (handle_data_member_location (die, cu, &offset))
7059 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7060 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7061 if (attr)
7062 {
5e2b427d 7063 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7064 {
7065 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7066 additional bit offset from the MSB of the containing
7067 anonymous object to the MSB of the field. We don't
7068 have to do anything special since we don't need to
7069 know the size of the anonymous object. */
c906108c
SS
7070 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7071 }
7072 else
7073 {
7074 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7075 MSB of the anonymous object, subtract off the number of
7076 bits from the MSB of the field to the MSB of the
7077 object, and then subtract off the number of bits of
7078 the field itself. The result is the bit offset of
7079 the LSB of the field. */
c906108c
SS
7080 int anonymous_size;
7081 int bit_offset = DW_UNSND (attr);
7082
e142c38c 7083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7084 if (attr)
7085 {
7086 /* The size of the anonymous object containing
7087 the bit field is explicit, so use the
7088 indicated size (in bytes). */
7089 anonymous_size = DW_UNSND (attr);
7090 }
7091 else
7092 {
7093 /* The size of the anonymous object containing
7094 the bit field must be inferred from the type
7095 attribute of the data member containing the
7096 bit field. */
7097 anonymous_size = TYPE_LENGTH (fp->type);
7098 }
7099 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7100 - bit_offset - FIELD_BITSIZE (*fp);
7101 }
7102 }
7103
7104 /* Get name of field. */
39cbfefa
DJ
7105 fieldname = dwarf2_name (die, cu);
7106 if (fieldname == NULL)
7107 fieldname = "";
d8151005
DJ
7108
7109 /* The name is already allocated along with this objfile, so we don't
7110 need to duplicate it for the type. */
7111 fp->name = fieldname;
c906108c
SS
7112
7113 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7114 pointer or virtual base class pointer) to private. */
e142c38c 7115 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7116 {
d48cc9dd 7117 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7118 new_field->accessibility = DW_ACCESS_private;
7119 fip->non_public_fields = 1;
7120 }
7121 }
a9a9bd0f 7122 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7123 {
a9a9bd0f
DC
7124 /* C++ static member. */
7125
7126 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7127 is a declaration, but all versions of G++ as of this writing
7128 (so through at least 3.2.1) incorrectly generate
7129 DW_TAG_variable tags. */
6e70227d 7130
ff355380 7131 const char *physname;
c906108c 7132
a9a9bd0f 7133 /* Get name of field. */
39cbfefa
DJ
7134 fieldname = dwarf2_name (die, cu);
7135 if (fieldname == NULL)
c906108c
SS
7136 return;
7137
254e6b9e 7138 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7139 if (attr
7140 /* Only create a symbol if this is an external value.
7141 new_symbol checks this and puts the value in the global symbol
7142 table, which we want. If it is not external, new_symbol
7143 will try to put the value in cu->list_in_scope which is wrong. */
7144 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7145 {
7146 /* A static const member, not much different than an enum as far as
7147 we're concerned, except that we can support more types. */
7148 new_symbol (die, NULL, cu);
7149 }
7150
2df3850c 7151 /* Get physical name. */
ff355380 7152 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7153
d8151005
DJ
7154 /* The name is already allocated along with this objfile, so we don't
7155 need to duplicate it for the type. */
7156 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7157 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7158 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7159 }
7160 else if (die->tag == DW_TAG_inheritance)
7161 {
74ac6d43 7162 LONGEST offset;
d4b96c9a 7163
74ac6d43
TT
7164 /* C++ base class field. */
7165 if (handle_data_member_location (die, cu, &offset))
7166 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7167 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7168 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7169 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7170 fip->nbaseclasses++;
7171 }
7172}
7173
98751a41
JK
7174/* Add a typedef defined in the scope of the FIP's class. */
7175
7176static void
7177dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7178 struct dwarf2_cu *cu)
6e70227d 7179{
98751a41 7180 struct objfile *objfile = cu->objfile;
98751a41
JK
7181 struct typedef_field_list *new_field;
7182 struct attribute *attr;
7183 struct typedef_field *fp;
7184 char *fieldname = "";
7185
7186 /* Allocate a new field list entry and link it in. */
7187 new_field = xzalloc (sizeof (*new_field));
7188 make_cleanup (xfree, new_field);
7189
7190 gdb_assert (die->tag == DW_TAG_typedef);
7191
7192 fp = &new_field->field;
7193
7194 /* Get name of field. */
7195 fp->name = dwarf2_name (die, cu);
7196 if (fp->name == NULL)
7197 return;
7198
7199 fp->type = read_type_die (die, cu);
7200
7201 new_field->next = fip->typedef_field_list;
7202 fip->typedef_field_list = new_field;
7203 fip->typedef_field_list_count++;
7204}
7205
c906108c
SS
7206/* Create the vector of fields, and attach it to the type. */
7207
7208static void
fba45db2 7209dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7210 struct dwarf2_cu *cu)
c906108c
SS
7211{
7212 int nfields = fip->nfields;
7213
7214 /* Record the field count, allocate space for the array of fields,
7215 and create blank accessibility bitfields if necessary. */
7216 TYPE_NFIELDS (type) = nfields;
7217 TYPE_FIELDS (type) = (struct field *)
7218 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7219 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7220
b4ba55a1 7221 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7222 {
7223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7224
7225 TYPE_FIELD_PRIVATE_BITS (type) =
7226 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7227 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7228
7229 TYPE_FIELD_PROTECTED_BITS (type) =
7230 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7231 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7232
774b6a14
TT
7233 TYPE_FIELD_IGNORE_BITS (type) =
7234 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7235 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7236 }
7237
7238 /* If the type has baseclasses, allocate and clear a bit vector for
7239 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7240 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7241 {
7242 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7243 unsigned char *pointer;
c906108c
SS
7244
7245 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7246 pointer = TYPE_ALLOC (type, num_bytes);
7247 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7248 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7249 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7250 }
7251
3e43a32a
MS
7252 /* Copy the saved-up fields into the field vector. Start from the head of
7253 the list, adding to the tail of the field array, so that they end up in
7254 the same order in the array in which they were added to the list. */
c906108c
SS
7255 while (nfields-- > 0)
7256 {
7d0ccb61
DJ
7257 struct nextfield *fieldp;
7258
7259 if (fip->fields)
7260 {
7261 fieldp = fip->fields;
7262 fip->fields = fieldp->next;
7263 }
7264 else
7265 {
7266 fieldp = fip->baseclasses;
7267 fip->baseclasses = fieldp->next;
7268 }
7269
7270 TYPE_FIELD (type, nfields) = fieldp->field;
7271 switch (fieldp->accessibility)
c906108c 7272 {
c5aa993b 7273 case DW_ACCESS_private:
b4ba55a1
JB
7274 if (cu->language != language_ada)
7275 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7276 break;
c906108c 7277
c5aa993b 7278 case DW_ACCESS_protected:
b4ba55a1
JB
7279 if (cu->language != language_ada)
7280 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7281 break;
c906108c 7282
c5aa993b
JM
7283 case DW_ACCESS_public:
7284 break;
c906108c 7285
c5aa993b
JM
7286 default:
7287 /* Unknown accessibility. Complain and treat it as public. */
7288 {
e2e0b3e5 7289 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7290 fieldp->accessibility);
c5aa993b
JM
7291 }
7292 break;
c906108c
SS
7293 }
7294 if (nfields < fip->nbaseclasses)
7295 {
7d0ccb61 7296 switch (fieldp->virtuality)
c906108c 7297 {
c5aa993b
JM
7298 case DW_VIRTUALITY_virtual:
7299 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7300 if (cu->language == language_ada)
a73c6dcd 7301 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7302 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7303 break;
c906108c
SS
7304 }
7305 }
c906108c
SS
7306 }
7307}
7308
c906108c
SS
7309/* Add a member function to the proper fieldlist. */
7310
7311static void
107d2387 7312dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7313 struct type *type, struct dwarf2_cu *cu)
c906108c 7314{
e7c27a73 7315 struct objfile *objfile = cu->objfile;
c906108c
SS
7316 struct attribute *attr;
7317 struct fnfieldlist *flp;
7318 int i;
7319 struct fn_field *fnp;
7320 char *fieldname;
c906108c 7321 struct nextfnfield *new_fnfield;
f792889a 7322 struct type *this_type;
60d5a603 7323 enum dwarf_access_attribute accessibility;
c906108c 7324
b4ba55a1 7325 if (cu->language == language_ada)
a73c6dcd 7326 error (_("unexpected member function in Ada type"));
b4ba55a1 7327
2df3850c 7328 /* Get name of member function. */
39cbfefa
DJ
7329 fieldname = dwarf2_name (die, cu);
7330 if (fieldname == NULL)
2df3850c 7331 return;
c906108c 7332
c906108c
SS
7333 /* Look up member function name in fieldlist. */
7334 for (i = 0; i < fip->nfnfields; i++)
7335 {
27bfe10e 7336 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7337 break;
7338 }
7339
7340 /* Create new list element if necessary. */
7341 if (i < fip->nfnfields)
7342 flp = &fip->fnfieldlists[i];
7343 else
7344 {
7345 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7346 {
7347 fip->fnfieldlists = (struct fnfieldlist *)
7348 xrealloc (fip->fnfieldlists,
7349 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7350 * sizeof (struct fnfieldlist));
c906108c 7351 if (fip->nfnfields == 0)
c13c43fd 7352 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7353 }
7354 flp = &fip->fnfieldlists[fip->nfnfields];
7355 flp->name = fieldname;
7356 flp->length = 0;
7357 flp->head = NULL;
3da10d80 7358 i = fip->nfnfields++;
c906108c
SS
7359 }
7360
7361 /* Create a new member function field and chain it to the field list
0963b4bd 7362 entry. */
c906108c 7363 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7364 make_cleanup (xfree, new_fnfield);
c906108c
SS
7365 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7366 new_fnfield->next = flp->head;
7367 flp->head = new_fnfield;
7368 flp->length++;
7369
7370 /* Fill in the member function field info. */
7371 fnp = &new_fnfield->fnfield;
3da10d80
KS
7372
7373 /* Delay processing of the physname until later. */
7374 if (cu->language == language_cplus || cu->language == language_java)
7375 {
7376 add_to_method_list (type, i, flp->length - 1, fieldname,
7377 die, cu);
7378 }
7379 else
7380 {
1d06ead6 7381 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7382 fnp->physname = physname ? physname : "";
7383 }
7384
c906108c 7385 fnp->type = alloc_type (objfile);
f792889a
DJ
7386 this_type = read_type_die (die, cu);
7387 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7388 {
f792889a 7389 int nparams = TYPE_NFIELDS (this_type);
c906108c 7390
f792889a 7391 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7392 of the method itself (TYPE_CODE_METHOD). */
7393 smash_to_method_type (fnp->type, type,
f792889a
DJ
7394 TYPE_TARGET_TYPE (this_type),
7395 TYPE_FIELDS (this_type),
7396 TYPE_NFIELDS (this_type),
7397 TYPE_VARARGS (this_type));
c906108c
SS
7398
7399 /* Handle static member functions.
c5aa993b 7400 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7401 member functions. G++ helps GDB by marking the first
7402 parameter for non-static member functions (which is the this
7403 pointer) as artificial. We obtain this information from
7404 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7405 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7406 fnp->voffset = VOFFSET_STATIC;
7407 }
7408 else
e2e0b3e5 7409 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7410 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7411
7412 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7413 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7414 fnp->fcontext = die_containing_type (die, cu);
c906108c 7415
3e43a32a
MS
7416 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7417 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7418
7419 /* Get accessibility. */
e142c38c 7420 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7421 if (attr)
60d5a603
JK
7422 accessibility = DW_UNSND (attr);
7423 else
7424 accessibility = dwarf2_default_access_attribute (die, cu);
7425 switch (accessibility)
c906108c 7426 {
60d5a603
JK
7427 case DW_ACCESS_private:
7428 fnp->is_private = 1;
7429 break;
7430 case DW_ACCESS_protected:
7431 fnp->is_protected = 1;
7432 break;
c906108c
SS
7433 }
7434
b02dede2 7435 /* Check for artificial methods. */
e142c38c 7436 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7437 if (attr && DW_UNSND (attr) != 0)
7438 fnp->is_artificial = 1;
7439
0d564a31 7440 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7441 function. For older versions of GCC, this is an offset in the
7442 appropriate virtual table, as specified by DW_AT_containing_type.
7443 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7444 to the object address. */
7445
e142c38c 7446 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7447 if (attr)
8e19ed76 7448 {
aec5aa8b 7449 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7450 {
aec5aa8b
TT
7451 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7452 {
7453 /* Old-style GCC. */
7454 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7455 }
7456 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7457 || (DW_BLOCK (attr)->size > 1
7458 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7459 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7460 {
7461 struct dwarf_block blk;
7462 int offset;
7463
7464 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7465 ? 1 : 2);
7466 blk.size = DW_BLOCK (attr)->size - offset;
7467 blk.data = DW_BLOCK (attr)->data + offset;
7468 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7469 if ((fnp->voffset % cu->header.addr_size) != 0)
7470 dwarf2_complex_location_expr_complaint ();
7471 else
7472 fnp->voffset /= cu->header.addr_size;
7473 fnp->voffset += 2;
7474 }
7475 else
7476 dwarf2_complex_location_expr_complaint ();
7477
7478 if (!fnp->fcontext)
7479 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7480 }
3690dd37 7481 else if (attr_form_is_section_offset (attr))
8e19ed76 7482 {
4d3c2250 7483 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7484 }
7485 else
7486 {
4d3c2250
KB
7487 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7488 fieldname);
8e19ed76 7489 }
0d564a31 7490 }
d48cc9dd
DJ
7491 else
7492 {
7493 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7494 if (attr && DW_UNSND (attr))
7495 {
7496 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7497 complaint (&symfile_complaints,
3e43a32a
MS
7498 _("Member function \"%s\" (offset %d) is virtual "
7499 "but the vtable offset is not specified"),
d48cc9dd 7500 fieldname, die->offset);
9655fd1a 7501 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7502 TYPE_CPLUS_DYNAMIC (type) = 1;
7503 }
7504 }
c906108c
SS
7505}
7506
7507/* Create the vector of member function fields, and attach it to the type. */
7508
7509static void
fba45db2 7510dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7511 struct dwarf2_cu *cu)
c906108c
SS
7512{
7513 struct fnfieldlist *flp;
7514 int total_length = 0;
7515 int i;
7516
b4ba55a1 7517 if (cu->language == language_ada)
a73c6dcd 7518 error (_("unexpected member functions in Ada type"));
b4ba55a1 7519
c906108c
SS
7520 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7521 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7522 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7523
7524 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7525 {
7526 struct nextfnfield *nfp = flp->head;
7527 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7528 int k;
7529
7530 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7531 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7532 fn_flp->fn_fields = (struct fn_field *)
7533 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7534 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7535 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7536
7537 total_length += flp->length;
7538 }
7539
7540 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7541 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7542}
7543
1168df01
JB
7544/* Returns non-zero if NAME is the name of a vtable member in CU's
7545 language, zero otherwise. */
7546static int
7547is_vtable_name (const char *name, struct dwarf2_cu *cu)
7548{
7549 static const char vptr[] = "_vptr";
987504bb 7550 static const char vtable[] = "vtable";
1168df01 7551
987504bb
JJ
7552 /* Look for the C++ and Java forms of the vtable. */
7553 if ((cu->language == language_java
7554 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7555 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7556 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7557 return 1;
7558
7559 return 0;
7560}
7561
c0dd20ea 7562/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7563 functions, with the ABI-specified layout. If TYPE describes
7564 such a structure, smash it into a member function type.
61049d3b
DJ
7565
7566 GCC shouldn't do this; it should just output pointer to member DIEs.
7567 This is GCC PR debug/28767. */
c0dd20ea 7568
0b92b5bb
TT
7569static void
7570quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7571{
0b92b5bb 7572 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7573
7574 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7575 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7576 return;
c0dd20ea
DJ
7577
7578 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7579 if (TYPE_FIELD_NAME (type, 0) == NULL
7580 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7581 || TYPE_FIELD_NAME (type, 1) == NULL
7582 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7583 return;
c0dd20ea
DJ
7584
7585 /* Find the type of the method. */
0b92b5bb 7586 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7587 if (pfn_type == NULL
7588 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7589 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7590 return;
c0dd20ea
DJ
7591
7592 /* Look for the "this" argument. */
7593 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7594 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7595 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7596 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7597 return;
c0dd20ea
DJ
7598
7599 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7600 new_type = alloc_type (objfile);
7601 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7602 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7603 TYPE_VARARGS (pfn_type));
0b92b5bb 7604 smash_to_methodptr_type (type, new_type);
c0dd20ea 7605}
1168df01 7606
c906108c 7607/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7608 (definition) to create a type for the structure or union. Fill in
7609 the type's name and general properties; the members will not be
7610 processed until process_structure_type.
c906108c 7611
c767944b
DJ
7612 NOTE: we need to call these functions regardless of whether or not the
7613 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7614 structure or union. This gets the type entered into our set of
7615 user defined types.
7616
7617 However, if the structure is incomplete (an opaque struct/union)
7618 then suppress creating a symbol table entry for it since gdb only
7619 wants to find the one with the complete definition. Note that if
7620 it is complete, we just call new_symbol, which does it's own
7621 checking about whether the struct/union is anonymous or not (and
7622 suppresses creating a symbol table entry itself). */
7623
f792889a 7624static struct type *
134d01f1 7625read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7626{
e7c27a73 7627 struct objfile *objfile = cu->objfile;
c906108c
SS
7628 struct type *type;
7629 struct attribute *attr;
39cbfefa 7630 char *name;
c906108c 7631
348e048f
DE
7632 /* If the definition of this type lives in .debug_types, read that type.
7633 Don't follow DW_AT_specification though, that will take us back up
7634 the chain and we want to go down. */
7635 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7636 if (attr)
7637 {
7638 struct dwarf2_cu *type_cu = cu;
7639 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7640
348e048f
DE
7641 /* We could just recurse on read_structure_type, but we need to call
7642 get_die_type to ensure only one type for this DIE is created.
7643 This is important, for example, because for c++ classes we need
7644 TYPE_NAME set which is only done by new_symbol. Blech. */
7645 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7646
7647 /* TYPE_CU may not be the same as CU.
7648 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7649 return set_die_type (die, type, cu);
7650 }
7651
c0dd20ea 7652 type = alloc_type (objfile);
c906108c 7653 INIT_CPLUS_SPECIFIC (type);
93311388 7654
39cbfefa
DJ
7655 name = dwarf2_name (die, cu);
7656 if (name != NULL)
c906108c 7657 {
987504bb
JJ
7658 if (cu->language == language_cplus
7659 || cu->language == language_java)
63d06c5c 7660 {
3da10d80
KS
7661 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7662
7663 /* dwarf2_full_name might have already finished building the DIE's
7664 type. If so, there is no need to continue. */
7665 if (get_die_type (die, cu) != NULL)
7666 return get_die_type (die, cu);
7667
7668 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7669 if (die->tag == DW_TAG_structure_type
7670 || die->tag == DW_TAG_class_type)
7671 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7672 }
7673 else
7674 {
d8151005
DJ
7675 /* The name is already allocated along with this objfile, so
7676 we don't need to duplicate it for the type. */
94af9270
KS
7677 TYPE_TAG_NAME (type) = (char *) name;
7678 if (die->tag == DW_TAG_class_type)
7679 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7680 }
c906108c
SS
7681 }
7682
7683 if (die->tag == DW_TAG_structure_type)
7684 {
7685 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7686 }
7687 else if (die->tag == DW_TAG_union_type)
7688 {
7689 TYPE_CODE (type) = TYPE_CODE_UNION;
7690 }
7691 else
7692 {
c906108c
SS
7693 TYPE_CODE (type) = TYPE_CODE_CLASS;
7694 }
7695
0cc2414c
TT
7696 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7697 TYPE_DECLARED_CLASS (type) = 1;
7698
e142c38c 7699 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7700 if (attr)
7701 {
7702 TYPE_LENGTH (type) = DW_UNSND (attr);
7703 }
7704 else
7705 {
7706 TYPE_LENGTH (type) = 0;
7707 }
7708
876cecd0 7709 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7710 if (die_is_declaration (die, cu))
876cecd0 7711 TYPE_STUB (type) = 1;
a6c727b2
DJ
7712 else if (attr == NULL && die->child == NULL
7713 && producer_is_realview (cu->producer))
7714 /* RealView does not output the required DW_AT_declaration
7715 on incomplete types. */
7716 TYPE_STUB (type) = 1;
dc718098 7717
c906108c
SS
7718 /* We need to add the type field to the die immediately so we don't
7719 infinitely recurse when dealing with pointers to the structure
0963b4bd 7720 type within the structure itself. */
1c379e20 7721 set_die_type (die, type, cu);
c906108c 7722
7e314c57
JK
7723 /* set_die_type should be already done. */
7724 set_descriptive_type (type, die, cu);
7725
c767944b
DJ
7726 return type;
7727}
7728
7729/* Finish creating a structure or union type, including filling in
7730 its members and creating a symbol for it. */
7731
7732static void
7733process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7734{
7735 struct objfile *objfile = cu->objfile;
7736 struct die_info *child_die = die->child;
7737 struct type *type;
7738
7739 type = get_die_type (die, cu);
7740 if (type == NULL)
7741 type = read_structure_type (die, cu);
7742
e142c38c 7743 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7744 {
7745 struct field_info fi;
7746 struct die_info *child_die;
34eaf542 7747 VEC (symbolp) *template_args = NULL;
c767944b 7748 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7749
7750 memset (&fi, 0, sizeof (struct field_info));
7751
639d11d3 7752 child_die = die->child;
c906108c
SS
7753
7754 while (child_die && child_die->tag)
7755 {
a9a9bd0f
DC
7756 if (child_die->tag == DW_TAG_member
7757 || child_die->tag == DW_TAG_variable)
c906108c 7758 {
a9a9bd0f
DC
7759 /* NOTE: carlton/2002-11-05: A C++ static data member
7760 should be a DW_TAG_member that is a declaration, but
7761 all versions of G++ as of this writing (so through at
7762 least 3.2.1) incorrectly generate DW_TAG_variable
7763 tags for them instead. */
e7c27a73 7764 dwarf2_add_field (&fi, child_die, cu);
c906108c 7765 }
8713b1b1 7766 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7767 {
0963b4bd 7768 /* C++ member function. */
e7c27a73 7769 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7770 }
7771 else if (child_die->tag == DW_TAG_inheritance)
7772 {
7773 /* C++ base class field. */
e7c27a73 7774 dwarf2_add_field (&fi, child_die, cu);
c906108c 7775 }
98751a41
JK
7776 else if (child_die->tag == DW_TAG_typedef)
7777 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7778 else if (child_die->tag == DW_TAG_template_type_param
7779 || child_die->tag == DW_TAG_template_value_param)
7780 {
7781 struct symbol *arg = new_symbol (child_die, NULL, cu);
7782
f1078f66
DJ
7783 if (arg != NULL)
7784 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7785 }
7786
c906108c
SS
7787 child_die = sibling_die (child_die);
7788 }
7789
34eaf542
TT
7790 /* Attach template arguments to type. */
7791 if (! VEC_empty (symbolp, template_args))
7792 {
7793 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7794 TYPE_N_TEMPLATE_ARGUMENTS (type)
7795 = VEC_length (symbolp, template_args);
7796 TYPE_TEMPLATE_ARGUMENTS (type)
7797 = obstack_alloc (&objfile->objfile_obstack,
7798 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7799 * sizeof (struct symbol *)));
7800 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7801 VEC_address (symbolp, template_args),
7802 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7803 * sizeof (struct symbol *)));
7804 VEC_free (symbolp, template_args);
7805 }
7806
c906108c
SS
7807 /* Attach fields and member functions to the type. */
7808 if (fi.nfields)
e7c27a73 7809 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7810 if (fi.nfnfields)
7811 {
e7c27a73 7812 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7813
c5aa993b 7814 /* Get the type which refers to the base class (possibly this
c906108c 7815 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7816 class from the DW_AT_containing_type attribute. This use of
7817 DW_AT_containing_type is a GNU extension. */
c906108c 7818
e142c38c 7819 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7820 {
e7c27a73 7821 struct type *t = die_containing_type (die, cu);
c906108c
SS
7822
7823 TYPE_VPTR_BASETYPE (type) = t;
7824 if (type == t)
7825 {
c906108c
SS
7826 int i;
7827
7828 /* Our own class provides vtbl ptr. */
7829 for (i = TYPE_NFIELDS (t) - 1;
7830 i >= TYPE_N_BASECLASSES (t);
7831 --i)
7832 {
7833 char *fieldname = TYPE_FIELD_NAME (t, i);
7834
1168df01 7835 if (is_vtable_name (fieldname, cu))
c906108c
SS
7836 {
7837 TYPE_VPTR_FIELDNO (type) = i;
7838 break;
7839 }
7840 }
7841
7842 /* Complain if virtual function table field not found. */
7843 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7844 complaint (&symfile_complaints,
3e43a32a
MS
7845 _("virtual function table pointer "
7846 "not found when defining class '%s'"),
4d3c2250
KB
7847 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7848 "");
c906108c
SS
7849 }
7850 else
7851 {
7852 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7853 }
7854 }
f6235d4c
EZ
7855 else if (cu->producer
7856 && strncmp (cu->producer,
7857 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7858 {
7859 /* The IBM XLC compiler does not provide direct indication
7860 of the containing type, but the vtable pointer is
7861 always named __vfp. */
7862
7863 int i;
7864
7865 for (i = TYPE_NFIELDS (type) - 1;
7866 i >= TYPE_N_BASECLASSES (type);
7867 --i)
7868 {
7869 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7870 {
7871 TYPE_VPTR_FIELDNO (type) = i;
7872 TYPE_VPTR_BASETYPE (type) = type;
7873 break;
7874 }
7875 }
7876 }
c906108c 7877 }
98751a41
JK
7878
7879 /* Copy fi.typedef_field_list linked list elements content into the
7880 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7881 if (fi.typedef_field_list)
7882 {
7883 int i = fi.typedef_field_list_count;
7884
a0d7a4ff 7885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7886 TYPE_TYPEDEF_FIELD_ARRAY (type)
7887 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7888 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7889
7890 /* Reverse the list order to keep the debug info elements order. */
7891 while (--i >= 0)
7892 {
7893 struct typedef_field *dest, *src;
6e70227d 7894
98751a41
JK
7895 dest = &TYPE_TYPEDEF_FIELD (type, i);
7896 src = &fi.typedef_field_list->field;
7897 fi.typedef_field_list = fi.typedef_field_list->next;
7898 *dest = *src;
7899 }
7900 }
c767944b
DJ
7901
7902 do_cleanups (back_to);
eb2a6f42
TT
7903
7904 if (HAVE_CPLUS_STRUCT (type))
7905 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7906 }
63d06c5c 7907
0b92b5bb
TT
7908 quirk_gcc_member_function_pointer (type, cu->objfile);
7909
90aeadfc
DC
7910 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7911 snapshots) has been known to create a die giving a declaration
7912 for a class that has, as a child, a die giving a definition for a
7913 nested class. So we have to process our children even if the
7914 current die is a declaration. Normally, of course, a declaration
7915 won't have any children at all. */
134d01f1 7916
90aeadfc
DC
7917 while (child_die != NULL && child_die->tag)
7918 {
7919 if (child_die->tag == DW_TAG_member
7920 || child_die->tag == DW_TAG_variable
34eaf542
TT
7921 || child_die->tag == DW_TAG_inheritance
7922 || child_die->tag == DW_TAG_template_value_param
7923 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7924 {
90aeadfc 7925 /* Do nothing. */
134d01f1 7926 }
90aeadfc
DC
7927 else
7928 process_die (child_die, cu);
134d01f1 7929
90aeadfc 7930 child_die = sibling_die (child_die);
134d01f1
DJ
7931 }
7932
fa4028e9
JB
7933 /* Do not consider external references. According to the DWARF standard,
7934 these DIEs are identified by the fact that they have no byte_size
7935 attribute, and a declaration attribute. */
7936 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7937 || !die_is_declaration (die, cu))
c767944b 7938 new_symbol (die, type, cu);
134d01f1
DJ
7939}
7940
7941/* Given a DW_AT_enumeration_type die, set its type. We do not
7942 complete the type's fields yet, or create any symbols. */
c906108c 7943
f792889a 7944static struct type *
134d01f1 7945read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7946{
e7c27a73 7947 struct objfile *objfile = cu->objfile;
c906108c 7948 struct type *type;
c906108c 7949 struct attribute *attr;
0114d602 7950 const char *name;
134d01f1 7951
348e048f
DE
7952 /* If the definition of this type lives in .debug_types, read that type.
7953 Don't follow DW_AT_specification though, that will take us back up
7954 the chain and we want to go down. */
7955 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7956 if (attr)
7957 {
7958 struct dwarf2_cu *type_cu = cu;
7959 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7960
348e048f 7961 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7962
7963 /* TYPE_CU may not be the same as CU.
7964 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7965 return set_die_type (die, type, cu);
7966 }
7967
c906108c
SS
7968 type = alloc_type (objfile);
7969
7970 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7971 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7972 if (name != NULL)
0114d602 7973 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7974
e142c38c 7975 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7976 if (attr)
7977 {
7978 TYPE_LENGTH (type) = DW_UNSND (attr);
7979 }
7980 else
7981 {
7982 TYPE_LENGTH (type) = 0;
7983 }
7984
137033e9
JB
7985 /* The enumeration DIE can be incomplete. In Ada, any type can be
7986 declared as private in the package spec, and then defined only
7987 inside the package body. Such types are known as Taft Amendment
7988 Types. When another package uses such a type, an incomplete DIE
7989 may be generated by the compiler. */
02eb380e 7990 if (die_is_declaration (die, cu))
876cecd0 7991 TYPE_STUB (type) = 1;
02eb380e 7992
f792889a 7993 return set_die_type (die, type, cu);
134d01f1
DJ
7994}
7995
7996/* Given a pointer to a die which begins an enumeration, process all
7997 the dies that define the members of the enumeration, and create the
7998 symbol for the enumeration type.
7999
8000 NOTE: We reverse the order of the element list. */
8001
8002static void
8003process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8004{
f792889a 8005 struct type *this_type;
134d01f1 8006
f792889a
DJ
8007 this_type = get_die_type (die, cu);
8008 if (this_type == NULL)
8009 this_type = read_enumeration_type (die, cu);
9dc481d3 8010
639d11d3 8011 if (die->child != NULL)
c906108c 8012 {
9dc481d3
DE
8013 struct die_info *child_die;
8014 struct symbol *sym;
8015 struct field *fields = NULL;
8016 int num_fields = 0;
8017 int unsigned_enum = 1;
8018 char *name;
8019
639d11d3 8020 child_die = die->child;
c906108c
SS
8021 while (child_die && child_die->tag)
8022 {
8023 if (child_die->tag != DW_TAG_enumerator)
8024 {
e7c27a73 8025 process_die (child_die, cu);
c906108c
SS
8026 }
8027 else
8028 {
39cbfefa
DJ
8029 name = dwarf2_name (child_die, cu);
8030 if (name)
c906108c 8031 {
f792889a 8032 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
8033 if (SYMBOL_VALUE (sym) < 0)
8034 unsigned_enum = 0;
8035
8036 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8037 {
8038 fields = (struct field *)
8039 xrealloc (fields,
8040 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8041 * sizeof (struct field));
c906108c
SS
8042 }
8043
3567439c 8044 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8045 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8046 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8047 FIELD_BITSIZE (fields[num_fields]) = 0;
8048
8049 num_fields++;
8050 }
8051 }
8052
8053 child_die = sibling_die (child_die);
8054 }
8055
8056 if (num_fields)
8057 {
f792889a
DJ
8058 TYPE_NFIELDS (this_type) = num_fields;
8059 TYPE_FIELDS (this_type) = (struct field *)
8060 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8061 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8062 sizeof (struct field) * num_fields);
b8c9b27d 8063 xfree (fields);
c906108c
SS
8064 }
8065 if (unsigned_enum)
876cecd0 8066 TYPE_UNSIGNED (this_type) = 1;
c906108c 8067 }
134d01f1 8068
6c83ed52
TT
8069 /* If we are reading an enum from a .debug_types unit, and the enum
8070 is a declaration, and the enum is not the signatured type in the
8071 unit, then we do not want to add a symbol for it. Adding a
8072 symbol would in some cases obscure the true definition of the
8073 enum, giving users an incomplete type when the definition is
8074 actually available. Note that we do not want to do this for all
8075 enums which are just declarations, because C++0x allows forward
8076 enum declarations. */
8077 if (cu->per_cu->debug_type_section
8078 && die_is_declaration (die, cu))
8079 {
8080 struct signatured_type *type_sig;
8081
8082 type_sig
8083 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8084 cu->per_cu->debug_type_section,
8085 cu->per_cu->offset);
8086 if (type_sig->type_offset != die->offset)
8087 return;
8088 }
8089
f792889a 8090 new_symbol (die, this_type, cu);
c906108c
SS
8091}
8092
8093/* Extract all information from a DW_TAG_array_type DIE and put it in
8094 the DIE's type field. For now, this only handles one dimensional
8095 arrays. */
8096
f792889a 8097static struct type *
e7c27a73 8098read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8099{
e7c27a73 8100 struct objfile *objfile = cu->objfile;
c906108c 8101 struct die_info *child_die;
7e314c57 8102 struct type *type;
c906108c
SS
8103 struct type *element_type, *range_type, *index_type;
8104 struct type **range_types = NULL;
8105 struct attribute *attr;
8106 int ndim = 0;
8107 struct cleanup *back_to;
39cbfefa 8108 char *name;
c906108c 8109
e7c27a73 8110 element_type = die_type (die, cu);
c906108c 8111
7e314c57
JK
8112 /* The die_type call above may have already set the type for this DIE. */
8113 type = get_die_type (die, cu);
8114 if (type)
8115 return type;
8116
c906108c
SS
8117 /* Irix 6.2 native cc creates array types without children for
8118 arrays with unspecified length. */
639d11d3 8119 if (die->child == NULL)
c906108c 8120 {
46bf5051 8121 index_type = objfile_type (objfile)->builtin_int;
c906108c 8122 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8123 type = create_array_type (NULL, element_type, range_type);
8124 return set_die_type (die, type, cu);
c906108c
SS
8125 }
8126
8127 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8128 child_die = die->child;
c906108c
SS
8129 while (child_die && child_die->tag)
8130 {
8131 if (child_die->tag == DW_TAG_subrange_type)
8132 {
f792889a 8133 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8134
f792889a 8135 if (child_type != NULL)
a02abb62 8136 {
0963b4bd
MS
8137 /* The range type was succesfully read. Save it for the
8138 array type creation. */
a02abb62
JB
8139 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8140 {
8141 range_types = (struct type **)
8142 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8143 * sizeof (struct type *));
8144 if (ndim == 0)
8145 make_cleanup (free_current_contents, &range_types);
8146 }
f792889a 8147 range_types[ndim++] = child_type;
a02abb62 8148 }
c906108c
SS
8149 }
8150 child_die = sibling_die (child_die);
8151 }
8152
8153 /* Dwarf2 dimensions are output from left to right, create the
8154 necessary array types in backwards order. */
7ca2d3a3 8155
c906108c 8156 type = element_type;
7ca2d3a3
DL
8157
8158 if (read_array_order (die, cu) == DW_ORD_col_major)
8159 {
8160 int i = 0;
9a619af0 8161
7ca2d3a3
DL
8162 while (i < ndim)
8163 type = create_array_type (NULL, type, range_types[i++]);
8164 }
8165 else
8166 {
8167 while (ndim-- > 0)
8168 type = create_array_type (NULL, type, range_types[ndim]);
8169 }
c906108c 8170
f5f8a009
EZ
8171 /* Understand Dwarf2 support for vector types (like they occur on
8172 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8173 array type. This is not part of the Dwarf2/3 standard yet, but a
8174 custom vendor extension. The main difference between a regular
8175 array and the vector variant is that vectors are passed by value
8176 to functions. */
e142c38c 8177 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8178 if (attr)
ea37ba09 8179 make_vector_type (type);
f5f8a009 8180
dbc98a8b
KW
8181 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8182 implementation may choose to implement triple vectors using this
8183 attribute. */
8184 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8185 if (attr)
8186 {
8187 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8188 TYPE_LENGTH (type) = DW_UNSND (attr);
8189 else
3e43a32a
MS
8190 complaint (&symfile_complaints,
8191 _("DW_AT_byte_size for array type smaller "
8192 "than the total size of elements"));
dbc98a8b
KW
8193 }
8194
39cbfefa
DJ
8195 name = dwarf2_name (die, cu);
8196 if (name)
8197 TYPE_NAME (type) = name;
6e70227d 8198
0963b4bd 8199 /* Install the type in the die. */
7e314c57
JK
8200 set_die_type (die, type, cu);
8201
8202 /* set_die_type should be already done. */
b4ba55a1
JB
8203 set_descriptive_type (type, die, cu);
8204
c906108c
SS
8205 do_cleanups (back_to);
8206
7e314c57 8207 return type;
c906108c
SS
8208}
8209
7ca2d3a3 8210static enum dwarf_array_dim_ordering
6e70227d 8211read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8212{
8213 struct attribute *attr;
8214
8215 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8216
8217 if (attr) return DW_SND (attr);
8218
0963b4bd
MS
8219 /* GNU F77 is a special case, as at 08/2004 array type info is the
8220 opposite order to the dwarf2 specification, but data is still
8221 laid out as per normal fortran.
7ca2d3a3 8222
0963b4bd
MS
8223 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8224 version checking. */
7ca2d3a3 8225
905e0470
PM
8226 if (cu->language == language_fortran
8227 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8228 {
8229 return DW_ORD_row_major;
8230 }
8231
6e70227d 8232 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8233 {
8234 case array_column_major:
8235 return DW_ORD_col_major;
8236 case array_row_major:
8237 default:
8238 return DW_ORD_row_major;
8239 };
8240}
8241
72019c9c 8242/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8243 the DIE's type field. */
72019c9c 8244
f792889a 8245static struct type *
72019c9c
GM
8246read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8247{
7e314c57
JK
8248 struct type *domain_type, *set_type;
8249 struct attribute *attr;
f792889a 8250
7e314c57
JK
8251 domain_type = die_type (die, cu);
8252
8253 /* The die_type call above may have already set the type for this DIE. */
8254 set_type = get_die_type (die, cu);
8255 if (set_type)
8256 return set_type;
8257
8258 set_type = create_set_type (NULL, domain_type);
8259
8260 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8261 if (attr)
8262 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8263
f792889a 8264 return set_die_type (die, set_type, cu);
72019c9c 8265}
7ca2d3a3 8266
c906108c
SS
8267/* First cut: install each common block member as a global variable. */
8268
8269static void
e7c27a73 8270read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8271{
8272 struct die_info *child_die;
8273 struct attribute *attr;
8274 struct symbol *sym;
8275 CORE_ADDR base = (CORE_ADDR) 0;
8276
e142c38c 8277 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8278 if (attr)
8279 {
0963b4bd 8280 /* Support the .debug_loc offsets. */
8e19ed76
PS
8281 if (attr_form_is_block (attr))
8282 {
e7c27a73 8283 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8284 }
3690dd37 8285 else if (attr_form_is_section_offset (attr))
8e19ed76 8286 {
4d3c2250 8287 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8288 }
8289 else
8290 {
4d3c2250
KB
8291 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8292 "common block member");
8e19ed76 8293 }
c906108c 8294 }
639d11d3 8295 if (die->child != NULL)
c906108c 8296 {
639d11d3 8297 child_die = die->child;
c906108c
SS
8298 while (child_die && child_die->tag)
8299 {
74ac6d43
TT
8300 LONGEST offset;
8301
e7c27a73 8302 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8303 if (sym != NULL
8304 && handle_data_member_location (child_die, cu, &offset))
c906108c 8305 {
74ac6d43 8306 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8307 add_symbol_to_list (sym, &global_symbols);
8308 }
8309 child_die = sibling_die (child_die);
8310 }
8311 }
8312}
8313
0114d602 8314/* Create a type for a C++ namespace. */
d9fa45fe 8315
0114d602
DJ
8316static struct type *
8317read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8318{
e7c27a73 8319 struct objfile *objfile = cu->objfile;
0114d602 8320 const char *previous_prefix, *name;
9219021c 8321 int is_anonymous;
0114d602
DJ
8322 struct type *type;
8323
8324 /* For extensions, reuse the type of the original namespace. */
8325 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8326 {
8327 struct die_info *ext_die;
8328 struct dwarf2_cu *ext_cu = cu;
9a619af0 8329
0114d602
DJ
8330 ext_die = dwarf2_extension (die, &ext_cu);
8331 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8332
8333 /* EXT_CU may not be the same as CU.
8334 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8335 return set_die_type (die, type, cu);
8336 }
9219021c 8337
e142c38c 8338 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8339
8340 /* Now build the name of the current namespace. */
8341
0114d602
DJ
8342 previous_prefix = determine_prefix (die, cu);
8343 if (previous_prefix[0] != '\0')
8344 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8345 previous_prefix, name, 0, cu);
0114d602
DJ
8346
8347 /* Create the type. */
8348 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8349 objfile);
8350 TYPE_NAME (type) = (char *) name;
8351 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8352
60531b24 8353 return set_die_type (die, type, cu);
0114d602
DJ
8354}
8355
8356/* Read a C++ namespace. */
8357
8358static void
8359read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8360{
8361 struct objfile *objfile = cu->objfile;
0114d602 8362 int is_anonymous;
9219021c 8363
5c4e30ca
DC
8364 /* Add a symbol associated to this if we haven't seen the namespace
8365 before. Also, add a using directive if it's an anonymous
8366 namespace. */
9219021c 8367
f2f0e013 8368 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8369 {
8370 struct type *type;
8371
0114d602 8372 type = read_type_die (die, cu);
e7c27a73 8373 new_symbol (die, type, cu);
5c4e30ca 8374
e8e80198 8375 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8376 if (is_anonymous)
0114d602
DJ
8377 {
8378 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8379
c0cc3a76 8380 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8381 NULL, NULL, &objfile->objfile_obstack);
0114d602 8382 }
5c4e30ca 8383 }
9219021c 8384
639d11d3 8385 if (die->child != NULL)
d9fa45fe 8386 {
639d11d3 8387 struct die_info *child_die = die->child;
6e70227d 8388
d9fa45fe
DC
8389 while (child_die && child_die->tag)
8390 {
e7c27a73 8391 process_die (child_die, cu);
d9fa45fe
DC
8392 child_die = sibling_die (child_die);
8393 }
8394 }
38d518c9
EZ
8395}
8396
f55ee35c
JK
8397/* Read a Fortran module as type. This DIE can be only a declaration used for
8398 imported module. Still we need that type as local Fortran "use ... only"
8399 declaration imports depend on the created type in determine_prefix. */
8400
8401static struct type *
8402read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8403{
8404 struct objfile *objfile = cu->objfile;
8405 char *module_name;
8406 struct type *type;
8407
8408 module_name = dwarf2_name (die, cu);
8409 if (!module_name)
3e43a32a
MS
8410 complaint (&symfile_complaints,
8411 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8412 die->offset);
8413 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8414
8415 /* determine_prefix uses TYPE_TAG_NAME. */
8416 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8417
8418 return set_die_type (die, type, cu);
8419}
8420
5d7cb8df
JK
8421/* Read a Fortran module. */
8422
8423static void
8424read_module (struct die_info *die, struct dwarf2_cu *cu)
8425{
8426 struct die_info *child_die = die->child;
8427
5d7cb8df
JK
8428 while (child_die && child_die->tag)
8429 {
8430 process_die (child_die, cu);
8431 child_die = sibling_die (child_die);
8432 }
8433}
8434
38d518c9
EZ
8435/* Return the name of the namespace represented by DIE. Set
8436 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8437 namespace. */
8438
8439static const char *
e142c38c 8440namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8441{
8442 struct die_info *current_die;
8443 const char *name = NULL;
8444
8445 /* Loop through the extensions until we find a name. */
8446
8447 for (current_die = die;
8448 current_die != NULL;
f2f0e013 8449 current_die = dwarf2_extension (die, &cu))
38d518c9 8450 {
e142c38c 8451 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8452 if (name != NULL)
8453 break;
8454 }
8455
8456 /* Is it an anonymous namespace? */
8457
8458 *is_anonymous = (name == NULL);
8459 if (*is_anonymous)
2b1dbab0 8460 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8461
8462 return name;
d9fa45fe
DC
8463}
8464
c906108c
SS
8465/* Extract all information from a DW_TAG_pointer_type DIE and add to
8466 the user defined type vector. */
8467
f792889a 8468static struct type *
e7c27a73 8469read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8470{
5e2b427d 8471 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8472 struct comp_unit_head *cu_header = &cu->header;
c906108c 8473 struct type *type;
8b2dbe47
KB
8474 struct attribute *attr_byte_size;
8475 struct attribute *attr_address_class;
8476 int byte_size, addr_class;
7e314c57
JK
8477 struct type *target_type;
8478
8479 target_type = die_type (die, cu);
c906108c 8480
7e314c57
JK
8481 /* The die_type call above may have already set the type for this DIE. */
8482 type = get_die_type (die, cu);
8483 if (type)
8484 return type;
8485
8486 type = lookup_pointer_type (target_type);
8b2dbe47 8487
e142c38c 8488 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8489 if (attr_byte_size)
8490 byte_size = DW_UNSND (attr_byte_size);
c906108c 8491 else
8b2dbe47
KB
8492 byte_size = cu_header->addr_size;
8493
e142c38c 8494 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8495 if (attr_address_class)
8496 addr_class = DW_UNSND (attr_address_class);
8497 else
8498 addr_class = DW_ADDR_none;
8499
8500 /* If the pointer size or address class is different than the
8501 default, create a type variant marked as such and set the
8502 length accordingly. */
8503 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8504 {
5e2b427d 8505 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8506 {
8507 int type_flags;
8508
849957d9 8509 type_flags = gdbarch_address_class_type_flags
5e2b427d 8510 (gdbarch, byte_size, addr_class);
876cecd0
TT
8511 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8512 == 0);
8b2dbe47
KB
8513 type = make_type_with_address_space (type, type_flags);
8514 }
8515 else if (TYPE_LENGTH (type) != byte_size)
8516 {
3e43a32a
MS
8517 complaint (&symfile_complaints,
8518 _("invalid pointer size %d"), byte_size);
8b2dbe47 8519 }
6e70227d 8520 else
9a619af0
MS
8521 {
8522 /* Should we also complain about unhandled address classes? */
8523 }
c906108c 8524 }
8b2dbe47
KB
8525
8526 TYPE_LENGTH (type) = byte_size;
f792889a 8527 return set_die_type (die, type, cu);
c906108c
SS
8528}
8529
8530/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8531 the user defined type vector. */
8532
f792889a 8533static struct type *
e7c27a73 8534read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8535{
8536 struct type *type;
8537 struct type *to_type;
8538 struct type *domain;
8539
e7c27a73
DJ
8540 to_type = die_type (die, cu);
8541 domain = die_containing_type (die, cu);
0d5de010 8542
7e314c57
JK
8543 /* The calls above may have already set the type for this DIE. */
8544 type = get_die_type (die, cu);
8545 if (type)
8546 return type;
8547
0d5de010
DJ
8548 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8549 type = lookup_methodptr_type (to_type);
8550 else
8551 type = lookup_memberptr_type (to_type, domain);
c906108c 8552
f792889a 8553 return set_die_type (die, type, cu);
c906108c
SS
8554}
8555
8556/* Extract all information from a DW_TAG_reference_type DIE and add to
8557 the user defined type vector. */
8558
f792889a 8559static struct type *
e7c27a73 8560read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8561{
e7c27a73 8562 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8563 struct type *type, *target_type;
c906108c
SS
8564 struct attribute *attr;
8565
7e314c57
JK
8566 target_type = die_type (die, cu);
8567
8568 /* The die_type call above may have already set the type for this DIE. */
8569 type = get_die_type (die, cu);
8570 if (type)
8571 return type;
8572
8573 type = lookup_reference_type (target_type);
e142c38c 8574 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8575 if (attr)
8576 {
8577 TYPE_LENGTH (type) = DW_UNSND (attr);
8578 }
8579 else
8580 {
107d2387 8581 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8582 }
f792889a 8583 return set_die_type (die, type, cu);
c906108c
SS
8584}
8585
f792889a 8586static struct type *
e7c27a73 8587read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8588{
f792889a 8589 struct type *base_type, *cv_type;
c906108c 8590
e7c27a73 8591 base_type = die_type (die, cu);
7e314c57
JK
8592
8593 /* The die_type call above may have already set the type for this DIE. */
8594 cv_type = get_die_type (die, cu);
8595 if (cv_type)
8596 return cv_type;
8597
2f608a3a
KW
8598 /* In case the const qualifier is applied to an array type, the element type
8599 is so qualified, not the array type (section 6.7.3 of C99). */
8600 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8601 {
8602 struct type *el_type, *inner_array;
8603
8604 base_type = copy_type (base_type);
8605 inner_array = base_type;
8606
8607 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8608 {
8609 TYPE_TARGET_TYPE (inner_array) =
8610 copy_type (TYPE_TARGET_TYPE (inner_array));
8611 inner_array = TYPE_TARGET_TYPE (inner_array);
8612 }
8613
8614 el_type = TYPE_TARGET_TYPE (inner_array);
8615 TYPE_TARGET_TYPE (inner_array) =
8616 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8617
8618 return set_die_type (die, base_type, cu);
8619 }
8620
f792889a
DJ
8621 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8622 return set_die_type (die, cv_type, cu);
c906108c
SS
8623}
8624
f792889a 8625static struct type *
e7c27a73 8626read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8627{
f792889a 8628 struct type *base_type, *cv_type;
c906108c 8629
e7c27a73 8630 base_type = die_type (die, cu);
7e314c57
JK
8631
8632 /* The die_type call above may have already set the type for this DIE. */
8633 cv_type = get_die_type (die, cu);
8634 if (cv_type)
8635 return cv_type;
8636
f792889a
DJ
8637 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8638 return set_die_type (die, cv_type, cu);
c906108c
SS
8639}
8640
8641/* Extract all information from a DW_TAG_string_type DIE and add to
8642 the user defined type vector. It isn't really a user defined type,
8643 but it behaves like one, with other DIE's using an AT_user_def_type
8644 attribute to reference it. */
8645
f792889a 8646static struct type *
e7c27a73 8647read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8648{
e7c27a73 8649 struct objfile *objfile = cu->objfile;
3b7538c0 8650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8651 struct type *type, *range_type, *index_type, *char_type;
8652 struct attribute *attr;
8653 unsigned int length;
8654
e142c38c 8655 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8656 if (attr)
8657 {
8658 length = DW_UNSND (attr);
8659 }
8660 else
8661 {
0963b4bd 8662 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8663 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8664 if (attr)
8665 {
8666 length = DW_UNSND (attr);
8667 }
8668 else
8669 {
8670 length = 1;
8671 }
c906108c 8672 }
6ccb9162 8673
46bf5051 8674 index_type = objfile_type (objfile)->builtin_int;
c906108c 8675 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8676 char_type = language_string_char_type (cu->language_defn, gdbarch);
8677 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8678
f792889a 8679 return set_die_type (die, type, cu);
c906108c
SS
8680}
8681
8682/* Handle DIES due to C code like:
8683
8684 struct foo
c5aa993b
JM
8685 {
8686 int (*funcp)(int a, long l);
8687 int b;
8688 };
c906108c 8689
0963b4bd 8690 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8691
f792889a 8692static struct type *
e7c27a73 8693read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8694{
0963b4bd
MS
8695 struct type *type; /* Type that this function returns. */
8696 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8697 struct attribute *attr;
8698
e7c27a73 8699 type = die_type (die, cu);
7e314c57
JK
8700
8701 /* The die_type call above may have already set the type for this DIE. */
8702 ftype = get_die_type (die, cu);
8703 if (ftype)
8704 return ftype;
8705
0c8b41f1 8706 ftype = lookup_function_type (type);
c906108c 8707
5b8101ae 8708 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8709 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8710 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8711 || cu->language == language_cplus
5b8101ae
PM
8712 || cu->language == language_java
8713 || cu->language == language_pascal)
876cecd0 8714 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8715 else if (producer_is_realview (cu->producer))
8716 /* RealView does not emit DW_AT_prototyped. We can not
8717 distinguish prototyped and unprototyped functions; default to
8718 prototyped, since that is more common in modern code (and
8719 RealView warns about unprototyped functions). */
8720 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8721
c055b101
CV
8722 /* Store the calling convention in the type if it's available in
8723 the subroutine die. Otherwise set the calling convention to
8724 the default value DW_CC_normal. */
8725 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8726 if (attr)
8727 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8728 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8729 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8730 else
8731 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8732
8733 /* We need to add the subroutine type to the die immediately so
8734 we don't infinitely recurse when dealing with parameters
0963b4bd 8735 declared as the same subroutine type. */
76c10ea2 8736 set_die_type (die, ftype, cu);
6e70227d 8737
639d11d3 8738 if (die->child != NULL)
c906108c 8739 {
8072405b 8740 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8741 struct die_info *child_die;
8072405b 8742 int nparams, iparams;
c906108c
SS
8743
8744 /* Count the number of parameters.
8745 FIXME: GDB currently ignores vararg functions, but knows about
8746 vararg member functions. */
8072405b 8747 nparams = 0;
639d11d3 8748 child_die = die->child;
c906108c
SS
8749 while (child_die && child_die->tag)
8750 {
8751 if (child_die->tag == DW_TAG_formal_parameter)
8752 nparams++;
8753 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8754 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8755 child_die = sibling_die (child_die);
8756 }
8757
8758 /* Allocate storage for parameters and fill them in. */
8759 TYPE_NFIELDS (ftype) = nparams;
8760 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8761 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8762
8072405b
JK
8763 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8764 even if we error out during the parameters reading below. */
8765 for (iparams = 0; iparams < nparams; iparams++)
8766 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8767
8768 iparams = 0;
639d11d3 8769 child_die = die->child;
c906108c
SS
8770 while (child_die && child_die->tag)
8771 {
8772 if (child_die->tag == DW_TAG_formal_parameter)
8773 {
3ce3b1ba
PA
8774 struct type *arg_type;
8775
8776 /* DWARF version 2 has no clean way to discern C++
8777 static and non-static member functions. G++ helps
8778 GDB by marking the first parameter for non-static
8779 member functions (which is the this pointer) as
8780 artificial. We pass this information to
8781 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8782
8783 DWARF version 3 added DW_AT_object_pointer, which GCC
8784 4.5 does not yet generate. */
e142c38c 8785 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8786 if (attr)
8787 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8788 else
418835cc
KS
8789 {
8790 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8791
8792 /* GCC/43521: In java, the formal parameter
8793 "this" is sometimes not marked with DW_AT_artificial. */
8794 if (cu->language == language_java)
8795 {
8796 const char *name = dwarf2_name (child_die, cu);
9a619af0 8797
418835cc
KS
8798 if (name && !strcmp (name, "this"))
8799 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8800 }
8801 }
3ce3b1ba
PA
8802 arg_type = die_type (child_die, cu);
8803
8804 /* RealView does not mark THIS as const, which the testsuite
8805 expects. GCC marks THIS as const in method definitions,
8806 but not in the class specifications (GCC PR 43053). */
8807 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8808 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8809 {
8810 int is_this = 0;
8811 struct dwarf2_cu *arg_cu = cu;
8812 const char *name = dwarf2_name (child_die, cu);
8813
8814 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8815 if (attr)
8816 {
8817 /* If the compiler emits this, use it. */
8818 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8819 is_this = 1;
8820 }
8821 else if (name && strcmp (name, "this") == 0)
8822 /* Function definitions will have the argument names. */
8823 is_this = 1;
8824 else if (name == NULL && iparams == 0)
8825 /* Declarations may not have the names, so like
8826 elsewhere in GDB, assume an artificial first
8827 argument is "this". */
8828 is_this = 1;
8829
8830 if (is_this)
8831 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8832 arg_type, 0);
8833 }
8834
8835 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8836 iparams++;
8837 }
8838 child_die = sibling_die (child_die);
8839 }
8840 }
8841
76c10ea2 8842 return ftype;
c906108c
SS
8843}
8844
f792889a 8845static struct type *
e7c27a73 8846read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8847{
e7c27a73 8848 struct objfile *objfile = cu->objfile;
0114d602 8849 const char *name = NULL;
f792889a 8850 struct type *this_type;
c906108c 8851
94af9270 8852 name = dwarf2_full_name (NULL, die, cu);
f792889a 8853 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8854 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8855 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8856 set_die_type (die, this_type, cu);
8857 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8858 return this_type;
c906108c
SS
8859}
8860
8861/* Find a representation of a given base type and install
8862 it in the TYPE field of the die. */
8863
f792889a 8864static struct type *
e7c27a73 8865read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8866{
e7c27a73 8867 struct objfile *objfile = cu->objfile;
c906108c
SS
8868 struct type *type;
8869 struct attribute *attr;
8870 int encoding = 0, size = 0;
39cbfefa 8871 char *name;
6ccb9162
UW
8872 enum type_code code = TYPE_CODE_INT;
8873 int type_flags = 0;
8874 struct type *target_type = NULL;
c906108c 8875
e142c38c 8876 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8877 if (attr)
8878 {
8879 encoding = DW_UNSND (attr);
8880 }
e142c38c 8881 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8882 if (attr)
8883 {
8884 size = DW_UNSND (attr);
8885 }
39cbfefa 8886 name = dwarf2_name (die, cu);
6ccb9162 8887 if (!name)
c906108c 8888 {
6ccb9162
UW
8889 complaint (&symfile_complaints,
8890 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8891 }
6ccb9162
UW
8892
8893 switch (encoding)
c906108c 8894 {
6ccb9162
UW
8895 case DW_ATE_address:
8896 /* Turn DW_ATE_address into a void * pointer. */
8897 code = TYPE_CODE_PTR;
8898 type_flags |= TYPE_FLAG_UNSIGNED;
8899 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8900 break;
8901 case DW_ATE_boolean:
8902 code = TYPE_CODE_BOOL;
8903 type_flags |= TYPE_FLAG_UNSIGNED;
8904 break;
8905 case DW_ATE_complex_float:
8906 code = TYPE_CODE_COMPLEX;
8907 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8908 break;
8909 case DW_ATE_decimal_float:
8910 code = TYPE_CODE_DECFLOAT;
8911 break;
8912 case DW_ATE_float:
8913 code = TYPE_CODE_FLT;
8914 break;
8915 case DW_ATE_signed:
8916 break;
8917 case DW_ATE_unsigned:
8918 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8919 if (cu->language == language_fortran
8920 && name
8921 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8922 code = TYPE_CODE_CHAR;
6ccb9162
UW
8923 break;
8924 case DW_ATE_signed_char:
6e70227d 8925 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8926 || cu->language == language_pascal
8927 || cu->language == language_fortran)
6ccb9162
UW
8928 code = TYPE_CODE_CHAR;
8929 break;
8930 case DW_ATE_unsigned_char:
868a0084 8931 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8932 || cu->language == language_pascal
8933 || cu->language == language_fortran)
6ccb9162
UW
8934 code = TYPE_CODE_CHAR;
8935 type_flags |= TYPE_FLAG_UNSIGNED;
8936 break;
75079b2b
TT
8937 case DW_ATE_UTF:
8938 /* We just treat this as an integer and then recognize the
8939 type by name elsewhere. */
8940 break;
8941
6ccb9162
UW
8942 default:
8943 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8944 dwarf_type_encoding_name (encoding));
8945 break;
c906108c 8946 }
6ccb9162 8947
0114d602
DJ
8948 type = init_type (code, size, type_flags, NULL, objfile);
8949 TYPE_NAME (type) = name;
6ccb9162
UW
8950 TYPE_TARGET_TYPE (type) = target_type;
8951
0114d602 8952 if (name && strcmp (name, "char") == 0)
876cecd0 8953 TYPE_NOSIGN (type) = 1;
0114d602 8954
f792889a 8955 return set_die_type (die, type, cu);
c906108c
SS
8956}
8957
a02abb62
JB
8958/* Read the given DW_AT_subrange DIE. */
8959
f792889a 8960static struct type *
a02abb62
JB
8961read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8962{
8963 struct type *base_type;
8964 struct type *range_type;
8965 struct attribute *attr;
43bbcdc2
PH
8966 LONGEST low = 0;
8967 LONGEST high = -1;
39cbfefa 8968 char *name;
43bbcdc2 8969 LONGEST negative_mask;
e77813c8 8970
a02abb62 8971 base_type = die_type (die, cu);
953ac07e
JK
8972 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8973 check_typedef (base_type);
a02abb62 8974
7e314c57
JK
8975 /* The die_type call above may have already set the type for this DIE. */
8976 range_type = get_die_type (die, cu);
8977 if (range_type)
8978 return range_type;
8979
e142c38c 8980 if (cu->language == language_fortran)
6e70227d 8981 {
a02abb62
JB
8982 /* FORTRAN implies a lower bound of 1, if not given. */
8983 low = 1;
8984 }
8985
dd5e6932
DJ
8986 /* FIXME: For variable sized arrays either of these could be
8987 a variable rather than a constant value. We'll allow it,
8988 but we don't know how to handle it. */
e142c38c 8989 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8990 if (attr)
8991 low = dwarf2_get_attr_constant_value (attr, 0);
8992
e142c38c 8993 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8994 if (attr)
6e70227d 8995 {
d48323d8 8996 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8997 {
8998 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8999 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9000 FIXME: GDB does not yet know how to handle dynamic
9001 arrays properly, treat them as arrays with unspecified
9002 length for now.
9003
9004 FIXME: jimb/2003-09-22: GDB does not really know
9005 how to handle arrays of unspecified length
9006 either; we just represent them as zero-length
9007 arrays. Choose an appropriate upper bound given
9008 the lower bound we've computed above. */
9009 high = low - 1;
9010 }
9011 else
9012 high = dwarf2_get_attr_constant_value (attr, 1);
9013 }
e77813c8
PM
9014 else
9015 {
9016 attr = dwarf2_attr (die, DW_AT_count, cu);
9017 if (attr)
9018 {
9019 int count = dwarf2_get_attr_constant_value (attr, 1);
9020 high = low + count - 1;
9021 }
c2ff108b
JK
9022 else
9023 {
9024 /* Unspecified array length. */
9025 high = low - 1;
9026 }
e77813c8
PM
9027 }
9028
9029 /* Dwarf-2 specifications explicitly allows to create subrange types
9030 without specifying a base type.
9031 In that case, the base type must be set to the type of
9032 the lower bound, upper bound or count, in that order, if any of these
9033 three attributes references an object that has a type.
9034 If no base type is found, the Dwarf-2 specifications say that
9035 a signed integer type of size equal to the size of an address should
9036 be used.
9037 For the following C code: `extern char gdb_int [];'
9038 GCC produces an empty range DIE.
9039 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9040 high bound or count are not yet handled by this code. */
e77813c8
PM
9041 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9042 {
9043 struct objfile *objfile = cu->objfile;
9044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9045 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9046 struct type *int_type = objfile_type (objfile)->builtin_int;
9047
9048 /* Test "int", "long int", and "long long int" objfile types,
9049 and select the first one having a size above or equal to the
9050 architecture address size. */
9051 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9052 base_type = int_type;
9053 else
9054 {
9055 int_type = objfile_type (objfile)->builtin_long;
9056 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9057 base_type = int_type;
9058 else
9059 {
9060 int_type = objfile_type (objfile)->builtin_long_long;
9061 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9062 base_type = int_type;
9063 }
9064 }
9065 }
a02abb62 9066
6e70227d 9067 negative_mask =
43bbcdc2
PH
9068 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9069 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9070 low |= negative_mask;
9071 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9072 high |= negative_mask;
9073
a02abb62
JB
9074 range_type = create_range_type (NULL, base_type, low, high);
9075
bbb0eef6
JK
9076 /* Mark arrays with dynamic length at least as an array of unspecified
9077 length. GDB could check the boundary but before it gets implemented at
9078 least allow accessing the array elements. */
d48323d8 9079 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9080 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9081
c2ff108b
JK
9082 /* Ada expects an empty array on no boundary attributes. */
9083 if (attr == NULL && cu->language != language_ada)
9084 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9085
39cbfefa
DJ
9086 name = dwarf2_name (die, cu);
9087 if (name)
9088 TYPE_NAME (range_type) = name;
6e70227d 9089
e142c38c 9090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9091 if (attr)
9092 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9093
7e314c57
JK
9094 set_die_type (die, range_type, cu);
9095
9096 /* set_die_type should be already done. */
b4ba55a1
JB
9097 set_descriptive_type (range_type, die, cu);
9098
7e314c57 9099 return range_type;
a02abb62 9100}
6e70227d 9101
f792889a 9102static struct type *
81a17f79
JB
9103read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9104{
9105 struct type *type;
81a17f79 9106
81a17f79
JB
9107 /* For now, we only support the C meaning of an unspecified type: void. */
9108
0114d602
DJ
9109 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9110 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9111
f792889a 9112 return set_die_type (die, type, cu);
81a17f79 9113}
a02abb62 9114
51545339
DJ
9115/* Trivial hash function for die_info: the hash value of a DIE
9116 is its offset in .debug_info for this objfile. */
9117
9118static hashval_t
9119die_hash (const void *item)
9120{
9121 const struct die_info *die = item;
9a619af0 9122
51545339
DJ
9123 return die->offset;
9124}
9125
9126/* Trivial comparison function for die_info structures: two DIEs
9127 are equal if they have the same offset. */
9128
9129static int
9130die_eq (const void *item_lhs, const void *item_rhs)
9131{
9132 const struct die_info *die_lhs = item_lhs;
9133 const struct die_info *die_rhs = item_rhs;
9a619af0 9134
51545339
DJ
9135 return die_lhs->offset == die_rhs->offset;
9136}
9137
c906108c
SS
9138/* Read a whole compilation unit into a linked list of dies. */
9139
f9aca02d 9140static struct die_info *
93311388 9141read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9142{
93311388 9143 struct die_reader_specs reader_specs;
98bfdba5 9144 int read_abbrevs = 0;
1d9ec526 9145 struct cleanup *back_to = NULL;
98bfdba5
PA
9146 struct die_info *die;
9147
9148 if (cu->dwarf2_abbrevs == NULL)
9149 {
9150 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9151 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9152 read_abbrevs = 1;
9153 }
93311388 9154
348e048f 9155 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9156 cu->die_hash
9157 = htab_create_alloc_ex (cu->header.length / 12,
9158 die_hash,
9159 die_eq,
9160 NULL,
9161 &cu->comp_unit_obstack,
9162 hashtab_obstack_allocate,
9163 dummy_obstack_deallocate);
9164
93311388
DE
9165 init_cu_die_reader (&reader_specs, cu);
9166
98bfdba5
PA
9167 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9168
9169 if (read_abbrevs)
9170 do_cleanups (back_to);
9171
9172 return die;
639d11d3
DC
9173}
9174
d97bc12b
DE
9175/* Main entry point for reading a DIE and all children.
9176 Read the DIE and dump it if requested. */
9177
9178static struct die_info *
93311388
DE
9179read_die_and_children (const struct die_reader_specs *reader,
9180 gdb_byte *info_ptr,
d97bc12b
DE
9181 gdb_byte **new_info_ptr,
9182 struct die_info *parent)
9183{
93311388 9184 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9185 new_info_ptr, parent);
9186
9187 if (dwarf2_die_debug)
9188 {
348e048f
DE
9189 fprintf_unfiltered (gdb_stdlog,
9190 "\nRead die from %s of %s:\n",
8b70b953
TT
9191 (reader->cu->per_cu->debug_type_section
9192 ? ".debug_types"
9193 : ".debug_info"),
348e048f 9194 reader->abfd->filename);
d97bc12b
DE
9195 dump_die (result, dwarf2_die_debug);
9196 }
9197
9198 return result;
9199}
9200
639d11d3
DC
9201/* Read a single die and all its descendents. Set the die's sibling
9202 field to NULL; set other fields in the die correctly, and set all
9203 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9204 location of the info_ptr after reading all of those dies. PARENT
9205 is the parent of the die in question. */
9206
9207static struct die_info *
93311388
DE
9208read_die_and_children_1 (const struct die_reader_specs *reader,
9209 gdb_byte *info_ptr,
d97bc12b
DE
9210 gdb_byte **new_info_ptr,
9211 struct die_info *parent)
639d11d3
DC
9212{
9213 struct die_info *die;
fe1b8b76 9214 gdb_byte *cur_ptr;
639d11d3
DC
9215 int has_children;
9216
93311388 9217 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9218 if (die == NULL)
9219 {
9220 *new_info_ptr = cur_ptr;
9221 return NULL;
9222 }
93311388 9223 store_in_ref_table (die, reader->cu);
639d11d3
DC
9224
9225 if (has_children)
348e048f 9226 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9227 else
9228 {
9229 die->child = NULL;
9230 *new_info_ptr = cur_ptr;
9231 }
9232
9233 die->sibling = NULL;
9234 die->parent = parent;
9235 return die;
9236}
9237
9238/* Read a die, all of its descendents, and all of its siblings; set
9239 all of the fields of all of the dies correctly. Arguments are as
9240 in read_die_and_children. */
9241
9242static struct die_info *
93311388
DE
9243read_die_and_siblings (const struct die_reader_specs *reader,
9244 gdb_byte *info_ptr,
fe1b8b76 9245 gdb_byte **new_info_ptr,
639d11d3
DC
9246 struct die_info *parent)
9247{
9248 struct die_info *first_die, *last_sibling;
fe1b8b76 9249 gdb_byte *cur_ptr;
639d11d3 9250
c906108c 9251 cur_ptr = info_ptr;
639d11d3
DC
9252 first_die = last_sibling = NULL;
9253
9254 while (1)
c906108c 9255 {
639d11d3 9256 struct die_info *die
93311388 9257 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9258
1d325ec1 9259 if (die == NULL)
c906108c 9260 {
639d11d3
DC
9261 *new_info_ptr = cur_ptr;
9262 return first_die;
c906108c 9263 }
1d325ec1
DJ
9264
9265 if (!first_die)
9266 first_die = die;
c906108c 9267 else
1d325ec1
DJ
9268 last_sibling->sibling = die;
9269
9270 last_sibling = die;
c906108c 9271 }
c906108c
SS
9272}
9273
93311388
DE
9274/* Read the die from the .debug_info section buffer. Set DIEP to
9275 point to a newly allocated die with its information, except for its
9276 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9277 whether the die has children or not. */
9278
9279static gdb_byte *
9280read_full_die (const struct die_reader_specs *reader,
9281 struct die_info **diep, gdb_byte *info_ptr,
9282 int *has_children)
9283{
9284 unsigned int abbrev_number, bytes_read, i, offset;
9285 struct abbrev_info *abbrev;
9286 struct die_info *die;
9287 struct dwarf2_cu *cu = reader->cu;
9288 bfd *abfd = reader->abfd;
9289
9290 offset = info_ptr - reader->buffer;
9291 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9292 info_ptr += bytes_read;
9293 if (!abbrev_number)
9294 {
9295 *diep = NULL;
9296 *has_children = 0;
9297 return info_ptr;
9298 }
9299
9300 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9301 if (!abbrev)
348e048f
DE
9302 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9303 abbrev_number,
9304 bfd_get_filename (abfd));
9305
93311388
DE
9306 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9307 die->offset = offset;
9308 die->tag = abbrev->tag;
9309 die->abbrev = abbrev_number;
9310
9311 die->num_attrs = abbrev->num_attrs;
9312
9313 for (i = 0; i < abbrev->num_attrs; ++i)
9314 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9315 abfd, info_ptr, cu);
9316
9317 *diep = die;
9318 *has_children = abbrev->has_children;
9319 return info_ptr;
9320}
9321
c906108c
SS
9322/* In DWARF version 2, the description of the debugging information is
9323 stored in a separate .debug_abbrev section. Before we read any
9324 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9325 in a hash table. This function also sets flags in CU describing
9326 the data found in the abbrev table. */
c906108c
SS
9327
9328static void
e7c27a73 9329dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9330{
e7c27a73 9331 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9332 gdb_byte *abbrev_ptr;
c906108c
SS
9333 struct abbrev_info *cur_abbrev;
9334 unsigned int abbrev_number, bytes_read, abbrev_name;
9335 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9336 struct attr_abbrev *cur_attrs;
9337 unsigned int allocated_attrs;
c906108c 9338
0963b4bd 9339 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9340 obstack_init (&cu->abbrev_obstack);
9341 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9342 (ABBREV_HASH_SIZE
9343 * sizeof (struct abbrev_info *)));
9344 memset (cu->dwarf2_abbrevs, 0,
9345 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9346
be391dca
TT
9347 dwarf2_read_section (dwarf2_per_objfile->objfile,
9348 &dwarf2_per_objfile->abbrev);
dce234bc 9349 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9350 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9351 abbrev_ptr += bytes_read;
9352
f3dd6933
DJ
9353 allocated_attrs = ATTR_ALLOC_CHUNK;
9354 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9355
0963b4bd 9356 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9357 while (abbrev_number)
9358 {
f3dd6933 9359 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9360
9361 /* read in abbrev header */
9362 cur_abbrev->number = abbrev_number;
9363 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9364 abbrev_ptr += bytes_read;
9365 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9366 abbrev_ptr += 1;
9367
72bf9492
DJ
9368 if (cur_abbrev->tag == DW_TAG_namespace)
9369 cu->has_namespace_info = 1;
9370
c906108c
SS
9371 /* now read in declarations */
9372 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9373 abbrev_ptr += bytes_read;
9374 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9375 abbrev_ptr += bytes_read;
9376 while (abbrev_name)
9377 {
f3dd6933 9378 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9379 {
f3dd6933
DJ
9380 allocated_attrs += ATTR_ALLOC_CHUNK;
9381 cur_attrs
9382 = xrealloc (cur_attrs, (allocated_attrs
9383 * sizeof (struct attr_abbrev)));
c906108c 9384 }
ae038cb0
DJ
9385
9386 /* Record whether this compilation unit might have
9387 inter-compilation-unit references. If we don't know what form
9388 this attribute will have, then it might potentially be a
9389 DW_FORM_ref_addr, so we conservatively expect inter-CU
9390 references. */
9391
9392 if (abbrev_form == DW_FORM_ref_addr
9393 || abbrev_form == DW_FORM_indirect)
9394 cu->has_form_ref_addr = 1;
9395
f3dd6933
DJ
9396 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9397 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9398 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9399 abbrev_ptr += bytes_read;
9400 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9401 abbrev_ptr += bytes_read;
9402 }
9403
f3dd6933
DJ
9404 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9405 (cur_abbrev->num_attrs
9406 * sizeof (struct attr_abbrev)));
9407 memcpy (cur_abbrev->attrs, cur_attrs,
9408 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9409
c906108c 9410 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9411 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9412 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9413
9414 /* Get next abbreviation.
9415 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9416 always properly terminated with an abbrev number of 0.
9417 Exit loop if we encounter an abbreviation which we have
9418 already read (which means we are about to read the abbreviations
9419 for the next compile unit) or if the end of the abbreviation
9420 table is reached. */
dce234bc
PP
9421 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9422 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9423 break;
9424 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9425 abbrev_ptr += bytes_read;
e7c27a73 9426 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9427 break;
9428 }
f3dd6933
DJ
9429
9430 xfree (cur_attrs);
c906108c
SS
9431}
9432
f3dd6933 9433/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9434
c906108c 9435static void
f3dd6933 9436dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9437{
f3dd6933 9438 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9439
f3dd6933
DJ
9440 obstack_free (&cu->abbrev_obstack, NULL);
9441 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9442}
9443
9444/* Lookup an abbrev_info structure in the abbrev hash table. */
9445
9446static struct abbrev_info *
e7c27a73 9447dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9448{
9449 unsigned int hash_number;
9450 struct abbrev_info *abbrev;
9451
9452 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9453 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9454
9455 while (abbrev)
9456 {
9457 if (abbrev->number == number)
9458 return abbrev;
9459 else
9460 abbrev = abbrev->next;
9461 }
9462 return NULL;
9463}
9464
72bf9492
DJ
9465/* Returns nonzero if TAG represents a type that we might generate a partial
9466 symbol for. */
9467
9468static int
9469is_type_tag_for_partial (int tag)
9470{
9471 switch (tag)
9472 {
9473#if 0
9474 /* Some types that would be reasonable to generate partial symbols for,
9475 that we don't at present. */
9476 case DW_TAG_array_type:
9477 case DW_TAG_file_type:
9478 case DW_TAG_ptr_to_member_type:
9479 case DW_TAG_set_type:
9480 case DW_TAG_string_type:
9481 case DW_TAG_subroutine_type:
9482#endif
9483 case DW_TAG_base_type:
9484 case DW_TAG_class_type:
680b30c7 9485 case DW_TAG_interface_type:
72bf9492
DJ
9486 case DW_TAG_enumeration_type:
9487 case DW_TAG_structure_type:
9488 case DW_TAG_subrange_type:
9489 case DW_TAG_typedef:
9490 case DW_TAG_union_type:
9491 return 1;
9492 default:
9493 return 0;
9494 }
9495}
9496
9497/* Load all DIEs that are interesting for partial symbols into memory. */
9498
9499static struct partial_die_info *
93311388
DE
9500load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9501 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9502{
9503 struct partial_die_info *part_die;
9504 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9505 struct abbrev_info *abbrev;
9506 unsigned int bytes_read;
5afb4e99 9507 unsigned int load_all = 0;
72bf9492
DJ
9508
9509 int nesting_level = 1;
9510
9511 parent_die = NULL;
9512 last_die = NULL;
9513
5afb4e99
DJ
9514 if (cu->per_cu && cu->per_cu->load_all_dies)
9515 load_all = 1;
9516
72bf9492
DJ
9517 cu->partial_dies
9518 = htab_create_alloc_ex (cu->header.length / 12,
9519 partial_die_hash,
9520 partial_die_eq,
9521 NULL,
9522 &cu->comp_unit_obstack,
9523 hashtab_obstack_allocate,
9524 dummy_obstack_deallocate);
9525
9526 part_die = obstack_alloc (&cu->comp_unit_obstack,
9527 sizeof (struct partial_die_info));
9528
9529 while (1)
9530 {
9531 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9532
9533 /* A NULL abbrev means the end of a series of children. */
9534 if (abbrev == NULL)
9535 {
9536 if (--nesting_level == 0)
9537 {
9538 /* PART_DIE was probably the last thing allocated on the
9539 comp_unit_obstack, so we could call obstack_free
9540 here. We don't do that because the waste is small,
9541 and will be cleaned up when we're done with this
9542 compilation unit. This way, we're also more robust
9543 against other users of the comp_unit_obstack. */
9544 return first_die;
9545 }
9546 info_ptr += bytes_read;
9547 last_die = parent_die;
9548 parent_die = parent_die->die_parent;
9549 continue;
9550 }
9551
98bfdba5
PA
9552 /* Check for template arguments. We never save these; if
9553 they're seen, we just mark the parent, and go on our way. */
9554 if (parent_die != NULL
9555 && cu->language == language_cplus
9556 && (abbrev->tag == DW_TAG_template_type_param
9557 || abbrev->tag == DW_TAG_template_value_param))
9558 {
9559 parent_die->has_template_arguments = 1;
9560
9561 if (!load_all)
9562 {
9563 /* We don't need a partial DIE for the template argument. */
9564 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9565 cu);
9566 continue;
9567 }
9568 }
9569
9570 /* We only recurse into subprograms looking for template arguments.
9571 Skip their other children. */
9572 if (!load_all
9573 && cu->language == language_cplus
9574 && parent_die != NULL
9575 && parent_die->tag == DW_TAG_subprogram)
9576 {
9577 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9578 continue;
9579 }
9580
5afb4e99
DJ
9581 /* Check whether this DIE is interesting enough to save. Normally
9582 we would not be interested in members here, but there may be
9583 later variables referencing them via DW_AT_specification (for
9584 static members). */
9585 if (!load_all
9586 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9587 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9588 && abbrev->tag != DW_TAG_enumerator
9589 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9590 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9591 && abbrev->tag != DW_TAG_variable
5afb4e99 9592 && abbrev->tag != DW_TAG_namespace
f55ee35c 9593 && abbrev->tag != DW_TAG_module
5afb4e99 9594 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9595 {
9596 /* Otherwise we skip to the next sibling, if any. */
93311388 9597 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9598 continue;
9599 }
9600
93311388
DE
9601 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9602 buffer, info_ptr, cu);
72bf9492
DJ
9603
9604 /* This two-pass algorithm for processing partial symbols has a
9605 high cost in cache pressure. Thus, handle some simple cases
9606 here which cover the majority of C partial symbols. DIEs
9607 which neither have specification tags in them, nor could have
9608 specification tags elsewhere pointing at them, can simply be
9609 processed and discarded.
9610
9611 This segment is also optional; scan_partial_symbols and
9612 add_partial_symbol will handle these DIEs if we chain
9613 them in normally. When compilers which do not emit large
9614 quantities of duplicate debug information are more common,
9615 this code can probably be removed. */
9616
9617 /* Any complete simple types at the top level (pretty much all
9618 of them, for a language without namespaces), can be processed
9619 directly. */
9620 if (parent_die == NULL
9621 && part_die->has_specification == 0
9622 && part_die->is_declaration == 0
d8228535 9623 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9624 || part_die->tag == DW_TAG_base_type
9625 || part_die->tag == DW_TAG_subrange_type))
9626 {
9627 if (building_psymtab && part_die->name != NULL)
04a679b8 9628 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9629 VAR_DOMAIN, LOC_TYPEDEF,
9630 &cu->objfile->static_psymbols,
9631 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9632 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9633 continue;
9634 }
9635
d8228535
JK
9636 /* The exception for DW_TAG_typedef with has_children above is
9637 a workaround of GCC PR debug/47510. In the case of this complaint
9638 type_name_no_tag_or_error will error on such types later.
9639
9640 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9641 it could not find the child DIEs referenced later, this is checked
9642 above. In correct DWARF DW_TAG_typedef should have no children. */
9643
9644 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9645 complaint (&symfile_complaints,
9646 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9647 "- DIE at 0x%x [in module %s]"),
9648 part_die->offset, cu->objfile->name);
9649
72bf9492
DJ
9650 /* If we're at the second level, and we're an enumerator, and
9651 our parent has no specification (meaning possibly lives in a
9652 namespace elsewhere), then we can add the partial symbol now
9653 instead of queueing it. */
9654 if (part_die->tag == DW_TAG_enumerator
9655 && parent_die != NULL
9656 && parent_die->die_parent == NULL
9657 && parent_die->tag == DW_TAG_enumeration_type
9658 && parent_die->has_specification == 0)
9659 {
9660 if (part_die->name == NULL)
3e43a32a
MS
9661 complaint (&symfile_complaints,
9662 _("malformed enumerator DIE ignored"));
72bf9492 9663 else if (building_psymtab)
04a679b8 9664 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9665 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9666 (cu->language == language_cplus
9667 || cu->language == language_java)
72bf9492
DJ
9668 ? &cu->objfile->global_psymbols
9669 : &cu->objfile->static_psymbols,
9670 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9671
93311388 9672 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9673 continue;
9674 }
9675
9676 /* We'll save this DIE so link it in. */
9677 part_die->die_parent = parent_die;
9678 part_die->die_sibling = NULL;
9679 part_die->die_child = NULL;
9680
9681 if (last_die && last_die == parent_die)
9682 last_die->die_child = part_die;
9683 else if (last_die)
9684 last_die->die_sibling = part_die;
9685
9686 last_die = part_die;
9687
9688 if (first_die == NULL)
9689 first_die = part_die;
9690
9691 /* Maybe add the DIE to the hash table. Not all DIEs that we
9692 find interesting need to be in the hash table, because we
9693 also have the parent/sibling/child chains; only those that we
9694 might refer to by offset later during partial symbol reading.
9695
9696 For now this means things that might have be the target of a
9697 DW_AT_specification, DW_AT_abstract_origin, or
9698 DW_AT_extension. DW_AT_extension will refer only to
9699 namespaces; DW_AT_abstract_origin refers to functions (and
9700 many things under the function DIE, but we do not recurse
9701 into function DIEs during partial symbol reading) and
9702 possibly variables as well; DW_AT_specification refers to
9703 declarations. Declarations ought to have the DW_AT_declaration
9704 flag. It happens that GCC forgets to put it in sometimes, but
9705 only for functions, not for types.
9706
9707 Adding more things than necessary to the hash table is harmless
9708 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9709 wasted time in find_partial_die, when we reread the compilation
9710 unit with load_all_dies set. */
72bf9492 9711
5afb4e99 9712 if (load_all
72929c62 9713 || abbrev->tag == DW_TAG_constant
5afb4e99 9714 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9715 || abbrev->tag == DW_TAG_variable
9716 || abbrev->tag == DW_TAG_namespace
9717 || part_die->is_declaration)
9718 {
9719 void **slot;
9720
9721 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9722 part_die->offset, INSERT);
9723 *slot = part_die;
9724 }
9725
9726 part_die = obstack_alloc (&cu->comp_unit_obstack,
9727 sizeof (struct partial_die_info));
9728
9729 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9730 we have no reason to follow the children of structures; for other
98bfdba5
PA
9731 languages we have to, so that we can get at method physnames
9732 to infer fully qualified class names, for DW_AT_specification,
9733 and for C++ template arguments. For C++, we also look one level
9734 inside functions to find template arguments (if the name of the
9735 function does not already contain the template arguments).
bc30ff58
JB
9736
9737 For Ada, we need to scan the children of subprograms and lexical
9738 blocks as well because Ada allows the definition of nested
9739 entities that could be interesting for the debugger, such as
9740 nested subprograms for instance. */
72bf9492 9741 if (last_die->has_children
5afb4e99
DJ
9742 && (load_all
9743 || last_die->tag == DW_TAG_namespace
f55ee35c 9744 || last_die->tag == DW_TAG_module
72bf9492 9745 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9746 || (cu->language == language_cplus
9747 && last_die->tag == DW_TAG_subprogram
9748 && (last_die->name == NULL
9749 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9750 || (cu->language != language_c
9751 && (last_die->tag == DW_TAG_class_type
680b30c7 9752 || last_die->tag == DW_TAG_interface_type
72bf9492 9753 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9754 || last_die->tag == DW_TAG_union_type))
9755 || (cu->language == language_ada
9756 && (last_die->tag == DW_TAG_subprogram
9757 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9758 {
9759 nesting_level++;
9760 parent_die = last_die;
9761 continue;
9762 }
9763
9764 /* Otherwise we skip to the next sibling, if any. */
93311388 9765 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9766
9767 /* Back to the top, do it again. */
9768 }
9769}
9770
c906108c
SS
9771/* Read a minimal amount of information into the minimal die structure. */
9772
fe1b8b76 9773static gdb_byte *
72bf9492
DJ
9774read_partial_die (struct partial_die_info *part_die,
9775 struct abbrev_info *abbrev,
9776 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9777 gdb_byte *buffer, gdb_byte *info_ptr,
9778 struct dwarf2_cu *cu)
c906108c 9779{
fa238c03 9780 unsigned int i;
c906108c 9781 struct attribute attr;
c5aa993b 9782 int has_low_pc_attr = 0;
c906108c
SS
9783 int has_high_pc_attr = 0;
9784
72bf9492 9785 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9786
93311388 9787 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9788
9789 info_ptr += abbrev_len;
9790
9791 if (abbrev == NULL)
9792 return info_ptr;
9793
c906108c
SS
9794 part_die->tag = abbrev->tag;
9795 part_die->has_children = abbrev->has_children;
c906108c
SS
9796
9797 for (i = 0; i < abbrev->num_attrs; ++i)
9798 {
e7c27a73 9799 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9800
9801 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9802 partial symbol table. */
c906108c
SS
9803 switch (attr.name)
9804 {
9805 case DW_AT_name:
71c25dea
TT
9806 switch (part_die->tag)
9807 {
9808 case DW_TAG_compile_unit:
348e048f 9809 case DW_TAG_type_unit:
71c25dea
TT
9810 /* Compilation units have a DW_AT_name that is a filename, not
9811 a source language identifier. */
9812 case DW_TAG_enumeration_type:
9813 case DW_TAG_enumerator:
9814 /* These tags always have simple identifiers already; no need
9815 to canonicalize them. */
9816 part_die->name = DW_STRING (&attr);
9817 break;
9818 default:
9819 part_die->name
9820 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9821 &cu->objfile->objfile_obstack);
71c25dea
TT
9822 break;
9823 }
c906108c 9824 break;
31ef98ae 9825 case DW_AT_linkage_name:
c906108c 9826 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9827 /* Note that both forms of linkage name might appear. We
9828 assume they will be the same, and we only store the last
9829 one we see. */
94af9270
KS
9830 if (cu->language == language_ada)
9831 part_die->name = DW_STRING (&attr);
abc72ce4 9832 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9833 break;
9834 case DW_AT_low_pc:
9835 has_low_pc_attr = 1;
9836 part_die->lowpc = DW_ADDR (&attr);
9837 break;
9838 case DW_AT_high_pc:
9839 has_high_pc_attr = 1;
9840 part_die->highpc = DW_ADDR (&attr);
9841 break;
9842 case DW_AT_location:
0963b4bd 9843 /* Support the .debug_loc offsets. */
8e19ed76
PS
9844 if (attr_form_is_block (&attr))
9845 {
9846 part_die->locdesc = DW_BLOCK (&attr);
9847 }
3690dd37 9848 else if (attr_form_is_section_offset (&attr))
8e19ed76 9849 {
4d3c2250 9850 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9851 }
9852 else
9853 {
4d3c2250
KB
9854 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9855 "partial symbol information");
8e19ed76 9856 }
c906108c 9857 break;
c906108c
SS
9858 case DW_AT_external:
9859 part_die->is_external = DW_UNSND (&attr);
9860 break;
9861 case DW_AT_declaration:
9862 part_die->is_declaration = DW_UNSND (&attr);
9863 break;
9864 case DW_AT_type:
9865 part_die->has_type = 1;
9866 break;
9867 case DW_AT_abstract_origin:
9868 case DW_AT_specification:
72bf9492
DJ
9869 case DW_AT_extension:
9870 part_die->has_specification = 1;
c764a876 9871 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9872 break;
9873 case DW_AT_sibling:
9874 /* Ignore absolute siblings, they might point outside of
9875 the current compile unit. */
9876 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9877 complaint (&symfile_complaints,
9878 _("ignoring absolute DW_AT_sibling"));
c906108c 9879 else
93311388 9880 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9881 break;
fa4028e9
JB
9882 case DW_AT_byte_size:
9883 part_die->has_byte_size = 1;
9884 break;
68511cec
CES
9885 case DW_AT_calling_convention:
9886 /* DWARF doesn't provide a way to identify a program's source-level
9887 entry point. DW_AT_calling_convention attributes are only meant
9888 to describe functions' calling conventions.
9889
9890 However, because it's a necessary piece of information in
9891 Fortran, and because DW_CC_program is the only piece of debugging
9892 information whose definition refers to a 'main program' at all,
9893 several compilers have begun marking Fortran main programs with
9894 DW_CC_program --- even when those functions use the standard
9895 calling conventions.
9896
9897 So until DWARF specifies a way to provide this information and
9898 compilers pick up the new representation, we'll support this
9899 practice. */
9900 if (DW_UNSND (&attr) == DW_CC_program
9901 && cu->language == language_fortran)
01f8c46d
JK
9902 {
9903 set_main_name (part_die->name);
9904
9905 /* As this DIE has a static linkage the name would be difficult
9906 to look up later. */
9907 language_of_main = language_fortran;
9908 }
68511cec 9909 break;
c906108c
SS
9910 default:
9911 break;
9912 }
9913 }
9914
9373cf26
JK
9915 if (has_low_pc_attr && has_high_pc_attr)
9916 {
9917 /* When using the GNU linker, .gnu.linkonce. sections are used to
9918 eliminate duplicate copies of functions and vtables and such.
9919 The linker will arbitrarily choose one and discard the others.
9920 The AT_*_pc values for such functions refer to local labels in
9921 these sections. If the section from that file was discarded, the
9922 labels are not in the output, so the relocs get a value of 0.
9923 If this is a discarded function, mark the pc bounds as invalid,
9924 so that GDB will ignore it. */
9925 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9926 {
9927 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9928
9929 complaint (&symfile_complaints,
9930 _("DW_AT_low_pc %s is zero "
9931 "for DIE at 0x%x [in module %s]"),
9932 paddress (gdbarch, part_die->lowpc),
9933 part_die->offset, cu->objfile->name);
9934 }
9935 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9936 else if (part_die->lowpc >= part_die->highpc)
9937 {
9938 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9939
9940 complaint (&symfile_complaints,
9941 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9942 "for DIE at 0x%x [in module %s]"),
9943 paddress (gdbarch, part_die->lowpc),
9944 paddress (gdbarch, part_die->highpc),
9945 part_die->offset, cu->objfile->name);
9946 }
9947 else
9948 part_die->has_pc_info = 1;
9949 }
85cbf3d3 9950
c906108c
SS
9951 return info_ptr;
9952}
9953
72bf9492
DJ
9954/* Find a cached partial DIE at OFFSET in CU. */
9955
9956static struct partial_die_info *
c764a876 9957find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9958{
9959 struct partial_die_info *lookup_die = NULL;
9960 struct partial_die_info part_die;
9961
9962 part_die.offset = offset;
9963 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9964
72bf9492
DJ
9965 return lookup_die;
9966}
9967
348e048f
DE
9968/* Find a partial DIE at OFFSET, which may or may not be in CU,
9969 except in the case of .debug_types DIEs which do not reference
9970 outside their CU (they do however referencing other types via
55f1336d 9971 DW_FORM_ref_sig8). */
72bf9492
DJ
9972
9973static struct partial_die_info *
c764a876 9974find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9975{
5afb4e99
DJ
9976 struct dwarf2_per_cu_data *per_cu = NULL;
9977 struct partial_die_info *pd = NULL;
72bf9492 9978
8b70b953 9979 if (cu->per_cu->debug_type_section)
348e048f
DE
9980 {
9981 pd = find_partial_die_in_comp_unit (offset, cu);
9982 if (pd != NULL)
9983 return pd;
9984 goto not_found;
9985 }
9986
45452591 9987 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9988 {
9989 pd = find_partial_die_in_comp_unit (offset, cu);
9990 if (pd != NULL)
9991 return pd;
9992 }
72bf9492 9993
ae038cb0
DJ
9994 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9995
98bfdba5
PA
9996 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9997 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9998
9999 per_cu->cu->last_used = 0;
5afb4e99
DJ
10000 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10001
10002 if (pd == NULL && per_cu->load_all_dies == 0)
10003 {
10004 struct cleanup *back_to;
10005 struct partial_die_info comp_unit_die;
10006 struct abbrev_info *abbrev;
10007 unsigned int bytes_read;
10008 char *info_ptr;
10009
10010 per_cu->load_all_dies = 1;
10011
10012 /* Re-read the DIEs. */
10013 back_to = make_cleanup (null_cleanup, 0);
10014 if (per_cu->cu->dwarf2_abbrevs == NULL)
10015 {
10016 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 10017 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10018 }
dce234bc 10019 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
10020 + per_cu->cu->header.offset
10021 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10022 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10023 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
10024 per_cu->cu->objfile->obfd,
10025 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10026 per_cu->cu);
10027 if (comp_unit_die.has_children)
93311388
DE
10028 load_partial_dies (per_cu->cu->objfile->obfd,
10029 dwarf2_per_objfile->info.buffer, info_ptr,
10030 0, per_cu->cu);
5afb4e99
DJ
10031 do_cleanups (back_to);
10032
10033 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10034 }
10035
348e048f
DE
10036 not_found:
10037
5afb4e99
DJ
10038 if (pd == NULL)
10039 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10040 _("could not find partial DIE 0x%x "
10041 "in cache [from module %s]\n"),
5afb4e99
DJ
10042 offset, bfd_get_filename (cu->objfile->obfd));
10043 return pd;
72bf9492
DJ
10044}
10045
abc72ce4
DE
10046/* See if we can figure out if the class lives in a namespace. We do
10047 this by looking for a member function; its demangled name will
10048 contain namespace info, if there is any. */
10049
10050static void
10051guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10052 struct dwarf2_cu *cu)
10053{
10054 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10055 what template types look like, because the demangler
10056 frequently doesn't give the same name as the debug info. We
10057 could fix this by only using the demangled name to get the
10058 prefix (but see comment in read_structure_type). */
10059
10060 struct partial_die_info *real_pdi;
10061 struct partial_die_info *child_pdi;
10062
10063 /* If this DIE (this DIE's specification, if any) has a parent, then
10064 we should not do this. We'll prepend the parent's fully qualified
10065 name when we create the partial symbol. */
10066
10067 real_pdi = struct_pdi;
10068 while (real_pdi->has_specification)
10069 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10070
10071 if (real_pdi->die_parent != NULL)
10072 return;
10073
10074 for (child_pdi = struct_pdi->die_child;
10075 child_pdi != NULL;
10076 child_pdi = child_pdi->die_sibling)
10077 {
10078 if (child_pdi->tag == DW_TAG_subprogram
10079 && child_pdi->linkage_name != NULL)
10080 {
10081 char *actual_class_name
10082 = language_class_name_from_physname (cu->language_defn,
10083 child_pdi->linkage_name);
10084 if (actual_class_name != NULL)
10085 {
10086 struct_pdi->name
10087 = obsavestring (actual_class_name,
10088 strlen (actual_class_name),
10089 &cu->objfile->objfile_obstack);
10090 xfree (actual_class_name);
10091 }
10092 break;
10093 }
10094 }
10095}
10096
72bf9492
DJ
10097/* Adjust PART_DIE before generating a symbol for it. This function
10098 may set the is_external flag or change the DIE's name. */
10099
10100static void
10101fixup_partial_die (struct partial_die_info *part_die,
10102 struct dwarf2_cu *cu)
10103{
abc72ce4
DE
10104 /* Once we've fixed up a die, there's no point in doing so again.
10105 This also avoids a memory leak if we were to call
10106 guess_partial_die_structure_name multiple times. */
10107 if (part_die->fixup_called)
10108 return;
10109
72bf9492
DJ
10110 /* If we found a reference attribute and the DIE has no name, try
10111 to find a name in the referred to DIE. */
10112
10113 if (part_die->name == NULL && part_die->has_specification)
10114 {
10115 struct partial_die_info *spec_die;
72bf9492 10116
10b3939b 10117 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10118
10b3939b 10119 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10120
10121 if (spec_die->name)
10122 {
10123 part_die->name = spec_die->name;
10124
10125 /* Copy DW_AT_external attribute if it is set. */
10126 if (spec_die->is_external)
10127 part_die->is_external = spec_die->is_external;
10128 }
10129 }
10130
10131 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10132
10133 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10134 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10135
abc72ce4
DE
10136 /* If there is no parent die to provide a namespace, and there are
10137 children, see if we can determine the namespace from their linkage
10138 name.
10139 NOTE: We need to do this even if cu->has_namespace_info != 0.
10140 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10141 if (cu->language == language_cplus
8b70b953 10142 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10143 && part_die->die_parent == NULL
10144 && part_die->has_children
10145 && (part_die->tag == DW_TAG_class_type
10146 || part_die->tag == DW_TAG_structure_type
10147 || part_die->tag == DW_TAG_union_type))
10148 guess_partial_die_structure_name (part_die, cu);
10149
53832f31
TT
10150 /* GCC might emit a nameless struct or union that has a linkage
10151 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10152 if (part_die->name == NULL
96408a79
SA
10153 && (part_die->tag == DW_TAG_class_type
10154 || part_die->tag == DW_TAG_interface_type
10155 || part_die->tag == DW_TAG_structure_type
10156 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10157 && part_die->linkage_name != NULL)
10158 {
10159 char *demangled;
10160
10161 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10162 if (demangled)
10163 {
96408a79
SA
10164 const char *base;
10165
10166 /* Strip any leading namespaces/classes, keep only the base name.
10167 DW_AT_name for named DIEs does not contain the prefixes. */
10168 base = strrchr (demangled, ':');
10169 if (base && base > demangled && base[-1] == ':')
10170 base++;
10171 else
10172 base = demangled;
10173
10174 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10175 &cu->objfile->objfile_obstack);
10176 xfree (demangled);
10177 }
10178 }
10179
abc72ce4 10180 part_die->fixup_called = 1;
72bf9492
DJ
10181}
10182
a8329558 10183/* Read an attribute value described by an attribute form. */
c906108c 10184
fe1b8b76 10185static gdb_byte *
a8329558 10186read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10187 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10188 struct dwarf2_cu *cu)
c906108c 10189{
e7c27a73 10190 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10191 unsigned int bytes_read;
10192 struct dwarf_block *blk;
10193
a8329558
KW
10194 attr->form = form;
10195 switch (form)
c906108c 10196 {
c906108c 10197 case DW_FORM_ref_addr:
ae411497
TT
10198 if (cu->header.version == 2)
10199 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10200 else
3e43a32a
MS
10201 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10202 &cu->header, &bytes_read);
ae411497
TT
10203 info_ptr += bytes_read;
10204 break;
10205 case DW_FORM_addr:
e7c27a73 10206 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10207 info_ptr += bytes_read;
c906108c
SS
10208 break;
10209 case DW_FORM_block2:
7b5a2f43 10210 blk = dwarf_alloc_block (cu);
c906108c
SS
10211 blk->size = read_2_bytes (abfd, info_ptr);
10212 info_ptr += 2;
10213 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10214 info_ptr += blk->size;
10215 DW_BLOCK (attr) = blk;
10216 break;
10217 case DW_FORM_block4:
7b5a2f43 10218 blk = dwarf_alloc_block (cu);
c906108c
SS
10219 blk->size = read_4_bytes (abfd, info_ptr);
10220 info_ptr += 4;
10221 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10222 info_ptr += blk->size;
10223 DW_BLOCK (attr) = blk;
10224 break;
10225 case DW_FORM_data2:
10226 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10227 info_ptr += 2;
10228 break;
10229 case DW_FORM_data4:
10230 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10231 info_ptr += 4;
10232 break;
10233 case DW_FORM_data8:
10234 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10235 info_ptr += 8;
10236 break;
2dc7f7b3
TT
10237 case DW_FORM_sec_offset:
10238 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10239 info_ptr += bytes_read;
10240 break;
c906108c 10241 case DW_FORM_string:
9b1c24c8 10242 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10243 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10244 info_ptr += bytes_read;
10245 break;
4bdf3d34
JJ
10246 case DW_FORM_strp:
10247 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10248 &bytes_read);
8285870a 10249 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10250 info_ptr += bytes_read;
10251 break;
2dc7f7b3 10252 case DW_FORM_exprloc:
c906108c 10253 case DW_FORM_block:
7b5a2f43 10254 blk = dwarf_alloc_block (cu);
c906108c
SS
10255 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10256 info_ptr += bytes_read;
10257 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10258 info_ptr += blk->size;
10259 DW_BLOCK (attr) = blk;
10260 break;
10261 case DW_FORM_block1:
7b5a2f43 10262 blk = dwarf_alloc_block (cu);
c906108c
SS
10263 blk->size = read_1_byte (abfd, info_ptr);
10264 info_ptr += 1;
10265 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10266 info_ptr += blk->size;
10267 DW_BLOCK (attr) = blk;
10268 break;
10269 case DW_FORM_data1:
10270 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10271 info_ptr += 1;
10272 break;
10273 case DW_FORM_flag:
10274 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10275 info_ptr += 1;
10276 break;
2dc7f7b3
TT
10277 case DW_FORM_flag_present:
10278 DW_UNSND (attr) = 1;
10279 break;
c906108c
SS
10280 case DW_FORM_sdata:
10281 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10282 info_ptr += bytes_read;
10283 break;
10284 case DW_FORM_udata:
10285 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10286 info_ptr += bytes_read;
10287 break;
10288 case DW_FORM_ref1:
10b3939b 10289 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10290 info_ptr += 1;
10291 break;
10292 case DW_FORM_ref2:
10b3939b 10293 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10294 info_ptr += 2;
10295 break;
10296 case DW_FORM_ref4:
10b3939b 10297 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10298 info_ptr += 4;
10299 break;
613e1657 10300 case DW_FORM_ref8:
10b3939b 10301 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10302 info_ptr += 8;
10303 break;
55f1336d 10304 case DW_FORM_ref_sig8:
348e048f
DE
10305 /* Convert the signature to something we can record in DW_UNSND
10306 for later lookup.
10307 NOTE: This is NULL if the type wasn't found. */
10308 DW_SIGNATURED_TYPE (attr) =
10309 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10310 info_ptr += 8;
10311 break;
c906108c 10312 case DW_FORM_ref_udata:
10b3939b
DJ
10313 DW_ADDR (attr) = (cu->header.offset
10314 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10315 info_ptr += bytes_read;
10316 break;
c906108c 10317 case DW_FORM_indirect:
a8329558
KW
10318 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10319 info_ptr += bytes_read;
e7c27a73 10320 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10321 break;
c906108c 10322 default:
8a3fe4f8 10323 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10324 dwarf_form_name (form),
10325 bfd_get_filename (abfd));
c906108c 10326 }
28e94949
JB
10327
10328 /* We have seen instances where the compiler tried to emit a byte
10329 size attribute of -1 which ended up being encoded as an unsigned
10330 0xffffffff. Although 0xffffffff is technically a valid size value,
10331 an object of this size seems pretty unlikely so we can relatively
10332 safely treat these cases as if the size attribute was invalid and
10333 treat them as zero by default. */
10334 if (attr->name == DW_AT_byte_size
10335 && form == DW_FORM_data4
10336 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10337 {
10338 complaint
10339 (&symfile_complaints,
43bbcdc2
PH
10340 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10341 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10342 DW_UNSND (attr) = 0;
10343 }
28e94949 10344
c906108c
SS
10345 return info_ptr;
10346}
10347
a8329558
KW
10348/* Read an attribute described by an abbreviated attribute. */
10349
fe1b8b76 10350static gdb_byte *
a8329558 10351read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10352 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10353{
10354 attr->name = abbrev->name;
e7c27a73 10355 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10356}
10357
0963b4bd 10358/* Read dwarf information from a buffer. */
c906108c
SS
10359
10360static unsigned int
fe1b8b76 10361read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10362{
fe1b8b76 10363 return bfd_get_8 (abfd, buf);
c906108c
SS
10364}
10365
10366static int
fe1b8b76 10367read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10368{
fe1b8b76 10369 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10370}
10371
10372static unsigned int
fe1b8b76 10373read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10374{
fe1b8b76 10375 return bfd_get_16 (abfd, buf);
c906108c
SS
10376}
10377
21ae7a4d
JK
10378static int
10379read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10380{
10381 return bfd_get_signed_16 (abfd, buf);
10382}
10383
c906108c 10384static unsigned int
fe1b8b76 10385read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10386{
fe1b8b76 10387 return bfd_get_32 (abfd, buf);
c906108c
SS
10388}
10389
21ae7a4d
JK
10390static int
10391read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10392{
10393 return bfd_get_signed_32 (abfd, buf);
10394}
10395
93311388 10396static ULONGEST
fe1b8b76 10397read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10398{
fe1b8b76 10399 return bfd_get_64 (abfd, buf);
c906108c
SS
10400}
10401
10402static CORE_ADDR
fe1b8b76 10403read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10404 unsigned int *bytes_read)
c906108c 10405{
e7c27a73 10406 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10407 CORE_ADDR retval = 0;
10408
107d2387 10409 if (cu_header->signed_addr_p)
c906108c 10410 {
107d2387
AC
10411 switch (cu_header->addr_size)
10412 {
10413 case 2:
fe1b8b76 10414 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10415 break;
10416 case 4:
fe1b8b76 10417 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10418 break;
10419 case 8:
fe1b8b76 10420 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10421 break;
10422 default:
8e65ff28 10423 internal_error (__FILE__, __LINE__,
e2e0b3e5 10424 _("read_address: bad switch, signed [in module %s]"),
659b0389 10425 bfd_get_filename (abfd));
107d2387
AC
10426 }
10427 }
10428 else
10429 {
10430 switch (cu_header->addr_size)
10431 {
10432 case 2:
fe1b8b76 10433 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10434 break;
10435 case 4:
fe1b8b76 10436 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10437 break;
10438 case 8:
fe1b8b76 10439 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10440 break;
10441 default:
8e65ff28 10442 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10443 _("read_address: bad switch, "
10444 "unsigned [in module %s]"),
659b0389 10445 bfd_get_filename (abfd));
107d2387 10446 }
c906108c 10447 }
64367e0a 10448
107d2387
AC
10449 *bytes_read = cu_header->addr_size;
10450 return retval;
c906108c
SS
10451}
10452
f7ef9339 10453/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10454 specification allows the initial length to take up either 4 bytes
10455 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10456 bytes describe the length and all offsets will be 8 bytes in length
10457 instead of 4.
10458
f7ef9339
KB
10459 An older, non-standard 64-bit format is also handled by this
10460 function. The older format in question stores the initial length
10461 as an 8-byte quantity without an escape value. Lengths greater
10462 than 2^32 aren't very common which means that the initial 4 bytes
10463 is almost always zero. Since a length value of zero doesn't make
10464 sense for the 32-bit format, this initial zero can be considered to
10465 be an escape value which indicates the presence of the older 64-bit
10466 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10467 greater than 4GB. If it becomes necessary to handle lengths
10468 somewhat larger than 4GB, we could allow other small values (such
10469 as the non-sensical values of 1, 2, and 3) to also be used as
10470 escape values indicating the presence of the old format.
f7ef9339 10471
917c78fc
MK
10472 The value returned via bytes_read should be used to increment the
10473 relevant pointer after calling read_initial_length().
c764a876 10474
613e1657
KB
10475 [ Note: read_initial_length() and read_offset() are based on the
10476 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10477 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10478 from:
10479
f7ef9339 10480 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10481
613e1657
KB
10482 This document is only a draft and is subject to change. (So beware.)
10483
f7ef9339 10484 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10485 determined empirically by examining 64-bit ELF files produced by
10486 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10487
10488 - Kevin, July 16, 2002
613e1657
KB
10489 ] */
10490
10491static LONGEST
c764a876 10492read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10493{
fe1b8b76 10494 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10495
dd373385 10496 if (length == 0xffffffff)
613e1657 10497 {
fe1b8b76 10498 length = bfd_get_64 (abfd, buf + 4);
613e1657 10499 *bytes_read = 12;
613e1657 10500 }
dd373385 10501 else if (length == 0)
f7ef9339 10502 {
dd373385 10503 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10504 length = bfd_get_64 (abfd, buf);
f7ef9339 10505 *bytes_read = 8;
f7ef9339 10506 }
613e1657
KB
10507 else
10508 {
10509 *bytes_read = 4;
613e1657
KB
10510 }
10511
c764a876
DE
10512 return length;
10513}
dd373385 10514
c764a876
DE
10515/* Cover function for read_initial_length.
10516 Returns the length of the object at BUF, and stores the size of the
10517 initial length in *BYTES_READ and stores the size that offsets will be in
10518 *OFFSET_SIZE.
10519 If the initial length size is not equivalent to that specified in
10520 CU_HEADER then issue a complaint.
10521 This is useful when reading non-comp-unit headers. */
dd373385 10522
c764a876
DE
10523static LONGEST
10524read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10525 const struct comp_unit_head *cu_header,
10526 unsigned int *bytes_read,
10527 unsigned int *offset_size)
10528{
10529 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10530
10531 gdb_assert (cu_header->initial_length_size == 4
10532 || cu_header->initial_length_size == 8
10533 || cu_header->initial_length_size == 12);
10534
10535 if (cu_header->initial_length_size != *bytes_read)
10536 complaint (&symfile_complaints,
10537 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10538
c764a876 10539 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10540 return length;
613e1657
KB
10541}
10542
10543/* Read an offset from the data stream. The size of the offset is
917c78fc 10544 given by cu_header->offset_size. */
613e1657
KB
10545
10546static LONGEST
fe1b8b76 10547read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10548 unsigned int *bytes_read)
c764a876
DE
10549{
10550 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10551
c764a876
DE
10552 *bytes_read = cu_header->offset_size;
10553 return offset;
10554}
10555
10556/* Read an offset from the data stream. */
10557
10558static LONGEST
10559read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10560{
10561 LONGEST retval = 0;
10562
c764a876 10563 switch (offset_size)
613e1657
KB
10564 {
10565 case 4:
fe1b8b76 10566 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10567 break;
10568 case 8:
fe1b8b76 10569 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10570 break;
10571 default:
8e65ff28 10572 internal_error (__FILE__, __LINE__,
c764a876 10573 _("read_offset_1: bad switch [in module %s]"),
659b0389 10574 bfd_get_filename (abfd));
613e1657
KB
10575 }
10576
917c78fc 10577 return retval;
613e1657
KB
10578}
10579
fe1b8b76
JB
10580static gdb_byte *
10581read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10582{
10583 /* If the size of a host char is 8 bits, we can return a pointer
10584 to the buffer, otherwise we have to copy the data to a buffer
10585 allocated on the temporary obstack. */
4bdf3d34 10586 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10587 return buf;
c906108c
SS
10588}
10589
10590static char *
9b1c24c8 10591read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10592{
10593 /* If the size of a host char is 8 bits, we can return a pointer
10594 to the string, otherwise we have to copy the string to a buffer
10595 allocated on the temporary obstack. */
4bdf3d34 10596 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10597 if (*buf == '\0')
10598 {
10599 *bytes_read_ptr = 1;
10600 return NULL;
10601 }
fe1b8b76
JB
10602 *bytes_read_ptr = strlen ((char *) buf) + 1;
10603 return (char *) buf;
4bdf3d34
JJ
10604}
10605
10606static char *
cf2c3c16 10607read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10608{
be391dca 10609 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10610 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10611 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10612 bfd_get_filename (abfd));
dce234bc 10613 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10614 error (_("DW_FORM_strp pointing outside of "
10615 ".debug_str section [in module %s]"),
10616 bfd_get_filename (abfd));
4bdf3d34 10617 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10618 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10619 return NULL;
dce234bc 10620 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10621}
10622
cf2c3c16
TT
10623static char *
10624read_indirect_string (bfd *abfd, gdb_byte *buf,
10625 const struct comp_unit_head *cu_header,
10626 unsigned int *bytes_read_ptr)
10627{
10628 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10629
10630 return read_indirect_string_at_offset (abfd, str_offset);
10631}
10632
ce5d95e1 10633static unsigned long
fe1b8b76 10634read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10635{
ce5d95e1
JB
10636 unsigned long result;
10637 unsigned int num_read;
c906108c
SS
10638 int i, shift;
10639 unsigned char byte;
10640
10641 result = 0;
10642 shift = 0;
10643 num_read = 0;
10644 i = 0;
10645 while (1)
10646 {
fe1b8b76 10647 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10648 buf++;
10649 num_read++;
ce5d95e1 10650 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10651 if ((byte & 128) == 0)
10652 {
10653 break;
10654 }
10655 shift += 7;
10656 }
10657 *bytes_read_ptr = num_read;
10658 return result;
10659}
10660
ce5d95e1 10661static long
fe1b8b76 10662read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10663{
ce5d95e1 10664 long result;
77e0b926 10665 int i, shift, num_read;
c906108c
SS
10666 unsigned char byte;
10667
10668 result = 0;
10669 shift = 0;
c906108c
SS
10670 num_read = 0;
10671 i = 0;
10672 while (1)
10673 {
fe1b8b76 10674 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10675 buf++;
10676 num_read++;
ce5d95e1 10677 result |= ((long)(byte & 127) << shift);
c906108c
SS
10678 shift += 7;
10679 if ((byte & 128) == 0)
10680 {
10681 break;
10682 }
10683 }
77e0b926
DJ
10684 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10685 result |= -(((long)1) << shift);
c906108c
SS
10686 *bytes_read_ptr = num_read;
10687 return result;
10688}
10689
4bb7a0a7
DJ
10690/* Return a pointer to just past the end of an LEB128 number in BUF. */
10691
fe1b8b76
JB
10692static gdb_byte *
10693skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10694{
10695 int byte;
10696
10697 while (1)
10698 {
fe1b8b76 10699 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10700 buf++;
10701 if ((byte & 128) == 0)
10702 return buf;
10703 }
10704}
10705
c906108c 10706static void
e142c38c 10707set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10708{
10709 switch (lang)
10710 {
10711 case DW_LANG_C89:
76bee0cc 10712 case DW_LANG_C99:
c906108c 10713 case DW_LANG_C:
e142c38c 10714 cu->language = language_c;
c906108c
SS
10715 break;
10716 case DW_LANG_C_plus_plus:
e142c38c 10717 cu->language = language_cplus;
c906108c 10718 break;
6aecb9c2
JB
10719 case DW_LANG_D:
10720 cu->language = language_d;
10721 break;
c906108c
SS
10722 case DW_LANG_Fortran77:
10723 case DW_LANG_Fortran90:
b21b22e0 10724 case DW_LANG_Fortran95:
e142c38c 10725 cu->language = language_fortran;
c906108c
SS
10726 break;
10727 case DW_LANG_Mips_Assembler:
e142c38c 10728 cu->language = language_asm;
c906108c 10729 break;
bebd888e 10730 case DW_LANG_Java:
e142c38c 10731 cu->language = language_java;
bebd888e 10732 break;
c906108c 10733 case DW_LANG_Ada83:
8aaf0b47 10734 case DW_LANG_Ada95:
bc5f45f8
JB
10735 cu->language = language_ada;
10736 break;
72019c9c
GM
10737 case DW_LANG_Modula2:
10738 cu->language = language_m2;
10739 break;
fe8e67fd
PM
10740 case DW_LANG_Pascal83:
10741 cu->language = language_pascal;
10742 break;
22566fbd
DJ
10743 case DW_LANG_ObjC:
10744 cu->language = language_objc;
10745 break;
c906108c
SS
10746 case DW_LANG_Cobol74:
10747 case DW_LANG_Cobol85:
c906108c 10748 default:
e142c38c 10749 cu->language = language_minimal;
c906108c
SS
10750 break;
10751 }
e142c38c 10752 cu->language_defn = language_def (cu->language);
c906108c
SS
10753}
10754
10755/* Return the named attribute or NULL if not there. */
10756
10757static struct attribute *
e142c38c 10758dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10759{
10760 unsigned int i;
10761 struct attribute *spec = NULL;
10762
10763 for (i = 0; i < die->num_attrs; ++i)
10764 {
10765 if (die->attrs[i].name == name)
10b3939b 10766 return &die->attrs[i];
c906108c
SS
10767 if (die->attrs[i].name == DW_AT_specification
10768 || die->attrs[i].name == DW_AT_abstract_origin)
10769 spec = &die->attrs[i];
10770 }
c906108c 10771
10b3939b 10772 if (spec)
f2f0e013
DJ
10773 {
10774 die = follow_die_ref (die, spec, &cu);
10775 return dwarf2_attr (die, name, cu);
10776 }
c5aa993b 10777
c906108c
SS
10778 return NULL;
10779}
10780
348e048f
DE
10781/* Return the named attribute or NULL if not there,
10782 but do not follow DW_AT_specification, etc.
10783 This is for use in contexts where we're reading .debug_types dies.
10784 Following DW_AT_specification, DW_AT_abstract_origin will take us
10785 back up the chain, and we want to go down. */
10786
10787static struct attribute *
10788dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10789 struct dwarf2_cu *cu)
10790{
10791 unsigned int i;
10792
10793 for (i = 0; i < die->num_attrs; ++i)
10794 if (die->attrs[i].name == name)
10795 return &die->attrs[i];
10796
10797 return NULL;
10798}
10799
05cf31d1
JB
10800/* Return non-zero iff the attribute NAME is defined for the given DIE,
10801 and holds a non-zero value. This function should only be used for
2dc7f7b3 10802 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10803
10804static int
10805dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10806{
10807 struct attribute *attr = dwarf2_attr (die, name, cu);
10808
10809 return (attr && DW_UNSND (attr));
10810}
10811
3ca72b44 10812static int
e142c38c 10813die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10814{
05cf31d1
JB
10815 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10816 which value is non-zero. However, we have to be careful with
10817 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10818 (via dwarf2_flag_true_p) follows this attribute. So we may
10819 end up accidently finding a declaration attribute that belongs
10820 to a different DIE referenced by the specification attribute,
10821 even though the given DIE does not have a declaration attribute. */
10822 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10823 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10824}
10825
63d06c5c 10826/* Return the die giving the specification for DIE, if there is
f2f0e013 10827 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10828 containing the return value on output. If there is no
10829 specification, but there is an abstract origin, that is
10830 returned. */
63d06c5c
DC
10831
10832static struct die_info *
f2f0e013 10833die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10834{
f2f0e013
DJ
10835 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10836 *spec_cu);
63d06c5c 10837
edb3359d
DJ
10838 if (spec_attr == NULL)
10839 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10840
63d06c5c
DC
10841 if (spec_attr == NULL)
10842 return NULL;
10843 else
f2f0e013 10844 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10845}
c906108c 10846
debd256d 10847/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10848 refers to.
10849 NOTE: This is also used as a "cleanup" function. */
10850
debd256d
JB
10851static void
10852free_line_header (struct line_header *lh)
10853{
10854 if (lh->standard_opcode_lengths)
a8bc7b56 10855 xfree (lh->standard_opcode_lengths);
debd256d
JB
10856
10857 /* Remember that all the lh->file_names[i].name pointers are
10858 pointers into debug_line_buffer, and don't need to be freed. */
10859 if (lh->file_names)
a8bc7b56 10860 xfree (lh->file_names);
debd256d
JB
10861
10862 /* Similarly for the include directory names. */
10863 if (lh->include_dirs)
a8bc7b56 10864 xfree (lh->include_dirs);
debd256d 10865
a8bc7b56 10866 xfree (lh);
debd256d
JB
10867}
10868
debd256d 10869/* Add an entry to LH's include directory table. */
ae2de4f8 10870
debd256d
JB
10871static void
10872add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10873{
debd256d
JB
10874 /* Grow the array if necessary. */
10875 if (lh->include_dirs_size == 0)
c5aa993b 10876 {
debd256d
JB
10877 lh->include_dirs_size = 1; /* for testing */
10878 lh->include_dirs = xmalloc (lh->include_dirs_size
10879 * sizeof (*lh->include_dirs));
10880 }
10881 else if (lh->num_include_dirs >= lh->include_dirs_size)
10882 {
10883 lh->include_dirs_size *= 2;
10884 lh->include_dirs = xrealloc (lh->include_dirs,
10885 (lh->include_dirs_size
10886 * sizeof (*lh->include_dirs)));
c5aa993b 10887 }
c906108c 10888
debd256d
JB
10889 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10890}
6e70227d 10891
debd256d 10892/* Add an entry to LH's file name table. */
ae2de4f8 10893
debd256d
JB
10894static void
10895add_file_name (struct line_header *lh,
10896 char *name,
10897 unsigned int dir_index,
10898 unsigned int mod_time,
10899 unsigned int length)
10900{
10901 struct file_entry *fe;
10902
10903 /* Grow the array if necessary. */
10904 if (lh->file_names_size == 0)
10905 {
10906 lh->file_names_size = 1; /* for testing */
10907 lh->file_names = xmalloc (lh->file_names_size
10908 * sizeof (*lh->file_names));
10909 }
10910 else if (lh->num_file_names >= lh->file_names_size)
10911 {
10912 lh->file_names_size *= 2;
10913 lh->file_names = xrealloc (lh->file_names,
10914 (lh->file_names_size
10915 * sizeof (*lh->file_names)));
10916 }
10917
10918 fe = &lh->file_names[lh->num_file_names++];
10919 fe->name = name;
10920 fe->dir_index = dir_index;
10921 fe->mod_time = mod_time;
10922 fe->length = length;
aaa75496 10923 fe->included_p = 0;
cb1df416 10924 fe->symtab = NULL;
debd256d 10925}
6e70227d 10926
debd256d 10927/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10928 .debug_line, according to the endianness of ABFD. Return a pointer
10929 to a struct line_header, allocated using xmalloc.
debd256d
JB
10930
10931 NOTE: the strings in the include directory and file name tables of
10932 the returned object point into debug_line_buffer, and must not be
10933 freed. */
ae2de4f8 10934
debd256d
JB
10935static struct line_header *
10936dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10937 struct dwarf2_cu *cu)
debd256d
JB
10938{
10939 struct cleanup *back_to;
10940 struct line_header *lh;
fe1b8b76 10941 gdb_byte *line_ptr;
c764a876 10942 unsigned int bytes_read, offset_size;
debd256d
JB
10943 int i;
10944 char *cur_dir, *cur_file;
10945
be391dca 10946 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10947 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10948 {
e2e0b3e5 10949 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10950 return 0;
10951 }
10952
a738430d
MK
10953 /* Make sure that at least there's room for the total_length field.
10954 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10955 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10956 {
4d3c2250 10957 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10958 return 0;
10959 }
10960
10961 lh = xmalloc (sizeof (*lh));
10962 memset (lh, 0, sizeof (*lh));
10963 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10964 (void *) lh);
10965
dce234bc 10966 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10967
a738430d 10968 /* Read in the header. */
6e70227d 10969 lh->total_length =
c764a876
DE
10970 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10971 &bytes_read, &offset_size);
debd256d 10972 line_ptr += bytes_read;
dce234bc
PP
10973 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10974 + dwarf2_per_objfile->line.size))
debd256d 10975 {
4d3c2250 10976 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10977 return 0;
10978 }
10979 lh->statement_program_end = line_ptr + lh->total_length;
10980 lh->version = read_2_bytes (abfd, line_ptr);
10981 line_ptr += 2;
c764a876
DE
10982 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10983 line_ptr += offset_size;
debd256d
JB
10984 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10985 line_ptr += 1;
2dc7f7b3
TT
10986 if (lh->version >= 4)
10987 {
10988 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10989 line_ptr += 1;
10990 }
10991 else
10992 lh->maximum_ops_per_instruction = 1;
10993
10994 if (lh->maximum_ops_per_instruction == 0)
10995 {
10996 lh->maximum_ops_per_instruction = 1;
10997 complaint (&symfile_complaints,
3e43a32a
MS
10998 _("invalid maximum_ops_per_instruction "
10999 "in `.debug_line' section"));
2dc7f7b3
TT
11000 }
11001
debd256d
JB
11002 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11003 line_ptr += 1;
11004 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11005 line_ptr += 1;
11006 lh->line_range = read_1_byte (abfd, line_ptr);
11007 line_ptr += 1;
11008 lh->opcode_base = read_1_byte (abfd, line_ptr);
11009 line_ptr += 1;
11010 lh->standard_opcode_lengths
fe1b8b76 11011 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11012
11013 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11014 for (i = 1; i < lh->opcode_base; ++i)
11015 {
11016 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11017 line_ptr += 1;
11018 }
11019
a738430d 11020 /* Read directory table. */
9b1c24c8 11021 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11022 {
11023 line_ptr += bytes_read;
11024 add_include_dir (lh, cur_dir);
11025 }
11026 line_ptr += bytes_read;
11027
a738430d 11028 /* Read file name table. */
9b1c24c8 11029 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11030 {
11031 unsigned int dir_index, mod_time, length;
11032
11033 line_ptr += bytes_read;
11034 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11035 line_ptr += bytes_read;
11036 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11037 line_ptr += bytes_read;
11038 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11039 line_ptr += bytes_read;
11040
11041 add_file_name (lh, cur_file, dir_index, mod_time, length);
11042 }
11043 line_ptr += bytes_read;
6e70227d 11044 lh->statement_program_start = line_ptr;
debd256d 11045
dce234bc
PP
11046 if (line_ptr > (dwarf2_per_objfile->line.buffer
11047 + dwarf2_per_objfile->line.size))
4d3c2250 11048 complaint (&symfile_complaints,
3e43a32a
MS
11049 _("line number info header doesn't "
11050 "fit in `.debug_line' section"));
debd256d
JB
11051
11052 discard_cleanups (back_to);
11053 return lh;
11054}
c906108c 11055
5fb290d7
DJ
11056/* This function exists to work around a bug in certain compilers
11057 (particularly GCC 2.95), in which the first line number marker of a
11058 function does not show up until after the prologue, right before
11059 the second line number marker. This function shifts ADDRESS down
11060 to the beginning of the function if necessary, and is called on
11061 addresses passed to record_line. */
11062
11063static CORE_ADDR
e142c38c 11064check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
11065{
11066 struct function_range *fn;
11067
11068 /* Find the function_range containing address. */
e142c38c 11069 if (!cu->first_fn)
5fb290d7
DJ
11070 return address;
11071
e142c38c
DJ
11072 if (!cu->cached_fn)
11073 cu->cached_fn = cu->first_fn;
5fb290d7 11074
e142c38c 11075 fn = cu->cached_fn;
5fb290d7
DJ
11076 while (fn)
11077 if (fn->lowpc <= address && fn->highpc > address)
11078 goto found;
11079 else
11080 fn = fn->next;
11081
e142c38c
DJ
11082 fn = cu->first_fn;
11083 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
11084 if (fn->lowpc <= address && fn->highpc > address)
11085 goto found;
11086 else
11087 fn = fn->next;
11088
11089 return address;
11090
11091 found:
11092 if (fn->seen_line)
11093 return address;
11094 if (address != fn->lowpc)
4d3c2250 11095 complaint (&symfile_complaints,
e2e0b3e5 11096 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 11097 (unsigned long) address, fn->name);
5fb290d7
DJ
11098 fn->seen_line = 1;
11099 return fn->lowpc;
11100}
11101
c6da4cef
DE
11102/* Subroutine of dwarf_decode_lines to simplify it.
11103 Return the file name of the psymtab for included file FILE_INDEX
11104 in line header LH of PST.
11105 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11106 If space for the result is malloc'd, it will be freed by a cleanup.
11107 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11108
11109static char *
11110psymtab_include_file_name (const struct line_header *lh, int file_index,
11111 const struct partial_symtab *pst,
11112 const char *comp_dir)
11113{
11114 const struct file_entry fe = lh->file_names [file_index];
11115 char *include_name = fe.name;
11116 char *include_name_to_compare = include_name;
11117 char *dir_name = NULL;
72b9f47f
TT
11118 const char *pst_filename;
11119 char *copied_name = NULL;
c6da4cef
DE
11120 int file_is_pst;
11121
11122 if (fe.dir_index)
11123 dir_name = lh->include_dirs[fe.dir_index - 1];
11124
11125 if (!IS_ABSOLUTE_PATH (include_name)
11126 && (dir_name != NULL || comp_dir != NULL))
11127 {
11128 /* Avoid creating a duplicate psymtab for PST.
11129 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11130 Before we do the comparison, however, we need to account
11131 for DIR_NAME and COMP_DIR.
11132 First prepend dir_name (if non-NULL). If we still don't
11133 have an absolute path prepend comp_dir (if non-NULL).
11134 However, the directory we record in the include-file's
11135 psymtab does not contain COMP_DIR (to match the
11136 corresponding symtab(s)).
11137
11138 Example:
11139
11140 bash$ cd /tmp
11141 bash$ gcc -g ./hello.c
11142 include_name = "hello.c"
11143 dir_name = "."
11144 DW_AT_comp_dir = comp_dir = "/tmp"
11145 DW_AT_name = "./hello.c" */
11146
11147 if (dir_name != NULL)
11148 {
11149 include_name = concat (dir_name, SLASH_STRING,
11150 include_name, (char *)NULL);
11151 include_name_to_compare = include_name;
11152 make_cleanup (xfree, include_name);
11153 }
11154 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11155 {
11156 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11157 include_name, (char *)NULL);
11158 }
11159 }
11160
11161 pst_filename = pst->filename;
11162 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11163 {
72b9f47f
TT
11164 copied_name = concat (pst->dirname, SLASH_STRING,
11165 pst_filename, (char *)NULL);
11166 pst_filename = copied_name;
c6da4cef
DE
11167 }
11168
1e3fad37 11169 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11170
11171 if (include_name_to_compare != include_name)
11172 xfree (include_name_to_compare);
72b9f47f
TT
11173 if (copied_name != NULL)
11174 xfree (copied_name);
c6da4cef
DE
11175
11176 if (file_is_pst)
11177 return NULL;
11178 return include_name;
11179}
11180
c91513d8
PP
11181/* Ignore this record_line request. */
11182
11183static void
11184noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11185{
11186 return;
11187}
11188
aaa75496
JB
11189/* Decode the Line Number Program (LNP) for the given line_header
11190 structure and CU. The actual information extracted and the type
11191 of structures created from the LNP depends on the value of PST.
11192
11193 1. If PST is NULL, then this procedure uses the data from the program
11194 to create all necessary symbol tables, and their linetables.
6e70227d 11195
aaa75496
JB
11196 2. If PST is not NULL, this procedure reads the program to determine
11197 the list of files included by the unit represented by PST, and
c6da4cef
DE
11198 builds all the associated partial symbol tables.
11199
11200 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11201 It is used for relative paths in the line table.
11202 NOTE: When processing partial symtabs (pst != NULL),
11203 comp_dir == pst->dirname.
11204
11205 NOTE: It is important that psymtabs have the same file name (via strcmp)
11206 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11207 symtab we don't use it in the name of the psymtabs we create.
11208 E.g. expand_line_sal requires this when finding psymtabs to expand.
11209 A good testcase for this is mb-inline.exp. */
debd256d 11210
c906108c 11211static void
72b9f47f 11212dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11213 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11214{
a8c50c1f 11215 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11216 gdb_byte *line_end;
a8c50c1f 11217 unsigned int bytes_read, extended_len;
c906108c 11218 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11219 CORE_ADDR baseaddr;
11220 struct objfile *objfile = cu->objfile;
fbf65064 11221 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11222 const int decode_for_pst_p = (pst != NULL);
cb1df416 11223 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11224 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11225 = record_line;
e142c38c
DJ
11226
11227 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11228
debd256d
JB
11229 line_ptr = lh->statement_program_start;
11230 line_end = lh->statement_program_end;
c906108c
SS
11231
11232 /* Read the statement sequences until there's nothing left. */
11233 while (line_ptr < line_end)
11234 {
11235 /* state machine registers */
11236 CORE_ADDR address = 0;
11237 unsigned int file = 1;
11238 unsigned int line = 1;
11239 unsigned int column = 0;
debd256d 11240 int is_stmt = lh->default_is_stmt;
c906108c
SS
11241 int basic_block = 0;
11242 int end_sequence = 0;
fbf65064 11243 CORE_ADDR addr;
2dc7f7b3 11244 unsigned char op_index = 0;
c906108c 11245
aaa75496 11246 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11247 {
aaa75496 11248 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11249 /* lh->include_dirs and lh->file_names are 0-based, but the
11250 directory and file name numbers in the statement program
11251 are 1-based. */
11252 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11253 char *dir = NULL;
a738430d 11254
debd256d
JB
11255 if (fe->dir_index)
11256 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11257
11258 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11259 }
11260
a738430d 11261 /* Decode the table. */
c5aa993b 11262 while (!end_sequence)
c906108c
SS
11263 {
11264 op_code = read_1_byte (abfd, line_ptr);
11265 line_ptr += 1;
59205f5a
JB
11266 if (line_ptr > line_end)
11267 {
11268 dwarf2_debug_line_missing_end_sequence_complaint ();
11269 break;
11270 }
9aa1fe7e 11271
debd256d 11272 if (op_code >= lh->opcode_base)
6e70227d 11273 {
a738430d 11274 /* Special operand. */
debd256d 11275 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11276 address += (((op_index + (adj_opcode / lh->line_range))
11277 / lh->maximum_ops_per_instruction)
11278 * lh->minimum_instruction_length);
11279 op_index = ((op_index + (adj_opcode / lh->line_range))
11280 % lh->maximum_ops_per_instruction);
debd256d 11281 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11282 if (lh->num_file_names < file || file == 0)
25e43795 11283 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11284 /* For now we ignore lines not starting on an
11285 instruction boundary. */
11286 else if (op_index == 0)
25e43795
DJ
11287 {
11288 lh->file_names[file - 1].included_p = 1;
ca5f395d 11289 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11290 {
11291 if (last_subfile != current_subfile)
11292 {
11293 addr = gdbarch_addr_bits_remove (gdbarch, address);
11294 if (last_subfile)
c91513d8 11295 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11296 last_subfile = current_subfile;
11297 }
25e43795 11298 /* Append row to matrix using current values. */
fbf65064
UW
11299 addr = check_cu_functions (address, cu);
11300 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11301 (*p_record_line) (current_subfile, line, addr);
366da635 11302 }
25e43795 11303 }
ca5f395d 11304 basic_block = 0;
9aa1fe7e
GK
11305 }
11306 else switch (op_code)
c906108c
SS
11307 {
11308 case DW_LNS_extended_op:
3e43a32a
MS
11309 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11310 &bytes_read);
473b7be6 11311 line_ptr += bytes_read;
a8c50c1f 11312 extended_end = line_ptr + extended_len;
c906108c
SS
11313 extended_op = read_1_byte (abfd, line_ptr);
11314 line_ptr += 1;
11315 switch (extended_op)
11316 {
11317 case DW_LNE_end_sequence:
c91513d8 11318 p_record_line = record_line;
c906108c 11319 end_sequence = 1;
c906108c
SS
11320 break;
11321 case DW_LNE_set_address:
e7c27a73 11322 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11323
11324 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11325 {
11326 /* This line table is for a function which has been
11327 GCd by the linker. Ignore it. PR gdb/12528 */
11328
11329 long line_offset
11330 = line_ptr - dwarf2_per_objfile->line.buffer;
11331
11332 complaint (&symfile_complaints,
11333 _(".debug_line address at offset 0x%lx is 0 "
11334 "[in module %s]"),
11335 line_offset, cu->objfile->name);
11336 p_record_line = noop_record_line;
11337 }
11338
2dc7f7b3 11339 op_index = 0;
107d2387
AC
11340 line_ptr += bytes_read;
11341 address += baseaddr;
c906108c
SS
11342 break;
11343 case DW_LNE_define_file:
debd256d
JB
11344 {
11345 char *cur_file;
11346 unsigned int dir_index, mod_time, length;
6e70227d 11347
3e43a32a
MS
11348 cur_file = read_direct_string (abfd, line_ptr,
11349 &bytes_read);
debd256d
JB
11350 line_ptr += bytes_read;
11351 dir_index =
11352 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11353 line_ptr += bytes_read;
11354 mod_time =
11355 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11356 line_ptr += bytes_read;
11357 length =
11358 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11359 line_ptr += bytes_read;
11360 add_file_name (lh, cur_file, dir_index, mod_time, length);
11361 }
c906108c 11362 break;
d0c6ba3d
CC
11363 case DW_LNE_set_discriminator:
11364 /* The discriminator is not interesting to the debugger;
11365 just ignore it. */
11366 line_ptr = extended_end;
11367 break;
c906108c 11368 default:
4d3c2250 11369 complaint (&symfile_complaints,
e2e0b3e5 11370 _("mangled .debug_line section"));
debd256d 11371 return;
c906108c 11372 }
a8c50c1f
DJ
11373 /* Make sure that we parsed the extended op correctly. If e.g.
11374 we expected a different address size than the producer used,
11375 we may have read the wrong number of bytes. */
11376 if (line_ptr != extended_end)
11377 {
11378 complaint (&symfile_complaints,
11379 _("mangled .debug_line section"));
11380 return;
11381 }
c906108c
SS
11382 break;
11383 case DW_LNS_copy:
59205f5a 11384 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11385 dwarf2_debug_line_missing_file_complaint ();
11386 else
366da635 11387 {
25e43795 11388 lh->file_names[file - 1].included_p = 1;
ca5f395d 11389 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11390 {
11391 if (last_subfile != current_subfile)
11392 {
11393 addr = gdbarch_addr_bits_remove (gdbarch, address);
11394 if (last_subfile)
c91513d8 11395 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11396 last_subfile = current_subfile;
11397 }
11398 addr = check_cu_functions (address, cu);
11399 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11400 (*p_record_line) (current_subfile, line, addr);
fbf65064 11401 }
366da635 11402 }
c906108c
SS
11403 basic_block = 0;
11404 break;
11405 case DW_LNS_advance_pc:
2dc7f7b3
TT
11406 {
11407 CORE_ADDR adjust
11408 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11409
11410 address += (((op_index + adjust)
11411 / lh->maximum_ops_per_instruction)
11412 * lh->minimum_instruction_length);
11413 op_index = ((op_index + adjust)
11414 % lh->maximum_ops_per_instruction);
11415 line_ptr += bytes_read;
11416 }
c906108c
SS
11417 break;
11418 case DW_LNS_advance_line:
11419 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11420 line_ptr += bytes_read;
11421 break;
11422 case DW_LNS_set_file:
debd256d 11423 {
a738430d
MK
11424 /* The arrays lh->include_dirs and lh->file_names are
11425 0-based, but the directory and file name numbers in
11426 the statement program are 1-based. */
debd256d 11427 struct file_entry *fe;
4f1520fb 11428 char *dir = NULL;
a738430d 11429
debd256d
JB
11430 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11431 line_ptr += bytes_read;
59205f5a 11432 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11433 dwarf2_debug_line_missing_file_complaint ();
11434 else
11435 {
11436 fe = &lh->file_names[file - 1];
11437 if (fe->dir_index)
11438 dir = lh->include_dirs[fe->dir_index - 1];
11439 if (!decode_for_pst_p)
11440 {
11441 last_subfile = current_subfile;
11442 dwarf2_start_subfile (fe->name, dir, comp_dir);
11443 }
11444 }
debd256d 11445 }
c906108c
SS
11446 break;
11447 case DW_LNS_set_column:
11448 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11449 line_ptr += bytes_read;
11450 break;
11451 case DW_LNS_negate_stmt:
11452 is_stmt = (!is_stmt);
11453 break;
11454 case DW_LNS_set_basic_block:
11455 basic_block = 1;
11456 break;
c2c6d25f
JM
11457 /* Add to the address register of the state machine the
11458 address increment value corresponding to special opcode
a738430d
MK
11459 255. I.e., this value is scaled by the minimum
11460 instruction length since special opcode 255 would have
b021a221 11461 scaled the increment. */
c906108c 11462 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11463 {
11464 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11465
11466 address += (((op_index + adjust)
11467 / lh->maximum_ops_per_instruction)
11468 * lh->minimum_instruction_length);
11469 op_index = ((op_index + adjust)
11470 % lh->maximum_ops_per_instruction);
11471 }
c906108c
SS
11472 break;
11473 case DW_LNS_fixed_advance_pc:
11474 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11475 op_index = 0;
c906108c
SS
11476 line_ptr += 2;
11477 break;
9aa1fe7e 11478 default:
a738430d
MK
11479 {
11480 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11481 int i;
a738430d 11482
debd256d 11483 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11484 {
11485 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11486 line_ptr += bytes_read;
11487 }
11488 }
c906108c
SS
11489 }
11490 }
59205f5a
JB
11491 if (lh->num_file_names < file || file == 0)
11492 dwarf2_debug_line_missing_file_complaint ();
11493 else
11494 {
11495 lh->file_names[file - 1].included_p = 1;
11496 if (!decode_for_pst_p)
fbf65064
UW
11497 {
11498 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11499 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11500 }
59205f5a 11501 }
c906108c 11502 }
aaa75496
JB
11503
11504 if (decode_for_pst_p)
11505 {
11506 int file_index;
11507
11508 /* Now that we're done scanning the Line Header Program, we can
11509 create the psymtab of each included file. */
11510 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11511 if (lh->file_names[file_index].included_p == 1)
11512 {
c6da4cef
DE
11513 char *include_name =
11514 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11515 if (include_name != NULL)
aaa75496
JB
11516 dwarf2_create_include_psymtab (include_name, pst, objfile);
11517 }
11518 }
cb1df416
DJ
11519 else
11520 {
11521 /* Make sure a symtab is created for every file, even files
11522 which contain only variables (i.e. no code with associated
11523 line numbers). */
11524
11525 int i;
11526 struct file_entry *fe;
11527
11528 for (i = 0; i < lh->num_file_names; i++)
11529 {
11530 char *dir = NULL;
9a619af0 11531
cb1df416
DJ
11532 fe = &lh->file_names[i];
11533 if (fe->dir_index)
11534 dir = lh->include_dirs[fe->dir_index - 1];
11535 dwarf2_start_subfile (fe->name, dir, comp_dir);
11536
11537 /* Skip the main file; we don't need it, and it must be
11538 allocated last, so that it will show up before the
11539 non-primary symtabs in the objfile's symtab list. */
11540 if (current_subfile == first_subfile)
11541 continue;
11542
11543 if (current_subfile->symtab == NULL)
11544 current_subfile->symtab = allocate_symtab (current_subfile->name,
11545 cu->objfile);
11546 fe->symtab = current_subfile->symtab;
11547 }
11548 }
c906108c
SS
11549}
11550
11551/* Start a subfile for DWARF. FILENAME is the name of the file and
11552 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11553 or NULL if not known. COMP_DIR is the compilation directory for the
11554 linetable's compilation unit or NULL if not known.
c906108c
SS
11555 This routine tries to keep line numbers from identical absolute and
11556 relative file names in a common subfile.
11557
11558 Using the `list' example from the GDB testsuite, which resides in
11559 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11560 of /srcdir/list0.c yields the following debugging information for list0.c:
11561
c5aa993b
JM
11562 DW_AT_name: /srcdir/list0.c
11563 DW_AT_comp_dir: /compdir
357e46e7 11564 files.files[0].name: list0.h
c5aa993b 11565 files.files[0].dir: /srcdir
357e46e7 11566 files.files[1].name: list0.c
c5aa993b 11567 files.files[1].dir: /srcdir
c906108c
SS
11568
11569 The line number information for list0.c has to end up in a single
4f1520fb
FR
11570 subfile, so that `break /srcdir/list0.c:1' works as expected.
11571 start_subfile will ensure that this happens provided that we pass the
11572 concatenation of files.files[1].dir and files.files[1].name as the
11573 subfile's name. */
c906108c
SS
11574
11575static void
3e43a32a
MS
11576dwarf2_start_subfile (char *filename, const char *dirname,
11577 const char *comp_dir)
c906108c 11578{
4f1520fb
FR
11579 char *fullname;
11580
11581 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11582 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11583 second argument to start_subfile. To be consistent, we do the
11584 same here. In order not to lose the line information directory,
11585 we concatenate it to the filename when it makes sense.
11586 Note that the Dwarf3 standard says (speaking of filenames in line
11587 information): ``The directory index is ignored for file names
11588 that represent full path names''. Thus ignoring dirname in the
11589 `else' branch below isn't an issue. */
c906108c 11590
d5166ae1 11591 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11592 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11593 else
11594 fullname = filename;
c906108c 11595
4f1520fb
FR
11596 start_subfile (fullname, comp_dir);
11597
11598 if (fullname != filename)
11599 xfree (fullname);
c906108c
SS
11600}
11601
4c2df51b
DJ
11602static void
11603var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11604 struct dwarf2_cu *cu)
4c2df51b 11605{
e7c27a73
DJ
11606 struct objfile *objfile = cu->objfile;
11607 struct comp_unit_head *cu_header = &cu->header;
11608
4c2df51b
DJ
11609 /* NOTE drow/2003-01-30: There used to be a comment and some special
11610 code here to turn a symbol with DW_AT_external and a
11611 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11612 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11613 with some versions of binutils) where shared libraries could have
11614 relocations against symbols in their debug information - the
11615 minimal symbol would have the right address, but the debug info
11616 would not. It's no longer necessary, because we will explicitly
11617 apply relocations when we read in the debug information now. */
11618
11619 /* A DW_AT_location attribute with no contents indicates that a
11620 variable has been optimized away. */
11621 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11622 {
11623 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11624 return;
11625 }
11626
11627 /* Handle one degenerate form of location expression specially, to
11628 preserve GDB's previous behavior when section offsets are
11629 specified. If this is just a DW_OP_addr then mark this symbol
11630 as LOC_STATIC. */
11631
11632 if (attr_form_is_block (attr)
11633 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11634 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11635 {
891d2f0b 11636 unsigned int dummy;
4c2df51b
DJ
11637
11638 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11639 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11640 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11641 fixup_symbol_section (sym, objfile);
11642 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11643 SYMBOL_SECTION (sym));
4c2df51b
DJ
11644 return;
11645 }
11646
11647 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11648 expression evaluator, and use LOC_COMPUTED only when necessary
11649 (i.e. when the value of a register or memory location is
11650 referenced, or a thread-local block, etc.). Then again, it might
11651 not be worthwhile. I'm assuming that it isn't unless performance
11652 or memory numbers show me otherwise. */
11653
e7c27a73 11654 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11655 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11656
11657 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11658 cu->has_loclist = 1;
4c2df51b
DJ
11659}
11660
c906108c
SS
11661/* Given a pointer to a DWARF information entry, figure out if we need
11662 to make a symbol table entry for it, and if so, create a new entry
11663 and return a pointer to it.
11664 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11665 used the passed type.
11666 If SPACE is not NULL, use it to hold the new symbol. If it is
11667 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11668
11669static struct symbol *
34eaf542
TT
11670new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11671 struct symbol *space)
c906108c 11672{
e7c27a73 11673 struct objfile *objfile = cu->objfile;
c906108c
SS
11674 struct symbol *sym = NULL;
11675 char *name;
11676 struct attribute *attr = NULL;
11677 struct attribute *attr2 = NULL;
e142c38c 11678 CORE_ADDR baseaddr;
e37fd15a
SW
11679 struct pending **list_to_add = NULL;
11680
edb3359d 11681 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11682
11683 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11684
94af9270 11685 name = dwarf2_name (die, cu);
c906108c
SS
11686 if (name)
11687 {
94af9270 11688 const char *linkagename;
34eaf542 11689 int suppress_add = 0;
94af9270 11690
34eaf542
TT
11691 if (space)
11692 sym = space;
11693 else
11694 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11695 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11696
11697 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11698 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11699 linkagename = dwarf2_physname (name, die, cu);
11700 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11701
f55ee35c
JK
11702 /* Fortran does not have mangling standard and the mangling does differ
11703 between gfortran, iFort etc. */
11704 if (cu->language == language_fortran
b250c185 11705 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11706 symbol_set_demangled_name (&(sym->ginfo),
11707 (char *) dwarf2_full_name (name, die, cu),
11708 NULL);
f55ee35c 11709
c906108c 11710 /* Default assumptions.
c5aa993b 11711 Use the passed type or decode it from the die. */
176620f1 11712 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11713 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11714 if (type != NULL)
11715 SYMBOL_TYPE (sym) = type;
11716 else
e7c27a73 11717 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11718 attr = dwarf2_attr (die,
11719 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11720 cu);
c906108c
SS
11721 if (attr)
11722 {
11723 SYMBOL_LINE (sym) = DW_UNSND (attr);
11724 }
cb1df416 11725
edb3359d
DJ
11726 attr = dwarf2_attr (die,
11727 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11728 cu);
cb1df416
DJ
11729 if (attr)
11730 {
11731 int file_index = DW_UNSND (attr);
9a619af0 11732
cb1df416
DJ
11733 if (cu->line_header == NULL
11734 || file_index > cu->line_header->num_file_names)
11735 complaint (&symfile_complaints,
11736 _("file index out of range"));
1c3d648d 11737 else if (file_index > 0)
cb1df416
DJ
11738 {
11739 struct file_entry *fe;
9a619af0 11740
cb1df416
DJ
11741 fe = &cu->line_header->file_names[file_index - 1];
11742 SYMBOL_SYMTAB (sym) = fe->symtab;
11743 }
11744 }
11745
c906108c
SS
11746 switch (die->tag)
11747 {
11748 case DW_TAG_label:
e142c38c 11749 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11750 if (attr)
11751 {
11752 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11753 }
0f5238ed
TT
11754 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11755 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11756 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11757 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11758 break;
11759 case DW_TAG_subprogram:
11760 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11761 finish_block. */
11762 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11763 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11764 if ((attr2 && (DW_UNSND (attr2) != 0))
11765 || cu->language == language_ada)
c906108c 11766 {
2cfa0c8d
JB
11767 /* Subprograms marked external are stored as a global symbol.
11768 Ada subprograms, whether marked external or not, are always
11769 stored as a global symbol, because we want to be able to
11770 access them globally. For instance, we want to be able
11771 to break on a nested subprogram without having to
11772 specify the context. */
e37fd15a 11773 list_to_add = &global_symbols;
c906108c
SS
11774 }
11775 else
11776 {
e37fd15a 11777 list_to_add = cu->list_in_scope;
c906108c
SS
11778 }
11779 break;
edb3359d
DJ
11780 case DW_TAG_inlined_subroutine:
11781 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11782 finish_block. */
11783 SYMBOL_CLASS (sym) = LOC_BLOCK;
11784 SYMBOL_INLINED (sym) = 1;
11785 /* Do not add the symbol to any lists. It will be found via
11786 BLOCK_FUNCTION from the blockvector. */
11787 break;
34eaf542
TT
11788 case DW_TAG_template_value_param:
11789 suppress_add = 1;
11790 /* Fall through. */
72929c62 11791 case DW_TAG_constant:
c906108c 11792 case DW_TAG_variable:
254e6b9e 11793 case DW_TAG_member:
0963b4bd
MS
11794 /* Compilation with minimal debug info may result in
11795 variables with missing type entries. Change the
11796 misleading `void' type to something sensible. */
c906108c 11797 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11798 SYMBOL_TYPE (sym)
46bf5051 11799 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11800
e142c38c 11801 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11802 /* In the case of DW_TAG_member, we should only be called for
11803 static const members. */
11804 if (die->tag == DW_TAG_member)
11805 {
3863f96c
DE
11806 /* dwarf2_add_field uses die_is_declaration,
11807 so we do the same. */
254e6b9e
DE
11808 gdb_assert (die_is_declaration (die, cu));
11809 gdb_assert (attr);
11810 }
c906108c
SS
11811 if (attr)
11812 {
e7c27a73 11813 dwarf2_const_value (attr, sym, cu);
e142c38c 11814 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11815 if (!suppress_add)
34eaf542
TT
11816 {
11817 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11818 list_to_add = &global_symbols;
34eaf542 11819 else
e37fd15a 11820 list_to_add = cu->list_in_scope;
34eaf542 11821 }
c906108c
SS
11822 break;
11823 }
e142c38c 11824 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11825 if (attr)
11826 {
e7c27a73 11827 var_decode_location (attr, sym, cu);
e142c38c 11828 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11829 if (SYMBOL_CLASS (sym) == LOC_STATIC
11830 && SYMBOL_VALUE_ADDRESS (sym) == 0
11831 && !dwarf2_per_objfile->has_section_at_zero)
11832 {
11833 /* When a static variable is eliminated by the linker,
11834 the corresponding debug information is not stripped
11835 out, but the variable address is set to null;
11836 do not add such variables into symbol table. */
11837 }
11838 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11839 {
f55ee35c
JK
11840 /* Workaround gfortran PR debug/40040 - it uses
11841 DW_AT_location for variables in -fPIC libraries which may
11842 get overriden by other libraries/executable and get
11843 a different address. Resolve it by the minimal symbol
11844 which may come from inferior's executable using copy
11845 relocation. Make this workaround only for gfortran as for
11846 other compilers GDB cannot guess the minimal symbol
11847 Fortran mangling kind. */
11848 if (cu->language == language_fortran && die->parent
11849 && die->parent->tag == DW_TAG_module
11850 && cu->producer
11851 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11852 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11853
1c809c68
TT
11854 /* A variable with DW_AT_external is never static,
11855 but it may be block-scoped. */
11856 list_to_add = (cu->list_in_scope == &file_symbols
11857 ? &global_symbols : cu->list_in_scope);
1c809c68 11858 }
c906108c 11859 else
e37fd15a 11860 list_to_add = cu->list_in_scope;
c906108c
SS
11861 }
11862 else
11863 {
11864 /* We do not know the address of this symbol.
c5aa993b
JM
11865 If it is an external symbol and we have type information
11866 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11867 The address of the variable will then be determined from
11868 the minimal symbol table whenever the variable is
11869 referenced. */
e142c38c 11870 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11871 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11872 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11873 {
0fe7935b
DJ
11874 /* A variable with DW_AT_external is never static, but it
11875 may be block-scoped. */
11876 list_to_add = (cu->list_in_scope == &file_symbols
11877 ? &global_symbols : cu->list_in_scope);
11878
c906108c 11879 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11880 }
442ddf59
JK
11881 else if (!die_is_declaration (die, cu))
11882 {
11883 /* Use the default LOC_OPTIMIZED_OUT class. */
11884 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11885 if (!suppress_add)
11886 list_to_add = cu->list_in_scope;
442ddf59 11887 }
c906108c
SS
11888 }
11889 break;
11890 case DW_TAG_formal_parameter:
edb3359d
DJ
11891 /* If we are inside a function, mark this as an argument. If
11892 not, we might be looking at an argument to an inlined function
11893 when we do not have enough information to show inlined frames;
11894 pretend it's a local variable in that case so that the user can
11895 still see it. */
11896 if (context_stack_depth > 0
11897 && context_stack[context_stack_depth - 1].name != NULL)
11898 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11899 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11900 if (attr)
11901 {
e7c27a73 11902 var_decode_location (attr, sym, cu);
c906108c 11903 }
e142c38c 11904 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11905 if (attr)
11906 {
e7c27a73 11907 dwarf2_const_value (attr, sym, cu);
c906108c 11908 }
f346a30d 11909
e37fd15a 11910 list_to_add = cu->list_in_scope;
c906108c
SS
11911 break;
11912 case DW_TAG_unspecified_parameters:
11913 /* From varargs functions; gdb doesn't seem to have any
11914 interest in this information, so just ignore it for now.
11915 (FIXME?) */
11916 break;
34eaf542
TT
11917 case DW_TAG_template_type_param:
11918 suppress_add = 1;
11919 /* Fall through. */
c906108c 11920 case DW_TAG_class_type:
680b30c7 11921 case DW_TAG_interface_type:
c906108c
SS
11922 case DW_TAG_structure_type:
11923 case DW_TAG_union_type:
72019c9c 11924 case DW_TAG_set_type:
c906108c
SS
11925 case DW_TAG_enumeration_type:
11926 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11927 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11928
63d06c5c 11929 {
987504bb 11930 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11931 really ever be static objects: otherwise, if you try
11932 to, say, break of a class's method and you're in a file
11933 which doesn't mention that class, it won't work unless
11934 the check for all static symbols in lookup_symbol_aux
11935 saves you. See the OtherFileClass tests in
11936 gdb.c++/namespace.exp. */
11937
e37fd15a 11938 if (!suppress_add)
34eaf542 11939 {
34eaf542
TT
11940 list_to_add = (cu->list_in_scope == &file_symbols
11941 && (cu->language == language_cplus
11942 || cu->language == language_java)
11943 ? &global_symbols : cu->list_in_scope);
63d06c5c 11944
64382290
TT
11945 /* The semantics of C++ state that "struct foo {
11946 ... }" also defines a typedef for "foo". A Java
11947 class declaration also defines a typedef for the
11948 class. */
11949 if (cu->language == language_cplus
11950 || cu->language == language_java
11951 || cu->language == language_ada)
11952 {
11953 /* The symbol's name is already allocated along
11954 with this objfile, so we don't need to
11955 duplicate it for the type. */
11956 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11957 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11958 }
63d06c5c
DC
11959 }
11960 }
c906108c
SS
11961 break;
11962 case DW_TAG_typedef:
63d06c5c
DC
11963 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11964 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11965 list_to_add = cu->list_in_scope;
63d06c5c 11966 break;
c906108c 11967 case DW_TAG_base_type:
a02abb62 11968 case DW_TAG_subrange_type:
c906108c 11969 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11970 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11971 list_to_add = cu->list_in_scope;
c906108c
SS
11972 break;
11973 case DW_TAG_enumerator:
e142c38c 11974 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11975 if (attr)
11976 {
e7c27a73 11977 dwarf2_const_value (attr, sym, cu);
c906108c 11978 }
63d06c5c
DC
11979 {
11980 /* NOTE: carlton/2003-11-10: See comment above in the
11981 DW_TAG_class_type, etc. block. */
11982
e142c38c 11983 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11984 && (cu->language == language_cplus
11985 || cu->language == language_java)
e142c38c 11986 ? &global_symbols : cu->list_in_scope);
63d06c5c 11987 }
c906108c 11988 break;
5c4e30ca
DC
11989 case DW_TAG_namespace:
11990 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11991 list_to_add = &global_symbols;
5c4e30ca 11992 break;
c906108c
SS
11993 default:
11994 /* Not a tag we recognize. Hopefully we aren't processing
11995 trash data, but since we must specifically ignore things
11996 we don't recognize, there is nothing else we should do at
0963b4bd 11997 this point. */
e2e0b3e5 11998 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11999 dwarf_tag_name (die->tag));
c906108c
SS
12000 break;
12001 }
df8a16a1 12002
e37fd15a
SW
12003 if (suppress_add)
12004 {
12005 sym->hash_next = objfile->template_symbols;
12006 objfile->template_symbols = sym;
12007 list_to_add = NULL;
12008 }
12009
12010 if (list_to_add != NULL)
12011 add_symbol_to_list (sym, list_to_add);
12012
df8a16a1
DJ
12013 /* For the benefit of old versions of GCC, check for anonymous
12014 namespaces based on the demangled name. */
12015 if (!processing_has_namespace_info
94af9270 12016 && cu->language == language_cplus)
a10964d1 12017 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12018 }
12019 return (sym);
12020}
12021
34eaf542
TT
12022/* A wrapper for new_symbol_full that always allocates a new symbol. */
12023
12024static struct symbol *
12025new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12026{
12027 return new_symbol_full (die, type, cu, NULL);
12028}
12029
98bfdba5
PA
12030/* Given an attr with a DW_FORM_dataN value in host byte order,
12031 zero-extend it as appropriate for the symbol's type. The DWARF
12032 standard (v4) is not entirely clear about the meaning of using
12033 DW_FORM_dataN for a constant with a signed type, where the type is
12034 wider than the data. The conclusion of a discussion on the DWARF
12035 list was that this is unspecified. We choose to always zero-extend
12036 because that is the interpretation long in use by GCC. */
c906108c 12037
98bfdba5
PA
12038static gdb_byte *
12039dwarf2_const_value_data (struct attribute *attr, struct type *type,
12040 const char *name, struct obstack *obstack,
12041 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12042{
e7c27a73 12043 struct objfile *objfile = cu->objfile;
e17a4113
UW
12044 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12045 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12046 LONGEST l = DW_UNSND (attr);
12047
12048 if (bits < sizeof (*value) * 8)
12049 {
12050 l &= ((LONGEST) 1 << bits) - 1;
12051 *value = l;
12052 }
12053 else if (bits == sizeof (*value) * 8)
12054 *value = l;
12055 else
12056 {
12057 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12058 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12059 return bytes;
12060 }
12061
12062 return NULL;
12063}
12064
12065/* Read a constant value from an attribute. Either set *VALUE, or if
12066 the value does not fit in *VALUE, set *BYTES - either already
12067 allocated on the objfile obstack, or newly allocated on OBSTACK,
12068 or, set *BATON, if we translated the constant to a location
12069 expression. */
12070
12071static void
12072dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12073 const char *name, struct obstack *obstack,
12074 struct dwarf2_cu *cu,
12075 long *value, gdb_byte **bytes,
12076 struct dwarf2_locexpr_baton **baton)
12077{
12078 struct objfile *objfile = cu->objfile;
12079 struct comp_unit_head *cu_header = &cu->header;
c906108c 12080 struct dwarf_block *blk;
98bfdba5
PA
12081 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12082 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12083
12084 *value = 0;
12085 *bytes = NULL;
12086 *baton = NULL;
c906108c
SS
12087
12088 switch (attr->form)
12089 {
12090 case DW_FORM_addr:
ac56253d 12091 {
ac56253d
TT
12092 gdb_byte *data;
12093
98bfdba5
PA
12094 if (TYPE_LENGTH (type) != cu_header->addr_size)
12095 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12096 cu_header->addr_size,
98bfdba5 12097 TYPE_LENGTH (type));
ac56253d
TT
12098 /* Symbols of this form are reasonably rare, so we just
12099 piggyback on the existing location code rather than writing
12100 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12101 *baton = obstack_alloc (&objfile->objfile_obstack,
12102 sizeof (struct dwarf2_locexpr_baton));
12103 (*baton)->per_cu = cu->per_cu;
12104 gdb_assert ((*baton)->per_cu);
ac56253d 12105
98bfdba5
PA
12106 (*baton)->size = 2 + cu_header->addr_size;
12107 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12108 (*baton)->data = data;
ac56253d
TT
12109
12110 data[0] = DW_OP_addr;
12111 store_unsigned_integer (&data[1], cu_header->addr_size,
12112 byte_order, DW_ADDR (attr));
12113 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12114 }
c906108c 12115 break;
4ac36638 12116 case DW_FORM_string:
93b5768b 12117 case DW_FORM_strp:
98bfdba5
PA
12118 /* DW_STRING is already allocated on the objfile obstack, point
12119 directly to it. */
12120 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12121 break;
c906108c
SS
12122 case DW_FORM_block1:
12123 case DW_FORM_block2:
12124 case DW_FORM_block4:
12125 case DW_FORM_block:
2dc7f7b3 12126 case DW_FORM_exprloc:
c906108c 12127 blk = DW_BLOCK (attr);
98bfdba5
PA
12128 if (TYPE_LENGTH (type) != blk->size)
12129 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12130 TYPE_LENGTH (type));
12131 *bytes = blk->data;
c906108c 12132 break;
2df3850c
JM
12133
12134 /* The DW_AT_const_value attributes are supposed to carry the
12135 symbol's value "represented as it would be on the target
12136 architecture." By the time we get here, it's already been
12137 converted to host endianness, so we just need to sign- or
12138 zero-extend it as appropriate. */
12139 case DW_FORM_data1:
3e43a32a
MS
12140 *bytes = dwarf2_const_value_data (attr, type, name,
12141 obstack, cu, value, 8);
2df3850c 12142 break;
c906108c 12143 case DW_FORM_data2:
3e43a32a
MS
12144 *bytes = dwarf2_const_value_data (attr, type, name,
12145 obstack, cu, value, 16);
2df3850c 12146 break;
c906108c 12147 case DW_FORM_data4:
3e43a32a
MS
12148 *bytes = dwarf2_const_value_data (attr, type, name,
12149 obstack, cu, value, 32);
2df3850c 12150 break;
c906108c 12151 case DW_FORM_data8:
3e43a32a
MS
12152 *bytes = dwarf2_const_value_data (attr, type, name,
12153 obstack, cu, value, 64);
2df3850c
JM
12154 break;
12155
c906108c 12156 case DW_FORM_sdata:
98bfdba5 12157 *value = DW_SND (attr);
2df3850c
JM
12158 break;
12159
c906108c 12160 case DW_FORM_udata:
98bfdba5 12161 *value = DW_UNSND (attr);
c906108c 12162 break;
2df3850c 12163
c906108c 12164 default:
4d3c2250 12165 complaint (&symfile_complaints,
e2e0b3e5 12166 _("unsupported const value attribute form: '%s'"),
4d3c2250 12167 dwarf_form_name (attr->form));
98bfdba5 12168 *value = 0;
c906108c
SS
12169 break;
12170 }
12171}
12172
2df3850c 12173
98bfdba5
PA
12174/* Copy constant value from an attribute to a symbol. */
12175
2df3850c 12176static void
98bfdba5
PA
12177dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12178 struct dwarf2_cu *cu)
2df3850c 12179{
98bfdba5
PA
12180 struct objfile *objfile = cu->objfile;
12181 struct comp_unit_head *cu_header = &cu->header;
12182 long value;
12183 gdb_byte *bytes;
12184 struct dwarf2_locexpr_baton *baton;
2df3850c 12185
98bfdba5
PA
12186 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12187 SYMBOL_PRINT_NAME (sym),
12188 &objfile->objfile_obstack, cu,
12189 &value, &bytes, &baton);
2df3850c 12190
98bfdba5
PA
12191 if (baton != NULL)
12192 {
12193 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12194 SYMBOL_LOCATION_BATON (sym) = baton;
12195 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12196 }
12197 else if (bytes != NULL)
12198 {
12199 SYMBOL_VALUE_BYTES (sym) = bytes;
12200 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12201 }
12202 else
12203 {
12204 SYMBOL_VALUE (sym) = value;
12205 SYMBOL_CLASS (sym) = LOC_CONST;
12206 }
2df3850c
JM
12207}
12208
c906108c
SS
12209/* Return the type of the die in question using its DW_AT_type attribute. */
12210
12211static struct type *
e7c27a73 12212die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12213{
c906108c 12214 struct attribute *type_attr;
c906108c 12215
e142c38c 12216 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12217 if (!type_attr)
12218 {
12219 /* A missing DW_AT_type represents a void type. */
46bf5051 12220 return objfile_type (cu->objfile)->builtin_void;
c906108c 12221 }
348e048f 12222
673bfd45 12223 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12224}
12225
b4ba55a1
JB
12226/* True iff CU's producer generates GNAT Ada auxiliary information
12227 that allows to find parallel types through that information instead
12228 of having to do expensive parallel lookups by type name. */
12229
12230static int
12231need_gnat_info (struct dwarf2_cu *cu)
12232{
12233 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12234 of GNAT produces this auxiliary information, without any indication
12235 that it is produced. Part of enhancing the FSF version of GNAT
12236 to produce that information will be to put in place an indicator
12237 that we can use in order to determine whether the descriptive type
12238 info is available or not. One suggestion that has been made is
12239 to use a new attribute, attached to the CU die. For now, assume
12240 that the descriptive type info is not available. */
12241 return 0;
12242}
12243
b4ba55a1
JB
12244/* Return the auxiliary type of the die in question using its
12245 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12246 attribute is not present. */
12247
12248static struct type *
12249die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12250{
b4ba55a1 12251 struct attribute *type_attr;
b4ba55a1
JB
12252
12253 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12254 if (!type_attr)
12255 return NULL;
12256
673bfd45 12257 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12258}
12259
12260/* If DIE has a descriptive_type attribute, then set the TYPE's
12261 descriptive type accordingly. */
12262
12263static void
12264set_descriptive_type (struct type *type, struct die_info *die,
12265 struct dwarf2_cu *cu)
12266{
12267 struct type *descriptive_type = die_descriptive_type (die, cu);
12268
12269 if (descriptive_type)
12270 {
12271 ALLOCATE_GNAT_AUX_TYPE (type);
12272 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12273 }
12274}
12275
c906108c
SS
12276/* Return the containing type of the die in question using its
12277 DW_AT_containing_type attribute. */
12278
12279static struct type *
e7c27a73 12280die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12281{
c906108c 12282 struct attribute *type_attr;
c906108c 12283
e142c38c 12284 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12285 if (!type_attr)
12286 error (_("Dwarf Error: Problem turning containing type into gdb type "
12287 "[in module %s]"), cu->objfile->name);
12288
673bfd45 12289 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12290}
12291
673bfd45
DE
12292/* Look up the type of DIE in CU using its type attribute ATTR.
12293 If there is no type substitute an error marker. */
12294
c906108c 12295static struct type *
673bfd45
DE
12296lookup_die_type (struct die_info *die, struct attribute *attr,
12297 struct dwarf2_cu *cu)
c906108c 12298{
f792889a
DJ
12299 struct type *this_type;
12300
673bfd45
DE
12301 /* First see if we have it cached. */
12302
12303 if (is_ref_attr (attr))
12304 {
12305 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12306
12307 this_type = get_die_type_at_offset (offset, cu->per_cu);
12308 }
55f1336d 12309 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12310 {
12311 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12312 struct dwarf2_cu *sig_cu;
12313 unsigned int offset;
12314
12315 /* sig_type will be NULL if the signatured type is missing from
12316 the debug info. */
12317 if (sig_type == NULL)
12318 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12319 "at 0x%x [in module %s]"),
12320 die->offset, cu->objfile->name);
12321
8b70b953 12322 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 12323 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12324 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12325 }
12326 else
12327 {
12328 dump_die_for_error (die);
12329 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12330 dwarf_attr_name (attr->name), cu->objfile->name);
12331 }
12332
12333 /* If not cached we need to read it in. */
12334
12335 if (this_type == NULL)
12336 {
12337 struct die_info *type_die;
12338 struct dwarf2_cu *type_cu = cu;
12339
12340 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12341 /* If the type is cached, we should have found it above. */
12342 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12343 this_type = read_type_die_1 (type_die, type_cu);
12344 }
12345
12346 /* If we still don't have a type use an error marker. */
12347
12348 if (this_type == NULL)
c906108c 12349 {
b00fdb78
TT
12350 char *message, *saved;
12351
12352 /* read_type_die already issued a complaint. */
12353 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12354 cu->objfile->name,
12355 cu->header.offset,
12356 die->offset);
12357 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12358 message, strlen (message));
12359 xfree (message);
12360
12361 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12362 }
673bfd45 12363
f792889a 12364 return this_type;
c906108c
SS
12365}
12366
673bfd45
DE
12367/* Return the type in DIE, CU.
12368 Returns NULL for invalid types.
12369
12370 This first does a lookup in the appropriate type_hash table,
12371 and only reads the die in if necessary.
12372
12373 NOTE: This can be called when reading in partial or full symbols. */
12374
f792889a 12375static struct type *
e7c27a73 12376read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12377{
f792889a
DJ
12378 struct type *this_type;
12379
12380 this_type = get_die_type (die, cu);
12381 if (this_type)
12382 return this_type;
12383
673bfd45
DE
12384 return read_type_die_1 (die, cu);
12385}
12386
12387/* Read the type in DIE, CU.
12388 Returns NULL for invalid types. */
12389
12390static struct type *
12391read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12392{
12393 struct type *this_type = NULL;
12394
c906108c
SS
12395 switch (die->tag)
12396 {
12397 case DW_TAG_class_type:
680b30c7 12398 case DW_TAG_interface_type:
c906108c
SS
12399 case DW_TAG_structure_type:
12400 case DW_TAG_union_type:
f792889a 12401 this_type = read_structure_type (die, cu);
c906108c
SS
12402 break;
12403 case DW_TAG_enumeration_type:
f792889a 12404 this_type = read_enumeration_type (die, cu);
c906108c
SS
12405 break;
12406 case DW_TAG_subprogram:
12407 case DW_TAG_subroutine_type:
edb3359d 12408 case DW_TAG_inlined_subroutine:
f792889a 12409 this_type = read_subroutine_type (die, cu);
c906108c
SS
12410 break;
12411 case DW_TAG_array_type:
f792889a 12412 this_type = read_array_type (die, cu);
c906108c 12413 break;
72019c9c 12414 case DW_TAG_set_type:
f792889a 12415 this_type = read_set_type (die, cu);
72019c9c 12416 break;
c906108c 12417 case DW_TAG_pointer_type:
f792889a 12418 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12419 break;
12420 case DW_TAG_ptr_to_member_type:
f792889a 12421 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12422 break;
12423 case DW_TAG_reference_type:
f792889a 12424 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12425 break;
12426 case DW_TAG_const_type:
f792889a 12427 this_type = read_tag_const_type (die, cu);
c906108c
SS
12428 break;
12429 case DW_TAG_volatile_type:
f792889a 12430 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12431 break;
12432 case DW_TAG_string_type:
f792889a 12433 this_type = read_tag_string_type (die, cu);
c906108c
SS
12434 break;
12435 case DW_TAG_typedef:
f792889a 12436 this_type = read_typedef (die, cu);
c906108c 12437 break;
a02abb62 12438 case DW_TAG_subrange_type:
f792889a 12439 this_type = read_subrange_type (die, cu);
a02abb62 12440 break;
c906108c 12441 case DW_TAG_base_type:
f792889a 12442 this_type = read_base_type (die, cu);
c906108c 12443 break;
81a17f79 12444 case DW_TAG_unspecified_type:
f792889a 12445 this_type = read_unspecified_type (die, cu);
81a17f79 12446 break;
0114d602
DJ
12447 case DW_TAG_namespace:
12448 this_type = read_namespace_type (die, cu);
12449 break;
f55ee35c
JK
12450 case DW_TAG_module:
12451 this_type = read_module_type (die, cu);
12452 break;
c906108c 12453 default:
3e43a32a
MS
12454 complaint (&symfile_complaints,
12455 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12456 dwarf_tag_name (die->tag));
c906108c
SS
12457 break;
12458 }
63d06c5c 12459
f792889a 12460 return this_type;
63d06c5c
DC
12461}
12462
abc72ce4
DE
12463/* See if we can figure out if the class lives in a namespace. We do
12464 this by looking for a member function; its demangled name will
12465 contain namespace info, if there is any.
12466 Return the computed name or NULL.
12467 Space for the result is allocated on the objfile's obstack.
12468 This is the full-die version of guess_partial_die_structure_name.
12469 In this case we know DIE has no useful parent. */
12470
12471static char *
12472guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12473{
12474 struct die_info *spec_die;
12475 struct dwarf2_cu *spec_cu;
12476 struct die_info *child;
12477
12478 spec_cu = cu;
12479 spec_die = die_specification (die, &spec_cu);
12480 if (spec_die != NULL)
12481 {
12482 die = spec_die;
12483 cu = spec_cu;
12484 }
12485
12486 for (child = die->child;
12487 child != NULL;
12488 child = child->sibling)
12489 {
12490 if (child->tag == DW_TAG_subprogram)
12491 {
12492 struct attribute *attr;
12493
12494 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12495 if (attr == NULL)
12496 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12497 if (attr != NULL)
12498 {
12499 char *actual_name
12500 = language_class_name_from_physname (cu->language_defn,
12501 DW_STRING (attr));
12502 char *name = NULL;
12503
12504 if (actual_name != NULL)
12505 {
12506 char *die_name = dwarf2_name (die, cu);
12507
12508 if (die_name != NULL
12509 && strcmp (die_name, actual_name) != 0)
12510 {
12511 /* Strip off the class name from the full name.
12512 We want the prefix. */
12513 int die_name_len = strlen (die_name);
12514 int actual_name_len = strlen (actual_name);
12515
12516 /* Test for '::' as a sanity check. */
12517 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12518 && actual_name[actual_name_len
12519 - die_name_len - 1] == ':')
abc72ce4
DE
12520 name =
12521 obsavestring (actual_name,
12522 actual_name_len - die_name_len - 2,
12523 &cu->objfile->objfile_obstack);
12524 }
12525 }
12526 xfree (actual_name);
12527 return name;
12528 }
12529 }
12530 }
12531
12532 return NULL;
12533}
12534
96408a79
SA
12535/* GCC might emit a nameless typedef that has a linkage name. Determine the
12536 prefix part in such case. See
12537 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12538
12539static char *
12540anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12541{
12542 struct attribute *attr;
12543 char *base;
12544
12545 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12546 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12547 return NULL;
12548
12549 attr = dwarf2_attr (die, DW_AT_name, cu);
12550 if (attr != NULL && DW_STRING (attr) != NULL)
12551 return NULL;
12552
12553 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12554 if (attr == NULL)
12555 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12556 if (attr == NULL || DW_STRING (attr) == NULL)
12557 return NULL;
12558
12559 /* dwarf2_name had to be already called. */
12560 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12561
12562 /* Strip the base name, keep any leading namespaces/classes. */
12563 base = strrchr (DW_STRING (attr), ':');
12564 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12565 return "";
12566
12567 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12568 &cu->objfile->objfile_obstack);
12569}
12570
fdde2d81 12571/* Return the name of the namespace/class that DIE is defined within,
0114d602 12572 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12573
0114d602
DJ
12574 For example, if we're within the method foo() in the following
12575 code:
12576
12577 namespace N {
12578 class C {
12579 void foo () {
12580 }
12581 };
12582 }
12583
12584 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12585
12586static char *
e142c38c 12587determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12588{
0114d602
DJ
12589 struct die_info *parent, *spec_die;
12590 struct dwarf2_cu *spec_cu;
12591 struct type *parent_type;
96408a79 12592 char *retval;
63d06c5c 12593
f55ee35c
JK
12594 if (cu->language != language_cplus && cu->language != language_java
12595 && cu->language != language_fortran)
0114d602
DJ
12596 return "";
12597
96408a79
SA
12598 retval = anonymous_struct_prefix (die, cu);
12599 if (retval)
12600 return retval;
12601
0114d602
DJ
12602 /* We have to be careful in the presence of DW_AT_specification.
12603 For example, with GCC 3.4, given the code
12604
12605 namespace N {
12606 void foo() {
12607 // Definition of N::foo.
12608 }
12609 }
12610
12611 then we'll have a tree of DIEs like this:
12612
12613 1: DW_TAG_compile_unit
12614 2: DW_TAG_namespace // N
12615 3: DW_TAG_subprogram // declaration of N::foo
12616 4: DW_TAG_subprogram // definition of N::foo
12617 DW_AT_specification // refers to die #3
12618
12619 Thus, when processing die #4, we have to pretend that we're in
12620 the context of its DW_AT_specification, namely the contex of die
12621 #3. */
12622 spec_cu = cu;
12623 spec_die = die_specification (die, &spec_cu);
12624 if (spec_die == NULL)
12625 parent = die->parent;
12626 else
63d06c5c 12627 {
0114d602
DJ
12628 parent = spec_die->parent;
12629 cu = spec_cu;
63d06c5c 12630 }
0114d602
DJ
12631
12632 if (parent == NULL)
12633 return "";
98bfdba5
PA
12634 else if (parent->building_fullname)
12635 {
12636 const char *name;
12637 const char *parent_name;
12638
12639 /* It has been seen on RealView 2.2 built binaries,
12640 DW_TAG_template_type_param types actually _defined_ as
12641 children of the parent class:
12642
12643 enum E {};
12644 template class <class Enum> Class{};
12645 Class<enum E> class_e;
12646
12647 1: DW_TAG_class_type (Class)
12648 2: DW_TAG_enumeration_type (E)
12649 3: DW_TAG_enumerator (enum1:0)
12650 3: DW_TAG_enumerator (enum2:1)
12651 ...
12652 2: DW_TAG_template_type_param
12653 DW_AT_type DW_FORM_ref_udata (E)
12654
12655 Besides being broken debug info, it can put GDB into an
12656 infinite loop. Consider:
12657
12658 When we're building the full name for Class<E>, we'll start
12659 at Class, and go look over its template type parameters,
12660 finding E. We'll then try to build the full name of E, and
12661 reach here. We're now trying to build the full name of E,
12662 and look over the parent DIE for containing scope. In the
12663 broken case, if we followed the parent DIE of E, we'd again
12664 find Class, and once again go look at its template type
12665 arguments, etc., etc. Simply don't consider such parent die
12666 as source-level parent of this die (it can't be, the language
12667 doesn't allow it), and break the loop here. */
12668 name = dwarf2_name (die, cu);
12669 parent_name = dwarf2_name (parent, cu);
12670 complaint (&symfile_complaints,
12671 _("template param type '%s' defined within parent '%s'"),
12672 name ? name : "<unknown>",
12673 parent_name ? parent_name : "<unknown>");
12674 return "";
12675 }
63d06c5c 12676 else
0114d602
DJ
12677 switch (parent->tag)
12678 {
63d06c5c 12679 case DW_TAG_namespace:
0114d602 12680 parent_type = read_type_die (parent, cu);
acebe513
UW
12681 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12682 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12683 Work around this problem here. */
12684 if (cu->language == language_cplus
12685 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12686 return "";
0114d602
DJ
12687 /* We give a name to even anonymous namespaces. */
12688 return TYPE_TAG_NAME (parent_type);
63d06c5c 12689 case DW_TAG_class_type:
680b30c7 12690 case DW_TAG_interface_type:
63d06c5c 12691 case DW_TAG_structure_type:
0114d602 12692 case DW_TAG_union_type:
f55ee35c 12693 case DW_TAG_module:
0114d602
DJ
12694 parent_type = read_type_die (parent, cu);
12695 if (TYPE_TAG_NAME (parent_type) != NULL)
12696 return TYPE_TAG_NAME (parent_type);
12697 else
12698 /* An anonymous structure is only allowed non-static data
12699 members; no typedefs, no member functions, et cetera.
12700 So it does not need a prefix. */
12701 return "";
abc72ce4
DE
12702 case DW_TAG_compile_unit:
12703 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12704 if (cu->language == language_cplus
8b70b953 12705 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12706 && die->child != NULL
12707 && (die->tag == DW_TAG_class_type
12708 || die->tag == DW_TAG_structure_type
12709 || die->tag == DW_TAG_union_type))
12710 {
12711 char *name = guess_full_die_structure_name (die, cu);
12712 if (name != NULL)
12713 return name;
12714 }
12715 return "";
63d06c5c 12716 default:
8176b9b8 12717 return determine_prefix (parent, cu);
63d06c5c 12718 }
63d06c5c
DC
12719}
12720
3e43a32a
MS
12721/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12722 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12723 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12724 an obconcat, otherwise allocate storage for the result. The CU argument is
12725 used to determine the language and hence, the appropriate separator. */
987504bb 12726
f55ee35c 12727#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12728
12729static char *
f55ee35c
JK
12730typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12731 int physname, struct dwarf2_cu *cu)
63d06c5c 12732{
f55ee35c 12733 const char *lead = "";
5c315b68 12734 const char *sep;
63d06c5c 12735
3e43a32a
MS
12736 if (suffix == NULL || suffix[0] == '\0'
12737 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12738 sep = "";
12739 else if (cu->language == language_java)
12740 sep = ".";
f55ee35c
JK
12741 else if (cu->language == language_fortran && physname)
12742 {
12743 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12744 DW_AT_MIPS_linkage_name is preferred and used instead. */
12745
12746 lead = "__";
12747 sep = "_MOD_";
12748 }
987504bb
JJ
12749 else
12750 sep = "::";
63d06c5c 12751
6dd47d34
DE
12752 if (prefix == NULL)
12753 prefix = "";
12754 if (suffix == NULL)
12755 suffix = "";
12756
987504bb
JJ
12757 if (obs == NULL)
12758 {
3e43a32a
MS
12759 char *retval
12760 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12761
f55ee35c
JK
12762 strcpy (retval, lead);
12763 strcat (retval, prefix);
6dd47d34
DE
12764 strcat (retval, sep);
12765 strcat (retval, suffix);
63d06c5c
DC
12766 return retval;
12767 }
987504bb
JJ
12768 else
12769 {
12770 /* We have an obstack. */
f55ee35c 12771 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12772 }
63d06c5c
DC
12773}
12774
c906108c
SS
12775/* Return sibling of die, NULL if no sibling. */
12776
f9aca02d 12777static struct die_info *
fba45db2 12778sibling_die (struct die_info *die)
c906108c 12779{
639d11d3 12780 return die->sibling;
c906108c
SS
12781}
12782
71c25dea
TT
12783/* Get name of a die, return NULL if not found. */
12784
12785static char *
12786dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12787 struct obstack *obstack)
12788{
12789 if (name && cu->language == language_cplus)
12790 {
12791 char *canon_name = cp_canonicalize_string (name);
12792
12793 if (canon_name != NULL)
12794 {
12795 if (strcmp (canon_name, name) != 0)
12796 name = obsavestring (canon_name, strlen (canon_name),
12797 obstack);
12798 xfree (canon_name);
12799 }
12800 }
12801
12802 return name;
c906108c
SS
12803}
12804
9219021c
DC
12805/* Get name of a die, return NULL if not found. */
12806
12807static char *
e142c38c 12808dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12809{
12810 struct attribute *attr;
12811
e142c38c 12812 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12813 if ((!attr || !DW_STRING (attr))
12814 && die->tag != DW_TAG_class_type
12815 && die->tag != DW_TAG_interface_type
12816 && die->tag != DW_TAG_structure_type
12817 && die->tag != DW_TAG_union_type)
71c25dea
TT
12818 return NULL;
12819
12820 switch (die->tag)
12821 {
12822 case DW_TAG_compile_unit:
12823 /* Compilation units have a DW_AT_name that is a filename, not
12824 a source language identifier. */
12825 case DW_TAG_enumeration_type:
12826 case DW_TAG_enumerator:
12827 /* These tags always have simple identifiers already; no need
12828 to canonicalize them. */
12829 return DW_STRING (attr);
907af001 12830
418835cc
KS
12831 case DW_TAG_subprogram:
12832 /* Java constructors will all be named "<init>", so return
12833 the class name when we see this special case. */
12834 if (cu->language == language_java
12835 && DW_STRING (attr) != NULL
12836 && strcmp (DW_STRING (attr), "<init>") == 0)
12837 {
12838 struct dwarf2_cu *spec_cu = cu;
12839 struct die_info *spec_die;
12840
12841 /* GCJ will output '<init>' for Java constructor names.
12842 For this special case, return the name of the parent class. */
12843
12844 /* GCJ may output suprogram DIEs with AT_specification set.
12845 If so, use the name of the specified DIE. */
12846 spec_die = die_specification (die, &spec_cu);
12847 if (spec_die != NULL)
12848 return dwarf2_name (spec_die, spec_cu);
12849
12850 do
12851 {
12852 die = die->parent;
12853 if (die->tag == DW_TAG_class_type)
12854 return dwarf2_name (die, cu);
12855 }
12856 while (die->tag != DW_TAG_compile_unit);
12857 }
907af001
UW
12858 break;
12859
12860 case DW_TAG_class_type:
12861 case DW_TAG_interface_type:
12862 case DW_TAG_structure_type:
12863 case DW_TAG_union_type:
12864 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12865 structures or unions. These were of the form "._%d" in GCC 4.1,
12866 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12867 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12868 if (attr && DW_STRING (attr)
12869 && (strncmp (DW_STRING (attr), "._", 2) == 0
12870 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12871 return NULL;
53832f31
TT
12872
12873 /* GCC might emit a nameless typedef that has a linkage name. See
12874 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12875 if (!attr || DW_STRING (attr) == NULL)
12876 {
df5c6c50 12877 char *demangled = NULL;
53832f31
TT
12878
12879 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12880 if (attr == NULL)
12881 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12882
12883 if (attr == NULL || DW_STRING (attr) == NULL)
12884 return NULL;
12885
df5c6c50
JK
12886 /* Avoid demangling DW_STRING (attr) the second time on a second
12887 call for the same DIE. */
12888 if (!DW_STRING_IS_CANONICAL (attr))
12889 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12890
12891 if (demangled)
12892 {
96408a79
SA
12893 char *base;
12894
53832f31 12895 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12896 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12897 &cu->objfile->objfile_obstack);
53832f31
TT
12898 DW_STRING_IS_CANONICAL (attr) = 1;
12899 xfree (demangled);
96408a79
SA
12900
12901 /* Strip any leading namespaces/classes, keep only the base name.
12902 DW_AT_name for named DIEs does not contain the prefixes. */
12903 base = strrchr (DW_STRING (attr), ':');
12904 if (base && base > DW_STRING (attr) && base[-1] == ':')
12905 return &base[1];
12906 else
12907 return DW_STRING (attr);
53832f31
TT
12908 }
12909 }
907af001
UW
12910 break;
12911
71c25dea 12912 default:
907af001
UW
12913 break;
12914 }
12915
12916 if (!DW_STRING_IS_CANONICAL (attr))
12917 {
12918 DW_STRING (attr)
12919 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12920 &cu->objfile->objfile_obstack);
12921 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12922 }
907af001 12923 return DW_STRING (attr);
9219021c
DC
12924}
12925
12926/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12927 is none. *EXT_CU is the CU containing DIE on input, and the CU
12928 containing the return value on output. */
9219021c
DC
12929
12930static struct die_info *
f2f0e013 12931dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12932{
12933 struct attribute *attr;
9219021c 12934
f2f0e013 12935 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12936 if (attr == NULL)
12937 return NULL;
12938
f2f0e013 12939 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12940}
12941
c906108c
SS
12942/* Convert a DIE tag into its string name. */
12943
12944static char *
aa1ee363 12945dwarf_tag_name (unsigned tag)
c906108c
SS
12946{
12947 switch (tag)
12948 {
12949 case DW_TAG_padding:
12950 return "DW_TAG_padding";
12951 case DW_TAG_array_type:
12952 return "DW_TAG_array_type";
12953 case DW_TAG_class_type:
12954 return "DW_TAG_class_type";
12955 case DW_TAG_entry_point:
12956 return "DW_TAG_entry_point";
12957 case DW_TAG_enumeration_type:
12958 return "DW_TAG_enumeration_type";
12959 case DW_TAG_formal_parameter:
12960 return "DW_TAG_formal_parameter";
12961 case DW_TAG_imported_declaration:
12962 return "DW_TAG_imported_declaration";
12963 case DW_TAG_label:
12964 return "DW_TAG_label";
12965 case DW_TAG_lexical_block:
12966 return "DW_TAG_lexical_block";
12967 case DW_TAG_member:
12968 return "DW_TAG_member";
12969 case DW_TAG_pointer_type:
12970 return "DW_TAG_pointer_type";
12971 case DW_TAG_reference_type:
12972 return "DW_TAG_reference_type";
12973 case DW_TAG_compile_unit:
12974 return "DW_TAG_compile_unit";
12975 case DW_TAG_string_type:
12976 return "DW_TAG_string_type";
12977 case DW_TAG_structure_type:
12978 return "DW_TAG_structure_type";
12979 case DW_TAG_subroutine_type:
12980 return "DW_TAG_subroutine_type";
12981 case DW_TAG_typedef:
12982 return "DW_TAG_typedef";
12983 case DW_TAG_union_type:
12984 return "DW_TAG_union_type";
12985 case DW_TAG_unspecified_parameters:
12986 return "DW_TAG_unspecified_parameters";
12987 case DW_TAG_variant:
12988 return "DW_TAG_variant";
12989 case DW_TAG_common_block:
12990 return "DW_TAG_common_block";
12991 case DW_TAG_common_inclusion:
12992 return "DW_TAG_common_inclusion";
12993 case DW_TAG_inheritance:
12994 return "DW_TAG_inheritance";
12995 case DW_TAG_inlined_subroutine:
12996 return "DW_TAG_inlined_subroutine";
12997 case DW_TAG_module:
12998 return "DW_TAG_module";
12999 case DW_TAG_ptr_to_member_type:
13000 return "DW_TAG_ptr_to_member_type";
13001 case DW_TAG_set_type:
13002 return "DW_TAG_set_type";
13003 case DW_TAG_subrange_type:
13004 return "DW_TAG_subrange_type";
13005 case DW_TAG_with_stmt:
13006 return "DW_TAG_with_stmt";
13007 case DW_TAG_access_declaration:
13008 return "DW_TAG_access_declaration";
13009 case DW_TAG_base_type:
13010 return "DW_TAG_base_type";
13011 case DW_TAG_catch_block:
13012 return "DW_TAG_catch_block";
13013 case DW_TAG_const_type:
13014 return "DW_TAG_const_type";
13015 case DW_TAG_constant:
13016 return "DW_TAG_constant";
13017 case DW_TAG_enumerator:
13018 return "DW_TAG_enumerator";
13019 case DW_TAG_file_type:
13020 return "DW_TAG_file_type";
13021 case DW_TAG_friend:
13022 return "DW_TAG_friend";
13023 case DW_TAG_namelist:
13024 return "DW_TAG_namelist";
13025 case DW_TAG_namelist_item:
13026 return "DW_TAG_namelist_item";
13027 case DW_TAG_packed_type:
13028 return "DW_TAG_packed_type";
13029 case DW_TAG_subprogram:
13030 return "DW_TAG_subprogram";
13031 case DW_TAG_template_type_param:
13032 return "DW_TAG_template_type_param";
13033 case DW_TAG_template_value_param:
13034 return "DW_TAG_template_value_param";
13035 case DW_TAG_thrown_type:
13036 return "DW_TAG_thrown_type";
13037 case DW_TAG_try_block:
13038 return "DW_TAG_try_block";
13039 case DW_TAG_variant_part:
13040 return "DW_TAG_variant_part";
13041 case DW_TAG_variable:
13042 return "DW_TAG_variable";
13043 case DW_TAG_volatile_type:
13044 return "DW_TAG_volatile_type";
d9fa45fe
DC
13045 case DW_TAG_dwarf_procedure:
13046 return "DW_TAG_dwarf_procedure";
13047 case DW_TAG_restrict_type:
13048 return "DW_TAG_restrict_type";
13049 case DW_TAG_interface_type:
13050 return "DW_TAG_interface_type";
13051 case DW_TAG_namespace:
13052 return "DW_TAG_namespace";
13053 case DW_TAG_imported_module:
13054 return "DW_TAG_imported_module";
13055 case DW_TAG_unspecified_type:
13056 return "DW_TAG_unspecified_type";
13057 case DW_TAG_partial_unit:
13058 return "DW_TAG_partial_unit";
13059 case DW_TAG_imported_unit:
13060 return "DW_TAG_imported_unit";
b7619582
GF
13061 case DW_TAG_condition:
13062 return "DW_TAG_condition";
13063 case DW_TAG_shared_type:
13064 return "DW_TAG_shared_type";
348e048f
DE
13065 case DW_TAG_type_unit:
13066 return "DW_TAG_type_unit";
c906108c
SS
13067 case DW_TAG_MIPS_loop:
13068 return "DW_TAG_MIPS_loop";
b7619582
GF
13069 case DW_TAG_HP_array_descriptor:
13070 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13071 case DW_TAG_format_label:
13072 return "DW_TAG_format_label";
13073 case DW_TAG_function_template:
13074 return "DW_TAG_function_template";
13075 case DW_TAG_class_template:
13076 return "DW_TAG_class_template";
b7619582
GF
13077 case DW_TAG_GNU_BINCL:
13078 return "DW_TAG_GNU_BINCL";
13079 case DW_TAG_GNU_EINCL:
13080 return "DW_TAG_GNU_EINCL";
13081 case DW_TAG_upc_shared_type:
13082 return "DW_TAG_upc_shared_type";
13083 case DW_TAG_upc_strict_type:
13084 return "DW_TAG_upc_strict_type";
13085 case DW_TAG_upc_relaxed_type:
13086 return "DW_TAG_upc_relaxed_type";
13087 case DW_TAG_PGI_kanji_type:
13088 return "DW_TAG_PGI_kanji_type";
13089 case DW_TAG_PGI_interface_block:
13090 return "DW_TAG_PGI_interface_block";
96408a79
SA
13091 case DW_TAG_GNU_call_site:
13092 return "DW_TAG_GNU_call_site";
c906108c
SS
13093 default:
13094 return "DW_TAG_<unknown>";
13095 }
13096}
13097
13098/* Convert a DWARF attribute code into its string name. */
13099
13100static char *
aa1ee363 13101dwarf_attr_name (unsigned attr)
c906108c
SS
13102{
13103 switch (attr)
13104 {
13105 case DW_AT_sibling:
13106 return "DW_AT_sibling";
13107 case DW_AT_location:
13108 return "DW_AT_location";
13109 case DW_AT_name:
13110 return "DW_AT_name";
13111 case DW_AT_ordering:
13112 return "DW_AT_ordering";
13113 case DW_AT_subscr_data:
13114 return "DW_AT_subscr_data";
13115 case DW_AT_byte_size:
13116 return "DW_AT_byte_size";
13117 case DW_AT_bit_offset:
13118 return "DW_AT_bit_offset";
13119 case DW_AT_bit_size:
13120 return "DW_AT_bit_size";
13121 case DW_AT_element_list:
13122 return "DW_AT_element_list";
13123 case DW_AT_stmt_list:
13124 return "DW_AT_stmt_list";
13125 case DW_AT_low_pc:
13126 return "DW_AT_low_pc";
13127 case DW_AT_high_pc:
13128 return "DW_AT_high_pc";
13129 case DW_AT_language:
13130 return "DW_AT_language";
13131 case DW_AT_member:
13132 return "DW_AT_member";
13133 case DW_AT_discr:
13134 return "DW_AT_discr";
13135 case DW_AT_discr_value:
13136 return "DW_AT_discr_value";
13137 case DW_AT_visibility:
13138 return "DW_AT_visibility";
13139 case DW_AT_import:
13140 return "DW_AT_import";
13141 case DW_AT_string_length:
13142 return "DW_AT_string_length";
13143 case DW_AT_common_reference:
13144 return "DW_AT_common_reference";
13145 case DW_AT_comp_dir:
13146 return "DW_AT_comp_dir";
13147 case DW_AT_const_value:
13148 return "DW_AT_const_value";
13149 case DW_AT_containing_type:
13150 return "DW_AT_containing_type";
13151 case DW_AT_default_value:
13152 return "DW_AT_default_value";
13153 case DW_AT_inline:
13154 return "DW_AT_inline";
13155 case DW_AT_is_optional:
13156 return "DW_AT_is_optional";
13157 case DW_AT_lower_bound:
13158 return "DW_AT_lower_bound";
13159 case DW_AT_producer:
13160 return "DW_AT_producer";
13161 case DW_AT_prototyped:
13162 return "DW_AT_prototyped";
13163 case DW_AT_return_addr:
13164 return "DW_AT_return_addr";
13165 case DW_AT_start_scope:
13166 return "DW_AT_start_scope";
09fa0d7c
JK
13167 case DW_AT_bit_stride:
13168 return "DW_AT_bit_stride";
c906108c
SS
13169 case DW_AT_upper_bound:
13170 return "DW_AT_upper_bound";
13171 case DW_AT_abstract_origin:
13172 return "DW_AT_abstract_origin";
13173 case DW_AT_accessibility:
13174 return "DW_AT_accessibility";
13175 case DW_AT_address_class:
13176 return "DW_AT_address_class";
13177 case DW_AT_artificial:
13178 return "DW_AT_artificial";
13179 case DW_AT_base_types:
13180 return "DW_AT_base_types";
13181 case DW_AT_calling_convention:
13182 return "DW_AT_calling_convention";
13183 case DW_AT_count:
13184 return "DW_AT_count";
13185 case DW_AT_data_member_location:
13186 return "DW_AT_data_member_location";
13187 case DW_AT_decl_column:
13188 return "DW_AT_decl_column";
13189 case DW_AT_decl_file:
13190 return "DW_AT_decl_file";
13191 case DW_AT_decl_line:
13192 return "DW_AT_decl_line";
13193 case DW_AT_declaration:
13194 return "DW_AT_declaration";
13195 case DW_AT_discr_list:
13196 return "DW_AT_discr_list";
13197 case DW_AT_encoding:
13198 return "DW_AT_encoding";
13199 case DW_AT_external:
13200 return "DW_AT_external";
13201 case DW_AT_frame_base:
13202 return "DW_AT_frame_base";
13203 case DW_AT_friend:
13204 return "DW_AT_friend";
13205 case DW_AT_identifier_case:
13206 return "DW_AT_identifier_case";
13207 case DW_AT_macro_info:
13208 return "DW_AT_macro_info";
13209 case DW_AT_namelist_items:
13210 return "DW_AT_namelist_items";
13211 case DW_AT_priority:
13212 return "DW_AT_priority";
13213 case DW_AT_segment:
13214 return "DW_AT_segment";
13215 case DW_AT_specification:
13216 return "DW_AT_specification";
13217 case DW_AT_static_link:
13218 return "DW_AT_static_link";
13219 case DW_AT_type:
13220 return "DW_AT_type";
13221 case DW_AT_use_location:
13222 return "DW_AT_use_location";
13223 case DW_AT_variable_parameter:
13224 return "DW_AT_variable_parameter";
13225 case DW_AT_virtuality:
13226 return "DW_AT_virtuality";
13227 case DW_AT_vtable_elem_location:
13228 return "DW_AT_vtable_elem_location";
b7619582 13229 /* DWARF 3 values. */
d9fa45fe
DC
13230 case DW_AT_allocated:
13231 return "DW_AT_allocated";
13232 case DW_AT_associated:
13233 return "DW_AT_associated";
13234 case DW_AT_data_location:
13235 return "DW_AT_data_location";
09fa0d7c
JK
13236 case DW_AT_byte_stride:
13237 return "DW_AT_byte_stride";
d9fa45fe
DC
13238 case DW_AT_entry_pc:
13239 return "DW_AT_entry_pc";
13240 case DW_AT_use_UTF8:
13241 return "DW_AT_use_UTF8";
13242 case DW_AT_extension:
13243 return "DW_AT_extension";
13244 case DW_AT_ranges:
13245 return "DW_AT_ranges";
13246 case DW_AT_trampoline:
13247 return "DW_AT_trampoline";
13248 case DW_AT_call_column:
13249 return "DW_AT_call_column";
13250 case DW_AT_call_file:
13251 return "DW_AT_call_file";
13252 case DW_AT_call_line:
13253 return "DW_AT_call_line";
b7619582
GF
13254 case DW_AT_description:
13255 return "DW_AT_description";
13256 case DW_AT_binary_scale:
13257 return "DW_AT_binary_scale";
13258 case DW_AT_decimal_scale:
13259 return "DW_AT_decimal_scale";
13260 case DW_AT_small:
13261 return "DW_AT_small";
13262 case DW_AT_decimal_sign:
13263 return "DW_AT_decimal_sign";
13264 case DW_AT_digit_count:
13265 return "DW_AT_digit_count";
13266 case DW_AT_picture_string:
13267 return "DW_AT_picture_string";
13268 case DW_AT_mutable:
13269 return "DW_AT_mutable";
13270 case DW_AT_threads_scaled:
13271 return "DW_AT_threads_scaled";
13272 case DW_AT_explicit:
13273 return "DW_AT_explicit";
13274 case DW_AT_object_pointer:
13275 return "DW_AT_object_pointer";
13276 case DW_AT_endianity:
13277 return "DW_AT_endianity";
13278 case DW_AT_elemental:
13279 return "DW_AT_elemental";
13280 case DW_AT_pure:
13281 return "DW_AT_pure";
13282 case DW_AT_recursive:
13283 return "DW_AT_recursive";
348e048f
DE
13284 /* DWARF 4 values. */
13285 case DW_AT_signature:
13286 return "DW_AT_signature";
31ef98ae
TT
13287 case DW_AT_linkage_name:
13288 return "DW_AT_linkage_name";
b7619582 13289 /* SGI/MIPS extensions. */
c764a876 13290#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13291 case DW_AT_MIPS_fde:
13292 return "DW_AT_MIPS_fde";
c764a876 13293#endif
c906108c
SS
13294 case DW_AT_MIPS_loop_begin:
13295 return "DW_AT_MIPS_loop_begin";
13296 case DW_AT_MIPS_tail_loop_begin:
13297 return "DW_AT_MIPS_tail_loop_begin";
13298 case DW_AT_MIPS_epilog_begin:
13299 return "DW_AT_MIPS_epilog_begin";
13300 case DW_AT_MIPS_loop_unroll_factor:
13301 return "DW_AT_MIPS_loop_unroll_factor";
13302 case DW_AT_MIPS_software_pipeline_depth:
13303 return "DW_AT_MIPS_software_pipeline_depth";
13304 case DW_AT_MIPS_linkage_name:
13305 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13306 case DW_AT_MIPS_stride:
13307 return "DW_AT_MIPS_stride";
13308 case DW_AT_MIPS_abstract_name:
13309 return "DW_AT_MIPS_abstract_name";
13310 case DW_AT_MIPS_clone_origin:
13311 return "DW_AT_MIPS_clone_origin";
13312 case DW_AT_MIPS_has_inlines:
13313 return "DW_AT_MIPS_has_inlines";
b7619582 13314 /* HP extensions. */
c764a876 13315#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13316 case DW_AT_HP_block_index:
13317 return "DW_AT_HP_block_index";
c764a876 13318#endif
b7619582
GF
13319 case DW_AT_HP_unmodifiable:
13320 return "DW_AT_HP_unmodifiable";
13321 case DW_AT_HP_actuals_stmt_list:
13322 return "DW_AT_HP_actuals_stmt_list";
13323 case DW_AT_HP_proc_per_section:
13324 return "DW_AT_HP_proc_per_section";
13325 case DW_AT_HP_raw_data_ptr:
13326 return "DW_AT_HP_raw_data_ptr";
13327 case DW_AT_HP_pass_by_reference:
13328 return "DW_AT_HP_pass_by_reference";
13329 case DW_AT_HP_opt_level:
13330 return "DW_AT_HP_opt_level";
13331 case DW_AT_HP_prof_version_id:
13332 return "DW_AT_HP_prof_version_id";
13333 case DW_AT_HP_opt_flags:
13334 return "DW_AT_HP_opt_flags";
13335 case DW_AT_HP_cold_region_low_pc:
13336 return "DW_AT_HP_cold_region_low_pc";
13337 case DW_AT_HP_cold_region_high_pc:
13338 return "DW_AT_HP_cold_region_high_pc";
13339 case DW_AT_HP_all_variables_modifiable:
13340 return "DW_AT_HP_all_variables_modifiable";
13341 case DW_AT_HP_linkage_name:
13342 return "DW_AT_HP_linkage_name";
13343 case DW_AT_HP_prof_flags:
13344 return "DW_AT_HP_prof_flags";
13345 /* GNU extensions. */
c906108c
SS
13346 case DW_AT_sf_names:
13347 return "DW_AT_sf_names";
13348 case DW_AT_src_info:
13349 return "DW_AT_src_info";
13350 case DW_AT_mac_info:
13351 return "DW_AT_mac_info";
13352 case DW_AT_src_coords:
13353 return "DW_AT_src_coords";
13354 case DW_AT_body_begin:
13355 return "DW_AT_body_begin";
13356 case DW_AT_body_end:
13357 return "DW_AT_body_end";
f5f8a009
EZ
13358 case DW_AT_GNU_vector:
13359 return "DW_AT_GNU_vector";
2de00c64
DE
13360 case DW_AT_GNU_odr_signature:
13361 return "DW_AT_GNU_odr_signature";
b7619582
GF
13362 /* VMS extensions. */
13363 case DW_AT_VMS_rtnbeg_pd_address:
13364 return "DW_AT_VMS_rtnbeg_pd_address";
13365 /* UPC extension. */
13366 case DW_AT_upc_threads_scaled:
13367 return "DW_AT_upc_threads_scaled";
13368 /* PGI (STMicroelectronics) extensions. */
13369 case DW_AT_PGI_lbase:
13370 return "DW_AT_PGI_lbase";
13371 case DW_AT_PGI_soffset:
13372 return "DW_AT_PGI_soffset";
13373 case DW_AT_PGI_lstride:
13374 return "DW_AT_PGI_lstride";
c906108c
SS
13375 default:
13376 return "DW_AT_<unknown>";
13377 }
13378}
13379
13380/* Convert a DWARF value form code into its string name. */
13381
13382static char *
aa1ee363 13383dwarf_form_name (unsigned form)
c906108c
SS
13384{
13385 switch (form)
13386 {
13387 case DW_FORM_addr:
13388 return "DW_FORM_addr";
13389 case DW_FORM_block2:
13390 return "DW_FORM_block2";
13391 case DW_FORM_block4:
13392 return "DW_FORM_block4";
13393 case DW_FORM_data2:
13394 return "DW_FORM_data2";
13395 case DW_FORM_data4:
13396 return "DW_FORM_data4";
13397 case DW_FORM_data8:
13398 return "DW_FORM_data8";
13399 case DW_FORM_string:
13400 return "DW_FORM_string";
13401 case DW_FORM_block:
13402 return "DW_FORM_block";
13403 case DW_FORM_block1:
13404 return "DW_FORM_block1";
13405 case DW_FORM_data1:
13406 return "DW_FORM_data1";
13407 case DW_FORM_flag:
13408 return "DW_FORM_flag";
13409 case DW_FORM_sdata:
13410 return "DW_FORM_sdata";
13411 case DW_FORM_strp:
13412 return "DW_FORM_strp";
13413 case DW_FORM_udata:
13414 return "DW_FORM_udata";
13415 case DW_FORM_ref_addr:
13416 return "DW_FORM_ref_addr";
13417 case DW_FORM_ref1:
13418 return "DW_FORM_ref1";
13419 case DW_FORM_ref2:
13420 return "DW_FORM_ref2";
13421 case DW_FORM_ref4:
13422 return "DW_FORM_ref4";
13423 case DW_FORM_ref8:
13424 return "DW_FORM_ref8";
13425 case DW_FORM_ref_udata:
13426 return "DW_FORM_ref_udata";
13427 case DW_FORM_indirect:
13428 return "DW_FORM_indirect";
348e048f
DE
13429 case DW_FORM_sec_offset:
13430 return "DW_FORM_sec_offset";
13431 case DW_FORM_exprloc:
13432 return "DW_FORM_exprloc";
13433 case DW_FORM_flag_present:
13434 return "DW_FORM_flag_present";
55f1336d
TT
13435 case DW_FORM_ref_sig8:
13436 return "DW_FORM_ref_sig8";
c906108c
SS
13437 default:
13438 return "DW_FORM_<unknown>";
13439 }
13440}
13441
13442/* Convert a DWARF stack opcode into its string name. */
13443
9eae7c52 13444const char *
b1bfef65 13445dwarf_stack_op_name (unsigned op)
c906108c
SS
13446{
13447 switch (op)
13448 {
13449 case DW_OP_addr:
13450 return "DW_OP_addr";
13451 case DW_OP_deref:
13452 return "DW_OP_deref";
13453 case DW_OP_const1u:
13454 return "DW_OP_const1u";
13455 case DW_OP_const1s:
13456 return "DW_OP_const1s";
13457 case DW_OP_const2u:
13458 return "DW_OP_const2u";
13459 case DW_OP_const2s:
13460 return "DW_OP_const2s";
13461 case DW_OP_const4u:
13462 return "DW_OP_const4u";
13463 case DW_OP_const4s:
13464 return "DW_OP_const4s";
13465 case DW_OP_const8u:
13466 return "DW_OP_const8u";
13467 case DW_OP_const8s:
13468 return "DW_OP_const8s";
13469 case DW_OP_constu:
13470 return "DW_OP_constu";
13471 case DW_OP_consts:
13472 return "DW_OP_consts";
13473 case DW_OP_dup:
13474 return "DW_OP_dup";
13475 case DW_OP_drop:
13476 return "DW_OP_drop";
13477 case DW_OP_over:
13478 return "DW_OP_over";
13479 case DW_OP_pick:
13480 return "DW_OP_pick";
13481 case DW_OP_swap:
13482 return "DW_OP_swap";
13483 case DW_OP_rot:
13484 return "DW_OP_rot";
13485 case DW_OP_xderef:
13486 return "DW_OP_xderef";
13487 case DW_OP_abs:
13488 return "DW_OP_abs";
13489 case DW_OP_and:
13490 return "DW_OP_and";
13491 case DW_OP_div:
13492 return "DW_OP_div";
13493 case DW_OP_minus:
13494 return "DW_OP_minus";
13495 case DW_OP_mod:
13496 return "DW_OP_mod";
13497 case DW_OP_mul:
13498 return "DW_OP_mul";
13499 case DW_OP_neg:
13500 return "DW_OP_neg";
13501 case DW_OP_not:
13502 return "DW_OP_not";
13503 case DW_OP_or:
13504 return "DW_OP_or";
13505 case DW_OP_plus:
13506 return "DW_OP_plus";
13507 case DW_OP_plus_uconst:
13508 return "DW_OP_plus_uconst";
13509 case DW_OP_shl:
13510 return "DW_OP_shl";
13511 case DW_OP_shr:
13512 return "DW_OP_shr";
13513 case DW_OP_shra:
13514 return "DW_OP_shra";
13515 case DW_OP_xor:
13516 return "DW_OP_xor";
13517 case DW_OP_bra:
13518 return "DW_OP_bra";
13519 case DW_OP_eq:
13520 return "DW_OP_eq";
13521 case DW_OP_ge:
13522 return "DW_OP_ge";
13523 case DW_OP_gt:
13524 return "DW_OP_gt";
13525 case DW_OP_le:
13526 return "DW_OP_le";
13527 case DW_OP_lt:
13528 return "DW_OP_lt";
13529 case DW_OP_ne:
13530 return "DW_OP_ne";
13531 case DW_OP_skip:
13532 return "DW_OP_skip";
13533 case DW_OP_lit0:
13534 return "DW_OP_lit0";
13535 case DW_OP_lit1:
13536 return "DW_OP_lit1";
13537 case DW_OP_lit2:
13538 return "DW_OP_lit2";
13539 case DW_OP_lit3:
13540 return "DW_OP_lit3";
13541 case DW_OP_lit4:
13542 return "DW_OP_lit4";
13543 case DW_OP_lit5:
13544 return "DW_OP_lit5";
13545 case DW_OP_lit6:
13546 return "DW_OP_lit6";
13547 case DW_OP_lit7:
13548 return "DW_OP_lit7";
13549 case DW_OP_lit8:
13550 return "DW_OP_lit8";
13551 case DW_OP_lit9:
13552 return "DW_OP_lit9";
13553 case DW_OP_lit10:
13554 return "DW_OP_lit10";
13555 case DW_OP_lit11:
13556 return "DW_OP_lit11";
13557 case DW_OP_lit12:
13558 return "DW_OP_lit12";
13559 case DW_OP_lit13:
13560 return "DW_OP_lit13";
13561 case DW_OP_lit14:
13562 return "DW_OP_lit14";
13563 case DW_OP_lit15:
13564 return "DW_OP_lit15";
13565 case DW_OP_lit16:
13566 return "DW_OP_lit16";
13567 case DW_OP_lit17:
13568 return "DW_OP_lit17";
13569 case DW_OP_lit18:
13570 return "DW_OP_lit18";
13571 case DW_OP_lit19:
13572 return "DW_OP_lit19";
13573 case DW_OP_lit20:
13574 return "DW_OP_lit20";
13575 case DW_OP_lit21:
13576 return "DW_OP_lit21";
13577 case DW_OP_lit22:
13578 return "DW_OP_lit22";
13579 case DW_OP_lit23:
13580 return "DW_OP_lit23";
13581 case DW_OP_lit24:
13582 return "DW_OP_lit24";
13583 case DW_OP_lit25:
13584 return "DW_OP_lit25";
13585 case DW_OP_lit26:
13586 return "DW_OP_lit26";
13587 case DW_OP_lit27:
13588 return "DW_OP_lit27";
13589 case DW_OP_lit28:
13590 return "DW_OP_lit28";
13591 case DW_OP_lit29:
13592 return "DW_OP_lit29";
13593 case DW_OP_lit30:
13594 return "DW_OP_lit30";
13595 case DW_OP_lit31:
13596 return "DW_OP_lit31";
13597 case DW_OP_reg0:
13598 return "DW_OP_reg0";
13599 case DW_OP_reg1:
13600 return "DW_OP_reg1";
13601 case DW_OP_reg2:
13602 return "DW_OP_reg2";
13603 case DW_OP_reg3:
13604 return "DW_OP_reg3";
13605 case DW_OP_reg4:
13606 return "DW_OP_reg4";
13607 case DW_OP_reg5:
13608 return "DW_OP_reg5";
13609 case DW_OP_reg6:
13610 return "DW_OP_reg6";
13611 case DW_OP_reg7:
13612 return "DW_OP_reg7";
13613 case DW_OP_reg8:
13614 return "DW_OP_reg8";
13615 case DW_OP_reg9:
13616 return "DW_OP_reg9";
13617 case DW_OP_reg10:
13618 return "DW_OP_reg10";
13619 case DW_OP_reg11:
13620 return "DW_OP_reg11";
13621 case DW_OP_reg12:
13622 return "DW_OP_reg12";
13623 case DW_OP_reg13:
13624 return "DW_OP_reg13";
13625 case DW_OP_reg14:
13626 return "DW_OP_reg14";
13627 case DW_OP_reg15:
13628 return "DW_OP_reg15";
13629 case DW_OP_reg16:
13630 return "DW_OP_reg16";
13631 case DW_OP_reg17:
13632 return "DW_OP_reg17";
13633 case DW_OP_reg18:
13634 return "DW_OP_reg18";
13635 case DW_OP_reg19:
13636 return "DW_OP_reg19";
13637 case DW_OP_reg20:
13638 return "DW_OP_reg20";
13639 case DW_OP_reg21:
13640 return "DW_OP_reg21";
13641 case DW_OP_reg22:
13642 return "DW_OP_reg22";
13643 case DW_OP_reg23:
13644 return "DW_OP_reg23";
13645 case DW_OP_reg24:
13646 return "DW_OP_reg24";
13647 case DW_OP_reg25:
13648 return "DW_OP_reg25";
13649 case DW_OP_reg26:
13650 return "DW_OP_reg26";
13651 case DW_OP_reg27:
13652 return "DW_OP_reg27";
13653 case DW_OP_reg28:
13654 return "DW_OP_reg28";
13655 case DW_OP_reg29:
13656 return "DW_OP_reg29";
13657 case DW_OP_reg30:
13658 return "DW_OP_reg30";
13659 case DW_OP_reg31:
13660 return "DW_OP_reg31";
13661 case DW_OP_breg0:
13662 return "DW_OP_breg0";
13663 case DW_OP_breg1:
13664 return "DW_OP_breg1";
13665 case DW_OP_breg2:
13666 return "DW_OP_breg2";
13667 case DW_OP_breg3:
13668 return "DW_OP_breg3";
13669 case DW_OP_breg4:
13670 return "DW_OP_breg4";
13671 case DW_OP_breg5:
13672 return "DW_OP_breg5";
13673 case DW_OP_breg6:
13674 return "DW_OP_breg6";
13675 case DW_OP_breg7:
13676 return "DW_OP_breg7";
13677 case DW_OP_breg8:
13678 return "DW_OP_breg8";
13679 case DW_OP_breg9:
13680 return "DW_OP_breg9";
13681 case DW_OP_breg10:
13682 return "DW_OP_breg10";
13683 case DW_OP_breg11:
13684 return "DW_OP_breg11";
13685 case DW_OP_breg12:
13686 return "DW_OP_breg12";
13687 case DW_OP_breg13:
13688 return "DW_OP_breg13";
13689 case DW_OP_breg14:
13690 return "DW_OP_breg14";
13691 case DW_OP_breg15:
13692 return "DW_OP_breg15";
13693 case DW_OP_breg16:
13694 return "DW_OP_breg16";
13695 case DW_OP_breg17:
13696 return "DW_OP_breg17";
13697 case DW_OP_breg18:
13698 return "DW_OP_breg18";
13699 case DW_OP_breg19:
13700 return "DW_OP_breg19";
13701 case DW_OP_breg20:
13702 return "DW_OP_breg20";
13703 case DW_OP_breg21:
13704 return "DW_OP_breg21";
13705 case DW_OP_breg22:
13706 return "DW_OP_breg22";
13707 case DW_OP_breg23:
13708 return "DW_OP_breg23";
13709 case DW_OP_breg24:
13710 return "DW_OP_breg24";
13711 case DW_OP_breg25:
13712 return "DW_OP_breg25";
13713 case DW_OP_breg26:
13714 return "DW_OP_breg26";
13715 case DW_OP_breg27:
13716 return "DW_OP_breg27";
13717 case DW_OP_breg28:
13718 return "DW_OP_breg28";
13719 case DW_OP_breg29:
13720 return "DW_OP_breg29";
13721 case DW_OP_breg30:
13722 return "DW_OP_breg30";
13723 case DW_OP_breg31:
13724 return "DW_OP_breg31";
13725 case DW_OP_regx:
13726 return "DW_OP_regx";
13727 case DW_OP_fbreg:
13728 return "DW_OP_fbreg";
13729 case DW_OP_bregx:
13730 return "DW_OP_bregx";
13731 case DW_OP_piece:
13732 return "DW_OP_piece";
13733 case DW_OP_deref_size:
13734 return "DW_OP_deref_size";
13735 case DW_OP_xderef_size:
13736 return "DW_OP_xderef_size";
13737 case DW_OP_nop:
13738 return "DW_OP_nop";
b7619582 13739 /* DWARF 3 extensions. */
ed348acc
EZ
13740 case DW_OP_push_object_address:
13741 return "DW_OP_push_object_address";
13742 case DW_OP_call2:
13743 return "DW_OP_call2";
13744 case DW_OP_call4:
13745 return "DW_OP_call4";
13746 case DW_OP_call_ref:
13747 return "DW_OP_call_ref";
b7619582
GF
13748 case DW_OP_form_tls_address:
13749 return "DW_OP_form_tls_address";
13750 case DW_OP_call_frame_cfa:
13751 return "DW_OP_call_frame_cfa";
13752 case DW_OP_bit_piece:
13753 return "DW_OP_bit_piece";
9eae7c52
TT
13754 /* DWARF 4 extensions. */
13755 case DW_OP_implicit_value:
13756 return "DW_OP_implicit_value";
13757 case DW_OP_stack_value:
13758 return "DW_OP_stack_value";
13759 /* GNU extensions. */
ed348acc
EZ
13760 case DW_OP_GNU_push_tls_address:
13761 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13762 case DW_OP_GNU_uninit:
13763 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13764 case DW_OP_GNU_implicit_pointer:
13765 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13766 case DW_OP_GNU_entry_value:
13767 return "DW_OP_GNU_entry_value";
13768 case DW_OP_GNU_const_type:
13769 return "DW_OP_GNU_const_type";
13770 case DW_OP_GNU_regval_type:
13771 return "DW_OP_GNU_regval_type";
13772 case DW_OP_GNU_deref_type:
13773 return "DW_OP_GNU_deref_type";
13774 case DW_OP_GNU_convert:
13775 return "DW_OP_GNU_convert";
13776 case DW_OP_GNU_reinterpret:
13777 return "DW_OP_GNU_reinterpret";
c906108c 13778 default:
b1bfef65 13779 return NULL;
c906108c
SS
13780 }
13781}
13782
13783static char *
fba45db2 13784dwarf_bool_name (unsigned mybool)
c906108c
SS
13785{
13786 if (mybool)
13787 return "TRUE";
13788 else
13789 return "FALSE";
13790}
13791
13792/* Convert a DWARF type code into its string name. */
13793
13794static char *
aa1ee363 13795dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13796{
13797 switch (enc)
13798 {
b7619582
GF
13799 case DW_ATE_void:
13800 return "DW_ATE_void";
c906108c
SS
13801 case DW_ATE_address:
13802 return "DW_ATE_address";
13803 case DW_ATE_boolean:
13804 return "DW_ATE_boolean";
13805 case DW_ATE_complex_float:
13806 return "DW_ATE_complex_float";
13807 case DW_ATE_float:
13808 return "DW_ATE_float";
13809 case DW_ATE_signed:
13810 return "DW_ATE_signed";
13811 case DW_ATE_signed_char:
13812 return "DW_ATE_signed_char";
13813 case DW_ATE_unsigned:
13814 return "DW_ATE_unsigned";
13815 case DW_ATE_unsigned_char:
13816 return "DW_ATE_unsigned_char";
b7619582 13817 /* DWARF 3. */
d9fa45fe
DC
13818 case DW_ATE_imaginary_float:
13819 return "DW_ATE_imaginary_float";
b7619582
GF
13820 case DW_ATE_packed_decimal:
13821 return "DW_ATE_packed_decimal";
13822 case DW_ATE_numeric_string:
13823 return "DW_ATE_numeric_string";
13824 case DW_ATE_edited:
13825 return "DW_ATE_edited";
13826 case DW_ATE_signed_fixed:
13827 return "DW_ATE_signed_fixed";
13828 case DW_ATE_unsigned_fixed:
13829 return "DW_ATE_unsigned_fixed";
13830 case DW_ATE_decimal_float:
13831 return "DW_ATE_decimal_float";
75079b2b
TT
13832 /* DWARF 4. */
13833 case DW_ATE_UTF:
13834 return "DW_ATE_UTF";
b7619582
GF
13835 /* HP extensions. */
13836 case DW_ATE_HP_float80:
13837 return "DW_ATE_HP_float80";
13838 case DW_ATE_HP_complex_float80:
13839 return "DW_ATE_HP_complex_float80";
13840 case DW_ATE_HP_float128:
13841 return "DW_ATE_HP_float128";
13842 case DW_ATE_HP_complex_float128:
13843 return "DW_ATE_HP_complex_float128";
13844 case DW_ATE_HP_floathpintel:
13845 return "DW_ATE_HP_floathpintel";
13846 case DW_ATE_HP_imaginary_float80:
13847 return "DW_ATE_HP_imaginary_float80";
13848 case DW_ATE_HP_imaginary_float128:
13849 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13850 default:
13851 return "DW_ATE_<unknown>";
13852 }
13853}
13854
0963b4bd 13855/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13856
13857#if 0
13858static char *
aa1ee363 13859dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13860{
13861 switch (cfi_opc)
13862 {
13863 case DW_CFA_advance_loc:
13864 return "DW_CFA_advance_loc";
13865 case DW_CFA_offset:
13866 return "DW_CFA_offset";
13867 case DW_CFA_restore:
13868 return "DW_CFA_restore";
13869 case DW_CFA_nop:
13870 return "DW_CFA_nop";
13871 case DW_CFA_set_loc:
13872 return "DW_CFA_set_loc";
13873 case DW_CFA_advance_loc1:
13874 return "DW_CFA_advance_loc1";
13875 case DW_CFA_advance_loc2:
13876 return "DW_CFA_advance_loc2";
13877 case DW_CFA_advance_loc4:
13878 return "DW_CFA_advance_loc4";
13879 case DW_CFA_offset_extended:
13880 return "DW_CFA_offset_extended";
13881 case DW_CFA_restore_extended:
13882 return "DW_CFA_restore_extended";
13883 case DW_CFA_undefined:
13884 return "DW_CFA_undefined";
13885 case DW_CFA_same_value:
13886 return "DW_CFA_same_value";
13887 case DW_CFA_register:
13888 return "DW_CFA_register";
13889 case DW_CFA_remember_state:
13890 return "DW_CFA_remember_state";
13891 case DW_CFA_restore_state:
13892 return "DW_CFA_restore_state";
13893 case DW_CFA_def_cfa:
13894 return "DW_CFA_def_cfa";
13895 case DW_CFA_def_cfa_register:
13896 return "DW_CFA_def_cfa_register";
13897 case DW_CFA_def_cfa_offset:
13898 return "DW_CFA_def_cfa_offset";
b7619582 13899 /* DWARF 3. */
985cb1a3
JM
13900 case DW_CFA_def_cfa_expression:
13901 return "DW_CFA_def_cfa_expression";
13902 case DW_CFA_expression:
13903 return "DW_CFA_expression";
13904 case DW_CFA_offset_extended_sf:
13905 return "DW_CFA_offset_extended_sf";
13906 case DW_CFA_def_cfa_sf:
13907 return "DW_CFA_def_cfa_sf";
13908 case DW_CFA_def_cfa_offset_sf:
13909 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13910 case DW_CFA_val_offset:
13911 return "DW_CFA_val_offset";
13912 case DW_CFA_val_offset_sf:
13913 return "DW_CFA_val_offset_sf";
13914 case DW_CFA_val_expression:
13915 return "DW_CFA_val_expression";
13916 /* SGI/MIPS specific. */
c906108c
SS
13917 case DW_CFA_MIPS_advance_loc8:
13918 return "DW_CFA_MIPS_advance_loc8";
b7619582 13919 /* GNU extensions. */
985cb1a3
JM
13920 case DW_CFA_GNU_window_save:
13921 return "DW_CFA_GNU_window_save";
13922 case DW_CFA_GNU_args_size:
13923 return "DW_CFA_GNU_args_size";
13924 case DW_CFA_GNU_negative_offset_extended:
13925 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13926 default:
13927 return "DW_CFA_<unknown>";
13928 }
13929}
13930#endif
13931
f9aca02d 13932static void
d97bc12b 13933dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13934{
13935 unsigned int i;
13936
d97bc12b
DE
13937 print_spaces (indent, f);
13938 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13939 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13940
13941 if (die->parent != NULL)
13942 {
13943 print_spaces (indent, f);
13944 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13945 die->parent->offset);
13946 }
13947
13948 print_spaces (indent, f);
13949 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13950 dwarf_bool_name (die->child != NULL));
c906108c 13951
d97bc12b
DE
13952 print_spaces (indent, f);
13953 fprintf_unfiltered (f, " attributes:\n");
13954
c906108c
SS
13955 for (i = 0; i < die->num_attrs; ++i)
13956 {
d97bc12b
DE
13957 print_spaces (indent, f);
13958 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13959 dwarf_attr_name (die->attrs[i].name),
13960 dwarf_form_name (die->attrs[i].form));
d97bc12b 13961
c906108c
SS
13962 switch (die->attrs[i].form)
13963 {
13964 case DW_FORM_ref_addr:
13965 case DW_FORM_addr:
d97bc12b 13966 fprintf_unfiltered (f, "address: ");
5af949e3 13967 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13968 break;
13969 case DW_FORM_block2:
13970 case DW_FORM_block4:
13971 case DW_FORM_block:
13972 case DW_FORM_block1:
3e43a32a
MS
13973 fprintf_unfiltered (f, "block: size %d",
13974 DW_BLOCK (&die->attrs[i])->size);
c906108c 13975 break;
2dc7f7b3
TT
13976 case DW_FORM_exprloc:
13977 fprintf_unfiltered (f, "expression: size %u",
13978 DW_BLOCK (&die->attrs[i])->size);
13979 break;
10b3939b
DJ
13980 case DW_FORM_ref1:
13981 case DW_FORM_ref2:
13982 case DW_FORM_ref4:
d97bc12b 13983 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13984 (long) (DW_ADDR (&die->attrs[i])));
13985 break;
c906108c
SS
13986 case DW_FORM_data1:
13987 case DW_FORM_data2:
13988 case DW_FORM_data4:
ce5d95e1 13989 case DW_FORM_data8:
c906108c
SS
13990 case DW_FORM_udata:
13991 case DW_FORM_sdata:
43bbcdc2
PH
13992 fprintf_unfiltered (f, "constant: %s",
13993 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13994 break;
2dc7f7b3
TT
13995 case DW_FORM_sec_offset:
13996 fprintf_unfiltered (f, "section offset: %s",
13997 pulongest (DW_UNSND (&die->attrs[i])));
13998 break;
55f1336d 13999 case DW_FORM_ref_sig8:
348e048f
DE
14000 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14001 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 14002 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
14003 else
14004 fprintf_unfiltered (f, "signatured type, offset: unknown");
14005 break;
c906108c 14006 case DW_FORM_string:
4bdf3d34 14007 case DW_FORM_strp:
8285870a 14008 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14009 DW_STRING (&die->attrs[i])
8285870a
JK
14010 ? DW_STRING (&die->attrs[i]) : "",
14011 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14012 break;
14013 case DW_FORM_flag:
14014 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14015 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14016 else
d97bc12b 14017 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14018 break;
2dc7f7b3
TT
14019 case DW_FORM_flag_present:
14020 fprintf_unfiltered (f, "flag: TRUE");
14021 break;
a8329558 14022 case DW_FORM_indirect:
0963b4bd
MS
14023 /* The reader will have reduced the indirect form to
14024 the "base form" so this form should not occur. */
3e43a32a
MS
14025 fprintf_unfiltered (f,
14026 "unexpected attribute form: DW_FORM_indirect");
a8329558 14027 break;
c906108c 14028 default:
d97bc12b 14029 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14030 die->attrs[i].form);
d97bc12b 14031 break;
c906108c 14032 }
d97bc12b 14033 fprintf_unfiltered (f, "\n");
c906108c
SS
14034 }
14035}
14036
f9aca02d 14037static void
d97bc12b 14038dump_die_for_error (struct die_info *die)
c906108c 14039{
d97bc12b
DE
14040 dump_die_shallow (gdb_stderr, 0, die);
14041}
14042
14043static void
14044dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14045{
14046 int indent = level * 4;
14047
14048 gdb_assert (die != NULL);
14049
14050 if (level >= max_level)
14051 return;
14052
14053 dump_die_shallow (f, indent, die);
14054
14055 if (die->child != NULL)
c906108c 14056 {
d97bc12b
DE
14057 print_spaces (indent, f);
14058 fprintf_unfiltered (f, " Children:");
14059 if (level + 1 < max_level)
14060 {
14061 fprintf_unfiltered (f, "\n");
14062 dump_die_1 (f, level + 1, max_level, die->child);
14063 }
14064 else
14065 {
3e43a32a
MS
14066 fprintf_unfiltered (f,
14067 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14068 }
14069 }
14070
14071 if (die->sibling != NULL && level > 0)
14072 {
14073 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14074 }
14075}
14076
d97bc12b
DE
14077/* This is called from the pdie macro in gdbinit.in.
14078 It's not static so gcc will keep a copy callable from gdb. */
14079
14080void
14081dump_die (struct die_info *die, int max_level)
14082{
14083 dump_die_1 (gdb_stdlog, 0, max_level, die);
14084}
14085
f9aca02d 14086static void
51545339 14087store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14088{
51545339 14089 void **slot;
c906108c 14090
51545339
DJ
14091 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14092
14093 *slot = die;
c906108c
SS
14094}
14095
93311388
DE
14096static int
14097is_ref_attr (struct attribute *attr)
c906108c 14098{
c906108c
SS
14099 switch (attr->form)
14100 {
14101 case DW_FORM_ref_addr:
c906108c
SS
14102 case DW_FORM_ref1:
14103 case DW_FORM_ref2:
14104 case DW_FORM_ref4:
613e1657 14105 case DW_FORM_ref8:
c906108c 14106 case DW_FORM_ref_udata:
93311388 14107 return 1;
c906108c 14108 default:
93311388 14109 return 0;
c906108c 14110 }
93311388
DE
14111}
14112
14113static unsigned int
14114dwarf2_get_ref_die_offset (struct attribute *attr)
14115{
14116 if (is_ref_attr (attr))
14117 return DW_ADDR (attr);
14118
14119 complaint (&symfile_complaints,
14120 _("unsupported die ref attribute form: '%s'"),
14121 dwarf_form_name (attr->form));
14122 return 0;
c906108c
SS
14123}
14124
43bbcdc2
PH
14125/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14126 * the value held by the attribute is not constant. */
a02abb62 14127
43bbcdc2 14128static LONGEST
a02abb62
JB
14129dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14130{
14131 if (attr->form == DW_FORM_sdata)
14132 return DW_SND (attr);
14133 else if (attr->form == DW_FORM_udata
14134 || attr->form == DW_FORM_data1
14135 || attr->form == DW_FORM_data2
14136 || attr->form == DW_FORM_data4
14137 || attr->form == DW_FORM_data8)
14138 return DW_UNSND (attr);
14139 else
14140 {
3e43a32a
MS
14141 complaint (&symfile_complaints,
14142 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14143 dwarf_form_name (attr->form));
14144 return default_value;
14145 }
14146}
14147
03dd20cc 14148/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14149 unit and add it to our queue.
14150 The result is non-zero if PER_CU was queued, otherwise the result is zero
14151 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14152
348e048f 14153static int
03dd20cc
DJ
14154maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14155 struct dwarf2_per_cu_data *per_cu)
14156{
98bfdba5
PA
14157 /* We may arrive here during partial symbol reading, if we need full
14158 DIEs to process an unusual case (e.g. template arguments). Do
14159 not queue PER_CU, just tell our caller to load its DIEs. */
14160 if (dwarf2_per_objfile->reading_partial_symbols)
14161 {
14162 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14163 return 1;
14164 return 0;
14165 }
14166
03dd20cc
DJ
14167 /* Mark the dependence relation so that we don't flush PER_CU
14168 too early. */
14169 dwarf2_add_dependence (this_cu, per_cu);
14170
14171 /* If it's already on the queue, we have nothing to do. */
14172 if (per_cu->queued)
348e048f 14173 return 0;
03dd20cc
DJ
14174
14175 /* If the compilation unit is already loaded, just mark it as
14176 used. */
14177 if (per_cu->cu != NULL)
14178 {
14179 per_cu->cu->last_used = 0;
348e048f 14180 return 0;
03dd20cc
DJ
14181 }
14182
14183 /* Add it to the queue. */
14184 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14185
14186 return 1;
14187}
14188
14189/* Follow reference or signature attribute ATTR of SRC_DIE.
14190 On entry *REF_CU is the CU of SRC_DIE.
14191 On exit *REF_CU is the CU of the result. */
14192
14193static struct die_info *
14194follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14195 struct dwarf2_cu **ref_cu)
14196{
14197 struct die_info *die;
14198
14199 if (is_ref_attr (attr))
14200 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14201 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14202 die = follow_die_sig (src_die, attr, ref_cu);
14203 else
14204 {
14205 dump_die_for_error (src_die);
14206 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14207 (*ref_cu)->objfile->name);
14208 }
14209
14210 return die;
03dd20cc
DJ
14211}
14212
5c631832 14213/* Follow reference OFFSET.
673bfd45
DE
14214 On entry *REF_CU is the CU of the source die referencing OFFSET.
14215 On exit *REF_CU is the CU of the result.
14216 Returns NULL if OFFSET is invalid. */
f504f079 14217
f9aca02d 14218static struct die_info *
5c631832 14219follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14220{
10b3939b 14221 struct die_info temp_die;
f2f0e013 14222 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14223
348e048f
DE
14224 gdb_assert (cu->per_cu != NULL);
14225
98bfdba5
PA
14226 target_cu = cu;
14227
8b70b953 14228 if (cu->per_cu->debug_type_section)
348e048f
DE
14229 {
14230 /* .debug_types CUs cannot reference anything outside their CU.
14231 If they need to, they have to reference a signatured type via
55f1336d 14232 DW_FORM_ref_sig8. */
348e048f 14233 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14234 return NULL;
348e048f
DE
14235 }
14236 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14237 {
14238 struct dwarf2_per_cu_data *per_cu;
9a619af0 14239
45452591 14240 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14241
14242 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14243 if (maybe_queue_comp_unit (cu, per_cu))
14244 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14245
10b3939b
DJ
14246 target_cu = per_cu->cu;
14247 }
98bfdba5
PA
14248 else if (cu->dies == NULL)
14249 {
14250 /* We're loading full DIEs during partial symbol reading. */
14251 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14252 load_full_comp_unit (cu->per_cu, cu->objfile);
14253 }
c906108c 14254
f2f0e013 14255 *ref_cu = target_cu;
51545339 14256 temp_die.offset = offset;
5c631832
JK
14257 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14258}
10b3939b 14259
5c631832
JK
14260/* Follow reference attribute ATTR of SRC_DIE.
14261 On entry *REF_CU is the CU of SRC_DIE.
14262 On exit *REF_CU is the CU of the result. */
14263
14264static struct die_info *
14265follow_die_ref (struct die_info *src_die, struct attribute *attr,
14266 struct dwarf2_cu **ref_cu)
14267{
14268 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14269 struct dwarf2_cu *cu = *ref_cu;
14270 struct die_info *die;
14271
14272 die = follow_die_offset (offset, ref_cu);
14273 if (!die)
14274 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14275 "at 0x%x [in module %s]"),
14276 offset, src_die->offset, cu->objfile->name);
348e048f 14277
5c631832
JK
14278 return die;
14279}
14280
d83e736b
JK
14281/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14282 Returned value is intended for DW_OP_call*. Returned
14283 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14284
14285struct dwarf2_locexpr_baton
14286dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14287 struct dwarf2_per_cu_data *per_cu,
14288 CORE_ADDR (*get_frame_pc) (void *baton),
14289 void *baton)
5c631832 14290{
918dd910 14291 struct dwarf2_cu *cu;
5c631832
JK
14292 struct die_info *die;
14293 struct attribute *attr;
14294 struct dwarf2_locexpr_baton retval;
14295
8cf6f0b1
TT
14296 dw2_setup (per_cu->objfile);
14297
918dd910
JK
14298 if (per_cu->cu == NULL)
14299 load_cu (per_cu);
14300 cu = per_cu->cu;
14301
5c631832
JK
14302 die = follow_die_offset (offset, &cu);
14303 if (!die)
14304 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14305 offset, per_cu->cu->objfile->name);
14306
14307 attr = dwarf2_attr (die, DW_AT_location, cu);
14308 if (!attr)
14309 {
e103e986
JK
14310 /* DWARF: "If there is no such attribute, then there is no effect.".
14311 DATA is ignored if SIZE is 0. */
5c631832 14312
e103e986 14313 retval.data = NULL;
5c631832
JK
14314 retval.size = 0;
14315 }
8cf6f0b1
TT
14316 else if (attr_form_is_section_offset (attr))
14317 {
14318 struct dwarf2_loclist_baton loclist_baton;
14319 CORE_ADDR pc = (*get_frame_pc) (baton);
14320 size_t size;
14321
14322 fill_in_loclist_baton (cu, &loclist_baton, attr);
14323
14324 retval.data = dwarf2_find_location_expression (&loclist_baton,
14325 &size, pc);
14326 retval.size = size;
14327 }
5c631832
JK
14328 else
14329 {
14330 if (!attr_form_is_block (attr))
14331 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14332 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14333 offset, per_cu->cu->objfile->name);
14334
14335 retval.data = DW_BLOCK (attr)->data;
14336 retval.size = DW_BLOCK (attr)->size;
14337 }
14338 retval.per_cu = cu->per_cu;
918dd910 14339
918dd910
JK
14340 age_cached_comp_units ();
14341
5c631832 14342 return retval;
348e048f
DE
14343}
14344
8a9b8146
TT
14345/* Return the type of the DIE at DIE_OFFSET in the CU named by
14346 PER_CU. */
14347
14348struct type *
14349dwarf2_get_die_type (unsigned int die_offset,
14350 struct dwarf2_per_cu_data *per_cu)
14351{
8a9b8146 14352 dw2_setup (per_cu->objfile);
9ff3b74f 14353 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14354}
14355
348e048f
DE
14356/* Follow the signature attribute ATTR in SRC_DIE.
14357 On entry *REF_CU is the CU of SRC_DIE.
14358 On exit *REF_CU is the CU of the result. */
14359
14360static struct die_info *
14361follow_die_sig (struct die_info *src_die, struct attribute *attr,
14362 struct dwarf2_cu **ref_cu)
14363{
14364 struct objfile *objfile = (*ref_cu)->objfile;
14365 struct die_info temp_die;
14366 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14367 struct dwarf2_cu *sig_cu;
14368 struct die_info *die;
14369
14370 /* sig_type will be NULL if the signatured type is missing from
14371 the debug info. */
14372 if (sig_type == NULL)
14373 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14374 "at 0x%x [in module %s]"),
14375 src_die->offset, objfile->name);
14376
14377 /* If necessary, add it to the queue and load its DIEs. */
14378
14379 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14380 read_signatured_type (objfile, sig_type);
14381
14382 gdb_assert (sig_type->per_cu.cu != NULL);
14383
14384 sig_cu = sig_type->per_cu.cu;
14385 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14386 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14387 if (die)
14388 {
14389 *ref_cu = sig_cu;
14390 return die;
14391 }
14392
3e43a32a
MS
14393 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14394 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14395 sig_type->type_offset, src_die->offset, objfile->name);
14396}
14397
14398/* Given an offset of a signatured type, return its signatured_type. */
14399
14400static struct signatured_type *
8b70b953
TT
14401lookup_signatured_type_at_offset (struct objfile *objfile,
14402 struct dwarf2_section_info *section,
14403 unsigned int offset)
348e048f 14404{
8b70b953 14405 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14406 unsigned int length, initial_length_size;
14407 unsigned int sig_offset;
14408 struct signatured_type find_entry, *type_sig;
14409
14410 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14411 sig_offset = (initial_length_size
14412 + 2 /*version*/
14413 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14414 + 1 /*address_size*/);
14415 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14416 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14417
14418 /* This is only used to lookup previously recorded types.
14419 If we didn't find it, it's our bug. */
14420 gdb_assert (type_sig != NULL);
b3c8eb43 14421 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14422
14423 return type_sig;
14424}
14425
14426/* Read in signatured type at OFFSET and build its CU and die(s). */
14427
14428static void
14429read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14430 struct dwarf2_section_info *sect,
348e048f
DE
14431 unsigned int offset)
14432{
14433 struct signatured_type *type_sig;
14434
8b70b953 14435 dwarf2_read_section (objfile, sect);
be391dca 14436
348e048f
DE
14437 /* We have the section offset, but we need the signature to do the
14438 hash table lookup. */
8b70b953 14439 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14440
14441 gdb_assert (type_sig->per_cu.cu == NULL);
14442
14443 read_signatured_type (objfile, type_sig);
14444
14445 gdb_assert (type_sig->per_cu.cu != NULL);
14446}
14447
14448/* Read in a signatured type and build its CU and DIEs. */
14449
14450static void
14451read_signatured_type (struct objfile *objfile,
14452 struct signatured_type *type_sig)
14453{
1fd400ff 14454 gdb_byte *types_ptr;
348e048f
DE
14455 struct die_reader_specs reader_specs;
14456 struct dwarf2_cu *cu;
14457 ULONGEST signature;
14458 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14459 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14460
8b70b953
TT
14461 dwarf2_read_section (objfile, section);
14462 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14463
348e048f
DE
14464 gdb_assert (type_sig->per_cu.cu == NULL);
14465
9816fde3
JK
14466 cu = xmalloc (sizeof (*cu));
14467 init_one_comp_unit (cu, objfile);
14468
348e048f
DE
14469 type_sig->per_cu.cu = cu;
14470 cu->per_cu = &type_sig->per_cu;
14471
14472 /* If an error occurs while loading, release our storage. */
14473 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14474
8b70b953 14475 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14476 types_ptr, objfile->obfd);
14477 gdb_assert (signature == type_sig->signature);
14478
14479 cu->die_hash
14480 = htab_create_alloc_ex (cu->header.length / 12,
14481 die_hash,
14482 die_eq,
14483 NULL,
14484 &cu->comp_unit_obstack,
14485 hashtab_obstack_allocate,
14486 dummy_obstack_deallocate);
14487
14488 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14489 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14490
14491 init_cu_die_reader (&reader_specs, cu);
14492
14493 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14494 NULL /*parent*/);
14495
14496 /* We try not to read any attributes in this function, because not
14497 all objfiles needed for references have been loaded yet, and symbol
14498 table processing isn't initialized. But we have to set the CU language,
14499 or we won't be able to build types correctly. */
9816fde3 14500 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14501
14502 do_cleanups (back_to);
14503
14504 /* We've successfully allocated this compilation unit. Let our caller
14505 clean it up when finished with it. */
14506 discard_cleanups (free_cu_cleanup);
14507
14508 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14509 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14510}
14511
c906108c
SS
14512/* Decode simple location descriptions.
14513 Given a pointer to a dwarf block that defines a location, compute
14514 the location and return the value.
14515
4cecd739
DJ
14516 NOTE drow/2003-11-18: This function is called in two situations
14517 now: for the address of static or global variables (partial symbols
14518 only) and for offsets into structures which are expected to be
14519 (more or less) constant. The partial symbol case should go away,
14520 and only the constant case should remain. That will let this
14521 function complain more accurately. A few special modes are allowed
14522 without complaint for global variables (for instance, global
14523 register values and thread-local values).
c906108c
SS
14524
14525 A location description containing no operations indicates that the
4cecd739 14526 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14527 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14528 callers will only want a very basic result and this can become a
21ae7a4d
JK
14529 complaint.
14530
14531 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14532
14533static CORE_ADDR
e7c27a73 14534decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14535{
e7c27a73 14536 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14537 int i;
14538 int size = blk->size;
14539 gdb_byte *data = blk->data;
14540 CORE_ADDR stack[64];
14541 int stacki;
14542 unsigned int bytes_read, unsnd;
14543 gdb_byte op;
c906108c 14544
21ae7a4d
JK
14545 i = 0;
14546 stacki = 0;
14547 stack[stacki] = 0;
14548 stack[++stacki] = 0;
14549
14550 while (i < size)
14551 {
14552 op = data[i++];
14553 switch (op)
14554 {
14555 case DW_OP_lit0:
14556 case DW_OP_lit1:
14557 case DW_OP_lit2:
14558 case DW_OP_lit3:
14559 case DW_OP_lit4:
14560 case DW_OP_lit5:
14561 case DW_OP_lit6:
14562 case DW_OP_lit7:
14563 case DW_OP_lit8:
14564 case DW_OP_lit9:
14565 case DW_OP_lit10:
14566 case DW_OP_lit11:
14567 case DW_OP_lit12:
14568 case DW_OP_lit13:
14569 case DW_OP_lit14:
14570 case DW_OP_lit15:
14571 case DW_OP_lit16:
14572 case DW_OP_lit17:
14573 case DW_OP_lit18:
14574 case DW_OP_lit19:
14575 case DW_OP_lit20:
14576 case DW_OP_lit21:
14577 case DW_OP_lit22:
14578 case DW_OP_lit23:
14579 case DW_OP_lit24:
14580 case DW_OP_lit25:
14581 case DW_OP_lit26:
14582 case DW_OP_lit27:
14583 case DW_OP_lit28:
14584 case DW_OP_lit29:
14585 case DW_OP_lit30:
14586 case DW_OP_lit31:
14587 stack[++stacki] = op - DW_OP_lit0;
14588 break;
f1bea926 14589
21ae7a4d
JK
14590 case DW_OP_reg0:
14591 case DW_OP_reg1:
14592 case DW_OP_reg2:
14593 case DW_OP_reg3:
14594 case DW_OP_reg4:
14595 case DW_OP_reg5:
14596 case DW_OP_reg6:
14597 case DW_OP_reg7:
14598 case DW_OP_reg8:
14599 case DW_OP_reg9:
14600 case DW_OP_reg10:
14601 case DW_OP_reg11:
14602 case DW_OP_reg12:
14603 case DW_OP_reg13:
14604 case DW_OP_reg14:
14605 case DW_OP_reg15:
14606 case DW_OP_reg16:
14607 case DW_OP_reg17:
14608 case DW_OP_reg18:
14609 case DW_OP_reg19:
14610 case DW_OP_reg20:
14611 case DW_OP_reg21:
14612 case DW_OP_reg22:
14613 case DW_OP_reg23:
14614 case DW_OP_reg24:
14615 case DW_OP_reg25:
14616 case DW_OP_reg26:
14617 case DW_OP_reg27:
14618 case DW_OP_reg28:
14619 case DW_OP_reg29:
14620 case DW_OP_reg30:
14621 case DW_OP_reg31:
14622 stack[++stacki] = op - DW_OP_reg0;
14623 if (i < size)
14624 dwarf2_complex_location_expr_complaint ();
14625 break;
c906108c 14626
21ae7a4d
JK
14627 case DW_OP_regx:
14628 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14629 i += bytes_read;
14630 stack[++stacki] = unsnd;
14631 if (i < size)
14632 dwarf2_complex_location_expr_complaint ();
14633 break;
c906108c 14634
21ae7a4d
JK
14635 case DW_OP_addr:
14636 stack[++stacki] = read_address (objfile->obfd, &data[i],
14637 cu, &bytes_read);
14638 i += bytes_read;
14639 break;
d53d4ac5 14640
21ae7a4d
JK
14641 case DW_OP_const1u:
14642 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14643 i += 1;
14644 break;
14645
14646 case DW_OP_const1s:
14647 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14648 i += 1;
14649 break;
14650
14651 case DW_OP_const2u:
14652 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14653 i += 2;
14654 break;
14655
14656 case DW_OP_const2s:
14657 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14658 i += 2;
14659 break;
d53d4ac5 14660
21ae7a4d
JK
14661 case DW_OP_const4u:
14662 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14663 i += 4;
14664 break;
14665
14666 case DW_OP_const4s:
14667 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14668 i += 4;
14669 break;
14670
14671 case DW_OP_constu:
14672 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14673 &bytes_read);
14674 i += bytes_read;
14675 break;
14676
14677 case DW_OP_consts:
14678 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14679 i += bytes_read;
14680 break;
14681
14682 case DW_OP_dup:
14683 stack[stacki + 1] = stack[stacki];
14684 stacki++;
14685 break;
14686
14687 case DW_OP_plus:
14688 stack[stacki - 1] += stack[stacki];
14689 stacki--;
14690 break;
14691
14692 case DW_OP_plus_uconst:
14693 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14694 &bytes_read);
14695 i += bytes_read;
14696 break;
14697
14698 case DW_OP_minus:
14699 stack[stacki - 1] -= stack[stacki];
14700 stacki--;
14701 break;
14702
14703 case DW_OP_deref:
14704 /* If we're not the last op, then we definitely can't encode
14705 this using GDB's address_class enum. This is valid for partial
14706 global symbols, although the variable's address will be bogus
14707 in the psymtab. */
14708 if (i < size)
14709 dwarf2_complex_location_expr_complaint ();
14710 break;
14711
14712 case DW_OP_GNU_push_tls_address:
14713 /* The top of the stack has the offset from the beginning
14714 of the thread control block at which the variable is located. */
14715 /* Nothing should follow this operator, so the top of stack would
14716 be returned. */
14717 /* This is valid for partial global symbols, but the variable's
14718 address will be bogus in the psymtab. */
14719 if (i < size)
14720 dwarf2_complex_location_expr_complaint ();
14721 break;
14722
14723 case DW_OP_GNU_uninit:
14724 break;
14725
14726 default:
14727 {
14728 const char *name = dwarf_stack_op_name (op);
14729
14730 if (name)
14731 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14732 name);
14733 else
14734 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14735 op);
14736 }
14737
14738 return (stack[stacki]);
d53d4ac5 14739 }
3c6e0cb3 14740
21ae7a4d
JK
14741 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14742 outside of the allocated space. Also enforce minimum>0. */
14743 if (stacki >= ARRAY_SIZE (stack) - 1)
14744 {
14745 complaint (&symfile_complaints,
14746 _("location description stack overflow"));
14747 return 0;
14748 }
14749
14750 if (stacki <= 0)
14751 {
14752 complaint (&symfile_complaints,
14753 _("location description stack underflow"));
14754 return 0;
14755 }
14756 }
14757 return (stack[stacki]);
c906108c
SS
14758}
14759
14760/* memory allocation interface */
14761
c906108c 14762static struct dwarf_block *
7b5a2f43 14763dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14764{
14765 struct dwarf_block *blk;
14766
14767 blk = (struct dwarf_block *)
7b5a2f43 14768 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14769 return (blk);
14770}
14771
14772static struct abbrev_info *
f3dd6933 14773dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14774{
14775 struct abbrev_info *abbrev;
14776
f3dd6933
DJ
14777 abbrev = (struct abbrev_info *)
14778 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14779 memset (abbrev, 0, sizeof (struct abbrev_info));
14780 return (abbrev);
14781}
14782
14783static struct die_info *
b60c80d6 14784dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14785{
14786 struct die_info *die;
b60c80d6
DJ
14787 size_t size = sizeof (struct die_info);
14788
14789 if (num_attrs > 1)
14790 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14791
b60c80d6 14792 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14793 memset (die, 0, sizeof (struct die_info));
14794 return (die);
14795}
2e276125
JB
14796
14797\f
14798/* Macro support. */
14799
2e276125
JB
14800/* Return the full name of file number I in *LH's file name table.
14801 Use COMP_DIR as the name of the current directory of the
14802 compilation. The result is allocated using xmalloc; the caller is
14803 responsible for freeing it. */
14804static char *
14805file_full_name (int file, struct line_header *lh, const char *comp_dir)
14806{
6a83a1e6
EZ
14807 /* Is the file number a valid index into the line header's file name
14808 table? Remember that file numbers start with one, not zero. */
14809 if (1 <= file && file <= lh->num_file_names)
14810 {
14811 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14812
6a83a1e6
EZ
14813 if (IS_ABSOLUTE_PATH (fe->name))
14814 return xstrdup (fe->name);
14815 else
14816 {
14817 const char *dir;
14818 int dir_len;
14819 char *full_name;
14820
14821 if (fe->dir_index)
14822 dir = lh->include_dirs[fe->dir_index - 1];
14823 else
14824 dir = comp_dir;
14825
14826 if (dir)
14827 {
14828 dir_len = strlen (dir);
14829 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14830 strcpy (full_name, dir);
14831 full_name[dir_len] = '/';
14832 strcpy (full_name + dir_len + 1, fe->name);
14833 return full_name;
14834 }
14835 else
14836 return xstrdup (fe->name);
14837 }
14838 }
2e276125
JB
14839 else
14840 {
6a83a1e6
EZ
14841 /* The compiler produced a bogus file number. We can at least
14842 record the macro definitions made in the file, even if we
14843 won't be able to find the file by name. */
14844 char fake_name[80];
9a619af0 14845
6a83a1e6 14846 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14847
6e70227d 14848 complaint (&symfile_complaints,
6a83a1e6
EZ
14849 _("bad file number in macro information (%d)"),
14850 file);
2e276125 14851
6a83a1e6 14852 return xstrdup (fake_name);
2e276125
JB
14853 }
14854}
14855
14856
14857static struct macro_source_file *
14858macro_start_file (int file, int line,
14859 struct macro_source_file *current_file,
14860 const char *comp_dir,
14861 struct line_header *lh, struct objfile *objfile)
14862{
14863 /* The full name of this source file. */
14864 char *full_name = file_full_name (file, lh, comp_dir);
14865
14866 /* We don't create a macro table for this compilation unit
14867 at all until we actually get a filename. */
14868 if (! pending_macros)
4a146b47 14869 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14870 objfile->macro_cache);
2e276125
JB
14871
14872 if (! current_file)
14873 /* If we have no current file, then this must be the start_file
14874 directive for the compilation unit's main source file. */
14875 current_file = macro_set_main (pending_macros, full_name);
14876 else
14877 current_file = macro_include (current_file, line, full_name);
14878
14879 xfree (full_name);
6e70227d 14880
2e276125
JB
14881 return current_file;
14882}
14883
14884
14885/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14886 followed by a null byte. */
14887static char *
14888copy_string (const char *buf, int len)
14889{
14890 char *s = xmalloc (len + 1);
9a619af0 14891
2e276125
JB
14892 memcpy (s, buf, len);
14893 s[len] = '\0';
2e276125
JB
14894 return s;
14895}
14896
14897
14898static const char *
14899consume_improper_spaces (const char *p, const char *body)
14900{
14901 if (*p == ' ')
14902 {
4d3c2250 14903 complaint (&symfile_complaints,
3e43a32a
MS
14904 _("macro definition contains spaces "
14905 "in formal argument list:\n`%s'"),
4d3c2250 14906 body);
2e276125
JB
14907
14908 while (*p == ' ')
14909 p++;
14910 }
14911
14912 return p;
14913}
14914
14915
14916static void
14917parse_macro_definition (struct macro_source_file *file, int line,
14918 const char *body)
14919{
14920 const char *p;
14921
14922 /* The body string takes one of two forms. For object-like macro
14923 definitions, it should be:
14924
14925 <macro name> " " <definition>
14926
14927 For function-like macro definitions, it should be:
14928
14929 <macro name> "() " <definition>
14930 or
14931 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14932
14933 Spaces may appear only where explicitly indicated, and in the
14934 <definition>.
14935
14936 The Dwarf 2 spec says that an object-like macro's name is always
14937 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14938 the space when the macro's definition is the empty string.
2e276125
JB
14939
14940 The Dwarf 2 spec says that there should be no spaces between the
14941 formal arguments in a function-like macro's formal argument list,
14942 but versions of GCC around March 2002 include spaces after the
14943 commas. */
14944
14945
14946 /* Find the extent of the macro name. The macro name is terminated
14947 by either a space or null character (for an object-like macro) or
14948 an opening paren (for a function-like macro). */
14949 for (p = body; *p; p++)
14950 if (*p == ' ' || *p == '(')
14951 break;
14952
14953 if (*p == ' ' || *p == '\0')
14954 {
14955 /* It's an object-like macro. */
14956 int name_len = p - body;
14957 char *name = copy_string (body, name_len);
14958 const char *replacement;
14959
14960 if (*p == ' ')
14961 replacement = body + name_len + 1;
14962 else
14963 {
4d3c2250 14964 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14965 replacement = body + name_len;
14966 }
6e70227d 14967
2e276125
JB
14968 macro_define_object (file, line, name, replacement);
14969
14970 xfree (name);
14971 }
14972 else if (*p == '(')
14973 {
14974 /* It's a function-like macro. */
14975 char *name = copy_string (body, p - body);
14976 int argc = 0;
14977 int argv_size = 1;
14978 char **argv = xmalloc (argv_size * sizeof (*argv));
14979
14980 p++;
14981
14982 p = consume_improper_spaces (p, body);
14983
14984 /* Parse the formal argument list. */
14985 while (*p && *p != ')')
14986 {
14987 /* Find the extent of the current argument name. */
14988 const char *arg_start = p;
14989
14990 while (*p && *p != ',' && *p != ')' && *p != ' ')
14991 p++;
14992
14993 if (! *p || p == arg_start)
4d3c2250 14994 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14995 else
14996 {
14997 /* Make sure argv has room for the new argument. */
14998 if (argc >= argv_size)
14999 {
15000 argv_size *= 2;
15001 argv = xrealloc (argv, argv_size * sizeof (*argv));
15002 }
15003
15004 argv[argc++] = copy_string (arg_start, p - arg_start);
15005 }
15006
15007 p = consume_improper_spaces (p, body);
15008
15009 /* Consume the comma, if present. */
15010 if (*p == ',')
15011 {
15012 p++;
15013
15014 p = consume_improper_spaces (p, body);
15015 }
15016 }
15017
15018 if (*p == ')')
15019 {
15020 p++;
15021
15022 if (*p == ' ')
15023 /* Perfectly formed definition, no complaints. */
15024 macro_define_function (file, line, name,
6e70227d 15025 argc, (const char **) argv,
2e276125
JB
15026 p + 1);
15027 else if (*p == '\0')
15028 {
15029 /* Complain, but do define it. */
4d3c2250 15030 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15031 macro_define_function (file, line, name,
6e70227d 15032 argc, (const char **) argv,
2e276125
JB
15033 p);
15034 }
15035 else
15036 /* Just complain. */
4d3c2250 15037 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15038 }
15039 else
15040 /* Just complain. */
4d3c2250 15041 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15042
15043 xfree (name);
15044 {
15045 int i;
15046
15047 for (i = 0; i < argc; i++)
15048 xfree (argv[i]);
15049 }
15050 xfree (argv);
15051 }
15052 else
4d3c2250 15053 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15054}
15055
cf2c3c16
TT
15056/* Skip some bytes from BYTES according to the form given in FORM.
15057 Returns the new pointer. */
2e276125 15058
cf2c3c16
TT
15059static gdb_byte *
15060skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15061 enum dwarf_form form,
15062 unsigned int offset_size,
15063 struct dwarf2_section_info *section)
2e276125 15064{
cf2c3c16 15065 unsigned int bytes_read;
2e276125 15066
cf2c3c16 15067 switch (form)
2e276125 15068 {
cf2c3c16
TT
15069 case DW_FORM_data1:
15070 case DW_FORM_flag:
15071 ++bytes;
15072 break;
15073
15074 case DW_FORM_data2:
15075 bytes += 2;
15076 break;
15077
15078 case DW_FORM_data4:
15079 bytes += 4;
15080 break;
15081
15082 case DW_FORM_data8:
15083 bytes += 8;
15084 break;
15085
15086 case DW_FORM_string:
15087 read_direct_string (abfd, bytes, &bytes_read);
15088 bytes += bytes_read;
15089 break;
15090
15091 case DW_FORM_sec_offset:
15092 case DW_FORM_strp:
15093 bytes += offset_size;
15094 break;
15095
15096 case DW_FORM_block:
15097 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15098 bytes += bytes_read;
15099 break;
15100
15101 case DW_FORM_block1:
15102 bytes += 1 + read_1_byte (abfd, bytes);
15103 break;
15104 case DW_FORM_block2:
15105 bytes += 2 + read_2_bytes (abfd, bytes);
15106 break;
15107 case DW_FORM_block4:
15108 bytes += 4 + read_4_bytes (abfd, bytes);
15109 break;
15110
15111 case DW_FORM_sdata:
15112 case DW_FORM_udata:
15113 bytes = skip_leb128 (abfd, bytes);
15114 break;
15115
15116 default:
15117 {
15118 complain:
15119 complaint (&symfile_complaints,
15120 _("invalid form 0x%x in `%s'"),
15121 form,
15122 section->asection->name);
15123 return NULL;
15124 }
2e276125
JB
15125 }
15126
cf2c3c16
TT
15127 return bytes;
15128}
757a13d0 15129
cf2c3c16
TT
15130/* A helper for dwarf_decode_macros that handles skipping an unknown
15131 opcode. Returns an updated pointer to the macro data buffer; or,
15132 on error, issues a complaint and returns NULL. */
757a13d0 15133
cf2c3c16
TT
15134static gdb_byte *
15135skip_unknown_opcode (unsigned int opcode,
15136 gdb_byte **opcode_definitions,
15137 gdb_byte *mac_ptr,
15138 bfd *abfd,
15139 unsigned int offset_size,
15140 struct dwarf2_section_info *section)
15141{
15142 unsigned int bytes_read, i;
15143 unsigned long arg;
15144 gdb_byte *defn;
2e276125 15145
cf2c3c16 15146 if (opcode_definitions[opcode] == NULL)
2e276125 15147 {
cf2c3c16
TT
15148 complaint (&symfile_complaints,
15149 _("unrecognized DW_MACFINO opcode 0x%x"),
15150 opcode);
15151 return NULL;
15152 }
2e276125 15153
cf2c3c16
TT
15154 defn = opcode_definitions[opcode];
15155 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15156 defn += bytes_read;
2e276125 15157
cf2c3c16
TT
15158 for (i = 0; i < arg; ++i)
15159 {
15160 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15161 if (mac_ptr == NULL)
15162 {
15163 /* skip_form_bytes already issued the complaint. */
15164 return NULL;
15165 }
15166 }
757a13d0 15167
cf2c3c16
TT
15168 return mac_ptr;
15169}
757a13d0 15170
cf2c3c16
TT
15171/* A helper function which parses the header of a macro section.
15172 If the macro section is the extended (for now called "GNU") type,
15173 then this updates *OFFSET_SIZE. Returns a pointer to just after
15174 the header, or issues a complaint and returns NULL on error. */
757a13d0 15175
cf2c3c16
TT
15176static gdb_byte *
15177dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15178 bfd *abfd,
15179 gdb_byte *mac_ptr,
15180 unsigned int *offset_size,
15181 int section_is_gnu)
15182{
15183 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15184
cf2c3c16
TT
15185 if (section_is_gnu)
15186 {
15187 unsigned int version, flags;
757a13d0 15188
cf2c3c16
TT
15189 version = read_2_bytes (abfd, mac_ptr);
15190 if (version != 4)
15191 {
15192 complaint (&symfile_complaints,
15193 _("unrecognized version `%d' in .debug_macro section"),
15194 version);
15195 return NULL;
15196 }
15197 mac_ptr += 2;
757a13d0 15198
cf2c3c16
TT
15199 flags = read_1_byte (abfd, mac_ptr);
15200 ++mac_ptr;
15201 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15202
cf2c3c16
TT
15203 if ((flags & 2) != 0)
15204 /* We don't need the line table offset. */
15205 mac_ptr += *offset_size;
757a13d0 15206
cf2c3c16
TT
15207 /* Vendor opcode descriptions. */
15208 if ((flags & 4) != 0)
15209 {
15210 unsigned int i, count;
757a13d0 15211
cf2c3c16
TT
15212 count = read_1_byte (abfd, mac_ptr);
15213 ++mac_ptr;
15214 for (i = 0; i < count; ++i)
15215 {
15216 unsigned int opcode, bytes_read;
15217 unsigned long arg;
15218
15219 opcode = read_1_byte (abfd, mac_ptr);
15220 ++mac_ptr;
15221 opcode_definitions[opcode] = mac_ptr;
15222 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15223 mac_ptr += bytes_read;
15224 mac_ptr += arg;
15225 }
757a13d0 15226 }
cf2c3c16 15227 }
757a13d0 15228
cf2c3c16
TT
15229 return mac_ptr;
15230}
757a13d0 15231
cf2c3c16
TT
15232/* A helper for dwarf_decode_macros that handles the GNU extensions,
15233 including DW_GNU_MACINFO_transparent_include. */
15234
15235static void
15236dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15237 struct macro_source_file *current_file,
15238 struct line_header *lh, char *comp_dir,
15239 struct dwarf2_section_info *section,
15240 int section_is_gnu,
15241 unsigned int offset_size,
15242 struct objfile *objfile)
15243{
15244 enum dwarf_macro_record_type macinfo_type;
15245 int at_commandline;
15246 gdb_byte *opcode_definitions[256];
757a13d0 15247
cf2c3c16
TT
15248 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15249 &offset_size, section_is_gnu);
15250 if (mac_ptr == NULL)
15251 {
15252 /* We already issued a complaint. */
15253 return;
15254 }
757a13d0
JK
15255
15256 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15257 GDB is still reading the definitions from command line. First
15258 DW_MACINFO_start_file will need to be ignored as it was already executed
15259 to create CURRENT_FILE for the main source holding also the command line
15260 definitions. On first met DW_MACINFO_start_file this flag is reset to
15261 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15262
15263 at_commandline = 1;
15264
15265 do
15266 {
15267 /* Do we at least have room for a macinfo type byte? */
15268 if (mac_ptr >= mac_end)
15269 {
cf2c3c16 15270 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15271 break;
15272 }
15273
15274 macinfo_type = read_1_byte (abfd, mac_ptr);
15275 mac_ptr++;
15276
cf2c3c16
TT
15277 /* Note that we rely on the fact that the corresponding GNU and
15278 DWARF constants are the same. */
757a13d0
JK
15279 switch (macinfo_type)
15280 {
15281 /* A zero macinfo type indicates the end of the macro
15282 information. */
15283 case 0:
15284 break;
2e276125 15285
cf2c3c16
TT
15286 case DW_MACRO_GNU_define:
15287 case DW_MACRO_GNU_undef:
15288 case DW_MACRO_GNU_define_indirect:
15289 case DW_MACRO_GNU_undef_indirect:
2e276125 15290 {
891d2f0b 15291 unsigned int bytes_read;
2e276125
JB
15292 int line;
15293 char *body;
cf2c3c16 15294 int is_define;
2e276125 15295
cf2c3c16
TT
15296 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15297 mac_ptr += bytes_read;
15298
15299 if (macinfo_type == DW_MACRO_GNU_define
15300 || macinfo_type == DW_MACRO_GNU_undef)
15301 {
15302 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15303 mac_ptr += bytes_read;
15304 }
15305 else
15306 {
15307 LONGEST str_offset;
15308
15309 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15310 mac_ptr += offset_size;
2e276125 15311
cf2c3c16
TT
15312 body = read_indirect_string_at_offset (abfd, str_offset);
15313 }
15314
15315 is_define = (macinfo_type == DW_MACRO_GNU_define
15316 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15317 if (! current_file)
757a13d0
JK
15318 {
15319 /* DWARF violation as no main source is present. */
15320 complaint (&symfile_complaints,
15321 _("debug info with no main source gives macro %s "
15322 "on line %d: %s"),
cf2c3c16
TT
15323 is_define ? _("definition") : _("undefinition"),
15324 line, body);
757a13d0
JK
15325 break;
15326 }
3e43a32a
MS
15327 if ((line == 0 && !at_commandline)
15328 || (line != 0 && at_commandline))
4d3c2250 15329 complaint (&symfile_complaints,
757a13d0
JK
15330 _("debug info gives %s macro %s with %s line %d: %s"),
15331 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15332 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15333 line == 0 ? _("zero") : _("non-zero"), line, body);
15334
cf2c3c16 15335 if (is_define)
757a13d0 15336 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15337 else
15338 {
15339 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15340 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15341 macro_undef (current_file, line, body);
15342 }
2e276125
JB
15343 }
15344 break;
15345
cf2c3c16 15346 case DW_MACRO_GNU_start_file:
2e276125 15347 {
891d2f0b 15348 unsigned int bytes_read;
2e276125
JB
15349 int line, file;
15350
15351 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15352 mac_ptr += bytes_read;
15353 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15354 mac_ptr += bytes_read;
15355
3e43a32a
MS
15356 if ((line == 0 && !at_commandline)
15357 || (line != 0 && at_commandline))
757a13d0
JK
15358 complaint (&symfile_complaints,
15359 _("debug info gives source %d included "
15360 "from %s at %s line %d"),
15361 file, at_commandline ? _("command-line") : _("file"),
15362 line == 0 ? _("zero") : _("non-zero"), line);
15363
15364 if (at_commandline)
15365 {
cf2c3c16
TT
15366 /* This DW_MACRO_GNU_start_file was executed in the
15367 pass one. */
757a13d0
JK
15368 at_commandline = 0;
15369 }
15370 else
15371 current_file = macro_start_file (file, line,
15372 current_file, comp_dir,
cf2c3c16 15373 lh, objfile);
2e276125
JB
15374 }
15375 break;
15376
cf2c3c16 15377 case DW_MACRO_GNU_end_file:
2e276125 15378 if (! current_file)
4d3c2250 15379 complaint (&symfile_complaints,
3e43a32a
MS
15380 _("macro debug info has an unmatched "
15381 "`close_file' directive"));
2e276125
JB
15382 else
15383 {
15384 current_file = current_file->included_by;
15385 if (! current_file)
15386 {
cf2c3c16 15387 enum dwarf_macro_record_type next_type;
2e276125
JB
15388
15389 /* GCC circa March 2002 doesn't produce the zero
15390 type byte marking the end of the compilation
15391 unit. Complain if it's not there, but exit no
15392 matter what. */
15393
15394 /* Do we at least have room for a macinfo type byte? */
15395 if (mac_ptr >= mac_end)
15396 {
cf2c3c16 15397 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15398 return;
15399 }
15400
15401 /* We don't increment mac_ptr here, so this is just
15402 a look-ahead. */
15403 next_type = read_1_byte (abfd, mac_ptr);
15404 if (next_type != 0)
4d3c2250 15405 complaint (&symfile_complaints,
3e43a32a
MS
15406 _("no terminating 0-type entry for "
15407 "macros in `.debug_macinfo' section"));
2e276125
JB
15408
15409 return;
15410 }
15411 }
15412 break;
15413
cf2c3c16
TT
15414 case DW_MACRO_GNU_transparent_include:
15415 {
15416 LONGEST offset;
15417
15418 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15419 mac_ptr += offset_size;
15420
15421 dwarf_decode_macro_bytes (abfd,
15422 section->buffer + offset,
15423 mac_end, current_file,
15424 lh, comp_dir,
15425 section, section_is_gnu,
15426 offset_size, objfile);
15427 }
15428 break;
15429
2e276125 15430 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15431 if (!section_is_gnu)
15432 {
15433 unsigned int bytes_read;
15434 int constant;
2e276125 15435
cf2c3c16
TT
15436 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15437 mac_ptr += bytes_read;
15438 read_direct_string (abfd, mac_ptr, &bytes_read);
15439 mac_ptr += bytes_read;
2e276125 15440
cf2c3c16
TT
15441 /* We don't recognize any vendor extensions. */
15442 break;
15443 }
15444 /* FALLTHROUGH */
15445
15446 default:
15447 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15448 mac_ptr, abfd, offset_size,
15449 section);
15450 if (mac_ptr == NULL)
15451 return;
15452 break;
2e276125 15453 }
757a13d0 15454 } while (macinfo_type != 0);
2e276125 15455}
8e19ed76 15456
cf2c3c16
TT
15457static void
15458dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15459 char *comp_dir, bfd *abfd,
15460 struct dwarf2_cu *cu,
15461 struct dwarf2_section_info *section,
15462 int section_is_gnu)
15463{
15464 gdb_byte *mac_ptr, *mac_end;
15465 struct macro_source_file *current_file = 0;
15466 enum dwarf_macro_record_type macinfo_type;
15467 unsigned int offset_size = cu->header.offset_size;
15468 gdb_byte *opcode_definitions[256];
15469
15470 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15471 if (section->buffer == NULL)
15472 {
15473 complaint (&symfile_complaints, _("missing %s section"),
15474 section->asection->name);
15475 return;
15476 }
15477
15478 /* First pass: Find the name of the base filename.
15479 This filename is needed in order to process all macros whose definition
15480 (or undefinition) comes from the command line. These macros are defined
15481 before the first DW_MACINFO_start_file entry, and yet still need to be
15482 associated to the base file.
15483
15484 To determine the base file name, we scan the macro definitions until we
15485 reach the first DW_MACINFO_start_file entry. We then initialize
15486 CURRENT_FILE accordingly so that any macro definition found before the
15487 first DW_MACINFO_start_file can still be associated to the base file. */
15488
15489 mac_ptr = section->buffer + offset;
15490 mac_end = section->buffer + section->size;
15491
15492 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15493 &offset_size, section_is_gnu);
15494 if (mac_ptr == NULL)
15495 {
15496 /* We already issued a complaint. */
15497 return;
15498 }
15499
15500 do
15501 {
15502 /* Do we at least have room for a macinfo type byte? */
15503 if (mac_ptr >= mac_end)
15504 {
15505 /* Complaint is printed during the second pass as GDB will probably
15506 stop the first pass earlier upon finding
15507 DW_MACINFO_start_file. */
15508 break;
15509 }
15510
15511 macinfo_type = read_1_byte (abfd, mac_ptr);
15512 mac_ptr++;
15513
15514 /* Note that we rely on the fact that the corresponding GNU and
15515 DWARF constants are the same. */
15516 switch (macinfo_type)
15517 {
15518 /* A zero macinfo type indicates the end of the macro
15519 information. */
15520 case 0:
15521 break;
15522
15523 case DW_MACRO_GNU_define:
15524 case DW_MACRO_GNU_undef:
15525 /* Only skip the data by MAC_PTR. */
15526 {
15527 unsigned int bytes_read;
15528
15529 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15530 mac_ptr += bytes_read;
15531 read_direct_string (abfd, mac_ptr, &bytes_read);
15532 mac_ptr += bytes_read;
15533 }
15534 break;
15535
15536 case DW_MACRO_GNU_start_file:
15537 {
15538 unsigned int bytes_read;
15539 int line, file;
15540
15541 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15542 mac_ptr += bytes_read;
15543 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15544 mac_ptr += bytes_read;
15545
15546 current_file = macro_start_file (file, line, current_file,
15547 comp_dir, lh, cu->objfile);
15548 }
15549 break;
15550
15551 case DW_MACRO_GNU_end_file:
15552 /* No data to skip by MAC_PTR. */
15553 break;
15554
15555 case DW_MACRO_GNU_define_indirect:
15556 case DW_MACRO_GNU_undef_indirect:
15557 {
15558 unsigned int bytes_read;
15559
15560 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15561 mac_ptr += bytes_read;
15562 mac_ptr += offset_size;
15563 }
15564 break;
15565
15566 case DW_MACRO_GNU_transparent_include:
15567 /* Note that, according to the spec, a transparent include
15568 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15569 skip this opcode. */
15570 mac_ptr += offset_size;
15571 break;
15572
15573 case DW_MACINFO_vendor_ext:
15574 /* Only skip the data by MAC_PTR. */
15575 if (!section_is_gnu)
15576 {
15577 unsigned int bytes_read;
15578
15579 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15580 mac_ptr += bytes_read;
15581 read_direct_string (abfd, mac_ptr, &bytes_read);
15582 mac_ptr += bytes_read;
15583 }
15584 /* FALLTHROUGH */
15585
15586 default:
15587 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15588 mac_ptr, abfd, offset_size,
15589 section);
15590 if (mac_ptr == NULL)
15591 return;
15592 break;
15593 }
15594 } while (macinfo_type != 0 && current_file == NULL);
15595
15596 /* Second pass: Process all entries.
15597
15598 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15599 command-line macro definitions/undefinitions. This flag is unset when we
15600 reach the first DW_MACINFO_start_file entry. */
15601
15602 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15603 current_file, lh, comp_dir, section, section_is_gnu,
15604 offset_size, cu->objfile);
15605}
15606
8e19ed76 15607/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15608 if so return true else false. */
8e19ed76
PS
15609static int
15610attr_form_is_block (struct attribute *attr)
15611{
15612 return (attr == NULL ? 0 :
15613 attr->form == DW_FORM_block1
15614 || attr->form == DW_FORM_block2
15615 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15616 || attr->form == DW_FORM_block
15617 || attr->form == DW_FORM_exprloc);
8e19ed76 15618}
4c2df51b 15619
c6a0999f
JB
15620/* Return non-zero if ATTR's value is a section offset --- classes
15621 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15622 You may use DW_UNSND (attr) to retrieve such offsets.
15623
15624 Section 7.5.4, "Attribute Encodings", explains that no attribute
15625 may have a value that belongs to more than one of these classes; it
15626 would be ambiguous if we did, because we use the same forms for all
15627 of them. */
3690dd37
JB
15628static int
15629attr_form_is_section_offset (struct attribute *attr)
15630{
15631 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15632 || attr->form == DW_FORM_data8
15633 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15634}
15635
15636
15637/* Return non-zero if ATTR's value falls in the 'constant' class, or
15638 zero otherwise. When this function returns true, you can apply
15639 dwarf2_get_attr_constant_value to it.
15640
15641 However, note that for some attributes you must check
15642 attr_form_is_section_offset before using this test. DW_FORM_data4
15643 and DW_FORM_data8 are members of both the constant class, and of
15644 the classes that contain offsets into other debug sections
15645 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15646 that, if an attribute's can be either a constant or one of the
15647 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15648 taken as section offsets, not constants. */
15649static int
15650attr_form_is_constant (struct attribute *attr)
15651{
15652 switch (attr->form)
15653 {
15654 case DW_FORM_sdata:
15655 case DW_FORM_udata:
15656 case DW_FORM_data1:
15657 case DW_FORM_data2:
15658 case DW_FORM_data4:
15659 case DW_FORM_data8:
15660 return 1;
15661 default:
15662 return 0;
15663 }
15664}
15665
8cf6f0b1
TT
15666/* A helper function that fills in a dwarf2_loclist_baton. */
15667
15668static void
15669fill_in_loclist_baton (struct dwarf2_cu *cu,
15670 struct dwarf2_loclist_baton *baton,
15671 struct attribute *attr)
15672{
15673 dwarf2_read_section (dwarf2_per_objfile->objfile,
15674 &dwarf2_per_objfile->loc);
15675
15676 baton->per_cu = cu->per_cu;
15677 gdb_assert (baton->per_cu);
15678 /* We don't know how long the location list is, but make sure we
15679 don't run off the edge of the section. */
15680 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15681 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15682 baton->base_address = cu->base_address;
15683}
15684
4c2df51b
DJ
15685static void
15686dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15687 struct dwarf2_cu *cu)
4c2df51b 15688{
3690dd37 15689 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15690 /* ".debug_loc" may not exist at all, or the offset may be outside
15691 the section. If so, fall through to the complaint in the
15692 other branch. */
9e0ac564
TT
15693 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15694 &dwarf2_per_objfile->loc))
4c2df51b 15695 {
0d53c4c4 15696 struct dwarf2_loclist_baton *baton;
4c2df51b 15697
4a146b47 15698 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15699 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15700
8cf6f0b1 15701 fill_in_loclist_baton (cu, baton, attr);
be391dca 15702
d00adf39 15703 if (cu->base_known == 0)
0d53c4c4 15704 complaint (&symfile_complaints,
3e43a32a
MS
15705 _("Location list used without "
15706 "specifying the CU base address."));
4c2df51b 15707
768a979c 15708 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15709 SYMBOL_LOCATION_BATON (sym) = baton;
15710 }
15711 else
15712 {
15713 struct dwarf2_locexpr_baton *baton;
15714
4a146b47 15715 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15716 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15717 baton->per_cu = cu->per_cu;
15718 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15719
15720 if (attr_form_is_block (attr))
15721 {
15722 /* Note that we're just copying the block's data pointer
15723 here, not the actual data. We're still pointing into the
6502dd73
DJ
15724 info_buffer for SYM's objfile; right now we never release
15725 that buffer, but when we do clean up properly this may
15726 need to change. */
0d53c4c4
DJ
15727 baton->size = DW_BLOCK (attr)->size;
15728 baton->data = DW_BLOCK (attr)->data;
15729 }
15730 else
15731 {
15732 dwarf2_invalid_attrib_class_complaint ("location description",
15733 SYMBOL_NATURAL_NAME (sym));
15734 baton->size = 0;
0d53c4c4 15735 }
6e70227d 15736
768a979c 15737 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15738 SYMBOL_LOCATION_BATON (sym) = baton;
15739 }
4c2df51b 15740}
6502dd73 15741
9aa1f1e3
TT
15742/* Return the OBJFILE associated with the compilation unit CU. If CU
15743 came from a separate debuginfo file, then the master objfile is
15744 returned. */
ae0d2f24
UW
15745
15746struct objfile *
15747dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15748{
9291a0cd 15749 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15750
15751 /* Return the master objfile, so that we can report and look up the
15752 correct file containing this variable. */
15753 if (objfile->separate_debug_objfile_backlink)
15754 objfile = objfile->separate_debug_objfile_backlink;
15755
15756 return objfile;
15757}
15758
96408a79
SA
15759/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15760 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15761 CU_HEADERP first. */
15762
15763static const struct comp_unit_head *
15764per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15765 struct dwarf2_per_cu_data *per_cu)
15766{
15767 struct objfile *objfile;
15768 struct dwarf2_per_objfile *per_objfile;
15769 gdb_byte *info_ptr;
15770
15771 if (per_cu->cu)
15772 return &per_cu->cu->header;
15773
15774 objfile = per_cu->objfile;
15775 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15776 info_ptr = per_objfile->info.buffer + per_cu->offset;
15777
15778 memset (cu_headerp, 0, sizeof (*cu_headerp));
15779 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15780
15781 return cu_headerp;
15782}
15783
ae0d2f24
UW
15784/* Return the address size given in the compilation unit header for CU. */
15785
15786CORE_ADDR
15787dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15788{
96408a79
SA
15789 struct comp_unit_head cu_header_local;
15790 const struct comp_unit_head *cu_headerp;
c471e790 15791
96408a79
SA
15792 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15793
15794 return cu_headerp->addr_size;
ae0d2f24
UW
15795}
15796
9eae7c52
TT
15797/* Return the offset size given in the compilation unit header for CU. */
15798
15799int
15800dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15801{
96408a79
SA
15802 struct comp_unit_head cu_header_local;
15803 const struct comp_unit_head *cu_headerp;
9c6c53f7 15804
96408a79
SA
15805 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15806
15807 return cu_headerp->offset_size;
15808}
15809
15810/* See its dwarf2loc.h declaration. */
15811
15812int
15813dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15814{
15815 struct comp_unit_head cu_header_local;
15816 const struct comp_unit_head *cu_headerp;
15817
15818 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15819
15820 if (cu_headerp->version == 2)
15821 return cu_headerp->addr_size;
15822 else
15823 return cu_headerp->offset_size;
181cebd4
JK
15824}
15825
9aa1f1e3
TT
15826/* Return the text offset of the CU. The returned offset comes from
15827 this CU's objfile. If this objfile came from a separate debuginfo
15828 file, then the offset may be different from the corresponding
15829 offset in the parent objfile. */
15830
15831CORE_ADDR
15832dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15833{
bb3fa9d0 15834 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15835
15836 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15837}
15838
348e048f
DE
15839/* Locate the .debug_info compilation unit from CU's objfile which contains
15840 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15841
15842static struct dwarf2_per_cu_data *
c764a876 15843dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15844 struct objfile *objfile)
15845{
15846 struct dwarf2_per_cu_data *this_cu;
15847 int low, high;
15848
ae038cb0
DJ
15849 low = 0;
15850 high = dwarf2_per_objfile->n_comp_units - 1;
15851 while (high > low)
15852 {
15853 int mid = low + (high - low) / 2;
9a619af0 15854
ae038cb0
DJ
15855 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15856 high = mid;
15857 else
15858 low = mid + 1;
15859 }
15860 gdb_assert (low == high);
15861 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15862 {
10b3939b 15863 if (low == 0)
8a3fe4f8
AC
15864 error (_("Dwarf Error: could not find partial DIE containing "
15865 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15866 (long) offset, bfd_get_filename (objfile->obfd));
15867
ae038cb0
DJ
15868 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15869 return dwarf2_per_objfile->all_comp_units[low-1];
15870 }
15871 else
15872 {
15873 this_cu = dwarf2_per_objfile->all_comp_units[low];
15874 if (low == dwarf2_per_objfile->n_comp_units - 1
15875 && offset >= this_cu->offset + this_cu->length)
c764a876 15876 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15877 gdb_assert (offset < this_cu->offset + this_cu->length);
15878 return this_cu;
15879 }
15880}
15881
10b3939b
DJ
15882/* Locate the compilation unit from OBJFILE which is located at exactly
15883 OFFSET. Raises an error on failure. */
15884
ae038cb0 15885static struct dwarf2_per_cu_data *
c764a876 15886dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15887{
15888 struct dwarf2_per_cu_data *this_cu;
9a619af0 15889
ae038cb0
DJ
15890 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15891 if (this_cu->offset != offset)
c764a876 15892 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15893 return this_cu;
15894}
15895
9816fde3 15896/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15897
9816fde3
JK
15898static void
15899init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15900{
9816fde3 15901 memset (cu, 0, sizeof (*cu));
93311388
DE
15902 cu->objfile = objfile;
15903 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15904}
15905
15906/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15907
15908static void
15909prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15910{
15911 struct attribute *attr;
15912
15913 /* Set the language we're debugging. */
15914 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15915 if (attr)
15916 set_cu_language (DW_UNSND (attr), cu);
15917 else
9cded63f
TT
15918 {
15919 cu->language = language_minimal;
15920 cu->language_defn = language_def (cu->language);
15921 }
93311388
DE
15922}
15923
ae038cb0
DJ
15924/* Release one cached compilation unit, CU. We unlink it from the tree
15925 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15926 the caller is responsible for that.
15927 NOTE: DATA is a void * because this function is also used as a
15928 cleanup routine. */
ae038cb0
DJ
15929
15930static void
15931free_one_comp_unit (void *data)
15932{
15933 struct dwarf2_cu *cu = data;
15934
15935 if (cu->per_cu != NULL)
15936 cu->per_cu->cu = NULL;
15937 cu->per_cu = NULL;
15938
15939 obstack_free (&cu->comp_unit_obstack, NULL);
15940
15941 xfree (cu);
15942}
15943
72bf9492 15944/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15945 when we're finished with it. We can't free the pointer itself, but be
15946 sure to unlink it from the cache. Also release any associated storage
15947 and perform cache maintenance.
72bf9492
DJ
15948
15949 Only used during partial symbol parsing. */
15950
15951static void
15952free_stack_comp_unit (void *data)
15953{
15954 struct dwarf2_cu *cu = data;
15955
15956 obstack_free (&cu->comp_unit_obstack, NULL);
15957 cu->partial_dies = NULL;
ae038cb0
DJ
15958
15959 if (cu->per_cu != NULL)
15960 {
15961 /* This compilation unit is on the stack in our caller, so we
15962 should not xfree it. Just unlink it. */
15963 cu->per_cu->cu = NULL;
15964 cu->per_cu = NULL;
15965
15966 /* If we had a per-cu pointer, then we may have other compilation
15967 units loaded, so age them now. */
15968 age_cached_comp_units ();
15969 }
15970}
15971
15972/* Free all cached compilation units. */
15973
15974static void
15975free_cached_comp_units (void *data)
15976{
15977 struct dwarf2_per_cu_data *per_cu, **last_chain;
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 free_one_comp_unit (per_cu->cu);
15988 *last_chain = next_cu;
15989
15990 per_cu = next_cu;
15991 }
15992}
15993
15994/* Increase the age counter on each cached compilation unit, and free
15995 any that are too old. */
15996
15997static void
15998age_cached_comp_units (void)
15999{
16000 struct dwarf2_per_cu_data *per_cu, **last_chain;
16001
16002 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16003 per_cu = dwarf2_per_objfile->read_in_chain;
16004 while (per_cu != NULL)
16005 {
16006 per_cu->cu->last_used ++;
16007 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16008 dwarf2_mark (per_cu->cu);
16009 per_cu = per_cu->cu->read_in_chain;
16010 }
16011
16012 per_cu = dwarf2_per_objfile->read_in_chain;
16013 last_chain = &dwarf2_per_objfile->read_in_chain;
16014 while (per_cu != NULL)
16015 {
16016 struct dwarf2_per_cu_data *next_cu;
16017
16018 next_cu = per_cu->cu->read_in_chain;
16019
16020 if (!per_cu->cu->mark)
16021 {
16022 free_one_comp_unit (per_cu->cu);
16023 *last_chain = next_cu;
16024 }
16025 else
16026 last_chain = &per_cu->cu->read_in_chain;
16027
16028 per_cu = next_cu;
16029 }
16030}
16031
16032/* Remove a single compilation unit from the cache. */
16033
16034static void
16035free_one_cached_comp_unit (void *target_cu)
16036{
16037 struct dwarf2_per_cu_data *per_cu, **last_chain;
16038
16039 per_cu = dwarf2_per_objfile->read_in_chain;
16040 last_chain = &dwarf2_per_objfile->read_in_chain;
16041 while (per_cu != NULL)
16042 {
16043 struct dwarf2_per_cu_data *next_cu;
16044
16045 next_cu = per_cu->cu->read_in_chain;
16046
16047 if (per_cu->cu == target_cu)
16048 {
16049 free_one_comp_unit (per_cu->cu);
16050 *last_chain = next_cu;
16051 break;
16052 }
16053 else
16054 last_chain = &per_cu->cu->read_in_chain;
16055
16056 per_cu = next_cu;
16057 }
16058}
16059
fe3e1990
DJ
16060/* Release all extra memory associated with OBJFILE. */
16061
16062void
16063dwarf2_free_objfile (struct objfile *objfile)
16064{
16065 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16066
16067 if (dwarf2_per_objfile == NULL)
16068 return;
16069
16070 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16071 free_cached_comp_units (NULL);
16072
7b9f3c50
DE
16073 if (dwarf2_per_objfile->quick_file_names_table)
16074 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16075
fe3e1990
DJ
16076 /* Everything else should be on the objfile obstack. */
16077}
16078
1c379e20
DJ
16079/* A pair of DIE offset and GDB type pointer. We store these
16080 in a hash table separate from the DIEs, and preserve them
16081 when the DIEs are flushed out of cache. */
16082
16083struct dwarf2_offset_and_type
16084{
16085 unsigned int offset;
16086 struct type *type;
16087};
16088
16089/* Hash function for a dwarf2_offset_and_type. */
16090
16091static hashval_t
16092offset_and_type_hash (const void *item)
16093{
16094 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16095
1c379e20
DJ
16096 return ofs->offset;
16097}
16098
16099/* Equality function for a dwarf2_offset_and_type. */
16100
16101static int
16102offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16103{
16104 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16105 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16106
1c379e20
DJ
16107 return ofs_lhs->offset == ofs_rhs->offset;
16108}
16109
16110/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16111 table if necessary. For convenience, return TYPE.
16112
16113 The DIEs reading must have careful ordering to:
16114 * Not cause infite loops trying to read in DIEs as a prerequisite for
16115 reading current DIE.
16116 * Not trying to dereference contents of still incompletely read in types
16117 while reading in other DIEs.
16118 * Enable referencing still incompletely read in types just by a pointer to
16119 the type without accessing its fields.
16120
16121 Therefore caller should follow these rules:
16122 * Try to fetch any prerequisite types we may need to build this DIE type
16123 before building the type and calling set_die_type.
e71ec853 16124 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16125 possible before fetching more types to complete the current type.
16126 * Make the type as complete as possible before fetching more types. */
1c379e20 16127
f792889a 16128static struct type *
1c379e20
DJ
16129set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16130{
16131 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16132 struct objfile *objfile = cu->objfile;
16133 htab_t *type_hash_ptr;
1c379e20 16134
b4ba55a1
JB
16135 /* For Ada types, make sure that the gnat-specific data is always
16136 initialized (if not already set). There are a few types where
16137 we should not be doing so, because the type-specific area is
16138 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16139 where the type-specific area is used to store the floatformat).
16140 But this is not a problem, because the gnat-specific information
16141 is actually not needed for these types. */
16142 if (need_gnat_info (cu)
16143 && TYPE_CODE (type) != TYPE_CODE_FUNC
16144 && TYPE_CODE (type) != TYPE_CODE_FLT
16145 && !HAVE_GNAT_AUX_INFO (type))
16146 INIT_GNAT_SPECIFIC (type);
16147
8b70b953 16148 if (cu->per_cu->debug_type_section)
673bfd45
DE
16149 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16150 else
16151 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16152
16153 if (*type_hash_ptr == NULL)
f792889a 16154 {
673bfd45
DE
16155 *type_hash_ptr
16156 = htab_create_alloc_ex (127,
f792889a
DJ
16157 offset_and_type_hash,
16158 offset_and_type_eq,
16159 NULL,
673bfd45 16160 &objfile->objfile_obstack,
f792889a
DJ
16161 hashtab_obstack_allocate,
16162 dummy_obstack_deallocate);
f792889a 16163 }
1c379e20
DJ
16164
16165 ofs.offset = die->offset;
16166 ofs.type = type;
16167 slot = (struct dwarf2_offset_and_type **)
673bfd45 16168 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16169 if (*slot)
16170 complaint (&symfile_complaints,
16171 _("A problem internal to GDB: DIE 0x%x has type already set"),
16172 die->offset);
673bfd45 16173 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16174 **slot = ofs;
f792889a 16175 return type;
1c379e20
DJ
16176}
16177
673bfd45
DE
16178/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16179 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16180
16181static struct type *
673bfd45
DE
16182get_die_type_at_offset (unsigned int offset,
16183 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16184{
16185 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16186 htab_t type_hash;
f792889a 16187
8b70b953 16188 if (per_cu->debug_type_section)
673bfd45
DE
16189 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16190 else
16191 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16192 if (type_hash == NULL)
16193 return NULL;
1c379e20 16194
673bfd45 16195 ofs.offset = offset;
1c379e20
DJ
16196 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16197 if (slot)
16198 return slot->type;
16199 else
16200 return NULL;
16201}
16202
673bfd45
DE
16203/* Look up the type for DIE in the appropriate type_hash table,
16204 or return NULL if DIE does not have a saved type. */
16205
16206static struct type *
16207get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16208{
16209 return get_die_type_at_offset (die->offset, cu->per_cu);
16210}
16211
10b3939b
DJ
16212/* Add a dependence relationship from CU to REF_PER_CU. */
16213
16214static void
16215dwarf2_add_dependence (struct dwarf2_cu *cu,
16216 struct dwarf2_per_cu_data *ref_per_cu)
16217{
16218 void **slot;
16219
16220 if (cu->dependencies == NULL)
16221 cu->dependencies
16222 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16223 NULL, &cu->comp_unit_obstack,
16224 hashtab_obstack_allocate,
16225 dummy_obstack_deallocate);
16226
16227 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16228 if (*slot == NULL)
16229 *slot = ref_per_cu;
16230}
1c379e20 16231
f504f079
DE
16232/* Subroutine of dwarf2_mark to pass to htab_traverse.
16233 Set the mark field in every compilation unit in the
ae038cb0
DJ
16234 cache that we must keep because we are keeping CU. */
16235
10b3939b
DJ
16236static int
16237dwarf2_mark_helper (void **slot, void *data)
16238{
16239 struct dwarf2_per_cu_data *per_cu;
16240
16241 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16242
16243 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16244 reading of the chain. As such dependencies remain valid it is not much
16245 useful to track and undo them during QUIT cleanups. */
16246 if (per_cu->cu == NULL)
16247 return 1;
16248
10b3939b
DJ
16249 if (per_cu->cu->mark)
16250 return 1;
16251 per_cu->cu->mark = 1;
16252
16253 if (per_cu->cu->dependencies != NULL)
16254 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16255
16256 return 1;
16257}
16258
f504f079
DE
16259/* Set the mark field in CU and in every other compilation unit in the
16260 cache that we must keep because we are keeping CU. */
16261
ae038cb0
DJ
16262static void
16263dwarf2_mark (struct dwarf2_cu *cu)
16264{
16265 if (cu->mark)
16266 return;
16267 cu->mark = 1;
10b3939b
DJ
16268 if (cu->dependencies != NULL)
16269 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16270}
16271
16272static void
16273dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16274{
16275 while (per_cu)
16276 {
16277 per_cu->cu->mark = 0;
16278 per_cu = per_cu->cu->read_in_chain;
16279 }
72bf9492
DJ
16280}
16281
72bf9492
DJ
16282/* Trivial hash function for partial_die_info: the hash value of a DIE
16283 is its offset in .debug_info for this objfile. */
16284
16285static hashval_t
16286partial_die_hash (const void *item)
16287{
16288 const struct partial_die_info *part_die = item;
9a619af0 16289
72bf9492
DJ
16290 return part_die->offset;
16291}
16292
16293/* Trivial comparison function for partial_die_info structures: two DIEs
16294 are equal if they have the same offset. */
16295
16296static int
16297partial_die_eq (const void *item_lhs, const void *item_rhs)
16298{
16299 const struct partial_die_info *part_die_lhs = item_lhs;
16300 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16301
72bf9492
DJ
16302 return part_die_lhs->offset == part_die_rhs->offset;
16303}
16304
ae038cb0
DJ
16305static struct cmd_list_element *set_dwarf2_cmdlist;
16306static struct cmd_list_element *show_dwarf2_cmdlist;
16307
16308static void
16309set_dwarf2_cmd (char *args, int from_tty)
16310{
16311 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16312}
16313
16314static void
16315show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16316{
ae038cb0
DJ
16317 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16318}
16319
dce234bc
PP
16320/* If section described by INFO was mmapped, munmap it now. */
16321
16322static void
16323munmap_section_buffer (struct dwarf2_section_info *info)
16324{
b315ab21 16325 if (info->map_addr != NULL)
dce234bc
PP
16326 {
16327#ifdef HAVE_MMAP
b315ab21 16328 int res;
9a619af0 16329
b315ab21
TG
16330 res = munmap (info->map_addr, info->map_len);
16331 gdb_assert (res == 0);
dce234bc
PP
16332#else
16333 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16334 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16335#endif
16336 }
16337}
16338
16339/* munmap debug sections for OBJFILE, if necessary. */
16340
16341static void
c1bd65d0 16342dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16343{
16344 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16345 int ix;
16346 struct dwarf2_section_info *section;
9a619af0 16347
16be1145
DE
16348 /* This is sorted according to the order they're defined in to make it easier
16349 to keep in sync. */
dce234bc
PP
16350 munmap_section_buffer (&data->info);
16351 munmap_section_buffer (&data->abbrev);
16352 munmap_section_buffer (&data->line);
16be1145 16353 munmap_section_buffer (&data->loc);
dce234bc 16354 munmap_section_buffer (&data->macinfo);
cf2c3c16 16355 munmap_section_buffer (&data->macro);
16be1145 16356 munmap_section_buffer (&data->str);
dce234bc 16357 munmap_section_buffer (&data->ranges);
dce234bc
PP
16358 munmap_section_buffer (&data->frame);
16359 munmap_section_buffer (&data->eh_frame);
9291a0cd 16360 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16361
16362 for (ix = 0;
16363 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16364 ++ix)
16365 munmap_section_buffer (section);
16366
16367 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16368}
16369
16370\f
ae2de4f8 16371/* The "save gdb-index" command. */
9291a0cd
TT
16372
16373/* The contents of the hash table we create when building the string
16374 table. */
16375struct strtab_entry
16376{
16377 offset_type offset;
16378 const char *str;
16379};
16380
559a7a62
JK
16381/* Hash function for a strtab_entry.
16382
16383 Function is used only during write_hash_table so no index format backward
16384 compatibility is needed. */
b89be57b 16385
9291a0cd
TT
16386static hashval_t
16387hash_strtab_entry (const void *e)
16388{
16389 const struct strtab_entry *entry = e;
559a7a62 16390 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16391}
16392
16393/* Equality function for a strtab_entry. */
b89be57b 16394
9291a0cd
TT
16395static int
16396eq_strtab_entry (const void *a, const void *b)
16397{
16398 const struct strtab_entry *ea = a;
16399 const struct strtab_entry *eb = b;
16400 return !strcmp (ea->str, eb->str);
16401}
16402
16403/* Create a strtab_entry hash table. */
b89be57b 16404
9291a0cd
TT
16405static htab_t
16406create_strtab (void)
16407{
16408 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16409 xfree, xcalloc, xfree);
16410}
16411
16412/* Add a string to the constant pool. Return the string's offset in
16413 host order. */
b89be57b 16414
9291a0cd
TT
16415static offset_type
16416add_string (htab_t table, struct obstack *cpool, const char *str)
16417{
16418 void **slot;
16419 struct strtab_entry entry;
16420 struct strtab_entry *result;
16421
16422 entry.str = str;
16423 slot = htab_find_slot (table, &entry, INSERT);
16424 if (*slot)
16425 result = *slot;
16426 else
16427 {
16428 result = XNEW (struct strtab_entry);
16429 result->offset = obstack_object_size (cpool);
16430 result->str = str;
16431 obstack_grow_str0 (cpool, str);
16432 *slot = result;
16433 }
16434 return result->offset;
16435}
16436
16437/* An entry in the symbol table. */
16438struct symtab_index_entry
16439{
16440 /* The name of the symbol. */
16441 const char *name;
16442 /* The offset of the name in the constant pool. */
16443 offset_type index_offset;
16444 /* A sorted vector of the indices of all the CUs that hold an object
16445 of this name. */
16446 VEC (offset_type) *cu_indices;
16447};
16448
16449/* The symbol table. This is a power-of-2-sized hash table. */
16450struct mapped_symtab
16451{
16452 offset_type n_elements;
16453 offset_type size;
16454 struct symtab_index_entry **data;
16455};
16456
16457/* Hash function for a symtab_index_entry. */
b89be57b 16458
9291a0cd
TT
16459static hashval_t
16460hash_symtab_entry (const void *e)
16461{
16462 const struct symtab_index_entry *entry = e;
16463 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16464 sizeof (offset_type) * VEC_length (offset_type,
16465 entry->cu_indices),
16466 0);
16467}
16468
16469/* Equality function for a symtab_index_entry. */
b89be57b 16470
9291a0cd
TT
16471static int
16472eq_symtab_entry (const void *a, const void *b)
16473{
16474 const struct symtab_index_entry *ea = a;
16475 const struct symtab_index_entry *eb = b;
16476 int len = VEC_length (offset_type, ea->cu_indices);
16477 if (len != VEC_length (offset_type, eb->cu_indices))
16478 return 0;
16479 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16480 VEC_address (offset_type, eb->cu_indices),
16481 sizeof (offset_type) * len);
16482}
16483
16484/* Destroy a symtab_index_entry. */
b89be57b 16485
9291a0cd
TT
16486static void
16487delete_symtab_entry (void *p)
16488{
16489 struct symtab_index_entry *entry = p;
16490 VEC_free (offset_type, entry->cu_indices);
16491 xfree (entry);
16492}
16493
16494/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16495
9291a0cd 16496static htab_t
3876f04e 16497create_symbol_hash_table (void)
9291a0cd
TT
16498{
16499 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16500 delete_symtab_entry, xcalloc, xfree);
16501}
16502
16503/* Create a new mapped symtab object. */
b89be57b 16504
9291a0cd
TT
16505static struct mapped_symtab *
16506create_mapped_symtab (void)
16507{
16508 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16509 symtab->n_elements = 0;
16510 symtab->size = 1024;
16511 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16512 return symtab;
16513}
16514
16515/* Destroy a mapped_symtab. */
b89be57b 16516
9291a0cd
TT
16517static void
16518cleanup_mapped_symtab (void *p)
16519{
16520 struct mapped_symtab *symtab = p;
16521 /* The contents of the array are freed when the other hash table is
16522 destroyed. */
16523 xfree (symtab->data);
16524 xfree (symtab);
16525}
16526
16527/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16528 the slot.
16529
16530 Function is used only during write_hash_table so no index format backward
16531 compatibility is needed. */
b89be57b 16532
9291a0cd
TT
16533static struct symtab_index_entry **
16534find_slot (struct mapped_symtab *symtab, const char *name)
16535{
559a7a62 16536 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16537
16538 index = hash & (symtab->size - 1);
16539 step = ((hash * 17) & (symtab->size - 1)) | 1;
16540
16541 for (;;)
16542 {
16543 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16544 return &symtab->data[index];
16545 index = (index + step) & (symtab->size - 1);
16546 }
16547}
16548
16549/* Expand SYMTAB's hash table. */
b89be57b 16550
9291a0cd
TT
16551static void
16552hash_expand (struct mapped_symtab *symtab)
16553{
16554 offset_type old_size = symtab->size;
16555 offset_type i;
16556 struct symtab_index_entry **old_entries = symtab->data;
16557
16558 symtab->size *= 2;
16559 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16560
16561 for (i = 0; i < old_size; ++i)
16562 {
16563 if (old_entries[i])
16564 {
16565 struct symtab_index_entry **slot = find_slot (symtab,
16566 old_entries[i]->name);
16567 *slot = old_entries[i];
16568 }
16569 }
16570
16571 xfree (old_entries);
16572}
16573
16574/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16575 is the index of the CU in which the symbol appears. */
b89be57b 16576
9291a0cd
TT
16577static void
16578add_index_entry (struct mapped_symtab *symtab, const char *name,
16579 offset_type cu_index)
16580{
16581 struct symtab_index_entry **slot;
16582
16583 ++symtab->n_elements;
16584 if (4 * symtab->n_elements / 3 >= symtab->size)
16585 hash_expand (symtab);
16586
16587 slot = find_slot (symtab, name);
16588 if (!*slot)
16589 {
16590 *slot = XNEW (struct symtab_index_entry);
16591 (*slot)->name = name;
16592 (*slot)->cu_indices = NULL;
16593 }
16594 /* Don't push an index twice. Due to how we add entries we only
16595 have to check the last one. */
16596 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16597 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16598 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16599}
16600
16601/* Add a vector of indices to the constant pool. */
b89be57b 16602
9291a0cd 16603static offset_type
3876f04e 16604add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16605 struct symtab_index_entry *entry)
16606{
16607 void **slot;
16608
3876f04e 16609 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16610 if (!*slot)
16611 {
16612 offset_type len = VEC_length (offset_type, entry->cu_indices);
16613 offset_type val = MAYBE_SWAP (len);
16614 offset_type iter;
16615 int i;
16616
16617 *slot = entry;
16618 entry->index_offset = obstack_object_size (cpool);
16619
16620 obstack_grow (cpool, &val, sizeof (val));
16621 for (i = 0;
16622 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16623 ++i)
16624 {
16625 val = MAYBE_SWAP (iter);
16626 obstack_grow (cpool, &val, sizeof (val));
16627 }
16628 }
16629 else
16630 {
16631 struct symtab_index_entry *old_entry = *slot;
16632 entry->index_offset = old_entry->index_offset;
16633 entry = old_entry;
16634 }
16635 return entry->index_offset;
16636}
16637
16638/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16639 constant pool entries going into the obstack CPOOL. */
b89be57b 16640
9291a0cd
TT
16641static void
16642write_hash_table (struct mapped_symtab *symtab,
16643 struct obstack *output, struct obstack *cpool)
16644{
16645 offset_type i;
3876f04e 16646 htab_t symbol_hash_table;
9291a0cd
TT
16647 htab_t str_table;
16648
3876f04e 16649 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16650 str_table = create_strtab ();
3876f04e 16651
9291a0cd
TT
16652 /* We add all the index vectors to the constant pool first, to
16653 ensure alignment is ok. */
16654 for (i = 0; i < symtab->size; ++i)
16655 {
16656 if (symtab->data[i])
3876f04e 16657 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16658 }
16659
16660 /* Now write out the hash table. */
16661 for (i = 0; i < symtab->size; ++i)
16662 {
16663 offset_type str_off, vec_off;
16664
16665 if (symtab->data[i])
16666 {
16667 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16668 vec_off = symtab->data[i]->index_offset;
16669 }
16670 else
16671 {
16672 /* While 0 is a valid constant pool index, it is not valid
16673 to have 0 for both offsets. */
16674 str_off = 0;
16675 vec_off = 0;
16676 }
16677
16678 str_off = MAYBE_SWAP (str_off);
16679 vec_off = MAYBE_SWAP (vec_off);
16680
16681 obstack_grow (output, &str_off, sizeof (str_off));
16682 obstack_grow (output, &vec_off, sizeof (vec_off));
16683 }
16684
16685 htab_delete (str_table);
3876f04e 16686 htab_delete (symbol_hash_table);
9291a0cd
TT
16687}
16688
0a5429f6
DE
16689/* Struct to map psymtab to CU index in the index file. */
16690struct psymtab_cu_index_map
16691{
16692 struct partial_symtab *psymtab;
16693 unsigned int cu_index;
16694};
16695
16696static hashval_t
16697hash_psymtab_cu_index (const void *item)
16698{
16699 const struct psymtab_cu_index_map *map = item;
16700
16701 return htab_hash_pointer (map->psymtab);
16702}
16703
16704static int
16705eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16706{
16707 const struct psymtab_cu_index_map *lhs = item_lhs;
16708 const struct psymtab_cu_index_map *rhs = item_rhs;
16709
16710 return lhs->psymtab == rhs->psymtab;
16711}
16712
16713/* Helper struct for building the address table. */
16714struct addrmap_index_data
16715{
16716 struct objfile *objfile;
16717 struct obstack *addr_obstack;
16718 htab_t cu_index_htab;
16719
16720 /* Non-zero if the previous_* fields are valid.
16721 We can't write an entry until we see the next entry (since it is only then
16722 that we know the end of the entry). */
16723 int previous_valid;
16724 /* Index of the CU in the table of all CUs in the index file. */
16725 unsigned int previous_cu_index;
0963b4bd 16726 /* Start address of the CU. */
0a5429f6
DE
16727 CORE_ADDR previous_cu_start;
16728};
16729
16730/* Write an address entry to OBSTACK. */
b89be57b 16731
9291a0cd 16732static void
0a5429f6
DE
16733add_address_entry (struct objfile *objfile, struct obstack *obstack,
16734 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16735{
0a5429f6 16736 offset_type cu_index_to_write;
9291a0cd
TT
16737 char addr[8];
16738 CORE_ADDR baseaddr;
16739
16740 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16741
0a5429f6
DE
16742 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16743 obstack_grow (obstack, addr, 8);
16744 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16745 obstack_grow (obstack, addr, 8);
16746 cu_index_to_write = MAYBE_SWAP (cu_index);
16747 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16748}
16749
16750/* Worker function for traversing an addrmap to build the address table. */
16751
16752static int
16753add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16754{
16755 struct addrmap_index_data *data = datap;
16756 struct partial_symtab *pst = obj;
16757 offset_type cu_index;
16758 void **slot;
16759
16760 if (data->previous_valid)
16761 add_address_entry (data->objfile, data->addr_obstack,
16762 data->previous_cu_start, start_addr,
16763 data->previous_cu_index);
16764
16765 data->previous_cu_start = start_addr;
16766 if (pst != NULL)
16767 {
16768 struct psymtab_cu_index_map find_map, *map;
16769 find_map.psymtab = pst;
16770 map = htab_find (data->cu_index_htab, &find_map);
16771 gdb_assert (map != NULL);
16772 data->previous_cu_index = map->cu_index;
16773 data->previous_valid = 1;
16774 }
16775 else
16776 data->previous_valid = 0;
16777
16778 return 0;
16779}
16780
16781/* Write OBJFILE's address map to OBSTACK.
16782 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16783 in the index file. */
16784
16785static void
16786write_address_map (struct objfile *objfile, struct obstack *obstack,
16787 htab_t cu_index_htab)
16788{
16789 struct addrmap_index_data addrmap_index_data;
16790
16791 /* When writing the address table, we have to cope with the fact that
16792 the addrmap iterator only provides the start of a region; we have to
16793 wait until the next invocation to get the start of the next region. */
16794
16795 addrmap_index_data.objfile = objfile;
16796 addrmap_index_data.addr_obstack = obstack;
16797 addrmap_index_data.cu_index_htab = cu_index_htab;
16798 addrmap_index_data.previous_valid = 0;
16799
16800 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16801 &addrmap_index_data);
16802
16803 /* It's highly unlikely the last entry (end address = 0xff...ff)
16804 is valid, but we should still handle it.
16805 The end address is recorded as the start of the next region, but that
16806 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16807 anyway. */
16808 if (addrmap_index_data.previous_valid)
16809 add_address_entry (objfile, obstack,
16810 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16811 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16812}
16813
16814/* Add a list of partial symbols to SYMTAB. */
b89be57b 16815
9291a0cd
TT
16816static void
16817write_psymbols (struct mapped_symtab *symtab,
987d643c 16818 htab_t psyms_seen,
9291a0cd
TT
16819 struct partial_symbol **psymp,
16820 int count,
987d643c
TT
16821 offset_type cu_index,
16822 int is_static)
9291a0cd
TT
16823{
16824 for (; count-- > 0; ++psymp)
16825 {
987d643c
TT
16826 void **slot, *lookup;
16827
9291a0cd
TT
16828 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16829 error (_("Ada is not currently supported by the index"));
987d643c
TT
16830
16831 /* We only want to add a given psymbol once. However, we also
16832 want to account for whether it is global or static. So, we
16833 may add it twice, using slightly different values. */
16834 if (is_static)
16835 {
16836 uintptr_t val = 1 | (uintptr_t) *psymp;
16837
16838 lookup = (void *) val;
16839 }
16840 else
16841 lookup = *psymp;
16842
16843 /* Only add a given psymbol once. */
16844 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16845 if (!*slot)
16846 {
16847 *slot = lookup;
16848 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16849 }
9291a0cd
TT
16850 }
16851}
16852
16853/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16854 exception if there is an error. */
b89be57b 16855
9291a0cd
TT
16856static void
16857write_obstack (FILE *file, struct obstack *obstack)
16858{
16859 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16860 file)
16861 != obstack_object_size (obstack))
16862 error (_("couldn't data write to file"));
16863}
16864
16865/* Unlink a file if the argument is not NULL. */
b89be57b 16866
9291a0cd
TT
16867static void
16868unlink_if_set (void *p)
16869{
16870 char **filename = p;
16871 if (*filename)
16872 unlink (*filename);
16873}
16874
1fd400ff
TT
16875/* A helper struct used when iterating over debug_types. */
16876struct signatured_type_index_data
16877{
16878 struct objfile *objfile;
16879 struct mapped_symtab *symtab;
16880 struct obstack *types_list;
987d643c 16881 htab_t psyms_seen;
1fd400ff
TT
16882 int cu_index;
16883};
16884
16885/* A helper function that writes a single signatured_type to an
16886 obstack. */
b89be57b 16887
1fd400ff
TT
16888static int
16889write_one_signatured_type (void **slot, void *d)
16890{
16891 struct signatured_type_index_data *info = d;
16892 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16893 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16894 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16895 gdb_byte val[8];
16896
16897 write_psymbols (info->symtab,
987d643c 16898 info->psyms_seen,
3e43a32a
MS
16899 info->objfile->global_psymbols.list
16900 + psymtab->globals_offset,
987d643c
TT
16901 psymtab->n_global_syms, info->cu_index,
16902 0);
1fd400ff 16903 write_psymbols (info->symtab,
987d643c 16904 info->psyms_seen,
3e43a32a
MS
16905 info->objfile->static_psymbols.list
16906 + psymtab->statics_offset,
987d643c
TT
16907 psymtab->n_static_syms, info->cu_index,
16908 1);
1fd400ff 16909
b3c8eb43 16910 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16911 obstack_grow (info->types_list, val, 8);
16912 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16913 obstack_grow (info->types_list, val, 8);
16914 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16915 obstack_grow (info->types_list, val, 8);
16916
16917 ++info->cu_index;
16918
16919 return 1;
16920}
16921
9291a0cd 16922/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16923
9291a0cd
TT
16924static void
16925write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16926{
16927 struct cleanup *cleanup;
16928 char *filename, *cleanup_filename;
1fd400ff
TT
16929 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16930 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16931 int i;
16932 FILE *out_file;
16933 struct mapped_symtab *symtab;
16934 offset_type val, size_of_contents, total_len;
16935 struct stat st;
16936 char buf[8];
987d643c 16937 htab_t psyms_seen;
0a5429f6
DE
16938 htab_t cu_index_htab;
16939 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16940
b4f2f049 16941 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16942 return;
b4f2f049 16943
9291a0cd
TT
16944 if (dwarf2_per_objfile->using_index)
16945 error (_("Cannot use an index to create the index"));
16946
8b70b953
TT
16947 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16948 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16949
9291a0cd 16950 if (stat (objfile->name, &st) < 0)
7e17e088 16951 perror_with_name (objfile->name);
9291a0cd
TT
16952
16953 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16954 INDEX_SUFFIX, (char *) NULL);
16955 cleanup = make_cleanup (xfree, filename);
16956
16957 out_file = fopen (filename, "wb");
16958 if (!out_file)
16959 error (_("Can't open `%s' for writing"), filename);
16960
16961 cleanup_filename = filename;
16962 make_cleanup (unlink_if_set, &cleanup_filename);
16963
16964 symtab = create_mapped_symtab ();
16965 make_cleanup (cleanup_mapped_symtab, symtab);
16966
16967 obstack_init (&addr_obstack);
16968 make_cleanup_obstack_free (&addr_obstack);
16969
16970 obstack_init (&cu_list);
16971 make_cleanup_obstack_free (&cu_list);
16972
1fd400ff
TT
16973 obstack_init (&types_cu_list);
16974 make_cleanup_obstack_free (&types_cu_list);
16975
987d643c
TT
16976 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16977 NULL, xcalloc, xfree);
96408a79 16978 make_cleanup_htab_delete (psyms_seen);
987d643c 16979
0a5429f6
DE
16980 /* While we're scanning CU's create a table that maps a psymtab pointer
16981 (which is what addrmap records) to its index (which is what is recorded
16982 in the index file). This will later be needed to write the address
16983 table. */
16984 cu_index_htab = htab_create_alloc (100,
16985 hash_psymtab_cu_index,
16986 eq_psymtab_cu_index,
16987 NULL, xcalloc, xfree);
96408a79 16988 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16989 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16990 xmalloc (sizeof (struct psymtab_cu_index_map)
16991 * dwarf2_per_objfile->n_comp_units);
16992 make_cleanup (xfree, psymtab_cu_index_map);
16993
16994 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16995 work here. Also, the debug_types entries do not appear in
16996 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16997 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16998 {
3e43a32a
MS
16999 struct dwarf2_per_cu_data *per_cu
17000 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17001 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17002 gdb_byte val[8];
0a5429f6
DE
17003 struct psymtab_cu_index_map *map;
17004 void **slot;
9291a0cd
TT
17005
17006 write_psymbols (symtab,
987d643c 17007 psyms_seen,
9291a0cd 17008 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17009 psymtab->n_global_syms, i,
17010 0);
9291a0cd 17011 write_psymbols (symtab,
987d643c 17012 psyms_seen,
9291a0cd 17013 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17014 psymtab->n_static_syms, i,
17015 1);
9291a0cd 17016
0a5429f6
DE
17017 map = &psymtab_cu_index_map[i];
17018 map->psymtab = psymtab;
17019 map->cu_index = i;
17020 slot = htab_find_slot (cu_index_htab, map, INSERT);
17021 gdb_assert (slot != NULL);
17022 gdb_assert (*slot == NULL);
17023 *slot = map;
9291a0cd 17024
e254ef6a 17025 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 17026 obstack_grow (&cu_list, val, 8);
e254ef6a 17027 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17028 obstack_grow (&cu_list, val, 8);
17029 }
17030
0a5429f6
DE
17031 /* Dump the address map. */
17032 write_address_map (objfile, &addr_obstack, cu_index_htab);
17033
1fd400ff
TT
17034 /* Write out the .debug_type entries, if any. */
17035 if (dwarf2_per_objfile->signatured_types)
17036 {
17037 struct signatured_type_index_data sig_data;
17038
17039 sig_data.objfile = objfile;
17040 sig_data.symtab = symtab;
17041 sig_data.types_list = &types_cu_list;
987d643c 17042 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17043 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17044 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17045 write_one_signatured_type, &sig_data);
17046 }
17047
9291a0cd
TT
17048 obstack_init (&constant_pool);
17049 make_cleanup_obstack_free (&constant_pool);
17050 obstack_init (&symtab_obstack);
17051 make_cleanup_obstack_free (&symtab_obstack);
17052 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17053
17054 obstack_init (&contents);
17055 make_cleanup_obstack_free (&contents);
1fd400ff 17056 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17057 total_len = size_of_contents;
17058
17059 /* The version number. */
559a7a62 17060 val = MAYBE_SWAP (5);
9291a0cd
TT
17061 obstack_grow (&contents, &val, sizeof (val));
17062
17063 /* The offset of the CU list from the start of the file. */
17064 val = MAYBE_SWAP (total_len);
17065 obstack_grow (&contents, &val, sizeof (val));
17066 total_len += obstack_object_size (&cu_list);
17067
1fd400ff
TT
17068 /* The offset of the types CU list from the start of the file. */
17069 val = MAYBE_SWAP (total_len);
17070 obstack_grow (&contents, &val, sizeof (val));
17071 total_len += obstack_object_size (&types_cu_list);
17072
9291a0cd
TT
17073 /* The offset of the address table from the start of the file. */
17074 val = MAYBE_SWAP (total_len);
17075 obstack_grow (&contents, &val, sizeof (val));
17076 total_len += obstack_object_size (&addr_obstack);
17077
17078 /* The offset of the symbol table from the start of the file. */
17079 val = MAYBE_SWAP (total_len);
17080 obstack_grow (&contents, &val, sizeof (val));
17081 total_len += obstack_object_size (&symtab_obstack);
17082
17083 /* The offset of the constant pool from the start of the file. */
17084 val = MAYBE_SWAP (total_len);
17085 obstack_grow (&contents, &val, sizeof (val));
17086 total_len += obstack_object_size (&constant_pool);
17087
17088 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17089
17090 write_obstack (out_file, &contents);
17091 write_obstack (out_file, &cu_list);
1fd400ff 17092 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17093 write_obstack (out_file, &addr_obstack);
17094 write_obstack (out_file, &symtab_obstack);
17095 write_obstack (out_file, &constant_pool);
17096
17097 fclose (out_file);
17098
17099 /* We want to keep the file, so we set cleanup_filename to NULL
17100 here. See unlink_if_set. */
17101 cleanup_filename = NULL;
17102
17103 do_cleanups (cleanup);
17104}
17105
90476074
TT
17106/* Implementation of the `save gdb-index' command.
17107
17108 Note that the file format used by this command is documented in the
17109 GDB manual. Any changes here must be documented there. */
11570e71 17110
9291a0cd
TT
17111static void
17112save_gdb_index_command (char *arg, int from_tty)
17113{
17114 struct objfile *objfile;
17115
17116 if (!arg || !*arg)
96d19272 17117 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17118
17119 ALL_OBJFILES (objfile)
17120 {
17121 struct stat st;
17122
17123 /* If the objfile does not correspond to an actual file, skip it. */
17124 if (stat (objfile->name, &st) < 0)
17125 continue;
17126
17127 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17128 if (dwarf2_per_objfile)
17129 {
17130 volatile struct gdb_exception except;
17131
17132 TRY_CATCH (except, RETURN_MASK_ERROR)
17133 {
17134 write_psymtabs_to_index (objfile, arg);
17135 }
17136 if (except.reason < 0)
17137 exception_fprintf (gdb_stderr, except,
17138 _("Error while writing index for `%s': "),
17139 objfile->name);
17140 }
17141 }
dce234bc
PP
17142}
17143
9291a0cd
TT
17144\f
17145
9eae7c52
TT
17146int dwarf2_always_disassemble;
17147
17148static void
17149show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17150 struct cmd_list_element *c, const char *value)
17151{
3e43a32a
MS
17152 fprintf_filtered (file,
17153 _("Whether to always disassemble "
17154 "DWARF expressions is %s.\n"),
9eae7c52
TT
17155 value);
17156}
17157
900e11f9
JK
17158static void
17159show_check_physname (struct ui_file *file, int from_tty,
17160 struct cmd_list_element *c, const char *value)
17161{
17162 fprintf_filtered (file,
17163 _("Whether to check \"physname\" is %s.\n"),
17164 value);
17165}
17166
6502dd73
DJ
17167void _initialize_dwarf2_read (void);
17168
17169void
17170_initialize_dwarf2_read (void)
17171{
96d19272
JK
17172 struct cmd_list_element *c;
17173
dce234bc 17174 dwarf2_objfile_data_key
c1bd65d0 17175 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17176
1bedd215
AC
17177 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17178Set DWARF 2 specific variables.\n\
17179Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17180 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17181 0/*allow-unknown*/, &maintenance_set_cmdlist);
17182
1bedd215
AC
17183 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17184Show DWARF 2 specific variables\n\
17185Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17186 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17187 0/*allow-unknown*/, &maintenance_show_cmdlist);
17188
17189 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17190 &dwarf2_max_cache_age, _("\
17191Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17192Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17193A higher limit means that cached compilation units will be stored\n\
17194in memory longer, and more total memory will be used. Zero disables\n\
17195caching, which can slow down startup."),
2c5b56ce 17196 NULL,
920d2a44 17197 show_dwarf2_max_cache_age,
2c5b56ce 17198 &set_dwarf2_cmdlist,
ae038cb0 17199 &show_dwarf2_cmdlist);
d97bc12b 17200
9eae7c52
TT
17201 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17202 &dwarf2_always_disassemble, _("\
17203Set whether `info address' always disassembles DWARF expressions."), _("\
17204Show whether `info address' always disassembles DWARF expressions."), _("\
17205When enabled, DWARF expressions are always printed in an assembly-like\n\
17206syntax. When disabled, expressions will be printed in a more\n\
17207conversational style, when possible."),
17208 NULL,
17209 show_dwarf2_always_disassemble,
17210 &set_dwarf2_cmdlist,
17211 &show_dwarf2_cmdlist);
17212
d97bc12b
DE
17213 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17214Set debugging of the dwarf2 DIE reader."), _("\
17215Show debugging of the dwarf2 DIE reader."), _("\
17216When enabled (non-zero), DIEs are dumped after they are read in.\n\
17217The value is the maximum depth to print."),
17218 NULL,
17219 NULL,
17220 &setdebuglist, &showdebuglist);
9291a0cd 17221
900e11f9
JK
17222 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17223Set cross-checking of \"physname\" code against demangler."), _("\
17224Show cross-checking of \"physname\" code against demangler."), _("\
17225When enabled, GDB's internal \"physname\" code is checked against\n\
17226the demangler."),
17227 NULL, show_check_physname,
17228 &setdebuglist, &showdebuglist);
17229
96d19272 17230 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17231 _("\
fc1a9d6e 17232Save a gdb-index file.\n\
11570e71 17233Usage: save gdb-index DIRECTORY"),
96d19272
JK
17234 &save_cmdlist);
17235 set_cmd_completer (c, filename_completer);
6502dd73 17236}
This page took 2.258548 seconds and 4 git commands to generate.