gdb:
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
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
93311388
DE
1244static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1245
ae038cb0
DJ
1246static void free_one_comp_unit (void *);
1247
1248static void free_cached_comp_units (void *);
1249
1250static void age_cached_comp_units (void);
1251
1252static void free_one_cached_comp_unit (void *);
1253
f792889a
DJ
1254static struct type *set_die_type (struct die_info *, struct type *,
1255 struct dwarf2_cu *);
1c379e20 1256
ae038cb0
DJ
1257static void create_all_comp_units (struct objfile *);
1258
1fd400ff
TT
1259static int create_debug_types_hash_table (struct objfile *objfile);
1260
93311388
DE
1261static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1262 struct objfile *);
10b3939b
DJ
1263
1264static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1265
1266static void dwarf2_add_dependence (struct dwarf2_cu *,
1267 struct dwarf2_per_cu_data *);
1268
ae038cb0
DJ
1269static void dwarf2_mark (struct dwarf2_cu *);
1270
1271static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1272
673bfd45
DE
1273static struct type *get_die_type_at_offset (unsigned int,
1274 struct dwarf2_per_cu_data *per_cu);
1275
f792889a 1276static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1277
9291a0cd
TT
1278static void dwarf2_release_queue (void *dummy);
1279
1280static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1281 struct objfile *objfile);
1282
1283static void process_queue (struct objfile *objfile);
1284
1285static void find_file_and_directory (struct die_info *die,
1286 struct dwarf2_cu *cu,
1287 char **name, char **comp_dir);
1288
1289static char *file_full_name (int file, struct line_header *lh,
1290 const char *comp_dir);
1291
1292static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1293 gdb_byte *info_ptr,
1294 gdb_byte *buffer,
1295 unsigned int buffer_size,
1296 bfd *abfd);
1297
1298static void init_cu_die_reader (struct die_reader_specs *reader,
1299 struct dwarf2_cu *cu);
1300
673bfd45 1301static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1302
9291a0cd
TT
1303#if WORDS_BIGENDIAN
1304
1305/* Convert VALUE between big- and little-endian. */
1306static offset_type
1307byte_swap (offset_type value)
1308{
1309 offset_type result;
1310
1311 result = (value & 0xff) << 24;
1312 result |= (value & 0xff00) << 8;
1313 result |= (value & 0xff0000) >> 8;
1314 result |= (value & 0xff000000) >> 24;
1315 return result;
1316}
1317
1318#define MAYBE_SWAP(V) byte_swap (V)
1319
1320#else
1321#define MAYBE_SWAP(V) (V)
1322#endif /* WORDS_BIGENDIAN */
1323
1324/* The suffix for an index file. */
1325#define INDEX_SUFFIX ".gdb-index"
1326
3da10d80
KS
1327static const char *dwarf2_physname (char *name, struct die_info *die,
1328 struct dwarf2_cu *cu);
1329
c906108c
SS
1330/* Try to locate the sections we need for DWARF 2 debugging
1331 information and return true if we have enough to do something. */
1332
1333int
6502dd73 1334dwarf2_has_info (struct objfile *objfile)
c906108c 1335{
be391dca
TT
1336 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1337 if (!dwarf2_per_objfile)
1338 {
1339 /* Initialize per-objfile state. */
1340 struct dwarf2_per_objfile *data
1341 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1342
be391dca
TT
1343 memset (data, 0, sizeof (*data));
1344 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1345 dwarf2_per_objfile = data;
6502dd73 1346
be391dca
TT
1347 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1348 dwarf2_per_objfile->objfile = objfile;
1349 }
1350 return (dwarf2_per_objfile->info.asection != NULL
1351 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1352}
1353
233a11ab
CS
1354/* When loading sections, we can either look for ".<name>", or for
1355 * ".z<name>", which indicates a compressed section. */
1356
1357static int
dce234bc 1358section_is_p (const char *section_name, const char *name)
233a11ab 1359{
dce234bc
PP
1360 return (section_name[0] == '.'
1361 && (strcmp (section_name + 1, name) == 0
1362 || (section_name[1] == 'z'
1363 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1364}
1365
c906108c
SS
1366/* This function is mapped across the sections and remembers the
1367 offset and size of each of the debugging sections we are interested
1368 in. */
1369
1370static void
72dca2f5 1371dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1372{
dce234bc 1373 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1374 {
dce234bc
PP
1375 dwarf2_per_objfile->info.asection = sectp;
1376 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1377 }
dce234bc 1378 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1379 {
dce234bc
PP
1380 dwarf2_per_objfile->abbrev.asection = sectp;
1381 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1382 }
dce234bc 1383 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1384 {
dce234bc
PP
1385 dwarf2_per_objfile->line.asection = sectp;
1386 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1387 }
dce234bc 1388 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1389 {
dce234bc
PP
1390 dwarf2_per_objfile->loc.asection = sectp;
1391 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1392 }
dce234bc 1393 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1394 {
dce234bc
PP
1395 dwarf2_per_objfile->macinfo.asection = sectp;
1396 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1397 }
dce234bc 1398 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1399 {
dce234bc
PP
1400 dwarf2_per_objfile->str.asection = sectp;
1401 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1402 }
dce234bc 1403 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1404 {
dce234bc
PP
1405 dwarf2_per_objfile->frame.asection = sectp;
1406 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1407 }
dce234bc 1408 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1409 {
3799ccc6 1410 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1411
3799ccc6
EZ
1412 if (aflag & SEC_HAS_CONTENTS)
1413 {
dce234bc
PP
1414 dwarf2_per_objfile->eh_frame.asection = sectp;
1415 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1416 }
b6af0555 1417 }
dce234bc 1418 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1419 {
dce234bc
PP
1420 dwarf2_per_objfile->ranges.asection = sectp;
1421 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1422 }
348e048f
DE
1423 else if (section_is_p (sectp->name, TYPES_SECTION))
1424 {
1425 dwarf2_per_objfile->types.asection = sectp;
1426 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1427 }
9291a0cd
TT
1428 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1429 {
1430 dwarf2_per_objfile->gdb_index.asection = sectp;
1431 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1432 }
dce234bc 1433
72dca2f5
FR
1434 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1435 && bfd_section_vma (abfd, sectp) == 0)
1436 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1437}
1438
dce234bc
PP
1439/* Decompress a section that was compressed using zlib. Store the
1440 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1441
1442static void
dce234bc
PP
1443zlib_decompress_section (struct objfile *objfile, asection *sectp,
1444 gdb_byte **outbuf, bfd_size_type *outsize)
1445{
1446 bfd *abfd = objfile->obfd;
1447#ifndef HAVE_ZLIB_H
1448 error (_("Support for zlib-compressed DWARF data (from '%s') "
1449 "is disabled in this copy of GDB"),
1450 bfd_get_filename (abfd));
1451#else
1452 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1453 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1454 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1455 bfd_size_type uncompressed_size;
1456 gdb_byte *uncompressed_buffer;
1457 z_stream strm;
1458 int rc;
1459 int header_size = 12;
1460
1461 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1462 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1463 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1464 bfd_get_filename (abfd));
1465
1466 /* Read the zlib header. In this case, it should be "ZLIB" followed
1467 by the uncompressed section size, 8 bytes in big-endian order. */
1468 if (compressed_size < header_size
1469 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1470 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1471 bfd_get_filename (abfd));
1472 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1475 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1476 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1477 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1478 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1479 uncompressed_size += compressed_buffer[11];
1480
1481 /* It is possible the section consists of several compressed
1482 buffers concatenated together, so we uncompress in a loop. */
1483 strm.zalloc = NULL;
1484 strm.zfree = NULL;
1485 strm.opaque = NULL;
1486 strm.avail_in = compressed_size - header_size;
1487 strm.next_in = (Bytef*) compressed_buffer + header_size;
1488 strm.avail_out = uncompressed_size;
1489 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1490 uncompressed_size);
1491 rc = inflateInit (&strm);
1492 while (strm.avail_in > 0)
1493 {
1494 if (rc != Z_OK)
1495 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1496 bfd_get_filename (abfd), rc);
1497 strm.next_out = ((Bytef*) uncompressed_buffer
1498 + (uncompressed_size - strm.avail_out));
1499 rc = inflate (&strm, Z_FINISH);
1500 if (rc != Z_STREAM_END)
1501 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1502 bfd_get_filename (abfd), rc);
1503 rc = inflateReset (&strm);
1504 }
1505 rc = inflateEnd (&strm);
1506 if (rc != Z_OK
1507 || strm.avail_out != 0)
1508 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1509 bfd_get_filename (abfd), rc);
1510
affddf13 1511 do_cleanups (cleanup);
dce234bc
PP
1512 *outbuf = uncompressed_buffer;
1513 *outsize = uncompressed_size;
1514#endif
233a11ab
CS
1515}
1516
dce234bc
PP
1517/* Read the contents of the section SECTP from object file specified by
1518 OBJFILE, store info about the section into INFO.
1519 If the section is compressed, uncompress it before returning. */
c906108c 1520
dce234bc
PP
1521static void
1522dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1523{
dce234bc
PP
1524 bfd *abfd = objfile->obfd;
1525 asection *sectp = info->asection;
1526 gdb_byte *buf, *retbuf;
1527 unsigned char header[4];
c906108c 1528
be391dca
TT
1529 if (info->readin)
1530 return;
dce234bc
PP
1531 info->buffer = NULL;
1532 info->was_mmapped = 0;
be391dca 1533 info->readin = 1;
188dd5d6 1534
dce234bc
PP
1535 if (info->asection == NULL || info->size == 0)
1536 return;
c906108c 1537
dce234bc
PP
1538 /* Check if the file has a 4-byte header indicating compression. */
1539 if (info->size > sizeof (header)
1540 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1541 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1542 {
1543 /* Upon decompression, update the buffer and its size. */
1544 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1545 {
1546 zlib_decompress_section (objfile, sectp, &info->buffer,
1547 &info->size);
1548 return;
1549 }
1550 }
4bdf3d34 1551
dce234bc
PP
1552#ifdef HAVE_MMAP
1553 if (pagesize == 0)
1554 pagesize = getpagesize ();
2e276125 1555
dce234bc
PP
1556 /* Only try to mmap sections which are large enough: we don't want to
1557 waste space due to fragmentation. Also, only try mmap for sections
1558 without relocations. */
1559
1560 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1561 {
1562 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1563 size_t map_length = info->size + sectp->filepos - pg_offset;
1564 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1565 MAP_PRIVATE, pg_offset);
1566
1567 if (retbuf != MAP_FAILED)
1568 {
1569 info->was_mmapped = 1;
1570 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1571#if HAVE_POSIX_MADVISE
1572 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1573#endif
dce234bc
PP
1574 return;
1575 }
1576 }
1577#endif
1578
1579 /* If we get here, we are a normal, not-compressed section. */
1580 info->buffer = buf
1581 = obstack_alloc (&objfile->objfile_obstack, info->size);
1582
1583 /* When debugging .o files, we may need to apply relocations; see
1584 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1585 We never compress sections in .o files, so we only need to
1586 try this when the section is not compressed. */
ac8035ab 1587 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1588 if (retbuf != NULL)
1589 {
1590 info->buffer = retbuf;
1591 return;
1592 }
1593
1594 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1595 || bfd_bread (buf, info->size, abfd) != info->size)
1596 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1597 bfd_get_filename (abfd));
1598}
1599
1600/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1601 SECTION_NAME. */
af34e669 1602
dce234bc
PP
1603void
1604dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1605 asection **sectp, gdb_byte **bufp,
1606 bfd_size_type *sizep)
1607{
1608 struct dwarf2_per_objfile *data
1609 = objfile_data (objfile, dwarf2_objfile_data_key);
1610 struct dwarf2_section_info *info;
a3b2a86b
TT
1611
1612 /* We may see an objfile without any DWARF, in which case we just
1613 return nothing. */
1614 if (data == NULL)
1615 {
1616 *sectp = NULL;
1617 *bufp = NULL;
1618 *sizep = 0;
1619 return;
1620 }
dce234bc
PP
1621 if (section_is_p (section_name, EH_FRAME_SECTION))
1622 info = &data->eh_frame;
1623 else if (section_is_p (section_name, FRAME_SECTION))
1624 info = &data->frame;
0d53c4c4 1625 else
f3574227 1626 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1627
1628 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1629 /* We haven't read this section in yet. Do it now. */
1630 dwarf2_read_section (objfile, info);
1631
1632 *sectp = info->asection;
1633 *bufp = info->buffer;
1634 *sizep = info->size;
1635}
1636
9291a0cd
TT
1637\f
1638
1639/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1640 this CU came. */
2fdf6df6 1641
9291a0cd
TT
1642static void
1643dw2_do_instantiate_symtab (struct objfile *objfile,
1644 struct dwarf2_per_cu_data *per_cu)
1645{
1646 struct cleanup *back_to;
1647
1648 back_to = make_cleanup (dwarf2_release_queue, NULL);
1649
1650 queue_comp_unit (per_cu, objfile);
1651
1652 if (per_cu->from_debug_types)
1653 read_signatured_type_at_offset (objfile, per_cu->offset);
1654 else
1655 load_full_comp_unit (per_cu, objfile);
1656
1657 process_queue (objfile);
1658
1659 /* Age the cache, releasing compilation units that have not
1660 been used recently. */
1661 age_cached_comp_units ();
1662
1663 do_cleanups (back_to);
1664}
1665
1666/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1667 the objfile from which this CU came. Returns the resulting symbol
1668 table. */
2fdf6df6 1669
9291a0cd
TT
1670static struct symtab *
1671dw2_instantiate_symtab (struct objfile *objfile,
1672 struct dwarf2_per_cu_data *per_cu)
1673{
1674 if (!per_cu->v.quick->symtab)
1675 {
1676 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1677 increment_reading_symtab ();
1678 dw2_do_instantiate_symtab (objfile, per_cu);
1679 do_cleanups (back_to);
1680 }
1681 return per_cu->v.quick->symtab;
1682}
1683
1fd400ff 1684/* Return the CU given its index. */
2fdf6df6 1685
1fd400ff
TT
1686static struct dwarf2_per_cu_data *
1687dw2_get_cu (int index)
1688{
1689 if (index >= dwarf2_per_objfile->n_comp_units)
1690 {
1691 index -= dwarf2_per_objfile->n_comp_units;
1692 return dwarf2_per_objfile->type_comp_units[index];
1693 }
1694 return dwarf2_per_objfile->all_comp_units[index];
1695}
1696
9291a0cd
TT
1697/* A helper function that knows how to read a 64-bit value in a way
1698 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1699 otherwise. */
2fdf6df6 1700
9291a0cd
TT
1701static int
1702extract_cu_value (const char *bytes, ULONGEST *result)
1703{
1704 if (sizeof (ULONGEST) < 8)
1705 {
1706 int i;
1707
1708 /* Ignore the upper 4 bytes if they are all zero. */
1709 for (i = 0; i < 4; ++i)
1710 if (bytes[i + 4] != 0)
1711 return 0;
1712
1713 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1714 }
1715 else
1716 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1717 return 1;
1718}
1719
1720/* Read the CU list from the mapped index, and use it to create all
1721 the CU objects for this objfile. Return 0 if something went wrong,
1722 1 if everything went ok. */
2fdf6df6 1723
9291a0cd 1724static int
1fd400ff
TT
1725create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1726 offset_type cu_list_elements)
9291a0cd
TT
1727{
1728 offset_type i;
9291a0cd
TT
1729
1730 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1731 dwarf2_per_objfile->all_comp_units
1732 = obstack_alloc (&objfile->objfile_obstack,
1733 dwarf2_per_objfile->n_comp_units
1734 * sizeof (struct dwarf2_per_cu_data *));
1735
1736 for (i = 0; i < cu_list_elements; i += 2)
1737 {
1738 struct dwarf2_per_cu_data *the_cu;
1739 ULONGEST offset, length;
1740
1741 if (!extract_cu_value (cu_list, &offset)
1742 || !extract_cu_value (cu_list + 8, &length))
1743 return 0;
1744 cu_list += 2 * 8;
1745
1746 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1747 struct dwarf2_per_cu_data);
1748 the_cu->offset = offset;
1749 the_cu->length = length;
1750 the_cu->objfile = objfile;
1751 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1752 struct dwarf2_per_cu_quick_data);
1753 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1754 }
1755
1756 return 1;
1757}
1758
1fd400ff 1759/* Create the signatured type hash table from the index. */
673bfd45 1760
1fd400ff 1761static int
673bfd45
DE
1762create_signatured_type_table_from_index (struct objfile *objfile,
1763 const gdb_byte *bytes,
1764 offset_type elements)
1fd400ff
TT
1765{
1766 offset_type i;
673bfd45 1767 htab_t sig_types_hash;
1fd400ff
TT
1768
1769 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1770 dwarf2_per_objfile->type_comp_units
1771 = obstack_alloc (&objfile->objfile_obstack,
1772 dwarf2_per_objfile->n_type_comp_units
1773 * sizeof (struct dwarf2_per_cu_data *));
1774
673bfd45 1775 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1776
1777 for (i = 0; i < elements; i += 3)
1778 {
1779 struct signatured_type *type_sig;
1780 ULONGEST offset, type_offset, signature;
1781 void **slot;
1782
1783 if (!extract_cu_value (bytes, &offset)
1784 || !extract_cu_value (bytes + 8, &type_offset))
1785 return 0;
1786 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1787 bytes += 3 * 8;
1788
1789 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1790 struct signatured_type);
1791 type_sig->signature = signature;
1792 type_sig->offset = offset;
1793 type_sig->type_offset = type_offset;
1794 type_sig->per_cu.from_debug_types = 1;
1795 type_sig->per_cu.offset = offset;
1796 type_sig->per_cu.objfile = objfile;
1797 type_sig->per_cu.v.quick
1798 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1799 struct dwarf2_per_cu_quick_data);
1800
673bfd45 1801 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1802 *slot = type_sig;
1803
1804 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1805 }
1806
673bfd45 1807 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1808
1809 return 1;
1810}
1811
9291a0cd
TT
1812/* Read the address map data from the mapped index, and use it to
1813 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1814
9291a0cd
TT
1815static void
1816create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1817{
1818 const gdb_byte *iter, *end;
1819 struct obstack temp_obstack;
1820 struct addrmap *mutable_map;
1821 struct cleanup *cleanup;
1822 CORE_ADDR baseaddr;
1823
1824 obstack_init (&temp_obstack);
1825 cleanup = make_cleanup_obstack_free (&temp_obstack);
1826 mutable_map = addrmap_create_mutable (&temp_obstack);
1827
1828 iter = index->address_table;
1829 end = iter + index->address_table_size;
1830
1831 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1832
1833 while (iter < end)
1834 {
1835 ULONGEST hi, lo, cu_index;
1836 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1837 iter += 8;
1838 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1839 iter += 8;
1840 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1841 iter += 4;
1842
1843 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1844 dw2_get_cu (cu_index));
9291a0cd
TT
1845 }
1846
1847 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1848 &objfile->objfile_obstack);
1849 do_cleanups (cleanup);
1850}
1851
1852/* The hash function for strings in the mapped index. This is the
1853 same as the hashtab.c hash function, but we keep a separate copy to
1854 maintain control over the implementation. This is necessary
1855 because the hash function is tied to the format of the mapped index
1856 file. */
2fdf6df6 1857
9291a0cd
TT
1858static hashval_t
1859mapped_index_string_hash (const void *p)
1860{
1861 const unsigned char *str = (const unsigned char *) p;
1862 hashval_t r = 0;
1863 unsigned char c;
1864
1865 while ((c = *str++) != 0)
1866 r = r * 67 + c - 113;
1867
1868 return r;
1869}
1870
1871/* Find a slot in the mapped index INDEX for the object named NAME.
1872 If NAME is found, set *VEC_OUT to point to the CU vector in the
1873 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1874
9291a0cd
TT
1875static int
1876find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1877 offset_type **vec_out)
1878{
1879 offset_type hash = mapped_index_string_hash (name);
1880 offset_type slot, step;
1881
3876f04e
DE
1882 slot = hash & (index->symbol_table_slots - 1);
1883 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1884
1885 for (;;)
1886 {
1887 /* Convert a slot number to an offset into the table. */
1888 offset_type i = 2 * slot;
1889 const char *str;
3876f04e 1890 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1891 return 0;
1892
3876f04e 1893 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1894 if (!strcmp (name, str))
1895 {
1896 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1897 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1898 return 1;
1899 }
1900
3876f04e 1901 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1902 }
1903}
1904
1905/* Read the index file. If everything went ok, initialize the "quick"
1906 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1907
9291a0cd
TT
1908static int
1909dwarf2_read_index (struct objfile *objfile)
1910{
9291a0cd
TT
1911 char *addr;
1912 struct mapped_index *map;
b3b272e1 1913 offset_type *metadata;
ac0b195c
KW
1914 const gdb_byte *cu_list;
1915 const gdb_byte *types_list = NULL;
1916 offset_type version, cu_list_elements;
1917 offset_type types_list_elements = 0;
1fd400ff 1918 int i;
9291a0cd
TT
1919
1920 if (dwarf2_per_objfile->gdb_index.asection == NULL
1921 || dwarf2_per_objfile->gdb_index.size == 0)
1922 return 0;
82430852
JK
1923
1924 /* Older elfutils strip versions could keep the section in the main
1925 executable while splitting it for the separate debug info file. */
1926 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1927 & SEC_HAS_CONTENTS) == 0)
1928 return 0;
1929
9291a0cd
TT
1930 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1931
1932 addr = dwarf2_per_objfile->gdb_index.buffer;
1933 /* Version check. */
1fd400ff 1934 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
1935 /* Versions earlier than 3 emitted every copy of a psymbol. This
1936 causes the index to behave very poorly for certain requests. So,
1937 it seems better to just ignore such indices. */
1938 if (version < 3)
9291a0cd
TT
1939 return 0;
1940
1941 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1942 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1943
1944 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1945
1946 i = 0;
1947 cu_list = addr + MAYBE_SWAP (metadata[i]);
1948 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1949 / 8);
1fd400ff
TT
1950 ++i;
1951
987d643c
TT
1952 types_list = addr + MAYBE_SWAP (metadata[i]);
1953 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1954 - MAYBE_SWAP (metadata[i]))
1955 / 8);
1956 ++i;
1fd400ff
TT
1957
1958 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1959 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1960 - MAYBE_SWAP (metadata[i]));
1961 ++i;
1962
3876f04e
DE
1963 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1964 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1965 - MAYBE_SWAP (metadata[i]))
1966 / (2 * sizeof (offset_type)));
1fd400ff 1967 ++i;
9291a0cd 1968
1fd400ff
TT
1969 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1970
1971 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1972 return 0;
1973
987d643c 1974 if (types_list_elements
673bfd45
DE
1975 && !create_signatured_type_table_from_index (objfile, types_list,
1976 types_list_elements))
9291a0cd
TT
1977 return 0;
1978
1979 create_addrmap_from_index (objfile, map);
1980
1981 dwarf2_per_objfile->index_table = map;
1982 dwarf2_per_objfile->using_index = 1;
1983
1984 return 1;
1985}
1986
1987/* A helper for the "quick" functions which sets the global
1988 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 1989
9291a0cd
TT
1990static void
1991dw2_setup (struct objfile *objfile)
1992{
1993 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1994 gdb_assert (dwarf2_per_objfile);
1995}
1996
1997/* A helper for the "quick" functions which attempts to read the line
1998 table for THIS_CU. */
2fdf6df6 1999
9291a0cd
TT
2000static void
2001dw2_require_line_header (struct objfile *objfile,
2002 struct dwarf2_per_cu_data *this_cu)
2003{
2004 bfd *abfd = objfile->obfd;
2005 struct line_header *lh = NULL;
2006 struct attribute *attr;
2007 struct cleanup *cleanups;
2008 struct die_info *comp_unit_die;
36374493 2009 struct dwarf2_section_info* sec;
9291a0cd
TT
2010 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2011 int has_children, i;
2012 struct dwarf2_cu cu;
2013 unsigned int bytes_read, buffer_size;
2014 struct die_reader_specs reader_specs;
2015 char *name, *comp_dir;
2016
2017 if (this_cu->v.quick->read_lines)
2018 return;
2019 this_cu->v.quick->read_lines = 1;
2020
2021 memset (&cu, 0, sizeof (cu));
2022 cu.objfile = objfile;
2023 obstack_init (&cu.comp_unit_obstack);
2024
2025 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2026
36374493
DE
2027 if (this_cu->from_debug_types)
2028 sec = &dwarf2_per_objfile->types;
2029 else
2030 sec = &dwarf2_per_objfile->info;
2031 dwarf2_read_section (objfile, sec);
2032 buffer_size = sec->size;
2033 buffer = sec->buffer;
9291a0cd
TT
2034 info_ptr = buffer + this_cu->offset;
2035 beg_of_comp_unit = info_ptr;
2036
2037 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2038 buffer, buffer_size,
2039 abfd);
2040
2041 /* Complete the cu_header. */
2042 cu.header.offset = beg_of_comp_unit - buffer;
2043 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2044
2045 this_cu->cu = &cu;
2046 cu.per_cu = this_cu;
2047
2048 dwarf2_read_abbrevs (abfd, &cu);
2049 make_cleanup (dwarf2_free_abbrev_table, &cu);
2050
2051 if (this_cu->from_debug_types)
2052 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2053 init_cu_die_reader (&reader_specs, &cu);
2054 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2055 &has_children);
2056
2057 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2058 if (attr)
2059 {
2060 unsigned int line_offset = DW_UNSND (attr);
2061 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2062 }
2063 if (lh == NULL)
2064 {
2065 do_cleanups (cleanups);
2066 return;
2067 }
2068
2069 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2070
2071 this_cu->v.quick->lines = lh;
2072
2073 this_cu->v.quick->file_names
2074 = obstack_alloc (&objfile->objfile_obstack,
2075 lh->num_file_names * sizeof (char *));
2076 for (i = 0; i < lh->num_file_names; ++i)
2077 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2078
2079 do_cleanups (cleanups);
2080}
2081
2082/* A helper for the "quick" functions which computes and caches the
2083 real path for a given file name from the line table.
2084 dw2_require_line_header must have been called before this is
2085 invoked. */
2fdf6df6 2086
9291a0cd
TT
2087static const char *
2088dw2_require_full_path (struct objfile *objfile,
e254ef6a 2089 struct dwarf2_per_cu_data *per_cu,
9291a0cd
TT
2090 int index)
2091{
e254ef6a
DE
2092 if (!per_cu->v.quick->full_names)
2093 per_cu->v.quick->full_names
9291a0cd 2094 = OBSTACK_CALLOC (&objfile->objfile_obstack,
e254ef6a 2095 per_cu->v.quick->lines->num_file_names,
9291a0cd
TT
2096 sizeof (char *));
2097
e254ef6a
DE
2098 if (!per_cu->v.quick->full_names[index])
2099 per_cu->v.quick->full_names[index]
2100 = gdb_realpath (per_cu->v.quick->file_names[index]);
9291a0cd 2101
e254ef6a 2102 return per_cu->v.quick->full_names[index];
9291a0cd
TT
2103}
2104
2105static struct symtab *
2106dw2_find_last_source_symtab (struct objfile *objfile)
2107{
2108 int index;
2109 dw2_setup (objfile);
2110 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2111 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2112}
2113
2114static void
2115dw2_forget_cached_source_info (struct objfile *objfile)
2116{
2117 int i;
2118
2119 dw2_setup (objfile);
1fd400ff
TT
2120 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2121 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2122 {
e254ef6a 2123 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2124
e254ef6a 2125 if (per_cu->v.quick->full_names)
9291a0cd
TT
2126 {
2127 int j;
2128
e254ef6a
DE
2129 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2130 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
2131 }
2132 }
2133}
2134
2135static int
2136dw2_lookup_symtab (struct objfile *objfile, const char *name,
2137 const char *full_path, const char *real_path,
2138 struct symtab **result)
2139{
2140 int i;
2141 int check_basename = lbasename (name) == name;
2142 struct dwarf2_per_cu_data *base_cu = NULL;
2143
2144 dw2_setup (objfile);
1fd400ff
TT
2145 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2146 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2147 {
2148 int j;
e254ef6a 2149 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2150
e254ef6a 2151 if (per_cu->v.quick->symtab)
9291a0cd
TT
2152 continue;
2153
e254ef6a
DE
2154 dw2_require_line_header (objfile, per_cu);
2155 if (!per_cu->v.quick->lines)
9291a0cd
TT
2156 continue;
2157
e254ef6a 2158 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2159 {
e254ef6a 2160 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2161
2162 if (FILENAME_CMP (name, this_name) == 0)
2163 {
e254ef6a 2164 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2165 return 1;
2166 }
2167
2168 if (check_basename && ! base_cu
2169 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2170 base_cu = per_cu;
9291a0cd
TT
2171
2172 if (full_path != NULL)
2173 {
2174 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2175 per_cu, j);
9291a0cd
TT
2176
2177 if (this_full_name
2178 && FILENAME_CMP (full_path, this_full_name) == 0)
2179 {
e254ef6a 2180 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2181 return 1;
2182 }
2183 }
2184
2185 if (real_path != NULL)
2186 {
2187 const char *this_full_name = dw2_require_full_path (objfile,
e254ef6a 2188 per_cu, j);
9291a0cd
TT
2189
2190 if (this_full_name != NULL)
2191 {
2192 char *rp = gdb_realpath (this_full_name);
2193 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2194 {
2195 xfree (rp);
e254ef6a 2196 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2197 return 1;
2198 }
2199 xfree (rp);
2200 }
2201 }
2202 }
2203 }
2204
2205 if (base_cu)
2206 {
2207 *result = dw2_instantiate_symtab (objfile, base_cu);
2208 return 1;
2209 }
2210
2211 return 0;
2212}
2213
2214static struct symtab *
2215dw2_lookup_symbol (struct objfile *objfile, int block_index,
2216 const char *name, domain_enum domain)
2217{
774b6a14 2218 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2219 instead. */
2220 return NULL;
2221}
2222
2223/* A helper function that expands all symtabs that hold an object
2224 named NAME. */
2fdf6df6 2225
9291a0cd
TT
2226static void
2227dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2228{
2229 dw2_setup (objfile);
2230
2231 if (dwarf2_per_objfile->index_table)
2232 {
2233 offset_type *vec;
2234
2235 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2236 name, &vec))
2237 {
2238 offset_type i, len = MAYBE_SWAP (*vec);
2239 for (i = 0; i < len; ++i)
2240 {
2241 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2242 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2243
e254ef6a 2244 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2245 }
2246 }
2247 }
2248}
2249
774b6a14
TT
2250static void
2251dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2252 int kind, const char *name,
2253 domain_enum domain)
9291a0cd 2254{
774b6a14 2255 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2256}
2257
2258static void
2259dw2_print_stats (struct objfile *objfile)
2260{
2261 int i, count;
2262
2263 dw2_setup (objfile);
2264 count = 0;
1fd400ff
TT
2265 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2266 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2267 {
e254ef6a 2268 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2269
e254ef6a 2270 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2271 ++count;
2272 }
2273 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2274}
2275
2276static void
2277dw2_dump (struct objfile *objfile)
2278{
2279 /* Nothing worth printing. */
2280}
2281
2282static void
2283dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2284 struct section_offsets *delta)
2285{
2286 /* There's nothing to relocate here. */
2287}
2288
2289static void
2290dw2_expand_symtabs_for_function (struct objfile *objfile,
2291 const char *func_name)
2292{
2293 dw2_do_expand_symtabs_matching (objfile, func_name);
2294}
2295
2296static void
2297dw2_expand_all_symtabs (struct objfile *objfile)
2298{
2299 int i;
2300
2301 dw2_setup (objfile);
1fd400ff
TT
2302
2303 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2304 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2305 {
e254ef6a 2306 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2307
e254ef6a 2308 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2309 }
2310}
2311
2312static void
2313dw2_expand_symtabs_with_filename (struct objfile *objfile,
2314 const char *filename)
2315{
2316 int i;
2317
2318 dw2_setup (objfile);
1fd400ff
TT
2319 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2320 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2321 {
2322 int j;
e254ef6a 2323 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2324
e254ef6a 2325 if (per_cu->v.quick->symtab)
9291a0cd
TT
2326 continue;
2327
e254ef6a
DE
2328 dw2_require_line_header (objfile, per_cu);
2329 if (!per_cu->v.quick->lines)
9291a0cd
TT
2330 continue;
2331
e254ef6a 2332 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2333 {
e254ef6a 2334 const char *this_name = per_cu->v.quick->file_names[j];
9291a0cd
TT
2335 if (strcmp (this_name, filename) == 0)
2336 {
e254ef6a 2337 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2338 break;
2339 }
2340 }
2341 }
2342}
2343
dd786858 2344static const char *
9291a0cd
TT
2345dw2_find_symbol_file (struct objfile *objfile, const char *name)
2346{
e254ef6a 2347 struct dwarf2_per_cu_data *per_cu;
9291a0cd
TT
2348 offset_type *vec;
2349
2350 dw2_setup (objfile);
2351
2352 if (!dwarf2_per_objfile->index_table)
2353 return NULL;
2354
2355 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2356 name, &vec))
2357 return NULL;
2358
2359 /* Note that this just looks at the very first one named NAME -- but
2360 actually we are looking for a function. find_main_filename
2361 should be rewritten so that it doesn't require a custom hook. It
2362 could just use the ordinary symbol tables. */
2363 /* vec[0] is the length, which must always be >0. */
e254ef6a 2364 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2365
e254ef6a
DE
2366 dw2_require_line_header (objfile, per_cu);
2367 if (!per_cu->v.quick->lines)
9291a0cd
TT
2368 return NULL;
2369
e254ef6a 2370 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2371}
2372
2373static void
40658b94
PH
2374dw2_map_matching_symbols (const char * name, domain_enum namespace,
2375 struct objfile *objfile, int global,
2376 int (*callback) (struct block *,
2377 struct symbol *, void *),
2378 void *data,
2379 int (*match) (const char *, const char *),
2380 int (*ordered_compare) (const char *,
2381 const char *))
9291a0cd 2382{
40658b94 2383 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2384 current language is Ada for a non-Ada objfile using GNU index. As Ada
2385 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2386}
2387
2388static void
2389dw2_expand_symtabs_matching (struct objfile *objfile,
2390 int (*file_matcher) (const char *, void *),
2391 int (*name_matcher) (const char *, void *),
2392 domain_enum kind,
2393 void *data)
2394{
2395 int i;
2396 offset_type iter;
4b5246aa 2397 struct mapped_index *index;
9291a0cd
TT
2398
2399 dw2_setup (objfile);
2400 if (!dwarf2_per_objfile->index_table)
2401 return;
4b5246aa 2402 index = dwarf2_per_objfile->index_table;
9291a0cd 2403
1fd400ff
TT
2404 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2405 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2406 {
2407 int j;
e254ef6a 2408 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2409
e254ef6a
DE
2410 per_cu->v.quick->mark = 0;
2411 if (per_cu->v.quick->symtab)
9291a0cd
TT
2412 continue;
2413
e254ef6a
DE
2414 dw2_require_line_header (objfile, per_cu);
2415 if (!per_cu->v.quick->lines)
9291a0cd
TT
2416 continue;
2417
e254ef6a 2418 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2419 {
e254ef6a 2420 if (file_matcher (per_cu->v.quick->file_names[j], data))
9291a0cd 2421 {
e254ef6a 2422 per_cu->v.quick->mark = 1;
9291a0cd
TT
2423 break;
2424 }
2425 }
2426 }
2427
3876f04e 2428 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2429 {
2430 offset_type idx = 2 * iter;
2431 const char *name;
2432 offset_type *vec, vec_len, vec_idx;
2433
3876f04e 2434 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2435 continue;
2436
3876f04e 2437 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2438
2439 if (! (*name_matcher) (name, data))
2440 continue;
2441
2442 /* The name was matched, now expand corresponding CUs that were
2443 marked. */
4b5246aa 2444 vec = (offset_type *) (index->constant_pool
3876f04e 2445 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2446 vec_len = MAYBE_SWAP (vec[0]);
2447 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2448 {
e254ef6a 2449 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2450
e254ef6a
DE
2451 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2452 if (per_cu->v.quick->mark)
2453 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2454 }
2455 }
2456}
2457
2458static struct symtab *
2459dw2_find_pc_sect_symtab (struct objfile *objfile,
2460 struct minimal_symbol *msymbol,
2461 CORE_ADDR pc,
2462 struct obj_section *section,
2463 int warn_if_readin)
2464{
2465 struct dwarf2_per_cu_data *data;
2466
2467 dw2_setup (objfile);
2468
2469 if (!objfile->psymtabs_addrmap)
2470 return NULL;
2471
2472 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2473 if (!data)
2474 return NULL;
2475
2476 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2477 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2478 paddress (get_objfile_arch (objfile), pc));
2479
2480 return dw2_instantiate_symtab (objfile, data);
2481}
2482
2483static void
2484dw2_map_symbol_names (struct objfile *objfile,
2485 void (*fun) (const char *, void *),
2486 void *data)
2487{
2488 offset_type iter;
4b5246aa
TT
2489 struct mapped_index *index;
2490
9291a0cd
TT
2491 dw2_setup (objfile);
2492
2493 if (!dwarf2_per_objfile->index_table)
2494 return;
4b5246aa 2495 index = dwarf2_per_objfile->index_table;
9291a0cd 2496
3876f04e 2497 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2498 {
2499 offset_type idx = 2 * iter;
2500 const char *name;
2501 offset_type *vec, vec_len, vec_idx;
2502
3876f04e 2503 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2504 continue;
2505
3876f04e 2506 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2507
2508 (*fun) (name, data);
2509 }
2510}
2511
2512static void
2513dw2_map_symbol_filenames (struct objfile *objfile,
2514 void (*fun) (const char *, const char *, void *),
2515 void *data)
2516{
2517 int i;
2518
2519 dw2_setup (objfile);
1fd400ff
TT
2520 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2521 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2522 {
2523 int j;
e254ef6a 2524 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2525
e254ef6a 2526 if (per_cu->v.quick->symtab)
9291a0cd
TT
2527 continue;
2528
e254ef6a
DE
2529 dw2_require_line_header (objfile, per_cu);
2530 if (!per_cu->v.quick->lines)
9291a0cd
TT
2531 continue;
2532
e254ef6a 2533 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 2534 {
e254ef6a
DE
2535 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2536 j);
2537 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
9291a0cd
TT
2538 }
2539 }
2540}
2541
2542static int
2543dw2_has_symbols (struct objfile *objfile)
2544{
2545 return 1;
2546}
2547
2548const struct quick_symbol_functions dwarf2_gdb_index_functions =
2549{
2550 dw2_has_symbols,
2551 dw2_find_last_source_symtab,
2552 dw2_forget_cached_source_info,
2553 dw2_lookup_symtab,
2554 dw2_lookup_symbol,
774b6a14 2555 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2556 dw2_print_stats,
2557 dw2_dump,
2558 dw2_relocate,
2559 dw2_expand_symtabs_for_function,
2560 dw2_expand_all_symtabs,
2561 dw2_expand_symtabs_with_filename,
2562 dw2_find_symbol_file,
40658b94 2563 dw2_map_matching_symbols,
9291a0cd
TT
2564 dw2_expand_symtabs_matching,
2565 dw2_find_pc_sect_symtab,
2566 dw2_map_symbol_names,
2567 dw2_map_symbol_filenames
2568};
2569
2570/* Initialize for reading DWARF for this objfile. Return 0 if this
2571 file will use psymtabs, or 1 if using the GNU index. */
2572
2573int
2574dwarf2_initialize_objfile (struct objfile *objfile)
2575{
2576 /* If we're about to read full symbols, don't bother with the
2577 indices. In this case we also don't care if some other debug
2578 format is making psymtabs, because they are all about to be
2579 expanded anyway. */
2580 if ((objfile->flags & OBJF_READNOW))
2581 {
2582 int i;
2583
2584 dwarf2_per_objfile->using_index = 1;
2585 create_all_comp_units (objfile);
1fd400ff 2586 create_debug_types_hash_table (objfile);
9291a0cd 2587
1fd400ff
TT
2588 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2589 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2590 {
e254ef6a 2591 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2592
e254ef6a
DE
2593 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2594 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2595 }
2596
2597 /* Return 1 so that gdb sees the "quick" functions. However,
2598 these functions will be no-ops because we will have expanded
2599 all symtabs. */
2600 return 1;
2601 }
2602
2603 if (dwarf2_read_index (objfile))
2604 return 1;
2605
2606 dwarf2_build_psymtabs (objfile);
2607 return 0;
2608}
2609
2610\f
2611
dce234bc
PP
2612/* Build a partial symbol table. */
2613
2614void
f29dff0a 2615dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2616{
f29dff0a 2617 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2618 {
2619 init_psymbol_list (objfile, 1024);
2620 }
2621
d146bf1e 2622 dwarf2_build_psymtabs_hard (objfile);
c906108c 2623}
c906108c 2624
45452591
DE
2625/* Return TRUE if OFFSET is within CU_HEADER. */
2626
2627static inline int
2628offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2629{
2630 unsigned int bottom = cu_header->offset;
2631 unsigned int top = (cu_header->offset
2632 + cu_header->length
2633 + cu_header->initial_length_size);
9a619af0 2634
45452591
DE
2635 return (offset >= bottom && offset < top);
2636}
2637
93311388
DE
2638/* Read in the comp unit header information from the debug_info at info_ptr.
2639 NOTE: This leaves members offset, first_die_offset to be filled in
2640 by the caller. */
107d2387 2641
fe1b8b76 2642static gdb_byte *
107d2387 2643read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2644 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2645{
2646 int signed_addr;
891d2f0b 2647 unsigned int bytes_read;
c764a876
DE
2648
2649 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2650 cu_header->initial_length_size = bytes_read;
2651 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2652 info_ptr += bytes_read;
107d2387
AC
2653 cu_header->version = read_2_bytes (abfd, info_ptr);
2654 info_ptr += 2;
613e1657 2655 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2656 &bytes_read);
613e1657 2657 info_ptr += bytes_read;
107d2387
AC
2658 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2659 info_ptr += 1;
2660 signed_addr = bfd_get_sign_extend_vma (abfd);
2661 if (signed_addr < 0)
8e65ff28 2662 internal_error (__FILE__, __LINE__,
e2e0b3e5 2663 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2664 cu_header->signed_addr_p = signed_addr;
c764a876 2665
107d2387
AC
2666 return info_ptr;
2667}
2668
fe1b8b76
JB
2669static gdb_byte *
2670partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2671 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2672 bfd *abfd)
2673{
fe1b8b76 2674 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2675
2676 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2677
2dc7f7b3 2678 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2679 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2680 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2681 bfd_get_filename (abfd));
72bf9492 2682
dce234bc 2683 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2684 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2685 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2686 (long) header->abbrev_offset,
93311388 2687 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2688 bfd_get_filename (abfd));
2689
2690 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2691 > buffer + buffer_size)
8a3fe4f8
AC
2692 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2693 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2694 (long) header->length,
93311388 2695 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2696 bfd_get_filename (abfd));
2697
2698 return info_ptr;
2699}
2700
348e048f
DE
2701/* Read in the types comp unit header information from .debug_types entry at
2702 types_ptr. The result is a pointer to one past the end of the header. */
2703
2704static gdb_byte *
2705read_type_comp_unit_head (struct comp_unit_head *cu_header,
2706 ULONGEST *signature,
2707 gdb_byte *types_ptr, bfd *abfd)
2708{
348e048f
DE
2709 gdb_byte *initial_types_ptr = types_ptr;
2710
6e70227d 2711 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2712 &dwarf2_per_objfile->types);
348e048f
DE
2713 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2714
2715 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2716
2717 *signature = read_8_bytes (abfd, types_ptr);
2718 types_ptr += 8;
2719 types_ptr += cu_header->offset_size;
2720 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2721
2722 return types_ptr;
2723}
2724
aaa75496
JB
2725/* Allocate a new partial symtab for file named NAME and mark this new
2726 partial symtab as being an include of PST. */
2727
2728static void
2729dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2730 struct objfile *objfile)
2731{
2732 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2733
2734 subpst->section_offsets = pst->section_offsets;
2735 subpst->textlow = 0;
2736 subpst->texthigh = 0;
2737
2738 subpst->dependencies = (struct partial_symtab **)
2739 obstack_alloc (&objfile->objfile_obstack,
2740 sizeof (struct partial_symtab *));
2741 subpst->dependencies[0] = pst;
2742 subpst->number_of_dependencies = 1;
2743
2744 subpst->globals_offset = 0;
2745 subpst->n_global_syms = 0;
2746 subpst->statics_offset = 0;
2747 subpst->n_static_syms = 0;
2748 subpst->symtab = NULL;
2749 subpst->read_symtab = pst->read_symtab;
2750 subpst->readin = 0;
2751
2752 /* No private part is necessary for include psymtabs. This property
2753 can be used to differentiate between such include psymtabs and
10b3939b 2754 the regular ones. */
58a9656e 2755 subpst->read_symtab_private = NULL;
aaa75496
JB
2756}
2757
2758/* Read the Line Number Program data and extract the list of files
2759 included by the source file represented by PST. Build an include
d85a05f0 2760 partial symtab for each of these included files. */
aaa75496
JB
2761
2762static void
2763dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2764 struct die_info *die,
aaa75496
JB
2765 struct partial_symtab *pst)
2766{
2767 struct objfile *objfile = cu->objfile;
2768 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2769 struct line_header *lh = NULL;
2770 struct attribute *attr;
aaa75496 2771
d85a05f0
DJ
2772 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2773 if (attr)
2774 {
2775 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2776
d85a05f0
DJ
2777 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2778 }
aaa75496
JB
2779 if (lh == NULL)
2780 return; /* No linetable, so no includes. */
2781
c6da4cef
DE
2782 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2783 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2784
2785 free_line_header (lh);
2786}
2787
348e048f
DE
2788static hashval_t
2789hash_type_signature (const void *item)
2790{
2791 const struct signatured_type *type_sig = item;
9a619af0 2792
348e048f
DE
2793 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2794 return type_sig->signature;
2795}
2796
2797static int
2798eq_type_signature (const void *item_lhs, const void *item_rhs)
2799{
2800 const struct signatured_type *lhs = item_lhs;
2801 const struct signatured_type *rhs = item_rhs;
9a619af0 2802
348e048f
DE
2803 return lhs->signature == rhs->signature;
2804}
2805
1fd400ff
TT
2806/* Allocate a hash table for signatured types. */
2807
2808static htab_t
673bfd45 2809allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2810{
2811 return htab_create_alloc_ex (41,
2812 hash_type_signature,
2813 eq_type_signature,
2814 NULL,
2815 &objfile->objfile_obstack,
2816 hashtab_obstack_allocate,
2817 dummy_obstack_deallocate);
2818}
2819
2820/* A helper function to add a signatured type CU to a list. */
2821
2822static int
2823add_signatured_type_cu_to_list (void **slot, void *datum)
2824{
2825 struct signatured_type *sigt = *slot;
2826 struct dwarf2_per_cu_data ***datap = datum;
2827
2828 **datap = &sigt->per_cu;
2829 ++*datap;
2830
2831 return 1;
2832}
2833
348e048f
DE
2834/* Create the hash table of all entries in the .debug_types section.
2835 The result is zero if there is an error (e.g. missing .debug_types section),
2836 otherwise non-zero. */
2837
2838static int
2839create_debug_types_hash_table (struct objfile *objfile)
2840{
be391dca 2841 gdb_byte *info_ptr;
348e048f 2842 htab_t types_htab;
1fd400ff 2843 struct dwarf2_per_cu_data **iter;
348e048f 2844
be391dca
TT
2845 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2846 info_ptr = dwarf2_per_objfile->types.buffer;
2847
348e048f
DE
2848 if (info_ptr == NULL)
2849 {
2850 dwarf2_per_objfile->signatured_types = NULL;
2851 return 0;
2852 }
2853
673bfd45 2854 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2855
2856 if (dwarf2_die_debug)
2857 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2858
2859 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2860 {
2861 unsigned int offset;
2862 unsigned int offset_size;
2863 unsigned int type_offset;
2864 unsigned int length, initial_length_size;
2865 unsigned short version;
2866 ULONGEST signature;
2867 struct signatured_type *type_sig;
2868 void **slot;
2869 gdb_byte *ptr = info_ptr;
2870
2871 offset = ptr - dwarf2_per_objfile->types.buffer;
2872
2873 /* We need to read the type's signature in order to build the hash
2874 table, but we don't need to read anything else just yet. */
2875
2876 /* Sanity check to ensure entire cu is present. */
2877 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2878 if (ptr + length + initial_length_size
2879 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2880 {
2881 complaint (&symfile_complaints,
2882 _("debug type entry runs off end of `.debug_types' section, ignored"));
2883 break;
2884 }
2885
2886 offset_size = initial_length_size == 4 ? 4 : 8;
2887 ptr += initial_length_size;
2888 version = bfd_get_16 (objfile->obfd, ptr);
2889 ptr += 2;
2890 ptr += offset_size; /* abbrev offset */
2891 ptr += 1; /* address size */
2892 signature = bfd_get_64 (objfile->obfd, ptr);
2893 ptr += 8;
2894 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2895
2896 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2897 memset (type_sig, 0, sizeof (*type_sig));
2898 type_sig->signature = signature;
2899 type_sig->offset = offset;
2900 type_sig->type_offset = type_offset;
ca1f3406 2901 type_sig->per_cu.objfile = objfile;
1fd400ff 2902 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2903
2904 slot = htab_find_slot (types_htab, type_sig, INSERT);
2905 gdb_assert (slot != NULL);
2906 *slot = type_sig;
2907
2908 if (dwarf2_die_debug)
2909 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2910 offset, phex (signature, sizeof (signature)));
2911
2912 info_ptr = info_ptr + initial_length_size + length;
2913 }
2914
2915 dwarf2_per_objfile->signatured_types = types_htab;
2916
1fd400ff
TT
2917 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2918 dwarf2_per_objfile->type_comp_units
2919 = obstack_alloc (&objfile->objfile_obstack,
2920 dwarf2_per_objfile->n_type_comp_units
2921 * sizeof (struct dwarf2_per_cu_data *));
2922 iter = &dwarf2_per_objfile->type_comp_units[0];
2923 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2924 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2925 == dwarf2_per_objfile->n_type_comp_units);
2926
348e048f
DE
2927 return 1;
2928}
2929
2930/* Lookup a signature based type.
2931 Returns NULL if SIG is not present in the table. */
2932
2933static struct signatured_type *
2934lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2935{
2936 struct signatured_type find_entry, *entry;
2937
2938 if (dwarf2_per_objfile->signatured_types == NULL)
2939 {
2940 complaint (&symfile_complaints,
2941 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2942 return 0;
2943 }
2944
2945 find_entry.signature = sig;
2946 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2947 return entry;
2948}
2949
d85a05f0
DJ
2950/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2951
2952static void
2953init_cu_die_reader (struct die_reader_specs *reader,
2954 struct dwarf2_cu *cu)
2955{
2956 reader->abfd = cu->objfile->obfd;
2957 reader->cu = cu;
2958 if (cu->per_cu->from_debug_types)
be391dca
TT
2959 {
2960 gdb_assert (dwarf2_per_objfile->types.readin);
2961 reader->buffer = dwarf2_per_objfile->types.buffer;
2962 }
d85a05f0 2963 else
be391dca
TT
2964 {
2965 gdb_assert (dwarf2_per_objfile->info.readin);
2966 reader->buffer = dwarf2_per_objfile->info.buffer;
2967 }
d85a05f0
DJ
2968}
2969
2970/* Find the base address of the compilation unit for range lists and
2971 location lists. It will normally be specified by DW_AT_low_pc.
2972 In DWARF-3 draft 4, the base address could be overridden by
2973 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2974 compilation units with discontinuous ranges. */
2975
2976static void
2977dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2978{
2979 struct attribute *attr;
2980
2981 cu->base_known = 0;
2982 cu->base_address = 0;
2983
2984 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2985 if (attr)
2986 {
2987 cu->base_address = DW_ADDR (attr);
2988 cu->base_known = 1;
2989 }
2990 else
2991 {
2992 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2993 if (attr)
2994 {
2995 cu->base_address = DW_ADDR (attr);
2996 cu->base_known = 1;
2997 }
2998 }
2999}
3000
348e048f
DE
3001/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3002 to combine the common parts.
93311388 3003 Process a compilation unit for a psymtab.
348e048f
DE
3004 BUFFER is a pointer to the beginning of the dwarf section buffer,
3005 either .debug_info or debug_types.
93311388
DE
3006 INFO_PTR is a pointer to the start of the CU.
3007 Returns a pointer to the next CU. */
aaa75496 3008
93311388
DE
3009static gdb_byte *
3010process_psymtab_comp_unit (struct objfile *objfile,
3011 struct dwarf2_per_cu_data *this_cu,
3012 gdb_byte *buffer, gdb_byte *info_ptr,
3013 unsigned int buffer_size)
c906108c 3014{
c906108c 3015 bfd *abfd = objfile->obfd;
93311388 3016 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3017 struct die_info *comp_unit_die;
c906108c 3018 struct partial_symtab *pst;
5734ee8b 3019 CORE_ADDR baseaddr;
93311388
DE
3020 struct cleanup *back_to_inner;
3021 struct dwarf2_cu cu;
d85a05f0
DJ
3022 int has_children, has_pc_info;
3023 struct attribute *attr;
d85a05f0
DJ
3024 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3025 struct die_reader_specs reader_specs;
c906108c 3026
93311388
DE
3027 memset (&cu, 0, sizeof (cu));
3028 cu.objfile = objfile;
3029 obstack_init (&cu.comp_unit_obstack);
c906108c 3030
93311388 3031 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3032
93311388
DE
3033 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3034 buffer, buffer_size,
3035 abfd);
10b3939b 3036
93311388
DE
3037 /* Complete the cu_header. */
3038 cu.header.offset = beg_of_comp_unit - buffer;
3039 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3040
93311388 3041 cu.list_in_scope = &file_symbols;
af703f96 3042
328c9494
DJ
3043 /* If this compilation unit was already read in, free the
3044 cached copy in order to read it in again. This is
3045 necessary because we skipped some symbols when we first
3046 read in the compilation unit (see load_partial_dies).
3047 This problem could be avoided, but the benefit is
3048 unclear. */
3049 if (this_cu->cu != NULL)
3050 free_one_cached_comp_unit (this_cu->cu);
3051
3052 /* Note that this is a pointer to our stack frame, being
3053 added to a global data structure. It will be cleaned up
3054 in free_stack_comp_unit when we finish with this
3055 compilation unit. */
3056 this_cu->cu = &cu;
d85a05f0
DJ
3057 cu.per_cu = this_cu;
3058
93311388
DE
3059 /* Read the abbrevs for this compilation unit into a table. */
3060 dwarf2_read_abbrevs (abfd, &cu);
3061 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3062
93311388 3063 /* Read the compilation unit die. */
348e048f
DE
3064 if (this_cu->from_debug_types)
3065 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3066 init_cu_die_reader (&reader_specs, &cu);
3067 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3068 &has_children);
93311388 3069
348e048f
DE
3070 if (this_cu->from_debug_types)
3071 {
3072 /* offset,length haven't been set yet for type units. */
3073 this_cu->offset = cu.header.offset;
3074 this_cu->length = cu.header.length + cu.header.initial_length_size;
3075 }
d85a05f0 3076 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3077 {
93311388
DE
3078 info_ptr = (beg_of_comp_unit + cu.header.length
3079 + cu.header.initial_length_size);
3080 do_cleanups (back_to_inner);
3081 return info_ptr;
3082 }
72bf9492 3083
93311388 3084 /* Set the language we're debugging. */
d85a05f0
DJ
3085 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3086 if (attr)
3087 set_cu_language (DW_UNSND (attr), &cu);
3088 else
3089 set_cu_language (language_minimal, &cu);
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 struct attribute *attr;
3305 int has_children;
3306 struct die_reader_specs reader_specs;
98bfdba5 3307 int read_cu = 0;
ae038cb0 3308
348e048f
DE
3309 gdb_assert (! this_cu->from_debug_types);
3310
be391dca 3311 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3312 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3313 beg_of_comp_unit = info_ptr;
3314
98bfdba5
PA
3315 if (this_cu->cu == NULL)
3316 {
3317 cu = alloc_one_comp_unit (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
DJ
3356
3357 /* Set the language we're debugging. */
d85a05f0
DJ
3358 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3359 if (attr)
3360 set_cu_language (DW_UNSND (attr), cu);
3361 else
3362 set_cu_language (language_minimal, cu);
ae038cb0 3363
ae038cb0
DJ
3364 /* Check if comp unit has_children.
3365 If so, read the rest of the partial symbols from this comp unit.
3366 If not, there's no more debug_info for this comp unit. */
d85a05f0 3367 if (has_children)
93311388 3368 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3369
98bfdba5
PA
3370 do_cleanups (free_abbrevs_cleanup);
3371
3372 if (read_cu)
3373 {
3374 /* We've successfully allocated this compilation unit. Let our
3375 caller clean it up when finished with it. */
3376 discard_cleanups (free_cu_cleanup);
3377 }
ae038cb0
DJ
3378}
3379
3380/* Create a list of all compilation units in OBJFILE. We do this only
3381 if an inter-comp-unit reference is found; presumably if there is one,
3382 there will be many, and one will occur early in the .debug_info section.
3383 So there's no point in building this list incrementally. */
3384
3385static void
3386create_all_comp_units (struct objfile *objfile)
3387{
3388 int n_allocated;
3389 int n_comp_units;
3390 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3391 gdb_byte *info_ptr;
3392
3393 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3394 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3395
3396 n_comp_units = 0;
3397 n_allocated = 10;
3398 all_comp_units = xmalloc (n_allocated
3399 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3400
dce234bc 3401 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3402 {
c764a876 3403 unsigned int length, initial_length_size;
ae038cb0 3404 struct dwarf2_per_cu_data *this_cu;
c764a876 3405 unsigned int offset;
ae038cb0 3406
dce234bc 3407 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3408
3409 /* Read just enough information to find out where the next
3410 compilation unit is. */
c764a876
DE
3411 length = read_initial_length (objfile->obfd, info_ptr,
3412 &initial_length_size);
ae038cb0
DJ
3413
3414 /* Save the compilation unit for later lookup. */
3415 this_cu = obstack_alloc (&objfile->objfile_obstack,
3416 sizeof (struct dwarf2_per_cu_data));
3417 memset (this_cu, 0, sizeof (*this_cu));
3418 this_cu->offset = offset;
c764a876 3419 this_cu->length = length + initial_length_size;
9291a0cd 3420 this_cu->objfile = objfile;
ae038cb0
DJ
3421
3422 if (n_comp_units == n_allocated)
3423 {
3424 n_allocated *= 2;
3425 all_comp_units = xrealloc (all_comp_units,
3426 n_allocated
3427 * sizeof (struct dwarf2_per_cu_data *));
3428 }
3429 all_comp_units[n_comp_units++] = this_cu;
3430
3431 info_ptr = info_ptr + this_cu->length;
3432 }
3433
3434 dwarf2_per_objfile->all_comp_units
3435 = obstack_alloc (&objfile->objfile_obstack,
3436 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3437 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3438 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3439 xfree (all_comp_units);
3440 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3441}
3442
5734ee8b
DJ
3443/* Process all loaded DIEs for compilation unit CU, starting at
3444 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3445 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3446 DW_AT_ranges). If NEED_PC is set, then this function will set
3447 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3448 and record the covered ranges in the addrmap. */
c906108c 3449
72bf9492
DJ
3450static void
3451scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3452 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3453{
72bf9492 3454 struct partial_die_info *pdi;
c906108c 3455
91c24f0a
DC
3456 /* Now, march along the PDI's, descending into ones which have
3457 interesting children but skipping the children of the other ones,
3458 until we reach the end of the compilation unit. */
c906108c 3459
72bf9492 3460 pdi = first_die;
91c24f0a 3461
72bf9492
DJ
3462 while (pdi != NULL)
3463 {
3464 fixup_partial_die (pdi, cu);
c906108c 3465
f55ee35c 3466 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3467 children, so we need to look at them. Ditto for anonymous
3468 enums. */
933c6fe4 3469
72bf9492 3470 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3471 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3472 {
72bf9492 3473 switch (pdi->tag)
c906108c
SS
3474 {
3475 case DW_TAG_subprogram:
5734ee8b 3476 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3477 break;
72929c62 3478 case DW_TAG_constant:
c906108c
SS
3479 case DW_TAG_variable:
3480 case DW_TAG_typedef:
91c24f0a 3481 case DW_TAG_union_type:
72bf9492 3482 if (!pdi->is_declaration)
63d06c5c 3483 {
72bf9492 3484 add_partial_symbol (pdi, cu);
63d06c5c
DC
3485 }
3486 break;
c906108c 3487 case DW_TAG_class_type:
680b30c7 3488 case DW_TAG_interface_type:
c906108c 3489 case DW_TAG_structure_type:
72bf9492 3490 if (!pdi->is_declaration)
c906108c 3491 {
72bf9492 3492 add_partial_symbol (pdi, cu);
c906108c
SS
3493 }
3494 break;
91c24f0a 3495 case DW_TAG_enumeration_type:
72bf9492
DJ
3496 if (!pdi->is_declaration)
3497 add_partial_enumeration (pdi, cu);
c906108c
SS
3498 break;
3499 case DW_TAG_base_type:
a02abb62 3500 case DW_TAG_subrange_type:
c906108c 3501 /* File scope base type definitions are added to the partial
c5aa993b 3502 symbol table. */
72bf9492 3503 add_partial_symbol (pdi, cu);
c906108c 3504 break;
d9fa45fe 3505 case DW_TAG_namespace:
5734ee8b 3506 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3507 break;
5d7cb8df
JK
3508 case DW_TAG_module:
3509 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3510 break;
c906108c
SS
3511 default:
3512 break;
3513 }
3514 }
3515
72bf9492
DJ
3516 /* If the die has a sibling, skip to the sibling. */
3517
3518 pdi = pdi->die_sibling;
3519 }
3520}
3521
3522/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3523
72bf9492 3524 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3525 name is concatenated with "::" and the partial DIE's name. For
3526 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3527 Enumerators are an exception; they use the scope of their parent
3528 enumeration type, i.e. the name of the enumeration type is not
3529 prepended to the enumerator.
91c24f0a 3530
72bf9492
DJ
3531 There are two complexities. One is DW_AT_specification; in this
3532 case "parent" means the parent of the target of the specification,
3533 instead of the direct parent of the DIE. The other is compilers
3534 which do not emit DW_TAG_namespace; in this case we try to guess
3535 the fully qualified name of structure types from their members'
3536 linkage names. This must be done using the DIE's children rather
3537 than the children of any DW_AT_specification target. We only need
3538 to do this for structures at the top level, i.e. if the target of
3539 any DW_AT_specification (if any; otherwise the DIE itself) does not
3540 have a parent. */
3541
3542/* Compute the scope prefix associated with PDI's parent, in
3543 compilation unit CU. The result will be allocated on CU's
3544 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3545 field. NULL is returned if no prefix is necessary. */
3546static char *
3547partial_die_parent_scope (struct partial_die_info *pdi,
3548 struct dwarf2_cu *cu)
3549{
3550 char *grandparent_scope;
3551 struct partial_die_info *parent, *real_pdi;
91c24f0a 3552
72bf9492
DJ
3553 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3554 then this means the parent of the specification DIE. */
3555
3556 real_pdi = pdi;
72bf9492 3557 while (real_pdi->has_specification)
10b3939b 3558 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3559
3560 parent = real_pdi->die_parent;
3561 if (parent == NULL)
3562 return NULL;
3563
3564 if (parent->scope_set)
3565 return parent->scope;
3566
3567 fixup_partial_die (parent, cu);
3568
10b3939b 3569 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3570
acebe513
UW
3571 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3572 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3573 Work around this problem here. */
3574 if (cu->language == language_cplus
6e70227d 3575 && parent->tag == DW_TAG_namespace
acebe513
UW
3576 && strcmp (parent->name, "::") == 0
3577 && grandparent_scope == NULL)
3578 {
3579 parent->scope = NULL;
3580 parent->scope_set = 1;
3581 return NULL;
3582 }
3583
72bf9492 3584 if (parent->tag == DW_TAG_namespace
f55ee35c 3585 || parent->tag == DW_TAG_module
72bf9492
DJ
3586 || parent->tag == DW_TAG_structure_type
3587 || parent->tag == DW_TAG_class_type
680b30c7 3588 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3589 || parent->tag == DW_TAG_union_type
3590 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3591 {
3592 if (grandparent_scope == NULL)
3593 parent->scope = parent->name;
3594 else
987504bb 3595 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3596 parent->name, 0, cu);
72bf9492 3597 }
ceeb3d5a 3598 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3599 /* Enumerators should not get the name of the enumeration as a prefix. */
3600 parent->scope = grandparent_scope;
3601 else
3602 {
3603 /* FIXME drow/2004-04-01: What should we be doing with
3604 function-local names? For partial symbols, we should probably be
3605 ignoring them. */
3606 complaint (&symfile_complaints,
e2e0b3e5 3607 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3608 parent->tag, pdi->offset);
3609 parent->scope = grandparent_scope;
c906108c
SS
3610 }
3611
72bf9492
DJ
3612 parent->scope_set = 1;
3613 return parent->scope;
3614}
3615
3616/* Return the fully scoped name associated with PDI, from compilation unit
3617 CU. The result will be allocated with malloc. */
3618static char *
3619partial_die_full_name (struct partial_die_info *pdi,
3620 struct dwarf2_cu *cu)
3621{
3622 char *parent_scope;
3623
98bfdba5
PA
3624 /* If this is a template instantiation, we can not work out the
3625 template arguments from partial DIEs. So, unfortunately, we have
3626 to go through the full DIEs. At least any work we do building
3627 types here will be reused if full symbols are loaded later. */
3628 if (pdi->has_template_arguments)
3629 {
3630 fixup_partial_die (pdi, cu);
3631
3632 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3633 {
3634 struct die_info *die;
3635 struct attribute attr;
3636 struct dwarf2_cu *ref_cu = cu;
3637
3638 attr.name = 0;
3639 attr.form = DW_FORM_ref_addr;
3640 attr.u.addr = pdi->offset;
3641 die = follow_die_ref (NULL, &attr, &ref_cu);
3642
3643 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3644 }
3645 }
3646
72bf9492
DJ
3647 parent_scope = partial_die_parent_scope (pdi, cu);
3648 if (parent_scope == NULL)
3649 return NULL;
3650 else
f55ee35c 3651 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3652}
3653
3654static void
72bf9492 3655add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3656{
e7c27a73 3657 struct objfile *objfile = cu->objfile;
c906108c 3658 CORE_ADDR addr = 0;
decbce07 3659 char *actual_name = NULL;
5c4e30ca 3660 const struct partial_symbol *psym = NULL;
e142c38c 3661 CORE_ADDR baseaddr;
72bf9492 3662 int built_actual_name = 0;
e142c38c
DJ
3663
3664 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3665
94af9270
KS
3666 actual_name = partial_die_full_name (pdi, cu);
3667 if (actual_name)
3668 built_actual_name = 1;
63d06c5c 3669
72bf9492
DJ
3670 if (actual_name == NULL)
3671 actual_name = pdi->name;
3672
c906108c
SS
3673 switch (pdi->tag)
3674 {
3675 case DW_TAG_subprogram:
2cfa0c8d 3676 if (pdi->is_external || cu->language == language_ada)
c906108c 3677 {
2cfa0c8d
JB
3678 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3679 of the global scope. But in Ada, we want to be able to access
3680 nested procedures globally. So all Ada subprograms are stored
3681 in the global scope. */
38d518c9 3682 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3683 mst_text, objfile); */
38d518c9 3684 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3685 built_actual_name,
5c4e30ca
DC
3686 VAR_DOMAIN, LOC_BLOCK,
3687 &objfile->global_psymbols,
3688 0, pdi->lowpc + baseaddr,
e142c38c 3689 cu->language, objfile);
c906108c
SS
3690 }
3691 else
3692 {
38d518c9 3693 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3694 mst_file_text, objfile); */
38d518c9 3695 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3696 built_actual_name,
5c4e30ca
DC
3697 VAR_DOMAIN, LOC_BLOCK,
3698 &objfile->static_psymbols,
3699 0, pdi->lowpc + baseaddr,
e142c38c 3700 cu->language, objfile);
c906108c
SS
3701 }
3702 break;
72929c62
JB
3703 case DW_TAG_constant:
3704 {
3705 struct psymbol_allocation_list *list;
3706
3707 if (pdi->is_external)
3708 list = &objfile->global_psymbols;
3709 else
3710 list = &objfile->static_psymbols;
3711 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3712 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3713 list, 0, 0, cu->language, objfile);
3714
3715 }
3716 break;
c906108c 3717 case DW_TAG_variable:
caac4577
JG
3718 if (pdi->locdesc)
3719 addr = decode_locdesc (pdi->locdesc, cu);
3720
3721 if (pdi->locdesc
3722 && addr == 0
3723 && !dwarf2_per_objfile->has_section_at_zero)
3724 {
3725 /* A global or static variable may also have been stripped
3726 out by the linker if unused, in which case its address
3727 will be nullified; do not add such variables into partial
3728 symbol table then. */
3729 }
3730 else if (pdi->is_external)
c906108c
SS
3731 {
3732 /* Global Variable.
3733 Don't enter into the minimal symbol tables as there is
3734 a minimal symbol table entry from the ELF symbols already.
3735 Enter into partial symbol table if it has a location
3736 descriptor or a type.
3737 If the location descriptor is missing, new_symbol will create
3738 a LOC_UNRESOLVED symbol, the address of the variable will then
3739 be determined from the minimal symbol table whenever the variable
3740 is referenced.
3741 The address for the partial symbol table entry is not
3742 used by GDB, but it comes in handy for debugging partial symbol
3743 table building. */
3744
c906108c 3745 if (pdi->locdesc || pdi->has_type)
38d518c9 3746 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3747 built_actual_name,
5c4e30ca
DC
3748 VAR_DOMAIN, LOC_STATIC,
3749 &objfile->global_psymbols,
3750 0, addr + baseaddr,
e142c38c 3751 cu->language, objfile);
c906108c
SS
3752 }
3753 else
3754 {
3755 /* Static Variable. Skip symbols without location descriptors. */
3756 if (pdi->locdesc == NULL)
decbce07
MS
3757 {
3758 if (built_actual_name)
3759 xfree (actual_name);
3760 return;
3761 }
38d518c9 3762 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3763 mst_file_data, objfile); */
38d518c9 3764 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3765 built_actual_name,
5c4e30ca
DC
3766 VAR_DOMAIN, LOC_STATIC,
3767 &objfile->static_psymbols,
3768 0, addr + baseaddr,
e142c38c 3769 cu->language, objfile);
c906108c
SS
3770 }
3771 break;
3772 case DW_TAG_typedef:
3773 case DW_TAG_base_type:
a02abb62 3774 case DW_TAG_subrange_type:
38d518c9 3775 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3776 built_actual_name,
176620f1 3777 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3778 &objfile->static_psymbols,
e142c38c 3779 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3780 break;
72bf9492
DJ
3781 case DW_TAG_namespace:
3782 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3783 built_actual_name,
72bf9492
DJ
3784 VAR_DOMAIN, LOC_TYPEDEF,
3785 &objfile->global_psymbols,
3786 0, (CORE_ADDR) 0, cu->language, objfile);
3787 break;
c906108c 3788 case DW_TAG_class_type:
680b30c7 3789 case DW_TAG_interface_type:
c906108c
SS
3790 case DW_TAG_structure_type:
3791 case DW_TAG_union_type:
3792 case DW_TAG_enumeration_type:
fa4028e9
JB
3793 /* Skip external references. The DWARF standard says in the section
3794 about "Structure, Union, and Class Type Entries": "An incomplete
3795 structure, union or class type is represented by a structure,
3796 union or class entry that does not have a byte size attribute
3797 and that has a DW_AT_declaration attribute." */
3798 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3799 {
3800 if (built_actual_name)
3801 xfree (actual_name);
3802 return;
3803 }
fa4028e9 3804
63d06c5c
DC
3805 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3806 static vs. global. */
38d518c9 3807 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3808 built_actual_name,
176620f1 3809 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3810 (cu->language == language_cplus
3811 || cu->language == language_java)
63d06c5c
DC
3812 ? &objfile->global_psymbols
3813 : &objfile->static_psymbols,
e142c38c 3814 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3815
c906108c
SS
3816 break;
3817 case DW_TAG_enumerator:
38d518c9 3818 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3819 built_actual_name,
176620f1 3820 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3821 (cu->language == language_cplus
3822 || cu->language == language_java)
f6fe98ef
DJ
3823 ? &objfile->global_psymbols
3824 : &objfile->static_psymbols,
e142c38c 3825 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3826 break;
3827 default:
3828 break;
3829 }
5c4e30ca 3830
72bf9492
DJ
3831 if (built_actual_name)
3832 xfree (actual_name);
c906108c
SS
3833}
3834
5c4e30ca
DC
3835/* Read a partial die corresponding to a namespace; also, add a symbol
3836 corresponding to that namespace to the symbol table. NAMESPACE is
3837 the name of the enclosing namespace. */
91c24f0a 3838
72bf9492
DJ
3839static void
3840add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3841 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3842 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3843{
72bf9492 3844 /* Add a symbol for the namespace. */
e7c27a73 3845
72bf9492 3846 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3847
3848 /* Now scan partial symbols in that namespace. */
3849
91c24f0a 3850 if (pdi->has_children)
5734ee8b 3851 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3852}
3853
5d7cb8df
JK
3854/* Read a partial die corresponding to a Fortran module. */
3855
3856static void
3857add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3858 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3859{
f55ee35c 3860 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3861
3862 if (pdi->has_children)
3863 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3864}
3865
bc30ff58
JB
3866/* Read a partial die corresponding to a subprogram and create a partial
3867 symbol for that subprogram. When the CU language allows it, this
3868 routine also defines a partial symbol for each nested subprogram
3869 that this subprogram contains.
6e70227d 3870
bc30ff58
JB
3871 DIE my also be a lexical block, in which case we simply search
3872 recursively for suprograms defined inside that lexical block.
3873 Again, this is only performed when the CU language allows this
3874 type of definitions. */
3875
3876static void
3877add_partial_subprogram (struct partial_die_info *pdi,
3878 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3879 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3880{
3881 if (pdi->tag == DW_TAG_subprogram)
3882 {
3883 if (pdi->has_pc_info)
3884 {
3885 if (pdi->lowpc < *lowpc)
3886 *lowpc = pdi->lowpc;
3887 if (pdi->highpc > *highpc)
3888 *highpc = pdi->highpc;
5734ee8b
DJ
3889 if (need_pc)
3890 {
3891 CORE_ADDR baseaddr;
3892 struct objfile *objfile = cu->objfile;
3893
3894 baseaddr = ANOFFSET (objfile->section_offsets,
3895 SECT_OFF_TEXT (objfile));
3896 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3897 pdi->lowpc + baseaddr,
3898 pdi->highpc - 1 + baseaddr,
9291a0cd 3899 cu->per_cu->v.psymtab);
5734ee8b 3900 }
bc30ff58 3901 if (!pdi->is_declaration)
e8d05480
JB
3902 /* Ignore subprogram DIEs that do not have a name, they are
3903 illegal. Do not emit a complaint at this point, we will
3904 do so when we convert this psymtab into a symtab. */
3905 if (pdi->name)
3906 add_partial_symbol (pdi, cu);
bc30ff58
JB
3907 }
3908 }
6e70227d 3909
bc30ff58
JB
3910 if (! pdi->has_children)
3911 return;
3912
3913 if (cu->language == language_ada)
3914 {
3915 pdi = pdi->die_child;
3916 while (pdi != NULL)
3917 {
3918 fixup_partial_die (pdi, cu);
3919 if (pdi->tag == DW_TAG_subprogram
3920 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3921 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3922 pdi = pdi->die_sibling;
3923 }
3924 }
3925}
3926
91c24f0a
DC
3927/* Read a partial die corresponding to an enumeration type. */
3928
72bf9492
DJ
3929static void
3930add_partial_enumeration (struct partial_die_info *enum_pdi,
3931 struct dwarf2_cu *cu)
91c24f0a 3932{
72bf9492 3933 struct partial_die_info *pdi;
91c24f0a
DC
3934
3935 if (enum_pdi->name != NULL)
72bf9492
DJ
3936 add_partial_symbol (enum_pdi, cu);
3937
3938 pdi = enum_pdi->die_child;
3939 while (pdi)
91c24f0a 3940 {
72bf9492 3941 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3942 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3943 else
72bf9492
DJ
3944 add_partial_symbol (pdi, cu);
3945 pdi = pdi->die_sibling;
91c24f0a 3946 }
91c24f0a
DC
3947}
3948
4bb7a0a7
DJ
3949/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3950 Return the corresponding abbrev, or NULL if the number is zero (indicating
3951 an empty DIE). In either case *BYTES_READ will be set to the length of
3952 the initial number. */
3953
3954static struct abbrev_info *
fe1b8b76 3955peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3956 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3957{
3958 bfd *abfd = cu->objfile->obfd;
3959 unsigned int abbrev_number;
3960 struct abbrev_info *abbrev;
3961
3962 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3963
3964 if (abbrev_number == 0)
3965 return NULL;
3966
3967 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3968 if (!abbrev)
3969 {
8a3fe4f8 3970 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3971 bfd_get_filename (abfd));
3972 }
3973
3974 return abbrev;
3975}
3976
93311388
DE
3977/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3978 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3979 DIE. Any children of the skipped DIEs will also be skipped. */
3980
fe1b8b76 3981static gdb_byte *
93311388 3982skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3983{
3984 struct abbrev_info *abbrev;
3985 unsigned int bytes_read;
3986
3987 while (1)
3988 {
3989 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3990 if (abbrev == NULL)
3991 return info_ptr + bytes_read;
3992 else
93311388 3993 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3994 }
3995}
3996
93311388
DE
3997/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3998 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3999 abbrev corresponding to that skipped uleb128 should be passed in
4000 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4001 children. */
4002
fe1b8b76 4003static gdb_byte *
93311388
DE
4004skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4005 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4006{
4007 unsigned int bytes_read;
4008 struct attribute attr;
4009 bfd *abfd = cu->objfile->obfd;
4010 unsigned int form, i;
4011
4012 for (i = 0; i < abbrev->num_attrs; i++)
4013 {
4014 /* The only abbrev we care about is DW_AT_sibling. */
4015 if (abbrev->attrs[i].name == DW_AT_sibling)
4016 {
4017 read_attribute (&attr, &abbrev->attrs[i],
4018 abfd, info_ptr, cu);
4019 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4020 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4021 else
93311388 4022 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4023 }
4024
4025 /* If it isn't DW_AT_sibling, skip this attribute. */
4026 form = abbrev->attrs[i].form;
4027 skip_attribute:
4028 switch (form)
4029 {
4bb7a0a7 4030 case DW_FORM_ref_addr:
ae411497
TT
4031 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4032 and later it is offset sized. */
4033 if (cu->header.version == 2)
4034 info_ptr += cu->header.addr_size;
4035 else
4036 info_ptr += cu->header.offset_size;
4037 break;
4038 case DW_FORM_addr:
4bb7a0a7
DJ
4039 info_ptr += cu->header.addr_size;
4040 break;
4041 case DW_FORM_data1:
4042 case DW_FORM_ref1:
4043 case DW_FORM_flag:
4044 info_ptr += 1;
4045 break;
2dc7f7b3
TT
4046 case DW_FORM_flag_present:
4047 break;
4bb7a0a7
DJ
4048 case DW_FORM_data2:
4049 case DW_FORM_ref2:
4050 info_ptr += 2;
4051 break;
4052 case DW_FORM_data4:
4053 case DW_FORM_ref4:
4054 info_ptr += 4;
4055 break;
4056 case DW_FORM_data8:
4057 case DW_FORM_ref8:
348e048f 4058 case DW_FORM_sig8:
4bb7a0a7
DJ
4059 info_ptr += 8;
4060 break;
4061 case DW_FORM_string:
9b1c24c8 4062 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4063 info_ptr += bytes_read;
4064 break;
2dc7f7b3 4065 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4066 case DW_FORM_strp:
4067 info_ptr += cu->header.offset_size;
4068 break;
2dc7f7b3 4069 case DW_FORM_exprloc:
4bb7a0a7
DJ
4070 case DW_FORM_block:
4071 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4072 info_ptr += bytes_read;
4073 break;
4074 case DW_FORM_block1:
4075 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4076 break;
4077 case DW_FORM_block2:
4078 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4079 break;
4080 case DW_FORM_block4:
4081 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4082 break;
4083 case DW_FORM_sdata:
4084 case DW_FORM_udata:
4085 case DW_FORM_ref_udata:
4086 info_ptr = skip_leb128 (abfd, info_ptr);
4087 break;
4088 case DW_FORM_indirect:
4089 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4090 info_ptr += bytes_read;
4091 /* We need to continue parsing from here, so just go back to
4092 the top. */
4093 goto skip_attribute;
4094
4095 default:
8a3fe4f8 4096 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4097 dwarf_form_name (form),
4098 bfd_get_filename (abfd));
4099 }
4100 }
4101
4102 if (abbrev->has_children)
93311388 4103 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4104 else
4105 return info_ptr;
4106}
4107
93311388
DE
4108/* Locate ORIG_PDI's sibling.
4109 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4110 in BUFFER. */
91c24f0a 4111
fe1b8b76 4112static gdb_byte *
93311388
DE
4113locate_pdi_sibling (struct partial_die_info *orig_pdi,
4114 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4115 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4116{
4117 /* Do we know the sibling already? */
72bf9492 4118
91c24f0a
DC
4119 if (orig_pdi->sibling)
4120 return orig_pdi->sibling;
4121
4122 /* Are there any children to deal with? */
4123
4124 if (!orig_pdi->has_children)
4125 return info_ptr;
4126
4bb7a0a7 4127 /* Skip the children the long way. */
91c24f0a 4128
93311388 4129 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4130}
4131
c906108c
SS
4132/* Expand this partial symbol table into a full symbol table. */
4133
4134static void
fba45db2 4135dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4136{
c906108c
SS
4137 if (pst != NULL)
4138 {
4139 if (pst->readin)
4140 {
8a3fe4f8 4141 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4142 }
4143 else
4144 {
4145 if (info_verbose)
4146 {
a3f17187 4147 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4148 gdb_flush (gdb_stdout);
4149 }
4150
10b3939b
DJ
4151 /* Restore our global data. */
4152 dwarf2_per_objfile = objfile_data (pst->objfile,
4153 dwarf2_objfile_data_key);
4154
b2ab525c
KB
4155 /* If this psymtab is constructed from a debug-only objfile, the
4156 has_section_at_zero flag will not necessarily be correct. We
4157 can get the correct value for this flag by looking at the data
4158 associated with the (presumably stripped) associated objfile. */
4159 if (pst->objfile->separate_debug_objfile_backlink)
4160 {
4161 struct dwarf2_per_objfile *dpo_backlink
4162 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4163 dwarf2_objfile_data_key);
9a619af0 4164
b2ab525c
KB
4165 dwarf2_per_objfile->has_section_at_zero
4166 = dpo_backlink->has_section_at_zero;
4167 }
4168
98bfdba5
PA
4169 dwarf2_per_objfile->reading_partial_symbols = 0;
4170
c906108c
SS
4171 psymtab_to_symtab_1 (pst);
4172
4173 /* Finish up the debug error message. */
4174 if (info_verbose)
a3f17187 4175 printf_filtered (_("done.\n"));
c906108c
SS
4176 }
4177 }
4178}
4179
10b3939b
DJ
4180/* Add PER_CU to the queue. */
4181
4182static void
03dd20cc 4183queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4184{
4185 struct dwarf2_queue_item *item;
4186
4187 per_cu->queued = 1;
4188 item = xmalloc (sizeof (*item));
4189 item->per_cu = per_cu;
4190 item->next = NULL;
4191
4192 if (dwarf2_queue == NULL)
4193 dwarf2_queue = item;
4194 else
4195 dwarf2_queue_tail->next = item;
4196
4197 dwarf2_queue_tail = item;
4198}
4199
4200/* Process the queue. */
4201
4202static void
4203process_queue (struct objfile *objfile)
4204{
4205 struct dwarf2_queue_item *item, *next_item;
4206
03dd20cc
DJ
4207 /* The queue starts out with one item, but following a DIE reference
4208 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4209 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4210 {
9291a0cd
TT
4211 if (dwarf2_per_objfile->using_index
4212 ? !item->per_cu->v.quick->symtab
4213 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4214 process_full_comp_unit (item->per_cu);
4215
4216 item->per_cu->queued = 0;
4217 next_item = item->next;
4218 xfree (item);
4219 }
4220
4221 dwarf2_queue_tail = NULL;
4222}
4223
4224/* Free all allocated queue entries. This function only releases anything if
4225 an error was thrown; if the queue was processed then it would have been
4226 freed as we went along. */
4227
4228static void
4229dwarf2_release_queue (void *dummy)
4230{
4231 struct dwarf2_queue_item *item, *last;
4232
4233 item = dwarf2_queue;
4234 while (item)
4235 {
4236 /* Anything still marked queued is likely to be in an
4237 inconsistent state, so discard it. */
4238 if (item->per_cu->queued)
4239 {
4240 if (item->per_cu->cu != NULL)
4241 free_one_cached_comp_unit (item->per_cu->cu);
4242 item->per_cu->queued = 0;
4243 }
4244
4245 last = item;
4246 item = item->next;
4247 xfree (last);
4248 }
4249
4250 dwarf2_queue = dwarf2_queue_tail = NULL;
4251}
4252
4253/* Read in full symbols for PST, and anything it depends on. */
4254
c906108c 4255static void
fba45db2 4256psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4257{
10b3939b 4258 struct dwarf2_per_cu_data *per_cu;
c906108c 4259 struct cleanup *back_to;
aaa75496
JB
4260 int i;
4261
4262 for (i = 0; i < pst->number_of_dependencies; i++)
4263 if (!pst->dependencies[i]->readin)
4264 {
4265 /* Inform about additional files that need to be read in. */
4266 if (info_verbose)
4267 {
a3f17187 4268 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4269 fputs_filtered (" ", gdb_stdout);
4270 wrap_here ("");
4271 fputs_filtered ("and ", gdb_stdout);
4272 wrap_here ("");
4273 printf_filtered ("%s...", pst->dependencies[i]->filename);
4274 wrap_here (""); /* Flush output */
4275 gdb_flush (gdb_stdout);
4276 }
4277 psymtab_to_symtab_1 (pst->dependencies[i]);
4278 }
4279
e38df1d0 4280 per_cu = pst->read_symtab_private;
10b3939b
DJ
4281
4282 if (per_cu == NULL)
aaa75496
JB
4283 {
4284 /* It's an include file, no symbols to read for it.
4285 Everything is in the parent symtab. */
4286 pst->readin = 1;
4287 return;
4288 }
c906108c 4289
9291a0cd 4290 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4291}
4292
93311388 4293/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4294
93311388 4295static void
31ffec48 4296load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4297{
31ffec48 4298 bfd *abfd = objfile->obfd;
10b3939b 4299 struct dwarf2_cu *cu;
c764a876 4300 unsigned int offset;
93311388 4301 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4302 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4303 struct attribute *attr;
98bfdba5 4304 int read_cu = 0;
6502dd73 4305
348e048f
DE
4306 gdb_assert (! per_cu->from_debug_types);
4307
c906108c 4308 /* Set local variables from the partial symbol table info. */
10b3939b 4309 offset = per_cu->offset;
6502dd73 4310
be391dca 4311 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4312 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4313 beg_of_comp_unit = info_ptr;
63d06c5c 4314
98bfdba5
PA
4315 if (per_cu->cu == NULL)
4316 {
4317 cu = alloc_one_comp_unit (objfile);
4318
4319 read_cu = 1;
c906108c 4320
98bfdba5
PA
4321 /* If an error occurs while loading, release our storage. */
4322 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4323
98bfdba5
PA
4324 /* Read in the comp_unit header. */
4325 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4326
98bfdba5
PA
4327 /* Complete the cu_header. */
4328 cu->header.offset = offset;
4329 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4330
98bfdba5
PA
4331 /* Read the abbrevs for this compilation unit. */
4332 dwarf2_read_abbrevs (abfd, cu);
4333 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4334
98bfdba5
PA
4335 /* Link this compilation unit into the compilation unit tree. */
4336 per_cu->cu = cu;
4337 cu->per_cu = per_cu;
98bfdba5
PA
4338
4339 /* Link this CU into read_in_chain. */
4340 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4341 dwarf2_per_objfile->read_in_chain = per_cu;
4342 }
4343 else
4344 {
4345 cu = per_cu->cu;
4346 info_ptr += cu->header.first_die_offset;
4347 }
e142c38c 4348
93311388 4349 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4350
4351 /* We try not to read any attributes in this function, because not
4352 all objfiles needed for references have been loaded yet, and symbol
4353 table processing isn't initialized. But we have to set the CU language,
4354 or we won't be able to build types correctly. */
4355 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4356 if (attr)
4357 set_cu_language (DW_UNSND (attr), cu);
4358 else
4359 set_cu_language (language_minimal, cu);
4360
a6c727b2
DJ
4361 /* Similarly, if we do not read the producer, we can not apply
4362 producer-specific interpretation. */
4363 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4364 if (attr)
4365 cu->producer = DW_STRING (attr);
4366
98bfdba5
PA
4367 if (read_cu)
4368 {
4369 do_cleanups (free_abbrevs_cleanup);
e142c38c 4370
98bfdba5
PA
4371 /* We've successfully allocated this compilation unit. Let our
4372 caller clean it up when finished with it. */
4373 discard_cleanups (free_cu_cleanup);
4374 }
10b3939b
DJ
4375}
4376
3da10d80
KS
4377/* Add a DIE to the delayed physname list. */
4378
4379static void
4380add_to_method_list (struct type *type, int fnfield_index, int index,
4381 const char *name, struct die_info *die,
4382 struct dwarf2_cu *cu)
4383{
4384 struct delayed_method_info mi;
4385 mi.type = type;
4386 mi.fnfield_index = fnfield_index;
4387 mi.index = index;
4388 mi.name = name;
4389 mi.die = die;
4390 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4391}
4392
4393/* A cleanup for freeing the delayed method list. */
4394
4395static void
4396free_delayed_list (void *ptr)
4397{
4398 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4399 if (cu->method_list != NULL)
4400 {
4401 VEC_free (delayed_method_info, cu->method_list);
4402 cu->method_list = NULL;
4403 }
4404}
4405
4406/* Compute the physnames of any methods on the CU's method list.
4407
4408 The computation of method physnames is delayed in order to avoid the
4409 (bad) condition that one of the method's formal parameters is of an as yet
4410 incomplete type. */
4411
4412static void
4413compute_delayed_physnames (struct dwarf2_cu *cu)
4414{
4415 int i;
4416 struct delayed_method_info *mi;
4417 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4418 {
4419 char *physname;
4420 struct fn_fieldlist *fn_flp
4421 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4422 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4423 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4424 }
4425}
4426
10b3939b
DJ
4427/* Generate full symbol information for PST and CU, whose DIEs have
4428 already been loaded into memory. */
4429
4430static void
4431process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4432{
10b3939b 4433 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4434 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4435 CORE_ADDR lowpc, highpc;
4436 struct symtab *symtab;
3da10d80 4437 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4438 CORE_ADDR baseaddr;
4439
4440 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4441
10b3939b
DJ
4442 buildsym_init ();
4443 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4444 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4445
4446 cu->list_in_scope = &file_symbols;
c906108c 4447
d85a05f0 4448 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4449
c906108c 4450 /* Do line number decoding in read_file_scope () */
10b3939b 4451 process_die (cu->dies, cu);
c906108c 4452
3da10d80
KS
4453 /* Now that we have processed all the DIEs in the CU, all the types
4454 should be complete, and it should now be safe to compute all of the
4455 physnames. */
4456 compute_delayed_physnames (cu);
4457 do_cleanups (delayed_list_cleanup);
4458
fae299cd
DC
4459 /* Some compilers don't define a DW_AT_high_pc attribute for the
4460 compilation unit. If the DW_AT_high_pc is missing, synthesize
4461 it, by scanning the DIE's below the compilation unit. */
10b3939b 4462 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4463
613e1657 4464 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4465
4466 /* Set symtab language to language from DW_AT_language.
4467 If the compilation is from a C file generated by language preprocessors,
4468 do not set the language if it was already deduced by start_subfile. */
4469 if (symtab != NULL
10b3939b 4470 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4471 {
10b3939b 4472 symtab->language = cu->language;
c906108c 4473 }
9291a0cd
TT
4474
4475 if (dwarf2_per_objfile->using_index)
4476 per_cu->v.quick->symtab = symtab;
4477 else
4478 {
4479 struct partial_symtab *pst = per_cu->v.psymtab;
4480 pst->symtab = symtab;
4481 pst->readin = 1;
4482 }
c906108c
SS
4483
4484 do_cleanups (back_to);
4485}
4486
4487/* Process a die and its children. */
4488
4489static void
e7c27a73 4490process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4491{
4492 switch (die->tag)
4493 {
4494 case DW_TAG_padding:
4495 break;
4496 case DW_TAG_compile_unit:
e7c27a73 4497 read_file_scope (die, cu);
c906108c 4498 break;
348e048f
DE
4499 case DW_TAG_type_unit:
4500 read_type_unit_scope (die, cu);
4501 break;
c906108c 4502 case DW_TAG_subprogram:
c906108c 4503 case DW_TAG_inlined_subroutine:
edb3359d 4504 read_func_scope (die, cu);
c906108c
SS
4505 break;
4506 case DW_TAG_lexical_block:
14898363
L
4507 case DW_TAG_try_block:
4508 case DW_TAG_catch_block:
e7c27a73 4509 read_lexical_block_scope (die, cu);
c906108c
SS
4510 break;
4511 case DW_TAG_class_type:
680b30c7 4512 case DW_TAG_interface_type:
c906108c
SS
4513 case DW_TAG_structure_type:
4514 case DW_TAG_union_type:
134d01f1 4515 process_structure_scope (die, cu);
c906108c
SS
4516 break;
4517 case DW_TAG_enumeration_type:
134d01f1 4518 process_enumeration_scope (die, cu);
c906108c 4519 break;
134d01f1 4520
f792889a
DJ
4521 /* These dies have a type, but processing them does not create
4522 a symbol or recurse to process the children. Therefore we can
4523 read them on-demand through read_type_die. */
c906108c 4524 case DW_TAG_subroutine_type:
72019c9c 4525 case DW_TAG_set_type:
c906108c 4526 case DW_TAG_array_type:
c906108c 4527 case DW_TAG_pointer_type:
c906108c 4528 case DW_TAG_ptr_to_member_type:
c906108c 4529 case DW_TAG_reference_type:
c906108c 4530 case DW_TAG_string_type:
c906108c 4531 break;
134d01f1 4532
c906108c 4533 case DW_TAG_base_type:
a02abb62 4534 case DW_TAG_subrange_type:
cb249c71 4535 case DW_TAG_typedef:
134d01f1
DJ
4536 /* Add a typedef symbol for the type definition, if it has a
4537 DW_AT_name. */
f792889a 4538 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4539 break;
c906108c 4540 case DW_TAG_common_block:
e7c27a73 4541 read_common_block (die, cu);
c906108c
SS
4542 break;
4543 case DW_TAG_common_inclusion:
4544 break;
d9fa45fe 4545 case DW_TAG_namespace:
63d06c5c 4546 processing_has_namespace_info = 1;
e7c27a73 4547 read_namespace (die, cu);
d9fa45fe 4548 break;
5d7cb8df 4549 case DW_TAG_module:
f55ee35c 4550 processing_has_namespace_info = 1;
5d7cb8df
JK
4551 read_module (die, cu);
4552 break;
d9fa45fe
DC
4553 case DW_TAG_imported_declaration:
4554 case DW_TAG_imported_module:
63d06c5c 4555 processing_has_namespace_info = 1;
27aa8d6a
SW
4556 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4557 || cu->language != language_fortran))
4558 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4559 dwarf_tag_name (die->tag));
4560 read_import_statement (die, cu);
d9fa45fe 4561 break;
c906108c 4562 default:
e7c27a73 4563 new_symbol (die, NULL, cu);
c906108c
SS
4564 break;
4565 }
4566}
4567
94af9270
KS
4568/* A helper function for dwarf2_compute_name which determines whether DIE
4569 needs to have the name of the scope prepended to the name listed in the
4570 die. */
4571
4572static int
4573die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4574{
1c809c68
TT
4575 struct attribute *attr;
4576
94af9270
KS
4577 switch (die->tag)
4578 {
4579 case DW_TAG_namespace:
4580 case DW_TAG_typedef:
4581 case DW_TAG_class_type:
4582 case DW_TAG_interface_type:
4583 case DW_TAG_structure_type:
4584 case DW_TAG_union_type:
4585 case DW_TAG_enumeration_type:
4586 case DW_TAG_enumerator:
4587 case DW_TAG_subprogram:
4588 case DW_TAG_member:
4589 return 1;
4590
4591 case DW_TAG_variable:
c2b0a229 4592 case DW_TAG_constant:
94af9270
KS
4593 /* We only need to prefix "globally" visible variables. These include
4594 any variable marked with DW_AT_external or any variable that
4595 lives in a namespace. [Variables in anonymous namespaces
4596 require prefixing, but they are not DW_AT_external.] */
4597
4598 if (dwarf2_attr (die, DW_AT_specification, cu))
4599 {
4600 struct dwarf2_cu *spec_cu = cu;
9a619af0 4601
94af9270
KS
4602 return die_needs_namespace (die_specification (die, &spec_cu),
4603 spec_cu);
4604 }
4605
1c809c68 4606 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4607 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4608 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4609 return 0;
4610 /* A variable in a lexical block of some kind does not need a
4611 namespace, even though in C++ such variables may be external
4612 and have a mangled name. */
4613 if (die->parent->tag == DW_TAG_lexical_block
4614 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4615 || die->parent->tag == DW_TAG_catch_block
4616 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4617 return 0;
4618 return 1;
94af9270
KS
4619
4620 default:
4621 return 0;
4622 }
4623}
4624
98bfdba5
PA
4625/* Retrieve the last character from a mem_file. */
4626
4627static void
4628do_ui_file_peek_last (void *object, const char *buffer, long length)
4629{
4630 char *last_char_p = (char *) object;
4631
4632 if (length > 0)
4633 *last_char_p = buffer[length - 1];
4634}
4635
94af9270
KS
4636/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4637 compute the physname for the object, which include a method's
4638 formal parameters (C++/Java) and return type (Java).
4639
af6b7be1
JB
4640 For Ada, return the DIE's linkage name rather than the fully qualified
4641 name. PHYSNAME is ignored..
4642
94af9270
KS
4643 The result is allocated on the objfile_obstack and canonicalized. */
4644
4645static const char *
4646dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4647 int physname)
4648{
4649 if (name == NULL)
4650 name = dwarf2_name (die, cu);
4651
f55ee35c
JK
4652 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4653 compute it by typename_concat inside GDB. */
4654 if (cu->language == language_ada
4655 || (cu->language == language_fortran && physname))
4656 {
4657 /* For Ada unit, we prefer the linkage name over the name, as
4658 the former contains the exported name, which the user expects
4659 to be able to reference. Ideally, we want the user to be able
4660 to reference this entity using either natural or linkage name,
4661 but we haven't started looking at this enhancement yet. */
4662 struct attribute *attr;
4663
4664 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4665 if (attr == NULL)
4666 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4667 if (attr && DW_STRING (attr))
4668 return DW_STRING (attr);
4669 }
4670
94af9270
KS
4671 /* These are the only languages we know how to qualify names in. */
4672 if (name != NULL
f55ee35c
JK
4673 && (cu->language == language_cplus || cu->language == language_java
4674 || cu->language == language_fortran))
94af9270
KS
4675 {
4676 if (die_needs_namespace (die, cu))
4677 {
4678 long length;
4679 char *prefix;
4680 struct ui_file *buf;
4681
4682 prefix = determine_prefix (die, cu);
4683 buf = mem_fileopen ();
4684 if (*prefix != '\0')
4685 {
f55ee35c
JK
4686 char *prefixed_name = typename_concat (NULL, prefix, name,
4687 physname, cu);
9a619af0 4688
94af9270
KS
4689 fputs_unfiltered (prefixed_name, buf);
4690 xfree (prefixed_name);
4691 }
4692 else
4693 fputs_unfiltered (name ? name : "", buf);
4694
98bfdba5
PA
4695 /* Template parameters may be specified in the DIE's DW_AT_name, or
4696 as children with DW_TAG_template_type_param or
4697 DW_TAG_value_type_param. If the latter, add them to the name
4698 here. If the name already has template parameters, then
4699 skip this step; some versions of GCC emit both, and
4700 it is more efficient to use the pre-computed name.
4701
4702 Something to keep in mind about this process: it is very
4703 unlikely, or in some cases downright impossible, to produce
4704 something that will match the mangled name of a function.
4705 If the definition of the function has the same debug info,
4706 we should be able to match up with it anyway. But fallbacks
4707 using the minimal symbol, for instance to find a method
4708 implemented in a stripped copy of libstdc++, will not work.
4709 If we do not have debug info for the definition, we will have to
4710 match them up some other way.
4711
4712 When we do name matching there is a related problem with function
4713 templates; two instantiated function templates are allowed to
4714 differ only by their return types, which we do not add here. */
4715
4716 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4717 {
4718 struct attribute *attr;
4719 struct die_info *child;
4720 int first = 1;
4721
4722 die->building_fullname = 1;
4723
4724 for (child = die->child; child != NULL; child = child->sibling)
4725 {
4726 struct type *type;
4727 long value;
4728 gdb_byte *bytes;
4729 struct dwarf2_locexpr_baton *baton;
4730 struct value *v;
4731
4732 if (child->tag != DW_TAG_template_type_param
4733 && child->tag != DW_TAG_template_value_param)
4734 continue;
4735
4736 if (first)
4737 {
4738 fputs_unfiltered ("<", buf);
4739 first = 0;
4740 }
4741 else
4742 fputs_unfiltered (", ", buf);
4743
4744 attr = dwarf2_attr (child, DW_AT_type, cu);
4745 if (attr == NULL)
4746 {
4747 complaint (&symfile_complaints,
4748 _("template parameter missing DW_AT_type"));
4749 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4750 continue;
4751 }
4752 type = die_type (child, cu);
4753
4754 if (child->tag == DW_TAG_template_type_param)
4755 {
4756 c_print_type (type, "", buf, -1, 0);
4757 continue;
4758 }
4759
4760 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4761 if (attr == NULL)
4762 {
4763 complaint (&symfile_complaints,
4764 _("template parameter missing DW_AT_const_value"));
4765 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4766 continue;
4767 }
4768
4769 dwarf2_const_value_attr (attr, type, name,
4770 &cu->comp_unit_obstack, cu,
4771 &value, &bytes, &baton);
4772
4773 if (TYPE_NOSIGN (type))
4774 /* GDB prints characters as NUMBER 'CHAR'. If that's
4775 changed, this can use value_print instead. */
4776 c_printchar (value, type, buf);
4777 else
4778 {
4779 struct value_print_options opts;
4780
4781 if (baton != NULL)
4782 v = dwarf2_evaluate_loc_desc (type, NULL,
4783 baton->data,
4784 baton->size,
4785 baton->per_cu);
4786 else if (bytes != NULL)
4787 {
4788 v = allocate_value (type);
4789 memcpy (value_contents_writeable (v), bytes,
4790 TYPE_LENGTH (type));
4791 }
4792 else
4793 v = value_from_longest (type, value);
4794
4795 /* Specify decimal so that we do not depend on the radix. */
4796 get_formatted_print_options (&opts, 'd');
4797 opts.raw = 1;
4798 value_print (v, buf, &opts);
4799 release_value (v);
4800 value_free (v);
4801 }
4802 }
4803
4804 die->building_fullname = 0;
4805
4806 if (!first)
4807 {
4808 /* Close the argument list, with a space if necessary
4809 (nested templates). */
4810 char last_char = '\0';
4811 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4812 if (last_char == '>')
4813 fputs_unfiltered (" >", buf);
4814 else
4815 fputs_unfiltered (">", buf);
4816 }
4817 }
4818
94af9270
KS
4819 /* For Java and C++ methods, append formal parameter type
4820 information, if PHYSNAME. */
6e70227d 4821
94af9270
KS
4822 if (physname && die->tag == DW_TAG_subprogram
4823 && (cu->language == language_cplus
4824 || cu->language == language_java))
4825 {
4826 struct type *type = read_type_die (die, cu);
4827
4828 c_type_print_args (type, buf, 0, cu->language);
4829
4830 if (cu->language == language_java)
4831 {
4832 /* For java, we must append the return type to method
4833 names. */
4834 if (die->tag == DW_TAG_subprogram)
4835 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4836 0, 0);
4837 }
4838 else if (cu->language == language_cplus)
4839 {
60430eff
DJ
4840 /* Assume that an artificial first parameter is
4841 "this", but do not crash if it is not. RealView
4842 marks unnamed (and thus unused) parameters as
4843 artificial; there is no way to differentiate
4844 the two cases. */
94af9270
KS
4845 if (TYPE_NFIELDS (type) > 0
4846 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4847 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4848 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4849 fputs_unfiltered (" const", buf);
4850 }
4851 }
4852
4853 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4854 &length);
4855 ui_file_delete (buf);
4856
4857 if (cu->language == language_cplus)
4858 {
4859 char *cname
4860 = dwarf2_canonicalize_name (name, cu,
4861 &cu->objfile->objfile_obstack);
9a619af0 4862
94af9270
KS
4863 if (cname != NULL)
4864 name = cname;
4865 }
4866 }
4867 }
4868
4869 return name;
4870}
4871
0114d602
DJ
4872/* Return the fully qualified name of DIE, based on its DW_AT_name.
4873 If scope qualifiers are appropriate they will be added. The result
4874 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4875 not have a name. NAME may either be from a previous call to
4876 dwarf2_name or NULL.
4877
4878 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4879
4880static const char *
94af9270 4881dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4882{
94af9270
KS
4883 return dwarf2_compute_name (name, die, cu, 0);
4884}
0114d602 4885
94af9270
KS
4886/* Construct a physname for the given DIE in CU. NAME may either be
4887 from a previous call to dwarf2_name or NULL. The result will be
4888 allocated on the objfile_objstack or NULL if the DIE does not have a
4889 name.
0114d602 4890
94af9270 4891 The output string will be canonicalized (if C++/Java). */
0114d602 4892
94af9270
KS
4893static const char *
4894dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4895{
4896 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4897}
4898
27aa8d6a
SW
4899/* Read the import statement specified by the given die and record it. */
4900
4901static void
4902read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4903{
4904 struct attribute *import_attr;
4905 struct die_info *imported_die;
de4affc9 4906 struct dwarf2_cu *imported_cu;
27aa8d6a 4907 const char *imported_name;
794684b6 4908 const char *imported_name_prefix;
13387711
SW
4909 const char *canonical_name;
4910 const char *import_alias;
4911 const char *imported_declaration = NULL;
794684b6 4912 const char *import_prefix;
13387711
SW
4913
4914 char *temp;
27aa8d6a
SW
4915
4916 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4917 if (import_attr == NULL)
4918 {
4919 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4920 dwarf_tag_name (die->tag));
4921 return;
4922 }
4923
de4affc9
CC
4924 imported_cu = cu;
4925 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4926 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4927 if (imported_name == NULL)
4928 {
4929 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4930
4931 The import in the following code:
4932 namespace A
4933 {
4934 typedef int B;
4935 }
4936
4937 int main ()
4938 {
4939 using A::B;
4940 B b;
4941 return b;
4942 }
4943
4944 ...
4945 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4946 <52> DW_AT_decl_file : 1
4947 <53> DW_AT_decl_line : 6
4948 <54> DW_AT_import : <0x75>
4949 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4950 <59> DW_AT_name : B
4951 <5b> DW_AT_decl_file : 1
4952 <5c> DW_AT_decl_line : 2
4953 <5d> DW_AT_type : <0x6e>
4954 ...
4955 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4956 <76> DW_AT_byte_size : 4
4957 <77> DW_AT_encoding : 5 (signed)
4958
4959 imports the wrong die ( 0x75 instead of 0x58 ).
4960 This case will be ignored until the gcc bug is fixed. */
4961 return;
4962 }
4963
82856980
SW
4964 /* Figure out the local name after import. */
4965 import_alias = dwarf2_name (die, cu);
27aa8d6a 4966
794684b6
SW
4967 /* Figure out where the statement is being imported to. */
4968 import_prefix = determine_prefix (die, cu);
4969
4970 /* Figure out what the scope of the imported die is and prepend it
4971 to the name of the imported die. */
de4affc9 4972 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4973
f55ee35c
JK
4974 if (imported_die->tag != DW_TAG_namespace
4975 && imported_die->tag != DW_TAG_module)
794684b6 4976 {
13387711
SW
4977 imported_declaration = imported_name;
4978 canonical_name = imported_name_prefix;
794684b6 4979 }
13387711 4980 else if (strlen (imported_name_prefix) > 0)
794684b6 4981 {
13387711
SW
4982 temp = alloca (strlen (imported_name_prefix)
4983 + 2 + strlen (imported_name) + 1);
4984 strcpy (temp, imported_name_prefix);
4985 strcat (temp, "::");
4986 strcat (temp, imported_name);
4987 canonical_name = temp;
794684b6 4988 }
13387711
SW
4989 else
4990 canonical_name = imported_name;
794684b6 4991
c0cc3a76
SW
4992 cp_add_using_directive (import_prefix,
4993 canonical_name,
4994 import_alias,
13387711 4995 imported_declaration,
c0cc3a76 4996 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4997}
4998
5fb290d7 4999static void
e142c38c 5000initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5001{
e142c38c 5002 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5003}
5004
cb1df416
DJ
5005static void
5006free_cu_line_header (void *arg)
5007{
5008 struct dwarf2_cu *cu = arg;
5009
5010 free_line_header (cu->line_header);
5011 cu->line_header = NULL;
5012}
5013
9291a0cd
TT
5014static void
5015find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5016 char **name, char **comp_dir)
5017{
5018 struct attribute *attr;
5019
5020 *name = NULL;
5021 *comp_dir = NULL;
5022
5023 /* Find the filename. Do not use dwarf2_name here, since the filename
5024 is not a source language identifier. */
5025 attr = dwarf2_attr (die, DW_AT_name, cu);
5026 if (attr)
5027 {
5028 *name = DW_STRING (attr);
5029 }
5030
5031 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5032 if (attr)
5033 *comp_dir = DW_STRING (attr);
5034 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5035 {
5036 *comp_dir = ldirname (*name);
5037 if (*comp_dir != NULL)
5038 make_cleanup (xfree, *comp_dir);
5039 }
5040 if (*comp_dir != NULL)
5041 {
5042 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5043 directory, get rid of it. */
5044 char *cp = strchr (*comp_dir, ':');
5045
5046 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5047 *comp_dir = cp + 1;
5048 }
5049
5050 if (*name == NULL)
5051 *name = "<unknown>";
5052}
5053
c906108c 5054static void
e7c27a73 5055read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5056{
e7c27a73 5057 struct objfile *objfile = cu->objfile;
debd256d 5058 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5059 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5060 CORE_ADDR highpc = ((CORE_ADDR) 0);
5061 struct attribute *attr;
e1024ff1 5062 char *name = NULL;
c906108c
SS
5063 char *comp_dir = NULL;
5064 struct die_info *child_die;
5065 bfd *abfd = objfile->obfd;
debd256d 5066 struct line_header *line_header = 0;
e142c38c 5067 CORE_ADDR baseaddr;
6e70227d 5068
e142c38c 5069 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5070
fae299cd 5071 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5072
5073 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5074 from finish_block. */
2acceee2 5075 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5076 lowpc = highpc;
5077 lowpc += baseaddr;
5078 highpc += baseaddr;
5079
9291a0cd 5080 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5081
e142c38c 5082 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5083 if (attr)
5084 {
e142c38c 5085 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5086 }
5087
b0f35d58 5088 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5089 if (attr)
b0f35d58 5090 cu->producer = DW_STRING (attr);
303b6f5d 5091
f4b8a18d
KW
5092 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5093 standardised yet. As a workaround for the language detection we fall
5094 back to the DW_AT_producer string. */
5095 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5096 cu->language = language_opencl;
5097
c906108c
SS
5098 /* We assume that we're processing GCC output. */
5099 processing_gcc_compilation = 2;
c906108c 5100
df8a16a1
DJ
5101 processing_has_namespace_info = 0;
5102
c906108c
SS
5103 start_symtab (name, comp_dir, lowpc);
5104 record_debugformat ("DWARF 2");
303b6f5d 5105 record_producer (cu->producer);
c906108c 5106
e142c38c 5107 initialize_cu_func_list (cu);
c906108c 5108
cb1df416
DJ
5109 /* Decode line number information if present. We do this before
5110 processing child DIEs, so that the line header table is available
5111 for DW_AT_decl_file. */
e142c38c 5112 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5113 if (attr)
5114 {
debd256d 5115 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5116 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5117 if (line_header)
5118 {
cb1df416
DJ
5119 cu->line_header = line_header;
5120 make_cleanup (free_cu_line_header, cu);
aaa75496 5121 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5122 }
5fb290d7 5123 }
debd256d 5124
cb1df416
DJ
5125 /* Process all dies in compilation unit. */
5126 if (die->child != NULL)
5127 {
5128 child_die = die->child;
5129 while (child_die && child_die->tag)
5130 {
5131 process_die (child_die, cu);
5132 child_die = sibling_die (child_die);
5133 }
5134 }
5135
2e276125
JB
5136 /* Decode macro information, if present. Dwarf 2 macro information
5137 refers to information in the line number info statement program
5138 header, so we can only read it if we've read the header
5139 successfully. */
e142c38c 5140 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5141 if (attr && line_header)
2e276125
JB
5142 {
5143 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5144
2e276125 5145 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5146 comp_dir, abfd, cu);
2e276125 5147 }
debd256d 5148 do_cleanups (back_to);
5fb290d7
DJ
5149}
5150
348e048f
DE
5151/* For TUs we want to skip the first top level sibling if it's not the
5152 actual type being defined by this TU. In this case the first top
5153 level sibling is there to provide context only. */
5154
5155static void
5156read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5157{
5158 struct objfile *objfile = cu->objfile;
5159 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5160 CORE_ADDR lowpc;
5161 struct attribute *attr;
5162 char *name = NULL;
5163 char *comp_dir = NULL;
5164 struct die_info *child_die;
5165 bfd *abfd = objfile->obfd;
348e048f
DE
5166
5167 /* start_symtab needs a low pc, but we don't really have one.
5168 Do what read_file_scope would do in the absence of such info. */
5169 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5170
5171 /* Find the filename. Do not use dwarf2_name here, since the filename
5172 is not a source language identifier. */
5173 attr = dwarf2_attr (die, DW_AT_name, cu);
5174 if (attr)
5175 name = DW_STRING (attr);
5176
5177 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5178 if (attr)
5179 comp_dir = DW_STRING (attr);
5180 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5181 {
5182 comp_dir = ldirname (name);
5183 if (comp_dir != NULL)
5184 make_cleanup (xfree, comp_dir);
5185 }
5186
5187 if (name == NULL)
5188 name = "<unknown>";
5189
5190 attr = dwarf2_attr (die, DW_AT_language, cu);
5191 if (attr)
5192 set_cu_language (DW_UNSND (attr), cu);
5193
5194 /* This isn't technically needed today. It is done for symmetry
5195 with read_file_scope. */
5196 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5197 if (attr)
348e048f
DE
5198 cu->producer = DW_STRING (attr);
5199
5200 /* We assume that we're processing GCC output. */
5201 processing_gcc_compilation = 2;
5202
5203 processing_has_namespace_info = 0;
5204
5205 start_symtab (name, comp_dir, lowpc);
5206 record_debugformat ("DWARF 2");
5207 record_producer (cu->producer);
5208
5209 /* Process the dies in the type unit. */
5210 if (die->child == NULL)
5211 {
5212 dump_die_for_error (die);
5213 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5214 bfd_get_filename (abfd));
5215 }
5216
5217 child_die = die->child;
5218
5219 while (child_die && child_die->tag)
5220 {
5221 process_die (child_die, cu);
5222
5223 child_die = sibling_die (child_die);
5224 }
5225
5226 do_cleanups (back_to);
5227}
5228
5fb290d7 5229static void
e142c38c
DJ
5230add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5231 struct dwarf2_cu *cu)
5fb290d7
DJ
5232{
5233 struct function_range *thisfn;
5234
5235 thisfn = (struct function_range *)
7b5a2f43 5236 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5237 thisfn->name = name;
5238 thisfn->lowpc = lowpc;
5239 thisfn->highpc = highpc;
5240 thisfn->seen_line = 0;
5241 thisfn->next = NULL;
5242
e142c38c
DJ
5243 if (cu->last_fn == NULL)
5244 cu->first_fn = thisfn;
5fb290d7 5245 else
e142c38c 5246 cu->last_fn->next = thisfn;
5fb290d7 5247
e142c38c 5248 cu->last_fn = thisfn;
c906108c
SS
5249}
5250
d389af10
JK
5251/* qsort helper for inherit_abstract_dies. */
5252
5253static int
5254unsigned_int_compar (const void *ap, const void *bp)
5255{
5256 unsigned int a = *(unsigned int *) ap;
5257 unsigned int b = *(unsigned int *) bp;
5258
5259 return (a > b) - (b > a);
5260}
5261
5262/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5263 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5264 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5265
5266static void
5267inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5268{
5269 struct die_info *child_die;
5270 unsigned die_children_count;
5271 /* CU offsets which were referenced by children of the current DIE. */
5272 unsigned *offsets;
5273 unsigned *offsets_end, *offsetp;
5274 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5275 struct die_info *origin_die;
5276 /* Iterator of the ORIGIN_DIE children. */
5277 struct die_info *origin_child_die;
5278 struct cleanup *cleanups;
5279 struct attribute *attr;
cd02d79d
PA
5280 struct dwarf2_cu *origin_cu;
5281 struct pending **origin_previous_list_in_scope;
d389af10
JK
5282
5283 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5284 if (!attr)
5285 return;
5286
cd02d79d
PA
5287 /* Note that following die references may follow to a die in a
5288 different cu. */
5289
5290 origin_cu = cu;
5291 origin_die = follow_die_ref (die, attr, &origin_cu);
5292
5293 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5294 symbols in. */
5295 origin_previous_list_in_scope = origin_cu->list_in_scope;
5296 origin_cu->list_in_scope = cu->list_in_scope;
5297
edb3359d
DJ
5298 if (die->tag != origin_die->tag
5299 && !(die->tag == DW_TAG_inlined_subroutine
5300 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5301 complaint (&symfile_complaints,
5302 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5303 die->offset, origin_die->offset);
5304
5305 child_die = die->child;
5306 die_children_count = 0;
5307 while (child_die && child_die->tag)
5308 {
5309 child_die = sibling_die (child_die);
5310 die_children_count++;
5311 }
5312 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5313 cleanups = make_cleanup (xfree, offsets);
5314
5315 offsets_end = offsets;
5316 child_die = die->child;
5317 while (child_die && child_die->tag)
5318 {
c38f313d
DJ
5319 /* For each CHILD_DIE, find the corresponding child of
5320 ORIGIN_DIE. If there is more than one layer of
5321 DW_AT_abstract_origin, follow them all; there shouldn't be,
5322 but GCC versions at least through 4.4 generate this (GCC PR
5323 40573). */
5324 struct die_info *child_origin_die = child_die;
cd02d79d 5325 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5326
c38f313d
DJ
5327 while (1)
5328 {
cd02d79d
PA
5329 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5330 child_origin_cu);
c38f313d
DJ
5331 if (attr == NULL)
5332 break;
cd02d79d
PA
5333 child_origin_die = follow_die_ref (child_origin_die, attr,
5334 &child_origin_cu);
c38f313d
DJ
5335 }
5336
d389af10
JK
5337 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5338 counterpart may exist. */
c38f313d 5339 if (child_origin_die != child_die)
d389af10 5340 {
edb3359d
DJ
5341 if (child_die->tag != child_origin_die->tag
5342 && !(child_die->tag == DW_TAG_inlined_subroutine
5343 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5344 complaint (&symfile_complaints,
5345 _("Child DIE 0x%x and its abstract origin 0x%x have "
5346 "different tags"), child_die->offset,
5347 child_origin_die->offset);
c38f313d
DJ
5348 if (child_origin_die->parent != origin_die)
5349 complaint (&symfile_complaints,
5350 _("Child DIE 0x%x and its abstract origin 0x%x have "
5351 "different parents"), child_die->offset,
5352 child_origin_die->offset);
5353 else
5354 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5355 }
5356 child_die = sibling_die (child_die);
5357 }
5358 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5359 unsigned_int_compar);
5360 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5361 if (offsetp[-1] == *offsetp)
5362 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5363 "to DIE 0x%x as their abstract origin"),
5364 die->offset, *offsetp);
5365
5366 offsetp = offsets;
5367 origin_child_die = origin_die->child;
5368 while (origin_child_die && origin_child_die->tag)
5369 {
5370 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5371 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5372 offsetp++;
5373 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5374 {
5375 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5376 process_die (origin_child_die, origin_cu);
d389af10
JK
5377 }
5378 origin_child_die = sibling_die (origin_child_die);
5379 }
cd02d79d 5380 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5381
5382 do_cleanups (cleanups);
5383}
5384
c906108c 5385static void
e7c27a73 5386read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5387{
e7c27a73 5388 struct objfile *objfile = cu->objfile;
52f0bd74 5389 struct context_stack *new;
c906108c
SS
5390 CORE_ADDR lowpc;
5391 CORE_ADDR highpc;
5392 struct die_info *child_die;
edb3359d 5393 struct attribute *attr, *call_line, *call_file;
c906108c 5394 char *name;
e142c38c 5395 CORE_ADDR baseaddr;
801e3a5b 5396 struct block *block;
edb3359d 5397 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5398 VEC (symbolp) *template_args = NULL;
5399 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5400
5401 if (inlined_func)
5402 {
5403 /* If we do not have call site information, we can't show the
5404 caller of this inlined function. That's too confusing, so
5405 only use the scope for local variables. */
5406 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5407 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5408 if (call_line == NULL || call_file == NULL)
5409 {
5410 read_lexical_block_scope (die, cu);
5411 return;
5412 }
5413 }
c906108c 5414
e142c38c
DJ
5415 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5416
94af9270 5417 name = dwarf2_name (die, cu);
c906108c 5418
e8d05480
JB
5419 /* Ignore functions with missing or empty names. These are actually
5420 illegal according to the DWARF standard. */
5421 if (name == NULL)
5422 {
5423 complaint (&symfile_complaints,
5424 _("missing name for subprogram DIE at %d"), die->offset);
5425 return;
5426 }
5427
5428 /* Ignore functions with missing or invalid low and high pc attributes. */
5429 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5430 {
ae4d0c03
PM
5431 attr = dwarf2_attr (die, DW_AT_external, cu);
5432 if (!attr || !DW_UNSND (attr))
5433 complaint (&symfile_complaints,
5434 _("cannot get low and high bounds for subprogram DIE at %d"),
5435 die->offset);
e8d05480
JB
5436 return;
5437 }
c906108c
SS
5438
5439 lowpc += baseaddr;
5440 highpc += baseaddr;
5441
5fb290d7 5442 /* Record the function range for dwarf_decode_lines. */
e142c38c 5443 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5444
34eaf542
TT
5445 /* If we have any template arguments, then we must allocate a
5446 different sort of symbol. */
5447 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5448 {
5449 if (child_die->tag == DW_TAG_template_type_param
5450 || child_die->tag == DW_TAG_template_value_param)
5451 {
5452 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5453 struct template_symbol);
5454 templ_func->base.is_cplus_template_function = 1;
5455 break;
5456 }
5457 }
5458
c906108c 5459 new = push_context (0, lowpc);
34eaf542
TT
5460 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5461 (struct symbol *) templ_func);
4c2df51b 5462
4cecd739
DJ
5463 /* If there is a location expression for DW_AT_frame_base, record
5464 it. */
e142c38c 5465 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5466 if (attr)
c034e007
AC
5467 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5468 expression is being recorded directly in the function's symbol
5469 and not in a separate frame-base object. I guess this hack is
5470 to avoid adding some sort of frame-base adjunct/annex to the
5471 function's symbol :-(. The problem with doing this is that it
5472 results in a function symbol with a location expression that
5473 has nothing to do with the location of the function, ouch! The
5474 relationship should be: a function's symbol has-a frame base; a
5475 frame-base has-a location expression. */
e7c27a73 5476 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5477
e142c38c 5478 cu->list_in_scope = &local_symbols;
c906108c 5479
639d11d3 5480 if (die->child != NULL)
c906108c 5481 {
639d11d3 5482 child_die = die->child;
c906108c
SS
5483 while (child_die && child_die->tag)
5484 {
34eaf542
TT
5485 if (child_die->tag == DW_TAG_template_type_param
5486 || child_die->tag == DW_TAG_template_value_param)
5487 {
5488 struct symbol *arg = new_symbol (child_die, NULL, cu);
5489
f1078f66
DJ
5490 if (arg != NULL)
5491 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5492 }
5493 else
5494 process_die (child_die, cu);
c906108c
SS
5495 child_die = sibling_die (child_die);
5496 }
5497 }
5498
d389af10
JK
5499 inherit_abstract_dies (die, cu);
5500
4a811a97
UW
5501 /* If we have a DW_AT_specification, we might need to import using
5502 directives from the context of the specification DIE. See the
5503 comment in determine_prefix. */
5504 if (cu->language == language_cplus
5505 && dwarf2_attr (die, DW_AT_specification, cu))
5506 {
5507 struct dwarf2_cu *spec_cu = cu;
5508 struct die_info *spec_die = die_specification (die, &spec_cu);
5509
5510 while (spec_die)
5511 {
5512 child_die = spec_die->child;
5513 while (child_die && child_die->tag)
5514 {
5515 if (child_die->tag == DW_TAG_imported_module)
5516 process_die (child_die, spec_cu);
5517 child_die = sibling_die (child_die);
5518 }
5519
5520 /* In some cases, GCC generates specification DIEs that
5521 themselves contain DW_AT_specification attributes. */
5522 spec_die = die_specification (spec_die, &spec_cu);
5523 }
5524 }
5525
c906108c
SS
5526 new = pop_context ();
5527 /* Make a block for the local symbols within. */
801e3a5b
JB
5528 block = finish_block (new->name, &local_symbols, new->old_blocks,
5529 lowpc, highpc, objfile);
5530
df8a16a1 5531 /* For C++, set the block's scope. */
f55ee35c 5532 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5533 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5534 determine_prefix (die, cu),
df8a16a1
DJ
5535 processing_has_namespace_info);
5536
801e3a5b
JB
5537 /* If we have address ranges, record them. */
5538 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5539
34eaf542
TT
5540 /* Attach template arguments to function. */
5541 if (! VEC_empty (symbolp, template_args))
5542 {
5543 gdb_assert (templ_func != NULL);
5544
5545 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5546 templ_func->template_arguments
5547 = obstack_alloc (&objfile->objfile_obstack,
5548 (templ_func->n_template_arguments
5549 * sizeof (struct symbol *)));
5550 memcpy (templ_func->template_arguments,
5551 VEC_address (symbolp, template_args),
5552 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5553 VEC_free (symbolp, template_args);
5554 }
5555
208d8187
JB
5556 /* In C++, we can have functions nested inside functions (e.g., when
5557 a function declares a class that has methods). This means that
5558 when we finish processing a function scope, we may need to go
5559 back to building a containing block's symbol lists. */
5560 local_symbols = new->locals;
5561 param_symbols = new->params;
27aa8d6a 5562 using_directives = new->using_directives;
208d8187 5563
921e78cf
JB
5564 /* If we've finished processing a top-level function, subsequent
5565 symbols go in the file symbol list. */
5566 if (outermost_context_p ())
e142c38c 5567 cu->list_in_scope = &file_symbols;
c906108c
SS
5568}
5569
5570/* Process all the DIES contained within a lexical block scope. Start
5571 a new scope, process the dies, and then close the scope. */
5572
5573static void
e7c27a73 5574read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5575{
e7c27a73 5576 struct objfile *objfile = cu->objfile;
52f0bd74 5577 struct context_stack *new;
c906108c
SS
5578 CORE_ADDR lowpc, highpc;
5579 struct die_info *child_die;
e142c38c
DJ
5580 CORE_ADDR baseaddr;
5581
5582 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5583
5584 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5585 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5586 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5587 be nasty. Might be easier to properly extend generic blocks to
af34e669 5588 describe ranges. */
d85a05f0 5589 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5590 return;
5591 lowpc += baseaddr;
5592 highpc += baseaddr;
5593
5594 push_context (0, lowpc);
639d11d3 5595 if (die->child != NULL)
c906108c 5596 {
639d11d3 5597 child_die = die->child;
c906108c
SS
5598 while (child_die && child_die->tag)
5599 {
e7c27a73 5600 process_die (child_die, cu);
c906108c
SS
5601 child_die = sibling_die (child_die);
5602 }
5603 }
5604 new = pop_context ();
5605
8540c487 5606 if (local_symbols != NULL || using_directives != NULL)
c906108c 5607 {
801e3a5b
JB
5608 struct block *block
5609 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5610 highpc, objfile);
5611
5612 /* Note that recording ranges after traversing children, as we
5613 do here, means that recording a parent's ranges entails
5614 walking across all its children's ranges as they appear in
5615 the address map, which is quadratic behavior.
5616
5617 It would be nicer to record the parent's ranges before
5618 traversing its children, simply overriding whatever you find
5619 there. But since we don't even decide whether to create a
5620 block until after we've traversed its children, that's hard
5621 to do. */
5622 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5623 }
5624 local_symbols = new->locals;
27aa8d6a 5625 using_directives = new->using_directives;
c906108c
SS
5626}
5627
43039443 5628/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5629 Return 1 if the attributes are present and valid, otherwise, return 0.
5630 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5631
5632static int
5633dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5634 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5635 struct partial_symtab *ranges_pst)
43039443
JK
5636{
5637 struct objfile *objfile = cu->objfile;
5638 struct comp_unit_head *cu_header = &cu->header;
5639 bfd *obfd = objfile->obfd;
5640 unsigned int addr_size = cu_header->addr_size;
5641 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5642 /* Base address selection entry. */
5643 CORE_ADDR base;
5644 int found_base;
5645 unsigned int dummy;
5646 gdb_byte *buffer;
5647 CORE_ADDR marker;
5648 int low_set;
5649 CORE_ADDR low = 0;
5650 CORE_ADDR high = 0;
ff013f42 5651 CORE_ADDR baseaddr;
43039443 5652
d00adf39
DE
5653 found_base = cu->base_known;
5654 base = cu->base_address;
43039443 5655
be391dca 5656 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5657 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5658 {
5659 complaint (&symfile_complaints,
5660 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5661 offset);
5662 return 0;
5663 }
dce234bc 5664 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5665
5666 /* Read in the largest possible address. */
5667 marker = read_address (obfd, buffer, cu, &dummy);
5668 if ((marker & mask) == mask)
5669 {
5670 /* If we found the largest possible address, then
5671 read the base address. */
5672 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5673 buffer += 2 * addr_size;
5674 offset += 2 * addr_size;
5675 found_base = 1;
5676 }
5677
5678 low_set = 0;
5679
e7030f15 5680 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5681
43039443
JK
5682 while (1)
5683 {
5684 CORE_ADDR range_beginning, range_end;
5685
5686 range_beginning = read_address (obfd, buffer, cu, &dummy);
5687 buffer += addr_size;
5688 range_end = read_address (obfd, buffer, cu, &dummy);
5689 buffer += addr_size;
5690 offset += 2 * addr_size;
5691
5692 /* An end of list marker is a pair of zero addresses. */
5693 if (range_beginning == 0 && range_end == 0)
5694 /* Found the end of list entry. */
5695 break;
5696
5697 /* Each base address selection entry is a pair of 2 values.
5698 The first is the largest possible address, the second is
5699 the base address. Check for a base address here. */
5700 if ((range_beginning & mask) == mask)
5701 {
5702 /* If we found the largest possible address, then
5703 read the base address. */
5704 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5705 found_base = 1;
5706 continue;
5707 }
5708
5709 if (!found_base)
5710 {
5711 /* We have no valid base address for the ranges
5712 data. */
5713 complaint (&symfile_complaints,
5714 _("Invalid .debug_ranges data (no base address)"));
5715 return 0;
5716 }
5717
5718 range_beginning += base;
5719 range_end += base;
5720
ff013f42
JK
5721 if (ranges_pst != NULL && range_beginning < range_end)
5722 addrmap_set_empty (objfile->psymtabs_addrmap,
5723 range_beginning + baseaddr, range_end - 1 + baseaddr,
5724 ranges_pst);
5725
43039443
JK
5726 /* FIXME: This is recording everything as a low-high
5727 segment of consecutive addresses. We should have a
5728 data structure for discontiguous block ranges
5729 instead. */
5730 if (! low_set)
5731 {
5732 low = range_beginning;
5733 high = range_end;
5734 low_set = 1;
5735 }
5736 else
5737 {
5738 if (range_beginning < low)
5739 low = range_beginning;
5740 if (range_end > high)
5741 high = range_end;
5742 }
5743 }
5744
5745 if (! low_set)
5746 /* If the first entry is an end-of-list marker, the range
5747 describes an empty scope, i.e. no instructions. */
5748 return 0;
5749
5750 if (low_return)
5751 *low_return = low;
5752 if (high_return)
5753 *high_return = high;
5754 return 1;
5755}
5756
af34e669
DJ
5757/* Get low and high pc attributes from a die. Return 1 if the attributes
5758 are present and valid, otherwise, return 0. Return -1 if the range is
5759 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5760static int
af34e669 5761dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5762 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5763 struct partial_symtab *pst)
c906108c
SS
5764{
5765 struct attribute *attr;
af34e669
DJ
5766 CORE_ADDR low = 0;
5767 CORE_ADDR high = 0;
5768 int ret = 0;
c906108c 5769
e142c38c 5770 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5771 if (attr)
af34e669
DJ
5772 {
5773 high = DW_ADDR (attr);
e142c38c 5774 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5775 if (attr)
5776 low = DW_ADDR (attr);
5777 else
5778 /* Found high w/o low attribute. */
5779 return 0;
5780
5781 /* Found consecutive range of addresses. */
5782 ret = 1;
5783 }
c906108c 5784 else
af34e669 5785 {
e142c38c 5786 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5787 if (attr != NULL)
5788 {
af34e669 5789 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5790 .debug_ranges section. */
d85a05f0 5791 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5792 return 0;
43039443 5793 /* Found discontinuous range of addresses. */
af34e669
DJ
5794 ret = -1;
5795 }
5796 }
c906108c
SS
5797
5798 if (high < low)
5799 return 0;
5800
5801 /* When using the GNU linker, .gnu.linkonce. sections are used to
5802 eliminate duplicate copies of functions and vtables and such.
5803 The linker will arbitrarily choose one and discard the others.
5804 The AT_*_pc values for such functions refer to local labels in
5805 these sections. If the section from that file was discarded, the
5806 labels are not in the output, so the relocs get a value of 0.
5807 If this is a discarded function, mark the pc bounds as invalid,
5808 so that GDB will ignore it. */
72dca2f5 5809 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5810 return 0;
5811
5812 *lowpc = low;
5813 *highpc = high;
af34e669 5814 return ret;
c906108c
SS
5815}
5816
b084d499
JB
5817/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5818 its low and high PC addresses. Do nothing if these addresses could not
5819 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5820 and HIGHPC to the high address if greater than HIGHPC. */
5821
5822static void
5823dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5824 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5825 struct dwarf2_cu *cu)
5826{
5827 CORE_ADDR low, high;
5828 struct die_info *child = die->child;
5829
d85a05f0 5830 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5831 {
5832 *lowpc = min (*lowpc, low);
5833 *highpc = max (*highpc, high);
5834 }
5835
5836 /* If the language does not allow nested subprograms (either inside
5837 subprograms or lexical blocks), we're done. */
5838 if (cu->language != language_ada)
5839 return;
6e70227d 5840
b084d499
JB
5841 /* Check all the children of the given DIE. If it contains nested
5842 subprograms, then check their pc bounds. Likewise, we need to
5843 check lexical blocks as well, as they may also contain subprogram
5844 definitions. */
5845 while (child && child->tag)
5846 {
5847 if (child->tag == DW_TAG_subprogram
5848 || child->tag == DW_TAG_lexical_block)
5849 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5850 child = sibling_die (child);
5851 }
5852}
5853
fae299cd
DC
5854/* Get the low and high pc's represented by the scope DIE, and store
5855 them in *LOWPC and *HIGHPC. If the correct values can't be
5856 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5857
5858static void
5859get_scope_pc_bounds (struct die_info *die,
5860 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5861 struct dwarf2_cu *cu)
5862{
5863 CORE_ADDR best_low = (CORE_ADDR) -1;
5864 CORE_ADDR best_high = (CORE_ADDR) 0;
5865 CORE_ADDR current_low, current_high;
5866
d85a05f0 5867 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5868 {
5869 best_low = current_low;
5870 best_high = current_high;
5871 }
5872 else
5873 {
5874 struct die_info *child = die->child;
5875
5876 while (child && child->tag)
5877 {
5878 switch (child->tag) {
5879 case DW_TAG_subprogram:
b084d499 5880 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5881 break;
5882 case DW_TAG_namespace:
f55ee35c 5883 case DW_TAG_module:
fae299cd
DC
5884 /* FIXME: carlton/2004-01-16: Should we do this for
5885 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5886 that current GCC's always emit the DIEs corresponding
5887 to definitions of methods of classes as children of a
5888 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5889 the DIEs giving the declarations, which could be
5890 anywhere). But I don't see any reason why the
5891 standards says that they have to be there. */
5892 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5893
5894 if (current_low != ((CORE_ADDR) -1))
5895 {
5896 best_low = min (best_low, current_low);
5897 best_high = max (best_high, current_high);
5898 }
5899 break;
5900 default:
5901 /* Ignore. */
5902 break;
5903 }
5904
5905 child = sibling_die (child);
5906 }
5907 }
5908
5909 *lowpc = best_low;
5910 *highpc = best_high;
5911}
5912
801e3a5b
JB
5913/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5914 in DIE. */
5915static void
5916dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5917 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5918{
5919 struct attribute *attr;
5920
5921 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5922 if (attr)
5923 {
5924 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5925
801e3a5b
JB
5926 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5927 if (attr)
5928 {
5929 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5930
801e3a5b
JB
5931 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5932 }
5933 }
5934
5935 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5936 if (attr)
5937 {
5938 bfd *obfd = cu->objfile->obfd;
5939
5940 /* The value of the DW_AT_ranges attribute is the offset of the
5941 address range list in the .debug_ranges section. */
5942 unsigned long offset = DW_UNSND (attr);
dce234bc 5943 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5944
5945 /* For some target architectures, but not others, the
5946 read_address function sign-extends the addresses it returns.
5947 To recognize base address selection entries, we need a
5948 mask. */
5949 unsigned int addr_size = cu->header.addr_size;
5950 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5951
5952 /* The base address, to which the next pair is relative. Note
5953 that this 'base' is a DWARF concept: most entries in a range
5954 list are relative, to reduce the number of relocs against the
5955 debugging information. This is separate from this function's
5956 'baseaddr' argument, which GDB uses to relocate debugging
5957 information from a shared library based on the address at
5958 which the library was loaded. */
d00adf39
DE
5959 CORE_ADDR base = cu->base_address;
5960 int base_known = cu->base_known;
801e3a5b 5961
be391dca 5962 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5963 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5964 {
5965 complaint (&symfile_complaints,
5966 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5967 offset);
5968 return;
5969 }
5970
5971 for (;;)
5972 {
5973 unsigned int bytes_read;
5974 CORE_ADDR start, end;
5975
5976 start = read_address (obfd, buffer, cu, &bytes_read);
5977 buffer += bytes_read;
5978 end = read_address (obfd, buffer, cu, &bytes_read);
5979 buffer += bytes_read;
5980
5981 /* Did we find the end of the range list? */
5982 if (start == 0 && end == 0)
5983 break;
5984
5985 /* Did we find a base address selection entry? */
5986 else if ((start & base_select_mask) == base_select_mask)
5987 {
5988 base = end;
5989 base_known = 1;
5990 }
5991
5992 /* We found an ordinary address range. */
5993 else
5994 {
5995 if (!base_known)
5996 {
5997 complaint (&symfile_complaints,
5998 _("Invalid .debug_ranges data (no base address)"));
5999 return;
6000 }
6001
6e70227d
DE
6002 record_block_range (block,
6003 baseaddr + base + start,
801e3a5b
JB
6004 baseaddr + base + end - 1);
6005 }
6006 }
6007 }
6008}
6009
c906108c
SS
6010/* Add an aggregate field to the field list. */
6011
6012static void
107d2387 6013dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6014 struct dwarf2_cu *cu)
6e70227d 6015{
e7c27a73 6016 struct objfile *objfile = cu->objfile;
5e2b427d 6017 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6018 struct nextfield *new_field;
6019 struct attribute *attr;
6020 struct field *fp;
6021 char *fieldname = "";
6022
6023 /* Allocate a new field list entry and link it in. */
6024 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6025 make_cleanup (xfree, new_field);
c906108c 6026 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6027
6028 if (die->tag == DW_TAG_inheritance)
6029 {
6030 new_field->next = fip->baseclasses;
6031 fip->baseclasses = new_field;
6032 }
6033 else
6034 {
6035 new_field->next = fip->fields;
6036 fip->fields = new_field;
6037 }
c906108c
SS
6038 fip->nfields++;
6039
6040 /* Handle accessibility and virtuality of field.
6041 The default accessibility for members is public, the default
6042 accessibility for inheritance is private. */
6043 if (die->tag != DW_TAG_inheritance)
6044 new_field->accessibility = DW_ACCESS_public;
6045 else
6046 new_field->accessibility = DW_ACCESS_private;
6047 new_field->virtuality = DW_VIRTUALITY_none;
6048
e142c38c 6049 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6050 if (attr)
6051 new_field->accessibility = DW_UNSND (attr);
6052 if (new_field->accessibility != DW_ACCESS_public)
6053 fip->non_public_fields = 1;
e142c38c 6054 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6055 if (attr)
6056 new_field->virtuality = DW_UNSND (attr);
6057
6058 fp = &new_field->field;
a9a9bd0f 6059
e142c38c 6060 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6061 {
a9a9bd0f 6062 /* Data member other than a C++ static data member. */
6e70227d 6063
c906108c 6064 /* Get type of field. */
e7c27a73 6065 fp->type = die_type (die, cu);
c906108c 6066
d6a843b5 6067 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6068
c906108c 6069 /* Get bit size of field (zero if none). */
e142c38c 6070 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6071 if (attr)
6072 {
6073 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6074 }
6075 else
6076 {
6077 FIELD_BITSIZE (*fp) = 0;
6078 }
6079
6080 /* Get bit offset of field. */
e142c38c 6081 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6082 if (attr)
6083 {
d4b96c9a 6084 int byte_offset = 0;
c6a0999f 6085
3690dd37 6086 if (attr_form_is_section_offset (attr))
d4b96c9a 6087 dwarf2_complex_location_expr_complaint ();
3690dd37 6088 else if (attr_form_is_constant (attr))
c6a0999f 6089 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6090 else if (attr_form_is_block (attr))
c6a0999f 6091 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6092 else
6093 dwarf2_complex_location_expr_complaint ();
c6a0999f 6094
d6a843b5 6095 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6096 }
e142c38c 6097 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6098 if (attr)
6099 {
5e2b427d 6100 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6101 {
6102 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6103 additional bit offset from the MSB of the containing
6104 anonymous object to the MSB of the field. We don't
6105 have to do anything special since we don't need to
6106 know the size of the anonymous object. */
c906108c
SS
6107 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6108 }
6109 else
6110 {
6111 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6112 MSB of the anonymous object, subtract off the number of
6113 bits from the MSB of the field to the MSB of the
6114 object, and then subtract off the number of bits of
6115 the field itself. The result is the bit offset of
6116 the LSB of the field. */
c906108c
SS
6117 int anonymous_size;
6118 int bit_offset = DW_UNSND (attr);
6119
e142c38c 6120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6121 if (attr)
6122 {
6123 /* The size of the anonymous object containing
6124 the bit field is explicit, so use the
6125 indicated size (in bytes). */
6126 anonymous_size = DW_UNSND (attr);
6127 }
6128 else
6129 {
6130 /* The size of the anonymous object containing
6131 the bit field must be inferred from the type
6132 attribute of the data member containing the
6133 bit field. */
6134 anonymous_size = TYPE_LENGTH (fp->type);
6135 }
6136 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6137 - bit_offset - FIELD_BITSIZE (*fp);
6138 }
6139 }
6140
6141 /* Get name of field. */
39cbfefa
DJ
6142 fieldname = dwarf2_name (die, cu);
6143 if (fieldname == NULL)
6144 fieldname = "";
d8151005
DJ
6145
6146 /* The name is already allocated along with this objfile, so we don't
6147 need to duplicate it for the type. */
6148 fp->name = fieldname;
c906108c
SS
6149
6150 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6151 pointer or virtual base class pointer) to private. */
e142c38c 6152 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6153 {
d48cc9dd 6154 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6155 new_field->accessibility = DW_ACCESS_private;
6156 fip->non_public_fields = 1;
6157 }
6158 }
a9a9bd0f 6159 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6160 {
a9a9bd0f
DC
6161 /* C++ static member. */
6162
6163 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6164 is a declaration, but all versions of G++ as of this writing
6165 (so through at least 3.2.1) incorrectly generate
6166 DW_TAG_variable tags. */
6e70227d 6167
c906108c 6168 char *physname;
c906108c 6169
a9a9bd0f 6170 /* Get name of field. */
39cbfefa
DJ
6171 fieldname = dwarf2_name (die, cu);
6172 if (fieldname == NULL)
c906108c
SS
6173 return;
6174
254e6b9e 6175 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6176 if (attr
6177 /* Only create a symbol if this is an external value.
6178 new_symbol checks this and puts the value in the global symbol
6179 table, which we want. If it is not external, new_symbol
6180 will try to put the value in cu->list_in_scope which is wrong. */
6181 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6182 {
6183 /* A static const member, not much different than an enum as far as
6184 we're concerned, except that we can support more types. */
6185 new_symbol (die, NULL, cu);
6186 }
6187
2df3850c 6188 /* Get physical name. */
94af9270 6189 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6190
d8151005
DJ
6191 /* The name is already allocated along with this objfile, so we don't
6192 need to duplicate it for the type. */
6193 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6194 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6195 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6196 }
6197 else if (die->tag == DW_TAG_inheritance)
6198 {
6199 /* C++ base class field. */
e142c38c 6200 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6201 if (attr)
d4b96c9a
JK
6202 {
6203 int byte_offset = 0;
6204
6205 if (attr_form_is_section_offset (attr))
6206 dwarf2_complex_location_expr_complaint ();
6207 else if (attr_form_is_constant (attr))
6208 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6209 else if (attr_form_is_block (attr))
6210 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6211 else
6212 dwarf2_complex_location_expr_complaint ();
6213
6214 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6215 }
c906108c 6216 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6217 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6218 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6219 fip->nbaseclasses++;
6220 }
6221}
6222
98751a41
JK
6223/* Add a typedef defined in the scope of the FIP's class. */
6224
6225static void
6226dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6227 struct dwarf2_cu *cu)
6e70227d 6228{
98751a41
JK
6229 struct objfile *objfile = cu->objfile;
6230 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6231 struct typedef_field_list *new_field;
6232 struct attribute *attr;
6233 struct typedef_field *fp;
6234 char *fieldname = "";
6235
6236 /* Allocate a new field list entry and link it in. */
6237 new_field = xzalloc (sizeof (*new_field));
6238 make_cleanup (xfree, new_field);
6239
6240 gdb_assert (die->tag == DW_TAG_typedef);
6241
6242 fp = &new_field->field;
6243
6244 /* Get name of field. */
6245 fp->name = dwarf2_name (die, cu);
6246 if (fp->name == NULL)
6247 return;
6248
6249 fp->type = read_type_die (die, cu);
6250
6251 new_field->next = fip->typedef_field_list;
6252 fip->typedef_field_list = new_field;
6253 fip->typedef_field_list_count++;
6254}
6255
c906108c
SS
6256/* Create the vector of fields, and attach it to the type. */
6257
6258static void
fba45db2 6259dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6260 struct dwarf2_cu *cu)
c906108c
SS
6261{
6262 int nfields = fip->nfields;
6263
6264 /* Record the field count, allocate space for the array of fields,
6265 and create blank accessibility bitfields if necessary. */
6266 TYPE_NFIELDS (type) = nfields;
6267 TYPE_FIELDS (type) = (struct field *)
6268 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6269 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6270
b4ba55a1 6271 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6272 {
6273 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6274
6275 TYPE_FIELD_PRIVATE_BITS (type) =
6276 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6277 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6278
6279 TYPE_FIELD_PROTECTED_BITS (type) =
6280 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6281 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6282
774b6a14
TT
6283 TYPE_FIELD_IGNORE_BITS (type) =
6284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6285 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6286 }
6287
6288 /* If the type has baseclasses, allocate and clear a bit vector for
6289 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6290 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6291 {
6292 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6293 unsigned char *pointer;
c906108c
SS
6294
6295 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6296 pointer = TYPE_ALLOC (type, num_bytes);
6297 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6298 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6299 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6300 }
6301
6302 /* Copy the saved-up fields into the field vector. Start from the head
6303 of the list, adding to the tail of the field array, so that they end
6304 up in the same order in the array in which they were added to the list. */
6305 while (nfields-- > 0)
6306 {
7d0ccb61
DJ
6307 struct nextfield *fieldp;
6308
6309 if (fip->fields)
6310 {
6311 fieldp = fip->fields;
6312 fip->fields = fieldp->next;
6313 }
6314 else
6315 {
6316 fieldp = fip->baseclasses;
6317 fip->baseclasses = fieldp->next;
6318 }
6319
6320 TYPE_FIELD (type, nfields) = fieldp->field;
6321 switch (fieldp->accessibility)
c906108c 6322 {
c5aa993b 6323 case DW_ACCESS_private:
b4ba55a1
JB
6324 if (cu->language != language_ada)
6325 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6326 break;
c906108c 6327
c5aa993b 6328 case DW_ACCESS_protected:
b4ba55a1
JB
6329 if (cu->language != language_ada)
6330 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6331 break;
c906108c 6332
c5aa993b
JM
6333 case DW_ACCESS_public:
6334 break;
c906108c 6335
c5aa993b
JM
6336 default:
6337 /* Unknown accessibility. Complain and treat it as public. */
6338 {
e2e0b3e5 6339 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6340 fieldp->accessibility);
c5aa993b
JM
6341 }
6342 break;
c906108c
SS
6343 }
6344 if (nfields < fip->nbaseclasses)
6345 {
7d0ccb61 6346 switch (fieldp->virtuality)
c906108c 6347 {
c5aa993b
JM
6348 case DW_VIRTUALITY_virtual:
6349 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6350 if (cu->language == language_ada)
6351 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6352 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6353 break;
c906108c
SS
6354 }
6355 }
c906108c
SS
6356 }
6357}
6358
c906108c
SS
6359/* Add a member function to the proper fieldlist. */
6360
6361static void
107d2387 6362dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6363 struct type *type, struct dwarf2_cu *cu)
c906108c 6364{
e7c27a73 6365 struct objfile *objfile = cu->objfile;
c906108c
SS
6366 struct attribute *attr;
6367 struct fnfieldlist *flp;
6368 int i;
6369 struct fn_field *fnp;
6370 char *fieldname;
c906108c 6371 struct nextfnfield *new_fnfield;
f792889a 6372 struct type *this_type;
c906108c 6373
b4ba55a1
JB
6374 if (cu->language == language_ada)
6375 error ("unexpected member function in Ada type");
6376
2df3850c 6377 /* Get name of member function. */
39cbfefa
DJ
6378 fieldname = dwarf2_name (die, cu);
6379 if (fieldname == NULL)
2df3850c 6380 return;
c906108c 6381
c906108c
SS
6382 /* Look up member function name in fieldlist. */
6383 for (i = 0; i < fip->nfnfields; i++)
6384 {
27bfe10e 6385 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6386 break;
6387 }
6388
6389 /* Create new list element if necessary. */
6390 if (i < fip->nfnfields)
6391 flp = &fip->fnfieldlists[i];
6392 else
6393 {
6394 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6395 {
6396 fip->fnfieldlists = (struct fnfieldlist *)
6397 xrealloc (fip->fnfieldlists,
6398 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6399 * sizeof (struct fnfieldlist));
c906108c 6400 if (fip->nfnfields == 0)
c13c43fd 6401 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6402 }
6403 flp = &fip->fnfieldlists[fip->nfnfields];
6404 flp->name = fieldname;
6405 flp->length = 0;
6406 flp->head = NULL;
3da10d80 6407 i = fip->nfnfields++;
c906108c
SS
6408 }
6409
6410 /* Create a new member function field and chain it to the field list
6411 entry. */
6412 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6413 make_cleanup (xfree, new_fnfield);
c906108c
SS
6414 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6415 new_fnfield->next = flp->head;
6416 flp->head = new_fnfield;
6417 flp->length++;
6418
6419 /* Fill in the member function field info. */
6420 fnp = &new_fnfield->fnfield;
3da10d80
KS
6421
6422 /* Delay processing of the physname until later. */
6423 if (cu->language == language_cplus || cu->language == language_java)
6424 {
6425 add_to_method_list (type, i, flp->length - 1, fieldname,
6426 die, cu);
6427 }
6428 else
6429 {
6430 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6431 fnp->physname = physname ? physname : "";
6432 }
6433
c906108c 6434 fnp->type = alloc_type (objfile);
f792889a
DJ
6435 this_type = read_type_die (die, cu);
6436 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6437 {
f792889a 6438 int nparams = TYPE_NFIELDS (this_type);
c906108c 6439
f792889a 6440 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6441 of the method itself (TYPE_CODE_METHOD). */
6442 smash_to_method_type (fnp->type, type,
f792889a
DJ
6443 TYPE_TARGET_TYPE (this_type),
6444 TYPE_FIELDS (this_type),
6445 TYPE_NFIELDS (this_type),
6446 TYPE_VARARGS (this_type));
c906108c
SS
6447
6448 /* Handle static member functions.
c5aa993b
JM
6449 Dwarf2 has no clean way to discern C++ static and non-static
6450 member functions. G++ helps GDB by marking the first
6451 parameter for non-static member functions (which is the
6452 this pointer) as artificial. We obtain this information
6453 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6454 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6455 fnp->voffset = VOFFSET_STATIC;
6456 }
6457 else
e2e0b3e5 6458 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6459 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6460
6461 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6462 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6463 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6464
6465 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6466 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6467
6468 /* Get accessibility. */
e142c38c 6469 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6470 if (attr)
6471 {
6472 switch (DW_UNSND (attr))
6473 {
c5aa993b
JM
6474 case DW_ACCESS_private:
6475 fnp->is_private = 1;
6476 break;
6477 case DW_ACCESS_protected:
6478 fnp->is_protected = 1;
6479 break;
c906108c
SS
6480 }
6481 }
6482
b02dede2 6483 /* Check for artificial methods. */
e142c38c 6484 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6485 if (attr && DW_UNSND (attr) != 0)
6486 fnp->is_artificial = 1;
6487
0d564a31 6488 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6489 function. For older versions of GCC, this is an offset in the
6490 appropriate virtual table, as specified by DW_AT_containing_type.
6491 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6492 to the object address. */
6493
e142c38c 6494 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6495 if (attr)
8e19ed76 6496 {
aec5aa8b 6497 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6498 {
aec5aa8b
TT
6499 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6500 {
6501 /* Old-style GCC. */
6502 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6503 }
6504 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6505 || (DW_BLOCK (attr)->size > 1
6506 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6507 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6508 {
6509 struct dwarf_block blk;
6510 int offset;
6511
6512 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6513 ? 1 : 2);
6514 blk.size = DW_BLOCK (attr)->size - offset;
6515 blk.data = DW_BLOCK (attr)->data + offset;
6516 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6517 if ((fnp->voffset % cu->header.addr_size) != 0)
6518 dwarf2_complex_location_expr_complaint ();
6519 else
6520 fnp->voffset /= cu->header.addr_size;
6521 fnp->voffset += 2;
6522 }
6523 else
6524 dwarf2_complex_location_expr_complaint ();
6525
6526 if (!fnp->fcontext)
6527 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6528 }
3690dd37 6529 else if (attr_form_is_section_offset (attr))
8e19ed76 6530 {
4d3c2250 6531 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6532 }
6533 else
6534 {
4d3c2250
KB
6535 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6536 fieldname);
8e19ed76 6537 }
0d564a31 6538 }
d48cc9dd
DJ
6539 else
6540 {
6541 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6542 if (attr && DW_UNSND (attr))
6543 {
6544 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6545 complaint (&symfile_complaints,
6546 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6547 fieldname, die->offset);
9655fd1a 6548 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6549 TYPE_CPLUS_DYNAMIC (type) = 1;
6550 }
6551 }
c906108c
SS
6552}
6553
6554/* Create the vector of member function fields, and attach it to the type. */
6555
6556static void
fba45db2 6557dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6558 struct dwarf2_cu *cu)
c906108c
SS
6559{
6560 struct fnfieldlist *flp;
6561 int total_length = 0;
6562 int i;
6563
b4ba55a1
JB
6564 if (cu->language == language_ada)
6565 error ("unexpected member functions in Ada type");
6566
c906108c
SS
6567 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6568 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6569 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6570
6571 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6572 {
6573 struct nextfnfield *nfp = flp->head;
6574 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6575 int k;
6576
6577 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6578 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6579 fn_flp->fn_fields = (struct fn_field *)
6580 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6581 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6582 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6583
6584 total_length += flp->length;
6585 }
6586
6587 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6588 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6589}
6590
1168df01
JB
6591/* Returns non-zero if NAME is the name of a vtable member in CU's
6592 language, zero otherwise. */
6593static int
6594is_vtable_name (const char *name, struct dwarf2_cu *cu)
6595{
6596 static const char vptr[] = "_vptr";
987504bb 6597 static const char vtable[] = "vtable";
1168df01 6598
987504bb
JJ
6599 /* Look for the C++ and Java forms of the vtable. */
6600 if ((cu->language == language_java
6601 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6602 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6603 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6604 return 1;
6605
6606 return 0;
6607}
6608
c0dd20ea 6609/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6610 functions, with the ABI-specified layout. If TYPE describes
6611 such a structure, smash it into a member function type.
61049d3b
DJ
6612
6613 GCC shouldn't do this; it should just output pointer to member DIEs.
6614 This is GCC PR debug/28767. */
c0dd20ea 6615
0b92b5bb
TT
6616static void
6617quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6618{
0b92b5bb 6619 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6620
6621 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6622 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6623 return;
c0dd20ea
DJ
6624
6625 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6626 if (TYPE_FIELD_NAME (type, 0) == NULL
6627 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6628 || TYPE_FIELD_NAME (type, 1) == NULL
6629 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6630 return;
c0dd20ea
DJ
6631
6632 /* Find the type of the method. */
0b92b5bb 6633 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6634 if (pfn_type == NULL
6635 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6636 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6637 return;
c0dd20ea
DJ
6638
6639 /* Look for the "this" argument. */
6640 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6641 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6642 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6643 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6644 return;
c0dd20ea
DJ
6645
6646 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6647 new_type = alloc_type (objfile);
6648 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6649 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6650 TYPE_VARARGS (pfn_type));
0b92b5bb 6651 smash_to_methodptr_type (type, new_type);
c0dd20ea 6652}
1168df01 6653
c906108c 6654/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6655 (definition) to create a type for the structure or union. Fill in
6656 the type's name and general properties; the members will not be
6657 processed until process_structure_type.
c906108c 6658
c767944b
DJ
6659 NOTE: we need to call these functions regardless of whether or not the
6660 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6661 structure or union. This gets the type entered into our set of
6662 user defined types.
6663
6664 However, if the structure is incomplete (an opaque struct/union)
6665 then suppress creating a symbol table entry for it since gdb only
6666 wants to find the one with the complete definition. Note that if
6667 it is complete, we just call new_symbol, which does it's own
6668 checking about whether the struct/union is anonymous or not (and
6669 suppresses creating a symbol table entry itself). */
6670
f792889a 6671static struct type *
134d01f1 6672read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6673{
e7c27a73 6674 struct objfile *objfile = cu->objfile;
c906108c
SS
6675 struct type *type;
6676 struct attribute *attr;
39cbfefa 6677 char *name;
c906108c 6678
348e048f
DE
6679 /* If the definition of this type lives in .debug_types, read that type.
6680 Don't follow DW_AT_specification though, that will take us back up
6681 the chain and we want to go down. */
6682 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6683 if (attr)
6684 {
6685 struct dwarf2_cu *type_cu = cu;
6686 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6687
348e048f
DE
6688 /* We could just recurse on read_structure_type, but we need to call
6689 get_die_type to ensure only one type for this DIE is created.
6690 This is important, for example, because for c++ classes we need
6691 TYPE_NAME set which is only done by new_symbol. Blech. */
6692 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6693
6694 /* TYPE_CU may not be the same as CU.
6695 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6696 return set_die_type (die, type, cu);
6697 }
6698
c0dd20ea 6699 type = alloc_type (objfile);
c906108c 6700 INIT_CPLUS_SPECIFIC (type);
93311388 6701
39cbfefa
DJ
6702 name = dwarf2_name (die, cu);
6703 if (name != NULL)
c906108c 6704 {
987504bb
JJ
6705 if (cu->language == language_cplus
6706 || cu->language == language_java)
63d06c5c 6707 {
3da10d80
KS
6708 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6709
6710 /* dwarf2_full_name might have already finished building the DIE's
6711 type. If so, there is no need to continue. */
6712 if (get_die_type (die, cu) != NULL)
6713 return get_die_type (die, cu);
6714
6715 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6716 if (die->tag == DW_TAG_structure_type
6717 || die->tag == DW_TAG_class_type)
6718 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6719 }
6720 else
6721 {
d8151005
DJ
6722 /* The name is already allocated along with this objfile, so
6723 we don't need to duplicate it for the type. */
94af9270
KS
6724 TYPE_TAG_NAME (type) = (char *) name;
6725 if (die->tag == DW_TAG_class_type)
6726 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6727 }
c906108c
SS
6728 }
6729
6730 if (die->tag == DW_TAG_structure_type)
6731 {
6732 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6733 }
6734 else if (die->tag == DW_TAG_union_type)
6735 {
6736 TYPE_CODE (type) = TYPE_CODE_UNION;
6737 }
6738 else
6739 {
c906108c
SS
6740 TYPE_CODE (type) = TYPE_CODE_CLASS;
6741 }
6742
0cc2414c
TT
6743 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6744 TYPE_DECLARED_CLASS (type) = 1;
6745
e142c38c 6746 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6747 if (attr)
6748 {
6749 TYPE_LENGTH (type) = DW_UNSND (attr);
6750 }
6751 else
6752 {
6753 TYPE_LENGTH (type) = 0;
6754 }
6755
876cecd0 6756 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6757 if (die_is_declaration (die, cu))
876cecd0 6758 TYPE_STUB (type) = 1;
a6c727b2
DJ
6759 else if (attr == NULL && die->child == NULL
6760 && producer_is_realview (cu->producer))
6761 /* RealView does not output the required DW_AT_declaration
6762 on incomplete types. */
6763 TYPE_STUB (type) = 1;
dc718098 6764
c906108c
SS
6765 /* We need to add the type field to the die immediately so we don't
6766 infinitely recurse when dealing with pointers to the structure
6767 type within the structure itself. */
1c379e20 6768 set_die_type (die, type, cu);
c906108c 6769
7e314c57
JK
6770 /* set_die_type should be already done. */
6771 set_descriptive_type (type, die, cu);
6772
c767944b
DJ
6773 return type;
6774}
6775
6776/* Finish creating a structure or union type, including filling in
6777 its members and creating a symbol for it. */
6778
6779static void
6780process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6781{
6782 struct objfile *objfile = cu->objfile;
6783 struct die_info *child_die = die->child;
6784 struct type *type;
6785
6786 type = get_die_type (die, cu);
6787 if (type == NULL)
6788 type = read_structure_type (die, cu);
6789
e142c38c 6790 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6791 {
6792 struct field_info fi;
6793 struct die_info *child_die;
34eaf542 6794 VEC (symbolp) *template_args = NULL;
c767944b 6795 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6796
6797 memset (&fi, 0, sizeof (struct field_info));
6798
639d11d3 6799 child_die = die->child;
c906108c
SS
6800
6801 while (child_die && child_die->tag)
6802 {
a9a9bd0f
DC
6803 if (child_die->tag == DW_TAG_member
6804 || child_die->tag == DW_TAG_variable)
c906108c 6805 {
a9a9bd0f
DC
6806 /* NOTE: carlton/2002-11-05: A C++ static data member
6807 should be a DW_TAG_member that is a declaration, but
6808 all versions of G++ as of this writing (so through at
6809 least 3.2.1) incorrectly generate DW_TAG_variable
6810 tags for them instead. */
e7c27a73 6811 dwarf2_add_field (&fi, child_die, cu);
c906108c 6812 }
8713b1b1 6813 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6814 {
6815 /* C++ member function. */
e7c27a73 6816 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6817 }
6818 else if (child_die->tag == DW_TAG_inheritance)
6819 {
6820 /* C++ base class field. */
e7c27a73 6821 dwarf2_add_field (&fi, child_die, cu);
c906108c 6822 }
98751a41
JK
6823 else if (child_die->tag == DW_TAG_typedef)
6824 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6825 else if (child_die->tag == DW_TAG_template_type_param
6826 || child_die->tag == DW_TAG_template_value_param)
6827 {
6828 struct symbol *arg = new_symbol (child_die, NULL, cu);
6829
f1078f66
DJ
6830 if (arg != NULL)
6831 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6832 }
6833
c906108c
SS
6834 child_die = sibling_die (child_die);
6835 }
6836
34eaf542
TT
6837 /* Attach template arguments to type. */
6838 if (! VEC_empty (symbolp, template_args))
6839 {
6840 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6841 TYPE_N_TEMPLATE_ARGUMENTS (type)
6842 = VEC_length (symbolp, template_args);
6843 TYPE_TEMPLATE_ARGUMENTS (type)
6844 = obstack_alloc (&objfile->objfile_obstack,
6845 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6846 * sizeof (struct symbol *)));
6847 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6848 VEC_address (symbolp, template_args),
6849 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6850 * sizeof (struct symbol *)));
6851 VEC_free (symbolp, template_args);
6852 }
6853
c906108c
SS
6854 /* Attach fields and member functions to the type. */
6855 if (fi.nfields)
e7c27a73 6856 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6857 if (fi.nfnfields)
6858 {
e7c27a73 6859 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6860
c5aa993b 6861 /* Get the type which refers to the base class (possibly this
c906108c 6862 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6863 class from the DW_AT_containing_type attribute. This use of
6864 DW_AT_containing_type is a GNU extension. */
c906108c 6865
e142c38c 6866 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6867 {
e7c27a73 6868 struct type *t = die_containing_type (die, cu);
c906108c
SS
6869
6870 TYPE_VPTR_BASETYPE (type) = t;
6871 if (type == t)
6872 {
c906108c
SS
6873 int i;
6874
6875 /* Our own class provides vtbl ptr. */
6876 for (i = TYPE_NFIELDS (t) - 1;
6877 i >= TYPE_N_BASECLASSES (t);
6878 --i)
6879 {
6880 char *fieldname = TYPE_FIELD_NAME (t, i);
6881
1168df01 6882 if (is_vtable_name (fieldname, cu))
c906108c
SS
6883 {
6884 TYPE_VPTR_FIELDNO (type) = i;
6885 break;
6886 }
6887 }
6888
6889 /* Complain if virtual function table field not found. */
6890 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6891 complaint (&symfile_complaints,
e2e0b3e5 6892 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6893 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6894 "");
c906108c
SS
6895 }
6896 else
6897 {
6898 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6899 }
6900 }
f6235d4c
EZ
6901 else if (cu->producer
6902 && strncmp (cu->producer,
6903 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6904 {
6905 /* The IBM XLC compiler does not provide direct indication
6906 of the containing type, but the vtable pointer is
6907 always named __vfp. */
6908
6909 int i;
6910
6911 for (i = TYPE_NFIELDS (type) - 1;
6912 i >= TYPE_N_BASECLASSES (type);
6913 --i)
6914 {
6915 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6916 {
6917 TYPE_VPTR_FIELDNO (type) = i;
6918 TYPE_VPTR_BASETYPE (type) = type;
6919 break;
6920 }
6921 }
6922 }
c906108c 6923 }
98751a41
JK
6924
6925 /* Copy fi.typedef_field_list linked list elements content into the
6926 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6927 if (fi.typedef_field_list)
6928 {
6929 int i = fi.typedef_field_list_count;
6930
a0d7a4ff 6931 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6932 TYPE_TYPEDEF_FIELD_ARRAY (type)
6933 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6934 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6935
6936 /* Reverse the list order to keep the debug info elements order. */
6937 while (--i >= 0)
6938 {
6939 struct typedef_field *dest, *src;
6e70227d 6940
98751a41
JK
6941 dest = &TYPE_TYPEDEF_FIELD (type, i);
6942 src = &fi.typedef_field_list->field;
6943 fi.typedef_field_list = fi.typedef_field_list->next;
6944 *dest = *src;
6945 }
6946 }
c767944b
DJ
6947
6948 do_cleanups (back_to);
c906108c 6949 }
63d06c5c 6950
0b92b5bb
TT
6951 quirk_gcc_member_function_pointer (type, cu->objfile);
6952
90aeadfc
DC
6953 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6954 snapshots) has been known to create a die giving a declaration
6955 for a class that has, as a child, a die giving a definition for a
6956 nested class. So we have to process our children even if the
6957 current die is a declaration. Normally, of course, a declaration
6958 won't have any children at all. */
134d01f1 6959
90aeadfc
DC
6960 while (child_die != NULL && child_die->tag)
6961 {
6962 if (child_die->tag == DW_TAG_member
6963 || child_die->tag == DW_TAG_variable
34eaf542
TT
6964 || child_die->tag == DW_TAG_inheritance
6965 || child_die->tag == DW_TAG_template_value_param
6966 || child_die->tag == DW_TAG_template_type_param)
134d01f1 6967 {
90aeadfc 6968 /* Do nothing. */
134d01f1 6969 }
90aeadfc
DC
6970 else
6971 process_die (child_die, cu);
134d01f1 6972
90aeadfc 6973 child_die = sibling_die (child_die);
134d01f1
DJ
6974 }
6975
fa4028e9
JB
6976 /* Do not consider external references. According to the DWARF standard,
6977 these DIEs are identified by the fact that they have no byte_size
6978 attribute, and a declaration attribute. */
6979 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6980 || !die_is_declaration (die, cu))
c767944b 6981 new_symbol (die, type, cu);
134d01f1
DJ
6982}
6983
6984/* Given a DW_AT_enumeration_type die, set its type. We do not
6985 complete the type's fields yet, or create any symbols. */
c906108c 6986
f792889a 6987static struct type *
134d01f1 6988read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6989{
e7c27a73 6990 struct objfile *objfile = cu->objfile;
c906108c 6991 struct type *type;
c906108c 6992 struct attribute *attr;
0114d602 6993 const char *name;
134d01f1 6994
348e048f
DE
6995 /* If the definition of this type lives in .debug_types, read that type.
6996 Don't follow DW_AT_specification though, that will take us back up
6997 the chain and we want to go down. */
6998 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6999 if (attr)
7000 {
7001 struct dwarf2_cu *type_cu = cu;
7002 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7003
348e048f 7004 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7005
7006 /* TYPE_CU may not be the same as CU.
7007 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7008 return set_die_type (die, type, cu);
7009 }
7010
c906108c
SS
7011 type = alloc_type (objfile);
7012
7013 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7014 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7015 if (name != NULL)
0114d602 7016 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7017
e142c38c 7018 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7019 if (attr)
7020 {
7021 TYPE_LENGTH (type) = DW_UNSND (attr);
7022 }
7023 else
7024 {
7025 TYPE_LENGTH (type) = 0;
7026 }
7027
137033e9
JB
7028 /* The enumeration DIE can be incomplete. In Ada, any type can be
7029 declared as private in the package spec, and then defined only
7030 inside the package body. Such types are known as Taft Amendment
7031 Types. When another package uses such a type, an incomplete DIE
7032 may be generated by the compiler. */
02eb380e 7033 if (die_is_declaration (die, cu))
876cecd0 7034 TYPE_STUB (type) = 1;
02eb380e 7035
f792889a 7036 return set_die_type (die, type, cu);
134d01f1
DJ
7037}
7038
7039/* Given a pointer to a die which begins an enumeration, process all
7040 the dies that define the members of the enumeration, and create the
7041 symbol for the enumeration type.
7042
7043 NOTE: We reverse the order of the element list. */
7044
7045static void
7046process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7047{
f792889a 7048 struct type *this_type;
134d01f1 7049
f792889a
DJ
7050 this_type = get_die_type (die, cu);
7051 if (this_type == NULL)
7052 this_type = read_enumeration_type (die, cu);
9dc481d3 7053
639d11d3 7054 if (die->child != NULL)
c906108c 7055 {
9dc481d3
DE
7056 struct die_info *child_die;
7057 struct symbol *sym;
7058 struct field *fields = NULL;
7059 int num_fields = 0;
7060 int unsigned_enum = 1;
7061 char *name;
7062
639d11d3 7063 child_die = die->child;
c906108c
SS
7064 while (child_die && child_die->tag)
7065 {
7066 if (child_die->tag != DW_TAG_enumerator)
7067 {
e7c27a73 7068 process_die (child_die, cu);
c906108c
SS
7069 }
7070 else
7071 {
39cbfefa
DJ
7072 name = dwarf2_name (child_die, cu);
7073 if (name)
c906108c 7074 {
f792889a 7075 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7076 if (SYMBOL_VALUE (sym) < 0)
7077 unsigned_enum = 0;
7078
7079 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7080 {
7081 fields = (struct field *)
7082 xrealloc (fields,
7083 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7084 * sizeof (struct field));
c906108c
SS
7085 }
7086
3567439c 7087 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7088 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7089 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7090 FIELD_BITSIZE (fields[num_fields]) = 0;
7091
7092 num_fields++;
7093 }
7094 }
7095
7096 child_die = sibling_die (child_die);
7097 }
7098
7099 if (num_fields)
7100 {
f792889a
DJ
7101 TYPE_NFIELDS (this_type) = num_fields;
7102 TYPE_FIELDS (this_type) = (struct field *)
7103 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7104 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7105 sizeof (struct field) * num_fields);
b8c9b27d 7106 xfree (fields);
c906108c
SS
7107 }
7108 if (unsigned_enum)
876cecd0 7109 TYPE_UNSIGNED (this_type) = 1;
c906108c 7110 }
134d01f1 7111
f792889a 7112 new_symbol (die, this_type, cu);
c906108c
SS
7113}
7114
7115/* Extract all information from a DW_TAG_array_type DIE and put it in
7116 the DIE's type field. For now, this only handles one dimensional
7117 arrays. */
7118
f792889a 7119static struct type *
e7c27a73 7120read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7121{
e7c27a73 7122 struct objfile *objfile = cu->objfile;
c906108c 7123 struct die_info *child_die;
7e314c57 7124 struct type *type;
c906108c
SS
7125 struct type *element_type, *range_type, *index_type;
7126 struct type **range_types = NULL;
7127 struct attribute *attr;
7128 int ndim = 0;
7129 struct cleanup *back_to;
39cbfefa 7130 char *name;
c906108c 7131
e7c27a73 7132 element_type = die_type (die, cu);
c906108c 7133
7e314c57
JK
7134 /* The die_type call above may have already set the type for this DIE. */
7135 type = get_die_type (die, cu);
7136 if (type)
7137 return type;
7138
c906108c
SS
7139 /* Irix 6.2 native cc creates array types without children for
7140 arrays with unspecified length. */
639d11d3 7141 if (die->child == NULL)
c906108c 7142 {
46bf5051 7143 index_type = objfile_type (objfile)->builtin_int;
c906108c 7144 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7145 type = create_array_type (NULL, element_type, range_type);
7146 return set_die_type (die, type, cu);
c906108c
SS
7147 }
7148
7149 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7150 child_die = die->child;
c906108c
SS
7151 while (child_die && child_die->tag)
7152 {
7153 if (child_die->tag == DW_TAG_subrange_type)
7154 {
f792889a 7155 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7156
f792889a 7157 if (child_type != NULL)
a02abb62
JB
7158 {
7159 /* The range type was succesfully read. Save it for
7160 the array type creation. */
7161 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7162 {
7163 range_types = (struct type **)
7164 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7165 * sizeof (struct type *));
7166 if (ndim == 0)
7167 make_cleanup (free_current_contents, &range_types);
7168 }
f792889a 7169 range_types[ndim++] = child_type;
a02abb62 7170 }
c906108c
SS
7171 }
7172 child_die = sibling_die (child_die);
7173 }
7174
7175 /* Dwarf2 dimensions are output from left to right, create the
7176 necessary array types in backwards order. */
7ca2d3a3 7177
c906108c 7178 type = element_type;
7ca2d3a3
DL
7179
7180 if (read_array_order (die, cu) == DW_ORD_col_major)
7181 {
7182 int i = 0;
9a619af0 7183
7ca2d3a3
DL
7184 while (i < ndim)
7185 type = create_array_type (NULL, type, range_types[i++]);
7186 }
7187 else
7188 {
7189 while (ndim-- > 0)
7190 type = create_array_type (NULL, type, range_types[ndim]);
7191 }
c906108c 7192
f5f8a009
EZ
7193 /* Understand Dwarf2 support for vector types (like they occur on
7194 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7195 array type. This is not part of the Dwarf2/3 standard yet, but a
7196 custom vendor extension. The main difference between a regular
7197 array and the vector variant is that vectors are passed by value
7198 to functions. */
e142c38c 7199 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7200 if (attr)
ea37ba09 7201 make_vector_type (type);
f5f8a009 7202
dbc98a8b
KW
7203 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7204 implementation may choose to implement triple vectors using this
7205 attribute. */
7206 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7207 if (attr)
7208 {
7209 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7210 TYPE_LENGTH (type) = DW_UNSND (attr);
7211 else
7212 complaint (&symfile_complaints, _("\
7213DW_AT_byte_size for array type smaller than the total size of elements"));
7214 }
7215
39cbfefa
DJ
7216 name = dwarf2_name (die, cu);
7217 if (name)
7218 TYPE_NAME (type) = name;
6e70227d 7219
7e314c57
JK
7220 /* Install the type in the die. */
7221 set_die_type (die, type, cu);
7222
7223 /* set_die_type should be already done. */
b4ba55a1
JB
7224 set_descriptive_type (type, die, cu);
7225
c906108c
SS
7226 do_cleanups (back_to);
7227
7e314c57 7228 return type;
c906108c
SS
7229}
7230
7ca2d3a3 7231static enum dwarf_array_dim_ordering
6e70227d 7232read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7233{
7234 struct attribute *attr;
7235
7236 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7237
7238 if (attr) return DW_SND (attr);
7239
7240 /*
7241 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7242 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7243 laid out as per normal fortran.
7244
6e70227d 7245 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7246 version checking.
7247 */
7248
905e0470
PM
7249 if (cu->language == language_fortran
7250 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7251 {
7252 return DW_ORD_row_major;
7253 }
7254
6e70227d 7255 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7256 {
7257 case array_column_major:
7258 return DW_ORD_col_major;
7259 case array_row_major:
7260 default:
7261 return DW_ORD_row_major;
7262 };
7263}
7264
72019c9c
GM
7265/* Extract all information from a DW_TAG_set_type DIE and put it in
7266 the DIE's type field. */
7267
f792889a 7268static struct type *
72019c9c
GM
7269read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7270{
7e314c57
JK
7271 struct type *domain_type, *set_type;
7272 struct attribute *attr;
f792889a 7273
7e314c57
JK
7274 domain_type = die_type (die, cu);
7275
7276 /* The die_type call above may have already set the type for this DIE. */
7277 set_type = get_die_type (die, cu);
7278 if (set_type)
7279 return set_type;
7280
7281 set_type = create_set_type (NULL, domain_type);
7282
7283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7284 if (attr)
7285 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7286
f792889a 7287 return set_die_type (die, set_type, cu);
72019c9c 7288}
7ca2d3a3 7289
c906108c
SS
7290/* First cut: install each common block member as a global variable. */
7291
7292static void
e7c27a73 7293read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7294{
7295 struct die_info *child_die;
7296 struct attribute *attr;
7297 struct symbol *sym;
7298 CORE_ADDR base = (CORE_ADDR) 0;
7299
e142c38c 7300 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7301 if (attr)
7302 {
8e19ed76
PS
7303 /* Support the .debug_loc offsets */
7304 if (attr_form_is_block (attr))
7305 {
e7c27a73 7306 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7307 }
3690dd37 7308 else if (attr_form_is_section_offset (attr))
8e19ed76 7309 {
4d3c2250 7310 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7311 }
7312 else
7313 {
4d3c2250
KB
7314 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7315 "common block member");
8e19ed76 7316 }
c906108c 7317 }
639d11d3 7318 if (die->child != NULL)
c906108c 7319 {
639d11d3 7320 child_die = die->child;
c906108c
SS
7321 while (child_die && child_die->tag)
7322 {
e7c27a73 7323 sym = new_symbol (child_die, NULL, cu);
e142c38c 7324 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7325 if (sym != NULL && attr != NULL)
c906108c 7326 {
d4b96c9a
JK
7327 CORE_ADDR byte_offset = 0;
7328
7329 if (attr_form_is_section_offset (attr))
7330 dwarf2_complex_location_expr_complaint ();
7331 else if (attr_form_is_constant (attr))
7332 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7333 else if (attr_form_is_block (attr))
7334 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7335 else
7336 dwarf2_complex_location_expr_complaint ();
7337
7338 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7339 add_symbol_to_list (sym, &global_symbols);
7340 }
7341 child_die = sibling_die (child_die);
7342 }
7343 }
7344}
7345
0114d602 7346/* Create a type for a C++ namespace. */
d9fa45fe 7347
0114d602
DJ
7348static struct type *
7349read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7350{
e7c27a73 7351 struct objfile *objfile = cu->objfile;
0114d602 7352 const char *previous_prefix, *name;
9219021c 7353 int is_anonymous;
0114d602
DJ
7354 struct type *type;
7355
7356 /* For extensions, reuse the type of the original namespace. */
7357 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7358 {
7359 struct die_info *ext_die;
7360 struct dwarf2_cu *ext_cu = cu;
9a619af0 7361
0114d602
DJ
7362 ext_die = dwarf2_extension (die, &ext_cu);
7363 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7364
7365 /* EXT_CU may not be the same as CU.
7366 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7367 return set_die_type (die, type, cu);
7368 }
9219021c 7369
e142c38c 7370 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7371
7372 /* Now build the name of the current namespace. */
7373
0114d602
DJ
7374 previous_prefix = determine_prefix (die, cu);
7375 if (previous_prefix[0] != '\0')
7376 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7377 previous_prefix, name, 0, cu);
0114d602
DJ
7378
7379 /* Create the type. */
7380 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7381 objfile);
7382 TYPE_NAME (type) = (char *) name;
7383 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7384
60531b24 7385 return set_die_type (die, type, cu);
0114d602
DJ
7386}
7387
7388/* Read a C++ namespace. */
7389
7390static void
7391read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7392{
7393 struct objfile *objfile = cu->objfile;
7394 const char *name;
7395 int is_anonymous;
9219021c 7396
5c4e30ca
DC
7397 /* Add a symbol associated to this if we haven't seen the namespace
7398 before. Also, add a using directive if it's an anonymous
7399 namespace. */
9219021c 7400
f2f0e013 7401 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7402 {
7403 struct type *type;
7404
0114d602 7405 type = read_type_die (die, cu);
e7c27a73 7406 new_symbol (die, type, cu);
5c4e30ca 7407
0114d602 7408 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7409 if (is_anonymous)
0114d602
DJ
7410 {
7411 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7412
c0cc3a76 7413 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7414 NULL, &objfile->objfile_obstack);
0114d602 7415 }
5c4e30ca 7416 }
9219021c 7417
639d11d3 7418 if (die->child != NULL)
d9fa45fe 7419 {
639d11d3 7420 struct die_info *child_die = die->child;
6e70227d 7421
d9fa45fe
DC
7422 while (child_die && child_die->tag)
7423 {
e7c27a73 7424 process_die (child_die, cu);
d9fa45fe
DC
7425 child_die = sibling_die (child_die);
7426 }
7427 }
38d518c9
EZ
7428}
7429
f55ee35c
JK
7430/* Read a Fortran module as type. This DIE can be only a declaration used for
7431 imported module. Still we need that type as local Fortran "use ... only"
7432 declaration imports depend on the created type in determine_prefix. */
7433
7434static struct type *
7435read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7436{
7437 struct objfile *objfile = cu->objfile;
7438 char *module_name;
7439 struct type *type;
7440
7441 module_name = dwarf2_name (die, cu);
7442 if (!module_name)
7443 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7444 die->offset);
7445 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7446
7447 /* determine_prefix uses TYPE_TAG_NAME. */
7448 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7449
7450 return set_die_type (die, type, cu);
7451}
7452
5d7cb8df
JK
7453/* Read a Fortran module. */
7454
7455static void
7456read_module (struct die_info *die, struct dwarf2_cu *cu)
7457{
7458 struct die_info *child_die = die->child;
7459
5d7cb8df
JK
7460 while (child_die && child_die->tag)
7461 {
7462 process_die (child_die, cu);
7463 child_die = sibling_die (child_die);
7464 }
7465}
7466
38d518c9
EZ
7467/* Return the name of the namespace represented by DIE. Set
7468 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7469 namespace. */
7470
7471static const char *
e142c38c 7472namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7473{
7474 struct die_info *current_die;
7475 const char *name = NULL;
7476
7477 /* Loop through the extensions until we find a name. */
7478
7479 for (current_die = die;
7480 current_die != NULL;
f2f0e013 7481 current_die = dwarf2_extension (die, &cu))
38d518c9 7482 {
e142c38c 7483 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7484 if (name != NULL)
7485 break;
7486 }
7487
7488 /* Is it an anonymous namespace? */
7489
7490 *is_anonymous = (name == NULL);
7491 if (*is_anonymous)
7492 name = "(anonymous namespace)";
7493
7494 return name;
d9fa45fe
DC
7495}
7496
c906108c
SS
7497/* Extract all information from a DW_TAG_pointer_type DIE and add to
7498 the user defined type vector. */
7499
f792889a 7500static struct type *
e7c27a73 7501read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7502{
5e2b427d 7503 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7504 struct comp_unit_head *cu_header = &cu->header;
c906108c 7505 struct type *type;
8b2dbe47
KB
7506 struct attribute *attr_byte_size;
7507 struct attribute *attr_address_class;
7508 int byte_size, addr_class;
7e314c57
JK
7509 struct type *target_type;
7510
7511 target_type = die_type (die, cu);
c906108c 7512
7e314c57
JK
7513 /* The die_type call above may have already set the type for this DIE. */
7514 type = get_die_type (die, cu);
7515 if (type)
7516 return type;
7517
7518 type = lookup_pointer_type (target_type);
8b2dbe47 7519
e142c38c 7520 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7521 if (attr_byte_size)
7522 byte_size = DW_UNSND (attr_byte_size);
c906108c 7523 else
8b2dbe47
KB
7524 byte_size = cu_header->addr_size;
7525
e142c38c 7526 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7527 if (attr_address_class)
7528 addr_class = DW_UNSND (attr_address_class);
7529 else
7530 addr_class = DW_ADDR_none;
7531
7532 /* If the pointer size or address class is different than the
7533 default, create a type variant marked as such and set the
7534 length accordingly. */
7535 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7536 {
5e2b427d 7537 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7538 {
7539 int type_flags;
7540
849957d9 7541 type_flags = gdbarch_address_class_type_flags
5e2b427d 7542 (gdbarch, byte_size, addr_class);
876cecd0
TT
7543 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7544 == 0);
8b2dbe47
KB
7545 type = make_type_with_address_space (type, type_flags);
7546 }
7547 else if (TYPE_LENGTH (type) != byte_size)
7548 {
e2e0b3e5 7549 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7550 }
6e70227d 7551 else
9a619af0
MS
7552 {
7553 /* Should we also complain about unhandled address classes? */
7554 }
c906108c 7555 }
8b2dbe47
KB
7556
7557 TYPE_LENGTH (type) = byte_size;
f792889a 7558 return set_die_type (die, type, cu);
c906108c
SS
7559}
7560
7561/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7562 the user defined type vector. */
7563
f792889a 7564static struct type *
e7c27a73 7565read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7566{
7567 struct type *type;
7568 struct type *to_type;
7569 struct type *domain;
7570
e7c27a73
DJ
7571 to_type = die_type (die, cu);
7572 domain = die_containing_type (die, cu);
0d5de010 7573
7e314c57
JK
7574 /* The calls above may have already set the type for this DIE. */
7575 type = get_die_type (die, cu);
7576 if (type)
7577 return type;
7578
0d5de010
DJ
7579 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7580 type = lookup_methodptr_type (to_type);
7581 else
7582 type = lookup_memberptr_type (to_type, domain);
c906108c 7583
f792889a 7584 return set_die_type (die, type, cu);
c906108c
SS
7585}
7586
7587/* Extract all information from a DW_TAG_reference_type DIE and add to
7588 the user defined type vector. */
7589
f792889a 7590static struct type *
e7c27a73 7591read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7592{
e7c27a73 7593 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7594 struct type *type, *target_type;
c906108c
SS
7595 struct attribute *attr;
7596
7e314c57
JK
7597 target_type = die_type (die, cu);
7598
7599 /* The die_type call above may have already set the type for this DIE. */
7600 type = get_die_type (die, cu);
7601 if (type)
7602 return type;
7603
7604 type = lookup_reference_type (target_type);
e142c38c 7605 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7606 if (attr)
7607 {
7608 TYPE_LENGTH (type) = DW_UNSND (attr);
7609 }
7610 else
7611 {
107d2387 7612 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7613 }
f792889a 7614 return set_die_type (die, type, cu);
c906108c
SS
7615}
7616
f792889a 7617static struct type *
e7c27a73 7618read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7619{
f792889a 7620 struct type *base_type, *cv_type;
c906108c 7621
e7c27a73 7622 base_type = die_type (die, cu);
7e314c57
JK
7623
7624 /* The die_type call above may have already set the type for this DIE. */
7625 cv_type = get_die_type (die, cu);
7626 if (cv_type)
7627 return cv_type;
7628
2f608a3a
KW
7629 /* In case the const qualifier is applied to an array type, the element type
7630 is so qualified, not the array type (section 6.7.3 of C99). */
7631 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7632 {
7633 struct type *el_type, *inner_array;
7634
7635 base_type = copy_type (base_type);
7636 inner_array = base_type;
7637
7638 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7639 {
7640 TYPE_TARGET_TYPE (inner_array) =
7641 copy_type (TYPE_TARGET_TYPE (inner_array));
7642 inner_array = TYPE_TARGET_TYPE (inner_array);
7643 }
7644
7645 el_type = TYPE_TARGET_TYPE (inner_array);
7646 TYPE_TARGET_TYPE (inner_array) =
7647 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7648
7649 return set_die_type (die, base_type, cu);
7650 }
7651
f792889a
DJ
7652 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7653 return set_die_type (die, cv_type, cu);
c906108c
SS
7654}
7655
f792889a 7656static struct type *
e7c27a73 7657read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7658{
f792889a 7659 struct type *base_type, *cv_type;
c906108c 7660
e7c27a73 7661 base_type = die_type (die, cu);
7e314c57
JK
7662
7663 /* The die_type call above may have already set the type for this DIE. */
7664 cv_type = get_die_type (die, cu);
7665 if (cv_type)
7666 return cv_type;
7667
f792889a
DJ
7668 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7669 return set_die_type (die, cv_type, cu);
c906108c
SS
7670}
7671
7672/* Extract all information from a DW_TAG_string_type DIE and add to
7673 the user defined type vector. It isn't really a user defined type,
7674 but it behaves like one, with other DIE's using an AT_user_def_type
7675 attribute to reference it. */
7676
f792889a 7677static struct type *
e7c27a73 7678read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7679{
e7c27a73 7680 struct objfile *objfile = cu->objfile;
3b7538c0 7681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7682 struct type *type, *range_type, *index_type, *char_type;
7683 struct attribute *attr;
7684 unsigned int length;
7685
e142c38c 7686 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7687 if (attr)
7688 {
7689 length = DW_UNSND (attr);
7690 }
7691 else
7692 {
b21b22e0 7693 /* check for the DW_AT_byte_size attribute */
e142c38c 7694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7695 if (attr)
7696 {
7697 length = DW_UNSND (attr);
7698 }
7699 else
7700 {
7701 length = 1;
7702 }
c906108c 7703 }
6ccb9162 7704
46bf5051 7705 index_type = objfile_type (objfile)->builtin_int;
c906108c 7706 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7707 char_type = language_string_char_type (cu->language_defn, gdbarch);
7708 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7709
f792889a 7710 return set_die_type (die, type, cu);
c906108c
SS
7711}
7712
7713/* Handle DIES due to C code like:
7714
7715 struct foo
c5aa993b
JM
7716 {
7717 int (*funcp)(int a, long l);
7718 int b;
7719 };
c906108c
SS
7720
7721 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7722 */
c906108c 7723
f792889a 7724static struct type *
e7c27a73 7725read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7726{
7727 struct type *type; /* Type that this function returns */
7728 struct type *ftype; /* Function that returns above type */
7729 struct attribute *attr;
7730
e7c27a73 7731 type = die_type (die, cu);
7e314c57
JK
7732
7733 /* The die_type call above may have already set the type for this DIE. */
7734 ftype = get_die_type (die, cu);
7735 if (ftype)
7736 return ftype;
7737
0c8b41f1 7738 ftype = lookup_function_type (type);
c906108c 7739
5b8101ae 7740 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7741 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7742 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7743 || cu->language == language_cplus
5b8101ae
PM
7744 || cu->language == language_java
7745 || cu->language == language_pascal)
876cecd0 7746 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7747 else if (producer_is_realview (cu->producer))
7748 /* RealView does not emit DW_AT_prototyped. We can not
7749 distinguish prototyped and unprototyped functions; default to
7750 prototyped, since that is more common in modern code (and
7751 RealView warns about unprototyped functions). */
7752 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7753
c055b101
CV
7754 /* Store the calling convention in the type if it's available in
7755 the subroutine die. Otherwise set the calling convention to
7756 the default value DW_CC_normal. */
7757 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7758 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7759
7760 /* We need to add the subroutine type to the die immediately so
7761 we don't infinitely recurse when dealing with parameters
7762 declared as the same subroutine type. */
7763 set_die_type (die, ftype, cu);
6e70227d 7764
639d11d3 7765 if (die->child != NULL)
c906108c 7766 {
8072405b 7767 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7768 struct die_info *child_die;
8072405b 7769 int nparams, iparams;
c906108c
SS
7770
7771 /* Count the number of parameters.
7772 FIXME: GDB currently ignores vararg functions, but knows about
7773 vararg member functions. */
8072405b 7774 nparams = 0;
639d11d3 7775 child_die = die->child;
c906108c
SS
7776 while (child_die && child_die->tag)
7777 {
7778 if (child_die->tag == DW_TAG_formal_parameter)
7779 nparams++;
7780 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7781 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7782 child_die = sibling_die (child_die);
7783 }
7784
7785 /* Allocate storage for parameters and fill them in. */
7786 TYPE_NFIELDS (ftype) = nparams;
7787 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7788 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7789
8072405b
JK
7790 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7791 even if we error out during the parameters reading below. */
7792 for (iparams = 0; iparams < nparams; iparams++)
7793 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7794
7795 iparams = 0;
639d11d3 7796 child_die = die->child;
c906108c
SS
7797 while (child_die && child_die->tag)
7798 {
7799 if (child_die->tag == DW_TAG_formal_parameter)
7800 {
3ce3b1ba
PA
7801 struct type *arg_type;
7802
7803 /* DWARF version 2 has no clean way to discern C++
7804 static and non-static member functions. G++ helps
7805 GDB by marking the first parameter for non-static
7806 member functions (which is the this pointer) as
7807 artificial. We pass this information to
7808 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7809
7810 DWARF version 3 added DW_AT_object_pointer, which GCC
7811 4.5 does not yet generate. */
e142c38c 7812 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7813 if (attr)
7814 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7815 else
418835cc
KS
7816 {
7817 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7818
7819 /* GCC/43521: In java, the formal parameter
7820 "this" is sometimes not marked with DW_AT_artificial. */
7821 if (cu->language == language_java)
7822 {
7823 const char *name = dwarf2_name (child_die, cu);
9a619af0 7824
418835cc
KS
7825 if (name && !strcmp (name, "this"))
7826 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7827 }
7828 }
3ce3b1ba
PA
7829 arg_type = die_type (child_die, cu);
7830
7831 /* RealView does not mark THIS as const, which the testsuite
7832 expects. GCC marks THIS as const in method definitions,
7833 but not in the class specifications (GCC PR 43053). */
7834 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7835 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7836 {
7837 int is_this = 0;
7838 struct dwarf2_cu *arg_cu = cu;
7839 const char *name = dwarf2_name (child_die, cu);
7840
7841 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7842 if (attr)
7843 {
7844 /* If the compiler emits this, use it. */
7845 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7846 is_this = 1;
7847 }
7848 else if (name && strcmp (name, "this") == 0)
7849 /* Function definitions will have the argument names. */
7850 is_this = 1;
7851 else if (name == NULL && iparams == 0)
7852 /* Declarations may not have the names, so like
7853 elsewhere in GDB, assume an artificial first
7854 argument is "this". */
7855 is_this = 1;
7856
7857 if (is_this)
7858 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7859 arg_type, 0);
7860 }
7861
7862 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7863 iparams++;
7864 }
7865 child_die = sibling_die (child_die);
7866 }
7867 }
7868
76c10ea2 7869 return ftype;
c906108c
SS
7870}
7871
f792889a 7872static struct type *
e7c27a73 7873read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7874{
e7c27a73 7875 struct objfile *objfile = cu->objfile;
0114d602 7876 const char *name = NULL;
f792889a 7877 struct type *this_type;
c906108c 7878
94af9270 7879 name = dwarf2_full_name (NULL, die, cu);
f792889a 7880 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7881 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7882 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7883 set_die_type (die, this_type, cu);
7884 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7885 return this_type;
c906108c
SS
7886}
7887
7888/* Find a representation of a given base type and install
7889 it in the TYPE field of the die. */
7890
f792889a 7891static struct type *
e7c27a73 7892read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7893{
e7c27a73 7894 struct objfile *objfile = cu->objfile;
c906108c
SS
7895 struct type *type;
7896 struct attribute *attr;
7897 int encoding = 0, size = 0;
39cbfefa 7898 char *name;
6ccb9162
UW
7899 enum type_code code = TYPE_CODE_INT;
7900 int type_flags = 0;
7901 struct type *target_type = NULL;
c906108c 7902
e142c38c 7903 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7904 if (attr)
7905 {
7906 encoding = DW_UNSND (attr);
7907 }
e142c38c 7908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7909 if (attr)
7910 {
7911 size = DW_UNSND (attr);
7912 }
39cbfefa 7913 name = dwarf2_name (die, cu);
6ccb9162 7914 if (!name)
c906108c 7915 {
6ccb9162
UW
7916 complaint (&symfile_complaints,
7917 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7918 }
6ccb9162
UW
7919
7920 switch (encoding)
c906108c 7921 {
6ccb9162
UW
7922 case DW_ATE_address:
7923 /* Turn DW_ATE_address into a void * pointer. */
7924 code = TYPE_CODE_PTR;
7925 type_flags |= TYPE_FLAG_UNSIGNED;
7926 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7927 break;
7928 case DW_ATE_boolean:
7929 code = TYPE_CODE_BOOL;
7930 type_flags |= TYPE_FLAG_UNSIGNED;
7931 break;
7932 case DW_ATE_complex_float:
7933 code = TYPE_CODE_COMPLEX;
7934 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7935 break;
7936 case DW_ATE_decimal_float:
7937 code = TYPE_CODE_DECFLOAT;
7938 break;
7939 case DW_ATE_float:
7940 code = TYPE_CODE_FLT;
7941 break;
7942 case DW_ATE_signed:
7943 break;
7944 case DW_ATE_unsigned:
7945 type_flags |= TYPE_FLAG_UNSIGNED;
7946 break;
7947 case DW_ATE_signed_char:
6e70227d 7948 if (cu->language == language_ada || cu->language == language_m2
868a0084 7949 || cu->language == language_pascal)
6ccb9162
UW
7950 code = TYPE_CODE_CHAR;
7951 break;
7952 case DW_ATE_unsigned_char:
868a0084
PM
7953 if (cu->language == language_ada || cu->language == language_m2
7954 || cu->language == language_pascal)
6ccb9162
UW
7955 code = TYPE_CODE_CHAR;
7956 type_flags |= TYPE_FLAG_UNSIGNED;
7957 break;
75079b2b
TT
7958 case DW_ATE_UTF:
7959 /* We just treat this as an integer and then recognize the
7960 type by name elsewhere. */
7961 break;
7962
6ccb9162
UW
7963 default:
7964 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7965 dwarf_type_encoding_name (encoding));
7966 break;
c906108c 7967 }
6ccb9162 7968
0114d602
DJ
7969 type = init_type (code, size, type_flags, NULL, objfile);
7970 TYPE_NAME (type) = name;
6ccb9162
UW
7971 TYPE_TARGET_TYPE (type) = target_type;
7972
0114d602 7973 if (name && strcmp (name, "char") == 0)
876cecd0 7974 TYPE_NOSIGN (type) = 1;
0114d602 7975
f792889a 7976 return set_die_type (die, type, cu);
c906108c
SS
7977}
7978
a02abb62
JB
7979/* Read the given DW_AT_subrange DIE. */
7980
f792889a 7981static struct type *
a02abb62
JB
7982read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7983{
5e2b427d 7984 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7985 struct type *base_type;
7986 struct type *range_type;
7987 struct attribute *attr;
43bbcdc2
PH
7988 LONGEST low = 0;
7989 LONGEST high = -1;
39cbfefa 7990 char *name;
43bbcdc2 7991 LONGEST negative_mask;
e77813c8 7992
a02abb62 7993 base_type = die_type (die, cu);
953ac07e
JK
7994 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7995 check_typedef (base_type);
a02abb62 7996
7e314c57
JK
7997 /* The die_type call above may have already set the type for this DIE. */
7998 range_type = get_die_type (die, cu);
7999 if (range_type)
8000 return range_type;
8001
e142c38c 8002 if (cu->language == language_fortran)
6e70227d 8003 {
a02abb62
JB
8004 /* FORTRAN implies a lower bound of 1, if not given. */
8005 low = 1;
8006 }
8007
dd5e6932
DJ
8008 /* FIXME: For variable sized arrays either of these could be
8009 a variable rather than a constant value. We'll allow it,
8010 but we don't know how to handle it. */
e142c38c 8011 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8012 if (attr)
8013 low = dwarf2_get_attr_constant_value (attr, 0);
8014
e142c38c 8015 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8016 if (attr)
6e70227d 8017 {
e77813c8 8018 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8019 {
8020 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8021 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8022 FIXME: GDB does not yet know how to handle dynamic
8023 arrays properly, treat them as arrays with unspecified
8024 length for now.
8025
8026 FIXME: jimb/2003-09-22: GDB does not really know
8027 how to handle arrays of unspecified length
8028 either; we just represent them as zero-length
8029 arrays. Choose an appropriate upper bound given
8030 the lower bound we've computed above. */
8031 high = low - 1;
8032 }
8033 else
8034 high = dwarf2_get_attr_constant_value (attr, 1);
8035 }
e77813c8
PM
8036 else
8037 {
8038 attr = dwarf2_attr (die, DW_AT_count, cu);
8039 if (attr)
8040 {
8041 int count = dwarf2_get_attr_constant_value (attr, 1);
8042 high = low + count - 1;
8043 }
8044 }
8045
8046 /* Dwarf-2 specifications explicitly allows to create subrange types
8047 without specifying a base type.
8048 In that case, the base type must be set to the type of
8049 the lower bound, upper bound or count, in that order, if any of these
8050 three attributes references an object that has a type.
8051 If no base type is found, the Dwarf-2 specifications say that
8052 a signed integer type of size equal to the size of an address should
8053 be used.
8054 For the following C code: `extern char gdb_int [];'
8055 GCC produces an empty range DIE.
8056 FIXME: muller/2010-05-28: Possible references to object for low bound,
8057 high bound or count are not yet handled by this code.
8058 */
8059 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8060 {
8061 struct objfile *objfile = cu->objfile;
8062 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8063 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8064 struct type *int_type = objfile_type (objfile)->builtin_int;
8065
8066 /* Test "int", "long int", and "long long int" objfile types,
8067 and select the first one having a size above or equal to the
8068 architecture address size. */
8069 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8070 base_type = int_type;
8071 else
8072 {
8073 int_type = objfile_type (objfile)->builtin_long;
8074 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8075 base_type = int_type;
8076 else
8077 {
8078 int_type = objfile_type (objfile)->builtin_long_long;
8079 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8080 base_type = int_type;
8081 }
8082 }
8083 }
a02abb62 8084
6e70227d 8085 negative_mask =
43bbcdc2
PH
8086 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8087 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8088 low |= negative_mask;
8089 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8090 high |= negative_mask;
8091
a02abb62
JB
8092 range_type = create_range_type (NULL, base_type, low, high);
8093
bbb0eef6
JK
8094 /* Mark arrays with dynamic length at least as an array of unspecified
8095 length. GDB could check the boundary but before it gets implemented at
8096 least allow accessing the array elements. */
8097 if (attr && attr->form == DW_FORM_block1)
8098 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8099
39cbfefa
DJ
8100 name = dwarf2_name (die, cu);
8101 if (name)
8102 TYPE_NAME (range_type) = name;
6e70227d 8103
e142c38c 8104 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8105 if (attr)
8106 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8107
7e314c57
JK
8108 set_die_type (die, range_type, cu);
8109
8110 /* set_die_type should be already done. */
b4ba55a1
JB
8111 set_descriptive_type (range_type, die, cu);
8112
7e314c57 8113 return range_type;
a02abb62 8114}
6e70227d 8115
f792889a 8116static struct type *
81a17f79
JB
8117read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8118{
8119 struct type *type;
81a17f79 8120
81a17f79
JB
8121 /* For now, we only support the C meaning of an unspecified type: void. */
8122
0114d602
DJ
8123 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8124 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8125
f792889a 8126 return set_die_type (die, type, cu);
81a17f79 8127}
a02abb62 8128
51545339
DJ
8129/* Trivial hash function for die_info: the hash value of a DIE
8130 is its offset in .debug_info for this objfile. */
8131
8132static hashval_t
8133die_hash (const void *item)
8134{
8135 const struct die_info *die = item;
9a619af0 8136
51545339
DJ
8137 return die->offset;
8138}
8139
8140/* Trivial comparison function for die_info structures: two DIEs
8141 are equal if they have the same offset. */
8142
8143static int
8144die_eq (const void *item_lhs, const void *item_rhs)
8145{
8146 const struct die_info *die_lhs = item_lhs;
8147 const struct die_info *die_rhs = item_rhs;
9a619af0 8148
51545339
DJ
8149 return die_lhs->offset == die_rhs->offset;
8150}
8151
c906108c
SS
8152/* Read a whole compilation unit into a linked list of dies. */
8153
f9aca02d 8154static struct die_info *
93311388 8155read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8156{
93311388 8157 struct die_reader_specs reader_specs;
98bfdba5 8158 int read_abbrevs = 0;
1d9ec526 8159 struct cleanup *back_to = NULL;
98bfdba5
PA
8160 struct die_info *die;
8161
8162 if (cu->dwarf2_abbrevs == NULL)
8163 {
8164 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8165 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8166 read_abbrevs = 1;
8167 }
93311388 8168
348e048f 8169 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8170 cu->die_hash
8171 = htab_create_alloc_ex (cu->header.length / 12,
8172 die_hash,
8173 die_eq,
8174 NULL,
8175 &cu->comp_unit_obstack,
8176 hashtab_obstack_allocate,
8177 dummy_obstack_deallocate);
8178
93311388
DE
8179 init_cu_die_reader (&reader_specs, cu);
8180
98bfdba5
PA
8181 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8182
8183 if (read_abbrevs)
8184 do_cleanups (back_to);
8185
8186 return die;
639d11d3
DC
8187}
8188
d97bc12b
DE
8189/* Main entry point for reading a DIE and all children.
8190 Read the DIE and dump it if requested. */
8191
8192static struct die_info *
93311388
DE
8193read_die_and_children (const struct die_reader_specs *reader,
8194 gdb_byte *info_ptr,
d97bc12b
DE
8195 gdb_byte **new_info_ptr,
8196 struct die_info *parent)
8197{
93311388 8198 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8199 new_info_ptr, parent);
8200
8201 if (dwarf2_die_debug)
8202 {
348e048f
DE
8203 fprintf_unfiltered (gdb_stdlog,
8204 "\nRead die from %s of %s:\n",
8205 reader->buffer == dwarf2_per_objfile->info.buffer
8206 ? ".debug_info"
8207 : reader->buffer == dwarf2_per_objfile->types.buffer
8208 ? ".debug_types"
8209 : "unknown section",
8210 reader->abfd->filename);
d97bc12b
DE
8211 dump_die (result, dwarf2_die_debug);
8212 }
8213
8214 return result;
8215}
8216
639d11d3
DC
8217/* Read a single die and all its descendents. Set the die's sibling
8218 field to NULL; set other fields in the die correctly, and set all
8219 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8220 location of the info_ptr after reading all of those dies. PARENT
8221 is the parent of the die in question. */
8222
8223static struct die_info *
93311388
DE
8224read_die_and_children_1 (const struct die_reader_specs *reader,
8225 gdb_byte *info_ptr,
d97bc12b
DE
8226 gdb_byte **new_info_ptr,
8227 struct die_info *parent)
639d11d3
DC
8228{
8229 struct die_info *die;
fe1b8b76 8230 gdb_byte *cur_ptr;
639d11d3
DC
8231 int has_children;
8232
93311388 8233 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8234 if (die == NULL)
8235 {
8236 *new_info_ptr = cur_ptr;
8237 return NULL;
8238 }
93311388 8239 store_in_ref_table (die, reader->cu);
639d11d3
DC
8240
8241 if (has_children)
348e048f 8242 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8243 else
8244 {
8245 die->child = NULL;
8246 *new_info_ptr = cur_ptr;
8247 }
8248
8249 die->sibling = NULL;
8250 die->parent = parent;
8251 return die;
8252}
8253
8254/* Read a die, all of its descendents, and all of its siblings; set
8255 all of the fields of all of the dies correctly. Arguments are as
8256 in read_die_and_children. */
8257
8258static struct die_info *
93311388
DE
8259read_die_and_siblings (const struct die_reader_specs *reader,
8260 gdb_byte *info_ptr,
fe1b8b76 8261 gdb_byte **new_info_ptr,
639d11d3
DC
8262 struct die_info *parent)
8263{
8264 struct die_info *first_die, *last_sibling;
fe1b8b76 8265 gdb_byte *cur_ptr;
639d11d3 8266
c906108c 8267 cur_ptr = info_ptr;
639d11d3
DC
8268 first_die = last_sibling = NULL;
8269
8270 while (1)
c906108c 8271 {
639d11d3 8272 struct die_info *die
93311388 8273 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8274
1d325ec1 8275 if (die == NULL)
c906108c 8276 {
639d11d3
DC
8277 *new_info_ptr = cur_ptr;
8278 return first_die;
c906108c 8279 }
1d325ec1
DJ
8280
8281 if (!first_die)
8282 first_die = die;
c906108c 8283 else
1d325ec1
DJ
8284 last_sibling->sibling = die;
8285
8286 last_sibling = die;
c906108c 8287 }
c906108c
SS
8288}
8289
93311388
DE
8290/* Read the die from the .debug_info section buffer. Set DIEP to
8291 point to a newly allocated die with its information, except for its
8292 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8293 whether the die has children or not. */
8294
8295static gdb_byte *
8296read_full_die (const struct die_reader_specs *reader,
8297 struct die_info **diep, gdb_byte *info_ptr,
8298 int *has_children)
8299{
8300 unsigned int abbrev_number, bytes_read, i, offset;
8301 struct abbrev_info *abbrev;
8302 struct die_info *die;
8303 struct dwarf2_cu *cu = reader->cu;
8304 bfd *abfd = reader->abfd;
8305
8306 offset = info_ptr - reader->buffer;
8307 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8308 info_ptr += bytes_read;
8309 if (!abbrev_number)
8310 {
8311 *diep = NULL;
8312 *has_children = 0;
8313 return info_ptr;
8314 }
8315
8316 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8317 if (!abbrev)
348e048f
DE
8318 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8319 abbrev_number,
8320 bfd_get_filename (abfd));
8321
93311388
DE
8322 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8323 die->offset = offset;
8324 die->tag = abbrev->tag;
8325 die->abbrev = abbrev_number;
8326
8327 die->num_attrs = abbrev->num_attrs;
8328
8329 for (i = 0; i < abbrev->num_attrs; ++i)
8330 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8331 abfd, info_ptr, cu);
8332
8333 *diep = die;
8334 *has_children = abbrev->has_children;
8335 return info_ptr;
8336}
8337
c906108c
SS
8338/* In DWARF version 2, the description of the debugging information is
8339 stored in a separate .debug_abbrev section. Before we read any
8340 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8341 in a hash table. This function also sets flags in CU describing
8342 the data found in the abbrev table. */
c906108c
SS
8343
8344static void
e7c27a73 8345dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8346{
e7c27a73 8347 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8348 gdb_byte *abbrev_ptr;
c906108c
SS
8349 struct abbrev_info *cur_abbrev;
8350 unsigned int abbrev_number, bytes_read, abbrev_name;
8351 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8352 struct attr_abbrev *cur_attrs;
8353 unsigned int allocated_attrs;
c906108c 8354
57349743 8355 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8356 obstack_init (&cu->abbrev_obstack);
8357 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8358 (ABBREV_HASH_SIZE
8359 * sizeof (struct abbrev_info *)));
8360 memset (cu->dwarf2_abbrevs, 0,
8361 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8362
be391dca
TT
8363 dwarf2_read_section (dwarf2_per_objfile->objfile,
8364 &dwarf2_per_objfile->abbrev);
dce234bc 8365 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8366 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8367 abbrev_ptr += bytes_read;
8368
f3dd6933
DJ
8369 allocated_attrs = ATTR_ALLOC_CHUNK;
8370 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8371
c906108c
SS
8372 /* loop until we reach an abbrev number of 0 */
8373 while (abbrev_number)
8374 {
f3dd6933 8375 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8376
8377 /* read in abbrev header */
8378 cur_abbrev->number = abbrev_number;
8379 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8380 abbrev_ptr += bytes_read;
8381 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8382 abbrev_ptr += 1;
8383
72bf9492
DJ
8384 if (cur_abbrev->tag == DW_TAG_namespace)
8385 cu->has_namespace_info = 1;
8386
c906108c
SS
8387 /* now read in declarations */
8388 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8389 abbrev_ptr += bytes_read;
8390 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8391 abbrev_ptr += bytes_read;
8392 while (abbrev_name)
8393 {
f3dd6933 8394 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8395 {
f3dd6933
DJ
8396 allocated_attrs += ATTR_ALLOC_CHUNK;
8397 cur_attrs
8398 = xrealloc (cur_attrs, (allocated_attrs
8399 * sizeof (struct attr_abbrev)));
c906108c 8400 }
ae038cb0
DJ
8401
8402 /* Record whether this compilation unit might have
8403 inter-compilation-unit references. If we don't know what form
8404 this attribute will have, then it might potentially be a
8405 DW_FORM_ref_addr, so we conservatively expect inter-CU
8406 references. */
8407
8408 if (abbrev_form == DW_FORM_ref_addr
8409 || abbrev_form == DW_FORM_indirect)
8410 cu->has_form_ref_addr = 1;
8411
f3dd6933
DJ
8412 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8413 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8414 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8415 abbrev_ptr += bytes_read;
8416 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8417 abbrev_ptr += bytes_read;
8418 }
8419
f3dd6933
DJ
8420 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8421 (cur_abbrev->num_attrs
8422 * sizeof (struct attr_abbrev)));
8423 memcpy (cur_abbrev->attrs, cur_attrs,
8424 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8425
c906108c 8426 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8427 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8428 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8429
8430 /* Get next abbreviation.
8431 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8432 always properly terminated with an abbrev number of 0.
8433 Exit loop if we encounter an abbreviation which we have
8434 already read (which means we are about to read the abbreviations
8435 for the next compile unit) or if the end of the abbreviation
8436 table is reached. */
dce234bc
PP
8437 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8438 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8439 break;
8440 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8441 abbrev_ptr += bytes_read;
e7c27a73 8442 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8443 break;
8444 }
f3dd6933
DJ
8445
8446 xfree (cur_attrs);
c906108c
SS
8447}
8448
f3dd6933 8449/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8450
c906108c 8451static void
f3dd6933 8452dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8453{
f3dd6933 8454 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8455
f3dd6933
DJ
8456 obstack_free (&cu->abbrev_obstack, NULL);
8457 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8458}
8459
8460/* Lookup an abbrev_info structure in the abbrev hash table. */
8461
8462static struct abbrev_info *
e7c27a73 8463dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8464{
8465 unsigned int hash_number;
8466 struct abbrev_info *abbrev;
8467
8468 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8469 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8470
8471 while (abbrev)
8472 {
8473 if (abbrev->number == number)
8474 return abbrev;
8475 else
8476 abbrev = abbrev->next;
8477 }
8478 return NULL;
8479}
8480
72bf9492
DJ
8481/* Returns nonzero if TAG represents a type that we might generate a partial
8482 symbol for. */
8483
8484static int
8485is_type_tag_for_partial (int tag)
8486{
8487 switch (tag)
8488 {
8489#if 0
8490 /* Some types that would be reasonable to generate partial symbols for,
8491 that we don't at present. */
8492 case DW_TAG_array_type:
8493 case DW_TAG_file_type:
8494 case DW_TAG_ptr_to_member_type:
8495 case DW_TAG_set_type:
8496 case DW_TAG_string_type:
8497 case DW_TAG_subroutine_type:
8498#endif
8499 case DW_TAG_base_type:
8500 case DW_TAG_class_type:
680b30c7 8501 case DW_TAG_interface_type:
72bf9492
DJ
8502 case DW_TAG_enumeration_type:
8503 case DW_TAG_structure_type:
8504 case DW_TAG_subrange_type:
8505 case DW_TAG_typedef:
8506 case DW_TAG_union_type:
8507 return 1;
8508 default:
8509 return 0;
8510 }
8511}
8512
8513/* Load all DIEs that are interesting for partial symbols into memory. */
8514
8515static struct partial_die_info *
93311388
DE
8516load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8517 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8518{
8519 struct partial_die_info *part_die;
8520 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8521 struct abbrev_info *abbrev;
8522 unsigned int bytes_read;
5afb4e99 8523 unsigned int load_all = 0;
72bf9492
DJ
8524
8525 int nesting_level = 1;
8526
8527 parent_die = NULL;
8528 last_die = NULL;
8529
5afb4e99
DJ
8530 if (cu->per_cu && cu->per_cu->load_all_dies)
8531 load_all = 1;
8532
72bf9492
DJ
8533 cu->partial_dies
8534 = htab_create_alloc_ex (cu->header.length / 12,
8535 partial_die_hash,
8536 partial_die_eq,
8537 NULL,
8538 &cu->comp_unit_obstack,
8539 hashtab_obstack_allocate,
8540 dummy_obstack_deallocate);
8541
8542 part_die = obstack_alloc (&cu->comp_unit_obstack,
8543 sizeof (struct partial_die_info));
8544
8545 while (1)
8546 {
8547 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8548
8549 /* A NULL abbrev means the end of a series of children. */
8550 if (abbrev == NULL)
8551 {
8552 if (--nesting_level == 0)
8553 {
8554 /* PART_DIE was probably the last thing allocated on the
8555 comp_unit_obstack, so we could call obstack_free
8556 here. We don't do that because the waste is small,
8557 and will be cleaned up when we're done with this
8558 compilation unit. This way, we're also more robust
8559 against other users of the comp_unit_obstack. */
8560 return first_die;
8561 }
8562 info_ptr += bytes_read;
8563 last_die = parent_die;
8564 parent_die = parent_die->die_parent;
8565 continue;
8566 }
8567
98bfdba5
PA
8568 /* Check for template arguments. We never save these; if
8569 they're seen, we just mark the parent, and go on our way. */
8570 if (parent_die != NULL
8571 && cu->language == language_cplus
8572 && (abbrev->tag == DW_TAG_template_type_param
8573 || abbrev->tag == DW_TAG_template_value_param))
8574 {
8575 parent_die->has_template_arguments = 1;
8576
8577 if (!load_all)
8578 {
8579 /* We don't need a partial DIE for the template argument. */
8580 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8581 cu);
8582 continue;
8583 }
8584 }
8585
8586 /* We only recurse into subprograms looking for template arguments.
8587 Skip their other children. */
8588 if (!load_all
8589 && cu->language == language_cplus
8590 && parent_die != NULL
8591 && parent_die->tag == DW_TAG_subprogram)
8592 {
8593 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8594 continue;
8595 }
8596
5afb4e99
DJ
8597 /* Check whether this DIE is interesting enough to save. Normally
8598 we would not be interested in members here, but there may be
8599 later variables referencing them via DW_AT_specification (for
8600 static members). */
8601 if (!load_all
8602 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8603 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8604 && abbrev->tag != DW_TAG_enumerator
8605 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8606 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8607 && abbrev->tag != DW_TAG_variable
5afb4e99 8608 && abbrev->tag != DW_TAG_namespace
f55ee35c 8609 && abbrev->tag != DW_TAG_module
5afb4e99 8610 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8611 {
8612 /* Otherwise we skip to the next sibling, if any. */
93311388 8613 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8614 continue;
8615 }
8616
93311388
DE
8617 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8618 buffer, info_ptr, cu);
72bf9492
DJ
8619
8620 /* This two-pass algorithm for processing partial symbols has a
8621 high cost in cache pressure. Thus, handle some simple cases
8622 here which cover the majority of C partial symbols. DIEs
8623 which neither have specification tags in them, nor could have
8624 specification tags elsewhere pointing at them, can simply be
8625 processed and discarded.
8626
8627 This segment is also optional; scan_partial_symbols and
8628 add_partial_symbol will handle these DIEs if we chain
8629 them in normally. When compilers which do not emit large
8630 quantities of duplicate debug information are more common,
8631 this code can probably be removed. */
8632
8633 /* Any complete simple types at the top level (pretty much all
8634 of them, for a language without namespaces), can be processed
8635 directly. */
8636 if (parent_die == NULL
8637 && part_die->has_specification == 0
8638 && part_die->is_declaration == 0
8639 && (part_die->tag == DW_TAG_typedef
8640 || part_die->tag == DW_TAG_base_type
8641 || part_die->tag == DW_TAG_subrange_type))
8642 {
8643 if (building_psymtab && part_die->name != NULL)
04a679b8 8644 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8645 VAR_DOMAIN, LOC_TYPEDEF,
8646 &cu->objfile->static_psymbols,
8647 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8648 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8649 continue;
8650 }
8651
8652 /* If we're at the second level, and we're an enumerator, and
8653 our parent has no specification (meaning possibly lives in a
8654 namespace elsewhere), then we can add the partial symbol now
8655 instead of queueing it. */
8656 if (part_die->tag == DW_TAG_enumerator
8657 && parent_die != NULL
8658 && parent_die->die_parent == NULL
8659 && parent_die->tag == DW_TAG_enumeration_type
8660 && parent_die->has_specification == 0)
8661 {
8662 if (part_die->name == NULL)
e2e0b3e5 8663 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8664 else if (building_psymtab)
04a679b8 8665 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8666 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8667 (cu->language == language_cplus
8668 || cu->language == language_java)
72bf9492
DJ
8669 ? &cu->objfile->global_psymbols
8670 : &cu->objfile->static_psymbols,
8671 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8672
93311388 8673 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8674 continue;
8675 }
8676
8677 /* We'll save this DIE so link it in. */
8678 part_die->die_parent = parent_die;
8679 part_die->die_sibling = NULL;
8680 part_die->die_child = NULL;
8681
8682 if (last_die && last_die == parent_die)
8683 last_die->die_child = part_die;
8684 else if (last_die)
8685 last_die->die_sibling = part_die;
8686
8687 last_die = part_die;
8688
8689 if (first_die == NULL)
8690 first_die = part_die;
8691
8692 /* Maybe add the DIE to the hash table. Not all DIEs that we
8693 find interesting need to be in the hash table, because we
8694 also have the parent/sibling/child chains; only those that we
8695 might refer to by offset later during partial symbol reading.
8696
8697 For now this means things that might have be the target of a
8698 DW_AT_specification, DW_AT_abstract_origin, or
8699 DW_AT_extension. DW_AT_extension will refer only to
8700 namespaces; DW_AT_abstract_origin refers to functions (and
8701 many things under the function DIE, but we do not recurse
8702 into function DIEs during partial symbol reading) and
8703 possibly variables as well; DW_AT_specification refers to
8704 declarations. Declarations ought to have the DW_AT_declaration
8705 flag. It happens that GCC forgets to put it in sometimes, but
8706 only for functions, not for types.
8707
8708 Adding more things than necessary to the hash table is harmless
8709 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8710 wasted time in find_partial_die, when we reread the compilation
8711 unit with load_all_dies set. */
72bf9492 8712
5afb4e99 8713 if (load_all
72929c62 8714 || abbrev->tag == DW_TAG_constant
5afb4e99 8715 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8716 || abbrev->tag == DW_TAG_variable
8717 || abbrev->tag == DW_TAG_namespace
8718 || part_die->is_declaration)
8719 {
8720 void **slot;
8721
8722 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8723 part_die->offset, INSERT);
8724 *slot = part_die;
8725 }
8726
8727 part_die = obstack_alloc (&cu->comp_unit_obstack,
8728 sizeof (struct partial_die_info));
8729
8730 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8731 we have no reason to follow the children of structures; for other
98bfdba5
PA
8732 languages we have to, so that we can get at method physnames
8733 to infer fully qualified class names, for DW_AT_specification,
8734 and for C++ template arguments. For C++, we also look one level
8735 inside functions to find template arguments (if the name of the
8736 function does not already contain the template arguments).
bc30ff58
JB
8737
8738 For Ada, we need to scan the children of subprograms and lexical
8739 blocks as well because Ada allows the definition of nested
8740 entities that could be interesting for the debugger, such as
8741 nested subprograms for instance. */
72bf9492 8742 if (last_die->has_children
5afb4e99
DJ
8743 && (load_all
8744 || last_die->tag == DW_TAG_namespace
f55ee35c 8745 || last_die->tag == DW_TAG_module
72bf9492 8746 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8747 || (cu->language == language_cplus
8748 && last_die->tag == DW_TAG_subprogram
8749 && (last_die->name == NULL
8750 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8751 || (cu->language != language_c
8752 && (last_die->tag == DW_TAG_class_type
680b30c7 8753 || last_die->tag == DW_TAG_interface_type
72bf9492 8754 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8755 || last_die->tag == DW_TAG_union_type))
8756 || (cu->language == language_ada
8757 && (last_die->tag == DW_TAG_subprogram
8758 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8759 {
8760 nesting_level++;
8761 parent_die = last_die;
8762 continue;
8763 }
8764
8765 /* Otherwise we skip to the next sibling, if any. */
93311388 8766 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8767
8768 /* Back to the top, do it again. */
8769 }
8770}
8771
c906108c
SS
8772/* Read a minimal amount of information into the minimal die structure. */
8773
fe1b8b76 8774static gdb_byte *
72bf9492
DJ
8775read_partial_die (struct partial_die_info *part_die,
8776 struct abbrev_info *abbrev,
8777 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8778 gdb_byte *buffer, gdb_byte *info_ptr,
8779 struct dwarf2_cu *cu)
c906108c 8780{
fa238c03 8781 unsigned int i;
c906108c 8782 struct attribute attr;
c5aa993b 8783 int has_low_pc_attr = 0;
c906108c
SS
8784 int has_high_pc_attr = 0;
8785
72bf9492 8786 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8787
93311388 8788 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8789
8790 info_ptr += abbrev_len;
8791
8792 if (abbrev == NULL)
8793 return info_ptr;
8794
c906108c
SS
8795 part_die->tag = abbrev->tag;
8796 part_die->has_children = abbrev->has_children;
c906108c
SS
8797
8798 for (i = 0; i < abbrev->num_attrs; ++i)
8799 {
e7c27a73 8800 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8801
8802 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8803 partial symbol table. */
c906108c
SS
8804 switch (attr.name)
8805 {
8806 case DW_AT_name:
71c25dea
TT
8807 switch (part_die->tag)
8808 {
8809 case DW_TAG_compile_unit:
348e048f 8810 case DW_TAG_type_unit:
71c25dea
TT
8811 /* Compilation units have a DW_AT_name that is a filename, not
8812 a source language identifier. */
8813 case DW_TAG_enumeration_type:
8814 case DW_TAG_enumerator:
8815 /* These tags always have simple identifiers already; no need
8816 to canonicalize them. */
8817 part_die->name = DW_STRING (&attr);
8818 break;
8819 default:
8820 part_die->name
8821 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8822 &cu->objfile->objfile_obstack);
71c25dea
TT
8823 break;
8824 }
c906108c 8825 break;
31ef98ae 8826 case DW_AT_linkage_name:
c906108c 8827 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8828 /* Note that both forms of linkage name might appear. We
8829 assume they will be the same, and we only store the last
8830 one we see. */
94af9270
KS
8831 if (cu->language == language_ada)
8832 part_die->name = DW_STRING (&attr);
abc72ce4 8833 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8834 break;
8835 case DW_AT_low_pc:
8836 has_low_pc_attr = 1;
8837 part_die->lowpc = DW_ADDR (&attr);
8838 break;
8839 case DW_AT_high_pc:
8840 has_high_pc_attr = 1;
8841 part_die->highpc = DW_ADDR (&attr);
8842 break;
8843 case DW_AT_location:
8e19ed76
PS
8844 /* Support the .debug_loc offsets */
8845 if (attr_form_is_block (&attr))
8846 {
8847 part_die->locdesc = DW_BLOCK (&attr);
8848 }
3690dd37 8849 else if (attr_form_is_section_offset (&attr))
8e19ed76 8850 {
4d3c2250 8851 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8852 }
8853 else
8854 {
4d3c2250
KB
8855 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8856 "partial symbol information");
8e19ed76 8857 }
c906108c 8858 break;
c906108c
SS
8859 case DW_AT_external:
8860 part_die->is_external = DW_UNSND (&attr);
8861 break;
8862 case DW_AT_declaration:
8863 part_die->is_declaration = DW_UNSND (&attr);
8864 break;
8865 case DW_AT_type:
8866 part_die->has_type = 1;
8867 break;
8868 case DW_AT_abstract_origin:
8869 case DW_AT_specification:
72bf9492
DJ
8870 case DW_AT_extension:
8871 part_die->has_specification = 1;
c764a876 8872 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8873 break;
8874 case DW_AT_sibling:
8875 /* Ignore absolute siblings, they might point outside of
8876 the current compile unit. */
8877 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8878 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8879 else
93311388 8880 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8881 break;
fa4028e9
JB
8882 case DW_AT_byte_size:
8883 part_die->has_byte_size = 1;
8884 break;
68511cec
CES
8885 case DW_AT_calling_convention:
8886 /* DWARF doesn't provide a way to identify a program's source-level
8887 entry point. DW_AT_calling_convention attributes are only meant
8888 to describe functions' calling conventions.
8889
8890 However, because it's a necessary piece of information in
8891 Fortran, and because DW_CC_program is the only piece of debugging
8892 information whose definition refers to a 'main program' at all,
8893 several compilers have begun marking Fortran main programs with
8894 DW_CC_program --- even when those functions use the standard
8895 calling conventions.
8896
8897 So until DWARF specifies a way to provide this information and
8898 compilers pick up the new representation, we'll support this
8899 practice. */
8900 if (DW_UNSND (&attr) == DW_CC_program
8901 && cu->language == language_fortran)
01f8c46d
JK
8902 {
8903 set_main_name (part_die->name);
8904
8905 /* As this DIE has a static linkage the name would be difficult
8906 to look up later. */
8907 language_of_main = language_fortran;
8908 }
68511cec 8909 break;
c906108c
SS
8910 default:
8911 break;
8912 }
8913 }
8914
c906108c
SS
8915 /* When using the GNU linker, .gnu.linkonce. sections are used to
8916 eliminate duplicate copies of functions and vtables and such.
8917 The linker will arbitrarily choose one and discard the others.
8918 The AT_*_pc values for such functions refer to local labels in
8919 these sections. If the section from that file was discarded, the
8920 labels are not in the output, so the relocs get a value of 0.
8921 If this is a discarded function, mark the pc bounds as invalid,
8922 so that GDB will ignore it. */
8923 if (has_low_pc_attr && has_high_pc_attr
8924 && part_die->lowpc < part_die->highpc
8925 && (part_die->lowpc != 0
72dca2f5 8926 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8927 part_die->has_pc_info = 1;
85cbf3d3 8928
c906108c
SS
8929 return info_ptr;
8930}
8931
72bf9492
DJ
8932/* Find a cached partial DIE at OFFSET in CU. */
8933
8934static struct partial_die_info *
c764a876 8935find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8936{
8937 struct partial_die_info *lookup_die = NULL;
8938 struct partial_die_info part_die;
8939
8940 part_die.offset = offset;
8941 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8942
72bf9492
DJ
8943 return lookup_die;
8944}
8945
348e048f
DE
8946/* Find a partial DIE at OFFSET, which may or may not be in CU,
8947 except in the case of .debug_types DIEs which do not reference
8948 outside their CU (they do however referencing other types via
8949 DW_FORM_sig8). */
72bf9492
DJ
8950
8951static struct partial_die_info *
c764a876 8952find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8953{
5afb4e99
DJ
8954 struct dwarf2_per_cu_data *per_cu = NULL;
8955 struct partial_die_info *pd = NULL;
72bf9492 8956
348e048f
DE
8957 if (cu->per_cu->from_debug_types)
8958 {
8959 pd = find_partial_die_in_comp_unit (offset, cu);
8960 if (pd != NULL)
8961 return pd;
8962 goto not_found;
8963 }
8964
45452591 8965 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8966 {
8967 pd = find_partial_die_in_comp_unit (offset, cu);
8968 if (pd != NULL)
8969 return pd;
8970 }
72bf9492 8971
ae038cb0
DJ
8972 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8973
98bfdba5
PA
8974 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8975 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8976
8977 per_cu->cu->last_used = 0;
5afb4e99
DJ
8978 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8979
8980 if (pd == NULL && per_cu->load_all_dies == 0)
8981 {
8982 struct cleanup *back_to;
8983 struct partial_die_info comp_unit_die;
8984 struct abbrev_info *abbrev;
8985 unsigned int bytes_read;
8986 char *info_ptr;
8987
8988 per_cu->load_all_dies = 1;
8989
8990 /* Re-read the DIEs. */
8991 back_to = make_cleanup (null_cleanup, 0);
8992 if (per_cu->cu->dwarf2_abbrevs == NULL)
8993 {
8994 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8995 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8996 }
dce234bc 8997 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8998 + per_cu->cu->header.offset
8999 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9000 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9001 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9002 per_cu->cu->objfile->obfd,
9003 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9004 per_cu->cu);
9005 if (comp_unit_die.has_children)
93311388
DE
9006 load_partial_dies (per_cu->cu->objfile->obfd,
9007 dwarf2_per_objfile->info.buffer, info_ptr,
9008 0, per_cu->cu);
5afb4e99
DJ
9009 do_cleanups (back_to);
9010
9011 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9012 }
9013
348e048f
DE
9014 not_found:
9015
5afb4e99
DJ
9016 if (pd == NULL)
9017 internal_error (__FILE__, __LINE__,
c764a876 9018 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9019 offset, bfd_get_filename (cu->objfile->obfd));
9020 return pd;
72bf9492
DJ
9021}
9022
abc72ce4
DE
9023/* See if we can figure out if the class lives in a namespace. We do
9024 this by looking for a member function; its demangled name will
9025 contain namespace info, if there is any. */
9026
9027static void
9028guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9029 struct dwarf2_cu *cu)
9030{
9031 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9032 what template types look like, because the demangler
9033 frequently doesn't give the same name as the debug info. We
9034 could fix this by only using the demangled name to get the
9035 prefix (but see comment in read_structure_type). */
9036
9037 struct partial_die_info *real_pdi;
9038 struct partial_die_info *child_pdi;
9039
9040 /* If this DIE (this DIE's specification, if any) has a parent, then
9041 we should not do this. We'll prepend the parent's fully qualified
9042 name when we create the partial symbol. */
9043
9044 real_pdi = struct_pdi;
9045 while (real_pdi->has_specification)
9046 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9047
9048 if (real_pdi->die_parent != NULL)
9049 return;
9050
9051 for (child_pdi = struct_pdi->die_child;
9052 child_pdi != NULL;
9053 child_pdi = child_pdi->die_sibling)
9054 {
9055 if (child_pdi->tag == DW_TAG_subprogram
9056 && child_pdi->linkage_name != NULL)
9057 {
9058 char *actual_class_name
9059 = language_class_name_from_physname (cu->language_defn,
9060 child_pdi->linkage_name);
9061 if (actual_class_name != NULL)
9062 {
9063 struct_pdi->name
9064 = obsavestring (actual_class_name,
9065 strlen (actual_class_name),
9066 &cu->objfile->objfile_obstack);
9067 xfree (actual_class_name);
9068 }
9069 break;
9070 }
9071 }
9072}
9073
72bf9492
DJ
9074/* Adjust PART_DIE before generating a symbol for it. This function
9075 may set the is_external flag or change the DIE's name. */
9076
9077static void
9078fixup_partial_die (struct partial_die_info *part_die,
9079 struct dwarf2_cu *cu)
9080{
abc72ce4
DE
9081 /* Once we've fixed up a die, there's no point in doing so again.
9082 This also avoids a memory leak if we were to call
9083 guess_partial_die_structure_name multiple times. */
9084 if (part_die->fixup_called)
9085 return;
9086
72bf9492
DJ
9087 /* If we found a reference attribute and the DIE has no name, try
9088 to find a name in the referred to DIE. */
9089
9090 if (part_die->name == NULL && part_die->has_specification)
9091 {
9092 struct partial_die_info *spec_die;
72bf9492 9093
10b3939b 9094 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9095
10b3939b 9096 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9097
9098 if (spec_die->name)
9099 {
9100 part_die->name = spec_die->name;
9101
9102 /* Copy DW_AT_external attribute if it is set. */
9103 if (spec_die->is_external)
9104 part_die->is_external = spec_die->is_external;
9105 }
9106 }
9107
9108 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9109
9110 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9111 part_die->name = "(anonymous namespace)";
9112
abc72ce4
DE
9113 /* If there is no parent die to provide a namespace, and there are
9114 children, see if we can determine the namespace from their linkage
9115 name.
9116 NOTE: We need to do this even if cu->has_namespace_info != 0.
9117 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9118 if (cu->language == language_cplus
9119 && dwarf2_per_objfile->types.asection != NULL
9120 && part_die->die_parent == NULL
9121 && part_die->has_children
9122 && (part_die->tag == DW_TAG_class_type
9123 || part_die->tag == DW_TAG_structure_type
9124 || part_die->tag == DW_TAG_union_type))
9125 guess_partial_die_structure_name (part_die, cu);
9126
9127 part_die->fixup_called = 1;
72bf9492
DJ
9128}
9129
a8329558 9130/* Read an attribute value described by an attribute form. */
c906108c 9131
fe1b8b76 9132static gdb_byte *
a8329558 9133read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9134 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9135 struct dwarf2_cu *cu)
c906108c 9136{
e7c27a73 9137 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9138 unsigned int bytes_read;
9139 struct dwarf_block *blk;
9140
a8329558
KW
9141 attr->form = form;
9142 switch (form)
c906108c 9143 {
c906108c 9144 case DW_FORM_ref_addr:
ae411497
TT
9145 if (cu->header.version == 2)
9146 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9147 else
9148 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9149 info_ptr += bytes_read;
9150 break;
9151 case DW_FORM_addr:
e7c27a73 9152 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9153 info_ptr += bytes_read;
c906108c
SS
9154 break;
9155 case DW_FORM_block2:
7b5a2f43 9156 blk = dwarf_alloc_block (cu);
c906108c
SS
9157 blk->size = read_2_bytes (abfd, info_ptr);
9158 info_ptr += 2;
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_block4:
7b5a2f43 9164 blk = dwarf_alloc_block (cu);
c906108c
SS
9165 blk->size = read_4_bytes (abfd, info_ptr);
9166 info_ptr += 4;
9167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9168 info_ptr += blk->size;
9169 DW_BLOCK (attr) = blk;
9170 break;
9171 case DW_FORM_data2:
9172 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9173 info_ptr += 2;
9174 break;
9175 case DW_FORM_data4:
9176 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9177 info_ptr += 4;
9178 break;
9179 case DW_FORM_data8:
9180 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9181 info_ptr += 8;
9182 break;
2dc7f7b3
TT
9183 case DW_FORM_sec_offset:
9184 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9185 info_ptr += bytes_read;
9186 break;
c906108c 9187 case DW_FORM_string:
9b1c24c8 9188 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9189 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9190 info_ptr += bytes_read;
9191 break;
4bdf3d34
JJ
9192 case DW_FORM_strp:
9193 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9194 &bytes_read);
8285870a 9195 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9196 info_ptr += bytes_read;
9197 break;
2dc7f7b3 9198 case DW_FORM_exprloc:
c906108c 9199 case DW_FORM_block:
7b5a2f43 9200 blk = dwarf_alloc_block (cu);
c906108c
SS
9201 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9202 info_ptr += bytes_read;
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_block1:
7b5a2f43 9208 blk = dwarf_alloc_block (cu);
c906108c
SS
9209 blk->size = read_1_byte (abfd, info_ptr);
9210 info_ptr += 1;
9211 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9212 info_ptr += blk->size;
9213 DW_BLOCK (attr) = blk;
9214 break;
9215 case DW_FORM_data1:
9216 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9217 info_ptr += 1;
9218 break;
9219 case DW_FORM_flag:
9220 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9221 info_ptr += 1;
9222 break;
2dc7f7b3
TT
9223 case DW_FORM_flag_present:
9224 DW_UNSND (attr) = 1;
9225 break;
c906108c
SS
9226 case DW_FORM_sdata:
9227 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9228 info_ptr += bytes_read;
9229 break;
9230 case DW_FORM_udata:
9231 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9232 info_ptr += bytes_read;
9233 break;
9234 case DW_FORM_ref1:
10b3939b 9235 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9236 info_ptr += 1;
9237 break;
9238 case DW_FORM_ref2:
10b3939b 9239 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9240 info_ptr += 2;
9241 break;
9242 case DW_FORM_ref4:
10b3939b 9243 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9244 info_ptr += 4;
9245 break;
613e1657 9246 case DW_FORM_ref8:
10b3939b 9247 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9248 info_ptr += 8;
9249 break;
348e048f
DE
9250 case DW_FORM_sig8:
9251 /* Convert the signature to something we can record in DW_UNSND
9252 for later lookup.
9253 NOTE: This is NULL if the type wasn't found. */
9254 DW_SIGNATURED_TYPE (attr) =
9255 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9256 info_ptr += 8;
9257 break;
c906108c 9258 case DW_FORM_ref_udata:
10b3939b
DJ
9259 DW_ADDR (attr) = (cu->header.offset
9260 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9261 info_ptr += bytes_read;
9262 break;
c906108c 9263 case DW_FORM_indirect:
a8329558
KW
9264 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9265 info_ptr += bytes_read;
e7c27a73 9266 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9267 break;
c906108c 9268 default:
8a3fe4f8 9269 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9270 dwarf_form_name (form),
9271 bfd_get_filename (abfd));
c906108c 9272 }
28e94949
JB
9273
9274 /* We have seen instances where the compiler tried to emit a byte
9275 size attribute of -1 which ended up being encoded as an unsigned
9276 0xffffffff. Although 0xffffffff is technically a valid size value,
9277 an object of this size seems pretty unlikely so we can relatively
9278 safely treat these cases as if the size attribute was invalid and
9279 treat them as zero by default. */
9280 if (attr->name == DW_AT_byte_size
9281 && form == DW_FORM_data4
9282 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9283 {
9284 complaint
9285 (&symfile_complaints,
43bbcdc2
PH
9286 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9287 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9288 DW_UNSND (attr) = 0;
9289 }
28e94949 9290
c906108c
SS
9291 return info_ptr;
9292}
9293
a8329558
KW
9294/* Read an attribute described by an abbreviated attribute. */
9295
fe1b8b76 9296static gdb_byte *
a8329558 9297read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9298 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9299{
9300 attr->name = abbrev->name;
e7c27a73 9301 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9302}
9303
c906108c
SS
9304/* read dwarf information from a buffer */
9305
9306static unsigned int
fe1b8b76 9307read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9308{
fe1b8b76 9309 return bfd_get_8 (abfd, buf);
c906108c
SS
9310}
9311
9312static int
fe1b8b76 9313read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9314{
fe1b8b76 9315 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9316}
9317
9318static unsigned int
fe1b8b76 9319read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9320{
fe1b8b76 9321 return bfd_get_16 (abfd, buf);
c906108c
SS
9322}
9323
9324static int
fe1b8b76 9325read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9326{
fe1b8b76 9327 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9328}
9329
9330static unsigned int
fe1b8b76 9331read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9332{
fe1b8b76 9333 return bfd_get_32 (abfd, buf);
c906108c
SS
9334}
9335
9336static int
fe1b8b76 9337read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9338{
fe1b8b76 9339 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9340}
9341
93311388 9342static ULONGEST
fe1b8b76 9343read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9344{
fe1b8b76 9345 return bfd_get_64 (abfd, buf);
c906108c
SS
9346}
9347
9348static CORE_ADDR
fe1b8b76 9349read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9350 unsigned int *bytes_read)
c906108c 9351{
e7c27a73 9352 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9353 CORE_ADDR retval = 0;
9354
107d2387 9355 if (cu_header->signed_addr_p)
c906108c 9356 {
107d2387
AC
9357 switch (cu_header->addr_size)
9358 {
9359 case 2:
fe1b8b76 9360 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9361 break;
9362 case 4:
fe1b8b76 9363 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9364 break;
9365 case 8:
fe1b8b76 9366 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9367 break;
9368 default:
8e65ff28 9369 internal_error (__FILE__, __LINE__,
e2e0b3e5 9370 _("read_address: bad switch, signed [in module %s]"),
659b0389 9371 bfd_get_filename (abfd));
107d2387
AC
9372 }
9373 }
9374 else
9375 {
9376 switch (cu_header->addr_size)
9377 {
9378 case 2:
fe1b8b76 9379 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9380 break;
9381 case 4:
fe1b8b76 9382 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9383 break;
9384 case 8:
fe1b8b76 9385 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9386 break;
9387 default:
8e65ff28 9388 internal_error (__FILE__, __LINE__,
e2e0b3e5 9389 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9390 bfd_get_filename (abfd));
107d2387 9391 }
c906108c 9392 }
64367e0a 9393
107d2387
AC
9394 *bytes_read = cu_header->addr_size;
9395 return retval;
c906108c
SS
9396}
9397
f7ef9339 9398/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9399 specification allows the initial length to take up either 4 bytes
9400 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9401 bytes describe the length and all offsets will be 8 bytes in length
9402 instead of 4.
9403
f7ef9339
KB
9404 An older, non-standard 64-bit format is also handled by this
9405 function. The older format in question stores the initial length
9406 as an 8-byte quantity without an escape value. Lengths greater
9407 than 2^32 aren't very common which means that the initial 4 bytes
9408 is almost always zero. Since a length value of zero doesn't make
9409 sense for the 32-bit format, this initial zero can be considered to
9410 be an escape value which indicates the presence of the older 64-bit
9411 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9412 greater than 4GB. If it becomes necessary to handle lengths
9413 somewhat larger than 4GB, we could allow other small values (such
9414 as the non-sensical values of 1, 2, and 3) to also be used as
9415 escape values indicating the presence of the old format.
f7ef9339 9416
917c78fc
MK
9417 The value returned via bytes_read should be used to increment the
9418 relevant pointer after calling read_initial_length().
c764a876 9419
613e1657
KB
9420 [ Note: read_initial_length() and read_offset() are based on the
9421 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9422 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9423 from:
9424
f7ef9339 9425 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9426
613e1657
KB
9427 This document is only a draft and is subject to change. (So beware.)
9428
f7ef9339 9429 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9430 determined empirically by examining 64-bit ELF files produced by
9431 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9432
9433 - Kevin, July 16, 2002
613e1657
KB
9434 ] */
9435
9436static LONGEST
c764a876 9437read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9438{
fe1b8b76 9439 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9440
dd373385 9441 if (length == 0xffffffff)
613e1657 9442 {
fe1b8b76 9443 length = bfd_get_64 (abfd, buf + 4);
613e1657 9444 *bytes_read = 12;
613e1657 9445 }
dd373385 9446 else if (length == 0)
f7ef9339 9447 {
dd373385 9448 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9449 length = bfd_get_64 (abfd, buf);
f7ef9339 9450 *bytes_read = 8;
f7ef9339 9451 }
613e1657
KB
9452 else
9453 {
9454 *bytes_read = 4;
613e1657
KB
9455 }
9456
c764a876
DE
9457 return length;
9458}
dd373385 9459
c764a876
DE
9460/* Cover function for read_initial_length.
9461 Returns the length of the object at BUF, and stores the size of the
9462 initial length in *BYTES_READ and stores the size that offsets will be in
9463 *OFFSET_SIZE.
9464 If the initial length size is not equivalent to that specified in
9465 CU_HEADER then issue a complaint.
9466 This is useful when reading non-comp-unit headers. */
dd373385 9467
c764a876
DE
9468static LONGEST
9469read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9470 const struct comp_unit_head *cu_header,
9471 unsigned int *bytes_read,
9472 unsigned int *offset_size)
9473{
9474 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9475
9476 gdb_assert (cu_header->initial_length_size == 4
9477 || cu_header->initial_length_size == 8
9478 || cu_header->initial_length_size == 12);
9479
9480 if (cu_header->initial_length_size != *bytes_read)
9481 complaint (&symfile_complaints,
9482 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9483
c764a876 9484 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9485 return length;
613e1657
KB
9486}
9487
9488/* Read an offset from the data stream. The size of the offset is
917c78fc 9489 given by cu_header->offset_size. */
613e1657
KB
9490
9491static LONGEST
fe1b8b76 9492read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9493 unsigned int *bytes_read)
c764a876
DE
9494{
9495 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9496
c764a876
DE
9497 *bytes_read = cu_header->offset_size;
9498 return offset;
9499}
9500
9501/* Read an offset from the data stream. */
9502
9503static LONGEST
9504read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9505{
9506 LONGEST retval = 0;
9507
c764a876 9508 switch (offset_size)
613e1657
KB
9509 {
9510 case 4:
fe1b8b76 9511 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9512 break;
9513 case 8:
fe1b8b76 9514 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9515 break;
9516 default:
8e65ff28 9517 internal_error (__FILE__, __LINE__,
c764a876 9518 _("read_offset_1: bad switch [in module %s]"),
659b0389 9519 bfd_get_filename (abfd));
613e1657
KB
9520 }
9521
917c78fc 9522 return retval;
613e1657
KB
9523}
9524
fe1b8b76
JB
9525static gdb_byte *
9526read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9527{
9528 /* If the size of a host char is 8 bits, we can return a pointer
9529 to the buffer, otherwise we have to copy the data to a buffer
9530 allocated on the temporary obstack. */
4bdf3d34 9531 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9532 return buf;
c906108c
SS
9533}
9534
9535static char *
9b1c24c8 9536read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9537{
9538 /* If the size of a host char is 8 bits, we can return a pointer
9539 to the string, otherwise we have to copy the string to a buffer
9540 allocated on the temporary obstack. */
4bdf3d34 9541 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9542 if (*buf == '\0')
9543 {
9544 *bytes_read_ptr = 1;
9545 return NULL;
9546 }
fe1b8b76
JB
9547 *bytes_read_ptr = strlen ((char *) buf) + 1;
9548 return (char *) buf;
4bdf3d34
JJ
9549}
9550
9551static char *
fe1b8b76 9552read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9553 const struct comp_unit_head *cu_header,
9554 unsigned int *bytes_read_ptr)
9555{
c764a876 9556 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9557
be391dca 9558 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9559 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9560 {
8a3fe4f8 9561 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9562 bfd_get_filename (abfd));
4bdf3d34 9563 return NULL;
c906108c 9564 }
dce234bc 9565 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9566 {
8a3fe4f8 9567 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9568 bfd_get_filename (abfd));
c906108c
SS
9569 return NULL;
9570 }
4bdf3d34 9571 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9572 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9573 return NULL;
dce234bc 9574 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9575}
9576
ce5d95e1 9577static unsigned long
fe1b8b76 9578read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9579{
ce5d95e1
JB
9580 unsigned long result;
9581 unsigned int num_read;
c906108c
SS
9582 int i, shift;
9583 unsigned char byte;
9584
9585 result = 0;
9586 shift = 0;
9587 num_read = 0;
9588 i = 0;
9589 while (1)
9590 {
fe1b8b76 9591 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9592 buf++;
9593 num_read++;
ce5d95e1 9594 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9595 if ((byte & 128) == 0)
9596 {
9597 break;
9598 }
9599 shift += 7;
9600 }
9601 *bytes_read_ptr = num_read;
9602 return result;
9603}
9604
ce5d95e1 9605static long
fe1b8b76 9606read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9607{
ce5d95e1 9608 long result;
77e0b926 9609 int i, shift, num_read;
c906108c
SS
9610 unsigned char byte;
9611
9612 result = 0;
9613 shift = 0;
c906108c
SS
9614 num_read = 0;
9615 i = 0;
9616 while (1)
9617 {
fe1b8b76 9618 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9619 buf++;
9620 num_read++;
ce5d95e1 9621 result |= ((long)(byte & 127) << shift);
c906108c
SS
9622 shift += 7;
9623 if ((byte & 128) == 0)
9624 {
9625 break;
9626 }
9627 }
77e0b926
DJ
9628 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9629 result |= -(((long)1) << shift);
c906108c
SS
9630 *bytes_read_ptr = num_read;
9631 return result;
9632}
9633
4bb7a0a7
DJ
9634/* Return a pointer to just past the end of an LEB128 number in BUF. */
9635
fe1b8b76
JB
9636static gdb_byte *
9637skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9638{
9639 int byte;
9640
9641 while (1)
9642 {
fe1b8b76 9643 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9644 buf++;
9645 if ((byte & 128) == 0)
9646 return buf;
9647 }
9648}
9649
c906108c 9650static void
e142c38c 9651set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9652{
9653 switch (lang)
9654 {
9655 case DW_LANG_C89:
76bee0cc 9656 case DW_LANG_C99:
c906108c 9657 case DW_LANG_C:
e142c38c 9658 cu->language = language_c;
c906108c
SS
9659 break;
9660 case DW_LANG_C_plus_plus:
e142c38c 9661 cu->language = language_cplus;
c906108c 9662 break;
6aecb9c2
JB
9663 case DW_LANG_D:
9664 cu->language = language_d;
9665 break;
c906108c
SS
9666 case DW_LANG_Fortran77:
9667 case DW_LANG_Fortran90:
b21b22e0 9668 case DW_LANG_Fortran95:
e142c38c 9669 cu->language = language_fortran;
c906108c
SS
9670 break;
9671 case DW_LANG_Mips_Assembler:
e142c38c 9672 cu->language = language_asm;
c906108c 9673 break;
bebd888e 9674 case DW_LANG_Java:
e142c38c 9675 cu->language = language_java;
bebd888e 9676 break;
c906108c 9677 case DW_LANG_Ada83:
8aaf0b47 9678 case DW_LANG_Ada95:
bc5f45f8
JB
9679 cu->language = language_ada;
9680 break;
72019c9c
GM
9681 case DW_LANG_Modula2:
9682 cu->language = language_m2;
9683 break;
fe8e67fd
PM
9684 case DW_LANG_Pascal83:
9685 cu->language = language_pascal;
9686 break;
22566fbd
DJ
9687 case DW_LANG_ObjC:
9688 cu->language = language_objc;
9689 break;
c906108c
SS
9690 case DW_LANG_Cobol74:
9691 case DW_LANG_Cobol85:
c906108c 9692 default:
e142c38c 9693 cu->language = language_minimal;
c906108c
SS
9694 break;
9695 }
e142c38c 9696 cu->language_defn = language_def (cu->language);
c906108c
SS
9697}
9698
9699/* Return the named attribute or NULL if not there. */
9700
9701static struct attribute *
e142c38c 9702dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9703{
9704 unsigned int i;
9705 struct attribute *spec = NULL;
9706
9707 for (i = 0; i < die->num_attrs; ++i)
9708 {
9709 if (die->attrs[i].name == name)
10b3939b 9710 return &die->attrs[i];
c906108c
SS
9711 if (die->attrs[i].name == DW_AT_specification
9712 || die->attrs[i].name == DW_AT_abstract_origin)
9713 spec = &die->attrs[i];
9714 }
c906108c 9715
10b3939b 9716 if (spec)
f2f0e013
DJ
9717 {
9718 die = follow_die_ref (die, spec, &cu);
9719 return dwarf2_attr (die, name, cu);
9720 }
c5aa993b 9721
c906108c
SS
9722 return NULL;
9723}
9724
348e048f
DE
9725/* Return the named attribute or NULL if not there,
9726 but do not follow DW_AT_specification, etc.
9727 This is for use in contexts where we're reading .debug_types dies.
9728 Following DW_AT_specification, DW_AT_abstract_origin will take us
9729 back up the chain, and we want to go down. */
9730
9731static struct attribute *
9732dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9733 struct dwarf2_cu *cu)
9734{
9735 unsigned int i;
9736
9737 for (i = 0; i < die->num_attrs; ++i)
9738 if (die->attrs[i].name == name)
9739 return &die->attrs[i];
9740
9741 return NULL;
9742}
9743
05cf31d1
JB
9744/* Return non-zero iff the attribute NAME is defined for the given DIE,
9745 and holds a non-zero value. This function should only be used for
2dc7f7b3 9746 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9747
9748static int
9749dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9750{
9751 struct attribute *attr = dwarf2_attr (die, name, cu);
9752
9753 return (attr && DW_UNSND (attr));
9754}
9755
3ca72b44 9756static int
e142c38c 9757die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9758{
05cf31d1
JB
9759 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9760 which value is non-zero. However, we have to be careful with
9761 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9762 (via dwarf2_flag_true_p) follows this attribute. So we may
9763 end up accidently finding a declaration attribute that belongs
9764 to a different DIE referenced by the specification attribute,
9765 even though the given DIE does not have a declaration attribute. */
9766 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9767 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9768}
9769
63d06c5c 9770/* Return the die giving the specification for DIE, if there is
f2f0e013 9771 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9772 containing the return value on output. If there is no
9773 specification, but there is an abstract origin, that is
9774 returned. */
63d06c5c
DC
9775
9776static struct die_info *
f2f0e013 9777die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9778{
f2f0e013
DJ
9779 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9780 *spec_cu);
63d06c5c 9781
edb3359d
DJ
9782 if (spec_attr == NULL)
9783 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9784
63d06c5c
DC
9785 if (spec_attr == NULL)
9786 return NULL;
9787 else
f2f0e013 9788 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9789}
c906108c 9790
debd256d
JB
9791/* Free the line_header structure *LH, and any arrays and strings it
9792 refers to. */
9793static void
9794free_line_header (struct line_header *lh)
9795{
9796 if (lh->standard_opcode_lengths)
a8bc7b56 9797 xfree (lh->standard_opcode_lengths);
debd256d
JB
9798
9799 /* Remember that all the lh->file_names[i].name pointers are
9800 pointers into debug_line_buffer, and don't need to be freed. */
9801 if (lh->file_names)
a8bc7b56 9802 xfree (lh->file_names);
debd256d
JB
9803
9804 /* Similarly for the include directory names. */
9805 if (lh->include_dirs)
a8bc7b56 9806 xfree (lh->include_dirs);
debd256d 9807
a8bc7b56 9808 xfree (lh);
debd256d
JB
9809}
9810
9811
9812/* Add an entry to LH's include directory table. */
9813static void
9814add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9815{
debd256d
JB
9816 /* Grow the array if necessary. */
9817 if (lh->include_dirs_size == 0)
c5aa993b 9818 {
debd256d
JB
9819 lh->include_dirs_size = 1; /* for testing */
9820 lh->include_dirs = xmalloc (lh->include_dirs_size
9821 * sizeof (*lh->include_dirs));
9822 }
9823 else if (lh->num_include_dirs >= lh->include_dirs_size)
9824 {
9825 lh->include_dirs_size *= 2;
9826 lh->include_dirs = xrealloc (lh->include_dirs,
9827 (lh->include_dirs_size
9828 * sizeof (*lh->include_dirs)));
c5aa993b 9829 }
c906108c 9830
debd256d
JB
9831 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9832}
6e70227d 9833
debd256d
JB
9834
9835/* Add an entry to LH's file name table. */
9836static void
9837add_file_name (struct line_header *lh,
9838 char *name,
9839 unsigned int dir_index,
9840 unsigned int mod_time,
9841 unsigned int length)
9842{
9843 struct file_entry *fe;
9844
9845 /* Grow the array if necessary. */
9846 if (lh->file_names_size == 0)
9847 {
9848 lh->file_names_size = 1; /* for testing */
9849 lh->file_names = xmalloc (lh->file_names_size
9850 * sizeof (*lh->file_names));
9851 }
9852 else if (lh->num_file_names >= lh->file_names_size)
9853 {
9854 lh->file_names_size *= 2;
9855 lh->file_names = xrealloc (lh->file_names,
9856 (lh->file_names_size
9857 * sizeof (*lh->file_names)));
9858 }
9859
9860 fe = &lh->file_names[lh->num_file_names++];
9861 fe->name = name;
9862 fe->dir_index = dir_index;
9863 fe->mod_time = mod_time;
9864 fe->length = length;
aaa75496 9865 fe->included_p = 0;
cb1df416 9866 fe->symtab = NULL;
debd256d 9867}
6e70227d 9868
debd256d
JB
9869
9870/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9871 .debug_line, according to the endianness of ABFD. Return a pointer
9872 to a struct line_header, allocated using xmalloc.
debd256d
JB
9873
9874 NOTE: the strings in the include directory and file name tables of
9875 the returned object point into debug_line_buffer, and must not be
9876 freed. */
9877static struct line_header *
9878dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9879 struct dwarf2_cu *cu)
debd256d
JB
9880{
9881 struct cleanup *back_to;
9882 struct line_header *lh;
fe1b8b76 9883 gdb_byte *line_ptr;
c764a876 9884 unsigned int bytes_read, offset_size;
debd256d
JB
9885 int i;
9886 char *cur_dir, *cur_file;
9887
be391dca 9888 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9889 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9890 {
e2e0b3e5 9891 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9892 return 0;
9893 }
9894
a738430d
MK
9895 /* Make sure that at least there's room for the total_length field.
9896 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9897 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9898 {
4d3c2250 9899 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9900 return 0;
9901 }
9902
9903 lh = xmalloc (sizeof (*lh));
9904 memset (lh, 0, sizeof (*lh));
9905 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9906 (void *) lh);
9907
dce234bc 9908 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9909
a738430d 9910 /* Read in the header. */
6e70227d 9911 lh->total_length =
c764a876
DE
9912 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9913 &bytes_read, &offset_size);
debd256d 9914 line_ptr += bytes_read;
dce234bc
PP
9915 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9916 + dwarf2_per_objfile->line.size))
debd256d 9917 {
4d3c2250 9918 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9919 return 0;
9920 }
9921 lh->statement_program_end = line_ptr + lh->total_length;
9922 lh->version = read_2_bytes (abfd, line_ptr);
9923 line_ptr += 2;
c764a876
DE
9924 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9925 line_ptr += offset_size;
debd256d
JB
9926 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9927 line_ptr += 1;
2dc7f7b3
TT
9928 if (lh->version >= 4)
9929 {
9930 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9931 line_ptr += 1;
9932 }
9933 else
9934 lh->maximum_ops_per_instruction = 1;
9935
9936 if (lh->maximum_ops_per_instruction == 0)
9937 {
9938 lh->maximum_ops_per_instruction = 1;
9939 complaint (&symfile_complaints,
9940 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9941 }
9942
debd256d
JB
9943 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9944 line_ptr += 1;
9945 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9946 line_ptr += 1;
9947 lh->line_range = read_1_byte (abfd, line_ptr);
9948 line_ptr += 1;
9949 lh->opcode_base = read_1_byte (abfd, line_ptr);
9950 line_ptr += 1;
9951 lh->standard_opcode_lengths
fe1b8b76 9952 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9953
9954 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9955 for (i = 1; i < lh->opcode_base; ++i)
9956 {
9957 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9958 line_ptr += 1;
9959 }
9960
a738430d 9961 /* Read directory table. */
9b1c24c8 9962 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9963 {
9964 line_ptr += bytes_read;
9965 add_include_dir (lh, cur_dir);
9966 }
9967 line_ptr += bytes_read;
9968
a738430d 9969 /* Read file name table. */
9b1c24c8 9970 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
9971 {
9972 unsigned int dir_index, mod_time, length;
9973
9974 line_ptr += bytes_read;
9975 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9976 line_ptr += bytes_read;
9977 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9978 line_ptr += bytes_read;
9979 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9980 line_ptr += bytes_read;
9981
9982 add_file_name (lh, cur_file, dir_index, mod_time, length);
9983 }
9984 line_ptr += bytes_read;
6e70227d 9985 lh->statement_program_start = line_ptr;
debd256d 9986
dce234bc
PP
9987 if (line_ptr > (dwarf2_per_objfile->line.buffer
9988 + dwarf2_per_objfile->line.size))
4d3c2250 9989 complaint (&symfile_complaints,
e2e0b3e5 9990 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9991
9992 discard_cleanups (back_to);
9993 return lh;
9994}
c906108c 9995
5fb290d7
DJ
9996/* This function exists to work around a bug in certain compilers
9997 (particularly GCC 2.95), in which the first line number marker of a
9998 function does not show up until after the prologue, right before
9999 the second line number marker. This function shifts ADDRESS down
10000 to the beginning of the function if necessary, and is called on
10001 addresses passed to record_line. */
10002
10003static CORE_ADDR
e142c38c 10004check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10005{
10006 struct function_range *fn;
10007
10008 /* Find the function_range containing address. */
e142c38c 10009 if (!cu->first_fn)
5fb290d7
DJ
10010 return address;
10011
e142c38c
DJ
10012 if (!cu->cached_fn)
10013 cu->cached_fn = cu->first_fn;
5fb290d7 10014
e142c38c 10015 fn = cu->cached_fn;
5fb290d7
DJ
10016 while (fn)
10017 if (fn->lowpc <= address && fn->highpc > address)
10018 goto found;
10019 else
10020 fn = fn->next;
10021
e142c38c
DJ
10022 fn = cu->first_fn;
10023 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10024 if (fn->lowpc <= address && fn->highpc > address)
10025 goto found;
10026 else
10027 fn = fn->next;
10028
10029 return address;
10030
10031 found:
10032 if (fn->seen_line)
10033 return address;
10034 if (address != fn->lowpc)
4d3c2250 10035 complaint (&symfile_complaints,
e2e0b3e5 10036 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10037 (unsigned long) address, fn->name);
5fb290d7
DJ
10038 fn->seen_line = 1;
10039 return fn->lowpc;
10040}
10041
c6da4cef
DE
10042/* Subroutine of dwarf_decode_lines to simplify it.
10043 Return the file name of the psymtab for included file FILE_INDEX
10044 in line header LH of PST.
10045 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10046 If space for the result is malloc'd, it will be freed by a cleanup.
10047 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10048
10049static char *
10050psymtab_include_file_name (const struct line_header *lh, int file_index,
10051 const struct partial_symtab *pst,
10052 const char *comp_dir)
10053{
10054 const struct file_entry fe = lh->file_names [file_index];
10055 char *include_name = fe.name;
10056 char *include_name_to_compare = include_name;
10057 char *dir_name = NULL;
72b9f47f
TT
10058 const char *pst_filename;
10059 char *copied_name = NULL;
c6da4cef
DE
10060 int file_is_pst;
10061
10062 if (fe.dir_index)
10063 dir_name = lh->include_dirs[fe.dir_index - 1];
10064
10065 if (!IS_ABSOLUTE_PATH (include_name)
10066 && (dir_name != NULL || comp_dir != NULL))
10067 {
10068 /* Avoid creating a duplicate psymtab for PST.
10069 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10070 Before we do the comparison, however, we need to account
10071 for DIR_NAME and COMP_DIR.
10072 First prepend dir_name (if non-NULL). If we still don't
10073 have an absolute path prepend comp_dir (if non-NULL).
10074 However, the directory we record in the include-file's
10075 psymtab does not contain COMP_DIR (to match the
10076 corresponding symtab(s)).
10077
10078 Example:
10079
10080 bash$ cd /tmp
10081 bash$ gcc -g ./hello.c
10082 include_name = "hello.c"
10083 dir_name = "."
10084 DW_AT_comp_dir = comp_dir = "/tmp"
10085 DW_AT_name = "./hello.c" */
10086
10087 if (dir_name != NULL)
10088 {
10089 include_name = concat (dir_name, SLASH_STRING,
10090 include_name, (char *)NULL);
10091 include_name_to_compare = include_name;
10092 make_cleanup (xfree, include_name);
10093 }
10094 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10095 {
10096 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10097 include_name, (char *)NULL);
10098 }
10099 }
10100
10101 pst_filename = pst->filename;
10102 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10103 {
72b9f47f
TT
10104 copied_name = concat (pst->dirname, SLASH_STRING,
10105 pst_filename, (char *)NULL);
10106 pst_filename = copied_name;
c6da4cef
DE
10107 }
10108
1e3fad37 10109 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10110
10111 if (include_name_to_compare != include_name)
10112 xfree (include_name_to_compare);
72b9f47f
TT
10113 if (copied_name != NULL)
10114 xfree (copied_name);
c6da4cef
DE
10115
10116 if (file_is_pst)
10117 return NULL;
10118 return include_name;
10119}
10120
aaa75496
JB
10121/* Decode the Line Number Program (LNP) for the given line_header
10122 structure and CU. The actual information extracted and the type
10123 of structures created from the LNP depends on the value of PST.
10124
10125 1. If PST is NULL, then this procedure uses the data from the program
10126 to create all necessary symbol tables, and their linetables.
6e70227d 10127
aaa75496
JB
10128 2. If PST is not NULL, this procedure reads the program to determine
10129 the list of files included by the unit represented by PST, and
c6da4cef
DE
10130 builds all the associated partial symbol tables.
10131
10132 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10133 It is used for relative paths in the line table.
10134 NOTE: When processing partial symtabs (pst != NULL),
10135 comp_dir == pst->dirname.
10136
10137 NOTE: It is important that psymtabs have the same file name (via strcmp)
10138 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10139 symtab we don't use it in the name of the psymtabs we create.
10140 E.g. expand_line_sal requires this when finding psymtabs to expand.
10141 A good testcase for this is mb-inline.exp. */
debd256d 10142
c906108c 10143static void
72b9f47f 10144dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10145 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10146{
a8c50c1f 10147 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10148 gdb_byte *line_end;
a8c50c1f 10149 unsigned int bytes_read, extended_len;
c906108c 10150 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10151 CORE_ADDR baseaddr;
10152 struct objfile *objfile = cu->objfile;
fbf65064 10153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10154 const int decode_for_pst_p = (pst != NULL);
cb1df416 10155 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10156
10157 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10158
debd256d
JB
10159 line_ptr = lh->statement_program_start;
10160 line_end = lh->statement_program_end;
c906108c
SS
10161
10162 /* Read the statement sequences until there's nothing left. */
10163 while (line_ptr < line_end)
10164 {
10165 /* state machine registers */
10166 CORE_ADDR address = 0;
10167 unsigned int file = 1;
10168 unsigned int line = 1;
10169 unsigned int column = 0;
debd256d 10170 int is_stmt = lh->default_is_stmt;
c906108c
SS
10171 int basic_block = 0;
10172 int end_sequence = 0;
fbf65064 10173 CORE_ADDR addr;
2dc7f7b3 10174 unsigned char op_index = 0;
c906108c 10175
aaa75496 10176 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10177 {
aaa75496 10178 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10179 /* lh->include_dirs and lh->file_names are 0-based, but the
10180 directory and file name numbers in the statement program
10181 are 1-based. */
10182 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10183 char *dir = NULL;
a738430d 10184
debd256d
JB
10185 if (fe->dir_index)
10186 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10187
10188 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10189 }
10190
a738430d 10191 /* Decode the table. */
c5aa993b 10192 while (!end_sequence)
c906108c
SS
10193 {
10194 op_code = read_1_byte (abfd, line_ptr);
10195 line_ptr += 1;
59205f5a
JB
10196 if (line_ptr > line_end)
10197 {
10198 dwarf2_debug_line_missing_end_sequence_complaint ();
10199 break;
10200 }
9aa1fe7e 10201
debd256d 10202 if (op_code >= lh->opcode_base)
6e70227d 10203 {
a738430d 10204 /* Special operand. */
debd256d 10205 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10206 address += (((op_index + (adj_opcode / lh->line_range))
10207 / lh->maximum_ops_per_instruction)
10208 * lh->minimum_instruction_length);
10209 op_index = ((op_index + (adj_opcode / lh->line_range))
10210 % lh->maximum_ops_per_instruction);
debd256d 10211 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10212 if (lh->num_file_names < file || file == 0)
25e43795 10213 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10214 /* For now we ignore lines not starting on an
10215 instruction boundary. */
10216 else if (op_index == 0)
25e43795
DJ
10217 {
10218 lh->file_names[file - 1].included_p = 1;
ca5f395d 10219 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10220 {
10221 if (last_subfile != current_subfile)
10222 {
10223 addr = gdbarch_addr_bits_remove (gdbarch, address);
10224 if (last_subfile)
10225 record_line (last_subfile, 0, addr);
10226 last_subfile = current_subfile;
10227 }
25e43795 10228 /* Append row to matrix using current values. */
fbf65064
UW
10229 addr = check_cu_functions (address, cu);
10230 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10231 record_line (current_subfile, line, addr);
366da635 10232 }
25e43795 10233 }
ca5f395d 10234 basic_block = 0;
9aa1fe7e
GK
10235 }
10236 else switch (op_code)
c906108c
SS
10237 {
10238 case DW_LNS_extended_op:
a8c50c1f 10239 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10240 line_ptr += bytes_read;
a8c50c1f 10241 extended_end = line_ptr + extended_len;
c906108c
SS
10242 extended_op = read_1_byte (abfd, line_ptr);
10243 line_ptr += 1;
10244 switch (extended_op)
10245 {
10246 case DW_LNE_end_sequence:
10247 end_sequence = 1;
c906108c
SS
10248 break;
10249 case DW_LNE_set_address:
e7c27a73 10250 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10251 op_index = 0;
107d2387
AC
10252 line_ptr += bytes_read;
10253 address += baseaddr;
c906108c
SS
10254 break;
10255 case DW_LNE_define_file:
debd256d
JB
10256 {
10257 char *cur_file;
10258 unsigned int dir_index, mod_time, length;
6e70227d 10259
9b1c24c8 10260 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10261 line_ptr += bytes_read;
10262 dir_index =
10263 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10264 line_ptr += bytes_read;
10265 mod_time =
10266 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10267 line_ptr += bytes_read;
10268 length =
10269 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10270 line_ptr += bytes_read;
10271 add_file_name (lh, cur_file, dir_index, mod_time, length);
10272 }
c906108c 10273 break;
d0c6ba3d
CC
10274 case DW_LNE_set_discriminator:
10275 /* The discriminator is not interesting to the debugger;
10276 just ignore it. */
10277 line_ptr = extended_end;
10278 break;
c906108c 10279 default:
4d3c2250 10280 complaint (&symfile_complaints,
e2e0b3e5 10281 _("mangled .debug_line section"));
debd256d 10282 return;
c906108c 10283 }
a8c50c1f
DJ
10284 /* Make sure that we parsed the extended op correctly. If e.g.
10285 we expected a different address size than the producer used,
10286 we may have read the wrong number of bytes. */
10287 if (line_ptr != extended_end)
10288 {
10289 complaint (&symfile_complaints,
10290 _("mangled .debug_line section"));
10291 return;
10292 }
c906108c
SS
10293 break;
10294 case DW_LNS_copy:
59205f5a 10295 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10296 dwarf2_debug_line_missing_file_complaint ();
10297 else
366da635 10298 {
25e43795 10299 lh->file_names[file - 1].included_p = 1;
ca5f395d 10300 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10301 {
10302 if (last_subfile != current_subfile)
10303 {
10304 addr = gdbarch_addr_bits_remove (gdbarch, address);
10305 if (last_subfile)
10306 record_line (last_subfile, 0, addr);
10307 last_subfile = current_subfile;
10308 }
10309 addr = check_cu_functions (address, cu);
10310 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10311 record_line (current_subfile, line, addr);
10312 }
366da635 10313 }
c906108c
SS
10314 basic_block = 0;
10315 break;
10316 case DW_LNS_advance_pc:
2dc7f7b3
TT
10317 {
10318 CORE_ADDR adjust
10319 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10320
10321 address += (((op_index + adjust)
10322 / lh->maximum_ops_per_instruction)
10323 * lh->minimum_instruction_length);
10324 op_index = ((op_index + adjust)
10325 % lh->maximum_ops_per_instruction);
10326 line_ptr += bytes_read;
10327 }
c906108c
SS
10328 break;
10329 case DW_LNS_advance_line:
10330 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10331 line_ptr += bytes_read;
10332 break;
10333 case DW_LNS_set_file:
debd256d 10334 {
a738430d
MK
10335 /* The arrays lh->include_dirs and lh->file_names are
10336 0-based, but the directory and file name numbers in
10337 the statement program are 1-based. */
debd256d 10338 struct file_entry *fe;
4f1520fb 10339 char *dir = NULL;
a738430d 10340
debd256d
JB
10341 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10342 line_ptr += bytes_read;
59205f5a 10343 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10344 dwarf2_debug_line_missing_file_complaint ();
10345 else
10346 {
10347 fe = &lh->file_names[file - 1];
10348 if (fe->dir_index)
10349 dir = lh->include_dirs[fe->dir_index - 1];
10350 if (!decode_for_pst_p)
10351 {
10352 last_subfile = current_subfile;
10353 dwarf2_start_subfile (fe->name, dir, comp_dir);
10354 }
10355 }
debd256d 10356 }
c906108c
SS
10357 break;
10358 case DW_LNS_set_column:
10359 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10360 line_ptr += bytes_read;
10361 break;
10362 case DW_LNS_negate_stmt:
10363 is_stmt = (!is_stmt);
10364 break;
10365 case DW_LNS_set_basic_block:
10366 basic_block = 1;
10367 break;
c2c6d25f
JM
10368 /* Add to the address register of the state machine the
10369 address increment value corresponding to special opcode
a738430d
MK
10370 255. I.e., this value is scaled by the minimum
10371 instruction length since special opcode 255 would have
10372 scaled the the increment. */
c906108c 10373 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10374 {
10375 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10376
10377 address += (((op_index + adjust)
10378 / lh->maximum_ops_per_instruction)
10379 * lh->minimum_instruction_length);
10380 op_index = ((op_index + adjust)
10381 % lh->maximum_ops_per_instruction);
10382 }
c906108c
SS
10383 break;
10384 case DW_LNS_fixed_advance_pc:
10385 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10386 op_index = 0;
c906108c
SS
10387 line_ptr += 2;
10388 break;
9aa1fe7e 10389 default:
a738430d
MK
10390 {
10391 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10392 int i;
a738430d 10393
debd256d 10394 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10395 {
10396 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10397 line_ptr += bytes_read;
10398 }
10399 }
c906108c
SS
10400 }
10401 }
59205f5a
JB
10402 if (lh->num_file_names < file || file == 0)
10403 dwarf2_debug_line_missing_file_complaint ();
10404 else
10405 {
10406 lh->file_names[file - 1].included_p = 1;
10407 if (!decode_for_pst_p)
fbf65064
UW
10408 {
10409 addr = gdbarch_addr_bits_remove (gdbarch, address);
10410 record_line (current_subfile, 0, addr);
10411 }
59205f5a 10412 }
c906108c 10413 }
aaa75496
JB
10414
10415 if (decode_for_pst_p)
10416 {
10417 int file_index;
10418
10419 /* Now that we're done scanning the Line Header Program, we can
10420 create the psymtab of each included file. */
10421 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10422 if (lh->file_names[file_index].included_p == 1)
10423 {
c6da4cef
DE
10424 char *include_name =
10425 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10426 if (include_name != NULL)
aaa75496
JB
10427 dwarf2_create_include_psymtab (include_name, pst, objfile);
10428 }
10429 }
cb1df416
DJ
10430 else
10431 {
10432 /* Make sure a symtab is created for every file, even files
10433 which contain only variables (i.e. no code with associated
10434 line numbers). */
10435
10436 int i;
10437 struct file_entry *fe;
10438
10439 for (i = 0; i < lh->num_file_names; i++)
10440 {
10441 char *dir = NULL;
9a619af0 10442
cb1df416
DJ
10443 fe = &lh->file_names[i];
10444 if (fe->dir_index)
10445 dir = lh->include_dirs[fe->dir_index - 1];
10446 dwarf2_start_subfile (fe->name, dir, comp_dir);
10447
10448 /* Skip the main file; we don't need it, and it must be
10449 allocated last, so that it will show up before the
10450 non-primary symtabs in the objfile's symtab list. */
10451 if (current_subfile == first_subfile)
10452 continue;
10453
10454 if (current_subfile->symtab == NULL)
10455 current_subfile->symtab = allocate_symtab (current_subfile->name,
10456 cu->objfile);
10457 fe->symtab = current_subfile->symtab;
10458 }
10459 }
c906108c
SS
10460}
10461
10462/* Start a subfile for DWARF. FILENAME is the name of the file and
10463 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10464 or NULL if not known. COMP_DIR is the compilation directory for the
10465 linetable's compilation unit or NULL if not known.
c906108c
SS
10466 This routine tries to keep line numbers from identical absolute and
10467 relative file names in a common subfile.
10468
10469 Using the `list' example from the GDB testsuite, which resides in
10470 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10471 of /srcdir/list0.c yields the following debugging information for list0.c:
10472
c5aa993b
JM
10473 DW_AT_name: /srcdir/list0.c
10474 DW_AT_comp_dir: /compdir
357e46e7 10475 files.files[0].name: list0.h
c5aa993b 10476 files.files[0].dir: /srcdir
357e46e7 10477 files.files[1].name: list0.c
c5aa993b 10478 files.files[1].dir: /srcdir
c906108c
SS
10479
10480 The line number information for list0.c has to end up in a single
4f1520fb
FR
10481 subfile, so that `break /srcdir/list0.c:1' works as expected.
10482 start_subfile will ensure that this happens provided that we pass the
10483 concatenation of files.files[1].dir and files.files[1].name as the
10484 subfile's name. */
c906108c
SS
10485
10486static void
72b9f47f 10487dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10488{
4f1520fb
FR
10489 char *fullname;
10490
10491 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10492 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10493 second argument to start_subfile. To be consistent, we do the
10494 same here. In order not to lose the line information directory,
10495 we concatenate it to the filename when it makes sense.
10496 Note that the Dwarf3 standard says (speaking of filenames in line
10497 information): ``The directory index is ignored for file names
10498 that represent full path names''. Thus ignoring dirname in the
10499 `else' branch below isn't an issue. */
c906108c 10500
d5166ae1 10501 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10502 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10503 else
10504 fullname = filename;
c906108c 10505
4f1520fb
FR
10506 start_subfile (fullname, comp_dir);
10507
10508 if (fullname != filename)
10509 xfree (fullname);
c906108c
SS
10510}
10511
4c2df51b
DJ
10512static void
10513var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10514 struct dwarf2_cu *cu)
4c2df51b 10515{
e7c27a73
DJ
10516 struct objfile *objfile = cu->objfile;
10517 struct comp_unit_head *cu_header = &cu->header;
10518
4c2df51b
DJ
10519 /* NOTE drow/2003-01-30: There used to be a comment and some special
10520 code here to turn a symbol with DW_AT_external and a
10521 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10522 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10523 with some versions of binutils) where shared libraries could have
10524 relocations against symbols in their debug information - the
10525 minimal symbol would have the right address, but the debug info
10526 would not. It's no longer necessary, because we will explicitly
10527 apply relocations when we read in the debug information now. */
10528
10529 /* A DW_AT_location attribute with no contents indicates that a
10530 variable has been optimized away. */
10531 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10532 {
10533 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10534 return;
10535 }
10536
10537 /* Handle one degenerate form of location expression specially, to
10538 preserve GDB's previous behavior when section offsets are
10539 specified. If this is just a DW_OP_addr then mark this symbol
10540 as LOC_STATIC. */
10541
10542 if (attr_form_is_block (attr)
10543 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10544 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10545 {
891d2f0b 10546 unsigned int dummy;
4c2df51b
DJ
10547
10548 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10549 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10550 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10551 fixup_symbol_section (sym, objfile);
10552 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10553 SYMBOL_SECTION (sym));
4c2df51b
DJ
10554 return;
10555 }
10556
10557 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10558 expression evaluator, and use LOC_COMPUTED only when necessary
10559 (i.e. when the value of a register or memory location is
10560 referenced, or a thread-local block, etc.). Then again, it might
10561 not be worthwhile. I'm assuming that it isn't unless performance
10562 or memory numbers show me otherwise. */
10563
e7c27a73 10564 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10565 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10566}
10567
c906108c
SS
10568/* Given a pointer to a DWARF information entry, figure out if we need
10569 to make a symbol table entry for it, and if so, create a new entry
10570 and return a pointer to it.
10571 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10572 used the passed type.
10573 If SPACE is not NULL, use it to hold the new symbol. If it is
10574 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10575
10576static struct symbol *
34eaf542
TT
10577new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10578 struct symbol *space)
c906108c 10579{
e7c27a73 10580 struct objfile *objfile = cu->objfile;
c906108c
SS
10581 struct symbol *sym = NULL;
10582 char *name;
10583 struct attribute *attr = NULL;
10584 struct attribute *attr2 = NULL;
e142c38c 10585 CORE_ADDR baseaddr;
e37fd15a
SW
10586 struct pending **list_to_add = NULL;
10587
edb3359d 10588 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10589
10590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10591
94af9270 10592 name = dwarf2_name (die, cu);
c906108c
SS
10593 if (name)
10594 {
94af9270 10595 const char *linkagename;
34eaf542 10596 int suppress_add = 0;
94af9270 10597
34eaf542
TT
10598 if (space)
10599 sym = space;
10600 else
10601 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10602 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10603
10604 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10605 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10606 linkagename = dwarf2_physname (name, die, cu);
10607 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10608
f55ee35c
JK
10609 /* Fortran does not have mangling standard and the mangling does differ
10610 between gfortran, iFort etc. */
10611 if (cu->language == language_fortran
b250c185 10612 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10613 symbol_set_demangled_name (&(sym->ginfo),
10614 (char *) dwarf2_full_name (name, die, cu),
10615 NULL);
f55ee35c 10616
c906108c 10617 /* Default assumptions.
c5aa993b 10618 Use the passed type or decode it from the die. */
176620f1 10619 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10620 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10621 if (type != NULL)
10622 SYMBOL_TYPE (sym) = type;
10623 else
e7c27a73 10624 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10625 attr = dwarf2_attr (die,
10626 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10627 cu);
c906108c
SS
10628 if (attr)
10629 {
10630 SYMBOL_LINE (sym) = DW_UNSND (attr);
10631 }
cb1df416 10632
edb3359d
DJ
10633 attr = dwarf2_attr (die,
10634 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10635 cu);
cb1df416
DJ
10636 if (attr)
10637 {
10638 int file_index = DW_UNSND (attr);
9a619af0 10639
cb1df416
DJ
10640 if (cu->line_header == NULL
10641 || file_index > cu->line_header->num_file_names)
10642 complaint (&symfile_complaints,
10643 _("file index out of range"));
1c3d648d 10644 else if (file_index > 0)
cb1df416
DJ
10645 {
10646 struct file_entry *fe;
9a619af0 10647
cb1df416
DJ
10648 fe = &cu->line_header->file_names[file_index - 1];
10649 SYMBOL_SYMTAB (sym) = fe->symtab;
10650 }
10651 }
10652
c906108c
SS
10653 switch (die->tag)
10654 {
10655 case DW_TAG_label:
e142c38c 10656 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10657 if (attr)
10658 {
10659 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10660 }
0f5238ed
TT
10661 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10662 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10663 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10664 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10665 break;
10666 case DW_TAG_subprogram:
10667 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10668 finish_block. */
10669 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10670 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10671 if ((attr2 && (DW_UNSND (attr2) != 0))
10672 || cu->language == language_ada)
c906108c 10673 {
2cfa0c8d
JB
10674 /* Subprograms marked external are stored as a global symbol.
10675 Ada subprograms, whether marked external or not, are always
10676 stored as a global symbol, because we want to be able to
10677 access them globally. For instance, we want to be able
10678 to break on a nested subprogram without having to
10679 specify the context. */
e37fd15a 10680 list_to_add = &global_symbols;
c906108c
SS
10681 }
10682 else
10683 {
e37fd15a 10684 list_to_add = cu->list_in_scope;
c906108c
SS
10685 }
10686 break;
edb3359d
DJ
10687 case DW_TAG_inlined_subroutine:
10688 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10689 finish_block. */
10690 SYMBOL_CLASS (sym) = LOC_BLOCK;
10691 SYMBOL_INLINED (sym) = 1;
10692 /* Do not add the symbol to any lists. It will be found via
10693 BLOCK_FUNCTION from the blockvector. */
10694 break;
34eaf542
TT
10695 case DW_TAG_template_value_param:
10696 suppress_add = 1;
10697 /* Fall through. */
72929c62 10698 case DW_TAG_constant:
c906108c 10699 case DW_TAG_variable:
254e6b9e 10700 case DW_TAG_member:
c906108c
SS
10701 /* Compilation with minimal debug info may result in variables
10702 with missing type entries. Change the misleading `void' type
10703 to something sensible. */
10704 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10705 SYMBOL_TYPE (sym)
46bf5051 10706 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10707
e142c38c 10708 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10709 /* In the case of DW_TAG_member, we should only be called for
10710 static const members. */
10711 if (die->tag == DW_TAG_member)
10712 {
3863f96c
DE
10713 /* dwarf2_add_field uses die_is_declaration,
10714 so we do the same. */
254e6b9e
DE
10715 gdb_assert (die_is_declaration (die, cu));
10716 gdb_assert (attr);
10717 }
c906108c
SS
10718 if (attr)
10719 {
e7c27a73 10720 dwarf2_const_value (attr, sym, cu);
e142c38c 10721 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10722 if (!suppress_add)
34eaf542
TT
10723 {
10724 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10725 list_to_add = &global_symbols;
34eaf542 10726 else
e37fd15a 10727 list_to_add = cu->list_in_scope;
34eaf542 10728 }
c906108c
SS
10729 break;
10730 }
e142c38c 10731 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10732 if (attr)
10733 {
e7c27a73 10734 var_decode_location (attr, sym, cu);
e142c38c 10735 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10736 if (SYMBOL_CLASS (sym) == LOC_STATIC
10737 && SYMBOL_VALUE_ADDRESS (sym) == 0
10738 && !dwarf2_per_objfile->has_section_at_zero)
10739 {
10740 /* When a static variable is eliminated by the linker,
10741 the corresponding debug information is not stripped
10742 out, but the variable address is set to null;
10743 do not add such variables into symbol table. */
10744 }
10745 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10746 {
f55ee35c
JK
10747 /* Workaround gfortran PR debug/40040 - it uses
10748 DW_AT_location for variables in -fPIC libraries which may
10749 get overriden by other libraries/executable and get
10750 a different address. Resolve it by the minimal symbol
10751 which may come from inferior's executable using copy
10752 relocation. Make this workaround only for gfortran as for
10753 other compilers GDB cannot guess the minimal symbol
10754 Fortran mangling kind. */
10755 if (cu->language == language_fortran && die->parent
10756 && die->parent->tag == DW_TAG_module
10757 && cu->producer
10758 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10759 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10760
1c809c68
TT
10761 /* A variable with DW_AT_external is never static,
10762 but it may be block-scoped. */
10763 list_to_add = (cu->list_in_scope == &file_symbols
10764 ? &global_symbols : cu->list_in_scope);
1c809c68 10765 }
c906108c 10766 else
e37fd15a 10767 list_to_add = cu->list_in_scope;
c906108c
SS
10768 }
10769 else
10770 {
10771 /* We do not know the address of this symbol.
c5aa993b
JM
10772 If it is an external symbol and we have type information
10773 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10774 The address of the variable will then be determined from
10775 the minimal symbol table whenever the variable is
10776 referenced. */
e142c38c 10777 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10778 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10779 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10780 {
0fe7935b
DJ
10781 /* A variable with DW_AT_external is never static, but it
10782 may be block-scoped. */
10783 list_to_add = (cu->list_in_scope == &file_symbols
10784 ? &global_symbols : cu->list_in_scope);
10785
c906108c 10786 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10787 }
442ddf59
JK
10788 else if (!die_is_declaration (die, cu))
10789 {
10790 /* Use the default LOC_OPTIMIZED_OUT class. */
10791 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10792 if (!suppress_add)
10793 list_to_add = cu->list_in_scope;
442ddf59 10794 }
c906108c
SS
10795 }
10796 break;
10797 case DW_TAG_formal_parameter:
edb3359d
DJ
10798 /* If we are inside a function, mark this as an argument. If
10799 not, we might be looking at an argument to an inlined function
10800 when we do not have enough information to show inlined frames;
10801 pretend it's a local variable in that case so that the user can
10802 still see it. */
10803 if (context_stack_depth > 0
10804 && context_stack[context_stack_depth - 1].name != NULL)
10805 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10806 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10807 if (attr)
10808 {
e7c27a73 10809 var_decode_location (attr, sym, cu);
c906108c 10810 }
e142c38c 10811 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10812 if (attr)
10813 {
e7c27a73 10814 dwarf2_const_value (attr, sym, cu);
c906108c 10815 }
f346a30d
PM
10816 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10817 if (attr && DW_UNSND (attr))
10818 {
10819 struct type *ref_type;
10820
10821 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10822 SYMBOL_TYPE (sym) = ref_type;
10823 }
10824
e37fd15a 10825 list_to_add = cu->list_in_scope;
c906108c
SS
10826 break;
10827 case DW_TAG_unspecified_parameters:
10828 /* From varargs functions; gdb doesn't seem to have any
10829 interest in this information, so just ignore it for now.
10830 (FIXME?) */
10831 break;
34eaf542
TT
10832 case DW_TAG_template_type_param:
10833 suppress_add = 1;
10834 /* Fall through. */
c906108c 10835 case DW_TAG_class_type:
680b30c7 10836 case DW_TAG_interface_type:
c906108c
SS
10837 case DW_TAG_structure_type:
10838 case DW_TAG_union_type:
72019c9c 10839 case DW_TAG_set_type:
c906108c
SS
10840 case DW_TAG_enumeration_type:
10841 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10842 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10843
63d06c5c 10844 {
987504bb 10845 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10846 really ever be static objects: otherwise, if you try
10847 to, say, break of a class's method and you're in a file
10848 which doesn't mention that class, it won't work unless
10849 the check for all static symbols in lookup_symbol_aux
10850 saves you. See the OtherFileClass tests in
10851 gdb.c++/namespace.exp. */
10852
e37fd15a 10853 if (!suppress_add)
34eaf542 10854 {
34eaf542
TT
10855 list_to_add = (cu->list_in_scope == &file_symbols
10856 && (cu->language == language_cplus
10857 || cu->language == language_java)
10858 ? &global_symbols : cu->list_in_scope);
63d06c5c 10859
64382290
TT
10860 /* The semantics of C++ state that "struct foo {
10861 ... }" also defines a typedef for "foo". A Java
10862 class declaration also defines a typedef for the
10863 class. */
10864 if (cu->language == language_cplus
10865 || cu->language == language_java
10866 || cu->language == language_ada)
10867 {
10868 /* The symbol's name is already allocated along
10869 with this objfile, so we don't need to
10870 duplicate it for the type. */
10871 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10872 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10873 }
63d06c5c
DC
10874 }
10875 }
c906108c
SS
10876 break;
10877 case DW_TAG_typedef:
63d06c5c
DC
10878 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10879 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10880 list_to_add = cu->list_in_scope;
63d06c5c 10881 break;
c906108c 10882 case DW_TAG_base_type:
a02abb62 10883 case DW_TAG_subrange_type:
c906108c 10884 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10885 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 10886 list_to_add = cu->list_in_scope;
c906108c
SS
10887 break;
10888 case DW_TAG_enumerator:
e142c38c 10889 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10890 if (attr)
10891 {
e7c27a73 10892 dwarf2_const_value (attr, sym, cu);
c906108c 10893 }
63d06c5c
DC
10894 {
10895 /* NOTE: carlton/2003-11-10: See comment above in the
10896 DW_TAG_class_type, etc. block. */
10897
e142c38c 10898 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10899 && (cu->language == language_cplus
10900 || cu->language == language_java)
e142c38c 10901 ? &global_symbols : cu->list_in_scope);
63d06c5c 10902 }
c906108c 10903 break;
5c4e30ca
DC
10904 case DW_TAG_namespace:
10905 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 10906 list_to_add = &global_symbols;
5c4e30ca 10907 break;
c906108c
SS
10908 default:
10909 /* Not a tag we recognize. Hopefully we aren't processing
10910 trash data, but since we must specifically ignore things
10911 we don't recognize, there is nothing else we should do at
10912 this point. */
e2e0b3e5 10913 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10914 dwarf_tag_name (die->tag));
c906108c
SS
10915 break;
10916 }
df8a16a1 10917
e37fd15a
SW
10918 if (suppress_add)
10919 {
10920 sym->hash_next = objfile->template_symbols;
10921 objfile->template_symbols = sym;
10922 list_to_add = NULL;
10923 }
10924
10925 if (list_to_add != NULL)
10926 add_symbol_to_list (sym, list_to_add);
10927
df8a16a1
DJ
10928 /* For the benefit of old versions of GCC, check for anonymous
10929 namespaces based on the demangled name. */
10930 if (!processing_has_namespace_info
94af9270 10931 && cu->language == language_cplus)
df8a16a1 10932 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10933 }
10934 return (sym);
10935}
10936
34eaf542
TT
10937/* A wrapper for new_symbol_full that always allocates a new symbol. */
10938
10939static struct symbol *
10940new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10941{
10942 return new_symbol_full (die, type, cu, NULL);
10943}
10944
98bfdba5
PA
10945/* Given an attr with a DW_FORM_dataN value in host byte order,
10946 zero-extend it as appropriate for the symbol's type. The DWARF
10947 standard (v4) is not entirely clear about the meaning of using
10948 DW_FORM_dataN for a constant with a signed type, where the type is
10949 wider than the data. The conclusion of a discussion on the DWARF
10950 list was that this is unspecified. We choose to always zero-extend
10951 because that is the interpretation long in use by GCC. */
c906108c 10952
98bfdba5
PA
10953static gdb_byte *
10954dwarf2_const_value_data (struct attribute *attr, struct type *type,
10955 const char *name, struct obstack *obstack,
10956 struct dwarf2_cu *cu, long *value, int bits)
c906108c 10957{
e7c27a73 10958 struct objfile *objfile = cu->objfile;
e17a4113
UW
10959 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10960 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
10961 LONGEST l = DW_UNSND (attr);
10962
10963 if (bits < sizeof (*value) * 8)
10964 {
10965 l &= ((LONGEST) 1 << bits) - 1;
10966 *value = l;
10967 }
10968 else if (bits == sizeof (*value) * 8)
10969 *value = l;
10970 else
10971 {
10972 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10973 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10974 return bytes;
10975 }
10976
10977 return NULL;
10978}
10979
10980/* Read a constant value from an attribute. Either set *VALUE, or if
10981 the value does not fit in *VALUE, set *BYTES - either already
10982 allocated on the objfile obstack, or newly allocated on OBSTACK,
10983 or, set *BATON, if we translated the constant to a location
10984 expression. */
10985
10986static void
10987dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10988 const char *name, struct obstack *obstack,
10989 struct dwarf2_cu *cu,
10990 long *value, gdb_byte **bytes,
10991 struct dwarf2_locexpr_baton **baton)
10992{
10993 struct objfile *objfile = cu->objfile;
10994 struct comp_unit_head *cu_header = &cu->header;
c906108c 10995 struct dwarf_block *blk;
98bfdba5
PA
10996 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10997 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10998
10999 *value = 0;
11000 *bytes = NULL;
11001 *baton = NULL;
c906108c
SS
11002
11003 switch (attr->form)
11004 {
11005 case DW_FORM_addr:
ac56253d 11006 {
ac56253d
TT
11007 gdb_byte *data;
11008
98bfdba5
PA
11009 if (TYPE_LENGTH (type) != cu_header->addr_size)
11010 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11011 cu_header->addr_size,
98bfdba5 11012 TYPE_LENGTH (type));
ac56253d
TT
11013 /* Symbols of this form are reasonably rare, so we just
11014 piggyback on the existing location code rather than writing
11015 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11016 *baton = obstack_alloc (&objfile->objfile_obstack,
11017 sizeof (struct dwarf2_locexpr_baton));
11018 (*baton)->per_cu = cu->per_cu;
11019 gdb_assert ((*baton)->per_cu);
ac56253d 11020
98bfdba5
PA
11021 (*baton)->size = 2 + cu_header->addr_size;
11022 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11023 (*baton)->data = data;
ac56253d
TT
11024
11025 data[0] = DW_OP_addr;
11026 store_unsigned_integer (&data[1], cu_header->addr_size,
11027 byte_order, DW_ADDR (attr));
11028 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11029 }
c906108c 11030 break;
4ac36638 11031 case DW_FORM_string:
93b5768b 11032 case DW_FORM_strp:
98bfdba5
PA
11033 /* DW_STRING is already allocated on the objfile obstack, point
11034 directly to it. */
11035 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11036 break;
c906108c
SS
11037 case DW_FORM_block1:
11038 case DW_FORM_block2:
11039 case DW_FORM_block4:
11040 case DW_FORM_block:
2dc7f7b3 11041 case DW_FORM_exprloc:
c906108c 11042 blk = DW_BLOCK (attr);
98bfdba5
PA
11043 if (TYPE_LENGTH (type) != blk->size)
11044 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11045 TYPE_LENGTH (type));
11046 *bytes = blk->data;
c906108c 11047 break;
2df3850c
JM
11048
11049 /* The DW_AT_const_value attributes are supposed to carry the
11050 symbol's value "represented as it would be on the target
11051 architecture." By the time we get here, it's already been
11052 converted to host endianness, so we just need to sign- or
11053 zero-extend it as appropriate. */
11054 case DW_FORM_data1:
98bfdba5 11055 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11056 break;
c906108c 11057 case DW_FORM_data2:
98bfdba5 11058 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11059 break;
c906108c 11060 case DW_FORM_data4:
98bfdba5 11061 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11062 break;
c906108c 11063 case DW_FORM_data8:
98bfdba5 11064 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11065 break;
11066
c906108c 11067 case DW_FORM_sdata:
98bfdba5 11068 *value = DW_SND (attr);
2df3850c
JM
11069 break;
11070
c906108c 11071 case DW_FORM_udata:
98bfdba5 11072 *value = DW_UNSND (attr);
c906108c 11073 break;
2df3850c 11074
c906108c 11075 default:
4d3c2250 11076 complaint (&symfile_complaints,
e2e0b3e5 11077 _("unsupported const value attribute form: '%s'"),
4d3c2250 11078 dwarf_form_name (attr->form));
98bfdba5 11079 *value = 0;
c906108c
SS
11080 break;
11081 }
11082}
11083
2df3850c 11084
98bfdba5
PA
11085/* Copy constant value from an attribute to a symbol. */
11086
2df3850c 11087static void
98bfdba5
PA
11088dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11089 struct dwarf2_cu *cu)
2df3850c 11090{
98bfdba5
PA
11091 struct objfile *objfile = cu->objfile;
11092 struct comp_unit_head *cu_header = &cu->header;
11093 long value;
11094 gdb_byte *bytes;
11095 struct dwarf2_locexpr_baton *baton;
2df3850c 11096
98bfdba5
PA
11097 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11098 SYMBOL_PRINT_NAME (sym),
11099 &objfile->objfile_obstack, cu,
11100 &value, &bytes, &baton);
2df3850c 11101
98bfdba5
PA
11102 if (baton != NULL)
11103 {
11104 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11105 SYMBOL_LOCATION_BATON (sym) = baton;
11106 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11107 }
11108 else if (bytes != NULL)
11109 {
11110 SYMBOL_VALUE_BYTES (sym) = bytes;
11111 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11112 }
11113 else
11114 {
11115 SYMBOL_VALUE (sym) = value;
11116 SYMBOL_CLASS (sym) = LOC_CONST;
11117 }
2df3850c
JM
11118}
11119
c906108c
SS
11120/* Return the type of the die in question using its DW_AT_type attribute. */
11121
11122static struct type *
e7c27a73 11123die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11124{
c906108c 11125 struct attribute *type_attr;
c906108c 11126
e142c38c 11127 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11128 if (!type_attr)
11129 {
11130 /* A missing DW_AT_type represents a void type. */
46bf5051 11131 return objfile_type (cu->objfile)->builtin_void;
c906108c 11132 }
348e048f 11133
673bfd45 11134 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11135}
11136
b4ba55a1
JB
11137/* True iff CU's producer generates GNAT Ada auxiliary information
11138 that allows to find parallel types through that information instead
11139 of having to do expensive parallel lookups by type name. */
11140
11141static int
11142need_gnat_info (struct dwarf2_cu *cu)
11143{
11144 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11145 of GNAT produces this auxiliary information, without any indication
11146 that it is produced. Part of enhancing the FSF version of GNAT
11147 to produce that information will be to put in place an indicator
11148 that we can use in order to determine whether the descriptive type
11149 info is available or not. One suggestion that has been made is
11150 to use a new attribute, attached to the CU die. For now, assume
11151 that the descriptive type info is not available. */
11152 return 0;
11153}
11154
b4ba55a1
JB
11155/* Return the auxiliary type of the die in question using its
11156 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11157 attribute is not present. */
11158
11159static struct type *
11160die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11161{
b4ba55a1 11162 struct attribute *type_attr;
b4ba55a1
JB
11163
11164 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11165 if (!type_attr)
11166 return NULL;
11167
673bfd45 11168 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11169}
11170
11171/* If DIE has a descriptive_type attribute, then set the TYPE's
11172 descriptive type accordingly. */
11173
11174static void
11175set_descriptive_type (struct type *type, struct die_info *die,
11176 struct dwarf2_cu *cu)
11177{
11178 struct type *descriptive_type = die_descriptive_type (die, cu);
11179
11180 if (descriptive_type)
11181 {
11182 ALLOCATE_GNAT_AUX_TYPE (type);
11183 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11184 }
11185}
11186
c906108c
SS
11187/* Return the containing type of the die in question using its
11188 DW_AT_containing_type attribute. */
11189
11190static struct type *
e7c27a73 11191die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11192{
c906108c 11193 struct attribute *type_attr;
c906108c 11194
e142c38c 11195 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11196 if (!type_attr)
11197 error (_("Dwarf Error: Problem turning containing type into gdb type "
11198 "[in module %s]"), cu->objfile->name);
11199
673bfd45 11200 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11201}
11202
673bfd45
DE
11203/* Look up the type of DIE in CU using its type attribute ATTR.
11204 If there is no type substitute an error marker. */
11205
c906108c 11206static struct type *
673bfd45
DE
11207lookup_die_type (struct die_info *die, struct attribute *attr,
11208 struct dwarf2_cu *cu)
c906108c 11209{
f792889a
DJ
11210 struct type *this_type;
11211
673bfd45
DE
11212 /* First see if we have it cached. */
11213
11214 if (is_ref_attr (attr))
11215 {
11216 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11217
11218 this_type = get_die_type_at_offset (offset, cu->per_cu);
11219 }
11220 else if (attr->form == DW_FORM_sig8)
11221 {
11222 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11223 struct dwarf2_cu *sig_cu;
11224 unsigned int offset;
11225
11226 /* sig_type will be NULL if the signatured type is missing from
11227 the debug info. */
11228 if (sig_type == NULL)
11229 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11230 "at 0x%x [in module %s]"),
11231 die->offset, cu->objfile->name);
11232
11233 gdb_assert (sig_type->per_cu.from_debug_types);
11234 offset = sig_type->offset + sig_type->type_offset;
11235 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11236 }
11237 else
11238 {
11239 dump_die_for_error (die);
11240 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11241 dwarf_attr_name (attr->name), cu->objfile->name);
11242 }
11243
11244 /* If not cached we need to read it in. */
11245
11246 if (this_type == NULL)
11247 {
11248 struct die_info *type_die;
11249 struct dwarf2_cu *type_cu = cu;
11250
11251 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11252 /* If the type is cached, we should have found it above. */
11253 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11254 this_type = read_type_die_1 (type_die, type_cu);
11255 }
11256
11257 /* If we still don't have a type use an error marker. */
11258
11259 if (this_type == NULL)
c906108c 11260 {
b00fdb78
TT
11261 char *message, *saved;
11262
11263 /* read_type_die already issued a complaint. */
11264 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11265 cu->objfile->name,
11266 cu->header.offset,
11267 die->offset);
11268 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11269 message, strlen (message));
11270 xfree (message);
11271
11272 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11273 }
673bfd45 11274
f792889a 11275 return this_type;
c906108c
SS
11276}
11277
673bfd45
DE
11278/* Return the type in DIE, CU.
11279 Returns NULL for invalid types.
11280
11281 This first does a lookup in the appropriate type_hash table,
11282 and only reads the die in if necessary.
11283
11284 NOTE: This can be called when reading in partial or full symbols. */
11285
f792889a 11286static struct type *
e7c27a73 11287read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11288{
f792889a
DJ
11289 struct type *this_type;
11290
11291 this_type = get_die_type (die, cu);
11292 if (this_type)
11293 return this_type;
11294
673bfd45
DE
11295 return read_type_die_1 (die, cu);
11296}
11297
11298/* Read the type in DIE, CU.
11299 Returns NULL for invalid types. */
11300
11301static struct type *
11302read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11303{
11304 struct type *this_type = NULL;
11305
c906108c
SS
11306 switch (die->tag)
11307 {
11308 case DW_TAG_class_type:
680b30c7 11309 case DW_TAG_interface_type:
c906108c
SS
11310 case DW_TAG_structure_type:
11311 case DW_TAG_union_type:
f792889a 11312 this_type = read_structure_type (die, cu);
c906108c
SS
11313 break;
11314 case DW_TAG_enumeration_type:
f792889a 11315 this_type = read_enumeration_type (die, cu);
c906108c
SS
11316 break;
11317 case DW_TAG_subprogram:
11318 case DW_TAG_subroutine_type:
edb3359d 11319 case DW_TAG_inlined_subroutine:
f792889a 11320 this_type = read_subroutine_type (die, cu);
c906108c
SS
11321 break;
11322 case DW_TAG_array_type:
f792889a 11323 this_type = read_array_type (die, cu);
c906108c 11324 break;
72019c9c 11325 case DW_TAG_set_type:
f792889a 11326 this_type = read_set_type (die, cu);
72019c9c 11327 break;
c906108c 11328 case DW_TAG_pointer_type:
f792889a 11329 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11330 break;
11331 case DW_TAG_ptr_to_member_type:
f792889a 11332 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11333 break;
11334 case DW_TAG_reference_type:
f792889a 11335 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11336 break;
11337 case DW_TAG_const_type:
f792889a 11338 this_type = read_tag_const_type (die, cu);
c906108c
SS
11339 break;
11340 case DW_TAG_volatile_type:
f792889a 11341 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11342 break;
11343 case DW_TAG_string_type:
f792889a 11344 this_type = read_tag_string_type (die, cu);
c906108c
SS
11345 break;
11346 case DW_TAG_typedef:
f792889a 11347 this_type = read_typedef (die, cu);
c906108c 11348 break;
a02abb62 11349 case DW_TAG_subrange_type:
f792889a 11350 this_type = read_subrange_type (die, cu);
a02abb62 11351 break;
c906108c 11352 case DW_TAG_base_type:
f792889a 11353 this_type = read_base_type (die, cu);
c906108c 11354 break;
81a17f79 11355 case DW_TAG_unspecified_type:
f792889a 11356 this_type = read_unspecified_type (die, cu);
81a17f79 11357 break;
0114d602
DJ
11358 case DW_TAG_namespace:
11359 this_type = read_namespace_type (die, cu);
11360 break;
f55ee35c
JK
11361 case DW_TAG_module:
11362 this_type = read_module_type (die, cu);
11363 break;
c906108c 11364 default:
a1f5b845 11365 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11366 dwarf_tag_name (die->tag));
c906108c
SS
11367 break;
11368 }
63d06c5c 11369
f792889a 11370 return this_type;
63d06c5c
DC
11371}
11372
abc72ce4
DE
11373/* See if we can figure out if the class lives in a namespace. We do
11374 this by looking for a member function; its demangled name will
11375 contain namespace info, if there is any.
11376 Return the computed name or NULL.
11377 Space for the result is allocated on the objfile's obstack.
11378 This is the full-die version of guess_partial_die_structure_name.
11379 In this case we know DIE has no useful parent. */
11380
11381static char *
11382guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11383{
11384 struct die_info *spec_die;
11385 struct dwarf2_cu *spec_cu;
11386 struct die_info *child;
11387
11388 spec_cu = cu;
11389 spec_die = die_specification (die, &spec_cu);
11390 if (spec_die != NULL)
11391 {
11392 die = spec_die;
11393 cu = spec_cu;
11394 }
11395
11396 for (child = die->child;
11397 child != NULL;
11398 child = child->sibling)
11399 {
11400 if (child->tag == DW_TAG_subprogram)
11401 {
11402 struct attribute *attr;
11403
11404 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11405 if (attr == NULL)
11406 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11407 if (attr != NULL)
11408 {
11409 char *actual_name
11410 = language_class_name_from_physname (cu->language_defn,
11411 DW_STRING (attr));
11412 char *name = NULL;
11413
11414 if (actual_name != NULL)
11415 {
11416 char *die_name = dwarf2_name (die, cu);
11417
11418 if (die_name != NULL
11419 && strcmp (die_name, actual_name) != 0)
11420 {
11421 /* Strip off the class name from the full name.
11422 We want the prefix. */
11423 int die_name_len = strlen (die_name);
11424 int actual_name_len = strlen (actual_name);
11425
11426 /* Test for '::' as a sanity check. */
11427 if (actual_name_len > die_name_len + 2
11428 && actual_name[actual_name_len - die_name_len - 1] == ':')
11429 name =
11430 obsavestring (actual_name,
11431 actual_name_len - die_name_len - 2,
11432 &cu->objfile->objfile_obstack);
11433 }
11434 }
11435 xfree (actual_name);
11436 return name;
11437 }
11438 }
11439 }
11440
11441 return NULL;
11442}
11443
fdde2d81 11444/* Return the name of the namespace/class that DIE is defined within,
0114d602 11445 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11446
0114d602
DJ
11447 For example, if we're within the method foo() in the following
11448 code:
11449
11450 namespace N {
11451 class C {
11452 void foo () {
11453 }
11454 };
11455 }
11456
11457 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11458
11459static char *
e142c38c 11460determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11461{
0114d602
DJ
11462 struct die_info *parent, *spec_die;
11463 struct dwarf2_cu *spec_cu;
11464 struct type *parent_type;
63d06c5c 11465
f55ee35c
JK
11466 if (cu->language != language_cplus && cu->language != language_java
11467 && cu->language != language_fortran)
0114d602
DJ
11468 return "";
11469
11470 /* We have to be careful in the presence of DW_AT_specification.
11471 For example, with GCC 3.4, given the code
11472
11473 namespace N {
11474 void foo() {
11475 // Definition of N::foo.
11476 }
11477 }
11478
11479 then we'll have a tree of DIEs like this:
11480
11481 1: DW_TAG_compile_unit
11482 2: DW_TAG_namespace // N
11483 3: DW_TAG_subprogram // declaration of N::foo
11484 4: DW_TAG_subprogram // definition of N::foo
11485 DW_AT_specification // refers to die #3
11486
11487 Thus, when processing die #4, we have to pretend that we're in
11488 the context of its DW_AT_specification, namely the contex of die
11489 #3. */
11490 spec_cu = cu;
11491 spec_die = die_specification (die, &spec_cu);
11492 if (spec_die == NULL)
11493 parent = die->parent;
11494 else
63d06c5c 11495 {
0114d602
DJ
11496 parent = spec_die->parent;
11497 cu = spec_cu;
63d06c5c 11498 }
0114d602
DJ
11499
11500 if (parent == NULL)
11501 return "";
98bfdba5
PA
11502 else if (parent->building_fullname)
11503 {
11504 const char *name;
11505 const char *parent_name;
11506
11507 /* It has been seen on RealView 2.2 built binaries,
11508 DW_TAG_template_type_param types actually _defined_ as
11509 children of the parent class:
11510
11511 enum E {};
11512 template class <class Enum> Class{};
11513 Class<enum E> class_e;
11514
11515 1: DW_TAG_class_type (Class)
11516 2: DW_TAG_enumeration_type (E)
11517 3: DW_TAG_enumerator (enum1:0)
11518 3: DW_TAG_enumerator (enum2:1)
11519 ...
11520 2: DW_TAG_template_type_param
11521 DW_AT_type DW_FORM_ref_udata (E)
11522
11523 Besides being broken debug info, it can put GDB into an
11524 infinite loop. Consider:
11525
11526 When we're building the full name for Class<E>, we'll start
11527 at Class, and go look over its template type parameters,
11528 finding E. We'll then try to build the full name of E, and
11529 reach here. We're now trying to build the full name of E,
11530 and look over the parent DIE for containing scope. In the
11531 broken case, if we followed the parent DIE of E, we'd again
11532 find Class, and once again go look at its template type
11533 arguments, etc., etc. Simply don't consider such parent die
11534 as source-level parent of this die (it can't be, the language
11535 doesn't allow it), and break the loop here. */
11536 name = dwarf2_name (die, cu);
11537 parent_name = dwarf2_name (parent, cu);
11538 complaint (&symfile_complaints,
11539 _("template param type '%s' defined within parent '%s'"),
11540 name ? name : "<unknown>",
11541 parent_name ? parent_name : "<unknown>");
11542 return "";
11543 }
63d06c5c 11544 else
0114d602
DJ
11545 switch (parent->tag)
11546 {
63d06c5c 11547 case DW_TAG_namespace:
0114d602 11548 parent_type = read_type_die (parent, cu);
acebe513
UW
11549 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11550 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11551 Work around this problem here. */
11552 if (cu->language == language_cplus
11553 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11554 return "";
0114d602
DJ
11555 /* We give a name to even anonymous namespaces. */
11556 return TYPE_TAG_NAME (parent_type);
63d06c5c 11557 case DW_TAG_class_type:
680b30c7 11558 case DW_TAG_interface_type:
63d06c5c 11559 case DW_TAG_structure_type:
0114d602 11560 case DW_TAG_union_type:
f55ee35c 11561 case DW_TAG_module:
0114d602
DJ
11562 parent_type = read_type_die (parent, cu);
11563 if (TYPE_TAG_NAME (parent_type) != NULL)
11564 return TYPE_TAG_NAME (parent_type);
11565 else
11566 /* An anonymous structure is only allowed non-static data
11567 members; no typedefs, no member functions, et cetera.
11568 So it does not need a prefix. */
11569 return "";
abc72ce4
DE
11570 case DW_TAG_compile_unit:
11571 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11572 if (cu->language == language_cplus
11573 && dwarf2_per_objfile->types.asection != NULL
11574 && die->child != NULL
11575 && (die->tag == DW_TAG_class_type
11576 || die->tag == DW_TAG_structure_type
11577 || die->tag == DW_TAG_union_type))
11578 {
11579 char *name = guess_full_die_structure_name (die, cu);
11580 if (name != NULL)
11581 return name;
11582 }
11583 return "";
63d06c5c 11584 default:
8176b9b8 11585 return determine_prefix (parent, cu);
63d06c5c 11586 }
63d06c5c
DC
11587}
11588
987504bb
JJ
11589/* Return a newly-allocated string formed by concatenating PREFIX and
11590 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11591 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11592 perform an obconcat, otherwise allocate storage for the result. The CU argument
11593 is used to determine the language and hence, the appropriate separator. */
11594
f55ee35c 11595#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11596
11597static char *
f55ee35c
JK
11598typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11599 int physname, struct dwarf2_cu *cu)
63d06c5c 11600{
f55ee35c 11601 const char *lead = "";
5c315b68 11602 const char *sep;
63d06c5c 11603
987504bb
JJ
11604 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11605 sep = "";
11606 else if (cu->language == language_java)
11607 sep = ".";
f55ee35c
JK
11608 else if (cu->language == language_fortran && physname)
11609 {
11610 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11611 DW_AT_MIPS_linkage_name is preferred and used instead. */
11612
11613 lead = "__";
11614 sep = "_MOD_";
11615 }
987504bb
JJ
11616 else
11617 sep = "::";
63d06c5c 11618
6dd47d34
DE
11619 if (prefix == NULL)
11620 prefix = "";
11621 if (suffix == NULL)
11622 suffix = "";
11623
987504bb
JJ
11624 if (obs == NULL)
11625 {
11626 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11627
f55ee35c
JK
11628 strcpy (retval, lead);
11629 strcat (retval, prefix);
6dd47d34
DE
11630 strcat (retval, sep);
11631 strcat (retval, suffix);
63d06c5c
DC
11632 return retval;
11633 }
987504bb
JJ
11634 else
11635 {
11636 /* We have an obstack. */
f55ee35c 11637 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11638 }
63d06c5c
DC
11639}
11640
c906108c
SS
11641/* Return sibling of die, NULL if no sibling. */
11642
f9aca02d 11643static struct die_info *
fba45db2 11644sibling_die (struct die_info *die)
c906108c 11645{
639d11d3 11646 return die->sibling;
c906108c
SS
11647}
11648
71c25dea
TT
11649/* Get name of a die, return NULL if not found. */
11650
11651static char *
11652dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11653 struct obstack *obstack)
11654{
11655 if (name && cu->language == language_cplus)
11656 {
11657 char *canon_name = cp_canonicalize_string (name);
11658
11659 if (canon_name != NULL)
11660 {
11661 if (strcmp (canon_name, name) != 0)
11662 name = obsavestring (canon_name, strlen (canon_name),
11663 obstack);
11664 xfree (canon_name);
11665 }
11666 }
11667
11668 return name;
c906108c
SS
11669}
11670
9219021c
DC
11671/* Get name of a die, return NULL if not found. */
11672
11673static char *
e142c38c 11674dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11675{
11676 struct attribute *attr;
11677
e142c38c 11678 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11679 if (!attr || !DW_STRING (attr))
11680 return NULL;
11681
11682 switch (die->tag)
11683 {
11684 case DW_TAG_compile_unit:
11685 /* Compilation units have a DW_AT_name that is a filename, not
11686 a source language identifier. */
11687 case DW_TAG_enumeration_type:
11688 case DW_TAG_enumerator:
11689 /* These tags always have simple identifiers already; no need
11690 to canonicalize them. */
11691 return DW_STRING (attr);
907af001 11692
418835cc
KS
11693 case DW_TAG_subprogram:
11694 /* Java constructors will all be named "<init>", so return
11695 the class name when we see this special case. */
11696 if (cu->language == language_java
11697 && DW_STRING (attr) != NULL
11698 && strcmp (DW_STRING (attr), "<init>") == 0)
11699 {
11700 struct dwarf2_cu *spec_cu = cu;
11701 struct die_info *spec_die;
11702
11703 /* GCJ will output '<init>' for Java constructor names.
11704 For this special case, return the name of the parent class. */
11705
11706 /* GCJ may output suprogram DIEs with AT_specification set.
11707 If so, use the name of the specified DIE. */
11708 spec_die = die_specification (die, &spec_cu);
11709 if (spec_die != NULL)
11710 return dwarf2_name (spec_die, spec_cu);
11711
11712 do
11713 {
11714 die = die->parent;
11715 if (die->tag == DW_TAG_class_type)
11716 return dwarf2_name (die, cu);
11717 }
11718 while (die->tag != DW_TAG_compile_unit);
11719 }
907af001
UW
11720 break;
11721
11722 case DW_TAG_class_type:
11723 case DW_TAG_interface_type:
11724 case DW_TAG_structure_type:
11725 case DW_TAG_union_type:
11726 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11727 structures or unions. These were of the form "._%d" in GCC 4.1,
11728 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11729 and GCC 4.4. We work around this problem by ignoring these. */
11730 if (strncmp (DW_STRING (attr), "._", 2) == 0
11731 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11732 return NULL;
11733 break;
11734
71c25dea 11735 default:
907af001
UW
11736 break;
11737 }
11738
11739 if (!DW_STRING_IS_CANONICAL (attr))
11740 {
11741 DW_STRING (attr)
11742 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11743 &cu->objfile->objfile_obstack);
11744 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11745 }
907af001 11746 return DW_STRING (attr);
9219021c
DC
11747}
11748
11749/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11750 is none. *EXT_CU is the CU containing DIE on input, and the CU
11751 containing the return value on output. */
9219021c
DC
11752
11753static struct die_info *
f2f0e013 11754dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11755{
11756 struct attribute *attr;
9219021c 11757
f2f0e013 11758 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11759 if (attr == NULL)
11760 return NULL;
11761
f2f0e013 11762 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11763}
11764
c906108c
SS
11765/* Convert a DIE tag into its string name. */
11766
11767static char *
aa1ee363 11768dwarf_tag_name (unsigned tag)
c906108c
SS
11769{
11770 switch (tag)
11771 {
11772 case DW_TAG_padding:
11773 return "DW_TAG_padding";
11774 case DW_TAG_array_type:
11775 return "DW_TAG_array_type";
11776 case DW_TAG_class_type:
11777 return "DW_TAG_class_type";
11778 case DW_TAG_entry_point:
11779 return "DW_TAG_entry_point";
11780 case DW_TAG_enumeration_type:
11781 return "DW_TAG_enumeration_type";
11782 case DW_TAG_formal_parameter:
11783 return "DW_TAG_formal_parameter";
11784 case DW_TAG_imported_declaration:
11785 return "DW_TAG_imported_declaration";
11786 case DW_TAG_label:
11787 return "DW_TAG_label";
11788 case DW_TAG_lexical_block:
11789 return "DW_TAG_lexical_block";
11790 case DW_TAG_member:
11791 return "DW_TAG_member";
11792 case DW_TAG_pointer_type:
11793 return "DW_TAG_pointer_type";
11794 case DW_TAG_reference_type:
11795 return "DW_TAG_reference_type";
11796 case DW_TAG_compile_unit:
11797 return "DW_TAG_compile_unit";
11798 case DW_TAG_string_type:
11799 return "DW_TAG_string_type";
11800 case DW_TAG_structure_type:
11801 return "DW_TAG_structure_type";
11802 case DW_TAG_subroutine_type:
11803 return "DW_TAG_subroutine_type";
11804 case DW_TAG_typedef:
11805 return "DW_TAG_typedef";
11806 case DW_TAG_union_type:
11807 return "DW_TAG_union_type";
11808 case DW_TAG_unspecified_parameters:
11809 return "DW_TAG_unspecified_parameters";
11810 case DW_TAG_variant:
11811 return "DW_TAG_variant";
11812 case DW_TAG_common_block:
11813 return "DW_TAG_common_block";
11814 case DW_TAG_common_inclusion:
11815 return "DW_TAG_common_inclusion";
11816 case DW_TAG_inheritance:
11817 return "DW_TAG_inheritance";
11818 case DW_TAG_inlined_subroutine:
11819 return "DW_TAG_inlined_subroutine";
11820 case DW_TAG_module:
11821 return "DW_TAG_module";
11822 case DW_TAG_ptr_to_member_type:
11823 return "DW_TAG_ptr_to_member_type";
11824 case DW_TAG_set_type:
11825 return "DW_TAG_set_type";
11826 case DW_TAG_subrange_type:
11827 return "DW_TAG_subrange_type";
11828 case DW_TAG_with_stmt:
11829 return "DW_TAG_with_stmt";
11830 case DW_TAG_access_declaration:
11831 return "DW_TAG_access_declaration";
11832 case DW_TAG_base_type:
11833 return "DW_TAG_base_type";
11834 case DW_TAG_catch_block:
11835 return "DW_TAG_catch_block";
11836 case DW_TAG_const_type:
11837 return "DW_TAG_const_type";
11838 case DW_TAG_constant:
11839 return "DW_TAG_constant";
11840 case DW_TAG_enumerator:
11841 return "DW_TAG_enumerator";
11842 case DW_TAG_file_type:
11843 return "DW_TAG_file_type";
11844 case DW_TAG_friend:
11845 return "DW_TAG_friend";
11846 case DW_TAG_namelist:
11847 return "DW_TAG_namelist";
11848 case DW_TAG_namelist_item:
11849 return "DW_TAG_namelist_item";
11850 case DW_TAG_packed_type:
11851 return "DW_TAG_packed_type";
11852 case DW_TAG_subprogram:
11853 return "DW_TAG_subprogram";
11854 case DW_TAG_template_type_param:
11855 return "DW_TAG_template_type_param";
11856 case DW_TAG_template_value_param:
11857 return "DW_TAG_template_value_param";
11858 case DW_TAG_thrown_type:
11859 return "DW_TAG_thrown_type";
11860 case DW_TAG_try_block:
11861 return "DW_TAG_try_block";
11862 case DW_TAG_variant_part:
11863 return "DW_TAG_variant_part";
11864 case DW_TAG_variable:
11865 return "DW_TAG_variable";
11866 case DW_TAG_volatile_type:
11867 return "DW_TAG_volatile_type";
d9fa45fe
DC
11868 case DW_TAG_dwarf_procedure:
11869 return "DW_TAG_dwarf_procedure";
11870 case DW_TAG_restrict_type:
11871 return "DW_TAG_restrict_type";
11872 case DW_TAG_interface_type:
11873 return "DW_TAG_interface_type";
11874 case DW_TAG_namespace:
11875 return "DW_TAG_namespace";
11876 case DW_TAG_imported_module:
11877 return "DW_TAG_imported_module";
11878 case DW_TAG_unspecified_type:
11879 return "DW_TAG_unspecified_type";
11880 case DW_TAG_partial_unit:
11881 return "DW_TAG_partial_unit";
11882 case DW_TAG_imported_unit:
11883 return "DW_TAG_imported_unit";
b7619582
GF
11884 case DW_TAG_condition:
11885 return "DW_TAG_condition";
11886 case DW_TAG_shared_type:
11887 return "DW_TAG_shared_type";
348e048f
DE
11888 case DW_TAG_type_unit:
11889 return "DW_TAG_type_unit";
c906108c
SS
11890 case DW_TAG_MIPS_loop:
11891 return "DW_TAG_MIPS_loop";
b7619582
GF
11892 case DW_TAG_HP_array_descriptor:
11893 return "DW_TAG_HP_array_descriptor";
c906108c
SS
11894 case DW_TAG_format_label:
11895 return "DW_TAG_format_label";
11896 case DW_TAG_function_template:
11897 return "DW_TAG_function_template";
11898 case DW_TAG_class_template:
11899 return "DW_TAG_class_template";
b7619582
GF
11900 case DW_TAG_GNU_BINCL:
11901 return "DW_TAG_GNU_BINCL";
11902 case DW_TAG_GNU_EINCL:
11903 return "DW_TAG_GNU_EINCL";
11904 case DW_TAG_upc_shared_type:
11905 return "DW_TAG_upc_shared_type";
11906 case DW_TAG_upc_strict_type:
11907 return "DW_TAG_upc_strict_type";
11908 case DW_TAG_upc_relaxed_type:
11909 return "DW_TAG_upc_relaxed_type";
11910 case DW_TAG_PGI_kanji_type:
11911 return "DW_TAG_PGI_kanji_type";
11912 case DW_TAG_PGI_interface_block:
11913 return "DW_TAG_PGI_interface_block";
c906108c
SS
11914 default:
11915 return "DW_TAG_<unknown>";
11916 }
11917}
11918
11919/* Convert a DWARF attribute code into its string name. */
11920
11921static char *
aa1ee363 11922dwarf_attr_name (unsigned attr)
c906108c
SS
11923{
11924 switch (attr)
11925 {
11926 case DW_AT_sibling:
11927 return "DW_AT_sibling";
11928 case DW_AT_location:
11929 return "DW_AT_location";
11930 case DW_AT_name:
11931 return "DW_AT_name";
11932 case DW_AT_ordering:
11933 return "DW_AT_ordering";
11934 case DW_AT_subscr_data:
11935 return "DW_AT_subscr_data";
11936 case DW_AT_byte_size:
11937 return "DW_AT_byte_size";
11938 case DW_AT_bit_offset:
11939 return "DW_AT_bit_offset";
11940 case DW_AT_bit_size:
11941 return "DW_AT_bit_size";
11942 case DW_AT_element_list:
11943 return "DW_AT_element_list";
11944 case DW_AT_stmt_list:
11945 return "DW_AT_stmt_list";
11946 case DW_AT_low_pc:
11947 return "DW_AT_low_pc";
11948 case DW_AT_high_pc:
11949 return "DW_AT_high_pc";
11950 case DW_AT_language:
11951 return "DW_AT_language";
11952 case DW_AT_member:
11953 return "DW_AT_member";
11954 case DW_AT_discr:
11955 return "DW_AT_discr";
11956 case DW_AT_discr_value:
11957 return "DW_AT_discr_value";
11958 case DW_AT_visibility:
11959 return "DW_AT_visibility";
11960 case DW_AT_import:
11961 return "DW_AT_import";
11962 case DW_AT_string_length:
11963 return "DW_AT_string_length";
11964 case DW_AT_common_reference:
11965 return "DW_AT_common_reference";
11966 case DW_AT_comp_dir:
11967 return "DW_AT_comp_dir";
11968 case DW_AT_const_value:
11969 return "DW_AT_const_value";
11970 case DW_AT_containing_type:
11971 return "DW_AT_containing_type";
11972 case DW_AT_default_value:
11973 return "DW_AT_default_value";
11974 case DW_AT_inline:
11975 return "DW_AT_inline";
11976 case DW_AT_is_optional:
11977 return "DW_AT_is_optional";
11978 case DW_AT_lower_bound:
11979 return "DW_AT_lower_bound";
11980 case DW_AT_producer:
11981 return "DW_AT_producer";
11982 case DW_AT_prototyped:
11983 return "DW_AT_prototyped";
11984 case DW_AT_return_addr:
11985 return "DW_AT_return_addr";
11986 case DW_AT_start_scope:
11987 return "DW_AT_start_scope";
09fa0d7c
JK
11988 case DW_AT_bit_stride:
11989 return "DW_AT_bit_stride";
c906108c
SS
11990 case DW_AT_upper_bound:
11991 return "DW_AT_upper_bound";
11992 case DW_AT_abstract_origin:
11993 return "DW_AT_abstract_origin";
11994 case DW_AT_accessibility:
11995 return "DW_AT_accessibility";
11996 case DW_AT_address_class:
11997 return "DW_AT_address_class";
11998 case DW_AT_artificial:
11999 return "DW_AT_artificial";
12000 case DW_AT_base_types:
12001 return "DW_AT_base_types";
12002 case DW_AT_calling_convention:
12003 return "DW_AT_calling_convention";
12004 case DW_AT_count:
12005 return "DW_AT_count";
12006 case DW_AT_data_member_location:
12007 return "DW_AT_data_member_location";
12008 case DW_AT_decl_column:
12009 return "DW_AT_decl_column";
12010 case DW_AT_decl_file:
12011 return "DW_AT_decl_file";
12012 case DW_AT_decl_line:
12013 return "DW_AT_decl_line";
12014 case DW_AT_declaration:
12015 return "DW_AT_declaration";
12016 case DW_AT_discr_list:
12017 return "DW_AT_discr_list";
12018 case DW_AT_encoding:
12019 return "DW_AT_encoding";
12020 case DW_AT_external:
12021 return "DW_AT_external";
12022 case DW_AT_frame_base:
12023 return "DW_AT_frame_base";
12024 case DW_AT_friend:
12025 return "DW_AT_friend";
12026 case DW_AT_identifier_case:
12027 return "DW_AT_identifier_case";
12028 case DW_AT_macro_info:
12029 return "DW_AT_macro_info";
12030 case DW_AT_namelist_items:
12031 return "DW_AT_namelist_items";
12032 case DW_AT_priority:
12033 return "DW_AT_priority";
12034 case DW_AT_segment:
12035 return "DW_AT_segment";
12036 case DW_AT_specification:
12037 return "DW_AT_specification";
12038 case DW_AT_static_link:
12039 return "DW_AT_static_link";
12040 case DW_AT_type:
12041 return "DW_AT_type";
12042 case DW_AT_use_location:
12043 return "DW_AT_use_location";
12044 case DW_AT_variable_parameter:
12045 return "DW_AT_variable_parameter";
12046 case DW_AT_virtuality:
12047 return "DW_AT_virtuality";
12048 case DW_AT_vtable_elem_location:
12049 return "DW_AT_vtable_elem_location";
b7619582 12050 /* DWARF 3 values. */
d9fa45fe
DC
12051 case DW_AT_allocated:
12052 return "DW_AT_allocated";
12053 case DW_AT_associated:
12054 return "DW_AT_associated";
12055 case DW_AT_data_location:
12056 return "DW_AT_data_location";
09fa0d7c
JK
12057 case DW_AT_byte_stride:
12058 return "DW_AT_byte_stride";
d9fa45fe
DC
12059 case DW_AT_entry_pc:
12060 return "DW_AT_entry_pc";
12061 case DW_AT_use_UTF8:
12062 return "DW_AT_use_UTF8";
12063 case DW_AT_extension:
12064 return "DW_AT_extension";
12065 case DW_AT_ranges:
12066 return "DW_AT_ranges";
12067 case DW_AT_trampoline:
12068 return "DW_AT_trampoline";
12069 case DW_AT_call_column:
12070 return "DW_AT_call_column";
12071 case DW_AT_call_file:
12072 return "DW_AT_call_file";
12073 case DW_AT_call_line:
12074 return "DW_AT_call_line";
b7619582
GF
12075 case DW_AT_description:
12076 return "DW_AT_description";
12077 case DW_AT_binary_scale:
12078 return "DW_AT_binary_scale";
12079 case DW_AT_decimal_scale:
12080 return "DW_AT_decimal_scale";
12081 case DW_AT_small:
12082 return "DW_AT_small";
12083 case DW_AT_decimal_sign:
12084 return "DW_AT_decimal_sign";
12085 case DW_AT_digit_count:
12086 return "DW_AT_digit_count";
12087 case DW_AT_picture_string:
12088 return "DW_AT_picture_string";
12089 case DW_AT_mutable:
12090 return "DW_AT_mutable";
12091 case DW_AT_threads_scaled:
12092 return "DW_AT_threads_scaled";
12093 case DW_AT_explicit:
12094 return "DW_AT_explicit";
12095 case DW_AT_object_pointer:
12096 return "DW_AT_object_pointer";
12097 case DW_AT_endianity:
12098 return "DW_AT_endianity";
12099 case DW_AT_elemental:
12100 return "DW_AT_elemental";
12101 case DW_AT_pure:
12102 return "DW_AT_pure";
12103 case DW_AT_recursive:
12104 return "DW_AT_recursive";
348e048f
DE
12105 /* DWARF 4 values. */
12106 case DW_AT_signature:
12107 return "DW_AT_signature";
31ef98ae
TT
12108 case DW_AT_linkage_name:
12109 return "DW_AT_linkage_name";
b7619582 12110 /* SGI/MIPS extensions. */
c764a876 12111#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12112 case DW_AT_MIPS_fde:
12113 return "DW_AT_MIPS_fde";
c764a876 12114#endif
c906108c
SS
12115 case DW_AT_MIPS_loop_begin:
12116 return "DW_AT_MIPS_loop_begin";
12117 case DW_AT_MIPS_tail_loop_begin:
12118 return "DW_AT_MIPS_tail_loop_begin";
12119 case DW_AT_MIPS_epilog_begin:
12120 return "DW_AT_MIPS_epilog_begin";
12121 case DW_AT_MIPS_loop_unroll_factor:
12122 return "DW_AT_MIPS_loop_unroll_factor";
12123 case DW_AT_MIPS_software_pipeline_depth:
12124 return "DW_AT_MIPS_software_pipeline_depth";
12125 case DW_AT_MIPS_linkage_name:
12126 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12127 case DW_AT_MIPS_stride:
12128 return "DW_AT_MIPS_stride";
12129 case DW_AT_MIPS_abstract_name:
12130 return "DW_AT_MIPS_abstract_name";
12131 case DW_AT_MIPS_clone_origin:
12132 return "DW_AT_MIPS_clone_origin";
12133 case DW_AT_MIPS_has_inlines:
12134 return "DW_AT_MIPS_has_inlines";
b7619582 12135 /* HP extensions. */
c764a876 12136#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12137 case DW_AT_HP_block_index:
12138 return "DW_AT_HP_block_index";
c764a876 12139#endif
b7619582
GF
12140 case DW_AT_HP_unmodifiable:
12141 return "DW_AT_HP_unmodifiable";
12142 case DW_AT_HP_actuals_stmt_list:
12143 return "DW_AT_HP_actuals_stmt_list";
12144 case DW_AT_HP_proc_per_section:
12145 return "DW_AT_HP_proc_per_section";
12146 case DW_AT_HP_raw_data_ptr:
12147 return "DW_AT_HP_raw_data_ptr";
12148 case DW_AT_HP_pass_by_reference:
12149 return "DW_AT_HP_pass_by_reference";
12150 case DW_AT_HP_opt_level:
12151 return "DW_AT_HP_opt_level";
12152 case DW_AT_HP_prof_version_id:
12153 return "DW_AT_HP_prof_version_id";
12154 case DW_AT_HP_opt_flags:
12155 return "DW_AT_HP_opt_flags";
12156 case DW_AT_HP_cold_region_low_pc:
12157 return "DW_AT_HP_cold_region_low_pc";
12158 case DW_AT_HP_cold_region_high_pc:
12159 return "DW_AT_HP_cold_region_high_pc";
12160 case DW_AT_HP_all_variables_modifiable:
12161 return "DW_AT_HP_all_variables_modifiable";
12162 case DW_AT_HP_linkage_name:
12163 return "DW_AT_HP_linkage_name";
12164 case DW_AT_HP_prof_flags:
12165 return "DW_AT_HP_prof_flags";
12166 /* GNU extensions. */
c906108c
SS
12167 case DW_AT_sf_names:
12168 return "DW_AT_sf_names";
12169 case DW_AT_src_info:
12170 return "DW_AT_src_info";
12171 case DW_AT_mac_info:
12172 return "DW_AT_mac_info";
12173 case DW_AT_src_coords:
12174 return "DW_AT_src_coords";
12175 case DW_AT_body_begin:
12176 return "DW_AT_body_begin";
12177 case DW_AT_body_end:
12178 return "DW_AT_body_end";
f5f8a009
EZ
12179 case DW_AT_GNU_vector:
12180 return "DW_AT_GNU_vector";
2de00c64
DE
12181 case DW_AT_GNU_odr_signature:
12182 return "DW_AT_GNU_odr_signature";
b7619582
GF
12183 /* VMS extensions. */
12184 case DW_AT_VMS_rtnbeg_pd_address:
12185 return "DW_AT_VMS_rtnbeg_pd_address";
12186 /* UPC extension. */
12187 case DW_AT_upc_threads_scaled:
12188 return "DW_AT_upc_threads_scaled";
12189 /* PGI (STMicroelectronics) extensions. */
12190 case DW_AT_PGI_lbase:
12191 return "DW_AT_PGI_lbase";
12192 case DW_AT_PGI_soffset:
12193 return "DW_AT_PGI_soffset";
12194 case DW_AT_PGI_lstride:
12195 return "DW_AT_PGI_lstride";
c906108c
SS
12196 default:
12197 return "DW_AT_<unknown>";
12198 }
12199}
12200
12201/* Convert a DWARF value form code into its string name. */
12202
12203static char *
aa1ee363 12204dwarf_form_name (unsigned form)
c906108c
SS
12205{
12206 switch (form)
12207 {
12208 case DW_FORM_addr:
12209 return "DW_FORM_addr";
12210 case DW_FORM_block2:
12211 return "DW_FORM_block2";
12212 case DW_FORM_block4:
12213 return "DW_FORM_block4";
12214 case DW_FORM_data2:
12215 return "DW_FORM_data2";
12216 case DW_FORM_data4:
12217 return "DW_FORM_data4";
12218 case DW_FORM_data8:
12219 return "DW_FORM_data8";
12220 case DW_FORM_string:
12221 return "DW_FORM_string";
12222 case DW_FORM_block:
12223 return "DW_FORM_block";
12224 case DW_FORM_block1:
12225 return "DW_FORM_block1";
12226 case DW_FORM_data1:
12227 return "DW_FORM_data1";
12228 case DW_FORM_flag:
12229 return "DW_FORM_flag";
12230 case DW_FORM_sdata:
12231 return "DW_FORM_sdata";
12232 case DW_FORM_strp:
12233 return "DW_FORM_strp";
12234 case DW_FORM_udata:
12235 return "DW_FORM_udata";
12236 case DW_FORM_ref_addr:
12237 return "DW_FORM_ref_addr";
12238 case DW_FORM_ref1:
12239 return "DW_FORM_ref1";
12240 case DW_FORM_ref2:
12241 return "DW_FORM_ref2";
12242 case DW_FORM_ref4:
12243 return "DW_FORM_ref4";
12244 case DW_FORM_ref8:
12245 return "DW_FORM_ref8";
12246 case DW_FORM_ref_udata:
12247 return "DW_FORM_ref_udata";
12248 case DW_FORM_indirect:
12249 return "DW_FORM_indirect";
348e048f
DE
12250 case DW_FORM_sec_offset:
12251 return "DW_FORM_sec_offset";
12252 case DW_FORM_exprloc:
12253 return "DW_FORM_exprloc";
12254 case DW_FORM_flag_present:
12255 return "DW_FORM_flag_present";
12256 case DW_FORM_sig8:
12257 return "DW_FORM_sig8";
c906108c
SS
12258 default:
12259 return "DW_FORM_<unknown>";
12260 }
12261}
12262
12263/* Convert a DWARF stack opcode into its string name. */
12264
9eae7c52
TT
12265const char *
12266dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12267{
12268 switch (op)
12269 {
12270 case DW_OP_addr:
12271 return "DW_OP_addr";
12272 case DW_OP_deref:
12273 return "DW_OP_deref";
12274 case DW_OP_const1u:
12275 return "DW_OP_const1u";
12276 case DW_OP_const1s:
12277 return "DW_OP_const1s";
12278 case DW_OP_const2u:
12279 return "DW_OP_const2u";
12280 case DW_OP_const2s:
12281 return "DW_OP_const2s";
12282 case DW_OP_const4u:
12283 return "DW_OP_const4u";
12284 case DW_OP_const4s:
12285 return "DW_OP_const4s";
12286 case DW_OP_const8u:
12287 return "DW_OP_const8u";
12288 case DW_OP_const8s:
12289 return "DW_OP_const8s";
12290 case DW_OP_constu:
12291 return "DW_OP_constu";
12292 case DW_OP_consts:
12293 return "DW_OP_consts";
12294 case DW_OP_dup:
12295 return "DW_OP_dup";
12296 case DW_OP_drop:
12297 return "DW_OP_drop";
12298 case DW_OP_over:
12299 return "DW_OP_over";
12300 case DW_OP_pick:
12301 return "DW_OP_pick";
12302 case DW_OP_swap:
12303 return "DW_OP_swap";
12304 case DW_OP_rot:
12305 return "DW_OP_rot";
12306 case DW_OP_xderef:
12307 return "DW_OP_xderef";
12308 case DW_OP_abs:
12309 return "DW_OP_abs";
12310 case DW_OP_and:
12311 return "DW_OP_and";
12312 case DW_OP_div:
12313 return "DW_OP_div";
12314 case DW_OP_minus:
12315 return "DW_OP_minus";
12316 case DW_OP_mod:
12317 return "DW_OP_mod";
12318 case DW_OP_mul:
12319 return "DW_OP_mul";
12320 case DW_OP_neg:
12321 return "DW_OP_neg";
12322 case DW_OP_not:
12323 return "DW_OP_not";
12324 case DW_OP_or:
12325 return "DW_OP_or";
12326 case DW_OP_plus:
12327 return "DW_OP_plus";
12328 case DW_OP_plus_uconst:
12329 return "DW_OP_plus_uconst";
12330 case DW_OP_shl:
12331 return "DW_OP_shl";
12332 case DW_OP_shr:
12333 return "DW_OP_shr";
12334 case DW_OP_shra:
12335 return "DW_OP_shra";
12336 case DW_OP_xor:
12337 return "DW_OP_xor";
12338 case DW_OP_bra:
12339 return "DW_OP_bra";
12340 case DW_OP_eq:
12341 return "DW_OP_eq";
12342 case DW_OP_ge:
12343 return "DW_OP_ge";
12344 case DW_OP_gt:
12345 return "DW_OP_gt";
12346 case DW_OP_le:
12347 return "DW_OP_le";
12348 case DW_OP_lt:
12349 return "DW_OP_lt";
12350 case DW_OP_ne:
12351 return "DW_OP_ne";
12352 case DW_OP_skip:
12353 return "DW_OP_skip";
12354 case DW_OP_lit0:
12355 return "DW_OP_lit0";
12356 case DW_OP_lit1:
12357 return "DW_OP_lit1";
12358 case DW_OP_lit2:
12359 return "DW_OP_lit2";
12360 case DW_OP_lit3:
12361 return "DW_OP_lit3";
12362 case DW_OP_lit4:
12363 return "DW_OP_lit4";
12364 case DW_OP_lit5:
12365 return "DW_OP_lit5";
12366 case DW_OP_lit6:
12367 return "DW_OP_lit6";
12368 case DW_OP_lit7:
12369 return "DW_OP_lit7";
12370 case DW_OP_lit8:
12371 return "DW_OP_lit8";
12372 case DW_OP_lit9:
12373 return "DW_OP_lit9";
12374 case DW_OP_lit10:
12375 return "DW_OP_lit10";
12376 case DW_OP_lit11:
12377 return "DW_OP_lit11";
12378 case DW_OP_lit12:
12379 return "DW_OP_lit12";
12380 case DW_OP_lit13:
12381 return "DW_OP_lit13";
12382 case DW_OP_lit14:
12383 return "DW_OP_lit14";
12384 case DW_OP_lit15:
12385 return "DW_OP_lit15";
12386 case DW_OP_lit16:
12387 return "DW_OP_lit16";
12388 case DW_OP_lit17:
12389 return "DW_OP_lit17";
12390 case DW_OP_lit18:
12391 return "DW_OP_lit18";
12392 case DW_OP_lit19:
12393 return "DW_OP_lit19";
12394 case DW_OP_lit20:
12395 return "DW_OP_lit20";
12396 case DW_OP_lit21:
12397 return "DW_OP_lit21";
12398 case DW_OP_lit22:
12399 return "DW_OP_lit22";
12400 case DW_OP_lit23:
12401 return "DW_OP_lit23";
12402 case DW_OP_lit24:
12403 return "DW_OP_lit24";
12404 case DW_OP_lit25:
12405 return "DW_OP_lit25";
12406 case DW_OP_lit26:
12407 return "DW_OP_lit26";
12408 case DW_OP_lit27:
12409 return "DW_OP_lit27";
12410 case DW_OP_lit28:
12411 return "DW_OP_lit28";
12412 case DW_OP_lit29:
12413 return "DW_OP_lit29";
12414 case DW_OP_lit30:
12415 return "DW_OP_lit30";
12416 case DW_OP_lit31:
12417 return "DW_OP_lit31";
12418 case DW_OP_reg0:
12419 return "DW_OP_reg0";
12420 case DW_OP_reg1:
12421 return "DW_OP_reg1";
12422 case DW_OP_reg2:
12423 return "DW_OP_reg2";
12424 case DW_OP_reg3:
12425 return "DW_OP_reg3";
12426 case DW_OP_reg4:
12427 return "DW_OP_reg4";
12428 case DW_OP_reg5:
12429 return "DW_OP_reg5";
12430 case DW_OP_reg6:
12431 return "DW_OP_reg6";
12432 case DW_OP_reg7:
12433 return "DW_OP_reg7";
12434 case DW_OP_reg8:
12435 return "DW_OP_reg8";
12436 case DW_OP_reg9:
12437 return "DW_OP_reg9";
12438 case DW_OP_reg10:
12439 return "DW_OP_reg10";
12440 case DW_OP_reg11:
12441 return "DW_OP_reg11";
12442 case DW_OP_reg12:
12443 return "DW_OP_reg12";
12444 case DW_OP_reg13:
12445 return "DW_OP_reg13";
12446 case DW_OP_reg14:
12447 return "DW_OP_reg14";
12448 case DW_OP_reg15:
12449 return "DW_OP_reg15";
12450 case DW_OP_reg16:
12451 return "DW_OP_reg16";
12452 case DW_OP_reg17:
12453 return "DW_OP_reg17";
12454 case DW_OP_reg18:
12455 return "DW_OP_reg18";
12456 case DW_OP_reg19:
12457 return "DW_OP_reg19";
12458 case DW_OP_reg20:
12459 return "DW_OP_reg20";
12460 case DW_OP_reg21:
12461 return "DW_OP_reg21";
12462 case DW_OP_reg22:
12463 return "DW_OP_reg22";
12464 case DW_OP_reg23:
12465 return "DW_OP_reg23";
12466 case DW_OP_reg24:
12467 return "DW_OP_reg24";
12468 case DW_OP_reg25:
12469 return "DW_OP_reg25";
12470 case DW_OP_reg26:
12471 return "DW_OP_reg26";
12472 case DW_OP_reg27:
12473 return "DW_OP_reg27";
12474 case DW_OP_reg28:
12475 return "DW_OP_reg28";
12476 case DW_OP_reg29:
12477 return "DW_OP_reg29";
12478 case DW_OP_reg30:
12479 return "DW_OP_reg30";
12480 case DW_OP_reg31:
12481 return "DW_OP_reg31";
12482 case DW_OP_breg0:
12483 return "DW_OP_breg0";
12484 case DW_OP_breg1:
12485 return "DW_OP_breg1";
12486 case DW_OP_breg2:
12487 return "DW_OP_breg2";
12488 case DW_OP_breg3:
12489 return "DW_OP_breg3";
12490 case DW_OP_breg4:
12491 return "DW_OP_breg4";
12492 case DW_OP_breg5:
12493 return "DW_OP_breg5";
12494 case DW_OP_breg6:
12495 return "DW_OP_breg6";
12496 case DW_OP_breg7:
12497 return "DW_OP_breg7";
12498 case DW_OP_breg8:
12499 return "DW_OP_breg8";
12500 case DW_OP_breg9:
12501 return "DW_OP_breg9";
12502 case DW_OP_breg10:
12503 return "DW_OP_breg10";
12504 case DW_OP_breg11:
12505 return "DW_OP_breg11";
12506 case DW_OP_breg12:
12507 return "DW_OP_breg12";
12508 case DW_OP_breg13:
12509 return "DW_OP_breg13";
12510 case DW_OP_breg14:
12511 return "DW_OP_breg14";
12512 case DW_OP_breg15:
12513 return "DW_OP_breg15";
12514 case DW_OP_breg16:
12515 return "DW_OP_breg16";
12516 case DW_OP_breg17:
12517 return "DW_OP_breg17";
12518 case DW_OP_breg18:
12519 return "DW_OP_breg18";
12520 case DW_OP_breg19:
12521 return "DW_OP_breg19";
12522 case DW_OP_breg20:
12523 return "DW_OP_breg20";
12524 case DW_OP_breg21:
12525 return "DW_OP_breg21";
12526 case DW_OP_breg22:
12527 return "DW_OP_breg22";
12528 case DW_OP_breg23:
12529 return "DW_OP_breg23";
12530 case DW_OP_breg24:
12531 return "DW_OP_breg24";
12532 case DW_OP_breg25:
12533 return "DW_OP_breg25";
12534 case DW_OP_breg26:
12535 return "DW_OP_breg26";
12536 case DW_OP_breg27:
12537 return "DW_OP_breg27";
12538 case DW_OP_breg28:
12539 return "DW_OP_breg28";
12540 case DW_OP_breg29:
12541 return "DW_OP_breg29";
12542 case DW_OP_breg30:
12543 return "DW_OP_breg30";
12544 case DW_OP_breg31:
12545 return "DW_OP_breg31";
12546 case DW_OP_regx:
12547 return "DW_OP_regx";
12548 case DW_OP_fbreg:
12549 return "DW_OP_fbreg";
12550 case DW_OP_bregx:
12551 return "DW_OP_bregx";
12552 case DW_OP_piece:
12553 return "DW_OP_piece";
12554 case DW_OP_deref_size:
12555 return "DW_OP_deref_size";
12556 case DW_OP_xderef_size:
12557 return "DW_OP_xderef_size";
12558 case DW_OP_nop:
12559 return "DW_OP_nop";
b7619582 12560 /* DWARF 3 extensions. */
ed348acc
EZ
12561 case DW_OP_push_object_address:
12562 return "DW_OP_push_object_address";
12563 case DW_OP_call2:
12564 return "DW_OP_call2";
12565 case DW_OP_call4:
12566 return "DW_OP_call4";
12567 case DW_OP_call_ref:
12568 return "DW_OP_call_ref";
b7619582
GF
12569 case DW_OP_form_tls_address:
12570 return "DW_OP_form_tls_address";
12571 case DW_OP_call_frame_cfa:
12572 return "DW_OP_call_frame_cfa";
12573 case DW_OP_bit_piece:
12574 return "DW_OP_bit_piece";
9eae7c52
TT
12575 /* DWARF 4 extensions. */
12576 case DW_OP_implicit_value:
12577 return "DW_OP_implicit_value";
12578 case DW_OP_stack_value:
12579 return "DW_OP_stack_value";
12580 /* GNU extensions. */
ed348acc
EZ
12581 case DW_OP_GNU_push_tls_address:
12582 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12583 case DW_OP_GNU_uninit:
12584 return "DW_OP_GNU_uninit";
c906108c 12585 default:
9eae7c52 12586 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12587 }
12588}
12589
12590static char *
fba45db2 12591dwarf_bool_name (unsigned mybool)
c906108c
SS
12592{
12593 if (mybool)
12594 return "TRUE";
12595 else
12596 return "FALSE";
12597}
12598
12599/* Convert a DWARF type code into its string name. */
12600
12601static char *
aa1ee363 12602dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12603{
12604 switch (enc)
12605 {
b7619582
GF
12606 case DW_ATE_void:
12607 return "DW_ATE_void";
c906108c
SS
12608 case DW_ATE_address:
12609 return "DW_ATE_address";
12610 case DW_ATE_boolean:
12611 return "DW_ATE_boolean";
12612 case DW_ATE_complex_float:
12613 return "DW_ATE_complex_float";
12614 case DW_ATE_float:
12615 return "DW_ATE_float";
12616 case DW_ATE_signed:
12617 return "DW_ATE_signed";
12618 case DW_ATE_signed_char:
12619 return "DW_ATE_signed_char";
12620 case DW_ATE_unsigned:
12621 return "DW_ATE_unsigned";
12622 case DW_ATE_unsigned_char:
12623 return "DW_ATE_unsigned_char";
b7619582 12624 /* DWARF 3. */
d9fa45fe
DC
12625 case DW_ATE_imaginary_float:
12626 return "DW_ATE_imaginary_float";
b7619582
GF
12627 case DW_ATE_packed_decimal:
12628 return "DW_ATE_packed_decimal";
12629 case DW_ATE_numeric_string:
12630 return "DW_ATE_numeric_string";
12631 case DW_ATE_edited:
12632 return "DW_ATE_edited";
12633 case DW_ATE_signed_fixed:
12634 return "DW_ATE_signed_fixed";
12635 case DW_ATE_unsigned_fixed:
12636 return "DW_ATE_unsigned_fixed";
12637 case DW_ATE_decimal_float:
12638 return "DW_ATE_decimal_float";
75079b2b
TT
12639 /* DWARF 4. */
12640 case DW_ATE_UTF:
12641 return "DW_ATE_UTF";
b7619582
GF
12642 /* HP extensions. */
12643 case DW_ATE_HP_float80:
12644 return "DW_ATE_HP_float80";
12645 case DW_ATE_HP_complex_float80:
12646 return "DW_ATE_HP_complex_float80";
12647 case DW_ATE_HP_float128:
12648 return "DW_ATE_HP_float128";
12649 case DW_ATE_HP_complex_float128:
12650 return "DW_ATE_HP_complex_float128";
12651 case DW_ATE_HP_floathpintel:
12652 return "DW_ATE_HP_floathpintel";
12653 case DW_ATE_HP_imaginary_float80:
12654 return "DW_ATE_HP_imaginary_float80";
12655 case DW_ATE_HP_imaginary_float128:
12656 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12657 default:
12658 return "DW_ATE_<unknown>";
12659 }
12660}
12661
12662/* Convert a DWARF call frame info operation to its string name. */
12663
12664#if 0
12665static char *
aa1ee363 12666dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12667{
12668 switch (cfi_opc)
12669 {
12670 case DW_CFA_advance_loc:
12671 return "DW_CFA_advance_loc";
12672 case DW_CFA_offset:
12673 return "DW_CFA_offset";
12674 case DW_CFA_restore:
12675 return "DW_CFA_restore";
12676 case DW_CFA_nop:
12677 return "DW_CFA_nop";
12678 case DW_CFA_set_loc:
12679 return "DW_CFA_set_loc";
12680 case DW_CFA_advance_loc1:
12681 return "DW_CFA_advance_loc1";
12682 case DW_CFA_advance_loc2:
12683 return "DW_CFA_advance_loc2";
12684 case DW_CFA_advance_loc4:
12685 return "DW_CFA_advance_loc4";
12686 case DW_CFA_offset_extended:
12687 return "DW_CFA_offset_extended";
12688 case DW_CFA_restore_extended:
12689 return "DW_CFA_restore_extended";
12690 case DW_CFA_undefined:
12691 return "DW_CFA_undefined";
12692 case DW_CFA_same_value:
12693 return "DW_CFA_same_value";
12694 case DW_CFA_register:
12695 return "DW_CFA_register";
12696 case DW_CFA_remember_state:
12697 return "DW_CFA_remember_state";
12698 case DW_CFA_restore_state:
12699 return "DW_CFA_restore_state";
12700 case DW_CFA_def_cfa:
12701 return "DW_CFA_def_cfa";
12702 case DW_CFA_def_cfa_register:
12703 return "DW_CFA_def_cfa_register";
12704 case DW_CFA_def_cfa_offset:
12705 return "DW_CFA_def_cfa_offset";
b7619582 12706 /* DWARF 3. */
985cb1a3
JM
12707 case DW_CFA_def_cfa_expression:
12708 return "DW_CFA_def_cfa_expression";
12709 case DW_CFA_expression:
12710 return "DW_CFA_expression";
12711 case DW_CFA_offset_extended_sf:
12712 return "DW_CFA_offset_extended_sf";
12713 case DW_CFA_def_cfa_sf:
12714 return "DW_CFA_def_cfa_sf";
12715 case DW_CFA_def_cfa_offset_sf:
12716 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12717 case DW_CFA_val_offset:
12718 return "DW_CFA_val_offset";
12719 case DW_CFA_val_offset_sf:
12720 return "DW_CFA_val_offset_sf";
12721 case DW_CFA_val_expression:
12722 return "DW_CFA_val_expression";
12723 /* SGI/MIPS specific. */
c906108c
SS
12724 case DW_CFA_MIPS_advance_loc8:
12725 return "DW_CFA_MIPS_advance_loc8";
b7619582 12726 /* GNU extensions. */
985cb1a3
JM
12727 case DW_CFA_GNU_window_save:
12728 return "DW_CFA_GNU_window_save";
12729 case DW_CFA_GNU_args_size:
12730 return "DW_CFA_GNU_args_size";
12731 case DW_CFA_GNU_negative_offset_extended:
12732 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12733 default:
12734 return "DW_CFA_<unknown>";
12735 }
12736}
12737#endif
12738
f9aca02d 12739static void
d97bc12b 12740dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12741{
12742 unsigned int i;
12743
d97bc12b
DE
12744 print_spaces (indent, f);
12745 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12746 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12747
12748 if (die->parent != NULL)
12749 {
12750 print_spaces (indent, f);
12751 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12752 die->parent->offset);
12753 }
12754
12755 print_spaces (indent, f);
12756 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12757 dwarf_bool_name (die->child != NULL));
c906108c 12758
d97bc12b
DE
12759 print_spaces (indent, f);
12760 fprintf_unfiltered (f, " attributes:\n");
12761
c906108c
SS
12762 for (i = 0; i < die->num_attrs; ++i)
12763 {
d97bc12b
DE
12764 print_spaces (indent, f);
12765 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12766 dwarf_attr_name (die->attrs[i].name),
12767 dwarf_form_name (die->attrs[i].form));
d97bc12b 12768
c906108c
SS
12769 switch (die->attrs[i].form)
12770 {
12771 case DW_FORM_ref_addr:
12772 case DW_FORM_addr:
d97bc12b 12773 fprintf_unfiltered (f, "address: ");
5af949e3 12774 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12775 break;
12776 case DW_FORM_block2:
12777 case DW_FORM_block4:
12778 case DW_FORM_block:
12779 case DW_FORM_block1:
d97bc12b 12780 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12781 break;
2dc7f7b3
TT
12782 case DW_FORM_exprloc:
12783 fprintf_unfiltered (f, "expression: size %u",
12784 DW_BLOCK (&die->attrs[i])->size);
12785 break;
10b3939b
DJ
12786 case DW_FORM_ref1:
12787 case DW_FORM_ref2:
12788 case DW_FORM_ref4:
d97bc12b 12789 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12790 (long) (DW_ADDR (&die->attrs[i])));
12791 break;
c906108c
SS
12792 case DW_FORM_data1:
12793 case DW_FORM_data2:
12794 case DW_FORM_data4:
ce5d95e1 12795 case DW_FORM_data8:
c906108c
SS
12796 case DW_FORM_udata:
12797 case DW_FORM_sdata:
43bbcdc2
PH
12798 fprintf_unfiltered (f, "constant: %s",
12799 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12800 break;
2dc7f7b3
TT
12801 case DW_FORM_sec_offset:
12802 fprintf_unfiltered (f, "section offset: %s",
12803 pulongest (DW_UNSND (&die->attrs[i])));
12804 break;
348e048f
DE
12805 case DW_FORM_sig8:
12806 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12807 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12808 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12809 else
12810 fprintf_unfiltered (f, "signatured type, offset: unknown");
12811 break;
c906108c 12812 case DW_FORM_string:
4bdf3d34 12813 case DW_FORM_strp:
8285870a 12814 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12815 DW_STRING (&die->attrs[i])
8285870a
JK
12816 ? DW_STRING (&die->attrs[i]) : "",
12817 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12818 break;
12819 case DW_FORM_flag:
12820 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12821 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12822 else
d97bc12b 12823 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12824 break;
2dc7f7b3
TT
12825 case DW_FORM_flag_present:
12826 fprintf_unfiltered (f, "flag: TRUE");
12827 break;
a8329558
KW
12828 case DW_FORM_indirect:
12829 /* the reader will have reduced the indirect form to
12830 the "base form" so this form should not occur */
d97bc12b 12831 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12832 break;
c906108c 12833 default:
d97bc12b 12834 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12835 die->attrs[i].form);
d97bc12b 12836 break;
c906108c 12837 }
d97bc12b 12838 fprintf_unfiltered (f, "\n");
c906108c
SS
12839 }
12840}
12841
f9aca02d 12842static void
d97bc12b 12843dump_die_for_error (struct die_info *die)
c906108c 12844{
d97bc12b
DE
12845 dump_die_shallow (gdb_stderr, 0, die);
12846}
12847
12848static void
12849dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12850{
12851 int indent = level * 4;
12852
12853 gdb_assert (die != NULL);
12854
12855 if (level >= max_level)
12856 return;
12857
12858 dump_die_shallow (f, indent, die);
12859
12860 if (die->child != NULL)
c906108c 12861 {
d97bc12b
DE
12862 print_spaces (indent, f);
12863 fprintf_unfiltered (f, " Children:");
12864 if (level + 1 < max_level)
12865 {
12866 fprintf_unfiltered (f, "\n");
12867 dump_die_1 (f, level + 1, max_level, die->child);
12868 }
12869 else
12870 {
12871 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12872 }
12873 }
12874
12875 if (die->sibling != NULL && level > 0)
12876 {
12877 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
12878 }
12879}
12880
d97bc12b
DE
12881/* This is called from the pdie macro in gdbinit.in.
12882 It's not static so gcc will keep a copy callable from gdb. */
12883
12884void
12885dump_die (struct die_info *die, int max_level)
12886{
12887 dump_die_1 (gdb_stdlog, 0, max_level, die);
12888}
12889
f9aca02d 12890static void
51545339 12891store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12892{
51545339 12893 void **slot;
c906108c 12894
51545339
DJ
12895 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12896
12897 *slot = die;
c906108c
SS
12898}
12899
93311388
DE
12900static int
12901is_ref_attr (struct attribute *attr)
c906108c 12902{
c906108c
SS
12903 switch (attr->form)
12904 {
12905 case DW_FORM_ref_addr:
c906108c
SS
12906 case DW_FORM_ref1:
12907 case DW_FORM_ref2:
12908 case DW_FORM_ref4:
613e1657 12909 case DW_FORM_ref8:
c906108c 12910 case DW_FORM_ref_udata:
93311388 12911 return 1;
c906108c 12912 default:
93311388 12913 return 0;
c906108c 12914 }
93311388
DE
12915}
12916
12917static unsigned int
12918dwarf2_get_ref_die_offset (struct attribute *attr)
12919{
12920 if (is_ref_attr (attr))
12921 return DW_ADDR (attr);
12922
12923 complaint (&symfile_complaints,
12924 _("unsupported die ref attribute form: '%s'"),
12925 dwarf_form_name (attr->form));
12926 return 0;
c906108c
SS
12927}
12928
43bbcdc2
PH
12929/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12930 * the value held by the attribute is not constant. */
a02abb62 12931
43bbcdc2 12932static LONGEST
a02abb62
JB
12933dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12934{
12935 if (attr->form == DW_FORM_sdata)
12936 return DW_SND (attr);
12937 else if (attr->form == DW_FORM_udata
12938 || attr->form == DW_FORM_data1
12939 || attr->form == DW_FORM_data2
12940 || attr->form == DW_FORM_data4
12941 || attr->form == DW_FORM_data8)
12942 return DW_UNSND (attr);
12943 else
12944 {
e2e0b3e5 12945 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
12946 dwarf_form_name (attr->form));
12947 return default_value;
12948 }
12949}
12950
03dd20cc 12951/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
12952 unit and add it to our queue.
12953 The result is non-zero if PER_CU was queued, otherwise the result is zero
12954 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 12955
348e048f 12956static int
03dd20cc
DJ
12957maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12958 struct dwarf2_per_cu_data *per_cu)
12959{
98bfdba5
PA
12960 /* We may arrive here during partial symbol reading, if we need full
12961 DIEs to process an unusual case (e.g. template arguments). Do
12962 not queue PER_CU, just tell our caller to load its DIEs. */
12963 if (dwarf2_per_objfile->reading_partial_symbols)
12964 {
12965 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12966 return 1;
12967 return 0;
12968 }
12969
03dd20cc
DJ
12970 /* Mark the dependence relation so that we don't flush PER_CU
12971 too early. */
12972 dwarf2_add_dependence (this_cu, per_cu);
12973
12974 /* If it's already on the queue, we have nothing to do. */
12975 if (per_cu->queued)
348e048f 12976 return 0;
03dd20cc
DJ
12977
12978 /* If the compilation unit is already loaded, just mark it as
12979 used. */
12980 if (per_cu->cu != NULL)
12981 {
12982 per_cu->cu->last_used = 0;
348e048f 12983 return 0;
03dd20cc
DJ
12984 }
12985
12986 /* Add it to the queue. */
12987 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12988
12989 return 1;
12990}
12991
12992/* Follow reference or signature attribute ATTR of SRC_DIE.
12993 On entry *REF_CU is the CU of SRC_DIE.
12994 On exit *REF_CU is the CU of the result. */
12995
12996static struct die_info *
12997follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12998 struct dwarf2_cu **ref_cu)
12999{
13000 struct die_info *die;
13001
13002 if (is_ref_attr (attr))
13003 die = follow_die_ref (src_die, attr, ref_cu);
13004 else if (attr->form == DW_FORM_sig8)
13005 die = follow_die_sig (src_die, attr, ref_cu);
13006 else
13007 {
13008 dump_die_for_error (src_die);
13009 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13010 (*ref_cu)->objfile->name);
13011 }
13012
13013 return die;
03dd20cc
DJ
13014}
13015
5c631832 13016/* Follow reference OFFSET.
673bfd45
DE
13017 On entry *REF_CU is the CU of the source die referencing OFFSET.
13018 On exit *REF_CU is the CU of the result.
13019 Returns NULL if OFFSET is invalid. */
f504f079 13020
f9aca02d 13021static struct die_info *
5c631832 13022follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13023{
10b3939b 13024 struct die_info temp_die;
f2f0e013 13025 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13026
348e048f
DE
13027 gdb_assert (cu->per_cu != NULL);
13028
98bfdba5
PA
13029 target_cu = cu;
13030
348e048f
DE
13031 if (cu->per_cu->from_debug_types)
13032 {
13033 /* .debug_types CUs cannot reference anything outside their CU.
13034 If they need to, they have to reference a signatured type via
13035 DW_FORM_sig8. */
13036 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13037 return NULL;
348e048f
DE
13038 }
13039 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13040 {
13041 struct dwarf2_per_cu_data *per_cu;
9a619af0 13042
45452591 13043 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13044
13045 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13046 if (maybe_queue_comp_unit (cu, per_cu))
13047 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13048
10b3939b
DJ
13049 target_cu = per_cu->cu;
13050 }
98bfdba5
PA
13051 else if (cu->dies == NULL)
13052 {
13053 /* We're loading full DIEs during partial symbol reading. */
13054 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13055 load_full_comp_unit (cu->per_cu, cu->objfile);
13056 }
c906108c 13057
f2f0e013 13058 *ref_cu = target_cu;
51545339 13059 temp_die.offset = offset;
5c631832
JK
13060 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13061}
10b3939b 13062
5c631832
JK
13063/* Follow reference attribute ATTR of SRC_DIE.
13064 On entry *REF_CU is the CU of SRC_DIE.
13065 On exit *REF_CU is the CU of the result. */
13066
13067static struct die_info *
13068follow_die_ref (struct die_info *src_die, struct attribute *attr,
13069 struct dwarf2_cu **ref_cu)
13070{
13071 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13072 struct dwarf2_cu *cu = *ref_cu;
13073 struct die_info *die;
13074
13075 die = follow_die_offset (offset, ref_cu);
13076 if (!die)
13077 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13078 "at 0x%x [in module %s]"),
13079 offset, src_die->offset, cu->objfile->name);
348e048f 13080
5c631832
JK
13081 return die;
13082}
13083
13084/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13085 value is intended for DW_OP_call*. */
13086
13087struct dwarf2_locexpr_baton
13088dwarf2_fetch_die_location_block (unsigned int offset,
13089 struct dwarf2_per_cu_data *per_cu)
13090{
13091 struct dwarf2_cu *cu = per_cu->cu;
13092 struct die_info *die;
13093 struct attribute *attr;
13094 struct dwarf2_locexpr_baton retval;
13095
13096 die = follow_die_offset (offset, &cu);
13097 if (!die)
13098 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13099 offset, per_cu->cu->objfile->name);
13100
13101 attr = dwarf2_attr (die, DW_AT_location, cu);
13102 if (!attr)
13103 {
13104 /* DWARF: "If there is no such attribute, then there is no effect.". */
13105
13106 retval.data = NULL;
13107 retval.size = 0;
13108 }
13109 else
13110 {
13111 if (!attr_form_is_block (attr))
13112 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13113 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13114 offset, per_cu->cu->objfile->name);
13115
13116 retval.data = DW_BLOCK (attr)->data;
13117 retval.size = DW_BLOCK (attr)->size;
13118 }
13119 retval.per_cu = cu->per_cu;
13120 return retval;
348e048f
DE
13121}
13122
13123/* Follow the signature attribute ATTR in SRC_DIE.
13124 On entry *REF_CU is the CU of SRC_DIE.
13125 On exit *REF_CU is the CU of the result. */
13126
13127static struct die_info *
13128follow_die_sig (struct die_info *src_die, struct attribute *attr,
13129 struct dwarf2_cu **ref_cu)
13130{
13131 struct objfile *objfile = (*ref_cu)->objfile;
13132 struct die_info temp_die;
13133 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13134 struct dwarf2_cu *sig_cu;
13135 struct die_info *die;
13136
13137 /* sig_type will be NULL if the signatured type is missing from
13138 the debug info. */
13139 if (sig_type == NULL)
13140 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13141 "at 0x%x [in module %s]"),
13142 src_die->offset, objfile->name);
13143
13144 /* If necessary, add it to the queue and load its DIEs. */
13145
13146 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13147 read_signatured_type (objfile, sig_type);
13148
13149 gdb_assert (sig_type->per_cu.cu != NULL);
13150
13151 sig_cu = sig_type->per_cu.cu;
13152 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13153 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13154 if (die)
13155 {
13156 *ref_cu = sig_cu;
13157 return die;
13158 }
13159
13160 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13161 "at 0x%x [in module %s]"),
13162 sig_type->type_offset, src_die->offset, objfile->name);
13163}
13164
13165/* Given an offset of a signatured type, return its signatured_type. */
13166
13167static struct signatured_type *
13168lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13169{
13170 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13171 unsigned int length, initial_length_size;
13172 unsigned int sig_offset;
13173 struct signatured_type find_entry, *type_sig;
13174
13175 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13176 sig_offset = (initial_length_size
13177 + 2 /*version*/
13178 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13179 + 1 /*address_size*/);
13180 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13181 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13182
13183 /* This is only used to lookup previously recorded types.
13184 If we didn't find it, it's our bug. */
13185 gdb_assert (type_sig != NULL);
13186 gdb_assert (offset == type_sig->offset);
13187
13188 return type_sig;
13189}
13190
13191/* Read in signatured type at OFFSET and build its CU and die(s). */
13192
13193static void
13194read_signatured_type_at_offset (struct objfile *objfile,
13195 unsigned int offset)
13196{
13197 struct signatured_type *type_sig;
13198
be391dca
TT
13199 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13200
348e048f
DE
13201 /* We have the section offset, but we need the signature to do the
13202 hash table lookup. */
13203 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13204
13205 gdb_assert (type_sig->per_cu.cu == NULL);
13206
13207 read_signatured_type (objfile, type_sig);
13208
13209 gdb_assert (type_sig->per_cu.cu != NULL);
13210}
13211
13212/* Read in a signatured type and build its CU and DIEs. */
13213
13214static void
13215read_signatured_type (struct objfile *objfile,
13216 struct signatured_type *type_sig)
13217{
1fd400ff 13218 gdb_byte *types_ptr;
348e048f
DE
13219 struct die_reader_specs reader_specs;
13220 struct dwarf2_cu *cu;
13221 ULONGEST signature;
13222 struct cleanup *back_to, *free_cu_cleanup;
13223 struct attribute *attr;
13224
1fd400ff
TT
13225 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13226 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13227
348e048f
DE
13228 gdb_assert (type_sig->per_cu.cu == NULL);
13229
13230 cu = xmalloc (sizeof (struct dwarf2_cu));
13231 memset (cu, 0, sizeof (struct dwarf2_cu));
13232 obstack_init (&cu->comp_unit_obstack);
13233 cu->objfile = objfile;
13234 type_sig->per_cu.cu = cu;
13235 cu->per_cu = &type_sig->per_cu;
13236
13237 /* If an error occurs while loading, release our storage. */
13238 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13239
13240 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13241 types_ptr, objfile->obfd);
13242 gdb_assert (signature == type_sig->signature);
13243
13244 cu->die_hash
13245 = htab_create_alloc_ex (cu->header.length / 12,
13246 die_hash,
13247 die_eq,
13248 NULL,
13249 &cu->comp_unit_obstack,
13250 hashtab_obstack_allocate,
13251 dummy_obstack_deallocate);
13252
13253 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13254 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13255
13256 init_cu_die_reader (&reader_specs, cu);
13257
13258 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13259 NULL /*parent*/);
13260
13261 /* We try not to read any attributes in this function, because not
13262 all objfiles needed for references have been loaded yet, and symbol
13263 table processing isn't initialized. But we have to set the CU language,
13264 or we won't be able to build types correctly. */
13265 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13266 if (attr)
13267 set_cu_language (DW_UNSND (attr), cu);
13268 else
13269 set_cu_language (language_minimal, cu);
13270
13271 do_cleanups (back_to);
13272
13273 /* We've successfully allocated this compilation unit. Let our caller
13274 clean it up when finished with it. */
13275 discard_cleanups (free_cu_cleanup);
13276
13277 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13278 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13279}
13280
c906108c
SS
13281/* Decode simple location descriptions.
13282 Given a pointer to a dwarf block that defines a location, compute
13283 the location and return the value.
13284
4cecd739
DJ
13285 NOTE drow/2003-11-18: This function is called in two situations
13286 now: for the address of static or global variables (partial symbols
13287 only) and for offsets into structures which are expected to be
13288 (more or less) constant. The partial symbol case should go away,
13289 and only the constant case should remain. That will let this
13290 function complain more accurately. A few special modes are allowed
13291 without complaint for global variables (for instance, global
13292 register values and thread-local values).
c906108c
SS
13293
13294 A location description containing no operations indicates that the
4cecd739 13295 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13296 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13297 callers will only want a very basic result and this can become a
13298 complaint.
c906108c 13299
c906108c
SS
13300 Note that stack[0] is unused except as a default error return.
13301 Note that stack overflow is not yet handled. */
13302
13303static CORE_ADDR
e7c27a73 13304decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13305{
e7c27a73 13306 struct objfile *objfile = cu->objfile;
c906108c
SS
13307 int i;
13308 int size = blk->size;
fe1b8b76 13309 gdb_byte *data = blk->data;
c906108c
SS
13310 CORE_ADDR stack[64];
13311 int stacki;
13312 unsigned int bytes_read, unsnd;
fe1b8b76 13313 gdb_byte op;
c906108c
SS
13314
13315 i = 0;
13316 stacki = 0;
13317 stack[stacki] = 0;
c906108c
SS
13318
13319 while (i < size)
13320 {
c906108c
SS
13321 op = data[i++];
13322 switch (op)
13323 {
f1bea926
JM
13324 case DW_OP_lit0:
13325 case DW_OP_lit1:
13326 case DW_OP_lit2:
13327 case DW_OP_lit3:
13328 case DW_OP_lit4:
13329 case DW_OP_lit5:
13330 case DW_OP_lit6:
13331 case DW_OP_lit7:
13332 case DW_OP_lit8:
13333 case DW_OP_lit9:
13334 case DW_OP_lit10:
13335 case DW_OP_lit11:
13336 case DW_OP_lit12:
13337 case DW_OP_lit13:
13338 case DW_OP_lit14:
13339 case DW_OP_lit15:
13340 case DW_OP_lit16:
13341 case DW_OP_lit17:
13342 case DW_OP_lit18:
13343 case DW_OP_lit19:
13344 case DW_OP_lit20:
13345 case DW_OP_lit21:
13346 case DW_OP_lit22:
13347 case DW_OP_lit23:
13348 case DW_OP_lit24:
13349 case DW_OP_lit25:
13350 case DW_OP_lit26:
13351 case DW_OP_lit27:
13352 case DW_OP_lit28:
13353 case DW_OP_lit29:
13354 case DW_OP_lit30:
13355 case DW_OP_lit31:
13356 stack[++stacki] = op - DW_OP_lit0;
13357 break;
13358
c906108c
SS
13359 case DW_OP_reg0:
13360 case DW_OP_reg1:
13361 case DW_OP_reg2:
13362 case DW_OP_reg3:
13363 case DW_OP_reg4:
13364 case DW_OP_reg5:
13365 case DW_OP_reg6:
13366 case DW_OP_reg7:
13367 case DW_OP_reg8:
13368 case DW_OP_reg9:
13369 case DW_OP_reg10:
13370 case DW_OP_reg11:
13371 case DW_OP_reg12:
13372 case DW_OP_reg13:
13373 case DW_OP_reg14:
13374 case DW_OP_reg15:
13375 case DW_OP_reg16:
13376 case DW_OP_reg17:
13377 case DW_OP_reg18:
13378 case DW_OP_reg19:
13379 case DW_OP_reg20:
13380 case DW_OP_reg21:
13381 case DW_OP_reg22:
13382 case DW_OP_reg23:
13383 case DW_OP_reg24:
13384 case DW_OP_reg25:
13385 case DW_OP_reg26:
13386 case DW_OP_reg27:
13387 case DW_OP_reg28:
13388 case DW_OP_reg29:
13389 case DW_OP_reg30:
13390 case DW_OP_reg31:
c906108c 13391 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13392 if (i < size)
13393 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13394 break;
13395
13396 case DW_OP_regx:
c906108c
SS
13397 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13398 i += bytes_read;
c906108c 13399 stack[++stacki] = unsnd;
4cecd739
DJ
13400 if (i < size)
13401 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13402 break;
13403
13404 case DW_OP_addr:
107d2387 13405 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13406 cu, &bytes_read);
107d2387 13407 i += bytes_read;
c906108c
SS
13408 break;
13409
13410 case DW_OP_const1u:
13411 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13412 i += 1;
13413 break;
13414
13415 case DW_OP_const1s:
13416 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13417 i += 1;
13418 break;
13419
13420 case DW_OP_const2u:
13421 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13422 i += 2;
13423 break;
13424
13425 case DW_OP_const2s:
13426 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13427 i += 2;
13428 break;
13429
13430 case DW_OP_const4u:
13431 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13432 i += 4;
13433 break;
13434
13435 case DW_OP_const4s:
13436 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13437 i += 4;
13438 break;
13439
13440 case DW_OP_constu:
13441 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13442 &bytes_read);
c906108c
SS
13443 i += bytes_read;
13444 break;
13445
13446 case DW_OP_consts:
13447 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13448 i += bytes_read;
13449 break;
13450
f1bea926
JM
13451 case DW_OP_dup:
13452 stack[stacki + 1] = stack[stacki];
13453 stacki++;
13454 break;
13455
c906108c
SS
13456 case DW_OP_plus:
13457 stack[stacki - 1] += stack[stacki];
13458 stacki--;
13459 break;
13460
13461 case DW_OP_plus_uconst:
13462 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13463 i += bytes_read;
13464 break;
13465
13466 case DW_OP_minus:
f1bea926 13467 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13468 stacki--;
13469 break;
13470
7a292a7a 13471 case DW_OP_deref:
7a292a7a 13472 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13473 this using GDB's address_class enum. This is valid for partial
13474 global symbols, although the variable's address will be bogus
13475 in the psymtab. */
7a292a7a 13476 if (i < size)
4d3c2250 13477 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13478 break;
13479
9d774e44 13480 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13481 /* The top of the stack has the offset from the beginning
13482 of the thread control block at which the variable is located. */
13483 /* Nothing should follow this operator, so the top of stack would
13484 be returned. */
4cecd739
DJ
13485 /* This is valid for partial global symbols, but the variable's
13486 address will be bogus in the psymtab. */
9d774e44 13487 if (i < size)
4d3c2250 13488 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13489 break;
13490
42be36b3
CT
13491 case DW_OP_GNU_uninit:
13492 break;
13493
c906108c 13494 default:
e2e0b3e5 13495 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13496 dwarf_stack_op_name (op, 1));
c906108c
SS
13497 return (stack[stacki]);
13498 }
13499 }
13500 return (stack[stacki]);
13501}
13502
13503/* memory allocation interface */
13504
c906108c 13505static struct dwarf_block *
7b5a2f43 13506dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13507{
13508 struct dwarf_block *blk;
13509
13510 blk = (struct dwarf_block *)
7b5a2f43 13511 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13512 return (blk);
13513}
13514
13515static struct abbrev_info *
f3dd6933 13516dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13517{
13518 struct abbrev_info *abbrev;
13519
f3dd6933
DJ
13520 abbrev = (struct abbrev_info *)
13521 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13522 memset (abbrev, 0, sizeof (struct abbrev_info));
13523 return (abbrev);
13524}
13525
13526static struct die_info *
b60c80d6 13527dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13528{
13529 struct die_info *die;
b60c80d6
DJ
13530 size_t size = sizeof (struct die_info);
13531
13532 if (num_attrs > 1)
13533 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13534
b60c80d6 13535 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13536 memset (die, 0, sizeof (struct die_info));
13537 return (die);
13538}
2e276125
JB
13539
13540\f
13541/* Macro support. */
13542
13543
13544/* Return the full name of file number I in *LH's file name table.
13545 Use COMP_DIR as the name of the current directory of the
13546 compilation. The result is allocated using xmalloc; the caller is
13547 responsible for freeing it. */
13548static char *
13549file_full_name (int file, struct line_header *lh, const char *comp_dir)
13550{
6a83a1e6
EZ
13551 /* Is the file number a valid index into the line header's file name
13552 table? Remember that file numbers start with one, not zero. */
13553 if (1 <= file && file <= lh->num_file_names)
13554 {
13555 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13556
6a83a1e6
EZ
13557 if (IS_ABSOLUTE_PATH (fe->name))
13558 return xstrdup (fe->name);
13559 else
13560 {
13561 const char *dir;
13562 int dir_len;
13563 char *full_name;
13564
13565 if (fe->dir_index)
13566 dir = lh->include_dirs[fe->dir_index - 1];
13567 else
13568 dir = comp_dir;
13569
13570 if (dir)
13571 {
13572 dir_len = strlen (dir);
13573 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13574 strcpy (full_name, dir);
13575 full_name[dir_len] = '/';
13576 strcpy (full_name + dir_len + 1, fe->name);
13577 return full_name;
13578 }
13579 else
13580 return xstrdup (fe->name);
13581 }
13582 }
2e276125
JB
13583 else
13584 {
6a83a1e6
EZ
13585 /* The compiler produced a bogus file number. We can at least
13586 record the macro definitions made in the file, even if we
13587 won't be able to find the file by name. */
13588 char fake_name[80];
9a619af0 13589
6a83a1e6 13590 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13591
6e70227d 13592 complaint (&symfile_complaints,
6a83a1e6
EZ
13593 _("bad file number in macro information (%d)"),
13594 file);
2e276125 13595
6a83a1e6 13596 return xstrdup (fake_name);
2e276125
JB
13597 }
13598}
13599
13600
13601static struct macro_source_file *
13602macro_start_file (int file, int line,
13603 struct macro_source_file *current_file,
13604 const char *comp_dir,
13605 struct line_header *lh, struct objfile *objfile)
13606{
13607 /* The full name of this source file. */
13608 char *full_name = file_full_name (file, lh, comp_dir);
13609
13610 /* We don't create a macro table for this compilation unit
13611 at all until we actually get a filename. */
13612 if (! pending_macros)
4a146b47 13613 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13614 objfile->macro_cache);
2e276125
JB
13615
13616 if (! current_file)
13617 /* If we have no current file, then this must be the start_file
13618 directive for the compilation unit's main source file. */
13619 current_file = macro_set_main (pending_macros, full_name);
13620 else
13621 current_file = macro_include (current_file, line, full_name);
13622
13623 xfree (full_name);
6e70227d 13624
2e276125
JB
13625 return current_file;
13626}
13627
13628
13629/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13630 followed by a null byte. */
13631static char *
13632copy_string (const char *buf, int len)
13633{
13634 char *s = xmalloc (len + 1);
9a619af0 13635
2e276125
JB
13636 memcpy (s, buf, len);
13637 s[len] = '\0';
2e276125
JB
13638 return s;
13639}
13640
13641
13642static const char *
13643consume_improper_spaces (const char *p, const char *body)
13644{
13645 if (*p == ' ')
13646 {
4d3c2250 13647 complaint (&symfile_complaints,
e2e0b3e5 13648 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13649 body);
2e276125
JB
13650
13651 while (*p == ' ')
13652 p++;
13653 }
13654
13655 return p;
13656}
13657
13658
13659static void
13660parse_macro_definition (struct macro_source_file *file, int line,
13661 const char *body)
13662{
13663 const char *p;
13664
13665 /* The body string takes one of two forms. For object-like macro
13666 definitions, it should be:
13667
13668 <macro name> " " <definition>
13669
13670 For function-like macro definitions, it should be:
13671
13672 <macro name> "() " <definition>
13673 or
13674 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13675
13676 Spaces may appear only where explicitly indicated, and in the
13677 <definition>.
13678
13679 The Dwarf 2 spec says that an object-like macro's name is always
13680 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13681 the space when the macro's definition is the empty string.
2e276125
JB
13682
13683 The Dwarf 2 spec says that there should be no spaces between the
13684 formal arguments in a function-like macro's formal argument list,
13685 but versions of GCC around March 2002 include spaces after the
13686 commas. */
13687
13688
13689 /* Find the extent of the macro name. The macro name is terminated
13690 by either a space or null character (for an object-like macro) or
13691 an opening paren (for a function-like macro). */
13692 for (p = body; *p; p++)
13693 if (*p == ' ' || *p == '(')
13694 break;
13695
13696 if (*p == ' ' || *p == '\0')
13697 {
13698 /* It's an object-like macro. */
13699 int name_len = p - body;
13700 char *name = copy_string (body, name_len);
13701 const char *replacement;
13702
13703 if (*p == ' ')
13704 replacement = body + name_len + 1;
13705 else
13706 {
4d3c2250 13707 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13708 replacement = body + name_len;
13709 }
6e70227d 13710
2e276125
JB
13711 macro_define_object (file, line, name, replacement);
13712
13713 xfree (name);
13714 }
13715 else if (*p == '(')
13716 {
13717 /* It's a function-like macro. */
13718 char *name = copy_string (body, p - body);
13719 int argc = 0;
13720 int argv_size = 1;
13721 char **argv = xmalloc (argv_size * sizeof (*argv));
13722
13723 p++;
13724
13725 p = consume_improper_spaces (p, body);
13726
13727 /* Parse the formal argument list. */
13728 while (*p && *p != ')')
13729 {
13730 /* Find the extent of the current argument name. */
13731 const char *arg_start = p;
13732
13733 while (*p && *p != ',' && *p != ')' && *p != ' ')
13734 p++;
13735
13736 if (! *p || p == arg_start)
4d3c2250 13737 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13738 else
13739 {
13740 /* Make sure argv has room for the new argument. */
13741 if (argc >= argv_size)
13742 {
13743 argv_size *= 2;
13744 argv = xrealloc (argv, argv_size * sizeof (*argv));
13745 }
13746
13747 argv[argc++] = copy_string (arg_start, p - arg_start);
13748 }
13749
13750 p = consume_improper_spaces (p, body);
13751
13752 /* Consume the comma, if present. */
13753 if (*p == ',')
13754 {
13755 p++;
13756
13757 p = consume_improper_spaces (p, body);
13758 }
13759 }
13760
13761 if (*p == ')')
13762 {
13763 p++;
13764
13765 if (*p == ' ')
13766 /* Perfectly formed definition, no complaints. */
13767 macro_define_function (file, line, name,
6e70227d 13768 argc, (const char **) argv,
2e276125
JB
13769 p + 1);
13770 else if (*p == '\0')
13771 {
13772 /* Complain, but do define it. */
4d3c2250 13773 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13774 macro_define_function (file, line, name,
6e70227d 13775 argc, (const char **) argv,
2e276125
JB
13776 p);
13777 }
13778 else
13779 /* Just complain. */
4d3c2250 13780 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13781 }
13782 else
13783 /* Just complain. */
4d3c2250 13784 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13785
13786 xfree (name);
13787 {
13788 int i;
13789
13790 for (i = 0; i < argc; i++)
13791 xfree (argv[i]);
13792 }
13793 xfree (argv);
13794 }
13795 else
4d3c2250 13796 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13797}
13798
13799
13800static void
13801dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13802 char *comp_dir, bfd *abfd,
e7c27a73 13803 struct dwarf2_cu *cu)
2e276125 13804{
fe1b8b76 13805 gdb_byte *mac_ptr, *mac_end;
2e276125 13806 struct macro_source_file *current_file = 0;
757a13d0
JK
13807 enum dwarf_macinfo_record_type macinfo_type;
13808 int at_commandline;
2e276125 13809
be391dca
TT
13810 dwarf2_read_section (dwarf2_per_objfile->objfile,
13811 &dwarf2_per_objfile->macinfo);
dce234bc 13812 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13813 {
e2e0b3e5 13814 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13815 return;
13816 }
13817
757a13d0
JK
13818 /* First pass: Find the name of the base filename.
13819 This filename is needed in order to process all macros whose definition
13820 (or undefinition) comes from the command line. These macros are defined
13821 before the first DW_MACINFO_start_file entry, and yet still need to be
13822 associated to the base file.
13823
13824 To determine the base file name, we scan the macro definitions until we
13825 reach the first DW_MACINFO_start_file entry. We then initialize
13826 CURRENT_FILE accordingly so that any macro definition found before the
13827 first DW_MACINFO_start_file can still be associated to the base file. */
13828
dce234bc
PP
13829 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13830 mac_end = dwarf2_per_objfile->macinfo.buffer
13831 + dwarf2_per_objfile->macinfo.size;
2e276125 13832
757a13d0 13833 do
2e276125 13834 {
2e276125
JB
13835 /* Do we at least have room for a macinfo type byte? */
13836 if (mac_ptr >= mac_end)
13837 {
757a13d0
JK
13838 /* Complaint is printed during the second pass as GDB will probably
13839 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13840 break;
2e276125
JB
13841 }
13842
13843 macinfo_type = read_1_byte (abfd, mac_ptr);
13844 mac_ptr++;
13845
13846 switch (macinfo_type)
13847 {
13848 /* A zero macinfo type indicates the end of the macro
13849 information. */
13850 case 0:
757a13d0
JK
13851 break;
13852
13853 case DW_MACINFO_define:
13854 case DW_MACINFO_undef:
13855 /* Only skip the data by MAC_PTR. */
13856 {
13857 unsigned int bytes_read;
13858
13859 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13860 mac_ptr += bytes_read;
9b1c24c8 13861 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13862 mac_ptr += bytes_read;
13863 }
13864 break;
13865
13866 case DW_MACINFO_start_file:
13867 {
13868 unsigned int bytes_read;
13869 int line, file;
13870
13871 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13872 mac_ptr += bytes_read;
13873 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13874 mac_ptr += bytes_read;
13875
13876 current_file = macro_start_file (file, line, current_file, comp_dir,
13877 lh, cu->objfile);
13878 }
13879 break;
13880
13881 case DW_MACINFO_end_file:
13882 /* No data to skip by MAC_PTR. */
13883 break;
13884
13885 case DW_MACINFO_vendor_ext:
13886 /* Only skip the data by MAC_PTR. */
13887 {
13888 unsigned int bytes_read;
13889
13890 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13891 mac_ptr += bytes_read;
9b1c24c8 13892 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
13893 mac_ptr += bytes_read;
13894 }
13895 break;
13896
13897 default:
13898 break;
13899 }
13900 } while (macinfo_type != 0 && current_file == NULL);
13901
13902 /* Second pass: Process all entries.
13903
13904 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13905 command-line macro definitions/undefinitions. This flag is unset when we
13906 reach the first DW_MACINFO_start_file entry. */
13907
dce234bc 13908 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
13909
13910 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13911 GDB is still reading the definitions from command line. First
13912 DW_MACINFO_start_file will need to be ignored as it was already executed
13913 to create CURRENT_FILE for the main source holding also the command line
13914 definitions. On first met DW_MACINFO_start_file this flag is reset to
13915 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13916
13917 at_commandline = 1;
13918
13919 do
13920 {
13921 /* Do we at least have room for a macinfo type byte? */
13922 if (mac_ptr >= mac_end)
13923 {
13924 dwarf2_macros_too_long_complaint ();
13925 break;
13926 }
13927
13928 macinfo_type = read_1_byte (abfd, mac_ptr);
13929 mac_ptr++;
13930
13931 switch (macinfo_type)
13932 {
13933 /* A zero macinfo type indicates the end of the macro
13934 information. */
13935 case 0:
13936 break;
2e276125
JB
13937
13938 case DW_MACINFO_define:
13939 case DW_MACINFO_undef:
13940 {
891d2f0b 13941 unsigned int bytes_read;
2e276125
JB
13942 int line;
13943 char *body;
13944
13945 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13946 mac_ptr += bytes_read;
9b1c24c8 13947 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
13948 mac_ptr += bytes_read;
13949
13950 if (! current_file)
757a13d0
JK
13951 {
13952 /* DWARF violation as no main source is present. */
13953 complaint (&symfile_complaints,
13954 _("debug info with no main source gives macro %s "
13955 "on line %d: %s"),
6e70227d
DE
13956 macinfo_type == DW_MACINFO_define ?
13957 _("definition") :
905e0470
PM
13958 macinfo_type == DW_MACINFO_undef ?
13959 _("undefinition") :
13960 _("something-or-other"), line, body);
757a13d0
JK
13961 break;
13962 }
13963 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 13964 complaint (&symfile_complaints,
757a13d0
JK
13965 _("debug info gives %s macro %s with %s line %d: %s"),
13966 at_commandline ? _("command-line") : _("in-file"),
905e0470 13967 macinfo_type == DW_MACINFO_define ?
6e70227d 13968 _("definition") :
905e0470
PM
13969 macinfo_type == DW_MACINFO_undef ?
13970 _("undefinition") :
13971 _("something-or-other"),
757a13d0
JK
13972 line == 0 ? _("zero") : _("non-zero"), line, body);
13973
13974 if (macinfo_type == DW_MACINFO_define)
13975 parse_macro_definition (current_file, line, body);
13976 else if (macinfo_type == DW_MACINFO_undef)
13977 macro_undef (current_file, line, body);
2e276125
JB
13978 }
13979 break;
13980
13981 case DW_MACINFO_start_file:
13982 {
891d2f0b 13983 unsigned int bytes_read;
2e276125
JB
13984 int line, file;
13985
13986 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13987 mac_ptr += bytes_read;
13988 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13989 mac_ptr += bytes_read;
13990
757a13d0
JK
13991 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13992 complaint (&symfile_complaints,
13993 _("debug info gives source %d included "
13994 "from %s at %s line %d"),
13995 file, at_commandline ? _("command-line") : _("file"),
13996 line == 0 ? _("zero") : _("non-zero"), line);
13997
13998 if (at_commandline)
13999 {
14000 /* This DW_MACINFO_start_file was executed in the pass one. */
14001 at_commandline = 0;
14002 }
14003 else
14004 current_file = macro_start_file (file, line,
14005 current_file, comp_dir,
14006 lh, cu->objfile);
2e276125
JB
14007 }
14008 break;
14009
14010 case DW_MACINFO_end_file:
14011 if (! current_file)
4d3c2250 14012 complaint (&symfile_complaints,
e2e0b3e5 14013 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14014 else
14015 {
14016 current_file = current_file->included_by;
14017 if (! current_file)
14018 {
14019 enum dwarf_macinfo_record_type next_type;
14020
14021 /* GCC circa March 2002 doesn't produce the zero
14022 type byte marking the end of the compilation
14023 unit. Complain if it's not there, but exit no
14024 matter what. */
14025
14026 /* Do we at least have room for a macinfo type byte? */
14027 if (mac_ptr >= mac_end)
14028 {
4d3c2250 14029 dwarf2_macros_too_long_complaint ();
2e276125
JB
14030 return;
14031 }
14032
14033 /* We don't increment mac_ptr here, so this is just
14034 a look-ahead. */
14035 next_type = read_1_byte (abfd, mac_ptr);
14036 if (next_type != 0)
4d3c2250 14037 complaint (&symfile_complaints,
e2e0b3e5 14038 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14039
14040 return;
14041 }
14042 }
14043 break;
14044
14045 case DW_MACINFO_vendor_ext:
14046 {
891d2f0b 14047 unsigned int bytes_read;
2e276125
JB
14048 int constant;
14049 char *string;
14050
14051 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14052 mac_ptr += bytes_read;
9b1c24c8 14053 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14054 mac_ptr += bytes_read;
14055
14056 /* We don't recognize any vendor extensions. */
14057 }
14058 break;
14059 }
757a13d0 14060 } while (macinfo_type != 0);
2e276125 14061}
8e19ed76
PS
14062
14063/* Check if the attribute's form is a DW_FORM_block*
14064 if so return true else false. */
14065static int
14066attr_form_is_block (struct attribute *attr)
14067{
14068 return (attr == NULL ? 0 :
14069 attr->form == DW_FORM_block1
14070 || attr->form == DW_FORM_block2
14071 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14072 || attr->form == DW_FORM_block
14073 || attr->form == DW_FORM_exprloc);
8e19ed76 14074}
4c2df51b 14075
c6a0999f
JB
14076/* Return non-zero if ATTR's value is a section offset --- classes
14077 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14078 You may use DW_UNSND (attr) to retrieve such offsets.
14079
14080 Section 7.5.4, "Attribute Encodings", explains that no attribute
14081 may have a value that belongs to more than one of these classes; it
14082 would be ambiguous if we did, because we use the same forms for all
14083 of them. */
3690dd37
JB
14084static int
14085attr_form_is_section_offset (struct attribute *attr)
14086{
14087 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14088 || attr->form == DW_FORM_data8
14089 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14090}
14091
14092
14093/* Return non-zero if ATTR's value falls in the 'constant' class, or
14094 zero otherwise. When this function returns true, you can apply
14095 dwarf2_get_attr_constant_value to it.
14096
14097 However, note that for some attributes you must check
14098 attr_form_is_section_offset before using this test. DW_FORM_data4
14099 and DW_FORM_data8 are members of both the constant class, and of
14100 the classes that contain offsets into other debug sections
14101 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14102 that, if an attribute's can be either a constant or one of the
14103 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14104 taken as section offsets, not constants. */
14105static int
14106attr_form_is_constant (struct attribute *attr)
14107{
14108 switch (attr->form)
14109 {
14110 case DW_FORM_sdata:
14111 case DW_FORM_udata:
14112 case DW_FORM_data1:
14113 case DW_FORM_data2:
14114 case DW_FORM_data4:
14115 case DW_FORM_data8:
14116 return 1;
14117 default:
14118 return 0;
14119 }
14120}
14121
4c2df51b
DJ
14122static void
14123dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14124 struct dwarf2_cu *cu)
4c2df51b 14125{
3690dd37 14126 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14127 /* ".debug_loc" may not exist at all, or the offset may be outside
14128 the section. If so, fall through to the complaint in the
14129 other branch. */
dce234bc 14130 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14131 {
0d53c4c4 14132 struct dwarf2_loclist_baton *baton;
4c2df51b 14133
4a146b47 14134 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14135 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
14136 baton->per_cu = cu->per_cu;
14137 gdb_assert (baton->per_cu);
4c2df51b 14138
be391dca
TT
14139 dwarf2_read_section (dwarf2_per_objfile->objfile,
14140 &dwarf2_per_objfile->loc);
14141
0d53c4c4
DJ
14142 /* We don't know how long the location list is, but make sure we
14143 don't run off the edge of the section. */
dce234bc
PP
14144 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14145 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
14146 baton->base_address = cu->base_address;
14147 if (cu->base_known == 0)
0d53c4c4 14148 complaint (&symfile_complaints,
e2e0b3e5 14149 _("Location list used without specifying the CU base address."));
4c2df51b 14150
768a979c 14151 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14152 SYMBOL_LOCATION_BATON (sym) = baton;
14153 }
14154 else
14155 {
14156 struct dwarf2_locexpr_baton *baton;
14157
4a146b47 14158 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14159 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14160 baton->per_cu = cu->per_cu;
14161 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14162
14163 if (attr_form_is_block (attr))
14164 {
14165 /* Note that we're just copying the block's data pointer
14166 here, not the actual data. We're still pointing into the
6502dd73
DJ
14167 info_buffer for SYM's objfile; right now we never release
14168 that buffer, but when we do clean up properly this may
14169 need to change. */
0d53c4c4
DJ
14170 baton->size = DW_BLOCK (attr)->size;
14171 baton->data = DW_BLOCK (attr)->data;
14172 }
14173 else
14174 {
14175 dwarf2_invalid_attrib_class_complaint ("location description",
14176 SYMBOL_NATURAL_NAME (sym));
14177 baton->size = 0;
14178 baton->data = NULL;
14179 }
6e70227d 14180
768a979c 14181 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14182 SYMBOL_LOCATION_BATON (sym) = baton;
14183 }
4c2df51b 14184}
6502dd73 14185
9aa1f1e3
TT
14186/* Return the OBJFILE associated with the compilation unit CU. If CU
14187 came from a separate debuginfo file, then the master objfile is
14188 returned. */
ae0d2f24
UW
14189
14190struct objfile *
14191dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14192{
9291a0cd 14193 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14194
14195 /* Return the master objfile, so that we can report and look up the
14196 correct file containing this variable. */
14197 if (objfile->separate_debug_objfile_backlink)
14198 objfile = objfile->separate_debug_objfile_backlink;
14199
14200 return objfile;
14201}
14202
14203/* Return the address size given in the compilation unit header for CU. */
14204
14205CORE_ADDR
14206dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14207{
14208 if (per_cu->cu)
14209 return per_cu->cu->header.addr_size;
14210 else
14211 {
14212 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14213 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14214 struct dwarf2_per_objfile *per_objfile
14215 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14216 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14217 struct comp_unit_head cu_header;
9a619af0 14218
ae0d2f24
UW
14219 memset (&cu_header, 0, sizeof cu_header);
14220 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14221 return cu_header.addr_size;
14222 }
14223}
14224
9eae7c52
TT
14225/* Return the offset size given in the compilation unit header for CU. */
14226
14227int
14228dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14229{
14230 if (per_cu->cu)
14231 return per_cu->cu->header.offset_size;
14232 else
14233 {
14234 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14235 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14236 struct dwarf2_per_objfile *per_objfile
14237 = objfile_data (objfile, dwarf2_objfile_data_key);
14238 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14239 struct comp_unit_head cu_header;
14240
14241 memset (&cu_header, 0, sizeof cu_header);
14242 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14243 return cu_header.offset_size;
14244 }
14245}
14246
9aa1f1e3
TT
14247/* Return the text offset of the CU. The returned offset comes from
14248 this CU's objfile. If this objfile came from a separate debuginfo
14249 file, then the offset may be different from the corresponding
14250 offset in the parent objfile. */
14251
14252CORE_ADDR
14253dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14254{
bb3fa9d0 14255 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14256
14257 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14258}
14259
348e048f
DE
14260/* Locate the .debug_info compilation unit from CU's objfile which contains
14261 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14262
14263static struct dwarf2_per_cu_data *
c764a876 14264dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14265 struct objfile *objfile)
14266{
14267 struct dwarf2_per_cu_data *this_cu;
14268 int low, high;
14269
ae038cb0
DJ
14270 low = 0;
14271 high = dwarf2_per_objfile->n_comp_units - 1;
14272 while (high > low)
14273 {
14274 int mid = low + (high - low) / 2;
9a619af0 14275
ae038cb0
DJ
14276 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14277 high = mid;
14278 else
14279 low = mid + 1;
14280 }
14281 gdb_assert (low == high);
14282 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14283 {
10b3939b 14284 if (low == 0)
8a3fe4f8
AC
14285 error (_("Dwarf Error: could not find partial DIE containing "
14286 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14287 (long) offset, bfd_get_filename (objfile->obfd));
14288
ae038cb0
DJ
14289 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14290 return dwarf2_per_objfile->all_comp_units[low-1];
14291 }
14292 else
14293 {
14294 this_cu = dwarf2_per_objfile->all_comp_units[low];
14295 if (low == dwarf2_per_objfile->n_comp_units - 1
14296 && offset >= this_cu->offset + this_cu->length)
c764a876 14297 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14298 gdb_assert (offset < this_cu->offset + this_cu->length);
14299 return this_cu;
14300 }
14301}
14302
10b3939b
DJ
14303/* Locate the compilation unit from OBJFILE which is located at exactly
14304 OFFSET. Raises an error on failure. */
14305
ae038cb0 14306static struct dwarf2_per_cu_data *
c764a876 14307dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14308{
14309 struct dwarf2_per_cu_data *this_cu;
9a619af0 14310
ae038cb0
DJ
14311 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14312 if (this_cu->offset != offset)
c764a876 14313 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14314 return this_cu;
14315}
14316
93311388
DE
14317/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14318
14319static struct dwarf2_cu *
14320alloc_one_comp_unit (struct objfile *objfile)
14321{
14322 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14323 cu->objfile = objfile;
14324 obstack_init (&cu->comp_unit_obstack);
14325 return cu;
14326}
14327
ae038cb0
DJ
14328/* Release one cached compilation unit, CU. We unlink it from the tree
14329 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14330 the caller is responsible for that.
14331 NOTE: DATA is a void * because this function is also used as a
14332 cleanup routine. */
ae038cb0
DJ
14333
14334static void
14335free_one_comp_unit (void *data)
14336{
14337 struct dwarf2_cu *cu = data;
14338
14339 if (cu->per_cu != NULL)
14340 cu->per_cu->cu = NULL;
14341 cu->per_cu = NULL;
14342
14343 obstack_free (&cu->comp_unit_obstack, NULL);
14344
14345 xfree (cu);
14346}
14347
72bf9492 14348/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14349 when we're finished with it. We can't free the pointer itself, but be
14350 sure to unlink it from the cache. Also release any associated storage
14351 and perform cache maintenance.
72bf9492
DJ
14352
14353 Only used during partial symbol parsing. */
14354
14355static void
14356free_stack_comp_unit (void *data)
14357{
14358 struct dwarf2_cu *cu = data;
14359
14360 obstack_free (&cu->comp_unit_obstack, NULL);
14361 cu->partial_dies = NULL;
ae038cb0
DJ
14362
14363 if (cu->per_cu != NULL)
14364 {
14365 /* This compilation unit is on the stack in our caller, so we
14366 should not xfree it. Just unlink it. */
14367 cu->per_cu->cu = NULL;
14368 cu->per_cu = NULL;
14369
14370 /* If we had a per-cu pointer, then we may have other compilation
14371 units loaded, so age them now. */
14372 age_cached_comp_units ();
14373 }
14374}
14375
14376/* Free all cached compilation units. */
14377
14378static void
14379free_cached_comp_units (void *data)
14380{
14381 struct dwarf2_per_cu_data *per_cu, **last_chain;
14382
14383 per_cu = dwarf2_per_objfile->read_in_chain;
14384 last_chain = &dwarf2_per_objfile->read_in_chain;
14385 while (per_cu != NULL)
14386 {
14387 struct dwarf2_per_cu_data *next_cu;
14388
14389 next_cu = per_cu->cu->read_in_chain;
14390
14391 free_one_comp_unit (per_cu->cu);
14392 *last_chain = next_cu;
14393
14394 per_cu = next_cu;
14395 }
14396}
14397
14398/* Increase the age counter on each cached compilation unit, and free
14399 any that are too old. */
14400
14401static void
14402age_cached_comp_units (void)
14403{
14404 struct dwarf2_per_cu_data *per_cu, **last_chain;
14405
14406 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14407 per_cu = dwarf2_per_objfile->read_in_chain;
14408 while (per_cu != NULL)
14409 {
14410 per_cu->cu->last_used ++;
14411 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14412 dwarf2_mark (per_cu->cu);
14413 per_cu = per_cu->cu->read_in_chain;
14414 }
14415
14416 per_cu = dwarf2_per_objfile->read_in_chain;
14417 last_chain = &dwarf2_per_objfile->read_in_chain;
14418 while (per_cu != NULL)
14419 {
14420 struct dwarf2_per_cu_data *next_cu;
14421
14422 next_cu = per_cu->cu->read_in_chain;
14423
14424 if (!per_cu->cu->mark)
14425 {
14426 free_one_comp_unit (per_cu->cu);
14427 *last_chain = next_cu;
14428 }
14429 else
14430 last_chain = &per_cu->cu->read_in_chain;
14431
14432 per_cu = next_cu;
14433 }
14434}
14435
14436/* Remove a single compilation unit from the cache. */
14437
14438static void
14439free_one_cached_comp_unit (void *target_cu)
14440{
14441 struct dwarf2_per_cu_data *per_cu, **last_chain;
14442
14443 per_cu = dwarf2_per_objfile->read_in_chain;
14444 last_chain = &dwarf2_per_objfile->read_in_chain;
14445 while (per_cu != NULL)
14446 {
14447 struct dwarf2_per_cu_data *next_cu;
14448
14449 next_cu = per_cu->cu->read_in_chain;
14450
14451 if (per_cu->cu == target_cu)
14452 {
14453 free_one_comp_unit (per_cu->cu);
14454 *last_chain = next_cu;
14455 break;
14456 }
14457 else
14458 last_chain = &per_cu->cu->read_in_chain;
14459
14460 per_cu = next_cu;
14461 }
14462}
14463
fe3e1990
DJ
14464/* Release all extra memory associated with OBJFILE. */
14465
14466void
14467dwarf2_free_objfile (struct objfile *objfile)
14468{
14469 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14470
14471 if (dwarf2_per_objfile == NULL)
14472 return;
14473
14474 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14475 free_cached_comp_units (NULL);
14476
9291a0cd
TT
14477 if (dwarf2_per_objfile->using_index)
14478 {
14479 int i;
14480
14481 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14482 {
14483 int j;
e254ef6a
DE
14484 struct dwarf2_per_cu_data *per_cu =
14485 dwarf2_per_objfile->all_comp_units[i];
9291a0cd 14486
e254ef6a 14487 if (!per_cu->v.quick->lines)
9291a0cd
TT
14488 continue;
14489
e254ef6a 14490 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
9291a0cd 14491 {
e254ef6a
DE
14492 if (per_cu->v.quick->file_names)
14493 xfree ((void *) per_cu->v.quick->file_names[j]);
14494 if (per_cu->v.quick->full_names)
14495 xfree ((void *) per_cu->v.quick->full_names[j]);
9291a0cd
TT
14496 }
14497
e254ef6a 14498 free_line_header (per_cu->v.quick->lines);
9291a0cd
TT
14499 }
14500 }
14501
fe3e1990
DJ
14502 /* Everything else should be on the objfile obstack. */
14503}
14504
1c379e20
DJ
14505/* A pair of DIE offset and GDB type pointer. We store these
14506 in a hash table separate from the DIEs, and preserve them
14507 when the DIEs are flushed out of cache. */
14508
14509struct dwarf2_offset_and_type
14510{
14511 unsigned int offset;
14512 struct type *type;
14513};
14514
14515/* Hash function for a dwarf2_offset_and_type. */
14516
14517static hashval_t
14518offset_and_type_hash (const void *item)
14519{
14520 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14521
1c379e20
DJ
14522 return ofs->offset;
14523}
14524
14525/* Equality function for a dwarf2_offset_and_type. */
14526
14527static int
14528offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14529{
14530 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14531 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14532
1c379e20
DJ
14533 return ofs_lhs->offset == ofs_rhs->offset;
14534}
14535
14536/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14537 table if necessary. For convenience, return TYPE.
14538
14539 The DIEs reading must have careful ordering to:
14540 * Not cause infite loops trying to read in DIEs as a prerequisite for
14541 reading current DIE.
14542 * Not trying to dereference contents of still incompletely read in types
14543 while reading in other DIEs.
14544 * Enable referencing still incompletely read in types just by a pointer to
14545 the type without accessing its fields.
14546
14547 Therefore caller should follow these rules:
14548 * Try to fetch any prerequisite types we may need to build this DIE type
14549 before building the type and calling set_die_type.
e71ec853 14550 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14551 possible before fetching more types to complete the current type.
14552 * Make the type as complete as possible before fetching more types. */
1c379e20 14553
f792889a 14554static struct type *
1c379e20
DJ
14555set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14556{
14557 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14558 struct objfile *objfile = cu->objfile;
14559 htab_t *type_hash_ptr;
1c379e20 14560
b4ba55a1
JB
14561 /* For Ada types, make sure that the gnat-specific data is always
14562 initialized (if not already set). There are a few types where
14563 we should not be doing so, because the type-specific area is
14564 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14565 where the type-specific area is used to store the floatformat).
14566 But this is not a problem, because the gnat-specific information
14567 is actually not needed for these types. */
14568 if (need_gnat_info (cu)
14569 && TYPE_CODE (type) != TYPE_CODE_FUNC
14570 && TYPE_CODE (type) != TYPE_CODE_FLT
14571 && !HAVE_GNAT_AUX_INFO (type))
14572 INIT_GNAT_SPECIFIC (type);
14573
673bfd45
DE
14574 if (cu->per_cu->from_debug_types)
14575 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14576 else
14577 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14578
14579 if (*type_hash_ptr == NULL)
f792889a 14580 {
673bfd45
DE
14581 *type_hash_ptr
14582 = htab_create_alloc_ex (127,
f792889a
DJ
14583 offset_and_type_hash,
14584 offset_and_type_eq,
14585 NULL,
673bfd45 14586 &objfile->objfile_obstack,
f792889a
DJ
14587 hashtab_obstack_allocate,
14588 dummy_obstack_deallocate);
f792889a 14589 }
1c379e20
DJ
14590
14591 ofs.offset = die->offset;
14592 ofs.type = type;
14593 slot = (struct dwarf2_offset_and_type **)
673bfd45 14594 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14595 if (*slot)
14596 complaint (&symfile_complaints,
14597 _("A problem internal to GDB: DIE 0x%x has type already set"),
14598 die->offset);
673bfd45 14599 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14600 **slot = ofs;
f792889a 14601 return type;
1c379e20
DJ
14602}
14603
673bfd45
DE
14604/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14605 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14606
14607static struct type *
673bfd45
DE
14608get_die_type_at_offset (unsigned int offset,
14609 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14610{
14611 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14612 htab_t type_hash;
f792889a 14613
673bfd45
DE
14614 if (per_cu->from_debug_types)
14615 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14616 else
14617 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14618 if (type_hash == NULL)
14619 return NULL;
1c379e20 14620
673bfd45 14621 ofs.offset = offset;
1c379e20
DJ
14622 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14623 if (slot)
14624 return slot->type;
14625 else
14626 return NULL;
14627}
14628
673bfd45
DE
14629/* Look up the type for DIE in the appropriate type_hash table,
14630 or return NULL if DIE does not have a saved type. */
14631
14632static struct type *
14633get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14634{
14635 return get_die_type_at_offset (die->offset, cu->per_cu);
14636}
14637
10b3939b
DJ
14638/* Add a dependence relationship from CU to REF_PER_CU. */
14639
14640static void
14641dwarf2_add_dependence (struct dwarf2_cu *cu,
14642 struct dwarf2_per_cu_data *ref_per_cu)
14643{
14644 void **slot;
14645
14646 if (cu->dependencies == NULL)
14647 cu->dependencies
14648 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14649 NULL, &cu->comp_unit_obstack,
14650 hashtab_obstack_allocate,
14651 dummy_obstack_deallocate);
14652
14653 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14654 if (*slot == NULL)
14655 *slot = ref_per_cu;
14656}
1c379e20 14657
f504f079
DE
14658/* Subroutine of dwarf2_mark to pass to htab_traverse.
14659 Set the mark field in every compilation unit in the
ae038cb0
DJ
14660 cache that we must keep because we are keeping CU. */
14661
10b3939b
DJ
14662static int
14663dwarf2_mark_helper (void **slot, void *data)
14664{
14665 struct dwarf2_per_cu_data *per_cu;
14666
14667 per_cu = (struct dwarf2_per_cu_data *) *slot;
14668 if (per_cu->cu->mark)
14669 return 1;
14670 per_cu->cu->mark = 1;
14671
14672 if (per_cu->cu->dependencies != NULL)
14673 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14674
14675 return 1;
14676}
14677
f504f079
DE
14678/* Set the mark field in CU and in every other compilation unit in the
14679 cache that we must keep because we are keeping CU. */
14680
ae038cb0
DJ
14681static void
14682dwarf2_mark (struct dwarf2_cu *cu)
14683{
14684 if (cu->mark)
14685 return;
14686 cu->mark = 1;
10b3939b
DJ
14687 if (cu->dependencies != NULL)
14688 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14689}
14690
14691static void
14692dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14693{
14694 while (per_cu)
14695 {
14696 per_cu->cu->mark = 0;
14697 per_cu = per_cu->cu->read_in_chain;
14698 }
72bf9492
DJ
14699}
14700
72bf9492
DJ
14701/* Trivial hash function for partial_die_info: the hash value of a DIE
14702 is its offset in .debug_info for this objfile. */
14703
14704static hashval_t
14705partial_die_hash (const void *item)
14706{
14707 const struct partial_die_info *part_die = item;
9a619af0 14708
72bf9492
DJ
14709 return part_die->offset;
14710}
14711
14712/* Trivial comparison function for partial_die_info structures: two DIEs
14713 are equal if they have the same offset. */
14714
14715static int
14716partial_die_eq (const void *item_lhs, const void *item_rhs)
14717{
14718 const struct partial_die_info *part_die_lhs = item_lhs;
14719 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14720
72bf9492
DJ
14721 return part_die_lhs->offset == part_die_rhs->offset;
14722}
14723
ae038cb0
DJ
14724static struct cmd_list_element *set_dwarf2_cmdlist;
14725static struct cmd_list_element *show_dwarf2_cmdlist;
14726
14727static void
14728set_dwarf2_cmd (char *args, int from_tty)
14729{
14730 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14731}
14732
14733static void
14734show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14735{
ae038cb0
DJ
14736 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14737}
14738
dce234bc
PP
14739/* If section described by INFO was mmapped, munmap it now. */
14740
14741static void
14742munmap_section_buffer (struct dwarf2_section_info *info)
14743{
14744 if (info->was_mmapped)
14745 {
14746#ifdef HAVE_MMAP
14747 intptr_t begin = (intptr_t) info->buffer;
14748 intptr_t map_begin = begin & ~(pagesize - 1);
14749 size_t map_length = info->size + begin - map_begin;
9a619af0 14750
dce234bc
PP
14751 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14752#else
14753 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14754 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14755#endif
14756 }
14757}
14758
14759/* munmap debug sections for OBJFILE, if necessary. */
14760
14761static void
c1bd65d0 14762dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14763{
14764 struct dwarf2_per_objfile *data = d;
9a619af0 14765
16be1145
DE
14766 /* This is sorted according to the order they're defined in to make it easier
14767 to keep in sync. */
dce234bc
PP
14768 munmap_section_buffer (&data->info);
14769 munmap_section_buffer (&data->abbrev);
14770 munmap_section_buffer (&data->line);
16be1145 14771 munmap_section_buffer (&data->loc);
dce234bc 14772 munmap_section_buffer (&data->macinfo);
16be1145 14773 munmap_section_buffer (&data->str);
dce234bc 14774 munmap_section_buffer (&data->ranges);
16be1145 14775 munmap_section_buffer (&data->types);
dce234bc
PP
14776 munmap_section_buffer (&data->frame);
14777 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14778 munmap_section_buffer (&data->gdb_index);
14779}
14780
14781\f
14782
14783/* The contents of the hash table we create when building the string
14784 table. */
14785struct strtab_entry
14786{
14787 offset_type offset;
14788 const char *str;
14789};
14790
14791/* Hash function for a strtab_entry. */
b89be57b 14792
9291a0cd
TT
14793static hashval_t
14794hash_strtab_entry (const void *e)
14795{
14796 const struct strtab_entry *entry = e;
14797 return mapped_index_string_hash (entry->str);
14798}
14799
14800/* Equality function for a strtab_entry. */
b89be57b 14801
9291a0cd
TT
14802static int
14803eq_strtab_entry (const void *a, const void *b)
14804{
14805 const struct strtab_entry *ea = a;
14806 const struct strtab_entry *eb = b;
14807 return !strcmp (ea->str, eb->str);
14808}
14809
14810/* Create a strtab_entry hash table. */
b89be57b 14811
9291a0cd
TT
14812static htab_t
14813create_strtab (void)
14814{
14815 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14816 xfree, xcalloc, xfree);
14817}
14818
14819/* Add a string to the constant pool. Return the string's offset in
14820 host order. */
b89be57b 14821
9291a0cd
TT
14822static offset_type
14823add_string (htab_t table, struct obstack *cpool, const char *str)
14824{
14825 void **slot;
14826 struct strtab_entry entry;
14827 struct strtab_entry *result;
14828
14829 entry.str = str;
14830 slot = htab_find_slot (table, &entry, INSERT);
14831 if (*slot)
14832 result = *slot;
14833 else
14834 {
14835 result = XNEW (struct strtab_entry);
14836 result->offset = obstack_object_size (cpool);
14837 result->str = str;
14838 obstack_grow_str0 (cpool, str);
14839 *slot = result;
14840 }
14841 return result->offset;
14842}
14843
14844/* An entry in the symbol table. */
14845struct symtab_index_entry
14846{
14847 /* The name of the symbol. */
14848 const char *name;
14849 /* The offset of the name in the constant pool. */
14850 offset_type index_offset;
14851 /* A sorted vector of the indices of all the CUs that hold an object
14852 of this name. */
14853 VEC (offset_type) *cu_indices;
14854};
14855
14856/* The symbol table. This is a power-of-2-sized hash table. */
14857struct mapped_symtab
14858{
14859 offset_type n_elements;
14860 offset_type size;
14861 struct symtab_index_entry **data;
14862};
14863
14864/* Hash function for a symtab_index_entry. */
b89be57b 14865
9291a0cd
TT
14866static hashval_t
14867hash_symtab_entry (const void *e)
14868{
14869 const struct symtab_index_entry *entry = e;
14870 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14871 sizeof (offset_type) * VEC_length (offset_type,
14872 entry->cu_indices),
14873 0);
14874}
14875
14876/* Equality function for a symtab_index_entry. */
b89be57b 14877
9291a0cd
TT
14878static int
14879eq_symtab_entry (const void *a, const void *b)
14880{
14881 const struct symtab_index_entry *ea = a;
14882 const struct symtab_index_entry *eb = b;
14883 int len = VEC_length (offset_type, ea->cu_indices);
14884 if (len != VEC_length (offset_type, eb->cu_indices))
14885 return 0;
14886 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14887 VEC_address (offset_type, eb->cu_indices),
14888 sizeof (offset_type) * len);
14889}
14890
14891/* Destroy a symtab_index_entry. */
b89be57b 14892
9291a0cd
TT
14893static void
14894delete_symtab_entry (void *p)
14895{
14896 struct symtab_index_entry *entry = p;
14897 VEC_free (offset_type, entry->cu_indices);
14898 xfree (entry);
14899}
14900
14901/* Create a hash table holding symtab_index_entry objects. */
b89be57b 14902
9291a0cd 14903static htab_t
3876f04e 14904create_symbol_hash_table (void)
9291a0cd
TT
14905{
14906 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14907 delete_symtab_entry, xcalloc, xfree);
14908}
14909
14910/* Create a new mapped symtab object. */
b89be57b 14911
9291a0cd
TT
14912static struct mapped_symtab *
14913create_mapped_symtab (void)
14914{
14915 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14916 symtab->n_elements = 0;
14917 symtab->size = 1024;
14918 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14919 return symtab;
14920}
14921
14922/* Destroy a mapped_symtab. */
b89be57b 14923
9291a0cd
TT
14924static void
14925cleanup_mapped_symtab (void *p)
14926{
14927 struct mapped_symtab *symtab = p;
14928 /* The contents of the array are freed when the other hash table is
14929 destroyed. */
14930 xfree (symtab->data);
14931 xfree (symtab);
14932}
14933
14934/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14935 the slot. */
b89be57b 14936
9291a0cd
TT
14937static struct symtab_index_entry **
14938find_slot (struct mapped_symtab *symtab, const char *name)
14939{
14940 offset_type index, step, hash = mapped_index_string_hash (name);
14941
14942 index = hash & (symtab->size - 1);
14943 step = ((hash * 17) & (symtab->size - 1)) | 1;
14944
14945 for (;;)
14946 {
14947 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14948 return &symtab->data[index];
14949 index = (index + step) & (symtab->size - 1);
14950 }
14951}
14952
14953/* Expand SYMTAB's hash table. */
b89be57b 14954
9291a0cd
TT
14955static void
14956hash_expand (struct mapped_symtab *symtab)
14957{
14958 offset_type old_size = symtab->size;
14959 offset_type i;
14960 struct symtab_index_entry **old_entries = symtab->data;
14961
14962 symtab->size *= 2;
14963 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14964
14965 for (i = 0; i < old_size; ++i)
14966 {
14967 if (old_entries[i])
14968 {
14969 struct symtab_index_entry **slot = find_slot (symtab,
14970 old_entries[i]->name);
14971 *slot = old_entries[i];
14972 }
14973 }
14974
14975 xfree (old_entries);
14976}
14977
14978/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14979 is the index of the CU in which the symbol appears. */
b89be57b 14980
9291a0cd
TT
14981static void
14982add_index_entry (struct mapped_symtab *symtab, const char *name,
14983 offset_type cu_index)
14984{
14985 struct symtab_index_entry **slot;
14986
14987 ++symtab->n_elements;
14988 if (4 * symtab->n_elements / 3 >= symtab->size)
14989 hash_expand (symtab);
14990
14991 slot = find_slot (symtab, name);
14992 if (!*slot)
14993 {
14994 *slot = XNEW (struct symtab_index_entry);
14995 (*slot)->name = name;
14996 (*slot)->cu_indices = NULL;
14997 }
14998 /* Don't push an index twice. Due to how we add entries we only
14999 have to check the last one. */
15000 if (VEC_empty (offset_type, (*slot)->cu_indices)
15001 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15002 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15003}
15004
15005/* Add a vector of indices to the constant pool. */
b89be57b 15006
9291a0cd 15007static offset_type
3876f04e 15008add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15009 struct symtab_index_entry *entry)
15010{
15011 void **slot;
15012
3876f04e 15013 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15014 if (!*slot)
15015 {
15016 offset_type len = VEC_length (offset_type, entry->cu_indices);
15017 offset_type val = MAYBE_SWAP (len);
15018 offset_type iter;
15019 int i;
15020
15021 *slot = entry;
15022 entry->index_offset = obstack_object_size (cpool);
15023
15024 obstack_grow (cpool, &val, sizeof (val));
15025 for (i = 0;
15026 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15027 ++i)
15028 {
15029 val = MAYBE_SWAP (iter);
15030 obstack_grow (cpool, &val, sizeof (val));
15031 }
15032 }
15033 else
15034 {
15035 struct symtab_index_entry *old_entry = *slot;
15036 entry->index_offset = old_entry->index_offset;
15037 entry = old_entry;
15038 }
15039 return entry->index_offset;
15040}
15041
15042/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15043 constant pool entries going into the obstack CPOOL. */
b89be57b 15044
9291a0cd
TT
15045static void
15046write_hash_table (struct mapped_symtab *symtab,
15047 struct obstack *output, struct obstack *cpool)
15048{
15049 offset_type i;
3876f04e 15050 htab_t symbol_hash_table;
9291a0cd
TT
15051 htab_t str_table;
15052
3876f04e 15053 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15054 str_table = create_strtab ();
3876f04e 15055
9291a0cd
TT
15056 /* We add all the index vectors to the constant pool first, to
15057 ensure alignment is ok. */
15058 for (i = 0; i < symtab->size; ++i)
15059 {
15060 if (symtab->data[i])
3876f04e 15061 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15062 }
15063
15064 /* Now write out the hash table. */
15065 for (i = 0; i < symtab->size; ++i)
15066 {
15067 offset_type str_off, vec_off;
15068
15069 if (symtab->data[i])
15070 {
15071 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15072 vec_off = symtab->data[i]->index_offset;
15073 }
15074 else
15075 {
15076 /* While 0 is a valid constant pool index, it is not valid
15077 to have 0 for both offsets. */
15078 str_off = 0;
15079 vec_off = 0;
15080 }
15081
15082 str_off = MAYBE_SWAP (str_off);
15083 vec_off = MAYBE_SWAP (vec_off);
15084
15085 obstack_grow (output, &str_off, sizeof (str_off));
15086 obstack_grow (output, &vec_off, sizeof (vec_off));
15087 }
15088
15089 htab_delete (str_table);
3876f04e 15090 htab_delete (symbol_hash_table);
9291a0cd
TT
15091}
15092
15093/* Write an address entry to ADDR_OBSTACK. The addresses are taken
15094 from PST; CU_INDEX is the index of the CU in the vector of all
15095 CUs. */
b89be57b 15096
9291a0cd
TT
15097static void
15098add_address_entry (struct objfile *objfile,
15099 struct obstack *addr_obstack, struct partial_symtab *pst,
15100 unsigned int cu_index)
15101{
15102 offset_type offset;
15103 char addr[8];
15104 CORE_ADDR baseaddr;
15105
1fd400ff
TT
15106 /* Don't bother recording empty ranges. */
15107 if (pst->textlow == pst->texthigh)
15108 return;
15109
9291a0cd
TT
15110 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15111
15112 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15113 obstack_grow (addr_obstack, addr, 8);
15114 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15115 obstack_grow (addr_obstack, addr, 8);
15116 offset = MAYBE_SWAP (cu_index);
15117 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15118}
15119
15120/* Add a list of partial symbols to SYMTAB. */
b89be57b 15121
9291a0cd
TT
15122static void
15123write_psymbols (struct mapped_symtab *symtab,
987d643c 15124 htab_t psyms_seen,
9291a0cd
TT
15125 struct partial_symbol **psymp,
15126 int count,
987d643c
TT
15127 offset_type cu_index,
15128 int is_static)
9291a0cd
TT
15129{
15130 for (; count-- > 0; ++psymp)
15131 {
987d643c
TT
15132 void **slot, *lookup;
15133
9291a0cd
TT
15134 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15135 error (_("Ada is not currently supported by the index"));
987d643c
TT
15136
15137 /* We only want to add a given psymbol once. However, we also
15138 want to account for whether it is global or static. So, we
15139 may add it twice, using slightly different values. */
15140 if (is_static)
15141 {
15142 uintptr_t val = 1 | (uintptr_t) *psymp;
15143
15144 lookup = (void *) val;
15145 }
15146 else
15147 lookup = *psymp;
15148
15149 /* Only add a given psymbol once. */
15150 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15151 if (!*slot)
15152 {
15153 *slot = lookup;
15154 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15155 }
9291a0cd
TT
15156 }
15157}
15158
15159/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15160 exception if there is an error. */
b89be57b 15161
9291a0cd
TT
15162static void
15163write_obstack (FILE *file, struct obstack *obstack)
15164{
15165 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15166 file)
15167 != obstack_object_size (obstack))
15168 error (_("couldn't data write to file"));
15169}
15170
15171/* Unlink a file if the argument is not NULL. */
b89be57b 15172
9291a0cd
TT
15173static void
15174unlink_if_set (void *p)
15175{
15176 char **filename = p;
15177 if (*filename)
15178 unlink (*filename);
15179}
15180
1fd400ff
TT
15181/* A helper struct used when iterating over debug_types. */
15182struct signatured_type_index_data
15183{
15184 struct objfile *objfile;
15185 struct mapped_symtab *symtab;
15186 struct obstack *types_list;
987d643c 15187 htab_t psyms_seen;
1fd400ff
TT
15188 int cu_index;
15189};
15190
15191/* A helper function that writes a single signatured_type to an
15192 obstack. */
b89be57b 15193
1fd400ff
TT
15194static int
15195write_one_signatured_type (void **slot, void *d)
15196{
15197 struct signatured_type_index_data *info = d;
15198 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15199 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15200 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15201 gdb_byte val[8];
15202
15203 write_psymbols (info->symtab,
987d643c 15204 info->psyms_seen,
1fd400ff 15205 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15206 psymtab->n_global_syms, info->cu_index,
15207 0);
1fd400ff 15208 write_psymbols (info->symtab,
987d643c 15209 info->psyms_seen,
1fd400ff 15210 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15211 psymtab->n_static_syms, info->cu_index,
15212 1);
1fd400ff
TT
15213
15214 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15215 obstack_grow (info->types_list, val, 8);
15216 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15217 obstack_grow (info->types_list, val, 8);
15218 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15219 obstack_grow (info->types_list, val, 8);
15220
15221 ++info->cu_index;
15222
15223 return 1;
15224}
15225
987d643c
TT
15226/* A cleanup function for an htab_t. */
15227
15228static void
15229cleanup_htab (void *arg)
15230{
15231 htab_delete (arg);
15232}
15233
9291a0cd 15234/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15235
9291a0cd
TT
15236static void
15237write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15238{
15239 struct cleanup *cleanup;
15240 char *filename, *cleanup_filename;
1fd400ff
TT
15241 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15242 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15243 int i;
15244 FILE *out_file;
15245 struct mapped_symtab *symtab;
15246 offset_type val, size_of_contents, total_len;
15247 struct stat st;
15248 char buf[8];
987d643c 15249 htab_t psyms_seen;
9291a0cd
TT
15250
15251 if (!objfile->psymtabs)
15252 return;
15253 if (dwarf2_per_objfile->using_index)
15254 error (_("Cannot use an index to create the index"));
15255
15256 if (stat (objfile->name, &st) < 0)
15257 perror_with_name (_("Could not stat"));
15258
15259 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15260 INDEX_SUFFIX, (char *) NULL);
15261 cleanup = make_cleanup (xfree, filename);
15262
15263 out_file = fopen (filename, "wb");
15264 if (!out_file)
15265 error (_("Can't open `%s' for writing"), filename);
15266
15267 cleanup_filename = filename;
15268 make_cleanup (unlink_if_set, &cleanup_filename);
15269
15270 symtab = create_mapped_symtab ();
15271 make_cleanup (cleanup_mapped_symtab, symtab);
15272
15273 obstack_init (&addr_obstack);
15274 make_cleanup_obstack_free (&addr_obstack);
15275
15276 obstack_init (&cu_list);
15277 make_cleanup_obstack_free (&cu_list);
15278
1fd400ff
TT
15279 obstack_init (&types_cu_list);
15280 make_cleanup_obstack_free (&types_cu_list);
15281
987d643c
TT
15282 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15283 NULL, xcalloc, xfree);
15284 make_cleanup (cleanup_htab, psyms_seen);
15285
1fd400ff
TT
15286 /* The list is already sorted, so we don't need to do additional
15287 work here. Also, the debug_types entries do not appear in
15288 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15289 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15290 {
e254ef6a
DE
15291 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15292 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd
TT
15293 gdb_byte val[8];
15294
15295 write_psymbols (symtab,
987d643c 15296 psyms_seen,
9291a0cd 15297 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15298 psymtab->n_global_syms, i,
15299 0);
9291a0cd 15300 write_psymbols (symtab,
987d643c 15301 psyms_seen,
9291a0cd 15302 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15303 psymtab->n_static_syms, i,
15304 1);
9291a0cd
TT
15305
15306 add_address_entry (objfile, &addr_obstack, psymtab, i);
15307
e254ef6a 15308 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15309 obstack_grow (&cu_list, val, 8);
e254ef6a 15310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15311 obstack_grow (&cu_list, val, 8);
15312 }
15313
1fd400ff
TT
15314 /* Write out the .debug_type entries, if any. */
15315 if (dwarf2_per_objfile->signatured_types)
15316 {
15317 struct signatured_type_index_data sig_data;
15318
15319 sig_data.objfile = objfile;
15320 sig_data.symtab = symtab;
15321 sig_data.types_list = &types_cu_list;
987d643c 15322 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15323 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15324 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15325 write_one_signatured_type, &sig_data);
15326 }
15327
9291a0cd
TT
15328 obstack_init (&constant_pool);
15329 make_cleanup_obstack_free (&constant_pool);
15330 obstack_init (&symtab_obstack);
15331 make_cleanup_obstack_free (&symtab_obstack);
15332 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15333
15334 obstack_init (&contents);
15335 make_cleanup_obstack_free (&contents);
1fd400ff 15336 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15337 total_len = size_of_contents;
15338
15339 /* The version number. */
987d643c 15340 val = MAYBE_SWAP (3);
9291a0cd
TT
15341 obstack_grow (&contents, &val, sizeof (val));
15342
15343 /* The offset of the CU list from the start of the file. */
15344 val = MAYBE_SWAP (total_len);
15345 obstack_grow (&contents, &val, sizeof (val));
15346 total_len += obstack_object_size (&cu_list);
15347
1fd400ff
TT
15348 /* The offset of the types CU list from the start of the file. */
15349 val = MAYBE_SWAP (total_len);
15350 obstack_grow (&contents, &val, sizeof (val));
15351 total_len += obstack_object_size (&types_cu_list);
15352
9291a0cd
TT
15353 /* The offset of the address table from the start of the file. */
15354 val = MAYBE_SWAP (total_len);
15355 obstack_grow (&contents, &val, sizeof (val));
15356 total_len += obstack_object_size (&addr_obstack);
15357
15358 /* The offset of the symbol table from the start of the file. */
15359 val = MAYBE_SWAP (total_len);
15360 obstack_grow (&contents, &val, sizeof (val));
15361 total_len += obstack_object_size (&symtab_obstack);
15362
15363 /* The offset of the constant pool from the start of the file. */
15364 val = MAYBE_SWAP (total_len);
15365 obstack_grow (&contents, &val, sizeof (val));
15366 total_len += obstack_object_size (&constant_pool);
15367
15368 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15369
15370 write_obstack (out_file, &contents);
15371 write_obstack (out_file, &cu_list);
1fd400ff 15372 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15373 write_obstack (out_file, &addr_obstack);
15374 write_obstack (out_file, &symtab_obstack);
15375 write_obstack (out_file, &constant_pool);
15376
15377 fclose (out_file);
15378
15379 /* We want to keep the file, so we set cleanup_filename to NULL
15380 here. See unlink_if_set. */
15381 cleanup_filename = NULL;
15382
15383 do_cleanups (cleanup);
15384}
15385
15386/* The mapped index file format is designed to be directly mmap()able
15387 on any architecture. In most cases, a datum is represented using a
15388 little-endian 32-bit integer value, called an offset_type. Big
15389 endian machines must byte-swap the values before using them.
15390 Exceptions to this rule are noted. The data is laid out such that
15391 alignment is always respected.
15392
15393 A mapped index consists of several sections.
15394
15395 1. The file header. This is a sequence of values, of offset_type
15396 unless otherwise noted:
987d643c
TT
15397
15398 [0] The version number, currently 3. Versions 1 and 2 are
15399 obsolete.
9291a0cd 15400 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15401 [2] The offset, from the start of the file, of the types CU list.
15402 Note that this section can be empty, in which case this offset will
15403 be equal to the next offset.
15404 [3] The offset, from the start of the file, of the address section.
15405 [4] The offset, from the start of the file, of the symbol table.
15406 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15407
15408 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15409 little-endian values, sorted by the CU offset. The first element
15410 in each pair is the offset of a CU in the .debug_info section. The
15411 second element in each pair is the length of that CU. References
15412 to a CU elsewhere in the map are done using a CU index, which is
15413 just the 0-based index into this table. Note that if there are
15414 type CUs, then conceptually CUs and type CUs form a single list for
15415 the purposes of CU indices.
15416
987d643c
TT
15417 3. The types CU list. This is a sequence of triplets of 64-bit
15418 little-endian values. In a triplet, the first value is the CU
15419 offset, the second value is the type offset in the CU, and the
15420 third value is the type signature. The types CU list is not
15421 sorted.
9291a0cd 15422
987d643c 15423 4. The address section. The address section consists of a sequence
9291a0cd
TT
15424 of address entries. Each address entry has three elements.
15425 [0] The low address. This is a 64-bit little-endian value.
15426 [1] The high address. This is a 64-bit little-endian value.
15427 [2] The CU index. This is an offset_type value.
15428
987d643c 15429 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15430 table is always a power of 2. The initial hash and the step are
15431 currently defined by the `find_slot' function.
15432
15433 Each slot in the hash table consists of a pair of offset_type
15434 values. The first value is the offset of the symbol's name in the
15435 constant pool. The second value is the offset of the CU vector in
15436 the constant pool.
15437
15438 If both values are 0, then this slot in the hash table is empty.
15439 This is ok because while 0 is a valid constant pool index, it
15440 cannot be a valid index for both a string and a CU vector.
15441
15442 A string in the constant pool is stored as a \0-terminated string,
15443 as you'd expect.
15444
15445 A CU vector in the constant pool is a sequence of offset_type
15446 values. The first value is the number of CU indices in the vector.
15447 Each subsequent value is the index of a CU in the CU list. This
15448 element in the hash table is used to indicate which CUs define the
15449 symbol.
15450
987d643c 15451 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15452 organized so that alignment is correct: CU vectors are stored
15453 first, followed by strings. */
11570e71 15454
9291a0cd
TT
15455static void
15456save_gdb_index_command (char *arg, int from_tty)
15457{
15458 struct objfile *objfile;
15459
15460 if (!arg || !*arg)
96d19272 15461 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15462
15463 ALL_OBJFILES (objfile)
15464 {
15465 struct stat st;
15466
15467 /* If the objfile does not correspond to an actual file, skip it. */
15468 if (stat (objfile->name, &st) < 0)
15469 continue;
15470
15471 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15472 if (dwarf2_per_objfile)
15473 {
15474 volatile struct gdb_exception except;
15475
15476 TRY_CATCH (except, RETURN_MASK_ERROR)
15477 {
15478 write_psymtabs_to_index (objfile, arg);
15479 }
15480 if (except.reason < 0)
15481 exception_fprintf (gdb_stderr, except,
15482 _("Error while writing index for `%s': "),
15483 objfile->name);
15484 }
15485 }
dce234bc
PP
15486}
15487
9291a0cd
TT
15488\f
15489
9eae7c52
TT
15490int dwarf2_always_disassemble;
15491
15492static void
15493show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15494 struct cmd_list_element *c, const char *value)
15495{
15496 fprintf_filtered (file, _("\
15497Whether to always disassemble DWARF expressions is %s.\n"),
15498 value);
15499}
15500
6502dd73
DJ
15501void _initialize_dwarf2_read (void);
15502
15503void
15504_initialize_dwarf2_read (void)
15505{
96d19272
JK
15506 struct cmd_list_element *c;
15507
dce234bc 15508 dwarf2_objfile_data_key
c1bd65d0 15509 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15510
1bedd215
AC
15511 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15512Set DWARF 2 specific variables.\n\
15513Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15514 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15515 0/*allow-unknown*/, &maintenance_set_cmdlist);
15516
1bedd215
AC
15517 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15518Show DWARF 2 specific variables\n\
15519Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15520 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15521 0/*allow-unknown*/, &maintenance_show_cmdlist);
15522
15523 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15524 &dwarf2_max_cache_age, _("\
15525Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15526Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15527A higher limit means that cached compilation units will be stored\n\
15528in memory longer, and more total memory will be used. Zero disables\n\
15529caching, which can slow down startup."),
2c5b56ce 15530 NULL,
920d2a44 15531 show_dwarf2_max_cache_age,
2c5b56ce 15532 &set_dwarf2_cmdlist,
ae038cb0 15533 &show_dwarf2_cmdlist);
d97bc12b 15534
9eae7c52
TT
15535 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15536 &dwarf2_always_disassemble, _("\
15537Set whether `info address' always disassembles DWARF expressions."), _("\
15538Show whether `info address' always disassembles DWARF expressions."), _("\
15539When enabled, DWARF expressions are always printed in an assembly-like\n\
15540syntax. When disabled, expressions will be printed in a more\n\
15541conversational style, when possible."),
15542 NULL,
15543 show_dwarf2_always_disassemble,
15544 &set_dwarf2_cmdlist,
15545 &show_dwarf2_cmdlist);
15546
d97bc12b
DE
15547 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15548Set debugging of the dwarf2 DIE reader."), _("\
15549Show debugging of the dwarf2 DIE reader."), _("\
15550When enabled (non-zero), DIEs are dumped after they are read in.\n\
15551The value is the maximum depth to print."),
15552 NULL,
15553 NULL,
15554 &setdebuglist, &showdebuglist);
9291a0cd 15555
96d19272 15556 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71
DE
15557 _("\
15558Save a .gdb-index file.\n\
15559Usage: save gdb-index DIRECTORY"),
96d19272
JK
15560 &save_cmdlist);
15561 set_cmd_completer (c, filename_completer);
6502dd73 15562}
This page took 2.325414 seconds and 4 git commands to generate.