]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
2010-11-24 Edjunior Machado <emachado@br.ibm.com>
[thirdparty/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,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd
TT
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
98bfdba5
PA
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
673bfd45
DE
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
6502dd73
DJ
226};
227
228static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
229
230/* names of the debugging sections */
231
233a11ab
CS
232/* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235#define INFO_SECTION "debug_info"
236#define ABBREV_SECTION "debug_abbrev"
237#define LINE_SECTION "debug_line"
233a11ab
CS
238#define LOC_SECTION "debug_loc"
239#define MACINFO_SECTION "debug_macinfo"
240#define STR_SECTION "debug_str"
241#define RANGES_SECTION "debug_ranges"
348e048f 242#define TYPES_SECTION "debug_types"
233a11ab
CS
243#define FRAME_SECTION "debug_frame"
244#define EH_FRAME_SECTION "eh_frame"
9291a0cd 245#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
246
247/* local data types */
248
57349743
JB
249/* We hold several abbreviation tables in memory at the same time. */
250#ifndef ABBREV_HASH_SIZE
251#define ABBREV_HASH_SIZE 121
252#endif
253
107d2387
AC
254/* The data in a compilation unit header, after target2host
255 translation, looks like this. */
c906108c 256struct comp_unit_head
a738430d 257{
c764a876 258 unsigned int length;
a738430d 259 short version;
a738430d
MK
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
9cbfa09e 262 unsigned int abbrev_offset;
57349743 263
a738430d
MK
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
57349743 266
a738430d
MK
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
57349743 269
a738430d
MK
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
57349743 273
d00adf39
DE
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
a738430d 277};
c906108c 278
3da10d80
KS
279/* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281struct delayed_method_info
282{
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297};
298
299typedef struct delayed_method_info delayed_method_info;
300DEF_VEC_O (delayed_method_info);
301
e7c27a73
DJ
302/* Internal state when decoding a particular compilation unit. */
303struct dwarf2_cu
304{
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
d00adf39 308 /* The header of the compilation unit. */
e7c27a73 309 struct comp_unit_head header;
e142c38c 310
d00adf39
DE
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
e142c38c
DJ
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
b0f35d58
DL
323 const char *producer;
324
e142c38c
DJ
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
f3dd6933
DJ
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
72bf9492
DJ
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
10b3939b 361 /* A hash table of die offsets for following references. */
51545339 362 htab_t die_hash;
10b3939b
DJ
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
cb1df416
DJ
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
3da10d80
KS
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
ae038cb0
DJ
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
72bf9492
DJ
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
e7c27a73
DJ
391};
392
9291a0cd
TT
393/* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396struct dwarf2_per_cu_quick_data
397{
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417};
418
10b3939b
DJ
419/* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
ae038cb0
DJ
425struct dwarf2_per_cu_data
426{
348e048f 427 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 428 bytes should suffice to store the length of any compilation unit
45452591
DE
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
c764a876 432 unsigned int offset;
348e048f 433 unsigned int length : 29;
ae038cb0
DJ
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
c764a876 437 unsigned int queued : 1;
ae038cb0 438
5afb4e99
DJ
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
348e048f
DE
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
17ea53c3
JK
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
ae038cb0 451 struct dwarf2_cu *cu;
1c379e20 452
9291a0cd
TT
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
ae038cb0
DJ
468};
469
348e048f
DE
470/* Entry in the signatured_types hash table. */
471
472struct signatured_type
473{
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484};
485
93311388
DE
486/* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492struct die_reader_specs
493{
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503};
504
debd256d
JB
505/* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508struct line_header
509{
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
2dc7f7b3 514 unsigned char maximum_ops_per_instruction;
debd256d
JB
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
c906108c 538 {
debd256d
JB
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
aaa75496 543 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 544 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
545 } *file_names;
546
547 /* The start and end of the statement program following this
6502dd73 548 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 549 gdb_byte *statement_program_start, *statement_program_end;
debd256d 550};
c906108c
SS
551
552/* When we construct a partial symbol table entry we only
553 need this much information. */
554struct partial_die_info
555 {
72bf9492 556 /* Offset of this DIE. */
c906108c 557 unsigned int offset;
72bf9492
DJ
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
72bf9492
DJ
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
fa4028e9
JB
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
98bfdba5
PA
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
abc72ce4
DE
580 /* Flag set if fixup_partial_die has been called on this die. */
581 unsigned int fixup_called : 1;
582
72bf9492 583 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 584 sometimes a default name for unnamed DIEs. */
c906108c 585 char *name;
72bf9492 586
abc72ce4
DE
587 /* The linkage name, if present. */
588 const char *linkage_name;
589
72bf9492
DJ
590 /* The scope to prepend to our children. This is generally
591 allocated on the comp_unit_obstack, so will disappear
592 when this compilation unit leaves the cache. */
593 char *scope;
594
595 /* The location description associated with this DIE, if any. */
596 struct dwarf_block *locdesc;
597
598 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
599 CORE_ADDR lowpc;
600 CORE_ADDR highpc;
72bf9492 601
93311388 602 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 603 DW_AT_sibling, if any. */
abc72ce4
DE
604 /* NOTE: This member isn't strictly necessary, read_partial_die could
605 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 606 gdb_byte *sibling;
72bf9492
DJ
607
608 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
609 DW_AT_specification (or DW_AT_abstract_origin or
610 DW_AT_extension). */
611 unsigned int spec_offset;
612
613 /* Pointers to this DIE's parent, first child, and next sibling,
614 if any. */
615 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
616 };
617
618/* This data structure holds the information of an abbrev. */
619struct abbrev_info
620 {
621 unsigned int number; /* number identifying abbrev */
622 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
623 unsigned short has_children; /* boolean */
624 unsigned short num_attrs; /* number of attributes */
c906108c
SS
625 struct attr_abbrev *attrs; /* an array of attribute descriptions */
626 struct abbrev_info *next; /* next in chain */
627 };
628
629struct attr_abbrev
630 {
9d25dd43
DE
631 ENUM_BITFIELD(dwarf_attribute) name : 16;
632 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
633 };
634
b60c80d6
DJ
635/* Attributes have a name and a value */
636struct attribute
637 {
9d25dd43 638 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
639 ENUM_BITFIELD(dwarf_form) form : 15;
640
641 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
642 field should be in u.str (existing only for DW_STRING) but it is kept
643 here for better struct attribute alignment. */
644 unsigned int string_is_canonical : 1;
645
b60c80d6
DJ
646 union
647 {
648 char *str;
649 struct dwarf_block *blk;
43bbcdc2
PH
650 ULONGEST unsnd;
651 LONGEST snd;
b60c80d6 652 CORE_ADDR addr;
348e048f 653 struct signatured_type *signatured_type;
b60c80d6
DJ
654 }
655 u;
656 };
657
c906108c
SS
658/* This data structure holds a complete die structure. */
659struct die_info
660 {
76815b17
DE
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Number of attributes */
98bfdba5
PA
665 unsigned char num_attrs;
666
667 /* True if we're presently building the full type name for the
668 type derived from this DIE. */
669 unsigned char building_fullname : 1;
76815b17
DE
670
671 /* Abbrev number */
672 unsigned int abbrev;
673
93311388 674 /* Offset in .debug_info or .debug_types section. */
76815b17 675 unsigned int offset;
78ba4af6
JB
676
677 /* The dies in a compilation unit form an n-ary tree. PARENT
678 points to this die's parent; CHILD points to the first child of
679 this node; and all the children of a given node are chained
4950bc1c 680 together via their SIBLING fields. */
639d11d3
DC
681 struct die_info *child; /* Its first child, if any. */
682 struct die_info *sibling; /* Its next sibling, if any. */
683 struct die_info *parent; /* Its parent, if any. */
c906108c 684
b60c80d6
DJ
685 /* An array of attributes, with NUM_ATTRS elements. There may be
686 zero, but it's not common and zero-sized arrays are not
687 sufficiently portable C. */
688 struct attribute attrs[1];
c906108c
SS
689 };
690
5fb290d7
DJ
691struct function_range
692{
693 const char *name;
694 CORE_ADDR lowpc, highpc;
695 int seen_line;
696 struct function_range *next;
697};
698
c906108c
SS
699/* Get at parts of an attribute structure */
700
701#define DW_STRING(attr) ((attr)->u.str)
8285870a 702#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
703#define DW_UNSND(attr) ((attr)->u.unsnd)
704#define DW_BLOCK(attr) ((attr)->u.blk)
705#define DW_SND(attr) ((attr)->u.snd)
706#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 707#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
708
709/* Blocks are a bunch of untyped bytes. */
710struct dwarf_block
711 {
712 unsigned int size;
fe1b8b76 713 gdb_byte *data;
c906108c
SS
714 };
715
c906108c
SS
716#ifndef ATTR_ALLOC_CHUNK
717#define ATTR_ALLOC_CHUNK 4
718#endif
719
c906108c
SS
720/* Allocate fields for structs, unions and enums in this size. */
721#ifndef DW_FIELD_ALLOC_CHUNK
722#define DW_FIELD_ALLOC_CHUNK 4
723#endif
724
c906108c
SS
725/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
726 but this would require a corresponding change in unpack_field_as_long
727 and friends. */
728static int bits_per_byte = 8;
729
730/* The routines that read and process dies for a C struct or C++ class
731 pass lists of data member fields and lists of member function fields
732 in an instance of a field_info structure, as defined below. */
733struct field_info
c5aa993b
JM
734 {
735 /* List of data member and baseclasses fields. */
736 struct nextfield
737 {
738 struct nextfield *next;
739 int accessibility;
740 int virtuality;
741 struct field field;
742 }
7d0ccb61 743 *fields, *baseclasses;
c906108c 744
7d0ccb61 745 /* Number of fields (including baseclasses). */
c5aa993b 746 int nfields;
c906108c 747
c5aa993b
JM
748 /* Number of baseclasses. */
749 int nbaseclasses;
c906108c 750
c5aa993b
JM
751 /* Set if the accesibility of one of the fields is not public. */
752 int non_public_fields;
c906108c 753
c5aa993b
JM
754 /* Member function fields array, entries are allocated in the order they
755 are encountered in the object file. */
756 struct nextfnfield
757 {
758 struct nextfnfield *next;
759 struct fn_field fnfield;
760 }
761 *fnfields;
c906108c 762
c5aa993b
JM
763 /* Member function fieldlist array, contains name of possibly overloaded
764 member function, number of overloaded member functions and a pointer
765 to the head of the member function field chain. */
766 struct fnfieldlist
767 {
768 char *name;
769 int length;
770 struct nextfnfield *head;
771 }
772 *fnfieldlists;
c906108c 773
c5aa993b
JM
774 /* Number of entries in the fnfieldlists array. */
775 int nfnfields;
98751a41
JK
776
777 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
778 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
779 struct typedef_field_list
780 {
781 struct typedef_field field;
782 struct typedef_field_list *next;
783 }
784 *typedef_field_list;
785 unsigned typedef_field_list_count;
c5aa993b 786 };
c906108c 787
10b3939b
DJ
788/* One item on the queue of compilation units to read in full symbols
789 for. */
790struct dwarf2_queue_item
791{
792 struct dwarf2_per_cu_data *per_cu;
793 struct dwarf2_queue_item *next;
794};
795
796/* The current queue. */
797static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
798
ae038cb0
DJ
799/* Loaded secondary compilation units are kept in memory until they
800 have not been referenced for the processing of this many
801 compilation units. Set this to zero to disable caching. Cache
802 sizes of up to at least twenty will improve startup time for
803 typical inter-CU-reference binaries, at an obvious memory cost. */
804static int dwarf2_max_cache_age = 5;
920d2a44
AC
805static void
806show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
807 struct cmd_list_element *c, const char *value)
808{
809 fprintf_filtered (file, _("\
810The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
811 value);
812}
813
ae038cb0 814
c906108c
SS
815/* Various complaints about symbol reading that don't abort the process */
816
4d3c2250
KB
817static void
818dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 819{
4d3c2250 820 complaint (&symfile_complaints,
e2e0b3e5 821 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
822}
823
25e43795
DJ
824static void
825dwarf2_debug_line_missing_file_complaint (void)
826{
827 complaint (&symfile_complaints,
828 _(".debug_line section has line data without a file"));
829}
830
59205f5a
JB
831static void
832dwarf2_debug_line_missing_end_sequence_complaint (void)
833{
834 complaint (&symfile_complaints,
835 _(".debug_line section has line program sequence without an end"));
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,
e2e0b3e5 849 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
850 arg2, arg3);
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,
e2e0b3e5 864 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
865 arg1);
866}
867
868static void
869dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 870{
4d3c2250 871 complaint (&symfile_complaints,
e2e0b3e5 872 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 873}
c906108c 874
c906108c
SS
875/* local function prototypes */
876
4efb68b1 877static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 878
aaa75496
JB
879static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880 struct objfile *);
881
c67a9c90 882static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 883
72bf9492
DJ
884static void scan_partial_symbols (struct partial_die_info *,
885 CORE_ADDR *, CORE_ADDR *,
5734ee8b 886 int, struct dwarf2_cu *);
c906108c 887
72bf9492
DJ
888static void add_partial_symbol (struct partial_die_info *,
889 struct dwarf2_cu *);
63d06c5c 890
72bf9492
DJ
891static void add_partial_namespace (struct partial_die_info *pdi,
892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 893 int need_pc, struct dwarf2_cu *cu);
63d06c5c 894
5d7cb8df
JK
895static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896 CORE_ADDR *highpc, int need_pc,
897 struct dwarf2_cu *cu);
898
72bf9492
DJ
899static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900 struct dwarf2_cu *cu);
91c24f0a 901
bc30ff58
JB
902static void add_partial_subprogram (struct partial_die_info *pdi,
903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 904 int need_pc, struct dwarf2_cu *cu);
bc30ff58 905
fe1b8b76 906static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
907 gdb_byte *buffer, gdb_byte *info_ptr,
908 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 909
a14ed312 910static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 911
a14ed312 912static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 913
e7c27a73 914static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 915
f3dd6933 916static void dwarf2_free_abbrev_table (void *);
c906108c 917
fe1b8b76 918static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 919 struct dwarf2_cu *);
72bf9492 920
57349743 921static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 922 struct dwarf2_cu *);
c906108c 923
93311388
DE
924static struct partial_die_info *load_partial_dies (bfd *,
925 gdb_byte *, gdb_byte *,
926 int, struct dwarf2_cu *);
72bf9492 927
fe1b8b76 928static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
929 struct abbrev_info *abbrev,
930 unsigned int, bfd *,
931 gdb_byte *, gdb_byte *,
932 struct dwarf2_cu *);
c906108c 933
c764a876 934static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 935 struct dwarf2_cu *);
72bf9492
DJ
936
937static void fixup_partial_die (struct partial_die_info *,
938 struct dwarf2_cu *);
939
fe1b8b76
JB
940static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 942
fe1b8b76
JB
943static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 945
fe1b8b76 946static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 947
fe1b8b76 948static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 949
fe1b8b76 950static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 951
fe1b8b76 952static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 953
93311388 954static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 955
fe1b8b76 956static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 957 unsigned int *);
c906108c 958
c764a876
DE
959static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961static LONGEST read_checked_initial_length_and_offset
962 (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *, unsigned int *);
613e1657 964
fe1b8b76 965static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
966 unsigned int *);
967
968static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 969
fe1b8b76 970static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 971
9b1c24c8 972static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 973
fe1b8b76
JB
974static char *read_indirect_string (bfd *, gdb_byte *,
975 const struct comp_unit_head *,
976 unsigned int *);
4bdf3d34 977
fe1b8b76 978static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 979
fe1b8b76 980static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 981
fe1b8b76 982static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 983
e142c38c 984static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 985
e142c38c
DJ
986static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 struct dwarf2_cu *);
c906108c 988
348e048f
DE
989static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990 unsigned int,
991 struct dwarf2_cu *);
992
05cf31d1
JB
993static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994 struct dwarf2_cu *cu);
995
e142c38c 996static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 997
e142c38c 998static struct die_info *die_specification (struct die_info *die,
f2f0e013 999 struct dwarf2_cu **);
63d06c5c 1000
debd256d
JB
1001static void free_line_header (struct line_header *lh);
1002
aaa75496
JB
1003static void add_file_name (struct line_header *, char *, unsigned int,
1004 unsigned int, unsigned int);
1005
debd256d
JB
1006static struct line_header *(dwarf_decode_line_header
1007 (unsigned int offset,
e7c27a73 1008 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1009
72b9f47f 1010static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1011 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1012
72b9f47f 1013static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1014
a14ed312 1015static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1016 struct dwarf2_cu *);
c906108c 1017
34eaf542
TT
1018static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol *);
1020
a14ed312 1021static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1022 struct dwarf2_cu *);
c906108c 1023
98bfdba5
PA
1024static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, long *value,
1029 gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
2df3850c 1031
e7c27a73 1032static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1033
b4ba55a1
JB
1034static int need_gnat_info (struct dwarf2_cu *);
1035
1036static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1037
1038static void set_descriptive_type (struct type *, struct die_info *,
1039 struct dwarf2_cu *);
1040
e7c27a73
DJ
1041static struct type *die_containing_type (struct die_info *,
1042 struct dwarf2_cu *);
c906108c 1043
673bfd45
DE
1044static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045 struct dwarf2_cu *);
c906108c 1046
f792889a 1047static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1048
673bfd45
DE
1049static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
086ed43d 1051static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1052
6e70227d 1053static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1054 const char *suffix, int physname,
1055 struct dwarf2_cu *cu);
63d06c5c 1056
e7c27a73 1057static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1058
348e048f
DE
1059static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
e7c27a73 1061static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1062
e7c27a73 1063static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1064
ff013f42
JK
1065static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *, struct partial_symtab *);
1067
a14ed312 1068static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1069 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070 struct partial_symtab *);
c906108c 1071
fae299cd
DC
1072static void get_scope_pc_bounds (struct die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 struct dwarf2_cu *);
1075
801e3a5b
JB
1076static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077 CORE_ADDR, struct dwarf2_cu *);
1078
a14ed312 1079static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1080 struct dwarf2_cu *);
c906108c 1081
a14ed312 1082static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1083 struct type *, struct dwarf2_cu *);
c906108c 1084
a14ed312 1085static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1086 struct die_info *, struct type *,
e7c27a73 1087 struct dwarf2_cu *);
c906108c 1088
a14ed312 1089static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1090 struct type *, struct dwarf2_cu *);
c906108c 1091
134d01f1 1092static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1093
e7c27a73 1094static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1097
5d7cb8df
JK
1098static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1099
27aa8d6a
SW
1100static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1101
f55ee35c
JK
1102static struct type *read_module_type (struct die_info *die,
1103 struct dwarf2_cu *cu);
1104
38d518c9 1105static const char *namespace_name (struct die_info *die,
e142c38c 1106 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1107
134d01f1 1108static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1109
e7c27a73 1110static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1111
6e70227d 1112static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1113 struct dwarf2_cu *);
1114
93311388 1115static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1116
93311388
DE
1117static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1118 gdb_byte *info_ptr,
d97bc12b
DE
1119 gdb_byte **new_info_ptr,
1120 struct die_info *parent);
1121
93311388
DE
1122static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1123 gdb_byte *info_ptr,
fe1b8b76 1124 gdb_byte **new_info_ptr,
639d11d3
DC
1125 struct die_info *parent);
1126
93311388
DE
1127static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1128 gdb_byte *info_ptr,
fe1b8b76 1129 gdb_byte **new_info_ptr,
639d11d3
DC
1130 struct die_info *parent);
1131
93311388
DE
1132static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1133 struct die_info **, gdb_byte *,
1134 int *);
1135
e7c27a73 1136static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1137
71c25dea
TT
1138static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1139 struct obstack *);
1140
e142c38c 1141static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1142
98bfdba5
PA
1143static const char *dwarf2_full_name (char *name,
1144 struct die_info *die,
1145 struct dwarf2_cu *cu);
1146
e142c38c 1147static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1148 struct dwarf2_cu **);
9219021c 1149
a14ed312 1150static char *dwarf_tag_name (unsigned int);
c906108c 1151
a14ed312 1152static char *dwarf_attr_name (unsigned int);
c906108c 1153
a14ed312 1154static char *dwarf_form_name (unsigned int);
c906108c 1155
a14ed312 1156static char *dwarf_bool_name (unsigned int);
c906108c 1157
a14ed312 1158static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1159
1160#if 0
a14ed312 1161static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1162#endif
1163
f9aca02d 1164static struct die_info *sibling_die (struct die_info *);
c906108c 1165
d97bc12b
DE
1166static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1167
1168static void dump_die_for_error (struct die_info *);
1169
1170static void dump_die_1 (struct ui_file *, int level, int max_level,
1171 struct die_info *);
c906108c 1172
d97bc12b 1173/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1174
51545339 1175static void store_in_ref_table (struct die_info *,
10b3939b 1176 struct dwarf2_cu *);
c906108c 1177
93311388
DE
1178static int is_ref_attr (struct attribute *);
1179
c764a876 1180static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1181
43bbcdc2 1182static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1183
348e048f
DE
1184static struct die_info *follow_die_ref_or_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
10b3939b
DJ
1188static struct die_info *follow_die_ref (struct die_info *,
1189 struct attribute *,
f2f0e013 1190 struct dwarf2_cu **);
c906108c 1191
348e048f
DE
1192static struct die_info *follow_die_sig (struct die_info *,
1193 struct attribute *,
1194 struct dwarf2_cu **);
1195
1196static void read_signatured_type_at_offset (struct objfile *objfile,
1197 unsigned int offset);
1198
1199static void read_signatured_type (struct objfile *,
1200 struct signatured_type *type_sig);
1201
c906108c
SS
1202/* memory allocation interface */
1203
7b5a2f43 1204static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1205
f3dd6933 1206static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1207
b60c80d6 1208static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1209
e142c38c 1210static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1211
e142c38c
DJ
1212static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1213 struct dwarf2_cu *);
5fb290d7 1214
2e276125 1215static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1216 char *, bfd *, struct dwarf2_cu *);
2e276125 1217
8e19ed76
PS
1218static int attr_form_is_block (struct attribute *);
1219
3690dd37
JB
1220static int attr_form_is_section_offset (struct attribute *);
1221
1222static int attr_form_is_constant (struct attribute *);
1223
93e7bd98
DJ
1224static void dwarf2_symbol_mark_computed (struct attribute *attr,
1225 struct symbol *sym,
1226 struct dwarf2_cu *cu);
4c2df51b 1227
93311388
DE
1228static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1229 struct abbrev_info *abbrev,
1230 struct dwarf2_cu *cu);
4bb7a0a7 1231
72bf9492
DJ
1232static void free_stack_comp_unit (void *);
1233
72bf9492
DJ
1234static hashval_t partial_die_hash (const void *item);
1235
1236static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1237
ae038cb0 1238static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1239 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1240
1241static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1242 (unsigned int offset, struct objfile *objfile);
ae038cb0 1243
9816fde3
JK
1244static void init_one_comp_unit (struct dwarf2_cu *cu,
1245 struct objfile *objfile);
1246
1247static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1248 struct die_info *comp_unit_die);
93311388 1249
ae038cb0
DJ
1250static void free_one_comp_unit (void *);
1251
1252static void free_cached_comp_units (void *);
1253
1254static void age_cached_comp_units (void);
1255
1256static void free_one_cached_comp_unit (void *);
1257
f792889a
DJ
1258static struct type *set_die_type (struct die_info *, struct type *,
1259 struct dwarf2_cu *);
1c379e20 1260
ae038cb0
DJ
1261static void create_all_comp_units (struct objfile *);
1262
1fd400ff
TT
1263static int create_debug_types_hash_table (struct objfile *objfile);
1264
93311388
DE
1265static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1266 struct objfile *);
10b3939b
DJ
1267
1268static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1269
1270static void dwarf2_add_dependence (struct dwarf2_cu *,
1271 struct dwarf2_per_cu_data *);
1272
ae038cb0
DJ
1273static void dwarf2_mark (struct dwarf2_cu *);
1274
1275static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1276
673bfd45
DE
1277static struct type *get_die_type_at_offset (unsigned int,
1278 struct dwarf2_per_cu_data *per_cu);
1279
f792889a 1280static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1281
9291a0cd
TT
1282static void dwarf2_release_queue (void *dummy);
1283
1284static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1285 struct objfile *objfile);
1286
1287static void process_queue (struct objfile *objfile);
1288
1289static void find_file_and_directory (struct die_info *die,
1290 struct dwarf2_cu *cu,
1291 char **name, char **comp_dir);
1292
1293static char *file_full_name (int file, struct line_header *lh,
1294 const char *comp_dir);
1295
1296static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1297 gdb_byte *info_ptr,
1298 gdb_byte *buffer,
1299 unsigned int buffer_size,
1300 bfd *abfd);
1301
1302static void init_cu_die_reader (struct die_reader_specs *reader,
1303 struct dwarf2_cu *cu);
1304
673bfd45 1305static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1306
9291a0cd
TT
1307#if WORDS_BIGENDIAN
1308
1309/* Convert VALUE between big- and little-endian. */
1310static offset_type
1311byte_swap (offset_type value)
1312{
1313 offset_type result;
1314
1315 result = (value & 0xff) << 24;
1316 result |= (value & 0xff00) << 8;
1317 result |= (value & 0xff0000) >> 8;
1318 result |= (value & 0xff000000) >> 24;
1319 return result;
1320}
1321
1322#define MAYBE_SWAP(V) byte_swap (V)
1323
1324#else
1325#define MAYBE_SWAP(V) (V)
1326#endif /* WORDS_BIGENDIAN */
1327
1328/* The suffix for an index file. */
1329#define INDEX_SUFFIX ".gdb-index"
1330
3da10d80
KS
1331static const char *dwarf2_physname (char *name, struct die_info *die,
1332 struct dwarf2_cu *cu);
1333
c906108c
SS
1334/* Try to locate the sections we need for DWARF 2 debugging
1335 information and return true if we have enough to do something. */
1336
1337int
6502dd73 1338dwarf2_has_info (struct objfile *objfile)
c906108c 1339{
be391dca
TT
1340 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1341 if (!dwarf2_per_objfile)
1342 {
1343 /* Initialize per-objfile state. */
1344 struct dwarf2_per_objfile *data
1345 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1346
be391dca
TT
1347 memset (data, 0, sizeof (*data));
1348 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1349 dwarf2_per_objfile = data;
6502dd73 1350
be391dca
TT
1351 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1352 dwarf2_per_objfile->objfile = objfile;
1353 }
1354 return (dwarf2_per_objfile->info.asection != NULL
1355 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1356}
1357
233a11ab
CS
1358/* When loading sections, we can either look for ".<name>", or for
1359 * ".z<name>", which indicates a compressed section. */
1360
1361static int
dce234bc 1362section_is_p (const char *section_name, const char *name)
233a11ab 1363{
dce234bc
PP
1364 return (section_name[0] == '.'
1365 && (strcmp (section_name + 1, name) == 0
1366 || (section_name[1] == 'z'
1367 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1368}
1369
c906108c
SS
1370/* This function is mapped across the sections and remembers the
1371 offset and size of each of the debugging sections we are interested
1372 in. */
1373
1374static void
72dca2f5 1375dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1376{
dce234bc 1377 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->info.asection = sectp;
1380 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1381 }
dce234bc 1382 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->abbrev.asection = sectp;
1385 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->line.asection = sectp;
1390 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->loc.asection = sectp;
1395 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->macinfo.asection = sectp;
1400 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1401 }
dce234bc 1402 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1403 {
dce234bc
PP
1404 dwarf2_per_objfile->str.asection = sectp;
1405 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1406 }
dce234bc 1407 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->frame.asection = sectp;
1410 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1411 }
dce234bc 1412 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1413 {
3799ccc6 1414 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1415
3799ccc6
EZ
1416 if (aflag & SEC_HAS_CONTENTS)
1417 {
dce234bc
PP
1418 dwarf2_per_objfile->eh_frame.asection = sectp;
1419 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1420 }
b6af0555 1421 }
dce234bc 1422 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1423 {
dce234bc
PP
1424 dwarf2_per_objfile->ranges.asection = sectp;
1425 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1426 }
348e048f
DE
1427 else if (section_is_p (sectp->name, TYPES_SECTION))
1428 {
1429 dwarf2_per_objfile->types.asection = sectp;
1430 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1431 }
9291a0cd
TT
1432 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1433 {
1434 dwarf2_per_objfile->gdb_index.asection = sectp;
1435 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1436 }
dce234bc 1437
72dca2f5
FR
1438 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1439 && bfd_section_vma (abfd, sectp) == 0)
1440 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1441}
1442
dce234bc
PP
1443/* Decompress a section that was compressed using zlib. Store the
1444 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1445
1446static void
dce234bc
PP
1447zlib_decompress_section (struct objfile *objfile, asection *sectp,
1448 gdb_byte **outbuf, bfd_size_type *outsize)
1449{
1450 bfd *abfd = objfile->obfd;
1451#ifndef HAVE_ZLIB_H
1452 error (_("Support for zlib-compressed DWARF data (from '%s') "
1453 "is disabled in this copy of GDB"),
1454 bfd_get_filename (abfd));
1455#else
1456 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1457 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1458 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1459 bfd_size_type uncompressed_size;
1460 gdb_byte *uncompressed_buffer;
1461 z_stream strm;
1462 int rc;
1463 int header_size = 12;
1464
1465 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1466 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1467 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1468 bfd_get_filename (abfd));
1469
1470 /* Read the zlib header. In this case, it should be "ZLIB" followed
1471 by the uncompressed section size, 8 bytes in big-endian order. */
1472 if (compressed_size < header_size
1473 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1474 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1475 bfd_get_filename (abfd));
1476 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1477 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1478 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1479 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1480 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[11];
1484
1485 /* It is possible the section consists of several compressed
1486 buffers concatenated together, so we uncompress in a loop. */
1487 strm.zalloc = NULL;
1488 strm.zfree = NULL;
1489 strm.opaque = NULL;
1490 strm.avail_in = compressed_size - header_size;
1491 strm.next_in = (Bytef*) compressed_buffer + header_size;
1492 strm.avail_out = uncompressed_size;
1493 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1494 uncompressed_size);
1495 rc = inflateInit (&strm);
1496 while (strm.avail_in > 0)
1497 {
1498 if (rc != Z_OK)
1499 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1500 bfd_get_filename (abfd), rc);
1501 strm.next_out = ((Bytef*) uncompressed_buffer
1502 + (uncompressed_size - strm.avail_out));
1503 rc = inflate (&strm, Z_FINISH);
1504 if (rc != Z_STREAM_END)
1505 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1506 bfd_get_filename (abfd), rc);
1507 rc = inflateReset (&strm);
1508 }
1509 rc = inflateEnd (&strm);
1510 if (rc != Z_OK
1511 || strm.avail_out != 0)
1512 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1513 bfd_get_filename (abfd), rc);
1514
affddf13 1515 do_cleanups (cleanup);
dce234bc
PP
1516 *outbuf = uncompressed_buffer;
1517 *outsize = uncompressed_size;
1518#endif
233a11ab
CS
1519}
1520
dce234bc
PP
1521/* Read the contents of the section SECTP from object file specified by
1522 OBJFILE, store info about the section into INFO.
1523 If the section is compressed, uncompress it before returning. */
c906108c 1524
dce234bc
PP
1525static void
1526dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1527{
dce234bc
PP
1528 bfd *abfd = objfile->obfd;
1529 asection *sectp = info->asection;
1530 gdb_byte *buf, *retbuf;
1531 unsigned char header[4];
c906108c 1532
be391dca
TT
1533 if (info->readin)
1534 return;
dce234bc
PP
1535 info->buffer = NULL;
1536 info->was_mmapped = 0;
be391dca 1537 info->readin = 1;
188dd5d6 1538
dce234bc
PP
1539 if (info->asection == NULL || info->size == 0)
1540 return;
c906108c 1541
dce234bc
PP
1542 /* Check if the file has a 4-byte header indicating compression. */
1543 if (info->size > sizeof (header)
1544 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1545 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1546 {
1547 /* Upon decompression, update the buffer and its size. */
1548 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1549 {
1550 zlib_decompress_section (objfile, sectp, &info->buffer,
1551 &info->size);
1552 return;
1553 }
1554 }
4bdf3d34 1555
dce234bc
PP
1556#ifdef HAVE_MMAP
1557 if (pagesize == 0)
1558 pagesize = getpagesize ();
2e276125 1559
dce234bc
PP
1560 /* Only try to mmap sections which are large enough: we don't want to
1561 waste space due to fragmentation. Also, only try mmap for sections
1562 without relocations. */
1563
1564 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1565 {
1566 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1567 size_t map_length = info->size + sectp->filepos - pg_offset;
1568 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1569 MAP_PRIVATE, pg_offset);
1570
1571 if (retbuf != MAP_FAILED)
1572 {
1573 info->was_mmapped = 1;
1574 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1575#if HAVE_POSIX_MADVISE
1576 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1577#endif
dce234bc
PP
1578 return;
1579 }
1580 }
1581#endif
1582
1583 /* If we get here, we are a normal, not-compressed section. */
1584 info->buffer = buf
1585 = obstack_alloc (&objfile->objfile_obstack, info->size);
1586
1587 /* When debugging .o files, we may need to apply relocations; see
1588 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1589 We never compress sections in .o files, so we only need to
1590 try this when the section is not compressed. */
ac8035ab 1591 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1592 if (retbuf != NULL)
1593 {
1594 info->buffer = retbuf;
1595 return;
1596 }
1597
1598 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1599 || bfd_bread (buf, info->size, abfd) != info->size)
1600 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1601 bfd_get_filename (abfd));
1602}
1603
1604/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1605 SECTION_NAME. */
af34e669 1606
dce234bc
PP
1607void
1608dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1609 asection **sectp, gdb_byte **bufp,
1610 bfd_size_type *sizep)
1611{
1612 struct dwarf2_per_objfile *data
1613 = objfile_data (objfile, dwarf2_objfile_data_key);
1614 struct dwarf2_section_info *info;
a3b2a86b
TT
1615
1616 /* We may see an objfile without any DWARF, in which case we just
1617 return nothing. */
1618 if (data == NULL)
1619 {
1620 *sectp = NULL;
1621 *bufp = NULL;
1622 *sizep = 0;
1623 return;
1624 }
dce234bc
PP
1625 if (section_is_p (section_name, EH_FRAME_SECTION))
1626 info = &data->eh_frame;
1627 else if (section_is_p (section_name, FRAME_SECTION))
1628 info = &data->frame;
0d53c4c4 1629 else
f3574227 1630 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1631
1632 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1633 /* We haven't read this section in yet. Do it now. */
1634 dwarf2_read_section (objfile, info);
1635
1636 *sectp = info->asection;
1637 *bufp = info->buffer;
1638 *sizep = info->size;
1639}
1640
9291a0cd
TT
1641\f
1642
1643/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1644 this CU came. */
2fdf6df6 1645
9291a0cd
TT
1646static void
1647dw2_do_instantiate_symtab (struct objfile *objfile,
1648 struct dwarf2_per_cu_data *per_cu)
1649{
1650 struct cleanup *back_to;
1651
1652 back_to = make_cleanup (dwarf2_release_queue, NULL);
1653
1654 queue_comp_unit (per_cu, objfile);
1655
1656 if (per_cu->from_debug_types)
1657 read_signatured_type_at_offset (objfile, per_cu->offset);
1658 else
1659 load_full_comp_unit (per_cu, objfile);
1660
1661 process_queue (objfile);
1662
1663 /* Age the cache, releasing compilation units that have not
1664 been used recently. */
1665 age_cached_comp_units ();
1666
1667 do_cleanups (back_to);
1668}
1669
1670/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1671 the objfile from which this CU came. Returns the resulting symbol
1672 table. */
2fdf6df6 1673
9291a0cd
TT
1674static struct symtab *
1675dw2_instantiate_symtab (struct objfile *objfile,
1676 struct dwarf2_per_cu_data *per_cu)
1677{
1678 if (!per_cu->v.quick->symtab)
1679 {
1680 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1681 increment_reading_symtab ();
1682 dw2_do_instantiate_symtab (objfile, per_cu);
1683 do_cleanups (back_to);
1684 }
1685 return per_cu->v.quick->symtab;
1686}
1687
1fd400ff 1688/* Return the CU given its index. */
2fdf6df6 1689
1fd400ff
TT
1690static struct dwarf2_per_cu_data *
1691dw2_get_cu (int index)
1692{
1693 if (index >= dwarf2_per_objfile->n_comp_units)
1694 {
1695 index -= dwarf2_per_objfile->n_comp_units;
1696 return dwarf2_per_objfile->type_comp_units[index];
1697 }
1698 return dwarf2_per_objfile->all_comp_units[index];
1699}
1700
9291a0cd
TT
1701/* A helper function that knows how to read a 64-bit value in a way
1702 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1703 otherwise. */
2fdf6df6 1704
9291a0cd
TT
1705static int
1706extract_cu_value (const char *bytes, ULONGEST *result)
1707{
1708 if (sizeof (ULONGEST) < 8)
1709 {
1710 int i;
1711
1712 /* Ignore the upper 4 bytes if they are all zero. */
1713 for (i = 0; i < 4; ++i)
1714 if (bytes[i + 4] != 0)
1715 return 0;
1716
1717 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1718 }
1719 else
1720 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1721 return 1;
1722}
1723
1724/* Read the CU list from the mapped index, and use it to create all
1725 the CU objects for this objfile. Return 0 if something went wrong,
1726 1 if everything went ok. */
2fdf6df6 1727
9291a0cd 1728static int
1fd400ff
TT
1729create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1730 offset_type cu_list_elements)
9291a0cd
TT
1731{
1732 offset_type i;
9291a0cd
TT
1733
1734 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1735 dwarf2_per_objfile->all_comp_units
1736 = obstack_alloc (&objfile->objfile_obstack,
1737 dwarf2_per_objfile->n_comp_units
1738 * sizeof (struct dwarf2_per_cu_data *));
1739
1740 for (i = 0; i < cu_list_elements; i += 2)
1741 {
1742 struct dwarf2_per_cu_data *the_cu;
1743 ULONGEST offset, length;
1744
1745 if (!extract_cu_value (cu_list, &offset)
1746 || !extract_cu_value (cu_list + 8, &length))
1747 return 0;
1748 cu_list += 2 * 8;
1749
1750 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1751 struct dwarf2_per_cu_data);
1752 the_cu->offset = offset;
1753 the_cu->length = length;
1754 the_cu->objfile = objfile;
1755 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1756 struct dwarf2_per_cu_quick_data);
1757 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1758 }
1759
1760 return 1;
1761}
1762
1fd400ff 1763/* Create the signatured type hash table from the index. */
673bfd45 1764
1fd400ff 1765static int
673bfd45
DE
1766create_signatured_type_table_from_index (struct objfile *objfile,
1767 const gdb_byte *bytes,
1768 offset_type elements)
1fd400ff
TT
1769{
1770 offset_type i;
673bfd45 1771 htab_t sig_types_hash;
1fd400ff
TT
1772
1773 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1774 dwarf2_per_objfile->type_comp_units
1775 = obstack_alloc (&objfile->objfile_obstack,
1776 dwarf2_per_objfile->n_type_comp_units
1777 * sizeof (struct dwarf2_per_cu_data *));
1778
673bfd45 1779 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1780
1781 for (i = 0; i < elements; i += 3)
1782 {
1783 struct signatured_type *type_sig;
1784 ULONGEST offset, type_offset, signature;
1785 void **slot;
1786
1787 if (!extract_cu_value (bytes, &offset)
1788 || !extract_cu_value (bytes + 8, &type_offset))
1789 return 0;
1790 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1791 bytes += 3 * 8;
1792
1793 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1794 struct signatured_type);
1795 type_sig->signature = signature;
1796 type_sig->offset = offset;
1797 type_sig->type_offset = type_offset;
1798 type_sig->per_cu.from_debug_types = 1;
1799 type_sig->per_cu.offset = offset;
1800 type_sig->per_cu.objfile = objfile;
1801 type_sig->per_cu.v.quick
1802 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1803 struct dwarf2_per_cu_quick_data);
1804
673bfd45 1805 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1806 *slot = type_sig;
1807
1808 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1809 }
1810
673bfd45 1811 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1812
1813 return 1;
1814}
1815
9291a0cd
TT
1816/* Read the address map data from the mapped index, and use it to
1817 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1818
9291a0cd
TT
1819static void
1820create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1821{
1822 const gdb_byte *iter, *end;
1823 struct obstack temp_obstack;
1824 struct addrmap *mutable_map;
1825 struct cleanup *cleanup;
1826 CORE_ADDR baseaddr;
1827
1828 obstack_init (&temp_obstack);
1829 cleanup = make_cleanup_obstack_free (&temp_obstack);
1830 mutable_map = addrmap_create_mutable (&temp_obstack);
1831
1832 iter = index->address_table;
1833 end = iter + index->address_table_size;
1834
1835 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1836
1837 while (iter < end)
1838 {
1839 ULONGEST hi, lo, cu_index;
1840 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1841 iter += 8;
1842 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1843 iter += 8;
1844 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1845 iter += 4;
1846
1847 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1848 dw2_get_cu (cu_index));
9291a0cd
TT
1849 }
1850
1851 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1852 &objfile->objfile_obstack);
1853 do_cleanups (cleanup);
1854}
1855
1856/* The hash function for strings in the mapped index. This is the
1857 same as the hashtab.c hash function, but we keep a separate copy to
1858 maintain control over the implementation. This is necessary
1859 because the hash function is tied to the format of the mapped index
1860 file. */
2fdf6df6 1861
9291a0cd
TT
1862static hashval_t
1863mapped_index_string_hash (const void *p)
1864{
1865 const unsigned char *str = (const unsigned char *) p;
1866 hashval_t r = 0;
1867 unsigned char c;
1868
1869 while ((c = *str++) != 0)
1870 r = r * 67 + c - 113;
1871
1872 return r;
1873}
1874
1875/* Find a slot in the mapped index INDEX for the object named NAME.
1876 If NAME is found, set *VEC_OUT to point to the CU vector in the
1877 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1878
9291a0cd
TT
1879static int
1880find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1881 offset_type **vec_out)
1882{
1883 offset_type hash = mapped_index_string_hash (name);
1884 offset_type slot, step;
1885
3876f04e
DE
1886 slot = hash & (index->symbol_table_slots - 1);
1887 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1888
1889 for (;;)
1890 {
1891 /* Convert a slot number to an offset into the table. */
1892 offset_type i = 2 * slot;
1893 const char *str;
3876f04e 1894 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1895 return 0;
1896
3876f04e 1897 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1898 if (!strcmp (name, str))
1899 {
1900 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1901 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1902 return 1;
1903 }
1904
3876f04e 1905 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1906 }
1907}
1908
1909/* Read the index file. If everything went ok, initialize the "quick"
1910 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1911
9291a0cd
TT
1912static int
1913dwarf2_read_index (struct objfile *objfile)
1914{
9291a0cd
TT
1915 char *addr;
1916 struct mapped_index *map;
b3b272e1 1917 offset_type *metadata;
ac0b195c
KW
1918 const gdb_byte *cu_list;
1919 const gdb_byte *types_list = NULL;
1920 offset_type version, cu_list_elements;
1921 offset_type types_list_elements = 0;
1fd400ff 1922 int i;
9291a0cd
TT
1923
1924 if (dwarf2_per_objfile->gdb_index.asection == NULL
1925 || dwarf2_per_objfile->gdb_index.size == 0)
1926 return 0;
82430852
JK
1927
1928 /* Older elfutils strip versions could keep the section in the main
1929 executable while splitting it for the separate debug info file. */
1930 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1931 & SEC_HAS_CONTENTS) == 0)
1932 return 0;
1933
9291a0cd
TT
1934 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1935
1936 addr = dwarf2_per_objfile->gdb_index.buffer;
1937 /* Version check. */
1fd400ff 1938 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1939 /* Versions earlier than 3 emitted every copy of a psymbol. This
1940 causes the index to behave very poorly for certain requests. So,
1941 it seems better to just ignore such indices. */
1942 if (version < 3)
9291a0cd 1943 return 0;
594e8718
JK
1944 /* Indexes with higher version than the one supported by GDB may be no
1945 longer backward compatible. */
1946 if (version > 3)
1947 return 0;
9291a0cd
TT
1948
1949 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1950 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1951
1952 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1953
1954 i = 0;
1955 cu_list = addr + MAYBE_SWAP (metadata[i]);
1956 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1957 / 8);
1fd400ff
TT
1958 ++i;
1959
987d643c
TT
1960 types_list = addr + MAYBE_SWAP (metadata[i]);
1961 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1962 - MAYBE_SWAP (metadata[i]))
1963 / 8);
1964 ++i;
1fd400ff
TT
1965
1966 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1967 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1968 - MAYBE_SWAP (metadata[i]));
1969 ++i;
1970
3876f04e
DE
1971 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1972 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1973 - MAYBE_SWAP (metadata[i]))
1974 / (2 * sizeof (offset_type)));
1fd400ff 1975 ++i;
9291a0cd 1976
1fd400ff
TT
1977 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1978
1979 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1980 return 0;
1981
987d643c 1982 if (types_list_elements
673bfd45
DE
1983 && !create_signatured_type_table_from_index (objfile, types_list,
1984 types_list_elements))
9291a0cd
TT
1985 return 0;
1986
1987 create_addrmap_from_index (objfile, map);
1988
1989 dwarf2_per_objfile->index_table = map;
1990 dwarf2_per_objfile->using_index = 1;
1991
1992 return 1;
1993}
1994
1995/* A helper for the "quick" functions which sets the global
1996 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 1997
9291a0cd
TT
1998static void
1999dw2_setup (struct objfile *objfile)
2000{
2001 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2002 gdb_assert (dwarf2_per_objfile);
2003}
2004
2005/* A helper for the "quick" functions which attempts to read the line
2006 table for THIS_CU. */
2fdf6df6 2007
9291a0cd
TT
2008static void
2009dw2_require_line_header (struct objfile *objfile,
2010 struct dwarf2_per_cu_data *this_cu)
2011{
2012 bfd *abfd = objfile->obfd;
2013 struct line_header *lh = NULL;
2014 struct attribute *attr;
2015 struct cleanup *cleanups;
2016 struct die_info *comp_unit_die;
36374493 2017 struct dwarf2_section_info* sec;
9291a0cd
TT
2018 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2019 int has_children, i;
2020 struct dwarf2_cu cu;
2021 unsigned int bytes_read, buffer_size;
2022 struct die_reader_specs reader_specs;
2023 char *name, *comp_dir;
2024
2025 if (this_cu->v.quick->read_lines)
2026 return;
2027 this_cu->v.quick->read_lines = 1;
2028
9816fde3 2029 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2030 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2031
36374493
DE
2032 if (this_cu->from_debug_types)
2033 sec = &dwarf2_per_objfile->types;
2034 else
2035 sec = &dwarf2_per_objfile->info;
2036 dwarf2_read_section (objfile, sec);
2037 buffer_size = sec->size;
2038 buffer = sec->buffer;
9291a0cd
TT
2039 info_ptr = buffer + this_cu->offset;
2040 beg_of_comp_unit = info_ptr;
2041
2042 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2043 buffer, buffer_size,
2044 abfd);
2045
2046 /* Complete the cu_header. */
2047 cu.header.offset = beg_of_comp_unit - buffer;
2048 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2049
2050 this_cu->cu = &cu;
2051 cu.per_cu = this_cu;
2052
2053 dwarf2_read_abbrevs (abfd, &cu);
2054 make_cleanup (dwarf2_free_abbrev_table, &cu);
2055
2056 if (this_cu->from_debug_types)
2057 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2058 init_cu_die_reader (&reader_specs, &cu);
2059 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2060 &has_children);
2061
2062 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2063 if (attr)
2064 {
2065 unsigned int line_offset = DW_UNSND (attr);
2066 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2067 }
2068 if (lh == NULL)
2069 {
2070 do_cleanups (cleanups);
2071 return;
2072 }
2073
2074 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2075
2076 this_cu->v.quick->lines = lh;
2077
2078 this_cu->v.quick->file_names
2079 = obstack_alloc (&objfile->objfile_obstack,
2080 lh->num_file_names * sizeof (char *));
2081 for (i = 0; i < lh->num_file_names; ++i)
2082 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2083
2084 do_cleanups (cleanups);
2085}
2086
2087/* A helper for the "quick" functions which computes and caches the
2088 real path for a given file name from the line table.
2089 dw2_require_line_header must have been called before this is
2090 invoked. */
2fdf6df6 2091
9291a0cd
TT
2092static const char *
2093dw2_require_full_path (struct objfile *objfile,
e254ef6a 2094 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2095 int index)
2096{
e254ef6a
DE
2097 if (!per_cu->v.quick->full_names)
2098 per_cu->v.quick->full_names
9291a0cd 2099 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2100 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2101 sizeof (char *));
2102
e254ef6a
DE
2103 if (!per_cu->v.quick->full_names[index])
2104 per_cu->v.quick->full_names[index]
2105 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2106
e254ef6a 2107 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2108}
2109
2110static struct symtab *
2111dw2_find_last_source_symtab (struct objfile *objfile)
2112{
2113 int index;
2114 dw2_setup (objfile);
2115 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2116 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2117}
2118
2119static void
2120dw2_forget_cached_source_info (struct objfile *objfile)
2121{
2122 int i;
2123
2124 dw2_setup (objfile);
1fd400ff
TT
2125 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2126 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2127 {
e254ef6a 2128 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2129
e254ef6a 2130 if (per_cu->v.quick->full_names)
9291a0cd
TT
2131 {
2132 int j;
2133
e254ef6a 2134 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
87df528e
JK
2135 {
2136 xfree ((void *) per_cu->v.quick->full_names[j]);
2137 per_cu->v.quick->full_names[j] = NULL;
2138 }
9291a0cd
TT
2139 }
2140 }
2141}
2142
2143static int
2144dw2_lookup_symtab (struct objfile *objfile, const char *name,
2145 const char *full_path, const char *real_path,
2146 struct symtab **result)
2147{
2148 int i;
2149 int check_basename = lbasename (name) == name;
2150 struct dwarf2_per_cu_data *base_cu = NULL;
2151
2152 dw2_setup (objfile);
1fd400ff
TT
2153 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2154 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2155 {
2156 int j;
e254ef6a 2157 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2158
e254ef6a 2159 if (per_cu->v.quick->symtab)
9291a0cd
TT
2160 continue;
2161
e254ef6a
DE
2162 dw2_require_line_header (objfile, per_cu);
2163 if (!per_cu->v.quick->lines)
9291a0cd
TT
2164 continue;
2165
e254ef6a 2166 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2167 {
e254ef6a 2168 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2169
2170 if (FILENAME_CMP (name, this_name) == 0)
2171 {
e254ef6a 2172 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2173 return 1;
2174 }
2175
2176 if (check_basename && ! base_cu
2177 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2178 base_cu = per_cu;
9291a0cd
TT
2179
2180 if (full_path != NULL)
2181 {
2182 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2183 per_cu, j);
9291a0cd
TT
2184
2185 if (this_full_name
2186 && FILENAME_CMP (full_path, this_full_name) == 0)
2187 {
e254ef6a 2188 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2189 return 1;
2190 }
2191 }
2192
2193 if (real_path != NULL)
2194 {
2195 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2196 per_cu, j);
9291a0cd
TT
2197
2198 if (this_full_name != NULL)
2199 {
2200 char *rp = gdb_realpath (this_full_name);
2201 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2202 {
2203 xfree (rp);
e254ef6a 2204 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2205 return 1;
2206 }
2207 xfree (rp);
2208 }
2209 }
2210 }
2211 }
2212
2213 if (base_cu)
2214 {
2215 *result = dw2_instantiate_symtab (objfile, base_cu);
2216 return 1;
2217 }
2218
2219 return 0;
2220}
2221
2222static struct symtab *
2223dw2_lookup_symbol (struct objfile *objfile, int block_index,
2224 const char *name, domain_enum domain)
2225{
774b6a14 2226 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2227 instead. */
2228 return NULL;
2229}
2230
2231/* A helper function that expands all symtabs that hold an object
2232 named NAME. */
2fdf6df6 2233
9291a0cd
TT
2234static void
2235dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2236{
2237 dw2_setup (objfile);
2238
2239 if (dwarf2_per_objfile->index_table)
2240 {
2241 offset_type *vec;
2242
2243 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2244 name, &vec))
2245 {
2246 offset_type i, len = MAYBE_SWAP (*vec);
2247 for (i = 0; i < len; ++i)
2248 {
2249 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2250 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2251
e254ef6a 2252 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2253 }
2254 }
2255 }
2256}
2257
774b6a14
TT
2258static void
2259dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2260 int kind, const char *name,
2261 domain_enum domain)
9291a0cd 2262{
774b6a14 2263 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2264}
2265
2266static void
2267dw2_print_stats (struct objfile *objfile)
2268{
2269 int i, count;
2270
2271 dw2_setup (objfile);
2272 count = 0;
1fd400ff
TT
2273 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2274 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2275 {
e254ef6a 2276 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2277
e254ef6a 2278 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2279 ++count;
2280 }
2281 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2282}
2283
2284static void
2285dw2_dump (struct objfile *objfile)
2286{
2287 /* Nothing worth printing. */
2288}
2289
2290static void
2291dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2292 struct section_offsets *delta)
2293{
2294 /* There's nothing to relocate here. */
2295}
2296
2297static void
2298dw2_expand_symtabs_for_function (struct objfile *objfile,
2299 const char *func_name)
2300{
2301 dw2_do_expand_symtabs_matching (objfile, func_name);
2302}
2303
2304static void
2305dw2_expand_all_symtabs (struct objfile *objfile)
2306{
2307 int i;
2308
2309 dw2_setup (objfile);
1fd400ff
TT
2310
2311 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2312 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2313 {
e254ef6a 2314 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2315
e254ef6a 2316 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2317 }
2318}
2319
2320static void
2321dw2_expand_symtabs_with_filename (struct objfile *objfile,
2322 const char *filename)
2323{
2324 int i;
2325
2326 dw2_setup (objfile);
1fd400ff
TT
2327 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2329 {
2330 int j;
e254ef6a 2331 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2332
e254ef6a 2333 if (per_cu->v.quick->symtab)
9291a0cd
TT
2334 continue;
2335
e254ef6a
DE
2336 dw2_require_line_header (objfile, per_cu);
2337 if (!per_cu->v.quick->lines)
9291a0cd
TT
2338 continue;
2339
e254ef6a 2340 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2341 {
e254ef6a 2342 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2343 if (strcmp (this_name, filename) == 0)
2344 {
e254ef6a 2345 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2346 break;
2347 }
2348 }
2349 }
2350}
2351
dd786858 2352static const char *
9291a0cd
TT
2353dw2_find_symbol_file (struct objfile *objfile, const char *name)
2354{
e254ef6a 2355 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2356 offset_type *vec;
2357
2358 dw2_setup (objfile);
2359
2360 if (!dwarf2_per_objfile->index_table)
2361 return NULL;
2362
2363 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2364 name, &vec))
2365 return NULL;
2366
2367 /* Note that this just looks at the very first one named NAME -- but
2368 actually we are looking for a function. find_main_filename
2369 should be rewritten so that it doesn't require a custom hook. It
2370 could just use the ordinary symbol tables. */
2371 /* vec[0] is the length, which must always be >0. */
e254ef6a 2372 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2373
e254ef6a
DE
2374 dw2_require_line_header (objfile, per_cu);
2375 if (!per_cu->v.quick->lines)
9291a0cd
TT
2376 return NULL;
2377
e254ef6a 2378 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2379}
2380
2381static void
40658b94
PH
2382dw2_map_matching_symbols (const char * name, domain_enum namespace,
2383 struct objfile *objfile, int global,
2384 int (*callback) (struct block *,
2385 struct symbol *, void *),
2386 void *data,
2387 int (*match) (const char *, const char *),
2388 int (*ordered_compare) (const char *,
2389 const char *))
9291a0cd 2390{
40658b94 2391 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2392 current language is Ada for a non-Ada objfile using GNU index. As Ada
2393 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2394}
2395
2396static void
2397dw2_expand_symtabs_matching (struct objfile *objfile,
2398 int (*file_matcher) (const char *, void *),
2399 int (*name_matcher) (const char *, void *),
2400 domain_enum kind,
2401 void *data)
2402{
2403 int i;
2404 offset_type iter;
4b5246aa 2405 struct mapped_index *index;
9291a0cd
TT
2406
2407 dw2_setup (objfile);
2408 if (!dwarf2_per_objfile->index_table)
2409 return;
4b5246aa 2410 index = dwarf2_per_objfile->index_table;
9291a0cd 2411
1fd400ff
TT
2412 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2413 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2414 {
2415 int j;
e254ef6a 2416 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2417
e254ef6a
DE
2418 per_cu->v.quick->mark = 0;
2419 if (per_cu->v.quick->symtab)
9291a0cd
TT
2420 continue;
2421
e254ef6a
DE
2422 dw2_require_line_header (objfile, per_cu);
2423 if (!per_cu->v.quick->lines)
9291a0cd
TT
2424 continue;
2425
e254ef6a 2426 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2427 {
e254ef6a 2428 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2429 {
e254ef6a 2430 per_cu->v.quick->mark = 1;
9291a0cd
TT
2431 break;
2432 }
2433 }
2434 }
2435
3876f04e 2436 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2437 {
2438 offset_type idx = 2 * iter;
2439 const char *name;
2440 offset_type *vec, vec_len, vec_idx;
2441
3876f04e 2442 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2443 continue;
2444
3876f04e 2445 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2446
2447 if (! (*name_matcher) (name, data))
2448 continue;
2449
2450 /* The name was matched, now expand corresponding CUs that were
2451 marked. */
4b5246aa 2452 vec = (offset_type *) (index->constant_pool
3876f04e 2453 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2454 vec_len = MAYBE_SWAP (vec[0]);
2455 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2456 {
e254ef6a 2457 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2458
e254ef6a
DE
2459 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2460 if (per_cu->v.quick->mark)
2461 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2462 }
2463 }
2464}
2465
2466static struct symtab *
2467dw2_find_pc_sect_symtab (struct objfile *objfile,
2468 struct minimal_symbol *msymbol,
2469 CORE_ADDR pc,
2470 struct obj_section *section,
2471 int warn_if_readin)
2472{
2473 struct dwarf2_per_cu_data *data;
2474
2475 dw2_setup (objfile);
2476
2477 if (!objfile->psymtabs_addrmap)
2478 return NULL;
2479
2480 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2481 if (!data)
2482 return NULL;
2483
2484 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2485 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2486 paddress (get_objfile_arch (objfile), pc));
2487
2488 return dw2_instantiate_symtab (objfile, data);
2489}
2490
2491static void
2492dw2_map_symbol_names (struct objfile *objfile,
2493 void (*fun) (const char *, void *),
2494 void *data)
2495{
2496 offset_type iter;
4b5246aa
TT
2497 struct mapped_index *index;
2498
9291a0cd
TT
2499 dw2_setup (objfile);
2500
2501 if (!dwarf2_per_objfile->index_table)
2502 return;
4b5246aa 2503 index = dwarf2_per_objfile->index_table;
9291a0cd 2504
3876f04e 2505 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2506 {
2507 offset_type idx = 2 * iter;
2508 const char *name;
2509 offset_type *vec, vec_len, vec_idx;
2510
3876f04e 2511 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2512 continue;
2513
3876f04e 2514 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2515
2516 (*fun) (name, data);
2517 }
2518}
2519
2520static void
2521dw2_map_symbol_filenames (struct objfile *objfile,
2522 void (*fun) (const char *, const char *, void *),
2523 void *data)
2524{
2525 int i;
2526
2527 dw2_setup (objfile);
1fd400ff
TT
2528 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2529 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2530 {
2531 int j;
e254ef6a 2532 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2533
e254ef6a 2534 if (per_cu->v.quick->symtab)
9291a0cd
TT
2535 continue;
2536
e254ef6a
DE
2537 dw2_require_line_header (objfile, per_cu);
2538 if (!per_cu->v.quick->lines)
9291a0cd
TT
2539 continue;
2540
e254ef6a 2541 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2542 {
e254ef6a
DE
2543 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2544 j);
2545 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2546 }
2547 }
2548}
2549
2550static int
2551dw2_has_symbols (struct objfile *objfile)
2552{
2553 return 1;
2554}
2555
2556const struct quick_symbol_functions dwarf2_gdb_index_functions =
2557{
2558 dw2_has_symbols,
2559 dw2_find_last_source_symtab,
2560 dw2_forget_cached_source_info,
2561 dw2_lookup_symtab,
2562 dw2_lookup_symbol,
774b6a14 2563 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2564 dw2_print_stats,
2565 dw2_dump,
2566 dw2_relocate,
2567 dw2_expand_symtabs_for_function,
2568 dw2_expand_all_symtabs,
2569 dw2_expand_symtabs_with_filename,
2570 dw2_find_symbol_file,
40658b94 2571 dw2_map_matching_symbols,
9291a0cd
TT
2572 dw2_expand_symtabs_matching,
2573 dw2_find_pc_sect_symtab,
2574 dw2_map_symbol_names,
2575 dw2_map_symbol_filenames
2576};
2577
2578/* Initialize for reading DWARF for this objfile. Return 0 if this
2579 file will use psymtabs, or 1 if using the GNU index. */
2580
2581int
2582dwarf2_initialize_objfile (struct objfile *objfile)
2583{
2584 /* If we're about to read full symbols, don't bother with the
2585 indices. In this case we also don't care if some other debug
2586 format is making psymtabs, because they are all about to be
2587 expanded anyway. */
2588 if ((objfile->flags & OBJF_READNOW))
2589 {
2590 int i;
2591
2592 dwarf2_per_objfile->using_index = 1;
2593 create_all_comp_units (objfile);
1fd400ff 2594 create_debug_types_hash_table (objfile);
9291a0cd 2595
1fd400ff
TT
2596 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2597 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2598 {
e254ef6a 2599 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2600
e254ef6a
DE
2601 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2602 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2603 }
2604
2605 /* Return 1 so that gdb sees the "quick" functions. However,
2606 these functions will be no-ops because we will have expanded
2607 all symtabs. */
2608 return 1;
2609 }
2610
2611 if (dwarf2_read_index (objfile))
2612 return 1;
2613
2614 dwarf2_build_psymtabs (objfile);
2615 return 0;
2616}
2617
2618\f
2619
dce234bc
PP
2620/* Build a partial symbol table. */
2621
2622void
f29dff0a 2623dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2624{
f29dff0a 2625 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2626 {
2627 init_psymbol_list (objfile, 1024);
2628 }
2629
d146bf1e 2630 dwarf2_build_psymtabs_hard (objfile);
c906108c 2631}
c906108c 2632
45452591
DE
2633/* Return TRUE if OFFSET is within CU_HEADER. */
2634
2635static inline int
2636offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2637{
2638 unsigned int bottom = cu_header->offset;
2639 unsigned int top = (cu_header->offset
2640 + cu_header->length
2641 + cu_header->initial_length_size);
9a619af0 2642
45452591
DE
2643 return (offset >= bottom && offset < top);
2644}
2645
93311388
DE
2646/* Read in the comp unit header information from the debug_info at info_ptr.
2647 NOTE: This leaves members offset, first_die_offset to be filled in
2648 by the caller. */
107d2387 2649
fe1b8b76 2650static gdb_byte *
107d2387 2651read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2652 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2653{
2654 int signed_addr;
891d2f0b 2655 unsigned int bytes_read;
c764a876
DE
2656
2657 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2658 cu_header->initial_length_size = bytes_read;
2659 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2660 info_ptr += bytes_read;
107d2387
AC
2661 cu_header->version = read_2_bytes (abfd, info_ptr);
2662 info_ptr += 2;
613e1657 2663 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2664 &bytes_read);
613e1657 2665 info_ptr += bytes_read;
107d2387
AC
2666 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2667 info_ptr += 1;
2668 signed_addr = bfd_get_sign_extend_vma (abfd);
2669 if (signed_addr < 0)
8e65ff28 2670 internal_error (__FILE__, __LINE__,
e2e0b3e5 2671 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2672 cu_header->signed_addr_p = signed_addr;
c764a876 2673
107d2387
AC
2674 return info_ptr;
2675}
2676
fe1b8b76
JB
2677static gdb_byte *
2678partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2679 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2680 bfd *abfd)
2681{
fe1b8b76 2682 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2683
2684 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2685
2dc7f7b3 2686 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2687 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2688 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2689 bfd_get_filename (abfd));
72bf9492 2690
dce234bc 2691 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2692 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2693 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2694 (long) header->abbrev_offset,
93311388 2695 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2696 bfd_get_filename (abfd));
2697
2698 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2699 > buffer + buffer_size)
8a3fe4f8
AC
2700 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2701 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2702 (long) header->length,
93311388 2703 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2704 bfd_get_filename (abfd));
2705
2706 return info_ptr;
2707}
2708
348e048f
DE
2709/* Read in the types comp unit header information from .debug_types entry at
2710 types_ptr. The result is a pointer to one past the end of the header. */
2711
2712static gdb_byte *
2713read_type_comp_unit_head (struct comp_unit_head *cu_header,
2714 ULONGEST *signature,
2715 gdb_byte *types_ptr, bfd *abfd)
2716{
348e048f
DE
2717 gdb_byte *initial_types_ptr = types_ptr;
2718
6e70227d 2719 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2720 &dwarf2_per_objfile->types);
348e048f
DE
2721 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2722
2723 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2724
2725 *signature = read_8_bytes (abfd, types_ptr);
2726 types_ptr += 8;
2727 types_ptr += cu_header->offset_size;
2728 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2729
2730 return types_ptr;
2731}
2732
aaa75496
JB
2733/* Allocate a new partial symtab for file named NAME and mark this new
2734 partial symtab as being an include of PST. */
2735
2736static void
2737dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2738 struct objfile *objfile)
2739{
2740 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2741
2742 subpst->section_offsets = pst->section_offsets;
2743 subpst->textlow = 0;
2744 subpst->texthigh = 0;
2745
2746 subpst->dependencies = (struct partial_symtab **)
2747 obstack_alloc (&objfile->objfile_obstack,
2748 sizeof (struct partial_symtab *));
2749 subpst->dependencies[0] = pst;
2750 subpst->number_of_dependencies = 1;
2751
2752 subpst->globals_offset = 0;
2753 subpst->n_global_syms = 0;
2754 subpst->statics_offset = 0;
2755 subpst->n_static_syms = 0;
2756 subpst->symtab = NULL;
2757 subpst->read_symtab = pst->read_symtab;
2758 subpst->readin = 0;
2759
2760 /* No private part is necessary for include psymtabs. This property
2761 can be used to differentiate between such include psymtabs and
10b3939b 2762 the regular ones. */
58a9656e 2763 subpst->read_symtab_private = NULL;
aaa75496
JB
2764}
2765
2766/* Read the Line Number Program data and extract the list of files
2767 included by the source file represented by PST. Build an include
d85a05f0 2768 partial symtab for each of these included files. */
aaa75496
JB
2769
2770static void
2771dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2772 struct die_info *die,
aaa75496
JB
2773 struct partial_symtab *pst)
2774{
2775 struct objfile *objfile = cu->objfile;
2776 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2777 struct line_header *lh = NULL;
2778 struct attribute *attr;
aaa75496 2779
d85a05f0
DJ
2780 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2781 if (attr)
2782 {
2783 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2784
d85a05f0
DJ
2785 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2786 }
aaa75496
JB
2787 if (lh == NULL)
2788 return; /* No linetable, so no includes. */
2789
c6da4cef
DE
2790 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2791 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2792
2793 free_line_header (lh);
2794}
2795
348e048f
DE
2796static hashval_t
2797hash_type_signature (const void *item)
2798{
2799 const struct signatured_type *type_sig = item;
9a619af0 2800
348e048f
DE
2801 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2802 return type_sig->signature;
2803}
2804
2805static int
2806eq_type_signature (const void *item_lhs, const void *item_rhs)
2807{
2808 const struct signatured_type *lhs = item_lhs;
2809 const struct signatured_type *rhs = item_rhs;
9a619af0 2810
348e048f
DE
2811 return lhs->signature == rhs->signature;
2812}
2813
1fd400ff
TT
2814/* Allocate a hash table for signatured types. */
2815
2816static htab_t
673bfd45 2817allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2818{
2819 return htab_create_alloc_ex (41,
2820 hash_type_signature,
2821 eq_type_signature,
2822 NULL,
2823 &objfile->objfile_obstack,
2824 hashtab_obstack_allocate,
2825 dummy_obstack_deallocate);
2826}
2827
2828/* A helper function to add a signatured type CU to a list. */
2829
2830static int
2831add_signatured_type_cu_to_list (void **slot, void *datum)
2832{
2833 struct signatured_type *sigt = *slot;
2834 struct dwarf2_per_cu_data ***datap = datum;
2835
2836 **datap = &sigt->per_cu;
2837 ++*datap;
2838
2839 return 1;
2840}
2841
348e048f
DE
2842/* Create the hash table of all entries in the .debug_types section.
2843 The result is zero if there is an error (e.g. missing .debug_types section),
2844 otherwise non-zero. */
2845
2846static int
2847create_debug_types_hash_table (struct objfile *objfile)
2848{
be391dca 2849 gdb_byte *info_ptr;
348e048f 2850 htab_t types_htab;
1fd400ff 2851 struct dwarf2_per_cu_data **iter;
348e048f 2852
be391dca
TT
2853 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2854 info_ptr = dwarf2_per_objfile->types.buffer;
2855
348e048f
DE
2856 if (info_ptr == NULL)
2857 {
2858 dwarf2_per_objfile->signatured_types = NULL;
2859 return 0;
2860 }
2861
673bfd45 2862 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2863
2864 if (dwarf2_die_debug)
2865 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2866
2867 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2868 {
2869 unsigned int offset;
2870 unsigned int offset_size;
2871 unsigned int type_offset;
2872 unsigned int length, initial_length_size;
2873 unsigned short version;
2874 ULONGEST signature;
2875 struct signatured_type *type_sig;
2876 void **slot;
2877 gdb_byte *ptr = info_ptr;
2878
2879 offset = ptr - dwarf2_per_objfile->types.buffer;
2880
2881 /* We need to read the type's signature in order to build the hash
2882 table, but we don't need to read anything else just yet. */
2883
2884 /* Sanity check to ensure entire cu is present. */
2885 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2886 if (ptr + length + initial_length_size
2887 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2888 {
2889 complaint (&symfile_complaints,
2890 _("debug type entry runs off end of `.debug_types' section, ignored"));
2891 break;
2892 }
2893
2894 offset_size = initial_length_size == 4 ? 4 : 8;
2895 ptr += initial_length_size;
2896 version = bfd_get_16 (objfile->obfd, ptr);
2897 ptr += 2;
2898 ptr += offset_size; /* abbrev offset */
2899 ptr += 1; /* address size */
2900 signature = bfd_get_64 (objfile->obfd, ptr);
2901 ptr += 8;
2902 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2903
2904 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2905 memset (type_sig, 0, sizeof (*type_sig));
2906 type_sig->signature = signature;
2907 type_sig->offset = offset;
2908 type_sig->type_offset = type_offset;
ca1f3406 2909 type_sig->per_cu.objfile = objfile;
1fd400ff 2910 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2911
2912 slot = htab_find_slot (types_htab, type_sig, INSERT);
2913 gdb_assert (slot != NULL);
2914 *slot = type_sig;
2915
2916 if (dwarf2_die_debug)
2917 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2918 offset, phex (signature, sizeof (signature)));
2919
2920 info_ptr = info_ptr + initial_length_size + length;
2921 }
2922
2923 dwarf2_per_objfile->signatured_types = types_htab;
2924
1fd400ff
TT
2925 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2926 dwarf2_per_objfile->type_comp_units
2927 = obstack_alloc (&objfile->objfile_obstack,
2928 dwarf2_per_objfile->n_type_comp_units
2929 * sizeof (struct dwarf2_per_cu_data *));
2930 iter = &dwarf2_per_objfile->type_comp_units[0];
2931 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2932 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2933 == dwarf2_per_objfile->n_type_comp_units);
2934
348e048f
DE
2935 return 1;
2936}
2937
2938/* Lookup a signature based type.
2939 Returns NULL if SIG is not present in the table. */
2940
2941static struct signatured_type *
2942lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2943{
2944 struct signatured_type find_entry, *entry;
2945
2946 if (dwarf2_per_objfile->signatured_types == NULL)
2947 {
2948 complaint (&symfile_complaints,
2949 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2950 return 0;
2951 }
2952
2953 find_entry.signature = sig;
2954 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2955 return entry;
2956}
2957
d85a05f0
DJ
2958/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2959
2960static void
2961init_cu_die_reader (struct die_reader_specs *reader,
2962 struct dwarf2_cu *cu)
2963{
2964 reader->abfd = cu->objfile->obfd;
2965 reader->cu = cu;
2966 if (cu->per_cu->from_debug_types)
be391dca
TT
2967 {
2968 gdb_assert (dwarf2_per_objfile->types.readin);
2969 reader->buffer = dwarf2_per_objfile->types.buffer;
2970 }
d85a05f0 2971 else
be391dca
TT
2972 {
2973 gdb_assert (dwarf2_per_objfile->info.readin);
2974 reader->buffer = dwarf2_per_objfile->info.buffer;
2975 }
d85a05f0
DJ
2976}
2977
2978/* Find the base address of the compilation unit for range lists and
2979 location lists. It will normally be specified by DW_AT_low_pc.
2980 In DWARF-3 draft 4, the base address could be overridden by
2981 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2982 compilation units with discontinuous ranges. */
2983
2984static void
2985dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2986{
2987 struct attribute *attr;
2988
2989 cu->base_known = 0;
2990 cu->base_address = 0;
2991
2992 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2993 if (attr)
2994 {
2995 cu->base_address = DW_ADDR (attr);
2996 cu->base_known = 1;
2997 }
2998 else
2999 {
3000 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3001 if (attr)
3002 {
3003 cu->base_address = DW_ADDR (attr);
3004 cu->base_known = 1;
3005 }
3006 }
3007}
3008
348e048f
DE
3009/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3010 to combine the common parts.
93311388 3011 Process a compilation unit for a psymtab.
348e048f
DE
3012 BUFFER is a pointer to the beginning of the dwarf section buffer,
3013 either .debug_info or debug_types.
93311388
DE
3014 INFO_PTR is a pointer to the start of the CU.
3015 Returns a pointer to the next CU. */
aaa75496 3016
93311388
DE
3017static gdb_byte *
3018process_psymtab_comp_unit (struct objfile *objfile,
3019 struct dwarf2_per_cu_data *this_cu,
3020 gdb_byte *buffer, gdb_byte *info_ptr,
3021 unsigned int buffer_size)
c906108c 3022{
c906108c 3023 bfd *abfd = objfile->obfd;
93311388 3024 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3025 struct die_info *comp_unit_die;
c906108c 3026 struct partial_symtab *pst;
5734ee8b 3027 CORE_ADDR baseaddr;
93311388
DE
3028 struct cleanup *back_to_inner;
3029 struct dwarf2_cu cu;
d85a05f0
DJ
3030 int has_children, has_pc_info;
3031 struct attribute *attr;
d85a05f0
DJ
3032 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3033 struct die_reader_specs reader_specs;
c906108c 3034
9816fde3 3035 init_one_comp_unit (&cu, objfile);
93311388 3036 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3037
93311388
DE
3038 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3039 buffer, buffer_size,
3040 abfd);
10b3939b 3041
93311388
DE
3042 /* Complete the cu_header. */
3043 cu.header.offset = beg_of_comp_unit - buffer;
3044 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3045
93311388 3046 cu.list_in_scope = &file_symbols;
af703f96 3047
328c9494
DJ
3048 /* If this compilation unit was already read in, free the
3049 cached copy in order to read it in again. This is
3050 necessary because we skipped some symbols when we first
3051 read in the compilation unit (see load_partial_dies).
3052 This problem could be avoided, but the benefit is
3053 unclear. */
3054 if (this_cu->cu != NULL)
3055 free_one_cached_comp_unit (this_cu->cu);
3056
3057 /* Note that this is a pointer to our stack frame, being
3058 added to a global data structure. It will be cleaned up
3059 in free_stack_comp_unit when we finish with this
3060 compilation unit. */
3061 this_cu->cu = &cu;
d85a05f0
DJ
3062 cu.per_cu = this_cu;
3063
93311388
DE
3064 /* Read the abbrevs for this compilation unit into a table. */
3065 dwarf2_read_abbrevs (abfd, &cu);
3066 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3067
93311388 3068 /* Read the compilation unit die. */
348e048f
DE
3069 if (this_cu->from_debug_types)
3070 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3071 init_cu_die_reader (&reader_specs, &cu);
3072 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3073 &has_children);
93311388 3074
348e048f
DE
3075 if (this_cu->from_debug_types)
3076 {
3077 /* offset,length haven't been set yet for type units. */
3078 this_cu->offset = cu.header.offset;
3079 this_cu->length = cu.header.length + cu.header.initial_length_size;
3080 }
d85a05f0 3081 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3082 {
93311388
DE
3083 info_ptr = (beg_of_comp_unit + cu.header.length
3084 + cu.header.initial_length_size);
3085 do_cleanups (back_to_inner);
3086 return info_ptr;
3087 }
72bf9492 3088
9816fde3 3089 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3090
93311388 3091 /* Allocate a new partial symbol table structure. */
d85a05f0 3092 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3093 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3094 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3095 /* TEXTLOW and TEXTHIGH are set below. */
3096 0,
3097 objfile->global_psymbols.next,
3098 objfile->static_psymbols.next);
72bf9492 3099
d85a05f0
DJ
3100 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3101 if (attr != NULL)
3102 pst->dirname = DW_STRING (attr);
72bf9492 3103
e38df1d0 3104 pst->read_symtab_private = this_cu;
72bf9492 3105
93311388 3106 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3107
93311388
DE
3108 /* Store the function that reads in the rest of the symbol table */
3109 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3110
9291a0cd 3111 this_cu->v.psymtab = pst;
c906108c 3112
d85a05f0
DJ
3113 dwarf2_find_base_address (comp_unit_die, &cu);
3114
93311388
DE
3115 /* Possibly set the default values of LOWPC and HIGHPC from
3116 `DW_AT_ranges'. */
d85a05f0
DJ
3117 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3118 &best_highpc, &cu, pst);
3119 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3120 /* Store the contiguous range if it is not empty; it can be empty for
3121 CUs with no code. */
3122 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3123 best_lowpc + baseaddr,
3124 best_highpc + baseaddr - 1, pst);
93311388
DE
3125
3126 /* Check if comp unit has_children.
3127 If so, read the rest of the partial symbols from this comp unit.
3128 If not, there's no more debug_info for this comp unit. */
d85a05f0 3129 if (has_children)
93311388
DE
3130 {
3131 struct partial_die_info *first_die;
3132 CORE_ADDR lowpc, highpc;
31ffec48 3133
93311388
DE
3134 lowpc = ((CORE_ADDR) -1);
3135 highpc = ((CORE_ADDR) 0);
c906108c 3136
93311388 3137 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3138
93311388 3139 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3140 ! has_pc_info, &cu);
57c22c6c 3141
93311388
DE
3142 /* If we didn't find a lowpc, set it to highpc to avoid
3143 complaints from `maint check'. */
3144 if (lowpc == ((CORE_ADDR) -1))
3145 lowpc = highpc;
10b3939b 3146
93311388
DE
3147 /* If the compilation unit didn't have an explicit address range,
3148 then use the information extracted from its child dies. */
d85a05f0 3149 if (! has_pc_info)
93311388 3150 {
d85a05f0
DJ
3151 best_lowpc = lowpc;
3152 best_highpc = highpc;
93311388
DE
3153 }
3154 }
d85a05f0
DJ
3155 pst->textlow = best_lowpc + baseaddr;
3156 pst->texthigh = best_highpc + baseaddr;
c906108c 3157
93311388
DE
3158 pst->n_global_syms = objfile->global_psymbols.next -
3159 (objfile->global_psymbols.list + pst->globals_offset);
3160 pst->n_static_syms = objfile->static_psymbols.next -
3161 (objfile->static_psymbols.list + pst->statics_offset);
3162 sort_pst_symbols (pst);
c906108c 3163
93311388
DE
3164 info_ptr = (beg_of_comp_unit + cu.header.length
3165 + cu.header.initial_length_size);
ae038cb0 3166
348e048f
DE
3167 if (this_cu->from_debug_types)
3168 {
3169 /* It's not clear we want to do anything with stmt lists here.
3170 Waiting to see what gcc ultimately does. */
3171 }
d85a05f0 3172 else
93311388
DE
3173 {
3174 /* Get the list of files included in the current compilation unit,
3175 and build a psymtab for each of them. */
d85a05f0 3176 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3177 }
ae038cb0 3178
93311388 3179 do_cleanups (back_to_inner);
ae038cb0 3180
93311388
DE
3181 return info_ptr;
3182}
ff013f42 3183
348e048f
DE
3184/* Traversal function for htab_traverse_noresize.
3185 Process one .debug_types comp-unit. */
3186
3187static int
3188process_type_comp_unit (void **slot, void *info)
3189{
3190 struct signatured_type *entry = (struct signatured_type *) *slot;
3191 struct objfile *objfile = (struct objfile *) info;
3192 struct dwarf2_per_cu_data *this_cu;
3193
3194 this_cu = &entry->per_cu;
348e048f 3195
be391dca 3196 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3197 process_psymtab_comp_unit (objfile, this_cu,
3198 dwarf2_per_objfile->types.buffer,
3199 dwarf2_per_objfile->types.buffer + entry->offset,
3200 dwarf2_per_objfile->types.size);
3201
3202 return 1;
3203}
3204
3205/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3206 Build partial symbol tables for the .debug_types comp-units. */
3207
3208static void
3209build_type_psymtabs (struct objfile *objfile)
3210{
3211 if (! create_debug_types_hash_table (objfile))
3212 return;
3213
3214 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3215 process_type_comp_unit, objfile);
3216}
3217
60606b2c
TT
3218/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3219
3220static void
3221psymtabs_addrmap_cleanup (void *o)
3222{
3223 struct objfile *objfile = o;
ec61707d 3224
60606b2c
TT
3225 objfile->psymtabs_addrmap = NULL;
3226}
3227
93311388
DE
3228/* Build the partial symbol table by doing a quick pass through the
3229 .debug_info and .debug_abbrev sections. */
72bf9492 3230
93311388 3231static void
c67a9c90 3232dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3233{
93311388 3234 gdb_byte *info_ptr;
60606b2c
TT
3235 struct cleanup *back_to, *addrmap_cleanup;
3236 struct obstack temp_obstack;
93311388 3237
98bfdba5
PA
3238 dwarf2_per_objfile->reading_partial_symbols = 1;
3239
be391dca 3240 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3241 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3242
93311388
DE
3243 /* Any cached compilation units will be linked by the per-objfile
3244 read_in_chain. Make sure to free them when we're done. */
3245 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3246
348e048f
DE
3247 build_type_psymtabs (objfile);
3248
93311388 3249 create_all_comp_units (objfile);
c906108c 3250
60606b2c
TT
3251 /* Create a temporary address map on a temporary obstack. We later
3252 copy this to the final obstack. */
3253 obstack_init (&temp_obstack);
3254 make_cleanup_obstack_free (&temp_obstack);
3255 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3256 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3257
93311388
DE
3258 /* Since the objects we're extracting from .debug_info vary in
3259 length, only the individual functions to extract them (like
3260 read_comp_unit_head and load_partial_die) can really know whether
3261 the buffer is large enough to hold another complete object.
c906108c 3262
93311388
DE
3263 At the moment, they don't actually check that. If .debug_info
3264 holds just one extra byte after the last compilation unit's dies,
3265 then read_comp_unit_head will happily read off the end of the
3266 buffer. read_partial_die is similarly casual. Those functions
3267 should be fixed.
c906108c 3268
93311388
DE
3269 For this loop condition, simply checking whether there's any data
3270 left at all should be sufficient. */
c906108c 3271
93311388
DE
3272 while (info_ptr < (dwarf2_per_objfile->info.buffer
3273 + dwarf2_per_objfile->info.size))
3274 {
3275 struct dwarf2_per_cu_data *this_cu;
dd373385 3276
93311388
DE
3277 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3278 objfile);
aaa75496 3279
93311388
DE
3280 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3281 dwarf2_per_objfile->info.buffer,
3282 info_ptr,
3283 dwarf2_per_objfile->info.size);
c906108c 3284 }
ff013f42
JK
3285
3286 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3287 &objfile->objfile_obstack);
60606b2c 3288 discard_cleanups (addrmap_cleanup);
ff013f42 3289
ae038cb0
DJ
3290 do_cleanups (back_to);
3291}
3292
93311388 3293/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3294
3295static void
93311388
DE
3296load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3297 struct objfile *objfile)
ae038cb0
DJ
3298{
3299 bfd *abfd = objfile->obfd;
fe1b8b76 3300 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3301 struct die_info *comp_unit_die;
ae038cb0 3302 struct dwarf2_cu *cu;
1d9ec526 3303 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3304 int has_children;
3305 struct die_reader_specs reader_specs;
98bfdba5 3306 int read_cu = 0;
ae038cb0 3307
348e048f
DE
3308 gdb_assert (! this_cu->from_debug_types);
3309
be391dca 3310 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3311 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3312 beg_of_comp_unit = info_ptr;
3313
98bfdba5
PA
3314 if (this_cu->cu == NULL)
3315 {
9816fde3
JK
3316 cu = xmalloc (sizeof (*cu));
3317 init_one_comp_unit (cu, objfile);
ae038cb0 3318
98bfdba5 3319 read_cu = 1;
ae038cb0 3320
98bfdba5
PA
3321 /* If an error occurs while loading, release our storage. */
3322 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3323
98bfdba5
PA
3324 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3325 dwarf2_per_objfile->info.buffer,
3326 dwarf2_per_objfile->info.size,
3327 abfd);
ae038cb0 3328
98bfdba5
PA
3329 /* Complete the cu_header. */
3330 cu->header.offset = this_cu->offset;
3331 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3332
3333 /* Link this compilation unit into the compilation unit tree. */
3334 this_cu->cu = cu;
3335 cu->per_cu = this_cu;
98bfdba5
PA
3336
3337 /* Link this CU into read_in_chain. */
3338 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3339 dwarf2_per_objfile->read_in_chain = this_cu;
3340 }
3341 else
3342 {
3343 cu = this_cu->cu;
3344 info_ptr += cu->header.first_die_offset;
3345 }
ae038cb0
DJ
3346
3347 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3348 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3349 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3350 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3351
3352 /* Read the compilation unit die. */
d85a05f0
DJ
3353 init_cu_die_reader (&reader_specs, cu);
3354 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3355 &has_children);
ae038cb0 3356
9816fde3 3357 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3358
ae038cb0
DJ
3359 /* Check if comp unit has_children.
3360 If so, read the rest of the partial symbols from this comp unit.
3361 If not, there's no more debug_info for this comp unit. */
d85a05f0 3362 if (has_children)
93311388 3363 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3364
98bfdba5
PA
3365 do_cleanups (free_abbrevs_cleanup);
3366
3367 if (read_cu)
3368 {
3369 /* We've successfully allocated this compilation unit. Let our
3370 caller clean it up when finished with it. */
3371 discard_cleanups (free_cu_cleanup);
3372 }
ae038cb0
DJ
3373}
3374
3375/* Create a list of all compilation units in OBJFILE. We do this only
3376 if an inter-comp-unit reference is found; presumably if there is one,
3377 there will be many, and one will occur early in the .debug_info section.
3378 So there's no point in building this list incrementally. */
3379
3380static void
3381create_all_comp_units (struct objfile *objfile)
3382{
3383 int n_allocated;
3384 int n_comp_units;
3385 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3386 gdb_byte *info_ptr;
3387
3388 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3389 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3390
3391 n_comp_units = 0;
3392 n_allocated = 10;
3393 all_comp_units = xmalloc (n_allocated
3394 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3395
dce234bc 3396 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3397 {
c764a876 3398 unsigned int length, initial_length_size;
ae038cb0 3399 struct dwarf2_per_cu_data *this_cu;
c764a876 3400 unsigned int offset;
ae038cb0 3401
dce234bc 3402 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3403
3404 /* Read just enough information to find out where the next
3405 compilation unit is. */
c764a876
DE
3406 length = read_initial_length (objfile->obfd, info_ptr,
3407 &initial_length_size);
ae038cb0
DJ
3408
3409 /* Save the compilation unit for later lookup. */
3410 this_cu = obstack_alloc (&objfile->objfile_obstack,
3411 sizeof (struct dwarf2_per_cu_data));
3412 memset (this_cu, 0, sizeof (*this_cu));
3413 this_cu->offset = offset;
c764a876 3414 this_cu->length = length + initial_length_size;
9291a0cd 3415 this_cu->objfile = objfile;
ae038cb0
DJ
3416
3417 if (n_comp_units == n_allocated)
3418 {
3419 n_allocated *= 2;
3420 all_comp_units = xrealloc (all_comp_units,
3421 n_allocated
3422 * sizeof (struct dwarf2_per_cu_data *));
3423 }
3424 all_comp_units[n_comp_units++] = this_cu;
3425
3426 info_ptr = info_ptr + this_cu->length;
3427 }
3428
3429 dwarf2_per_objfile->all_comp_units
3430 = obstack_alloc (&objfile->objfile_obstack,
3431 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3432 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3433 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3434 xfree (all_comp_units);
3435 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3436}
3437
5734ee8b
DJ
3438/* Process all loaded DIEs for compilation unit CU, starting at
3439 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3440 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3441 DW_AT_ranges). If NEED_PC is set, then this function will set
3442 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3443 and record the covered ranges in the addrmap. */
c906108c 3444
72bf9492
DJ
3445static void
3446scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3447 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3448{
72bf9492 3449 struct partial_die_info *pdi;
c906108c 3450
91c24f0a
DC
3451 /* Now, march along the PDI's, descending into ones which have
3452 interesting children but skipping the children of the other ones,
3453 until we reach the end of the compilation unit. */
c906108c 3454
72bf9492 3455 pdi = first_die;
91c24f0a 3456
72bf9492
DJ
3457 while (pdi != NULL)
3458 {
3459 fixup_partial_die (pdi, cu);
c906108c 3460
f55ee35c 3461 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3462 children, so we need to look at them. Ditto for anonymous
3463 enums. */
933c6fe4 3464
72bf9492 3465 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3466 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3467 {
72bf9492 3468 switch (pdi->tag)
c906108c
SS
3469 {
3470 case DW_TAG_subprogram:
5734ee8b 3471 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3472 break;
72929c62 3473 case DW_TAG_constant:
c906108c
SS
3474 case DW_TAG_variable:
3475 case DW_TAG_typedef:
91c24f0a 3476 case DW_TAG_union_type:
72bf9492 3477 if (!pdi->is_declaration)
63d06c5c 3478 {
72bf9492 3479 add_partial_symbol (pdi, cu);
63d06c5c
DC
3480 }
3481 break;
c906108c 3482 case DW_TAG_class_type:
680b30c7 3483 case DW_TAG_interface_type:
c906108c 3484 case DW_TAG_structure_type:
72bf9492 3485 if (!pdi->is_declaration)
c906108c 3486 {
72bf9492 3487 add_partial_symbol (pdi, cu);
c906108c
SS
3488 }
3489 break;
91c24f0a 3490 case DW_TAG_enumeration_type:
72bf9492
DJ
3491 if (!pdi->is_declaration)
3492 add_partial_enumeration (pdi, cu);
c906108c
SS
3493 break;
3494 case DW_TAG_base_type:
a02abb62 3495 case DW_TAG_subrange_type:
c906108c 3496 /* File scope base type definitions are added to the partial
c5aa993b 3497 symbol table. */
72bf9492 3498 add_partial_symbol (pdi, cu);
c906108c 3499 break;
d9fa45fe 3500 case DW_TAG_namespace:
5734ee8b 3501 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3502 break;
5d7cb8df
JK
3503 case DW_TAG_module:
3504 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3505 break;
c906108c
SS
3506 default:
3507 break;
3508 }
3509 }
3510
72bf9492
DJ
3511 /* If the die has a sibling, skip to the sibling. */
3512
3513 pdi = pdi->die_sibling;
3514 }
3515}
3516
3517/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3518
72bf9492 3519 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3520 name is concatenated with "::" and the partial DIE's name. For
3521 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3522 Enumerators are an exception; they use the scope of their parent
3523 enumeration type, i.e. the name of the enumeration type is not
3524 prepended to the enumerator.
91c24f0a 3525
72bf9492
DJ
3526 There are two complexities. One is DW_AT_specification; in this
3527 case "parent" means the parent of the target of the specification,
3528 instead of the direct parent of the DIE. The other is compilers
3529 which do not emit DW_TAG_namespace; in this case we try to guess
3530 the fully qualified name of structure types from their members'
3531 linkage names. This must be done using the DIE's children rather
3532 than the children of any DW_AT_specification target. We only need
3533 to do this for structures at the top level, i.e. if the target of
3534 any DW_AT_specification (if any; otherwise the DIE itself) does not
3535 have a parent. */
3536
3537/* Compute the scope prefix associated with PDI's parent, in
3538 compilation unit CU. The result will be allocated on CU's
3539 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3540 field. NULL is returned if no prefix is necessary. */
3541static char *
3542partial_die_parent_scope (struct partial_die_info *pdi,
3543 struct dwarf2_cu *cu)
3544{
3545 char *grandparent_scope;
3546 struct partial_die_info *parent, *real_pdi;
91c24f0a 3547
72bf9492
DJ
3548 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3549 then this means the parent of the specification DIE. */
3550
3551 real_pdi = pdi;
72bf9492 3552 while (real_pdi->has_specification)
10b3939b 3553 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3554
3555 parent = real_pdi->die_parent;
3556 if (parent == NULL)
3557 return NULL;
3558
3559 if (parent->scope_set)
3560 return parent->scope;
3561
3562 fixup_partial_die (parent, cu);
3563
10b3939b 3564 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3565
acebe513
UW
3566 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3567 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3568 Work around this problem here. */
3569 if (cu->language == language_cplus
6e70227d 3570 && parent->tag == DW_TAG_namespace
acebe513
UW
3571 && strcmp (parent->name, "::") == 0
3572 && grandparent_scope == NULL)
3573 {
3574 parent->scope = NULL;
3575 parent->scope_set = 1;
3576 return NULL;
3577 }
3578
72bf9492 3579 if (parent->tag == DW_TAG_namespace
f55ee35c 3580 || parent->tag == DW_TAG_module
72bf9492
DJ
3581 || parent->tag == DW_TAG_structure_type
3582 || parent->tag == DW_TAG_class_type
680b30c7 3583 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3584 || parent->tag == DW_TAG_union_type
3585 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3586 {
3587 if (grandparent_scope == NULL)
3588 parent->scope = parent->name;
3589 else
987504bb 3590 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3591 parent->name, 0, cu);
72bf9492 3592 }
ceeb3d5a 3593 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3594 /* Enumerators should not get the name of the enumeration as a prefix. */
3595 parent->scope = grandparent_scope;
3596 else
3597 {
3598 /* FIXME drow/2004-04-01: What should we be doing with
3599 function-local names? For partial symbols, we should probably be
3600 ignoring them. */
3601 complaint (&symfile_complaints,
e2e0b3e5 3602 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3603 parent->tag, pdi->offset);
3604 parent->scope = grandparent_scope;
c906108c
SS
3605 }
3606
72bf9492
DJ
3607 parent->scope_set = 1;
3608 return parent->scope;
3609}
3610
3611/* Return the fully scoped name associated with PDI, from compilation unit
3612 CU. The result will be allocated with malloc. */
3613static char *
3614partial_die_full_name (struct partial_die_info *pdi,
3615 struct dwarf2_cu *cu)
3616{
3617 char *parent_scope;
3618
98bfdba5
PA
3619 /* If this is a template instantiation, we can not work out the
3620 template arguments from partial DIEs. So, unfortunately, we have
3621 to go through the full DIEs. At least any work we do building
3622 types here will be reused if full symbols are loaded later. */
3623 if (pdi->has_template_arguments)
3624 {
3625 fixup_partial_die (pdi, cu);
3626
3627 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3628 {
3629 struct die_info *die;
3630 struct attribute attr;
3631 struct dwarf2_cu *ref_cu = cu;
3632
3633 attr.name = 0;
3634 attr.form = DW_FORM_ref_addr;
3635 attr.u.addr = pdi->offset;
3636 die = follow_die_ref (NULL, &attr, &ref_cu);
3637
3638 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3639 }
3640 }
3641
72bf9492
DJ
3642 parent_scope = partial_die_parent_scope (pdi, cu);
3643 if (parent_scope == NULL)
3644 return NULL;
3645 else
f55ee35c 3646 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3647}
3648
3649static void
72bf9492 3650add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3651{
e7c27a73 3652 struct objfile *objfile = cu->objfile;
c906108c 3653 CORE_ADDR addr = 0;
decbce07 3654 char *actual_name = NULL;
5c4e30ca 3655 const struct partial_symbol *psym = NULL;
e142c38c 3656 CORE_ADDR baseaddr;
72bf9492 3657 int built_actual_name = 0;
e142c38c
DJ
3658
3659 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3660
94af9270
KS
3661 actual_name = partial_die_full_name (pdi, cu);
3662 if (actual_name)
3663 built_actual_name = 1;
63d06c5c 3664
72bf9492
DJ
3665 if (actual_name == NULL)
3666 actual_name = pdi->name;
3667
c906108c
SS
3668 switch (pdi->tag)
3669 {
3670 case DW_TAG_subprogram:
2cfa0c8d 3671 if (pdi->is_external || cu->language == language_ada)
c906108c 3672 {
2cfa0c8d
JB
3673 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3674 of the global scope. But in Ada, we want to be able to access
3675 nested procedures globally. So all Ada subprograms are stored
3676 in the global scope. */
38d518c9 3677 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3678 mst_text, objfile); */
38d518c9 3679 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3680 built_actual_name,
5c4e30ca
DC
3681 VAR_DOMAIN, LOC_BLOCK,
3682 &objfile->global_psymbols,
3683 0, pdi->lowpc + baseaddr,
e142c38c 3684 cu->language, objfile);
c906108c
SS
3685 }
3686 else
3687 {
38d518c9 3688 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3689 mst_file_text, objfile); */
38d518c9 3690 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3691 built_actual_name,
5c4e30ca
DC
3692 VAR_DOMAIN, LOC_BLOCK,
3693 &objfile->static_psymbols,
3694 0, pdi->lowpc + baseaddr,
e142c38c 3695 cu->language, objfile);
c906108c
SS
3696 }
3697 break;
72929c62
JB
3698 case DW_TAG_constant:
3699 {
3700 struct psymbol_allocation_list *list;
3701
3702 if (pdi->is_external)
3703 list = &objfile->global_psymbols;
3704 else
3705 list = &objfile->static_psymbols;
3706 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3707 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3708 list, 0, 0, cu->language, objfile);
3709
3710 }
3711 break;
c906108c 3712 case DW_TAG_variable:
caac4577
JG
3713 if (pdi->locdesc)
3714 addr = decode_locdesc (pdi->locdesc, cu);
3715
3716 if (pdi->locdesc
3717 && addr == 0
3718 && !dwarf2_per_objfile->has_section_at_zero)
3719 {
3720 /* A global or static variable may also have been stripped
3721 out by the linker if unused, in which case its address
3722 will be nullified; do not add such variables into partial
3723 symbol table then. */
3724 }
3725 else if (pdi->is_external)
c906108c
SS
3726 {
3727 /* Global Variable.
3728 Don't enter into the minimal symbol tables as there is
3729 a minimal symbol table entry from the ELF symbols already.
3730 Enter into partial symbol table if it has a location
3731 descriptor or a type.
3732 If the location descriptor is missing, new_symbol will create
3733 a LOC_UNRESOLVED symbol, the address of the variable will then
3734 be determined from the minimal symbol table whenever the variable
3735 is referenced.
3736 The address for the partial symbol table entry is not
3737 used by GDB, but it comes in handy for debugging partial symbol
3738 table building. */
3739
c906108c 3740 if (pdi->locdesc || pdi->has_type)
38d518c9 3741 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3742 built_actual_name,
5c4e30ca
DC
3743 VAR_DOMAIN, LOC_STATIC,
3744 &objfile->global_psymbols,
3745 0, addr + baseaddr,
e142c38c 3746 cu->language, objfile);
c906108c
SS
3747 }
3748 else
3749 {
3750 /* Static Variable. Skip symbols without location descriptors. */
3751 if (pdi->locdesc == NULL)
decbce07
MS
3752 {
3753 if (built_actual_name)
3754 xfree (actual_name);
3755 return;
3756 }
38d518c9 3757 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3758 mst_file_data, objfile); */
38d518c9 3759 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3760 built_actual_name,
5c4e30ca
DC
3761 VAR_DOMAIN, LOC_STATIC,
3762 &objfile->static_psymbols,
3763 0, addr + baseaddr,
e142c38c 3764 cu->language, objfile);
c906108c
SS
3765 }
3766 break;
3767 case DW_TAG_typedef:
3768 case DW_TAG_base_type:
a02abb62 3769 case DW_TAG_subrange_type:
38d518c9 3770 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3771 built_actual_name,
176620f1 3772 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3773 &objfile->static_psymbols,
e142c38c 3774 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3775 break;
72bf9492
DJ
3776 case DW_TAG_namespace:
3777 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3778 built_actual_name,
72bf9492
DJ
3779 VAR_DOMAIN, LOC_TYPEDEF,
3780 &objfile->global_psymbols,
3781 0, (CORE_ADDR) 0, cu->language, objfile);
3782 break;
c906108c 3783 case DW_TAG_class_type:
680b30c7 3784 case DW_TAG_interface_type:
c906108c
SS
3785 case DW_TAG_structure_type:
3786 case DW_TAG_union_type:
3787 case DW_TAG_enumeration_type:
fa4028e9
JB
3788 /* Skip external references. The DWARF standard says in the section
3789 about "Structure, Union, and Class Type Entries": "An incomplete
3790 structure, union or class type is represented by a structure,
3791 union or class entry that does not have a byte size attribute
3792 and that has a DW_AT_declaration attribute." */
3793 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3794 {
3795 if (built_actual_name)
3796 xfree (actual_name);
3797 return;
3798 }
fa4028e9 3799
63d06c5c
DC
3800 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3801 static vs. global. */
38d518c9 3802 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3803 built_actual_name,
176620f1 3804 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3805 (cu->language == language_cplus
3806 || cu->language == language_java)
63d06c5c
DC
3807 ? &objfile->global_psymbols
3808 : &objfile->static_psymbols,
e142c38c 3809 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3810
c906108c
SS
3811 break;
3812 case DW_TAG_enumerator:
38d518c9 3813 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3814 built_actual_name,
176620f1 3815 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3816 (cu->language == language_cplus
3817 || cu->language == language_java)
f6fe98ef
DJ
3818 ? &objfile->global_psymbols
3819 : &objfile->static_psymbols,
e142c38c 3820 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3821 break;
3822 default:
3823 break;
3824 }
5c4e30ca 3825
72bf9492
DJ
3826 if (built_actual_name)
3827 xfree (actual_name);
c906108c
SS
3828}
3829
5c4e30ca
DC
3830/* Read a partial die corresponding to a namespace; also, add a symbol
3831 corresponding to that namespace to the symbol table. NAMESPACE is
3832 the name of the enclosing namespace. */
91c24f0a 3833
72bf9492
DJ
3834static void
3835add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3836 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3837 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3838{
72bf9492 3839 /* Add a symbol for the namespace. */
e7c27a73 3840
72bf9492 3841 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3842
3843 /* Now scan partial symbols in that namespace. */
3844
91c24f0a 3845 if (pdi->has_children)
5734ee8b 3846 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3847}
3848
5d7cb8df
JK
3849/* Read a partial die corresponding to a Fortran module. */
3850
3851static void
3852add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3853 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3854{
f55ee35c 3855 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3856
3857 if (pdi->has_children)
3858 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3859}
3860
bc30ff58
JB
3861/* Read a partial die corresponding to a subprogram and create a partial
3862 symbol for that subprogram. When the CU language allows it, this
3863 routine also defines a partial symbol for each nested subprogram
3864 that this subprogram contains.
6e70227d 3865
bc30ff58
JB
3866 DIE my also be a lexical block, in which case we simply search
3867 recursively for suprograms defined inside that lexical block.
3868 Again, this is only performed when the CU language allows this
3869 type of definitions. */
3870
3871static void
3872add_partial_subprogram (struct partial_die_info *pdi,
3873 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3874 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3875{
3876 if (pdi->tag == DW_TAG_subprogram)
3877 {
3878 if (pdi->has_pc_info)
3879 {
3880 if (pdi->lowpc < *lowpc)
3881 *lowpc = pdi->lowpc;
3882 if (pdi->highpc > *highpc)
3883 *highpc = pdi->highpc;
5734ee8b
DJ
3884 if (need_pc)
3885 {
3886 CORE_ADDR baseaddr;
3887 struct objfile *objfile = cu->objfile;
3888
3889 baseaddr = ANOFFSET (objfile->section_offsets,
3890 SECT_OFF_TEXT (objfile));
3891 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3892 pdi->lowpc + baseaddr,
3893 pdi->highpc - 1 + baseaddr,
9291a0cd 3894 cu->per_cu->v.psymtab);
5734ee8b 3895 }
bc30ff58 3896 if (!pdi->is_declaration)
e8d05480
JB
3897 /* Ignore subprogram DIEs that do not have a name, they are
3898 illegal. Do not emit a complaint at this point, we will
3899 do so when we convert this psymtab into a symtab. */
3900 if (pdi->name)
3901 add_partial_symbol (pdi, cu);
bc30ff58
JB
3902 }
3903 }
6e70227d 3904
bc30ff58
JB
3905 if (! pdi->has_children)
3906 return;
3907
3908 if (cu->language == language_ada)
3909 {
3910 pdi = pdi->die_child;
3911 while (pdi != NULL)
3912 {
3913 fixup_partial_die (pdi, cu);
3914 if (pdi->tag == DW_TAG_subprogram
3915 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3916 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3917 pdi = pdi->die_sibling;
3918 }
3919 }
3920}
3921
91c24f0a
DC
3922/* Read a partial die corresponding to an enumeration type. */
3923
72bf9492
DJ
3924static void
3925add_partial_enumeration (struct partial_die_info *enum_pdi,
3926 struct dwarf2_cu *cu)
91c24f0a 3927{
72bf9492 3928 struct partial_die_info *pdi;
91c24f0a
DC
3929
3930 if (enum_pdi->name != NULL)
72bf9492
DJ
3931 add_partial_symbol (enum_pdi, cu);
3932
3933 pdi = enum_pdi->die_child;
3934 while (pdi)
91c24f0a 3935 {
72bf9492 3936 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3937 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3938 else
72bf9492
DJ
3939 add_partial_symbol (pdi, cu);
3940 pdi = pdi->die_sibling;
91c24f0a 3941 }
91c24f0a
DC
3942}
3943
4bb7a0a7
DJ
3944/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3945 Return the corresponding abbrev, or NULL if the number is zero (indicating
3946 an empty DIE). In either case *BYTES_READ will be set to the length of
3947 the initial number. */
3948
3949static struct abbrev_info *
fe1b8b76 3950peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3951 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3952{
3953 bfd *abfd = cu->objfile->obfd;
3954 unsigned int abbrev_number;
3955 struct abbrev_info *abbrev;
3956
3957 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3958
3959 if (abbrev_number == 0)
3960 return NULL;
3961
3962 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3963 if (!abbrev)
3964 {
8a3fe4f8 3965 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3966 bfd_get_filename (abfd));
3967 }
3968
3969 return abbrev;
3970}
3971
93311388
DE
3972/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3973 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3974 DIE. Any children of the skipped DIEs will also be skipped. */
3975
fe1b8b76 3976static gdb_byte *
93311388 3977skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3978{
3979 struct abbrev_info *abbrev;
3980 unsigned int bytes_read;
3981
3982 while (1)
3983 {
3984 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3985 if (abbrev == NULL)
3986 return info_ptr + bytes_read;
3987 else
93311388 3988 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3989 }
3990}
3991
93311388
DE
3992/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3993 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3994 abbrev corresponding to that skipped uleb128 should be passed in
3995 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3996 children. */
3997
fe1b8b76 3998static gdb_byte *
93311388
DE
3999skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4000 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4001{
4002 unsigned int bytes_read;
4003 struct attribute attr;
4004 bfd *abfd = cu->objfile->obfd;
4005 unsigned int form, i;
4006
4007 for (i = 0; i < abbrev->num_attrs; i++)
4008 {
4009 /* The only abbrev we care about is DW_AT_sibling. */
4010 if (abbrev->attrs[i].name == DW_AT_sibling)
4011 {
4012 read_attribute (&attr, &abbrev->attrs[i],
4013 abfd, info_ptr, cu);
4014 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4015 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4016 else
93311388 4017 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4018 }
4019
4020 /* If it isn't DW_AT_sibling, skip this attribute. */
4021 form = abbrev->attrs[i].form;
4022 skip_attribute:
4023 switch (form)
4024 {
4bb7a0a7 4025 case DW_FORM_ref_addr:
ae411497
TT
4026 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4027 and later it is offset sized. */
4028 if (cu->header.version == 2)
4029 info_ptr += cu->header.addr_size;
4030 else
4031 info_ptr += cu->header.offset_size;
4032 break;
4033 case DW_FORM_addr:
4bb7a0a7
DJ
4034 info_ptr += cu->header.addr_size;
4035 break;
4036 case DW_FORM_data1:
4037 case DW_FORM_ref1:
4038 case DW_FORM_flag:
4039 info_ptr += 1;
4040 break;
2dc7f7b3
TT
4041 case DW_FORM_flag_present:
4042 break;
4bb7a0a7
DJ
4043 case DW_FORM_data2:
4044 case DW_FORM_ref2:
4045 info_ptr += 2;
4046 break;
4047 case DW_FORM_data4:
4048 case DW_FORM_ref4:
4049 info_ptr += 4;
4050 break;
4051 case DW_FORM_data8:
4052 case DW_FORM_ref8:
348e048f 4053 case DW_FORM_sig8:
4bb7a0a7
DJ
4054 info_ptr += 8;
4055 break;
4056 case DW_FORM_string:
9b1c24c8 4057 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4058 info_ptr += bytes_read;
4059 break;
2dc7f7b3 4060 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4061 case DW_FORM_strp:
4062 info_ptr += cu->header.offset_size;
4063 break;
2dc7f7b3 4064 case DW_FORM_exprloc:
4bb7a0a7
DJ
4065 case DW_FORM_block:
4066 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4067 info_ptr += bytes_read;
4068 break;
4069 case DW_FORM_block1:
4070 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4071 break;
4072 case DW_FORM_block2:
4073 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4074 break;
4075 case DW_FORM_block4:
4076 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4077 break;
4078 case DW_FORM_sdata:
4079 case DW_FORM_udata:
4080 case DW_FORM_ref_udata:
4081 info_ptr = skip_leb128 (abfd, info_ptr);
4082 break;
4083 case DW_FORM_indirect:
4084 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4085 info_ptr += bytes_read;
4086 /* We need to continue parsing from here, so just go back to
4087 the top. */
4088 goto skip_attribute;
4089
4090 default:
8a3fe4f8 4091 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4092 dwarf_form_name (form),
4093 bfd_get_filename (abfd));
4094 }
4095 }
4096
4097 if (abbrev->has_children)
93311388 4098 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4099 else
4100 return info_ptr;
4101}
4102
93311388
DE
4103/* Locate ORIG_PDI's sibling.
4104 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4105 in BUFFER. */
91c24f0a 4106
fe1b8b76 4107static gdb_byte *
93311388
DE
4108locate_pdi_sibling (struct partial_die_info *orig_pdi,
4109 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4110 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4111{
4112 /* Do we know the sibling already? */
72bf9492 4113
91c24f0a
DC
4114 if (orig_pdi->sibling)
4115 return orig_pdi->sibling;
4116
4117 /* Are there any children to deal with? */
4118
4119 if (!orig_pdi->has_children)
4120 return info_ptr;
4121
4bb7a0a7 4122 /* Skip the children the long way. */
91c24f0a 4123
93311388 4124 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4125}
4126
c906108c
SS
4127/* Expand this partial symbol table into a full symbol table. */
4128
4129static void
fba45db2 4130dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4131{
c906108c
SS
4132 if (pst != NULL)
4133 {
4134 if (pst->readin)
4135 {
8a3fe4f8 4136 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4137 }
4138 else
4139 {
4140 if (info_verbose)
4141 {
a3f17187 4142 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4143 gdb_flush (gdb_stdout);
4144 }
4145
10b3939b
DJ
4146 /* Restore our global data. */
4147 dwarf2_per_objfile = objfile_data (pst->objfile,
4148 dwarf2_objfile_data_key);
4149
b2ab525c
KB
4150 /* If this psymtab is constructed from a debug-only objfile, the
4151 has_section_at_zero flag will not necessarily be correct. We
4152 can get the correct value for this flag by looking at the data
4153 associated with the (presumably stripped) associated objfile. */
4154 if (pst->objfile->separate_debug_objfile_backlink)
4155 {
4156 struct dwarf2_per_objfile *dpo_backlink
4157 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4158 dwarf2_objfile_data_key);
9a619af0 4159
b2ab525c
KB
4160 dwarf2_per_objfile->has_section_at_zero
4161 = dpo_backlink->has_section_at_zero;
4162 }
4163
98bfdba5
PA
4164 dwarf2_per_objfile->reading_partial_symbols = 0;
4165
c906108c
SS
4166 psymtab_to_symtab_1 (pst);
4167
4168 /* Finish up the debug error message. */
4169 if (info_verbose)
a3f17187 4170 printf_filtered (_("done.\n"));
c906108c
SS
4171 }
4172 }
4173}
4174
10b3939b
DJ
4175/* Add PER_CU to the queue. */
4176
4177static void
03dd20cc 4178queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4179{
4180 struct dwarf2_queue_item *item;
4181
4182 per_cu->queued = 1;
4183 item = xmalloc (sizeof (*item));
4184 item->per_cu = per_cu;
4185 item->next = NULL;
4186
4187 if (dwarf2_queue == NULL)
4188 dwarf2_queue = item;
4189 else
4190 dwarf2_queue_tail->next = item;
4191
4192 dwarf2_queue_tail = item;
4193}
4194
4195/* Process the queue. */
4196
4197static void
4198process_queue (struct objfile *objfile)
4199{
4200 struct dwarf2_queue_item *item, *next_item;
4201
03dd20cc
DJ
4202 /* The queue starts out with one item, but following a DIE reference
4203 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4204 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4205 {
9291a0cd
TT
4206 if (dwarf2_per_objfile->using_index
4207 ? !item->per_cu->v.quick->symtab
4208 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4209 process_full_comp_unit (item->per_cu);
4210
4211 item->per_cu->queued = 0;
4212 next_item = item->next;
4213 xfree (item);
4214 }
4215
4216 dwarf2_queue_tail = NULL;
4217}
4218
4219/* Free all allocated queue entries. This function only releases anything if
4220 an error was thrown; if the queue was processed then it would have been
4221 freed as we went along. */
4222
4223static void
4224dwarf2_release_queue (void *dummy)
4225{
4226 struct dwarf2_queue_item *item, *last;
4227
4228 item = dwarf2_queue;
4229 while (item)
4230 {
4231 /* Anything still marked queued is likely to be in an
4232 inconsistent state, so discard it. */
4233 if (item->per_cu->queued)
4234 {
4235 if (item->per_cu->cu != NULL)
4236 free_one_cached_comp_unit (item->per_cu->cu);
4237 item->per_cu->queued = 0;
4238 }
4239
4240 last = item;
4241 item = item->next;
4242 xfree (last);
4243 }
4244
4245 dwarf2_queue = dwarf2_queue_tail = NULL;
4246}
4247
4248/* Read in full symbols for PST, and anything it depends on. */
4249
c906108c 4250static void
fba45db2 4251psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4252{
10b3939b 4253 struct dwarf2_per_cu_data *per_cu;
c906108c 4254 struct cleanup *back_to;
aaa75496
JB
4255 int i;
4256
4257 for (i = 0; i < pst->number_of_dependencies; i++)
4258 if (!pst->dependencies[i]->readin)
4259 {
4260 /* Inform about additional files that need to be read in. */
4261 if (info_verbose)
4262 {
a3f17187 4263 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4264 fputs_filtered (" ", gdb_stdout);
4265 wrap_here ("");
4266 fputs_filtered ("and ", gdb_stdout);
4267 wrap_here ("");
4268 printf_filtered ("%s...", pst->dependencies[i]->filename);
4269 wrap_here (""); /* Flush output */
4270 gdb_flush (gdb_stdout);
4271 }
4272 psymtab_to_symtab_1 (pst->dependencies[i]);
4273 }
4274
e38df1d0 4275 per_cu = pst->read_symtab_private;
10b3939b
DJ
4276
4277 if (per_cu == NULL)
aaa75496
JB
4278 {
4279 /* It's an include file, no symbols to read for it.
4280 Everything is in the parent symtab. */
4281 pst->readin = 1;
4282 return;
4283 }
c906108c 4284
9291a0cd 4285 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4286}
4287
93311388 4288/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4289
93311388 4290static void
31ffec48 4291load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4292{
31ffec48 4293 bfd *abfd = objfile->obfd;
10b3939b 4294 struct dwarf2_cu *cu;
c764a876 4295 unsigned int offset;
93311388 4296 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4297 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4298 struct attribute *attr;
98bfdba5 4299 int read_cu = 0;
6502dd73 4300
348e048f
DE
4301 gdb_assert (! per_cu->from_debug_types);
4302
c906108c 4303 /* Set local variables from the partial symbol table info. */
10b3939b 4304 offset = per_cu->offset;
6502dd73 4305
be391dca 4306 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4307 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4308 beg_of_comp_unit = info_ptr;
63d06c5c 4309
98bfdba5
PA
4310 if (per_cu->cu == NULL)
4311 {
9816fde3
JK
4312 cu = xmalloc (sizeof (*cu));
4313 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4314
4315 read_cu = 1;
c906108c 4316
98bfdba5
PA
4317 /* If an error occurs while loading, release our storage. */
4318 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4319
98bfdba5
PA
4320 /* Read in the comp_unit header. */
4321 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4322
98bfdba5
PA
4323 /* Complete the cu_header. */
4324 cu->header.offset = offset;
4325 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4326
98bfdba5
PA
4327 /* Read the abbrevs for this compilation unit. */
4328 dwarf2_read_abbrevs (abfd, cu);
4329 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4330
98bfdba5
PA
4331 /* Link this compilation unit into the compilation unit tree. */
4332 per_cu->cu = cu;
4333 cu->per_cu = per_cu;
98bfdba5
PA
4334
4335 /* Link this CU into read_in_chain. */
4336 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4337 dwarf2_per_objfile->read_in_chain = per_cu;
4338 }
4339 else
4340 {
4341 cu = per_cu->cu;
4342 info_ptr += cu->header.first_die_offset;
4343 }
e142c38c 4344
93311388 4345 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4346
4347 /* We try not to read any attributes in this function, because not
4348 all objfiles needed for references have been loaded yet, and symbol
4349 table processing isn't initialized. But we have to set the CU language,
4350 or we won't be able to build types correctly. */
9816fde3 4351 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4352
a6c727b2
DJ
4353 /* Similarly, if we do not read the producer, we can not apply
4354 producer-specific interpretation. */
4355 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4356 if (attr)
4357 cu->producer = DW_STRING (attr);
4358
98bfdba5
PA
4359 if (read_cu)
4360 {
4361 do_cleanups (free_abbrevs_cleanup);
e142c38c 4362
98bfdba5
PA
4363 /* We've successfully allocated this compilation unit. Let our
4364 caller clean it up when finished with it. */
4365 discard_cleanups (free_cu_cleanup);
4366 }
10b3939b
DJ
4367}
4368
3da10d80
KS
4369/* Add a DIE to the delayed physname list. */
4370
4371static void
4372add_to_method_list (struct type *type, int fnfield_index, int index,
4373 const char *name, struct die_info *die,
4374 struct dwarf2_cu *cu)
4375{
4376 struct delayed_method_info mi;
4377 mi.type = type;
4378 mi.fnfield_index = fnfield_index;
4379 mi.index = index;
4380 mi.name = name;
4381 mi.die = die;
4382 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4383}
4384
4385/* A cleanup for freeing the delayed method list. */
4386
4387static void
4388free_delayed_list (void *ptr)
4389{
4390 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4391 if (cu->method_list != NULL)
4392 {
4393 VEC_free (delayed_method_info, cu->method_list);
4394 cu->method_list = NULL;
4395 }
4396}
4397
4398/* Compute the physnames of any methods on the CU's method list.
4399
4400 The computation of method physnames is delayed in order to avoid the
4401 (bad) condition that one of the method's formal parameters is of an as yet
4402 incomplete type. */
4403
4404static void
4405compute_delayed_physnames (struct dwarf2_cu *cu)
4406{
4407 int i;
4408 struct delayed_method_info *mi;
4409 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4410 {
4411 char *physname;
4412 struct fn_fieldlist *fn_flp
4413 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4414 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4415 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4416 }
4417}
4418
10b3939b
DJ
4419/* Generate full symbol information for PST and CU, whose DIEs have
4420 already been loaded into memory. */
4421
4422static void
4423process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4424{
10b3939b 4425 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4426 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4427 CORE_ADDR lowpc, highpc;
4428 struct symtab *symtab;
3da10d80 4429 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4430 CORE_ADDR baseaddr;
4431
4432 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4433
10b3939b
DJ
4434 buildsym_init ();
4435 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4436 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4437
4438 cu->list_in_scope = &file_symbols;
c906108c 4439
d85a05f0 4440 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4441
c906108c 4442 /* Do line number decoding in read_file_scope () */
10b3939b 4443 process_die (cu->dies, cu);
c906108c 4444
3da10d80
KS
4445 /* Now that we have processed all the DIEs in the CU, all the types
4446 should be complete, and it should now be safe to compute all of the
4447 physnames. */
4448 compute_delayed_physnames (cu);
4449 do_cleanups (delayed_list_cleanup);
4450
fae299cd
DC
4451 /* Some compilers don't define a DW_AT_high_pc attribute for the
4452 compilation unit. If the DW_AT_high_pc is missing, synthesize
4453 it, by scanning the DIE's below the compilation unit. */
10b3939b 4454 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4455
613e1657 4456 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4457
4458 /* Set symtab language to language from DW_AT_language.
4459 If the compilation is from a C file generated by language preprocessors,
4460 do not set the language if it was already deduced by start_subfile. */
4461 if (symtab != NULL
10b3939b 4462 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4463 {
10b3939b 4464 symtab->language = cu->language;
c906108c 4465 }
9291a0cd
TT
4466
4467 if (dwarf2_per_objfile->using_index)
4468 per_cu->v.quick->symtab = symtab;
4469 else
4470 {
4471 struct partial_symtab *pst = per_cu->v.psymtab;
4472 pst->symtab = symtab;
4473 pst->readin = 1;
4474 }
c906108c
SS
4475
4476 do_cleanups (back_to);
4477}
4478
4479/* Process a die and its children. */
4480
4481static void
e7c27a73 4482process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4483{
4484 switch (die->tag)
4485 {
4486 case DW_TAG_padding:
4487 break;
4488 case DW_TAG_compile_unit:
e7c27a73 4489 read_file_scope (die, cu);
c906108c 4490 break;
348e048f
DE
4491 case DW_TAG_type_unit:
4492 read_type_unit_scope (die, cu);
4493 break;
c906108c 4494 case DW_TAG_subprogram:
c906108c 4495 case DW_TAG_inlined_subroutine:
edb3359d 4496 read_func_scope (die, cu);
c906108c
SS
4497 break;
4498 case DW_TAG_lexical_block:
14898363
L
4499 case DW_TAG_try_block:
4500 case DW_TAG_catch_block:
e7c27a73 4501 read_lexical_block_scope (die, cu);
c906108c
SS
4502 break;
4503 case DW_TAG_class_type:
680b30c7 4504 case DW_TAG_interface_type:
c906108c
SS
4505 case DW_TAG_structure_type:
4506 case DW_TAG_union_type:
134d01f1 4507 process_structure_scope (die, cu);
c906108c
SS
4508 break;
4509 case DW_TAG_enumeration_type:
134d01f1 4510 process_enumeration_scope (die, cu);
c906108c 4511 break;
134d01f1 4512
f792889a
DJ
4513 /* These dies have a type, but processing them does not create
4514 a symbol or recurse to process the children. Therefore we can
4515 read them on-demand through read_type_die. */
c906108c 4516 case DW_TAG_subroutine_type:
72019c9c 4517 case DW_TAG_set_type:
c906108c 4518 case DW_TAG_array_type:
c906108c 4519 case DW_TAG_pointer_type:
c906108c 4520 case DW_TAG_ptr_to_member_type:
c906108c 4521 case DW_TAG_reference_type:
c906108c 4522 case DW_TAG_string_type:
c906108c 4523 break;
134d01f1 4524
c906108c 4525 case DW_TAG_base_type:
a02abb62 4526 case DW_TAG_subrange_type:
cb249c71 4527 case DW_TAG_typedef:
134d01f1
DJ
4528 /* Add a typedef symbol for the type definition, if it has a
4529 DW_AT_name. */
f792889a 4530 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4531 break;
c906108c 4532 case DW_TAG_common_block:
e7c27a73 4533 read_common_block (die, cu);
c906108c
SS
4534 break;
4535 case DW_TAG_common_inclusion:
4536 break;
d9fa45fe 4537 case DW_TAG_namespace:
63d06c5c 4538 processing_has_namespace_info = 1;
e7c27a73 4539 read_namespace (die, cu);
d9fa45fe 4540 break;
5d7cb8df 4541 case DW_TAG_module:
f55ee35c 4542 processing_has_namespace_info = 1;
5d7cb8df
JK
4543 read_module (die, cu);
4544 break;
d9fa45fe
DC
4545 case DW_TAG_imported_declaration:
4546 case DW_TAG_imported_module:
63d06c5c 4547 processing_has_namespace_info = 1;
27aa8d6a
SW
4548 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4549 || cu->language != language_fortran))
4550 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4551 dwarf_tag_name (die->tag));
4552 read_import_statement (die, cu);
d9fa45fe 4553 break;
c906108c 4554 default:
e7c27a73 4555 new_symbol (die, NULL, cu);
c906108c
SS
4556 break;
4557 }
4558}
4559
94af9270
KS
4560/* A helper function for dwarf2_compute_name which determines whether DIE
4561 needs to have the name of the scope prepended to the name listed in the
4562 die. */
4563
4564static int
4565die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4566{
1c809c68
TT
4567 struct attribute *attr;
4568
94af9270
KS
4569 switch (die->tag)
4570 {
4571 case DW_TAG_namespace:
4572 case DW_TAG_typedef:
4573 case DW_TAG_class_type:
4574 case DW_TAG_interface_type:
4575 case DW_TAG_structure_type:
4576 case DW_TAG_union_type:
4577 case DW_TAG_enumeration_type:
4578 case DW_TAG_enumerator:
4579 case DW_TAG_subprogram:
4580 case DW_TAG_member:
4581 return 1;
4582
4583 case DW_TAG_variable:
c2b0a229 4584 case DW_TAG_constant:
94af9270
KS
4585 /* We only need to prefix "globally" visible variables. These include
4586 any variable marked with DW_AT_external or any variable that
4587 lives in a namespace. [Variables in anonymous namespaces
4588 require prefixing, but they are not DW_AT_external.] */
4589
4590 if (dwarf2_attr (die, DW_AT_specification, cu))
4591 {
4592 struct dwarf2_cu *spec_cu = cu;
9a619af0 4593
94af9270
KS
4594 return die_needs_namespace (die_specification (die, &spec_cu),
4595 spec_cu);
4596 }
4597
1c809c68 4598 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4599 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4600 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4601 return 0;
4602 /* A variable in a lexical block of some kind does not need a
4603 namespace, even though in C++ such variables may be external
4604 and have a mangled name. */
4605 if (die->parent->tag == DW_TAG_lexical_block
4606 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4607 || die->parent->tag == DW_TAG_catch_block
4608 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4609 return 0;
4610 return 1;
94af9270
KS
4611
4612 default:
4613 return 0;
4614 }
4615}
4616
98bfdba5
PA
4617/* Retrieve the last character from a mem_file. */
4618
4619static void
4620do_ui_file_peek_last (void *object, const char *buffer, long length)
4621{
4622 char *last_char_p = (char *) object;
4623
4624 if (length > 0)
4625 *last_char_p = buffer[length - 1];
4626}
4627
94af9270
KS
4628/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4629 compute the physname for the object, which include a method's
4630 formal parameters (C++/Java) and return type (Java).
4631
af6b7be1
JB
4632 For Ada, return the DIE's linkage name rather than the fully qualified
4633 name. PHYSNAME is ignored..
4634
94af9270
KS
4635 The result is allocated on the objfile_obstack and canonicalized. */
4636
4637static const char *
4638dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4639 int physname)
4640{
4641 if (name == NULL)
4642 name = dwarf2_name (die, cu);
4643
f55ee35c
JK
4644 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4645 compute it by typename_concat inside GDB. */
4646 if (cu->language == language_ada
4647 || (cu->language == language_fortran && physname))
4648 {
4649 /* For Ada unit, we prefer the linkage name over the name, as
4650 the former contains the exported name, which the user expects
4651 to be able to reference. Ideally, we want the user to be able
4652 to reference this entity using either natural or linkage name,
4653 but we haven't started looking at this enhancement yet. */
4654 struct attribute *attr;
4655
4656 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4657 if (attr == NULL)
4658 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4659 if (attr && DW_STRING (attr))
4660 return DW_STRING (attr);
4661 }
4662
94af9270
KS
4663 /* These are the only languages we know how to qualify names in. */
4664 if (name != NULL
f55ee35c
JK
4665 && (cu->language == language_cplus || cu->language == language_java
4666 || cu->language == language_fortran))
94af9270
KS
4667 {
4668 if (die_needs_namespace (die, cu))
4669 {
4670 long length;
4671 char *prefix;
4672 struct ui_file *buf;
4673
4674 prefix = determine_prefix (die, cu);
4675 buf = mem_fileopen ();
4676 if (*prefix != '\0')
4677 {
f55ee35c
JK
4678 char *prefixed_name = typename_concat (NULL, prefix, name,
4679 physname, cu);
9a619af0 4680
94af9270
KS
4681 fputs_unfiltered (prefixed_name, buf);
4682 xfree (prefixed_name);
4683 }
4684 else
4685 fputs_unfiltered (name ? name : "", buf);
4686
98bfdba5
PA
4687 /* Template parameters may be specified in the DIE's DW_AT_name, or
4688 as children with DW_TAG_template_type_param or
4689 DW_TAG_value_type_param. If the latter, add them to the name
4690 here. If the name already has template parameters, then
4691 skip this step; some versions of GCC emit both, and
4692 it is more efficient to use the pre-computed name.
4693
4694 Something to keep in mind about this process: it is very
4695 unlikely, or in some cases downright impossible, to produce
4696 something that will match the mangled name of a function.
4697 If the definition of the function has the same debug info,
4698 we should be able to match up with it anyway. But fallbacks
4699 using the minimal symbol, for instance to find a method
4700 implemented in a stripped copy of libstdc++, will not work.
4701 If we do not have debug info for the definition, we will have to
4702 match them up some other way.
4703
4704 When we do name matching there is a related problem with function
4705 templates; two instantiated function templates are allowed to
4706 differ only by their return types, which we do not add here. */
4707
4708 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4709 {
4710 struct attribute *attr;
4711 struct die_info *child;
4712 int first = 1;
4713
4714 die->building_fullname = 1;
4715
4716 for (child = die->child; child != NULL; child = child->sibling)
4717 {
4718 struct type *type;
4719 long value;
4720 gdb_byte *bytes;
4721 struct dwarf2_locexpr_baton *baton;
4722 struct value *v;
4723
4724 if (child->tag != DW_TAG_template_type_param
4725 && child->tag != DW_TAG_template_value_param)
4726 continue;
4727
4728 if (first)
4729 {
4730 fputs_unfiltered ("<", buf);
4731 first = 0;
4732 }
4733 else
4734 fputs_unfiltered (", ", buf);
4735
4736 attr = dwarf2_attr (child, DW_AT_type, cu);
4737 if (attr == NULL)
4738 {
4739 complaint (&symfile_complaints,
4740 _("template parameter missing DW_AT_type"));
4741 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4742 continue;
4743 }
4744 type = die_type (child, cu);
4745
4746 if (child->tag == DW_TAG_template_type_param)
4747 {
4748 c_print_type (type, "", buf, -1, 0);
4749 continue;
4750 }
4751
4752 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4753 if (attr == NULL)
4754 {
4755 complaint (&symfile_complaints,
4756 _("template parameter missing DW_AT_const_value"));
4757 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4758 continue;
4759 }
4760
4761 dwarf2_const_value_attr (attr, type, name,
4762 &cu->comp_unit_obstack, cu,
4763 &value, &bytes, &baton);
4764
4765 if (TYPE_NOSIGN (type))
4766 /* GDB prints characters as NUMBER 'CHAR'. If that's
4767 changed, this can use value_print instead. */
4768 c_printchar (value, type, buf);
4769 else
4770 {
4771 struct value_print_options opts;
4772
4773 if (baton != NULL)
4774 v = dwarf2_evaluate_loc_desc (type, NULL,
4775 baton->data,
4776 baton->size,
4777 baton->per_cu);
4778 else if (bytes != NULL)
4779 {
4780 v = allocate_value (type);
4781 memcpy (value_contents_writeable (v), bytes,
4782 TYPE_LENGTH (type));
4783 }
4784 else
4785 v = value_from_longest (type, value);
4786
4787 /* Specify decimal so that we do not depend on the radix. */
4788 get_formatted_print_options (&opts, 'd');
4789 opts.raw = 1;
4790 value_print (v, buf, &opts);
4791 release_value (v);
4792 value_free (v);
4793 }
4794 }
4795
4796 die->building_fullname = 0;
4797
4798 if (!first)
4799 {
4800 /* Close the argument list, with a space if necessary
4801 (nested templates). */
4802 char last_char = '\0';
4803 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4804 if (last_char == '>')
4805 fputs_unfiltered (" >", buf);
4806 else
4807 fputs_unfiltered (">", buf);
4808 }
4809 }
4810
94af9270
KS
4811 /* For Java and C++ methods, append formal parameter type
4812 information, if PHYSNAME. */
6e70227d 4813
94af9270
KS
4814 if (physname && die->tag == DW_TAG_subprogram
4815 && (cu->language == language_cplus
4816 || cu->language == language_java))
4817 {
4818 struct type *type = read_type_die (die, cu);
4819
4820 c_type_print_args (type, buf, 0, cu->language);
4821
4822 if (cu->language == language_java)
4823 {
4824 /* For java, we must append the return type to method
4825 names. */
4826 if (die->tag == DW_TAG_subprogram)
4827 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4828 0, 0);
4829 }
4830 else if (cu->language == language_cplus)
4831 {
60430eff
DJ
4832 /* Assume that an artificial first parameter is
4833 "this", but do not crash if it is not. RealView
4834 marks unnamed (and thus unused) parameters as
4835 artificial; there is no way to differentiate
4836 the two cases. */
94af9270
KS
4837 if (TYPE_NFIELDS (type) > 0
4838 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4839 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4840 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4841 fputs_unfiltered (" const", buf);
4842 }
4843 }
4844
4845 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4846 &length);
4847 ui_file_delete (buf);
4848
4849 if (cu->language == language_cplus)
4850 {
4851 char *cname
4852 = dwarf2_canonicalize_name (name, cu,
4853 &cu->objfile->objfile_obstack);
9a619af0 4854
94af9270
KS
4855 if (cname != NULL)
4856 name = cname;
4857 }
4858 }
4859 }
4860
4861 return name;
4862}
4863
0114d602
DJ
4864/* Return the fully qualified name of DIE, based on its DW_AT_name.
4865 If scope qualifiers are appropriate they will be added. The result
4866 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4867 not have a name. NAME may either be from a previous call to
4868 dwarf2_name or NULL.
4869
4870 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4871
4872static const char *
94af9270 4873dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4874{
94af9270
KS
4875 return dwarf2_compute_name (name, die, cu, 0);
4876}
0114d602 4877
94af9270
KS
4878/* Construct a physname for the given DIE in CU. NAME may either be
4879 from a previous call to dwarf2_name or NULL. The result will be
4880 allocated on the objfile_objstack or NULL if the DIE does not have a
4881 name.
0114d602 4882
94af9270 4883 The output string will be canonicalized (if C++/Java). */
0114d602 4884
94af9270
KS
4885static const char *
4886dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4887{
4888 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4889}
4890
27aa8d6a
SW
4891/* Read the import statement specified by the given die and record it. */
4892
4893static void
4894read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4895{
4896 struct attribute *import_attr;
4897 struct die_info *imported_die;
de4affc9 4898 struct dwarf2_cu *imported_cu;
27aa8d6a 4899 const char *imported_name;
794684b6 4900 const char *imported_name_prefix;
13387711
SW
4901 const char *canonical_name;
4902 const char *import_alias;
4903 const char *imported_declaration = NULL;
794684b6 4904 const char *import_prefix;
13387711
SW
4905
4906 char *temp;
27aa8d6a
SW
4907
4908 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4909 if (import_attr == NULL)
4910 {
4911 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4912 dwarf_tag_name (die->tag));
4913 return;
4914 }
4915
de4affc9
CC
4916 imported_cu = cu;
4917 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4918 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4919 if (imported_name == NULL)
4920 {
4921 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4922
4923 The import in the following code:
4924 namespace A
4925 {
4926 typedef int B;
4927 }
4928
4929 int main ()
4930 {
4931 using A::B;
4932 B b;
4933 return b;
4934 }
4935
4936 ...
4937 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4938 <52> DW_AT_decl_file : 1
4939 <53> DW_AT_decl_line : 6
4940 <54> DW_AT_import : <0x75>
4941 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4942 <59> DW_AT_name : B
4943 <5b> DW_AT_decl_file : 1
4944 <5c> DW_AT_decl_line : 2
4945 <5d> DW_AT_type : <0x6e>
4946 ...
4947 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4948 <76> DW_AT_byte_size : 4
4949 <77> DW_AT_encoding : 5 (signed)
4950
4951 imports the wrong die ( 0x75 instead of 0x58 ).
4952 This case will be ignored until the gcc bug is fixed. */
4953 return;
4954 }
4955
82856980
SW
4956 /* Figure out the local name after import. */
4957 import_alias = dwarf2_name (die, cu);
27aa8d6a 4958
794684b6
SW
4959 /* Figure out where the statement is being imported to. */
4960 import_prefix = determine_prefix (die, cu);
4961
4962 /* Figure out what the scope of the imported die is and prepend it
4963 to the name of the imported die. */
de4affc9 4964 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4965
f55ee35c
JK
4966 if (imported_die->tag != DW_TAG_namespace
4967 && imported_die->tag != DW_TAG_module)
794684b6 4968 {
13387711
SW
4969 imported_declaration = imported_name;
4970 canonical_name = imported_name_prefix;
794684b6 4971 }
13387711 4972 else if (strlen (imported_name_prefix) > 0)
794684b6 4973 {
13387711
SW
4974 temp = alloca (strlen (imported_name_prefix)
4975 + 2 + strlen (imported_name) + 1);
4976 strcpy (temp, imported_name_prefix);
4977 strcat (temp, "::");
4978 strcat (temp, imported_name);
4979 canonical_name = temp;
794684b6 4980 }
13387711
SW
4981 else
4982 canonical_name = imported_name;
794684b6 4983
c0cc3a76
SW
4984 cp_add_using_directive (import_prefix,
4985 canonical_name,
4986 import_alias,
13387711 4987 imported_declaration,
c0cc3a76 4988 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4989}
4990
5fb290d7 4991static void
e142c38c 4992initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4993{
e142c38c 4994 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4995}
4996
cb1df416
DJ
4997static void
4998free_cu_line_header (void *arg)
4999{
5000 struct dwarf2_cu *cu = arg;
5001
5002 free_line_header (cu->line_header);
5003 cu->line_header = NULL;
5004}
5005
9291a0cd
TT
5006static void
5007find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5008 char **name, char **comp_dir)
5009{
5010 struct attribute *attr;
5011
5012 *name = NULL;
5013 *comp_dir = NULL;
5014
5015 /* Find the filename. Do not use dwarf2_name here, since the filename
5016 is not a source language identifier. */
5017 attr = dwarf2_attr (die, DW_AT_name, cu);
5018 if (attr)
5019 {
5020 *name = DW_STRING (attr);
5021 }
5022
5023 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5024 if (attr)
5025 *comp_dir = DW_STRING (attr);
5026 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5027 {
5028 *comp_dir = ldirname (*name);
5029 if (*comp_dir != NULL)
5030 make_cleanup (xfree, *comp_dir);
5031 }
5032 if (*comp_dir != NULL)
5033 {
5034 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5035 directory, get rid of it. */
5036 char *cp = strchr (*comp_dir, ':');
5037
5038 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5039 *comp_dir = cp + 1;
5040 }
5041
5042 if (*name == NULL)
5043 *name = "<unknown>";
5044}
5045
c906108c 5046static void
e7c27a73 5047read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5048{
e7c27a73 5049 struct objfile *objfile = cu->objfile;
debd256d 5050 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5051 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5052 CORE_ADDR highpc = ((CORE_ADDR) 0);
5053 struct attribute *attr;
e1024ff1 5054 char *name = NULL;
c906108c
SS
5055 char *comp_dir = NULL;
5056 struct die_info *child_die;
5057 bfd *abfd = objfile->obfd;
debd256d 5058 struct line_header *line_header = 0;
e142c38c 5059 CORE_ADDR baseaddr;
6e70227d 5060
e142c38c 5061 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5062
fae299cd 5063 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5064
5065 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5066 from finish_block. */
2acceee2 5067 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5068 lowpc = highpc;
5069 lowpc += baseaddr;
5070 highpc += baseaddr;
5071
9291a0cd 5072 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5073
e142c38c 5074 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5075 if (attr)
5076 {
e142c38c 5077 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5078 }
5079
b0f35d58 5080 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5081 if (attr)
b0f35d58 5082 cu->producer = DW_STRING (attr);
303b6f5d 5083
f4b8a18d
KW
5084 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5085 standardised yet. As a workaround for the language detection we fall
5086 back to the DW_AT_producer string. */
5087 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5088 cu->language = language_opencl;
5089
c906108c
SS
5090 /* We assume that we're processing GCC output. */
5091 processing_gcc_compilation = 2;
c906108c 5092
df8a16a1
DJ
5093 processing_has_namespace_info = 0;
5094
c906108c
SS
5095 start_symtab (name, comp_dir, lowpc);
5096 record_debugformat ("DWARF 2");
303b6f5d 5097 record_producer (cu->producer);
c906108c 5098
e142c38c 5099 initialize_cu_func_list (cu);
c906108c 5100
cb1df416
DJ
5101 /* Decode line number information if present. We do this before
5102 processing child DIEs, so that the line header table is available
5103 for DW_AT_decl_file. */
e142c38c 5104 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5105 if (attr)
5106 {
debd256d 5107 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5108 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5109 if (line_header)
5110 {
cb1df416
DJ
5111 cu->line_header = line_header;
5112 make_cleanup (free_cu_line_header, cu);
aaa75496 5113 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5114 }
5fb290d7 5115 }
debd256d 5116
cb1df416
DJ
5117 /* Process all dies in compilation unit. */
5118 if (die->child != NULL)
5119 {
5120 child_die = die->child;
5121 while (child_die && child_die->tag)
5122 {
5123 process_die (child_die, cu);
5124 child_die = sibling_die (child_die);
5125 }
5126 }
5127
2e276125
JB
5128 /* Decode macro information, if present. Dwarf 2 macro information
5129 refers to information in the line number info statement program
5130 header, so we can only read it if we've read the header
5131 successfully. */
e142c38c 5132 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5133 if (attr && line_header)
2e276125
JB
5134 {
5135 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5136
2e276125 5137 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5138 comp_dir, abfd, cu);
2e276125 5139 }
debd256d 5140 do_cleanups (back_to);
5fb290d7
DJ
5141}
5142
348e048f
DE
5143/* For TUs we want to skip the first top level sibling if it's not the
5144 actual type being defined by this TU. In this case the first top
5145 level sibling is there to provide context only. */
5146
5147static void
5148read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5149{
5150 struct objfile *objfile = cu->objfile;
5151 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5152 CORE_ADDR lowpc;
5153 struct attribute *attr;
5154 char *name = NULL;
5155 char *comp_dir = NULL;
5156 struct die_info *child_die;
5157 bfd *abfd = objfile->obfd;
348e048f
DE
5158
5159 /* start_symtab needs a low pc, but we don't really have one.
5160 Do what read_file_scope would do in the absence of such info. */
5161 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5162
5163 /* Find the filename. Do not use dwarf2_name here, since the filename
5164 is not a source language identifier. */
5165 attr = dwarf2_attr (die, DW_AT_name, cu);
5166 if (attr)
5167 name = DW_STRING (attr);
5168
5169 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5170 if (attr)
5171 comp_dir = DW_STRING (attr);
5172 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5173 {
5174 comp_dir = ldirname (name);
5175 if (comp_dir != NULL)
5176 make_cleanup (xfree, comp_dir);
5177 }
5178
5179 if (name == NULL)
5180 name = "<unknown>";
5181
5182 attr = dwarf2_attr (die, DW_AT_language, cu);
5183 if (attr)
5184 set_cu_language (DW_UNSND (attr), cu);
5185
5186 /* This isn't technically needed today. It is done for symmetry
5187 with read_file_scope. */
5188 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5189 if (attr)
348e048f
DE
5190 cu->producer = DW_STRING (attr);
5191
5192 /* We assume that we're processing GCC output. */
5193 processing_gcc_compilation = 2;
5194
5195 processing_has_namespace_info = 0;
5196
5197 start_symtab (name, comp_dir, lowpc);
5198 record_debugformat ("DWARF 2");
5199 record_producer (cu->producer);
5200
5201 /* Process the dies in the type unit. */
5202 if (die->child == NULL)
5203 {
5204 dump_die_for_error (die);
5205 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5206 bfd_get_filename (abfd));
5207 }
5208
5209 child_die = die->child;
5210
5211 while (child_die && child_die->tag)
5212 {
5213 process_die (child_die, cu);
5214
5215 child_die = sibling_die (child_die);
5216 }
5217
5218 do_cleanups (back_to);
5219}
5220
5fb290d7 5221static void
e142c38c
DJ
5222add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5223 struct dwarf2_cu *cu)
5fb290d7
DJ
5224{
5225 struct function_range *thisfn;
5226
5227 thisfn = (struct function_range *)
7b5a2f43 5228 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5229 thisfn->name = name;
5230 thisfn->lowpc = lowpc;
5231 thisfn->highpc = highpc;
5232 thisfn->seen_line = 0;
5233 thisfn->next = NULL;
5234
e142c38c
DJ
5235 if (cu->last_fn == NULL)
5236 cu->first_fn = thisfn;
5fb290d7 5237 else
e142c38c 5238 cu->last_fn->next = thisfn;
5fb290d7 5239
e142c38c 5240 cu->last_fn = thisfn;
c906108c
SS
5241}
5242
d389af10
JK
5243/* qsort helper for inherit_abstract_dies. */
5244
5245static int
5246unsigned_int_compar (const void *ap, const void *bp)
5247{
5248 unsigned int a = *(unsigned int *) ap;
5249 unsigned int b = *(unsigned int *) bp;
5250
5251 return (a > b) - (b > a);
5252}
5253
5254/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5255 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5256 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5257
5258static void
5259inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5260{
5261 struct die_info *child_die;
5262 unsigned die_children_count;
5263 /* CU offsets which were referenced by children of the current DIE. */
5264 unsigned *offsets;
5265 unsigned *offsets_end, *offsetp;
5266 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5267 struct die_info *origin_die;
5268 /* Iterator of the ORIGIN_DIE children. */
5269 struct die_info *origin_child_die;
5270 struct cleanup *cleanups;
5271 struct attribute *attr;
cd02d79d
PA
5272 struct dwarf2_cu *origin_cu;
5273 struct pending **origin_previous_list_in_scope;
d389af10
JK
5274
5275 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5276 if (!attr)
5277 return;
5278
cd02d79d
PA
5279 /* Note that following die references may follow to a die in a
5280 different cu. */
5281
5282 origin_cu = cu;
5283 origin_die = follow_die_ref (die, attr, &origin_cu);
5284
5285 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5286 symbols in. */
5287 origin_previous_list_in_scope = origin_cu->list_in_scope;
5288 origin_cu->list_in_scope = cu->list_in_scope;
5289
edb3359d
DJ
5290 if (die->tag != origin_die->tag
5291 && !(die->tag == DW_TAG_inlined_subroutine
5292 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5293 complaint (&symfile_complaints,
5294 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5295 die->offset, origin_die->offset);
5296
5297 child_die = die->child;
5298 die_children_count = 0;
5299 while (child_die && child_die->tag)
5300 {
5301 child_die = sibling_die (child_die);
5302 die_children_count++;
5303 }
5304 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5305 cleanups = make_cleanup (xfree, offsets);
5306
5307 offsets_end = offsets;
5308 child_die = die->child;
5309 while (child_die && child_die->tag)
5310 {
c38f313d
DJ
5311 /* For each CHILD_DIE, find the corresponding child of
5312 ORIGIN_DIE. If there is more than one layer of
5313 DW_AT_abstract_origin, follow them all; there shouldn't be,
5314 but GCC versions at least through 4.4 generate this (GCC PR
5315 40573). */
5316 struct die_info *child_origin_die = child_die;
cd02d79d 5317 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5318
c38f313d
DJ
5319 while (1)
5320 {
cd02d79d
PA
5321 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5322 child_origin_cu);
c38f313d
DJ
5323 if (attr == NULL)
5324 break;
cd02d79d
PA
5325 child_origin_die = follow_die_ref (child_origin_die, attr,
5326 &child_origin_cu);
c38f313d
DJ
5327 }
5328
d389af10
JK
5329 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5330 counterpart may exist. */
c38f313d 5331 if (child_origin_die != child_die)
d389af10 5332 {
edb3359d
DJ
5333 if (child_die->tag != child_origin_die->tag
5334 && !(child_die->tag == DW_TAG_inlined_subroutine
5335 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5336 complaint (&symfile_complaints,
5337 _("Child DIE 0x%x and its abstract origin 0x%x have "
5338 "different tags"), child_die->offset,
5339 child_origin_die->offset);
c38f313d
DJ
5340 if (child_origin_die->parent != origin_die)
5341 complaint (&symfile_complaints,
5342 _("Child DIE 0x%x and its abstract origin 0x%x have "
5343 "different parents"), child_die->offset,
5344 child_origin_die->offset);
5345 else
5346 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5347 }
5348 child_die = sibling_die (child_die);
5349 }
5350 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5351 unsigned_int_compar);
5352 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5353 if (offsetp[-1] == *offsetp)
5354 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5355 "to DIE 0x%x as their abstract origin"),
5356 die->offset, *offsetp);
5357
5358 offsetp = offsets;
5359 origin_child_die = origin_die->child;
5360 while (origin_child_die && origin_child_die->tag)
5361 {
5362 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5363 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5364 offsetp++;
5365 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5366 {
5367 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5368 process_die (origin_child_die, origin_cu);
d389af10
JK
5369 }
5370 origin_child_die = sibling_die (origin_child_die);
5371 }
cd02d79d 5372 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5373
5374 do_cleanups (cleanups);
5375}
5376
c906108c 5377static void
e7c27a73 5378read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5379{
e7c27a73 5380 struct objfile *objfile = cu->objfile;
52f0bd74 5381 struct context_stack *new;
c906108c
SS
5382 CORE_ADDR lowpc;
5383 CORE_ADDR highpc;
5384 struct die_info *child_die;
edb3359d 5385 struct attribute *attr, *call_line, *call_file;
c906108c 5386 char *name;
e142c38c 5387 CORE_ADDR baseaddr;
801e3a5b 5388 struct block *block;
edb3359d 5389 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5390 VEC (symbolp) *template_args = NULL;
5391 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5392
5393 if (inlined_func)
5394 {
5395 /* If we do not have call site information, we can't show the
5396 caller of this inlined function. That's too confusing, so
5397 only use the scope for local variables. */
5398 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5399 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5400 if (call_line == NULL || call_file == NULL)
5401 {
5402 read_lexical_block_scope (die, cu);
5403 return;
5404 }
5405 }
c906108c 5406
e142c38c
DJ
5407 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5408
94af9270 5409 name = dwarf2_name (die, cu);
c906108c 5410
e8d05480
JB
5411 /* Ignore functions with missing or empty names. These are actually
5412 illegal according to the DWARF standard. */
5413 if (name == NULL)
5414 {
5415 complaint (&symfile_complaints,
5416 _("missing name for subprogram DIE at %d"), die->offset);
5417 return;
5418 }
5419
5420 /* Ignore functions with missing or invalid low and high pc attributes. */
5421 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5422 {
ae4d0c03
PM
5423 attr = dwarf2_attr (die, DW_AT_external, cu);
5424 if (!attr || !DW_UNSND (attr))
5425 complaint (&symfile_complaints,
5426 _("cannot get low and high bounds for subprogram DIE at %d"),
5427 die->offset);
e8d05480
JB
5428 return;
5429 }
c906108c
SS
5430
5431 lowpc += baseaddr;
5432 highpc += baseaddr;
5433
5fb290d7 5434 /* Record the function range for dwarf_decode_lines. */
e142c38c 5435 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5436
34eaf542
TT
5437 /* If we have any template arguments, then we must allocate a
5438 different sort of symbol. */
5439 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5440 {
5441 if (child_die->tag == DW_TAG_template_type_param
5442 || child_die->tag == DW_TAG_template_value_param)
5443 {
5444 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5445 struct template_symbol);
5446 templ_func->base.is_cplus_template_function = 1;
5447 break;
5448 }
5449 }
5450
c906108c 5451 new = push_context (0, lowpc);
34eaf542
TT
5452 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5453 (struct symbol *) templ_func);
4c2df51b 5454
4cecd739
DJ
5455 /* If there is a location expression for DW_AT_frame_base, record
5456 it. */
e142c38c 5457 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5458 if (attr)
c034e007
AC
5459 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5460 expression is being recorded directly in the function's symbol
5461 and not in a separate frame-base object. I guess this hack is
5462 to avoid adding some sort of frame-base adjunct/annex to the
5463 function's symbol :-(. The problem with doing this is that it
5464 results in a function symbol with a location expression that
5465 has nothing to do with the location of the function, ouch! The
5466 relationship should be: a function's symbol has-a frame base; a
5467 frame-base has-a location expression. */
e7c27a73 5468 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5469
e142c38c 5470 cu->list_in_scope = &local_symbols;
c906108c 5471
639d11d3 5472 if (die->child != NULL)
c906108c 5473 {
639d11d3 5474 child_die = die->child;
c906108c
SS
5475 while (child_die && child_die->tag)
5476 {
34eaf542
TT
5477 if (child_die->tag == DW_TAG_template_type_param
5478 || child_die->tag == DW_TAG_template_value_param)
5479 {
5480 struct symbol *arg = new_symbol (child_die, NULL, cu);
5481
f1078f66
DJ
5482 if (arg != NULL)
5483 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5484 }
5485 else
5486 process_die (child_die, cu);
c906108c
SS
5487 child_die = sibling_die (child_die);
5488 }
5489 }
5490
d389af10
JK
5491 inherit_abstract_dies (die, cu);
5492
4a811a97
UW
5493 /* If we have a DW_AT_specification, we might need to import using
5494 directives from the context of the specification DIE. See the
5495 comment in determine_prefix. */
5496 if (cu->language == language_cplus
5497 && dwarf2_attr (die, DW_AT_specification, cu))
5498 {
5499 struct dwarf2_cu *spec_cu = cu;
5500 struct die_info *spec_die = die_specification (die, &spec_cu);
5501
5502 while (spec_die)
5503 {
5504 child_die = spec_die->child;
5505 while (child_die && child_die->tag)
5506 {
5507 if (child_die->tag == DW_TAG_imported_module)
5508 process_die (child_die, spec_cu);
5509 child_die = sibling_die (child_die);
5510 }
5511
5512 /* In some cases, GCC generates specification DIEs that
5513 themselves contain DW_AT_specification attributes. */
5514 spec_die = die_specification (spec_die, &spec_cu);
5515 }
5516 }
5517
c906108c
SS
5518 new = pop_context ();
5519 /* Make a block for the local symbols within. */
801e3a5b
JB
5520 block = finish_block (new->name, &local_symbols, new->old_blocks,
5521 lowpc, highpc, objfile);
5522
df8a16a1 5523 /* For C++, set the block's scope. */
f55ee35c 5524 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5525 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5526 determine_prefix (die, cu),
df8a16a1
DJ
5527 processing_has_namespace_info);
5528
801e3a5b
JB
5529 /* If we have address ranges, record them. */
5530 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5531
34eaf542
TT
5532 /* Attach template arguments to function. */
5533 if (! VEC_empty (symbolp, template_args))
5534 {
5535 gdb_assert (templ_func != NULL);
5536
5537 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5538 templ_func->template_arguments
5539 = obstack_alloc (&objfile->objfile_obstack,
5540 (templ_func->n_template_arguments
5541 * sizeof (struct symbol *)));
5542 memcpy (templ_func->template_arguments,
5543 VEC_address (symbolp, template_args),
5544 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5545 VEC_free (symbolp, template_args);
5546 }
5547
208d8187
JB
5548 /* In C++, we can have functions nested inside functions (e.g., when
5549 a function declares a class that has methods). This means that
5550 when we finish processing a function scope, we may need to go
5551 back to building a containing block's symbol lists. */
5552 local_symbols = new->locals;
5553 param_symbols = new->params;
27aa8d6a 5554 using_directives = new->using_directives;
208d8187 5555
921e78cf
JB
5556 /* If we've finished processing a top-level function, subsequent
5557 symbols go in the file symbol list. */
5558 if (outermost_context_p ())
e142c38c 5559 cu->list_in_scope = &file_symbols;
c906108c
SS
5560}
5561
5562/* Process all the DIES contained within a lexical block scope. Start
5563 a new scope, process the dies, and then close the scope. */
5564
5565static void
e7c27a73 5566read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5567{
e7c27a73 5568 struct objfile *objfile = cu->objfile;
52f0bd74 5569 struct context_stack *new;
c906108c
SS
5570 CORE_ADDR lowpc, highpc;
5571 struct die_info *child_die;
e142c38c
DJ
5572 CORE_ADDR baseaddr;
5573
5574 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5575
5576 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5577 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5578 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5579 be nasty. Might be easier to properly extend generic blocks to
af34e669 5580 describe ranges. */
d85a05f0 5581 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5582 return;
5583 lowpc += baseaddr;
5584 highpc += baseaddr;
5585
5586 push_context (0, lowpc);
639d11d3 5587 if (die->child != NULL)
c906108c 5588 {
639d11d3 5589 child_die = die->child;
c906108c
SS
5590 while (child_die && child_die->tag)
5591 {
e7c27a73 5592 process_die (child_die, cu);
c906108c
SS
5593 child_die = sibling_die (child_die);
5594 }
5595 }
5596 new = pop_context ();
5597
8540c487 5598 if (local_symbols != NULL || using_directives != NULL)
c906108c 5599 {
801e3a5b
JB
5600 struct block *block
5601 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5602 highpc, objfile);
5603
5604 /* Note that recording ranges after traversing children, as we
5605 do here, means that recording a parent's ranges entails
5606 walking across all its children's ranges as they appear in
5607 the address map, which is quadratic behavior.
5608
5609 It would be nicer to record the parent's ranges before
5610 traversing its children, simply overriding whatever you find
5611 there. But since we don't even decide whether to create a
5612 block until after we've traversed its children, that's hard
5613 to do. */
5614 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5615 }
5616 local_symbols = new->locals;
27aa8d6a 5617 using_directives = new->using_directives;
c906108c
SS
5618}
5619
43039443 5620/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5621 Return 1 if the attributes are present and valid, otherwise, return 0.
5622 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5623
5624static int
5625dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5626 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5627 struct partial_symtab *ranges_pst)
43039443
JK
5628{
5629 struct objfile *objfile = cu->objfile;
5630 struct comp_unit_head *cu_header = &cu->header;
5631 bfd *obfd = objfile->obfd;
5632 unsigned int addr_size = cu_header->addr_size;
5633 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5634 /* Base address selection entry. */
5635 CORE_ADDR base;
5636 int found_base;
5637 unsigned int dummy;
5638 gdb_byte *buffer;
5639 CORE_ADDR marker;
5640 int low_set;
5641 CORE_ADDR low = 0;
5642 CORE_ADDR high = 0;
ff013f42 5643 CORE_ADDR baseaddr;
43039443 5644
d00adf39
DE
5645 found_base = cu->base_known;
5646 base = cu->base_address;
43039443 5647
be391dca 5648 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5649 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5650 {
5651 complaint (&symfile_complaints,
5652 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5653 offset);
5654 return 0;
5655 }
dce234bc 5656 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5657
5658 /* Read in the largest possible address. */
5659 marker = read_address (obfd, buffer, cu, &dummy);
5660 if ((marker & mask) == mask)
5661 {
5662 /* If we found the largest possible address, then
5663 read the base address. */
5664 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5665 buffer += 2 * addr_size;
5666 offset += 2 * addr_size;
5667 found_base = 1;
5668 }
5669
5670 low_set = 0;
5671
e7030f15 5672 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5673
43039443
JK
5674 while (1)
5675 {
5676 CORE_ADDR range_beginning, range_end;
5677
5678 range_beginning = read_address (obfd, buffer, cu, &dummy);
5679 buffer += addr_size;
5680 range_end = read_address (obfd, buffer, cu, &dummy);
5681 buffer += addr_size;
5682 offset += 2 * addr_size;
5683
5684 /* An end of list marker is a pair of zero addresses. */
5685 if (range_beginning == 0 && range_end == 0)
5686 /* Found the end of list entry. */
5687 break;
5688
5689 /* Each base address selection entry is a pair of 2 values.
5690 The first is the largest possible address, the second is
5691 the base address. Check for a base address here. */
5692 if ((range_beginning & mask) == mask)
5693 {
5694 /* If we found the largest possible address, then
5695 read the base address. */
5696 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5697 found_base = 1;
5698 continue;
5699 }
5700
5701 if (!found_base)
5702 {
5703 /* We have no valid base address for the ranges
5704 data. */
5705 complaint (&symfile_complaints,
5706 _("Invalid .debug_ranges data (no base address)"));
5707 return 0;
5708 }
5709
5710 range_beginning += base;
5711 range_end += base;
5712
ff013f42
JK
5713 if (ranges_pst != NULL && range_beginning < range_end)
5714 addrmap_set_empty (objfile->psymtabs_addrmap,
5715 range_beginning + baseaddr, range_end - 1 + baseaddr,
5716 ranges_pst);
5717
43039443
JK
5718 /* FIXME: This is recording everything as a low-high
5719 segment of consecutive addresses. We should have a
5720 data structure for discontiguous block ranges
5721 instead. */
5722 if (! low_set)
5723 {
5724 low = range_beginning;
5725 high = range_end;
5726 low_set = 1;
5727 }
5728 else
5729 {
5730 if (range_beginning < low)
5731 low = range_beginning;
5732 if (range_end > high)
5733 high = range_end;
5734 }
5735 }
5736
5737 if (! low_set)
5738 /* If the first entry is an end-of-list marker, the range
5739 describes an empty scope, i.e. no instructions. */
5740 return 0;
5741
5742 if (low_return)
5743 *low_return = low;
5744 if (high_return)
5745 *high_return = high;
5746 return 1;
5747}
5748
af34e669
DJ
5749/* Get low and high pc attributes from a die. Return 1 if the attributes
5750 are present and valid, otherwise, return 0. Return -1 if the range is
5751 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5752static int
af34e669 5753dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5754 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5755 struct partial_symtab *pst)
c906108c
SS
5756{
5757 struct attribute *attr;
af34e669
DJ
5758 CORE_ADDR low = 0;
5759 CORE_ADDR high = 0;
5760 int ret = 0;
c906108c 5761
e142c38c 5762 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5763 if (attr)
af34e669
DJ
5764 {
5765 high = DW_ADDR (attr);
e142c38c 5766 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5767 if (attr)
5768 low = DW_ADDR (attr);
5769 else
5770 /* Found high w/o low attribute. */
5771 return 0;
5772
5773 /* Found consecutive range of addresses. */
5774 ret = 1;
5775 }
c906108c 5776 else
af34e669 5777 {
e142c38c 5778 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5779 if (attr != NULL)
5780 {
af34e669 5781 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5782 .debug_ranges section. */
d85a05f0 5783 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5784 return 0;
43039443 5785 /* Found discontinuous range of addresses. */
af34e669
DJ
5786 ret = -1;
5787 }
5788 }
c906108c
SS
5789
5790 if (high < low)
5791 return 0;
5792
5793 /* When using the GNU linker, .gnu.linkonce. sections are used to
5794 eliminate duplicate copies of functions and vtables and such.
5795 The linker will arbitrarily choose one and discard the others.
5796 The AT_*_pc values for such functions refer to local labels in
5797 these sections. If the section from that file was discarded, the
5798 labels are not in the output, so the relocs get a value of 0.
5799 If this is a discarded function, mark the pc bounds as invalid,
5800 so that GDB will ignore it. */
72dca2f5 5801 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5802 return 0;
5803
5804 *lowpc = low;
5805 *highpc = high;
af34e669 5806 return ret;
c906108c
SS
5807}
5808
b084d499
JB
5809/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5810 its low and high PC addresses. Do nothing if these addresses could not
5811 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5812 and HIGHPC to the high address if greater than HIGHPC. */
5813
5814static void
5815dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5816 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5817 struct dwarf2_cu *cu)
5818{
5819 CORE_ADDR low, high;
5820 struct die_info *child = die->child;
5821
d85a05f0 5822 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5823 {
5824 *lowpc = min (*lowpc, low);
5825 *highpc = max (*highpc, high);
5826 }
5827
5828 /* If the language does not allow nested subprograms (either inside
5829 subprograms or lexical blocks), we're done. */
5830 if (cu->language != language_ada)
5831 return;
6e70227d 5832
b084d499
JB
5833 /* Check all the children of the given DIE. If it contains nested
5834 subprograms, then check their pc bounds. Likewise, we need to
5835 check lexical blocks as well, as they may also contain subprogram
5836 definitions. */
5837 while (child && child->tag)
5838 {
5839 if (child->tag == DW_TAG_subprogram
5840 || child->tag == DW_TAG_lexical_block)
5841 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5842 child = sibling_die (child);
5843 }
5844}
5845
fae299cd
DC
5846/* Get the low and high pc's represented by the scope DIE, and store
5847 them in *LOWPC and *HIGHPC. If the correct values can't be
5848 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5849
5850static void
5851get_scope_pc_bounds (struct die_info *die,
5852 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5853 struct dwarf2_cu *cu)
5854{
5855 CORE_ADDR best_low = (CORE_ADDR) -1;
5856 CORE_ADDR best_high = (CORE_ADDR) 0;
5857 CORE_ADDR current_low, current_high;
5858
d85a05f0 5859 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5860 {
5861 best_low = current_low;
5862 best_high = current_high;
5863 }
5864 else
5865 {
5866 struct die_info *child = die->child;
5867
5868 while (child && child->tag)
5869 {
5870 switch (child->tag) {
5871 case DW_TAG_subprogram:
b084d499 5872 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5873 break;
5874 case DW_TAG_namespace:
f55ee35c 5875 case DW_TAG_module:
fae299cd
DC
5876 /* FIXME: carlton/2004-01-16: Should we do this for
5877 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5878 that current GCC's always emit the DIEs corresponding
5879 to definitions of methods of classes as children of a
5880 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5881 the DIEs giving the declarations, which could be
5882 anywhere). But I don't see any reason why the
5883 standards says that they have to be there. */
5884 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5885
5886 if (current_low != ((CORE_ADDR) -1))
5887 {
5888 best_low = min (best_low, current_low);
5889 best_high = max (best_high, current_high);
5890 }
5891 break;
5892 default:
5893 /* Ignore. */
5894 break;
5895 }
5896
5897 child = sibling_die (child);
5898 }
5899 }
5900
5901 *lowpc = best_low;
5902 *highpc = best_high;
5903}
5904
801e3a5b
JB
5905/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5906 in DIE. */
5907static void
5908dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5909 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5910{
5911 struct attribute *attr;
5912
5913 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5914 if (attr)
5915 {
5916 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5917
801e3a5b
JB
5918 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5919 if (attr)
5920 {
5921 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5922
801e3a5b
JB
5923 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5924 }
5925 }
5926
5927 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5928 if (attr)
5929 {
5930 bfd *obfd = cu->objfile->obfd;
5931
5932 /* The value of the DW_AT_ranges attribute is the offset of the
5933 address range list in the .debug_ranges section. */
5934 unsigned long offset = DW_UNSND (attr);
dce234bc 5935 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5936
5937 /* For some target architectures, but not others, the
5938 read_address function sign-extends the addresses it returns.
5939 To recognize base address selection entries, we need a
5940 mask. */
5941 unsigned int addr_size = cu->header.addr_size;
5942 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5943
5944 /* The base address, to which the next pair is relative. Note
5945 that this 'base' is a DWARF concept: most entries in a range
5946 list are relative, to reduce the number of relocs against the
5947 debugging information. This is separate from this function's
5948 'baseaddr' argument, which GDB uses to relocate debugging
5949 information from a shared library based on the address at
5950 which the library was loaded. */
d00adf39
DE
5951 CORE_ADDR base = cu->base_address;
5952 int base_known = cu->base_known;
801e3a5b 5953
be391dca 5954 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5955 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5956 {
5957 complaint (&symfile_complaints,
5958 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5959 offset);
5960 return;
5961 }
5962
5963 for (;;)
5964 {
5965 unsigned int bytes_read;
5966 CORE_ADDR start, end;
5967
5968 start = read_address (obfd, buffer, cu, &bytes_read);
5969 buffer += bytes_read;
5970 end = read_address (obfd, buffer, cu, &bytes_read);
5971 buffer += bytes_read;
5972
5973 /* Did we find the end of the range list? */
5974 if (start == 0 && end == 0)
5975 break;
5976
5977 /* Did we find a base address selection entry? */
5978 else if ((start & base_select_mask) == base_select_mask)
5979 {
5980 base = end;
5981 base_known = 1;
5982 }
5983
5984 /* We found an ordinary address range. */
5985 else
5986 {
5987 if (!base_known)
5988 {
5989 complaint (&symfile_complaints,
5990 _("Invalid .debug_ranges data (no base address)"));
5991 return;
5992 }
5993
6e70227d
DE
5994 record_block_range (block,
5995 baseaddr + base + start,
801e3a5b
JB
5996 baseaddr + base + end - 1);
5997 }
5998 }
5999 }
6000}
6001
c906108c
SS
6002/* Add an aggregate field to the field list. */
6003
6004static void
107d2387 6005dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6006 struct dwarf2_cu *cu)
6e70227d 6007{
e7c27a73 6008 struct objfile *objfile = cu->objfile;
5e2b427d 6009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6010 struct nextfield *new_field;
6011 struct attribute *attr;
6012 struct field *fp;
6013 char *fieldname = "";
6014
6015 /* Allocate a new field list entry and link it in. */
6016 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6017 make_cleanup (xfree, new_field);
c906108c 6018 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6019
6020 if (die->tag == DW_TAG_inheritance)
6021 {
6022 new_field->next = fip->baseclasses;
6023 fip->baseclasses = new_field;
6024 }
6025 else
6026 {
6027 new_field->next = fip->fields;
6028 fip->fields = new_field;
6029 }
c906108c
SS
6030 fip->nfields++;
6031
6032 /* Handle accessibility and virtuality of field.
6033 The default accessibility for members is public, the default
6034 accessibility for inheritance is private. */
6035 if (die->tag != DW_TAG_inheritance)
6036 new_field->accessibility = DW_ACCESS_public;
6037 else
6038 new_field->accessibility = DW_ACCESS_private;
6039 new_field->virtuality = DW_VIRTUALITY_none;
6040
e142c38c 6041 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6042 if (attr)
6043 new_field->accessibility = DW_UNSND (attr);
6044 if (new_field->accessibility != DW_ACCESS_public)
6045 fip->non_public_fields = 1;
e142c38c 6046 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6047 if (attr)
6048 new_field->virtuality = DW_UNSND (attr);
6049
6050 fp = &new_field->field;
a9a9bd0f 6051
e142c38c 6052 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6053 {
a9a9bd0f 6054 /* Data member other than a C++ static data member. */
6e70227d 6055
c906108c 6056 /* Get type of field. */
e7c27a73 6057 fp->type = die_type (die, cu);
c906108c 6058
d6a843b5 6059 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6060
c906108c 6061 /* Get bit size of field (zero if none). */
e142c38c 6062 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6063 if (attr)
6064 {
6065 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6066 }
6067 else
6068 {
6069 FIELD_BITSIZE (*fp) = 0;
6070 }
6071
6072 /* Get bit offset of field. */
e142c38c 6073 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6074 if (attr)
6075 {
d4b96c9a 6076 int byte_offset = 0;
c6a0999f 6077
3690dd37 6078 if (attr_form_is_section_offset (attr))
d4b96c9a 6079 dwarf2_complex_location_expr_complaint ();
3690dd37 6080 else if (attr_form_is_constant (attr))
c6a0999f 6081 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6082 else if (attr_form_is_block (attr))
c6a0999f 6083 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6084 else
6085 dwarf2_complex_location_expr_complaint ();
c6a0999f 6086
d6a843b5 6087 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6088 }
e142c38c 6089 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6090 if (attr)
6091 {
5e2b427d 6092 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6093 {
6094 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6095 additional bit offset from the MSB of the containing
6096 anonymous object to the MSB of the field. We don't
6097 have to do anything special since we don't need to
6098 know the size of the anonymous object. */
c906108c
SS
6099 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6100 }
6101 else
6102 {
6103 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6104 MSB of the anonymous object, subtract off the number of
6105 bits from the MSB of the field to the MSB of the
6106 object, and then subtract off the number of bits of
6107 the field itself. The result is the bit offset of
6108 the LSB of the field. */
c906108c
SS
6109 int anonymous_size;
6110 int bit_offset = DW_UNSND (attr);
6111
e142c38c 6112 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6113 if (attr)
6114 {
6115 /* The size of the anonymous object containing
6116 the bit field is explicit, so use the
6117 indicated size (in bytes). */
6118 anonymous_size = DW_UNSND (attr);
6119 }
6120 else
6121 {
6122 /* The size of the anonymous object containing
6123 the bit field must be inferred from the type
6124 attribute of the data member containing the
6125 bit field. */
6126 anonymous_size = TYPE_LENGTH (fp->type);
6127 }
6128 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6129 - bit_offset - FIELD_BITSIZE (*fp);
6130 }
6131 }
6132
6133 /* Get name of field. */
39cbfefa
DJ
6134 fieldname = dwarf2_name (die, cu);
6135 if (fieldname == NULL)
6136 fieldname = "";
d8151005
DJ
6137
6138 /* The name is already allocated along with this objfile, so we don't
6139 need to duplicate it for the type. */
6140 fp->name = fieldname;
c906108c
SS
6141
6142 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6143 pointer or virtual base class pointer) to private. */
e142c38c 6144 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6145 {
d48cc9dd 6146 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6147 new_field->accessibility = DW_ACCESS_private;
6148 fip->non_public_fields = 1;
6149 }
6150 }
a9a9bd0f 6151 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6152 {
a9a9bd0f
DC
6153 /* C++ static member. */
6154
6155 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6156 is a declaration, but all versions of G++ as of this writing
6157 (so through at least 3.2.1) incorrectly generate
6158 DW_TAG_variable tags. */
6e70227d 6159
c906108c 6160 char *physname;
c906108c 6161
a9a9bd0f 6162 /* Get name of field. */
39cbfefa
DJ
6163 fieldname = dwarf2_name (die, cu);
6164 if (fieldname == NULL)
c906108c
SS
6165 return;
6166
254e6b9e 6167 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6168 if (attr
6169 /* Only create a symbol if this is an external value.
6170 new_symbol checks this and puts the value in the global symbol
6171 table, which we want. If it is not external, new_symbol
6172 will try to put the value in cu->list_in_scope which is wrong. */
6173 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6174 {
6175 /* A static const member, not much different than an enum as far as
6176 we're concerned, except that we can support more types. */
6177 new_symbol (die, NULL, cu);
6178 }
6179
2df3850c 6180 /* Get physical name. */
94af9270 6181 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6182
d8151005
DJ
6183 /* The name is already allocated along with this objfile, so we don't
6184 need to duplicate it for the type. */
6185 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6186 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6187 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6188 }
6189 else if (die->tag == DW_TAG_inheritance)
6190 {
6191 /* C++ base class field. */
e142c38c 6192 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6193 if (attr)
d4b96c9a
JK
6194 {
6195 int byte_offset = 0;
6196
6197 if (attr_form_is_section_offset (attr))
6198 dwarf2_complex_location_expr_complaint ();
6199 else if (attr_form_is_constant (attr))
6200 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6201 else if (attr_form_is_block (attr))
6202 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6203 else
6204 dwarf2_complex_location_expr_complaint ();
6205
6206 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6207 }
c906108c 6208 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6209 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6210 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6211 fip->nbaseclasses++;
6212 }
6213}
6214
98751a41
JK
6215/* Add a typedef defined in the scope of the FIP's class. */
6216
6217static void
6218dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6219 struct dwarf2_cu *cu)
6e70227d 6220{
98751a41
JK
6221 struct objfile *objfile = cu->objfile;
6222 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6223 struct typedef_field_list *new_field;
6224 struct attribute *attr;
6225 struct typedef_field *fp;
6226 char *fieldname = "";
6227
6228 /* Allocate a new field list entry and link it in. */
6229 new_field = xzalloc (sizeof (*new_field));
6230 make_cleanup (xfree, new_field);
6231
6232 gdb_assert (die->tag == DW_TAG_typedef);
6233
6234 fp = &new_field->field;
6235
6236 /* Get name of field. */
6237 fp->name = dwarf2_name (die, cu);
6238 if (fp->name == NULL)
6239 return;
6240
6241 fp->type = read_type_die (die, cu);
6242
6243 new_field->next = fip->typedef_field_list;
6244 fip->typedef_field_list = new_field;
6245 fip->typedef_field_list_count++;
6246}
6247
c906108c
SS
6248/* Create the vector of fields, and attach it to the type. */
6249
6250static void
fba45db2 6251dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6252 struct dwarf2_cu *cu)
c906108c
SS
6253{
6254 int nfields = fip->nfields;
6255
6256 /* Record the field count, allocate space for the array of fields,
6257 and create blank accessibility bitfields if necessary. */
6258 TYPE_NFIELDS (type) = nfields;
6259 TYPE_FIELDS (type) = (struct field *)
6260 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6261 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6262
b4ba55a1 6263 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6264 {
6265 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6266
6267 TYPE_FIELD_PRIVATE_BITS (type) =
6268 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6269 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6270
6271 TYPE_FIELD_PROTECTED_BITS (type) =
6272 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6273 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6274
774b6a14
TT
6275 TYPE_FIELD_IGNORE_BITS (type) =
6276 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6277 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6278 }
6279
6280 /* If the type has baseclasses, allocate and clear a bit vector for
6281 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6282 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6283 {
6284 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6285 unsigned char *pointer;
c906108c
SS
6286
6287 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6288 pointer = TYPE_ALLOC (type, num_bytes);
6289 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6290 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6291 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6292 }
6293
6294 /* Copy the saved-up fields into the field vector. Start from the head
6295 of the list, adding to the tail of the field array, so that they end
6296 up in the same order in the array in which they were added to the list. */
6297 while (nfields-- > 0)
6298 {
7d0ccb61
DJ
6299 struct nextfield *fieldp;
6300
6301 if (fip->fields)
6302 {
6303 fieldp = fip->fields;
6304 fip->fields = fieldp->next;
6305 }
6306 else
6307 {
6308 fieldp = fip->baseclasses;
6309 fip->baseclasses = fieldp->next;
6310 }
6311
6312 TYPE_FIELD (type, nfields) = fieldp->field;
6313 switch (fieldp->accessibility)
c906108c 6314 {
c5aa993b 6315 case DW_ACCESS_private:
b4ba55a1
JB
6316 if (cu->language != language_ada)
6317 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6318 break;
c906108c 6319
c5aa993b 6320 case DW_ACCESS_protected:
b4ba55a1
JB
6321 if (cu->language != language_ada)
6322 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6323 break;
c906108c 6324
c5aa993b
JM
6325 case DW_ACCESS_public:
6326 break;
c906108c 6327
c5aa993b
JM
6328 default:
6329 /* Unknown accessibility. Complain and treat it as public. */
6330 {
e2e0b3e5 6331 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6332 fieldp->accessibility);
c5aa993b
JM
6333 }
6334 break;
c906108c
SS
6335 }
6336 if (nfields < fip->nbaseclasses)
6337 {
7d0ccb61 6338 switch (fieldp->virtuality)
c906108c 6339 {
c5aa993b
JM
6340 case DW_VIRTUALITY_virtual:
6341 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6342 if (cu->language == language_ada)
6343 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6344 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6345 break;
c906108c
SS
6346 }
6347 }
c906108c
SS
6348 }
6349}
6350
c906108c
SS
6351/* Add a member function to the proper fieldlist. */
6352
6353static void
107d2387 6354dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6355 struct type *type, struct dwarf2_cu *cu)
c906108c 6356{
e7c27a73 6357 struct objfile *objfile = cu->objfile;
c906108c
SS
6358 struct attribute *attr;
6359 struct fnfieldlist *flp;
6360 int i;
6361 struct fn_field *fnp;
6362 char *fieldname;
c906108c 6363 struct nextfnfield *new_fnfield;
f792889a 6364 struct type *this_type;
c906108c 6365
b4ba55a1
JB
6366 if (cu->language == language_ada)
6367 error ("unexpected member function in Ada type");
6368
2df3850c 6369 /* Get name of member function. */
39cbfefa
DJ
6370 fieldname = dwarf2_name (die, cu);
6371 if (fieldname == NULL)
2df3850c 6372 return;
c906108c 6373
c906108c
SS
6374 /* Look up member function name in fieldlist. */
6375 for (i = 0; i < fip->nfnfields; i++)
6376 {
27bfe10e 6377 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6378 break;
6379 }
6380
6381 /* Create new list element if necessary. */
6382 if (i < fip->nfnfields)
6383 flp = &fip->fnfieldlists[i];
6384 else
6385 {
6386 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6387 {
6388 fip->fnfieldlists = (struct fnfieldlist *)
6389 xrealloc (fip->fnfieldlists,
6390 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6391 * sizeof (struct fnfieldlist));
c906108c 6392 if (fip->nfnfields == 0)
c13c43fd 6393 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6394 }
6395 flp = &fip->fnfieldlists[fip->nfnfields];
6396 flp->name = fieldname;
6397 flp->length = 0;
6398 flp->head = NULL;
3da10d80 6399 i = fip->nfnfields++;
c906108c
SS
6400 }
6401
6402 /* Create a new member function field and chain it to the field list
6403 entry. */
6404 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6405 make_cleanup (xfree, new_fnfield);
c906108c
SS
6406 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6407 new_fnfield->next = flp->head;
6408 flp->head = new_fnfield;
6409 flp->length++;
6410
6411 /* Fill in the member function field info. */
6412 fnp = &new_fnfield->fnfield;
3da10d80
KS
6413
6414 /* Delay processing of the physname until later. */
6415 if (cu->language == language_cplus || cu->language == language_java)
6416 {
6417 add_to_method_list (type, i, flp->length - 1, fieldname,
6418 die, cu);
6419 }
6420 else
6421 {
6422 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6423 fnp->physname = physname ? physname : "";
6424 }
6425
c906108c 6426 fnp->type = alloc_type (objfile);
f792889a
DJ
6427 this_type = read_type_die (die, cu);
6428 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6429 {
f792889a 6430 int nparams = TYPE_NFIELDS (this_type);
c906108c 6431
f792889a 6432 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6433 of the method itself (TYPE_CODE_METHOD). */
6434 smash_to_method_type (fnp->type, type,
f792889a
DJ
6435 TYPE_TARGET_TYPE (this_type),
6436 TYPE_FIELDS (this_type),
6437 TYPE_NFIELDS (this_type),
6438 TYPE_VARARGS (this_type));
c906108c
SS
6439
6440 /* Handle static member functions.
c5aa993b
JM
6441 Dwarf2 has no clean way to discern C++ static and non-static
6442 member functions. G++ helps GDB by marking the first
6443 parameter for non-static member functions (which is the
6444 this pointer) as artificial. We obtain this information
6445 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6446 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6447 fnp->voffset = VOFFSET_STATIC;
6448 }
6449 else
e2e0b3e5 6450 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6451 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6452
6453 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6454 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6455 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6456
6457 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6458 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6459
6460 /* Get accessibility. */
e142c38c 6461 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6462 if (attr)
6463 {
6464 switch (DW_UNSND (attr))
6465 {
c5aa993b
JM
6466 case DW_ACCESS_private:
6467 fnp->is_private = 1;
6468 break;
6469 case DW_ACCESS_protected:
6470 fnp->is_protected = 1;
6471 break;
c906108c
SS
6472 }
6473 }
6474
b02dede2 6475 /* Check for artificial methods. */
e142c38c 6476 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6477 if (attr && DW_UNSND (attr) != 0)
6478 fnp->is_artificial = 1;
6479
0d564a31 6480 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6481 function. For older versions of GCC, this is an offset in the
6482 appropriate virtual table, as specified by DW_AT_containing_type.
6483 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6484 to the object address. */
6485
e142c38c 6486 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6487 if (attr)
8e19ed76 6488 {
aec5aa8b 6489 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6490 {
aec5aa8b
TT
6491 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6492 {
6493 /* Old-style GCC. */
6494 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6495 }
6496 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6497 || (DW_BLOCK (attr)->size > 1
6498 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6499 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6500 {
6501 struct dwarf_block blk;
6502 int offset;
6503
6504 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6505 ? 1 : 2);
6506 blk.size = DW_BLOCK (attr)->size - offset;
6507 blk.data = DW_BLOCK (attr)->data + offset;
6508 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6509 if ((fnp->voffset % cu->header.addr_size) != 0)
6510 dwarf2_complex_location_expr_complaint ();
6511 else
6512 fnp->voffset /= cu->header.addr_size;
6513 fnp->voffset += 2;
6514 }
6515 else
6516 dwarf2_complex_location_expr_complaint ();
6517
6518 if (!fnp->fcontext)
6519 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6520 }
3690dd37 6521 else if (attr_form_is_section_offset (attr))
8e19ed76 6522 {
4d3c2250 6523 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6524 }
6525 else
6526 {
4d3c2250
KB
6527 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6528 fieldname);
8e19ed76 6529 }
0d564a31 6530 }
d48cc9dd
DJ
6531 else
6532 {
6533 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6534 if (attr && DW_UNSND (attr))
6535 {
6536 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6537 complaint (&symfile_complaints,
6538 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6539 fieldname, die->offset);
9655fd1a 6540 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6541 TYPE_CPLUS_DYNAMIC (type) = 1;
6542 }
6543 }
c906108c
SS
6544}
6545
6546/* Create the vector of member function fields, and attach it to the type. */
6547
6548static void
fba45db2 6549dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6550 struct dwarf2_cu *cu)
c906108c
SS
6551{
6552 struct fnfieldlist *flp;
6553 int total_length = 0;
6554 int i;
6555
b4ba55a1
JB
6556 if (cu->language == language_ada)
6557 error ("unexpected member functions in Ada type");
6558
c906108c
SS
6559 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6560 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6561 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6562
6563 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6564 {
6565 struct nextfnfield *nfp = flp->head;
6566 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6567 int k;
6568
6569 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6570 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6571 fn_flp->fn_fields = (struct fn_field *)
6572 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6573 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6574 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6575
6576 total_length += flp->length;
6577 }
6578
6579 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6580 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6581}
6582
1168df01
JB
6583/* Returns non-zero if NAME is the name of a vtable member in CU's
6584 language, zero otherwise. */
6585static int
6586is_vtable_name (const char *name, struct dwarf2_cu *cu)
6587{
6588 static const char vptr[] = "_vptr";
987504bb 6589 static const char vtable[] = "vtable";
1168df01 6590
987504bb
JJ
6591 /* Look for the C++ and Java forms of the vtable. */
6592 if ((cu->language == language_java
6593 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6594 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6595 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6596 return 1;
6597
6598 return 0;
6599}
6600
c0dd20ea 6601/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6602 functions, with the ABI-specified layout. If TYPE describes
6603 such a structure, smash it into a member function type.
61049d3b
DJ
6604
6605 GCC shouldn't do this; it should just output pointer to member DIEs.
6606 This is GCC PR debug/28767. */
c0dd20ea 6607
0b92b5bb
TT
6608static void
6609quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6610{
0b92b5bb 6611 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6612
6613 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6614 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6615 return;
c0dd20ea
DJ
6616
6617 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6618 if (TYPE_FIELD_NAME (type, 0) == NULL
6619 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6620 || TYPE_FIELD_NAME (type, 1) == NULL
6621 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6622 return;
c0dd20ea
DJ
6623
6624 /* Find the type of the method. */
0b92b5bb 6625 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6626 if (pfn_type == NULL
6627 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6628 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6629 return;
c0dd20ea
DJ
6630
6631 /* Look for the "this" argument. */
6632 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6633 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6634 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6635 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6636 return;
c0dd20ea
DJ
6637
6638 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6639 new_type = alloc_type (objfile);
6640 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6641 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6642 TYPE_VARARGS (pfn_type));
0b92b5bb 6643 smash_to_methodptr_type (type, new_type);
c0dd20ea 6644}
1168df01 6645
c906108c 6646/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6647 (definition) to create a type for the structure or union. Fill in
6648 the type's name and general properties; the members will not be
6649 processed until process_structure_type.
c906108c 6650
c767944b
DJ
6651 NOTE: we need to call these functions regardless of whether or not the
6652 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6653 structure or union. This gets the type entered into our set of
6654 user defined types.
6655
6656 However, if the structure is incomplete (an opaque struct/union)
6657 then suppress creating a symbol table entry for it since gdb only
6658 wants to find the one with the complete definition. Note that if
6659 it is complete, we just call new_symbol, which does it's own
6660 checking about whether the struct/union is anonymous or not (and
6661 suppresses creating a symbol table entry itself). */
6662
f792889a 6663static struct type *
134d01f1 6664read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6665{
e7c27a73 6666 struct objfile *objfile = cu->objfile;
c906108c
SS
6667 struct type *type;
6668 struct attribute *attr;
39cbfefa 6669 char *name;
c906108c 6670
348e048f
DE
6671 /* If the definition of this type lives in .debug_types, read that type.
6672 Don't follow DW_AT_specification though, that will take us back up
6673 the chain and we want to go down. */
6674 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6675 if (attr)
6676 {
6677 struct dwarf2_cu *type_cu = cu;
6678 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6679
348e048f
DE
6680 /* We could just recurse on read_structure_type, but we need to call
6681 get_die_type to ensure only one type for this DIE is created.
6682 This is important, for example, because for c++ classes we need
6683 TYPE_NAME set which is only done by new_symbol. Blech. */
6684 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6685
6686 /* TYPE_CU may not be the same as CU.
6687 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6688 return set_die_type (die, type, cu);
6689 }
6690
c0dd20ea 6691 type = alloc_type (objfile);
c906108c 6692 INIT_CPLUS_SPECIFIC (type);
93311388 6693
39cbfefa
DJ
6694 name = dwarf2_name (die, cu);
6695 if (name != NULL)
c906108c 6696 {
987504bb
JJ
6697 if (cu->language == language_cplus
6698 || cu->language == language_java)
63d06c5c 6699 {
3da10d80
KS
6700 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6701
6702 /* dwarf2_full_name might have already finished building the DIE's
6703 type. If so, there is no need to continue. */
6704 if (get_die_type (die, cu) != NULL)
6705 return get_die_type (die, cu);
6706
6707 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6708 if (die->tag == DW_TAG_structure_type
6709 || die->tag == DW_TAG_class_type)
6710 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6711 }
6712 else
6713 {
d8151005
DJ
6714 /* The name is already allocated along with this objfile, so
6715 we don't need to duplicate it for the type. */
94af9270
KS
6716 TYPE_TAG_NAME (type) = (char *) name;
6717 if (die->tag == DW_TAG_class_type)
6718 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6719 }
c906108c
SS
6720 }
6721
6722 if (die->tag == DW_TAG_structure_type)
6723 {
6724 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6725 }
6726 else if (die->tag == DW_TAG_union_type)
6727 {
6728 TYPE_CODE (type) = TYPE_CODE_UNION;
6729 }
6730 else
6731 {
c906108c
SS
6732 TYPE_CODE (type) = TYPE_CODE_CLASS;
6733 }
6734
0cc2414c
TT
6735 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6736 TYPE_DECLARED_CLASS (type) = 1;
6737
e142c38c 6738 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6739 if (attr)
6740 {
6741 TYPE_LENGTH (type) = DW_UNSND (attr);
6742 }
6743 else
6744 {
6745 TYPE_LENGTH (type) = 0;
6746 }
6747
876cecd0 6748 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6749 if (die_is_declaration (die, cu))
876cecd0 6750 TYPE_STUB (type) = 1;
a6c727b2
DJ
6751 else if (attr == NULL && die->child == NULL
6752 && producer_is_realview (cu->producer))
6753 /* RealView does not output the required DW_AT_declaration
6754 on incomplete types. */
6755 TYPE_STUB (type) = 1;
dc718098 6756
c906108c
SS
6757 /* We need to add the type field to the die immediately so we don't
6758 infinitely recurse when dealing with pointers to the structure
6759 type within the structure itself. */
1c379e20 6760 set_die_type (die, type, cu);
c906108c 6761
7e314c57
JK
6762 /* set_die_type should be already done. */
6763 set_descriptive_type (type, die, cu);
6764
c767944b
DJ
6765 return type;
6766}
6767
6768/* Finish creating a structure or union type, including filling in
6769 its members and creating a symbol for it. */
6770
6771static void
6772process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6773{
6774 struct objfile *objfile = cu->objfile;
6775 struct die_info *child_die = die->child;
6776 struct type *type;
6777
6778 type = get_die_type (die, cu);
6779 if (type == NULL)
6780 type = read_structure_type (die, cu);
6781
e142c38c 6782 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6783 {
6784 struct field_info fi;
6785 struct die_info *child_die;
34eaf542 6786 VEC (symbolp) *template_args = NULL;
c767944b 6787 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6788
6789 memset (&fi, 0, sizeof (struct field_info));
6790
639d11d3 6791 child_die = die->child;
c906108c
SS
6792
6793 while (child_die && child_die->tag)
6794 {
a9a9bd0f
DC
6795 if (child_die->tag == DW_TAG_member
6796 || child_die->tag == DW_TAG_variable)
c906108c 6797 {
a9a9bd0f
DC
6798 /* NOTE: carlton/2002-11-05: A C++ static data member
6799 should be a DW_TAG_member that is a declaration, but
6800 all versions of G++ as of this writing (so through at
6801 least 3.2.1) incorrectly generate DW_TAG_variable
6802 tags for them instead. */
e7c27a73 6803 dwarf2_add_field (&fi, child_die, cu);
c906108c 6804 }
8713b1b1 6805 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6806 {
6807 /* C++ member function. */
e7c27a73 6808 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6809 }
6810 else if (child_die->tag == DW_TAG_inheritance)
6811 {
6812 /* C++ base class field. */
e7c27a73 6813 dwarf2_add_field (&fi, child_die, cu);
c906108c 6814 }
98751a41
JK
6815 else if (child_die->tag == DW_TAG_typedef)
6816 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6817 else if (child_die->tag == DW_TAG_template_type_param
6818 || child_die->tag == DW_TAG_template_value_param)
6819 {
6820 struct symbol *arg = new_symbol (child_die, NULL, cu);
6821
f1078f66
DJ
6822 if (arg != NULL)
6823 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6824 }
6825
c906108c
SS
6826 child_die = sibling_die (child_die);
6827 }
6828
34eaf542
TT
6829 /* Attach template arguments to type. */
6830 if (! VEC_empty (symbolp, template_args))
6831 {
6832 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6833 TYPE_N_TEMPLATE_ARGUMENTS (type)
6834 = VEC_length (symbolp, template_args);
6835 TYPE_TEMPLATE_ARGUMENTS (type)
6836 = obstack_alloc (&objfile->objfile_obstack,
6837 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6838 * sizeof (struct symbol *)));
6839 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6840 VEC_address (symbolp, template_args),
6841 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6842 * sizeof (struct symbol *)));
6843 VEC_free (symbolp, template_args);
6844 }
6845
c906108c
SS
6846 /* Attach fields and member functions to the type. */
6847 if (fi.nfields)
e7c27a73 6848 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6849 if (fi.nfnfields)
6850 {
e7c27a73 6851 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6852
c5aa993b 6853 /* Get the type which refers to the base class (possibly this
c906108c 6854 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6855 class from the DW_AT_containing_type attribute. This use of
6856 DW_AT_containing_type is a GNU extension. */
c906108c 6857
e142c38c 6858 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6859 {
e7c27a73 6860 struct type *t = die_containing_type (die, cu);
c906108c
SS
6861
6862 TYPE_VPTR_BASETYPE (type) = t;
6863 if (type == t)
6864 {
c906108c
SS
6865 int i;
6866
6867 /* Our own class provides vtbl ptr. */
6868 for (i = TYPE_NFIELDS (t) - 1;
6869 i >= TYPE_N_BASECLASSES (t);
6870 --i)
6871 {
6872 char *fieldname = TYPE_FIELD_NAME (t, i);
6873
1168df01 6874 if (is_vtable_name (fieldname, cu))
c906108c
SS
6875 {
6876 TYPE_VPTR_FIELDNO (type) = i;
6877 break;
6878 }
6879 }
6880
6881 /* Complain if virtual function table field not found. */
6882 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6883 complaint (&symfile_complaints,
e2e0b3e5 6884 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6885 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6886 "");
c906108c
SS
6887 }
6888 else
6889 {
6890 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6891 }
6892 }
f6235d4c
EZ
6893 else if (cu->producer
6894 && strncmp (cu->producer,
6895 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6896 {
6897 /* The IBM XLC compiler does not provide direct indication
6898 of the containing type, but the vtable pointer is
6899 always named __vfp. */
6900
6901 int i;
6902
6903 for (i = TYPE_NFIELDS (type) - 1;
6904 i >= TYPE_N_BASECLASSES (type);
6905 --i)
6906 {
6907 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6908 {
6909 TYPE_VPTR_FIELDNO (type) = i;
6910 TYPE_VPTR_BASETYPE (type) = type;
6911 break;
6912 }
6913 }
6914 }
c906108c 6915 }
98751a41
JK
6916
6917 /* Copy fi.typedef_field_list linked list elements content into the
6918 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6919 if (fi.typedef_field_list)
6920 {
6921 int i = fi.typedef_field_list_count;
6922
a0d7a4ff 6923 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6924 TYPE_TYPEDEF_FIELD_ARRAY (type)
6925 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6926 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6927
6928 /* Reverse the list order to keep the debug info elements order. */
6929 while (--i >= 0)
6930 {
6931 struct typedef_field *dest, *src;
6e70227d 6932
98751a41
JK
6933 dest = &TYPE_TYPEDEF_FIELD (type, i);
6934 src = &fi.typedef_field_list->field;
6935 fi.typedef_field_list = fi.typedef_field_list->next;
6936 *dest = *src;
6937 }
6938 }
c767944b
DJ
6939
6940 do_cleanups (back_to);
c906108c 6941 }
63d06c5c 6942
0b92b5bb
TT
6943 quirk_gcc_member_function_pointer (type, cu->objfile);
6944
90aeadfc
DC
6945 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6946 snapshots) has been known to create a die giving a declaration
6947 for a class that has, as a child, a die giving a definition for a
6948 nested class. So we have to process our children even if the
6949 current die is a declaration. Normally, of course, a declaration
6950 won't have any children at all. */
134d01f1 6951
90aeadfc
DC
6952 while (child_die != NULL && child_die->tag)
6953 {
6954 if (child_die->tag == DW_TAG_member
6955 || child_die->tag == DW_TAG_variable
34eaf542
TT
6956 || child_die->tag == DW_TAG_inheritance
6957 || child_die->tag == DW_TAG_template_value_param
6958 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6959 {
90aeadfc 6960 /* Do nothing. */
134d01f1 6961 }
90aeadfc
DC
6962 else
6963 process_die (child_die, cu);
134d01f1 6964
90aeadfc 6965 child_die = sibling_die (child_die);
134d01f1
DJ
6966 }
6967
fa4028e9
JB
6968 /* Do not consider external references. According to the DWARF standard,
6969 these DIEs are identified by the fact that they have no byte_size
6970 attribute, and a declaration attribute. */
6971 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6972 || !die_is_declaration (die, cu))
c767944b 6973 new_symbol (die, type, cu);
134d01f1
DJ
6974}
6975
6976/* Given a DW_AT_enumeration_type die, set its type. We do not
6977 complete the type's fields yet, or create any symbols. */
c906108c 6978
f792889a 6979static struct type *
134d01f1 6980read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6981{
e7c27a73 6982 struct objfile *objfile = cu->objfile;
c906108c 6983 struct type *type;
c906108c 6984 struct attribute *attr;
0114d602 6985 const char *name;
134d01f1 6986
348e048f
DE
6987 /* If the definition of this type lives in .debug_types, read that type.
6988 Don't follow DW_AT_specification though, that will take us back up
6989 the chain and we want to go down. */
6990 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6991 if (attr)
6992 {
6993 struct dwarf2_cu *type_cu = cu;
6994 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6995
348e048f 6996 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6997
6998 /* TYPE_CU may not be the same as CU.
6999 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7000 return set_die_type (die, type, cu);
7001 }
7002
c906108c
SS
7003 type = alloc_type (objfile);
7004
7005 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7006 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7007 if (name != NULL)
0114d602 7008 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7009
e142c38c 7010 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7011 if (attr)
7012 {
7013 TYPE_LENGTH (type) = DW_UNSND (attr);
7014 }
7015 else
7016 {
7017 TYPE_LENGTH (type) = 0;
7018 }
7019
137033e9
JB
7020 /* The enumeration DIE can be incomplete. In Ada, any type can be
7021 declared as private in the package spec, and then defined only
7022 inside the package body. Such types are known as Taft Amendment
7023 Types. When another package uses such a type, an incomplete DIE
7024 may be generated by the compiler. */
02eb380e 7025 if (die_is_declaration (die, cu))
876cecd0 7026 TYPE_STUB (type) = 1;
02eb380e 7027
f792889a 7028 return set_die_type (die, type, cu);
134d01f1
DJ
7029}
7030
7031/* Given a pointer to a die which begins an enumeration, process all
7032 the dies that define the members of the enumeration, and create the
7033 symbol for the enumeration type.
7034
7035 NOTE: We reverse the order of the element list. */
7036
7037static void
7038process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7039{
f792889a 7040 struct type *this_type;
134d01f1 7041
f792889a
DJ
7042 this_type = get_die_type (die, cu);
7043 if (this_type == NULL)
7044 this_type = read_enumeration_type (die, cu);
9dc481d3 7045
639d11d3 7046 if (die->child != NULL)
c906108c 7047 {
9dc481d3
DE
7048 struct die_info *child_die;
7049 struct symbol *sym;
7050 struct field *fields = NULL;
7051 int num_fields = 0;
7052 int unsigned_enum = 1;
7053 char *name;
7054
639d11d3 7055 child_die = die->child;
c906108c
SS
7056 while (child_die && child_die->tag)
7057 {
7058 if (child_die->tag != DW_TAG_enumerator)
7059 {
e7c27a73 7060 process_die (child_die, cu);
c906108c
SS
7061 }
7062 else
7063 {
39cbfefa
DJ
7064 name = dwarf2_name (child_die, cu);
7065 if (name)
c906108c 7066 {
f792889a 7067 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7068 if (SYMBOL_VALUE (sym) < 0)
7069 unsigned_enum = 0;
7070
7071 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7072 {
7073 fields = (struct field *)
7074 xrealloc (fields,
7075 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7076 * sizeof (struct field));
c906108c
SS
7077 }
7078
3567439c 7079 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7080 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7081 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7082 FIELD_BITSIZE (fields[num_fields]) = 0;
7083
7084 num_fields++;
7085 }
7086 }
7087
7088 child_die = sibling_die (child_die);
7089 }
7090
7091 if (num_fields)
7092 {
f792889a
DJ
7093 TYPE_NFIELDS (this_type) = num_fields;
7094 TYPE_FIELDS (this_type) = (struct field *)
7095 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7096 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7097 sizeof (struct field) * num_fields);
b8c9b27d 7098 xfree (fields);
c906108c
SS
7099 }
7100 if (unsigned_enum)
876cecd0 7101 TYPE_UNSIGNED (this_type) = 1;
c906108c 7102 }
134d01f1 7103
f792889a 7104 new_symbol (die, this_type, cu);
c906108c
SS
7105}
7106
7107/* Extract all information from a DW_TAG_array_type DIE and put it in
7108 the DIE's type field. For now, this only handles one dimensional
7109 arrays. */
7110
f792889a 7111static struct type *
e7c27a73 7112read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7113{
e7c27a73 7114 struct objfile *objfile = cu->objfile;
c906108c 7115 struct die_info *child_die;
7e314c57 7116 struct type *type;
c906108c
SS
7117 struct type *element_type, *range_type, *index_type;
7118 struct type **range_types = NULL;
7119 struct attribute *attr;
7120 int ndim = 0;
7121 struct cleanup *back_to;
39cbfefa 7122 char *name;
c906108c 7123
e7c27a73 7124 element_type = die_type (die, cu);
c906108c 7125
7e314c57
JK
7126 /* The die_type call above may have already set the type for this DIE. */
7127 type = get_die_type (die, cu);
7128 if (type)
7129 return type;
7130
c906108c
SS
7131 /* Irix 6.2 native cc creates array types without children for
7132 arrays with unspecified length. */
639d11d3 7133 if (die->child == NULL)
c906108c 7134 {
46bf5051 7135 index_type = objfile_type (objfile)->builtin_int;
c906108c 7136 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7137 type = create_array_type (NULL, element_type, range_type);
7138 return set_die_type (die, type, cu);
c906108c
SS
7139 }
7140
7141 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7142 child_die = die->child;
c906108c
SS
7143 while (child_die && child_die->tag)
7144 {
7145 if (child_die->tag == DW_TAG_subrange_type)
7146 {
f792889a 7147 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7148
f792889a 7149 if (child_type != NULL)
a02abb62
JB
7150 {
7151 /* The range type was succesfully read. Save it for
7152 the array type creation. */
7153 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7154 {
7155 range_types = (struct type **)
7156 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7157 * sizeof (struct type *));
7158 if (ndim == 0)
7159 make_cleanup (free_current_contents, &range_types);
7160 }
f792889a 7161 range_types[ndim++] = child_type;
a02abb62 7162 }
c906108c
SS
7163 }
7164 child_die = sibling_die (child_die);
7165 }
7166
7167 /* Dwarf2 dimensions are output from left to right, create the
7168 necessary array types in backwards order. */
7ca2d3a3 7169
c906108c 7170 type = element_type;
7ca2d3a3
DL
7171
7172 if (read_array_order (die, cu) == DW_ORD_col_major)
7173 {
7174 int i = 0;
9a619af0 7175
7ca2d3a3
DL
7176 while (i < ndim)
7177 type = create_array_type (NULL, type, range_types[i++]);
7178 }
7179 else
7180 {
7181 while (ndim-- > 0)
7182 type = create_array_type (NULL, type, range_types[ndim]);
7183 }
c906108c 7184
f5f8a009
EZ
7185 /* Understand Dwarf2 support for vector types (like they occur on
7186 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7187 array type. This is not part of the Dwarf2/3 standard yet, but a
7188 custom vendor extension. The main difference between a regular
7189 array and the vector variant is that vectors are passed by value
7190 to functions. */
e142c38c 7191 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7192 if (attr)
ea37ba09 7193 make_vector_type (type);
f5f8a009 7194
dbc98a8b
KW
7195 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7196 implementation may choose to implement triple vectors using this
7197 attribute. */
7198 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7199 if (attr)
7200 {
7201 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7202 TYPE_LENGTH (type) = DW_UNSND (attr);
7203 else
7204 complaint (&symfile_complaints, _("\
7205DW_AT_byte_size for array type smaller than the total size of elements"));
7206 }
7207
39cbfefa
DJ
7208 name = dwarf2_name (die, cu);
7209 if (name)
7210 TYPE_NAME (type) = name;
6e70227d 7211
7e314c57
JK
7212 /* Install the type in the die. */
7213 set_die_type (die, type, cu);
7214
7215 /* set_die_type should be already done. */
b4ba55a1
JB
7216 set_descriptive_type (type, die, cu);
7217
c906108c
SS
7218 do_cleanups (back_to);
7219
7e314c57 7220 return type;
c906108c
SS
7221}
7222
7ca2d3a3 7223static enum dwarf_array_dim_ordering
6e70227d 7224read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7225{
7226 struct attribute *attr;
7227
7228 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7229
7230 if (attr) return DW_SND (attr);
7231
7232 /*
7233 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7234 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7235 laid out as per normal fortran.
7236
6e70227d 7237 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7238 version checking.
7239 */
7240
905e0470
PM
7241 if (cu->language == language_fortran
7242 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7243 {
7244 return DW_ORD_row_major;
7245 }
7246
6e70227d 7247 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7248 {
7249 case array_column_major:
7250 return DW_ORD_col_major;
7251 case array_row_major:
7252 default:
7253 return DW_ORD_row_major;
7254 };
7255}
7256
72019c9c
GM
7257/* Extract all information from a DW_TAG_set_type DIE and put it in
7258 the DIE's type field. */
7259
f792889a 7260static struct type *
72019c9c
GM
7261read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7262{
7e314c57
JK
7263 struct type *domain_type, *set_type;
7264 struct attribute *attr;
f792889a 7265
7e314c57
JK
7266 domain_type = die_type (die, cu);
7267
7268 /* The die_type call above may have already set the type for this DIE. */
7269 set_type = get_die_type (die, cu);
7270 if (set_type)
7271 return set_type;
7272
7273 set_type = create_set_type (NULL, domain_type);
7274
7275 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7276 if (attr)
7277 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7278
f792889a 7279 return set_die_type (die, set_type, cu);
72019c9c 7280}
7ca2d3a3 7281
c906108c
SS
7282/* First cut: install each common block member as a global variable. */
7283
7284static void
e7c27a73 7285read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7286{
7287 struct die_info *child_die;
7288 struct attribute *attr;
7289 struct symbol *sym;
7290 CORE_ADDR base = (CORE_ADDR) 0;
7291
e142c38c 7292 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7293 if (attr)
7294 {
8e19ed76
PS
7295 /* Support the .debug_loc offsets */
7296 if (attr_form_is_block (attr))
7297 {
e7c27a73 7298 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7299 }
3690dd37 7300 else if (attr_form_is_section_offset (attr))
8e19ed76 7301 {
4d3c2250 7302 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7303 }
7304 else
7305 {
4d3c2250
KB
7306 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7307 "common block member");
8e19ed76 7308 }
c906108c 7309 }
639d11d3 7310 if (die->child != NULL)
c906108c 7311 {
639d11d3 7312 child_die = die->child;
c906108c
SS
7313 while (child_die && child_die->tag)
7314 {
e7c27a73 7315 sym = new_symbol (child_die, NULL, cu);
e142c38c 7316 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7317 if (sym != NULL && attr != NULL)
c906108c 7318 {
d4b96c9a
JK
7319 CORE_ADDR byte_offset = 0;
7320
7321 if (attr_form_is_section_offset (attr))
7322 dwarf2_complex_location_expr_complaint ();
7323 else if (attr_form_is_constant (attr))
7324 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7325 else if (attr_form_is_block (attr))
7326 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7327 else
7328 dwarf2_complex_location_expr_complaint ();
7329
7330 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7331 add_symbol_to_list (sym, &global_symbols);
7332 }
7333 child_die = sibling_die (child_die);
7334 }
7335 }
7336}
7337
0114d602 7338/* Create a type for a C++ namespace. */
d9fa45fe 7339
0114d602
DJ
7340static struct type *
7341read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7342{
e7c27a73 7343 struct objfile *objfile = cu->objfile;
0114d602 7344 const char *previous_prefix, *name;
9219021c 7345 int is_anonymous;
0114d602
DJ
7346 struct type *type;
7347
7348 /* For extensions, reuse the type of the original namespace. */
7349 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7350 {
7351 struct die_info *ext_die;
7352 struct dwarf2_cu *ext_cu = cu;
9a619af0 7353
0114d602
DJ
7354 ext_die = dwarf2_extension (die, &ext_cu);
7355 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7356
7357 /* EXT_CU may not be the same as CU.
7358 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7359 return set_die_type (die, type, cu);
7360 }
9219021c 7361
e142c38c 7362 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7363
7364 /* Now build the name of the current namespace. */
7365
0114d602
DJ
7366 previous_prefix = determine_prefix (die, cu);
7367 if (previous_prefix[0] != '\0')
7368 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7369 previous_prefix, name, 0, cu);
0114d602
DJ
7370
7371 /* Create the type. */
7372 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7373 objfile);
7374 TYPE_NAME (type) = (char *) name;
7375 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7376
60531b24 7377 return set_die_type (die, type, cu);
0114d602
DJ
7378}
7379
7380/* Read a C++ namespace. */
7381
7382static void
7383read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7384{
7385 struct objfile *objfile = cu->objfile;
7386 const char *name;
7387 int is_anonymous;
9219021c 7388
5c4e30ca
DC
7389 /* Add a symbol associated to this if we haven't seen the namespace
7390 before. Also, add a using directive if it's an anonymous
7391 namespace. */
9219021c 7392
f2f0e013 7393 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7394 {
7395 struct type *type;
7396
0114d602 7397 type = read_type_die (die, cu);
e7c27a73 7398 new_symbol (die, type, cu);
5c4e30ca 7399
0114d602 7400 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7401 if (is_anonymous)
0114d602
DJ
7402 {
7403 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7404
c0cc3a76 7405 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7406 NULL, &objfile->objfile_obstack);
0114d602 7407 }
5c4e30ca 7408 }
9219021c 7409
639d11d3 7410 if (die->child != NULL)
d9fa45fe 7411 {
639d11d3 7412 struct die_info *child_die = die->child;
6e70227d 7413
d9fa45fe
DC
7414 while (child_die && child_die->tag)
7415 {
e7c27a73 7416 process_die (child_die, cu);
d9fa45fe
DC
7417 child_die = sibling_die (child_die);
7418 }
7419 }
38d518c9
EZ
7420}
7421
f55ee35c
JK
7422/* Read a Fortran module as type. This DIE can be only a declaration used for
7423 imported module. Still we need that type as local Fortran "use ... only"
7424 declaration imports depend on the created type in determine_prefix. */
7425
7426static struct type *
7427read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7428{
7429 struct objfile *objfile = cu->objfile;
7430 char *module_name;
7431 struct type *type;
7432
7433 module_name = dwarf2_name (die, cu);
7434 if (!module_name)
7435 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7436 die->offset);
7437 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7438
7439 /* determine_prefix uses TYPE_TAG_NAME. */
7440 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7441
7442 return set_die_type (die, type, cu);
7443}
7444
5d7cb8df
JK
7445/* Read a Fortran module. */
7446
7447static void
7448read_module (struct die_info *die, struct dwarf2_cu *cu)
7449{
7450 struct die_info *child_die = die->child;
7451
5d7cb8df
JK
7452 while (child_die && child_die->tag)
7453 {
7454 process_die (child_die, cu);
7455 child_die = sibling_die (child_die);
7456 }
7457}
7458
38d518c9
EZ
7459/* Return the name of the namespace represented by DIE. Set
7460 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7461 namespace. */
7462
7463static const char *
e142c38c 7464namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7465{
7466 struct die_info *current_die;
7467 const char *name = NULL;
7468
7469 /* Loop through the extensions until we find a name. */
7470
7471 for (current_die = die;
7472 current_die != NULL;
f2f0e013 7473 current_die = dwarf2_extension (die, &cu))
38d518c9 7474 {
e142c38c 7475 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7476 if (name != NULL)
7477 break;
7478 }
7479
7480 /* Is it an anonymous namespace? */
7481
7482 *is_anonymous = (name == NULL);
7483 if (*is_anonymous)
7484 name = "(anonymous namespace)";
7485
7486 return name;
d9fa45fe
DC
7487}
7488
c906108c
SS
7489/* Extract all information from a DW_TAG_pointer_type DIE and add to
7490 the user defined type vector. */
7491
f792889a 7492static struct type *
e7c27a73 7493read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7494{
5e2b427d 7495 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7496 struct comp_unit_head *cu_header = &cu->header;
c906108c 7497 struct type *type;
8b2dbe47
KB
7498 struct attribute *attr_byte_size;
7499 struct attribute *attr_address_class;
7500 int byte_size, addr_class;
7e314c57
JK
7501 struct type *target_type;
7502
7503 target_type = die_type (die, cu);
c906108c 7504
7e314c57
JK
7505 /* The die_type call above may have already set the type for this DIE. */
7506 type = get_die_type (die, cu);
7507 if (type)
7508 return type;
7509
7510 type = lookup_pointer_type (target_type);
8b2dbe47 7511
e142c38c 7512 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7513 if (attr_byte_size)
7514 byte_size = DW_UNSND (attr_byte_size);
c906108c 7515 else
8b2dbe47
KB
7516 byte_size = cu_header->addr_size;
7517
e142c38c 7518 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7519 if (attr_address_class)
7520 addr_class = DW_UNSND (attr_address_class);
7521 else
7522 addr_class = DW_ADDR_none;
7523
7524 /* If the pointer size or address class is different than the
7525 default, create a type variant marked as such and set the
7526 length accordingly. */
7527 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7528 {
5e2b427d 7529 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7530 {
7531 int type_flags;
7532
849957d9 7533 type_flags = gdbarch_address_class_type_flags
5e2b427d 7534 (gdbarch, byte_size, addr_class);
876cecd0
TT
7535 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7536 == 0);
8b2dbe47
KB
7537 type = make_type_with_address_space (type, type_flags);
7538 }
7539 else if (TYPE_LENGTH (type) != byte_size)
7540 {
e2e0b3e5 7541 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7542 }
6e70227d 7543 else
9a619af0
MS
7544 {
7545 /* Should we also complain about unhandled address classes? */
7546 }
c906108c 7547 }
8b2dbe47
KB
7548
7549 TYPE_LENGTH (type) = byte_size;
f792889a 7550 return set_die_type (die, type, cu);
c906108c
SS
7551}
7552
7553/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7554 the user defined type vector. */
7555
f792889a 7556static struct type *
e7c27a73 7557read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7558{
7559 struct type *type;
7560 struct type *to_type;
7561 struct type *domain;
7562
e7c27a73
DJ
7563 to_type = die_type (die, cu);
7564 domain = die_containing_type (die, cu);
0d5de010 7565
7e314c57
JK
7566 /* The calls above may have already set the type for this DIE. */
7567 type = get_die_type (die, cu);
7568 if (type)
7569 return type;
7570
0d5de010
DJ
7571 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7572 type = lookup_methodptr_type (to_type);
7573 else
7574 type = lookup_memberptr_type (to_type, domain);
c906108c 7575
f792889a 7576 return set_die_type (die, type, cu);
c906108c
SS
7577}
7578
7579/* Extract all information from a DW_TAG_reference_type DIE and add to
7580 the user defined type vector. */
7581
f792889a 7582static struct type *
e7c27a73 7583read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7584{
e7c27a73 7585 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7586 struct type *type, *target_type;
c906108c
SS
7587 struct attribute *attr;
7588
7e314c57
JK
7589 target_type = die_type (die, cu);
7590
7591 /* The die_type call above may have already set the type for this DIE. */
7592 type = get_die_type (die, cu);
7593 if (type)
7594 return type;
7595
7596 type = lookup_reference_type (target_type);
e142c38c 7597 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7598 if (attr)
7599 {
7600 TYPE_LENGTH (type) = DW_UNSND (attr);
7601 }
7602 else
7603 {
107d2387 7604 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7605 }
f792889a 7606 return set_die_type (die, type, cu);
c906108c
SS
7607}
7608
f792889a 7609static struct type *
e7c27a73 7610read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7611{
f792889a 7612 struct type *base_type, *cv_type;
c906108c 7613
e7c27a73 7614 base_type = die_type (die, cu);
7e314c57
JK
7615
7616 /* The die_type call above may have already set the type for this DIE. */
7617 cv_type = get_die_type (die, cu);
7618 if (cv_type)
7619 return cv_type;
7620
2f608a3a
KW
7621 /* In case the const qualifier is applied to an array type, the element type
7622 is so qualified, not the array type (section 6.7.3 of C99). */
7623 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7624 {
7625 struct type *el_type, *inner_array;
7626
7627 base_type = copy_type (base_type);
7628 inner_array = base_type;
7629
7630 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7631 {
7632 TYPE_TARGET_TYPE (inner_array) =
7633 copy_type (TYPE_TARGET_TYPE (inner_array));
7634 inner_array = TYPE_TARGET_TYPE (inner_array);
7635 }
7636
7637 el_type = TYPE_TARGET_TYPE (inner_array);
7638 TYPE_TARGET_TYPE (inner_array) =
7639 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7640
7641 return set_die_type (die, base_type, cu);
7642 }
7643
f792889a
DJ
7644 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7645 return set_die_type (die, cv_type, cu);
c906108c
SS
7646}
7647
f792889a 7648static struct type *
e7c27a73 7649read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7650{
f792889a 7651 struct type *base_type, *cv_type;
c906108c 7652
e7c27a73 7653 base_type = die_type (die, cu);
7e314c57
JK
7654
7655 /* The die_type call above may have already set the type for this DIE. */
7656 cv_type = get_die_type (die, cu);
7657 if (cv_type)
7658 return cv_type;
7659
f792889a
DJ
7660 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7661 return set_die_type (die, cv_type, cu);
c906108c
SS
7662}
7663
7664/* Extract all information from a DW_TAG_string_type DIE and add to
7665 the user defined type vector. It isn't really a user defined type,
7666 but it behaves like one, with other DIE's using an AT_user_def_type
7667 attribute to reference it. */
7668
f792889a 7669static struct type *
e7c27a73 7670read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7671{
e7c27a73 7672 struct objfile *objfile = cu->objfile;
3b7538c0 7673 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7674 struct type *type, *range_type, *index_type, *char_type;
7675 struct attribute *attr;
7676 unsigned int length;
7677
e142c38c 7678 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7679 if (attr)
7680 {
7681 length = DW_UNSND (attr);
7682 }
7683 else
7684 {
b21b22e0 7685 /* check for the DW_AT_byte_size attribute */
e142c38c 7686 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7687 if (attr)
7688 {
7689 length = DW_UNSND (attr);
7690 }
7691 else
7692 {
7693 length = 1;
7694 }
c906108c 7695 }
6ccb9162 7696
46bf5051 7697 index_type = objfile_type (objfile)->builtin_int;
c906108c 7698 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7699 char_type = language_string_char_type (cu->language_defn, gdbarch);
7700 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7701
f792889a 7702 return set_die_type (die, type, cu);
c906108c
SS
7703}
7704
7705/* Handle DIES due to C code like:
7706
7707 struct foo
c5aa993b
JM
7708 {
7709 int (*funcp)(int a, long l);
7710 int b;
7711 };
c906108c
SS
7712
7713 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7714 */
c906108c 7715
f792889a 7716static struct type *
e7c27a73 7717read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7718{
7719 struct type *type; /* Type that this function returns */
7720 struct type *ftype; /* Function that returns above type */
7721 struct attribute *attr;
7722
e7c27a73 7723 type = die_type (die, cu);
7e314c57
JK
7724
7725 /* The die_type call above may have already set the type for this DIE. */
7726 ftype = get_die_type (die, cu);
7727 if (ftype)
7728 return ftype;
7729
0c8b41f1 7730 ftype = lookup_function_type (type);
c906108c 7731
5b8101ae 7732 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7733 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7734 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7735 || cu->language == language_cplus
5b8101ae
PM
7736 || cu->language == language_java
7737 || cu->language == language_pascal)
876cecd0 7738 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7739 else if (producer_is_realview (cu->producer))
7740 /* RealView does not emit DW_AT_prototyped. We can not
7741 distinguish prototyped and unprototyped functions; default to
7742 prototyped, since that is more common in modern code (and
7743 RealView warns about unprototyped functions). */
7744 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7745
c055b101
CV
7746 /* Store the calling convention in the type if it's available in
7747 the subroutine die. Otherwise set the calling convention to
7748 the default value DW_CC_normal. */
7749 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7750 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7751
7752 /* We need to add the subroutine type to the die immediately so
7753 we don't infinitely recurse when dealing with parameters
7754 declared as the same subroutine type. */
7755 set_die_type (die, ftype, cu);
6e70227d 7756
639d11d3 7757 if (die->child != NULL)
c906108c 7758 {
8072405b 7759 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7760 struct die_info *child_die;
8072405b 7761 int nparams, iparams;
c906108c
SS
7762
7763 /* Count the number of parameters.
7764 FIXME: GDB currently ignores vararg functions, but knows about
7765 vararg member functions. */
8072405b 7766 nparams = 0;
639d11d3 7767 child_die = die->child;
c906108c
SS
7768 while (child_die && child_die->tag)
7769 {
7770 if (child_die->tag == DW_TAG_formal_parameter)
7771 nparams++;
7772 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7773 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7774 child_die = sibling_die (child_die);
7775 }
7776
7777 /* Allocate storage for parameters and fill them in. */
7778 TYPE_NFIELDS (ftype) = nparams;
7779 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7780 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7781
8072405b
JK
7782 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7783 even if we error out during the parameters reading below. */
7784 for (iparams = 0; iparams < nparams; iparams++)
7785 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7786
7787 iparams = 0;
639d11d3 7788 child_die = die->child;
c906108c
SS
7789 while (child_die && child_die->tag)
7790 {
7791 if (child_die->tag == DW_TAG_formal_parameter)
7792 {
3ce3b1ba
PA
7793 struct type *arg_type;
7794
7795 /* DWARF version 2 has no clean way to discern C++
7796 static and non-static member functions. G++ helps
7797 GDB by marking the first parameter for non-static
7798 member functions (which is the this pointer) as
7799 artificial. We pass this information to
7800 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7801
7802 DWARF version 3 added DW_AT_object_pointer, which GCC
7803 4.5 does not yet generate. */
e142c38c 7804 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7805 if (attr)
7806 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7807 else
418835cc
KS
7808 {
7809 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7810
7811 /* GCC/43521: In java, the formal parameter
7812 "this" is sometimes not marked with DW_AT_artificial. */
7813 if (cu->language == language_java)
7814 {
7815 const char *name = dwarf2_name (child_die, cu);
9a619af0 7816
418835cc
KS
7817 if (name && !strcmp (name, "this"))
7818 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7819 }
7820 }
3ce3b1ba
PA
7821 arg_type = die_type (child_die, cu);
7822
7823 /* RealView does not mark THIS as const, which the testsuite
7824 expects. GCC marks THIS as const in method definitions,
7825 but not in the class specifications (GCC PR 43053). */
7826 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7827 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7828 {
7829 int is_this = 0;
7830 struct dwarf2_cu *arg_cu = cu;
7831 const char *name = dwarf2_name (child_die, cu);
7832
7833 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7834 if (attr)
7835 {
7836 /* If the compiler emits this, use it. */
7837 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7838 is_this = 1;
7839 }
7840 else if (name && strcmp (name, "this") == 0)
7841 /* Function definitions will have the argument names. */
7842 is_this = 1;
7843 else if (name == NULL && iparams == 0)
7844 /* Declarations may not have the names, so like
7845 elsewhere in GDB, assume an artificial first
7846 argument is "this". */
7847 is_this = 1;
7848
7849 if (is_this)
7850 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7851 arg_type, 0);
7852 }
7853
7854 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7855 iparams++;
7856 }
7857 child_die = sibling_die (child_die);
7858 }
7859 }
7860
76c10ea2 7861 return ftype;
c906108c
SS
7862}
7863
f792889a 7864static struct type *
e7c27a73 7865read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7866{
e7c27a73 7867 struct objfile *objfile = cu->objfile;
0114d602 7868 const char *name = NULL;
f792889a 7869 struct type *this_type;
c906108c 7870
94af9270 7871 name = dwarf2_full_name (NULL, die, cu);
f792889a 7872 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7873 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7874 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7875 set_die_type (die, this_type, cu);
7876 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7877 return this_type;
c906108c
SS
7878}
7879
7880/* Find a representation of a given base type and install
7881 it in the TYPE field of the die. */
7882
f792889a 7883static struct type *
e7c27a73 7884read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7885{
e7c27a73 7886 struct objfile *objfile = cu->objfile;
c906108c
SS
7887 struct type *type;
7888 struct attribute *attr;
7889 int encoding = 0, size = 0;
39cbfefa 7890 char *name;
6ccb9162
UW
7891 enum type_code code = TYPE_CODE_INT;
7892 int type_flags = 0;
7893 struct type *target_type = NULL;
c906108c 7894
e142c38c 7895 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7896 if (attr)
7897 {
7898 encoding = DW_UNSND (attr);
7899 }
e142c38c 7900 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7901 if (attr)
7902 {
7903 size = DW_UNSND (attr);
7904 }
39cbfefa 7905 name = dwarf2_name (die, cu);
6ccb9162 7906 if (!name)
c906108c 7907 {
6ccb9162
UW
7908 complaint (&symfile_complaints,
7909 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7910 }
6ccb9162
UW
7911
7912 switch (encoding)
c906108c 7913 {
6ccb9162
UW
7914 case DW_ATE_address:
7915 /* Turn DW_ATE_address into a void * pointer. */
7916 code = TYPE_CODE_PTR;
7917 type_flags |= TYPE_FLAG_UNSIGNED;
7918 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7919 break;
7920 case DW_ATE_boolean:
7921 code = TYPE_CODE_BOOL;
7922 type_flags |= TYPE_FLAG_UNSIGNED;
7923 break;
7924 case DW_ATE_complex_float:
7925 code = TYPE_CODE_COMPLEX;
7926 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7927 break;
7928 case DW_ATE_decimal_float:
7929 code = TYPE_CODE_DECFLOAT;
7930 break;
7931 case DW_ATE_float:
7932 code = TYPE_CODE_FLT;
7933 break;
7934 case DW_ATE_signed:
7935 break;
7936 case DW_ATE_unsigned:
7937 type_flags |= TYPE_FLAG_UNSIGNED;
7938 break;
7939 case DW_ATE_signed_char:
6e70227d 7940 if (cu->language == language_ada || cu->language == language_m2
868a0084 7941 || cu->language == language_pascal)
6ccb9162
UW
7942 code = TYPE_CODE_CHAR;
7943 break;
7944 case DW_ATE_unsigned_char:
868a0084
PM
7945 if (cu->language == language_ada || cu->language == language_m2
7946 || cu->language == language_pascal)
6ccb9162
UW
7947 code = TYPE_CODE_CHAR;
7948 type_flags |= TYPE_FLAG_UNSIGNED;
7949 break;
75079b2b
TT
7950 case DW_ATE_UTF:
7951 /* We just treat this as an integer and then recognize the
7952 type by name elsewhere. */
7953 break;
7954
6ccb9162
UW
7955 default:
7956 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7957 dwarf_type_encoding_name (encoding));
7958 break;
c906108c 7959 }
6ccb9162 7960
0114d602
DJ
7961 type = init_type (code, size, type_flags, NULL, objfile);
7962 TYPE_NAME (type) = name;
6ccb9162
UW
7963 TYPE_TARGET_TYPE (type) = target_type;
7964
0114d602 7965 if (name && strcmp (name, "char") == 0)
876cecd0 7966 TYPE_NOSIGN (type) = 1;
0114d602 7967
f792889a 7968 return set_die_type (die, type, cu);
c906108c
SS
7969}
7970
a02abb62
JB
7971/* Read the given DW_AT_subrange DIE. */
7972
f792889a 7973static struct type *
a02abb62
JB
7974read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7975{
5e2b427d 7976 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7977 struct type *base_type;
7978 struct type *range_type;
7979 struct attribute *attr;
43bbcdc2
PH
7980 LONGEST low = 0;
7981 LONGEST high = -1;
39cbfefa 7982 char *name;
43bbcdc2 7983 LONGEST negative_mask;
e77813c8 7984
a02abb62 7985 base_type = die_type (die, cu);
953ac07e
JK
7986 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7987 check_typedef (base_type);
a02abb62 7988
7e314c57
JK
7989 /* The die_type call above may have already set the type for this DIE. */
7990 range_type = get_die_type (die, cu);
7991 if (range_type)
7992 return range_type;
7993
e142c38c 7994 if (cu->language == language_fortran)
6e70227d 7995 {
a02abb62
JB
7996 /* FORTRAN implies a lower bound of 1, if not given. */
7997 low = 1;
7998 }
7999
dd5e6932
DJ
8000 /* FIXME: For variable sized arrays either of these could be
8001 a variable rather than a constant value. We'll allow it,
8002 but we don't know how to handle it. */
e142c38c 8003 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8004 if (attr)
8005 low = dwarf2_get_attr_constant_value (attr, 0);
8006
e142c38c 8007 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8008 if (attr)
6e70227d 8009 {
e77813c8 8010 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8011 {
8012 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8013 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8014 FIXME: GDB does not yet know how to handle dynamic
8015 arrays properly, treat them as arrays with unspecified
8016 length for now.
8017
8018 FIXME: jimb/2003-09-22: GDB does not really know
8019 how to handle arrays of unspecified length
8020 either; we just represent them as zero-length
8021 arrays. Choose an appropriate upper bound given
8022 the lower bound we've computed above. */
8023 high = low - 1;
8024 }
8025 else
8026 high = dwarf2_get_attr_constant_value (attr, 1);
8027 }
e77813c8
PM
8028 else
8029 {
8030 attr = dwarf2_attr (die, DW_AT_count, cu);
8031 if (attr)
8032 {
8033 int count = dwarf2_get_attr_constant_value (attr, 1);
8034 high = low + count - 1;
8035 }
8036 }
8037
8038 /* Dwarf-2 specifications explicitly allows to create subrange types
8039 without specifying a base type.
8040 In that case, the base type must be set to the type of
8041 the lower bound, upper bound or count, in that order, if any of these
8042 three attributes references an object that has a type.
8043 If no base type is found, the Dwarf-2 specifications say that
8044 a signed integer type of size equal to the size of an address should
8045 be used.
8046 For the following C code: `extern char gdb_int [];'
8047 GCC produces an empty range DIE.
8048 FIXME: muller/2010-05-28: Possible references to object for low bound,
8049 high bound or count are not yet handled by this code.
8050 */
8051 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8052 {
8053 struct objfile *objfile = cu->objfile;
8054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8055 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8056 struct type *int_type = objfile_type (objfile)->builtin_int;
8057
8058 /* Test "int", "long int", and "long long int" objfile types,
8059 and select the first one having a size above or equal to the
8060 architecture address size. */
8061 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8062 base_type = int_type;
8063 else
8064 {
8065 int_type = objfile_type (objfile)->builtin_long;
8066 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8067 base_type = int_type;
8068 else
8069 {
8070 int_type = objfile_type (objfile)->builtin_long_long;
8071 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8072 base_type = int_type;
8073 }
8074 }
8075 }
a02abb62 8076
6e70227d 8077 negative_mask =
43bbcdc2
PH
8078 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8079 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8080 low |= negative_mask;
8081 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8082 high |= negative_mask;
8083
a02abb62
JB
8084 range_type = create_range_type (NULL, base_type, low, high);
8085
bbb0eef6
JK
8086 /* Mark arrays with dynamic length at least as an array of unspecified
8087 length. GDB could check the boundary but before it gets implemented at
8088 least allow accessing the array elements. */
8089 if (attr && attr->form == DW_FORM_block1)
8090 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8091
39cbfefa
DJ
8092 name = dwarf2_name (die, cu);
8093 if (name)
8094 TYPE_NAME (range_type) = name;
6e70227d 8095
e142c38c 8096 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8097 if (attr)
8098 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8099
7e314c57
JK
8100 set_die_type (die, range_type, cu);
8101
8102 /* set_die_type should be already done. */
b4ba55a1
JB
8103 set_descriptive_type (range_type, die, cu);
8104
7e314c57 8105 return range_type;
a02abb62 8106}
6e70227d 8107
f792889a 8108static struct type *
81a17f79
JB
8109read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8110{
8111 struct type *type;
81a17f79 8112
81a17f79
JB
8113 /* For now, we only support the C meaning of an unspecified type: void. */
8114
0114d602
DJ
8115 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8116 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8117
f792889a 8118 return set_die_type (die, type, cu);
81a17f79 8119}
a02abb62 8120
51545339
DJ
8121/* Trivial hash function for die_info: the hash value of a DIE
8122 is its offset in .debug_info for this objfile. */
8123
8124static hashval_t
8125die_hash (const void *item)
8126{
8127 const struct die_info *die = item;
9a619af0 8128
51545339
DJ
8129 return die->offset;
8130}
8131
8132/* Trivial comparison function for die_info structures: two DIEs
8133 are equal if they have the same offset. */
8134
8135static int
8136die_eq (const void *item_lhs, const void *item_rhs)
8137{
8138 const struct die_info *die_lhs = item_lhs;
8139 const struct die_info *die_rhs = item_rhs;
9a619af0 8140
51545339
DJ
8141 return die_lhs->offset == die_rhs->offset;
8142}
8143
c906108c
SS
8144/* Read a whole compilation unit into a linked list of dies. */
8145
f9aca02d 8146static struct die_info *
93311388 8147read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8148{
93311388 8149 struct die_reader_specs reader_specs;
98bfdba5 8150 int read_abbrevs = 0;
1d9ec526 8151 struct cleanup *back_to = NULL;
98bfdba5
PA
8152 struct die_info *die;
8153
8154 if (cu->dwarf2_abbrevs == NULL)
8155 {
8156 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8157 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8158 read_abbrevs = 1;
8159 }
93311388 8160
348e048f 8161 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8162 cu->die_hash
8163 = htab_create_alloc_ex (cu->header.length / 12,
8164 die_hash,
8165 die_eq,
8166 NULL,
8167 &cu->comp_unit_obstack,
8168 hashtab_obstack_allocate,
8169 dummy_obstack_deallocate);
8170
93311388
DE
8171 init_cu_die_reader (&reader_specs, cu);
8172
98bfdba5
PA
8173 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8174
8175 if (read_abbrevs)
8176 do_cleanups (back_to);
8177
8178 return die;
639d11d3
DC
8179}
8180
d97bc12b
DE
8181/* Main entry point for reading a DIE and all children.
8182 Read the DIE and dump it if requested. */
8183
8184static struct die_info *
93311388
DE
8185read_die_and_children (const struct die_reader_specs *reader,
8186 gdb_byte *info_ptr,
d97bc12b
DE
8187 gdb_byte **new_info_ptr,
8188 struct die_info *parent)
8189{
93311388 8190 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8191 new_info_ptr, parent);
8192
8193 if (dwarf2_die_debug)
8194 {
348e048f
DE
8195 fprintf_unfiltered (gdb_stdlog,
8196 "\nRead die from %s of %s:\n",
8197 reader->buffer == dwarf2_per_objfile->info.buffer
8198 ? ".debug_info"
8199 : reader->buffer == dwarf2_per_objfile->types.buffer
8200 ? ".debug_types"
8201 : "unknown section",
8202 reader->abfd->filename);
d97bc12b
DE
8203 dump_die (result, dwarf2_die_debug);
8204 }
8205
8206 return result;
8207}
8208
639d11d3
DC
8209/* Read a single die and all its descendents. Set the die's sibling
8210 field to NULL; set other fields in the die correctly, and set all
8211 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8212 location of the info_ptr after reading all of those dies. PARENT
8213 is the parent of the die in question. */
8214
8215static struct die_info *
93311388
DE
8216read_die_and_children_1 (const struct die_reader_specs *reader,
8217 gdb_byte *info_ptr,
d97bc12b
DE
8218 gdb_byte **new_info_ptr,
8219 struct die_info *parent)
639d11d3
DC
8220{
8221 struct die_info *die;
fe1b8b76 8222 gdb_byte *cur_ptr;
639d11d3
DC
8223 int has_children;
8224
93311388 8225 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8226 if (die == NULL)
8227 {
8228 *new_info_ptr = cur_ptr;
8229 return NULL;
8230 }
93311388 8231 store_in_ref_table (die, reader->cu);
639d11d3
DC
8232
8233 if (has_children)
348e048f 8234 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8235 else
8236 {
8237 die->child = NULL;
8238 *new_info_ptr = cur_ptr;
8239 }
8240
8241 die->sibling = NULL;
8242 die->parent = parent;
8243 return die;
8244}
8245
8246/* Read a die, all of its descendents, and all of its siblings; set
8247 all of the fields of all of the dies correctly. Arguments are as
8248 in read_die_and_children. */
8249
8250static struct die_info *
93311388
DE
8251read_die_and_siblings (const struct die_reader_specs *reader,
8252 gdb_byte *info_ptr,
fe1b8b76 8253 gdb_byte **new_info_ptr,
639d11d3
DC
8254 struct die_info *parent)
8255{
8256 struct die_info *first_die, *last_sibling;
fe1b8b76 8257 gdb_byte *cur_ptr;
639d11d3 8258
c906108c 8259 cur_ptr = info_ptr;
639d11d3
DC
8260 first_die = last_sibling = NULL;
8261
8262 while (1)
c906108c 8263 {
639d11d3 8264 struct die_info *die
93311388 8265 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8266
1d325ec1 8267 if (die == NULL)
c906108c 8268 {
639d11d3
DC
8269 *new_info_ptr = cur_ptr;
8270 return first_die;
c906108c 8271 }
1d325ec1
DJ
8272
8273 if (!first_die)
8274 first_die = die;
c906108c 8275 else
1d325ec1
DJ
8276 last_sibling->sibling = die;
8277
8278 last_sibling = die;
c906108c 8279 }
c906108c
SS
8280}
8281
93311388
DE
8282/* Read the die from the .debug_info section buffer. Set DIEP to
8283 point to a newly allocated die with its information, except for its
8284 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8285 whether the die has children or not. */
8286
8287static gdb_byte *
8288read_full_die (const struct die_reader_specs *reader,
8289 struct die_info **diep, gdb_byte *info_ptr,
8290 int *has_children)
8291{
8292 unsigned int abbrev_number, bytes_read, i, offset;
8293 struct abbrev_info *abbrev;
8294 struct die_info *die;
8295 struct dwarf2_cu *cu = reader->cu;
8296 bfd *abfd = reader->abfd;
8297
8298 offset = info_ptr - reader->buffer;
8299 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8300 info_ptr += bytes_read;
8301 if (!abbrev_number)
8302 {
8303 *diep = NULL;
8304 *has_children = 0;
8305 return info_ptr;
8306 }
8307
8308 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8309 if (!abbrev)
348e048f
DE
8310 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8311 abbrev_number,
8312 bfd_get_filename (abfd));
8313
93311388
DE
8314 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8315 die->offset = offset;
8316 die->tag = abbrev->tag;
8317 die->abbrev = abbrev_number;
8318
8319 die->num_attrs = abbrev->num_attrs;
8320
8321 for (i = 0; i < abbrev->num_attrs; ++i)
8322 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8323 abfd, info_ptr, cu);
8324
8325 *diep = die;
8326 *has_children = abbrev->has_children;
8327 return info_ptr;
8328}
8329
c906108c
SS
8330/* In DWARF version 2, the description of the debugging information is
8331 stored in a separate .debug_abbrev section. Before we read any
8332 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8333 in a hash table. This function also sets flags in CU describing
8334 the data found in the abbrev table. */
c906108c
SS
8335
8336static void
e7c27a73 8337dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8338{
e7c27a73 8339 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8340 gdb_byte *abbrev_ptr;
c906108c
SS
8341 struct abbrev_info *cur_abbrev;
8342 unsigned int abbrev_number, bytes_read, abbrev_name;
8343 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8344 struct attr_abbrev *cur_attrs;
8345 unsigned int allocated_attrs;
c906108c 8346
57349743 8347 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8348 obstack_init (&cu->abbrev_obstack);
8349 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8350 (ABBREV_HASH_SIZE
8351 * sizeof (struct abbrev_info *)));
8352 memset (cu->dwarf2_abbrevs, 0,
8353 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8354
be391dca
TT
8355 dwarf2_read_section (dwarf2_per_objfile->objfile,
8356 &dwarf2_per_objfile->abbrev);
dce234bc 8357 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8358 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8359 abbrev_ptr += bytes_read;
8360
f3dd6933
DJ
8361 allocated_attrs = ATTR_ALLOC_CHUNK;
8362 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8363
c906108c
SS
8364 /* loop until we reach an abbrev number of 0 */
8365 while (abbrev_number)
8366 {
f3dd6933 8367 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8368
8369 /* read in abbrev header */
8370 cur_abbrev->number = abbrev_number;
8371 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8372 abbrev_ptr += bytes_read;
8373 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8374 abbrev_ptr += 1;
8375
72bf9492
DJ
8376 if (cur_abbrev->tag == DW_TAG_namespace)
8377 cu->has_namespace_info = 1;
8378
c906108c
SS
8379 /* now read in declarations */
8380 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8381 abbrev_ptr += bytes_read;
8382 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8383 abbrev_ptr += bytes_read;
8384 while (abbrev_name)
8385 {
f3dd6933 8386 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8387 {
f3dd6933
DJ
8388 allocated_attrs += ATTR_ALLOC_CHUNK;
8389 cur_attrs
8390 = xrealloc (cur_attrs, (allocated_attrs
8391 * sizeof (struct attr_abbrev)));
c906108c 8392 }
ae038cb0
DJ
8393
8394 /* Record whether this compilation unit might have
8395 inter-compilation-unit references. If we don't know what form
8396 this attribute will have, then it might potentially be a
8397 DW_FORM_ref_addr, so we conservatively expect inter-CU
8398 references. */
8399
8400 if (abbrev_form == DW_FORM_ref_addr
8401 || abbrev_form == DW_FORM_indirect)
8402 cu->has_form_ref_addr = 1;
8403
f3dd6933
DJ
8404 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8405 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8406 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8407 abbrev_ptr += bytes_read;
8408 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8409 abbrev_ptr += bytes_read;
8410 }
8411
f3dd6933
DJ
8412 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8413 (cur_abbrev->num_attrs
8414 * sizeof (struct attr_abbrev)));
8415 memcpy (cur_abbrev->attrs, cur_attrs,
8416 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8417
c906108c 8418 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8419 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8420 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8421
8422 /* Get next abbreviation.
8423 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8424 always properly terminated with an abbrev number of 0.
8425 Exit loop if we encounter an abbreviation which we have
8426 already read (which means we are about to read the abbreviations
8427 for the next compile unit) or if the end of the abbreviation
8428 table is reached. */
dce234bc
PP
8429 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8430 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8431 break;
8432 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8433 abbrev_ptr += bytes_read;
e7c27a73 8434 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8435 break;
8436 }
f3dd6933
DJ
8437
8438 xfree (cur_attrs);
c906108c
SS
8439}
8440
f3dd6933 8441/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8442
c906108c 8443static void
f3dd6933 8444dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8445{
f3dd6933 8446 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8447
f3dd6933
DJ
8448 obstack_free (&cu->abbrev_obstack, NULL);
8449 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8450}
8451
8452/* Lookup an abbrev_info structure in the abbrev hash table. */
8453
8454static struct abbrev_info *
e7c27a73 8455dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8456{
8457 unsigned int hash_number;
8458 struct abbrev_info *abbrev;
8459
8460 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8461 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8462
8463 while (abbrev)
8464 {
8465 if (abbrev->number == number)
8466 return abbrev;
8467 else
8468 abbrev = abbrev->next;
8469 }
8470 return NULL;
8471}
8472
72bf9492
DJ
8473/* Returns nonzero if TAG represents a type that we might generate a partial
8474 symbol for. */
8475
8476static int
8477is_type_tag_for_partial (int tag)
8478{
8479 switch (tag)
8480 {
8481#if 0
8482 /* Some types that would be reasonable to generate partial symbols for,
8483 that we don't at present. */
8484 case DW_TAG_array_type:
8485 case DW_TAG_file_type:
8486 case DW_TAG_ptr_to_member_type:
8487 case DW_TAG_set_type:
8488 case DW_TAG_string_type:
8489 case DW_TAG_subroutine_type:
8490#endif
8491 case DW_TAG_base_type:
8492 case DW_TAG_class_type:
680b30c7 8493 case DW_TAG_interface_type:
72bf9492
DJ
8494 case DW_TAG_enumeration_type:
8495 case DW_TAG_structure_type:
8496 case DW_TAG_subrange_type:
8497 case DW_TAG_typedef:
8498 case DW_TAG_union_type:
8499 return 1;
8500 default:
8501 return 0;
8502 }
8503}
8504
8505/* Load all DIEs that are interesting for partial symbols into memory. */
8506
8507static struct partial_die_info *
93311388
DE
8508load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8509 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8510{
8511 struct partial_die_info *part_die;
8512 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8513 struct abbrev_info *abbrev;
8514 unsigned int bytes_read;
5afb4e99 8515 unsigned int load_all = 0;
72bf9492
DJ
8516
8517 int nesting_level = 1;
8518
8519 parent_die = NULL;
8520 last_die = NULL;
8521
5afb4e99
DJ
8522 if (cu->per_cu && cu->per_cu->load_all_dies)
8523 load_all = 1;
8524
72bf9492
DJ
8525 cu->partial_dies
8526 = htab_create_alloc_ex (cu->header.length / 12,
8527 partial_die_hash,
8528 partial_die_eq,
8529 NULL,
8530 &cu->comp_unit_obstack,
8531 hashtab_obstack_allocate,
8532 dummy_obstack_deallocate);
8533
8534 part_die = obstack_alloc (&cu->comp_unit_obstack,
8535 sizeof (struct partial_die_info));
8536
8537 while (1)
8538 {
8539 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8540
8541 /* A NULL abbrev means the end of a series of children. */
8542 if (abbrev == NULL)
8543 {
8544 if (--nesting_level == 0)
8545 {
8546 /* PART_DIE was probably the last thing allocated on the
8547 comp_unit_obstack, so we could call obstack_free
8548 here. We don't do that because the waste is small,
8549 and will be cleaned up when we're done with this
8550 compilation unit. This way, we're also more robust
8551 against other users of the comp_unit_obstack. */
8552 return first_die;
8553 }
8554 info_ptr += bytes_read;
8555 last_die = parent_die;
8556 parent_die = parent_die->die_parent;
8557 continue;
8558 }
8559
98bfdba5
PA
8560 /* Check for template arguments. We never save these; if
8561 they're seen, we just mark the parent, and go on our way. */
8562 if (parent_die != NULL
8563 && cu->language == language_cplus
8564 && (abbrev->tag == DW_TAG_template_type_param
8565 || abbrev->tag == DW_TAG_template_value_param))
8566 {
8567 parent_die->has_template_arguments = 1;
8568
8569 if (!load_all)
8570 {
8571 /* We don't need a partial DIE for the template argument. */
8572 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8573 cu);
8574 continue;
8575 }
8576 }
8577
8578 /* We only recurse into subprograms looking for template arguments.
8579 Skip their other children. */
8580 if (!load_all
8581 && cu->language == language_cplus
8582 && parent_die != NULL
8583 && parent_die->tag == DW_TAG_subprogram)
8584 {
8585 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8586 continue;
8587 }
8588
5afb4e99
DJ
8589 /* Check whether this DIE is interesting enough to save. Normally
8590 we would not be interested in members here, but there may be
8591 later variables referencing them via DW_AT_specification (for
8592 static members). */
8593 if (!load_all
8594 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8595 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8596 && abbrev->tag != DW_TAG_enumerator
8597 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8598 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8599 && abbrev->tag != DW_TAG_variable
5afb4e99 8600 && abbrev->tag != DW_TAG_namespace
f55ee35c 8601 && abbrev->tag != DW_TAG_module
5afb4e99 8602 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8603 {
8604 /* Otherwise we skip to the next sibling, if any. */
93311388 8605 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8606 continue;
8607 }
8608
93311388
DE
8609 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8610 buffer, info_ptr, cu);
72bf9492
DJ
8611
8612 /* This two-pass algorithm for processing partial symbols has a
8613 high cost in cache pressure. Thus, handle some simple cases
8614 here which cover the majority of C partial symbols. DIEs
8615 which neither have specification tags in them, nor could have
8616 specification tags elsewhere pointing at them, can simply be
8617 processed and discarded.
8618
8619 This segment is also optional; scan_partial_symbols and
8620 add_partial_symbol will handle these DIEs if we chain
8621 them in normally. When compilers which do not emit large
8622 quantities of duplicate debug information are more common,
8623 this code can probably be removed. */
8624
8625 /* Any complete simple types at the top level (pretty much all
8626 of them, for a language without namespaces), can be processed
8627 directly. */
8628 if (parent_die == NULL
8629 && part_die->has_specification == 0
8630 && part_die->is_declaration == 0
8631 && (part_die->tag == DW_TAG_typedef
8632 || part_die->tag == DW_TAG_base_type
8633 || part_die->tag == DW_TAG_subrange_type))
8634 {
8635 if (building_psymtab && part_die->name != NULL)
04a679b8 8636 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8637 VAR_DOMAIN, LOC_TYPEDEF,
8638 &cu->objfile->static_psymbols,
8639 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8640 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8641 continue;
8642 }
8643
8644 /* If we're at the second level, and we're an enumerator, and
8645 our parent has no specification (meaning possibly lives in a
8646 namespace elsewhere), then we can add the partial symbol now
8647 instead of queueing it. */
8648 if (part_die->tag == DW_TAG_enumerator
8649 && parent_die != NULL
8650 && parent_die->die_parent == NULL
8651 && parent_die->tag == DW_TAG_enumeration_type
8652 && parent_die->has_specification == 0)
8653 {
8654 if (part_die->name == NULL)
e2e0b3e5 8655 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8656 else if (building_psymtab)
04a679b8 8657 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8658 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8659 (cu->language == language_cplus
8660 || cu->language == language_java)
72bf9492
DJ
8661 ? &cu->objfile->global_psymbols
8662 : &cu->objfile->static_psymbols,
8663 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8664
93311388 8665 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8666 continue;
8667 }
8668
8669 /* We'll save this DIE so link it in. */
8670 part_die->die_parent = parent_die;
8671 part_die->die_sibling = NULL;
8672 part_die->die_child = NULL;
8673
8674 if (last_die && last_die == parent_die)
8675 last_die->die_child = part_die;
8676 else if (last_die)
8677 last_die->die_sibling = part_die;
8678
8679 last_die = part_die;
8680
8681 if (first_die == NULL)
8682 first_die = part_die;
8683
8684 /* Maybe add the DIE to the hash table. Not all DIEs that we
8685 find interesting need to be in the hash table, because we
8686 also have the parent/sibling/child chains; only those that we
8687 might refer to by offset later during partial symbol reading.
8688
8689 For now this means things that might have be the target of a
8690 DW_AT_specification, DW_AT_abstract_origin, or
8691 DW_AT_extension. DW_AT_extension will refer only to
8692 namespaces; DW_AT_abstract_origin refers to functions (and
8693 many things under the function DIE, but we do not recurse
8694 into function DIEs during partial symbol reading) and
8695 possibly variables as well; DW_AT_specification refers to
8696 declarations. Declarations ought to have the DW_AT_declaration
8697 flag. It happens that GCC forgets to put it in sometimes, but
8698 only for functions, not for types.
8699
8700 Adding more things than necessary to the hash table is harmless
8701 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8702 wasted time in find_partial_die, when we reread the compilation
8703 unit with load_all_dies set. */
72bf9492 8704
5afb4e99 8705 if (load_all
72929c62 8706 || abbrev->tag == DW_TAG_constant
5afb4e99 8707 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8708 || abbrev->tag == DW_TAG_variable
8709 || abbrev->tag == DW_TAG_namespace
8710 || part_die->is_declaration)
8711 {
8712 void **slot;
8713
8714 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8715 part_die->offset, INSERT);
8716 *slot = part_die;
8717 }
8718
8719 part_die = obstack_alloc (&cu->comp_unit_obstack,
8720 sizeof (struct partial_die_info));
8721
8722 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8723 we have no reason to follow the children of structures; for other
98bfdba5
PA
8724 languages we have to, so that we can get at method physnames
8725 to infer fully qualified class names, for DW_AT_specification,
8726 and for C++ template arguments. For C++, we also look one level
8727 inside functions to find template arguments (if the name of the
8728 function does not already contain the template arguments).
bc30ff58
JB
8729
8730 For Ada, we need to scan the children of subprograms and lexical
8731 blocks as well because Ada allows the definition of nested
8732 entities that could be interesting for the debugger, such as
8733 nested subprograms for instance. */
72bf9492 8734 if (last_die->has_children
5afb4e99
DJ
8735 && (load_all
8736 || last_die->tag == DW_TAG_namespace
f55ee35c 8737 || last_die->tag == DW_TAG_module
72bf9492 8738 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8739 || (cu->language == language_cplus
8740 && last_die->tag == DW_TAG_subprogram
8741 && (last_die->name == NULL
8742 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8743 || (cu->language != language_c
8744 && (last_die->tag == DW_TAG_class_type
680b30c7 8745 || last_die->tag == DW_TAG_interface_type
72bf9492 8746 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8747 || last_die->tag == DW_TAG_union_type))
8748 || (cu->language == language_ada
8749 && (last_die->tag == DW_TAG_subprogram
8750 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8751 {
8752 nesting_level++;
8753 parent_die = last_die;
8754 continue;
8755 }
8756
8757 /* Otherwise we skip to the next sibling, if any. */
93311388 8758 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8759
8760 /* Back to the top, do it again. */
8761 }
8762}
8763
c906108c
SS
8764/* Read a minimal amount of information into the minimal die structure. */
8765
fe1b8b76 8766static gdb_byte *
72bf9492
DJ
8767read_partial_die (struct partial_die_info *part_die,
8768 struct abbrev_info *abbrev,
8769 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8770 gdb_byte *buffer, gdb_byte *info_ptr,
8771 struct dwarf2_cu *cu)
c906108c 8772{
fa238c03 8773 unsigned int i;
c906108c 8774 struct attribute attr;
c5aa993b 8775 int has_low_pc_attr = 0;
c906108c
SS
8776 int has_high_pc_attr = 0;
8777
72bf9492 8778 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8779
93311388 8780 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8781
8782 info_ptr += abbrev_len;
8783
8784 if (abbrev == NULL)
8785 return info_ptr;
8786
c906108c
SS
8787 part_die->tag = abbrev->tag;
8788 part_die->has_children = abbrev->has_children;
c906108c
SS
8789
8790 for (i = 0; i < abbrev->num_attrs; ++i)
8791 {
e7c27a73 8792 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8793
8794 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8795 partial symbol table. */
c906108c
SS
8796 switch (attr.name)
8797 {
8798 case DW_AT_name:
71c25dea
TT
8799 switch (part_die->tag)
8800 {
8801 case DW_TAG_compile_unit:
348e048f 8802 case DW_TAG_type_unit:
71c25dea
TT
8803 /* Compilation units have a DW_AT_name that is a filename, not
8804 a source language identifier. */
8805 case DW_TAG_enumeration_type:
8806 case DW_TAG_enumerator:
8807 /* These tags always have simple identifiers already; no need
8808 to canonicalize them. */
8809 part_die->name = DW_STRING (&attr);
8810 break;
8811 default:
8812 part_die->name
8813 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8814 &cu->objfile->objfile_obstack);
71c25dea
TT
8815 break;
8816 }
c906108c 8817 break;
31ef98ae 8818 case DW_AT_linkage_name:
c906108c 8819 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8820 /* Note that both forms of linkage name might appear. We
8821 assume they will be the same, and we only store the last
8822 one we see. */
94af9270
KS
8823 if (cu->language == language_ada)
8824 part_die->name = DW_STRING (&attr);
abc72ce4 8825 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8826 break;
8827 case DW_AT_low_pc:
8828 has_low_pc_attr = 1;
8829 part_die->lowpc = DW_ADDR (&attr);
8830 break;
8831 case DW_AT_high_pc:
8832 has_high_pc_attr = 1;
8833 part_die->highpc = DW_ADDR (&attr);
8834 break;
8835 case DW_AT_location:
8e19ed76
PS
8836 /* Support the .debug_loc offsets */
8837 if (attr_form_is_block (&attr))
8838 {
8839 part_die->locdesc = DW_BLOCK (&attr);
8840 }
3690dd37 8841 else if (attr_form_is_section_offset (&attr))
8e19ed76 8842 {
4d3c2250 8843 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8844 }
8845 else
8846 {
4d3c2250
KB
8847 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8848 "partial symbol information");
8e19ed76 8849 }
c906108c 8850 break;
c906108c
SS
8851 case DW_AT_external:
8852 part_die->is_external = DW_UNSND (&attr);
8853 break;
8854 case DW_AT_declaration:
8855 part_die->is_declaration = DW_UNSND (&attr);
8856 break;
8857 case DW_AT_type:
8858 part_die->has_type = 1;
8859 break;
8860 case DW_AT_abstract_origin:
8861 case DW_AT_specification:
72bf9492
DJ
8862 case DW_AT_extension:
8863 part_die->has_specification = 1;
c764a876 8864 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8865 break;
8866 case DW_AT_sibling:
8867 /* Ignore absolute siblings, they might point outside of
8868 the current compile unit. */
8869 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8870 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8871 else
93311388 8872 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8873 break;
fa4028e9
JB
8874 case DW_AT_byte_size:
8875 part_die->has_byte_size = 1;
8876 break;
68511cec
CES
8877 case DW_AT_calling_convention:
8878 /* DWARF doesn't provide a way to identify a program's source-level
8879 entry point. DW_AT_calling_convention attributes are only meant
8880 to describe functions' calling conventions.
8881
8882 However, because it's a necessary piece of information in
8883 Fortran, and because DW_CC_program is the only piece of debugging
8884 information whose definition refers to a 'main program' at all,
8885 several compilers have begun marking Fortran main programs with
8886 DW_CC_program --- even when those functions use the standard
8887 calling conventions.
8888
8889 So until DWARF specifies a way to provide this information and
8890 compilers pick up the new representation, we'll support this
8891 practice. */
8892 if (DW_UNSND (&attr) == DW_CC_program
8893 && cu->language == language_fortran)
01f8c46d
JK
8894 {
8895 set_main_name (part_die->name);
8896
8897 /* As this DIE has a static linkage the name would be difficult
8898 to look up later. */
8899 language_of_main = language_fortran;
8900 }
68511cec 8901 break;
c906108c
SS
8902 default:
8903 break;
8904 }
8905 }
8906
c906108c
SS
8907 /* When using the GNU linker, .gnu.linkonce. sections are used to
8908 eliminate duplicate copies of functions and vtables and such.
8909 The linker will arbitrarily choose one and discard the others.
8910 The AT_*_pc values for such functions refer to local labels in
8911 these sections. If the section from that file was discarded, the
8912 labels are not in the output, so the relocs get a value of 0.
8913 If this is a discarded function, mark the pc bounds as invalid,
8914 so that GDB will ignore it. */
8915 if (has_low_pc_attr && has_high_pc_attr
8916 && part_die->lowpc < part_die->highpc
8917 && (part_die->lowpc != 0
72dca2f5 8918 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8919 part_die->has_pc_info = 1;
85cbf3d3 8920
c906108c
SS
8921 return info_ptr;
8922}
8923
72bf9492
DJ
8924/* Find a cached partial DIE at OFFSET in CU. */
8925
8926static struct partial_die_info *
c764a876 8927find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8928{
8929 struct partial_die_info *lookup_die = NULL;
8930 struct partial_die_info part_die;
8931
8932 part_die.offset = offset;
8933 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8934
72bf9492
DJ
8935 return lookup_die;
8936}
8937
348e048f
DE
8938/* Find a partial DIE at OFFSET, which may or may not be in CU,
8939 except in the case of .debug_types DIEs which do not reference
8940 outside their CU (they do however referencing other types via
8941 DW_FORM_sig8). */
72bf9492
DJ
8942
8943static struct partial_die_info *
c764a876 8944find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8945{
5afb4e99
DJ
8946 struct dwarf2_per_cu_data *per_cu = NULL;
8947 struct partial_die_info *pd = NULL;
72bf9492 8948
348e048f
DE
8949 if (cu->per_cu->from_debug_types)
8950 {
8951 pd = find_partial_die_in_comp_unit (offset, cu);
8952 if (pd != NULL)
8953 return pd;
8954 goto not_found;
8955 }
8956
45452591 8957 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8958 {
8959 pd = find_partial_die_in_comp_unit (offset, cu);
8960 if (pd != NULL)
8961 return pd;
8962 }
72bf9492 8963
ae038cb0
DJ
8964 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8965
98bfdba5
PA
8966 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8967 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8968
8969 per_cu->cu->last_used = 0;
5afb4e99
DJ
8970 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8971
8972 if (pd == NULL && per_cu->load_all_dies == 0)
8973 {
8974 struct cleanup *back_to;
8975 struct partial_die_info comp_unit_die;
8976 struct abbrev_info *abbrev;
8977 unsigned int bytes_read;
8978 char *info_ptr;
8979
8980 per_cu->load_all_dies = 1;
8981
8982 /* Re-read the DIEs. */
8983 back_to = make_cleanup (null_cleanup, 0);
8984 if (per_cu->cu->dwarf2_abbrevs == NULL)
8985 {
8986 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8987 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8988 }
dce234bc 8989 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8990 + per_cu->cu->header.offset
8991 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8992 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8993 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8994 per_cu->cu->objfile->obfd,
8995 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8996 per_cu->cu);
8997 if (comp_unit_die.has_children)
93311388
DE
8998 load_partial_dies (per_cu->cu->objfile->obfd,
8999 dwarf2_per_objfile->info.buffer, info_ptr,
9000 0, per_cu->cu);
5afb4e99
DJ
9001 do_cleanups (back_to);
9002
9003 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9004 }
9005
348e048f
DE
9006 not_found:
9007
5afb4e99
DJ
9008 if (pd == NULL)
9009 internal_error (__FILE__, __LINE__,
c764a876 9010 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9011 offset, bfd_get_filename (cu->objfile->obfd));
9012 return pd;
72bf9492
DJ
9013}
9014
abc72ce4
DE
9015/* See if we can figure out if the class lives in a namespace. We do
9016 this by looking for a member function; its demangled name will
9017 contain namespace info, if there is any. */
9018
9019static void
9020guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9021 struct dwarf2_cu *cu)
9022{
9023 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9024 what template types look like, because the demangler
9025 frequently doesn't give the same name as the debug info. We
9026 could fix this by only using the demangled name to get the
9027 prefix (but see comment in read_structure_type). */
9028
9029 struct partial_die_info *real_pdi;
9030 struct partial_die_info *child_pdi;
9031
9032 /* If this DIE (this DIE's specification, if any) has a parent, then
9033 we should not do this. We'll prepend the parent's fully qualified
9034 name when we create the partial symbol. */
9035
9036 real_pdi = struct_pdi;
9037 while (real_pdi->has_specification)
9038 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9039
9040 if (real_pdi->die_parent != NULL)
9041 return;
9042
9043 for (child_pdi = struct_pdi->die_child;
9044 child_pdi != NULL;
9045 child_pdi = child_pdi->die_sibling)
9046 {
9047 if (child_pdi->tag == DW_TAG_subprogram
9048 && child_pdi->linkage_name != NULL)
9049 {
9050 char *actual_class_name
9051 = language_class_name_from_physname (cu->language_defn,
9052 child_pdi->linkage_name);
9053 if (actual_class_name != NULL)
9054 {
9055 struct_pdi->name
9056 = obsavestring (actual_class_name,
9057 strlen (actual_class_name),
9058 &cu->objfile->objfile_obstack);
9059 xfree (actual_class_name);
9060 }
9061 break;
9062 }
9063 }
9064}
9065
72bf9492
DJ
9066/* Adjust PART_DIE before generating a symbol for it. This function
9067 may set the is_external flag or change the DIE's name. */
9068
9069static void
9070fixup_partial_die (struct partial_die_info *part_die,
9071 struct dwarf2_cu *cu)
9072{
abc72ce4
DE
9073 /* Once we've fixed up a die, there's no point in doing so again.
9074 This also avoids a memory leak if we were to call
9075 guess_partial_die_structure_name multiple times. */
9076 if (part_die->fixup_called)
9077 return;
9078
72bf9492
DJ
9079 /* If we found a reference attribute and the DIE has no name, try
9080 to find a name in the referred to DIE. */
9081
9082 if (part_die->name == NULL && part_die->has_specification)
9083 {
9084 struct partial_die_info *spec_die;
72bf9492 9085
10b3939b 9086 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9087
10b3939b 9088 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9089
9090 if (spec_die->name)
9091 {
9092 part_die->name = spec_die->name;
9093
9094 /* Copy DW_AT_external attribute if it is set. */
9095 if (spec_die->is_external)
9096 part_die->is_external = spec_die->is_external;
9097 }
9098 }
9099
9100 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9101
9102 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9103 part_die->name = "(anonymous namespace)";
9104
abc72ce4
DE
9105 /* If there is no parent die to provide a namespace, and there are
9106 children, see if we can determine the namespace from their linkage
9107 name.
9108 NOTE: We need to do this even if cu->has_namespace_info != 0.
9109 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9110 if (cu->language == language_cplus
9111 && dwarf2_per_objfile->types.asection != NULL
9112 && part_die->die_parent == NULL
9113 && part_die->has_children
9114 && (part_die->tag == DW_TAG_class_type
9115 || part_die->tag == DW_TAG_structure_type
9116 || part_die->tag == DW_TAG_union_type))
9117 guess_partial_die_structure_name (part_die, cu);
9118
9119 part_die->fixup_called = 1;
72bf9492
DJ
9120}
9121
a8329558 9122/* Read an attribute value described by an attribute form. */
c906108c 9123
fe1b8b76 9124static gdb_byte *
a8329558 9125read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9126 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9127 struct dwarf2_cu *cu)
c906108c 9128{
e7c27a73 9129 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9130 unsigned int bytes_read;
9131 struct dwarf_block *blk;
9132
a8329558
KW
9133 attr->form = form;
9134 switch (form)
c906108c 9135 {
c906108c 9136 case DW_FORM_ref_addr:
ae411497
TT
9137 if (cu->header.version == 2)
9138 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9139 else
9140 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9141 info_ptr += bytes_read;
9142 break;
9143 case DW_FORM_addr:
e7c27a73 9144 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9145 info_ptr += bytes_read;
c906108c
SS
9146 break;
9147 case DW_FORM_block2:
7b5a2f43 9148 blk = dwarf_alloc_block (cu);
c906108c
SS
9149 blk->size = read_2_bytes (abfd, info_ptr);
9150 info_ptr += 2;
9151 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9152 info_ptr += blk->size;
9153 DW_BLOCK (attr) = blk;
9154 break;
9155 case DW_FORM_block4:
7b5a2f43 9156 blk = dwarf_alloc_block (cu);
c906108c
SS
9157 blk->size = read_4_bytes (abfd, info_ptr);
9158 info_ptr += 4;
9159 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9160 info_ptr += blk->size;
9161 DW_BLOCK (attr) = blk;
9162 break;
9163 case DW_FORM_data2:
9164 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9165 info_ptr += 2;
9166 break;
9167 case DW_FORM_data4:
9168 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9169 info_ptr += 4;
9170 break;
9171 case DW_FORM_data8:
9172 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9173 info_ptr += 8;
9174 break;
2dc7f7b3
TT
9175 case DW_FORM_sec_offset:
9176 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9177 info_ptr += bytes_read;
9178 break;
c906108c 9179 case DW_FORM_string:
9b1c24c8 9180 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9181 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9182 info_ptr += bytes_read;
9183 break;
4bdf3d34
JJ
9184 case DW_FORM_strp:
9185 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9186 &bytes_read);
8285870a 9187 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9188 info_ptr += bytes_read;
9189 break;
2dc7f7b3 9190 case DW_FORM_exprloc:
c906108c 9191 case DW_FORM_block:
7b5a2f43 9192 blk = dwarf_alloc_block (cu);
c906108c
SS
9193 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9194 info_ptr += bytes_read;
9195 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9196 info_ptr += blk->size;
9197 DW_BLOCK (attr) = blk;
9198 break;
9199 case DW_FORM_block1:
7b5a2f43 9200 blk = dwarf_alloc_block (cu);
c906108c
SS
9201 blk->size = read_1_byte (abfd, info_ptr);
9202 info_ptr += 1;
9203 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9204 info_ptr += blk->size;
9205 DW_BLOCK (attr) = blk;
9206 break;
9207 case DW_FORM_data1:
9208 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9209 info_ptr += 1;
9210 break;
9211 case DW_FORM_flag:
9212 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9213 info_ptr += 1;
9214 break;
2dc7f7b3
TT
9215 case DW_FORM_flag_present:
9216 DW_UNSND (attr) = 1;
9217 break;
c906108c
SS
9218 case DW_FORM_sdata:
9219 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9220 info_ptr += bytes_read;
9221 break;
9222 case DW_FORM_udata:
9223 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9224 info_ptr += bytes_read;
9225 break;
9226 case DW_FORM_ref1:
10b3939b 9227 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9228 info_ptr += 1;
9229 break;
9230 case DW_FORM_ref2:
10b3939b 9231 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9232 info_ptr += 2;
9233 break;
9234 case DW_FORM_ref4:
10b3939b 9235 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9236 info_ptr += 4;
9237 break;
613e1657 9238 case DW_FORM_ref8:
10b3939b 9239 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9240 info_ptr += 8;
9241 break;
348e048f
DE
9242 case DW_FORM_sig8:
9243 /* Convert the signature to something we can record in DW_UNSND
9244 for later lookup.
9245 NOTE: This is NULL if the type wasn't found. */
9246 DW_SIGNATURED_TYPE (attr) =
9247 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9248 info_ptr += 8;
9249 break;
c906108c 9250 case DW_FORM_ref_udata:
10b3939b
DJ
9251 DW_ADDR (attr) = (cu->header.offset
9252 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9253 info_ptr += bytes_read;
9254 break;
c906108c 9255 case DW_FORM_indirect:
a8329558
KW
9256 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9257 info_ptr += bytes_read;
e7c27a73 9258 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9259 break;
c906108c 9260 default:
8a3fe4f8 9261 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9262 dwarf_form_name (form),
9263 bfd_get_filename (abfd));
c906108c 9264 }
28e94949
JB
9265
9266 /* We have seen instances where the compiler tried to emit a byte
9267 size attribute of -1 which ended up being encoded as an unsigned
9268 0xffffffff. Although 0xffffffff is technically a valid size value,
9269 an object of this size seems pretty unlikely so we can relatively
9270 safely treat these cases as if the size attribute was invalid and
9271 treat them as zero by default. */
9272 if (attr->name == DW_AT_byte_size
9273 && form == DW_FORM_data4
9274 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9275 {
9276 complaint
9277 (&symfile_complaints,
43bbcdc2
PH
9278 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9279 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9280 DW_UNSND (attr) = 0;
9281 }
28e94949 9282
c906108c
SS
9283 return info_ptr;
9284}
9285
a8329558
KW
9286/* Read an attribute described by an abbreviated attribute. */
9287
fe1b8b76 9288static gdb_byte *
a8329558 9289read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9290 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9291{
9292 attr->name = abbrev->name;
e7c27a73 9293 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9294}
9295
c906108c
SS
9296/* read dwarf information from a buffer */
9297
9298static unsigned int
fe1b8b76 9299read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9300{
fe1b8b76 9301 return bfd_get_8 (abfd, buf);
c906108c
SS
9302}
9303
9304static int
fe1b8b76 9305read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9306{
fe1b8b76 9307 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9308}
9309
9310static unsigned int
fe1b8b76 9311read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9312{
fe1b8b76 9313 return bfd_get_16 (abfd, buf);
c906108c
SS
9314}
9315
9316static int
fe1b8b76 9317read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9318{
fe1b8b76 9319 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9320}
9321
9322static unsigned int
fe1b8b76 9323read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9324{
fe1b8b76 9325 return bfd_get_32 (abfd, buf);
c906108c
SS
9326}
9327
9328static int
fe1b8b76 9329read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9330{
fe1b8b76 9331 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9332}
9333
93311388 9334static ULONGEST
fe1b8b76 9335read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9336{
fe1b8b76 9337 return bfd_get_64 (abfd, buf);
c906108c
SS
9338}
9339
9340static CORE_ADDR
fe1b8b76 9341read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9342 unsigned int *bytes_read)
c906108c 9343{
e7c27a73 9344 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9345 CORE_ADDR retval = 0;
9346
107d2387 9347 if (cu_header->signed_addr_p)
c906108c 9348 {
107d2387
AC
9349 switch (cu_header->addr_size)
9350 {
9351 case 2:
fe1b8b76 9352 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9353 break;
9354 case 4:
fe1b8b76 9355 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9356 break;
9357 case 8:
fe1b8b76 9358 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9359 break;
9360 default:
8e65ff28 9361 internal_error (__FILE__, __LINE__,
e2e0b3e5 9362 _("read_address: bad switch, signed [in module %s]"),
659b0389 9363 bfd_get_filename (abfd));
107d2387
AC
9364 }
9365 }
9366 else
9367 {
9368 switch (cu_header->addr_size)
9369 {
9370 case 2:
fe1b8b76 9371 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9372 break;
9373 case 4:
fe1b8b76 9374 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9375 break;
9376 case 8:
fe1b8b76 9377 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9378 break;
9379 default:
8e65ff28 9380 internal_error (__FILE__, __LINE__,
e2e0b3e5 9381 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9382 bfd_get_filename (abfd));
107d2387 9383 }
c906108c 9384 }
64367e0a 9385
107d2387
AC
9386 *bytes_read = cu_header->addr_size;
9387 return retval;
c906108c
SS
9388}
9389
f7ef9339 9390/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9391 specification allows the initial length to take up either 4 bytes
9392 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9393 bytes describe the length and all offsets will be 8 bytes in length
9394 instead of 4.
9395
f7ef9339
KB
9396 An older, non-standard 64-bit format is also handled by this
9397 function. The older format in question stores the initial length
9398 as an 8-byte quantity without an escape value. Lengths greater
9399 than 2^32 aren't very common which means that the initial 4 bytes
9400 is almost always zero. Since a length value of zero doesn't make
9401 sense for the 32-bit format, this initial zero can be considered to
9402 be an escape value which indicates the presence of the older 64-bit
9403 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9404 greater than 4GB. If it becomes necessary to handle lengths
9405 somewhat larger than 4GB, we could allow other small values (such
9406 as the non-sensical values of 1, 2, and 3) to also be used as
9407 escape values indicating the presence of the old format.
f7ef9339 9408
917c78fc
MK
9409 The value returned via bytes_read should be used to increment the
9410 relevant pointer after calling read_initial_length().
c764a876 9411
613e1657
KB
9412 [ Note: read_initial_length() and read_offset() are based on the
9413 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9414 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9415 from:
9416
f7ef9339 9417 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9418
613e1657
KB
9419 This document is only a draft and is subject to change. (So beware.)
9420
f7ef9339 9421 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9422 determined empirically by examining 64-bit ELF files produced by
9423 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9424
9425 - Kevin, July 16, 2002
613e1657
KB
9426 ] */
9427
9428static LONGEST
c764a876 9429read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9430{
fe1b8b76 9431 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9432
dd373385 9433 if (length == 0xffffffff)
613e1657 9434 {
fe1b8b76 9435 length = bfd_get_64 (abfd, buf + 4);
613e1657 9436 *bytes_read = 12;
613e1657 9437 }
dd373385 9438 else if (length == 0)
f7ef9339 9439 {
dd373385 9440 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9441 length = bfd_get_64 (abfd, buf);
f7ef9339 9442 *bytes_read = 8;
f7ef9339 9443 }
613e1657
KB
9444 else
9445 {
9446 *bytes_read = 4;
613e1657
KB
9447 }
9448
c764a876
DE
9449 return length;
9450}
dd373385 9451
c764a876
DE
9452/* Cover function for read_initial_length.
9453 Returns the length of the object at BUF, and stores the size of the
9454 initial length in *BYTES_READ and stores the size that offsets will be in
9455 *OFFSET_SIZE.
9456 If the initial length size is not equivalent to that specified in
9457 CU_HEADER then issue a complaint.
9458 This is useful when reading non-comp-unit headers. */
dd373385 9459
c764a876
DE
9460static LONGEST
9461read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9462 const struct comp_unit_head *cu_header,
9463 unsigned int *bytes_read,
9464 unsigned int *offset_size)
9465{
9466 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9467
9468 gdb_assert (cu_header->initial_length_size == 4
9469 || cu_header->initial_length_size == 8
9470 || cu_header->initial_length_size == 12);
9471
9472 if (cu_header->initial_length_size != *bytes_read)
9473 complaint (&symfile_complaints,
9474 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9475
c764a876 9476 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9477 return length;
613e1657
KB
9478}
9479
9480/* Read an offset from the data stream. The size of the offset is
917c78fc 9481 given by cu_header->offset_size. */
613e1657
KB
9482
9483static LONGEST
fe1b8b76 9484read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9485 unsigned int *bytes_read)
c764a876
DE
9486{
9487 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9488
c764a876
DE
9489 *bytes_read = cu_header->offset_size;
9490 return offset;
9491}
9492
9493/* Read an offset from the data stream. */
9494
9495static LONGEST
9496read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9497{
9498 LONGEST retval = 0;
9499
c764a876 9500 switch (offset_size)
613e1657
KB
9501 {
9502 case 4:
fe1b8b76 9503 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9504 break;
9505 case 8:
fe1b8b76 9506 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9507 break;
9508 default:
8e65ff28 9509 internal_error (__FILE__, __LINE__,
c764a876 9510 _("read_offset_1: bad switch [in module %s]"),
659b0389 9511 bfd_get_filename (abfd));
613e1657
KB
9512 }
9513
917c78fc 9514 return retval;
613e1657
KB
9515}
9516
fe1b8b76
JB
9517static gdb_byte *
9518read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9519{
9520 /* If the size of a host char is 8 bits, we can return a pointer
9521 to the buffer, otherwise we have to copy the data to a buffer
9522 allocated on the temporary obstack. */
4bdf3d34 9523 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9524 return buf;
c906108c
SS
9525}
9526
9527static char *
9b1c24c8 9528read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9529{
9530 /* If the size of a host char is 8 bits, we can return a pointer
9531 to the string, otherwise we have to copy the string to a buffer
9532 allocated on the temporary obstack. */
4bdf3d34 9533 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9534 if (*buf == '\0')
9535 {
9536 *bytes_read_ptr = 1;
9537 return NULL;
9538 }
fe1b8b76
JB
9539 *bytes_read_ptr = strlen ((char *) buf) + 1;
9540 return (char *) buf;
4bdf3d34
JJ
9541}
9542
9543static char *
fe1b8b76 9544read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9545 const struct comp_unit_head *cu_header,
9546 unsigned int *bytes_read_ptr)
9547{
c764a876 9548 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9549
be391dca 9550 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9551 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9552 {
8a3fe4f8 9553 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9554 bfd_get_filename (abfd));
4bdf3d34 9555 return NULL;
c906108c 9556 }
dce234bc 9557 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9558 {
8a3fe4f8 9559 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9560 bfd_get_filename (abfd));
c906108c
SS
9561 return NULL;
9562 }
4bdf3d34 9563 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9564 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9565 return NULL;
dce234bc 9566 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9567}
9568
ce5d95e1 9569static unsigned long
fe1b8b76 9570read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9571{
ce5d95e1
JB
9572 unsigned long result;
9573 unsigned int num_read;
c906108c
SS
9574 int i, shift;
9575 unsigned char byte;
9576
9577 result = 0;
9578 shift = 0;
9579 num_read = 0;
9580 i = 0;
9581 while (1)
9582 {
fe1b8b76 9583 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9584 buf++;
9585 num_read++;
ce5d95e1 9586 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9587 if ((byte & 128) == 0)
9588 {
9589 break;
9590 }
9591 shift += 7;
9592 }
9593 *bytes_read_ptr = num_read;
9594 return result;
9595}
9596
ce5d95e1 9597static long
fe1b8b76 9598read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9599{
ce5d95e1 9600 long result;
77e0b926 9601 int i, shift, num_read;
c906108c
SS
9602 unsigned char byte;
9603
9604 result = 0;
9605 shift = 0;
c906108c
SS
9606 num_read = 0;
9607 i = 0;
9608 while (1)
9609 {
fe1b8b76 9610 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9611 buf++;
9612 num_read++;
ce5d95e1 9613 result |= ((long)(byte & 127) << shift);
c906108c
SS
9614 shift += 7;
9615 if ((byte & 128) == 0)
9616 {
9617 break;
9618 }
9619 }
77e0b926
DJ
9620 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9621 result |= -(((long)1) << shift);
c906108c
SS
9622 *bytes_read_ptr = num_read;
9623 return result;
9624}
9625
4bb7a0a7
DJ
9626/* Return a pointer to just past the end of an LEB128 number in BUF. */
9627
fe1b8b76
JB
9628static gdb_byte *
9629skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9630{
9631 int byte;
9632
9633 while (1)
9634 {
fe1b8b76 9635 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9636 buf++;
9637 if ((byte & 128) == 0)
9638 return buf;
9639 }
9640}
9641
c906108c 9642static void
e142c38c 9643set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9644{
9645 switch (lang)
9646 {
9647 case DW_LANG_C89:
76bee0cc 9648 case DW_LANG_C99:
c906108c 9649 case DW_LANG_C:
e142c38c 9650 cu->language = language_c;
c906108c
SS
9651 break;
9652 case DW_LANG_C_plus_plus:
e142c38c 9653 cu->language = language_cplus;
c906108c 9654 break;
6aecb9c2
JB
9655 case DW_LANG_D:
9656 cu->language = language_d;
9657 break;
c906108c
SS
9658 case DW_LANG_Fortran77:
9659 case DW_LANG_Fortran90:
b21b22e0 9660 case DW_LANG_Fortran95:
e142c38c 9661 cu->language = language_fortran;
c906108c
SS
9662 break;
9663 case DW_LANG_Mips_Assembler:
e142c38c 9664 cu->language = language_asm;
c906108c 9665 break;
bebd888e 9666 case DW_LANG_Java:
e142c38c 9667 cu->language = language_java;
bebd888e 9668 break;
c906108c 9669 case DW_LANG_Ada83:
8aaf0b47 9670 case DW_LANG_Ada95:
bc5f45f8
JB
9671 cu->language = language_ada;
9672 break;
72019c9c
GM
9673 case DW_LANG_Modula2:
9674 cu->language = language_m2;
9675 break;
fe8e67fd
PM
9676 case DW_LANG_Pascal83:
9677 cu->language = language_pascal;
9678 break;
22566fbd
DJ
9679 case DW_LANG_ObjC:
9680 cu->language = language_objc;
9681 break;
c906108c
SS
9682 case DW_LANG_Cobol74:
9683 case DW_LANG_Cobol85:
c906108c 9684 default:
e142c38c 9685 cu->language = language_minimal;
c906108c
SS
9686 break;
9687 }
e142c38c 9688 cu->language_defn = language_def (cu->language);
c906108c
SS
9689}
9690
9691/* Return the named attribute or NULL if not there. */
9692
9693static struct attribute *
e142c38c 9694dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9695{
9696 unsigned int i;
9697 struct attribute *spec = NULL;
9698
9699 for (i = 0; i < die->num_attrs; ++i)
9700 {
9701 if (die->attrs[i].name == name)
10b3939b 9702 return &die->attrs[i];
c906108c
SS
9703 if (die->attrs[i].name == DW_AT_specification
9704 || die->attrs[i].name == DW_AT_abstract_origin)
9705 spec = &die->attrs[i];
9706 }
c906108c 9707
10b3939b 9708 if (spec)
f2f0e013
DJ
9709 {
9710 die = follow_die_ref (die, spec, &cu);
9711 return dwarf2_attr (die, name, cu);
9712 }
c5aa993b 9713
c906108c
SS
9714 return NULL;
9715}
9716
348e048f
DE
9717/* Return the named attribute or NULL if not there,
9718 but do not follow DW_AT_specification, etc.
9719 This is for use in contexts where we're reading .debug_types dies.
9720 Following DW_AT_specification, DW_AT_abstract_origin will take us
9721 back up the chain, and we want to go down. */
9722
9723static struct attribute *
9724dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9725 struct dwarf2_cu *cu)
9726{
9727 unsigned int i;
9728
9729 for (i = 0; i < die->num_attrs; ++i)
9730 if (die->attrs[i].name == name)
9731 return &die->attrs[i];
9732
9733 return NULL;
9734}
9735
05cf31d1
JB
9736/* Return non-zero iff the attribute NAME is defined for the given DIE,
9737 and holds a non-zero value. This function should only be used for
2dc7f7b3 9738 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9739
9740static int
9741dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9742{
9743 struct attribute *attr = dwarf2_attr (die, name, cu);
9744
9745 return (attr && DW_UNSND (attr));
9746}
9747
3ca72b44 9748static int
e142c38c 9749die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9750{
05cf31d1
JB
9751 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9752 which value is non-zero. However, we have to be careful with
9753 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9754 (via dwarf2_flag_true_p) follows this attribute. So we may
9755 end up accidently finding a declaration attribute that belongs
9756 to a different DIE referenced by the specification attribute,
9757 even though the given DIE does not have a declaration attribute. */
9758 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9759 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9760}
9761
63d06c5c 9762/* Return the die giving the specification for DIE, if there is
f2f0e013 9763 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9764 containing the return value on output. If there is no
9765 specification, but there is an abstract origin, that is
9766 returned. */
63d06c5c
DC
9767
9768static struct die_info *
f2f0e013 9769die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9770{
f2f0e013
DJ
9771 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9772 *spec_cu);
63d06c5c 9773
edb3359d
DJ
9774 if (spec_attr == NULL)
9775 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9776
63d06c5c
DC
9777 if (spec_attr == NULL)
9778 return NULL;
9779 else
f2f0e013 9780 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9781}
c906108c 9782
debd256d
JB
9783/* Free the line_header structure *LH, and any arrays and strings it
9784 refers to. */
9785static void
9786free_line_header (struct line_header *lh)
9787{
9788 if (lh->standard_opcode_lengths)
a8bc7b56 9789 xfree (lh->standard_opcode_lengths);
debd256d
JB
9790
9791 /* Remember that all the lh->file_names[i].name pointers are
9792 pointers into debug_line_buffer, and don't need to be freed. */
9793 if (lh->file_names)
a8bc7b56 9794 xfree (lh->file_names);
debd256d
JB
9795
9796 /* Similarly for the include directory names. */
9797 if (lh->include_dirs)
a8bc7b56 9798 xfree (lh->include_dirs);
debd256d 9799
a8bc7b56 9800 xfree (lh);
debd256d
JB
9801}
9802
9803
9804/* Add an entry to LH's include directory table. */
9805static void
9806add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9807{
debd256d
JB
9808 /* Grow the array if necessary. */
9809 if (lh->include_dirs_size == 0)
c5aa993b 9810 {
debd256d
JB
9811 lh->include_dirs_size = 1; /* for testing */
9812 lh->include_dirs = xmalloc (lh->include_dirs_size
9813 * sizeof (*lh->include_dirs));
9814 }
9815 else if (lh->num_include_dirs >= lh->include_dirs_size)
9816 {
9817 lh->include_dirs_size *= 2;
9818 lh->include_dirs = xrealloc (lh->include_dirs,
9819 (lh->include_dirs_size
9820 * sizeof (*lh->include_dirs)));
c5aa993b 9821 }
c906108c 9822
debd256d
JB
9823 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9824}
6e70227d 9825
debd256d
JB
9826
9827/* Add an entry to LH's file name table. */
9828static void
9829add_file_name (struct line_header *lh,
9830 char *name,
9831 unsigned int dir_index,
9832 unsigned int mod_time,
9833 unsigned int length)
9834{
9835 struct file_entry *fe;
9836
9837 /* Grow the array if necessary. */
9838 if (lh->file_names_size == 0)
9839 {
9840 lh->file_names_size = 1; /* for testing */
9841 lh->file_names = xmalloc (lh->file_names_size
9842 * sizeof (*lh->file_names));
9843 }
9844 else if (lh->num_file_names >= lh->file_names_size)
9845 {
9846 lh->file_names_size *= 2;
9847 lh->file_names = xrealloc (lh->file_names,
9848 (lh->file_names_size
9849 * sizeof (*lh->file_names)));
9850 }
9851
9852 fe = &lh->file_names[lh->num_file_names++];
9853 fe->name = name;
9854 fe->dir_index = dir_index;
9855 fe->mod_time = mod_time;
9856 fe->length = length;
aaa75496 9857 fe->included_p = 0;
cb1df416 9858 fe->symtab = NULL;
debd256d 9859}
6e70227d 9860
debd256d
JB
9861
9862/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9863 .debug_line, according to the endianness of ABFD. Return a pointer
9864 to a struct line_header, allocated using xmalloc.
debd256d
JB
9865
9866 NOTE: the strings in the include directory and file name tables of
9867 the returned object point into debug_line_buffer, and must not be
9868 freed. */
9869static struct line_header *
9870dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9871 struct dwarf2_cu *cu)
debd256d
JB
9872{
9873 struct cleanup *back_to;
9874 struct line_header *lh;
fe1b8b76 9875 gdb_byte *line_ptr;
c764a876 9876 unsigned int bytes_read, offset_size;
debd256d
JB
9877 int i;
9878 char *cur_dir, *cur_file;
9879
be391dca 9880 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9881 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9882 {
e2e0b3e5 9883 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9884 return 0;
9885 }
9886
a738430d
MK
9887 /* Make sure that at least there's room for the total_length field.
9888 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9889 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9890 {
4d3c2250 9891 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9892 return 0;
9893 }
9894
9895 lh = xmalloc (sizeof (*lh));
9896 memset (lh, 0, sizeof (*lh));
9897 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9898 (void *) lh);
9899
dce234bc 9900 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9901
a738430d 9902 /* Read in the header. */
6e70227d 9903 lh->total_length =
c764a876
DE
9904 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9905 &bytes_read, &offset_size);
debd256d 9906 line_ptr += bytes_read;
dce234bc
PP
9907 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9908 + dwarf2_per_objfile->line.size))
debd256d 9909 {
4d3c2250 9910 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9911 return 0;
9912 }
9913 lh->statement_program_end = line_ptr + lh->total_length;
9914 lh->version = read_2_bytes (abfd, line_ptr);
9915 line_ptr += 2;
c764a876
DE
9916 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9917 line_ptr += offset_size;
debd256d
JB
9918 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9919 line_ptr += 1;
2dc7f7b3
TT
9920 if (lh->version >= 4)
9921 {
9922 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9923 line_ptr += 1;
9924 }
9925 else
9926 lh->maximum_ops_per_instruction = 1;
9927
9928 if (lh->maximum_ops_per_instruction == 0)
9929 {
9930 lh->maximum_ops_per_instruction = 1;
9931 complaint (&symfile_complaints,
9932 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9933 }
9934
debd256d
JB
9935 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9936 line_ptr += 1;
9937 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9938 line_ptr += 1;
9939 lh->line_range = read_1_byte (abfd, line_ptr);
9940 line_ptr += 1;
9941 lh->opcode_base = read_1_byte (abfd, line_ptr);
9942 line_ptr += 1;
9943 lh->standard_opcode_lengths
fe1b8b76 9944 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9945
9946 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9947 for (i = 1; i < lh->opcode_base; ++i)
9948 {
9949 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9950 line_ptr += 1;
9951 }
9952
a738430d 9953 /* Read directory table. */
9b1c24c8 9954 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9955 {
9956 line_ptr += bytes_read;
9957 add_include_dir (lh, cur_dir);
9958 }
9959 line_ptr += bytes_read;
9960
a738430d 9961 /* Read file name table. */
9b1c24c8 9962 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9963 {
9964 unsigned int dir_index, mod_time, length;
9965
9966 line_ptr += bytes_read;
9967 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9968 line_ptr += bytes_read;
9969 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9970 line_ptr += bytes_read;
9971 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9972 line_ptr += bytes_read;
9973
9974 add_file_name (lh, cur_file, dir_index, mod_time, length);
9975 }
9976 line_ptr += bytes_read;
6e70227d 9977 lh->statement_program_start = line_ptr;
debd256d 9978
dce234bc
PP
9979 if (line_ptr > (dwarf2_per_objfile->line.buffer
9980 + dwarf2_per_objfile->line.size))
4d3c2250 9981 complaint (&symfile_complaints,
e2e0b3e5 9982 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9983
9984 discard_cleanups (back_to);
9985 return lh;
9986}
c906108c 9987
5fb290d7
DJ
9988/* This function exists to work around a bug in certain compilers
9989 (particularly GCC 2.95), in which the first line number marker of a
9990 function does not show up until after the prologue, right before
9991 the second line number marker. This function shifts ADDRESS down
9992 to the beginning of the function if necessary, and is called on
9993 addresses passed to record_line. */
9994
9995static CORE_ADDR
e142c38c 9996check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9997{
9998 struct function_range *fn;
9999
10000 /* Find the function_range containing address. */
e142c38c 10001 if (!cu->first_fn)
5fb290d7
DJ
10002 return address;
10003
e142c38c
DJ
10004 if (!cu->cached_fn)
10005 cu->cached_fn = cu->first_fn;
5fb290d7 10006
e142c38c 10007 fn = cu->cached_fn;
5fb290d7
DJ
10008 while (fn)
10009 if (fn->lowpc <= address && fn->highpc > address)
10010 goto found;
10011 else
10012 fn = fn->next;
10013
e142c38c
DJ
10014 fn = cu->first_fn;
10015 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10016 if (fn->lowpc <= address && fn->highpc > address)
10017 goto found;
10018 else
10019 fn = fn->next;
10020
10021 return address;
10022
10023 found:
10024 if (fn->seen_line)
10025 return address;
10026 if (address != fn->lowpc)
4d3c2250 10027 complaint (&symfile_complaints,
e2e0b3e5 10028 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10029 (unsigned long) address, fn->name);
5fb290d7
DJ
10030 fn->seen_line = 1;
10031 return fn->lowpc;
10032}
10033
c6da4cef
DE
10034/* Subroutine of dwarf_decode_lines to simplify it.
10035 Return the file name of the psymtab for included file FILE_INDEX
10036 in line header LH of PST.
10037 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10038 If space for the result is malloc'd, it will be freed by a cleanup.
10039 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10040
10041static char *
10042psymtab_include_file_name (const struct line_header *lh, int file_index,
10043 const struct partial_symtab *pst,
10044 const char *comp_dir)
10045{
10046 const struct file_entry fe = lh->file_names [file_index];
10047 char *include_name = fe.name;
10048 char *include_name_to_compare = include_name;
10049 char *dir_name = NULL;
72b9f47f
TT
10050 const char *pst_filename;
10051 char *copied_name = NULL;
c6da4cef
DE
10052 int file_is_pst;
10053
10054 if (fe.dir_index)
10055 dir_name = lh->include_dirs[fe.dir_index - 1];
10056
10057 if (!IS_ABSOLUTE_PATH (include_name)
10058 && (dir_name != NULL || comp_dir != NULL))
10059 {
10060 /* Avoid creating a duplicate psymtab for PST.
10061 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10062 Before we do the comparison, however, we need to account
10063 for DIR_NAME and COMP_DIR.
10064 First prepend dir_name (if non-NULL). If we still don't
10065 have an absolute path prepend comp_dir (if non-NULL).
10066 However, the directory we record in the include-file's
10067 psymtab does not contain COMP_DIR (to match the
10068 corresponding symtab(s)).
10069
10070 Example:
10071
10072 bash$ cd /tmp
10073 bash$ gcc -g ./hello.c
10074 include_name = "hello.c"
10075 dir_name = "."
10076 DW_AT_comp_dir = comp_dir = "/tmp"
10077 DW_AT_name = "./hello.c" */
10078
10079 if (dir_name != NULL)
10080 {
10081 include_name = concat (dir_name, SLASH_STRING,
10082 include_name, (char *)NULL);
10083 include_name_to_compare = include_name;
10084 make_cleanup (xfree, include_name);
10085 }
10086 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10087 {
10088 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10089 include_name, (char *)NULL);
10090 }
10091 }
10092
10093 pst_filename = pst->filename;
10094 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10095 {
72b9f47f
TT
10096 copied_name = concat (pst->dirname, SLASH_STRING,
10097 pst_filename, (char *)NULL);
10098 pst_filename = copied_name;
c6da4cef
DE
10099 }
10100
1e3fad37 10101 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10102
10103 if (include_name_to_compare != include_name)
10104 xfree (include_name_to_compare);
72b9f47f
TT
10105 if (copied_name != NULL)
10106 xfree (copied_name);
c6da4cef
DE
10107
10108 if (file_is_pst)
10109 return NULL;
10110 return include_name;
10111}
10112
aaa75496
JB
10113/* Decode the Line Number Program (LNP) for the given line_header
10114 structure and CU. The actual information extracted and the type
10115 of structures created from the LNP depends on the value of PST.
10116
10117 1. If PST is NULL, then this procedure uses the data from the program
10118 to create all necessary symbol tables, and their linetables.
6e70227d 10119
aaa75496
JB
10120 2. If PST is not NULL, this procedure reads the program to determine
10121 the list of files included by the unit represented by PST, and
c6da4cef
DE
10122 builds all the associated partial symbol tables.
10123
10124 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10125 It is used for relative paths in the line table.
10126 NOTE: When processing partial symtabs (pst != NULL),
10127 comp_dir == pst->dirname.
10128
10129 NOTE: It is important that psymtabs have the same file name (via strcmp)
10130 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10131 symtab we don't use it in the name of the psymtabs we create.
10132 E.g. expand_line_sal requires this when finding psymtabs to expand.
10133 A good testcase for this is mb-inline.exp. */
debd256d 10134
c906108c 10135static void
72b9f47f 10136dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10137 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10138{
a8c50c1f 10139 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10140 gdb_byte *line_end;
a8c50c1f 10141 unsigned int bytes_read, extended_len;
c906108c 10142 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10143 CORE_ADDR baseaddr;
10144 struct objfile *objfile = cu->objfile;
fbf65064 10145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10146 const int decode_for_pst_p = (pst != NULL);
cb1df416 10147 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10148
10149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10150
debd256d
JB
10151 line_ptr = lh->statement_program_start;
10152 line_end = lh->statement_program_end;
c906108c
SS
10153
10154 /* Read the statement sequences until there's nothing left. */
10155 while (line_ptr < line_end)
10156 {
10157 /* state machine registers */
10158 CORE_ADDR address = 0;
10159 unsigned int file = 1;
10160 unsigned int line = 1;
10161 unsigned int column = 0;
debd256d 10162 int is_stmt = lh->default_is_stmt;
c906108c
SS
10163 int basic_block = 0;
10164 int end_sequence = 0;
fbf65064 10165 CORE_ADDR addr;
2dc7f7b3 10166 unsigned char op_index = 0;
c906108c 10167
aaa75496 10168 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10169 {
aaa75496 10170 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10171 /* lh->include_dirs and lh->file_names are 0-based, but the
10172 directory and file name numbers in the statement program
10173 are 1-based. */
10174 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10175 char *dir = NULL;
a738430d 10176
debd256d
JB
10177 if (fe->dir_index)
10178 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10179
10180 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10181 }
10182
a738430d 10183 /* Decode the table. */
c5aa993b 10184 while (!end_sequence)
c906108c
SS
10185 {
10186 op_code = read_1_byte (abfd, line_ptr);
10187 line_ptr += 1;
59205f5a
JB
10188 if (line_ptr > line_end)
10189 {
10190 dwarf2_debug_line_missing_end_sequence_complaint ();
10191 break;
10192 }
9aa1fe7e 10193
debd256d 10194 if (op_code >= lh->opcode_base)
6e70227d 10195 {
a738430d 10196 /* Special operand. */
debd256d 10197 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10198 address += (((op_index + (adj_opcode / lh->line_range))
10199 / lh->maximum_ops_per_instruction)
10200 * lh->minimum_instruction_length);
10201 op_index = ((op_index + (adj_opcode / lh->line_range))
10202 % lh->maximum_ops_per_instruction);
debd256d 10203 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10204 if (lh->num_file_names < file || file == 0)
25e43795 10205 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10206 /* For now we ignore lines not starting on an
10207 instruction boundary. */
10208 else if (op_index == 0)
25e43795
DJ
10209 {
10210 lh->file_names[file - 1].included_p = 1;
ca5f395d 10211 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10212 {
10213 if (last_subfile != current_subfile)
10214 {
10215 addr = gdbarch_addr_bits_remove (gdbarch, address);
10216 if (last_subfile)
10217 record_line (last_subfile, 0, addr);
10218 last_subfile = current_subfile;
10219 }
25e43795 10220 /* Append row to matrix using current values. */
fbf65064
UW
10221 addr = check_cu_functions (address, cu);
10222 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10223 record_line (current_subfile, line, addr);
366da635 10224 }
25e43795 10225 }
ca5f395d 10226 basic_block = 0;
9aa1fe7e
GK
10227 }
10228 else switch (op_code)
c906108c
SS
10229 {
10230 case DW_LNS_extended_op:
a8c50c1f 10231 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10232 line_ptr += bytes_read;
a8c50c1f 10233 extended_end = line_ptr + extended_len;
c906108c
SS
10234 extended_op = read_1_byte (abfd, line_ptr);
10235 line_ptr += 1;
10236 switch (extended_op)
10237 {
10238 case DW_LNE_end_sequence:
10239 end_sequence = 1;
c906108c
SS
10240 break;
10241 case DW_LNE_set_address:
e7c27a73 10242 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10243 op_index = 0;
107d2387
AC
10244 line_ptr += bytes_read;
10245 address += baseaddr;
c906108c
SS
10246 break;
10247 case DW_LNE_define_file:
debd256d
JB
10248 {
10249 char *cur_file;
10250 unsigned int dir_index, mod_time, length;
6e70227d 10251
9b1c24c8 10252 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10253 line_ptr += bytes_read;
10254 dir_index =
10255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10256 line_ptr += bytes_read;
10257 mod_time =
10258 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10259 line_ptr += bytes_read;
10260 length =
10261 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10262 line_ptr += bytes_read;
10263 add_file_name (lh, cur_file, dir_index, mod_time, length);
10264 }
c906108c 10265 break;
d0c6ba3d
CC
10266 case DW_LNE_set_discriminator:
10267 /* The discriminator is not interesting to the debugger;
10268 just ignore it. */
10269 line_ptr = extended_end;
10270 break;
c906108c 10271 default:
4d3c2250 10272 complaint (&symfile_complaints,
e2e0b3e5 10273 _("mangled .debug_line section"));
debd256d 10274 return;
c906108c 10275 }
a8c50c1f
DJ
10276 /* Make sure that we parsed the extended op correctly. If e.g.
10277 we expected a different address size than the producer used,
10278 we may have read the wrong number of bytes. */
10279 if (line_ptr != extended_end)
10280 {
10281 complaint (&symfile_complaints,
10282 _("mangled .debug_line section"));
10283 return;
10284 }
c906108c
SS
10285 break;
10286 case DW_LNS_copy:
59205f5a 10287 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10288 dwarf2_debug_line_missing_file_complaint ();
10289 else
366da635 10290 {
25e43795 10291 lh->file_names[file - 1].included_p = 1;
ca5f395d 10292 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10293 {
10294 if (last_subfile != current_subfile)
10295 {
10296 addr = gdbarch_addr_bits_remove (gdbarch, address);
10297 if (last_subfile)
10298 record_line (last_subfile, 0, addr);
10299 last_subfile = current_subfile;
10300 }
10301 addr = check_cu_functions (address, cu);
10302 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10303 record_line (current_subfile, line, addr);
10304 }
366da635 10305 }
c906108c
SS
10306 basic_block = 0;
10307 break;
10308 case DW_LNS_advance_pc:
2dc7f7b3
TT
10309 {
10310 CORE_ADDR adjust
10311 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10312
10313 address += (((op_index + adjust)
10314 / lh->maximum_ops_per_instruction)
10315 * lh->minimum_instruction_length);
10316 op_index = ((op_index + adjust)
10317 % lh->maximum_ops_per_instruction);
10318 line_ptr += bytes_read;
10319 }
c906108c
SS
10320 break;
10321 case DW_LNS_advance_line:
10322 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10323 line_ptr += bytes_read;
10324 break;
10325 case DW_LNS_set_file:
debd256d 10326 {
a738430d
MK
10327 /* The arrays lh->include_dirs and lh->file_names are
10328 0-based, but the directory and file name numbers in
10329 the statement program are 1-based. */
debd256d 10330 struct file_entry *fe;
4f1520fb 10331 char *dir = NULL;
a738430d 10332
debd256d
JB
10333 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10334 line_ptr += bytes_read;
59205f5a 10335 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10336 dwarf2_debug_line_missing_file_complaint ();
10337 else
10338 {
10339 fe = &lh->file_names[file - 1];
10340 if (fe->dir_index)
10341 dir = lh->include_dirs[fe->dir_index - 1];
10342 if (!decode_for_pst_p)
10343 {
10344 last_subfile = current_subfile;
10345 dwarf2_start_subfile (fe->name, dir, comp_dir);
10346 }
10347 }
debd256d 10348 }
c906108c
SS
10349 break;
10350 case DW_LNS_set_column:
10351 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10352 line_ptr += bytes_read;
10353 break;
10354 case DW_LNS_negate_stmt:
10355 is_stmt = (!is_stmt);
10356 break;
10357 case DW_LNS_set_basic_block:
10358 basic_block = 1;
10359 break;
c2c6d25f
JM
10360 /* Add to the address register of the state machine the
10361 address increment value corresponding to special opcode
a738430d
MK
10362 255. I.e., this value is scaled by the minimum
10363 instruction length since special opcode 255 would have
10364 scaled the the increment. */
c906108c 10365 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10366 {
10367 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10368
10369 address += (((op_index + adjust)
10370 / lh->maximum_ops_per_instruction)
10371 * lh->minimum_instruction_length);
10372 op_index = ((op_index + adjust)
10373 % lh->maximum_ops_per_instruction);
10374 }
c906108c
SS
10375 break;
10376 case DW_LNS_fixed_advance_pc:
10377 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10378 op_index = 0;
c906108c
SS
10379 line_ptr += 2;
10380 break;
9aa1fe7e 10381 default:
a738430d
MK
10382 {
10383 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10384 int i;
a738430d 10385
debd256d 10386 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10387 {
10388 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10389 line_ptr += bytes_read;
10390 }
10391 }
c906108c
SS
10392 }
10393 }
59205f5a
JB
10394 if (lh->num_file_names < file || file == 0)
10395 dwarf2_debug_line_missing_file_complaint ();
10396 else
10397 {
10398 lh->file_names[file - 1].included_p = 1;
10399 if (!decode_for_pst_p)
fbf65064
UW
10400 {
10401 addr = gdbarch_addr_bits_remove (gdbarch, address);
10402 record_line (current_subfile, 0, addr);
10403 }
59205f5a 10404 }
c906108c 10405 }
aaa75496
JB
10406
10407 if (decode_for_pst_p)
10408 {
10409 int file_index;
10410
10411 /* Now that we're done scanning the Line Header Program, we can
10412 create the psymtab of each included file. */
10413 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10414 if (lh->file_names[file_index].included_p == 1)
10415 {
c6da4cef
DE
10416 char *include_name =
10417 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10418 if (include_name != NULL)
aaa75496
JB
10419 dwarf2_create_include_psymtab (include_name, pst, objfile);
10420 }
10421 }
cb1df416
DJ
10422 else
10423 {
10424 /* Make sure a symtab is created for every file, even files
10425 which contain only variables (i.e. no code with associated
10426 line numbers). */
10427
10428 int i;
10429 struct file_entry *fe;
10430
10431 for (i = 0; i < lh->num_file_names; i++)
10432 {
10433 char *dir = NULL;
9a619af0 10434
cb1df416
DJ
10435 fe = &lh->file_names[i];
10436 if (fe->dir_index)
10437 dir = lh->include_dirs[fe->dir_index - 1];
10438 dwarf2_start_subfile (fe->name, dir, comp_dir);
10439
10440 /* Skip the main file; we don't need it, and it must be
10441 allocated last, so that it will show up before the
10442 non-primary symtabs in the objfile's symtab list. */
10443 if (current_subfile == first_subfile)
10444 continue;
10445
10446 if (current_subfile->symtab == NULL)
10447 current_subfile->symtab = allocate_symtab (current_subfile->name,
10448 cu->objfile);
10449 fe->symtab = current_subfile->symtab;
10450 }
10451 }
c906108c
SS
10452}
10453
10454/* Start a subfile for DWARF. FILENAME is the name of the file and
10455 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10456 or NULL if not known. COMP_DIR is the compilation directory for the
10457 linetable's compilation unit or NULL if not known.
c906108c
SS
10458 This routine tries to keep line numbers from identical absolute and
10459 relative file names in a common subfile.
10460
10461 Using the `list' example from the GDB testsuite, which resides in
10462 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10463 of /srcdir/list0.c yields the following debugging information for list0.c:
10464
c5aa993b
JM
10465 DW_AT_name: /srcdir/list0.c
10466 DW_AT_comp_dir: /compdir
357e46e7 10467 files.files[0].name: list0.h
c5aa993b 10468 files.files[0].dir: /srcdir
357e46e7 10469 files.files[1].name: list0.c
c5aa993b 10470 files.files[1].dir: /srcdir
c906108c
SS
10471
10472 The line number information for list0.c has to end up in a single
4f1520fb
FR
10473 subfile, so that `break /srcdir/list0.c:1' works as expected.
10474 start_subfile will ensure that this happens provided that we pass the
10475 concatenation of files.files[1].dir and files.files[1].name as the
10476 subfile's name. */
c906108c
SS
10477
10478static void
72b9f47f 10479dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10480{
4f1520fb
FR
10481 char *fullname;
10482
10483 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10484 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10485 second argument to start_subfile. To be consistent, we do the
10486 same here. In order not to lose the line information directory,
10487 we concatenate it to the filename when it makes sense.
10488 Note that the Dwarf3 standard says (speaking of filenames in line
10489 information): ``The directory index is ignored for file names
10490 that represent full path names''. Thus ignoring dirname in the
10491 `else' branch below isn't an issue. */
c906108c 10492
d5166ae1 10493 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10494 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10495 else
10496 fullname = filename;
c906108c 10497
4f1520fb
FR
10498 start_subfile (fullname, comp_dir);
10499
10500 if (fullname != filename)
10501 xfree (fullname);
c906108c
SS
10502}
10503
4c2df51b
DJ
10504static void
10505var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10506 struct dwarf2_cu *cu)
4c2df51b 10507{
e7c27a73
DJ
10508 struct objfile *objfile = cu->objfile;
10509 struct comp_unit_head *cu_header = &cu->header;
10510
4c2df51b
DJ
10511 /* NOTE drow/2003-01-30: There used to be a comment and some special
10512 code here to turn a symbol with DW_AT_external and a
10513 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10514 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10515 with some versions of binutils) where shared libraries could have
10516 relocations against symbols in their debug information - the
10517 minimal symbol would have the right address, but the debug info
10518 would not. It's no longer necessary, because we will explicitly
10519 apply relocations when we read in the debug information now. */
10520
10521 /* A DW_AT_location attribute with no contents indicates that a
10522 variable has been optimized away. */
10523 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10524 {
10525 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10526 return;
10527 }
10528
10529 /* Handle one degenerate form of location expression specially, to
10530 preserve GDB's previous behavior when section offsets are
10531 specified. If this is just a DW_OP_addr then mark this symbol
10532 as LOC_STATIC. */
10533
10534 if (attr_form_is_block (attr)
10535 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10536 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10537 {
891d2f0b 10538 unsigned int dummy;
4c2df51b
DJ
10539
10540 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10541 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10542 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10543 fixup_symbol_section (sym, objfile);
10544 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10545 SYMBOL_SECTION (sym));
4c2df51b
DJ
10546 return;
10547 }
10548
10549 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10550 expression evaluator, and use LOC_COMPUTED only when necessary
10551 (i.e. when the value of a register or memory location is
10552 referenced, or a thread-local block, etc.). Then again, it might
10553 not be worthwhile. I'm assuming that it isn't unless performance
10554 or memory numbers show me otherwise. */
10555
e7c27a73 10556 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10557 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10558}
10559
c906108c
SS
10560/* Given a pointer to a DWARF information entry, figure out if we need
10561 to make a symbol table entry for it, and if so, create a new entry
10562 and return a pointer to it.
10563 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10564 used the passed type.
10565 If SPACE is not NULL, use it to hold the new symbol. If it is
10566 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10567
10568static struct symbol *
34eaf542
TT
10569new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10570 struct symbol *space)
c906108c 10571{
e7c27a73 10572 struct objfile *objfile = cu->objfile;
c906108c
SS
10573 struct symbol *sym = NULL;
10574 char *name;
10575 struct attribute *attr = NULL;
10576 struct attribute *attr2 = NULL;
e142c38c 10577 CORE_ADDR baseaddr;
e37fd15a
SW
10578 struct pending **list_to_add = NULL;
10579
edb3359d 10580 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10581
10582 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10583
94af9270 10584 name = dwarf2_name (die, cu);
c906108c
SS
10585 if (name)
10586 {
94af9270 10587 const char *linkagename;
34eaf542 10588 int suppress_add = 0;
94af9270 10589
34eaf542
TT
10590 if (space)
10591 sym = space;
10592 else
10593 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10594 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10595
10596 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10597 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10598 linkagename = dwarf2_physname (name, die, cu);
10599 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10600
f55ee35c
JK
10601 /* Fortran does not have mangling standard and the mangling does differ
10602 between gfortran, iFort etc. */
10603 if (cu->language == language_fortran
b250c185 10604 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10605 symbol_set_demangled_name (&(sym->ginfo),
10606 (char *) dwarf2_full_name (name, die, cu),
10607 NULL);
f55ee35c 10608
c906108c 10609 /* Default assumptions.
c5aa993b 10610 Use the passed type or decode it from the die. */
176620f1 10611 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10612 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10613 if (type != NULL)
10614 SYMBOL_TYPE (sym) = type;
10615 else
e7c27a73 10616 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10617 attr = dwarf2_attr (die,
10618 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10619 cu);
c906108c
SS
10620 if (attr)
10621 {
10622 SYMBOL_LINE (sym) = DW_UNSND (attr);
10623 }
cb1df416 10624
edb3359d
DJ
10625 attr = dwarf2_attr (die,
10626 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10627 cu);
cb1df416
DJ
10628 if (attr)
10629 {
10630 int file_index = DW_UNSND (attr);
9a619af0 10631
cb1df416
DJ
10632 if (cu->line_header == NULL
10633 || file_index > cu->line_header->num_file_names)
10634 complaint (&symfile_complaints,
10635 _("file index out of range"));
1c3d648d 10636 else if (file_index > 0)
cb1df416
DJ
10637 {
10638 struct file_entry *fe;
9a619af0 10639
cb1df416
DJ
10640 fe = &cu->line_header->file_names[file_index - 1];
10641 SYMBOL_SYMTAB (sym) = fe->symtab;
10642 }
10643 }
10644
c906108c
SS
10645 switch (die->tag)
10646 {
10647 case DW_TAG_label:
e142c38c 10648 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10649 if (attr)
10650 {
10651 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10652 }
0f5238ed
TT
10653 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10654 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10655 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10656 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10657 break;
10658 case DW_TAG_subprogram:
10659 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10660 finish_block. */
10661 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10662 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10663 if ((attr2 && (DW_UNSND (attr2) != 0))
10664 || cu->language == language_ada)
c906108c 10665 {
2cfa0c8d
JB
10666 /* Subprograms marked external are stored as a global symbol.
10667 Ada subprograms, whether marked external or not, are always
10668 stored as a global symbol, because we want to be able to
10669 access them globally. For instance, we want to be able
10670 to break on a nested subprogram without having to
10671 specify the context. */
e37fd15a 10672 list_to_add = &global_symbols;
c906108c
SS
10673 }
10674 else
10675 {
e37fd15a 10676 list_to_add = cu->list_in_scope;
c906108c
SS
10677 }
10678 break;
edb3359d
DJ
10679 case DW_TAG_inlined_subroutine:
10680 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10681 finish_block. */
10682 SYMBOL_CLASS (sym) = LOC_BLOCK;
10683 SYMBOL_INLINED (sym) = 1;
10684 /* Do not add the symbol to any lists. It will be found via
10685 BLOCK_FUNCTION from the blockvector. */
10686 break;
34eaf542
TT
10687 case DW_TAG_template_value_param:
10688 suppress_add = 1;
10689 /* Fall through. */
72929c62 10690 case DW_TAG_constant:
c906108c 10691 case DW_TAG_variable:
254e6b9e 10692 case DW_TAG_member:
c906108c
SS
10693 /* Compilation with minimal debug info may result in variables
10694 with missing type entries. Change the misleading `void' type
10695 to something sensible. */
10696 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10697 SYMBOL_TYPE (sym)
46bf5051 10698 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10699
e142c38c 10700 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10701 /* In the case of DW_TAG_member, we should only be called for
10702 static const members. */
10703 if (die->tag == DW_TAG_member)
10704 {
3863f96c
DE
10705 /* dwarf2_add_field uses die_is_declaration,
10706 so we do the same. */
254e6b9e
DE
10707 gdb_assert (die_is_declaration (die, cu));
10708 gdb_assert (attr);
10709 }
c906108c
SS
10710 if (attr)
10711 {
e7c27a73 10712 dwarf2_const_value (attr, sym, cu);
e142c38c 10713 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10714 if (!suppress_add)
34eaf542
TT
10715 {
10716 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10717 list_to_add = &global_symbols;
34eaf542 10718 else
e37fd15a 10719 list_to_add = cu->list_in_scope;
34eaf542 10720 }
c906108c
SS
10721 break;
10722 }
e142c38c 10723 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10724 if (attr)
10725 {
e7c27a73 10726 var_decode_location (attr, sym, cu);
e142c38c 10727 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10728 if (SYMBOL_CLASS (sym) == LOC_STATIC
10729 && SYMBOL_VALUE_ADDRESS (sym) == 0
10730 && !dwarf2_per_objfile->has_section_at_zero)
10731 {
10732 /* When a static variable is eliminated by the linker,
10733 the corresponding debug information is not stripped
10734 out, but the variable address is set to null;
10735 do not add such variables into symbol table. */
10736 }
10737 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10738 {
f55ee35c
JK
10739 /* Workaround gfortran PR debug/40040 - it uses
10740 DW_AT_location for variables in -fPIC libraries which may
10741 get overriden by other libraries/executable and get
10742 a different address. Resolve it by the minimal symbol
10743 which may come from inferior's executable using copy
10744 relocation. Make this workaround only for gfortran as for
10745 other compilers GDB cannot guess the minimal symbol
10746 Fortran mangling kind. */
10747 if (cu->language == language_fortran && die->parent
10748 && die->parent->tag == DW_TAG_module
10749 && cu->producer
10750 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10751 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10752
1c809c68
TT
10753 /* A variable with DW_AT_external is never static,
10754 but it may be block-scoped. */
10755 list_to_add = (cu->list_in_scope == &file_symbols
10756 ? &global_symbols : cu->list_in_scope);
1c809c68 10757 }
c906108c 10758 else
e37fd15a 10759 list_to_add = cu->list_in_scope;
c906108c
SS
10760 }
10761 else
10762 {
10763 /* We do not know the address of this symbol.
c5aa993b
JM
10764 If it is an external symbol and we have type information
10765 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10766 The address of the variable will then be determined from
10767 the minimal symbol table whenever the variable is
10768 referenced. */
e142c38c 10769 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10770 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10771 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10772 {
0fe7935b
DJ
10773 /* A variable with DW_AT_external is never static, but it
10774 may be block-scoped. */
10775 list_to_add = (cu->list_in_scope == &file_symbols
10776 ? &global_symbols : cu->list_in_scope);
10777
c906108c 10778 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10779 }
442ddf59
JK
10780 else if (!die_is_declaration (die, cu))
10781 {
10782 /* Use the default LOC_OPTIMIZED_OUT class. */
10783 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10784 if (!suppress_add)
10785 list_to_add = cu->list_in_scope;
442ddf59 10786 }
c906108c
SS
10787 }
10788 break;
10789 case DW_TAG_formal_parameter:
edb3359d
DJ
10790 /* If we are inside a function, mark this as an argument. If
10791 not, we might be looking at an argument to an inlined function
10792 when we do not have enough information to show inlined frames;
10793 pretend it's a local variable in that case so that the user can
10794 still see it. */
10795 if (context_stack_depth > 0
10796 && context_stack[context_stack_depth - 1].name != NULL)
10797 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10798 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10799 if (attr)
10800 {
e7c27a73 10801 var_decode_location (attr, sym, cu);
c906108c 10802 }
e142c38c 10803 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10804 if (attr)
10805 {
e7c27a73 10806 dwarf2_const_value (attr, sym, cu);
c906108c 10807 }
f346a30d
PM
10808 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10809 if (attr && DW_UNSND (attr))
10810 {
10811 struct type *ref_type;
10812
10813 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10814 SYMBOL_TYPE (sym) = ref_type;
10815 }
10816
e37fd15a 10817 list_to_add = cu->list_in_scope;
c906108c
SS
10818 break;
10819 case DW_TAG_unspecified_parameters:
10820 /* From varargs functions; gdb doesn't seem to have any
10821 interest in this information, so just ignore it for now.
10822 (FIXME?) */
10823 break;
34eaf542
TT
10824 case DW_TAG_template_type_param:
10825 suppress_add = 1;
10826 /* Fall through. */
c906108c 10827 case DW_TAG_class_type:
680b30c7 10828 case DW_TAG_interface_type:
c906108c
SS
10829 case DW_TAG_structure_type:
10830 case DW_TAG_union_type:
72019c9c 10831 case DW_TAG_set_type:
c906108c
SS
10832 case DW_TAG_enumeration_type:
10833 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10834 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10835
63d06c5c 10836 {
987504bb 10837 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10838 really ever be static objects: otherwise, if you try
10839 to, say, break of a class's method and you're in a file
10840 which doesn't mention that class, it won't work unless
10841 the check for all static symbols in lookup_symbol_aux
10842 saves you. See the OtherFileClass tests in
10843 gdb.c++/namespace.exp. */
10844
e37fd15a 10845 if (!suppress_add)
34eaf542 10846 {
34eaf542
TT
10847 list_to_add = (cu->list_in_scope == &file_symbols
10848 && (cu->language == language_cplus
10849 || cu->language == language_java)
10850 ? &global_symbols : cu->list_in_scope);
63d06c5c 10851
64382290
TT
10852 /* The semantics of C++ state that "struct foo {
10853 ... }" also defines a typedef for "foo". A Java
10854 class declaration also defines a typedef for the
10855 class. */
10856 if (cu->language == language_cplus
10857 || cu->language == language_java
10858 || cu->language == language_ada)
10859 {
10860 /* The symbol's name is already allocated along
10861 with this objfile, so we don't need to
10862 duplicate it for the type. */
10863 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10864 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10865 }
63d06c5c
DC
10866 }
10867 }
c906108c
SS
10868 break;
10869 case DW_TAG_typedef:
63d06c5c
DC
10870 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10871 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10872 list_to_add = cu->list_in_scope;
63d06c5c 10873 break;
c906108c 10874 case DW_TAG_base_type:
a02abb62 10875 case DW_TAG_subrange_type:
c906108c 10876 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10877 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10878 list_to_add = cu->list_in_scope;
c906108c
SS
10879 break;
10880 case DW_TAG_enumerator:
e142c38c 10881 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10882 if (attr)
10883 {
e7c27a73 10884 dwarf2_const_value (attr, sym, cu);
c906108c 10885 }
63d06c5c
DC
10886 {
10887 /* NOTE: carlton/2003-11-10: See comment above in the
10888 DW_TAG_class_type, etc. block. */
10889
e142c38c 10890 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10891 && (cu->language == language_cplus
10892 || cu->language == language_java)
e142c38c 10893 ? &global_symbols : cu->list_in_scope);
63d06c5c 10894 }
c906108c 10895 break;
5c4e30ca
DC
10896 case DW_TAG_namespace:
10897 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10898 list_to_add = &global_symbols;
5c4e30ca 10899 break;
c906108c
SS
10900 default:
10901 /* Not a tag we recognize. Hopefully we aren't processing
10902 trash data, but since we must specifically ignore things
10903 we don't recognize, there is nothing else we should do at
10904 this point. */
e2e0b3e5 10905 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10906 dwarf_tag_name (die->tag));
c906108c
SS
10907 break;
10908 }
df8a16a1 10909
e37fd15a
SW
10910 if (suppress_add)
10911 {
10912 sym->hash_next = objfile->template_symbols;
10913 objfile->template_symbols = sym;
10914 list_to_add = NULL;
10915 }
10916
10917 if (list_to_add != NULL)
10918 add_symbol_to_list (sym, list_to_add);
10919
df8a16a1
DJ
10920 /* For the benefit of old versions of GCC, check for anonymous
10921 namespaces based on the demangled name. */
10922 if (!processing_has_namespace_info
94af9270 10923 && cu->language == language_cplus)
df8a16a1 10924 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10925 }
10926 return (sym);
10927}
10928
34eaf542
TT
10929/* A wrapper for new_symbol_full that always allocates a new symbol. */
10930
10931static struct symbol *
10932new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10933{
10934 return new_symbol_full (die, type, cu, NULL);
10935}
10936
98bfdba5
PA
10937/* Given an attr with a DW_FORM_dataN value in host byte order,
10938 zero-extend it as appropriate for the symbol's type. The DWARF
10939 standard (v4) is not entirely clear about the meaning of using
10940 DW_FORM_dataN for a constant with a signed type, where the type is
10941 wider than the data. The conclusion of a discussion on the DWARF
10942 list was that this is unspecified. We choose to always zero-extend
10943 because that is the interpretation long in use by GCC. */
c906108c 10944
98bfdba5
PA
10945static gdb_byte *
10946dwarf2_const_value_data (struct attribute *attr, struct type *type,
10947 const char *name, struct obstack *obstack,
10948 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10949{
e7c27a73 10950 struct objfile *objfile = cu->objfile;
e17a4113
UW
10951 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10952 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10953 LONGEST l = DW_UNSND (attr);
10954
10955 if (bits < sizeof (*value) * 8)
10956 {
10957 l &= ((LONGEST) 1 << bits) - 1;
10958 *value = l;
10959 }
10960 else if (bits == sizeof (*value) * 8)
10961 *value = l;
10962 else
10963 {
10964 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10965 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10966 return bytes;
10967 }
10968
10969 return NULL;
10970}
10971
10972/* Read a constant value from an attribute. Either set *VALUE, or if
10973 the value does not fit in *VALUE, set *BYTES - either already
10974 allocated on the objfile obstack, or newly allocated on OBSTACK,
10975 or, set *BATON, if we translated the constant to a location
10976 expression. */
10977
10978static void
10979dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10980 const char *name, struct obstack *obstack,
10981 struct dwarf2_cu *cu,
10982 long *value, gdb_byte **bytes,
10983 struct dwarf2_locexpr_baton **baton)
10984{
10985 struct objfile *objfile = cu->objfile;
10986 struct comp_unit_head *cu_header = &cu->header;
c906108c 10987 struct dwarf_block *blk;
98bfdba5
PA
10988 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10989 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10990
10991 *value = 0;
10992 *bytes = NULL;
10993 *baton = NULL;
c906108c
SS
10994
10995 switch (attr->form)
10996 {
10997 case DW_FORM_addr:
ac56253d 10998 {
ac56253d
TT
10999 gdb_byte *data;
11000
98bfdba5
PA
11001 if (TYPE_LENGTH (type) != cu_header->addr_size)
11002 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11003 cu_header->addr_size,
98bfdba5 11004 TYPE_LENGTH (type));
ac56253d
TT
11005 /* Symbols of this form are reasonably rare, so we just
11006 piggyback on the existing location code rather than writing
11007 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11008 *baton = obstack_alloc (&objfile->objfile_obstack,
11009 sizeof (struct dwarf2_locexpr_baton));
11010 (*baton)->per_cu = cu->per_cu;
11011 gdb_assert ((*baton)->per_cu);
ac56253d 11012
98bfdba5
PA
11013 (*baton)->size = 2 + cu_header->addr_size;
11014 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11015 (*baton)->data = data;
ac56253d
TT
11016
11017 data[0] = DW_OP_addr;
11018 store_unsigned_integer (&data[1], cu_header->addr_size,
11019 byte_order, DW_ADDR (attr));
11020 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11021 }
c906108c 11022 break;
4ac36638 11023 case DW_FORM_string:
93b5768b 11024 case DW_FORM_strp:
98bfdba5
PA
11025 /* DW_STRING is already allocated on the objfile obstack, point
11026 directly to it. */
11027 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11028 break;
c906108c
SS
11029 case DW_FORM_block1:
11030 case DW_FORM_block2:
11031 case DW_FORM_block4:
11032 case DW_FORM_block:
2dc7f7b3 11033 case DW_FORM_exprloc:
c906108c 11034 blk = DW_BLOCK (attr);
98bfdba5
PA
11035 if (TYPE_LENGTH (type) != blk->size)
11036 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11037 TYPE_LENGTH (type));
11038 *bytes = blk->data;
c906108c 11039 break;
2df3850c
JM
11040
11041 /* The DW_AT_const_value attributes are supposed to carry the
11042 symbol's value "represented as it would be on the target
11043 architecture." By the time we get here, it's already been
11044 converted to host endianness, so we just need to sign- or
11045 zero-extend it as appropriate. */
11046 case DW_FORM_data1:
98bfdba5 11047 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11048 break;
c906108c 11049 case DW_FORM_data2:
98bfdba5 11050 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11051 break;
c906108c 11052 case DW_FORM_data4:
98bfdba5 11053 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11054 break;
c906108c 11055 case DW_FORM_data8:
98bfdba5 11056 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11057 break;
11058
c906108c 11059 case DW_FORM_sdata:
98bfdba5 11060 *value = DW_SND (attr);
2df3850c
JM
11061 break;
11062
c906108c 11063 case DW_FORM_udata:
98bfdba5 11064 *value = DW_UNSND (attr);
c906108c 11065 break;
2df3850c 11066
c906108c 11067 default:
4d3c2250 11068 complaint (&symfile_complaints,
e2e0b3e5 11069 _("unsupported const value attribute form: '%s'"),
4d3c2250 11070 dwarf_form_name (attr->form));
98bfdba5 11071 *value = 0;
c906108c
SS
11072 break;
11073 }
11074}
11075
2df3850c 11076
98bfdba5
PA
11077/* Copy constant value from an attribute to a symbol. */
11078
2df3850c 11079static void
98bfdba5
PA
11080dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11081 struct dwarf2_cu *cu)
2df3850c 11082{
98bfdba5
PA
11083 struct objfile *objfile = cu->objfile;
11084 struct comp_unit_head *cu_header = &cu->header;
11085 long value;
11086 gdb_byte *bytes;
11087 struct dwarf2_locexpr_baton *baton;
2df3850c 11088
98bfdba5
PA
11089 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11090 SYMBOL_PRINT_NAME (sym),
11091 &objfile->objfile_obstack, cu,
11092 &value, &bytes, &baton);
2df3850c 11093
98bfdba5
PA
11094 if (baton != NULL)
11095 {
11096 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11097 SYMBOL_LOCATION_BATON (sym) = baton;
11098 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11099 }
11100 else if (bytes != NULL)
11101 {
11102 SYMBOL_VALUE_BYTES (sym) = bytes;
11103 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11104 }
11105 else
11106 {
11107 SYMBOL_VALUE (sym) = value;
11108 SYMBOL_CLASS (sym) = LOC_CONST;
11109 }
2df3850c
JM
11110}
11111
c906108c
SS
11112/* Return the type of the die in question using its DW_AT_type attribute. */
11113
11114static struct type *
e7c27a73 11115die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11116{
c906108c 11117 struct attribute *type_attr;
c906108c 11118
e142c38c 11119 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11120 if (!type_attr)
11121 {
11122 /* A missing DW_AT_type represents a void type. */
46bf5051 11123 return objfile_type (cu->objfile)->builtin_void;
c906108c 11124 }
348e048f 11125
673bfd45 11126 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11127}
11128
b4ba55a1
JB
11129/* True iff CU's producer generates GNAT Ada auxiliary information
11130 that allows to find parallel types through that information instead
11131 of having to do expensive parallel lookups by type name. */
11132
11133static int
11134need_gnat_info (struct dwarf2_cu *cu)
11135{
11136 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11137 of GNAT produces this auxiliary information, without any indication
11138 that it is produced. Part of enhancing the FSF version of GNAT
11139 to produce that information will be to put in place an indicator
11140 that we can use in order to determine whether the descriptive type
11141 info is available or not. One suggestion that has been made is
11142 to use a new attribute, attached to the CU die. For now, assume
11143 that the descriptive type info is not available. */
11144 return 0;
11145}
11146
b4ba55a1
JB
11147/* Return the auxiliary type of the die in question using its
11148 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11149 attribute is not present. */
11150
11151static struct type *
11152die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11153{
b4ba55a1 11154 struct attribute *type_attr;
b4ba55a1
JB
11155
11156 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11157 if (!type_attr)
11158 return NULL;
11159
673bfd45 11160 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11161}
11162
11163/* If DIE has a descriptive_type attribute, then set the TYPE's
11164 descriptive type accordingly. */
11165
11166static void
11167set_descriptive_type (struct type *type, struct die_info *die,
11168 struct dwarf2_cu *cu)
11169{
11170 struct type *descriptive_type = die_descriptive_type (die, cu);
11171
11172 if (descriptive_type)
11173 {
11174 ALLOCATE_GNAT_AUX_TYPE (type);
11175 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11176 }
11177}
11178
c906108c
SS
11179/* Return the containing type of the die in question using its
11180 DW_AT_containing_type attribute. */
11181
11182static struct type *
e7c27a73 11183die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11184{
c906108c 11185 struct attribute *type_attr;
c906108c 11186
e142c38c 11187 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11188 if (!type_attr)
11189 error (_("Dwarf Error: Problem turning containing type into gdb type "
11190 "[in module %s]"), cu->objfile->name);
11191
673bfd45 11192 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11193}
11194
673bfd45
DE
11195/* Look up the type of DIE in CU using its type attribute ATTR.
11196 If there is no type substitute an error marker. */
11197
c906108c 11198static struct type *
673bfd45
DE
11199lookup_die_type (struct die_info *die, struct attribute *attr,
11200 struct dwarf2_cu *cu)
c906108c 11201{
f792889a
DJ
11202 struct type *this_type;
11203
673bfd45
DE
11204 /* First see if we have it cached. */
11205
11206 if (is_ref_attr (attr))
11207 {
11208 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11209
11210 this_type = get_die_type_at_offset (offset, cu->per_cu);
11211 }
11212 else if (attr->form == DW_FORM_sig8)
11213 {
11214 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11215 struct dwarf2_cu *sig_cu;
11216 unsigned int offset;
11217
11218 /* sig_type will be NULL if the signatured type is missing from
11219 the debug info. */
11220 if (sig_type == NULL)
11221 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11222 "at 0x%x [in module %s]"),
11223 die->offset, cu->objfile->name);
11224
11225 gdb_assert (sig_type->per_cu.from_debug_types);
11226 offset = sig_type->offset + sig_type->type_offset;
11227 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11228 }
11229 else
11230 {
11231 dump_die_for_error (die);
11232 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11233 dwarf_attr_name (attr->name), cu->objfile->name);
11234 }
11235
11236 /* If not cached we need to read it in. */
11237
11238 if (this_type == NULL)
11239 {
11240 struct die_info *type_die;
11241 struct dwarf2_cu *type_cu = cu;
11242
11243 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11244 /* If the type is cached, we should have found it above. */
11245 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11246 this_type = read_type_die_1 (type_die, type_cu);
11247 }
11248
11249 /* If we still don't have a type use an error marker. */
11250
11251 if (this_type == NULL)
c906108c 11252 {
b00fdb78
TT
11253 char *message, *saved;
11254
11255 /* read_type_die already issued a complaint. */
11256 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11257 cu->objfile->name,
11258 cu->header.offset,
11259 die->offset);
11260 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11261 message, strlen (message));
11262 xfree (message);
11263
11264 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11265 }
673bfd45 11266
f792889a 11267 return this_type;
c906108c
SS
11268}
11269
673bfd45
DE
11270/* Return the type in DIE, CU.
11271 Returns NULL for invalid types.
11272
11273 This first does a lookup in the appropriate type_hash table,
11274 and only reads the die in if necessary.
11275
11276 NOTE: This can be called when reading in partial or full symbols. */
11277
f792889a 11278static struct type *
e7c27a73 11279read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11280{
f792889a
DJ
11281 struct type *this_type;
11282
11283 this_type = get_die_type (die, cu);
11284 if (this_type)
11285 return this_type;
11286
673bfd45
DE
11287 return read_type_die_1 (die, cu);
11288}
11289
11290/* Read the type in DIE, CU.
11291 Returns NULL for invalid types. */
11292
11293static struct type *
11294read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11295{
11296 struct type *this_type = NULL;
11297
c906108c
SS
11298 switch (die->tag)
11299 {
11300 case DW_TAG_class_type:
680b30c7 11301 case DW_TAG_interface_type:
c906108c
SS
11302 case DW_TAG_structure_type:
11303 case DW_TAG_union_type:
f792889a 11304 this_type = read_structure_type (die, cu);
c906108c
SS
11305 break;
11306 case DW_TAG_enumeration_type:
f792889a 11307 this_type = read_enumeration_type (die, cu);
c906108c
SS
11308 break;
11309 case DW_TAG_subprogram:
11310 case DW_TAG_subroutine_type:
edb3359d 11311 case DW_TAG_inlined_subroutine:
f792889a 11312 this_type = read_subroutine_type (die, cu);
c906108c
SS
11313 break;
11314 case DW_TAG_array_type:
f792889a 11315 this_type = read_array_type (die, cu);
c906108c 11316 break;
72019c9c 11317 case DW_TAG_set_type:
f792889a 11318 this_type = read_set_type (die, cu);
72019c9c 11319 break;
c906108c 11320 case DW_TAG_pointer_type:
f792889a 11321 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11322 break;
11323 case DW_TAG_ptr_to_member_type:
f792889a 11324 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11325 break;
11326 case DW_TAG_reference_type:
f792889a 11327 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11328 break;
11329 case DW_TAG_const_type:
f792889a 11330 this_type = read_tag_const_type (die, cu);
c906108c
SS
11331 break;
11332 case DW_TAG_volatile_type:
f792889a 11333 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11334 break;
11335 case DW_TAG_string_type:
f792889a 11336 this_type = read_tag_string_type (die, cu);
c906108c
SS
11337 break;
11338 case DW_TAG_typedef:
f792889a 11339 this_type = read_typedef (die, cu);
c906108c 11340 break;
a02abb62 11341 case DW_TAG_subrange_type:
f792889a 11342 this_type = read_subrange_type (die, cu);
a02abb62 11343 break;
c906108c 11344 case DW_TAG_base_type:
f792889a 11345 this_type = read_base_type (die, cu);
c906108c 11346 break;
81a17f79 11347 case DW_TAG_unspecified_type:
f792889a 11348 this_type = read_unspecified_type (die, cu);
81a17f79 11349 break;
0114d602
DJ
11350 case DW_TAG_namespace:
11351 this_type = read_namespace_type (die, cu);
11352 break;
f55ee35c
JK
11353 case DW_TAG_module:
11354 this_type = read_module_type (die, cu);
11355 break;
c906108c 11356 default:
a1f5b845 11357 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11358 dwarf_tag_name (die->tag));
c906108c
SS
11359 break;
11360 }
63d06c5c 11361
f792889a 11362 return this_type;
63d06c5c
DC
11363}
11364
abc72ce4
DE
11365/* See if we can figure out if the class lives in a namespace. We do
11366 this by looking for a member function; its demangled name will
11367 contain namespace info, if there is any.
11368 Return the computed name or NULL.
11369 Space for the result is allocated on the objfile's obstack.
11370 This is the full-die version of guess_partial_die_structure_name.
11371 In this case we know DIE has no useful parent. */
11372
11373static char *
11374guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11375{
11376 struct die_info *spec_die;
11377 struct dwarf2_cu *spec_cu;
11378 struct die_info *child;
11379
11380 spec_cu = cu;
11381 spec_die = die_specification (die, &spec_cu);
11382 if (spec_die != NULL)
11383 {
11384 die = spec_die;
11385 cu = spec_cu;
11386 }
11387
11388 for (child = die->child;
11389 child != NULL;
11390 child = child->sibling)
11391 {
11392 if (child->tag == DW_TAG_subprogram)
11393 {
11394 struct attribute *attr;
11395
11396 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11397 if (attr == NULL)
11398 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11399 if (attr != NULL)
11400 {
11401 char *actual_name
11402 = language_class_name_from_physname (cu->language_defn,
11403 DW_STRING (attr));
11404 char *name = NULL;
11405
11406 if (actual_name != NULL)
11407 {
11408 char *die_name = dwarf2_name (die, cu);
11409
11410 if (die_name != NULL
11411 && strcmp (die_name, actual_name) != 0)
11412 {
11413 /* Strip off the class name from the full name.
11414 We want the prefix. */
11415 int die_name_len = strlen (die_name);
11416 int actual_name_len = strlen (actual_name);
11417
11418 /* Test for '::' as a sanity check. */
11419 if (actual_name_len > die_name_len + 2
11420 && actual_name[actual_name_len - die_name_len - 1] == ':')
11421 name =
11422 obsavestring (actual_name,
11423 actual_name_len - die_name_len - 2,
11424 &cu->objfile->objfile_obstack);
11425 }
11426 }
11427 xfree (actual_name);
11428 return name;
11429 }
11430 }
11431 }
11432
11433 return NULL;
11434}
11435
fdde2d81 11436/* Return the name of the namespace/class that DIE is defined within,
0114d602 11437 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11438
0114d602
DJ
11439 For example, if we're within the method foo() in the following
11440 code:
11441
11442 namespace N {
11443 class C {
11444 void foo () {
11445 }
11446 };
11447 }
11448
11449 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11450
11451static char *
e142c38c 11452determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11453{
0114d602
DJ
11454 struct die_info *parent, *spec_die;
11455 struct dwarf2_cu *spec_cu;
11456 struct type *parent_type;
63d06c5c 11457
f55ee35c
JK
11458 if (cu->language != language_cplus && cu->language != language_java
11459 && cu->language != language_fortran)
0114d602
DJ
11460 return "";
11461
11462 /* We have to be careful in the presence of DW_AT_specification.
11463 For example, with GCC 3.4, given the code
11464
11465 namespace N {
11466 void foo() {
11467 // Definition of N::foo.
11468 }
11469 }
11470
11471 then we'll have a tree of DIEs like this:
11472
11473 1: DW_TAG_compile_unit
11474 2: DW_TAG_namespace // N
11475 3: DW_TAG_subprogram // declaration of N::foo
11476 4: DW_TAG_subprogram // definition of N::foo
11477 DW_AT_specification // refers to die #3
11478
11479 Thus, when processing die #4, we have to pretend that we're in
11480 the context of its DW_AT_specification, namely the contex of die
11481 #3. */
11482 spec_cu = cu;
11483 spec_die = die_specification (die, &spec_cu);
11484 if (spec_die == NULL)
11485 parent = die->parent;
11486 else
63d06c5c 11487 {
0114d602
DJ
11488 parent = spec_die->parent;
11489 cu = spec_cu;
63d06c5c 11490 }
0114d602
DJ
11491
11492 if (parent == NULL)
11493 return "";
98bfdba5
PA
11494 else if (parent->building_fullname)
11495 {
11496 const char *name;
11497 const char *parent_name;
11498
11499 /* It has been seen on RealView 2.2 built binaries,
11500 DW_TAG_template_type_param types actually _defined_ as
11501 children of the parent class:
11502
11503 enum E {};
11504 template class <class Enum> Class{};
11505 Class<enum E> class_e;
11506
11507 1: DW_TAG_class_type (Class)
11508 2: DW_TAG_enumeration_type (E)
11509 3: DW_TAG_enumerator (enum1:0)
11510 3: DW_TAG_enumerator (enum2:1)
11511 ...
11512 2: DW_TAG_template_type_param
11513 DW_AT_type DW_FORM_ref_udata (E)
11514
11515 Besides being broken debug info, it can put GDB into an
11516 infinite loop. Consider:
11517
11518 When we're building the full name for Class<E>, we'll start
11519 at Class, and go look over its template type parameters,
11520 finding E. We'll then try to build the full name of E, and
11521 reach here. We're now trying to build the full name of E,
11522 and look over the parent DIE for containing scope. In the
11523 broken case, if we followed the parent DIE of E, we'd again
11524 find Class, and once again go look at its template type
11525 arguments, etc., etc. Simply don't consider such parent die
11526 as source-level parent of this die (it can't be, the language
11527 doesn't allow it), and break the loop here. */
11528 name = dwarf2_name (die, cu);
11529 parent_name = dwarf2_name (parent, cu);
11530 complaint (&symfile_complaints,
11531 _("template param type '%s' defined within parent '%s'"),
11532 name ? name : "<unknown>",
11533 parent_name ? parent_name : "<unknown>");
11534 return "";
11535 }
63d06c5c 11536 else
0114d602
DJ
11537 switch (parent->tag)
11538 {
63d06c5c 11539 case DW_TAG_namespace:
0114d602 11540 parent_type = read_type_die (parent, cu);
acebe513
UW
11541 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11542 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11543 Work around this problem here. */
11544 if (cu->language == language_cplus
11545 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11546 return "";
0114d602
DJ
11547 /* We give a name to even anonymous namespaces. */
11548 return TYPE_TAG_NAME (parent_type);
63d06c5c 11549 case DW_TAG_class_type:
680b30c7 11550 case DW_TAG_interface_type:
63d06c5c 11551 case DW_TAG_structure_type:
0114d602 11552 case DW_TAG_union_type:
f55ee35c 11553 case DW_TAG_module:
0114d602
DJ
11554 parent_type = read_type_die (parent, cu);
11555 if (TYPE_TAG_NAME (parent_type) != NULL)
11556 return TYPE_TAG_NAME (parent_type);
11557 else
11558 /* An anonymous structure is only allowed non-static data
11559 members; no typedefs, no member functions, et cetera.
11560 So it does not need a prefix. */
11561 return "";
abc72ce4
DE
11562 case DW_TAG_compile_unit:
11563 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11564 if (cu->language == language_cplus
11565 && dwarf2_per_objfile->types.asection != NULL
11566 && die->child != NULL
11567 && (die->tag == DW_TAG_class_type
11568 || die->tag == DW_TAG_structure_type
11569 || die->tag == DW_TAG_union_type))
11570 {
11571 char *name = guess_full_die_structure_name (die, cu);
11572 if (name != NULL)
11573 return name;
11574 }
11575 return "";
63d06c5c 11576 default:
8176b9b8 11577 return determine_prefix (parent, cu);
63d06c5c 11578 }
63d06c5c
DC
11579}
11580
987504bb
JJ
11581/* Return a newly-allocated string formed by concatenating PREFIX and
11582 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11583 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11584 perform an obconcat, otherwise allocate storage for the result. The CU argument
11585 is used to determine the language and hence, the appropriate separator. */
11586
f55ee35c 11587#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11588
11589static char *
f55ee35c
JK
11590typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11591 int physname, struct dwarf2_cu *cu)
63d06c5c 11592{
f55ee35c 11593 const char *lead = "";
5c315b68 11594 const char *sep;
63d06c5c 11595
987504bb
JJ
11596 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11597 sep = "";
11598 else if (cu->language == language_java)
11599 sep = ".";
f55ee35c
JK
11600 else if (cu->language == language_fortran && physname)
11601 {
11602 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11603 DW_AT_MIPS_linkage_name is preferred and used instead. */
11604
11605 lead = "__";
11606 sep = "_MOD_";
11607 }
987504bb
JJ
11608 else
11609 sep = "::";
63d06c5c 11610
6dd47d34
DE
11611 if (prefix == NULL)
11612 prefix = "";
11613 if (suffix == NULL)
11614 suffix = "";
11615
987504bb
JJ
11616 if (obs == NULL)
11617 {
11618 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11619
f55ee35c
JK
11620 strcpy (retval, lead);
11621 strcat (retval, prefix);
6dd47d34
DE
11622 strcat (retval, sep);
11623 strcat (retval, suffix);
63d06c5c
DC
11624 return retval;
11625 }
987504bb
JJ
11626 else
11627 {
11628 /* We have an obstack. */
f55ee35c 11629 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11630 }
63d06c5c
DC
11631}
11632
c906108c
SS
11633/* Return sibling of die, NULL if no sibling. */
11634
f9aca02d 11635static struct die_info *
fba45db2 11636sibling_die (struct die_info *die)
c906108c 11637{
639d11d3 11638 return die->sibling;
c906108c
SS
11639}
11640
71c25dea
TT
11641/* Get name of a die, return NULL if not found. */
11642
11643static char *
11644dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11645 struct obstack *obstack)
11646{
11647 if (name && cu->language == language_cplus)
11648 {
11649 char *canon_name = cp_canonicalize_string (name);
11650
11651 if (canon_name != NULL)
11652 {
11653 if (strcmp (canon_name, name) != 0)
11654 name = obsavestring (canon_name, strlen (canon_name),
11655 obstack);
11656 xfree (canon_name);
11657 }
11658 }
11659
11660 return name;
c906108c
SS
11661}
11662
9219021c
DC
11663/* Get name of a die, return NULL if not found. */
11664
11665static char *
e142c38c 11666dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11667{
11668 struct attribute *attr;
11669
e142c38c 11670 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11671 if (!attr || !DW_STRING (attr))
11672 return NULL;
11673
11674 switch (die->tag)
11675 {
11676 case DW_TAG_compile_unit:
11677 /* Compilation units have a DW_AT_name that is a filename, not
11678 a source language identifier. */
11679 case DW_TAG_enumeration_type:
11680 case DW_TAG_enumerator:
11681 /* These tags always have simple identifiers already; no need
11682 to canonicalize them. */
11683 return DW_STRING (attr);
907af001 11684
418835cc
KS
11685 case DW_TAG_subprogram:
11686 /* Java constructors will all be named "<init>", so return
11687 the class name when we see this special case. */
11688 if (cu->language == language_java
11689 && DW_STRING (attr) != NULL
11690 && strcmp (DW_STRING (attr), "<init>") == 0)
11691 {
11692 struct dwarf2_cu *spec_cu = cu;
11693 struct die_info *spec_die;
11694
11695 /* GCJ will output '<init>' for Java constructor names.
11696 For this special case, return the name of the parent class. */
11697
11698 /* GCJ may output suprogram DIEs with AT_specification set.
11699 If so, use the name of the specified DIE. */
11700 spec_die = die_specification (die, &spec_cu);
11701 if (spec_die != NULL)
11702 return dwarf2_name (spec_die, spec_cu);
11703
11704 do
11705 {
11706 die = die->parent;
11707 if (die->tag == DW_TAG_class_type)
11708 return dwarf2_name (die, cu);
11709 }
11710 while (die->tag != DW_TAG_compile_unit);
11711 }
907af001
UW
11712 break;
11713
11714 case DW_TAG_class_type:
11715 case DW_TAG_interface_type:
11716 case DW_TAG_structure_type:
11717 case DW_TAG_union_type:
11718 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11719 structures or unions. These were of the form "._%d" in GCC 4.1,
11720 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11721 and GCC 4.4. We work around this problem by ignoring these. */
11722 if (strncmp (DW_STRING (attr), "._", 2) == 0
11723 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11724 return NULL;
11725 break;
11726
71c25dea 11727 default:
907af001
UW
11728 break;
11729 }
11730
11731 if (!DW_STRING_IS_CANONICAL (attr))
11732 {
11733 DW_STRING (attr)
11734 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11735 &cu->objfile->objfile_obstack);
11736 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11737 }
907af001 11738 return DW_STRING (attr);
9219021c
DC
11739}
11740
11741/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11742 is none. *EXT_CU is the CU containing DIE on input, and the CU
11743 containing the return value on output. */
9219021c
DC
11744
11745static struct die_info *
f2f0e013 11746dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11747{
11748 struct attribute *attr;
9219021c 11749
f2f0e013 11750 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11751 if (attr == NULL)
11752 return NULL;
11753
f2f0e013 11754 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11755}
11756
c906108c
SS
11757/* Convert a DIE tag into its string name. */
11758
11759static char *
aa1ee363 11760dwarf_tag_name (unsigned tag)
c906108c
SS
11761{
11762 switch (tag)
11763 {
11764 case DW_TAG_padding:
11765 return "DW_TAG_padding";
11766 case DW_TAG_array_type:
11767 return "DW_TAG_array_type";
11768 case DW_TAG_class_type:
11769 return "DW_TAG_class_type";
11770 case DW_TAG_entry_point:
11771 return "DW_TAG_entry_point";
11772 case DW_TAG_enumeration_type:
11773 return "DW_TAG_enumeration_type";
11774 case DW_TAG_formal_parameter:
11775 return "DW_TAG_formal_parameter";
11776 case DW_TAG_imported_declaration:
11777 return "DW_TAG_imported_declaration";
11778 case DW_TAG_label:
11779 return "DW_TAG_label";
11780 case DW_TAG_lexical_block:
11781 return "DW_TAG_lexical_block";
11782 case DW_TAG_member:
11783 return "DW_TAG_member";
11784 case DW_TAG_pointer_type:
11785 return "DW_TAG_pointer_type";
11786 case DW_TAG_reference_type:
11787 return "DW_TAG_reference_type";
11788 case DW_TAG_compile_unit:
11789 return "DW_TAG_compile_unit";
11790 case DW_TAG_string_type:
11791 return "DW_TAG_string_type";
11792 case DW_TAG_structure_type:
11793 return "DW_TAG_structure_type";
11794 case DW_TAG_subroutine_type:
11795 return "DW_TAG_subroutine_type";
11796 case DW_TAG_typedef:
11797 return "DW_TAG_typedef";
11798 case DW_TAG_union_type:
11799 return "DW_TAG_union_type";
11800 case DW_TAG_unspecified_parameters:
11801 return "DW_TAG_unspecified_parameters";
11802 case DW_TAG_variant:
11803 return "DW_TAG_variant";
11804 case DW_TAG_common_block:
11805 return "DW_TAG_common_block";
11806 case DW_TAG_common_inclusion:
11807 return "DW_TAG_common_inclusion";
11808 case DW_TAG_inheritance:
11809 return "DW_TAG_inheritance";
11810 case DW_TAG_inlined_subroutine:
11811 return "DW_TAG_inlined_subroutine";
11812 case DW_TAG_module:
11813 return "DW_TAG_module";
11814 case DW_TAG_ptr_to_member_type:
11815 return "DW_TAG_ptr_to_member_type";
11816 case DW_TAG_set_type:
11817 return "DW_TAG_set_type";
11818 case DW_TAG_subrange_type:
11819 return "DW_TAG_subrange_type";
11820 case DW_TAG_with_stmt:
11821 return "DW_TAG_with_stmt";
11822 case DW_TAG_access_declaration:
11823 return "DW_TAG_access_declaration";
11824 case DW_TAG_base_type:
11825 return "DW_TAG_base_type";
11826 case DW_TAG_catch_block:
11827 return "DW_TAG_catch_block";
11828 case DW_TAG_const_type:
11829 return "DW_TAG_const_type";
11830 case DW_TAG_constant:
11831 return "DW_TAG_constant";
11832 case DW_TAG_enumerator:
11833 return "DW_TAG_enumerator";
11834 case DW_TAG_file_type:
11835 return "DW_TAG_file_type";
11836 case DW_TAG_friend:
11837 return "DW_TAG_friend";
11838 case DW_TAG_namelist:
11839 return "DW_TAG_namelist";
11840 case DW_TAG_namelist_item:
11841 return "DW_TAG_namelist_item";
11842 case DW_TAG_packed_type:
11843 return "DW_TAG_packed_type";
11844 case DW_TAG_subprogram:
11845 return "DW_TAG_subprogram";
11846 case DW_TAG_template_type_param:
11847 return "DW_TAG_template_type_param";
11848 case DW_TAG_template_value_param:
11849 return "DW_TAG_template_value_param";
11850 case DW_TAG_thrown_type:
11851 return "DW_TAG_thrown_type";
11852 case DW_TAG_try_block:
11853 return "DW_TAG_try_block";
11854 case DW_TAG_variant_part:
11855 return "DW_TAG_variant_part";
11856 case DW_TAG_variable:
11857 return "DW_TAG_variable";
11858 case DW_TAG_volatile_type:
11859 return "DW_TAG_volatile_type";
d9fa45fe
DC
11860 case DW_TAG_dwarf_procedure:
11861 return "DW_TAG_dwarf_procedure";
11862 case DW_TAG_restrict_type:
11863 return "DW_TAG_restrict_type";
11864 case DW_TAG_interface_type:
11865 return "DW_TAG_interface_type";
11866 case DW_TAG_namespace:
11867 return "DW_TAG_namespace";
11868 case DW_TAG_imported_module:
11869 return "DW_TAG_imported_module";
11870 case DW_TAG_unspecified_type:
11871 return "DW_TAG_unspecified_type";
11872 case DW_TAG_partial_unit:
11873 return "DW_TAG_partial_unit";
11874 case DW_TAG_imported_unit:
11875 return "DW_TAG_imported_unit";
b7619582
GF
11876 case DW_TAG_condition:
11877 return "DW_TAG_condition";
11878 case DW_TAG_shared_type:
11879 return "DW_TAG_shared_type";
348e048f
DE
11880 case DW_TAG_type_unit:
11881 return "DW_TAG_type_unit";
c906108c
SS
11882 case DW_TAG_MIPS_loop:
11883 return "DW_TAG_MIPS_loop";
b7619582
GF
11884 case DW_TAG_HP_array_descriptor:
11885 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11886 case DW_TAG_format_label:
11887 return "DW_TAG_format_label";
11888 case DW_TAG_function_template:
11889 return "DW_TAG_function_template";
11890 case DW_TAG_class_template:
11891 return "DW_TAG_class_template";
b7619582
GF
11892 case DW_TAG_GNU_BINCL:
11893 return "DW_TAG_GNU_BINCL";
11894 case DW_TAG_GNU_EINCL:
11895 return "DW_TAG_GNU_EINCL";
11896 case DW_TAG_upc_shared_type:
11897 return "DW_TAG_upc_shared_type";
11898 case DW_TAG_upc_strict_type:
11899 return "DW_TAG_upc_strict_type";
11900 case DW_TAG_upc_relaxed_type:
11901 return "DW_TAG_upc_relaxed_type";
11902 case DW_TAG_PGI_kanji_type:
11903 return "DW_TAG_PGI_kanji_type";
11904 case DW_TAG_PGI_interface_block:
11905 return "DW_TAG_PGI_interface_block";
c906108c
SS
11906 default:
11907 return "DW_TAG_<unknown>";
11908 }
11909}
11910
11911/* Convert a DWARF attribute code into its string name. */
11912
11913static char *
aa1ee363 11914dwarf_attr_name (unsigned attr)
c906108c
SS
11915{
11916 switch (attr)
11917 {
11918 case DW_AT_sibling:
11919 return "DW_AT_sibling";
11920 case DW_AT_location:
11921 return "DW_AT_location";
11922 case DW_AT_name:
11923 return "DW_AT_name";
11924 case DW_AT_ordering:
11925 return "DW_AT_ordering";
11926 case DW_AT_subscr_data:
11927 return "DW_AT_subscr_data";
11928 case DW_AT_byte_size:
11929 return "DW_AT_byte_size";
11930 case DW_AT_bit_offset:
11931 return "DW_AT_bit_offset";
11932 case DW_AT_bit_size:
11933 return "DW_AT_bit_size";
11934 case DW_AT_element_list:
11935 return "DW_AT_element_list";
11936 case DW_AT_stmt_list:
11937 return "DW_AT_stmt_list";
11938 case DW_AT_low_pc:
11939 return "DW_AT_low_pc";
11940 case DW_AT_high_pc:
11941 return "DW_AT_high_pc";
11942 case DW_AT_language:
11943 return "DW_AT_language";
11944 case DW_AT_member:
11945 return "DW_AT_member";
11946 case DW_AT_discr:
11947 return "DW_AT_discr";
11948 case DW_AT_discr_value:
11949 return "DW_AT_discr_value";
11950 case DW_AT_visibility:
11951 return "DW_AT_visibility";
11952 case DW_AT_import:
11953 return "DW_AT_import";
11954 case DW_AT_string_length:
11955 return "DW_AT_string_length";
11956 case DW_AT_common_reference:
11957 return "DW_AT_common_reference";
11958 case DW_AT_comp_dir:
11959 return "DW_AT_comp_dir";
11960 case DW_AT_const_value:
11961 return "DW_AT_const_value";
11962 case DW_AT_containing_type:
11963 return "DW_AT_containing_type";
11964 case DW_AT_default_value:
11965 return "DW_AT_default_value";
11966 case DW_AT_inline:
11967 return "DW_AT_inline";
11968 case DW_AT_is_optional:
11969 return "DW_AT_is_optional";
11970 case DW_AT_lower_bound:
11971 return "DW_AT_lower_bound";
11972 case DW_AT_producer:
11973 return "DW_AT_producer";
11974 case DW_AT_prototyped:
11975 return "DW_AT_prototyped";
11976 case DW_AT_return_addr:
11977 return "DW_AT_return_addr";
11978 case DW_AT_start_scope:
11979 return "DW_AT_start_scope";
09fa0d7c
JK
11980 case DW_AT_bit_stride:
11981 return "DW_AT_bit_stride";
c906108c
SS
11982 case DW_AT_upper_bound:
11983 return "DW_AT_upper_bound";
11984 case DW_AT_abstract_origin:
11985 return "DW_AT_abstract_origin";
11986 case DW_AT_accessibility:
11987 return "DW_AT_accessibility";
11988 case DW_AT_address_class:
11989 return "DW_AT_address_class";
11990 case DW_AT_artificial:
11991 return "DW_AT_artificial";
11992 case DW_AT_base_types:
11993 return "DW_AT_base_types";
11994 case DW_AT_calling_convention:
11995 return "DW_AT_calling_convention";
11996 case DW_AT_count:
11997 return "DW_AT_count";
11998 case DW_AT_data_member_location:
11999 return "DW_AT_data_member_location";
12000 case DW_AT_decl_column:
12001 return "DW_AT_decl_column";
12002 case DW_AT_decl_file:
12003 return "DW_AT_decl_file";
12004 case DW_AT_decl_line:
12005 return "DW_AT_decl_line";
12006 case DW_AT_declaration:
12007 return "DW_AT_declaration";
12008 case DW_AT_discr_list:
12009 return "DW_AT_discr_list";
12010 case DW_AT_encoding:
12011 return "DW_AT_encoding";
12012 case DW_AT_external:
12013 return "DW_AT_external";
12014 case DW_AT_frame_base:
12015 return "DW_AT_frame_base";
12016 case DW_AT_friend:
12017 return "DW_AT_friend";
12018 case DW_AT_identifier_case:
12019 return "DW_AT_identifier_case";
12020 case DW_AT_macro_info:
12021 return "DW_AT_macro_info";
12022 case DW_AT_namelist_items:
12023 return "DW_AT_namelist_items";
12024 case DW_AT_priority:
12025 return "DW_AT_priority";
12026 case DW_AT_segment:
12027 return "DW_AT_segment";
12028 case DW_AT_specification:
12029 return "DW_AT_specification";
12030 case DW_AT_static_link:
12031 return "DW_AT_static_link";
12032 case DW_AT_type:
12033 return "DW_AT_type";
12034 case DW_AT_use_location:
12035 return "DW_AT_use_location";
12036 case DW_AT_variable_parameter:
12037 return "DW_AT_variable_parameter";
12038 case DW_AT_virtuality:
12039 return "DW_AT_virtuality";
12040 case DW_AT_vtable_elem_location:
12041 return "DW_AT_vtable_elem_location";
b7619582 12042 /* DWARF 3 values. */
d9fa45fe
DC
12043 case DW_AT_allocated:
12044 return "DW_AT_allocated";
12045 case DW_AT_associated:
12046 return "DW_AT_associated";
12047 case DW_AT_data_location:
12048 return "DW_AT_data_location";
09fa0d7c
JK
12049 case DW_AT_byte_stride:
12050 return "DW_AT_byte_stride";
d9fa45fe
DC
12051 case DW_AT_entry_pc:
12052 return "DW_AT_entry_pc";
12053 case DW_AT_use_UTF8:
12054 return "DW_AT_use_UTF8";
12055 case DW_AT_extension:
12056 return "DW_AT_extension";
12057 case DW_AT_ranges:
12058 return "DW_AT_ranges";
12059 case DW_AT_trampoline:
12060 return "DW_AT_trampoline";
12061 case DW_AT_call_column:
12062 return "DW_AT_call_column";
12063 case DW_AT_call_file:
12064 return "DW_AT_call_file";
12065 case DW_AT_call_line:
12066 return "DW_AT_call_line";
b7619582
GF
12067 case DW_AT_description:
12068 return "DW_AT_description";
12069 case DW_AT_binary_scale:
12070 return "DW_AT_binary_scale";
12071 case DW_AT_decimal_scale:
12072 return "DW_AT_decimal_scale";
12073 case DW_AT_small:
12074 return "DW_AT_small";
12075 case DW_AT_decimal_sign:
12076 return "DW_AT_decimal_sign";
12077 case DW_AT_digit_count:
12078 return "DW_AT_digit_count";
12079 case DW_AT_picture_string:
12080 return "DW_AT_picture_string";
12081 case DW_AT_mutable:
12082 return "DW_AT_mutable";
12083 case DW_AT_threads_scaled:
12084 return "DW_AT_threads_scaled";
12085 case DW_AT_explicit:
12086 return "DW_AT_explicit";
12087 case DW_AT_object_pointer:
12088 return "DW_AT_object_pointer";
12089 case DW_AT_endianity:
12090 return "DW_AT_endianity";
12091 case DW_AT_elemental:
12092 return "DW_AT_elemental";
12093 case DW_AT_pure:
12094 return "DW_AT_pure";
12095 case DW_AT_recursive:
12096 return "DW_AT_recursive";
348e048f
DE
12097 /* DWARF 4 values. */
12098 case DW_AT_signature:
12099 return "DW_AT_signature";
31ef98ae
TT
12100 case DW_AT_linkage_name:
12101 return "DW_AT_linkage_name";
b7619582 12102 /* SGI/MIPS extensions. */
c764a876 12103#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12104 case DW_AT_MIPS_fde:
12105 return "DW_AT_MIPS_fde";
c764a876 12106#endif
c906108c
SS
12107 case DW_AT_MIPS_loop_begin:
12108 return "DW_AT_MIPS_loop_begin";
12109 case DW_AT_MIPS_tail_loop_begin:
12110 return "DW_AT_MIPS_tail_loop_begin";
12111 case DW_AT_MIPS_epilog_begin:
12112 return "DW_AT_MIPS_epilog_begin";
12113 case DW_AT_MIPS_loop_unroll_factor:
12114 return "DW_AT_MIPS_loop_unroll_factor";
12115 case DW_AT_MIPS_software_pipeline_depth:
12116 return "DW_AT_MIPS_software_pipeline_depth";
12117 case DW_AT_MIPS_linkage_name:
12118 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12119 case DW_AT_MIPS_stride:
12120 return "DW_AT_MIPS_stride";
12121 case DW_AT_MIPS_abstract_name:
12122 return "DW_AT_MIPS_abstract_name";
12123 case DW_AT_MIPS_clone_origin:
12124 return "DW_AT_MIPS_clone_origin";
12125 case DW_AT_MIPS_has_inlines:
12126 return "DW_AT_MIPS_has_inlines";
b7619582 12127 /* HP extensions. */
c764a876 12128#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12129 case DW_AT_HP_block_index:
12130 return "DW_AT_HP_block_index";
c764a876 12131#endif
b7619582
GF
12132 case DW_AT_HP_unmodifiable:
12133 return "DW_AT_HP_unmodifiable";
12134 case DW_AT_HP_actuals_stmt_list:
12135 return "DW_AT_HP_actuals_stmt_list";
12136 case DW_AT_HP_proc_per_section:
12137 return "DW_AT_HP_proc_per_section";
12138 case DW_AT_HP_raw_data_ptr:
12139 return "DW_AT_HP_raw_data_ptr";
12140 case DW_AT_HP_pass_by_reference:
12141 return "DW_AT_HP_pass_by_reference";
12142 case DW_AT_HP_opt_level:
12143 return "DW_AT_HP_opt_level";
12144 case DW_AT_HP_prof_version_id:
12145 return "DW_AT_HP_prof_version_id";
12146 case DW_AT_HP_opt_flags:
12147 return "DW_AT_HP_opt_flags";
12148 case DW_AT_HP_cold_region_low_pc:
12149 return "DW_AT_HP_cold_region_low_pc";
12150 case DW_AT_HP_cold_region_high_pc:
12151 return "DW_AT_HP_cold_region_high_pc";
12152 case DW_AT_HP_all_variables_modifiable:
12153 return "DW_AT_HP_all_variables_modifiable";
12154 case DW_AT_HP_linkage_name:
12155 return "DW_AT_HP_linkage_name";
12156 case DW_AT_HP_prof_flags:
12157 return "DW_AT_HP_prof_flags";
12158 /* GNU extensions. */
c906108c
SS
12159 case DW_AT_sf_names:
12160 return "DW_AT_sf_names";
12161 case DW_AT_src_info:
12162 return "DW_AT_src_info";
12163 case DW_AT_mac_info:
12164 return "DW_AT_mac_info";
12165 case DW_AT_src_coords:
12166 return "DW_AT_src_coords";
12167 case DW_AT_body_begin:
12168 return "DW_AT_body_begin";
12169 case DW_AT_body_end:
12170 return "DW_AT_body_end";
f5f8a009
EZ
12171 case DW_AT_GNU_vector:
12172 return "DW_AT_GNU_vector";
2de00c64
DE
12173 case DW_AT_GNU_odr_signature:
12174 return "DW_AT_GNU_odr_signature";
b7619582
GF
12175 /* VMS extensions. */
12176 case DW_AT_VMS_rtnbeg_pd_address:
12177 return "DW_AT_VMS_rtnbeg_pd_address";
12178 /* UPC extension. */
12179 case DW_AT_upc_threads_scaled:
12180 return "DW_AT_upc_threads_scaled";
12181 /* PGI (STMicroelectronics) extensions. */
12182 case DW_AT_PGI_lbase:
12183 return "DW_AT_PGI_lbase";
12184 case DW_AT_PGI_soffset:
12185 return "DW_AT_PGI_soffset";
12186 case DW_AT_PGI_lstride:
12187 return "DW_AT_PGI_lstride";
c906108c
SS
12188 default:
12189 return "DW_AT_<unknown>";
12190 }
12191}
12192
12193/* Convert a DWARF value form code into its string name. */
12194
12195static char *
aa1ee363 12196dwarf_form_name (unsigned form)
c906108c
SS
12197{
12198 switch (form)
12199 {
12200 case DW_FORM_addr:
12201 return "DW_FORM_addr";
12202 case DW_FORM_block2:
12203 return "DW_FORM_block2";
12204 case DW_FORM_block4:
12205 return "DW_FORM_block4";
12206 case DW_FORM_data2:
12207 return "DW_FORM_data2";
12208 case DW_FORM_data4:
12209 return "DW_FORM_data4";
12210 case DW_FORM_data8:
12211 return "DW_FORM_data8";
12212 case DW_FORM_string:
12213 return "DW_FORM_string";
12214 case DW_FORM_block:
12215 return "DW_FORM_block";
12216 case DW_FORM_block1:
12217 return "DW_FORM_block1";
12218 case DW_FORM_data1:
12219 return "DW_FORM_data1";
12220 case DW_FORM_flag:
12221 return "DW_FORM_flag";
12222 case DW_FORM_sdata:
12223 return "DW_FORM_sdata";
12224 case DW_FORM_strp:
12225 return "DW_FORM_strp";
12226 case DW_FORM_udata:
12227 return "DW_FORM_udata";
12228 case DW_FORM_ref_addr:
12229 return "DW_FORM_ref_addr";
12230 case DW_FORM_ref1:
12231 return "DW_FORM_ref1";
12232 case DW_FORM_ref2:
12233 return "DW_FORM_ref2";
12234 case DW_FORM_ref4:
12235 return "DW_FORM_ref4";
12236 case DW_FORM_ref8:
12237 return "DW_FORM_ref8";
12238 case DW_FORM_ref_udata:
12239 return "DW_FORM_ref_udata";
12240 case DW_FORM_indirect:
12241 return "DW_FORM_indirect";
348e048f
DE
12242 case DW_FORM_sec_offset:
12243 return "DW_FORM_sec_offset";
12244 case DW_FORM_exprloc:
12245 return "DW_FORM_exprloc";
12246 case DW_FORM_flag_present:
12247 return "DW_FORM_flag_present";
12248 case DW_FORM_sig8:
12249 return "DW_FORM_sig8";
c906108c
SS
12250 default:
12251 return "DW_FORM_<unknown>";
12252 }
12253}
12254
12255/* Convert a DWARF stack opcode into its string name. */
12256
9eae7c52
TT
12257const char *
12258dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12259{
12260 switch (op)
12261 {
12262 case DW_OP_addr:
12263 return "DW_OP_addr";
12264 case DW_OP_deref:
12265 return "DW_OP_deref";
12266 case DW_OP_const1u:
12267 return "DW_OP_const1u";
12268 case DW_OP_const1s:
12269 return "DW_OP_const1s";
12270 case DW_OP_const2u:
12271 return "DW_OP_const2u";
12272 case DW_OP_const2s:
12273 return "DW_OP_const2s";
12274 case DW_OP_const4u:
12275 return "DW_OP_const4u";
12276 case DW_OP_const4s:
12277 return "DW_OP_const4s";
12278 case DW_OP_const8u:
12279 return "DW_OP_const8u";
12280 case DW_OP_const8s:
12281 return "DW_OP_const8s";
12282 case DW_OP_constu:
12283 return "DW_OP_constu";
12284 case DW_OP_consts:
12285 return "DW_OP_consts";
12286 case DW_OP_dup:
12287 return "DW_OP_dup";
12288 case DW_OP_drop:
12289 return "DW_OP_drop";
12290 case DW_OP_over:
12291 return "DW_OP_over";
12292 case DW_OP_pick:
12293 return "DW_OP_pick";
12294 case DW_OP_swap:
12295 return "DW_OP_swap";
12296 case DW_OP_rot:
12297 return "DW_OP_rot";
12298 case DW_OP_xderef:
12299 return "DW_OP_xderef";
12300 case DW_OP_abs:
12301 return "DW_OP_abs";
12302 case DW_OP_and:
12303 return "DW_OP_and";
12304 case DW_OP_div:
12305 return "DW_OP_div";
12306 case DW_OP_minus:
12307 return "DW_OP_minus";
12308 case DW_OP_mod:
12309 return "DW_OP_mod";
12310 case DW_OP_mul:
12311 return "DW_OP_mul";
12312 case DW_OP_neg:
12313 return "DW_OP_neg";
12314 case DW_OP_not:
12315 return "DW_OP_not";
12316 case DW_OP_or:
12317 return "DW_OP_or";
12318 case DW_OP_plus:
12319 return "DW_OP_plus";
12320 case DW_OP_plus_uconst:
12321 return "DW_OP_plus_uconst";
12322 case DW_OP_shl:
12323 return "DW_OP_shl";
12324 case DW_OP_shr:
12325 return "DW_OP_shr";
12326 case DW_OP_shra:
12327 return "DW_OP_shra";
12328 case DW_OP_xor:
12329 return "DW_OP_xor";
12330 case DW_OP_bra:
12331 return "DW_OP_bra";
12332 case DW_OP_eq:
12333 return "DW_OP_eq";
12334 case DW_OP_ge:
12335 return "DW_OP_ge";
12336 case DW_OP_gt:
12337 return "DW_OP_gt";
12338 case DW_OP_le:
12339 return "DW_OP_le";
12340 case DW_OP_lt:
12341 return "DW_OP_lt";
12342 case DW_OP_ne:
12343 return "DW_OP_ne";
12344 case DW_OP_skip:
12345 return "DW_OP_skip";
12346 case DW_OP_lit0:
12347 return "DW_OP_lit0";
12348 case DW_OP_lit1:
12349 return "DW_OP_lit1";
12350 case DW_OP_lit2:
12351 return "DW_OP_lit2";
12352 case DW_OP_lit3:
12353 return "DW_OP_lit3";
12354 case DW_OP_lit4:
12355 return "DW_OP_lit4";
12356 case DW_OP_lit5:
12357 return "DW_OP_lit5";
12358 case DW_OP_lit6:
12359 return "DW_OP_lit6";
12360 case DW_OP_lit7:
12361 return "DW_OP_lit7";
12362 case DW_OP_lit8:
12363 return "DW_OP_lit8";
12364 case DW_OP_lit9:
12365 return "DW_OP_lit9";
12366 case DW_OP_lit10:
12367 return "DW_OP_lit10";
12368 case DW_OP_lit11:
12369 return "DW_OP_lit11";
12370 case DW_OP_lit12:
12371 return "DW_OP_lit12";
12372 case DW_OP_lit13:
12373 return "DW_OP_lit13";
12374 case DW_OP_lit14:
12375 return "DW_OP_lit14";
12376 case DW_OP_lit15:
12377 return "DW_OP_lit15";
12378 case DW_OP_lit16:
12379 return "DW_OP_lit16";
12380 case DW_OP_lit17:
12381 return "DW_OP_lit17";
12382 case DW_OP_lit18:
12383 return "DW_OP_lit18";
12384 case DW_OP_lit19:
12385 return "DW_OP_lit19";
12386 case DW_OP_lit20:
12387 return "DW_OP_lit20";
12388 case DW_OP_lit21:
12389 return "DW_OP_lit21";
12390 case DW_OP_lit22:
12391 return "DW_OP_lit22";
12392 case DW_OP_lit23:
12393 return "DW_OP_lit23";
12394 case DW_OP_lit24:
12395 return "DW_OP_lit24";
12396 case DW_OP_lit25:
12397 return "DW_OP_lit25";
12398 case DW_OP_lit26:
12399 return "DW_OP_lit26";
12400 case DW_OP_lit27:
12401 return "DW_OP_lit27";
12402 case DW_OP_lit28:
12403 return "DW_OP_lit28";
12404 case DW_OP_lit29:
12405 return "DW_OP_lit29";
12406 case DW_OP_lit30:
12407 return "DW_OP_lit30";
12408 case DW_OP_lit31:
12409 return "DW_OP_lit31";
12410 case DW_OP_reg0:
12411 return "DW_OP_reg0";
12412 case DW_OP_reg1:
12413 return "DW_OP_reg1";
12414 case DW_OP_reg2:
12415 return "DW_OP_reg2";
12416 case DW_OP_reg3:
12417 return "DW_OP_reg3";
12418 case DW_OP_reg4:
12419 return "DW_OP_reg4";
12420 case DW_OP_reg5:
12421 return "DW_OP_reg5";
12422 case DW_OP_reg6:
12423 return "DW_OP_reg6";
12424 case DW_OP_reg7:
12425 return "DW_OP_reg7";
12426 case DW_OP_reg8:
12427 return "DW_OP_reg8";
12428 case DW_OP_reg9:
12429 return "DW_OP_reg9";
12430 case DW_OP_reg10:
12431 return "DW_OP_reg10";
12432 case DW_OP_reg11:
12433 return "DW_OP_reg11";
12434 case DW_OP_reg12:
12435 return "DW_OP_reg12";
12436 case DW_OP_reg13:
12437 return "DW_OP_reg13";
12438 case DW_OP_reg14:
12439 return "DW_OP_reg14";
12440 case DW_OP_reg15:
12441 return "DW_OP_reg15";
12442 case DW_OP_reg16:
12443 return "DW_OP_reg16";
12444 case DW_OP_reg17:
12445 return "DW_OP_reg17";
12446 case DW_OP_reg18:
12447 return "DW_OP_reg18";
12448 case DW_OP_reg19:
12449 return "DW_OP_reg19";
12450 case DW_OP_reg20:
12451 return "DW_OP_reg20";
12452 case DW_OP_reg21:
12453 return "DW_OP_reg21";
12454 case DW_OP_reg22:
12455 return "DW_OP_reg22";
12456 case DW_OP_reg23:
12457 return "DW_OP_reg23";
12458 case DW_OP_reg24:
12459 return "DW_OP_reg24";
12460 case DW_OP_reg25:
12461 return "DW_OP_reg25";
12462 case DW_OP_reg26:
12463 return "DW_OP_reg26";
12464 case DW_OP_reg27:
12465 return "DW_OP_reg27";
12466 case DW_OP_reg28:
12467 return "DW_OP_reg28";
12468 case DW_OP_reg29:
12469 return "DW_OP_reg29";
12470 case DW_OP_reg30:
12471 return "DW_OP_reg30";
12472 case DW_OP_reg31:
12473 return "DW_OP_reg31";
12474 case DW_OP_breg0:
12475 return "DW_OP_breg0";
12476 case DW_OP_breg1:
12477 return "DW_OP_breg1";
12478 case DW_OP_breg2:
12479 return "DW_OP_breg2";
12480 case DW_OP_breg3:
12481 return "DW_OP_breg3";
12482 case DW_OP_breg4:
12483 return "DW_OP_breg4";
12484 case DW_OP_breg5:
12485 return "DW_OP_breg5";
12486 case DW_OP_breg6:
12487 return "DW_OP_breg6";
12488 case DW_OP_breg7:
12489 return "DW_OP_breg7";
12490 case DW_OP_breg8:
12491 return "DW_OP_breg8";
12492 case DW_OP_breg9:
12493 return "DW_OP_breg9";
12494 case DW_OP_breg10:
12495 return "DW_OP_breg10";
12496 case DW_OP_breg11:
12497 return "DW_OP_breg11";
12498 case DW_OP_breg12:
12499 return "DW_OP_breg12";
12500 case DW_OP_breg13:
12501 return "DW_OP_breg13";
12502 case DW_OP_breg14:
12503 return "DW_OP_breg14";
12504 case DW_OP_breg15:
12505 return "DW_OP_breg15";
12506 case DW_OP_breg16:
12507 return "DW_OP_breg16";
12508 case DW_OP_breg17:
12509 return "DW_OP_breg17";
12510 case DW_OP_breg18:
12511 return "DW_OP_breg18";
12512 case DW_OP_breg19:
12513 return "DW_OP_breg19";
12514 case DW_OP_breg20:
12515 return "DW_OP_breg20";
12516 case DW_OP_breg21:
12517 return "DW_OP_breg21";
12518 case DW_OP_breg22:
12519 return "DW_OP_breg22";
12520 case DW_OP_breg23:
12521 return "DW_OP_breg23";
12522 case DW_OP_breg24:
12523 return "DW_OP_breg24";
12524 case DW_OP_breg25:
12525 return "DW_OP_breg25";
12526 case DW_OP_breg26:
12527 return "DW_OP_breg26";
12528 case DW_OP_breg27:
12529 return "DW_OP_breg27";
12530 case DW_OP_breg28:
12531 return "DW_OP_breg28";
12532 case DW_OP_breg29:
12533 return "DW_OP_breg29";
12534 case DW_OP_breg30:
12535 return "DW_OP_breg30";
12536 case DW_OP_breg31:
12537 return "DW_OP_breg31";
12538 case DW_OP_regx:
12539 return "DW_OP_regx";
12540 case DW_OP_fbreg:
12541 return "DW_OP_fbreg";
12542 case DW_OP_bregx:
12543 return "DW_OP_bregx";
12544 case DW_OP_piece:
12545 return "DW_OP_piece";
12546 case DW_OP_deref_size:
12547 return "DW_OP_deref_size";
12548 case DW_OP_xderef_size:
12549 return "DW_OP_xderef_size";
12550 case DW_OP_nop:
12551 return "DW_OP_nop";
b7619582 12552 /* DWARF 3 extensions. */
ed348acc
EZ
12553 case DW_OP_push_object_address:
12554 return "DW_OP_push_object_address";
12555 case DW_OP_call2:
12556 return "DW_OP_call2";
12557 case DW_OP_call4:
12558 return "DW_OP_call4";
12559 case DW_OP_call_ref:
12560 return "DW_OP_call_ref";
b7619582
GF
12561 case DW_OP_form_tls_address:
12562 return "DW_OP_form_tls_address";
12563 case DW_OP_call_frame_cfa:
12564 return "DW_OP_call_frame_cfa";
12565 case DW_OP_bit_piece:
12566 return "DW_OP_bit_piece";
9eae7c52
TT
12567 /* DWARF 4 extensions. */
12568 case DW_OP_implicit_value:
12569 return "DW_OP_implicit_value";
12570 case DW_OP_stack_value:
12571 return "DW_OP_stack_value";
12572 /* GNU extensions. */
ed348acc
EZ
12573 case DW_OP_GNU_push_tls_address:
12574 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12575 case DW_OP_GNU_uninit:
12576 return "DW_OP_GNU_uninit";
c906108c 12577 default:
9eae7c52 12578 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12579 }
12580}
12581
12582static char *
fba45db2 12583dwarf_bool_name (unsigned mybool)
c906108c
SS
12584{
12585 if (mybool)
12586 return "TRUE";
12587 else
12588 return "FALSE";
12589}
12590
12591/* Convert a DWARF type code into its string name. */
12592
12593static char *
aa1ee363 12594dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12595{
12596 switch (enc)
12597 {
b7619582
GF
12598 case DW_ATE_void:
12599 return "DW_ATE_void";
c906108c
SS
12600 case DW_ATE_address:
12601 return "DW_ATE_address";
12602 case DW_ATE_boolean:
12603 return "DW_ATE_boolean";
12604 case DW_ATE_complex_float:
12605 return "DW_ATE_complex_float";
12606 case DW_ATE_float:
12607 return "DW_ATE_float";
12608 case DW_ATE_signed:
12609 return "DW_ATE_signed";
12610 case DW_ATE_signed_char:
12611 return "DW_ATE_signed_char";
12612 case DW_ATE_unsigned:
12613 return "DW_ATE_unsigned";
12614 case DW_ATE_unsigned_char:
12615 return "DW_ATE_unsigned_char";
b7619582 12616 /* DWARF 3. */
d9fa45fe
DC
12617 case DW_ATE_imaginary_float:
12618 return "DW_ATE_imaginary_float";
b7619582
GF
12619 case DW_ATE_packed_decimal:
12620 return "DW_ATE_packed_decimal";
12621 case DW_ATE_numeric_string:
12622 return "DW_ATE_numeric_string";
12623 case DW_ATE_edited:
12624 return "DW_ATE_edited";
12625 case DW_ATE_signed_fixed:
12626 return "DW_ATE_signed_fixed";
12627 case DW_ATE_unsigned_fixed:
12628 return "DW_ATE_unsigned_fixed";
12629 case DW_ATE_decimal_float:
12630 return "DW_ATE_decimal_float";
75079b2b
TT
12631 /* DWARF 4. */
12632 case DW_ATE_UTF:
12633 return "DW_ATE_UTF";
b7619582
GF
12634 /* HP extensions. */
12635 case DW_ATE_HP_float80:
12636 return "DW_ATE_HP_float80";
12637 case DW_ATE_HP_complex_float80:
12638 return "DW_ATE_HP_complex_float80";
12639 case DW_ATE_HP_float128:
12640 return "DW_ATE_HP_float128";
12641 case DW_ATE_HP_complex_float128:
12642 return "DW_ATE_HP_complex_float128";
12643 case DW_ATE_HP_floathpintel:
12644 return "DW_ATE_HP_floathpintel";
12645 case DW_ATE_HP_imaginary_float80:
12646 return "DW_ATE_HP_imaginary_float80";
12647 case DW_ATE_HP_imaginary_float128:
12648 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12649 default:
12650 return "DW_ATE_<unknown>";
12651 }
12652}
12653
12654/* Convert a DWARF call frame info operation to its string name. */
12655
12656#if 0
12657static char *
aa1ee363 12658dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12659{
12660 switch (cfi_opc)
12661 {
12662 case DW_CFA_advance_loc:
12663 return "DW_CFA_advance_loc";
12664 case DW_CFA_offset:
12665 return "DW_CFA_offset";
12666 case DW_CFA_restore:
12667 return "DW_CFA_restore";
12668 case DW_CFA_nop:
12669 return "DW_CFA_nop";
12670 case DW_CFA_set_loc:
12671 return "DW_CFA_set_loc";
12672 case DW_CFA_advance_loc1:
12673 return "DW_CFA_advance_loc1";
12674 case DW_CFA_advance_loc2:
12675 return "DW_CFA_advance_loc2";
12676 case DW_CFA_advance_loc4:
12677 return "DW_CFA_advance_loc4";
12678 case DW_CFA_offset_extended:
12679 return "DW_CFA_offset_extended";
12680 case DW_CFA_restore_extended:
12681 return "DW_CFA_restore_extended";
12682 case DW_CFA_undefined:
12683 return "DW_CFA_undefined";
12684 case DW_CFA_same_value:
12685 return "DW_CFA_same_value";
12686 case DW_CFA_register:
12687 return "DW_CFA_register";
12688 case DW_CFA_remember_state:
12689 return "DW_CFA_remember_state";
12690 case DW_CFA_restore_state:
12691 return "DW_CFA_restore_state";
12692 case DW_CFA_def_cfa:
12693 return "DW_CFA_def_cfa";
12694 case DW_CFA_def_cfa_register:
12695 return "DW_CFA_def_cfa_register";
12696 case DW_CFA_def_cfa_offset:
12697 return "DW_CFA_def_cfa_offset";
b7619582 12698 /* DWARF 3. */
985cb1a3
JM
12699 case DW_CFA_def_cfa_expression:
12700 return "DW_CFA_def_cfa_expression";
12701 case DW_CFA_expression:
12702 return "DW_CFA_expression";
12703 case DW_CFA_offset_extended_sf:
12704 return "DW_CFA_offset_extended_sf";
12705 case DW_CFA_def_cfa_sf:
12706 return "DW_CFA_def_cfa_sf";
12707 case DW_CFA_def_cfa_offset_sf:
12708 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12709 case DW_CFA_val_offset:
12710 return "DW_CFA_val_offset";
12711 case DW_CFA_val_offset_sf:
12712 return "DW_CFA_val_offset_sf";
12713 case DW_CFA_val_expression:
12714 return "DW_CFA_val_expression";
12715 /* SGI/MIPS specific. */
c906108c
SS
12716 case DW_CFA_MIPS_advance_loc8:
12717 return "DW_CFA_MIPS_advance_loc8";
b7619582 12718 /* GNU extensions. */
985cb1a3
JM
12719 case DW_CFA_GNU_window_save:
12720 return "DW_CFA_GNU_window_save";
12721 case DW_CFA_GNU_args_size:
12722 return "DW_CFA_GNU_args_size";
12723 case DW_CFA_GNU_negative_offset_extended:
12724 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12725 default:
12726 return "DW_CFA_<unknown>";
12727 }
12728}
12729#endif
12730
f9aca02d 12731static void
d97bc12b 12732dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12733{
12734 unsigned int i;
12735
d97bc12b
DE
12736 print_spaces (indent, f);
12737 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12738 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12739
12740 if (die->parent != NULL)
12741 {
12742 print_spaces (indent, f);
12743 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12744 die->parent->offset);
12745 }
12746
12747 print_spaces (indent, f);
12748 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12749 dwarf_bool_name (die->child != NULL));
c906108c 12750
d97bc12b
DE
12751 print_spaces (indent, f);
12752 fprintf_unfiltered (f, " attributes:\n");
12753
c906108c
SS
12754 for (i = 0; i < die->num_attrs; ++i)
12755 {
d97bc12b
DE
12756 print_spaces (indent, f);
12757 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12758 dwarf_attr_name (die->attrs[i].name),
12759 dwarf_form_name (die->attrs[i].form));
d97bc12b 12760
c906108c
SS
12761 switch (die->attrs[i].form)
12762 {
12763 case DW_FORM_ref_addr:
12764 case DW_FORM_addr:
d97bc12b 12765 fprintf_unfiltered (f, "address: ");
5af949e3 12766 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12767 break;
12768 case DW_FORM_block2:
12769 case DW_FORM_block4:
12770 case DW_FORM_block:
12771 case DW_FORM_block1:
d97bc12b 12772 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12773 break;
2dc7f7b3
TT
12774 case DW_FORM_exprloc:
12775 fprintf_unfiltered (f, "expression: size %u",
12776 DW_BLOCK (&die->attrs[i])->size);
12777 break;
10b3939b
DJ
12778 case DW_FORM_ref1:
12779 case DW_FORM_ref2:
12780 case DW_FORM_ref4:
d97bc12b 12781 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12782 (long) (DW_ADDR (&die->attrs[i])));
12783 break;
c906108c
SS
12784 case DW_FORM_data1:
12785 case DW_FORM_data2:
12786 case DW_FORM_data4:
ce5d95e1 12787 case DW_FORM_data8:
c906108c
SS
12788 case DW_FORM_udata:
12789 case DW_FORM_sdata:
43bbcdc2
PH
12790 fprintf_unfiltered (f, "constant: %s",
12791 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12792 break;
2dc7f7b3
TT
12793 case DW_FORM_sec_offset:
12794 fprintf_unfiltered (f, "section offset: %s",
12795 pulongest (DW_UNSND (&die->attrs[i])));
12796 break;
348e048f
DE
12797 case DW_FORM_sig8:
12798 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12799 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12800 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12801 else
12802 fprintf_unfiltered (f, "signatured type, offset: unknown");
12803 break;
c906108c 12804 case DW_FORM_string:
4bdf3d34 12805 case DW_FORM_strp:
8285870a 12806 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12807 DW_STRING (&die->attrs[i])
8285870a
JK
12808 ? DW_STRING (&die->attrs[i]) : "",
12809 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12810 break;
12811 case DW_FORM_flag:
12812 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12813 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12814 else
d97bc12b 12815 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12816 break;
2dc7f7b3
TT
12817 case DW_FORM_flag_present:
12818 fprintf_unfiltered (f, "flag: TRUE");
12819 break;
a8329558
KW
12820 case DW_FORM_indirect:
12821 /* the reader will have reduced the indirect form to
12822 the "base form" so this form should not occur */
d97bc12b 12823 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12824 break;
c906108c 12825 default:
d97bc12b 12826 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12827 die->attrs[i].form);
d97bc12b 12828 break;
c906108c 12829 }
d97bc12b 12830 fprintf_unfiltered (f, "\n");
c906108c
SS
12831 }
12832}
12833
f9aca02d 12834static void
d97bc12b 12835dump_die_for_error (struct die_info *die)
c906108c 12836{
d97bc12b
DE
12837 dump_die_shallow (gdb_stderr, 0, die);
12838}
12839
12840static void
12841dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12842{
12843 int indent = level * 4;
12844
12845 gdb_assert (die != NULL);
12846
12847 if (level >= max_level)
12848 return;
12849
12850 dump_die_shallow (f, indent, die);
12851
12852 if (die->child != NULL)
c906108c 12853 {
d97bc12b
DE
12854 print_spaces (indent, f);
12855 fprintf_unfiltered (f, " Children:");
12856 if (level + 1 < max_level)
12857 {
12858 fprintf_unfiltered (f, "\n");
12859 dump_die_1 (f, level + 1, max_level, die->child);
12860 }
12861 else
12862 {
12863 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12864 }
12865 }
12866
12867 if (die->sibling != NULL && level > 0)
12868 {
12869 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12870 }
12871}
12872
d97bc12b
DE
12873/* This is called from the pdie macro in gdbinit.in.
12874 It's not static so gcc will keep a copy callable from gdb. */
12875
12876void
12877dump_die (struct die_info *die, int max_level)
12878{
12879 dump_die_1 (gdb_stdlog, 0, max_level, die);
12880}
12881
f9aca02d 12882static void
51545339 12883store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12884{
51545339 12885 void **slot;
c906108c 12886
51545339
DJ
12887 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12888
12889 *slot = die;
c906108c
SS
12890}
12891
93311388
DE
12892static int
12893is_ref_attr (struct attribute *attr)
c906108c 12894{
c906108c
SS
12895 switch (attr->form)
12896 {
12897 case DW_FORM_ref_addr:
c906108c
SS
12898 case DW_FORM_ref1:
12899 case DW_FORM_ref2:
12900 case DW_FORM_ref4:
613e1657 12901 case DW_FORM_ref8:
c906108c 12902 case DW_FORM_ref_udata:
93311388 12903 return 1;
c906108c 12904 default:
93311388 12905 return 0;
c906108c 12906 }
93311388
DE
12907}
12908
12909static unsigned int
12910dwarf2_get_ref_die_offset (struct attribute *attr)
12911{
12912 if (is_ref_attr (attr))
12913 return DW_ADDR (attr);
12914
12915 complaint (&symfile_complaints,
12916 _("unsupported die ref attribute form: '%s'"),
12917 dwarf_form_name (attr->form));
12918 return 0;
c906108c
SS
12919}
12920
43bbcdc2
PH
12921/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12922 * the value held by the attribute is not constant. */
a02abb62 12923
43bbcdc2 12924static LONGEST
a02abb62
JB
12925dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12926{
12927 if (attr->form == DW_FORM_sdata)
12928 return DW_SND (attr);
12929 else if (attr->form == DW_FORM_udata
12930 || attr->form == DW_FORM_data1
12931 || attr->form == DW_FORM_data2
12932 || attr->form == DW_FORM_data4
12933 || attr->form == DW_FORM_data8)
12934 return DW_UNSND (attr);
12935 else
12936 {
e2e0b3e5 12937 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12938 dwarf_form_name (attr->form));
12939 return default_value;
12940 }
12941}
12942
03dd20cc 12943/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12944 unit and add it to our queue.
12945 The result is non-zero if PER_CU was queued, otherwise the result is zero
12946 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12947
348e048f 12948static int
03dd20cc
DJ
12949maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12950 struct dwarf2_per_cu_data *per_cu)
12951{
98bfdba5
PA
12952 /* We may arrive here during partial symbol reading, if we need full
12953 DIEs to process an unusual case (e.g. template arguments). Do
12954 not queue PER_CU, just tell our caller to load its DIEs. */
12955 if (dwarf2_per_objfile->reading_partial_symbols)
12956 {
12957 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12958 return 1;
12959 return 0;
12960 }
12961
03dd20cc
DJ
12962 /* Mark the dependence relation so that we don't flush PER_CU
12963 too early. */
12964 dwarf2_add_dependence (this_cu, per_cu);
12965
12966 /* If it's already on the queue, we have nothing to do. */
12967 if (per_cu->queued)
348e048f 12968 return 0;
03dd20cc
DJ
12969
12970 /* If the compilation unit is already loaded, just mark it as
12971 used. */
12972 if (per_cu->cu != NULL)
12973 {
12974 per_cu->cu->last_used = 0;
348e048f 12975 return 0;
03dd20cc
DJ
12976 }
12977
12978 /* Add it to the queue. */
12979 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12980
12981 return 1;
12982}
12983
12984/* Follow reference or signature attribute ATTR of SRC_DIE.
12985 On entry *REF_CU is the CU of SRC_DIE.
12986 On exit *REF_CU is the CU of the result. */
12987
12988static struct die_info *
12989follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12990 struct dwarf2_cu **ref_cu)
12991{
12992 struct die_info *die;
12993
12994 if (is_ref_attr (attr))
12995 die = follow_die_ref (src_die, attr, ref_cu);
12996 else if (attr->form == DW_FORM_sig8)
12997 die = follow_die_sig (src_die, attr, ref_cu);
12998 else
12999 {
13000 dump_die_for_error (src_die);
13001 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13002 (*ref_cu)->objfile->name);
13003 }
13004
13005 return die;
03dd20cc
DJ
13006}
13007
5c631832 13008/* Follow reference OFFSET.
673bfd45
DE
13009 On entry *REF_CU is the CU of the source die referencing OFFSET.
13010 On exit *REF_CU is the CU of the result.
13011 Returns NULL if OFFSET is invalid. */
f504f079 13012
f9aca02d 13013static struct die_info *
5c631832 13014follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13015{
10b3939b 13016 struct die_info temp_die;
f2f0e013 13017 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13018
348e048f
DE
13019 gdb_assert (cu->per_cu != NULL);
13020
98bfdba5
PA
13021 target_cu = cu;
13022
348e048f
DE
13023 if (cu->per_cu->from_debug_types)
13024 {
13025 /* .debug_types CUs cannot reference anything outside their CU.
13026 If they need to, they have to reference a signatured type via
13027 DW_FORM_sig8. */
13028 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13029 return NULL;
348e048f
DE
13030 }
13031 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13032 {
13033 struct dwarf2_per_cu_data *per_cu;
9a619af0 13034
45452591 13035 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13036
13037 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13038 if (maybe_queue_comp_unit (cu, per_cu))
13039 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13040
10b3939b
DJ
13041 target_cu = per_cu->cu;
13042 }
98bfdba5
PA
13043 else if (cu->dies == NULL)
13044 {
13045 /* We're loading full DIEs during partial symbol reading. */
13046 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13047 load_full_comp_unit (cu->per_cu, cu->objfile);
13048 }
c906108c 13049
f2f0e013 13050 *ref_cu = target_cu;
51545339 13051 temp_die.offset = offset;
5c631832
JK
13052 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13053}
10b3939b 13054
5c631832
JK
13055/* Follow reference attribute ATTR of SRC_DIE.
13056 On entry *REF_CU is the CU of SRC_DIE.
13057 On exit *REF_CU is the CU of the result. */
13058
13059static struct die_info *
13060follow_die_ref (struct die_info *src_die, struct attribute *attr,
13061 struct dwarf2_cu **ref_cu)
13062{
13063 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13064 struct dwarf2_cu *cu = *ref_cu;
13065 struct die_info *die;
13066
13067 die = follow_die_offset (offset, ref_cu);
13068 if (!die)
13069 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13070 "at 0x%x [in module %s]"),
13071 offset, src_die->offset, cu->objfile->name);
348e048f 13072
5c631832
JK
13073 return die;
13074}
13075
13076/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13077 value is intended for DW_OP_call*. */
13078
13079struct dwarf2_locexpr_baton
13080dwarf2_fetch_die_location_block (unsigned int offset,
13081 struct dwarf2_per_cu_data *per_cu)
13082{
13083 struct dwarf2_cu *cu = per_cu->cu;
13084 struct die_info *die;
13085 struct attribute *attr;
13086 struct dwarf2_locexpr_baton retval;
13087
13088 die = follow_die_offset (offset, &cu);
13089 if (!die)
13090 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13091 offset, per_cu->cu->objfile->name);
13092
13093 attr = dwarf2_attr (die, DW_AT_location, cu);
13094 if (!attr)
13095 {
13096 /* DWARF: "If there is no such attribute, then there is no effect.". */
13097
13098 retval.data = NULL;
13099 retval.size = 0;
13100 }
13101 else
13102 {
13103 if (!attr_form_is_block (attr))
13104 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13105 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13106 offset, per_cu->cu->objfile->name);
13107
13108 retval.data = DW_BLOCK (attr)->data;
13109 retval.size = DW_BLOCK (attr)->size;
13110 }
13111 retval.per_cu = cu->per_cu;
13112 return retval;
348e048f
DE
13113}
13114
13115/* Follow the signature attribute ATTR in SRC_DIE.
13116 On entry *REF_CU is the CU of SRC_DIE.
13117 On exit *REF_CU is the CU of the result. */
13118
13119static struct die_info *
13120follow_die_sig (struct die_info *src_die, struct attribute *attr,
13121 struct dwarf2_cu **ref_cu)
13122{
13123 struct objfile *objfile = (*ref_cu)->objfile;
13124 struct die_info temp_die;
13125 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13126 struct dwarf2_cu *sig_cu;
13127 struct die_info *die;
13128
13129 /* sig_type will be NULL if the signatured type is missing from
13130 the debug info. */
13131 if (sig_type == NULL)
13132 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13133 "at 0x%x [in module %s]"),
13134 src_die->offset, objfile->name);
13135
13136 /* If necessary, add it to the queue and load its DIEs. */
13137
13138 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13139 read_signatured_type (objfile, sig_type);
13140
13141 gdb_assert (sig_type->per_cu.cu != NULL);
13142
13143 sig_cu = sig_type->per_cu.cu;
13144 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13145 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13146 if (die)
13147 {
13148 *ref_cu = sig_cu;
13149 return die;
13150 }
13151
13152 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13153 "at 0x%x [in module %s]"),
13154 sig_type->type_offset, src_die->offset, objfile->name);
13155}
13156
13157/* Given an offset of a signatured type, return its signatured_type. */
13158
13159static struct signatured_type *
13160lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13161{
13162 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13163 unsigned int length, initial_length_size;
13164 unsigned int sig_offset;
13165 struct signatured_type find_entry, *type_sig;
13166
13167 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13168 sig_offset = (initial_length_size
13169 + 2 /*version*/
13170 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13171 + 1 /*address_size*/);
13172 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13173 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13174
13175 /* This is only used to lookup previously recorded types.
13176 If we didn't find it, it's our bug. */
13177 gdb_assert (type_sig != NULL);
13178 gdb_assert (offset == type_sig->offset);
13179
13180 return type_sig;
13181}
13182
13183/* Read in signatured type at OFFSET and build its CU and die(s). */
13184
13185static void
13186read_signatured_type_at_offset (struct objfile *objfile,
13187 unsigned int offset)
13188{
13189 struct signatured_type *type_sig;
13190
be391dca
TT
13191 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13192
348e048f
DE
13193 /* We have the section offset, but we need the signature to do the
13194 hash table lookup. */
13195 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13196
13197 gdb_assert (type_sig->per_cu.cu == NULL);
13198
13199 read_signatured_type (objfile, type_sig);
13200
13201 gdb_assert (type_sig->per_cu.cu != NULL);
13202}
13203
13204/* Read in a signatured type and build its CU and DIEs. */
13205
13206static void
13207read_signatured_type (struct objfile *objfile,
13208 struct signatured_type *type_sig)
13209{
1fd400ff 13210 gdb_byte *types_ptr;
348e048f
DE
13211 struct die_reader_specs reader_specs;
13212 struct dwarf2_cu *cu;
13213 ULONGEST signature;
13214 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13215
1fd400ff
TT
13216 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13217 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13218
348e048f
DE
13219 gdb_assert (type_sig->per_cu.cu == NULL);
13220
9816fde3
JK
13221 cu = xmalloc (sizeof (*cu));
13222 init_one_comp_unit (cu, objfile);
13223
348e048f
DE
13224 type_sig->per_cu.cu = cu;
13225 cu->per_cu = &type_sig->per_cu;
13226
13227 /* If an error occurs while loading, release our storage. */
13228 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13229
13230 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13231 types_ptr, objfile->obfd);
13232 gdb_assert (signature == type_sig->signature);
13233
13234 cu->die_hash
13235 = htab_create_alloc_ex (cu->header.length / 12,
13236 die_hash,
13237 die_eq,
13238 NULL,
13239 &cu->comp_unit_obstack,
13240 hashtab_obstack_allocate,
13241 dummy_obstack_deallocate);
13242
13243 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13244 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13245
13246 init_cu_die_reader (&reader_specs, cu);
13247
13248 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13249 NULL /*parent*/);
13250
13251 /* We try not to read any attributes in this function, because not
13252 all objfiles needed for references have been loaded yet, and symbol
13253 table processing isn't initialized. But we have to set the CU language,
13254 or we won't be able to build types correctly. */
9816fde3 13255 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13256
13257 do_cleanups (back_to);
13258
13259 /* We've successfully allocated this compilation unit. Let our caller
13260 clean it up when finished with it. */
13261 discard_cleanups (free_cu_cleanup);
13262
13263 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13264 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13265}
13266
c906108c
SS
13267/* Decode simple location descriptions.
13268 Given a pointer to a dwarf block that defines a location, compute
13269 the location and return the value.
13270
4cecd739
DJ
13271 NOTE drow/2003-11-18: This function is called in two situations
13272 now: for the address of static or global variables (partial symbols
13273 only) and for offsets into structures which are expected to be
13274 (more or less) constant. The partial symbol case should go away,
13275 and only the constant case should remain. That will let this
13276 function complain more accurately. A few special modes are allowed
13277 without complaint for global variables (for instance, global
13278 register values and thread-local values).
c906108c
SS
13279
13280 A location description containing no operations indicates that the
4cecd739 13281 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13282 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13283 callers will only want a very basic result and this can become a
13284 complaint.
c906108c 13285
d53d4ac5 13286 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13287
13288static CORE_ADDR
e7c27a73 13289decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13290{
e7c27a73 13291 struct objfile *objfile = cu->objfile;
c906108c
SS
13292 int i;
13293 int size = blk->size;
fe1b8b76 13294 gdb_byte *data = blk->data;
c906108c
SS
13295 CORE_ADDR stack[64];
13296 int stacki;
13297 unsigned int bytes_read, unsnd;
fe1b8b76 13298 gdb_byte op;
c906108c
SS
13299
13300 i = 0;
13301 stacki = 0;
13302 stack[stacki] = 0;
d53d4ac5 13303 stack[++stacki] = 0;
c906108c
SS
13304
13305 while (i < size)
13306 {
c906108c
SS
13307 op = data[i++];
13308 switch (op)
13309 {
f1bea926
JM
13310 case DW_OP_lit0:
13311 case DW_OP_lit1:
13312 case DW_OP_lit2:
13313 case DW_OP_lit3:
13314 case DW_OP_lit4:
13315 case DW_OP_lit5:
13316 case DW_OP_lit6:
13317 case DW_OP_lit7:
13318 case DW_OP_lit8:
13319 case DW_OP_lit9:
13320 case DW_OP_lit10:
13321 case DW_OP_lit11:
13322 case DW_OP_lit12:
13323 case DW_OP_lit13:
13324 case DW_OP_lit14:
13325 case DW_OP_lit15:
13326 case DW_OP_lit16:
13327 case DW_OP_lit17:
13328 case DW_OP_lit18:
13329 case DW_OP_lit19:
13330 case DW_OP_lit20:
13331 case DW_OP_lit21:
13332 case DW_OP_lit22:
13333 case DW_OP_lit23:
13334 case DW_OP_lit24:
13335 case DW_OP_lit25:
13336 case DW_OP_lit26:
13337 case DW_OP_lit27:
13338 case DW_OP_lit28:
13339 case DW_OP_lit29:
13340 case DW_OP_lit30:
13341 case DW_OP_lit31:
13342 stack[++stacki] = op - DW_OP_lit0;
13343 break;
13344
c906108c
SS
13345 case DW_OP_reg0:
13346 case DW_OP_reg1:
13347 case DW_OP_reg2:
13348 case DW_OP_reg3:
13349 case DW_OP_reg4:
13350 case DW_OP_reg5:
13351 case DW_OP_reg6:
13352 case DW_OP_reg7:
13353 case DW_OP_reg8:
13354 case DW_OP_reg9:
13355 case DW_OP_reg10:
13356 case DW_OP_reg11:
13357 case DW_OP_reg12:
13358 case DW_OP_reg13:
13359 case DW_OP_reg14:
13360 case DW_OP_reg15:
13361 case DW_OP_reg16:
13362 case DW_OP_reg17:
13363 case DW_OP_reg18:
13364 case DW_OP_reg19:
13365 case DW_OP_reg20:
13366 case DW_OP_reg21:
13367 case DW_OP_reg22:
13368 case DW_OP_reg23:
13369 case DW_OP_reg24:
13370 case DW_OP_reg25:
13371 case DW_OP_reg26:
13372 case DW_OP_reg27:
13373 case DW_OP_reg28:
13374 case DW_OP_reg29:
13375 case DW_OP_reg30:
13376 case DW_OP_reg31:
c906108c 13377 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13378 if (i < size)
13379 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13380 break;
13381
13382 case DW_OP_regx:
c906108c
SS
13383 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13384 i += bytes_read;
c906108c 13385 stack[++stacki] = unsnd;
4cecd739
DJ
13386 if (i < size)
13387 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13388 break;
13389
13390 case DW_OP_addr:
107d2387 13391 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13392 cu, &bytes_read);
107d2387 13393 i += bytes_read;
c906108c
SS
13394 break;
13395
13396 case DW_OP_const1u:
13397 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13398 i += 1;
13399 break;
13400
13401 case DW_OP_const1s:
13402 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13403 i += 1;
13404 break;
13405
13406 case DW_OP_const2u:
13407 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13408 i += 2;
13409 break;
13410
13411 case DW_OP_const2s:
13412 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13413 i += 2;
13414 break;
13415
13416 case DW_OP_const4u:
13417 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13418 i += 4;
13419 break;
13420
13421 case DW_OP_const4s:
13422 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13423 i += 4;
13424 break;
13425
13426 case DW_OP_constu:
13427 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13428 &bytes_read);
c906108c
SS
13429 i += bytes_read;
13430 break;
13431
13432 case DW_OP_consts:
13433 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13434 i += bytes_read;
13435 break;
13436
f1bea926
JM
13437 case DW_OP_dup:
13438 stack[stacki + 1] = stack[stacki];
13439 stacki++;
13440 break;
13441
c906108c
SS
13442 case DW_OP_plus:
13443 stack[stacki - 1] += stack[stacki];
13444 stacki--;
13445 break;
13446
13447 case DW_OP_plus_uconst:
13448 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13449 i += bytes_read;
13450 break;
13451
13452 case DW_OP_minus:
f1bea926 13453 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13454 stacki--;
13455 break;
13456
7a292a7a 13457 case DW_OP_deref:
7a292a7a 13458 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13459 this using GDB's address_class enum. This is valid for partial
13460 global symbols, although the variable's address will be bogus
13461 in the psymtab. */
7a292a7a 13462 if (i < size)
4d3c2250 13463 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13464 break;
13465
9d774e44 13466 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13467 /* The top of the stack has the offset from the beginning
13468 of the thread control block at which the variable is located. */
13469 /* Nothing should follow this operator, so the top of stack would
13470 be returned. */
4cecd739
DJ
13471 /* This is valid for partial global symbols, but the variable's
13472 address will be bogus in the psymtab. */
9d774e44 13473 if (i < size)
4d3c2250 13474 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13475 break;
13476
42be36b3
CT
13477 case DW_OP_GNU_uninit:
13478 break;
13479
c906108c 13480 default:
e2e0b3e5 13481 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13482 dwarf_stack_op_name (op, 1));
c906108c
SS
13483 return (stack[stacki]);
13484 }
d53d4ac5
TT
13485
13486 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13487 outside of the allocated space. Also enforce minimum>0. */
13488 if (stacki >= ARRAY_SIZE (stack) - 1)
13489 {
13490 complaint (&symfile_complaints,
13491 _("location description stack overflow"));
13492 return 0;
13493 }
13494
13495 if (stacki <= 0)
13496 {
13497 complaint (&symfile_complaints,
13498 _("location description stack underflow"));
13499 return 0;
13500 }
c906108c
SS
13501 }
13502 return (stack[stacki]);
13503}
13504
13505/* memory allocation interface */
13506
c906108c 13507static struct dwarf_block *
7b5a2f43 13508dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13509{
13510 struct dwarf_block *blk;
13511
13512 blk = (struct dwarf_block *)
7b5a2f43 13513 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13514 return (blk);
13515}
13516
13517static struct abbrev_info *
f3dd6933 13518dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13519{
13520 struct abbrev_info *abbrev;
13521
f3dd6933
DJ
13522 abbrev = (struct abbrev_info *)
13523 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13524 memset (abbrev, 0, sizeof (struct abbrev_info));
13525 return (abbrev);
13526}
13527
13528static struct die_info *
b60c80d6 13529dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13530{
13531 struct die_info *die;
b60c80d6
DJ
13532 size_t size = sizeof (struct die_info);
13533
13534 if (num_attrs > 1)
13535 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13536
b60c80d6 13537 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13538 memset (die, 0, sizeof (struct die_info));
13539 return (die);
13540}
2e276125
JB
13541
13542\f
13543/* Macro support. */
13544
13545
13546/* Return the full name of file number I in *LH's file name table.
13547 Use COMP_DIR as the name of the current directory of the
13548 compilation. The result is allocated using xmalloc; the caller is
13549 responsible for freeing it. */
13550static char *
13551file_full_name (int file, struct line_header *lh, const char *comp_dir)
13552{
6a83a1e6
EZ
13553 /* Is the file number a valid index into the line header's file name
13554 table? Remember that file numbers start with one, not zero. */
13555 if (1 <= file && file <= lh->num_file_names)
13556 {
13557 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13558
6a83a1e6
EZ
13559 if (IS_ABSOLUTE_PATH (fe->name))
13560 return xstrdup (fe->name);
13561 else
13562 {
13563 const char *dir;
13564 int dir_len;
13565 char *full_name;
13566
13567 if (fe->dir_index)
13568 dir = lh->include_dirs[fe->dir_index - 1];
13569 else
13570 dir = comp_dir;
13571
13572 if (dir)
13573 {
13574 dir_len = strlen (dir);
13575 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13576 strcpy (full_name, dir);
13577 full_name[dir_len] = '/';
13578 strcpy (full_name + dir_len + 1, fe->name);
13579 return full_name;
13580 }
13581 else
13582 return xstrdup (fe->name);
13583 }
13584 }
2e276125
JB
13585 else
13586 {
6a83a1e6
EZ
13587 /* The compiler produced a bogus file number. We can at least
13588 record the macro definitions made in the file, even if we
13589 won't be able to find the file by name. */
13590 char fake_name[80];
9a619af0 13591
6a83a1e6 13592 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13593
6e70227d 13594 complaint (&symfile_complaints,
6a83a1e6
EZ
13595 _("bad file number in macro information (%d)"),
13596 file);
2e276125 13597
6a83a1e6 13598 return xstrdup (fake_name);
2e276125
JB
13599 }
13600}
13601
13602
13603static struct macro_source_file *
13604macro_start_file (int file, int line,
13605 struct macro_source_file *current_file,
13606 const char *comp_dir,
13607 struct line_header *lh, struct objfile *objfile)
13608{
13609 /* The full name of this source file. */
13610 char *full_name = file_full_name (file, lh, comp_dir);
13611
13612 /* We don't create a macro table for this compilation unit
13613 at all until we actually get a filename. */
13614 if (! pending_macros)
4a146b47 13615 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13616 objfile->macro_cache);
2e276125
JB
13617
13618 if (! current_file)
13619 /* If we have no current file, then this must be the start_file
13620 directive for the compilation unit's main source file. */
13621 current_file = macro_set_main (pending_macros, full_name);
13622 else
13623 current_file = macro_include (current_file, line, full_name);
13624
13625 xfree (full_name);
6e70227d 13626
2e276125
JB
13627 return current_file;
13628}
13629
13630
13631/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13632 followed by a null byte. */
13633static char *
13634copy_string (const char *buf, int len)
13635{
13636 char *s = xmalloc (len + 1);
9a619af0 13637
2e276125
JB
13638 memcpy (s, buf, len);
13639 s[len] = '\0';
2e276125
JB
13640 return s;
13641}
13642
13643
13644static const char *
13645consume_improper_spaces (const char *p, const char *body)
13646{
13647 if (*p == ' ')
13648 {
4d3c2250 13649 complaint (&symfile_complaints,
e2e0b3e5 13650 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13651 body);
2e276125
JB
13652
13653 while (*p == ' ')
13654 p++;
13655 }
13656
13657 return p;
13658}
13659
13660
13661static void
13662parse_macro_definition (struct macro_source_file *file, int line,
13663 const char *body)
13664{
13665 const char *p;
13666
13667 /* The body string takes one of two forms. For object-like macro
13668 definitions, it should be:
13669
13670 <macro name> " " <definition>
13671
13672 For function-like macro definitions, it should be:
13673
13674 <macro name> "() " <definition>
13675 or
13676 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13677
13678 Spaces may appear only where explicitly indicated, and in the
13679 <definition>.
13680
13681 The Dwarf 2 spec says that an object-like macro's name is always
13682 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13683 the space when the macro's definition is the empty string.
2e276125
JB
13684
13685 The Dwarf 2 spec says that there should be no spaces between the
13686 formal arguments in a function-like macro's formal argument list,
13687 but versions of GCC around March 2002 include spaces after the
13688 commas. */
13689
13690
13691 /* Find the extent of the macro name. The macro name is terminated
13692 by either a space or null character (for an object-like macro) or
13693 an opening paren (for a function-like macro). */
13694 for (p = body; *p; p++)
13695 if (*p == ' ' || *p == '(')
13696 break;
13697
13698 if (*p == ' ' || *p == '\0')
13699 {
13700 /* It's an object-like macro. */
13701 int name_len = p - body;
13702 char *name = copy_string (body, name_len);
13703 const char *replacement;
13704
13705 if (*p == ' ')
13706 replacement = body + name_len + 1;
13707 else
13708 {
4d3c2250 13709 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13710 replacement = body + name_len;
13711 }
6e70227d 13712
2e276125
JB
13713 macro_define_object (file, line, name, replacement);
13714
13715 xfree (name);
13716 }
13717 else if (*p == '(')
13718 {
13719 /* It's a function-like macro. */
13720 char *name = copy_string (body, p - body);
13721 int argc = 0;
13722 int argv_size = 1;
13723 char **argv = xmalloc (argv_size * sizeof (*argv));
13724
13725 p++;
13726
13727 p = consume_improper_spaces (p, body);
13728
13729 /* Parse the formal argument list. */
13730 while (*p && *p != ')')
13731 {
13732 /* Find the extent of the current argument name. */
13733 const char *arg_start = p;
13734
13735 while (*p && *p != ',' && *p != ')' && *p != ' ')
13736 p++;
13737
13738 if (! *p || p == arg_start)
4d3c2250 13739 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13740 else
13741 {
13742 /* Make sure argv has room for the new argument. */
13743 if (argc >= argv_size)
13744 {
13745 argv_size *= 2;
13746 argv = xrealloc (argv, argv_size * sizeof (*argv));
13747 }
13748
13749 argv[argc++] = copy_string (arg_start, p - arg_start);
13750 }
13751
13752 p = consume_improper_spaces (p, body);
13753
13754 /* Consume the comma, if present. */
13755 if (*p == ',')
13756 {
13757 p++;
13758
13759 p = consume_improper_spaces (p, body);
13760 }
13761 }
13762
13763 if (*p == ')')
13764 {
13765 p++;
13766
13767 if (*p == ' ')
13768 /* Perfectly formed definition, no complaints. */
13769 macro_define_function (file, line, name,
6e70227d 13770 argc, (const char **) argv,
2e276125
JB
13771 p + 1);
13772 else if (*p == '\0')
13773 {
13774 /* Complain, but do define it. */
4d3c2250 13775 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13776 macro_define_function (file, line, name,
6e70227d 13777 argc, (const char **) argv,
2e276125
JB
13778 p);
13779 }
13780 else
13781 /* Just complain. */
4d3c2250 13782 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13783 }
13784 else
13785 /* Just complain. */
4d3c2250 13786 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13787
13788 xfree (name);
13789 {
13790 int i;
13791
13792 for (i = 0; i < argc; i++)
13793 xfree (argv[i]);
13794 }
13795 xfree (argv);
13796 }
13797 else
4d3c2250 13798 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13799}
13800
13801
13802static void
13803dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13804 char *comp_dir, bfd *abfd,
e7c27a73 13805 struct dwarf2_cu *cu)
2e276125 13806{
fe1b8b76 13807 gdb_byte *mac_ptr, *mac_end;
2e276125 13808 struct macro_source_file *current_file = 0;
757a13d0
JK
13809 enum dwarf_macinfo_record_type macinfo_type;
13810 int at_commandline;
2e276125 13811
be391dca
TT
13812 dwarf2_read_section (dwarf2_per_objfile->objfile,
13813 &dwarf2_per_objfile->macinfo);
dce234bc 13814 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13815 {
e2e0b3e5 13816 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13817 return;
13818 }
13819
757a13d0
JK
13820 /* First pass: Find the name of the base filename.
13821 This filename is needed in order to process all macros whose definition
13822 (or undefinition) comes from the command line. These macros are defined
13823 before the first DW_MACINFO_start_file entry, and yet still need to be
13824 associated to the base file.
13825
13826 To determine the base file name, we scan the macro definitions until we
13827 reach the first DW_MACINFO_start_file entry. We then initialize
13828 CURRENT_FILE accordingly so that any macro definition found before the
13829 first DW_MACINFO_start_file can still be associated to the base file. */
13830
dce234bc
PP
13831 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13832 mac_end = dwarf2_per_objfile->macinfo.buffer
13833 + dwarf2_per_objfile->macinfo.size;
2e276125 13834
757a13d0 13835 do
2e276125 13836 {
2e276125
JB
13837 /* Do we at least have room for a macinfo type byte? */
13838 if (mac_ptr >= mac_end)
13839 {
757a13d0
JK
13840 /* Complaint is printed during the second pass as GDB will probably
13841 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13842 break;
2e276125
JB
13843 }
13844
13845 macinfo_type = read_1_byte (abfd, mac_ptr);
13846 mac_ptr++;
13847
13848 switch (macinfo_type)
13849 {
13850 /* A zero macinfo type indicates the end of the macro
13851 information. */
13852 case 0:
757a13d0
JK
13853 break;
13854
13855 case DW_MACINFO_define:
13856 case DW_MACINFO_undef:
13857 /* Only skip the data by MAC_PTR. */
13858 {
13859 unsigned int bytes_read;
13860
13861 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13862 mac_ptr += bytes_read;
9b1c24c8 13863 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13864 mac_ptr += bytes_read;
13865 }
13866 break;
13867
13868 case DW_MACINFO_start_file:
13869 {
13870 unsigned int bytes_read;
13871 int line, file;
13872
13873 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13874 mac_ptr += bytes_read;
13875 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13876 mac_ptr += bytes_read;
13877
13878 current_file = macro_start_file (file, line, current_file, comp_dir,
13879 lh, cu->objfile);
13880 }
13881 break;
13882
13883 case DW_MACINFO_end_file:
13884 /* No data to skip by MAC_PTR. */
13885 break;
13886
13887 case DW_MACINFO_vendor_ext:
13888 /* Only skip the data by MAC_PTR. */
13889 {
13890 unsigned int bytes_read;
13891
13892 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13893 mac_ptr += bytes_read;
9b1c24c8 13894 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13895 mac_ptr += bytes_read;
13896 }
13897 break;
13898
13899 default:
13900 break;
13901 }
13902 } while (macinfo_type != 0 && current_file == NULL);
13903
13904 /* Second pass: Process all entries.
13905
13906 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13907 command-line macro definitions/undefinitions. This flag is unset when we
13908 reach the first DW_MACINFO_start_file entry. */
13909
dce234bc 13910 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13911
13912 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13913 GDB is still reading the definitions from command line. First
13914 DW_MACINFO_start_file will need to be ignored as it was already executed
13915 to create CURRENT_FILE for the main source holding also the command line
13916 definitions. On first met DW_MACINFO_start_file this flag is reset to
13917 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13918
13919 at_commandline = 1;
13920
13921 do
13922 {
13923 /* Do we at least have room for a macinfo type byte? */
13924 if (mac_ptr >= mac_end)
13925 {
13926 dwarf2_macros_too_long_complaint ();
13927 break;
13928 }
13929
13930 macinfo_type = read_1_byte (abfd, mac_ptr);
13931 mac_ptr++;
13932
13933 switch (macinfo_type)
13934 {
13935 /* A zero macinfo type indicates the end of the macro
13936 information. */
13937 case 0:
13938 break;
2e276125
JB
13939
13940 case DW_MACINFO_define:
13941 case DW_MACINFO_undef:
13942 {
891d2f0b 13943 unsigned int bytes_read;
2e276125
JB
13944 int line;
13945 char *body;
13946
13947 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13948 mac_ptr += bytes_read;
9b1c24c8 13949 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13950 mac_ptr += bytes_read;
13951
13952 if (! current_file)
757a13d0
JK
13953 {
13954 /* DWARF violation as no main source is present. */
13955 complaint (&symfile_complaints,
13956 _("debug info with no main source gives macro %s "
13957 "on line %d: %s"),
6e70227d
DE
13958 macinfo_type == DW_MACINFO_define ?
13959 _("definition") :
905e0470
PM
13960 macinfo_type == DW_MACINFO_undef ?
13961 _("undefinition") :
13962 _("something-or-other"), line, body);
757a13d0
JK
13963 break;
13964 }
13965 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13966 complaint (&symfile_complaints,
757a13d0
JK
13967 _("debug info gives %s macro %s with %s line %d: %s"),
13968 at_commandline ? _("command-line") : _("in-file"),
905e0470 13969 macinfo_type == DW_MACINFO_define ?
6e70227d 13970 _("definition") :
905e0470
PM
13971 macinfo_type == DW_MACINFO_undef ?
13972 _("undefinition") :
13973 _("something-or-other"),
757a13d0
JK
13974 line == 0 ? _("zero") : _("non-zero"), line, body);
13975
13976 if (macinfo_type == DW_MACINFO_define)
13977 parse_macro_definition (current_file, line, body);
13978 else if (macinfo_type == DW_MACINFO_undef)
13979 macro_undef (current_file, line, body);
2e276125
JB
13980 }
13981 break;
13982
13983 case DW_MACINFO_start_file:
13984 {
891d2f0b 13985 unsigned int bytes_read;
2e276125
JB
13986 int line, file;
13987
13988 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13989 mac_ptr += bytes_read;
13990 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13991 mac_ptr += bytes_read;
13992
757a13d0
JK
13993 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13994 complaint (&symfile_complaints,
13995 _("debug info gives source %d included "
13996 "from %s at %s line %d"),
13997 file, at_commandline ? _("command-line") : _("file"),
13998 line == 0 ? _("zero") : _("non-zero"), line);
13999
14000 if (at_commandline)
14001 {
14002 /* This DW_MACINFO_start_file was executed in the pass one. */
14003 at_commandline = 0;
14004 }
14005 else
14006 current_file = macro_start_file (file, line,
14007 current_file, comp_dir,
14008 lh, cu->objfile);
2e276125
JB
14009 }
14010 break;
14011
14012 case DW_MACINFO_end_file:
14013 if (! current_file)
4d3c2250 14014 complaint (&symfile_complaints,
e2e0b3e5 14015 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14016 else
14017 {
14018 current_file = current_file->included_by;
14019 if (! current_file)
14020 {
14021 enum dwarf_macinfo_record_type next_type;
14022
14023 /* GCC circa March 2002 doesn't produce the zero
14024 type byte marking the end of the compilation
14025 unit. Complain if it's not there, but exit no
14026 matter what. */
14027
14028 /* Do we at least have room for a macinfo type byte? */
14029 if (mac_ptr >= mac_end)
14030 {
4d3c2250 14031 dwarf2_macros_too_long_complaint ();
2e276125
JB
14032 return;
14033 }
14034
14035 /* We don't increment mac_ptr here, so this is just
14036 a look-ahead. */
14037 next_type = read_1_byte (abfd, mac_ptr);
14038 if (next_type != 0)
4d3c2250 14039 complaint (&symfile_complaints,
e2e0b3e5 14040 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14041
14042 return;
14043 }
14044 }
14045 break;
14046
14047 case DW_MACINFO_vendor_ext:
14048 {
891d2f0b 14049 unsigned int bytes_read;
2e276125
JB
14050 int constant;
14051 char *string;
14052
14053 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14054 mac_ptr += bytes_read;
9b1c24c8 14055 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14056 mac_ptr += bytes_read;
14057
14058 /* We don't recognize any vendor extensions. */
14059 }
14060 break;
14061 }
757a13d0 14062 } while (macinfo_type != 0);
2e276125 14063}
8e19ed76
PS
14064
14065/* Check if the attribute's form is a DW_FORM_block*
14066 if so return true else false. */
14067static int
14068attr_form_is_block (struct attribute *attr)
14069{
14070 return (attr == NULL ? 0 :
14071 attr->form == DW_FORM_block1
14072 || attr->form == DW_FORM_block2
14073 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14074 || attr->form == DW_FORM_block
14075 || attr->form == DW_FORM_exprloc);
8e19ed76 14076}
4c2df51b 14077
c6a0999f
JB
14078/* Return non-zero if ATTR's value is a section offset --- classes
14079 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14080 You may use DW_UNSND (attr) to retrieve such offsets.
14081
14082 Section 7.5.4, "Attribute Encodings", explains that no attribute
14083 may have a value that belongs to more than one of these classes; it
14084 would be ambiguous if we did, because we use the same forms for all
14085 of them. */
3690dd37
JB
14086static int
14087attr_form_is_section_offset (struct attribute *attr)
14088{
14089 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14090 || attr->form == DW_FORM_data8
14091 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14092}
14093
14094
14095/* Return non-zero if ATTR's value falls in the 'constant' class, or
14096 zero otherwise. When this function returns true, you can apply
14097 dwarf2_get_attr_constant_value to it.
14098
14099 However, note that for some attributes you must check
14100 attr_form_is_section_offset before using this test. DW_FORM_data4
14101 and DW_FORM_data8 are members of both the constant class, and of
14102 the classes that contain offsets into other debug sections
14103 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14104 that, if an attribute's can be either a constant or one of the
14105 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14106 taken as section offsets, not constants. */
14107static int
14108attr_form_is_constant (struct attribute *attr)
14109{
14110 switch (attr->form)
14111 {
14112 case DW_FORM_sdata:
14113 case DW_FORM_udata:
14114 case DW_FORM_data1:
14115 case DW_FORM_data2:
14116 case DW_FORM_data4:
14117 case DW_FORM_data8:
14118 return 1;
14119 default:
14120 return 0;
14121 }
14122}
14123
4c2df51b
DJ
14124static void
14125dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14126 struct dwarf2_cu *cu)
4c2df51b 14127{
3690dd37 14128 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14129 /* ".debug_loc" may not exist at all, or the offset may be outside
14130 the section. If so, fall through to the complaint in the
14131 other branch. */
dce234bc 14132 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14133 {
0d53c4c4 14134 struct dwarf2_loclist_baton *baton;
4c2df51b 14135
4a146b47 14136 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14137 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
14138 baton->per_cu = cu->per_cu;
14139 gdb_assert (baton->per_cu);
4c2df51b 14140
be391dca
TT
14141 dwarf2_read_section (dwarf2_per_objfile->objfile,
14142 &dwarf2_per_objfile->loc);
14143
0d53c4c4
DJ
14144 /* We don't know how long the location list is, but make sure we
14145 don't run off the edge of the section. */
dce234bc
PP
14146 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14147 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
14148 baton->base_address = cu->base_address;
14149 if (cu->base_known == 0)
0d53c4c4 14150 complaint (&symfile_complaints,
e2e0b3e5 14151 _("Location list used without specifying the CU base address."));
4c2df51b 14152
768a979c 14153 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14154 SYMBOL_LOCATION_BATON (sym) = baton;
14155 }
14156 else
14157 {
14158 struct dwarf2_locexpr_baton *baton;
14159
4a146b47 14160 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14161 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14162 baton->per_cu = cu->per_cu;
14163 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14164
14165 if (attr_form_is_block (attr))
14166 {
14167 /* Note that we're just copying the block's data pointer
14168 here, not the actual data. We're still pointing into the
6502dd73
DJ
14169 info_buffer for SYM's objfile; right now we never release
14170 that buffer, but when we do clean up properly this may
14171 need to change. */
0d53c4c4
DJ
14172 baton->size = DW_BLOCK (attr)->size;
14173 baton->data = DW_BLOCK (attr)->data;
14174 }
14175 else
14176 {
14177 dwarf2_invalid_attrib_class_complaint ("location description",
14178 SYMBOL_NATURAL_NAME (sym));
14179 baton->size = 0;
14180 baton->data = NULL;
14181 }
6e70227d 14182
768a979c 14183 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14184 SYMBOL_LOCATION_BATON (sym) = baton;
14185 }
4c2df51b 14186}
6502dd73 14187
9aa1f1e3
TT
14188/* Return the OBJFILE associated with the compilation unit CU. If CU
14189 came from a separate debuginfo file, then the master objfile is
14190 returned. */
ae0d2f24
UW
14191
14192struct objfile *
14193dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14194{
9291a0cd 14195 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14196
14197 /* Return the master objfile, so that we can report and look up the
14198 correct file containing this variable. */
14199 if (objfile->separate_debug_objfile_backlink)
14200 objfile = objfile->separate_debug_objfile_backlink;
14201
14202 return objfile;
14203}
14204
14205/* Return the address size given in the compilation unit header for CU. */
14206
14207CORE_ADDR
14208dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14209{
14210 if (per_cu->cu)
14211 return per_cu->cu->header.addr_size;
14212 else
14213 {
14214 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14215 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14216 struct dwarf2_per_objfile *per_objfile
14217 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14218 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14219 struct comp_unit_head cu_header;
9a619af0 14220
ae0d2f24
UW
14221 memset (&cu_header, 0, sizeof cu_header);
14222 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14223 return cu_header.addr_size;
14224 }
14225}
14226
9eae7c52
TT
14227/* Return the offset size given in the compilation unit header for CU. */
14228
14229int
14230dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14231{
14232 if (per_cu->cu)
14233 return per_cu->cu->header.offset_size;
14234 else
14235 {
14236 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14237 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14238 struct dwarf2_per_objfile *per_objfile
14239 = objfile_data (objfile, dwarf2_objfile_data_key);
14240 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14241 struct comp_unit_head cu_header;
14242
14243 memset (&cu_header, 0, sizeof cu_header);
14244 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14245 return cu_header.offset_size;
14246 }
14247}
14248
9aa1f1e3
TT
14249/* Return the text offset of the CU. The returned offset comes from
14250 this CU's objfile. If this objfile came from a separate debuginfo
14251 file, then the offset may be different from the corresponding
14252 offset in the parent objfile. */
14253
14254CORE_ADDR
14255dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14256{
bb3fa9d0 14257 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14258
14259 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14260}
14261
348e048f
DE
14262/* Locate the .debug_info compilation unit from CU's objfile which contains
14263 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14264
14265static struct dwarf2_per_cu_data *
c764a876 14266dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14267 struct objfile *objfile)
14268{
14269 struct dwarf2_per_cu_data *this_cu;
14270 int low, high;
14271
ae038cb0
DJ
14272 low = 0;
14273 high = dwarf2_per_objfile->n_comp_units - 1;
14274 while (high > low)
14275 {
14276 int mid = low + (high - low) / 2;
9a619af0 14277
ae038cb0
DJ
14278 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14279 high = mid;
14280 else
14281 low = mid + 1;
14282 }
14283 gdb_assert (low == high);
14284 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14285 {
10b3939b 14286 if (low == 0)
8a3fe4f8
AC
14287 error (_("Dwarf Error: could not find partial DIE containing "
14288 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14289 (long) offset, bfd_get_filename (objfile->obfd));
14290
ae038cb0
DJ
14291 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14292 return dwarf2_per_objfile->all_comp_units[low-1];
14293 }
14294 else
14295 {
14296 this_cu = dwarf2_per_objfile->all_comp_units[low];
14297 if (low == dwarf2_per_objfile->n_comp_units - 1
14298 && offset >= this_cu->offset + this_cu->length)
c764a876 14299 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14300 gdb_assert (offset < this_cu->offset + this_cu->length);
14301 return this_cu;
14302 }
14303}
14304
10b3939b
DJ
14305/* Locate the compilation unit from OBJFILE which is located at exactly
14306 OFFSET. Raises an error on failure. */
14307
ae038cb0 14308static struct dwarf2_per_cu_data *
c764a876 14309dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14310{
14311 struct dwarf2_per_cu_data *this_cu;
9a619af0 14312
ae038cb0
DJ
14313 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14314 if (this_cu->offset != offset)
c764a876 14315 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14316 return this_cu;
14317}
14318
9816fde3 14319/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14320
9816fde3
JK
14321static void
14322init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14323{
9816fde3 14324 memset (cu, 0, sizeof (*cu));
93311388
DE
14325 cu->objfile = objfile;
14326 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14327}
14328
14329/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14330
14331static void
14332prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14333{
14334 struct attribute *attr;
14335
14336 /* Set the language we're debugging. */
14337 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14338 if (attr)
14339 set_cu_language (DW_UNSND (attr), cu);
14340 else
14341 set_cu_language (language_minimal, cu);
93311388
DE
14342}
14343
ae038cb0
DJ
14344/* Release one cached compilation unit, CU. We unlink it from the tree
14345 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14346 the caller is responsible for that.
14347 NOTE: DATA is a void * because this function is also used as a
14348 cleanup routine. */
ae038cb0
DJ
14349
14350static void
14351free_one_comp_unit (void *data)
14352{
14353 struct dwarf2_cu *cu = data;
14354
14355 if (cu->per_cu != NULL)
14356 cu->per_cu->cu = NULL;
14357 cu->per_cu = NULL;
14358
14359 obstack_free (&cu->comp_unit_obstack, NULL);
14360
14361 xfree (cu);
14362}
14363
72bf9492 14364/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14365 when we're finished with it. We can't free the pointer itself, but be
14366 sure to unlink it from the cache. Also release any associated storage
14367 and perform cache maintenance.
72bf9492
DJ
14368
14369 Only used during partial symbol parsing. */
14370
14371static void
14372free_stack_comp_unit (void *data)
14373{
14374 struct dwarf2_cu *cu = data;
14375
14376 obstack_free (&cu->comp_unit_obstack, NULL);
14377 cu->partial_dies = NULL;
ae038cb0
DJ
14378
14379 if (cu->per_cu != NULL)
14380 {
14381 /* This compilation unit is on the stack in our caller, so we
14382 should not xfree it. Just unlink it. */
14383 cu->per_cu->cu = NULL;
14384 cu->per_cu = NULL;
14385
14386 /* If we had a per-cu pointer, then we may have other compilation
14387 units loaded, so age them now. */
14388 age_cached_comp_units ();
14389 }
14390}
14391
14392/* Free all cached compilation units. */
14393
14394static void
14395free_cached_comp_units (void *data)
14396{
14397 struct dwarf2_per_cu_data *per_cu, **last_chain;
14398
14399 per_cu = dwarf2_per_objfile->read_in_chain;
14400 last_chain = &dwarf2_per_objfile->read_in_chain;
14401 while (per_cu != NULL)
14402 {
14403 struct dwarf2_per_cu_data *next_cu;
14404
14405 next_cu = per_cu->cu->read_in_chain;
14406
14407 free_one_comp_unit (per_cu->cu);
14408 *last_chain = next_cu;
14409
14410 per_cu = next_cu;
14411 }
14412}
14413
14414/* Increase the age counter on each cached compilation unit, and free
14415 any that are too old. */
14416
14417static void
14418age_cached_comp_units (void)
14419{
14420 struct dwarf2_per_cu_data *per_cu, **last_chain;
14421
14422 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14423 per_cu = dwarf2_per_objfile->read_in_chain;
14424 while (per_cu != NULL)
14425 {
14426 per_cu->cu->last_used ++;
14427 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14428 dwarf2_mark (per_cu->cu);
14429 per_cu = per_cu->cu->read_in_chain;
14430 }
14431
14432 per_cu = dwarf2_per_objfile->read_in_chain;
14433 last_chain = &dwarf2_per_objfile->read_in_chain;
14434 while (per_cu != NULL)
14435 {
14436 struct dwarf2_per_cu_data *next_cu;
14437
14438 next_cu = per_cu->cu->read_in_chain;
14439
14440 if (!per_cu->cu->mark)
14441 {
14442 free_one_comp_unit (per_cu->cu);
14443 *last_chain = next_cu;
14444 }
14445 else
14446 last_chain = &per_cu->cu->read_in_chain;
14447
14448 per_cu = next_cu;
14449 }
14450}
14451
14452/* Remove a single compilation unit from the cache. */
14453
14454static void
14455free_one_cached_comp_unit (void *target_cu)
14456{
14457 struct dwarf2_per_cu_data *per_cu, **last_chain;
14458
14459 per_cu = dwarf2_per_objfile->read_in_chain;
14460 last_chain = &dwarf2_per_objfile->read_in_chain;
14461 while (per_cu != NULL)
14462 {
14463 struct dwarf2_per_cu_data *next_cu;
14464
14465 next_cu = per_cu->cu->read_in_chain;
14466
14467 if (per_cu->cu == target_cu)
14468 {
14469 free_one_comp_unit (per_cu->cu);
14470 *last_chain = next_cu;
14471 break;
14472 }
14473 else
14474 last_chain = &per_cu->cu->read_in_chain;
14475
14476 per_cu = next_cu;
14477 }
14478}
14479
fe3e1990
DJ
14480/* Release all extra memory associated with OBJFILE. */
14481
14482void
14483dwarf2_free_objfile (struct objfile *objfile)
14484{
14485 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14486
14487 if (dwarf2_per_objfile == NULL)
14488 return;
14489
14490 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14491 free_cached_comp_units (NULL);
14492
9291a0cd
TT
14493 if (dwarf2_per_objfile->using_index)
14494 {
14495 int i;
14496
14497 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14498 {
14499 int j;
e254ef6a
DE
14500 struct dwarf2_per_cu_data *per_cu =
14501 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14502
e254ef6a 14503 if (!per_cu->v.quick->lines)
9291a0cd
TT
14504 continue;
14505
e254ef6a 14506 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14507 {
e254ef6a
DE
14508 if (per_cu->v.quick->file_names)
14509 xfree ((void *) per_cu->v.quick->file_names[j]);
14510 if (per_cu->v.quick->full_names)
14511 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14512 }
14513
e254ef6a 14514 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14515 }
14516 }
14517
fe3e1990
DJ
14518 /* Everything else should be on the objfile obstack. */
14519}
14520
1c379e20
DJ
14521/* A pair of DIE offset and GDB type pointer. We store these
14522 in a hash table separate from the DIEs, and preserve them
14523 when the DIEs are flushed out of cache. */
14524
14525struct dwarf2_offset_and_type
14526{
14527 unsigned int offset;
14528 struct type *type;
14529};
14530
14531/* Hash function for a dwarf2_offset_and_type. */
14532
14533static hashval_t
14534offset_and_type_hash (const void *item)
14535{
14536 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14537
1c379e20
DJ
14538 return ofs->offset;
14539}
14540
14541/* Equality function for a dwarf2_offset_and_type. */
14542
14543static int
14544offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14545{
14546 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14547 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14548
1c379e20
DJ
14549 return ofs_lhs->offset == ofs_rhs->offset;
14550}
14551
14552/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14553 table if necessary. For convenience, return TYPE.
14554
14555 The DIEs reading must have careful ordering to:
14556 * Not cause infite loops trying to read in DIEs as a prerequisite for
14557 reading current DIE.
14558 * Not trying to dereference contents of still incompletely read in types
14559 while reading in other DIEs.
14560 * Enable referencing still incompletely read in types just by a pointer to
14561 the type without accessing its fields.
14562
14563 Therefore caller should follow these rules:
14564 * Try to fetch any prerequisite types we may need to build this DIE type
14565 before building the type and calling set_die_type.
e71ec853 14566 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14567 possible before fetching more types to complete the current type.
14568 * Make the type as complete as possible before fetching more types. */
1c379e20 14569
f792889a 14570static struct type *
1c379e20
DJ
14571set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14572{
14573 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14574 struct objfile *objfile = cu->objfile;
14575 htab_t *type_hash_ptr;
1c379e20 14576
b4ba55a1
JB
14577 /* For Ada types, make sure that the gnat-specific data is always
14578 initialized (if not already set). There are a few types where
14579 we should not be doing so, because the type-specific area is
14580 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14581 where the type-specific area is used to store the floatformat).
14582 But this is not a problem, because the gnat-specific information
14583 is actually not needed for these types. */
14584 if (need_gnat_info (cu)
14585 && TYPE_CODE (type) != TYPE_CODE_FUNC
14586 && TYPE_CODE (type) != TYPE_CODE_FLT
14587 && !HAVE_GNAT_AUX_INFO (type))
14588 INIT_GNAT_SPECIFIC (type);
14589
673bfd45
DE
14590 if (cu->per_cu->from_debug_types)
14591 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14592 else
14593 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14594
14595 if (*type_hash_ptr == NULL)
f792889a 14596 {
673bfd45
DE
14597 *type_hash_ptr
14598 = htab_create_alloc_ex (127,
f792889a
DJ
14599 offset_and_type_hash,
14600 offset_and_type_eq,
14601 NULL,
673bfd45 14602 &objfile->objfile_obstack,
f792889a
DJ
14603 hashtab_obstack_allocate,
14604 dummy_obstack_deallocate);
f792889a 14605 }
1c379e20
DJ
14606
14607 ofs.offset = die->offset;
14608 ofs.type = type;
14609 slot = (struct dwarf2_offset_and_type **)
673bfd45 14610 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14611 if (*slot)
14612 complaint (&symfile_complaints,
14613 _("A problem internal to GDB: DIE 0x%x has type already set"),
14614 die->offset);
673bfd45 14615 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14616 **slot = ofs;
f792889a 14617 return type;
1c379e20
DJ
14618}
14619
673bfd45
DE
14620/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14621 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14622
14623static struct type *
673bfd45
DE
14624get_die_type_at_offset (unsigned int offset,
14625 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14626{
14627 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14628 htab_t type_hash;
f792889a 14629
673bfd45
DE
14630 if (per_cu->from_debug_types)
14631 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14632 else
14633 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14634 if (type_hash == NULL)
14635 return NULL;
1c379e20 14636
673bfd45 14637 ofs.offset = offset;
1c379e20
DJ
14638 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14639 if (slot)
14640 return slot->type;
14641 else
14642 return NULL;
14643}
14644
673bfd45
DE
14645/* Look up the type for DIE in the appropriate type_hash table,
14646 or return NULL if DIE does not have a saved type. */
14647
14648static struct type *
14649get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14650{
14651 return get_die_type_at_offset (die->offset, cu->per_cu);
14652}
14653
10b3939b
DJ
14654/* Add a dependence relationship from CU to REF_PER_CU. */
14655
14656static void
14657dwarf2_add_dependence (struct dwarf2_cu *cu,
14658 struct dwarf2_per_cu_data *ref_per_cu)
14659{
14660 void **slot;
14661
14662 if (cu->dependencies == NULL)
14663 cu->dependencies
14664 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14665 NULL, &cu->comp_unit_obstack,
14666 hashtab_obstack_allocate,
14667 dummy_obstack_deallocate);
14668
14669 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14670 if (*slot == NULL)
14671 *slot = ref_per_cu;
14672}
1c379e20 14673
f504f079
DE
14674/* Subroutine of dwarf2_mark to pass to htab_traverse.
14675 Set the mark field in every compilation unit in the
ae038cb0
DJ
14676 cache that we must keep because we are keeping CU. */
14677
10b3939b
DJ
14678static int
14679dwarf2_mark_helper (void **slot, void *data)
14680{
14681 struct dwarf2_per_cu_data *per_cu;
14682
14683 per_cu = (struct dwarf2_per_cu_data *) *slot;
14684 if (per_cu->cu->mark)
14685 return 1;
14686 per_cu->cu->mark = 1;
14687
14688 if (per_cu->cu->dependencies != NULL)
14689 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14690
14691 return 1;
14692}
14693
f504f079
DE
14694/* Set the mark field in CU and in every other compilation unit in the
14695 cache that we must keep because we are keeping CU. */
14696
ae038cb0
DJ
14697static void
14698dwarf2_mark (struct dwarf2_cu *cu)
14699{
14700 if (cu->mark)
14701 return;
14702 cu->mark = 1;
10b3939b
DJ
14703 if (cu->dependencies != NULL)
14704 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14705}
14706
14707static void
14708dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14709{
14710 while (per_cu)
14711 {
14712 per_cu->cu->mark = 0;
14713 per_cu = per_cu->cu->read_in_chain;
14714 }
72bf9492
DJ
14715}
14716
72bf9492
DJ
14717/* Trivial hash function for partial_die_info: the hash value of a DIE
14718 is its offset in .debug_info for this objfile. */
14719
14720static hashval_t
14721partial_die_hash (const void *item)
14722{
14723 const struct partial_die_info *part_die = item;
9a619af0 14724
72bf9492
DJ
14725 return part_die->offset;
14726}
14727
14728/* Trivial comparison function for partial_die_info structures: two DIEs
14729 are equal if they have the same offset. */
14730
14731static int
14732partial_die_eq (const void *item_lhs, const void *item_rhs)
14733{
14734 const struct partial_die_info *part_die_lhs = item_lhs;
14735 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14736
72bf9492
DJ
14737 return part_die_lhs->offset == part_die_rhs->offset;
14738}
14739
ae038cb0
DJ
14740static struct cmd_list_element *set_dwarf2_cmdlist;
14741static struct cmd_list_element *show_dwarf2_cmdlist;
14742
14743static void
14744set_dwarf2_cmd (char *args, int from_tty)
14745{
14746 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14747}
14748
14749static void
14750show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14751{
ae038cb0
DJ
14752 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14753}
14754
dce234bc
PP
14755/* If section described by INFO was mmapped, munmap it now. */
14756
14757static void
14758munmap_section_buffer (struct dwarf2_section_info *info)
14759{
14760 if (info->was_mmapped)
14761 {
14762#ifdef HAVE_MMAP
14763 intptr_t begin = (intptr_t) info->buffer;
14764 intptr_t map_begin = begin & ~(pagesize - 1);
14765 size_t map_length = info->size + begin - map_begin;
9a619af0 14766
dce234bc
PP
14767 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14768#else
14769 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14770 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14771#endif
14772 }
14773}
14774
14775/* munmap debug sections for OBJFILE, if necessary. */
14776
14777static void
c1bd65d0 14778dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14779{
14780 struct dwarf2_per_objfile *data = d;
9a619af0 14781
16be1145
DE
14782 /* This is sorted according to the order they're defined in to make it easier
14783 to keep in sync. */
dce234bc
PP
14784 munmap_section_buffer (&data->info);
14785 munmap_section_buffer (&data->abbrev);
14786 munmap_section_buffer (&data->line);
16be1145 14787 munmap_section_buffer (&data->loc);
dce234bc 14788 munmap_section_buffer (&data->macinfo);
16be1145 14789 munmap_section_buffer (&data->str);
dce234bc 14790 munmap_section_buffer (&data->ranges);
16be1145 14791 munmap_section_buffer (&data->types);
dce234bc
PP
14792 munmap_section_buffer (&data->frame);
14793 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14794 munmap_section_buffer (&data->gdb_index);
14795}
14796
14797\f
14798
14799/* The contents of the hash table we create when building the string
14800 table. */
14801struct strtab_entry
14802{
14803 offset_type offset;
14804 const char *str;
14805};
14806
14807/* Hash function for a strtab_entry. */
b89be57b 14808
9291a0cd
TT
14809static hashval_t
14810hash_strtab_entry (const void *e)
14811{
14812 const struct strtab_entry *entry = e;
14813 return mapped_index_string_hash (entry->str);
14814}
14815
14816/* Equality function for a strtab_entry. */
b89be57b 14817
9291a0cd
TT
14818static int
14819eq_strtab_entry (const void *a, const void *b)
14820{
14821 const struct strtab_entry *ea = a;
14822 const struct strtab_entry *eb = b;
14823 return !strcmp (ea->str, eb->str);
14824}
14825
14826/* Create a strtab_entry hash table. */
b89be57b 14827
9291a0cd
TT
14828static htab_t
14829create_strtab (void)
14830{
14831 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14832 xfree, xcalloc, xfree);
14833}
14834
14835/* Add a string to the constant pool. Return the string's offset in
14836 host order. */
b89be57b 14837
9291a0cd
TT
14838static offset_type
14839add_string (htab_t table, struct obstack *cpool, const char *str)
14840{
14841 void **slot;
14842 struct strtab_entry entry;
14843 struct strtab_entry *result;
14844
14845 entry.str = str;
14846 slot = htab_find_slot (table, &entry, INSERT);
14847 if (*slot)
14848 result = *slot;
14849 else
14850 {
14851 result = XNEW (struct strtab_entry);
14852 result->offset = obstack_object_size (cpool);
14853 result->str = str;
14854 obstack_grow_str0 (cpool, str);
14855 *slot = result;
14856 }
14857 return result->offset;
14858}
14859
14860/* An entry in the symbol table. */
14861struct symtab_index_entry
14862{
14863 /* The name of the symbol. */
14864 const char *name;
14865 /* The offset of the name in the constant pool. */
14866 offset_type index_offset;
14867 /* A sorted vector of the indices of all the CUs that hold an object
14868 of this name. */
14869 VEC (offset_type) *cu_indices;
14870};
14871
14872/* The symbol table. This is a power-of-2-sized hash table. */
14873struct mapped_symtab
14874{
14875 offset_type n_elements;
14876 offset_type size;
14877 struct symtab_index_entry **data;
14878};
14879
14880/* Hash function for a symtab_index_entry. */
b89be57b 14881
9291a0cd
TT
14882static hashval_t
14883hash_symtab_entry (const void *e)
14884{
14885 const struct symtab_index_entry *entry = e;
14886 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14887 sizeof (offset_type) * VEC_length (offset_type,
14888 entry->cu_indices),
14889 0);
14890}
14891
14892/* Equality function for a symtab_index_entry. */
b89be57b 14893
9291a0cd
TT
14894static int
14895eq_symtab_entry (const void *a, const void *b)
14896{
14897 const struct symtab_index_entry *ea = a;
14898 const struct symtab_index_entry *eb = b;
14899 int len = VEC_length (offset_type, ea->cu_indices);
14900 if (len != VEC_length (offset_type, eb->cu_indices))
14901 return 0;
14902 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14903 VEC_address (offset_type, eb->cu_indices),
14904 sizeof (offset_type) * len);
14905}
14906
14907/* Destroy a symtab_index_entry. */
b89be57b 14908
9291a0cd
TT
14909static void
14910delete_symtab_entry (void *p)
14911{
14912 struct symtab_index_entry *entry = p;
14913 VEC_free (offset_type, entry->cu_indices);
14914 xfree (entry);
14915}
14916
14917/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14918
9291a0cd 14919static htab_t
3876f04e 14920create_symbol_hash_table (void)
9291a0cd
TT
14921{
14922 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14923 delete_symtab_entry, xcalloc, xfree);
14924}
14925
14926/* Create a new mapped symtab object. */
b89be57b 14927
9291a0cd
TT
14928static struct mapped_symtab *
14929create_mapped_symtab (void)
14930{
14931 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14932 symtab->n_elements = 0;
14933 symtab->size = 1024;
14934 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14935 return symtab;
14936}
14937
14938/* Destroy a mapped_symtab. */
b89be57b 14939
9291a0cd
TT
14940static void
14941cleanup_mapped_symtab (void *p)
14942{
14943 struct mapped_symtab *symtab = p;
14944 /* The contents of the array are freed when the other hash table is
14945 destroyed. */
14946 xfree (symtab->data);
14947 xfree (symtab);
14948}
14949
14950/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14951 the slot. */
b89be57b 14952
9291a0cd
TT
14953static struct symtab_index_entry **
14954find_slot (struct mapped_symtab *symtab, const char *name)
14955{
14956 offset_type index, step, hash = mapped_index_string_hash (name);
14957
14958 index = hash & (symtab->size - 1);
14959 step = ((hash * 17) & (symtab->size - 1)) | 1;
14960
14961 for (;;)
14962 {
14963 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14964 return &symtab->data[index];
14965 index = (index + step) & (symtab->size - 1);
14966 }
14967}
14968
14969/* Expand SYMTAB's hash table. */
b89be57b 14970
9291a0cd
TT
14971static void
14972hash_expand (struct mapped_symtab *symtab)
14973{
14974 offset_type old_size = symtab->size;
14975 offset_type i;
14976 struct symtab_index_entry **old_entries = symtab->data;
14977
14978 symtab->size *= 2;
14979 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14980
14981 for (i = 0; i < old_size; ++i)
14982 {
14983 if (old_entries[i])
14984 {
14985 struct symtab_index_entry **slot = find_slot (symtab,
14986 old_entries[i]->name);
14987 *slot = old_entries[i];
14988 }
14989 }
14990
14991 xfree (old_entries);
14992}
14993
14994/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14995 is the index of the CU in which the symbol appears. */
b89be57b 14996
9291a0cd
TT
14997static void
14998add_index_entry (struct mapped_symtab *symtab, const char *name,
14999 offset_type cu_index)
15000{
15001 struct symtab_index_entry **slot;
15002
15003 ++symtab->n_elements;
15004 if (4 * symtab->n_elements / 3 >= symtab->size)
15005 hash_expand (symtab);
15006
15007 slot = find_slot (symtab, name);
15008 if (!*slot)
15009 {
15010 *slot = XNEW (struct symtab_index_entry);
15011 (*slot)->name = name;
15012 (*slot)->cu_indices = NULL;
15013 }
15014 /* Don't push an index twice. Due to how we add entries we only
15015 have to check the last one. */
15016 if (VEC_empty (offset_type, (*slot)->cu_indices)
15017 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15018 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15019}
15020
15021/* Add a vector of indices to the constant pool. */
b89be57b 15022
9291a0cd 15023static offset_type
3876f04e 15024add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15025 struct symtab_index_entry *entry)
15026{
15027 void **slot;
15028
3876f04e 15029 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15030 if (!*slot)
15031 {
15032 offset_type len = VEC_length (offset_type, entry->cu_indices);
15033 offset_type val = MAYBE_SWAP (len);
15034 offset_type iter;
15035 int i;
15036
15037 *slot = entry;
15038 entry->index_offset = obstack_object_size (cpool);
15039
15040 obstack_grow (cpool, &val, sizeof (val));
15041 for (i = 0;
15042 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15043 ++i)
15044 {
15045 val = MAYBE_SWAP (iter);
15046 obstack_grow (cpool, &val, sizeof (val));
15047 }
15048 }
15049 else
15050 {
15051 struct symtab_index_entry *old_entry = *slot;
15052 entry->index_offset = old_entry->index_offset;
15053 entry = old_entry;
15054 }
15055 return entry->index_offset;
15056}
15057
15058/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15059 constant pool entries going into the obstack CPOOL. */
b89be57b 15060
9291a0cd
TT
15061static void
15062write_hash_table (struct mapped_symtab *symtab,
15063 struct obstack *output, struct obstack *cpool)
15064{
15065 offset_type i;
3876f04e 15066 htab_t symbol_hash_table;
9291a0cd
TT
15067 htab_t str_table;
15068
3876f04e 15069 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15070 str_table = create_strtab ();
3876f04e 15071
9291a0cd
TT
15072 /* We add all the index vectors to the constant pool first, to
15073 ensure alignment is ok. */
15074 for (i = 0; i < symtab->size; ++i)
15075 {
15076 if (symtab->data[i])
3876f04e 15077 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15078 }
15079
15080 /* Now write out the hash table. */
15081 for (i = 0; i < symtab->size; ++i)
15082 {
15083 offset_type str_off, vec_off;
15084
15085 if (symtab->data[i])
15086 {
15087 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15088 vec_off = symtab->data[i]->index_offset;
15089 }
15090 else
15091 {
15092 /* While 0 is a valid constant pool index, it is not valid
15093 to have 0 for both offsets. */
15094 str_off = 0;
15095 vec_off = 0;
15096 }
15097
15098 str_off = MAYBE_SWAP (str_off);
15099 vec_off = MAYBE_SWAP (vec_off);
15100
15101 obstack_grow (output, &str_off, sizeof (str_off));
15102 obstack_grow (output, &vec_off, sizeof (vec_off));
15103 }
15104
15105 htab_delete (str_table);
3876f04e 15106 htab_delete (symbol_hash_table);
9291a0cd
TT
15107}
15108
15109/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15110 from PST; CU_INDEX is the index of the CU in the vector of all
15111 CUs. */
b89be57b 15112
9291a0cd
TT
15113static void
15114add_address_entry (struct objfile *objfile,
15115 struct obstack *addr_obstack, struct partial_symtab *pst,
15116 unsigned int cu_index)
15117{
15118 offset_type offset;
15119 char addr[8];
15120 CORE_ADDR baseaddr;
15121
1fd400ff
TT
15122 /* Don't bother recording empty ranges. */
15123 if (pst->textlow == pst->texthigh)
15124 return;
15125
9291a0cd
TT
15126 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15127
15128 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15129 obstack_grow (addr_obstack, addr, 8);
15130 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15131 obstack_grow (addr_obstack, addr, 8);
15132 offset = MAYBE_SWAP (cu_index);
15133 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15134}
15135
15136/* Add a list of partial symbols to SYMTAB. */
b89be57b 15137
9291a0cd
TT
15138static void
15139write_psymbols (struct mapped_symtab *symtab,
987d643c 15140 htab_t psyms_seen,
9291a0cd
TT
15141 struct partial_symbol **psymp,
15142 int count,
987d643c
TT
15143 offset_type cu_index,
15144 int is_static)
9291a0cd
TT
15145{
15146 for (; count-- > 0; ++psymp)
15147 {
987d643c
TT
15148 void **slot, *lookup;
15149
9291a0cd
TT
15150 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15151 error (_("Ada is not currently supported by the index"));
987d643c
TT
15152
15153 /* We only want to add a given psymbol once. However, we also
15154 want to account for whether it is global or static. So, we
15155 may add it twice, using slightly different values. */
15156 if (is_static)
15157 {
15158 uintptr_t val = 1 | (uintptr_t) *psymp;
15159
15160 lookup = (void *) val;
15161 }
15162 else
15163 lookup = *psymp;
15164
15165 /* Only add a given psymbol once. */
15166 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15167 if (!*slot)
15168 {
15169 *slot = lookup;
15170 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15171 }
9291a0cd
TT
15172 }
15173}
15174
15175/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15176 exception if there is an error. */
b89be57b 15177
9291a0cd
TT
15178static void
15179write_obstack (FILE *file, struct obstack *obstack)
15180{
15181 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15182 file)
15183 != obstack_object_size (obstack))
15184 error (_("couldn't data write to file"));
15185}
15186
15187/* Unlink a file if the argument is not NULL. */
b89be57b 15188
9291a0cd
TT
15189static void
15190unlink_if_set (void *p)
15191{
15192 char **filename = p;
15193 if (*filename)
15194 unlink (*filename);
15195}
15196
1fd400ff
TT
15197/* A helper struct used when iterating over debug_types. */
15198struct signatured_type_index_data
15199{
15200 struct objfile *objfile;
15201 struct mapped_symtab *symtab;
15202 struct obstack *types_list;
987d643c 15203 htab_t psyms_seen;
1fd400ff
TT
15204 int cu_index;
15205};
15206
15207/* A helper function that writes a single signatured_type to an
15208 obstack. */
b89be57b 15209
1fd400ff
TT
15210static int
15211write_one_signatured_type (void **slot, void *d)
15212{
15213 struct signatured_type_index_data *info = d;
15214 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15215 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15216 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15217 gdb_byte val[8];
15218
15219 write_psymbols (info->symtab,
987d643c 15220 info->psyms_seen,
1fd400ff 15221 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15222 psymtab->n_global_syms, info->cu_index,
15223 0);
1fd400ff 15224 write_psymbols (info->symtab,
987d643c 15225 info->psyms_seen,
1fd400ff 15226 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15227 psymtab->n_static_syms, info->cu_index,
15228 1);
1fd400ff
TT
15229
15230 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15231 obstack_grow (info->types_list, val, 8);
15232 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15233 obstack_grow (info->types_list, val, 8);
15234 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15235 obstack_grow (info->types_list, val, 8);
15236
15237 ++info->cu_index;
15238
15239 return 1;
15240}
15241
987d643c
TT
15242/* A cleanup function for an htab_t. */
15243
15244static void
15245cleanup_htab (void *arg)
15246{
15247 htab_delete (arg);
15248}
15249
9291a0cd 15250/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15251
9291a0cd
TT
15252static void
15253write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15254{
15255 struct cleanup *cleanup;
15256 char *filename, *cleanup_filename;
1fd400ff
TT
15257 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15258 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15259 int i;
15260 FILE *out_file;
15261 struct mapped_symtab *symtab;
15262 offset_type val, size_of_contents, total_len;
15263 struct stat st;
15264 char buf[8];
987d643c 15265 htab_t psyms_seen;
9291a0cd
TT
15266
15267 if (!objfile->psymtabs)
15268 return;
15269 if (dwarf2_per_objfile->using_index)
15270 error (_("Cannot use an index to create the index"));
15271
15272 if (stat (objfile->name, &st) < 0)
15273 perror_with_name (_("Could not stat"));
15274
15275 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15276 INDEX_SUFFIX, (char *) NULL);
15277 cleanup = make_cleanup (xfree, filename);
15278
15279 out_file = fopen (filename, "wb");
15280 if (!out_file)
15281 error (_("Can't open `%s' for writing"), filename);
15282
15283 cleanup_filename = filename;
15284 make_cleanup (unlink_if_set, &cleanup_filename);
15285
15286 symtab = create_mapped_symtab ();
15287 make_cleanup (cleanup_mapped_symtab, symtab);
15288
15289 obstack_init (&addr_obstack);
15290 make_cleanup_obstack_free (&addr_obstack);
15291
15292 obstack_init (&cu_list);
15293 make_cleanup_obstack_free (&cu_list);
15294
1fd400ff
TT
15295 obstack_init (&types_cu_list);
15296 make_cleanup_obstack_free (&types_cu_list);
15297
987d643c
TT
15298 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15299 NULL, xcalloc, xfree);
15300 make_cleanup (cleanup_htab, psyms_seen);
15301
1fd400ff
TT
15302 /* The list is already sorted, so we don't need to do additional
15303 work here. Also, the debug_types entries do not appear in
15304 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15305 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15306 {
e254ef6a
DE
15307 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15308 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15309 gdb_byte val[8];
15310
15311 write_psymbols (symtab,
987d643c 15312 psyms_seen,
9291a0cd 15313 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15314 psymtab->n_global_syms, i,
15315 0);
9291a0cd 15316 write_psymbols (symtab,
987d643c 15317 psyms_seen,
9291a0cd 15318 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15319 psymtab->n_static_syms, i,
15320 1);
9291a0cd
TT
15321
15322 add_address_entry (objfile, &addr_obstack, psymtab, i);
15323
e254ef6a 15324 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15325 obstack_grow (&cu_list, val, 8);
e254ef6a 15326 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15327 obstack_grow (&cu_list, val, 8);
15328 }
15329
1fd400ff
TT
15330 /* Write out the .debug_type entries, if any. */
15331 if (dwarf2_per_objfile->signatured_types)
15332 {
15333 struct signatured_type_index_data sig_data;
15334
15335 sig_data.objfile = objfile;
15336 sig_data.symtab = symtab;
15337 sig_data.types_list = &types_cu_list;
987d643c 15338 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15339 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15340 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15341 write_one_signatured_type, &sig_data);
15342 }
15343
9291a0cd
TT
15344 obstack_init (&constant_pool);
15345 make_cleanup_obstack_free (&constant_pool);
15346 obstack_init (&symtab_obstack);
15347 make_cleanup_obstack_free (&symtab_obstack);
15348 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15349
15350 obstack_init (&contents);
15351 make_cleanup_obstack_free (&contents);
1fd400ff 15352 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15353 total_len = size_of_contents;
15354
15355 /* The version number. */
987d643c 15356 val = MAYBE_SWAP (3);
9291a0cd
TT
15357 obstack_grow (&contents, &val, sizeof (val));
15358
15359 /* The offset of the CU list from the start of the file. */
15360 val = MAYBE_SWAP (total_len);
15361 obstack_grow (&contents, &val, sizeof (val));
15362 total_len += obstack_object_size (&cu_list);
15363
1fd400ff
TT
15364 /* The offset of the types CU list from the start of the file. */
15365 val = MAYBE_SWAP (total_len);
15366 obstack_grow (&contents, &val, sizeof (val));
15367 total_len += obstack_object_size (&types_cu_list);
15368
9291a0cd
TT
15369 /* The offset of the address table from the start of the file. */
15370 val = MAYBE_SWAP (total_len);
15371 obstack_grow (&contents, &val, sizeof (val));
15372 total_len += obstack_object_size (&addr_obstack);
15373
15374 /* The offset of the symbol table from the start of the file. */
15375 val = MAYBE_SWAP (total_len);
15376 obstack_grow (&contents, &val, sizeof (val));
15377 total_len += obstack_object_size (&symtab_obstack);
15378
15379 /* The offset of the constant pool from the start of the file. */
15380 val = MAYBE_SWAP (total_len);
15381 obstack_grow (&contents, &val, sizeof (val));
15382 total_len += obstack_object_size (&constant_pool);
15383
15384 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15385
15386 write_obstack (out_file, &contents);
15387 write_obstack (out_file, &cu_list);
1fd400ff 15388 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15389 write_obstack (out_file, &addr_obstack);
15390 write_obstack (out_file, &symtab_obstack);
15391 write_obstack (out_file, &constant_pool);
15392
15393 fclose (out_file);
15394
15395 /* We want to keep the file, so we set cleanup_filename to NULL
15396 here. See unlink_if_set. */
15397 cleanup_filename = NULL;
15398
15399 do_cleanups (cleanup);
15400}
15401
15402/* The mapped index file format is designed to be directly mmap()able
15403 on any architecture. In most cases, a datum is represented using a
15404 little-endian 32-bit integer value, called an offset_type. Big
15405 endian machines must byte-swap the values before using them.
15406 Exceptions to this rule are noted. The data is laid out such that
15407 alignment is always respected.
15408
15409 A mapped index consists of several sections.
15410
15411 1. The file header. This is a sequence of values, of offset_type
15412 unless otherwise noted:
987d643c
TT
15413
15414 [0] The version number, currently 3. Versions 1 and 2 are
15415 obsolete.
9291a0cd 15416 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15417 [2] The offset, from the start of the file, of the types CU list.
15418 Note that this section can be empty, in which case this offset will
15419 be equal to the next offset.
15420 [3] The offset, from the start of the file, of the address section.
15421 [4] The offset, from the start of the file, of the symbol table.
15422 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15423
15424 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15425 little-endian values, sorted by the CU offset. The first element
15426 in each pair is the offset of a CU in the .debug_info section. The
15427 second element in each pair is the length of that CU. References
15428 to a CU elsewhere in the map are done using a CU index, which is
15429 just the 0-based index into this table. Note that if there are
15430 type CUs, then conceptually CUs and type CUs form a single list for
15431 the purposes of CU indices.
15432
987d643c
TT
15433 3. The types CU list. This is a sequence of triplets of 64-bit
15434 little-endian values. In a triplet, the first value is the CU
15435 offset, the second value is the type offset in the CU, and the
15436 third value is the type signature. The types CU list is not
15437 sorted.
9291a0cd 15438
987d643c 15439 4. The address section. The address section consists of a sequence
9291a0cd
TT
15440 of address entries. Each address entry has three elements.
15441 [0] The low address. This is a 64-bit little-endian value.
15442 [1] The high address. This is a 64-bit little-endian value.
15443 [2] The CU index. This is an offset_type value.
15444
987d643c 15445 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15446 table is always a power of 2. The initial hash and the step are
15447 currently defined by the `find_slot' function.
15448
15449 Each slot in the hash table consists of a pair of offset_type
15450 values. The first value is the offset of the symbol's name in the
15451 constant pool. The second value is the offset of the CU vector in
15452 the constant pool.
15453
15454 If both values are 0, then this slot in the hash table is empty.
15455 This is ok because while 0 is a valid constant pool index, it
15456 cannot be a valid index for both a string and a CU vector.
15457
15458 A string in the constant pool is stored as a \0-terminated string,
15459 as you'd expect.
15460
15461 A CU vector in the constant pool is a sequence of offset_type
15462 values. The first value is the number of CU indices in the vector.
15463 Each subsequent value is the index of a CU in the CU list. This
15464 element in the hash table is used to indicate which CUs define the
15465 symbol.
15466
987d643c 15467 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15468 organized so that alignment is correct: CU vectors are stored
15469 first, followed by strings. */
11570e71 15470
9291a0cd
TT
15471static void
15472save_gdb_index_command (char *arg, int from_tty)
15473{
15474 struct objfile *objfile;
15475
15476 if (!arg || !*arg)
96d19272 15477 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15478
15479 ALL_OBJFILES (objfile)
15480 {
15481 struct stat st;
15482
15483 /* If the objfile does not correspond to an actual file, skip it. */
15484 if (stat (objfile->name, &st) < 0)
15485 continue;
15486
15487 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15488 if (dwarf2_per_objfile)
15489 {
15490 volatile struct gdb_exception except;
15491
15492 TRY_CATCH (except, RETURN_MASK_ERROR)
15493 {
15494 write_psymtabs_to_index (objfile, arg);
15495 }
15496 if (except.reason < 0)
15497 exception_fprintf (gdb_stderr, except,
15498 _("Error while writing index for `%s': "),
15499 objfile->name);
15500 }
15501 }
dce234bc
PP
15502}
15503
9291a0cd
TT
15504\f
15505
9eae7c52
TT
15506int dwarf2_always_disassemble;
15507
15508static void
15509show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15510 struct cmd_list_element *c, const char *value)
15511{
15512 fprintf_filtered (file, _("\
15513Whether to always disassemble DWARF expressions is %s.\n"),
15514 value);
15515}
15516
6502dd73
DJ
15517void _initialize_dwarf2_read (void);
15518
15519void
15520_initialize_dwarf2_read (void)
15521{
96d19272
JK
15522 struct cmd_list_element *c;
15523
dce234bc 15524 dwarf2_objfile_data_key
c1bd65d0 15525 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15526
1bedd215
AC
15527 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15528Set DWARF 2 specific variables.\n\
15529Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15530 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15531 0/*allow-unknown*/, &maintenance_set_cmdlist);
15532
1bedd215
AC
15533 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15534Show DWARF 2 specific variables\n\
15535Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15536 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15537 0/*allow-unknown*/, &maintenance_show_cmdlist);
15538
15539 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15540 &dwarf2_max_cache_age, _("\
15541Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15542Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15543A higher limit means that cached compilation units will be stored\n\
15544in memory longer, and more total memory will be used. Zero disables\n\
15545caching, which can slow down startup."),
2c5b56ce 15546 NULL,
920d2a44 15547 show_dwarf2_max_cache_age,
2c5b56ce 15548 &set_dwarf2_cmdlist,
ae038cb0 15549 &show_dwarf2_cmdlist);
d97bc12b 15550
9eae7c52
TT
15551 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15552 &dwarf2_always_disassemble, _("\
15553Set whether `info address' always disassembles DWARF expressions."), _("\
15554Show whether `info address' always disassembles DWARF expressions."), _("\
15555When enabled, DWARF expressions are always printed in an assembly-like\n\
15556syntax. When disabled, expressions will be printed in a more\n\
15557conversational style, when possible."),
15558 NULL,
15559 show_dwarf2_always_disassemble,
15560 &set_dwarf2_cmdlist,
15561 &show_dwarf2_cmdlist);
15562
d97bc12b
DE
15563 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15564Set debugging of the dwarf2 DIE reader."), _("\
15565Show debugging of the dwarf2 DIE reader."), _("\
15566When enabled (non-zero), DIEs are dumped after they are read in.\n\
15567The value is the maximum depth to print."),
15568 NULL,
15569 NULL,
15570 &setdebuglist, &showdebuglist);
9291a0cd 15571
96d19272 15572 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15573 _("\
15574Save a .gdb-index file.\n\
15575Usage: save gdb-index DIRECTORY"),
96d19272
JK
15576 &save_cmdlist);
15577 set_cmd_completer (c, filename_completer);
6502dd73 15578}