2011-07-25 Pedro Alves <pedro@codesourcery.com>
[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"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
900e11f9
JK
122/* When non-zero, cross-check physname against demangler. */
123static int check_physname = 0;
124
dce234bc
PP
125static int pagesize;
126
df8a16a1
DJ
127/* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131static int processing_has_namespace_info;
132
6502dd73
DJ
133static const struct objfile_data *dwarf2_objfile_data_key;
134
dce234bc
PP
135struct dwarf2_section_info
136{
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
b315ab21
TG
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
be391dca
TT
144 /* True if we have tried to read this section. */
145 int readin;
dce234bc
PP
146};
147
8b70b953
TT
148typedef struct dwarf2_section_info dwarf2_section_info_def;
149DEF_VEC_O (dwarf2_section_info_def);
150
9291a0cd
TT
151/* All offsets in the index are of this type. It must be
152 architecture-independent. */
153typedef uint32_t offset_type;
154
155DEF_VEC_I (offset_type);
156
157/* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159struct mapped_index
160{
559a7a62
JK
161 /* Index data format version. */
162 int version;
163
9291a0cd
TT
164 /* The total length of the buffer. */
165 off_t total_size;
b11b1f88 166
9291a0cd
TT
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
b11b1f88 169
9291a0cd
TT
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
b11b1f88 172
3876f04e
DE
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
b11b1f88 175
9291a0cd 176 /* Size in slots, each slot is 2 offset_types. */
3876f04e 177 offset_type symbol_table_slots;
b11b1f88 178
9291a0cd
TT
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181};
182
6502dd73
DJ
183struct dwarf2_per_objfile
184{
dce234bc
PP
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
dce234bc
PP
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
190 struct dwarf2_section_info str;
191 struct dwarf2_section_info ranges;
192 struct dwarf2_section_info frame;
193 struct dwarf2_section_info eh_frame;
9291a0cd 194 struct dwarf2_section_info gdb_index;
ae038cb0 195
8b70b953
TT
196 VEC (dwarf2_section_info_def) *types;
197
be391dca
TT
198 /* Back link. */
199 struct objfile *objfile;
200
10b3939b
DJ
201 /* A list of all the compilation units. This is used to locate
202 the target compilation unit of a particular reference. */
ae038cb0
DJ
203 struct dwarf2_per_cu_data **all_comp_units;
204
205 /* The number of compilation units in ALL_COMP_UNITS. */
206 int n_comp_units;
207
1fd400ff
TT
208 /* The number of .debug_types-related CUs. */
209 int n_type_comp_units;
210
211 /* The .debug_types-related CUs. */
212 struct dwarf2_per_cu_data **type_comp_units;
213
ae038cb0
DJ
214 /* A chain of compilation units that are currently read in, so that
215 they can be freed later. */
216 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 217
348e048f
DE
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
221
72dca2f5
FR
222 /* A flag indicating wether this objfile has a section loaded at a
223 VMA of 0. */
224 int has_section_at_zero;
9291a0cd 225
ae2de4f8
DE
226 /* True if we are using the mapped index,
227 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
228 unsigned char using_index;
229
ae2de4f8 230 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 231 struct mapped_index *index_table;
98bfdba5 232
7b9f3c50
DE
233 /* When using index_table, this keeps track of all quick_file_names entries.
234 TUs can share line table entries with CUs or other TUs, and there can be
235 a lot more TUs than unique line tables, so we maintain a separate table
236 of all line table entries to support the sharing. */
237 htab_t quick_file_names_table;
238
98bfdba5
PA
239 /* Set during partial symbol reading, to prevent queueing of full
240 symbols. */
241 int reading_partial_symbols;
673bfd45
DE
242
243 /* Table mapping type .debug_info DIE offsets to types.
244 This is NULL if not allocated yet.
245 It (currently) makes sense to allocate debug_types_type_hash lazily.
246 To keep things simple we allocate both lazily. */
247 htab_t debug_info_type_hash;
248
249 /* Table mapping type .debug_types DIE offsets to types.
250 This is NULL if not allocated yet. */
251 htab_t debug_types_type_hash;
6502dd73
DJ
252};
253
254static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 255
251d32d9 256/* Default names of the debugging sections. */
c906108c 257
233a11ab
CS
258/* Note that if the debugging section has been compressed, it might
259 have a name like .zdebug_info. */
260
251d32d9
TG
261static const struct dwarf2_debug_sections dwarf2_elf_names = {
262 { ".debug_info", ".zdebug_info" },
263 { ".debug_abbrev", ".zdebug_abbrev" },
264 { ".debug_line", ".zdebug_line" },
265 { ".debug_loc", ".zdebug_loc" },
266 { ".debug_macinfo", ".zdebug_macinfo" },
267 { ".debug_str", ".zdebug_str" },
268 { ".debug_ranges", ".zdebug_ranges" },
269 { ".debug_types", ".zdebug_types" },
270 { ".debug_frame", ".zdebug_frame" },
271 { ".eh_frame", NULL },
272 { ".gdb_index", ".zgdb_index" }
273};
c906108c
SS
274
275/* local data types */
276
0963b4bd 277/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
278#ifndef ABBREV_HASH_SIZE
279#define ABBREV_HASH_SIZE 121
280#endif
281
107d2387
AC
282/* The data in a compilation unit header, after target2host
283 translation, looks like this. */
c906108c 284struct comp_unit_head
a738430d 285{
c764a876 286 unsigned int length;
a738430d 287 short version;
a738430d
MK
288 unsigned char addr_size;
289 unsigned char signed_addr_p;
9cbfa09e 290 unsigned int abbrev_offset;
57349743 291
a738430d
MK
292 /* Size of file offsets; either 4 or 8. */
293 unsigned int offset_size;
57349743 294
a738430d
MK
295 /* Size of the length field; either 4 or 12. */
296 unsigned int initial_length_size;
57349743 297
a738430d
MK
298 /* Offset to the first byte of this compilation unit header in the
299 .debug_info section, for resolving relative reference dies. */
300 unsigned int offset;
57349743 301
d00adf39
DE
302 /* Offset to first die in this cu from the start of the cu.
303 This will be the first byte following the compilation unit header. */
304 unsigned int first_die_offset;
a738430d 305};
c906108c 306
3da10d80
KS
307/* Type used for delaying computation of method physnames.
308 See comments for compute_delayed_physnames. */
309struct delayed_method_info
310{
311 /* The type to which the method is attached, i.e., its parent class. */
312 struct type *type;
313
314 /* The index of the method in the type's function fieldlists. */
315 int fnfield_index;
316
317 /* The index of the method in the fieldlist. */
318 int index;
319
320 /* The name of the DIE. */
321 const char *name;
322
323 /* The DIE associated with this method. */
324 struct die_info *die;
325};
326
327typedef struct delayed_method_info delayed_method_info;
328DEF_VEC_O (delayed_method_info);
329
e7c27a73
DJ
330/* Internal state when decoding a particular compilation unit. */
331struct dwarf2_cu
332{
333 /* The objfile containing this compilation unit. */
334 struct objfile *objfile;
335
d00adf39 336 /* The header of the compilation unit. */
e7c27a73 337 struct comp_unit_head header;
e142c38c 338
d00adf39
DE
339 /* Base address of this compilation unit. */
340 CORE_ADDR base_address;
341
342 /* Non-zero if base_address has been set. */
343 int base_known;
344
e142c38c
DJ
345 struct function_range *first_fn, *last_fn, *cached_fn;
346
347 /* The language we are debugging. */
348 enum language language;
349 const struct language_defn *language_defn;
350
b0f35d58
DL
351 const char *producer;
352
e142c38c
DJ
353 /* The generic symbol table building routines have separate lists for
354 file scope symbols and all all other scopes (local scopes). So
355 we need to select the right one to pass to add_symbol_to_list().
356 We do it by keeping a pointer to the correct list in list_in_scope.
357
358 FIXME: The original dwarf code just treated the file scope as the
359 first local scope, and all other local scopes as nested local
360 scopes, and worked fine. Check to see if we really need to
361 distinguish these in buildsym.c. */
362 struct pending **list_in_scope;
363
f3dd6933
DJ
364 /* DWARF abbreviation table associated with this compilation unit. */
365 struct abbrev_info **dwarf2_abbrevs;
366
367 /* Storage for the abbrev table. */
368 struct obstack abbrev_obstack;
72bf9492
DJ
369
370 /* Hash table holding all the loaded partial DIEs. */
371 htab_t partial_dies;
372
373 /* Storage for things with the same lifetime as this read-in compilation
374 unit, including partial DIEs. */
375 struct obstack comp_unit_obstack;
376
ae038cb0
DJ
377 /* When multiple dwarf2_cu structures are living in memory, this field
378 chains them all together, so that they can be released efficiently.
379 We will probably also want a generation counter so that most-recently-used
380 compilation units are cached... */
381 struct dwarf2_per_cu_data *read_in_chain;
382
383 /* Backchain to our per_cu entry if the tree has been built. */
384 struct dwarf2_per_cu_data *per_cu;
385
386 /* How many compilation units ago was this CU last referenced? */
387 int last_used;
388
10b3939b 389 /* A hash table of die offsets for following references. */
51545339 390 htab_t die_hash;
10b3939b
DJ
391
392 /* Full DIEs if read in. */
393 struct die_info *dies;
394
395 /* A set of pointers to dwarf2_per_cu_data objects for compilation
396 units referenced by this one. Only set during full symbol processing;
397 partial symbol tables do not have dependencies. */
398 htab_t dependencies;
399
cb1df416
DJ
400 /* Header data from the line table, during full symbol processing. */
401 struct line_header *line_header;
402
3da10d80
KS
403 /* A list of methods which need to have physnames computed
404 after all type information has been read. */
405 VEC (delayed_method_info) *method_list;
406
ae038cb0
DJ
407 /* Mark used when releasing cached dies. */
408 unsigned int mark : 1;
409
410 /* This flag will be set if this compilation unit might include
411 inter-compilation-unit references. */
412 unsigned int has_form_ref_addr : 1;
413
72bf9492
DJ
414 /* This flag will be set if this compilation unit includes any
415 DW_TAG_namespace DIEs. If we know that there are explicit
416 DIEs for namespaces, we don't need to try to infer them
417 from mangled names. */
418 unsigned int has_namespace_info : 1;
8be455d7
JK
419
420 /* This CU references .debug_loc. See the symtab->locations_valid field.
421 This test is imperfect as there may exist optimized debug code not using
422 any location list and still facing inlining issues if handled as
423 unoptimized code. For a future better test see GCC PR other/32998. */
424
425 unsigned int has_loclist : 1;
e7c27a73
DJ
426};
427
10b3939b
DJ
428/* Persistent data held for a compilation unit, even when not
429 processing it. We put a pointer to this structure in the
430 read_symtab_private field of the psymtab. If we encounter
431 inter-compilation-unit references, we also maintain a sorted
432 list of all compilation units. */
433
ae038cb0
DJ
434struct dwarf2_per_cu_data
435{
348e048f 436 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 437 bytes should suffice to store the length of any compilation unit
45452591
DE
438 - if it doesn't, GDB will fall over anyway.
439 NOTE: Unlike comp_unit_head.length, this length includes
440 initial_length_size. */
c764a876 441 unsigned int offset;
348e048f 442 unsigned int length : 29;
ae038cb0
DJ
443
444 /* Flag indicating this compilation unit will be read in before
445 any of the current compilation units are processed. */
c764a876 446 unsigned int queued : 1;
ae038cb0 447
5afb4e99
DJ
448 /* This flag will be set if we need to load absolutely all DIEs
449 for this compilation unit, instead of just the ones we think
450 are interesting. It gets set if we look for a DIE in the
451 hash table and don't find it. */
452 unsigned int load_all_dies : 1;
453
8b70b953
TT
454 /* Non-null if this CU is from .debug_types; in which case it points
455 to the section. Otherwise it's from .debug_info. */
456 struct dwarf2_section_info *debug_type_section;
348e048f 457
17ea53c3
JK
458 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
459 of the CU cache it gets reset to NULL again. */
ae038cb0 460 struct dwarf2_cu *cu;
1c379e20 461
9291a0cd
TT
462 /* The corresponding objfile. */
463 struct objfile *objfile;
464
465 /* When using partial symbol tables, the 'psymtab' field is active.
466 Otherwise the 'quick' field is active. */
467 union
468 {
469 /* The partial symbol table associated with this compilation unit,
470 or NULL for partial units (which do not have an associated
471 symtab). */
472 struct partial_symtab *psymtab;
473
474 /* Data needed by the "quick" functions. */
475 struct dwarf2_per_cu_quick_data *quick;
476 } v;
ae038cb0
DJ
477};
478
348e048f
DE
479/* Entry in the signatured_types hash table. */
480
481struct signatured_type
482{
483 ULONGEST signature;
484
348e048f
DE
485 /* Offset in .debug_types of the type defined by this TU. */
486 unsigned int type_offset;
487
488 /* The CU(/TU) of this type. */
489 struct dwarf2_per_cu_data per_cu;
490};
491
0963b4bd
MS
492/* Struct used to pass misc. parameters to read_die_and_children, et
493 al. which are used for both .debug_info and .debug_types dies.
494 All parameters here are unchanging for the life of the call. This
495 struct exists to abstract away the constant parameters of die
496 reading. */
93311388
DE
497
498struct die_reader_specs
499{
500 /* The bfd of this objfile. */
501 bfd* abfd;
502
503 /* The CU of the DIE we are parsing. */
504 struct dwarf2_cu *cu;
505
506 /* Pointer to start of section buffer.
507 This is either the start of .debug_info or .debug_types. */
508 const gdb_byte *buffer;
509};
510
debd256d
JB
511/* The line number information for a compilation unit (found in the
512 .debug_line section) begins with a "statement program header",
513 which contains the following information. */
514struct line_header
515{
516 unsigned int total_length;
517 unsigned short version;
518 unsigned int header_length;
519 unsigned char minimum_instruction_length;
2dc7f7b3 520 unsigned char maximum_ops_per_instruction;
debd256d
JB
521 unsigned char default_is_stmt;
522 int line_base;
523 unsigned char line_range;
524 unsigned char opcode_base;
525
526 /* standard_opcode_lengths[i] is the number of operands for the
527 standard opcode whose value is i. This means that
528 standard_opcode_lengths[0] is unused, and the last meaningful
529 element is standard_opcode_lengths[opcode_base - 1]. */
530 unsigned char *standard_opcode_lengths;
531
532 /* The include_directories table. NOTE! These strings are not
533 allocated with xmalloc; instead, they are pointers into
534 debug_line_buffer. If you try to free them, `free' will get
535 indigestion. */
536 unsigned int num_include_dirs, include_dirs_size;
537 char **include_dirs;
538
539 /* The file_names table. NOTE! These strings are not allocated
540 with xmalloc; instead, they are pointers into debug_line_buffer.
541 Don't try to free them directly. */
542 unsigned int num_file_names, file_names_size;
543 struct file_entry
c906108c 544 {
debd256d
JB
545 char *name;
546 unsigned int dir_index;
547 unsigned int mod_time;
548 unsigned int length;
aaa75496 549 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 550 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
551 } *file_names;
552
553 /* The start and end of the statement program following this
6502dd73 554 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 555 gdb_byte *statement_program_start, *statement_program_end;
debd256d 556};
c906108c
SS
557
558/* When we construct a partial symbol table entry we only
0963b4bd 559 need this much information. */
c906108c
SS
560struct partial_die_info
561 {
72bf9492 562 /* Offset of this DIE. */
c906108c 563 unsigned int offset;
72bf9492
DJ
564
565 /* DWARF-2 tag for this DIE. */
566 ENUM_BITFIELD(dwarf_tag) tag : 16;
567
72bf9492
DJ
568 /* Assorted flags describing the data found in this DIE. */
569 unsigned int has_children : 1;
570 unsigned int is_external : 1;
571 unsigned int is_declaration : 1;
572 unsigned int has_type : 1;
573 unsigned int has_specification : 1;
574 unsigned int has_pc_info : 1;
575
576 /* Flag set if the SCOPE field of this structure has been
577 computed. */
578 unsigned int scope_set : 1;
579
fa4028e9
JB
580 /* Flag set if the DIE has a byte_size attribute. */
581 unsigned int has_byte_size : 1;
582
98bfdba5
PA
583 /* Flag set if any of the DIE's children are template arguments. */
584 unsigned int has_template_arguments : 1;
585
abc72ce4
DE
586 /* Flag set if fixup_partial_die has been called on this die. */
587 unsigned int fixup_called : 1;
588
72bf9492 589 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 590 sometimes a default name for unnamed DIEs. */
c906108c 591 char *name;
72bf9492 592
abc72ce4
DE
593 /* The linkage name, if present. */
594 const char *linkage_name;
595
72bf9492
DJ
596 /* The scope to prepend to our children. This is generally
597 allocated on the comp_unit_obstack, so will disappear
598 when this compilation unit leaves the cache. */
599 char *scope;
600
601 /* The location description associated with this DIE, if any. */
602 struct dwarf_block *locdesc;
603
604 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
605 CORE_ADDR lowpc;
606 CORE_ADDR highpc;
72bf9492 607
93311388 608 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 609 DW_AT_sibling, if any. */
abc72ce4
DE
610 /* NOTE: This member isn't strictly necessary, read_partial_die could
611 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 612 gdb_byte *sibling;
72bf9492
DJ
613
614 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
615 DW_AT_specification (or DW_AT_abstract_origin or
616 DW_AT_extension). */
617 unsigned int spec_offset;
618
619 /* Pointers to this DIE's parent, first child, and next sibling,
620 if any. */
621 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
622 };
623
0963b4bd 624/* This data structure holds the information of an abbrev. */
c906108c
SS
625struct abbrev_info
626 {
627 unsigned int number; /* number identifying abbrev */
628 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
629 unsigned short has_children; /* boolean */
630 unsigned short num_attrs; /* number of attributes */
c906108c
SS
631 struct attr_abbrev *attrs; /* an array of attribute descriptions */
632 struct abbrev_info *next; /* next in chain */
633 };
634
635struct attr_abbrev
636 {
9d25dd43
DE
637 ENUM_BITFIELD(dwarf_attribute) name : 16;
638 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
639 };
640
0963b4bd 641/* Attributes have a name and a value. */
b60c80d6
DJ
642struct attribute
643 {
9d25dd43 644 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
645 ENUM_BITFIELD(dwarf_form) form : 15;
646
647 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
648 field should be in u.str (existing only for DW_STRING) but it is kept
649 here for better struct attribute alignment. */
650 unsigned int string_is_canonical : 1;
651
b60c80d6
DJ
652 union
653 {
654 char *str;
655 struct dwarf_block *blk;
43bbcdc2
PH
656 ULONGEST unsnd;
657 LONGEST snd;
b60c80d6 658 CORE_ADDR addr;
348e048f 659 struct signatured_type *signatured_type;
b60c80d6
DJ
660 }
661 u;
662 };
663
0963b4bd 664/* This data structure holds a complete die structure. */
c906108c
SS
665struct die_info
666 {
76815b17
DE
667 /* DWARF-2 tag for this DIE. */
668 ENUM_BITFIELD(dwarf_tag) tag : 16;
669
670 /* Number of attributes */
98bfdba5
PA
671 unsigned char num_attrs;
672
673 /* True if we're presently building the full type name for the
674 type derived from this DIE. */
675 unsigned char building_fullname : 1;
76815b17
DE
676
677 /* Abbrev number */
678 unsigned int abbrev;
679
93311388 680 /* Offset in .debug_info or .debug_types section. */
76815b17 681 unsigned int offset;
78ba4af6
JB
682
683 /* The dies in a compilation unit form an n-ary tree. PARENT
684 points to this die's parent; CHILD points to the first child of
685 this node; and all the children of a given node are chained
4950bc1c 686 together via their SIBLING fields. */
639d11d3
DC
687 struct die_info *child; /* Its first child, if any. */
688 struct die_info *sibling; /* Its next sibling, if any. */
689 struct die_info *parent; /* Its parent, if any. */
c906108c 690
b60c80d6
DJ
691 /* An array of attributes, with NUM_ATTRS elements. There may be
692 zero, but it's not common and zero-sized arrays are not
693 sufficiently portable C. */
694 struct attribute attrs[1];
c906108c
SS
695 };
696
5fb290d7
DJ
697struct function_range
698{
699 const char *name;
700 CORE_ADDR lowpc, highpc;
701 int seen_line;
702 struct function_range *next;
703};
704
0963b4bd 705/* Get at parts of an attribute structure. */
c906108c
SS
706
707#define DW_STRING(attr) ((attr)->u.str)
8285870a 708#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
709#define DW_UNSND(attr) ((attr)->u.unsnd)
710#define DW_BLOCK(attr) ((attr)->u.blk)
711#define DW_SND(attr) ((attr)->u.snd)
712#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 713#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 714
0963b4bd 715/* Blocks are a bunch of untyped bytes. */
c906108c
SS
716struct dwarf_block
717 {
718 unsigned int size;
fe1b8b76 719 gdb_byte *data;
c906108c
SS
720 };
721
c906108c
SS
722#ifndef ATTR_ALLOC_CHUNK
723#define ATTR_ALLOC_CHUNK 4
724#endif
725
c906108c
SS
726/* Allocate fields for structs, unions and enums in this size. */
727#ifndef DW_FIELD_ALLOC_CHUNK
728#define DW_FIELD_ALLOC_CHUNK 4
729#endif
730
c906108c
SS
731/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
732 but this would require a corresponding change in unpack_field_as_long
733 and friends. */
734static int bits_per_byte = 8;
735
736/* The routines that read and process dies for a C struct or C++ class
737 pass lists of data member fields and lists of member function fields
738 in an instance of a field_info structure, as defined below. */
739struct field_info
c5aa993b 740 {
0963b4bd 741 /* List of data member and baseclasses fields. */
c5aa993b
JM
742 struct nextfield
743 {
744 struct nextfield *next;
745 int accessibility;
746 int virtuality;
747 struct field field;
748 }
7d0ccb61 749 *fields, *baseclasses;
c906108c 750
7d0ccb61 751 /* Number of fields (including baseclasses). */
c5aa993b 752 int nfields;
c906108c 753
c5aa993b
JM
754 /* Number of baseclasses. */
755 int nbaseclasses;
c906108c 756
c5aa993b
JM
757 /* Set if the accesibility of one of the fields is not public. */
758 int non_public_fields;
c906108c 759
c5aa993b
JM
760 /* Member function fields array, entries are allocated in the order they
761 are encountered in the object file. */
762 struct nextfnfield
763 {
764 struct nextfnfield *next;
765 struct fn_field fnfield;
766 }
767 *fnfields;
c906108c 768
c5aa993b
JM
769 /* Member function fieldlist array, contains name of possibly overloaded
770 member function, number of overloaded member functions and a pointer
771 to the head of the member function field chain. */
772 struct fnfieldlist
773 {
774 char *name;
775 int length;
776 struct nextfnfield *head;
777 }
778 *fnfieldlists;
c906108c 779
c5aa993b
JM
780 /* Number of entries in the fnfieldlists array. */
781 int nfnfields;
98751a41
JK
782
783 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
784 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
785 struct typedef_field_list
786 {
787 struct typedef_field field;
788 struct typedef_field_list *next;
789 }
790 *typedef_field_list;
791 unsigned typedef_field_list_count;
c5aa993b 792 };
c906108c 793
10b3939b
DJ
794/* One item on the queue of compilation units to read in full symbols
795 for. */
796struct dwarf2_queue_item
797{
798 struct dwarf2_per_cu_data *per_cu;
799 struct dwarf2_queue_item *next;
800};
801
802/* The current queue. */
803static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
804
ae038cb0
DJ
805/* Loaded secondary compilation units are kept in memory until they
806 have not been referenced for the processing of this many
807 compilation units. Set this to zero to disable caching. Cache
808 sizes of up to at least twenty will improve startup time for
809 typical inter-CU-reference binaries, at an obvious memory cost. */
810static int dwarf2_max_cache_age = 5;
920d2a44
AC
811static void
812show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
813 struct cmd_list_element *c, const char *value)
814{
3e43a32a
MS
815 fprintf_filtered (file, _("The upper bound on the age of cached "
816 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
817 value);
818}
819
ae038cb0 820
0963b4bd 821/* Various complaints about symbol reading that don't abort the process. */
c906108c 822
4d3c2250
KB
823static void
824dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 825{
4d3c2250 826 complaint (&symfile_complaints,
e2e0b3e5 827 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
828}
829
25e43795
DJ
830static void
831dwarf2_debug_line_missing_file_complaint (void)
832{
833 complaint (&symfile_complaints,
834 _(".debug_line section has line data without a file"));
835}
836
59205f5a
JB
837static void
838dwarf2_debug_line_missing_end_sequence_complaint (void)
839{
840 complaint (&symfile_complaints,
3e43a32a
MS
841 _(".debug_line section has line "
842 "program sequence without an end"));
59205f5a
JB
843}
844
4d3c2250
KB
845static void
846dwarf2_complex_location_expr_complaint (void)
2e276125 847{
e2e0b3e5 848 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
849}
850
4d3c2250
KB
851static void
852dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
853 int arg3)
2e276125 854{
4d3c2250 855 complaint (&symfile_complaints,
3e43a32a
MS
856 _("const value length mismatch for '%s', got %d, expected %d"),
857 arg1, arg2, arg3);
4d3c2250
KB
858}
859
860static void
861dwarf2_macros_too_long_complaint (void)
2e276125 862{
4d3c2250 863 complaint (&symfile_complaints,
e2e0b3e5 864 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
865}
866
867static void
868dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 869{
4d3c2250 870 complaint (&symfile_complaints,
3e43a32a
MS
871 _("macro debug info contains a "
872 "malformed macro definition:\n`%s'"),
4d3c2250
KB
873 arg1);
874}
875
876static void
877dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 878{
4d3c2250 879 complaint (&symfile_complaints,
3e43a32a
MS
880 _("invalid attribute class or form for '%s' in '%s'"),
881 arg1, arg2);
4d3c2250 882}
c906108c 883
c906108c
SS
884/* local function prototypes */
885
4efb68b1 886static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 887
aaa75496
JB
888static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
889 struct objfile *);
890
918dd910
JK
891static void dwarf2_find_base_address (struct die_info *die,
892 struct dwarf2_cu *cu);
893
c67a9c90 894static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 895
72bf9492
DJ
896static void scan_partial_symbols (struct partial_die_info *,
897 CORE_ADDR *, CORE_ADDR *,
5734ee8b 898 int, struct dwarf2_cu *);
c906108c 899
72bf9492
DJ
900static void add_partial_symbol (struct partial_die_info *,
901 struct dwarf2_cu *);
63d06c5c 902
72bf9492
DJ
903static void add_partial_namespace (struct partial_die_info *pdi,
904 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 905 int need_pc, struct dwarf2_cu *cu);
63d06c5c 906
5d7cb8df
JK
907static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
908 CORE_ADDR *highpc, int need_pc,
909 struct dwarf2_cu *cu);
910
72bf9492
DJ
911static void add_partial_enumeration (struct partial_die_info *enum_pdi,
912 struct dwarf2_cu *cu);
91c24f0a 913
bc30ff58
JB
914static void add_partial_subprogram (struct partial_die_info *pdi,
915 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 916 int need_pc, struct dwarf2_cu *cu);
bc30ff58 917
fe1b8b76 918static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
919 gdb_byte *buffer, gdb_byte *info_ptr,
920 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 921
a14ed312 922static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 923
a14ed312 924static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 925
e7c27a73 926static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 927
f3dd6933 928static void dwarf2_free_abbrev_table (void *);
c906108c 929
fe1b8b76 930static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 931 struct dwarf2_cu *);
72bf9492 932
57349743 933static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 934 struct dwarf2_cu *);
c906108c 935
93311388
DE
936static struct partial_die_info *load_partial_dies (bfd *,
937 gdb_byte *, gdb_byte *,
938 int, struct dwarf2_cu *);
72bf9492 939
fe1b8b76 940static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
941 struct abbrev_info *abbrev,
942 unsigned int, bfd *,
943 gdb_byte *, gdb_byte *,
944 struct dwarf2_cu *);
c906108c 945
c764a876 946static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 947 struct dwarf2_cu *);
72bf9492
DJ
948
949static void fixup_partial_die (struct partial_die_info *,
950 struct dwarf2_cu *);
951
fe1b8b76
JB
952static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
953 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 954
fe1b8b76
JB
955static gdb_byte *read_attribute_value (struct attribute *, unsigned,
956 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 957
fe1b8b76 958static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 959
fe1b8b76 960static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 961
fe1b8b76 962static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 963
fe1b8b76 964static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 965
93311388 966static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 967
fe1b8b76 968static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 969 unsigned int *);
c906108c 970
c764a876
DE
971static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
972
973static LONGEST read_checked_initial_length_and_offset
974 (bfd *, gdb_byte *, const struct comp_unit_head *,
975 unsigned int *, unsigned int *);
613e1657 976
fe1b8b76 977static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
978 unsigned int *);
979
980static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 981
fe1b8b76 982static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 983
9b1c24c8 984static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 985
fe1b8b76
JB
986static char *read_indirect_string (bfd *, gdb_byte *,
987 const struct comp_unit_head *,
988 unsigned int *);
4bdf3d34 989
fe1b8b76 990static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 991
fe1b8b76 992static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 993
fe1b8b76 994static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 995
e142c38c 996static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 997
e142c38c
DJ
998static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
999 struct dwarf2_cu *);
c906108c 1000
348e048f
DE
1001static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1002 unsigned int,
1003 struct dwarf2_cu *);
1004
05cf31d1
JB
1005static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1006 struct dwarf2_cu *cu);
1007
e142c38c 1008static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1009
e142c38c 1010static struct die_info *die_specification (struct die_info *die,
f2f0e013 1011 struct dwarf2_cu **);
63d06c5c 1012
debd256d
JB
1013static void free_line_header (struct line_header *lh);
1014
aaa75496
JB
1015static void add_file_name (struct line_header *, char *, unsigned int,
1016 unsigned int, unsigned int);
1017
debd256d
JB
1018static struct line_header *(dwarf_decode_line_header
1019 (unsigned int offset,
e7c27a73 1020 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1021
72b9f47f 1022static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1023 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1024
72b9f47f 1025static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1026
a14ed312 1027static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1028 struct dwarf2_cu *);
c906108c 1029
34eaf542
TT
1030static struct symbol *new_symbol_full (struct die_info *, struct type *,
1031 struct dwarf2_cu *, struct symbol *);
1032
a14ed312 1033static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1034 struct dwarf2_cu *);
c906108c 1035
98bfdba5
PA
1036static void dwarf2_const_value_attr (struct attribute *attr,
1037 struct type *type,
1038 const char *name,
1039 struct obstack *obstack,
1040 struct dwarf2_cu *cu, long *value,
1041 gdb_byte **bytes,
1042 struct dwarf2_locexpr_baton **baton);
2df3850c 1043
e7c27a73 1044static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1045
b4ba55a1
JB
1046static int need_gnat_info (struct dwarf2_cu *);
1047
3e43a32a
MS
1048static struct type *die_descriptive_type (struct die_info *,
1049 struct dwarf2_cu *);
b4ba55a1
JB
1050
1051static void set_descriptive_type (struct type *, struct die_info *,
1052 struct dwarf2_cu *);
1053
e7c27a73
DJ
1054static struct type *die_containing_type (struct die_info *,
1055 struct dwarf2_cu *);
c906108c 1056
673bfd45
DE
1057static struct type *lookup_die_type (struct die_info *, struct attribute *,
1058 struct dwarf2_cu *);
c906108c 1059
f792889a 1060static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1061
673bfd45
DE
1062static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1063
086ed43d 1064static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1065
6e70227d 1066static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1067 const char *suffix, int physname,
1068 struct dwarf2_cu *cu);
63d06c5c 1069
e7c27a73 1070static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1071
348e048f
DE
1072static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1073
e7c27a73 1074static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1075
e7c27a73 1076static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1077
ff013f42
JK
1078static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1079 struct dwarf2_cu *, struct partial_symtab *);
1080
a14ed312 1081static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1082 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1083 struct partial_symtab *);
c906108c 1084
fae299cd
DC
1085static void get_scope_pc_bounds (struct die_info *,
1086 CORE_ADDR *, CORE_ADDR *,
1087 struct dwarf2_cu *);
1088
801e3a5b
JB
1089static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1090 CORE_ADDR, struct dwarf2_cu *);
1091
a14ed312 1092static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1093 struct dwarf2_cu *);
c906108c 1094
a14ed312 1095static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1096 struct type *, struct dwarf2_cu *);
c906108c 1097
a14ed312 1098static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1099 struct die_info *, struct type *,
e7c27a73 1100 struct dwarf2_cu *);
c906108c 1101
a14ed312 1102static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1103 struct type *,
1104 struct dwarf2_cu *);
c906108c 1105
134d01f1 1106static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1107
e7c27a73 1108static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1109
e7c27a73 1110static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1111
5d7cb8df
JK
1112static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1113
27aa8d6a
SW
1114static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1115
f55ee35c
JK
1116static struct type *read_module_type (struct die_info *die,
1117 struct dwarf2_cu *cu);
1118
38d518c9 1119static const char *namespace_name (struct die_info *die,
e142c38c 1120 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1121
134d01f1 1122static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1123
e7c27a73 1124static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1125
6e70227d 1126static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1127 struct dwarf2_cu *);
1128
93311388 1129static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1130
93311388
DE
1131static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1132 gdb_byte *info_ptr,
d97bc12b
DE
1133 gdb_byte **new_info_ptr,
1134 struct die_info *parent);
1135
93311388
DE
1136static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1137 gdb_byte *info_ptr,
fe1b8b76 1138 gdb_byte **new_info_ptr,
639d11d3
DC
1139 struct die_info *parent);
1140
93311388
DE
1141static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1142 gdb_byte *info_ptr,
fe1b8b76 1143 gdb_byte **new_info_ptr,
639d11d3
DC
1144 struct die_info *parent);
1145
93311388
DE
1146static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1147 struct die_info **, gdb_byte *,
1148 int *);
1149
e7c27a73 1150static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1151
71c25dea
TT
1152static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1153 struct obstack *);
1154
e142c38c 1155static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1156
98bfdba5
PA
1157static const char *dwarf2_full_name (char *name,
1158 struct die_info *die,
1159 struct dwarf2_cu *cu);
1160
e142c38c 1161static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1162 struct dwarf2_cu **);
9219021c 1163
a14ed312 1164static char *dwarf_tag_name (unsigned int);
c906108c 1165
a14ed312 1166static char *dwarf_attr_name (unsigned int);
c906108c 1167
a14ed312 1168static char *dwarf_form_name (unsigned int);
c906108c 1169
a14ed312 1170static char *dwarf_bool_name (unsigned int);
c906108c 1171
a14ed312 1172static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1173
1174#if 0
a14ed312 1175static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1176#endif
1177
f9aca02d 1178static struct die_info *sibling_die (struct die_info *);
c906108c 1179
d97bc12b
DE
1180static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1181
1182static void dump_die_for_error (struct die_info *);
1183
1184static void dump_die_1 (struct ui_file *, int level, int max_level,
1185 struct die_info *);
c906108c 1186
d97bc12b 1187/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1188
51545339 1189static void store_in_ref_table (struct die_info *,
10b3939b 1190 struct dwarf2_cu *);
c906108c 1191
93311388
DE
1192static int is_ref_attr (struct attribute *);
1193
c764a876 1194static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1195
43bbcdc2 1196static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1197
348e048f
DE
1198static struct die_info *follow_die_ref_or_sig (struct die_info *,
1199 struct attribute *,
1200 struct dwarf2_cu **);
1201
10b3939b
DJ
1202static struct die_info *follow_die_ref (struct die_info *,
1203 struct attribute *,
f2f0e013 1204 struct dwarf2_cu **);
c906108c 1205
348e048f
DE
1206static struct die_info *follow_die_sig (struct die_info *,
1207 struct attribute *,
1208 struct dwarf2_cu **);
1209
6c83ed52
TT
1210static struct signatured_type *lookup_signatured_type_at_offset
1211 (struct objfile *objfile,
1212 struct dwarf2_section_info *section,
1213 unsigned int offset);
1214
348e048f 1215static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1216 struct dwarf2_section_info *sect,
348e048f
DE
1217 unsigned int offset);
1218
1219static void read_signatured_type (struct objfile *,
1220 struct signatured_type *type_sig);
1221
c906108c
SS
1222/* memory allocation interface */
1223
7b5a2f43 1224static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1225
f3dd6933 1226static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1227
b60c80d6 1228static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1229
e142c38c 1230static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1231
e142c38c
DJ
1232static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1233 struct dwarf2_cu *);
5fb290d7 1234
2e276125 1235static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1236 char *, bfd *, struct dwarf2_cu *);
2e276125 1237
8e19ed76
PS
1238static int attr_form_is_block (struct attribute *);
1239
3690dd37
JB
1240static int attr_form_is_section_offset (struct attribute *);
1241
1242static int attr_form_is_constant (struct attribute *);
1243
8cf6f0b1
TT
1244static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1245 struct dwarf2_loclist_baton *baton,
1246 struct attribute *attr);
1247
93e7bd98
DJ
1248static void dwarf2_symbol_mark_computed (struct attribute *attr,
1249 struct symbol *sym,
1250 struct dwarf2_cu *cu);
4c2df51b 1251
93311388
DE
1252static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1253 struct abbrev_info *abbrev,
1254 struct dwarf2_cu *cu);
4bb7a0a7 1255
72bf9492
DJ
1256static void free_stack_comp_unit (void *);
1257
72bf9492
DJ
1258static hashval_t partial_die_hash (const void *item);
1259
1260static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1261
ae038cb0 1262static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1263 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1264
1265static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1266 (unsigned int offset, struct objfile *objfile);
ae038cb0 1267
9816fde3
JK
1268static void init_one_comp_unit (struct dwarf2_cu *cu,
1269 struct objfile *objfile);
1270
1271static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1272 struct die_info *comp_unit_die);
93311388 1273
ae038cb0
DJ
1274static void free_one_comp_unit (void *);
1275
1276static void free_cached_comp_units (void *);
1277
1278static void age_cached_comp_units (void);
1279
1280static void free_one_cached_comp_unit (void *);
1281
f792889a
DJ
1282static struct type *set_die_type (struct die_info *, struct type *,
1283 struct dwarf2_cu *);
1c379e20 1284
ae038cb0
DJ
1285static void create_all_comp_units (struct objfile *);
1286
1fd400ff
TT
1287static int create_debug_types_hash_table (struct objfile *objfile);
1288
93311388
DE
1289static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1290 struct objfile *);
10b3939b
DJ
1291
1292static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1293
1294static void dwarf2_add_dependence (struct dwarf2_cu *,
1295 struct dwarf2_per_cu_data *);
1296
ae038cb0
DJ
1297static void dwarf2_mark (struct dwarf2_cu *);
1298
1299static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1300
673bfd45
DE
1301static struct type *get_die_type_at_offset (unsigned int,
1302 struct dwarf2_per_cu_data *per_cu);
1303
f792889a 1304static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1305
9291a0cd
TT
1306static void dwarf2_release_queue (void *dummy);
1307
1308static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1309 struct objfile *objfile);
1310
1311static void process_queue (struct objfile *objfile);
1312
1313static void find_file_and_directory (struct die_info *die,
1314 struct dwarf2_cu *cu,
1315 char **name, char **comp_dir);
1316
1317static char *file_full_name (int file, struct line_header *lh,
1318 const char *comp_dir);
1319
1320static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1321 gdb_byte *info_ptr,
1322 gdb_byte *buffer,
1323 unsigned int buffer_size,
1324 bfd *abfd);
1325
1326static void init_cu_die_reader (struct die_reader_specs *reader,
1327 struct dwarf2_cu *cu);
1328
673bfd45 1329static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1330
9291a0cd
TT
1331#if WORDS_BIGENDIAN
1332
1333/* Convert VALUE between big- and little-endian. */
1334static offset_type
1335byte_swap (offset_type value)
1336{
1337 offset_type result;
1338
1339 result = (value & 0xff) << 24;
1340 result |= (value & 0xff00) << 8;
1341 result |= (value & 0xff0000) >> 8;
1342 result |= (value & 0xff000000) >> 24;
1343 return result;
1344}
1345
1346#define MAYBE_SWAP(V) byte_swap (V)
1347
1348#else
1349#define MAYBE_SWAP(V) (V)
1350#endif /* WORDS_BIGENDIAN */
1351
1352/* The suffix for an index file. */
1353#define INDEX_SUFFIX ".gdb-index"
1354
3da10d80
KS
1355static const char *dwarf2_physname (char *name, struct die_info *die,
1356 struct dwarf2_cu *cu);
1357
c906108c 1358/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1359 information and return true if we have enough to do something.
1360 NAMES points to the dwarf2 section names, or is NULL if the standard
1361 ELF names are used. */
c906108c
SS
1362
1363int
251d32d9
TG
1364dwarf2_has_info (struct objfile *objfile,
1365 const struct dwarf2_debug_sections *names)
c906108c 1366{
be391dca
TT
1367 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1368 if (!dwarf2_per_objfile)
1369 {
1370 /* Initialize per-objfile state. */
1371 struct dwarf2_per_objfile *data
1372 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1373
be391dca
TT
1374 memset (data, 0, sizeof (*data));
1375 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1376 dwarf2_per_objfile = data;
6502dd73 1377
251d32d9
TG
1378 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1379 (void *) names);
be391dca
TT
1380 dwarf2_per_objfile->objfile = objfile;
1381 }
1382 return (dwarf2_per_objfile->info.asection != NULL
1383 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1384}
1385
251d32d9
TG
1386/* When loading sections, we look either for uncompressed section or for
1387 compressed section names. */
233a11ab
CS
1388
1389static int
251d32d9
TG
1390section_is_p (const char *section_name,
1391 const struct dwarf2_section_names *names)
233a11ab 1392{
251d32d9
TG
1393 if (names->normal != NULL
1394 && strcmp (section_name, names->normal) == 0)
1395 return 1;
1396 if (names->compressed != NULL
1397 && strcmp (section_name, names->compressed) == 0)
1398 return 1;
1399 return 0;
233a11ab
CS
1400}
1401
c906108c
SS
1402/* This function is mapped across the sections and remembers the
1403 offset and size of each of the debugging sections we are interested
1404 in. */
1405
1406static void
251d32d9 1407dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1408{
251d32d9
TG
1409 const struct dwarf2_debug_sections *names;
1410
1411 if (vnames == NULL)
1412 names = &dwarf2_elf_names;
1413 else
1414 names = (const struct dwarf2_debug_sections *) vnames;
1415
1416 if (section_is_p (sectp->name, &names->info))
c906108c 1417 {
dce234bc
PP
1418 dwarf2_per_objfile->info.asection = sectp;
1419 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1420 }
251d32d9 1421 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1422 {
dce234bc
PP
1423 dwarf2_per_objfile->abbrev.asection = sectp;
1424 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1425 }
251d32d9 1426 else if (section_is_p (sectp->name, &names->line))
c906108c 1427 {
dce234bc
PP
1428 dwarf2_per_objfile->line.asection = sectp;
1429 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1430 }
251d32d9 1431 else if (section_is_p (sectp->name, &names->loc))
c906108c 1432 {
dce234bc
PP
1433 dwarf2_per_objfile->loc.asection = sectp;
1434 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1435 }
251d32d9 1436 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1437 {
dce234bc
PP
1438 dwarf2_per_objfile->macinfo.asection = sectp;
1439 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1440 }
251d32d9 1441 else if (section_is_p (sectp->name, &names->str))
c906108c 1442 {
dce234bc
PP
1443 dwarf2_per_objfile->str.asection = sectp;
1444 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1445 }
251d32d9 1446 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1447 {
dce234bc
PP
1448 dwarf2_per_objfile->frame.asection = sectp;
1449 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1450 }
251d32d9 1451 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1452 {
3799ccc6 1453 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1454
3799ccc6
EZ
1455 if (aflag & SEC_HAS_CONTENTS)
1456 {
dce234bc
PP
1457 dwarf2_per_objfile->eh_frame.asection = sectp;
1458 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1459 }
b6af0555 1460 }
251d32d9 1461 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1462 {
dce234bc
PP
1463 dwarf2_per_objfile->ranges.asection = sectp;
1464 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1465 }
251d32d9 1466 else if (section_is_p (sectp->name, &names->types))
348e048f 1467 {
8b70b953
TT
1468 struct dwarf2_section_info type_section;
1469
1470 memset (&type_section, 0, sizeof (type_section));
1471 type_section.asection = sectp;
1472 type_section.size = bfd_get_section_size (sectp);
1473
1474 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1475 &type_section);
348e048f 1476 }
251d32d9 1477 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1478 {
1479 dwarf2_per_objfile->gdb_index.asection = sectp;
1480 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1481 }
dce234bc 1482
72dca2f5
FR
1483 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1484 && bfd_section_vma (abfd, sectp) == 0)
1485 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1486}
1487
dce234bc
PP
1488/* Decompress a section that was compressed using zlib. Store the
1489 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1490
1491static void
dce234bc
PP
1492zlib_decompress_section (struct objfile *objfile, asection *sectp,
1493 gdb_byte **outbuf, bfd_size_type *outsize)
1494{
1495 bfd *abfd = objfile->obfd;
1496#ifndef HAVE_ZLIB_H
1497 error (_("Support for zlib-compressed DWARF data (from '%s') "
1498 "is disabled in this copy of GDB"),
1499 bfd_get_filename (abfd));
1500#else
1501 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1502 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1503 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1504 bfd_size_type uncompressed_size;
1505 gdb_byte *uncompressed_buffer;
1506 z_stream strm;
1507 int rc;
1508 int header_size = 12;
1509
1510 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1511 || bfd_bread (compressed_buffer,
1512 compressed_size, abfd) != compressed_size)
dce234bc
PP
1513 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1514 bfd_get_filename (abfd));
1515
1516 /* Read the zlib header. In this case, it should be "ZLIB" followed
1517 by the uncompressed section size, 8 bytes in big-endian order. */
1518 if (compressed_size < header_size
1519 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1520 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1521 bfd_get_filename (abfd));
1522 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1523 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1524 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1525 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1526 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1527 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1528 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1529 uncompressed_size += compressed_buffer[11];
1530
1531 /* It is possible the section consists of several compressed
1532 buffers concatenated together, so we uncompress in a loop. */
1533 strm.zalloc = NULL;
1534 strm.zfree = NULL;
1535 strm.opaque = NULL;
1536 strm.avail_in = compressed_size - header_size;
1537 strm.next_in = (Bytef*) compressed_buffer + header_size;
1538 strm.avail_out = uncompressed_size;
1539 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1540 uncompressed_size);
1541 rc = inflateInit (&strm);
1542 while (strm.avail_in > 0)
1543 {
1544 if (rc != Z_OK)
1545 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1546 bfd_get_filename (abfd), rc);
1547 strm.next_out = ((Bytef*) uncompressed_buffer
1548 + (uncompressed_size - strm.avail_out));
1549 rc = inflate (&strm, Z_FINISH);
1550 if (rc != Z_STREAM_END)
1551 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1552 bfd_get_filename (abfd), rc);
1553 rc = inflateReset (&strm);
1554 }
1555 rc = inflateEnd (&strm);
1556 if (rc != Z_OK
1557 || strm.avail_out != 0)
1558 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1559 bfd_get_filename (abfd), rc);
1560
affddf13 1561 do_cleanups (cleanup);
dce234bc
PP
1562 *outbuf = uncompressed_buffer;
1563 *outsize = uncompressed_size;
1564#endif
233a11ab
CS
1565}
1566
9e0ac564
TT
1567/* A helper function that decides whether a section is empty. */
1568
1569static int
1570dwarf2_section_empty_p (struct dwarf2_section_info *info)
1571{
1572 return info->asection == NULL || info->size == 0;
1573}
1574
dce234bc
PP
1575/* Read the contents of the section SECTP from object file specified by
1576 OBJFILE, store info about the section into INFO.
1577 If the section is compressed, uncompress it before returning. */
c906108c 1578
dce234bc
PP
1579static void
1580dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1581{
dce234bc
PP
1582 bfd *abfd = objfile->obfd;
1583 asection *sectp = info->asection;
1584 gdb_byte *buf, *retbuf;
1585 unsigned char header[4];
c906108c 1586
be391dca
TT
1587 if (info->readin)
1588 return;
dce234bc 1589 info->buffer = NULL;
b315ab21 1590 info->map_addr = NULL;
be391dca 1591 info->readin = 1;
188dd5d6 1592
9e0ac564 1593 if (dwarf2_section_empty_p (info))
dce234bc 1594 return;
c906108c 1595
dce234bc
PP
1596 /* Check if the file has a 4-byte header indicating compression. */
1597 if (info->size > sizeof (header)
1598 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1599 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1600 {
1601 /* Upon decompression, update the buffer and its size. */
1602 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1603 {
1604 zlib_decompress_section (objfile, sectp, &info->buffer,
1605 &info->size);
1606 return;
1607 }
1608 }
4bdf3d34 1609
dce234bc
PP
1610#ifdef HAVE_MMAP
1611 if (pagesize == 0)
1612 pagesize = getpagesize ();
2e276125 1613
dce234bc
PP
1614 /* Only try to mmap sections which are large enough: we don't want to
1615 waste space due to fragmentation. Also, only try mmap for sections
1616 without relocations. */
1617
1618 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1619 {
b315ab21
TG
1620 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1621 MAP_PRIVATE, sectp->filepos,
1622 &info->map_addr, &info->map_len);
dce234bc 1623
b315ab21 1624 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1625 {
be391dca 1626#if HAVE_POSIX_MADVISE
b315ab21 1627 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1628#endif
dce234bc
PP
1629 return;
1630 }
1631 }
1632#endif
1633
1634 /* If we get here, we are a normal, not-compressed section. */
1635 info->buffer = buf
1636 = obstack_alloc (&objfile->objfile_obstack, info->size);
1637
1638 /* When debugging .o files, we may need to apply relocations; see
1639 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1640 We never compress sections in .o files, so we only need to
1641 try this when the section is not compressed. */
ac8035ab 1642 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1643 if (retbuf != NULL)
1644 {
1645 info->buffer = retbuf;
1646 return;
1647 }
1648
1649 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1650 || bfd_bread (buf, info->size, abfd) != info->size)
1651 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1652 bfd_get_filename (abfd));
1653}
1654
9e0ac564
TT
1655/* A helper function that returns the size of a section in a safe way.
1656 If you are positive that the section has been read before using the
1657 size, then it is safe to refer to the dwarf2_section_info object's
1658 "size" field directly. In other cases, you must call this
1659 function, because for compressed sections the size field is not set
1660 correctly until the section has been read. */
1661
1662static bfd_size_type
1663dwarf2_section_size (struct objfile *objfile,
1664 struct dwarf2_section_info *info)
1665{
1666 if (!info->readin)
1667 dwarf2_read_section (objfile, info);
1668 return info->size;
1669}
1670
dce234bc 1671/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1672 SECTION_NAME. */
af34e669 1673
dce234bc 1674void
3017a003
TG
1675dwarf2_get_section_info (struct objfile *objfile,
1676 enum dwarf2_section_enum sect,
dce234bc
PP
1677 asection **sectp, gdb_byte **bufp,
1678 bfd_size_type *sizep)
1679{
1680 struct dwarf2_per_objfile *data
1681 = objfile_data (objfile, dwarf2_objfile_data_key);
1682 struct dwarf2_section_info *info;
a3b2a86b
TT
1683
1684 /* We may see an objfile without any DWARF, in which case we just
1685 return nothing. */
1686 if (data == NULL)
1687 {
1688 *sectp = NULL;
1689 *bufp = NULL;
1690 *sizep = 0;
1691 return;
1692 }
3017a003
TG
1693 switch (sect)
1694 {
1695 case DWARF2_DEBUG_FRAME:
1696 info = &data->frame;
1697 break;
1698 case DWARF2_EH_FRAME:
1699 info = &data->eh_frame;
1700 break;
1701 default:
1702 gdb_assert_not_reached ("unexpected section");
1703 }
dce234bc 1704
9e0ac564 1705 dwarf2_read_section (objfile, info);
dce234bc
PP
1706
1707 *sectp = info->asection;
1708 *bufp = info->buffer;
1709 *sizep = info->size;
1710}
1711
9291a0cd 1712\f
7b9f3c50
DE
1713/* DWARF quick_symbols_functions support. */
1714
1715/* TUs can share .debug_line entries, and there can be a lot more TUs than
1716 unique line tables, so we maintain a separate table of all .debug_line
1717 derived entries to support the sharing.
1718 All the quick functions need is the list of file names. We discard the
1719 line_header when we're done and don't need to record it here. */
1720struct quick_file_names
1721{
1722 /* The offset in .debug_line of the line table. We hash on this. */
1723 unsigned int offset;
1724
1725 /* The number of entries in file_names, real_names. */
1726 unsigned int num_file_names;
1727
1728 /* The file names from the line table, after being run through
1729 file_full_name. */
1730 const char **file_names;
1731
1732 /* The file names from the line table after being run through
1733 gdb_realpath. These are computed lazily. */
1734 const char **real_names;
1735};
1736
1737/* When using the index (and thus not using psymtabs), each CU has an
1738 object of this type. This is used to hold information needed by
1739 the various "quick" methods. */
1740struct dwarf2_per_cu_quick_data
1741{
1742 /* The file table. This can be NULL if there was no file table
1743 or it's currently not read in.
1744 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1745 struct quick_file_names *file_names;
1746
1747 /* The corresponding symbol table. This is NULL if symbols for this
1748 CU have not yet been read. */
1749 struct symtab *symtab;
1750
1751 /* A temporary mark bit used when iterating over all CUs in
1752 expand_symtabs_matching. */
1753 unsigned int mark : 1;
1754
1755 /* True if we've tried to read the file table and found there isn't one.
1756 There will be no point in trying to read it again next time. */
1757 unsigned int no_file_data : 1;
1758};
1759
1760/* Hash function for a quick_file_names. */
1761
1762static hashval_t
1763hash_file_name_entry (const void *e)
1764{
1765 const struct quick_file_names *file_data = e;
1766
1767 return file_data->offset;
1768}
1769
1770/* Equality function for a quick_file_names. */
1771
1772static int
1773eq_file_name_entry (const void *a, const void *b)
1774{
1775 const struct quick_file_names *ea = a;
1776 const struct quick_file_names *eb = b;
1777
1778 return ea->offset == eb->offset;
1779}
1780
1781/* Delete function for a quick_file_names. */
1782
1783static void
1784delete_file_name_entry (void *e)
1785{
1786 struct quick_file_names *file_data = e;
1787 int i;
1788
1789 for (i = 0; i < file_data->num_file_names; ++i)
1790 {
1791 xfree ((void*) file_data->file_names[i]);
1792 if (file_data->real_names)
1793 xfree ((void*) file_data->real_names[i]);
1794 }
1795
1796 /* The space for the struct itself lives on objfile_obstack,
1797 so we don't free it here. */
1798}
1799
1800/* Create a quick_file_names hash table. */
1801
1802static htab_t
1803create_quick_file_names_table (unsigned int nr_initial_entries)
1804{
1805 return htab_create_alloc (nr_initial_entries,
1806 hash_file_name_entry, eq_file_name_entry,
1807 delete_file_name_entry, xcalloc, xfree);
1808}
9291a0cd 1809
918dd910
JK
1810/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1811 have to be created afterwards. You should call age_cached_comp_units after
1812 processing PER_CU->CU. dw2_setup must have been already called. */
1813
1814static void
1815load_cu (struct dwarf2_per_cu_data *per_cu)
1816{
8b70b953
TT
1817 if (per_cu->debug_type_section)
1818 read_signatured_type_at_offset (per_cu->objfile,
1819 per_cu->debug_type_section,
1820 per_cu->offset);
918dd910
JK
1821 else
1822 load_full_comp_unit (per_cu, per_cu->objfile);
1823
1824 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1825
1826 gdb_assert (per_cu->cu != NULL);
1827}
1828
9291a0cd
TT
1829/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1830 this CU came. */
2fdf6df6 1831
9291a0cd
TT
1832static void
1833dw2_do_instantiate_symtab (struct objfile *objfile,
1834 struct dwarf2_per_cu_data *per_cu)
1835{
1836 struct cleanup *back_to;
1837
1838 back_to = make_cleanup (dwarf2_release_queue, NULL);
1839
1840 queue_comp_unit (per_cu, objfile);
1841
918dd910 1842 load_cu (per_cu);
9291a0cd
TT
1843
1844 process_queue (objfile);
1845
1846 /* Age the cache, releasing compilation units that have not
1847 been used recently. */
1848 age_cached_comp_units ();
1849
1850 do_cleanups (back_to);
1851}
1852
1853/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1854 the objfile from which this CU came. Returns the resulting symbol
1855 table. */
2fdf6df6 1856
9291a0cd
TT
1857static struct symtab *
1858dw2_instantiate_symtab (struct objfile *objfile,
1859 struct dwarf2_per_cu_data *per_cu)
1860{
1861 if (!per_cu->v.quick->symtab)
1862 {
1863 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1864 increment_reading_symtab ();
1865 dw2_do_instantiate_symtab (objfile, per_cu);
1866 do_cleanups (back_to);
1867 }
1868 return per_cu->v.quick->symtab;
1869}
1870
1fd400ff 1871/* Return the CU given its index. */
2fdf6df6 1872
1fd400ff
TT
1873static struct dwarf2_per_cu_data *
1874dw2_get_cu (int index)
1875{
1876 if (index >= dwarf2_per_objfile->n_comp_units)
1877 {
1878 index -= dwarf2_per_objfile->n_comp_units;
1879 return dwarf2_per_objfile->type_comp_units[index];
1880 }
1881 return dwarf2_per_objfile->all_comp_units[index];
1882}
1883
9291a0cd
TT
1884/* A helper function that knows how to read a 64-bit value in a way
1885 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1886 otherwise. */
2fdf6df6 1887
9291a0cd
TT
1888static int
1889extract_cu_value (const char *bytes, ULONGEST *result)
1890{
1891 if (sizeof (ULONGEST) < 8)
1892 {
1893 int i;
1894
1895 /* Ignore the upper 4 bytes if they are all zero. */
1896 for (i = 0; i < 4; ++i)
1897 if (bytes[i + 4] != 0)
1898 return 0;
1899
1900 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1901 }
1902 else
1903 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1904 return 1;
1905}
1906
1907/* Read the CU list from the mapped index, and use it to create all
1908 the CU objects for this objfile. Return 0 if something went wrong,
1909 1 if everything went ok. */
2fdf6df6 1910
9291a0cd 1911static int
1fd400ff
TT
1912create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1913 offset_type cu_list_elements)
9291a0cd
TT
1914{
1915 offset_type i;
9291a0cd
TT
1916
1917 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1918 dwarf2_per_objfile->all_comp_units
1919 = obstack_alloc (&objfile->objfile_obstack,
1920 dwarf2_per_objfile->n_comp_units
1921 * sizeof (struct dwarf2_per_cu_data *));
1922
1923 for (i = 0; i < cu_list_elements; i += 2)
1924 {
1925 struct dwarf2_per_cu_data *the_cu;
1926 ULONGEST offset, length;
1927
1928 if (!extract_cu_value (cu_list, &offset)
1929 || !extract_cu_value (cu_list + 8, &length))
1930 return 0;
1931 cu_list += 2 * 8;
1932
1933 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1934 struct dwarf2_per_cu_data);
1935 the_cu->offset = offset;
1936 the_cu->length = length;
1937 the_cu->objfile = objfile;
1938 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939 struct dwarf2_per_cu_quick_data);
1940 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1941 }
1942
1943 return 1;
1944}
1945
1fd400ff 1946/* Create the signatured type hash table from the index. */
673bfd45 1947
1fd400ff 1948static int
673bfd45 1949create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1950 struct dwarf2_section_info *section,
673bfd45
DE
1951 const gdb_byte *bytes,
1952 offset_type elements)
1fd400ff
TT
1953{
1954 offset_type i;
673bfd45 1955 htab_t sig_types_hash;
1fd400ff
TT
1956
1957 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1958 dwarf2_per_objfile->type_comp_units
1959 = obstack_alloc (&objfile->objfile_obstack,
1960 dwarf2_per_objfile->n_type_comp_units
1961 * sizeof (struct dwarf2_per_cu_data *));
1962
673bfd45 1963 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1964
1965 for (i = 0; i < elements; i += 3)
1966 {
1967 struct signatured_type *type_sig;
1968 ULONGEST offset, type_offset, signature;
1969 void **slot;
1970
1971 if (!extract_cu_value (bytes, &offset)
1972 || !extract_cu_value (bytes + 8, &type_offset))
1973 return 0;
1974 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1975 bytes += 3 * 8;
1976
1977 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1978 struct signatured_type);
1979 type_sig->signature = signature;
1fd400ff 1980 type_sig->type_offset = type_offset;
8b70b953 1981 type_sig->per_cu.debug_type_section = section;
1fd400ff
TT
1982 type_sig->per_cu.offset = offset;
1983 type_sig->per_cu.objfile = objfile;
1984 type_sig->per_cu.v.quick
1985 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1986 struct dwarf2_per_cu_quick_data);
1987
673bfd45 1988 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1989 *slot = type_sig;
1990
1991 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1992 }
1993
673bfd45 1994 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1995
1996 return 1;
1997}
1998
9291a0cd
TT
1999/* Read the address map data from the mapped index, and use it to
2000 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2001
9291a0cd
TT
2002static void
2003create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2004{
2005 const gdb_byte *iter, *end;
2006 struct obstack temp_obstack;
2007 struct addrmap *mutable_map;
2008 struct cleanup *cleanup;
2009 CORE_ADDR baseaddr;
2010
2011 obstack_init (&temp_obstack);
2012 cleanup = make_cleanup_obstack_free (&temp_obstack);
2013 mutable_map = addrmap_create_mutable (&temp_obstack);
2014
2015 iter = index->address_table;
2016 end = iter + index->address_table_size;
2017
2018 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2019
2020 while (iter < end)
2021 {
2022 ULONGEST hi, lo, cu_index;
2023 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2024 iter += 8;
2025 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2026 iter += 8;
2027 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2028 iter += 4;
2029
2030 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2031 dw2_get_cu (cu_index));
9291a0cd
TT
2032 }
2033
2034 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2035 &objfile->objfile_obstack);
2036 do_cleanups (cleanup);
2037}
2038
59d7bcaf
JK
2039/* The hash function for strings in the mapped index. This is the same as
2040 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2041 implementation. This is necessary because the hash function is tied to the
2042 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2043 SYMBOL_HASH_NEXT.
2044
2045 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2046
9291a0cd 2047static hashval_t
559a7a62 2048mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2049{
2050 const unsigned char *str = (const unsigned char *) p;
2051 hashval_t r = 0;
2052 unsigned char c;
2053
2054 while ((c = *str++) != 0)
559a7a62
JK
2055 {
2056 if (index_version >= 5)
2057 c = tolower (c);
2058 r = r * 67 + c - 113;
2059 }
9291a0cd
TT
2060
2061 return r;
2062}
2063
2064/* Find a slot in the mapped index INDEX for the object named NAME.
2065 If NAME is found, set *VEC_OUT to point to the CU vector in the
2066 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2067
9291a0cd
TT
2068static int
2069find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2070 offset_type **vec_out)
2071{
0cf03b49
JK
2072 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2073 offset_type hash;
9291a0cd 2074 offset_type slot, step;
559a7a62 2075 int (*cmp) (const char *, const char *);
9291a0cd 2076
0cf03b49
JK
2077 if (current_language->la_language == language_cplus
2078 || current_language->la_language == language_java
2079 || current_language->la_language == language_fortran)
2080 {
2081 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2082 not contain any. */
2083 const char *paren = strchr (name, '(');
2084
2085 if (paren)
2086 {
2087 char *dup;
2088
2089 dup = xmalloc (paren - name + 1);
2090 memcpy (dup, name, paren - name);
2091 dup[paren - name] = 0;
2092
2093 make_cleanup (xfree, dup);
2094 name = dup;
2095 }
2096 }
2097
559a7a62
JK
2098 /* Index version 4 did not support case insensitive searches. But the
2099 indexes for case insensitive languages are built in lowercase, therefore
2100 simulate our NAME being searched is also lowercased. */
2101 hash = mapped_index_string_hash ((index->version == 4
2102 && case_sensitivity == case_sensitive_off
2103 ? 5 : index->version),
2104 name);
2105
3876f04e
DE
2106 slot = hash & (index->symbol_table_slots - 1);
2107 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2108 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2109
2110 for (;;)
2111 {
2112 /* Convert a slot number to an offset into the table. */
2113 offset_type i = 2 * slot;
2114 const char *str;
3876f04e 2115 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2116 {
2117 do_cleanups (back_to);
2118 return 0;
2119 }
9291a0cd 2120
3876f04e 2121 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2122 if (!cmp (name, str))
9291a0cd
TT
2123 {
2124 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2125 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2126 do_cleanups (back_to);
9291a0cd
TT
2127 return 1;
2128 }
2129
3876f04e 2130 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2131 }
2132}
2133
2134/* Read the index file. If everything went ok, initialize the "quick"
2135 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2136
9291a0cd
TT
2137static int
2138dwarf2_read_index (struct objfile *objfile)
2139{
9291a0cd
TT
2140 char *addr;
2141 struct mapped_index *map;
b3b272e1 2142 offset_type *metadata;
ac0b195c
KW
2143 const gdb_byte *cu_list;
2144 const gdb_byte *types_list = NULL;
2145 offset_type version, cu_list_elements;
2146 offset_type types_list_elements = 0;
1fd400ff 2147 int i;
9291a0cd 2148
9e0ac564 2149 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2150 return 0;
82430852
JK
2151
2152 /* Older elfutils strip versions could keep the section in the main
2153 executable while splitting it for the separate debug info file. */
2154 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2155 & SEC_HAS_CONTENTS) == 0)
2156 return 0;
2157
9291a0cd
TT
2158 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2159
2160 addr = dwarf2_per_objfile->gdb_index.buffer;
2161 /* Version check. */
1fd400ff 2162 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2163 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2164 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2165 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2166 indices. Index version 4 uses a different hash function than index
2167 version 5 and later. */
831adc1f 2168 if (version < 4)
9291a0cd 2169 return 0;
594e8718
JK
2170 /* Indexes with higher version than the one supported by GDB may be no
2171 longer backward compatible. */
559a7a62 2172 if (version > 5)
594e8718 2173 return 0;
9291a0cd
TT
2174
2175 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2176 map->version = version;
b3b272e1 2177 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2178
2179 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2180
2181 i = 0;
2182 cu_list = addr + MAYBE_SWAP (metadata[i]);
2183 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2184 / 8);
1fd400ff
TT
2185 ++i;
2186
987d643c
TT
2187 types_list = addr + MAYBE_SWAP (metadata[i]);
2188 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2189 - MAYBE_SWAP (metadata[i]))
2190 / 8);
2191 ++i;
1fd400ff
TT
2192
2193 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2194 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2195 - MAYBE_SWAP (metadata[i]));
2196 ++i;
2197
3876f04e
DE
2198 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2199 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2200 - MAYBE_SWAP (metadata[i]))
2201 / (2 * sizeof (offset_type)));
1fd400ff 2202 ++i;
9291a0cd 2203
1fd400ff
TT
2204 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2205
2206 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2207 return 0;
2208
8b70b953
TT
2209 if (types_list_elements)
2210 {
2211 struct dwarf2_section_info *section;
2212
2213 /* We can only handle a single .debug_types when we have an
2214 index. */
2215 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2216 return 0;
2217
2218 section = VEC_index (dwarf2_section_info_def,
2219 dwarf2_per_objfile->types, 0);
2220
2221 if (!create_signatured_type_table_from_index (objfile, section,
2222 types_list,
2223 types_list_elements))
2224 return 0;
2225 }
9291a0cd
TT
2226
2227 create_addrmap_from_index (objfile, map);
2228
2229 dwarf2_per_objfile->index_table = map;
2230 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2231 dwarf2_per_objfile->quick_file_names_table =
2232 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2233
2234 return 1;
2235}
2236
2237/* A helper for the "quick" functions which sets the global
2238 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2239
9291a0cd
TT
2240static void
2241dw2_setup (struct objfile *objfile)
2242{
2243 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2244 gdb_assert (dwarf2_per_objfile);
2245}
2246
2247/* A helper for the "quick" functions which attempts to read the line
2248 table for THIS_CU. */
2fdf6df6 2249
7b9f3c50
DE
2250static struct quick_file_names *
2251dw2_get_file_names (struct objfile *objfile,
2252 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2253{
2254 bfd *abfd = objfile->obfd;
7b9f3c50 2255 struct line_header *lh;
9291a0cd
TT
2256 struct attribute *attr;
2257 struct cleanup *cleanups;
2258 struct die_info *comp_unit_die;
36374493 2259 struct dwarf2_section_info* sec;
9291a0cd
TT
2260 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2261 int has_children, i;
2262 struct dwarf2_cu cu;
2263 unsigned int bytes_read, buffer_size;
2264 struct die_reader_specs reader_specs;
2265 char *name, *comp_dir;
7b9f3c50
DE
2266 void **slot;
2267 struct quick_file_names *qfn;
2268 unsigned int line_offset;
9291a0cd 2269
7b9f3c50
DE
2270 if (this_cu->v.quick->file_names != NULL)
2271 return this_cu->v.quick->file_names;
2272 /* If we know there is no line data, no point in looking again. */
2273 if (this_cu->v.quick->no_file_data)
2274 return NULL;
9291a0cd 2275
9816fde3 2276 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2277 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2278
8b70b953
TT
2279 if (this_cu->debug_type_section)
2280 sec = this_cu->debug_type_section;
36374493
DE
2281 else
2282 sec = &dwarf2_per_objfile->info;
2283 dwarf2_read_section (objfile, sec);
2284 buffer_size = sec->size;
2285 buffer = sec->buffer;
9291a0cd
TT
2286 info_ptr = buffer + this_cu->offset;
2287 beg_of_comp_unit = info_ptr;
2288
2289 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2290 buffer, buffer_size,
2291 abfd);
2292
2293 /* Complete the cu_header. */
2294 cu.header.offset = beg_of_comp_unit - buffer;
2295 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2296
2297 this_cu->cu = &cu;
2298 cu.per_cu = this_cu;
2299
2300 dwarf2_read_abbrevs (abfd, &cu);
2301 make_cleanup (dwarf2_free_abbrev_table, &cu);
2302
8b70b953 2303 if (this_cu->debug_type_section)
9291a0cd
TT
2304 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2305 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2306 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2307 &has_children);
9291a0cd 2308
7b9f3c50
DE
2309 lh = NULL;
2310 slot = NULL;
2311 line_offset = 0;
9291a0cd
TT
2312 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2313 if (attr)
2314 {
7b9f3c50
DE
2315 struct quick_file_names find_entry;
2316
2317 line_offset = DW_UNSND (attr);
2318
2319 /* We may have already read in this line header (TU line header sharing).
2320 If we have we're done. */
2321 find_entry.offset = line_offset;
2322 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2323 &find_entry, INSERT);
2324 if (*slot != NULL)
2325 {
2326 do_cleanups (cleanups);
2327 this_cu->v.quick->file_names = *slot;
2328 return *slot;
2329 }
2330
9291a0cd
TT
2331 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2332 }
2333 if (lh == NULL)
2334 {
2335 do_cleanups (cleanups);
7b9f3c50
DE
2336 this_cu->v.quick->no_file_data = 1;
2337 return NULL;
9291a0cd
TT
2338 }
2339
7b9f3c50
DE
2340 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2341 qfn->offset = line_offset;
2342 gdb_assert (slot != NULL);
2343 *slot = qfn;
9291a0cd 2344
7b9f3c50 2345 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2346
7b9f3c50
DE
2347 qfn->num_file_names = lh->num_file_names;
2348 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2349 lh->num_file_names * sizeof (char *));
9291a0cd 2350 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2351 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2352 qfn->real_names = NULL;
9291a0cd 2353
7b9f3c50 2354 free_line_header (lh);
9291a0cd 2355 do_cleanups (cleanups);
7b9f3c50
DE
2356
2357 this_cu->v.quick->file_names = qfn;
2358 return qfn;
9291a0cd
TT
2359}
2360
2361/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2362 real path for a given file name from the line table. */
2fdf6df6 2363
9291a0cd 2364static const char *
7b9f3c50
DE
2365dw2_get_real_path (struct objfile *objfile,
2366 struct quick_file_names *qfn, int index)
9291a0cd 2367{
7b9f3c50
DE
2368 if (qfn->real_names == NULL)
2369 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2370 qfn->num_file_names, sizeof (char *));
9291a0cd 2371
7b9f3c50
DE
2372 if (qfn->real_names[index] == NULL)
2373 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2374
7b9f3c50 2375 return qfn->real_names[index];
9291a0cd
TT
2376}
2377
2378static struct symtab *
2379dw2_find_last_source_symtab (struct objfile *objfile)
2380{
2381 int index;
ae2de4f8 2382
9291a0cd
TT
2383 dw2_setup (objfile);
2384 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2385 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2386}
2387
7b9f3c50
DE
2388/* Traversal function for dw2_forget_cached_source_info. */
2389
2390static int
2391dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2392{
7b9f3c50 2393 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2394
7b9f3c50 2395 if (file_data->real_names)
9291a0cd 2396 {
7b9f3c50 2397 int i;
9291a0cd 2398
7b9f3c50 2399 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2400 {
7b9f3c50
DE
2401 xfree ((void*) file_data->real_names[i]);
2402 file_data->real_names[i] = NULL;
9291a0cd
TT
2403 }
2404 }
7b9f3c50
DE
2405
2406 return 1;
2407}
2408
2409static void
2410dw2_forget_cached_source_info (struct objfile *objfile)
2411{
2412 dw2_setup (objfile);
2413
2414 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2415 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2416}
2417
2418static int
2419dw2_lookup_symtab (struct objfile *objfile, const char *name,
2420 const char *full_path, const char *real_path,
2421 struct symtab **result)
2422{
2423 int i;
2424 int check_basename = lbasename (name) == name;
2425 struct dwarf2_per_cu_data *base_cu = NULL;
2426
2427 dw2_setup (objfile);
ae2de4f8 2428
1fd400ff
TT
2429 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2430 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2431 {
2432 int j;
e254ef6a 2433 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2434 struct quick_file_names *file_data;
9291a0cd 2435
e254ef6a 2436 if (per_cu->v.quick->symtab)
9291a0cd
TT
2437 continue;
2438
7b9f3c50
DE
2439 file_data = dw2_get_file_names (objfile, per_cu);
2440 if (file_data == NULL)
9291a0cd
TT
2441 continue;
2442
7b9f3c50 2443 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2444 {
7b9f3c50 2445 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2446
2447 if (FILENAME_CMP (name, this_name) == 0)
2448 {
e254ef6a 2449 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2450 return 1;
2451 }
2452
2453 if (check_basename && ! base_cu
2454 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2455 base_cu = per_cu;
9291a0cd
TT
2456
2457 if (full_path != NULL)
2458 {
7b9f3c50
DE
2459 const char *this_real_name = dw2_get_real_path (objfile,
2460 file_data, j);
9291a0cd 2461
7b9f3c50
DE
2462 if (this_real_name != NULL
2463 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2464 {
e254ef6a 2465 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2466 return 1;
2467 }
2468 }
2469
2470 if (real_path != NULL)
2471 {
7b9f3c50
DE
2472 const char *this_real_name = dw2_get_real_path (objfile,
2473 file_data, j);
9291a0cd 2474
7b9f3c50
DE
2475 if (this_real_name != NULL
2476 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2477 {
74dd2ca6
DE
2478 *result = dw2_instantiate_symtab (objfile, per_cu);
2479 return 1;
9291a0cd
TT
2480 }
2481 }
2482 }
2483 }
2484
2485 if (base_cu)
2486 {
2487 *result = dw2_instantiate_symtab (objfile, base_cu);
2488 return 1;
2489 }
2490
2491 return 0;
2492}
2493
2494static struct symtab *
2495dw2_lookup_symbol (struct objfile *objfile, int block_index,
2496 const char *name, domain_enum domain)
2497{
774b6a14 2498 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2499 instead. */
2500 return NULL;
2501}
2502
2503/* A helper function that expands all symtabs that hold an object
2504 named NAME. */
2fdf6df6 2505
9291a0cd
TT
2506static void
2507dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2508{
2509 dw2_setup (objfile);
2510
ae2de4f8 2511 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2512 if (dwarf2_per_objfile->index_table)
2513 {
2514 offset_type *vec;
2515
2516 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2517 name, &vec))
2518 {
2519 offset_type i, len = MAYBE_SWAP (*vec);
2520 for (i = 0; i < len; ++i)
2521 {
2522 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2523 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2524
e254ef6a 2525 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2526 }
2527 }
2528 }
2529}
2530
774b6a14
TT
2531static void
2532dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2533 enum block_enum block_kind, const char *name,
774b6a14 2534 domain_enum domain)
9291a0cd 2535{
774b6a14 2536 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2537}
2538
2539static void
2540dw2_print_stats (struct objfile *objfile)
2541{
2542 int i, count;
2543
2544 dw2_setup (objfile);
2545 count = 0;
1fd400ff
TT
2546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2547 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2548 {
e254ef6a 2549 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2550
e254ef6a 2551 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2552 ++count;
2553 }
2554 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2555}
2556
2557static void
2558dw2_dump (struct objfile *objfile)
2559{
2560 /* Nothing worth printing. */
2561}
2562
2563static void
2564dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2565 struct section_offsets *delta)
2566{
2567 /* There's nothing to relocate here. */
2568}
2569
2570static void
2571dw2_expand_symtabs_for_function (struct objfile *objfile,
2572 const char *func_name)
2573{
2574 dw2_do_expand_symtabs_matching (objfile, func_name);
2575}
2576
2577static void
2578dw2_expand_all_symtabs (struct objfile *objfile)
2579{
2580 int i;
2581
2582 dw2_setup (objfile);
1fd400ff
TT
2583
2584 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2585 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2586 {
e254ef6a 2587 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2588
e254ef6a 2589 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2590 }
2591}
2592
2593static void
2594dw2_expand_symtabs_with_filename (struct objfile *objfile,
2595 const char *filename)
2596{
2597 int i;
2598
2599 dw2_setup (objfile);
d4637a04
DE
2600
2601 /* We don't need to consider type units here.
2602 This is only called for examining code, e.g. expand_line_sal.
2603 There can be an order of magnitude (or more) more type units
2604 than comp units, and we avoid them if we can. */
2605
2606 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2607 {
2608 int j;
e254ef6a 2609 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2610 struct quick_file_names *file_data;
9291a0cd 2611
e254ef6a 2612 if (per_cu->v.quick->symtab)
9291a0cd
TT
2613 continue;
2614
7b9f3c50
DE
2615 file_data = dw2_get_file_names (objfile, per_cu);
2616 if (file_data == NULL)
9291a0cd
TT
2617 continue;
2618
7b9f3c50 2619 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2620 {
7b9f3c50 2621 const char *this_name = file_data->file_names[j];
1ef75ecc 2622 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2623 {
e254ef6a 2624 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2625 break;
2626 }
2627 }
2628 }
2629}
2630
dd786858 2631static const char *
9291a0cd
TT
2632dw2_find_symbol_file (struct objfile *objfile, const char *name)
2633{
e254ef6a 2634 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2635 offset_type *vec;
7b9f3c50 2636 struct quick_file_names *file_data;
9291a0cd
TT
2637
2638 dw2_setup (objfile);
2639
ae2de4f8 2640 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2641 if (!dwarf2_per_objfile->index_table)
2642 return NULL;
2643
2644 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2645 name, &vec))
2646 return NULL;
2647
2648 /* Note that this just looks at the very first one named NAME -- but
2649 actually we are looking for a function. find_main_filename
2650 should be rewritten so that it doesn't require a custom hook. It
2651 could just use the ordinary symbol tables. */
2652 /* vec[0] is the length, which must always be >0. */
e254ef6a 2653 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2654
7b9f3c50
DE
2655 file_data = dw2_get_file_names (objfile, per_cu);
2656 if (file_data == NULL)
9291a0cd
TT
2657 return NULL;
2658
7b9f3c50 2659 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2660}
2661
2662static void
40658b94
PH
2663dw2_map_matching_symbols (const char * name, domain_enum namespace,
2664 struct objfile *objfile, int global,
2665 int (*callback) (struct block *,
2666 struct symbol *, void *),
2edb89d3
JK
2667 void *data, symbol_compare_ftype *match,
2668 symbol_compare_ftype *ordered_compare)
9291a0cd 2669{
40658b94 2670 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2671 current language is Ada for a non-Ada objfile using GNU index. As Ada
2672 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2673}
2674
2675static void
2676dw2_expand_symtabs_matching (struct objfile *objfile,
2677 int (*file_matcher) (const char *, void *),
2678 int (*name_matcher) (const char *, void *),
8903c50d 2679 enum search_domain kind,
9291a0cd
TT
2680 void *data)
2681{
2682 int i;
2683 offset_type iter;
4b5246aa 2684 struct mapped_index *index;
9291a0cd
TT
2685
2686 dw2_setup (objfile);
ae2de4f8
DE
2687
2688 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2689 if (!dwarf2_per_objfile->index_table)
2690 return;
4b5246aa 2691 index = dwarf2_per_objfile->index_table;
9291a0cd 2692
7b08b9eb
JK
2693 if (file_matcher != NULL)
2694 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2695 + dwarf2_per_objfile->n_type_comp_units); ++i)
2696 {
2697 int j;
2698 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2699 struct quick_file_names *file_data;
2700
2701 per_cu->v.quick->mark = 0;
2702 if (per_cu->v.quick->symtab)
2703 continue;
2704
2705 file_data = dw2_get_file_names (objfile, per_cu);
2706 if (file_data == NULL)
2707 continue;
2708
2709 for (j = 0; j < file_data->num_file_names; ++j)
2710 {
2711 if (file_matcher (file_data->file_names[j], data))
2712 {
2713 per_cu->v.quick->mark = 1;
2714 break;
2715 }
2716 }
2717 }
9291a0cd 2718
3876f04e 2719 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2720 {
2721 offset_type idx = 2 * iter;
2722 const char *name;
2723 offset_type *vec, vec_len, vec_idx;
2724
3876f04e 2725 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2726 continue;
2727
3876f04e 2728 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2729
2730 if (! (*name_matcher) (name, data))
2731 continue;
2732
2733 /* The name was matched, now expand corresponding CUs that were
2734 marked. */
4b5246aa 2735 vec = (offset_type *) (index->constant_pool
3876f04e 2736 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2737 vec_len = MAYBE_SWAP (vec[0]);
2738 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2739 {
e254ef6a 2740 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2741
e254ef6a 2742 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2743 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2744 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2745 }
2746 }
2747}
2748
2749static struct symtab *
2750dw2_find_pc_sect_symtab (struct objfile *objfile,
2751 struct minimal_symbol *msymbol,
2752 CORE_ADDR pc,
2753 struct obj_section *section,
2754 int warn_if_readin)
2755{
2756 struct dwarf2_per_cu_data *data;
2757
2758 dw2_setup (objfile);
2759
2760 if (!objfile->psymtabs_addrmap)
2761 return NULL;
2762
2763 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2764 if (!data)
2765 return NULL;
2766
2767 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2768 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2769 paddress (get_objfile_arch (objfile), pc));
2770
2771 return dw2_instantiate_symtab (objfile, data);
2772}
2773
9291a0cd 2774static void
44b13c5a 2775dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2776 void *data)
2777{
2778 int i;
2779
2780 dw2_setup (objfile);
ae2de4f8 2781
1fd400ff
TT
2782 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2783 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2784 {
2785 int j;
e254ef6a 2786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2787 struct quick_file_names *file_data;
9291a0cd 2788
e254ef6a 2789 if (per_cu->v.quick->symtab)
9291a0cd
TT
2790 continue;
2791
7b9f3c50
DE
2792 file_data = dw2_get_file_names (objfile, per_cu);
2793 if (file_data == NULL)
9291a0cd
TT
2794 continue;
2795
7b9f3c50 2796 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2797 {
7b9f3c50
DE
2798 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2799 j);
2800 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2801 }
2802 }
2803}
2804
2805static int
2806dw2_has_symbols (struct objfile *objfile)
2807{
2808 return 1;
2809}
2810
2811const struct quick_symbol_functions dwarf2_gdb_index_functions =
2812{
2813 dw2_has_symbols,
2814 dw2_find_last_source_symtab,
2815 dw2_forget_cached_source_info,
2816 dw2_lookup_symtab,
2817 dw2_lookup_symbol,
774b6a14 2818 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2819 dw2_print_stats,
2820 dw2_dump,
2821 dw2_relocate,
2822 dw2_expand_symtabs_for_function,
2823 dw2_expand_all_symtabs,
2824 dw2_expand_symtabs_with_filename,
2825 dw2_find_symbol_file,
40658b94 2826 dw2_map_matching_symbols,
9291a0cd
TT
2827 dw2_expand_symtabs_matching,
2828 dw2_find_pc_sect_symtab,
9291a0cd
TT
2829 dw2_map_symbol_filenames
2830};
2831
2832/* Initialize for reading DWARF for this objfile. Return 0 if this
2833 file will use psymtabs, or 1 if using the GNU index. */
2834
2835int
2836dwarf2_initialize_objfile (struct objfile *objfile)
2837{
2838 /* If we're about to read full symbols, don't bother with the
2839 indices. In this case we also don't care if some other debug
2840 format is making psymtabs, because they are all about to be
2841 expanded anyway. */
2842 if ((objfile->flags & OBJF_READNOW))
2843 {
2844 int i;
2845
2846 dwarf2_per_objfile->using_index = 1;
2847 create_all_comp_units (objfile);
1fd400ff 2848 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2849 dwarf2_per_objfile->quick_file_names_table =
2850 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2851
1fd400ff
TT
2852 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2853 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2854 {
e254ef6a 2855 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2856
e254ef6a
DE
2857 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2858 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2859 }
2860
2861 /* Return 1 so that gdb sees the "quick" functions. However,
2862 these functions will be no-ops because we will have expanded
2863 all symtabs. */
2864 return 1;
2865 }
2866
2867 if (dwarf2_read_index (objfile))
2868 return 1;
2869
9291a0cd
TT
2870 return 0;
2871}
2872
2873\f
2874
dce234bc
PP
2875/* Build a partial symbol table. */
2876
2877void
f29dff0a 2878dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2879{
f29dff0a 2880 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2881 {
2882 init_psymbol_list (objfile, 1024);
2883 }
2884
d146bf1e 2885 dwarf2_build_psymtabs_hard (objfile);
c906108c 2886}
c906108c 2887
45452591
DE
2888/* Return TRUE if OFFSET is within CU_HEADER. */
2889
2890static inline int
2891offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2892{
2893 unsigned int bottom = cu_header->offset;
2894 unsigned int top = (cu_header->offset
2895 + cu_header->length
2896 + cu_header->initial_length_size);
9a619af0 2897
45452591
DE
2898 return (offset >= bottom && offset < top);
2899}
2900
93311388
DE
2901/* Read in the comp unit header information from the debug_info at info_ptr.
2902 NOTE: This leaves members offset, first_die_offset to be filled in
2903 by the caller. */
107d2387 2904
fe1b8b76 2905static gdb_byte *
107d2387 2906read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2907 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2908{
2909 int signed_addr;
891d2f0b 2910 unsigned int bytes_read;
c764a876
DE
2911
2912 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2913 cu_header->initial_length_size = bytes_read;
2914 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2915 info_ptr += bytes_read;
107d2387
AC
2916 cu_header->version = read_2_bytes (abfd, info_ptr);
2917 info_ptr += 2;
613e1657 2918 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2919 &bytes_read);
613e1657 2920 info_ptr += bytes_read;
107d2387
AC
2921 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2922 info_ptr += 1;
2923 signed_addr = bfd_get_sign_extend_vma (abfd);
2924 if (signed_addr < 0)
8e65ff28 2925 internal_error (__FILE__, __LINE__,
e2e0b3e5 2926 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2927 cu_header->signed_addr_p = signed_addr;
c764a876 2928
107d2387
AC
2929 return info_ptr;
2930}
2931
fe1b8b76
JB
2932static gdb_byte *
2933partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2934 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2935 bfd *abfd)
2936{
fe1b8b76 2937 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2938
2939 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2940
2dc7f7b3 2941 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2942 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2943 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2944 bfd_get_filename (abfd));
72bf9492 2945
9e0ac564
TT
2946 if (header->abbrev_offset
2947 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2948 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2949 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2950 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2951 (long) header->abbrev_offset,
93311388 2952 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2953 bfd_get_filename (abfd));
2954
2955 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2956 > buffer + buffer_size)
8a3fe4f8
AC
2957 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2958 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2959 (long) header->length,
93311388 2960 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2961 bfd_get_filename (abfd));
2962
2963 return info_ptr;
2964}
2965
348e048f
DE
2966/* Read in the types comp unit header information from .debug_types entry at
2967 types_ptr. The result is a pointer to one past the end of the header. */
2968
2969static gdb_byte *
2970read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 2971 struct dwarf2_section_info *section,
348e048f
DE
2972 ULONGEST *signature,
2973 gdb_byte *types_ptr, bfd *abfd)
2974{
348e048f
DE
2975 gdb_byte *initial_types_ptr = types_ptr;
2976
8b70b953
TT
2977 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
2978 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
2979
2980 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2981
2982 *signature = read_8_bytes (abfd, types_ptr);
2983 types_ptr += 8;
2984 types_ptr += cu_header->offset_size;
2985 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2986
2987 return types_ptr;
2988}
2989
aaa75496
JB
2990/* Allocate a new partial symtab for file named NAME and mark this new
2991 partial symtab as being an include of PST. */
2992
2993static void
2994dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2995 struct objfile *objfile)
2996{
2997 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2998
2999 subpst->section_offsets = pst->section_offsets;
3000 subpst->textlow = 0;
3001 subpst->texthigh = 0;
3002
3003 subpst->dependencies = (struct partial_symtab **)
3004 obstack_alloc (&objfile->objfile_obstack,
3005 sizeof (struct partial_symtab *));
3006 subpst->dependencies[0] = pst;
3007 subpst->number_of_dependencies = 1;
3008
3009 subpst->globals_offset = 0;
3010 subpst->n_global_syms = 0;
3011 subpst->statics_offset = 0;
3012 subpst->n_static_syms = 0;
3013 subpst->symtab = NULL;
3014 subpst->read_symtab = pst->read_symtab;
3015 subpst->readin = 0;
3016
3017 /* No private part is necessary for include psymtabs. This property
3018 can be used to differentiate between such include psymtabs and
10b3939b 3019 the regular ones. */
58a9656e 3020 subpst->read_symtab_private = NULL;
aaa75496
JB
3021}
3022
3023/* Read the Line Number Program data and extract the list of files
3024 included by the source file represented by PST. Build an include
d85a05f0 3025 partial symtab for each of these included files. */
aaa75496
JB
3026
3027static void
3028dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3029 struct die_info *die,
aaa75496
JB
3030 struct partial_symtab *pst)
3031{
3032 struct objfile *objfile = cu->objfile;
3033 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3034 struct line_header *lh = NULL;
3035 struct attribute *attr;
aaa75496 3036
d85a05f0
DJ
3037 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3038 if (attr)
3039 {
3040 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3041
d85a05f0
DJ
3042 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3043 }
aaa75496
JB
3044 if (lh == NULL)
3045 return; /* No linetable, so no includes. */
3046
c6da4cef
DE
3047 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3048 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3049
3050 free_line_header (lh);
3051}
3052
348e048f
DE
3053static hashval_t
3054hash_type_signature (const void *item)
3055{
3056 const struct signatured_type *type_sig = item;
9a619af0 3057
348e048f
DE
3058 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3059 return type_sig->signature;
3060}
3061
3062static int
3063eq_type_signature (const void *item_lhs, const void *item_rhs)
3064{
3065 const struct signatured_type *lhs = item_lhs;
3066 const struct signatured_type *rhs = item_rhs;
9a619af0 3067
348e048f
DE
3068 return lhs->signature == rhs->signature;
3069}
3070
1fd400ff
TT
3071/* Allocate a hash table for signatured types. */
3072
3073static htab_t
673bfd45 3074allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3075{
3076 return htab_create_alloc_ex (41,
3077 hash_type_signature,
3078 eq_type_signature,
3079 NULL,
3080 &objfile->objfile_obstack,
3081 hashtab_obstack_allocate,
3082 dummy_obstack_deallocate);
3083}
3084
3085/* A helper function to add a signatured type CU to a list. */
3086
3087static int
3088add_signatured_type_cu_to_list (void **slot, void *datum)
3089{
3090 struct signatured_type *sigt = *slot;
3091 struct dwarf2_per_cu_data ***datap = datum;
3092
3093 **datap = &sigt->per_cu;
3094 ++*datap;
3095
3096 return 1;
3097}
3098
348e048f
DE
3099/* Create the hash table of all entries in the .debug_types section.
3100 The result is zero if there is an error (e.g. missing .debug_types section),
3101 otherwise non-zero. */
3102
3103static int
3104create_debug_types_hash_table (struct objfile *objfile)
3105{
8b70b953 3106 htab_t types_htab = NULL;
1fd400ff 3107 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3108 int ix;
3109 struct dwarf2_section_info *section;
348e048f 3110
8b70b953 3111 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3112 {
3113 dwarf2_per_objfile->signatured_types = NULL;
3114 return 0;
3115 }
3116
8b70b953
TT
3117 for (ix = 0;
3118 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3119 ix, section);
3120 ++ix)
3121 {
3122 gdb_byte *info_ptr, *end_ptr;
348e048f 3123
8b70b953
TT
3124 dwarf2_read_section (objfile, section);
3125 info_ptr = section->buffer;
348e048f 3126
8b70b953
TT
3127 if (info_ptr == NULL)
3128 continue;
348e048f 3129
8b70b953
TT
3130 if (types_htab == NULL)
3131 types_htab = allocate_signatured_type_table (objfile);
348e048f 3132
8b70b953
TT
3133 if (dwarf2_die_debug)
3134 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3135
3136 end_ptr = info_ptr + section->size;
3137 while (info_ptr < end_ptr)
3138 {
3139 unsigned int offset;
3140 unsigned int offset_size;
3141 unsigned int type_offset;
3142 unsigned int length, initial_length_size;
3143 unsigned short version;
3144 ULONGEST signature;
3145 struct signatured_type *type_sig;
3146 void **slot;
3147 gdb_byte *ptr = info_ptr;
348e048f 3148
8b70b953 3149 offset = ptr - section->buffer;
348e048f 3150
8b70b953
TT
3151 /* We need to read the type's signature in order to build the hash
3152 table, but we don't need to read anything else just yet. */
348e048f 3153
8b70b953
TT
3154 /* Sanity check to ensure entire cu is present. */
3155 length = read_initial_length (objfile->obfd, ptr,
3156 &initial_length_size);
3157 if (ptr + length + initial_length_size > end_ptr)
3158 {
3159 complaint (&symfile_complaints,
3160 _("debug type entry runs off end "
3161 "of `.debug_types' section, ignored"));
3162 break;
3163 }
348e048f 3164
8b70b953
TT
3165 offset_size = initial_length_size == 4 ? 4 : 8;
3166 ptr += initial_length_size;
3167 version = bfd_get_16 (objfile->obfd, ptr);
3168 ptr += 2;
3169 ptr += offset_size; /* abbrev offset */
3170 ptr += 1; /* address size */
3171 signature = bfd_get_64 (objfile->obfd, ptr);
3172 ptr += 8;
3173 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3174
3175 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3176 memset (type_sig, 0, sizeof (*type_sig));
3177 type_sig->signature = signature;
3178 type_sig->type_offset = type_offset;
3179 type_sig->per_cu.objfile = objfile;
3180 type_sig->per_cu.debug_type_section = section;
3181 type_sig->per_cu.offset = offset;
3182
3183 slot = htab_find_slot (types_htab, type_sig, INSERT);
3184 gdb_assert (slot != NULL);
3185 if (*slot != NULL)
3186 {
3187 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3188
8b70b953
TT
3189 complaint (&symfile_complaints,
3190 _("debug type entry at offset 0x%x is duplicate to the "
3191 "entry at offset 0x%x, signature 0x%s"),
3192 offset, dup_sig->per_cu.offset,
3193 phex (signature, sizeof (signature)));
3194 gdb_assert (signature == dup_sig->signature);
3195 }
3196 *slot = type_sig;
348e048f 3197
8b70b953
TT
3198 if (dwarf2_die_debug)
3199 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3200 offset, phex (signature, sizeof (signature)));
348e048f 3201
8b70b953
TT
3202 info_ptr = info_ptr + initial_length_size + length;
3203 }
348e048f
DE
3204 }
3205
3206 dwarf2_per_objfile->signatured_types = types_htab;
3207
1fd400ff
TT
3208 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3209 dwarf2_per_objfile->type_comp_units
3210 = obstack_alloc (&objfile->objfile_obstack,
3211 dwarf2_per_objfile->n_type_comp_units
3212 * sizeof (struct dwarf2_per_cu_data *));
3213 iter = &dwarf2_per_objfile->type_comp_units[0];
3214 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3215 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3216 == dwarf2_per_objfile->n_type_comp_units);
3217
348e048f
DE
3218 return 1;
3219}
3220
3221/* Lookup a signature based type.
3222 Returns NULL if SIG is not present in the table. */
3223
3224static struct signatured_type *
3225lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3226{
3227 struct signatured_type find_entry, *entry;
3228
3229 if (dwarf2_per_objfile->signatured_types == NULL)
3230 {
3231 complaint (&symfile_complaints,
55f1336d 3232 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3233 return 0;
3234 }
3235
3236 find_entry.signature = sig;
3237 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3238 return entry;
3239}
3240
d85a05f0
DJ
3241/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3242
3243static void
3244init_cu_die_reader (struct die_reader_specs *reader,
3245 struct dwarf2_cu *cu)
3246{
3247 reader->abfd = cu->objfile->obfd;
3248 reader->cu = cu;
8b70b953 3249 if (cu->per_cu->debug_type_section)
be391dca 3250 {
8b70b953
TT
3251 gdb_assert (cu->per_cu->debug_type_section->readin);
3252 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3253 }
d85a05f0 3254 else
be391dca
TT
3255 {
3256 gdb_assert (dwarf2_per_objfile->info.readin);
3257 reader->buffer = dwarf2_per_objfile->info.buffer;
3258 }
d85a05f0
DJ
3259}
3260
3261/* Find the base address of the compilation unit for range lists and
3262 location lists. It will normally be specified by DW_AT_low_pc.
3263 In DWARF-3 draft 4, the base address could be overridden by
3264 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3265 compilation units with discontinuous ranges. */
3266
3267static void
3268dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3269{
3270 struct attribute *attr;
3271
3272 cu->base_known = 0;
3273 cu->base_address = 0;
3274
3275 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3276 if (attr)
3277 {
3278 cu->base_address = DW_ADDR (attr);
3279 cu->base_known = 1;
3280 }
3281 else
3282 {
3283 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3284 if (attr)
3285 {
3286 cu->base_address = DW_ADDR (attr);
3287 cu->base_known = 1;
3288 }
3289 }
3290}
3291
348e048f
DE
3292/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3293 to combine the common parts.
93311388 3294 Process a compilation unit for a psymtab.
348e048f
DE
3295 BUFFER is a pointer to the beginning of the dwarf section buffer,
3296 either .debug_info or debug_types.
93311388
DE
3297 INFO_PTR is a pointer to the start of the CU.
3298 Returns a pointer to the next CU. */
aaa75496 3299
93311388
DE
3300static gdb_byte *
3301process_psymtab_comp_unit (struct objfile *objfile,
3302 struct dwarf2_per_cu_data *this_cu,
3303 gdb_byte *buffer, gdb_byte *info_ptr,
3304 unsigned int buffer_size)
c906108c 3305{
c906108c 3306 bfd *abfd = objfile->obfd;
93311388 3307 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3308 struct die_info *comp_unit_die;
c906108c 3309 struct partial_symtab *pst;
5734ee8b 3310 CORE_ADDR baseaddr;
93311388
DE
3311 struct cleanup *back_to_inner;
3312 struct dwarf2_cu cu;
d85a05f0
DJ
3313 int has_children, has_pc_info;
3314 struct attribute *attr;
d85a05f0
DJ
3315 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3316 struct die_reader_specs reader_specs;
3e2a0cee 3317 const char *filename;
c906108c 3318
9816fde3 3319 init_one_comp_unit (&cu, objfile);
93311388 3320 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3321
93311388
DE
3322 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3323 buffer, buffer_size,
3324 abfd);
10b3939b 3325
93311388
DE
3326 /* Complete the cu_header. */
3327 cu.header.offset = beg_of_comp_unit - buffer;
3328 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3329
93311388 3330 cu.list_in_scope = &file_symbols;
af703f96 3331
328c9494
DJ
3332 /* If this compilation unit was already read in, free the
3333 cached copy in order to read it in again. This is
3334 necessary because we skipped some symbols when we first
3335 read in the compilation unit (see load_partial_dies).
3336 This problem could be avoided, but the benefit is
3337 unclear. */
3338 if (this_cu->cu != NULL)
3339 free_one_cached_comp_unit (this_cu->cu);
3340
3341 /* Note that this is a pointer to our stack frame, being
3342 added to a global data structure. It will be cleaned up
3343 in free_stack_comp_unit when we finish with this
3344 compilation unit. */
3345 this_cu->cu = &cu;
d85a05f0
DJ
3346 cu.per_cu = this_cu;
3347
93311388
DE
3348 /* Read the abbrevs for this compilation unit into a table. */
3349 dwarf2_read_abbrevs (abfd, &cu);
3350 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3351
93311388 3352 /* Read the compilation unit die. */
8b70b953 3353 if (this_cu->debug_type_section)
348e048f 3354 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3355 init_cu_die_reader (&reader_specs, &cu);
3356 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3357 &has_children);
93311388 3358
8b70b953 3359 if (this_cu->debug_type_section)
348e048f 3360 {
b3c8eb43
JK
3361 /* LENGTH has not been set yet for type units. */
3362 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3363 this_cu->length = cu.header.length + cu.header.initial_length_size;
3364 }
d85a05f0 3365 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3366 {
93311388
DE
3367 info_ptr = (beg_of_comp_unit + cu.header.length
3368 + cu.header.initial_length_size);
3369 do_cleanups (back_to_inner);
3370 return info_ptr;
3371 }
72bf9492 3372
9816fde3 3373 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3374
93311388 3375 /* Allocate a new partial symbol table structure. */
d85a05f0 3376 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3377 if (attr == NULL || !DW_STRING (attr))
3378 filename = "";
3379 else
3380 filename = DW_STRING (attr);
93311388 3381 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3382 filename,
93311388
DE
3383 /* TEXTLOW and TEXTHIGH are set below. */
3384 0,
3385 objfile->global_psymbols.next,
3386 objfile->static_psymbols.next);
72bf9492 3387
d85a05f0
DJ
3388 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3389 if (attr != NULL)
3390 pst->dirname = DW_STRING (attr);
72bf9492 3391
e38df1d0 3392 pst->read_symtab_private = this_cu;
72bf9492 3393
93311388 3394 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3395
0963b4bd 3396 /* Store the function that reads in the rest of the symbol table. */
93311388 3397 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3398
9291a0cd 3399 this_cu->v.psymtab = pst;
c906108c 3400
d85a05f0
DJ
3401 dwarf2_find_base_address (comp_unit_die, &cu);
3402
93311388
DE
3403 /* Possibly set the default values of LOWPC and HIGHPC from
3404 `DW_AT_ranges'. */
d85a05f0
DJ
3405 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3406 &best_highpc, &cu, pst);
3407 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3408 /* Store the contiguous range if it is not empty; it can be empty for
3409 CUs with no code. */
3410 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3411 best_lowpc + baseaddr,
3412 best_highpc + baseaddr - 1, pst);
93311388
DE
3413
3414 /* Check if comp unit has_children.
3415 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3416 If not, there's no more debug_info for this comp unit. */
d85a05f0 3417 if (has_children)
93311388
DE
3418 {
3419 struct partial_die_info *first_die;
3420 CORE_ADDR lowpc, highpc;
31ffec48 3421
93311388
DE
3422 lowpc = ((CORE_ADDR) -1);
3423 highpc = ((CORE_ADDR) 0);
c906108c 3424
93311388 3425 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3426
93311388 3427 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3428 ! has_pc_info, &cu);
57c22c6c 3429
93311388
DE
3430 /* If we didn't find a lowpc, set it to highpc to avoid
3431 complaints from `maint check'. */
3432 if (lowpc == ((CORE_ADDR) -1))
3433 lowpc = highpc;
10b3939b 3434
93311388
DE
3435 /* If the compilation unit didn't have an explicit address range,
3436 then use the information extracted from its child dies. */
d85a05f0 3437 if (! has_pc_info)
93311388 3438 {
d85a05f0
DJ
3439 best_lowpc = lowpc;
3440 best_highpc = highpc;
93311388
DE
3441 }
3442 }
d85a05f0
DJ
3443 pst->textlow = best_lowpc + baseaddr;
3444 pst->texthigh = best_highpc + baseaddr;
c906108c 3445
93311388
DE
3446 pst->n_global_syms = objfile->global_psymbols.next -
3447 (objfile->global_psymbols.list + pst->globals_offset);
3448 pst->n_static_syms = objfile->static_psymbols.next -
3449 (objfile->static_psymbols.list + pst->statics_offset);
3450 sort_pst_symbols (pst);
c906108c 3451
93311388
DE
3452 info_ptr = (beg_of_comp_unit + cu.header.length
3453 + cu.header.initial_length_size);
ae038cb0 3454
8b70b953 3455 if (this_cu->debug_type_section)
348e048f
DE
3456 {
3457 /* It's not clear we want to do anything with stmt lists here.
3458 Waiting to see what gcc ultimately does. */
3459 }
d85a05f0 3460 else
93311388
DE
3461 {
3462 /* Get the list of files included in the current compilation unit,
3463 and build a psymtab for each of them. */
d85a05f0 3464 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3465 }
ae038cb0 3466
93311388 3467 do_cleanups (back_to_inner);
ae038cb0 3468
93311388
DE
3469 return info_ptr;
3470}
ff013f42 3471
348e048f
DE
3472/* Traversal function for htab_traverse_noresize.
3473 Process one .debug_types comp-unit. */
3474
3475static int
3476process_type_comp_unit (void **slot, void *info)
3477{
3478 struct signatured_type *entry = (struct signatured_type *) *slot;
3479 struct objfile *objfile = (struct objfile *) info;
3480 struct dwarf2_per_cu_data *this_cu;
3481
3482 this_cu = &entry->per_cu;
348e048f 3483
8b70b953 3484 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3485 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3486 this_cu->debug_type_section->buffer,
3487 (this_cu->debug_type_section->buffer
3488 + this_cu->offset),
3489 this_cu->debug_type_section->size);
348e048f
DE
3490
3491 return 1;
3492}
3493
3494/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3495 Build partial symbol tables for the .debug_types comp-units. */
3496
3497static void
3498build_type_psymtabs (struct objfile *objfile)
3499{
3500 if (! create_debug_types_hash_table (objfile))
3501 return;
3502
3503 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3504 process_type_comp_unit, objfile);
3505}
3506
60606b2c
TT
3507/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3508
3509static void
3510psymtabs_addrmap_cleanup (void *o)
3511{
3512 struct objfile *objfile = o;
ec61707d 3513
60606b2c
TT
3514 objfile->psymtabs_addrmap = NULL;
3515}
3516
93311388
DE
3517/* Build the partial symbol table by doing a quick pass through the
3518 .debug_info and .debug_abbrev sections. */
72bf9492 3519
93311388 3520static void
c67a9c90 3521dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3522{
93311388 3523 gdb_byte *info_ptr;
60606b2c
TT
3524 struct cleanup *back_to, *addrmap_cleanup;
3525 struct obstack temp_obstack;
93311388 3526
98bfdba5
PA
3527 dwarf2_per_objfile->reading_partial_symbols = 1;
3528
be391dca 3529 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3530 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3531
93311388
DE
3532 /* Any cached compilation units will be linked by the per-objfile
3533 read_in_chain. Make sure to free them when we're done. */
3534 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3535
348e048f
DE
3536 build_type_psymtabs (objfile);
3537
93311388 3538 create_all_comp_units (objfile);
c906108c 3539
60606b2c
TT
3540 /* Create a temporary address map on a temporary obstack. We later
3541 copy this to the final obstack. */
3542 obstack_init (&temp_obstack);
3543 make_cleanup_obstack_free (&temp_obstack);
3544 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3545 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3546
93311388
DE
3547 /* Since the objects we're extracting from .debug_info vary in
3548 length, only the individual functions to extract them (like
3549 read_comp_unit_head and load_partial_die) can really know whether
3550 the buffer is large enough to hold another complete object.
c906108c 3551
93311388
DE
3552 At the moment, they don't actually check that. If .debug_info
3553 holds just one extra byte after the last compilation unit's dies,
3554 then read_comp_unit_head will happily read off the end of the
3555 buffer. read_partial_die is similarly casual. Those functions
3556 should be fixed.
c906108c 3557
93311388
DE
3558 For this loop condition, simply checking whether there's any data
3559 left at all should be sufficient. */
c906108c 3560
93311388
DE
3561 while (info_ptr < (dwarf2_per_objfile->info.buffer
3562 + dwarf2_per_objfile->info.size))
3563 {
3564 struct dwarf2_per_cu_data *this_cu;
dd373385 3565
3e43a32a
MS
3566 this_cu = dwarf2_find_comp_unit (info_ptr
3567 - dwarf2_per_objfile->info.buffer,
93311388 3568 objfile);
aaa75496 3569
93311388
DE
3570 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3571 dwarf2_per_objfile->info.buffer,
3572 info_ptr,
3573 dwarf2_per_objfile->info.size);
c906108c 3574 }
ff013f42
JK
3575
3576 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3577 &objfile->objfile_obstack);
60606b2c 3578 discard_cleanups (addrmap_cleanup);
ff013f42 3579
ae038cb0
DJ
3580 do_cleanups (back_to);
3581}
3582
93311388 3583/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3584
3585static void
93311388
DE
3586load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3587 struct objfile *objfile)
ae038cb0
DJ
3588{
3589 bfd *abfd = objfile->obfd;
fe1b8b76 3590 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3591 struct die_info *comp_unit_die;
ae038cb0 3592 struct dwarf2_cu *cu;
1d9ec526 3593 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3594 int has_children;
3595 struct die_reader_specs reader_specs;
98bfdba5 3596 int read_cu = 0;
ae038cb0 3597
8b70b953 3598 gdb_assert (! this_cu->debug_type_section);
348e048f 3599
be391dca 3600 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3601 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3602 beg_of_comp_unit = info_ptr;
3603
98bfdba5
PA
3604 if (this_cu->cu == NULL)
3605 {
9816fde3
JK
3606 cu = xmalloc (sizeof (*cu));
3607 init_one_comp_unit (cu, objfile);
ae038cb0 3608
98bfdba5 3609 read_cu = 1;
ae038cb0 3610
98bfdba5
PA
3611 /* If an error occurs while loading, release our storage. */
3612 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3613
98bfdba5
PA
3614 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3615 dwarf2_per_objfile->info.buffer,
3616 dwarf2_per_objfile->info.size,
3617 abfd);
ae038cb0 3618
98bfdba5
PA
3619 /* Complete the cu_header. */
3620 cu->header.offset = this_cu->offset;
3621 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3622
3623 /* Link this compilation unit into the compilation unit tree. */
3624 this_cu->cu = cu;
3625 cu->per_cu = this_cu;
98bfdba5
PA
3626
3627 /* Link this CU into read_in_chain. */
3628 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3629 dwarf2_per_objfile->read_in_chain = this_cu;
3630 }
3631 else
3632 {
3633 cu = this_cu->cu;
3634 info_ptr += cu->header.first_die_offset;
3635 }
ae038cb0
DJ
3636
3637 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3638 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3639 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3640 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3641
3642 /* Read the compilation unit die. */
d85a05f0
DJ
3643 init_cu_die_reader (&reader_specs, cu);
3644 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3645 &has_children);
ae038cb0 3646
9816fde3 3647 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3648
ae038cb0
DJ
3649 /* Check if comp unit has_children.
3650 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3651 If not, there's no more debug_info for this comp unit. */
d85a05f0 3652 if (has_children)
93311388 3653 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3654
98bfdba5
PA
3655 do_cleanups (free_abbrevs_cleanup);
3656
3657 if (read_cu)
3658 {
3659 /* We've successfully allocated this compilation unit. Let our
3660 caller clean it up when finished with it. */
3661 discard_cleanups (free_cu_cleanup);
3662 }
ae038cb0
DJ
3663}
3664
3665/* Create a list of all compilation units in OBJFILE. We do this only
3666 if an inter-comp-unit reference is found; presumably if there is one,
3667 there will be many, and one will occur early in the .debug_info section.
3668 So there's no point in building this list incrementally. */
3669
3670static void
3671create_all_comp_units (struct objfile *objfile)
3672{
3673 int n_allocated;
3674 int n_comp_units;
3675 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3676 gdb_byte *info_ptr;
3677
3678 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3679 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3680
3681 n_comp_units = 0;
3682 n_allocated = 10;
3683 all_comp_units = xmalloc (n_allocated
3684 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3685
3e43a32a
MS
3686 while (info_ptr < dwarf2_per_objfile->info.buffer
3687 + dwarf2_per_objfile->info.size)
ae038cb0 3688 {
c764a876 3689 unsigned int length, initial_length_size;
ae038cb0 3690 struct dwarf2_per_cu_data *this_cu;
c764a876 3691 unsigned int offset;
ae038cb0 3692
dce234bc 3693 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3694
3695 /* Read just enough information to find out where the next
3696 compilation unit is. */
c764a876
DE
3697 length = read_initial_length (objfile->obfd, info_ptr,
3698 &initial_length_size);
ae038cb0
DJ
3699
3700 /* Save the compilation unit for later lookup. */
3701 this_cu = obstack_alloc (&objfile->objfile_obstack,
3702 sizeof (struct dwarf2_per_cu_data));
3703 memset (this_cu, 0, sizeof (*this_cu));
3704 this_cu->offset = offset;
c764a876 3705 this_cu->length = length + initial_length_size;
9291a0cd 3706 this_cu->objfile = objfile;
ae038cb0
DJ
3707
3708 if (n_comp_units == n_allocated)
3709 {
3710 n_allocated *= 2;
3711 all_comp_units = xrealloc (all_comp_units,
3712 n_allocated
3713 * sizeof (struct dwarf2_per_cu_data *));
3714 }
3715 all_comp_units[n_comp_units++] = this_cu;
3716
3717 info_ptr = info_ptr + this_cu->length;
3718 }
3719
3720 dwarf2_per_objfile->all_comp_units
3721 = obstack_alloc (&objfile->objfile_obstack,
3722 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3723 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3724 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3725 xfree (all_comp_units);
3726 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3727}
3728
5734ee8b
DJ
3729/* Process all loaded DIEs for compilation unit CU, starting at
3730 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3731 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3732 DW_AT_ranges). If NEED_PC is set, then this function will set
3733 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3734 and record the covered ranges in the addrmap. */
c906108c 3735
72bf9492
DJ
3736static void
3737scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3738 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3739{
72bf9492 3740 struct partial_die_info *pdi;
c906108c 3741
91c24f0a
DC
3742 /* Now, march along the PDI's, descending into ones which have
3743 interesting children but skipping the children of the other ones,
3744 until we reach the end of the compilation unit. */
c906108c 3745
72bf9492 3746 pdi = first_die;
91c24f0a 3747
72bf9492
DJ
3748 while (pdi != NULL)
3749 {
3750 fixup_partial_die (pdi, cu);
c906108c 3751
f55ee35c 3752 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3753 children, so we need to look at them. Ditto for anonymous
3754 enums. */
933c6fe4 3755
72bf9492 3756 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3757 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3758 {
72bf9492 3759 switch (pdi->tag)
c906108c
SS
3760 {
3761 case DW_TAG_subprogram:
5734ee8b 3762 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3763 break;
72929c62 3764 case DW_TAG_constant:
c906108c
SS
3765 case DW_TAG_variable:
3766 case DW_TAG_typedef:
91c24f0a 3767 case DW_TAG_union_type:
72bf9492 3768 if (!pdi->is_declaration)
63d06c5c 3769 {
72bf9492 3770 add_partial_symbol (pdi, cu);
63d06c5c
DC
3771 }
3772 break;
c906108c 3773 case DW_TAG_class_type:
680b30c7 3774 case DW_TAG_interface_type:
c906108c 3775 case DW_TAG_structure_type:
72bf9492 3776 if (!pdi->is_declaration)
c906108c 3777 {
72bf9492 3778 add_partial_symbol (pdi, cu);
c906108c
SS
3779 }
3780 break;
91c24f0a 3781 case DW_TAG_enumeration_type:
72bf9492
DJ
3782 if (!pdi->is_declaration)
3783 add_partial_enumeration (pdi, cu);
c906108c
SS
3784 break;
3785 case DW_TAG_base_type:
a02abb62 3786 case DW_TAG_subrange_type:
c906108c 3787 /* File scope base type definitions are added to the partial
c5aa993b 3788 symbol table. */
72bf9492 3789 add_partial_symbol (pdi, cu);
c906108c 3790 break;
d9fa45fe 3791 case DW_TAG_namespace:
5734ee8b 3792 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3793 break;
5d7cb8df
JK
3794 case DW_TAG_module:
3795 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3796 break;
c906108c
SS
3797 default:
3798 break;
3799 }
3800 }
3801
72bf9492
DJ
3802 /* If the die has a sibling, skip to the sibling. */
3803
3804 pdi = pdi->die_sibling;
3805 }
3806}
3807
3808/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3809
72bf9492 3810 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3811 name is concatenated with "::" and the partial DIE's name. For
3812 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3813 Enumerators are an exception; they use the scope of their parent
3814 enumeration type, i.e. the name of the enumeration type is not
3815 prepended to the enumerator.
91c24f0a 3816
72bf9492
DJ
3817 There are two complexities. One is DW_AT_specification; in this
3818 case "parent" means the parent of the target of the specification,
3819 instead of the direct parent of the DIE. The other is compilers
3820 which do not emit DW_TAG_namespace; in this case we try to guess
3821 the fully qualified name of structure types from their members'
3822 linkage names. This must be done using the DIE's children rather
3823 than the children of any DW_AT_specification target. We only need
3824 to do this for structures at the top level, i.e. if the target of
3825 any DW_AT_specification (if any; otherwise the DIE itself) does not
3826 have a parent. */
3827
3828/* Compute the scope prefix associated with PDI's parent, in
3829 compilation unit CU. The result will be allocated on CU's
3830 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3831 field. NULL is returned if no prefix is necessary. */
3832static char *
3833partial_die_parent_scope (struct partial_die_info *pdi,
3834 struct dwarf2_cu *cu)
3835{
3836 char *grandparent_scope;
3837 struct partial_die_info *parent, *real_pdi;
91c24f0a 3838
72bf9492
DJ
3839 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3840 then this means the parent of the specification DIE. */
3841
3842 real_pdi = pdi;
72bf9492 3843 while (real_pdi->has_specification)
10b3939b 3844 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3845
3846 parent = real_pdi->die_parent;
3847 if (parent == NULL)
3848 return NULL;
3849
3850 if (parent->scope_set)
3851 return parent->scope;
3852
3853 fixup_partial_die (parent, cu);
3854
10b3939b 3855 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3856
acebe513
UW
3857 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3858 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3859 Work around this problem here. */
3860 if (cu->language == language_cplus
6e70227d 3861 && parent->tag == DW_TAG_namespace
acebe513
UW
3862 && strcmp (parent->name, "::") == 0
3863 && grandparent_scope == NULL)
3864 {
3865 parent->scope = NULL;
3866 parent->scope_set = 1;
3867 return NULL;
3868 }
3869
72bf9492 3870 if (parent->tag == DW_TAG_namespace
f55ee35c 3871 || parent->tag == DW_TAG_module
72bf9492
DJ
3872 || parent->tag == DW_TAG_structure_type
3873 || parent->tag == DW_TAG_class_type
680b30c7 3874 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3875 || parent->tag == DW_TAG_union_type
3876 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3877 {
3878 if (grandparent_scope == NULL)
3879 parent->scope = parent->name;
3880 else
3e43a32a
MS
3881 parent->scope = typename_concat (&cu->comp_unit_obstack,
3882 grandparent_scope,
f55ee35c 3883 parent->name, 0, cu);
72bf9492 3884 }
ceeb3d5a 3885 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3886 /* Enumerators should not get the name of the enumeration as a prefix. */
3887 parent->scope = grandparent_scope;
3888 else
3889 {
3890 /* FIXME drow/2004-04-01: What should we be doing with
3891 function-local names? For partial symbols, we should probably be
3892 ignoring them. */
3893 complaint (&symfile_complaints,
e2e0b3e5 3894 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3895 parent->tag, pdi->offset);
3896 parent->scope = grandparent_scope;
c906108c
SS
3897 }
3898
72bf9492
DJ
3899 parent->scope_set = 1;
3900 return parent->scope;
3901}
3902
3903/* Return the fully scoped name associated with PDI, from compilation unit
3904 CU. The result will be allocated with malloc. */
3905static char *
3906partial_die_full_name (struct partial_die_info *pdi,
3907 struct dwarf2_cu *cu)
3908{
3909 char *parent_scope;
3910
98bfdba5
PA
3911 /* If this is a template instantiation, we can not work out the
3912 template arguments from partial DIEs. So, unfortunately, we have
3913 to go through the full DIEs. At least any work we do building
3914 types here will be reused if full symbols are loaded later. */
3915 if (pdi->has_template_arguments)
3916 {
3917 fixup_partial_die (pdi, cu);
3918
3919 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3920 {
3921 struct die_info *die;
3922 struct attribute attr;
3923 struct dwarf2_cu *ref_cu = cu;
3924
3925 attr.name = 0;
3926 attr.form = DW_FORM_ref_addr;
3927 attr.u.addr = pdi->offset;
3928 die = follow_die_ref (NULL, &attr, &ref_cu);
3929
3930 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3931 }
3932 }
3933
72bf9492
DJ
3934 parent_scope = partial_die_parent_scope (pdi, cu);
3935 if (parent_scope == NULL)
3936 return NULL;
3937 else
f55ee35c 3938 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3939}
3940
3941static void
72bf9492 3942add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3943{
e7c27a73 3944 struct objfile *objfile = cu->objfile;
c906108c 3945 CORE_ADDR addr = 0;
decbce07 3946 char *actual_name = NULL;
5c4e30ca 3947 const struct partial_symbol *psym = NULL;
e142c38c 3948 CORE_ADDR baseaddr;
72bf9492 3949 int built_actual_name = 0;
e142c38c
DJ
3950
3951 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3952
94af9270
KS
3953 actual_name = partial_die_full_name (pdi, cu);
3954 if (actual_name)
3955 built_actual_name = 1;
63d06c5c 3956
72bf9492
DJ
3957 if (actual_name == NULL)
3958 actual_name = pdi->name;
3959
c906108c
SS
3960 switch (pdi->tag)
3961 {
3962 case DW_TAG_subprogram:
2cfa0c8d 3963 if (pdi->is_external || cu->language == language_ada)
c906108c 3964 {
2cfa0c8d
JB
3965 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3966 of the global scope. But in Ada, we want to be able to access
3967 nested procedures globally. So all Ada subprograms are stored
3968 in the global scope. */
f47fb265 3969 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3970 mst_text, objfile); */
f47fb265
MS
3971 add_psymbol_to_list (actual_name, strlen (actual_name),
3972 built_actual_name,
3973 VAR_DOMAIN, LOC_BLOCK,
3974 &objfile->global_psymbols,
3975 0, pdi->lowpc + baseaddr,
3976 cu->language, objfile);
c906108c
SS
3977 }
3978 else
3979 {
f47fb265 3980 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3981 mst_file_text, objfile); */
f47fb265
MS
3982 add_psymbol_to_list (actual_name, strlen (actual_name),
3983 built_actual_name,
3984 VAR_DOMAIN, LOC_BLOCK,
3985 &objfile->static_psymbols,
3986 0, pdi->lowpc + baseaddr,
3987 cu->language, objfile);
c906108c
SS
3988 }
3989 break;
72929c62
JB
3990 case DW_TAG_constant:
3991 {
3992 struct psymbol_allocation_list *list;
3993
3994 if (pdi->is_external)
3995 list = &objfile->global_psymbols;
3996 else
3997 list = &objfile->static_psymbols;
f47fb265
MS
3998 add_psymbol_to_list (actual_name, strlen (actual_name),
3999 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4000 list, 0, 0, cu->language, objfile);
72929c62
JB
4001 }
4002 break;
c906108c 4003 case DW_TAG_variable:
caac4577
JG
4004 if (pdi->locdesc)
4005 addr = decode_locdesc (pdi->locdesc, cu);
4006
4007 if (pdi->locdesc
4008 && addr == 0
4009 && !dwarf2_per_objfile->has_section_at_zero)
4010 {
4011 /* A global or static variable may also have been stripped
4012 out by the linker if unused, in which case its address
4013 will be nullified; do not add such variables into partial
4014 symbol table then. */
4015 }
4016 else if (pdi->is_external)
c906108c
SS
4017 {
4018 /* Global Variable.
4019 Don't enter into the minimal symbol tables as there is
4020 a minimal symbol table entry from the ELF symbols already.
4021 Enter into partial symbol table if it has a location
4022 descriptor or a type.
4023 If the location descriptor is missing, new_symbol will create
4024 a LOC_UNRESOLVED symbol, the address of the variable will then
4025 be determined from the minimal symbol table whenever the variable
4026 is referenced.
4027 The address for the partial symbol table entry is not
4028 used by GDB, but it comes in handy for debugging partial symbol
4029 table building. */
4030
c906108c 4031 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4032 add_psymbol_to_list (actual_name, strlen (actual_name),
4033 built_actual_name,
4034 VAR_DOMAIN, LOC_STATIC,
4035 &objfile->global_psymbols,
4036 0, addr + baseaddr,
4037 cu->language, objfile);
c906108c
SS
4038 }
4039 else
4040 {
0963b4bd 4041 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4042 if (pdi->locdesc == NULL)
decbce07
MS
4043 {
4044 if (built_actual_name)
4045 xfree (actual_name);
4046 return;
4047 }
f47fb265 4048 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4049 mst_file_data, objfile); */
f47fb265
MS
4050 add_psymbol_to_list (actual_name, strlen (actual_name),
4051 built_actual_name,
4052 VAR_DOMAIN, LOC_STATIC,
4053 &objfile->static_psymbols,
4054 0, addr + baseaddr,
4055 cu->language, objfile);
c906108c
SS
4056 }
4057 break;
4058 case DW_TAG_typedef:
4059 case DW_TAG_base_type:
a02abb62 4060 case DW_TAG_subrange_type:
38d518c9 4061 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4062 built_actual_name,
176620f1 4063 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4064 &objfile->static_psymbols,
e142c38c 4065 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4066 break;
72bf9492
DJ
4067 case DW_TAG_namespace:
4068 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4069 built_actual_name,
72bf9492
DJ
4070 VAR_DOMAIN, LOC_TYPEDEF,
4071 &objfile->global_psymbols,
4072 0, (CORE_ADDR) 0, cu->language, objfile);
4073 break;
c906108c 4074 case DW_TAG_class_type:
680b30c7 4075 case DW_TAG_interface_type:
c906108c
SS
4076 case DW_TAG_structure_type:
4077 case DW_TAG_union_type:
4078 case DW_TAG_enumeration_type:
fa4028e9
JB
4079 /* Skip external references. The DWARF standard says in the section
4080 about "Structure, Union, and Class Type Entries": "An incomplete
4081 structure, union or class type is represented by a structure,
4082 union or class entry that does not have a byte size attribute
4083 and that has a DW_AT_declaration attribute." */
4084 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4085 {
4086 if (built_actual_name)
4087 xfree (actual_name);
4088 return;
4089 }
fa4028e9 4090
63d06c5c
DC
4091 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4092 static vs. global. */
38d518c9 4093 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4094 built_actual_name,
176620f1 4095 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4096 (cu->language == language_cplus
4097 || cu->language == language_java)
63d06c5c
DC
4098 ? &objfile->global_psymbols
4099 : &objfile->static_psymbols,
e142c38c 4100 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4101
c906108c
SS
4102 break;
4103 case DW_TAG_enumerator:
38d518c9 4104 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4105 built_actual_name,
176620f1 4106 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4107 (cu->language == language_cplus
4108 || cu->language == language_java)
f6fe98ef
DJ
4109 ? &objfile->global_psymbols
4110 : &objfile->static_psymbols,
e142c38c 4111 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4112 break;
4113 default:
4114 break;
4115 }
5c4e30ca 4116
72bf9492
DJ
4117 if (built_actual_name)
4118 xfree (actual_name);
c906108c
SS
4119}
4120
5c4e30ca
DC
4121/* Read a partial die corresponding to a namespace; also, add a symbol
4122 corresponding to that namespace to the symbol table. NAMESPACE is
4123 the name of the enclosing namespace. */
91c24f0a 4124
72bf9492
DJ
4125static void
4126add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4127 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4128 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4129{
72bf9492 4130 /* Add a symbol for the namespace. */
e7c27a73 4131
72bf9492 4132 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4133
4134 /* Now scan partial symbols in that namespace. */
4135
91c24f0a 4136 if (pdi->has_children)
5734ee8b 4137 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4138}
4139
5d7cb8df
JK
4140/* Read a partial die corresponding to a Fortran module. */
4141
4142static void
4143add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4144 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4145{
f55ee35c 4146 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4147
4148 if (pdi->has_children)
4149 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4150}
4151
bc30ff58
JB
4152/* Read a partial die corresponding to a subprogram and create a partial
4153 symbol for that subprogram. When the CU language allows it, this
4154 routine also defines a partial symbol for each nested subprogram
4155 that this subprogram contains.
6e70227d 4156
bc30ff58
JB
4157 DIE my also be a lexical block, in which case we simply search
4158 recursively for suprograms defined inside that lexical block.
4159 Again, this is only performed when the CU language allows this
4160 type of definitions. */
4161
4162static void
4163add_partial_subprogram (struct partial_die_info *pdi,
4164 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4165 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4166{
4167 if (pdi->tag == DW_TAG_subprogram)
4168 {
4169 if (pdi->has_pc_info)
4170 {
4171 if (pdi->lowpc < *lowpc)
4172 *lowpc = pdi->lowpc;
4173 if (pdi->highpc > *highpc)
4174 *highpc = pdi->highpc;
5734ee8b
DJ
4175 if (need_pc)
4176 {
4177 CORE_ADDR baseaddr;
4178 struct objfile *objfile = cu->objfile;
4179
4180 baseaddr = ANOFFSET (objfile->section_offsets,
4181 SECT_OFF_TEXT (objfile));
4182 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4183 pdi->lowpc + baseaddr,
4184 pdi->highpc - 1 + baseaddr,
9291a0cd 4185 cu->per_cu->v.psymtab);
5734ee8b 4186 }
bc30ff58 4187 if (!pdi->is_declaration)
e8d05480
JB
4188 /* Ignore subprogram DIEs that do not have a name, they are
4189 illegal. Do not emit a complaint at this point, we will
4190 do so when we convert this psymtab into a symtab. */
4191 if (pdi->name)
4192 add_partial_symbol (pdi, cu);
bc30ff58
JB
4193 }
4194 }
6e70227d 4195
bc30ff58
JB
4196 if (! pdi->has_children)
4197 return;
4198
4199 if (cu->language == language_ada)
4200 {
4201 pdi = pdi->die_child;
4202 while (pdi != NULL)
4203 {
4204 fixup_partial_die (pdi, cu);
4205 if (pdi->tag == DW_TAG_subprogram
4206 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4207 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4208 pdi = pdi->die_sibling;
4209 }
4210 }
4211}
4212
91c24f0a
DC
4213/* Read a partial die corresponding to an enumeration type. */
4214
72bf9492
DJ
4215static void
4216add_partial_enumeration (struct partial_die_info *enum_pdi,
4217 struct dwarf2_cu *cu)
91c24f0a 4218{
72bf9492 4219 struct partial_die_info *pdi;
91c24f0a
DC
4220
4221 if (enum_pdi->name != NULL)
72bf9492
DJ
4222 add_partial_symbol (enum_pdi, cu);
4223
4224 pdi = enum_pdi->die_child;
4225 while (pdi)
91c24f0a 4226 {
72bf9492 4227 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4228 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4229 else
72bf9492
DJ
4230 add_partial_symbol (pdi, cu);
4231 pdi = pdi->die_sibling;
91c24f0a 4232 }
91c24f0a
DC
4233}
4234
4bb7a0a7
DJ
4235/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4236 Return the corresponding abbrev, or NULL if the number is zero (indicating
4237 an empty DIE). In either case *BYTES_READ will be set to the length of
4238 the initial number. */
4239
4240static struct abbrev_info *
fe1b8b76 4241peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4242 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4243{
4244 bfd *abfd = cu->objfile->obfd;
4245 unsigned int abbrev_number;
4246 struct abbrev_info *abbrev;
4247
4248 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4249
4250 if (abbrev_number == 0)
4251 return NULL;
4252
4253 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4254 if (!abbrev)
4255 {
3e43a32a
MS
4256 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4257 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4258 }
4259
4260 return abbrev;
4261}
4262
93311388
DE
4263/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4264 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4265 DIE. Any children of the skipped DIEs will also be skipped. */
4266
fe1b8b76 4267static gdb_byte *
93311388 4268skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4269{
4270 struct abbrev_info *abbrev;
4271 unsigned int bytes_read;
4272
4273 while (1)
4274 {
4275 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4276 if (abbrev == NULL)
4277 return info_ptr + bytes_read;
4278 else
93311388 4279 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4280 }
4281}
4282
93311388
DE
4283/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4284 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4285 abbrev corresponding to that skipped uleb128 should be passed in
4286 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4287 children. */
4288
fe1b8b76 4289static gdb_byte *
93311388
DE
4290skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4291 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4292{
4293 unsigned int bytes_read;
4294 struct attribute attr;
4295 bfd *abfd = cu->objfile->obfd;
4296 unsigned int form, i;
4297
4298 for (i = 0; i < abbrev->num_attrs; i++)
4299 {
4300 /* The only abbrev we care about is DW_AT_sibling. */
4301 if (abbrev->attrs[i].name == DW_AT_sibling)
4302 {
4303 read_attribute (&attr, &abbrev->attrs[i],
4304 abfd, info_ptr, cu);
4305 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4306 complaint (&symfile_complaints,
4307 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4308 else
93311388 4309 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4310 }
4311
4312 /* If it isn't DW_AT_sibling, skip this attribute. */
4313 form = abbrev->attrs[i].form;
4314 skip_attribute:
4315 switch (form)
4316 {
4bb7a0a7 4317 case DW_FORM_ref_addr:
ae411497
TT
4318 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4319 and later it is offset sized. */
4320 if (cu->header.version == 2)
4321 info_ptr += cu->header.addr_size;
4322 else
4323 info_ptr += cu->header.offset_size;
4324 break;
4325 case DW_FORM_addr:
4bb7a0a7
DJ
4326 info_ptr += cu->header.addr_size;
4327 break;
4328 case DW_FORM_data1:
4329 case DW_FORM_ref1:
4330 case DW_FORM_flag:
4331 info_ptr += 1;
4332 break;
2dc7f7b3
TT
4333 case DW_FORM_flag_present:
4334 break;
4bb7a0a7
DJ
4335 case DW_FORM_data2:
4336 case DW_FORM_ref2:
4337 info_ptr += 2;
4338 break;
4339 case DW_FORM_data4:
4340 case DW_FORM_ref4:
4341 info_ptr += 4;
4342 break;
4343 case DW_FORM_data8:
4344 case DW_FORM_ref8:
55f1336d 4345 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4346 info_ptr += 8;
4347 break;
4348 case DW_FORM_string:
9b1c24c8 4349 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4350 info_ptr += bytes_read;
4351 break;
2dc7f7b3 4352 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4353 case DW_FORM_strp:
4354 info_ptr += cu->header.offset_size;
4355 break;
2dc7f7b3 4356 case DW_FORM_exprloc:
4bb7a0a7
DJ
4357 case DW_FORM_block:
4358 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4359 info_ptr += bytes_read;
4360 break;
4361 case DW_FORM_block1:
4362 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4363 break;
4364 case DW_FORM_block2:
4365 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4366 break;
4367 case DW_FORM_block4:
4368 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4369 break;
4370 case DW_FORM_sdata:
4371 case DW_FORM_udata:
4372 case DW_FORM_ref_udata:
4373 info_ptr = skip_leb128 (abfd, info_ptr);
4374 break;
4375 case DW_FORM_indirect:
4376 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4377 info_ptr += bytes_read;
4378 /* We need to continue parsing from here, so just go back to
4379 the top. */
4380 goto skip_attribute;
4381
4382 default:
3e43a32a
MS
4383 error (_("Dwarf Error: Cannot handle %s "
4384 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4385 dwarf_form_name (form),
4386 bfd_get_filename (abfd));
4387 }
4388 }
4389
4390 if (abbrev->has_children)
93311388 4391 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4392 else
4393 return info_ptr;
4394}
4395
93311388
DE
4396/* Locate ORIG_PDI's sibling.
4397 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4398 in BUFFER. */
91c24f0a 4399
fe1b8b76 4400static gdb_byte *
93311388
DE
4401locate_pdi_sibling (struct partial_die_info *orig_pdi,
4402 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4403 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4404{
4405 /* Do we know the sibling already? */
72bf9492 4406
91c24f0a
DC
4407 if (orig_pdi->sibling)
4408 return orig_pdi->sibling;
4409
4410 /* Are there any children to deal with? */
4411
4412 if (!orig_pdi->has_children)
4413 return info_ptr;
4414
4bb7a0a7 4415 /* Skip the children the long way. */
91c24f0a 4416
93311388 4417 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4418}
4419
c906108c
SS
4420/* Expand this partial symbol table into a full symbol table. */
4421
4422static void
fba45db2 4423dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4424{
c906108c
SS
4425 if (pst != NULL)
4426 {
4427 if (pst->readin)
4428 {
3e43a32a
MS
4429 warning (_("bug: psymtab for %s is already read in."),
4430 pst->filename);
c906108c
SS
4431 }
4432 else
4433 {
4434 if (info_verbose)
4435 {
3e43a32a
MS
4436 printf_filtered (_("Reading in symbols for %s..."),
4437 pst->filename);
c906108c
SS
4438 gdb_flush (gdb_stdout);
4439 }
4440
10b3939b
DJ
4441 /* Restore our global data. */
4442 dwarf2_per_objfile = objfile_data (pst->objfile,
4443 dwarf2_objfile_data_key);
4444
b2ab525c
KB
4445 /* If this psymtab is constructed from a debug-only objfile, the
4446 has_section_at_zero flag will not necessarily be correct. We
4447 can get the correct value for this flag by looking at the data
4448 associated with the (presumably stripped) associated objfile. */
4449 if (pst->objfile->separate_debug_objfile_backlink)
4450 {
4451 struct dwarf2_per_objfile *dpo_backlink
4452 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4453 dwarf2_objfile_data_key);
9a619af0 4454
b2ab525c
KB
4455 dwarf2_per_objfile->has_section_at_zero
4456 = dpo_backlink->has_section_at_zero;
4457 }
4458
98bfdba5
PA
4459 dwarf2_per_objfile->reading_partial_symbols = 0;
4460
c906108c
SS
4461 psymtab_to_symtab_1 (pst);
4462
4463 /* Finish up the debug error message. */
4464 if (info_verbose)
a3f17187 4465 printf_filtered (_("done.\n"));
c906108c
SS
4466 }
4467 }
4468}
4469
10b3939b
DJ
4470/* Add PER_CU to the queue. */
4471
4472static void
03dd20cc 4473queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4474{
4475 struct dwarf2_queue_item *item;
4476
4477 per_cu->queued = 1;
4478 item = xmalloc (sizeof (*item));
4479 item->per_cu = per_cu;
4480 item->next = NULL;
4481
4482 if (dwarf2_queue == NULL)
4483 dwarf2_queue = item;
4484 else
4485 dwarf2_queue_tail->next = item;
4486
4487 dwarf2_queue_tail = item;
4488}
4489
4490/* Process the queue. */
4491
4492static void
4493process_queue (struct objfile *objfile)
4494{
4495 struct dwarf2_queue_item *item, *next_item;
4496
03dd20cc
DJ
4497 /* The queue starts out with one item, but following a DIE reference
4498 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4499 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4500 {
9291a0cd
TT
4501 if (dwarf2_per_objfile->using_index
4502 ? !item->per_cu->v.quick->symtab
4503 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4504 process_full_comp_unit (item->per_cu);
4505
4506 item->per_cu->queued = 0;
4507 next_item = item->next;
4508 xfree (item);
4509 }
4510
4511 dwarf2_queue_tail = NULL;
4512}
4513
4514/* Free all allocated queue entries. This function only releases anything if
4515 an error was thrown; if the queue was processed then it would have been
4516 freed as we went along. */
4517
4518static void
4519dwarf2_release_queue (void *dummy)
4520{
4521 struct dwarf2_queue_item *item, *last;
4522
4523 item = dwarf2_queue;
4524 while (item)
4525 {
4526 /* Anything still marked queued is likely to be in an
4527 inconsistent state, so discard it. */
4528 if (item->per_cu->queued)
4529 {
4530 if (item->per_cu->cu != NULL)
4531 free_one_cached_comp_unit (item->per_cu->cu);
4532 item->per_cu->queued = 0;
4533 }
4534
4535 last = item;
4536 item = item->next;
4537 xfree (last);
4538 }
4539
4540 dwarf2_queue = dwarf2_queue_tail = NULL;
4541}
4542
4543/* Read in full symbols for PST, and anything it depends on. */
4544
c906108c 4545static void
fba45db2 4546psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4547{
10b3939b 4548 struct dwarf2_per_cu_data *per_cu;
c906108c 4549 struct cleanup *back_to;
aaa75496
JB
4550 int i;
4551
4552 for (i = 0; i < pst->number_of_dependencies; i++)
4553 if (!pst->dependencies[i]->readin)
4554 {
4555 /* Inform about additional files that need to be read in. */
4556 if (info_verbose)
4557 {
a3f17187 4558 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4559 fputs_filtered (" ", gdb_stdout);
4560 wrap_here ("");
4561 fputs_filtered ("and ", gdb_stdout);
4562 wrap_here ("");
4563 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4564 wrap_here (""); /* Flush output. */
aaa75496
JB
4565 gdb_flush (gdb_stdout);
4566 }
4567 psymtab_to_symtab_1 (pst->dependencies[i]);
4568 }
4569
e38df1d0 4570 per_cu = pst->read_symtab_private;
10b3939b
DJ
4571
4572 if (per_cu == NULL)
aaa75496
JB
4573 {
4574 /* It's an include file, no symbols to read for it.
4575 Everything is in the parent symtab. */
4576 pst->readin = 1;
4577 return;
4578 }
c906108c 4579
9291a0cd 4580 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4581}
4582
93311388 4583/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4584
93311388 4585static void
3e43a32a
MS
4586load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4587 struct objfile *objfile)
10b3939b 4588{
31ffec48 4589 bfd *abfd = objfile->obfd;
10b3939b 4590 struct dwarf2_cu *cu;
c764a876 4591 unsigned int offset;
93311388 4592 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4593 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4594 struct attribute *attr;
98bfdba5 4595 int read_cu = 0;
6502dd73 4596
8b70b953 4597 gdb_assert (! per_cu->debug_type_section);
348e048f 4598
c906108c 4599 /* Set local variables from the partial symbol table info. */
10b3939b 4600 offset = per_cu->offset;
6502dd73 4601
be391dca 4602 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4603 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4604 beg_of_comp_unit = info_ptr;
63d06c5c 4605
98bfdba5
PA
4606 if (per_cu->cu == NULL)
4607 {
9816fde3
JK
4608 cu = xmalloc (sizeof (*cu));
4609 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4610
4611 read_cu = 1;
c906108c 4612
98bfdba5
PA
4613 /* If an error occurs while loading, release our storage. */
4614 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4615
98bfdba5
PA
4616 /* Read in the comp_unit header. */
4617 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4618
98bfdba5
PA
4619 /* Complete the cu_header. */
4620 cu->header.offset = offset;
4621 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4622
98bfdba5
PA
4623 /* Read the abbrevs for this compilation unit. */
4624 dwarf2_read_abbrevs (abfd, cu);
4625 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4626
98bfdba5
PA
4627 /* Link this compilation unit into the compilation unit tree. */
4628 per_cu->cu = cu;
4629 cu->per_cu = per_cu;
98bfdba5
PA
4630
4631 /* Link this CU into read_in_chain. */
4632 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4633 dwarf2_per_objfile->read_in_chain = per_cu;
4634 }
4635 else
4636 {
4637 cu = per_cu->cu;
4638 info_ptr += cu->header.first_die_offset;
4639 }
e142c38c 4640
93311388 4641 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4642
4643 /* We try not to read any attributes in this function, because not
4644 all objfiles needed for references have been loaded yet, and symbol
4645 table processing isn't initialized. But we have to set the CU language,
4646 or we won't be able to build types correctly. */
9816fde3 4647 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4648
a6c727b2
DJ
4649 /* Similarly, if we do not read the producer, we can not apply
4650 producer-specific interpretation. */
4651 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4652 if (attr)
4653 cu->producer = DW_STRING (attr);
4654
98bfdba5
PA
4655 if (read_cu)
4656 {
4657 do_cleanups (free_abbrevs_cleanup);
e142c38c 4658
98bfdba5
PA
4659 /* We've successfully allocated this compilation unit. Let our
4660 caller clean it up when finished with it. */
4661 discard_cleanups (free_cu_cleanup);
4662 }
10b3939b
DJ
4663}
4664
3da10d80
KS
4665/* Add a DIE to the delayed physname list. */
4666
4667static void
4668add_to_method_list (struct type *type, int fnfield_index, int index,
4669 const char *name, struct die_info *die,
4670 struct dwarf2_cu *cu)
4671{
4672 struct delayed_method_info mi;
4673 mi.type = type;
4674 mi.fnfield_index = fnfield_index;
4675 mi.index = index;
4676 mi.name = name;
4677 mi.die = die;
4678 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4679}
4680
4681/* A cleanup for freeing the delayed method list. */
4682
4683static void
4684free_delayed_list (void *ptr)
4685{
4686 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4687 if (cu->method_list != NULL)
4688 {
4689 VEC_free (delayed_method_info, cu->method_list);
4690 cu->method_list = NULL;
4691 }
4692}
4693
4694/* Compute the physnames of any methods on the CU's method list.
4695
4696 The computation of method physnames is delayed in order to avoid the
4697 (bad) condition that one of the method's formal parameters is of an as yet
4698 incomplete type. */
4699
4700static void
4701compute_delayed_physnames (struct dwarf2_cu *cu)
4702{
4703 int i;
4704 struct delayed_method_info *mi;
4705 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4706 {
1d06ead6 4707 const char *physname;
3da10d80
KS
4708 struct fn_fieldlist *fn_flp
4709 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4710 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4711 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4712 }
4713}
4714
4632c0d0
JK
4715/* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4716 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4717 higher return INT_MAX. */
8be455d7
JK
4718
4719static int
4632c0d0 4720producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
8be455d7
JK
4721{
4722 const char *cs;
4723 int major, minor;
4724
4725 if (cu->producer == NULL)
4726 {
4727 /* For unknown compilers expect their behavior is not compliant. For GCC
4728 this case can also happen for -gdwarf-4 type units supported since
4729 gcc-4.5. */
4730
4632c0d0 4731 return -1;
8be455d7
JK
4732 }
4733
4734 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4735
4736 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4737 {
4738 /* For non-GCC compilers expect their behavior is not compliant. */
4739
4632c0d0 4740 return -1;
8be455d7
JK
4741 }
4742 cs = &cu->producer[strlen ("GNU ")];
4743 while (*cs && !isdigit (*cs))
4744 cs++;
4745 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4746 {
4747 /* Not recognized as GCC. */
4748
4632c0d0 4749 return -1;
8be455d7
JK
4750 }
4751
4632c0d0
JK
4752 if (major < 4)
4753 return -1;
4754 if (major > 4)
4755 return INT_MAX;
4756 return minor;
8be455d7
JK
4757}
4758
10b3939b
DJ
4759/* Generate full symbol information for PST and CU, whose DIEs have
4760 already been loaded into memory. */
4761
4762static void
4763process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4764{
10b3939b 4765 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4766 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4767 CORE_ADDR lowpc, highpc;
4768 struct symtab *symtab;
3da10d80 4769 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4770 CORE_ADDR baseaddr;
4771
4772 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4773
10b3939b
DJ
4774 buildsym_init ();
4775 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4776 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4777
4778 cu->list_in_scope = &file_symbols;
c906108c
SS
4779
4780 /* Do line number decoding in read_file_scope () */
10b3939b 4781 process_die (cu->dies, cu);
c906108c 4782
3da10d80
KS
4783 /* Now that we have processed all the DIEs in the CU, all the types
4784 should be complete, and it should now be safe to compute all of the
4785 physnames. */
4786 compute_delayed_physnames (cu);
4787 do_cleanups (delayed_list_cleanup);
4788
fae299cd
DC
4789 /* Some compilers don't define a DW_AT_high_pc attribute for the
4790 compilation unit. If the DW_AT_high_pc is missing, synthesize
4791 it, by scanning the DIE's below the compilation unit. */
10b3939b 4792 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4793
613e1657 4794 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4795
8be455d7 4796 if (symtab != NULL)
c906108c 4797 {
4632c0d0
JK
4798 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4799
8be455d7
JK
4800 /* Set symtab language to language from DW_AT_language. If the
4801 compilation is from a C file generated by language preprocessors, do
4802 not set the language if it was already deduced by start_subfile. */
4803 if (!(cu->language == language_c && symtab->language != language_c))
4804 symtab->language = cu->language;
4805
4806 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4807 produce DW_AT_location with location lists but it can be possibly
4808 invalid without -fvar-tracking.
4809
4810 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4811 needed, it would be wrong due to missing DW_AT_producer there.
4812
4813 Still one can confuse GDB by using non-standard GCC compilation
4814 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4815 */
4632c0d0 4816 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4817 symtab->locations_valid = 1;
e0d00bc7
JK
4818
4819 if (gcc_4_minor >= 5)
4820 symtab->epilogue_unwind_valid = 1;
c906108c 4821 }
9291a0cd
TT
4822
4823 if (dwarf2_per_objfile->using_index)
4824 per_cu->v.quick->symtab = symtab;
4825 else
4826 {
4827 struct partial_symtab *pst = per_cu->v.psymtab;
4828 pst->symtab = symtab;
4829 pst->readin = 1;
4830 }
c906108c
SS
4831
4832 do_cleanups (back_to);
4833}
4834
4835/* Process a die and its children. */
4836
4837static void
e7c27a73 4838process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4839{
4840 switch (die->tag)
4841 {
4842 case DW_TAG_padding:
4843 break;
4844 case DW_TAG_compile_unit:
e7c27a73 4845 read_file_scope (die, cu);
c906108c 4846 break;
348e048f
DE
4847 case DW_TAG_type_unit:
4848 read_type_unit_scope (die, cu);
4849 break;
c906108c 4850 case DW_TAG_subprogram:
c906108c 4851 case DW_TAG_inlined_subroutine:
edb3359d 4852 read_func_scope (die, cu);
c906108c
SS
4853 break;
4854 case DW_TAG_lexical_block:
14898363
L
4855 case DW_TAG_try_block:
4856 case DW_TAG_catch_block:
e7c27a73 4857 read_lexical_block_scope (die, cu);
c906108c
SS
4858 break;
4859 case DW_TAG_class_type:
680b30c7 4860 case DW_TAG_interface_type:
c906108c
SS
4861 case DW_TAG_structure_type:
4862 case DW_TAG_union_type:
134d01f1 4863 process_structure_scope (die, cu);
c906108c
SS
4864 break;
4865 case DW_TAG_enumeration_type:
134d01f1 4866 process_enumeration_scope (die, cu);
c906108c 4867 break;
134d01f1 4868
f792889a
DJ
4869 /* These dies have a type, but processing them does not create
4870 a symbol or recurse to process the children. Therefore we can
4871 read them on-demand through read_type_die. */
c906108c 4872 case DW_TAG_subroutine_type:
72019c9c 4873 case DW_TAG_set_type:
c906108c 4874 case DW_TAG_array_type:
c906108c 4875 case DW_TAG_pointer_type:
c906108c 4876 case DW_TAG_ptr_to_member_type:
c906108c 4877 case DW_TAG_reference_type:
c906108c 4878 case DW_TAG_string_type:
c906108c 4879 break;
134d01f1 4880
c906108c 4881 case DW_TAG_base_type:
a02abb62 4882 case DW_TAG_subrange_type:
cb249c71 4883 case DW_TAG_typedef:
134d01f1
DJ
4884 /* Add a typedef symbol for the type definition, if it has a
4885 DW_AT_name. */
f792889a 4886 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4887 break;
c906108c 4888 case DW_TAG_common_block:
e7c27a73 4889 read_common_block (die, cu);
c906108c
SS
4890 break;
4891 case DW_TAG_common_inclusion:
4892 break;
d9fa45fe 4893 case DW_TAG_namespace:
63d06c5c 4894 processing_has_namespace_info = 1;
e7c27a73 4895 read_namespace (die, cu);
d9fa45fe 4896 break;
5d7cb8df 4897 case DW_TAG_module:
f55ee35c 4898 processing_has_namespace_info = 1;
5d7cb8df
JK
4899 read_module (die, cu);
4900 break;
d9fa45fe
DC
4901 case DW_TAG_imported_declaration:
4902 case DW_TAG_imported_module:
63d06c5c 4903 processing_has_namespace_info = 1;
27aa8d6a
SW
4904 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4905 || cu->language != language_fortran))
4906 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4907 dwarf_tag_name (die->tag));
4908 read_import_statement (die, cu);
d9fa45fe 4909 break;
c906108c 4910 default:
e7c27a73 4911 new_symbol (die, NULL, cu);
c906108c
SS
4912 break;
4913 }
4914}
4915
94af9270
KS
4916/* A helper function for dwarf2_compute_name which determines whether DIE
4917 needs to have the name of the scope prepended to the name listed in the
4918 die. */
4919
4920static int
4921die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4922{
1c809c68
TT
4923 struct attribute *attr;
4924
94af9270
KS
4925 switch (die->tag)
4926 {
4927 case DW_TAG_namespace:
4928 case DW_TAG_typedef:
4929 case DW_TAG_class_type:
4930 case DW_TAG_interface_type:
4931 case DW_TAG_structure_type:
4932 case DW_TAG_union_type:
4933 case DW_TAG_enumeration_type:
4934 case DW_TAG_enumerator:
4935 case DW_TAG_subprogram:
4936 case DW_TAG_member:
4937 return 1;
4938
4939 case DW_TAG_variable:
c2b0a229 4940 case DW_TAG_constant:
94af9270
KS
4941 /* We only need to prefix "globally" visible variables. These include
4942 any variable marked with DW_AT_external or any variable that
4943 lives in a namespace. [Variables in anonymous namespaces
4944 require prefixing, but they are not DW_AT_external.] */
4945
4946 if (dwarf2_attr (die, DW_AT_specification, cu))
4947 {
4948 struct dwarf2_cu *spec_cu = cu;
9a619af0 4949
94af9270
KS
4950 return die_needs_namespace (die_specification (die, &spec_cu),
4951 spec_cu);
4952 }
4953
1c809c68 4954 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4955 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4956 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4957 return 0;
4958 /* A variable in a lexical block of some kind does not need a
4959 namespace, even though in C++ such variables may be external
4960 and have a mangled name. */
4961 if (die->parent->tag == DW_TAG_lexical_block
4962 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4963 || die->parent->tag == DW_TAG_catch_block
4964 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4965 return 0;
4966 return 1;
94af9270
KS
4967
4968 default:
4969 return 0;
4970 }
4971}
4972
98bfdba5
PA
4973/* Retrieve the last character from a mem_file. */
4974
4975static void
4976do_ui_file_peek_last (void *object, const char *buffer, long length)
4977{
4978 char *last_char_p = (char *) object;
4979
4980 if (length > 0)
4981 *last_char_p = buffer[length - 1];
4982}
4983
94af9270
KS
4984/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4985 compute the physname for the object, which include a method's
4986 formal parameters (C++/Java) and return type (Java).
4987
af6b7be1
JB
4988 For Ada, return the DIE's linkage name rather than the fully qualified
4989 name. PHYSNAME is ignored..
4990
94af9270
KS
4991 The result is allocated on the objfile_obstack and canonicalized. */
4992
4993static const char *
4994dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4995 int physname)
4996{
4997 if (name == NULL)
4998 name = dwarf2_name (die, cu);
4999
f55ee35c
JK
5000 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5001 compute it by typename_concat inside GDB. */
5002 if (cu->language == language_ada
5003 || (cu->language == language_fortran && physname))
5004 {
5005 /* For Ada unit, we prefer the linkage name over the name, as
5006 the former contains the exported name, which the user expects
5007 to be able to reference. Ideally, we want the user to be able
5008 to reference this entity using either natural or linkage name,
5009 but we haven't started looking at this enhancement yet. */
5010 struct attribute *attr;
5011
5012 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5013 if (attr == NULL)
5014 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5015 if (attr && DW_STRING (attr))
5016 return DW_STRING (attr);
5017 }
5018
94af9270
KS
5019 /* These are the only languages we know how to qualify names in. */
5020 if (name != NULL
f55ee35c
JK
5021 && (cu->language == language_cplus || cu->language == language_java
5022 || cu->language == language_fortran))
94af9270
KS
5023 {
5024 if (die_needs_namespace (die, cu))
5025 {
5026 long length;
5027 char *prefix;
5028 struct ui_file *buf;
5029
5030 prefix = determine_prefix (die, cu);
5031 buf = mem_fileopen ();
5032 if (*prefix != '\0')
5033 {
f55ee35c
JK
5034 char *prefixed_name = typename_concat (NULL, prefix, name,
5035 physname, cu);
9a619af0 5036
94af9270
KS
5037 fputs_unfiltered (prefixed_name, buf);
5038 xfree (prefixed_name);
5039 }
5040 else
62d5b8da 5041 fputs_unfiltered (name, buf);
94af9270 5042
98bfdba5
PA
5043 /* Template parameters may be specified in the DIE's DW_AT_name, or
5044 as children with DW_TAG_template_type_param or
5045 DW_TAG_value_type_param. If the latter, add them to the name
5046 here. If the name already has template parameters, then
5047 skip this step; some versions of GCC emit both, and
5048 it is more efficient to use the pre-computed name.
5049
5050 Something to keep in mind about this process: it is very
5051 unlikely, or in some cases downright impossible, to produce
5052 something that will match the mangled name of a function.
5053 If the definition of the function has the same debug info,
5054 we should be able to match up with it anyway. But fallbacks
5055 using the minimal symbol, for instance to find a method
5056 implemented in a stripped copy of libstdc++, will not work.
5057 If we do not have debug info for the definition, we will have to
5058 match them up some other way.
5059
5060 When we do name matching there is a related problem with function
5061 templates; two instantiated function templates are allowed to
5062 differ only by their return types, which we do not add here. */
5063
5064 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5065 {
5066 struct attribute *attr;
5067 struct die_info *child;
5068 int first = 1;
5069
5070 die->building_fullname = 1;
5071
5072 for (child = die->child; child != NULL; child = child->sibling)
5073 {
5074 struct type *type;
5075 long value;
5076 gdb_byte *bytes;
5077 struct dwarf2_locexpr_baton *baton;
5078 struct value *v;
5079
5080 if (child->tag != DW_TAG_template_type_param
5081 && child->tag != DW_TAG_template_value_param)
5082 continue;
5083
5084 if (first)
5085 {
5086 fputs_unfiltered ("<", buf);
5087 first = 0;
5088 }
5089 else
5090 fputs_unfiltered (", ", buf);
5091
5092 attr = dwarf2_attr (child, DW_AT_type, cu);
5093 if (attr == NULL)
5094 {
5095 complaint (&symfile_complaints,
5096 _("template parameter missing DW_AT_type"));
5097 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5098 continue;
5099 }
5100 type = die_type (child, cu);
5101
5102 if (child->tag == DW_TAG_template_type_param)
5103 {
5104 c_print_type (type, "", buf, -1, 0);
5105 continue;
5106 }
5107
5108 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5109 if (attr == NULL)
5110 {
5111 complaint (&symfile_complaints,
3e43a32a
MS
5112 _("template parameter missing "
5113 "DW_AT_const_value"));
98bfdba5
PA
5114 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5115 continue;
5116 }
5117
5118 dwarf2_const_value_attr (attr, type, name,
5119 &cu->comp_unit_obstack, cu,
5120 &value, &bytes, &baton);
5121
5122 if (TYPE_NOSIGN (type))
5123 /* GDB prints characters as NUMBER 'CHAR'. If that's
5124 changed, this can use value_print instead. */
5125 c_printchar (value, type, buf);
5126 else
5127 {
5128 struct value_print_options opts;
5129
5130 if (baton != NULL)
5131 v = dwarf2_evaluate_loc_desc (type, NULL,
5132 baton->data,
5133 baton->size,
5134 baton->per_cu);
5135 else if (bytes != NULL)
5136 {
5137 v = allocate_value (type);
5138 memcpy (value_contents_writeable (v), bytes,
5139 TYPE_LENGTH (type));
5140 }
5141 else
5142 v = value_from_longest (type, value);
5143
3e43a32a
MS
5144 /* Specify decimal so that we do not depend on
5145 the radix. */
98bfdba5
PA
5146 get_formatted_print_options (&opts, 'd');
5147 opts.raw = 1;
5148 value_print (v, buf, &opts);
5149 release_value (v);
5150 value_free (v);
5151 }
5152 }
5153
5154 die->building_fullname = 0;
5155
5156 if (!first)
5157 {
5158 /* Close the argument list, with a space if necessary
5159 (nested templates). */
5160 char last_char = '\0';
5161 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5162 if (last_char == '>')
5163 fputs_unfiltered (" >", buf);
5164 else
5165 fputs_unfiltered (">", buf);
5166 }
5167 }
5168
94af9270
KS
5169 /* For Java and C++ methods, append formal parameter type
5170 information, if PHYSNAME. */
6e70227d 5171
94af9270
KS
5172 if (physname && die->tag == DW_TAG_subprogram
5173 && (cu->language == language_cplus
5174 || cu->language == language_java))
5175 {
5176 struct type *type = read_type_die (die, cu);
5177
3167638f 5178 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5179
5180 if (cu->language == language_java)
5181 {
5182 /* For java, we must append the return type to method
0963b4bd 5183 names. */
94af9270
KS
5184 if (die->tag == DW_TAG_subprogram)
5185 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5186 0, 0);
5187 }
5188 else if (cu->language == language_cplus)
5189 {
60430eff
DJ
5190 /* Assume that an artificial first parameter is
5191 "this", but do not crash if it is not. RealView
5192 marks unnamed (and thus unused) parameters as
5193 artificial; there is no way to differentiate
5194 the two cases. */
94af9270
KS
5195 if (TYPE_NFIELDS (type) > 0
5196 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5197 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5198 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5199 0))))
94af9270
KS
5200 fputs_unfiltered (" const", buf);
5201 }
5202 }
5203
5204 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5205 &length);
5206 ui_file_delete (buf);
5207
5208 if (cu->language == language_cplus)
5209 {
5210 char *cname
5211 = dwarf2_canonicalize_name (name, cu,
5212 &cu->objfile->objfile_obstack);
9a619af0 5213
94af9270
KS
5214 if (cname != NULL)
5215 name = cname;
5216 }
5217 }
5218 }
5219
5220 return name;
5221}
5222
0114d602
DJ
5223/* Return the fully qualified name of DIE, based on its DW_AT_name.
5224 If scope qualifiers are appropriate they will be added. The result
5225 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5226 not have a name. NAME may either be from a previous call to
5227 dwarf2_name or NULL.
5228
0963b4bd 5229 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5230
5231static const char *
94af9270 5232dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5233{
94af9270
KS
5234 return dwarf2_compute_name (name, die, cu, 0);
5235}
0114d602 5236
94af9270
KS
5237/* Construct a physname for the given DIE in CU. NAME may either be
5238 from a previous call to dwarf2_name or NULL. The result will be
5239 allocated on the objfile_objstack or NULL if the DIE does not have a
5240 name.
0114d602 5241
94af9270 5242 The output string will be canonicalized (if C++/Java). */
0114d602 5243
94af9270
KS
5244static const char *
5245dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5246{
900e11f9
JK
5247 struct attribute *attr;
5248 const char *retval, *mangled = NULL, *canon = NULL;
5249 struct cleanup *back_to;
5250 int need_copy = 1;
5251
5252 /* In this case dwarf2_compute_name is just a shortcut not building anything
5253 on its own. */
5254 if (!die_needs_namespace (die, cu))
5255 return dwarf2_compute_name (name, die, cu, 1);
5256
5257 back_to = make_cleanup (null_cleanup, NULL);
5258
5259 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5260 if (!attr)
5261 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5262
5263 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5264 has computed. */
5265 if (attr && DW_STRING (attr))
5266 {
5267 char *demangled;
5268
5269 mangled = DW_STRING (attr);
5270
5271 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5272 type. It is easier for GDB users to search for such functions as
5273 `name(params)' than `long name(params)'. In such case the minimal
5274 symbol names do not match the full symbol names but for template
5275 functions there is never a need to look up their definition from their
5276 declaration so the only disadvantage remains the minimal symbol
5277 variant `long name(params)' does not have the proper inferior type.
5278 */
5279
5280 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5281 | (cu->language == language_java
5282 ? DMGL_JAVA | DMGL_RET_POSTFIX
5283 : DMGL_RET_DROP)));
5284 if (demangled)
5285 {
5286 make_cleanup (xfree, demangled);
5287 canon = demangled;
5288 }
5289 else
5290 {
5291 canon = mangled;
5292 need_copy = 0;
5293 }
5294 }
5295
5296 if (canon == NULL || check_physname)
5297 {
5298 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5299
5300 if (canon != NULL && strcmp (physname, canon) != 0)
5301 {
5302 /* It may not mean a bug in GDB. The compiler could also
5303 compute DW_AT_linkage_name incorrectly. But in such case
5304 GDB would need to be bug-to-bug compatible. */
5305
5306 complaint (&symfile_complaints,
5307 _("Computed physname <%s> does not match demangled <%s> "
5308 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5309 physname, canon, mangled, die->offset, cu->objfile->name);
5310
5311 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5312 is available here - over computed PHYSNAME. It is safer
5313 against both buggy GDB and buggy compilers. */
5314
5315 retval = canon;
5316 }
5317 else
5318 {
5319 retval = physname;
5320 need_copy = 0;
5321 }
5322 }
5323 else
5324 retval = canon;
5325
5326 if (need_copy)
5327 retval = obsavestring (retval, strlen (retval),
5328 &cu->objfile->objfile_obstack);
5329
5330 do_cleanups (back_to);
5331 return retval;
0114d602
DJ
5332}
5333
27aa8d6a
SW
5334/* Read the import statement specified by the given die and record it. */
5335
5336static void
5337read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5338{
5339 struct attribute *import_attr;
32019081 5340 struct die_info *imported_die, *child_die;
de4affc9 5341 struct dwarf2_cu *imported_cu;
27aa8d6a 5342 const char *imported_name;
794684b6 5343 const char *imported_name_prefix;
13387711
SW
5344 const char *canonical_name;
5345 const char *import_alias;
5346 const char *imported_declaration = NULL;
794684b6 5347 const char *import_prefix;
32019081
JK
5348 VEC (const_char_ptr) *excludes = NULL;
5349 struct cleanup *cleanups;
13387711
SW
5350
5351 char *temp;
27aa8d6a
SW
5352
5353 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5354 if (import_attr == NULL)
5355 {
5356 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5357 dwarf_tag_name (die->tag));
5358 return;
5359 }
5360
de4affc9
CC
5361 imported_cu = cu;
5362 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5363 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5364 if (imported_name == NULL)
5365 {
5366 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5367
5368 The import in the following code:
5369 namespace A
5370 {
5371 typedef int B;
5372 }
5373
5374 int main ()
5375 {
5376 using A::B;
5377 B b;
5378 return b;
5379 }
5380
5381 ...
5382 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5383 <52> DW_AT_decl_file : 1
5384 <53> DW_AT_decl_line : 6
5385 <54> DW_AT_import : <0x75>
5386 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5387 <59> DW_AT_name : B
5388 <5b> DW_AT_decl_file : 1
5389 <5c> DW_AT_decl_line : 2
5390 <5d> DW_AT_type : <0x6e>
5391 ...
5392 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5393 <76> DW_AT_byte_size : 4
5394 <77> DW_AT_encoding : 5 (signed)
5395
5396 imports the wrong die ( 0x75 instead of 0x58 ).
5397 This case will be ignored until the gcc bug is fixed. */
5398 return;
5399 }
5400
82856980
SW
5401 /* Figure out the local name after import. */
5402 import_alias = dwarf2_name (die, cu);
27aa8d6a 5403
794684b6
SW
5404 /* Figure out where the statement is being imported to. */
5405 import_prefix = determine_prefix (die, cu);
5406
5407 /* Figure out what the scope of the imported die is and prepend it
5408 to the name of the imported die. */
de4affc9 5409 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5410
f55ee35c
JK
5411 if (imported_die->tag != DW_TAG_namespace
5412 && imported_die->tag != DW_TAG_module)
794684b6 5413 {
13387711
SW
5414 imported_declaration = imported_name;
5415 canonical_name = imported_name_prefix;
794684b6 5416 }
13387711 5417 else if (strlen (imported_name_prefix) > 0)
794684b6 5418 {
13387711
SW
5419 temp = alloca (strlen (imported_name_prefix)
5420 + 2 + strlen (imported_name) + 1);
5421 strcpy (temp, imported_name_prefix);
5422 strcat (temp, "::");
5423 strcat (temp, imported_name);
5424 canonical_name = temp;
794684b6 5425 }
13387711
SW
5426 else
5427 canonical_name = imported_name;
794684b6 5428
32019081
JK
5429 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5430
5431 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5432 for (child_die = die->child; child_die && child_die->tag;
5433 child_die = sibling_die (child_die))
5434 {
5435 /* DWARF-4: A Fortran use statement with a “rename list” may be
5436 represented by an imported module entry with an import attribute
5437 referring to the module and owned entries corresponding to those
5438 entities that are renamed as part of being imported. */
5439
5440 if (child_die->tag != DW_TAG_imported_declaration)
5441 {
5442 complaint (&symfile_complaints,
5443 _("child DW_TAG_imported_declaration expected "
5444 "- DIE at 0x%x [in module %s]"),
5445 child_die->offset, cu->objfile->name);
5446 continue;
5447 }
5448
5449 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5450 if (import_attr == NULL)
5451 {
5452 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5453 dwarf_tag_name (child_die->tag));
5454 continue;
5455 }
5456
5457 imported_cu = cu;
5458 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5459 &imported_cu);
5460 imported_name = dwarf2_name (imported_die, imported_cu);
5461 if (imported_name == NULL)
5462 {
5463 complaint (&symfile_complaints,
5464 _("child DW_TAG_imported_declaration has unknown "
5465 "imported name - DIE at 0x%x [in module %s]"),
5466 child_die->offset, cu->objfile->name);
5467 continue;
5468 }
5469
5470 VEC_safe_push (const_char_ptr, excludes, imported_name);
5471
5472 process_die (child_die, cu);
5473 }
5474
c0cc3a76
SW
5475 cp_add_using_directive (import_prefix,
5476 canonical_name,
5477 import_alias,
13387711 5478 imported_declaration,
32019081 5479 excludes,
c0cc3a76 5480 &cu->objfile->objfile_obstack);
32019081
JK
5481
5482 do_cleanups (cleanups);
27aa8d6a
SW
5483}
5484
5fb290d7 5485static void
e142c38c 5486initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5487{
e142c38c 5488 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5489}
5490
ae2de4f8
DE
5491/* Cleanup function for read_file_scope. */
5492
cb1df416
DJ
5493static void
5494free_cu_line_header (void *arg)
5495{
5496 struct dwarf2_cu *cu = arg;
5497
5498 free_line_header (cu->line_header);
5499 cu->line_header = NULL;
5500}
5501
9291a0cd
TT
5502static void
5503find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5504 char **name, char **comp_dir)
5505{
5506 struct attribute *attr;
5507
5508 *name = NULL;
5509 *comp_dir = NULL;
5510
5511 /* Find the filename. Do not use dwarf2_name here, since the filename
5512 is not a source language identifier. */
5513 attr = dwarf2_attr (die, DW_AT_name, cu);
5514 if (attr)
5515 {
5516 *name = DW_STRING (attr);
5517 }
5518
5519 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5520 if (attr)
5521 *comp_dir = DW_STRING (attr);
5522 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5523 {
5524 *comp_dir = ldirname (*name);
5525 if (*comp_dir != NULL)
5526 make_cleanup (xfree, *comp_dir);
5527 }
5528 if (*comp_dir != NULL)
5529 {
5530 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5531 directory, get rid of it. */
5532 char *cp = strchr (*comp_dir, ':');
5533
5534 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5535 *comp_dir = cp + 1;
5536 }
5537
5538 if (*name == NULL)
5539 *name = "<unknown>";
5540}
5541
2ab95328
TT
5542/* Handle DW_AT_stmt_list for a compilation unit. */
5543
5544static void
5545handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5546 const char *comp_dir)
5547{
5548 struct attribute *attr;
5549 struct objfile *objfile = cu->objfile;
5550 bfd *abfd = objfile->obfd;
5551
5552 /* Decode line number information if present. We do this before
5553 processing child DIEs, so that the line header table is available
5554 for DW_AT_decl_file. */
5555 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5556 if (attr)
5557 {
5558 unsigned int line_offset = DW_UNSND (attr);
5559 struct line_header *line_header
5560 = dwarf_decode_line_header (line_offset, abfd, cu);
5561
5562 if (line_header)
5563 {
5564 cu->line_header = line_header;
5565 make_cleanup (free_cu_line_header, cu);
5566 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5567 }
5568 }
5569}
5570
ae2de4f8
DE
5571/* Process DW_TAG_compile_unit. */
5572
c906108c 5573static void
e7c27a73 5574read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5575{
e7c27a73 5576 struct objfile *objfile = cu->objfile;
debd256d 5577 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5578 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5579 CORE_ADDR highpc = ((CORE_ADDR) 0);
5580 struct attribute *attr;
e1024ff1 5581 char *name = NULL;
c906108c
SS
5582 char *comp_dir = NULL;
5583 struct die_info *child_die;
5584 bfd *abfd = objfile->obfd;
e142c38c 5585 CORE_ADDR baseaddr;
6e70227d 5586
e142c38c 5587 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5588
fae299cd 5589 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5590
5591 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5592 from finish_block. */
2acceee2 5593 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5594 lowpc = highpc;
5595 lowpc += baseaddr;
5596 highpc += baseaddr;
5597
9291a0cd 5598 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5599
e142c38c 5600 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5601 if (attr)
5602 {
e142c38c 5603 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5604 }
5605
b0f35d58 5606 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5607 if (attr)
b0f35d58 5608 cu->producer = DW_STRING (attr);
303b6f5d 5609
f4b8a18d
KW
5610 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5611 standardised yet. As a workaround for the language detection we fall
5612 back to the DW_AT_producer string. */
5613 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5614 cu->language = language_opencl;
5615
0963b4bd 5616 /* We assume that we're processing GCC output. */
c906108c 5617 processing_gcc_compilation = 2;
c906108c 5618
df8a16a1
DJ
5619 processing_has_namespace_info = 0;
5620
c906108c
SS
5621 start_symtab (name, comp_dir, lowpc);
5622 record_debugformat ("DWARF 2");
303b6f5d 5623 record_producer (cu->producer);
c906108c 5624
e142c38c 5625 initialize_cu_func_list (cu);
c906108c 5626
2ab95328 5627 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5628
cb1df416
DJ
5629 /* Process all dies in compilation unit. */
5630 if (die->child != NULL)
5631 {
5632 child_die = die->child;
5633 while (child_die && child_die->tag)
5634 {
5635 process_die (child_die, cu);
5636 child_die = sibling_die (child_die);
5637 }
5638 }
5639
2e276125
JB
5640 /* Decode macro information, if present. Dwarf 2 macro information
5641 refers to information in the line number info statement program
5642 header, so we can only read it if we've read the header
5643 successfully. */
e142c38c 5644 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
2ab95328 5645 if (attr && cu->line_header)
2e276125
JB
5646 {
5647 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5648
2ab95328 5649 dwarf_decode_macros (cu->line_header, macro_offset,
e7c27a73 5650 comp_dir, abfd, cu);
2e276125 5651 }
debd256d 5652 do_cleanups (back_to);
5fb290d7
DJ
5653}
5654
ae2de4f8
DE
5655/* Process DW_TAG_type_unit.
5656 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5657 actual type being defined by this TU. In this case the first top
5658 level sibling is there to provide context only. */
5659
5660static void
5661read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5662{
5663 struct objfile *objfile = cu->objfile;
5664 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5665 CORE_ADDR lowpc;
5666 struct attribute *attr;
5667 char *name = NULL;
5668 char *comp_dir = NULL;
5669 struct die_info *child_die;
5670 bfd *abfd = objfile->obfd;
348e048f
DE
5671
5672 /* start_symtab needs a low pc, but we don't really have one.
5673 Do what read_file_scope would do in the absence of such info. */
5674 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 name = DW_STRING (attr);
5681
5682 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5683 if (attr)
5684 comp_dir = DW_STRING (attr);
5685 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5686 {
5687 comp_dir = ldirname (name);
5688 if (comp_dir != NULL)
5689 make_cleanup (xfree, comp_dir);
5690 }
5691
5692 if (name == NULL)
5693 name = "<unknown>";
5694
5695 attr = dwarf2_attr (die, DW_AT_language, cu);
5696 if (attr)
5697 set_cu_language (DW_UNSND (attr), cu);
5698
5699 /* This isn't technically needed today. It is done for symmetry
5700 with read_file_scope. */
5701 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5702 if (attr)
348e048f
DE
5703 cu->producer = DW_STRING (attr);
5704
0963b4bd 5705 /* We assume that we're processing GCC output. */
348e048f
DE
5706 processing_gcc_compilation = 2;
5707
5708 processing_has_namespace_info = 0;
5709
5710 start_symtab (name, comp_dir, lowpc);
5711 record_debugformat ("DWARF 2");
5712 record_producer (cu->producer);
5713
2ab95328
TT
5714 handle_DW_AT_stmt_list (die, cu, comp_dir);
5715
348e048f
DE
5716 /* Process the dies in the type unit. */
5717 if (die->child == NULL)
5718 {
5719 dump_die_for_error (die);
5720 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5721 bfd_get_filename (abfd));
5722 }
5723
5724 child_die = die->child;
5725
5726 while (child_die && child_die->tag)
5727 {
5728 process_die (child_die, cu);
5729
5730 child_die = sibling_die (child_die);
5731 }
5732
5733 do_cleanups (back_to);
5734}
5735
5fb290d7 5736static void
e142c38c
DJ
5737add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5738 struct dwarf2_cu *cu)
5fb290d7
DJ
5739{
5740 struct function_range *thisfn;
5741
5742 thisfn = (struct function_range *)
7b5a2f43 5743 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5744 thisfn->name = name;
5745 thisfn->lowpc = lowpc;
5746 thisfn->highpc = highpc;
5747 thisfn->seen_line = 0;
5748 thisfn->next = NULL;
5749
e142c38c
DJ
5750 if (cu->last_fn == NULL)
5751 cu->first_fn = thisfn;
5fb290d7 5752 else
e142c38c 5753 cu->last_fn->next = thisfn;
5fb290d7 5754
e142c38c 5755 cu->last_fn = thisfn;
c906108c
SS
5756}
5757
d389af10
JK
5758/* qsort helper for inherit_abstract_dies. */
5759
5760static int
5761unsigned_int_compar (const void *ap, const void *bp)
5762{
5763 unsigned int a = *(unsigned int *) ap;
5764 unsigned int b = *(unsigned int *) bp;
5765
5766 return (a > b) - (b > a);
5767}
5768
5769/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5770 Inherit only the children of the DW_AT_abstract_origin DIE not being
5771 already referenced by DW_AT_abstract_origin from the children of the
5772 current DIE. */
d389af10
JK
5773
5774static void
5775inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5776{
5777 struct die_info *child_die;
5778 unsigned die_children_count;
5779 /* CU offsets which were referenced by children of the current DIE. */
5780 unsigned *offsets;
5781 unsigned *offsets_end, *offsetp;
5782 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5783 struct die_info *origin_die;
5784 /* Iterator of the ORIGIN_DIE children. */
5785 struct die_info *origin_child_die;
5786 struct cleanup *cleanups;
5787 struct attribute *attr;
cd02d79d
PA
5788 struct dwarf2_cu *origin_cu;
5789 struct pending **origin_previous_list_in_scope;
d389af10
JK
5790
5791 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5792 if (!attr)
5793 return;
5794
cd02d79d
PA
5795 /* Note that following die references may follow to a die in a
5796 different cu. */
5797
5798 origin_cu = cu;
5799 origin_die = follow_die_ref (die, attr, &origin_cu);
5800
5801 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5802 symbols in. */
5803 origin_previous_list_in_scope = origin_cu->list_in_scope;
5804 origin_cu->list_in_scope = cu->list_in_scope;
5805
edb3359d
DJ
5806 if (die->tag != origin_die->tag
5807 && !(die->tag == DW_TAG_inlined_subroutine
5808 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5809 complaint (&symfile_complaints,
5810 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5811 die->offset, origin_die->offset);
5812
5813 child_die = die->child;
5814 die_children_count = 0;
5815 while (child_die && child_die->tag)
5816 {
5817 child_die = sibling_die (child_die);
5818 die_children_count++;
5819 }
5820 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5821 cleanups = make_cleanup (xfree, offsets);
5822
5823 offsets_end = offsets;
5824 child_die = die->child;
5825 while (child_die && child_die->tag)
5826 {
c38f313d
DJ
5827 /* For each CHILD_DIE, find the corresponding child of
5828 ORIGIN_DIE. If there is more than one layer of
5829 DW_AT_abstract_origin, follow them all; there shouldn't be,
5830 but GCC versions at least through 4.4 generate this (GCC PR
5831 40573). */
5832 struct die_info *child_origin_die = child_die;
cd02d79d 5833 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5834
c38f313d
DJ
5835 while (1)
5836 {
cd02d79d
PA
5837 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5838 child_origin_cu);
c38f313d
DJ
5839 if (attr == NULL)
5840 break;
cd02d79d
PA
5841 child_origin_die = follow_die_ref (child_origin_die, attr,
5842 &child_origin_cu);
c38f313d
DJ
5843 }
5844
d389af10
JK
5845 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5846 counterpart may exist. */
c38f313d 5847 if (child_origin_die != child_die)
d389af10 5848 {
edb3359d
DJ
5849 if (child_die->tag != child_origin_die->tag
5850 && !(child_die->tag == DW_TAG_inlined_subroutine
5851 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5852 complaint (&symfile_complaints,
5853 _("Child DIE 0x%x and its abstract origin 0x%x have "
5854 "different tags"), child_die->offset,
5855 child_origin_die->offset);
c38f313d
DJ
5856 if (child_origin_die->parent != origin_die)
5857 complaint (&symfile_complaints,
5858 _("Child DIE 0x%x and its abstract origin 0x%x have "
5859 "different parents"), child_die->offset,
5860 child_origin_die->offset);
5861 else
5862 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5863 }
5864 child_die = sibling_die (child_die);
5865 }
5866 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5867 unsigned_int_compar);
5868 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5869 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5870 complaint (&symfile_complaints,
5871 _("Multiple children of DIE 0x%x refer "
5872 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5873 die->offset, *offsetp);
5874
5875 offsetp = offsets;
5876 origin_child_die = origin_die->child;
5877 while (origin_child_die && origin_child_die->tag)
5878 {
5879 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5880 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5881 offsetp++;
5882 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5883 {
5884 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5885 process_die (origin_child_die, origin_cu);
d389af10
JK
5886 }
5887 origin_child_die = sibling_die (origin_child_die);
5888 }
cd02d79d 5889 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5890
5891 do_cleanups (cleanups);
5892}
5893
c906108c 5894static void
e7c27a73 5895read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5896{
e7c27a73 5897 struct objfile *objfile = cu->objfile;
52f0bd74 5898 struct context_stack *new;
c906108c
SS
5899 CORE_ADDR lowpc;
5900 CORE_ADDR highpc;
5901 struct die_info *child_die;
edb3359d 5902 struct attribute *attr, *call_line, *call_file;
c906108c 5903 char *name;
e142c38c 5904 CORE_ADDR baseaddr;
801e3a5b 5905 struct block *block;
edb3359d 5906 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5907 VEC (symbolp) *template_args = NULL;
5908 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5909
5910 if (inlined_func)
5911 {
5912 /* If we do not have call site information, we can't show the
5913 caller of this inlined function. That's too confusing, so
5914 only use the scope for local variables. */
5915 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5916 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5917 if (call_line == NULL || call_file == NULL)
5918 {
5919 read_lexical_block_scope (die, cu);
5920 return;
5921 }
5922 }
c906108c 5923
e142c38c
DJ
5924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5925
94af9270 5926 name = dwarf2_name (die, cu);
c906108c 5927
e8d05480
JB
5928 /* Ignore functions with missing or empty names. These are actually
5929 illegal according to the DWARF standard. */
5930 if (name == NULL)
5931 {
5932 complaint (&symfile_complaints,
5933 _("missing name for subprogram DIE at %d"), die->offset);
5934 return;
5935 }
5936
5937 /* Ignore functions with missing or invalid low and high pc attributes. */
5938 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5939 {
ae4d0c03
PM
5940 attr = dwarf2_attr (die, DW_AT_external, cu);
5941 if (!attr || !DW_UNSND (attr))
5942 complaint (&symfile_complaints,
3e43a32a
MS
5943 _("cannot get low and high bounds "
5944 "for subprogram DIE at %d"),
ae4d0c03 5945 die->offset);
e8d05480
JB
5946 return;
5947 }
c906108c
SS
5948
5949 lowpc += baseaddr;
5950 highpc += baseaddr;
5951
5fb290d7 5952 /* Record the function range for dwarf_decode_lines. */
e142c38c 5953 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5954
34eaf542
TT
5955 /* If we have any template arguments, then we must allocate a
5956 different sort of symbol. */
5957 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5958 {
5959 if (child_die->tag == DW_TAG_template_type_param
5960 || child_die->tag == DW_TAG_template_value_param)
5961 {
5962 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5963 struct template_symbol);
5964 templ_func->base.is_cplus_template_function = 1;
5965 break;
5966 }
5967 }
5968
c906108c 5969 new = push_context (0, lowpc);
34eaf542
TT
5970 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5971 (struct symbol *) templ_func);
4c2df51b 5972
4cecd739
DJ
5973 /* If there is a location expression for DW_AT_frame_base, record
5974 it. */
e142c38c 5975 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5976 if (attr)
c034e007
AC
5977 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5978 expression is being recorded directly in the function's symbol
5979 and not in a separate frame-base object. I guess this hack is
5980 to avoid adding some sort of frame-base adjunct/annex to the
5981 function's symbol :-(. The problem with doing this is that it
5982 results in a function symbol with a location expression that
5983 has nothing to do with the location of the function, ouch! The
5984 relationship should be: a function's symbol has-a frame base; a
5985 frame-base has-a location expression. */
e7c27a73 5986 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5987
e142c38c 5988 cu->list_in_scope = &local_symbols;
c906108c 5989
639d11d3 5990 if (die->child != NULL)
c906108c 5991 {
639d11d3 5992 child_die = die->child;
c906108c
SS
5993 while (child_die && child_die->tag)
5994 {
34eaf542
TT
5995 if (child_die->tag == DW_TAG_template_type_param
5996 || child_die->tag == DW_TAG_template_value_param)
5997 {
5998 struct symbol *arg = new_symbol (child_die, NULL, cu);
5999
f1078f66
DJ
6000 if (arg != NULL)
6001 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6002 }
6003 else
6004 process_die (child_die, cu);
c906108c
SS
6005 child_die = sibling_die (child_die);
6006 }
6007 }
6008
d389af10
JK
6009 inherit_abstract_dies (die, cu);
6010
4a811a97
UW
6011 /* If we have a DW_AT_specification, we might need to import using
6012 directives from the context of the specification DIE. See the
6013 comment in determine_prefix. */
6014 if (cu->language == language_cplus
6015 && dwarf2_attr (die, DW_AT_specification, cu))
6016 {
6017 struct dwarf2_cu *spec_cu = cu;
6018 struct die_info *spec_die = die_specification (die, &spec_cu);
6019
6020 while (spec_die)
6021 {
6022 child_die = spec_die->child;
6023 while (child_die && child_die->tag)
6024 {
6025 if (child_die->tag == DW_TAG_imported_module)
6026 process_die (child_die, spec_cu);
6027 child_die = sibling_die (child_die);
6028 }
6029
6030 /* In some cases, GCC generates specification DIEs that
6031 themselves contain DW_AT_specification attributes. */
6032 spec_die = die_specification (spec_die, &spec_cu);
6033 }
6034 }
6035
c906108c
SS
6036 new = pop_context ();
6037 /* Make a block for the local symbols within. */
801e3a5b
JB
6038 block = finish_block (new->name, &local_symbols, new->old_blocks,
6039 lowpc, highpc, objfile);
6040
df8a16a1 6041 /* For C++, set the block's scope. */
f55ee35c 6042 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6043 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6044 determine_prefix (die, cu),
df8a16a1
DJ
6045 processing_has_namespace_info);
6046
801e3a5b
JB
6047 /* If we have address ranges, record them. */
6048 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6049
34eaf542
TT
6050 /* Attach template arguments to function. */
6051 if (! VEC_empty (symbolp, template_args))
6052 {
6053 gdb_assert (templ_func != NULL);
6054
6055 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6056 templ_func->template_arguments
6057 = obstack_alloc (&objfile->objfile_obstack,
6058 (templ_func->n_template_arguments
6059 * sizeof (struct symbol *)));
6060 memcpy (templ_func->template_arguments,
6061 VEC_address (symbolp, template_args),
6062 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6063 VEC_free (symbolp, template_args);
6064 }
6065
208d8187
JB
6066 /* In C++, we can have functions nested inside functions (e.g., when
6067 a function declares a class that has methods). This means that
6068 when we finish processing a function scope, we may need to go
6069 back to building a containing block's symbol lists. */
6070 local_symbols = new->locals;
6071 param_symbols = new->params;
27aa8d6a 6072 using_directives = new->using_directives;
208d8187 6073
921e78cf
JB
6074 /* If we've finished processing a top-level function, subsequent
6075 symbols go in the file symbol list. */
6076 if (outermost_context_p ())
e142c38c 6077 cu->list_in_scope = &file_symbols;
c906108c
SS
6078}
6079
6080/* Process all the DIES contained within a lexical block scope. Start
6081 a new scope, process the dies, and then close the scope. */
6082
6083static void
e7c27a73 6084read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6085{
e7c27a73 6086 struct objfile *objfile = cu->objfile;
52f0bd74 6087 struct context_stack *new;
c906108c
SS
6088 CORE_ADDR lowpc, highpc;
6089 struct die_info *child_die;
e142c38c
DJ
6090 CORE_ADDR baseaddr;
6091
6092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6093
6094 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6095 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6096 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6097 be nasty. Might be easier to properly extend generic blocks to
af34e669 6098 describe ranges. */
d85a05f0 6099 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6100 return;
6101 lowpc += baseaddr;
6102 highpc += baseaddr;
6103
6104 push_context (0, lowpc);
639d11d3 6105 if (die->child != NULL)
c906108c 6106 {
639d11d3 6107 child_die = die->child;
c906108c
SS
6108 while (child_die && child_die->tag)
6109 {
e7c27a73 6110 process_die (child_die, cu);
c906108c
SS
6111 child_die = sibling_die (child_die);
6112 }
6113 }
6114 new = pop_context ();
6115
8540c487 6116 if (local_symbols != NULL || using_directives != NULL)
c906108c 6117 {
801e3a5b
JB
6118 struct block *block
6119 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6120 highpc, objfile);
6121
6122 /* Note that recording ranges after traversing children, as we
6123 do here, means that recording a parent's ranges entails
6124 walking across all its children's ranges as they appear in
6125 the address map, which is quadratic behavior.
6126
6127 It would be nicer to record the parent's ranges before
6128 traversing its children, simply overriding whatever you find
6129 there. But since we don't even decide whether to create a
6130 block until after we've traversed its children, that's hard
6131 to do. */
6132 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6133 }
6134 local_symbols = new->locals;
27aa8d6a 6135 using_directives = new->using_directives;
c906108c
SS
6136}
6137
43039443 6138/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6139 Return 1 if the attributes are present and valid, otherwise, return 0.
6140 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6141
6142static int
6143dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6144 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6145 struct partial_symtab *ranges_pst)
43039443
JK
6146{
6147 struct objfile *objfile = cu->objfile;
6148 struct comp_unit_head *cu_header = &cu->header;
6149 bfd *obfd = objfile->obfd;
6150 unsigned int addr_size = cu_header->addr_size;
6151 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6152 /* Base address selection entry. */
6153 CORE_ADDR base;
6154 int found_base;
6155 unsigned int dummy;
6156 gdb_byte *buffer;
6157 CORE_ADDR marker;
6158 int low_set;
6159 CORE_ADDR low = 0;
6160 CORE_ADDR high = 0;
ff013f42 6161 CORE_ADDR baseaddr;
43039443 6162
d00adf39
DE
6163 found_base = cu->base_known;
6164 base = cu->base_address;
43039443 6165
be391dca 6166 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6167 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6168 {
6169 complaint (&symfile_complaints,
6170 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6171 offset);
6172 return 0;
6173 }
dce234bc 6174 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6175
6176 /* Read in the largest possible address. */
6177 marker = read_address (obfd, buffer, cu, &dummy);
6178 if ((marker & mask) == mask)
6179 {
6180 /* If we found the largest possible address, then
6181 read the base address. */
6182 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6183 buffer += 2 * addr_size;
6184 offset += 2 * addr_size;
6185 found_base = 1;
6186 }
6187
6188 low_set = 0;
6189
e7030f15 6190 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6191
43039443
JK
6192 while (1)
6193 {
6194 CORE_ADDR range_beginning, range_end;
6195
6196 range_beginning = read_address (obfd, buffer, cu, &dummy);
6197 buffer += addr_size;
6198 range_end = read_address (obfd, buffer, cu, &dummy);
6199 buffer += addr_size;
6200 offset += 2 * addr_size;
6201
6202 /* An end of list marker is a pair of zero addresses. */
6203 if (range_beginning == 0 && range_end == 0)
6204 /* Found the end of list entry. */
6205 break;
6206
6207 /* Each base address selection entry is a pair of 2 values.
6208 The first is the largest possible address, the second is
6209 the base address. Check for a base address here. */
6210 if ((range_beginning & mask) == mask)
6211 {
6212 /* If we found the largest possible address, then
6213 read the base address. */
6214 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6215 found_base = 1;
6216 continue;
6217 }
6218
6219 if (!found_base)
6220 {
6221 /* We have no valid base address for the ranges
6222 data. */
6223 complaint (&symfile_complaints,
6224 _("Invalid .debug_ranges data (no base address)"));
6225 return 0;
6226 }
6227
9277c30c
UW
6228 if (range_beginning > range_end)
6229 {
6230 /* Inverted range entries are invalid. */
6231 complaint (&symfile_complaints,
6232 _("Invalid .debug_ranges data (inverted range)"));
6233 return 0;
6234 }
6235
6236 /* Empty range entries have no effect. */
6237 if (range_beginning == range_end)
6238 continue;
6239
43039443
JK
6240 range_beginning += base;
6241 range_end += base;
6242
9277c30c 6243 if (ranges_pst != NULL)
ff013f42 6244 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6245 range_beginning + baseaddr,
6246 range_end - 1 + baseaddr,
ff013f42
JK
6247 ranges_pst);
6248
43039443
JK
6249 /* FIXME: This is recording everything as a low-high
6250 segment of consecutive addresses. We should have a
6251 data structure for discontiguous block ranges
6252 instead. */
6253 if (! low_set)
6254 {
6255 low = range_beginning;
6256 high = range_end;
6257 low_set = 1;
6258 }
6259 else
6260 {
6261 if (range_beginning < low)
6262 low = range_beginning;
6263 if (range_end > high)
6264 high = range_end;
6265 }
6266 }
6267
6268 if (! low_set)
6269 /* If the first entry is an end-of-list marker, the range
6270 describes an empty scope, i.e. no instructions. */
6271 return 0;
6272
6273 if (low_return)
6274 *low_return = low;
6275 if (high_return)
6276 *high_return = high;
6277 return 1;
6278}
6279
af34e669
DJ
6280/* Get low and high pc attributes from a die. Return 1 if the attributes
6281 are present and valid, otherwise, return 0. Return -1 if the range is
6282 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6283static int
af34e669 6284dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6285 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6286 struct partial_symtab *pst)
c906108c
SS
6287{
6288 struct attribute *attr;
af34e669
DJ
6289 CORE_ADDR low = 0;
6290 CORE_ADDR high = 0;
6291 int ret = 0;
c906108c 6292
e142c38c 6293 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6294 if (attr)
af34e669
DJ
6295 {
6296 high = DW_ADDR (attr);
e142c38c 6297 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6298 if (attr)
6299 low = DW_ADDR (attr);
6300 else
6301 /* Found high w/o low attribute. */
6302 return 0;
6303
6304 /* Found consecutive range of addresses. */
6305 ret = 1;
6306 }
c906108c 6307 else
af34e669 6308 {
e142c38c 6309 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6310 if (attr != NULL)
6311 {
af34e669 6312 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6313 .debug_ranges section. */
d85a05f0 6314 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6315 return 0;
43039443 6316 /* Found discontinuous range of addresses. */
af34e669
DJ
6317 ret = -1;
6318 }
6319 }
c906108c 6320
9373cf26
JK
6321 /* read_partial_die has also the strict LOW < HIGH requirement. */
6322 if (high <= low)
c906108c
SS
6323 return 0;
6324
6325 /* When using the GNU linker, .gnu.linkonce. sections are used to
6326 eliminate duplicate copies of functions and vtables and such.
6327 The linker will arbitrarily choose one and discard the others.
6328 The AT_*_pc values for such functions refer to local labels in
6329 these sections. If the section from that file was discarded, the
6330 labels are not in the output, so the relocs get a value of 0.
6331 If this is a discarded function, mark the pc bounds as invalid,
6332 so that GDB will ignore it. */
72dca2f5 6333 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6334 return 0;
6335
6336 *lowpc = low;
6337 *highpc = high;
af34e669 6338 return ret;
c906108c
SS
6339}
6340
b084d499
JB
6341/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6342 its low and high PC addresses. Do nothing if these addresses could not
6343 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6344 and HIGHPC to the high address if greater than HIGHPC. */
6345
6346static void
6347dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6348 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6349 struct dwarf2_cu *cu)
6350{
6351 CORE_ADDR low, high;
6352 struct die_info *child = die->child;
6353
d85a05f0 6354 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6355 {
6356 *lowpc = min (*lowpc, low);
6357 *highpc = max (*highpc, high);
6358 }
6359
6360 /* If the language does not allow nested subprograms (either inside
6361 subprograms or lexical blocks), we're done. */
6362 if (cu->language != language_ada)
6363 return;
6e70227d 6364
b084d499
JB
6365 /* Check all the children of the given DIE. If it contains nested
6366 subprograms, then check their pc bounds. Likewise, we need to
6367 check lexical blocks as well, as they may also contain subprogram
6368 definitions. */
6369 while (child && child->tag)
6370 {
6371 if (child->tag == DW_TAG_subprogram
6372 || child->tag == DW_TAG_lexical_block)
6373 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6374 child = sibling_die (child);
6375 }
6376}
6377
fae299cd
DC
6378/* Get the low and high pc's represented by the scope DIE, and store
6379 them in *LOWPC and *HIGHPC. If the correct values can't be
6380 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6381
6382static void
6383get_scope_pc_bounds (struct die_info *die,
6384 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6385 struct dwarf2_cu *cu)
6386{
6387 CORE_ADDR best_low = (CORE_ADDR) -1;
6388 CORE_ADDR best_high = (CORE_ADDR) 0;
6389 CORE_ADDR current_low, current_high;
6390
d85a05f0 6391 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6392 {
6393 best_low = current_low;
6394 best_high = current_high;
6395 }
6396 else
6397 {
6398 struct die_info *child = die->child;
6399
6400 while (child && child->tag)
6401 {
6402 switch (child->tag) {
6403 case DW_TAG_subprogram:
b084d499 6404 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6405 break;
6406 case DW_TAG_namespace:
f55ee35c 6407 case DW_TAG_module:
fae299cd
DC
6408 /* FIXME: carlton/2004-01-16: Should we do this for
6409 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6410 that current GCC's always emit the DIEs corresponding
6411 to definitions of methods of classes as children of a
6412 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6413 the DIEs giving the declarations, which could be
6414 anywhere). But I don't see any reason why the
6415 standards says that they have to be there. */
6416 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6417
6418 if (current_low != ((CORE_ADDR) -1))
6419 {
6420 best_low = min (best_low, current_low);
6421 best_high = max (best_high, current_high);
6422 }
6423 break;
6424 default:
0963b4bd 6425 /* Ignore. */
fae299cd
DC
6426 break;
6427 }
6428
6429 child = sibling_die (child);
6430 }
6431 }
6432
6433 *lowpc = best_low;
6434 *highpc = best_high;
6435}
6436
801e3a5b
JB
6437/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6438 in DIE. */
6439static void
6440dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6441 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6442{
6443 struct attribute *attr;
6444
6445 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6446 if (attr)
6447 {
6448 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6449
801e3a5b
JB
6450 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6451 if (attr)
6452 {
6453 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6454
801e3a5b
JB
6455 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6456 }
6457 }
6458
6459 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6460 if (attr)
6461 {
6462 bfd *obfd = cu->objfile->obfd;
6463
6464 /* The value of the DW_AT_ranges attribute is the offset of the
6465 address range list in the .debug_ranges section. */
6466 unsigned long offset = DW_UNSND (attr);
dce234bc 6467 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6468
6469 /* For some target architectures, but not others, the
6470 read_address function sign-extends the addresses it returns.
6471 To recognize base address selection entries, we need a
6472 mask. */
6473 unsigned int addr_size = cu->header.addr_size;
6474 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6475
6476 /* The base address, to which the next pair is relative. Note
6477 that this 'base' is a DWARF concept: most entries in a range
6478 list are relative, to reduce the number of relocs against the
6479 debugging information. This is separate from this function's
6480 'baseaddr' argument, which GDB uses to relocate debugging
6481 information from a shared library based on the address at
6482 which the library was loaded. */
d00adf39
DE
6483 CORE_ADDR base = cu->base_address;
6484 int base_known = cu->base_known;
801e3a5b 6485
be391dca 6486 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6487 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6488 {
6489 complaint (&symfile_complaints,
6490 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6491 offset);
6492 return;
6493 }
6494
6495 for (;;)
6496 {
6497 unsigned int bytes_read;
6498 CORE_ADDR start, end;
6499
6500 start = read_address (obfd, buffer, cu, &bytes_read);
6501 buffer += bytes_read;
6502 end = read_address (obfd, buffer, cu, &bytes_read);
6503 buffer += bytes_read;
6504
6505 /* Did we find the end of the range list? */
6506 if (start == 0 && end == 0)
6507 break;
6508
6509 /* Did we find a base address selection entry? */
6510 else if ((start & base_select_mask) == base_select_mask)
6511 {
6512 base = end;
6513 base_known = 1;
6514 }
6515
6516 /* We found an ordinary address range. */
6517 else
6518 {
6519 if (!base_known)
6520 {
6521 complaint (&symfile_complaints,
3e43a32a
MS
6522 _("Invalid .debug_ranges data "
6523 "(no base address)"));
801e3a5b
JB
6524 return;
6525 }
6526
9277c30c
UW
6527 if (start > end)
6528 {
6529 /* Inverted range entries are invalid. */
6530 complaint (&symfile_complaints,
6531 _("Invalid .debug_ranges data "
6532 "(inverted range)"));
6533 return;
6534 }
6535
6536 /* Empty range entries have no effect. */
6537 if (start == end)
6538 continue;
6539
6e70227d
DE
6540 record_block_range (block,
6541 baseaddr + base + start,
801e3a5b
JB
6542 baseaddr + base + end - 1);
6543 }
6544 }
6545 }
6546}
6547
60d5a603
JK
6548/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6549 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6550 during 4.6.0 experimental. */
6551
6552static int
6553producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6554{
6555 const char *cs;
6556 int major, minor, release;
6557
6558 if (cu->producer == NULL)
6559 {
6560 /* For unknown compilers expect their behavior is DWARF version
6561 compliant.
6562
6563 GCC started to support .debug_types sections by -gdwarf-4 since
6564 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6565 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6566 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6567 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6568
6569 return 0;
6570 }
6571
6572 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6573
6574 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6575 {
6576 /* For non-GCC compilers expect their behavior is DWARF version
6577 compliant. */
6578
6579 return 0;
6580 }
6581 cs = &cu->producer[strlen ("GNU ")];
6582 while (*cs && !isdigit (*cs))
6583 cs++;
6584 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6585 {
6586 /* Not recognized as GCC. */
6587
6588 return 0;
6589 }
6590
6591 return major < 4 || (major == 4 && minor < 6);
6592}
6593
6594/* Return the default accessibility type if it is not overriden by
6595 DW_AT_accessibility. */
6596
6597static enum dwarf_access_attribute
6598dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6599{
6600 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6601 {
6602 /* The default DWARF 2 accessibility for members is public, the default
6603 accessibility for inheritance is private. */
6604
6605 if (die->tag != DW_TAG_inheritance)
6606 return DW_ACCESS_public;
6607 else
6608 return DW_ACCESS_private;
6609 }
6610 else
6611 {
6612 /* DWARF 3+ defines the default accessibility a different way. The same
6613 rules apply now for DW_TAG_inheritance as for the members and it only
6614 depends on the container kind. */
6615
6616 if (die->parent->tag == DW_TAG_class_type)
6617 return DW_ACCESS_private;
6618 else
6619 return DW_ACCESS_public;
6620 }
6621}
6622
74ac6d43
TT
6623/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6624 offset. If the attribute was not found return 0, otherwise return
6625 1. If it was found but could not properly be handled, set *OFFSET
6626 to 0. */
6627
6628static int
6629handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6630 LONGEST *offset)
6631{
6632 struct attribute *attr;
6633
6634 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6635 if (attr != NULL)
6636 {
6637 *offset = 0;
6638
6639 /* Note that we do not check for a section offset first here.
6640 This is because DW_AT_data_member_location is new in DWARF 4,
6641 so if we see it, we can assume that a constant form is really
6642 a constant and not a section offset. */
6643 if (attr_form_is_constant (attr))
6644 *offset = dwarf2_get_attr_constant_value (attr, 0);
6645 else if (attr_form_is_section_offset (attr))
6646 dwarf2_complex_location_expr_complaint ();
6647 else if (attr_form_is_block (attr))
6648 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6649 else
6650 dwarf2_complex_location_expr_complaint ();
6651
6652 return 1;
6653 }
6654
6655 return 0;
6656}
6657
c906108c
SS
6658/* Add an aggregate field to the field list. */
6659
6660static void
107d2387 6661dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6662 struct dwarf2_cu *cu)
6e70227d 6663{
e7c27a73 6664 struct objfile *objfile = cu->objfile;
5e2b427d 6665 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6666 struct nextfield *new_field;
6667 struct attribute *attr;
6668 struct field *fp;
6669 char *fieldname = "";
6670
6671 /* Allocate a new field list entry and link it in. */
6672 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6673 make_cleanup (xfree, new_field);
c906108c 6674 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6675
6676 if (die->tag == DW_TAG_inheritance)
6677 {
6678 new_field->next = fip->baseclasses;
6679 fip->baseclasses = new_field;
6680 }
6681 else
6682 {
6683 new_field->next = fip->fields;
6684 fip->fields = new_field;
6685 }
c906108c
SS
6686 fip->nfields++;
6687
e142c38c 6688 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6689 if (attr)
6690 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6691 else
6692 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6693 if (new_field->accessibility != DW_ACCESS_public)
6694 fip->non_public_fields = 1;
60d5a603 6695
e142c38c 6696 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6697 if (attr)
6698 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6699 else
6700 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6701
6702 fp = &new_field->field;
a9a9bd0f 6703
e142c38c 6704 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6705 {
74ac6d43
TT
6706 LONGEST offset;
6707
a9a9bd0f 6708 /* Data member other than a C++ static data member. */
6e70227d 6709
c906108c 6710 /* Get type of field. */
e7c27a73 6711 fp->type = die_type (die, cu);
c906108c 6712
d6a843b5 6713 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6714
c906108c 6715 /* Get bit size of field (zero if none). */
e142c38c 6716 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6717 if (attr)
6718 {
6719 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6720 }
6721 else
6722 {
6723 FIELD_BITSIZE (*fp) = 0;
6724 }
6725
6726 /* Get bit offset of field. */
74ac6d43
TT
6727 if (handle_data_member_location (die, cu, &offset))
6728 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6729 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6730 if (attr)
6731 {
5e2b427d 6732 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6733 {
6734 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6735 additional bit offset from the MSB of the containing
6736 anonymous object to the MSB of the field. We don't
6737 have to do anything special since we don't need to
6738 know the size of the anonymous object. */
c906108c
SS
6739 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6740 }
6741 else
6742 {
6743 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6744 MSB of the anonymous object, subtract off the number of
6745 bits from the MSB of the field to the MSB of the
6746 object, and then subtract off the number of bits of
6747 the field itself. The result is the bit offset of
6748 the LSB of the field. */
c906108c
SS
6749 int anonymous_size;
6750 int bit_offset = DW_UNSND (attr);
6751
e142c38c 6752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6753 if (attr)
6754 {
6755 /* The size of the anonymous object containing
6756 the bit field is explicit, so use the
6757 indicated size (in bytes). */
6758 anonymous_size = DW_UNSND (attr);
6759 }
6760 else
6761 {
6762 /* The size of the anonymous object containing
6763 the bit field must be inferred from the type
6764 attribute of the data member containing the
6765 bit field. */
6766 anonymous_size = TYPE_LENGTH (fp->type);
6767 }
6768 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6769 - bit_offset - FIELD_BITSIZE (*fp);
6770 }
6771 }
6772
6773 /* Get name of field. */
39cbfefa
DJ
6774 fieldname = dwarf2_name (die, cu);
6775 if (fieldname == NULL)
6776 fieldname = "";
d8151005
DJ
6777
6778 /* The name is already allocated along with this objfile, so we don't
6779 need to duplicate it for the type. */
6780 fp->name = fieldname;
c906108c
SS
6781
6782 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6783 pointer or virtual base class pointer) to private. */
e142c38c 6784 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6785 {
d48cc9dd 6786 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6787 new_field->accessibility = DW_ACCESS_private;
6788 fip->non_public_fields = 1;
6789 }
6790 }
a9a9bd0f 6791 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6792 {
a9a9bd0f
DC
6793 /* C++ static member. */
6794
6795 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6796 is a declaration, but all versions of G++ as of this writing
6797 (so through at least 3.2.1) incorrectly generate
6798 DW_TAG_variable tags. */
6e70227d 6799
ff355380 6800 const char *physname;
c906108c 6801
a9a9bd0f 6802 /* Get name of field. */
39cbfefa
DJ
6803 fieldname = dwarf2_name (die, cu);
6804 if (fieldname == NULL)
c906108c
SS
6805 return;
6806
254e6b9e 6807 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6808 if (attr
6809 /* Only create a symbol if this is an external value.
6810 new_symbol checks this and puts the value in the global symbol
6811 table, which we want. If it is not external, new_symbol
6812 will try to put the value in cu->list_in_scope which is wrong. */
6813 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6814 {
6815 /* A static const member, not much different than an enum as far as
6816 we're concerned, except that we can support more types. */
6817 new_symbol (die, NULL, cu);
6818 }
6819
2df3850c 6820 /* Get physical name. */
ff355380 6821 physname = dwarf2_physname (fieldname, die, cu);
c906108c 6822
d8151005
DJ
6823 /* The name is already allocated along with this objfile, so we don't
6824 need to duplicate it for the type. */
6825 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6826 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6827 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6828 }
6829 else if (die->tag == DW_TAG_inheritance)
6830 {
74ac6d43 6831 LONGEST offset;
d4b96c9a 6832
74ac6d43
TT
6833 /* C++ base class field. */
6834 if (handle_data_member_location (die, cu, &offset))
6835 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6836 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6837 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6838 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6839 fip->nbaseclasses++;
6840 }
6841}
6842
98751a41
JK
6843/* Add a typedef defined in the scope of the FIP's class. */
6844
6845static void
6846dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6847 struct dwarf2_cu *cu)
6e70227d 6848{
98751a41 6849 struct objfile *objfile = cu->objfile;
98751a41
JK
6850 struct typedef_field_list *new_field;
6851 struct attribute *attr;
6852 struct typedef_field *fp;
6853 char *fieldname = "";
6854
6855 /* Allocate a new field list entry and link it in. */
6856 new_field = xzalloc (sizeof (*new_field));
6857 make_cleanup (xfree, new_field);
6858
6859 gdb_assert (die->tag == DW_TAG_typedef);
6860
6861 fp = &new_field->field;
6862
6863 /* Get name of field. */
6864 fp->name = dwarf2_name (die, cu);
6865 if (fp->name == NULL)
6866 return;
6867
6868 fp->type = read_type_die (die, cu);
6869
6870 new_field->next = fip->typedef_field_list;
6871 fip->typedef_field_list = new_field;
6872 fip->typedef_field_list_count++;
6873}
6874
c906108c
SS
6875/* Create the vector of fields, and attach it to the type. */
6876
6877static void
fba45db2 6878dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6879 struct dwarf2_cu *cu)
c906108c
SS
6880{
6881 int nfields = fip->nfields;
6882
6883 /* Record the field count, allocate space for the array of fields,
6884 and create blank accessibility bitfields if necessary. */
6885 TYPE_NFIELDS (type) = nfields;
6886 TYPE_FIELDS (type) = (struct field *)
6887 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6888 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6889
b4ba55a1 6890 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6891 {
6892 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6893
6894 TYPE_FIELD_PRIVATE_BITS (type) =
6895 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6896 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6897
6898 TYPE_FIELD_PROTECTED_BITS (type) =
6899 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6900 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6901
774b6a14
TT
6902 TYPE_FIELD_IGNORE_BITS (type) =
6903 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6904 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6905 }
6906
6907 /* If the type has baseclasses, allocate and clear a bit vector for
6908 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6909 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6910 {
6911 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6912 unsigned char *pointer;
c906108c
SS
6913
6914 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6915 pointer = TYPE_ALLOC (type, num_bytes);
6916 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6917 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6918 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6919 }
6920
3e43a32a
MS
6921 /* Copy the saved-up fields into the field vector. Start from the head of
6922 the list, adding to the tail of the field array, so that they end up in
6923 the same order in the array in which they were added to the list. */
c906108c
SS
6924 while (nfields-- > 0)
6925 {
7d0ccb61
DJ
6926 struct nextfield *fieldp;
6927
6928 if (fip->fields)
6929 {
6930 fieldp = fip->fields;
6931 fip->fields = fieldp->next;
6932 }
6933 else
6934 {
6935 fieldp = fip->baseclasses;
6936 fip->baseclasses = fieldp->next;
6937 }
6938
6939 TYPE_FIELD (type, nfields) = fieldp->field;
6940 switch (fieldp->accessibility)
c906108c 6941 {
c5aa993b 6942 case DW_ACCESS_private:
b4ba55a1
JB
6943 if (cu->language != language_ada)
6944 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6945 break;
c906108c 6946
c5aa993b 6947 case DW_ACCESS_protected:
b4ba55a1
JB
6948 if (cu->language != language_ada)
6949 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6950 break;
c906108c 6951
c5aa993b
JM
6952 case DW_ACCESS_public:
6953 break;
c906108c 6954
c5aa993b
JM
6955 default:
6956 /* Unknown accessibility. Complain and treat it as public. */
6957 {
e2e0b3e5 6958 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6959 fieldp->accessibility);
c5aa993b
JM
6960 }
6961 break;
c906108c
SS
6962 }
6963 if (nfields < fip->nbaseclasses)
6964 {
7d0ccb61 6965 switch (fieldp->virtuality)
c906108c 6966 {
c5aa993b
JM
6967 case DW_VIRTUALITY_virtual:
6968 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6969 if (cu->language == language_ada)
a73c6dcd 6970 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6971 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6972 break;
c906108c
SS
6973 }
6974 }
c906108c
SS
6975 }
6976}
6977
c906108c
SS
6978/* Add a member function to the proper fieldlist. */
6979
6980static void
107d2387 6981dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6982 struct type *type, struct dwarf2_cu *cu)
c906108c 6983{
e7c27a73 6984 struct objfile *objfile = cu->objfile;
c906108c
SS
6985 struct attribute *attr;
6986 struct fnfieldlist *flp;
6987 int i;
6988 struct fn_field *fnp;
6989 char *fieldname;
c906108c 6990 struct nextfnfield *new_fnfield;
f792889a 6991 struct type *this_type;
60d5a603 6992 enum dwarf_access_attribute accessibility;
c906108c 6993
b4ba55a1 6994 if (cu->language == language_ada)
a73c6dcd 6995 error (_("unexpected member function in Ada type"));
b4ba55a1 6996
2df3850c 6997 /* Get name of member function. */
39cbfefa
DJ
6998 fieldname = dwarf2_name (die, cu);
6999 if (fieldname == NULL)
2df3850c 7000 return;
c906108c 7001
c906108c
SS
7002 /* Look up member function name in fieldlist. */
7003 for (i = 0; i < fip->nfnfields; i++)
7004 {
27bfe10e 7005 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7006 break;
7007 }
7008
7009 /* Create new list element if necessary. */
7010 if (i < fip->nfnfields)
7011 flp = &fip->fnfieldlists[i];
7012 else
7013 {
7014 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7015 {
7016 fip->fnfieldlists = (struct fnfieldlist *)
7017 xrealloc (fip->fnfieldlists,
7018 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7019 * sizeof (struct fnfieldlist));
c906108c 7020 if (fip->nfnfields == 0)
c13c43fd 7021 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7022 }
7023 flp = &fip->fnfieldlists[fip->nfnfields];
7024 flp->name = fieldname;
7025 flp->length = 0;
7026 flp->head = NULL;
3da10d80 7027 i = fip->nfnfields++;
c906108c
SS
7028 }
7029
7030 /* Create a new member function field and chain it to the field list
0963b4bd 7031 entry. */
c906108c 7032 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7033 make_cleanup (xfree, new_fnfield);
c906108c
SS
7034 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7035 new_fnfield->next = flp->head;
7036 flp->head = new_fnfield;
7037 flp->length++;
7038
7039 /* Fill in the member function field info. */
7040 fnp = &new_fnfield->fnfield;
3da10d80
KS
7041
7042 /* Delay processing of the physname until later. */
7043 if (cu->language == language_cplus || cu->language == language_java)
7044 {
7045 add_to_method_list (type, i, flp->length - 1, fieldname,
7046 die, cu);
7047 }
7048 else
7049 {
1d06ead6 7050 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7051 fnp->physname = physname ? physname : "";
7052 }
7053
c906108c 7054 fnp->type = alloc_type (objfile);
f792889a
DJ
7055 this_type = read_type_die (die, cu);
7056 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7057 {
f792889a 7058 int nparams = TYPE_NFIELDS (this_type);
c906108c 7059
f792889a 7060 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7061 of the method itself (TYPE_CODE_METHOD). */
7062 smash_to_method_type (fnp->type, type,
f792889a
DJ
7063 TYPE_TARGET_TYPE (this_type),
7064 TYPE_FIELDS (this_type),
7065 TYPE_NFIELDS (this_type),
7066 TYPE_VARARGS (this_type));
c906108c
SS
7067
7068 /* Handle static member functions.
c5aa993b 7069 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7070 member functions. G++ helps GDB by marking the first
7071 parameter for non-static member functions (which is the this
7072 pointer) as artificial. We obtain this information from
7073 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7074 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7075 fnp->voffset = VOFFSET_STATIC;
7076 }
7077 else
e2e0b3e5 7078 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7079 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7080
7081 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7082 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7083 fnp->fcontext = die_containing_type (die, cu);
c906108c 7084
3e43a32a
MS
7085 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7086 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7087
7088 /* Get accessibility. */
e142c38c 7089 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7090 if (attr)
60d5a603
JK
7091 accessibility = DW_UNSND (attr);
7092 else
7093 accessibility = dwarf2_default_access_attribute (die, cu);
7094 switch (accessibility)
c906108c 7095 {
60d5a603
JK
7096 case DW_ACCESS_private:
7097 fnp->is_private = 1;
7098 break;
7099 case DW_ACCESS_protected:
7100 fnp->is_protected = 1;
7101 break;
c906108c
SS
7102 }
7103
b02dede2 7104 /* Check for artificial methods. */
e142c38c 7105 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7106 if (attr && DW_UNSND (attr) != 0)
7107 fnp->is_artificial = 1;
7108
0d564a31 7109 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7110 function. For older versions of GCC, this is an offset in the
7111 appropriate virtual table, as specified by DW_AT_containing_type.
7112 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7113 to the object address. */
7114
e142c38c 7115 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7116 if (attr)
8e19ed76 7117 {
aec5aa8b 7118 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7119 {
aec5aa8b
TT
7120 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7121 {
7122 /* Old-style GCC. */
7123 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7124 }
7125 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7126 || (DW_BLOCK (attr)->size > 1
7127 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7128 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7129 {
7130 struct dwarf_block blk;
7131 int offset;
7132
7133 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7134 ? 1 : 2);
7135 blk.size = DW_BLOCK (attr)->size - offset;
7136 blk.data = DW_BLOCK (attr)->data + offset;
7137 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7138 if ((fnp->voffset % cu->header.addr_size) != 0)
7139 dwarf2_complex_location_expr_complaint ();
7140 else
7141 fnp->voffset /= cu->header.addr_size;
7142 fnp->voffset += 2;
7143 }
7144 else
7145 dwarf2_complex_location_expr_complaint ();
7146
7147 if (!fnp->fcontext)
7148 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7149 }
3690dd37 7150 else if (attr_form_is_section_offset (attr))
8e19ed76 7151 {
4d3c2250 7152 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7153 }
7154 else
7155 {
4d3c2250
KB
7156 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7157 fieldname);
8e19ed76 7158 }
0d564a31 7159 }
d48cc9dd
DJ
7160 else
7161 {
7162 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7163 if (attr && DW_UNSND (attr))
7164 {
7165 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7166 complaint (&symfile_complaints,
3e43a32a
MS
7167 _("Member function \"%s\" (offset %d) is virtual "
7168 "but the vtable offset is not specified"),
d48cc9dd 7169 fieldname, die->offset);
9655fd1a 7170 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7171 TYPE_CPLUS_DYNAMIC (type) = 1;
7172 }
7173 }
c906108c
SS
7174}
7175
7176/* Create the vector of member function fields, and attach it to the type. */
7177
7178static void
fba45db2 7179dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7180 struct dwarf2_cu *cu)
c906108c
SS
7181{
7182 struct fnfieldlist *flp;
7183 int total_length = 0;
7184 int i;
7185
b4ba55a1 7186 if (cu->language == language_ada)
a73c6dcd 7187 error (_("unexpected member functions in Ada type"));
b4ba55a1 7188
c906108c
SS
7189 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7190 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7191 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7192
7193 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7194 {
7195 struct nextfnfield *nfp = flp->head;
7196 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7197 int k;
7198
7199 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7200 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7201 fn_flp->fn_fields = (struct fn_field *)
7202 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7203 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7204 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7205
7206 total_length += flp->length;
7207 }
7208
7209 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7210 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7211}
7212
1168df01
JB
7213/* Returns non-zero if NAME is the name of a vtable member in CU's
7214 language, zero otherwise. */
7215static int
7216is_vtable_name (const char *name, struct dwarf2_cu *cu)
7217{
7218 static const char vptr[] = "_vptr";
987504bb 7219 static const char vtable[] = "vtable";
1168df01 7220
987504bb
JJ
7221 /* Look for the C++ and Java forms of the vtable. */
7222 if ((cu->language == language_java
7223 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7224 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7225 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7226 return 1;
7227
7228 return 0;
7229}
7230
c0dd20ea 7231/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7232 functions, with the ABI-specified layout. If TYPE describes
7233 such a structure, smash it into a member function type.
61049d3b
DJ
7234
7235 GCC shouldn't do this; it should just output pointer to member DIEs.
7236 This is GCC PR debug/28767. */
c0dd20ea 7237
0b92b5bb
TT
7238static void
7239quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7240{
0b92b5bb 7241 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7242
7243 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7244 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7245 return;
c0dd20ea
DJ
7246
7247 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7248 if (TYPE_FIELD_NAME (type, 0) == NULL
7249 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7250 || TYPE_FIELD_NAME (type, 1) == NULL
7251 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7252 return;
c0dd20ea
DJ
7253
7254 /* Find the type of the method. */
0b92b5bb 7255 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7256 if (pfn_type == NULL
7257 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7258 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7259 return;
c0dd20ea
DJ
7260
7261 /* Look for the "this" argument. */
7262 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7263 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7264 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7265 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7266 return;
c0dd20ea
DJ
7267
7268 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7269 new_type = alloc_type (objfile);
7270 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7271 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7272 TYPE_VARARGS (pfn_type));
0b92b5bb 7273 smash_to_methodptr_type (type, new_type);
c0dd20ea 7274}
1168df01 7275
c906108c 7276/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7277 (definition) to create a type for the structure or union. Fill in
7278 the type's name and general properties; the members will not be
7279 processed until process_structure_type.
c906108c 7280
c767944b
DJ
7281 NOTE: we need to call these functions regardless of whether or not the
7282 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7283 structure or union. This gets the type entered into our set of
7284 user defined types.
7285
7286 However, if the structure is incomplete (an opaque struct/union)
7287 then suppress creating a symbol table entry for it since gdb only
7288 wants to find the one with the complete definition. Note that if
7289 it is complete, we just call new_symbol, which does it's own
7290 checking about whether the struct/union is anonymous or not (and
7291 suppresses creating a symbol table entry itself). */
7292
f792889a 7293static struct type *
134d01f1 7294read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7295{
e7c27a73 7296 struct objfile *objfile = cu->objfile;
c906108c
SS
7297 struct type *type;
7298 struct attribute *attr;
39cbfefa 7299 char *name;
c906108c 7300
348e048f
DE
7301 /* If the definition of this type lives in .debug_types, read that type.
7302 Don't follow DW_AT_specification though, that will take us back up
7303 the chain and we want to go down. */
7304 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7305 if (attr)
7306 {
7307 struct dwarf2_cu *type_cu = cu;
7308 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7309
348e048f
DE
7310 /* We could just recurse on read_structure_type, but we need to call
7311 get_die_type to ensure only one type for this DIE is created.
7312 This is important, for example, because for c++ classes we need
7313 TYPE_NAME set which is only done by new_symbol. Blech. */
7314 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7315
7316 /* TYPE_CU may not be the same as CU.
7317 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7318 return set_die_type (die, type, cu);
7319 }
7320
c0dd20ea 7321 type = alloc_type (objfile);
c906108c 7322 INIT_CPLUS_SPECIFIC (type);
93311388 7323
39cbfefa
DJ
7324 name = dwarf2_name (die, cu);
7325 if (name != NULL)
c906108c 7326 {
987504bb
JJ
7327 if (cu->language == language_cplus
7328 || cu->language == language_java)
63d06c5c 7329 {
3da10d80
KS
7330 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7331
7332 /* dwarf2_full_name might have already finished building the DIE's
7333 type. If so, there is no need to continue. */
7334 if (get_die_type (die, cu) != NULL)
7335 return get_die_type (die, cu);
7336
7337 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7338 if (die->tag == DW_TAG_structure_type
7339 || die->tag == DW_TAG_class_type)
7340 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7341 }
7342 else
7343 {
d8151005
DJ
7344 /* The name is already allocated along with this objfile, so
7345 we don't need to duplicate it for the type. */
94af9270
KS
7346 TYPE_TAG_NAME (type) = (char *) name;
7347 if (die->tag == DW_TAG_class_type)
7348 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7349 }
c906108c
SS
7350 }
7351
7352 if (die->tag == DW_TAG_structure_type)
7353 {
7354 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7355 }
7356 else if (die->tag == DW_TAG_union_type)
7357 {
7358 TYPE_CODE (type) = TYPE_CODE_UNION;
7359 }
7360 else
7361 {
c906108c
SS
7362 TYPE_CODE (type) = TYPE_CODE_CLASS;
7363 }
7364
0cc2414c
TT
7365 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7366 TYPE_DECLARED_CLASS (type) = 1;
7367
e142c38c 7368 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7369 if (attr)
7370 {
7371 TYPE_LENGTH (type) = DW_UNSND (attr);
7372 }
7373 else
7374 {
7375 TYPE_LENGTH (type) = 0;
7376 }
7377
876cecd0 7378 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7379 if (die_is_declaration (die, cu))
876cecd0 7380 TYPE_STUB (type) = 1;
a6c727b2
DJ
7381 else if (attr == NULL && die->child == NULL
7382 && producer_is_realview (cu->producer))
7383 /* RealView does not output the required DW_AT_declaration
7384 on incomplete types. */
7385 TYPE_STUB (type) = 1;
dc718098 7386
c906108c
SS
7387 /* We need to add the type field to the die immediately so we don't
7388 infinitely recurse when dealing with pointers to the structure
0963b4bd 7389 type within the structure itself. */
1c379e20 7390 set_die_type (die, type, cu);
c906108c 7391
7e314c57
JK
7392 /* set_die_type should be already done. */
7393 set_descriptive_type (type, die, cu);
7394
c767944b
DJ
7395 return type;
7396}
7397
7398/* Finish creating a structure or union type, including filling in
7399 its members and creating a symbol for it. */
7400
7401static void
7402process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7403{
7404 struct objfile *objfile = cu->objfile;
7405 struct die_info *child_die = die->child;
7406 struct type *type;
7407
7408 type = get_die_type (die, cu);
7409 if (type == NULL)
7410 type = read_structure_type (die, cu);
7411
e142c38c 7412 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7413 {
7414 struct field_info fi;
7415 struct die_info *child_die;
34eaf542 7416 VEC (symbolp) *template_args = NULL;
c767944b 7417 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7418
7419 memset (&fi, 0, sizeof (struct field_info));
7420
639d11d3 7421 child_die = die->child;
c906108c
SS
7422
7423 while (child_die && child_die->tag)
7424 {
a9a9bd0f
DC
7425 if (child_die->tag == DW_TAG_member
7426 || child_die->tag == DW_TAG_variable)
c906108c 7427 {
a9a9bd0f
DC
7428 /* NOTE: carlton/2002-11-05: A C++ static data member
7429 should be a DW_TAG_member that is a declaration, but
7430 all versions of G++ as of this writing (so through at
7431 least 3.2.1) incorrectly generate DW_TAG_variable
7432 tags for them instead. */
e7c27a73 7433 dwarf2_add_field (&fi, child_die, cu);
c906108c 7434 }
8713b1b1 7435 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7436 {
0963b4bd 7437 /* C++ member function. */
e7c27a73 7438 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7439 }
7440 else if (child_die->tag == DW_TAG_inheritance)
7441 {
7442 /* C++ base class field. */
e7c27a73 7443 dwarf2_add_field (&fi, child_die, cu);
c906108c 7444 }
98751a41
JK
7445 else if (child_die->tag == DW_TAG_typedef)
7446 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7447 else if (child_die->tag == DW_TAG_template_type_param
7448 || child_die->tag == DW_TAG_template_value_param)
7449 {
7450 struct symbol *arg = new_symbol (child_die, NULL, cu);
7451
f1078f66
DJ
7452 if (arg != NULL)
7453 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7454 }
7455
c906108c
SS
7456 child_die = sibling_die (child_die);
7457 }
7458
34eaf542
TT
7459 /* Attach template arguments to type. */
7460 if (! VEC_empty (symbolp, template_args))
7461 {
7462 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7463 TYPE_N_TEMPLATE_ARGUMENTS (type)
7464 = VEC_length (symbolp, template_args);
7465 TYPE_TEMPLATE_ARGUMENTS (type)
7466 = obstack_alloc (&objfile->objfile_obstack,
7467 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7468 * sizeof (struct symbol *)));
7469 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7470 VEC_address (symbolp, template_args),
7471 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7472 * sizeof (struct symbol *)));
7473 VEC_free (symbolp, template_args);
7474 }
7475
c906108c
SS
7476 /* Attach fields and member functions to the type. */
7477 if (fi.nfields)
e7c27a73 7478 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7479 if (fi.nfnfields)
7480 {
e7c27a73 7481 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7482
c5aa993b 7483 /* Get the type which refers to the base class (possibly this
c906108c 7484 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7485 class from the DW_AT_containing_type attribute. This use of
7486 DW_AT_containing_type is a GNU extension. */
c906108c 7487
e142c38c 7488 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7489 {
e7c27a73 7490 struct type *t = die_containing_type (die, cu);
c906108c
SS
7491
7492 TYPE_VPTR_BASETYPE (type) = t;
7493 if (type == t)
7494 {
c906108c
SS
7495 int i;
7496
7497 /* Our own class provides vtbl ptr. */
7498 for (i = TYPE_NFIELDS (t) - 1;
7499 i >= TYPE_N_BASECLASSES (t);
7500 --i)
7501 {
7502 char *fieldname = TYPE_FIELD_NAME (t, i);
7503
1168df01 7504 if (is_vtable_name (fieldname, cu))
c906108c
SS
7505 {
7506 TYPE_VPTR_FIELDNO (type) = i;
7507 break;
7508 }
7509 }
7510
7511 /* Complain if virtual function table field not found. */
7512 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7513 complaint (&symfile_complaints,
3e43a32a
MS
7514 _("virtual function table pointer "
7515 "not found when defining class '%s'"),
4d3c2250
KB
7516 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7517 "");
c906108c
SS
7518 }
7519 else
7520 {
7521 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7522 }
7523 }
f6235d4c
EZ
7524 else if (cu->producer
7525 && strncmp (cu->producer,
7526 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7527 {
7528 /* The IBM XLC compiler does not provide direct indication
7529 of the containing type, but the vtable pointer is
7530 always named __vfp. */
7531
7532 int i;
7533
7534 for (i = TYPE_NFIELDS (type) - 1;
7535 i >= TYPE_N_BASECLASSES (type);
7536 --i)
7537 {
7538 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7539 {
7540 TYPE_VPTR_FIELDNO (type) = i;
7541 TYPE_VPTR_BASETYPE (type) = type;
7542 break;
7543 }
7544 }
7545 }
c906108c 7546 }
98751a41
JK
7547
7548 /* Copy fi.typedef_field_list linked list elements content into the
7549 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7550 if (fi.typedef_field_list)
7551 {
7552 int i = fi.typedef_field_list_count;
7553
a0d7a4ff 7554 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7555 TYPE_TYPEDEF_FIELD_ARRAY (type)
7556 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7557 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7558
7559 /* Reverse the list order to keep the debug info elements order. */
7560 while (--i >= 0)
7561 {
7562 struct typedef_field *dest, *src;
6e70227d 7563
98751a41
JK
7564 dest = &TYPE_TYPEDEF_FIELD (type, i);
7565 src = &fi.typedef_field_list->field;
7566 fi.typedef_field_list = fi.typedef_field_list->next;
7567 *dest = *src;
7568 }
7569 }
c767944b
DJ
7570
7571 do_cleanups (back_to);
eb2a6f42
TT
7572
7573 if (HAVE_CPLUS_STRUCT (type))
7574 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7575 }
63d06c5c 7576
0b92b5bb
TT
7577 quirk_gcc_member_function_pointer (type, cu->objfile);
7578
90aeadfc
DC
7579 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7580 snapshots) has been known to create a die giving a declaration
7581 for a class that has, as a child, a die giving a definition for a
7582 nested class. So we have to process our children even if the
7583 current die is a declaration. Normally, of course, a declaration
7584 won't have any children at all. */
134d01f1 7585
90aeadfc
DC
7586 while (child_die != NULL && child_die->tag)
7587 {
7588 if (child_die->tag == DW_TAG_member
7589 || child_die->tag == DW_TAG_variable
34eaf542
TT
7590 || child_die->tag == DW_TAG_inheritance
7591 || child_die->tag == DW_TAG_template_value_param
7592 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7593 {
90aeadfc 7594 /* Do nothing. */
134d01f1 7595 }
90aeadfc
DC
7596 else
7597 process_die (child_die, cu);
134d01f1 7598
90aeadfc 7599 child_die = sibling_die (child_die);
134d01f1
DJ
7600 }
7601
fa4028e9
JB
7602 /* Do not consider external references. According to the DWARF standard,
7603 these DIEs are identified by the fact that they have no byte_size
7604 attribute, and a declaration attribute. */
7605 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7606 || !die_is_declaration (die, cu))
c767944b 7607 new_symbol (die, type, cu);
134d01f1
DJ
7608}
7609
7610/* Given a DW_AT_enumeration_type die, set its type. We do not
7611 complete the type's fields yet, or create any symbols. */
c906108c 7612
f792889a 7613static struct type *
134d01f1 7614read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7615{
e7c27a73 7616 struct objfile *objfile = cu->objfile;
c906108c 7617 struct type *type;
c906108c 7618 struct attribute *attr;
0114d602 7619 const char *name;
134d01f1 7620
348e048f
DE
7621 /* If the definition of this type lives in .debug_types, read that type.
7622 Don't follow DW_AT_specification though, that will take us back up
7623 the chain and we want to go down. */
7624 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7625 if (attr)
7626 {
7627 struct dwarf2_cu *type_cu = cu;
7628 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7629
348e048f 7630 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7631
7632 /* TYPE_CU may not be the same as CU.
7633 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7634 return set_die_type (die, type, cu);
7635 }
7636
c906108c
SS
7637 type = alloc_type (objfile);
7638
7639 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7640 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7641 if (name != NULL)
0114d602 7642 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7643
e142c38c 7644 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7645 if (attr)
7646 {
7647 TYPE_LENGTH (type) = DW_UNSND (attr);
7648 }
7649 else
7650 {
7651 TYPE_LENGTH (type) = 0;
7652 }
7653
137033e9
JB
7654 /* The enumeration DIE can be incomplete. In Ada, any type can be
7655 declared as private in the package spec, and then defined only
7656 inside the package body. Such types are known as Taft Amendment
7657 Types. When another package uses such a type, an incomplete DIE
7658 may be generated by the compiler. */
02eb380e 7659 if (die_is_declaration (die, cu))
876cecd0 7660 TYPE_STUB (type) = 1;
02eb380e 7661
f792889a 7662 return set_die_type (die, type, cu);
134d01f1
DJ
7663}
7664
7665/* Given a pointer to a die which begins an enumeration, process all
7666 the dies that define the members of the enumeration, and create the
7667 symbol for the enumeration type.
7668
7669 NOTE: We reverse the order of the element list. */
7670
7671static void
7672process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7673{
f792889a 7674 struct type *this_type;
134d01f1 7675
f792889a
DJ
7676 this_type = get_die_type (die, cu);
7677 if (this_type == NULL)
7678 this_type = read_enumeration_type (die, cu);
9dc481d3 7679
639d11d3 7680 if (die->child != NULL)
c906108c 7681 {
9dc481d3
DE
7682 struct die_info *child_die;
7683 struct symbol *sym;
7684 struct field *fields = NULL;
7685 int num_fields = 0;
7686 int unsigned_enum = 1;
7687 char *name;
7688
639d11d3 7689 child_die = die->child;
c906108c
SS
7690 while (child_die && child_die->tag)
7691 {
7692 if (child_die->tag != DW_TAG_enumerator)
7693 {
e7c27a73 7694 process_die (child_die, cu);
c906108c
SS
7695 }
7696 else
7697 {
39cbfefa
DJ
7698 name = dwarf2_name (child_die, cu);
7699 if (name)
c906108c 7700 {
f792889a 7701 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7702 if (SYMBOL_VALUE (sym) < 0)
7703 unsigned_enum = 0;
7704
7705 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7706 {
7707 fields = (struct field *)
7708 xrealloc (fields,
7709 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7710 * sizeof (struct field));
c906108c
SS
7711 }
7712
3567439c 7713 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7714 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7715 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7716 FIELD_BITSIZE (fields[num_fields]) = 0;
7717
7718 num_fields++;
7719 }
7720 }
7721
7722 child_die = sibling_die (child_die);
7723 }
7724
7725 if (num_fields)
7726 {
f792889a
DJ
7727 TYPE_NFIELDS (this_type) = num_fields;
7728 TYPE_FIELDS (this_type) = (struct field *)
7729 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7730 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7731 sizeof (struct field) * num_fields);
b8c9b27d 7732 xfree (fields);
c906108c
SS
7733 }
7734 if (unsigned_enum)
876cecd0 7735 TYPE_UNSIGNED (this_type) = 1;
c906108c 7736 }
134d01f1 7737
6c83ed52
TT
7738 /* If we are reading an enum from a .debug_types unit, and the enum
7739 is a declaration, and the enum is not the signatured type in the
7740 unit, then we do not want to add a symbol for it. Adding a
7741 symbol would in some cases obscure the true definition of the
7742 enum, giving users an incomplete type when the definition is
7743 actually available. Note that we do not want to do this for all
7744 enums which are just declarations, because C++0x allows forward
7745 enum declarations. */
7746 if (cu->per_cu->debug_type_section
7747 && die_is_declaration (die, cu))
7748 {
7749 struct signatured_type *type_sig;
7750
7751 type_sig
7752 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7753 cu->per_cu->debug_type_section,
7754 cu->per_cu->offset);
7755 if (type_sig->type_offset != die->offset)
7756 return;
7757 }
7758
f792889a 7759 new_symbol (die, this_type, cu);
c906108c
SS
7760}
7761
7762/* Extract all information from a DW_TAG_array_type DIE and put it in
7763 the DIE's type field. For now, this only handles one dimensional
7764 arrays. */
7765
f792889a 7766static struct type *
e7c27a73 7767read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7768{
e7c27a73 7769 struct objfile *objfile = cu->objfile;
c906108c 7770 struct die_info *child_die;
7e314c57 7771 struct type *type;
c906108c
SS
7772 struct type *element_type, *range_type, *index_type;
7773 struct type **range_types = NULL;
7774 struct attribute *attr;
7775 int ndim = 0;
7776 struct cleanup *back_to;
39cbfefa 7777 char *name;
c906108c 7778
e7c27a73 7779 element_type = die_type (die, cu);
c906108c 7780
7e314c57
JK
7781 /* The die_type call above may have already set the type for this DIE. */
7782 type = get_die_type (die, cu);
7783 if (type)
7784 return type;
7785
c906108c
SS
7786 /* Irix 6.2 native cc creates array types without children for
7787 arrays with unspecified length. */
639d11d3 7788 if (die->child == NULL)
c906108c 7789 {
46bf5051 7790 index_type = objfile_type (objfile)->builtin_int;
c906108c 7791 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7792 type = create_array_type (NULL, element_type, range_type);
7793 return set_die_type (die, type, cu);
c906108c
SS
7794 }
7795
7796 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7797 child_die = die->child;
c906108c
SS
7798 while (child_die && child_die->tag)
7799 {
7800 if (child_die->tag == DW_TAG_subrange_type)
7801 {
f792889a 7802 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7803
f792889a 7804 if (child_type != NULL)
a02abb62 7805 {
0963b4bd
MS
7806 /* The range type was succesfully read. Save it for the
7807 array type creation. */
a02abb62
JB
7808 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7809 {
7810 range_types = (struct type **)
7811 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7812 * sizeof (struct type *));
7813 if (ndim == 0)
7814 make_cleanup (free_current_contents, &range_types);
7815 }
f792889a 7816 range_types[ndim++] = child_type;
a02abb62 7817 }
c906108c
SS
7818 }
7819 child_die = sibling_die (child_die);
7820 }
7821
7822 /* Dwarf2 dimensions are output from left to right, create the
7823 necessary array types in backwards order. */
7ca2d3a3 7824
c906108c 7825 type = element_type;
7ca2d3a3
DL
7826
7827 if (read_array_order (die, cu) == DW_ORD_col_major)
7828 {
7829 int i = 0;
9a619af0 7830
7ca2d3a3
DL
7831 while (i < ndim)
7832 type = create_array_type (NULL, type, range_types[i++]);
7833 }
7834 else
7835 {
7836 while (ndim-- > 0)
7837 type = create_array_type (NULL, type, range_types[ndim]);
7838 }
c906108c 7839
f5f8a009
EZ
7840 /* Understand Dwarf2 support for vector types (like they occur on
7841 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7842 array type. This is not part of the Dwarf2/3 standard yet, but a
7843 custom vendor extension. The main difference between a regular
7844 array and the vector variant is that vectors are passed by value
7845 to functions. */
e142c38c 7846 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7847 if (attr)
ea37ba09 7848 make_vector_type (type);
f5f8a009 7849
dbc98a8b
KW
7850 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7851 implementation may choose to implement triple vectors using this
7852 attribute. */
7853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7854 if (attr)
7855 {
7856 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7857 TYPE_LENGTH (type) = DW_UNSND (attr);
7858 else
3e43a32a
MS
7859 complaint (&symfile_complaints,
7860 _("DW_AT_byte_size for array type smaller "
7861 "than the total size of elements"));
dbc98a8b
KW
7862 }
7863
39cbfefa
DJ
7864 name = dwarf2_name (die, cu);
7865 if (name)
7866 TYPE_NAME (type) = name;
6e70227d 7867
0963b4bd 7868 /* Install the type in the die. */
7e314c57
JK
7869 set_die_type (die, type, cu);
7870
7871 /* set_die_type should be already done. */
b4ba55a1
JB
7872 set_descriptive_type (type, die, cu);
7873
c906108c
SS
7874 do_cleanups (back_to);
7875
7e314c57 7876 return type;
c906108c
SS
7877}
7878
7ca2d3a3 7879static enum dwarf_array_dim_ordering
6e70227d 7880read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7881{
7882 struct attribute *attr;
7883
7884 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7885
7886 if (attr) return DW_SND (attr);
7887
0963b4bd
MS
7888 /* GNU F77 is a special case, as at 08/2004 array type info is the
7889 opposite order to the dwarf2 specification, but data is still
7890 laid out as per normal fortran.
7ca2d3a3 7891
0963b4bd
MS
7892 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7893 version checking. */
7ca2d3a3 7894
905e0470
PM
7895 if (cu->language == language_fortran
7896 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7897 {
7898 return DW_ORD_row_major;
7899 }
7900
6e70227d 7901 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7902 {
7903 case array_column_major:
7904 return DW_ORD_col_major;
7905 case array_row_major:
7906 default:
7907 return DW_ORD_row_major;
7908 };
7909}
7910
72019c9c 7911/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7912 the DIE's type field. */
72019c9c 7913
f792889a 7914static struct type *
72019c9c
GM
7915read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7916{
7e314c57
JK
7917 struct type *domain_type, *set_type;
7918 struct attribute *attr;
f792889a 7919
7e314c57
JK
7920 domain_type = die_type (die, cu);
7921
7922 /* The die_type call above may have already set the type for this DIE. */
7923 set_type = get_die_type (die, cu);
7924 if (set_type)
7925 return set_type;
7926
7927 set_type = create_set_type (NULL, domain_type);
7928
7929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7930 if (attr)
7931 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7932
f792889a 7933 return set_die_type (die, set_type, cu);
72019c9c 7934}
7ca2d3a3 7935
c906108c
SS
7936/* First cut: install each common block member as a global variable. */
7937
7938static void
e7c27a73 7939read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7940{
7941 struct die_info *child_die;
7942 struct attribute *attr;
7943 struct symbol *sym;
7944 CORE_ADDR base = (CORE_ADDR) 0;
7945
e142c38c 7946 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7947 if (attr)
7948 {
0963b4bd 7949 /* Support the .debug_loc offsets. */
8e19ed76
PS
7950 if (attr_form_is_block (attr))
7951 {
e7c27a73 7952 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7953 }
3690dd37 7954 else if (attr_form_is_section_offset (attr))
8e19ed76 7955 {
4d3c2250 7956 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7957 }
7958 else
7959 {
4d3c2250
KB
7960 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7961 "common block member");
8e19ed76 7962 }
c906108c 7963 }
639d11d3 7964 if (die->child != NULL)
c906108c 7965 {
639d11d3 7966 child_die = die->child;
c906108c
SS
7967 while (child_die && child_die->tag)
7968 {
74ac6d43
TT
7969 LONGEST offset;
7970
e7c27a73 7971 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7972 if (sym != NULL
7973 && handle_data_member_location (child_die, cu, &offset))
c906108c 7974 {
74ac6d43 7975 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
7976 add_symbol_to_list (sym, &global_symbols);
7977 }
7978 child_die = sibling_die (child_die);
7979 }
7980 }
7981}
7982
0114d602 7983/* Create a type for a C++ namespace. */
d9fa45fe 7984
0114d602
DJ
7985static struct type *
7986read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7987{
e7c27a73 7988 struct objfile *objfile = cu->objfile;
0114d602 7989 const char *previous_prefix, *name;
9219021c 7990 int is_anonymous;
0114d602
DJ
7991 struct type *type;
7992
7993 /* For extensions, reuse the type of the original namespace. */
7994 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7995 {
7996 struct die_info *ext_die;
7997 struct dwarf2_cu *ext_cu = cu;
9a619af0 7998
0114d602
DJ
7999 ext_die = dwarf2_extension (die, &ext_cu);
8000 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8001
8002 /* EXT_CU may not be the same as CU.
8003 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8004 return set_die_type (die, type, cu);
8005 }
9219021c 8006
e142c38c 8007 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8008
8009 /* Now build the name of the current namespace. */
8010
0114d602
DJ
8011 previous_prefix = determine_prefix (die, cu);
8012 if (previous_prefix[0] != '\0')
8013 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8014 previous_prefix, name, 0, cu);
0114d602
DJ
8015
8016 /* Create the type. */
8017 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8018 objfile);
8019 TYPE_NAME (type) = (char *) name;
8020 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8021
60531b24 8022 return set_die_type (die, type, cu);
0114d602
DJ
8023}
8024
8025/* Read a C++ namespace. */
8026
8027static void
8028read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8029{
8030 struct objfile *objfile = cu->objfile;
0114d602 8031 int is_anonymous;
9219021c 8032
5c4e30ca
DC
8033 /* Add a symbol associated to this if we haven't seen the namespace
8034 before. Also, add a using directive if it's an anonymous
8035 namespace. */
9219021c 8036
f2f0e013 8037 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8038 {
8039 struct type *type;
8040
0114d602 8041 type = read_type_die (die, cu);
e7c27a73 8042 new_symbol (die, type, cu);
5c4e30ca 8043
e8e80198 8044 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8045 if (is_anonymous)
0114d602
DJ
8046 {
8047 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8048
c0cc3a76 8049 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8050 NULL, NULL, &objfile->objfile_obstack);
0114d602 8051 }
5c4e30ca 8052 }
9219021c 8053
639d11d3 8054 if (die->child != NULL)
d9fa45fe 8055 {
639d11d3 8056 struct die_info *child_die = die->child;
6e70227d 8057
d9fa45fe
DC
8058 while (child_die && child_die->tag)
8059 {
e7c27a73 8060 process_die (child_die, cu);
d9fa45fe
DC
8061 child_die = sibling_die (child_die);
8062 }
8063 }
38d518c9
EZ
8064}
8065
f55ee35c
JK
8066/* Read a Fortran module as type. This DIE can be only a declaration used for
8067 imported module. Still we need that type as local Fortran "use ... only"
8068 declaration imports depend on the created type in determine_prefix. */
8069
8070static struct type *
8071read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8072{
8073 struct objfile *objfile = cu->objfile;
8074 char *module_name;
8075 struct type *type;
8076
8077 module_name = dwarf2_name (die, cu);
8078 if (!module_name)
3e43a32a
MS
8079 complaint (&symfile_complaints,
8080 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8081 die->offset);
8082 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8083
8084 /* determine_prefix uses TYPE_TAG_NAME. */
8085 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8086
8087 return set_die_type (die, type, cu);
8088}
8089
5d7cb8df
JK
8090/* Read a Fortran module. */
8091
8092static void
8093read_module (struct die_info *die, struct dwarf2_cu *cu)
8094{
8095 struct die_info *child_die = die->child;
8096
5d7cb8df
JK
8097 while (child_die && child_die->tag)
8098 {
8099 process_die (child_die, cu);
8100 child_die = sibling_die (child_die);
8101 }
8102}
8103
38d518c9
EZ
8104/* Return the name of the namespace represented by DIE. Set
8105 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8106 namespace. */
8107
8108static const char *
e142c38c 8109namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8110{
8111 struct die_info *current_die;
8112 const char *name = NULL;
8113
8114 /* Loop through the extensions until we find a name. */
8115
8116 for (current_die = die;
8117 current_die != NULL;
f2f0e013 8118 current_die = dwarf2_extension (die, &cu))
38d518c9 8119 {
e142c38c 8120 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8121 if (name != NULL)
8122 break;
8123 }
8124
8125 /* Is it an anonymous namespace? */
8126
8127 *is_anonymous = (name == NULL);
8128 if (*is_anonymous)
2b1dbab0 8129 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8130
8131 return name;
d9fa45fe
DC
8132}
8133
c906108c
SS
8134/* Extract all information from a DW_TAG_pointer_type DIE and add to
8135 the user defined type vector. */
8136
f792889a 8137static struct type *
e7c27a73 8138read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8139{
5e2b427d 8140 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8141 struct comp_unit_head *cu_header = &cu->header;
c906108c 8142 struct type *type;
8b2dbe47
KB
8143 struct attribute *attr_byte_size;
8144 struct attribute *attr_address_class;
8145 int byte_size, addr_class;
7e314c57
JK
8146 struct type *target_type;
8147
8148 target_type = die_type (die, cu);
c906108c 8149
7e314c57
JK
8150 /* The die_type call above may have already set the type for this DIE. */
8151 type = get_die_type (die, cu);
8152 if (type)
8153 return type;
8154
8155 type = lookup_pointer_type (target_type);
8b2dbe47 8156
e142c38c 8157 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8158 if (attr_byte_size)
8159 byte_size = DW_UNSND (attr_byte_size);
c906108c 8160 else
8b2dbe47
KB
8161 byte_size = cu_header->addr_size;
8162
e142c38c 8163 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8164 if (attr_address_class)
8165 addr_class = DW_UNSND (attr_address_class);
8166 else
8167 addr_class = DW_ADDR_none;
8168
8169 /* If the pointer size or address class is different than the
8170 default, create a type variant marked as such and set the
8171 length accordingly. */
8172 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8173 {
5e2b427d 8174 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8175 {
8176 int type_flags;
8177
849957d9 8178 type_flags = gdbarch_address_class_type_flags
5e2b427d 8179 (gdbarch, byte_size, addr_class);
876cecd0
TT
8180 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8181 == 0);
8b2dbe47
KB
8182 type = make_type_with_address_space (type, type_flags);
8183 }
8184 else if (TYPE_LENGTH (type) != byte_size)
8185 {
3e43a32a
MS
8186 complaint (&symfile_complaints,
8187 _("invalid pointer size %d"), byte_size);
8b2dbe47 8188 }
6e70227d 8189 else
9a619af0
MS
8190 {
8191 /* Should we also complain about unhandled address classes? */
8192 }
c906108c 8193 }
8b2dbe47
KB
8194
8195 TYPE_LENGTH (type) = byte_size;
f792889a 8196 return set_die_type (die, type, cu);
c906108c
SS
8197}
8198
8199/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8200 the user defined type vector. */
8201
f792889a 8202static struct type *
e7c27a73 8203read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8204{
8205 struct type *type;
8206 struct type *to_type;
8207 struct type *domain;
8208
e7c27a73
DJ
8209 to_type = die_type (die, cu);
8210 domain = die_containing_type (die, cu);
0d5de010 8211
7e314c57
JK
8212 /* The calls above may have already set the type for this DIE. */
8213 type = get_die_type (die, cu);
8214 if (type)
8215 return type;
8216
0d5de010
DJ
8217 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8218 type = lookup_methodptr_type (to_type);
8219 else
8220 type = lookup_memberptr_type (to_type, domain);
c906108c 8221
f792889a 8222 return set_die_type (die, type, cu);
c906108c
SS
8223}
8224
8225/* Extract all information from a DW_TAG_reference_type DIE and add to
8226 the user defined type vector. */
8227
f792889a 8228static struct type *
e7c27a73 8229read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8230{
e7c27a73 8231 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8232 struct type *type, *target_type;
c906108c
SS
8233 struct attribute *attr;
8234
7e314c57
JK
8235 target_type = die_type (die, cu);
8236
8237 /* The die_type call above may have already set the type for this DIE. */
8238 type = get_die_type (die, cu);
8239 if (type)
8240 return type;
8241
8242 type = lookup_reference_type (target_type);
e142c38c 8243 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8244 if (attr)
8245 {
8246 TYPE_LENGTH (type) = DW_UNSND (attr);
8247 }
8248 else
8249 {
107d2387 8250 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8251 }
f792889a 8252 return set_die_type (die, type, cu);
c906108c
SS
8253}
8254
f792889a 8255static struct type *
e7c27a73 8256read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8257{
f792889a 8258 struct type *base_type, *cv_type;
c906108c 8259
e7c27a73 8260 base_type = die_type (die, cu);
7e314c57
JK
8261
8262 /* The die_type call above may have already set the type for this DIE. */
8263 cv_type = get_die_type (die, cu);
8264 if (cv_type)
8265 return cv_type;
8266
2f608a3a
KW
8267 /* In case the const qualifier is applied to an array type, the element type
8268 is so qualified, not the array type (section 6.7.3 of C99). */
8269 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8270 {
8271 struct type *el_type, *inner_array;
8272
8273 base_type = copy_type (base_type);
8274 inner_array = base_type;
8275
8276 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8277 {
8278 TYPE_TARGET_TYPE (inner_array) =
8279 copy_type (TYPE_TARGET_TYPE (inner_array));
8280 inner_array = TYPE_TARGET_TYPE (inner_array);
8281 }
8282
8283 el_type = TYPE_TARGET_TYPE (inner_array);
8284 TYPE_TARGET_TYPE (inner_array) =
8285 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8286
8287 return set_die_type (die, base_type, cu);
8288 }
8289
f792889a
DJ
8290 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8291 return set_die_type (die, cv_type, cu);
c906108c
SS
8292}
8293
f792889a 8294static struct type *
e7c27a73 8295read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8296{
f792889a 8297 struct type *base_type, *cv_type;
c906108c 8298
e7c27a73 8299 base_type = die_type (die, cu);
7e314c57
JK
8300
8301 /* The die_type call above may have already set the type for this DIE. */
8302 cv_type = get_die_type (die, cu);
8303 if (cv_type)
8304 return cv_type;
8305
f792889a
DJ
8306 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8307 return set_die_type (die, cv_type, cu);
c906108c
SS
8308}
8309
8310/* Extract all information from a DW_TAG_string_type DIE and add to
8311 the user defined type vector. It isn't really a user defined type,
8312 but it behaves like one, with other DIE's using an AT_user_def_type
8313 attribute to reference it. */
8314
f792889a 8315static struct type *
e7c27a73 8316read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8317{
e7c27a73 8318 struct objfile *objfile = cu->objfile;
3b7538c0 8319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8320 struct type *type, *range_type, *index_type, *char_type;
8321 struct attribute *attr;
8322 unsigned int length;
8323
e142c38c 8324 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8325 if (attr)
8326 {
8327 length = DW_UNSND (attr);
8328 }
8329 else
8330 {
0963b4bd 8331 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8332 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8333 if (attr)
8334 {
8335 length = DW_UNSND (attr);
8336 }
8337 else
8338 {
8339 length = 1;
8340 }
c906108c 8341 }
6ccb9162 8342
46bf5051 8343 index_type = objfile_type (objfile)->builtin_int;
c906108c 8344 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8345 char_type = language_string_char_type (cu->language_defn, gdbarch);
8346 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8347
f792889a 8348 return set_die_type (die, type, cu);
c906108c
SS
8349}
8350
8351/* Handle DIES due to C code like:
8352
8353 struct foo
c5aa993b
JM
8354 {
8355 int (*funcp)(int a, long l);
8356 int b;
8357 };
c906108c 8358
0963b4bd 8359 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8360
f792889a 8361static struct type *
e7c27a73 8362read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8363{
0963b4bd
MS
8364 struct type *type; /* Type that this function returns. */
8365 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8366 struct attribute *attr;
8367
e7c27a73 8368 type = die_type (die, cu);
7e314c57
JK
8369
8370 /* The die_type call above may have already set the type for this DIE. */
8371 ftype = get_die_type (die, cu);
8372 if (ftype)
8373 return ftype;
8374
0c8b41f1 8375 ftype = lookup_function_type (type);
c906108c 8376
5b8101ae 8377 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8378 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8379 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8380 || cu->language == language_cplus
5b8101ae
PM
8381 || cu->language == language_java
8382 || cu->language == language_pascal)
876cecd0 8383 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8384 else if (producer_is_realview (cu->producer))
8385 /* RealView does not emit DW_AT_prototyped. We can not
8386 distinguish prototyped and unprototyped functions; default to
8387 prototyped, since that is more common in modern code (and
8388 RealView warns about unprototyped functions). */
8389 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8390
c055b101
CV
8391 /* Store the calling convention in the type if it's available in
8392 the subroutine die. Otherwise set the calling convention to
8393 the default value DW_CC_normal. */
8394 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8395 if (attr)
8396 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8397 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8398 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8399 else
8400 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8401
8402 /* We need to add the subroutine type to the die immediately so
8403 we don't infinitely recurse when dealing with parameters
0963b4bd 8404 declared as the same subroutine type. */
76c10ea2 8405 set_die_type (die, ftype, cu);
6e70227d 8406
639d11d3 8407 if (die->child != NULL)
c906108c 8408 {
8072405b 8409 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8410 struct die_info *child_die;
8072405b 8411 int nparams, iparams;
c906108c
SS
8412
8413 /* Count the number of parameters.
8414 FIXME: GDB currently ignores vararg functions, but knows about
8415 vararg member functions. */
8072405b 8416 nparams = 0;
639d11d3 8417 child_die = die->child;
c906108c
SS
8418 while (child_die && child_die->tag)
8419 {
8420 if (child_die->tag == DW_TAG_formal_parameter)
8421 nparams++;
8422 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8423 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8424 child_die = sibling_die (child_die);
8425 }
8426
8427 /* Allocate storage for parameters and fill them in. */
8428 TYPE_NFIELDS (ftype) = nparams;
8429 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8430 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8431
8072405b
JK
8432 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8433 even if we error out during the parameters reading below. */
8434 for (iparams = 0; iparams < nparams; iparams++)
8435 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8436
8437 iparams = 0;
639d11d3 8438 child_die = die->child;
c906108c
SS
8439 while (child_die && child_die->tag)
8440 {
8441 if (child_die->tag == DW_TAG_formal_parameter)
8442 {
3ce3b1ba
PA
8443 struct type *arg_type;
8444
8445 /* DWARF version 2 has no clean way to discern C++
8446 static and non-static member functions. G++ helps
8447 GDB by marking the first parameter for non-static
8448 member functions (which is the this pointer) as
8449 artificial. We pass this information to
8450 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8451
8452 DWARF version 3 added DW_AT_object_pointer, which GCC
8453 4.5 does not yet generate. */
e142c38c 8454 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8455 if (attr)
8456 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8457 else
418835cc
KS
8458 {
8459 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8460
8461 /* GCC/43521: In java, the formal parameter
8462 "this" is sometimes not marked with DW_AT_artificial. */
8463 if (cu->language == language_java)
8464 {
8465 const char *name = dwarf2_name (child_die, cu);
9a619af0 8466
418835cc
KS
8467 if (name && !strcmp (name, "this"))
8468 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8469 }
8470 }
3ce3b1ba
PA
8471 arg_type = die_type (child_die, cu);
8472
8473 /* RealView does not mark THIS as const, which the testsuite
8474 expects. GCC marks THIS as const in method definitions,
8475 but not in the class specifications (GCC PR 43053). */
8476 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8477 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8478 {
8479 int is_this = 0;
8480 struct dwarf2_cu *arg_cu = cu;
8481 const char *name = dwarf2_name (child_die, cu);
8482
8483 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8484 if (attr)
8485 {
8486 /* If the compiler emits this, use it. */
8487 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8488 is_this = 1;
8489 }
8490 else if (name && strcmp (name, "this") == 0)
8491 /* Function definitions will have the argument names. */
8492 is_this = 1;
8493 else if (name == NULL && iparams == 0)
8494 /* Declarations may not have the names, so like
8495 elsewhere in GDB, assume an artificial first
8496 argument is "this". */
8497 is_this = 1;
8498
8499 if (is_this)
8500 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8501 arg_type, 0);
8502 }
8503
8504 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8505 iparams++;
8506 }
8507 child_die = sibling_die (child_die);
8508 }
8509 }
8510
76c10ea2 8511 return ftype;
c906108c
SS
8512}
8513
f792889a 8514static struct type *
e7c27a73 8515read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8516{
e7c27a73 8517 struct objfile *objfile = cu->objfile;
0114d602 8518 const char *name = NULL;
f792889a 8519 struct type *this_type;
c906108c 8520
94af9270 8521 name = dwarf2_full_name (NULL, die, cu);
f792889a 8522 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8523 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8524 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8525 set_die_type (die, this_type, cu);
8526 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8527 return this_type;
c906108c
SS
8528}
8529
8530/* Find a representation of a given base type and install
8531 it in the TYPE field of the die. */
8532
f792889a 8533static struct type *
e7c27a73 8534read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8535{
e7c27a73 8536 struct objfile *objfile = cu->objfile;
c906108c
SS
8537 struct type *type;
8538 struct attribute *attr;
8539 int encoding = 0, size = 0;
39cbfefa 8540 char *name;
6ccb9162
UW
8541 enum type_code code = TYPE_CODE_INT;
8542 int type_flags = 0;
8543 struct type *target_type = NULL;
c906108c 8544
e142c38c 8545 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8546 if (attr)
8547 {
8548 encoding = DW_UNSND (attr);
8549 }
e142c38c 8550 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8551 if (attr)
8552 {
8553 size = DW_UNSND (attr);
8554 }
39cbfefa 8555 name = dwarf2_name (die, cu);
6ccb9162 8556 if (!name)
c906108c 8557 {
6ccb9162
UW
8558 complaint (&symfile_complaints,
8559 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8560 }
6ccb9162
UW
8561
8562 switch (encoding)
c906108c 8563 {
6ccb9162
UW
8564 case DW_ATE_address:
8565 /* Turn DW_ATE_address into a void * pointer. */
8566 code = TYPE_CODE_PTR;
8567 type_flags |= TYPE_FLAG_UNSIGNED;
8568 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8569 break;
8570 case DW_ATE_boolean:
8571 code = TYPE_CODE_BOOL;
8572 type_flags |= TYPE_FLAG_UNSIGNED;
8573 break;
8574 case DW_ATE_complex_float:
8575 code = TYPE_CODE_COMPLEX;
8576 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8577 break;
8578 case DW_ATE_decimal_float:
8579 code = TYPE_CODE_DECFLOAT;
8580 break;
8581 case DW_ATE_float:
8582 code = TYPE_CODE_FLT;
8583 break;
8584 case DW_ATE_signed:
8585 break;
8586 case DW_ATE_unsigned:
8587 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8588 if (cu->language == language_fortran
8589 && name
8590 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8591 code = TYPE_CODE_CHAR;
6ccb9162
UW
8592 break;
8593 case DW_ATE_signed_char:
6e70227d 8594 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8595 || cu->language == language_pascal
8596 || cu->language == language_fortran)
6ccb9162
UW
8597 code = TYPE_CODE_CHAR;
8598 break;
8599 case DW_ATE_unsigned_char:
868a0084 8600 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8601 || cu->language == language_pascal
8602 || cu->language == language_fortran)
6ccb9162
UW
8603 code = TYPE_CODE_CHAR;
8604 type_flags |= TYPE_FLAG_UNSIGNED;
8605 break;
75079b2b
TT
8606 case DW_ATE_UTF:
8607 /* We just treat this as an integer and then recognize the
8608 type by name elsewhere. */
8609 break;
8610
6ccb9162
UW
8611 default:
8612 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8613 dwarf_type_encoding_name (encoding));
8614 break;
c906108c 8615 }
6ccb9162 8616
0114d602
DJ
8617 type = init_type (code, size, type_flags, NULL, objfile);
8618 TYPE_NAME (type) = name;
6ccb9162
UW
8619 TYPE_TARGET_TYPE (type) = target_type;
8620
0114d602 8621 if (name && strcmp (name, "char") == 0)
876cecd0 8622 TYPE_NOSIGN (type) = 1;
0114d602 8623
f792889a 8624 return set_die_type (die, type, cu);
c906108c
SS
8625}
8626
a02abb62
JB
8627/* Read the given DW_AT_subrange DIE. */
8628
f792889a 8629static struct type *
a02abb62
JB
8630read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8631{
8632 struct type *base_type;
8633 struct type *range_type;
8634 struct attribute *attr;
43bbcdc2
PH
8635 LONGEST low = 0;
8636 LONGEST high = -1;
39cbfefa 8637 char *name;
43bbcdc2 8638 LONGEST negative_mask;
e77813c8 8639
a02abb62 8640 base_type = die_type (die, cu);
953ac07e
JK
8641 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8642 check_typedef (base_type);
a02abb62 8643
7e314c57
JK
8644 /* The die_type call above may have already set the type for this DIE. */
8645 range_type = get_die_type (die, cu);
8646 if (range_type)
8647 return range_type;
8648
e142c38c 8649 if (cu->language == language_fortran)
6e70227d 8650 {
a02abb62
JB
8651 /* FORTRAN implies a lower bound of 1, if not given. */
8652 low = 1;
8653 }
8654
dd5e6932
DJ
8655 /* FIXME: For variable sized arrays either of these could be
8656 a variable rather than a constant value. We'll allow it,
8657 but we don't know how to handle it. */
e142c38c 8658 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8659 if (attr)
8660 low = dwarf2_get_attr_constant_value (attr, 0);
8661
e142c38c 8662 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8663 if (attr)
6e70227d 8664 {
d48323d8 8665 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8666 {
8667 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8668 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8669 FIXME: GDB does not yet know how to handle dynamic
8670 arrays properly, treat them as arrays with unspecified
8671 length for now.
8672
8673 FIXME: jimb/2003-09-22: GDB does not really know
8674 how to handle arrays of unspecified length
8675 either; we just represent them as zero-length
8676 arrays. Choose an appropriate upper bound given
8677 the lower bound we've computed above. */
8678 high = low - 1;
8679 }
8680 else
8681 high = dwarf2_get_attr_constant_value (attr, 1);
8682 }
e77813c8
PM
8683 else
8684 {
8685 attr = dwarf2_attr (die, DW_AT_count, cu);
8686 if (attr)
8687 {
8688 int count = dwarf2_get_attr_constant_value (attr, 1);
8689 high = low + count - 1;
8690 }
c2ff108b
JK
8691 else
8692 {
8693 /* Unspecified array length. */
8694 high = low - 1;
8695 }
e77813c8
PM
8696 }
8697
8698 /* Dwarf-2 specifications explicitly allows to create subrange types
8699 without specifying a base type.
8700 In that case, the base type must be set to the type of
8701 the lower bound, upper bound or count, in that order, if any of these
8702 three attributes references an object that has a type.
8703 If no base type is found, the Dwarf-2 specifications say that
8704 a signed integer type of size equal to the size of an address should
8705 be used.
8706 For the following C code: `extern char gdb_int [];'
8707 GCC produces an empty range DIE.
8708 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8709 high bound or count are not yet handled by this code. */
e77813c8
PM
8710 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8711 {
8712 struct objfile *objfile = cu->objfile;
8713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8714 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8715 struct type *int_type = objfile_type (objfile)->builtin_int;
8716
8717 /* Test "int", "long int", and "long long int" objfile types,
8718 and select the first one having a size above or equal to the
8719 architecture address size. */
8720 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8721 base_type = int_type;
8722 else
8723 {
8724 int_type = objfile_type (objfile)->builtin_long;
8725 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8726 base_type = int_type;
8727 else
8728 {
8729 int_type = objfile_type (objfile)->builtin_long_long;
8730 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8731 base_type = int_type;
8732 }
8733 }
8734 }
a02abb62 8735
6e70227d 8736 negative_mask =
43bbcdc2
PH
8737 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8738 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8739 low |= negative_mask;
8740 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8741 high |= negative_mask;
8742
a02abb62
JB
8743 range_type = create_range_type (NULL, base_type, low, high);
8744
bbb0eef6
JK
8745 /* Mark arrays with dynamic length at least as an array of unspecified
8746 length. GDB could check the boundary but before it gets implemented at
8747 least allow accessing the array elements. */
d48323d8 8748 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
8749 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8750
c2ff108b
JK
8751 /* Ada expects an empty array on no boundary attributes. */
8752 if (attr == NULL && cu->language != language_ada)
8753 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8754
39cbfefa
DJ
8755 name = dwarf2_name (die, cu);
8756 if (name)
8757 TYPE_NAME (range_type) = name;
6e70227d 8758
e142c38c 8759 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8760 if (attr)
8761 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8762
7e314c57
JK
8763 set_die_type (die, range_type, cu);
8764
8765 /* set_die_type should be already done. */
b4ba55a1
JB
8766 set_descriptive_type (range_type, die, cu);
8767
7e314c57 8768 return range_type;
a02abb62 8769}
6e70227d 8770
f792889a 8771static struct type *
81a17f79
JB
8772read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8773{
8774 struct type *type;
81a17f79 8775
81a17f79
JB
8776 /* For now, we only support the C meaning of an unspecified type: void. */
8777
0114d602
DJ
8778 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8779 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8780
f792889a 8781 return set_die_type (die, type, cu);
81a17f79 8782}
a02abb62 8783
51545339
DJ
8784/* Trivial hash function for die_info: the hash value of a DIE
8785 is its offset in .debug_info for this objfile. */
8786
8787static hashval_t
8788die_hash (const void *item)
8789{
8790 const struct die_info *die = item;
9a619af0 8791
51545339
DJ
8792 return die->offset;
8793}
8794
8795/* Trivial comparison function for die_info structures: two DIEs
8796 are equal if they have the same offset. */
8797
8798static int
8799die_eq (const void *item_lhs, const void *item_rhs)
8800{
8801 const struct die_info *die_lhs = item_lhs;
8802 const struct die_info *die_rhs = item_rhs;
9a619af0 8803
51545339
DJ
8804 return die_lhs->offset == die_rhs->offset;
8805}
8806
c906108c
SS
8807/* Read a whole compilation unit into a linked list of dies. */
8808
f9aca02d 8809static struct die_info *
93311388 8810read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8811{
93311388 8812 struct die_reader_specs reader_specs;
98bfdba5 8813 int read_abbrevs = 0;
1d9ec526 8814 struct cleanup *back_to = NULL;
98bfdba5
PA
8815 struct die_info *die;
8816
8817 if (cu->dwarf2_abbrevs == NULL)
8818 {
8819 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8820 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8821 read_abbrevs = 1;
8822 }
93311388 8823
348e048f 8824 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8825 cu->die_hash
8826 = htab_create_alloc_ex (cu->header.length / 12,
8827 die_hash,
8828 die_eq,
8829 NULL,
8830 &cu->comp_unit_obstack,
8831 hashtab_obstack_allocate,
8832 dummy_obstack_deallocate);
8833
93311388
DE
8834 init_cu_die_reader (&reader_specs, cu);
8835
98bfdba5
PA
8836 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8837
8838 if (read_abbrevs)
8839 do_cleanups (back_to);
8840
8841 return die;
639d11d3
DC
8842}
8843
d97bc12b
DE
8844/* Main entry point for reading a DIE and all children.
8845 Read the DIE and dump it if requested. */
8846
8847static struct die_info *
93311388
DE
8848read_die_and_children (const struct die_reader_specs *reader,
8849 gdb_byte *info_ptr,
d97bc12b
DE
8850 gdb_byte **new_info_ptr,
8851 struct die_info *parent)
8852{
93311388 8853 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8854 new_info_ptr, parent);
8855
8856 if (dwarf2_die_debug)
8857 {
348e048f
DE
8858 fprintf_unfiltered (gdb_stdlog,
8859 "\nRead die from %s of %s:\n",
8b70b953
TT
8860 (reader->cu->per_cu->debug_type_section
8861 ? ".debug_types"
8862 : ".debug_info"),
348e048f 8863 reader->abfd->filename);
d97bc12b
DE
8864 dump_die (result, dwarf2_die_debug);
8865 }
8866
8867 return result;
8868}
8869
639d11d3
DC
8870/* Read a single die and all its descendents. Set the die's sibling
8871 field to NULL; set other fields in the die correctly, and set all
8872 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8873 location of the info_ptr after reading all of those dies. PARENT
8874 is the parent of the die in question. */
8875
8876static struct die_info *
93311388
DE
8877read_die_and_children_1 (const struct die_reader_specs *reader,
8878 gdb_byte *info_ptr,
d97bc12b
DE
8879 gdb_byte **new_info_ptr,
8880 struct die_info *parent)
639d11d3
DC
8881{
8882 struct die_info *die;
fe1b8b76 8883 gdb_byte *cur_ptr;
639d11d3
DC
8884 int has_children;
8885
93311388 8886 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8887 if (die == NULL)
8888 {
8889 *new_info_ptr = cur_ptr;
8890 return NULL;
8891 }
93311388 8892 store_in_ref_table (die, reader->cu);
639d11d3
DC
8893
8894 if (has_children)
348e048f 8895 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8896 else
8897 {
8898 die->child = NULL;
8899 *new_info_ptr = cur_ptr;
8900 }
8901
8902 die->sibling = NULL;
8903 die->parent = parent;
8904 return die;
8905}
8906
8907/* Read a die, all of its descendents, and all of its siblings; set
8908 all of the fields of all of the dies correctly. Arguments are as
8909 in read_die_and_children. */
8910
8911static struct die_info *
93311388
DE
8912read_die_and_siblings (const struct die_reader_specs *reader,
8913 gdb_byte *info_ptr,
fe1b8b76 8914 gdb_byte **new_info_ptr,
639d11d3
DC
8915 struct die_info *parent)
8916{
8917 struct die_info *first_die, *last_sibling;
fe1b8b76 8918 gdb_byte *cur_ptr;
639d11d3 8919
c906108c 8920 cur_ptr = info_ptr;
639d11d3
DC
8921 first_die = last_sibling = NULL;
8922
8923 while (1)
c906108c 8924 {
639d11d3 8925 struct die_info *die
93311388 8926 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8927
1d325ec1 8928 if (die == NULL)
c906108c 8929 {
639d11d3
DC
8930 *new_info_ptr = cur_ptr;
8931 return first_die;
c906108c 8932 }
1d325ec1
DJ
8933
8934 if (!first_die)
8935 first_die = die;
c906108c 8936 else
1d325ec1
DJ
8937 last_sibling->sibling = die;
8938
8939 last_sibling = die;
c906108c 8940 }
c906108c
SS
8941}
8942
93311388
DE
8943/* Read the die from the .debug_info section buffer. Set DIEP to
8944 point to a newly allocated die with its information, except for its
8945 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8946 whether the die has children or not. */
8947
8948static gdb_byte *
8949read_full_die (const struct die_reader_specs *reader,
8950 struct die_info **diep, gdb_byte *info_ptr,
8951 int *has_children)
8952{
8953 unsigned int abbrev_number, bytes_read, i, offset;
8954 struct abbrev_info *abbrev;
8955 struct die_info *die;
8956 struct dwarf2_cu *cu = reader->cu;
8957 bfd *abfd = reader->abfd;
8958
8959 offset = info_ptr - reader->buffer;
8960 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8961 info_ptr += bytes_read;
8962 if (!abbrev_number)
8963 {
8964 *diep = NULL;
8965 *has_children = 0;
8966 return info_ptr;
8967 }
8968
8969 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8970 if (!abbrev)
348e048f
DE
8971 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8972 abbrev_number,
8973 bfd_get_filename (abfd));
8974
93311388
DE
8975 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8976 die->offset = offset;
8977 die->tag = abbrev->tag;
8978 die->abbrev = abbrev_number;
8979
8980 die->num_attrs = abbrev->num_attrs;
8981
8982 for (i = 0; i < abbrev->num_attrs; ++i)
8983 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8984 abfd, info_ptr, cu);
8985
8986 *diep = die;
8987 *has_children = abbrev->has_children;
8988 return info_ptr;
8989}
8990
c906108c
SS
8991/* In DWARF version 2, the description of the debugging information is
8992 stored in a separate .debug_abbrev section. Before we read any
8993 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8994 in a hash table. This function also sets flags in CU describing
8995 the data found in the abbrev table. */
c906108c
SS
8996
8997static void
e7c27a73 8998dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8999{
e7c27a73 9000 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9001 gdb_byte *abbrev_ptr;
c906108c
SS
9002 struct abbrev_info *cur_abbrev;
9003 unsigned int abbrev_number, bytes_read, abbrev_name;
9004 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9005 struct attr_abbrev *cur_attrs;
9006 unsigned int allocated_attrs;
c906108c 9007
0963b4bd 9008 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9009 obstack_init (&cu->abbrev_obstack);
9010 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9011 (ABBREV_HASH_SIZE
9012 * sizeof (struct abbrev_info *)));
9013 memset (cu->dwarf2_abbrevs, 0,
9014 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9015
be391dca
TT
9016 dwarf2_read_section (dwarf2_per_objfile->objfile,
9017 &dwarf2_per_objfile->abbrev);
dce234bc 9018 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9019 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9020 abbrev_ptr += bytes_read;
9021
f3dd6933
DJ
9022 allocated_attrs = ATTR_ALLOC_CHUNK;
9023 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9024
0963b4bd 9025 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9026 while (abbrev_number)
9027 {
f3dd6933 9028 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9029
9030 /* read in abbrev header */
9031 cur_abbrev->number = abbrev_number;
9032 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9033 abbrev_ptr += bytes_read;
9034 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9035 abbrev_ptr += 1;
9036
72bf9492
DJ
9037 if (cur_abbrev->tag == DW_TAG_namespace)
9038 cu->has_namespace_info = 1;
9039
c906108c
SS
9040 /* now read in declarations */
9041 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9042 abbrev_ptr += bytes_read;
9043 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9044 abbrev_ptr += bytes_read;
9045 while (abbrev_name)
9046 {
f3dd6933 9047 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9048 {
f3dd6933
DJ
9049 allocated_attrs += ATTR_ALLOC_CHUNK;
9050 cur_attrs
9051 = xrealloc (cur_attrs, (allocated_attrs
9052 * sizeof (struct attr_abbrev)));
c906108c 9053 }
ae038cb0
DJ
9054
9055 /* Record whether this compilation unit might have
9056 inter-compilation-unit references. If we don't know what form
9057 this attribute will have, then it might potentially be a
9058 DW_FORM_ref_addr, so we conservatively expect inter-CU
9059 references. */
9060
9061 if (abbrev_form == DW_FORM_ref_addr
9062 || abbrev_form == DW_FORM_indirect)
9063 cu->has_form_ref_addr = 1;
9064
f3dd6933
DJ
9065 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9066 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9067 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9068 abbrev_ptr += bytes_read;
9069 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9070 abbrev_ptr += bytes_read;
9071 }
9072
f3dd6933
DJ
9073 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9074 (cur_abbrev->num_attrs
9075 * sizeof (struct attr_abbrev)));
9076 memcpy (cur_abbrev->attrs, cur_attrs,
9077 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9078
c906108c 9079 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9080 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9081 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9082
9083 /* Get next abbreviation.
9084 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9085 always properly terminated with an abbrev number of 0.
9086 Exit loop if we encounter an abbreviation which we have
9087 already read (which means we are about to read the abbreviations
9088 for the next compile unit) or if the end of the abbreviation
9089 table is reached. */
dce234bc
PP
9090 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9091 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9092 break;
9093 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9094 abbrev_ptr += bytes_read;
e7c27a73 9095 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9096 break;
9097 }
f3dd6933
DJ
9098
9099 xfree (cur_attrs);
c906108c
SS
9100}
9101
f3dd6933 9102/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9103
c906108c 9104static void
f3dd6933 9105dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9106{
f3dd6933 9107 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9108
f3dd6933
DJ
9109 obstack_free (&cu->abbrev_obstack, NULL);
9110 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9111}
9112
9113/* Lookup an abbrev_info structure in the abbrev hash table. */
9114
9115static struct abbrev_info *
e7c27a73 9116dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9117{
9118 unsigned int hash_number;
9119 struct abbrev_info *abbrev;
9120
9121 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9122 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9123
9124 while (abbrev)
9125 {
9126 if (abbrev->number == number)
9127 return abbrev;
9128 else
9129 abbrev = abbrev->next;
9130 }
9131 return NULL;
9132}
9133
72bf9492
DJ
9134/* Returns nonzero if TAG represents a type that we might generate a partial
9135 symbol for. */
9136
9137static int
9138is_type_tag_for_partial (int tag)
9139{
9140 switch (tag)
9141 {
9142#if 0
9143 /* Some types that would be reasonable to generate partial symbols for,
9144 that we don't at present. */
9145 case DW_TAG_array_type:
9146 case DW_TAG_file_type:
9147 case DW_TAG_ptr_to_member_type:
9148 case DW_TAG_set_type:
9149 case DW_TAG_string_type:
9150 case DW_TAG_subroutine_type:
9151#endif
9152 case DW_TAG_base_type:
9153 case DW_TAG_class_type:
680b30c7 9154 case DW_TAG_interface_type:
72bf9492
DJ
9155 case DW_TAG_enumeration_type:
9156 case DW_TAG_structure_type:
9157 case DW_TAG_subrange_type:
9158 case DW_TAG_typedef:
9159 case DW_TAG_union_type:
9160 return 1;
9161 default:
9162 return 0;
9163 }
9164}
9165
9166/* Load all DIEs that are interesting for partial symbols into memory. */
9167
9168static struct partial_die_info *
93311388
DE
9169load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9170 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9171{
9172 struct partial_die_info *part_die;
9173 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9174 struct abbrev_info *abbrev;
9175 unsigned int bytes_read;
5afb4e99 9176 unsigned int load_all = 0;
72bf9492
DJ
9177
9178 int nesting_level = 1;
9179
9180 parent_die = NULL;
9181 last_die = NULL;
9182
5afb4e99
DJ
9183 if (cu->per_cu && cu->per_cu->load_all_dies)
9184 load_all = 1;
9185
72bf9492
DJ
9186 cu->partial_dies
9187 = htab_create_alloc_ex (cu->header.length / 12,
9188 partial_die_hash,
9189 partial_die_eq,
9190 NULL,
9191 &cu->comp_unit_obstack,
9192 hashtab_obstack_allocate,
9193 dummy_obstack_deallocate);
9194
9195 part_die = obstack_alloc (&cu->comp_unit_obstack,
9196 sizeof (struct partial_die_info));
9197
9198 while (1)
9199 {
9200 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9201
9202 /* A NULL abbrev means the end of a series of children. */
9203 if (abbrev == NULL)
9204 {
9205 if (--nesting_level == 0)
9206 {
9207 /* PART_DIE was probably the last thing allocated on the
9208 comp_unit_obstack, so we could call obstack_free
9209 here. We don't do that because the waste is small,
9210 and will be cleaned up when we're done with this
9211 compilation unit. This way, we're also more robust
9212 against other users of the comp_unit_obstack. */
9213 return first_die;
9214 }
9215 info_ptr += bytes_read;
9216 last_die = parent_die;
9217 parent_die = parent_die->die_parent;
9218 continue;
9219 }
9220
98bfdba5
PA
9221 /* Check for template arguments. We never save these; if
9222 they're seen, we just mark the parent, and go on our way. */
9223 if (parent_die != NULL
9224 && cu->language == language_cplus
9225 && (abbrev->tag == DW_TAG_template_type_param
9226 || abbrev->tag == DW_TAG_template_value_param))
9227 {
9228 parent_die->has_template_arguments = 1;
9229
9230 if (!load_all)
9231 {
9232 /* We don't need a partial DIE for the template argument. */
9233 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9234 cu);
9235 continue;
9236 }
9237 }
9238
9239 /* We only recurse into subprograms looking for template arguments.
9240 Skip their other children. */
9241 if (!load_all
9242 && cu->language == language_cplus
9243 && parent_die != NULL
9244 && parent_die->tag == DW_TAG_subprogram)
9245 {
9246 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9247 continue;
9248 }
9249
5afb4e99
DJ
9250 /* Check whether this DIE is interesting enough to save. Normally
9251 we would not be interested in members here, but there may be
9252 later variables referencing them via DW_AT_specification (for
9253 static members). */
9254 if (!load_all
9255 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9256 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9257 && abbrev->tag != DW_TAG_enumerator
9258 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9259 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9260 && abbrev->tag != DW_TAG_variable
5afb4e99 9261 && abbrev->tag != DW_TAG_namespace
f55ee35c 9262 && abbrev->tag != DW_TAG_module
5afb4e99 9263 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9264 {
9265 /* Otherwise we skip to the next sibling, if any. */
93311388 9266 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9267 continue;
9268 }
9269
93311388
DE
9270 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9271 buffer, info_ptr, cu);
72bf9492
DJ
9272
9273 /* This two-pass algorithm for processing partial symbols has a
9274 high cost in cache pressure. Thus, handle some simple cases
9275 here which cover the majority of C partial symbols. DIEs
9276 which neither have specification tags in them, nor could have
9277 specification tags elsewhere pointing at them, can simply be
9278 processed and discarded.
9279
9280 This segment is also optional; scan_partial_symbols and
9281 add_partial_symbol will handle these DIEs if we chain
9282 them in normally. When compilers which do not emit large
9283 quantities of duplicate debug information are more common,
9284 this code can probably be removed. */
9285
9286 /* Any complete simple types at the top level (pretty much all
9287 of them, for a language without namespaces), can be processed
9288 directly. */
9289 if (parent_die == NULL
9290 && part_die->has_specification == 0
9291 && part_die->is_declaration == 0
d8228535 9292 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9293 || part_die->tag == DW_TAG_base_type
9294 || part_die->tag == DW_TAG_subrange_type))
9295 {
9296 if (building_psymtab && part_die->name != NULL)
04a679b8 9297 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9298 VAR_DOMAIN, LOC_TYPEDEF,
9299 &cu->objfile->static_psymbols,
9300 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9301 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9302 continue;
9303 }
9304
d8228535
JK
9305 /* The exception for DW_TAG_typedef with has_children above is
9306 a workaround of GCC PR debug/47510. In the case of this complaint
9307 type_name_no_tag_or_error will error on such types later.
9308
9309 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9310 it could not find the child DIEs referenced later, this is checked
9311 above. In correct DWARF DW_TAG_typedef should have no children. */
9312
9313 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9314 complaint (&symfile_complaints,
9315 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9316 "- DIE at 0x%x [in module %s]"),
9317 part_die->offset, cu->objfile->name);
9318
72bf9492
DJ
9319 /* If we're at the second level, and we're an enumerator, and
9320 our parent has no specification (meaning possibly lives in a
9321 namespace elsewhere), then we can add the partial symbol now
9322 instead of queueing it. */
9323 if (part_die->tag == DW_TAG_enumerator
9324 && parent_die != NULL
9325 && parent_die->die_parent == NULL
9326 && parent_die->tag == DW_TAG_enumeration_type
9327 && parent_die->has_specification == 0)
9328 {
9329 if (part_die->name == NULL)
3e43a32a
MS
9330 complaint (&symfile_complaints,
9331 _("malformed enumerator DIE ignored"));
72bf9492 9332 else if (building_psymtab)
04a679b8 9333 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9334 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9335 (cu->language == language_cplus
9336 || cu->language == language_java)
72bf9492
DJ
9337 ? &cu->objfile->global_psymbols
9338 : &cu->objfile->static_psymbols,
9339 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9340
93311388 9341 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9342 continue;
9343 }
9344
9345 /* We'll save this DIE so link it in. */
9346 part_die->die_parent = parent_die;
9347 part_die->die_sibling = NULL;
9348 part_die->die_child = NULL;
9349
9350 if (last_die && last_die == parent_die)
9351 last_die->die_child = part_die;
9352 else if (last_die)
9353 last_die->die_sibling = part_die;
9354
9355 last_die = part_die;
9356
9357 if (first_die == NULL)
9358 first_die = part_die;
9359
9360 /* Maybe add the DIE to the hash table. Not all DIEs that we
9361 find interesting need to be in the hash table, because we
9362 also have the parent/sibling/child chains; only those that we
9363 might refer to by offset later during partial symbol reading.
9364
9365 For now this means things that might have be the target of a
9366 DW_AT_specification, DW_AT_abstract_origin, or
9367 DW_AT_extension. DW_AT_extension will refer only to
9368 namespaces; DW_AT_abstract_origin refers to functions (and
9369 many things under the function DIE, but we do not recurse
9370 into function DIEs during partial symbol reading) and
9371 possibly variables as well; DW_AT_specification refers to
9372 declarations. Declarations ought to have the DW_AT_declaration
9373 flag. It happens that GCC forgets to put it in sometimes, but
9374 only for functions, not for types.
9375
9376 Adding more things than necessary to the hash table is harmless
9377 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9378 wasted time in find_partial_die, when we reread the compilation
9379 unit with load_all_dies set. */
72bf9492 9380
5afb4e99 9381 if (load_all
72929c62 9382 || abbrev->tag == DW_TAG_constant
5afb4e99 9383 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9384 || abbrev->tag == DW_TAG_variable
9385 || abbrev->tag == DW_TAG_namespace
9386 || part_die->is_declaration)
9387 {
9388 void **slot;
9389
9390 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9391 part_die->offset, INSERT);
9392 *slot = part_die;
9393 }
9394
9395 part_die = obstack_alloc (&cu->comp_unit_obstack,
9396 sizeof (struct partial_die_info));
9397
9398 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9399 we have no reason to follow the children of structures; for other
98bfdba5
PA
9400 languages we have to, so that we can get at method physnames
9401 to infer fully qualified class names, for DW_AT_specification,
9402 and for C++ template arguments. For C++, we also look one level
9403 inside functions to find template arguments (if the name of the
9404 function does not already contain the template arguments).
bc30ff58
JB
9405
9406 For Ada, we need to scan the children of subprograms and lexical
9407 blocks as well because Ada allows the definition of nested
9408 entities that could be interesting for the debugger, such as
9409 nested subprograms for instance. */
72bf9492 9410 if (last_die->has_children
5afb4e99
DJ
9411 && (load_all
9412 || last_die->tag == DW_TAG_namespace
f55ee35c 9413 || last_die->tag == DW_TAG_module
72bf9492 9414 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9415 || (cu->language == language_cplus
9416 && last_die->tag == DW_TAG_subprogram
9417 && (last_die->name == NULL
9418 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9419 || (cu->language != language_c
9420 && (last_die->tag == DW_TAG_class_type
680b30c7 9421 || last_die->tag == DW_TAG_interface_type
72bf9492 9422 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9423 || last_die->tag == DW_TAG_union_type))
9424 || (cu->language == language_ada
9425 && (last_die->tag == DW_TAG_subprogram
9426 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9427 {
9428 nesting_level++;
9429 parent_die = last_die;
9430 continue;
9431 }
9432
9433 /* Otherwise we skip to the next sibling, if any. */
93311388 9434 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9435
9436 /* Back to the top, do it again. */
9437 }
9438}
9439
c906108c
SS
9440/* Read a minimal amount of information into the minimal die structure. */
9441
fe1b8b76 9442static gdb_byte *
72bf9492
DJ
9443read_partial_die (struct partial_die_info *part_die,
9444 struct abbrev_info *abbrev,
9445 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9446 gdb_byte *buffer, gdb_byte *info_ptr,
9447 struct dwarf2_cu *cu)
c906108c 9448{
fa238c03 9449 unsigned int i;
c906108c 9450 struct attribute attr;
c5aa993b 9451 int has_low_pc_attr = 0;
c906108c
SS
9452 int has_high_pc_attr = 0;
9453
72bf9492 9454 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9455
93311388 9456 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9457
9458 info_ptr += abbrev_len;
9459
9460 if (abbrev == NULL)
9461 return info_ptr;
9462
c906108c
SS
9463 part_die->tag = abbrev->tag;
9464 part_die->has_children = abbrev->has_children;
c906108c
SS
9465
9466 for (i = 0; i < abbrev->num_attrs; ++i)
9467 {
e7c27a73 9468 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9469
9470 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9471 partial symbol table. */
c906108c
SS
9472 switch (attr.name)
9473 {
9474 case DW_AT_name:
71c25dea
TT
9475 switch (part_die->tag)
9476 {
9477 case DW_TAG_compile_unit:
348e048f 9478 case DW_TAG_type_unit:
71c25dea
TT
9479 /* Compilation units have a DW_AT_name that is a filename, not
9480 a source language identifier. */
9481 case DW_TAG_enumeration_type:
9482 case DW_TAG_enumerator:
9483 /* These tags always have simple identifiers already; no need
9484 to canonicalize them. */
9485 part_die->name = DW_STRING (&attr);
9486 break;
9487 default:
9488 part_die->name
9489 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9490 &cu->objfile->objfile_obstack);
71c25dea
TT
9491 break;
9492 }
c906108c 9493 break;
31ef98ae 9494 case DW_AT_linkage_name:
c906108c 9495 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9496 /* Note that both forms of linkage name might appear. We
9497 assume they will be the same, and we only store the last
9498 one we see. */
94af9270
KS
9499 if (cu->language == language_ada)
9500 part_die->name = DW_STRING (&attr);
abc72ce4 9501 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9502 break;
9503 case DW_AT_low_pc:
9504 has_low_pc_attr = 1;
9505 part_die->lowpc = DW_ADDR (&attr);
9506 break;
9507 case DW_AT_high_pc:
9508 has_high_pc_attr = 1;
9509 part_die->highpc = DW_ADDR (&attr);
9510 break;
9511 case DW_AT_location:
0963b4bd 9512 /* Support the .debug_loc offsets. */
8e19ed76
PS
9513 if (attr_form_is_block (&attr))
9514 {
9515 part_die->locdesc = DW_BLOCK (&attr);
9516 }
3690dd37 9517 else if (attr_form_is_section_offset (&attr))
8e19ed76 9518 {
4d3c2250 9519 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9520 }
9521 else
9522 {
4d3c2250
KB
9523 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9524 "partial symbol information");
8e19ed76 9525 }
c906108c 9526 break;
c906108c
SS
9527 case DW_AT_external:
9528 part_die->is_external = DW_UNSND (&attr);
9529 break;
9530 case DW_AT_declaration:
9531 part_die->is_declaration = DW_UNSND (&attr);
9532 break;
9533 case DW_AT_type:
9534 part_die->has_type = 1;
9535 break;
9536 case DW_AT_abstract_origin:
9537 case DW_AT_specification:
72bf9492
DJ
9538 case DW_AT_extension:
9539 part_die->has_specification = 1;
c764a876 9540 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9541 break;
9542 case DW_AT_sibling:
9543 /* Ignore absolute siblings, they might point outside of
9544 the current compile unit. */
9545 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9546 complaint (&symfile_complaints,
9547 _("ignoring absolute DW_AT_sibling"));
c906108c 9548 else
93311388 9549 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9550 break;
fa4028e9
JB
9551 case DW_AT_byte_size:
9552 part_die->has_byte_size = 1;
9553 break;
68511cec
CES
9554 case DW_AT_calling_convention:
9555 /* DWARF doesn't provide a way to identify a program's source-level
9556 entry point. DW_AT_calling_convention attributes are only meant
9557 to describe functions' calling conventions.
9558
9559 However, because it's a necessary piece of information in
9560 Fortran, and because DW_CC_program is the only piece of debugging
9561 information whose definition refers to a 'main program' at all,
9562 several compilers have begun marking Fortran main programs with
9563 DW_CC_program --- even when those functions use the standard
9564 calling conventions.
9565
9566 So until DWARF specifies a way to provide this information and
9567 compilers pick up the new representation, we'll support this
9568 practice. */
9569 if (DW_UNSND (&attr) == DW_CC_program
9570 && cu->language == language_fortran)
01f8c46d
JK
9571 {
9572 set_main_name (part_die->name);
9573
9574 /* As this DIE has a static linkage the name would be difficult
9575 to look up later. */
9576 language_of_main = language_fortran;
9577 }
68511cec 9578 break;
c906108c
SS
9579 default:
9580 break;
9581 }
9582 }
9583
9373cf26
JK
9584 if (has_low_pc_attr && has_high_pc_attr)
9585 {
9586 /* When using the GNU linker, .gnu.linkonce. sections are used to
9587 eliminate duplicate copies of functions and vtables and such.
9588 The linker will arbitrarily choose one and discard the others.
9589 The AT_*_pc values for such functions refer to local labels in
9590 these sections. If the section from that file was discarded, the
9591 labels are not in the output, so the relocs get a value of 0.
9592 If this is a discarded function, mark the pc bounds as invalid,
9593 so that GDB will ignore it. */
9594 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9595 {
9596 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9597
9598 complaint (&symfile_complaints,
9599 _("DW_AT_low_pc %s is zero "
9600 "for DIE at 0x%x [in module %s]"),
9601 paddress (gdbarch, part_die->lowpc),
9602 part_die->offset, cu->objfile->name);
9603 }
9604 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9605 else if (part_die->lowpc >= part_die->highpc)
9606 {
9607 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9608
9609 complaint (&symfile_complaints,
9610 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9611 "for DIE at 0x%x [in module %s]"),
9612 paddress (gdbarch, part_die->lowpc),
9613 paddress (gdbarch, part_die->highpc),
9614 part_die->offset, cu->objfile->name);
9615 }
9616 else
9617 part_die->has_pc_info = 1;
9618 }
85cbf3d3 9619
c906108c
SS
9620 return info_ptr;
9621}
9622
72bf9492
DJ
9623/* Find a cached partial DIE at OFFSET in CU. */
9624
9625static struct partial_die_info *
c764a876 9626find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9627{
9628 struct partial_die_info *lookup_die = NULL;
9629 struct partial_die_info part_die;
9630
9631 part_die.offset = offset;
9632 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9633
72bf9492
DJ
9634 return lookup_die;
9635}
9636
348e048f
DE
9637/* Find a partial DIE at OFFSET, which may or may not be in CU,
9638 except in the case of .debug_types DIEs which do not reference
9639 outside their CU (they do however referencing other types via
55f1336d 9640 DW_FORM_ref_sig8). */
72bf9492
DJ
9641
9642static struct partial_die_info *
c764a876 9643find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9644{
5afb4e99
DJ
9645 struct dwarf2_per_cu_data *per_cu = NULL;
9646 struct partial_die_info *pd = NULL;
72bf9492 9647
8b70b953 9648 if (cu->per_cu->debug_type_section)
348e048f
DE
9649 {
9650 pd = find_partial_die_in_comp_unit (offset, cu);
9651 if (pd != NULL)
9652 return pd;
9653 goto not_found;
9654 }
9655
45452591 9656 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9657 {
9658 pd = find_partial_die_in_comp_unit (offset, cu);
9659 if (pd != NULL)
9660 return pd;
9661 }
72bf9492 9662
ae038cb0
DJ
9663 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9664
98bfdba5
PA
9665 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9666 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9667
9668 per_cu->cu->last_used = 0;
5afb4e99
DJ
9669 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9670
9671 if (pd == NULL && per_cu->load_all_dies == 0)
9672 {
9673 struct cleanup *back_to;
9674 struct partial_die_info comp_unit_die;
9675 struct abbrev_info *abbrev;
9676 unsigned int bytes_read;
9677 char *info_ptr;
9678
9679 per_cu->load_all_dies = 1;
9680
9681 /* Re-read the DIEs. */
9682 back_to = make_cleanup (null_cleanup, 0);
9683 if (per_cu->cu->dwarf2_abbrevs == NULL)
9684 {
9685 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9686 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9687 }
dce234bc 9688 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9689 + per_cu->cu->header.offset
9690 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9691 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9692 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9693 per_cu->cu->objfile->obfd,
9694 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9695 per_cu->cu);
9696 if (comp_unit_die.has_children)
93311388
DE
9697 load_partial_dies (per_cu->cu->objfile->obfd,
9698 dwarf2_per_objfile->info.buffer, info_ptr,
9699 0, per_cu->cu);
5afb4e99
DJ
9700 do_cleanups (back_to);
9701
9702 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9703 }
9704
348e048f
DE
9705 not_found:
9706
5afb4e99
DJ
9707 if (pd == NULL)
9708 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9709 _("could not find partial DIE 0x%x "
9710 "in cache [from module %s]\n"),
5afb4e99
DJ
9711 offset, bfd_get_filename (cu->objfile->obfd));
9712 return pd;
72bf9492
DJ
9713}
9714
abc72ce4
DE
9715/* See if we can figure out if the class lives in a namespace. We do
9716 this by looking for a member function; its demangled name will
9717 contain namespace info, if there is any. */
9718
9719static void
9720guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9721 struct dwarf2_cu *cu)
9722{
9723 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9724 what template types look like, because the demangler
9725 frequently doesn't give the same name as the debug info. We
9726 could fix this by only using the demangled name to get the
9727 prefix (but see comment in read_structure_type). */
9728
9729 struct partial_die_info *real_pdi;
9730 struct partial_die_info *child_pdi;
9731
9732 /* If this DIE (this DIE's specification, if any) has a parent, then
9733 we should not do this. We'll prepend the parent's fully qualified
9734 name when we create the partial symbol. */
9735
9736 real_pdi = struct_pdi;
9737 while (real_pdi->has_specification)
9738 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9739
9740 if (real_pdi->die_parent != NULL)
9741 return;
9742
9743 for (child_pdi = struct_pdi->die_child;
9744 child_pdi != NULL;
9745 child_pdi = child_pdi->die_sibling)
9746 {
9747 if (child_pdi->tag == DW_TAG_subprogram
9748 && child_pdi->linkage_name != NULL)
9749 {
9750 char *actual_class_name
9751 = language_class_name_from_physname (cu->language_defn,
9752 child_pdi->linkage_name);
9753 if (actual_class_name != NULL)
9754 {
9755 struct_pdi->name
9756 = obsavestring (actual_class_name,
9757 strlen (actual_class_name),
9758 &cu->objfile->objfile_obstack);
9759 xfree (actual_class_name);
9760 }
9761 break;
9762 }
9763 }
9764}
9765
72bf9492
DJ
9766/* Adjust PART_DIE before generating a symbol for it. This function
9767 may set the is_external flag or change the DIE's name. */
9768
9769static void
9770fixup_partial_die (struct partial_die_info *part_die,
9771 struct dwarf2_cu *cu)
9772{
abc72ce4
DE
9773 /* Once we've fixed up a die, there's no point in doing so again.
9774 This also avoids a memory leak if we were to call
9775 guess_partial_die_structure_name multiple times. */
9776 if (part_die->fixup_called)
9777 return;
9778
72bf9492
DJ
9779 /* If we found a reference attribute and the DIE has no name, try
9780 to find a name in the referred to DIE. */
9781
9782 if (part_die->name == NULL && part_die->has_specification)
9783 {
9784 struct partial_die_info *spec_die;
72bf9492 9785
10b3939b 9786 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9787
10b3939b 9788 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9789
9790 if (spec_die->name)
9791 {
9792 part_die->name = spec_die->name;
9793
9794 /* Copy DW_AT_external attribute if it is set. */
9795 if (spec_die->is_external)
9796 part_die->is_external = spec_die->is_external;
9797 }
9798 }
9799
9800 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9801
9802 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 9803 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 9804
abc72ce4
DE
9805 /* If there is no parent die to provide a namespace, and there are
9806 children, see if we can determine the namespace from their linkage
9807 name.
9808 NOTE: We need to do this even if cu->has_namespace_info != 0.
9809 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9810 if (cu->language == language_cplus
8b70b953 9811 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
9812 && part_die->die_parent == NULL
9813 && part_die->has_children
9814 && (part_die->tag == DW_TAG_class_type
9815 || part_die->tag == DW_TAG_structure_type
9816 || part_die->tag == DW_TAG_union_type))
9817 guess_partial_die_structure_name (part_die, cu);
9818
53832f31
TT
9819 /* GCC might emit a nameless struct or union that has a linkage
9820 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9821 if (part_die->name == NULL
9822 && (part_die->tag == DW_TAG_structure_type
9823 || part_die->tag == DW_TAG_union_type
9824 || part_die->tag == DW_TAG_class_type)
9825 && part_die->linkage_name != NULL)
9826 {
9827 char *demangled;
9828
9829 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9830 if (demangled)
9831 {
9832 part_die->name = obsavestring (demangled, strlen (demangled),
9833 &cu->objfile->objfile_obstack);
9834 xfree (demangled);
9835 }
9836 }
9837
abc72ce4 9838 part_die->fixup_called = 1;
72bf9492
DJ
9839}
9840
a8329558 9841/* Read an attribute value described by an attribute form. */
c906108c 9842
fe1b8b76 9843static gdb_byte *
a8329558 9844read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9845 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9846 struct dwarf2_cu *cu)
c906108c 9847{
e7c27a73 9848 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9849 unsigned int bytes_read;
9850 struct dwarf_block *blk;
9851
a8329558
KW
9852 attr->form = form;
9853 switch (form)
c906108c 9854 {
c906108c 9855 case DW_FORM_ref_addr:
ae411497
TT
9856 if (cu->header.version == 2)
9857 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9858 else
3e43a32a
MS
9859 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9860 &cu->header, &bytes_read);
ae411497
TT
9861 info_ptr += bytes_read;
9862 break;
9863 case DW_FORM_addr:
e7c27a73 9864 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9865 info_ptr += bytes_read;
c906108c
SS
9866 break;
9867 case DW_FORM_block2:
7b5a2f43 9868 blk = dwarf_alloc_block (cu);
c906108c
SS
9869 blk->size = read_2_bytes (abfd, info_ptr);
9870 info_ptr += 2;
9871 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9872 info_ptr += blk->size;
9873 DW_BLOCK (attr) = blk;
9874 break;
9875 case DW_FORM_block4:
7b5a2f43 9876 blk = dwarf_alloc_block (cu);
c906108c
SS
9877 blk->size = read_4_bytes (abfd, info_ptr);
9878 info_ptr += 4;
9879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9880 info_ptr += blk->size;
9881 DW_BLOCK (attr) = blk;
9882 break;
9883 case DW_FORM_data2:
9884 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9885 info_ptr += 2;
9886 break;
9887 case DW_FORM_data4:
9888 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9889 info_ptr += 4;
9890 break;
9891 case DW_FORM_data8:
9892 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9893 info_ptr += 8;
9894 break;
2dc7f7b3
TT
9895 case DW_FORM_sec_offset:
9896 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9897 info_ptr += bytes_read;
9898 break;
c906108c 9899 case DW_FORM_string:
9b1c24c8 9900 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9901 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9902 info_ptr += bytes_read;
9903 break;
4bdf3d34
JJ
9904 case DW_FORM_strp:
9905 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9906 &bytes_read);
8285870a 9907 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9908 info_ptr += bytes_read;
9909 break;
2dc7f7b3 9910 case DW_FORM_exprloc:
c906108c 9911 case DW_FORM_block:
7b5a2f43 9912 blk = dwarf_alloc_block (cu);
c906108c
SS
9913 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9914 info_ptr += bytes_read;
9915 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9916 info_ptr += blk->size;
9917 DW_BLOCK (attr) = blk;
9918 break;
9919 case DW_FORM_block1:
7b5a2f43 9920 blk = dwarf_alloc_block (cu);
c906108c
SS
9921 blk->size = read_1_byte (abfd, info_ptr);
9922 info_ptr += 1;
9923 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9924 info_ptr += blk->size;
9925 DW_BLOCK (attr) = blk;
9926 break;
9927 case DW_FORM_data1:
9928 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9929 info_ptr += 1;
9930 break;
9931 case DW_FORM_flag:
9932 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9933 info_ptr += 1;
9934 break;
2dc7f7b3
TT
9935 case DW_FORM_flag_present:
9936 DW_UNSND (attr) = 1;
9937 break;
c906108c
SS
9938 case DW_FORM_sdata:
9939 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9940 info_ptr += bytes_read;
9941 break;
9942 case DW_FORM_udata:
9943 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9944 info_ptr += bytes_read;
9945 break;
9946 case DW_FORM_ref1:
10b3939b 9947 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9948 info_ptr += 1;
9949 break;
9950 case DW_FORM_ref2:
10b3939b 9951 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9952 info_ptr += 2;
9953 break;
9954 case DW_FORM_ref4:
10b3939b 9955 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9956 info_ptr += 4;
9957 break;
613e1657 9958 case DW_FORM_ref8:
10b3939b 9959 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9960 info_ptr += 8;
9961 break;
55f1336d 9962 case DW_FORM_ref_sig8:
348e048f
DE
9963 /* Convert the signature to something we can record in DW_UNSND
9964 for later lookup.
9965 NOTE: This is NULL if the type wasn't found. */
9966 DW_SIGNATURED_TYPE (attr) =
9967 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9968 info_ptr += 8;
9969 break;
c906108c 9970 case DW_FORM_ref_udata:
10b3939b
DJ
9971 DW_ADDR (attr) = (cu->header.offset
9972 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9973 info_ptr += bytes_read;
9974 break;
c906108c 9975 case DW_FORM_indirect:
a8329558
KW
9976 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9977 info_ptr += bytes_read;
e7c27a73 9978 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9979 break;
c906108c 9980 default:
8a3fe4f8 9981 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9982 dwarf_form_name (form),
9983 bfd_get_filename (abfd));
c906108c 9984 }
28e94949
JB
9985
9986 /* We have seen instances where the compiler tried to emit a byte
9987 size attribute of -1 which ended up being encoded as an unsigned
9988 0xffffffff. Although 0xffffffff is technically a valid size value,
9989 an object of this size seems pretty unlikely so we can relatively
9990 safely treat these cases as if the size attribute was invalid and
9991 treat them as zero by default. */
9992 if (attr->name == DW_AT_byte_size
9993 && form == DW_FORM_data4
9994 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9995 {
9996 complaint
9997 (&symfile_complaints,
43bbcdc2
PH
9998 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9999 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10000 DW_UNSND (attr) = 0;
10001 }
28e94949 10002
c906108c
SS
10003 return info_ptr;
10004}
10005
a8329558
KW
10006/* Read an attribute described by an abbreviated attribute. */
10007
fe1b8b76 10008static gdb_byte *
a8329558 10009read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10010 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10011{
10012 attr->name = abbrev->name;
e7c27a73 10013 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10014}
10015
0963b4bd 10016/* Read dwarf information from a buffer. */
c906108c
SS
10017
10018static unsigned int
fe1b8b76 10019read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10020{
fe1b8b76 10021 return bfd_get_8 (abfd, buf);
c906108c
SS
10022}
10023
10024static int
fe1b8b76 10025read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10026{
fe1b8b76 10027 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10028}
10029
10030static unsigned int
fe1b8b76 10031read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10032{
fe1b8b76 10033 return bfd_get_16 (abfd, buf);
c906108c
SS
10034}
10035
10036static int
fe1b8b76 10037read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10038{
fe1b8b76 10039 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
10040}
10041
10042static unsigned int
fe1b8b76 10043read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10044{
fe1b8b76 10045 return bfd_get_32 (abfd, buf);
c906108c
SS
10046}
10047
10048static int
fe1b8b76 10049read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10050{
fe1b8b76 10051 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
10052}
10053
93311388 10054static ULONGEST
fe1b8b76 10055read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10056{
fe1b8b76 10057 return bfd_get_64 (abfd, buf);
c906108c
SS
10058}
10059
10060static CORE_ADDR
fe1b8b76 10061read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10062 unsigned int *bytes_read)
c906108c 10063{
e7c27a73 10064 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10065 CORE_ADDR retval = 0;
10066
107d2387 10067 if (cu_header->signed_addr_p)
c906108c 10068 {
107d2387
AC
10069 switch (cu_header->addr_size)
10070 {
10071 case 2:
fe1b8b76 10072 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10073 break;
10074 case 4:
fe1b8b76 10075 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10076 break;
10077 case 8:
fe1b8b76 10078 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10079 break;
10080 default:
8e65ff28 10081 internal_error (__FILE__, __LINE__,
e2e0b3e5 10082 _("read_address: bad switch, signed [in module %s]"),
659b0389 10083 bfd_get_filename (abfd));
107d2387
AC
10084 }
10085 }
10086 else
10087 {
10088 switch (cu_header->addr_size)
10089 {
10090 case 2:
fe1b8b76 10091 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10092 break;
10093 case 4:
fe1b8b76 10094 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10095 break;
10096 case 8:
fe1b8b76 10097 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10098 break;
10099 default:
8e65ff28 10100 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10101 _("read_address: bad switch, "
10102 "unsigned [in module %s]"),
659b0389 10103 bfd_get_filename (abfd));
107d2387 10104 }
c906108c 10105 }
64367e0a 10106
107d2387
AC
10107 *bytes_read = cu_header->addr_size;
10108 return retval;
c906108c
SS
10109}
10110
f7ef9339 10111/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10112 specification allows the initial length to take up either 4 bytes
10113 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10114 bytes describe the length and all offsets will be 8 bytes in length
10115 instead of 4.
10116
f7ef9339
KB
10117 An older, non-standard 64-bit format is also handled by this
10118 function. The older format in question stores the initial length
10119 as an 8-byte quantity without an escape value. Lengths greater
10120 than 2^32 aren't very common which means that the initial 4 bytes
10121 is almost always zero. Since a length value of zero doesn't make
10122 sense for the 32-bit format, this initial zero can be considered to
10123 be an escape value which indicates the presence of the older 64-bit
10124 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10125 greater than 4GB. If it becomes necessary to handle lengths
10126 somewhat larger than 4GB, we could allow other small values (such
10127 as the non-sensical values of 1, 2, and 3) to also be used as
10128 escape values indicating the presence of the old format.
f7ef9339 10129
917c78fc
MK
10130 The value returned via bytes_read should be used to increment the
10131 relevant pointer after calling read_initial_length().
c764a876 10132
613e1657
KB
10133 [ Note: read_initial_length() and read_offset() are based on the
10134 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10135 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10136 from:
10137
f7ef9339 10138 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10139
613e1657
KB
10140 This document is only a draft and is subject to change. (So beware.)
10141
f7ef9339 10142 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10143 determined empirically by examining 64-bit ELF files produced by
10144 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10145
10146 - Kevin, July 16, 2002
613e1657
KB
10147 ] */
10148
10149static LONGEST
c764a876 10150read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10151{
fe1b8b76 10152 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10153
dd373385 10154 if (length == 0xffffffff)
613e1657 10155 {
fe1b8b76 10156 length = bfd_get_64 (abfd, buf + 4);
613e1657 10157 *bytes_read = 12;
613e1657 10158 }
dd373385 10159 else if (length == 0)
f7ef9339 10160 {
dd373385 10161 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10162 length = bfd_get_64 (abfd, buf);
f7ef9339 10163 *bytes_read = 8;
f7ef9339 10164 }
613e1657
KB
10165 else
10166 {
10167 *bytes_read = 4;
613e1657
KB
10168 }
10169
c764a876
DE
10170 return length;
10171}
dd373385 10172
c764a876
DE
10173/* Cover function for read_initial_length.
10174 Returns the length of the object at BUF, and stores the size of the
10175 initial length in *BYTES_READ and stores the size that offsets will be in
10176 *OFFSET_SIZE.
10177 If the initial length size is not equivalent to that specified in
10178 CU_HEADER then issue a complaint.
10179 This is useful when reading non-comp-unit headers. */
dd373385 10180
c764a876
DE
10181static LONGEST
10182read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10183 const struct comp_unit_head *cu_header,
10184 unsigned int *bytes_read,
10185 unsigned int *offset_size)
10186{
10187 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10188
10189 gdb_assert (cu_header->initial_length_size == 4
10190 || cu_header->initial_length_size == 8
10191 || cu_header->initial_length_size == 12);
10192
10193 if (cu_header->initial_length_size != *bytes_read)
10194 complaint (&symfile_complaints,
10195 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10196
c764a876 10197 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10198 return length;
613e1657
KB
10199}
10200
10201/* Read an offset from the data stream. The size of the offset is
917c78fc 10202 given by cu_header->offset_size. */
613e1657
KB
10203
10204static LONGEST
fe1b8b76 10205read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10206 unsigned int *bytes_read)
c764a876
DE
10207{
10208 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10209
c764a876
DE
10210 *bytes_read = cu_header->offset_size;
10211 return offset;
10212}
10213
10214/* Read an offset from the data stream. */
10215
10216static LONGEST
10217read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10218{
10219 LONGEST retval = 0;
10220
c764a876 10221 switch (offset_size)
613e1657
KB
10222 {
10223 case 4:
fe1b8b76 10224 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10225 break;
10226 case 8:
fe1b8b76 10227 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10228 break;
10229 default:
8e65ff28 10230 internal_error (__FILE__, __LINE__,
c764a876 10231 _("read_offset_1: bad switch [in module %s]"),
659b0389 10232 bfd_get_filename (abfd));
613e1657
KB
10233 }
10234
917c78fc 10235 return retval;
613e1657
KB
10236}
10237
fe1b8b76
JB
10238static gdb_byte *
10239read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10240{
10241 /* If the size of a host char is 8 bits, we can return a pointer
10242 to the buffer, otherwise we have to copy the data to a buffer
10243 allocated on the temporary obstack. */
4bdf3d34 10244 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10245 return buf;
c906108c
SS
10246}
10247
10248static char *
9b1c24c8 10249read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10250{
10251 /* If the size of a host char is 8 bits, we can return a pointer
10252 to the string, otherwise we have to copy the string to a buffer
10253 allocated on the temporary obstack. */
4bdf3d34 10254 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10255 if (*buf == '\0')
10256 {
10257 *bytes_read_ptr = 1;
10258 return NULL;
10259 }
fe1b8b76
JB
10260 *bytes_read_ptr = strlen ((char *) buf) + 1;
10261 return (char *) buf;
4bdf3d34
JJ
10262}
10263
10264static char *
fe1b8b76 10265read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
10266 const struct comp_unit_head *cu_header,
10267 unsigned int *bytes_read_ptr)
10268{
c764a876 10269 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 10270
be391dca 10271 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10272 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 10273 {
8a3fe4f8 10274 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 10275 bfd_get_filename (abfd));
4bdf3d34 10276 return NULL;
c906108c 10277 }
dce234bc 10278 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 10279 {
3e43a32a
MS
10280 error (_("DW_FORM_strp pointing outside of "
10281 ".debug_str section [in module %s]"),
10282 bfd_get_filename (abfd));
c906108c
SS
10283 return NULL;
10284 }
4bdf3d34 10285 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10286 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10287 return NULL;
dce234bc 10288 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10289}
10290
ce5d95e1 10291static unsigned long
fe1b8b76 10292read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10293{
ce5d95e1
JB
10294 unsigned long result;
10295 unsigned int num_read;
c906108c
SS
10296 int i, shift;
10297 unsigned char byte;
10298
10299 result = 0;
10300 shift = 0;
10301 num_read = 0;
10302 i = 0;
10303 while (1)
10304 {
fe1b8b76 10305 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10306 buf++;
10307 num_read++;
ce5d95e1 10308 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10309 if ((byte & 128) == 0)
10310 {
10311 break;
10312 }
10313 shift += 7;
10314 }
10315 *bytes_read_ptr = num_read;
10316 return result;
10317}
10318
ce5d95e1 10319static long
fe1b8b76 10320read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10321{
ce5d95e1 10322 long result;
77e0b926 10323 int i, shift, num_read;
c906108c
SS
10324 unsigned char byte;
10325
10326 result = 0;
10327 shift = 0;
c906108c
SS
10328 num_read = 0;
10329 i = 0;
10330 while (1)
10331 {
fe1b8b76 10332 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10333 buf++;
10334 num_read++;
ce5d95e1 10335 result |= ((long)(byte & 127) << shift);
c906108c
SS
10336 shift += 7;
10337 if ((byte & 128) == 0)
10338 {
10339 break;
10340 }
10341 }
77e0b926
DJ
10342 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10343 result |= -(((long)1) << shift);
c906108c
SS
10344 *bytes_read_ptr = num_read;
10345 return result;
10346}
10347
4bb7a0a7
DJ
10348/* Return a pointer to just past the end of an LEB128 number in BUF. */
10349
fe1b8b76
JB
10350static gdb_byte *
10351skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10352{
10353 int byte;
10354
10355 while (1)
10356 {
fe1b8b76 10357 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10358 buf++;
10359 if ((byte & 128) == 0)
10360 return buf;
10361 }
10362}
10363
c906108c 10364static void
e142c38c 10365set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10366{
10367 switch (lang)
10368 {
10369 case DW_LANG_C89:
76bee0cc 10370 case DW_LANG_C99:
c906108c 10371 case DW_LANG_C:
e142c38c 10372 cu->language = language_c;
c906108c
SS
10373 break;
10374 case DW_LANG_C_plus_plus:
e142c38c 10375 cu->language = language_cplus;
c906108c 10376 break;
6aecb9c2
JB
10377 case DW_LANG_D:
10378 cu->language = language_d;
10379 break;
c906108c
SS
10380 case DW_LANG_Fortran77:
10381 case DW_LANG_Fortran90:
b21b22e0 10382 case DW_LANG_Fortran95:
e142c38c 10383 cu->language = language_fortran;
c906108c
SS
10384 break;
10385 case DW_LANG_Mips_Assembler:
e142c38c 10386 cu->language = language_asm;
c906108c 10387 break;
bebd888e 10388 case DW_LANG_Java:
e142c38c 10389 cu->language = language_java;
bebd888e 10390 break;
c906108c 10391 case DW_LANG_Ada83:
8aaf0b47 10392 case DW_LANG_Ada95:
bc5f45f8
JB
10393 cu->language = language_ada;
10394 break;
72019c9c
GM
10395 case DW_LANG_Modula2:
10396 cu->language = language_m2;
10397 break;
fe8e67fd
PM
10398 case DW_LANG_Pascal83:
10399 cu->language = language_pascal;
10400 break;
22566fbd
DJ
10401 case DW_LANG_ObjC:
10402 cu->language = language_objc;
10403 break;
c906108c
SS
10404 case DW_LANG_Cobol74:
10405 case DW_LANG_Cobol85:
c906108c 10406 default:
e142c38c 10407 cu->language = language_minimal;
c906108c
SS
10408 break;
10409 }
e142c38c 10410 cu->language_defn = language_def (cu->language);
c906108c
SS
10411}
10412
10413/* Return the named attribute or NULL if not there. */
10414
10415static struct attribute *
e142c38c 10416dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10417{
10418 unsigned int i;
10419 struct attribute *spec = NULL;
10420
10421 for (i = 0; i < die->num_attrs; ++i)
10422 {
10423 if (die->attrs[i].name == name)
10b3939b 10424 return &die->attrs[i];
c906108c
SS
10425 if (die->attrs[i].name == DW_AT_specification
10426 || die->attrs[i].name == DW_AT_abstract_origin)
10427 spec = &die->attrs[i];
10428 }
c906108c 10429
10b3939b 10430 if (spec)
f2f0e013
DJ
10431 {
10432 die = follow_die_ref (die, spec, &cu);
10433 return dwarf2_attr (die, name, cu);
10434 }
c5aa993b 10435
c906108c
SS
10436 return NULL;
10437}
10438
348e048f
DE
10439/* Return the named attribute or NULL if not there,
10440 but do not follow DW_AT_specification, etc.
10441 This is for use in contexts where we're reading .debug_types dies.
10442 Following DW_AT_specification, DW_AT_abstract_origin will take us
10443 back up the chain, and we want to go down. */
10444
10445static struct attribute *
10446dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10447 struct dwarf2_cu *cu)
10448{
10449 unsigned int i;
10450
10451 for (i = 0; i < die->num_attrs; ++i)
10452 if (die->attrs[i].name == name)
10453 return &die->attrs[i];
10454
10455 return NULL;
10456}
10457
05cf31d1
JB
10458/* Return non-zero iff the attribute NAME is defined for the given DIE,
10459 and holds a non-zero value. This function should only be used for
2dc7f7b3 10460 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10461
10462static int
10463dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10464{
10465 struct attribute *attr = dwarf2_attr (die, name, cu);
10466
10467 return (attr && DW_UNSND (attr));
10468}
10469
3ca72b44 10470static int
e142c38c 10471die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10472{
05cf31d1
JB
10473 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10474 which value is non-zero. However, we have to be careful with
10475 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10476 (via dwarf2_flag_true_p) follows this attribute. So we may
10477 end up accidently finding a declaration attribute that belongs
10478 to a different DIE referenced by the specification attribute,
10479 even though the given DIE does not have a declaration attribute. */
10480 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10481 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10482}
10483
63d06c5c 10484/* Return the die giving the specification for DIE, if there is
f2f0e013 10485 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10486 containing the return value on output. If there is no
10487 specification, but there is an abstract origin, that is
10488 returned. */
63d06c5c
DC
10489
10490static struct die_info *
f2f0e013 10491die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10492{
f2f0e013
DJ
10493 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10494 *spec_cu);
63d06c5c 10495
edb3359d
DJ
10496 if (spec_attr == NULL)
10497 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10498
63d06c5c
DC
10499 if (spec_attr == NULL)
10500 return NULL;
10501 else
f2f0e013 10502 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10503}
c906108c 10504
debd256d 10505/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10506 refers to.
10507 NOTE: This is also used as a "cleanup" function. */
10508
debd256d
JB
10509static void
10510free_line_header (struct line_header *lh)
10511{
10512 if (lh->standard_opcode_lengths)
a8bc7b56 10513 xfree (lh->standard_opcode_lengths);
debd256d
JB
10514
10515 /* Remember that all the lh->file_names[i].name pointers are
10516 pointers into debug_line_buffer, and don't need to be freed. */
10517 if (lh->file_names)
a8bc7b56 10518 xfree (lh->file_names);
debd256d
JB
10519
10520 /* Similarly for the include directory names. */
10521 if (lh->include_dirs)
a8bc7b56 10522 xfree (lh->include_dirs);
debd256d 10523
a8bc7b56 10524 xfree (lh);
debd256d
JB
10525}
10526
debd256d 10527/* Add an entry to LH's include directory table. */
ae2de4f8 10528
debd256d
JB
10529static void
10530add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10531{
debd256d
JB
10532 /* Grow the array if necessary. */
10533 if (lh->include_dirs_size == 0)
c5aa993b 10534 {
debd256d
JB
10535 lh->include_dirs_size = 1; /* for testing */
10536 lh->include_dirs = xmalloc (lh->include_dirs_size
10537 * sizeof (*lh->include_dirs));
10538 }
10539 else if (lh->num_include_dirs >= lh->include_dirs_size)
10540 {
10541 lh->include_dirs_size *= 2;
10542 lh->include_dirs = xrealloc (lh->include_dirs,
10543 (lh->include_dirs_size
10544 * sizeof (*lh->include_dirs)));
c5aa993b 10545 }
c906108c 10546
debd256d
JB
10547 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10548}
6e70227d 10549
debd256d 10550/* Add an entry to LH's file name table. */
ae2de4f8 10551
debd256d
JB
10552static void
10553add_file_name (struct line_header *lh,
10554 char *name,
10555 unsigned int dir_index,
10556 unsigned int mod_time,
10557 unsigned int length)
10558{
10559 struct file_entry *fe;
10560
10561 /* Grow the array if necessary. */
10562 if (lh->file_names_size == 0)
10563 {
10564 lh->file_names_size = 1; /* for testing */
10565 lh->file_names = xmalloc (lh->file_names_size
10566 * sizeof (*lh->file_names));
10567 }
10568 else if (lh->num_file_names >= lh->file_names_size)
10569 {
10570 lh->file_names_size *= 2;
10571 lh->file_names = xrealloc (lh->file_names,
10572 (lh->file_names_size
10573 * sizeof (*lh->file_names)));
10574 }
10575
10576 fe = &lh->file_names[lh->num_file_names++];
10577 fe->name = name;
10578 fe->dir_index = dir_index;
10579 fe->mod_time = mod_time;
10580 fe->length = length;
aaa75496 10581 fe->included_p = 0;
cb1df416 10582 fe->symtab = NULL;
debd256d 10583}
6e70227d 10584
debd256d 10585/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10586 .debug_line, according to the endianness of ABFD. Return a pointer
10587 to a struct line_header, allocated using xmalloc.
debd256d
JB
10588
10589 NOTE: the strings in the include directory and file name tables of
10590 the returned object point into debug_line_buffer, and must not be
10591 freed. */
ae2de4f8 10592
debd256d
JB
10593static struct line_header *
10594dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10595 struct dwarf2_cu *cu)
debd256d
JB
10596{
10597 struct cleanup *back_to;
10598 struct line_header *lh;
fe1b8b76 10599 gdb_byte *line_ptr;
c764a876 10600 unsigned int bytes_read, offset_size;
debd256d
JB
10601 int i;
10602 char *cur_dir, *cur_file;
10603
be391dca 10604 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10605 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10606 {
e2e0b3e5 10607 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10608 return 0;
10609 }
10610
a738430d
MK
10611 /* Make sure that at least there's room for the total_length field.
10612 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10613 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10614 {
4d3c2250 10615 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10616 return 0;
10617 }
10618
10619 lh = xmalloc (sizeof (*lh));
10620 memset (lh, 0, sizeof (*lh));
10621 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10622 (void *) lh);
10623
dce234bc 10624 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10625
a738430d 10626 /* Read in the header. */
6e70227d 10627 lh->total_length =
c764a876
DE
10628 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10629 &bytes_read, &offset_size);
debd256d 10630 line_ptr += bytes_read;
dce234bc
PP
10631 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10632 + dwarf2_per_objfile->line.size))
debd256d 10633 {
4d3c2250 10634 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10635 return 0;
10636 }
10637 lh->statement_program_end = line_ptr + lh->total_length;
10638 lh->version = read_2_bytes (abfd, line_ptr);
10639 line_ptr += 2;
c764a876
DE
10640 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10641 line_ptr += offset_size;
debd256d
JB
10642 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10643 line_ptr += 1;
2dc7f7b3
TT
10644 if (lh->version >= 4)
10645 {
10646 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10647 line_ptr += 1;
10648 }
10649 else
10650 lh->maximum_ops_per_instruction = 1;
10651
10652 if (lh->maximum_ops_per_instruction == 0)
10653 {
10654 lh->maximum_ops_per_instruction = 1;
10655 complaint (&symfile_complaints,
3e43a32a
MS
10656 _("invalid maximum_ops_per_instruction "
10657 "in `.debug_line' section"));
2dc7f7b3
TT
10658 }
10659
debd256d
JB
10660 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10661 line_ptr += 1;
10662 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10663 line_ptr += 1;
10664 lh->line_range = read_1_byte (abfd, line_ptr);
10665 line_ptr += 1;
10666 lh->opcode_base = read_1_byte (abfd, line_ptr);
10667 line_ptr += 1;
10668 lh->standard_opcode_lengths
fe1b8b76 10669 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10670
10671 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10672 for (i = 1; i < lh->opcode_base; ++i)
10673 {
10674 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10675 line_ptr += 1;
10676 }
10677
a738430d 10678 /* Read directory table. */
9b1c24c8 10679 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10680 {
10681 line_ptr += bytes_read;
10682 add_include_dir (lh, cur_dir);
10683 }
10684 line_ptr += bytes_read;
10685
a738430d 10686 /* Read file name table. */
9b1c24c8 10687 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10688 {
10689 unsigned int dir_index, mod_time, length;
10690
10691 line_ptr += bytes_read;
10692 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10693 line_ptr += bytes_read;
10694 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10695 line_ptr += bytes_read;
10696 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10697 line_ptr += bytes_read;
10698
10699 add_file_name (lh, cur_file, dir_index, mod_time, length);
10700 }
10701 line_ptr += bytes_read;
6e70227d 10702 lh->statement_program_start = line_ptr;
debd256d 10703
dce234bc
PP
10704 if (line_ptr > (dwarf2_per_objfile->line.buffer
10705 + dwarf2_per_objfile->line.size))
4d3c2250 10706 complaint (&symfile_complaints,
3e43a32a
MS
10707 _("line number info header doesn't "
10708 "fit in `.debug_line' section"));
debd256d
JB
10709
10710 discard_cleanups (back_to);
10711 return lh;
10712}
c906108c 10713
5fb290d7
DJ
10714/* This function exists to work around a bug in certain compilers
10715 (particularly GCC 2.95), in which the first line number marker of a
10716 function does not show up until after the prologue, right before
10717 the second line number marker. This function shifts ADDRESS down
10718 to the beginning of the function if necessary, and is called on
10719 addresses passed to record_line. */
10720
10721static CORE_ADDR
e142c38c 10722check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10723{
10724 struct function_range *fn;
10725
10726 /* Find the function_range containing address. */
e142c38c 10727 if (!cu->first_fn)
5fb290d7
DJ
10728 return address;
10729
e142c38c
DJ
10730 if (!cu->cached_fn)
10731 cu->cached_fn = cu->first_fn;
5fb290d7 10732
e142c38c 10733 fn = cu->cached_fn;
5fb290d7
DJ
10734 while (fn)
10735 if (fn->lowpc <= address && fn->highpc > address)
10736 goto found;
10737 else
10738 fn = fn->next;
10739
e142c38c
DJ
10740 fn = cu->first_fn;
10741 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10742 if (fn->lowpc <= address && fn->highpc > address)
10743 goto found;
10744 else
10745 fn = fn->next;
10746
10747 return address;
10748
10749 found:
10750 if (fn->seen_line)
10751 return address;
10752 if (address != fn->lowpc)
4d3c2250 10753 complaint (&symfile_complaints,
e2e0b3e5 10754 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10755 (unsigned long) address, fn->name);
5fb290d7
DJ
10756 fn->seen_line = 1;
10757 return fn->lowpc;
10758}
10759
c6da4cef
DE
10760/* Subroutine of dwarf_decode_lines to simplify it.
10761 Return the file name of the psymtab for included file FILE_INDEX
10762 in line header LH of PST.
10763 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10764 If space for the result is malloc'd, it will be freed by a cleanup.
10765 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10766
10767static char *
10768psymtab_include_file_name (const struct line_header *lh, int file_index,
10769 const struct partial_symtab *pst,
10770 const char *comp_dir)
10771{
10772 const struct file_entry fe = lh->file_names [file_index];
10773 char *include_name = fe.name;
10774 char *include_name_to_compare = include_name;
10775 char *dir_name = NULL;
72b9f47f
TT
10776 const char *pst_filename;
10777 char *copied_name = NULL;
c6da4cef
DE
10778 int file_is_pst;
10779
10780 if (fe.dir_index)
10781 dir_name = lh->include_dirs[fe.dir_index - 1];
10782
10783 if (!IS_ABSOLUTE_PATH (include_name)
10784 && (dir_name != NULL || comp_dir != NULL))
10785 {
10786 /* Avoid creating a duplicate psymtab for PST.
10787 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10788 Before we do the comparison, however, we need to account
10789 for DIR_NAME and COMP_DIR.
10790 First prepend dir_name (if non-NULL). If we still don't
10791 have an absolute path prepend comp_dir (if non-NULL).
10792 However, the directory we record in the include-file's
10793 psymtab does not contain COMP_DIR (to match the
10794 corresponding symtab(s)).
10795
10796 Example:
10797
10798 bash$ cd /tmp
10799 bash$ gcc -g ./hello.c
10800 include_name = "hello.c"
10801 dir_name = "."
10802 DW_AT_comp_dir = comp_dir = "/tmp"
10803 DW_AT_name = "./hello.c" */
10804
10805 if (dir_name != NULL)
10806 {
10807 include_name = concat (dir_name, SLASH_STRING,
10808 include_name, (char *)NULL);
10809 include_name_to_compare = include_name;
10810 make_cleanup (xfree, include_name);
10811 }
10812 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10813 {
10814 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10815 include_name, (char *)NULL);
10816 }
10817 }
10818
10819 pst_filename = pst->filename;
10820 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10821 {
72b9f47f
TT
10822 copied_name = concat (pst->dirname, SLASH_STRING,
10823 pst_filename, (char *)NULL);
10824 pst_filename = copied_name;
c6da4cef
DE
10825 }
10826
1e3fad37 10827 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10828
10829 if (include_name_to_compare != include_name)
10830 xfree (include_name_to_compare);
72b9f47f
TT
10831 if (copied_name != NULL)
10832 xfree (copied_name);
c6da4cef
DE
10833
10834 if (file_is_pst)
10835 return NULL;
10836 return include_name;
10837}
10838
c91513d8
PP
10839/* Ignore this record_line request. */
10840
10841static void
10842noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10843{
10844 return;
10845}
10846
aaa75496
JB
10847/* Decode the Line Number Program (LNP) for the given line_header
10848 structure and CU. The actual information extracted and the type
10849 of structures created from the LNP depends on the value of PST.
10850
10851 1. If PST is NULL, then this procedure uses the data from the program
10852 to create all necessary symbol tables, and their linetables.
6e70227d 10853
aaa75496
JB
10854 2. If PST is not NULL, this procedure reads the program to determine
10855 the list of files included by the unit represented by PST, and
c6da4cef
DE
10856 builds all the associated partial symbol tables.
10857
10858 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10859 It is used for relative paths in the line table.
10860 NOTE: When processing partial symtabs (pst != NULL),
10861 comp_dir == pst->dirname.
10862
10863 NOTE: It is important that psymtabs have the same file name (via strcmp)
10864 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10865 symtab we don't use it in the name of the psymtabs we create.
10866 E.g. expand_line_sal requires this when finding psymtabs to expand.
10867 A good testcase for this is mb-inline.exp. */
debd256d 10868
c906108c 10869static void
72b9f47f 10870dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10871 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10872{
a8c50c1f 10873 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10874 gdb_byte *line_end;
a8c50c1f 10875 unsigned int bytes_read, extended_len;
c906108c 10876 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10877 CORE_ADDR baseaddr;
10878 struct objfile *objfile = cu->objfile;
fbf65064 10879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10880 const int decode_for_pst_p = (pst != NULL);
cb1df416 10881 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10882 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10883 = record_line;
e142c38c
DJ
10884
10885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10886
debd256d
JB
10887 line_ptr = lh->statement_program_start;
10888 line_end = lh->statement_program_end;
c906108c
SS
10889
10890 /* Read the statement sequences until there's nothing left. */
10891 while (line_ptr < line_end)
10892 {
10893 /* state machine registers */
10894 CORE_ADDR address = 0;
10895 unsigned int file = 1;
10896 unsigned int line = 1;
10897 unsigned int column = 0;
debd256d 10898 int is_stmt = lh->default_is_stmt;
c906108c
SS
10899 int basic_block = 0;
10900 int end_sequence = 0;
fbf65064 10901 CORE_ADDR addr;
2dc7f7b3 10902 unsigned char op_index = 0;
c906108c 10903
aaa75496 10904 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10905 {
aaa75496 10906 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10907 /* lh->include_dirs and lh->file_names are 0-based, but the
10908 directory and file name numbers in the statement program
10909 are 1-based. */
10910 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10911 char *dir = NULL;
a738430d 10912
debd256d
JB
10913 if (fe->dir_index)
10914 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10915
10916 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10917 }
10918
a738430d 10919 /* Decode the table. */
c5aa993b 10920 while (!end_sequence)
c906108c
SS
10921 {
10922 op_code = read_1_byte (abfd, line_ptr);
10923 line_ptr += 1;
59205f5a
JB
10924 if (line_ptr > line_end)
10925 {
10926 dwarf2_debug_line_missing_end_sequence_complaint ();
10927 break;
10928 }
9aa1fe7e 10929
debd256d 10930 if (op_code >= lh->opcode_base)
6e70227d 10931 {
a738430d 10932 /* Special operand. */
debd256d 10933 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10934 address += (((op_index + (adj_opcode / lh->line_range))
10935 / lh->maximum_ops_per_instruction)
10936 * lh->minimum_instruction_length);
10937 op_index = ((op_index + (adj_opcode / lh->line_range))
10938 % lh->maximum_ops_per_instruction);
debd256d 10939 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10940 if (lh->num_file_names < file || file == 0)
25e43795 10941 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10942 /* For now we ignore lines not starting on an
10943 instruction boundary. */
10944 else if (op_index == 0)
25e43795
DJ
10945 {
10946 lh->file_names[file - 1].included_p = 1;
ca5f395d 10947 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10948 {
10949 if (last_subfile != current_subfile)
10950 {
10951 addr = gdbarch_addr_bits_remove (gdbarch, address);
10952 if (last_subfile)
c91513d8 10953 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10954 last_subfile = current_subfile;
10955 }
25e43795 10956 /* Append row to matrix using current values. */
fbf65064
UW
10957 addr = check_cu_functions (address, cu);
10958 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10959 (*p_record_line) (current_subfile, line, addr);
366da635 10960 }
25e43795 10961 }
ca5f395d 10962 basic_block = 0;
9aa1fe7e
GK
10963 }
10964 else switch (op_code)
c906108c
SS
10965 {
10966 case DW_LNS_extended_op:
3e43a32a
MS
10967 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10968 &bytes_read);
473b7be6 10969 line_ptr += bytes_read;
a8c50c1f 10970 extended_end = line_ptr + extended_len;
c906108c
SS
10971 extended_op = read_1_byte (abfd, line_ptr);
10972 line_ptr += 1;
10973 switch (extended_op)
10974 {
10975 case DW_LNE_end_sequence:
c91513d8 10976 p_record_line = record_line;
c906108c 10977 end_sequence = 1;
c906108c
SS
10978 break;
10979 case DW_LNE_set_address:
e7c27a73 10980 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10981
10982 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10983 {
10984 /* This line table is for a function which has been
10985 GCd by the linker. Ignore it. PR gdb/12528 */
10986
10987 long line_offset
10988 = line_ptr - dwarf2_per_objfile->line.buffer;
10989
10990 complaint (&symfile_complaints,
10991 _(".debug_line address at offset 0x%lx is 0 "
10992 "[in module %s]"),
10993 line_offset, cu->objfile->name);
10994 p_record_line = noop_record_line;
10995 }
10996
2dc7f7b3 10997 op_index = 0;
107d2387
AC
10998 line_ptr += bytes_read;
10999 address += baseaddr;
c906108c
SS
11000 break;
11001 case DW_LNE_define_file:
debd256d
JB
11002 {
11003 char *cur_file;
11004 unsigned int dir_index, mod_time, length;
6e70227d 11005
3e43a32a
MS
11006 cur_file = read_direct_string (abfd, line_ptr,
11007 &bytes_read);
debd256d
JB
11008 line_ptr += bytes_read;
11009 dir_index =
11010 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11011 line_ptr += bytes_read;
11012 mod_time =
11013 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11014 line_ptr += bytes_read;
11015 length =
11016 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11017 line_ptr += bytes_read;
11018 add_file_name (lh, cur_file, dir_index, mod_time, length);
11019 }
c906108c 11020 break;
d0c6ba3d
CC
11021 case DW_LNE_set_discriminator:
11022 /* The discriminator is not interesting to the debugger;
11023 just ignore it. */
11024 line_ptr = extended_end;
11025 break;
c906108c 11026 default:
4d3c2250 11027 complaint (&symfile_complaints,
e2e0b3e5 11028 _("mangled .debug_line section"));
debd256d 11029 return;
c906108c 11030 }
a8c50c1f
DJ
11031 /* Make sure that we parsed the extended op correctly. If e.g.
11032 we expected a different address size than the producer used,
11033 we may have read the wrong number of bytes. */
11034 if (line_ptr != extended_end)
11035 {
11036 complaint (&symfile_complaints,
11037 _("mangled .debug_line section"));
11038 return;
11039 }
c906108c
SS
11040 break;
11041 case DW_LNS_copy:
59205f5a 11042 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11043 dwarf2_debug_line_missing_file_complaint ();
11044 else
366da635 11045 {
25e43795 11046 lh->file_names[file - 1].included_p = 1;
ca5f395d 11047 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11048 {
11049 if (last_subfile != current_subfile)
11050 {
11051 addr = gdbarch_addr_bits_remove (gdbarch, address);
11052 if (last_subfile)
c91513d8 11053 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11054 last_subfile = current_subfile;
11055 }
11056 addr = check_cu_functions (address, cu);
11057 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11058 (*p_record_line) (current_subfile, line, addr);
fbf65064 11059 }
366da635 11060 }
c906108c
SS
11061 basic_block = 0;
11062 break;
11063 case DW_LNS_advance_pc:
2dc7f7b3
TT
11064 {
11065 CORE_ADDR adjust
11066 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11067
11068 address += (((op_index + adjust)
11069 / lh->maximum_ops_per_instruction)
11070 * lh->minimum_instruction_length);
11071 op_index = ((op_index + adjust)
11072 % lh->maximum_ops_per_instruction);
11073 line_ptr += bytes_read;
11074 }
c906108c
SS
11075 break;
11076 case DW_LNS_advance_line:
11077 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11078 line_ptr += bytes_read;
11079 break;
11080 case DW_LNS_set_file:
debd256d 11081 {
a738430d
MK
11082 /* The arrays lh->include_dirs and lh->file_names are
11083 0-based, but the directory and file name numbers in
11084 the statement program are 1-based. */
debd256d 11085 struct file_entry *fe;
4f1520fb 11086 char *dir = NULL;
a738430d 11087
debd256d
JB
11088 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11089 line_ptr += bytes_read;
59205f5a 11090 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11091 dwarf2_debug_line_missing_file_complaint ();
11092 else
11093 {
11094 fe = &lh->file_names[file - 1];
11095 if (fe->dir_index)
11096 dir = lh->include_dirs[fe->dir_index - 1];
11097 if (!decode_for_pst_p)
11098 {
11099 last_subfile = current_subfile;
11100 dwarf2_start_subfile (fe->name, dir, comp_dir);
11101 }
11102 }
debd256d 11103 }
c906108c
SS
11104 break;
11105 case DW_LNS_set_column:
11106 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11107 line_ptr += bytes_read;
11108 break;
11109 case DW_LNS_negate_stmt:
11110 is_stmt = (!is_stmt);
11111 break;
11112 case DW_LNS_set_basic_block:
11113 basic_block = 1;
11114 break;
c2c6d25f
JM
11115 /* Add to the address register of the state machine the
11116 address increment value corresponding to special opcode
a738430d
MK
11117 255. I.e., this value is scaled by the minimum
11118 instruction length since special opcode 255 would have
b021a221 11119 scaled the increment. */
c906108c 11120 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11121 {
11122 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11123
11124 address += (((op_index + adjust)
11125 / lh->maximum_ops_per_instruction)
11126 * lh->minimum_instruction_length);
11127 op_index = ((op_index + adjust)
11128 % lh->maximum_ops_per_instruction);
11129 }
c906108c
SS
11130 break;
11131 case DW_LNS_fixed_advance_pc:
11132 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11133 op_index = 0;
c906108c
SS
11134 line_ptr += 2;
11135 break;
9aa1fe7e 11136 default:
a738430d
MK
11137 {
11138 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11139 int i;
a738430d 11140
debd256d 11141 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11142 {
11143 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11144 line_ptr += bytes_read;
11145 }
11146 }
c906108c
SS
11147 }
11148 }
59205f5a
JB
11149 if (lh->num_file_names < file || file == 0)
11150 dwarf2_debug_line_missing_file_complaint ();
11151 else
11152 {
11153 lh->file_names[file - 1].included_p = 1;
11154 if (!decode_for_pst_p)
fbf65064
UW
11155 {
11156 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11157 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11158 }
59205f5a 11159 }
c906108c 11160 }
aaa75496
JB
11161
11162 if (decode_for_pst_p)
11163 {
11164 int file_index;
11165
11166 /* Now that we're done scanning the Line Header Program, we can
11167 create the psymtab of each included file. */
11168 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11169 if (lh->file_names[file_index].included_p == 1)
11170 {
c6da4cef
DE
11171 char *include_name =
11172 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11173 if (include_name != NULL)
aaa75496
JB
11174 dwarf2_create_include_psymtab (include_name, pst, objfile);
11175 }
11176 }
cb1df416
DJ
11177 else
11178 {
11179 /* Make sure a symtab is created for every file, even files
11180 which contain only variables (i.e. no code with associated
11181 line numbers). */
11182
11183 int i;
11184 struct file_entry *fe;
11185
11186 for (i = 0; i < lh->num_file_names; i++)
11187 {
11188 char *dir = NULL;
9a619af0 11189
cb1df416
DJ
11190 fe = &lh->file_names[i];
11191 if (fe->dir_index)
11192 dir = lh->include_dirs[fe->dir_index - 1];
11193 dwarf2_start_subfile (fe->name, dir, comp_dir);
11194
11195 /* Skip the main file; we don't need it, and it must be
11196 allocated last, so that it will show up before the
11197 non-primary symtabs in the objfile's symtab list. */
11198 if (current_subfile == first_subfile)
11199 continue;
11200
11201 if (current_subfile->symtab == NULL)
11202 current_subfile->symtab = allocate_symtab (current_subfile->name,
11203 cu->objfile);
11204 fe->symtab = current_subfile->symtab;
11205 }
11206 }
c906108c
SS
11207}
11208
11209/* Start a subfile for DWARF. FILENAME is the name of the file and
11210 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11211 or NULL if not known. COMP_DIR is the compilation directory for the
11212 linetable's compilation unit or NULL if not known.
c906108c
SS
11213 This routine tries to keep line numbers from identical absolute and
11214 relative file names in a common subfile.
11215
11216 Using the `list' example from the GDB testsuite, which resides in
11217 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11218 of /srcdir/list0.c yields the following debugging information for list0.c:
11219
c5aa993b
JM
11220 DW_AT_name: /srcdir/list0.c
11221 DW_AT_comp_dir: /compdir
357e46e7 11222 files.files[0].name: list0.h
c5aa993b 11223 files.files[0].dir: /srcdir
357e46e7 11224 files.files[1].name: list0.c
c5aa993b 11225 files.files[1].dir: /srcdir
c906108c
SS
11226
11227 The line number information for list0.c has to end up in a single
4f1520fb
FR
11228 subfile, so that `break /srcdir/list0.c:1' works as expected.
11229 start_subfile will ensure that this happens provided that we pass the
11230 concatenation of files.files[1].dir and files.files[1].name as the
11231 subfile's name. */
c906108c
SS
11232
11233static void
3e43a32a
MS
11234dwarf2_start_subfile (char *filename, const char *dirname,
11235 const char *comp_dir)
c906108c 11236{
4f1520fb
FR
11237 char *fullname;
11238
11239 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11240 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11241 second argument to start_subfile. To be consistent, we do the
11242 same here. In order not to lose the line information directory,
11243 we concatenate it to the filename when it makes sense.
11244 Note that the Dwarf3 standard says (speaking of filenames in line
11245 information): ``The directory index is ignored for file names
11246 that represent full path names''. Thus ignoring dirname in the
11247 `else' branch below isn't an issue. */
c906108c 11248
d5166ae1 11249 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11250 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11251 else
11252 fullname = filename;
c906108c 11253
4f1520fb
FR
11254 start_subfile (fullname, comp_dir);
11255
11256 if (fullname != filename)
11257 xfree (fullname);
c906108c
SS
11258}
11259
4c2df51b
DJ
11260static void
11261var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11262 struct dwarf2_cu *cu)
4c2df51b 11263{
e7c27a73
DJ
11264 struct objfile *objfile = cu->objfile;
11265 struct comp_unit_head *cu_header = &cu->header;
11266
4c2df51b
DJ
11267 /* NOTE drow/2003-01-30: There used to be a comment and some special
11268 code here to turn a symbol with DW_AT_external and a
11269 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11270 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11271 with some versions of binutils) where shared libraries could have
11272 relocations against symbols in their debug information - the
11273 minimal symbol would have the right address, but the debug info
11274 would not. It's no longer necessary, because we will explicitly
11275 apply relocations when we read in the debug information now. */
11276
11277 /* A DW_AT_location attribute with no contents indicates that a
11278 variable has been optimized away. */
11279 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11280 {
11281 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11282 return;
11283 }
11284
11285 /* Handle one degenerate form of location expression specially, to
11286 preserve GDB's previous behavior when section offsets are
11287 specified. If this is just a DW_OP_addr then mark this symbol
11288 as LOC_STATIC. */
11289
11290 if (attr_form_is_block (attr)
11291 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11292 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11293 {
891d2f0b 11294 unsigned int dummy;
4c2df51b
DJ
11295
11296 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11297 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11298 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11299 fixup_symbol_section (sym, objfile);
11300 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11301 SYMBOL_SECTION (sym));
4c2df51b
DJ
11302 return;
11303 }
11304
11305 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11306 expression evaluator, and use LOC_COMPUTED only when necessary
11307 (i.e. when the value of a register or memory location is
11308 referenced, or a thread-local block, etc.). Then again, it might
11309 not be worthwhile. I'm assuming that it isn't unless performance
11310 or memory numbers show me otherwise. */
11311
e7c27a73 11312 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11313 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11314
11315 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11316 cu->has_loclist = 1;
4c2df51b
DJ
11317}
11318
c906108c
SS
11319/* Given a pointer to a DWARF information entry, figure out if we need
11320 to make a symbol table entry for it, and if so, create a new entry
11321 and return a pointer to it.
11322 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11323 used the passed type.
11324 If SPACE is not NULL, use it to hold the new symbol. If it is
11325 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11326
11327static struct symbol *
34eaf542
TT
11328new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11329 struct symbol *space)
c906108c 11330{
e7c27a73 11331 struct objfile *objfile = cu->objfile;
c906108c
SS
11332 struct symbol *sym = NULL;
11333 char *name;
11334 struct attribute *attr = NULL;
11335 struct attribute *attr2 = NULL;
e142c38c 11336 CORE_ADDR baseaddr;
e37fd15a
SW
11337 struct pending **list_to_add = NULL;
11338
edb3359d 11339 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11340
11341 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11342
94af9270 11343 name = dwarf2_name (die, cu);
c906108c
SS
11344 if (name)
11345 {
94af9270 11346 const char *linkagename;
34eaf542 11347 int suppress_add = 0;
94af9270 11348
34eaf542
TT
11349 if (space)
11350 sym = space;
11351 else
11352 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11353 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11354
11355 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11356 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11357 linkagename = dwarf2_physname (name, die, cu);
11358 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11359
f55ee35c
JK
11360 /* Fortran does not have mangling standard and the mangling does differ
11361 between gfortran, iFort etc. */
11362 if (cu->language == language_fortran
b250c185 11363 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11364 symbol_set_demangled_name (&(sym->ginfo),
11365 (char *) dwarf2_full_name (name, die, cu),
11366 NULL);
f55ee35c 11367
c906108c 11368 /* Default assumptions.
c5aa993b 11369 Use the passed type or decode it from the die. */
176620f1 11370 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11371 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11372 if (type != NULL)
11373 SYMBOL_TYPE (sym) = type;
11374 else
e7c27a73 11375 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11376 attr = dwarf2_attr (die,
11377 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11378 cu);
c906108c
SS
11379 if (attr)
11380 {
11381 SYMBOL_LINE (sym) = DW_UNSND (attr);
11382 }
cb1df416 11383
edb3359d
DJ
11384 attr = dwarf2_attr (die,
11385 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11386 cu);
cb1df416
DJ
11387 if (attr)
11388 {
11389 int file_index = DW_UNSND (attr);
9a619af0 11390
cb1df416
DJ
11391 if (cu->line_header == NULL
11392 || file_index > cu->line_header->num_file_names)
11393 complaint (&symfile_complaints,
11394 _("file index out of range"));
1c3d648d 11395 else if (file_index > 0)
cb1df416
DJ
11396 {
11397 struct file_entry *fe;
9a619af0 11398
cb1df416
DJ
11399 fe = &cu->line_header->file_names[file_index - 1];
11400 SYMBOL_SYMTAB (sym) = fe->symtab;
11401 }
11402 }
11403
c906108c
SS
11404 switch (die->tag)
11405 {
11406 case DW_TAG_label:
e142c38c 11407 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11408 if (attr)
11409 {
11410 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11411 }
0f5238ed
TT
11412 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11413 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11414 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11415 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11416 break;
11417 case DW_TAG_subprogram:
11418 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11419 finish_block. */
11420 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11421 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11422 if ((attr2 && (DW_UNSND (attr2) != 0))
11423 || cu->language == language_ada)
c906108c 11424 {
2cfa0c8d
JB
11425 /* Subprograms marked external are stored as a global symbol.
11426 Ada subprograms, whether marked external or not, are always
11427 stored as a global symbol, because we want to be able to
11428 access them globally. For instance, we want to be able
11429 to break on a nested subprogram without having to
11430 specify the context. */
e37fd15a 11431 list_to_add = &global_symbols;
c906108c
SS
11432 }
11433 else
11434 {
e37fd15a 11435 list_to_add = cu->list_in_scope;
c906108c
SS
11436 }
11437 break;
edb3359d
DJ
11438 case DW_TAG_inlined_subroutine:
11439 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11440 finish_block. */
11441 SYMBOL_CLASS (sym) = LOC_BLOCK;
11442 SYMBOL_INLINED (sym) = 1;
11443 /* Do not add the symbol to any lists. It will be found via
11444 BLOCK_FUNCTION from the blockvector. */
11445 break;
34eaf542
TT
11446 case DW_TAG_template_value_param:
11447 suppress_add = 1;
11448 /* Fall through. */
72929c62 11449 case DW_TAG_constant:
c906108c 11450 case DW_TAG_variable:
254e6b9e 11451 case DW_TAG_member:
0963b4bd
MS
11452 /* Compilation with minimal debug info may result in
11453 variables with missing type entries. Change the
11454 misleading `void' type to something sensible. */
c906108c 11455 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11456 SYMBOL_TYPE (sym)
46bf5051 11457 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11458
e142c38c 11459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11460 /* In the case of DW_TAG_member, we should only be called for
11461 static const members. */
11462 if (die->tag == DW_TAG_member)
11463 {
3863f96c
DE
11464 /* dwarf2_add_field uses die_is_declaration,
11465 so we do the same. */
254e6b9e
DE
11466 gdb_assert (die_is_declaration (die, cu));
11467 gdb_assert (attr);
11468 }
c906108c
SS
11469 if (attr)
11470 {
e7c27a73 11471 dwarf2_const_value (attr, sym, cu);
e142c38c 11472 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11473 if (!suppress_add)
34eaf542
TT
11474 {
11475 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11476 list_to_add = &global_symbols;
34eaf542 11477 else
e37fd15a 11478 list_to_add = cu->list_in_scope;
34eaf542 11479 }
c906108c
SS
11480 break;
11481 }
e142c38c 11482 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11483 if (attr)
11484 {
e7c27a73 11485 var_decode_location (attr, sym, cu);
e142c38c 11486 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11487 if (SYMBOL_CLASS (sym) == LOC_STATIC
11488 && SYMBOL_VALUE_ADDRESS (sym) == 0
11489 && !dwarf2_per_objfile->has_section_at_zero)
11490 {
11491 /* When a static variable is eliminated by the linker,
11492 the corresponding debug information is not stripped
11493 out, but the variable address is set to null;
11494 do not add such variables into symbol table. */
11495 }
11496 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11497 {
f55ee35c
JK
11498 /* Workaround gfortran PR debug/40040 - it uses
11499 DW_AT_location for variables in -fPIC libraries which may
11500 get overriden by other libraries/executable and get
11501 a different address. Resolve it by the minimal symbol
11502 which may come from inferior's executable using copy
11503 relocation. Make this workaround only for gfortran as for
11504 other compilers GDB cannot guess the minimal symbol
11505 Fortran mangling kind. */
11506 if (cu->language == language_fortran && die->parent
11507 && die->parent->tag == DW_TAG_module
11508 && cu->producer
11509 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11510 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11511
1c809c68
TT
11512 /* A variable with DW_AT_external is never static,
11513 but it may be block-scoped. */
11514 list_to_add = (cu->list_in_scope == &file_symbols
11515 ? &global_symbols : cu->list_in_scope);
1c809c68 11516 }
c906108c 11517 else
e37fd15a 11518 list_to_add = cu->list_in_scope;
c906108c
SS
11519 }
11520 else
11521 {
11522 /* We do not know the address of this symbol.
c5aa993b
JM
11523 If it is an external symbol and we have type information
11524 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11525 The address of the variable will then be determined from
11526 the minimal symbol table whenever the variable is
11527 referenced. */
e142c38c 11528 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11529 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11530 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11531 {
0fe7935b
DJ
11532 /* A variable with DW_AT_external is never static, but it
11533 may be block-scoped. */
11534 list_to_add = (cu->list_in_scope == &file_symbols
11535 ? &global_symbols : cu->list_in_scope);
11536
c906108c 11537 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11538 }
442ddf59
JK
11539 else if (!die_is_declaration (die, cu))
11540 {
11541 /* Use the default LOC_OPTIMIZED_OUT class. */
11542 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11543 if (!suppress_add)
11544 list_to_add = cu->list_in_scope;
442ddf59 11545 }
c906108c
SS
11546 }
11547 break;
11548 case DW_TAG_formal_parameter:
edb3359d
DJ
11549 /* If we are inside a function, mark this as an argument. If
11550 not, we might be looking at an argument to an inlined function
11551 when we do not have enough information to show inlined frames;
11552 pretend it's a local variable in that case so that the user can
11553 still see it. */
11554 if (context_stack_depth > 0
11555 && context_stack[context_stack_depth - 1].name != NULL)
11556 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11557 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11558 if (attr)
11559 {
e7c27a73 11560 var_decode_location (attr, sym, cu);
c906108c 11561 }
e142c38c 11562 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11563 if (attr)
11564 {
e7c27a73 11565 dwarf2_const_value (attr, sym, cu);
c906108c 11566 }
f346a30d
PM
11567 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11568 if (attr && DW_UNSND (attr))
11569 {
11570 struct type *ref_type;
11571
11572 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11573 SYMBOL_TYPE (sym) = ref_type;
11574 }
11575
e37fd15a 11576 list_to_add = cu->list_in_scope;
c906108c
SS
11577 break;
11578 case DW_TAG_unspecified_parameters:
11579 /* From varargs functions; gdb doesn't seem to have any
11580 interest in this information, so just ignore it for now.
11581 (FIXME?) */
11582 break;
34eaf542
TT
11583 case DW_TAG_template_type_param:
11584 suppress_add = 1;
11585 /* Fall through. */
c906108c 11586 case DW_TAG_class_type:
680b30c7 11587 case DW_TAG_interface_type:
c906108c
SS
11588 case DW_TAG_structure_type:
11589 case DW_TAG_union_type:
72019c9c 11590 case DW_TAG_set_type:
c906108c
SS
11591 case DW_TAG_enumeration_type:
11592 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11593 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11594
63d06c5c 11595 {
987504bb 11596 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11597 really ever be static objects: otherwise, if you try
11598 to, say, break of a class's method and you're in a file
11599 which doesn't mention that class, it won't work unless
11600 the check for all static symbols in lookup_symbol_aux
11601 saves you. See the OtherFileClass tests in
11602 gdb.c++/namespace.exp. */
11603
e37fd15a 11604 if (!suppress_add)
34eaf542 11605 {
34eaf542
TT
11606 list_to_add = (cu->list_in_scope == &file_symbols
11607 && (cu->language == language_cplus
11608 || cu->language == language_java)
11609 ? &global_symbols : cu->list_in_scope);
63d06c5c 11610
64382290
TT
11611 /* The semantics of C++ state that "struct foo {
11612 ... }" also defines a typedef for "foo". A Java
11613 class declaration also defines a typedef for the
11614 class. */
11615 if (cu->language == language_cplus
11616 || cu->language == language_java
11617 || cu->language == language_ada)
11618 {
11619 /* The symbol's name is already allocated along
11620 with this objfile, so we don't need to
11621 duplicate it for the type. */
11622 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11623 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11624 }
63d06c5c
DC
11625 }
11626 }
c906108c
SS
11627 break;
11628 case DW_TAG_typedef:
63d06c5c
DC
11629 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11630 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11631 list_to_add = cu->list_in_scope;
63d06c5c 11632 break;
c906108c 11633 case DW_TAG_base_type:
a02abb62 11634 case DW_TAG_subrange_type:
c906108c 11635 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11636 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11637 list_to_add = cu->list_in_scope;
c906108c
SS
11638 break;
11639 case DW_TAG_enumerator:
e142c38c 11640 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11641 if (attr)
11642 {
e7c27a73 11643 dwarf2_const_value (attr, sym, cu);
c906108c 11644 }
63d06c5c
DC
11645 {
11646 /* NOTE: carlton/2003-11-10: See comment above in the
11647 DW_TAG_class_type, etc. block. */
11648
e142c38c 11649 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11650 && (cu->language == language_cplus
11651 || cu->language == language_java)
e142c38c 11652 ? &global_symbols : cu->list_in_scope);
63d06c5c 11653 }
c906108c 11654 break;
5c4e30ca
DC
11655 case DW_TAG_namespace:
11656 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11657 list_to_add = &global_symbols;
5c4e30ca 11658 break;
c906108c
SS
11659 default:
11660 /* Not a tag we recognize. Hopefully we aren't processing
11661 trash data, but since we must specifically ignore things
11662 we don't recognize, there is nothing else we should do at
0963b4bd 11663 this point. */
e2e0b3e5 11664 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11665 dwarf_tag_name (die->tag));
c906108c
SS
11666 break;
11667 }
df8a16a1 11668
e37fd15a
SW
11669 if (suppress_add)
11670 {
11671 sym->hash_next = objfile->template_symbols;
11672 objfile->template_symbols = sym;
11673 list_to_add = NULL;
11674 }
11675
11676 if (list_to_add != NULL)
11677 add_symbol_to_list (sym, list_to_add);
11678
df8a16a1
DJ
11679 /* For the benefit of old versions of GCC, check for anonymous
11680 namespaces based on the demangled name. */
11681 if (!processing_has_namespace_info
94af9270 11682 && cu->language == language_cplus)
df8a16a1 11683 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11684 }
11685 return (sym);
11686}
11687
34eaf542
TT
11688/* A wrapper for new_symbol_full that always allocates a new symbol. */
11689
11690static struct symbol *
11691new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11692{
11693 return new_symbol_full (die, type, cu, NULL);
11694}
11695
98bfdba5
PA
11696/* Given an attr with a DW_FORM_dataN value in host byte order,
11697 zero-extend it as appropriate for the symbol's type. The DWARF
11698 standard (v4) is not entirely clear about the meaning of using
11699 DW_FORM_dataN for a constant with a signed type, where the type is
11700 wider than the data. The conclusion of a discussion on the DWARF
11701 list was that this is unspecified. We choose to always zero-extend
11702 because that is the interpretation long in use by GCC. */
c906108c 11703
98bfdba5
PA
11704static gdb_byte *
11705dwarf2_const_value_data (struct attribute *attr, struct type *type,
11706 const char *name, struct obstack *obstack,
11707 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11708{
e7c27a73 11709 struct objfile *objfile = cu->objfile;
e17a4113
UW
11710 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11711 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11712 LONGEST l = DW_UNSND (attr);
11713
11714 if (bits < sizeof (*value) * 8)
11715 {
11716 l &= ((LONGEST) 1 << bits) - 1;
11717 *value = l;
11718 }
11719 else if (bits == sizeof (*value) * 8)
11720 *value = l;
11721 else
11722 {
11723 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11724 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11725 return bytes;
11726 }
11727
11728 return NULL;
11729}
11730
11731/* Read a constant value from an attribute. Either set *VALUE, or if
11732 the value does not fit in *VALUE, set *BYTES - either already
11733 allocated on the objfile obstack, or newly allocated on OBSTACK,
11734 or, set *BATON, if we translated the constant to a location
11735 expression. */
11736
11737static void
11738dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11739 const char *name, struct obstack *obstack,
11740 struct dwarf2_cu *cu,
11741 long *value, gdb_byte **bytes,
11742 struct dwarf2_locexpr_baton **baton)
11743{
11744 struct objfile *objfile = cu->objfile;
11745 struct comp_unit_head *cu_header = &cu->header;
c906108c 11746 struct dwarf_block *blk;
98bfdba5
PA
11747 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11748 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11749
11750 *value = 0;
11751 *bytes = NULL;
11752 *baton = NULL;
c906108c
SS
11753
11754 switch (attr->form)
11755 {
11756 case DW_FORM_addr:
ac56253d 11757 {
ac56253d
TT
11758 gdb_byte *data;
11759
98bfdba5
PA
11760 if (TYPE_LENGTH (type) != cu_header->addr_size)
11761 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11762 cu_header->addr_size,
98bfdba5 11763 TYPE_LENGTH (type));
ac56253d
TT
11764 /* Symbols of this form are reasonably rare, so we just
11765 piggyback on the existing location code rather than writing
11766 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11767 *baton = obstack_alloc (&objfile->objfile_obstack,
11768 sizeof (struct dwarf2_locexpr_baton));
11769 (*baton)->per_cu = cu->per_cu;
11770 gdb_assert ((*baton)->per_cu);
ac56253d 11771
98bfdba5
PA
11772 (*baton)->size = 2 + cu_header->addr_size;
11773 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11774 (*baton)->data = data;
ac56253d
TT
11775
11776 data[0] = DW_OP_addr;
11777 store_unsigned_integer (&data[1], cu_header->addr_size,
11778 byte_order, DW_ADDR (attr));
11779 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11780 }
c906108c 11781 break;
4ac36638 11782 case DW_FORM_string:
93b5768b 11783 case DW_FORM_strp:
98bfdba5
PA
11784 /* DW_STRING is already allocated on the objfile obstack, point
11785 directly to it. */
11786 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11787 break;
c906108c
SS
11788 case DW_FORM_block1:
11789 case DW_FORM_block2:
11790 case DW_FORM_block4:
11791 case DW_FORM_block:
2dc7f7b3 11792 case DW_FORM_exprloc:
c906108c 11793 blk = DW_BLOCK (attr);
98bfdba5
PA
11794 if (TYPE_LENGTH (type) != blk->size)
11795 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11796 TYPE_LENGTH (type));
11797 *bytes = blk->data;
c906108c 11798 break;
2df3850c
JM
11799
11800 /* The DW_AT_const_value attributes are supposed to carry the
11801 symbol's value "represented as it would be on the target
11802 architecture." By the time we get here, it's already been
11803 converted to host endianness, so we just need to sign- or
11804 zero-extend it as appropriate. */
11805 case DW_FORM_data1:
3e43a32a
MS
11806 *bytes = dwarf2_const_value_data (attr, type, name,
11807 obstack, cu, value, 8);
2df3850c 11808 break;
c906108c 11809 case DW_FORM_data2:
3e43a32a
MS
11810 *bytes = dwarf2_const_value_data (attr, type, name,
11811 obstack, cu, value, 16);
2df3850c 11812 break;
c906108c 11813 case DW_FORM_data4:
3e43a32a
MS
11814 *bytes = dwarf2_const_value_data (attr, type, name,
11815 obstack, cu, value, 32);
2df3850c 11816 break;
c906108c 11817 case DW_FORM_data8:
3e43a32a
MS
11818 *bytes = dwarf2_const_value_data (attr, type, name,
11819 obstack, cu, value, 64);
2df3850c
JM
11820 break;
11821
c906108c 11822 case DW_FORM_sdata:
98bfdba5 11823 *value = DW_SND (attr);
2df3850c
JM
11824 break;
11825
c906108c 11826 case DW_FORM_udata:
98bfdba5 11827 *value = DW_UNSND (attr);
c906108c 11828 break;
2df3850c 11829
c906108c 11830 default:
4d3c2250 11831 complaint (&symfile_complaints,
e2e0b3e5 11832 _("unsupported const value attribute form: '%s'"),
4d3c2250 11833 dwarf_form_name (attr->form));
98bfdba5 11834 *value = 0;
c906108c
SS
11835 break;
11836 }
11837}
11838
2df3850c 11839
98bfdba5
PA
11840/* Copy constant value from an attribute to a symbol. */
11841
2df3850c 11842static void
98bfdba5
PA
11843dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11844 struct dwarf2_cu *cu)
2df3850c 11845{
98bfdba5
PA
11846 struct objfile *objfile = cu->objfile;
11847 struct comp_unit_head *cu_header = &cu->header;
11848 long value;
11849 gdb_byte *bytes;
11850 struct dwarf2_locexpr_baton *baton;
2df3850c 11851
98bfdba5
PA
11852 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11853 SYMBOL_PRINT_NAME (sym),
11854 &objfile->objfile_obstack, cu,
11855 &value, &bytes, &baton);
2df3850c 11856
98bfdba5
PA
11857 if (baton != NULL)
11858 {
11859 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11860 SYMBOL_LOCATION_BATON (sym) = baton;
11861 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11862 }
11863 else if (bytes != NULL)
11864 {
11865 SYMBOL_VALUE_BYTES (sym) = bytes;
11866 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11867 }
11868 else
11869 {
11870 SYMBOL_VALUE (sym) = value;
11871 SYMBOL_CLASS (sym) = LOC_CONST;
11872 }
2df3850c
JM
11873}
11874
c906108c
SS
11875/* Return the type of the die in question using its DW_AT_type attribute. */
11876
11877static struct type *
e7c27a73 11878die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11879{
c906108c 11880 struct attribute *type_attr;
c906108c 11881
e142c38c 11882 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11883 if (!type_attr)
11884 {
11885 /* A missing DW_AT_type represents a void type. */
46bf5051 11886 return objfile_type (cu->objfile)->builtin_void;
c906108c 11887 }
348e048f 11888
673bfd45 11889 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11890}
11891
b4ba55a1
JB
11892/* True iff CU's producer generates GNAT Ada auxiliary information
11893 that allows to find parallel types through that information instead
11894 of having to do expensive parallel lookups by type name. */
11895
11896static int
11897need_gnat_info (struct dwarf2_cu *cu)
11898{
11899 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11900 of GNAT produces this auxiliary information, without any indication
11901 that it is produced. Part of enhancing the FSF version of GNAT
11902 to produce that information will be to put in place an indicator
11903 that we can use in order to determine whether the descriptive type
11904 info is available or not. One suggestion that has been made is
11905 to use a new attribute, attached to the CU die. For now, assume
11906 that the descriptive type info is not available. */
11907 return 0;
11908}
11909
b4ba55a1
JB
11910/* Return the auxiliary type of the die in question using its
11911 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11912 attribute is not present. */
11913
11914static struct type *
11915die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11916{
b4ba55a1 11917 struct attribute *type_attr;
b4ba55a1
JB
11918
11919 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11920 if (!type_attr)
11921 return NULL;
11922
673bfd45 11923 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11924}
11925
11926/* If DIE has a descriptive_type attribute, then set the TYPE's
11927 descriptive type accordingly. */
11928
11929static void
11930set_descriptive_type (struct type *type, struct die_info *die,
11931 struct dwarf2_cu *cu)
11932{
11933 struct type *descriptive_type = die_descriptive_type (die, cu);
11934
11935 if (descriptive_type)
11936 {
11937 ALLOCATE_GNAT_AUX_TYPE (type);
11938 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11939 }
11940}
11941
c906108c
SS
11942/* Return the containing type of the die in question using its
11943 DW_AT_containing_type attribute. */
11944
11945static struct type *
e7c27a73 11946die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11947{
c906108c 11948 struct attribute *type_attr;
c906108c 11949
e142c38c 11950 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11951 if (!type_attr)
11952 error (_("Dwarf Error: Problem turning containing type into gdb type "
11953 "[in module %s]"), cu->objfile->name);
11954
673bfd45 11955 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11956}
11957
673bfd45
DE
11958/* Look up the type of DIE in CU using its type attribute ATTR.
11959 If there is no type substitute an error marker. */
11960
c906108c 11961static struct type *
673bfd45
DE
11962lookup_die_type (struct die_info *die, struct attribute *attr,
11963 struct dwarf2_cu *cu)
c906108c 11964{
f792889a
DJ
11965 struct type *this_type;
11966
673bfd45
DE
11967 /* First see if we have it cached. */
11968
11969 if (is_ref_attr (attr))
11970 {
11971 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11972
11973 this_type = get_die_type_at_offset (offset, cu->per_cu);
11974 }
55f1336d 11975 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11976 {
11977 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11978 struct dwarf2_cu *sig_cu;
11979 unsigned int offset;
11980
11981 /* sig_type will be NULL if the signatured type is missing from
11982 the debug info. */
11983 if (sig_type == NULL)
11984 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11985 "at 0x%x [in module %s]"),
11986 die->offset, cu->objfile->name);
11987
8b70b953 11988 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 11989 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
11990 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11991 }
11992 else
11993 {
11994 dump_die_for_error (die);
11995 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11996 dwarf_attr_name (attr->name), cu->objfile->name);
11997 }
11998
11999 /* If not cached we need to read it in. */
12000
12001 if (this_type == NULL)
12002 {
12003 struct die_info *type_die;
12004 struct dwarf2_cu *type_cu = cu;
12005
12006 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12007 /* If the type is cached, we should have found it above. */
12008 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12009 this_type = read_type_die_1 (type_die, type_cu);
12010 }
12011
12012 /* If we still don't have a type use an error marker. */
12013
12014 if (this_type == NULL)
c906108c 12015 {
b00fdb78
TT
12016 char *message, *saved;
12017
12018 /* read_type_die already issued a complaint. */
12019 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12020 cu->objfile->name,
12021 cu->header.offset,
12022 die->offset);
12023 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12024 message, strlen (message));
12025 xfree (message);
12026
12027 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12028 }
673bfd45 12029
f792889a 12030 return this_type;
c906108c
SS
12031}
12032
673bfd45
DE
12033/* Return the type in DIE, CU.
12034 Returns NULL for invalid types.
12035
12036 This first does a lookup in the appropriate type_hash table,
12037 and only reads the die in if necessary.
12038
12039 NOTE: This can be called when reading in partial or full symbols. */
12040
f792889a 12041static struct type *
e7c27a73 12042read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12043{
f792889a
DJ
12044 struct type *this_type;
12045
12046 this_type = get_die_type (die, cu);
12047 if (this_type)
12048 return this_type;
12049
673bfd45
DE
12050 return read_type_die_1 (die, cu);
12051}
12052
12053/* Read the type in DIE, CU.
12054 Returns NULL for invalid types. */
12055
12056static struct type *
12057read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12058{
12059 struct type *this_type = NULL;
12060
c906108c
SS
12061 switch (die->tag)
12062 {
12063 case DW_TAG_class_type:
680b30c7 12064 case DW_TAG_interface_type:
c906108c
SS
12065 case DW_TAG_structure_type:
12066 case DW_TAG_union_type:
f792889a 12067 this_type = read_structure_type (die, cu);
c906108c
SS
12068 break;
12069 case DW_TAG_enumeration_type:
f792889a 12070 this_type = read_enumeration_type (die, cu);
c906108c
SS
12071 break;
12072 case DW_TAG_subprogram:
12073 case DW_TAG_subroutine_type:
edb3359d 12074 case DW_TAG_inlined_subroutine:
f792889a 12075 this_type = read_subroutine_type (die, cu);
c906108c
SS
12076 break;
12077 case DW_TAG_array_type:
f792889a 12078 this_type = read_array_type (die, cu);
c906108c 12079 break;
72019c9c 12080 case DW_TAG_set_type:
f792889a 12081 this_type = read_set_type (die, cu);
72019c9c 12082 break;
c906108c 12083 case DW_TAG_pointer_type:
f792889a 12084 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12085 break;
12086 case DW_TAG_ptr_to_member_type:
f792889a 12087 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12088 break;
12089 case DW_TAG_reference_type:
f792889a 12090 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12091 break;
12092 case DW_TAG_const_type:
f792889a 12093 this_type = read_tag_const_type (die, cu);
c906108c
SS
12094 break;
12095 case DW_TAG_volatile_type:
f792889a 12096 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12097 break;
12098 case DW_TAG_string_type:
f792889a 12099 this_type = read_tag_string_type (die, cu);
c906108c
SS
12100 break;
12101 case DW_TAG_typedef:
f792889a 12102 this_type = read_typedef (die, cu);
c906108c 12103 break;
a02abb62 12104 case DW_TAG_subrange_type:
f792889a 12105 this_type = read_subrange_type (die, cu);
a02abb62 12106 break;
c906108c 12107 case DW_TAG_base_type:
f792889a 12108 this_type = read_base_type (die, cu);
c906108c 12109 break;
81a17f79 12110 case DW_TAG_unspecified_type:
f792889a 12111 this_type = read_unspecified_type (die, cu);
81a17f79 12112 break;
0114d602
DJ
12113 case DW_TAG_namespace:
12114 this_type = read_namespace_type (die, cu);
12115 break;
f55ee35c
JK
12116 case DW_TAG_module:
12117 this_type = read_module_type (die, cu);
12118 break;
c906108c 12119 default:
3e43a32a
MS
12120 complaint (&symfile_complaints,
12121 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12122 dwarf_tag_name (die->tag));
c906108c
SS
12123 break;
12124 }
63d06c5c 12125
f792889a 12126 return this_type;
63d06c5c
DC
12127}
12128
abc72ce4
DE
12129/* See if we can figure out if the class lives in a namespace. We do
12130 this by looking for a member function; its demangled name will
12131 contain namespace info, if there is any.
12132 Return the computed name or NULL.
12133 Space for the result is allocated on the objfile's obstack.
12134 This is the full-die version of guess_partial_die_structure_name.
12135 In this case we know DIE has no useful parent. */
12136
12137static char *
12138guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12139{
12140 struct die_info *spec_die;
12141 struct dwarf2_cu *spec_cu;
12142 struct die_info *child;
12143
12144 spec_cu = cu;
12145 spec_die = die_specification (die, &spec_cu);
12146 if (spec_die != NULL)
12147 {
12148 die = spec_die;
12149 cu = spec_cu;
12150 }
12151
12152 for (child = die->child;
12153 child != NULL;
12154 child = child->sibling)
12155 {
12156 if (child->tag == DW_TAG_subprogram)
12157 {
12158 struct attribute *attr;
12159
12160 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12161 if (attr == NULL)
12162 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12163 if (attr != NULL)
12164 {
12165 char *actual_name
12166 = language_class_name_from_physname (cu->language_defn,
12167 DW_STRING (attr));
12168 char *name = NULL;
12169
12170 if (actual_name != NULL)
12171 {
12172 char *die_name = dwarf2_name (die, cu);
12173
12174 if (die_name != NULL
12175 && strcmp (die_name, actual_name) != 0)
12176 {
12177 /* Strip off the class name from the full name.
12178 We want the prefix. */
12179 int die_name_len = strlen (die_name);
12180 int actual_name_len = strlen (actual_name);
12181
12182 /* Test for '::' as a sanity check. */
12183 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12184 && actual_name[actual_name_len
12185 - die_name_len - 1] == ':')
abc72ce4
DE
12186 name =
12187 obsavestring (actual_name,
12188 actual_name_len - die_name_len - 2,
12189 &cu->objfile->objfile_obstack);
12190 }
12191 }
12192 xfree (actual_name);
12193 return name;
12194 }
12195 }
12196 }
12197
12198 return NULL;
12199}
12200
fdde2d81 12201/* Return the name of the namespace/class that DIE is defined within,
0114d602 12202 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12203
0114d602
DJ
12204 For example, if we're within the method foo() in the following
12205 code:
12206
12207 namespace N {
12208 class C {
12209 void foo () {
12210 }
12211 };
12212 }
12213
12214 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12215
12216static char *
e142c38c 12217determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12218{
0114d602
DJ
12219 struct die_info *parent, *spec_die;
12220 struct dwarf2_cu *spec_cu;
12221 struct type *parent_type;
63d06c5c 12222
f55ee35c
JK
12223 if (cu->language != language_cplus && cu->language != language_java
12224 && cu->language != language_fortran)
0114d602
DJ
12225 return "";
12226
12227 /* We have to be careful in the presence of DW_AT_specification.
12228 For example, with GCC 3.4, given the code
12229
12230 namespace N {
12231 void foo() {
12232 // Definition of N::foo.
12233 }
12234 }
12235
12236 then we'll have a tree of DIEs like this:
12237
12238 1: DW_TAG_compile_unit
12239 2: DW_TAG_namespace // N
12240 3: DW_TAG_subprogram // declaration of N::foo
12241 4: DW_TAG_subprogram // definition of N::foo
12242 DW_AT_specification // refers to die #3
12243
12244 Thus, when processing die #4, we have to pretend that we're in
12245 the context of its DW_AT_specification, namely the contex of die
12246 #3. */
12247 spec_cu = cu;
12248 spec_die = die_specification (die, &spec_cu);
12249 if (spec_die == NULL)
12250 parent = die->parent;
12251 else
63d06c5c 12252 {
0114d602
DJ
12253 parent = spec_die->parent;
12254 cu = spec_cu;
63d06c5c 12255 }
0114d602
DJ
12256
12257 if (parent == NULL)
12258 return "";
98bfdba5
PA
12259 else if (parent->building_fullname)
12260 {
12261 const char *name;
12262 const char *parent_name;
12263
12264 /* It has been seen on RealView 2.2 built binaries,
12265 DW_TAG_template_type_param types actually _defined_ as
12266 children of the parent class:
12267
12268 enum E {};
12269 template class <class Enum> Class{};
12270 Class<enum E> class_e;
12271
12272 1: DW_TAG_class_type (Class)
12273 2: DW_TAG_enumeration_type (E)
12274 3: DW_TAG_enumerator (enum1:0)
12275 3: DW_TAG_enumerator (enum2:1)
12276 ...
12277 2: DW_TAG_template_type_param
12278 DW_AT_type DW_FORM_ref_udata (E)
12279
12280 Besides being broken debug info, it can put GDB into an
12281 infinite loop. Consider:
12282
12283 When we're building the full name for Class<E>, we'll start
12284 at Class, and go look over its template type parameters,
12285 finding E. We'll then try to build the full name of E, and
12286 reach here. We're now trying to build the full name of E,
12287 and look over the parent DIE for containing scope. In the
12288 broken case, if we followed the parent DIE of E, we'd again
12289 find Class, and once again go look at its template type
12290 arguments, etc., etc. Simply don't consider such parent die
12291 as source-level parent of this die (it can't be, the language
12292 doesn't allow it), and break the loop here. */
12293 name = dwarf2_name (die, cu);
12294 parent_name = dwarf2_name (parent, cu);
12295 complaint (&symfile_complaints,
12296 _("template param type '%s' defined within parent '%s'"),
12297 name ? name : "<unknown>",
12298 parent_name ? parent_name : "<unknown>");
12299 return "";
12300 }
63d06c5c 12301 else
0114d602
DJ
12302 switch (parent->tag)
12303 {
63d06c5c 12304 case DW_TAG_namespace:
0114d602 12305 parent_type = read_type_die (parent, cu);
acebe513
UW
12306 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12307 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12308 Work around this problem here. */
12309 if (cu->language == language_cplus
12310 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12311 return "";
0114d602
DJ
12312 /* We give a name to even anonymous namespaces. */
12313 return TYPE_TAG_NAME (parent_type);
63d06c5c 12314 case DW_TAG_class_type:
680b30c7 12315 case DW_TAG_interface_type:
63d06c5c 12316 case DW_TAG_structure_type:
0114d602 12317 case DW_TAG_union_type:
f55ee35c 12318 case DW_TAG_module:
0114d602
DJ
12319 parent_type = read_type_die (parent, cu);
12320 if (TYPE_TAG_NAME (parent_type) != NULL)
12321 return TYPE_TAG_NAME (parent_type);
12322 else
12323 /* An anonymous structure is only allowed non-static data
12324 members; no typedefs, no member functions, et cetera.
12325 So it does not need a prefix. */
12326 return "";
abc72ce4
DE
12327 case DW_TAG_compile_unit:
12328 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12329 if (cu->language == language_cplus
8b70b953 12330 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12331 && die->child != NULL
12332 && (die->tag == DW_TAG_class_type
12333 || die->tag == DW_TAG_structure_type
12334 || die->tag == DW_TAG_union_type))
12335 {
12336 char *name = guess_full_die_structure_name (die, cu);
12337 if (name != NULL)
12338 return name;
12339 }
12340 return "";
63d06c5c 12341 default:
8176b9b8 12342 return determine_prefix (parent, cu);
63d06c5c 12343 }
63d06c5c
DC
12344}
12345
3e43a32a
MS
12346/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12347 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12348 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12349 an obconcat, otherwise allocate storage for the result. The CU argument is
12350 used to determine the language and hence, the appropriate separator. */
987504bb 12351
f55ee35c 12352#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12353
12354static char *
f55ee35c
JK
12355typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12356 int physname, struct dwarf2_cu *cu)
63d06c5c 12357{
f55ee35c 12358 const char *lead = "";
5c315b68 12359 const char *sep;
63d06c5c 12360
3e43a32a
MS
12361 if (suffix == NULL || suffix[0] == '\0'
12362 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12363 sep = "";
12364 else if (cu->language == language_java)
12365 sep = ".";
f55ee35c
JK
12366 else if (cu->language == language_fortran && physname)
12367 {
12368 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12369 DW_AT_MIPS_linkage_name is preferred and used instead. */
12370
12371 lead = "__";
12372 sep = "_MOD_";
12373 }
987504bb
JJ
12374 else
12375 sep = "::";
63d06c5c 12376
6dd47d34
DE
12377 if (prefix == NULL)
12378 prefix = "";
12379 if (suffix == NULL)
12380 suffix = "";
12381
987504bb
JJ
12382 if (obs == NULL)
12383 {
3e43a32a
MS
12384 char *retval
12385 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12386
f55ee35c
JK
12387 strcpy (retval, lead);
12388 strcat (retval, prefix);
6dd47d34
DE
12389 strcat (retval, sep);
12390 strcat (retval, suffix);
63d06c5c
DC
12391 return retval;
12392 }
987504bb
JJ
12393 else
12394 {
12395 /* We have an obstack. */
f55ee35c 12396 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12397 }
63d06c5c
DC
12398}
12399
c906108c
SS
12400/* Return sibling of die, NULL if no sibling. */
12401
f9aca02d 12402static struct die_info *
fba45db2 12403sibling_die (struct die_info *die)
c906108c 12404{
639d11d3 12405 return die->sibling;
c906108c
SS
12406}
12407
71c25dea
TT
12408/* Get name of a die, return NULL if not found. */
12409
12410static char *
12411dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12412 struct obstack *obstack)
12413{
12414 if (name && cu->language == language_cplus)
12415 {
12416 char *canon_name = cp_canonicalize_string (name);
12417
12418 if (canon_name != NULL)
12419 {
12420 if (strcmp (canon_name, name) != 0)
12421 name = obsavestring (canon_name, strlen (canon_name),
12422 obstack);
12423 xfree (canon_name);
12424 }
12425 }
12426
12427 return name;
c906108c
SS
12428}
12429
9219021c
DC
12430/* Get name of a die, return NULL if not found. */
12431
12432static char *
e142c38c 12433dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12434{
12435 struct attribute *attr;
12436
e142c38c 12437 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12438 if ((!attr || !DW_STRING (attr))
12439 && die->tag != DW_TAG_class_type
12440 && die->tag != DW_TAG_interface_type
12441 && die->tag != DW_TAG_structure_type
12442 && die->tag != DW_TAG_union_type)
71c25dea
TT
12443 return NULL;
12444
12445 switch (die->tag)
12446 {
12447 case DW_TAG_compile_unit:
12448 /* Compilation units have a DW_AT_name that is a filename, not
12449 a source language identifier. */
12450 case DW_TAG_enumeration_type:
12451 case DW_TAG_enumerator:
12452 /* These tags always have simple identifiers already; no need
12453 to canonicalize them. */
12454 return DW_STRING (attr);
907af001 12455
418835cc
KS
12456 case DW_TAG_subprogram:
12457 /* Java constructors will all be named "<init>", so return
12458 the class name when we see this special case. */
12459 if (cu->language == language_java
12460 && DW_STRING (attr) != NULL
12461 && strcmp (DW_STRING (attr), "<init>") == 0)
12462 {
12463 struct dwarf2_cu *spec_cu = cu;
12464 struct die_info *spec_die;
12465
12466 /* GCJ will output '<init>' for Java constructor names.
12467 For this special case, return the name of the parent class. */
12468
12469 /* GCJ may output suprogram DIEs with AT_specification set.
12470 If so, use the name of the specified DIE. */
12471 spec_die = die_specification (die, &spec_cu);
12472 if (spec_die != NULL)
12473 return dwarf2_name (spec_die, spec_cu);
12474
12475 do
12476 {
12477 die = die->parent;
12478 if (die->tag == DW_TAG_class_type)
12479 return dwarf2_name (die, cu);
12480 }
12481 while (die->tag != DW_TAG_compile_unit);
12482 }
907af001
UW
12483 break;
12484
12485 case DW_TAG_class_type:
12486 case DW_TAG_interface_type:
12487 case DW_TAG_structure_type:
12488 case DW_TAG_union_type:
12489 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12490 structures or unions. These were of the form "._%d" in GCC 4.1,
12491 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12492 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12493 if (attr && DW_STRING (attr)
12494 && (strncmp (DW_STRING (attr), "._", 2) == 0
12495 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12496 return NULL;
53832f31
TT
12497
12498 /* GCC might emit a nameless typedef that has a linkage name. See
12499 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12500 if (!attr || DW_STRING (attr) == NULL)
12501 {
df5c6c50 12502 char *demangled = NULL;
53832f31
TT
12503
12504 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12505 if (attr == NULL)
12506 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12507
12508 if (attr == NULL || DW_STRING (attr) == NULL)
12509 return NULL;
12510
df5c6c50
JK
12511 /* Avoid demangling DW_STRING (attr) the second time on a second
12512 call for the same DIE. */
12513 if (!DW_STRING_IS_CANONICAL (attr))
12514 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12515
12516 if (demangled)
12517 {
12518 /* FIXME: we already did this for the partial symbol... */
12519 DW_STRING (attr)
12520 = obsavestring (demangled, strlen (demangled),
12521 &cu->objfile->objfile_obstack);
12522 DW_STRING_IS_CANONICAL (attr) = 1;
12523 xfree (demangled);
12524 }
12525 }
907af001
UW
12526 break;
12527
71c25dea 12528 default:
907af001
UW
12529 break;
12530 }
12531
12532 if (!DW_STRING_IS_CANONICAL (attr))
12533 {
12534 DW_STRING (attr)
12535 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12536 &cu->objfile->objfile_obstack);
12537 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12538 }
907af001 12539 return DW_STRING (attr);
9219021c
DC
12540}
12541
12542/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12543 is none. *EXT_CU is the CU containing DIE on input, and the CU
12544 containing the return value on output. */
9219021c
DC
12545
12546static struct die_info *
f2f0e013 12547dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12548{
12549 struct attribute *attr;
9219021c 12550
f2f0e013 12551 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12552 if (attr == NULL)
12553 return NULL;
12554
f2f0e013 12555 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12556}
12557
c906108c
SS
12558/* Convert a DIE tag into its string name. */
12559
12560static char *
aa1ee363 12561dwarf_tag_name (unsigned tag)
c906108c
SS
12562{
12563 switch (tag)
12564 {
12565 case DW_TAG_padding:
12566 return "DW_TAG_padding";
12567 case DW_TAG_array_type:
12568 return "DW_TAG_array_type";
12569 case DW_TAG_class_type:
12570 return "DW_TAG_class_type";
12571 case DW_TAG_entry_point:
12572 return "DW_TAG_entry_point";
12573 case DW_TAG_enumeration_type:
12574 return "DW_TAG_enumeration_type";
12575 case DW_TAG_formal_parameter:
12576 return "DW_TAG_formal_parameter";
12577 case DW_TAG_imported_declaration:
12578 return "DW_TAG_imported_declaration";
12579 case DW_TAG_label:
12580 return "DW_TAG_label";
12581 case DW_TAG_lexical_block:
12582 return "DW_TAG_lexical_block";
12583 case DW_TAG_member:
12584 return "DW_TAG_member";
12585 case DW_TAG_pointer_type:
12586 return "DW_TAG_pointer_type";
12587 case DW_TAG_reference_type:
12588 return "DW_TAG_reference_type";
12589 case DW_TAG_compile_unit:
12590 return "DW_TAG_compile_unit";
12591 case DW_TAG_string_type:
12592 return "DW_TAG_string_type";
12593 case DW_TAG_structure_type:
12594 return "DW_TAG_structure_type";
12595 case DW_TAG_subroutine_type:
12596 return "DW_TAG_subroutine_type";
12597 case DW_TAG_typedef:
12598 return "DW_TAG_typedef";
12599 case DW_TAG_union_type:
12600 return "DW_TAG_union_type";
12601 case DW_TAG_unspecified_parameters:
12602 return "DW_TAG_unspecified_parameters";
12603 case DW_TAG_variant:
12604 return "DW_TAG_variant";
12605 case DW_TAG_common_block:
12606 return "DW_TAG_common_block";
12607 case DW_TAG_common_inclusion:
12608 return "DW_TAG_common_inclusion";
12609 case DW_TAG_inheritance:
12610 return "DW_TAG_inheritance";
12611 case DW_TAG_inlined_subroutine:
12612 return "DW_TAG_inlined_subroutine";
12613 case DW_TAG_module:
12614 return "DW_TAG_module";
12615 case DW_TAG_ptr_to_member_type:
12616 return "DW_TAG_ptr_to_member_type";
12617 case DW_TAG_set_type:
12618 return "DW_TAG_set_type";
12619 case DW_TAG_subrange_type:
12620 return "DW_TAG_subrange_type";
12621 case DW_TAG_with_stmt:
12622 return "DW_TAG_with_stmt";
12623 case DW_TAG_access_declaration:
12624 return "DW_TAG_access_declaration";
12625 case DW_TAG_base_type:
12626 return "DW_TAG_base_type";
12627 case DW_TAG_catch_block:
12628 return "DW_TAG_catch_block";
12629 case DW_TAG_const_type:
12630 return "DW_TAG_const_type";
12631 case DW_TAG_constant:
12632 return "DW_TAG_constant";
12633 case DW_TAG_enumerator:
12634 return "DW_TAG_enumerator";
12635 case DW_TAG_file_type:
12636 return "DW_TAG_file_type";
12637 case DW_TAG_friend:
12638 return "DW_TAG_friend";
12639 case DW_TAG_namelist:
12640 return "DW_TAG_namelist";
12641 case DW_TAG_namelist_item:
12642 return "DW_TAG_namelist_item";
12643 case DW_TAG_packed_type:
12644 return "DW_TAG_packed_type";
12645 case DW_TAG_subprogram:
12646 return "DW_TAG_subprogram";
12647 case DW_TAG_template_type_param:
12648 return "DW_TAG_template_type_param";
12649 case DW_TAG_template_value_param:
12650 return "DW_TAG_template_value_param";
12651 case DW_TAG_thrown_type:
12652 return "DW_TAG_thrown_type";
12653 case DW_TAG_try_block:
12654 return "DW_TAG_try_block";
12655 case DW_TAG_variant_part:
12656 return "DW_TAG_variant_part";
12657 case DW_TAG_variable:
12658 return "DW_TAG_variable";
12659 case DW_TAG_volatile_type:
12660 return "DW_TAG_volatile_type";
d9fa45fe
DC
12661 case DW_TAG_dwarf_procedure:
12662 return "DW_TAG_dwarf_procedure";
12663 case DW_TAG_restrict_type:
12664 return "DW_TAG_restrict_type";
12665 case DW_TAG_interface_type:
12666 return "DW_TAG_interface_type";
12667 case DW_TAG_namespace:
12668 return "DW_TAG_namespace";
12669 case DW_TAG_imported_module:
12670 return "DW_TAG_imported_module";
12671 case DW_TAG_unspecified_type:
12672 return "DW_TAG_unspecified_type";
12673 case DW_TAG_partial_unit:
12674 return "DW_TAG_partial_unit";
12675 case DW_TAG_imported_unit:
12676 return "DW_TAG_imported_unit";
b7619582
GF
12677 case DW_TAG_condition:
12678 return "DW_TAG_condition";
12679 case DW_TAG_shared_type:
12680 return "DW_TAG_shared_type";
348e048f
DE
12681 case DW_TAG_type_unit:
12682 return "DW_TAG_type_unit";
c906108c
SS
12683 case DW_TAG_MIPS_loop:
12684 return "DW_TAG_MIPS_loop";
b7619582
GF
12685 case DW_TAG_HP_array_descriptor:
12686 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12687 case DW_TAG_format_label:
12688 return "DW_TAG_format_label";
12689 case DW_TAG_function_template:
12690 return "DW_TAG_function_template";
12691 case DW_TAG_class_template:
12692 return "DW_TAG_class_template";
b7619582
GF
12693 case DW_TAG_GNU_BINCL:
12694 return "DW_TAG_GNU_BINCL";
12695 case DW_TAG_GNU_EINCL:
12696 return "DW_TAG_GNU_EINCL";
12697 case DW_TAG_upc_shared_type:
12698 return "DW_TAG_upc_shared_type";
12699 case DW_TAG_upc_strict_type:
12700 return "DW_TAG_upc_strict_type";
12701 case DW_TAG_upc_relaxed_type:
12702 return "DW_TAG_upc_relaxed_type";
12703 case DW_TAG_PGI_kanji_type:
12704 return "DW_TAG_PGI_kanji_type";
12705 case DW_TAG_PGI_interface_block:
12706 return "DW_TAG_PGI_interface_block";
c906108c
SS
12707 default:
12708 return "DW_TAG_<unknown>";
12709 }
12710}
12711
12712/* Convert a DWARF attribute code into its string name. */
12713
12714static char *
aa1ee363 12715dwarf_attr_name (unsigned attr)
c906108c
SS
12716{
12717 switch (attr)
12718 {
12719 case DW_AT_sibling:
12720 return "DW_AT_sibling";
12721 case DW_AT_location:
12722 return "DW_AT_location";
12723 case DW_AT_name:
12724 return "DW_AT_name";
12725 case DW_AT_ordering:
12726 return "DW_AT_ordering";
12727 case DW_AT_subscr_data:
12728 return "DW_AT_subscr_data";
12729 case DW_AT_byte_size:
12730 return "DW_AT_byte_size";
12731 case DW_AT_bit_offset:
12732 return "DW_AT_bit_offset";
12733 case DW_AT_bit_size:
12734 return "DW_AT_bit_size";
12735 case DW_AT_element_list:
12736 return "DW_AT_element_list";
12737 case DW_AT_stmt_list:
12738 return "DW_AT_stmt_list";
12739 case DW_AT_low_pc:
12740 return "DW_AT_low_pc";
12741 case DW_AT_high_pc:
12742 return "DW_AT_high_pc";
12743 case DW_AT_language:
12744 return "DW_AT_language";
12745 case DW_AT_member:
12746 return "DW_AT_member";
12747 case DW_AT_discr:
12748 return "DW_AT_discr";
12749 case DW_AT_discr_value:
12750 return "DW_AT_discr_value";
12751 case DW_AT_visibility:
12752 return "DW_AT_visibility";
12753 case DW_AT_import:
12754 return "DW_AT_import";
12755 case DW_AT_string_length:
12756 return "DW_AT_string_length";
12757 case DW_AT_common_reference:
12758 return "DW_AT_common_reference";
12759 case DW_AT_comp_dir:
12760 return "DW_AT_comp_dir";
12761 case DW_AT_const_value:
12762 return "DW_AT_const_value";
12763 case DW_AT_containing_type:
12764 return "DW_AT_containing_type";
12765 case DW_AT_default_value:
12766 return "DW_AT_default_value";
12767 case DW_AT_inline:
12768 return "DW_AT_inline";
12769 case DW_AT_is_optional:
12770 return "DW_AT_is_optional";
12771 case DW_AT_lower_bound:
12772 return "DW_AT_lower_bound";
12773 case DW_AT_producer:
12774 return "DW_AT_producer";
12775 case DW_AT_prototyped:
12776 return "DW_AT_prototyped";
12777 case DW_AT_return_addr:
12778 return "DW_AT_return_addr";
12779 case DW_AT_start_scope:
12780 return "DW_AT_start_scope";
09fa0d7c
JK
12781 case DW_AT_bit_stride:
12782 return "DW_AT_bit_stride";
c906108c
SS
12783 case DW_AT_upper_bound:
12784 return "DW_AT_upper_bound";
12785 case DW_AT_abstract_origin:
12786 return "DW_AT_abstract_origin";
12787 case DW_AT_accessibility:
12788 return "DW_AT_accessibility";
12789 case DW_AT_address_class:
12790 return "DW_AT_address_class";
12791 case DW_AT_artificial:
12792 return "DW_AT_artificial";
12793 case DW_AT_base_types:
12794 return "DW_AT_base_types";
12795 case DW_AT_calling_convention:
12796 return "DW_AT_calling_convention";
12797 case DW_AT_count:
12798 return "DW_AT_count";
12799 case DW_AT_data_member_location:
12800 return "DW_AT_data_member_location";
12801 case DW_AT_decl_column:
12802 return "DW_AT_decl_column";
12803 case DW_AT_decl_file:
12804 return "DW_AT_decl_file";
12805 case DW_AT_decl_line:
12806 return "DW_AT_decl_line";
12807 case DW_AT_declaration:
12808 return "DW_AT_declaration";
12809 case DW_AT_discr_list:
12810 return "DW_AT_discr_list";
12811 case DW_AT_encoding:
12812 return "DW_AT_encoding";
12813 case DW_AT_external:
12814 return "DW_AT_external";
12815 case DW_AT_frame_base:
12816 return "DW_AT_frame_base";
12817 case DW_AT_friend:
12818 return "DW_AT_friend";
12819 case DW_AT_identifier_case:
12820 return "DW_AT_identifier_case";
12821 case DW_AT_macro_info:
12822 return "DW_AT_macro_info";
12823 case DW_AT_namelist_items:
12824 return "DW_AT_namelist_items";
12825 case DW_AT_priority:
12826 return "DW_AT_priority";
12827 case DW_AT_segment:
12828 return "DW_AT_segment";
12829 case DW_AT_specification:
12830 return "DW_AT_specification";
12831 case DW_AT_static_link:
12832 return "DW_AT_static_link";
12833 case DW_AT_type:
12834 return "DW_AT_type";
12835 case DW_AT_use_location:
12836 return "DW_AT_use_location";
12837 case DW_AT_variable_parameter:
12838 return "DW_AT_variable_parameter";
12839 case DW_AT_virtuality:
12840 return "DW_AT_virtuality";
12841 case DW_AT_vtable_elem_location:
12842 return "DW_AT_vtable_elem_location";
b7619582 12843 /* DWARF 3 values. */
d9fa45fe
DC
12844 case DW_AT_allocated:
12845 return "DW_AT_allocated";
12846 case DW_AT_associated:
12847 return "DW_AT_associated";
12848 case DW_AT_data_location:
12849 return "DW_AT_data_location";
09fa0d7c
JK
12850 case DW_AT_byte_stride:
12851 return "DW_AT_byte_stride";
d9fa45fe
DC
12852 case DW_AT_entry_pc:
12853 return "DW_AT_entry_pc";
12854 case DW_AT_use_UTF8:
12855 return "DW_AT_use_UTF8";
12856 case DW_AT_extension:
12857 return "DW_AT_extension";
12858 case DW_AT_ranges:
12859 return "DW_AT_ranges";
12860 case DW_AT_trampoline:
12861 return "DW_AT_trampoline";
12862 case DW_AT_call_column:
12863 return "DW_AT_call_column";
12864 case DW_AT_call_file:
12865 return "DW_AT_call_file";
12866 case DW_AT_call_line:
12867 return "DW_AT_call_line";
b7619582
GF
12868 case DW_AT_description:
12869 return "DW_AT_description";
12870 case DW_AT_binary_scale:
12871 return "DW_AT_binary_scale";
12872 case DW_AT_decimal_scale:
12873 return "DW_AT_decimal_scale";
12874 case DW_AT_small:
12875 return "DW_AT_small";
12876 case DW_AT_decimal_sign:
12877 return "DW_AT_decimal_sign";
12878 case DW_AT_digit_count:
12879 return "DW_AT_digit_count";
12880 case DW_AT_picture_string:
12881 return "DW_AT_picture_string";
12882 case DW_AT_mutable:
12883 return "DW_AT_mutable";
12884 case DW_AT_threads_scaled:
12885 return "DW_AT_threads_scaled";
12886 case DW_AT_explicit:
12887 return "DW_AT_explicit";
12888 case DW_AT_object_pointer:
12889 return "DW_AT_object_pointer";
12890 case DW_AT_endianity:
12891 return "DW_AT_endianity";
12892 case DW_AT_elemental:
12893 return "DW_AT_elemental";
12894 case DW_AT_pure:
12895 return "DW_AT_pure";
12896 case DW_AT_recursive:
12897 return "DW_AT_recursive";
348e048f
DE
12898 /* DWARF 4 values. */
12899 case DW_AT_signature:
12900 return "DW_AT_signature";
31ef98ae
TT
12901 case DW_AT_linkage_name:
12902 return "DW_AT_linkage_name";
b7619582 12903 /* SGI/MIPS extensions. */
c764a876 12904#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12905 case DW_AT_MIPS_fde:
12906 return "DW_AT_MIPS_fde";
c764a876 12907#endif
c906108c
SS
12908 case DW_AT_MIPS_loop_begin:
12909 return "DW_AT_MIPS_loop_begin";
12910 case DW_AT_MIPS_tail_loop_begin:
12911 return "DW_AT_MIPS_tail_loop_begin";
12912 case DW_AT_MIPS_epilog_begin:
12913 return "DW_AT_MIPS_epilog_begin";
12914 case DW_AT_MIPS_loop_unroll_factor:
12915 return "DW_AT_MIPS_loop_unroll_factor";
12916 case DW_AT_MIPS_software_pipeline_depth:
12917 return "DW_AT_MIPS_software_pipeline_depth";
12918 case DW_AT_MIPS_linkage_name:
12919 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12920 case DW_AT_MIPS_stride:
12921 return "DW_AT_MIPS_stride";
12922 case DW_AT_MIPS_abstract_name:
12923 return "DW_AT_MIPS_abstract_name";
12924 case DW_AT_MIPS_clone_origin:
12925 return "DW_AT_MIPS_clone_origin";
12926 case DW_AT_MIPS_has_inlines:
12927 return "DW_AT_MIPS_has_inlines";
b7619582 12928 /* HP extensions. */
c764a876 12929#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12930 case DW_AT_HP_block_index:
12931 return "DW_AT_HP_block_index";
c764a876 12932#endif
b7619582
GF
12933 case DW_AT_HP_unmodifiable:
12934 return "DW_AT_HP_unmodifiable";
12935 case DW_AT_HP_actuals_stmt_list:
12936 return "DW_AT_HP_actuals_stmt_list";
12937 case DW_AT_HP_proc_per_section:
12938 return "DW_AT_HP_proc_per_section";
12939 case DW_AT_HP_raw_data_ptr:
12940 return "DW_AT_HP_raw_data_ptr";
12941 case DW_AT_HP_pass_by_reference:
12942 return "DW_AT_HP_pass_by_reference";
12943 case DW_AT_HP_opt_level:
12944 return "DW_AT_HP_opt_level";
12945 case DW_AT_HP_prof_version_id:
12946 return "DW_AT_HP_prof_version_id";
12947 case DW_AT_HP_opt_flags:
12948 return "DW_AT_HP_opt_flags";
12949 case DW_AT_HP_cold_region_low_pc:
12950 return "DW_AT_HP_cold_region_low_pc";
12951 case DW_AT_HP_cold_region_high_pc:
12952 return "DW_AT_HP_cold_region_high_pc";
12953 case DW_AT_HP_all_variables_modifiable:
12954 return "DW_AT_HP_all_variables_modifiable";
12955 case DW_AT_HP_linkage_name:
12956 return "DW_AT_HP_linkage_name";
12957 case DW_AT_HP_prof_flags:
12958 return "DW_AT_HP_prof_flags";
12959 /* GNU extensions. */
c906108c
SS
12960 case DW_AT_sf_names:
12961 return "DW_AT_sf_names";
12962 case DW_AT_src_info:
12963 return "DW_AT_src_info";
12964 case DW_AT_mac_info:
12965 return "DW_AT_mac_info";
12966 case DW_AT_src_coords:
12967 return "DW_AT_src_coords";
12968 case DW_AT_body_begin:
12969 return "DW_AT_body_begin";
12970 case DW_AT_body_end:
12971 return "DW_AT_body_end";
f5f8a009
EZ
12972 case DW_AT_GNU_vector:
12973 return "DW_AT_GNU_vector";
2de00c64
DE
12974 case DW_AT_GNU_odr_signature:
12975 return "DW_AT_GNU_odr_signature";
b7619582
GF
12976 /* VMS extensions. */
12977 case DW_AT_VMS_rtnbeg_pd_address:
12978 return "DW_AT_VMS_rtnbeg_pd_address";
12979 /* UPC extension. */
12980 case DW_AT_upc_threads_scaled:
12981 return "DW_AT_upc_threads_scaled";
12982 /* PGI (STMicroelectronics) extensions. */
12983 case DW_AT_PGI_lbase:
12984 return "DW_AT_PGI_lbase";
12985 case DW_AT_PGI_soffset:
12986 return "DW_AT_PGI_soffset";
12987 case DW_AT_PGI_lstride:
12988 return "DW_AT_PGI_lstride";
c906108c
SS
12989 default:
12990 return "DW_AT_<unknown>";
12991 }
12992}
12993
12994/* Convert a DWARF value form code into its string name. */
12995
12996static char *
aa1ee363 12997dwarf_form_name (unsigned form)
c906108c
SS
12998{
12999 switch (form)
13000 {
13001 case DW_FORM_addr:
13002 return "DW_FORM_addr";
13003 case DW_FORM_block2:
13004 return "DW_FORM_block2";
13005 case DW_FORM_block4:
13006 return "DW_FORM_block4";
13007 case DW_FORM_data2:
13008 return "DW_FORM_data2";
13009 case DW_FORM_data4:
13010 return "DW_FORM_data4";
13011 case DW_FORM_data8:
13012 return "DW_FORM_data8";
13013 case DW_FORM_string:
13014 return "DW_FORM_string";
13015 case DW_FORM_block:
13016 return "DW_FORM_block";
13017 case DW_FORM_block1:
13018 return "DW_FORM_block1";
13019 case DW_FORM_data1:
13020 return "DW_FORM_data1";
13021 case DW_FORM_flag:
13022 return "DW_FORM_flag";
13023 case DW_FORM_sdata:
13024 return "DW_FORM_sdata";
13025 case DW_FORM_strp:
13026 return "DW_FORM_strp";
13027 case DW_FORM_udata:
13028 return "DW_FORM_udata";
13029 case DW_FORM_ref_addr:
13030 return "DW_FORM_ref_addr";
13031 case DW_FORM_ref1:
13032 return "DW_FORM_ref1";
13033 case DW_FORM_ref2:
13034 return "DW_FORM_ref2";
13035 case DW_FORM_ref4:
13036 return "DW_FORM_ref4";
13037 case DW_FORM_ref8:
13038 return "DW_FORM_ref8";
13039 case DW_FORM_ref_udata:
13040 return "DW_FORM_ref_udata";
13041 case DW_FORM_indirect:
13042 return "DW_FORM_indirect";
348e048f
DE
13043 case DW_FORM_sec_offset:
13044 return "DW_FORM_sec_offset";
13045 case DW_FORM_exprloc:
13046 return "DW_FORM_exprloc";
13047 case DW_FORM_flag_present:
13048 return "DW_FORM_flag_present";
55f1336d
TT
13049 case DW_FORM_ref_sig8:
13050 return "DW_FORM_ref_sig8";
c906108c
SS
13051 default:
13052 return "DW_FORM_<unknown>";
13053 }
13054}
13055
13056/* Convert a DWARF stack opcode into its string name. */
13057
9eae7c52 13058const char *
b1bfef65 13059dwarf_stack_op_name (unsigned op)
c906108c
SS
13060{
13061 switch (op)
13062 {
13063 case DW_OP_addr:
13064 return "DW_OP_addr";
13065 case DW_OP_deref:
13066 return "DW_OP_deref";
13067 case DW_OP_const1u:
13068 return "DW_OP_const1u";
13069 case DW_OP_const1s:
13070 return "DW_OP_const1s";
13071 case DW_OP_const2u:
13072 return "DW_OP_const2u";
13073 case DW_OP_const2s:
13074 return "DW_OP_const2s";
13075 case DW_OP_const4u:
13076 return "DW_OP_const4u";
13077 case DW_OP_const4s:
13078 return "DW_OP_const4s";
13079 case DW_OP_const8u:
13080 return "DW_OP_const8u";
13081 case DW_OP_const8s:
13082 return "DW_OP_const8s";
13083 case DW_OP_constu:
13084 return "DW_OP_constu";
13085 case DW_OP_consts:
13086 return "DW_OP_consts";
13087 case DW_OP_dup:
13088 return "DW_OP_dup";
13089 case DW_OP_drop:
13090 return "DW_OP_drop";
13091 case DW_OP_over:
13092 return "DW_OP_over";
13093 case DW_OP_pick:
13094 return "DW_OP_pick";
13095 case DW_OP_swap:
13096 return "DW_OP_swap";
13097 case DW_OP_rot:
13098 return "DW_OP_rot";
13099 case DW_OP_xderef:
13100 return "DW_OP_xderef";
13101 case DW_OP_abs:
13102 return "DW_OP_abs";
13103 case DW_OP_and:
13104 return "DW_OP_and";
13105 case DW_OP_div:
13106 return "DW_OP_div";
13107 case DW_OP_minus:
13108 return "DW_OP_minus";
13109 case DW_OP_mod:
13110 return "DW_OP_mod";
13111 case DW_OP_mul:
13112 return "DW_OP_mul";
13113 case DW_OP_neg:
13114 return "DW_OP_neg";
13115 case DW_OP_not:
13116 return "DW_OP_not";
13117 case DW_OP_or:
13118 return "DW_OP_or";
13119 case DW_OP_plus:
13120 return "DW_OP_plus";
13121 case DW_OP_plus_uconst:
13122 return "DW_OP_plus_uconst";
13123 case DW_OP_shl:
13124 return "DW_OP_shl";
13125 case DW_OP_shr:
13126 return "DW_OP_shr";
13127 case DW_OP_shra:
13128 return "DW_OP_shra";
13129 case DW_OP_xor:
13130 return "DW_OP_xor";
13131 case DW_OP_bra:
13132 return "DW_OP_bra";
13133 case DW_OP_eq:
13134 return "DW_OP_eq";
13135 case DW_OP_ge:
13136 return "DW_OP_ge";
13137 case DW_OP_gt:
13138 return "DW_OP_gt";
13139 case DW_OP_le:
13140 return "DW_OP_le";
13141 case DW_OP_lt:
13142 return "DW_OP_lt";
13143 case DW_OP_ne:
13144 return "DW_OP_ne";
13145 case DW_OP_skip:
13146 return "DW_OP_skip";
13147 case DW_OP_lit0:
13148 return "DW_OP_lit0";
13149 case DW_OP_lit1:
13150 return "DW_OP_lit1";
13151 case DW_OP_lit2:
13152 return "DW_OP_lit2";
13153 case DW_OP_lit3:
13154 return "DW_OP_lit3";
13155 case DW_OP_lit4:
13156 return "DW_OP_lit4";
13157 case DW_OP_lit5:
13158 return "DW_OP_lit5";
13159 case DW_OP_lit6:
13160 return "DW_OP_lit6";
13161 case DW_OP_lit7:
13162 return "DW_OP_lit7";
13163 case DW_OP_lit8:
13164 return "DW_OP_lit8";
13165 case DW_OP_lit9:
13166 return "DW_OP_lit9";
13167 case DW_OP_lit10:
13168 return "DW_OP_lit10";
13169 case DW_OP_lit11:
13170 return "DW_OP_lit11";
13171 case DW_OP_lit12:
13172 return "DW_OP_lit12";
13173 case DW_OP_lit13:
13174 return "DW_OP_lit13";
13175 case DW_OP_lit14:
13176 return "DW_OP_lit14";
13177 case DW_OP_lit15:
13178 return "DW_OP_lit15";
13179 case DW_OP_lit16:
13180 return "DW_OP_lit16";
13181 case DW_OP_lit17:
13182 return "DW_OP_lit17";
13183 case DW_OP_lit18:
13184 return "DW_OP_lit18";
13185 case DW_OP_lit19:
13186 return "DW_OP_lit19";
13187 case DW_OP_lit20:
13188 return "DW_OP_lit20";
13189 case DW_OP_lit21:
13190 return "DW_OP_lit21";
13191 case DW_OP_lit22:
13192 return "DW_OP_lit22";
13193 case DW_OP_lit23:
13194 return "DW_OP_lit23";
13195 case DW_OP_lit24:
13196 return "DW_OP_lit24";
13197 case DW_OP_lit25:
13198 return "DW_OP_lit25";
13199 case DW_OP_lit26:
13200 return "DW_OP_lit26";
13201 case DW_OP_lit27:
13202 return "DW_OP_lit27";
13203 case DW_OP_lit28:
13204 return "DW_OP_lit28";
13205 case DW_OP_lit29:
13206 return "DW_OP_lit29";
13207 case DW_OP_lit30:
13208 return "DW_OP_lit30";
13209 case DW_OP_lit31:
13210 return "DW_OP_lit31";
13211 case DW_OP_reg0:
13212 return "DW_OP_reg0";
13213 case DW_OP_reg1:
13214 return "DW_OP_reg1";
13215 case DW_OP_reg2:
13216 return "DW_OP_reg2";
13217 case DW_OP_reg3:
13218 return "DW_OP_reg3";
13219 case DW_OP_reg4:
13220 return "DW_OP_reg4";
13221 case DW_OP_reg5:
13222 return "DW_OP_reg5";
13223 case DW_OP_reg6:
13224 return "DW_OP_reg6";
13225 case DW_OP_reg7:
13226 return "DW_OP_reg7";
13227 case DW_OP_reg8:
13228 return "DW_OP_reg8";
13229 case DW_OP_reg9:
13230 return "DW_OP_reg9";
13231 case DW_OP_reg10:
13232 return "DW_OP_reg10";
13233 case DW_OP_reg11:
13234 return "DW_OP_reg11";
13235 case DW_OP_reg12:
13236 return "DW_OP_reg12";
13237 case DW_OP_reg13:
13238 return "DW_OP_reg13";
13239 case DW_OP_reg14:
13240 return "DW_OP_reg14";
13241 case DW_OP_reg15:
13242 return "DW_OP_reg15";
13243 case DW_OP_reg16:
13244 return "DW_OP_reg16";
13245 case DW_OP_reg17:
13246 return "DW_OP_reg17";
13247 case DW_OP_reg18:
13248 return "DW_OP_reg18";
13249 case DW_OP_reg19:
13250 return "DW_OP_reg19";
13251 case DW_OP_reg20:
13252 return "DW_OP_reg20";
13253 case DW_OP_reg21:
13254 return "DW_OP_reg21";
13255 case DW_OP_reg22:
13256 return "DW_OP_reg22";
13257 case DW_OP_reg23:
13258 return "DW_OP_reg23";
13259 case DW_OP_reg24:
13260 return "DW_OP_reg24";
13261 case DW_OP_reg25:
13262 return "DW_OP_reg25";
13263 case DW_OP_reg26:
13264 return "DW_OP_reg26";
13265 case DW_OP_reg27:
13266 return "DW_OP_reg27";
13267 case DW_OP_reg28:
13268 return "DW_OP_reg28";
13269 case DW_OP_reg29:
13270 return "DW_OP_reg29";
13271 case DW_OP_reg30:
13272 return "DW_OP_reg30";
13273 case DW_OP_reg31:
13274 return "DW_OP_reg31";
13275 case DW_OP_breg0:
13276 return "DW_OP_breg0";
13277 case DW_OP_breg1:
13278 return "DW_OP_breg1";
13279 case DW_OP_breg2:
13280 return "DW_OP_breg2";
13281 case DW_OP_breg3:
13282 return "DW_OP_breg3";
13283 case DW_OP_breg4:
13284 return "DW_OP_breg4";
13285 case DW_OP_breg5:
13286 return "DW_OP_breg5";
13287 case DW_OP_breg6:
13288 return "DW_OP_breg6";
13289 case DW_OP_breg7:
13290 return "DW_OP_breg7";
13291 case DW_OP_breg8:
13292 return "DW_OP_breg8";
13293 case DW_OP_breg9:
13294 return "DW_OP_breg9";
13295 case DW_OP_breg10:
13296 return "DW_OP_breg10";
13297 case DW_OP_breg11:
13298 return "DW_OP_breg11";
13299 case DW_OP_breg12:
13300 return "DW_OP_breg12";
13301 case DW_OP_breg13:
13302 return "DW_OP_breg13";
13303 case DW_OP_breg14:
13304 return "DW_OP_breg14";
13305 case DW_OP_breg15:
13306 return "DW_OP_breg15";
13307 case DW_OP_breg16:
13308 return "DW_OP_breg16";
13309 case DW_OP_breg17:
13310 return "DW_OP_breg17";
13311 case DW_OP_breg18:
13312 return "DW_OP_breg18";
13313 case DW_OP_breg19:
13314 return "DW_OP_breg19";
13315 case DW_OP_breg20:
13316 return "DW_OP_breg20";
13317 case DW_OP_breg21:
13318 return "DW_OP_breg21";
13319 case DW_OP_breg22:
13320 return "DW_OP_breg22";
13321 case DW_OP_breg23:
13322 return "DW_OP_breg23";
13323 case DW_OP_breg24:
13324 return "DW_OP_breg24";
13325 case DW_OP_breg25:
13326 return "DW_OP_breg25";
13327 case DW_OP_breg26:
13328 return "DW_OP_breg26";
13329 case DW_OP_breg27:
13330 return "DW_OP_breg27";
13331 case DW_OP_breg28:
13332 return "DW_OP_breg28";
13333 case DW_OP_breg29:
13334 return "DW_OP_breg29";
13335 case DW_OP_breg30:
13336 return "DW_OP_breg30";
13337 case DW_OP_breg31:
13338 return "DW_OP_breg31";
13339 case DW_OP_regx:
13340 return "DW_OP_regx";
13341 case DW_OP_fbreg:
13342 return "DW_OP_fbreg";
13343 case DW_OP_bregx:
13344 return "DW_OP_bregx";
13345 case DW_OP_piece:
13346 return "DW_OP_piece";
13347 case DW_OP_deref_size:
13348 return "DW_OP_deref_size";
13349 case DW_OP_xderef_size:
13350 return "DW_OP_xderef_size";
13351 case DW_OP_nop:
13352 return "DW_OP_nop";
b7619582 13353 /* DWARF 3 extensions. */
ed348acc
EZ
13354 case DW_OP_push_object_address:
13355 return "DW_OP_push_object_address";
13356 case DW_OP_call2:
13357 return "DW_OP_call2";
13358 case DW_OP_call4:
13359 return "DW_OP_call4";
13360 case DW_OP_call_ref:
13361 return "DW_OP_call_ref";
b7619582
GF
13362 case DW_OP_form_tls_address:
13363 return "DW_OP_form_tls_address";
13364 case DW_OP_call_frame_cfa:
13365 return "DW_OP_call_frame_cfa";
13366 case DW_OP_bit_piece:
13367 return "DW_OP_bit_piece";
9eae7c52
TT
13368 /* DWARF 4 extensions. */
13369 case DW_OP_implicit_value:
13370 return "DW_OP_implicit_value";
13371 case DW_OP_stack_value:
13372 return "DW_OP_stack_value";
13373 /* GNU extensions. */
ed348acc
EZ
13374 case DW_OP_GNU_push_tls_address:
13375 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13376 case DW_OP_GNU_uninit:
13377 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13378 case DW_OP_GNU_implicit_pointer:
13379 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13380 case DW_OP_GNU_entry_value:
13381 return "DW_OP_GNU_entry_value";
13382 case DW_OP_GNU_const_type:
13383 return "DW_OP_GNU_const_type";
13384 case DW_OP_GNU_regval_type:
13385 return "DW_OP_GNU_regval_type";
13386 case DW_OP_GNU_deref_type:
13387 return "DW_OP_GNU_deref_type";
13388 case DW_OP_GNU_convert:
13389 return "DW_OP_GNU_convert";
13390 case DW_OP_GNU_reinterpret:
13391 return "DW_OP_GNU_reinterpret";
c906108c 13392 default:
b1bfef65 13393 return NULL;
c906108c
SS
13394 }
13395}
13396
13397static char *
fba45db2 13398dwarf_bool_name (unsigned mybool)
c906108c
SS
13399{
13400 if (mybool)
13401 return "TRUE";
13402 else
13403 return "FALSE";
13404}
13405
13406/* Convert a DWARF type code into its string name. */
13407
13408static char *
aa1ee363 13409dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13410{
13411 switch (enc)
13412 {
b7619582
GF
13413 case DW_ATE_void:
13414 return "DW_ATE_void";
c906108c
SS
13415 case DW_ATE_address:
13416 return "DW_ATE_address";
13417 case DW_ATE_boolean:
13418 return "DW_ATE_boolean";
13419 case DW_ATE_complex_float:
13420 return "DW_ATE_complex_float";
13421 case DW_ATE_float:
13422 return "DW_ATE_float";
13423 case DW_ATE_signed:
13424 return "DW_ATE_signed";
13425 case DW_ATE_signed_char:
13426 return "DW_ATE_signed_char";
13427 case DW_ATE_unsigned:
13428 return "DW_ATE_unsigned";
13429 case DW_ATE_unsigned_char:
13430 return "DW_ATE_unsigned_char";
b7619582 13431 /* DWARF 3. */
d9fa45fe
DC
13432 case DW_ATE_imaginary_float:
13433 return "DW_ATE_imaginary_float";
b7619582
GF
13434 case DW_ATE_packed_decimal:
13435 return "DW_ATE_packed_decimal";
13436 case DW_ATE_numeric_string:
13437 return "DW_ATE_numeric_string";
13438 case DW_ATE_edited:
13439 return "DW_ATE_edited";
13440 case DW_ATE_signed_fixed:
13441 return "DW_ATE_signed_fixed";
13442 case DW_ATE_unsigned_fixed:
13443 return "DW_ATE_unsigned_fixed";
13444 case DW_ATE_decimal_float:
13445 return "DW_ATE_decimal_float";
75079b2b
TT
13446 /* DWARF 4. */
13447 case DW_ATE_UTF:
13448 return "DW_ATE_UTF";
b7619582
GF
13449 /* HP extensions. */
13450 case DW_ATE_HP_float80:
13451 return "DW_ATE_HP_float80";
13452 case DW_ATE_HP_complex_float80:
13453 return "DW_ATE_HP_complex_float80";
13454 case DW_ATE_HP_float128:
13455 return "DW_ATE_HP_float128";
13456 case DW_ATE_HP_complex_float128:
13457 return "DW_ATE_HP_complex_float128";
13458 case DW_ATE_HP_floathpintel:
13459 return "DW_ATE_HP_floathpintel";
13460 case DW_ATE_HP_imaginary_float80:
13461 return "DW_ATE_HP_imaginary_float80";
13462 case DW_ATE_HP_imaginary_float128:
13463 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13464 default:
13465 return "DW_ATE_<unknown>";
13466 }
13467}
13468
0963b4bd 13469/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13470
13471#if 0
13472static char *
aa1ee363 13473dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13474{
13475 switch (cfi_opc)
13476 {
13477 case DW_CFA_advance_loc:
13478 return "DW_CFA_advance_loc";
13479 case DW_CFA_offset:
13480 return "DW_CFA_offset";
13481 case DW_CFA_restore:
13482 return "DW_CFA_restore";
13483 case DW_CFA_nop:
13484 return "DW_CFA_nop";
13485 case DW_CFA_set_loc:
13486 return "DW_CFA_set_loc";
13487 case DW_CFA_advance_loc1:
13488 return "DW_CFA_advance_loc1";
13489 case DW_CFA_advance_loc2:
13490 return "DW_CFA_advance_loc2";
13491 case DW_CFA_advance_loc4:
13492 return "DW_CFA_advance_loc4";
13493 case DW_CFA_offset_extended:
13494 return "DW_CFA_offset_extended";
13495 case DW_CFA_restore_extended:
13496 return "DW_CFA_restore_extended";
13497 case DW_CFA_undefined:
13498 return "DW_CFA_undefined";
13499 case DW_CFA_same_value:
13500 return "DW_CFA_same_value";
13501 case DW_CFA_register:
13502 return "DW_CFA_register";
13503 case DW_CFA_remember_state:
13504 return "DW_CFA_remember_state";
13505 case DW_CFA_restore_state:
13506 return "DW_CFA_restore_state";
13507 case DW_CFA_def_cfa:
13508 return "DW_CFA_def_cfa";
13509 case DW_CFA_def_cfa_register:
13510 return "DW_CFA_def_cfa_register";
13511 case DW_CFA_def_cfa_offset:
13512 return "DW_CFA_def_cfa_offset";
b7619582 13513 /* DWARF 3. */
985cb1a3
JM
13514 case DW_CFA_def_cfa_expression:
13515 return "DW_CFA_def_cfa_expression";
13516 case DW_CFA_expression:
13517 return "DW_CFA_expression";
13518 case DW_CFA_offset_extended_sf:
13519 return "DW_CFA_offset_extended_sf";
13520 case DW_CFA_def_cfa_sf:
13521 return "DW_CFA_def_cfa_sf";
13522 case DW_CFA_def_cfa_offset_sf:
13523 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13524 case DW_CFA_val_offset:
13525 return "DW_CFA_val_offset";
13526 case DW_CFA_val_offset_sf:
13527 return "DW_CFA_val_offset_sf";
13528 case DW_CFA_val_expression:
13529 return "DW_CFA_val_expression";
13530 /* SGI/MIPS specific. */
c906108c
SS
13531 case DW_CFA_MIPS_advance_loc8:
13532 return "DW_CFA_MIPS_advance_loc8";
b7619582 13533 /* GNU extensions. */
985cb1a3
JM
13534 case DW_CFA_GNU_window_save:
13535 return "DW_CFA_GNU_window_save";
13536 case DW_CFA_GNU_args_size:
13537 return "DW_CFA_GNU_args_size";
13538 case DW_CFA_GNU_negative_offset_extended:
13539 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13540 default:
13541 return "DW_CFA_<unknown>";
13542 }
13543}
13544#endif
13545
f9aca02d 13546static void
d97bc12b 13547dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13548{
13549 unsigned int i;
13550
d97bc12b
DE
13551 print_spaces (indent, f);
13552 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13553 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13554
13555 if (die->parent != NULL)
13556 {
13557 print_spaces (indent, f);
13558 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13559 die->parent->offset);
13560 }
13561
13562 print_spaces (indent, f);
13563 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13564 dwarf_bool_name (die->child != NULL));
c906108c 13565
d97bc12b
DE
13566 print_spaces (indent, f);
13567 fprintf_unfiltered (f, " attributes:\n");
13568
c906108c
SS
13569 for (i = 0; i < die->num_attrs; ++i)
13570 {
d97bc12b
DE
13571 print_spaces (indent, f);
13572 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13573 dwarf_attr_name (die->attrs[i].name),
13574 dwarf_form_name (die->attrs[i].form));
d97bc12b 13575
c906108c
SS
13576 switch (die->attrs[i].form)
13577 {
13578 case DW_FORM_ref_addr:
13579 case DW_FORM_addr:
d97bc12b 13580 fprintf_unfiltered (f, "address: ");
5af949e3 13581 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13582 break;
13583 case DW_FORM_block2:
13584 case DW_FORM_block4:
13585 case DW_FORM_block:
13586 case DW_FORM_block1:
3e43a32a
MS
13587 fprintf_unfiltered (f, "block: size %d",
13588 DW_BLOCK (&die->attrs[i])->size);
c906108c 13589 break;
2dc7f7b3
TT
13590 case DW_FORM_exprloc:
13591 fprintf_unfiltered (f, "expression: size %u",
13592 DW_BLOCK (&die->attrs[i])->size);
13593 break;
10b3939b
DJ
13594 case DW_FORM_ref1:
13595 case DW_FORM_ref2:
13596 case DW_FORM_ref4:
d97bc12b 13597 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13598 (long) (DW_ADDR (&die->attrs[i])));
13599 break;
c906108c
SS
13600 case DW_FORM_data1:
13601 case DW_FORM_data2:
13602 case DW_FORM_data4:
ce5d95e1 13603 case DW_FORM_data8:
c906108c
SS
13604 case DW_FORM_udata:
13605 case DW_FORM_sdata:
43bbcdc2
PH
13606 fprintf_unfiltered (f, "constant: %s",
13607 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13608 break;
2dc7f7b3
TT
13609 case DW_FORM_sec_offset:
13610 fprintf_unfiltered (f, "section offset: %s",
13611 pulongest (DW_UNSND (&die->attrs[i])));
13612 break;
55f1336d 13613 case DW_FORM_ref_sig8:
348e048f
DE
13614 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13615 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13616 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13617 else
13618 fprintf_unfiltered (f, "signatured type, offset: unknown");
13619 break;
c906108c 13620 case DW_FORM_string:
4bdf3d34 13621 case DW_FORM_strp:
8285870a 13622 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13623 DW_STRING (&die->attrs[i])
8285870a
JK
13624 ? DW_STRING (&die->attrs[i]) : "",
13625 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13626 break;
13627 case DW_FORM_flag:
13628 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13629 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13630 else
d97bc12b 13631 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13632 break;
2dc7f7b3
TT
13633 case DW_FORM_flag_present:
13634 fprintf_unfiltered (f, "flag: TRUE");
13635 break;
a8329558 13636 case DW_FORM_indirect:
0963b4bd
MS
13637 /* The reader will have reduced the indirect form to
13638 the "base form" so this form should not occur. */
3e43a32a
MS
13639 fprintf_unfiltered (f,
13640 "unexpected attribute form: DW_FORM_indirect");
a8329558 13641 break;
c906108c 13642 default:
d97bc12b 13643 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13644 die->attrs[i].form);
d97bc12b 13645 break;
c906108c 13646 }
d97bc12b 13647 fprintf_unfiltered (f, "\n");
c906108c
SS
13648 }
13649}
13650
f9aca02d 13651static void
d97bc12b 13652dump_die_for_error (struct die_info *die)
c906108c 13653{
d97bc12b
DE
13654 dump_die_shallow (gdb_stderr, 0, die);
13655}
13656
13657static void
13658dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13659{
13660 int indent = level * 4;
13661
13662 gdb_assert (die != NULL);
13663
13664 if (level >= max_level)
13665 return;
13666
13667 dump_die_shallow (f, indent, die);
13668
13669 if (die->child != NULL)
c906108c 13670 {
d97bc12b
DE
13671 print_spaces (indent, f);
13672 fprintf_unfiltered (f, " Children:");
13673 if (level + 1 < max_level)
13674 {
13675 fprintf_unfiltered (f, "\n");
13676 dump_die_1 (f, level + 1, max_level, die->child);
13677 }
13678 else
13679 {
3e43a32a
MS
13680 fprintf_unfiltered (f,
13681 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13682 }
13683 }
13684
13685 if (die->sibling != NULL && level > 0)
13686 {
13687 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13688 }
13689}
13690
d97bc12b
DE
13691/* This is called from the pdie macro in gdbinit.in.
13692 It's not static so gcc will keep a copy callable from gdb. */
13693
13694void
13695dump_die (struct die_info *die, int max_level)
13696{
13697 dump_die_1 (gdb_stdlog, 0, max_level, die);
13698}
13699
f9aca02d 13700static void
51545339 13701store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13702{
51545339 13703 void **slot;
c906108c 13704
51545339
DJ
13705 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13706
13707 *slot = die;
c906108c
SS
13708}
13709
93311388
DE
13710static int
13711is_ref_attr (struct attribute *attr)
c906108c 13712{
c906108c
SS
13713 switch (attr->form)
13714 {
13715 case DW_FORM_ref_addr:
c906108c
SS
13716 case DW_FORM_ref1:
13717 case DW_FORM_ref2:
13718 case DW_FORM_ref4:
613e1657 13719 case DW_FORM_ref8:
c906108c 13720 case DW_FORM_ref_udata:
93311388 13721 return 1;
c906108c 13722 default:
93311388 13723 return 0;
c906108c 13724 }
93311388
DE
13725}
13726
13727static unsigned int
13728dwarf2_get_ref_die_offset (struct attribute *attr)
13729{
13730 if (is_ref_attr (attr))
13731 return DW_ADDR (attr);
13732
13733 complaint (&symfile_complaints,
13734 _("unsupported die ref attribute form: '%s'"),
13735 dwarf_form_name (attr->form));
13736 return 0;
c906108c
SS
13737}
13738
43bbcdc2
PH
13739/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13740 * the value held by the attribute is not constant. */
a02abb62 13741
43bbcdc2 13742static LONGEST
a02abb62
JB
13743dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13744{
13745 if (attr->form == DW_FORM_sdata)
13746 return DW_SND (attr);
13747 else if (attr->form == DW_FORM_udata
13748 || attr->form == DW_FORM_data1
13749 || attr->form == DW_FORM_data2
13750 || attr->form == DW_FORM_data4
13751 || attr->form == DW_FORM_data8)
13752 return DW_UNSND (attr);
13753 else
13754 {
3e43a32a
MS
13755 complaint (&symfile_complaints,
13756 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13757 dwarf_form_name (attr->form));
13758 return default_value;
13759 }
13760}
13761
03dd20cc 13762/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13763 unit and add it to our queue.
13764 The result is non-zero if PER_CU was queued, otherwise the result is zero
13765 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13766
348e048f 13767static int
03dd20cc
DJ
13768maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13769 struct dwarf2_per_cu_data *per_cu)
13770{
98bfdba5
PA
13771 /* We may arrive here during partial symbol reading, if we need full
13772 DIEs to process an unusual case (e.g. template arguments). Do
13773 not queue PER_CU, just tell our caller to load its DIEs. */
13774 if (dwarf2_per_objfile->reading_partial_symbols)
13775 {
13776 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13777 return 1;
13778 return 0;
13779 }
13780
03dd20cc
DJ
13781 /* Mark the dependence relation so that we don't flush PER_CU
13782 too early. */
13783 dwarf2_add_dependence (this_cu, per_cu);
13784
13785 /* If it's already on the queue, we have nothing to do. */
13786 if (per_cu->queued)
348e048f 13787 return 0;
03dd20cc
DJ
13788
13789 /* If the compilation unit is already loaded, just mark it as
13790 used. */
13791 if (per_cu->cu != NULL)
13792 {
13793 per_cu->cu->last_used = 0;
348e048f 13794 return 0;
03dd20cc
DJ
13795 }
13796
13797 /* Add it to the queue. */
13798 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13799
13800 return 1;
13801}
13802
13803/* Follow reference or signature attribute ATTR of SRC_DIE.
13804 On entry *REF_CU is the CU of SRC_DIE.
13805 On exit *REF_CU is the CU of the result. */
13806
13807static struct die_info *
13808follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13809 struct dwarf2_cu **ref_cu)
13810{
13811 struct die_info *die;
13812
13813 if (is_ref_attr (attr))
13814 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13815 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13816 die = follow_die_sig (src_die, attr, ref_cu);
13817 else
13818 {
13819 dump_die_for_error (src_die);
13820 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13821 (*ref_cu)->objfile->name);
13822 }
13823
13824 return die;
03dd20cc
DJ
13825}
13826
5c631832 13827/* Follow reference OFFSET.
673bfd45
DE
13828 On entry *REF_CU is the CU of the source die referencing OFFSET.
13829 On exit *REF_CU is the CU of the result.
13830 Returns NULL if OFFSET is invalid. */
f504f079 13831
f9aca02d 13832static struct die_info *
5c631832 13833follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13834{
10b3939b 13835 struct die_info temp_die;
f2f0e013 13836 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13837
348e048f
DE
13838 gdb_assert (cu->per_cu != NULL);
13839
98bfdba5
PA
13840 target_cu = cu;
13841
8b70b953 13842 if (cu->per_cu->debug_type_section)
348e048f
DE
13843 {
13844 /* .debug_types CUs cannot reference anything outside their CU.
13845 If they need to, they have to reference a signatured type via
55f1336d 13846 DW_FORM_ref_sig8. */
348e048f 13847 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13848 return NULL;
348e048f
DE
13849 }
13850 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13851 {
13852 struct dwarf2_per_cu_data *per_cu;
9a619af0 13853
45452591 13854 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13855
13856 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13857 if (maybe_queue_comp_unit (cu, per_cu))
13858 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13859
10b3939b
DJ
13860 target_cu = per_cu->cu;
13861 }
98bfdba5
PA
13862 else if (cu->dies == NULL)
13863 {
13864 /* We're loading full DIEs during partial symbol reading. */
13865 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13866 load_full_comp_unit (cu->per_cu, cu->objfile);
13867 }
c906108c 13868
f2f0e013 13869 *ref_cu = target_cu;
51545339 13870 temp_die.offset = offset;
5c631832
JK
13871 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13872}
10b3939b 13873
5c631832
JK
13874/* Follow reference attribute ATTR of SRC_DIE.
13875 On entry *REF_CU is the CU of SRC_DIE.
13876 On exit *REF_CU is the CU of the result. */
13877
13878static struct die_info *
13879follow_die_ref (struct die_info *src_die, struct attribute *attr,
13880 struct dwarf2_cu **ref_cu)
13881{
13882 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13883 struct dwarf2_cu *cu = *ref_cu;
13884 struct die_info *die;
13885
13886 die = follow_die_offset (offset, ref_cu);
13887 if (!die)
13888 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13889 "at 0x%x [in module %s]"),
13890 offset, src_die->offset, cu->objfile->name);
348e048f 13891
5c631832
JK
13892 return die;
13893}
13894
13895/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
918dd910
JK
13896 value is intended for DW_OP_call*. You must call xfree on returned
13897 dwarf2_locexpr_baton->data. */
5c631832
JK
13898
13899struct dwarf2_locexpr_baton
13900dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13901 struct dwarf2_per_cu_data *per_cu,
13902 CORE_ADDR (*get_frame_pc) (void *baton),
13903 void *baton)
5c631832 13904{
918dd910 13905 struct dwarf2_cu *cu;
5c631832
JK
13906 struct die_info *die;
13907 struct attribute *attr;
13908 struct dwarf2_locexpr_baton retval;
13909
8cf6f0b1
TT
13910 dw2_setup (per_cu->objfile);
13911
918dd910
JK
13912 if (per_cu->cu == NULL)
13913 load_cu (per_cu);
13914 cu = per_cu->cu;
13915
5c631832
JK
13916 die = follow_die_offset (offset, &cu);
13917 if (!die)
13918 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13919 offset, per_cu->cu->objfile->name);
13920
13921 attr = dwarf2_attr (die, DW_AT_location, cu);
13922 if (!attr)
13923 {
13924 /* DWARF: "If there is no such attribute, then there is no effect.". */
13925
13926 retval.data = NULL;
13927 retval.size = 0;
13928 }
8cf6f0b1
TT
13929 else if (attr_form_is_section_offset (attr))
13930 {
13931 struct dwarf2_loclist_baton loclist_baton;
13932 CORE_ADDR pc = (*get_frame_pc) (baton);
13933 size_t size;
13934
13935 fill_in_loclist_baton (cu, &loclist_baton, attr);
13936
13937 retval.data = dwarf2_find_location_expression (&loclist_baton,
13938 &size, pc);
13939 retval.size = size;
13940 }
5c631832
JK
13941 else
13942 {
13943 if (!attr_form_is_block (attr))
13944 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13945 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13946 offset, per_cu->cu->objfile->name);
13947
13948 retval.data = DW_BLOCK (attr)->data;
13949 retval.size = DW_BLOCK (attr)->size;
13950 }
13951 retval.per_cu = cu->per_cu;
918dd910
JK
13952
13953 if (retval.data)
13954 retval.data = xmemdup (retval.data, retval.size, retval.size);
13955
13956 age_cached_comp_units ();
13957
5c631832 13958 return retval;
348e048f
DE
13959}
13960
8a9b8146
TT
13961/* Return the type of the DIE at DIE_OFFSET in the CU named by
13962 PER_CU. */
13963
13964struct type *
13965dwarf2_get_die_type (unsigned int die_offset,
13966 struct dwarf2_per_cu_data *per_cu)
13967{
8a9b8146 13968 dw2_setup (per_cu->objfile);
9ff3b74f 13969 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13970}
13971
348e048f
DE
13972/* Follow the signature attribute ATTR in SRC_DIE.
13973 On entry *REF_CU is the CU of SRC_DIE.
13974 On exit *REF_CU is the CU of the result. */
13975
13976static struct die_info *
13977follow_die_sig (struct die_info *src_die, struct attribute *attr,
13978 struct dwarf2_cu **ref_cu)
13979{
13980 struct objfile *objfile = (*ref_cu)->objfile;
13981 struct die_info temp_die;
13982 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13983 struct dwarf2_cu *sig_cu;
13984 struct die_info *die;
13985
13986 /* sig_type will be NULL if the signatured type is missing from
13987 the debug info. */
13988 if (sig_type == NULL)
13989 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13990 "at 0x%x [in module %s]"),
13991 src_die->offset, objfile->name);
13992
13993 /* If necessary, add it to the queue and load its DIEs. */
13994
13995 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13996 read_signatured_type (objfile, sig_type);
13997
13998 gdb_assert (sig_type->per_cu.cu != NULL);
13999
14000 sig_cu = sig_type->per_cu.cu;
14001 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14002 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14003 if (die)
14004 {
14005 *ref_cu = sig_cu;
14006 return die;
14007 }
14008
3e43a32a
MS
14009 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14010 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14011 sig_type->type_offset, src_die->offset, objfile->name);
14012}
14013
14014/* Given an offset of a signatured type, return its signatured_type. */
14015
14016static struct signatured_type *
8b70b953
TT
14017lookup_signatured_type_at_offset (struct objfile *objfile,
14018 struct dwarf2_section_info *section,
14019 unsigned int offset)
348e048f 14020{
8b70b953 14021 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14022 unsigned int length, initial_length_size;
14023 unsigned int sig_offset;
14024 struct signatured_type find_entry, *type_sig;
14025
14026 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14027 sig_offset = (initial_length_size
14028 + 2 /*version*/
14029 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14030 + 1 /*address_size*/);
14031 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14032 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14033
14034 /* This is only used to lookup previously recorded types.
14035 If we didn't find it, it's our bug. */
14036 gdb_assert (type_sig != NULL);
b3c8eb43 14037 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14038
14039 return type_sig;
14040}
14041
14042/* Read in signatured type at OFFSET and build its CU and die(s). */
14043
14044static void
14045read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14046 struct dwarf2_section_info *sect,
348e048f
DE
14047 unsigned int offset)
14048{
14049 struct signatured_type *type_sig;
14050
8b70b953 14051 dwarf2_read_section (objfile, sect);
be391dca 14052
348e048f
DE
14053 /* We have the section offset, but we need the signature to do the
14054 hash table lookup. */
8b70b953 14055 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14056
14057 gdb_assert (type_sig->per_cu.cu == NULL);
14058
14059 read_signatured_type (objfile, type_sig);
14060
14061 gdb_assert (type_sig->per_cu.cu != NULL);
14062}
14063
14064/* Read in a signatured type and build its CU and DIEs. */
14065
14066static void
14067read_signatured_type (struct objfile *objfile,
14068 struct signatured_type *type_sig)
14069{
1fd400ff 14070 gdb_byte *types_ptr;
348e048f
DE
14071 struct die_reader_specs reader_specs;
14072 struct dwarf2_cu *cu;
14073 ULONGEST signature;
14074 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14075 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14076
8b70b953
TT
14077 dwarf2_read_section (objfile, section);
14078 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14079
348e048f
DE
14080 gdb_assert (type_sig->per_cu.cu == NULL);
14081
9816fde3
JK
14082 cu = xmalloc (sizeof (*cu));
14083 init_one_comp_unit (cu, objfile);
14084
348e048f
DE
14085 type_sig->per_cu.cu = cu;
14086 cu->per_cu = &type_sig->per_cu;
14087
14088 /* If an error occurs while loading, release our storage. */
14089 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14090
8b70b953 14091 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14092 types_ptr, objfile->obfd);
14093 gdb_assert (signature == type_sig->signature);
14094
14095 cu->die_hash
14096 = htab_create_alloc_ex (cu->header.length / 12,
14097 die_hash,
14098 die_eq,
14099 NULL,
14100 &cu->comp_unit_obstack,
14101 hashtab_obstack_allocate,
14102 dummy_obstack_deallocate);
14103
14104 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14105 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14106
14107 init_cu_die_reader (&reader_specs, cu);
14108
14109 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14110 NULL /*parent*/);
14111
14112 /* We try not to read any attributes in this function, because not
14113 all objfiles needed for references have been loaded yet, and symbol
14114 table processing isn't initialized. But we have to set the CU language,
14115 or we won't be able to build types correctly. */
9816fde3 14116 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14117
14118 do_cleanups (back_to);
14119
14120 /* We've successfully allocated this compilation unit. Let our caller
14121 clean it up when finished with it. */
14122 discard_cleanups (free_cu_cleanup);
14123
14124 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14125 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14126}
14127
c906108c
SS
14128/* Decode simple location descriptions.
14129 Given a pointer to a dwarf block that defines a location, compute
14130 the location and return the value.
14131
4cecd739
DJ
14132 NOTE drow/2003-11-18: This function is called in two situations
14133 now: for the address of static or global variables (partial symbols
14134 only) and for offsets into structures which are expected to be
14135 (more or less) constant. The partial symbol case should go away,
14136 and only the constant case should remain. That will let this
14137 function complain more accurately. A few special modes are allowed
14138 without complaint for global variables (for instance, global
14139 register values and thread-local values).
c906108c
SS
14140
14141 A location description containing no operations indicates that the
4cecd739 14142 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14143 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14144 callers will only want a very basic result and this can become a
14145 complaint.
c906108c 14146
d53d4ac5 14147 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14148
14149static CORE_ADDR
e7c27a73 14150decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14151{
e7c27a73 14152 struct objfile *objfile = cu->objfile;
c906108c
SS
14153 int i;
14154 int size = blk->size;
fe1b8b76 14155 gdb_byte *data = blk->data;
c906108c
SS
14156 CORE_ADDR stack[64];
14157 int stacki;
14158 unsigned int bytes_read, unsnd;
fe1b8b76 14159 gdb_byte op;
c906108c
SS
14160
14161 i = 0;
14162 stacki = 0;
14163 stack[stacki] = 0;
d53d4ac5 14164 stack[++stacki] = 0;
c906108c
SS
14165
14166 while (i < size)
14167 {
c906108c
SS
14168 op = data[i++];
14169 switch (op)
14170 {
f1bea926
JM
14171 case DW_OP_lit0:
14172 case DW_OP_lit1:
14173 case DW_OP_lit2:
14174 case DW_OP_lit3:
14175 case DW_OP_lit4:
14176 case DW_OP_lit5:
14177 case DW_OP_lit6:
14178 case DW_OP_lit7:
14179 case DW_OP_lit8:
14180 case DW_OP_lit9:
14181 case DW_OP_lit10:
14182 case DW_OP_lit11:
14183 case DW_OP_lit12:
14184 case DW_OP_lit13:
14185 case DW_OP_lit14:
14186 case DW_OP_lit15:
14187 case DW_OP_lit16:
14188 case DW_OP_lit17:
14189 case DW_OP_lit18:
14190 case DW_OP_lit19:
14191 case DW_OP_lit20:
14192 case DW_OP_lit21:
14193 case DW_OP_lit22:
14194 case DW_OP_lit23:
14195 case DW_OP_lit24:
14196 case DW_OP_lit25:
14197 case DW_OP_lit26:
14198 case DW_OP_lit27:
14199 case DW_OP_lit28:
14200 case DW_OP_lit29:
14201 case DW_OP_lit30:
14202 case DW_OP_lit31:
14203 stack[++stacki] = op - DW_OP_lit0;
14204 break;
14205
c906108c
SS
14206 case DW_OP_reg0:
14207 case DW_OP_reg1:
14208 case DW_OP_reg2:
14209 case DW_OP_reg3:
14210 case DW_OP_reg4:
14211 case DW_OP_reg5:
14212 case DW_OP_reg6:
14213 case DW_OP_reg7:
14214 case DW_OP_reg8:
14215 case DW_OP_reg9:
14216 case DW_OP_reg10:
14217 case DW_OP_reg11:
14218 case DW_OP_reg12:
14219 case DW_OP_reg13:
14220 case DW_OP_reg14:
14221 case DW_OP_reg15:
14222 case DW_OP_reg16:
14223 case DW_OP_reg17:
14224 case DW_OP_reg18:
14225 case DW_OP_reg19:
14226 case DW_OP_reg20:
14227 case DW_OP_reg21:
14228 case DW_OP_reg22:
14229 case DW_OP_reg23:
14230 case DW_OP_reg24:
14231 case DW_OP_reg25:
14232 case DW_OP_reg26:
14233 case DW_OP_reg27:
14234 case DW_OP_reg28:
14235 case DW_OP_reg29:
14236 case DW_OP_reg30:
14237 case DW_OP_reg31:
c906108c 14238 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
14239 if (i < size)
14240 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14241 break;
14242
14243 case DW_OP_regx:
c906108c
SS
14244 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14245 i += bytes_read;
c906108c 14246 stack[++stacki] = unsnd;
4cecd739
DJ
14247 if (i < size)
14248 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14249 break;
14250
14251 case DW_OP_addr:
107d2387 14252 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 14253 cu, &bytes_read);
107d2387 14254 i += bytes_read;
c906108c
SS
14255 break;
14256
14257 case DW_OP_const1u:
14258 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14259 i += 1;
14260 break;
14261
14262 case DW_OP_const1s:
14263 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14264 i += 1;
14265 break;
14266
14267 case DW_OP_const2u:
14268 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14269 i += 2;
14270 break;
14271
14272 case DW_OP_const2s:
14273 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14274 i += 2;
14275 break;
14276
14277 case DW_OP_const4u:
14278 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14279 i += 4;
14280 break;
14281
14282 case DW_OP_const4s:
14283 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14284 i += 4;
14285 break;
14286
14287 case DW_OP_constu:
14288 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 14289 &bytes_read);
c906108c
SS
14290 i += bytes_read;
14291 break;
14292
14293 case DW_OP_consts:
14294 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14295 i += bytes_read;
14296 break;
14297
f1bea926
JM
14298 case DW_OP_dup:
14299 stack[stacki + 1] = stack[stacki];
14300 stacki++;
14301 break;
14302
c906108c
SS
14303 case DW_OP_plus:
14304 stack[stacki - 1] += stack[stacki];
14305 stacki--;
14306 break;
14307
14308 case DW_OP_plus_uconst:
3e43a32a
MS
14309 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14310 &bytes_read);
c906108c
SS
14311 i += bytes_read;
14312 break;
14313
14314 case DW_OP_minus:
f1bea926 14315 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14316 stacki--;
14317 break;
14318
7a292a7a 14319 case DW_OP_deref:
7a292a7a 14320 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14321 this using GDB's address_class enum. This is valid for partial
14322 global symbols, although the variable's address will be bogus
14323 in the psymtab. */
7a292a7a 14324 if (i < size)
4d3c2250 14325 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14326 break;
14327
9d774e44 14328 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14329 /* The top of the stack has the offset from the beginning
14330 of the thread control block at which the variable is located. */
14331 /* Nothing should follow this operator, so the top of stack would
14332 be returned. */
4cecd739
DJ
14333 /* This is valid for partial global symbols, but the variable's
14334 address will be bogus in the psymtab. */
9d774e44 14335 if (i < size)
4d3c2250 14336 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14337 break;
14338
42be36b3
CT
14339 case DW_OP_GNU_uninit:
14340 break;
14341
c906108c 14342 default:
b1bfef65
TT
14343 {
14344 const char *name = dwarf_stack_op_name (op);
14345
14346 if (name)
14347 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14348 name);
14349 else
14350 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14351 op);
14352 }
14353
c906108c
SS
14354 return (stack[stacki]);
14355 }
d53d4ac5
TT
14356
14357 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14358 outside of the allocated space. Also enforce minimum>0. */
14359 if (stacki >= ARRAY_SIZE (stack) - 1)
14360 {
14361 complaint (&symfile_complaints,
14362 _("location description stack overflow"));
14363 return 0;
14364 }
14365
14366 if (stacki <= 0)
14367 {
14368 complaint (&symfile_complaints,
14369 _("location description stack underflow"));
14370 return 0;
14371 }
c906108c
SS
14372 }
14373 return (stack[stacki]);
14374}
14375
14376/* memory allocation interface */
14377
c906108c 14378static struct dwarf_block *
7b5a2f43 14379dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14380{
14381 struct dwarf_block *blk;
14382
14383 blk = (struct dwarf_block *)
7b5a2f43 14384 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14385 return (blk);
14386}
14387
14388static struct abbrev_info *
f3dd6933 14389dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14390{
14391 struct abbrev_info *abbrev;
14392
f3dd6933
DJ
14393 abbrev = (struct abbrev_info *)
14394 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14395 memset (abbrev, 0, sizeof (struct abbrev_info));
14396 return (abbrev);
14397}
14398
14399static struct die_info *
b60c80d6 14400dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14401{
14402 struct die_info *die;
b60c80d6
DJ
14403 size_t size = sizeof (struct die_info);
14404
14405 if (num_attrs > 1)
14406 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14407
b60c80d6 14408 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14409 memset (die, 0, sizeof (struct die_info));
14410 return (die);
14411}
2e276125
JB
14412
14413\f
14414/* Macro support. */
14415
2e276125
JB
14416/* Return the full name of file number I in *LH's file name table.
14417 Use COMP_DIR as the name of the current directory of the
14418 compilation. The result is allocated using xmalloc; the caller is
14419 responsible for freeing it. */
14420static char *
14421file_full_name (int file, struct line_header *lh, const char *comp_dir)
14422{
6a83a1e6
EZ
14423 /* Is the file number a valid index into the line header's file name
14424 table? Remember that file numbers start with one, not zero. */
14425 if (1 <= file && file <= lh->num_file_names)
14426 {
14427 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14428
6a83a1e6
EZ
14429 if (IS_ABSOLUTE_PATH (fe->name))
14430 return xstrdup (fe->name);
14431 else
14432 {
14433 const char *dir;
14434 int dir_len;
14435 char *full_name;
14436
14437 if (fe->dir_index)
14438 dir = lh->include_dirs[fe->dir_index - 1];
14439 else
14440 dir = comp_dir;
14441
14442 if (dir)
14443 {
14444 dir_len = strlen (dir);
14445 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14446 strcpy (full_name, dir);
14447 full_name[dir_len] = '/';
14448 strcpy (full_name + dir_len + 1, fe->name);
14449 return full_name;
14450 }
14451 else
14452 return xstrdup (fe->name);
14453 }
14454 }
2e276125
JB
14455 else
14456 {
6a83a1e6
EZ
14457 /* The compiler produced a bogus file number. We can at least
14458 record the macro definitions made in the file, even if we
14459 won't be able to find the file by name. */
14460 char fake_name[80];
9a619af0 14461
6a83a1e6 14462 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14463
6e70227d 14464 complaint (&symfile_complaints,
6a83a1e6
EZ
14465 _("bad file number in macro information (%d)"),
14466 file);
2e276125 14467
6a83a1e6 14468 return xstrdup (fake_name);
2e276125
JB
14469 }
14470}
14471
14472
14473static struct macro_source_file *
14474macro_start_file (int file, int line,
14475 struct macro_source_file *current_file,
14476 const char *comp_dir,
14477 struct line_header *lh, struct objfile *objfile)
14478{
14479 /* The full name of this source file. */
14480 char *full_name = file_full_name (file, lh, comp_dir);
14481
14482 /* We don't create a macro table for this compilation unit
14483 at all until we actually get a filename. */
14484 if (! pending_macros)
4a146b47 14485 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14486 objfile->macro_cache);
2e276125
JB
14487
14488 if (! current_file)
14489 /* If we have no current file, then this must be the start_file
14490 directive for the compilation unit's main source file. */
14491 current_file = macro_set_main (pending_macros, full_name);
14492 else
14493 current_file = macro_include (current_file, line, full_name);
14494
14495 xfree (full_name);
6e70227d 14496
2e276125
JB
14497 return current_file;
14498}
14499
14500
14501/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14502 followed by a null byte. */
14503static char *
14504copy_string (const char *buf, int len)
14505{
14506 char *s = xmalloc (len + 1);
9a619af0 14507
2e276125
JB
14508 memcpy (s, buf, len);
14509 s[len] = '\0';
2e276125
JB
14510 return s;
14511}
14512
14513
14514static const char *
14515consume_improper_spaces (const char *p, const char *body)
14516{
14517 if (*p == ' ')
14518 {
4d3c2250 14519 complaint (&symfile_complaints,
3e43a32a
MS
14520 _("macro definition contains spaces "
14521 "in formal argument list:\n`%s'"),
4d3c2250 14522 body);
2e276125
JB
14523
14524 while (*p == ' ')
14525 p++;
14526 }
14527
14528 return p;
14529}
14530
14531
14532static void
14533parse_macro_definition (struct macro_source_file *file, int line,
14534 const char *body)
14535{
14536 const char *p;
14537
14538 /* The body string takes one of two forms. For object-like macro
14539 definitions, it should be:
14540
14541 <macro name> " " <definition>
14542
14543 For function-like macro definitions, it should be:
14544
14545 <macro name> "() " <definition>
14546 or
14547 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14548
14549 Spaces may appear only where explicitly indicated, and in the
14550 <definition>.
14551
14552 The Dwarf 2 spec says that an object-like macro's name is always
14553 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14554 the space when the macro's definition is the empty string.
2e276125
JB
14555
14556 The Dwarf 2 spec says that there should be no spaces between the
14557 formal arguments in a function-like macro's formal argument list,
14558 but versions of GCC around March 2002 include spaces after the
14559 commas. */
14560
14561
14562 /* Find the extent of the macro name. The macro name is terminated
14563 by either a space or null character (for an object-like macro) or
14564 an opening paren (for a function-like macro). */
14565 for (p = body; *p; p++)
14566 if (*p == ' ' || *p == '(')
14567 break;
14568
14569 if (*p == ' ' || *p == '\0')
14570 {
14571 /* It's an object-like macro. */
14572 int name_len = p - body;
14573 char *name = copy_string (body, name_len);
14574 const char *replacement;
14575
14576 if (*p == ' ')
14577 replacement = body + name_len + 1;
14578 else
14579 {
4d3c2250 14580 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14581 replacement = body + name_len;
14582 }
6e70227d 14583
2e276125
JB
14584 macro_define_object (file, line, name, replacement);
14585
14586 xfree (name);
14587 }
14588 else if (*p == '(')
14589 {
14590 /* It's a function-like macro. */
14591 char *name = copy_string (body, p - body);
14592 int argc = 0;
14593 int argv_size = 1;
14594 char **argv = xmalloc (argv_size * sizeof (*argv));
14595
14596 p++;
14597
14598 p = consume_improper_spaces (p, body);
14599
14600 /* Parse the formal argument list. */
14601 while (*p && *p != ')')
14602 {
14603 /* Find the extent of the current argument name. */
14604 const char *arg_start = p;
14605
14606 while (*p && *p != ',' && *p != ')' && *p != ' ')
14607 p++;
14608
14609 if (! *p || p == arg_start)
4d3c2250 14610 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14611 else
14612 {
14613 /* Make sure argv has room for the new argument. */
14614 if (argc >= argv_size)
14615 {
14616 argv_size *= 2;
14617 argv = xrealloc (argv, argv_size * sizeof (*argv));
14618 }
14619
14620 argv[argc++] = copy_string (arg_start, p - arg_start);
14621 }
14622
14623 p = consume_improper_spaces (p, body);
14624
14625 /* Consume the comma, if present. */
14626 if (*p == ',')
14627 {
14628 p++;
14629
14630 p = consume_improper_spaces (p, body);
14631 }
14632 }
14633
14634 if (*p == ')')
14635 {
14636 p++;
14637
14638 if (*p == ' ')
14639 /* Perfectly formed definition, no complaints. */
14640 macro_define_function (file, line, name,
6e70227d 14641 argc, (const char **) argv,
2e276125
JB
14642 p + 1);
14643 else if (*p == '\0')
14644 {
14645 /* Complain, but do define it. */
4d3c2250 14646 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14647 macro_define_function (file, line, name,
6e70227d 14648 argc, (const char **) argv,
2e276125
JB
14649 p);
14650 }
14651 else
14652 /* Just complain. */
4d3c2250 14653 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14654 }
14655 else
14656 /* Just complain. */
4d3c2250 14657 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14658
14659 xfree (name);
14660 {
14661 int i;
14662
14663 for (i = 0; i < argc; i++)
14664 xfree (argv[i]);
14665 }
14666 xfree (argv);
14667 }
14668 else
4d3c2250 14669 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14670}
14671
14672
14673static void
14674dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14675 char *comp_dir, bfd *abfd,
e7c27a73 14676 struct dwarf2_cu *cu)
2e276125 14677{
fe1b8b76 14678 gdb_byte *mac_ptr, *mac_end;
2e276125 14679 struct macro_source_file *current_file = 0;
757a13d0
JK
14680 enum dwarf_macinfo_record_type macinfo_type;
14681 int at_commandline;
2e276125 14682
be391dca
TT
14683 dwarf2_read_section (dwarf2_per_objfile->objfile,
14684 &dwarf2_per_objfile->macinfo);
dce234bc 14685 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14686 {
e2e0b3e5 14687 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14688 return;
14689 }
14690
757a13d0
JK
14691 /* First pass: Find the name of the base filename.
14692 This filename is needed in order to process all macros whose definition
14693 (or undefinition) comes from the command line. These macros are defined
14694 before the first DW_MACINFO_start_file entry, and yet still need to be
14695 associated to the base file.
14696
14697 To determine the base file name, we scan the macro definitions until we
14698 reach the first DW_MACINFO_start_file entry. We then initialize
14699 CURRENT_FILE accordingly so that any macro definition found before the
14700 first DW_MACINFO_start_file can still be associated to the base file. */
14701
dce234bc
PP
14702 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14703 mac_end = dwarf2_per_objfile->macinfo.buffer
14704 + dwarf2_per_objfile->macinfo.size;
2e276125 14705
757a13d0 14706 do
2e276125 14707 {
2e276125
JB
14708 /* Do we at least have room for a macinfo type byte? */
14709 if (mac_ptr >= mac_end)
14710 {
757a13d0 14711 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14712 stop the first pass earlier upon finding
14713 DW_MACINFO_start_file. */
757a13d0 14714 break;
2e276125
JB
14715 }
14716
14717 macinfo_type = read_1_byte (abfd, mac_ptr);
14718 mac_ptr++;
14719
14720 switch (macinfo_type)
14721 {
14722 /* A zero macinfo type indicates the end of the macro
14723 information. */
14724 case 0:
757a13d0
JK
14725 break;
14726
14727 case DW_MACINFO_define:
14728 case DW_MACINFO_undef:
14729 /* Only skip the data by MAC_PTR. */
14730 {
14731 unsigned int bytes_read;
14732
14733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14734 mac_ptr += bytes_read;
9b1c24c8 14735 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14736 mac_ptr += bytes_read;
14737 }
14738 break;
14739
14740 case DW_MACINFO_start_file:
14741 {
14742 unsigned int bytes_read;
14743 int line, file;
14744
14745 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14746 mac_ptr += bytes_read;
14747 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14748 mac_ptr += bytes_read;
14749
3e43a32a
MS
14750 current_file = macro_start_file (file, line, current_file,
14751 comp_dir, lh, cu->objfile);
757a13d0
JK
14752 }
14753 break;
14754
14755 case DW_MACINFO_end_file:
14756 /* No data to skip by MAC_PTR. */
14757 break;
14758
14759 case DW_MACINFO_vendor_ext:
14760 /* Only skip the data by MAC_PTR. */
14761 {
14762 unsigned int bytes_read;
14763
14764 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14765 mac_ptr += bytes_read;
9b1c24c8 14766 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14767 mac_ptr += bytes_read;
14768 }
14769 break;
14770
14771 default:
14772 break;
14773 }
14774 } while (macinfo_type != 0 && current_file == NULL);
14775
14776 /* Second pass: Process all entries.
14777
14778 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14779 command-line macro definitions/undefinitions. This flag is unset when we
14780 reach the first DW_MACINFO_start_file entry. */
14781
dce234bc 14782 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14783
14784 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14785 GDB is still reading the definitions from command line. First
14786 DW_MACINFO_start_file will need to be ignored as it was already executed
14787 to create CURRENT_FILE for the main source holding also the command line
14788 definitions. On first met DW_MACINFO_start_file this flag is reset to
14789 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14790
14791 at_commandline = 1;
14792
14793 do
14794 {
14795 /* Do we at least have room for a macinfo type byte? */
14796 if (mac_ptr >= mac_end)
14797 {
14798 dwarf2_macros_too_long_complaint ();
14799 break;
14800 }
14801
14802 macinfo_type = read_1_byte (abfd, mac_ptr);
14803 mac_ptr++;
14804
14805 switch (macinfo_type)
14806 {
14807 /* A zero macinfo type indicates the end of the macro
14808 information. */
14809 case 0:
14810 break;
2e276125
JB
14811
14812 case DW_MACINFO_define:
14813 case DW_MACINFO_undef:
14814 {
891d2f0b 14815 unsigned int bytes_read;
2e276125
JB
14816 int line;
14817 char *body;
14818
14819 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14820 mac_ptr += bytes_read;
9b1c24c8 14821 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14822 mac_ptr += bytes_read;
14823
14824 if (! current_file)
757a13d0
JK
14825 {
14826 /* DWARF violation as no main source is present. */
14827 complaint (&symfile_complaints,
14828 _("debug info with no main source gives macro %s "
14829 "on line %d: %s"),
6e70227d
DE
14830 macinfo_type == DW_MACINFO_define ?
14831 _("definition") :
905e0470
PM
14832 macinfo_type == DW_MACINFO_undef ?
14833 _("undefinition") :
14834 _("something-or-other"), line, body);
757a13d0
JK
14835 break;
14836 }
3e43a32a
MS
14837 if ((line == 0 && !at_commandline)
14838 || (line != 0 && at_commandline))
4d3c2250 14839 complaint (&symfile_complaints,
757a13d0
JK
14840 _("debug info gives %s macro %s with %s line %d: %s"),
14841 at_commandline ? _("command-line") : _("in-file"),
905e0470 14842 macinfo_type == DW_MACINFO_define ?
6e70227d 14843 _("definition") :
905e0470
PM
14844 macinfo_type == DW_MACINFO_undef ?
14845 _("undefinition") :
14846 _("something-or-other"),
757a13d0
JK
14847 line == 0 ? _("zero") : _("non-zero"), line, body);
14848
14849 if (macinfo_type == DW_MACINFO_define)
14850 parse_macro_definition (current_file, line, body);
14851 else if (macinfo_type == DW_MACINFO_undef)
14852 macro_undef (current_file, line, body);
2e276125
JB
14853 }
14854 break;
14855
14856 case DW_MACINFO_start_file:
14857 {
891d2f0b 14858 unsigned int bytes_read;
2e276125
JB
14859 int line, file;
14860
14861 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14862 mac_ptr += bytes_read;
14863 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14864 mac_ptr += bytes_read;
14865
3e43a32a
MS
14866 if ((line == 0 && !at_commandline)
14867 || (line != 0 && at_commandline))
757a13d0
JK
14868 complaint (&symfile_complaints,
14869 _("debug info gives source %d included "
14870 "from %s at %s line %d"),
14871 file, at_commandline ? _("command-line") : _("file"),
14872 line == 0 ? _("zero") : _("non-zero"), line);
14873
14874 if (at_commandline)
14875 {
14876 /* This DW_MACINFO_start_file was executed in the pass one. */
14877 at_commandline = 0;
14878 }
14879 else
14880 current_file = macro_start_file (file, line,
14881 current_file, comp_dir,
14882 lh, cu->objfile);
2e276125
JB
14883 }
14884 break;
14885
14886 case DW_MACINFO_end_file:
14887 if (! current_file)
4d3c2250 14888 complaint (&symfile_complaints,
3e43a32a
MS
14889 _("macro debug info has an unmatched "
14890 "`close_file' directive"));
2e276125
JB
14891 else
14892 {
14893 current_file = current_file->included_by;
14894 if (! current_file)
14895 {
14896 enum dwarf_macinfo_record_type next_type;
14897
14898 /* GCC circa March 2002 doesn't produce the zero
14899 type byte marking the end of the compilation
14900 unit. Complain if it's not there, but exit no
14901 matter what. */
14902
14903 /* Do we at least have room for a macinfo type byte? */
14904 if (mac_ptr >= mac_end)
14905 {
4d3c2250 14906 dwarf2_macros_too_long_complaint ();
2e276125
JB
14907 return;
14908 }
14909
14910 /* We don't increment mac_ptr here, so this is just
14911 a look-ahead. */
14912 next_type = read_1_byte (abfd, mac_ptr);
14913 if (next_type != 0)
4d3c2250 14914 complaint (&symfile_complaints,
3e43a32a
MS
14915 _("no terminating 0-type entry for "
14916 "macros in `.debug_macinfo' section"));
2e276125
JB
14917
14918 return;
14919 }
14920 }
14921 break;
14922
14923 case DW_MACINFO_vendor_ext:
14924 {
891d2f0b 14925 unsigned int bytes_read;
2e276125 14926 int constant;
2e276125
JB
14927
14928 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14929 mac_ptr += bytes_read;
e8e80198 14930 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14931 mac_ptr += bytes_read;
14932
14933 /* We don't recognize any vendor extensions. */
14934 }
14935 break;
14936 }
757a13d0 14937 } while (macinfo_type != 0);
2e276125 14938}
8e19ed76
PS
14939
14940/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14941 if so return true else false. */
8e19ed76
PS
14942static int
14943attr_form_is_block (struct attribute *attr)
14944{
14945 return (attr == NULL ? 0 :
14946 attr->form == DW_FORM_block1
14947 || attr->form == DW_FORM_block2
14948 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14949 || attr->form == DW_FORM_block
14950 || attr->form == DW_FORM_exprloc);
8e19ed76 14951}
4c2df51b 14952
c6a0999f
JB
14953/* Return non-zero if ATTR's value is a section offset --- classes
14954 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14955 You may use DW_UNSND (attr) to retrieve such offsets.
14956
14957 Section 7.5.4, "Attribute Encodings", explains that no attribute
14958 may have a value that belongs to more than one of these classes; it
14959 would be ambiguous if we did, because we use the same forms for all
14960 of them. */
3690dd37
JB
14961static int
14962attr_form_is_section_offset (struct attribute *attr)
14963{
14964 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14965 || attr->form == DW_FORM_data8
14966 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14967}
14968
14969
14970/* Return non-zero if ATTR's value falls in the 'constant' class, or
14971 zero otherwise. When this function returns true, you can apply
14972 dwarf2_get_attr_constant_value to it.
14973
14974 However, note that for some attributes you must check
14975 attr_form_is_section_offset before using this test. DW_FORM_data4
14976 and DW_FORM_data8 are members of both the constant class, and of
14977 the classes that contain offsets into other debug sections
14978 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14979 that, if an attribute's can be either a constant or one of the
14980 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14981 taken as section offsets, not constants. */
14982static int
14983attr_form_is_constant (struct attribute *attr)
14984{
14985 switch (attr->form)
14986 {
14987 case DW_FORM_sdata:
14988 case DW_FORM_udata:
14989 case DW_FORM_data1:
14990 case DW_FORM_data2:
14991 case DW_FORM_data4:
14992 case DW_FORM_data8:
14993 return 1;
14994 default:
14995 return 0;
14996 }
14997}
14998
8cf6f0b1
TT
14999/* A helper function that fills in a dwarf2_loclist_baton. */
15000
15001static void
15002fill_in_loclist_baton (struct dwarf2_cu *cu,
15003 struct dwarf2_loclist_baton *baton,
15004 struct attribute *attr)
15005{
15006 dwarf2_read_section (dwarf2_per_objfile->objfile,
15007 &dwarf2_per_objfile->loc);
15008
15009 baton->per_cu = cu->per_cu;
15010 gdb_assert (baton->per_cu);
15011 /* We don't know how long the location list is, but make sure we
15012 don't run off the edge of the section. */
15013 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15014 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15015 baton->base_address = cu->base_address;
15016}
15017
4c2df51b
DJ
15018static void
15019dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15020 struct dwarf2_cu *cu)
4c2df51b 15021{
3690dd37 15022 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15023 /* ".debug_loc" may not exist at all, or the offset may be outside
15024 the section. If so, fall through to the complaint in the
15025 other branch. */
9e0ac564
TT
15026 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15027 &dwarf2_per_objfile->loc))
4c2df51b 15028 {
0d53c4c4 15029 struct dwarf2_loclist_baton *baton;
4c2df51b 15030
4a146b47 15031 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15032 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15033
8cf6f0b1 15034 fill_in_loclist_baton (cu, baton, attr);
be391dca 15035
d00adf39 15036 if (cu->base_known == 0)
0d53c4c4 15037 complaint (&symfile_complaints,
3e43a32a
MS
15038 _("Location list used without "
15039 "specifying the CU base address."));
4c2df51b 15040
768a979c 15041 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15042 SYMBOL_LOCATION_BATON (sym) = baton;
15043 }
15044 else
15045 {
15046 struct dwarf2_locexpr_baton *baton;
15047
4a146b47 15048 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15049 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15050 baton->per_cu = cu->per_cu;
15051 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15052
15053 if (attr_form_is_block (attr))
15054 {
15055 /* Note that we're just copying the block's data pointer
15056 here, not the actual data. We're still pointing into the
6502dd73
DJ
15057 info_buffer for SYM's objfile; right now we never release
15058 that buffer, but when we do clean up properly this may
15059 need to change. */
0d53c4c4
DJ
15060 baton->size = DW_BLOCK (attr)->size;
15061 baton->data = DW_BLOCK (attr)->data;
15062 }
15063 else
15064 {
15065 dwarf2_invalid_attrib_class_complaint ("location description",
15066 SYMBOL_NATURAL_NAME (sym));
15067 baton->size = 0;
15068 baton->data = NULL;
15069 }
6e70227d 15070
768a979c 15071 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15072 SYMBOL_LOCATION_BATON (sym) = baton;
15073 }
4c2df51b 15074}
6502dd73 15075
9aa1f1e3
TT
15076/* Return the OBJFILE associated with the compilation unit CU. If CU
15077 came from a separate debuginfo file, then the master objfile is
15078 returned. */
ae0d2f24
UW
15079
15080struct objfile *
15081dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15082{
9291a0cd 15083 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15084
15085 /* Return the master objfile, so that we can report and look up the
15086 correct file containing this variable. */
15087 if (objfile->separate_debug_objfile_backlink)
15088 objfile = objfile->separate_debug_objfile_backlink;
15089
15090 return objfile;
15091}
15092
15093/* Return the address size given in the compilation unit header for CU. */
15094
15095CORE_ADDR
15096dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15097{
15098 if (per_cu->cu)
15099 return per_cu->cu->header.addr_size;
15100 else
15101 {
15102 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 15103 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15104 struct dwarf2_per_objfile *per_objfile
15105 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 15106 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 15107 struct comp_unit_head cu_header;
9a619af0 15108
ae0d2f24
UW
15109 memset (&cu_header, 0, sizeof cu_header);
15110 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15111 return cu_header.addr_size;
15112 }
15113}
15114
9eae7c52
TT
15115/* Return the offset size given in the compilation unit header for CU. */
15116
15117int
15118dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15119{
15120 if (per_cu->cu)
15121 return per_cu->cu->header.offset_size;
15122 else
15123 {
15124 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 15125 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
15126 struct dwarf2_per_objfile *per_objfile
15127 = objfile_data (objfile, dwarf2_objfile_data_key);
15128 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15129 struct comp_unit_head cu_header;
15130
15131 memset (&cu_header, 0, sizeof cu_header);
15132 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15133 return cu_header.offset_size;
15134 }
15135}
15136
9aa1f1e3
TT
15137/* Return the text offset of the CU. The returned offset comes from
15138 this CU's objfile. If this objfile came from a separate debuginfo
15139 file, then the offset may be different from the corresponding
15140 offset in the parent objfile. */
15141
15142CORE_ADDR
15143dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15144{
bb3fa9d0 15145 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15146
15147 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15148}
15149
348e048f
DE
15150/* Locate the .debug_info compilation unit from CU's objfile which contains
15151 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15152
15153static struct dwarf2_per_cu_data *
c764a876 15154dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15155 struct objfile *objfile)
15156{
15157 struct dwarf2_per_cu_data *this_cu;
15158 int low, high;
15159
ae038cb0
DJ
15160 low = 0;
15161 high = dwarf2_per_objfile->n_comp_units - 1;
15162 while (high > low)
15163 {
15164 int mid = low + (high - low) / 2;
9a619af0 15165
ae038cb0
DJ
15166 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15167 high = mid;
15168 else
15169 low = mid + 1;
15170 }
15171 gdb_assert (low == high);
15172 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15173 {
10b3939b 15174 if (low == 0)
8a3fe4f8
AC
15175 error (_("Dwarf Error: could not find partial DIE containing "
15176 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15177 (long) offset, bfd_get_filename (objfile->obfd));
15178
ae038cb0
DJ
15179 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15180 return dwarf2_per_objfile->all_comp_units[low-1];
15181 }
15182 else
15183 {
15184 this_cu = dwarf2_per_objfile->all_comp_units[low];
15185 if (low == dwarf2_per_objfile->n_comp_units - 1
15186 && offset >= this_cu->offset + this_cu->length)
c764a876 15187 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15188 gdb_assert (offset < this_cu->offset + this_cu->length);
15189 return this_cu;
15190 }
15191}
15192
10b3939b
DJ
15193/* Locate the compilation unit from OBJFILE which is located at exactly
15194 OFFSET. Raises an error on failure. */
15195
ae038cb0 15196static struct dwarf2_per_cu_data *
c764a876 15197dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15198{
15199 struct dwarf2_per_cu_data *this_cu;
9a619af0 15200
ae038cb0
DJ
15201 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15202 if (this_cu->offset != offset)
c764a876 15203 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15204 return this_cu;
15205}
15206
9816fde3 15207/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15208
9816fde3
JK
15209static void
15210init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15211{
9816fde3 15212 memset (cu, 0, sizeof (*cu));
93311388
DE
15213 cu->objfile = objfile;
15214 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15215}
15216
15217/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15218
15219static void
15220prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15221{
15222 struct attribute *attr;
15223
15224 /* Set the language we're debugging. */
15225 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15226 if (attr)
15227 set_cu_language (DW_UNSND (attr), cu);
15228 else
9cded63f
TT
15229 {
15230 cu->language = language_minimal;
15231 cu->language_defn = language_def (cu->language);
15232 }
93311388
DE
15233}
15234
ae038cb0
DJ
15235/* Release one cached compilation unit, CU. We unlink it from the tree
15236 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15237 the caller is responsible for that.
15238 NOTE: DATA is a void * because this function is also used as a
15239 cleanup routine. */
ae038cb0
DJ
15240
15241static void
15242free_one_comp_unit (void *data)
15243{
15244 struct dwarf2_cu *cu = data;
15245
15246 if (cu->per_cu != NULL)
15247 cu->per_cu->cu = NULL;
15248 cu->per_cu = NULL;
15249
15250 obstack_free (&cu->comp_unit_obstack, NULL);
15251
15252 xfree (cu);
15253}
15254
72bf9492 15255/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15256 when we're finished with it. We can't free the pointer itself, but be
15257 sure to unlink it from the cache. Also release any associated storage
15258 and perform cache maintenance.
72bf9492
DJ
15259
15260 Only used during partial symbol parsing. */
15261
15262static void
15263free_stack_comp_unit (void *data)
15264{
15265 struct dwarf2_cu *cu = data;
15266
15267 obstack_free (&cu->comp_unit_obstack, NULL);
15268 cu->partial_dies = NULL;
ae038cb0
DJ
15269
15270 if (cu->per_cu != NULL)
15271 {
15272 /* This compilation unit is on the stack in our caller, so we
15273 should not xfree it. Just unlink it. */
15274 cu->per_cu->cu = NULL;
15275 cu->per_cu = NULL;
15276
15277 /* If we had a per-cu pointer, then we may have other compilation
15278 units loaded, so age them now. */
15279 age_cached_comp_units ();
15280 }
15281}
15282
15283/* Free all cached compilation units. */
15284
15285static void
15286free_cached_comp_units (void *data)
15287{
15288 struct dwarf2_per_cu_data *per_cu, **last_chain;
15289
15290 per_cu = dwarf2_per_objfile->read_in_chain;
15291 last_chain = &dwarf2_per_objfile->read_in_chain;
15292 while (per_cu != NULL)
15293 {
15294 struct dwarf2_per_cu_data *next_cu;
15295
15296 next_cu = per_cu->cu->read_in_chain;
15297
15298 free_one_comp_unit (per_cu->cu);
15299 *last_chain = next_cu;
15300
15301 per_cu = next_cu;
15302 }
15303}
15304
15305/* Increase the age counter on each cached compilation unit, and free
15306 any that are too old. */
15307
15308static void
15309age_cached_comp_units (void)
15310{
15311 struct dwarf2_per_cu_data *per_cu, **last_chain;
15312
15313 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15314 per_cu = dwarf2_per_objfile->read_in_chain;
15315 while (per_cu != NULL)
15316 {
15317 per_cu->cu->last_used ++;
15318 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15319 dwarf2_mark (per_cu->cu);
15320 per_cu = per_cu->cu->read_in_chain;
15321 }
15322
15323 per_cu = dwarf2_per_objfile->read_in_chain;
15324 last_chain = &dwarf2_per_objfile->read_in_chain;
15325 while (per_cu != NULL)
15326 {
15327 struct dwarf2_per_cu_data *next_cu;
15328
15329 next_cu = per_cu->cu->read_in_chain;
15330
15331 if (!per_cu->cu->mark)
15332 {
15333 free_one_comp_unit (per_cu->cu);
15334 *last_chain = next_cu;
15335 }
15336 else
15337 last_chain = &per_cu->cu->read_in_chain;
15338
15339 per_cu = next_cu;
15340 }
15341}
15342
15343/* Remove a single compilation unit from the cache. */
15344
15345static void
15346free_one_cached_comp_unit (void *target_cu)
15347{
15348 struct dwarf2_per_cu_data *per_cu, **last_chain;
15349
15350 per_cu = dwarf2_per_objfile->read_in_chain;
15351 last_chain = &dwarf2_per_objfile->read_in_chain;
15352 while (per_cu != NULL)
15353 {
15354 struct dwarf2_per_cu_data *next_cu;
15355
15356 next_cu = per_cu->cu->read_in_chain;
15357
15358 if (per_cu->cu == target_cu)
15359 {
15360 free_one_comp_unit (per_cu->cu);
15361 *last_chain = next_cu;
15362 break;
15363 }
15364 else
15365 last_chain = &per_cu->cu->read_in_chain;
15366
15367 per_cu = next_cu;
15368 }
15369}
15370
fe3e1990
DJ
15371/* Release all extra memory associated with OBJFILE. */
15372
15373void
15374dwarf2_free_objfile (struct objfile *objfile)
15375{
15376 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15377
15378 if (dwarf2_per_objfile == NULL)
15379 return;
15380
15381 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15382 free_cached_comp_units (NULL);
15383
7b9f3c50
DE
15384 if (dwarf2_per_objfile->quick_file_names_table)
15385 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15386
fe3e1990
DJ
15387 /* Everything else should be on the objfile obstack. */
15388}
15389
1c379e20
DJ
15390/* A pair of DIE offset and GDB type pointer. We store these
15391 in a hash table separate from the DIEs, and preserve them
15392 when the DIEs are flushed out of cache. */
15393
15394struct dwarf2_offset_and_type
15395{
15396 unsigned int offset;
15397 struct type *type;
15398};
15399
15400/* Hash function for a dwarf2_offset_and_type. */
15401
15402static hashval_t
15403offset_and_type_hash (const void *item)
15404{
15405 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15406
1c379e20
DJ
15407 return ofs->offset;
15408}
15409
15410/* Equality function for a dwarf2_offset_and_type. */
15411
15412static int
15413offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15414{
15415 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15416 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15417
1c379e20
DJ
15418 return ofs_lhs->offset == ofs_rhs->offset;
15419}
15420
15421/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15422 table if necessary. For convenience, return TYPE.
15423
15424 The DIEs reading must have careful ordering to:
15425 * Not cause infite loops trying to read in DIEs as a prerequisite for
15426 reading current DIE.
15427 * Not trying to dereference contents of still incompletely read in types
15428 while reading in other DIEs.
15429 * Enable referencing still incompletely read in types just by a pointer to
15430 the type without accessing its fields.
15431
15432 Therefore caller should follow these rules:
15433 * Try to fetch any prerequisite types we may need to build this DIE type
15434 before building the type and calling set_die_type.
e71ec853 15435 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15436 possible before fetching more types to complete the current type.
15437 * Make the type as complete as possible before fetching more types. */
1c379e20 15438
f792889a 15439static struct type *
1c379e20
DJ
15440set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15441{
15442 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15443 struct objfile *objfile = cu->objfile;
15444 htab_t *type_hash_ptr;
1c379e20 15445
b4ba55a1
JB
15446 /* For Ada types, make sure that the gnat-specific data is always
15447 initialized (if not already set). There are a few types where
15448 we should not be doing so, because the type-specific area is
15449 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15450 where the type-specific area is used to store the floatformat).
15451 But this is not a problem, because the gnat-specific information
15452 is actually not needed for these types. */
15453 if (need_gnat_info (cu)
15454 && TYPE_CODE (type) != TYPE_CODE_FUNC
15455 && TYPE_CODE (type) != TYPE_CODE_FLT
15456 && !HAVE_GNAT_AUX_INFO (type))
15457 INIT_GNAT_SPECIFIC (type);
15458
8b70b953 15459 if (cu->per_cu->debug_type_section)
673bfd45
DE
15460 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15461 else
15462 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15463
15464 if (*type_hash_ptr == NULL)
f792889a 15465 {
673bfd45
DE
15466 *type_hash_ptr
15467 = htab_create_alloc_ex (127,
f792889a
DJ
15468 offset_and_type_hash,
15469 offset_and_type_eq,
15470 NULL,
673bfd45 15471 &objfile->objfile_obstack,
f792889a
DJ
15472 hashtab_obstack_allocate,
15473 dummy_obstack_deallocate);
f792889a 15474 }
1c379e20
DJ
15475
15476 ofs.offset = die->offset;
15477 ofs.type = type;
15478 slot = (struct dwarf2_offset_and_type **)
673bfd45 15479 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15480 if (*slot)
15481 complaint (&symfile_complaints,
15482 _("A problem internal to GDB: DIE 0x%x has type already set"),
15483 die->offset);
673bfd45 15484 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15485 **slot = ofs;
f792889a 15486 return type;
1c379e20
DJ
15487}
15488
673bfd45
DE
15489/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15490 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15491
15492static struct type *
673bfd45
DE
15493get_die_type_at_offset (unsigned int offset,
15494 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15495{
15496 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15497 htab_t type_hash;
f792889a 15498
8b70b953 15499 if (per_cu->debug_type_section)
673bfd45
DE
15500 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15501 else
15502 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15503 if (type_hash == NULL)
15504 return NULL;
1c379e20 15505
673bfd45 15506 ofs.offset = offset;
1c379e20
DJ
15507 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15508 if (slot)
15509 return slot->type;
15510 else
15511 return NULL;
15512}
15513
673bfd45
DE
15514/* Look up the type for DIE in the appropriate type_hash table,
15515 or return NULL if DIE does not have a saved type. */
15516
15517static struct type *
15518get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15519{
15520 return get_die_type_at_offset (die->offset, cu->per_cu);
15521}
15522
10b3939b
DJ
15523/* Add a dependence relationship from CU to REF_PER_CU. */
15524
15525static void
15526dwarf2_add_dependence (struct dwarf2_cu *cu,
15527 struct dwarf2_per_cu_data *ref_per_cu)
15528{
15529 void **slot;
15530
15531 if (cu->dependencies == NULL)
15532 cu->dependencies
15533 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15534 NULL, &cu->comp_unit_obstack,
15535 hashtab_obstack_allocate,
15536 dummy_obstack_deallocate);
15537
15538 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15539 if (*slot == NULL)
15540 *slot = ref_per_cu;
15541}
1c379e20 15542
f504f079
DE
15543/* Subroutine of dwarf2_mark to pass to htab_traverse.
15544 Set the mark field in every compilation unit in the
ae038cb0
DJ
15545 cache that we must keep because we are keeping CU. */
15546
10b3939b
DJ
15547static int
15548dwarf2_mark_helper (void **slot, void *data)
15549{
15550 struct dwarf2_per_cu_data *per_cu;
15551
15552 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
15553
15554 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15555 reading of the chain. As such dependencies remain valid it is not much
15556 useful to track and undo them during QUIT cleanups. */
15557 if (per_cu->cu == NULL)
15558 return 1;
15559
10b3939b
DJ
15560 if (per_cu->cu->mark)
15561 return 1;
15562 per_cu->cu->mark = 1;
15563
15564 if (per_cu->cu->dependencies != NULL)
15565 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15566
15567 return 1;
15568}
15569
f504f079
DE
15570/* Set the mark field in CU and in every other compilation unit in the
15571 cache that we must keep because we are keeping CU. */
15572
ae038cb0
DJ
15573static void
15574dwarf2_mark (struct dwarf2_cu *cu)
15575{
15576 if (cu->mark)
15577 return;
15578 cu->mark = 1;
10b3939b
DJ
15579 if (cu->dependencies != NULL)
15580 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15581}
15582
15583static void
15584dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15585{
15586 while (per_cu)
15587 {
15588 per_cu->cu->mark = 0;
15589 per_cu = per_cu->cu->read_in_chain;
15590 }
72bf9492
DJ
15591}
15592
72bf9492
DJ
15593/* Trivial hash function for partial_die_info: the hash value of a DIE
15594 is its offset in .debug_info for this objfile. */
15595
15596static hashval_t
15597partial_die_hash (const void *item)
15598{
15599 const struct partial_die_info *part_die = item;
9a619af0 15600
72bf9492
DJ
15601 return part_die->offset;
15602}
15603
15604/* Trivial comparison function for partial_die_info structures: two DIEs
15605 are equal if they have the same offset. */
15606
15607static int
15608partial_die_eq (const void *item_lhs, const void *item_rhs)
15609{
15610 const struct partial_die_info *part_die_lhs = item_lhs;
15611 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15612
72bf9492
DJ
15613 return part_die_lhs->offset == part_die_rhs->offset;
15614}
15615
ae038cb0
DJ
15616static struct cmd_list_element *set_dwarf2_cmdlist;
15617static struct cmd_list_element *show_dwarf2_cmdlist;
15618
15619static void
15620set_dwarf2_cmd (char *args, int from_tty)
15621{
15622 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15623}
15624
15625static void
15626show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15627{
ae038cb0
DJ
15628 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15629}
15630
dce234bc
PP
15631/* If section described by INFO was mmapped, munmap it now. */
15632
15633static void
15634munmap_section_buffer (struct dwarf2_section_info *info)
15635{
b315ab21 15636 if (info->map_addr != NULL)
dce234bc
PP
15637 {
15638#ifdef HAVE_MMAP
b315ab21 15639 int res;
9a619af0 15640
b315ab21
TG
15641 res = munmap (info->map_addr, info->map_len);
15642 gdb_assert (res == 0);
dce234bc
PP
15643#else
15644 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15645 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15646#endif
15647 }
15648}
15649
15650/* munmap debug sections for OBJFILE, if necessary. */
15651
15652static void
c1bd65d0 15653dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15654{
15655 struct dwarf2_per_objfile *data = d;
8b70b953
TT
15656 int ix;
15657 struct dwarf2_section_info *section;
9a619af0 15658
16be1145
DE
15659 /* This is sorted according to the order they're defined in to make it easier
15660 to keep in sync. */
dce234bc
PP
15661 munmap_section_buffer (&data->info);
15662 munmap_section_buffer (&data->abbrev);
15663 munmap_section_buffer (&data->line);
16be1145 15664 munmap_section_buffer (&data->loc);
dce234bc 15665 munmap_section_buffer (&data->macinfo);
16be1145 15666 munmap_section_buffer (&data->str);
dce234bc 15667 munmap_section_buffer (&data->ranges);
dce234bc
PP
15668 munmap_section_buffer (&data->frame);
15669 munmap_section_buffer (&data->eh_frame);
9291a0cd 15670 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
15671
15672 for (ix = 0;
15673 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15674 ++ix)
15675 munmap_section_buffer (section);
15676
15677 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
15678}
15679
15680\f
ae2de4f8 15681/* The "save gdb-index" command. */
9291a0cd
TT
15682
15683/* The contents of the hash table we create when building the string
15684 table. */
15685struct strtab_entry
15686{
15687 offset_type offset;
15688 const char *str;
15689};
15690
559a7a62
JK
15691/* Hash function for a strtab_entry.
15692
15693 Function is used only during write_hash_table so no index format backward
15694 compatibility is needed. */
b89be57b 15695
9291a0cd
TT
15696static hashval_t
15697hash_strtab_entry (const void *e)
15698{
15699 const struct strtab_entry *entry = e;
559a7a62 15700 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15701}
15702
15703/* Equality function for a strtab_entry. */
b89be57b 15704
9291a0cd
TT
15705static int
15706eq_strtab_entry (const void *a, const void *b)
15707{
15708 const struct strtab_entry *ea = a;
15709 const struct strtab_entry *eb = b;
15710 return !strcmp (ea->str, eb->str);
15711}
15712
15713/* Create a strtab_entry hash table. */
b89be57b 15714
9291a0cd
TT
15715static htab_t
15716create_strtab (void)
15717{
15718 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15719 xfree, xcalloc, xfree);
15720}
15721
15722/* Add a string to the constant pool. Return the string's offset in
15723 host order. */
b89be57b 15724
9291a0cd
TT
15725static offset_type
15726add_string (htab_t table, struct obstack *cpool, const char *str)
15727{
15728 void **slot;
15729 struct strtab_entry entry;
15730 struct strtab_entry *result;
15731
15732 entry.str = str;
15733 slot = htab_find_slot (table, &entry, INSERT);
15734 if (*slot)
15735 result = *slot;
15736 else
15737 {
15738 result = XNEW (struct strtab_entry);
15739 result->offset = obstack_object_size (cpool);
15740 result->str = str;
15741 obstack_grow_str0 (cpool, str);
15742 *slot = result;
15743 }
15744 return result->offset;
15745}
15746
15747/* An entry in the symbol table. */
15748struct symtab_index_entry
15749{
15750 /* The name of the symbol. */
15751 const char *name;
15752 /* The offset of the name in the constant pool. */
15753 offset_type index_offset;
15754 /* A sorted vector of the indices of all the CUs that hold an object
15755 of this name. */
15756 VEC (offset_type) *cu_indices;
15757};
15758
15759/* The symbol table. This is a power-of-2-sized hash table. */
15760struct mapped_symtab
15761{
15762 offset_type n_elements;
15763 offset_type size;
15764 struct symtab_index_entry **data;
15765};
15766
15767/* Hash function for a symtab_index_entry. */
b89be57b 15768
9291a0cd
TT
15769static hashval_t
15770hash_symtab_entry (const void *e)
15771{
15772 const struct symtab_index_entry *entry = e;
15773 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15774 sizeof (offset_type) * VEC_length (offset_type,
15775 entry->cu_indices),
15776 0);
15777}
15778
15779/* Equality function for a symtab_index_entry. */
b89be57b 15780
9291a0cd
TT
15781static int
15782eq_symtab_entry (const void *a, const void *b)
15783{
15784 const struct symtab_index_entry *ea = a;
15785 const struct symtab_index_entry *eb = b;
15786 int len = VEC_length (offset_type, ea->cu_indices);
15787 if (len != VEC_length (offset_type, eb->cu_indices))
15788 return 0;
15789 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15790 VEC_address (offset_type, eb->cu_indices),
15791 sizeof (offset_type) * len);
15792}
15793
15794/* Destroy a symtab_index_entry. */
b89be57b 15795
9291a0cd
TT
15796static void
15797delete_symtab_entry (void *p)
15798{
15799 struct symtab_index_entry *entry = p;
15800 VEC_free (offset_type, entry->cu_indices);
15801 xfree (entry);
15802}
15803
15804/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15805
9291a0cd 15806static htab_t
3876f04e 15807create_symbol_hash_table (void)
9291a0cd
TT
15808{
15809 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15810 delete_symtab_entry, xcalloc, xfree);
15811}
15812
15813/* Create a new mapped symtab object. */
b89be57b 15814
9291a0cd
TT
15815static struct mapped_symtab *
15816create_mapped_symtab (void)
15817{
15818 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15819 symtab->n_elements = 0;
15820 symtab->size = 1024;
15821 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15822 return symtab;
15823}
15824
15825/* Destroy a mapped_symtab. */
b89be57b 15826
9291a0cd
TT
15827static void
15828cleanup_mapped_symtab (void *p)
15829{
15830 struct mapped_symtab *symtab = p;
15831 /* The contents of the array are freed when the other hash table is
15832 destroyed. */
15833 xfree (symtab->data);
15834 xfree (symtab);
15835}
15836
15837/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15838 the slot.
15839
15840 Function is used only during write_hash_table so no index format backward
15841 compatibility is needed. */
b89be57b 15842
9291a0cd
TT
15843static struct symtab_index_entry **
15844find_slot (struct mapped_symtab *symtab, const char *name)
15845{
559a7a62 15846 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15847
15848 index = hash & (symtab->size - 1);
15849 step = ((hash * 17) & (symtab->size - 1)) | 1;
15850
15851 for (;;)
15852 {
15853 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15854 return &symtab->data[index];
15855 index = (index + step) & (symtab->size - 1);
15856 }
15857}
15858
15859/* Expand SYMTAB's hash table. */
b89be57b 15860
9291a0cd
TT
15861static void
15862hash_expand (struct mapped_symtab *symtab)
15863{
15864 offset_type old_size = symtab->size;
15865 offset_type i;
15866 struct symtab_index_entry **old_entries = symtab->data;
15867
15868 symtab->size *= 2;
15869 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15870
15871 for (i = 0; i < old_size; ++i)
15872 {
15873 if (old_entries[i])
15874 {
15875 struct symtab_index_entry **slot = find_slot (symtab,
15876 old_entries[i]->name);
15877 *slot = old_entries[i];
15878 }
15879 }
15880
15881 xfree (old_entries);
15882}
15883
15884/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15885 is the index of the CU in which the symbol appears. */
b89be57b 15886
9291a0cd
TT
15887static void
15888add_index_entry (struct mapped_symtab *symtab, const char *name,
15889 offset_type cu_index)
15890{
15891 struct symtab_index_entry **slot;
15892
15893 ++symtab->n_elements;
15894 if (4 * symtab->n_elements / 3 >= symtab->size)
15895 hash_expand (symtab);
15896
15897 slot = find_slot (symtab, name);
15898 if (!*slot)
15899 {
15900 *slot = XNEW (struct symtab_index_entry);
15901 (*slot)->name = name;
15902 (*slot)->cu_indices = NULL;
15903 }
15904 /* Don't push an index twice. Due to how we add entries we only
15905 have to check the last one. */
15906 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15907 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15908 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15909}
15910
15911/* Add a vector of indices to the constant pool. */
b89be57b 15912
9291a0cd 15913static offset_type
3876f04e 15914add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15915 struct symtab_index_entry *entry)
15916{
15917 void **slot;
15918
3876f04e 15919 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15920 if (!*slot)
15921 {
15922 offset_type len = VEC_length (offset_type, entry->cu_indices);
15923 offset_type val = MAYBE_SWAP (len);
15924 offset_type iter;
15925 int i;
15926
15927 *slot = entry;
15928 entry->index_offset = obstack_object_size (cpool);
15929
15930 obstack_grow (cpool, &val, sizeof (val));
15931 for (i = 0;
15932 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15933 ++i)
15934 {
15935 val = MAYBE_SWAP (iter);
15936 obstack_grow (cpool, &val, sizeof (val));
15937 }
15938 }
15939 else
15940 {
15941 struct symtab_index_entry *old_entry = *slot;
15942 entry->index_offset = old_entry->index_offset;
15943 entry = old_entry;
15944 }
15945 return entry->index_offset;
15946}
15947
15948/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15949 constant pool entries going into the obstack CPOOL. */
b89be57b 15950
9291a0cd
TT
15951static void
15952write_hash_table (struct mapped_symtab *symtab,
15953 struct obstack *output, struct obstack *cpool)
15954{
15955 offset_type i;
3876f04e 15956 htab_t symbol_hash_table;
9291a0cd
TT
15957 htab_t str_table;
15958
3876f04e 15959 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15960 str_table = create_strtab ();
3876f04e 15961
9291a0cd
TT
15962 /* We add all the index vectors to the constant pool first, to
15963 ensure alignment is ok. */
15964 for (i = 0; i < symtab->size; ++i)
15965 {
15966 if (symtab->data[i])
3876f04e 15967 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15968 }
15969
15970 /* Now write out the hash table. */
15971 for (i = 0; i < symtab->size; ++i)
15972 {
15973 offset_type str_off, vec_off;
15974
15975 if (symtab->data[i])
15976 {
15977 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15978 vec_off = symtab->data[i]->index_offset;
15979 }
15980 else
15981 {
15982 /* While 0 is a valid constant pool index, it is not valid
15983 to have 0 for both offsets. */
15984 str_off = 0;
15985 vec_off = 0;
15986 }
15987
15988 str_off = MAYBE_SWAP (str_off);
15989 vec_off = MAYBE_SWAP (vec_off);
15990
15991 obstack_grow (output, &str_off, sizeof (str_off));
15992 obstack_grow (output, &vec_off, sizeof (vec_off));
15993 }
15994
15995 htab_delete (str_table);
3876f04e 15996 htab_delete (symbol_hash_table);
9291a0cd
TT
15997}
15998
0a5429f6
DE
15999/* Struct to map psymtab to CU index in the index file. */
16000struct psymtab_cu_index_map
16001{
16002 struct partial_symtab *psymtab;
16003 unsigned int cu_index;
16004};
16005
16006static hashval_t
16007hash_psymtab_cu_index (const void *item)
16008{
16009 const struct psymtab_cu_index_map *map = item;
16010
16011 return htab_hash_pointer (map->psymtab);
16012}
16013
16014static int
16015eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16016{
16017 const struct psymtab_cu_index_map *lhs = item_lhs;
16018 const struct psymtab_cu_index_map *rhs = item_rhs;
16019
16020 return lhs->psymtab == rhs->psymtab;
16021}
16022
16023/* Helper struct for building the address table. */
16024struct addrmap_index_data
16025{
16026 struct objfile *objfile;
16027 struct obstack *addr_obstack;
16028 htab_t cu_index_htab;
16029
16030 /* Non-zero if the previous_* fields are valid.
16031 We can't write an entry until we see the next entry (since it is only then
16032 that we know the end of the entry). */
16033 int previous_valid;
16034 /* Index of the CU in the table of all CUs in the index file. */
16035 unsigned int previous_cu_index;
0963b4bd 16036 /* Start address of the CU. */
0a5429f6
DE
16037 CORE_ADDR previous_cu_start;
16038};
16039
16040/* Write an address entry to OBSTACK. */
b89be57b 16041
9291a0cd 16042static void
0a5429f6
DE
16043add_address_entry (struct objfile *objfile, struct obstack *obstack,
16044 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16045{
0a5429f6 16046 offset_type cu_index_to_write;
9291a0cd
TT
16047 char addr[8];
16048 CORE_ADDR baseaddr;
16049
16050 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16051
0a5429f6
DE
16052 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16053 obstack_grow (obstack, addr, 8);
16054 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16055 obstack_grow (obstack, addr, 8);
16056 cu_index_to_write = MAYBE_SWAP (cu_index);
16057 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16058}
16059
16060/* Worker function for traversing an addrmap to build the address table. */
16061
16062static int
16063add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16064{
16065 struct addrmap_index_data *data = datap;
16066 struct partial_symtab *pst = obj;
16067 offset_type cu_index;
16068 void **slot;
16069
16070 if (data->previous_valid)
16071 add_address_entry (data->objfile, data->addr_obstack,
16072 data->previous_cu_start, start_addr,
16073 data->previous_cu_index);
16074
16075 data->previous_cu_start = start_addr;
16076 if (pst != NULL)
16077 {
16078 struct psymtab_cu_index_map find_map, *map;
16079 find_map.psymtab = pst;
16080 map = htab_find (data->cu_index_htab, &find_map);
16081 gdb_assert (map != NULL);
16082 data->previous_cu_index = map->cu_index;
16083 data->previous_valid = 1;
16084 }
16085 else
16086 data->previous_valid = 0;
16087
16088 return 0;
16089}
16090
16091/* Write OBJFILE's address map to OBSTACK.
16092 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16093 in the index file. */
16094
16095static void
16096write_address_map (struct objfile *objfile, struct obstack *obstack,
16097 htab_t cu_index_htab)
16098{
16099 struct addrmap_index_data addrmap_index_data;
16100
16101 /* When writing the address table, we have to cope with the fact that
16102 the addrmap iterator only provides the start of a region; we have to
16103 wait until the next invocation to get the start of the next region. */
16104
16105 addrmap_index_data.objfile = objfile;
16106 addrmap_index_data.addr_obstack = obstack;
16107 addrmap_index_data.cu_index_htab = cu_index_htab;
16108 addrmap_index_data.previous_valid = 0;
16109
16110 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16111 &addrmap_index_data);
16112
16113 /* It's highly unlikely the last entry (end address = 0xff...ff)
16114 is valid, but we should still handle it.
16115 The end address is recorded as the start of the next region, but that
16116 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16117 anyway. */
16118 if (addrmap_index_data.previous_valid)
16119 add_address_entry (objfile, obstack,
16120 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16121 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16122}
16123
16124/* Add a list of partial symbols to SYMTAB. */
b89be57b 16125
9291a0cd
TT
16126static void
16127write_psymbols (struct mapped_symtab *symtab,
987d643c 16128 htab_t psyms_seen,
9291a0cd
TT
16129 struct partial_symbol **psymp,
16130 int count,
987d643c
TT
16131 offset_type cu_index,
16132 int is_static)
9291a0cd
TT
16133{
16134 for (; count-- > 0; ++psymp)
16135 {
987d643c
TT
16136 void **slot, *lookup;
16137
9291a0cd
TT
16138 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16139 error (_("Ada is not currently supported by the index"));
987d643c
TT
16140
16141 /* We only want to add a given psymbol once. However, we also
16142 want to account for whether it is global or static. So, we
16143 may add it twice, using slightly different values. */
16144 if (is_static)
16145 {
16146 uintptr_t val = 1 | (uintptr_t) *psymp;
16147
16148 lookup = (void *) val;
16149 }
16150 else
16151 lookup = *psymp;
16152
16153 /* Only add a given psymbol once. */
16154 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16155 if (!*slot)
16156 {
16157 *slot = lookup;
16158 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16159 }
9291a0cd
TT
16160 }
16161}
16162
16163/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16164 exception if there is an error. */
b89be57b 16165
9291a0cd
TT
16166static void
16167write_obstack (FILE *file, struct obstack *obstack)
16168{
16169 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16170 file)
16171 != obstack_object_size (obstack))
16172 error (_("couldn't data write to file"));
16173}
16174
16175/* Unlink a file if the argument is not NULL. */
b89be57b 16176
9291a0cd
TT
16177static void
16178unlink_if_set (void *p)
16179{
16180 char **filename = p;
16181 if (*filename)
16182 unlink (*filename);
16183}
16184
1fd400ff
TT
16185/* A helper struct used when iterating over debug_types. */
16186struct signatured_type_index_data
16187{
16188 struct objfile *objfile;
16189 struct mapped_symtab *symtab;
16190 struct obstack *types_list;
987d643c 16191 htab_t psyms_seen;
1fd400ff
TT
16192 int cu_index;
16193};
16194
16195/* A helper function that writes a single signatured_type to an
16196 obstack. */
b89be57b 16197
1fd400ff
TT
16198static int
16199write_one_signatured_type (void **slot, void *d)
16200{
16201 struct signatured_type_index_data *info = d;
16202 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16203 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16204 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16205 gdb_byte val[8];
16206
16207 write_psymbols (info->symtab,
987d643c 16208 info->psyms_seen,
3e43a32a
MS
16209 info->objfile->global_psymbols.list
16210 + psymtab->globals_offset,
987d643c
TT
16211 psymtab->n_global_syms, info->cu_index,
16212 0);
1fd400ff 16213 write_psymbols (info->symtab,
987d643c 16214 info->psyms_seen,
3e43a32a
MS
16215 info->objfile->static_psymbols.list
16216 + psymtab->statics_offset,
987d643c
TT
16217 psymtab->n_static_syms, info->cu_index,
16218 1);
1fd400ff 16219
b3c8eb43 16220 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16221 obstack_grow (info->types_list, val, 8);
16222 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16223 obstack_grow (info->types_list, val, 8);
16224 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16225 obstack_grow (info->types_list, val, 8);
16226
16227 ++info->cu_index;
16228
16229 return 1;
16230}
16231
987d643c
TT
16232/* A cleanup function for an htab_t. */
16233
16234static void
16235cleanup_htab (void *arg)
16236{
16237 htab_delete (arg);
16238}
16239
9291a0cd 16240/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16241
9291a0cd
TT
16242static void
16243write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16244{
16245 struct cleanup *cleanup;
16246 char *filename, *cleanup_filename;
1fd400ff
TT
16247 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16248 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16249 int i;
16250 FILE *out_file;
16251 struct mapped_symtab *symtab;
16252 offset_type val, size_of_contents, total_len;
16253 struct stat st;
16254 char buf[8];
987d643c 16255 htab_t psyms_seen;
0a5429f6
DE
16256 htab_t cu_index_htab;
16257 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16258
b4f2f049 16259 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16260 return;
b4f2f049 16261
9291a0cd
TT
16262 if (dwarf2_per_objfile->using_index)
16263 error (_("Cannot use an index to create the index"));
16264
8b70b953
TT
16265 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16266 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16267
9291a0cd 16268 if (stat (objfile->name, &st) < 0)
7e17e088 16269 perror_with_name (objfile->name);
9291a0cd
TT
16270
16271 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16272 INDEX_SUFFIX, (char *) NULL);
16273 cleanup = make_cleanup (xfree, filename);
16274
16275 out_file = fopen (filename, "wb");
16276 if (!out_file)
16277 error (_("Can't open `%s' for writing"), filename);
16278
16279 cleanup_filename = filename;
16280 make_cleanup (unlink_if_set, &cleanup_filename);
16281
16282 symtab = create_mapped_symtab ();
16283 make_cleanup (cleanup_mapped_symtab, symtab);
16284
16285 obstack_init (&addr_obstack);
16286 make_cleanup_obstack_free (&addr_obstack);
16287
16288 obstack_init (&cu_list);
16289 make_cleanup_obstack_free (&cu_list);
16290
1fd400ff
TT
16291 obstack_init (&types_cu_list);
16292 make_cleanup_obstack_free (&types_cu_list);
16293
987d643c
TT
16294 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16295 NULL, xcalloc, xfree);
16296 make_cleanup (cleanup_htab, psyms_seen);
16297
0a5429f6
DE
16298 /* While we're scanning CU's create a table that maps a psymtab pointer
16299 (which is what addrmap records) to its index (which is what is recorded
16300 in the index file). This will later be needed to write the address
16301 table. */
16302 cu_index_htab = htab_create_alloc (100,
16303 hash_psymtab_cu_index,
16304 eq_psymtab_cu_index,
16305 NULL, xcalloc, xfree);
16306 make_cleanup (cleanup_htab, cu_index_htab);
16307 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16308 xmalloc (sizeof (struct psymtab_cu_index_map)
16309 * dwarf2_per_objfile->n_comp_units);
16310 make_cleanup (xfree, psymtab_cu_index_map);
16311
16312 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16313 work here. Also, the debug_types entries do not appear in
16314 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16315 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16316 {
3e43a32a
MS
16317 struct dwarf2_per_cu_data *per_cu
16318 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16319 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16320 gdb_byte val[8];
0a5429f6
DE
16321 struct psymtab_cu_index_map *map;
16322 void **slot;
9291a0cd
TT
16323
16324 write_psymbols (symtab,
987d643c 16325 psyms_seen,
9291a0cd 16326 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16327 psymtab->n_global_syms, i,
16328 0);
9291a0cd 16329 write_psymbols (symtab,
987d643c 16330 psyms_seen,
9291a0cd 16331 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16332 psymtab->n_static_syms, i,
16333 1);
9291a0cd 16334
0a5429f6
DE
16335 map = &psymtab_cu_index_map[i];
16336 map->psymtab = psymtab;
16337 map->cu_index = i;
16338 slot = htab_find_slot (cu_index_htab, map, INSERT);
16339 gdb_assert (slot != NULL);
16340 gdb_assert (*slot == NULL);
16341 *slot = map;
9291a0cd 16342
e254ef6a 16343 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16344 obstack_grow (&cu_list, val, 8);
e254ef6a 16345 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16346 obstack_grow (&cu_list, val, 8);
16347 }
16348
0a5429f6
DE
16349 /* Dump the address map. */
16350 write_address_map (objfile, &addr_obstack, cu_index_htab);
16351
1fd400ff
TT
16352 /* Write out the .debug_type entries, if any. */
16353 if (dwarf2_per_objfile->signatured_types)
16354 {
16355 struct signatured_type_index_data sig_data;
16356
16357 sig_data.objfile = objfile;
16358 sig_data.symtab = symtab;
16359 sig_data.types_list = &types_cu_list;
987d643c 16360 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16361 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16362 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16363 write_one_signatured_type, &sig_data);
16364 }
16365
9291a0cd
TT
16366 obstack_init (&constant_pool);
16367 make_cleanup_obstack_free (&constant_pool);
16368 obstack_init (&symtab_obstack);
16369 make_cleanup_obstack_free (&symtab_obstack);
16370 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16371
16372 obstack_init (&contents);
16373 make_cleanup_obstack_free (&contents);
1fd400ff 16374 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16375 total_len = size_of_contents;
16376
16377 /* The version number. */
559a7a62 16378 val = MAYBE_SWAP (5);
9291a0cd
TT
16379 obstack_grow (&contents, &val, sizeof (val));
16380
16381 /* The offset of the CU list from the start of the file. */
16382 val = MAYBE_SWAP (total_len);
16383 obstack_grow (&contents, &val, sizeof (val));
16384 total_len += obstack_object_size (&cu_list);
16385
1fd400ff
TT
16386 /* The offset of the types CU list from the start of the file. */
16387 val = MAYBE_SWAP (total_len);
16388 obstack_grow (&contents, &val, sizeof (val));
16389 total_len += obstack_object_size (&types_cu_list);
16390
9291a0cd
TT
16391 /* The offset of the address table from the start of the file. */
16392 val = MAYBE_SWAP (total_len);
16393 obstack_grow (&contents, &val, sizeof (val));
16394 total_len += obstack_object_size (&addr_obstack);
16395
16396 /* The offset of the symbol table from the start of the file. */
16397 val = MAYBE_SWAP (total_len);
16398 obstack_grow (&contents, &val, sizeof (val));
16399 total_len += obstack_object_size (&symtab_obstack);
16400
16401 /* The offset of the constant pool from the start of the file. */
16402 val = MAYBE_SWAP (total_len);
16403 obstack_grow (&contents, &val, sizeof (val));
16404 total_len += obstack_object_size (&constant_pool);
16405
16406 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16407
16408 write_obstack (out_file, &contents);
16409 write_obstack (out_file, &cu_list);
1fd400ff 16410 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16411 write_obstack (out_file, &addr_obstack);
16412 write_obstack (out_file, &symtab_obstack);
16413 write_obstack (out_file, &constant_pool);
16414
16415 fclose (out_file);
16416
16417 /* We want to keep the file, so we set cleanup_filename to NULL
16418 here. See unlink_if_set. */
16419 cleanup_filename = NULL;
16420
16421 do_cleanups (cleanup);
16422}
16423
90476074
TT
16424/* Implementation of the `save gdb-index' command.
16425
16426 Note that the file format used by this command is documented in the
16427 GDB manual. Any changes here must be documented there. */
11570e71 16428
9291a0cd
TT
16429static void
16430save_gdb_index_command (char *arg, int from_tty)
16431{
16432 struct objfile *objfile;
16433
16434 if (!arg || !*arg)
96d19272 16435 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16436
16437 ALL_OBJFILES (objfile)
16438 {
16439 struct stat st;
16440
16441 /* If the objfile does not correspond to an actual file, skip it. */
16442 if (stat (objfile->name, &st) < 0)
16443 continue;
16444
16445 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16446 if (dwarf2_per_objfile)
16447 {
16448 volatile struct gdb_exception except;
16449
16450 TRY_CATCH (except, RETURN_MASK_ERROR)
16451 {
16452 write_psymtabs_to_index (objfile, arg);
16453 }
16454 if (except.reason < 0)
16455 exception_fprintf (gdb_stderr, except,
16456 _("Error while writing index for `%s': "),
16457 objfile->name);
16458 }
16459 }
dce234bc
PP
16460}
16461
9291a0cd
TT
16462\f
16463
9eae7c52
TT
16464int dwarf2_always_disassemble;
16465
16466static void
16467show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16468 struct cmd_list_element *c, const char *value)
16469{
3e43a32a
MS
16470 fprintf_filtered (file,
16471 _("Whether to always disassemble "
16472 "DWARF expressions is %s.\n"),
9eae7c52
TT
16473 value);
16474}
16475
900e11f9
JK
16476static void
16477show_check_physname (struct ui_file *file, int from_tty,
16478 struct cmd_list_element *c, const char *value)
16479{
16480 fprintf_filtered (file,
16481 _("Whether to check \"physname\" is %s.\n"),
16482 value);
16483}
16484
6502dd73
DJ
16485void _initialize_dwarf2_read (void);
16486
16487void
16488_initialize_dwarf2_read (void)
16489{
96d19272
JK
16490 struct cmd_list_element *c;
16491
dce234bc 16492 dwarf2_objfile_data_key
c1bd65d0 16493 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16494
1bedd215
AC
16495 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16496Set DWARF 2 specific variables.\n\
16497Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16498 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16499 0/*allow-unknown*/, &maintenance_set_cmdlist);
16500
1bedd215
AC
16501 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16502Show DWARF 2 specific variables\n\
16503Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16504 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16505 0/*allow-unknown*/, &maintenance_show_cmdlist);
16506
16507 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16508 &dwarf2_max_cache_age, _("\
16509Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16510Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16511A higher limit means that cached compilation units will be stored\n\
16512in memory longer, and more total memory will be used. Zero disables\n\
16513caching, which can slow down startup."),
2c5b56ce 16514 NULL,
920d2a44 16515 show_dwarf2_max_cache_age,
2c5b56ce 16516 &set_dwarf2_cmdlist,
ae038cb0 16517 &show_dwarf2_cmdlist);
d97bc12b 16518
9eae7c52
TT
16519 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16520 &dwarf2_always_disassemble, _("\
16521Set whether `info address' always disassembles DWARF expressions."), _("\
16522Show whether `info address' always disassembles DWARF expressions."), _("\
16523When enabled, DWARF expressions are always printed in an assembly-like\n\
16524syntax. When disabled, expressions will be printed in a more\n\
16525conversational style, when possible."),
16526 NULL,
16527 show_dwarf2_always_disassemble,
16528 &set_dwarf2_cmdlist,
16529 &show_dwarf2_cmdlist);
16530
d97bc12b
DE
16531 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16532Set debugging of the dwarf2 DIE reader."), _("\
16533Show debugging of the dwarf2 DIE reader."), _("\
16534When enabled (non-zero), DIEs are dumped after they are read in.\n\
16535The value is the maximum depth to print."),
16536 NULL,
16537 NULL,
16538 &setdebuglist, &showdebuglist);
9291a0cd 16539
900e11f9
JK
16540 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16541Set cross-checking of \"physname\" code against demangler."), _("\
16542Show cross-checking of \"physname\" code against demangler."), _("\
16543When enabled, GDB's internal \"physname\" code is checked against\n\
16544the demangler."),
16545 NULL, show_check_physname,
16546 &setdebuglist, &showdebuglist);
16547
96d19272 16548 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16549 _("\
fc1a9d6e 16550Save a gdb-index file.\n\
11570e71 16551Usage: save gdb-index DIRECTORY"),
96d19272
JK
16552 &save_cmdlist);
16553 set_cmd_completer (c, filename_completer);
6502dd73 16554}
This page took 2.179058 seconds and 4 git commands to generate.