remove comment in machoread.c (macho_symfile_read)
[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
dce234bc
PP
122static int pagesize;
123
df8a16a1
DJ
124/* When set, the file that we're processing is known to have debugging
125 info for C++ namespaces. GCC 3.3.x did not produce this information,
126 but later versions do. */
127
128static int processing_has_namespace_info;
129
6502dd73
DJ
130static const struct objfile_data *dwarf2_objfile_data_key;
131
dce234bc
PP
132struct dwarf2_section_info
133{
134 asection *asection;
135 gdb_byte *buffer;
136 bfd_size_type size;
b315ab21
TG
137 /* Not NULL if the section was actually mmapped. */
138 void *map_addr;
139 /* Page aligned size of mmapped area. */
140 bfd_size_type map_len;
be391dca
TT
141 /* True if we have tried to read this section. */
142 int readin;
dce234bc
PP
143};
144
9291a0cd
TT
145/* All offsets in the index are of this type. It must be
146 architecture-independent. */
147typedef uint32_t offset_type;
148
149DEF_VEC_I (offset_type);
150
151/* A description of the mapped index. The file format is described in
152 a comment by the code that writes the index. */
153struct mapped_index
154{
559a7a62
JK
155 /* Index data format version. */
156 int version;
157
9291a0cd
TT
158 /* The total length of the buffer. */
159 off_t total_size;
b11b1f88 160
9291a0cd
TT
161 /* A pointer to the address table data. */
162 const gdb_byte *address_table;
b11b1f88 163
9291a0cd
TT
164 /* Size of the address table data in bytes. */
165 offset_type address_table_size;
b11b1f88 166
3876f04e
DE
167 /* The symbol table, implemented as a hash table. */
168 const offset_type *symbol_table;
b11b1f88 169
9291a0cd 170 /* Size in slots, each slot is 2 offset_types. */
3876f04e 171 offset_type symbol_table_slots;
b11b1f88 172
9291a0cd
TT
173 /* A pointer to the constant pool. */
174 const char *constant_pool;
175};
176
6502dd73
DJ
177struct dwarf2_per_objfile
178{
dce234bc
PP
179 struct dwarf2_section_info info;
180 struct dwarf2_section_info abbrev;
181 struct dwarf2_section_info line;
dce234bc
PP
182 struct dwarf2_section_info loc;
183 struct dwarf2_section_info macinfo;
184 struct dwarf2_section_info str;
185 struct dwarf2_section_info ranges;
348e048f 186 struct dwarf2_section_info types;
dce234bc
PP
187 struct dwarf2_section_info frame;
188 struct dwarf2_section_info eh_frame;
9291a0cd 189 struct dwarf2_section_info gdb_index;
ae038cb0 190
be391dca
TT
191 /* Back link. */
192 struct objfile *objfile;
193
10b3939b
DJ
194 /* A list of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
ae038cb0
DJ
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
1fd400ff
TT
201 /* The number of .debug_types-related CUs. */
202 int n_type_comp_units;
203
204 /* The .debug_types-related CUs. */
205 struct dwarf2_per_cu_data **type_comp_units;
206
ae038cb0
DJ
207 /* A chain of compilation units that are currently read in, so that
208 they can be freed later. */
209 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 210
348e048f
DE
211 /* A table mapping .debug_types signatures to its signatured_type entry.
212 This is NULL if the .debug_types section hasn't been read in yet. */
213 htab_t signatured_types;
214
72dca2f5
FR
215 /* A flag indicating wether this objfile has a section loaded at a
216 VMA of 0. */
217 int has_section_at_zero;
9291a0cd 218
ae2de4f8
DE
219 /* True if we are using the mapped index,
220 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
221 unsigned char using_index;
222
ae2de4f8 223 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 224 struct mapped_index *index_table;
98bfdba5 225
7b9f3c50
DE
226 /* When using index_table, this keeps track of all quick_file_names entries.
227 TUs can share line table entries with CUs or other TUs, and there can be
228 a lot more TUs than unique line tables, so we maintain a separate table
229 of all line table entries to support the sharing. */
230 htab_t quick_file_names_table;
231
98bfdba5
PA
232 /* Set during partial symbol reading, to prevent queueing of full
233 symbols. */
234 int reading_partial_symbols;
673bfd45
DE
235
236 /* Table mapping type .debug_info DIE offsets to types.
237 This is NULL if not allocated yet.
238 It (currently) makes sense to allocate debug_types_type_hash lazily.
239 To keep things simple we allocate both lazily. */
240 htab_t debug_info_type_hash;
241
242 /* Table mapping type .debug_types DIE offsets to types.
243 This is NULL if not allocated yet. */
244 htab_t debug_types_type_hash;
6502dd73
DJ
245};
246
247static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 248
251d32d9 249/* Default names of the debugging sections. */
c906108c 250
233a11ab
CS
251/* Note that if the debugging section has been compressed, it might
252 have a name like .zdebug_info. */
253
251d32d9
TG
254static const struct dwarf2_debug_sections dwarf2_elf_names = {
255 { ".debug_info", ".zdebug_info" },
256 { ".debug_abbrev", ".zdebug_abbrev" },
257 { ".debug_line", ".zdebug_line" },
258 { ".debug_loc", ".zdebug_loc" },
259 { ".debug_macinfo", ".zdebug_macinfo" },
260 { ".debug_str", ".zdebug_str" },
261 { ".debug_ranges", ".zdebug_ranges" },
262 { ".debug_types", ".zdebug_types" },
263 { ".debug_frame", ".zdebug_frame" },
264 { ".eh_frame", NULL },
265 { ".gdb_index", ".zgdb_index" }
266};
c906108c
SS
267
268/* local data types */
269
0963b4bd 270/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
271#ifndef ABBREV_HASH_SIZE
272#define ABBREV_HASH_SIZE 121
273#endif
274
107d2387
AC
275/* The data in a compilation unit header, after target2host
276 translation, looks like this. */
c906108c 277struct comp_unit_head
a738430d 278{
c764a876 279 unsigned int length;
a738430d 280 short version;
a738430d
MK
281 unsigned char addr_size;
282 unsigned char signed_addr_p;
9cbfa09e 283 unsigned int abbrev_offset;
57349743 284
a738430d
MK
285 /* Size of file offsets; either 4 or 8. */
286 unsigned int offset_size;
57349743 287
a738430d
MK
288 /* Size of the length field; either 4 or 12. */
289 unsigned int initial_length_size;
57349743 290
a738430d
MK
291 /* Offset to the first byte of this compilation unit header in the
292 .debug_info section, for resolving relative reference dies. */
293 unsigned int offset;
57349743 294
d00adf39
DE
295 /* Offset to first die in this cu from the start of the cu.
296 This will be the first byte following the compilation unit header. */
297 unsigned int first_die_offset;
a738430d 298};
c906108c 299
3da10d80
KS
300/* Type used for delaying computation of method physnames.
301 See comments for compute_delayed_physnames. */
302struct delayed_method_info
303{
304 /* The type to which the method is attached, i.e., its parent class. */
305 struct type *type;
306
307 /* The index of the method in the type's function fieldlists. */
308 int fnfield_index;
309
310 /* The index of the method in the fieldlist. */
311 int index;
312
313 /* The name of the DIE. */
314 const char *name;
315
316 /* The DIE associated with this method. */
317 struct die_info *die;
318};
319
320typedef struct delayed_method_info delayed_method_info;
321DEF_VEC_O (delayed_method_info);
322
e7c27a73
DJ
323/* Internal state when decoding a particular compilation unit. */
324struct dwarf2_cu
325{
326 /* The objfile containing this compilation unit. */
327 struct objfile *objfile;
328
d00adf39 329 /* The header of the compilation unit. */
e7c27a73 330 struct comp_unit_head header;
e142c38c 331
d00adf39
DE
332 /* Base address of this compilation unit. */
333 CORE_ADDR base_address;
334
335 /* Non-zero if base_address has been set. */
336 int base_known;
337
e142c38c
DJ
338 struct function_range *first_fn, *last_fn, *cached_fn;
339
340 /* The language we are debugging. */
341 enum language language;
342 const struct language_defn *language_defn;
343
b0f35d58
DL
344 const char *producer;
345
e142c38c
DJ
346 /* The generic symbol table building routines have separate lists for
347 file scope symbols and all all other scopes (local scopes). So
348 we need to select the right one to pass to add_symbol_to_list().
349 We do it by keeping a pointer to the correct list in list_in_scope.
350
351 FIXME: The original dwarf code just treated the file scope as the
352 first local scope, and all other local scopes as nested local
353 scopes, and worked fine. Check to see if we really need to
354 distinguish these in buildsym.c. */
355 struct pending **list_in_scope;
356
f3dd6933
DJ
357 /* DWARF abbreviation table associated with this compilation unit. */
358 struct abbrev_info **dwarf2_abbrevs;
359
360 /* Storage for the abbrev table. */
361 struct obstack abbrev_obstack;
72bf9492
DJ
362
363 /* Hash table holding all the loaded partial DIEs. */
364 htab_t partial_dies;
365
366 /* Storage for things with the same lifetime as this read-in compilation
367 unit, including partial DIEs. */
368 struct obstack comp_unit_obstack;
369
ae038cb0
DJ
370 /* When multiple dwarf2_cu structures are living in memory, this field
371 chains them all together, so that they can be released efficiently.
372 We will probably also want a generation counter so that most-recently-used
373 compilation units are cached... */
374 struct dwarf2_per_cu_data *read_in_chain;
375
376 /* Backchain to our per_cu entry if the tree has been built. */
377 struct dwarf2_per_cu_data *per_cu;
378
379 /* How many compilation units ago was this CU last referenced? */
380 int last_used;
381
10b3939b 382 /* A hash table of die offsets for following references. */
51545339 383 htab_t die_hash;
10b3939b
DJ
384
385 /* Full DIEs if read in. */
386 struct die_info *dies;
387
388 /* A set of pointers to dwarf2_per_cu_data objects for compilation
389 units referenced by this one. Only set during full symbol processing;
390 partial symbol tables do not have dependencies. */
391 htab_t dependencies;
392
cb1df416
DJ
393 /* Header data from the line table, during full symbol processing. */
394 struct line_header *line_header;
395
3da10d80
KS
396 /* A list of methods which need to have physnames computed
397 after all type information has been read. */
398 VEC (delayed_method_info) *method_list;
399
ae038cb0
DJ
400 /* Mark used when releasing cached dies. */
401 unsigned int mark : 1;
402
403 /* This flag will be set if this compilation unit might include
404 inter-compilation-unit references. */
405 unsigned int has_form_ref_addr : 1;
406
72bf9492
DJ
407 /* This flag will be set if this compilation unit includes any
408 DW_TAG_namespace DIEs. If we know that there are explicit
409 DIEs for namespaces, we don't need to try to infer them
410 from mangled names. */
411 unsigned int has_namespace_info : 1;
8be455d7
JK
412
413 /* This CU references .debug_loc. See the symtab->locations_valid field.
414 This test is imperfect as there may exist optimized debug code not using
415 any location list and still facing inlining issues if handled as
416 unoptimized code. For a future better test see GCC PR other/32998. */
417
418 unsigned int has_loclist : 1;
e7c27a73
DJ
419};
420
10b3939b
DJ
421/* Persistent data held for a compilation unit, even when not
422 processing it. We put a pointer to this structure in the
423 read_symtab_private field of the psymtab. If we encounter
424 inter-compilation-unit references, we also maintain a sorted
425 list of all compilation units. */
426
ae038cb0
DJ
427struct dwarf2_per_cu_data
428{
348e048f 429 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 430 bytes should suffice to store the length of any compilation unit
45452591
DE
431 - if it doesn't, GDB will fall over anyway.
432 NOTE: Unlike comp_unit_head.length, this length includes
433 initial_length_size. */
c764a876 434 unsigned int offset;
348e048f 435 unsigned int length : 29;
ae038cb0
DJ
436
437 /* Flag indicating this compilation unit will be read in before
438 any of the current compilation units are processed. */
c764a876 439 unsigned int queued : 1;
ae038cb0 440
5afb4e99
DJ
441 /* This flag will be set if we need to load absolutely all DIEs
442 for this compilation unit, instead of just the ones we think
443 are interesting. It gets set if we look for a DIE in the
444 hash table and don't find it. */
445 unsigned int load_all_dies : 1;
446
348e048f
DE
447 /* Non-zero if this CU is from .debug_types.
448 Otherwise it's from .debug_info. */
449 unsigned int from_debug_types : 1;
450
17ea53c3
JK
451 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
452 of the CU cache it gets reset to NULL again. */
ae038cb0 453 struct dwarf2_cu *cu;
1c379e20 454
9291a0cd
TT
455 /* The corresponding objfile. */
456 struct objfile *objfile;
457
458 /* When using partial symbol tables, the 'psymtab' field is active.
459 Otherwise the 'quick' field is active. */
460 union
461 {
462 /* The partial symbol table associated with this compilation unit,
463 or NULL for partial units (which do not have an associated
464 symtab). */
465 struct partial_symtab *psymtab;
466
467 /* Data needed by the "quick" functions. */
468 struct dwarf2_per_cu_quick_data *quick;
469 } v;
ae038cb0
DJ
470};
471
348e048f
DE
472/* Entry in the signatured_types hash table. */
473
474struct signatured_type
475{
476 ULONGEST signature;
477
348e048f
DE
478 /* Offset in .debug_types of the type defined by this TU. */
479 unsigned int type_offset;
480
481 /* The CU(/TU) of this type. */
482 struct dwarf2_per_cu_data per_cu;
483};
484
0963b4bd
MS
485/* Struct used to pass misc. parameters to read_die_and_children, et
486 al. which are used for both .debug_info and .debug_types dies.
487 All parameters here are unchanging for the life of the call. This
488 struct exists to abstract away the constant parameters of die
489 reading. */
93311388
DE
490
491struct die_reader_specs
492{
493 /* The bfd of this objfile. */
494 bfd* abfd;
495
496 /* The CU of the DIE we are parsing. */
497 struct dwarf2_cu *cu;
498
499 /* Pointer to start of section buffer.
500 This is either the start of .debug_info or .debug_types. */
501 const gdb_byte *buffer;
502};
503
debd256d
JB
504/* The line number information for a compilation unit (found in the
505 .debug_line section) begins with a "statement program header",
506 which contains the following information. */
507struct line_header
508{
509 unsigned int total_length;
510 unsigned short version;
511 unsigned int header_length;
512 unsigned char minimum_instruction_length;
2dc7f7b3 513 unsigned char maximum_ops_per_instruction;
debd256d
JB
514 unsigned char default_is_stmt;
515 int line_base;
516 unsigned char line_range;
517 unsigned char opcode_base;
518
519 /* standard_opcode_lengths[i] is the number of operands for the
520 standard opcode whose value is i. This means that
521 standard_opcode_lengths[0] is unused, and the last meaningful
522 element is standard_opcode_lengths[opcode_base - 1]. */
523 unsigned char *standard_opcode_lengths;
524
525 /* The include_directories table. NOTE! These strings are not
526 allocated with xmalloc; instead, they are pointers into
527 debug_line_buffer. If you try to free them, `free' will get
528 indigestion. */
529 unsigned int num_include_dirs, include_dirs_size;
530 char **include_dirs;
531
532 /* The file_names table. NOTE! These strings are not allocated
533 with xmalloc; instead, they are pointers into debug_line_buffer.
534 Don't try to free them directly. */
535 unsigned int num_file_names, file_names_size;
536 struct file_entry
c906108c 537 {
debd256d
JB
538 char *name;
539 unsigned int dir_index;
540 unsigned int mod_time;
541 unsigned int length;
aaa75496 542 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 543 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
544 } *file_names;
545
546 /* The start and end of the statement program following this
6502dd73 547 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 548 gdb_byte *statement_program_start, *statement_program_end;
debd256d 549};
c906108c
SS
550
551/* When we construct a partial symbol table entry we only
0963b4bd 552 need this much information. */
c906108c
SS
553struct partial_die_info
554 {
72bf9492 555 /* Offset of this DIE. */
c906108c 556 unsigned int offset;
72bf9492
DJ
557
558 /* DWARF-2 tag for this DIE. */
559 ENUM_BITFIELD(dwarf_tag) tag : 16;
560
72bf9492
DJ
561 /* Assorted flags describing the data found in this DIE. */
562 unsigned int has_children : 1;
563 unsigned int is_external : 1;
564 unsigned int is_declaration : 1;
565 unsigned int has_type : 1;
566 unsigned int has_specification : 1;
567 unsigned int has_pc_info : 1;
568
569 /* Flag set if the SCOPE field of this structure has been
570 computed. */
571 unsigned int scope_set : 1;
572
fa4028e9
JB
573 /* Flag set if the DIE has a byte_size attribute. */
574 unsigned int has_byte_size : 1;
575
98bfdba5
PA
576 /* Flag set if any of the DIE's children are template arguments. */
577 unsigned int has_template_arguments : 1;
578
abc72ce4
DE
579 /* Flag set if fixup_partial_die has been called on this die. */
580 unsigned int fixup_called : 1;
581
72bf9492 582 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 583 sometimes a default name for unnamed DIEs. */
c906108c 584 char *name;
72bf9492 585
abc72ce4
DE
586 /* The linkage name, if present. */
587 const char *linkage_name;
588
72bf9492
DJ
589 /* The scope to prepend to our children. This is generally
590 allocated on the comp_unit_obstack, so will disappear
591 when this compilation unit leaves the cache. */
592 char *scope;
593
594 /* The location description associated with this DIE, if any. */
595 struct dwarf_block *locdesc;
596
597 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
598 CORE_ADDR lowpc;
599 CORE_ADDR highpc;
72bf9492 600
93311388 601 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 602 DW_AT_sibling, if any. */
abc72ce4
DE
603 /* NOTE: This member isn't strictly necessary, read_partial_die could
604 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 605 gdb_byte *sibling;
72bf9492
DJ
606
607 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
608 DW_AT_specification (or DW_AT_abstract_origin or
609 DW_AT_extension). */
610 unsigned int spec_offset;
611
612 /* Pointers to this DIE's parent, first child, and next sibling,
613 if any. */
614 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
615 };
616
0963b4bd 617/* This data structure holds the information of an abbrev. */
c906108c
SS
618struct abbrev_info
619 {
620 unsigned int number; /* number identifying abbrev */
621 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
622 unsigned short has_children; /* boolean */
623 unsigned short num_attrs; /* number of attributes */
c906108c
SS
624 struct attr_abbrev *attrs; /* an array of attribute descriptions */
625 struct abbrev_info *next; /* next in chain */
626 };
627
628struct attr_abbrev
629 {
9d25dd43
DE
630 ENUM_BITFIELD(dwarf_attribute) name : 16;
631 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
632 };
633
0963b4bd 634/* Attributes have a name and a value. */
b60c80d6
DJ
635struct attribute
636 {
9d25dd43 637 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
638 ENUM_BITFIELD(dwarf_form) form : 15;
639
640 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
641 field should be in u.str (existing only for DW_STRING) but it is kept
642 here for better struct attribute alignment. */
643 unsigned int string_is_canonical : 1;
644
b60c80d6
DJ
645 union
646 {
647 char *str;
648 struct dwarf_block *blk;
43bbcdc2
PH
649 ULONGEST unsnd;
650 LONGEST snd;
b60c80d6 651 CORE_ADDR addr;
348e048f 652 struct signatured_type *signatured_type;
b60c80d6
DJ
653 }
654 u;
655 };
656
0963b4bd 657/* This data structure holds a complete die structure. */
c906108c
SS
658struct die_info
659 {
76815b17
DE
660 /* DWARF-2 tag for this DIE. */
661 ENUM_BITFIELD(dwarf_tag) tag : 16;
662
663 /* Number of attributes */
98bfdba5
PA
664 unsigned char num_attrs;
665
666 /* True if we're presently building the full type name for the
667 type derived from this DIE. */
668 unsigned char building_fullname : 1;
76815b17
DE
669
670 /* Abbrev number */
671 unsigned int abbrev;
672
93311388 673 /* Offset in .debug_info or .debug_types section. */
76815b17 674 unsigned int offset;
78ba4af6
JB
675
676 /* The dies in a compilation unit form an n-ary tree. PARENT
677 points to this die's parent; CHILD points to the first child of
678 this node; and all the children of a given node are chained
4950bc1c 679 together via their SIBLING fields. */
639d11d3
DC
680 struct die_info *child; /* Its first child, if any. */
681 struct die_info *sibling; /* Its next sibling, if any. */
682 struct die_info *parent; /* Its parent, if any. */
c906108c 683
b60c80d6
DJ
684 /* An array of attributes, with NUM_ATTRS elements. There may be
685 zero, but it's not common and zero-sized arrays are not
686 sufficiently portable C. */
687 struct attribute attrs[1];
c906108c
SS
688 };
689
5fb290d7
DJ
690struct function_range
691{
692 const char *name;
693 CORE_ADDR lowpc, highpc;
694 int seen_line;
695 struct function_range *next;
696};
697
0963b4bd 698/* Get at parts of an attribute structure. */
c906108c
SS
699
700#define DW_STRING(attr) ((attr)->u.str)
8285870a 701#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
702#define DW_UNSND(attr) ((attr)->u.unsnd)
703#define DW_BLOCK(attr) ((attr)->u.blk)
704#define DW_SND(attr) ((attr)->u.snd)
705#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 706#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 707
0963b4bd 708/* Blocks are a bunch of untyped bytes. */
c906108c
SS
709struct dwarf_block
710 {
711 unsigned int size;
fe1b8b76 712 gdb_byte *data;
c906108c
SS
713 };
714
c906108c
SS
715#ifndef ATTR_ALLOC_CHUNK
716#define ATTR_ALLOC_CHUNK 4
717#endif
718
c906108c
SS
719/* Allocate fields for structs, unions and enums in this size. */
720#ifndef DW_FIELD_ALLOC_CHUNK
721#define DW_FIELD_ALLOC_CHUNK 4
722#endif
723
c906108c
SS
724/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
725 but this would require a corresponding change in unpack_field_as_long
726 and friends. */
727static int bits_per_byte = 8;
728
729/* The routines that read and process dies for a C struct or C++ class
730 pass lists of data member fields and lists of member function fields
731 in an instance of a field_info structure, as defined below. */
732struct field_info
c5aa993b 733 {
0963b4bd 734 /* List of data member and baseclasses fields. */
c5aa993b
JM
735 struct nextfield
736 {
737 struct nextfield *next;
738 int accessibility;
739 int virtuality;
740 struct field field;
741 }
7d0ccb61 742 *fields, *baseclasses;
c906108c 743
7d0ccb61 744 /* Number of fields (including baseclasses). */
c5aa993b 745 int nfields;
c906108c 746
c5aa993b
JM
747 /* Number of baseclasses. */
748 int nbaseclasses;
c906108c 749
c5aa993b
JM
750 /* Set if the accesibility of one of the fields is not public. */
751 int non_public_fields;
c906108c 752
c5aa993b
JM
753 /* Member function fields array, entries are allocated in the order they
754 are encountered in the object file. */
755 struct nextfnfield
756 {
757 struct nextfnfield *next;
758 struct fn_field fnfield;
759 }
760 *fnfields;
c906108c 761
c5aa993b
JM
762 /* Member function fieldlist array, contains name of possibly overloaded
763 member function, number of overloaded member functions and a pointer
764 to the head of the member function field chain. */
765 struct fnfieldlist
766 {
767 char *name;
768 int length;
769 struct nextfnfield *head;
770 }
771 *fnfieldlists;
c906108c 772
c5aa993b
JM
773 /* Number of entries in the fnfieldlists array. */
774 int nfnfields;
98751a41
JK
775
776 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
777 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
778 struct typedef_field_list
779 {
780 struct typedef_field field;
781 struct typedef_field_list *next;
782 }
783 *typedef_field_list;
784 unsigned typedef_field_list_count;
c5aa993b 785 };
c906108c 786
10b3939b
DJ
787/* One item on the queue of compilation units to read in full symbols
788 for. */
789struct dwarf2_queue_item
790{
791 struct dwarf2_per_cu_data *per_cu;
792 struct dwarf2_queue_item *next;
793};
794
795/* The current queue. */
796static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
797
ae038cb0
DJ
798/* Loaded secondary compilation units are kept in memory until they
799 have not been referenced for the processing of this many
800 compilation units. Set this to zero to disable caching. Cache
801 sizes of up to at least twenty will improve startup time for
802 typical inter-CU-reference binaries, at an obvious memory cost. */
803static int dwarf2_max_cache_age = 5;
920d2a44
AC
804static void
805show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
806 struct cmd_list_element *c, const char *value)
807{
3e43a32a
MS
808 fprintf_filtered (file, _("The upper bound on the age of cached "
809 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
810 value);
811}
812
ae038cb0 813
0963b4bd 814/* Various complaints about symbol reading that don't abort the process. */
c906108c 815
4d3c2250
KB
816static void
817dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 818{
4d3c2250 819 complaint (&symfile_complaints,
e2e0b3e5 820 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
821}
822
25e43795
DJ
823static void
824dwarf2_debug_line_missing_file_complaint (void)
825{
826 complaint (&symfile_complaints,
827 _(".debug_line section has line data without a file"));
828}
829
59205f5a
JB
830static void
831dwarf2_debug_line_missing_end_sequence_complaint (void)
832{
833 complaint (&symfile_complaints,
3e43a32a
MS
834 _(".debug_line section has line "
835 "program sequence without an end"));
59205f5a
JB
836}
837
4d3c2250
KB
838static void
839dwarf2_complex_location_expr_complaint (void)
2e276125 840{
e2e0b3e5 841 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
842}
843
4d3c2250
KB
844static void
845dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846 int arg3)
2e276125 847{
4d3c2250 848 complaint (&symfile_complaints,
3e43a32a
MS
849 _("const value length mismatch for '%s', got %d, expected %d"),
850 arg1, arg2, arg3);
4d3c2250
KB
851}
852
853static void
854dwarf2_macros_too_long_complaint (void)
2e276125 855{
4d3c2250 856 complaint (&symfile_complaints,
e2e0b3e5 857 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
858}
859
860static void
861dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 862{
4d3c2250 863 complaint (&symfile_complaints,
3e43a32a
MS
864 _("macro debug info contains a "
865 "malformed macro definition:\n`%s'"),
4d3c2250
KB
866 arg1);
867}
868
869static void
870dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 871{
4d3c2250 872 complaint (&symfile_complaints,
3e43a32a
MS
873 _("invalid attribute class or form for '%s' in '%s'"),
874 arg1, arg2);
4d3c2250 875}
c906108c 876
c906108c
SS
877/* local function prototypes */
878
4efb68b1 879static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 880
aaa75496
JB
881static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
882 struct objfile *);
883
c67a9c90 884static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 885
72bf9492
DJ
886static void scan_partial_symbols (struct partial_die_info *,
887 CORE_ADDR *, CORE_ADDR *,
5734ee8b 888 int, struct dwarf2_cu *);
c906108c 889
72bf9492
DJ
890static void add_partial_symbol (struct partial_die_info *,
891 struct dwarf2_cu *);
63d06c5c 892
72bf9492
DJ
893static void add_partial_namespace (struct partial_die_info *pdi,
894 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 895 int need_pc, struct dwarf2_cu *cu);
63d06c5c 896
5d7cb8df
JK
897static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
898 CORE_ADDR *highpc, int need_pc,
899 struct dwarf2_cu *cu);
900
72bf9492
DJ
901static void add_partial_enumeration (struct partial_die_info *enum_pdi,
902 struct dwarf2_cu *cu);
91c24f0a 903
bc30ff58
JB
904static void add_partial_subprogram (struct partial_die_info *pdi,
905 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 906 int need_pc, struct dwarf2_cu *cu);
bc30ff58 907
fe1b8b76 908static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
909 gdb_byte *buffer, gdb_byte *info_ptr,
910 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 911
a14ed312 912static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 913
a14ed312 914static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 915
e7c27a73 916static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 917
f3dd6933 918static void dwarf2_free_abbrev_table (void *);
c906108c 919
fe1b8b76 920static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 921 struct dwarf2_cu *);
72bf9492 922
57349743 923static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 924 struct dwarf2_cu *);
c906108c 925
93311388
DE
926static struct partial_die_info *load_partial_dies (bfd *,
927 gdb_byte *, gdb_byte *,
928 int, struct dwarf2_cu *);
72bf9492 929
fe1b8b76 930static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
931 struct abbrev_info *abbrev,
932 unsigned int, bfd *,
933 gdb_byte *, gdb_byte *,
934 struct dwarf2_cu *);
c906108c 935
c764a876 936static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 937 struct dwarf2_cu *);
72bf9492
DJ
938
939static void fixup_partial_die (struct partial_die_info *,
940 struct dwarf2_cu *);
941
fe1b8b76
JB
942static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
943 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 944
fe1b8b76
JB
945static gdb_byte *read_attribute_value (struct attribute *, unsigned,
946 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 947
fe1b8b76 948static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 951
fe1b8b76 952static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 953
fe1b8b76 954static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 955
93311388 956static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 957
fe1b8b76 958static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 959 unsigned int *);
c906108c 960
c764a876
DE
961static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
962
963static LONGEST read_checked_initial_length_and_offset
964 (bfd *, gdb_byte *, const struct comp_unit_head *,
965 unsigned int *, unsigned int *);
613e1657 966
fe1b8b76 967static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
968 unsigned int *);
969
970static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 971
fe1b8b76 972static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 973
9b1c24c8 974static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 975
fe1b8b76
JB
976static char *read_indirect_string (bfd *, gdb_byte *,
977 const struct comp_unit_head *,
978 unsigned int *);
4bdf3d34 979
fe1b8b76 980static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 981
fe1b8b76 982static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 983
fe1b8b76 984static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 985
e142c38c 986static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 987
e142c38c
DJ
988static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
989 struct dwarf2_cu *);
c906108c 990
348e048f
DE
991static struct attribute *dwarf2_attr_no_follow (struct die_info *,
992 unsigned int,
993 struct dwarf2_cu *);
994
05cf31d1
JB
995static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
996 struct dwarf2_cu *cu);
997
e142c38c 998static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 999
e142c38c 1000static struct die_info *die_specification (struct die_info *die,
f2f0e013 1001 struct dwarf2_cu **);
63d06c5c 1002
debd256d
JB
1003static void free_line_header (struct line_header *lh);
1004
aaa75496
JB
1005static void add_file_name (struct line_header *, char *, unsigned int,
1006 unsigned int, unsigned int);
1007
debd256d
JB
1008static struct line_header *(dwarf_decode_line_header
1009 (unsigned int offset,
e7c27a73 1010 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1011
72b9f47f 1012static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1013 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1014
72b9f47f 1015static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1016
a14ed312 1017static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1018 struct dwarf2_cu *);
c906108c 1019
34eaf542
TT
1020static struct symbol *new_symbol_full (struct die_info *, struct type *,
1021 struct dwarf2_cu *, struct symbol *);
1022
a14ed312 1023static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1024 struct dwarf2_cu *);
c906108c 1025
98bfdba5
PA
1026static void dwarf2_const_value_attr (struct attribute *attr,
1027 struct type *type,
1028 const char *name,
1029 struct obstack *obstack,
1030 struct dwarf2_cu *cu, long *value,
1031 gdb_byte **bytes,
1032 struct dwarf2_locexpr_baton **baton);
2df3850c 1033
e7c27a73 1034static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1035
b4ba55a1
JB
1036static int need_gnat_info (struct dwarf2_cu *);
1037
3e43a32a
MS
1038static struct type *die_descriptive_type (struct die_info *,
1039 struct dwarf2_cu *);
b4ba55a1
JB
1040
1041static void set_descriptive_type (struct type *, struct die_info *,
1042 struct dwarf2_cu *);
1043
e7c27a73
DJ
1044static struct type *die_containing_type (struct die_info *,
1045 struct dwarf2_cu *);
c906108c 1046
673bfd45
DE
1047static struct type *lookup_die_type (struct die_info *, struct attribute *,
1048 struct dwarf2_cu *);
c906108c 1049
f792889a 1050static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1051
673bfd45
DE
1052static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1053
086ed43d 1054static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1055
6e70227d 1056static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1057 const char *suffix, int physname,
1058 struct dwarf2_cu *cu);
63d06c5c 1059
e7c27a73 1060static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1061
348e048f
DE
1062static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1063
e7c27a73 1064static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1065
e7c27a73 1066static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1067
ff013f42
JK
1068static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1069 struct dwarf2_cu *, struct partial_symtab *);
1070
a14ed312 1071static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1072 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1073 struct partial_symtab *);
c906108c 1074
fae299cd
DC
1075static void get_scope_pc_bounds (struct die_info *,
1076 CORE_ADDR *, CORE_ADDR *,
1077 struct dwarf2_cu *);
1078
801e3a5b
JB
1079static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1080 CORE_ADDR, struct dwarf2_cu *);
1081
a14ed312 1082static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1083 struct dwarf2_cu *);
c906108c 1084
a14ed312 1085static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1086 struct type *, struct dwarf2_cu *);
c906108c 1087
a14ed312 1088static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1089 struct die_info *, struct type *,
e7c27a73 1090 struct dwarf2_cu *);
c906108c 1091
a14ed312 1092static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1093 struct type *,
1094 struct dwarf2_cu *);
c906108c 1095
134d01f1 1096static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1097
e7c27a73 1098static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1099
e7c27a73 1100static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1101
5d7cb8df
JK
1102static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1103
27aa8d6a
SW
1104static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1105
f55ee35c
JK
1106static struct type *read_module_type (struct die_info *die,
1107 struct dwarf2_cu *cu);
1108
38d518c9 1109static const char *namespace_name (struct die_info *die,
e142c38c 1110 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1111
134d01f1 1112static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1113
e7c27a73 1114static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1115
6e70227d 1116static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1117 struct dwarf2_cu *);
1118
93311388 1119static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1120
93311388
DE
1121static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1122 gdb_byte *info_ptr,
d97bc12b
DE
1123 gdb_byte **new_info_ptr,
1124 struct die_info *parent);
1125
93311388
DE
1126static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1127 gdb_byte *info_ptr,
fe1b8b76 1128 gdb_byte **new_info_ptr,
639d11d3
DC
1129 struct die_info *parent);
1130
93311388
DE
1131static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1132 gdb_byte *info_ptr,
fe1b8b76 1133 gdb_byte **new_info_ptr,
639d11d3
DC
1134 struct die_info *parent);
1135
93311388
DE
1136static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1137 struct die_info **, gdb_byte *,
1138 int *);
1139
e7c27a73 1140static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1141
71c25dea
TT
1142static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1143 struct obstack *);
1144
e142c38c 1145static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1146
98bfdba5
PA
1147static const char *dwarf2_full_name (char *name,
1148 struct die_info *die,
1149 struct dwarf2_cu *cu);
1150
e142c38c 1151static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1152 struct dwarf2_cu **);
9219021c 1153
a14ed312 1154static char *dwarf_tag_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_attr_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_form_name (unsigned int);
c906108c 1159
a14ed312 1160static char *dwarf_bool_name (unsigned int);
c906108c 1161
a14ed312 1162static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1163
1164#if 0
a14ed312 1165static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1166#endif
1167
f9aca02d 1168static struct die_info *sibling_die (struct die_info *);
c906108c 1169
d97bc12b
DE
1170static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1171
1172static void dump_die_for_error (struct die_info *);
1173
1174static void dump_die_1 (struct ui_file *, int level, int max_level,
1175 struct die_info *);
c906108c 1176
d97bc12b 1177/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1178
51545339 1179static void store_in_ref_table (struct die_info *,
10b3939b 1180 struct dwarf2_cu *);
c906108c 1181
93311388
DE
1182static int is_ref_attr (struct attribute *);
1183
c764a876 1184static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1185
43bbcdc2 1186static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1187
348e048f
DE
1188static struct die_info *follow_die_ref_or_sig (struct die_info *,
1189 struct attribute *,
1190 struct dwarf2_cu **);
1191
10b3939b
DJ
1192static struct die_info *follow_die_ref (struct die_info *,
1193 struct attribute *,
f2f0e013 1194 struct dwarf2_cu **);
c906108c 1195
348e048f
DE
1196static struct die_info *follow_die_sig (struct die_info *,
1197 struct attribute *,
1198 struct dwarf2_cu **);
1199
1200static void read_signatured_type_at_offset (struct objfile *objfile,
1201 unsigned int offset);
1202
1203static void read_signatured_type (struct objfile *,
1204 struct signatured_type *type_sig);
1205
c906108c
SS
1206/* memory allocation interface */
1207
7b5a2f43 1208static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1209
f3dd6933 1210static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1211
b60c80d6 1212static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1213
e142c38c 1214static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1215
e142c38c
DJ
1216static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1217 struct dwarf2_cu *);
5fb290d7 1218
2e276125 1219static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1220 char *, bfd *, struct dwarf2_cu *);
2e276125 1221
8e19ed76
PS
1222static int attr_form_is_block (struct attribute *);
1223
3690dd37
JB
1224static int attr_form_is_section_offset (struct attribute *);
1225
1226static int attr_form_is_constant (struct attribute *);
1227
8cf6f0b1
TT
1228static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1229 struct dwarf2_loclist_baton *baton,
1230 struct attribute *attr);
1231
93e7bd98
DJ
1232static void dwarf2_symbol_mark_computed (struct attribute *attr,
1233 struct symbol *sym,
1234 struct dwarf2_cu *cu);
4c2df51b 1235
93311388
DE
1236static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1237 struct abbrev_info *abbrev,
1238 struct dwarf2_cu *cu);
4bb7a0a7 1239
72bf9492
DJ
1240static void free_stack_comp_unit (void *);
1241
72bf9492
DJ
1242static hashval_t partial_die_hash (const void *item);
1243
1244static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1245
ae038cb0 1246static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1247 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1248
1249static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1250 (unsigned int offset, struct objfile *objfile);
ae038cb0 1251
9816fde3
JK
1252static void init_one_comp_unit (struct dwarf2_cu *cu,
1253 struct objfile *objfile);
1254
1255static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1256 struct die_info *comp_unit_die);
93311388 1257
ae038cb0
DJ
1258static void free_one_comp_unit (void *);
1259
1260static void free_cached_comp_units (void *);
1261
1262static void age_cached_comp_units (void);
1263
1264static void free_one_cached_comp_unit (void *);
1265
f792889a
DJ
1266static struct type *set_die_type (struct die_info *, struct type *,
1267 struct dwarf2_cu *);
1c379e20 1268
ae038cb0
DJ
1269static void create_all_comp_units (struct objfile *);
1270
1fd400ff
TT
1271static int create_debug_types_hash_table (struct objfile *objfile);
1272
93311388
DE
1273static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1274 struct objfile *);
10b3939b
DJ
1275
1276static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1277
1278static void dwarf2_add_dependence (struct dwarf2_cu *,
1279 struct dwarf2_per_cu_data *);
1280
ae038cb0
DJ
1281static void dwarf2_mark (struct dwarf2_cu *);
1282
1283static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1284
673bfd45
DE
1285static struct type *get_die_type_at_offset (unsigned int,
1286 struct dwarf2_per_cu_data *per_cu);
1287
f792889a 1288static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1289
9291a0cd
TT
1290static void dwarf2_release_queue (void *dummy);
1291
1292static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1293 struct objfile *objfile);
1294
1295static void process_queue (struct objfile *objfile);
1296
1297static void find_file_and_directory (struct die_info *die,
1298 struct dwarf2_cu *cu,
1299 char **name, char **comp_dir);
1300
1301static char *file_full_name (int file, struct line_header *lh,
1302 const char *comp_dir);
1303
1304static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1305 gdb_byte *info_ptr,
1306 gdb_byte *buffer,
1307 unsigned int buffer_size,
1308 bfd *abfd);
1309
1310static void init_cu_die_reader (struct die_reader_specs *reader,
1311 struct dwarf2_cu *cu);
1312
673bfd45 1313static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1314
9291a0cd
TT
1315#if WORDS_BIGENDIAN
1316
1317/* Convert VALUE between big- and little-endian. */
1318static offset_type
1319byte_swap (offset_type value)
1320{
1321 offset_type result;
1322
1323 result = (value & 0xff) << 24;
1324 result |= (value & 0xff00) << 8;
1325 result |= (value & 0xff0000) >> 8;
1326 result |= (value & 0xff000000) >> 24;
1327 return result;
1328}
1329
1330#define MAYBE_SWAP(V) byte_swap (V)
1331
1332#else
1333#define MAYBE_SWAP(V) (V)
1334#endif /* WORDS_BIGENDIAN */
1335
1336/* The suffix for an index file. */
1337#define INDEX_SUFFIX ".gdb-index"
1338
3da10d80
KS
1339static const char *dwarf2_physname (char *name, struct die_info *die,
1340 struct dwarf2_cu *cu);
1341
c906108c 1342/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1343 information and return true if we have enough to do something.
1344 NAMES points to the dwarf2 section names, or is NULL if the standard
1345 ELF names are used. */
c906108c
SS
1346
1347int
251d32d9
TG
1348dwarf2_has_info (struct objfile *objfile,
1349 const struct dwarf2_debug_sections *names)
c906108c 1350{
be391dca
TT
1351 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1352 if (!dwarf2_per_objfile)
1353 {
1354 /* Initialize per-objfile state. */
1355 struct dwarf2_per_objfile *data
1356 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1357
be391dca
TT
1358 memset (data, 0, sizeof (*data));
1359 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1360 dwarf2_per_objfile = data;
6502dd73 1361
251d32d9
TG
1362 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1363 (void *) names);
be391dca
TT
1364 dwarf2_per_objfile->objfile = objfile;
1365 }
1366 return (dwarf2_per_objfile->info.asection != NULL
1367 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1368}
1369
251d32d9
TG
1370/* When loading sections, we look either for uncompressed section or for
1371 compressed section names. */
233a11ab
CS
1372
1373static int
251d32d9
TG
1374section_is_p (const char *section_name,
1375 const struct dwarf2_section_names *names)
233a11ab 1376{
251d32d9
TG
1377 if (names->normal != NULL
1378 && strcmp (section_name, names->normal) == 0)
1379 return 1;
1380 if (names->compressed != NULL
1381 && strcmp (section_name, names->compressed) == 0)
1382 return 1;
1383 return 0;
233a11ab
CS
1384}
1385
c906108c
SS
1386/* This function is mapped across the sections and remembers the
1387 offset and size of each of the debugging sections we are interested
1388 in. */
1389
1390static void
251d32d9 1391dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1392{
251d32d9
TG
1393 const struct dwarf2_debug_sections *names;
1394
1395 if (vnames == NULL)
1396 names = &dwarf2_elf_names;
1397 else
1398 names = (const struct dwarf2_debug_sections *) vnames;
1399
1400 if (section_is_p (sectp->name, &names->info))
c906108c 1401 {
dce234bc
PP
1402 dwarf2_per_objfile->info.asection = sectp;
1403 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1404 }
251d32d9 1405 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1406 {
dce234bc
PP
1407 dwarf2_per_objfile->abbrev.asection = sectp;
1408 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1409 }
251d32d9 1410 else if (section_is_p (sectp->name, &names->line))
c906108c 1411 {
dce234bc
PP
1412 dwarf2_per_objfile->line.asection = sectp;
1413 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1414 }
251d32d9 1415 else if (section_is_p (sectp->name, &names->loc))
c906108c 1416 {
dce234bc
PP
1417 dwarf2_per_objfile->loc.asection = sectp;
1418 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1419 }
251d32d9 1420 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1421 {
dce234bc
PP
1422 dwarf2_per_objfile->macinfo.asection = sectp;
1423 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1424 }
251d32d9 1425 else if (section_is_p (sectp->name, &names->str))
c906108c 1426 {
dce234bc
PP
1427 dwarf2_per_objfile->str.asection = sectp;
1428 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1429 }
251d32d9 1430 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1431 {
dce234bc
PP
1432 dwarf2_per_objfile->frame.asection = sectp;
1433 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1434 }
251d32d9 1435 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1436 {
3799ccc6 1437 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1438
3799ccc6
EZ
1439 if (aflag & SEC_HAS_CONTENTS)
1440 {
dce234bc
PP
1441 dwarf2_per_objfile->eh_frame.asection = sectp;
1442 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1443 }
b6af0555 1444 }
251d32d9 1445 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1446 {
dce234bc
PP
1447 dwarf2_per_objfile->ranges.asection = sectp;
1448 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1449 }
251d32d9 1450 else if (section_is_p (sectp->name, &names->types))
348e048f
DE
1451 {
1452 dwarf2_per_objfile->types.asection = sectp;
1453 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1454 }
251d32d9 1455 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1456 {
1457 dwarf2_per_objfile->gdb_index.asection = sectp;
1458 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1459 }
dce234bc 1460
72dca2f5
FR
1461 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1462 && bfd_section_vma (abfd, sectp) == 0)
1463 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1464}
1465
dce234bc
PP
1466/* Decompress a section that was compressed using zlib. Store the
1467 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1468
1469static void
dce234bc
PP
1470zlib_decompress_section (struct objfile *objfile, asection *sectp,
1471 gdb_byte **outbuf, bfd_size_type *outsize)
1472{
1473 bfd *abfd = objfile->obfd;
1474#ifndef HAVE_ZLIB_H
1475 error (_("Support for zlib-compressed DWARF data (from '%s') "
1476 "is disabled in this copy of GDB"),
1477 bfd_get_filename (abfd));
1478#else
1479 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1480 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1481 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1482 bfd_size_type uncompressed_size;
1483 gdb_byte *uncompressed_buffer;
1484 z_stream strm;
1485 int rc;
1486 int header_size = 12;
1487
1488 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1489 || bfd_bread (compressed_buffer,
1490 compressed_size, abfd) != compressed_size)
dce234bc
PP
1491 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1492 bfd_get_filename (abfd));
1493
1494 /* Read the zlib header. In this case, it should be "ZLIB" followed
1495 by the uncompressed section size, 8 bytes in big-endian order. */
1496 if (compressed_size < header_size
1497 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1498 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1499 bfd_get_filename (abfd));
1500 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1501 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1502 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1503 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1504 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1505 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1506 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1507 uncompressed_size += compressed_buffer[11];
1508
1509 /* It is possible the section consists of several compressed
1510 buffers concatenated together, so we uncompress in a loop. */
1511 strm.zalloc = NULL;
1512 strm.zfree = NULL;
1513 strm.opaque = NULL;
1514 strm.avail_in = compressed_size - header_size;
1515 strm.next_in = (Bytef*) compressed_buffer + header_size;
1516 strm.avail_out = uncompressed_size;
1517 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1518 uncompressed_size);
1519 rc = inflateInit (&strm);
1520 while (strm.avail_in > 0)
1521 {
1522 if (rc != Z_OK)
1523 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1524 bfd_get_filename (abfd), rc);
1525 strm.next_out = ((Bytef*) uncompressed_buffer
1526 + (uncompressed_size - strm.avail_out));
1527 rc = inflate (&strm, Z_FINISH);
1528 if (rc != Z_STREAM_END)
1529 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1530 bfd_get_filename (abfd), rc);
1531 rc = inflateReset (&strm);
1532 }
1533 rc = inflateEnd (&strm);
1534 if (rc != Z_OK
1535 || strm.avail_out != 0)
1536 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1537 bfd_get_filename (abfd), rc);
1538
affddf13 1539 do_cleanups (cleanup);
dce234bc
PP
1540 *outbuf = uncompressed_buffer;
1541 *outsize = uncompressed_size;
1542#endif
233a11ab
CS
1543}
1544
9e0ac564
TT
1545/* A helper function that decides whether a section is empty. */
1546
1547static int
1548dwarf2_section_empty_p (struct dwarf2_section_info *info)
1549{
1550 return info->asection == NULL || info->size == 0;
1551}
1552
dce234bc
PP
1553/* Read the contents of the section SECTP from object file specified by
1554 OBJFILE, store info about the section into INFO.
1555 If the section is compressed, uncompress it before returning. */
c906108c 1556
dce234bc
PP
1557static void
1558dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1559{
dce234bc
PP
1560 bfd *abfd = objfile->obfd;
1561 asection *sectp = info->asection;
1562 gdb_byte *buf, *retbuf;
1563 unsigned char header[4];
c906108c 1564
be391dca
TT
1565 if (info->readin)
1566 return;
dce234bc 1567 info->buffer = NULL;
b315ab21 1568 info->map_addr = NULL;
be391dca 1569 info->readin = 1;
188dd5d6 1570
9e0ac564 1571 if (dwarf2_section_empty_p (info))
dce234bc 1572 return;
c906108c 1573
dce234bc
PP
1574 /* Check if the file has a 4-byte header indicating compression. */
1575 if (info->size > sizeof (header)
1576 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1577 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1578 {
1579 /* Upon decompression, update the buffer and its size. */
1580 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1581 {
1582 zlib_decompress_section (objfile, sectp, &info->buffer,
1583 &info->size);
1584 return;
1585 }
1586 }
4bdf3d34 1587
dce234bc
PP
1588#ifdef HAVE_MMAP
1589 if (pagesize == 0)
1590 pagesize = getpagesize ();
2e276125 1591
dce234bc
PP
1592 /* Only try to mmap sections which are large enough: we don't want to
1593 waste space due to fragmentation. Also, only try mmap for sections
1594 without relocations. */
1595
1596 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1597 {
b315ab21
TG
1598 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1599 MAP_PRIVATE, sectp->filepos,
1600 &info->map_addr, &info->map_len);
dce234bc 1601
b315ab21 1602 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1603 {
be391dca 1604#if HAVE_POSIX_MADVISE
b315ab21 1605 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1606#endif
dce234bc
PP
1607 return;
1608 }
1609 }
1610#endif
1611
1612 /* If we get here, we are a normal, not-compressed section. */
1613 info->buffer = buf
1614 = obstack_alloc (&objfile->objfile_obstack, info->size);
1615
1616 /* When debugging .o files, we may need to apply relocations; see
1617 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1618 We never compress sections in .o files, so we only need to
1619 try this when the section is not compressed. */
ac8035ab 1620 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1621 if (retbuf != NULL)
1622 {
1623 info->buffer = retbuf;
1624 return;
1625 }
1626
1627 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1628 || bfd_bread (buf, info->size, abfd) != info->size)
1629 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1630 bfd_get_filename (abfd));
1631}
1632
9e0ac564
TT
1633/* A helper function that returns the size of a section in a safe way.
1634 If you are positive that the section has been read before using the
1635 size, then it is safe to refer to the dwarf2_section_info object's
1636 "size" field directly. In other cases, you must call this
1637 function, because for compressed sections the size field is not set
1638 correctly until the section has been read. */
1639
1640static bfd_size_type
1641dwarf2_section_size (struct objfile *objfile,
1642 struct dwarf2_section_info *info)
1643{
1644 if (!info->readin)
1645 dwarf2_read_section (objfile, info);
1646 return info->size;
1647}
1648
dce234bc 1649/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1650 SECTION_NAME. */
af34e669 1651
dce234bc 1652void
3017a003
TG
1653dwarf2_get_section_info (struct objfile *objfile,
1654 enum dwarf2_section_enum sect,
dce234bc
PP
1655 asection **sectp, gdb_byte **bufp,
1656 bfd_size_type *sizep)
1657{
1658 struct dwarf2_per_objfile *data
1659 = objfile_data (objfile, dwarf2_objfile_data_key);
1660 struct dwarf2_section_info *info;
a3b2a86b
TT
1661
1662 /* We may see an objfile without any DWARF, in which case we just
1663 return nothing. */
1664 if (data == NULL)
1665 {
1666 *sectp = NULL;
1667 *bufp = NULL;
1668 *sizep = 0;
1669 return;
1670 }
3017a003
TG
1671 switch (sect)
1672 {
1673 case DWARF2_DEBUG_FRAME:
1674 info = &data->frame;
1675 break;
1676 case DWARF2_EH_FRAME:
1677 info = &data->eh_frame;
1678 break;
1679 default:
1680 gdb_assert_not_reached ("unexpected section");
1681 }
dce234bc 1682
9e0ac564 1683 dwarf2_read_section (objfile, info);
dce234bc
PP
1684
1685 *sectp = info->asection;
1686 *bufp = info->buffer;
1687 *sizep = info->size;
1688}
1689
9291a0cd 1690\f
7b9f3c50
DE
1691/* DWARF quick_symbols_functions support. */
1692
1693/* TUs can share .debug_line entries, and there can be a lot more TUs than
1694 unique line tables, so we maintain a separate table of all .debug_line
1695 derived entries to support the sharing.
1696 All the quick functions need is the list of file names. We discard the
1697 line_header when we're done and don't need to record it here. */
1698struct quick_file_names
1699{
1700 /* The offset in .debug_line of the line table. We hash on this. */
1701 unsigned int offset;
1702
1703 /* The number of entries in file_names, real_names. */
1704 unsigned int num_file_names;
1705
1706 /* The file names from the line table, after being run through
1707 file_full_name. */
1708 const char **file_names;
1709
1710 /* The file names from the line table after being run through
1711 gdb_realpath. These are computed lazily. */
1712 const char **real_names;
1713};
1714
1715/* When using the index (and thus not using psymtabs), each CU has an
1716 object of this type. This is used to hold information needed by
1717 the various "quick" methods. */
1718struct dwarf2_per_cu_quick_data
1719{
1720 /* The file table. This can be NULL if there was no file table
1721 or it's currently not read in.
1722 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1723 struct quick_file_names *file_names;
1724
1725 /* The corresponding symbol table. This is NULL if symbols for this
1726 CU have not yet been read. */
1727 struct symtab *symtab;
1728
1729 /* A temporary mark bit used when iterating over all CUs in
1730 expand_symtabs_matching. */
1731 unsigned int mark : 1;
1732
1733 /* True if we've tried to read the file table and found there isn't one.
1734 There will be no point in trying to read it again next time. */
1735 unsigned int no_file_data : 1;
1736};
1737
1738/* Hash function for a quick_file_names. */
1739
1740static hashval_t
1741hash_file_name_entry (const void *e)
1742{
1743 const struct quick_file_names *file_data = e;
1744
1745 return file_data->offset;
1746}
1747
1748/* Equality function for a quick_file_names. */
1749
1750static int
1751eq_file_name_entry (const void *a, const void *b)
1752{
1753 const struct quick_file_names *ea = a;
1754 const struct quick_file_names *eb = b;
1755
1756 return ea->offset == eb->offset;
1757}
1758
1759/* Delete function for a quick_file_names. */
1760
1761static void
1762delete_file_name_entry (void *e)
1763{
1764 struct quick_file_names *file_data = e;
1765 int i;
1766
1767 for (i = 0; i < file_data->num_file_names; ++i)
1768 {
1769 xfree ((void*) file_data->file_names[i]);
1770 if (file_data->real_names)
1771 xfree ((void*) file_data->real_names[i]);
1772 }
1773
1774 /* The space for the struct itself lives on objfile_obstack,
1775 so we don't free it here. */
1776}
1777
1778/* Create a quick_file_names hash table. */
1779
1780static htab_t
1781create_quick_file_names_table (unsigned int nr_initial_entries)
1782{
1783 return htab_create_alloc (nr_initial_entries,
1784 hash_file_name_entry, eq_file_name_entry,
1785 delete_file_name_entry, xcalloc, xfree);
1786}
9291a0cd
TT
1787
1788/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1789 this CU came. */
2fdf6df6 1790
9291a0cd
TT
1791static void
1792dw2_do_instantiate_symtab (struct objfile *objfile,
1793 struct dwarf2_per_cu_data *per_cu)
1794{
1795 struct cleanup *back_to;
1796
1797 back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799 queue_comp_unit (per_cu, objfile);
1800
1801 if (per_cu->from_debug_types)
1802 read_signatured_type_at_offset (objfile, per_cu->offset);
1803 else
1804 load_full_comp_unit (per_cu, objfile);
1805
1806 process_queue (objfile);
1807
1808 /* Age the cache, releasing compilation units that have not
1809 been used recently. */
1810 age_cached_comp_units ();
1811
1812 do_cleanups (back_to);
1813}
1814
1815/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1816 the objfile from which this CU came. Returns the resulting symbol
1817 table. */
2fdf6df6 1818
9291a0cd
TT
1819static struct symtab *
1820dw2_instantiate_symtab (struct objfile *objfile,
1821 struct dwarf2_per_cu_data *per_cu)
1822{
1823 if (!per_cu->v.quick->symtab)
1824 {
1825 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1826 increment_reading_symtab ();
1827 dw2_do_instantiate_symtab (objfile, per_cu);
1828 do_cleanups (back_to);
1829 }
1830 return per_cu->v.quick->symtab;
1831}
1832
1fd400ff 1833/* Return the CU given its index. */
2fdf6df6 1834
1fd400ff
TT
1835static struct dwarf2_per_cu_data *
1836dw2_get_cu (int index)
1837{
1838 if (index >= dwarf2_per_objfile->n_comp_units)
1839 {
1840 index -= dwarf2_per_objfile->n_comp_units;
1841 return dwarf2_per_objfile->type_comp_units[index];
1842 }
1843 return dwarf2_per_objfile->all_comp_units[index];
1844}
1845
9291a0cd
TT
1846/* A helper function that knows how to read a 64-bit value in a way
1847 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1848 otherwise. */
2fdf6df6 1849
9291a0cd
TT
1850static int
1851extract_cu_value (const char *bytes, ULONGEST *result)
1852{
1853 if (sizeof (ULONGEST) < 8)
1854 {
1855 int i;
1856
1857 /* Ignore the upper 4 bytes if they are all zero. */
1858 for (i = 0; i < 4; ++i)
1859 if (bytes[i + 4] != 0)
1860 return 0;
1861
1862 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1863 }
1864 else
1865 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1866 return 1;
1867}
1868
1869/* Read the CU list from the mapped index, and use it to create all
1870 the CU objects for this objfile. Return 0 if something went wrong,
1871 1 if everything went ok. */
2fdf6df6 1872
9291a0cd 1873static int
1fd400ff
TT
1874create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1875 offset_type cu_list_elements)
9291a0cd
TT
1876{
1877 offset_type i;
9291a0cd
TT
1878
1879 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1880 dwarf2_per_objfile->all_comp_units
1881 = obstack_alloc (&objfile->objfile_obstack,
1882 dwarf2_per_objfile->n_comp_units
1883 * sizeof (struct dwarf2_per_cu_data *));
1884
1885 for (i = 0; i < cu_list_elements; i += 2)
1886 {
1887 struct dwarf2_per_cu_data *the_cu;
1888 ULONGEST offset, length;
1889
1890 if (!extract_cu_value (cu_list, &offset)
1891 || !extract_cu_value (cu_list + 8, &length))
1892 return 0;
1893 cu_list += 2 * 8;
1894
1895 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1896 struct dwarf2_per_cu_data);
1897 the_cu->offset = offset;
1898 the_cu->length = length;
1899 the_cu->objfile = objfile;
1900 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1901 struct dwarf2_per_cu_quick_data);
1902 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1903 }
1904
1905 return 1;
1906}
1907
1fd400ff 1908/* Create the signatured type hash table from the index. */
673bfd45 1909
1fd400ff 1910static int
673bfd45
DE
1911create_signatured_type_table_from_index (struct objfile *objfile,
1912 const gdb_byte *bytes,
1913 offset_type elements)
1fd400ff
TT
1914{
1915 offset_type i;
673bfd45 1916 htab_t sig_types_hash;
1fd400ff
TT
1917
1918 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1919 dwarf2_per_objfile->type_comp_units
1920 = obstack_alloc (&objfile->objfile_obstack,
1921 dwarf2_per_objfile->n_type_comp_units
1922 * sizeof (struct dwarf2_per_cu_data *));
1923
673bfd45 1924 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1925
1926 for (i = 0; i < elements; i += 3)
1927 {
1928 struct signatured_type *type_sig;
1929 ULONGEST offset, type_offset, signature;
1930 void **slot;
1931
1932 if (!extract_cu_value (bytes, &offset)
1933 || !extract_cu_value (bytes + 8, &type_offset))
1934 return 0;
1935 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936 bytes += 3 * 8;
1937
1938 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939 struct signatured_type);
1940 type_sig->signature = signature;
1fd400ff
TT
1941 type_sig->type_offset = type_offset;
1942 type_sig->per_cu.from_debug_types = 1;
1943 type_sig->per_cu.offset = offset;
1944 type_sig->per_cu.objfile = objfile;
1945 type_sig->per_cu.v.quick
1946 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947 struct dwarf2_per_cu_quick_data);
1948
673bfd45 1949 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1950 *slot = type_sig;
1951
1952 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1953 }
1954
673bfd45 1955 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1956
1957 return 1;
1958}
1959
9291a0cd
TT
1960/* Read the address map data from the mapped index, and use it to
1961 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1962
9291a0cd
TT
1963static void
1964create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965{
1966 const gdb_byte *iter, *end;
1967 struct obstack temp_obstack;
1968 struct addrmap *mutable_map;
1969 struct cleanup *cleanup;
1970 CORE_ADDR baseaddr;
1971
1972 obstack_init (&temp_obstack);
1973 cleanup = make_cleanup_obstack_free (&temp_obstack);
1974 mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976 iter = index->address_table;
1977 end = iter + index->address_table_size;
1978
1979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981 while (iter < end)
1982 {
1983 ULONGEST hi, lo, cu_index;
1984 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987 iter += 8;
1988 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989 iter += 4;
1990
1991 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1992 dw2_get_cu (cu_index));
9291a0cd
TT
1993 }
1994
1995 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996 &objfile->objfile_obstack);
1997 do_cleanups (cleanup);
1998}
1999
59d7bcaf
JK
2000/* The hash function for strings in the mapped index. This is the same as
2001 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002 implementation. This is necessary because the hash function is tied to the
2003 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2004 SYMBOL_HASH_NEXT.
2005
2006 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2007
9291a0cd 2008static hashval_t
559a7a62 2009mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2010{
2011 const unsigned char *str = (const unsigned char *) p;
2012 hashval_t r = 0;
2013 unsigned char c;
2014
2015 while ((c = *str++) != 0)
559a7a62
JK
2016 {
2017 if (index_version >= 5)
2018 c = tolower (c);
2019 r = r * 67 + c - 113;
2020 }
9291a0cd
TT
2021
2022 return r;
2023}
2024
2025/* Find a slot in the mapped index INDEX for the object named NAME.
2026 If NAME is found, set *VEC_OUT to point to the CU vector in the
2027 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2028
9291a0cd
TT
2029static int
2030find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031 offset_type **vec_out)
2032{
0cf03b49
JK
2033 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034 offset_type hash;
9291a0cd 2035 offset_type slot, step;
559a7a62 2036 int (*cmp) (const char *, const char *);
9291a0cd 2037
0cf03b49
JK
2038 if (current_language->la_language == language_cplus
2039 || current_language->la_language == language_java
2040 || current_language->la_language == language_fortran)
2041 {
2042 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2043 not contain any. */
2044 const char *paren = strchr (name, '(');
2045
2046 if (paren)
2047 {
2048 char *dup;
2049
2050 dup = xmalloc (paren - name + 1);
2051 memcpy (dup, name, paren - name);
2052 dup[paren - name] = 0;
2053
2054 make_cleanup (xfree, dup);
2055 name = dup;
2056 }
2057 }
2058
559a7a62
JK
2059 /* Index version 4 did not support case insensitive searches. But the
2060 indexes for case insensitive languages are built in lowercase, therefore
2061 simulate our NAME being searched is also lowercased. */
2062 hash = mapped_index_string_hash ((index->version == 4
2063 && case_sensitivity == case_sensitive_off
2064 ? 5 : index->version),
2065 name);
2066
3876f04e
DE
2067 slot = hash & (index->symbol_table_slots - 1);
2068 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2069 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2070
2071 for (;;)
2072 {
2073 /* Convert a slot number to an offset into the table. */
2074 offset_type i = 2 * slot;
2075 const char *str;
3876f04e 2076 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2077 {
2078 do_cleanups (back_to);
2079 return 0;
2080 }
9291a0cd 2081
3876f04e 2082 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2083 if (!cmp (name, str))
9291a0cd
TT
2084 {
2085 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2086 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2087 do_cleanups (back_to);
9291a0cd
TT
2088 return 1;
2089 }
2090
3876f04e 2091 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2092 }
2093}
2094
2095/* Read the index file. If everything went ok, initialize the "quick"
2096 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2097
9291a0cd
TT
2098static int
2099dwarf2_read_index (struct objfile *objfile)
2100{
9291a0cd
TT
2101 char *addr;
2102 struct mapped_index *map;
b3b272e1 2103 offset_type *metadata;
ac0b195c
KW
2104 const gdb_byte *cu_list;
2105 const gdb_byte *types_list = NULL;
2106 offset_type version, cu_list_elements;
2107 offset_type types_list_elements = 0;
1fd400ff 2108 int i;
9291a0cd 2109
9e0ac564 2110 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2111 return 0;
82430852
JK
2112
2113 /* Older elfutils strip versions could keep the section in the main
2114 executable while splitting it for the separate debug info file. */
2115 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116 & SEC_HAS_CONTENTS) == 0)
2117 return 0;
2118
9291a0cd
TT
2119 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121 addr = dwarf2_per_objfile->gdb_index.buffer;
2122 /* Version check. */
1fd400ff 2123 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2124 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2125 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2126 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2127 indices. Index version 4 uses a different hash function than index
2128 version 5 and later. */
831adc1f 2129 if (version < 4)
9291a0cd 2130 return 0;
594e8718
JK
2131 /* Indexes with higher version than the one supported by GDB may be no
2132 longer backward compatible. */
559a7a62 2133 if (version > 5)
594e8718 2134 return 0;
9291a0cd
TT
2135
2136 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2137 map->version = version;
b3b272e1 2138 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2139
2140 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2141
2142 i = 0;
2143 cu_list = addr + MAYBE_SWAP (metadata[i]);
2144 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2145 / 8);
1fd400ff
TT
2146 ++i;
2147
987d643c
TT
2148 types_list = addr + MAYBE_SWAP (metadata[i]);
2149 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2150 - MAYBE_SWAP (metadata[i]))
2151 / 8);
2152 ++i;
1fd400ff
TT
2153
2154 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2155 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2156 - MAYBE_SWAP (metadata[i]));
2157 ++i;
2158
3876f04e
DE
2159 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2160 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2161 - MAYBE_SWAP (metadata[i]))
2162 / (2 * sizeof (offset_type)));
1fd400ff 2163 ++i;
9291a0cd 2164
1fd400ff
TT
2165 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2166
2167 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2168 return 0;
2169
987d643c 2170 if (types_list_elements
673bfd45
DE
2171 && !create_signatured_type_table_from_index (objfile, types_list,
2172 types_list_elements))
9291a0cd
TT
2173 return 0;
2174
2175 create_addrmap_from_index (objfile, map);
2176
2177 dwarf2_per_objfile->index_table = map;
2178 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2179 dwarf2_per_objfile->quick_file_names_table =
2180 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2181
2182 return 1;
2183}
2184
2185/* A helper for the "quick" functions which sets the global
2186 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2187
9291a0cd
TT
2188static void
2189dw2_setup (struct objfile *objfile)
2190{
2191 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2192 gdb_assert (dwarf2_per_objfile);
2193}
2194
2195/* A helper for the "quick" functions which attempts to read the line
2196 table for THIS_CU. */
2fdf6df6 2197
7b9f3c50
DE
2198static struct quick_file_names *
2199dw2_get_file_names (struct objfile *objfile,
2200 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2201{
2202 bfd *abfd = objfile->obfd;
7b9f3c50 2203 struct line_header *lh;
9291a0cd
TT
2204 struct attribute *attr;
2205 struct cleanup *cleanups;
2206 struct die_info *comp_unit_die;
36374493 2207 struct dwarf2_section_info* sec;
9291a0cd
TT
2208 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2209 int has_children, i;
2210 struct dwarf2_cu cu;
2211 unsigned int bytes_read, buffer_size;
2212 struct die_reader_specs reader_specs;
2213 char *name, *comp_dir;
7b9f3c50
DE
2214 void **slot;
2215 struct quick_file_names *qfn;
2216 unsigned int line_offset;
9291a0cd 2217
7b9f3c50
DE
2218 if (this_cu->v.quick->file_names != NULL)
2219 return this_cu->v.quick->file_names;
2220 /* If we know there is no line data, no point in looking again. */
2221 if (this_cu->v.quick->no_file_data)
2222 return NULL;
9291a0cd 2223
9816fde3 2224 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2225 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2226
36374493
DE
2227 if (this_cu->from_debug_types)
2228 sec = &dwarf2_per_objfile->types;
2229 else
2230 sec = &dwarf2_per_objfile->info;
2231 dwarf2_read_section (objfile, sec);
2232 buffer_size = sec->size;
2233 buffer = sec->buffer;
9291a0cd
TT
2234 info_ptr = buffer + this_cu->offset;
2235 beg_of_comp_unit = info_ptr;
2236
2237 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2238 buffer, buffer_size,
2239 abfd);
2240
2241 /* Complete the cu_header. */
2242 cu.header.offset = beg_of_comp_unit - buffer;
2243 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2244
2245 this_cu->cu = &cu;
2246 cu.per_cu = this_cu;
2247
2248 dwarf2_read_abbrevs (abfd, &cu);
2249 make_cleanup (dwarf2_free_abbrev_table, &cu);
2250
2251 if (this_cu->from_debug_types)
2252 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2253 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2254 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2255 &has_children);
9291a0cd 2256
7b9f3c50
DE
2257 lh = NULL;
2258 slot = NULL;
2259 line_offset = 0;
9291a0cd
TT
2260 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2261 if (attr)
2262 {
7b9f3c50
DE
2263 struct quick_file_names find_entry;
2264
2265 line_offset = DW_UNSND (attr);
2266
2267 /* We may have already read in this line header (TU line header sharing).
2268 If we have we're done. */
2269 find_entry.offset = line_offset;
2270 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2271 &find_entry, INSERT);
2272 if (*slot != NULL)
2273 {
2274 do_cleanups (cleanups);
2275 this_cu->v.quick->file_names = *slot;
2276 return *slot;
2277 }
2278
9291a0cd
TT
2279 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2280 }
2281 if (lh == NULL)
2282 {
2283 do_cleanups (cleanups);
7b9f3c50
DE
2284 this_cu->v.quick->no_file_data = 1;
2285 return NULL;
9291a0cd
TT
2286 }
2287
7b9f3c50
DE
2288 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2289 qfn->offset = line_offset;
2290 gdb_assert (slot != NULL);
2291 *slot = qfn;
9291a0cd 2292
7b9f3c50 2293 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2294
7b9f3c50
DE
2295 qfn->num_file_names = lh->num_file_names;
2296 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2297 lh->num_file_names * sizeof (char *));
9291a0cd 2298 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2299 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2300 qfn->real_names = NULL;
9291a0cd 2301
7b9f3c50 2302 free_line_header (lh);
9291a0cd 2303 do_cleanups (cleanups);
7b9f3c50
DE
2304
2305 this_cu->v.quick->file_names = qfn;
2306 return qfn;
9291a0cd
TT
2307}
2308
2309/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2310 real path for a given file name from the line table. */
2fdf6df6 2311
9291a0cd 2312static const char *
7b9f3c50
DE
2313dw2_get_real_path (struct objfile *objfile,
2314 struct quick_file_names *qfn, int index)
9291a0cd 2315{
7b9f3c50
DE
2316 if (qfn->real_names == NULL)
2317 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2318 qfn->num_file_names, sizeof (char *));
9291a0cd 2319
7b9f3c50
DE
2320 if (qfn->real_names[index] == NULL)
2321 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2322
7b9f3c50 2323 return qfn->real_names[index];
9291a0cd
TT
2324}
2325
2326static struct symtab *
2327dw2_find_last_source_symtab (struct objfile *objfile)
2328{
2329 int index;
ae2de4f8 2330
9291a0cd
TT
2331 dw2_setup (objfile);
2332 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2333 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2334}
2335
7b9f3c50
DE
2336/* Traversal function for dw2_forget_cached_source_info. */
2337
2338static int
2339dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2340{
7b9f3c50 2341 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2342
7b9f3c50 2343 if (file_data->real_names)
9291a0cd 2344 {
7b9f3c50 2345 int i;
9291a0cd 2346
7b9f3c50 2347 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2348 {
7b9f3c50
DE
2349 xfree ((void*) file_data->real_names[i]);
2350 file_data->real_names[i] = NULL;
9291a0cd
TT
2351 }
2352 }
7b9f3c50
DE
2353
2354 return 1;
2355}
2356
2357static void
2358dw2_forget_cached_source_info (struct objfile *objfile)
2359{
2360 dw2_setup (objfile);
2361
2362 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2363 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2364}
2365
2366static int
2367dw2_lookup_symtab (struct objfile *objfile, const char *name,
2368 const char *full_path, const char *real_path,
2369 struct symtab **result)
2370{
2371 int i;
2372 int check_basename = lbasename (name) == name;
2373 struct dwarf2_per_cu_data *base_cu = NULL;
2374
2375 dw2_setup (objfile);
ae2de4f8 2376
1fd400ff
TT
2377 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2378 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2379 {
2380 int j;
e254ef6a 2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2382 struct quick_file_names *file_data;
9291a0cd 2383
e254ef6a 2384 if (per_cu->v.quick->symtab)
9291a0cd
TT
2385 continue;
2386
7b9f3c50
DE
2387 file_data = dw2_get_file_names (objfile, per_cu);
2388 if (file_data == NULL)
9291a0cd
TT
2389 continue;
2390
7b9f3c50 2391 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2392 {
7b9f3c50 2393 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2394
2395 if (FILENAME_CMP (name, this_name) == 0)
2396 {
e254ef6a 2397 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2398 return 1;
2399 }
2400
2401 if (check_basename && ! base_cu
2402 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2403 base_cu = per_cu;
9291a0cd
TT
2404
2405 if (full_path != NULL)
2406 {
7b9f3c50
DE
2407 const char *this_real_name = dw2_get_real_path (objfile,
2408 file_data, j);
9291a0cd 2409
7b9f3c50
DE
2410 if (this_real_name != NULL
2411 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2412 {
e254ef6a 2413 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2414 return 1;
2415 }
2416 }
2417
2418 if (real_path != NULL)
2419 {
7b9f3c50
DE
2420 const char *this_real_name = dw2_get_real_path (objfile,
2421 file_data, j);
9291a0cd 2422
7b9f3c50
DE
2423 if (this_real_name != NULL
2424 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2425 {
74dd2ca6
DE
2426 *result = dw2_instantiate_symtab (objfile, per_cu);
2427 return 1;
9291a0cd
TT
2428 }
2429 }
2430 }
2431 }
2432
2433 if (base_cu)
2434 {
2435 *result = dw2_instantiate_symtab (objfile, base_cu);
2436 return 1;
2437 }
2438
2439 return 0;
2440}
2441
2442static struct symtab *
2443dw2_lookup_symbol (struct objfile *objfile, int block_index,
2444 const char *name, domain_enum domain)
2445{
774b6a14 2446 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2447 instead. */
2448 return NULL;
2449}
2450
2451/* A helper function that expands all symtabs that hold an object
2452 named NAME. */
2fdf6df6 2453
9291a0cd
TT
2454static void
2455dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2456{
2457 dw2_setup (objfile);
2458
ae2de4f8 2459 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2460 if (dwarf2_per_objfile->index_table)
2461 {
2462 offset_type *vec;
2463
2464 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2465 name, &vec))
2466 {
2467 offset_type i, len = MAYBE_SWAP (*vec);
2468 for (i = 0; i < len; ++i)
2469 {
2470 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2471 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2472
e254ef6a 2473 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2474 }
2475 }
2476 }
2477}
2478
774b6a14
TT
2479static void
2480dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2481 enum block_enum block_kind, const char *name,
774b6a14 2482 domain_enum domain)
9291a0cd 2483{
774b6a14 2484 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2485}
2486
2487static void
2488dw2_print_stats (struct objfile *objfile)
2489{
2490 int i, count;
2491
2492 dw2_setup (objfile);
2493 count = 0;
1fd400ff
TT
2494 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2495 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2496 {
e254ef6a 2497 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2498
e254ef6a 2499 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2500 ++count;
2501 }
2502 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2503}
2504
2505static void
2506dw2_dump (struct objfile *objfile)
2507{
2508 /* Nothing worth printing. */
2509}
2510
2511static void
2512dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2513 struct section_offsets *delta)
2514{
2515 /* There's nothing to relocate here. */
2516}
2517
2518static void
2519dw2_expand_symtabs_for_function (struct objfile *objfile,
2520 const char *func_name)
2521{
2522 dw2_do_expand_symtabs_matching (objfile, func_name);
2523}
2524
2525static void
2526dw2_expand_all_symtabs (struct objfile *objfile)
2527{
2528 int i;
2529
2530 dw2_setup (objfile);
1fd400ff
TT
2531
2532 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2533 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2534 {
e254ef6a 2535 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2536
e254ef6a 2537 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2538 }
2539}
2540
2541static void
2542dw2_expand_symtabs_with_filename (struct objfile *objfile,
2543 const char *filename)
2544{
2545 int i;
2546
2547 dw2_setup (objfile);
d4637a04
DE
2548
2549 /* We don't need to consider type units here.
2550 This is only called for examining code, e.g. expand_line_sal.
2551 There can be an order of magnitude (or more) more type units
2552 than comp units, and we avoid them if we can. */
2553
2554 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2555 {
2556 int j;
e254ef6a 2557 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2558 struct quick_file_names *file_data;
9291a0cd 2559
e254ef6a 2560 if (per_cu->v.quick->symtab)
9291a0cd
TT
2561 continue;
2562
7b9f3c50
DE
2563 file_data = dw2_get_file_names (objfile, per_cu);
2564 if (file_data == NULL)
9291a0cd
TT
2565 continue;
2566
7b9f3c50 2567 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2568 {
7b9f3c50 2569 const char *this_name = file_data->file_names[j];
1ef75ecc 2570 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2571 {
e254ef6a 2572 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2573 break;
2574 }
2575 }
2576 }
2577}
2578
dd786858 2579static const char *
9291a0cd
TT
2580dw2_find_symbol_file (struct objfile *objfile, const char *name)
2581{
e254ef6a 2582 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2583 offset_type *vec;
7b9f3c50 2584 struct quick_file_names *file_data;
9291a0cd
TT
2585
2586 dw2_setup (objfile);
2587
ae2de4f8 2588 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2589 if (!dwarf2_per_objfile->index_table)
2590 return NULL;
2591
2592 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2593 name, &vec))
2594 return NULL;
2595
2596 /* Note that this just looks at the very first one named NAME -- but
2597 actually we are looking for a function. find_main_filename
2598 should be rewritten so that it doesn't require a custom hook. It
2599 could just use the ordinary symbol tables. */
2600 /* vec[0] is the length, which must always be >0. */
e254ef6a 2601 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2602
7b9f3c50
DE
2603 file_data = dw2_get_file_names (objfile, per_cu);
2604 if (file_data == NULL)
9291a0cd
TT
2605 return NULL;
2606
7b9f3c50 2607 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2608}
2609
2610static void
40658b94
PH
2611dw2_map_matching_symbols (const char * name, domain_enum namespace,
2612 struct objfile *objfile, int global,
2613 int (*callback) (struct block *,
2614 struct symbol *, void *),
2edb89d3
JK
2615 void *data, symbol_compare_ftype *match,
2616 symbol_compare_ftype *ordered_compare)
9291a0cd 2617{
40658b94 2618 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2619 current language is Ada for a non-Ada objfile using GNU index. As Ada
2620 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2621}
2622
2623static void
2624dw2_expand_symtabs_matching (struct objfile *objfile,
2625 int (*file_matcher) (const char *, void *),
2626 int (*name_matcher) (const char *, void *),
8903c50d 2627 enum search_domain kind,
9291a0cd
TT
2628 void *data)
2629{
2630 int i;
2631 offset_type iter;
4b5246aa 2632 struct mapped_index *index;
9291a0cd
TT
2633
2634 dw2_setup (objfile);
ae2de4f8
DE
2635
2636 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2637 if (!dwarf2_per_objfile->index_table)
2638 return;
4b5246aa 2639 index = dwarf2_per_objfile->index_table;
9291a0cd 2640
7b08b9eb
JK
2641 if (file_matcher != NULL)
2642 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2643 + dwarf2_per_objfile->n_type_comp_units); ++i)
2644 {
2645 int j;
2646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647 struct quick_file_names *file_data;
2648
2649 per_cu->v.quick->mark = 0;
2650 if (per_cu->v.quick->symtab)
2651 continue;
2652
2653 file_data = dw2_get_file_names (objfile, per_cu);
2654 if (file_data == NULL)
2655 continue;
2656
2657 for (j = 0; j < file_data->num_file_names; ++j)
2658 {
2659 if (file_matcher (file_data->file_names[j], data))
2660 {
2661 per_cu->v.quick->mark = 1;
2662 break;
2663 }
2664 }
2665 }
9291a0cd 2666
3876f04e 2667 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2668 {
2669 offset_type idx = 2 * iter;
2670 const char *name;
2671 offset_type *vec, vec_len, vec_idx;
2672
3876f04e 2673 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2674 continue;
2675
3876f04e 2676 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2677
2678 if (! (*name_matcher) (name, data))
2679 continue;
2680
2681 /* The name was matched, now expand corresponding CUs that were
2682 marked. */
4b5246aa 2683 vec = (offset_type *) (index->constant_pool
3876f04e 2684 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2685 vec_len = MAYBE_SWAP (vec[0]);
2686 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2687 {
e254ef6a 2688 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2689
e254ef6a 2690 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2691 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2692 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2693 }
2694 }
2695}
2696
2697static struct symtab *
2698dw2_find_pc_sect_symtab (struct objfile *objfile,
2699 struct minimal_symbol *msymbol,
2700 CORE_ADDR pc,
2701 struct obj_section *section,
2702 int warn_if_readin)
2703{
2704 struct dwarf2_per_cu_data *data;
2705
2706 dw2_setup (objfile);
2707
2708 if (!objfile->psymtabs_addrmap)
2709 return NULL;
2710
2711 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2712 if (!data)
2713 return NULL;
2714
2715 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2716 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2717 paddress (get_objfile_arch (objfile), pc));
2718
2719 return dw2_instantiate_symtab (objfile, data);
2720}
2721
9291a0cd 2722static void
44b13c5a 2723dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2724 void *data)
2725{
2726 int i;
2727
2728 dw2_setup (objfile);
ae2de4f8 2729
1fd400ff
TT
2730 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2731 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2732 {
2733 int j;
e254ef6a 2734 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2735 struct quick_file_names *file_data;
9291a0cd 2736
e254ef6a 2737 if (per_cu->v.quick->symtab)
9291a0cd
TT
2738 continue;
2739
7b9f3c50
DE
2740 file_data = dw2_get_file_names (objfile, per_cu);
2741 if (file_data == NULL)
9291a0cd
TT
2742 continue;
2743
7b9f3c50 2744 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2745 {
7b9f3c50
DE
2746 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2747 j);
2748 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2749 }
2750 }
2751}
2752
2753static int
2754dw2_has_symbols (struct objfile *objfile)
2755{
2756 return 1;
2757}
2758
2759const struct quick_symbol_functions dwarf2_gdb_index_functions =
2760{
2761 dw2_has_symbols,
2762 dw2_find_last_source_symtab,
2763 dw2_forget_cached_source_info,
2764 dw2_lookup_symtab,
2765 dw2_lookup_symbol,
774b6a14 2766 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2767 dw2_print_stats,
2768 dw2_dump,
2769 dw2_relocate,
2770 dw2_expand_symtabs_for_function,
2771 dw2_expand_all_symtabs,
2772 dw2_expand_symtabs_with_filename,
2773 dw2_find_symbol_file,
40658b94 2774 dw2_map_matching_symbols,
9291a0cd
TT
2775 dw2_expand_symtabs_matching,
2776 dw2_find_pc_sect_symtab,
9291a0cd
TT
2777 dw2_map_symbol_filenames
2778};
2779
2780/* Initialize for reading DWARF for this objfile. Return 0 if this
2781 file will use psymtabs, or 1 if using the GNU index. */
2782
2783int
2784dwarf2_initialize_objfile (struct objfile *objfile)
2785{
2786 /* If we're about to read full symbols, don't bother with the
2787 indices. In this case we also don't care if some other debug
2788 format is making psymtabs, because they are all about to be
2789 expanded anyway. */
2790 if ((objfile->flags & OBJF_READNOW))
2791 {
2792 int i;
2793
2794 dwarf2_per_objfile->using_index = 1;
2795 create_all_comp_units (objfile);
1fd400ff 2796 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2797 dwarf2_per_objfile->quick_file_names_table =
2798 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2799
1fd400ff
TT
2800 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2801 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2802 {
e254ef6a 2803 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2804
e254ef6a
DE
2805 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2806 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2807 }
2808
2809 /* Return 1 so that gdb sees the "quick" functions. However,
2810 these functions will be no-ops because we will have expanded
2811 all symtabs. */
2812 return 1;
2813 }
2814
2815 if (dwarf2_read_index (objfile))
2816 return 1;
2817
9291a0cd
TT
2818 return 0;
2819}
2820
2821\f
2822
dce234bc
PP
2823/* Build a partial symbol table. */
2824
2825void
f29dff0a 2826dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2827{
f29dff0a 2828 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2829 {
2830 init_psymbol_list (objfile, 1024);
2831 }
2832
d146bf1e 2833 dwarf2_build_psymtabs_hard (objfile);
c906108c 2834}
c906108c 2835
45452591
DE
2836/* Return TRUE if OFFSET is within CU_HEADER. */
2837
2838static inline int
2839offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2840{
2841 unsigned int bottom = cu_header->offset;
2842 unsigned int top = (cu_header->offset
2843 + cu_header->length
2844 + cu_header->initial_length_size);
9a619af0 2845
45452591
DE
2846 return (offset >= bottom && offset < top);
2847}
2848
93311388
DE
2849/* Read in the comp unit header information from the debug_info at info_ptr.
2850 NOTE: This leaves members offset, first_die_offset to be filled in
2851 by the caller. */
107d2387 2852
fe1b8b76 2853static gdb_byte *
107d2387 2854read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2855 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2856{
2857 int signed_addr;
891d2f0b 2858 unsigned int bytes_read;
c764a876
DE
2859
2860 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2861 cu_header->initial_length_size = bytes_read;
2862 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2863 info_ptr += bytes_read;
107d2387
AC
2864 cu_header->version = read_2_bytes (abfd, info_ptr);
2865 info_ptr += 2;
613e1657 2866 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2867 &bytes_read);
613e1657 2868 info_ptr += bytes_read;
107d2387
AC
2869 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2870 info_ptr += 1;
2871 signed_addr = bfd_get_sign_extend_vma (abfd);
2872 if (signed_addr < 0)
8e65ff28 2873 internal_error (__FILE__, __LINE__,
e2e0b3e5 2874 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2875 cu_header->signed_addr_p = signed_addr;
c764a876 2876
107d2387
AC
2877 return info_ptr;
2878}
2879
fe1b8b76
JB
2880static gdb_byte *
2881partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2882 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2883 bfd *abfd)
2884{
fe1b8b76 2885 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2886
2887 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2888
2dc7f7b3 2889 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2890 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2891 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2892 bfd_get_filename (abfd));
72bf9492 2893
9e0ac564
TT
2894 if (header->abbrev_offset
2895 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2896 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2897 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2898 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2899 (long) header->abbrev_offset,
93311388 2900 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2901 bfd_get_filename (abfd));
2902
2903 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2904 > buffer + buffer_size)
8a3fe4f8
AC
2905 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2906 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2907 (long) header->length,
93311388 2908 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2909 bfd_get_filename (abfd));
2910
2911 return info_ptr;
2912}
2913
348e048f
DE
2914/* Read in the types comp unit header information from .debug_types entry at
2915 types_ptr. The result is a pointer to one past the end of the header. */
2916
2917static gdb_byte *
2918read_type_comp_unit_head (struct comp_unit_head *cu_header,
2919 ULONGEST *signature,
2920 gdb_byte *types_ptr, bfd *abfd)
2921{
348e048f
DE
2922 gdb_byte *initial_types_ptr = types_ptr;
2923
6e70227d 2924 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2925 &dwarf2_per_objfile->types);
348e048f
DE
2926 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2927
2928 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2929
2930 *signature = read_8_bytes (abfd, types_ptr);
2931 types_ptr += 8;
2932 types_ptr += cu_header->offset_size;
2933 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2934
2935 return types_ptr;
2936}
2937
aaa75496
JB
2938/* Allocate a new partial symtab for file named NAME and mark this new
2939 partial symtab as being an include of PST. */
2940
2941static void
2942dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2943 struct objfile *objfile)
2944{
2945 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2946
2947 subpst->section_offsets = pst->section_offsets;
2948 subpst->textlow = 0;
2949 subpst->texthigh = 0;
2950
2951 subpst->dependencies = (struct partial_symtab **)
2952 obstack_alloc (&objfile->objfile_obstack,
2953 sizeof (struct partial_symtab *));
2954 subpst->dependencies[0] = pst;
2955 subpst->number_of_dependencies = 1;
2956
2957 subpst->globals_offset = 0;
2958 subpst->n_global_syms = 0;
2959 subpst->statics_offset = 0;
2960 subpst->n_static_syms = 0;
2961 subpst->symtab = NULL;
2962 subpst->read_symtab = pst->read_symtab;
2963 subpst->readin = 0;
2964
2965 /* No private part is necessary for include psymtabs. This property
2966 can be used to differentiate between such include psymtabs and
10b3939b 2967 the regular ones. */
58a9656e 2968 subpst->read_symtab_private = NULL;
aaa75496
JB
2969}
2970
2971/* Read the Line Number Program data and extract the list of files
2972 included by the source file represented by PST. Build an include
d85a05f0 2973 partial symtab for each of these included files. */
aaa75496
JB
2974
2975static void
2976dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2977 struct die_info *die,
aaa75496
JB
2978 struct partial_symtab *pst)
2979{
2980 struct objfile *objfile = cu->objfile;
2981 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2982 struct line_header *lh = NULL;
2983 struct attribute *attr;
aaa75496 2984
d85a05f0
DJ
2985 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2986 if (attr)
2987 {
2988 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2989
d85a05f0
DJ
2990 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2991 }
aaa75496
JB
2992 if (lh == NULL)
2993 return; /* No linetable, so no includes. */
2994
c6da4cef
DE
2995 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2996 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2997
2998 free_line_header (lh);
2999}
3000
348e048f
DE
3001static hashval_t
3002hash_type_signature (const void *item)
3003{
3004 const struct signatured_type *type_sig = item;
9a619af0 3005
348e048f
DE
3006 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3007 return type_sig->signature;
3008}
3009
3010static int
3011eq_type_signature (const void *item_lhs, const void *item_rhs)
3012{
3013 const struct signatured_type *lhs = item_lhs;
3014 const struct signatured_type *rhs = item_rhs;
9a619af0 3015
348e048f
DE
3016 return lhs->signature == rhs->signature;
3017}
3018
1fd400ff
TT
3019/* Allocate a hash table for signatured types. */
3020
3021static htab_t
673bfd45 3022allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3023{
3024 return htab_create_alloc_ex (41,
3025 hash_type_signature,
3026 eq_type_signature,
3027 NULL,
3028 &objfile->objfile_obstack,
3029 hashtab_obstack_allocate,
3030 dummy_obstack_deallocate);
3031}
3032
3033/* A helper function to add a signatured type CU to a list. */
3034
3035static int
3036add_signatured_type_cu_to_list (void **slot, void *datum)
3037{
3038 struct signatured_type *sigt = *slot;
3039 struct dwarf2_per_cu_data ***datap = datum;
3040
3041 **datap = &sigt->per_cu;
3042 ++*datap;
3043
3044 return 1;
3045}
3046
348e048f
DE
3047/* Create the hash table of all entries in the .debug_types section.
3048 The result is zero if there is an error (e.g. missing .debug_types section),
3049 otherwise non-zero. */
3050
3051static int
3052create_debug_types_hash_table (struct objfile *objfile)
3053{
be391dca 3054 gdb_byte *info_ptr;
348e048f 3055 htab_t types_htab;
1fd400ff 3056 struct dwarf2_per_cu_data **iter;
348e048f 3057
be391dca
TT
3058 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3059 info_ptr = dwarf2_per_objfile->types.buffer;
3060
348e048f
DE
3061 if (info_ptr == NULL)
3062 {
3063 dwarf2_per_objfile->signatured_types = NULL;
3064 return 0;
3065 }
3066
673bfd45 3067 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3068
3069 if (dwarf2_die_debug)
3070 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3071
3e43a32a
MS
3072 while (info_ptr < dwarf2_per_objfile->types.buffer
3073 + dwarf2_per_objfile->types.size)
348e048f
DE
3074 {
3075 unsigned int offset;
3076 unsigned int offset_size;
3077 unsigned int type_offset;
3078 unsigned int length, initial_length_size;
3079 unsigned short version;
3080 ULONGEST signature;
3081 struct signatured_type *type_sig;
3082 void **slot;
3083 gdb_byte *ptr = info_ptr;
3084
3085 offset = ptr - dwarf2_per_objfile->types.buffer;
3086
3087 /* We need to read the type's signature in order to build the hash
3088 table, but we don't need to read anything else just yet. */
3089
3090 /* Sanity check to ensure entire cu is present. */
3091 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3092 if (ptr + length + initial_length_size
3093 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3094 {
3095 complaint (&symfile_complaints,
3e43a32a
MS
3096 _("debug type entry runs off end "
3097 "of `.debug_types' section, ignored"));
348e048f
DE
3098 break;
3099 }
3100
3101 offset_size = initial_length_size == 4 ? 4 : 8;
3102 ptr += initial_length_size;
3103 version = bfd_get_16 (objfile->obfd, ptr);
3104 ptr += 2;
3105 ptr += offset_size; /* abbrev offset */
3106 ptr += 1; /* address size */
3107 signature = bfd_get_64 (objfile->obfd, ptr);
3108 ptr += 8;
3109 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3110
3111 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3112 memset (type_sig, 0, sizeof (*type_sig));
3113 type_sig->signature = signature;
348e048f 3114 type_sig->type_offset = type_offset;
ca1f3406 3115 type_sig->per_cu.objfile = objfile;
1fd400ff 3116 type_sig->per_cu.from_debug_types = 1;
b3c8eb43 3117 type_sig->per_cu.offset = offset;
348e048f
DE
3118
3119 slot = htab_find_slot (types_htab, type_sig, INSERT);
3120 gdb_assert (slot != NULL);
b3c8eb43
JK
3121 if (*slot != NULL)
3122 {
3123 const struct signatured_type *dup_sig = *slot;
3124
3125 complaint (&symfile_complaints,
3126 _("debug type entry at offset 0x%x is duplicate to the "
3127 "entry at offset 0x%x, signature 0x%s"),
3128 offset, dup_sig->per_cu.offset,
3129 phex (signature, sizeof (signature)));
3130 gdb_assert (signature == dup_sig->signature);
3131 }
348e048f
DE
3132 *slot = type_sig;
3133
3134 if (dwarf2_die_debug)
3135 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3136 offset, phex (signature, sizeof (signature)));
3137
3138 info_ptr = info_ptr + initial_length_size + length;
3139 }
3140
3141 dwarf2_per_objfile->signatured_types = types_htab;
3142
1fd400ff
TT
3143 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3144 dwarf2_per_objfile->type_comp_units
3145 = obstack_alloc (&objfile->objfile_obstack,
3146 dwarf2_per_objfile->n_type_comp_units
3147 * sizeof (struct dwarf2_per_cu_data *));
3148 iter = &dwarf2_per_objfile->type_comp_units[0];
3149 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3150 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3151 == dwarf2_per_objfile->n_type_comp_units);
3152
348e048f
DE
3153 return 1;
3154}
3155
3156/* Lookup a signature based type.
3157 Returns NULL if SIG is not present in the table. */
3158
3159static struct signatured_type *
3160lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3161{
3162 struct signatured_type find_entry, *entry;
3163
3164 if (dwarf2_per_objfile->signatured_types == NULL)
3165 {
3166 complaint (&symfile_complaints,
55f1336d 3167 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3168 return 0;
3169 }
3170
3171 find_entry.signature = sig;
3172 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3173 return entry;
3174}
3175
d85a05f0
DJ
3176/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3177
3178static void
3179init_cu_die_reader (struct die_reader_specs *reader,
3180 struct dwarf2_cu *cu)
3181{
3182 reader->abfd = cu->objfile->obfd;
3183 reader->cu = cu;
3184 if (cu->per_cu->from_debug_types)
be391dca
TT
3185 {
3186 gdb_assert (dwarf2_per_objfile->types.readin);
3187 reader->buffer = dwarf2_per_objfile->types.buffer;
3188 }
d85a05f0 3189 else
be391dca
TT
3190 {
3191 gdb_assert (dwarf2_per_objfile->info.readin);
3192 reader->buffer = dwarf2_per_objfile->info.buffer;
3193 }
d85a05f0
DJ
3194}
3195
3196/* Find the base address of the compilation unit for range lists and
3197 location lists. It will normally be specified by DW_AT_low_pc.
3198 In DWARF-3 draft 4, the base address could be overridden by
3199 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3200 compilation units with discontinuous ranges. */
3201
3202static void
3203dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3204{
3205 struct attribute *attr;
3206
3207 cu->base_known = 0;
3208 cu->base_address = 0;
3209
3210 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3211 if (attr)
3212 {
3213 cu->base_address = DW_ADDR (attr);
3214 cu->base_known = 1;
3215 }
3216 else
3217 {
3218 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3219 if (attr)
3220 {
3221 cu->base_address = DW_ADDR (attr);
3222 cu->base_known = 1;
3223 }
3224 }
3225}
3226
348e048f
DE
3227/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3228 to combine the common parts.
93311388 3229 Process a compilation unit for a psymtab.
348e048f
DE
3230 BUFFER is a pointer to the beginning of the dwarf section buffer,
3231 either .debug_info or debug_types.
93311388
DE
3232 INFO_PTR is a pointer to the start of the CU.
3233 Returns a pointer to the next CU. */
aaa75496 3234
93311388
DE
3235static gdb_byte *
3236process_psymtab_comp_unit (struct objfile *objfile,
3237 struct dwarf2_per_cu_data *this_cu,
3238 gdb_byte *buffer, gdb_byte *info_ptr,
3239 unsigned int buffer_size)
c906108c 3240{
c906108c 3241 bfd *abfd = objfile->obfd;
93311388 3242 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3243 struct die_info *comp_unit_die;
c906108c 3244 struct partial_symtab *pst;
5734ee8b 3245 CORE_ADDR baseaddr;
93311388
DE
3246 struct cleanup *back_to_inner;
3247 struct dwarf2_cu cu;
d85a05f0
DJ
3248 int has_children, has_pc_info;
3249 struct attribute *attr;
d85a05f0
DJ
3250 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3251 struct die_reader_specs reader_specs;
3e2a0cee 3252 const char *filename;
c906108c 3253
9816fde3 3254 init_one_comp_unit (&cu, objfile);
93311388 3255 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3256
93311388
DE
3257 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3258 buffer, buffer_size,
3259 abfd);
10b3939b 3260
93311388
DE
3261 /* Complete the cu_header. */
3262 cu.header.offset = beg_of_comp_unit - buffer;
3263 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3264
93311388 3265 cu.list_in_scope = &file_symbols;
af703f96 3266
328c9494
DJ
3267 /* If this compilation unit was already read in, free the
3268 cached copy in order to read it in again. This is
3269 necessary because we skipped some symbols when we first
3270 read in the compilation unit (see load_partial_dies).
3271 This problem could be avoided, but the benefit is
3272 unclear. */
3273 if (this_cu->cu != NULL)
3274 free_one_cached_comp_unit (this_cu->cu);
3275
3276 /* Note that this is a pointer to our stack frame, being
3277 added to a global data structure. It will be cleaned up
3278 in free_stack_comp_unit when we finish with this
3279 compilation unit. */
3280 this_cu->cu = &cu;
d85a05f0
DJ
3281 cu.per_cu = this_cu;
3282
93311388
DE
3283 /* Read the abbrevs for this compilation unit into a table. */
3284 dwarf2_read_abbrevs (abfd, &cu);
3285 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3286
93311388 3287 /* Read the compilation unit die. */
348e048f
DE
3288 if (this_cu->from_debug_types)
3289 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3290 init_cu_die_reader (&reader_specs, &cu);
3291 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3292 &has_children);
93311388 3293
348e048f
DE
3294 if (this_cu->from_debug_types)
3295 {
b3c8eb43
JK
3296 /* LENGTH has not been set yet for type units. */
3297 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3298 this_cu->length = cu.header.length + cu.header.initial_length_size;
3299 }
d85a05f0 3300 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3301 {
93311388
DE
3302 info_ptr = (beg_of_comp_unit + cu.header.length
3303 + cu.header.initial_length_size);
3304 do_cleanups (back_to_inner);
3305 return info_ptr;
3306 }
72bf9492 3307
9816fde3 3308 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3309
93311388 3310 /* Allocate a new partial symbol table structure. */
d85a05f0 3311 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3312 if (attr == NULL || !DW_STRING (attr))
3313 filename = "";
3314 else
3315 filename = DW_STRING (attr);
93311388 3316 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3317 filename,
93311388
DE
3318 /* TEXTLOW and TEXTHIGH are set below. */
3319 0,
3320 objfile->global_psymbols.next,
3321 objfile->static_psymbols.next);
72bf9492 3322
d85a05f0
DJ
3323 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3324 if (attr != NULL)
3325 pst->dirname = DW_STRING (attr);
72bf9492 3326
e38df1d0 3327 pst->read_symtab_private = this_cu;
72bf9492 3328
93311388 3329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3330
0963b4bd 3331 /* Store the function that reads in the rest of the symbol table. */
93311388 3332 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3333
9291a0cd 3334 this_cu->v.psymtab = pst;
c906108c 3335
d85a05f0
DJ
3336 dwarf2_find_base_address (comp_unit_die, &cu);
3337
93311388
DE
3338 /* Possibly set the default values of LOWPC and HIGHPC from
3339 `DW_AT_ranges'. */
d85a05f0
DJ
3340 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3341 &best_highpc, &cu, pst);
3342 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3343 /* Store the contiguous range if it is not empty; it can be empty for
3344 CUs with no code. */
3345 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3346 best_lowpc + baseaddr,
3347 best_highpc + baseaddr - 1, pst);
93311388
DE
3348
3349 /* Check if comp unit has_children.
3350 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3351 If not, there's no more debug_info for this comp unit. */
d85a05f0 3352 if (has_children)
93311388
DE
3353 {
3354 struct partial_die_info *first_die;
3355 CORE_ADDR lowpc, highpc;
31ffec48 3356
93311388
DE
3357 lowpc = ((CORE_ADDR) -1);
3358 highpc = ((CORE_ADDR) 0);
c906108c 3359
93311388 3360 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3361
93311388 3362 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3363 ! has_pc_info, &cu);
57c22c6c 3364
93311388
DE
3365 /* If we didn't find a lowpc, set it to highpc to avoid
3366 complaints from `maint check'. */
3367 if (lowpc == ((CORE_ADDR) -1))
3368 lowpc = highpc;
10b3939b 3369
93311388
DE
3370 /* If the compilation unit didn't have an explicit address range,
3371 then use the information extracted from its child dies. */
d85a05f0 3372 if (! has_pc_info)
93311388 3373 {
d85a05f0
DJ
3374 best_lowpc = lowpc;
3375 best_highpc = highpc;
93311388
DE
3376 }
3377 }
d85a05f0
DJ
3378 pst->textlow = best_lowpc + baseaddr;
3379 pst->texthigh = best_highpc + baseaddr;
c906108c 3380
93311388
DE
3381 pst->n_global_syms = objfile->global_psymbols.next -
3382 (objfile->global_psymbols.list + pst->globals_offset);
3383 pst->n_static_syms = objfile->static_psymbols.next -
3384 (objfile->static_psymbols.list + pst->statics_offset);
3385 sort_pst_symbols (pst);
c906108c 3386
93311388
DE
3387 info_ptr = (beg_of_comp_unit + cu.header.length
3388 + cu.header.initial_length_size);
ae038cb0 3389
348e048f
DE
3390 if (this_cu->from_debug_types)
3391 {
3392 /* It's not clear we want to do anything with stmt lists here.
3393 Waiting to see what gcc ultimately does. */
3394 }
d85a05f0 3395 else
93311388
DE
3396 {
3397 /* Get the list of files included in the current compilation unit,
3398 and build a psymtab for each of them. */
d85a05f0 3399 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3400 }
ae038cb0 3401
93311388 3402 do_cleanups (back_to_inner);
ae038cb0 3403
93311388
DE
3404 return info_ptr;
3405}
ff013f42 3406
348e048f
DE
3407/* Traversal function for htab_traverse_noresize.
3408 Process one .debug_types comp-unit. */
3409
3410static int
3411process_type_comp_unit (void **slot, void *info)
3412{
3413 struct signatured_type *entry = (struct signatured_type *) *slot;
3414 struct objfile *objfile = (struct objfile *) info;
3415 struct dwarf2_per_cu_data *this_cu;
3416
3417 this_cu = &entry->per_cu;
348e048f 3418
be391dca 3419 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3420 process_psymtab_comp_unit (objfile, this_cu,
3421 dwarf2_per_objfile->types.buffer,
b3c8eb43 3422 dwarf2_per_objfile->types.buffer + this_cu->offset,
348e048f
DE
3423 dwarf2_per_objfile->types.size);
3424
3425 return 1;
3426}
3427
3428/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3429 Build partial symbol tables for the .debug_types comp-units. */
3430
3431static void
3432build_type_psymtabs (struct objfile *objfile)
3433{
3434 if (! create_debug_types_hash_table (objfile))
3435 return;
3436
3437 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3438 process_type_comp_unit, objfile);
3439}
3440
60606b2c
TT
3441/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3442
3443static void
3444psymtabs_addrmap_cleanup (void *o)
3445{
3446 struct objfile *objfile = o;
ec61707d 3447
60606b2c
TT
3448 objfile->psymtabs_addrmap = NULL;
3449}
3450
93311388
DE
3451/* Build the partial symbol table by doing a quick pass through the
3452 .debug_info and .debug_abbrev sections. */
72bf9492 3453
93311388 3454static void
c67a9c90 3455dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3456{
93311388 3457 gdb_byte *info_ptr;
60606b2c
TT
3458 struct cleanup *back_to, *addrmap_cleanup;
3459 struct obstack temp_obstack;
93311388 3460
98bfdba5
PA
3461 dwarf2_per_objfile->reading_partial_symbols = 1;
3462
be391dca 3463 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3464 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3465
93311388
DE
3466 /* Any cached compilation units will be linked by the per-objfile
3467 read_in_chain. Make sure to free them when we're done. */
3468 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3469
348e048f
DE
3470 build_type_psymtabs (objfile);
3471
93311388 3472 create_all_comp_units (objfile);
c906108c 3473
60606b2c
TT
3474 /* Create a temporary address map on a temporary obstack. We later
3475 copy this to the final obstack. */
3476 obstack_init (&temp_obstack);
3477 make_cleanup_obstack_free (&temp_obstack);
3478 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3479 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3480
93311388
DE
3481 /* Since the objects we're extracting from .debug_info vary in
3482 length, only the individual functions to extract them (like
3483 read_comp_unit_head and load_partial_die) can really know whether
3484 the buffer is large enough to hold another complete object.
c906108c 3485
93311388
DE
3486 At the moment, they don't actually check that. If .debug_info
3487 holds just one extra byte after the last compilation unit's dies,
3488 then read_comp_unit_head will happily read off the end of the
3489 buffer. read_partial_die is similarly casual. Those functions
3490 should be fixed.
c906108c 3491
93311388
DE
3492 For this loop condition, simply checking whether there's any data
3493 left at all should be sufficient. */
c906108c 3494
93311388
DE
3495 while (info_ptr < (dwarf2_per_objfile->info.buffer
3496 + dwarf2_per_objfile->info.size))
3497 {
3498 struct dwarf2_per_cu_data *this_cu;
dd373385 3499
3e43a32a
MS
3500 this_cu = dwarf2_find_comp_unit (info_ptr
3501 - dwarf2_per_objfile->info.buffer,
93311388 3502 objfile);
aaa75496 3503
93311388
DE
3504 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3505 dwarf2_per_objfile->info.buffer,
3506 info_ptr,
3507 dwarf2_per_objfile->info.size);
c906108c 3508 }
ff013f42
JK
3509
3510 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3511 &objfile->objfile_obstack);
60606b2c 3512 discard_cleanups (addrmap_cleanup);
ff013f42 3513
ae038cb0
DJ
3514 do_cleanups (back_to);
3515}
3516
93311388 3517/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3518
3519static void
93311388
DE
3520load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3521 struct objfile *objfile)
ae038cb0
DJ
3522{
3523 bfd *abfd = objfile->obfd;
fe1b8b76 3524 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3525 struct die_info *comp_unit_die;
ae038cb0 3526 struct dwarf2_cu *cu;
1d9ec526 3527 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3528 int has_children;
3529 struct die_reader_specs reader_specs;
98bfdba5 3530 int read_cu = 0;
ae038cb0 3531
348e048f
DE
3532 gdb_assert (! this_cu->from_debug_types);
3533
be391dca 3534 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3535 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3536 beg_of_comp_unit = info_ptr;
3537
98bfdba5
PA
3538 if (this_cu->cu == NULL)
3539 {
9816fde3
JK
3540 cu = xmalloc (sizeof (*cu));
3541 init_one_comp_unit (cu, objfile);
ae038cb0 3542
98bfdba5 3543 read_cu = 1;
ae038cb0 3544
98bfdba5
PA
3545 /* If an error occurs while loading, release our storage. */
3546 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3547
98bfdba5
PA
3548 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3549 dwarf2_per_objfile->info.buffer,
3550 dwarf2_per_objfile->info.size,
3551 abfd);
ae038cb0 3552
98bfdba5
PA
3553 /* Complete the cu_header. */
3554 cu->header.offset = this_cu->offset;
3555 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3556
3557 /* Link this compilation unit into the compilation unit tree. */
3558 this_cu->cu = cu;
3559 cu->per_cu = this_cu;
98bfdba5
PA
3560
3561 /* Link this CU into read_in_chain. */
3562 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3563 dwarf2_per_objfile->read_in_chain = this_cu;
3564 }
3565 else
3566 {
3567 cu = this_cu->cu;
3568 info_ptr += cu->header.first_die_offset;
3569 }
ae038cb0
DJ
3570
3571 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3572 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3573 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3574 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3575
3576 /* Read the compilation unit die. */
d85a05f0
DJ
3577 init_cu_die_reader (&reader_specs, cu);
3578 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3579 &has_children);
ae038cb0 3580
9816fde3 3581 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3582
ae038cb0
DJ
3583 /* Check if comp unit has_children.
3584 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3585 If not, there's no more debug_info for this comp unit. */
d85a05f0 3586 if (has_children)
93311388 3587 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3588
98bfdba5
PA
3589 do_cleanups (free_abbrevs_cleanup);
3590
3591 if (read_cu)
3592 {
3593 /* We've successfully allocated this compilation unit. Let our
3594 caller clean it up when finished with it. */
3595 discard_cleanups (free_cu_cleanup);
3596 }
ae038cb0
DJ
3597}
3598
3599/* Create a list of all compilation units in OBJFILE. We do this only
3600 if an inter-comp-unit reference is found; presumably if there is one,
3601 there will be many, and one will occur early in the .debug_info section.
3602 So there's no point in building this list incrementally. */
3603
3604static void
3605create_all_comp_units (struct objfile *objfile)
3606{
3607 int n_allocated;
3608 int n_comp_units;
3609 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3610 gdb_byte *info_ptr;
3611
3612 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3613 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3614
3615 n_comp_units = 0;
3616 n_allocated = 10;
3617 all_comp_units = xmalloc (n_allocated
3618 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3619
3e43a32a
MS
3620 while (info_ptr < dwarf2_per_objfile->info.buffer
3621 + dwarf2_per_objfile->info.size)
ae038cb0 3622 {
c764a876 3623 unsigned int length, initial_length_size;
ae038cb0 3624 struct dwarf2_per_cu_data *this_cu;
c764a876 3625 unsigned int offset;
ae038cb0 3626
dce234bc 3627 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3628
3629 /* Read just enough information to find out where the next
3630 compilation unit is. */
c764a876
DE
3631 length = read_initial_length (objfile->obfd, info_ptr,
3632 &initial_length_size);
ae038cb0
DJ
3633
3634 /* Save the compilation unit for later lookup. */
3635 this_cu = obstack_alloc (&objfile->objfile_obstack,
3636 sizeof (struct dwarf2_per_cu_data));
3637 memset (this_cu, 0, sizeof (*this_cu));
3638 this_cu->offset = offset;
c764a876 3639 this_cu->length = length + initial_length_size;
9291a0cd 3640 this_cu->objfile = objfile;
ae038cb0
DJ
3641
3642 if (n_comp_units == n_allocated)
3643 {
3644 n_allocated *= 2;
3645 all_comp_units = xrealloc (all_comp_units,
3646 n_allocated
3647 * sizeof (struct dwarf2_per_cu_data *));
3648 }
3649 all_comp_units[n_comp_units++] = this_cu;
3650
3651 info_ptr = info_ptr + this_cu->length;
3652 }
3653
3654 dwarf2_per_objfile->all_comp_units
3655 = obstack_alloc (&objfile->objfile_obstack,
3656 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3657 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3658 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3659 xfree (all_comp_units);
3660 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3661}
3662
5734ee8b
DJ
3663/* Process all loaded DIEs for compilation unit CU, starting at
3664 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3665 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3666 DW_AT_ranges). If NEED_PC is set, then this function will set
3667 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3668 and record the covered ranges in the addrmap. */
c906108c 3669
72bf9492
DJ
3670static void
3671scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3672 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3673{
72bf9492 3674 struct partial_die_info *pdi;
c906108c 3675
91c24f0a
DC
3676 /* Now, march along the PDI's, descending into ones which have
3677 interesting children but skipping the children of the other ones,
3678 until we reach the end of the compilation unit. */
c906108c 3679
72bf9492 3680 pdi = first_die;
91c24f0a 3681
72bf9492
DJ
3682 while (pdi != NULL)
3683 {
3684 fixup_partial_die (pdi, cu);
c906108c 3685
f55ee35c 3686 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3687 children, so we need to look at them. Ditto for anonymous
3688 enums. */
933c6fe4 3689
72bf9492 3690 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3691 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3692 {
72bf9492 3693 switch (pdi->tag)
c906108c
SS
3694 {
3695 case DW_TAG_subprogram:
5734ee8b 3696 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3697 break;
72929c62 3698 case DW_TAG_constant:
c906108c
SS
3699 case DW_TAG_variable:
3700 case DW_TAG_typedef:
91c24f0a 3701 case DW_TAG_union_type:
72bf9492 3702 if (!pdi->is_declaration)
63d06c5c 3703 {
72bf9492 3704 add_partial_symbol (pdi, cu);
63d06c5c
DC
3705 }
3706 break;
c906108c 3707 case DW_TAG_class_type:
680b30c7 3708 case DW_TAG_interface_type:
c906108c 3709 case DW_TAG_structure_type:
72bf9492 3710 if (!pdi->is_declaration)
c906108c 3711 {
72bf9492 3712 add_partial_symbol (pdi, cu);
c906108c
SS
3713 }
3714 break;
91c24f0a 3715 case DW_TAG_enumeration_type:
72bf9492
DJ
3716 if (!pdi->is_declaration)
3717 add_partial_enumeration (pdi, cu);
c906108c
SS
3718 break;
3719 case DW_TAG_base_type:
a02abb62 3720 case DW_TAG_subrange_type:
c906108c 3721 /* File scope base type definitions are added to the partial
c5aa993b 3722 symbol table. */
72bf9492 3723 add_partial_symbol (pdi, cu);
c906108c 3724 break;
d9fa45fe 3725 case DW_TAG_namespace:
5734ee8b 3726 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3727 break;
5d7cb8df
JK
3728 case DW_TAG_module:
3729 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3730 break;
c906108c
SS
3731 default:
3732 break;
3733 }
3734 }
3735
72bf9492
DJ
3736 /* If the die has a sibling, skip to the sibling. */
3737
3738 pdi = pdi->die_sibling;
3739 }
3740}
3741
3742/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3743
72bf9492 3744 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3745 name is concatenated with "::" and the partial DIE's name. For
3746 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3747 Enumerators are an exception; they use the scope of their parent
3748 enumeration type, i.e. the name of the enumeration type is not
3749 prepended to the enumerator.
91c24f0a 3750
72bf9492
DJ
3751 There are two complexities. One is DW_AT_specification; in this
3752 case "parent" means the parent of the target of the specification,
3753 instead of the direct parent of the DIE. The other is compilers
3754 which do not emit DW_TAG_namespace; in this case we try to guess
3755 the fully qualified name of structure types from their members'
3756 linkage names. This must be done using the DIE's children rather
3757 than the children of any DW_AT_specification target. We only need
3758 to do this for structures at the top level, i.e. if the target of
3759 any DW_AT_specification (if any; otherwise the DIE itself) does not
3760 have a parent. */
3761
3762/* Compute the scope prefix associated with PDI's parent, in
3763 compilation unit CU. The result will be allocated on CU's
3764 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3765 field. NULL is returned if no prefix is necessary. */
3766static char *
3767partial_die_parent_scope (struct partial_die_info *pdi,
3768 struct dwarf2_cu *cu)
3769{
3770 char *grandparent_scope;
3771 struct partial_die_info *parent, *real_pdi;
91c24f0a 3772
72bf9492
DJ
3773 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3774 then this means the parent of the specification DIE. */
3775
3776 real_pdi = pdi;
72bf9492 3777 while (real_pdi->has_specification)
10b3939b 3778 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3779
3780 parent = real_pdi->die_parent;
3781 if (parent == NULL)
3782 return NULL;
3783
3784 if (parent->scope_set)
3785 return parent->scope;
3786
3787 fixup_partial_die (parent, cu);
3788
10b3939b 3789 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3790
acebe513
UW
3791 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3792 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3793 Work around this problem here. */
3794 if (cu->language == language_cplus
6e70227d 3795 && parent->tag == DW_TAG_namespace
acebe513
UW
3796 && strcmp (parent->name, "::") == 0
3797 && grandparent_scope == NULL)
3798 {
3799 parent->scope = NULL;
3800 parent->scope_set = 1;
3801 return NULL;
3802 }
3803
72bf9492 3804 if (parent->tag == DW_TAG_namespace
f55ee35c 3805 || parent->tag == DW_TAG_module
72bf9492
DJ
3806 || parent->tag == DW_TAG_structure_type
3807 || parent->tag == DW_TAG_class_type
680b30c7 3808 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3809 || parent->tag == DW_TAG_union_type
3810 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3811 {
3812 if (grandparent_scope == NULL)
3813 parent->scope = parent->name;
3814 else
3e43a32a
MS
3815 parent->scope = typename_concat (&cu->comp_unit_obstack,
3816 grandparent_scope,
f55ee35c 3817 parent->name, 0, cu);
72bf9492 3818 }
ceeb3d5a 3819 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3820 /* Enumerators should not get the name of the enumeration as a prefix. */
3821 parent->scope = grandparent_scope;
3822 else
3823 {
3824 /* FIXME drow/2004-04-01: What should we be doing with
3825 function-local names? For partial symbols, we should probably be
3826 ignoring them. */
3827 complaint (&symfile_complaints,
e2e0b3e5 3828 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3829 parent->tag, pdi->offset);
3830 parent->scope = grandparent_scope;
c906108c
SS
3831 }
3832
72bf9492
DJ
3833 parent->scope_set = 1;
3834 return parent->scope;
3835}
3836
3837/* Return the fully scoped name associated with PDI, from compilation unit
3838 CU. The result will be allocated with malloc. */
3839static char *
3840partial_die_full_name (struct partial_die_info *pdi,
3841 struct dwarf2_cu *cu)
3842{
3843 char *parent_scope;
3844
98bfdba5
PA
3845 /* If this is a template instantiation, we can not work out the
3846 template arguments from partial DIEs. So, unfortunately, we have
3847 to go through the full DIEs. At least any work we do building
3848 types here will be reused if full symbols are loaded later. */
3849 if (pdi->has_template_arguments)
3850 {
3851 fixup_partial_die (pdi, cu);
3852
3853 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3854 {
3855 struct die_info *die;
3856 struct attribute attr;
3857 struct dwarf2_cu *ref_cu = cu;
3858
3859 attr.name = 0;
3860 attr.form = DW_FORM_ref_addr;
3861 attr.u.addr = pdi->offset;
3862 die = follow_die_ref (NULL, &attr, &ref_cu);
3863
3864 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3865 }
3866 }
3867
72bf9492
DJ
3868 parent_scope = partial_die_parent_scope (pdi, cu);
3869 if (parent_scope == NULL)
3870 return NULL;
3871 else
f55ee35c 3872 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3873}
3874
3875static void
72bf9492 3876add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3877{
e7c27a73 3878 struct objfile *objfile = cu->objfile;
c906108c 3879 CORE_ADDR addr = 0;
decbce07 3880 char *actual_name = NULL;
5c4e30ca 3881 const struct partial_symbol *psym = NULL;
e142c38c 3882 CORE_ADDR baseaddr;
72bf9492 3883 int built_actual_name = 0;
e142c38c
DJ
3884
3885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3886
94af9270
KS
3887 actual_name = partial_die_full_name (pdi, cu);
3888 if (actual_name)
3889 built_actual_name = 1;
63d06c5c 3890
72bf9492
DJ
3891 if (actual_name == NULL)
3892 actual_name = pdi->name;
3893
c906108c
SS
3894 switch (pdi->tag)
3895 {
3896 case DW_TAG_subprogram:
2cfa0c8d 3897 if (pdi->is_external || cu->language == language_ada)
c906108c 3898 {
2cfa0c8d
JB
3899 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3900 of the global scope. But in Ada, we want to be able to access
3901 nested procedures globally. So all Ada subprograms are stored
3902 in the global scope. */
f47fb265 3903 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3904 mst_text, objfile); */
f47fb265
MS
3905 add_psymbol_to_list (actual_name, strlen (actual_name),
3906 built_actual_name,
3907 VAR_DOMAIN, LOC_BLOCK,
3908 &objfile->global_psymbols,
3909 0, pdi->lowpc + baseaddr,
3910 cu->language, objfile);
c906108c
SS
3911 }
3912 else
3913 {
f47fb265 3914 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3915 mst_file_text, objfile); */
f47fb265
MS
3916 add_psymbol_to_list (actual_name, strlen (actual_name),
3917 built_actual_name,
3918 VAR_DOMAIN, LOC_BLOCK,
3919 &objfile->static_psymbols,
3920 0, pdi->lowpc + baseaddr,
3921 cu->language, objfile);
c906108c
SS
3922 }
3923 break;
72929c62
JB
3924 case DW_TAG_constant:
3925 {
3926 struct psymbol_allocation_list *list;
3927
3928 if (pdi->is_external)
3929 list = &objfile->global_psymbols;
3930 else
3931 list = &objfile->static_psymbols;
f47fb265
MS
3932 add_psymbol_to_list (actual_name, strlen (actual_name),
3933 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3934 list, 0, 0, cu->language, objfile);
72929c62
JB
3935 }
3936 break;
c906108c 3937 case DW_TAG_variable:
caac4577
JG
3938 if (pdi->locdesc)
3939 addr = decode_locdesc (pdi->locdesc, cu);
3940
3941 if (pdi->locdesc
3942 && addr == 0
3943 && !dwarf2_per_objfile->has_section_at_zero)
3944 {
3945 /* A global or static variable may also have been stripped
3946 out by the linker if unused, in which case its address
3947 will be nullified; do not add such variables into partial
3948 symbol table then. */
3949 }
3950 else if (pdi->is_external)
c906108c
SS
3951 {
3952 /* Global Variable.
3953 Don't enter into the minimal symbol tables as there is
3954 a minimal symbol table entry from the ELF symbols already.
3955 Enter into partial symbol table if it has a location
3956 descriptor or a type.
3957 If the location descriptor is missing, new_symbol will create
3958 a LOC_UNRESOLVED symbol, the address of the variable will then
3959 be determined from the minimal symbol table whenever the variable
3960 is referenced.
3961 The address for the partial symbol table entry is not
3962 used by GDB, but it comes in handy for debugging partial symbol
3963 table building. */
3964
c906108c 3965 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3966 add_psymbol_to_list (actual_name, strlen (actual_name),
3967 built_actual_name,
3968 VAR_DOMAIN, LOC_STATIC,
3969 &objfile->global_psymbols,
3970 0, addr + baseaddr,
3971 cu->language, objfile);
c906108c
SS
3972 }
3973 else
3974 {
0963b4bd 3975 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3976 if (pdi->locdesc == NULL)
decbce07
MS
3977 {
3978 if (built_actual_name)
3979 xfree (actual_name);
3980 return;
3981 }
f47fb265 3982 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3983 mst_file_data, objfile); */
f47fb265
MS
3984 add_psymbol_to_list (actual_name, strlen (actual_name),
3985 built_actual_name,
3986 VAR_DOMAIN, LOC_STATIC,
3987 &objfile->static_psymbols,
3988 0, addr + baseaddr,
3989 cu->language, objfile);
c906108c
SS
3990 }
3991 break;
3992 case DW_TAG_typedef:
3993 case DW_TAG_base_type:
a02abb62 3994 case DW_TAG_subrange_type:
38d518c9 3995 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3996 built_actual_name,
176620f1 3997 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3998 &objfile->static_psymbols,
e142c38c 3999 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4000 break;
72bf9492
DJ
4001 case DW_TAG_namespace:
4002 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4003 built_actual_name,
72bf9492
DJ
4004 VAR_DOMAIN, LOC_TYPEDEF,
4005 &objfile->global_psymbols,
4006 0, (CORE_ADDR) 0, cu->language, objfile);
4007 break;
c906108c 4008 case DW_TAG_class_type:
680b30c7 4009 case DW_TAG_interface_type:
c906108c
SS
4010 case DW_TAG_structure_type:
4011 case DW_TAG_union_type:
4012 case DW_TAG_enumeration_type:
fa4028e9
JB
4013 /* Skip external references. The DWARF standard says in the section
4014 about "Structure, Union, and Class Type Entries": "An incomplete
4015 structure, union or class type is represented by a structure,
4016 union or class entry that does not have a byte size attribute
4017 and that has a DW_AT_declaration attribute." */
4018 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4019 {
4020 if (built_actual_name)
4021 xfree (actual_name);
4022 return;
4023 }
fa4028e9 4024
63d06c5c
DC
4025 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4026 static vs. global. */
38d518c9 4027 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4028 built_actual_name,
176620f1 4029 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4030 (cu->language == language_cplus
4031 || cu->language == language_java)
63d06c5c
DC
4032 ? &objfile->global_psymbols
4033 : &objfile->static_psymbols,
e142c38c 4034 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4035
c906108c
SS
4036 break;
4037 case DW_TAG_enumerator:
38d518c9 4038 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4039 built_actual_name,
176620f1 4040 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4041 (cu->language == language_cplus
4042 || cu->language == language_java)
f6fe98ef
DJ
4043 ? &objfile->global_psymbols
4044 : &objfile->static_psymbols,
e142c38c 4045 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4046 break;
4047 default:
4048 break;
4049 }
5c4e30ca 4050
72bf9492
DJ
4051 if (built_actual_name)
4052 xfree (actual_name);
c906108c
SS
4053}
4054
5c4e30ca
DC
4055/* Read a partial die corresponding to a namespace; also, add a symbol
4056 corresponding to that namespace to the symbol table. NAMESPACE is
4057 the name of the enclosing namespace. */
91c24f0a 4058
72bf9492
DJ
4059static void
4060add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4061 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4062 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4063{
72bf9492 4064 /* Add a symbol for the namespace. */
e7c27a73 4065
72bf9492 4066 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4067
4068 /* Now scan partial symbols in that namespace. */
4069
91c24f0a 4070 if (pdi->has_children)
5734ee8b 4071 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4072}
4073
5d7cb8df
JK
4074/* Read a partial die corresponding to a Fortran module. */
4075
4076static void
4077add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4078 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4079{
f55ee35c 4080 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4081
4082 if (pdi->has_children)
4083 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4084}
4085
bc30ff58
JB
4086/* Read a partial die corresponding to a subprogram and create a partial
4087 symbol for that subprogram. When the CU language allows it, this
4088 routine also defines a partial symbol for each nested subprogram
4089 that this subprogram contains.
6e70227d 4090
bc30ff58
JB
4091 DIE my also be a lexical block, in which case we simply search
4092 recursively for suprograms defined inside that lexical block.
4093 Again, this is only performed when the CU language allows this
4094 type of definitions. */
4095
4096static void
4097add_partial_subprogram (struct partial_die_info *pdi,
4098 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4099 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4100{
4101 if (pdi->tag == DW_TAG_subprogram)
4102 {
4103 if (pdi->has_pc_info)
4104 {
4105 if (pdi->lowpc < *lowpc)
4106 *lowpc = pdi->lowpc;
4107 if (pdi->highpc > *highpc)
4108 *highpc = pdi->highpc;
5734ee8b
DJ
4109 if (need_pc)
4110 {
4111 CORE_ADDR baseaddr;
4112 struct objfile *objfile = cu->objfile;
4113
4114 baseaddr = ANOFFSET (objfile->section_offsets,
4115 SECT_OFF_TEXT (objfile));
4116 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4117 pdi->lowpc + baseaddr,
4118 pdi->highpc - 1 + baseaddr,
9291a0cd 4119 cu->per_cu->v.psymtab);
5734ee8b 4120 }
bc30ff58 4121 if (!pdi->is_declaration)
e8d05480
JB
4122 /* Ignore subprogram DIEs that do not have a name, they are
4123 illegal. Do not emit a complaint at this point, we will
4124 do so when we convert this psymtab into a symtab. */
4125 if (pdi->name)
4126 add_partial_symbol (pdi, cu);
bc30ff58
JB
4127 }
4128 }
6e70227d 4129
bc30ff58
JB
4130 if (! pdi->has_children)
4131 return;
4132
4133 if (cu->language == language_ada)
4134 {
4135 pdi = pdi->die_child;
4136 while (pdi != NULL)
4137 {
4138 fixup_partial_die (pdi, cu);
4139 if (pdi->tag == DW_TAG_subprogram
4140 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4141 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4142 pdi = pdi->die_sibling;
4143 }
4144 }
4145}
4146
91c24f0a
DC
4147/* Read a partial die corresponding to an enumeration type. */
4148
72bf9492
DJ
4149static void
4150add_partial_enumeration (struct partial_die_info *enum_pdi,
4151 struct dwarf2_cu *cu)
91c24f0a 4152{
72bf9492 4153 struct partial_die_info *pdi;
91c24f0a
DC
4154
4155 if (enum_pdi->name != NULL)
72bf9492
DJ
4156 add_partial_symbol (enum_pdi, cu);
4157
4158 pdi = enum_pdi->die_child;
4159 while (pdi)
91c24f0a 4160 {
72bf9492 4161 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4162 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4163 else
72bf9492
DJ
4164 add_partial_symbol (pdi, cu);
4165 pdi = pdi->die_sibling;
91c24f0a 4166 }
91c24f0a
DC
4167}
4168
4bb7a0a7
DJ
4169/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4170 Return the corresponding abbrev, or NULL if the number is zero (indicating
4171 an empty DIE). In either case *BYTES_READ will be set to the length of
4172 the initial number. */
4173
4174static struct abbrev_info *
fe1b8b76 4175peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4176 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4177{
4178 bfd *abfd = cu->objfile->obfd;
4179 unsigned int abbrev_number;
4180 struct abbrev_info *abbrev;
4181
4182 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4183
4184 if (abbrev_number == 0)
4185 return NULL;
4186
4187 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4188 if (!abbrev)
4189 {
3e43a32a
MS
4190 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4191 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4192 }
4193
4194 return abbrev;
4195}
4196
93311388
DE
4197/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4198 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4199 DIE. Any children of the skipped DIEs will also be skipped. */
4200
fe1b8b76 4201static gdb_byte *
93311388 4202skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4203{
4204 struct abbrev_info *abbrev;
4205 unsigned int bytes_read;
4206
4207 while (1)
4208 {
4209 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4210 if (abbrev == NULL)
4211 return info_ptr + bytes_read;
4212 else
93311388 4213 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4214 }
4215}
4216
93311388
DE
4217/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4218 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4219 abbrev corresponding to that skipped uleb128 should be passed in
4220 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4221 children. */
4222
fe1b8b76 4223static gdb_byte *
93311388
DE
4224skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4225 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4226{
4227 unsigned int bytes_read;
4228 struct attribute attr;
4229 bfd *abfd = cu->objfile->obfd;
4230 unsigned int form, i;
4231
4232 for (i = 0; i < abbrev->num_attrs; i++)
4233 {
4234 /* The only abbrev we care about is DW_AT_sibling. */
4235 if (abbrev->attrs[i].name == DW_AT_sibling)
4236 {
4237 read_attribute (&attr, &abbrev->attrs[i],
4238 abfd, info_ptr, cu);
4239 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4240 complaint (&symfile_complaints,
4241 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4242 else
93311388 4243 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4244 }
4245
4246 /* If it isn't DW_AT_sibling, skip this attribute. */
4247 form = abbrev->attrs[i].form;
4248 skip_attribute:
4249 switch (form)
4250 {
4bb7a0a7 4251 case DW_FORM_ref_addr:
ae411497
TT
4252 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4253 and later it is offset sized. */
4254 if (cu->header.version == 2)
4255 info_ptr += cu->header.addr_size;
4256 else
4257 info_ptr += cu->header.offset_size;
4258 break;
4259 case DW_FORM_addr:
4bb7a0a7
DJ
4260 info_ptr += cu->header.addr_size;
4261 break;
4262 case DW_FORM_data1:
4263 case DW_FORM_ref1:
4264 case DW_FORM_flag:
4265 info_ptr += 1;
4266 break;
2dc7f7b3
TT
4267 case DW_FORM_flag_present:
4268 break;
4bb7a0a7
DJ
4269 case DW_FORM_data2:
4270 case DW_FORM_ref2:
4271 info_ptr += 2;
4272 break;
4273 case DW_FORM_data4:
4274 case DW_FORM_ref4:
4275 info_ptr += 4;
4276 break;
4277 case DW_FORM_data8:
4278 case DW_FORM_ref8:
55f1336d 4279 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4280 info_ptr += 8;
4281 break;
4282 case DW_FORM_string:
9b1c24c8 4283 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4284 info_ptr += bytes_read;
4285 break;
2dc7f7b3 4286 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4287 case DW_FORM_strp:
4288 info_ptr += cu->header.offset_size;
4289 break;
2dc7f7b3 4290 case DW_FORM_exprloc:
4bb7a0a7
DJ
4291 case DW_FORM_block:
4292 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4293 info_ptr += bytes_read;
4294 break;
4295 case DW_FORM_block1:
4296 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4297 break;
4298 case DW_FORM_block2:
4299 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4300 break;
4301 case DW_FORM_block4:
4302 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4303 break;
4304 case DW_FORM_sdata:
4305 case DW_FORM_udata:
4306 case DW_FORM_ref_udata:
4307 info_ptr = skip_leb128 (abfd, info_ptr);
4308 break;
4309 case DW_FORM_indirect:
4310 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4311 info_ptr += bytes_read;
4312 /* We need to continue parsing from here, so just go back to
4313 the top. */
4314 goto skip_attribute;
4315
4316 default:
3e43a32a
MS
4317 error (_("Dwarf Error: Cannot handle %s "
4318 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4319 dwarf_form_name (form),
4320 bfd_get_filename (abfd));
4321 }
4322 }
4323
4324 if (abbrev->has_children)
93311388 4325 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4326 else
4327 return info_ptr;
4328}
4329
93311388
DE
4330/* Locate ORIG_PDI's sibling.
4331 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4332 in BUFFER. */
91c24f0a 4333
fe1b8b76 4334static gdb_byte *
93311388
DE
4335locate_pdi_sibling (struct partial_die_info *orig_pdi,
4336 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4337 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4338{
4339 /* Do we know the sibling already? */
72bf9492 4340
91c24f0a
DC
4341 if (orig_pdi->sibling)
4342 return orig_pdi->sibling;
4343
4344 /* Are there any children to deal with? */
4345
4346 if (!orig_pdi->has_children)
4347 return info_ptr;
4348
4bb7a0a7 4349 /* Skip the children the long way. */
91c24f0a 4350
93311388 4351 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4352}
4353
c906108c
SS
4354/* Expand this partial symbol table into a full symbol table. */
4355
4356static void
fba45db2 4357dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4358{
c906108c
SS
4359 if (pst != NULL)
4360 {
4361 if (pst->readin)
4362 {
3e43a32a
MS
4363 warning (_("bug: psymtab for %s is already read in."),
4364 pst->filename);
c906108c
SS
4365 }
4366 else
4367 {
4368 if (info_verbose)
4369 {
3e43a32a
MS
4370 printf_filtered (_("Reading in symbols for %s..."),
4371 pst->filename);
c906108c
SS
4372 gdb_flush (gdb_stdout);
4373 }
4374
10b3939b
DJ
4375 /* Restore our global data. */
4376 dwarf2_per_objfile = objfile_data (pst->objfile,
4377 dwarf2_objfile_data_key);
4378
b2ab525c
KB
4379 /* If this psymtab is constructed from a debug-only objfile, the
4380 has_section_at_zero flag will not necessarily be correct. We
4381 can get the correct value for this flag by looking at the data
4382 associated with the (presumably stripped) associated objfile. */
4383 if (pst->objfile->separate_debug_objfile_backlink)
4384 {
4385 struct dwarf2_per_objfile *dpo_backlink
4386 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4387 dwarf2_objfile_data_key);
9a619af0 4388
b2ab525c
KB
4389 dwarf2_per_objfile->has_section_at_zero
4390 = dpo_backlink->has_section_at_zero;
4391 }
4392
98bfdba5
PA
4393 dwarf2_per_objfile->reading_partial_symbols = 0;
4394
c906108c
SS
4395 psymtab_to_symtab_1 (pst);
4396
4397 /* Finish up the debug error message. */
4398 if (info_verbose)
a3f17187 4399 printf_filtered (_("done.\n"));
c906108c
SS
4400 }
4401 }
4402}
4403
10b3939b
DJ
4404/* Add PER_CU to the queue. */
4405
4406static void
03dd20cc 4407queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4408{
4409 struct dwarf2_queue_item *item;
4410
4411 per_cu->queued = 1;
4412 item = xmalloc (sizeof (*item));
4413 item->per_cu = per_cu;
4414 item->next = NULL;
4415
4416 if (dwarf2_queue == NULL)
4417 dwarf2_queue = item;
4418 else
4419 dwarf2_queue_tail->next = item;
4420
4421 dwarf2_queue_tail = item;
4422}
4423
4424/* Process the queue. */
4425
4426static void
4427process_queue (struct objfile *objfile)
4428{
4429 struct dwarf2_queue_item *item, *next_item;
4430
03dd20cc
DJ
4431 /* The queue starts out with one item, but following a DIE reference
4432 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4433 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4434 {
9291a0cd
TT
4435 if (dwarf2_per_objfile->using_index
4436 ? !item->per_cu->v.quick->symtab
4437 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4438 process_full_comp_unit (item->per_cu);
4439
4440 item->per_cu->queued = 0;
4441 next_item = item->next;
4442 xfree (item);
4443 }
4444
4445 dwarf2_queue_tail = NULL;
4446}
4447
4448/* Free all allocated queue entries. This function only releases anything if
4449 an error was thrown; if the queue was processed then it would have been
4450 freed as we went along. */
4451
4452static void
4453dwarf2_release_queue (void *dummy)
4454{
4455 struct dwarf2_queue_item *item, *last;
4456
4457 item = dwarf2_queue;
4458 while (item)
4459 {
4460 /* Anything still marked queued is likely to be in an
4461 inconsistent state, so discard it. */
4462 if (item->per_cu->queued)
4463 {
4464 if (item->per_cu->cu != NULL)
4465 free_one_cached_comp_unit (item->per_cu->cu);
4466 item->per_cu->queued = 0;
4467 }
4468
4469 last = item;
4470 item = item->next;
4471 xfree (last);
4472 }
4473
4474 dwarf2_queue = dwarf2_queue_tail = NULL;
4475}
4476
4477/* Read in full symbols for PST, and anything it depends on. */
4478
c906108c 4479static void
fba45db2 4480psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4481{
10b3939b 4482 struct dwarf2_per_cu_data *per_cu;
c906108c 4483 struct cleanup *back_to;
aaa75496
JB
4484 int i;
4485
4486 for (i = 0; i < pst->number_of_dependencies; i++)
4487 if (!pst->dependencies[i]->readin)
4488 {
4489 /* Inform about additional files that need to be read in. */
4490 if (info_verbose)
4491 {
a3f17187 4492 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4493 fputs_filtered (" ", gdb_stdout);
4494 wrap_here ("");
4495 fputs_filtered ("and ", gdb_stdout);
4496 wrap_here ("");
4497 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4498 wrap_here (""); /* Flush output. */
aaa75496
JB
4499 gdb_flush (gdb_stdout);
4500 }
4501 psymtab_to_symtab_1 (pst->dependencies[i]);
4502 }
4503
e38df1d0 4504 per_cu = pst->read_symtab_private;
10b3939b
DJ
4505
4506 if (per_cu == NULL)
aaa75496
JB
4507 {
4508 /* It's an include file, no symbols to read for it.
4509 Everything is in the parent symtab. */
4510 pst->readin = 1;
4511 return;
4512 }
c906108c 4513
9291a0cd 4514 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4515}
4516
93311388 4517/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4518
93311388 4519static void
3e43a32a
MS
4520load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4521 struct objfile *objfile)
10b3939b 4522{
31ffec48 4523 bfd *abfd = objfile->obfd;
10b3939b 4524 struct dwarf2_cu *cu;
c764a876 4525 unsigned int offset;
93311388 4526 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4527 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4528 struct attribute *attr;
98bfdba5 4529 int read_cu = 0;
6502dd73 4530
348e048f
DE
4531 gdb_assert (! per_cu->from_debug_types);
4532
c906108c 4533 /* Set local variables from the partial symbol table info. */
10b3939b 4534 offset = per_cu->offset;
6502dd73 4535
be391dca 4536 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4537 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4538 beg_of_comp_unit = info_ptr;
63d06c5c 4539
98bfdba5
PA
4540 if (per_cu->cu == NULL)
4541 {
9816fde3
JK
4542 cu = xmalloc (sizeof (*cu));
4543 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4544
4545 read_cu = 1;
c906108c 4546
98bfdba5
PA
4547 /* If an error occurs while loading, release our storage. */
4548 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4549
98bfdba5
PA
4550 /* Read in the comp_unit header. */
4551 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4552
98bfdba5
PA
4553 /* Complete the cu_header. */
4554 cu->header.offset = offset;
4555 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4556
98bfdba5
PA
4557 /* Read the abbrevs for this compilation unit. */
4558 dwarf2_read_abbrevs (abfd, cu);
4559 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4560
98bfdba5
PA
4561 /* Link this compilation unit into the compilation unit tree. */
4562 per_cu->cu = cu;
4563 cu->per_cu = per_cu;
98bfdba5
PA
4564
4565 /* Link this CU into read_in_chain. */
4566 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4567 dwarf2_per_objfile->read_in_chain = per_cu;
4568 }
4569 else
4570 {
4571 cu = per_cu->cu;
4572 info_ptr += cu->header.first_die_offset;
4573 }
e142c38c 4574
93311388 4575 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4576
4577 /* We try not to read any attributes in this function, because not
4578 all objfiles needed for references have been loaded yet, and symbol
4579 table processing isn't initialized. But we have to set the CU language,
4580 or we won't be able to build types correctly. */
9816fde3 4581 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4582
a6c727b2
DJ
4583 /* Similarly, if we do not read the producer, we can not apply
4584 producer-specific interpretation. */
4585 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4586 if (attr)
4587 cu->producer = DW_STRING (attr);
4588
98bfdba5
PA
4589 if (read_cu)
4590 {
4591 do_cleanups (free_abbrevs_cleanup);
e142c38c 4592
98bfdba5
PA
4593 /* We've successfully allocated this compilation unit. Let our
4594 caller clean it up when finished with it. */
4595 discard_cleanups (free_cu_cleanup);
4596 }
10b3939b
DJ
4597}
4598
3da10d80
KS
4599/* Add a DIE to the delayed physname list. */
4600
4601static void
4602add_to_method_list (struct type *type, int fnfield_index, int index,
4603 const char *name, struct die_info *die,
4604 struct dwarf2_cu *cu)
4605{
4606 struct delayed_method_info mi;
4607 mi.type = type;
4608 mi.fnfield_index = fnfield_index;
4609 mi.index = index;
4610 mi.name = name;
4611 mi.die = die;
4612 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4613}
4614
4615/* A cleanup for freeing the delayed method list. */
4616
4617static void
4618free_delayed_list (void *ptr)
4619{
4620 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4621 if (cu->method_list != NULL)
4622 {
4623 VEC_free (delayed_method_info, cu->method_list);
4624 cu->method_list = NULL;
4625 }
4626}
4627
4628/* Compute the physnames of any methods on the CU's method list.
4629
4630 The computation of method physnames is delayed in order to avoid the
4631 (bad) condition that one of the method's formal parameters is of an as yet
4632 incomplete type. */
4633
4634static void
4635compute_delayed_physnames (struct dwarf2_cu *cu)
4636{
4637 int i;
4638 struct delayed_method_info *mi;
4639 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4640 {
1d06ead6 4641 const char *physname;
3da10d80
KS
4642 struct fn_fieldlist *fn_flp
4643 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4644 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4645 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4646 }
4647}
4648
4632c0d0
JK
4649/* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4650 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4651 higher return INT_MAX. */
8be455d7
JK
4652
4653static int
4632c0d0 4654producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
8be455d7
JK
4655{
4656 const char *cs;
4657 int major, minor;
4658
4659 if (cu->producer == NULL)
4660 {
4661 /* For unknown compilers expect their behavior is not compliant. For GCC
4662 this case can also happen for -gdwarf-4 type units supported since
4663 gcc-4.5. */
4664
4632c0d0 4665 return -1;
8be455d7
JK
4666 }
4667
4668 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4669
4670 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4671 {
4672 /* For non-GCC compilers expect their behavior is not compliant. */
4673
4632c0d0 4674 return -1;
8be455d7
JK
4675 }
4676 cs = &cu->producer[strlen ("GNU ")];
4677 while (*cs && !isdigit (*cs))
4678 cs++;
4679 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4680 {
4681 /* Not recognized as GCC. */
4682
4632c0d0 4683 return -1;
8be455d7
JK
4684 }
4685
4632c0d0
JK
4686 if (major < 4)
4687 return -1;
4688 if (major > 4)
4689 return INT_MAX;
4690 return minor;
8be455d7
JK
4691}
4692
10b3939b
DJ
4693/* Generate full symbol information for PST and CU, whose DIEs have
4694 already been loaded into memory. */
4695
4696static void
4697process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4698{
10b3939b 4699 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4700 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4701 CORE_ADDR lowpc, highpc;
4702 struct symtab *symtab;
3da10d80 4703 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4704 CORE_ADDR baseaddr;
4705
4706 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4707
10b3939b
DJ
4708 buildsym_init ();
4709 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4710 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4711
4712 cu->list_in_scope = &file_symbols;
c906108c 4713
d85a05f0 4714 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4715
c906108c 4716 /* Do line number decoding in read_file_scope () */
10b3939b 4717 process_die (cu->dies, cu);
c906108c 4718
3da10d80
KS
4719 /* Now that we have processed all the DIEs in the CU, all the types
4720 should be complete, and it should now be safe to compute all of the
4721 physnames. */
4722 compute_delayed_physnames (cu);
4723 do_cleanups (delayed_list_cleanup);
4724
fae299cd
DC
4725 /* Some compilers don't define a DW_AT_high_pc attribute for the
4726 compilation unit. If the DW_AT_high_pc is missing, synthesize
4727 it, by scanning the DIE's below the compilation unit. */
10b3939b 4728 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4729
613e1657 4730 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4731
8be455d7 4732 if (symtab != NULL)
c906108c 4733 {
4632c0d0
JK
4734 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4735
8be455d7
JK
4736 /* Set symtab language to language from DW_AT_language. If the
4737 compilation is from a C file generated by language preprocessors, do
4738 not set the language if it was already deduced by start_subfile. */
4739 if (!(cu->language == language_c && symtab->language != language_c))
4740 symtab->language = cu->language;
4741
4742 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4743 produce DW_AT_location with location lists but it can be possibly
4744 invalid without -fvar-tracking.
4745
4746 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4747 needed, it would be wrong due to missing DW_AT_producer there.
4748
4749 Still one can confuse GDB by using non-standard GCC compilation
4750 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4751 */
4632c0d0 4752 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4753 symtab->locations_valid = 1;
e0d00bc7
JK
4754
4755 if (gcc_4_minor >= 5)
4756 symtab->epilogue_unwind_valid = 1;
c906108c 4757 }
9291a0cd
TT
4758
4759 if (dwarf2_per_objfile->using_index)
4760 per_cu->v.quick->symtab = symtab;
4761 else
4762 {
4763 struct partial_symtab *pst = per_cu->v.psymtab;
4764 pst->symtab = symtab;
4765 pst->readin = 1;
4766 }
c906108c
SS
4767
4768 do_cleanups (back_to);
4769}
4770
4771/* Process a die and its children. */
4772
4773static void
e7c27a73 4774process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4775{
4776 switch (die->tag)
4777 {
4778 case DW_TAG_padding:
4779 break;
4780 case DW_TAG_compile_unit:
e7c27a73 4781 read_file_scope (die, cu);
c906108c 4782 break;
348e048f
DE
4783 case DW_TAG_type_unit:
4784 read_type_unit_scope (die, cu);
4785 break;
c906108c 4786 case DW_TAG_subprogram:
c906108c 4787 case DW_TAG_inlined_subroutine:
edb3359d 4788 read_func_scope (die, cu);
c906108c
SS
4789 break;
4790 case DW_TAG_lexical_block:
14898363
L
4791 case DW_TAG_try_block:
4792 case DW_TAG_catch_block:
e7c27a73 4793 read_lexical_block_scope (die, cu);
c906108c
SS
4794 break;
4795 case DW_TAG_class_type:
680b30c7 4796 case DW_TAG_interface_type:
c906108c
SS
4797 case DW_TAG_structure_type:
4798 case DW_TAG_union_type:
134d01f1 4799 process_structure_scope (die, cu);
c906108c
SS
4800 break;
4801 case DW_TAG_enumeration_type:
134d01f1 4802 process_enumeration_scope (die, cu);
c906108c 4803 break;
134d01f1 4804
f792889a
DJ
4805 /* These dies have a type, but processing them does not create
4806 a symbol or recurse to process the children. Therefore we can
4807 read them on-demand through read_type_die. */
c906108c 4808 case DW_TAG_subroutine_type:
72019c9c 4809 case DW_TAG_set_type:
c906108c 4810 case DW_TAG_array_type:
c906108c 4811 case DW_TAG_pointer_type:
c906108c 4812 case DW_TAG_ptr_to_member_type:
c906108c 4813 case DW_TAG_reference_type:
c906108c 4814 case DW_TAG_string_type:
c906108c 4815 break;
134d01f1 4816
c906108c 4817 case DW_TAG_base_type:
a02abb62 4818 case DW_TAG_subrange_type:
cb249c71 4819 case DW_TAG_typedef:
134d01f1
DJ
4820 /* Add a typedef symbol for the type definition, if it has a
4821 DW_AT_name. */
f792889a 4822 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4823 break;
c906108c 4824 case DW_TAG_common_block:
e7c27a73 4825 read_common_block (die, cu);
c906108c
SS
4826 break;
4827 case DW_TAG_common_inclusion:
4828 break;
d9fa45fe 4829 case DW_TAG_namespace:
63d06c5c 4830 processing_has_namespace_info = 1;
e7c27a73 4831 read_namespace (die, cu);
d9fa45fe 4832 break;
5d7cb8df 4833 case DW_TAG_module:
f55ee35c 4834 processing_has_namespace_info = 1;
5d7cb8df
JK
4835 read_module (die, cu);
4836 break;
d9fa45fe
DC
4837 case DW_TAG_imported_declaration:
4838 case DW_TAG_imported_module:
63d06c5c 4839 processing_has_namespace_info = 1;
27aa8d6a
SW
4840 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4841 || cu->language != language_fortran))
4842 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4843 dwarf_tag_name (die->tag));
4844 read_import_statement (die, cu);
d9fa45fe 4845 break;
c906108c 4846 default:
e7c27a73 4847 new_symbol (die, NULL, cu);
c906108c
SS
4848 break;
4849 }
4850}
4851
94af9270
KS
4852/* A helper function for dwarf2_compute_name which determines whether DIE
4853 needs to have the name of the scope prepended to the name listed in the
4854 die. */
4855
4856static int
4857die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4858{
1c809c68
TT
4859 struct attribute *attr;
4860
94af9270
KS
4861 switch (die->tag)
4862 {
4863 case DW_TAG_namespace:
4864 case DW_TAG_typedef:
4865 case DW_TAG_class_type:
4866 case DW_TAG_interface_type:
4867 case DW_TAG_structure_type:
4868 case DW_TAG_union_type:
4869 case DW_TAG_enumeration_type:
4870 case DW_TAG_enumerator:
4871 case DW_TAG_subprogram:
4872 case DW_TAG_member:
4873 return 1;
4874
4875 case DW_TAG_variable:
c2b0a229 4876 case DW_TAG_constant:
94af9270
KS
4877 /* We only need to prefix "globally" visible variables. These include
4878 any variable marked with DW_AT_external or any variable that
4879 lives in a namespace. [Variables in anonymous namespaces
4880 require prefixing, but they are not DW_AT_external.] */
4881
4882 if (dwarf2_attr (die, DW_AT_specification, cu))
4883 {
4884 struct dwarf2_cu *spec_cu = cu;
9a619af0 4885
94af9270
KS
4886 return die_needs_namespace (die_specification (die, &spec_cu),
4887 spec_cu);
4888 }
4889
1c809c68 4890 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4891 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4892 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4893 return 0;
4894 /* A variable in a lexical block of some kind does not need a
4895 namespace, even though in C++ such variables may be external
4896 and have a mangled name. */
4897 if (die->parent->tag == DW_TAG_lexical_block
4898 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4899 || die->parent->tag == DW_TAG_catch_block
4900 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4901 return 0;
4902 return 1;
94af9270
KS
4903
4904 default:
4905 return 0;
4906 }
4907}
4908
98bfdba5
PA
4909/* Retrieve the last character from a mem_file. */
4910
4911static void
4912do_ui_file_peek_last (void *object, const char *buffer, long length)
4913{
4914 char *last_char_p = (char *) object;
4915
4916 if (length > 0)
4917 *last_char_p = buffer[length - 1];
4918}
4919
94af9270
KS
4920/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4921 compute the physname for the object, which include a method's
4922 formal parameters (C++/Java) and return type (Java).
4923
af6b7be1
JB
4924 For Ada, return the DIE's linkage name rather than the fully qualified
4925 name. PHYSNAME is ignored..
4926
94af9270
KS
4927 The result is allocated on the objfile_obstack and canonicalized. */
4928
4929static const char *
4930dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4931 int physname)
4932{
4933 if (name == NULL)
4934 name = dwarf2_name (die, cu);
4935
f55ee35c
JK
4936 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4937 compute it by typename_concat inside GDB. */
4938 if (cu->language == language_ada
4939 || (cu->language == language_fortran && physname))
4940 {
4941 /* For Ada unit, we prefer the linkage name over the name, as
4942 the former contains the exported name, which the user expects
4943 to be able to reference. Ideally, we want the user to be able
4944 to reference this entity using either natural or linkage name,
4945 but we haven't started looking at this enhancement yet. */
4946 struct attribute *attr;
4947
4948 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4949 if (attr == NULL)
4950 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4951 if (attr && DW_STRING (attr))
4952 return DW_STRING (attr);
4953 }
4954
94af9270
KS
4955 /* These are the only languages we know how to qualify names in. */
4956 if (name != NULL
f55ee35c
JK
4957 && (cu->language == language_cplus || cu->language == language_java
4958 || cu->language == language_fortran))
94af9270
KS
4959 {
4960 if (die_needs_namespace (die, cu))
4961 {
4962 long length;
4963 char *prefix;
4964 struct ui_file *buf;
4965
4966 prefix = determine_prefix (die, cu);
4967 buf = mem_fileopen ();
4968 if (*prefix != '\0')
4969 {
f55ee35c
JK
4970 char *prefixed_name = typename_concat (NULL, prefix, name,
4971 physname, cu);
9a619af0 4972
94af9270
KS
4973 fputs_unfiltered (prefixed_name, buf);
4974 xfree (prefixed_name);
4975 }
4976 else
62d5b8da 4977 fputs_unfiltered (name, buf);
94af9270 4978
98bfdba5
PA
4979 /* Template parameters may be specified in the DIE's DW_AT_name, or
4980 as children with DW_TAG_template_type_param or
4981 DW_TAG_value_type_param. If the latter, add them to the name
4982 here. If the name already has template parameters, then
4983 skip this step; some versions of GCC emit both, and
4984 it is more efficient to use the pre-computed name.
4985
4986 Something to keep in mind about this process: it is very
4987 unlikely, or in some cases downright impossible, to produce
4988 something that will match the mangled name of a function.
4989 If the definition of the function has the same debug info,
4990 we should be able to match up with it anyway. But fallbacks
4991 using the minimal symbol, for instance to find a method
4992 implemented in a stripped copy of libstdc++, will not work.
4993 If we do not have debug info for the definition, we will have to
4994 match them up some other way.
4995
4996 When we do name matching there is a related problem with function
4997 templates; two instantiated function templates are allowed to
4998 differ only by their return types, which we do not add here. */
4999
5000 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5001 {
5002 struct attribute *attr;
5003 struct die_info *child;
5004 int first = 1;
5005
5006 die->building_fullname = 1;
5007
5008 for (child = die->child; child != NULL; child = child->sibling)
5009 {
5010 struct type *type;
5011 long value;
5012 gdb_byte *bytes;
5013 struct dwarf2_locexpr_baton *baton;
5014 struct value *v;
5015
5016 if (child->tag != DW_TAG_template_type_param
5017 && child->tag != DW_TAG_template_value_param)
5018 continue;
5019
5020 if (first)
5021 {
5022 fputs_unfiltered ("<", buf);
5023 first = 0;
5024 }
5025 else
5026 fputs_unfiltered (", ", buf);
5027
5028 attr = dwarf2_attr (child, DW_AT_type, cu);
5029 if (attr == NULL)
5030 {
5031 complaint (&symfile_complaints,
5032 _("template parameter missing DW_AT_type"));
5033 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5034 continue;
5035 }
5036 type = die_type (child, cu);
5037
5038 if (child->tag == DW_TAG_template_type_param)
5039 {
5040 c_print_type (type, "", buf, -1, 0);
5041 continue;
5042 }
5043
5044 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5045 if (attr == NULL)
5046 {
5047 complaint (&symfile_complaints,
3e43a32a
MS
5048 _("template parameter missing "
5049 "DW_AT_const_value"));
98bfdba5
PA
5050 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5051 continue;
5052 }
5053
5054 dwarf2_const_value_attr (attr, type, name,
5055 &cu->comp_unit_obstack, cu,
5056 &value, &bytes, &baton);
5057
5058 if (TYPE_NOSIGN (type))
5059 /* GDB prints characters as NUMBER 'CHAR'. If that's
5060 changed, this can use value_print instead. */
5061 c_printchar (value, type, buf);
5062 else
5063 {
5064 struct value_print_options opts;
5065
5066 if (baton != NULL)
5067 v = dwarf2_evaluate_loc_desc (type, NULL,
5068 baton->data,
5069 baton->size,
5070 baton->per_cu);
5071 else if (bytes != NULL)
5072 {
5073 v = allocate_value (type);
5074 memcpy (value_contents_writeable (v), bytes,
5075 TYPE_LENGTH (type));
5076 }
5077 else
5078 v = value_from_longest (type, value);
5079
3e43a32a
MS
5080 /* Specify decimal so that we do not depend on
5081 the radix. */
98bfdba5
PA
5082 get_formatted_print_options (&opts, 'd');
5083 opts.raw = 1;
5084 value_print (v, buf, &opts);
5085 release_value (v);
5086 value_free (v);
5087 }
5088 }
5089
5090 die->building_fullname = 0;
5091
5092 if (!first)
5093 {
5094 /* Close the argument list, with a space if necessary
5095 (nested templates). */
5096 char last_char = '\0';
5097 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5098 if (last_char == '>')
5099 fputs_unfiltered (" >", buf);
5100 else
5101 fputs_unfiltered (">", buf);
5102 }
5103 }
5104
94af9270
KS
5105 /* For Java and C++ methods, append formal parameter type
5106 information, if PHYSNAME. */
6e70227d 5107
94af9270
KS
5108 if (physname && die->tag == DW_TAG_subprogram
5109 && (cu->language == language_cplus
5110 || cu->language == language_java))
5111 {
5112 struct type *type = read_type_die (die, cu);
5113
3167638f 5114 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5115
5116 if (cu->language == language_java)
5117 {
5118 /* For java, we must append the return type to method
0963b4bd 5119 names. */
94af9270
KS
5120 if (die->tag == DW_TAG_subprogram)
5121 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5122 0, 0);
5123 }
5124 else if (cu->language == language_cplus)
5125 {
60430eff
DJ
5126 /* Assume that an artificial first parameter is
5127 "this", but do not crash if it is not. RealView
5128 marks unnamed (and thus unused) parameters as
5129 artificial; there is no way to differentiate
5130 the two cases. */
94af9270
KS
5131 if (TYPE_NFIELDS (type) > 0
5132 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5133 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5134 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5135 0))))
94af9270
KS
5136 fputs_unfiltered (" const", buf);
5137 }
5138 }
5139
5140 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5141 &length);
5142 ui_file_delete (buf);
5143
5144 if (cu->language == language_cplus)
5145 {
5146 char *cname
5147 = dwarf2_canonicalize_name (name, cu,
5148 &cu->objfile->objfile_obstack);
9a619af0 5149
94af9270
KS
5150 if (cname != NULL)
5151 name = cname;
5152 }
5153 }
5154 }
5155
5156 return name;
5157}
5158
0114d602
DJ
5159/* Return the fully qualified name of DIE, based on its DW_AT_name.
5160 If scope qualifiers are appropriate they will be added. The result
5161 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5162 not have a name. NAME may either be from a previous call to
5163 dwarf2_name or NULL.
5164
0963b4bd 5165 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5166
5167static const char *
94af9270 5168dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5169{
94af9270
KS
5170 return dwarf2_compute_name (name, die, cu, 0);
5171}
0114d602 5172
94af9270
KS
5173/* Construct a physname for the given DIE in CU. NAME may either be
5174 from a previous call to dwarf2_name or NULL. The result will be
5175 allocated on the objfile_objstack or NULL if the DIE does not have a
5176 name.
0114d602 5177
94af9270 5178 The output string will be canonicalized (if C++/Java). */
0114d602 5179
94af9270
KS
5180static const char *
5181dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5182{
5183 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5184}
5185
27aa8d6a
SW
5186/* Read the import statement specified by the given die and record it. */
5187
5188static void
5189read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5190{
5191 struct attribute *import_attr;
32019081 5192 struct die_info *imported_die, *child_die;
de4affc9 5193 struct dwarf2_cu *imported_cu;
27aa8d6a 5194 const char *imported_name;
794684b6 5195 const char *imported_name_prefix;
13387711
SW
5196 const char *canonical_name;
5197 const char *import_alias;
5198 const char *imported_declaration = NULL;
794684b6 5199 const char *import_prefix;
32019081
JK
5200 VEC (const_char_ptr) *excludes = NULL;
5201 struct cleanup *cleanups;
13387711
SW
5202
5203 char *temp;
27aa8d6a
SW
5204
5205 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5206 if (import_attr == NULL)
5207 {
5208 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5209 dwarf_tag_name (die->tag));
5210 return;
5211 }
5212
de4affc9
CC
5213 imported_cu = cu;
5214 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5215 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5216 if (imported_name == NULL)
5217 {
5218 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5219
5220 The import in the following code:
5221 namespace A
5222 {
5223 typedef int B;
5224 }
5225
5226 int main ()
5227 {
5228 using A::B;
5229 B b;
5230 return b;
5231 }
5232
5233 ...
5234 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5235 <52> DW_AT_decl_file : 1
5236 <53> DW_AT_decl_line : 6
5237 <54> DW_AT_import : <0x75>
5238 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5239 <59> DW_AT_name : B
5240 <5b> DW_AT_decl_file : 1
5241 <5c> DW_AT_decl_line : 2
5242 <5d> DW_AT_type : <0x6e>
5243 ...
5244 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5245 <76> DW_AT_byte_size : 4
5246 <77> DW_AT_encoding : 5 (signed)
5247
5248 imports the wrong die ( 0x75 instead of 0x58 ).
5249 This case will be ignored until the gcc bug is fixed. */
5250 return;
5251 }
5252
82856980
SW
5253 /* Figure out the local name after import. */
5254 import_alias = dwarf2_name (die, cu);
27aa8d6a 5255
794684b6
SW
5256 /* Figure out where the statement is being imported to. */
5257 import_prefix = determine_prefix (die, cu);
5258
5259 /* Figure out what the scope of the imported die is and prepend it
5260 to the name of the imported die. */
de4affc9 5261 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5262
f55ee35c
JK
5263 if (imported_die->tag != DW_TAG_namespace
5264 && imported_die->tag != DW_TAG_module)
794684b6 5265 {
13387711
SW
5266 imported_declaration = imported_name;
5267 canonical_name = imported_name_prefix;
794684b6 5268 }
13387711 5269 else if (strlen (imported_name_prefix) > 0)
794684b6 5270 {
13387711
SW
5271 temp = alloca (strlen (imported_name_prefix)
5272 + 2 + strlen (imported_name) + 1);
5273 strcpy (temp, imported_name_prefix);
5274 strcat (temp, "::");
5275 strcat (temp, imported_name);
5276 canonical_name = temp;
794684b6 5277 }
13387711
SW
5278 else
5279 canonical_name = imported_name;
794684b6 5280
32019081
JK
5281 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5282
5283 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5284 for (child_die = die->child; child_die && child_die->tag;
5285 child_die = sibling_die (child_die))
5286 {
5287 /* DWARF-4: A Fortran use statement with a “rename list” may be
5288 represented by an imported module entry with an import attribute
5289 referring to the module and owned entries corresponding to those
5290 entities that are renamed as part of being imported. */
5291
5292 if (child_die->tag != DW_TAG_imported_declaration)
5293 {
5294 complaint (&symfile_complaints,
5295 _("child DW_TAG_imported_declaration expected "
5296 "- DIE at 0x%x [in module %s]"),
5297 child_die->offset, cu->objfile->name);
5298 continue;
5299 }
5300
5301 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5302 if (import_attr == NULL)
5303 {
5304 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5305 dwarf_tag_name (child_die->tag));
5306 continue;
5307 }
5308
5309 imported_cu = cu;
5310 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5311 &imported_cu);
5312 imported_name = dwarf2_name (imported_die, imported_cu);
5313 if (imported_name == NULL)
5314 {
5315 complaint (&symfile_complaints,
5316 _("child DW_TAG_imported_declaration has unknown "
5317 "imported name - DIE at 0x%x [in module %s]"),
5318 child_die->offset, cu->objfile->name);
5319 continue;
5320 }
5321
5322 VEC_safe_push (const_char_ptr, excludes, imported_name);
5323
5324 process_die (child_die, cu);
5325 }
5326
c0cc3a76
SW
5327 cp_add_using_directive (import_prefix,
5328 canonical_name,
5329 import_alias,
13387711 5330 imported_declaration,
32019081 5331 excludes,
c0cc3a76 5332 &cu->objfile->objfile_obstack);
32019081
JK
5333
5334 do_cleanups (cleanups);
27aa8d6a
SW
5335}
5336
5fb290d7 5337static void
e142c38c 5338initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5339{
e142c38c 5340 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5341}
5342
ae2de4f8
DE
5343/* Cleanup function for read_file_scope. */
5344
cb1df416
DJ
5345static void
5346free_cu_line_header (void *arg)
5347{
5348 struct dwarf2_cu *cu = arg;
5349
5350 free_line_header (cu->line_header);
5351 cu->line_header = NULL;
5352}
5353
9291a0cd
TT
5354static void
5355find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5356 char **name, char **comp_dir)
5357{
5358 struct attribute *attr;
5359
5360 *name = NULL;
5361 *comp_dir = NULL;
5362
5363 /* Find the filename. Do not use dwarf2_name here, since the filename
5364 is not a source language identifier. */
5365 attr = dwarf2_attr (die, DW_AT_name, cu);
5366 if (attr)
5367 {
5368 *name = DW_STRING (attr);
5369 }
5370
5371 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5372 if (attr)
5373 *comp_dir = DW_STRING (attr);
5374 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5375 {
5376 *comp_dir = ldirname (*name);
5377 if (*comp_dir != NULL)
5378 make_cleanup (xfree, *comp_dir);
5379 }
5380 if (*comp_dir != NULL)
5381 {
5382 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5383 directory, get rid of it. */
5384 char *cp = strchr (*comp_dir, ':');
5385
5386 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5387 *comp_dir = cp + 1;
5388 }
5389
5390 if (*name == NULL)
5391 *name = "<unknown>";
5392}
5393
ae2de4f8
DE
5394/* Process DW_TAG_compile_unit. */
5395
c906108c 5396static void
e7c27a73 5397read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5398{
e7c27a73 5399 struct objfile *objfile = cu->objfile;
debd256d 5400 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5401 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5402 CORE_ADDR highpc = ((CORE_ADDR) 0);
5403 struct attribute *attr;
e1024ff1 5404 char *name = NULL;
c906108c
SS
5405 char *comp_dir = NULL;
5406 struct die_info *child_die;
5407 bfd *abfd = objfile->obfd;
debd256d 5408 struct line_header *line_header = 0;
e142c38c 5409 CORE_ADDR baseaddr;
6e70227d 5410
e142c38c 5411 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5412
fae299cd 5413 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5414
5415 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5416 from finish_block. */
2acceee2 5417 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5418 lowpc = highpc;
5419 lowpc += baseaddr;
5420 highpc += baseaddr;
5421
9291a0cd 5422 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5423
e142c38c 5424 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5425 if (attr)
5426 {
e142c38c 5427 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5428 }
5429
b0f35d58 5430 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5431 if (attr)
b0f35d58 5432 cu->producer = DW_STRING (attr);
303b6f5d 5433
f4b8a18d
KW
5434 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5435 standardised yet. As a workaround for the language detection we fall
5436 back to the DW_AT_producer string. */
5437 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5438 cu->language = language_opencl;
5439
0963b4bd 5440 /* We assume that we're processing GCC output. */
c906108c 5441 processing_gcc_compilation = 2;
c906108c 5442
df8a16a1
DJ
5443 processing_has_namespace_info = 0;
5444
c906108c
SS
5445 start_symtab (name, comp_dir, lowpc);
5446 record_debugformat ("DWARF 2");
303b6f5d 5447 record_producer (cu->producer);
c906108c 5448
e142c38c 5449 initialize_cu_func_list (cu);
c906108c 5450
cb1df416
DJ
5451 /* Decode line number information if present. We do this before
5452 processing child DIEs, so that the line header table is available
5453 for DW_AT_decl_file. */
e142c38c 5454 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5455 if (attr)
5456 {
debd256d 5457 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5458 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5459 if (line_header)
5460 {
cb1df416
DJ
5461 cu->line_header = line_header;
5462 make_cleanup (free_cu_line_header, cu);
aaa75496 5463 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5464 }
5fb290d7 5465 }
debd256d 5466
cb1df416
DJ
5467 /* Process all dies in compilation unit. */
5468 if (die->child != NULL)
5469 {
5470 child_die = die->child;
5471 while (child_die && child_die->tag)
5472 {
5473 process_die (child_die, cu);
5474 child_die = sibling_die (child_die);
5475 }
5476 }
5477
2e276125
JB
5478 /* Decode macro information, if present. Dwarf 2 macro information
5479 refers to information in the line number info statement program
5480 header, so we can only read it if we've read the header
5481 successfully. */
e142c38c 5482 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5483 if (attr && line_header)
2e276125
JB
5484 {
5485 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5486
2e276125 5487 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5488 comp_dir, abfd, cu);
2e276125 5489 }
debd256d 5490 do_cleanups (back_to);
5fb290d7
DJ
5491}
5492
ae2de4f8
DE
5493/* Process DW_TAG_type_unit.
5494 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5495 actual type being defined by this TU. In this case the first top
5496 level sibling is there to provide context only. */
5497
5498static void
5499read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5500{
5501 struct objfile *objfile = cu->objfile;
5502 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5503 CORE_ADDR lowpc;
5504 struct attribute *attr;
5505 char *name = NULL;
5506 char *comp_dir = NULL;
5507 struct die_info *child_die;
5508 bfd *abfd = objfile->obfd;
348e048f
DE
5509
5510 /* start_symtab needs a low pc, but we don't really have one.
5511 Do what read_file_scope would do in the absence of such info. */
5512 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5513
5514 /* Find the filename. Do not use dwarf2_name here, since the filename
5515 is not a source language identifier. */
5516 attr = dwarf2_attr (die, DW_AT_name, cu);
5517 if (attr)
5518 name = DW_STRING (attr);
5519
5520 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5521 if (attr)
5522 comp_dir = DW_STRING (attr);
5523 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5524 {
5525 comp_dir = ldirname (name);
5526 if (comp_dir != NULL)
5527 make_cleanup (xfree, comp_dir);
5528 }
5529
5530 if (name == NULL)
5531 name = "<unknown>";
5532
5533 attr = dwarf2_attr (die, DW_AT_language, cu);
5534 if (attr)
5535 set_cu_language (DW_UNSND (attr), cu);
5536
5537 /* This isn't technically needed today. It is done for symmetry
5538 with read_file_scope. */
5539 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5540 if (attr)
348e048f
DE
5541 cu->producer = DW_STRING (attr);
5542
0963b4bd 5543 /* We assume that we're processing GCC output. */
348e048f
DE
5544 processing_gcc_compilation = 2;
5545
5546 processing_has_namespace_info = 0;
5547
5548 start_symtab (name, comp_dir, lowpc);
5549 record_debugformat ("DWARF 2");
5550 record_producer (cu->producer);
5551
5552 /* Process the dies in the type unit. */
5553 if (die->child == NULL)
5554 {
5555 dump_die_for_error (die);
5556 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5557 bfd_get_filename (abfd));
5558 }
5559
5560 child_die = die->child;
5561
5562 while (child_die && child_die->tag)
5563 {
5564 process_die (child_die, cu);
5565
5566 child_die = sibling_die (child_die);
5567 }
5568
5569 do_cleanups (back_to);
5570}
5571
5fb290d7 5572static void
e142c38c
DJ
5573add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5574 struct dwarf2_cu *cu)
5fb290d7
DJ
5575{
5576 struct function_range *thisfn;
5577
5578 thisfn = (struct function_range *)
7b5a2f43 5579 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5580 thisfn->name = name;
5581 thisfn->lowpc = lowpc;
5582 thisfn->highpc = highpc;
5583 thisfn->seen_line = 0;
5584 thisfn->next = NULL;
5585
e142c38c
DJ
5586 if (cu->last_fn == NULL)
5587 cu->first_fn = thisfn;
5fb290d7 5588 else
e142c38c 5589 cu->last_fn->next = thisfn;
5fb290d7 5590
e142c38c 5591 cu->last_fn = thisfn;
c906108c
SS
5592}
5593
d389af10
JK
5594/* qsort helper for inherit_abstract_dies. */
5595
5596static int
5597unsigned_int_compar (const void *ap, const void *bp)
5598{
5599 unsigned int a = *(unsigned int *) ap;
5600 unsigned int b = *(unsigned int *) bp;
5601
5602 return (a > b) - (b > a);
5603}
5604
5605/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5606 Inherit only the children of the DW_AT_abstract_origin DIE not being
5607 already referenced by DW_AT_abstract_origin from the children of the
5608 current DIE. */
d389af10
JK
5609
5610static void
5611inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5612{
5613 struct die_info *child_die;
5614 unsigned die_children_count;
5615 /* CU offsets which were referenced by children of the current DIE. */
5616 unsigned *offsets;
5617 unsigned *offsets_end, *offsetp;
5618 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5619 struct die_info *origin_die;
5620 /* Iterator of the ORIGIN_DIE children. */
5621 struct die_info *origin_child_die;
5622 struct cleanup *cleanups;
5623 struct attribute *attr;
cd02d79d
PA
5624 struct dwarf2_cu *origin_cu;
5625 struct pending **origin_previous_list_in_scope;
d389af10
JK
5626
5627 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5628 if (!attr)
5629 return;
5630
cd02d79d
PA
5631 /* Note that following die references may follow to a die in a
5632 different cu. */
5633
5634 origin_cu = cu;
5635 origin_die = follow_die_ref (die, attr, &origin_cu);
5636
5637 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5638 symbols in. */
5639 origin_previous_list_in_scope = origin_cu->list_in_scope;
5640 origin_cu->list_in_scope = cu->list_in_scope;
5641
edb3359d
DJ
5642 if (die->tag != origin_die->tag
5643 && !(die->tag == DW_TAG_inlined_subroutine
5644 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5645 complaint (&symfile_complaints,
5646 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5647 die->offset, origin_die->offset);
5648
5649 child_die = die->child;
5650 die_children_count = 0;
5651 while (child_die && child_die->tag)
5652 {
5653 child_die = sibling_die (child_die);
5654 die_children_count++;
5655 }
5656 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5657 cleanups = make_cleanup (xfree, offsets);
5658
5659 offsets_end = offsets;
5660 child_die = die->child;
5661 while (child_die && child_die->tag)
5662 {
c38f313d
DJ
5663 /* For each CHILD_DIE, find the corresponding child of
5664 ORIGIN_DIE. If there is more than one layer of
5665 DW_AT_abstract_origin, follow them all; there shouldn't be,
5666 but GCC versions at least through 4.4 generate this (GCC PR
5667 40573). */
5668 struct die_info *child_origin_die = child_die;
cd02d79d 5669 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5670
c38f313d
DJ
5671 while (1)
5672 {
cd02d79d
PA
5673 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5674 child_origin_cu);
c38f313d
DJ
5675 if (attr == NULL)
5676 break;
cd02d79d
PA
5677 child_origin_die = follow_die_ref (child_origin_die, attr,
5678 &child_origin_cu);
c38f313d
DJ
5679 }
5680
d389af10
JK
5681 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5682 counterpart may exist. */
c38f313d 5683 if (child_origin_die != child_die)
d389af10 5684 {
edb3359d
DJ
5685 if (child_die->tag != child_origin_die->tag
5686 && !(child_die->tag == DW_TAG_inlined_subroutine
5687 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5688 complaint (&symfile_complaints,
5689 _("Child DIE 0x%x and its abstract origin 0x%x have "
5690 "different tags"), child_die->offset,
5691 child_origin_die->offset);
c38f313d
DJ
5692 if (child_origin_die->parent != origin_die)
5693 complaint (&symfile_complaints,
5694 _("Child DIE 0x%x and its abstract origin 0x%x have "
5695 "different parents"), child_die->offset,
5696 child_origin_die->offset);
5697 else
5698 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5699 }
5700 child_die = sibling_die (child_die);
5701 }
5702 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5703 unsigned_int_compar);
5704 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5705 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5706 complaint (&symfile_complaints,
5707 _("Multiple children of DIE 0x%x refer "
5708 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5709 die->offset, *offsetp);
5710
5711 offsetp = offsets;
5712 origin_child_die = origin_die->child;
5713 while (origin_child_die && origin_child_die->tag)
5714 {
5715 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5716 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5717 offsetp++;
5718 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5719 {
5720 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5721 process_die (origin_child_die, origin_cu);
d389af10
JK
5722 }
5723 origin_child_die = sibling_die (origin_child_die);
5724 }
cd02d79d 5725 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5726
5727 do_cleanups (cleanups);
5728}
5729
c906108c 5730static void
e7c27a73 5731read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5732{
e7c27a73 5733 struct objfile *objfile = cu->objfile;
52f0bd74 5734 struct context_stack *new;
c906108c
SS
5735 CORE_ADDR lowpc;
5736 CORE_ADDR highpc;
5737 struct die_info *child_die;
edb3359d 5738 struct attribute *attr, *call_line, *call_file;
c906108c 5739 char *name;
e142c38c 5740 CORE_ADDR baseaddr;
801e3a5b 5741 struct block *block;
edb3359d 5742 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5743 VEC (symbolp) *template_args = NULL;
5744 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5745
5746 if (inlined_func)
5747 {
5748 /* If we do not have call site information, we can't show the
5749 caller of this inlined function. That's too confusing, so
5750 only use the scope for local variables. */
5751 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5752 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5753 if (call_line == NULL || call_file == NULL)
5754 {
5755 read_lexical_block_scope (die, cu);
5756 return;
5757 }
5758 }
c906108c 5759
e142c38c
DJ
5760 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5761
94af9270 5762 name = dwarf2_name (die, cu);
c906108c 5763
e8d05480
JB
5764 /* Ignore functions with missing or empty names. These are actually
5765 illegal according to the DWARF standard. */
5766 if (name == NULL)
5767 {
5768 complaint (&symfile_complaints,
5769 _("missing name for subprogram DIE at %d"), die->offset);
5770 return;
5771 }
5772
5773 /* Ignore functions with missing or invalid low and high pc attributes. */
5774 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5775 {
ae4d0c03
PM
5776 attr = dwarf2_attr (die, DW_AT_external, cu);
5777 if (!attr || !DW_UNSND (attr))
5778 complaint (&symfile_complaints,
3e43a32a
MS
5779 _("cannot get low and high bounds "
5780 "for subprogram DIE at %d"),
ae4d0c03 5781 die->offset);
e8d05480
JB
5782 return;
5783 }
c906108c
SS
5784
5785 lowpc += baseaddr;
5786 highpc += baseaddr;
5787
5fb290d7 5788 /* Record the function range for dwarf_decode_lines. */
e142c38c 5789 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5790
34eaf542
TT
5791 /* If we have any template arguments, then we must allocate a
5792 different sort of symbol. */
5793 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5794 {
5795 if (child_die->tag == DW_TAG_template_type_param
5796 || child_die->tag == DW_TAG_template_value_param)
5797 {
5798 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5799 struct template_symbol);
5800 templ_func->base.is_cplus_template_function = 1;
5801 break;
5802 }
5803 }
5804
c906108c 5805 new = push_context (0, lowpc);
34eaf542
TT
5806 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5807 (struct symbol *) templ_func);
4c2df51b 5808
4cecd739
DJ
5809 /* If there is a location expression for DW_AT_frame_base, record
5810 it. */
e142c38c 5811 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5812 if (attr)
c034e007
AC
5813 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5814 expression is being recorded directly in the function's symbol
5815 and not in a separate frame-base object. I guess this hack is
5816 to avoid adding some sort of frame-base adjunct/annex to the
5817 function's symbol :-(. The problem with doing this is that it
5818 results in a function symbol with a location expression that
5819 has nothing to do with the location of the function, ouch! The
5820 relationship should be: a function's symbol has-a frame base; a
5821 frame-base has-a location expression. */
e7c27a73 5822 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5823
e142c38c 5824 cu->list_in_scope = &local_symbols;
c906108c 5825
639d11d3 5826 if (die->child != NULL)
c906108c 5827 {
639d11d3 5828 child_die = die->child;
c906108c
SS
5829 while (child_die && child_die->tag)
5830 {
34eaf542
TT
5831 if (child_die->tag == DW_TAG_template_type_param
5832 || child_die->tag == DW_TAG_template_value_param)
5833 {
5834 struct symbol *arg = new_symbol (child_die, NULL, cu);
5835
f1078f66
DJ
5836 if (arg != NULL)
5837 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5838 }
5839 else
5840 process_die (child_die, cu);
c906108c
SS
5841 child_die = sibling_die (child_die);
5842 }
5843 }
5844
d389af10
JK
5845 inherit_abstract_dies (die, cu);
5846
4a811a97
UW
5847 /* If we have a DW_AT_specification, we might need to import using
5848 directives from the context of the specification DIE. See the
5849 comment in determine_prefix. */
5850 if (cu->language == language_cplus
5851 && dwarf2_attr (die, DW_AT_specification, cu))
5852 {
5853 struct dwarf2_cu *spec_cu = cu;
5854 struct die_info *spec_die = die_specification (die, &spec_cu);
5855
5856 while (spec_die)
5857 {
5858 child_die = spec_die->child;
5859 while (child_die && child_die->tag)
5860 {
5861 if (child_die->tag == DW_TAG_imported_module)
5862 process_die (child_die, spec_cu);
5863 child_die = sibling_die (child_die);
5864 }
5865
5866 /* In some cases, GCC generates specification DIEs that
5867 themselves contain DW_AT_specification attributes. */
5868 spec_die = die_specification (spec_die, &spec_cu);
5869 }
5870 }
5871
c906108c
SS
5872 new = pop_context ();
5873 /* Make a block for the local symbols within. */
801e3a5b
JB
5874 block = finish_block (new->name, &local_symbols, new->old_blocks,
5875 lowpc, highpc, objfile);
5876
df8a16a1 5877 /* For C++, set the block's scope. */
f55ee35c 5878 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5879 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5880 determine_prefix (die, cu),
df8a16a1
DJ
5881 processing_has_namespace_info);
5882
801e3a5b
JB
5883 /* If we have address ranges, record them. */
5884 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5885
34eaf542
TT
5886 /* Attach template arguments to function. */
5887 if (! VEC_empty (symbolp, template_args))
5888 {
5889 gdb_assert (templ_func != NULL);
5890
5891 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5892 templ_func->template_arguments
5893 = obstack_alloc (&objfile->objfile_obstack,
5894 (templ_func->n_template_arguments
5895 * sizeof (struct symbol *)));
5896 memcpy (templ_func->template_arguments,
5897 VEC_address (symbolp, template_args),
5898 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5899 VEC_free (symbolp, template_args);
5900 }
5901
208d8187
JB
5902 /* In C++, we can have functions nested inside functions (e.g., when
5903 a function declares a class that has methods). This means that
5904 when we finish processing a function scope, we may need to go
5905 back to building a containing block's symbol lists. */
5906 local_symbols = new->locals;
5907 param_symbols = new->params;
27aa8d6a 5908 using_directives = new->using_directives;
208d8187 5909
921e78cf
JB
5910 /* If we've finished processing a top-level function, subsequent
5911 symbols go in the file symbol list. */
5912 if (outermost_context_p ())
e142c38c 5913 cu->list_in_scope = &file_symbols;
c906108c
SS
5914}
5915
5916/* Process all the DIES contained within a lexical block scope. Start
5917 a new scope, process the dies, and then close the scope. */
5918
5919static void
e7c27a73 5920read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5921{
e7c27a73 5922 struct objfile *objfile = cu->objfile;
52f0bd74 5923 struct context_stack *new;
c906108c
SS
5924 CORE_ADDR lowpc, highpc;
5925 struct die_info *child_die;
e142c38c
DJ
5926 CORE_ADDR baseaddr;
5927
5928 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5929
5930 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5931 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5932 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5933 be nasty. Might be easier to properly extend generic blocks to
af34e669 5934 describe ranges. */
d85a05f0 5935 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5936 return;
5937 lowpc += baseaddr;
5938 highpc += baseaddr;
5939
5940 push_context (0, lowpc);
639d11d3 5941 if (die->child != NULL)
c906108c 5942 {
639d11d3 5943 child_die = die->child;
c906108c
SS
5944 while (child_die && child_die->tag)
5945 {
e7c27a73 5946 process_die (child_die, cu);
c906108c
SS
5947 child_die = sibling_die (child_die);
5948 }
5949 }
5950 new = pop_context ();
5951
8540c487 5952 if (local_symbols != NULL || using_directives != NULL)
c906108c 5953 {
801e3a5b
JB
5954 struct block *block
5955 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5956 highpc, objfile);
5957
5958 /* Note that recording ranges after traversing children, as we
5959 do here, means that recording a parent's ranges entails
5960 walking across all its children's ranges as they appear in
5961 the address map, which is quadratic behavior.
5962
5963 It would be nicer to record the parent's ranges before
5964 traversing its children, simply overriding whatever you find
5965 there. But since we don't even decide whether to create a
5966 block until after we've traversed its children, that's hard
5967 to do. */
5968 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5969 }
5970 local_symbols = new->locals;
27aa8d6a 5971 using_directives = new->using_directives;
c906108c
SS
5972}
5973
43039443 5974/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5975 Return 1 if the attributes are present and valid, otherwise, return 0.
5976 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5977
5978static int
5979dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5980 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5981 struct partial_symtab *ranges_pst)
43039443
JK
5982{
5983 struct objfile *objfile = cu->objfile;
5984 struct comp_unit_head *cu_header = &cu->header;
5985 bfd *obfd = objfile->obfd;
5986 unsigned int addr_size = cu_header->addr_size;
5987 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5988 /* Base address selection entry. */
5989 CORE_ADDR base;
5990 int found_base;
5991 unsigned int dummy;
5992 gdb_byte *buffer;
5993 CORE_ADDR marker;
5994 int low_set;
5995 CORE_ADDR low = 0;
5996 CORE_ADDR high = 0;
ff013f42 5997 CORE_ADDR baseaddr;
43039443 5998
d00adf39
DE
5999 found_base = cu->base_known;
6000 base = cu->base_address;
43039443 6001
be391dca 6002 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6003 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6004 {
6005 complaint (&symfile_complaints,
6006 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6007 offset);
6008 return 0;
6009 }
dce234bc 6010 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6011
6012 /* Read in the largest possible address. */
6013 marker = read_address (obfd, buffer, cu, &dummy);
6014 if ((marker & mask) == mask)
6015 {
6016 /* If we found the largest possible address, then
6017 read the base address. */
6018 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6019 buffer += 2 * addr_size;
6020 offset += 2 * addr_size;
6021 found_base = 1;
6022 }
6023
6024 low_set = 0;
6025
e7030f15 6026 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6027
43039443
JK
6028 while (1)
6029 {
6030 CORE_ADDR range_beginning, range_end;
6031
6032 range_beginning = read_address (obfd, buffer, cu, &dummy);
6033 buffer += addr_size;
6034 range_end = read_address (obfd, buffer, cu, &dummy);
6035 buffer += addr_size;
6036 offset += 2 * addr_size;
6037
6038 /* An end of list marker is a pair of zero addresses. */
6039 if (range_beginning == 0 && range_end == 0)
6040 /* Found the end of list entry. */
6041 break;
6042
6043 /* Each base address selection entry is a pair of 2 values.
6044 The first is the largest possible address, the second is
6045 the base address. Check for a base address here. */
6046 if ((range_beginning & mask) == mask)
6047 {
6048 /* If we found the largest possible address, then
6049 read the base address. */
6050 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6051 found_base = 1;
6052 continue;
6053 }
6054
6055 if (!found_base)
6056 {
6057 /* We have no valid base address for the ranges
6058 data. */
6059 complaint (&symfile_complaints,
6060 _("Invalid .debug_ranges data (no base address)"));
6061 return 0;
6062 }
6063
9277c30c
UW
6064 if (range_beginning > range_end)
6065 {
6066 /* Inverted range entries are invalid. */
6067 complaint (&symfile_complaints,
6068 _("Invalid .debug_ranges data (inverted range)"));
6069 return 0;
6070 }
6071
6072 /* Empty range entries have no effect. */
6073 if (range_beginning == range_end)
6074 continue;
6075
43039443
JK
6076 range_beginning += base;
6077 range_end += base;
6078
9277c30c 6079 if (ranges_pst != NULL)
ff013f42 6080 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6081 range_beginning + baseaddr,
6082 range_end - 1 + baseaddr,
ff013f42
JK
6083 ranges_pst);
6084
43039443
JK
6085 /* FIXME: This is recording everything as a low-high
6086 segment of consecutive addresses. We should have a
6087 data structure for discontiguous block ranges
6088 instead. */
6089 if (! low_set)
6090 {
6091 low = range_beginning;
6092 high = range_end;
6093 low_set = 1;
6094 }
6095 else
6096 {
6097 if (range_beginning < low)
6098 low = range_beginning;
6099 if (range_end > high)
6100 high = range_end;
6101 }
6102 }
6103
6104 if (! low_set)
6105 /* If the first entry is an end-of-list marker, the range
6106 describes an empty scope, i.e. no instructions. */
6107 return 0;
6108
6109 if (low_return)
6110 *low_return = low;
6111 if (high_return)
6112 *high_return = high;
6113 return 1;
6114}
6115
af34e669
DJ
6116/* Get low and high pc attributes from a die. Return 1 if the attributes
6117 are present and valid, otherwise, return 0. Return -1 if the range is
6118 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6119static int
af34e669 6120dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6121 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6122 struct partial_symtab *pst)
c906108c
SS
6123{
6124 struct attribute *attr;
af34e669
DJ
6125 CORE_ADDR low = 0;
6126 CORE_ADDR high = 0;
6127 int ret = 0;
c906108c 6128
e142c38c 6129 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6130 if (attr)
af34e669
DJ
6131 {
6132 high = DW_ADDR (attr);
e142c38c 6133 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6134 if (attr)
6135 low = DW_ADDR (attr);
6136 else
6137 /* Found high w/o low attribute. */
6138 return 0;
6139
6140 /* Found consecutive range of addresses. */
6141 ret = 1;
6142 }
c906108c 6143 else
af34e669 6144 {
e142c38c 6145 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6146 if (attr != NULL)
6147 {
af34e669 6148 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6149 .debug_ranges section. */
d85a05f0 6150 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6151 return 0;
43039443 6152 /* Found discontinuous range of addresses. */
af34e669
DJ
6153 ret = -1;
6154 }
6155 }
c906108c 6156
9373cf26
JK
6157 /* read_partial_die has also the strict LOW < HIGH requirement. */
6158 if (high <= low)
c906108c
SS
6159 return 0;
6160
6161 /* When using the GNU linker, .gnu.linkonce. sections are used to
6162 eliminate duplicate copies of functions and vtables and such.
6163 The linker will arbitrarily choose one and discard the others.
6164 The AT_*_pc values for such functions refer to local labels in
6165 these sections. If the section from that file was discarded, the
6166 labels are not in the output, so the relocs get a value of 0.
6167 If this is a discarded function, mark the pc bounds as invalid,
6168 so that GDB will ignore it. */
72dca2f5 6169 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6170 return 0;
6171
6172 *lowpc = low;
6173 *highpc = high;
af34e669 6174 return ret;
c906108c
SS
6175}
6176
b084d499
JB
6177/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6178 its low and high PC addresses. Do nothing if these addresses could not
6179 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6180 and HIGHPC to the high address if greater than HIGHPC. */
6181
6182static void
6183dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6184 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6185 struct dwarf2_cu *cu)
6186{
6187 CORE_ADDR low, high;
6188 struct die_info *child = die->child;
6189
d85a05f0 6190 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6191 {
6192 *lowpc = min (*lowpc, low);
6193 *highpc = max (*highpc, high);
6194 }
6195
6196 /* If the language does not allow nested subprograms (either inside
6197 subprograms or lexical blocks), we're done. */
6198 if (cu->language != language_ada)
6199 return;
6e70227d 6200
b084d499
JB
6201 /* Check all the children of the given DIE. If it contains nested
6202 subprograms, then check their pc bounds. Likewise, we need to
6203 check lexical blocks as well, as they may also contain subprogram
6204 definitions. */
6205 while (child && child->tag)
6206 {
6207 if (child->tag == DW_TAG_subprogram
6208 || child->tag == DW_TAG_lexical_block)
6209 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6210 child = sibling_die (child);
6211 }
6212}
6213
fae299cd
DC
6214/* Get the low and high pc's represented by the scope DIE, and store
6215 them in *LOWPC and *HIGHPC. If the correct values can't be
6216 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6217
6218static void
6219get_scope_pc_bounds (struct die_info *die,
6220 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6221 struct dwarf2_cu *cu)
6222{
6223 CORE_ADDR best_low = (CORE_ADDR) -1;
6224 CORE_ADDR best_high = (CORE_ADDR) 0;
6225 CORE_ADDR current_low, current_high;
6226
d85a05f0 6227 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6228 {
6229 best_low = current_low;
6230 best_high = current_high;
6231 }
6232 else
6233 {
6234 struct die_info *child = die->child;
6235
6236 while (child && child->tag)
6237 {
6238 switch (child->tag) {
6239 case DW_TAG_subprogram:
b084d499 6240 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6241 break;
6242 case DW_TAG_namespace:
f55ee35c 6243 case DW_TAG_module:
fae299cd
DC
6244 /* FIXME: carlton/2004-01-16: Should we do this for
6245 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6246 that current GCC's always emit the DIEs corresponding
6247 to definitions of methods of classes as children of a
6248 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6249 the DIEs giving the declarations, which could be
6250 anywhere). But I don't see any reason why the
6251 standards says that they have to be there. */
6252 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6253
6254 if (current_low != ((CORE_ADDR) -1))
6255 {
6256 best_low = min (best_low, current_low);
6257 best_high = max (best_high, current_high);
6258 }
6259 break;
6260 default:
0963b4bd 6261 /* Ignore. */
fae299cd
DC
6262 break;
6263 }
6264
6265 child = sibling_die (child);
6266 }
6267 }
6268
6269 *lowpc = best_low;
6270 *highpc = best_high;
6271}
6272
801e3a5b
JB
6273/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6274 in DIE. */
6275static void
6276dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6277 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6278{
6279 struct attribute *attr;
6280
6281 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6282 if (attr)
6283 {
6284 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6285
801e3a5b
JB
6286 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6287 if (attr)
6288 {
6289 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6290
801e3a5b
JB
6291 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6292 }
6293 }
6294
6295 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6296 if (attr)
6297 {
6298 bfd *obfd = cu->objfile->obfd;
6299
6300 /* The value of the DW_AT_ranges attribute is the offset of the
6301 address range list in the .debug_ranges section. */
6302 unsigned long offset = DW_UNSND (attr);
dce234bc 6303 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6304
6305 /* For some target architectures, but not others, the
6306 read_address function sign-extends the addresses it returns.
6307 To recognize base address selection entries, we need a
6308 mask. */
6309 unsigned int addr_size = cu->header.addr_size;
6310 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6311
6312 /* The base address, to which the next pair is relative. Note
6313 that this 'base' is a DWARF concept: most entries in a range
6314 list are relative, to reduce the number of relocs against the
6315 debugging information. This is separate from this function's
6316 'baseaddr' argument, which GDB uses to relocate debugging
6317 information from a shared library based on the address at
6318 which the library was loaded. */
d00adf39
DE
6319 CORE_ADDR base = cu->base_address;
6320 int base_known = cu->base_known;
801e3a5b 6321
be391dca 6322 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6323 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6324 {
6325 complaint (&symfile_complaints,
6326 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6327 offset);
6328 return;
6329 }
6330
6331 for (;;)
6332 {
6333 unsigned int bytes_read;
6334 CORE_ADDR start, end;
6335
6336 start = read_address (obfd, buffer, cu, &bytes_read);
6337 buffer += bytes_read;
6338 end = read_address (obfd, buffer, cu, &bytes_read);
6339 buffer += bytes_read;
6340
6341 /* Did we find the end of the range list? */
6342 if (start == 0 && end == 0)
6343 break;
6344
6345 /* Did we find a base address selection entry? */
6346 else if ((start & base_select_mask) == base_select_mask)
6347 {
6348 base = end;
6349 base_known = 1;
6350 }
6351
6352 /* We found an ordinary address range. */
6353 else
6354 {
6355 if (!base_known)
6356 {
6357 complaint (&symfile_complaints,
3e43a32a
MS
6358 _("Invalid .debug_ranges data "
6359 "(no base address)"));
801e3a5b
JB
6360 return;
6361 }
6362
9277c30c
UW
6363 if (start > end)
6364 {
6365 /* Inverted range entries are invalid. */
6366 complaint (&symfile_complaints,
6367 _("Invalid .debug_ranges data "
6368 "(inverted range)"));
6369 return;
6370 }
6371
6372 /* Empty range entries have no effect. */
6373 if (start == end)
6374 continue;
6375
6e70227d
DE
6376 record_block_range (block,
6377 baseaddr + base + start,
801e3a5b
JB
6378 baseaddr + base + end - 1);
6379 }
6380 }
6381 }
6382}
6383
60d5a603
JK
6384/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6385 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6386 during 4.6.0 experimental. */
6387
6388static int
6389producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6390{
6391 const char *cs;
6392 int major, minor, release;
6393
6394 if (cu->producer == NULL)
6395 {
6396 /* For unknown compilers expect their behavior is DWARF version
6397 compliant.
6398
6399 GCC started to support .debug_types sections by -gdwarf-4 since
6400 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6401 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6402 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6403 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6404
6405 return 0;
6406 }
6407
6408 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6409
6410 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6411 {
6412 /* For non-GCC compilers expect their behavior is DWARF version
6413 compliant. */
6414
6415 return 0;
6416 }
6417 cs = &cu->producer[strlen ("GNU ")];
6418 while (*cs && !isdigit (*cs))
6419 cs++;
6420 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6421 {
6422 /* Not recognized as GCC. */
6423
6424 return 0;
6425 }
6426
6427 return major < 4 || (major == 4 && minor < 6);
6428}
6429
6430/* Return the default accessibility type if it is not overriden by
6431 DW_AT_accessibility. */
6432
6433static enum dwarf_access_attribute
6434dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6435{
6436 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6437 {
6438 /* The default DWARF 2 accessibility for members is public, the default
6439 accessibility for inheritance is private. */
6440
6441 if (die->tag != DW_TAG_inheritance)
6442 return DW_ACCESS_public;
6443 else
6444 return DW_ACCESS_private;
6445 }
6446 else
6447 {
6448 /* DWARF 3+ defines the default accessibility a different way. The same
6449 rules apply now for DW_TAG_inheritance as for the members and it only
6450 depends on the container kind. */
6451
6452 if (die->parent->tag == DW_TAG_class_type)
6453 return DW_ACCESS_private;
6454 else
6455 return DW_ACCESS_public;
6456 }
6457}
6458
74ac6d43
TT
6459/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6460 offset. If the attribute was not found return 0, otherwise return
6461 1. If it was found but could not properly be handled, set *OFFSET
6462 to 0. */
6463
6464static int
6465handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6466 LONGEST *offset)
6467{
6468 struct attribute *attr;
6469
6470 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6471 if (attr != NULL)
6472 {
6473 *offset = 0;
6474
6475 /* Note that we do not check for a section offset first here.
6476 This is because DW_AT_data_member_location is new in DWARF 4,
6477 so if we see it, we can assume that a constant form is really
6478 a constant and not a section offset. */
6479 if (attr_form_is_constant (attr))
6480 *offset = dwarf2_get_attr_constant_value (attr, 0);
6481 else if (attr_form_is_section_offset (attr))
6482 dwarf2_complex_location_expr_complaint ();
6483 else if (attr_form_is_block (attr))
6484 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6485 else
6486 dwarf2_complex_location_expr_complaint ();
6487
6488 return 1;
6489 }
6490
6491 return 0;
6492}
6493
c906108c
SS
6494/* Add an aggregate field to the field list. */
6495
6496static void
107d2387 6497dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6498 struct dwarf2_cu *cu)
6e70227d 6499{
e7c27a73 6500 struct objfile *objfile = cu->objfile;
5e2b427d 6501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6502 struct nextfield *new_field;
6503 struct attribute *attr;
6504 struct field *fp;
6505 char *fieldname = "";
6506
6507 /* Allocate a new field list entry and link it in. */
6508 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6509 make_cleanup (xfree, new_field);
c906108c 6510 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6511
6512 if (die->tag == DW_TAG_inheritance)
6513 {
6514 new_field->next = fip->baseclasses;
6515 fip->baseclasses = new_field;
6516 }
6517 else
6518 {
6519 new_field->next = fip->fields;
6520 fip->fields = new_field;
6521 }
c906108c
SS
6522 fip->nfields++;
6523
e142c38c 6524 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6525 if (attr)
6526 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6527 else
6528 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6529 if (new_field->accessibility != DW_ACCESS_public)
6530 fip->non_public_fields = 1;
60d5a603 6531
e142c38c 6532 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6533 if (attr)
6534 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6535 else
6536 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6537
6538 fp = &new_field->field;
a9a9bd0f 6539
e142c38c 6540 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6541 {
74ac6d43
TT
6542 LONGEST offset;
6543
a9a9bd0f 6544 /* Data member other than a C++ static data member. */
6e70227d 6545
c906108c 6546 /* Get type of field. */
e7c27a73 6547 fp->type = die_type (die, cu);
c906108c 6548
d6a843b5 6549 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6550
c906108c 6551 /* Get bit size of field (zero if none). */
e142c38c 6552 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6553 if (attr)
6554 {
6555 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6556 }
6557 else
6558 {
6559 FIELD_BITSIZE (*fp) = 0;
6560 }
6561
6562 /* Get bit offset of field. */
74ac6d43
TT
6563 if (handle_data_member_location (die, cu, &offset))
6564 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6565 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6566 if (attr)
6567 {
5e2b427d 6568 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6569 {
6570 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6571 additional bit offset from the MSB of the containing
6572 anonymous object to the MSB of the field. We don't
6573 have to do anything special since we don't need to
6574 know the size of the anonymous object. */
c906108c
SS
6575 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6576 }
6577 else
6578 {
6579 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6580 MSB of the anonymous object, subtract off the number of
6581 bits from the MSB of the field to the MSB of the
6582 object, and then subtract off the number of bits of
6583 the field itself. The result is the bit offset of
6584 the LSB of the field. */
c906108c
SS
6585 int anonymous_size;
6586 int bit_offset = DW_UNSND (attr);
6587
e142c38c 6588 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6589 if (attr)
6590 {
6591 /* The size of the anonymous object containing
6592 the bit field is explicit, so use the
6593 indicated size (in bytes). */
6594 anonymous_size = DW_UNSND (attr);
6595 }
6596 else
6597 {
6598 /* The size of the anonymous object containing
6599 the bit field must be inferred from the type
6600 attribute of the data member containing the
6601 bit field. */
6602 anonymous_size = TYPE_LENGTH (fp->type);
6603 }
6604 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6605 - bit_offset - FIELD_BITSIZE (*fp);
6606 }
6607 }
6608
6609 /* Get name of field. */
39cbfefa
DJ
6610 fieldname = dwarf2_name (die, cu);
6611 if (fieldname == NULL)
6612 fieldname = "";
d8151005
DJ
6613
6614 /* The name is already allocated along with this objfile, so we don't
6615 need to duplicate it for the type. */
6616 fp->name = fieldname;
c906108c
SS
6617
6618 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6619 pointer or virtual base class pointer) to private. */
e142c38c 6620 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6621 {
d48cc9dd 6622 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6623 new_field->accessibility = DW_ACCESS_private;
6624 fip->non_public_fields = 1;
6625 }
6626 }
a9a9bd0f 6627 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6628 {
a9a9bd0f
DC
6629 /* C++ static member. */
6630
6631 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6632 is a declaration, but all versions of G++ as of this writing
6633 (so through at least 3.2.1) incorrectly generate
6634 DW_TAG_variable tags. */
6e70227d 6635
ff355380 6636 const char *physname;
c906108c 6637
a9a9bd0f 6638 /* Get name of field. */
39cbfefa
DJ
6639 fieldname = dwarf2_name (die, cu);
6640 if (fieldname == NULL)
c906108c
SS
6641 return;
6642
254e6b9e 6643 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6644 if (attr
6645 /* Only create a symbol if this is an external value.
6646 new_symbol checks this and puts the value in the global symbol
6647 table, which we want. If it is not external, new_symbol
6648 will try to put the value in cu->list_in_scope which is wrong. */
6649 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6650 {
6651 /* A static const member, not much different than an enum as far as
6652 we're concerned, except that we can support more types. */
6653 new_symbol (die, NULL, cu);
6654 }
6655
2df3850c 6656 /* Get physical name. */
ff355380 6657 physname = dwarf2_physname (fieldname, die, cu);
c906108c 6658
d8151005
DJ
6659 /* The name is already allocated along with this objfile, so we don't
6660 need to duplicate it for the type. */
6661 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6662 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6663 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6664 }
6665 else if (die->tag == DW_TAG_inheritance)
6666 {
74ac6d43 6667 LONGEST offset;
d4b96c9a 6668
74ac6d43
TT
6669 /* C++ base class field. */
6670 if (handle_data_member_location (die, cu, &offset))
6671 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6672 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6673 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6674 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6675 fip->nbaseclasses++;
6676 }
6677}
6678
98751a41
JK
6679/* Add a typedef defined in the scope of the FIP's class. */
6680
6681static void
6682dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6683 struct dwarf2_cu *cu)
6e70227d 6684{
98751a41 6685 struct objfile *objfile = cu->objfile;
98751a41
JK
6686 struct typedef_field_list *new_field;
6687 struct attribute *attr;
6688 struct typedef_field *fp;
6689 char *fieldname = "";
6690
6691 /* Allocate a new field list entry and link it in. */
6692 new_field = xzalloc (sizeof (*new_field));
6693 make_cleanup (xfree, new_field);
6694
6695 gdb_assert (die->tag == DW_TAG_typedef);
6696
6697 fp = &new_field->field;
6698
6699 /* Get name of field. */
6700 fp->name = dwarf2_name (die, cu);
6701 if (fp->name == NULL)
6702 return;
6703
6704 fp->type = read_type_die (die, cu);
6705
6706 new_field->next = fip->typedef_field_list;
6707 fip->typedef_field_list = new_field;
6708 fip->typedef_field_list_count++;
6709}
6710
c906108c
SS
6711/* Create the vector of fields, and attach it to the type. */
6712
6713static void
fba45db2 6714dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6715 struct dwarf2_cu *cu)
c906108c
SS
6716{
6717 int nfields = fip->nfields;
6718
6719 /* Record the field count, allocate space for the array of fields,
6720 and create blank accessibility bitfields if necessary. */
6721 TYPE_NFIELDS (type) = nfields;
6722 TYPE_FIELDS (type) = (struct field *)
6723 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6724 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6725
b4ba55a1 6726 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6727 {
6728 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6729
6730 TYPE_FIELD_PRIVATE_BITS (type) =
6731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6732 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6733
6734 TYPE_FIELD_PROTECTED_BITS (type) =
6735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6736 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6737
774b6a14
TT
6738 TYPE_FIELD_IGNORE_BITS (type) =
6739 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6740 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6741 }
6742
6743 /* If the type has baseclasses, allocate and clear a bit vector for
6744 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6745 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6746 {
6747 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6748 unsigned char *pointer;
c906108c
SS
6749
6750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6751 pointer = TYPE_ALLOC (type, num_bytes);
6752 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6753 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6754 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6755 }
6756
3e43a32a
MS
6757 /* Copy the saved-up fields into the field vector. Start from the head of
6758 the list, adding to the tail of the field array, so that they end up in
6759 the same order in the array in which they were added to the list. */
c906108c
SS
6760 while (nfields-- > 0)
6761 {
7d0ccb61
DJ
6762 struct nextfield *fieldp;
6763
6764 if (fip->fields)
6765 {
6766 fieldp = fip->fields;
6767 fip->fields = fieldp->next;
6768 }
6769 else
6770 {
6771 fieldp = fip->baseclasses;
6772 fip->baseclasses = fieldp->next;
6773 }
6774
6775 TYPE_FIELD (type, nfields) = fieldp->field;
6776 switch (fieldp->accessibility)
c906108c 6777 {
c5aa993b 6778 case DW_ACCESS_private:
b4ba55a1
JB
6779 if (cu->language != language_ada)
6780 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6781 break;
c906108c 6782
c5aa993b 6783 case DW_ACCESS_protected:
b4ba55a1
JB
6784 if (cu->language != language_ada)
6785 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6786 break;
c906108c 6787
c5aa993b
JM
6788 case DW_ACCESS_public:
6789 break;
c906108c 6790
c5aa993b
JM
6791 default:
6792 /* Unknown accessibility. Complain and treat it as public. */
6793 {
e2e0b3e5 6794 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6795 fieldp->accessibility);
c5aa993b
JM
6796 }
6797 break;
c906108c
SS
6798 }
6799 if (nfields < fip->nbaseclasses)
6800 {
7d0ccb61 6801 switch (fieldp->virtuality)
c906108c 6802 {
c5aa993b
JM
6803 case DW_VIRTUALITY_virtual:
6804 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6805 if (cu->language == language_ada)
a73c6dcd 6806 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6807 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6808 break;
c906108c
SS
6809 }
6810 }
c906108c
SS
6811 }
6812}
6813
c906108c
SS
6814/* Add a member function to the proper fieldlist. */
6815
6816static void
107d2387 6817dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6818 struct type *type, struct dwarf2_cu *cu)
c906108c 6819{
e7c27a73 6820 struct objfile *objfile = cu->objfile;
c906108c
SS
6821 struct attribute *attr;
6822 struct fnfieldlist *flp;
6823 int i;
6824 struct fn_field *fnp;
6825 char *fieldname;
c906108c 6826 struct nextfnfield *new_fnfield;
f792889a 6827 struct type *this_type;
60d5a603 6828 enum dwarf_access_attribute accessibility;
c906108c 6829
b4ba55a1 6830 if (cu->language == language_ada)
a73c6dcd 6831 error (_("unexpected member function in Ada type"));
b4ba55a1 6832
2df3850c 6833 /* Get name of member function. */
39cbfefa
DJ
6834 fieldname = dwarf2_name (die, cu);
6835 if (fieldname == NULL)
2df3850c 6836 return;
c906108c 6837
c906108c
SS
6838 /* Look up member function name in fieldlist. */
6839 for (i = 0; i < fip->nfnfields; i++)
6840 {
27bfe10e 6841 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6842 break;
6843 }
6844
6845 /* Create new list element if necessary. */
6846 if (i < fip->nfnfields)
6847 flp = &fip->fnfieldlists[i];
6848 else
6849 {
6850 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6851 {
6852 fip->fnfieldlists = (struct fnfieldlist *)
6853 xrealloc (fip->fnfieldlists,
6854 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6855 * sizeof (struct fnfieldlist));
c906108c 6856 if (fip->nfnfields == 0)
c13c43fd 6857 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6858 }
6859 flp = &fip->fnfieldlists[fip->nfnfields];
6860 flp->name = fieldname;
6861 flp->length = 0;
6862 flp->head = NULL;
3da10d80 6863 i = fip->nfnfields++;
c906108c
SS
6864 }
6865
6866 /* Create a new member function field and chain it to the field list
0963b4bd 6867 entry. */
c906108c 6868 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6869 make_cleanup (xfree, new_fnfield);
c906108c
SS
6870 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6871 new_fnfield->next = flp->head;
6872 flp->head = new_fnfield;
6873 flp->length++;
6874
6875 /* Fill in the member function field info. */
6876 fnp = &new_fnfield->fnfield;
3da10d80
KS
6877
6878 /* Delay processing of the physname until later. */
6879 if (cu->language == language_cplus || cu->language == language_java)
6880 {
6881 add_to_method_list (type, i, flp->length - 1, fieldname,
6882 die, cu);
6883 }
6884 else
6885 {
1d06ead6 6886 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
6887 fnp->physname = physname ? physname : "";
6888 }
6889
c906108c 6890 fnp->type = alloc_type (objfile);
f792889a
DJ
6891 this_type = read_type_die (die, cu);
6892 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6893 {
f792889a 6894 int nparams = TYPE_NFIELDS (this_type);
c906108c 6895
f792889a 6896 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6897 of the method itself (TYPE_CODE_METHOD). */
6898 smash_to_method_type (fnp->type, type,
f792889a
DJ
6899 TYPE_TARGET_TYPE (this_type),
6900 TYPE_FIELDS (this_type),
6901 TYPE_NFIELDS (this_type),
6902 TYPE_VARARGS (this_type));
c906108c
SS
6903
6904 /* Handle static member functions.
c5aa993b 6905 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6906 member functions. G++ helps GDB by marking the first
6907 parameter for non-static member functions (which is the this
6908 pointer) as artificial. We obtain this information from
6909 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6910 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6911 fnp->voffset = VOFFSET_STATIC;
6912 }
6913 else
e2e0b3e5 6914 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6915 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6916
6917 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6918 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6919 fnp->fcontext = die_containing_type (die, cu);
c906108c 6920
3e43a32a
MS
6921 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6922 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6923
6924 /* Get accessibility. */
e142c38c 6925 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6926 if (attr)
60d5a603
JK
6927 accessibility = DW_UNSND (attr);
6928 else
6929 accessibility = dwarf2_default_access_attribute (die, cu);
6930 switch (accessibility)
c906108c 6931 {
60d5a603
JK
6932 case DW_ACCESS_private:
6933 fnp->is_private = 1;
6934 break;
6935 case DW_ACCESS_protected:
6936 fnp->is_protected = 1;
6937 break;
c906108c
SS
6938 }
6939
b02dede2 6940 /* Check for artificial methods. */
e142c38c 6941 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6942 if (attr && DW_UNSND (attr) != 0)
6943 fnp->is_artificial = 1;
6944
0d564a31 6945 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6946 function. For older versions of GCC, this is an offset in the
6947 appropriate virtual table, as specified by DW_AT_containing_type.
6948 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6949 to the object address. */
6950
e142c38c 6951 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6952 if (attr)
8e19ed76 6953 {
aec5aa8b 6954 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6955 {
aec5aa8b
TT
6956 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6957 {
6958 /* Old-style GCC. */
6959 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6960 }
6961 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6962 || (DW_BLOCK (attr)->size > 1
6963 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6964 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6965 {
6966 struct dwarf_block blk;
6967 int offset;
6968
6969 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6970 ? 1 : 2);
6971 blk.size = DW_BLOCK (attr)->size - offset;
6972 blk.data = DW_BLOCK (attr)->data + offset;
6973 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6974 if ((fnp->voffset % cu->header.addr_size) != 0)
6975 dwarf2_complex_location_expr_complaint ();
6976 else
6977 fnp->voffset /= cu->header.addr_size;
6978 fnp->voffset += 2;
6979 }
6980 else
6981 dwarf2_complex_location_expr_complaint ();
6982
6983 if (!fnp->fcontext)
6984 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6985 }
3690dd37 6986 else if (attr_form_is_section_offset (attr))
8e19ed76 6987 {
4d3c2250 6988 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6989 }
6990 else
6991 {
4d3c2250
KB
6992 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6993 fieldname);
8e19ed76 6994 }
0d564a31 6995 }
d48cc9dd
DJ
6996 else
6997 {
6998 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6999 if (attr && DW_UNSND (attr))
7000 {
7001 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7002 complaint (&symfile_complaints,
3e43a32a
MS
7003 _("Member function \"%s\" (offset %d) is virtual "
7004 "but the vtable offset is not specified"),
d48cc9dd 7005 fieldname, die->offset);
9655fd1a 7006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7007 TYPE_CPLUS_DYNAMIC (type) = 1;
7008 }
7009 }
c906108c
SS
7010}
7011
7012/* Create the vector of member function fields, and attach it to the type. */
7013
7014static void
fba45db2 7015dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7016 struct dwarf2_cu *cu)
c906108c
SS
7017{
7018 struct fnfieldlist *flp;
7019 int total_length = 0;
7020 int i;
7021
b4ba55a1 7022 if (cu->language == language_ada)
a73c6dcd 7023 error (_("unexpected member functions in Ada type"));
b4ba55a1 7024
c906108c
SS
7025 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7026 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7027 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7028
7029 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7030 {
7031 struct nextfnfield *nfp = flp->head;
7032 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7033 int k;
7034
7035 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7036 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7037 fn_flp->fn_fields = (struct fn_field *)
7038 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7039 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7040 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7041
7042 total_length += flp->length;
7043 }
7044
7045 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7046 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7047}
7048
1168df01
JB
7049/* Returns non-zero if NAME is the name of a vtable member in CU's
7050 language, zero otherwise. */
7051static int
7052is_vtable_name (const char *name, struct dwarf2_cu *cu)
7053{
7054 static const char vptr[] = "_vptr";
987504bb 7055 static const char vtable[] = "vtable";
1168df01 7056
987504bb
JJ
7057 /* Look for the C++ and Java forms of the vtable. */
7058 if ((cu->language == language_java
7059 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7060 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7061 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7062 return 1;
7063
7064 return 0;
7065}
7066
c0dd20ea 7067/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7068 functions, with the ABI-specified layout. If TYPE describes
7069 such a structure, smash it into a member function type.
61049d3b
DJ
7070
7071 GCC shouldn't do this; it should just output pointer to member DIEs.
7072 This is GCC PR debug/28767. */
c0dd20ea 7073
0b92b5bb
TT
7074static void
7075quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7076{
0b92b5bb 7077 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7078
7079 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7080 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7081 return;
c0dd20ea
DJ
7082
7083 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7084 if (TYPE_FIELD_NAME (type, 0) == NULL
7085 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7086 || TYPE_FIELD_NAME (type, 1) == NULL
7087 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7088 return;
c0dd20ea
DJ
7089
7090 /* Find the type of the method. */
0b92b5bb 7091 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7092 if (pfn_type == NULL
7093 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7094 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7095 return;
c0dd20ea
DJ
7096
7097 /* Look for the "this" argument. */
7098 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7099 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7100 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7101 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7102 return;
c0dd20ea
DJ
7103
7104 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7105 new_type = alloc_type (objfile);
7106 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7107 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7108 TYPE_VARARGS (pfn_type));
0b92b5bb 7109 smash_to_methodptr_type (type, new_type);
c0dd20ea 7110}
1168df01 7111
c906108c 7112/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7113 (definition) to create a type for the structure or union. Fill in
7114 the type's name and general properties; the members will not be
7115 processed until process_structure_type.
c906108c 7116
c767944b
DJ
7117 NOTE: we need to call these functions regardless of whether or not the
7118 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7119 structure or union. This gets the type entered into our set of
7120 user defined types.
7121
7122 However, if the structure is incomplete (an opaque struct/union)
7123 then suppress creating a symbol table entry for it since gdb only
7124 wants to find the one with the complete definition. Note that if
7125 it is complete, we just call new_symbol, which does it's own
7126 checking about whether the struct/union is anonymous or not (and
7127 suppresses creating a symbol table entry itself). */
7128
f792889a 7129static struct type *
134d01f1 7130read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7131{
e7c27a73 7132 struct objfile *objfile = cu->objfile;
c906108c
SS
7133 struct type *type;
7134 struct attribute *attr;
39cbfefa 7135 char *name;
c906108c 7136
348e048f
DE
7137 /* If the definition of this type lives in .debug_types, read that type.
7138 Don't follow DW_AT_specification though, that will take us back up
7139 the chain and we want to go down. */
7140 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7141 if (attr)
7142 {
7143 struct dwarf2_cu *type_cu = cu;
7144 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7145
348e048f
DE
7146 /* We could just recurse on read_structure_type, but we need to call
7147 get_die_type to ensure only one type for this DIE is created.
7148 This is important, for example, because for c++ classes we need
7149 TYPE_NAME set which is only done by new_symbol. Blech. */
7150 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7151
7152 /* TYPE_CU may not be the same as CU.
7153 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7154 return set_die_type (die, type, cu);
7155 }
7156
c0dd20ea 7157 type = alloc_type (objfile);
c906108c 7158 INIT_CPLUS_SPECIFIC (type);
93311388 7159
39cbfefa
DJ
7160 name = dwarf2_name (die, cu);
7161 if (name != NULL)
c906108c 7162 {
987504bb
JJ
7163 if (cu->language == language_cplus
7164 || cu->language == language_java)
63d06c5c 7165 {
3da10d80
KS
7166 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7167
7168 /* dwarf2_full_name might have already finished building the DIE's
7169 type. If so, there is no need to continue. */
7170 if (get_die_type (die, cu) != NULL)
7171 return get_die_type (die, cu);
7172
7173 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7174 if (die->tag == DW_TAG_structure_type
7175 || die->tag == DW_TAG_class_type)
7176 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7177 }
7178 else
7179 {
d8151005
DJ
7180 /* The name is already allocated along with this objfile, so
7181 we don't need to duplicate it for the type. */
94af9270
KS
7182 TYPE_TAG_NAME (type) = (char *) name;
7183 if (die->tag == DW_TAG_class_type)
7184 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7185 }
c906108c
SS
7186 }
7187
7188 if (die->tag == DW_TAG_structure_type)
7189 {
7190 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7191 }
7192 else if (die->tag == DW_TAG_union_type)
7193 {
7194 TYPE_CODE (type) = TYPE_CODE_UNION;
7195 }
7196 else
7197 {
c906108c
SS
7198 TYPE_CODE (type) = TYPE_CODE_CLASS;
7199 }
7200
0cc2414c
TT
7201 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7202 TYPE_DECLARED_CLASS (type) = 1;
7203
e142c38c 7204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7205 if (attr)
7206 {
7207 TYPE_LENGTH (type) = DW_UNSND (attr);
7208 }
7209 else
7210 {
7211 TYPE_LENGTH (type) = 0;
7212 }
7213
876cecd0 7214 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7215 if (die_is_declaration (die, cu))
876cecd0 7216 TYPE_STUB (type) = 1;
a6c727b2
DJ
7217 else if (attr == NULL && die->child == NULL
7218 && producer_is_realview (cu->producer))
7219 /* RealView does not output the required DW_AT_declaration
7220 on incomplete types. */
7221 TYPE_STUB (type) = 1;
dc718098 7222
c906108c
SS
7223 /* We need to add the type field to the die immediately so we don't
7224 infinitely recurse when dealing with pointers to the structure
0963b4bd 7225 type within the structure itself. */
1c379e20 7226 set_die_type (die, type, cu);
c906108c 7227
7e314c57
JK
7228 /* set_die_type should be already done. */
7229 set_descriptive_type (type, die, cu);
7230
c767944b
DJ
7231 return type;
7232}
7233
7234/* Finish creating a structure or union type, including filling in
7235 its members and creating a symbol for it. */
7236
7237static void
7238process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7239{
7240 struct objfile *objfile = cu->objfile;
7241 struct die_info *child_die = die->child;
7242 struct type *type;
7243
7244 type = get_die_type (die, cu);
7245 if (type == NULL)
7246 type = read_structure_type (die, cu);
7247
e142c38c 7248 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7249 {
7250 struct field_info fi;
7251 struct die_info *child_die;
34eaf542 7252 VEC (symbolp) *template_args = NULL;
c767944b 7253 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7254
7255 memset (&fi, 0, sizeof (struct field_info));
7256
639d11d3 7257 child_die = die->child;
c906108c
SS
7258
7259 while (child_die && child_die->tag)
7260 {
a9a9bd0f
DC
7261 if (child_die->tag == DW_TAG_member
7262 || child_die->tag == DW_TAG_variable)
c906108c 7263 {
a9a9bd0f
DC
7264 /* NOTE: carlton/2002-11-05: A C++ static data member
7265 should be a DW_TAG_member that is a declaration, but
7266 all versions of G++ as of this writing (so through at
7267 least 3.2.1) incorrectly generate DW_TAG_variable
7268 tags for them instead. */
e7c27a73 7269 dwarf2_add_field (&fi, child_die, cu);
c906108c 7270 }
8713b1b1 7271 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7272 {
0963b4bd 7273 /* C++ member function. */
e7c27a73 7274 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7275 }
7276 else if (child_die->tag == DW_TAG_inheritance)
7277 {
7278 /* C++ base class field. */
e7c27a73 7279 dwarf2_add_field (&fi, child_die, cu);
c906108c 7280 }
98751a41
JK
7281 else if (child_die->tag == DW_TAG_typedef)
7282 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7283 else if (child_die->tag == DW_TAG_template_type_param
7284 || child_die->tag == DW_TAG_template_value_param)
7285 {
7286 struct symbol *arg = new_symbol (child_die, NULL, cu);
7287
f1078f66
DJ
7288 if (arg != NULL)
7289 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7290 }
7291
c906108c
SS
7292 child_die = sibling_die (child_die);
7293 }
7294
34eaf542
TT
7295 /* Attach template arguments to type. */
7296 if (! VEC_empty (symbolp, template_args))
7297 {
7298 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7299 TYPE_N_TEMPLATE_ARGUMENTS (type)
7300 = VEC_length (symbolp, template_args);
7301 TYPE_TEMPLATE_ARGUMENTS (type)
7302 = obstack_alloc (&objfile->objfile_obstack,
7303 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7304 * sizeof (struct symbol *)));
7305 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7306 VEC_address (symbolp, template_args),
7307 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7308 * sizeof (struct symbol *)));
7309 VEC_free (symbolp, template_args);
7310 }
7311
c906108c
SS
7312 /* Attach fields and member functions to the type. */
7313 if (fi.nfields)
e7c27a73 7314 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7315 if (fi.nfnfields)
7316 {
e7c27a73 7317 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7318
c5aa993b 7319 /* Get the type which refers to the base class (possibly this
c906108c 7320 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7321 class from the DW_AT_containing_type attribute. This use of
7322 DW_AT_containing_type is a GNU extension. */
c906108c 7323
e142c38c 7324 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7325 {
e7c27a73 7326 struct type *t = die_containing_type (die, cu);
c906108c
SS
7327
7328 TYPE_VPTR_BASETYPE (type) = t;
7329 if (type == t)
7330 {
c906108c
SS
7331 int i;
7332
7333 /* Our own class provides vtbl ptr. */
7334 for (i = TYPE_NFIELDS (t) - 1;
7335 i >= TYPE_N_BASECLASSES (t);
7336 --i)
7337 {
7338 char *fieldname = TYPE_FIELD_NAME (t, i);
7339
1168df01 7340 if (is_vtable_name (fieldname, cu))
c906108c
SS
7341 {
7342 TYPE_VPTR_FIELDNO (type) = i;
7343 break;
7344 }
7345 }
7346
7347 /* Complain if virtual function table field not found. */
7348 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7349 complaint (&symfile_complaints,
3e43a32a
MS
7350 _("virtual function table pointer "
7351 "not found when defining class '%s'"),
4d3c2250
KB
7352 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7353 "");
c906108c
SS
7354 }
7355 else
7356 {
7357 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7358 }
7359 }
f6235d4c
EZ
7360 else if (cu->producer
7361 && strncmp (cu->producer,
7362 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7363 {
7364 /* The IBM XLC compiler does not provide direct indication
7365 of the containing type, but the vtable pointer is
7366 always named __vfp. */
7367
7368 int i;
7369
7370 for (i = TYPE_NFIELDS (type) - 1;
7371 i >= TYPE_N_BASECLASSES (type);
7372 --i)
7373 {
7374 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7375 {
7376 TYPE_VPTR_FIELDNO (type) = i;
7377 TYPE_VPTR_BASETYPE (type) = type;
7378 break;
7379 }
7380 }
7381 }
c906108c 7382 }
98751a41
JK
7383
7384 /* Copy fi.typedef_field_list linked list elements content into the
7385 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7386 if (fi.typedef_field_list)
7387 {
7388 int i = fi.typedef_field_list_count;
7389
a0d7a4ff 7390 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7391 TYPE_TYPEDEF_FIELD_ARRAY (type)
7392 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7393 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7394
7395 /* Reverse the list order to keep the debug info elements order. */
7396 while (--i >= 0)
7397 {
7398 struct typedef_field *dest, *src;
6e70227d 7399
98751a41
JK
7400 dest = &TYPE_TYPEDEF_FIELD (type, i);
7401 src = &fi.typedef_field_list->field;
7402 fi.typedef_field_list = fi.typedef_field_list->next;
7403 *dest = *src;
7404 }
7405 }
c767944b
DJ
7406
7407 do_cleanups (back_to);
c906108c 7408 }
63d06c5c 7409
0b92b5bb
TT
7410 quirk_gcc_member_function_pointer (type, cu->objfile);
7411
90aeadfc
DC
7412 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7413 snapshots) has been known to create a die giving a declaration
7414 for a class that has, as a child, a die giving a definition for a
7415 nested class. So we have to process our children even if the
7416 current die is a declaration. Normally, of course, a declaration
7417 won't have any children at all. */
134d01f1 7418
90aeadfc
DC
7419 while (child_die != NULL && child_die->tag)
7420 {
7421 if (child_die->tag == DW_TAG_member
7422 || child_die->tag == DW_TAG_variable
34eaf542
TT
7423 || child_die->tag == DW_TAG_inheritance
7424 || child_die->tag == DW_TAG_template_value_param
7425 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7426 {
90aeadfc 7427 /* Do nothing. */
134d01f1 7428 }
90aeadfc
DC
7429 else
7430 process_die (child_die, cu);
134d01f1 7431
90aeadfc 7432 child_die = sibling_die (child_die);
134d01f1
DJ
7433 }
7434
fa4028e9
JB
7435 /* Do not consider external references. According to the DWARF standard,
7436 these DIEs are identified by the fact that they have no byte_size
7437 attribute, and a declaration attribute. */
7438 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7439 || !die_is_declaration (die, cu))
c767944b 7440 new_symbol (die, type, cu);
134d01f1
DJ
7441}
7442
7443/* Given a DW_AT_enumeration_type die, set its type. We do not
7444 complete the type's fields yet, or create any symbols. */
c906108c 7445
f792889a 7446static struct type *
134d01f1 7447read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7448{
e7c27a73 7449 struct objfile *objfile = cu->objfile;
c906108c 7450 struct type *type;
c906108c 7451 struct attribute *attr;
0114d602 7452 const char *name;
134d01f1 7453
348e048f
DE
7454 /* If the definition of this type lives in .debug_types, read that type.
7455 Don't follow DW_AT_specification though, that will take us back up
7456 the chain and we want to go down. */
7457 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7458 if (attr)
7459 {
7460 struct dwarf2_cu *type_cu = cu;
7461 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7462
348e048f 7463 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7464
7465 /* TYPE_CU may not be the same as CU.
7466 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7467 return set_die_type (die, type, cu);
7468 }
7469
c906108c
SS
7470 type = alloc_type (objfile);
7471
7472 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7473 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7474 if (name != NULL)
0114d602 7475 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7476
e142c38c 7477 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7478 if (attr)
7479 {
7480 TYPE_LENGTH (type) = DW_UNSND (attr);
7481 }
7482 else
7483 {
7484 TYPE_LENGTH (type) = 0;
7485 }
7486
137033e9
JB
7487 /* The enumeration DIE can be incomplete. In Ada, any type can be
7488 declared as private in the package spec, and then defined only
7489 inside the package body. Such types are known as Taft Amendment
7490 Types. When another package uses such a type, an incomplete DIE
7491 may be generated by the compiler. */
02eb380e 7492 if (die_is_declaration (die, cu))
876cecd0 7493 TYPE_STUB (type) = 1;
02eb380e 7494
f792889a 7495 return set_die_type (die, type, cu);
134d01f1
DJ
7496}
7497
7498/* Given a pointer to a die which begins an enumeration, process all
7499 the dies that define the members of the enumeration, and create the
7500 symbol for the enumeration type.
7501
7502 NOTE: We reverse the order of the element list. */
7503
7504static void
7505process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7506{
f792889a 7507 struct type *this_type;
134d01f1 7508
f792889a
DJ
7509 this_type = get_die_type (die, cu);
7510 if (this_type == NULL)
7511 this_type = read_enumeration_type (die, cu);
9dc481d3 7512
639d11d3 7513 if (die->child != NULL)
c906108c 7514 {
9dc481d3
DE
7515 struct die_info *child_die;
7516 struct symbol *sym;
7517 struct field *fields = NULL;
7518 int num_fields = 0;
7519 int unsigned_enum = 1;
7520 char *name;
7521
639d11d3 7522 child_die = die->child;
c906108c
SS
7523 while (child_die && child_die->tag)
7524 {
7525 if (child_die->tag != DW_TAG_enumerator)
7526 {
e7c27a73 7527 process_die (child_die, cu);
c906108c
SS
7528 }
7529 else
7530 {
39cbfefa
DJ
7531 name = dwarf2_name (child_die, cu);
7532 if (name)
c906108c 7533 {
f792889a 7534 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7535 if (SYMBOL_VALUE (sym) < 0)
7536 unsigned_enum = 0;
7537
7538 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7539 {
7540 fields = (struct field *)
7541 xrealloc (fields,
7542 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7543 * sizeof (struct field));
c906108c
SS
7544 }
7545
3567439c 7546 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7547 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7548 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7549 FIELD_BITSIZE (fields[num_fields]) = 0;
7550
7551 num_fields++;
7552 }
7553 }
7554
7555 child_die = sibling_die (child_die);
7556 }
7557
7558 if (num_fields)
7559 {
f792889a
DJ
7560 TYPE_NFIELDS (this_type) = num_fields;
7561 TYPE_FIELDS (this_type) = (struct field *)
7562 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7563 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7564 sizeof (struct field) * num_fields);
b8c9b27d 7565 xfree (fields);
c906108c
SS
7566 }
7567 if (unsigned_enum)
876cecd0 7568 TYPE_UNSIGNED (this_type) = 1;
c906108c 7569 }
134d01f1 7570
f792889a 7571 new_symbol (die, this_type, cu);
c906108c
SS
7572}
7573
7574/* Extract all information from a DW_TAG_array_type DIE and put it in
7575 the DIE's type field. For now, this only handles one dimensional
7576 arrays. */
7577
f792889a 7578static struct type *
e7c27a73 7579read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7580{
e7c27a73 7581 struct objfile *objfile = cu->objfile;
c906108c 7582 struct die_info *child_die;
7e314c57 7583 struct type *type;
c906108c
SS
7584 struct type *element_type, *range_type, *index_type;
7585 struct type **range_types = NULL;
7586 struct attribute *attr;
7587 int ndim = 0;
7588 struct cleanup *back_to;
39cbfefa 7589 char *name;
c906108c 7590
e7c27a73 7591 element_type = die_type (die, cu);
c906108c 7592
7e314c57
JK
7593 /* The die_type call above may have already set the type for this DIE. */
7594 type = get_die_type (die, cu);
7595 if (type)
7596 return type;
7597
c906108c
SS
7598 /* Irix 6.2 native cc creates array types without children for
7599 arrays with unspecified length. */
639d11d3 7600 if (die->child == NULL)
c906108c 7601 {
46bf5051 7602 index_type = objfile_type (objfile)->builtin_int;
c906108c 7603 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7604 type = create_array_type (NULL, element_type, range_type);
7605 return set_die_type (die, type, cu);
c906108c
SS
7606 }
7607
7608 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7609 child_die = die->child;
c906108c
SS
7610 while (child_die && child_die->tag)
7611 {
7612 if (child_die->tag == DW_TAG_subrange_type)
7613 {
f792889a 7614 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7615
f792889a 7616 if (child_type != NULL)
a02abb62 7617 {
0963b4bd
MS
7618 /* The range type was succesfully read. Save it for the
7619 array type creation. */
a02abb62
JB
7620 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7621 {
7622 range_types = (struct type **)
7623 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7624 * sizeof (struct type *));
7625 if (ndim == 0)
7626 make_cleanup (free_current_contents, &range_types);
7627 }
f792889a 7628 range_types[ndim++] = child_type;
a02abb62 7629 }
c906108c
SS
7630 }
7631 child_die = sibling_die (child_die);
7632 }
7633
7634 /* Dwarf2 dimensions are output from left to right, create the
7635 necessary array types in backwards order. */
7ca2d3a3 7636
c906108c 7637 type = element_type;
7ca2d3a3
DL
7638
7639 if (read_array_order (die, cu) == DW_ORD_col_major)
7640 {
7641 int i = 0;
9a619af0 7642
7ca2d3a3
DL
7643 while (i < ndim)
7644 type = create_array_type (NULL, type, range_types[i++]);
7645 }
7646 else
7647 {
7648 while (ndim-- > 0)
7649 type = create_array_type (NULL, type, range_types[ndim]);
7650 }
c906108c 7651
f5f8a009
EZ
7652 /* Understand Dwarf2 support for vector types (like they occur on
7653 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7654 array type. This is not part of the Dwarf2/3 standard yet, but a
7655 custom vendor extension. The main difference between a regular
7656 array and the vector variant is that vectors are passed by value
7657 to functions. */
e142c38c 7658 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7659 if (attr)
ea37ba09 7660 make_vector_type (type);
f5f8a009 7661
dbc98a8b
KW
7662 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7663 implementation may choose to implement triple vectors using this
7664 attribute. */
7665 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7666 if (attr)
7667 {
7668 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7669 TYPE_LENGTH (type) = DW_UNSND (attr);
7670 else
3e43a32a
MS
7671 complaint (&symfile_complaints,
7672 _("DW_AT_byte_size for array type smaller "
7673 "than the total size of elements"));
dbc98a8b
KW
7674 }
7675
39cbfefa
DJ
7676 name = dwarf2_name (die, cu);
7677 if (name)
7678 TYPE_NAME (type) = name;
6e70227d 7679
0963b4bd 7680 /* Install the type in the die. */
7e314c57
JK
7681 set_die_type (die, type, cu);
7682
7683 /* set_die_type should be already done. */
b4ba55a1
JB
7684 set_descriptive_type (type, die, cu);
7685
c906108c
SS
7686 do_cleanups (back_to);
7687
7e314c57 7688 return type;
c906108c
SS
7689}
7690
7ca2d3a3 7691static enum dwarf_array_dim_ordering
6e70227d 7692read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7693{
7694 struct attribute *attr;
7695
7696 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7697
7698 if (attr) return DW_SND (attr);
7699
0963b4bd
MS
7700 /* GNU F77 is a special case, as at 08/2004 array type info is the
7701 opposite order to the dwarf2 specification, but data is still
7702 laid out as per normal fortran.
7ca2d3a3 7703
0963b4bd
MS
7704 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7705 version checking. */
7ca2d3a3 7706
905e0470
PM
7707 if (cu->language == language_fortran
7708 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7709 {
7710 return DW_ORD_row_major;
7711 }
7712
6e70227d 7713 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7714 {
7715 case array_column_major:
7716 return DW_ORD_col_major;
7717 case array_row_major:
7718 default:
7719 return DW_ORD_row_major;
7720 };
7721}
7722
72019c9c 7723/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7724 the DIE's type field. */
72019c9c 7725
f792889a 7726static struct type *
72019c9c
GM
7727read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7728{
7e314c57
JK
7729 struct type *domain_type, *set_type;
7730 struct attribute *attr;
f792889a 7731
7e314c57
JK
7732 domain_type = die_type (die, cu);
7733
7734 /* The die_type call above may have already set the type for this DIE. */
7735 set_type = get_die_type (die, cu);
7736 if (set_type)
7737 return set_type;
7738
7739 set_type = create_set_type (NULL, domain_type);
7740
7741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7742 if (attr)
7743 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7744
f792889a 7745 return set_die_type (die, set_type, cu);
72019c9c 7746}
7ca2d3a3 7747
c906108c
SS
7748/* First cut: install each common block member as a global variable. */
7749
7750static void
e7c27a73 7751read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7752{
7753 struct die_info *child_die;
7754 struct attribute *attr;
7755 struct symbol *sym;
7756 CORE_ADDR base = (CORE_ADDR) 0;
7757
e142c38c 7758 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7759 if (attr)
7760 {
0963b4bd 7761 /* Support the .debug_loc offsets. */
8e19ed76
PS
7762 if (attr_form_is_block (attr))
7763 {
e7c27a73 7764 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7765 }
3690dd37 7766 else if (attr_form_is_section_offset (attr))
8e19ed76 7767 {
4d3c2250 7768 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7769 }
7770 else
7771 {
4d3c2250
KB
7772 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7773 "common block member");
8e19ed76 7774 }
c906108c 7775 }
639d11d3 7776 if (die->child != NULL)
c906108c 7777 {
639d11d3 7778 child_die = die->child;
c906108c
SS
7779 while (child_die && child_die->tag)
7780 {
74ac6d43
TT
7781 LONGEST offset;
7782
e7c27a73 7783 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7784 if (sym != NULL
7785 && handle_data_member_location (child_die, cu, &offset))
c906108c 7786 {
74ac6d43 7787 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
7788 add_symbol_to_list (sym, &global_symbols);
7789 }
7790 child_die = sibling_die (child_die);
7791 }
7792 }
7793}
7794
0114d602 7795/* Create a type for a C++ namespace. */
d9fa45fe 7796
0114d602
DJ
7797static struct type *
7798read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7799{
e7c27a73 7800 struct objfile *objfile = cu->objfile;
0114d602 7801 const char *previous_prefix, *name;
9219021c 7802 int is_anonymous;
0114d602
DJ
7803 struct type *type;
7804
7805 /* For extensions, reuse the type of the original namespace. */
7806 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7807 {
7808 struct die_info *ext_die;
7809 struct dwarf2_cu *ext_cu = cu;
9a619af0 7810
0114d602
DJ
7811 ext_die = dwarf2_extension (die, &ext_cu);
7812 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7813
7814 /* EXT_CU may not be the same as CU.
7815 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7816 return set_die_type (die, type, cu);
7817 }
9219021c 7818
e142c38c 7819 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7820
7821 /* Now build the name of the current namespace. */
7822
0114d602
DJ
7823 previous_prefix = determine_prefix (die, cu);
7824 if (previous_prefix[0] != '\0')
7825 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7826 previous_prefix, name, 0, cu);
0114d602
DJ
7827
7828 /* Create the type. */
7829 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7830 objfile);
7831 TYPE_NAME (type) = (char *) name;
7832 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7833
60531b24 7834 return set_die_type (die, type, cu);
0114d602
DJ
7835}
7836
7837/* Read a C++ namespace. */
7838
7839static void
7840read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7841{
7842 struct objfile *objfile = cu->objfile;
0114d602 7843 int is_anonymous;
9219021c 7844
5c4e30ca
DC
7845 /* Add a symbol associated to this if we haven't seen the namespace
7846 before. Also, add a using directive if it's an anonymous
7847 namespace. */
9219021c 7848
f2f0e013 7849 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7850 {
7851 struct type *type;
7852
0114d602 7853 type = read_type_die (die, cu);
e7c27a73 7854 new_symbol (die, type, cu);
5c4e30ca 7855
e8e80198 7856 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7857 if (is_anonymous)
0114d602
DJ
7858 {
7859 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7860
c0cc3a76 7861 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 7862 NULL, NULL, &objfile->objfile_obstack);
0114d602 7863 }
5c4e30ca 7864 }
9219021c 7865
639d11d3 7866 if (die->child != NULL)
d9fa45fe 7867 {
639d11d3 7868 struct die_info *child_die = die->child;
6e70227d 7869
d9fa45fe
DC
7870 while (child_die && child_die->tag)
7871 {
e7c27a73 7872 process_die (child_die, cu);
d9fa45fe
DC
7873 child_die = sibling_die (child_die);
7874 }
7875 }
38d518c9
EZ
7876}
7877
f55ee35c
JK
7878/* Read a Fortran module as type. This DIE can be only a declaration used for
7879 imported module. Still we need that type as local Fortran "use ... only"
7880 declaration imports depend on the created type in determine_prefix. */
7881
7882static struct type *
7883read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7884{
7885 struct objfile *objfile = cu->objfile;
7886 char *module_name;
7887 struct type *type;
7888
7889 module_name = dwarf2_name (die, cu);
7890 if (!module_name)
3e43a32a
MS
7891 complaint (&symfile_complaints,
7892 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7893 die->offset);
7894 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7895
7896 /* determine_prefix uses TYPE_TAG_NAME. */
7897 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7898
7899 return set_die_type (die, type, cu);
7900}
7901
5d7cb8df
JK
7902/* Read a Fortran module. */
7903
7904static void
7905read_module (struct die_info *die, struct dwarf2_cu *cu)
7906{
7907 struct die_info *child_die = die->child;
7908
5d7cb8df
JK
7909 while (child_die && child_die->tag)
7910 {
7911 process_die (child_die, cu);
7912 child_die = sibling_die (child_die);
7913 }
7914}
7915
38d518c9
EZ
7916/* Return the name of the namespace represented by DIE. Set
7917 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7918 namespace. */
7919
7920static const char *
e142c38c 7921namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7922{
7923 struct die_info *current_die;
7924 const char *name = NULL;
7925
7926 /* Loop through the extensions until we find a name. */
7927
7928 for (current_die = die;
7929 current_die != NULL;
f2f0e013 7930 current_die = dwarf2_extension (die, &cu))
38d518c9 7931 {
e142c38c 7932 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7933 if (name != NULL)
7934 break;
7935 }
7936
7937 /* Is it an anonymous namespace? */
7938
7939 *is_anonymous = (name == NULL);
7940 if (*is_anonymous)
2b1dbab0 7941 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
7942
7943 return name;
d9fa45fe
DC
7944}
7945
c906108c
SS
7946/* Extract all information from a DW_TAG_pointer_type DIE and add to
7947 the user defined type vector. */
7948
f792889a 7949static struct type *
e7c27a73 7950read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7951{
5e2b427d 7952 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7953 struct comp_unit_head *cu_header = &cu->header;
c906108c 7954 struct type *type;
8b2dbe47
KB
7955 struct attribute *attr_byte_size;
7956 struct attribute *attr_address_class;
7957 int byte_size, addr_class;
7e314c57
JK
7958 struct type *target_type;
7959
7960 target_type = die_type (die, cu);
c906108c 7961
7e314c57
JK
7962 /* The die_type call above may have already set the type for this DIE. */
7963 type = get_die_type (die, cu);
7964 if (type)
7965 return type;
7966
7967 type = lookup_pointer_type (target_type);
8b2dbe47 7968
e142c38c 7969 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7970 if (attr_byte_size)
7971 byte_size = DW_UNSND (attr_byte_size);
c906108c 7972 else
8b2dbe47
KB
7973 byte_size = cu_header->addr_size;
7974
e142c38c 7975 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7976 if (attr_address_class)
7977 addr_class = DW_UNSND (attr_address_class);
7978 else
7979 addr_class = DW_ADDR_none;
7980
7981 /* If the pointer size or address class is different than the
7982 default, create a type variant marked as such and set the
7983 length accordingly. */
7984 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7985 {
5e2b427d 7986 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7987 {
7988 int type_flags;
7989
849957d9 7990 type_flags = gdbarch_address_class_type_flags
5e2b427d 7991 (gdbarch, byte_size, addr_class);
876cecd0
TT
7992 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7993 == 0);
8b2dbe47
KB
7994 type = make_type_with_address_space (type, type_flags);
7995 }
7996 else if (TYPE_LENGTH (type) != byte_size)
7997 {
3e43a32a
MS
7998 complaint (&symfile_complaints,
7999 _("invalid pointer size %d"), byte_size);
8b2dbe47 8000 }
6e70227d 8001 else
9a619af0
MS
8002 {
8003 /* Should we also complain about unhandled address classes? */
8004 }
c906108c 8005 }
8b2dbe47
KB
8006
8007 TYPE_LENGTH (type) = byte_size;
f792889a 8008 return set_die_type (die, type, cu);
c906108c
SS
8009}
8010
8011/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8012 the user defined type vector. */
8013
f792889a 8014static struct type *
e7c27a73 8015read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8016{
8017 struct type *type;
8018 struct type *to_type;
8019 struct type *domain;
8020
e7c27a73
DJ
8021 to_type = die_type (die, cu);
8022 domain = die_containing_type (die, cu);
0d5de010 8023
7e314c57
JK
8024 /* The calls above may have already set the type for this DIE. */
8025 type = get_die_type (die, cu);
8026 if (type)
8027 return type;
8028
0d5de010
DJ
8029 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8030 type = lookup_methodptr_type (to_type);
8031 else
8032 type = lookup_memberptr_type (to_type, domain);
c906108c 8033
f792889a 8034 return set_die_type (die, type, cu);
c906108c
SS
8035}
8036
8037/* Extract all information from a DW_TAG_reference_type DIE and add to
8038 the user defined type vector. */
8039
f792889a 8040static struct type *
e7c27a73 8041read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8042{
e7c27a73 8043 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8044 struct type *type, *target_type;
c906108c
SS
8045 struct attribute *attr;
8046
7e314c57
JK
8047 target_type = die_type (die, cu);
8048
8049 /* The die_type call above may have already set the type for this DIE. */
8050 type = get_die_type (die, cu);
8051 if (type)
8052 return type;
8053
8054 type = lookup_reference_type (target_type);
e142c38c 8055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8056 if (attr)
8057 {
8058 TYPE_LENGTH (type) = DW_UNSND (attr);
8059 }
8060 else
8061 {
107d2387 8062 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8063 }
f792889a 8064 return set_die_type (die, type, cu);
c906108c
SS
8065}
8066
f792889a 8067static struct type *
e7c27a73 8068read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8069{
f792889a 8070 struct type *base_type, *cv_type;
c906108c 8071
e7c27a73 8072 base_type = die_type (die, cu);
7e314c57
JK
8073
8074 /* The die_type call above may have already set the type for this DIE. */
8075 cv_type = get_die_type (die, cu);
8076 if (cv_type)
8077 return cv_type;
8078
2f608a3a
KW
8079 /* In case the const qualifier is applied to an array type, the element type
8080 is so qualified, not the array type (section 6.7.3 of C99). */
8081 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8082 {
8083 struct type *el_type, *inner_array;
8084
8085 base_type = copy_type (base_type);
8086 inner_array = base_type;
8087
8088 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8089 {
8090 TYPE_TARGET_TYPE (inner_array) =
8091 copy_type (TYPE_TARGET_TYPE (inner_array));
8092 inner_array = TYPE_TARGET_TYPE (inner_array);
8093 }
8094
8095 el_type = TYPE_TARGET_TYPE (inner_array);
8096 TYPE_TARGET_TYPE (inner_array) =
8097 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8098
8099 return set_die_type (die, base_type, cu);
8100 }
8101
f792889a
DJ
8102 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8103 return set_die_type (die, cv_type, cu);
c906108c
SS
8104}
8105
f792889a 8106static struct type *
e7c27a73 8107read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8108{
f792889a 8109 struct type *base_type, *cv_type;
c906108c 8110
e7c27a73 8111 base_type = die_type (die, cu);
7e314c57
JK
8112
8113 /* The die_type call above may have already set the type for this DIE. */
8114 cv_type = get_die_type (die, cu);
8115 if (cv_type)
8116 return cv_type;
8117
f792889a
DJ
8118 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8119 return set_die_type (die, cv_type, cu);
c906108c
SS
8120}
8121
8122/* Extract all information from a DW_TAG_string_type DIE and add to
8123 the user defined type vector. It isn't really a user defined type,
8124 but it behaves like one, with other DIE's using an AT_user_def_type
8125 attribute to reference it. */
8126
f792889a 8127static struct type *
e7c27a73 8128read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8129{
e7c27a73 8130 struct objfile *objfile = cu->objfile;
3b7538c0 8131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8132 struct type *type, *range_type, *index_type, *char_type;
8133 struct attribute *attr;
8134 unsigned int length;
8135
e142c38c 8136 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8137 if (attr)
8138 {
8139 length = DW_UNSND (attr);
8140 }
8141 else
8142 {
0963b4bd 8143 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8144 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8145 if (attr)
8146 {
8147 length = DW_UNSND (attr);
8148 }
8149 else
8150 {
8151 length = 1;
8152 }
c906108c 8153 }
6ccb9162 8154
46bf5051 8155 index_type = objfile_type (objfile)->builtin_int;
c906108c 8156 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8157 char_type = language_string_char_type (cu->language_defn, gdbarch);
8158 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8159
f792889a 8160 return set_die_type (die, type, cu);
c906108c
SS
8161}
8162
8163/* Handle DIES due to C code like:
8164
8165 struct foo
c5aa993b
JM
8166 {
8167 int (*funcp)(int a, long l);
8168 int b;
8169 };
c906108c 8170
0963b4bd 8171 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8172
f792889a 8173static struct type *
e7c27a73 8174read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8175{
0963b4bd
MS
8176 struct type *type; /* Type that this function returns. */
8177 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8178 struct attribute *attr;
8179
e7c27a73 8180 type = die_type (die, cu);
7e314c57
JK
8181
8182 /* The die_type call above may have already set the type for this DIE. */
8183 ftype = get_die_type (die, cu);
8184 if (ftype)
8185 return ftype;
8186
0c8b41f1 8187 ftype = lookup_function_type (type);
c906108c 8188
5b8101ae 8189 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8190 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8191 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8192 || cu->language == language_cplus
5b8101ae
PM
8193 || cu->language == language_java
8194 || cu->language == language_pascal)
876cecd0 8195 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8196 else if (producer_is_realview (cu->producer))
8197 /* RealView does not emit DW_AT_prototyped. We can not
8198 distinguish prototyped and unprototyped functions; default to
8199 prototyped, since that is more common in modern code (and
8200 RealView warns about unprototyped functions). */
8201 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8202
c055b101
CV
8203 /* Store the calling convention in the type if it's available in
8204 the subroutine die. Otherwise set the calling convention to
8205 the default value DW_CC_normal. */
8206 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8207 if (attr)
8208 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8209 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8210 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8211 else
8212 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8213
8214 /* We need to add the subroutine type to the die immediately so
8215 we don't infinitely recurse when dealing with parameters
0963b4bd 8216 declared as the same subroutine type. */
76c10ea2 8217 set_die_type (die, ftype, cu);
6e70227d 8218
639d11d3 8219 if (die->child != NULL)
c906108c 8220 {
8072405b 8221 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8222 struct die_info *child_die;
8072405b 8223 int nparams, iparams;
c906108c
SS
8224
8225 /* Count the number of parameters.
8226 FIXME: GDB currently ignores vararg functions, but knows about
8227 vararg member functions. */
8072405b 8228 nparams = 0;
639d11d3 8229 child_die = die->child;
c906108c
SS
8230 while (child_die && child_die->tag)
8231 {
8232 if (child_die->tag == DW_TAG_formal_parameter)
8233 nparams++;
8234 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8235 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8236 child_die = sibling_die (child_die);
8237 }
8238
8239 /* Allocate storage for parameters and fill them in. */
8240 TYPE_NFIELDS (ftype) = nparams;
8241 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8242 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8243
8072405b
JK
8244 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8245 even if we error out during the parameters reading below. */
8246 for (iparams = 0; iparams < nparams; iparams++)
8247 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8248
8249 iparams = 0;
639d11d3 8250 child_die = die->child;
c906108c
SS
8251 while (child_die && child_die->tag)
8252 {
8253 if (child_die->tag == DW_TAG_formal_parameter)
8254 {
3ce3b1ba
PA
8255 struct type *arg_type;
8256
8257 /* DWARF version 2 has no clean way to discern C++
8258 static and non-static member functions. G++ helps
8259 GDB by marking the first parameter for non-static
8260 member functions (which is the this pointer) as
8261 artificial. We pass this information to
8262 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8263
8264 DWARF version 3 added DW_AT_object_pointer, which GCC
8265 4.5 does not yet generate. */
e142c38c 8266 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8267 if (attr)
8268 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8269 else
418835cc
KS
8270 {
8271 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8272
8273 /* GCC/43521: In java, the formal parameter
8274 "this" is sometimes not marked with DW_AT_artificial. */
8275 if (cu->language == language_java)
8276 {
8277 const char *name = dwarf2_name (child_die, cu);
9a619af0 8278
418835cc
KS
8279 if (name && !strcmp (name, "this"))
8280 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8281 }
8282 }
3ce3b1ba
PA
8283 arg_type = die_type (child_die, cu);
8284
8285 /* RealView does not mark THIS as const, which the testsuite
8286 expects. GCC marks THIS as const in method definitions,
8287 but not in the class specifications (GCC PR 43053). */
8288 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8289 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8290 {
8291 int is_this = 0;
8292 struct dwarf2_cu *arg_cu = cu;
8293 const char *name = dwarf2_name (child_die, cu);
8294
8295 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8296 if (attr)
8297 {
8298 /* If the compiler emits this, use it. */
8299 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8300 is_this = 1;
8301 }
8302 else if (name && strcmp (name, "this") == 0)
8303 /* Function definitions will have the argument names. */
8304 is_this = 1;
8305 else if (name == NULL && iparams == 0)
8306 /* Declarations may not have the names, so like
8307 elsewhere in GDB, assume an artificial first
8308 argument is "this". */
8309 is_this = 1;
8310
8311 if (is_this)
8312 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8313 arg_type, 0);
8314 }
8315
8316 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8317 iparams++;
8318 }
8319 child_die = sibling_die (child_die);
8320 }
8321 }
8322
76c10ea2 8323 return ftype;
c906108c
SS
8324}
8325
f792889a 8326static struct type *
e7c27a73 8327read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8328{
e7c27a73 8329 struct objfile *objfile = cu->objfile;
0114d602 8330 const char *name = NULL;
f792889a 8331 struct type *this_type;
c906108c 8332
94af9270 8333 name = dwarf2_full_name (NULL, die, cu);
f792889a 8334 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8335 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8336 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8337 set_die_type (die, this_type, cu);
8338 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8339 return this_type;
c906108c
SS
8340}
8341
8342/* Find a representation of a given base type and install
8343 it in the TYPE field of the die. */
8344
f792889a 8345static struct type *
e7c27a73 8346read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8347{
e7c27a73 8348 struct objfile *objfile = cu->objfile;
c906108c
SS
8349 struct type *type;
8350 struct attribute *attr;
8351 int encoding = 0, size = 0;
39cbfefa 8352 char *name;
6ccb9162
UW
8353 enum type_code code = TYPE_CODE_INT;
8354 int type_flags = 0;
8355 struct type *target_type = NULL;
c906108c 8356
e142c38c 8357 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8358 if (attr)
8359 {
8360 encoding = DW_UNSND (attr);
8361 }
e142c38c 8362 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8363 if (attr)
8364 {
8365 size = DW_UNSND (attr);
8366 }
39cbfefa 8367 name = dwarf2_name (die, cu);
6ccb9162 8368 if (!name)
c906108c 8369 {
6ccb9162
UW
8370 complaint (&symfile_complaints,
8371 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8372 }
6ccb9162
UW
8373
8374 switch (encoding)
c906108c 8375 {
6ccb9162
UW
8376 case DW_ATE_address:
8377 /* Turn DW_ATE_address into a void * pointer. */
8378 code = TYPE_CODE_PTR;
8379 type_flags |= TYPE_FLAG_UNSIGNED;
8380 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8381 break;
8382 case DW_ATE_boolean:
8383 code = TYPE_CODE_BOOL;
8384 type_flags |= TYPE_FLAG_UNSIGNED;
8385 break;
8386 case DW_ATE_complex_float:
8387 code = TYPE_CODE_COMPLEX;
8388 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8389 break;
8390 case DW_ATE_decimal_float:
8391 code = TYPE_CODE_DECFLOAT;
8392 break;
8393 case DW_ATE_float:
8394 code = TYPE_CODE_FLT;
8395 break;
8396 case DW_ATE_signed:
8397 break;
8398 case DW_ATE_unsigned:
8399 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8400 if (cu->language == language_fortran
8401 && name
8402 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8403 code = TYPE_CODE_CHAR;
6ccb9162
UW
8404 break;
8405 case DW_ATE_signed_char:
6e70227d 8406 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8407 || cu->language == language_pascal
8408 || cu->language == language_fortran)
6ccb9162
UW
8409 code = TYPE_CODE_CHAR;
8410 break;
8411 case DW_ATE_unsigned_char:
868a0084 8412 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8413 || cu->language == language_pascal
8414 || cu->language == language_fortran)
6ccb9162
UW
8415 code = TYPE_CODE_CHAR;
8416 type_flags |= TYPE_FLAG_UNSIGNED;
8417 break;
75079b2b
TT
8418 case DW_ATE_UTF:
8419 /* We just treat this as an integer and then recognize the
8420 type by name elsewhere. */
8421 break;
8422
6ccb9162
UW
8423 default:
8424 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8425 dwarf_type_encoding_name (encoding));
8426 break;
c906108c 8427 }
6ccb9162 8428
0114d602
DJ
8429 type = init_type (code, size, type_flags, NULL, objfile);
8430 TYPE_NAME (type) = name;
6ccb9162
UW
8431 TYPE_TARGET_TYPE (type) = target_type;
8432
0114d602 8433 if (name && strcmp (name, "char") == 0)
876cecd0 8434 TYPE_NOSIGN (type) = 1;
0114d602 8435
f792889a 8436 return set_die_type (die, type, cu);
c906108c
SS
8437}
8438
a02abb62
JB
8439/* Read the given DW_AT_subrange DIE. */
8440
f792889a 8441static struct type *
a02abb62
JB
8442read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8443{
8444 struct type *base_type;
8445 struct type *range_type;
8446 struct attribute *attr;
43bbcdc2
PH
8447 LONGEST low = 0;
8448 LONGEST high = -1;
39cbfefa 8449 char *name;
43bbcdc2 8450 LONGEST negative_mask;
e77813c8 8451
a02abb62 8452 base_type = die_type (die, cu);
953ac07e
JK
8453 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8454 check_typedef (base_type);
a02abb62 8455
7e314c57
JK
8456 /* The die_type call above may have already set the type for this DIE. */
8457 range_type = get_die_type (die, cu);
8458 if (range_type)
8459 return range_type;
8460
e142c38c 8461 if (cu->language == language_fortran)
6e70227d 8462 {
a02abb62
JB
8463 /* FORTRAN implies a lower bound of 1, if not given. */
8464 low = 1;
8465 }
8466
dd5e6932
DJ
8467 /* FIXME: For variable sized arrays either of these could be
8468 a variable rather than a constant value. We'll allow it,
8469 but we don't know how to handle it. */
e142c38c 8470 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8471 if (attr)
8472 low = dwarf2_get_attr_constant_value (attr, 0);
8473
e142c38c 8474 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8475 if (attr)
6e70227d 8476 {
e77813c8 8477 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8478 {
8479 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8480 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8481 FIXME: GDB does not yet know how to handle dynamic
8482 arrays properly, treat them as arrays with unspecified
8483 length for now.
8484
8485 FIXME: jimb/2003-09-22: GDB does not really know
8486 how to handle arrays of unspecified length
8487 either; we just represent them as zero-length
8488 arrays. Choose an appropriate upper bound given
8489 the lower bound we've computed above. */
8490 high = low - 1;
8491 }
8492 else
8493 high = dwarf2_get_attr_constant_value (attr, 1);
8494 }
e77813c8
PM
8495 else
8496 {
8497 attr = dwarf2_attr (die, DW_AT_count, cu);
8498 if (attr)
8499 {
8500 int count = dwarf2_get_attr_constant_value (attr, 1);
8501 high = low + count - 1;
8502 }
c2ff108b
JK
8503 else
8504 {
8505 /* Unspecified array length. */
8506 high = low - 1;
8507 }
e77813c8
PM
8508 }
8509
8510 /* Dwarf-2 specifications explicitly allows to create subrange types
8511 without specifying a base type.
8512 In that case, the base type must be set to the type of
8513 the lower bound, upper bound or count, in that order, if any of these
8514 three attributes references an object that has a type.
8515 If no base type is found, the Dwarf-2 specifications say that
8516 a signed integer type of size equal to the size of an address should
8517 be used.
8518 For the following C code: `extern char gdb_int [];'
8519 GCC produces an empty range DIE.
8520 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8521 high bound or count are not yet handled by this code. */
e77813c8
PM
8522 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8523 {
8524 struct objfile *objfile = cu->objfile;
8525 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8526 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8527 struct type *int_type = objfile_type (objfile)->builtin_int;
8528
8529 /* Test "int", "long int", and "long long int" objfile types,
8530 and select the first one having a size above or equal to the
8531 architecture address size. */
8532 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8533 base_type = int_type;
8534 else
8535 {
8536 int_type = objfile_type (objfile)->builtin_long;
8537 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8538 base_type = int_type;
8539 else
8540 {
8541 int_type = objfile_type (objfile)->builtin_long_long;
8542 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8543 base_type = int_type;
8544 }
8545 }
8546 }
a02abb62 8547
6e70227d 8548 negative_mask =
43bbcdc2
PH
8549 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8550 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8551 low |= negative_mask;
8552 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8553 high |= negative_mask;
8554
a02abb62
JB
8555 range_type = create_range_type (NULL, base_type, low, high);
8556
bbb0eef6
JK
8557 /* Mark arrays with dynamic length at least as an array of unspecified
8558 length. GDB could check the boundary but before it gets implemented at
8559 least allow accessing the array elements. */
8560 if (attr && attr->form == DW_FORM_block1)
8561 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8562
c2ff108b
JK
8563 /* Ada expects an empty array on no boundary attributes. */
8564 if (attr == NULL && cu->language != language_ada)
8565 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8566
39cbfefa
DJ
8567 name = dwarf2_name (die, cu);
8568 if (name)
8569 TYPE_NAME (range_type) = name;
6e70227d 8570
e142c38c 8571 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8572 if (attr)
8573 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8574
7e314c57
JK
8575 set_die_type (die, range_type, cu);
8576
8577 /* set_die_type should be already done. */
b4ba55a1
JB
8578 set_descriptive_type (range_type, die, cu);
8579
7e314c57 8580 return range_type;
a02abb62 8581}
6e70227d 8582
f792889a 8583static struct type *
81a17f79
JB
8584read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8585{
8586 struct type *type;
81a17f79 8587
81a17f79
JB
8588 /* For now, we only support the C meaning of an unspecified type: void. */
8589
0114d602
DJ
8590 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8591 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8592
f792889a 8593 return set_die_type (die, type, cu);
81a17f79 8594}
a02abb62 8595
51545339
DJ
8596/* Trivial hash function for die_info: the hash value of a DIE
8597 is its offset in .debug_info for this objfile. */
8598
8599static hashval_t
8600die_hash (const void *item)
8601{
8602 const struct die_info *die = item;
9a619af0 8603
51545339
DJ
8604 return die->offset;
8605}
8606
8607/* Trivial comparison function for die_info structures: two DIEs
8608 are equal if they have the same offset. */
8609
8610static int
8611die_eq (const void *item_lhs, const void *item_rhs)
8612{
8613 const struct die_info *die_lhs = item_lhs;
8614 const struct die_info *die_rhs = item_rhs;
9a619af0 8615
51545339
DJ
8616 return die_lhs->offset == die_rhs->offset;
8617}
8618
c906108c
SS
8619/* Read a whole compilation unit into a linked list of dies. */
8620
f9aca02d 8621static struct die_info *
93311388 8622read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8623{
93311388 8624 struct die_reader_specs reader_specs;
98bfdba5 8625 int read_abbrevs = 0;
1d9ec526 8626 struct cleanup *back_to = NULL;
98bfdba5
PA
8627 struct die_info *die;
8628
8629 if (cu->dwarf2_abbrevs == NULL)
8630 {
8631 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8632 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8633 read_abbrevs = 1;
8634 }
93311388 8635
348e048f 8636 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8637 cu->die_hash
8638 = htab_create_alloc_ex (cu->header.length / 12,
8639 die_hash,
8640 die_eq,
8641 NULL,
8642 &cu->comp_unit_obstack,
8643 hashtab_obstack_allocate,
8644 dummy_obstack_deallocate);
8645
93311388
DE
8646 init_cu_die_reader (&reader_specs, cu);
8647
98bfdba5
PA
8648 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8649
8650 if (read_abbrevs)
8651 do_cleanups (back_to);
8652
8653 return die;
639d11d3
DC
8654}
8655
d97bc12b
DE
8656/* Main entry point for reading a DIE and all children.
8657 Read the DIE and dump it if requested. */
8658
8659static struct die_info *
93311388
DE
8660read_die_and_children (const struct die_reader_specs *reader,
8661 gdb_byte *info_ptr,
d97bc12b
DE
8662 gdb_byte **new_info_ptr,
8663 struct die_info *parent)
8664{
93311388 8665 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8666 new_info_ptr, parent);
8667
8668 if (dwarf2_die_debug)
8669 {
348e048f
DE
8670 fprintf_unfiltered (gdb_stdlog,
8671 "\nRead die from %s of %s:\n",
8672 reader->buffer == dwarf2_per_objfile->info.buffer
8673 ? ".debug_info"
8674 : reader->buffer == dwarf2_per_objfile->types.buffer
8675 ? ".debug_types"
8676 : "unknown section",
8677 reader->abfd->filename);
d97bc12b
DE
8678 dump_die (result, dwarf2_die_debug);
8679 }
8680
8681 return result;
8682}
8683
639d11d3
DC
8684/* Read a single die and all its descendents. Set the die's sibling
8685 field to NULL; set other fields in the die correctly, and set all
8686 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8687 location of the info_ptr after reading all of those dies. PARENT
8688 is the parent of the die in question. */
8689
8690static struct die_info *
93311388
DE
8691read_die_and_children_1 (const struct die_reader_specs *reader,
8692 gdb_byte *info_ptr,
d97bc12b
DE
8693 gdb_byte **new_info_ptr,
8694 struct die_info *parent)
639d11d3
DC
8695{
8696 struct die_info *die;
fe1b8b76 8697 gdb_byte *cur_ptr;
639d11d3
DC
8698 int has_children;
8699
93311388 8700 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8701 if (die == NULL)
8702 {
8703 *new_info_ptr = cur_ptr;
8704 return NULL;
8705 }
93311388 8706 store_in_ref_table (die, reader->cu);
639d11d3
DC
8707
8708 if (has_children)
348e048f 8709 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8710 else
8711 {
8712 die->child = NULL;
8713 *new_info_ptr = cur_ptr;
8714 }
8715
8716 die->sibling = NULL;
8717 die->parent = parent;
8718 return die;
8719}
8720
8721/* Read a die, all of its descendents, and all of its siblings; set
8722 all of the fields of all of the dies correctly. Arguments are as
8723 in read_die_and_children. */
8724
8725static struct die_info *
93311388
DE
8726read_die_and_siblings (const struct die_reader_specs *reader,
8727 gdb_byte *info_ptr,
fe1b8b76 8728 gdb_byte **new_info_ptr,
639d11d3
DC
8729 struct die_info *parent)
8730{
8731 struct die_info *first_die, *last_sibling;
fe1b8b76 8732 gdb_byte *cur_ptr;
639d11d3 8733
c906108c 8734 cur_ptr = info_ptr;
639d11d3
DC
8735 first_die = last_sibling = NULL;
8736
8737 while (1)
c906108c 8738 {
639d11d3 8739 struct die_info *die
93311388 8740 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8741
1d325ec1 8742 if (die == NULL)
c906108c 8743 {
639d11d3
DC
8744 *new_info_ptr = cur_ptr;
8745 return first_die;
c906108c 8746 }
1d325ec1
DJ
8747
8748 if (!first_die)
8749 first_die = die;
c906108c 8750 else
1d325ec1
DJ
8751 last_sibling->sibling = die;
8752
8753 last_sibling = die;
c906108c 8754 }
c906108c
SS
8755}
8756
93311388
DE
8757/* Read the die from the .debug_info section buffer. Set DIEP to
8758 point to a newly allocated die with its information, except for its
8759 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8760 whether the die has children or not. */
8761
8762static gdb_byte *
8763read_full_die (const struct die_reader_specs *reader,
8764 struct die_info **diep, gdb_byte *info_ptr,
8765 int *has_children)
8766{
8767 unsigned int abbrev_number, bytes_read, i, offset;
8768 struct abbrev_info *abbrev;
8769 struct die_info *die;
8770 struct dwarf2_cu *cu = reader->cu;
8771 bfd *abfd = reader->abfd;
8772
8773 offset = info_ptr - reader->buffer;
8774 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8775 info_ptr += bytes_read;
8776 if (!abbrev_number)
8777 {
8778 *diep = NULL;
8779 *has_children = 0;
8780 return info_ptr;
8781 }
8782
8783 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8784 if (!abbrev)
348e048f
DE
8785 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8786 abbrev_number,
8787 bfd_get_filename (abfd));
8788
93311388
DE
8789 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8790 die->offset = offset;
8791 die->tag = abbrev->tag;
8792 die->abbrev = abbrev_number;
8793
8794 die->num_attrs = abbrev->num_attrs;
8795
8796 for (i = 0; i < abbrev->num_attrs; ++i)
8797 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8798 abfd, info_ptr, cu);
8799
8800 *diep = die;
8801 *has_children = abbrev->has_children;
8802 return info_ptr;
8803}
8804
c906108c
SS
8805/* In DWARF version 2, the description of the debugging information is
8806 stored in a separate .debug_abbrev section. Before we read any
8807 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8808 in a hash table. This function also sets flags in CU describing
8809 the data found in the abbrev table. */
c906108c
SS
8810
8811static void
e7c27a73 8812dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8813{
e7c27a73 8814 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8815 gdb_byte *abbrev_ptr;
c906108c
SS
8816 struct abbrev_info *cur_abbrev;
8817 unsigned int abbrev_number, bytes_read, abbrev_name;
8818 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8819 struct attr_abbrev *cur_attrs;
8820 unsigned int allocated_attrs;
c906108c 8821
0963b4bd 8822 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8823 obstack_init (&cu->abbrev_obstack);
8824 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8825 (ABBREV_HASH_SIZE
8826 * sizeof (struct abbrev_info *)));
8827 memset (cu->dwarf2_abbrevs, 0,
8828 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8829
be391dca
TT
8830 dwarf2_read_section (dwarf2_per_objfile->objfile,
8831 &dwarf2_per_objfile->abbrev);
dce234bc 8832 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8833 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8834 abbrev_ptr += bytes_read;
8835
f3dd6933
DJ
8836 allocated_attrs = ATTR_ALLOC_CHUNK;
8837 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8838
0963b4bd 8839 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8840 while (abbrev_number)
8841 {
f3dd6933 8842 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8843
8844 /* read in abbrev header */
8845 cur_abbrev->number = abbrev_number;
8846 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8847 abbrev_ptr += bytes_read;
8848 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8849 abbrev_ptr += 1;
8850
72bf9492
DJ
8851 if (cur_abbrev->tag == DW_TAG_namespace)
8852 cu->has_namespace_info = 1;
8853
c906108c
SS
8854 /* now read in declarations */
8855 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8856 abbrev_ptr += bytes_read;
8857 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8858 abbrev_ptr += bytes_read;
8859 while (abbrev_name)
8860 {
f3dd6933 8861 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8862 {
f3dd6933
DJ
8863 allocated_attrs += ATTR_ALLOC_CHUNK;
8864 cur_attrs
8865 = xrealloc (cur_attrs, (allocated_attrs
8866 * sizeof (struct attr_abbrev)));
c906108c 8867 }
ae038cb0
DJ
8868
8869 /* Record whether this compilation unit might have
8870 inter-compilation-unit references. If we don't know what form
8871 this attribute will have, then it might potentially be a
8872 DW_FORM_ref_addr, so we conservatively expect inter-CU
8873 references. */
8874
8875 if (abbrev_form == DW_FORM_ref_addr
8876 || abbrev_form == DW_FORM_indirect)
8877 cu->has_form_ref_addr = 1;
8878
f3dd6933
DJ
8879 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8880 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8881 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8882 abbrev_ptr += bytes_read;
8883 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8884 abbrev_ptr += bytes_read;
8885 }
8886
f3dd6933
DJ
8887 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8888 (cur_abbrev->num_attrs
8889 * sizeof (struct attr_abbrev)));
8890 memcpy (cur_abbrev->attrs, cur_attrs,
8891 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8892
c906108c 8893 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8894 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8895 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8896
8897 /* Get next abbreviation.
8898 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8899 always properly terminated with an abbrev number of 0.
8900 Exit loop if we encounter an abbreviation which we have
8901 already read (which means we are about to read the abbreviations
8902 for the next compile unit) or if the end of the abbreviation
8903 table is reached. */
dce234bc
PP
8904 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8905 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8906 break;
8907 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8908 abbrev_ptr += bytes_read;
e7c27a73 8909 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8910 break;
8911 }
f3dd6933
DJ
8912
8913 xfree (cur_attrs);
c906108c
SS
8914}
8915
f3dd6933 8916/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8917
c906108c 8918static void
f3dd6933 8919dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8920{
f3dd6933 8921 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8922
f3dd6933
DJ
8923 obstack_free (&cu->abbrev_obstack, NULL);
8924 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8925}
8926
8927/* Lookup an abbrev_info structure in the abbrev hash table. */
8928
8929static struct abbrev_info *
e7c27a73 8930dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8931{
8932 unsigned int hash_number;
8933 struct abbrev_info *abbrev;
8934
8935 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8936 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8937
8938 while (abbrev)
8939 {
8940 if (abbrev->number == number)
8941 return abbrev;
8942 else
8943 abbrev = abbrev->next;
8944 }
8945 return NULL;
8946}
8947
72bf9492
DJ
8948/* Returns nonzero if TAG represents a type that we might generate a partial
8949 symbol for. */
8950
8951static int
8952is_type_tag_for_partial (int tag)
8953{
8954 switch (tag)
8955 {
8956#if 0
8957 /* Some types that would be reasonable to generate partial symbols for,
8958 that we don't at present. */
8959 case DW_TAG_array_type:
8960 case DW_TAG_file_type:
8961 case DW_TAG_ptr_to_member_type:
8962 case DW_TAG_set_type:
8963 case DW_TAG_string_type:
8964 case DW_TAG_subroutine_type:
8965#endif
8966 case DW_TAG_base_type:
8967 case DW_TAG_class_type:
680b30c7 8968 case DW_TAG_interface_type:
72bf9492
DJ
8969 case DW_TAG_enumeration_type:
8970 case DW_TAG_structure_type:
8971 case DW_TAG_subrange_type:
8972 case DW_TAG_typedef:
8973 case DW_TAG_union_type:
8974 return 1;
8975 default:
8976 return 0;
8977 }
8978}
8979
8980/* Load all DIEs that are interesting for partial symbols into memory. */
8981
8982static struct partial_die_info *
93311388
DE
8983load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8984 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8985{
8986 struct partial_die_info *part_die;
8987 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8988 struct abbrev_info *abbrev;
8989 unsigned int bytes_read;
5afb4e99 8990 unsigned int load_all = 0;
72bf9492
DJ
8991
8992 int nesting_level = 1;
8993
8994 parent_die = NULL;
8995 last_die = NULL;
8996
5afb4e99
DJ
8997 if (cu->per_cu && cu->per_cu->load_all_dies)
8998 load_all = 1;
8999
72bf9492
DJ
9000 cu->partial_dies
9001 = htab_create_alloc_ex (cu->header.length / 12,
9002 partial_die_hash,
9003 partial_die_eq,
9004 NULL,
9005 &cu->comp_unit_obstack,
9006 hashtab_obstack_allocate,
9007 dummy_obstack_deallocate);
9008
9009 part_die = obstack_alloc (&cu->comp_unit_obstack,
9010 sizeof (struct partial_die_info));
9011
9012 while (1)
9013 {
9014 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9015
9016 /* A NULL abbrev means the end of a series of children. */
9017 if (abbrev == NULL)
9018 {
9019 if (--nesting_level == 0)
9020 {
9021 /* PART_DIE was probably the last thing allocated on the
9022 comp_unit_obstack, so we could call obstack_free
9023 here. We don't do that because the waste is small,
9024 and will be cleaned up when we're done with this
9025 compilation unit. This way, we're also more robust
9026 against other users of the comp_unit_obstack. */
9027 return first_die;
9028 }
9029 info_ptr += bytes_read;
9030 last_die = parent_die;
9031 parent_die = parent_die->die_parent;
9032 continue;
9033 }
9034
98bfdba5
PA
9035 /* Check for template arguments. We never save these; if
9036 they're seen, we just mark the parent, and go on our way. */
9037 if (parent_die != NULL
9038 && cu->language == language_cplus
9039 && (abbrev->tag == DW_TAG_template_type_param
9040 || abbrev->tag == DW_TAG_template_value_param))
9041 {
9042 parent_die->has_template_arguments = 1;
9043
9044 if (!load_all)
9045 {
9046 /* We don't need a partial DIE for the template argument. */
9047 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9048 cu);
9049 continue;
9050 }
9051 }
9052
9053 /* We only recurse into subprograms looking for template arguments.
9054 Skip their other children. */
9055 if (!load_all
9056 && cu->language == language_cplus
9057 && parent_die != NULL
9058 && parent_die->tag == DW_TAG_subprogram)
9059 {
9060 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9061 continue;
9062 }
9063
5afb4e99
DJ
9064 /* Check whether this DIE is interesting enough to save. Normally
9065 we would not be interested in members here, but there may be
9066 later variables referencing them via DW_AT_specification (for
9067 static members). */
9068 if (!load_all
9069 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9070 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9071 && abbrev->tag != DW_TAG_enumerator
9072 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9073 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9074 && abbrev->tag != DW_TAG_variable
5afb4e99 9075 && abbrev->tag != DW_TAG_namespace
f55ee35c 9076 && abbrev->tag != DW_TAG_module
5afb4e99 9077 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9078 {
9079 /* Otherwise we skip to the next sibling, if any. */
93311388 9080 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9081 continue;
9082 }
9083
93311388
DE
9084 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9085 buffer, info_ptr, cu);
72bf9492
DJ
9086
9087 /* This two-pass algorithm for processing partial symbols has a
9088 high cost in cache pressure. Thus, handle some simple cases
9089 here which cover the majority of C partial symbols. DIEs
9090 which neither have specification tags in them, nor could have
9091 specification tags elsewhere pointing at them, can simply be
9092 processed and discarded.
9093
9094 This segment is also optional; scan_partial_symbols and
9095 add_partial_symbol will handle these DIEs if we chain
9096 them in normally. When compilers which do not emit large
9097 quantities of duplicate debug information are more common,
9098 this code can probably be removed. */
9099
9100 /* Any complete simple types at the top level (pretty much all
9101 of them, for a language without namespaces), can be processed
9102 directly. */
9103 if (parent_die == NULL
9104 && part_die->has_specification == 0
9105 && part_die->is_declaration == 0
d8228535 9106 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9107 || part_die->tag == DW_TAG_base_type
9108 || part_die->tag == DW_TAG_subrange_type))
9109 {
9110 if (building_psymtab && part_die->name != NULL)
04a679b8 9111 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9112 VAR_DOMAIN, LOC_TYPEDEF,
9113 &cu->objfile->static_psymbols,
9114 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9115 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9116 continue;
9117 }
9118
d8228535
JK
9119 /* The exception for DW_TAG_typedef with has_children above is
9120 a workaround of GCC PR debug/47510. In the case of this complaint
9121 type_name_no_tag_or_error will error on such types later.
9122
9123 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9124 it could not find the child DIEs referenced later, this is checked
9125 above. In correct DWARF DW_TAG_typedef should have no children. */
9126
9127 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9128 complaint (&symfile_complaints,
9129 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9130 "- DIE at 0x%x [in module %s]"),
9131 part_die->offset, cu->objfile->name);
9132
72bf9492
DJ
9133 /* If we're at the second level, and we're an enumerator, and
9134 our parent has no specification (meaning possibly lives in a
9135 namespace elsewhere), then we can add the partial symbol now
9136 instead of queueing it. */
9137 if (part_die->tag == DW_TAG_enumerator
9138 && parent_die != NULL
9139 && parent_die->die_parent == NULL
9140 && parent_die->tag == DW_TAG_enumeration_type
9141 && parent_die->has_specification == 0)
9142 {
9143 if (part_die->name == NULL)
3e43a32a
MS
9144 complaint (&symfile_complaints,
9145 _("malformed enumerator DIE ignored"));
72bf9492 9146 else if (building_psymtab)
04a679b8 9147 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9148 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9149 (cu->language == language_cplus
9150 || cu->language == language_java)
72bf9492
DJ
9151 ? &cu->objfile->global_psymbols
9152 : &cu->objfile->static_psymbols,
9153 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9154
93311388 9155 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9156 continue;
9157 }
9158
9159 /* We'll save this DIE so link it in. */
9160 part_die->die_parent = parent_die;
9161 part_die->die_sibling = NULL;
9162 part_die->die_child = NULL;
9163
9164 if (last_die && last_die == parent_die)
9165 last_die->die_child = part_die;
9166 else if (last_die)
9167 last_die->die_sibling = part_die;
9168
9169 last_die = part_die;
9170
9171 if (first_die == NULL)
9172 first_die = part_die;
9173
9174 /* Maybe add the DIE to the hash table. Not all DIEs that we
9175 find interesting need to be in the hash table, because we
9176 also have the parent/sibling/child chains; only those that we
9177 might refer to by offset later during partial symbol reading.
9178
9179 For now this means things that might have be the target of a
9180 DW_AT_specification, DW_AT_abstract_origin, or
9181 DW_AT_extension. DW_AT_extension will refer only to
9182 namespaces; DW_AT_abstract_origin refers to functions (and
9183 many things under the function DIE, but we do not recurse
9184 into function DIEs during partial symbol reading) and
9185 possibly variables as well; DW_AT_specification refers to
9186 declarations. Declarations ought to have the DW_AT_declaration
9187 flag. It happens that GCC forgets to put it in sometimes, but
9188 only for functions, not for types.
9189
9190 Adding more things than necessary to the hash table is harmless
9191 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9192 wasted time in find_partial_die, when we reread the compilation
9193 unit with load_all_dies set. */
72bf9492 9194
5afb4e99 9195 if (load_all
72929c62 9196 || abbrev->tag == DW_TAG_constant
5afb4e99 9197 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9198 || abbrev->tag == DW_TAG_variable
9199 || abbrev->tag == DW_TAG_namespace
9200 || part_die->is_declaration)
9201 {
9202 void **slot;
9203
9204 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9205 part_die->offset, INSERT);
9206 *slot = part_die;
9207 }
9208
9209 part_die = obstack_alloc (&cu->comp_unit_obstack,
9210 sizeof (struct partial_die_info));
9211
9212 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9213 we have no reason to follow the children of structures; for other
98bfdba5
PA
9214 languages we have to, so that we can get at method physnames
9215 to infer fully qualified class names, for DW_AT_specification,
9216 and for C++ template arguments. For C++, we also look one level
9217 inside functions to find template arguments (if the name of the
9218 function does not already contain the template arguments).
bc30ff58
JB
9219
9220 For Ada, we need to scan the children of subprograms and lexical
9221 blocks as well because Ada allows the definition of nested
9222 entities that could be interesting for the debugger, such as
9223 nested subprograms for instance. */
72bf9492 9224 if (last_die->has_children
5afb4e99
DJ
9225 && (load_all
9226 || last_die->tag == DW_TAG_namespace
f55ee35c 9227 || last_die->tag == DW_TAG_module
72bf9492 9228 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9229 || (cu->language == language_cplus
9230 && last_die->tag == DW_TAG_subprogram
9231 && (last_die->name == NULL
9232 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9233 || (cu->language != language_c
9234 && (last_die->tag == DW_TAG_class_type
680b30c7 9235 || last_die->tag == DW_TAG_interface_type
72bf9492 9236 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9237 || last_die->tag == DW_TAG_union_type))
9238 || (cu->language == language_ada
9239 && (last_die->tag == DW_TAG_subprogram
9240 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9241 {
9242 nesting_level++;
9243 parent_die = last_die;
9244 continue;
9245 }
9246
9247 /* Otherwise we skip to the next sibling, if any. */
93311388 9248 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9249
9250 /* Back to the top, do it again. */
9251 }
9252}
9253
c906108c
SS
9254/* Read a minimal amount of information into the minimal die structure. */
9255
fe1b8b76 9256static gdb_byte *
72bf9492
DJ
9257read_partial_die (struct partial_die_info *part_die,
9258 struct abbrev_info *abbrev,
9259 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9260 gdb_byte *buffer, gdb_byte *info_ptr,
9261 struct dwarf2_cu *cu)
c906108c 9262{
fa238c03 9263 unsigned int i;
c906108c 9264 struct attribute attr;
c5aa993b 9265 int has_low_pc_attr = 0;
c906108c
SS
9266 int has_high_pc_attr = 0;
9267
72bf9492 9268 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9269
93311388 9270 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9271
9272 info_ptr += abbrev_len;
9273
9274 if (abbrev == NULL)
9275 return info_ptr;
9276
c906108c
SS
9277 part_die->tag = abbrev->tag;
9278 part_die->has_children = abbrev->has_children;
c906108c
SS
9279
9280 for (i = 0; i < abbrev->num_attrs; ++i)
9281 {
e7c27a73 9282 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9283
9284 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9285 partial symbol table. */
c906108c
SS
9286 switch (attr.name)
9287 {
9288 case DW_AT_name:
71c25dea
TT
9289 switch (part_die->tag)
9290 {
9291 case DW_TAG_compile_unit:
348e048f 9292 case DW_TAG_type_unit:
71c25dea
TT
9293 /* Compilation units have a DW_AT_name that is a filename, not
9294 a source language identifier. */
9295 case DW_TAG_enumeration_type:
9296 case DW_TAG_enumerator:
9297 /* These tags always have simple identifiers already; no need
9298 to canonicalize them. */
9299 part_die->name = DW_STRING (&attr);
9300 break;
9301 default:
9302 part_die->name
9303 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9304 &cu->objfile->objfile_obstack);
71c25dea
TT
9305 break;
9306 }
c906108c 9307 break;
31ef98ae 9308 case DW_AT_linkage_name:
c906108c 9309 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9310 /* Note that both forms of linkage name might appear. We
9311 assume they will be the same, and we only store the last
9312 one we see. */
94af9270
KS
9313 if (cu->language == language_ada)
9314 part_die->name = DW_STRING (&attr);
abc72ce4 9315 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9316 break;
9317 case DW_AT_low_pc:
9318 has_low_pc_attr = 1;
9319 part_die->lowpc = DW_ADDR (&attr);
9320 break;
9321 case DW_AT_high_pc:
9322 has_high_pc_attr = 1;
9323 part_die->highpc = DW_ADDR (&attr);
9324 break;
9325 case DW_AT_location:
0963b4bd 9326 /* Support the .debug_loc offsets. */
8e19ed76
PS
9327 if (attr_form_is_block (&attr))
9328 {
9329 part_die->locdesc = DW_BLOCK (&attr);
9330 }
3690dd37 9331 else if (attr_form_is_section_offset (&attr))
8e19ed76 9332 {
4d3c2250 9333 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9334 }
9335 else
9336 {
4d3c2250
KB
9337 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9338 "partial symbol information");
8e19ed76 9339 }
c906108c 9340 break;
c906108c
SS
9341 case DW_AT_external:
9342 part_die->is_external = DW_UNSND (&attr);
9343 break;
9344 case DW_AT_declaration:
9345 part_die->is_declaration = DW_UNSND (&attr);
9346 break;
9347 case DW_AT_type:
9348 part_die->has_type = 1;
9349 break;
9350 case DW_AT_abstract_origin:
9351 case DW_AT_specification:
72bf9492
DJ
9352 case DW_AT_extension:
9353 part_die->has_specification = 1;
c764a876 9354 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9355 break;
9356 case DW_AT_sibling:
9357 /* Ignore absolute siblings, they might point outside of
9358 the current compile unit. */
9359 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9360 complaint (&symfile_complaints,
9361 _("ignoring absolute DW_AT_sibling"));
c906108c 9362 else
93311388 9363 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9364 break;
fa4028e9
JB
9365 case DW_AT_byte_size:
9366 part_die->has_byte_size = 1;
9367 break;
68511cec
CES
9368 case DW_AT_calling_convention:
9369 /* DWARF doesn't provide a way to identify a program's source-level
9370 entry point. DW_AT_calling_convention attributes are only meant
9371 to describe functions' calling conventions.
9372
9373 However, because it's a necessary piece of information in
9374 Fortran, and because DW_CC_program is the only piece of debugging
9375 information whose definition refers to a 'main program' at all,
9376 several compilers have begun marking Fortran main programs with
9377 DW_CC_program --- even when those functions use the standard
9378 calling conventions.
9379
9380 So until DWARF specifies a way to provide this information and
9381 compilers pick up the new representation, we'll support this
9382 practice. */
9383 if (DW_UNSND (&attr) == DW_CC_program
9384 && cu->language == language_fortran)
01f8c46d
JK
9385 {
9386 set_main_name (part_die->name);
9387
9388 /* As this DIE has a static linkage the name would be difficult
9389 to look up later. */
9390 language_of_main = language_fortran;
9391 }
68511cec 9392 break;
c906108c
SS
9393 default:
9394 break;
9395 }
9396 }
9397
9373cf26
JK
9398 if (has_low_pc_attr && has_high_pc_attr)
9399 {
9400 /* When using the GNU linker, .gnu.linkonce. sections are used to
9401 eliminate duplicate copies of functions and vtables and such.
9402 The linker will arbitrarily choose one and discard the others.
9403 The AT_*_pc values for such functions refer to local labels in
9404 these sections. If the section from that file was discarded, the
9405 labels are not in the output, so the relocs get a value of 0.
9406 If this is a discarded function, mark the pc bounds as invalid,
9407 so that GDB will ignore it. */
9408 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9409 {
9410 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9411
9412 complaint (&symfile_complaints,
9413 _("DW_AT_low_pc %s is zero "
9414 "for DIE at 0x%x [in module %s]"),
9415 paddress (gdbarch, part_die->lowpc),
9416 part_die->offset, cu->objfile->name);
9417 }
9418 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9419 else if (part_die->lowpc >= part_die->highpc)
9420 {
9421 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9422
9423 complaint (&symfile_complaints,
9424 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9425 "for DIE at 0x%x [in module %s]"),
9426 paddress (gdbarch, part_die->lowpc),
9427 paddress (gdbarch, part_die->highpc),
9428 part_die->offset, cu->objfile->name);
9429 }
9430 else
9431 part_die->has_pc_info = 1;
9432 }
85cbf3d3 9433
c906108c
SS
9434 return info_ptr;
9435}
9436
72bf9492
DJ
9437/* Find a cached partial DIE at OFFSET in CU. */
9438
9439static struct partial_die_info *
c764a876 9440find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9441{
9442 struct partial_die_info *lookup_die = NULL;
9443 struct partial_die_info part_die;
9444
9445 part_die.offset = offset;
9446 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9447
72bf9492
DJ
9448 return lookup_die;
9449}
9450
348e048f
DE
9451/* Find a partial DIE at OFFSET, which may or may not be in CU,
9452 except in the case of .debug_types DIEs which do not reference
9453 outside their CU (they do however referencing other types via
55f1336d 9454 DW_FORM_ref_sig8). */
72bf9492
DJ
9455
9456static struct partial_die_info *
c764a876 9457find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9458{
5afb4e99
DJ
9459 struct dwarf2_per_cu_data *per_cu = NULL;
9460 struct partial_die_info *pd = NULL;
72bf9492 9461
348e048f
DE
9462 if (cu->per_cu->from_debug_types)
9463 {
9464 pd = find_partial_die_in_comp_unit (offset, cu);
9465 if (pd != NULL)
9466 return pd;
9467 goto not_found;
9468 }
9469
45452591 9470 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9471 {
9472 pd = find_partial_die_in_comp_unit (offset, cu);
9473 if (pd != NULL)
9474 return pd;
9475 }
72bf9492 9476
ae038cb0
DJ
9477 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9478
98bfdba5
PA
9479 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9480 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9481
9482 per_cu->cu->last_used = 0;
5afb4e99
DJ
9483 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9484
9485 if (pd == NULL && per_cu->load_all_dies == 0)
9486 {
9487 struct cleanup *back_to;
9488 struct partial_die_info comp_unit_die;
9489 struct abbrev_info *abbrev;
9490 unsigned int bytes_read;
9491 char *info_ptr;
9492
9493 per_cu->load_all_dies = 1;
9494
9495 /* Re-read the DIEs. */
9496 back_to = make_cleanup (null_cleanup, 0);
9497 if (per_cu->cu->dwarf2_abbrevs == NULL)
9498 {
9499 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9500 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9501 }
dce234bc 9502 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9503 + per_cu->cu->header.offset
9504 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9505 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9506 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9507 per_cu->cu->objfile->obfd,
9508 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9509 per_cu->cu);
9510 if (comp_unit_die.has_children)
93311388
DE
9511 load_partial_dies (per_cu->cu->objfile->obfd,
9512 dwarf2_per_objfile->info.buffer, info_ptr,
9513 0, per_cu->cu);
5afb4e99
DJ
9514 do_cleanups (back_to);
9515
9516 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9517 }
9518
348e048f
DE
9519 not_found:
9520
5afb4e99
DJ
9521 if (pd == NULL)
9522 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9523 _("could not find partial DIE 0x%x "
9524 "in cache [from module %s]\n"),
5afb4e99
DJ
9525 offset, bfd_get_filename (cu->objfile->obfd));
9526 return pd;
72bf9492
DJ
9527}
9528
abc72ce4
DE
9529/* See if we can figure out if the class lives in a namespace. We do
9530 this by looking for a member function; its demangled name will
9531 contain namespace info, if there is any. */
9532
9533static void
9534guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9535 struct dwarf2_cu *cu)
9536{
9537 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9538 what template types look like, because the demangler
9539 frequently doesn't give the same name as the debug info. We
9540 could fix this by only using the demangled name to get the
9541 prefix (but see comment in read_structure_type). */
9542
9543 struct partial_die_info *real_pdi;
9544 struct partial_die_info *child_pdi;
9545
9546 /* If this DIE (this DIE's specification, if any) has a parent, then
9547 we should not do this. We'll prepend the parent's fully qualified
9548 name when we create the partial symbol. */
9549
9550 real_pdi = struct_pdi;
9551 while (real_pdi->has_specification)
9552 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9553
9554 if (real_pdi->die_parent != NULL)
9555 return;
9556
9557 for (child_pdi = struct_pdi->die_child;
9558 child_pdi != NULL;
9559 child_pdi = child_pdi->die_sibling)
9560 {
9561 if (child_pdi->tag == DW_TAG_subprogram
9562 && child_pdi->linkage_name != NULL)
9563 {
9564 char *actual_class_name
9565 = language_class_name_from_physname (cu->language_defn,
9566 child_pdi->linkage_name);
9567 if (actual_class_name != NULL)
9568 {
9569 struct_pdi->name
9570 = obsavestring (actual_class_name,
9571 strlen (actual_class_name),
9572 &cu->objfile->objfile_obstack);
9573 xfree (actual_class_name);
9574 }
9575 break;
9576 }
9577 }
9578}
9579
72bf9492
DJ
9580/* Adjust PART_DIE before generating a symbol for it. This function
9581 may set the is_external flag or change the DIE's name. */
9582
9583static void
9584fixup_partial_die (struct partial_die_info *part_die,
9585 struct dwarf2_cu *cu)
9586{
abc72ce4
DE
9587 /* Once we've fixed up a die, there's no point in doing so again.
9588 This also avoids a memory leak if we were to call
9589 guess_partial_die_structure_name multiple times. */
9590 if (part_die->fixup_called)
9591 return;
9592
72bf9492
DJ
9593 /* If we found a reference attribute and the DIE has no name, try
9594 to find a name in the referred to DIE. */
9595
9596 if (part_die->name == NULL && part_die->has_specification)
9597 {
9598 struct partial_die_info *spec_die;
72bf9492 9599
10b3939b 9600 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9601
10b3939b 9602 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9603
9604 if (spec_die->name)
9605 {
9606 part_die->name = spec_die->name;
9607
9608 /* Copy DW_AT_external attribute if it is set. */
9609 if (spec_die->is_external)
9610 part_die->is_external = spec_die->is_external;
9611 }
9612 }
9613
9614 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9615
9616 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 9617 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 9618
abc72ce4
DE
9619 /* If there is no parent die to provide a namespace, and there are
9620 children, see if we can determine the namespace from their linkage
9621 name.
9622 NOTE: We need to do this even if cu->has_namespace_info != 0.
9623 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9624 if (cu->language == language_cplus
9625 && dwarf2_per_objfile->types.asection != NULL
9626 && part_die->die_parent == NULL
9627 && part_die->has_children
9628 && (part_die->tag == DW_TAG_class_type
9629 || part_die->tag == DW_TAG_structure_type
9630 || part_die->tag == DW_TAG_union_type))
9631 guess_partial_die_structure_name (part_die, cu);
9632
53832f31
TT
9633 /* GCC might emit a nameless struct or union that has a linkage
9634 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9635 if (part_die->name == NULL
9636 && (part_die->tag == DW_TAG_structure_type
9637 || part_die->tag == DW_TAG_union_type
9638 || part_die->tag == DW_TAG_class_type)
9639 && part_die->linkage_name != NULL)
9640 {
9641 char *demangled;
9642
9643 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9644 if (demangled)
9645 {
9646 part_die->name = obsavestring (demangled, strlen (demangled),
9647 &cu->objfile->objfile_obstack);
9648 xfree (demangled);
9649 }
9650 }
9651
abc72ce4 9652 part_die->fixup_called = 1;
72bf9492
DJ
9653}
9654
a8329558 9655/* Read an attribute value described by an attribute form. */
c906108c 9656
fe1b8b76 9657static gdb_byte *
a8329558 9658read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9659 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9660 struct dwarf2_cu *cu)
c906108c 9661{
e7c27a73 9662 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9663 unsigned int bytes_read;
9664 struct dwarf_block *blk;
9665
a8329558
KW
9666 attr->form = form;
9667 switch (form)
c906108c 9668 {
c906108c 9669 case DW_FORM_ref_addr:
ae411497
TT
9670 if (cu->header.version == 2)
9671 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9672 else
3e43a32a
MS
9673 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9674 &cu->header, &bytes_read);
ae411497
TT
9675 info_ptr += bytes_read;
9676 break;
9677 case DW_FORM_addr:
e7c27a73 9678 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9679 info_ptr += bytes_read;
c906108c
SS
9680 break;
9681 case DW_FORM_block2:
7b5a2f43 9682 blk = dwarf_alloc_block (cu);
c906108c
SS
9683 blk->size = read_2_bytes (abfd, info_ptr);
9684 info_ptr += 2;
9685 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9686 info_ptr += blk->size;
9687 DW_BLOCK (attr) = blk;
9688 break;
9689 case DW_FORM_block4:
7b5a2f43 9690 blk = dwarf_alloc_block (cu);
c906108c
SS
9691 blk->size = read_4_bytes (abfd, info_ptr);
9692 info_ptr += 4;
9693 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9694 info_ptr += blk->size;
9695 DW_BLOCK (attr) = blk;
9696 break;
9697 case DW_FORM_data2:
9698 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9699 info_ptr += 2;
9700 break;
9701 case DW_FORM_data4:
9702 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9703 info_ptr += 4;
9704 break;
9705 case DW_FORM_data8:
9706 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9707 info_ptr += 8;
9708 break;
2dc7f7b3
TT
9709 case DW_FORM_sec_offset:
9710 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9711 info_ptr += bytes_read;
9712 break;
c906108c 9713 case DW_FORM_string:
9b1c24c8 9714 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9715 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9716 info_ptr += bytes_read;
9717 break;
4bdf3d34
JJ
9718 case DW_FORM_strp:
9719 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9720 &bytes_read);
8285870a 9721 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9722 info_ptr += bytes_read;
9723 break;
2dc7f7b3 9724 case DW_FORM_exprloc:
c906108c 9725 case DW_FORM_block:
7b5a2f43 9726 blk = dwarf_alloc_block (cu);
c906108c
SS
9727 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9728 info_ptr += bytes_read;
9729 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9730 info_ptr += blk->size;
9731 DW_BLOCK (attr) = blk;
9732 break;
9733 case DW_FORM_block1:
7b5a2f43 9734 blk = dwarf_alloc_block (cu);
c906108c
SS
9735 blk->size = read_1_byte (abfd, info_ptr);
9736 info_ptr += 1;
9737 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9738 info_ptr += blk->size;
9739 DW_BLOCK (attr) = blk;
9740 break;
9741 case DW_FORM_data1:
9742 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9743 info_ptr += 1;
9744 break;
9745 case DW_FORM_flag:
9746 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9747 info_ptr += 1;
9748 break;
2dc7f7b3
TT
9749 case DW_FORM_flag_present:
9750 DW_UNSND (attr) = 1;
9751 break;
c906108c
SS
9752 case DW_FORM_sdata:
9753 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9754 info_ptr += bytes_read;
9755 break;
9756 case DW_FORM_udata:
9757 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9758 info_ptr += bytes_read;
9759 break;
9760 case DW_FORM_ref1:
10b3939b 9761 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9762 info_ptr += 1;
9763 break;
9764 case DW_FORM_ref2:
10b3939b 9765 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9766 info_ptr += 2;
9767 break;
9768 case DW_FORM_ref4:
10b3939b 9769 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9770 info_ptr += 4;
9771 break;
613e1657 9772 case DW_FORM_ref8:
10b3939b 9773 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9774 info_ptr += 8;
9775 break;
55f1336d 9776 case DW_FORM_ref_sig8:
348e048f
DE
9777 /* Convert the signature to something we can record in DW_UNSND
9778 for later lookup.
9779 NOTE: This is NULL if the type wasn't found. */
9780 DW_SIGNATURED_TYPE (attr) =
9781 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9782 info_ptr += 8;
9783 break;
c906108c 9784 case DW_FORM_ref_udata:
10b3939b
DJ
9785 DW_ADDR (attr) = (cu->header.offset
9786 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9787 info_ptr += bytes_read;
9788 break;
c906108c 9789 case DW_FORM_indirect:
a8329558
KW
9790 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9791 info_ptr += bytes_read;
e7c27a73 9792 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9793 break;
c906108c 9794 default:
8a3fe4f8 9795 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9796 dwarf_form_name (form),
9797 bfd_get_filename (abfd));
c906108c 9798 }
28e94949
JB
9799
9800 /* We have seen instances where the compiler tried to emit a byte
9801 size attribute of -1 which ended up being encoded as an unsigned
9802 0xffffffff. Although 0xffffffff is technically a valid size value,
9803 an object of this size seems pretty unlikely so we can relatively
9804 safely treat these cases as if the size attribute was invalid and
9805 treat them as zero by default. */
9806 if (attr->name == DW_AT_byte_size
9807 && form == DW_FORM_data4
9808 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9809 {
9810 complaint
9811 (&symfile_complaints,
43bbcdc2
PH
9812 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9813 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9814 DW_UNSND (attr) = 0;
9815 }
28e94949 9816
c906108c
SS
9817 return info_ptr;
9818}
9819
a8329558
KW
9820/* Read an attribute described by an abbreviated attribute. */
9821
fe1b8b76 9822static gdb_byte *
a8329558 9823read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9824 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9825{
9826 attr->name = abbrev->name;
e7c27a73 9827 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9828}
9829
0963b4bd 9830/* Read dwarf information from a buffer. */
c906108c
SS
9831
9832static unsigned int
fe1b8b76 9833read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9834{
fe1b8b76 9835 return bfd_get_8 (abfd, buf);
c906108c
SS
9836}
9837
9838static int
fe1b8b76 9839read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9840{
fe1b8b76 9841 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9842}
9843
9844static unsigned int
fe1b8b76 9845read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9846{
fe1b8b76 9847 return bfd_get_16 (abfd, buf);
c906108c
SS
9848}
9849
9850static int
fe1b8b76 9851read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9852{
fe1b8b76 9853 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9854}
9855
9856static unsigned int
fe1b8b76 9857read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9858{
fe1b8b76 9859 return bfd_get_32 (abfd, buf);
c906108c
SS
9860}
9861
9862static int
fe1b8b76 9863read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9864{
fe1b8b76 9865 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9866}
9867
93311388 9868static ULONGEST
fe1b8b76 9869read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9870{
fe1b8b76 9871 return bfd_get_64 (abfd, buf);
c906108c
SS
9872}
9873
9874static CORE_ADDR
fe1b8b76 9875read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9876 unsigned int *bytes_read)
c906108c 9877{
e7c27a73 9878 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9879 CORE_ADDR retval = 0;
9880
107d2387 9881 if (cu_header->signed_addr_p)
c906108c 9882 {
107d2387
AC
9883 switch (cu_header->addr_size)
9884 {
9885 case 2:
fe1b8b76 9886 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9887 break;
9888 case 4:
fe1b8b76 9889 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9890 break;
9891 case 8:
fe1b8b76 9892 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9893 break;
9894 default:
8e65ff28 9895 internal_error (__FILE__, __LINE__,
e2e0b3e5 9896 _("read_address: bad switch, signed [in module %s]"),
659b0389 9897 bfd_get_filename (abfd));
107d2387
AC
9898 }
9899 }
9900 else
9901 {
9902 switch (cu_header->addr_size)
9903 {
9904 case 2:
fe1b8b76 9905 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9906 break;
9907 case 4:
fe1b8b76 9908 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9909 break;
9910 case 8:
fe1b8b76 9911 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9912 break;
9913 default:
8e65ff28 9914 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9915 _("read_address: bad switch, "
9916 "unsigned [in module %s]"),
659b0389 9917 bfd_get_filename (abfd));
107d2387 9918 }
c906108c 9919 }
64367e0a 9920
107d2387
AC
9921 *bytes_read = cu_header->addr_size;
9922 return retval;
c906108c
SS
9923}
9924
f7ef9339 9925/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9926 specification allows the initial length to take up either 4 bytes
9927 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9928 bytes describe the length and all offsets will be 8 bytes in length
9929 instead of 4.
9930
f7ef9339
KB
9931 An older, non-standard 64-bit format is also handled by this
9932 function. The older format in question stores the initial length
9933 as an 8-byte quantity without an escape value. Lengths greater
9934 than 2^32 aren't very common which means that the initial 4 bytes
9935 is almost always zero. Since a length value of zero doesn't make
9936 sense for the 32-bit format, this initial zero can be considered to
9937 be an escape value which indicates the presence of the older 64-bit
9938 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9939 greater than 4GB. If it becomes necessary to handle lengths
9940 somewhat larger than 4GB, we could allow other small values (such
9941 as the non-sensical values of 1, 2, and 3) to also be used as
9942 escape values indicating the presence of the old format.
f7ef9339 9943
917c78fc
MK
9944 The value returned via bytes_read should be used to increment the
9945 relevant pointer after calling read_initial_length().
c764a876 9946
613e1657
KB
9947 [ Note: read_initial_length() and read_offset() are based on the
9948 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9949 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9950 from:
9951
f7ef9339 9952 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9953
613e1657
KB
9954 This document is only a draft and is subject to change. (So beware.)
9955
f7ef9339 9956 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9957 determined empirically by examining 64-bit ELF files produced by
9958 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9959
9960 - Kevin, July 16, 2002
613e1657
KB
9961 ] */
9962
9963static LONGEST
c764a876 9964read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9965{
fe1b8b76 9966 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9967
dd373385 9968 if (length == 0xffffffff)
613e1657 9969 {
fe1b8b76 9970 length = bfd_get_64 (abfd, buf + 4);
613e1657 9971 *bytes_read = 12;
613e1657 9972 }
dd373385 9973 else if (length == 0)
f7ef9339 9974 {
dd373385 9975 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9976 length = bfd_get_64 (abfd, buf);
f7ef9339 9977 *bytes_read = 8;
f7ef9339 9978 }
613e1657
KB
9979 else
9980 {
9981 *bytes_read = 4;
613e1657
KB
9982 }
9983
c764a876
DE
9984 return length;
9985}
dd373385 9986
c764a876
DE
9987/* Cover function for read_initial_length.
9988 Returns the length of the object at BUF, and stores the size of the
9989 initial length in *BYTES_READ and stores the size that offsets will be in
9990 *OFFSET_SIZE.
9991 If the initial length size is not equivalent to that specified in
9992 CU_HEADER then issue a complaint.
9993 This is useful when reading non-comp-unit headers. */
dd373385 9994
c764a876
DE
9995static LONGEST
9996read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9997 const struct comp_unit_head *cu_header,
9998 unsigned int *bytes_read,
9999 unsigned int *offset_size)
10000{
10001 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10002
10003 gdb_assert (cu_header->initial_length_size == 4
10004 || cu_header->initial_length_size == 8
10005 || cu_header->initial_length_size == 12);
10006
10007 if (cu_header->initial_length_size != *bytes_read)
10008 complaint (&symfile_complaints,
10009 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10010
c764a876 10011 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10012 return length;
613e1657
KB
10013}
10014
10015/* Read an offset from the data stream. The size of the offset is
917c78fc 10016 given by cu_header->offset_size. */
613e1657
KB
10017
10018static LONGEST
fe1b8b76 10019read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10020 unsigned int *bytes_read)
c764a876
DE
10021{
10022 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10023
c764a876
DE
10024 *bytes_read = cu_header->offset_size;
10025 return offset;
10026}
10027
10028/* Read an offset from the data stream. */
10029
10030static LONGEST
10031read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10032{
10033 LONGEST retval = 0;
10034
c764a876 10035 switch (offset_size)
613e1657
KB
10036 {
10037 case 4:
fe1b8b76 10038 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10039 break;
10040 case 8:
fe1b8b76 10041 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10042 break;
10043 default:
8e65ff28 10044 internal_error (__FILE__, __LINE__,
c764a876 10045 _("read_offset_1: bad switch [in module %s]"),
659b0389 10046 bfd_get_filename (abfd));
613e1657
KB
10047 }
10048
917c78fc 10049 return retval;
613e1657
KB
10050}
10051
fe1b8b76
JB
10052static gdb_byte *
10053read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10054{
10055 /* If the size of a host char is 8 bits, we can return a pointer
10056 to the buffer, otherwise we have to copy the data to a buffer
10057 allocated on the temporary obstack. */
4bdf3d34 10058 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10059 return buf;
c906108c
SS
10060}
10061
10062static char *
9b1c24c8 10063read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10064{
10065 /* If the size of a host char is 8 bits, we can return a pointer
10066 to the string, otherwise we have to copy the string to a buffer
10067 allocated on the temporary obstack. */
4bdf3d34 10068 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10069 if (*buf == '\0')
10070 {
10071 *bytes_read_ptr = 1;
10072 return NULL;
10073 }
fe1b8b76
JB
10074 *bytes_read_ptr = strlen ((char *) buf) + 1;
10075 return (char *) buf;
4bdf3d34
JJ
10076}
10077
10078static char *
fe1b8b76 10079read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
10080 const struct comp_unit_head *cu_header,
10081 unsigned int *bytes_read_ptr)
10082{
c764a876 10083 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 10084
be391dca 10085 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10086 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 10087 {
8a3fe4f8 10088 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 10089 bfd_get_filename (abfd));
4bdf3d34 10090 return NULL;
c906108c 10091 }
dce234bc 10092 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 10093 {
3e43a32a
MS
10094 error (_("DW_FORM_strp pointing outside of "
10095 ".debug_str section [in module %s]"),
10096 bfd_get_filename (abfd));
c906108c
SS
10097 return NULL;
10098 }
4bdf3d34 10099 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10100 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10101 return NULL;
dce234bc 10102 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10103}
10104
ce5d95e1 10105static unsigned long
fe1b8b76 10106read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10107{
ce5d95e1
JB
10108 unsigned long result;
10109 unsigned int num_read;
c906108c
SS
10110 int i, shift;
10111 unsigned char byte;
10112
10113 result = 0;
10114 shift = 0;
10115 num_read = 0;
10116 i = 0;
10117 while (1)
10118 {
fe1b8b76 10119 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10120 buf++;
10121 num_read++;
ce5d95e1 10122 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10123 if ((byte & 128) == 0)
10124 {
10125 break;
10126 }
10127 shift += 7;
10128 }
10129 *bytes_read_ptr = num_read;
10130 return result;
10131}
10132
ce5d95e1 10133static long
fe1b8b76 10134read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10135{
ce5d95e1 10136 long result;
77e0b926 10137 int i, shift, num_read;
c906108c
SS
10138 unsigned char byte;
10139
10140 result = 0;
10141 shift = 0;
c906108c
SS
10142 num_read = 0;
10143 i = 0;
10144 while (1)
10145 {
fe1b8b76 10146 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10147 buf++;
10148 num_read++;
ce5d95e1 10149 result |= ((long)(byte & 127) << shift);
c906108c
SS
10150 shift += 7;
10151 if ((byte & 128) == 0)
10152 {
10153 break;
10154 }
10155 }
77e0b926
DJ
10156 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10157 result |= -(((long)1) << shift);
c906108c
SS
10158 *bytes_read_ptr = num_read;
10159 return result;
10160}
10161
4bb7a0a7
DJ
10162/* Return a pointer to just past the end of an LEB128 number in BUF. */
10163
fe1b8b76
JB
10164static gdb_byte *
10165skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10166{
10167 int byte;
10168
10169 while (1)
10170 {
fe1b8b76 10171 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10172 buf++;
10173 if ((byte & 128) == 0)
10174 return buf;
10175 }
10176}
10177
c906108c 10178static void
e142c38c 10179set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10180{
10181 switch (lang)
10182 {
10183 case DW_LANG_C89:
76bee0cc 10184 case DW_LANG_C99:
c906108c 10185 case DW_LANG_C:
e142c38c 10186 cu->language = language_c;
c906108c
SS
10187 break;
10188 case DW_LANG_C_plus_plus:
e142c38c 10189 cu->language = language_cplus;
c906108c 10190 break;
6aecb9c2
JB
10191 case DW_LANG_D:
10192 cu->language = language_d;
10193 break;
c906108c
SS
10194 case DW_LANG_Fortran77:
10195 case DW_LANG_Fortran90:
b21b22e0 10196 case DW_LANG_Fortran95:
e142c38c 10197 cu->language = language_fortran;
c906108c
SS
10198 break;
10199 case DW_LANG_Mips_Assembler:
e142c38c 10200 cu->language = language_asm;
c906108c 10201 break;
bebd888e 10202 case DW_LANG_Java:
e142c38c 10203 cu->language = language_java;
bebd888e 10204 break;
c906108c 10205 case DW_LANG_Ada83:
8aaf0b47 10206 case DW_LANG_Ada95:
bc5f45f8
JB
10207 cu->language = language_ada;
10208 break;
72019c9c
GM
10209 case DW_LANG_Modula2:
10210 cu->language = language_m2;
10211 break;
fe8e67fd
PM
10212 case DW_LANG_Pascal83:
10213 cu->language = language_pascal;
10214 break;
22566fbd
DJ
10215 case DW_LANG_ObjC:
10216 cu->language = language_objc;
10217 break;
c906108c
SS
10218 case DW_LANG_Cobol74:
10219 case DW_LANG_Cobol85:
c906108c 10220 default:
e142c38c 10221 cu->language = language_minimal;
c906108c
SS
10222 break;
10223 }
e142c38c 10224 cu->language_defn = language_def (cu->language);
c906108c
SS
10225}
10226
10227/* Return the named attribute or NULL if not there. */
10228
10229static struct attribute *
e142c38c 10230dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10231{
10232 unsigned int i;
10233 struct attribute *spec = NULL;
10234
10235 for (i = 0; i < die->num_attrs; ++i)
10236 {
10237 if (die->attrs[i].name == name)
10b3939b 10238 return &die->attrs[i];
c906108c
SS
10239 if (die->attrs[i].name == DW_AT_specification
10240 || die->attrs[i].name == DW_AT_abstract_origin)
10241 spec = &die->attrs[i];
10242 }
c906108c 10243
10b3939b 10244 if (spec)
f2f0e013
DJ
10245 {
10246 die = follow_die_ref (die, spec, &cu);
10247 return dwarf2_attr (die, name, cu);
10248 }
c5aa993b 10249
c906108c
SS
10250 return NULL;
10251}
10252
348e048f
DE
10253/* Return the named attribute or NULL if not there,
10254 but do not follow DW_AT_specification, etc.
10255 This is for use in contexts where we're reading .debug_types dies.
10256 Following DW_AT_specification, DW_AT_abstract_origin will take us
10257 back up the chain, and we want to go down. */
10258
10259static struct attribute *
10260dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10261 struct dwarf2_cu *cu)
10262{
10263 unsigned int i;
10264
10265 for (i = 0; i < die->num_attrs; ++i)
10266 if (die->attrs[i].name == name)
10267 return &die->attrs[i];
10268
10269 return NULL;
10270}
10271
05cf31d1
JB
10272/* Return non-zero iff the attribute NAME is defined for the given DIE,
10273 and holds a non-zero value. This function should only be used for
2dc7f7b3 10274 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10275
10276static int
10277dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10278{
10279 struct attribute *attr = dwarf2_attr (die, name, cu);
10280
10281 return (attr && DW_UNSND (attr));
10282}
10283
3ca72b44 10284static int
e142c38c 10285die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10286{
05cf31d1
JB
10287 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10288 which value is non-zero. However, we have to be careful with
10289 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10290 (via dwarf2_flag_true_p) follows this attribute. So we may
10291 end up accidently finding a declaration attribute that belongs
10292 to a different DIE referenced by the specification attribute,
10293 even though the given DIE does not have a declaration attribute. */
10294 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10295 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10296}
10297
63d06c5c 10298/* Return the die giving the specification for DIE, if there is
f2f0e013 10299 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10300 containing the return value on output. If there is no
10301 specification, but there is an abstract origin, that is
10302 returned. */
63d06c5c
DC
10303
10304static struct die_info *
f2f0e013 10305die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10306{
f2f0e013
DJ
10307 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10308 *spec_cu);
63d06c5c 10309
edb3359d
DJ
10310 if (spec_attr == NULL)
10311 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10312
63d06c5c
DC
10313 if (spec_attr == NULL)
10314 return NULL;
10315 else
f2f0e013 10316 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10317}
c906108c 10318
debd256d 10319/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10320 refers to.
10321 NOTE: This is also used as a "cleanup" function. */
10322
debd256d
JB
10323static void
10324free_line_header (struct line_header *lh)
10325{
10326 if (lh->standard_opcode_lengths)
a8bc7b56 10327 xfree (lh->standard_opcode_lengths);
debd256d
JB
10328
10329 /* Remember that all the lh->file_names[i].name pointers are
10330 pointers into debug_line_buffer, and don't need to be freed. */
10331 if (lh->file_names)
a8bc7b56 10332 xfree (lh->file_names);
debd256d
JB
10333
10334 /* Similarly for the include directory names. */
10335 if (lh->include_dirs)
a8bc7b56 10336 xfree (lh->include_dirs);
debd256d 10337
a8bc7b56 10338 xfree (lh);
debd256d
JB
10339}
10340
debd256d 10341/* Add an entry to LH's include directory table. */
ae2de4f8 10342
debd256d
JB
10343static void
10344add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10345{
debd256d
JB
10346 /* Grow the array if necessary. */
10347 if (lh->include_dirs_size == 0)
c5aa993b 10348 {
debd256d
JB
10349 lh->include_dirs_size = 1; /* for testing */
10350 lh->include_dirs = xmalloc (lh->include_dirs_size
10351 * sizeof (*lh->include_dirs));
10352 }
10353 else if (lh->num_include_dirs >= lh->include_dirs_size)
10354 {
10355 lh->include_dirs_size *= 2;
10356 lh->include_dirs = xrealloc (lh->include_dirs,
10357 (lh->include_dirs_size
10358 * sizeof (*lh->include_dirs)));
c5aa993b 10359 }
c906108c 10360
debd256d
JB
10361 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10362}
6e70227d 10363
debd256d 10364/* Add an entry to LH's file name table. */
ae2de4f8 10365
debd256d
JB
10366static void
10367add_file_name (struct line_header *lh,
10368 char *name,
10369 unsigned int dir_index,
10370 unsigned int mod_time,
10371 unsigned int length)
10372{
10373 struct file_entry *fe;
10374
10375 /* Grow the array if necessary. */
10376 if (lh->file_names_size == 0)
10377 {
10378 lh->file_names_size = 1; /* for testing */
10379 lh->file_names = xmalloc (lh->file_names_size
10380 * sizeof (*lh->file_names));
10381 }
10382 else if (lh->num_file_names >= lh->file_names_size)
10383 {
10384 lh->file_names_size *= 2;
10385 lh->file_names = xrealloc (lh->file_names,
10386 (lh->file_names_size
10387 * sizeof (*lh->file_names)));
10388 }
10389
10390 fe = &lh->file_names[lh->num_file_names++];
10391 fe->name = name;
10392 fe->dir_index = dir_index;
10393 fe->mod_time = mod_time;
10394 fe->length = length;
aaa75496 10395 fe->included_p = 0;
cb1df416 10396 fe->symtab = NULL;
debd256d 10397}
6e70227d 10398
debd256d 10399/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10400 .debug_line, according to the endianness of ABFD. Return a pointer
10401 to a struct line_header, allocated using xmalloc.
debd256d
JB
10402
10403 NOTE: the strings in the include directory and file name tables of
10404 the returned object point into debug_line_buffer, and must not be
10405 freed. */
ae2de4f8 10406
debd256d
JB
10407static struct line_header *
10408dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10409 struct dwarf2_cu *cu)
debd256d
JB
10410{
10411 struct cleanup *back_to;
10412 struct line_header *lh;
fe1b8b76 10413 gdb_byte *line_ptr;
c764a876 10414 unsigned int bytes_read, offset_size;
debd256d
JB
10415 int i;
10416 char *cur_dir, *cur_file;
10417
be391dca 10418 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10419 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10420 {
e2e0b3e5 10421 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10422 return 0;
10423 }
10424
a738430d
MK
10425 /* Make sure that at least there's room for the total_length field.
10426 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10427 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10428 {
4d3c2250 10429 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10430 return 0;
10431 }
10432
10433 lh = xmalloc (sizeof (*lh));
10434 memset (lh, 0, sizeof (*lh));
10435 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10436 (void *) lh);
10437
dce234bc 10438 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10439
a738430d 10440 /* Read in the header. */
6e70227d 10441 lh->total_length =
c764a876
DE
10442 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10443 &bytes_read, &offset_size);
debd256d 10444 line_ptr += bytes_read;
dce234bc
PP
10445 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10446 + dwarf2_per_objfile->line.size))
debd256d 10447 {
4d3c2250 10448 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10449 return 0;
10450 }
10451 lh->statement_program_end = line_ptr + lh->total_length;
10452 lh->version = read_2_bytes (abfd, line_ptr);
10453 line_ptr += 2;
c764a876
DE
10454 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10455 line_ptr += offset_size;
debd256d
JB
10456 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10457 line_ptr += 1;
2dc7f7b3
TT
10458 if (lh->version >= 4)
10459 {
10460 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10461 line_ptr += 1;
10462 }
10463 else
10464 lh->maximum_ops_per_instruction = 1;
10465
10466 if (lh->maximum_ops_per_instruction == 0)
10467 {
10468 lh->maximum_ops_per_instruction = 1;
10469 complaint (&symfile_complaints,
3e43a32a
MS
10470 _("invalid maximum_ops_per_instruction "
10471 "in `.debug_line' section"));
2dc7f7b3
TT
10472 }
10473
debd256d
JB
10474 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10475 line_ptr += 1;
10476 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10477 line_ptr += 1;
10478 lh->line_range = read_1_byte (abfd, line_ptr);
10479 line_ptr += 1;
10480 lh->opcode_base = read_1_byte (abfd, line_ptr);
10481 line_ptr += 1;
10482 lh->standard_opcode_lengths
fe1b8b76 10483 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10484
10485 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10486 for (i = 1; i < lh->opcode_base; ++i)
10487 {
10488 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10489 line_ptr += 1;
10490 }
10491
a738430d 10492 /* Read directory table. */
9b1c24c8 10493 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10494 {
10495 line_ptr += bytes_read;
10496 add_include_dir (lh, cur_dir);
10497 }
10498 line_ptr += bytes_read;
10499
a738430d 10500 /* Read file name table. */
9b1c24c8 10501 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10502 {
10503 unsigned int dir_index, mod_time, length;
10504
10505 line_ptr += bytes_read;
10506 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10507 line_ptr += bytes_read;
10508 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10509 line_ptr += bytes_read;
10510 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10511 line_ptr += bytes_read;
10512
10513 add_file_name (lh, cur_file, dir_index, mod_time, length);
10514 }
10515 line_ptr += bytes_read;
6e70227d 10516 lh->statement_program_start = line_ptr;
debd256d 10517
dce234bc
PP
10518 if (line_ptr > (dwarf2_per_objfile->line.buffer
10519 + dwarf2_per_objfile->line.size))
4d3c2250 10520 complaint (&symfile_complaints,
3e43a32a
MS
10521 _("line number info header doesn't "
10522 "fit in `.debug_line' section"));
debd256d
JB
10523
10524 discard_cleanups (back_to);
10525 return lh;
10526}
c906108c 10527
5fb290d7
DJ
10528/* This function exists to work around a bug in certain compilers
10529 (particularly GCC 2.95), in which the first line number marker of a
10530 function does not show up until after the prologue, right before
10531 the second line number marker. This function shifts ADDRESS down
10532 to the beginning of the function if necessary, and is called on
10533 addresses passed to record_line. */
10534
10535static CORE_ADDR
e142c38c 10536check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10537{
10538 struct function_range *fn;
10539
10540 /* Find the function_range containing address. */
e142c38c 10541 if (!cu->first_fn)
5fb290d7
DJ
10542 return address;
10543
e142c38c
DJ
10544 if (!cu->cached_fn)
10545 cu->cached_fn = cu->first_fn;
5fb290d7 10546
e142c38c 10547 fn = cu->cached_fn;
5fb290d7
DJ
10548 while (fn)
10549 if (fn->lowpc <= address && fn->highpc > address)
10550 goto found;
10551 else
10552 fn = fn->next;
10553
e142c38c
DJ
10554 fn = cu->first_fn;
10555 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10556 if (fn->lowpc <= address && fn->highpc > address)
10557 goto found;
10558 else
10559 fn = fn->next;
10560
10561 return address;
10562
10563 found:
10564 if (fn->seen_line)
10565 return address;
10566 if (address != fn->lowpc)
4d3c2250 10567 complaint (&symfile_complaints,
e2e0b3e5 10568 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10569 (unsigned long) address, fn->name);
5fb290d7
DJ
10570 fn->seen_line = 1;
10571 return fn->lowpc;
10572}
10573
c6da4cef
DE
10574/* Subroutine of dwarf_decode_lines to simplify it.
10575 Return the file name of the psymtab for included file FILE_INDEX
10576 in line header LH of PST.
10577 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10578 If space for the result is malloc'd, it will be freed by a cleanup.
10579 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10580
10581static char *
10582psymtab_include_file_name (const struct line_header *lh, int file_index,
10583 const struct partial_symtab *pst,
10584 const char *comp_dir)
10585{
10586 const struct file_entry fe = lh->file_names [file_index];
10587 char *include_name = fe.name;
10588 char *include_name_to_compare = include_name;
10589 char *dir_name = NULL;
72b9f47f
TT
10590 const char *pst_filename;
10591 char *copied_name = NULL;
c6da4cef
DE
10592 int file_is_pst;
10593
10594 if (fe.dir_index)
10595 dir_name = lh->include_dirs[fe.dir_index - 1];
10596
10597 if (!IS_ABSOLUTE_PATH (include_name)
10598 && (dir_name != NULL || comp_dir != NULL))
10599 {
10600 /* Avoid creating a duplicate psymtab for PST.
10601 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10602 Before we do the comparison, however, we need to account
10603 for DIR_NAME and COMP_DIR.
10604 First prepend dir_name (if non-NULL). If we still don't
10605 have an absolute path prepend comp_dir (if non-NULL).
10606 However, the directory we record in the include-file's
10607 psymtab does not contain COMP_DIR (to match the
10608 corresponding symtab(s)).
10609
10610 Example:
10611
10612 bash$ cd /tmp
10613 bash$ gcc -g ./hello.c
10614 include_name = "hello.c"
10615 dir_name = "."
10616 DW_AT_comp_dir = comp_dir = "/tmp"
10617 DW_AT_name = "./hello.c" */
10618
10619 if (dir_name != NULL)
10620 {
10621 include_name = concat (dir_name, SLASH_STRING,
10622 include_name, (char *)NULL);
10623 include_name_to_compare = include_name;
10624 make_cleanup (xfree, include_name);
10625 }
10626 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10627 {
10628 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10629 include_name, (char *)NULL);
10630 }
10631 }
10632
10633 pst_filename = pst->filename;
10634 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10635 {
72b9f47f
TT
10636 copied_name = concat (pst->dirname, SLASH_STRING,
10637 pst_filename, (char *)NULL);
10638 pst_filename = copied_name;
c6da4cef
DE
10639 }
10640
1e3fad37 10641 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10642
10643 if (include_name_to_compare != include_name)
10644 xfree (include_name_to_compare);
72b9f47f
TT
10645 if (copied_name != NULL)
10646 xfree (copied_name);
c6da4cef
DE
10647
10648 if (file_is_pst)
10649 return NULL;
10650 return include_name;
10651}
10652
c91513d8
PP
10653/* Ignore this record_line request. */
10654
10655static void
10656noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10657{
10658 return;
10659}
10660
aaa75496
JB
10661/* Decode the Line Number Program (LNP) for the given line_header
10662 structure and CU. The actual information extracted and the type
10663 of structures created from the LNP depends on the value of PST.
10664
10665 1. If PST is NULL, then this procedure uses the data from the program
10666 to create all necessary symbol tables, and their linetables.
6e70227d 10667
aaa75496
JB
10668 2. If PST is not NULL, this procedure reads the program to determine
10669 the list of files included by the unit represented by PST, and
c6da4cef
DE
10670 builds all the associated partial symbol tables.
10671
10672 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10673 It is used for relative paths in the line table.
10674 NOTE: When processing partial symtabs (pst != NULL),
10675 comp_dir == pst->dirname.
10676
10677 NOTE: It is important that psymtabs have the same file name (via strcmp)
10678 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10679 symtab we don't use it in the name of the psymtabs we create.
10680 E.g. expand_line_sal requires this when finding psymtabs to expand.
10681 A good testcase for this is mb-inline.exp. */
debd256d 10682
c906108c 10683static void
72b9f47f 10684dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10685 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10686{
a8c50c1f 10687 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10688 gdb_byte *line_end;
a8c50c1f 10689 unsigned int bytes_read, extended_len;
c906108c 10690 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10691 CORE_ADDR baseaddr;
10692 struct objfile *objfile = cu->objfile;
fbf65064 10693 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10694 const int decode_for_pst_p = (pst != NULL);
cb1df416 10695 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10696 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10697 = record_line;
e142c38c
DJ
10698
10699 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10700
debd256d
JB
10701 line_ptr = lh->statement_program_start;
10702 line_end = lh->statement_program_end;
c906108c
SS
10703
10704 /* Read the statement sequences until there's nothing left. */
10705 while (line_ptr < line_end)
10706 {
10707 /* state machine registers */
10708 CORE_ADDR address = 0;
10709 unsigned int file = 1;
10710 unsigned int line = 1;
10711 unsigned int column = 0;
debd256d 10712 int is_stmt = lh->default_is_stmt;
c906108c
SS
10713 int basic_block = 0;
10714 int end_sequence = 0;
fbf65064 10715 CORE_ADDR addr;
2dc7f7b3 10716 unsigned char op_index = 0;
c906108c 10717
aaa75496 10718 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10719 {
aaa75496 10720 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10721 /* lh->include_dirs and lh->file_names are 0-based, but the
10722 directory and file name numbers in the statement program
10723 are 1-based. */
10724 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10725 char *dir = NULL;
a738430d 10726
debd256d
JB
10727 if (fe->dir_index)
10728 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10729
10730 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10731 }
10732
a738430d 10733 /* Decode the table. */
c5aa993b 10734 while (!end_sequence)
c906108c
SS
10735 {
10736 op_code = read_1_byte (abfd, line_ptr);
10737 line_ptr += 1;
59205f5a
JB
10738 if (line_ptr > line_end)
10739 {
10740 dwarf2_debug_line_missing_end_sequence_complaint ();
10741 break;
10742 }
9aa1fe7e 10743
debd256d 10744 if (op_code >= lh->opcode_base)
6e70227d 10745 {
a738430d 10746 /* Special operand. */
debd256d 10747 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10748 address += (((op_index + (adj_opcode / lh->line_range))
10749 / lh->maximum_ops_per_instruction)
10750 * lh->minimum_instruction_length);
10751 op_index = ((op_index + (adj_opcode / lh->line_range))
10752 % lh->maximum_ops_per_instruction);
debd256d 10753 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10754 if (lh->num_file_names < file || file == 0)
25e43795 10755 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10756 /* For now we ignore lines not starting on an
10757 instruction boundary. */
10758 else if (op_index == 0)
25e43795
DJ
10759 {
10760 lh->file_names[file - 1].included_p = 1;
ca5f395d 10761 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10762 {
10763 if (last_subfile != current_subfile)
10764 {
10765 addr = gdbarch_addr_bits_remove (gdbarch, address);
10766 if (last_subfile)
c91513d8 10767 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10768 last_subfile = current_subfile;
10769 }
25e43795 10770 /* Append row to matrix using current values. */
fbf65064
UW
10771 addr = check_cu_functions (address, cu);
10772 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10773 (*p_record_line) (current_subfile, line, addr);
366da635 10774 }
25e43795 10775 }
ca5f395d 10776 basic_block = 0;
9aa1fe7e
GK
10777 }
10778 else switch (op_code)
c906108c
SS
10779 {
10780 case DW_LNS_extended_op:
3e43a32a
MS
10781 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10782 &bytes_read);
473b7be6 10783 line_ptr += bytes_read;
a8c50c1f 10784 extended_end = line_ptr + extended_len;
c906108c
SS
10785 extended_op = read_1_byte (abfd, line_ptr);
10786 line_ptr += 1;
10787 switch (extended_op)
10788 {
10789 case DW_LNE_end_sequence:
c91513d8 10790 p_record_line = record_line;
c906108c 10791 end_sequence = 1;
c906108c
SS
10792 break;
10793 case DW_LNE_set_address:
e7c27a73 10794 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10795
10796 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10797 {
10798 /* This line table is for a function which has been
10799 GCd by the linker. Ignore it. PR gdb/12528 */
10800
10801 long line_offset
10802 = line_ptr - dwarf2_per_objfile->line.buffer;
10803
10804 complaint (&symfile_complaints,
10805 _(".debug_line address at offset 0x%lx is 0 "
10806 "[in module %s]"),
10807 line_offset, cu->objfile->name);
10808 p_record_line = noop_record_line;
10809 }
10810
2dc7f7b3 10811 op_index = 0;
107d2387
AC
10812 line_ptr += bytes_read;
10813 address += baseaddr;
c906108c
SS
10814 break;
10815 case DW_LNE_define_file:
debd256d
JB
10816 {
10817 char *cur_file;
10818 unsigned int dir_index, mod_time, length;
6e70227d 10819
3e43a32a
MS
10820 cur_file = read_direct_string (abfd, line_ptr,
10821 &bytes_read);
debd256d
JB
10822 line_ptr += bytes_read;
10823 dir_index =
10824 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10825 line_ptr += bytes_read;
10826 mod_time =
10827 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10828 line_ptr += bytes_read;
10829 length =
10830 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10831 line_ptr += bytes_read;
10832 add_file_name (lh, cur_file, dir_index, mod_time, length);
10833 }
c906108c 10834 break;
d0c6ba3d
CC
10835 case DW_LNE_set_discriminator:
10836 /* The discriminator is not interesting to the debugger;
10837 just ignore it. */
10838 line_ptr = extended_end;
10839 break;
c906108c 10840 default:
4d3c2250 10841 complaint (&symfile_complaints,
e2e0b3e5 10842 _("mangled .debug_line section"));
debd256d 10843 return;
c906108c 10844 }
a8c50c1f
DJ
10845 /* Make sure that we parsed the extended op correctly. If e.g.
10846 we expected a different address size than the producer used,
10847 we may have read the wrong number of bytes. */
10848 if (line_ptr != extended_end)
10849 {
10850 complaint (&symfile_complaints,
10851 _("mangled .debug_line section"));
10852 return;
10853 }
c906108c
SS
10854 break;
10855 case DW_LNS_copy:
59205f5a 10856 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10857 dwarf2_debug_line_missing_file_complaint ();
10858 else
366da635 10859 {
25e43795 10860 lh->file_names[file - 1].included_p = 1;
ca5f395d 10861 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10862 {
10863 if (last_subfile != current_subfile)
10864 {
10865 addr = gdbarch_addr_bits_remove (gdbarch, address);
10866 if (last_subfile)
c91513d8 10867 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10868 last_subfile = current_subfile;
10869 }
10870 addr = check_cu_functions (address, cu);
10871 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10872 (*p_record_line) (current_subfile, line, addr);
fbf65064 10873 }
366da635 10874 }
c906108c
SS
10875 basic_block = 0;
10876 break;
10877 case DW_LNS_advance_pc:
2dc7f7b3
TT
10878 {
10879 CORE_ADDR adjust
10880 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10881
10882 address += (((op_index + adjust)
10883 / lh->maximum_ops_per_instruction)
10884 * lh->minimum_instruction_length);
10885 op_index = ((op_index + adjust)
10886 % lh->maximum_ops_per_instruction);
10887 line_ptr += bytes_read;
10888 }
c906108c
SS
10889 break;
10890 case DW_LNS_advance_line:
10891 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10892 line_ptr += bytes_read;
10893 break;
10894 case DW_LNS_set_file:
debd256d 10895 {
a738430d
MK
10896 /* The arrays lh->include_dirs and lh->file_names are
10897 0-based, but the directory and file name numbers in
10898 the statement program are 1-based. */
debd256d 10899 struct file_entry *fe;
4f1520fb 10900 char *dir = NULL;
a738430d 10901
debd256d
JB
10902 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10903 line_ptr += bytes_read;
59205f5a 10904 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10905 dwarf2_debug_line_missing_file_complaint ();
10906 else
10907 {
10908 fe = &lh->file_names[file - 1];
10909 if (fe->dir_index)
10910 dir = lh->include_dirs[fe->dir_index - 1];
10911 if (!decode_for_pst_p)
10912 {
10913 last_subfile = current_subfile;
10914 dwarf2_start_subfile (fe->name, dir, comp_dir);
10915 }
10916 }
debd256d 10917 }
c906108c
SS
10918 break;
10919 case DW_LNS_set_column:
10920 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10921 line_ptr += bytes_read;
10922 break;
10923 case DW_LNS_negate_stmt:
10924 is_stmt = (!is_stmt);
10925 break;
10926 case DW_LNS_set_basic_block:
10927 basic_block = 1;
10928 break;
c2c6d25f
JM
10929 /* Add to the address register of the state machine the
10930 address increment value corresponding to special opcode
a738430d
MK
10931 255. I.e., this value is scaled by the minimum
10932 instruction length since special opcode 255 would have
b021a221 10933 scaled the increment. */
c906108c 10934 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10935 {
10936 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10937
10938 address += (((op_index + adjust)
10939 / lh->maximum_ops_per_instruction)
10940 * lh->minimum_instruction_length);
10941 op_index = ((op_index + adjust)
10942 % lh->maximum_ops_per_instruction);
10943 }
c906108c
SS
10944 break;
10945 case DW_LNS_fixed_advance_pc:
10946 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10947 op_index = 0;
c906108c
SS
10948 line_ptr += 2;
10949 break;
9aa1fe7e 10950 default:
a738430d
MK
10951 {
10952 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10953 int i;
a738430d 10954
debd256d 10955 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10956 {
10957 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10958 line_ptr += bytes_read;
10959 }
10960 }
c906108c
SS
10961 }
10962 }
59205f5a
JB
10963 if (lh->num_file_names < file || file == 0)
10964 dwarf2_debug_line_missing_file_complaint ();
10965 else
10966 {
10967 lh->file_names[file - 1].included_p = 1;
10968 if (!decode_for_pst_p)
fbf65064
UW
10969 {
10970 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10971 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10972 }
59205f5a 10973 }
c906108c 10974 }
aaa75496
JB
10975
10976 if (decode_for_pst_p)
10977 {
10978 int file_index;
10979
10980 /* Now that we're done scanning the Line Header Program, we can
10981 create the psymtab of each included file. */
10982 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10983 if (lh->file_names[file_index].included_p == 1)
10984 {
c6da4cef
DE
10985 char *include_name =
10986 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10987 if (include_name != NULL)
aaa75496
JB
10988 dwarf2_create_include_psymtab (include_name, pst, objfile);
10989 }
10990 }
cb1df416
DJ
10991 else
10992 {
10993 /* Make sure a symtab is created for every file, even files
10994 which contain only variables (i.e. no code with associated
10995 line numbers). */
10996
10997 int i;
10998 struct file_entry *fe;
10999
11000 for (i = 0; i < lh->num_file_names; i++)
11001 {
11002 char *dir = NULL;
9a619af0 11003
cb1df416
DJ
11004 fe = &lh->file_names[i];
11005 if (fe->dir_index)
11006 dir = lh->include_dirs[fe->dir_index - 1];
11007 dwarf2_start_subfile (fe->name, dir, comp_dir);
11008
11009 /* Skip the main file; we don't need it, and it must be
11010 allocated last, so that it will show up before the
11011 non-primary symtabs in the objfile's symtab list. */
11012 if (current_subfile == first_subfile)
11013 continue;
11014
11015 if (current_subfile->symtab == NULL)
11016 current_subfile->symtab = allocate_symtab (current_subfile->name,
11017 cu->objfile);
11018 fe->symtab = current_subfile->symtab;
11019 }
11020 }
c906108c
SS
11021}
11022
11023/* Start a subfile for DWARF. FILENAME is the name of the file and
11024 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11025 or NULL if not known. COMP_DIR is the compilation directory for the
11026 linetable's compilation unit or NULL if not known.
c906108c
SS
11027 This routine tries to keep line numbers from identical absolute and
11028 relative file names in a common subfile.
11029
11030 Using the `list' example from the GDB testsuite, which resides in
11031 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11032 of /srcdir/list0.c yields the following debugging information for list0.c:
11033
c5aa993b
JM
11034 DW_AT_name: /srcdir/list0.c
11035 DW_AT_comp_dir: /compdir
357e46e7 11036 files.files[0].name: list0.h
c5aa993b 11037 files.files[0].dir: /srcdir
357e46e7 11038 files.files[1].name: list0.c
c5aa993b 11039 files.files[1].dir: /srcdir
c906108c
SS
11040
11041 The line number information for list0.c has to end up in a single
4f1520fb
FR
11042 subfile, so that `break /srcdir/list0.c:1' works as expected.
11043 start_subfile will ensure that this happens provided that we pass the
11044 concatenation of files.files[1].dir and files.files[1].name as the
11045 subfile's name. */
c906108c
SS
11046
11047static void
3e43a32a
MS
11048dwarf2_start_subfile (char *filename, const char *dirname,
11049 const char *comp_dir)
c906108c 11050{
4f1520fb
FR
11051 char *fullname;
11052
11053 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11054 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11055 second argument to start_subfile. To be consistent, we do the
11056 same here. In order not to lose the line information directory,
11057 we concatenate it to the filename when it makes sense.
11058 Note that the Dwarf3 standard says (speaking of filenames in line
11059 information): ``The directory index is ignored for file names
11060 that represent full path names''. Thus ignoring dirname in the
11061 `else' branch below isn't an issue. */
c906108c 11062
d5166ae1 11063 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11064 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11065 else
11066 fullname = filename;
c906108c 11067
4f1520fb
FR
11068 start_subfile (fullname, comp_dir);
11069
11070 if (fullname != filename)
11071 xfree (fullname);
c906108c
SS
11072}
11073
4c2df51b
DJ
11074static void
11075var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11076 struct dwarf2_cu *cu)
4c2df51b 11077{
e7c27a73
DJ
11078 struct objfile *objfile = cu->objfile;
11079 struct comp_unit_head *cu_header = &cu->header;
11080
4c2df51b
DJ
11081 /* NOTE drow/2003-01-30: There used to be a comment and some special
11082 code here to turn a symbol with DW_AT_external and a
11083 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11084 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11085 with some versions of binutils) where shared libraries could have
11086 relocations against symbols in their debug information - the
11087 minimal symbol would have the right address, but the debug info
11088 would not. It's no longer necessary, because we will explicitly
11089 apply relocations when we read in the debug information now. */
11090
11091 /* A DW_AT_location attribute with no contents indicates that a
11092 variable has been optimized away. */
11093 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11094 {
11095 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11096 return;
11097 }
11098
11099 /* Handle one degenerate form of location expression specially, to
11100 preserve GDB's previous behavior when section offsets are
11101 specified. If this is just a DW_OP_addr then mark this symbol
11102 as LOC_STATIC. */
11103
11104 if (attr_form_is_block (attr)
11105 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11106 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11107 {
891d2f0b 11108 unsigned int dummy;
4c2df51b
DJ
11109
11110 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11111 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11112 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11113 fixup_symbol_section (sym, objfile);
11114 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11115 SYMBOL_SECTION (sym));
4c2df51b
DJ
11116 return;
11117 }
11118
11119 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11120 expression evaluator, and use LOC_COMPUTED only when necessary
11121 (i.e. when the value of a register or memory location is
11122 referenced, or a thread-local block, etc.). Then again, it might
11123 not be worthwhile. I'm assuming that it isn't unless performance
11124 or memory numbers show me otherwise. */
11125
e7c27a73 11126 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11127 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11128
11129 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11130 cu->has_loclist = 1;
4c2df51b
DJ
11131}
11132
c906108c
SS
11133/* Given a pointer to a DWARF information entry, figure out if we need
11134 to make a symbol table entry for it, and if so, create a new entry
11135 and return a pointer to it.
11136 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11137 used the passed type.
11138 If SPACE is not NULL, use it to hold the new symbol. If it is
11139 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11140
11141static struct symbol *
34eaf542
TT
11142new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11143 struct symbol *space)
c906108c 11144{
e7c27a73 11145 struct objfile *objfile = cu->objfile;
c906108c
SS
11146 struct symbol *sym = NULL;
11147 char *name;
11148 struct attribute *attr = NULL;
11149 struct attribute *attr2 = NULL;
e142c38c 11150 CORE_ADDR baseaddr;
e37fd15a
SW
11151 struct pending **list_to_add = NULL;
11152
edb3359d 11153 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11154
11155 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11156
94af9270 11157 name = dwarf2_name (die, cu);
c906108c
SS
11158 if (name)
11159 {
94af9270 11160 const char *linkagename;
34eaf542 11161 int suppress_add = 0;
94af9270 11162
34eaf542
TT
11163 if (space)
11164 sym = space;
11165 else
11166 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11167 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11168
11169 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11170 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11171 linkagename = dwarf2_physname (name, die, cu);
11172 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11173
f55ee35c
JK
11174 /* Fortran does not have mangling standard and the mangling does differ
11175 between gfortran, iFort etc. */
11176 if (cu->language == language_fortran
b250c185 11177 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11178 symbol_set_demangled_name (&(sym->ginfo),
11179 (char *) dwarf2_full_name (name, die, cu),
11180 NULL);
f55ee35c 11181
c906108c 11182 /* Default assumptions.
c5aa993b 11183 Use the passed type or decode it from the die. */
176620f1 11184 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11185 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11186 if (type != NULL)
11187 SYMBOL_TYPE (sym) = type;
11188 else
e7c27a73 11189 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11190 attr = dwarf2_attr (die,
11191 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11192 cu);
c906108c
SS
11193 if (attr)
11194 {
11195 SYMBOL_LINE (sym) = DW_UNSND (attr);
11196 }
cb1df416 11197
edb3359d
DJ
11198 attr = dwarf2_attr (die,
11199 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11200 cu);
cb1df416
DJ
11201 if (attr)
11202 {
11203 int file_index = DW_UNSND (attr);
9a619af0 11204
cb1df416
DJ
11205 if (cu->line_header == NULL
11206 || file_index > cu->line_header->num_file_names)
11207 complaint (&symfile_complaints,
11208 _("file index out of range"));
1c3d648d 11209 else if (file_index > 0)
cb1df416
DJ
11210 {
11211 struct file_entry *fe;
9a619af0 11212
cb1df416
DJ
11213 fe = &cu->line_header->file_names[file_index - 1];
11214 SYMBOL_SYMTAB (sym) = fe->symtab;
11215 }
11216 }
11217
c906108c
SS
11218 switch (die->tag)
11219 {
11220 case DW_TAG_label:
e142c38c 11221 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11222 if (attr)
11223 {
11224 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11225 }
0f5238ed
TT
11226 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11227 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11228 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11229 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11230 break;
11231 case DW_TAG_subprogram:
11232 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11233 finish_block. */
11234 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11235 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11236 if ((attr2 && (DW_UNSND (attr2) != 0))
11237 || cu->language == language_ada)
c906108c 11238 {
2cfa0c8d
JB
11239 /* Subprograms marked external are stored as a global symbol.
11240 Ada subprograms, whether marked external or not, are always
11241 stored as a global symbol, because we want to be able to
11242 access them globally. For instance, we want to be able
11243 to break on a nested subprogram without having to
11244 specify the context. */
e37fd15a 11245 list_to_add = &global_symbols;
c906108c
SS
11246 }
11247 else
11248 {
e37fd15a 11249 list_to_add = cu->list_in_scope;
c906108c
SS
11250 }
11251 break;
edb3359d
DJ
11252 case DW_TAG_inlined_subroutine:
11253 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11254 finish_block. */
11255 SYMBOL_CLASS (sym) = LOC_BLOCK;
11256 SYMBOL_INLINED (sym) = 1;
11257 /* Do not add the symbol to any lists. It will be found via
11258 BLOCK_FUNCTION from the blockvector. */
11259 break;
34eaf542
TT
11260 case DW_TAG_template_value_param:
11261 suppress_add = 1;
11262 /* Fall through. */
72929c62 11263 case DW_TAG_constant:
c906108c 11264 case DW_TAG_variable:
254e6b9e 11265 case DW_TAG_member:
0963b4bd
MS
11266 /* Compilation with minimal debug info may result in
11267 variables with missing type entries. Change the
11268 misleading `void' type to something sensible. */
c906108c 11269 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11270 SYMBOL_TYPE (sym)
46bf5051 11271 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11272
e142c38c 11273 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11274 /* In the case of DW_TAG_member, we should only be called for
11275 static const members. */
11276 if (die->tag == DW_TAG_member)
11277 {
3863f96c
DE
11278 /* dwarf2_add_field uses die_is_declaration,
11279 so we do the same. */
254e6b9e
DE
11280 gdb_assert (die_is_declaration (die, cu));
11281 gdb_assert (attr);
11282 }
c906108c
SS
11283 if (attr)
11284 {
e7c27a73 11285 dwarf2_const_value (attr, sym, cu);
e142c38c 11286 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11287 if (!suppress_add)
34eaf542
TT
11288 {
11289 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11290 list_to_add = &global_symbols;
34eaf542 11291 else
e37fd15a 11292 list_to_add = cu->list_in_scope;
34eaf542 11293 }
c906108c
SS
11294 break;
11295 }
e142c38c 11296 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11297 if (attr)
11298 {
e7c27a73 11299 var_decode_location (attr, sym, cu);
e142c38c 11300 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11301 if (SYMBOL_CLASS (sym) == LOC_STATIC
11302 && SYMBOL_VALUE_ADDRESS (sym) == 0
11303 && !dwarf2_per_objfile->has_section_at_zero)
11304 {
11305 /* When a static variable is eliminated by the linker,
11306 the corresponding debug information is not stripped
11307 out, but the variable address is set to null;
11308 do not add such variables into symbol table. */
11309 }
11310 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11311 {
f55ee35c
JK
11312 /* Workaround gfortran PR debug/40040 - it uses
11313 DW_AT_location for variables in -fPIC libraries which may
11314 get overriden by other libraries/executable and get
11315 a different address. Resolve it by the minimal symbol
11316 which may come from inferior's executable using copy
11317 relocation. Make this workaround only for gfortran as for
11318 other compilers GDB cannot guess the minimal symbol
11319 Fortran mangling kind. */
11320 if (cu->language == language_fortran && die->parent
11321 && die->parent->tag == DW_TAG_module
11322 && cu->producer
11323 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11324 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11325
1c809c68
TT
11326 /* A variable with DW_AT_external is never static,
11327 but it may be block-scoped. */
11328 list_to_add = (cu->list_in_scope == &file_symbols
11329 ? &global_symbols : cu->list_in_scope);
1c809c68 11330 }
c906108c 11331 else
e37fd15a 11332 list_to_add = cu->list_in_scope;
c906108c
SS
11333 }
11334 else
11335 {
11336 /* We do not know the address of this symbol.
c5aa993b
JM
11337 If it is an external symbol and we have type information
11338 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11339 The address of the variable will then be determined from
11340 the minimal symbol table whenever the variable is
11341 referenced. */
e142c38c 11342 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11343 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11344 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11345 {
0fe7935b
DJ
11346 /* A variable with DW_AT_external is never static, but it
11347 may be block-scoped. */
11348 list_to_add = (cu->list_in_scope == &file_symbols
11349 ? &global_symbols : cu->list_in_scope);
11350
c906108c 11351 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11352 }
442ddf59
JK
11353 else if (!die_is_declaration (die, cu))
11354 {
11355 /* Use the default LOC_OPTIMIZED_OUT class. */
11356 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11357 if (!suppress_add)
11358 list_to_add = cu->list_in_scope;
442ddf59 11359 }
c906108c
SS
11360 }
11361 break;
11362 case DW_TAG_formal_parameter:
edb3359d
DJ
11363 /* If we are inside a function, mark this as an argument. If
11364 not, we might be looking at an argument to an inlined function
11365 when we do not have enough information to show inlined frames;
11366 pretend it's a local variable in that case so that the user can
11367 still see it. */
11368 if (context_stack_depth > 0
11369 && context_stack[context_stack_depth - 1].name != NULL)
11370 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11371 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11372 if (attr)
11373 {
e7c27a73 11374 var_decode_location (attr, sym, cu);
c906108c 11375 }
e142c38c 11376 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11377 if (attr)
11378 {
e7c27a73 11379 dwarf2_const_value (attr, sym, cu);
c906108c 11380 }
f346a30d
PM
11381 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11382 if (attr && DW_UNSND (attr))
11383 {
11384 struct type *ref_type;
11385
11386 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11387 SYMBOL_TYPE (sym) = ref_type;
11388 }
11389
e37fd15a 11390 list_to_add = cu->list_in_scope;
c906108c
SS
11391 break;
11392 case DW_TAG_unspecified_parameters:
11393 /* From varargs functions; gdb doesn't seem to have any
11394 interest in this information, so just ignore it for now.
11395 (FIXME?) */
11396 break;
34eaf542
TT
11397 case DW_TAG_template_type_param:
11398 suppress_add = 1;
11399 /* Fall through. */
c906108c 11400 case DW_TAG_class_type:
680b30c7 11401 case DW_TAG_interface_type:
c906108c
SS
11402 case DW_TAG_structure_type:
11403 case DW_TAG_union_type:
72019c9c 11404 case DW_TAG_set_type:
c906108c
SS
11405 case DW_TAG_enumeration_type:
11406 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11407 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11408
63d06c5c 11409 {
987504bb 11410 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11411 really ever be static objects: otherwise, if you try
11412 to, say, break of a class's method and you're in a file
11413 which doesn't mention that class, it won't work unless
11414 the check for all static symbols in lookup_symbol_aux
11415 saves you. See the OtherFileClass tests in
11416 gdb.c++/namespace.exp. */
11417
e37fd15a 11418 if (!suppress_add)
34eaf542 11419 {
34eaf542
TT
11420 list_to_add = (cu->list_in_scope == &file_symbols
11421 && (cu->language == language_cplus
11422 || cu->language == language_java)
11423 ? &global_symbols : cu->list_in_scope);
63d06c5c 11424
64382290
TT
11425 /* The semantics of C++ state that "struct foo {
11426 ... }" also defines a typedef for "foo". A Java
11427 class declaration also defines a typedef for the
11428 class. */
11429 if (cu->language == language_cplus
11430 || cu->language == language_java
11431 || cu->language == language_ada)
11432 {
11433 /* The symbol's name is already allocated along
11434 with this objfile, so we don't need to
11435 duplicate it for the type. */
11436 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11437 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11438 }
63d06c5c
DC
11439 }
11440 }
c906108c
SS
11441 break;
11442 case DW_TAG_typedef:
63d06c5c
DC
11443 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11444 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11445 list_to_add = cu->list_in_scope;
63d06c5c 11446 break;
c906108c 11447 case DW_TAG_base_type:
a02abb62 11448 case DW_TAG_subrange_type:
c906108c 11449 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11450 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11451 list_to_add = cu->list_in_scope;
c906108c
SS
11452 break;
11453 case DW_TAG_enumerator:
e142c38c 11454 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11455 if (attr)
11456 {
e7c27a73 11457 dwarf2_const_value (attr, sym, cu);
c906108c 11458 }
63d06c5c
DC
11459 {
11460 /* NOTE: carlton/2003-11-10: See comment above in the
11461 DW_TAG_class_type, etc. block. */
11462
e142c38c 11463 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11464 && (cu->language == language_cplus
11465 || cu->language == language_java)
e142c38c 11466 ? &global_symbols : cu->list_in_scope);
63d06c5c 11467 }
c906108c 11468 break;
5c4e30ca
DC
11469 case DW_TAG_namespace:
11470 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11471 list_to_add = &global_symbols;
5c4e30ca 11472 break;
c906108c
SS
11473 default:
11474 /* Not a tag we recognize. Hopefully we aren't processing
11475 trash data, but since we must specifically ignore things
11476 we don't recognize, there is nothing else we should do at
0963b4bd 11477 this point. */
e2e0b3e5 11478 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11479 dwarf_tag_name (die->tag));
c906108c
SS
11480 break;
11481 }
df8a16a1 11482
e37fd15a
SW
11483 if (suppress_add)
11484 {
11485 sym->hash_next = objfile->template_symbols;
11486 objfile->template_symbols = sym;
11487 list_to_add = NULL;
11488 }
11489
11490 if (list_to_add != NULL)
11491 add_symbol_to_list (sym, list_to_add);
11492
df8a16a1
DJ
11493 /* For the benefit of old versions of GCC, check for anonymous
11494 namespaces based on the demangled name. */
11495 if (!processing_has_namespace_info
94af9270 11496 && cu->language == language_cplus)
df8a16a1 11497 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11498 }
11499 return (sym);
11500}
11501
34eaf542
TT
11502/* A wrapper for new_symbol_full that always allocates a new symbol. */
11503
11504static struct symbol *
11505new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11506{
11507 return new_symbol_full (die, type, cu, NULL);
11508}
11509
98bfdba5
PA
11510/* Given an attr with a DW_FORM_dataN value in host byte order,
11511 zero-extend it as appropriate for the symbol's type. The DWARF
11512 standard (v4) is not entirely clear about the meaning of using
11513 DW_FORM_dataN for a constant with a signed type, where the type is
11514 wider than the data. The conclusion of a discussion on the DWARF
11515 list was that this is unspecified. We choose to always zero-extend
11516 because that is the interpretation long in use by GCC. */
c906108c 11517
98bfdba5
PA
11518static gdb_byte *
11519dwarf2_const_value_data (struct attribute *attr, struct type *type,
11520 const char *name, struct obstack *obstack,
11521 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11522{
e7c27a73 11523 struct objfile *objfile = cu->objfile;
e17a4113
UW
11524 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11525 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11526 LONGEST l = DW_UNSND (attr);
11527
11528 if (bits < sizeof (*value) * 8)
11529 {
11530 l &= ((LONGEST) 1 << bits) - 1;
11531 *value = l;
11532 }
11533 else if (bits == sizeof (*value) * 8)
11534 *value = l;
11535 else
11536 {
11537 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11538 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11539 return bytes;
11540 }
11541
11542 return NULL;
11543}
11544
11545/* Read a constant value from an attribute. Either set *VALUE, or if
11546 the value does not fit in *VALUE, set *BYTES - either already
11547 allocated on the objfile obstack, or newly allocated on OBSTACK,
11548 or, set *BATON, if we translated the constant to a location
11549 expression. */
11550
11551static void
11552dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11553 const char *name, struct obstack *obstack,
11554 struct dwarf2_cu *cu,
11555 long *value, gdb_byte **bytes,
11556 struct dwarf2_locexpr_baton **baton)
11557{
11558 struct objfile *objfile = cu->objfile;
11559 struct comp_unit_head *cu_header = &cu->header;
c906108c 11560 struct dwarf_block *blk;
98bfdba5
PA
11561 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11562 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11563
11564 *value = 0;
11565 *bytes = NULL;
11566 *baton = NULL;
c906108c
SS
11567
11568 switch (attr->form)
11569 {
11570 case DW_FORM_addr:
ac56253d 11571 {
ac56253d
TT
11572 gdb_byte *data;
11573
98bfdba5
PA
11574 if (TYPE_LENGTH (type) != cu_header->addr_size)
11575 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11576 cu_header->addr_size,
98bfdba5 11577 TYPE_LENGTH (type));
ac56253d
TT
11578 /* Symbols of this form are reasonably rare, so we just
11579 piggyback on the existing location code rather than writing
11580 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11581 *baton = obstack_alloc (&objfile->objfile_obstack,
11582 sizeof (struct dwarf2_locexpr_baton));
11583 (*baton)->per_cu = cu->per_cu;
11584 gdb_assert ((*baton)->per_cu);
ac56253d 11585
98bfdba5
PA
11586 (*baton)->size = 2 + cu_header->addr_size;
11587 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11588 (*baton)->data = data;
ac56253d
TT
11589
11590 data[0] = DW_OP_addr;
11591 store_unsigned_integer (&data[1], cu_header->addr_size,
11592 byte_order, DW_ADDR (attr));
11593 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11594 }
c906108c 11595 break;
4ac36638 11596 case DW_FORM_string:
93b5768b 11597 case DW_FORM_strp:
98bfdba5
PA
11598 /* DW_STRING is already allocated on the objfile obstack, point
11599 directly to it. */
11600 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11601 break;
c906108c
SS
11602 case DW_FORM_block1:
11603 case DW_FORM_block2:
11604 case DW_FORM_block4:
11605 case DW_FORM_block:
2dc7f7b3 11606 case DW_FORM_exprloc:
c906108c 11607 blk = DW_BLOCK (attr);
98bfdba5
PA
11608 if (TYPE_LENGTH (type) != blk->size)
11609 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11610 TYPE_LENGTH (type));
11611 *bytes = blk->data;
c906108c 11612 break;
2df3850c
JM
11613
11614 /* The DW_AT_const_value attributes are supposed to carry the
11615 symbol's value "represented as it would be on the target
11616 architecture." By the time we get here, it's already been
11617 converted to host endianness, so we just need to sign- or
11618 zero-extend it as appropriate. */
11619 case DW_FORM_data1:
3e43a32a
MS
11620 *bytes = dwarf2_const_value_data (attr, type, name,
11621 obstack, cu, value, 8);
2df3850c 11622 break;
c906108c 11623 case DW_FORM_data2:
3e43a32a
MS
11624 *bytes = dwarf2_const_value_data (attr, type, name,
11625 obstack, cu, value, 16);
2df3850c 11626 break;
c906108c 11627 case DW_FORM_data4:
3e43a32a
MS
11628 *bytes = dwarf2_const_value_data (attr, type, name,
11629 obstack, cu, value, 32);
2df3850c 11630 break;
c906108c 11631 case DW_FORM_data8:
3e43a32a
MS
11632 *bytes = dwarf2_const_value_data (attr, type, name,
11633 obstack, cu, value, 64);
2df3850c
JM
11634 break;
11635
c906108c 11636 case DW_FORM_sdata:
98bfdba5 11637 *value = DW_SND (attr);
2df3850c
JM
11638 break;
11639
c906108c 11640 case DW_FORM_udata:
98bfdba5 11641 *value = DW_UNSND (attr);
c906108c 11642 break;
2df3850c 11643
c906108c 11644 default:
4d3c2250 11645 complaint (&symfile_complaints,
e2e0b3e5 11646 _("unsupported const value attribute form: '%s'"),
4d3c2250 11647 dwarf_form_name (attr->form));
98bfdba5 11648 *value = 0;
c906108c
SS
11649 break;
11650 }
11651}
11652
2df3850c 11653
98bfdba5
PA
11654/* Copy constant value from an attribute to a symbol. */
11655
2df3850c 11656static void
98bfdba5
PA
11657dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11658 struct dwarf2_cu *cu)
2df3850c 11659{
98bfdba5
PA
11660 struct objfile *objfile = cu->objfile;
11661 struct comp_unit_head *cu_header = &cu->header;
11662 long value;
11663 gdb_byte *bytes;
11664 struct dwarf2_locexpr_baton *baton;
2df3850c 11665
98bfdba5
PA
11666 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11667 SYMBOL_PRINT_NAME (sym),
11668 &objfile->objfile_obstack, cu,
11669 &value, &bytes, &baton);
2df3850c 11670
98bfdba5
PA
11671 if (baton != NULL)
11672 {
11673 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11674 SYMBOL_LOCATION_BATON (sym) = baton;
11675 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11676 }
11677 else if (bytes != NULL)
11678 {
11679 SYMBOL_VALUE_BYTES (sym) = bytes;
11680 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11681 }
11682 else
11683 {
11684 SYMBOL_VALUE (sym) = value;
11685 SYMBOL_CLASS (sym) = LOC_CONST;
11686 }
2df3850c
JM
11687}
11688
c906108c
SS
11689/* Return the type of the die in question using its DW_AT_type attribute. */
11690
11691static struct type *
e7c27a73 11692die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11693{
c906108c 11694 struct attribute *type_attr;
c906108c 11695
e142c38c 11696 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11697 if (!type_attr)
11698 {
11699 /* A missing DW_AT_type represents a void type. */
46bf5051 11700 return objfile_type (cu->objfile)->builtin_void;
c906108c 11701 }
348e048f 11702
673bfd45 11703 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11704}
11705
b4ba55a1
JB
11706/* True iff CU's producer generates GNAT Ada auxiliary information
11707 that allows to find parallel types through that information instead
11708 of having to do expensive parallel lookups by type name. */
11709
11710static int
11711need_gnat_info (struct dwarf2_cu *cu)
11712{
11713 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11714 of GNAT produces this auxiliary information, without any indication
11715 that it is produced. Part of enhancing the FSF version of GNAT
11716 to produce that information will be to put in place an indicator
11717 that we can use in order to determine whether the descriptive type
11718 info is available or not. One suggestion that has been made is
11719 to use a new attribute, attached to the CU die. For now, assume
11720 that the descriptive type info is not available. */
11721 return 0;
11722}
11723
b4ba55a1
JB
11724/* Return the auxiliary type of the die in question using its
11725 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11726 attribute is not present. */
11727
11728static struct type *
11729die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11730{
b4ba55a1 11731 struct attribute *type_attr;
b4ba55a1
JB
11732
11733 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11734 if (!type_attr)
11735 return NULL;
11736
673bfd45 11737 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11738}
11739
11740/* If DIE has a descriptive_type attribute, then set the TYPE's
11741 descriptive type accordingly. */
11742
11743static void
11744set_descriptive_type (struct type *type, struct die_info *die,
11745 struct dwarf2_cu *cu)
11746{
11747 struct type *descriptive_type = die_descriptive_type (die, cu);
11748
11749 if (descriptive_type)
11750 {
11751 ALLOCATE_GNAT_AUX_TYPE (type);
11752 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11753 }
11754}
11755
c906108c
SS
11756/* Return the containing type of the die in question using its
11757 DW_AT_containing_type attribute. */
11758
11759static struct type *
e7c27a73 11760die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11761{
c906108c 11762 struct attribute *type_attr;
c906108c 11763
e142c38c 11764 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11765 if (!type_attr)
11766 error (_("Dwarf Error: Problem turning containing type into gdb type "
11767 "[in module %s]"), cu->objfile->name);
11768
673bfd45 11769 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11770}
11771
673bfd45
DE
11772/* Look up the type of DIE in CU using its type attribute ATTR.
11773 If there is no type substitute an error marker. */
11774
c906108c 11775static struct type *
673bfd45
DE
11776lookup_die_type (struct die_info *die, struct attribute *attr,
11777 struct dwarf2_cu *cu)
c906108c 11778{
f792889a
DJ
11779 struct type *this_type;
11780
673bfd45
DE
11781 /* First see if we have it cached. */
11782
11783 if (is_ref_attr (attr))
11784 {
11785 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11786
11787 this_type = get_die_type_at_offset (offset, cu->per_cu);
11788 }
55f1336d 11789 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11790 {
11791 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11792 struct dwarf2_cu *sig_cu;
11793 unsigned int offset;
11794
11795 /* sig_type will be NULL if the signatured type is missing from
11796 the debug info. */
11797 if (sig_type == NULL)
11798 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11799 "at 0x%x [in module %s]"),
11800 die->offset, cu->objfile->name);
11801
11802 gdb_assert (sig_type->per_cu.from_debug_types);
b3c8eb43 11803 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
11804 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11805 }
11806 else
11807 {
11808 dump_die_for_error (die);
11809 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11810 dwarf_attr_name (attr->name), cu->objfile->name);
11811 }
11812
11813 /* If not cached we need to read it in. */
11814
11815 if (this_type == NULL)
11816 {
11817 struct die_info *type_die;
11818 struct dwarf2_cu *type_cu = cu;
11819
11820 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11821 /* If the type is cached, we should have found it above. */
11822 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11823 this_type = read_type_die_1 (type_die, type_cu);
11824 }
11825
11826 /* If we still don't have a type use an error marker. */
11827
11828 if (this_type == NULL)
c906108c 11829 {
b00fdb78
TT
11830 char *message, *saved;
11831
11832 /* read_type_die already issued a complaint. */
11833 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11834 cu->objfile->name,
11835 cu->header.offset,
11836 die->offset);
11837 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11838 message, strlen (message));
11839 xfree (message);
11840
11841 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11842 }
673bfd45 11843
f792889a 11844 return this_type;
c906108c
SS
11845}
11846
673bfd45
DE
11847/* Return the type in DIE, CU.
11848 Returns NULL for invalid types.
11849
11850 This first does a lookup in the appropriate type_hash table,
11851 and only reads the die in if necessary.
11852
11853 NOTE: This can be called when reading in partial or full symbols. */
11854
f792889a 11855static struct type *
e7c27a73 11856read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11857{
f792889a
DJ
11858 struct type *this_type;
11859
11860 this_type = get_die_type (die, cu);
11861 if (this_type)
11862 return this_type;
11863
673bfd45
DE
11864 return read_type_die_1 (die, cu);
11865}
11866
11867/* Read the type in DIE, CU.
11868 Returns NULL for invalid types. */
11869
11870static struct type *
11871read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11872{
11873 struct type *this_type = NULL;
11874
c906108c
SS
11875 switch (die->tag)
11876 {
11877 case DW_TAG_class_type:
680b30c7 11878 case DW_TAG_interface_type:
c906108c
SS
11879 case DW_TAG_structure_type:
11880 case DW_TAG_union_type:
f792889a 11881 this_type = read_structure_type (die, cu);
c906108c
SS
11882 break;
11883 case DW_TAG_enumeration_type:
f792889a 11884 this_type = read_enumeration_type (die, cu);
c906108c
SS
11885 break;
11886 case DW_TAG_subprogram:
11887 case DW_TAG_subroutine_type:
edb3359d 11888 case DW_TAG_inlined_subroutine:
f792889a 11889 this_type = read_subroutine_type (die, cu);
c906108c
SS
11890 break;
11891 case DW_TAG_array_type:
f792889a 11892 this_type = read_array_type (die, cu);
c906108c 11893 break;
72019c9c 11894 case DW_TAG_set_type:
f792889a 11895 this_type = read_set_type (die, cu);
72019c9c 11896 break;
c906108c 11897 case DW_TAG_pointer_type:
f792889a 11898 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11899 break;
11900 case DW_TAG_ptr_to_member_type:
f792889a 11901 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11902 break;
11903 case DW_TAG_reference_type:
f792889a 11904 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11905 break;
11906 case DW_TAG_const_type:
f792889a 11907 this_type = read_tag_const_type (die, cu);
c906108c
SS
11908 break;
11909 case DW_TAG_volatile_type:
f792889a 11910 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11911 break;
11912 case DW_TAG_string_type:
f792889a 11913 this_type = read_tag_string_type (die, cu);
c906108c
SS
11914 break;
11915 case DW_TAG_typedef:
f792889a 11916 this_type = read_typedef (die, cu);
c906108c 11917 break;
a02abb62 11918 case DW_TAG_subrange_type:
f792889a 11919 this_type = read_subrange_type (die, cu);
a02abb62 11920 break;
c906108c 11921 case DW_TAG_base_type:
f792889a 11922 this_type = read_base_type (die, cu);
c906108c 11923 break;
81a17f79 11924 case DW_TAG_unspecified_type:
f792889a 11925 this_type = read_unspecified_type (die, cu);
81a17f79 11926 break;
0114d602
DJ
11927 case DW_TAG_namespace:
11928 this_type = read_namespace_type (die, cu);
11929 break;
f55ee35c
JK
11930 case DW_TAG_module:
11931 this_type = read_module_type (die, cu);
11932 break;
c906108c 11933 default:
3e43a32a
MS
11934 complaint (&symfile_complaints,
11935 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11936 dwarf_tag_name (die->tag));
c906108c
SS
11937 break;
11938 }
63d06c5c 11939
f792889a 11940 return this_type;
63d06c5c
DC
11941}
11942
abc72ce4
DE
11943/* See if we can figure out if the class lives in a namespace. We do
11944 this by looking for a member function; its demangled name will
11945 contain namespace info, if there is any.
11946 Return the computed name or NULL.
11947 Space for the result is allocated on the objfile's obstack.
11948 This is the full-die version of guess_partial_die_structure_name.
11949 In this case we know DIE has no useful parent. */
11950
11951static char *
11952guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11953{
11954 struct die_info *spec_die;
11955 struct dwarf2_cu *spec_cu;
11956 struct die_info *child;
11957
11958 spec_cu = cu;
11959 spec_die = die_specification (die, &spec_cu);
11960 if (spec_die != NULL)
11961 {
11962 die = spec_die;
11963 cu = spec_cu;
11964 }
11965
11966 for (child = die->child;
11967 child != NULL;
11968 child = child->sibling)
11969 {
11970 if (child->tag == DW_TAG_subprogram)
11971 {
11972 struct attribute *attr;
11973
11974 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11975 if (attr == NULL)
11976 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11977 if (attr != NULL)
11978 {
11979 char *actual_name
11980 = language_class_name_from_physname (cu->language_defn,
11981 DW_STRING (attr));
11982 char *name = NULL;
11983
11984 if (actual_name != NULL)
11985 {
11986 char *die_name = dwarf2_name (die, cu);
11987
11988 if (die_name != NULL
11989 && strcmp (die_name, actual_name) != 0)
11990 {
11991 /* Strip off the class name from the full name.
11992 We want the prefix. */
11993 int die_name_len = strlen (die_name);
11994 int actual_name_len = strlen (actual_name);
11995
11996 /* Test for '::' as a sanity check. */
11997 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11998 && actual_name[actual_name_len
11999 - die_name_len - 1] == ':')
abc72ce4
DE
12000 name =
12001 obsavestring (actual_name,
12002 actual_name_len - die_name_len - 2,
12003 &cu->objfile->objfile_obstack);
12004 }
12005 }
12006 xfree (actual_name);
12007 return name;
12008 }
12009 }
12010 }
12011
12012 return NULL;
12013}
12014
fdde2d81 12015/* Return the name of the namespace/class that DIE is defined within,
0114d602 12016 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12017
0114d602
DJ
12018 For example, if we're within the method foo() in the following
12019 code:
12020
12021 namespace N {
12022 class C {
12023 void foo () {
12024 }
12025 };
12026 }
12027
12028 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12029
12030static char *
e142c38c 12031determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12032{
0114d602
DJ
12033 struct die_info *parent, *spec_die;
12034 struct dwarf2_cu *spec_cu;
12035 struct type *parent_type;
63d06c5c 12036
f55ee35c
JK
12037 if (cu->language != language_cplus && cu->language != language_java
12038 && cu->language != language_fortran)
0114d602
DJ
12039 return "";
12040
12041 /* We have to be careful in the presence of DW_AT_specification.
12042 For example, with GCC 3.4, given the code
12043
12044 namespace N {
12045 void foo() {
12046 // Definition of N::foo.
12047 }
12048 }
12049
12050 then we'll have a tree of DIEs like this:
12051
12052 1: DW_TAG_compile_unit
12053 2: DW_TAG_namespace // N
12054 3: DW_TAG_subprogram // declaration of N::foo
12055 4: DW_TAG_subprogram // definition of N::foo
12056 DW_AT_specification // refers to die #3
12057
12058 Thus, when processing die #4, we have to pretend that we're in
12059 the context of its DW_AT_specification, namely the contex of die
12060 #3. */
12061 spec_cu = cu;
12062 spec_die = die_specification (die, &spec_cu);
12063 if (spec_die == NULL)
12064 parent = die->parent;
12065 else
63d06c5c 12066 {
0114d602
DJ
12067 parent = spec_die->parent;
12068 cu = spec_cu;
63d06c5c 12069 }
0114d602
DJ
12070
12071 if (parent == NULL)
12072 return "";
98bfdba5
PA
12073 else if (parent->building_fullname)
12074 {
12075 const char *name;
12076 const char *parent_name;
12077
12078 /* It has been seen on RealView 2.2 built binaries,
12079 DW_TAG_template_type_param types actually _defined_ as
12080 children of the parent class:
12081
12082 enum E {};
12083 template class <class Enum> Class{};
12084 Class<enum E> class_e;
12085
12086 1: DW_TAG_class_type (Class)
12087 2: DW_TAG_enumeration_type (E)
12088 3: DW_TAG_enumerator (enum1:0)
12089 3: DW_TAG_enumerator (enum2:1)
12090 ...
12091 2: DW_TAG_template_type_param
12092 DW_AT_type DW_FORM_ref_udata (E)
12093
12094 Besides being broken debug info, it can put GDB into an
12095 infinite loop. Consider:
12096
12097 When we're building the full name for Class<E>, we'll start
12098 at Class, and go look over its template type parameters,
12099 finding E. We'll then try to build the full name of E, and
12100 reach here. We're now trying to build the full name of E,
12101 and look over the parent DIE for containing scope. In the
12102 broken case, if we followed the parent DIE of E, we'd again
12103 find Class, and once again go look at its template type
12104 arguments, etc., etc. Simply don't consider such parent die
12105 as source-level parent of this die (it can't be, the language
12106 doesn't allow it), and break the loop here. */
12107 name = dwarf2_name (die, cu);
12108 parent_name = dwarf2_name (parent, cu);
12109 complaint (&symfile_complaints,
12110 _("template param type '%s' defined within parent '%s'"),
12111 name ? name : "<unknown>",
12112 parent_name ? parent_name : "<unknown>");
12113 return "";
12114 }
63d06c5c 12115 else
0114d602
DJ
12116 switch (parent->tag)
12117 {
63d06c5c 12118 case DW_TAG_namespace:
0114d602 12119 parent_type = read_type_die (parent, cu);
acebe513
UW
12120 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12121 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12122 Work around this problem here. */
12123 if (cu->language == language_cplus
12124 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12125 return "";
0114d602
DJ
12126 /* We give a name to even anonymous namespaces. */
12127 return TYPE_TAG_NAME (parent_type);
63d06c5c 12128 case DW_TAG_class_type:
680b30c7 12129 case DW_TAG_interface_type:
63d06c5c 12130 case DW_TAG_structure_type:
0114d602 12131 case DW_TAG_union_type:
f55ee35c 12132 case DW_TAG_module:
0114d602
DJ
12133 parent_type = read_type_die (parent, cu);
12134 if (TYPE_TAG_NAME (parent_type) != NULL)
12135 return TYPE_TAG_NAME (parent_type);
12136 else
12137 /* An anonymous structure is only allowed non-static data
12138 members; no typedefs, no member functions, et cetera.
12139 So it does not need a prefix. */
12140 return "";
abc72ce4
DE
12141 case DW_TAG_compile_unit:
12142 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12143 if (cu->language == language_cplus
12144 && dwarf2_per_objfile->types.asection != NULL
12145 && die->child != NULL
12146 && (die->tag == DW_TAG_class_type
12147 || die->tag == DW_TAG_structure_type
12148 || die->tag == DW_TAG_union_type))
12149 {
12150 char *name = guess_full_die_structure_name (die, cu);
12151 if (name != NULL)
12152 return name;
12153 }
12154 return "";
63d06c5c 12155 default:
8176b9b8 12156 return determine_prefix (parent, cu);
63d06c5c 12157 }
63d06c5c
DC
12158}
12159
3e43a32a
MS
12160/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12161 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12162 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12163 an obconcat, otherwise allocate storage for the result. The CU argument is
12164 used to determine the language and hence, the appropriate separator. */
987504bb 12165
f55ee35c 12166#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12167
12168static char *
f55ee35c
JK
12169typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12170 int physname, struct dwarf2_cu *cu)
63d06c5c 12171{
f55ee35c 12172 const char *lead = "";
5c315b68 12173 const char *sep;
63d06c5c 12174
3e43a32a
MS
12175 if (suffix == NULL || suffix[0] == '\0'
12176 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12177 sep = "";
12178 else if (cu->language == language_java)
12179 sep = ".";
f55ee35c
JK
12180 else if (cu->language == language_fortran && physname)
12181 {
12182 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12183 DW_AT_MIPS_linkage_name is preferred and used instead. */
12184
12185 lead = "__";
12186 sep = "_MOD_";
12187 }
987504bb
JJ
12188 else
12189 sep = "::";
63d06c5c 12190
6dd47d34
DE
12191 if (prefix == NULL)
12192 prefix = "";
12193 if (suffix == NULL)
12194 suffix = "";
12195
987504bb
JJ
12196 if (obs == NULL)
12197 {
3e43a32a
MS
12198 char *retval
12199 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12200
f55ee35c
JK
12201 strcpy (retval, lead);
12202 strcat (retval, prefix);
6dd47d34
DE
12203 strcat (retval, sep);
12204 strcat (retval, suffix);
63d06c5c
DC
12205 return retval;
12206 }
987504bb
JJ
12207 else
12208 {
12209 /* We have an obstack. */
f55ee35c 12210 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12211 }
63d06c5c
DC
12212}
12213
c906108c
SS
12214/* Return sibling of die, NULL if no sibling. */
12215
f9aca02d 12216static struct die_info *
fba45db2 12217sibling_die (struct die_info *die)
c906108c 12218{
639d11d3 12219 return die->sibling;
c906108c
SS
12220}
12221
71c25dea
TT
12222/* Get name of a die, return NULL if not found. */
12223
12224static char *
12225dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12226 struct obstack *obstack)
12227{
12228 if (name && cu->language == language_cplus)
12229 {
12230 char *canon_name = cp_canonicalize_string (name);
12231
12232 if (canon_name != NULL)
12233 {
12234 if (strcmp (canon_name, name) != 0)
12235 name = obsavestring (canon_name, strlen (canon_name),
12236 obstack);
12237 xfree (canon_name);
12238 }
12239 }
12240
12241 return name;
c906108c
SS
12242}
12243
9219021c
DC
12244/* Get name of a die, return NULL if not found. */
12245
12246static char *
e142c38c 12247dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12248{
12249 struct attribute *attr;
12250
e142c38c 12251 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12252 if ((!attr || !DW_STRING (attr))
12253 && die->tag != DW_TAG_class_type
12254 && die->tag != DW_TAG_interface_type
12255 && die->tag != DW_TAG_structure_type
12256 && die->tag != DW_TAG_union_type)
71c25dea
TT
12257 return NULL;
12258
12259 switch (die->tag)
12260 {
12261 case DW_TAG_compile_unit:
12262 /* Compilation units have a DW_AT_name that is a filename, not
12263 a source language identifier. */
12264 case DW_TAG_enumeration_type:
12265 case DW_TAG_enumerator:
12266 /* These tags always have simple identifiers already; no need
12267 to canonicalize them. */
12268 return DW_STRING (attr);
907af001 12269
418835cc
KS
12270 case DW_TAG_subprogram:
12271 /* Java constructors will all be named "<init>", so return
12272 the class name when we see this special case. */
12273 if (cu->language == language_java
12274 && DW_STRING (attr) != NULL
12275 && strcmp (DW_STRING (attr), "<init>") == 0)
12276 {
12277 struct dwarf2_cu *spec_cu = cu;
12278 struct die_info *spec_die;
12279
12280 /* GCJ will output '<init>' for Java constructor names.
12281 For this special case, return the name of the parent class. */
12282
12283 /* GCJ may output suprogram DIEs with AT_specification set.
12284 If so, use the name of the specified DIE. */
12285 spec_die = die_specification (die, &spec_cu);
12286 if (spec_die != NULL)
12287 return dwarf2_name (spec_die, spec_cu);
12288
12289 do
12290 {
12291 die = die->parent;
12292 if (die->tag == DW_TAG_class_type)
12293 return dwarf2_name (die, cu);
12294 }
12295 while (die->tag != DW_TAG_compile_unit);
12296 }
907af001
UW
12297 break;
12298
12299 case DW_TAG_class_type:
12300 case DW_TAG_interface_type:
12301 case DW_TAG_structure_type:
12302 case DW_TAG_union_type:
12303 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12304 structures or unions. These were of the form "._%d" in GCC 4.1,
12305 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12306 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12307 if (attr && DW_STRING (attr)
12308 && (strncmp (DW_STRING (attr), "._", 2) == 0
12309 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12310 return NULL;
53832f31
TT
12311
12312 /* GCC might emit a nameless typedef that has a linkage name. See
12313 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12314 if (!attr || DW_STRING (attr) == NULL)
12315 {
df5c6c50 12316 char *demangled = NULL;
53832f31
TT
12317
12318 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12319 if (attr == NULL)
12320 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12321
12322 if (attr == NULL || DW_STRING (attr) == NULL)
12323 return NULL;
12324
df5c6c50
JK
12325 /* Avoid demangling DW_STRING (attr) the second time on a second
12326 call for the same DIE. */
12327 if (!DW_STRING_IS_CANONICAL (attr))
12328 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12329
12330 if (demangled)
12331 {
12332 /* FIXME: we already did this for the partial symbol... */
12333 DW_STRING (attr)
12334 = obsavestring (demangled, strlen (demangled),
12335 &cu->objfile->objfile_obstack);
12336 DW_STRING_IS_CANONICAL (attr) = 1;
12337 xfree (demangled);
12338 }
12339 }
907af001
UW
12340 break;
12341
71c25dea 12342 default:
907af001
UW
12343 break;
12344 }
12345
12346 if (!DW_STRING_IS_CANONICAL (attr))
12347 {
12348 DW_STRING (attr)
12349 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12350 &cu->objfile->objfile_obstack);
12351 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12352 }
907af001 12353 return DW_STRING (attr);
9219021c
DC
12354}
12355
12356/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12357 is none. *EXT_CU is the CU containing DIE on input, and the CU
12358 containing the return value on output. */
9219021c
DC
12359
12360static struct die_info *
f2f0e013 12361dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12362{
12363 struct attribute *attr;
9219021c 12364
f2f0e013 12365 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12366 if (attr == NULL)
12367 return NULL;
12368
f2f0e013 12369 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12370}
12371
c906108c
SS
12372/* Convert a DIE tag into its string name. */
12373
12374static char *
aa1ee363 12375dwarf_tag_name (unsigned tag)
c906108c
SS
12376{
12377 switch (tag)
12378 {
12379 case DW_TAG_padding:
12380 return "DW_TAG_padding";
12381 case DW_TAG_array_type:
12382 return "DW_TAG_array_type";
12383 case DW_TAG_class_type:
12384 return "DW_TAG_class_type";
12385 case DW_TAG_entry_point:
12386 return "DW_TAG_entry_point";
12387 case DW_TAG_enumeration_type:
12388 return "DW_TAG_enumeration_type";
12389 case DW_TAG_formal_parameter:
12390 return "DW_TAG_formal_parameter";
12391 case DW_TAG_imported_declaration:
12392 return "DW_TAG_imported_declaration";
12393 case DW_TAG_label:
12394 return "DW_TAG_label";
12395 case DW_TAG_lexical_block:
12396 return "DW_TAG_lexical_block";
12397 case DW_TAG_member:
12398 return "DW_TAG_member";
12399 case DW_TAG_pointer_type:
12400 return "DW_TAG_pointer_type";
12401 case DW_TAG_reference_type:
12402 return "DW_TAG_reference_type";
12403 case DW_TAG_compile_unit:
12404 return "DW_TAG_compile_unit";
12405 case DW_TAG_string_type:
12406 return "DW_TAG_string_type";
12407 case DW_TAG_structure_type:
12408 return "DW_TAG_structure_type";
12409 case DW_TAG_subroutine_type:
12410 return "DW_TAG_subroutine_type";
12411 case DW_TAG_typedef:
12412 return "DW_TAG_typedef";
12413 case DW_TAG_union_type:
12414 return "DW_TAG_union_type";
12415 case DW_TAG_unspecified_parameters:
12416 return "DW_TAG_unspecified_parameters";
12417 case DW_TAG_variant:
12418 return "DW_TAG_variant";
12419 case DW_TAG_common_block:
12420 return "DW_TAG_common_block";
12421 case DW_TAG_common_inclusion:
12422 return "DW_TAG_common_inclusion";
12423 case DW_TAG_inheritance:
12424 return "DW_TAG_inheritance";
12425 case DW_TAG_inlined_subroutine:
12426 return "DW_TAG_inlined_subroutine";
12427 case DW_TAG_module:
12428 return "DW_TAG_module";
12429 case DW_TAG_ptr_to_member_type:
12430 return "DW_TAG_ptr_to_member_type";
12431 case DW_TAG_set_type:
12432 return "DW_TAG_set_type";
12433 case DW_TAG_subrange_type:
12434 return "DW_TAG_subrange_type";
12435 case DW_TAG_with_stmt:
12436 return "DW_TAG_with_stmt";
12437 case DW_TAG_access_declaration:
12438 return "DW_TAG_access_declaration";
12439 case DW_TAG_base_type:
12440 return "DW_TAG_base_type";
12441 case DW_TAG_catch_block:
12442 return "DW_TAG_catch_block";
12443 case DW_TAG_const_type:
12444 return "DW_TAG_const_type";
12445 case DW_TAG_constant:
12446 return "DW_TAG_constant";
12447 case DW_TAG_enumerator:
12448 return "DW_TAG_enumerator";
12449 case DW_TAG_file_type:
12450 return "DW_TAG_file_type";
12451 case DW_TAG_friend:
12452 return "DW_TAG_friend";
12453 case DW_TAG_namelist:
12454 return "DW_TAG_namelist";
12455 case DW_TAG_namelist_item:
12456 return "DW_TAG_namelist_item";
12457 case DW_TAG_packed_type:
12458 return "DW_TAG_packed_type";
12459 case DW_TAG_subprogram:
12460 return "DW_TAG_subprogram";
12461 case DW_TAG_template_type_param:
12462 return "DW_TAG_template_type_param";
12463 case DW_TAG_template_value_param:
12464 return "DW_TAG_template_value_param";
12465 case DW_TAG_thrown_type:
12466 return "DW_TAG_thrown_type";
12467 case DW_TAG_try_block:
12468 return "DW_TAG_try_block";
12469 case DW_TAG_variant_part:
12470 return "DW_TAG_variant_part";
12471 case DW_TAG_variable:
12472 return "DW_TAG_variable";
12473 case DW_TAG_volatile_type:
12474 return "DW_TAG_volatile_type";
d9fa45fe
DC
12475 case DW_TAG_dwarf_procedure:
12476 return "DW_TAG_dwarf_procedure";
12477 case DW_TAG_restrict_type:
12478 return "DW_TAG_restrict_type";
12479 case DW_TAG_interface_type:
12480 return "DW_TAG_interface_type";
12481 case DW_TAG_namespace:
12482 return "DW_TAG_namespace";
12483 case DW_TAG_imported_module:
12484 return "DW_TAG_imported_module";
12485 case DW_TAG_unspecified_type:
12486 return "DW_TAG_unspecified_type";
12487 case DW_TAG_partial_unit:
12488 return "DW_TAG_partial_unit";
12489 case DW_TAG_imported_unit:
12490 return "DW_TAG_imported_unit";
b7619582
GF
12491 case DW_TAG_condition:
12492 return "DW_TAG_condition";
12493 case DW_TAG_shared_type:
12494 return "DW_TAG_shared_type";
348e048f
DE
12495 case DW_TAG_type_unit:
12496 return "DW_TAG_type_unit";
c906108c
SS
12497 case DW_TAG_MIPS_loop:
12498 return "DW_TAG_MIPS_loop";
b7619582
GF
12499 case DW_TAG_HP_array_descriptor:
12500 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12501 case DW_TAG_format_label:
12502 return "DW_TAG_format_label";
12503 case DW_TAG_function_template:
12504 return "DW_TAG_function_template";
12505 case DW_TAG_class_template:
12506 return "DW_TAG_class_template";
b7619582
GF
12507 case DW_TAG_GNU_BINCL:
12508 return "DW_TAG_GNU_BINCL";
12509 case DW_TAG_GNU_EINCL:
12510 return "DW_TAG_GNU_EINCL";
12511 case DW_TAG_upc_shared_type:
12512 return "DW_TAG_upc_shared_type";
12513 case DW_TAG_upc_strict_type:
12514 return "DW_TAG_upc_strict_type";
12515 case DW_TAG_upc_relaxed_type:
12516 return "DW_TAG_upc_relaxed_type";
12517 case DW_TAG_PGI_kanji_type:
12518 return "DW_TAG_PGI_kanji_type";
12519 case DW_TAG_PGI_interface_block:
12520 return "DW_TAG_PGI_interface_block";
c906108c
SS
12521 default:
12522 return "DW_TAG_<unknown>";
12523 }
12524}
12525
12526/* Convert a DWARF attribute code into its string name. */
12527
12528static char *
aa1ee363 12529dwarf_attr_name (unsigned attr)
c906108c
SS
12530{
12531 switch (attr)
12532 {
12533 case DW_AT_sibling:
12534 return "DW_AT_sibling";
12535 case DW_AT_location:
12536 return "DW_AT_location";
12537 case DW_AT_name:
12538 return "DW_AT_name";
12539 case DW_AT_ordering:
12540 return "DW_AT_ordering";
12541 case DW_AT_subscr_data:
12542 return "DW_AT_subscr_data";
12543 case DW_AT_byte_size:
12544 return "DW_AT_byte_size";
12545 case DW_AT_bit_offset:
12546 return "DW_AT_bit_offset";
12547 case DW_AT_bit_size:
12548 return "DW_AT_bit_size";
12549 case DW_AT_element_list:
12550 return "DW_AT_element_list";
12551 case DW_AT_stmt_list:
12552 return "DW_AT_stmt_list";
12553 case DW_AT_low_pc:
12554 return "DW_AT_low_pc";
12555 case DW_AT_high_pc:
12556 return "DW_AT_high_pc";
12557 case DW_AT_language:
12558 return "DW_AT_language";
12559 case DW_AT_member:
12560 return "DW_AT_member";
12561 case DW_AT_discr:
12562 return "DW_AT_discr";
12563 case DW_AT_discr_value:
12564 return "DW_AT_discr_value";
12565 case DW_AT_visibility:
12566 return "DW_AT_visibility";
12567 case DW_AT_import:
12568 return "DW_AT_import";
12569 case DW_AT_string_length:
12570 return "DW_AT_string_length";
12571 case DW_AT_common_reference:
12572 return "DW_AT_common_reference";
12573 case DW_AT_comp_dir:
12574 return "DW_AT_comp_dir";
12575 case DW_AT_const_value:
12576 return "DW_AT_const_value";
12577 case DW_AT_containing_type:
12578 return "DW_AT_containing_type";
12579 case DW_AT_default_value:
12580 return "DW_AT_default_value";
12581 case DW_AT_inline:
12582 return "DW_AT_inline";
12583 case DW_AT_is_optional:
12584 return "DW_AT_is_optional";
12585 case DW_AT_lower_bound:
12586 return "DW_AT_lower_bound";
12587 case DW_AT_producer:
12588 return "DW_AT_producer";
12589 case DW_AT_prototyped:
12590 return "DW_AT_prototyped";
12591 case DW_AT_return_addr:
12592 return "DW_AT_return_addr";
12593 case DW_AT_start_scope:
12594 return "DW_AT_start_scope";
09fa0d7c
JK
12595 case DW_AT_bit_stride:
12596 return "DW_AT_bit_stride";
c906108c
SS
12597 case DW_AT_upper_bound:
12598 return "DW_AT_upper_bound";
12599 case DW_AT_abstract_origin:
12600 return "DW_AT_abstract_origin";
12601 case DW_AT_accessibility:
12602 return "DW_AT_accessibility";
12603 case DW_AT_address_class:
12604 return "DW_AT_address_class";
12605 case DW_AT_artificial:
12606 return "DW_AT_artificial";
12607 case DW_AT_base_types:
12608 return "DW_AT_base_types";
12609 case DW_AT_calling_convention:
12610 return "DW_AT_calling_convention";
12611 case DW_AT_count:
12612 return "DW_AT_count";
12613 case DW_AT_data_member_location:
12614 return "DW_AT_data_member_location";
12615 case DW_AT_decl_column:
12616 return "DW_AT_decl_column";
12617 case DW_AT_decl_file:
12618 return "DW_AT_decl_file";
12619 case DW_AT_decl_line:
12620 return "DW_AT_decl_line";
12621 case DW_AT_declaration:
12622 return "DW_AT_declaration";
12623 case DW_AT_discr_list:
12624 return "DW_AT_discr_list";
12625 case DW_AT_encoding:
12626 return "DW_AT_encoding";
12627 case DW_AT_external:
12628 return "DW_AT_external";
12629 case DW_AT_frame_base:
12630 return "DW_AT_frame_base";
12631 case DW_AT_friend:
12632 return "DW_AT_friend";
12633 case DW_AT_identifier_case:
12634 return "DW_AT_identifier_case";
12635 case DW_AT_macro_info:
12636 return "DW_AT_macro_info";
12637 case DW_AT_namelist_items:
12638 return "DW_AT_namelist_items";
12639 case DW_AT_priority:
12640 return "DW_AT_priority";
12641 case DW_AT_segment:
12642 return "DW_AT_segment";
12643 case DW_AT_specification:
12644 return "DW_AT_specification";
12645 case DW_AT_static_link:
12646 return "DW_AT_static_link";
12647 case DW_AT_type:
12648 return "DW_AT_type";
12649 case DW_AT_use_location:
12650 return "DW_AT_use_location";
12651 case DW_AT_variable_parameter:
12652 return "DW_AT_variable_parameter";
12653 case DW_AT_virtuality:
12654 return "DW_AT_virtuality";
12655 case DW_AT_vtable_elem_location:
12656 return "DW_AT_vtable_elem_location";
b7619582 12657 /* DWARF 3 values. */
d9fa45fe
DC
12658 case DW_AT_allocated:
12659 return "DW_AT_allocated";
12660 case DW_AT_associated:
12661 return "DW_AT_associated";
12662 case DW_AT_data_location:
12663 return "DW_AT_data_location";
09fa0d7c
JK
12664 case DW_AT_byte_stride:
12665 return "DW_AT_byte_stride";
d9fa45fe
DC
12666 case DW_AT_entry_pc:
12667 return "DW_AT_entry_pc";
12668 case DW_AT_use_UTF8:
12669 return "DW_AT_use_UTF8";
12670 case DW_AT_extension:
12671 return "DW_AT_extension";
12672 case DW_AT_ranges:
12673 return "DW_AT_ranges";
12674 case DW_AT_trampoline:
12675 return "DW_AT_trampoline";
12676 case DW_AT_call_column:
12677 return "DW_AT_call_column";
12678 case DW_AT_call_file:
12679 return "DW_AT_call_file";
12680 case DW_AT_call_line:
12681 return "DW_AT_call_line";
b7619582
GF
12682 case DW_AT_description:
12683 return "DW_AT_description";
12684 case DW_AT_binary_scale:
12685 return "DW_AT_binary_scale";
12686 case DW_AT_decimal_scale:
12687 return "DW_AT_decimal_scale";
12688 case DW_AT_small:
12689 return "DW_AT_small";
12690 case DW_AT_decimal_sign:
12691 return "DW_AT_decimal_sign";
12692 case DW_AT_digit_count:
12693 return "DW_AT_digit_count";
12694 case DW_AT_picture_string:
12695 return "DW_AT_picture_string";
12696 case DW_AT_mutable:
12697 return "DW_AT_mutable";
12698 case DW_AT_threads_scaled:
12699 return "DW_AT_threads_scaled";
12700 case DW_AT_explicit:
12701 return "DW_AT_explicit";
12702 case DW_AT_object_pointer:
12703 return "DW_AT_object_pointer";
12704 case DW_AT_endianity:
12705 return "DW_AT_endianity";
12706 case DW_AT_elemental:
12707 return "DW_AT_elemental";
12708 case DW_AT_pure:
12709 return "DW_AT_pure";
12710 case DW_AT_recursive:
12711 return "DW_AT_recursive";
348e048f
DE
12712 /* DWARF 4 values. */
12713 case DW_AT_signature:
12714 return "DW_AT_signature";
31ef98ae
TT
12715 case DW_AT_linkage_name:
12716 return "DW_AT_linkage_name";
b7619582 12717 /* SGI/MIPS extensions. */
c764a876 12718#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12719 case DW_AT_MIPS_fde:
12720 return "DW_AT_MIPS_fde";
c764a876 12721#endif
c906108c
SS
12722 case DW_AT_MIPS_loop_begin:
12723 return "DW_AT_MIPS_loop_begin";
12724 case DW_AT_MIPS_tail_loop_begin:
12725 return "DW_AT_MIPS_tail_loop_begin";
12726 case DW_AT_MIPS_epilog_begin:
12727 return "DW_AT_MIPS_epilog_begin";
12728 case DW_AT_MIPS_loop_unroll_factor:
12729 return "DW_AT_MIPS_loop_unroll_factor";
12730 case DW_AT_MIPS_software_pipeline_depth:
12731 return "DW_AT_MIPS_software_pipeline_depth";
12732 case DW_AT_MIPS_linkage_name:
12733 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12734 case DW_AT_MIPS_stride:
12735 return "DW_AT_MIPS_stride";
12736 case DW_AT_MIPS_abstract_name:
12737 return "DW_AT_MIPS_abstract_name";
12738 case DW_AT_MIPS_clone_origin:
12739 return "DW_AT_MIPS_clone_origin";
12740 case DW_AT_MIPS_has_inlines:
12741 return "DW_AT_MIPS_has_inlines";
b7619582 12742 /* HP extensions. */
c764a876 12743#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12744 case DW_AT_HP_block_index:
12745 return "DW_AT_HP_block_index";
c764a876 12746#endif
b7619582
GF
12747 case DW_AT_HP_unmodifiable:
12748 return "DW_AT_HP_unmodifiable";
12749 case DW_AT_HP_actuals_stmt_list:
12750 return "DW_AT_HP_actuals_stmt_list";
12751 case DW_AT_HP_proc_per_section:
12752 return "DW_AT_HP_proc_per_section";
12753 case DW_AT_HP_raw_data_ptr:
12754 return "DW_AT_HP_raw_data_ptr";
12755 case DW_AT_HP_pass_by_reference:
12756 return "DW_AT_HP_pass_by_reference";
12757 case DW_AT_HP_opt_level:
12758 return "DW_AT_HP_opt_level";
12759 case DW_AT_HP_prof_version_id:
12760 return "DW_AT_HP_prof_version_id";
12761 case DW_AT_HP_opt_flags:
12762 return "DW_AT_HP_opt_flags";
12763 case DW_AT_HP_cold_region_low_pc:
12764 return "DW_AT_HP_cold_region_low_pc";
12765 case DW_AT_HP_cold_region_high_pc:
12766 return "DW_AT_HP_cold_region_high_pc";
12767 case DW_AT_HP_all_variables_modifiable:
12768 return "DW_AT_HP_all_variables_modifiable";
12769 case DW_AT_HP_linkage_name:
12770 return "DW_AT_HP_linkage_name";
12771 case DW_AT_HP_prof_flags:
12772 return "DW_AT_HP_prof_flags";
12773 /* GNU extensions. */
c906108c
SS
12774 case DW_AT_sf_names:
12775 return "DW_AT_sf_names";
12776 case DW_AT_src_info:
12777 return "DW_AT_src_info";
12778 case DW_AT_mac_info:
12779 return "DW_AT_mac_info";
12780 case DW_AT_src_coords:
12781 return "DW_AT_src_coords";
12782 case DW_AT_body_begin:
12783 return "DW_AT_body_begin";
12784 case DW_AT_body_end:
12785 return "DW_AT_body_end";
f5f8a009
EZ
12786 case DW_AT_GNU_vector:
12787 return "DW_AT_GNU_vector";
2de00c64
DE
12788 case DW_AT_GNU_odr_signature:
12789 return "DW_AT_GNU_odr_signature";
b7619582
GF
12790 /* VMS extensions. */
12791 case DW_AT_VMS_rtnbeg_pd_address:
12792 return "DW_AT_VMS_rtnbeg_pd_address";
12793 /* UPC extension. */
12794 case DW_AT_upc_threads_scaled:
12795 return "DW_AT_upc_threads_scaled";
12796 /* PGI (STMicroelectronics) extensions. */
12797 case DW_AT_PGI_lbase:
12798 return "DW_AT_PGI_lbase";
12799 case DW_AT_PGI_soffset:
12800 return "DW_AT_PGI_soffset";
12801 case DW_AT_PGI_lstride:
12802 return "DW_AT_PGI_lstride";
c906108c
SS
12803 default:
12804 return "DW_AT_<unknown>";
12805 }
12806}
12807
12808/* Convert a DWARF value form code into its string name. */
12809
12810static char *
aa1ee363 12811dwarf_form_name (unsigned form)
c906108c
SS
12812{
12813 switch (form)
12814 {
12815 case DW_FORM_addr:
12816 return "DW_FORM_addr";
12817 case DW_FORM_block2:
12818 return "DW_FORM_block2";
12819 case DW_FORM_block4:
12820 return "DW_FORM_block4";
12821 case DW_FORM_data2:
12822 return "DW_FORM_data2";
12823 case DW_FORM_data4:
12824 return "DW_FORM_data4";
12825 case DW_FORM_data8:
12826 return "DW_FORM_data8";
12827 case DW_FORM_string:
12828 return "DW_FORM_string";
12829 case DW_FORM_block:
12830 return "DW_FORM_block";
12831 case DW_FORM_block1:
12832 return "DW_FORM_block1";
12833 case DW_FORM_data1:
12834 return "DW_FORM_data1";
12835 case DW_FORM_flag:
12836 return "DW_FORM_flag";
12837 case DW_FORM_sdata:
12838 return "DW_FORM_sdata";
12839 case DW_FORM_strp:
12840 return "DW_FORM_strp";
12841 case DW_FORM_udata:
12842 return "DW_FORM_udata";
12843 case DW_FORM_ref_addr:
12844 return "DW_FORM_ref_addr";
12845 case DW_FORM_ref1:
12846 return "DW_FORM_ref1";
12847 case DW_FORM_ref2:
12848 return "DW_FORM_ref2";
12849 case DW_FORM_ref4:
12850 return "DW_FORM_ref4";
12851 case DW_FORM_ref8:
12852 return "DW_FORM_ref8";
12853 case DW_FORM_ref_udata:
12854 return "DW_FORM_ref_udata";
12855 case DW_FORM_indirect:
12856 return "DW_FORM_indirect";
348e048f
DE
12857 case DW_FORM_sec_offset:
12858 return "DW_FORM_sec_offset";
12859 case DW_FORM_exprloc:
12860 return "DW_FORM_exprloc";
12861 case DW_FORM_flag_present:
12862 return "DW_FORM_flag_present";
55f1336d
TT
12863 case DW_FORM_ref_sig8:
12864 return "DW_FORM_ref_sig8";
c906108c
SS
12865 default:
12866 return "DW_FORM_<unknown>";
12867 }
12868}
12869
12870/* Convert a DWARF stack opcode into its string name. */
12871
9eae7c52 12872const char *
b1bfef65 12873dwarf_stack_op_name (unsigned op)
c906108c
SS
12874{
12875 switch (op)
12876 {
12877 case DW_OP_addr:
12878 return "DW_OP_addr";
12879 case DW_OP_deref:
12880 return "DW_OP_deref";
12881 case DW_OP_const1u:
12882 return "DW_OP_const1u";
12883 case DW_OP_const1s:
12884 return "DW_OP_const1s";
12885 case DW_OP_const2u:
12886 return "DW_OP_const2u";
12887 case DW_OP_const2s:
12888 return "DW_OP_const2s";
12889 case DW_OP_const4u:
12890 return "DW_OP_const4u";
12891 case DW_OP_const4s:
12892 return "DW_OP_const4s";
12893 case DW_OP_const8u:
12894 return "DW_OP_const8u";
12895 case DW_OP_const8s:
12896 return "DW_OP_const8s";
12897 case DW_OP_constu:
12898 return "DW_OP_constu";
12899 case DW_OP_consts:
12900 return "DW_OP_consts";
12901 case DW_OP_dup:
12902 return "DW_OP_dup";
12903 case DW_OP_drop:
12904 return "DW_OP_drop";
12905 case DW_OP_over:
12906 return "DW_OP_over";
12907 case DW_OP_pick:
12908 return "DW_OP_pick";
12909 case DW_OP_swap:
12910 return "DW_OP_swap";
12911 case DW_OP_rot:
12912 return "DW_OP_rot";
12913 case DW_OP_xderef:
12914 return "DW_OP_xderef";
12915 case DW_OP_abs:
12916 return "DW_OP_abs";
12917 case DW_OP_and:
12918 return "DW_OP_and";
12919 case DW_OP_div:
12920 return "DW_OP_div";
12921 case DW_OP_minus:
12922 return "DW_OP_minus";
12923 case DW_OP_mod:
12924 return "DW_OP_mod";
12925 case DW_OP_mul:
12926 return "DW_OP_mul";
12927 case DW_OP_neg:
12928 return "DW_OP_neg";
12929 case DW_OP_not:
12930 return "DW_OP_not";
12931 case DW_OP_or:
12932 return "DW_OP_or";
12933 case DW_OP_plus:
12934 return "DW_OP_plus";
12935 case DW_OP_plus_uconst:
12936 return "DW_OP_plus_uconst";
12937 case DW_OP_shl:
12938 return "DW_OP_shl";
12939 case DW_OP_shr:
12940 return "DW_OP_shr";
12941 case DW_OP_shra:
12942 return "DW_OP_shra";
12943 case DW_OP_xor:
12944 return "DW_OP_xor";
12945 case DW_OP_bra:
12946 return "DW_OP_bra";
12947 case DW_OP_eq:
12948 return "DW_OP_eq";
12949 case DW_OP_ge:
12950 return "DW_OP_ge";
12951 case DW_OP_gt:
12952 return "DW_OP_gt";
12953 case DW_OP_le:
12954 return "DW_OP_le";
12955 case DW_OP_lt:
12956 return "DW_OP_lt";
12957 case DW_OP_ne:
12958 return "DW_OP_ne";
12959 case DW_OP_skip:
12960 return "DW_OP_skip";
12961 case DW_OP_lit0:
12962 return "DW_OP_lit0";
12963 case DW_OP_lit1:
12964 return "DW_OP_lit1";
12965 case DW_OP_lit2:
12966 return "DW_OP_lit2";
12967 case DW_OP_lit3:
12968 return "DW_OP_lit3";
12969 case DW_OP_lit4:
12970 return "DW_OP_lit4";
12971 case DW_OP_lit5:
12972 return "DW_OP_lit5";
12973 case DW_OP_lit6:
12974 return "DW_OP_lit6";
12975 case DW_OP_lit7:
12976 return "DW_OP_lit7";
12977 case DW_OP_lit8:
12978 return "DW_OP_lit8";
12979 case DW_OP_lit9:
12980 return "DW_OP_lit9";
12981 case DW_OP_lit10:
12982 return "DW_OP_lit10";
12983 case DW_OP_lit11:
12984 return "DW_OP_lit11";
12985 case DW_OP_lit12:
12986 return "DW_OP_lit12";
12987 case DW_OP_lit13:
12988 return "DW_OP_lit13";
12989 case DW_OP_lit14:
12990 return "DW_OP_lit14";
12991 case DW_OP_lit15:
12992 return "DW_OP_lit15";
12993 case DW_OP_lit16:
12994 return "DW_OP_lit16";
12995 case DW_OP_lit17:
12996 return "DW_OP_lit17";
12997 case DW_OP_lit18:
12998 return "DW_OP_lit18";
12999 case DW_OP_lit19:
13000 return "DW_OP_lit19";
13001 case DW_OP_lit20:
13002 return "DW_OP_lit20";
13003 case DW_OP_lit21:
13004 return "DW_OP_lit21";
13005 case DW_OP_lit22:
13006 return "DW_OP_lit22";
13007 case DW_OP_lit23:
13008 return "DW_OP_lit23";
13009 case DW_OP_lit24:
13010 return "DW_OP_lit24";
13011 case DW_OP_lit25:
13012 return "DW_OP_lit25";
13013 case DW_OP_lit26:
13014 return "DW_OP_lit26";
13015 case DW_OP_lit27:
13016 return "DW_OP_lit27";
13017 case DW_OP_lit28:
13018 return "DW_OP_lit28";
13019 case DW_OP_lit29:
13020 return "DW_OP_lit29";
13021 case DW_OP_lit30:
13022 return "DW_OP_lit30";
13023 case DW_OP_lit31:
13024 return "DW_OP_lit31";
13025 case DW_OP_reg0:
13026 return "DW_OP_reg0";
13027 case DW_OP_reg1:
13028 return "DW_OP_reg1";
13029 case DW_OP_reg2:
13030 return "DW_OP_reg2";
13031 case DW_OP_reg3:
13032 return "DW_OP_reg3";
13033 case DW_OP_reg4:
13034 return "DW_OP_reg4";
13035 case DW_OP_reg5:
13036 return "DW_OP_reg5";
13037 case DW_OP_reg6:
13038 return "DW_OP_reg6";
13039 case DW_OP_reg7:
13040 return "DW_OP_reg7";
13041 case DW_OP_reg8:
13042 return "DW_OP_reg8";
13043 case DW_OP_reg9:
13044 return "DW_OP_reg9";
13045 case DW_OP_reg10:
13046 return "DW_OP_reg10";
13047 case DW_OP_reg11:
13048 return "DW_OP_reg11";
13049 case DW_OP_reg12:
13050 return "DW_OP_reg12";
13051 case DW_OP_reg13:
13052 return "DW_OP_reg13";
13053 case DW_OP_reg14:
13054 return "DW_OP_reg14";
13055 case DW_OP_reg15:
13056 return "DW_OP_reg15";
13057 case DW_OP_reg16:
13058 return "DW_OP_reg16";
13059 case DW_OP_reg17:
13060 return "DW_OP_reg17";
13061 case DW_OP_reg18:
13062 return "DW_OP_reg18";
13063 case DW_OP_reg19:
13064 return "DW_OP_reg19";
13065 case DW_OP_reg20:
13066 return "DW_OP_reg20";
13067 case DW_OP_reg21:
13068 return "DW_OP_reg21";
13069 case DW_OP_reg22:
13070 return "DW_OP_reg22";
13071 case DW_OP_reg23:
13072 return "DW_OP_reg23";
13073 case DW_OP_reg24:
13074 return "DW_OP_reg24";
13075 case DW_OP_reg25:
13076 return "DW_OP_reg25";
13077 case DW_OP_reg26:
13078 return "DW_OP_reg26";
13079 case DW_OP_reg27:
13080 return "DW_OP_reg27";
13081 case DW_OP_reg28:
13082 return "DW_OP_reg28";
13083 case DW_OP_reg29:
13084 return "DW_OP_reg29";
13085 case DW_OP_reg30:
13086 return "DW_OP_reg30";
13087 case DW_OP_reg31:
13088 return "DW_OP_reg31";
13089 case DW_OP_breg0:
13090 return "DW_OP_breg0";
13091 case DW_OP_breg1:
13092 return "DW_OP_breg1";
13093 case DW_OP_breg2:
13094 return "DW_OP_breg2";
13095 case DW_OP_breg3:
13096 return "DW_OP_breg3";
13097 case DW_OP_breg4:
13098 return "DW_OP_breg4";
13099 case DW_OP_breg5:
13100 return "DW_OP_breg5";
13101 case DW_OP_breg6:
13102 return "DW_OP_breg6";
13103 case DW_OP_breg7:
13104 return "DW_OP_breg7";
13105 case DW_OP_breg8:
13106 return "DW_OP_breg8";
13107 case DW_OP_breg9:
13108 return "DW_OP_breg9";
13109 case DW_OP_breg10:
13110 return "DW_OP_breg10";
13111 case DW_OP_breg11:
13112 return "DW_OP_breg11";
13113 case DW_OP_breg12:
13114 return "DW_OP_breg12";
13115 case DW_OP_breg13:
13116 return "DW_OP_breg13";
13117 case DW_OP_breg14:
13118 return "DW_OP_breg14";
13119 case DW_OP_breg15:
13120 return "DW_OP_breg15";
13121 case DW_OP_breg16:
13122 return "DW_OP_breg16";
13123 case DW_OP_breg17:
13124 return "DW_OP_breg17";
13125 case DW_OP_breg18:
13126 return "DW_OP_breg18";
13127 case DW_OP_breg19:
13128 return "DW_OP_breg19";
13129 case DW_OP_breg20:
13130 return "DW_OP_breg20";
13131 case DW_OP_breg21:
13132 return "DW_OP_breg21";
13133 case DW_OP_breg22:
13134 return "DW_OP_breg22";
13135 case DW_OP_breg23:
13136 return "DW_OP_breg23";
13137 case DW_OP_breg24:
13138 return "DW_OP_breg24";
13139 case DW_OP_breg25:
13140 return "DW_OP_breg25";
13141 case DW_OP_breg26:
13142 return "DW_OP_breg26";
13143 case DW_OP_breg27:
13144 return "DW_OP_breg27";
13145 case DW_OP_breg28:
13146 return "DW_OP_breg28";
13147 case DW_OP_breg29:
13148 return "DW_OP_breg29";
13149 case DW_OP_breg30:
13150 return "DW_OP_breg30";
13151 case DW_OP_breg31:
13152 return "DW_OP_breg31";
13153 case DW_OP_regx:
13154 return "DW_OP_regx";
13155 case DW_OP_fbreg:
13156 return "DW_OP_fbreg";
13157 case DW_OP_bregx:
13158 return "DW_OP_bregx";
13159 case DW_OP_piece:
13160 return "DW_OP_piece";
13161 case DW_OP_deref_size:
13162 return "DW_OP_deref_size";
13163 case DW_OP_xderef_size:
13164 return "DW_OP_xderef_size";
13165 case DW_OP_nop:
13166 return "DW_OP_nop";
b7619582 13167 /* DWARF 3 extensions. */
ed348acc
EZ
13168 case DW_OP_push_object_address:
13169 return "DW_OP_push_object_address";
13170 case DW_OP_call2:
13171 return "DW_OP_call2";
13172 case DW_OP_call4:
13173 return "DW_OP_call4";
13174 case DW_OP_call_ref:
13175 return "DW_OP_call_ref";
b7619582
GF
13176 case DW_OP_form_tls_address:
13177 return "DW_OP_form_tls_address";
13178 case DW_OP_call_frame_cfa:
13179 return "DW_OP_call_frame_cfa";
13180 case DW_OP_bit_piece:
13181 return "DW_OP_bit_piece";
9eae7c52
TT
13182 /* DWARF 4 extensions. */
13183 case DW_OP_implicit_value:
13184 return "DW_OP_implicit_value";
13185 case DW_OP_stack_value:
13186 return "DW_OP_stack_value";
13187 /* GNU extensions. */
ed348acc
EZ
13188 case DW_OP_GNU_push_tls_address:
13189 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13190 case DW_OP_GNU_uninit:
13191 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13192 case DW_OP_GNU_implicit_pointer:
13193 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13194 case DW_OP_GNU_entry_value:
13195 return "DW_OP_GNU_entry_value";
13196 case DW_OP_GNU_const_type:
13197 return "DW_OP_GNU_const_type";
13198 case DW_OP_GNU_regval_type:
13199 return "DW_OP_GNU_regval_type";
13200 case DW_OP_GNU_deref_type:
13201 return "DW_OP_GNU_deref_type";
13202 case DW_OP_GNU_convert:
13203 return "DW_OP_GNU_convert";
13204 case DW_OP_GNU_reinterpret:
13205 return "DW_OP_GNU_reinterpret";
c906108c 13206 default:
b1bfef65 13207 return NULL;
c906108c
SS
13208 }
13209}
13210
13211static char *
fba45db2 13212dwarf_bool_name (unsigned mybool)
c906108c
SS
13213{
13214 if (mybool)
13215 return "TRUE";
13216 else
13217 return "FALSE";
13218}
13219
13220/* Convert a DWARF type code into its string name. */
13221
13222static char *
aa1ee363 13223dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13224{
13225 switch (enc)
13226 {
b7619582
GF
13227 case DW_ATE_void:
13228 return "DW_ATE_void";
c906108c
SS
13229 case DW_ATE_address:
13230 return "DW_ATE_address";
13231 case DW_ATE_boolean:
13232 return "DW_ATE_boolean";
13233 case DW_ATE_complex_float:
13234 return "DW_ATE_complex_float";
13235 case DW_ATE_float:
13236 return "DW_ATE_float";
13237 case DW_ATE_signed:
13238 return "DW_ATE_signed";
13239 case DW_ATE_signed_char:
13240 return "DW_ATE_signed_char";
13241 case DW_ATE_unsigned:
13242 return "DW_ATE_unsigned";
13243 case DW_ATE_unsigned_char:
13244 return "DW_ATE_unsigned_char";
b7619582 13245 /* DWARF 3. */
d9fa45fe
DC
13246 case DW_ATE_imaginary_float:
13247 return "DW_ATE_imaginary_float";
b7619582
GF
13248 case DW_ATE_packed_decimal:
13249 return "DW_ATE_packed_decimal";
13250 case DW_ATE_numeric_string:
13251 return "DW_ATE_numeric_string";
13252 case DW_ATE_edited:
13253 return "DW_ATE_edited";
13254 case DW_ATE_signed_fixed:
13255 return "DW_ATE_signed_fixed";
13256 case DW_ATE_unsigned_fixed:
13257 return "DW_ATE_unsigned_fixed";
13258 case DW_ATE_decimal_float:
13259 return "DW_ATE_decimal_float";
75079b2b
TT
13260 /* DWARF 4. */
13261 case DW_ATE_UTF:
13262 return "DW_ATE_UTF";
b7619582
GF
13263 /* HP extensions. */
13264 case DW_ATE_HP_float80:
13265 return "DW_ATE_HP_float80";
13266 case DW_ATE_HP_complex_float80:
13267 return "DW_ATE_HP_complex_float80";
13268 case DW_ATE_HP_float128:
13269 return "DW_ATE_HP_float128";
13270 case DW_ATE_HP_complex_float128:
13271 return "DW_ATE_HP_complex_float128";
13272 case DW_ATE_HP_floathpintel:
13273 return "DW_ATE_HP_floathpintel";
13274 case DW_ATE_HP_imaginary_float80:
13275 return "DW_ATE_HP_imaginary_float80";
13276 case DW_ATE_HP_imaginary_float128:
13277 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13278 default:
13279 return "DW_ATE_<unknown>";
13280 }
13281}
13282
0963b4bd 13283/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13284
13285#if 0
13286static char *
aa1ee363 13287dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13288{
13289 switch (cfi_opc)
13290 {
13291 case DW_CFA_advance_loc:
13292 return "DW_CFA_advance_loc";
13293 case DW_CFA_offset:
13294 return "DW_CFA_offset";
13295 case DW_CFA_restore:
13296 return "DW_CFA_restore";
13297 case DW_CFA_nop:
13298 return "DW_CFA_nop";
13299 case DW_CFA_set_loc:
13300 return "DW_CFA_set_loc";
13301 case DW_CFA_advance_loc1:
13302 return "DW_CFA_advance_loc1";
13303 case DW_CFA_advance_loc2:
13304 return "DW_CFA_advance_loc2";
13305 case DW_CFA_advance_loc4:
13306 return "DW_CFA_advance_loc4";
13307 case DW_CFA_offset_extended:
13308 return "DW_CFA_offset_extended";
13309 case DW_CFA_restore_extended:
13310 return "DW_CFA_restore_extended";
13311 case DW_CFA_undefined:
13312 return "DW_CFA_undefined";
13313 case DW_CFA_same_value:
13314 return "DW_CFA_same_value";
13315 case DW_CFA_register:
13316 return "DW_CFA_register";
13317 case DW_CFA_remember_state:
13318 return "DW_CFA_remember_state";
13319 case DW_CFA_restore_state:
13320 return "DW_CFA_restore_state";
13321 case DW_CFA_def_cfa:
13322 return "DW_CFA_def_cfa";
13323 case DW_CFA_def_cfa_register:
13324 return "DW_CFA_def_cfa_register";
13325 case DW_CFA_def_cfa_offset:
13326 return "DW_CFA_def_cfa_offset";
b7619582 13327 /* DWARF 3. */
985cb1a3
JM
13328 case DW_CFA_def_cfa_expression:
13329 return "DW_CFA_def_cfa_expression";
13330 case DW_CFA_expression:
13331 return "DW_CFA_expression";
13332 case DW_CFA_offset_extended_sf:
13333 return "DW_CFA_offset_extended_sf";
13334 case DW_CFA_def_cfa_sf:
13335 return "DW_CFA_def_cfa_sf";
13336 case DW_CFA_def_cfa_offset_sf:
13337 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13338 case DW_CFA_val_offset:
13339 return "DW_CFA_val_offset";
13340 case DW_CFA_val_offset_sf:
13341 return "DW_CFA_val_offset_sf";
13342 case DW_CFA_val_expression:
13343 return "DW_CFA_val_expression";
13344 /* SGI/MIPS specific. */
c906108c
SS
13345 case DW_CFA_MIPS_advance_loc8:
13346 return "DW_CFA_MIPS_advance_loc8";
b7619582 13347 /* GNU extensions. */
985cb1a3
JM
13348 case DW_CFA_GNU_window_save:
13349 return "DW_CFA_GNU_window_save";
13350 case DW_CFA_GNU_args_size:
13351 return "DW_CFA_GNU_args_size";
13352 case DW_CFA_GNU_negative_offset_extended:
13353 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13354 default:
13355 return "DW_CFA_<unknown>";
13356 }
13357}
13358#endif
13359
f9aca02d 13360static void
d97bc12b 13361dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13362{
13363 unsigned int i;
13364
d97bc12b
DE
13365 print_spaces (indent, f);
13366 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13367 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13368
13369 if (die->parent != NULL)
13370 {
13371 print_spaces (indent, f);
13372 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13373 die->parent->offset);
13374 }
13375
13376 print_spaces (indent, f);
13377 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13378 dwarf_bool_name (die->child != NULL));
c906108c 13379
d97bc12b
DE
13380 print_spaces (indent, f);
13381 fprintf_unfiltered (f, " attributes:\n");
13382
c906108c
SS
13383 for (i = 0; i < die->num_attrs; ++i)
13384 {
d97bc12b
DE
13385 print_spaces (indent, f);
13386 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13387 dwarf_attr_name (die->attrs[i].name),
13388 dwarf_form_name (die->attrs[i].form));
d97bc12b 13389
c906108c
SS
13390 switch (die->attrs[i].form)
13391 {
13392 case DW_FORM_ref_addr:
13393 case DW_FORM_addr:
d97bc12b 13394 fprintf_unfiltered (f, "address: ");
5af949e3 13395 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13396 break;
13397 case DW_FORM_block2:
13398 case DW_FORM_block4:
13399 case DW_FORM_block:
13400 case DW_FORM_block1:
3e43a32a
MS
13401 fprintf_unfiltered (f, "block: size %d",
13402 DW_BLOCK (&die->attrs[i])->size);
c906108c 13403 break;
2dc7f7b3
TT
13404 case DW_FORM_exprloc:
13405 fprintf_unfiltered (f, "expression: size %u",
13406 DW_BLOCK (&die->attrs[i])->size);
13407 break;
10b3939b
DJ
13408 case DW_FORM_ref1:
13409 case DW_FORM_ref2:
13410 case DW_FORM_ref4:
d97bc12b 13411 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13412 (long) (DW_ADDR (&die->attrs[i])));
13413 break;
c906108c
SS
13414 case DW_FORM_data1:
13415 case DW_FORM_data2:
13416 case DW_FORM_data4:
ce5d95e1 13417 case DW_FORM_data8:
c906108c
SS
13418 case DW_FORM_udata:
13419 case DW_FORM_sdata:
43bbcdc2
PH
13420 fprintf_unfiltered (f, "constant: %s",
13421 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13422 break;
2dc7f7b3
TT
13423 case DW_FORM_sec_offset:
13424 fprintf_unfiltered (f, "section offset: %s",
13425 pulongest (DW_UNSND (&die->attrs[i])));
13426 break;
55f1336d 13427 case DW_FORM_ref_sig8:
348e048f
DE
13428 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13429 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13430 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13431 else
13432 fprintf_unfiltered (f, "signatured type, offset: unknown");
13433 break;
c906108c 13434 case DW_FORM_string:
4bdf3d34 13435 case DW_FORM_strp:
8285870a 13436 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13437 DW_STRING (&die->attrs[i])
8285870a
JK
13438 ? DW_STRING (&die->attrs[i]) : "",
13439 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13440 break;
13441 case DW_FORM_flag:
13442 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13443 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13444 else
d97bc12b 13445 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13446 break;
2dc7f7b3
TT
13447 case DW_FORM_flag_present:
13448 fprintf_unfiltered (f, "flag: TRUE");
13449 break;
a8329558 13450 case DW_FORM_indirect:
0963b4bd
MS
13451 /* The reader will have reduced the indirect form to
13452 the "base form" so this form should not occur. */
3e43a32a
MS
13453 fprintf_unfiltered (f,
13454 "unexpected attribute form: DW_FORM_indirect");
a8329558 13455 break;
c906108c 13456 default:
d97bc12b 13457 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13458 die->attrs[i].form);
d97bc12b 13459 break;
c906108c 13460 }
d97bc12b 13461 fprintf_unfiltered (f, "\n");
c906108c
SS
13462 }
13463}
13464
f9aca02d 13465static void
d97bc12b 13466dump_die_for_error (struct die_info *die)
c906108c 13467{
d97bc12b
DE
13468 dump_die_shallow (gdb_stderr, 0, die);
13469}
13470
13471static void
13472dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13473{
13474 int indent = level * 4;
13475
13476 gdb_assert (die != NULL);
13477
13478 if (level >= max_level)
13479 return;
13480
13481 dump_die_shallow (f, indent, die);
13482
13483 if (die->child != NULL)
c906108c 13484 {
d97bc12b
DE
13485 print_spaces (indent, f);
13486 fprintf_unfiltered (f, " Children:");
13487 if (level + 1 < max_level)
13488 {
13489 fprintf_unfiltered (f, "\n");
13490 dump_die_1 (f, level + 1, max_level, die->child);
13491 }
13492 else
13493 {
3e43a32a
MS
13494 fprintf_unfiltered (f,
13495 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13496 }
13497 }
13498
13499 if (die->sibling != NULL && level > 0)
13500 {
13501 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13502 }
13503}
13504
d97bc12b
DE
13505/* This is called from the pdie macro in gdbinit.in.
13506 It's not static so gcc will keep a copy callable from gdb. */
13507
13508void
13509dump_die (struct die_info *die, int max_level)
13510{
13511 dump_die_1 (gdb_stdlog, 0, max_level, die);
13512}
13513
f9aca02d 13514static void
51545339 13515store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13516{
51545339 13517 void **slot;
c906108c 13518
51545339
DJ
13519 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13520
13521 *slot = die;
c906108c
SS
13522}
13523
93311388
DE
13524static int
13525is_ref_attr (struct attribute *attr)
c906108c 13526{
c906108c
SS
13527 switch (attr->form)
13528 {
13529 case DW_FORM_ref_addr:
c906108c
SS
13530 case DW_FORM_ref1:
13531 case DW_FORM_ref2:
13532 case DW_FORM_ref4:
613e1657 13533 case DW_FORM_ref8:
c906108c 13534 case DW_FORM_ref_udata:
93311388 13535 return 1;
c906108c 13536 default:
93311388 13537 return 0;
c906108c 13538 }
93311388
DE
13539}
13540
13541static unsigned int
13542dwarf2_get_ref_die_offset (struct attribute *attr)
13543{
13544 if (is_ref_attr (attr))
13545 return DW_ADDR (attr);
13546
13547 complaint (&symfile_complaints,
13548 _("unsupported die ref attribute form: '%s'"),
13549 dwarf_form_name (attr->form));
13550 return 0;
c906108c
SS
13551}
13552
43bbcdc2
PH
13553/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13554 * the value held by the attribute is not constant. */
a02abb62 13555
43bbcdc2 13556static LONGEST
a02abb62
JB
13557dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13558{
13559 if (attr->form == DW_FORM_sdata)
13560 return DW_SND (attr);
13561 else if (attr->form == DW_FORM_udata
13562 || attr->form == DW_FORM_data1
13563 || attr->form == DW_FORM_data2
13564 || attr->form == DW_FORM_data4
13565 || attr->form == DW_FORM_data8)
13566 return DW_UNSND (attr);
13567 else
13568 {
3e43a32a
MS
13569 complaint (&symfile_complaints,
13570 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13571 dwarf_form_name (attr->form));
13572 return default_value;
13573 }
13574}
13575
03dd20cc 13576/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13577 unit and add it to our queue.
13578 The result is non-zero if PER_CU was queued, otherwise the result is zero
13579 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13580
348e048f 13581static int
03dd20cc
DJ
13582maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13583 struct dwarf2_per_cu_data *per_cu)
13584{
98bfdba5
PA
13585 /* We may arrive here during partial symbol reading, if we need full
13586 DIEs to process an unusual case (e.g. template arguments). Do
13587 not queue PER_CU, just tell our caller to load its DIEs. */
13588 if (dwarf2_per_objfile->reading_partial_symbols)
13589 {
13590 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13591 return 1;
13592 return 0;
13593 }
13594
03dd20cc
DJ
13595 /* Mark the dependence relation so that we don't flush PER_CU
13596 too early. */
13597 dwarf2_add_dependence (this_cu, per_cu);
13598
13599 /* If it's already on the queue, we have nothing to do. */
13600 if (per_cu->queued)
348e048f 13601 return 0;
03dd20cc
DJ
13602
13603 /* If the compilation unit is already loaded, just mark it as
13604 used. */
13605 if (per_cu->cu != NULL)
13606 {
13607 per_cu->cu->last_used = 0;
348e048f 13608 return 0;
03dd20cc
DJ
13609 }
13610
13611 /* Add it to the queue. */
13612 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13613
13614 return 1;
13615}
13616
13617/* Follow reference or signature attribute ATTR of SRC_DIE.
13618 On entry *REF_CU is the CU of SRC_DIE.
13619 On exit *REF_CU is the CU of the result. */
13620
13621static struct die_info *
13622follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13623 struct dwarf2_cu **ref_cu)
13624{
13625 struct die_info *die;
13626
13627 if (is_ref_attr (attr))
13628 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13629 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13630 die = follow_die_sig (src_die, attr, ref_cu);
13631 else
13632 {
13633 dump_die_for_error (src_die);
13634 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13635 (*ref_cu)->objfile->name);
13636 }
13637
13638 return die;
03dd20cc
DJ
13639}
13640
5c631832 13641/* Follow reference OFFSET.
673bfd45
DE
13642 On entry *REF_CU is the CU of the source die referencing OFFSET.
13643 On exit *REF_CU is the CU of the result.
13644 Returns NULL if OFFSET is invalid. */
f504f079 13645
f9aca02d 13646static struct die_info *
5c631832 13647follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13648{
10b3939b 13649 struct die_info temp_die;
f2f0e013 13650 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13651
348e048f
DE
13652 gdb_assert (cu->per_cu != NULL);
13653
98bfdba5
PA
13654 target_cu = cu;
13655
348e048f
DE
13656 if (cu->per_cu->from_debug_types)
13657 {
13658 /* .debug_types CUs cannot reference anything outside their CU.
13659 If they need to, they have to reference a signatured type via
55f1336d 13660 DW_FORM_ref_sig8. */
348e048f 13661 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13662 return NULL;
348e048f
DE
13663 }
13664 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13665 {
13666 struct dwarf2_per_cu_data *per_cu;
9a619af0 13667
45452591 13668 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13669
13670 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13671 if (maybe_queue_comp_unit (cu, per_cu))
13672 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13673
10b3939b
DJ
13674 target_cu = per_cu->cu;
13675 }
98bfdba5
PA
13676 else if (cu->dies == NULL)
13677 {
13678 /* We're loading full DIEs during partial symbol reading. */
13679 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13680 load_full_comp_unit (cu->per_cu, cu->objfile);
13681 }
c906108c 13682
f2f0e013 13683 *ref_cu = target_cu;
51545339 13684 temp_die.offset = offset;
5c631832
JK
13685 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13686}
10b3939b 13687
5c631832
JK
13688/* Follow reference attribute ATTR of SRC_DIE.
13689 On entry *REF_CU is the CU of SRC_DIE.
13690 On exit *REF_CU is the CU of the result. */
13691
13692static struct die_info *
13693follow_die_ref (struct die_info *src_die, struct attribute *attr,
13694 struct dwarf2_cu **ref_cu)
13695{
13696 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13697 struct dwarf2_cu *cu = *ref_cu;
13698 struct die_info *die;
13699
13700 die = follow_die_offset (offset, ref_cu);
13701 if (!die)
13702 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13703 "at 0x%x [in module %s]"),
13704 offset, src_die->offset, cu->objfile->name);
348e048f 13705
5c631832
JK
13706 return die;
13707}
13708
13709/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13710 value is intended for DW_OP_call*. */
13711
13712struct dwarf2_locexpr_baton
13713dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13714 struct dwarf2_per_cu_data *per_cu,
13715 CORE_ADDR (*get_frame_pc) (void *baton),
13716 void *baton)
5c631832
JK
13717{
13718 struct dwarf2_cu *cu = per_cu->cu;
13719 struct die_info *die;
13720 struct attribute *attr;
13721 struct dwarf2_locexpr_baton retval;
13722
8cf6f0b1
TT
13723 dw2_setup (per_cu->objfile);
13724
5c631832
JK
13725 die = follow_die_offset (offset, &cu);
13726 if (!die)
13727 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13728 offset, per_cu->cu->objfile->name);
13729
13730 attr = dwarf2_attr (die, DW_AT_location, cu);
13731 if (!attr)
13732 {
13733 /* DWARF: "If there is no such attribute, then there is no effect.". */
13734
13735 retval.data = NULL;
13736 retval.size = 0;
13737 }
8cf6f0b1
TT
13738 else if (attr_form_is_section_offset (attr))
13739 {
13740 struct dwarf2_loclist_baton loclist_baton;
13741 CORE_ADDR pc = (*get_frame_pc) (baton);
13742 size_t size;
13743
13744 fill_in_loclist_baton (cu, &loclist_baton, attr);
13745
13746 retval.data = dwarf2_find_location_expression (&loclist_baton,
13747 &size, pc);
13748 retval.size = size;
13749 }
5c631832
JK
13750 else
13751 {
13752 if (!attr_form_is_block (attr))
13753 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13754 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13755 offset, per_cu->cu->objfile->name);
13756
13757 retval.data = DW_BLOCK (attr)->data;
13758 retval.size = DW_BLOCK (attr)->size;
13759 }
13760 retval.per_cu = cu->per_cu;
13761 return retval;
348e048f
DE
13762}
13763
8a9b8146
TT
13764/* Return the type of the DIE at DIE_OFFSET in the CU named by
13765 PER_CU. */
13766
13767struct type *
13768dwarf2_get_die_type (unsigned int die_offset,
13769 struct dwarf2_per_cu_data *per_cu)
13770{
8a9b8146 13771 dw2_setup (per_cu->objfile);
9ff3b74f 13772 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13773}
13774
348e048f
DE
13775/* Follow the signature attribute ATTR in SRC_DIE.
13776 On entry *REF_CU is the CU of SRC_DIE.
13777 On exit *REF_CU is the CU of the result. */
13778
13779static struct die_info *
13780follow_die_sig (struct die_info *src_die, struct attribute *attr,
13781 struct dwarf2_cu **ref_cu)
13782{
13783 struct objfile *objfile = (*ref_cu)->objfile;
13784 struct die_info temp_die;
13785 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13786 struct dwarf2_cu *sig_cu;
13787 struct die_info *die;
13788
13789 /* sig_type will be NULL if the signatured type is missing from
13790 the debug info. */
13791 if (sig_type == NULL)
13792 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13793 "at 0x%x [in module %s]"),
13794 src_die->offset, objfile->name);
13795
13796 /* If necessary, add it to the queue and load its DIEs. */
13797
13798 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13799 read_signatured_type (objfile, sig_type);
13800
13801 gdb_assert (sig_type->per_cu.cu != NULL);
13802
13803 sig_cu = sig_type->per_cu.cu;
13804 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13805 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13806 if (die)
13807 {
13808 *ref_cu = sig_cu;
13809 return die;
13810 }
13811
3e43a32a
MS
13812 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13813 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13814 sig_type->type_offset, src_die->offset, objfile->name);
13815}
13816
13817/* Given an offset of a signatured type, return its signatured_type. */
13818
13819static struct signatured_type *
13820lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13821{
13822 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13823 unsigned int length, initial_length_size;
13824 unsigned int sig_offset;
13825 struct signatured_type find_entry, *type_sig;
13826
13827 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13828 sig_offset = (initial_length_size
13829 + 2 /*version*/
13830 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13831 + 1 /*address_size*/);
13832 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13833 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13834
13835 /* This is only used to lookup previously recorded types.
13836 If we didn't find it, it's our bug. */
13837 gdb_assert (type_sig != NULL);
b3c8eb43 13838 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
13839
13840 return type_sig;
13841}
13842
13843/* Read in signatured type at OFFSET and build its CU and die(s). */
13844
13845static void
13846read_signatured_type_at_offset (struct objfile *objfile,
13847 unsigned int offset)
13848{
13849 struct signatured_type *type_sig;
13850
be391dca
TT
13851 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13852
348e048f
DE
13853 /* We have the section offset, but we need the signature to do the
13854 hash table lookup. */
13855 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13856
13857 gdb_assert (type_sig->per_cu.cu == NULL);
13858
13859 read_signatured_type (objfile, type_sig);
13860
13861 gdb_assert (type_sig->per_cu.cu != NULL);
13862}
13863
13864/* Read in a signatured type and build its CU and DIEs. */
13865
13866static void
13867read_signatured_type (struct objfile *objfile,
13868 struct signatured_type *type_sig)
13869{
1fd400ff 13870 gdb_byte *types_ptr;
348e048f
DE
13871 struct die_reader_specs reader_specs;
13872 struct dwarf2_cu *cu;
13873 ULONGEST signature;
13874 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13875
1fd400ff 13876 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
b3c8eb43 13877 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
1fd400ff 13878
348e048f
DE
13879 gdb_assert (type_sig->per_cu.cu == NULL);
13880
9816fde3
JK
13881 cu = xmalloc (sizeof (*cu));
13882 init_one_comp_unit (cu, objfile);
13883
348e048f
DE
13884 type_sig->per_cu.cu = cu;
13885 cu->per_cu = &type_sig->per_cu;
13886
13887 /* If an error occurs while loading, release our storage. */
13888 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13889
13890 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13891 types_ptr, objfile->obfd);
13892 gdb_assert (signature == type_sig->signature);
13893
13894 cu->die_hash
13895 = htab_create_alloc_ex (cu->header.length / 12,
13896 die_hash,
13897 die_eq,
13898 NULL,
13899 &cu->comp_unit_obstack,
13900 hashtab_obstack_allocate,
13901 dummy_obstack_deallocate);
13902
13903 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13904 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13905
13906 init_cu_die_reader (&reader_specs, cu);
13907
13908 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13909 NULL /*parent*/);
13910
13911 /* We try not to read any attributes in this function, because not
13912 all objfiles needed for references have been loaded yet, and symbol
13913 table processing isn't initialized. But we have to set the CU language,
13914 or we won't be able to build types correctly. */
9816fde3 13915 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13916
13917 do_cleanups (back_to);
13918
13919 /* We've successfully allocated this compilation unit. Let our caller
13920 clean it up when finished with it. */
13921 discard_cleanups (free_cu_cleanup);
13922
13923 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13924 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13925}
13926
c906108c
SS
13927/* Decode simple location descriptions.
13928 Given a pointer to a dwarf block that defines a location, compute
13929 the location and return the value.
13930
4cecd739
DJ
13931 NOTE drow/2003-11-18: This function is called in two situations
13932 now: for the address of static or global variables (partial symbols
13933 only) and for offsets into structures which are expected to be
13934 (more or less) constant. The partial symbol case should go away,
13935 and only the constant case should remain. That will let this
13936 function complain more accurately. A few special modes are allowed
13937 without complaint for global variables (for instance, global
13938 register values and thread-local values).
c906108c
SS
13939
13940 A location description containing no operations indicates that the
4cecd739 13941 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13942 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13943 callers will only want a very basic result and this can become a
13944 complaint.
c906108c 13945
d53d4ac5 13946 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13947
13948static CORE_ADDR
e7c27a73 13949decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13950{
e7c27a73 13951 struct objfile *objfile = cu->objfile;
c906108c
SS
13952 int i;
13953 int size = blk->size;
fe1b8b76 13954 gdb_byte *data = blk->data;
c906108c
SS
13955 CORE_ADDR stack[64];
13956 int stacki;
13957 unsigned int bytes_read, unsnd;
fe1b8b76 13958 gdb_byte op;
c906108c
SS
13959
13960 i = 0;
13961 stacki = 0;
13962 stack[stacki] = 0;
d53d4ac5 13963 stack[++stacki] = 0;
c906108c
SS
13964
13965 while (i < size)
13966 {
c906108c
SS
13967 op = data[i++];
13968 switch (op)
13969 {
f1bea926
JM
13970 case DW_OP_lit0:
13971 case DW_OP_lit1:
13972 case DW_OP_lit2:
13973 case DW_OP_lit3:
13974 case DW_OP_lit4:
13975 case DW_OP_lit5:
13976 case DW_OP_lit6:
13977 case DW_OP_lit7:
13978 case DW_OP_lit8:
13979 case DW_OP_lit9:
13980 case DW_OP_lit10:
13981 case DW_OP_lit11:
13982 case DW_OP_lit12:
13983 case DW_OP_lit13:
13984 case DW_OP_lit14:
13985 case DW_OP_lit15:
13986 case DW_OP_lit16:
13987 case DW_OP_lit17:
13988 case DW_OP_lit18:
13989 case DW_OP_lit19:
13990 case DW_OP_lit20:
13991 case DW_OP_lit21:
13992 case DW_OP_lit22:
13993 case DW_OP_lit23:
13994 case DW_OP_lit24:
13995 case DW_OP_lit25:
13996 case DW_OP_lit26:
13997 case DW_OP_lit27:
13998 case DW_OP_lit28:
13999 case DW_OP_lit29:
14000 case DW_OP_lit30:
14001 case DW_OP_lit31:
14002 stack[++stacki] = op - DW_OP_lit0;
14003 break;
14004
c906108c
SS
14005 case DW_OP_reg0:
14006 case DW_OP_reg1:
14007 case DW_OP_reg2:
14008 case DW_OP_reg3:
14009 case DW_OP_reg4:
14010 case DW_OP_reg5:
14011 case DW_OP_reg6:
14012 case DW_OP_reg7:
14013 case DW_OP_reg8:
14014 case DW_OP_reg9:
14015 case DW_OP_reg10:
14016 case DW_OP_reg11:
14017 case DW_OP_reg12:
14018 case DW_OP_reg13:
14019 case DW_OP_reg14:
14020 case DW_OP_reg15:
14021 case DW_OP_reg16:
14022 case DW_OP_reg17:
14023 case DW_OP_reg18:
14024 case DW_OP_reg19:
14025 case DW_OP_reg20:
14026 case DW_OP_reg21:
14027 case DW_OP_reg22:
14028 case DW_OP_reg23:
14029 case DW_OP_reg24:
14030 case DW_OP_reg25:
14031 case DW_OP_reg26:
14032 case DW_OP_reg27:
14033 case DW_OP_reg28:
14034 case DW_OP_reg29:
14035 case DW_OP_reg30:
14036 case DW_OP_reg31:
c906108c 14037 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
14038 if (i < size)
14039 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14040 break;
14041
14042 case DW_OP_regx:
c906108c
SS
14043 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14044 i += bytes_read;
c906108c 14045 stack[++stacki] = unsnd;
4cecd739
DJ
14046 if (i < size)
14047 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14048 break;
14049
14050 case DW_OP_addr:
107d2387 14051 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 14052 cu, &bytes_read);
107d2387 14053 i += bytes_read;
c906108c
SS
14054 break;
14055
14056 case DW_OP_const1u:
14057 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14058 i += 1;
14059 break;
14060
14061 case DW_OP_const1s:
14062 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14063 i += 1;
14064 break;
14065
14066 case DW_OP_const2u:
14067 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14068 i += 2;
14069 break;
14070
14071 case DW_OP_const2s:
14072 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14073 i += 2;
14074 break;
14075
14076 case DW_OP_const4u:
14077 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14078 i += 4;
14079 break;
14080
14081 case DW_OP_const4s:
14082 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14083 i += 4;
14084 break;
14085
14086 case DW_OP_constu:
14087 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 14088 &bytes_read);
c906108c
SS
14089 i += bytes_read;
14090 break;
14091
14092 case DW_OP_consts:
14093 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14094 i += bytes_read;
14095 break;
14096
f1bea926
JM
14097 case DW_OP_dup:
14098 stack[stacki + 1] = stack[stacki];
14099 stacki++;
14100 break;
14101
c906108c
SS
14102 case DW_OP_plus:
14103 stack[stacki - 1] += stack[stacki];
14104 stacki--;
14105 break;
14106
14107 case DW_OP_plus_uconst:
3e43a32a
MS
14108 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14109 &bytes_read);
c906108c
SS
14110 i += bytes_read;
14111 break;
14112
14113 case DW_OP_minus:
f1bea926 14114 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14115 stacki--;
14116 break;
14117
7a292a7a 14118 case DW_OP_deref:
7a292a7a 14119 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14120 this using GDB's address_class enum. This is valid for partial
14121 global symbols, although the variable's address will be bogus
14122 in the psymtab. */
7a292a7a 14123 if (i < size)
4d3c2250 14124 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14125 break;
14126
9d774e44 14127 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14128 /* The top of the stack has the offset from the beginning
14129 of the thread control block at which the variable is located. */
14130 /* Nothing should follow this operator, so the top of stack would
14131 be returned. */
4cecd739
DJ
14132 /* This is valid for partial global symbols, but the variable's
14133 address will be bogus in the psymtab. */
9d774e44 14134 if (i < size)
4d3c2250 14135 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14136 break;
14137
42be36b3
CT
14138 case DW_OP_GNU_uninit:
14139 break;
14140
c906108c 14141 default:
b1bfef65
TT
14142 {
14143 const char *name = dwarf_stack_op_name (op);
14144
14145 if (name)
14146 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14147 name);
14148 else
14149 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14150 op);
14151 }
14152
c906108c
SS
14153 return (stack[stacki]);
14154 }
d53d4ac5
TT
14155
14156 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14157 outside of the allocated space. Also enforce minimum>0. */
14158 if (stacki >= ARRAY_SIZE (stack) - 1)
14159 {
14160 complaint (&symfile_complaints,
14161 _("location description stack overflow"));
14162 return 0;
14163 }
14164
14165 if (stacki <= 0)
14166 {
14167 complaint (&symfile_complaints,
14168 _("location description stack underflow"));
14169 return 0;
14170 }
c906108c
SS
14171 }
14172 return (stack[stacki]);
14173}
14174
14175/* memory allocation interface */
14176
c906108c 14177static struct dwarf_block *
7b5a2f43 14178dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14179{
14180 struct dwarf_block *blk;
14181
14182 blk = (struct dwarf_block *)
7b5a2f43 14183 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14184 return (blk);
14185}
14186
14187static struct abbrev_info *
f3dd6933 14188dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14189{
14190 struct abbrev_info *abbrev;
14191
f3dd6933
DJ
14192 abbrev = (struct abbrev_info *)
14193 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14194 memset (abbrev, 0, sizeof (struct abbrev_info));
14195 return (abbrev);
14196}
14197
14198static struct die_info *
b60c80d6 14199dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14200{
14201 struct die_info *die;
b60c80d6
DJ
14202 size_t size = sizeof (struct die_info);
14203
14204 if (num_attrs > 1)
14205 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14206
b60c80d6 14207 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14208 memset (die, 0, sizeof (struct die_info));
14209 return (die);
14210}
2e276125
JB
14211
14212\f
14213/* Macro support. */
14214
2e276125
JB
14215/* Return the full name of file number I in *LH's file name table.
14216 Use COMP_DIR as the name of the current directory of the
14217 compilation. The result is allocated using xmalloc; the caller is
14218 responsible for freeing it. */
14219static char *
14220file_full_name (int file, struct line_header *lh, const char *comp_dir)
14221{
6a83a1e6
EZ
14222 /* Is the file number a valid index into the line header's file name
14223 table? Remember that file numbers start with one, not zero. */
14224 if (1 <= file && file <= lh->num_file_names)
14225 {
14226 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14227
6a83a1e6
EZ
14228 if (IS_ABSOLUTE_PATH (fe->name))
14229 return xstrdup (fe->name);
14230 else
14231 {
14232 const char *dir;
14233 int dir_len;
14234 char *full_name;
14235
14236 if (fe->dir_index)
14237 dir = lh->include_dirs[fe->dir_index - 1];
14238 else
14239 dir = comp_dir;
14240
14241 if (dir)
14242 {
14243 dir_len = strlen (dir);
14244 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14245 strcpy (full_name, dir);
14246 full_name[dir_len] = '/';
14247 strcpy (full_name + dir_len + 1, fe->name);
14248 return full_name;
14249 }
14250 else
14251 return xstrdup (fe->name);
14252 }
14253 }
2e276125
JB
14254 else
14255 {
6a83a1e6
EZ
14256 /* The compiler produced a bogus file number. We can at least
14257 record the macro definitions made in the file, even if we
14258 won't be able to find the file by name. */
14259 char fake_name[80];
9a619af0 14260
6a83a1e6 14261 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14262
6e70227d 14263 complaint (&symfile_complaints,
6a83a1e6
EZ
14264 _("bad file number in macro information (%d)"),
14265 file);
2e276125 14266
6a83a1e6 14267 return xstrdup (fake_name);
2e276125
JB
14268 }
14269}
14270
14271
14272static struct macro_source_file *
14273macro_start_file (int file, int line,
14274 struct macro_source_file *current_file,
14275 const char *comp_dir,
14276 struct line_header *lh, struct objfile *objfile)
14277{
14278 /* The full name of this source file. */
14279 char *full_name = file_full_name (file, lh, comp_dir);
14280
14281 /* We don't create a macro table for this compilation unit
14282 at all until we actually get a filename. */
14283 if (! pending_macros)
4a146b47 14284 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14285 objfile->macro_cache);
2e276125
JB
14286
14287 if (! current_file)
14288 /* If we have no current file, then this must be the start_file
14289 directive for the compilation unit's main source file. */
14290 current_file = macro_set_main (pending_macros, full_name);
14291 else
14292 current_file = macro_include (current_file, line, full_name);
14293
14294 xfree (full_name);
6e70227d 14295
2e276125
JB
14296 return current_file;
14297}
14298
14299
14300/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14301 followed by a null byte. */
14302static char *
14303copy_string (const char *buf, int len)
14304{
14305 char *s = xmalloc (len + 1);
9a619af0 14306
2e276125
JB
14307 memcpy (s, buf, len);
14308 s[len] = '\0';
2e276125
JB
14309 return s;
14310}
14311
14312
14313static const char *
14314consume_improper_spaces (const char *p, const char *body)
14315{
14316 if (*p == ' ')
14317 {
4d3c2250 14318 complaint (&symfile_complaints,
3e43a32a
MS
14319 _("macro definition contains spaces "
14320 "in formal argument list:\n`%s'"),
4d3c2250 14321 body);
2e276125
JB
14322
14323 while (*p == ' ')
14324 p++;
14325 }
14326
14327 return p;
14328}
14329
14330
14331static void
14332parse_macro_definition (struct macro_source_file *file, int line,
14333 const char *body)
14334{
14335 const char *p;
14336
14337 /* The body string takes one of two forms. For object-like macro
14338 definitions, it should be:
14339
14340 <macro name> " " <definition>
14341
14342 For function-like macro definitions, it should be:
14343
14344 <macro name> "() " <definition>
14345 or
14346 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14347
14348 Spaces may appear only where explicitly indicated, and in the
14349 <definition>.
14350
14351 The Dwarf 2 spec says that an object-like macro's name is always
14352 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14353 the space when the macro's definition is the empty string.
2e276125
JB
14354
14355 The Dwarf 2 spec says that there should be no spaces between the
14356 formal arguments in a function-like macro's formal argument list,
14357 but versions of GCC around March 2002 include spaces after the
14358 commas. */
14359
14360
14361 /* Find the extent of the macro name. The macro name is terminated
14362 by either a space or null character (for an object-like macro) or
14363 an opening paren (for a function-like macro). */
14364 for (p = body; *p; p++)
14365 if (*p == ' ' || *p == '(')
14366 break;
14367
14368 if (*p == ' ' || *p == '\0')
14369 {
14370 /* It's an object-like macro. */
14371 int name_len = p - body;
14372 char *name = copy_string (body, name_len);
14373 const char *replacement;
14374
14375 if (*p == ' ')
14376 replacement = body + name_len + 1;
14377 else
14378 {
4d3c2250 14379 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14380 replacement = body + name_len;
14381 }
6e70227d 14382
2e276125
JB
14383 macro_define_object (file, line, name, replacement);
14384
14385 xfree (name);
14386 }
14387 else if (*p == '(')
14388 {
14389 /* It's a function-like macro. */
14390 char *name = copy_string (body, p - body);
14391 int argc = 0;
14392 int argv_size = 1;
14393 char **argv = xmalloc (argv_size * sizeof (*argv));
14394
14395 p++;
14396
14397 p = consume_improper_spaces (p, body);
14398
14399 /* Parse the formal argument list. */
14400 while (*p && *p != ')')
14401 {
14402 /* Find the extent of the current argument name. */
14403 const char *arg_start = p;
14404
14405 while (*p && *p != ',' && *p != ')' && *p != ' ')
14406 p++;
14407
14408 if (! *p || p == arg_start)
4d3c2250 14409 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14410 else
14411 {
14412 /* Make sure argv has room for the new argument. */
14413 if (argc >= argv_size)
14414 {
14415 argv_size *= 2;
14416 argv = xrealloc (argv, argv_size * sizeof (*argv));
14417 }
14418
14419 argv[argc++] = copy_string (arg_start, p - arg_start);
14420 }
14421
14422 p = consume_improper_spaces (p, body);
14423
14424 /* Consume the comma, if present. */
14425 if (*p == ',')
14426 {
14427 p++;
14428
14429 p = consume_improper_spaces (p, body);
14430 }
14431 }
14432
14433 if (*p == ')')
14434 {
14435 p++;
14436
14437 if (*p == ' ')
14438 /* Perfectly formed definition, no complaints. */
14439 macro_define_function (file, line, name,
6e70227d 14440 argc, (const char **) argv,
2e276125
JB
14441 p + 1);
14442 else if (*p == '\0')
14443 {
14444 /* Complain, but do define it. */
4d3c2250 14445 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14446 macro_define_function (file, line, name,
6e70227d 14447 argc, (const char **) argv,
2e276125
JB
14448 p);
14449 }
14450 else
14451 /* Just complain. */
4d3c2250 14452 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14453 }
14454 else
14455 /* Just complain. */
4d3c2250 14456 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14457
14458 xfree (name);
14459 {
14460 int i;
14461
14462 for (i = 0; i < argc; i++)
14463 xfree (argv[i]);
14464 }
14465 xfree (argv);
14466 }
14467 else
4d3c2250 14468 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14469}
14470
14471
14472static void
14473dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14474 char *comp_dir, bfd *abfd,
e7c27a73 14475 struct dwarf2_cu *cu)
2e276125 14476{
fe1b8b76 14477 gdb_byte *mac_ptr, *mac_end;
2e276125 14478 struct macro_source_file *current_file = 0;
757a13d0
JK
14479 enum dwarf_macinfo_record_type macinfo_type;
14480 int at_commandline;
2e276125 14481
be391dca
TT
14482 dwarf2_read_section (dwarf2_per_objfile->objfile,
14483 &dwarf2_per_objfile->macinfo);
dce234bc 14484 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14485 {
e2e0b3e5 14486 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14487 return;
14488 }
14489
757a13d0
JK
14490 /* First pass: Find the name of the base filename.
14491 This filename is needed in order to process all macros whose definition
14492 (or undefinition) comes from the command line. These macros are defined
14493 before the first DW_MACINFO_start_file entry, and yet still need to be
14494 associated to the base file.
14495
14496 To determine the base file name, we scan the macro definitions until we
14497 reach the first DW_MACINFO_start_file entry. We then initialize
14498 CURRENT_FILE accordingly so that any macro definition found before the
14499 first DW_MACINFO_start_file can still be associated to the base file. */
14500
dce234bc
PP
14501 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14502 mac_end = dwarf2_per_objfile->macinfo.buffer
14503 + dwarf2_per_objfile->macinfo.size;
2e276125 14504
757a13d0 14505 do
2e276125 14506 {
2e276125
JB
14507 /* Do we at least have room for a macinfo type byte? */
14508 if (mac_ptr >= mac_end)
14509 {
757a13d0 14510 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14511 stop the first pass earlier upon finding
14512 DW_MACINFO_start_file. */
757a13d0 14513 break;
2e276125
JB
14514 }
14515
14516 macinfo_type = read_1_byte (abfd, mac_ptr);
14517 mac_ptr++;
14518
14519 switch (macinfo_type)
14520 {
14521 /* A zero macinfo type indicates the end of the macro
14522 information. */
14523 case 0:
757a13d0
JK
14524 break;
14525
14526 case DW_MACINFO_define:
14527 case DW_MACINFO_undef:
14528 /* Only skip the data by MAC_PTR. */
14529 {
14530 unsigned int bytes_read;
14531
14532 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14533 mac_ptr += bytes_read;
9b1c24c8 14534 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14535 mac_ptr += bytes_read;
14536 }
14537 break;
14538
14539 case DW_MACINFO_start_file:
14540 {
14541 unsigned int bytes_read;
14542 int line, file;
14543
14544 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14545 mac_ptr += bytes_read;
14546 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14547 mac_ptr += bytes_read;
14548
3e43a32a
MS
14549 current_file = macro_start_file (file, line, current_file,
14550 comp_dir, lh, cu->objfile);
757a13d0
JK
14551 }
14552 break;
14553
14554 case DW_MACINFO_end_file:
14555 /* No data to skip by MAC_PTR. */
14556 break;
14557
14558 case DW_MACINFO_vendor_ext:
14559 /* Only skip the data by MAC_PTR. */
14560 {
14561 unsigned int bytes_read;
14562
14563 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14564 mac_ptr += bytes_read;
9b1c24c8 14565 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14566 mac_ptr += bytes_read;
14567 }
14568 break;
14569
14570 default:
14571 break;
14572 }
14573 } while (macinfo_type != 0 && current_file == NULL);
14574
14575 /* Second pass: Process all entries.
14576
14577 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14578 command-line macro definitions/undefinitions. This flag is unset when we
14579 reach the first DW_MACINFO_start_file entry. */
14580
dce234bc 14581 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14582
14583 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14584 GDB is still reading the definitions from command line. First
14585 DW_MACINFO_start_file will need to be ignored as it was already executed
14586 to create CURRENT_FILE for the main source holding also the command line
14587 definitions. On first met DW_MACINFO_start_file this flag is reset to
14588 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14589
14590 at_commandline = 1;
14591
14592 do
14593 {
14594 /* Do we at least have room for a macinfo type byte? */
14595 if (mac_ptr >= mac_end)
14596 {
14597 dwarf2_macros_too_long_complaint ();
14598 break;
14599 }
14600
14601 macinfo_type = read_1_byte (abfd, mac_ptr);
14602 mac_ptr++;
14603
14604 switch (macinfo_type)
14605 {
14606 /* A zero macinfo type indicates the end of the macro
14607 information. */
14608 case 0:
14609 break;
2e276125
JB
14610
14611 case DW_MACINFO_define:
14612 case DW_MACINFO_undef:
14613 {
891d2f0b 14614 unsigned int bytes_read;
2e276125
JB
14615 int line;
14616 char *body;
14617
14618 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14619 mac_ptr += bytes_read;
9b1c24c8 14620 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14621 mac_ptr += bytes_read;
14622
14623 if (! current_file)
757a13d0
JK
14624 {
14625 /* DWARF violation as no main source is present. */
14626 complaint (&symfile_complaints,
14627 _("debug info with no main source gives macro %s "
14628 "on line %d: %s"),
6e70227d
DE
14629 macinfo_type == DW_MACINFO_define ?
14630 _("definition") :
905e0470
PM
14631 macinfo_type == DW_MACINFO_undef ?
14632 _("undefinition") :
14633 _("something-or-other"), line, body);
757a13d0
JK
14634 break;
14635 }
3e43a32a
MS
14636 if ((line == 0 && !at_commandline)
14637 || (line != 0 && at_commandline))
4d3c2250 14638 complaint (&symfile_complaints,
757a13d0
JK
14639 _("debug info gives %s macro %s with %s line %d: %s"),
14640 at_commandline ? _("command-line") : _("in-file"),
905e0470 14641 macinfo_type == DW_MACINFO_define ?
6e70227d 14642 _("definition") :
905e0470
PM
14643 macinfo_type == DW_MACINFO_undef ?
14644 _("undefinition") :
14645 _("something-or-other"),
757a13d0
JK
14646 line == 0 ? _("zero") : _("non-zero"), line, body);
14647
14648 if (macinfo_type == DW_MACINFO_define)
14649 parse_macro_definition (current_file, line, body);
14650 else if (macinfo_type == DW_MACINFO_undef)
14651 macro_undef (current_file, line, body);
2e276125
JB
14652 }
14653 break;
14654
14655 case DW_MACINFO_start_file:
14656 {
891d2f0b 14657 unsigned int bytes_read;
2e276125
JB
14658 int line, file;
14659
14660 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14661 mac_ptr += bytes_read;
14662 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14663 mac_ptr += bytes_read;
14664
3e43a32a
MS
14665 if ((line == 0 && !at_commandline)
14666 || (line != 0 && at_commandline))
757a13d0
JK
14667 complaint (&symfile_complaints,
14668 _("debug info gives source %d included "
14669 "from %s at %s line %d"),
14670 file, at_commandline ? _("command-line") : _("file"),
14671 line == 0 ? _("zero") : _("non-zero"), line);
14672
14673 if (at_commandline)
14674 {
14675 /* This DW_MACINFO_start_file was executed in the pass one. */
14676 at_commandline = 0;
14677 }
14678 else
14679 current_file = macro_start_file (file, line,
14680 current_file, comp_dir,
14681 lh, cu->objfile);
2e276125
JB
14682 }
14683 break;
14684
14685 case DW_MACINFO_end_file:
14686 if (! current_file)
4d3c2250 14687 complaint (&symfile_complaints,
3e43a32a
MS
14688 _("macro debug info has an unmatched "
14689 "`close_file' directive"));
2e276125
JB
14690 else
14691 {
14692 current_file = current_file->included_by;
14693 if (! current_file)
14694 {
14695 enum dwarf_macinfo_record_type next_type;
14696
14697 /* GCC circa March 2002 doesn't produce the zero
14698 type byte marking the end of the compilation
14699 unit. Complain if it's not there, but exit no
14700 matter what. */
14701
14702 /* Do we at least have room for a macinfo type byte? */
14703 if (mac_ptr >= mac_end)
14704 {
4d3c2250 14705 dwarf2_macros_too_long_complaint ();
2e276125
JB
14706 return;
14707 }
14708
14709 /* We don't increment mac_ptr here, so this is just
14710 a look-ahead. */
14711 next_type = read_1_byte (abfd, mac_ptr);
14712 if (next_type != 0)
4d3c2250 14713 complaint (&symfile_complaints,
3e43a32a
MS
14714 _("no terminating 0-type entry for "
14715 "macros in `.debug_macinfo' section"));
2e276125
JB
14716
14717 return;
14718 }
14719 }
14720 break;
14721
14722 case DW_MACINFO_vendor_ext:
14723 {
891d2f0b 14724 unsigned int bytes_read;
2e276125 14725 int constant;
2e276125
JB
14726
14727 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14728 mac_ptr += bytes_read;
e8e80198 14729 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14730 mac_ptr += bytes_read;
14731
14732 /* We don't recognize any vendor extensions. */
14733 }
14734 break;
14735 }
757a13d0 14736 } while (macinfo_type != 0);
2e276125 14737}
8e19ed76
PS
14738
14739/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14740 if so return true else false. */
8e19ed76
PS
14741static int
14742attr_form_is_block (struct attribute *attr)
14743{
14744 return (attr == NULL ? 0 :
14745 attr->form == DW_FORM_block1
14746 || attr->form == DW_FORM_block2
14747 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14748 || attr->form == DW_FORM_block
14749 || attr->form == DW_FORM_exprloc);
8e19ed76 14750}
4c2df51b 14751
c6a0999f
JB
14752/* Return non-zero if ATTR's value is a section offset --- classes
14753 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14754 You may use DW_UNSND (attr) to retrieve such offsets.
14755
14756 Section 7.5.4, "Attribute Encodings", explains that no attribute
14757 may have a value that belongs to more than one of these classes; it
14758 would be ambiguous if we did, because we use the same forms for all
14759 of them. */
3690dd37
JB
14760static int
14761attr_form_is_section_offset (struct attribute *attr)
14762{
14763 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14764 || attr->form == DW_FORM_data8
14765 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14766}
14767
14768
14769/* Return non-zero if ATTR's value falls in the 'constant' class, or
14770 zero otherwise. When this function returns true, you can apply
14771 dwarf2_get_attr_constant_value to it.
14772
14773 However, note that for some attributes you must check
14774 attr_form_is_section_offset before using this test. DW_FORM_data4
14775 and DW_FORM_data8 are members of both the constant class, and of
14776 the classes that contain offsets into other debug sections
14777 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14778 that, if an attribute's can be either a constant or one of the
14779 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14780 taken as section offsets, not constants. */
14781static int
14782attr_form_is_constant (struct attribute *attr)
14783{
14784 switch (attr->form)
14785 {
14786 case DW_FORM_sdata:
14787 case DW_FORM_udata:
14788 case DW_FORM_data1:
14789 case DW_FORM_data2:
14790 case DW_FORM_data4:
14791 case DW_FORM_data8:
14792 return 1;
14793 default:
14794 return 0;
14795 }
14796}
14797
8cf6f0b1
TT
14798/* A helper function that fills in a dwarf2_loclist_baton. */
14799
14800static void
14801fill_in_loclist_baton (struct dwarf2_cu *cu,
14802 struct dwarf2_loclist_baton *baton,
14803 struct attribute *attr)
14804{
14805 dwarf2_read_section (dwarf2_per_objfile->objfile,
14806 &dwarf2_per_objfile->loc);
14807
14808 baton->per_cu = cu->per_cu;
14809 gdb_assert (baton->per_cu);
14810 /* We don't know how long the location list is, but make sure we
14811 don't run off the edge of the section. */
14812 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14813 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14814 baton->base_address = cu->base_address;
14815}
14816
4c2df51b
DJ
14817static void
14818dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14819 struct dwarf2_cu *cu)
4c2df51b 14820{
3690dd37 14821 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14822 /* ".debug_loc" may not exist at all, or the offset may be outside
14823 the section. If so, fall through to the complaint in the
14824 other branch. */
9e0ac564
TT
14825 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14826 &dwarf2_per_objfile->loc))
4c2df51b 14827 {
0d53c4c4 14828 struct dwarf2_loclist_baton *baton;
4c2df51b 14829
4a146b47 14830 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14831 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14832
8cf6f0b1 14833 fill_in_loclist_baton (cu, baton, attr);
be391dca 14834
d00adf39 14835 if (cu->base_known == 0)
0d53c4c4 14836 complaint (&symfile_complaints,
3e43a32a
MS
14837 _("Location list used without "
14838 "specifying the CU base address."));
4c2df51b 14839
768a979c 14840 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14841 SYMBOL_LOCATION_BATON (sym) = baton;
14842 }
14843 else
14844 {
14845 struct dwarf2_locexpr_baton *baton;
14846
4a146b47 14847 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14848 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14849 baton->per_cu = cu->per_cu;
14850 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14851
14852 if (attr_form_is_block (attr))
14853 {
14854 /* Note that we're just copying the block's data pointer
14855 here, not the actual data. We're still pointing into the
6502dd73
DJ
14856 info_buffer for SYM's objfile; right now we never release
14857 that buffer, but when we do clean up properly this may
14858 need to change. */
0d53c4c4
DJ
14859 baton->size = DW_BLOCK (attr)->size;
14860 baton->data = DW_BLOCK (attr)->data;
14861 }
14862 else
14863 {
14864 dwarf2_invalid_attrib_class_complaint ("location description",
14865 SYMBOL_NATURAL_NAME (sym));
14866 baton->size = 0;
14867 baton->data = NULL;
14868 }
6e70227d 14869
768a979c 14870 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14871 SYMBOL_LOCATION_BATON (sym) = baton;
14872 }
4c2df51b 14873}
6502dd73 14874
9aa1f1e3
TT
14875/* Return the OBJFILE associated with the compilation unit CU. If CU
14876 came from a separate debuginfo file, then the master objfile is
14877 returned. */
ae0d2f24
UW
14878
14879struct objfile *
14880dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14881{
9291a0cd 14882 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14883
14884 /* Return the master objfile, so that we can report and look up the
14885 correct file containing this variable. */
14886 if (objfile->separate_debug_objfile_backlink)
14887 objfile = objfile->separate_debug_objfile_backlink;
14888
14889 return objfile;
14890}
14891
14892/* Return the address size given in the compilation unit header for CU. */
14893
14894CORE_ADDR
14895dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14896{
14897 if (per_cu->cu)
14898 return per_cu->cu->header.addr_size;
14899 else
14900 {
14901 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14902 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14903 struct dwarf2_per_objfile *per_objfile
14904 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14905 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14906 struct comp_unit_head cu_header;
9a619af0 14907
ae0d2f24
UW
14908 memset (&cu_header, 0, sizeof cu_header);
14909 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14910 return cu_header.addr_size;
14911 }
14912}
14913
9eae7c52
TT
14914/* Return the offset size given in the compilation unit header for CU. */
14915
14916int
14917dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14918{
14919 if (per_cu->cu)
14920 return per_cu->cu->header.offset_size;
14921 else
14922 {
14923 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14924 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14925 struct dwarf2_per_objfile *per_objfile
14926 = objfile_data (objfile, dwarf2_objfile_data_key);
14927 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14928 struct comp_unit_head cu_header;
14929
14930 memset (&cu_header, 0, sizeof cu_header);
14931 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14932 return cu_header.offset_size;
14933 }
14934}
14935
9aa1f1e3
TT
14936/* Return the text offset of the CU. The returned offset comes from
14937 this CU's objfile. If this objfile came from a separate debuginfo
14938 file, then the offset may be different from the corresponding
14939 offset in the parent objfile. */
14940
14941CORE_ADDR
14942dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14943{
bb3fa9d0 14944 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14945
14946 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14947}
14948
348e048f
DE
14949/* Locate the .debug_info compilation unit from CU's objfile which contains
14950 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14951
14952static struct dwarf2_per_cu_data *
c764a876 14953dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14954 struct objfile *objfile)
14955{
14956 struct dwarf2_per_cu_data *this_cu;
14957 int low, high;
14958
ae038cb0
DJ
14959 low = 0;
14960 high = dwarf2_per_objfile->n_comp_units - 1;
14961 while (high > low)
14962 {
14963 int mid = low + (high - low) / 2;
9a619af0 14964
ae038cb0
DJ
14965 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14966 high = mid;
14967 else
14968 low = mid + 1;
14969 }
14970 gdb_assert (low == high);
14971 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14972 {
10b3939b 14973 if (low == 0)
8a3fe4f8
AC
14974 error (_("Dwarf Error: could not find partial DIE containing "
14975 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14976 (long) offset, bfd_get_filename (objfile->obfd));
14977
ae038cb0
DJ
14978 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14979 return dwarf2_per_objfile->all_comp_units[low-1];
14980 }
14981 else
14982 {
14983 this_cu = dwarf2_per_objfile->all_comp_units[low];
14984 if (low == dwarf2_per_objfile->n_comp_units - 1
14985 && offset >= this_cu->offset + this_cu->length)
c764a876 14986 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14987 gdb_assert (offset < this_cu->offset + this_cu->length);
14988 return this_cu;
14989 }
14990}
14991
10b3939b
DJ
14992/* Locate the compilation unit from OBJFILE which is located at exactly
14993 OFFSET. Raises an error on failure. */
14994
ae038cb0 14995static struct dwarf2_per_cu_data *
c764a876 14996dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14997{
14998 struct dwarf2_per_cu_data *this_cu;
9a619af0 14999
ae038cb0
DJ
15000 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15001 if (this_cu->offset != offset)
c764a876 15002 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15003 return this_cu;
15004}
15005
9816fde3 15006/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15007
9816fde3
JK
15008static void
15009init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15010{
9816fde3 15011 memset (cu, 0, sizeof (*cu));
93311388
DE
15012 cu->objfile = objfile;
15013 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15014}
15015
15016/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15017
15018static void
15019prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15020{
15021 struct attribute *attr;
15022
15023 /* Set the language we're debugging. */
15024 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15025 if (attr)
15026 set_cu_language (DW_UNSND (attr), cu);
15027 else
9cded63f
TT
15028 {
15029 cu->language = language_minimal;
15030 cu->language_defn = language_def (cu->language);
15031 }
93311388
DE
15032}
15033
ae038cb0
DJ
15034/* Release one cached compilation unit, CU. We unlink it from the tree
15035 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15036 the caller is responsible for that.
15037 NOTE: DATA is a void * because this function is also used as a
15038 cleanup routine. */
ae038cb0
DJ
15039
15040static void
15041free_one_comp_unit (void *data)
15042{
15043 struct dwarf2_cu *cu = data;
15044
15045 if (cu->per_cu != NULL)
15046 cu->per_cu->cu = NULL;
15047 cu->per_cu = NULL;
15048
15049 obstack_free (&cu->comp_unit_obstack, NULL);
15050
15051 xfree (cu);
15052}
15053
72bf9492 15054/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15055 when we're finished with it. We can't free the pointer itself, but be
15056 sure to unlink it from the cache. Also release any associated storage
15057 and perform cache maintenance.
72bf9492
DJ
15058
15059 Only used during partial symbol parsing. */
15060
15061static void
15062free_stack_comp_unit (void *data)
15063{
15064 struct dwarf2_cu *cu = data;
15065
15066 obstack_free (&cu->comp_unit_obstack, NULL);
15067 cu->partial_dies = NULL;
ae038cb0
DJ
15068
15069 if (cu->per_cu != NULL)
15070 {
15071 /* This compilation unit is on the stack in our caller, so we
15072 should not xfree it. Just unlink it. */
15073 cu->per_cu->cu = NULL;
15074 cu->per_cu = NULL;
15075
15076 /* If we had a per-cu pointer, then we may have other compilation
15077 units loaded, so age them now. */
15078 age_cached_comp_units ();
15079 }
15080}
15081
15082/* Free all cached compilation units. */
15083
15084static void
15085free_cached_comp_units (void *data)
15086{
15087 struct dwarf2_per_cu_data *per_cu, **last_chain;
15088
15089 per_cu = dwarf2_per_objfile->read_in_chain;
15090 last_chain = &dwarf2_per_objfile->read_in_chain;
15091 while (per_cu != NULL)
15092 {
15093 struct dwarf2_per_cu_data *next_cu;
15094
15095 next_cu = per_cu->cu->read_in_chain;
15096
15097 free_one_comp_unit (per_cu->cu);
15098 *last_chain = next_cu;
15099
15100 per_cu = next_cu;
15101 }
15102}
15103
15104/* Increase the age counter on each cached compilation unit, and free
15105 any that are too old. */
15106
15107static void
15108age_cached_comp_units (void)
15109{
15110 struct dwarf2_per_cu_data *per_cu, **last_chain;
15111
15112 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15113 per_cu = dwarf2_per_objfile->read_in_chain;
15114 while (per_cu != NULL)
15115 {
15116 per_cu->cu->last_used ++;
15117 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15118 dwarf2_mark (per_cu->cu);
15119 per_cu = per_cu->cu->read_in_chain;
15120 }
15121
15122 per_cu = dwarf2_per_objfile->read_in_chain;
15123 last_chain = &dwarf2_per_objfile->read_in_chain;
15124 while (per_cu != NULL)
15125 {
15126 struct dwarf2_per_cu_data *next_cu;
15127
15128 next_cu = per_cu->cu->read_in_chain;
15129
15130 if (!per_cu->cu->mark)
15131 {
15132 free_one_comp_unit (per_cu->cu);
15133 *last_chain = next_cu;
15134 }
15135 else
15136 last_chain = &per_cu->cu->read_in_chain;
15137
15138 per_cu = next_cu;
15139 }
15140}
15141
15142/* Remove a single compilation unit from the cache. */
15143
15144static void
15145free_one_cached_comp_unit (void *target_cu)
15146{
15147 struct dwarf2_per_cu_data *per_cu, **last_chain;
15148
15149 per_cu = dwarf2_per_objfile->read_in_chain;
15150 last_chain = &dwarf2_per_objfile->read_in_chain;
15151 while (per_cu != NULL)
15152 {
15153 struct dwarf2_per_cu_data *next_cu;
15154
15155 next_cu = per_cu->cu->read_in_chain;
15156
15157 if (per_cu->cu == target_cu)
15158 {
15159 free_one_comp_unit (per_cu->cu);
15160 *last_chain = next_cu;
15161 break;
15162 }
15163 else
15164 last_chain = &per_cu->cu->read_in_chain;
15165
15166 per_cu = next_cu;
15167 }
15168}
15169
fe3e1990
DJ
15170/* Release all extra memory associated with OBJFILE. */
15171
15172void
15173dwarf2_free_objfile (struct objfile *objfile)
15174{
15175 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15176
15177 if (dwarf2_per_objfile == NULL)
15178 return;
15179
15180 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15181 free_cached_comp_units (NULL);
15182
7b9f3c50
DE
15183 if (dwarf2_per_objfile->quick_file_names_table)
15184 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15185
fe3e1990
DJ
15186 /* Everything else should be on the objfile obstack. */
15187}
15188
1c379e20
DJ
15189/* A pair of DIE offset and GDB type pointer. We store these
15190 in a hash table separate from the DIEs, and preserve them
15191 when the DIEs are flushed out of cache. */
15192
15193struct dwarf2_offset_and_type
15194{
15195 unsigned int offset;
15196 struct type *type;
15197};
15198
15199/* Hash function for a dwarf2_offset_and_type. */
15200
15201static hashval_t
15202offset_and_type_hash (const void *item)
15203{
15204 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15205
1c379e20
DJ
15206 return ofs->offset;
15207}
15208
15209/* Equality function for a dwarf2_offset_and_type. */
15210
15211static int
15212offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15213{
15214 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15215 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15216
1c379e20
DJ
15217 return ofs_lhs->offset == ofs_rhs->offset;
15218}
15219
15220/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15221 table if necessary. For convenience, return TYPE.
15222
15223 The DIEs reading must have careful ordering to:
15224 * Not cause infite loops trying to read in DIEs as a prerequisite for
15225 reading current DIE.
15226 * Not trying to dereference contents of still incompletely read in types
15227 while reading in other DIEs.
15228 * Enable referencing still incompletely read in types just by a pointer to
15229 the type without accessing its fields.
15230
15231 Therefore caller should follow these rules:
15232 * Try to fetch any prerequisite types we may need to build this DIE type
15233 before building the type and calling set_die_type.
e71ec853 15234 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15235 possible before fetching more types to complete the current type.
15236 * Make the type as complete as possible before fetching more types. */
1c379e20 15237
f792889a 15238static struct type *
1c379e20
DJ
15239set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15240{
15241 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15242 struct objfile *objfile = cu->objfile;
15243 htab_t *type_hash_ptr;
1c379e20 15244
b4ba55a1
JB
15245 /* For Ada types, make sure that the gnat-specific data is always
15246 initialized (if not already set). There are a few types where
15247 we should not be doing so, because the type-specific area is
15248 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15249 where the type-specific area is used to store the floatformat).
15250 But this is not a problem, because the gnat-specific information
15251 is actually not needed for these types. */
15252 if (need_gnat_info (cu)
15253 && TYPE_CODE (type) != TYPE_CODE_FUNC
15254 && TYPE_CODE (type) != TYPE_CODE_FLT
15255 && !HAVE_GNAT_AUX_INFO (type))
15256 INIT_GNAT_SPECIFIC (type);
15257
673bfd45
DE
15258 if (cu->per_cu->from_debug_types)
15259 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15260 else
15261 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15262
15263 if (*type_hash_ptr == NULL)
f792889a 15264 {
673bfd45
DE
15265 *type_hash_ptr
15266 = htab_create_alloc_ex (127,
f792889a
DJ
15267 offset_and_type_hash,
15268 offset_and_type_eq,
15269 NULL,
673bfd45 15270 &objfile->objfile_obstack,
f792889a
DJ
15271 hashtab_obstack_allocate,
15272 dummy_obstack_deallocate);
f792889a 15273 }
1c379e20
DJ
15274
15275 ofs.offset = die->offset;
15276 ofs.type = type;
15277 slot = (struct dwarf2_offset_and_type **)
673bfd45 15278 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15279 if (*slot)
15280 complaint (&symfile_complaints,
15281 _("A problem internal to GDB: DIE 0x%x has type already set"),
15282 die->offset);
673bfd45 15283 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15284 **slot = ofs;
f792889a 15285 return type;
1c379e20
DJ
15286}
15287
673bfd45
DE
15288/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15289 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15290
15291static struct type *
673bfd45
DE
15292get_die_type_at_offset (unsigned int offset,
15293 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15294{
15295 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15296 htab_t type_hash;
f792889a 15297
673bfd45
DE
15298 if (per_cu->from_debug_types)
15299 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15300 else
15301 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15302 if (type_hash == NULL)
15303 return NULL;
1c379e20 15304
673bfd45 15305 ofs.offset = offset;
1c379e20
DJ
15306 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15307 if (slot)
15308 return slot->type;
15309 else
15310 return NULL;
15311}
15312
673bfd45
DE
15313/* Look up the type for DIE in the appropriate type_hash table,
15314 or return NULL if DIE does not have a saved type. */
15315
15316static struct type *
15317get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15318{
15319 return get_die_type_at_offset (die->offset, cu->per_cu);
15320}
15321
10b3939b
DJ
15322/* Add a dependence relationship from CU to REF_PER_CU. */
15323
15324static void
15325dwarf2_add_dependence (struct dwarf2_cu *cu,
15326 struct dwarf2_per_cu_data *ref_per_cu)
15327{
15328 void **slot;
15329
15330 if (cu->dependencies == NULL)
15331 cu->dependencies
15332 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15333 NULL, &cu->comp_unit_obstack,
15334 hashtab_obstack_allocate,
15335 dummy_obstack_deallocate);
15336
15337 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15338 if (*slot == NULL)
15339 *slot = ref_per_cu;
15340}
1c379e20 15341
f504f079
DE
15342/* Subroutine of dwarf2_mark to pass to htab_traverse.
15343 Set the mark field in every compilation unit in the
ae038cb0
DJ
15344 cache that we must keep because we are keeping CU. */
15345
10b3939b
DJ
15346static int
15347dwarf2_mark_helper (void **slot, void *data)
15348{
15349 struct dwarf2_per_cu_data *per_cu;
15350
15351 per_cu = (struct dwarf2_per_cu_data *) *slot;
15352 if (per_cu->cu->mark)
15353 return 1;
15354 per_cu->cu->mark = 1;
15355
15356 if (per_cu->cu->dependencies != NULL)
15357 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15358
15359 return 1;
15360}
15361
f504f079
DE
15362/* Set the mark field in CU and in every other compilation unit in the
15363 cache that we must keep because we are keeping CU. */
15364
ae038cb0
DJ
15365static void
15366dwarf2_mark (struct dwarf2_cu *cu)
15367{
15368 if (cu->mark)
15369 return;
15370 cu->mark = 1;
10b3939b
DJ
15371 if (cu->dependencies != NULL)
15372 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15373}
15374
15375static void
15376dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15377{
15378 while (per_cu)
15379 {
15380 per_cu->cu->mark = 0;
15381 per_cu = per_cu->cu->read_in_chain;
15382 }
72bf9492
DJ
15383}
15384
72bf9492
DJ
15385/* Trivial hash function for partial_die_info: the hash value of a DIE
15386 is its offset in .debug_info for this objfile. */
15387
15388static hashval_t
15389partial_die_hash (const void *item)
15390{
15391 const struct partial_die_info *part_die = item;
9a619af0 15392
72bf9492
DJ
15393 return part_die->offset;
15394}
15395
15396/* Trivial comparison function for partial_die_info structures: two DIEs
15397 are equal if they have the same offset. */
15398
15399static int
15400partial_die_eq (const void *item_lhs, const void *item_rhs)
15401{
15402 const struct partial_die_info *part_die_lhs = item_lhs;
15403 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15404
72bf9492
DJ
15405 return part_die_lhs->offset == part_die_rhs->offset;
15406}
15407
ae038cb0
DJ
15408static struct cmd_list_element *set_dwarf2_cmdlist;
15409static struct cmd_list_element *show_dwarf2_cmdlist;
15410
15411static void
15412set_dwarf2_cmd (char *args, int from_tty)
15413{
15414 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15415}
15416
15417static void
15418show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15419{
ae038cb0
DJ
15420 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15421}
15422
dce234bc
PP
15423/* If section described by INFO was mmapped, munmap it now. */
15424
15425static void
15426munmap_section_buffer (struct dwarf2_section_info *info)
15427{
b315ab21 15428 if (info->map_addr != NULL)
dce234bc
PP
15429 {
15430#ifdef HAVE_MMAP
b315ab21 15431 int res;
9a619af0 15432
b315ab21
TG
15433 res = munmap (info->map_addr, info->map_len);
15434 gdb_assert (res == 0);
dce234bc
PP
15435#else
15436 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15437 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15438#endif
15439 }
15440}
15441
15442/* munmap debug sections for OBJFILE, if necessary. */
15443
15444static void
c1bd65d0 15445dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15446{
15447 struct dwarf2_per_objfile *data = d;
9a619af0 15448
16be1145
DE
15449 /* This is sorted according to the order they're defined in to make it easier
15450 to keep in sync. */
dce234bc
PP
15451 munmap_section_buffer (&data->info);
15452 munmap_section_buffer (&data->abbrev);
15453 munmap_section_buffer (&data->line);
16be1145 15454 munmap_section_buffer (&data->loc);
dce234bc 15455 munmap_section_buffer (&data->macinfo);
16be1145 15456 munmap_section_buffer (&data->str);
dce234bc 15457 munmap_section_buffer (&data->ranges);
16be1145 15458 munmap_section_buffer (&data->types);
dce234bc
PP
15459 munmap_section_buffer (&data->frame);
15460 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15461 munmap_section_buffer (&data->gdb_index);
15462}
15463
15464\f
ae2de4f8 15465/* The "save gdb-index" command. */
9291a0cd
TT
15466
15467/* The contents of the hash table we create when building the string
15468 table. */
15469struct strtab_entry
15470{
15471 offset_type offset;
15472 const char *str;
15473};
15474
559a7a62
JK
15475/* Hash function for a strtab_entry.
15476
15477 Function is used only during write_hash_table so no index format backward
15478 compatibility is needed. */
b89be57b 15479
9291a0cd
TT
15480static hashval_t
15481hash_strtab_entry (const void *e)
15482{
15483 const struct strtab_entry *entry = e;
559a7a62 15484 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15485}
15486
15487/* Equality function for a strtab_entry. */
b89be57b 15488
9291a0cd
TT
15489static int
15490eq_strtab_entry (const void *a, const void *b)
15491{
15492 const struct strtab_entry *ea = a;
15493 const struct strtab_entry *eb = b;
15494 return !strcmp (ea->str, eb->str);
15495}
15496
15497/* Create a strtab_entry hash table. */
b89be57b 15498
9291a0cd
TT
15499static htab_t
15500create_strtab (void)
15501{
15502 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15503 xfree, xcalloc, xfree);
15504}
15505
15506/* Add a string to the constant pool. Return the string's offset in
15507 host order. */
b89be57b 15508
9291a0cd
TT
15509static offset_type
15510add_string (htab_t table, struct obstack *cpool, const char *str)
15511{
15512 void **slot;
15513 struct strtab_entry entry;
15514 struct strtab_entry *result;
15515
15516 entry.str = str;
15517 slot = htab_find_slot (table, &entry, INSERT);
15518 if (*slot)
15519 result = *slot;
15520 else
15521 {
15522 result = XNEW (struct strtab_entry);
15523 result->offset = obstack_object_size (cpool);
15524 result->str = str;
15525 obstack_grow_str0 (cpool, str);
15526 *slot = result;
15527 }
15528 return result->offset;
15529}
15530
15531/* An entry in the symbol table. */
15532struct symtab_index_entry
15533{
15534 /* The name of the symbol. */
15535 const char *name;
15536 /* The offset of the name in the constant pool. */
15537 offset_type index_offset;
15538 /* A sorted vector of the indices of all the CUs that hold an object
15539 of this name. */
15540 VEC (offset_type) *cu_indices;
15541};
15542
15543/* The symbol table. This is a power-of-2-sized hash table. */
15544struct mapped_symtab
15545{
15546 offset_type n_elements;
15547 offset_type size;
15548 struct symtab_index_entry **data;
15549};
15550
15551/* Hash function for a symtab_index_entry. */
b89be57b 15552
9291a0cd
TT
15553static hashval_t
15554hash_symtab_entry (const void *e)
15555{
15556 const struct symtab_index_entry *entry = e;
15557 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15558 sizeof (offset_type) * VEC_length (offset_type,
15559 entry->cu_indices),
15560 0);
15561}
15562
15563/* Equality function for a symtab_index_entry. */
b89be57b 15564
9291a0cd
TT
15565static int
15566eq_symtab_entry (const void *a, const void *b)
15567{
15568 const struct symtab_index_entry *ea = a;
15569 const struct symtab_index_entry *eb = b;
15570 int len = VEC_length (offset_type, ea->cu_indices);
15571 if (len != VEC_length (offset_type, eb->cu_indices))
15572 return 0;
15573 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15574 VEC_address (offset_type, eb->cu_indices),
15575 sizeof (offset_type) * len);
15576}
15577
15578/* Destroy a symtab_index_entry. */
b89be57b 15579
9291a0cd
TT
15580static void
15581delete_symtab_entry (void *p)
15582{
15583 struct symtab_index_entry *entry = p;
15584 VEC_free (offset_type, entry->cu_indices);
15585 xfree (entry);
15586}
15587
15588/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15589
9291a0cd 15590static htab_t
3876f04e 15591create_symbol_hash_table (void)
9291a0cd
TT
15592{
15593 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15594 delete_symtab_entry, xcalloc, xfree);
15595}
15596
15597/* Create a new mapped symtab object. */
b89be57b 15598
9291a0cd
TT
15599static struct mapped_symtab *
15600create_mapped_symtab (void)
15601{
15602 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15603 symtab->n_elements = 0;
15604 symtab->size = 1024;
15605 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15606 return symtab;
15607}
15608
15609/* Destroy a mapped_symtab. */
b89be57b 15610
9291a0cd
TT
15611static void
15612cleanup_mapped_symtab (void *p)
15613{
15614 struct mapped_symtab *symtab = p;
15615 /* The contents of the array are freed when the other hash table is
15616 destroyed. */
15617 xfree (symtab->data);
15618 xfree (symtab);
15619}
15620
15621/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15622 the slot.
15623
15624 Function is used only during write_hash_table so no index format backward
15625 compatibility is needed. */
b89be57b 15626
9291a0cd
TT
15627static struct symtab_index_entry **
15628find_slot (struct mapped_symtab *symtab, const char *name)
15629{
559a7a62 15630 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15631
15632 index = hash & (symtab->size - 1);
15633 step = ((hash * 17) & (symtab->size - 1)) | 1;
15634
15635 for (;;)
15636 {
15637 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15638 return &symtab->data[index];
15639 index = (index + step) & (symtab->size - 1);
15640 }
15641}
15642
15643/* Expand SYMTAB's hash table. */
b89be57b 15644
9291a0cd
TT
15645static void
15646hash_expand (struct mapped_symtab *symtab)
15647{
15648 offset_type old_size = symtab->size;
15649 offset_type i;
15650 struct symtab_index_entry **old_entries = symtab->data;
15651
15652 symtab->size *= 2;
15653 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15654
15655 for (i = 0; i < old_size; ++i)
15656 {
15657 if (old_entries[i])
15658 {
15659 struct symtab_index_entry **slot = find_slot (symtab,
15660 old_entries[i]->name);
15661 *slot = old_entries[i];
15662 }
15663 }
15664
15665 xfree (old_entries);
15666}
15667
15668/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15669 is the index of the CU in which the symbol appears. */
b89be57b 15670
9291a0cd
TT
15671static void
15672add_index_entry (struct mapped_symtab *symtab, const char *name,
15673 offset_type cu_index)
15674{
15675 struct symtab_index_entry **slot;
15676
15677 ++symtab->n_elements;
15678 if (4 * symtab->n_elements / 3 >= symtab->size)
15679 hash_expand (symtab);
15680
15681 slot = find_slot (symtab, name);
15682 if (!*slot)
15683 {
15684 *slot = XNEW (struct symtab_index_entry);
15685 (*slot)->name = name;
15686 (*slot)->cu_indices = NULL;
15687 }
15688 /* Don't push an index twice. Due to how we add entries we only
15689 have to check the last one. */
15690 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15691 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15692 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15693}
15694
15695/* Add a vector of indices to the constant pool. */
b89be57b 15696
9291a0cd 15697static offset_type
3876f04e 15698add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15699 struct symtab_index_entry *entry)
15700{
15701 void **slot;
15702
3876f04e 15703 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15704 if (!*slot)
15705 {
15706 offset_type len = VEC_length (offset_type, entry->cu_indices);
15707 offset_type val = MAYBE_SWAP (len);
15708 offset_type iter;
15709 int i;
15710
15711 *slot = entry;
15712 entry->index_offset = obstack_object_size (cpool);
15713
15714 obstack_grow (cpool, &val, sizeof (val));
15715 for (i = 0;
15716 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15717 ++i)
15718 {
15719 val = MAYBE_SWAP (iter);
15720 obstack_grow (cpool, &val, sizeof (val));
15721 }
15722 }
15723 else
15724 {
15725 struct symtab_index_entry *old_entry = *slot;
15726 entry->index_offset = old_entry->index_offset;
15727 entry = old_entry;
15728 }
15729 return entry->index_offset;
15730}
15731
15732/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15733 constant pool entries going into the obstack CPOOL. */
b89be57b 15734
9291a0cd
TT
15735static void
15736write_hash_table (struct mapped_symtab *symtab,
15737 struct obstack *output, struct obstack *cpool)
15738{
15739 offset_type i;
3876f04e 15740 htab_t symbol_hash_table;
9291a0cd
TT
15741 htab_t str_table;
15742
3876f04e 15743 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15744 str_table = create_strtab ();
3876f04e 15745
9291a0cd
TT
15746 /* We add all the index vectors to the constant pool first, to
15747 ensure alignment is ok. */
15748 for (i = 0; i < symtab->size; ++i)
15749 {
15750 if (symtab->data[i])
3876f04e 15751 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15752 }
15753
15754 /* Now write out the hash table. */
15755 for (i = 0; i < symtab->size; ++i)
15756 {
15757 offset_type str_off, vec_off;
15758
15759 if (symtab->data[i])
15760 {
15761 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15762 vec_off = symtab->data[i]->index_offset;
15763 }
15764 else
15765 {
15766 /* While 0 is a valid constant pool index, it is not valid
15767 to have 0 for both offsets. */
15768 str_off = 0;
15769 vec_off = 0;
15770 }
15771
15772 str_off = MAYBE_SWAP (str_off);
15773 vec_off = MAYBE_SWAP (vec_off);
15774
15775 obstack_grow (output, &str_off, sizeof (str_off));
15776 obstack_grow (output, &vec_off, sizeof (vec_off));
15777 }
15778
15779 htab_delete (str_table);
3876f04e 15780 htab_delete (symbol_hash_table);
9291a0cd
TT
15781}
15782
0a5429f6
DE
15783/* Struct to map psymtab to CU index in the index file. */
15784struct psymtab_cu_index_map
15785{
15786 struct partial_symtab *psymtab;
15787 unsigned int cu_index;
15788};
15789
15790static hashval_t
15791hash_psymtab_cu_index (const void *item)
15792{
15793 const struct psymtab_cu_index_map *map = item;
15794
15795 return htab_hash_pointer (map->psymtab);
15796}
15797
15798static int
15799eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15800{
15801 const struct psymtab_cu_index_map *lhs = item_lhs;
15802 const struct psymtab_cu_index_map *rhs = item_rhs;
15803
15804 return lhs->psymtab == rhs->psymtab;
15805}
15806
15807/* Helper struct for building the address table. */
15808struct addrmap_index_data
15809{
15810 struct objfile *objfile;
15811 struct obstack *addr_obstack;
15812 htab_t cu_index_htab;
15813
15814 /* Non-zero if the previous_* fields are valid.
15815 We can't write an entry until we see the next entry (since it is only then
15816 that we know the end of the entry). */
15817 int previous_valid;
15818 /* Index of the CU in the table of all CUs in the index file. */
15819 unsigned int previous_cu_index;
0963b4bd 15820 /* Start address of the CU. */
0a5429f6
DE
15821 CORE_ADDR previous_cu_start;
15822};
15823
15824/* Write an address entry to OBSTACK. */
b89be57b 15825
9291a0cd 15826static void
0a5429f6
DE
15827add_address_entry (struct objfile *objfile, struct obstack *obstack,
15828 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15829{
0a5429f6 15830 offset_type cu_index_to_write;
9291a0cd
TT
15831 char addr[8];
15832 CORE_ADDR baseaddr;
15833
15834 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15835
0a5429f6
DE
15836 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15837 obstack_grow (obstack, addr, 8);
15838 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15839 obstack_grow (obstack, addr, 8);
15840 cu_index_to_write = MAYBE_SWAP (cu_index);
15841 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15842}
15843
15844/* Worker function for traversing an addrmap to build the address table. */
15845
15846static int
15847add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15848{
15849 struct addrmap_index_data *data = datap;
15850 struct partial_symtab *pst = obj;
15851 offset_type cu_index;
15852 void **slot;
15853
15854 if (data->previous_valid)
15855 add_address_entry (data->objfile, data->addr_obstack,
15856 data->previous_cu_start, start_addr,
15857 data->previous_cu_index);
15858
15859 data->previous_cu_start = start_addr;
15860 if (pst != NULL)
15861 {
15862 struct psymtab_cu_index_map find_map, *map;
15863 find_map.psymtab = pst;
15864 map = htab_find (data->cu_index_htab, &find_map);
15865 gdb_assert (map != NULL);
15866 data->previous_cu_index = map->cu_index;
15867 data->previous_valid = 1;
15868 }
15869 else
15870 data->previous_valid = 0;
15871
15872 return 0;
15873}
15874
15875/* Write OBJFILE's address map to OBSTACK.
15876 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15877 in the index file. */
15878
15879static void
15880write_address_map (struct objfile *objfile, struct obstack *obstack,
15881 htab_t cu_index_htab)
15882{
15883 struct addrmap_index_data addrmap_index_data;
15884
15885 /* When writing the address table, we have to cope with the fact that
15886 the addrmap iterator only provides the start of a region; we have to
15887 wait until the next invocation to get the start of the next region. */
15888
15889 addrmap_index_data.objfile = objfile;
15890 addrmap_index_data.addr_obstack = obstack;
15891 addrmap_index_data.cu_index_htab = cu_index_htab;
15892 addrmap_index_data.previous_valid = 0;
15893
15894 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15895 &addrmap_index_data);
15896
15897 /* It's highly unlikely the last entry (end address = 0xff...ff)
15898 is valid, but we should still handle it.
15899 The end address is recorded as the start of the next region, but that
15900 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15901 anyway. */
15902 if (addrmap_index_data.previous_valid)
15903 add_address_entry (objfile, obstack,
15904 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15905 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15906}
15907
15908/* Add a list of partial symbols to SYMTAB. */
b89be57b 15909
9291a0cd
TT
15910static void
15911write_psymbols (struct mapped_symtab *symtab,
987d643c 15912 htab_t psyms_seen,
9291a0cd
TT
15913 struct partial_symbol **psymp,
15914 int count,
987d643c
TT
15915 offset_type cu_index,
15916 int is_static)
9291a0cd
TT
15917{
15918 for (; count-- > 0; ++psymp)
15919 {
987d643c
TT
15920 void **slot, *lookup;
15921
9291a0cd
TT
15922 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15923 error (_("Ada is not currently supported by the index"));
987d643c
TT
15924
15925 /* We only want to add a given psymbol once. However, we also
15926 want to account for whether it is global or static. So, we
15927 may add it twice, using slightly different values. */
15928 if (is_static)
15929 {
15930 uintptr_t val = 1 | (uintptr_t) *psymp;
15931
15932 lookup = (void *) val;
15933 }
15934 else
15935 lookup = *psymp;
15936
15937 /* Only add a given psymbol once. */
15938 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15939 if (!*slot)
15940 {
15941 *slot = lookup;
15942 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15943 }
9291a0cd
TT
15944 }
15945}
15946
15947/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15948 exception if there is an error. */
b89be57b 15949
9291a0cd
TT
15950static void
15951write_obstack (FILE *file, struct obstack *obstack)
15952{
15953 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15954 file)
15955 != obstack_object_size (obstack))
15956 error (_("couldn't data write to file"));
15957}
15958
15959/* Unlink a file if the argument is not NULL. */
b89be57b 15960
9291a0cd
TT
15961static void
15962unlink_if_set (void *p)
15963{
15964 char **filename = p;
15965 if (*filename)
15966 unlink (*filename);
15967}
15968
1fd400ff
TT
15969/* A helper struct used when iterating over debug_types. */
15970struct signatured_type_index_data
15971{
15972 struct objfile *objfile;
15973 struct mapped_symtab *symtab;
15974 struct obstack *types_list;
987d643c 15975 htab_t psyms_seen;
1fd400ff
TT
15976 int cu_index;
15977};
15978
15979/* A helper function that writes a single signatured_type to an
15980 obstack. */
b89be57b 15981
1fd400ff
TT
15982static int
15983write_one_signatured_type (void **slot, void *d)
15984{
15985 struct signatured_type_index_data *info = d;
15986 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15987 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15988 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15989 gdb_byte val[8];
15990
15991 write_psymbols (info->symtab,
987d643c 15992 info->psyms_seen,
3e43a32a
MS
15993 info->objfile->global_psymbols.list
15994 + psymtab->globals_offset,
987d643c
TT
15995 psymtab->n_global_syms, info->cu_index,
15996 0);
1fd400ff 15997 write_psymbols (info->symtab,
987d643c 15998 info->psyms_seen,
3e43a32a
MS
15999 info->objfile->static_psymbols.list
16000 + psymtab->statics_offset,
987d643c
TT
16001 psymtab->n_static_syms, info->cu_index,
16002 1);
1fd400ff 16003
b3c8eb43 16004 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16005 obstack_grow (info->types_list, val, 8);
16006 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16007 obstack_grow (info->types_list, val, 8);
16008 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16009 obstack_grow (info->types_list, val, 8);
16010
16011 ++info->cu_index;
16012
16013 return 1;
16014}
16015
987d643c
TT
16016/* A cleanup function for an htab_t. */
16017
16018static void
16019cleanup_htab (void *arg)
16020{
16021 htab_delete (arg);
16022}
16023
9291a0cd 16024/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16025
9291a0cd
TT
16026static void
16027write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16028{
16029 struct cleanup *cleanup;
16030 char *filename, *cleanup_filename;
1fd400ff
TT
16031 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16032 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16033 int i;
16034 FILE *out_file;
16035 struct mapped_symtab *symtab;
16036 offset_type val, size_of_contents, total_len;
16037 struct stat st;
16038 char buf[8];
987d643c 16039 htab_t psyms_seen;
0a5429f6
DE
16040 htab_t cu_index_htab;
16041 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16042
b4f2f049 16043 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16044 return;
b4f2f049 16045
9291a0cd
TT
16046 if (dwarf2_per_objfile->using_index)
16047 error (_("Cannot use an index to create the index"));
16048
16049 if (stat (objfile->name, &st) < 0)
7e17e088 16050 perror_with_name (objfile->name);
9291a0cd
TT
16051
16052 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16053 INDEX_SUFFIX, (char *) NULL);
16054 cleanup = make_cleanup (xfree, filename);
16055
16056 out_file = fopen (filename, "wb");
16057 if (!out_file)
16058 error (_("Can't open `%s' for writing"), filename);
16059
16060 cleanup_filename = filename;
16061 make_cleanup (unlink_if_set, &cleanup_filename);
16062
16063 symtab = create_mapped_symtab ();
16064 make_cleanup (cleanup_mapped_symtab, symtab);
16065
16066 obstack_init (&addr_obstack);
16067 make_cleanup_obstack_free (&addr_obstack);
16068
16069 obstack_init (&cu_list);
16070 make_cleanup_obstack_free (&cu_list);
16071
1fd400ff
TT
16072 obstack_init (&types_cu_list);
16073 make_cleanup_obstack_free (&types_cu_list);
16074
987d643c
TT
16075 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16076 NULL, xcalloc, xfree);
16077 make_cleanup (cleanup_htab, psyms_seen);
16078
0a5429f6
DE
16079 /* While we're scanning CU's create a table that maps a psymtab pointer
16080 (which is what addrmap records) to its index (which is what is recorded
16081 in the index file). This will later be needed to write the address
16082 table. */
16083 cu_index_htab = htab_create_alloc (100,
16084 hash_psymtab_cu_index,
16085 eq_psymtab_cu_index,
16086 NULL, xcalloc, xfree);
16087 make_cleanup (cleanup_htab, cu_index_htab);
16088 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16089 xmalloc (sizeof (struct psymtab_cu_index_map)
16090 * dwarf2_per_objfile->n_comp_units);
16091 make_cleanup (xfree, psymtab_cu_index_map);
16092
16093 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16094 work here. Also, the debug_types entries do not appear in
16095 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16096 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16097 {
3e43a32a
MS
16098 struct dwarf2_per_cu_data *per_cu
16099 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16100 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16101 gdb_byte val[8];
0a5429f6
DE
16102 struct psymtab_cu_index_map *map;
16103 void **slot;
9291a0cd
TT
16104
16105 write_psymbols (symtab,
987d643c 16106 psyms_seen,
9291a0cd 16107 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16108 psymtab->n_global_syms, i,
16109 0);
9291a0cd 16110 write_psymbols (symtab,
987d643c 16111 psyms_seen,
9291a0cd 16112 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16113 psymtab->n_static_syms, i,
16114 1);
9291a0cd 16115
0a5429f6
DE
16116 map = &psymtab_cu_index_map[i];
16117 map->psymtab = psymtab;
16118 map->cu_index = i;
16119 slot = htab_find_slot (cu_index_htab, map, INSERT);
16120 gdb_assert (slot != NULL);
16121 gdb_assert (*slot == NULL);
16122 *slot = map;
9291a0cd 16123
e254ef6a 16124 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16125 obstack_grow (&cu_list, val, 8);
e254ef6a 16126 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16127 obstack_grow (&cu_list, val, 8);
16128 }
16129
0a5429f6
DE
16130 /* Dump the address map. */
16131 write_address_map (objfile, &addr_obstack, cu_index_htab);
16132
1fd400ff
TT
16133 /* Write out the .debug_type entries, if any. */
16134 if (dwarf2_per_objfile->signatured_types)
16135 {
16136 struct signatured_type_index_data sig_data;
16137
16138 sig_data.objfile = objfile;
16139 sig_data.symtab = symtab;
16140 sig_data.types_list = &types_cu_list;
987d643c 16141 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16142 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16143 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16144 write_one_signatured_type, &sig_data);
16145 }
16146
9291a0cd
TT
16147 obstack_init (&constant_pool);
16148 make_cleanup_obstack_free (&constant_pool);
16149 obstack_init (&symtab_obstack);
16150 make_cleanup_obstack_free (&symtab_obstack);
16151 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16152
16153 obstack_init (&contents);
16154 make_cleanup_obstack_free (&contents);
1fd400ff 16155 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16156 total_len = size_of_contents;
16157
16158 /* The version number. */
559a7a62 16159 val = MAYBE_SWAP (5);
9291a0cd
TT
16160 obstack_grow (&contents, &val, sizeof (val));
16161
16162 /* The offset of the CU list from the start of the file. */
16163 val = MAYBE_SWAP (total_len);
16164 obstack_grow (&contents, &val, sizeof (val));
16165 total_len += obstack_object_size (&cu_list);
16166
1fd400ff
TT
16167 /* The offset of the types CU list from the start of the file. */
16168 val = MAYBE_SWAP (total_len);
16169 obstack_grow (&contents, &val, sizeof (val));
16170 total_len += obstack_object_size (&types_cu_list);
16171
9291a0cd
TT
16172 /* The offset of the address table from the start of the file. */
16173 val = MAYBE_SWAP (total_len);
16174 obstack_grow (&contents, &val, sizeof (val));
16175 total_len += obstack_object_size (&addr_obstack);
16176
16177 /* The offset of the symbol table from the start of the file. */
16178 val = MAYBE_SWAP (total_len);
16179 obstack_grow (&contents, &val, sizeof (val));
16180 total_len += obstack_object_size (&symtab_obstack);
16181
16182 /* The offset of the constant pool from the start of the file. */
16183 val = MAYBE_SWAP (total_len);
16184 obstack_grow (&contents, &val, sizeof (val));
16185 total_len += obstack_object_size (&constant_pool);
16186
16187 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16188
16189 write_obstack (out_file, &contents);
16190 write_obstack (out_file, &cu_list);
1fd400ff 16191 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16192 write_obstack (out_file, &addr_obstack);
16193 write_obstack (out_file, &symtab_obstack);
16194 write_obstack (out_file, &constant_pool);
16195
16196 fclose (out_file);
16197
16198 /* We want to keep the file, so we set cleanup_filename to NULL
16199 here. See unlink_if_set. */
16200 cleanup_filename = NULL;
16201
16202 do_cleanups (cleanup);
16203}
16204
90476074
TT
16205/* Implementation of the `save gdb-index' command.
16206
16207 Note that the file format used by this command is documented in the
16208 GDB manual. Any changes here must be documented there. */
11570e71 16209
9291a0cd
TT
16210static void
16211save_gdb_index_command (char *arg, int from_tty)
16212{
16213 struct objfile *objfile;
16214
16215 if (!arg || !*arg)
96d19272 16216 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16217
16218 ALL_OBJFILES (objfile)
16219 {
16220 struct stat st;
16221
16222 /* If the objfile does not correspond to an actual file, skip it. */
16223 if (stat (objfile->name, &st) < 0)
16224 continue;
16225
16226 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16227 if (dwarf2_per_objfile)
16228 {
16229 volatile struct gdb_exception except;
16230
16231 TRY_CATCH (except, RETURN_MASK_ERROR)
16232 {
16233 write_psymtabs_to_index (objfile, arg);
16234 }
16235 if (except.reason < 0)
16236 exception_fprintf (gdb_stderr, except,
16237 _("Error while writing index for `%s': "),
16238 objfile->name);
16239 }
16240 }
dce234bc
PP
16241}
16242
9291a0cd
TT
16243\f
16244
9eae7c52
TT
16245int dwarf2_always_disassemble;
16246
16247static void
16248show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16249 struct cmd_list_element *c, const char *value)
16250{
3e43a32a
MS
16251 fprintf_filtered (file,
16252 _("Whether to always disassemble "
16253 "DWARF expressions is %s.\n"),
9eae7c52
TT
16254 value);
16255}
16256
6502dd73
DJ
16257void _initialize_dwarf2_read (void);
16258
16259void
16260_initialize_dwarf2_read (void)
16261{
96d19272
JK
16262 struct cmd_list_element *c;
16263
dce234bc 16264 dwarf2_objfile_data_key
c1bd65d0 16265 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16266
1bedd215
AC
16267 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16268Set DWARF 2 specific variables.\n\
16269Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16270 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16271 0/*allow-unknown*/, &maintenance_set_cmdlist);
16272
1bedd215
AC
16273 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16274Show DWARF 2 specific variables\n\
16275Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16276 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16277 0/*allow-unknown*/, &maintenance_show_cmdlist);
16278
16279 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16280 &dwarf2_max_cache_age, _("\
16281Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16282Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16283A higher limit means that cached compilation units will be stored\n\
16284in memory longer, and more total memory will be used. Zero disables\n\
16285caching, which can slow down startup."),
2c5b56ce 16286 NULL,
920d2a44 16287 show_dwarf2_max_cache_age,
2c5b56ce 16288 &set_dwarf2_cmdlist,
ae038cb0 16289 &show_dwarf2_cmdlist);
d97bc12b 16290
9eae7c52
TT
16291 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16292 &dwarf2_always_disassemble, _("\
16293Set whether `info address' always disassembles DWARF expressions."), _("\
16294Show whether `info address' always disassembles DWARF expressions."), _("\
16295When enabled, DWARF expressions are always printed in an assembly-like\n\
16296syntax. When disabled, expressions will be printed in a more\n\
16297conversational style, when possible."),
16298 NULL,
16299 show_dwarf2_always_disassemble,
16300 &set_dwarf2_cmdlist,
16301 &show_dwarf2_cmdlist);
16302
d97bc12b
DE
16303 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16304Set debugging of the dwarf2 DIE reader."), _("\
16305Show debugging of the dwarf2 DIE reader."), _("\
16306When enabled (non-zero), DIEs are dumped after they are read in.\n\
16307The value is the maximum depth to print."),
16308 NULL,
16309 NULL,
16310 &setdebuglist, &showdebuglist);
9291a0cd 16311
96d19272 16312 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16313 _("\
fc1a9d6e 16314Save a gdb-index file.\n\
11570e71 16315Usage: save gdb-index DIRECTORY"),
96d19272
JK
16316 &save_cmdlist);
16317 set_cmd_completer (c, filename_completer);
6502dd73 16318}
This page took 3.226087 seconds and 4 git commands to generate.